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 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 | /* * Copyright (c) 1998-2015 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) 1995 NeXT Computer, Inc. All Rights Reserved */ /* * Copyright (c) 1982, 1986, 1988, 1990, 1993 * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. * * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94 */ /* * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce * support for mandatory and extensible security protections. This notice * is included in support of clause 2.2 (b) of the Apple Public License, * Version 2.0. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/filedesc.h> #include <sys/proc.h> #include <sys/proc_internal.h> #include <sys/kauth.h> #include <sys/file_internal.h> #include <sys/fcntl.h> #include <sys/malloc.h> #include <sys/mbuf.h> #include <sys/domain.h> #include <sys/kernel.h> #include <sys/event.h> #include <sys/poll.h> #include <sys/protosw.h> #include <sys/socket.h> #include <sys/socketvar.h> #include <sys/resourcevar.h> #include <sys/signalvar.h> #include <sys/sysctl.h> #include <sys/syslog.h> #include <sys/uio.h> #include <sys/uio_internal.h> #include <sys/ev.h> #include <sys/kdebug.h> #include <sys/un.h> #include <sys/user.h> #include <sys/priv.h> #include <sys/kern_event.h> #include <net/route.h> #include <net/init.h> #include <net/ntstat.h> #include <net/content_filter.h> #include <netinet/in.h> #include <netinet/in_pcb.h> #include <netinet/ip6.h> #include <netinet6/ip6_var.h> #include <netinet/flow_divert.h> #include <kern/zalloc.h> #include <kern/locks.h> #include <machine/limits.h> #include <libkern/OSAtomic.h> #include <pexpert/pexpert.h> #include <kern/assert.h> #include <kern/task.h> #include <sys/kpi_mbuf.h> #include <sys/mcache.h> #include <sys/unpcb.h> #if CONFIG_MACF #include <security/mac.h> #include <security/mac_framework.h> #endif /* MAC */ #if MULTIPATH #include <netinet/mp_pcb.h> #include <netinet/mptcp_var.h> #endif /* MULTIPATH */ #define ROUNDUP(a, b) (((a) + ((b) - 1)) & (~((b) - 1))) #if DEBUG || DEVELOPMENT #define DEBUG_KERNEL_ADDRPERM(_v) (_v) #else #define DEBUG_KERNEL_ADDRPERM(_v) VM_KERNEL_ADDRPERM(_v) #endif /* TODO: this should be in a header file somewhere */ extern char *proc_name_address(void *p); static u_int32_t so_cache_hw; /* High water mark for socache */ static u_int32_t so_cache_timeouts; /* number of timeouts */ static u_int32_t so_cache_max_freed; /* max freed per timeout */ static u_int32_t cached_sock_count = 0; STAILQ_HEAD(, socket) so_cache_head; int max_cached_sock_count = MAX_CACHED_SOCKETS; static u_int32_t so_cache_time; static int socketinit_done; static struct zone *so_cache_zone; static lck_grp_t *so_cache_mtx_grp; static lck_attr_t *so_cache_mtx_attr; static lck_grp_attr_t *so_cache_mtx_grp_attr; static lck_mtx_t *so_cache_mtx; #include <machine/limits.h> static void filt_sordetach(struct knote *kn); static int filt_soread(struct knote *kn, long hint); static void filt_sowdetach(struct knote *kn); static int filt_sowrite(struct knote *kn, long hint); static void filt_sockdetach(struct knote *kn); static int filt_sockev(struct knote *kn, long hint); static void filt_socktouch(struct knote *kn, struct kevent_internal_s *kev, long type); static int sooptcopyin_timeval(struct sockopt *, struct timeval *); static int sooptcopyout_timeval(struct sockopt *, const struct timeval *); static struct filterops soread_filtops = { .f_isfd = 1, .f_detach = filt_sordetach, .f_event = filt_soread, }; static struct filterops sowrite_filtops = { .f_isfd = 1, .f_detach = filt_sowdetach, .f_event = filt_sowrite, }; static struct filterops sock_filtops = { .f_isfd = 1, .f_detach = filt_sockdetach, .f_event = filt_sockev, .f_touch = filt_socktouch, }; SYSCTL_DECL(_kern_ipc); #define EVEN_MORE_LOCKING_DEBUG 0 int socket_debug = 0; SYSCTL_INT(_kern_ipc, OID_AUTO, socket_debug, CTLFLAG_RW | CTLFLAG_LOCKED, &socket_debug, 0, ""); static int socket_zone = M_SOCKET; so_gen_t so_gencnt; /* generation count for sockets */ MALLOC_DEFINE(M_SONAME, "soname", "socket name"); MALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0) #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2) #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1) #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3) #define DBG_FNC_SOSEND NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 1) #define DBG_FNC_SOSEND_LIST NETDBG_CODE(DBG_NETSOCK, (4 << 8) | 3) #define DBG_FNC_SORECEIVE NETDBG_CODE(DBG_NETSOCK, (8 << 8)) #define DBG_FNC_SORECEIVE_LIST NETDBG_CODE(DBG_NETSOCK, (8 << 8) | 3) #define DBG_FNC_SOSHUTDOWN NETDBG_CODE(DBG_NETSOCK, (9 << 8)) #define MAX_SOOPTGETM_SIZE (128 * MCLBYTES) int somaxconn = SOMAXCONN; SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW | CTLFLAG_LOCKED, &somaxconn, 0, ""); /* Should we get a maximum also ??? */ static int sosendmaxchain = 65536; static int sosendminchain = 16384; static int sorecvmincopy = 16384; SYSCTL_INT(_kern_ipc, OID_AUTO, sosendminchain, CTLFLAG_RW | CTLFLAG_LOCKED, &sosendminchain, 0, ""); SYSCTL_INT(_kern_ipc, OID_AUTO, sorecvmincopy, CTLFLAG_RW | CTLFLAG_LOCKED, &sorecvmincopy, 0, ""); /* * Set to enable jumbo clusters (if available) for large writes when * the socket is marked with SOF_MULTIPAGES; see below. */ int sosendjcl = 1; SYSCTL_INT(_kern_ipc, OID_AUTO, sosendjcl, CTLFLAG_RW | CTLFLAG_LOCKED, &sosendjcl, 0, ""); /* * Set this to ignore SOF_MULTIPAGES and use jumbo clusters for large * writes on the socket for all protocols on any network interfaces, * depending upon sosendjcl above. Be extra careful when setting this * to 1, because sending down packets that cross physical pages down to * broken drivers (those that falsely assume that the physical pages * are contiguous) might lead to system panics or silent data corruption. * When set to 0, the system will respect SOF_MULTIPAGES, which is set * only for TCP sockets whose outgoing interface is IFNET_MULTIPAGES * capable. Set this to 1 only for testing/debugging purposes. */ int sosendjcl_ignore_capab = 0; SYSCTL_INT(_kern_ipc, OID_AUTO, sosendjcl_ignore_capab, CTLFLAG_RW | CTLFLAG_LOCKED, &sosendjcl_ignore_capab, 0, ""); /* * Set this to ignore SOF1_IF_2KCL and use big clusters for large * writes on the socket for all protocols on any network interfaces. * Be extra careful when setting this to 1, because sending down packets with * clusters larger that 2 KB might lead to system panics or data corruption. * When set to 0, the system will respect SOF1_IF_2KCL, which is set * on the outgoing interface * Set this to 1 for testing/debugging purposes only. */ int sosendbigcl_ignore_capab = 0; SYSCTL_INT(_kern_ipc, OID_AUTO, sosendbigcl_ignore_capab, CTLFLAG_RW | CTLFLAG_LOCKED, &sosendbigcl_ignore_capab, 0, ""); int sodefunctlog = 0; SYSCTL_INT(_kern_ipc, OID_AUTO, sodefunctlog, CTLFLAG_RW | CTLFLAG_LOCKED, &sodefunctlog, 0, ""); int sothrottlelog = 0; SYSCTL_INT(_kern_ipc, OID_AUTO, sothrottlelog, CTLFLAG_RW | CTLFLAG_LOCKED, &sothrottlelog, 0, ""); int sorestrictrecv = 1; SYSCTL_INT(_kern_ipc, OID_AUTO, sorestrictrecv, CTLFLAG_RW | CTLFLAG_LOCKED, &sorestrictrecv, 0, "Enable inbound interface restrictions"); int sorestrictsend = 1; SYSCTL_INT(_kern_ipc, OID_AUTO, sorestrictsend, CTLFLAG_RW | CTLFLAG_LOCKED, &sorestrictsend, 0, "Enable outbound interface restrictions"); int soreserveheadroom = 1; SYSCTL_INT(_kern_ipc, OID_AUTO, soreserveheadroom, CTLFLAG_RW | CTLFLAG_LOCKED, &soreserveheadroom, 0, "To allocate contiguous datagram buffers"); extern struct inpcbinfo tcbinfo; /* TODO: these should be in header file */ extern int get_inpcb_str_size(void); extern int get_tcp_str_size(void); static unsigned int sl_zone_size; /* size of sockaddr_list */ static struct zone *sl_zone; /* zone for sockaddr_list */ static unsigned int se_zone_size; /* size of sockaddr_entry */ static struct zone *se_zone; /* zone for sockaddr_entry */ vm_size_t so_cache_zone_element_size; static int sodelayed_copy(struct socket *, struct uio *, struct mbuf **, user_ssize_t *); static void cached_sock_alloc(struct socket **, int); static void cached_sock_free(struct socket *); /* * Maximum of extended background idle sockets per process * Set to zero to disable further setting of the option */ #define SO_IDLE_BK_IDLE_MAX_PER_PROC 1 #define SO_IDLE_BK_IDLE_TIME 600 #define SO_IDLE_BK_IDLE_RCV_HIWAT 131072 struct soextbkidlestat soextbkidlestat; SYSCTL_UINT(_kern_ipc, OID_AUTO, maxextbkidleperproc, CTLFLAG_RW | CTLFLAG_LOCKED, &soextbkidlestat.so_xbkidle_maxperproc, 0, "Maximum of extended background idle sockets per process"); SYSCTL_UINT(_kern_ipc, OID_AUTO, extbkidletime, CTLFLAG_RW | CTLFLAG_LOCKED, &soextbkidlestat.so_xbkidle_time, 0, "Time in seconds to keep extended background idle sockets"); SYSCTL_UINT(_kern_ipc, OID_AUTO, extbkidlercvhiwat, CTLFLAG_RW | CTLFLAG_LOCKED, &soextbkidlestat.so_xbkidle_rcvhiwat, 0, "High water mark for extended background idle sockets"); SYSCTL_STRUCT(_kern_ipc, OID_AUTO, extbkidlestat, CTLFLAG_RD | CTLFLAG_LOCKED, &soextbkidlestat, soextbkidlestat, ""); int so_set_extended_bk_idle(struct socket *, int); /* * SOTCDB_NO_DSCP is set by default, to prevent the networking stack from * setting the DSCP code on the packet based on the service class; see * <rdar://problem/11277343> for details. */ __private_extern__ u_int32_t sotcdb = SOTCDB_NO_DSCP; SYSCTL_INT(_kern_ipc, OID_AUTO, sotcdb, CTLFLAG_RW | CTLFLAG_LOCKED, &sotcdb, 0, ""); void socketinit(void) { _CASSERT(sizeof(so_gencnt) == sizeof(uint64_t)); VERIFY(IS_P2ALIGNED(&so_gencnt, sizeof(uint32_t))); #ifdef __LP64__ _CASSERT(sizeof(struct sa_endpoints) == sizeof(struct user64_sa_endpoints)); _CASSERT(offsetof(struct sa_endpoints, sae_srcif) == offsetof(struct user64_sa_endpoints, sae_srcif)); _CASSERT(offsetof(struct sa_endpoints, sae_srcaddr) == offsetof(struct user64_sa_endpoints, sae_srcaddr)); _CASSERT(offsetof(struct sa_endpoints, sae_srcaddrlen) == offsetof(struct user64_sa_endpoints, sae_srcaddrlen)); _CASSERT(offsetof(struct sa_endpoints, sae_dstaddr) == offsetof(struct user64_sa_endpoints, sae_dstaddr)); _CASSERT(offsetof(struct sa_endpoints, sae_dstaddrlen) == offsetof(struct user64_sa_endpoints, sae_dstaddrlen)); #else _CASSERT(sizeof(struct sa_endpoints) == sizeof(struct user32_sa_endpoints)); _CASSERT(offsetof(struct sa_endpoints, sae_srcif) == offsetof(struct user32_sa_endpoints, sae_srcif)); _CASSERT(offsetof(struct sa_endpoints, sae_srcaddr) == offsetof(struct user32_sa_endpoints, sae_srcaddr)); _CASSERT(offsetof(struct sa_endpoints, sae_srcaddrlen) == offsetof(struct user32_sa_endpoints, sae_srcaddrlen)); _CASSERT(offsetof(struct sa_endpoints, sae_dstaddr) == offsetof(struct user32_sa_endpoints, sae_dstaddr)); _CASSERT(offsetof(struct sa_endpoints, sae_dstaddrlen) == offsetof(struct user32_sa_endpoints, sae_dstaddrlen)); #endif if (socketinit_done) { printf("socketinit: already called...\n"); return; } socketinit_done = 1; PE_parse_boot_argn("socket_debug", &socket_debug, sizeof (socket_debug)); /* * allocate lock group attribute and group for socket cache mutex */ so_cache_mtx_grp_attr = lck_grp_attr_alloc_init(); so_cache_mtx_grp = lck_grp_alloc_init("so_cache", so_cache_mtx_grp_attr); /* * allocate the lock attribute for socket cache mutex */ so_cache_mtx_attr = lck_attr_alloc_init(); /* cached sockets mutex */ so_cache_mtx = lck_mtx_alloc_init(so_cache_mtx_grp, so_cache_mtx_attr); if (so_cache_mtx == NULL) { panic("%s: unable to allocate so_cache_mtx\n", __func__); /* NOTREACHED */ } STAILQ_INIT(&so_cache_head); so_cache_zone_element_size = (vm_size_t)(sizeof (struct socket) + 4 + get_inpcb_str_size() + 4 + get_tcp_str_size()); so_cache_zone = zinit(so_cache_zone_element_size, (120000 * so_cache_zone_element_size), 8192, "socache zone"); zone_change(so_cache_zone, Z_CALLERACCT, FALSE); zone_change(so_cache_zone, Z_NOENCRYPT, TRUE); sl_zone_size = sizeof (struct sockaddr_list); if ((sl_zone = zinit(sl_zone_size, 1024 * sl_zone_size, 1024, "sockaddr_list")) == NULL) { panic("%s: unable to allocate sockaddr_list zone\n", __func__); /* NOTREACHED */ } zone_change(sl_zone, Z_CALLERACCT, FALSE); zone_change(sl_zone, Z_EXPAND, TRUE); se_zone_size = sizeof (struct sockaddr_entry); if ((se_zone = zinit(se_zone_size, 1024 * se_zone_size, 1024, "sockaddr_entry")) == NULL) { panic("%s: unable to allocate sockaddr_entry zone\n", __func__); /* NOTREACHED */ } zone_change(se_zone, Z_CALLERACCT, FALSE); zone_change(se_zone, Z_EXPAND, TRUE); bzero(&soextbkidlestat, sizeof(struct soextbkidlestat)); soextbkidlestat.so_xbkidle_maxperproc = SO_IDLE_BK_IDLE_MAX_PER_PROC; soextbkidlestat.so_xbkidle_time = SO_IDLE_BK_IDLE_TIME; soextbkidlestat.so_xbkidle_rcvhiwat = SO_IDLE_BK_IDLE_RCV_HIWAT; in_pcbinit(); sflt_init(); socket_tclass_init(); #if MULTIPATH mp_pcbinit(); #endif /* MULTIPATH */ } static void cached_sock_alloc(struct socket **so, int waitok) { caddr_t temp; uintptr_t offset; lck_mtx_lock(so_cache_mtx); if (!STAILQ_EMPTY(&so_cache_head)) { VERIFY(cached_sock_count > 0); *so = STAILQ_FIRST(&so_cache_head); STAILQ_REMOVE_HEAD(&so_cache_head, so_cache_ent); STAILQ_NEXT((*so), so_cache_ent) = NULL; cached_sock_count--; lck_mtx_unlock(so_cache_mtx); temp = (*so)->so_saved_pcb; bzero((caddr_t)*so, sizeof (struct socket)); (*so)->so_saved_pcb = temp; } else { lck_mtx_unlock(so_cache_mtx); if (waitok) *so = (struct socket *)zalloc(so_cache_zone); else *so = (struct socket *)zalloc_noblock(so_cache_zone); if (*so == NULL) return; bzero((caddr_t)*so, sizeof (struct socket)); /* * Define offsets for extra structures into our * single block of memory. Align extra structures * on longword boundaries. */ offset = (uintptr_t)*so; offset += sizeof (struct socket); offset = ALIGN(offset); (*so)->so_saved_pcb = (caddr_t)offset; offset += get_inpcb_str_size(); offset = ALIGN(offset); ((struct inpcb *)(void *)(*so)->so_saved_pcb)->inp_saved_ppcb = (caddr_t)offset; } OSBitOrAtomic(SOF1_CACHED_IN_SOCK_LAYER, &(*so)->so_flags1); } static void cached_sock_free(struct socket *so) { lck_mtx_lock(so_cache_mtx); so_cache_time = net_uptime(); if (++cached_sock_count > max_cached_sock_count) { --cached_sock_count; lck_mtx_unlock(so_cache_mtx); zfree(so_cache_zone, so); } else { if (so_cache_hw < cached_sock_count) so_cache_hw = cached_sock_count; STAILQ_INSERT_TAIL(&so_cache_head, so, so_cache_ent); so->cache_timestamp = so_cache_time; lck_mtx_unlock(so_cache_mtx); } } void so_update_last_owner_locked(struct socket *so, proc_t self) { if (so->last_pid != 0) { /* * last_pid and last_upid should remain zero for sockets * created using sock_socket. The check above achieves that */ if (self == PROC_NULL) self = current_proc(); if (so->last_upid != proc_uniqueid(self) || so->last_pid != proc_pid(self)) { so->last_upid = proc_uniqueid(self); so->last_pid = proc_pid(self); proc_getexecutableuuid(self, so->last_uuid, sizeof (so->last_uuid)); } proc_pidoriginatoruuid(so->so_vuuid, sizeof(so->so_vuuid)); } } void so_update_policy(struct socket *so) { if (SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) (void) inp_update_policy(sotoinpcb(so)); } #if NECP static void so_update_necp_policy(struct socket *so, struct sockaddr *override_local_addr, struct sockaddr *override_remote_addr) { if (SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) inp_update_necp_policy(sotoinpcb(so), override_local_addr, override_remote_addr, 0); } #endif /* NECP */ boolean_t so_cache_timer(void) { struct socket *p; int n_freed = 0; boolean_t rc = FALSE; lck_mtx_lock(so_cache_mtx); so_cache_timeouts++; so_cache_time = net_uptime(); while (!STAILQ_EMPTY(&so_cache_head)) { VERIFY(cached_sock_count > 0); p = STAILQ_FIRST(&so_cache_head); if ((so_cache_time - p->cache_timestamp) < SO_CACHE_TIME_LIMIT) break; STAILQ_REMOVE_HEAD(&so_cache_head, so_cache_ent); --cached_sock_count; zfree(so_cache_zone, p); if (++n_freed >= SO_CACHE_MAX_FREE_BATCH) { so_cache_max_freed++; break; } } /* Schedule again if there is more to cleanup */ if (!STAILQ_EMPTY(&so_cache_head)) rc = TRUE; lck_mtx_unlock(so_cache_mtx); return (rc); } /* * Get a socket structure from our zone, and initialize it. * We don't implement `waitok' yet (see comments in uipc_domain.c). * Note that it would probably be better to allocate socket * and PCB at the same time, but I'm not convinced that all * the protocols can be easily modified to do this. */ struct socket * soalloc(int waitok, int dom, int type) { struct socket *so; if ((dom == PF_INET) && (type == SOCK_STREAM)) { cached_sock_alloc(&so, waitok); } else { MALLOC_ZONE(so, struct socket *, sizeof (*so), socket_zone, M_WAITOK); if (so != NULL) bzero(so, sizeof (*so)); } if (so != NULL) { so->so_gencnt = OSIncrementAtomic64((SInt64 *)&so_gencnt); so->so_zone = socket_zone; #if CONFIG_MACF_SOCKET /* Convert waitok to M_WAITOK/M_NOWAIT for MAC Framework. */ if (mac_socket_label_init(so, !waitok) != 0) { sodealloc(so); return (NULL); } #endif /* MAC_SOCKET */ } return (so); } int socreate_internal(int dom, struct socket **aso, int type, int proto, struct proc *p, uint32_t flags, struct proc *ep) { struct protosw *prp; struct socket *so; int error = 0; #if TCPDEBUG extern int tcpconsdebug; #endif VERIFY(aso != NULL); *aso = NULL; if (proto != 0) prp = pffindproto(dom, proto, type); else prp = pffindtype(dom, type); if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL) { if (pffinddomain(dom) == NULL) return (EAFNOSUPPORT); if (proto != 0) { if (pffindprotonotype(dom, proto) != NULL) return (EPROTOTYPE); } return (EPROTONOSUPPORT); } if (prp->pr_type != type) return (EPROTOTYPE); so = soalloc(1, dom, type); if (so == NULL) return (ENOBUFS); if (flags & SOCF_ASYNC) so->so_state |= SS_NBIO; #if MULTIPATH if (flags & SOCF_MP_SUBFLOW) { /* * A multipath subflow socket is used internally in the kernel, * therefore it does not have a file desciptor associated by * default. */ so->so_state |= SS_NOFDREF; so->so_flags |= SOF_MP_SUBFLOW; } #endif /* MULTIPATH */ TAILQ_INIT(&so->so_incomp); TAILQ_INIT(&so->so_comp); so->so_type = type; so->last_upid = proc_uniqueid(p); so->last_pid = proc_pid(p); proc_getexecutableuuid(p, so->last_uuid, sizeof (so->last_uuid)); proc_pidoriginatoruuid(so->so_vuuid, sizeof(so->so_vuuid)); if (ep != PROC_NULL && ep != p) { so->e_upid = proc_uniqueid(ep); so->e_pid = proc_pid(ep); proc_getexecutableuuid(ep, so->e_uuid, sizeof (so->e_uuid)); so->so_flags |= SOF_DELEGATED; } so->so_cred = kauth_cred_proc_ref(p); if (!suser(kauth_cred_get(), NULL)) so->so_state |= SS_PRIV; so->so_proto = prp; so->so_rcv.sb_flags |= SB_RECV; so->so_rcv.sb_so = so->so_snd.sb_so = so; so->next_lock_lr = 0; so->next_unlock_lr = 0; #if CONFIG_MACF_SOCKET mac_socket_label_associate(kauth_cred_get(), so); #endif /* MAC_SOCKET */ /* * Attachment will create the per pcb lock if necessary and * increase refcount for creation, make sure it's done before * socket is inserted in lists. */ so->so_usecount++; error = (*prp->pr_usrreqs->pru_attach)(so, proto, p); if (error != 0) { /* * Warning: * If so_pcb is not zero, the socket will be leaked, * so protocol attachment handler must be coded carefuly */ so->so_state |= SS_NOFDREF; so->so_usecount--; sofreelastref(so, 1); /* will deallocate the socket */ return (error); } atomic_add_32(&prp->pr_domain->dom_refs, 1); TAILQ_INIT(&so->so_evlist); /* Attach socket filters for this protocol */ sflt_initsock(so); #if TCPDEBUG if (tcpconsdebug == 2) so->so_options |= SO_DEBUG; #endif so_set_default_traffic_class(so); /* * If this thread or task is marked to create backgrounded sockets, * mark the socket as background. */ if (proc_get_effective_thread_policy(current_thread(), TASK_POLICY_NEW_SOCKETS_BG)) { socket_set_traffic_mgt_flags(so, TRAFFIC_MGT_SO_BACKGROUND); so->so_background_thread = current_thread(); } switch (dom) { /* * Don't mark Unix domain, system or multipath sockets as * eligible for defunct by default. */ case PF_LOCAL: case PF_SYSTEM: case PF_MULTIPATH: so->so_flags |= SOF_NODEFUNCT; break; default: break; } /* * Entitlements can't be checked at socket creation time except if the * application requested a feature guarded by a privilege (c.f., socket * delegation). * The priv(9) and the Sandboxing APIs are designed with the idea that * a privilege check should only be triggered by a userland request. * A privilege check at socket creation time is time consuming and * could trigger many authorisation error messages from the security * APIs. */ *aso = so; return (0); } /* * Returns: 0 Success * EAFNOSUPPORT * EPROTOTYPE * EPROTONOSUPPORT * ENOBUFS * <pru_attach>:ENOBUFS[AF_UNIX] * <pru_attach>:ENOBUFS[TCP] * <pru_attach>:ENOMEM[TCP] * <pru_attach>:??? [other protocol families, IPSEC] */ int socreate(int dom, struct socket **aso, int type, int proto) { return (socreate_internal(dom, aso, type, proto, current_proc(), 0, PROC_NULL)); } int socreate_delegate(int dom, struct socket **aso, int type, int proto, pid_t epid) { int error = 0; struct proc *ep = PROC_NULL; if ((proc_selfpid() != epid) && ((ep = proc_find(epid)) == PROC_NULL)) { error = ESRCH; goto done; } error = socreate_internal(dom, aso, type, proto, current_proc(), 0, ep); /* * It might not be wise to hold the proc reference when calling * socreate_internal since it calls soalloc with M_WAITOK */ done: if (ep != PROC_NULL) proc_rele(ep); return (error); } /* * Returns: 0 Success * <pru_bind>:EINVAL Invalid argument [COMMON_START] * <pru_bind>:EAFNOSUPPORT Address family not supported * <pru_bind>:EADDRNOTAVAIL Address not available. * <pru_bind>:EINVAL Invalid argument * <pru_bind>:EAFNOSUPPORT Address family not supported [notdef] * <pru_bind>:EACCES Permission denied * <pru_bind>:EADDRINUSE Address in use * <pru_bind>:EAGAIN Resource unavailable, try again * <pru_bind>:EPERM Operation not permitted * <pru_bind>:??? * <sf_bind>:??? * * Notes: It's not possible to fully enumerate the return codes above, * since socket filter authors and protocol family authors may * not choose to limit their error returns to those listed, even * though this may result in some software operating incorrectly. * * The error codes which are enumerated above are those known to * be returned by the tcp_usr_bind function supplied. */ int sobindlock(struct socket *so, struct sockaddr *nam, int dolock) { struct proc *p = current_proc(); int error = 0; if (dolock) socket_lock(so, 1); VERIFY(so->so_usecount > 1); so_update_last_owner_locked(so, p); so_update_policy(so); #if NECP so_update_necp_policy(so, nam, NULL); #endif /* NECP */ /* * If this is a bind request on a socket that has been marked * as inactive, reject it now before we go any further. */ if (so->so_flags & SOF_DEFUNCT) { error = EINVAL; SODEFUNCTLOG(("%s[%d]: defunct so 0x%llx [%d,%d] (%d)\n", __func__, proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), error)); goto out; } /* Socket filter */ error = sflt_bind(so, nam); if (error == 0) error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p); out: if (dolock) socket_unlock(so, 1); if (error == EJUSTRETURN) error = 0; return (error); } void sodealloc(struct socket *so) { kauth_cred_unref(&so->so_cred); /* Remove any filters */ sflt_termsock(so); #if CONTENT_FILTER cfil_sock_detach(so); #endif /* CONTENT_FILTER */ /* Delete the state allocated for msg queues on a socket */ if (so->so_flags & SOF_ENABLE_MSGS) { FREE(so->so_msg_state, M_TEMP); so->so_msg_state = NULL; } VERIFY(so->so_msg_state == NULL); so->so_gencnt = OSIncrementAtomic64((SInt64 *)&so_gencnt); #if CONFIG_MACF_SOCKET mac_socket_label_destroy(so); #endif /* MAC_SOCKET */ if (so->so_flags1 & SOF1_CACHED_IN_SOCK_LAYER) { cached_sock_free(so); } else { FREE_ZONE(so, sizeof (*so), so->so_zone); } } /* * Returns: 0 Success * EINVAL * EOPNOTSUPP * <pru_listen>:EINVAL[AF_UNIX] * <pru_listen>:EINVAL[TCP] * <pru_listen>:EADDRNOTAVAIL[TCP] Address not available. * <pru_listen>:EINVAL[TCP] Invalid argument * <pru_listen>:EAFNOSUPPORT[TCP] Address family not supported [notdef] * <pru_listen>:EACCES[TCP] Permission denied * <pru_listen>:EADDRINUSE[TCP] Address in use * <pru_listen>:EAGAIN[TCP] Resource unavailable, try again * <pru_listen>:EPERM[TCP] Operation not permitted * <sf_listen>:??? * * Notes: Other <pru_listen> returns depend on the protocol family; all * <sf_listen> returns depend on what the filter author causes * their filter to return. */ int solisten(struct socket *so, int backlog) { struct proc *p = current_proc(); int error = 0; socket_lock(so, 1); so_update_last_owner_locked(so, p); so_update_policy(so); #if NECP so_update_necp_policy(so, NULL, NULL); #endif /* NECP */ if (so->so_proto == NULL) { error = EINVAL; goto out; } if ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0) { error = EOPNOTSUPP; goto out; } /* * If the listen request is made on a socket that is not fully * disconnected, or on a socket that has been marked as inactive, * reject the request now. */ if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING)) || (so->so_flags & SOF_DEFUNCT)) { error = EINVAL; if (so->so_flags & SOF_DEFUNCT) { SODEFUNCTLOG(("%s[%d]: defunct so 0x%llx [%d,%d] " "(%d)\n", __func__, proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), error)); } goto out; } if ((so->so_restrictions & SO_RESTRICT_DENY_IN) != 0) { error = EPERM; goto out; } error = sflt_listen(so); if (error == 0) error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p); if (error) { if (error == EJUSTRETURN) error = 0; goto out; } if (TAILQ_EMPTY(&so->so_comp)) so->so_options |= SO_ACCEPTCONN; /* * POSIX: The implementation may have an upper limit on the length of * the listen queue-either global or per accepting socket. If backlog * exceeds this limit, the length of the listen queue is set to the * limit. * * If listen() is called with a backlog argument value that is less * than 0, the function behaves as if it had been called with a backlog * argument value of 0. * * A backlog argument of 0 may allow the socket to accept connections, * in which case the length of the listen queue may be set to an * implementation-defined minimum value. */ if (backlog <= 0 || backlog > somaxconn) backlog = somaxconn; so->so_qlimit = backlog; out: socket_unlock(so, 1); return (error); } void sofreelastref(struct socket *so, int dealloc) { struct socket *head = so->so_head; /* Assume socket is locked */ if (!(so->so_flags & SOF_PCBCLEARING) || !(so->so_state & SS_NOFDREF)) { selthreadclear(&so->so_snd.sb_sel); selthreadclear(&so->so_rcv.sb_sel); so->so_rcv.sb_flags &= ~(SB_SEL|SB_UPCALL); so->so_snd.sb_flags &= ~(SB_SEL|SB_UPCALL); so->so_event = sonullevent; return; } if (head != NULL) { socket_lock(head, 1); if (so->so_state & SS_INCOMP) { TAILQ_REMOVE(&head->so_incomp, so, so_list); head->so_incqlen--; } else if (so->so_state & SS_COMP) { /* * We must not decommission a socket that's * on the accept(2) queue. If we do, then * accept(2) may hang after select(2) indicated * that the listening socket was ready. */ selthreadclear(&so->so_snd.sb_sel); selthreadclear(&so->so_rcv.sb_sel); so->so_rcv.sb_flags &= ~(SB_SEL|SB_UPCALL); so->so_snd.sb_flags &= ~(SB_SEL|SB_UPCALL); so->so_event = sonullevent; socket_unlock(head, 1); return; } else { panic("sofree: not queued"); } head->so_qlen--; so->so_state &= ~SS_INCOMP; so->so_head = NULL; socket_unlock(head, 1); } sowflush(so); sorflush(so); #if FLOW_DIVERT if (so->so_flags & SOF_FLOW_DIVERT) { flow_divert_detach(so); } #endif /* FLOW_DIVERT */ /* 3932268: disable upcall */ so->so_rcv.sb_flags &= ~SB_UPCALL; so->so_snd.sb_flags &= ~SB_UPCALL; so->so_event = sonullevent; if (dealloc) sodealloc(so); } void soclose_wait_locked(struct socket *so) { lck_mtx_t *mutex_held; if (so->so_proto->pr_getlock != NULL) mutex_held = (*so->so_proto->pr_getlock)(so, 0); else mutex_held = so->so_proto->pr_domain->dom_mtx; lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); /* * Double check here and return if there's no outstanding upcall; * otherwise proceed further only if SOF_UPCALLCLOSEWAIT is set. */ if (!so->so_upcallusecount || !(so->so_flags & SOF_UPCALLCLOSEWAIT)) return; so->so_rcv.sb_flags &= ~SB_UPCALL; so->so_snd.sb_flags &= ~SB_UPCALL; so->so_flags |= SOF_CLOSEWAIT; (void) msleep((caddr_t)&so->so_upcallusecount, mutex_held, (PZERO - 1), "soclose_wait_locked", NULL); lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); so->so_flags &= ~SOF_CLOSEWAIT; } /* * Close a socket on last file table reference removal. * Initiate disconnect if connected. * Free socket when disconnect complete. */ int soclose_locked(struct socket *so) { int error = 0; lck_mtx_t *mutex_held; struct timespec ts; if (so->so_usecount == 0) { panic("soclose: so=%p refcount=0\n", so); /* NOTREACHED */ } sflt_notify(so, sock_evt_closing, NULL); if (so->so_upcallusecount) soclose_wait_locked(so); #if CONTENT_FILTER /* * We have to wait until the content filters are done */ if ((so->so_flags & SOF_CONTENT_FILTER) != 0) { cfil_sock_close_wait(so); cfil_sock_is_closed(so); cfil_sock_detach(so); } #endif /* CONTENT_FILTER */ if (so->so_flags1 & SOF1_EXTEND_BK_IDLE_INPROG) { soresume(current_proc(), so, 1); so->so_flags1 &= ~SOF1_EXTEND_BK_IDLE_WANTED; } if ((so->so_options & SO_ACCEPTCONN)) { struct socket *sp, *sonext; int socklock = 0; /* * We do not want new connection to be added * to the connection queues */ so->so_options &= ~SO_ACCEPTCONN; for (sp = TAILQ_FIRST(&so->so_incomp); sp != NULL; sp = sonext) { sonext = TAILQ_NEXT(sp, so_list); /* * Radar 5350314 * skip sockets thrown away by tcpdropdropblreq * they will get cleanup by the garbage collection. * otherwise, remove the incomp socket from the queue * and let soabort trigger the appropriate cleanup. */ if (sp->so_flags & SOF_OVERFLOW) continue; if (so->so_proto->pr_getlock != NULL) { /* * Lock ordering for consistency with the * rest of the stack, we lock the socket * first and then grabb the head. */ socket_unlock(so, 0); socket_lock(sp, 1); socket_lock(so, 0); socklock = 1; } TAILQ_REMOVE(&so->so_incomp, sp, so_list); so->so_incqlen--; if (sp->so_state & SS_INCOMP) { sp->so_state &= ~SS_INCOMP; sp->so_head = NULL; (void) soabort(sp); } if (socklock) socket_unlock(sp, 1); } while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) { /* Dequeue from so_comp since sofree() won't do it */ TAILQ_REMOVE(&so->so_comp, sp, so_list); so->so_qlen--; if (so->so_proto->pr_getlock != NULL) { socket_unlock(so, 0); socket_lock(sp, 1); } if (sp->so_state & SS_COMP) { sp->so_state &= ~SS_COMP; sp->so_head = NULL; (void) soabort(sp); } if (so->so_proto->pr_getlock != NULL) { socket_unlock(sp, 1); socket_lock(so, 0); } } } if (so->so_pcb == NULL) { /* 3915887: mark the socket as ready for dealloc */ so->so_flags |= SOF_PCBCLEARING; goto discard; } if (so->so_state & SS_ISCONNECTED) { if ((so->so_state & SS_ISDISCONNECTING) == 0) { error = sodisconnectlocked(so); if (error) goto drop; } if (so->so_options & SO_LINGER) { if ((so->so_state & SS_ISDISCONNECTING) && (so->so_state & SS_NBIO)) goto drop; if (so->so_proto->pr_getlock != NULL) mutex_held = (*so->so_proto->pr_getlock)(so, 0); else mutex_held = so->so_proto->pr_domain->dom_mtx; while (so->so_state & SS_ISCONNECTED) { ts.tv_sec = (so->so_linger/100); ts.tv_nsec = (so->so_linger % 100) * NSEC_PER_USEC * 1000 * 10; error = msleep((caddr_t)&so->so_timeo, mutex_held, PSOCK | PCATCH, "soclose", &ts); if (error) { /* * It's OK when the time fires, * don't report an error */ if (error == EWOULDBLOCK) error = 0; break; } } } } drop: if (so->so_usecount == 0) { panic("soclose: usecount is zero so=%p\n", so); /* NOTREACHED */ } if (so->so_pcb != NULL && !(so->so_flags & SOF_PCBCLEARING)) { int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so); if (error == 0) error = error2; } if (so->so_usecount <= 0) { panic("soclose: usecount is zero so=%p\n", so); /* NOTREACHED */ } discard: if (so->so_pcb != NULL && !(so->so_flags & SOF_MP_SUBFLOW) && (so->so_state & SS_NOFDREF)) { panic("soclose: NOFDREF"); /* NOTREACHED */ } so->so_state |= SS_NOFDREF; if (so->so_flags & SOF_MP_SUBFLOW) so->so_flags &= ~SOF_MP_SUBFLOW; if ((so->so_flags & SOF_KNOTE) != 0) KNOTE(&so->so_klist, SO_FILT_HINT_LOCKED); atomic_add_32(&so->so_proto->pr_domain->dom_refs, -1); evsofree(so); so->so_usecount--; sofree(so); return (error); } int soclose(struct socket *so) { int error = 0; socket_lock(so, 1); if (so->so_retaincnt == 0) { error = soclose_locked(so); } else { /* * if the FD is going away, but socket is * retained in kernel remove its reference */ so->so_usecount--; if (so->so_usecount < 2) panic("soclose: retaincnt non null and so=%p " "usecount=%d\n", so, so->so_usecount); } socket_unlock(so, 1); return (error); } /* * Must be called at splnet... */ /* Should already be locked */ int soabort(struct socket *so) { int error; #ifdef MORE_LOCKING_DEBUG lck_mtx_t *mutex_held; if (so->so_proto->pr_getlock != NULL) mutex_held = (*so->so_proto->pr_getlock)(so, 0); else mutex_held = so->so_proto->pr_domain->dom_mtx; lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); #endif if ((so->so_flags & SOF_ABORTED) == 0) { so->so_flags |= SOF_ABORTED; error = (*so->so_proto->pr_usrreqs->pru_abort)(so); if (error) { sofree(so); return (error); } } return (0); } int soacceptlock(struct socket *so, struct sockaddr **nam, int dolock) { int error; if (dolock) socket_lock(so, 1); so_update_last_owner_locked(so, PROC_NULL); so_update_policy(so); #if NECP so_update_necp_policy(so, NULL, NULL); #endif /* NECP */ if ((so->so_state & SS_NOFDREF) == 0) panic("soaccept: !NOFDREF"); so->so_state &= ~SS_NOFDREF; error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam); if (dolock) socket_unlock(so, 1); return (error); } int soaccept(struct socket *so, struct sockaddr **nam) { return (soacceptlock(so, nam, 1)); } int soacceptfilter(struct socket *so) { struct sockaddr *local = NULL, *remote = NULL; int error = 0; struct socket *head = so->so_head; /* * Hold the lock even if this socket has not been made visible * to the filter(s). For sockets with global locks, this protects * against the head or peer going away */ socket_lock(so, 1); if (sogetaddr_locked(so, &remote, 1) != 0 || sogetaddr_locked(so, &local, 0) != 0) { so->so_state &= ~(SS_NOFDREF | SS_COMP); so->so_head = NULL; socket_unlock(so, 1); soclose(so); /* Out of resources; try it again next time */ error = ECONNABORTED; goto done; } error = sflt_accept(head, so, local, remote); /* * If we get EJUSTRETURN from one of the filters, mark this socket * as inactive and return it anyway. This newly accepted socket * will be disconnected later before we hand it off to the caller. */ if (error == EJUSTRETURN) { error = 0; (void) sosetdefunct(current_proc(), so, SHUTDOWN_SOCKET_LEVEL_DISCONNECT_INTERNAL, FALSE); } if (error != 0) { /* * This may seem like a duplication to the above error * handling part when we return ECONNABORTED, except * the following is done while holding the lock since * the socket has been exposed to the filter(s) earlier. */ so->so_state &= ~(SS_NOFDREF | SS_COMP); so->so_head = NULL; socket_unlock(so, 1); soclose(so); /* Propagate socket filter's error code to the caller */ } else { socket_unlock(so, 1); } done: /* Callee checks for NULL pointer */ sock_freeaddr(remote); sock_freeaddr(local); return (error); } /* * Returns: 0 Success * EOPNOTSUPP Operation not supported on socket * EISCONN Socket is connected * <pru_connect>:EADDRNOTAVAIL Address not available. * <pru_connect>:EINVAL Invalid argument * <pru_connect>:EAFNOSUPPORT Address family not supported [notdef] * <pru_connect>:EACCES Permission denied * <pru_connect>:EADDRINUSE Address in use * <pru_connect>:EAGAIN Resource unavailable, try again * <pru_connect>:EPERM Operation not permitted * <sf_connect_out>:??? [anything a filter writer might set] */ int soconnectlock(struct socket *so, struct sockaddr *nam, int dolock) { int error; struct proc *p = current_proc(); if (dolock) socket_lock(so, 1); so_update_last_owner_locked(so, p); so_update_policy(so); #if NECP so_update_necp_policy(so, NULL, nam); #endif /* NECP */ /* * If this is a listening socket or if this is a previously-accepted * socket that has been marked as inactive, reject the connect request. */ if ((so->so_options & SO_ACCEPTCONN) || (so->so_flags & SOF_DEFUNCT)) { error = EOPNOTSUPP; if (so->so_flags & SOF_DEFUNCT) { SODEFUNCTLOG(("%s[%d]: defunct so 0x%llx [%d,%d] " "(%d)\n", __func__, proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), error)); } if (dolock) socket_unlock(so, 1); return (error); } if ((so->so_restrictions & SO_RESTRICT_DENY_OUT) != 0) { if (dolock) socket_unlock(so, 1); return (EPERM); } /* * If protocol is connection-based, can only connect once. * Otherwise, if connected, try to disconnect first. * This allows user to disconnect by connecting to, e.g., * a null address. */ if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && ((so->so_proto->pr_flags & PR_CONNREQUIRED) || (error = sodisconnectlocked(so)))) { error = EISCONN; } else { /* * Run connect filter before calling protocol: * - non-blocking connect returns before completion; */ error = sflt_connectout(so, nam); if (error != 0) { if (error == EJUSTRETURN) error = 0; } else { error = (*so->so_proto->pr_usrreqs->pru_connect) (so, nam, p); } } if (dolock) socket_unlock(so, 1); return (error); } int soconnect(struct socket *so, struct sockaddr *nam) { return (soconnectlock(so, nam, 1)); } /* * Returns: 0 Success * <pru_connect2>:EINVAL[AF_UNIX] * <pru_connect2>:EPROTOTYPE[AF_UNIX] * <pru_connect2>:??? [other protocol families] * * Notes: <pru_connect2> is not supported by [TCP]. */ int soconnect2(struct socket *so1, struct socket *so2) { int error; socket_lock(so1, 1); if (so2->so_proto->pr_lock) socket_lock(so2, 1); error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2); socket_unlock(so1, 1); if (so2->so_proto->pr_lock) socket_unlock(so2, 1); return (error); } int soconnectxlocked(struct socket *so, struct sockaddr_list **src_sl, struct sockaddr_list **dst_sl, struct proc *p, uint32_t ifscope, sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg, uint32_t arglen, uio_t auio, user_ssize_t *bytes_written) { int error; so_update_last_owner_locked(so, p); so_update_policy(so); /* * If this is a listening socket or if this is a previously-accepted * socket that has been marked as inactive, reject the connect request. */ if ((so->so_options & SO_ACCEPTCONN) || (so->so_flags & SOF_DEFUNCT)) { error = EOPNOTSUPP; if (so->so_flags & SOF_DEFUNCT) { SODEFUNCTLOG(("%s[%d]: defunct so 0x%llx [%d,%d] " "(%d)\n", __func__, proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), error)); } return (error); } if ((so->so_restrictions & SO_RESTRICT_DENY_OUT) != 0) return (EPERM); /* * If protocol is connection-based, can only connect once * unless PR_MULTICONN is set. Otherwise, if connected, * try to disconnect first. This allows user to disconnect * by connecting to, e.g., a null address. */ if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) && !(so->so_proto->pr_flags & PR_MULTICONN) && ((so->so_proto->pr_flags & PR_CONNREQUIRED) || (error = sodisconnectlocked(so)) != 0)) { error = EISCONN; } else { /* * Run connect filter before calling protocol: * - non-blocking connect returns before completion; */ error = sflt_connectxout(so, dst_sl); if (error != 0) { if (error == EJUSTRETURN) error = 0; } else { error = (*so->so_proto->pr_usrreqs->pru_connectx) (so, src_sl, dst_sl, p, ifscope, aid, pcid, flags, arg, arglen, auio, bytes_written); } } return (error); } int sodisconnectlocked(struct socket *so) { int error; if ((so->so_state & SS_ISCONNECTED) == 0) { error = ENOTCONN; goto bad; } if (so->so_state & SS_ISDISCONNECTING) { error = EALREADY; goto bad; } error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so); if (error == 0) sflt_notify(so, sock_evt_disconnected, NULL); bad: return (error); } /* Locking version */ int sodisconnect(struct socket *so) { int error; socket_lock(so, 1); error = sodisconnectlocked(so); socket_unlock(so, 1); return (error); } int sodisconnectxlocked(struct socket *so, sae_associd_t aid, sae_connid_t cid) { int error; /* * Call the protocol disconnectx handler; let it handle all * matters related to the connection state of this session. */ error = (*so->so_proto->pr_usrreqs->pru_disconnectx)(so, aid, cid); if (error == 0) { /* * The event applies only for the session, not for * the disconnection of individual subflows. */ if (so->so_state & (SS_ISDISCONNECTING|SS_ISDISCONNECTED)) sflt_notify(so, sock_evt_disconnected, NULL); } return (error); } int sodisconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid) { int error; socket_lock(so, 1); error = sodisconnectxlocked(so, aid, cid); socket_unlock(so, 1); return (error); } int sopeelofflocked(struct socket *so, sae_associd_t aid, struct socket **psop) { return ((*so->so_proto->pr_usrreqs->pru_peeloff)(so, aid, psop)); } #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT) /* * sosendcheck will lock the socket buffer if it isn't locked and * verify that there is space for the data being inserted. * * Returns: 0 Success * EPIPE * sblock:EWOULDBLOCK * sblock:EINTR * sbwait:EBADF * sbwait:EINTR * [so_error]:??? */ int sosendcheck(struct socket *so, struct sockaddr *addr, user_ssize_t resid, int32_t clen, int32_t atomic, int flags, int *sblocked, struct mbuf *control) { int error = 0; int32_t space; int assumelock = 0; restart: if (*sblocked == 0) { if ((so->so_snd.sb_flags & SB_LOCK) != 0 && so->so_send_filt_thread != 0 && so->so_send_filt_thread == current_thread()) { /* * We're being called recursively from a filter, * allow this to continue. Radar 4150520. * Don't set sblocked because we don't want * to perform an unlock later. */ assumelock = 1; } else { error = sblock(&so->so_snd, SBLOCKWAIT(flags)); if (error) { if (so->so_flags & SOF_DEFUNCT) goto defunct; return (error); } *sblocked = 1; } } /* * If a send attempt is made on a socket that has been marked * as inactive (disconnected), reject the request. */ if (so->so_flags & SOF_DEFUNCT) { defunct: error = EPIPE; SODEFUNCTLOG(("%s[%d]: defunct so 0x%llx [%d,%d] (%d)\n", __func__, proc_selfpid(), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), error)); return (error); } if (so->so_state & SS_CANTSENDMORE) { #if CONTENT_FILTER /* * Can re-inject data of half closed connections */ if ((so->so_state & SS_ISDISCONNECTED) == 0 && so->so_snd.sb_cfil_thread == current_thread() && cfil_sock_data_pending(&so->so_snd) != 0) CFIL_LOG(LOG_INFO, "so %llx ignore SS_CANTSENDMORE", (uint64_t)DEBUG_KERNEL_ADDRPERM(so)); else #endif /* CONTENT_FILTER */ return (EPIPE); } if (so->so_error) { error = so->so_error; so->so_error = 0; return (error); } if ((so->so_state & SS_ISCONNECTED) == 0) { if ((so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) { if (((so->so_state & SS_ISCONFIRMING) == 0) && (resid != 0 || clen == 0) && !(so->so_flags1 & SOF1_PRECONNECT_DATA)) { #if MPTCP /* * MPTCP Fast Join sends data before the * socket is truly connected. */ if ((so->so_flags & (SOF_MP_SUBFLOW | SOF_MPTCP_FASTJOIN)) != (SOF_MP_SUBFLOW | SOF_MPTCP_FASTJOIN)) #endif /* MPTCP */ return (ENOTCONN); } } else if (addr == 0 && !(flags&MSG_HOLD)) { return ((so->so_proto->pr_flags & PR_CONNREQUIRED) ? ENOTCONN : EDESTADDRREQ); } } if (so->so_flags & SOF_ENABLE_MSGS) space = msgq_sbspace(so, control); else space = sbspace(&so->so_snd); if (flags & MSG_OOB) space += 1024; if ((atomic && resid > so->so_snd.sb_hiwat) || clen > so->so_snd.sb_hiwat) return (EMSGSIZE); if ((space < resid + clen && (atomic || (space < (int32_t)so->so_snd.sb_lowat) || space < clen)) || (so->so_type == SOCK_STREAM && so_wait_for_if_feedback(so))) { /* * don't block the connectx call when there's more data * than can be copied. */ if (so->so_flags1 & SOF1_PRECONNECT_DATA) { if (space == 0) { return (EWOULDBLOCK); } if (space < (int32_t)so->so_snd.sb_lowat) { return (0); } } if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO) || assumelock) { return (EWOULDBLOCK); } sbunlock(&so->so_snd, TRUE); /* keep socket locked */ *sblocked = 0; error = sbwait(&so->so_snd); if (error) { if (so->so_flags & SOF_DEFUNCT) goto defunct; return (error); } goto restart; } return (0); } /* * Send on a socket. * If send must go all at once and message is larger than * send buffering, then hard error. * Lock against other senders. * If must go all at once and not enough room now, then * inform user that this would block and do nothing. * Otherwise, if nonblocking, send as much as possible. * The data to be sent is described by "uio" if nonzero, * otherwise by the mbuf chain "top" (which must be null * if uio is not). Data provided in mbuf chain must be small * enough to send all at once. * * Returns nonzero on error, timeout or signal; callers * must check for short counts if EINTR/ERESTART are returned. * Data and control buffers are freed on return. * Experiment: * MSG_HOLD: go thru most of sosend(), but just enqueue the mbuf * MSG_SEND: go thru as for MSG_HOLD on current fragment, then * point at the mbuf chain being constructed and go from there. * * Returns: 0 Success * EOPNOTSUPP * EINVAL * ENOBUFS * uiomove:EFAULT * sosendcheck:EPIPE * sosendcheck:EWOULDBLOCK * sosendcheck:EINTR * sosendcheck:EBADF * sosendcheck:EINTR * sosendcheck:??? [value from so_error] * <pru_send>:ECONNRESET[TCP] * <pru_send>:EINVAL[TCP] * <pru_send>:ENOBUFS[TCP] * <pru_send>:EADDRINUSE[TCP] * <pru_send>:EADDRNOTAVAIL[TCP] * <pru_send>:EAFNOSUPPORT[TCP] * <pru_send>:EACCES[TCP] * <pru_send>:EAGAIN[TCP] * <pru_send>:EPERM[TCP] * <pru_send>:EMSGSIZE[TCP] * <pru_send>:EHOSTUNREACH[TCP] * <pru_send>:ENETUNREACH[TCP] * <pru_send>:ENETDOWN[TCP] * <pru_send>:ENOMEM[TCP] * <pru_send>:ENOBUFS[TCP] * <pru_send>:???[TCP] [ignorable: mostly IPSEC/firewall/DLIL] * <pru_send>:EINVAL[AF_UNIX] * <pru_send>:EOPNOTSUPP[AF_UNIX] * <pru_send>:EPIPE[AF_UNIX] * <pru_send>:ENOTCONN[AF_UNIX] * <pru_send>:EISCONN[AF_UNIX] * <pru_send>:???[AF_UNIX] [whatever a filter author chooses] * <sf_data_out>:??? [whatever a filter author chooses] * * Notes: Other <pru_send> returns depend on the protocol family; all * <sf_data_out> returns depend on what the filter author causes * their filter to return. */ int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, struct mbuf *top, struct mbuf *control, int flags) { struct mbuf **mp; struct mbuf *m, *freelist = NULL; user_ssize_t space, len, resid, orig_resid; int clen = 0, error, dontroute, mlen, sendflags; int atomic = sosendallatonce(so) || top; int sblocked = 0; struct proc *p = current_proc(); struct mbuf *control_copy = NULL; uint16_t headroom = 0; boolean_t en_tracing = FALSE; if (uio != NULL) resid = uio_resid(uio); else resid = top->m_pkthdr.len; KERNEL_DEBUG((DBG_FNC_SOSEND | DBG_FUNC_START), so, resid, so->so_snd.sb_cc, so->so_snd.sb_lowat, so->so_snd.sb_hiwat); socket_lock(so, 1); /* * trace if tracing & network (vs. unix) sockets & and * non-loopback */ if (ENTR_SHOULDTRACE && (SOCK_CHECK_DOM(so, AF_INET) || SOCK_CHECK_DOM(so, AF_INET6))) { struct inpcb *inp = sotoinpcb(so); if (inp->inp_last_outifp != NULL && !(inp->inp_last_outifp->if_flags & IFF_LOOPBACK)) { en_tracing = TRUE; KERNEL_ENERGYTRACE(kEnTrActKernSockWrite, DBG_FUNC_START, VM_KERNEL_ADDRPERM(so), ((so->so_state & SS_NBIO) ? kEnTrFlagNonBlocking : 0), (int64_t)resid); orig_resid = resid; } } /* * Re-injection should not affect process accounting */ if ((flags & MSG_SKIPCFIL) == 0) { so_update_last_owner_locked(so, p); so_update_policy(so); #if NECP so_update_necp_policy(so, NULL, addr); #endif /* NECP */ } if (so->so_type != SOCK_STREAM && (flags & MSG_OOB) != 0) { error = EOPNOTSUPP; socket_unlock(so, 1); goto out; } /* * In theory resid should be unsigned. * However, space must be signed, as it might be less than 0 * if we over-committed, and we must use a signed comparison * of space and resid. On the other hand, a negative resid * causes us to loop sending 0-length segments to the protocol. * * Usually, MSG_EOR isn't used on SOCK_STREAM type sockets. * But it will be used by sockets doing message delivery. * * Note: We limit resid to be a positive int value as we use * imin() to set bytes_to_copy -- radr://14558484 */ if (resid < 0 || resid > INT_MAX || (so->so_type == SOCK_STREAM && !(so->so_flags & SOF_ENABLE_MSGS) && (flags & MSG_EOR))) { error = EINVAL; socket_unlock(so, 1); goto out; } dontroute = (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && (so->so_proto->pr_flags & PR_ATOMIC); OSIncrementAtomicLong(&p->p_stats->p_ru.ru_msgsnd); if (control != NULL) clen = control->m_len; if (soreserveheadroom != 0) headroom = so->so_pktheadroom; do { error = sosendcheck(so, addr, resid, clen, atomic, flags, &sblocked, control); if (error) goto release; mp = ⊤ if (so->so_flags & SOF_ENABLE_MSGS) space = msgq_sbspace(so, control); else space = sbspace(&so->so_snd) - clen; space += ((flags & MSG_OOB) ? 1024 : 0); do { if (uio == NULL) { /* * Data is prepackaged in "top". */ resid = 0; if (flags & MSG_EOR) top->m_flags |= M_EOR; } else { int chainlength; int bytes_to_copy; boolean_t jumbocl; boolean_t bigcl; int bytes_to_alloc; bytes_to_copy = imin(resid, space); bytes_to_alloc = bytes_to_copy; if (top == NULL) bytes_to_alloc += headroom; if (sosendminchain > 0) chainlength = 0; else chainlength = sosendmaxchain; /* * Use big 4 KB cluster when the outgoing interface * does not prefer 2 KB clusters */ bigcl = !(so->so_flags1 & SOF1_IF_2KCL) || sosendbigcl_ignore_capab; /* * Attempt to use larger than system page-size * clusters for large writes only if there is * a jumbo cluster pool and if the socket is * marked accordingly. */ jumbocl = sosendjcl && njcl > 0 && ((so->so_flags & SOF_MULTIPAGES) || sosendjcl_ignore_capab) && bigcl; socket_unlock(so, 0); do { int num_needed; int hdrs_needed = (top == NULL) ? 1 : 0; /* * try to maintain a local cache of mbuf * clusters needed to complete this * write the list is further limited to * the number that are currently needed * to fill the socket this mechanism * allows a large number of mbufs/ * clusters to be grabbed under a single * mbuf lock... if we can't get any * clusters, than fall back to trying * for mbufs if we fail early (or * miscalcluate the number needed) make * sure to release any clusters we * haven't yet consumed. */ if (freelist == NULL && bytes_to_alloc > MBIGCLBYTES && jumbocl) { num_needed = bytes_to_alloc / M16KCLBYTES; if ((bytes_to_alloc - (num_needed * M16KCLBYTES)) >= MINCLSIZE) num_needed++; freelist = m_getpackets_internal( (unsigned int *)&num_needed, hdrs_needed, M_WAIT, 0, M16KCLBYTES); /* * Fall back to 4K cluster size * if allocation failed */ } if (freelist == NULL && bytes_to_alloc > MCLBYTES && bigcl) { num_needed = bytes_to_alloc / MBIGCLBYTES; if ((bytes_to_alloc - (num_needed * MBIGCLBYTES)) >= MINCLSIZE) num_needed++; freelist = m_getpackets_internal( (unsigned int *)&num_needed, hdrs_needed, M_WAIT, 0, MBIGCLBYTES); /* * Fall back to cluster size * if allocation failed */ } /* * Allocate a cluster as we want to * avoid to split the data in more * that one segment and using MINCLSIZE * would lead us to allocate two mbufs */ if (soreserveheadroom != 0 && freelist == NULL && ((top == NULL && bytes_to_alloc > _MHLEN) || bytes_to_alloc > _MLEN)) { num_needed = ROUNDUP(bytes_to_alloc, MCLBYTES) / MCLBYTES; freelist = m_getpackets_internal( (unsigned int *)&num_needed, hdrs_needed, M_WAIT, 0, MCLBYTES); /* * Fall back to a single mbuf * if allocation failed */ } else if (freelist == NULL && bytes_to_alloc > MINCLSIZE) { num_needed = bytes_to_alloc / MCLBYTES; if ((bytes_to_alloc - (num_needed * MCLBYTES)) >= MINCLSIZE) num_needed++; freelist = m_getpackets_internal( (unsigned int *)&num_needed, hdrs_needed, M_WAIT, 0, MCLBYTES); /* * Fall back to a single mbuf * if allocation failed */ } /* * For datagram protocols, leave * headroom for protocol headers * in the first cluster of the chain */ if (freelist != NULL && atomic && top == NULL && headroom > 0) { freelist->m_data += headroom; } /* * Fall back to regular mbufs without * reserving the socket headroom */ if (freelist == NULL) { if (top == NULL) MGETHDR(freelist, M_WAIT, MT_DATA); else MGET(freelist, M_WAIT, MT_DATA); if (freelist == NULL) { error = ENOBUFS; socket_lock(so, 0); goto release; } /* * For datagram protocols, * leave room for protocol * headers in first mbuf. */ if (atomic && top == NULL && bytes_to_copy < MHLEN) { MH_ALIGN(freelist, bytes_to_copy); } } m = freelist; freelist = m->m_next; m->m_next = NULL; if ((m->m_flags & M_EXT)) mlen = m->m_ext.ext_size - m_leadingspace(m); else if ((m->m_flags & M_PKTHDR)) mlen = MHLEN - m_leadingspace(m); else mlen = MLEN - m_leadingspace(m); len = imin(mlen, bytes_to_copy); chainlength += len; space -= len; error = uiomove(mtod(m, caddr_t), len, uio); resid = uio_resid(uio); m->m_len = len; *mp = m; top->m_pkthdr.len += len; if (error) break; mp = &m->m_next; if (resid <= 0) { if (flags & MSG_EOR) top->m_flags |= M_EOR; break; } bytes_to_copy = min(resid, space); } while (space > 0 && (chainlength < sosendmaxchain || atomic || resid < MINCLSIZE)); socket_lock(so, 0); if (error) goto release; } if (flags & (MSG_HOLD|MSG_SEND)) { /* Enqueue for later, go away if HOLD */ struct mbuf *mb1; if (so->so_temp && (flags & MSG_FLUSH)) { m_freem(so->so_temp); so->so_temp = NULL; } if (so->so_temp) so->so_tail->m_next = top; else so->so_temp = top; mb1 = top; while (mb1->m_next) mb1 = mb1->m_next; so->so_tail = mb1; if (flags & MSG_HOLD) { top = NULL; goto release; } top = so->so_temp; } if (dontroute) so->so_options |= SO_DONTROUTE; /* * Compute flags here, for pru_send and NKEs * * If the user set MSG_EOF, the protocol * understands this flag and nothing left to * send then use PRU_SEND_EOF instead of PRU_SEND. */ sendflags = (flags & MSG_OOB) ? PRUS_OOB : ((flags & MSG_EOF) && (so->so_proto->pr_flags & PR_IMPLOPCL) && (resid <= 0)) ? PRUS_EOF : /* If there is more to send set PRUS_MORETOCOME */ (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0; if ((flags & MSG_SKIPCFIL) == 0) { /* * Socket filter processing */ error = sflt_data_out(so, addr, &top, &control, (sendflags & MSG_OOB) ? sock_data_filt_flag_oob : 0); if (error) { if (error == EJUSTRETURN) { error = 0; clen = 0; control = NULL; top = NULL; } goto release; } #if CONTENT_FILTER /* * Content filter processing */ error = cfil_sock_data_out(so, addr, top, control, (sendflags & MSG_OOB) ? sock_data_filt_flag_oob : 0); if (error) { if (error == EJUSTRETURN) { error = 0; clen = 0; control = NULL; top = NULL; } goto release; } #endif /* CONTENT_FILTER */ } if (so->so_flags & SOF_ENABLE_MSGS) { /* * Make a copy of control mbuf, * so that msg priority can be * passed to subsequent mbufs. */ control_copy = m_dup(control, M_NOWAIT); } error = (*so->so_proto->pr_usrreqs->pru_send) (so, sendflags, top, addr, control, p); if (flags & MSG_SEND) so->so_temp = NULL; if (dontroute) so->so_options &= ~SO_DONTROUTE; clen = 0; control = control_copy; control_copy = NULL; top = NULL; mp = ⊤ if (error) goto release; } while (resid && space > 0); } while (resid); release: if (sblocked) sbunlock(&so->so_snd, FALSE); /* will unlock socket */ else socket_unlock(so, 1); out: if (top != NULL) m_freem(top); if (control != NULL) m_freem(control); if (freelist != NULL) m_freem_list(freelist); if (control_copy != NULL) m_freem(control_copy); /* * One write has been done. This was enough. Get back to "normal" * behavior. */ if (so->so_flags1 & SOF1_PRECONNECT_DATA) so->so_flags1 &= ~SOF1_PRECONNECT_DATA; if (en_tracing) { /* resid passed here is the bytes left in uio */ KERNEL_ENERGYTRACE(kEnTrActKernSockWrite, DBG_FUNC_END, VM_KERNEL_ADDRPERM(so), ((error == EWOULDBLOCK) ? kEnTrFlagNoWork : 0), (int64_t)(orig_resid - resid)); } KERNEL_DEBUG(DBG_FNC_SOSEND | DBG_FUNC_END, so, resid, so->so_snd.sb_cc, space, error); return (error); } /* * Supported only connected sockets (no address) without ancillary data * (control mbuf) for atomic protocols */ int sosend_list(struct socket *so, struct uio **uioarray, u_int uiocnt, int flags) { struct mbuf *m, *freelist = NULL; user_ssize_t len, resid; int error, dontroute, mlen; int atomic = sosendallatonce(so); int sblocked = 0; struct proc *p = current_proc(); u_int uiofirst = 0; u_int uiolast = 0; struct mbuf *top = NULL; uint16_t headroom = 0; boolean_t bigcl; KERNEL_DEBUG((DBG_FNC_SOSEND_LIST | DBG_FUNC_START), so, uiocnt, so->so_snd.sb_cc, so->so_snd.sb_lowat, so->so_snd.sb_hiwat); if (so->so_type != SOCK_DGRAM) { error = EINVAL; goto out; } if (atomic == 0) { error = EINVAL; goto out; } if (so->so_proto->pr_usrreqs->pru_send_list == NULL) { error = EPROTONOSUPPORT; goto out; } if (flags & ~(MSG_DONTWAIT | MSG_NBIO)) { error = EINVAL; goto out; } resid = uio_array_resid(uioarray, uiocnt); /* * In theory resid should be unsigned. * However, space must be signed, as it might be less than 0 * if we over-committed, and we must use a signed comparison * of space and resid. On the other hand, a negative resid * causes us to loop sending 0-length segments to the protocol. * * Note: We limit resid to be a positive int value as we use * imin() to set bytes_to_copy -- radr://14558484 */ if (resid < 0 || resid > INT_MAX) { error = EINVAL; goto out; } socket_lock(so, 1); so_update_last_owner_locked(so, p); so_update_policy(so); #if NECP so_update_necp_policy(so, NULL, NULL); #endif /* NECP */ dontroute = (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && (so->so_proto->pr_flags & PR_ATOMIC); OSIncrementAtomicLong(&p->p_stats->p_ru.ru_msgsnd); error = sosendcheck(so, NULL, resid, 0, atomic, flags, &sblocked, NULL); if (error) goto release; /* * Use big 4 KB clusters when the outgoing interface does not prefer * 2 KB clusters */ bigcl = !(so->so_flags1 & SOF1_IF_2KCL) || sosendbigcl_ignore_capab; if (soreserveheadroom != 0) headroom = so->so_pktheadroom; do { int i; int num_needed = 0; int chainlength; size_t maxpktlen = 0; int bytes_to_alloc; if (sosendminchain > 0) chainlength = 0; else chainlength = sosendmaxchain; socket_unlock(so, 0); /* * Find a set of uio that fit in a reasonable number * of mbuf packets */ for (i = uiofirst; i < uiocnt; i++) { struct uio *auio = uioarray[i]; len = uio_resid(auio); /* Do nothing for empty messages */ if (len == 0) continue; num_needed += 1; uiolast += 1; if (len > maxpktlen) maxpktlen = len; chainlength += len; if (chainlength > sosendmaxchain) break; } /* * Nothing left to send */ if (num_needed == 0) { socket_lock(so, 0); break; } /* * Allocate buffer large enough to include headroom space for * network and link header * */ bytes_to_alloc = maxpktlen + headroom; /* * Allocate a single contiguous buffer of the smallest available * size when possible */ if (bytes_to_alloc > MCLBYTES && bytes_to_alloc <= MBIGCLBYTES && bigcl) { freelist = m_getpackets_internal( (unsigned int *)&num_needed, num_needed, M_WAIT, 1, MBIGCLBYTES); } else if (bytes_to_alloc > _MHLEN && bytes_to_alloc <= MCLBYTES) { freelist = m_getpackets_internal( (unsigned int *)&num_needed, num_needed, M_WAIT, 1, MCLBYTES); } else { freelist = m_allocpacket_internal( (unsigned int *)&num_needed, bytes_to_alloc, NULL, M_WAIT, 1, 0); } if (freelist == NULL) { socket_lock(so, 0); error = ENOMEM; goto release; } /* * Copy each uio of the set into its own mbuf packet */ for (i = uiofirst, m = freelist; i < uiolast && m != NULL; i++) { int bytes_to_copy; struct mbuf *n; struct uio *auio = uioarray[i]; bytes_to_copy = uio_resid(auio); /* Do nothing for empty messages */ if (bytes_to_copy == 0) continue; /* * Leave headroom for protocol headers * in the first mbuf of the chain */ m->m_data += headroom; for (n = m; n != NULL; n = n->m_next) { if ((m->m_flags & M_EXT)) mlen = m->m_ext.ext_size - m_leadingspace(m); else if ((m->m_flags & M_PKTHDR)) mlen = MHLEN - m_leadingspace(m); else mlen = MLEN - m_leadingspace(m); len = imin(mlen, bytes_to_copy); /* * Note: uiomove() decrements the iovec * length */ error = uiomove(mtod(n, caddr_t), len, auio); if (error != 0) break; n->m_len = len; m->m_pkthdr.len += len; VERIFY(m->m_pkthdr.len <= maxpktlen); bytes_to_copy -= len; resid -= len; } if (m->m_pkthdr.len == 0) { printf( "%s:%d so %llx pkt %llx type %u len null\n", __func__, __LINE__, (uint64_t)DEBUG_KERNEL_ADDRPERM(so), (uint64_t)DEBUG_KERNEL_ADDRPERM(m), m->m_type); } if (error != 0) break; m = m->m_nextpkt; } socket_lock(so, 0); if (error) goto release; top = freelist; freelist = NULL; if (dontroute) so->so_options |= SO_DONTROUTE; if ((flags & MSG_SKIPCFIL) == 0) { struct mbuf **prevnextp = NULL; for (i = uiofirst, m = top; i < uiolast && m != NULL; i++) { struct mbuf *nextpkt = m->m_nextpkt; /* * Socket filter processing */ error = sflt_data_out(so, NULL, &m, NULL, 0); if (error != 0 && error != EJUSTRETURN) goto release; #if CONTENT_FILTER if (error == 0) { /* * Content filter processing */ error = cfil_sock_data_out(so, NULL, m, NULL, 0); if (error != 0 && error != EJUSTRETURN) goto release; } #endif /* CONTENT_FILTER */ /* * Remove packet from the list when * swallowed by a filter */ if (error == EJUSTRETURN) { error = 0; if (prevnextp != NULL) *prevnextp = nextpkt; else top = nextpkt; } m = nextpkt; if (m != NULL) prevnextp = &m->m_nextpkt; } } if (top != NULL) error = (*so->so_proto->pr_usrreqs->pru_send_list) (so, 0, top, NULL, NULL, p); if (dontroute) so->so_options &= ~SO_DONTROUTE; top = NULL; uiofirst = uiolast; } while (resid > 0 && error == 0); release: if (sblocked) sbunlock(&so->so_snd, FALSE); /* will unlock socket */ else socket_unlock(so, 1); out: if (top != NULL) m_freem(top); if (freelist != NULL) m_freem_list(freelist); KERNEL_DEBUG(DBG_FNC_SOSEND_LIST | DBG_FUNC_END, so, resid, so->so_snd.sb_cc, 0, error); return (error); } /* * May return ERESTART when packet is dropped by MAC policy check */ static int soreceive_addr(struct proc *p, struct socket *so, struct sockaddr **psa, int flags, struct mbuf **mp, struct mbuf **nextrecordp, int canwait) { int error = 0; struct mbuf *m = *mp; struct mbuf *nextrecord = *nextrecordp; KASSERT(m->m_type == MT_SONAME, ("receive 1a")); #if CONFIG_MACF_SOCKET_SUBSET /* * Call the MAC framework for policy checking if we're in * the user process context and the socket isn't connected. */ if (p != kernproc && !(so->so_state & SS_ISCONNECTED)) { struct mbuf *m0 = m; /* * Dequeue this record (temporarily) from the receive * list since we're about to drop the socket's lock * where a new record may arrive and be appended to * the list. Upon MAC policy failure, the record * will be freed. Otherwise, we'll add it back to * the head of the list. We cannot rely on SB_LOCK * because append operation uses the socket's lock. */ do { m->m_nextpkt = NULL; sbfree(&so->so_rcv, m); m = m->m_next; } while (m != NULL); m = m0; so->so_rcv.sb_mb = nextrecord; SB_EMPTY_FIXUP(&so->so_rcv); SBLASTRECORDCHK(&so->so_rcv, "soreceive 1a"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 1a"); socket_unlock(so, 0); if (mac_socket_check_received(proc_ucred(p), so, mtod(m, struct sockaddr *)) != 0) { /* * MAC policy failure; free this record and * process the next record (or block until * one is available). We have adjusted sb_cc * and sb_mbcnt above so there is no need to * call sbfree() again. */ m_freem(m); /* * Clear SB_LOCK but don't unlock the socket. * Process the next record or wait for one. */ socket_lock(so, 0); sbunlock(&so->so_rcv, TRUE); /* stay locked */ error = ERESTART; goto done; } socket_lock(so, 0); /* * If the socket has been defunct'd, drop it. */ if (so->so_flags & SOF_DEFUNCT) { m_freem(m); error = ENOTCONN; goto done; } /* * Re-adjust the socket receive list and re-enqueue * the record in front of any packets which may have * been appended while we dropped the lock. */ for (m = m0; m->m_next != NULL; m = m->m_next) sballoc(&so->so_rcv, m); sballoc(&so->so_rcv, m); if (so->so_rcv.sb_mb == NULL) { so->so_rcv.sb_lastrecord = m0; so->so_rcv.sb_mbtail = m; } m = m0; nextrecord = m->m_nextpkt = so->so_rcv.sb_mb; so->so_rcv.sb_mb = m; SBLASTRECORDCHK(&so->so_rcv, "soreceive 1b"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 1b"); } #endif /* CONFIG_MACF_SOCKET_SUBSET */ if (psa != NULL) { *psa = dup_sockaddr(mtod(m, struct sockaddr *), canwait); if ((*psa == NULL) && (flags & MSG_NEEDSA)) { error = EWOULDBLOCK; goto done; } } if (flags & MSG_PEEK) { m = m->m_next; } else { sbfree(&so->so_rcv, m); if (m->m_next == NULL && so->so_rcv.sb_cc != 0) { panic("%s: about to create invalid socketbuf", __func__); /* NOTREACHED */ } MFREE(m, so->so_rcv.sb_mb); m = so->so_rcv.sb_mb; if (m != NULL) { m->m_nextpkt = nextrecord; } else { so->so_rcv.sb_mb = nextrecord; SB_EMPTY_FIXUP(&so->so_rcv); } } done: *mp = m; *nextrecordp = nextrecord; return (error); } /* * Process one or more MT_CONTROL mbufs present before any data mbufs * in the first mbuf chain on the socket buffer. If MSG_PEEK, we * just copy the data; if !MSG_PEEK, we call into the protocol to * perform externalization. */ static int soreceive_ctl(struct socket *so, struct mbuf **controlp, int flags, struct mbuf **mp, struct mbuf **nextrecordp) { int error = 0; struct mbuf *cm = NULL, *cmn; struct mbuf **cme = &cm; struct sockbuf *sb_rcv = &so->so_rcv; struct mbuf **msgpcm = NULL; struct mbuf *m = *mp; struct mbuf *nextrecord = *nextrecordp; struct protosw *pr = so->so_proto; /* * Externalizing the control messages would require us to * drop the socket's lock below. Once we re-acquire the * lock, the mbuf chain might change. In order to preserve * consistency, we unlink all control messages from the * first mbuf chain in one shot and link them separately * onto a different chain. */ do { if (flags & MSG_PEEK) { if (controlp != NULL) { if (*controlp == NULL) { msgpcm = controlp; } *controlp = m_copy(m, 0, m->m_len); /* * If we failed to allocate an mbuf, * release any previously allocated * mbufs for control data. Return * an error. Keep the mbufs in the * socket as this is using * MSG_PEEK flag. */ if (*controlp == NULL) { m_freem(*msgpcm); error = ENOBUFS; goto done; } controlp = &(*controlp)->m_next; } m = m->m_next; } else { m->m_nextpkt = NULL; sbfree(sb_rcv, m); sb_rcv->sb_mb = m->m_next; m->m_next = NULL; *cme = m; cme = &(*cme)->m_next; m = sb_rcv->sb_mb; } } while (m != NULL && m->m_type == MT_CONTROL); if (!(flags & MSG_PEEK)) { if (sb_rcv->sb_mb != NULL) { sb_rcv->sb_mb->m_nextpkt = nextrecord; } else { sb_rcv->sb_mb = nextrecord; SB_EMPTY_FIXUP(sb_rcv); } if (nextrecord == NULL) sb_rcv->sb_lastrecord = m; } SBLASTRECORDCHK(&so->so_rcv, "soreceive ctl"); SBLASTMBUFCHK(&so->so_rcv, "soreceive ctl"); while (cm != NULL) { int cmsg_type; cmn = cm->m_next; cm->m_next = NULL; cmsg_type = mtod(cm, struct cmsghdr *)->cmsg_type; /* * Call the protocol to externalize SCM_RIGHTS message * and return the modified message to the caller upon * success. Otherwise, all other control messages are * returned unmodified to the caller. Note that we * only get into this loop if MSG_PEEK is not set. */ if (pr->pr_domain->dom_externalize != NULL && cmsg_type == SCM_RIGHTS) { /* * Release socket lock: see 3903171. This * would also allow more records to be appended * to the socket buffer. We still have SB_LOCK * set on it, so we can be sure that the head * of the mbuf chain won't change. */ socket_unlock(so, 0); error = (*pr->pr_domain->dom_externalize)(cm); socket_lock(so, 0); } else { error = 0; } if (controlp != NULL && error == 0) { *controlp = cm; controlp = &(*controlp)->m_next; } else { (void) m_free(cm); } cm = cmn; } /* * Update the value of nextrecord in case we received new * records when the socket was unlocked above for * externalizing SCM_RIGHTS. */ if (m != NULL) nextrecord = sb_rcv->sb_mb->m_nextpkt; else nextrecord = sb_rcv->sb_mb; done: *mp = m; *nextrecordp = nextrecord; return (error); } /* * Implement receive operations on a socket. * We depend on the way that records are added to the sockbuf * by sbappend*. In particular, each record (mbufs linked through m_next) * must begin with an address if the protocol so specifies, * followed by an optional mbuf or mbufs containing ancillary data, * and then zero or more mbufs of data. * In order to avoid blocking network interrupts for the entire time here, * we splx() while doing the actual copy to user space. * Although the sockbuf is locked, new data may still be appended, * and thus we must maintain consistency of the sockbuf during that time. * * The caller may receive the data as a single mbuf chain by supplying * an mbuf **mp0 for use in returning the chain. The uio is then used * only for the count in uio_resid. * * Returns: 0 Success * ENOBUFS * ENOTCONN * EWOULDBLOCK * uiomove:EFAULT * sblock:EWOULDBLOCK * sblock:EINTR * sbwait:EBADF * sbwait:EINTR * sodelayed_copy:EFAULT * <pru_rcvoob>:EINVAL[TCP] * <pru_rcvoob>:EWOULDBLOCK[TCP] * <pru_rcvoob>:??? * <pr_domain->dom_externalize>:EMSGSIZE[AF_UNIX] * <pr_domain->dom_externalize>:ENOBUFS[AF_UNIX] * <pr_domain->dom_externalize>:??? * * Notes: Additional return values from calls through <pru_rcvoob> and * <pr_domain->dom_externalize> depend on protocols other than * TCP or AF_UNIX, which are documented above. */ int soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp) { struct mbuf *m, **mp, *ml = NULL; struct mbuf *nextrecord, *free_list; int flags, error, offset; user_ssize_t len; struct protosw *pr = so->so_proto; int moff, type = 0; user_ssize_t orig_resid = uio_resid(uio); user_ssize_t delayed_copy_len; int can_delay; int need_event; struct proc *p = current_proc(); boolean_t en_tracing = FALSE; /* * Sanity check on the length passed by caller as we are making 'int' * comparisons */ if (orig_resid < 0 || orig_resid > INT_MAX) return (EINVAL); KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_START, so, uio_resid(uio), so->so_rcv.sb_cc, so->so_rcv.sb_lowat, so->so_rcv.sb_hiwat); socket_lock(so, 1); so_update_last_owner_locked(so, p); so_update_policy(so); #ifdef MORE_LOCKING_DEBUG if (so->so_usecount == 1) { panic("%s: so=%x no other reference on socket\n", __func__, so); /* NOTREACHED */ } #endif mp = mp0; if (psa != NULL) *psa = NULL; if (controlp != NULL) *controlp = NULL; if (flagsp != NULL) flags = *flagsp &~ MSG_EOR; else flags = 0; /* * If a recv attempt is made on a previously-accepted socket * that has been marked as inactive (disconnected), reject * the request. */ if (so->so_flags & SOF_DEFUNCT) { struct sockbuf *sb = &so->so_rcv; error = ENOTCONN; SODEFUNCTLOG(("%s[%d]: defunct so 0x%llx [%d,%d] (%d)\n", __func__, proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), error)); /* * This socket should have been disconnected and flushed * prior to being returned from sodefunct(); there should * be no data on its receive list, so panic otherwise. */ if (so->so_state & SS_DEFUNCT) sb_empty_assert(sb, __func__); socket_unlock(so, 1); return (error); } if ((so->so_flags1 & SOF1_PRECONNECT_DATA) && pr->pr_usrreqs->pru_preconnect) { /* * A user may set the CONNECT_RESUME_ON_READ_WRITE-flag but not * calling write() right after this. *If* the app calls a read * we do not want to block this read indefinetely. Thus, * we trigger a connect so that the session gets initiated. */ error = (*pr->pr_usrreqs->pru_preconnect)(so); if (error) { socket_unlock(so, 1); return (error); } } if (ENTR_SHOULDTRACE && (SOCK_CHECK_DOM(so, AF_INET) || SOCK_CHECK_DOM(so, AF_INET6))) { /* * enable energy tracing for inet sockets that go over * non-loopback interfaces only. */ struct inpcb *inp = sotoinpcb(so); if (inp->inp_last_outifp != NULL && !(inp->inp_last_outifp->if_flags & IFF_LOOPBACK)) { en_tracing = TRUE; KERNEL_ENERGYTRACE(kEnTrActKernSockRead, DBG_FUNC_START, VM_KERNEL_ADDRPERM(so), ((so->so_state & SS_NBIO) ? kEnTrFlagNonBlocking : 0), (int64_t)orig_resid); } } /* * When SO_WANTOOBFLAG is set we try to get out-of-band data * regardless of the flags argument. Here is the case were * out-of-band data is not inline. */ if ((flags & MSG_OOB) || ((so->so_options & SO_WANTOOBFLAG) != 0 && (so->so_options & SO_OOBINLINE) == 0 && (so->so_oobmark || (so->so_state & SS_RCVATMARK)))) { m = m_get(M_WAIT, MT_DATA); if (m == NULL) { socket_unlock(so, 1); KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END, ENOBUFS, 0, 0, 0, 0); return (ENOBUFS); } error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK); if (error) goto bad; socket_unlock(so, 0); do { error = uiomove(mtod(m, caddr_t), imin(uio_resid(uio), m->m_len), uio); m = m_free(m); } while (uio_resid(uio) && error == 0 && m != NULL); socket_lock(so, 0); bad: if (m != NULL) m_freem(m); if ((so->so_options & SO_WANTOOBFLAG) != 0) { if (error == EWOULDBLOCK || error == EINVAL) { /* * Let's try to get normal data: * EWOULDBLOCK: out-of-band data not * receive yet. EINVAL: out-of-band data * already read. */ error = 0; goto nooob; } else if (error == 0 && flagsp != NULL) { *flagsp |= MSG_OOB; } } socket_unlock(so, 1); if (en_tracing) { KERNEL_ENERGYTRACE(kEnTrActKernSockRead, DBG_FUNC_END, VM_KERNEL_ADDRPERM(so), 0, (int64_t)(orig_resid - uio_resid(uio))); } KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END, error, 0, 0, 0, 0); return (error); } nooob: if (mp != NULL) *mp = NULL; if (so->so_state & SS_ISCONFIRMING && uio_resid(uio)) { (*pr->pr_usrreqs->pru_rcvd)(so, 0); } free_list = NULL; delayed_copy_len = 0; restart: #ifdef MORE_LOCKING_DEBUG if (so->so_usecount <= 1) printf("soreceive: sblock so=0x%llx ref=%d on socket\n", (uint64_t)DEBUG_KERNEL_ADDRPERM(so), so->so_usecount); #endif /* * See if the socket has been closed (SS_NOFDREF|SS_CANTRCVMORE) * and if so just return to the caller. This could happen when * soreceive() is called by a socket upcall function during the * time the socket is freed. The socket buffer would have been * locked across the upcall, therefore we cannot put this thread * to sleep (else we will deadlock) or return EWOULDBLOCK (else * we may livelock), because the lock on the socket buffer will * only be released when the upcall routine returns to its caller. * Because the socket has been officially closed, there can be * no further read on it. * * A multipath subflow socket would have its SS_NOFDREF set by * default, so check for SOF_MP_SUBFLOW socket flag; when the * socket is closed for real, SOF_MP_SUBFLOW would be cleared. */ if ((so->so_state & (SS_NOFDREF | SS_CANTRCVMORE)) == (SS_NOFDREF | SS_CANTRCVMORE) && !(so->so_flags & SOF_MP_SUBFLOW)) { socket_unlock(so, 1); return (0); } error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); if (error) { socket_unlock(so, 1); KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END, error, 0, 0, 0, 0); if (en_tracing) { KERNEL_ENERGYTRACE(kEnTrActKernSockRead, DBG_FUNC_END, VM_KERNEL_ADDRPERM(so), 0, (int64_t)(orig_resid - uio_resid(uio))); } return (error); } m = so->so_rcv.sb_mb; /* * If we have less data than requested, block awaiting more * (subject to any timeout) if: * 1. the current count is less than the low water mark, or * 2. MSG_WAITALL is set, and it is possible to do the entire * receive operation at once if we block (resid <= hiwat). * 3. MSG_DONTWAIT is not set * If MSG_WAITALL is set but resid is larger than the receive buffer, * we have to do the receive in sections, and thus risk returning * a short count if a timeout or signal occurs after we start. */ if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && so->so_rcv.sb_cc < uio_resid(uio)) && (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || ((flags & MSG_WAITALL) && uio_resid(uio) <= so->so_rcv.sb_hiwat)) && m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) { /* * Panic if we notice inconsistencies in the socket's * receive list; both sb_mb and sb_cc should correctly * reflect the contents of the list, otherwise we may * end up with false positives during select() or poll() * which could put the application in a bad state. */ SB_MB_CHECK(&so->so_rcv); if (so->so_error) { if (m != NULL) goto dontblock; error = so->so_error; if ((flags & MSG_PEEK) == 0) so->so_error = 0; goto release; } if (so->so_state & SS_CANTRCVMORE) { #if CONTENT_FILTER /* * Deal with half closed connections */ if ((so->so_state & SS_ISDISCONNECTED) == 0 && cfil_sock_data_pending(&so->so_rcv) != 0) CFIL_LOG(LOG_INFO, "so %llx ignore SS_CANTRCVMORE", (uint64_t)DEBUG_KERNEL_ADDRPERM(so)); else #endif /* CONTENT_FILTER */ if (m != NULL) goto dontblock; else goto release; } for (; m != NULL; m = m->m_next) if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { m = so->so_rcv.sb_mb; goto dontblock; } if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && (so->so_proto->pr_flags & PR_CONNREQUIRED)) { error = ENOTCONN; goto release; } if (uio_resid(uio) == 0) goto release; if ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO))) { error = EWOULDBLOCK; goto release; } SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); sbunlock(&so->so_rcv, TRUE); /* keep socket locked */ #if EVEN_MORE_LOCKING_DEBUG if (socket_debug) printf("Waiting for socket data\n"); #endif error = sbwait(&so->so_rcv); #if EVEN_MORE_LOCKING_DEBUG if (socket_debug) printf("SORECEIVE - sbwait returned %d\n", error); #endif if (so->so_usecount < 1) { panic("%s: after 2nd sblock so=%p ref=%d on socket\n", __func__, so, so->so_usecount); /* NOTREACHED */ } if (error) { socket_unlock(so, 1); KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END, error, 0, 0, 0, 0); if (en_tracing) { KERNEL_ENERGYTRACE(kEnTrActKernSockRead, DBG_FUNC_END, VM_KERNEL_ADDRPERM(so), 0, (int64_t)(orig_resid - uio_resid(uio))); } return (error); } goto restart; } dontblock: OSIncrementAtomicLong(&p->p_stats->p_ru.ru_msgrcv); SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 1"); nextrecord = m->m_nextpkt; if ((pr->pr_flags & PR_ADDR) && m->m_type == MT_SONAME) { error = soreceive_addr(p, so, psa, flags, &m, &nextrecord, mp0 == NULL); if (error == ERESTART) goto restart; else if (error != 0) goto release; orig_resid = 0; } /* * Process one or more MT_CONTROL mbufs present before any data mbufs * in the first mbuf chain on the socket buffer. If MSG_PEEK, we * just copy the data; if !MSG_PEEK, we call into the protocol to * perform externalization. */ if (m != NULL && m->m_type == MT_CONTROL) { error = soreceive_ctl(so, controlp, flags, &m, &nextrecord); if (error != 0) goto release; orig_resid = 0; } /* * If the socket is a TCP socket with message delivery * enabled, then create a control msg to deliver the * relative TCP sequence number for this data. Waiting * until this point will protect against failures to * allocate an mbuf for control msgs. */ if (so->so_type == SOCK_STREAM && SOCK_PROTO(so) == IPPROTO_TCP && (so->so_flags & SOF_ENABLE_MSGS) && controlp != NULL) { struct mbuf *seq_cm; seq_cm = sbcreatecontrol((caddr_t)&m->m_pkthdr.msg_seq, sizeof (uint32_t), SCM_SEQNUM, SOL_SOCKET); if (seq_cm == NULL) { /* unable to allocate a control mbuf */ error = ENOBUFS; goto release; } *controlp = seq_cm; controlp = &seq_cm->m_next; } if (m != NULL) { if (!(flags & MSG_PEEK)) { /* * We get here because m points to an mbuf following * any MT_SONAME or MT_CONTROL mbufs which have been * processed above. In any case, m should be pointing * to the head of the mbuf chain, and the nextrecord * should be either NULL or equal to m->m_nextpkt. * See comments above about SB_LOCK. */ if (m != so->so_rcv.sb_mb || m->m_nextpkt != nextrecord) { panic("%s: post-control !sync so=%p m=%p " "nextrecord=%p\n", __func__, so, m, nextrecord); /* NOTREACHED */ } if (nextrecord == NULL) so->so_rcv.sb_lastrecord = m; } type = m->m_type; if (type == MT_OOBDATA) flags |= MSG_OOB; } else { if (!(flags & MSG_PEEK)) { SB_EMPTY_FIXUP(&so->so_rcv); } } SBLASTRECORDCHK(&so->so_rcv, "soreceive 2"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 2"); moff = 0; offset = 0; if (!(flags & MSG_PEEK) && uio_resid(uio) > sorecvmincopy) can_delay = 1; else can_delay = 0; need_event = 0; while (m != NULL && (uio_resid(uio) - delayed_copy_len) > 0 && error == 0) { if (m->m_type == MT_OOBDATA) { if (type != MT_OOBDATA) break; } else if (type == MT_OOBDATA) { break; } /* * Make sure to allways set MSG_OOB event when getting * out of band data inline. */ if ((so->so_options & SO_WANTOOBFLAG) != 0 && (so->so_options & SO_OOBINLINE) != 0 && (so->so_state & SS_RCVATMARK) != 0) { flags |= MSG_OOB; } so->so_state &= ~SS_RCVATMARK; len = uio_resid(uio) - delayed_copy_len; if (so->so_oobmark && len > so->so_oobmark - offset) len = so->so_oobmark - offset; if (len > m->m_len - moff) len = m->m_len - moff; /* * If mp is set, just pass back the mbufs. * Otherwise copy them out via the uio, then free. * Sockbuf must be consistent here (points to current mbuf, * it points to next record) when we drop priority; * we must note any additions to the sockbuf when we * block interrupts again. */ if (mp == NULL) { SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove"); SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); if (can_delay && len == m->m_len) { /* * only delay the copy if we're consuming the * mbuf and we're NOT in MSG_PEEK mode * and we have enough data to make it worthwile * to drop and retake the lock... can_delay * reflects the state of the 2 latter * constraints moff should always be zero * in these cases */ delayed_copy_len += len; } else { if (delayed_copy_len) { error = sodelayed_copy(so, uio, &free_list, &delayed_copy_len); if (error) { goto release; } /* * can only get here if MSG_PEEK is not * set therefore, m should point at the * head of the rcv queue; if it doesn't, * it means something drastically * changed while we were out from behind * the lock in sodelayed_copy. perhaps * a RST on the stream. in any event, * the stream has been interrupted. it's * probably best just to return whatever * data we've moved and let the caller * sort it out... */ if (m != so->so_rcv.sb_mb) { break; } } socket_unlock(so, 0); error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio); socket_lock(so, 0); if (error) goto release; } } else { uio_setresid(uio, (uio_resid(uio) - len)); } if (len == m->m_len - moff) { if (m->m_flags & M_EOR) flags |= MSG_EOR; if (flags & MSG_PEEK) { m = m->m_next; moff = 0; } else { nextrecord = m->m_nextpkt; sbfree(&so->so_rcv, m); m->m_nextpkt = NULL; /* * If this packet is an unordered packet * (indicated by M_UNORDERED_DATA flag), remove * the additional bytes added to the * receive socket buffer size. */ if ((so->so_flags & SOF_ENABLE_MSGS) && m->m_len && (m->m_flags & M_UNORDERED_DATA) && sbreserve(&so->so_rcv, so->so_rcv.sb_hiwat - m->m_len)) { if (so->so_msg_state->msg_uno_bytes > m->m_len) { so->so_msg_state-> msg_uno_bytes -= m->m_len; } else { so->so_msg_state-> msg_uno_bytes = 0; } m->m_flags &= ~M_UNORDERED_DATA; } if (mp != NULL) { *mp = m; mp = &m->m_next; so->so_rcv.sb_mb = m = m->m_next; *mp = NULL; } else { if (free_list == NULL) free_list = m; else ml->m_next = m; ml = m; so->so_rcv.sb_mb = m = m->m_next; ml->m_next = NULL; } if (m != NULL) { m->m_nextpkt = nextrecord; if (nextrecord == NULL) so->so_rcv.sb_lastrecord = m; } else { so->so_rcv.sb_mb = nextrecord; SB_EMPTY_FIXUP(&so->so_rcv); } SBLASTRECORDCHK(&so->so_rcv, "soreceive 3"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 3"); } } else { if (flags & MSG_PEEK) { moff += len; } else { if (mp != NULL) { int copy_flag; if (flags & MSG_DONTWAIT) copy_flag = M_DONTWAIT; else copy_flag = M_WAIT; *mp = m_copym(m, 0, len, copy_flag); /* * Failed to allocate an mbuf? * Adjust uio_resid back, it was * adjusted down by len bytes which * we didn't copy over. */ if (*mp == NULL) { uio_setresid(uio, (uio_resid(uio) + len)); break; } } m->m_data += len; m->m_len -= len; so->so_rcv.sb_cc -= len; } } if (so->so_oobmark) { if ((flags & MSG_PEEK) == 0) { so->so_oobmark -= len; if (so->so_oobmark == 0) { so->so_state |= SS_RCVATMARK; /* * delay posting the actual event until * after any delayed copy processing * has finished */ need_event = 1; break; } } else { offset += len; if (offset == so->so_oobmark) break; } } if (flags & MSG_EOR) break; /* * If the MSG_WAITALL or MSG_WAITSTREAM flag is set * (for non-atomic socket), we must not quit until * "uio->uio_resid == 0" or an error termination. * If a signal/timeout occurs, return with a short * count but without error. Keep sockbuf locked * against other readers. */ while (flags & (MSG_WAITALL|MSG_WAITSTREAM) && m == NULL && (uio_resid(uio) - delayed_copy_len) > 0 && !sosendallatonce(so) && !nextrecord) { if (so->so_error || ((so->so_state & SS_CANTRCVMORE) #if CONTENT_FILTER && cfil_sock_data_pending(&so->so_rcv) == 0 #endif /* CONTENT_FILTER */ )) goto release; /* * Depending on the protocol (e.g. TCP), the following * might cause the socket lock to be dropped and later * be reacquired, and more data could have arrived and * have been appended to the receive socket buffer by * the time it returns. Therefore, we only sleep in * sbwait() below if and only if the socket buffer is * empty, in order to avoid a false sleep. */ if (pr->pr_flags & PR_WANTRCVD && so->so_pcb && (((struct inpcb *)so->so_pcb)->inp_state != INPCB_STATE_DEAD)) (*pr->pr_usrreqs->pru_rcvd)(so, flags); SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); if (so->so_rcv.sb_mb == NULL && sbwait(&so->so_rcv)) { error = 0; goto release; } /* * have to wait until after we get back from the sbwait * to do the copy because we will drop the lock if we * have enough data that has been delayed... by dropping * the lock we open up a window allowing the netisr * thread to process the incoming packets and to change * the state of this socket... we're issuing the sbwait * because the socket is empty and we're expecting the * netisr thread to wake us up when more packets arrive; * if we allow that processing to happen and then sbwait * we could stall forever with packets sitting in the * socket if no further packets arrive from the remote * side. * * we want to copy before we've collected all the data * to satisfy this request to allow the copy to overlap * the incoming packet processing on an MP system */ if (delayed_copy_len > sorecvmincopy && (delayed_copy_len > (so->so_rcv.sb_hiwat / 2))) { error = sodelayed_copy(so, uio, &free_list, &delayed_copy_len); if (error) goto release; } m = so->so_rcv.sb_mb; if (m != NULL) { nextrecord = m->m_nextpkt; } SB_MB_CHECK(&so->so_rcv); } } #ifdef MORE_LOCKING_DEBUG if (so->so_usecount <= 1) { panic("%s: after big while so=%p ref=%d on socket\n", __func__, so, so->so_usecount); /* NOTREACHED */ } #endif if (m != NULL && pr->pr_flags & PR_ATOMIC) { if (so->so_options & SO_DONTTRUNC) { flags |= MSG_RCVMORE; } else { flags |= MSG_TRUNC; if ((flags & MSG_PEEK) == 0) (void) sbdroprecord(&so->so_rcv); } } /* * pru_rcvd below (for TCP) may cause more data to be received * if the socket lock is dropped prior to sending the ACK; some * legacy OpenTransport applications don't handle this well * (if it receives less data than requested while MSG_HAVEMORE * is set), and so we set the flag now based on what we know * prior to calling pru_rcvd. */ if ((so->so_options & SO_WANTMORE) && so->so_rcv.sb_cc > 0) flags |= MSG_HAVEMORE; if ((flags & MSG_PEEK) == 0) { if (m == NULL) { so->so_rcv.sb_mb = nextrecord; /* * First part is an inline SB_EMPTY_FIXUP(). Second * part makes sure sb_lastrecord is up-to-date if * there is still data in the socket buffer. */ if (so->so_rcv.sb_mb == NULL) { so->so_rcv.sb_mbtail = NULL; so->so_rcv.sb_lastrecord = NULL; } else if (nextrecord->m_nextpkt == NULL) { so->so_rcv.sb_lastrecord = nextrecord; } SB_MB_CHECK(&so->so_rcv); } SBLASTRECORDCHK(&so->so_rcv, "soreceive 4"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) (*pr->pr_usrreqs->pru_rcvd)(so, flags); } if (delayed_copy_len) { error = sodelayed_copy(so, uio, &free_list, &delayed_copy_len); if (error) goto release; } if (free_list != NULL) { m_freem_list(free_list); free_list = NULL; } if (need_event) postevent(so, 0, EV_OOB); if (orig_resid == uio_resid(uio) && orig_resid && (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { sbunlock(&so->so_rcv, TRUE); /* keep socket locked */ goto restart; } if (flagsp != NULL) *flagsp |= flags; release: #ifdef MORE_LOCKING_DEBUG if (so->so_usecount <= 1) { panic("%s: release so=%p ref=%d on socket\n", __func__, so, so->so_usecount); /* NOTREACHED */ } #endif if (delayed_copy_len) error = sodelayed_copy(so, uio, &free_list, &delayed_copy_len); if (free_list != NULL) m_freem_list(free_list); sbunlock(&so->so_rcv, FALSE); /* will unlock socket */ if (en_tracing) { KERNEL_ENERGYTRACE(kEnTrActKernSockRead, DBG_FUNC_END, VM_KERNEL_ADDRPERM(so), ((error == EWOULDBLOCK) ? kEnTrFlagNoWork : 0), (int64_t)(orig_resid - uio_resid(uio))); } KERNEL_DEBUG(DBG_FNC_SORECEIVE | DBG_FUNC_END, so, uio_resid(uio), so->so_rcv.sb_cc, 0, error); return (error); } /* * Returns: 0 Success * uiomove:EFAULT */ static int sodelayed_copy(struct socket *so, struct uio *uio, struct mbuf **free_list, user_ssize_t *resid) { int error = 0; struct mbuf *m; m = *free_list; socket_unlock(so, 0); while (m != NULL && error == 0) { error = uiomove(mtod(m, caddr_t), (int)m->m_len, uio); m = m->m_next; } m_freem_list(*free_list); *free_list = NULL; *resid = 0; socket_lock(so, 0); return (error); } static int sodelayed_copy_list(struct socket *so, struct recv_msg_elem *msgarray, u_int uiocnt, struct mbuf **free_list, user_ssize_t *resid) { #pragma unused(so) int error = 0; struct mbuf *ml, *m; int i = 0; struct uio *auio; for (ml = *free_list, i = 0; ml != NULL && i < uiocnt; ml = ml->m_nextpkt, i++) { auio = msgarray[i].uio; for (m = ml; m != NULL; m = m->m_next) { error = uiomove(mtod(m, caddr_t), m->m_len, auio); if (error != 0) goto out; } } out: m_freem_list(*free_list); *free_list = NULL; *resid = 0; return (error); } int soreceive_list(struct socket *so, struct recv_msg_elem *msgarray, u_int uiocnt, int *flagsp) { struct mbuf *m; struct mbuf *nextrecord; struct mbuf *ml = NULL, *free_list = NULL, *free_tail = NULL; int error; user_ssize_t len, pktlen, delayed_copy_len = 0; struct protosw *pr = so->so_proto; user_ssize_t resid; struct proc *p = current_proc(); struct uio *auio = NULL; int npkts = 0; int sblocked = 0; struct sockaddr **psa = NULL; struct mbuf **controlp = NULL; int can_delay; int flags; struct mbuf *free_others = NULL; KERNEL_DEBUG(DBG_FNC_SORECEIVE_LIST | DBG_FUNC_START, so, uiocnt, so->so_rcv.sb_cc, so->so_rcv.sb_lowat, so->so_rcv.sb_hiwat); /* * Sanity checks: * - Only supports don't wait flags * - Only support datagram sockets (could be extended to raw) * - Must be atomic * - Protocol must support packet chains * - The uio array is NULL (should we panic?) */ if (flagsp != NULL) flags = *flagsp; else flags = 0; if (flags & ~(MSG_PEEK | MSG_WAITALL | MSG_DONTWAIT | MSG_NEEDSA | MSG_NBIO)) { printf("%s invalid flags 0x%x\n", __func__, flags); error = EINVAL; goto out; } if (so->so_type != SOCK_DGRAM) { error = EINVAL; goto out; } if (sosendallatonce(so) == 0) { error = EINVAL; goto out; } if (so->so_proto->pr_usrreqs->pru_send_list == NULL) { error = EPROTONOSUPPORT; goto out; } if (msgarray == NULL) { printf("%s uioarray is NULL\n", __func__); error = EINVAL; goto out; } if (uiocnt == 0) { printf("%s uiocnt is 0\n", __func__); error = EINVAL; goto out; } /* * Sanity check on the length passed by caller as we are making 'int' * comparisons */ resid = recv_msg_array_resid(msgarray, uiocnt); if (resid < 0 || resid > INT_MAX) { error = EINVAL; goto out; } if (!(flags & MSG_PEEK) && sorecvmincopy > 0) can_delay = 1; else can_delay = 0; socket_lock(so, 1); so_update_last_owner_locked(so, p); so_update_policy(so); #if NECP so_update_necp_policy(so, NULL, NULL); #endif /* NECP */ /* * If a recv attempt is made on a previously-accepted socket * that has been marked as inactive (disconnected), reject * the request. */ if (so->so_flags & SOF_DEFUNCT) { struct sockbuf *sb = &so->so_rcv; error = ENOTCONN; SODEFUNCTLOG(("%s[%d]: defunct so 0x%llx [%d,%d] (%d)\n", __func__, proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), error)); /* * This socket should have been disconnected and flushed * prior to being returned from sodefunct(); there should * be no data on its receive list, so panic otherwise. */ if (so->so_state & SS_DEFUNCT) sb_empty_assert(sb, __func__); goto release; } next: /* * The uio may be empty */ if (npkts >= uiocnt) { error = 0; goto release; } restart: /* * See if the socket has been closed (SS_NOFDREF|SS_CANTRCVMORE) * and if so just return to the caller. This could happen when * soreceive() is called by a socket upcall function during the * time the socket is freed. The socket buffer would have been * locked across the upcall, therefore we cannot put this thread * to sleep (else we will deadlock) or return EWOULDBLOCK (else * we may livelock), because the lock on the socket buffer will * only be released when the upcall routine returns to its caller. * Because the socket has been officially closed, there can be * no further read on it. */ if ((so->so_state & (SS_NOFDREF | SS_CANTRCVMORE)) == (SS_NOFDREF | SS_CANTRCVMORE)) { error = 0; goto release; } error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); if (error) { goto release; } sblocked = 1; m = so->so_rcv.sb_mb; /* * Block awaiting more datagram if needed */ if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || ((flags & MSG_WAITALL) && npkts < uiocnt))))) { /* * Panic if we notice inconsistencies in the socket's * receive list; both sb_mb and sb_cc should correctly * reflect the contents of the list, otherwise we may * end up with false positives during select() or poll() * which could put the application in a bad state. */ SB_MB_CHECK(&so->so_rcv); if (so->so_error) { error = so->so_error; if ((flags & MSG_PEEK) == 0) so->so_error = 0; goto release; } if (so->so_state & SS_CANTRCVMORE) { goto release; } if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && (so->so_proto->pr_flags & PR_CONNREQUIRED)) { error = ENOTCONN; goto release; } if ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO))) { error = EWOULDBLOCK; goto release; } /* * Do not block if we got some data */ if (free_list != NULL) { error = 0; goto release; } SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); sbunlock(&so->so_rcv, TRUE); /* keep socket locked */ sblocked = 0; error = sbwait(&so->so_rcv); if (error) { goto release; } goto restart; } OSIncrementAtomicLong(&p->p_stats->p_ru.ru_msgrcv); SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 1"); /* * Consume the current uio index as we have a datagram */ auio = msgarray[npkts].uio; resid = uio_resid(auio); msgarray[npkts].which |= SOCK_MSG_DATA; psa = (msgarray[npkts].which & SOCK_MSG_SA) ? &msgarray[npkts].psa : NULL; controlp = (msgarray[npkts].which & SOCK_MSG_CONTROL) ? &msgarray[npkts].controlp : NULL; npkts += 1; nextrecord = m->m_nextpkt; if ((pr->pr_flags & PR_ADDR) && m->m_type == MT_SONAME) { error = soreceive_addr(p, so, psa, flags, &m, &nextrecord, 1); if (error == ERESTART) goto restart; else if (error != 0) goto release; } if (m != NULL && m->m_type == MT_CONTROL) { error = soreceive_ctl(so, controlp, flags, &m, &nextrecord); if (error != 0) goto release; } if (m->m_pkthdr.len == 0) { printf("%s:%d so %llx pkt %llx type %u pktlen null\n", __func__, __LINE__, (uint64_t)DEBUG_KERNEL_ADDRPERM(so), (uint64_t)DEBUG_KERNEL_ADDRPERM(m), m->m_type); } /* * Loop to copy the mbufs of the current record * Support zero length packets */ ml = NULL; pktlen = 0; while (m != NULL && (len = resid - pktlen) >= 0 && error == 0) { if (m->m_len == 0) panic("%p m_len zero", m); if (m->m_type == 0) panic("%p m_type zero", m); /* * Clip to the residual length */ if (len > m->m_len) len = m->m_len; pktlen += len; /* * Copy the mbufs via the uio or delay the copy * Sockbuf must be consistent here (points to current mbuf, * it points to next record) when we drop priority; * we must note any additions to the sockbuf when we * block interrupts again. */ if (len > 0 && can_delay == 0) { socket_unlock(so, 0); error = uiomove(mtod(m, caddr_t), (int)len, auio); socket_lock(so, 0); if (error) goto release; } else { delayed_copy_len += len; } if (len == m->m_len) { /* * m was entirely copied */ sbfree(&so->so_rcv, m); nextrecord = m->m_nextpkt; m->m_nextpkt = NULL; /* * Set the first packet to the head of the free list */ if (free_list == NULL) free_list = m; /* * Link current packet to tail of free list */ if (ml == NULL) { if (free_tail != NULL) free_tail->m_nextpkt = m; free_tail = m; } /* * Link current mbuf to last mbuf of current packet */ if (ml != NULL) ml->m_next = m; ml = m; /* * Move next buf to head of socket buffer */ so->so_rcv.sb_mb = m = ml->m_next; ml->m_next = NULL; if (m != NULL) { m->m_nextpkt = nextrecord; if (nextrecord == NULL) so->so_rcv.sb_lastrecord = m; } else { so->so_rcv.sb_mb = nextrecord; SB_EMPTY_FIXUP(&so->so_rcv); } SBLASTRECORDCHK(&so->so_rcv, "soreceive 3"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 3"); } else { /* * Stop the loop on partial copy */ break; } } #ifdef MORE_LOCKING_DEBUG if (so->so_usecount <= 1) { panic("%s: after big while so=%llx ref=%d on socket\n", __func__, (uint64_t)DEBUG_KERNEL_ADDRPERM(so), so->so_usecount); /* NOTREACHED */ } #endif /* * Tell the caller we made a partial copy */ if (m != NULL) { if (so->so_options & SO_DONTTRUNC) { /* * Copyout first the freelist then the partial mbuf */ socket_unlock(so, 0); if (delayed_copy_len) error = sodelayed_copy_list(so, msgarray, uiocnt, &free_list, &delayed_copy_len); if (error == 0) { error = uiomove(mtod(m, caddr_t), (int)len, auio); } socket_lock(so, 0); if (error) goto release; m->m_data += len; m->m_len -= len; so->so_rcv.sb_cc -= len; flags |= MSG_RCVMORE; } else { (void) sbdroprecord(&so->so_rcv); nextrecord = so->so_rcv.sb_mb; m = NULL; flags |= MSG_TRUNC; } } if (m == NULL) { so->so_rcv.sb_mb = nextrecord; /* * First part is an inline SB_EMPTY_FIXUP(). Second * part makes sure sb_lastrecord is up-to-date if * there is still data in the socket buffer. */ if (so->so_rcv.sb_mb == NULL) { so->so_rcv.sb_mbtail = NULL; so->so_rcv.sb_lastrecord = NULL; } else if (nextrecord->m_nextpkt == NULL) { so->so_rcv.sb_lastrecord = nextrecord; } SB_MB_CHECK(&so->so_rcv); } SBLASTRECORDCHK(&so->so_rcv, "soreceive 4"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); /* * We can continue to the next packet as long as: * - We haven't exhausted the uio array * - There was no error * - A packet was not truncated * - We can still receive more data */ if (npkts < uiocnt && error == 0 && (flags & (MSG_RCVMORE | MSG_TRUNC)) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { sbunlock(&so->so_rcv, TRUE); /* keep socket locked */ sblocked = 0; goto next; } if (flagsp != NULL) *flagsp |= flags; release: /* * pru_rcvd may cause more data to be received if the socket lock * is dropped so we set MSG_HAVEMORE now based on what we know. * That way the caller won't be surprised if it receives less data * than requested. */ if ((so->so_options & SO_WANTMORE) && so->so_rcv.sb_cc > 0) flags |= MSG_HAVEMORE; if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) (*pr->pr_usrreqs->pru_rcvd)(so, flags); if (sblocked) sbunlock(&so->so_rcv, FALSE); /* will unlock socket */ else socket_unlock(so, 1); if (delayed_copy_len) error = sodelayed_copy_list(so, msgarray, uiocnt, &free_list, &delayed_copy_len); out: /* * Amortize the cost of freeing the mbufs */ if (free_list != NULL) m_freem_list(free_list); if (free_others != NULL) m_freem_list(free_others); KERNEL_DEBUG(DBG_FNC_SORECEIVE_LIST | DBG_FUNC_END, error, 0, 0, 0, 0); return (error); } /* * Returns: 0 Success * EINVAL * ENOTCONN * <pru_shutdown>:EINVAL * <pru_shutdown>:EADDRNOTAVAIL[TCP] * <pru_shutdown>:ENOBUFS[TCP] * <pru_shutdown>:EMSGSIZE[TCP] * <pru_shutdown>:EHOSTUNREACH[TCP] * <pru_shutdown>:ENETUNREACH[TCP] * <pru_shutdown>:ENETDOWN[TCP] * <pru_shutdown>:ENOMEM[TCP] * <pru_shutdown>:EACCES[TCP] * <pru_shutdown>:EMSGSIZE[TCP] * <pru_shutdown>:ENOBUFS[TCP] * <pru_shutdown>:???[TCP] [ignorable: mostly IPSEC/firewall/DLIL] * <pru_shutdown>:??? [other protocol families] */ int soshutdown(struct socket *so, int how) { int error; KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN | DBG_FUNC_START, how, 0, 0, 0, 0); switch (how) { case SHUT_RD: case SHUT_WR: case SHUT_RDWR: socket_lock(so, 1); if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING)) == 0) { error = ENOTCONN; } else { error = soshutdownlock(so, how); } socket_unlock(so, 1); break; default: error = EINVAL; break; } KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN | DBG_FUNC_END, how, error, 0, 0, 0); return (error); } int soshutdownlock_final(struct socket *so, int how) { struct protosw *pr = so->so_proto; int error = 0; sflt_notify(so, sock_evt_shutdown, &how); if (how != SHUT_WR) { if ((so->so_state & SS_CANTRCVMORE) != 0) { /* read already shut down */ error = ENOTCONN; goto done; } sorflush(so); postevent(so, 0, EV_RCLOSED); } if (how != SHUT_RD) { if ((so->so_state & SS_CANTSENDMORE) != 0) { /* write already shut down */ error = ENOTCONN; goto done; } error = (*pr->pr_usrreqs->pru_shutdown)(so); postevent(so, 0, EV_WCLOSED); } done: KERNEL_DEBUG(DBG_FNC_SOSHUTDOWN, how, 1, 0, 0, 0); return (error); } int soshutdownlock(struct socket *so, int how) { int error = 0; #if CONTENT_FILTER /* * A content filter may delay the actual shutdown until it * has processed the pending data */ if (so->so_flags & SOF_CONTENT_FILTER) { error = cfil_sock_shutdown(so, &how); if (error == EJUSTRETURN) { error = 0; goto done; } else if (error != 0) { goto done; } } #endif /* CONTENT_FILTER */ error = soshutdownlock_final(so, how); done: return (error); } void sowflush(struct socket *so) { struct sockbuf *sb = &so->so_snd; #ifdef notyet lck_mtx_t *mutex_held; /* * XXX: This code is currently commented out, because we may get here * as part of sofreelastref(), and at that time, pr_getlock() may no * longer be able to return us the lock; this will be fixed in future. */ if (so->so_proto->pr_getlock != NULL) mutex_held = (*so->so_proto->pr_getlock)(so, 0); else mutex_held = so->so_proto->pr_domain->dom_mtx; lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); #endif /* notyet */ /* * Obtain lock on the socket buffer (SB_LOCK). This is required * to prevent the socket buffer from being unexpectedly altered * while it is used by another thread in socket send/receive. * * sblock() must not fail here, hence the assertion. */ (void) sblock(sb, SBL_WAIT | SBL_NOINTR | SBL_IGNDEFUNCT); VERIFY(sb->sb_flags & SB_LOCK); sb->sb_flags &= ~(SB_SEL|SB_UPCALL); sb->sb_flags |= SB_DROP; sb->sb_upcall = NULL; sb->sb_upcallarg = NULL; sbunlock(sb, TRUE); /* keep socket locked */ selthreadclear(&sb->sb_sel); sbrelease(sb); } void sorflush(struct socket *so) { struct sockbuf *sb = &so->so_rcv; struct protosw *pr = so->so_proto; struct sockbuf asb; #ifdef notyet lck_mtx_t *mutex_held; /* * XXX: This code is currently commented out, because we may get here * as part of sofreelastref(), and at that time, pr_getlock() may no * longer be able to return us the lock; this will be fixed in future. */ if (so->so_proto->pr_getlock != NULL) mutex_held = (*so->so_proto->pr_getlock)(so, 0); else mutex_held = so->so_proto->pr_domain->dom_mtx; lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); #endif /* notyet */ sflt_notify(so, sock_evt_flush_read, NULL); socantrcvmore(so); /* * Obtain lock on the socket buffer (SB_LOCK). This is required * to prevent the socket buffer from being unexpectedly altered * while it is used by another thread in socket send/receive. * * sblock() must not fail here, hence the assertion. */ (void) sblock(sb, SBL_WAIT | SBL_NOINTR | SBL_IGNDEFUNCT); VERIFY(sb->sb_flags & SB_LOCK); /* * Copy only the relevant fields from "sb" to "asb" which we * need for sbrelease() to function. In particular, skip * sb_sel as it contains the wait queue linkage, which would * wreak havoc if we were to issue selthreadclear() on "asb". * Make sure to not carry over SB_LOCK in "asb", as we need * to acquire it later as part of sbrelease(). */ bzero(&asb, sizeof (asb)); asb.sb_cc = sb->sb_cc; asb.sb_hiwat = sb->sb_hiwat; asb.sb_mbcnt = sb->sb_mbcnt; asb.sb_mbmax = sb->sb_mbmax; asb.sb_ctl = sb->sb_ctl; asb.sb_lowat = sb->sb_lowat; asb.sb_mb = sb->sb_mb; asb.sb_mbtail = sb->sb_mbtail; asb.sb_lastrecord = sb->sb_lastrecord; asb.sb_so = sb->sb_so; asb.sb_flags = sb->sb_flags; asb.sb_flags &= ~(SB_LOCK|SB_SEL|SB_KNOTE|SB_UPCALL); asb.sb_flags |= SB_DROP; /* * Ideally we'd bzero() these and preserve the ones we need; * but to do that we'd need to shuffle things around in the * sockbuf, and we can't do it now because there are KEXTS * that are directly referring to the socket structure. * * Setting SB_DROP acts as a barrier to prevent further appends. * Clearing SB_SEL is done for selthreadclear() below. */ sb->sb_cc = 0; sb->sb_hiwat = 0; sb->sb_mbcnt = 0; sb->sb_mbmax = 0; sb->sb_ctl = 0; sb->sb_lowat = 0; sb->sb_mb = NULL; sb->sb_mbtail = NULL; sb->sb_lastrecord = NULL; sb->sb_timeo.tv_sec = 0; sb->sb_timeo.tv_usec = 0; sb->sb_upcall = NULL; sb->sb_upcallarg = NULL; sb->sb_flags &= ~(SB_SEL|SB_UPCALL); sb->sb_flags |= SB_DROP; sbunlock(sb, TRUE); /* keep socket locked */ /* * Note that selthreadclear() is called on the original "sb" and * not the local "asb" because of the way wait queue linkage is * implemented. Given that selwakeup() may be triggered, SB_SEL * should no longer be set (cleared above.) */ selthreadclear(&sb->sb_sel); if ((pr->pr_flags & PR_RIGHTS) && pr->pr_domain->dom_dispose) (*pr->pr_domain->dom_dispose)(asb.sb_mb); sbrelease(&asb); } /* * Perhaps this routine, and sooptcopyout(), below, ought to come in * an additional variant to handle the case where the option value needs * to be some kind of integer, but not a specific size. * In addition to their use here, these functions are also called by the * protocol-level pr_ctloutput() routines. * * Returns: 0 Success * EINVAL * copyin:EFAULT */ int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen) { size_t valsize; /* * If the user gives us more than we wanted, we ignore it, * but if we don't get the minimum length the caller * wants, we return EINVAL. On success, sopt->sopt_valsize * is set to however much we actually retrieved. */ if ((valsize = sopt->sopt_valsize) < minlen) return (EINVAL); if (valsize > len) sopt->sopt_valsize = valsize = len; if (sopt->sopt_p != kernproc) return (copyin(sopt->sopt_val, buf, valsize)); bcopy(CAST_DOWN(caddr_t, sopt->sopt_val), buf, valsize); return (0); } /* * sooptcopyin_timeval * Copy in a timeval value into tv_p, and take into account whether the * the calling process is 64-bit or 32-bit. Moved the sanity checking * code here so that we can verify the 64-bit tv_sec value before we lose * the top 32-bits assigning tv64.tv_sec to tv_p->tv_sec. */ static int sooptcopyin_timeval(struct sockopt *sopt, struct timeval *tv_p) { int error; if (proc_is64bit(sopt->sopt_p)) { struct user64_timeval tv64; if (sopt->sopt_valsize < sizeof (tv64)) return (EINVAL); sopt->sopt_valsize = sizeof (tv64); if (sopt->sopt_p != kernproc) { error = copyin(sopt->sopt_val, &tv64, sizeof (tv64)); if (error != 0) return (error); } else { bcopy(CAST_DOWN(caddr_t, sopt->sopt_val), &tv64, sizeof (tv64)); } if (tv64.tv_sec < 0 || tv64.tv_sec > LONG_MAX || tv64.tv_usec < 0 || tv64.tv_usec >= 1000000) return (EDOM); tv_p->tv_sec = tv64.tv_sec; tv_p->tv_usec = tv64.tv_usec; } else { struct user32_timeval tv32; if (sopt->sopt_valsize < sizeof (tv32)) return (EINVAL); sopt->sopt_valsize = sizeof (tv32); if (sopt->sopt_p != kernproc) { error = copyin(sopt->sopt_val, &tv32, sizeof (tv32)); if (error != 0) { return (error); } } else { bcopy(CAST_DOWN(caddr_t, sopt->sopt_val), &tv32, sizeof (tv32)); } #ifndef __LP64__ /* * K64todo "comparison is always false due to * limited range of data type" */ if (tv32.tv_sec < 0 || tv32.tv_sec > LONG_MAX || tv32.tv_usec < 0 || tv32.tv_usec >= 1000000) return (EDOM); #endif tv_p->tv_sec = tv32.tv_sec; tv_p->tv_usec = tv32.tv_usec; } return (0); } /* * Returns: 0 Success * EINVAL * ENOPROTOOPT * ENOBUFS * EDOM * sooptcopyin:EINVAL * sooptcopyin:EFAULT * sooptcopyin_timeval:EINVAL * sooptcopyin_timeval:EFAULT * sooptcopyin_timeval:EDOM * <pr_ctloutput>:EOPNOTSUPP[AF_UNIX] * <pr_ctloutput>:???w * sflt_attach_private:??? [whatever a filter author chooses] * <sf_setoption>:??? [whatever a filter author chooses] * * Notes: Other <pru_listen> returns depend on the protocol family; all * <sf_listen> returns depend on what the filter author causes * their filter to return. */ int sosetoptlock(struct socket *so, struct sockopt *sopt, int dolock) { int error, optval; struct linger l; struct timeval tv; #if CONFIG_MACF_SOCKET struct mac extmac; #endif /* MAC_SOCKET */ if (sopt->sopt_dir != SOPT_SET) sopt->sopt_dir = SOPT_SET; if (dolock) socket_lock(so, 1); if ((so->so_state & (SS_CANTRCVMORE | SS_CANTSENDMORE)) == (SS_CANTRCVMORE | SS_CANTSENDMORE) && (so->so_flags & SOF_NPX_SETOPTSHUT) == 0) { /* the socket has been shutdown, no more sockopt's */ error = EINVAL; goto out; } error = sflt_setsockopt(so, sopt); if (error != 0) { if (error == EJUSTRETURN) error = 0; goto out; } if (sopt->sopt_level != SOL_SOCKET) { if (so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) { error = (*so->so_proto->pr_ctloutput)(so, sopt); goto out; } error = ENOPROTOOPT; } else { /* * Allow socket-level (SOL_SOCKET) options to be filtered by * the protocol layer, if needed. A zero value returned from * the handler means use default socket-level processing as * done by the rest of this routine. Otherwise, any other * return value indicates that the option is unsupported. */ if (so->so_proto != NULL && (error = so->so_proto->pr_usrreqs-> pru_socheckopt(so, sopt)) != 0) goto out; error = 0; switch (sopt->sopt_name) { case SO_LINGER: case SO_LINGER_SEC: error = sooptcopyin(sopt, &l, sizeof (l), sizeof (l)); if (error != 0) goto out; so->so_linger = (sopt->sopt_name == SO_LINGER) ? l.l_linger : l.l_linger * hz; if (l.l_onoff != 0) so->so_options |= SO_LINGER; else so->so_options &= ~SO_LINGER; break; case SO_DEBUG: case SO_KEEPALIVE: case SO_DONTROUTE: case SO_USELOOPBACK: case SO_BROADCAST: case SO_REUSEADDR: case SO_REUSEPORT: case SO_OOBINLINE: case SO_TIMESTAMP: case SO_TIMESTAMP_MONOTONIC: case SO_DONTTRUNC: case SO_WANTMORE: case SO_WANTOOBFLAG: case SO_NOWAKEFROMSLEEP: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; if (optval) so->so_options |= sopt->sopt_name; else so->so_options &= ~sopt->sopt_name; break; case SO_SNDBUF: case SO_RCVBUF: case SO_SNDLOWAT: case SO_RCVLOWAT: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; /* * Values < 1 make no sense for any of these * options, so disallow them. */ if (optval < 1) { error = EINVAL; goto out; } switch (sopt->sopt_name) { case SO_SNDBUF: case SO_RCVBUF: { struct sockbuf *sb = (sopt->sopt_name == SO_SNDBUF) ? &so->so_snd : &so->so_rcv; if (sbreserve(sb, (u_int32_t)optval) == 0) { error = ENOBUFS; goto out; } sb->sb_flags |= SB_USRSIZE; sb->sb_flags &= ~SB_AUTOSIZE; sb->sb_idealsize = (u_int32_t)optval; break; } /* * Make sure the low-water is never greater than * the high-water. */ case SO_SNDLOWAT: { int space = sbspace(&so->so_snd); u_int32_t hiwat = so->so_snd.sb_hiwat; if (so->so_snd.sb_flags & SB_UNIX) { struct unpcb *unp = (struct unpcb *)(so->so_pcb); if (unp != NULL && unp->unp_conn != NULL) { hiwat += unp->unp_conn->unp_cc; } } so->so_snd.sb_lowat = (optval > hiwat) ? hiwat : optval; if (space >= so->so_snd.sb_lowat) { sowwakeup(so); } break; } case SO_RCVLOWAT: { int64_t data_len; so->so_rcv.sb_lowat = (optval > so->so_rcv.sb_hiwat) ? so->so_rcv.sb_hiwat : optval; data_len = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; if (data_len >= so->so_rcv.sb_lowat) sorwakeup(so); break; } } break; case SO_SNDTIMEO: case SO_RCVTIMEO: error = sooptcopyin_timeval(sopt, &tv); if (error != 0) goto out; switch (sopt->sopt_name) { case SO_SNDTIMEO: so->so_snd.sb_timeo = tv; break; case SO_RCVTIMEO: so->so_rcv.sb_timeo = tv; break; } break; case SO_NKE: { struct so_nke nke; error = sooptcopyin(sopt, &nke, sizeof (nke), sizeof (nke)); if (error != 0) goto out; error = sflt_attach_internal(so, nke.nke_handle); break; } case SO_NOSIGPIPE: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; if (optval != 0) so->so_flags |= SOF_NOSIGPIPE; else so->so_flags &= ~SOF_NOSIGPIPE; break; case SO_NOADDRERR: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; if (optval != 0) so->so_flags |= SOF_NOADDRAVAIL; else so->so_flags &= ~SOF_NOADDRAVAIL; break; case SO_REUSESHAREUID: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; if (optval != 0) so->so_flags |= SOF_REUSESHAREUID; else so->so_flags &= ~SOF_REUSESHAREUID; break; case SO_NOTIFYCONFLICT: if (kauth_cred_issuser(kauth_cred_get()) == 0) { error = EPERM; goto out; } error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; if (optval != 0) so->so_flags |= SOF_NOTIFYCONFLICT; else so->so_flags &= ~SOF_NOTIFYCONFLICT; break; case SO_RESTRICTIONS: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; error = so_set_restrictions(so, optval); break; case SO_AWDL_UNRESTRICTED: if (SOCK_DOM(so) != PF_INET && SOCK_DOM(so) != PF_INET6) { error = EOPNOTSUPP; goto out; } error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval)); if (error != 0) goto out; if (optval != 0) { kauth_cred_t cred = NULL; proc_t ep = PROC_NULL; if (so->so_flags & SOF_DELEGATED) { ep = proc_find(so->e_pid); if (ep) cred = kauth_cred_proc_ref(ep); } error = priv_check_cred( cred ? cred : so->so_cred, PRIV_NET_RESTRICTED_AWDL, 0); if (error == 0) inp_set_awdl_unrestricted( sotoinpcb(so)); if (cred) kauth_cred_unref(&cred); if (ep != PROC_NULL) proc_rele(ep); } else inp_clear_awdl_unrestricted(sotoinpcb(so)); break; case SO_LABEL: #if CONFIG_MACF_SOCKET if ((error = sooptcopyin(sopt, &extmac, sizeof (extmac), sizeof (extmac))) != 0) goto out; error = mac_setsockopt_label(proc_ucred(sopt->sopt_p), so, &extmac); #else error = EOPNOTSUPP; #endif /* MAC_SOCKET */ break; case SO_UPCALLCLOSEWAIT: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; if (optval != 0) so->so_flags |= SOF_UPCALLCLOSEWAIT; else so->so_flags &= ~SOF_UPCALLCLOSEWAIT; break; case SO_RANDOMPORT: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; if (optval != 0) so->so_flags |= SOF_BINDRANDOMPORT; else so->so_flags &= ~SOF_BINDRANDOMPORT; break; case SO_NP_EXTENSIONS: { struct so_np_extensions sonpx; error = sooptcopyin(sopt, &sonpx, sizeof (sonpx), sizeof (sonpx)); if (error != 0) goto out; if (sonpx.npx_mask & ~SONPX_MASK_VALID) { error = EINVAL; goto out; } /* * Only one bit defined for now */ if ((sonpx.npx_mask & SONPX_SETOPTSHUT)) { if ((sonpx.npx_flags & SONPX_SETOPTSHUT)) so->so_flags |= SOF_NPX_SETOPTSHUT; else so->so_flags &= ~SOF_NPX_SETOPTSHUT; } break; } case SO_TRAFFIC_CLASS: { error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; error = so_set_traffic_class(so, optval); if (error != 0) goto out; break; } case SO_RECV_TRAFFIC_CLASS: { error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; if (optval == 0) so->so_flags &= ~SOF_RECV_TRAFFIC_CLASS; else so->so_flags |= SOF_RECV_TRAFFIC_CLASS; break; } case SO_TRAFFIC_CLASS_DBG: { struct so_tcdbg so_tcdbg; error = sooptcopyin(sopt, &so_tcdbg, sizeof (struct so_tcdbg), sizeof (struct so_tcdbg)); if (error != 0) goto out; error = so_set_tcdbg(so, &so_tcdbg); if (error != 0) goto out; break; } case SO_PRIVILEGED_TRAFFIC_CLASS: error = priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_TRAFFIC_CLASS, 0); if (error != 0) goto out; error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; if (optval == 0) so->so_flags &= ~SOF_PRIVILEGED_TRAFFIC_CLASS; else so->so_flags |= SOF_PRIVILEGED_TRAFFIC_CLASS; break; case SO_DEFUNCTOK: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0 || (so->so_flags & SOF_DEFUNCT)) { if (error == 0) error = EBADF; goto out; } /* * Any process can set SO_DEFUNCTOK (clear * SOF_NODEFUNCT), but only root can clear * SO_DEFUNCTOK (set SOF_NODEFUNCT). */ if (optval == 0 && kauth_cred_issuser(kauth_cred_get()) == 0) { error = EPERM; goto out; } if (optval) so->so_flags &= ~SOF_NODEFUNCT; else so->so_flags |= SOF_NODEFUNCT; if (SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) { char s[MAX_IPv6_STR_LEN]; char d[MAX_IPv6_STR_LEN]; struct inpcb *inp = sotoinpcb(so); SODEFUNCTLOG(("%s[%d]: so 0x%llx [%s %s:%d -> " "%s:%d] is now marked as %seligible for " "defunct\n", __func__, proc_selfpid(), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), (SOCK_TYPE(so) == SOCK_STREAM) ? "TCP" : "UDP", inet_ntop(SOCK_DOM(so), ((SOCK_DOM(so) == PF_INET) ? (void *)&inp->inp_laddr.s_addr : (void *)&inp->in6p_laddr), s, sizeof (s)), ntohs(inp->in6p_lport), inet_ntop(SOCK_DOM(so), (SOCK_DOM(so) == PF_INET) ? (void *)&inp->inp_faddr.s_addr : (void *)&inp->in6p_faddr, d, sizeof (d)), ntohs(inp->in6p_fport), (so->so_flags & SOF_NODEFUNCT) ? "not " : "")); } else { SODEFUNCTLOG(("%s[%d]: so 0x%llx [%d,%d] is " "now marked as %seligible for defunct\n", __func__, proc_selfpid(), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), (so->so_flags & SOF_NODEFUNCT) ? "not " : "")); } break; case SO_ISDEFUNCT: /* This option is not settable */ error = EINVAL; break; case SO_OPPORTUNISTIC: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error == 0) error = so_set_opportunistic(so, optval); break; case SO_FLUSH: /* This option is handled by lower layer(s) */ error = 0; break; case SO_RECV_ANYIF: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error == 0) error = so_set_recv_anyif(so, optval); break; case SO_TRAFFIC_MGT_BACKGROUND: { /* This option is handled by lower layer(s) */ error = 0; break; } #if FLOW_DIVERT case SO_FLOW_DIVERT_TOKEN: error = flow_divert_token_set(so, sopt); break; #endif /* FLOW_DIVERT */ case SO_DELEGATED: if ((error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval))) != 0) break; error = so_set_effective_pid(so, optval, sopt->sopt_p); break; case SO_DELEGATED_UUID: { uuid_t euuid; if ((error = sooptcopyin(sopt, &euuid, sizeof (euuid), sizeof (euuid))) != 0) break; error = so_set_effective_uuid(so, euuid, sopt->sopt_p); break; } #if NECP case SO_NECP_ATTRIBUTES: error = necp_set_socket_attributes(so, sopt); break; #endif /* NECP */ #if MPTCP case SO_MPTCP_FASTJOIN: if (!((so->so_flags & SOF_MP_SUBFLOW) || ((SOCK_CHECK_DOM(so, PF_MULTIPATH)) && (SOCK_CHECK_PROTO(so, IPPROTO_TCP))))) { error = ENOPROTOOPT; break; } error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error != 0) goto out; if (optval == 0) so->so_flags &= ~SOF_MPTCP_FASTJOIN; else so->so_flags |= SOF_MPTCP_FASTJOIN; break; #endif /* MPTCP */ case SO_EXTENDED_BK_IDLE: error = sooptcopyin(sopt, &optval, sizeof (optval), sizeof (optval)); if (error == 0) error = so_set_extended_bk_idle(so, optval); break; default: error = ENOPROTOOPT; break; } if (error == 0 && so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) { (void) so->so_proto->pr_ctloutput(so, sopt); } } out: if (dolock) socket_unlock(so, 1); return (error); } /* Helper routines for getsockopt */ int sooptcopyout(struct sockopt *sopt, void *buf, size_t len) { int error; size_t valsize; error = 0; /* * Documented get behavior is that we always return a value, * possibly truncated to fit in the user's buffer. * Traditional behavior is that we always tell the user * precisely how much we copied, rather than something useful * like the total amount we had available for her. * Note that this interface is not idempotent; the entire answer must * generated ahead of time. */ valsize = min(len, sopt->sopt_valsize); sopt->sopt_valsize = valsize; if (sopt->sopt_val != USER_ADDR_NULL) { if (sopt->sopt_p != kernproc) error = copyout(buf, sopt->sopt_val, valsize); else bcopy(buf, CAST_DOWN(caddr_t, sopt->sopt_val), valsize); } return (error); } static int sooptcopyout_timeval(struct sockopt *sopt, const struct timeval *tv_p) { int error; size_t len; struct user64_timeval tv64; struct user32_timeval tv32; const void * val; size_t valsize; error = 0; if (proc_is64bit(sopt->sopt_p)) { len = sizeof (tv64); tv64.tv_sec = tv_p->tv_sec; tv64.tv_usec = tv_p->tv_usec; val = &tv64; } else { len = sizeof (tv32); tv32.tv_sec = tv_p->tv_sec; tv32.tv_usec = tv_p->tv_usec; val = &tv32; } valsize = min(len, sopt->sopt_valsize); sopt->sopt_valsize = valsize; if (sopt->sopt_val != USER_ADDR_NULL) { if (sopt->sopt_p != kernproc) error = copyout(val, sopt->sopt_val, valsize); else bcopy(val, CAST_DOWN(caddr_t, sopt->sopt_val), valsize); } return (error); } /* * Return: 0 Success * ENOPROTOOPT * <pr_ctloutput>:EOPNOTSUPP[AF_UNIX] * <pr_ctloutput>:??? * <sf_getoption>:??? */ int sogetoptlock(struct socket *so, struct sockopt *sopt, int dolock) { int error, optval; struct linger l; struct timeval tv; #if CONFIG_MACF_SOCKET struct mac extmac; #endif /* MAC_SOCKET */ if (sopt->sopt_dir != SOPT_GET) sopt->sopt_dir = SOPT_GET; if (dolock) socket_lock(so, 1); error = sflt_getsockopt(so, sopt); if (error != 0) { if (error == EJUSTRETURN) error = 0; goto out; } if (sopt->sopt_level != SOL_SOCKET) { if (so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) { error = (*so->so_proto->pr_ctloutput)(so, sopt); goto out; } error = ENOPROTOOPT; } else { /* * Allow socket-level (SOL_SOCKET) options to be filtered by * the protocol layer, if needed. A zero value returned from * the handler means use default socket-level processing as * done by the rest of this routine. Otherwise, any other * return value indicates that the option is unsupported. */ if (so->so_proto != NULL && (error = so->so_proto->pr_usrreqs-> pru_socheckopt(so, sopt)) != 0) goto out; error = 0; switch (sopt->sopt_name) { case SO_LINGER: case SO_LINGER_SEC: l.l_onoff = ((so->so_options & SO_LINGER) ? 1 : 0); l.l_linger = (sopt->sopt_name == SO_LINGER) ? so->so_linger : so->so_linger / hz; error = sooptcopyout(sopt, &l, sizeof (l)); break; case SO_USELOOPBACK: case SO_DONTROUTE: case SO_DEBUG: case SO_KEEPALIVE: case SO_REUSEADDR: case SO_REUSEPORT: case SO_BROADCAST: case SO_OOBINLINE: case SO_TIMESTAMP: case SO_TIMESTAMP_MONOTONIC: case SO_DONTTRUNC: case SO_WANTMORE: case SO_WANTOOBFLAG: case SO_NOWAKEFROMSLEEP: optval = so->so_options & sopt->sopt_name; integer: error = sooptcopyout(sopt, &optval, sizeof (optval)); break; case SO_TYPE: optval = so->so_type; goto integer; case SO_NREAD: if (so->so_proto->pr_flags & PR_ATOMIC) { int pkt_total; struct mbuf *m1; pkt_total = 0; m1 = so->so_rcv.sb_mb; while (m1 != NULL) { if (m1->m_type == MT_DATA || m1->m_type == MT_HEADER || m1->m_type == MT_OOBDATA) pkt_total += m1->m_len; m1 = m1->m_next; } optval = pkt_total; } else { optval = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; } goto integer; case SO_NUMRCVPKT: if (so->so_proto->pr_flags & PR_ATOMIC) { int cnt = 0; struct mbuf *m1; m1 = so->so_rcv.sb_mb; while (m1 != NULL) { if (m1->m_type == MT_DATA || m1->m_type == MT_HEADER || m1->m_type == MT_OOBDATA) cnt += 1; m1 = m1->m_nextpkt; } optval = cnt; goto integer; } else { error = EINVAL; break; } case SO_NWRITE: optval = so->so_snd.sb_cc; goto integer; case SO_ERROR: optval = so->so_error; so->so_error = 0; goto integer; case SO_SNDBUF: { u_int32_t hiwat = so->so_snd.sb_hiwat; if (so->so_snd.sb_flags & SB_UNIX) { struct unpcb *unp = (struct unpcb *)(so->so_pcb); if (unp != NULL && unp->unp_conn != NULL) { hiwat += unp->unp_conn->unp_cc; } } optval = hiwat; goto integer; } case SO_RCVBUF: optval = so->so_rcv.sb_hiwat; goto integer; case SO_SNDLOWAT: optval = so->so_snd.sb_lowat; goto integer; case SO_RCVLOWAT: optval = so->so_rcv.sb_lowat; goto integer; case SO_SNDTIMEO: case SO_RCVTIMEO: tv = (sopt->sopt_name == SO_SNDTIMEO ? so->so_snd.sb_timeo : so->so_rcv.sb_timeo); error = sooptcopyout_timeval(sopt, &tv); break; case SO_NOSIGPIPE: optval = (so->so_flags & SOF_NOSIGPIPE); goto integer; case SO_NOADDRERR: optval = (so->so_flags & SOF_NOADDRAVAIL); goto integer; case SO_REUSESHAREUID: optval = (so->so_flags & SOF_REUSESHAREUID); goto integer; case SO_NOTIFYCONFLICT: optval = (so->so_flags & SOF_NOTIFYCONFLICT); goto integer; case SO_RESTRICTIONS: optval = so_get_restrictions(so); goto integer; case SO_AWDL_UNRESTRICTED: if (SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) { optval = inp_get_awdl_unrestricted( sotoinpcb(so)); goto integer; } else error = EOPNOTSUPP; break; case SO_LABEL: #if CONFIG_MACF_SOCKET if ((error = sooptcopyin(sopt, &extmac, sizeof (extmac), sizeof (extmac))) != 0 || (error = mac_socket_label_get(proc_ucred( sopt->sopt_p), so, &extmac)) != 0) break; error = sooptcopyout(sopt, &extmac, sizeof (extmac)); #else error = EOPNOTSUPP; #endif /* MAC_SOCKET */ break; case SO_PEERLABEL: #if CONFIG_MACF_SOCKET if ((error = sooptcopyin(sopt, &extmac, sizeof (extmac), sizeof (extmac))) != 0 || (error = mac_socketpeer_label_get(proc_ucred( sopt->sopt_p), so, &extmac)) != 0) break; error = sooptcopyout(sopt, &extmac, sizeof (extmac)); #else error = EOPNOTSUPP; #endif /* MAC_SOCKET */ break; #ifdef __APPLE_API_PRIVATE case SO_UPCALLCLOSEWAIT: optval = (so->so_flags & SOF_UPCALLCLOSEWAIT); goto integer; #endif case SO_RANDOMPORT: optval = (so->so_flags & SOF_BINDRANDOMPORT); goto integer; case SO_NP_EXTENSIONS: { struct so_np_extensions sonpx; sonpx.npx_flags = (so->so_flags & SOF_NPX_SETOPTSHUT) ? SONPX_SETOPTSHUT : 0; sonpx.npx_mask = SONPX_MASK_VALID; error = sooptcopyout(sopt, &sonpx, sizeof (struct so_np_extensions)); break; } case SO_TRAFFIC_CLASS: optval = so->so_traffic_class; goto integer; case SO_RECV_TRAFFIC_CLASS: optval = (so->so_flags & SOF_RECV_TRAFFIC_CLASS); goto integer; case SO_TRAFFIC_CLASS_STATS: error = sooptcopyout(sopt, &so->so_tc_stats, sizeof (so->so_tc_stats)); break; case SO_TRAFFIC_CLASS_DBG: error = sogetopt_tcdbg(so, sopt); break; case SO_PRIVILEGED_TRAFFIC_CLASS: optval = (so->so_flags & SOF_PRIVILEGED_TRAFFIC_CLASS); goto integer; case SO_DEFUNCTOK: optval = !(so->so_flags & SOF_NODEFUNCT); goto integer; case SO_ISDEFUNCT: optval = (so->so_flags & SOF_DEFUNCT); goto integer; case SO_OPPORTUNISTIC: optval = so_get_opportunistic(so); goto integer; case SO_FLUSH: /* This option is not gettable */ error = EINVAL; break; case SO_RECV_ANYIF: optval = so_get_recv_anyif(so); goto integer; case SO_TRAFFIC_MGT_BACKGROUND: /* This option is handled by lower layer(s) */ if (so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) { (void) so->so_proto->pr_ctloutput(so, sopt); } break; #if FLOW_DIVERT case SO_FLOW_DIVERT_TOKEN: error = flow_divert_token_get(so, sopt); break; #endif /* FLOW_DIVERT */ #if NECP case SO_NECP_ATTRIBUTES: error = necp_get_socket_attributes(so, sopt); break; #endif /* NECP */ #if CONTENT_FILTER case SO_CFIL_SOCK_ID: { cfil_sock_id_t sock_id; sock_id = cfil_sock_id_from_socket(so); error = sooptcopyout(sopt, &sock_id, sizeof(cfil_sock_id_t)); break; } #endif /* CONTENT_FILTER */ #if MPTCP case SO_MPTCP_FASTJOIN: if (!((so->so_flags & SOF_MP_SUBFLOW) || ((SOCK_CHECK_DOM(so, PF_MULTIPATH)) && (SOCK_CHECK_PROTO(so, IPPROTO_TCP))))) { error = ENOPROTOOPT; break; } optval = (so->so_flags & SOF_MPTCP_FASTJOIN); /* Fixed along with rdar://19391339 */ goto integer; #endif /* MPTCP */ case SO_EXTENDED_BK_IDLE: optval = (so->so_flags1 & SOF1_EXTEND_BK_IDLE_WANTED); goto integer; default: error = ENOPROTOOPT; break; } } out: if (dolock) socket_unlock(so, 1); return (error); } /* * The size limits on our soopt_getm is different from that on FreeBSD. * We limit the size of options to MCLBYTES. This will have to change * if we need to define options that need more space than MCLBYTES. */ int soopt_getm(struct sockopt *sopt, struct mbuf **mp) { struct mbuf *m, *m_prev; int sopt_size = sopt->sopt_valsize; int how; if (sopt_size <= 0 || sopt_size > MCLBYTES) return (EMSGSIZE); how = sopt->sopt_p != kernproc ? M_WAIT : M_DONTWAIT; MGET(m, how, MT_DATA); if (m == NULL) return (ENOBUFS); if (sopt_size > MLEN) { MCLGET(m, how); if ((m->m_flags & M_EXT) == 0) { m_free(m); return (ENOBUFS); } m->m_len = min(MCLBYTES, sopt_size); } else { m->m_len = min(MLEN, sopt_size); } sopt_size -= m->m_len; *mp = m; m_prev = m; while (sopt_size > 0) { MGET(m, how, MT_DATA); if (m == NULL) { m_freem(*mp); return (ENOBUFS); } if (sopt_size > MLEN) { MCLGET(m, how); if ((m->m_flags & M_EXT) == 0) { m_freem(*mp); m_freem(m); return (ENOBUFS); } m->m_len = min(MCLBYTES, sopt_size); } else { m->m_len = min(MLEN, sopt_size); } sopt_size -= m->m_len; m_prev->m_next = m; m_prev = m; } return (0); } /* copyin sopt data into mbuf chain */ int soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) { struct mbuf *m0 = m; if (sopt->sopt_val == USER_ADDR_NULL) return (0); while (m != NULL && sopt->sopt_valsize >= m->m_len) { if (sopt->sopt_p != kernproc) { int error; error = copyin(sopt->sopt_val, mtod(m, char *), m->m_len); if (error != 0) { m_freem(m0); return (error); } } else { bcopy(CAST_DOWN(caddr_t, sopt->sopt_val), mtod(m, char *), m->m_len); } sopt->sopt_valsize -= m->m_len; sopt->sopt_val += m->m_len; m = m->m_next; } /* should be allocated enoughly at ip6_sooptmcopyin() */ if (m != NULL) { panic("soopt_mcopyin"); /* NOTREACHED */ } return (0); } /* copyout mbuf chain data into soopt */ int soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) { struct mbuf *m0 = m; size_t valsize = 0; if (sopt->sopt_val == USER_ADDR_NULL) return (0); while (m != NULL && sopt->sopt_valsize >= m->m_len) { if (sopt->sopt_p != kernproc) { int error; error = copyout(mtod(m, char *), sopt->sopt_val, m->m_len); if (error != 0) { m_freem(m0); return (error); } } else { bcopy(mtod(m, char *), CAST_DOWN(caddr_t, sopt->sopt_val), m->m_len); } sopt->sopt_valsize -= m->m_len; sopt->sopt_val += m->m_len; valsize += m->m_len; m = m->m_next; } if (m != NULL) { /* enough soopt buffer should be given from user-land */ m_freem(m0); return (EINVAL); } sopt->sopt_valsize = valsize; return (0); } void sohasoutofband(struct socket *so) { if (so->so_pgid < 0) gsignal(-so->so_pgid, SIGURG); else if (so->so_pgid > 0) proc_signal(so->so_pgid, SIGURG); selwakeup(&so->so_rcv.sb_sel); } int sopoll(struct socket *so, int events, kauth_cred_t cred, void * wql) { #pragma unused(cred) struct proc *p = current_proc(); int revents = 0; socket_lock(so, 1); so_update_last_owner_locked(so, PROC_NULL); so_update_policy(so); if (events & (POLLIN | POLLRDNORM)) if (soreadable(so)) revents |= events & (POLLIN | POLLRDNORM); if (events & (POLLOUT | POLLWRNORM)) if (sowriteable(so)) revents |= events & (POLLOUT | POLLWRNORM); if (events & (POLLPRI | POLLRDBAND)) if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) revents |= events & (POLLPRI | POLLRDBAND); if (revents == 0) { if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) { /* * Darwin sets the flag first, * BSD calls selrecord first */ so->so_rcv.sb_flags |= SB_SEL; selrecord(p, &so->so_rcv.sb_sel, wql); } if (events & (POLLOUT | POLLWRNORM)) { /* * Darwin sets the flag first, * BSD calls selrecord first */ so->so_snd.sb_flags |= SB_SEL; selrecord(p, &so->so_snd.sb_sel, wql); } } socket_unlock(so, 1); return (revents); } int soo_kqfilter(struct fileproc *fp, struct knote *kn, vfs_context_t ctx) { #pragma unused(fp) #if !CONFIG_MACF_SOCKET #pragma unused(ctx) #endif /* MAC_SOCKET */ struct socket *so = (struct socket *)kn->kn_fp->f_fglob->fg_data; struct klist *skl; socket_lock(so, 1); so_update_last_owner_locked(so, PROC_NULL); so_update_policy(so); #if CONFIG_MACF_SOCKET if (mac_socket_check_kqfilter(proc_ucred(vfs_context_proc(ctx)), kn, so) != 0) { socket_unlock(so, 1); return (1); } #endif /* MAC_SOCKET */ switch (kn->kn_filter) { case EVFILT_READ: kn->kn_fop = &soread_filtops; /* * If the caller explicitly asked for OOB results (e.g. poll()), * save that off in the hookid field and reserve the kn_flags * EV_OOBAND bit for output only. */ if (kn->kn_flags & EV_OOBAND) { kn->kn_flags &= ~EV_OOBAND; kn->kn_hookid = EV_OOBAND; } else { kn->kn_hookid = 0; } skl = &so->so_rcv.sb_sel.si_note; break; case EVFILT_WRITE: kn->kn_fop = &sowrite_filtops; skl = &so->so_snd.sb_sel.si_note; break; case EVFILT_SOCK: kn->kn_fop = &sock_filtops; skl = &so->so_klist; kn->kn_hookid = 0; kn->kn_status |= KN_TOUCH; break; default: socket_unlock(so, 1); return (1); } if (KNOTE_ATTACH(skl, kn)) { switch (kn->kn_filter) { case EVFILT_READ: so->so_rcv.sb_flags |= SB_KNOTE; break; case EVFILT_WRITE: so->so_snd.sb_flags |= SB_KNOTE; break; case EVFILT_SOCK: so->so_flags |= SOF_KNOTE; break; default: socket_unlock(so, 1); return (1); } } socket_unlock(so, 1); return (0); } static void filt_sordetach(struct knote *kn) { struct socket *so = (struct socket *)kn->kn_fp->f_fglob->fg_data; socket_lock(so, 1); if (so->so_rcv.sb_flags & SB_KNOTE) if (KNOTE_DETACH(&so->so_rcv.sb_sel.si_note, kn)) so->so_rcv.sb_flags &= ~SB_KNOTE; socket_unlock(so, 1); } /*ARGSUSED*/ static int filt_soread(struct knote *kn, long hint) { struct socket *so = (struct socket *)kn->kn_fp->f_fglob->fg_data; if ((hint & SO_FILT_HINT_LOCKED) == 0) socket_lock(so, 1); if (so->so_options & SO_ACCEPTCONN) { int isempty; /* * Radar 6615193 handle the listen case dynamically * for kqueue read filter. This allows to call listen() * after registering the kqueue EVFILT_READ. */ kn->kn_data = so->so_qlen; isempty = ! TAILQ_EMPTY(&so->so_comp); if ((hint & SO_FILT_HINT_LOCKED) == 0) socket_unlock(so, 1); return (isempty); } /* socket isn't a listener */ /* * NOTE_LOWAT specifies new low water mark in data, i.e. * the bytes of protocol data. We therefore exclude any * control bytes. */ kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; /* * Clear out EV_OOBAND that filt_soread may have set in the * past. */ kn->kn_flags &= ~EV_OOBAND; if ((so->so_oobmark) || (so->so_state & SS_RCVATMARK)) { kn->kn_flags |= EV_OOBAND; /* * If caller registered explicit interest in OOB data, * return immediately (data == amount beyond mark, for * legacy reasons - that should be changed later). */ if (kn->kn_hookid == EV_OOBAND) { /* * When so_state is SS_RCVATMARK, so_oobmark * is 0. */ kn->kn_data -= so->so_oobmark; if ((hint & SO_FILT_HINT_LOCKED) == 0) socket_unlock(so, 1); return (1); } } if ((so->so_state & SS_CANTRCVMORE) #if CONTENT_FILTER && cfil_sock_data_pending(&so->so_rcv) == 0 #endif /* CONTENT_FILTER */ ) { kn->kn_flags |= EV_EOF; kn->kn_fflags = so->so_error; if ((hint & SO_FILT_HINT_LOCKED) == 0) socket_unlock(so, 1); return (1); } if (so->so_error) { /* temporary udp error */ if ((hint & SO_FILT_HINT_LOCKED) == 0) socket_unlock(so, 1); return (1); } int64_t lowwat = so->so_rcv.sb_lowat; /* * Ensure that when NOTE_LOWAT is used, the derived * low water mark is bounded by socket's rcv buf's * high and low water mark values. */ if (kn->kn_sfflags & NOTE_LOWAT) { if (kn->kn_sdata > so->so_rcv.sb_hiwat) lowwat = so->so_rcv.sb_hiwat; else if (kn->kn_sdata > lowwat) lowwat = kn->kn_sdata; } if ((hint & SO_FILT_HINT_LOCKED) == 0) socket_unlock(so, 1); /* * The order below is important. Since NOTE_LOWAT * overrides sb_lowat, check for NOTE_LOWAT case * first. */ if (kn->kn_sfflags & NOTE_LOWAT) return (kn->kn_data >= lowwat); return (so->so_rcv.sb_cc >= lowwat); } static void filt_sowdetach(struct knote *kn) { struct socket *so = (struct socket *)kn->kn_fp->f_fglob->fg_data; socket_lock(so, 1); if (so->so_snd.sb_flags & SB_KNOTE) if (KNOTE_DETACH(&so->so_snd.sb_sel.si_note, kn)) so->so_snd.sb_flags &= ~SB_KNOTE; socket_unlock(so, 1); } int so_wait_for_if_feedback(struct socket *so) { if ((SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) && (so->so_state & SS_ISCONNECTED)) { struct inpcb *inp = sotoinpcb(so); if (INP_WAIT_FOR_IF_FEEDBACK(inp)) return (1); } return (0); } /*ARGSUSED*/ static int filt_sowrite(struct knote *kn, long hint) { struct socket *so = (struct socket *)kn->kn_fp->f_fglob->fg_data; int ret = 0; if ((hint & SO_FILT_HINT_LOCKED) == 0) socket_lock(so, 1); kn->kn_data = sbspace(&so->so_snd); if (so->so_state & SS_CANTSENDMORE) { kn->kn_flags |= EV_EOF; kn->kn_fflags = so->so_error; ret = 1; goto out; } if (so->so_error) { /* temporary udp error */ ret = 1; goto out; } if (!socanwrite(so)) { ret = 0; goto out; } if (so->so_flags1 & SOF1_PRECONNECT_DATA) { ret = 1; goto out; } int64_t lowwat = so->so_snd.sb_lowat; if (kn->kn_sfflags & NOTE_LOWAT) { if (kn->kn_sdata > so->so_snd.sb_hiwat) lowwat = so->so_snd.sb_hiwat; else if (kn->kn_sdata > lowwat) lowwat = kn->kn_sdata; } if (kn->kn_data >= lowwat) { if (so->so_flags & SOF_NOTSENT_LOWAT) { if ((SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) && so->so_type == SOCK_STREAM) { ret = tcp_notsent_lowat_check(so); } #if MPTCP else if ((SOCK_DOM(so) == PF_MULTIPATH) && (SOCK_PROTO(so) == IPPROTO_TCP)) { ret = mptcp_notsent_lowat_check(so); } #endif else { ret = 1; goto out; } } else { ret = 1; } } if (so_wait_for_if_feedback(so)) ret = 0; out: if ((hint & SO_FILT_HINT_LOCKED) == 0) socket_unlock(so, 1); return (ret); } static void filt_sockdetach(struct knote *kn) { struct socket *so = (struct socket *)kn->kn_fp->f_fglob->fg_data; socket_lock(so, 1); if ((so->so_flags & SOF_KNOTE) != 0) if (KNOTE_DETACH(&so->so_klist, kn)) so->so_flags &= ~SOF_KNOTE; socket_unlock(so, 1); } static int filt_sockev(struct knote *kn, long hint) { int ret = 0, locked = 0; struct socket *so = (struct socket *)kn->kn_fp->f_fglob->fg_data; long ev_hint = (hint & SO_FILT_HINT_EV); uint32_t level_trigger = 0; if ((hint & SO_FILT_HINT_LOCKED) == 0) { socket_lock(so, 1); locked = 1; } if (ev_hint & SO_FILT_HINT_CONNRESET) { kn->kn_fflags |= NOTE_CONNRESET; } if (ev_hint & SO_FILT_HINT_TIMEOUT) { kn->kn_fflags |= NOTE_TIMEOUT; } if (ev_hint & SO_FILT_HINT_NOSRCADDR) { kn->kn_fflags |= NOTE_NOSRCADDR; } if (ev_hint & SO_FILT_HINT_IFDENIED) { kn->kn_fflags |= NOTE_IFDENIED; } if (ev_hint & SO_FILT_HINT_KEEPALIVE) { kn->kn_fflags |= NOTE_KEEPALIVE; } if (ev_hint & SO_FILT_HINT_ADAPTIVE_WTIMO) { kn->kn_fflags |= NOTE_ADAPTIVE_WTIMO; } if (ev_hint & SO_FILT_HINT_ADAPTIVE_RTIMO) { kn->kn_fflags |= NOTE_ADAPTIVE_RTIMO; } if ((ev_hint & SO_FILT_HINT_CONNECTED) || (so->so_state & SS_ISCONNECTED)) { kn->kn_fflags |= NOTE_CONNECTED; level_trigger |= NOTE_CONNECTED; } if ((ev_hint & SO_FILT_HINT_DISCONNECTED) || (so->so_state & SS_ISDISCONNECTED)) { kn->kn_fflags |= NOTE_DISCONNECTED; level_trigger |= NOTE_DISCONNECTED; } if (ev_hint & SO_FILT_HINT_CONNINFO_UPDATED) { if (so->so_proto != NULL && (so->so_proto->pr_flags & PR_EVCONNINFO)) kn->kn_fflags |= NOTE_CONNINFO_UPDATED; } if ((so->so_state & SS_CANTRCVMORE) #if CONTENT_FILTER && cfil_sock_data_pending(&so->so_rcv) == 0 #endif /* CONTENT_FILTER */ ) { kn->kn_fflags |= NOTE_READCLOSED; level_trigger |= NOTE_READCLOSED; } if (so->so_state & SS_CANTSENDMORE) { kn->kn_fflags |= NOTE_WRITECLOSED; level_trigger |= NOTE_WRITECLOSED; } if ((ev_hint & SO_FILT_HINT_SUSPEND) || (so->so_flags & SOF_SUSPENDED)) { kn->kn_fflags &= ~(NOTE_SUSPEND | NOTE_RESUME); /* If resume event was delivered before, reset it */ kn->kn_hookid &= ~NOTE_RESUME; kn->kn_fflags |= NOTE_SUSPEND; level_trigger |= NOTE_SUSPEND; } if ((ev_hint & SO_FILT_HINT_RESUME) || (so->so_flags & SOF_SUSPENDED) == 0) { kn->kn_fflags &= ~(NOTE_SUSPEND | NOTE_RESUME); /* If suspend event was delivered before, reset it */ kn->kn_hookid &= ~NOTE_SUSPEND; kn->kn_fflags |= NOTE_RESUME; level_trigger |= NOTE_RESUME; } if (so->so_error != 0) { ret = 1; kn->kn_data = so->so_error; kn->kn_flags |= EV_EOF; } else { get_sockev_state(so, (u_int32_t *)&(kn->kn_data)); } /* Reset any events that are not requested on this knote */ kn->kn_fflags &= (kn->kn_sfflags & EVFILT_SOCK_ALL_MASK); level_trigger &= (kn->kn_sfflags & EVFILT_SOCK_ALL_MASK); /* Find the level triggerred events that are already delivered */ level_trigger &= kn->kn_hookid; level_trigger &= EVFILT_SOCK_LEVEL_TRIGGER_MASK; /* Do not deliver level triggerred events more than once */ if ((kn->kn_fflags & ~level_trigger) != 0) ret = 1; if (locked) socket_unlock(so, 1); return (ret); } static void filt_socktouch(struct knote *kn, struct kevent_internal_s *kev, long type) { #pragma unused(kev) switch (type) { case EVENT_REGISTER: { uint32_t changed_flags; changed_flags = (kn->kn_sfflags ^ kn->kn_hookid); /* * Since we keep track of events that are already * delivered, if any of those events are not requested * anymore the state related to them can be reset */ kn->kn_hookid &= ~(changed_flags & EVFILT_SOCK_LEVEL_TRIGGER_MASK); break; } case EVENT_PROCESS: /* * Store the state of the events being delivered. This * state can be used to deliver level triggered events * ateast once and still avoid waking up the application * multiple times as long as the event is active. */ if (kn->kn_fflags != 0) kn->kn_hookid |= (kn->kn_fflags & EVFILT_SOCK_LEVEL_TRIGGER_MASK); /* * NOTE_RESUME and NOTE_SUSPEND are an exception, deliver * only one of them and remember the last one that was * delivered last */ if (kn->kn_fflags & NOTE_SUSPEND) kn->kn_hookid &= ~NOTE_RESUME; if (kn->kn_fflags & NOTE_RESUME) kn->kn_hookid &= ~NOTE_SUSPEND; break; default: break; } } void get_sockev_state(struct socket *so, u_int32_t *statep) { u_int32_t state = *(statep); if (so->so_state & SS_ISCONNECTED) state |= SOCKEV_CONNECTED; else state &= ~(SOCKEV_CONNECTED); state |= ((so->so_state & SS_ISDISCONNECTED) ? SOCKEV_DISCONNECTED : 0); *(statep) = state; } #define SO_LOCK_HISTORY_STR_LEN \ (2 * SO_LCKDBG_MAX * (2 + (2 * sizeof (void *)) + 1) + 1) __private_extern__ const char * solockhistory_nr(struct socket *so) { size_t n = 0; int i; static char lock_history_str[SO_LOCK_HISTORY_STR_LEN]; bzero(lock_history_str, sizeof (lock_history_str)); for (i = SO_LCKDBG_MAX - 1; i >= 0; i--) { n += snprintf(lock_history_str + n, SO_LOCK_HISTORY_STR_LEN - n, "%p:%p ", so->lock_lr[(so->next_lock_lr + i) % SO_LCKDBG_MAX], so->unlock_lr[(so->next_unlock_lr + i) % SO_LCKDBG_MAX]); } return (lock_history_str); } int socket_lock(struct socket *so, int refcount) { int error = 0; void *lr_saved; lr_saved = __builtin_return_address(0); if (so->so_proto->pr_lock) { error = (*so->so_proto->pr_lock)(so, refcount, lr_saved); } else { #ifdef MORE_LOCKING_DEBUG lck_mtx_assert(so->so_proto->pr_domain->dom_mtx, LCK_MTX_ASSERT_NOTOWNED); #endif lck_mtx_lock(so->so_proto->pr_domain->dom_mtx); if (refcount) so->so_usecount++; so->lock_lr[so->next_lock_lr] = lr_saved; so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX; } return (error); } int socket_unlock(struct socket *so, int refcount) { int error = 0; void *lr_saved; lck_mtx_t *mutex_held; lr_saved = __builtin_return_address(0); if (so->so_proto == NULL) { panic("%s: null so_proto so=%p\n", __func__, so); /* NOTREACHED */ } if (so && so->so_proto->pr_unlock) { error = (*so->so_proto->pr_unlock)(so, refcount, lr_saved); } else { mutex_held = so->so_proto->pr_domain->dom_mtx; #ifdef MORE_LOCKING_DEBUG lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); #endif so->unlock_lr[so->next_unlock_lr] = lr_saved; so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX; if (refcount) { if (so->so_usecount <= 0) { panic("%s: bad refcount=%d so=%p (%d, %d, %d) " "lrh=%s", __func__, so->so_usecount, so, SOCK_DOM(so), so->so_type, SOCK_PROTO(so), solockhistory_nr(so)); /* NOTREACHED */ } so->so_usecount--; if (so->so_usecount == 0) sofreelastref(so, 1); } lck_mtx_unlock(mutex_held); } return (error); } /* Called with socket locked, will unlock socket */ void sofree(struct socket *so) { lck_mtx_t *mutex_held; if (so->so_proto->pr_getlock != NULL) mutex_held = (*so->so_proto->pr_getlock)(so, 0); else mutex_held = so->so_proto->pr_domain->dom_mtx; lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); sofreelastref(so, 0); } void soreference(struct socket *so) { socket_lock(so, 1); /* locks & take one reference on socket */ socket_unlock(so, 0); /* unlock only */ } void sodereference(struct socket *so) { socket_lock(so, 0); socket_unlock(so, 1); } /* * Set or clear SOF_MULTIPAGES on the socket to enable or disable the * possibility of using jumbo clusters. Caller must ensure to hold * the socket lock. */ void somultipages(struct socket *so, boolean_t set) { if (set) so->so_flags |= SOF_MULTIPAGES; else so->so_flags &= ~SOF_MULTIPAGES; } void soif2kcl(struct socket *so, boolean_t set) { if (set) so->so_flags1 |= SOF1_IF_2KCL; else so->so_flags1 &= ~SOF1_IF_2KCL; } int so_isdstlocal(struct socket *so) { struct inpcb *inp = (struct inpcb *)so->so_pcb; if (SOCK_DOM(so) == PF_INET) return (inaddr_local(inp->inp_faddr)); else if (SOCK_DOM(so) == PF_INET6) return (in6addr_local(&inp->in6p_faddr)); return (0); } int sosetdefunct(struct proc *p, struct socket *so, int level, boolean_t noforce) { struct sockbuf *rcv, *snd; int err = 0, defunct; rcv = &so->so_rcv; snd = &so->so_snd; defunct = (so->so_flags & SOF_DEFUNCT); if (defunct) { if (!(snd->sb_flags & rcv->sb_flags & SB_DROP)) { panic("%s: SB_DROP not set", __func__); /* NOTREACHED */ } goto done; } if (so->so_flags & SOF_NODEFUNCT) { if (noforce) { err = EOPNOTSUPP; SODEFUNCTLOG(("%s[%d]: (target pid %d level %d) " "so 0x%llx [%d,%d] is not eligible for defunct " "(%d)\n", __func__, proc_selfpid(), proc_pid(p), level, (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), err)); return (err); } so->so_flags &= ~SOF_NODEFUNCT; SODEFUNCTLOG(("%s[%d]: (target pid %d level %d) so 0x%llx " "[%d,%d] defunct by force\n", __func__, proc_selfpid(), proc_pid(p), level, (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so))); } else if (so->so_flags1 & SOF1_EXTEND_BK_IDLE_WANTED) { struct inpcb *inp = (struct inpcb *)so->so_pcb; struct ifnet *ifp = inp->inp_last_outifp; if (ifp && IFNET_IS_CELLULAR(ifp)) { OSIncrementAtomic(&soextbkidlestat.so_xbkidle_nocell); } else if (so->so_flags & SOF_DELEGATED) { OSIncrementAtomic(&soextbkidlestat.so_xbkidle_nodlgtd); } else if (soextbkidlestat.so_xbkidle_time == 0) { OSIncrementAtomic(&soextbkidlestat.so_xbkidle_notime); } else if (noforce) { OSIncrementAtomic(&soextbkidlestat.so_xbkidle_active); so->so_flags1 |= SOF1_EXTEND_BK_IDLE_INPROG; so->so_extended_bk_start = net_uptime(); OSBitOrAtomic(P_LXBKIDLEINPROG, &p->p_ladvflag); inpcb_timer_sched(inp->inp_pcbinfo, INPCB_TIMER_LAZY); err = EOPNOTSUPP; SODEFUNCTLOG(("%s[%d]: (target pid %d level %d) " "extend bk idle " "so 0x%llx rcv hw %d cc %d\n", __func__, proc_selfpid(), proc_pid(p), level, (uint64_t)DEBUG_KERNEL_ADDRPERM(so), so->so_rcv.sb_hiwat, so->so_rcv.sb_cc)); return (err); } else { OSIncrementAtomic(&soextbkidlestat.so_xbkidle_forced); } } so->so_flags |= SOF_DEFUNCT; /* Prevent further data from being appended to the socket buffers */ snd->sb_flags |= SB_DROP; rcv->sb_flags |= SB_DROP; /* Flush any existing data in the socket buffers */ if (rcv->sb_cc != 0) { rcv->sb_flags &= ~SB_SEL; selthreadclear(&rcv->sb_sel); sbrelease(rcv); } if (snd->sb_cc != 0) { snd->sb_flags &= ~SB_SEL; selthreadclear(&snd->sb_sel); sbrelease(snd); } done: SODEFUNCTLOG(("%s[%d]: (target pid %d level %d) so 0x%llx [%d,%d] %s " "defunct%s\n", __func__, proc_selfpid(), proc_pid(p), level, (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), defunct ? "is already" : "marked as", (so->so_flags1 & SOF1_EXTEND_BK_IDLE_WANTED) ? " extbkidle" : "")); return (err); } int sodefunct(struct proc *p, struct socket *so, int level) { struct sockbuf *rcv, *snd; if (!(so->so_flags & SOF_DEFUNCT)) { panic("%s improperly called", __func__); /* NOTREACHED */ } if (so->so_state & SS_DEFUNCT) goto done; rcv = &so->so_rcv; snd = &so->so_snd; if (SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) { char s[MAX_IPv6_STR_LEN]; char d[MAX_IPv6_STR_LEN]; struct inpcb *inp = sotoinpcb(so); SODEFUNCTLOG(("%s[%d]: (target pid %d level %d) so 0x%llx [%s " "%s:%d -> %s:%d] is now defunct [rcv_si 0x%x, snd_si 0x%x, " "rcv_fl 0x%x, snd_fl 0x%x]\n", __func__, proc_selfpid(), proc_pid(p), level, (uint64_t)DEBUG_KERNEL_ADDRPERM(so), (SOCK_TYPE(so) == SOCK_STREAM) ? "TCP" : "UDP", inet_ntop(SOCK_DOM(so), ((SOCK_DOM(so) == PF_INET) ? (void *)&inp->inp_laddr.s_addr : (void *)&inp->in6p_laddr), s, sizeof (s)), ntohs(inp->in6p_lport), inet_ntop(SOCK_DOM(so), (SOCK_DOM(so) == PF_INET) ? (void *)&inp->inp_faddr.s_addr : (void *)&inp->in6p_faddr, d, sizeof (d)), ntohs(inp->in6p_fport), (uint32_t)rcv->sb_sel.si_flags, (uint32_t)snd->sb_sel.si_flags, rcv->sb_flags, snd->sb_flags)); } else { SODEFUNCTLOG(("%s[%d]: (target pid %d level %d) so 0x%llx " "[%d,%d] is now defunct [rcv_si 0x%x, snd_si 0x%x, " "rcv_fl 0x%x, snd_fl 0x%x]\n", __func__, proc_selfpid(), proc_pid(p), level, (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), (uint32_t)rcv->sb_sel.si_flags, (uint32_t)snd->sb_sel.si_flags, rcv->sb_flags, snd->sb_flags)); } /* * Unwedge threads blocked on sbwait() and sb_lock(). */ sbwakeup(rcv); sbwakeup(snd); so->so_flags1 |= SOF1_DEFUNCTINPROG; if (rcv->sb_flags & SB_LOCK) sbunlock(rcv, TRUE); /* keep socket locked */ if (snd->sb_flags & SB_LOCK) sbunlock(snd, TRUE); /* keep socket locked */ /* * Flush the buffers and disconnect. We explicitly call shutdown * on both data directions to ensure that SS_CANT{RCV,SEND}MORE * states are set for the socket. This would also flush out data * hanging off the receive list of this socket. */ (void) soshutdownlock_final(so, SHUT_RD); (void) soshutdownlock_final(so, SHUT_WR); (void) sodisconnectlocked(so); /* * Explicitly handle connectionless-protocol disconnection * and release any remaining data in the socket buffers. */ if (!(so->so_flags & SS_ISDISCONNECTED)) (void) soisdisconnected(so); if (so->so_error == 0) so->so_error = EBADF; if (rcv->sb_cc != 0) { rcv->sb_flags &= ~SB_SEL; selthreadclear(&rcv->sb_sel); sbrelease(rcv); } if (snd->sb_cc != 0) { snd->sb_flags &= ~SB_SEL; selthreadclear(&snd->sb_sel); sbrelease(snd); } so->so_state |= SS_DEFUNCT; done: return (0); } int soresume(struct proc *p, struct socket *so, int locked) { if (locked == 0) socket_lock(so, 1); if (so->so_flags1 & SOF1_EXTEND_BK_IDLE_INPROG) { SODEFUNCTLOG(("%s[%d]: )target pid %d) so 0x%llx [%d,%d] " "resumed from bk idle\n", __func__, proc_selfpid(), proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so))); so->so_flags1 &= ~SOF1_EXTEND_BK_IDLE_INPROG; so->so_extended_bk_start = 0; OSBitAndAtomic(~P_LXBKIDLEINPROG, &p->p_ladvflag); OSIncrementAtomic(&soextbkidlestat.so_xbkidle_resumed); OSDecrementAtomic(&soextbkidlestat.so_xbkidle_active); VERIFY(soextbkidlestat.so_xbkidle_active >= 0); } if (locked == 0) socket_unlock(so, 1); return (0); } /* * Does not attempt to account for sockets that are delegated from * the current process */ int so_set_extended_bk_idle(struct socket *so, int optval) { int error = 0; if ((SOCK_DOM(so) != PF_INET && SOCK_DOM(so) != PF_INET6) || SOCK_PROTO(so) != IPPROTO_TCP) { OSDecrementAtomic(&soextbkidlestat.so_xbkidle_notsupp); error = EOPNOTSUPP; } else if (optval == 0) { so->so_flags1 &= ~SOF1_EXTEND_BK_IDLE_WANTED; soresume(current_proc(), so, 1); } else { struct proc *p = current_proc(); int i; struct filedesc *fdp; int count = 0; proc_fdlock(p); fdp = p->p_fd; for (i = 0; i < fdp->fd_nfiles; i++) { struct fileproc *fp = fdp->fd_ofiles[i]; struct socket *so2; if (fp == NULL || (fdp->fd_ofileflags[i] & UF_RESERVED) != 0 || FILEGLOB_DTYPE(fp->f_fglob) != DTYPE_SOCKET) continue; so2 = (struct socket *)fp->f_fglob->fg_data; if (so != so2 && so2->so_flags1 & SOF1_EXTEND_BK_IDLE_WANTED) count++; if (count >= soextbkidlestat.so_xbkidle_maxperproc) break; } if (count >= soextbkidlestat.so_xbkidle_maxperproc) { OSIncrementAtomic(&soextbkidlestat.so_xbkidle_toomany); error = EBUSY; } else if (so->so_flags & SOF_DELEGATED) { OSIncrementAtomic(&soextbkidlestat.so_xbkidle_nodlgtd); error = EBUSY; } else { so->so_flags1 |= SOF1_EXTEND_BK_IDLE_WANTED; OSIncrementAtomic(&soextbkidlestat.so_xbkidle_wantok); } SODEFUNCTLOG(("%s[%d]: so 0x%llx [%d,%d] " "%s marked for extended bk idle\n", __func__, proc_selfpid(), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), (so->so_flags1 & SOF1_EXTEND_BK_IDLE_WANTED) ? "is" : "not")); proc_fdunlock(p); } return (error); } static void so_stop_extended_bk_idle(struct socket *so) { so->so_flags1 &= ~SOF1_EXTEND_BK_IDLE_INPROG; so->so_extended_bk_start = 0; OSDecrementAtomic(&soextbkidlestat.so_xbkidle_active); VERIFY(soextbkidlestat.so_xbkidle_active >= 0); /* * Force defunct */ sosetdefunct(current_proc(), so, SHUTDOWN_SOCKET_LEVEL_DISCONNECT_INTERNAL, FALSE); if (so->so_flags & SOF_DEFUNCT) { sodefunct(current_proc(), so, SHUTDOWN_SOCKET_LEVEL_DISCONNECT_INTERNAL); } } void so_drain_extended_bk_idle(struct socket *so) { if (so && (so->so_flags1 & SOF1_EXTEND_BK_IDLE_INPROG)) { /* * Only penalize sockets that have outstanding data */ if (so->so_rcv.sb_cc || so->so_snd.sb_cc) { so_stop_extended_bk_idle(so); OSIncrementAtomic(&soextbkidlestat.so_xbkidle_drained); } } } /* * Return values tells if socket is still in extended background idle */ int so_check_extended_bk_idle_time(struct socket *so) { int ret = 1; if ((so->so_flags1 & SOF1_EXTEND_BK_IDLE_INPROG)) { SODEFUNCTLOG(("%s[%d]: so 0x%llx [%d,%d]\n", __func__, proc_selfpid(), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so))); if (net_uptime() - so->so_extended_bk_start > soextbkidlestat.so_xbkidle_time) { so_stop_extended_bk_idle(so); OSIncrementAtomic(&soextbkidlestat.so_xbkidle_expired); ret = 0; } else { struct inpcb *inp = (struct inpcb *)so->so_pcb; inpcb_timer_sched(inp->inp_pcbinfo, INPCB_TIMER_LAZY); OSIncrementAtomic(&soextbkidlestat.so_xbkidle_resched); } } return (ret); } void resume_proc_sockets(proc_t p) { if (p->p_ladvflag & P_LXBKIDLEINPROG) { struct filedesc *fdp; int i; proc_fdlock(p); fdp = p->p_fd; for (i = 0; i < fdp->fd_nfiles; i++) { struct fileproc *fp; struct socket *so; fp = fdp->fd_ofiles[i]; if (fp == NULL || (fdp->fd_ofileflags[i] & UF_RESERVED) != 0 || FILEGLOB_DTYPE(fp->f_fglob) != DTYPE_SOCKET) continue; so = (struct socket *)fp->f_fglob->fg_data; (void) soresume(p, so, 0); } proc_fdunlock(p); OSBitAndAtomic(~P_LXBKIDLEINPROG, &p->p_ladvflag); } } __private_extern__ int so_set_recv_anyif(struct socket *so, int optval) { int ret = 0; #if INET6 if (SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) { #else if (SOCK_DOM(so) == PF_INET) { #endif /* !INET6 */ if (optval) sotoinpcb(so)->inp_flags |= INP_RECV_ANYIF; else sotoinpcb(so)->inp_flags &= ~INP_RECV_ANYIF; } return (ret); } __private_extern__ int so_get_recv_anyif(struct socket *so) { int ret = 0; #if INET6 if (SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) { #else if (SOCK_DOM(so) == PF_INET) { #endif /* !INET6 */ ret = (sotoinpcb(so)->inp_flags & INP_RECV_ANYIF) ? 1 : 0; } return (ret); } int so_set_restrictions(struct socket *so, uint32_t vals) { int nocell_old, nocell_new; int noexpensive_old, noexpensive_new; /* * Deny-type restrictions are trapdoors; once set they cannot be * unset for the lifetime of the socket. This allows them to be * issued by a framework on behalf of the application without * having to worry that they can be undone. * * Note here that socket-level restrictions overrides any protocol * level restrictions. For instance, SO_RESTRICT_DENY_CELLULAR * socket restriction issued on the socket has a higher precendence * than INP_NO_IFT_CELLULAR. The latter is affected by the UUID * policy PROC_UUID_NO_CELLULAR for unrestricted sockets only, * i.e. when SO_RESTRICT_DENY_CELLULAR has not been issued. */ nocell_old = (so->so_restrictions & SO_RESTRICT_DENY_CELLULAR); noexpensive_old = (so->so_restrictions & SO_RESTRICT_DENY_EXPENSIVE); so->so_restrictions |= (vals & (SO_RESTRICT_DENY_IN | SO_RESTRICT_DENY_OUT | SO_RESTRICT_DENY_CELLULAR | SO_RESTRICT_DENY_EXPENSIVE)); nocell_new = (so->so_restrictions & SO_RESTRICT_DENY_CELLULAR); noexpensive_new = (so->so_restrictions & SO_RESTRICT_DENY_EXPENSIVE); /* we can only set, not clear restrictions */ if ((nocell_new - nocell_old) == 0 && (noexpensive_new - noexpensive_old) == 0) return (0); #if INET6 if (SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) { #else if (SOCK_DOM(so) == PF_INET) { #endif /* !INET6 */ if (nocell_new - nocell_old != 0) { /* * if deny cellular is now set, do what's needed * for INPCB */ inp_set_nocellular(sotoinpcb(so)); } if (noexpensive_new - noexpensive_old != 0) { inp_set_noexpensive(sotoinpcb(so)); } } return (0); } uint32_t so_get_restrictions(struct socket *so) { return (so->so_restrictions & (SO_RESTRICT_DENY_IN | SO_RESTRICT_DENY_OUT | SO_RESTRICT_DENY_CELLULAR | SO_RESTRICT_DENY_EXPENSIVE)); } struct sockaddr_entry * sockaddrentry_alloc(int how) { struct sockaddr_entry *se; se = (how == M_WAITOK) ? zalloc(se_zone) : zalloc_noblock(se_zone); if (se != NULL) bzero(se, se_zone_size); return (se); } void sockaddrentry_free(struct sockaddr_entry *se) { if (se->se_addr != NULL) { FREE(se->se_addr, M_SONAME); se->se_addr = NULL; } zfree(se_zone, se); } struct sockaddr_entry * sockaddrentry_dup(const struct sockaddr_entry *src_se, int how) { struct sockaddr_entry *dst_se; dst_se = sockaddrentry_alloc(how); if (dst_se != NULL) { int len = src_se->se_addr->sa_len; MALLOC(dst_se->se_addr, struct sockaddr *, len, M_SONAME, how | M_ZERO); if (dst_se->se_addr != NULL) { bcopy(src_se->se_addr, dst_se->se_addr, len); } else { sockaddrentry_free(dst_se); dst_se = NULL; } } return (dst_se); } struct sockaddr_list * sockaddrlist_alloc(int how) { struct sockaddr_list *sl; sl = (how == M_WAITOK) ? zalloc(sl_zone) : zalloc_noblock(sl_zone); if (sl != NULL) { bzero(sl, sl_zone_size); TAILQ_INIT(&sl->sl_head); } return (sl); } void sockaddrlist_free(struct sockaddr_list *sl) { struct sockaddr_entry *se, *tse; TAILQ_FOREACH_SAFE(se, &sl->sl_head, se_link, tse) { sockaddrlist_remove(sl, se); sockaddrentry_free(se); } VERIFY(sl->sl_cnt == 0 && TAILQ_EMPTY(&sl->sl_head)); zfree(sl_zone, sl); } void sockaddrlist_insert(struct sockaddr_list *sl, struct sockaddr_entry *se) { VERIFY(!(se->se_flags & SEF_ATTACHED)); se->se_flags |= SEF_ATTACHED; TAILQ_INSERT_TAIL(&sl->sl_head, se, se_link); sl->sl_cnt++; VERIFY(sl->sl_cnt != 0); } void sockaddrlist_remove(struct sockaddr_list *sl, struct sockaddr_entry *se) { VERIFY(se->se_flags & SEF_ATTACHED); se->se_flags &= ~SEF_ATTACHED; VERIFY(sl->sl_cnt != 0); sl->sl_cnt--; TAILQ_REMOVE(&sl->sl_head, se, se_link); } struct sockaddr_list * sockaddrlist_dup(const struct sockaddr_list *src_sl, int how) { struct sockaddr_entry *src_se, *tse; struct sockaddr_list *dst_sl; dst_sl = sockaddrlist_alloc(how); if (dst_sl == NULL) return (NULL); TAILQ_FOREACH_SAFE(src_se, &src_sl->sl_head, se_link, tse) { struct sockaddr_entry *dst_se; if (src_se->se_addr == NULL) continue; dst_se = sockaddrentry_dup(src_se, how); if (dst_se == NULL) { sockaddrlist_free(dst_sl); return (NULL); } sockaddrlist_insert(dst_sl, dst_se); } VERIFY(src_sl->sl_cnt == dst_sl->sl_cnt); return (dst_sl); } int so_set_effective_pid(struct socket *so, int epid, struct proc *p) { struct proc *ep = PROC_NULL; int error = 0; /* pid 0 is reserved for kernel */ if (epid == 0) { error = EINVAL; goto done; } /* * If this is an in-kernel socket, prevent its delegate * association from changing unless the socket option is * coming from within the kernel itself. */ if (so->last_pid == 0 && p != kernproc) { error = EACCES; goto done; } /* * If this is issued by a process that's recorded as the * real owner of the socket, or if the pid is the same as * the process's own pid, then proceed. Otherwise ensure * that the issuing process has the necessary privileges. */ if (epid != so->last_pid || epid != proc_pid(p)) { if ((error = priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_SOCKET_DELEGATE, 0))) { error = EACCES; goto done; } } /* Find the process that corresponds to the effective pid */ if ((ep = proc_find(epid)) == PROC_NULL) { error = ESRCH; goto done; } /* * If a process tries to delegate the socket to itself, then * there's really nothing to do; treat it as a way for the * delegate association to be cleared. Note that we check * the passed-in proc rather than calling proc_selfpid(), * as we need to check the process issuing the socket option * which could be kernproc. Given that we don't allow 0 for * effective pid, it means that a delegated in-kernel socket * stays delegated during its lifetime (which is probably OK.) */ if (epid == proc_pid(p)) { so->so_flags &= ~SOF_DELEGATED; so->e_upid = 0; so->e_pid = 0; uuid_clear(so->e_uuid); } else { so->so_flags |= SOF_DELEGATED; so->e_upid = proc_uniqueid(ep); so->e_pid = proc_pid(ep); proc_getexecutableuuid(ep, so->e_uuid, sizeof (so->e_uuid)); } done: if (error == 0 && net_io_policy_log) { uuid_string_t buf; uuid_unparse(so->e_uuid, buf); log(LOG_DEBUG, "%s[%s,%d]: so 0x%llx [%d,%d] epid %d (%s) " "euuid %s%s\n", __func__, proc_name_address(p), proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), so->e_pid, proc_name_address(ep), buf, ((so->so_flags & SOF_DELEGATED) ? " [delegated]" : "")); } else if (error != 0 && net_io_policy_log) { log(LOG_ERR, "%s[%s,%d]: so 0x%llx [%d,%d] epid %d (%s) " "ERROR (%d)\n", __func__, proc_name_address(p), proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), epid, (ep == PROC_NULL) ? "PROC_NULL" : proc_name_address(ep), error); } /* Update this socket's policy upon success */ if (error == 0) { so->so_policy_gencnt *= -1; so_update_policy(so); #if NECP so_update_necp_policy(so, NULL, NULL); #endif /* NECP */ } if (ep != PROC_NULL) proc_rele(ep); return (error); } int so_set_effective_uuid(struct socket *so, uuid_t euuid, struct proc *p) { uuid_string_t buf; uuid_t uuid; int error = 0; /* UUID must not be all-zeroes (reserved for kernel) */ if (uuid_is_null(euuid)) { error = EINVAL; goto done; } /* * If this is an in-kernel socket, prevent its delegate * association from changing unless the socket option is * coming from within the kernel itself. */ if (so->last_pid == 0 && p != kernproc) { error = EACCES; goto done; } /* Get the UUID of the issuing process */ proc_getexecutableuuid(p, uuid, sizeof (uuid)); /* * If this is issued by a process that's recorded as the * real owner of the socket, or if the uuid is the same as * the process's own uuid, then proceed. Otherwise ensure * that the issuing process has the necessary privileges. */ if (uuid_compare(euuid, so->last_uuid) != 0 || uuid_compare(euuid, uuid) != 0) { if ((error = priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_SOCKET_DELEGATE, 0))) { error = EACCES; goto done; } } /* * If a process tries to delegate the socket to itself, then * there's really nothing to do; treat it as a way for the * delegate association to be cleared. Note that we check * the uuid of the passed-in proc rather than that of the * current process, as we need to check the process issuing * the socket option which could be kernproc itself. Given * that we don't allow 0 for effective uuid, it means that * a delegated in-kernel socket stays delegated during its * lifetime (which is okay.) */ if (uuid_compare(euuid, uuid) == 0) { so->so_flags &= ~SOF_DELEGATED; so->e_upid = 0; so->e_pid = 0; uuid_clear(so->e_uuid); } else { so->so_flags |= SOF_DELEGATED; /* * Unlike so_set_effective_pid(), we only have the UUID * here and the process ID is not known. Inherit the * real {pid,upid} of the socket. */ so->e_upid = so->last_upid; so->e_pid = so->last_pid; uuid_copy(so->e_uuid, euuid); } done: if (error == 0 && net_io_policy_log) { uuid_unparse(so->e_uuid, buf); log(LOG_DEBUG, "%s[%s,%d]: so 0x%llx [%d,%d] epid %d " "euuid %s%s\n", __func__, proc_name_address(p), proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), so->e_pid, buf, ((so->so_flags & SOF_DELEGATED) ? " [delegated]" : "")); } else if (error != 0 && net_io_policy_log) { uuid_unparse(euuid, buf); log(LOG_DEBUG, "%s[%s,%d]: so 0x%llx [%d,%d] euuid %s " "ERROR (%d)\n", __func__, proc_name_address(p), proc_pid(p), (uint64_t)DEBUG_KERNEL_ADDRPERM(so), SOCK_DOM(so), SOCK_TYPE(so), buf, error); } /* Update this socket's policy upon success */ if (error == 0) { so->so_policy_gencnt *= -1; so_update_policy(so); #if NECP so_update_necp_policy(so, NULL, NULL); #endif /* NECP */ } return (error); } void netpolicy_post_msg(uint32_t ev_code, struct netpolicy_event_data *ev_data, uint32_t ev_datalen) { struct kev_msg ev_msg; /* * A netpolicy event always starts with a netpolicy_event_data * structure, but the caller can provide for a longer event * structure to post, depending on the event code. */ VERIFY(ev_data != NULL && ev_datalen >= sizeof (*ev_data)); bzero(&ev_msg, sizeof (ev_msg)); ev_msg.vendor_code = KEV_VENDOR_APPLE; ev_msg.kev_class = KEV_NETWORK_CLASS; ev_msg.kev_subclass = KEV_NETPOLICY_SUBCLASS; ev_msg.event_code = ev_code; ev_msg.dv[0].data_ptr = ev_data; ev_msg.dv[0].data_length = ev_datalen; kev_post_msg(&ev_msg); } void socket_post_kev_msg(uint32_t ev_code, struct kev_socket_event_data *ev_data, uint32_t ev_datalen) { struct kev_msg ev_msg; bzero(&ev_msg, sizeof(ev_msg)); ev_msg.vendor_code = KEV_VENDOR_APPLE; ev_msg.kev_class = KEV_NETWORK_CLASS; ev_msg.kev_subclass = KEV_SOCKET_SUBCLASS; ev_msg.event_code = ev_code; ev_msg.dv[0].data_ptr = ev_data; ev_msg.dv[0]. data_length = ev_datalen; kev_post_msg(&ev_msg); } void socket_post_kev_msg_closed(struct socket *so) { struct kev_socket_closed ev; struct sockaddr *socksa = NULL, *peersa = NULL; int err; bzero(&ev, sizeof(ev)); err = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &socksa); if (err == 0) { err = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &peersa); if (err == 0) { memcpy(&ev.ev_data.kev_sockname, socksa, min(socksa->sa_len, sizeof (ev.ev_data.kev_sockname))); memcpy(&ev.ev_data.kev_peername, peersa, min(peersa->sa_len, sizeof (ev.ev_data.kev_peername))); socket_post_kev_msg(KEV_SOCKET_CLOSED, &ev.ev_data, sizeof (ev)); } } if (socksa != NULL) FREE(socksa, M_SONAME); if (peersa != NULL) FREE(peersa, M_SONAME); } |