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
|
/* 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 "pcapng.h"
#include "wtap_opttypes.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <jtckdint.h>
#include <wsutil/application_flavor.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/ws_padding_to.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_module.h"
#include "secrets-types.h"
#include "pcapng-darwin-custom.h"
#define NS_PER_S 1000000000U
static bool
pcapng_read(wtap *wth, wtap_rec *rec, int *err,
char **err_info, int64_t *data_offset);
static bool
pcapng_seek_read(wtap *wth, int64_t seek_off,
wtap_rec *rec, int *err, char **err_info);
static void
pcapng_close(wtap *wth);
static bool
pcapng_encap_is_ft_specific(int encap);
static bool
pcapng_write_if_descr_block(wtap_dumper *wdh, wtap_block_t int_data,
int *err, char **err_info);
/*
* Minimum block size = size of block header + size of block trailer.
*/
#define MIN_BLOCK_SIZE ((uint32_t)(sizeof(pcapng_block_header_t) + sizeof(uint32_t)))
/*
* Minimum SHB size = minimum block size + size of fixed length portion of SHB.
*/
#define MIN_SHB_SIZE ((uint32_t)(MIN_BLOCK_SIZE + sizeof(pcapng_section_header_block_t)))
/* pcapng: packet block file encoding (obsolete) */
typedef struct pcapng_packet_block_s {
uint16_t interface_id;
uint16_t drops_count;
uint32_t timestamp_high;
uint32_t timestamp_low;
uint32_t captured_len;
uint32_t packet_len;
/* ... Packet Data ... */
/* ... Padding ... */
/* ... Options ... */
} pcapng_packet_block_t;
/* pcapng: enhanced packet block file encoding */
typedef struct pcapng_enhanced_packet_block_s {
uint32_t interface_id;
uint32_t timestamp_high;
uint32_t timestamp_low;
uint32_t captured_len;
uint32_t 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 ((uint32_t)(MIN_BLOCK_SIZE + sizeof(pcapng_enhanced_packet_block_t)))
/* pcapng: simple packet block file encoding */
typedef struct pcapng_simple_packet_block_s {
uint32_t packet_len;
/* ... Packet Data ... */
/* ... Padding ... */
} pcapng_simple_packet_block_t;
/* pcapng: name resolution block file encoding */
typedef struct pcapng_name_resolution_block_s {
uint16_t record_type;
uint16_t record_len;
/* ... Record ... */
} pcapng_name_resolution_block_t;
/* pcapng: custom block file encoding */
typedef struct pcapng_custom_block_s {
uint32_t pen;
/* Custom data and options */
} pcapng_custom_block_t;
/* pcapng: legacy DPEB (Darwin Process Event Block) file encoding */
typedef struct pcapng_legacy_darwin_process_event_block_s {
uint32_t process_id;
/* Options */
} pcapng_legacy_darwin_process_event_block_t;
/*
* 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"
/* pcapng: common option header file encoding for every option type */
typedef struct pcapng_option_header_s {
uint16_t option_code;
uint16_t option_length;
/* ... x bytes Option Body ... */
/* ... Padding ... */
} pcapng_option_header_t;
struct pcapng_option {
uint16_t type;
uint16_t value_length;
};
/* 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 */
uint32_t ts_high; /* seconds since 1.1.1970 */
uint32_t ts_low; /* fraction of seconds, depends on if_tsresol */
uint32_t cap_len; /* data length in the file */
uint32_t packet_len; /* data length on the wire */
uint32_t interface_id; /* identifier of the interface. */
uint16_t 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 */
uint32_t cap_len; /* data length in the file */
uint32_t 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;
uint32_t snap_len;
uint64_t time_units_per_second;
int tsprecision;
int64_t tsoffset;
int fcslen;
uint8_t tsresol_binary;
} interface_info_t;
typedef struct {
unsigned 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 type, whether its internally handled and "read"
* and "write" routines.
*
* 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 structure's Buffer with
* data for which there's no place in the wtap_rec structure.
*
* A "write" routine takes a libwiretap record and out a block.
*/
static GHashTable *block_handlers;
void
register_pcapng_block_type_information(pcapng_block_type_information_t* handler)
{
if (handler == NULL) {
ws_warning("Attempt to register NULL plugin block type handler");
return;
}
/* Don't allow duplication of block types */
if (g_hash_table_lookup(block_handlers, GUINT_TO_POINTER(handler->type)) != NULL) {
if (handler->type == BLOCK_TYPE_LEGACY_DPIB) {
/* This special exception because stuff happened the way it had happened */
g_hash_table_remove(block_handlers, GUINT_TO_POINTER(handler->type));
}
else {
ws_warning("Attempt to register plugin for an existing block type 0x%08x not allowed",
handler->type);
return;
}
}
g_hash_table_insert(block_handlers, GUINT_TO_POINTER(handler->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 bool value of true if the attempt to
* write the option succeeds and false if the attempt to
* write the option gets an error.
*/
typedef struct {
option_parser parser;
option_sizer sizer;
option_writer writer;
} option_handler;
static GHashTable *custom_enterprise_handlers;
/* 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)
{
pcapng_block_type_information_t *handler;
handler = g_hash_table_lookup(block_handlers, GUINT_TO_POINTER(block_type));
if (handler != NULL)
return handler->internal;
else
return true;
}
GHashTable *
pcapng_create_option_handler_table(void)
{
return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
}
static GHashTable *
get_option_handlers(unsigned block_type)
{
pcapng_block_type_information_t *block_handler;
block_handler = g_hash_table_lookup(block_handlers, GUINT_TO_POINTER(block_type));
if (block_handler == NULL) {
/* No such block type. */
return NULL;
}
if (block_handler->option_handlers == NULL) {
/*
* This block type doesn't support options other than
* those supported by all blocks.
*/
return NULL;
}
return block_handler->option_handlers;
}
void
register_pcapng_option_handler(unsigned block_type, unsigned option_code,
option_parser parser,
option_sizer sizer,
option_writer writer)
{
GHashTable *option_handlers;
option_handler *handler;
/*
* Get the table of option handlers for this block type.
*/
option_handlers = get_option_handlers(block_type);
/*
* If there isn't one, the block only supports the standard options
* (if it supports options at all; the SPB doesn't).
*/
if (option_handlers == NULL)
return;
/*
* Is this combination already registered?
*/
handler = (option_handler *)g_hash_table_lookup(option_handlers,
GUINT_TO_POINTER(option_code));
if (handler != NULL) {
if (handler->parser == parser &&
handler->sizer == sizer &&
handler->writer == writer) {
/*
* Yes. This might be a case where multiple block types
* share the same table, and some code registers the same
* option for all of those blocks, which is OK. Just
* ignore it.
*/
return;
}
/*
* No. XXX - report this.
*/
return;
}
/*
* No - register it.
*/
handler = g_new(option_handler, 1);
handler->parser = parser;
handler->sizer = sizer;
handler->writer = writer;
g_hash_table_insert(option_handlers,
GUINT_TO_POINTER(option_code), handler);
}
void
pcapng_add_cb_section_info_data(section_info_t *section_info,
uint32_t pen, void *data)
{
g_hash_table_insert(section_info->custom_block_data,
GUINT_TO_POINTER(pen), data);
}
void *
pcapng_get_cb_section_info_data(section_info_t *section_info, uint32_t pen,
const section_info_funcs_t *funcs)
{
void *data;
if (section_info->custom_block_data == NULL) {
/*
* Create the table of custom block data for this section_info_t.
*
* XXX - there's no "g_uint_hash()" or "g_uint_equal()",
* so we use "g_direct_hash()" and "g_direct_equal()".
*/
section_info->custom_block_data = g_hash_table_new_full(g_direct_hash,
g_direct_equal,
NULL,
funcs->free);
/*
* The newly-created hash table is empty, so no point in looking
* for an element in it.
*/
data = NULL;
} else {
/*
* We have the hash table; look for the entry.
*/
data = g_hash_table_lookup(section_info->custom_block_data,
GUINT_TO_POINTER(pen));
}
if (data == NULL) {
/*
* No entry found - create a new one, and add it to the
* hash table.
*/
data = funcs->new();
g_hash_table_insert(section_info->custom_block_data,
GUINT_TO_POINTER(pen), data);
}
return data;
}
static void
pcapng_destroy_section_info(section_info_t *section_info)
{
if (section_info->interfaces != NULL)
g_array_free(section_info->interfaces, true);
if (section_info->custom_block_data != NULL)
g_hash_table_destroy(section_info->custom_block_data);
if (section_info->local_block_data != NULL)
g_hash_table_destroy(section_info->local_block_data);
}
void *
pcapng_get_lb_section_info_data(section_info_t *section_info,
uint32_t block_type,
const section_info_funcs_t *funcs)
{
void *data;
if (section_info->local_block_data == NULL) {
/*
* Create the table of local block data for this section_info_t.
*
* XXX - there's no "g_uint_hash()" or "g_uint_equal()",
* so we use "g_direct_hash()" and "g_direct_equal()".
*/
section_info->local_block_data = g_hash_table_new_full(g_direct_hash,
g_direct_equal,
NULL,
funcs->free);
/*
* The newly-created hash table is empty, so no point in looking
* for an element in it.
*/
data = NULL;
} else {
/*
* We have the hash table; look for the entry.
*/
data = g_hash_table_lookup(section_info->local_block_data,
GUINT_TO_POINTER(block_type));
}
if (data == NULL) {
/*
* No entry found - create a new one, and add it to the
* hash table.
*/
data = funcs->new();
g_hash_table_insert(section_info->local_block_data,
GUINT_TO_POINTER(block_type), data);
}
return data;
}
void
pcapng_process_uint8_option(wtapng_block_t *wblock,
uint16_t option_code, uint16_t option_length,
const uint8_t *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,
section_info_t *section_info,
pcapng_opt_byte_order_e byte_order,
uint16_t option_code, uint16_t option_length,
const uint8_t *option_content)
{
uint32_t uint32;
if (option_length == 4) {
/* Don't cast a uint8_t * into a uint32_t *--the
* uint8_t * 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(uint32_t));
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,
section_info_t *section_info,
pcapng_opt_byte_order_e byte_order,
uint16_t option_code, uint16_t option_length,
const uint8_t *option_content)
{
if (option_length == 8) {
uint32_t high, low;
uint64_t timestamp;
/* Don't cast a uint8_t * into a uint32_t *--the
* uint8_t * may not point to something that's
* aligned correctly.
*/
memcpy(&high, option_content, sizeof(uint32_t));
memcpy(&low, option_content + sizeof(uint32_t), sizeof(uint32_t));
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 = (uint64_t)high;
timestamp <<= 32;
timestamp += (uint64_t)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,
section_info_t *section_info,
pcapng_opt_byte_order_e byte_order,
uint16_t option_code, uint16_t option_length,
const uint8_t *option_content)
{
uint64_t uint64;
if (option_length == 8) {
/* Don't cast a uint8_t * into a uint64_t *--the
* uint8_t * may not point to something that's
* aligned correctly.
*/
memcpy(&uint64, option_content, sizeof(uint64_t));
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,
section_info_t *section_info,
pcapng_opt_byte_order_e byte_order,
uint16_t option_code, uint16_t option_length,
const uint8_t *option_content)
{
int64_t int64;
if (option_length == 8) {
/* Don't cast a int8_t * into a int64_t *--the
* uint8_t * may not point to something that's
* aligned correctly.
*/
memcpy(&int64, option_content, sizeof(int64_t));
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, uint16_t option_code,
uint16_t option_length, const uint8_t *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);
/*
* 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.
*/
if (wtap_block_add_string_option_owned(wblock->block, option_code, str) != WTAP_OPTTYPE_SUCCESS) {
g_free(str);
}
}
void
pcapng_process_bytes_option(wtapng_block_t *wblock, uint16_t option_code,
uint16_t option_length, const uint8_t *option_content)
{
wtap_block_add_bytes_option(wblock->block, option_code, (const char *)option_content, option_length);
}
static bool
pcapng_process_custom_option_common(section_info_t *section_info,
uint16_t option_length,
const uint8_t *option_content,
pcapng_opt_byte_order_e byte_order,
uint32_t *pen,
int *err, char **err_info)
{
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(uint32_t));
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;
}
return true;
}
static bool
pcapng_process_custom_string_option(wtapng_block_t *wblock,
section_info_t *section_info,
uint16_t option_code,
uint16_t option_length,
const uint8_t *option_content,
pcapng_opt_byte_order_e byte_order,
int *err, char **err_info)
{
uint32_t pen;
bool ret;
if (!pcapng_process_custom_option_common(section_info, option_length,
option_content, byte_order,
&pen, err, err_info)) {
return false;
}
ret = wtap_block_add_custom_string_option(wblock->block, option_code, pen, option_content + 4, option_length - 4) == WTAP_OPTTYPE_SUCCESS;
ws_debug("returning %d", ret);
return ret;
}
static bool
pcapng_process_custom_binary_option(wtapng_block_t *wblock,
section_info_t *section_info,
uint16_t option_code,
uint16_t option_length,
const uint8_t *option_content,
pcapng_opt_byte_order_e byte_order,
int *err, char **err_info)
{
uint32_t pen;
pcapng_custom_block_enterprise_handler_t* pen_handler;
bool ret;
if (!pcapng_process_custom_option_common(section_info, option_length,
option_content, byte_order,
&pen, err, err_info)) {
return false;
}
pen_handler = (pcapng_custom_block_enterprise_handler_t*)g_hash_table_lookup(custom_enterprise_handlers, GUINT_TO_POINTER(pen));
if (pen_handler != NULL)
{
ret = pen_handler->processor(wblock, section_info, option_code, option_content + 4, option_length - 4);
}
else
{
ret = wtap_block_add_custom_binary_option_from_data(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);
}
ws_debug("returning %d", ret);
return ret;
}
#ifdef HAVE_PLUGINS
static bool
pcapng_process_unhandled_option(wtapng_block_t *wblock,
section_info_t *section_info,
uint16_t option_code, uint16_t option_length,
const uint8_t *option_content,
int *err, char **err_info)
{
GHashTable *option_handlers;
option_handler *handler;
/*
* Get the table of option handlers for this block type.
*/
option_handlers = get_option_handlers(wblock->type);
/*
* Do we have a handler for this packet block option code?
*/
if (option_handlers != NULL &&
(handler = (option_handler *)g_hash_table_lookup(option_handlers,
GUINT_TO_POINTER((unsigned)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 bool
pcapng_process_unhandled_option(wtapng_block_t *wblock _U_,
section_info_t *section_info _U_,
uint16_t option_code _U_, uint16_t option_length _U_,
const uint8_t *option_content _U_,
int *err _U_, char **err_info _U_)
{
return true;
}
#endif
bool
pcapng_process_options(FILE_T fh, wtapng_block_t *wblock,
section_info_t *section_info,
unsigned opt_cont_buf_len,
bool (*process_option)(wtapng_block_t *,
section_info_t *,
uint16_t, uint16_t,
const uint8_t *,
int *, char **),
pcapng_opt_byte_order_e byte_order,
int *err, char **err_info)
{
uint8_t *option_content; /* Allocate as large as the options block */
unsigned opt_bytes_remaining;
const uint8_t *option_ptr;
const pcapng_option_header_t *oh;
uint16_t option_code, option_length;
unsigned 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 = (uint8_t *)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 = WS_ROUNDUP_4(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_STR_NO_COPY):
if (!pcapng_process_custom_string_option(wblock, section_info,
option_code,
option_length,
option_ptr,
byte_order,
err, err_info)) {
g_free(option_content);
return false;
}
break;
case(OPT_CUSTOM_BIN_COPY):
case(OPT_CUSTOM_BIN_NO_COPY):
if (!pcapng_process_custom_binary_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, section_info, option_code,
option_length, option_ptr,
err, err_info)) {
g_free(option_content);
return false;
}
break;
}
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 bool
pcapng_process_section_header_block_option(wtapng_block_t *wblock,
section_info_t *section_info,
uint16_t option_code,
uint16_t option_length,
const uint8_t *option_content,
int *err, char **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, 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, char **err_info)
{
bool byte_swapped;
uint16_t version_major;
uint16_t version_minor;
unsigned 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 = WS_ROUNDUP_4(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)) {
pcapng_destroy_section_info(section_info);
return PCAPNG_BLOCK_ERROR;
}
/*
* We don't return these to the caller in pcapng_read().
*/
wblock->internal = true;
return PCAPNG_BLOCK_OK;
}
static bool
pcapng_process_if_descr_block_option(wtapng_block_t *wblock,
section_info_t *section_info,
uint16_t option_code,
uint16_t option_length,
const uint8_t *option_content,
int *err, char **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_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.
* 02 34 56 FF FE 78 9A BC
*/
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;
case(OPT_IDB_TZONE): /* if_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_FILTER): /* if_filter */
if (option_length < 1) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: IDB interface filter 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?
*/
unsigned num_insns;
const uint8_t *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 (unsigned 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_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 option
* 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;
case(OPT_IDB_HARDWARE): /* if_hardware */
pcapng_process_string_option(wblock, option_code, option_length,
option_content);
break;
case(OPT_IDB_TXSPEED): /* if_txspeed */
pcapng_process_uint64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_IDB_RXSPEED): /* if_rxspeed */
pcapng_process_uint64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_IDB_IANA_TZNAME): /* if_iana_tzname */
pcapng_process_string_option(wblock, option_code, option_length,
option_content);
break;
default:
if (!pcapng_process_unhandled_option(wblock, section_info,
option_code, option_length,
option_content,
err, err_info))
return false;
break;
}
return true;
}
/* "Interface Description Block" */
static bool
pcapng_read_if_descr_block(wtap *wth, FILE_T fh, uint32_t block_type _U_,
uint32_t block_content_length,
section_info_t *section_info,
wtapng_block_t *wblock, int *err, char **err_info)
{
/* Default time stamp resolution is 10^6 */
uint64_t time_units_per_second = 1000000;
int tsprecision = 6;
unsigned opt_cont_buf_len;
pcapng_interface_description_block_t idb;
wtapng_if_descr_mandatory_t* if_descr_mand;
unsigned link_type;
uint8_t if_tsresol;
/*
* Is this block long enough to be an IDB?
*/
if (block_content_length < sizeof idb) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of an IDB is less than the minimum IDB content size %zu",
block_content_length, sizeof idb);
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 = block_content_length - sizeof idb;
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.
*/
uint8_t exponent;
exponent = (uint8_t)(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 = UINT64_C(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 more files than
* pcapng files, e.g. ERF files.)
*/
if (time_units_per_second >= NS_PER_S)
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.)
*/
uint64_t 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 (unsigned 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 > (((uint64_t)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 bool
pcapng_read_decryption_secrets_block(wtap *wth _U_, FILE_T fh,
uint32_t block_read _U_,
uint32_t block_content_length,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, char **err_info)
{
unsigned to_read;
pcapng_decryption_secrets_block_t dsb;
wtapng_dsb_mandatory_t *dsb_mand;
/*
* Is this block long enough to be an DSB?
*/
if (block_content_length < sizeof dsb) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of an DSB is less than the minimum DSB content size %zu",
block_content_length, sizeof dsb);
return false;
}
/* read block content */
if (!wtap_read_bytes(fh, &dsb, sizeof dsb, err, err_info)) {
ws_debug("failed to read DSB fixed portion");
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;
}
/*
* Is this block long enough to contain the secrets?
*/
if (block_content_length < sizeof dsb + dsb_mand->secrets_len) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of an DSB is less the size needed for the secrets in the DSB %zu",
block_content_length,
sizeof dsb + dsb_mand->secrets_len);
return false;
}
/*
* Sanity check: assume the secrets will never need to be 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 = (uint8_t *)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 secrets");
return false;
}
/* Skip past padding and discard options (not supported yet). */
to_read = block_content_length - sizeof dsb - 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_process_packet_block_option(wtapng_block_t *wblock,
section_info_t *section_info,
uint16_t option_code,
uint16_t option_length,
const uint8_t *option_content,
int *err, char **err_info)
{
uint64_t 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_PKT_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_PKT_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((uint8_t *)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_PKT_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_PKT_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_PKT_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_PKT_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((uint8_t *)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 uint8_t * into a uint64_t *--the
* uint8_t * may not point to something that's
* aligned correctly.
*/
memcpy(&tmp64, &option_content[1], sizeof(uint64_t));
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 uint8_t * into a uint64_t *--the
* uint8_t * may not point to something that's
* aligned correctly.
*/
memcpy(&tmp64, &option_content[1], sizeof(uint64_t));
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;
case(OPT_PKT_PROCIDTHRDID):
if (option_length != 8) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: packet block process id thread id option length %u is not 8",
option_length);
/* XXX - free anything? */
return false;
}
// XXX - It's two concatenated 32 bit unsigned integers
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, section_info,
option_code, option_length,
option_content,
err, err_info))
return false;
break;
}
return true;
}
static bool
pcapng_read_packet_block(wtap *wth _U_, FILE_T fh, uint32_t block_type,
uint32_t block_content_length,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, char **err_info)
{
unsigned block_read;
unsigned opt_cont_buf_len;
pcapng_enhanced_packet_block_t epb;
pcapng_packet_block_t pb;
wtapng_packet_t packet;
uint32_t padding;
uint32_t flags;
uint64_t tmp64;
interface_info_t iface_info;
uint64_t ts;
int pseudo_header_len;
int fcslen;
bool enhanced = (block_type == BLOCK_TYPE_EPB);
wblock->block = wtap_block_create(WTAP_BLOCK_PACKET);
if (enhanced) {
/*
* Is this block long enough to be an EPB?
*/
if (block_content_length < sizeof epb) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of an EPB is less than the minimum EPB content size %zu",
block_content_length, sizeof epb);
return false;
}
/* "Enhanced Packet Block" read fixed part */
if (!wtap_read_bytes(fh, &epb, sizeof epb, err, err_info)) {
ws_debug("failed to read EPB fixed portion");
return false;
}
block_read = (unsigned)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 (block_content_length < sizeof pb) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of a PB is less than the minimum PB content size %zu",
block_content_length, sizeof pb);
return false;
}
/* "Packet Block" read fixed part */
if (!wtap_read_bytes(fh, &pb, sizeof pb, err, err_info)) {
ws_debug("failed to read packet data");
return false;
}
block_read = (unsigned)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);
}
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 maximum supported length %u",
packet.cap_len,
wtap_max_snaplen_for_encap(iface_info.wtap_encap));
return false;
}
/*
* How much padding is there at the end of the packet data?
*/
padding = WS_PADDING_TO_4(packet.cap_len);
/*
* Is this block long enough to hold the packet data?
*/
if (enhanced) {
if (block_content_length < sizeof epb + packet.cap_len + padding) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of an EPB is too small for %u bytes of packet data",
block_content_length, packet.cap_len);
return false;
}
} else {
if (block_content_length < sizeof pb + 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",
block_content_length, packet.cap_len);
return false;
}
}
ws_debug("Need to read pseudo header of size %u",
pcap_get_phdr_size(iface_info.wtap_encap, &wblock->rec->rec_header.packet_header.pseudo_header));
wtap_setup_packet_rec(wblock->rec, iface_info.wtap_encap);
wblock->rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID;
ws_debug("encapsulation = %d (%s), pseudo header size = %u.",
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->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 = (((uint64_t)packet.ts_high) << 32) | ((uint64_t)packet.ts_low);
/* Convert it to seconds and nanoseconds. */
wblock->rec->ts.secs = (time_t)(ts / iface_info.time_units_per_second);
/* This can overflow if iface_info.time_units_per_seconds > (2^64 - 1) / 10^9;
* log10((2^64 - 1) / 10^9) ~ 10.266 and log2((2^64 - 1) / 10^9) ~ 32.103,
* so that's if the power of 10 exponent is greater than 10 or the power of 2
* exponent is greater than 32.
*
* We could test for and use 128 bit integers and platforms and compilers
* that have it (C23, and gcc, clang, and ICC on most 64-bit platforms).
* For C23, if we include <limits.h> and BITINT_MAXWIDTH is defined to be
* at least 128 (or even just 96) we could use unsigned _BitInt(128).
* If __SIZEOF_INT128__ is defined we can use unsigned __int128. Some
* testing (including with godbolt.org) suggests it's faster to check
* overflow and handle our two special cases.
*/
uint64_t ts_frac = ts % iface_info.time_units_per_second;
uint64_t ts_ns;
if (ckd_mul(&ts_ns, ts_frac, NS_PER_S)) {
/* We have 10^N where N > 10 or 2^N where N > 32. */
if (!iface_info.tsresol_binary) {
/* 10^N where N > 10, so this divides evenly. */
ws_assert(iface_info.time_units_per_second > NS_PER_S);
wblock->rec->ts.nsecs = (int)(ts_frac / (iface_info.time_units_per_second / NS_PER_S));
} else {
/* Multiplying a 64 bit integer by a 32 bit integer, then dividing
* by 2^N, where N > 32. */
uint64_t ts_frac_low = (ts_frac & 0xFFFFFFFF) * NS_PER_S;
uint64_t ts_frac_high = (ts_frac >> 32) * NS_PER_S;
// Add the carry.
ts_frac_high += ts_frac_low >> 32;
//ts_frac_low &= 0xFFFFFFFF;
ws_assert(iface_info.tsresol_binary > 32);
uint8_t high_shift = iface_info.tsresol_binary - 32;
wblock->rec->ts.nsecs = (int)(ts_frac_high >> high_shift);
}
} else {
wblock->rec->ts.nsecs = (int)(ts_ns / 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_bytes_buffer(fh, &wblock->rec->data,
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 = block_content_length - block_read;
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, (uint64_t)packet.drops_count);
}
pcap_read_post_process(false, iface_info.wtap_encap, wblock->rec,
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 bool
pcapng_read_simple_packet_block(wtap *wth _U_, FILE_T fh,
uint32_t block_type _U_,
uint32_t block_content_length,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, char **err_info)
{
pcapng_simple_packet_block_t spb;
wtapng_simple_packet_t simple_packet;
uint32_t padding;
interface_info_t iface_info;
int pseudo_header_len;
/*
* Is this block long enough to be an SPB?
*/
if (block_content_length < sizeof spb) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of an SPB is less than the minimum SPB content size %zu",
block_content_length, sizeof spb);
return false;
}
/* "Simple Packet Block" read fixed part */
if (!wtap_read_bytes(fh, &spb, sizeof spb, err, err_info)) {
ws_debug("failed to read SPB fixed portion");
return false;
}
if (section_info->byte_swapped) {
simple_packet.packet_len = GUINT32_SWAP_LE_BE(spb.packet_len);
} else {
simple_packet.packet_len = spb.packet_len;
}
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);
/*
* 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;
ws_debug("packet data: packet_len %u",
simple_packet.packet_len);
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 maximum supported length %u",
simple_packet.cap_len,
wtap_max_snaplen_for_encap(iface_info.wtap_encap));
return false;
}
/*
* How much padding is there at the end of the packet data?
*/
padding = WS_PADDING_TO_4(simple_packet.cap_len);
/*
* Is this block long enough to hold the packet data?
*/
if (block_content_length < sizeof spb + 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: block content length %u of an SPB is too small for %u bytes of packet data",
block_content_length, simple_packet.cap_len);
return false;
}
ws_debug("Need to read pseudo header of size %u",
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 */
wtap_setup_packet_rec(wblock->rec, iface_info.wtap_encap);
wblock->rec->presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID;
wblock->rec->rec_header.packet_header.interface_id = 0;
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, /* not a Nokia pcap - not a pcap at all */
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;
/* "Simple Packet Block" read capture data */
if (!wtap_read_bytes_buffer(fh, &wblock->rec->data,
simple_packet.cap_len - pseudo_header_len, err, err_info))
return false;
/* 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;
}
pcap_read_post_process(false, iface_info.wtap_encap, wblock->rec,
section_info->byte_swapped, iface_info.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
* (not that there will be any, as an SPB has no options). 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;
}
#define NRES_ENDOFRECORD 0
#define NRES_IP4RECORD 1
#define NRES_IP6RECORD 2
/* IPv6 + MAXDNSNAMELEN */
#define INITIAL_NRB_REC_SIZE (16 + MAXDNSNAMELEN)
/*
* 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, unsigned record_len, int *err,
char **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 bool
pcapng_process_name_resolution_block_option(wtapng_block_t *wblock,
section_info_t *section_info,
uint16_t option_code,
uint16_t option_length,
const uint8_t *option_content,
int *err, char **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, section_info,
option_code, option_length,
option_content,
err, err_info))
return false;
break;
}
return true;
}
static bool
pcapng_read_name_resolution_block(wtap *wth _U_, FILE_T fh,
uint32_t block_type _U_,
uint32_t block_content_length,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, char **err_info)
{
unsigned to_read;
pcapng_name_resolution_block_t nrb;
Buffer nrb_rec;
uint32_t v4_addr;
unsigned record_len, opt_cont_buf_len;
const char *namep;
int namelen;
wtapng_nrb_mandatory_t *nrb_mand;
/*
* Is this block long enough to be an NRB?
* There must be at least an "end of records" record.
*/
if (block_content_length < sizeof nrb) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of an NRB is less than the minimum NRB content size %zu",
block_content_length, sizeof nrb);
return false;
}
to_read = block_content_length;
ws_debug("total content %u bytes", block_content_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);
while (to_read != 0) {
unsigned padding;
/*
* There must be at least one record's worth of data
* here.
*/
if (to_read < sizeof nrb) {
ws_buffer_free(&nrb_rec);
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: %u bytes left in the block < NRB record header size %zu",
to_read, 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;
}
to_read -= (unsigned)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);
}
padding = WS_PADDING_TO_4(nrb.record_len); /* padding at end of record */
if (to_read < nrb.record_len + padding) {
ws_buffer_free(&nrb_rec);
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: %u bytes left in the block < NRB record length + padding %u",
to_read, nrb.record_len + padding);
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 IPv4 record length %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;
}
to_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 = (const 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.
*
* This will never return a value > record_len.
*/
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, MAXDNSNAMELEN);
nrb_mand->ipv4_addr_list = g_list_prepend(nrb_mand->ipv4_addr_list, tp);
}
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;
}
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;
}
to_read -= nrb.record_len;
for (namep = (const 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.
*
* This will never return a value > record_len.
*/
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, MAXDNSNAMELEN);
nrb_mand->ipv6_addr_list = g_list_prepend(nrb_mand->ipv6_addr_list, tp);
}
break;
default:
ws_debug("unknown record type 0x%x", nrb.record_type);
if (!wtap_read_bytes(fh, NULL, nrb.record_len, err, err_info)) {
ws_buffer_free(&nrb_rec);
return false;
}
to_read -= nrb.record_len;
break;
}
/* Skip padding */
if (!wtap_read_bytes(fh, NULL, padding, err, err_info)) {
ws_buffer_free(&nrb_rec);
return false;
}
to_read -= padding;
}
read_options:
/* 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)) {
ws_buffer_free(&nrb_rec);
return false;
}
ws_buffer_free(&nrb_rec);
/*
* We don't return these to the caller in pcapng_read().
*/
wblock->internal = true;
return true;
}
static bool
pcapng_process_interface_statistics_block_option(wtapng_block_t *wblock,
section_info_t *section_info,
uint16_t option_code,
uint16_t option_length,
const uint8_t *option_content,
int *err, char **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, section_info,
option_code, option_length,
option_content,
err, err_info))
return false;
break;
}
return true;
}
static bool
pcapng_read_interface_statistics_block(wtap *wth _U_, FILE_T fh,
uint32_t block_type _U_,
uint32_t block_content_length,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, char **err_info)
{
unsigned 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 (block_content_length < sizeof isb) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of an ISB is less than the minimum ISB content size %zu",
block_content_length, sizeof isb);
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 = block_content_length - sizeof isb;
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;
}
void
register_pcapng_custom_block_enterprise_handler(unsigned enterprise_number, pcapng_custom_block_enterprise_handler_t* handler)
{
g_hash_table_insert(custom_enterprise_handlers, GUINT_TO_POINTER(enterprise_number), handler);
}
static bool
pcapng_read_custom_block(wtap *wth _U_, FILE_T fh, uint32_t block_type,
uint32_t block_content_length,
section_info_t *section_info, wtapng_block_t *wblock,
int *err, char **err_info)
{
pcapng_custom_block_t cb;
uint32_t pen;
pcapng_custom_block_enterprise_handler_t* pen_handler;
/* Is this block long enough to be an CB? */
if (block_content_length < sizeof cb) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of a CB is less than the minimum CB content size %zu",
block_content_length, sizeof cb);
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;
}
uint32_t block_payload_length = block_content_length - sizeof cb;
ws_debug("pen %u, custom data and option length %u", pen, block_payload_length);
wtap_setup_custom_block_rec(wblock->rec, pen, block_payload_length,
(block_type == BLOCK_TYPE_CB_COPY));
pen_handler = (pcapng_custom_block_enterprise_handler_t*)g_hash_table_lookup(custom_enterprise_handlers, GUINT_TO_POINTER(pen));
if (pen_handler != NULL)
{
if (!pen_handler->parser(fh, section_info, wblock, err, err_info)) {
if (*err == WTAP_ERR_REC_MALFORMED) {
/* Allow the packet to be kept */
wblock->rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_NULL;
}
else {
return false;
}
}
}
else
{
ws_debug("unknown pen %u", pen);
if (!wtap_read_bytes_buffer(fh, &wblock->rec->data,
block_payload_length, err, err_info))
return false;
}
wblock->rec->block = wblock->block;
wblock->block = NULL;
/*
* We return these to the caller in pcapng_read().
*/
wblock->internal = false;
return true;
}
static bool
pcapng_read_systemd_journal_export_block(wtap *wth, FILE_T fh,
uint32_t block_type _U_,
uint32_t block_content_length,
section_info_t *section_info _U_,
wtapng_block_t *wblock,
int *err, char **err_info)
{
uint32_t entry_length;
uint64_t rt_ts;
bool have_ts = false;
if (block_content_length < MIN_SYSTEMD_JOURNAL_EXPORT_ENTRY_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: block content length %u of a systemd journal export is less than the minimum systemd journal export content size %u",
block_content_length,
MIN_SYSTEMD_JOURNAL_EXPORT_ENTRY_SIZE);
return false;
}
entry_length = block_content_length;
/* Includes padding bytes. */
if (!wtap_read_bytes_buffer(fh, &wblock->rec->data,
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->rec->data, entry_length+1);
char *buf_ptr = (char *) ws_buffer_start_ptr(&wblock->rec->data);
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)",
entry_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");
}
}
wtap_setup_systemd_journal_export_rec(wblock->rec);
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 bool
pcapng_read_unknown_block(FILE_T fh, guint32 block_content_length,
section_info_t *section_info _U_, wtapng_block_t *wblock,
int *err, char **err_info)
{
/* Skip the block content. */
if (!wtap_read_bytes(fh, NULL, block_content_length, 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 bool
pcapng_read_and_check_block_trailer(FILE_T fh, pcapng_block_header_t *bh,
section_info_t *section_info,
int *err, char **err_info)
{
uint32_t 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 = WS_ROUNDUP_4(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 bool
pcapng_read_block(wtap *wth, FILE_T fh,
section_info_t *section_info,
section_info_t *new_section_info,
wtapng_block_t *wblock,
int *err, char **err_info)
{
pcapng_block_type_information_t *handler;
block_return_val ret;
pcapng_block_header_t bh;
uint32_t block_padded_length;
uint32_t block_content_length;
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;
/*
* Get information for this block type, for use when setting the
* internal flag.
*/
handler = g_hash_table_lookup(block_handlers, GUINT_TO_POINTER(bh.block_type));
} 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);
}
if (bh.block_total_length < MIN_BLOCK_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of block is less than the minimum block size %u",
bh.block_total_length, MIN_BLOCK_SIZE);
return false;
}
/*
* 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 - where it says
* that "[The value of 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.)
*/
block_padded_length = WS_ROUNDUP_4(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 (block_padded_length < bh.block_total_length || block_padded_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;
}
/*
* Length of the contents of the block.
*/
block_content_length = block_padded_length - MIN_BLOCK_SIZE;
/*
* Do we have a handler for this block type?
*/
handler = g_hash_table_lookup(block_handlers, GUINT_TO_POINTER(bh.block_type));
if (handler != NULL) {
/* Yes - call it to read this block type. */
if (!handler->reader(wth, fh, bh.block_type,
block_content_length, section_info,
wblock, err, err_info))
return false;
} else {
ws_debug("Unknown block_type: 0x%08x (block ignored), block total length %u",
bh.block_type, block_padded_length);
if (!pcapng_read_unknown_block(fh, block_content_length,
section_info, wblock,
err, err_info))
return false;
}
}
/*
* 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;
}
static void
pcapng_process_shb(wtap *wth, pcapng_t *pcapng, section_info_t new_section, wtapng_block_t *wblock, const int64_t *data_offset)
{
/*
* Add this SHB to the table of SHBs.
*/
g_array_append_val(wth->shb_hdrs, wblock->block);
g_array_append_val(wth->shb_iface_to_global, wth->interface_data->len);
/*
* 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);
}
/* Process an IDB that we've just read. The contents of wblock are copied as needed. */
static bool
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);
uint8_t if_fcslen;
wtap_block_copy(int_data, wblock->block);
/* 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) {
/*
* No. Default to 0, meaning that time stamps in the file are
* absolute time stamps.
*/
iface_info.tsoffset = 0;
}
/*
* Did we get a time stamp precision option?
*/
iface_info.tsresol_binary = 0;
uint8_t if_tsresol;
if (wtap_block_get_uint8_option_value(wblock->block, OPT_IDB_TSRESOL,
&if_tsresol) == WTAP_OPTTYPE_SUCCESS) {
/* Is the timestamp resolution a power of two? */
if (if_tsresol & 0x80) {
/* Note that 0x80 and 0x80 mean the same thing, as 2^-0 == 10^-0 */
iface_info.tsresol_binary = if_tsresol & 0x7F;
}
}
g_array_append_val(section_info->interfaces, iface_info);
wtap_block_unref(wblock->block);
return true;
}
/* Process an NRB that we have just read. */
static bool
pcapng_process_nrb(wtap *wth, section_info_t *section_info _U_,
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);
/* Do not free wblock->block, it is consumed above */
return true;
}
/* Process a DSB that we have just read. */
static bool
pcapng_process_dsb(wtap *wth, section_info_t *section_info _U_,
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);
/* Do not free wblock->block, it is consumed above */
return true;
}
/* Process a ISB that we have just read. */
static bool
pcapng_process_isb(wtap *wth, section_info_t *section_info _U_,
wtapng_block_t *wblock)
{
wtapng_if_stats_mandatory_t *if_stats_mand_block, *if_stats_mand;
wtap_block_t if_stats;
wtap_block_t wtapng_if_descr;
wtapng_if_descr_mandatory_t *wtapng_if_descr_mand;
/*
* 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);
return true;
}
static void
pcapng_process_internal_block(wtap *wth, pcapng_t *pcapng, section_info_t *section, section_info_t new_section, wtapng_block_t *wblock, const int64_t *data_offset)
{
if (wblock->type == BLOCK_TYPE_SHB) {
pcapng_process_shb(wth, pcapng, new_section, wblock, data_offset);
} else {
pcapng_block_type_information_t* handler = g_hash_table_lookup(block_handlers, GUINT_TO_POINTER(wblock->type));
if (handler != NULL) {
/* XXX - Is it okay to not have a processor? */
if (handler->processor != NULL) {
handler->processor(wth, section, wblock);
}
} else {
/* XXX - improve handling of "unknown" blocks */
ws_debug("Unknown block type 0x%08x", wblock->type);
}
}
}
/* classic wtap: open capture file */
wtap_open_return_val
pcapng_open(wtap *wth, int *err, char **err_info)
{
wtapng_block_t wblock;
pcapng_t *pcapng;
pcapng_block_header_t bh;
int64_t 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.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, Sysdig meta event blocks, and Darwin Process 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->meta_events = g_array_new(false, false, sizeof(wtap_block_t));
wth->dpibs = 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.
*
* If this is a live capture, and we're reading the initially written
* header, we'll loop until we reach EOF. (If compressed, it might
* also set WTAP_ERR_SHORT_READ from the stream / frame end not being
* present until the file is closed.) So we'll need to clear that at
* some point before reading packets.
*/
while (!file_eof(wth->fh)) {
/* 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, 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 bool
pcapng_read(wtap *wth, wtap_rec *rec, int *err, char **err_info,
int64_t *data_offset)
{
pcapng_t *pcapng = (pcapng_t *)wth->priv;
section_info_t *current_section, new_section;
wtapng_block_t wblock;
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, 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 bool
pcapng_seek_read(wtap *wth, int64_t seek_off, wtap_rec *rec,
int *err, char **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 sections and pretty unlikely to have more than one.
*/
unsigned 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.rec = rec;
/* read the block */
if (!pcapng_read_block(wth, wth->random_fh, 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 (unsigned i = 0; i < pcapng->sections->len; i++) {
pcapng_destroy_section_info(&g_array_index(pcapng->sections,
section_info_t, i));
}
g_array_free(pcapng->sections, true);
}
/*
* As it says at the top of the file, an option sizer "calculates how many
* bytes the option's data requires, not including any padding bytes."
* Callers are responsible for rounding up to multiples of 4 bytes.
* compute_block_options_size() does that for each option in the block;
* option writers that call an option sizer (which helps ensure that the
* sizes are internally consistent) should do the same.
*/
static uint32_t pcapng_compute_string_option_size(wtap_optval_t *optval)
{
uint32_t size = 0;
size = (uint32_t)strlen(optval->stringval);
if (size > 65535) {
/*
* Too big to fit in the option.
* Don't write anything.
*
* XXX - truncate it? Report an error?
*/
size = 0;
}
return size;
}
#if 0
static uint32_t pcapng_compute_bytes_option_size(wtap_optval_t *optval)
{
uint32_t size = 0;
size = (uint32_t)g_bytes_get_size(optval->byteval) & 0xffff;
return size;
}
#endif
static uint32_t pcapng_compute_if_filter_option_size(wtap_optval_t *optval)
{
if_filter_opt_t* filter = &optval->if_filterval;
uint32_t size;
if (filter->type == if_filter_pcap) {
size = (uint32_t)(strlen(filter->data.filter_str) + 1) & 0xffff;
} else if (filter->type == if_filter_bpf) {
size = (uint32_t)((filter->data.bpf_prog.bpf_prog_len * 8) + 1) & 0xffff;
} else {
/* Unknown type; don't write it */
size = 0;
}
return size;
}
static uint32_t pcapng_compute_custom_string_option_size(wtap_optval_t *optval)
{
uint32_t size = 0;
size = (uint32_t)strlen(optval->custom_stringval.string) & 0xffff;
return size;
}
static uint32_t pcapng_compute_custom_binary_option_size(wtap_optval_t *optval)
{
size_t size;
/* PEN */
size = sizeof(uint32_t) + optval->custom_binaryval.data.custom_data_len;
if (size > 65535) {
size = 65535;
}
return (uint32_t)size;
}
static uint32_t pcapng_compute_packet_hash_option_size(wtap_optval_t *optval)
{
packet_hash_opt_t* hash = &optval->packet_hash;
uint32_t 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;
return size;
}
static uint32_t pcapng_compute_packet_verdict_option_size(wtap_optval_t *optval)
{
packet_verdict_opt_t* verdict = &optval->packet_verdictval;
uint32_t 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;
}
return size;
}
static bool
compute_block_option_size(wtap_block_t block _U_, unsigned 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;
uint32_t 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:
size = pcapng_compute_custom_string_option_size(optval);
break;
case OPT_CUSTOM_BIN_COPY:
size = pcapng_compute_custom_binary_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. */
if (options_size->compute_option_size) {
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. The length of this option is 4 bytes for the option
* header, plus the size of the option data, rounded up
* to a multiple of 4 bytes (32 bits).
*/
options_size->size += WS_ROUNDUP_4(4 + size);
}
return true; /* we always succeed */
}
uint32_t
pcapng_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 succeeds, 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 uint32_t compute_shb_option_size(wtap_block_t block _U_, unsigned option_id, wtap_opttype_e option_type _U_, wtap_optval_t* optval)
{
uint32_t 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 struct write_options_t
{
wtap_dumper *wdh;
pcapng_opt_byte_order_e byte_order;
write_option_func write_option;
int *err;
char **err_info;
}
write_options_t;
static bool pcapng_write_option_eofopt(wtap_dumper *wdh, int *err)
{
struct pcapng_option_header option_hdr;
/*
* Write end of options.
*
* OPT_EOFOPT is zero, so we don't need to know the byte order to
* be used, as both fields in the option header are zero and thus
* unchanged if byte-swapped.
*/
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 bool pcapng_write_uint8_option(wtap_dumper *wdh, unsigned option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)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 (!pcapng_write_padding(wdh, 3, err))
return false;
return true;
}
static bool pcapng_write_uint32_option(wtap_dumper *wdh, unsigned option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)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 bool pcapng_write_uint64_option(wtap_dumper *wdh, unsigned option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)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 bool pcapng_write_timestamp_option(wtap_dumper *wdh, unsigned option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
uint32_t high, low;
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)8;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return false;
high = (uint32_t)(optval->uint64val >> 32);
low = (uint32_t)(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 bool pcapng_write_string_option(wtap_dumper *wdh,
pcapng_opt_byte_order_e byte_order,
unsigned option_id,
wtap_optval_t *optval,
int *err, char **err_info)
{
struct pcapng_option_header option_hdr;
size_t size = strlen(optval->stringval);
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;
}
/* write option header */
/* String options don't consider pad bytes part of the length */
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)size;
switch (byte_order) {
case OPT_SECTION_BYTE_ORDER:
/* This is host byte order when writing, so nothing to do. */
break;
case OPT_BIG_ENDIAN:
option_hdr.type = GUINT16_TO_BE(option_hdr.type);
option_hdr.value_length = GUINT16_TO_BE(option_hdr.value_length);
break;
case OPT_LITTLE_ENDIAN:
option_hdr.type = GUINT16_TO_LE(option_hdr.type);
option_hdr.value_length = GUINT16_TO_LE(option_hdr.value_length);
break;
default:
/*
* This indicates somebody passed an invalid option to
* pcapng_write_options().
*
* Report this as an internal error.
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("pcapng: invalid byte order %d passed to pcapng_write_options()",
byte_order);
return true;
}
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return false;
if (!wtap_dump_file_write(wdh, optval->stringval, size, err))
return false;
/* write padding (if any) */
return pcapng_write_padding(wdh, WS_PADDING_TO_4(size), err);
}
#if 0
static bool pcapng_write_bytes_option(wtap_dumper *wdh, unsigned option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
size_t size = g_bytes_get_size(optval->byteval);
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 = (uint16_t)option_id;
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, optval->stringval, size, err))
return false;
/* write padding (if any) */
return pcapng_write_padding(wdh, WS_PADDING_TO_4(size), err);
}
static bool pcapng_write_ipv4_option(wtap_dumper *wdh, unsigned option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)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 bool pcapng_write_ipv6_option(wtap_dumper *wdh, unsigned option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)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 bool pcapng_write_if_filter_option(wtap_dumper *wdh, unsigned option_id, wtap_optval_t *optval, int *err)
{
if_filter_opt_t* filter = &optval->if_filterval;
uint32_t size;
uint8_t filter_type;
size_t filter_data_len;
struct pcapng_option_header option_hdr;
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 = (uint32_t)(filter_data_len + 1);
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) */
return pcapng_write_padding(wdh, WS_PADDING_TO_4(size), err);
}
static bool pcapng_write_custom_string_option(wtap_dumper *wdh,
pcapng_opt_byte_order_e byte_order,
unsigned option_id,
wtap_optval_t *optval,
int *err, char **err_info)
{
struct pcapng_option_header option_hdr;
size_t stringlen;
size_t size;
uint32_t pen;
if (option_id == OPT_CUSTOM_STR_NO_COPY)
return true;
ws_debug("PEN %u", optval->custom_stringval.pen);
stringlen = strlen(optval->custom_stringval.string);
size = sizeof(uint32_t) + stringlen;
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 and PEN */
/* String options don't consider pad bytes part of the length */
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)size;
pen = optval->custom_stringval.pen;
switch (byte_order) {
case OPT_SECTION_BYTE_ORDER:
/* This is host byte order when writing, so nothing to do. */
break;
case OPT_BIG_ENDIAN:
option_hdr.type = GUINT16_TO_BE(option_hdr.type);
option_hdr.value_length = GUINT16_TO_BE(option_hdr.value_length);
pen = GUINT32_TO_BE(pen);
break;
case OPT_LITTLE_ENDIAN:
option_hdr.type = GUINT16_TO_LE(option_hdr.type);
option_hdr.value_length = GUINT16_TO_LE(option_hdr.value_length);
pen = GUINT32_TO_LE(pen);
break;
default:
/*
* This indicates somebody passed an invalid option to
* pcapng_write_options().
*
* Report this as an internal error.
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("pcapng: invalid byte order %d passed to pcapng_write_options()",
byte_order);
return true;
}
if (!wtap_dump_file_write(wdh, &option_hdr, sizeof(struct pcapng_option_header), err))
return false;
if (!wtap_dump_file_write(wdh, &pen, sizeof(uint32_t), err))
return false;
/* write custom data */
if (!wtap_dump_file_write(wdh, optval->custom_stringval.string, stringlen, err)) {
return false;
}
ws_debug("Wrote custom option: type %u, length %u", option_hdr.type, option_hdr.value_length);
/* write padding (if any) */
return pcapng_write_padding(wdh, WS_PADDING_TO_4(size), err);
}
static bool pcapng_write_custom_binary_option(wtap_dumper *wdh,
pcapng_opt_byte_order_e byte_order,
unsigned option_id,
wtap_optval_t *optval,
int *err, char **err_info)
{
struct pcapng_option_header option_hdr;
size_t size;
uint32_t pen;
if (option_id == OPT_CUSTOM_BIN_NO_COPY)
return true;
ws_debug("PEN %u", optval->custom_binaryval.pen);
size = sizeof(uint32_t) + optval->custom_binaryval.data.custom_data_len;
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 and PEN */
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)size;
pen = optval->custom_binaryval.pen;
switch (byte_order) {
case OPT_SECTION_BYTE_ORDER:
/* This is host byte order when writing, so nothing to do. */
break;
case OPT_BIG_ENDIAN:
option_hdr.type = GUINT16_TO_BE(option_hdr.type);
option_hdr.value_length = GUINT16_TO_BE(option_hdr.value_length);
pen = GUINT32_TO_BE(pen);
break;
case OPT_LITTLE_ENDIAN:
option_hdr.type = GUINT16_TO_LE(option_hdr.type);
option_hdr.value_length = GUINT16_TO_LE(option_hdr.value_length);
pen = GUINT32_TO_LE(pen);
break;
default:
/*
* This indicates somebody passed an invalid option to
* pcapng_write_options().
*
* Report this as an internal error.
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("pcapng: invalid byte order %d passed to pcapng_write_options()",
byte_order);
return true;
}
if (!wtap_dump_file_write(wdh, &option_hdr, sizeof(struct pcapng_option_header), err))
return false;
if (!wtap_dump_file_write(wdh, &pen, sizeof(uint32_t), err))
return false;
/* write custom data */
if (!wtap_dump_file_write(wdh, optval->custom_binaryval.data.custom_data, optval->custom_binaryval.data.custom_data_len, err)) {
return false;
}
ws_debug("Wrote custom option: type %u, length %u", option_hdr.type, option_hdr.value_length);
/* write padding (if any) */
return pcapng_write_padding(wdh, WS_PADDING_TO_4(size), err);
}
static bool pcapng_write_packet_verdict_option(wtap_dumper *wdh, unsigned option_id, wtap_optval_t *optval, int *err)
{
packet_verdict_opt_t* verdict = &optval->packet_verdictval;
struct pcapng_option_header option_hdr;
uint8_t type;
size_t size;
size = pcapng_compute_packet_verdict_option_size(optval);
switch (verdict->type) {
case packet_verdict_hardware:
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 = (uint16_t)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(uint8_t), 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:
option_hdr.type = option_id;
option_hdr.value_length = (uint16_t)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(uint8_t), err))
return false;
if (!wtap_dump_file_write(wdh, &verdict->data.verdict_linux_ebpf_tc,
sizeof(uint64_t), err))
return false;
break;
case packet_verdict_linux_ebpf_xdp:
option_hdr.type = option_id;
option_hdr.value_length = (uint16_t)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(uint8_t), err))
return false;
if (!wtap_dump_file_write(wdh, &verdict->data.verdict_linux_ebpf_xdp,
sizeof(uint64_t), err))
return false;
break;
default:
/* Unknown - don't write it out. */
return true;
}
/* write padding (if any) */
return pcapng_write_padding(wdh, WS_PADDING_TO_4(size), err);
}
static bool 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;
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) */
return pcapng_write_padding(wdh, WS_PADDING_TO_4(size), err);
}
static bool write_block_option(wtap_block_t block,
unsigned 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 write function for others.
*/
switch(option_id)
{
case OPT_COMMENT:
if (!pcapng_write_string_option(options->wdh, options->byte_order,
option_id, optval,
options->err, options->err_info))
return false;
break;
case OPT_CUSTOM_STR_COPY:
if (!pcapng_write_custom_string_option(options->wdh,
options->byte_order,
option_id, optval,
options->err, options->err_info))
return false;
break;
case OPT_CUSTOM_BIN_COPY:
if (!pcapng_write_custom_binary_option(options->wdh,
options->byte_order,
option_id, optval,
options->err,
options->err_info))
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, options->err_info))
return false;
break;
}
return true;
}
bool
pcapng_write_options(wtap_dumper *wdh, pcapng_opt_byte_order_e byte_order,
wtap_block_t block, write_option_func write_option,
int *err, char **err_info)
{
write_options_t options;
options.wdh = wdh;
options.byte_order = byte_order;
options.write_option = write_option;
options.err = err;
options.err_info = err_info;
if (!wtap_block_foreach_option(block, write_block_option, &options))
return false;
/* Write end of options */
return pcapng_write_option_eofopt(wdh, err);
}
static bool write_wtap_shb_option(wtap_dumper *wdh, wtap_block_t block _U_,
unsigned option_id,
wtap_opttype_e option_type _U_,
wtap_optval_t *optval,
int *err, char **err_info)
{
switch(option_id)
{
case OPT_SHB_HARDWARE:
case OPT_SHB_OS:
case OPT_SHB_USERAPPL:
if (!pcapng_write_string_option(wdh, OPT_SECTION_BYTE_ORDER,
option_id, optval, err, err_info))
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 bool
pcapng_write_section_header_block(wtap_dumper *wdh, int *err, char **err_info)
{
uint32_t block_content_length;
pcapng_section_header_block_t shb;
uint32_t 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);
}
block_content_length = (uint32_t)sizeof(shb);
options_size = 0;
if (wdh_shb) {
ws_debug("Have shb_hdr");
/* Compute size of all the options */
options_size = pcapng_compute_options_size(wdh_shb, compute_shb_option_size);
block_content_length += options_size;
}
/* write block header */
if (!pcapng_write_block_header(wdh, BLOCK_TYPE_SHB, block_content_length,
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 (!pcapng_write_options(wdh, OPT_SECTION_BYTE_ORDER,
wdh_shb, write_wtap_shb_option,
err, err_info))
return false;
}
}
/* write block footer */
return pcapng_write_block_footer(wdh, block_content_length, err);
}
/* 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 uint32_t
compute_epb_option_size(wtap_block_t block _U_, unsigned option_id, wtap_opttype_e option_type _U_, wtap_optval_t* optval)
{
uint32_t size;
switch(option_id)
{
case OPT_PKT_FLAGS:
size = 4;
break;
case OPT_PKT_HASH:
size = pcapng_compute_packet_hash_option_size(optval);
break;
case OPT_PKT_DROPCOUNT:
size = 8;
break;
case OPT_PKT_PACKETID:
size = 8;
break;
case OPT_PKT_QUEUE:
size = 4;
break;
case OPT_PKT_VERDICT:
size = pcapng_compute_packet_verdict_option_size(optval);
break;
case OPT_PKT_PROCIDTHRDID:
size = 8;
break;
default:
/* Unknown options - size by datatype? */
size = pcapng_compute_epb_legacy_darwin_size(option_id, optval);
break;
}
return size;
}
static bool write_wtap_epb_option(wtap_dumper *wdh, wtap_block_t block _U_,
unsigned option_id,
wtap_opttype_e option_type _U_,
wtap_optval_t *optval,
int *err, char **err_info _U_)
{
switch(option_id)
{
case OPT_PKT_FLAGS:
if (!pcapng_write_uint32_option(wdh, OPT_PKT_FLAGS, optval, err))
return false;
break;
case OPT_PKT_HASH:
if (!pcapng_write_packet_hash_option(wdh, OPT_PKT_HASH, optval, err))
return false;
break;
case OPT_PKT_DROPCOUNT:
if (!pcapng_write_uint64_option(wdh, OPT_PKT_DROPCOUNT, optval, err))
return false;
break;
case OPT_PKT_PACKETID:
if (!pcapng_write_uint64_option(wdh, OPT_PKT_PACKETID, optval, err))
return false;
break;
case OPT_PKT_QUEUE:
if (!pcapng_write_uint32_option(wdh, OPT_PKT_QUEUE, optval, err))
return false;
break;
case OPT_PKT_VERDICT:
if (!pcapng_write_packet_verdict_option(wdh, OPT_PKT_VERDICT, optval, err))
return false;
break;
case OPT_PKT_PROCIDTHRDID:
if (!pcapng_write_uint64_option(wdh, OPT_PKT_PROCIDTHRDID, optval, err))
return false;
break;
default: {
/* Check if this is legacy darwin option */
if (!pcapng_write_epb_legacy_darwin_option(wdh, block, option_id, option_type, optval, err, err_info))
return false;
/* Unknown options - write by datatype? */
break;
}
}
return true; /* success */
}
static bool
pcapng_write_simple_packet_block(wtap_dumper* wdh, const wtap_rec* rec,
int* err, char** err_info _U_)
{
const union wtap_pseudo_header* pseudo_header = &rec->rec_header.packet_header.pseudo_header;
uint32_t block_content_length;
pcapng_simple_packet_block_t spb;
uint32_t pad_len;
uint32_t phdr_len;
/* 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 = pcap_get_phdr_size(rec->rec_header.packet_header.pkt_encap, pseudo_header);
pad_len = WS_PADDING_TO_4(phdr_len + rec->rec_header.packet_header.caplen);
/* write (simple) packet block header */
block_content_length = (uint32_t)sizeof(spb) + phdr_len + rec->rec_header.packet_header.caplen + pad_len;
if (!pcapng_write_block_header(wdh, BLOCK_TYPE_SPB, block_content_length,
err))
return false;
/* write block fixed content */
spb.packet_len = rec->rec_header.packet_header.len + phdr_len;
if (!wtap_dump_file_write(wdh, &spb, sizeof spb, 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, ws_buffer_start_ptr(&rec->data), rec->rec_header.packet_header.caplen, err))
return false;
/* write padding (if any) */
if (!pcapng_write_padding(wdh, pad_len, err))
return false;
/* write block footer */
return pcapng_write_block_footer(wdh, block_content_length, err);
}
static bool
pcapng_write_enhanced_packet_block(wtap_dumper *wdh, const wtap_rec *rec,
int *err, char **err_info)
{
const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
uint32_t block_content_length;
pcapng_enhanced_packet_block_t epb;
uint32_t options_size = 0;
uint64_t ts;
uint32_t pad_len;
uint32_t phdr_len;
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 = pcap_get_phdr_size(rec->rec_header.packet_header.pkt_encap, pseudo_header);
pad_len = WS_PADDING_TO_4(phdr_len + rec->rec_header.packet_header.caplen);
if (rec->block != NULL) {
/* Compute size of all the options */
options_size = pcapng_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;
if (rec->presence_flags & WTAP_HAS_SECTION_NUMBER && wdh->shb_iface_to_global) {
/*
* In the extremely unlikely event this overflows we give the
* wrong interface ID.
*/
epb.interface_id += g_array_index(wdh->shb_iface_to_global, unsigned, rec->section_number);
}
} 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, err_info)) {
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 */
block_content_length = (uint32_t)sizeof(epb) + phdr_len + rec->rec_header.packet_header.caplen + pad_len + options_size;
if (!pcapng_write_block_header(wdh, BLOCK_TYPE_EPB, block_content_length,
err))
return false;
/* write block fixed content */
/* Calculate the time stamp as a 64-bit integer. */
/* TODO - This can't overflow currently because we don't allow greater
* than nanosecond resolution, but if and when we do, we need to check for
* overflow. Normally it shouldn't, but what if there was a time shift?
*/
ts = ((uint64_t)rec->ts.secs) * int_data_mand->time_units_per_second +
(((uint64_t)rec->ts.nsecs) * int_data_mand->time_units_per_second) / NS_PER_S;
/*
* Split the 64-bit timestamp into two 32-bit pieces, using
* the time stamp resolution for the interface.
*/
epb.timestamp_high = (uint32_t)(ts >> 32);
epb.timestamp_low = (uint32_t)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, ws_buffer_start_ptr(&rec->data), rec->rec_header.packet_header.caplen, err))
return false;
/* write padding (if any) */
if (!pcapng_write_padding(wdh, pad_len, err))
return false;
/* Write options, if we have any */
if (options_size != 0) {
if (!pcapng_write_options(wdh, OPT_SECTION_BYTE_ORDER,
rec->block, write_wtap_epb_option,
err, err_info))
return false;
}
/* write block footer */
return pcapng_write_block_footer(wdh, block_content_length, err);
}
static bool
pcapng_write_systemd_journal_export_block(wtap_dumper *wdh, const wtap_rec *rec,
int *err, char **err_info _U_)
{
uint32_t block_content_length;
uint32_t 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;
}
pad_len = WS_PADDING_TO_4(rec->rec_header.systemd_journal_export_header.record_len);
/* write systemd journal export block header */
block_content_length = rec->rec_header.systemd_journal_export_header.record_len + pad_len;
ws_debug("writing %u bytes, %u padded",
rec->rec_header.systemd_journal_export_header.record_len,
block_content_length);
if (!pcapng_write_block_header(wdh, BLOCK_TYPE_SYSTEMD_JOURNAL_EXPORT,
block_content_length, err))
return false;
/* write entry data */
if (!wtap_dump_file_write(wdh, ws_buffer_start_ptr(&rec->data), rec->rec_header.systemd_journal_export_header.record_len, err))
return false;
/* write padding (if any) */
if (!pcapng_write_padding(wdh, pad_len, err))
return false;
/* write block footer */
return pcapng_write_block_footer(wdh, block_content_length, err);
}
static bool
pcapng_write_custom_block_copy(wtap_dumper *wdh, const wtap_rec *rec,
int *err, char **err_info _U_)
{
pcapng_custom_block_enterprise_handler_t *pen_handler;
uint32_t block_content_length;
pcapng_custom_block_t cb;
uint32_t pad_len;
/* Don't write anything we are not supposed to. */
if (!rec->rec_header.custom_block_header.copy_allowed) {
return true;
}
pen_handler = (pcapng_custom_block_enterprise_handler_t*)g_hash_table_lookup(custom_enterprise_handlers, GUINT_TO_POINTER(rec->rec_header.custom_block_header.pen));
if (pen_handler != NULL)
{
if (!pen_handler->writer(wdh, rec, err, err_info))
return false;
}
else
{
/* 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;
}
pad_len = WS_PADDING_TO_4(rec->rec_header.custom_block_header.length);
/* write block header */
block_content_length = (uint32_t)sizeof(cb) + rec->rec_header.custom_block_header.length + pad_len;
ws_debug("writing %u bytes, %u padded, PEN %u",
(uint32_t)sizeof(cb) + rec->rec_header.custom_block_header.length,
block_content_length, rec->rec_header.custom_block_header.pen);
if (!pcapng_write_block_header(wdh, BLOCK_TYPE_CB_COPY,
block_content_length, 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, ws_buffer_start_ptr(&rec->data), rec->rec_header.custom_block_header.length, err)) {
return false;
}
/* write padding (if any) */
if (!pcapng_write_padding(wdh, pad_len, err))
return false;
/* write block footer */
return pcapng_write_block_footer(wdh, block_content_length, err);
}
return true;
}
static bool
pcapng_write_custom_block_no_copy(wtap_dumper *wdh _U_, const wtap_rec *rec _U_,
int *err _U_, char **err_info _U_)
{
/* Don't write anything we are not supposed to. */
return true;
}
static bool
pcapng_write_decryption_secrets_block(wtap_dumper *wdh, wtap_block_t sdata, int *err)
{
uint32_t block_content_length;
pcapng_decryption_secrets_block_t dsb;
wtapng_dsb_mandatory_t *mand_data = (wtapng_dsb_mandatory_t *)wtap_block_get_mandatory_data(sdata);
uint32_t pad_len;
pad_len = WS_PADDING_TO_4(mand_data->secrets_len);
/* write block header */
block_content_length = (uint32_t)sizeof(dsb) + mand_data->secrets_len + pad_len;
if (!pcapng_write_block_header(wdh, BLOCK_TYPE_DSB, block_content_length,
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;
/* write padding (if any) */
if (!pcapng_write_padding(wdh, pad_len, err))
return false;
/* write block footer */
return pcapng_write_block_footer(wdh, block_content_length, err);
}
static bool
pcapng_write_meta_event_block(wtap_dumper *wdh, wtap_block_t mev_data, int *err)
{
uint32_t block_content_length;
wtapng_meta_event_mandatory_t *mand_data = (wtapng_meta_event_mandatory_t *)wtap_block_get_mandatory_data(mev_data);
uint32_t pad_len;
pad_len = WS_PADDING_TO_4(mand_data->mev_data_len);
/* write block header */
block_content_length = mand_data->mev_data_len + pad_len;
if (!pcapng_write_block_header(wdh, mand_data->mev_block_type,
block_content_length, err))
return false;
ws_debug("Sysdig mev len %u", block_content_length);
/* write block fixed content */
if (!wtap_dump_file_write(wdh, mand_data->mev_data, mand_data->mev_data_len, err))
return false;
/* write padding (if any) */
if (!pcapng_write_padding(wdh, pad_len, err))
return false;
/* write block footer */
return pcapng_write_block_footer(wdh, block_content_length, err);
}
/*
* 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 uint32_t
compute_nrb_option_size(wtap_block_t block _U_, unsigned option_id, wtap_opttype_e option_type _U_, wtap_optval_t* optval)
{
uint32_t 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 bool
put_nrb_option(wtap_block_t block _U_, unsigned option_id, wtap_opttype_e option_type _U_, wtap_optval_t* optval, void* user_data)
{
uint8_t **opt_ptrp = (uint8_t **)user_data;
size_t stringlen;
size_t size = 0;
struct pcapng_option_header option_hdr;
uint32_t pad;
switch(option_id)
{
case OPT_COMMENT:
case OPT_NS_DNSNAME:
size = strlen(optval->stringval);
if (size > 65535) {
/*
* Too big to fit in the option.
* Don't write anything.
*
* XXX - truncate it? Report an error?
*/
return true;
}
/* Put option header */
/* String options don't consider pad bytes part of the length */
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)size;
memcpy(*opt_ptrp, &option_hdr, 4);
*opt_ptrp += 4;
memcpy(*opt_ptrp, optval->stringval, size);
*opt_ptrp += size;
/* put padding (if any) */
pad = WS_PADDING_TO_4(size);
if (pad != 0) {
memset(*opt_ptrp, 0, pad);
*opt_ptrp += pad;
}
break;
case OPT_CUSTOM_STR_COPY:
/* String options don't consider pad bytes part of the length */
stringlen = strlen(optval->custom_stringval.string);
size = sizeof(uint32_t) + stringlen;
if (size > 65535) {
/*
* Too big to fit in the option.
* Don't write anything.
*
* XXX - truncate it? Report an error?
*/
return true;
}
/* Put option header and PEN */
/* String options don't consider pad bytes part of the length */
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)size;
memcpy(*opt_ptrp, &option_hdr, 4);
*opt_ptrp += 4;
memcpy(*opt_ptrp, &optval->custom_stringval.pen, sizeof(uint32_t));
*opt_ptrp += sizeof(uint32_t);
memcpy(*opt_ptrp, optval->custom_stringval.string, size);
*opt_ptrp += size;
/* put padding (if any) */
pad = WS_PADDING_TO_4(size);
if (pad != 0) {
memset(*opt_ptrp, 0, pad);
*opt_ptrp += pad;
}
break;
case OPT_CUSTOM_BIN_COPY:
/* Custom options don't consider pad bytes part of the length */
size = (uint32_t)(optval->custom_binaryval.data.custom_data_len + sizeof(uint32_t)) & 0xffff;
option_hdr.type = (uint16_t)option_id;
option_hdr.value_length = (uint16_t)size;
memcpy(*opt_ptrp, &option_hdr, 4);
*opt_ptrp += 4;
memcpy(*opt_ptrp, &optval->custom_binaryval.pen, sizeof(uint32_t));
*opt_ptrp += sizeof(uint32_t);
memcpy(*opt_ptrp, optval->custom_binaryval.data.custom_data, optval->custom_binaryval.data.custom_data_len);
*opt_ptrp += optval->custom_binaryval.data.custom_data_len;
/* put padding (if any) */
pad = WS_PADDING_TO_4(size);
if (pad != 0) {
memset(*opt_ptrp, 0, pad);
*opt_ptrp += pad;
}
break;
case OPT_NS_DNSIP4ADDR:
option_hdr.type = (uint16_t)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 = (uint16_t)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, uint8_t *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 bool
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);
uint32_t options_size;
size_t max_rec_data_size;
uint8_t *block_data;
uint32_t block_off;
size_t hostnamelen;
uint16_t namelen;
uint32_t 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 = pcapng_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 = (uint8_t *)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 > (UINT16_MAX - 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 = (uint16_t)(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 + WS_PADDING_TO_4(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, WS_PADDING_TO_4(namelen));
block_off += WS_PADDING_TO_4(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 > (UINT16_MAX - 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 = (uint16_t)(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 + WS_PADDING_TO_4(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, WS_PADDING_TO_4(namelen));
block_off += WS_PADDING_TO_4(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 uint32_t compute_isb_option_size(wtap_block_t block _U_, unsigned option_id, wtap_opttype_e option_type _U_, wtap_optval_t *optval _U_)
{
uint32_t 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 bool write_wtap_isb_option(wtap_dumper *wdh, wtap_block_t block _U_,
unsigned option_id,
wtap_opttype_e option_type _U_,
wtap_optval_t *optval,
int *err, char **err_info _U_)
{
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 bool
pcapng_write_interface_statistics_block(wtap_dumper *wdh,
wtap_block_t if_stats,
int *err, char **err_info)
{
uint32_t block_content_length;
pcapng_interface_statistics_block_t isb;
uint32_t 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 = pcapng_compute_options_size(if_stats, compute_isb_option_size);
/* write block header */
block_content_length = (uint32_t)sizeof(isb) + options_size;
if (!pcapng_write_block_header(wdh, BLOCK_TYPE_ISB, block_content_length,
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 (!pcapng_write_options(wdh, OPT_SECTION_BYTE_ORDER,
if_stats, write_wtap_isb_option,
err, err_info))
return false;
}
/* write block footer */
return pcapng_write_block_footer(wdh, block_content_length, err);
}
static uint32_t compute_idb_option_size(wtap_block_t block _U_, unsigned option_id, wtap_opttype_e option_type _U_, wtap_optval_t *optval)
{
uint32_t 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:
size = 8;
break;
default:
/* Unknown options - size by datatype? */
size = 0;
break;
}
return size;
}
static bool write_wtap_idb_option(wtap_dumper *wdh, wtap_block_t block _U_,
unsigned option_id,
wtap_opttype_e option_type _U_,
wtap_optval_t *optval,
int *err, char **err_info)
{
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, OPT_SECTION_BYTE_ORDER,
option_id, optval, err, err_info))
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:
if (!pcapng_write_uint64_option(wdh, option_id, optval, err))
return false;
break;
break;
default:
/* Unknown options - size by datatype? */
break;
}
return true;
}
static bool
pcapng_write_if_descr_block(wtap_dumper *wdh, wtap_block_t int_data,
int *err, char **err_info)
{
uint32_t block_content_length;
pcapng_interface_description_block_t idb;
uint32_t 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 = pcapng_compute_options_size(int_data, compute_idb_option_size);
/* write block header */
block_content_length = (uint32_t)sizeof(idb) + options_size;
if (!pcapng_write_block_header(wdh, BLOCK_TYPE_IDB, block_content_length,
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 (!pcapng_write_options(wdh, OPT_SECTION_BYTE_ORDER,
int_data, write_wtap_idb_option,
err, err_info))
return false;
}
/* write block footer */
return pcapng_write_block_footer(wdh, block_content_length, err);
}
static bool pcapng_add_idb(wtap_dumper *wdh, wtap_block_t idb,
int *err, char **err_info)
{
/*
* Write it to the output file.
*/
return pcapng_write_if_descr_block(wdh, idb, err, err_info);
}
static bool 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 (unsigned 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->mevs_growing) {
for (unsigned i = wdh->mevs_growing_written; i < wdh->mevs_growing->len; i++) {
ws_debug("writing Sysdig mev %u", i);
wtap_block_t mev = g_array_index(wdh->mevs_growing, wtap_block_t, i);
if (!pcapng_write_meta_event_block(wdh, mev, err)) {
return false;
}
++wdh->mevs_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 (unsigned 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;
}
}
/* Write (optional) Darwin Process Event Blocks that were collected while
* reading packet blocks. */
if (wdh->dpibs_growing) {
ws_noisy("writing internal blocks: dpibs_growing: written: %u len: %u",
wdh->dpibs_growing_written, wdh->dpibs_growing->len);
for (unsigned i = wdh->dpibs_growing_written; i < wdh->dpibs_growing->len; i++) {
wtap_block_t dpib = g_array_index(wdh->dpibs_growing, wtap_block_t, i);
if (!pcapng_write_legacy_darwin_process_event_block(wdh, dpib, err)) {
return false;
}
++wdh->dpibs_growing_written;
}
}
return true;
}
static bool pcapng_dump(wtap_dumper *wdh, const wtap_rec *rec,
int *err, char **err_info)
{
uint32_t block_type;
pcapng_block_type_information_t* handler;
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:
/* Write Simple Packet Block if appropriate, Enhanced Packet Block otherwise. */
if (!(rec->presence_flags & WTAP_HAS_TS) &&
(!(rec->presence_flags & WTAP_HAS_INTERFACE_ID) || rec->rec_header.packet_header.interface_id == 0) &&
(!(rec->presence_flags & WTAP_HAS_CAP_LEN) || rec->rec_header.packet_header.len == rec->rec_header.packet_header.caplen) &&
(rec->block == NULL || pcapng_compute_options_size(rec->block, compute_epb_option_size) == 0)) {
block_type = BLOCK_TYPE_SPB;
}
else {
block_type = BLOCK_TYPE_EPB;
}
break;
case REC_TYPE_FT_SPECIFIC_EVENT:
case REC_TYPE_FT_SPECIFIC_REPORT:
/*
* Is this an event or report for our file type?
*/
if (rec->rec_header.ft_specific_header.file_type_subtype != pcapng_file_type_subtype) {
/*
* No. We can't write that.
*/
*err = WTAP_ERR_UNWRITABLE_REC_TYPE;
*err_info = g_strdup_printf("%s records for \"%s\" files aren't supported for this file type",
rec->rec_type_name,
wtap_file_type_subtype_name(rec->rec_header.ft_specific_header.file_type_subtype));
return false;
}
block_type = rec->rec_header.ft_specific_header.record_type;
break;
case REC_TYPE_SYSCALL:
block_type = rec->rec_header.syscall_header.record_type;
break;
case REC_TYPE_SYSTEMD_JOURNAL_EXPORT:
block_type = BLOCK_TYPE_SYSTEMD_JOURNAL_EXPORT;
break;
case REC_TYPE_CUSTOM_BLOCK:
{
/* Don't write anything we are not supposed to. */
if (!rec->rec_header.custom_block_header.copy_allowed) {
return true;
}
block_type = BLOCK_TYPE_CB_COPY;
break;
}
default:
/* We don't support writing this record type. */
*err = WTAP_ERR_UNWRITABLE_REC_TYPE;
*err_info = wtap_unwritable_rec_type_err_string(rec);
return false;
}
/*
* Do we have a handler for this block type?
*/
handler = (pcapng_block_type_information_t*)g_hash_table_lookup(block_handlers,
GUINT_TO_POINTER(block_type));
if (handler == NULL) {
/* No. We can't write that. */
*err = WTAP_ERR_UNWRITABLE_REC_TYPE;
*err_info = g_strdup_printf("Pcapng blocks of type 0x%8x aren't supported",
rec->rec_header.ft_specific_header.record_type);
return false;
}
/* Yes. Call it to write out this record. */
return handler->writer(wdh, rec, err, err_info);
}
/*
* Write block header.
*/
bool
pcapng_write_block_header(wtap_dumper *wdh, uint32_t block_type,
uint32_t block_content_length, int *err)
{
pcapng_block_header_t bh;
bh.block_type = block_type;
/*
* Total block length is the length of the header plus the length
* of the block content (which is padded to a multiple of 4 bytes)
* plus the length of the trailer.
*/
bh.block_total_length = (uint32_t)sizeof(pcapng_block_header_t) + block_content_length + 4;
ws_debug("Total len %u", bh.block_total_length);
return wtap_dump_file_write(wdh, &bh, sizeof bh, err);
}
/*
* Write block footer.
*/
bool
pcapng_write_block_footer(wtap_dumper *wdh, uint32_t block_content_length,
int *err)
{
uint32_t bf;
bf = (uint32_t)sizeof(pcapng_block_header_t) + block_content_length + 4;
return wtap_dump_file_write(wdh, &bf, sizeof bf, err);
}
/* Finish writing to a dump file.
Returns true on success, false on failure. */
static bool pcapng_dump_finish(wtap_dumper *wdh, int *err, char **err_info)
{
unsigned 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, err_info)) {
return false;
}
}
}
ws_debug("leaving function");
return true;
}
/* Returns true on success, false on failure; sets "*err" to an error code on
failure */
static bool
pcapng_dump_open(wtap_dumper *wdh, int *err, char **err_info)
{
unsigned 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, err_info)) {
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, err_info)) {
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.
*/
bool 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 },
{ OPT_IDB_TXSPEED, ONE_OPTION_SUPPORTED },
{ OPT_IDB_RXSPEED, ONE_OPTION_SUPPORTED },
{ OPT_IDB_IANA_TZNAME, 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 meta event blocks. */
static const struct supported_option_type 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_HASH, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_PKT_DROPCOUNT, ONE_OPTION_SUPPORTED },
{ OPT_PKT_PACKETID, ONE_OPTION_SUPPORTED },
{ OPT_PKT_QUEUE, ONE_OPTION_SUPPORTED },
{ OPT_PKT_VERDICT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_PKT_PROCIDTHRDID, ONE_OPTION_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-specific 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-specific 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 }
};
/* Options for file-type-specific information. */
static const struct supported_option_type ftr_specific_information_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_DPIB_NAME, ONE_OPTION_SUPPORTED },
{ OPT_DPIB_UUID, ONE_OPTION_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 meta evens.. */
{ WTAP_BLOCK_META_EVENT, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(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 },
/* Multiple file-type-specific information blocks. */
{ WTAP_BLOCK_FT_SPECIFIC_INFORMATION, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(ftr_specific_information_block_options_supported) },
};
static const struct file_type_subtype_info wireshark_pcapng_info = {
"Wireshark/... - pcapng", "pcapng", "pcapng", "ntar",
false, BLOCKS_SUPPORTED(pcapng_blocks_supported),
pcapng_dump_can_write_encap, pcapng_dump_open, NULL
};
static const struct file_type_subtype_info stratoshark_pcapng_info = {
"Stratoshark/... - scap", "scap", "scap", "scap",
false, BLOCKS_SUPPORTED(pcapng_blocks_supported),
pcapng_dump_can_write_encap, pcapng_dump_open, NULL
};
void register_pcapng(void)
{
if (application_flavor_is_wireshark()) {
pcapng_file_type_subtype = wtap_register_file_type_subtype(&wireshark_pcapng_info);
} else {
pcapng_file_type_subtype = wtap_register_file_type_subtype(&stratoshark_pcapng_info);
}
wtap_register_backwards_compatibility_lua_name("PCAPNG",
pcapng_file_type_subtype);
/* Setup the tables that will be used to handle custom block options */
/*
* 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()".
*/
block_handlers = g_hash_table_new_full(g_direct_hash,
g_direct_equal,
NULL, g_free);
custom_enterprise_handlers = g_hash_table_new_full(g_direct_hash,
g_direct_equal,
NULL, g_free);
/* SHBs require special handling, so they don't have handlers here. */
static pcapng_block_type_information_t SHB = { BLOCK_TYPE_SHB, NULL, NULL, NULL, true, NULL };
SHB.option_handlers = pcapng_create_option_handler_table();
register_pcapng_block_type_information(&SHB);
static pcapng_block_type_information_t IDB = { BLOCK_TYPE_IDB, pcapng_read_if_descr_block, pcapng_process_idb, NULL, true, NULL };
IDB.option_handlers = pcapng_create_option_handler_table();
register_pcapng_block_type_information(&IDB);
static pcapng_block_type_information_t EPB = { BLOCK_TYPE_EPB, pcapng_read_packet_block, NULL, pcapng_write_enhanced_packet_block, false, NULL };
EPB.option_handlers = pcapng_create_option_handler_table();
register_pcapng_block_type_information(&EPB);
static pcapng_block_type_information_t PB = { BLOCK_TYPE_PB, pcapng_read_packet_block, NULL, NULL, false, NULL };
/* PBs and EPBs have the same options. */
PB.option_handlers = EPB.option_handlers;
register_pcapng_block_type_information(&PB);
static pcapng_block_type_information_t SPB = { BLOCK_TYPE_SPB, pcapng_read_simple_packet_block, NULL, pcapng_write_simple_packet_block, false, NULL };
/* SPBs don't support options */
register_pcapng_block_type_information(&SPB);
static pcapng_block_type_information_t NRB = { BLOCK_TYPE_NRB, pcapng_read_name_resolution_block, pcapng_process_nrb, NULL, true, NULL };
NRB.option_handlers = pcapng_create_option_handler_table();
register_pcapng_block_type_information(&NRB);
static pcapng_block_type_information_t ISB = { BLOCK_TYPE_ISB, pcapng_read_interface_statistics_block, pcapng_process_isb, NULL, true, NULL };
ISB.option_handlers = pcapng_create_option_handler_table();
register_pcapng_block_type_information(&ISB);
static pcapng_block_type_information_t DSB = { BLOCK_TYPE_DSB, pcapng_read_decryption_secrets_block, pcapng_process_dsb, NULL, true, NULL };
DSB.option_handlers = pcapng_create_option_handler_table();
register_pcapng_block_type_information(&DSB);
static pcapng_block_type_information_t CB_COPY = { BLOCK_TYPE_CB_COPY, pcapng_read_custom_block, NULL, pcapng_write_custom_block_copy, false, NULL };
CB_COPY.option_handlers = pcapng_create_option_handler_table();
register_pcapng_block_type_information(&CB_COPY);
static pcapng_block_type_information_t CB_NO_COPY = { BLOCK_TYPE_CB_NO_COPY, pcapng_read_custom_block, NULL, pcapng_write_custom_block_no_copy, false, NULL };
/* Copy and no-copy and CBs have the same options. */
CB_NO_COPY.option_handlers = CB_COPY.option_handlers;
register_pcapng_block_type_information(&CB_NO_COPY);
static pcapng_block_type_information_t SYSTEMD_JOURNAL_EXPORT = { BLOCK_TYPE_SYSTEMD_JOURNAL_EXPORT, pcapng_read_systemd_journal_export_block, NULL, pcapng_write_systemd_journal_export_block, false, NULL };
SYSTEMD_JOURNAL_EXPORT.option_handlers = pcapng_create_option_handler_table();
register_pcapng_block_type_information(&SYSTEMD_JOURNAL_EXPORT);
}
/*
* 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:
*/
|