Loading...
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 | /* * Copyright (c) 2010-2022 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /*- * Copyright (c) 2007-2009 Bruce Simpson. * Copyright (c) 2005 Robert N. M. Watson. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * IPv4 multicast socket, group, and socket option processing module. */ #include <sys/cdefs.h> #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <sys/mbuf.h> #include <sys/protosw.h> #include <sys/socket.h> #include <sys/socketvar.h> #include <sys/protosw.h> #include <sys/sysctl.h> #include <sys/tree.h> #include <sys/mcache.h> #include <kern/zalloc.h> #include <pexpert/pexpert.h> #include <net/if.h> #include <net/if_dl.h> #include <net/net_api_stats.h> #include <net/route.h> #include <netinet/in.h> #include <netinet/in_systm.h> #include <netinet/in_pcb.h> #include <netinet/in_var.h> #include <netinet/ip_var.h> #include <netinet/igmp_var.h> /* * Functions with non-static linkage defined in this file should be * declared in in_var.h: * imo_multi_filter() * in_addmulti() * in_delmulti() * in_joingroup() * in_leavegroup() * and ip_var.h: * inp_freemoptions() * inp_getmoptions() * inp_setmoptions() * * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti() * and in_delmulti(). */ static void imf_commit(struct in_mfilter *); static int imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin, struct in_msource **); static struct in_msource * imf_graft(struct in_mfilter *, const uint8_t, const struct sockaddr_in *); static int imf_prune(struct in_mfilter *, const struct sockaddr_in *); static void imf_rollback(struct in_mfilter *); static void imf_reap(struct in_mfilter *); static int imo_grow(struct ip_moptions *, uint16_t); static size_t imo_match_group(const struct ip_moptions *, const struct ifnet *, const struct sockaddr_in *); static struct in_msource * imo_match_source(const struct ip_moptions *, const size_t, const struct sockaddr_in *); static void ims_merge(struct ip_msource *ims, const struct in_msource *lims, const int rollback); static int in_getmulti(struct ifnet *, const struct in_addr *, struct in_multi **); static int in_joingroup(struct ifnet *, const struct in_addr *, struct in_mfilter *, struct in_multi **); static int inm_get_source(struct in_multi *inm, const in_addr_t haddr, const int noalloc, struct ip_msource **pims); static int inm_is_ifp_detached(const struct in_multi *); static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *); static void inm_reap(struct in_multi *); static struct ip_moptions * inp_findmoptions(struct inpcb *); static int inp_get_source_filters(struct inpcb *, struct sockopt *); static struct ifnet * inp_lookup_mcast_ifp(const struct inpcb *, const struct sockaddr_in *, const struct in_addr); static int inp_block_unblock_source(struct inpcb *, struct sockopt *); static int inp_set_multicast_if(struct inpcb *, struct sockopt *); static int inp_set_source_filters(struct inpcb *, struct sockopt *); static int sysctl_ip_mcast_filters SYSCTL_HANDLER_ARGS; static struct ifnet * ip_multicast_if(struct in_addr *, unsigned int *); static __inline__ int ip_msource_cmp(const struct ip_msource *, const struct ip_msource *); SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "IPv4 multicast"); static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER; SYSCTL_LONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc, CTLFLAG_RW | CTLFLAG_LOCKED, &in_mcast_maxgrpsrc, "Max source filters per group"); static u_int in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER; SYSCTL_UINT(_net_inet_ip_mcast, OID_AUTO, maxsocksrc, CTLFLAG_RW | CTLFLAG_LOCKED, &in_mcast_maxsocksrc, IP_MAX_SOCK_SRC_FILTER, "Max source filters per socket"); int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP; SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_LOCKED, &in_mcast_loop, 0, "Loopback multicast datagrams by default"); SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters, CTLFLAG_RD | CTLFLAG_LOCKED, sysctl_ip_mcast_filters, "Per-interface stack-wide source filters"); RB_GENERATE_PREV(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp); #define INM_TRACE_HIST_SIZE 32 /* size of trace history */ /* For gdb */ __private_extern__ unsigned int inm_trace_hist_size = INM_TRACE_HIST_SIZE; struct in_multi_dbg { struct in_multi inm; /* in_multi */ u_int16_t inm_refhold_cnt; /* # of ref */ u_int16_t inm_refrele_cnt; /* # of rele */ /* * Circular lists of inm_addref and inm_remref callers. */ ctrace_t inm_refhold[INM_TRACE_HIST_SIZE]; ctrace_t inm_refrele[INM_TRACE_HIST_SIZE]; /* * Trash list linkage */ TAILQ_ENTRY(in_multi_dbg) inm_trash_link; }; static LCK_ATTR_DECLARE(in_multihead_lock_attr, 0, 0); static LCK_GRP_DECLARE(in_multihead_lock_grp, "in_multihead"); /* List of trash in_multi entries protected by inm_trash_lock */ static TAILQ_HEAD(, in_multi_dbg) inm_trash_head; static LCK_MTX_DECLARE_ATTR(inm_trash_lock, &in_multihead_lock_grp, &in_multihead_lock_attr); #if DEBUG static TUNABLE(bool, inm_debug, "ifa_debug", true); /* debugging (enabled) */ #else static TUNABLE(bool, inm_debug, "ifa_debug", false); /* debugging (disabled) */ #endif /* !DEBUG */ #define INM_ZONE_NAME "in_multi" /* zone name */ static struct zone *inm_zone; /* zone for in_multi */ static KALLOC_TYPE_DEFINE(ipms_zone, struct ip_msource, NET_KT_DEFAULT); static KALLOC_TYPE_DEFINE(inms_zone, struct in_msource, NET_KT_DEFAULT); static LCK_RW_DECLARE_ATTR(in_multihead_lock, &in_multihead_lock_grp, &in_multihead_lock_attr); struct in_multihead in_multihead; static struct in_multi *in_multi_alloc(zalloc_flags_t); static void in_multi_free(struct in_multi *); static void in_multi_attach(struct in_multi *); static void inm_trace(struct in_multi *, int); static struct ip_msource *ipms_alloc(zalloc_flags_t); static void ipms_free(struct ip_msource *); static struct in_msource *inms_alloc(zalloc_flags_t); static void inms_free(struct in_msource *); static __inline int ip_msource_cmp(const struct ip_msource *a, const struct ip_msource *b) { if (a->ims_haddr < b->ims_haddr) { return -1; } if (a->ims_haddr == b->ims_haddr) { return 0; } return 1; } /* * Inline function which wraps assertions for a valid ifp. */ static __inline__ int inm_is_ifp_detached(const struct in_multi *inm) { VERIFY(inm->inm_ifma != NULL); VERIFY(inm->inm_ifp == inm->inm_ifma->ifma_ifp); return !ifnet_is_attached(inm->inm_ifp, 0); } /* * Initialize an in_mfilter structure to a known state at t0, t1 * with an empty source filter list. */ static __inline__ void imf_init(struct in_mfilter *imf, const uint8_t st0, const uint8_t st1) { memset(imf, 0, sizeof(struct in_mfilter)); RB_INIT(&imf->imf_sources); imf->imf_st[0] = st0; imf->imf_st[1] = st1; } /* * Resize the ip_moptions vector to the next power-of-two minus 1. */ static int imo_grow(struct ip_moptions *imo, uint16_t newmax) { struct in_multi **nmships; struct in_multi **omships; struct in_mfilter *nmfilters; struct in_mfilter *omfilters; int err; uint16_t idx; uint16_t oldmax; IMO_LOCK_ASSERT_HELD(imo); nmships = NULL; nmfilters = NULL; err = 0; omships = imo->imo_membership; omfilters = imo->imo_mfilters; oldmax = imo->imo_max_memberships; if (newmax == 0) { newmax = ((oldmax + 1) * 2) - 1; } else if (newmax <= oldmax) { /* Nothing to do, exit early. */ return 0; } if (newmax > IP_MAX_MEMBERSHIPS) { err = ETOOMANYREFS; goto cleanup; } if ((nmships = kalloc_type(struct in_multi *, newmax, Z_WAITOK | Z_ZERO)) == NULL) { err = ENOMEM; goto cleanup; } if ((nmfilters = kalloc_type(struct in_mfilter, newmax, Z_WAITOK | Z_ZERO)) == NULL) { err = ENOMEM; goto cleanup; } /* Copy the existing memberships and release the memory. */ if (omships != NULL) { VERIFY(oldmax <= newmax); memcpy(nmships, omships, oldmax * sizeof(struct in_multi *)); kfree_type(struct in_multi *, oldmax, omships); } /* Copy the existing filters and release the memory. */ if (omfilters != NULL) { VERIFY(oldmax <= newmax); memcpy(nmfilters, omfilters, oldmax * sizeof(struct in_mfilter)); kfree_type(struct in_mfilter, oldmax, omfilters); } /* Initialize the newly allocated source filter heads. */ for (idx = oldmax; idx < newmax; idx++) { imf_init(&nmfilters[idx], MCAST_UNDEFINED, MCAST_EXCLUDE); } imo->imo_membership = nmships; nmships = NULL; imo->imo_mfilters = nmfilters; nmfilters = NULL; imo->imo_max_memberships = newmax; return 0; cleanup: if (nmfilters != NULL) { kfree_type(struct in_mfilter, newmax, nmfilters); } if (nmships != NULL) { kfree_type(struct in_multi *, newmax, nmships); } return err; } /* * Find an IPv4 multicast group entry for this ip_moptions instance * which matches the specified group, and optionally an interface. * Return its index into the array, or -1 if not found. */ static size_t imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp, const struct sockaddr_in *group) { struct in_multi *pinm; int idx; int nmships; IMO_LOCK_ASSERT_HELD(__DECONST(struct ip_moptions *, imo)); /* The imo_membership array may be lazy allocated. */ if (imo->imo_membership == NULL || imo->imo_num_memberships == 0) { return -1; } nmships = imo->imo_num_memberships; for (idx = 0; idx < nmships; idx++) { pinm = imo->imo_membership[idx]; if (pinm == NULL) { continue; } INM_LOCK(pinm); if ((ifp == NULL || (pinm->inm_ifp == ifp)) && in_hosteq(pinm->inm_addr, group->sin_addr)) { INM_UNLOCK(pinm); break; } INM_UNLOCK(pinm); } if (idx >= nmships) { idx = -1; } return idx; } /* * Find an IPv4 multicast source entry for this imo which matches * the given group index for this socket, and source address. * * NOTE: This does not check if the entry is in-mode, merely if * it exists, which may not be the desired behaviour. */ static struct in_msource * imo_match_source(const struct ip_moptions *imo, const size_t gidx, const struct sockaddr_in *src) { struct ip_msource find; struct in_mfilter *imf; struct ip_msource *ims; IMO_LOCK_ASSERT_HELD(__DECONST(struct ip_moptions *, imo)); VERIFY(src->sin_family == AF_INET); VERIFY(gidx != (size_t)-1 && gidx < imo->imo_num_memberships); /* The imo_mfilters array may be lazy allocated. */ if (imo->imo_mfilters == NULL) { return NULL; } imf = &imo->imo_mfilters[gidx]; /* Source trees are keyed in host byte order. */ find.ims_haddr = ntohl(src->sin_addr.s_addr); ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); return (struct in_msource *)ims; } /* * Perform filtering for multicast datagrams on a socket by group and source. * * Returns 0 if a datagram should be allowed through, or various error codes * if the socket was not a member of the group, or the source was muted, etc. */ int imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp, const struct sockaddr_in *group, const struct sockaddr_in *src) { size_t gidx; struct in_msource *ims; int mode; IMO_LOCK_ASSERT_HELD(__DECONST(struct ip_moptions *, imo)); VERIFY(ifp != NULL); gidx = imo_match_group(imo, ifp, group); if (gidx == (size_t)-1) { return MCAST_NOTGMEMBER; } /* * Check if the source was included in an (S,G) join. * Allow reception on exclusive memberships by default, * reject reception on inclusive memberships by default. * Exclude source only if an in-mode exclude filter exists. * Include source only if an in-mode include filter exists. * NOTE: We are comparing group state here at IGMP t1 (now) * with socket-layer t0 (since last downcall). */ mode = imo->imo_mfilters[gidx].imf_st[1]; ims = imo_match_source(imo, gidx, src); if ((ims == NULL && mode == MCAST_INCLUDE) || (ims != NULL && ims->imsl_st[0] != mode)) { return MCAST_NOTSMEMBER; } return MCAST_PASS; } int imo_clone(struct inpcb *from_inp, struct inpcb *to_inp) { int err = 0; struct ip_moptions *from; struct ip_moptions *to; from = inp_findmoptions(from_inp); if (from == NULL) { return ENOMEM; } to = inp_findmoptions(to_inp); if (to == NULL) { IMO_REMREF(from); return ENOMEM; } IMO_LOCK(from); IMO_LOCK(to); to->imo_multicast_ifp = from->imo_multicast_ifp; to->imo_multicast_vif = from->imo_multicast_vif; to->imo_multicast_ttl = from->imo_multicast_ttl; to->imo_multicast_loop = from->imo_multicast_loop; /* * We're cloning, so drop any existing memberships and source * filters on the destination ip_moptions. */ IMO_PURGE_LOCKED(to); VERIFY(to->imo_max_memberships != 0 && from->imo_max_memberships != 0); if (to->imo_max_memberships < from->imo_max_memberships) { /* * Ensure source and destination ip_moptions memberships * and source filters arrays are at least equal in size. */ err = imo_grow(to, from->imo_max_memberships); if (err != 0) { goto done; } } VERIFY(to->imo_max_memberships >= from->imo_max_memberships); /* * Source filtering doesn't apply to OpenTransport socket, * so simply hold additional reference count per membership. */ for (int i = 0; i < from->imo_num_memberships; i++) { to->imo_membership[i] = in_addmulti(&from->imo_membership[i]->inm_addr, from->imo_membership[i]->inm_ifp); if (to->imo_membership[i] == NULL) { break; } to->imo_num_memberships++; } VERIFY(to->imo_num_memberships == from->imo_num_memberships); done: IMO_UNLOCK(to); IMO_REMREF(to); IMO_UNLOCK(from); IMO_REMREF(from); return err; } /* * Find and return a reference to an in_multi record for (ifp, group), * and bump its reference count. * If one does not exist, try to allocate it, and update link-layer multicast * filters on ifp to listen for group. * Return 0 if successful, otherwise return an appropriate error code. */ static int in_getmulti(struct ifnet *ifp, const struct in_addr *group, struct in_multi **pinm) { struct sockaddr_in gsin; struct ifmultiaddr *ifma; struct in_multi *inm; int error; in_multihead_lock_shared(); IN_LOOKUP_MULTI(group, ifp, inm); if (inm != NULL) { INM_LOCK(inm); VERIFY(inm->inm_reqcnt >= 1); inm->inm_reqcnt++; VERIFY(inm->inm_reqcnt != 0); *pinm = inm; INM_UNLOCK(inm); in_multihead_lock_done(); /* * We already joined this group; return the inm * with a refcount held (via lookup) for caller. */ return 0; } in_multihead_lock_done(); bzero(&gsin, sizeof(gsin)); gsin.sin_family = AF_INET; gsin.sin_len = sizeof(struct sockaddr_in); gsin.sin_addr = *group; /* * Check if a link-layer group is already associated * with this network-layer group on the given ifnet. */ error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma); if (error != 0) { return error; } /* * See comments in inm_remref() for access to ifma_protospec. */ in_multihead_lock_exclusive(); IFMA_LOCK(ifma); if ((inm = ifma->ifma_protospec) != NULL) { VERIFY(ifma->ifma_addr != NULL); VERIFY(ifma->ifma_addr->sa_family == AF_INET); INM_ADDREF(inm); /* for caller */ IFMA_UNLOCK(ifma); INM_LOCK(inm); VERIFY(inm->inm_ifma == ifma); VERIFY(inm->inm_ifp == ifp); VERIFY(in_hosteq(inm->inm_addr, *group)); if (inm->inm_debug & IFD_ATTACHED) { VERIFY(inm->inm_reqcnt >= 1); inm->inm_reqcnt++; VERIFY(inm->inm_reqcnt != 0); *pinm = inm; INM_UNLOCK(inm); in_multihead_lock_done(); IFMA_REMREF(ifma); /* * We lost the race with another thread doing * in_getmulti(); since this group has already * been joined; return the inm with a refcount * held for caller. */ return 0; } /* * We lost the race with another thread doing in_delmulti(); * the inm referring to the ifma has been detached, thus we * reattach it back to the in_multihead list and return the * inm with a refcount held for the caller. */ in_multi_attach(inm); VERIFY((inm->inm_debug & (IFD_ATTACHED | IFD_TRASHED)) == IFD_ATTACHED); *pinm = inm; INM_UNLOCK(inm); in_multihead_lock_done(); IFMA_REMREF(ifma); return 0; } IFMA_UNLOCK(ifma); /* * A new in_multi record is needed; allocate and initialize it. * We DO NOT perform an IGMP join as the in_ layer may need to * push an initial source list down to IGMP to support SSM. * * The initial source filter state is INCLUDE, {} as per the RFC. */ inm = in_multi_alloc(Z_WAITOK); INM_LOCK(inm); inm->inm_addr = *group; inm->inm_ifp = ifp; inm->inm_igi = IGMP_IFINFO(ifp); VERIFY(inm->inm_igi != NULL); IGI_ADDREF(inm->inm_igi); inm->inm_ifma = ifma; /* keep refcount from if_addmulti() */ inm->inm_state = IGMP_NOT_MEMBER; /* * Pending state-changes per group are subject to a bounds check. */ inm->inm_scq.ifq_maxlen = IGMP_MAX_STATE_CHANGES; inm->inm_st[0].iss_fmode = MCAST_UNDEFINED; inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; RB_INIT(&inm->inm_srcs); *pinm = inm; in_multi_attach(inm); VERIFY((inm->inm_debug & (IFD_ATTACHED | IFD_TRASHED)) == IFD_ATTACHED); INM_ADDREF_LOCKED(inm); /* for caller */ INM_UNLOCK(inm); IFMA_LOCK(ifma); VERIFY(ifma->ifma_protospec == NULL); ifma->ifma_protospec = inm; IFMA_UNLOCK(ifma); in_multihead_lock_done(); return 0; } /* * Clear recorded source entries for a group. * Used by the IGMP code. * FIXME: Should reap. */ void inm_clear_recorded(struct in_multi *inm) { struct ip_msource *ims; INM_LOCK_ASSERT_HELD(inm); RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { if (ims->ims_stp) { ims->ims_stp = 0; --inm->inm_st[1].iss_rec; } } VERIFY(inm->inm_st[1].iss_rec == 0); } /* * Record a source as pending for a Source-Group IGMPv3 query. * This lives here as it modifies the shared tree. * * inm is the group descriptor. * naddr is the address of the source to record in network-byte order. * * If the net.inet.igmp.sgalloc sysctl is non-zero, we will * lazy-allocate a source node in response to an SG query. * Otherwise, no allocation is performed. This saves some memory * with the trade-off that the source will not be reported to the * router if joined in the window between the query response and * the group actually being joined on the local host. * * Return 0 if the source didn't exist or was already marked as recorded. * Return 1 if the source was marked as recorded by this function. * Return <0 if any error occured (negated errno code). */ int inm_record_source(struct in_multi *inm, const in_addr_t naddr) { struct ip_msource find; struct ip_msource *ims, *nims; INM_LOCK_ASSERT_HELD(inm); find.ims_haddr = ntohl(naddr); ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find); if (ims && ims->ims_stp) { return 0; } if (ims == NULL) { if (inm->inm_nsrc == in_mcast_maxgrpsrc) { return -ENOSPC; } nims = ipms_alloc(Z_WAITOK); nims->ims_haddr = find.ims_haddr; RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims); ++inm->inm_nsrc; ims = nims; } /* * Mark the source as recorded and update the recorded * source count. */ ++ims->ims_stp; ++inm->inm_st[1].iss_rec; return 1; } /* * Return a pointer to an in_msource owned by an in_mfilter, * given its source address. * Lazy-allocate if needed. If this is a new entry its filter state is * undefined at t0. * * imf is the filter set being modified. * haddr is the source address in *host* byte-order. * * Caller is expected to be holding imo_lock. */ static int imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin, struct in_msource **plims) { struct ip_msource find; struct ip_msource *ims; struct in_msource *lims; int error; error = 0; ims = NULL; lims = NULL; /* key is host byte order */ find.ims_haddr = ntohl(psin->sin_addr.s_addr); ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); lims = (struct in_msource *)ims; if (lims == NULL) { if (imf->imf_nsrc == in_mcast_maxsocksrc) { return ENOSPC; } lims = inms_alloc(Z_WAITOK); lims->ims_haddr = find.ims_haddr; lims->imsl_st[0] = MCAST_UNDEFINED; RB_INSERT(ip_msource_tree, &imf->imf_sources, (struct ip_msource *)lims); ++imf->imf_nsrc; } *plims = lims; return error; } /* * Graft a source entry into an existing socket-layer filter set, * maintaining any required invariants and checking allocations. * * The source is marked as being in the new filter mode at t1. * * Return the pointer to the new node, otherwise return NULL. * * Caller is expected to be holding imo_lock. */ static struct in_msource * imf_graft(struct in_mfilter *imf, const uint8_t st1, const struct sockaddr_in *psin) { struct in_msource *lims; lims = inms_alloc(Z_WAITOK); lims->ims_haddr = ntohl(psin->sin_addr.s_addr); lims->imsl_st[0] = MCAST_UNDEFINED; lims->imsl_st[1] = st1; RB_INSERT(ip_msource_tree, &imf->imf_sources, (struct ip_msource *)lims); ++imf->imf_nsrc; return lims; } /* * Prune a source entry from an existing socket-layer filter set, * maintaining any required invariants and checking allocations. * * The source is marked as being left at t1, it is not freed. * * Return 0 if no error occurred, otherwise return an errno value. * * Caller is expected to be holding imo_lock. */ static int imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin) { struct ip_msource find; struct ip_msource *ims; struct in_msource *lims; /* key is host byte order */ find.ims_haddr = ntohl(psin->sin_addr.s_addr); ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); if (ims == NULL) { return ENOENT; } lims = (struct in_msource *)ims; lims->imsl_st[1] = MCAST_UNDEFINED; return 0; } /* * Revert socket-layer filter set deltas at t1 to t0 state. * * Caller is expected to be holding imo_lock. */ static void imf_rollback(struct in_mfilter *imf) { struct ip_msource *ims, *tims; struct in_msource *lims; RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { lims = (struct in_msource *)ims; if (lims->imsl_st[0] == lims->imsl_st[1]) { /* no change at t1 */ continue; } else if (lims->imsl_st[0] != MCAST_UNDEFINED) { /* revert change to existing source at t1 */ lims->imsl_st[1] = lims->imsl_st[0]; } else { /* revert source added t1 */ IGMP_PRINTF(("%s: free inms 0x%llx\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(lims))); RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); inms_free(lims); imf->imf_nsrc--; } } imf->imf_st[1] = imf->imf_st[0]; } /* * Mark socket-layer filter set as INCLUDE {} at t1. * * Caller is expected to be holding imo_lock. */ void imf_leave(struct in_mfilter *imf) { struct ip_msource *ims; struct in_msource *lims; RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { lims = (struct in_msource *)ims; lims->imsl_st[1] = MCAST_UNDEFINED; } imf->imf_st[1] = MCAST_INCLUDE; } /* * Mark socket-layer filter set deltas as committed. * * Caller is expected to be holding imo_lock. */ static void imf_commit(struct in_mfilter *imf) { struct ip_msource *ims; struct in_msource *lims; RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { lims = (struct in_msource *)ims; lims->imsl_st[0] = lims->imsl_st[1]; } imf->imf_st[0] = imf->imf_st[1]; } /* * Reap unreferenced sources from socket-layer filter set. * * Caller is expected to be holding imo_lock. */ static void imf_reap(struct in_mfilter *imf) { struct ip_msource *ims, *tims; struct in_msource *lims; RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { lims = (struct in_msource *)ims; if ((lims->imsl_st[0] == MCAST_UNDEFINED) && (lims->imsl_st[1] == MCAST_UNDEFINED)) { IGMP_PRINTF(("%s: free inms 0x%llx\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(lims))); RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); inms_free(lims); imf->imf_nsrc--; } } } /* * Purge socket-layer filter set. * * Caller is expected to be holding imo_lock. */ void imf_purge(struct in_mfilter *imf) { struct ip_msource *ims, *tims; struct in_msource *lims; RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { lims = (struct in_msource *)ims; IGMP_PRINTF(("%s: free inms 0x%llx\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(lims))); RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); inms_free(lims); imf->imf_nsrc--; } imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED; VERIFY(RB_EMPTY(&imf->imf_sources)); } /* * Look up a source filter entry for a multicast group. * * inm is the group descriptor to work with. * haddr is the host-byte-order IPv4 address to look up. * noalloc may be non-zero to suppress allocation of sources. * *pims will be set to the address of the retrieved or allocated source. * * Return 0 if successful, otherwise return a non-zero error code. */ static int inm_get_source(struct in_multi *inm, const in_addr_t haddr, const int noalloc, struct ip_msource **pims) { struct ip_msource find; struct ip_msource *ims, *nims; #ifdef IGMP_DEBUG struct in_addr ia; char buf[MAX_IPv4_STR_LEN]; #endif INM_LOCK_ASSERT_HELD(inm); find.ims_haddr = haddr; ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find); if (ims == NULL && !noalloc) { if (inm->inm_nsrc == in_mcast_maxgrpsrc) { return ENOSPC; } nims = ipms_alloc(Z_WAITOK); nims->ims_haddr = haddr; RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims); ++inm->inm_nsrc; ims = nims; #ifdef IGMP_DEBUG ia.s_addr = htonl(haddr); inet_ntop(AF_INET, &ia, buf, sizeof(buf)); IGMP_PRINTF(("%s: allocated %s as 0x%llx\n", __func__, buf, (uint64_t)VM_KERNEL_ADDRPERM(ims))); #endif } *pims = ims; return 0; } /* * Helper function to derive the filter mode on a source entry * from its internal counters. Predicates are: * A source is only excluded if all listeners exclude it. * A source is only included if no listeners exclude it, * and at least one listener includes it. * May be used by ifmcstat(8). */ uint8_t ims_get_mode(const struct in_multi *inm, const struct ip_msource *ims, uint8_t t) { INM_LOCK_ASSERT_HELD(__DECONST(struct in_multi *, inm)); t = !!t; if (inm->inm_st[t].iss_ex > 0 && inm->inm_st[t].iss_ex == ims->ims_st[t].ex) { return MCAST_EXCLUDE; } else if (ims->ims_st[t].in > 0 && ims->ims_st[t].ex == 0) { return MCAST_INCLUDE; } return MCAST_UNDEFINED; } /* * Merge socket-layer source into IGMP-layer source. * If rollback is non-zero, perform the inverse of the merge. */ static void ims_merge(struct ip_msource *ims, const struct in_msource *lims, const int rollback) { int n = rollback ? -1 : 1; #ifdef IGMP_DEBUG struct in_addr ia; ia.s_addr = htonl(ims->ims_haddr); #endif if (lims->imsl_st[0] == MCAST_EXCLUDE) { IGMP_INET_PRINTF(ia, ("%s: t1 ex -= %d on %s\n", __func__, n, _igmp_inet_buf)); ims->ims_st[1].ex -= n; } else if (lims->imsl_st[0] == MCAST_INCLUDE) { IGMP_INET_PRINTF(ia, ("%s: t1 in -= %d on %s\n", __func__, n, _igmp_inet_buf)); ims->ims_st[1].in -= n; } if (lims->imsl_st[1] == MCAST_EXCLUDE) { IGMP_INET_PRINTF(ia, ("%s: t1 ex += %d on %s\n", __func__, n, _igmp_inet_buf)); ims->ims_st[1].ex += n; } else if (lims->imsl_st[1] == MCAST_INCLUDE) { IGMP_INET_PRINTF(ia, ("%s: t1 in += %d on %s\n", __func__, n, _igmp_inet_buf)); ims->ims_st[1].in += n; } } /* * Atomically update the global in_multi state, when a membership's * filter list is being updated in any way. * * imf is the per-inpcb-membership group filter pointer. * A fake imf may be passed for in-kernel consumers. * * XXX This is a candidate for a set-symmetric-difference style loop * which would eliminate the repeated lookup from root of ims nodes, * as they share the same key space. * * If any error occurred this function will back out of refcounts * and return a non-zero value. */ static int inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf) { struct ip_msource *ims, *nims = NULL; struct in_msource *lims; int schanged, error; int nsrc0, nsrc1; INM_LOCK_ASSERT_HELD(inm); schanged = 0; error = 0; nsrc1 = nsrc0 = 0; /* * Update the source filters first, as this may fail. * Maintain count of in-mode filters at t0, t1. These are * used to work out if we transition into ASM mode or not. * Maintain a count of source filters whose state was * actually modified by this operation. */ RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { lims = (struct in_msource *)ims; if (lims->imsl_st[0] == imf->imf_st[0]) { nsrc0++; } if (lims->imsl_st[1] == imf->imf_st[1]) { nsrc1++; } if (lims->imsl_st[0] == lims->imsl_st[1]) { continue; } error = inm_get_source(inm, lims->ims_haddr, 0, &nims); ++schanged; if (error) { break; } ims_merge(nims, lims, 0); } if (error) { struct ip_msource *bims; RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) { lims = (struct in_msource *)ims; if (lims->imsl_st[0] == lims->imsl_st[1]) { continue; } (void) inm_get_source(inm, lims->ims_haddr, 1, &bims); if (bims == NULL) { continue; } ims_merge(bims, lims, 1); } goto out_reap; } IGMP_PRINTF(("%s: imf filters in-mode: %d at t0, %d at t1\n", __func__, nsrc0, nsrc1)); /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */ if (imf->imf_st[0] == imf->imf_st[1] && imf->imf_st[1] == MCAST_INCLUDE) { if (nsrc1 == 0) { IGMP_PRINTF(("%s: --in on inm at t1\n", __func__)); --inm->inm_st[1].iss_in; } } /* Handle filter mode transition on socket. */ if (imf->imf_st[0] != imf->imf_st[1]) { IGMP_PRINTF(("%s: imf transition %d to %d\n", __func__, imf->imf_st[0], imf->imf_st[1])); if (imf->imf_st[0] == MCAST_EXCLUDE) { IGMP_PRINTF(("%s: --ex on inm at t1\n", __func__)); --inm->inm_st[1].iss_ex; } else if (imf->imf_st[0] == MCAST_INCLUDE) { IGMP_PRINTF(("%s: --in on inm at t1\n", __func__)); --inm->inm_st[1].iss_in; } if (imf->imf_st[1] == MCAST_EXCLUDE) { IGMP_PRINTF(("%s: ex++ on inm at t1\n", __func__)); inm->inm_st[1].iss_ex++; } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) { IGMP_PRINTF(("%s: in++ on inm at t1\n", __func__)); inm->inm_st[1].iss_in++; } } /* * Track inm filter state in terms of listener counts. * If there are any exclusive listeners, stack-wide * membership is exclusive. * Otherwise, if only inclusive listeners, stack-wide is inclusive. * If no listeners remain, state is undefined at t1, * and the IGMP lifecycle for this group should finish. */ if (inm->inm_st[1].iss_ex > 0) { IGMP_PRINTF(("%s: transition to EX\n", __func__)); inm->inm_st[1].iss_fmode = MCAST_EXCLUDE; } else if (inm->inm_st[1].iss_in > 0) { IGMP_PRINTF(("%s: transition to IN\n", __func__)); inm->inm_st[1].iss_fmode = MCAST_INCLUDE; } else { IGMP_PRINTF(("%s: transition to UNDEF\n", __func__)); inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; } /* Decrement ASM listener count on transition out of ASM mode. */ if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) { if ((imf->imf_st[1] != MCAST_EXCLUDE) || (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) { IGMP_PRINTF(("%s: --asm on inm at t1\n", __func__)); --inm->inm_st[1].iss_asm; } } /* Increment ASM listener count on transition to ASM mode. */ if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) { IGMP_PRINTF(("%s: asm++ on inm at t1\n", __func__)); inm->inm_st[1].iss_asm++; } IGMP_PRINTF(("%s: merged imf 0x%llx to inm 0x%llx\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(imf), (uint64_t)VM_KERNEL_ADDRPERM(inm))); inm_print(inm); out_reap: if (schanged > 0) { IGMP_PRINTF(("%s: sources changed; reaping\n", __func__)); inm_reap(inm); } return error; } /* * Mark an in_multi's filter set deltas as committed. * Called by IGMP after a state change has been enqueued. */ void inm_commit(struct in_multi *inm) { struct ip_msource *ims; INM_LOCK_ASSERT_HELD(inm); IGMP_PRINTF(("%s: commit inm 0x%llx\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(inm))); IGMP_PRINTF(("%s: pre commit:\n", __func__)); inm_print(inm); RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { ims->ims_st[0] = ims->ims_st[1]; } inm->inm_st[0] = inm->inm_st[1]; } /* * Reap unreferenced nodes from an in_multi's filter set. */ static void inm_reap(struct in_multi *inm) { struct ip_msource *ims, *tims; INM_LOCK_ASSERT_HELD(inm); RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) { if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 || ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 || ims->ims_stp != 0) { continue; } IGMP_PRINTF(("%s: free ims 0x%llx\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(ims))); RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims); ipms_free(ims); inm->inm_nsrc--; } } /* * Purge all source nodes from an in_multi's filter set. */ void inm_purge(struct in_multi *inm) { struct ip_msource *ims, *tims; INM_LOCK_ASSERT_HELD(inm); RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) { IGMP_PRINTF(("%s: free ims 0x%llx\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(ims))); RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims); ipms_free(ims); inm->inm_nsrc--; } } /* * Join a multicast group; real entry point. * * Only preserves atomicity at inm level. * NOTE: imf argument cannot be const due to sys/tree.h limitations. * * If the IGMP downcall fails, the group is not joined, and an error * code is returned. */ static int in_joingroup(struct ifnet *ifp, const struct in_addr *gina, /*const*/ struct in_mfilter *imf, struct in_multi **pinm) { struct in_mfilter timf; struct in_multi *inm = NULL; int error = 0; struct igmp_tparams itp; IGMP_INET_PRINTF(*gina, ("%s: join %s on 0x%llx(%s))\n", __func__, _igmp_inet_buf, (uint64_t)VM_KERNEL_ADDRPERM(ifp), if_name(ifp))); bzero(&itp, sizeof(itp)); *pinm = NULL; /* * If no imf was specified (i.e. kernel consumer), * fake one up and assume it is an ASM join. */ if (imf == NULL) { imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE); imf = &timf; } error = in_getmulti(ifp, gina, &inm); if (error) { IGMP_PRINTF(("%s: in_getmulti() failure\n", __func__)); return error; } IGMP_PRINTF(("%s: merge inm state\n", __func__)); INM_LOCK(inm); error = inm_merge(inm, imf); if (error) { IGMP_PRINTF(("%s: failed to merge inm state\n", __func__)); goto out_inm_release; } IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); error = igmp_change_state(inm, &itp); if (error) { IGMP_PRINTF(("%s: failed to update source\n", __func__)); imf_rollback(imf); goto out_inm_release; } out_inm_release: if (error) { IGMP_PRINTF(("%s: dropping ref on 0x%llx\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(inm))); INM_UNLOCK(inm); INM_REMREF(inm); } else { INM_UNLOCK(inm); *pinm = inm; /* keep refcount from in_getmulti() */ } /* schedule timer now that we've dropped the lock(s) */ igmp_set_fast_timeout(&itp); return error; } /* * Leave a multicast group; real entry point. * All source filters will be expunged. * * Only preserves atomicity at inm level. * * Note: This is not the same as inm_release(*) as this function also * makes a state change downcall into IGMP. */ int in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf) { struct in_mfilter timf; int error, lastref; struct igmp_tparams itp; bzero(&itp, sizeof(itp)); error = 0; INM_LOCK_ASSERT_NOTHELD(inm); in_multihead_lock_exclusive(); INM_LOCK(inm); IGMP_INET_PRINTF(inm->inm_addr, ("%s: leave inm 0x%llx, %s/%s%d, imf 0x%llx\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(inm), _igmp_inet_buf, (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_name), inm->inm_ifp->if_unit, (uint64_t)VM_KERNEL_ADDRPERM(imf))); /* * If no imf was specified (i.e. kernel consumer), * fake one up and assume it is an ASM join. */ if (imf == NULL) { imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED); imf = &timf; } /* * Begin state merge transaction at IGMP layer. * * As this particular invocation should not cause any memory * to be allocated, and there is no opportunity to roll back * the transaction, it MUST NOT fail. */ IGMP_PRINTF(("%s: merge inm state\n", __func__)); error = inm_merge(inm, imf); KASSERT(error == 0, ("%s: failed to merge inm state\n", __func__)); IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); error = igmp_change_state(inm, &itp); #if IGMP_DEBUG if (error) { IGMP_PRINTF(("%s: failed igmp downcall\n", __func__)); } #endif lastref = in_multi_detach(inm); VERIFY(!lastref || (!(inm->inm_debug & IFD_ATTACHED) && inm->inm_reqcnt == 0)); INM_UNLOCK(inm); in_multihead_lock_done(); if (lastref) { INM_REMREF(inm); /* for in_multihead list */ } /* schedule timer now that we've dropped the lock(s) */ igmp_set_fast_timeout(&itp); return error; } /* * Join an IPv4 multicast group in (*,G) exclusive mode. * The group must be a 224.0.0.0/24 link-scope group. * This KPI is for legacy kernel consumers only. */ struct in_multi * in_addmulti(struct in_addr *ap, struct ifnet *ifp) { struct in_multi *pinm = NULL; int error; KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)), ("%s: %s not in 224.0.0.0/24\n", __func__, inet_ntoa(*ap))); error = in_joingroup(ifp, ap, NULL, &pinm); VERIFY(pinm != NULL || error != 0); return pinm; } /* * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode. * This KPI is for legacy kernel consumers only. */ void in_delmulti(struct in_multi *inm) { (void) in_leavegroup(inm, NULL); } /* * Block or unblock an ASM multicast source on an inpcb. * This implements the delta-based API described in RFC 3678. * * The delta-based API applies only to exclusive-mode memberships. * An IGMP downcall will be performed. * * Return 0 if successful, otherwise return an appropriate error code. */ static int inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt) { struct group_source_req gsr; struct sockaddr_in *gsa, *ssa; struct ifnet *ifp; struct in_mfilter *imf; struct ip_moptions *imo; struct in_msource *ims; struct in_multi *inm; size_t idx; uint8_t fmode; int error, doblock; unsigned int ifindex = 0; struct igmp_tparams itp; bzero(&itp, sizeof(itp)); ifp = NULL; error = 0; doblock = 0; memset(&gsr, 0, sizeof(struct group_source_req)); gsa = (struct sockaddr_in *)&gsr.gsr_group; ssa = (struct sockaddr_in *)&gsr.gsr_source; switch (sopt->sopt_name) { case IP_BLOCK_SOURCE: case IP_UNBLOCK_SOURCE: { struct ip_mreq_source mreqs; error = sooptcopyin(sopt, &mreqs, sizeof(struct ip_mreq_source), sizeof(struct ip_mreq_source)); if (error) { return error; } gsa->sin_family = AF_INET; gsa->sin_len = sizeof(struct sockaddr_in); gsa->sin_addr = mreqs.imr_multiaddr; ssa->sin_family = AF_INET; ssa->sin_len = sizeof(struct sockaddr_in); ssa->sin_addr = mreqs.imr_sourceaddr; if (!in_nullhost(mreqs.imr_interface)) { ifp = ip_multicast_if(&mreqs.imr_interface, &ifindex); } if (sopt->sopt_name == IP_BLOCK_SOURCE) { doblock = 1; } IGMP_INET_PRINTF(mreqs.imr_interface, ("%s: imr_interface = %s, ifp = 0x%llx\n", __func__, _igmp_inet_buf, (uint64_t)VM_KERNEL_ADDRPERM(ifp))); break; } case MCAST_BLOCK_SOURCE: case MCAST_UNBLOCK_SOURCE: error = sooptcopyin(sopt, &gsr, sizeof(struct group_source_req), sizeof(struct group_source_req)); if (error) { return error; } if (gsa->sin_family != AF_INET || gsa->sin_len != sizeof(struct sockaddr_in)) { return EINVAL; } if (ssa->sin_family != AF_INET || ssa->sin_len != sizeof(struct sockaddr_in)) { return EINVAL; } ifnet_head_lock_shared(); if (gsr.gsr_interface == 0 || (u_int)if_index < gsr.gsr_interface) { ifnet_head_done(); return EADDRNOTAVAIL; } ifp = ifindex2ifnet[gsr.gsr_interface]; ifnet_head_done(); if (ifp == NULL) { return EADDRNOTAVAIL; } if (sopt->sopt_name == MCAST_BLOCK_SOURCE) { doblock = 1; } break; default: IGMP_PRINTF(("%s: unknown sopt_name %d\n", __func__, sopt->sopt_name)); return EOPNOTSUPP; } if (!IN_MULTICAST(ntohl(gsa->sin_addr.s_addr))) { return EINVAL; } /* * Check if we are actually a member of this group. */ imo = inp_findmoptions(inp); if (imo == NULL) { return ENOMEM; } IMO_LOCK(imo); idx = imo_match_group(imo, ifp, gsa); if (idx == (size_t)-1 || imo->imo_mfilters == NULL) { error = EADDRNOTAVAIL; goto out_imo_locked; } VERIFY(imo->imo_mfilters != NULL); imf = &imo->imo_mfilters[idx]; inm = imo->imo_membership[idx]; /* * Attempting to use the delta-based API on an * non exclusive-mode membership is an error. */ fmode = imf->imf_st[0]; if (fmode != MCAST_EXCLUDE) { error = EINVAL; goto out_imo_locked; } /* * Deal with error cases up-front: * Asked to block, but already blocked; or * Asked to unblock, but nothing to unblock. * If adding a new block entry, allocate it. */ ims = imo_match_source(imo, idx, ssa); if ((ims != NULL && doblock) || (ims == NULL && !doblock)) { IGMP_INET_PRINTF(ssa->sin_addr, ("%s: source %s %spresent\n", __func__, _igmp_inet_buf, doblock ? "" : "not ")); error = EADDRNOTAVAIL; goto out_imo_locked; } /* * Begin state merge transaction at socket layer. */ if (doblock) { IGMP_PRINTF(("%s: %s source\n", __func__, "block")); ims = imf_graft(imf, fmode, ssa); if (ims == NULL) { error = ENOMEM; } } else { IGMP_PRINTF(("%s: %s source\n", __func__, "allow")); error = imf_prune(imf, ssa); } if (error) { IGMP_PRINTF(("%s: merge imf state failed\n", __func__)); goto out_imf_rollback; } /* * Begin state merge transaction at IGMP layer. */ INM_LOCK(inm); IGMP_PRINTF(("%s: merge inm state\n", __func__)); error = inm_merge(inm, imf); if (error) { IGMP_PRINTF(("%s: failed to merge inm state\n", __func__)); INM_UNLOCK(inm); goto out_imf_rollback; } IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); error = igmp_change_state(inm, &itp); INM_UNLOCK(inm); #if IGMP_DEBUG if (error) { IGMP_PRINTF(("%s: failed igmp downcall\n", __func__)); } #endif out_imf_rollback: if (error) { imf_rollback(imf); } else { imf_commit(imf); } imf_reap(imf); out_imo_locked: IMO_UNLOCK(imo); IMO_REMREF(imo); /* from inp_findmoptions() */ /* schedule timer now that we've dropped the lock(s) */ igmp_set_fast_timeout(&itp); return error; } /* * Given an inpcb, return its multicast options structure pointer. * * Caller is responsible for locking the inpcb, and releasing the * extra reference held on the imo, upon a successful return. */ static struct ip_moptions * inp_findmoptions(struct inpcb *inp) { struct ip_moptions *imo; struct in_multi **immp; struct in_mfilter *imfp; size_t idx; if ((imo = inp->inp_moptions) != NULL) { IMO_ADDREF(imo); /* for caller */ return imo; } imo = ip_allocmoptions(Z_WAITOK); if (imo == NULL) { return NULL; } immp = kalloc_type(struct in_multi *, IP_MIN_MEMBERSHIPS, Z_WAITOK | Z_ZERO | Z_NOFAIL); imfp = kalloc_type(struct in_mfilter, IP_MIN_MEMBERSHIPS, Z_WAITOK | Z_ZERO | Z_NOFAIL); imo->imo_multicast_ifp = NULL; imo->imo_multicast_addr.s_addr = INADDR_ANY; imo->imo_multicast_vif = -1; imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; imo->imo_multicast_loop = !!in_mcast_loop; imo->imo_num_memberships = 0; imo->imo_max_memberships = IP_MIN_MEMBERSHIPS; imo->imo_membership = immp; /* Initialize per-group source filters. */ for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++) { imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE); } imo->imo_mfilters = imfp; inp->inp_moptions = imo; /* keep reference from ip_allocmoptions() */ IMO_ADDREF(imo); /* for caller */ return imo; } /* * Atomically get source filters on a socket for an IPv4 multicast group. */ static int inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt) { struct __msfilterreq64 msfr = {}, msfr64; struct __msfilterreq32 msfr32; struct sockaddr_in *gsa; struct ifnet *ifp; struct ip_moptions *imo; struct in_mfilter *imf; struct ip_msource *ims; struct in_msource *lims; struct sockaddr_in *psin; struct sockaddr_storage *ptss; struct sockaddr_storage *tss; int error; size_t idx; uint32_t nsrcs, ncsrcs; user_addr_t tmp_ptr; imo = inp->inp_moptions; VERIFY(imo != NULL); int is_64bit_proc = IS_64BIT_PROCESS(current_proc()); if (is_64bit_proc) { error = sooptcopyin(sopt, &msfr64, sizeof(struct __msfilterreq64), sizeof(struct __msfilterreq64)); if (error) { return error; } /* we never use msfr.msfr_srcs; */ memcpy(&msfr, &msfr64, sizeof(msfr64)); } else { error = sooptcopyin(sopt, &msfr32, sizeof(struct __msfilterreq32), sizeof(struct __msfilterreq32)); if (error) { return error; } /* we never use msfr.msfr_srcs; */ memcpy(&msfr, &msfr32, sizeof(msfr32)); } ifnet_head_lock_shared(); if (msfr.msfr_ifindex == 0 || (u_int)if_index < msfr.msfr_ifindex) { ifnet_head_done(); return EADDRNOTAVAIL; } ifp = ifindex2ifnet[msfr.msfr_ifindex]; ifnet_head_done(); if (ifp == NULL) { return EADDRNOTAVAIL; } if ((size_t) msfr.msfr_nsrcs > UINT32_MAX / sizeof(struct sockaddr_storage)) { msfr.msfr_nsrcs = UINT32_MAX / sizeof(struct sockaddr_storage); } if (msfr.msfr_nsrcs > in_mcast_maxsocksrc) { msfr.msfr_nsrcs = in_mcast_maxsocksrc; } IMO_LOCK(imo); /* * Lookup group on the socket. */ gsa = (struct sockaddr_in *)&msfr.msfr_group; idx = imo_match_group(imo, ifp, gsa); if (idx == (size_t)-1 || imo->imo_mfilters == NULL) { IMO_UNLOCK(imo); return EADDRNOTAVAIL; } imf = &imo->imo_mfilters[idx]; /* * Ignore memberships which are in limbo. */ if (imf->imf_st[1] == MCAST_UNDEFINED) { IMO_UNLOCK(imo); return EAGAIN; } msfr.msfr_fmode = imf->imf_st[1]; /* * If the user specified a buffer, copy out the source filter * entries to userland gracefully. * We only copy out the number of entries which userland * has asked for, but we always tell userland how big the * buffer really needs to be. */ if (is_64bit_proc) { tmp_ptr = CAST_USER_ADDR_T(msfr64.msfr_srcs); } else { tmp_ptr = CAST_USER_ADDR_T(msfr32.msfr_srcs); } tss = NULL; if (tmp_ptr != USER_ADDR_NULL && msfr.msfr_nsrcs > 0) { tss = kalloc_data((size_t)msfr.msfr_nsrcs * sizeof(*tss), Z_WAITOK | Z_ZERO); if (tss == NULL) { IMO_UNLOCK(imo); return ENOBUFS; } } /* * Count number of sources in-mode at t0. * If buffer space exists and remains, copy out source entries. */ nsrcs = msfr.msfr_nsrcs; ncsrcs = 0; ptss = tss; RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { lims = (struct in_msource *)ims; if (lims->imsl_st[0] == MCAST_UNDEFINED || lims->imsl_st[0] != imf->imf_st[0]) { continue; } if (tss != NULL && nsrcs > 0) { psin = (struct sockaddr_in *)ptss; psin->sin_family = AF_INET; psin->sin_len = sizeof(struct sockaddr_in); psin->sin_addr.s_addr = htonl(lims->ims_haddr); psin->sin_port = 0; ++ptss; --nsrcs; ++ncsrcs; } } IMO_UNLOCK(imo); if (tss != NULL) { error = copyout(tss, CAST_USER_ADDR_T(tmp_ptr), ncsrcs * sizeof(*tss)); kfree_data(tss, (size_t)msfr.msfr_nsrcs * sizeof(*tss)); if (error) { return error; } } msfr.msfr_nsrcs = ncsrcs; if (is_64bit_proc) { msfr64.msfr_ifindex = msfr.msfr_ifindex; msfr64.msfr_fmode = msfr.msfr_fmode; msfr64.msfr_nsrcs = msfr.msfr_nsrcs; memcpy(&msfr64.msfr_group, &msfr.msfr_group, sizeof(struct sockaddr_storage)); error = sooptcopyout(sopt, &msfr64, sizeof(struct __msfilterreq64)); } else { msfr32.msfr_ifindex = msfr.msfr_ifindex; msfr32.msfr_fmode = msfr.msfr_fmode; msfr32.msfr_nsrcs = msfr.msfr_nsrcs; memcpy(&msfr32.msfr_group, &msfr.msfr_group, sizeof(struct sockaddr_storage)); error = sooptcopyout(sopt, &msfr32, sizeof(struct __msfilterreq32)); } return error; } /* * Return the IP multicast options in response to user getsockopt(). */ int inp_getmoptions(struct inpcb *inp, struct sockopt *sopt) { struct ip_mreqn mreqn; struct ip_moptions *imo; struct ifnet *ifp; struct in_ifaddr *ia; int error, optval; unsigned int ifindex; u_char coptval; imo = inp->inp_moptions; /* * If socket is neither of type SOCK_RAW or SOCK_DGRAM, * or is a divert socket, reject it. */ if (SOCK_PROTO(inp->inp_socket) == IPPROTO_DIVERT || (SOCK_TYPE(inp->inp_socket) != SOCK_RAW && SOCK_TYPE(inp->inp_socket) != SOCK_DGRAM)) { return EOPNOTSUPP; } error = 0; switch (sopt->sopt_name) { case IP_MULTICAST_IF: memset(&mreqn, 0, sizeof(struct ip_mreqn)); if (imo != NULL) { IMO_LOCK(imo); ifp = imo->imo_multicast_ifp; if (!in_nullhost(imo->imo_multicast_addr)) { mreqn.imr_address = imo->imo_multicast_addr; } else if (ifp != NULL) { mreqn.imr_ifindex = ifp->if_index; IFP_TO_IA(ifp, ia); if (ia != NULL) { IFA_LOCK_SPIN(&ia->ia_ifa); mreqn.imr_address = IA_SIN(ia)->sin_addr; IFA_UNLOCK(&ia->ia_ifa); IFA_REMREF(&ia->ia_ifa); } } IMO_UNLOCK(imo); } if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) { error = sooptcopyout(sopt, &mreqn, sizeof(struct ip_mreqn)); } else { error = sooptcopyout(sopt, &mreqn.imr_address, sizeof(struct in_addr)); } break; case IP_MULTICAST_IFINDEX: if (imo != NULL) { IMO_LOCK(imo); } if (imo == NULL || imo->imo_multicast_ifp == NULL) { ifindex = 0; } else { ifindex = imo->imo_multicast_ifp->if_index; } if (imo != NULL) { IMO_UNLOCK(imo); } error = sooptcopyout(sopt, &ifindex, sizeof(ifindex)); break; case IP_MULTICAST_TTL: if (imo == NULL) { optval = coptval = IP_DEFAULT_MULTICAST_TTL; } else { IMO_LOCK(imo); optval = coptval = imo->imo_multicast_ttl; IMO_UNLOCK(imo); } if (sopt->sopt_valsize == sizeof(u_char)) { error = sooptcopyout(sopt, &coptval, sizeof(u_char)); } else { error = sooptcopyout(sopt, &optval, sizeof(int)); } break; case IP_MULTICAST_LOOP: if (imo == 0) { optval = coptval = IP_DEFAULT_MULTICAST_LOOP; } else { IMO_LOCK(imo); optval = coptval = imo->imo_multicast_loop; IMO_UNLOCK(imo); } if (sopt->sopt_valsize == sizeof(u_char)) { error = sooptcopyout(sopt, &coptval, sizeof(u_char)); } else { error = sooptcopyout(sopt, &optval, sizeof(int)); } break; case IP_MSFILTER: if (imo == NULL) { error = EADDRNOTAVAIL; } else { error = inp_get_source_filters(inp, sopt); } break; default: error = ENOPROTOOPT; break; } return error; } /* * Look up the ifnet to use for a multicast group membership, * given the IPv4 address of an interface, and the IPv4 group address. * * This routine exists to support legacy multicast applications * which do not understand that multicast memberships are scoped to * specific physical links in the networking stack, or which need * to join link-scope groups before IPv4 addresses are configured. * * If inp is non-NULL and is bound to an interface, use this socket's * inp_boundif for any required routing table lookup. * * If the route lookup fails, attempt to use the first non-loopback * interface with multicast capability in the system as a * last resort. The legacy IPv4 ASM API requires that we do * this in order to allow groups to be joined when the routing * table has not yet been populated during boot. * * Returns NULL if no ifp could be found. * */ static struct ifnet * inp_lookup_mcast_ifp(const struct inpcb *inp, const struct sockaddr_in *gsin, const struct in_addr ina) { struct ifnet *ifp; unsigned int ifindex = 0; VERIFY(gsin->sin_family == AF_INET); VERIFY(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr))); ifp = NULL; if (!in_nullhost(ina)) { struct in_addr new_ina; memcpy(&new_ina, &ina, sizeof(struct in_addr)); ifp = ip_multicast_if(&new_ina, &ifindex); } else { struct route ro; unsigned int ifscope = IFSCOPE_NONE; if (inp != NULL && (inp->inp_flags & INP_BOUND_IF)) { ifscope = inp->inp_boundifp->if_index; } bzero(&ro, sizeof(ro)); memcpy(&ro.ro_dst, gsin, sizeof(struct sockaddr_in)); rtalloc_scoped_ign(&ro, 0, ifscope); if (ro.ro_rt != NULL) { ifp = ro.ro_rt->rt_ifp; VERIFY(ifp != NULL); } else { struct in_ifaddr *ia; struct ifnet *mifp; mifp = NULL; lck_rw_lock_shared(&in_ifaddr_rwlock); TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { IFA_LOCK_SPIN(&ia->ia_ifa); mifp = ia->ia_ifp; IFA_UNLOCK(&ia->ia_ifa); if (!(mifp->if_flags & IFF_LOOPBACK) && (mifp->if_flags & IFF_MULTICAST)) { ifp = mifp; break; } } lck_rw_done(&in_ifaddr_rwlock); } ROUTE_RELEASE(&ro); } return ifp; } /* * Join an IPv4 multicast group, possibly with a source. * * NB: sopt->sopt_val might point to the kernel address space. This means that * we were called by the IPv6 stack due to the presence of an IPv6 v4 mapped * address. In this scenario, sopt_p points to kernproc and sooptcopyin() will * just issue an in-kernel memcpy. */ int inp_join_group(struct inpcb *inp, struct sockopt *sopt) { struct group_source_req gsr; struct sockaddr_in *gsa, *ssa; struct ifnet *ifp; struct in_mfilter *imf; struct ip_moptions *imo; struct in_multi *inm = NULL; struct in_msource *lims; size_t idx; int error, is_new; struct igmp_tparams itp; bzero(&itp, sizeof(itp)); ifp = NULL; imf = NULL; error = 0; is_new = 0; memset(&gsr, 0, sizeof(struct group_source_req)); gsa = (struct sockaddr_in *)&gsr.gsr_group; gsa->sin_family = AF_UNSPEC; ssa = (struct sockaddr_in *)&gsr.gsr_source; ssa->sin_family = AF_UNSPEC; switch (sopt->sopt_name) { case IP_ADD_MEMBERSHIP: case IP_ADD_SOURCE_MEMBERSHIP: { struct ip_mreq_source mreqs; if (sopt->sopt_name == IP_ADD_MEMBERSHIP) { error = sooptcopyin(sopt, &mreqs, sizeof(struct ip_mreq), sizeof(struct ip_mreq)); /* * Do argument switcharoo from ip_mreq into * ip_mreq_source to avoid using two instances. */ mreqs.imr_interface = mreqs.imr_sourceaddr; mreqs.imr_sourceaddr.s_addr = INADDR_ANY; } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) { error = sooptcopyin(sopt, &mreqs, sizeof(struct ip_mreq_source), sizeof(struct ip_mreq_source)); } if (error) { IGMP_PRINTF(("%s: error copyin IP_ADD_MEMBERSHIP/" "IP_ADD_SOURCE_MEMBERSHIP %d err=%d\n", __func__, sopt->sopt_name, error)); return error; } gsa->sin_family = AF_INET; gsa->sin_len = sizeof(struct sockaddr_in); gsa->sin_addr = mreqs.imr_multiaddr; if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) { ssa->sin_family = AF_INET; ssa->sin_len = sizeof(struct sockaddr_in); ssa->sin_addr = mreqs.imr_sourceaddr; } if (!IN_MULTICAST(ntohl(gsa->sin_addr.s_addr))) { return EINVAL; } ifp = inp_lookup_mcast_ifp(inp, gsa, mreqs.imr_interface); IGMP_INET_PRINTF(mreqs.imr_interface, ("%s: imr_interface = %s, ifp = 0x%llx\n", __func__, _igmp_inet_buf, (uint64_t)VM_KERNEL_ADDRPERM(ifp))); break; } case MCAST_JOIN_GROUP: case MCAST_JOIN_SOURCE_GROUP: if (sopt->sopt_name == MCAST_JOIN_GROUP) { error = sooptcopyin(sopt, &gsr, sizeof(struct group_req), sizeof(struct group_req)); } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { error = sooptcopyin(sopt, &gsr, sizeof(struct group_source_req), sizeof(struct group_source_req)); } if (error) { return error; } if (gsa->sin_family != AF_INET || gsa->sin_len != sizeof(struct sockaddr_in)) { return EINVAL; } /* * Overwrite the port field if present, as the sockaddr * being copied in may be matched with a binary comparison. */ gsa->sin_port = 0; if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { if (ssa->sin_family != AF_INET || ssa->sin_len != sizeof(struct sockaddr_in)) { return EINVAL; } ssa->sin_port = 0; } if (!IN_MULTICAST(ntohl(gsa->sin_addr.s_addr))) { return EINVAL; } ifnet_head_lock_shared(); if (gsr.gsr_interface == 0 || (u_int)if_index < gsr.gsr_interface) { ifnet_head_done(); return EADDRNOTAVAIL; } ifp = ifindex2ifnet[gsr.gsr_interface]; ifnet_head_done(); if (ifp == NULL) { return EADDRNOTAVAIL; } break; default: IGMP_PRINTF(("%s: unknown sopt_name %d\n", __func__, sopt->sopt_name)); return EOPNOTSUPP; } if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { return EADDRNOTAVAIL; } INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_mcast_join_total); /* * TBD: revisit the criteria for non-OS initiated joins */ if (inp->inp_lport == htons(5353)) { INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_mcast_join_os_total); } imo = inp_findmoptions(inp); if (imo == NULL) { return ENOMEM; } IMO_LOCK(imo); idx = imo_match_group(imo, ifp, gsa); if (idx == (size_t)-1) { is_new = 1; } else { inm = imo->imo_membership[idx]; imf = &imo->imo_mfilters[idx]; if (ssa->sin_family != AF_UNSPEC) { /* * MCAST_JOIN_SOURCE_GROUP on an exclusive membership * is an error. On an existing inclusive membership, * it just adds the source to the filter list. */ if (imf->imf_st[1] != MCAST_INCLUDE) { error = EINVAL; goto out_imo_locked; } /* * Throw out duplicates. * * XXX FIXME: This makes a naive assumption that * even if entries exist for *ssa in this imf, * they will be rejected as dupes, even if they * are not valid in the current mode (in-mode). * * in_msource is transactioned just as for anything * else in SSM -- but note naive use of inm_graft() * below for allocating new filter entries. * * This is only an issue if someone mixes the * full-state SSM API with the delta-based API, * which is discouraged in the relevant RFCs. */ lims = imo_match_source(imo, idx, ssa); if (lims != NULL /*&& * lims->imsl_st[1] == MCAST_INCLUDE*/) { error = EADDRNOTAVAIL; goto out_imo_locked; } } else { /* * MCAST_JOIN_GROUP on an existing exclusive * membership is an error; return EADDRINUSE * to preserve 4.4BSD API idempotence, and * avoid tedious detour to code below. * NOTE: This is bending RFC 3678 a bit. * * On an existing inclusive membership, this is also * an error; if you want to change filter mode, * you must use the userland API setsourcefilter(). * XXX We don't reject this for imf in UNDEFINED * state at t1, because allocation of a filter * is atomic with allocation of a membership. */ error = EINVAL; /* See comments above for EADDRINUSE */ if (imf->imf_st[1] == MCAST_EXCLUDE) { error = EADDRINUSE; } goto out_imo_locked; } } /* * Begin state merge transaction at socket layer. */ if (is_new) { if (imo->imo_num_memberships == imo->imo_max_memberships) { error = imo_grow(imo, 0); if (error) { goto out_imo_locked; } } /* * Allocate the new slot upfront so we can deal with * grafting the new source filter in same code path * as for join-source on existing membership. */ idx = imo->imo_num_memberships; imo->imo_membership[idx] = NULL; imo->imo_num_memberships++; VERIFY(imo->imo_mfilters != NULL); imf = &imo->imo_mfilters[idx]; VERIFY(RB_EMPTY(&imf->imf_sources)); } /* * Graft new source into filter list for this inpcb's * membership of the group. The in_multi may not have * been allocated yet if this is a new membership, however, * the in_mfilter slot will be allocated and must be initialized. */ if (ssa->sin_family != AF_UNSPEC) { /* Membership starts in IN mode */ if (is_new) { IGMP_PRINTF(("%s: new join w/source\n", __func__)); imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE); } else { IGMP_PRINTF(("%s: %s source\n", __func__, "allow")); } lims = imf_graft(imf, MCAST_INCLUDE, ssa); if (lims == NULL) { IGMP_PRINTF(("%s: merge imf state failed\n", __func__)); error = ENOMEM; goto out_imo_free; } } else { /* No address specified; Membership starts in EX mode */ if (is_new) { IGMP_PRINTF(("%s: new join w/o source\n", __func__)); imf_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE); } } /* * Begin state merge transaction at IGMP layer. */ if (is_new) { VERIFY(inm == NULL); error = in_joingroup(ifp, &gsa->sin_addr, imf, &inm); VERIFY(inm != NULL || error != 0); if (error) { goto out_imo_free; } imo->imo_membership[idx] = inm; /* from in_joingroup() */ } else { IGMP_PRINTF(("%s: merge inm state\n", __func__)); INM_LOCK(inm); error = inm_merge(inm, imf); if (error) { IGMP_PRINTF(("%s: failed to merge inm state\n", __func__)); INM_UNLOCK(inm); goto out_imf_rollback; } IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); error = igmp_change_state(inm, &itp); INM_UNLOCK(inm); if (error) { IGMP_PRINTF(("%s: failed igmp downcall\n", __func__)); goto out_imf_rollback; } } out_imf_rollback: if (error) { imf_rollback(imf); if (is_new) { imf_purge(imf); } else { imf_reap(imf); } } else { imf_commit(imf); } out_imo_free: if (error && is_new) { VERIFY(inm == NULL); imo->imo_membership[idx] = NULL; --imo->imo_num_memberships; } out_imo_locked: IMO_UNLOCK(imo); IMO_REMREF(imo); /* from inp_findmoptions() */ /* schedule timer now that we've dropped the lock(s) */ igmp_set_fast_timeout(&itp); return error; } /* * Leave an IPv4 multicast group on an inpcb, possibly with a source. * * NB: sopt->sopt_val might point to the kernel address space. Refer to the * block comment on top of inp_join_group() for more information. */ int inp_leave_group(struct inpcb *inp, struct sockopt *sopt) { struct group_source_req gsr; struct ip_mreq_source mreqs; struct sockaddr_in *gsa, *ssa; struct ifnet *ifp; struct in_mfilter *imf; struct ip_moptions *imo; struct in_msource *ims; struct in_multi *inm = NULL; size_t idx; int error, is_final; unsigned int ifindex = 0; struct igmp_tparams itp; bzero(&itp, sizeof(itp)); ifp = NULL; error = 0; is_final = 1; memset(&gsr, 0, sizeof(struct group_source_req)); gsa = (struct sockaddr_in *)&gsr.gsr_group; ssa = (struct sockaddr_in *)&gsr.gsr_source; switch (sopt->sopt_name) { case IP_DROP_MEMBERSHIP: case IP_DROP_SOURCE_MEMBERSHIP: if (sopt->sopt_name == IP_DROP_MEMBERSHIP) { error = sooptcopyin(sopt, &mreqs, sizeof(struct ip_mreq), sizeof(struct ip_mreq)); /* * Swap interface and sourceaddr arguments, * as ip_mreq and ip_mreq_source are laid * out differently. */ mreqs.imr_interface = mreqs.imr_sourceaddr; mreqs.imr_sourceaddr.s_addr = INADDR_ANY; } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) { error = sooptcopyin(sopt, &mreqs, sizeof(struct ip_mreq_source), sizeof(struct ip_mreq_source)); } if (error) { return error; } gsa->sin_family = AF_INET; gsa->sin_len = sizeof(struct sockaddr_in); gsa->sin_addr = mreqs.imr_multiaddr; if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) { ssa->sin_family = AF_INET; ssa->sin_len = sizeof(struct sockaddr_in); ssa->sin_addr = mreqs.imr_sourceaddr; } /* * Attempt to look up hinted ifp from interface address. * Fallthrough with null ifp iff lookup fails, to * preserve 4.4BSD mcast API idempotence. * XXX NOTE WELL: The RFC 3678 API is preferred because * using an IPv4 address as a key is racy. */ if (!in_nullhost(mreqs.imr_interface)) { ifp = ip_multicast_if(&mreqs.imr_interface, &ifindex); } IGMP_INET_PRINTF(mreqs.imr_interface, ("%s: imr_interface = %s, ifp = 0x%llx\n", __func__, _igmp_inet_buf, (uint64_t)VM_KERNEL_ADDRPERM(ifp))); break; case MCAST_LEAVE_GROUP: case MCAST_LEAVE_SOURCE_GROUP: if (sopt->sopt_name == MCAST_LEAVE_GROUP) { error = sooptcopyin(sopt, &gsr, sizeof(struct group_req), sizeof(struct group_req)); } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { error = sooptcopyin(sopt, &gsr, sizeof(struct group_source_req), sizeof(struct group_source_req)); } if (error) { return error; } if (gsa->sin_family != AF_INET || gsa->sin_len != sizeof(struct sockaddr_in)) { return EINVAL; } if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { if (ssa->sin_family != AF_INET || ssa->sin_len != sizeof(struct sockaddr_in)) { return EINVAL; } } ifnet_head_lock_shared(); if (gsr.gsr_interface == 0 || (u_int)if_index < gsr.gsr_interface) { ifnet_head_done(); return EADDRNOTAVAIL; } ifp = ifindex2ifnet[gsr.gsr_interface]; ifnet_head_done(); if (ifp == NULL) { return EADDRNOTAVAIL; } break; default: IGMP_PRINTF(("%s: unknown sopt_name %d\n", __func__, sopt->sopt_name)); return EOPNOTSUPP; } if (!IN_MULTICAST(ntohl(gsa->sin_addr.s_addr))) { return EINVAL; } /* * Find the membership in the membership array. */ imo = inp_findmoptions(inp); if (imo == NULL) { return ENOMEM; } IMO_LOCK(imo); idx = imo_match_group(imo, ifp, gsa); if (idx == (size_t)-1) { error = EADDRNOTAVAIL; goto out_locked; } inm = imo->imo_membership[idx]; if (inm == NULL) { error = EINVAL; goto out_locked; } imf = &imo->imo_mfilters[idx]; if (ssa->sin_family != AF_UNSPEC) { IGMP_PRINTF(("%s: opt=%d is_final=0\n", __func__, sopt->sopt_name)); is_final = 0; } /* * Begin state merge transaction at socket layer. */ /* * If we were instructed only to leave a given source, do so. * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships. */ if (is_final) { imf_leave(imf); } else { if (imf->imf_st[0] == MCAST_EXCLUDE) { error = EADDRNOTAVAIL; goto out_locked; } ims = imo_match_source(imo, idx, ssa); if (ims == NULL) { IGMP_INET_PRINTF(ssa->sin_addr, ("%s: source %s %spresent\n", __func__, _igmp_inet_buf, "not ")); error = EADDRNOTAVAIL; goto out_locked; } IGMP_PRINTF(("%s: %s source\n", __func__, "block")); error = imf_prune(imf, ssa); if (error) { IGMP_PRINTF(("%s: merge imf state failed\n", __func__)); goto out_locked; } } /* * Begin state merge transaction at IGMP layer. */ if (is_final) { /* * Give up the multicast address record to which * the membership points. Reference held in imo * will be released below. */ (void) in_leavegroup(inm, imf); } else { IGMP_PRINTF(("%s: merge inm state\n", __func__)); INM_LOCK(inm); error = inm_merge(inm, imf); if (error) { IGMP_PRINTF(("%s: failed to merge inm state\n", __func__)); INM_UNLOCK(inm); goto out_imf_rollback; } IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); error = igmp_change_state(inm, &itp); if (error) { IGMP_PRINTF(("%s: failed igmp downcall\n", __func__)); } INM_UNLOCK(inm); } out_imf_rollback: if (error) { imf_rollback(imf); } else { imf_commit(imf); } imf_reap(imf); if (is_final) { /* Remove the gap in the membership array and filter array. */ VERIFY(inm == imo->imo_membership[idx]); INM_REMREF(inm); for (++idx; idx < imo->imo_num_memberships; ++idx) { imo->imo_membership[idx - 1] = imo->imo_membership[idx]; imo->imo_mfilters[idx - 1] = imo->imo_mfilters[idx]; } imo->imo_num_memberships--; /* Re-initialize the now unused tail of the list */ imo->imo_membership[imo->imo_num_memberships] = NULL; imf_init(&imo->imo_mfilters[imo->imo_num_memberships], MCAST_UNDEFINED, MCAST_EXCLUDE); } out_locked: IMO_UNLOCK(imo); IMO_REMREF(imo); /* from inp_findmoptions() */ /* schedule timer now that we've dropped the lock(s) */ igmp_set_fast_timeout(&itp); return error; } /* * Select the interface for transmitting IPv4 multicast datagrams. * * Either an instance of struct in_addr or an instance of struct ip_mreqn * may be passed to this socket option. An address of INADDR_ANY or an * interface index of 0 is used to remove a previous selection. * When no interface is selected, one is chosen for every send. */ static int inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt) { struct in_addr addr; struct ip_mreqn mreqn; struct ifnet *ifp; struct ip_moptions *imo; int error = 0; unsigned int ifindex = 0; bzero(&addr, sizeof(addr)); if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) { /* * An interface index was specified using the * Linux-derived ip_mreqn structure. */ error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn), sizeof(struct ip_mreqn)); if (error) { return error; } ifnet_head_lock_shared(); if (mreqn.imr_ifindex < 0 || if_index < mreqn.imr_ifindex) { ifnet_head_done(); return EINVAL; } if (mreqn.imr_ifindex == 0) { ifp = NULL; } else { ifp = ifindex2ifnet[mreqn.imr_ifindex]; if (ifp == NULL) { ifnet_head_done(); return EADDRNOTAVAIL; } } ifnet_head_done(); } else { /* * An interface was specified by IPv4 address. * This is the traditional BSD usage. */ error = sooptcopyin(sopt, &addr, sizeof(struct in_addr), sizeof(struct in_addr)); if (error) { return error; } if (in_nullhost(addr)) { ifp = NULL; } else { ifp = ip_multicast_if(&addr, &ifindex); if (ifp == NULL) { IGMP_INET_PRINTF(addr, ("%s: can't find ifp for addr=%s\n", __func__, _igmp_inet_buf)); return EADDRNOTAVAIL; } } } /* Reject interfaces which do not support multicast. */ if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0) { return EOPNOTSUPP; } imo = inp_findmoptions(inp); if (imo == NULL) { return ENOMEM; } IMO_LOCK(imo); imo->imo_multicast_ifp = ifp; if (ifindex) { imo->imo_multicast_addr = addr; } else { imo->imo_multicast_addr.s_addr = INADDR_ANY; } IMO_UNLOCK(imo); IMO_REMREF(imo); /* from inp_findmoptions() */ return 0; } /* * Atomically set source filters on a socket for an IPv4 multicast group. */ static int inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt) { struct __msfilterreq64 msfr = {}, msfr64; struct __msfilterreq32 msfr32; struct sockaddr_in *gsa; struct ifnet *ifp; struct in_mfilter *imf; struct ip_moptions *imo; struct in_multi *inm; size_t idx; int error; uint64_t tmp_ptr; struct igmp_tparams itp; bzero(&itp, sizeof(itp)); int is_64bit_proc = IS_64BIT_PROCESS(current_proc()); if (is_64bit_proc) { error = sooptcopyin(sopt, &msfr64, sizeof(struct __msfilterreq64), sizeof(struct __msfilterreq64)); if (error) { return error; } /* we never use msfr.msfr_srcs; */ memcpy(&msfr, &msfr64, sizeof(msfr64)); } else { error = sooptcopyin(sopt, &msfr32, sizeof(struct __msfilterreq32), sizeof(struct __msfilterreq32)); if (error) { return error; } /* we never use msfr.msfr_srcs; */ memcpy(&msfr, &msfr32, sizeof(msfr32)); } if ((size_t) msfr.msfr_nsrcs > UINT32_MAX / sizeof(struct sockaddr_storage)) { msfr.msfr_nsrcs = UINT32_MAX / sizeof(struct sockaddr_storage); } if (msfr.msfr_nsrcs > in_mcast_maxsocksrc) { return ENOBUFS; } if ((msfr.msfr_fmode != MCAST_EXCLUDE && msfr.msfr_fmode != MCAST_INCLUDE)) { return EINVAL; } if (msfr.msfr_group.ss_family != AF_INET || msfr.msfr_group.ss_len != sizeof(struct sockaddr_in)) { return EINVAL; } gsa = (struct sockaddr_in *)&msfr.msfr_group; if (!IN_MULTICAST(ntohl(gsa->sin_addr.s_addr))) { return EINVAL; } gsa->sin_port = 0; /* ignore port */ ifnet_head_lock_shared(); if (msfr.msfr_ifindex == 0 || (u_int)if_index < msfr.msfr_ifindex) { ifnet_head_done(); return EADDRNOTAVAIL; } ifp = ifindex2ifnet[msfr.msfr_ifindex]; ifnet_head_done(); if (ifp == NULL) { return EADDRNOTAVAIL; } /* * Check if this socket is a member of this group. */ imo = inp_findmoptions(inp); if (imo == NULL) { return ENOMEM; } IMO_LOCK(imo); idx = imo_match_group(imo, ifp, gsa); if (idx == (size_t)-1 || imo->imo_mfilters == NULL) { error = EADDRNOTAVAIL; goto out_imo_locked; } inm = imo->imo_membership[idx]; imf = &imo->imo_mfilters[idx]; /* * Begin state merge transaction at socket layer. */ imf->imf_st[1] = (uint8_t)msfr.msfr_fmode; /* * Apply any new source filters, if present. * Make a copy of the user-space source vector so * that we may copy them with a single copyin. This * allows us to deal with page faults up-front. */ if (msfr.msfr_nsrcs > 0) { struct in_msource *lims; struct sockaddr_in *psin; struct sockaddr_storage *kss, *pkss; int i; if (is_64bit_proc) { tmp_ptr = msfr64.msfr_srcs; } else { tmp_ptr = CAST_USER_ADDR_T(msfr32.msfr_srcs); } IGMP_PRINTF(("%s: loading %lu source list entries\n", __func__, (unsigned long)msfr.msfr_nsrcs)); kss = kalloc_data((size_t)msfr.msfr_nsrcs * sizeof(*kss), Z_WAITOK); if (kss == NULL) { error = ENOMEM; goto out_imo_locked; } error = copyin(CAST_USER_ADDR_T(tmp_ptr), kss, (size_t) msfr.msfr_nsrcs * sizeof(*kss)); if (error) { kfree_data(kss, (size_t)msfr.msfr_nsrcs * sizeof(*kss)); goto out_imo_locked; } /* * Mark all source filters as UNDEFINED at t1. * Restore new group filter mode, as imf_leave() * will set it to INCLUDE. */ imf_leave(imf); imf->imf_st[1] = (uint8_t)msfr.msfr_fmode; /* * Update socket layer filters at t1, lazy-allocating * new entries. This saves a bunch of memory at the * cost of one RB_FIND() per source entry; duplicate * entries in the msfr_nsrcs vector are ignored. * If we encounter an error, rollback transaction. * * XXX This too could be replaced with a set-symmetric * difference like loop to avoid walking from root * every time, as the key space is common. */ for (i = 0, pkss = kss; (u_int)i < msfr.msfr_nsrcs; i++, pkss++) { psin = (struct sockaddr_in *)pkss; if (psin->sin_family != AF_INET) { error = EAFNOSUPPORT; break; } if (psin->sin_len != sizeof(struct sockaddr_in)) { error = EINVAL; break; } error = imf_get_source(imf, psin, &lims); if (error) { break; } lims->imsl_st[1] = imf->imf_st[1]; } kfree_data(kss, (size_t)msfr.msfr_nsrcs * sizeof(*kss)); } if (error) { goto out_imf_rollback; } /* * Begin state merge transaction at IGMP layer. */ INM_LOCK(inm); IGMP_PRINTF(("%s: merge inm state\n", __func__)); error = inm_merge(inm, imf); if (error) { IGMP_PRINTF(("%s: failed to merge inm state\n", __func__)); INM_UNLOCK(inm); goto out_imf_rollback; } IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); error = igmp_change_state(inm, &itp); INM_UNLOCK(inm); #ifdef IGMP_DEBUG if (error) { IGMP_PRINTF(("%s: failed igmp downcall\n", __func__)); } #endif out_imf_rollback: if (error) { imf_rollback(imf); } else { imf_commit(imf); } imf_reap(imf); out_imo_locked: IMO_UNLOCK(imo); IMO_REMREF(imo); /* from inp_findmoptions() */ /* schedule timer now that we've dropped the lock(s) */ igmp_set_fast_timeout(&itp); return error; } /* * Set the IP multicast options in response to user setsockopt(). * * Many of the socket options handled in this function duplicate the * functionality of socket options in the regular unicast API. However, * it is not possible to merge the duplicate code, because the idempotence * of the IPv4 multicast part of the BSD Sockets API must be preserved; * the effects of these options must be treated as separate and distinct. */ int inp_setmoptions(struct inpcb *inp, struct sockopt *sopt) { struct ip_moptions *imo; int error; unsigned int ifindex; struct ifnet *ifp; error = 0; /* * If socket is neither of type SOCK_RAW or SOCK_DGRAM, * or is a divert socket, reject it. */ if (SOCK_PROTO(inp->inp_socket) == IPPROTO_DIVERT || (SOCK_TYPE(inp->inp_socket) != SOCK_RAW && SOCK_TYPE(inp->inp_socket) != SOCK_DGRAM)) { return EOPNOTSUPP; } switch (sopt->sopt_name) { case IP_MULTICAST_IF: error = inp_set_multicast_if(inp, sopt); break; case IP_MULTICAST_IFINDEX: /* * Select the interface for outgoing multicast packets. */ error = sooptcopyin(sopt, &ifindex, sizeof(ifindex), sizeof(ifindex)); if (error) { break; } imo = inp_findmoptions(inp); if (imo == NULL) { error = ENOMEM; break; } /* * Index 0 is used to remove a previous selection. * When no interface is selected, a default one is * chosen every time a multicast packet is sent. */ if (ifindex == 0) { IMO_LOCK(imo); imo->imo_multicast_ifp = NULL; IMO_UNLOCK(imo); IMO_REMREF(imo); /* from inp_findmoptions() */ break; } ifnet_head_lock_shared(); /* Don't need to check is ifindex is < 0 since it's unsigned */ if ((unsigned int)if_index < ifindex) { ifnet_head_done(); IMO_REMREF(imo); /* from inp_findmoptions() */ error = ENXIO; /* per IPV6_MULTICAST_IF */ break; } ifp = ifindex2ifnet[ifindex]; ifnet_head_done(); /* If it's detached or isn't a multicast interface, bail out */ if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) { IMO_REMREF(imo); /* from inp_findmoptions() */ error = EADDRNOTAVAIL; break; } IMO_LOCK(imo); imo->imo_multicast_ifp = ifp; /* * Clear out any remnants of past IP_MULTICAST_IF. The addr * isn't really used anywhere in the kernel; we could have * iterated thru the addresses of the interface and pick one * here, but that is redundant since ip_getmoptions() already * takes care of that for INADDR_ANY. */ imo->imo_multicast_addr.s_addr = INADDR_ANY; IMO_UNLOCK(imo); IMO_REMREF(imo); /* from inp_findmoptions() */ break; case IP_MULTICAST_TTL: { u_char ttl; /* * Set the IP time-to-live for outgoing multicast packets. * The original multicast API required a char argument, * which is inconsistent with the rest of the socket API. * We allow either a char or an int. */ if (sopt->sopt_valsize == sizeof(u_char)) { error = sooptcopyin(sopt, &ttl, sizeof(u_char), sizeof(u_char)); if (error) { break; } } else { u_int ittl; error = sooptcopyin(sopt, &ittl, sizeof(u_int), sizeof(u_int)); if (error) { break; } if (ittl > 255) { error = EINVAL; break; } ttl = (u_char)ittl; } imo = inp_findmoptions(inp); if (imo == NULL) { error = ENOMEM; break; } IMO_LOCK(imo); imo->imo_multicast_ttl = ttl; IMO_UNLOCK(imo); IMO_REMREF(imo); /* from inp_findmoptions() */ break; } case IP_MULTICAST_LOOP: { u_char loop; /* * Set the loopback flag for outgoing multicast packets. * Must be zero or one. The original multicast API required a * char argument, which is inconsistent with the rest * of the socket API. We allow either a char or an int. */ if (sopt->sopt_valsize == sizeof(u_char)) { error = sooptcopyin(sopt, &loop, sizeof(u_char), sizeof(u_char)); if (error) { break; } } else { u_int iloop; error = sooptcopyin(sopt, &iloop, sizeof(u_int), sizeof(u_int)); if (error) { break; } loop = (u_char)iloop; } imo = inp_findmoptions(inp); if (imo == NULL) { error = ENOMEM; break; } IMO_LOCK(imo); imo->imo_multicast_loop = !!loop; IMO_UNLOCK(imo); IMO_REMREF(imo); /* from inp_findmoptions() */ break; } case IP_ADD_MEMBERSHIP: case IP_ADD_SOURCE_MEMBERSHIP: case MCAST_JOIN_GROUP: case MCAST_JOIN_SOURCE_GROUP: error = inp_join_group(inp, sopt); break; case IP_DROP_MEMBERSHIP: case IP_DROP_SOURCE_MEMBERSHIP: case MCAST_LEAVE_GROUP: case MCAST_LEAVE_SOURCE_GROUP: error = inp_leave_group(inp, sopt); break; case IP_BLOCK_SOURCE: case IP_UNBLOCK_SOURCE: case MCAST_BLOCK_SOURCE: case MCAST_UNBLOCK_SOURCE: error = inp_block_unblock_source(inp, sopt); break; case IP_MSFILTER: error = inp_set_source_filters(inp, sopt); break; default: error = EOPNOTSUPP; break; } return error; } /* * Expose IGMP's multicast filter mode and source list(s) to userland, * keyed by (ifindex, group). * The filter mode is written out as a uint32_t, followed by * 0..n of struct in_addr. * For use by ifmcstat(8). */ static int sysctl_ip_mcast_filters SYSCTL_HANDLER_ARGS { #pragma unused(oidp) struct in_addr src = {}, group; struct ifnet *ifp; struct in_multi *inm; struct in_multistep step; struct ip_msource *ims; int *name; int retval = 0; u_int namelen; uint32_t fmode, ifindex; name = (int *)arg1; namelen = (u_int)arg2; if (req->newptr != USER_ADDR_NULL) { return EPERM; } if (namelen != 2) { return EINVAL; } ifindex = name[0]; ifnet_head_lock_shared(); if (ifindex <= 0 || ifindex > (u_int)if_index) { IGMP_PRINTF(("%s: ifindex %u out of range\n", __func__, ifindex)); ifnet_head_done(); return ENOENT; } group.s_addr = name[1]; if (!IN_MULTICAST(ntohl(group.s_addr))) { IGMP_INET_PRINTF(group, ("%s: group %s is not multicast\n", __func__, _igmp_inet_buf)); ifnet_head_done(); return EINVAL; } ifp = ifindex2ifnet[ifindex]; ifnet_head_done(); if (ifp == NULL) { IGMP_PRINTF(("%s: no ifp for ifindex %u\n", __func__, ifindex)); return ENOENT; } in_multihead_lock_shared(); IN_FIRST_MULTI(step, inm); while (inm != NULL) { INM_LOCK(inm); if (inm->inm_ifp != ifp) { goto next; } if (!in_hosteq(inm->inm_addr, group)) { goto next; } fmode = inm->inm_st[1].iss_fmode; retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t)); if (retval != 0) { INM_UNLOCK(inm); break; /* abort */ } RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { #ifdef IGMP_DEBUG struct in_addr ina; ina.s_addr = htonl(ims->ims_haddr); IGMP_INET_PRINTF(ina, ("%s: visit node %s\n", __func__, _igmp_inet_buf)); #endif /* * Only copy-out sources which are in-mode. */ if (fmode != ims_get_mode(inm, ims, 1)) { IGMP_PRINTF(("%s: skip non-in-mode\n", __func__)); continue; /* process next source */ } src.s_addr = htonl(ims->ims_haddr); retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr)); if (retval != 0) { break; /* process next inm */ } } next: INM_UNLOCK(inm); IN_NEXT_MULTI(step, inm); } in_multihead_lock_done(); return retval; } /* * XXX * The whole multicast option thing needs to be re-thought. * Several of these options are equally applicable to non-multicast * transmission, and one (IP_MULTICAST_TTL) totally duplicates a * standard option (IP_TTL). */ /* * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. */ static struct ifnet * ip_multicast_if(struct in_addr *a, unsigned int *ifindexp) { unsigned int ifindex; struct ifnet *ifp; if (ifindexp != NULL) { *ifindexp = 0; } if (ntohl(a->s_addr) >> 24 == 0) { ifindex = ntohl(a->s_addr) & 0xffffff; ifnet_head_lock_shared(); /* Don't need to check is ifindex is < 0 since it's unsigned */ if ((unsigned int)if_index < ifindex) { ifnet_head_done(); return NULL; } ifp = ifindex2ifnet[ifindex]; ifnet_head_done(); if (ifp != NULL && ifindexp != NULL) { *ifindexp = ifindex; } } else { INADDR_TO_IFP(*a, ifp); } return ifp; } void in_multi_init(void) { TAILQ_INIT(&inm_trash_head); vm_size_t inm_size = (inm_debug == 0) ? sizeof(struct in_multi) : sizeof(struct in_multi_dbg); inm_zone = zone_create(INM_ZONE_NAME, inm_size, ZC_ZFREE_CLEARMEM); } static struct in_multi * in_multi_alloc(zalloc_flags_t how) { struct in_multi *inm; inm = zalloc_flags(inm_zone, how | Z_ZERO); if (inm != NULL) { lck_mtx_init(&inm->inm_lock, &in_multihead_lock_grp, &in_multihead_lock_attr); inm->inm_debug |= IFD_ALLOC; if (inm_debug != 0) { inm->inm_debug |= IFD_DEBUG; inm->inm_trace = inm_trace; } } return inm; } static void in_multi_free(struct in_multi *inm) { INM_LOCK(inm); if (inm->inm_debug & IFD_ATTACHED) { panic("%s: attached inm=%p is being freed", __func__, inm); /* NOTREACHED */ } else if (inm->inm_ifma != NULL) { panic("%s: ifma not NULL for inm=%p", __func__, inm); /* NOTREACHED */ } else if (!(inm->inm_debug & IFD_ALLOC)) { panic("%s: inm %p cannot be freed", __func__, inm); /* NOTREACHED */ } else if (inm->inm_refcount != 0) { panic("%s: non-zero refcount inm=%p", __func__, inm); /* NOTREACHED */ } else if (inm->inm_reqcnt != 0) { panic("%s: non-zero reqcnt inm=%p", __func__, inm); /* NOTREACHED */ } /* Free any pending IGMPv3 state-change records */ IF_DRAIN(&inm->inm_scq); inm->inm_debug &= ~IFD_ALLOC; if ((inm->inm_debug & (IFD_DEBUG | IFD_TRASHED)) == (IFD_DEBUG | IFD_TRASHED)) { lck_mtx_lock(&inm_trash_lock); TAILQ_REMOVE(&inm_trash_head, (struct in_multi_dbg *)inm, inm_trash_link); lck_mtx_unlock(&inm_trash_lock); inm->inm_debug &= ~IFD_TRASHED; } INM_UNLOCK(inm); lck_mtx_destroy(&inm->inm_lock, &in_multihead_lock_grp); zfree(inm_zone, inm); } static void in_multi_attach(struct in_multi *inm) { in_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE); INM_LOCK_ASSERT_HELD(inm); if (inm->inm_debug & IFD_ATTACHED) { panic("%s: Attempt to attach an already attached inm=%p", __func__, inm); /* NOTREACHED */ } else if (inm->inm_debug & IFD_TRASHED) { panic("%s: Attempt to reattach a detached inm=%p", __func__, inm); /* NOTREACHED */ } inm->inm_reqcnt++; VERIFY(inm->inm_reqcnt == 1); INM_ADDREF_LOCKED(inm); inm->inm_debug |= IFD_ATTACHED; /* * Reattach case: If debugging is enabled, take it * out of the trash list and clear IFD_TRASHED. */ if ((inm->inm_debug & (IFD_DEBUG | IFD_TRASHED)) == (IFD_DEBUG | IFD_TRASHED)) { /* Become a regular mutex, just in case */ INM_CONVERT_LOCK(inm); lck_mtx_lock(&inm_trash_lock); TAILQ_REMOVE(&inm_trash_head, (struct in_multi_dbg *)inm, inm_trash_link); lck_mtx_unlock(&inm_trash_lock); inm->inm_debug &= ~IFD_TRASHED; } LIST_INSERT_HEAD(&in_multihead, inm, inm_link); } int in_multi_detach(struct in_multi *inm) { in_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE); INM_LOCK_ASSERT_HELD(inm); if (inm->inm_reqcnt == 0) { panic("%s: inm=%p negative reqcnt", __func__, inm); /* NOTREACHED */ } --inm->inm_reqcnt; if (inm->inm_reqcnt > 0) { return 0; } if (!(inm->inm_debug & IFD_ATTACHED)) { panic("%s: Attempt to detach an unattached record inm=%p", __func__, inm); /* NOTREACHED */ } else if (inm->inm_debug & IFD_TRASHED) { panic("%s: inm %p is already in trash list", __func__, inm); /* NOTREACHED */ } /* * NOTE: Caller calls IFMA_REMREF */ inm->inm_debug &= ~IFD_ATTACHED; LIST_REMOVE(inm, inm_link); if (inm->inm_debug & IFD_DEBUG) { /* Become a regular mutex, just in case */ INM_CONVERT_LOCK(inm); lck_mtx_lock(&inm_trash_lock); TAILQ_INSERT_TAIL(&inm_trash_head, (struct in_multi_dbg *)inm, inm_trash_link); lck_mtx_unlock(&inm_trash_lock); inm->inm_debug |= IFD_TRASHED; } return 1; } void inm_addref(struct in_multi *inm, int locked) { if (!locked) { INM_LOCK_SPIN(inm); } else { INM_LOCK_ASSERT_HELD(inm); } if (++inm->inm_refcount == 0) { panic("%s: inm=%p wraparound refcnt", __func__, inm); /* NOTREACHED */ } else if (inm->inm_trace != NULL) { (*inm->inm_trace)(inm, TRUE); } if (!locked) { INM_UNLOCK(inm); } } void inm_remref(struct in_multi *inm, int locked) { struct ifmultiaddr *ifma; struct igmp_ifinfo *igi; if (!locked) { INM_LOCK_SPIN(inm); } else { INM_LOCK_ASSERT_HELD(inm); } if (inm->inm_refcount == 0 || (inm->inm_refcount == 1 && locked)) { panic("%s: inm=%p negative/missing refcnt", __func__, inm); /* NOTREACHED */ } else if (inm->inm_trace != NULL) { (*inm->inm_trace)(inm, FALSE); } --inm->inm_refcount; if (inm->inm_refcount > 0) { if (!locked) { INM_UNLOCK(inm); } return; } /* * Synchronization with in_getmulti(). In the event the inm has been * detached, the underlying ifma would still be in the if_multiaddrs * list, and thus can be looked up via if_addmulti(). At that point, * the only way to find this inm is via ifma_protospec. To avoid * race conditions between the last inm_remref() of that inm and its * use via ifma_protospec, in_multihead lock is used for serialization. * In order to avoid violating the lock order, we must drop inm_lock * before acquiring in_multihead lock. To prevent the inm from being * freed prematurely, we hold an extra reference. */ ++inm->inm_refcount; INM_UNLOCK(inm); in_multihead_lock_shared(); INM_LOCK_SPIN(inm); --inm->inm_refcount; if (inm->inm_refcount > 0) { /* We've lost the race, so abort since inm is still in use */ INM_UNLOCK(inm); in_multihead_lock_done(); /* If it was locked, return it as such */ if (locked) { INM_LOCK(inm); } return; } inm_purge(inm); ifma = inm->inm_ifma; inm->inm_ifma = NULL; inm->inm_ifp = NULL; igi = inm->inm_igi; inm->inm_igi = NULL; INM_UNLOCK(inm); IFMA_LOCK_SPIN(ifma); ifma->ifma_protospec = NULL; IFMA_UNLOCK(ifma); in_multihead_lock_done(); in_multi_free(inm); if_delmulti_ifma(ifma); /* Release reference held to the underlying ifmultiaddr */ IFMA_REMREF(ifma); if (igi != NULL) { IGI_REMREF(igi); } } static void inm_trace(struct in_multi *inm, int refhold) { struct in_multi_dbg *inm_dbg = (struct in_multi_dbg *)inm; ctrace_t *tr; u_int32_t idx; u_int16_t *cnt; if (!(inm->inm_debug & IFD_DEBUG)) { panic("%s: inm %p has no debug structure", __func__, inm); /* NOTREACHED */ } if (refhold) { cnt = &inm_dbg->inm_refhold_cnt; tr = inm_dbg->inm_refhold; } else { cnt = &inm_dbg->inm_refrele_cnt; tr = inm_dbg->inm_refrele; } idx = atomic_add_16_ov(cnt, 1) % INM_TRACE_HIST_SIZE; ctrace_record(&tr[idx]); } void in_multihead_lock_exclusive(void) { lck_rw_lock_exclusive(&in_multihead_lock); } void in_multihead_lock_shared(void) { lck_rw_lock_shared(&in_multihead_lock); } void in_multihead_lock_assert(int what) { #if !MACH_ASSERT #pragma unused(what) #endif LCK_RW_ASSERT(&in_multihead_lock, what); } void in_multihead_lock_done(void) { lck_rw_done(&in_multihead_lock); } static struct ip_msource * ipms_alloc(zalloc_flags_t how) { return zalloc_flags(ipms_zone, how | Z_ZERO); } static void ipms_free(struct ip_msource *ims) { zfree(ipms_zone, ims); } static struct in_msource * inms_alloc(zalloc_flags_t how) { return zalloc_flags(inms_zone, how | Z_ZERO); } static void inms_free(struct in_msource *inms) { zfree(inms_zone, inms); } #ifdef IGMP_DEBUG static const char *inm_modestrs[] = { "un\n", "in", "ex" }; static const char * inm_mode_str(const int mode) { if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE) { return inm_modestrs[mode]; } return "??"; } static const char *inm_statestrs[] = { "not-member\n", "silent\n", "reporting\n", "idle\n", "lazy\n", "sleeping\n", "awakening\n", "query-pending\n", "sg-query-pending\n", "leaving" }; static const char * inm_state_str(const int state) { if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER) { return inm_statestrs[state]; } return "??"; } /* * Dump an in_multi structure to the console. */ void inm_print(const struct in_multi *inm) { int t; char buf[MAX_IPv4_STR_LEN]; INM_LOCK_ASSERT_HELD(__DECONST(struct in_multi *, inm)); if (igmp_debug == 0) { return; } inet_ntop(AF_INET, &inm->inm_addr, buf, sizeof(buf)); printf("%s: --- begin inm 0x%llx ---\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(inm)); printf("addr %s ifp 0x%llx(%s) ifma 0x%llx\n", buf, (uint64_t)VM_KERNEL_ADDRPERM(inm->inm_ifp), if_name(inm->inm_ifp), (uint64_t)VM_KERNEL_ADDRPERM(inm->inm_ifma)); printf("timer %u state %s refcount %u scq.len %u\n", inm->inm_timer, inm_state_str(inm->inm_state), inm->inm_refcount, inm->inm_scq.ifq_len); printf("igi 0x%llx nsrc %lu sctimer %u scrv %u\n", (uint64_t)VM_KERNEL_ADDRPERM(inm->inm_igi), inm->inm_nsrc, inm->inm_sctimer, inm->inm_scrv); for (t = 0; t < 2; t++) { printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t, inm_mode_str(inm->inm_st[t].iss_fmode), inm->inm_st[t].iss_asm, inm->inm_st[t].iss_ex, inm->inm_st[t].iss_in, inm->inm_st[t].iss_rec); } printf("%s: --- end inm 0x%llx ---\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(inm)); } #else void inm_print(__unused const struct in_multi *inm) { } #endif |