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 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 | /* * Copyright (c) 2004-2021 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@ */ /* $NetBSD: if_bridge.c,v 1.31 2005/06/01 19:45:34 jdc Exp $ */ /* * Copyright 2001 Wasabi Systems, Inc. * All rights reserved. * * Written by Jason R. Thorpe for Wasabi Systems, Inc. * * 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 for the NetBSD Project by * Wasabi Systems, Inc. * 4. The name of Wasabi Systems, Inc. may not be used to endorse * or promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC * 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. */ /* * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net) * 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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. * * OpenBSD: if_bridge.c,v 1.60 2001/06/15 03:38:33 itojun Exp */ /* * Network interface bridge support. * * TODO: * * - Currently only supports Ethernet-like interfaces (Ethernet, * 802.11, VLANs on Ethernet, etc.) Figure out a nice way * to bridge other types of interfaces (FDDI-FDDI, and maybe * consider heterogenous bridges). * * - GIF isn't handled due to the lack of IPPROTO_ETHERIP support. */ #include <sys/cdefs.h> #include <sys/param.h> #include <sys/mbuf.h> #include <sys/malloc.h> #include <sys/protosw.h> #include <sys/systm.h> #include <sys/time.h> #include <sys/socket.h> /* for net/if.h */ #include <sys/sockio.h> #include <sys/kernel.h> #include <sys/random.h> #include <sys/syslog.h> #include <sys/sysctl.h> #include <sys/proc.h> #include <sys/lock.h> #include <sys/mcache.h> #include <sys/kauth.h> #include <kern/thread_call.h> #include <libkern/libkern.h> #include <kern/zalloc.h> #if NBPFILTER > 0 #include <net/bpf.h> #endif #include <net/if.h> #include <net/if_dl.h> #include <net/if_types.h> #include <net/if_var.h> #include <net/if_media.h> #include <net/net_api_stats.h> #include <net/pfvar.h> #include <netinet/in.h> /* for struct arpcom */ #include <netinet/tcp.h> /* for struct tcphdr */ #include <netinet/in_systm.h> #include <netinet/in_var.h> #define _IP_VHL #include <netinet/ip.h> #include <netinet/ip_var.h> #include <netinet/ip6.h> #include <netinet6/ip6_var.h> #ifdef DEV_CARP #include <netinet/ip_carp.h> #endif #include <netinet/if_ether.h> /* for struct arpcom */ #include <net/bridgestp.h> #include <net/if_bridgevar.h> #include <net/if_llc.h> #if NVLAN > 0 #include <net/if_vlan_var.h> #endif /* NVLAN > 0 */ #include <net/if_ether.h> #include <net/dlil.h> #include <net/kpi_interfacefilter.h> #include <net/route.h> #include <dev/random/randomdev.h> #include <netinet/bootp.h> #include <netinet/dhcp.h> #if SKYWALK #include <skywalk/nexus/netif/nx_netif.h> #endif /* SKYWALK */ #include <os/log.h> /* * if_bridge_debug, BR_DBGF_* * - 'if_bridge_debug' is a bitmask of BR_DBGF_* flags that can be set * to enable additional logs for the corresponding bridge function * - "sysctl net.link.bridge.debug" controls the value of * 'if_bridge_debug' */ static uint32_t if_bridge_debug = 0; #define BR_DBGF_LIFECYCLE 0x0001 #define BR_DBGF_INPUT 0x0002 #define BR_DBGF_OUTPUT 0x0004 #define BR_DBGF_RT_TABLE 0x0008 #define BR_DBGF_DELAYED_CALL 0x0010 #define BR_DBGF_IOCTL 0x0020 #define BR_DBGF_MBUF 0x0040 #define BR_DBGF_MCAST 0x0080 #define BR_DBGF_HOSTFILTER 0x0100 #define BR_DBGF_CHECKSUM 0x0200 #define BR_DBGF_MAC_NAT 0x0400 /* * if_bridge_log_level * - 'if_bridge_log_level' ensures that by default important logs are * logged regardless of if_bridge_debug by comparing the log level * in BRIDGE_LOG to if_bridge_log_level * - use "sysctl net.link.bridge.log_level" controls the value of * 'if_bridge_log_level' * - the default value of 'if_bridge_log_level' is LOG_NOTICE; important * logs must use LOG_NOTICE to ensure they appear by default */ static int if_bridge_log_level = LOG_NOTICE; #define BRIDGE_DBGF_ENABLED(__flag) ((if_bridge_debug & __flag) != 0) /* * BRIDGE_LOG, BRIDGE_LOG_SIMPLE * - macros to generate the specified log conditionally based on * the specified log level and debug flags * - BRIDGE_LOG_SIMPLE does not include the function name in the log */ #define BRIDGE_LOG(__level, __dbgf, __string, ...) \ do { \ if (__level <= if_bridge_log_level || \ BRIDGE_DBGF_ENABLED(__dbgf)) { \ os_log(OS_LOG_DEFAULT, "%s: " __string, \ __func__, ## __VA_ARGS__); \ } \ } while (0) #define BRIDGE_LOG_SIMPLE(__level, __dbgf, __string, ...) \ do { \ if (__level <= if_bridge_log_level || \ BRIDGE_DBGF_ENABLED(__dbgf)) { \ os_log(OS_LOG_DEFAULT, __string, ## __VA_ARGS__); \ } \ } while (0) #define _BRIDGE_LOCK(_sc) lck_mtx_lock(&(_sc)->sc_mtx) #define _BRIDGE_UNLOCK(_sc) lck_mtx_unlock(&(_sc)->sc_mtx) #define BRIDGE_LOCK_ASSERT_HELD(_sc) \ LCK_MTX_ASSERT(&(_sc)->sc_mtx, LCK_MTX_ASSERT_OWNED) #define BRIDGE_LOCK_ASSERT_NOTHELD(_sc) \ LCK_MTX_ASSERT(&(_sc)->sc_mtx, LCK_MTX_ASSERT_NOTOWNED) #define BRIDGE_LOCK_DEBUG 1 #if BRIDGE_LOCK_DEBUG #define BR_LCKDBG_MAX 4 #define BRIDGE_LOCK(_sc) bridge_lock(_sc) #define BRIDGE_UNLOCK(_sc) bridge_unlock(_sc) #define BRIDGE_LOCK2REF(_sc, _err) _err = bridge_lock2ref(_sc) #define BRIDGE_UNREF(_sc) bridge_unref(_sc) #define BRIDGE_XLOCK(_sc) bridge_xlock(_sc) #define BRIDGE_XDROP(_sc) bridge_xdrop(_sc) #else /* !BRIDGE_LOCK_DEBUG */ #define BRIDGE_LOCK(_sc) _BRIDGE_LOCK(_sc) #define BRIDGE_UNLOCK(_sc) _BRIDGE_UNLOCK(_sc) #define BRIDGE_LOCK2REF(_sc, _err) do { \ BRIDGE_LOCK_ASSERT_HELD(_sc); \ if ((_sc)->sc_iflist_xcnt > 0) \ (_err) = EBUSY; \ else { \ (_sc)->sc_iflist_ref++; \ (_err) = 0; \ } \ _BRIDGE_UNLOCK(_sc); \ } while (0) #define BRIDGE_UNREF(_sc) do { \ _BRIDGE_LOCK(_sc); \ (_sc)->sc_iflist_ref--; \ if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0)) { \ _BRIDGE_UNLOCK(_sc); \ wakeup(&(_sc)->sc_cv); \ } else \ _BRIDGE_UNLOCK(_sc); \ } while (0) #define BRIDGE_XLOCK(_sc) do { \ BRIDGE_LOCK_ASSERT_HELD(_sc); \ (_sc)->sc_iflist_xcnt++; \ while ((_sc)->sc_iflist_ref > 0) \ msleep(&(_sc)->sc_cv, &(_sc)->sc_mtx, PZERO, \ "BRIDGE_XLOCK", NULL); \ } while (0) #define BRIDGE_XDROP(_sc) do { \ BRIDGE_LOCK_ASSERT_HELD(_sc); \ (_sc)->sc_iflist_xcnt--; \ } while (0) #endif /* BRIDGE_LOCK_DEBUG */ #if NBPFILTER > 0 #define BRIDGE_BPF_MTAP_INPUT(sc, m) \ if (sc->sc_bpf_input != NULL) \ bridge_bpf_input(sc->sc_ifp, m, __func__, __LINE__) #else /* NBPFILTER */ #define BRIDGE_BPF_MTAP_INPUT(ifp, m) #endif /* NBPFILTER */ /* * Initial size of the route hash table. Must be a power of two. */ #ifndef BRIDGE_RTHASH_SIZE #define BRIDGE_RTHASH_SIZE 16 #endif /* * Maximum size of the routing hash table */ #define BRIDGE_RTHASH_SIZE_MAX 2048 #define BRIDGE_RTHASH_MASK(sc) ((sc)->sc_rthash_size - 1) /* * Maximum number of addresses to cache. */ #ifndef BRIDGE_RTABLE_MAX #define BRIDGE_RTABLE_MAX 100 #endif /* * Timeout (in seconds) for entries learned dynamically. */ #ifndef BRIDGE_RTABLE_TIMEOUT #define BRIDGE_RTABLE_TIMEOUT (20 * 60) /* same as ARP */ #endif /* * Number of seconds between walks of the route list. */ #ifndef BRIDGE_RTABLE_PRUNE_PERIOD #define BRIDGE_RTABLE_PRUNE_PERIOD (5 * 60) #endif /* * Number of MAC NAT entries * - sized based on 16 clients (including MAC NAT interface) * each with 4 addresses */ #ifndef BRIDGE_MAC_NAT_ENTRY_MAX #define BRIDGE_MAC_NAT_ENTRY_MAX 64 #endif /* BRIDGE_MAC_NAT_ENTRY_MAX */ /* * List of capabilities to possibly mask on the member interface. */ #define BRIDGE_IFCAPS_MASK (IFCAP_TSO | IFCAP_TXCSUM) /* * List of capabilities to disable on the member interface. */ #define BRIDGE_IFCAPS_STRIP IFCAP_LRO /* * Bridge interface list entry. */ struct bridge_iflist { TAILQ_ENTRY(bridge_iflist) bif_next; struct ifnet *bif_ifp; /* member if */ struct bstp_port bif_stp; /* STP state */ uint32_t bif_ifflags; /* member if flags */ int bif_savedcaps; /* saved capabilities */ uint32_t bif_addrmax; /* max # of addresses */ uint32_t bif_addrcnt; /* cur. # of addresses */ uint32_t bif_addrexceeded; /* # of address violations */ interface_filter_t bif_iff_ref; struct bridge_softc *bif_sc; uint32_t bif_flags; /* host filter */ struct in_addr bif_hf_ipsrc; uint8_t bif_hf_hwsrc[ETHER_ADDR_LEN]; struct ifbrmstats bif_stats; }; static inline bool bif_ifflags_are_set(struct bridge_iflist * bif, uint32_t flags) { return (bif->bif_ifflags & flags) == flags; } static inline bool bif_has_checksum_offload(struct bridge_iflist * bif) { return bif_ifflags_are_set(bif, IFBIF_CHECKSUM_OFFLOAD); } /* fake errors to make the code clearer */ #define _EBADIP EJUSTRETURN #define _EBADIPCHECKSUM EJUSTRETURN #define _EBADIPV6 EJUSTRETURN #define _EBADUDP EJUSTRETURN #define _EBADTCP EJUSTRETURN #define _EBADUDPCHECKSUM EJUSTRETURN #define _EBADTCPCHECKSUM EJUSTRETURN #define BIFF_PROMISC 0x01 /* promiscuous mode set */ #define BIFF_PROTO_ATTACHED 0x02 /* protocol attached */ #define BIFF_FILTER_ATTACHED 0x04 /* interface filter attached */ #define BIFF_MEDIA_ACTIVE 0x08 /* interface media active */ #define BIFF_HOST_FILTER 0x10 /* host filter enabled */ #define BIFF_HF_HWSRC 0x20 /* host filter source MAC is set */ #define BIFF_HF_IPSRC 0x40 /* host filter source IP is set */ #define BIFF_INPUT_BROADCAST 0x80 /* send broadcast packets in */ #define BIFF_IN_MEMBER_LIST 0x100 /* added to the member list */ #if SKYWALK #define BIFF_FLOWSWITCH_ATTACHED 0x1000 /* we attached the flowswitch */ #define BIFF_NETAGENT_REMOVED 0x2000 /* we removed the netagent */ #endif /* SKYWALK */ /* * mac_nat_entry * - translates between an IP address and MAC address on a specific * bridge interface member */ struct mac_nat_entry { LIST_ENTRY(mac_nat_entry) mne_list; /* list linkage */ struct bridge_iflist *mne_bif; /* originating interface */ unsigned long mne_expire; /* expiration time */ union { struct in_addr mneu_ip; /* originating IPv4 address */ struct in6_addr mneu_ip6; /* originating IPv6 address */ } mne_u; uint8_t mne_mac[ETHER_ADDR_LEN]; uint8_t mne_flags; uint8_t mne_reserved; }; #define mne_ip mne_u.mneu_ip #define mne_ip6 mne_u.mneu_ip6 #define MNE_FLAGS_IPV6 0x01 /* IPv6 address */ LIST_HEAD(mac_nat_entry_list, mac_nat_entry); /* * mac_nat_record * - used by bridge_mac_nat_output() to convey the translation that needs * to take place in bridge_mac_nat_translate * - holds enough information so that the translation can be done later without * holding the bridge lock */ struct mac_nat_record { uint16_t mnr_ether_type; union { uint16_t mnru_arp_offset; struct { uint16_t mnruip_dhcp_flags; uint16_t mnruip_udp_csum; uint8_t mnruip_header_len; } mnru_ip; struct { uint16_t mnruip6_icmp6_len; uint16_t mnruip6_lladdr_offset; uint8_t mnruip6_icmp6_type; uint8_t mnruip6_header_len; } mnru_ip6; } mnr_u; }; #define mnr_arp_offset mnr_u.mnru_arp_offset #define mnr_ip_header_len mnr_u.mnru_ip.mnruip_header_len #define mnr_ip_dhcp_flags mnr_u.mnru_ip.mnruip_dhcp_flags #define mnr_ip_udp_csum mnr_u.mnru_ip.mnruip_udp_csum #define mnr_ip6_icmp6_len mnr_u.mnru_ip6.mnruip6_icmp6_len #define mnr_ip6_icmp6_type mnr_u.mnru_ip6.mnruip6_icmp6_type #define mnr_ip6_header_len mnr_u.mnru_ip6.mnruip6_header_len #define mnr_ip6_lladdr_offset mnr_u.mnru_ip6.mnruip6_lladdr_offset /* * Bridge route node. */ struct bridge_rtnode { LIST_ENTRY(bridge_rtnode) brt_hash; /* hash table linkage */ LIST_ENTRY(bridge_rtnode) brt_list; /* list linkage */ struct bridge_iflist *brt_dst; /* destination if */ unsigned long brt_expire; /* expiration time */ uint8_t brt_flags; /* address flags */ uint8_t brt_addr[ETHER_ADDR_LEN]; uint16_t brt_vlan; /* vlan id */ }; #define brt_ifp brt_dst->bif_ifp /* * Bridge delayed function call context */ typedef void (*bridge_delayed_func_t)(struct bridge_softc *); struct bridge_delayed_call { struct bridge_softc *bdc_sc; bridge_delayed_func_t bdc_func; /* Function to call */ struct timespec bdc_ts; /* Time to call */ u_int32_t bdc_flags; thread_call_t bdc_thread_call; }; #define BDCF_OUTSTANDING 0x01 /* Delayed call has been scheduled */ #define BDCF_CANCELLING 0x02 /* May be waiting for call completion */ /* * Software state for each bridge. */ LIST_HEAD(_bridge_rtnode_list, bridge_rtnode); struct bridge_softc { struct ifnet *sc_ifp; /* make this an interface */ u_int32_t sc_flags; LIST_ENTRY(bridge_softc) sc_list; decl_lck_mtx_data(, sc_mtx); struct _bridge_rtnode_list *sc_rthash; /* our forwarding table */ struct _bridge_rtnode_list sc_rtlist; /* list version of above */ uint32_t sc_rthash_key; /* key for hash */ uint32_t sc_rthash_size; /* size of the hash table */ struct bridge_delayed_call sc_aging_timer; struct bridge_delayed_call sc_resize_call; TAILQ_HEAD(, bridge_iflist) sc_spanlist; /* span ports list */ struct bstp_state sc_stp; /* STP state */ bpf_packet_func sc_bpf_input; bpf_packet_func sc_bpf_output; void *sc_cv; uint32_t sc_brtmax; /* max # of addresses */ uint32_t sc_brtcnt; /* cur. # of addresses */ uint32_t sc_brttimeout; /* rt timeout in seconds */ uint32_t sc_iflist_ref; /* refcount for sc_iflist */ uint32_t sc_iflist_xcnt; /* refcount for sc_iflist */ TAILQ_HEAD(, bridge_iflist) sc_iflist; /* member interface list */ uint32_t sc_brtexceeded; /* # of cache drops */ uint32_t sc_filter_flags; /* ipf and flags */ struct ifnet *sc_ifaddr; /* member mac copied from */ u_char sc_defaddr[6]; /* Default MAC address */ char sc_if_xname[IFNAMSIZ]; struct bridge_iflist *sc_mac_nat_bif; /* single MAC NAT interface */ struct mac_nat_entry_list sc_mne_list; /* MAC NAT IPv4 */ struct mac_nat_entry_list sc_mne_list_v6;/* MAC NAT IPv6 */ uint32_t sc_mne_max; /* max # of entries */ uint32_t sc_mne_count; /* cur. # of entries */ uint32_t sc_mne_allocation_failures; #if BRIDGE_LOCK_DEBUG /* * Locking and unlocking calling history */ void *lock_lr[BR_LCKDBG_MAX]; int next_lock_lr; void *unlock_lr[BR_LCKDBG_MAX]; int next_unlock_lr; #endif /* BRIDGE_LOCK_DEBUG */ }; #define SCF_DETACHING 0x01 #define SCF_RESIZING 0x02 #define SCF_MEDIA_ACTIVE 0x04 typedef enum { CHECKSUM_OPERATION_NONE = 0, CHECKSUM_OPERATION_CLEAR_OFFLOAD = 1, CHECKSUM_OPERATION_FINALIZE = 2, CHECKSUM_OPERATION_COMPUTE = 3, } ChecksumOperation; union iphdr { struct ip *ip; struct ip6_hdr *ip6; void * ptr; }; typedef struct { u_int ip_hlen; /* IP header length */ u_int ip_pay_len; /* length of payload (exclusive of ip_hlen) */ u_int ip_opt_len; /* IPv6 options headers length */ uint8_t ip_proto; /* IPPROTO_TCP, IPPROTO_UDP, etc. */ bool ip_is_ipv4; bool ip_is_fragmented; union iphdr ip_hdr; /* pointer to IP header */ void * ip_proto_hdr; /* ptr to protocol header (TCP) */ } ip_packet_info, *ip_packet_info_t; struct bridge_hostfilter_stats bridge_hostfilter_stats; static LCK_GRP_DECLARE(bridge_lock_grp, "if_bridge"); #if BRIDGE_LOCK_DEBUG static LCK_ATTR_DECLARE(bridge_lock_attr, 0, 0); #else static LCK_ATTR_DECLARE(bridge_lock_attr, LCK_ATTR_DEBUG, 0); #endif static LCK_MTX_DECLARE_ATTR(bridge_list_mtx, &bridge_lock_grp, &bridge_lock_attr); static int bridge_rtable_prune_period = BRIDGE_RTABLE_PRUNE_PERIOD; static ZONE_DEFINE(bridge_rtnode_pool, "bridge_rtnode", sizeof(struct bridge_rtnode), ZC_NONE); static ZONE_DEFINE(bridge_mne_pool, "bridge_mac_nat_entry", sizeof(struct mac_nat_entry), ZC_NONE); static int bridge_clone_create(struct if_clone *, uint32_t, void *); static int bridge_clone_destroy(struct ifnet *); static errno_t bridge_ioctl(struct ifnet *, u_long, void *); #if HAS_IF_CAP static void bridge_mutecaps(struct bridge_softc *); static void bridge_set_ifcap(struct bridge_softc *, struct bridge_iflist *, int); #endif static errno_t bridge_set_tso(struct bridge_softc *); static void bridge_proto_attach_changed(struct ifnet *); static int bridge_init(struct ifnet *); #if HAS_BRIDGE_DUMMYNET static void bridge_dummynet(struct mbuf *, struct ifnet *); #endif static void bridge_ifstop(struct ifnet *, int); static int bridge_output(struct ifnet *, struct mbuf *); static void bridge_finalize_cksum(struct ifnet *, struct mbuf *); static void bridge_start(struct ifnet *); static errno_t bridge_input(struct ifnet *, mbuf_t *); static errno_t bridge_iff_input(void *, ifnet_t, protocol_family_t, mbuf_t *, char **); static errno_t bridge_iff_output(void *, ifnet_t, protocol_family_t, mbuf_t *); static errno_t bridge_member_output(struct bridge_softc *sc, ifnet_t ifp, mbuf_t *m); static int bridge_enqueue(ifnet_t, struct ifnet *, struct ifnet *, struct mbuf *, ChecksumOperation); static void bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int); static void bridge_forward(struct bridge_softc *, struct bridge_iflist *, struct mbuf *); static void bridge_aging_timer(struct bridge_softc *sc); static void bridge_broadcast(struct bridge_softc *, struct bridge_iflist *, struct mbuf *, int); static void bridge_span(struct bridge_softc *, struct mbuf *); static int bridge_rtupdate(struct bridge_softc *, const uint8_t *, uint16_t, struct bridge_iflist *, int, uint8_t); static struct ifnet *bridge_rtlookup(struct bridge_softc *, const uint8_t *, uint16_t); static void bridge_rttrim(struct bridge_softc *); static void bridge_rtage(struct bridge_softc *); static void bridge_rtflush(struct bridge_softc *, int); static int bridge_rtdaddr(struct bridge_softc *, const uint8_t *, uint16_t); static int bridge_rtable_init(struct bridge_softc *); static void bridge_rtable_fini(struct bridge_softc *); static void bridge_rthash_resize(struct bridge_softc *); static int bridge_rtnode_addr_cmp(const uint8_t *, const uint8_t *); static struct bridge_rtnode *bridge_rtnode_lookup(struct bridge_softc *, const uint8_t *, uint16_t); static int bridge_rtnode_hash(struct bridge_softc *, struct bridge_rtnode *); static int bridge_rtnode_insert(struct bridge_softc *, struct bridge_rtnode *); static void bridge_rtnode_destroy(struct bridge_softc *, struct bridge_rtnode *); #if BRIDGESTP static void bridge_rtable_expire(struct ifnet *, int); static void bridge_state_change(struct ifnet *, int); #endif /* BRIDGESTP */ static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *, const char *name); static struct bridge_iflist *bridge_lookup_member_if(struct bridge_softc *, struct ifnet *ifp); static void bridge_delete_member(struct bridge_softc *, struct bridge_iflist *); static void bridge_delete_span(struct bridge_softc *, struct bridge_iflist *); static int bridge_ioctl_add(struct bridge_softc *, void *); static int bridge_ioctl_del(struct bridge_softc *, void *); static int bridge_ioctl_gifflags(struct bridge_softc *, void *); static int bridge_ioctl_sifflags(struct bridge_softc *, void *); static int bridge_ioctl_scache(struct bridge_softc *, void *); static int bridge_ioctl_gcache(struct bridge_softc *, void *); static int bridge_ioctl_gifs32(struct bridge_softc *, void *); static int bridge_ioctl_gifs64(struct bridge_softc *, void *); static int bridge_ioctl_rts32(struct bridge_softc *, void *); static int bridge_ioctl_rts64(struct bridge_softc *, void *); static int bridge_ioctl_saddr32(struct bridge_softc *, void *); static int bridge_ioctl_saddr64(struct bridge_softc *, void *); static int bridge_ioctl_sto(struct bridge_softc *, void *); static int bridge_ioctl_gto(struct bridge_softc *, void *); static int bridge_ioctl_daddr32(struct bridge_softc *, void *); static int bridge_ioctl_daddr64(struct bridge_softc *, void *); static int bridge_ioctl_flush(struct bridge_softc *, void *); static int bridge_ioctl_gpri(struct bridge_softc *, void *); static int bridge_ioctl_spri(struct bridge_softc *, void *); static int bridge_ioctl_ght(struct bridge_softc *, void *); static int bridge_ioctl_sht(struct bridge_softc *, void *); static int bridge_ioctl_gfd(struct bridge_softc *, void *); static int bridge_ioctl_sfd(struct bridge_softc *, void *); static int bridge_ioctl_gma(struct bridge_softc *, void *); static int bridge_ioctl_sma(struct bridge_softc *, void *); static int bridge_ioctl_sifprio(struct bridge_softc *, void *); static int bridge_ioctl_sifcost(struct bridge_softc *, void *); static int bridge_ioctl_sifmaxaddr(struct bridge_softc *, void *); static int bridge_ioctl_addspan(struct bridge_softc *, void *); static int bridge_ioctl_delspan(struct bridge_softc *, void *); static int bridge_ioctl_gbparam32(struct bridge_softc *, void *); static int bridge_ioctl_gbparam64(struct bridge_softc *, void *); static int bridge_ioctl_grte(struct bridge_softc *, void *); static int bridge_ioctl_gifsstp32(struct bridge_softc *, void *); static int bridge_ioctl_gifsstp64(struct bridge_softc *, void *); static int bridge_ioctl_sproto(struct bridge_softc *, void *); static int bridge_ioctl_stxhc(struct bridge_softc *, void *); static int bridge_ioctl_purge(struct bridge_softc *sc, void *); static int bridge_ioctl_gfilt(struct bridge_softc *, void *); static int bridge_ioctl_sfilt(struct bridge_softc *, void *); static int bridge_ioctl_ghostfilter(struct bridge_softc *, void *); static int bridge_ioctl_shostfilter(struct bridge_softc *, void *); static int bridge_ioctl_gmnelist32(struct bridge_softc *, void *); static int bridge_ioctl_gmnelist64(struct bridge_softc *, void *); static int bridge_ioctl_gifstats32(struct bridge_softc *, void *); static int bridge_ioctl_gifstats64(struct bridge_softc *, void *); static int bridge_pf(struct mbuf **, struct ifnet *, uint32_t sc_filter_flags, int input); static int bridge_ip_checkbasic(struct mbuf **); static int bridge_ip6_checkbasic(struct mbuf **); static errno_t bridge_set_bpf_tap(ifnet_t, bpf_tap_mode, bpf_packet_func); static errno_t bridge_bpf_input(ifnet_t, struct mbuf *, const char *, int); static errno_t bridge_bpf_output(ifnet_t, struct mbuf *); static void bridge_detach(ifnet_t); static void bridge_link_event(struct ifnet *, u_int32_t); static void bridge_iflinkevent(struct ifnet *); static u_int32_t bridge_updatelinkstatus(struct bridge_softc *); static int interface_media_active(struct ifnet *); static void bridge_schedule_delayed_call(struct bridge_delayed_call *); static void bridge_cancel_delayed_call(struct bridge_delayed_call *); static void bridge_cleanup_delayed_call(struct bridge_delayed_call *); static int bridge_host_filter(struct bridge_iflist *, mbuf_t *); static errno_t bridge_mac_nat_enable(struct bridge_softc *, struct bridge_iflist *); static void bridge_mac_nat_disable(struct bridge_softc *sc); static void bridge_mac_nat_age_entries(struct bridge_softc *sc, unsigned long); static void bridge_mac_nat_populate_entries(struct bridge_softc *sc); static void bridge_mac_nat_flush_entries(struct bridge_softc *sc, struct bridge_iflist *); static ifnet_t bridge_mac_nat_input(struct bridge_softc *, mbuf_t *, boolean_t *); static boolean_t bridge_mac_nat_output(struct bridge_softc *, struct bridge_iflist *, mbuf_t *, struct mac_nat_record *); static void bridge_mac_nat_translate(mbuf_t *, struct mac_nat_record *, const caddr_t); static bool is_broadcast_ip_packet(mbuf_t *); static bool in_addr_is_ours(const struct in_addr); static bool in6_addr_is_ours(const struct in6_addr *, uint32_t); #define m_copypacket(m, how) m_copym(m, 0, M_COPYALL, how) static int gso_tcp(struct ifnet *ifp, struct mbuf **mp, u_int mac_hlen, bool is_ipv4, boolean_t is_tx); /* The default bridge vlan is 1 (IEEE 802.1Q-2003 Table 9-2) */ #define VLANTAGOF(_m) 0 u_int8_t bstp_etheraddr[ETHER_ADDR_LEN] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }; static u_int8_t ethernulladdr[ETHER_ADDR_LEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; #if BRIDGESTP static struct bstp_cb_ops bridge_ops = { .bcb_state = bridge_state_change, .bcb_rtage = bridge_rtable_expire }; #endif /* BRIDGESTP */ SYSCTL_DECL(_net_link); SYSCTL_NODE(_net_link, IFT_BRIDGE, bridge, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Bridge"); static int bridge_inherit_mac = 0; /* share MAC with first bridge member */ SYSCTL_INT(_net_link_bridge, OID_AUTO, inherit_mac, CTLFLAG_RW | CTLFLAG_LOCKED, &bridge_inherit_mac, 0, "Inherit MAC address from the first bridge member"); SYSCTL_INT(_net_link_bridge, OID_AUTO, rtable_prune_period, CTLFLAG_RW | CTLFLAG_LOCKED, &bridge_rtable_prune_period, 0, "Interval between pruning of routing table"); static unsigned int bridge_rtable_hash_size_max = BRIDGE_RTHASH_SIZE_MAX; SYSCTL_UINT(_net_link_bridge, OID_AUTO, rtable_hash_size_max, CTLFLAG_RW | CTLFLAG_LOCKED, &bridge_rtable_hash_size_max, 0, "Maximum size of the routing hash table"); #if BRIDGE_DELAYED_CALLBACK_DEBUG static int bridge_delayed_callback_delay = 0; SYSCTL_INT(_net_link_bridge, OID_AUTO, delayed_callback_delay, CTLFLAG_RW | CTLFLAG_LOCKED, &bridge_delayed_callback_delay, 0, "Delay before calling delayed function"); #endif SYSCTL_STRUCT(_net_link_bridge, OID_AUTO, hostfilterstats, CTLFLAG_RD | CTLFLAG_LOCKED, &bridge_hostfilter_stats, bridge_hostfilter_stats, ""); #if BRIDGESTP static int log_stp = 0; /* log STP state changes */ SYSCTL_INT(_net_link_bridge, OID_AUTO, log_stp, CTLFLAG_RW, &log_stp, 0, "Log STP state changes"); #endif /* BRIDGESTP */ struct bridge_control { int (*bc_func)(struct bridge_softc *, void *); unsigned int bc_argsize; unsigned int bc_flags; }; #define VMNET_TAG "com.apple.vmnet" #define VMNET_LOCAL_TAG VMNET_TAG ".local" #define VMNET_BROADCAST_TAG VMNET_TAG ".broadcast" #define VMNET_MULTICAST_TAG VMNET_TAG ".multicast" static u_int16_t vmnet_tag; static u_int16_t vmnet_local_tag; static u_int16_t vmnet_broadcast_tag; static u_int16_t vmnet_multicast_tag; static u_int16_t allocate_pf_tag(char * name) { u_int16_t tag; tag = pf_tagname2tag_ext(name); BRIDGE_LOG(LOG_NOTICE, 0, "%s %d", name, tag); return tag; } static void allocate_vmnet_pf_tags(void) { /* allocate tags to use with PF */ if (vmnet_tag == 0) { vmnet_tag = allocate_pf_tag(VMNET_TAG); } if (vmnet_local_tag == 0) { vmnet_local_tag = allocate_pf_tag(VMNET_LOCAL_TAG); } if (vmnet_broadcast_tag == 0) { vmnet_broadcast_tag = allocate_pf_tag(VMNET_BROADCAST_TAG); } if (vmnet_multicast_tag == 0) { vmnet_multicast_tag = allocate_pf_tag(VMNET_MULTICAST_TAG); } } #define BC_F_COPYIN 0x01 /* copy arguments in */ #define BC_F_COPYOUT 0x02 /* copy arguments out */ #define BC_F_SUSER 0x04 /* do super-user check */ static const struct bridge_control bridge_control_table32[] = { { .bc_func = bridge_ioctl_add, .bc_argsize = sizeof(struct ifbreq), /* 0 */ .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_del, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gifflags, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sifflags, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_scache, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gcache, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_gifs32, .bc_argsize = sizeof(struct ifbifconf32), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_rts32, .bc_argsize = sizeof(struct ifbaconf32), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_saddr32, .bc_argsize = sizeof(struct ifbareq32), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_sto, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gto, .bc_argsize = sizeof(struct ifbrparam), /* 10 */ .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_daddr32, .bc_argsize = sizeof(struct ifbareq32), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_flush, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gpri, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_spri, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_ght, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sht, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gfd, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sfd, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gma, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sma, .bc_argsize = sizeof(struct ifbrparam), /* 20 */ .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_sifprio, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_sifcost, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gfilt, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sfilt, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_purge, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_addspan, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_delspan, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gbparam32, .bc_argsize = sizeof(struct ifbropreq32), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_grte, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_gifsstp32, .bc_argsize = sizeof(struct ifbpstpconf32), /* 30 */ .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sproto, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_stxhc, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_sifmaxaddr, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_ghostfilter, .bc_argsize = sizeof(struct ifbrhostfilter), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_shostfilter, .bc_argsize = sizeof(struct ifbrhostfilter), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gmnelist32, .bc_argsize = sizeof(struct ifbrmnelist32), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_gifstats32, .bc_argsize = sizeof(struct ifbrmreq32), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, }; static const struct bridge_control bridge_control_table64[] = { { .bc_func = bridge_ioctl_add, .bc_argsize = sizeof(struct ifbreq), /* 0 */ .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_del, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gifflags, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sifflags, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_scache, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gcache, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_gifs64, .bc_argsize = sizeof(struct ifbifconf64), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_rts64, .bc_argsize = sizeof(struct ifbaconf64), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_saddr64, .bc_argsize = sizeof(struct ifbareq64), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_sto, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gto, .bc_argsize = sizeof(struct ifbrparam), /* 10 */ .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_daddr64, .bc_argsize = sizeof(struct ifbareq64), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_flush, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gpri, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_spri, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_ght, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sht, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gfd, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sfd, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gma, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sma, .bc_argsize = sizeof(struct ifbrparam), /* 20 */ .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_sifprio, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_sifcost, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gfilt, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sfilt, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_purge, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_addspan, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_delspan, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gbparam64, .bc_argsize = sizeof(struct ifbropreq64), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_grte, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYOUT }, { .bc_func = bridge_ioctl_gifsstp64, .bc_argsize = sizeof(struct ifbpstpconf64), /* 30 */ .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_sproto, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_stxhc, .bc_argsize = sizeof(struct ifbrparam), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_sifmaxaddr, .bc_argsize = sizeof(struct ifbreq), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_ghostfilter, .bc_argsize = sizeof(struct ifbrhostfilter), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_shostfilter, .bc_argsize = sizeof(struct ifbrhostfilter), .bc_flags = BC_F_COPYIN | BC_F_SUSER }, { .bc_func = bridge_ioctl_gmnelist64, .bc_argsize = sizeof(struct ifbrmnelist64), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, { .bc_func = bridge_ioctl_gifstats64, .bc_argsize = sizeof(struct ifbrmreq64), .bc_flags = BC_F_COPYIN | BC_F_COPYOUT }, }; static const unsigned int bridge_control_table_size = sizeof(bridge_control_table32) / sizeof(bridge_control_table32[0]); static LIST_HEAD(, bridge_softc) bridge_list = LIST_HEAD_INITIALIZER(bridge_list); #define BRIDGENAME "bridge" #define BRIDGES_MAX IF_MAXUNIT #define BRIDGE_ZONE_MAX_ELEM MIN(IFNETS_MAX, BRIDGES_MAX) static struct if_clone bridge_cloner = IF_CLONE_INITIALIZER(BRIDGENAME, bridge_clone_create, bridge_clone_destroy, 0, BRIDGES_MAX, BRIDGE_ZONE_MAX_ELEM, sizeof(struct bridge_softc)); static int if_bridge_txstart = 0; SYSCTL_INT(_net_link_bridge, OID_AUTO, txstart, CTLFLAG_RW | CTLFLAG_LOCKED, &if_bridge_txstart, 0, "Bridge interface uses TXSTART model"); SYSCTL_INT(_net_link_bridge, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_LOCKED, &if_bridge_debug, 0, "Bridge debug flags"); SYSCTL_INT(_net_link_bridge, OID_AUTO, log_level, CTLFLAG_RW | CTLFLAG_LOCKED, &if_bridge_log_level, 0, "Bridge log level"); static int if_bridge_segmentation = 1; SYSCTL_INT(_net_link_bridge, OID_AUTO, segmentation, CTLFLAG_RW | CTLFLAG_LOCKED, &if_bridge_segmentation, 0, "Bridge interface enable segmentation"); static int if_bridge_vmnet_pf_tagging = 1; SYSCTL_INT(_net_link_bridge, OID_AUTO, vmnet_pf_tagging, CTLFLAG_RW | CTLFLAG_LOCKED, &if_bridge_segmentation, 0, "Bridge interface enable vmnet PF tagging"); #define BRIDGE_TSO_REDUCE_MSS_FORWARDING_MAX 256 #define BRIDGE_TSO_REDUCE_MSS_FORWARDING_DEFAULT 110 #define BRIDGE_TSO_REDUCE_MSS_TX_MAX 256 #define BRIDGE_TSO_REDUCE_MSS_TX_DEFAULT 0 static u_int if_bridge_tso_reduce_mss_forwarding = BRIDGE_TSO_REDUCE_MSS_FORWARDING_DEFAULT; static u_int if_bridge_tso_reduce_mss_tx = BRIDGE_TSO_REDUCE_MSS_TX_DEFAULT; static int bridge_tso_reduce_mss(struct sysctl_req *req, u_int * val, u_int val_max) { int changed; int error; u_int new_value; error = sysctl_io_number(req, *val, sizeof(*val), &new_value, &changed); if (error == 0 && changed != 0) { if (new_value > val_max) { return EINVAL; } *val = new_value; } return error; } static int bridge_tso_reduce_mss_forwarding_sysctl SYSCTL_HANDLER_ARGS { return bridge_tso_reduce_mss(req, &if_bridge_tso_reduce_mss_forwarding, BRIDGE_TSO_REDUCE_MSS_FORWARDING_MAX); } SYSCTL_PROC(_net_link_bridge, OID_AUTO, tso_reduce_mss_forwarding, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, bridge_tso_reduce_mss_forwarding_sysctl, "IU", "Bridge tso reduce mss when forwarding"); static int bridge_tso_reduce_mss_tx_sysctl SYSCTL_HANDLER_ARGS { return bridge_tso_reduce_mss(req, &if_bridge_tso_reduce_mss_tx, BRIDGE_TSO_REDUCE_MSS_TX_MAX); } SYSCTL_PROC(_net_link_bridge, OID_AUTO, tso_reduce_mss_tx, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, bridge_tso_reduce_mss_tx_sysctl, "IU", "Bridge tso reduce mss on transmit"); #if DEBUG || DEVELOPMENT #define BRIDGE_FORCE_ONE 0x00000001 #define BRIDGE_FORCE_TWO 0x00000002 static u_int32_t if_bridge_force_errors = 0; SYSCTL_INT(_net_link_bridge, OID_AUTO, force_errors, CTLFLAG_RW | CTLFLAG_LOCKED, &if_bridge_force_errors, 0, "Bridge interface force errors"); static inline bool bridge_error_is_forced(u_int32_t flags) { return (if_bridge_force_errors & flags) != 0; } #define BRIDGE_ERROR_GET_FORCED(__is_forced, __flags) \ do { \ __is_forced = bridge_error_is_forced(__flags); \ if (__is_forced) { \ BRIDGE_LOG(LOG_NOTICE, 0, "0x%x forced", __flags); \ } \ } while (0) #endif /* DEBUG || DEVELOPMENT */ static void brlog_ether_header(struct ether_header *); static void brlog_mbuf_data(mbuf_t, size_t, size_t); static void brlog_mbuf_pkthdr(mbuf_t, const char *, const char *); static void brlog_mbuf(mbuf_t, const char *, const char *); static void brlog_link(struct bridge_softc * sc); #if BRIDGE_LOCK_DEBUG static void bridge_lock(struct bridge_softc *); static void bridge_unlock(struct bridge_softc *); static int bridge_lock2ref(struct bridge_softc *); static void bridge_unref(struct bridge_softc *); static void bridge_xlock(struct bridge_softc *); static void bridge_xdrop(struct bridge_softc *); static void bridge_lock(struct bridge_softc *sc) { void *lr_saved = __builtin_return_address(0); BRIDGE_LOCK_ASSERT_NOTHELD(sc); _BRIDGE_LOCK(sc); sc->lock_lr[sc->next_lock_lr] = lr_saved; sc->next_lock_lr = (sc->next_lock_lr + 1) % SO_LCKDBG_MAX; } static void bridge_unlock(struct bridge_softc *sc) { void *lr_saved = __builtin_return_address(0); BRIDGE_LOCK_ASSERT_HELD(sc); sc->unlock_lr[sc->next_unlock_lr] = lr_saved; sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX; _BRIDGE_UNLOCK(sc); } static int bridge_lock2ref(struct bridge_softc *sc) { int error = 0; void *lr_saved = __builtin_return_address(0); BRIDGE_LOCK_ASSERT_HELD(sc); if (sc->sc_iflist_xcnt > 0) { error = EBUSY; } else { sc->sc_iflist_ref++; } sc->unlock_lr[sc->next_unlock_lr] = lr_saved; sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX; _BRIDGE_UNLOCK(sc); return error; } static void bridge_unref(struct bridge_softc *sc) { void *lr_saved = __builtin_return_address(0); BRIDGE_LOCK_ASSERT_NOTHELD(sc); _BRIDGE_LOCK(sc); sc->lock_lr[sc->next_lock_lr] = lr_saved; sc->next_lock_lr = (sc->next_lock_lr + 1) % SO_LCKDBG_MAX; sc->sc_iflist_ref--; sc->unlock_lr[sc->next_unlock_lr] = lr_saved; sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX; if ((sc->sc_iflist_xcnt > 0) && (sc->sc_iflist_ref == 0)) { _BRIDGE_UNLOCK(sc); wakeup(&sc->sc_cv); } else { _BRIDGE_UNLOCK(sc); } } static void bridge_xlock(struct bridge_softc *sc) { void *lr_saved = __builtin_return_address(0); BRIDGE_LOCK_ASSERT_HELD(sc); sc->sc_iflist_xcnt++; while (sc->sc_iflist_ref > 0) { sc->unlock_lr[sc->next_unlock_lr] = lr_saved; sc->next_unlock_lr = (sc->next_unlock_lr + 1) % SO_LCKDBG_MAX; msleep(&sc->sc_cv, &sc->sc_mtx, PZERO, "BRIDGE_XLOCK", NULL); sc->lock_lr[sc->next_lock_lr] = lr_saved; sc->next_lock_lr = (sc->next_lock_lr + 1) % SO_LCKDBG_MAX; } } static void bridge_xdrop(struct bridge_softc *sc) { BRIDGE_LOCK_ASSERT_HELD(sc); sc->sc_iflist_xcnt--; } #endif /* BRIDGE_LOCK_DEBUG */ static void brlog_mbuf_pkthdr(mbuf_t m, const char *prefix, const char *suffix) { if (m) { BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0, "%spktlen: %u rcvif: 0x%llx header: 0x%llx nextpkt: 0x%llx%s", prefix ? prefix : "", (unsigned int)mbuf_pkthdr_len(m), (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_rcvif(m)), (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_header(m)), (uint64_t)VM_KERNEL_ADDRPERM(mbuf_nextpkt(m)), suffix ? suffix : ""); } else { BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0, "%s<NULL>%s", prefix, suffix); } } static void brlog_mbuf(mbuf_t m, const char *prefix, const char *suffix) { if (m) { BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0, "%s0x%llx type: %u flags: 0x%x len: %u data: 0x%llx " "maxlen: %u datastart: 0x%llx next: 0x%llx%s", prefix ? prefix : "", (uint64_t)VM_KERNEL_ADDRPERM(m), mbuf_type(m), mbuf_flags(m), (unsigned int)mbuf_len(m), (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m)), (unsigned int)mbuf_maxlen(m), (uint64_t)VM_KERNEL_ADDRPERM(mbuf_datastart(m)), (uint64_t)VM_KERNEL_ADDRPERM(mbuf_next(m)), !suffix || (mbuf_flags(m) & MBUF_PKTHDR) ? "" : suffix); if ((mbuf_flags(m) & MBUF_PKTHDR)) { brlog_mbuf_pkthdr(m, "", suffix); } } else { BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0, "%s<NULL>%s", prefix, suffix); } } static void brlog_mbuf_data(mbuf_t m, size_t offset, size_t len) { mbuf_t n; size_t i, j; size_t pktlen, mlen, maxlen; unsigned char *ptr; pktlen = mbuf_pkthdr_len(m); if (offset > pktlen) { return; } maxlen = (pktlen - offset > len) ? len : pktlen - offset; n = m; mlen = mbuf_len(n); ptr = mbuf_data(n); for (i = 0, j = 0; i < maxlen; i++, j++) { if (j >= mlen) { n = mbuf_next(n); if (n == 0) { break; } ptr = mbuf_data(n); mlen = mbuf_len(n); j = 0; } if (i >= offset) { BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0, "%02x%s", ptr[j], i % 2 ? " " : ""); } } } static void brlog_ether_header(struct ether_header *eh) { BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0, "%02x:%02x:%02x:%02x:%02x:%02x > " "%02x:%02x:%02x:%02x:%02x:%02x 0x%04x ", eh->ether_shost[0], eh->ether_shost[1], eh->ether_shost[2], eh->ether_shost[3], eh->ether_shost[4], eh->ether_shost[5], eh->ether_dhost[0], eh->ether_dhost[1], eh->ether_dhost[2], eh->ether_dhost[3], eh->ether_dhost[4], eh->ether_dhost[5], ntohs(eh->ether_type)); } static char * ether_ntop(char *buf, size_t len, const u_char *ap) { snprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x", ap[0], ap[1], ap[2], ap[3], ap[4], ap[5]); return buf; } static void brlog_link(struct bridge_softc * sc) { int i; uint32_t sdl_buffer[offsetof(struct sockaddr_dl, sdl_data) + IFNAMSIZ + ETHER_ADDR_LEN]; struct sockaddr_dl *sdl = (struct sockaddr_dl *)sdl_buffer; const u_char * lladdr; char lladdr_str[48]; memset(sdl, 0, sizeof(sdl_buffer)); sdl->sdl_family = AF_LINK; sdl->sdl_nlen = strlen(sc->sc_if_xname); sdl->sdl_alen = ETHER_ADDR_LEN; sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data); memcpy(sdl->sdl_data, sc->sc_if_xname, sdl->sdl_nlen); memcpy(LLADDR(sdl), sc->sc_defaddr, ETHER_ADDR_LEN); lladdr_str[0] = '\0'; for (i = 0, lladdr = CONST_LLADDR(sdl); i < sdl->sdl_alen; i++, lladdr++) { char byte_str[4]; snprintf(byte_str, sizeof(byte_str), "%s%x", i ? ":" : "", *lladdr); strlcat(lladdr_str, byte_str, sizeof(lladdr_str)); } BRIDGE_LOG_SIMPLE(LOG_NOTICE, 0, "%s sdl len %d index %d family %d type 0x%x nlen %d alen %d" " slen %d addr %s", sc->sc_if_xname, sdl->sdl_len, sdl->sdl_index, sdl->sdl_family, sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen, lladdr_str); } /* * bridgeattach: * * Pseudo-device attach routine. */ __private_extern__ int bridgeattach(int n) { #pragma unused(n) int error; LIST_INIT(&bridge_list); #if BRIDGESTP bstp_sys_init(); #endif /* BRIDGESTP */ error = if_clone_attach(&bridge_cloner); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_clone_attach failed %d", error); } return error; } static errno_t bridge_ifnet_set_attrs(struct ifnet * ifp) { errno_t error; error = ifnet_set_mtu(ifp, ETHERMTU); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_mtu failed %d", error); goto done; } error = ifnet_set_addrlen(ifp, ETHER_ADDR_LEN); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_addrlen failed %d", error); goto done; } error = ifnet_set_hdrlen(ifp, ETHER_HDR_LEN); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_hdrlen failed %d", error); goto done; } error = ifnet_set_flags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST, 0xffff); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_flags failed %d", error); goto done; } done: return error; } /* * bridge_clone_create: * * Create a new bridge instance. */ static int bridge_clone_create(struct if_clone *ifc, uint32_t unit, void *params) { #pragma unused(params) struct ifnet *ifp = NULL; struct bridge_softc *sc = NULL; struct bridge_softc *sc2 = NULL; struct ifnet_init_eparams init_params; errno_t error = 0; uint8_t eth_hostid[ETHER_ADDR_LEN]; int fb, retry, has_hostid; sc = if_clone_softc_allocate(&bridge_cloner); if (sc == NULL) { error = ENOMEM; goto done; } lck_mtx_init(&sc->sc_mtx, &bridge_lock_grp, &bridge_lock_attr); sc->sc_brtmax = BRIDGE_RTABLE_MAX; sc->sc_mne_max = BRIDGE_MAC_NAT_ENTRY_MAX; sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT; sc->sc_filter_flags = 0; TAILQ_INIT(&sc->sc_iflist); /* use the interface name as the unique id for ifp recycle */ snprintf(sc->sc_if_xname, sizeof(sc->sc_if_xname), "%s%d", ifc->ifc_name, unit); bzero(&init_params, sizeof(init_params)); init_params.ver = IFNET_INIT_CURRENT_VERSION; init_params.len = sizeof(init_params); /* Initialize our routing table. */ error = bridge_rtable_init(sc); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "bridge_rtable_init failed %d", error); goto done; } TAILQ_INIT(&sc->sc_spanlist); if (if_bridge_txstart) { init_params.start = bridge_start; } else { init_params.flags = IFNET_INIT_LEGACY; init_params.output = bridge_output; } init_params.set_bpf_tap = bridge_set_bpf_tap; init_params.uniqueid = sc->sc_if_xname; init_params.uniqueid_len = strlen(sc->sc_if_xname); init_params.sndq_maxlen = IFQ_MAXLEN; init_params.name = ifc->ifc_name; init_params.unit = unit; init_params.family = IFNET_FAMILY_ETHERNET; init_params.type = IFT_BRIDGE; init_params.demux = ether_demux; init_params.add_proto = ether_add_proto; init_params.del_proto = ether_del_proto; init_params.check_multi = ether_check_multi; init_params.framer_extended = ether_frameout_extended; init_params.softc = sc; init_params.ioctl = bridge_ioctl; init_params.detach = bridge_detach; init_params.broadcast_addr = etherbroadcastaddr; init_params.broadcast_len = ETHER_ADDR_LEN; error = ifnet_allocate_extended(&init_params, &ifp); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_allocate failed %d", error); goto done; } LIST_INIT(&sc->sc_mne_list); LIST_INIT(&sc->sc_mne_list_v6); sc->sc_ifp = ifp; error = bridge_ifnet_set_attrs(ifp); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "bridge_ifnet_set_attrs failed %d", error); goto done; } /* * Generate an ethernet address with a locally administered address. * * Since we are using random ethernet addresses for the bridge, it is * possible that we might have address collisions, so make sure that * this hardware address isn't already in use on another bridge. * The first try uses the "hostid" and falls back to read_frandom(); * for "hostid", we use the MAC address of the first-encountered * Ethernet-type interface that is currently configured. */ fb = 0; has_hostid = (uuid_get_ethernet(ð_hostid[0]) == 0); for (retry = 1; retry != 0;) { if (fb || has_hostid == 0) { read_frandom(&sc->sc_defaddr, ETHER_ADDR_LEN); sc->sc_defaddr[0] &= ~1; /* clear multicast bit */ sc->sc_defaddr[0] |= 2; /* set the LAA bit */ } else { bcopy(ð_hostid[0], &sc->sc_defaddr, ETHER_ADDR_LEN); sc->sc_defaddr[0] &= ~1; /* clear multicast bit */ sc->sc_defaddr[0] |= 2; /* set the LAA bit */ sc->sc_defaddr[3] = /* stir it up a bit */ ((sc->sc_defaddr[3] & 0x0f) << 4) | ((sc->sc_defaddr[3] & 0xf0) >> 4); /* * Mix in the LSB as it's actually pretty significant, * see rdar://14076061 */ sc->sc_defaddr[4] = (((sc->sc_defaddr[4] & 0x0f) << 4) | ((sc->sc_defaddr[4] & 0xf0) >> 4)) ^ sc->sc_defaddr[5]; sc->sc_defaddr[5] = ifp->if_unit & 0xff; } fb = 1; retry = 0; lck_mtx_lock(&bridge_list_mtx); LIST_FOREACH(sc2, &bridge_list, sc_list) { if (_ether_cmp(sc->sc_defaddr, IF_LLADDR(sc2->sc_ifp)) == 0) { retry = 1; } } lck_mtx_unlock(&bridge_list_mtx); } sc->sc_flags &= ~SCF_MEDIA_ACTIVE; if (BRIDGE_DBGF_ENABLED(BR_DBGF_LIFECYCLE)) { brlog_link(sc); } error = ifnet_attach(ifp, NULL); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_attach failed %d", error); goto done; } error = ifnet_set_lladdr_and_type(ifp, sc->sc_defaddr, ETHER_ADDR_LEN, IFT_ETHER); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_lladdr_and_type failed %d", error); goto done; } ifnet_set_offload(ifp, IFNET_CSUM_IP | IFNET_CSUM_TCP | IFNET_CSUM_UDP | IFNET_CSUM_TCPIPV6 | IFNET_CSUM_UDPIPV6 | IFNET_MULTIPAGES); error = bridge_set_tso(sc); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "bridge_set_tso failed %d", error); goto done; } #if BRIDGESTP bstp_attach(&sc->sc_stp, &bridge_ops); #endif /* BRIDGESTP */ lck_mtx_lock(&bridge_list_mtx); LIST_INSERT_HEAD(&bridge_list, sc, sc_list); lck_mtx_unlock(&bridge_list_mtx); /* attach as ethernet */ error = bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header), NULL, NULL); done: if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "failed error %d", error); /* TBD: Clean up: sc, sc_rthash etc */ } return error; } /* * bridge_clone_destroy: * * Destroy a bridge instance. */ static int bridge_clone_destroy(struct ifnet *ifp) { struct bridge_softc *sc = ifp->if_softc; struct bridge_iflist *bif; errno_t error; BRIDGE_LOCK(sc); if ((sc->sc_flags & SCF_DETACHING)) { BRIDGE_UNLOCK(sc); return 0; } sc->sc_flags |= SCF_DETACHING; bridge_ifstop(ifp, 1); bridge_cancel_delayed_call(&sc->sc_resize_call); bridge_cleanup_delayed_call(&sc->sc_resize_call); bridge_cleanup_delayed_call(&sc->sc_aging_timer); error = ifnet_set_flags(ifp, 0, IFF_UP); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_flags failed %d", error); } while ((bif = TAILQ_FIRST(&sc->sc_iflist)) != NULL) { bridge_delete_member(sc, bif); } while ((bif = TAILQ_FIRST(&sc->sc_spanlist)) != NULL) { bridge_delete_span(sc, bif); } BRIDGE_UNLOCK(sc); error = ifnet_detach(ifp); if (error != 0) { panic("%s (%d): ifnet_detach(%p) failed %d", __func__, __LINE__, ifp, error); } return 0; } #define DRVSPEC do { \ if (ifd->ifd_cmd >= bridge_control_table_size) { \ error = EINVAL; \ break; \ } \ bc = &bridge_control_table[ifd->ifd_cmd]; \ \ if (cmd == SIOCGDRVSPEC && \ (bc->bc_flags & BC_F_COPYOUT) == 0) { \ error = EINVAL; \ break; \ } else if (cmd == SIOCSDRVSPEC && \ (bc->bc_flags & BC_F_COPYOUT) != 0) { \ error = EINVAL; \ break; \ } \ \ if (bc->bc_flags & BC_F_SUSER) { \ error = kauth_authorize_generic(kauth_cred_get(), \ KAUTH_GENERIC_ISSUSER); \ if (error) \ break; \ } \ \ if (ifd->ifd_len != bc->bc_argsize || \ ifd->ifd_len > sizeof (args)) { \ error = EINVAL; \ break; \ } \ \ bzero(&args, sizeof (args)); \ if (bc->bc_flags & BC_F_COPYIN) { \ error = copyin(ifd->ifd_data, &args, ifd->ifd_len); \ if (error) \ break; \ } \ \ BRIDGE_LOCK(sc); \ error = (*bc->bc_func)(sc, &args); \ BRIDGE_UNLOCK(sc); \ if (error) \ break; \ \ if (bc->bc_flags & BC_F_COPYOUT) \ error = copyout(&args, ifd->ifd_data, ifd->ifd_len); \ } while (0) static boolean_t interface_needs_input_broadcast(struct ifnet * ifp) { /* * Selectively enable input broadcast only when necessary. * The bridge interface itself attaches a fake protocol * so checking for at least two protocols means that the * interface is being used for something besides bridging * and needs to see broadcast packets from other members. */ return if_get_protolist(ifp, NULL, 0) >= 2; } static boolean_t bif_set_input_broadcast(struct bridge_iflist * bif, boolean_t input_broadcast) { boolean_t old_input_broadcast; old_input_broadcast = (bif->bif_flags & BIFF_INPUT_BROADCAST) != 0; if (input_broadcast) { bif->bif_flags |= BIFF_INPUT_BROADCAST; } else { bif->bif_flags &= ~BIFF_INPUT_BROADCAST; } return old_input_broadcast != input_broadcast; } /* * bridge_ioctl: * * Handle a control request from the operator. */ static errno_t bridge_ioctl(struct ifnet *ifp, u_long cmd, void *data) { struct bridge_softc *sc = ifp->if_softc; struct ifreq *ifr = (struct ifreq *)data; struct bridge_iflist *bif; int error = 0; BRIDGE_LOCK_ASSERT_NOTHELD(sc); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_IOCTL, "ifp %s cmd 0x%08lx (%c%c [%lu] %c %lu)", ifp->if_xname, cmd, (cmd & IOC_IN) ? 'I' : ' ', (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd & 0xff); switch (cmd) { case SIOCSIFADDR: case SIOCAIFADDR: ifnet_set_flags(ifp, IFF_UP, IFF_UP); break; case SIOCGIFMEDIA32: case SIOCGIFMEDIA64: { struct ifmediareq *ifmr = (struct ifmediareq *)data; user_addr_t user_addr; user_addr = (cmd == SIOCGIFMEDIA64) ? ((struct ifmediareq64 *)ifmr)->ifmu_ulist : CAST_USER_ADDR_T(((struct ifmediareq32 *)ifmr)->ifmu_ulist); ifmr->ifm_status = IFM_AVALID; ifmr->ifm_mask = 0; ifmr->ifm_count = 1; BRIDGE_LOCK(sc); if (!(sc->sc_flags & SCF_DETACHING) && (sc->sc_flags & SCF_MEDIA_ACTIVE)) { ifmr->ifm_status |= IFM_ACTIVE; ifmr->ifm_active = ifmr->ifm_current = IFM_ETHER | IFM_AUTO; } else { ifmr->ifm_active = ifmr->ifm_current = IFM_NONE; } BRIDGE_UNLOCK(sc); if (user_addr != USER_ADDR_NULL) { error = copyout(&ifmr->ifm_current, user_addr, sizeof(int)); } break; } case SIOCADDMULTI: case SIOCDELMULTI: break; case SIOCSDRVSPEC32: case SIOCGDRVSPEC32: { union { struct ifbreq ifbreq; struct ifbifconf32 ifbifconf; struct ifbareq32 ifbareq; struct ifbaconf32 ifbaconf; struct ifbrparam ifbrparam; struct ifbropreq32 ifbropreq; } args; struct ifdrv32 *ifd = (struct ifdrv32 *)data; const struct bridge_control *bridge_control_table = bridge_control_table32, *bc; DRVSPEC; break; } case SIOCSDRVSPEC64: case SIOCGDRVSPEC64: { union { struct ifbreq ifbreq; struct ifbifconf64 ifbifconf; struct ifbareq64 ifbareq; struct ifbaconf64 ifbaconf; struct ifbrparam ifbrparam; struct ifbropreq64 ifbropreq; } args; struct ifdrv64 *ifd = (struct ifdrv64 *)data; const struct bridge_control *bridge_control_table = bridge_control_table64, *bc; DRVSPEC; break; } case SIOCSIFFLAGS: if (!(ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING)) { /* * If interface is marked down and it is running, * then stop and disable it. */ BRIDGE_LOCK(sc); bridge_ifstop(ifp, 1); BRIDGE_UNLOCK(sc); } else if ((ifp->if_flags & IFF_UP) && !(ifp->if_flags & IFF_RUNNING)) { /* * If interface is marked up and it is stopped, then * start it. */ BRIDGE_LOCK(sc); error = bridge_init(ifp); BRIDGE_UNLOCK(sc); } break; case SIOCSIFLLADDR: error = ifnet_set_lladdr(ifp, ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_IOCTL, "%s SIOCSIFLLADDR error %d", ifp->if_xname, error); } break; case SIOCSIFMTU: if (ifr->ifr_mtu < 576) { error = EINVAL; break; } BRIDGE_LOCK(sc); if (TAILQ_EMPTY(&sc->sc_iflist)) { sc->sc_ifp->if_mtu = ifr->ifr_mtu; BRIDGE_UNLOCK(sc); break; } TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { if (bif->bif_ifp->if_mtu != (unsigned)ifr->ifr_mtu) { BRIDGE_LOG(LOG_NOTICE, 0, "%s invalid MTU: %u(%s) != %d", sc->sc_ifp->if_xname, bif->bif_ifp->if_mtu, bif->bif_ifp->if_xname, ifr->ifr_mtu); error = EINVAL; break; } } if (!error) { sc->sc_ifp->if_mtu = ifr->ifr_mtu; } BRIDGE_UNLOCK(sc); break; default: error = ether_ioctl(ifp, cmd, data); if (error != 0 && error != EOPNOTSUPP) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_IOCTL, "ifp %s cmd 0x%08lx " "(%c%c [%lu] %c %lu) failed error: %d", ifp->if_xname, cmd, (cmd & IOC_IN) ? 'I' : ' ', (cmd & IOC_OUT) ? 'O' : ' ', IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd & 0xff, error); } break; } BRIDGE_LOCK_ASSERT_NOTHELD(sc); return error; } #if HAS_IF_CAP /* * bridge_mutecaps: * * Clear or restore unwanted capabilities on the member interface */ static void bridge_mutecaps(struct bridge_softc *sc) { struct bridge_iflist *bif; int enabled, mask; /* Initial bitmask of capabilities to test */ mask = BRIDGE_IFCAPS_MASK; TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { /* Every member must support it or its disabled */ mask &= bif->bif_savedcaps; } TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { enabled = bif->bif_ifp->if_capenable; enabled &= ~BRIDGE_IFCAPS_STRIP; /* strip off mask bits and enable them again if allowed */ enabled &= ~BRIDGE_IFCAPS_MASK; enabled |= mask; bridge_set_ifcap(sc, bif, enabled); } } static void bridge_set_ifcap(struct bridge_softc *sc, struct bridge_iflist *bif, int set) { struct ifnet *ifp = bif->bif_ifp; struct ifreq ifr; int error; bzero(&ifr, sizeof(ifr)); ifr.ifr_reqcap = set; if (ifp->if_capenable != set) { IFF_LOCKGIANT(ifp); error = (*ifp->if_ioctl)(ifp, SIOCSIFCAP, (caddr_t)&ifr); IFF_UNLOCKGIANT(ifp); if (error) { BRIDGE_LOG(LOG_NOTICE, 0, "%s error setting interface capabilities on %s", sc->sc_ifp->if_xname, ifp->if_xname); } } } #endif /* HAS_IF_CAP */ static errno_t bridge_set_tso(struct bridge_softc *sc) { struct bridge_iflist *bif; u_int32_t tso_v4_mtu; u_int32_t tso_v6_mtu; ifnet_offload_t offload; errno_t error = 0; /* By default, support TSO */ offload = sc->sc_ifp->if_hwassist | IFNET_TSO_IPV4 | IFNET_TSO_IPV6; tso_v4_mtu = IP_MAXPACKET; tso_v6_mtu = IP_MAXPACKET; /* Use the lowest common denominator of the members */ TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { ifnet_t ifp = bif->bif_ifp; if (ifp == NULL) { continue; } if (offload & IFNET_TSO_IPV4) { if (ifp->if_hwassist & IFNET_TSO_IPV4) { if (tso_v4_mtu > ifp->if_tso_v4_mtu) { tso_v4_mtu = ifp->if_tso_v4_mtu; } } else { offload &= ~IFNET_TSO_IPV4; tso_v4_mtu = 0; } } if (offload & IFNET_TSO_IPV6) { if (ifp->if_hwassist & IFNET_TSO_IPV6) { if (tso_v6_mtu > ifp->if_tso_v6_mtu) { tso_v6_mtu = ifp->if_tso_v6_mtu; } } else { offload &= ~IFNET_TSO_IPV6; tso_v6_mtu = 0; } } } if (offload != sc->sc_ifp->if_hwassist) { error = ifnet_set_offload(sc->sc_ifp, offload); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_LIFECYCLE, "ifnet_set_offload(%s, 0x%x) failed %d", sc->sc_ifp->if_xname, offload, error); goto done; } /* * For ifnet_set_tso_mtu() sake, the TSO MTU must be at least * as large as the interface MTU */ if (sc->sc_ifp->if_hwassist & IFNET_TSO_IPV4) { if (tso_v4_mtu < sc->sc_ifp->if_mtu) { tso_v4_mtu = sc->sc_ifp->if_mtu; } error = ifnet_set_tso_mtu(sc->sc_ifp, AF_INET, tso_v4_mtu); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_LIFECYCLE, "ifnet_set_tso_mtu(%s, " "AF_INET, %u) failed %d", sc->sc_ifp->if_xname, tso_v4_mtu, error); goto done; } } if (sc->sc_ifp->if_hwassist & IFNET_TSO_IPV6) { if (tso_v6_mtu < sc->sc_ifp->if_mtu) { tso_v6_mtu = sc->sc_ifp->if_mtu; } error = ifnet_set_tso_mtu(sc->sc_ifp, AF_INET6, tso_v6_mtu); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_LIFECYCLE, "ifnet_set_tso_mtu(%s, " "AF_INET6, %u) failed %d", sc->sc_ifp->if_xname, tso_v6_mtu, error); goto done; } } } done: return error; } /* * bridge_lookup_member: * * Lookup a bridge member interface. */ static struct bridge_iflist * bridge_lookup_member(struct bridge_softc *sc, const char *name) { struct bridge_iflist *bif; struct ifnet *ifp; BRIDGE_LOCK_ASSERT_HELD(sc); TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { ifp = bif->bif_ifp; if (strcmp(ifp->if_xname, name) == 0) { return bif; } } return NULL; } /* * bridge_lookup_member_if: * * Lookup a bridge member interface by ifnet*. */ static struct bridge_iflist * bridge_lookup_member_if(struct bridge_softc *sc, struct ifnet *member_ifp) { struct bridge_iflist *bif; BRIDGE_LOCK_ASSERT_HELD(sc); TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { if (bif->bif_ifp == member_ifp) { return bif; } } return NULL; } static errno_t bridge_iff_input(void *cookie, ifnet_t ifp, protocol_family_t protocol, mbuf_t *data, char **frame_ptr) { #pragma unused(protocol) errno_t error = 0; struct bridge_iflist *bif = (struct bridge_iflist *)cookie; struct bridge_softc *sc = bif->bif_sc; int included = 0; size_t frmlen = 0; mbuf_t m = *data; if ((m->m_flags & M_PROTO1)) { goto out; } if (*frame_ptr >= (char *)mbuf_datastart(m) && *frame_ptr <= (char *)mbuf_data(m)) { included = 1; frmlen = (char *)mbuf_data(m) - *frame_ptr; } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT, "%s from %s m 0x%llx data 0x%llx frame 0x%llx %s " "frmlen %lu", sc->sc_ifp->if_xname, ifp->if_xname, (uint64_t)VM_KERNEL_ADDRPERM(m), (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m)), (uint64_t)VM_KERNEL_ADDRPERM(*frame_ptr), included ? "inside" : "outside", frmlen); if (BRIDGE_DBGF_ENABLED(BR_DBGF_MBUF)) { brlog_mbuf(m, "bridge_iff_input[", ""); brlog_ether_header((struct ether_header *) (void *)*frame_ptr); brlog_mbuf_data(m, 0, 20); } if (included == 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT, "frame_ptr outside mbuf"); goto out; } /* Move data pointer to start of frame to the link layer header */ (void) mbuf_setdata(m, (char *)mbuf_data(m) - frmlen, mbuf_len(m) + frmlen); (void) mbuf_pkthdr_adjustlen(m, frmlen); /* make sure we can access the ethernet header */ if (mbuf_pkthdr_len(m) < sizeof(struct ether_header)) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT, "short frame %lu < %lu", mbuf_pkthdr_len(m), sizeof(struct ether_header)); goto out; } if (mbuf_len(m) < sizeof(struct ether_header)) { error = mbuf_pullup(data, sizeof(struct ether_header)); if (error != 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT, "mbuf_pullup(%lu) failed %d", sizeof(struct ether_header), error); error = EJUSTRETURN; goto out; } if (m != *data) { m = *data; *frame_ptr = mbuf_data(m); } } error = bridge_input(ifp, data); /* Adjust packet back to original */ if (error == 0) { /* bridge_input might have modified *data */ if (*data != m) { m = *data; *frame_ptr = mbuf_data(m); } (void) mbuf_setdata(m, (char *)mbuf_data(m) + frmlen, mbuf_len(m) - frmlen); (void) mbuf_pkthdr_adjustlen(m, -frmlen); } if (BRIDGE_DBGF_ENABLED(BR_DBGF_MBUF) && BRIDGE_DBGF_ENABLED(BR_DBGF_INPUT)) { brlog_mbuf(m, "bridge_iff_input]", ""); } out: BRIDGE_LOCK_ASSERT_NOTHELD(sc); return error; } static errno_t bridge_iff_output(void *cookie, ifnet_t ifp, protocol_family_t protocol, mbuf_t *data) { #pragma unused(protocol) errno_t error = 0; struct bridge_iflist *bif = (struct bridge_iflist *)cookie; struct bridge_softc *sc = bif->bif_sc; mbuf_t m = *data; if ((m->m_flags & M_PROTO1)) { goto out; } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_OUTPUT, "%s from %s m 0x%llx data 0x%llx", sc->sc_ifp->if_xname, ifp->if_xname, (uint64_t)VM_KERNEL_ADDRPERM(m), (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m))); error = bridge_member_output(sc, ifp, data); if (error != 0 && error != EJUSTRETURN) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_OUTPUT, "bridge_member_output failed error %d", error); } out: BRIDGE_LOCK_ASSERT_NOTHELD(sc); return error; } static void bridge_iff_event(void *cookie, ifnet_t ifp, protocol_family_t protocol, const struct kev_msg *event_msg) { #pragma unused(protocol) struct bridge_iflist *bif = (struct bridge_iflist *)cookie; struct bridge_softc *sc = bif->bif_sc; if (event_msg->vendor_code == KEV_VENDOR_APPLE && event_msg->kev_class == KEV_NETWORK_CLASS && event_msg->kev_subclass == KEV_DL_SUBCLASS) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s event_code %u - %s", ifp->if_xname, event_msg->event_code, dlil_kev_dl_code_str(event_msg->event_code)); switch (event_msg->event_code) { case KEV_DL_LINK_OFF: case KEV_DL_LINK_ON: { bridge_iflinkevent(ifp); #if BRIDGESTP bstp_linkstate(ifp, event_msg->event_code); #endif /* BRIDGESTP */ break; } case KEV_DL_SIFFLAGS: { if ((bif->bif_flags & BIFF_PROMISC) == 0 && (ifp->if_flags & IFF_UP)) { errno_t error; error = ifnet_set_promiscuous(ifp, 1); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_promiscuous (%s)" " failed %d", ifp->if_xname, error); } else { bif->bif_flags |= BIFF_PROMISC; } } break; } case KEV_DL_IFCAP_CHANGED: { BRIDGE_LOCK(sc); bridge_set_tso(sc); BRIDGE_UNLOCK(sc); break; } case KEV_DL_PROTO_DETACHED: case KEV_DL_PROTO_ATTACHED: { bridge_proto_attach_changed(ifp); break; } default: break; } } } /* * bridge_iff_detached: * * Called when our interface filter has been detached from a * member interface. */ static void bridge_iff_detached(void *cookie, ifnet_t ifp) { #pragma unused(cookie) struct bridge_iflist *bif; struct bridge_softc *sc = ifp->if_bridge; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s", ifp->if_xname); /* Check if the interface is a bridge member */ if (sc != NULL) { BRIDGE_LOCK(sc); bif = bridge_lookup_member_if(sc, ifp); if (bif != NULL) { bridge_delete_member(sc, bif); } BRIDGE_UNLOCK(sc); return; } /* Check if the interface is a span port */ lck_mtx_lock(&bridge_list_mtx); LIST_FOREACH(sc, &bridge_list, sc_list) { BRIDGE_LOCK(sc); TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) if (ifp == bif->bif_ifp) { bridge_delete_span(sc, bif); break; } BRIDGE_UNLOCK(sc); } lck_mtx_unlock(&bridge_list_mtx); } static errno_t bridge_proto_input(ifnet_t ifp, protocol_family_t protocol, mbuf_t packet, char *header) { #pragma unused(protocol, packet, header) BRIDGE_LOG(LOG_NOTICE, 0, "%s unexpected packet", ifp->if_xname); return 0; } static int bridge_attach_protocol(struct ifnet *ifp) { int error; struct ifnet_attach_proto_param reg; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s", ifp->if_xname); bzero(®, sizeof(reg)); reg.input = bridge_proto_input; error = ifnet_attach_protocol(ifp, PF_BRIDGE, ®); if (error) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_attach_protocol(%s) failed, %d", ifp->if_xname, error); } return error; } static int bridge_detach_protocol(struct ifnet *ifp) { int error; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s", ifp->if_xname); error = ifnet_detach_protocol(ifp, PF_BRIDGE); if (error) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_detach_protocol(%s) failed, %d", ifp->if_xname, error); } return error; } /* * bridge_delete_member: * * Delete the specified member interface. */ static void bridge_delete_member(struct bridge_softc *sc, struct bridge_iflist *bif) { uint32_t bif_flags; struct ifnet *ifs = bif->bif_ifp, *bifp = sc->sc_ifp; int lladdr_changed = 0, error; uint8_t eaddr[ETHER_ADDR_LEN]; u_int32_t event_code = 0; BRIDGE_LOCK_ASSERT_HELD(sc); VERIFY(ifs != NULL); /* * Remove the member from the list first so it cannot be found anymore * when we release the bridge lock below */ if ((bif->bif_flags & BIFF_IN_MEMBER_LIST) != 0) { BRIDGE_XLOCK(sc); TAILQ_REMOVE(&sc->sc_iflist, bif, bif_next); BRIDGE_XDROP(sc); } if (sc->sc_mac_nat_bif != NULL) { if (bif == sc->sc_mac_nat_bif) { bridge_mac_nat_disable(sc); } else { bridge_mac_nat_flush_entries(sc, bif); } } #if BRIDGESTP if ((bif->bif_ifflags & IFBIF_STP) != 0) { bstp_disable(&bif->bif_stp); } #endif /* BRIDGESTP */ /* * If removing the interface that gave the bridge its mac address, set * the mac address of the bridge to the address of the next member, or * to its default address if no members are left. */ if (bridge_inherit_mac && sc->sc_ifaddr == ifs) { ifnet_release(sc->sc_ifaddr); if (TAILQ_EMPTY(&sc->sc_iflist)) { bcopy(sc->sc_defaddr, eaddr, ETHER_ADDR_LEN); sc->sc_ifaddr = NULL; } else { struct ifnet *fif = TAILQ_FIRST(&sc->sc_iflist)->bif_ifp; bcopy(IF_LLADDR(fif), eaddr, ETHER_ADDR_LEN); sc->sc_ifaddr = fif; ifnet_reference(fif); /* for sc_ifaddr */ } lladdr_changed = 1; } #if HAS_IF_CAP bridge_mutecaps(sc); /* recalculate now this interface is removed */ #endif /* HAS_IF_CAP */ error = bridge_set_tso(sc); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "bridge_set_tso failed %d", error); } bridge_rtdelete(sc, ifs, IFBF_FLUSHALL); KASSERT(bif->bif_addrcnt == 0, ("%s: %d bridge routes referenced", __func__, bif->bif_addrcnt)); /* * Update link status of the bridge based on its remaining members */ event_code = bridge_updatelinkstatus(sc); bif_flags = bif->bif_flags; BRIDGE_UNLOCK(sc); /* only perform these steps if the interface is still attached */ if (ifnet_is_attached(ifs, 1)) { #if SKYWALK if ((bif_flags & BIFF_NETAGENT_REMOVED) != 0) { ifnet_add_netagent(ifs); } if ((bif_flags & BIFF_FLOWSWITCH_ATTACHED) != 0) { ifnet_detach_flowswitch_nexus(ifs); } #endif /* SKYWALK */ /* disable promiscuous mode */ if ((bif_flags & BIFF_PROMISC) != 0) { (void) ifnet_set_promiscuous(ifs, 0); } #if HAS_IF_CAP /* re-enable any interface capabilities */ bridge_set_ifcap(sc, bif, bif->bif_savedcaps); #endif /* detach bridge "protocol" */ if ((bif_flags & BIFF_PROTO_ATTACHED) != 0) { (void)bridge_detach_protocol(ifs); } /* detach interface filter */ if ((bif_flags & BIFF_FILTER_ATTACHED) != 0) { iflt_detach(bif->bif_iff_ref); } ifnet_decr_iorefcnt(ifs); } if (lladdr_changed && (error = ifnet_set_lladdr(bifp, eaddr, ETHER_ADDR_LEN)) != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_lladdr failed %d", error); } if (event_code != 0) { bridge_link_event(bifp, event_code); } #if BRIDGESTP bstp_destroy(&bif->bif_stp); /* prepare to free */ #endif /* BRIDGESTP */ kfree_type(struct bridge_iflist, bif); ifs->if_bridge = NULL; ifnet_release(ifs); BRIDGE_LOCK(sc); } /* * bridge_delete_span: * * Delete the specified span interface. */ static void bridge_delete_span(struct bridge_softc *sc, struct bridge_iflist *bif) { BRIDGE_LOCK_ASSERT_HELD(sc); KASSERT(bif->bif_ifp->if_bridge == NULL, ("%s: not a span interface", __func__)); ifnet_release(bif->bif_ifp); TAILQ_REMOVE(&sc->sc_spanlist, bif, bif_next); kfree_type(struct bridge_iflist, bif); } static int bridge_ioctl_add(struct bridge_softc *sc, void *arg) { struct ifbreq *req = arg; struct bridge_iflist *bif = NULL; struct ifnet *ifs, *bifp = sc->sc_ifp; int error = 0, lladdr_changed = 0; uint8_t eaddr[ETHER_ADDR_LEN]; struct iff_filter iff; u_int32_t event_code = 0; boolean_t mac_nat = FALSE; boolean_t input_broadcast; ifs = ifunit(req->ifbr_ifsname); if (ifs == NULL) { return ENOENT; } if (ifs->if_ioctl == NULL) { /* must be supported */ return EINVAL; } if (IFNET_IS_INTCOPROC(ifs)) { return EINVAL; } /* If it's in the span list, it can't be a member. */ TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) { if (ifs == bif->bif_ifp) { return EBUSY; } } if (ifs->if_bridge == sc) { return EEXIST; } if (ifs->if_bridge != NULL) { return EBUSY; } switch (ifs->if_type) { case IFT_ETHER: if (strcmp(ifs->if_name, "en") == 0 && ifs->if_subfamily == IFNET_SUBFAMILY_WIFI && (ifs->if_eflags & IFEF_IPV4_ROUTER) == 0) { /* XXX is there a better way to identify Wi-Fi STA? */ mac_nat = TRUE; } break; case IFT_L2VLAN: case IFT_IEEE8023ADLAG: break; case IFT_GIF: /* currently not supported */ /* FALLTHRU */ default: return EINVAL; } /* fail to add the interface if the MTU doesn't match */ if (!TAILQ_EMPTY(&sc->sc_iflist) && sc->sc_ifp->if_mtu != ifs->if_mtu) { BRIDGE_LOG(LOG_NOTICE, 0, "%s invalid MTU for %s", sc->sc_ifp->if_xname, ifs->if_xname); return EINVAL; } /* there's already an interface that's doing MAC NAT */ if (mac_nat && sc->sc_mac_nat_bif != NULL) { return EBUSY; } /* prevent the interface from detaching while we add the member */ if (!ifnet_is_attached(ifs, 1)) { return ENXIO; } /* allocate a new member */ bif = kalloc_type(struct bridge_iflist, Z_WAITOK | Z_ZERO | Z_NOFAIL); bif->bif_ifp = ifs; ifnet_reference(ifs); bif->bif_ifflags |= IFBIF_LEARNING | IFBIF_DISCOVER; #if HAS_IF_CAP bif->bif_savedcaps = ifs->if_capenable; #endif /* HAS_IF_CAP */ bif->bif_sc = sc; if (mac_nat) { (void)bridge_mac_nat_enable(sc, bif); } if (IFNET_IS_VMNET(ifs)) { allocate_vmnet_pf_tags(); } /* Allow the first Ethernet member to define the MTU */ if (TAILQ_EMPTY(&sc->sc_iflist)) { sc->sc_ifp->if_mtu = ifs->if_mtu; } /* * Assign the interface's MAC address to the bridge if it's the first * member and the MAC address of the bridge has not been changed from * the default (randomly) generated one. */ if (bridge_inherit_mac && TAILQ_EMPTY(&sc->sc_iflist) && _ether_cmp(IF_LLADDR(sc->sc_ifp), sc->sc_defaddr) == 0) { bcopy(IF_LLADDR(ifs), eaddr, ETHER_ADDR_LEN); sc->sc_ifaddr = ifs; ifnet_reference(ifs); /* for sc_ifaddr */ lladdr_changed = 1; } ifs->if_bridge = sc; #if BRIDGESTP bstp_create(&sc->sc_stp, &bif->bif_stp, bif->bif_ifp); #endif /* BRIDGESTP */ #if HAS_IF_CAP /* Set interface capabilities to the intersection set of all members */ bridge_mutecaps(sc); #endif /* HAS_IF_CAP */ /* * Respect lock ordering with DLIL lock for the following operations */ BRIDGE_UNLOCK(sc); /* enable promiscuous mode */ error = ifnet_set_promiscuous(ifs, 1); switch (error) { case 0: bif->bif_flags |= BIFF_PROMISC; break; case ENETDOWN: case EPWROFF: BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_promiscuous(%s) failed %d, ignoring", ifs->if_xname, error); /* Ignore error when device is not up */ error = 0; break; default: BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_promiscuous(%s) failed %d", ifs->if_xname, error); BRIDGE_LOCK(sc); goto out; } #if SKYWALK /* ensure that the flowswitch is present for native interface */ if (SKYWALK_NATIVE(ifs)) { if (ifnet_attach_flowswitch_nexus(ifs)) { bif->bif_flags |= BIFF_FLOWSWITCH_ATTACHED; } } /* remove the netagent on the flowswitch (rdar://75050182) */ if (ifnet_remove_netagent(ifs)) { bif->bif_flags |= BIFF_NETAGENT_REMOVED; } #endif /* SKYWALK */ /* * install an interface filter */ memset(&iff, 0, sizeof(struct iff_filter)); iff.iff_cookie = bif; iff.iff_name = "com.apple.kernel.bsd.net.if_bridge"; iff.iff_input = bridge_iff_input; iff.iff_output = bridge_iff_output; iff.iff_event = bridge_iff_event; iff.iff_detached = bridge_iff_detached; error = dlil_attach_filter(ifs, &iff, &bif->bif_iff_ref, DLIL_IFF_TSO | DLIL_IFF_INTERNAL); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "iflt_attach failed %d", error); BRIDGE_LOCK(sc); goto out; } bif->bif_flags |= BIFF_FILTER_ATTACHED; /* * install a dummy "bridge" protocol */ if ((error = bridge_attach_protocol(ifs)) != 0) { if (error != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "bridge_attach_protocol failed %d", error); BRIDGE_LOCK(sc); goto out; } } bif->bif_flags |= BIFF_PROTO_ATTACHED; if (lladdr_changed && (error = ifnet_set_lladdr(bifp, eaddr, ETHER_ADDR_LEN)) != 0) { BRIDGE_LOG(LOG_NOTICE, 0, "ifnet_set_lladdr failed %d", error); } /* * No failures past this point. Add the member to the list. */ BRIDGE_LOCK(sc); bif->bif_flags |= BIFF_IN_MEMBER_LIST; BRIDGE_XLOCK(sc); TAILQ_INSERT_TAIL(&sc->sc_iflist, bif, bif_next); BRIDGE_XDROP(sc); /* cache the member link status */ if (interface_media_active(ifs)) { bif->bif_flags |= BIFF_MEDIA_ACTIVE; } else { bif->bif_flags &= ~BIFF_MEDIA_ACTIVE; } /* the new member may change the link status of the bridge interface */ event_code = bridge_updatelinkstatus(sc); /* check whether we need input broadcast or not */ input_broadcast = interface_needs_input_broadcast(ifs); bif_set_input_broadcast(bif, input_broadcast); BRIDGE_UNLOCK(sc); if (event_code != 0) { bridge_link_event(bifp, event_code); } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s input broadcast %s", ifs->if_xname, input_broadcast ? "ENABLED" : "DISABLED"); BRIDGE_LOCK(sc); bridge_set_tso(sc); out: /* allow the interface to detach */ ifnet_decr_iorefcnt(ifs); if (error != 0) { if (bif != NULL) { bridge_delete_member(sc, bif); } } else if (IFNET_IS_VMNET(ifs)) { INC_ATOMIC_INT64_LIM(net_api_stats.nas_vmnet_total); } return error; } static int bridge_ioctl_del(struct bridge_softc *sc, void *arg) { struct ifbreq *req = arg; struct bridge_iflist *bif; bif = bridge_lookup_member(sc, req->ifbr_ifsname); if (bif == NULL) { return ENOENT; } bridge_delete_member(sc, bif); return 0; } static int bridge_ioctl_purge(struct bridge_softc *sc, void *arg) { #pragma unused(sc, arg) return 0; } static int bridge_ioctl_gifflags(struct bridge_softc *sc, void *arg) { struct ifbreq *req = arg; struct bridge_iflist *bif; bif = bridge_lookup_member(sc, req->ifbr_ifsname); if (bif == NULL) { return ENOENT; } struct bstp_port *bp; bp = &bif->bif_stp; req->ifbr_state = bp->bp_state; req->ifbr_priority = bp->bp_priority; req->ifbr_path_cost = bp->bp_path_cost; req->ifbr_proto = bp->bp_protover; req->ifbr_role = bp->bp_role; req->ifbr_stpflags = bp->bp_flags; req->ifbr_ifsflags = bif->bif_ifflags; /* Copy STP state options as flags */ if (bp->bp_operedge) { req->ifbr_ifsflags |= IFBIF_BSTP_EDGE; } if (bp->bp_flags & BSTP_PORT_AUTOEDGE) { req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE; } if (bp->bp_ptp_link) { req->ifbr_ifsflags |= IFBIF_BSTP_PTP; } if (bp->bp_flags & BSTP_PORT_AUTOPTP) { req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP; } if (bp->bp_flags & BSTP_PORT_ADMEDGE) { req->ifbr_ifsflags |= IFBIF_BSTP_ADMEDGE; } if (bp->bp_flags & BSTP_PORT_ADMCOST) { req->ifbr_ifsflags |= IFBIF_BSTP_ADMCOST; } req->ifbr_portno = bif->bif_ifp->if_index & 0xfff; req->ifbr_addrcnt = bif->bif_addrcnt; req->ifbr_addrmax = bif->bif_addrmax; req->ifbr_addrexceeded = bif->bif_addrexceeded; return 0; } static int bridge_ioctl_sifflags(struct bridge_softc *sc, void *arg) { struct ifbreq *req = arg; struct bridge_iflist *bif; #if BRIDGESTP struct bstp_port *bp; int error; #endif /* BRIDGESTP */ bif = bridge_lookup_member(sc, req->ifbr_ifsname); if (bif == NULL) { return ENOENT; } if (req->ifbr_ifsflags & IFBIF_SPAN) { /* SPAN is readonly */ return EINVAL; } #define _EXCLUSIVE_FLAGS (IFBIF_CHECKSUM_OFFLOAD | IFBIF_MAC_NAT) if ((req->ifbr_ifsflags & _EXCLUSIVE_FLAGS) == _EXCLUSIVE_FLAGS) { /* can't specify both MAC-NAT and checksum offload */ return EINVAL; } if ((req->ifbr_ifsflags & IFBIF_MAC_NAT) != 0) { errno_t error; error = bridge_mac_nat_enable(sc, bif); if (error != 0) { return error; } } else if (sc->sc_mac_nat_bif == bif) { bridge_mac_nat_disable(sc); } #if BRIDGESTP if (req->ifbr_ifsflags & IFBIF_STP) { if ((bif->bif_ifflags & IFBIF_STP) == 0) { error = bstp_enable(&bif->bif_stp); if (error) { return error; } } } else { if ((bif->bif_ifflags & IFBIF_STP) != 0) { bstp_disable(&bif->bif_stp); } } /* Pass on STP flags */ bp = &bif->bif_stp; bstp_set_edge(bp, req->ifbr_ifsflags & IFBIF_BSTP_EDGE ? 1 : 0); bstp_set_autoedge(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOEDGE ? 1 : 0); bstp_set_ptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_PTP ? 1 : 0); bstp_set_autoptp(bp, req->ifbr_ifsflags & IFBIF_BSTP_AUTOPTP ? 1 : 0); #else /* !BRIDGESTP */ if (req->ifbr_ifsflags & IFBIF_STP) { return EOPNOTSUPP; } #endif /* !BRIDGESTP */ /* Save the bits relating to the bridge */ bif->bif_ifflags = req->ifbr_ifsflags & IFBIFMASK; return 0; } static int bridge_ioctl_scache(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; sc->sc_brtmax = param->ifbrp_csize; bridge_rttrim(sc); return 0; } static int bridge_ioctl_gcache(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; param->ifbrp_csize = sc->sc_brtmax; return 0; } #define BRIDGE_IOCTL_GIFS do { \ struct bridge_iflist *bif; \ struct ifbreq breq; \ char *buf, *outbuf; \ unsigned int count, buflen, len; \ \ count = 0; \ TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) \ count++; \ TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) \ count++; \ \ buflen = sizeof (breq) * count; \ if (bifc->ifbic_len == 0) { \ bifc->ifbic_len = buflen; \ return (0); \ } \ BRIDGE_UNLOCK(sc); \ outbuf = (char *)kalloc_data(buflen, Z_WAITOK | Z_ZERO); \ BRIDGE_LOCK(sc); \ \ count = 0; \ buf = outbuf; \ len = min(bifc->ifbic_len, buflen); \ bzero(&breq, sizeof (breq)); \ TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { \ if (len < sizeof (breq)) \ break; \ \ snprintf(breq.ifbr_ifsname, sizeof (breq.ifbr_ifsname), \ "%s", bif->bif_ifp->if_xname); \ /* Fill in the ifbreq structure */ \ error = bridge_ioctl_gifflags(sc, &breq); \ if (error) \ break; \ memcpy(buf, &breq, sizeof (breq)); \ count++; \ buf += sizeof (breq); \ len -= sizeof (breq); \ } \ TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) { \ if (len < sizeof (breq)) \ break; \ \ snprintf(breq.ifbr_ifsname, \ sizeof (breq.ifbr_ifsname), \ "%s", bif->bif_ifp->if_xname); \ breq.ifbr_ifsflags = bif->bif_ifflags; \ breq.ifbr_portno \ = bif->bif_ifp->if_index & 0xfff; \ memcpy(buf, &breq, sizeof (breq)); \ count++; \ buf += sizeof (breq); \ len -= sizeof (breq); \ } \ \ BRIDGE_UNLOCK(sc); \ bifc->ifbic_len = sizeof (breq) * count; \ error = copyout(outbuf, bifc->ifbic_req, bifc->ifbic_len); \ BRIDGE_LOCK(sc); \ kfree_data(outbuf, buflen); \ } while (0) static int bridge_ioctl_gifs64(struct bridge_softc *sc, void *arg) { struct ifbifconf64 *bifc = arg; int error = 0; BRIDGE_IOCTL_GIFS; return error; } static int bridge_ioctl_gifs32(struct bridge_softc *sc, void *arg) { struct ifbifconf32 *bifc = arg; int error = 0; BRIDGE_IOCTL_GIFS; return error; } #define BRIDGE_IOCTL_RTS do { \ struct bridge_rtnode *brt; \ char *buf; \ char *outbuf = NULL; \ unsigned int count, buflen, len; \ unsigned long now; \ \ if (bac->ifbac_len == 0) \ return (0); \ \ bzero(&bareq, sizeof (bareq)); \ count = 0; \ LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) \ count++; \ buflen = sizeof (bareq) * count; \ \ BRIDGE_UNLOCK(sc); \ outbuf = (char *)kalloc_data(buflen, Z_WAITOK | Z_ZERO); \ BRIDGE_LOCK(sc); \ \ count = 0; \ buf = outbuf; \ len = min(bac->ifbac_len, buflen); \ LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { \ if (len < sizeof (bareq)) \ goto out; \ snprintf(bareq.ifba_ifsname, sizeof (bareq.ifba_ifsname), \ "%s", brt->brt_ifp->if_xname); \ memcpy(bareq.ifba_dst, brt->brt_addr, sizeof (brt->brt_addr)); \ bareq.ifba_vlan = brt->brt_vlan; \ if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { \ now = (unsigned long) net_uptime(); \ if (now < brt->brt_expire) \ bareq.ifba_expire = \ brt->brt_expire - now; \ } else \ bareq.ifba_expire = 0; \ bareq.ifba_flags = brt->brt_flags; \ \ memcpy(buf, &bareq, sizeof (bareq)); \ count++; \ buf += sizeof (bareq); \ len -= sizeof (bareq); \ } \ out: \ bac->ifbac_len = sizeof (bareq) * count; \ if (outbuf != NULL) { \ BRIDGE_UNLOCK(sc); \ error = copyout(outbuf, bac->ifbac_req, bac->ifbac_len); \ kfree_data(outbuf, buflen); \ BRIDGE_LOCK(sc); \ } \ return (error); \ } while (0) static int bridge_ioctl_rts64(struct bridge_softc *sc, void *arg) { struct ifbaconf64 *bac = arg; struct ifbareq64 bareq; int error = 0; BRIDGE_IOCTL_RTS; return error; } static int bridge_ioctl_rts32(struct bridge_softc *sc, void *arg) { struct ifbaconf32 *bac = arg; struct ifbareq32 bareq; int error = 0; BRIDGE_IOCTL_RTS; return error; } static int bridge_ioctl_saddr32(struct bridge_softc *sc, void *arg) { struct ifbareq32 *req = arg; struct bridge_iflist *bif; int error; bif = bridge_lookup_member(sc, req->ifba_ifsname); if (bif == NULL) { return ENOENT; } error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1, req->ifba_flags); return error; } static int bridge_ioctl_saddr64(struct bridge_softc *sc, void *arg) { struct ifbareq64 *req = arg; struct bridge_iflist *bif; int error; bif = bridge_lookup_member(sc, req->ifba_ifsname); if (bif == NULL) { return ENOENT; } error = bridge_rtupdate(sc, req->ifba_dst, req->ifba_vlan, bif, 1, req->ifba_flags); return error; } static int bridge_ioctl_sto(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; sc->sc_brttimeout = param->ifbrp_ctime; return 0; } static int bridge_ioctl_gto(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; param->ifbrp_ctime = sc->sc_brttimeout; return 0; } static int bridge_ioctl_daddr32(struct bridge_softc *sc, void *arg) { struct ifbareq32 *req = arg; return bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan); } static int bridge_ioctl_daddr64(struct bridge_softc *sc, void *arg) { struct ifbareq64 *req = arg; return bridge_rtdaddr(sc, req->ifba_dst, req->ifba_vlan); } static int bridge_ioctl_flush(struct bridge_softc *sc, void *arg) { struct ifbreq *req = arg; bridge_rtflush(sc, req->ifbr_ifsflags); return 0; } static int bridge_ioctl_gpri(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; struct bstp_state *bs = &sc->sc_stp; param->ifbrp_prio = bs->bs_bridge_priority; return 0; } static int bridge_ioctl_spri(struct bridge_softc *sc, void *arg) { #if BRIDGESTP struct ifbrparam *param = arg; return bstp_set_priority(&sc->sc_stp, param->ifbrp_prio); #else /* !BRIDGESTP */ #pragma unused(sc, arg) return EOPNOTSUPP; #endif /* !BRIDGESTP */ } static int bridge_ioctl_ght(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; struct bstp_state *bs = &sc->sc_stp; param->ifbrp_hellotime = bs->bs_bridge_htime >> 8; return 0; } static int bridge_ioctl_sht(struct bridge_softc *sc, void *arg) { #if BRIDGESTP struct ifbrparam *param = arg; return bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime); #else /* !BRIDGESTP */ #pragma unused(sc, arg) return EOPNOTSUPP; #endif /* !BRIDGESTP */ } static int bridge_ioctl_gfd(struct bridge_softc *sc, void *arg) { struct ifbrparam *param; struct bstp_state *bs; param = arg; bs = &sc->sc_stp; param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8; return 0; } static int bridge_ioctl_sfd(struct bridge_softc *sc, void *arg) { #if BRIDGESTP struct ifbrparam *param = arg; return bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay); #else /* !BRIDGESTP */ #pragma unused(sc, arg) return EOPNOTSUPP; #endif /* !BRIDGESTP */ } static int bridge_ioctl_gma(struct bridge_softc *sc, void *arg) { struct ifbrparam *param; struct bstp_state *bs; param = arg; bs = &sc->sc_stp; param->ifbrp_maxage = bs->bs_bridge_max_age >> 8; return 0; } static int bridge_ioctl_sma(struct bridge_softc *sc, void *arg) { #if BRIDGESTP struct ifbrparam *param = arg; return bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage); #else /* !BRIDGESTP */ #pragma unused(sc, arg) return EOPNOTSUPP; #endif /* !BRIDGESTP */ } static int bridge_ioctl_sifprio(struct bridge_softc *sc, void *arg) { #if BRIDGESTP struct ifbreq *req = arg; struct bridge_iflist *bif; bif = bridge_lookup_member(sc, req->ifbr_ifsname); if (bif == NULL) { return ENOENT; } return bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority); #else /* !BRIDGESTP */ #pragma unused(sc, arg) return EOPNOTSUPP; #endif /* !BRIDGESTP */ } static int bridge_ioctl_sifcost(struct bridge_softc *sc, void *arg) { #if BRIDGESTP struct ifbreq *req = arg; struct bridge_iflist *bif; bif = bridge_lookup_member(sc, req->ifbr_ifsname); if (bif == NULL) { return ENOENT; } return bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost); #else /* !BRIDGESTP */ #pragma unused(sc, arg) return EOPNOTSUPP; #endif /* !BRIDGESTP */ } static int bridge_ioctl_gfilt(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; param->ifbrp_filter = sc->sc_filter_flags; return 0; } static int bridge_ioctl_sfilt(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; if (param->ifbrp_filter & ~IFBF_FILT_MASK) { return EINVAL; } if (param->ifbrp_filter & IFBF_FILT_USEIPF) { return EINVAL; } sc->sc_filter_flags = param->ifbrp_filter; return 0; } static int bridge_ioctl_sifmaxaddr(struct bridge_softc *sc, void *arg) { struct ifbreq *req = arg; struct bridge_iflist *bif; bif = bridge_lookup_member(sc, req->ifbr_ifsname); if (bif == NULL) { return ENOENT; } bif->bif_addrmax = req->ifbr_addrmax; return 0; } static int bridge_ioctl_addspan(struct bridge_softc *sc, void *arg) { struct ifbreq *req = arg; struct bridge_iflist *bif = NULL; struct ifnet *ifs; ifs = ifunit(req->ifbr_ifsname); if (ifs == NULL) { return ENOENT; } if (IFNET_IS_INTCOPROC(ifs)) { return EINVAL; } TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) if (ifs == bif->bif_ifp) { return EBUSY; } if (ifs->if_bridge != NULL) { return EBUSY; } switch (ifs->if_type) { case IFT_ETHER: case IFT_L2VLAN: case IFT_IEEE8023ADLAG: break; case IFT_GIF: /* currently not supported */ /* FALLTHRU */ default: return EINVAL; } bif = kalloc_type(struct bridge_iflist, Z_WAITOK | Z_ZERO | Z_NOFAIL); bif->bif_ifp = ifs; bif->bif_ifflags = IFBIF_SPAN; ifnet_reference(bif->bif_ifp); TAILQ_INSERT_HEAD(&sc->sc_spanlist, bif, bif_next); return 0; } static int bridge_ioctl_delspan(struct bridge_softc *sc, void *arg) { struct ifbreq *req = arg; struct bridge_iflist *bif; struct ifnet *ifs; ifs = ifunit(req->ifbr_ifsname); if (ifs == NULL) { return ENOENT; } TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) if (ifs == bif->bif_ifp) { break; } if (bif == NULL) { return ENOENT; } bridge_delete_span(sc, bif); return 0; } #define BRIDGE_IOCTL_GBPARAM do { \ struct bstp_state *bs = &sc->sc_stp; \ struct bstp_port *root_port; \ \ req->ifbop_maxage = bs->bs_bridge_max_age >> 8; \ req->ifbop_hellotime = bs->bs_bridge_htime >> 8; \ req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8; \ \ root_port = bs->bs_root_port; \ if (root_port == NULL) \ req->ifbop_root_port = 0; \ else \ req->ifbop_root_port = root_port->bp_ifp->if_index; \ \ req->ifbop_holdcount = bs->bs_txholdcount; \ req->ifbop_priority = bs->bs_bridge_priority; \ req->ifbop_protocol = bs->bs_protover; \ req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost; \ req->ifbop_bridgeid = bs->bs_bridge_pv.pv_dbridge_id; \ req->ifbop_designated_root = bs->bs_root_pv.pv_root_id; \ req->ifbop_designated_bridge = bs->bs_root_pv.pv_dbridge_id; \ req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec; \ req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec; \ } while (0) static int bridge_ioctl_gbparam32(struct bridge_softc *sc, void *arg) { struct ifbropreq32 *req = arg; BRIDGE_IOCTL_GBPARAM; return 0; } static int bridge_ioctl_gbparam64(struct bridge_softc *sc, void *arg) { struct ifbropreq64 *req = arg; BRIDGE_IOCTL_GBPARAM; return 0; } static int bridge_ioctl_grte(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; param->ifbrp_cexceeded = sc->sc_brtexceeded; return 0; } #define BRIDGE_IOCTL_GIFSSTP do { \ struct bridge_iflist *bif; \ struct bstp_port *bp; \ struct ifbpstpreq bpreq; \ char *buf, *outbuf; \ unsigned int count, buflen, len; \ \ count = 0; \ TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { \ if ((bif->bif_ifflags & IFBIF_STP) != 0) \ count++; \ } \ \ buflen = sizeof (bpreq) * count; \ if (bifstp->ifbpstp_len == 0) { \ bifstp->ifbpstp_len = buflen; \ return (0); \ } \ \ BRIDGE_UNLOCK(sc); \ outbuf = (char *)kalloc_data(buflen, Z_WAITOK | Z_ZERO); \ BRIDGE_LOCK(sc); \ \ count = 0; \ buf = outbuf; \ len = min(bifstp->ifbpstp_len, buflen); \ bzero(&bpreq, sizeof (bpreq)); \ TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { \ if (len < sizeof (bpreq)) \ break; \ \ if ((bif->bif_ifflags & IFBIF_STP) == 0) \ continue; \ \ bp = &bif->bif_stp; \ bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff; \ bpreq.ifbp_fwd_trans = bp->bp_forward_transitions; \ bpreq.ifbp_design_cost = bp->bp_desg_pv.pv_cost; \ bpreq.ifbp_design_port = bp->bp_desg_pv.pv_port_id; \ bpreq.ifbp_design_bridge = bp->bp_desg_pv.pv_dbridge_id; \ bpreq.ifbp_design_root = bp->bp_desg_pv.pv_root_id; \ \ memcpy(buf, &bpreq, sizeof (bpreq)); \ count++; \ buf += sizeof (bpreq); \ len -= sizeof (bpreq); \ } \ \ BRIDGE_UNLOCK(sc); \ bifstp->ifbpstp_len = sizeof (bpreq) * count; \ error = copyout(outbuf, bifstp->ifbpstp_req, bifstp->ifbpstp_len); \ BRIDGE_LOCK(sc); \ kfree_data(outbuf, buflen); \ return (error); \ } while (0) static int bridge_ioctl_gifsstp32(struct bridge_softc *sc, void *arg) { struct ifbpstpconf32 *bifstp = arg; int error = 0; BRIDGE_IOCTL_GIFSSTP; return error; } static int bridge_ioctl_gifsstp64(struct bridge_softc *sc, void *arg) { struct ifbpstpconf64 *bifstp = arg; int error = 0; BRIDGE_IOCTL_GIFSSTP; return error; } static int bridge_ioctl_sproto(struct bridge_softc *sc, void *arg) { #if BRIDGESTP struct ifbrparam *param = arg; return bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto); #else /* !BRIDGESTP */ #pragma unused(sc, arg) return EOPNOTSUPP; #endif /* !BRIDGESTP */ } static int bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg) { #if BRIDGESTP struct ifbrparam *param = arg; return bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc); #else /* !BRIDGESTP */ #pragma unused(sc, arg) return EOPNOTSUPP; #endif /* !BRIDGESTP */ } static int bridge_ioctl_ghostfilter(struct bridge_softc *sc, void *arg) { struct ifbrhostfilter *req = arg; struct bridge_iflist *bif; bif = bridge_lookup_member(sc, req->ifbrhf_ifsname); if (bif == NULL) { return ENOENT; } bzero(req, sizeof(struct ifbrhostfilter)); if (bif->bif_flags & BIFF_HOST_FILTER) { req->ifbrhf_flags |= IFBRHF_ENABLED; bcopy(bif->bif_hf_hwsrc, req->ifbrhf_hwsrca, ETHER_ADDR_LEN); req->ifbrhf_ipsrc = bif->bif_hf_ipsrc.s_addr; } return 0; } static int bridge_ioctl_shostfilter(struct bridge_softc *sc, void *arg) { struct ifbrhostfilter *req = arg; struct bridge_iflist *bif; bif = bridge_lookup_member(sc, req->ifbrhf_ifsname); if (bif == NULL) { return ENOENT; } if (req->ifbrhf_flags & IFBRHF_ENABLED) { bif->bif_flags |= BIFF_HOST_FILTER; if (req->ifbrhf_flags & IFBRHF_HWSRC) { bcopy(req->ifbrhf_hwsrca, bif->bif_hf_hwsrc, ETHER_ADDR_LEN); if (bcmp(req->ifbrhf_hwsrca, ethernulladdr, ETHER_ADDR_LEN) != 0) { bif->bif_flags |= BIFF_HF_HWSRC; } else { bif->bif_flags &= ~BIFF_HF_HWSRC; } } if (req->ifbrhf_flags & IFBRHF_IPSRC) { bif->bif_hf_ipsrc.s_addr = req->ifbrhf_ipsrc; if (bif->bif_hf_ipsrc.s_addr != INADDR_ANY) { bif->bif_flags |= BIFF_HF_IPSRC; } else { bif->bif_flags &= ~BIFF_HF_IPSRC; } } } else { bif->bif_flags &= ~(BIFF_HOST_FILTER | BIFF_HF_HWSRC | BIFF_HF_IPSRC); bzero(bif->bif_hf_hwsrc, ETHER_ADDR_LEN); bif->bif_hf_ipsrc.s_addr = INADDR_ANY; } return 0; } static char * bridge_mac_nat_entry_out(struct mac_nat_entry_list * list, unsigned int * count_p, char *buf, unsigned int *len_p) { unsigned int count = *count_p; struct ifbrmne ifbmne; unsigned int len = *len_p; struct mac_nat_entry *mne; unsigned long now; bzero(&ifbmne, sizeof(ifbmne)); LIST_FOREACH(mne, list, mne_list) { if (len < sizeof(ifbmne)) { break; } snprintf(ifbmne.ifbmne_ifname, sizeof(ifbmne.ifbmne_ifname), "%s", mne->mne_bif->bif_ifp->if_xname); memcpy(ifbmne.ifbmne_mac, mne->mne_mac, sizeof(ifbmne.ifbmne_mac)); now = (unsigned long) net_uptime(); if (now < mne->mne_expire) { ifbmne.ifbmne_expire = mne->mne_expire - now; } else { ifbmne.ifbmne_expire = 0; } if ((mne->mne_flags & MNE_FLAGS_IPV6) != 0) { ifbmne.ifbmne_af = AF_INET6; ifbmne.ifbmne_ip6_addr = mne->mne_ip6; } else { ifbmne.ifbmne_af = AF_INET; ifbmne.ifbmne_ip_addr = mne->mne_ip; } memcpy(buf, &ifbmne, sizeof(ifbmne)); count++; buf += sizeof(ifbmne); len -= sizeof(ifbmne); } *count_p = count; *len_p = len; return buf; } /* * bridge_ioctl_gmnelist() * Perform the get mac_nat_entry list ioctl. * * Note: * The struct ifbrmnelist32 and struct ifbrmnelist64 have the same * field size/layout except for the last field ifbml_buf, the user-supplied * buffer pointer. That is passed in separately via the 'user_addr' * parameter from the respective 32-bit or 64-bit ioctl routine. */ static int bridge_ioctl_gmnelist(struct bridge_softc *sc, struct ifbrmnelist32 *mnl, user_addr_t user_addr) { unsigned int count; char *buf; int error = 0; char *outbuf = NULL; struct mac_nat_entry *mne; unsigned int buflen; unsigned int len; mnl->ifbml_elsize = sizeof(struct ifbrmne); count = 0; LIST_FOREACH(mne, &sc->sc_mne_list, mne_list) { count++; } LIST_FOREACH(mne, &sc->sc_mne_list_v6, mne_list) { count++; } buflen = sizeof(struct ifbrmne) * count; if (buflen == 0 || mnl->ifbml_len == 0) { mnl->ifbml_len = buflen; return error; } BRIDGE_UNLOCK(sc); outbuf = (char *)kalloc_data(buflen, Z_WAITOK | Z_ZERO); BRIDGE_LOCK(sc); count = 0; buf = outbuf; len = min(mnl->ifbml_len, buflen); buf = bridge_mac_nat_entry_out(&sc->sc_mne_list, &count, buf, &len); buf = bridge_mac_nat_entry_out(&sc->sc_mne_list_v6, &count, buf, &len); mnl->ifbml_len = count * sizeof(struct ifbrmne); BRIDGE_UNLOCK(sc); error = copyout(outbuf, user_addr, mnl->ifbml_len); kfree_data(outbuf, buflen); BRIDGE_LOCK(sc); return error; } static int bridge_ioctl_gmnelist64(struct bridge_softc *sc, void *arg) { struct ifbrmnelist64 *mnl = arg; return bridge_ioctl_gmnelist(sc, arg, mnl->ifbml_buf); } static int bridge_ioctl_gmnelist32(struct bridge_softc *sc, void *arg) { struct ifbrmnelist32 *mnl = arg; return bridge_ioctl_gmnelist(sc, arg, CAST_USER_ADDR_T(mnl->ifbml_buf)); } /* * bridge_ioctl_gifstats() * Return per-member stats. * * Note: * The ifbrmreq32 and ifbrmreq64 structures have the same * field size/layout except for the last field brmr_buf, the user-supplied * buffer pointer. That is passed in separately via the 'user_addr' * parameter from the respective 32-bit or 64-bit ioctl routine. */ static int bridge_ioctl_gifstats(struct bridge_softc *sc, struct ifbrmreq32 *mreq, user_addr_t user_addr) { struct bridge_iflist *bif; int error = 0; unsigned int buflen; bif = bridge_lookup_member(sc, mreq->brmr_ifname); if (bif == NULL) { error = ENOENT; goto done; } buflen = mreq->brmr_elsize = sizeof(struct ifbrmstats); if (buflen == 0 || mreq->brmr_len == 0) { mreq->brmr_len = buflen; goto done; } if (mreq->brmr_len != 0 && mreq->brmr_len < buflen) { error = ENOBUFS; goto done; } mreq->brmr_len = buflen; error = copyout(&bif->bif_stats, user_addr, buflen); done: return error; } static int bridge_ioctl_gifstats32(struct bridge_softc *sc, void *arg) { struct ifbrmreq32 *mreq = arg; return bridge_ioctl_gifstats(sc, arg, mreq->brmr_buf); } static int bridge_ioctl_gifstats64(struct bridge_softc *sc, void *arg) { struct ifbrmreq64 *mreq = arg; return bridge_ioctl_gifstats(sc, arg, mreq->brmr_buf); } /* * bridge_proto_attach_changed * * Called when protocol attachment on the interface changes. */ static void bridge_proto_attach_changed(struct ifnet *ifp) { boolean_t changed = FALSE; struct bridge_iflist *bif; boolean_t input_broadcast; struct bridge_softc *sc = ifp->if_bridge; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s", ifp->if_xname); if (sc == NULL) { return; } input_broadcast = interface_needs_input_broadcast(ifp); BRIDGE_LOCK(sc); bif = bridge_lookup_member_if(sc, ifp); if (bif != NULL) { changed = bif_set_input_broadcast(bif, input_broadcast); } BRIDGE_UNLOCK(sc); if (changed) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s input broadcast %s", ifp->if_xname, input_broadcast ? "ENABLED" : "DISABLED"); } return; } /* * interface_media_active: * * Tells if an interface media is active. */ static int interface_media_active(struct ifnet *ifp) { struct ifmediareq ifmr; int status = 0; bzero(&ifmr, sizeof(ifmr)); if (ifnet_ioctl(ifp, 0, SIOCGIFMEDIA, &ifmr) == 0) { if ((ifmr.ifm_status & IFM_AVALID) && ifmr.ifm_count > 0) { status = ifmr.ifm_status & IFM_ACTIVE ? 1 : 0; } } return status; } /* * bridge_updatelinkstatus: * * Update the media active status of the bridge based on the * media active status of its member. * If changed, return the corresponding onf/off link event. */ static u_int32_t bridge_updatelinkstatus(struct bridge_softc *sc) { struct bridge_iflist *bif; int active_member = 0; u_int32_t event_code = 0; BRIDGE_LOCK_ASSERT_HELD(sc); /* * Find out if we have an active interface */ TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { if (bif->bif_flags & BIFF_MEDIA_ACTIVE) { active_member = 1; break; } } if (active_member && !(sc->sc_flags & SCF_MEDIA_ACTIVE)) { sc->sc_flags |= SCF_MEDIA_ACTIVE; event_code = KEV_DL_LINK_ON; } else if (!active_member && (sc->sc_flags & SCF_MEDIA_ACTIVE)) { sc->sc_flags &= ~SCF_MEDIA_ACTIVE; event_code = KEV_DL_LINK_OFF; } return event_code; } /* * bridge_iflinkevent: */ static void bridge_iflinkevent(struct ifnet *ifp) { struct bridge_softc *sc = ifp->if_bridge; struct bridge_iflist *bif; u_int32_t event_code = 0; int media_active; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s", ifp->if_xname); /* Check if the interface is a bridge member */ if (sc == NULL) { return; } media_active = interface_media_active(ifp); BRIDGE_LOCK(sc); bif = bridge_lookup_member_if(sc, ifp); if (bif != NULL) { if (media_active) { bif->bif_flags |= BIFF_MEDIA_ACTIVE; } else { bif->bif_flags &= ~BIFF_MEDIA_ACTIVE; } if (sc->sc_mac_nat_bif != NULL) { bridge_mac_nat_flush_entries(sc, bif); } event_code = bridge_updatelinkstatus(sc); } BRIDGE_UNLOCK(sc); if (event_code != 0) { bridge_link_event(sc->sc_ifp, event_code); } } /* * bridge_delayed_callback: * * Makes a delayed call */ static void bridge_delayed_callback(void *param, __unused void *param2) { struct bridge_delayed_call *call = (struct bridge_delayed_call *)param; struct bridge_softc *sc = call->bdc_sc; #if BRIDGE_DELAYED_CALLBACK_DEBUG if (bridge_delayed_callback_delay > 0) { struct timespec ts; ts.tv_sec = bridge_delayed_callback_delay; ts.tv_nsec = 0; BRIDGE_LOG(LOG_NOTICE, 0, "sleeping for %d seconds", bridge_delayed_callback_delay); msleep(&bridge_delayed_callback_delay, NULL, PZERO, __func__, &ts); BRIDGE_LOG(LOG_NOTICE, 0, "awoken"); } #endif /* BRIDGE_DELAYED_CALLBACK_DEBUG */ BRIDGE_LOCK(sc); #if BRIDGE_DELAYED_CALLBACK_DEBUG BRIDGE_LOG(LOG_DEBUG, BR_DBGF_DELAYED_CALL, "%s call 0x%llx flags 0x%x", sc->sc_if_xname, (uint64_t)VM_KERNEL_ADDRPERM(call), call->bdc_flags); } #endif /* BRIDGE_DELAYED_CALLBACK_DEBUG */ if (call->bdc_flags & BDCF_CANCELLING) { wakeup(call); } else { if ((sc->sc_flags & SCF_DETACHING) == 0) { (*call->bdc_func)(sc); } } call->bdc_flags &= ~BDCF_OUTSTANDING; BRIDGE_UNLOCK(sc); } /* * bridge_schedule_delayed_call: * * Schedule a function to be called on a separate thread * The actual call may be scheduled to run at a given time or ASAP. */ static void bridge_schedule_delayed_call(struct bridge_delayed_call *call) { uint64_t deadline = 0; struct bridge_softc *sc = call->bdc_sc; BRIDGE_LOCK_ASSERT_HELD(sc); if ((sc->sc_flags & SCF_DETACHING) || (call->bdc_flags & (BDCF_OUTSTANDING | BDCF_CANCELLING))) { return; } if (call->bdc_ts.tv_sec || call->bdc_ts.tv_nsec) { nanoseconds_to_absolutetime( (uint64_t)call->bdc_ts.tv_sec * NSEC_PER_SEC + call->bdc_ts.tv_nsec, &deadline); clock_absolutetime_interval_to_deadline(deadline, &deadline); } call->bdc_flags = BDCF_OUTSTANDING; #if BRIDGE_DELAYED_CALLBACK_DEBUG BRIDGE_LOG(LOG_DEBUG, BR_DBGF_DELAYED_CALL, "%s call 0x%llx flags 0x%x", sc->sc_if_xname, (uint64_t)VM_KERNEL_ADDRPERM(call), call->bdc_flags); } #endif /* BRIDGE_DELAYED_CALLBACK_DEBUG */ if (call->bdc_ts.tv_sec || call->bdc_ts.tv_nsec) { thread_call_func_delayed( (thread_call_func_t)bridge_delayed_callback, call, deadline); } else { if (call->bdc_thread_call == NULL) { call->bdc_thread_call = thread_call_allocate( (thread_call_func_t)bridge_delayed_callback, call); } thread_call_enter(call->bdc_thread_call); } } /* * bridge_cancel_delayed_call: * * Cancel a queued or running delayed call. * If call is running, does not return until the call is done to * prevent race condition with the brigde interface getting destroyed */ static void bridge_cancel_delayed_call(struct bridge_delayed_call *call) { boolean_t result; struct bridge_softc *sc = call->bdc_sc; /* * The call was never scheduled */ if (sc == NULL) { return; } BRIDGE_LOCK_ASSERT_HELD(sc); call->bdc_flags |= BDCF_CANCELLING; while (call->bdc_flags & BDCF_OUTSTANDING) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_DELAYED_CALL, "%s call 0x%llx flags 0x%x", sc->sc_if_xname, (uint64_t)VM_KERNEL_ADDRPERM(call), call->bdc_flags); result = thread_call_func_cancel( (thread_call_func_t)bridge_delayed_callback, call, FALSE); if (result) { /* * We managed to dequeue the delayed call */ call->bdc_flags &= ~BDCF_OUTSTANDING; } else { /* * Wait for delayed call do be done running */ msleep(call, &sc->sc_mtx, PZERO, __func__, NULL); } } call->bdc_flags &= ~BDCF_CANCELLING; } /* * bridge_cleanup_delayed_call: * * Dispose resource allocated for a delayed call * Assume the delayed call is not queued or running . */ static void bridge_cleanup_delayed_call(struct bridge_delayed_call *call) { boolean_t result; struct bridge_softc *sc = call->bdc_sc; /* * The call was never scheduled */ if (sc == NULL) { return; } BRIDGE_LOCK_ASSERT_HELD(sc); VERIFY((call->bdc_flags & BDCF_OUTSTANDING) == 0); VERIFY((call->bdc_flags & BDCF_CANCELLING) == 0); if (call->bdc_thread_call != NULL) { result = thread_call_free(call->bdc_thread_call); if (result == FALSE) { panic("%s thread_call_free() failed for call %p", __func__, call); } call->bdc_thread_call = NULL; } } /* * bridge_init: * * Initialize a bridge interface. */ static int bridge_init(struct ifnet *ifp) { struct bridge_softc *sc = (struct bridge_softc *)ifp->if_softc; errno_t error; BRIDGE_LOCK_ASSERT_HELD(sc); if ((ifnet_flags(ifp) & IFF_RUNNING)) { return 0; } error = ifnet_set_flags(ifp, IFF_RUNNING, IFF_RUNNING); /* * Calling bridge_aging_timer() is OK as there are no entries to * age so we're just going to arm the timer */ bridge_aging_timer(sc); #if BRIDGESTP if (error == 0) { bstp_init(&sc->sc_stp); /* Initialize Spanning Tree */ } #endif /* BRIDGESTP */ return error; } /* * bridge_ifstop: * * Stop the bridge interface. */ static void bridge_ifstop(struct ifnet *ifp, int disable) { #pragma unused(disable) struct bridge_softc *sc = ifp->if_softc; BRIDGE_LOCK_ASSERT_HELD(sc); if ((ifnet_flags(ifp) & IFF_RUNNING) == 0) { return; } bridge_cancel_delayed_call(&sc->sc_aging_timer); #if BRIDGESTP bstp_stop(&sc->sc_stp); #endif /* BRIDGESTP */ bridge_rtflush(sc, IFBF_FLUSHDYN); (void) ifnet_set_flags(ifp, 0, IFF_RUNNING); } /* * bridge_compute_cksum: * * If the packet has checksum flags, compare the hardware checksum * capabilities of the source and destination interfaces. If they * are the same, there's nothing to do. If they are different, * finalize the checksum so that it can be sent on the destination * interface. */ static void bridge_compute_cksum(struct ifnet *src_if, struct ifnet *dst_if, struct mbuf *m) { uint32_t csum_flags; uint16_t dst_hw_csum; uint32_t did_sw = 0; struct ether_header *eh; uint16_t src_hw_csum; if (src_if == dst_if) { return; } csum_flags = m->m_pkthdr.csum_flags & IF_HWASSIST_CSUM_MASK; if (csum_flags == 0) { /* no checksum offload */ return; } /* * if destination/source differ in checksum offload * capabilities, finalize/compute the checksum */ dst_hw_csum = IF_HWASSIST_CSUM_FLAGS(dst_if->if_hwassist); src_hw_csum = IF_HWASSIST_CSUM_FLAGS(src_if->if_hwassist); if (dst_hw_csum == src_hw_csum) { return; } eh = mtod(m, struct ether_header *); switch (ntohs(eh->ether_type)) { case ETHERTYPE_IP: did_sw = in_finalize_cksum(m, sizeof(*eh), csum_flags); break; case ETHERTYPE_IPV6: did_sw = in6_finalize_cksum(m, sizeof(*eh), -1, -1, csum_flags); break; } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "[%s -> %s] before 0x%x did 0x%x after 0x%x", src_if->if_xname, dst_if->if_xname, csum_flags, did_sw, m->m_pkthdr.csum_flags); } static errno_t bridge_transmit(struct ifnet * ifp, struct mbuf *m) { struct flowadv adv = { .code = FADV_SUCCESS }; errno_t error; error = dlil_output(ifp, 0, m, NULL, NULL, 1, &adv); if (error == 0) { if (adv.code == FADV_FLOW_CONTROLLED) { error = EQFULL; } else if (adv.code == FADV_SUSPENDED) { error = EQSUSPENDED; } } return error; } static int get_last_ip6_hdr(struct mbuf *m, int off, int proto, int * nxtp, bool *is_fragmented) { int newoff; *is_fragmented = false; while (1) { newoff = ip6_nexthdr(m, off, proto, nxtp); if (newoff < 0) { return off; } else if (newoff < off) { return -1; /* invalid */ } else if (newoff == off) { return newoff; } off = newoff; proto = *nxtp; if (proto == IPPROTO_FRAGMENT) { *is_fragmented = true; } } } static int bridge_get_ip_proto(struct mbuf * * mp, u_int mac_hlen, bool is_ipv4, ip_packet_info_t info_p, struct bripstats * stats_p) { int error = 0; u_int hlen; u_int ip_hlen; u_int ip_pay_len; struct mbuf * m0 = *mp; int off; int opt_len = 0; int proto = 0; bzero(info_p, sizeof(*info_p)); if (is_ipv4) { struct ip * ip; u_int ip_total_len; /* IPv4 */ hlen = mac_hlen + sizeof(struct ip); if (m0->m_pkthdr.len < hlen) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "Short IP packet %d < %d", m0->m_pkthdr.len, hlen); error = _EBADIP; stats_p->bips_bad_ip++; goto done; } if (m0->m_len < hlen) { *mp = m0 = m_pullup(m0, hlen); if (m0 == NULL) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "m_pullup failed hlen %d", hlen); error = ENOBUFS; stats_p->bips_bad_ip++; goto done; } } ip = (struct ip *)(void *)(mtod(m0, uint8_t *) + mac_hlen); if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "bad IP version"); error = _EBADIP; stats_p->bips_bad_ip++; goto done; } ip_hlen = IP_VHL_HL(ip->ip_vhl) << 2; if (ip_hlen < sizeof(struct ip)) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "bad IP header length %d < %d", ip_hlen, (int)sizeof(struct ip)); error = _EBADIP; stats_p->bips_bad_ip++; goto done; } hlen = mac_hlen + ip_hlen; if (m0->m_len < hlen) { *mp = m0 = m_pullup(m0, hlen); if (m0 == NULL) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "m_pullup failed hlen %d", hlen); error = ENOBUFS; stats_p->bips_bad_ip++; goto done; } } ip_total_len = ntohs(ip->ip_len); if (ip_total_len < ip_hlen) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "IP total len %d < header len %d", ip_total_len, ip_hlen); error = _EBADIP; stats_p->bips_bad_ip++; goto done; } if (ip_total_len > (m0->m_pkthdr.len - mac_hlen)) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "invalid IP payload length %d > %d", ip_total_len, (m0->m_pkthdr.len - mac_hlen)); error = _EBADIP; stats_p->bips_bad_ip++; goto done; } ip_pay_len = ip_total_len - ip_hlen; info_p->ip_proto = ip->ip_p; info_p->ip_hdr.ip = ip; #define FRAG_BITS (IP_OFFMASK | IP_MF) if ((ntohs(ip->ip_off) & FRAG_BITS) != 0) { info_p->ip_is_fragmented = true; } stats_p->bips_ip++; } else { struct ip6_hdr *ip6; /* IPv6 */ hlen = mac_hlen + sizeof(struct ip6_hdr); if (m0->m_pkthdr.len < hlen) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "short IPv6 packet %d < %d", m0->m_pkthdr.len, hlen); error = _EBADIPV6; stats_p->bips_bad_ip6++; goto done; } if (m0->m_len < hlen) { *mp = m0 = m_pullup(m0, hlen); if (m0 == NULL) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "m_pullup failed hlen %d", hlen); error = ENOBUFS; stats_p->bips_bad_ip6++; goto done; } } ip6 = (struct ip6_hdr *)(mtod(m0, uint8_t *) + mac_hlen); if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "bad IPv6 version"); error = _EBADIPV6; stats_p->bips_bad_ip6++; goto done; } off = get_last_ip6_hdr(m0, mac_hlen, IPPROTO_IPV6, &proto, &info_p->ip_is_fragmented); if (off < 0 || m0->m_pkthdr.len < off) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "ip6_lasthdr() returned %d", off); error = _EBADIPV6; stats_p->bips_bad_ip6++; goto done; } ip_hlen = sizeof(*ip6); opt_len = off - mac_hlen - ip_hlen; if (opt_len < 0) { error = _EBADIPV6; stats_p->bips_bad_ip6++; goto done; } info_p->ip_proto = proto; info_p->ip_hdr.ip6 = ip6; ip_pay_len = ntohs(ip6->ip6_plen); if (ip_pay_len > (m0->m_pkthdr.len - mac_hlen - ip_hlen)) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "invalid IPv6 payload length %d > %d", ip_pay_len, (m0->m_pkthdr.len - mac_hlen - ip_hlen)); error = _EBADIPV6; stats_p->bips_bad_ip6++; goto done; } stats_p->bips_ip6++; } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "IPv%c proto %d ip %u pay %u opt %u pkt %u%s", is_ipv4 ? '4' : '6', proto, ip_hlen, ip_pay_len, opt_len, m0->m_pkthdr.len, info_p->ip_is_fragmented ? " frag" : ""); info_p->ip_hlen = ip_hlen; info_p->ip_pay_len = ip_pay_len; info_p->ip_opt_len = opt_len; info_p->ip_is_ipv4 = is_ipv4; done: return error; } static int bridge_get_tcp_header(struct mbuf * * mp, u_int mac_hlen, bool is_ipv4, ip_packet_info_t info_p, struct bripstats * stats_p) { int error; u_int hlen; error = bridge_get_ip_proto(mp, mac_hlen, is_ipv4, info_p, stats_p); if (error != 0) { goto done; } if (info_p->ip_proto != IPPROTO_TCP) { /* not a TCP frame, not an error, just a bad guess */ BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "non-TCP (%d) IPv%c frame %d bytes", info_p->ip_proto, is_ipv4 ? '4' : '6', (*mp)->m_pkthdr.len); goto done; } if (info_p->ip_is_fragmented) { /* both TSO and IP fragmentation don't make sense */ BRIDGE_LOG(LOG_NOTICE, BR_DBGF_CHECKSUM, "fragmented TSO packet?"); stats_p->bips_bad_tcp++; error = _EBADTCP; goto done; } hlen = mac_hlen + info_p->ip_hlen + sizeof(struct tcphdr) + info_p->ip_opt_len; if ((*mp)->m_len < hlen) { *mp = m_pullup(*mp, hlen); if (*mp == NULL) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "m_pullup %d failed", hlen); stats_p->bips_bad_tcp++; error = _EBADTCP; goto done; } } info_p->ip_proto_hdr = ((caddr_t)info_p->ip_hdr.ptr) + info_p->ip_hlen + info_p->ip_opt_len; done: return error; } static inline void proto_csum_stats_increment(uint8_t proto, struct brcsumstats * stats_p) { if (proto == IPPROTO_TCP) { stats_p->brcs_tcp_checksum++; } else { stats_p->brcs_udp_checksum++; } return; } static bool ether_header_type_is_ip(struct ether_header * eh, bool *is_ipv4) { uint16_t ether_type; bool is_ip = TRUE; ether_type = ntohs(eh->ether_type); switch (ether_type) { case ETHERTYPE_IP: *is_ipv4 = TRUE; break; case ETHERTYPE_IPV6: *is_ipv4 = FALSE; break; default: is_ip = FALSE; break; } return is_ip; } static errno_t bridge_verify_checksum(struct mbuf * * mp, struct ifbrmstats *stats_p) { struct brcsumstats *csum_stats_p; struct ether_header *eh; errno_t error = 0; ip_packet_info info; bool is_ipv4; struct mbuf * m; u_int mac_hlen = sizeof(struct ether_header); uint16_t sum; bool valid; eh = mtod(*mp, struct ether_header *); if (!ether_header_type_is_ip(eh, &is_ipv4)) { goto done; } error = bridge_get_ip_proto(mp, mac_hlen, is_ipv4, &info, &stats_p->brms_out_ip); m = *mp; if (error != 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "bridge_get_ip_proto failed %d", error); goto done; } if (is_ipv4) { if ((m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) != 0) { /* hardware offloaded IP header checksum */ valid = (m->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "IP checksum HW %svalid", valid ? "" : "in"); if (!valid) { stats_p->brms_out_cksum_bad_hw.brcs_ip_checksum++; error = _EBADIPCHECKSUM; goto done; } stats_p->brms_out_cksum_good_hw.brcs_ip_checksum++; } else { /* verify */ sum = inet_cksum(m, 0, mac_hlen, info.ip_hlen); valid = (sum == 0); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "IP checksum SW %svalid", valid ? "" : "in"); if (!valid) { stats_p->brms_out_cksum_bad.brcs_ip_checksum++; error = _EBADIPCHECKSUM; goto done; } stats_p->brms_out_cksum_good.brcs_ip_checksum++; } } if (info.ip_is_fragmented) { /* can't verify checksum on fragmented packets */ goto done; } switch (info.ip_proto) { case IPPROTO_TCP: stats_p->brms_out_ip.bips_tcp++; break; case IPPROTO_UDP: stats_p->brms_out_ip.bips_udp++; break; default: goto done; } /* check for hardware offloaded UDP/TCP checksum */ #define HW_CSUM (CSUM_DATA_VALID | CSUM_PSEUDO_HDR) if ((m->m_pkthdr.csum_flags & HW_CSUM) == HW_CSUM) { /* checksum verified by hardware */ valid = (m->m_pkthdr.csum_rx_val == 0xffff); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "IPv%c %s checksum HW 0x%x %svalid", is_ipv4 ? '4' : '6', (info.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP", m->m_pkthdr.csum_data, valid ? "" : "in" ); if (!valid) { /* bad checksum */ csum_stats_p = &stats_p->brms_out_cksum_bad_hw; error = (info.ip_proto == IPPROTO_TCP) ? _EBADTCPCHECKSUM : _EBADTCPCHECKSUM; } else { /* good checksum */ csum_stats_p = &stats_p->brms_out_cksum_good_hw; } proto_csum_stats_increment(info.ip_proto, csum_stats_p); goto done; } m->m_data += mac_hlen; m->m_len -= mac_hlen; m->m_pkthdr.len -= mac_hlen; if (is_ipv4) { sum = inet_cksum(m, info.ip_proto, info.ip_hlen, info.ip_pay_len); } else { sum = inet6_cksum(m, info.ip_proto, info.ip_hlen + info.ip_opt_len, info.ip_pay_len - info.ip_opt_len); } valid = (sum == 0); if (valid) { csum_stats_p = &stats_p->brms_out_cksum_good; } else { csum_stats_p = &stats_p->brms_out_cksum_bad; error = (info.ip_proto == IPPROTO_TCP) ? _EBADTCPCHECKSUM : _EBADUDPCHECKSUM; } proto_csum_stats_increment(info.ip_proto, csum_stats_p); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "IPv%c %s checksum SW %svalid (0x%x) hlen %d paylen %d", is_ipv4 ? '4' : '6', (info.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP", valid ? "" : "in", sum, info.ip_hlen, info.ip_pay_len); m->m_data -= mac_hlen; m->m_len += mac_hlen; m->m_pkthdr.len += mac_hlen; done: return error; } static errno_t bridge_offload_checksum(struct mbuf * * mp, ip_packet_info * info_p, struct ifbrmstats * stats_p) { uint16_t * csum_p; errno_t error = 0; u_int hlen; struct mbuf * m0 = *mp; u_int mac_hlen = sizeof(struct ether_header); u_int pkt_hdr_len; struct tcphdr * tcp; u_int tcp_hlen; struct udphdr * udp; if (info_p->ip_is_ipv4) { /* compute IP header checksum */ info_p->ip_hdr.ip->ip_sum = 0; info_p->ip_hdr.ip->ip_sum = inet_cksum(m0, 0, mac_hlen, info_p->ip_hlen); stats_p->brms_in_computed_cksum.brcs_ip_checksum++; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "IPv4 checksum 0x%x", ntohs(info_p->ip_hdr.ip->ip_sum)); } if (info_p->ip_is_fragmented) { /* can't compute checksum on fragmented packets */ goto done; } pkt_hdr_len = m0->m_pkthdr.len; switch (info_p->ip_proto) { case IPPROTO_TCP: hlen = mac_hlen + info_p->ip_hlen + info_p->ip_opt_len + sizeof(struct tcphdr); if (m0->m_len < hlen) { *mp = m0 = m_pullup(m0, hlen); if (m0 == NULL) { stats_p->brms_in_ip.bips_bad_tcp++; error = _EBADTCP; goto done; } } tcp = (struct tcphdr *)(void *) ((caddr_t)info_p->ip_hdr.ptr + info_p->ip_hlen + info_p->ip_opt_len); tcp_hlen = tcp->th_off << 2; hlen = mac_hlen + info_p->ip_hlen + info_p->ip_opt_len + tcp_hlen; if (hlen > pkt_hdr_len) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "bad tcp header length %u", tcp_hlen); stats_p->brms_in_ip.bips_bad_tcp++; error = _EBADTCP; goto done; } csum_p = &tcp->th_sum; stats_p->brms_in_ip.bips_tcp++; break; case IPPROTO_UDP: hlen = mac_hlen + info_p->ip_hlen + info_p->ip_opt_len + sizeof(*udp); if (m0->m_len < hlen) { *mp = m0 = m_pullup(m0, hlen); if (m0 == NULL) { stats_p->brms_in_ip.bips_bad_udp++; error = ENOBUFS; goto done; } } udp = (struct udphdr *)(void *) ((caddr_t)info_p->ip_hdr.ptr + info_p->ip_hlen + info_p->ip_opt_len); csum_p = &udp->uh_sum; stats_p->brms_in_ip.bips_udp++; break; default: /* not TCP or UDP */ goto done; } *csum_p = 0; m0->m_data += mac_hlen; m0->m_len -= mac_hlen; m0->m_pkthdr.len -= mac_hlen; if (info_p->ip_is_ipv4) { *csum_p = inet_cksum(m0, info_p->ip_proto, info_p->ip_hlen, info_p->ip_pay_len); } else { *csum_p = inet6_cksum(m0, info_p->ip_proto, info_p->ip_hlen + info_p->ip_opt_len, info_p->ip_pay_len - info_p->ip_opt_len); } if (info_p->ip_proto == IPPROTO_UDP && *csum_p == 0) { /* RFC 1122 4.1.3.4 */ *csum_p = 0xffff; } m0->m_data -= mac_hlen; m0->m_len += mac_hlen; m0->m_pkthdr.len += mac_hlen; proto_csum_stats_increment(info_p->ip_proto, &stats_p->brms_in_computed_cksum); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "IPv%c %s set checksum 0x%x", info_p->ip_is_ipv4 ? '4' : '6', (info_p->ip_proto == IPPROTO_TCP) ? "TCP" : "UDP", ntohs(*csum_p)); done: return error; } static errno_t bridge_send(struct ifnet *src_ifp, struct ifnet *dst_ifp, struct mbuf *m, ChecksumOperation cksum_op) { switch (cksum_op) { case CHECKSUM_OPERATION_CLEAR_OFFLOAD: m->m_pkthdr.csum_flags &= ~CSUM_TX_FLAGS; break; case CHECKSUM_OPERATION_FINALIZE: /* the checksum might not be correct, finalize now */ bridge_finalize_cksum(dst_ifp, m); break; case CHECKSUM_OPERATION_COMPUTE: bridge_compute_cksum(src_ifp, dst_ifp, m); break; default: break; } #if HAS_IF_CAP /* * If underlying interface can not do VLAN tag insertion itself * then attach a packet tag that holds it. */ if ((m->m_flags & M_VLANTAG) && (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) { m = ether_vlanencap(m, m->m_pkthdr.ether_vtag); if (m == NULL) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_CHECKSUM, "%s: unable to prepend VLAN header", dst_ifp->if_xname); (void) ifnet_stat_increment_out(dst_ifp, 0, 0, 1); return 0; } m->m_flags &= ~M_VLANTAG; } #endif /* HAS_IF_CAP */ return bridge_transmit(dst_ifp, m); } static errno_t bridge_send_tso(struct ifnet *dst_ifp, struct mbuf *m, bool is_ipv4) { errno_t error; u_int mac_hlen; mac_hlen = sizeof(struct ether_header); #if HAS_IF_CAP /* * If underlying interface can not do VLAN tag insertion itself * then attach a packet tag that holds it. */ if ((m->m_flags & M_VLANTAG) && (dst_ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) { m = ether_vlanencap(m, m->m_pkthdr.ether_vtag); if (m == NULL) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_CHECKSUM, "%s: unable to prepend VLAN header", dst_ifp->if_xname); (void) ifnet_stat_increment_out(dst_ifp, 0, 0, 1); error = ENOBUFS; goto done; } m->m_flags &= ~M_VLANTAG; mac_hlen += ETHER_VLAN_ENCAP_LEN; } #endif /* HAS_IF_CAP */ error = gso_tcp(dst_ifp, &m, mac_hlen, is_ipv4, TRUE); return error; } /* * tso_hwassist: * - determine whether the destination interface supports TSO offload * - if the packet is already marked for offload and the hardware supports * it, just allow the packet to continue on * - if not, parse the packet headers to verify that this is a large TCP * packet requiring segmentation; if the hardware doesn't support it * set need_sw_tso; otherwise, mark the packet for TSO offload */ static int tso_hwassist(struct mbuf **mp, bool is_ipv4, struct ifnet * ifp, u_int mac_hlen, bool * need_sw_tso, bool * is_large_tcp) { int error = 0; u_int32_t if_csum; u_int32_t if_tso; u_int32_t mbuf_tso; bool supports_cksum = false; *need_sw_tso = false; *is_large_tcp = false; if (is_ipv4) { /* * Enable both TCP and IP offload if the hardware supports it. * If the hardware doesn't support TCP offload, supports_cksum * will be false so we won't set either offload. */ if_csum = ifp->if_hwassist & (CSUM_TCP | CSUM_IP); supports_cksum = (if_csum & CSUM_TCP) != 0; if_tso = IFNET_TSO_IPV4; mbuf_tso = CSUM_TSO_IPV4; } else { supports_cksum = (ifp->if_hwassist & CSUM_TCPIPV6) != 0; if_csum = CSUM_TCPIPV6; if_tso = IFNET_TSO_IPV6; mbuf_tso = CSUM_TSO_IPV6; } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "%s: does%s support checksum 0x%x if_csum 0x%x", ifp->if_xname, supports_cksum ? "" : " not", ifp->if_hwassist, if_csum); if ((ifp->if_hwassist & if_tso) != 0 && ((*mp)->m_pkthdr.csum_flags & mbuf_tso) != 0) { /* hardware TSO, mbuf already marked */ } else { /* verify that this is a large TCP frame */ uint32_t csum_flags; ip_packet_info info; int mss; struct bripstats stats; struct tcphdr * tcp; error = bridge_get_tcp_header(mp, mac_hlen, is_ipv4, &info, &stats); if (error != 0) { /* bad packet */ goto done; } if ((info.ip_hlen + info.ip_pay_len + info.ip_opt_len) <= ifp->if_mtu) { /* not actually a large packet */ goto done; } if (info.ip_proto_hdr == NULL) { /* not a TCP packet */ goto done; } if ((ifp->if_hwassist & if_tso) == 0) { /* hardware does not support TSO, enable sw tso */ *need_sw_tso = if_bridge_segmentation != 0; goto done; } /* use hardware TSO */ (*mp)->m_pkthdr.pkt_proto = IPPROTO_TCP; tcp = (struct tcphdr *)info.ip_proto_hdr; mss = ifp->if_mtu - info.ip_hlen - info.ip_opt_len - (tcp->th_off << 2) - if_bridge_tso_reduce_mss_tx; assert(mss > 0); csum_flags = mbuf_tso; if (supports_cksum) { csum_flags |= if_csum; } (*mp)->m_pkthdr.tso_segsz = mss; (*mp)->m_pkthdr.csum_flags |= csum_flags; (*mp)->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); *is_large_tcp = true; } done: return error; } /* * bridge_enqueue: * * Enqueue a packet on a bridge member interface. * */ static errno_t bridge_enqueue(ifnet_t bridge_ifp, struct ifnet *src_ifp, struct ifnet *dst_ifp, struct mbuf *m, ChecksumOperation cksum_op) { errno_t error = 0; int len; VERIFY(dst_ifp != NULL); /* * We may be sending a fragment so traverse the mbuf * * NOTE: bridge_fragment() is called only when PFIL_HOOKS is enabled. */ for (struct mbuf *next_m = NULL; m != NULL; m = next_m) { bool need_sw_tso = false; bool is_ipv4 = false; bool is_large_pkt; errno_t _error = 0; len = m->m_pkthdr.len; m->m_flags |= M_PROTO1; /* set to avoid loops */ next_m = m->m_nextpkt; m->m_nextpkt = NULL; /* * Need to segment the packet if it is a large frame * and the destination interface does not support TSO. * * Note that with trailers, it's possible for a packet to * be large but not actually require segmentation. */ is_large_pkt = (len > (bridge_ifp->if_mtu + ETHER_HDR_LEN)); if (is_large_pkt) { struct ether_header *eh; bool is_large_tcp = false; eh = mtod(m, struct ether_header *); if (ether_header_type_is_ip(eh, &is_ipv4)) { _error = tso_hwassist(&m, is_ipv4, dst_ifp, sizeof(struct ether_header), &need_sw_tso, &is_large_tcp); if (is_large_tcp) { cksum_op = CHECKSUM_OPERATION_NONE; } } else { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "large non IP packet"); } } if (_error != 0) { if (m != NULL) { m_freem(m); } } else if (need_sw_tso) { _error = bridge_send_tso(dst_ifp, m, is_ipv4); } else { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "%s bridge_send(%s) len %d op %d", bridge_ifp->if_xname, dst_ifp->if_xname, len, cksum_op); _error = bridge_send(src_ifp, dst_ifp, m, cksum_op); } /* Preserve first error value */ if (error == 0 && _error != 0) { error = _error; } if (_error == 0) { (void) ifnet_stat_increment_out(bridge_ifp, 1, len, 0); } else { (void) ifnet_stat_increment_out(bridge_ifp, 0, 0, 1); } } return error; } #if HAS_BRIDGE_DUMMYNET /* * bridge_dummynet: * * Receive a queued packet from dummynet and pass it on to the output * interface. * * The mbuf has the Ethernet header already attached. */ static void bridge_dummynet(struct mbuf *m, struct ifnet *ifp) { struct bridge_softc *sc; sc = ifp->if_bridge; /* * The packet didn't originate from a member interface. This should only * ever happen if a member interface is removed while packets are * queued for it. */ if (sc == NULL) { m_freem(m); return; } if (PFIL_HOOKED(&inet_pfil_hook) || PFIL_HOOKED_INET6) { if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0) { return; } if (m == NULL) { return; } } (void) bridge_enqueue(sc->sc_ifp, NULL, ifp, m, CHECKSUM_OPERATION_NONE); } #endif /* HAS_BRIDGE_DUMMYNET */ /* * bridge_member_output: * * Send output from a bridge member interface. This * performs the bridging function for locally originated * packets. * * The mbuf has the Ethernet header already attached. */ static errno_t bridge_member_output(struct bridge_softc *sc, ifnet_t ifp, mbuf_t *data) { ifnet_t bridge_ifp; struct ether_header *eh; struct ifnet *dst_if; uint16_t vlan; struct bridge_iflist *mac_nat_bif; ifnet_t mac_nat_ifp; mbuf_t m = *data; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_OUTPUT, "ifp %s", ifp->if_xname); if (m->m_len < ETHER_HDR_LEN) { m = m_pullup(m, ETHER_HDR_LEN); if (m == NULL) { *data = NULL; return EJUSTRETURN; } } eh = mtod(m, struct ether_header *); vlan = VLANTAGOF(m); BRIDGE_LOCK(sc); mac_nat_bif = sc->sc_mac_nat_bif; mac_nat_ifp = (mac_nat_bif != NULL) ? mac_nat_bif->bif_ifp : NULL; if (mac_nat_ifp == ifp) { /* record the IP address used by the MAC NAT interface */ (void)bridge_mac_nat_output(sc, mac_nat_bif, data, NULL); m = *data; if (m == NULL) { /* packet was deallocated */ BRIDGE_UNLOCK(sc); return EJUSTRETURN; } } bridge_ifp = sc->sc_ifp; /* * APPLE MODIFICATION * If the packet is an 802.1X ethertype, then only send on the * original output interface. */ if (eh->ether_type == htons(ETHERTYPE_PAE)) { dst_if = ifp; goto sendunicast; } /* * If bridge is down, but the original output interface is up, * go ahead and send out that interface. Otherwise, the packet * is dropped below. */ if ((bridge_ifp->if_flags & IFF_RUNNING) == 0) { dst_if = ifp; goto sendunicast; } /* * If the packet is a multicast, or we don't know a better way to * get there, send to all interfaces. */ if (ETHER_IS_MULTICAST(eh->ether_dhost)) { dst_if = NULL; } else { dst_if = bridge_rtlookup(sc, eh->ether_dhost, vlan); } if (dst_if == NULL) { struct bridge_iflist *bif; struct mbuf *mc; errno_t error; bridge_span(sc, m); BRIDGE_LOCK2REF(sc, error); if (error != 0) { m_freem(m); return EJUSTRETURN; } /* * Duplicate and send the packet across all member interfaces * except the originating interface. */ TAILQ_FOREACH(bif, &sc->sc_iflist, bif_next) { dst_if = bif->bif_ifp; if (dst_if == ifp) { /* skip the originating interface */ continue; } /* skip interface with inactive link status */ if ((bif->bif_flags & BIFF_MEDIA_ACTIVE) == 0) { continue; } #if 0 if (dst_if->if_type == IFT_GIF) { continue; } #endif /* skip interface that isn't running */ if ((dst_if->if_flags & IFF_RUNNING) == 0) { continue; } /* * If the interface is participating in spanning * tree, make sure the port is in a state that * allows forwarding. */ if ((bif->bif_ifflags & IFBIF_STP) && bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { continue; } /* * If the destination is the MAC NAT interface, * skip sending the packet. The packet can't be sent * if the source MAC is incorrect. */ if (dst_if == mac_nat_ifp) { continue; } /* make a deep copy to send on this member interface */ mc = m_dup(m, M_DONTWAIT); if (mc == NULL) { (void)ifnet_stat_increment_out(bridge_ifp, 0, 0, 1); continue; } (void)bridge_enqueue(bridge_ifp, ifp, dst_if, mc, CHECKSUM_OPERATION_COMPUTE); } BRIDGE_UNREF(sc); if ((ifp->if_flags & IFF_RUNNING) == 0) { m_freem(m); return EJUSTRETURN; } /* allow packet to continue on the originating interface */ return 0; } sendunicast: /* * XXX Spanning tree consideration here? */ bridge_span(sc, m); if ((dst_if->if_flags & IFF_RUNNING) == 0) { m_freem(m); BRIDGE_UNLOCK(sc); return EJUSTRETURN; } BRIDGE_UNLOCK(sc); if (dst_if == ifp) { /* allow packet to continue on the originating interface */ return 0; } if (dst_if != mac_nat_ifp) { (void) bridge_enqueue(bridge_ifp, ifp, dst_if, m, CHECKSUM_OPERATION_COMPUTE); } else { /* * This is not the original output interface * and the destination is the MAC NAT interface. * Drop the packet because the packet can't be sent * if the source MAC is incorrect. */ m_freem(m); } return EJUSTRETURN; } /* * Output callback. * * This routine is called externally from above only when if_bridge_txstart * is disabled; otherwise it is called internally by bridge_start(). */ static int bridge_output(struct ifnet *ifp, struct mbuf *m) { struct bridge_softc *sc = ifnet_softc(ifp); struct ether_header *eh; struct ifnet *dst_if = NULL; int error = 0; eh = mtod(m, struct ether_header *); BRIDGE_LOCK(sc); if (!(m->m_flags & (M_BCAST | M_MCAST))) { dst_if = bridge_rtlookup(sc, eh->ether_dhost, 0); } (void) ifnet_stat_increment_out(ifp, 1, m->m_pkthdr.len, 0); #if NBPFILTER > 0 if (sc->sc_bpf_output) { bridge_bpf_output(ifp, m); } #endif if (dst_if == NULL) { /* callee will unlock */ bridge_broadcast(sc, NULL, m, 0); } else { ifnet_t bridge_ifp; bridge_ifp = sc->sc_ifp; BRIDGE_UNLOCK(sc); error = bridge_enqueue(bridge_ifp, NULL, dst_if, m, CHECKSUM_OPERATION_FINALIZE); } return error; } static void bridge_finalize_cksum(struct ifnet *ifp, struct mbuf *m) { struct ether_header *eh; bool is_ipv4; uint32_t sw_csum, hwcap; uint32_t did_sw; uint32_t csum_flags; eh = mtod(m, struct ether_header *); if (!ether_header_type_is_ip(eh, &is_ipv4)) { return; } /* do in software what the hardware cannot */ hwcap = (ifp->if_hwassist | CSUM_DATA_VALID); csum_flags = m->m_pkthdr.csum_flags; sw_csum = csum_flags & ~IF_HWASSIST_CSUM_FLAGS(hwcap); sw_csum &= IF_HWASSIST_CSUM_MASK; if (is_ipv4) { if ((hwcap & CSUM_PARTIAL) && !(sw_csum & CSUM_DELAY_DATA) && (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)) { if (m->m_pkthdr.csum_flags & CSUM_TCP) { uint16_t start = sizeof(*eh) + sizeof(struct ip); uint16_t ulpoff = m->m_pkthdr.csum_data & 0xffff; m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID | CSUM_PARTIAL); m->m_pkthdr.csum_tx_stuff = (ulpoff + start); m->m_pkthdr.csum_tx_start = start; } else { sw_csum |= (CSUM_DELAY_DATA & m->m_pkthdr.csum_flags); } } did_sw = in_finalize_cksum(m, sizeof(*eh), sw_csum); } else { if ((hwcap & CSUM_PARTIAL) && !(sw_csum & CSUM_DELAY_IPV6_DATA) && (m->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA)) { if (m->m_pkthdr.csum_flags & CSUM_TCPIPV6) { uint16_t start = sizeof(*eh) + sizeof(struct ip6_hdr); uint16_t ulpoff = m->m_pkthdr.csum_data & 0xffff; m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID | CSUM_PARTIAL); m->m_pkthdr.csum_tx_stuff = (ulpoff + start); m->m_pkthdr.csum_tx_start = start; } else { sw_csum |= (CSUM_DELAY_IPV6_DATA & m->m_pkthdr.csum_flags); } } did_sw = in6_finalize_cksum(m, sizeof(*eh), -1, -1, sw_csum); } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "[%s] before 0x%x hwcap 0x%x sw_csum 0x%x did 0x%x after 0x%x", ifp->if_xname, csum_flags, hwcap, sw_csum, did_sw, m->m_pkthdr.csum_flags); } /* * bridge_start: * * Start output on a bridge. * * This routine is invoked by the start worker thread; because we never call * it directly, there is no need do deploy any serialization mechanism other * than what's already used by the worker thread, i.e. this is already single * threaded. * * This routine is called only when if_bridge_txstart is enabled. */ static void bridge_start(struct ifnet *ifp) { struct mbuf *m; for (;;) { if (ifnet_dequeue(ifp, &m) != 0) { break; } (void) bridge_output(ifp, m); } } /* * bridge_forward: * * The forwarding function of the bridge. * * NOTE: Releases the lock on return. */ static void bridge_forward(struct bridge_softc *sc, struct bridge_iflist *sbif, struct mbuf *m) { struct bridge_iflist *dbif; ifnet_t bridge_ifp; struct ifnet *src_if, *dst_if; struct ether_header *eh; uint16_t vlan; uint8_t *dst; int error; struct mac_nat_record mnr; bool translate_mac = FALSE; uint32_t sc_filter_flags = 0; BRIDGE_LOCK_ASSERT_HELD(sc); bridge_ifp = sc->sc_ifp; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_OUTPUT, "%s m 0x%llx", bridge_ifp->if_xname, (uint64_t)VM_KERNEL_ADDRPERM(m)); src_if = m->m_pkthdr.rcvif; if (src_if != sbif->bif_ifp) { const char * src_if_name; src_if_name = (src_if != NULL) ? src_if->if_xname : "?"; BRIDGE_LOG(LOG_NOTICE, 0, "src_if %s != bif_ifp %s", src_if_name, sbif->bif_ifp->if_xname); goto drop; } (void) ifnet_stat_increment_in(bridge_ifp, 1, m->m_pkthdr.len, 0); vlan = VLANTAGOF(m); if ((sbif->bif_ifflags & IFBIF_STP) && sbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { goto drop; } eh = mtod(m, struct ether_header *); dst = eh->ether_dhost; /* If the interface is learning, record the address. */ if (sbif->bif_ifflags & IFBIF_LEARNING) { error = bridge_rtupdate(sc, eh->ether_shost, vlan, sbif, 0, IFBAF_DYNAMIC); /* * If the interface has addresses limits then deny any source * that is not in the cache. */ if (error && sbif->bif_addrmax) { goto drop; } } if ((sbif->bif_ifflags & IFBIF_STP) != 0 && sbif->bif_stp.bp_state == BSTP_IFSTATE_LEARNING) { goto drop; } /* * At this point, the port either doesn't participate * in spanning tree or it is in the forwarding state. */ /* * If the packet is unicast, destined for someone on * "this" side of the bridge, drop it. */ if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) { /* unicast */ dst_if = bridge_rtlookup(sc, dst, vlan); if (src_if == dst_if) { goto drop; } } else { /* broadcast/multicast */ /* * Check if its a reserved multicast address, any address * listed in 802.1D section 7.12.6 may not be forwarded by the * bridge. * This is currently 01-80-C2-00-00-00 to 01-80-C2-00-00-0F */ if (dst[0] == 0x01 && dst[1] == 0x80 && dst[2] == 0xc2 && dst[3] == 0x00 && dst[4] == 0x00 && dst[5] <= 0x0f) { goto drop; } /* ...forward it to all interfaces. */ atomic_add_64(&bridge_ifp->if_imcasts, 1); dst_if = NULL; } /* * If we have a destination interface which is a member of our bridge, * OR this is a unicast packet, push it through the bpf(4) machinery. * For broadcast or multicast packets, don't bother because it will * be reinjected into ether_input. We do this before we pass the packets * through the pfil(9) framework, as it is possible that pfil(9) will * drop the packet, or possibly modify it, making it difficult to debug * firewall issues on the bridge. */ #if NBPFILTER > 0 if (eh->ether_type == htons(ETHERTYPE_RSN_PREAUTH) || dst_if != NULL || (m->m_flags & (M_BCAST | M_MCAST)) == 0) { m->m_pkthdr.rcvif = bridge_ifp; BRIDGE_BPF_MTAP_INPUT(sc, m); } #endif /* NBPFILTER */ if (dst_if == NULL) { /* bridge_broadcast will unlock */ bridge_broadcast(sc, sbif, m, 1); return; } /* * Unicast. */ /* * At this point, we're dealing with a unicast frame * going to a different interface. */ if ((dst_if->if_flags & IFF_RUNNING) == 0) { goto drop; } dbif = bridge_lookup_member_if(sc, dst_if); if (dbif == NULL) { /* Not a member of the bridge (anymore?) */ goto drop; } /* Private segments can not talk to each other */ if (sbif->bif_ifflags & dbif->bif_ifflags & IFBIF_PRIVATE) { goto drop; } if ((dbif->bif_ifflags & IFBIF_STP) && dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { goto drop; } #if HAS_DHCPRA_MASK /* APPLE MODIFICATION <rdar:6985737> */ if ((dst_if->if_extflags & IFEXTF_DHCPRA_MASK) != 0) { m = ip_xdhcpra_output(dst_if, m); if (!m) { ++bridge_ifp.if_xdhcpra; BRIDGE_UNLOCK(sc); return; } } #endif /* HAS_DHCPRA_MASK */ if (dbif == sc->sc_mac_nat_bif) { /* determine how to translate the packet */ translate_mac = bridge_mac_nat_output(sc, sbif, &m, &mnr); if (m == NULL) { /* packet was deallocated */ BRIDGE_UNLOCK(sc); return; } } else if (bif_has_checksum_offload(dbif) && !bif_has_checksum_offload(sbif)) { /* * If the destination interface has checksum offload enabled, * verify the checksum now, unless the source interface also has * checksum offload enabled. The checksum in that case has * already just been computed and verifying it is unnecessary. */ error = bridge_verify_checksum(&m, &dbif->bif_stats); if (error != 0) { BRIDGE_UNLOCK(sc); if (m != NULL) { m_freem(m); } return; } } sc_filter_flags = sc->sc_filter_flags; BRIDGE_UNLOCK(sc); if (PF_IS_ENABLED && (sc_filter_flags & IFBF_FILT_MEMBER)) { if (bridge_pf(&m, dst_if, sc_filter_flags, FALSE) != 0) { return; } if (m == NULL) { return; } } /* if we need to, translate the MAC address */ if (translate_mac) { bridge_mac_nat_translate(&m, &mnr, IF_LLADDR(dst_if)); } /* * We're forwarding an inbound packet in which the checksum must * already have been computed and if required, verified. */ if (m != NULL) { (void) bridge_enqueue(bridge_ifp, src_if, dst_if, m, CHECKSUM_OPERATION_CLEAR_OFFLOAD); } return; drop: BRIDGE_UNLOCK(sc); m_freem(m); } static void inject_input_packet(ifnet_t ifp, mbuf_t m) { mbuf_pkthdr_setrcvif(m, ifp); mbuf_pkthdr_setheader(m, mbuf_data(m)); mbuf_setdata(m, (char *)mbuf_data(m) + ETHER_HDR_LEN, mbuf_len(m) - ETHER_HDR_LEN); mbuf_pkthdr_adjustlen(m, -ETHER_HDR_LEN); m->m_flags |= M_PROTO1; /* set to avoid loops */ dlil_input_packet_list(ifp, m); return; } static bool in_addr_is_ours(struct in_addr ip) { struct in_ifaddr *ia; bool ours = false; lck_rw_lock_shared(&in_ifaddr_rwlock); TAILQ_FOREACH(ia, INADDR_HASH(ip.s_addr), ia_hash) { if (IA_SIN(ia)->sin_addr.s_addr == ip.s_addr) { ours = true; break; } } lck_rw_done(&in_ifaddr_rwlock); return ours; } static bool in6_addr_is_ours(const struct in6_addr * ip6_p, uint32_t ifscope) { struct in6_ifaddr *ia6; bool ours = false; if (in6_embedded_scope && IN6_IS_ADDR_LINKLOCAL(ip6_p)) { struct in6_addr dst_ip; /* need to embed scope ID for comparison */ bcopy(ip6_p, &dst_ip, sizeof(dst_ip)); dst_ip.s6_addr16[1] = htons(ifscope); ip6_p = &dst_ip; } lck_rw_lock_shared(&in6_ifaddr_rwlock); TAILQ_FOREACH(ia6, IN6ADDR_HASH(ip6_p), ia6_hash) { if (in6_are_addr_equal_scoped(&ia6->ia_addr.sin6_addr, ip6_p, ia6->ia_addr.sin6_scope_id, ifscope)) { ours = true; break; } } lck_rw_done(&in6_ifaddr_rwlock); return ours; } static void bridge_interface_input(ifnet_t bridge_ifp, mbuf_t m, bpf_packet_func bpf_input_func) { size_t byte_count; struct ether_header *eh; errno_t error; bool is_ipv4; int len; u_int mac_hlen; int pkt_count; /* segment large packets before sending them up */ if (if_bridge_segmentation == 0) { goto done; } len = m->m_pkthdr.len; if (len <= (bridge_ifp->if_mtu + ETHER_HDR_LEN)) { goto done; } eh = mtod(m, struct ether_header *); if (!ether_header_type_is_ip(eh, &is_ipv4)) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "large non IPv4/IPv6 packet"); goto done; } /* * We have a large IPv4/IPv6 TCP packet. Segment it if required. * * If gso_tcp() returns success (0), the packet(s) are * ready to be passed up. If the destination is a local IP address, * the packet will be passed up as a large, single packet. * * If gso_tcp() returns an error, the packet has already * been freed. */ mac_hlen = sizeof(*eh); error = gso_tcp(bridge_ifp, &m, mac_hlen, is_ipv4, FALSE); if (error != 0) { return; } done: pkt_count = 0; byte_count = 0; for (mbuf_t scan = m; scan != NULL; scan = scan->m_nextpkt) { /* Mark the packet as arriving on the bridge interface */ mbuf_pkthdr_setrcvif(scan, bridge_ifp); mbuf_pkthdr_setheader(scan, mbuf_data(scan)); if (bpf_input_func != NULL) { (*bpf_input_func)(bridge_ifp, scan); } mbuf_setdata(scan, (char *)mbuf_data(scan) + ETHER_HDR_LEN, mbuf_len(scan) - ETHER_HDR_LEN); mbuf_pkthdr_adjustlen(scan, -ETHER_HDR_LEN); byte_count += mbuf_pkthdr_len(scan); pkt_count++; } (void)ifnet_stat_increment_in(bridge_ifp, pkt_count, byte_count, 0); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT, "%s %d packet(s) %ld bytes", bridge_ifp->if_xname, pkt_count, byte_count); dlil_input_packet_list(bridge_ifp, m); return; } static bool is_our_ip(ip_packet_info_t info_p, uint32_t ifscope) { bool ours; if (info_p->ip_is_ipv4) { struct in_addr dst_ip; bcopy(&info_p->ip_hdr.ip->ip_dst, &dst_ip, sizeof(dst_ip)); ours = in_addr_is_ours(dst_ip); } else { ours = in6_addr_is_ours(&info_p->ip_hdr.ip6->ip6_dst, ifscope); } return ours; } static inline errno_t bridge_vmnet_tag_input(ifnet_t bridge_ifp, ifnet_t ifp, const u_char * ether_dhost, mbuf_t *mp, bool is_broadcast, bool is_ip, bool is_ipv4, ip_packet_info * info_p, struct bripstats * stats_p, bool *info_initialized) { errno_t error = 0; bool is_local = false; struct pf_mtag *pf_mtag; u_int16_t tag = vmnet_tag; *info_initialized = false; if (is_broadcast) { if (_ether_cmp(ether_dhost, etherbroadcastaddr) == 0) { tag = vmnet_broadcast_tag; } else { tag = vmnet_multicast_tag; } } else if (is_ip) { unsigned int mac_hlen = sizeof(struct ether_header); bzero(stats_p, sizeof(*stats_p)); *info_initialized = true; error = bridge_get_ip_proto(mp, mac_hlen, is_ipv4, info_p, stats_p); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_INPUT, "%s(%s) bridge_get_ip_proto failed %d", bridge_ifp->if_xname, ifp->if_xname, error); if (*mp == NULL) { return EJUSTRETURN; } } else { is_local = is_our_ip(info_p, bridge_ifp->if_index); if (is_local) { tag = vmnet_local_tag; } } } pf_mtag = pf_get_mtag(*mp); if (pf_mtag != NULL) { pf_mtag->pftag_tag = tag; } #if DEBUG || DEVELOPMENT { bool forced; BRIDGE_ERROR_GET_FORCED(forced, BRIDGE_FORCE_ONE); if (forced) { m_freem(*mp); *mp = NULL; error = EJUSTRETURN; goto done; } BRIDGE_ERROR_GET_FORCED(forced, BRIDGE_FORCE_TWO); if (forced) { error = _EBADIP; goto done; } } done: #endif /* DEBUG || DEVELOPMENT */ return error; } static void bripstats_apply(struct bripstats *dst_p, const struct bripstats *src_p) { dst_p->bips_ip += src_p->bips_ip; dst_p->bips_ip6 += src_p->bips_ip6; dst_p->bips_udp += src_p->bips_udp; dst_p->bips_tcp += src_p->bips_tcp; dst_p->bips_bad_ip += src_p->bips_bad_ip; dst_p->bips_bad_ip6 += src_p->bips_bad_ip6; dst_p->bips_bad_udp += src_p->bips_bad_udp; dst_p->bips_bad_tcp += src_p->bips_bad_tcp; } static void bridge_bripstats_apply(ifnet_t ifp, const struct bripstats *stats_p) { struct bridge_iflist *bif; struct bridge_softc *sc = ifp->if_bridge; BRIDGE_LOCK(sc); bif = bridge_lookup_member_if(sc, ifp); if (bif == NULL) { goto done; } if (!bif_has_checksum_offload(bif)) { goto done; } bripstats_apply(&bif->bif_stats.brms_in_ip, stats_p); done: BRIDGE_UNLOCK(sc); return; } /* * bridge_input: * * Filter input from a member interface. Queue the packet for * bridging if it is not for us. */ errno_t bridge_input(struct ifnet *ifp, mbuf_t *data) { struct bridge_softc *sc = ifp->if_bridge; struct bridge_iflist *bif, *bif2; struct ether_header eh_in; bool is_ip = false; bool is_ipv4 = false; ifnet_t bridge_ifp; struct mbuf *mc, *mc2; unsigned int mac_hlen = sizeof(struct ether_header); uint16_t vlan; errno_t error; ip_packet_info info; struct bripstats stats; bool info_initialized = false; errno_t ip_packet_error = 0; bool is_broadcast; bool is_ip_broadcast = false; bool is_ifp_mac = false; mbuf_t m = *data; uint32_t sc_filter_flags = 0; bridge_ifp = sc->sc_ifp; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT, "%s from %s m 0x%llx data 0x%llx", bridge_ifp->if_xname, ifp->if_xname, (uint64_t)VM_KERNEL_ADDRPERM(m), (uint64_t)VM_KERNEL_ADDRPERM(mbuf_data(m))); if ((sc->sc_ifp->if_flags & IFF_RUNNING) == 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT, "%s not running passing along", bridge_ifp->if_xname); return 0; } vlan = VLANTAGOF(m); #ifdef IFF_MONITOR /* * Implement support for bridge monitoring. If this flag has been * set on this interface, discard the packet once we push it through * the bpf(4) machinery, but before we do, increment the byte and * packet counters associated with this interface. */ if ((bridge_ifp->if_flags & IFF_MONITOR) != 0) { m->m_pkthdr.rcvif = bridge_ifp; BRIDGE_BPF_MTAP_INPUT(sc, m); (void) ifnet_stat_increment_in(bridge_ifp, 1, m->m_pkthdr.len, 0); *data = NULL; m_freem(m); return EJUSTRETURN; } #endif /* IFF_MONITOR */ is_broadcast = (m->m_flags & (M_BCAST | M_MCAST)) != 0; /* * Need to clear the promiscuous flag otherwise it will be * dropped by DLIL after processing filters */ if ((mbuf_flags(m) & MBUF_PROMISC)) { mbuf_setflags_mask(m, 0, MBUF_PROMISC); } /* copy the ethernet header */ eh_in = *(mtod(m, struct ether_header *)); is_ip = ether_header_type_is_ip(&eh_in, &is_ipv4); if (if_bridge_vmnet_pf_tagging != 0 && IFNET_IS_VMNET(ifp)) { /* tag packets coming from VMNET interfaces */ ip_packet_error = bridge_vmnet_tag_input(bridge_ifp, ifp, eh_in.ether_dhost, data, is_broadcast, is_ip, is_ipv4, &info, &stats, &info_initialized); m = *data; if (m == NULL) { bridge_bripstats_apply(ifp, &stats); return EJUSTRETURN; } } sc_filter_flags = sc->sc_filter_flags; if (PF_IS_ENABLED && (sc_filter_flags & IFBF_FILT_MEMBER)) { error = bridge_pf(data, ifp, sc_filter_flags, TRUE); m = *data; if (error != 0 || m == NULL) { return EJUSTRETURN; } } BRIDGE_LOCK(sc); bif = bridge_lookup_member_if(sc, ifp); if (bif == NULL) { BRIDGE_UNLOCK(sc); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT, "%s bridge_lookup_member_if failed", bridge_ifp->if_xname); return 0; } if (is_ip && bif_has_checksum_offload(bif)) { if (info_initialized) { bripstats_apply(&bif->bif_stats.brms_in_ip, &stats); } else { error = bridge_get_ip_proto(data, mac_hlen, is_ipv4, &info, &bif->bif_stats.brms_in_ip); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_CHECKSUM, "%s(%s) bridge_get_ip_proto failed %d", bridge_ifp->if_xname, bif->bif_ifp->if_xname, error); ip_packet_error = error; } } if (ip_packet_error == 0) { /* need to compute IP/UDP/TCP/checksums */ error = bridge_offload_checksum(data, &info, &bif->bif_stats); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_CHECKSUM, "%s(%s) bridge_offload_checksum failed %d", bridge_ifp->if_xname, bif->bif_ifp->if_xname, error); ip_packet_error = error; } } if (ip_packet_error != 0) { BRIDGE_UNLOCK(sc); if (*data != NULL) { m_freem(*data); *data = NULL; } return EJUSTRETURN; } m = *data; } if (bif->bif_flags & BIFF_HOST_FILTER) { error = bridge_host_filter(bif, data); if (error != 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_INPUT, "%s bridge_host_filter failed", bif->bif_ifp->if_xname); BRIDGE_UNLOCK(sc); return EJUSTRETURN; } m = *data; } if (!is_broadcast && _ether_cmp(eh_in.ether_dhost, IF_LLADDR(ifp)) == 0) { /* the packet is unicast to the interface's MAC address */ if (is_ip && sc->sc_mac_nat_bif == bif) { /* doing MAC-NAT, check if destination is IP broadcast */ is_ip_broadcast = is_broadcast_ip_packet(data); if (*data == NULL) { BRIDGE_UNLOCK(sc); return EJUSTRETURN; } m = *data; } if (!is_ip_broadcast) { is_ifp_mac = TRUE; } } bridge_span(sc, m); if (is_broadcast || is_ip_broadcast) { if (is_broadcast && (m->m_flags & M_MCAST) != 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MCAST, " multicast: " "%02x:%02x:%02x:%02x:%02x:%02x", eh_in.ether_dhost[0], eh_in.ether_dhost[1], eh_in.ether_dhost[2], eh_in.ether_dhost[3], eh_in.ether_dhost[4], eh_in.ether_dhost[5]); } /* Tap off 802.1D packets; they do not get forwarded. */ if (is_broadcast && _ether_cmp(eh_in.ether_dhost, bstp_etheraddr) == 0) { #if BRIDGESTP m = bstp_input(&bif->bif_stp, ifp, m); #else /* !BRIDGESTP */ m_freem(m); m = NULL; #endif /* !BRIDGESTP */ if (m == NULL) { BRIDGE_UNLOCK(sc); return EJUSTRETURN; } } if ((bif->bif_ifflags & IFBIF_STP) && bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { BRIDGE_UNLOCK(sc); return 0; } /* * Make a deep copy of the packet and enqueue the copy * for bridge processing. */ mc = m_dup(m, M_DONTWAIT); if (mc == NULL) { BRIDGE_UNLOCK(sc); return 0; } /* * Perform the bridge forwarding function with the copy. * * Note that bridge_forward calls BRIDGE_UNLOCK */ if (is_ip_broadcast) { struct ether_header *eh; /* make the copy look like it is actually broadcast */ mc->m_flags |= M_BCAST; eh = mtod(mc, struct ether_header *); bcopy(etherbroadcastaddr, eh->ether_dhost, ETHER_ADDR_LEN); } bridge_forward(sc, bif, mc); /* * Reinject the mbuf as arriving on the bridge so we have a * chance at claiming multicast packets. We can not loop back * here from ether_input as a bridge is never a member of a * bridge. */ VERIFY(bridge_ifp->if_bridge == NULL); mc2 = m_dup(m, M_DONTWAIT); if (mc2 != NULL) { /* Keep the layer3 header aligned */ int i = min(mc2->m_pkthdr.len, max_protohdr); mc2 = m_copyup(mc2, i, ETHER_ALIGN); } if (mc2 != NULL) { /* mark packet as arriving on the bridge */ mc2->m_pkthdr.rcvif = bridge_ifp; mc2->m_pkthdr.pkt_hdr = mbuf_data(mc2); BRIDGE_BPF_MTAP_INPUT(sc, mc2); (void) mbuf_setdata(mc2, (char *)mbuf_data(mc2) + ETHER_HDR_LEN, mbuf_len(mc2) - ETHER_HDR_LEN); (void) mbuf_pkthdr_adjustlen(mc2, -ETHER_HDR_LEN); (void) ifnet_stat_increment_in(bridge_ifp, 1, mbuf_pkthdr_len(mc2), 0); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MCAST, "%s mcast for us", bridge_ifp->if_xname); dlil_input_packet_list(bridge_ifp, mc2); } /* Return the original packet for local processing. */ return 0; } if ((bif->bif_ifflags & IFBIF_STP) && bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { BRIDGE_UNLOCK(sc); return 0; } #ifdef DEV_CARP #define CARP_CHECK_WE_ARE_DST(iface) \ ((iface)->if_carp &&\ carp_forus((iface)->if_carp, eh_in.ether_dhost)) #define CARP_CHECK_WE_ARE_SRC(iface) \ ((iface)->if_carp &&\ carp_forus((iface)->if_carp, eh_in.ether_shost)) #else #define CARP_CHECK_WE_ARE_DST(iface) 0 #define CARP_CHECK_WE_ARE_SRC(iface) 0 #endif #define PFIL_HOOKED_INET6 PFIL_HOOKED(&inet6_pfil_hook) #define PFIL_PHYS(sc, ifp, m) #define GRAB_OUR_PACKETS(iface) \ if ((iface)->if_type == IFT_GIF) \ continue; \ /* It is destined for us. */ \ if (_ether_cmp(IF_LLADDR((iface)), eh_in.ether_dhost) == 0 || \ CARP_CHECK_WE_ARE_DST((iface))) { \ if ((iface)->if_type == IFT_BRIDGE) { \ BRIDGE_BPF_MTAP_INPUT(sc, m); \ /* Filter on the physical interface. */ \ PFIL_PHYS(sc, iface, m); \ } else { \ bpf_tap_in(iface, DLT_EN10MB, m, NULL, 0); \ } \ if (bif->bif_ifflags & IFBIF_LEARNING) { \ error = bridge_rtupdate(sc, eh_in.ether_shost, \ vlan, bif, 0, IFBAF_DYNAMIC); \ if (error && bif->bif_addrmax) { \ BRIDGE_UNLOCK(sc); \ m_freem(m); \ return (EJUSTRETURN); \ } \ } \ BRIDGE_UNLOCK(sc); \ inject_input_packet(iface, m); \ return (EJUSTRETURN); \ } \ \ /* We just received a packet that we sent out. */ \ if (_ether_cmp(IF_LLADDR((iface)), eh_in.ether_shost) == 0 || \ CARP_CHECK_WE_ARE_SRC((iface))) { \ BRIDGE_UNLOCK(sc); \ m_freem(m); \ return (EJUSTRETURN); \ } /* * Unicast. */ /* handle MAC-NAT if enabled */ if (is_ifp_mac && sc->sc_mac_nat_bif == bif) { ifnet_t dst_if; boolean_t is_input = FALSE; dst_if = bridge_mac_nat_input(sc, data, &is_input); m = *data; if (dst_if == ifp) { /* our input packet */ } else if (dst_if != NULL || m == NULL) { BRIDGE_UNLOCK(sc); if (dst_if != NULL) { ASSERT(m != NULL); if (is_input) { inject_input_packet(dst_if, m); } else { (void)bridge_enqueue(bridge_ifp, NULL, dst_if, m, CHECKSUM_OPERATION_CLEAR_OFFLOAD); } } return EJUSTRETURN; } } /* * If the packet is for the bridge, pass it up for local processing. */ if (_ether_cmp(eh_in.ether_dhost, IF_LLADDR(bridge_ifp)) == 0 || CARP_CHECK_WE_ARE_DST(bridge_ifp)) { bpf_packet_func bpf_input_func = sc->sc_bpf_input; /* * If the interface is learning, and the source * address is valid and not multicast, record * the address. */ if (bif->bif_ifflags & IFBIF_LEARNING) { (void) bridge_rtupdate(sc, eh_in.ether_shost, vlan, bif, 0, IFBAF_DYNAMIC); } BRIDGE_UNLOCK(sc); bridge_interface_input(bridge_ifp, m, bpf_input_func); return EJUSTRETURN; } /* * if the destination of the packet is for the MAC address of * the member interface itself, then we don't need to forward * it -- just pass it back. Note that it'll likely just be * dropped by the stack, but if something else is bound to * the interface directly (for example, the wireless stats * protocol -- although that actually uses BPF right now), * then it will consume the packet * * ALSO, note that we do this check AFTER checking for the * bridge's own MAC address, because the bridge may be * using the SAME MAC address as one of its interfaces */ if (is_ifp_mac) { #ifdef VERY_VERY_VERY_DIAGNOSTIC BRIDGE_LOG(LOG_NOTICE, 0, "not forwarding packet bound for member interface"); #endif BRIDGE_UNLOCK(sc); return 0; } /* Now check the remaining bridge members. */ TAILQ_FOREACH(bif2, &sc->sc_iflist, bif_next) { if (bif2->bif_ifp != ifp) { GRAB_OUR_PACKETS(bif2->bif_ifp); } } #undef CARP_CHECK_WE_ARE_DST #undef CARP_CHECK_WE_ARE_SRC #undef GRAB_OUR_PACKETS /* * Perform the bridge forwarding function. * * Note that bridge_forward calls BRIDGE_UNLOCK */ bridge_forward(sc, bif, m); return EJUSTRETURN; } /* * bridge_broadcast: * * Send a frame to all interfaces that are members of * the bridge, except for the one on which the packet * arrived. * * NOTE: Releases the lock on return. */ static void bridge_broadcast(struct bridge_softc *sc, struct bridge_iflist * sbif, struct mbuf *m, int runfilt) { ifnet_t bridge_ifp; struct bridge_iflist *dbif; struct ifnet * src_if; struct mbuf *mc; struct mbuf *mc_in; struct ifnet *dst_if; int error = 0, used = 0; boolean_t bridge_if_out; ChecksumOperation cksum_op; struct mac_nat_record mnr; struct bridge_iflist *mac_nat_bif = sc->sc_mac_nat_bif; boolean_t translate_mac = FALSE; uint32_t sc_filter_flags = 0; bridge_ifp = sc->sc_ifp; if (sbif != NULL) { bridge_if_out = FALSE; src_if = sbif->bif_ifp; cksum_op = CHECKSUM_OPERATION_CLEAR_OFFLOAD; if (mac_nat_bif != NULL && sbif != mac_nat_bif) { /* get the translation record while holding the lock */ translate_mac = bridge_mac_nat_output(sc, sbif, &m, &mnr); if (m == NULL) { /* packet was deallocated */ BRIDGE_UNLOCK(sc); return; } } } else { /* * sbif is NULL when the bridge interface calls * bridge_broadcast(). */ bridge_if_out = TRUE; cksum_op = CHECKSUM_OPERATION_FINALIZE; sbif = NULL; src_if = NULL; } BRIDGE_LOCK2REF(sc, error); if (error) { m_freem(m); return; } TAILQ_FOREACH(dbif, &sc->sc_iflist, bif_next) { dst_if = dbif->bif_ifp; if (dst_if == src_if) { /* skip the interface that the packet came in on */ continue; } /* Private segments can not talk to each other */ if (sbif != NULL && (sbif->bif_ifflags & dbif->bif_ifflags & IFBIF_PRIVATE)) { continue; } if ((dbif->bif_ifflags & IFBIF_STP) && dbif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { continue; } if ((dbif->bif_ifflags & IFBIF_DISCOVER) == 0 && (m->m_flags & (M_BCAST | M_MCAST)) == 0) { continue; } if ((dst_if->if_flags & IFF_RUNNING) == 0) { continue; } if (!(dbif->bif_flags & BIFF_MEDIA_ACTIVE)) { continue; } if (TAILQ_NEXT(dbif, bif_next) == NULL) { mc = m; used = 1; } else { mc = m_dup(m, M_DONTWAIT); if (mc == NULL) { (void) ifnet_stat_increment_out(bridge_ifp, 0, 0, 1); continue; } } /* * If broadcast input is enabled, do so only if this * is an input packet. */ if (!bridge_if_out && (dbif->bif_flags & BIFF_INPUT_BROADCAST) != 0) { mc_in = m_dup(mc, M_DONTWAIT); /* this could fail, but we continue anyways */ } else { mc_in = NULL; } /* out */ if (translate_mac && mac_nat_bif == dbif) { /* translate the packet without holding the lock */ bridge_mac_nat_translate(&mc, &mnr, IF_LLADDR(dst_if)); } sc_filter_flags = sc->sc_filter_flags; if (runfilt && PF_IS_ENABLED && (sc_filter_flags & IFBF_FILT_MEMBER)) { if (used == 0) { /* Keep the layer3 header aligned */ int i = min(mc->m_pkthdr.len, max_protohdr); mc = m_copyup(mc, i, ETHER_ALIGN); if (mc == NULL) { (void) ifnet_stat_increment_out( sc->sc_ifp, 0, 0, 1); if (mc_in != NULL) { m_freem(mc_in); mc_in = NULL; } continue; } } if (bridge_pf(&mc, dst_if, sc_filter_flags, FALSE) != 0) { if (mc_in != NULL) { m_freem(mc_in); mc_in = NULL; } continue; } if (mc == NULL) { if (mc_in != NULL) { m_freem(mc_in); mc_in = NULL; } continue; } } if (mc != NULL) { /* verify checksum if necessary */ if (bif_has_checksum_offload(dbif) && sbif != NULL && !bif_has_checksum_offload(sbif)) { error = bridge_verify_checksum(&mc, &dbif->bif_stats); if (error != 0) { if (mc != NULL) { m_freem(mc); } mc = NULL; } } if (mc != NULL) { (void) bridge_enqueue(bridge_ifp, NULL, dst_if, mc, cksum_op); } } /* in */ if (mc_in == NULL) { continue; } bpf_tap_in(dst_if, DLT_EN10MB, mc_in, NULL, 0); mbuf_pkthdr_setrcvif(mc_in, dst_if); mbuf_pkthdr_setheader(mc_in, mbuf_data(mc_in)); mbuf_setdata(mc_in, (char *)mbuf_data(mc_in) + ETHER_HDR_LEN, mbuf_len(mc_in) - ETHER_HDR_LEN); mbuf_pkthdr_adjustlen(mc_in, -ETHER_HDR_LEN); mc_in->m_flags |= M_PROTO1; /* set to avoid loops */ dlil_input_packet_list(dst_if, mc_in); } if (used == 0) { m_freem(m); } BRIDGE_UNREF(sc); } /* * bridge_span: * * Duplicate a packet out one or more interfaces that are in span mode, * the original mbuf is unmodified. */ static void bridge_span(struct bridge_softc *sc, struct mbuf *m) { struct bridge_iflist *bif; struct ifnet *dst_if; struct mbuf *mc; if (TAILQ_EMPTY(&sc->sc_spanlist)) { return; } TAILQ_FOREACH(bif, &sc->sc_spanlist, bif_next) { dst_if = bif->bif_ifp; if ((dst_if->if_flags & IFF_RUNNING) == 0) { continue; } mc = m_copypacket(m, M_DONTWAIT); if (mc == NULL) { (void) ifnet_stat_increment_out(sc->sc_ifp, 0, 0, 1); continue; } (void) bridge_enqueue(sc->sc_ifp, NULL, dst_if, mc, CHECKSUM_OPERATION_NONE); } } /* * bridge_rtupdate: * * Add a bridge routing entry. */ static int bridge_rtupdate(struct bridge_softc *sc, const uint8_t *dst, uint16_t vlan, struct bridge_iflist *bif, int setflags, uint8_t flags) { struct bridge_rtnode *brt; int error; BRIDGE_LOCK_ASSERT_HELD(sc); /* Check the source address is valid and not multicast. */ if (ETHER_IS_MULTICAST(dst) || (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 && dst[3] == 0 && dst[4] == 0 && dst[5] == 0) != 0) { return EINVAL; } /* 802.1p frames map to vlan 1 */ if (vlan == 0) { vlan = 1; } /* * A route for this destination might already exist. If so, * update it, otherwise create a new one. */ if ((brt = bridge_rtnode_lookup(sc, dst, vlan)) == NULL) { if (sc->sc_brtcnt >= sc->sc_brtmax) { sc->sc_brtexceeded++; return ENOSPC; } /* Check per interface address limits (if enabled) */ if (bif->bif_addrmax && bif->bif_addrcnt >= bif->bif_addrmax) { bif->bif_addrexceeded++; return ENOSPC; } /* * Allocate a new bridge forwarding node, and * initialize the expiration time and Ethernet * address. */ brt = zalloc_noblock(bridge_rtnode_pool); if (brt == NULL) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_RT_TABLE, "zalloc_nolock failed"); return ENOMEM; } bzero(brt, sizeof(struct bridge_rtnode)); if (bif->bif_ifflags & IFBIF_STICKY) { brt->brt_flags = IFBAF_STICKY; } else { brt->brt_flags = IFBAF_DYNAMIC; } memcpy(brt->brt_addr, dst, ETHER_ADDR_LEN); brt->brt_vlan = vlan; if ((error = bridge_rtnode_insert(sc, brt)) != 0) { zfree(bridge_rtnode_pool, brt); return error; } brt->brt_dst = bif; bif->bif_addrcnt++; BRIDGE_LOG(LOG_DEBUG, BR_DBGF_RT_TABLE, "added %02x:%02x:%02x:%02x:%02x:%02x " "on %s count %u hashsize %u", dst[0], dst[1], dst[2], dst[3], dst[4], dst[5], sc->sc_ifp->if_xname, sc->sc_brtcnt, sc->sc_rthash_size); } if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC && brt->brt_dst != bif) { brt->brt_dst->bif_addrcnt--; brt->brt_dst = bif; brt->brt_dst->bif_addrcnt++; } if ((flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { unsigned long now; now = (unsigned long) net_uptime(); brt->brt_expire = now + sc->sc_brttimeout; } if (setflags) { brt->brt_flags = flags; } return 0; } /* * bridge_rtlookup: * * Lookup the destination interface for an address. */ static struct ifnet * bridge_rtlookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan) { struct bridge_rtnode *brt; BRIDGE_LOCK_ASSERT_HELD(sc); if ((brt = bridge_rtnode_lookup(sc, addr, vlan)) == NULL) { return NULL; } return brt->brt_ifp; } /* * bridge_rttrim: * * Trim the routine table so that we have a number * of routing entries less than or equal to the * maximum number. */ static void bridge_rttrim(struct bridge_softc *sc) { struct bridge_rtnode *brt, *nbrt; BRIDGE_LOCK_ASSERT_HELD(sc); /* Make sure we actually need to do this. */ if (sc->sc_brtcnt <= sc->sc_brtmax) { return; } /* Force an aging cycle; this might trim enough addresses. */ bridge_rtage(sc); if (sc->sc_brtcnt <= sc->sc_brtmax) { return; } LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { bridge_rtnode_destroy(sc, brt); if (sc->sc_brtcnt <= sc->sc_brtmax) { return; } } } } /* * bridge_aging_timer: * * Aging periodic timer for the bridge routing table. */ static void bridge_aging_timer(struct bridge_softc *sc) { BRIDGE_LOCK_ASSERT_HELD(sc); bridge_rtage(sc); if ((sc->sc_ifp->if_flags & IFF_RUNNING) && (sc->sc_flags & SCF_DETACHING) == 0) { sc->sc_aging_timer.bdc_sc = sc; sc->sc_aging_timer.bdc_func = bridge_aging_timer; sc->sc_aging_timer.bdc_ts.tv_sec = bridge_rtable_prune_period; bridge_schedule_delayed_call(&sc->sc_aging_timer); } } /* * bridge_rtage: * * Perform an aging cycle. */ static void bridge_rtage(struct bridge_softc *sc) { struct bridge_rtnode *brt, *nbrt; unsigned long now; BRIDGE_LOCK_ASSERT_HELD(sc); now = (unsigned long) net_uptime(); LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { if ((brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { if (now >= brt->brt_expire) { bridge_rtnode_destroy(sc, brt); } } } if (sc->sc_mac_nat_bif != NULL) { bridge_mac_nat_age_entries(sc, now); } } /* * bridge_rtflush: * * Remove all dynamic addresses from the bridge. */ static void bridge_rtflush(struct bridge_softc *sc, int full) { struct bridge_rtnode *brt, *nbrt; BRIDGE_LOCK_ASSERT_HELD(sc); LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { if (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { bridge_rtnode_destroy(sc, brt); } } } /* * bridge_rtdaddr: * * Remove an address from the table. */ static int bridge_rtdaddr(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan) { struct bridge_rtnode *brt; int found = 0; BRIDGE_LOCK_ASSERT_HELD(sc); /* * If vlan is zero then we want to delete for all vlans so the lookup * may return more than one. */ while ((brt = bridge_rtnode_lookup(sc, addr, vlan)) != NULL) { bridge_rtnode_destroy(sc, brt); found = 1; } return found ? 0 : ENOENT; } /* * bridge_rtdelete: * * Delete routes to a specific member interface. */ static void bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int full) { struct bridge_rtnode *brt, *nbrt; BRIDGE_LOCK_ASSERT_HELD(sc); LIST_FOREACH_SAFE(brt, &sc->sc_rtlist, brt_list, nbrt) { if (brt->brt_ifp == ifp && (full || (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)) { bridge_rtnode_destroy(sc, brt); } } } /* * bridge_rtable_init: * * Initialize the route table for this bridge. */ static int bridge_rtable_init(struct bridge_softc *sc) { u_int32_t i; sc->sc_rthash = kalloc_type(struct _bridge_rtnode_list, BRIDGE_RTHASH_SIZE, Z_WAITOK_ZERO_NOFAIL); sc->sc_rthash_size = BRIDGE_RTHASH_SIZE; for (i = 0; i < sc->sc_rthash_size; i++) { LIST_INIT(&sc->sc_rthash[i]); } sc->sc_rthash_key = RandomULong(); LIST_INIT(&sc->sc_rtlist); return 0; } /* * bridge_rthash_delayed_resize: * * Resize the routing table hash on a delayed thread call. */ static void bridge_rthash_delayed_resize(struct bridge_softc *sc) { u_int32_t new_rthash_size = 0; u_int32_t old_rthash_size = 0; struct _bridge_rtnode_list *new_rthash = NULL; struct _bridge_rtnode_list *old_rthash = NULL; u_int32_t i; struct bridge_rtnode *brt; int error = 0; BRIDGE_LOCK_ASSERT_HELD(sc); /* * Four entries per hash bucket is our ideal load factor */ if (sc->sc_brtcnt < sc->sc_rthash_size * 4) { goto out; } /* * Doubling the number of hash buckets may be too simplistic * especially when facing a spike of new entries */ new_rthash_size = sc->sc_rthash_size * 2; sc->sc_flags |= SCF_RESIZING; BRIDGE_UNLOCK(sc); new_rthash = kalloc_type(struct _bridge_rtnode_list, new_rthash_size, Z_WAITOK | Z_ZERO); BRIDGE_LOCK(sc); sc->sc_flags &= ~SCF_RESIZING; if (new_rthash == NULL) { error = ENOMEM; goto out; } if ((sc->sc_flags & SCF_DETACHING)) { error = ENODEV; goto out; } /* * Fail safe from here on */ old_rthash = sc->sc_rthash; old_rthash_size = sc->sc_rthash_size; sc->sc_rthash = new_rthash; sc->sc_rthash_size = new_rthash_size; /* * Get a new key to force entries to be shuffled around to reduce * the likelihood they will land in the same buckets */ sc->sc_rthash_key = RandomULong(); for (i = 0; i < sc->sc_rthash_size; i++) { LIST_INIT(&sc->sc_rthash[i]); } LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { LIST_REMOVE(brt, brt_hash); (void) bridge_rtnode_hash(sc, brt); } out: if (error == 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_RT_TABLE, "%s new size %u", sc->sc_ifp->if_xname, sc->sc_rthash_size); kfree_type(struct _bridge_rtnode_list, old_rthash_size, old_rthash); } else { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_RT_TABLE, "%s failed %d", sc->sc_ifp->if_xname, error); kfree_type(struct _bridge_rtnode_list, new_rthash_size, new_rthash); } } /* * Resize the number of hash buckets based on the load factor * Currently only grow * Failing to resize the hash table is not fatal */ static void bridge_rthash_resize(struct bridge_softc *sc) { BRIDGE_LOCK_ASSERT_HELD(sc); if ((sc->sc_flags & SCF_DETACHING) || (sc->sc_flags & SCF_RESIZING)) { return; } /* * Four entries per hash bucket is our ideal load factor */ if (sc->sc_brtcnt < sc->sc_rthash_size * 4) { return; } /* * Hard limit on the size of the routing hash table */ if (sc->sc_rthash_size >= bridge_rtable_hash_size_max) { return; } sc->sc_resize_call.bdc_sc = sc; sc->sc_resize_call.bdc_func = bridge_rthash_delayed_resize; bridge_schedule_delayed_call(&sc->sc_resize_call); } /* * bridge_rtable_fini: * * Deconstruct the route table for this bridge. */ static void bridge_rtable_fini(struct bridge_softc *sc) { KASSERT(sc->sc_brtcnt == 0, ("%s: %d bridge routes referenced", __func__, sc->sc_brtcnt)); kfree_type(struct _bridge_rtnode_list, sc->sc_rthash_size, sc->sc_rthash); sc->sc_rthash = NULL; sc->sc_rthash_size = 0; } /* * The following hash function is adapted from "Hash Functions" by Bob Jenkins * ("Algorithm Alley", Dr. Dobbs Journal, September 1997). */ #define mix(a, b, c) \ do { \ a -= b; a -= c; a ^= (c >> 13); \ b -= c; b -= a; b ^= (a << 8); \ c -= a; c -= b; c ^= (b >> 13); \ a -= b; a -= c; a ^= (c >> 12); \ b -= c; b -= a; b ^= (a << 16); \ c -= a; c -= b; c ^= (b >> 5); \ a -= b; a -= c; a ^= (c >> 3); \ b -= c; b -= a; b ^= (a << 10); \ c -= a; c -= b; c ^= (b >> 15); \ } while ( /*CONSTCOND*/ 0) static __inline uint32_t bridge_rthash(struct bridge_softc *sc, const uint8_t *addr) { uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->sc_rthash_key; b += addr[5] << 8; b += addr[4]; a += addr[3] << 24; a += addr[2] << 16; a += addr[1] << 8; a += addr[0]; mix(a, b, c); return c & BRIDGE_RTHASH_MASK(sc); } #undef mix static int bridge_rtnode_addr_cmp(const uint8_t *a, const uint8_t *b) { int i, d; for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) { d = ((int)a[i]) - ((int)b[i]); } return d; } /* * bridge_rtnode_lookup: * * Look up a bridge route node for the specified destination. Compare the * vlan id or if zero then just return the first match. */ static struct bridge_rtnode * bridge_rtnode_lookup(struct bridge_softc *sc, const uint8_t *addr, uint16_t vlan) { struct bridge_rtnode *brt; uint32_t hash; int dir; BRIDGE_LOCK_ASSERT_HELD(sc); hash = bridge_rthash(sc, addr); LIST_FOREACH(brt, &sc->sc_rthash[hash], brt_hash) { dir = bridge_rtnode_addr_cmp(addr, brt->brt_addr); if (dir == 0 && (brt->brt_vlan == vlan || vlan == 0)) { return brt; } if (dir > 0) { return NULL; } } return NULL; } /* * bridge_rtnode_hash: * * Insert the specified bridge node into the route hash table. * This is used when adding a new node or to rehash when resizing * the hash table */ static int bridge_rtnode_hash(struct bridge_softc *sc, struct bridge_rtnode *brt) { struct bridge_rtnode *lbrt; uint32_t hash; int dir; BRIDGE_LOCK_ASSERT_HELD(sc); hash = bridge_rthash(sc, brt->brt_addr); lbrt = LIST_FIRST(&sc->sc_rthash[hash]); if (lbrt == NULL) { LIST_INSERT_HEAD(&sc->sc_rthash[hash], brt, brt_hash); goto out; } do { dir = bridge_rtnode_addr_cmp(brt->brt_addr, lbrt->brt_addr); if (dir == 0 && brt->brt_vlan == lbrt->brt_vlan) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_RT_TABLE, "%s EEXIST %02x:%02x:%02x:%02x:%02x:%02x", sc->sc_ifp->if_xname, brt->brt_addr[0], brt->brt_addr[1], brt->brt_addr[2], brt->brt_addr[3], brt->brt_addr[4], brt->brt_addr[5]); return EEXIST; } if (dir > 0) { LIST_INSERT_BEFORE(lbrt, brt, brt_hash); goto out; } if (LIST_NEXT(lbrt, brt_hash) == NULL) { LIST_INSERT_AFTER(lbrt, brt, brt_hash); goto out; } lbrt = LIST_NEXT(lbrt, brt_hash); } while (lbrt != NULL); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_RT_TABLE, "%s impossible %02x:%02x:%02x:%02x:%02x:%02x", sc->sc_ifp->if_xname, brt->brt_addr[0], brt->brt_addr[1], brt->brt_addr[2], brt->brt_addr[3], brt->brt_addr[4], brt->brt_addr[5]); out: return 0; } /* * bridge_rtnode_insert: * * Insert the specified bridge node into the route table. We * assume the entry is not already in the table. */ static int bridge_rtnode_insert(struct bridge_softc *sc, struct bridge_rtnode *brt) { int error; error = bridge_rtnode_hash(sc, brt); if (error != 0) { return error; } LIST_INSERT_HEAD(&sc->sc_rtlist, brt, brt_list); sc->sc_brtcnt++; bridge_rthash_resize(sc); return 0; } /* * bridge_rtnode_destroy: * * Destroy a bridge rtnode. */ static void bridge_rtnode_destroy(struct bridge_softc *sc, struct bridge_rtnode *brt) { BRIDGE_LOCK_ASSERT_HELD(sc); LIST_REMOVE(brt, brt_hash); LIST_REMOVE(brt, brt_list); sc->sc_brtcnt--; brt->brt_dst->bif_addrcnt--; zfree(bridge_rtnode_pool, brt); } #if BRIDGESTP /* * bridge_rtable_expire: * * Set the expiry time for all routes on an interface. */ static void bridge_rtable_expire(struct ifnet *ifp, int age) { struct bridge_softc *sc = ifp->if_bridge; struct bridge_rtnode *brt; BRIDGE_LOCK(sc); /* * If the age is zero then flush, otherwise set all the expiry times to * age for the interface */ if (age == 0) { bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN); } else { unsigned long now; now = (unsigned long) net_uptime(); LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { /* Cap the expiry time to 'age' */ if (brt->brt_ifp == ifp && brt->brt_expire > now + age && (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) { brt->brt_expire = now + age; } } } BRIDGE_UNLOCK(sc); } /* * bridge_state_change: * * Callback from the bridgestp code when a port changes states. */ static void bridge_state_change(struct ifnet *ifp, int state) { struct bridge_softc *sc = ifp->if_bridge; static const char *stpstates[] = { "disabled", "listening", "learning", "forwarding", "blocking", "discarding" }; if (log_stp) { log(LOG_NOTICE, "%s: state changed to %s on %s", sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname); } } #endif /* BRIDGESTP */ /* * bridge_set_bpf_tap: * * Sets ups the BPF callbacks. */ static errno_t bridge_set_bpf_tap(ifnet_t ifp, bpf_tap_mode mode, bpf_packet_func bpf_callback) { struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp); /* TBD locking */ if (sc == NULL || (sc->sc_flags & SCF_DETACHING)) { return ENODEV; } switch (mode) { case BPF_TAP_DISABLE: sc->sc_bpf_input = sc->sc_bpf_output = NULL; break; case BPF_TAP_INPUT: sc->sc_bpf_input = bpf_callback; break; case BPF_TAP_OUTPUT: sc->sc_bpf_output = bpf_callback; break; case BPF_TAP_INPUT_OUTPUT: sc->sc_bpf_input = sc->sc_bpf_output = bpf_callback; break; default: break; } return 0; } /* * bridge_detach: * * Callback when interface has been detached. */ static void bridge_detach(ifnet_t ifp) { struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp); #if BRIDGESTP bstp_detach(&sc->sc_stp); #endif /* BRIDGESTP */ /* Tear down the routing table. */ bridge_rtable_fini(sc); lck_mtx_lock(&bridge_list_mtx); LIST_REMOVE(sc, sc_list); lck_mtx_unlock(&bridge_list_mtx); ifnet_release(ifp); lck_mtx_destroy(&sc->sc_mtx, &bridge_lock_grp); if_clone_softc_deallocate(&bridge_cloner, sc); } /* * bridge_bpf_input: * * Invoke the input BPF callback if enabled */ static errno_t bridge_bpf_input(ifnet_t ifp, struct mbuf *m, const char * func, int line) { struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp); bpf_packet_func input_func = sc->sc_bpf_input; if (input_func != NULL) { if (mbuf_pkthdr_rcvif(m) != ifp) { BRIDGE_LOG(LOG_NOTICE, 0, "%s.%d: rcvif: 0x%llx != ifp 0x%llx", func, line, (uint64_t)VM_KERNEL_ADDRPERM(mbuf_pkthdr_rcvif(m)), (uint64_t)VM_KERNEL_ADDRPERM(ifp)); } (*input_func)(ifp, m); } return 0; } /* * bridge_bpf_output: * * Invoke the output BPF callback if enabled */ static errno_t bridge_bpf_output(ifnet_t ifp, struct mbuf *m) { struct bridge_softc *sc = (struct bridge_softc *)ifnet_softc(ifp); bpf_packet_func output_func = sc->sc_bpf_output; if (output_func != NULL) { (*output_func)(ifp, m); } return 0; } /* * bridge_link_event: * * Report a data link event on an interface */ static void bridge_link_event(struct ifnet *ifp, u_int32_t event_code) { struct event { u_int32_t ifnet_family; u_int32_t unit; char if_name[IFNAMSIZ]; }; _Alignas(struct kern_event_msg) char message[sizeof(struct kern_event_msg) + sizeof(struct event)] = { 0 }; struct kern_event_msg *header = (struct kern_event_msg*)message; struct event *data = (struct event *)(header + 1); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_LIFECYCLE, "%s event_code %u - %s", ifp->if_xname, event_code, dlil_kev_dl_code_str(event_code)); header->total_size = sizeof(message); header->vendor_code = KEV_VENDOR_APPLE; header->kev_class = KEV_NETWORK_CLASS; header->kev_subclass = KEV_DL_SUBCLASS; header->event_code = event_code; data->ifnet_family = ifnet_family(ifp); data->unit = (u_int32_t)ifnet_unit(ifp); strlcpy(data->if_name, ifnet_name(ifp), IFNAMSIZ); ifnet_event(ifp, header); } #define BRIDGE_HF_DROP(reason, func, line) { \ bridge_hostfilter_stats.reason++; \ BRIDGE_LOG(LOG_DEBUG, BR_DBGF_HOSTFILTER, \ "%s.%d" #reason, func, line); \ error = EINVAL; \ } /* * Make sure this is a DHCP or Bootp request that match the host filter */ static int bridge_dhcp_filter(struct bridge_iflist *bif, struct mbuf *m, size_t offset) { int error = EINVAL; struct dhcp dhcp; /* * Note: We use the dhcp structure because bootp structure definition * is larger and some vendors do not pad the request */ error = mbuf_copydata(m, offset, sizeof(struct dhcp), &dhcp); if (error != 0) { BRIDGE_HF_DROP(brhf_dhcp_too_small, __func__, __LINE__); goto done; } if (dhcp.dp_op != BOOTREQUEST) { BRIDGE_HF_DROP(brhf_dhcp_bad_op, __func__, __LINE__); goto done; } /* * The hardware address must be an exact match */ if (dhcp.dp_htype != ARPHRD_ETHER) { BRIDGE_HF_DROP(brhf_dhcp_bad_htype, __func__, __LINE__); goto done; } if (dhcp.dp_hlen != ETHER_ADDR_LEN) { BRIDGE_HF_DROP(brhf_dhcp_bad_hlen, __func__, __LINE__); goto done; } if (bcmp(dhcp.dp_chaddr, bif->bif_hf_hwsrc, ETHER_ADDR_LEN) != 0) { BRIDGE_HF_DROP(brhf_dhcp_bad_chaddr, __func__, __LINE__); goto done; } /* * Client address must match the host address or be not specified */ if (dhcp.dp_ciaddr.s_addr != bif->bif_hf_ipsrc.s_addr && dhcp.dp_ciaddr.s_addr != INADDR_ANY) { BRIDGE_HF_DROP(brhf_dhcp_bad_ciaddr, __func__, __LINE__); goto done; } error = 0; done: return error; } static int bridge_host_filter(struct bridge_iflist *bif, mbuf_t *data) { int error = EINVAL; struct ether_header *eh; static struct in_addr inaddr_any = { .s_addr = INADDR_ANY }; mbuf_t m = *data; eh = mtod(m, struct ether_header *); /* * Restrict the source hardware address */ if ((bif->bif_flags & BIFF_HF_HWSRC) == 0 || bcmp(eh->ether_shost, bif->bif_hf_hwsrc, ETHER_ADDR_LEN) != 0) { BRIDGE_HF_DROP(brhf_bad_ether_srchw_addr, __func__, __LINE__); goto done; } /* * Restrict Ethernet protocols to ARP and IP */ if (eh->ether_type == htons(ETHERTYPE_ARP)) { struct ether_arp *ea; size_t minlen = sizeof(struct ether_header) + sizeof(struct ether_arp); /* * Make the Ethernet and ARP headers contiguous */ if (mbuf_pkthdr_len(m) < minlen) { BRIDGE_HF_DROP(brhf_arp_too_small, __func__, __LINE__); goto done; } if (mbuf_len(m) < minlen && mbuf_pullup(data, minlen) != 0) { BRIDGE_HF_DROP(brhf_arp_pullup_failed, __func__, __LINE__); goto done; } m = *data; /* * Verify this is an ethernet/ip arp */ eh = mtod(m, struct ether_header *); ea = (struct ether_arp *)(eh + 1); if (ea->arp_hrd != htons(ARPHRD_ETHER)) { BRIDGE_HF_DROP(brhf_arp_bad_hw_type, __func__, __LINE__); goto done; } if (ea->arp_pro != htons(ETHERTYPE_IP)) { BRIDGE_HF_DROP(brhf_arp_bad_pro_type, __func__, __LINE__); goto done; } /* * Verify the address lengths are correct */ if (ea->arp_hln != ETHER_ADDR_LEN) { BRIDGE_HF_DROP(brhf_arp_bad_hw_len, __func__, __LINE__); goto done; } if (ea->arp_pln != sizeof(struct in_addr)) { BRIDGE_HF_DROP(brhf_arp_bad_pro_len, __func__, __LINE__); goto done; } /* * Allow only ARP request or ARP reply */ if (ea->arp_op != htons(ARPOP_REQUEST) && ea->arp_op != htons(ARPOP_REPLY)) { BRIDGE_HF_DROP(brhf_arp_bad_op, __func__, __LINE__); goto done; } /* * Verify source hardware address matches */ if (bcmp(ea->arp_sha, bif->bif_hf_hwsrc, ETHER_ADDR_LEN) != 0) { BRIDGE_HF_DROP(brhf_arp_bad_sha, __func__, __LINE__); goto done; } /* * Verify source protocol address: * May be null for an ARP probe */ if (bcmp(ea->arp_spa, &bif->bif_hf_ipsrc.s_addr, sizeof(struct in_addr)) != 0 && bcmp(ea->arp_spa, &inaddr_any, sizeof(struct in_addr)) != 0) { BRIDGE_HF_DROP(brhf_arp_bad_spa, __func__, __LINE__); goto done; } bridge_hostfilter_stats.brhf_arp_ok += 1; error = 0; } else if (eh->ether_type == htons(ETHERTYPE_IP)) { size_t minlen = sizeof(struct ether_header) + sizeof(struct ip); struct ip iphdr; size_t offset; /* * Make the Ethernet and IP headers contiguous */ if (mbuf_pkthdr_len(m) < minlen) { BRIDGE_HF_DROP(brhf_ip_too_small, __func__, __LINE__); goto done; } offset = sizeof(struct ether_header); error = mbuf_copydata(m, offset, sizeof(struct ip), &iphdr); if (error != 0) { BRIDGE_HF_DROP(brhf_ip_too_small, __func__, __LINE__); goto done; } /* * Verify the source IP address */ if (iphdr.ip_p == IPPROTO_UDP) { struct udphdr udp; minlen += sizeof(struct udphdr); if (mbuf_pkthdr_len(m) < minlen) { BRIDGE_HF_DROP(brhf_ip_too_small, __func__, __LINE__); goto done; } /* * Allow all zero addresses for DHCP requests */ if (iphdr.ip_src.s_addr != bif->bif_hf_ipsrc.s_addr && iphdr.ip_src.s_addr != INADDR_ANY) { BRIDGE_HF_DROP(brhf_ip_bad_srcaddr, __func__, __LINE__); goto done; } offset = sizeof(struct ether_header) + (IP_VHL_HL(iphdr.ip_vhl) << 2); error = mbuf_copydata(m, offset, sizeof(struct udphdr), &udp); if (error != 0) { BRIDGE_HF_DROP(brhf_ip_too_small, __func__, __LINE__); goto done; } /* * Either it's a Bootp/DHCP packet that we like or * it's a UDP packet from the host IP as source address */ if (udp.uh_sport == htons(IPPORT_BOOTPC) && udp.uh_dport == htons(IPPORT_BOOTPS)) { minlen += sizeof(struct dhcp); if (mbuf_pkthdr_len(m) < minlen) { BRIDGE_HF_DROP(brhf_ip_too_small, __func__, __LINE__); goto done; } offset += sizeof(struct udphdr); error = bridge_dhcp_filter(bif, m, offset); if (error != 0) { goto done; } } else if (iphdr.ip_src.s_addr == INADDR_ANY) { BRIDGE_HF_DROP(brhf_ip_bad_srcaddr, __func__, __LINE__); goto done; } } else if (iphdr.ip_src.s_addr != bif->bif_hf_ipsrc.s_addr || bif->bif_hf_ipsrc.s_addr == INADDR_ANY) { BRIDGE_HF_DROP(brhf_ip_bad_srcaddr, __func__, __LINE__); goto done; } /* * Allow only boring IP protocols */ if (iphdr.ip_p != IPPROTO_TCP && iphdr.ip_p != IPPROTO_UDP && iphdr.ip_p != IPPROTO_ICMP && iphdr.ip_p != IPPROTO_ESP && iphdr.ip_p != IPPROTO_AH && iphdr.ip_p != IPPROTO_GRE) { BRIDGE_HF_DROP(brhf_ip_bad_proto, __func__, __LINE__); goto done; } bridge_hostfilter_stats.brhf_ip_ok += 1; error = 0; } else { BRIDGE_HF_DROP(brhf_bad_ether_type, __func__, __LINE__); goto done; } done: if (error != 0) { if (BRIDGE_DBGF_ENABLED(BR_DBGF_HOSTFILTER)) { if (m) { brlog_mbuf_data(m, 0, sizeof(struct ether_header) + sizeof(struct ip)); } } if (m != NULL) { m_freem(m); } } return error; } /* * MAC NAT */ static errno_t bridge_mac_nat_enable(struct bridge_softc *sc, struct bridge_iflist *bif) { errno_t error = 0; BRIDGE_LOCK_ASSERT_HELD(sc); if (IFNET_IS_VMNET(bif->bif_ifp)) { error = EINVAL; goto done; } if (sc->sc_mac_nat_bif != NULL) { if (sc->sc_mac_nat_bif != bif) { error = EBUSY; } goto done; } sc->sc_mac_nat_bif = bif; bif->bif_ifflags |= IFBIF_MAC_NAT; bridge_mac_nat_populate_entries(sc); done: return error; } static void bridge_mac_nat_disable(struct bridge_softc *sc) { struct bridge_iflist *mac_nat_bif = sc->sc_mac_nat_bif; assert(mac_nat_bif != NULL); bridge_mac_nat_flush_entries(sc, mac_nat_bif); mac_nat_bif->bif_ifflags &= ~IFBIF_MAC_NAT; sc->sc_mac_nat_bif = NULL; return; } static void mac_nat_entry_print2(struct mac_nat_entry *mne, char *ifname, const char *msg1, const char *msg2) { int af; char etopbuf[24]; char ntopbuf[MAX_IPv6_STR_LEN]; const char *space; af = ((mne->mne_flags & MNE_FLAGS_IPV6) != 0) ? AF_INET6 : AF_INET; ether_ntop(etopbuf, sizeof(etopbuf), mne->mne_mac); (void)inet_ntop(af, &mne->mne_u, ntopbuf, sizeof(ntopbuf)); if (msg2 == NULL) { msg2 = ""; space = ""; } else { space = " "; } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "%s %s%s%s %p (%s, %s, %s)", ifname, msg1, space, msg2, mne, mne->mne_bif->bif_ifp->if_xname, ntopbuf, etopbuf); } static void mac_nat_entry_print(struct mac_nat_entry *mne, char *ifname, const char *msg) { mac_nat_entry_print2(mne, ifname, msg, NULL); } static struct mac_nat_entry * bridge_lookup_mac_nat_entry(struct bridge_softc *sc, int af, void * ip) { struct mac_nat_entry *mne; struct mac_nat_entry *ret_mne = NULL; if (af == AF_INET) { in_addr_t s_addr = ((struct in_addr *)ip)->s_addr; LIST_FOREACH(mne, &sc->sc_mne_list, mne_list) { if (mne->mne_ip.s_addr == s_addr) { if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) { mac_nat_entry_print(mne, sc->sc_if_xname, "found"); } ret_mne = mne; break; } } } else { const struct in6_addr *ip6 = (const struct in6_addr *)ip; LIST_FOREACH(mne, &sc->sc_mne_list_v6, mne_list) { if (IN6_ARE_ADDR_EQUAL(&mne->mne_ip6, ip6)) { if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) { mac_nat_entry_print(mne, sc->sc_if_xname, "found"); } ret_mne = mne; break; } } } return ret_mne; } static void bridge_destroy_mac_nat_entry(struct bridge_softc *sc, struct mac_nat_entry *mne, const char *reason) { LIST_REMOVE(mne, mne_list); if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) { mac_nat_entry_print(mne, sc->sc_if_xname, reason); } zfree(bridge_mne_pool, mne); sc->sc_mne_count--; } static struct mac_nat_entry * bridge_create_mac_nat_entry(struct bridge_softc *sc, struct bridge_iflist *bif, int af, const void *ip, uint8_t *eaddr) { struct mac_nat_entry_list *list; struct mac_nat_entry *mne; if (sc->sc_mne_count >= sc->sc_mne_max) { sc->sc_mne_allocation_failures++; return NULL; } mne = zalloc_noblock(bridge_mne_pool); if (mne == NULL) { sc->sc_mne_allocation_failures++; return NULL; } sc->sc_mne_count++; bzero(mne, sizeof(*mne)); bcopy(eaddr, mne->mne_mac, sizeof(mne->mne_mac)); mne->mne_bif = bif; if (af == AF_INET) { bcopy(ip, &mne->mne_ip, sizeof(mne->mne_ip)); list = &sc->sc_mne_list; } else { bcopy(ip, &mne->mne_ip6, sizeof(mne->mne_ip6)); mne->mne_flags |= MNE_FLAGS_IPV6; list = &sc->sc_mne_list_v6; } LIST_INSERT_HEAD(list, mne, mne_list); mne->mne_expire = (unsigned long)net_uptime() + sc->sc_brttimeout; if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) { mac_nat_entry_print(mne, sc->sc_if_xname, "created"); } return mne; } static struct mac_nat_entry * bridge_update_mac_nat_entry(struct bridge_softc *sc, struct bridge_iflist *bif, int af, void *ip, uint8_t *eaddr) { struct mac_nat_entry *mne; mne = bridge_lookup_mac_nat_entry(sc, af, ip); if (mne != NULL) { struct bridge_iflist *mac_nat_bif = sc->sc_mac_nat_bif; if (mne->mne_bif == mac_nat_bif) { /* the MAC NAT interface takes precedence */ if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) { if (mne->mne_bif != bif) { mac_nat_entry_print2(mne, sc->sc_if_xname, "reject", bif->bif_ifp->if_xname); } } } else if (mne->mne_bif != bif) { const char *old_if = mne->mne_bif->bif_ifp->if_xname; mne->mne_bif = bif; if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) { mac_nat_entry_print2(mne, sc->sc_if_xname, "replaced", old_if); } bcopy(eaddr, mne->mne_mac, sizeof(mne->mne_mac)); } mne->mne_expire = (unsigned long)net_uptime() + sc->sc_brttimeout; } else { mne = bridge_create_mac_nat_entry(sc, bif, af, ip, eaddr); } return mne; } static void bridge_mac_nat_flush_entries_common(struct bridge_softc *sc, struct mac_nat_entry_list *list, struct bridge_iflist *bif) { struct mac_nat_entry *mne; struct mac_nat_entry *tmne; LIST_FOREACH_SAFE(mne, list, mne_list, tmne) { if (bif != NULL && mne->mne_bif != bif) { continue; } bridge_destroy_mac_nat_entry(sc, mne, "flushed"); } } /* * bridge_mac_nat_flush_entries: * * Flush MAC NAT entries for the specified member. Flush all entries if * the member is the one that requires MAC NAT, otherwise just flush the * ones for the specified member. */ static void bridge_mac_nat_flush_entries(struct bridge_softc *sc, struct bridge_iflist * bif) { struct bridge_iflist *flush_bif; flush_bif = (bif == sc->sc_mac_nat_bif) ? NULL : bif; bridge_mac_nat_flush_entries_common(sc, &sc->sc_mne_list, flush_bif); bridge_mac_nat_flush_entries_common(sc, &sc->sc_mne_list_v6, flush_bif); } static void bridge_mac_nat_populate_entries(struct bridge_softc *sc) { errno_t error; ifnet_t ifp; ifaddr_t *list; struct bridge_iflist *mac_nat_bif = sc->sc_mac_nat_bif; assert(mac_nat_bif != NULL); ifp = mac_nat_bif->bif_ifp; error = ifnet_get_address_list(ifp, &list); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT, "ifnet_get_address_list(%s) failed %d", ifp->if_xname, error); return; } for (ifaddr_t *scan = list; *scan != NULL; scan++) { sa_family_t af; void *ip; union { struct sockaddr sa; struct sockaddr_in sin; struct sockaddr_in6 sin6; } u; af = ifaddr_address_family(*scan); switch (af) { case AF_INET: case AF_INET6: error = ifaddr_address(*scan, &u.sa, sizeof(u)); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT, "ifaddr_address failed %d", error); break; } if (af == AF_INET) { ip = (void *)&u.sin.sin_addr; } else { if (IN6_IS_ADDR_LINKLOCAL(&u.sin6.sin6_addr)) { /* remove scope ID */ u.sin6.sin6_addr.s6_addr16[1] = 0; } ip = (void *)&u.sin6.sin6_addr; } bridge_create_mac_nat_entry(sc, mac_nat_bif, af, ip, (uint8_t *)IF_LLADDR(ifp)); break; default: break; } } ifnet_free_address_list(list); return; } static void bridge_mac_nat_age_entries_common(struct bridge_softc *sc, struct mac_nat_entry_list *list, unsigned long now) { struct mac_nat_entry *mne; struct mac_nat_entry *tmne; LIST_FOREACH_SAFE(mne, list, mne_list, tmne) { if (now >= mne->mne_expire) { bridge_destroy_mac_nat_entry(sc, mne, "aged out"); } } } static void bridge_mac_nat_age_entries(struct bridge_softc *sc, unsigned long now) { if (sc->sc_mac_nat_bif == NULL) { return; } bridge_mac_nat_age_entries_common(sc, &sc->sc_mne_list, now); bridge_mac_nat_age_entries_common(sc, &sc->sc_mne_list_v6, now); } static const char * get_in_out_string(boolean_t is_output) { return is_output ? "OUT" : "IN"; } /* * is_valid_arp_packet: * Verify that this is a valid ARP packet. * * Returns TRUE if the packet is valid, FALSE otherwise. */ static boolean_t is_valid_arp_packet(mbuf_t *data, boolean_t is_output, struct ether_header **eh_p, struct ether_arp **ea_p) { struct ether_arp *ea; struct ether_header *eh; size_t minlen = sizeof(struct ether_header) + sizeof(struct ether_arp); boolean_t is_valid = FALSE; int flags = is_output ? BR_DBGF_OUTPUT : BR_DBGF_INPUT; if (mbuf_pkthdr_len(*data) < minlen) { BRIDGE_LOG(LOG_DEBUG, flags, "ARP %s short frame %lu < %lu", get_in_out_string(is_output), mbuf_pkthdr_len(*data), minlen); goto done; } if (mbuf_len(*data) < minlen && mbuf_pullup(data, minlen) != 0) { BRIDGE_LOG(LOG_DEBUG, flags, "ARP %s size %lu mbuf_pullup fail", get_in_out_string(is_output), minlen); *data = NULL; goto done; } /* validate ARP packet */ eh = mtod(*data, struct ether_header *); ea = (struct ether_arp *)(eh + 1); if (ntohs(ea->arp_hrd) != ARPHRD_ETHER) { BRIDGE_LOG(LOG_DEBUG, flags, "ARP %s htype not ethernet", get_in_out_string(is_output)); goto done; } if (ea->arp_hln != ETHER_ADDR_LEN) { BRIDGE_LOG(LOG_DEBUG, flags, "ARP %s hlen not ethernet", get_in_out_string(is_output)); goto done; } if (ntohs(ea->arp_pro) != ETHERTYPE_IP) { BRIDGE_LOG(LOG_DEBUG, flags, "ARP %s ptype not IP", get_in_out_string(is_output)); goto done; } if (ea->arp_pln != sizeof(struct in_addr)) { BRIDGE_LOG(LOG_DEBUG, flags, "ARP %s plen not IP", get_in_out_string(is_output)); goto done; } is_valid = TRUE; *ea_p = ea; *eh_p = eh; done: return is_valid; } static struct mac_nat_entry * bridge_mac_nat_arp_input(struct bridge_softc *sc, mbuf_t *data) { struct ether_arp *ea; struct ether_header *eh; struct mac_nat_entry *mne = NULL; u_short op; struct in_addr tpa; if (!is_valid_arp_packet(data, FALSE, &eh, &ea)) { goto done; } op = ntohs(ea->arp_op); switch (op) { case ARPOP_REQUEST: case ARPOP_REPLY: /* only care about REQUEST and REPLY */ break; default: goto done; } /* check the target IP address for a NAT entry */ bcopy(ea->arp_tpa, &tpa, sizeof(tpa)); if (tpa.s_addr != 0) { mne = bridge_lookup_mac_nat_entry(sc, AF_INET, &tpa); } if (mne != NULL) { if (op == ARPOP_REPLY) { /* translate the MAC address */ if (BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) { char mac_src[24]; char mac_dst[24]; ether_ntop(mac_src, sizeof(mac_src), ea->arp_tha); ether_ntop(mac_dst, sizeof(mac_dst), mne->mne_mac); BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "%s %s ARP %s -> %s", sc->sc_if_xname, mne->mne_bif->bif_ifp->if_xname, mac_src, mac_dst); } bcopy(mne->mne_mac, ea->arp_tha, sizeof(ea->arp_tha)); } } else { /* handle conflicting ARP (sender matches mne) */ struct in_addr spa; bcopy(ea->arp_spa, &spa, sizeof(spa)); if (spa.s_addr != 0 && spa.s_addr != tpa.s_addr) { /* check the source IP for a NAT entry */ mne = bridge_lookup_mac_nat_entry(sc, AF_INET, &spa); } } done: return mne; } static boolean_t bridge_mac_nat_arp_output(struct bridge_softc *sc, struct bridge_iflist *bif, mbuf_t *data, struct mac_nat_record *mnr) { struct ether_arp *ea; struct ether_header *eh; struct in_addr ip; struct mac_nat_entry *mne = NULL; u_short op; boolean_t translate = FALSE; if (!is_valid_arp_packet(data, TRUE, &eh, &ea)) { goto done; } op = ntohs(ea->arp_op); switch (op) { case ARPOP_REQUEST: case ARPOP_REPLY: /* only care about REQUEST and REPLY */ break; default: goto done; } bcopy(ea->arp_spa, &ip, sizeof(ip)); if (ip.s_addr == 0) { goto done; } /* XXX validate IP address: no multicast/broadcast */ mne = bridge_update_mac_nat_entry(sc, bif, AF_INET, &ip, ea->arp_sha); if (mnr != NULL && mne != NULL) { /* record the offset to do the replacement */ translate = TRUE; mnr->mnr_arp_offset = (char *)ea->arp_sha - (char *)eh; } done: return translate; } #define ETHER_IPV4_HEADER_LEN (sizeof(struct ether_header) + \ + sizeof(struct ip)) static struct ether_header * get_ether_ip_header(mbuf_t *data, boolean_t is_output) { struct ether_header *eh = NULL; int flags = is_output ? BR_DBGF_OUTPUT : BR_DBGF_INPUT; size_t minlen = ETHER_IPV4_HEADER_LEN; if (mbuf_pkthdr_len(*data) < minlen) { BRIDGE_LOG(LOG_DEBUG, flags, "IP %s short frame %lu < %lu", get_in_out_string(is_output), mbuf_pkthdr_len(*data), minlen); goto done; } if (mbuf_len(*data) < minlen && mbuf_pullup(data, minlen) != 0) { BRIDGE_LOG(LOG_DEBUG, flags, "IP %s size %lu mbuf_pullup fail", get_in_out_string(is_output), minlen); *data = NULL; goto done; } eh = mtod(*data, struct ether_header *); done: return eh; } static bool is_broadcast_ip_packet(mbuf_t *data) { struct ether_header *eh; uint16_t ether_type; bool is_broadcast = FALSE; eh = mtod(*data, struct ether_header *); ether_type = ntohs(eh->ether_type); switch (ether_type) { case ETHERTYPE_IP: eh = get_ether_ip_header(data, FALSE); if (eh != NULL) { struct in_addr dst; struct ip *iphdr; iphdr = (struct ip *)(void *)(eh + 1); bcopy(&iphdr->ip_dst, &dst, sizeof(dst)); is_broadcast = (dst.s_addr == INADDR_BROADCAST); } break; default: break; } return is_broadcast; } static struct mac_nat_entry * bridge_mac_nat_ip_input(struct bridge_softc *sc, mbuf_t *data) { struct in_addr dst; struct ether_header *eh; struct ip *iphdr; struct mac_nat_entry *mne = NULL; eh = get_ether_ip_header(data, FALSE); if (eh == NULL) { goto done; } iphdr = (struct ip *)(void *)(eh + 1); bcopy(&iphdr->ip_dst, &dst, sizeof(dst)); /* XXX validate IP address */ if (dst.s_addr == 0) { goto done; } mne = bridge_lookup_mac_nat_entry(sc, AF_INET, &dst); done: return mne; } static void bridge_mac_nat_udp_output(struct bridge_softc *sc, struct bridge_iflist *bif, mbuf_t m, uint8_t ip_header_len, struct mac_nat_record *mnr) { uint16_t dp_flags; errno_t error; size_t offset; struct udphdr udphdr; /* copy the UDP header */ offset = sizeof(struct ether_header) + ip_header_len; error = mbuf_copydata(m, offset, sizeof(struct udphdr), &udphdr); if (error != 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "mbuf_copydata udphdr failed %d", error); return; } if (ntohs(udphdr.uh_sport) != IPPORT_BOOTPC || ntohs(udphdr.uh_dport) != IPPORT_BOOTPS) { /* not a BOOTP/DHCP packet */ return; } /* check whether the broadcast bit is already set */ offset += sizeof(struct udphdr) + offsetof(struct dhcp, dp_flags); error = mbuf_copydata(m, offset, sizeof(dp_flags), &dp_flags); if (error != 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "mbuf_copydata dp_flags failed %d", error); return; } if ((ntohs(dp_flags) & DHCP_FLAGS_BROADCAST) != 0) { /* it's already set, nothing to do */ return; } /* broadcast bit needs to be set */ mnr->mnr_ip_dhcp_flags = dp_flags | htons(DHCP_FLAGS_BROADCAST); mnr->mnr_ip_header_len = ip_header_len; if (udphdr.uh_sum != 0) { uint16_t delta; /* adjust checksum to take modified dp_flags into account */ delta = dp_flags - mnr->mnr_ip_dhcp_flags; mnr->mnr_ip_udp_csum = udphdr.uh_sum + delta; } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "%s %s DHCP dp_flags 0x%x UDP cksum 0x%x", sc->sc_if_xname, bif->bif_ifp->if_xname, ntohs(mnr->mnr_ip_dhcp_flags), ntohs(mnr->mnr_ip_udp_csum)); return; } static boolean_t bridge_mac_nat_ip_output(struct bridge_softc *sc, struct bridge_iflist *bif, mbuf_t *data, struct mac_nat_record *mnr) { #pragma unused(mnr) struct ether_header *eh; struct in_addr ip; struct ip *iphdr; uint8_t ip_header_len; struct mac_nat_entry *mne = NULL; boolean_t translate = FALSE; eh = get_ether_ip_header(data, TRUE); if (eh == NULL) { goto done; } iphdr = (struct ip *)(void *)(eh + 1); ip_header_len = IP_VHL_HL(iphdr->ip_vhl) << 2; if (ip_header_len < sizeof(ip)) { /* bogus IP header */ goto done; } bcopy(&iphdr->ip_src, &ip, sizeof(ip)); /* XXX validate the source address */ if (ip.s_addr != 0) { mne = bridge_update_mac_nat_entry(sc, bif, AF_INET, &ip, eh->ether_shost); } if (mnr != NULL) { if (iphdr->ip_p == IPPROTO_UDP) { /* handle DHCP must broadcast */ bridge_mac_nat_udp_output(sc, bif, *data, ip_header_len, mnr); } translate = TRUE; } done: return translate; } #define ETHER_IPV6_HEADER_LEN (sizeof(struct ether_header) + \ + sizeof(struct ip6_hdr)) static struct ether_header * get_ether_ipv6_header(mbuf_t *data, boolean_t is_output) { struct ether_header *eh = NULL; int flags = is_output ? BR_DBGF_OUTPUT : BR_DBGF_INPUT; size_t minlen = ETHER_IPV6_HEADER_LEN; if (mbuf_pkthdr_len(*data) < minlen) { BRIDGE_LOG(LOG_DEBUG, flags, "IP %s short frame %lu < %lu", get_in_out_string(is_output), mbuf_pkthdr_len(*data), minlen); goto done; } if (mbuf_len(*data) < minlen && mbuf_pullup(data, minlen) != 0) { BRIDGE_LOG(LOG_DEBUG, flags, "IP %s size %lu mbuf_pullup fail", get_in_out_string(is_output), minlen); *data = NULL; goto done; } eh = mtod(*data, struct ether_header *); done: return eh; } #include <netinet/icmp6.h> #include <netinet6/nd6.h> #define ETHER_ND_LLADDR_LEN (ETHER_ADDR_LEN + sizeof(struct nd_opt_hdr)) static void bridge_mac_nat_icmpv6_output(struct bridge_softc *sc, struct bridge_iflist *bif, mbuf_t *data, struct ether_header *eh, struct ip6_hdr *ip6h, struct in6_addr *saddrp, struct mac_nat_record *mnr) { struct icmp6_hdr *icmp6; unsigned int icmp6len; int lladdrlen = 0; char *lladdr = NULL; mbuf_t m = *data; unsigned int off = sizeof(*ip6h); icmp6len = m->m_pkthdr.len - sizeof(*eh) - off; if (icmp6len < sizeof(*icmp6)) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT, "short packet %d < %lu", icmp6len, sizeof(*icmp6)); return; } icmp6 = (struct icmp6_hdr *)((caddr_t)ip6h + off); switch (icmp6->icmp6_type) { case ND_NEIGHBOR_SOLICIT: { struct nd_neighbor_solicit *nd_ns; union nd_opts ndopts; boolean_t is_dad_probe; struct in6_addr taddr; if (icmp6len < sizeof(*nd_ns)) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "short nd_ns %d < %lu", icmp6len, sizeof(*nd_ns)); return; } nd_ns = (struct nd_neighbor_solicit *)(void *)icmp6; bcopy(&nd_ns->nd_ns_target, &taddr, sizeof(taddr)); if (IN6_IS_ADDR_MULTICAST(&taddr) || IN6_IS_ADDR_UNSPECIFIED(&taddr)) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "invalid target ignored"); return; } /* parse options */ nd6_option_init(nd_ns + 1, icmp6len - sizeof(*nd_ns), &ndopts); if (nd6_options(&ndopts) < 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "invalid ND6 NS option"); return; } if (ndopts.nd_opts_src_lladdr != NULL) { lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; } is_dad_probe = IN6_IS_ADDR_UNSPECIFIED(saddrp); if (lladdr != NULL) { if (is_dad_probe) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "bad ND6 DAD packet"); return; } if (lladdrlen != ETHER_ND_LLADDR_LEN) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "source lladdrlen %d != %lu", lladdrlen, ETHER_ND_LLADDR_LEN); return; } mnr->mnr_ip6_lladdr_offset = (uint16_t)((uintptr_t)lladdr - (uintptr_t)eh); mnr->mnr_ip6_icmp6_len = icmp6len; mnr->mnr_ip6_icmp6_type = icmp6->icmp6_type; mnr->mnr_ip6_header_len = off; } if (is_dad_probe) { /* node is trying use taddr, create an mne using taddr */ *saddrp = taddr; } break; } case ND_NEIGHBOR_ADVERT: { struct nd_neighbor_advert *nd_na; union nd_opts ndopts; struct in6_addr taddr; nd_na = (struct nd_neighbor_advert *)(void *)icmp6; if (icmp6len < sizeof(*nd_na)) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "short nd_na %d < %lu", icmp6len, sizeof(*nd_na)); return; } bcopy(&nd_na->nd_na_target, &taddr, sizeof(taddr)); if (IN6_IS_ADDR_MULTICAST(&taddr) || IN6_IS_ADDR_UNSPECIFIED(&taddr)) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "invalid target ignored"); return; } /* parse options */ nd6_option_init(nd_na + 1, icmp6len - sizeof(*nd_na), &ndopts); if (nd6_options(&ndopts) < 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "invalid ND6 NA option"); return; } if (ndopts.nd_opts_tgt_lladdr == NULL) { /* target linklayer, nothing to do */ return; } lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; if (lladdrlen != ETHER_ND_LLADDR_LEN) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "target lladdrlen %d != %lu", lladdrlen, ETHER_ND_LLADDR_LEN); return; } mnr->mnr_ip6_lladdr_offset = (uint16_t)((uintptr_t)lladdr - (uintptr_t)eh); mnr->mnr_ip6_icmp6_len = icmp6len; mnr->mnr_ip6_header_len = off; mnr->mnr_ip6_icmp6_type = icmp6->icmp6_type; break; } case ND_ROUTER_SOLICIT: { struct nd_router_solicit *nd_rs; union nd_opts ndopts; if (icmp6len < sizeof(*nd_rs)) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "short nd_rs %d < %lu", icmp6len, sizeof(*nd_rs)); return; } nd_rs = (struct nd_router_solicit *)(void *)icmp6; /* parse options */ nd6_option_init(nd_rs + 1, icmp6len - sizeof(*nd_rs), &ndopts); if (nd6_options(&ndopts) < 0) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "invalid ND6 RS option"); return; } if (ndopts.nd_opts_src_lladdr != NULL) { lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; } if (lladdr != NULL) { if (lladdrlen != ETHER_ND_LLADDR_LEN) { BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "source lladdrlen %d != %lu", lladdrlen, ETHER_ND_LLADDR_LEN); return; } mnr->mnr_ip6_lladdr_offset = (uint16_t)((uintptr_t)lladdr - (uintptr_t)eh); mnr->mnr_ip6_icmp6_len = icmp6len; mnr->mnr_ip6_icmp6_type = icmp6->icmp6_type; mnr->mnr_ip6_header_len = off; } break; } default: break; } if (mnr->mnr_ip6_lladdr_offset != 0 && BRIDGE_DBGF_ENABLED(BR_DBGF_MAC_NAT)) { const char *str; switch (mnr->mnr_ip6_icmp6_type) { case ND_ROUTER_SOLICIT: str = "ROUTER SOLICIT"; break; case ND_NEIGHBOR_ADVERT: str = "NEIGHBOR ADVERT"; break; case ND_NEIGHBOR_SOLICIT: str = "NEIGHBOR SOLICIT"; break; default: str = ""; break; } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_MAC_NAT, "%s %s %s ip6len %d icmp6len %d lladdr offset %d", sc->sc_if_xname, bif->bif_ifp->if_xname, str, mnr->mnr_ip6_header_len, mnr->mnr_ip6_icmp6_len, mnr->mnr_ip6_lladdr_offset); } } static struct mac_nat_entry * bridge_mac_nat_ipv6_input(struct bridge_softc *sc, mbuf_t *data) { struct in6_addr dst; struct ether_header *eh; struct ip6_hdr *ip6h; struct mac_nat_entry *mne = NULL; eh = get_ether_ipv6_header(data, FALSE); if (eh == NULL) { goto done; } ip6h = (struct ip6_hdr *)(void *)(eh + 1); bcopy(&ip6h->ip6_dst, &dst, sizeof(dst)); /* XXX validate IPv6 address */ if (IN6_IS_ADDR_UNSPECIFIED(&dst)) { goto done; } mne = bridge_lookup_mac_nat_entry(sc, AF_INET6, &dst); done: return mne; } static boolean_t bridge_mac_nat_ipv6_output(struct bridge_softc *sc, struct bridge_iflist *bif, mbuf_t *data, struct mac_nat_record *mnr) { struct ether_header *eh; struct ip6_hdr *ip6h; struct in6_addr saddr; boolean_t translate; translate = (bif == sc->sc_mac_nat_bif) ? FALSE : TRUE; eh = get_ether_ipv6_header(data, TRUE); if (eh == NULL) { translate = FALSE; goto done; } ip6h = (struct ip6_hdr *)(void *)(eh + 1); bcopy(&ip6h->ip6_src, &saddr, sizeof(saddr)); if (mnr != NULL && ip6h->ip6_nxt == IPPROTO_ICMPV6) { bridge_mac_nat_icmpv6_output(sc, bif, data, eh, ip6h, &saddr, mnr); } if (IN6_IS_ADDR_UNSPECIFIED(&saddr)) { goto done; } (void)bridge_update_mac_nat_entry(sc, bif, AF_INET6, &saddr, eh->ether_shost); done: return translate; } /* * bridge_mac_nat_input: * Process a packet arriving on the MAC NAT interface (sc_mac_nat_bif). * This interface is the "external" interface with respect to NAT. * The interface is only capable of receiving a single MAC address * (e.g. a Wi-Fi STA interface). * * When a packet arrives on the external interface, look up the destination * IP address in the mac_nat_entry table. If there is a match, *is_input * is set to TRUE if it's for the MAC NAT interface, otherwise *is_input * is set to FALSE and translate the MAC address if necessary. * * Returns: * The internal interface to direct the packet to, or NULL if the packet * should not be redirected. * * *data may be updated to point at a different mbuf chain, or set to NULL * if the chain was deallocated during processing. */ static ifnet_t bridge_mac_nat_input(struct bridge_softc *sc, mbuf_t *data, boolean_t *is_input) { ifnet_t dst_if = NULL; struct ether_header *eh; uint16_t ether_type; boolean_t is_unicast; mbuf_t m = *data; struct mac_nat_entry *mne = NULL; BRIDGE_LOCK_ASSERT_HELD(sc); *is_input = FALSE; assert(sc->sc_mac_nat_bif != NULL); is_unicast = ((m->m_flags & (M_BCAST | M_MCAST)) == 0); eh = mtod(m, struct ether_header *); ether_type = ntohs(eh->ether_type); switch (ether_type) { case ETHERTYPE_ARP: mne = bridge_mac_nat_arp_input(sc, data); break; case ETHERTYPE_IP: if (is_unicast) { mne = bridge_mac_nat_ip_input(sc, data); } break; case ETHERTYPE_IPV6: if (is_unicast) { mne = bridge_mac_nat_ipv6_input(sc, data); } break; default: break; } if (mne != NULL) { if (is_unicast) { if (m != *data) { /* it may have changed */ eh = mtod(*data, struct ether_header *); } bcopy(mne->mne_mac, eh->ether_dhost, sizeof(eh->ether_dhost)); } dst_if = mne->mne_bif->bif_ifp; *is_input = (mne->mne_bif == sc->sc_mac_nat_bif); } return dst_if; } /* * bridge_mac_nat_output: * Process a packet destined to the MAC NAT interface (sc_mac_nat_bif) * from the interface 'bif'. * * Create a mac_nat_entry containing the source IP address and MAC address * from the packet. Populate a mac_nat_record with information detailing * how to translate the packet. Translation takes place later when * the bridge lock is no longer held. * * If 'bif' == sc_mac_nat_bif, the stack over the MAC NAT * interface is generating an output packet. No translation is required in this * case, we just record the IP address used to prevent another bif from * claiming our IP address. * * Returns: * TRUE if the packet should be translated (*mnr updated as well), * FALSE otherwise. * * *data may be updated to point at a different mbuf chain or NULL if * the chain was deallocated during processing. */ static boolean_t bridge_mac_nat_output(struct bridge_softc *sc, struct bridge_iflist *bif, mbuf_t *data, struct mac_nat_record *mnr) { struct ether_header *eh; uint16_t ether_type; boolean_t translate = FALSE; BRIDGE_LOCK_ASSERT_HELD(sc); assert(sc->sc_mac_nat_bif != NULL); eh = mtod(*data, struct ether_header *); ether_type = ntohs(eh->ether_type); if (mnr != NULL) { bzero(mnr, sizeof(*mnr)); mnr->mnr_ether_type = ether_type; } switch (ether_type) { case ETHERTYPE_ARP: translate = bridge_mac_nat_arp_output(sc, bif, data, mnr); break; case ETHERTYPE_IP: translate = bridge_mac_nat_ip_output(sc, bif, data, mnr); break; case ETHERTYPE_IPV6: translate = bridge_mac_nat_ipv6_output(sc, bif, data, mnr); break; default: break; } return translate; } static void bridge_mac_nat_arp_translate(mbuf_t *data, struct mac_nat_record *mnr, const caddr_t eaddr) { errno_t error; if (mnr->mnr_arp_offset == 0) { return; } /* replace the source hardware address */ error = mbuf_copyback(*data, mnr->mnr_arp_offset, ETHER_ADDR_LEN, eaddr, MBUF_DONTWAIT); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT, "mbuf_copyback failed"); m_freem(*data); *data = NULL; } return; } static void bridge_mac_nat_ip_translate(mbuf_t *data, struct mac_nat_record *mnr) { errno_t error; size_t offset; if (mnr->mnr_ip_header_len == 0) { return; } /* update the UDP checksum */ offset = sizeof(struct ether_header) + mnr->mnr_ip_header_len; error = mbuf_copyback(*data, offset + offsetof(struct udphdr, uh_sum), sizeof(mnr->mnr_ip_udp_csum), &mnr->mnr_ip_udp_csum, MBUF_DONTWAIT); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT, "mbuf_copyback uh_sum failed"); m_freem(*data); *data = NULL; } /* update the DHCP must broadcast flag */ offset += sizeof(struct udphdr); error = mbuf_copyback(*data, offset + offsetof(struct dhcp, dp_flags), sizeof(mnr->mnr_ip_dhcp_flags), &mnr->mnr_ip_dhcp_flags, MBUF_DONTWAIT); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT, "mbuf_copyback dp_flags failed"); m_freem(*data); *data = NULL; } } static void bridge_mac_nat_ipv6_translate(mbuf_t *data, struct mac_nat_record *mnr, const caddr_t eaddr) { uint16_t cksum; errno_t error; mbuf_t m = *data; if (mnr->mnr_ip6_header_len == 0) { return; } switch (mnr->mnr_ip6_icmp6_type) { case ND_ROUTER_SOLICIT: case ND_NEIGHBOR_SOLICIT: case ND_NEIGHBOR_ADVERT: if (mnr->mnr_ip6_lladdr_offset == 0) { /* nothing to do */ return; } break; default: return; } /* * replace the lladdr */ error = mbuf_copyback(m, mnr->mnr_ip6_lladdr_offset, ETHER_ADDR_LEN, eaddr, MBUF_DONTWAIT); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT, "mbuf_copyback lladdr failed"); m_freem(m); *data = NULL; return; } /* * recompute the icmp6 checksum */ /* skip past the ethernet header */ mbuf_setdata(m, (char *)mbuf_data(m) + ETHER_HDR_LEN, mbuf_len(m) - ETHER_HDR_LEN); mbuf_pkthdr_adjustlen(m, -ETHER_HDR_LEN); #define CKSUM_OFFSET_ICMP6 offsetof(struct icmp6_hdr, icmp6_cksum) /* set the checksum to zero */ cksum = 0; error = mbuf_copyback(m, mnr->mnr_ip6_header_len + CKSUM_OFFSET_ICMP6, sizeof(cksum), &cksum, MBUF_DONTWAIT); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT, "mbuf_copyback cksum=0 failed"); m_freem(m); *data = NULL; return; } /* compute and set the new checksum */ cksum = in6_cksum(m, IPPROTO_ICMPV6, mnr->mnr_ip6_header_len, mnr->mnr_ip6_icmp6_len); error = mbuf_copyback(m, mnr->mnr_ip6_header_len + CKSUM_OFFSET_ICMP6, sizeof(cksum), &cksum, MBUF_DONTWAIT); if (error != 0) { BRIDGE_LOG(LOG_NOTICE, BR_DBGF_MAC_NAT, "mbuf_copyback cksum failed"); m_freem(m); *data = NULL; return; } /* restore the ethernet header */ mbuf_setdata(m, (char *)mbuf_data(m) - ETHER_HDR_LEN, mbuf_len(m) + ETHER_HDR_LEN); mbuf_pkthdr_adjustlen(m, ETHER_HDR_LEN); return; } static void bridge_mac_nat_translate(mbuf_t *data, struct mac_nat_record *mnr, const caddr_t eaddr) { struct ether_header *eh; /* replace the source ethernet address with the single MAC */ eh = mtod(*data, struct ether_header *); bcopy(eaddr, eh->ether_shost, sizeof(eh->ether_shost)); switch (mnr->mnr_ether_type) { case ETHERTYPE_ARP: bridge_mac_nat_arp_translate(data, mnr, eaddr); break; case ETHERTYPE_IP: bridge_mac_nat_ip_translate(data, mnr); break; case ETHERTYPE_IPV6: bridge_mac_nat_ipv6_translate(data, mnr, eaddr); break; default: break; } return; } /* * bridge packet filtering */ /* * Perform basic checks on header size since * pfil assumes ip_input has already processed * it for it. Cut-and-pasted from ip_input.c. * Given how simple the IPv6 version is, * does the IPv4 version really need to be * this complicated? * * XXX Should we update ipstat here, or not? * XXX Right now we update ipstat but not * XXX csum_counter. */ static int bridge_ip_checkbasic(struct mbuf **mp) { struct mbuf *m = *mp; struct ip *ip; int len, hlen; u_short sum; if (*mp == NULL) { return -1; } if (IP_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { /* max_linkhdr is already rounded up to nearest 4-byte */ if ((m = m_copyup(m, sizeof(struct ip), max_linkhdr)) == NULL) { /* XXXJRT new stat, please */ ipstat.ips_toosmall++; goto bad; } } else if (OS_EXPECT((size_t)m->m_len < sizeof(struct ip), 0)) { if ((m = m_pullup(m, sizeof(struct ip))) == NULL) { ipstat.ips_toosmall++; goto bad; } } ip = mtod(m, struct ip *); if (ip == NULL) { goto bad; } if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { ipstat.ips_badvers++; goto bad; } hlen = IP_VHL_HL(ip->ip_vhl) << 2; if (hlen < (int)sizeof(struct ip)) { /* minimum header length */ ipstat.ips_badhlen++; goto bad; } if (hlen > m->m_len) { if ((m = m_pullup(m, hlen)) == 0) { ipstat.ips_badhlen++; goto bad; } ip = mtod(m, struct ip *); if (ip == NULL) { goto bad; } } if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); } else { if (hlen == sizeof(struct ip)) { sum = in_cksum_hdr(ip); } else { sum = in_cksum(m, hlen); } } if (sum) { ipstat.ips_badsum++; goto bad; } /* Retrieve the packet length. */ len = ntohs(ip->ip_len); /* * Check for additional length bogosity */ if (len < hlen) { ipstat.ips_badlen++; goto bad; } /* * Check that the amount of data in the buffers * is as at least much as the IP header would have us expect. * Drop packet if shorter than we expect. */ if (m->m_pkthdr.len < len) { ipstat.ips_tooshort++; goto bad; } /* Checks out, proceed */ *mp = m; return 0; bad: *mp = m; return -1; } /* * Same as above, but for IPv6. * Cut-and-pasted from ip6_input.c. * XXX Should we update ip6stat, or not? */ static int bridge_ip6_checkbasic(struct mbuf **mp) { struct mbuf *m = *mp; struct ip6_hdr *ip6; /* * If the IPv6 header is not aligned, slurp it up into a new * mbuf with space for link headers, in the event we forward * it. Otherwise, if it is aligned, make sure the entire base * IPv6 header is in the first mbuf of the chain. */ if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) { struct ifnet *inifp = m->m_pkthdr.rcvif; /* max_linkhdr is already rounded up to nearest 4-byte */ if ((m = m_copyup(m, sizeof(struct ip6_hdr), max_linkhdr)) == NULL) { /* XXXJRT new stat, please */ ip6stat.ip6s_toosmall++; in6_ifstat_inc(inifp, ifs6_in_hdrerr); goto bad; } } else if (OS_EXPECT((size_t)m->m_len < sizeof(struct ip6_hdr), 0)) { struct ifnet *inifp = m->m_pkthdr.rcvif; if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) { ip6stat.ip6s_toosmall++; in6_ifstat_inc(inifp, ifs6_in_hdrerr); goto bad; } } ip6 = mtod(m, struct ip6_hdr *); if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { ip6stat.ip6s_badvers++; in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); goto bad; } /* Checks out, proceed */ *mp = m; return 0; bad: *mp = m; return -1; } /* * the PF routines expect to be called from ip_input, so we * need to do and undo here some of the same processing. * * XXX : this is heavily inspired on bridge_pfil() */ static int bridge_pf(struct mbuf **mp, struct ifnet *ifp, uint32_t sc_filter_flags, int input) { /* * XXX : mpetit : heavily inspired by bridge_pfil() */ int snap, error, i, hlen; struct ether_header *eh1, eh2; struct ip *ip; struct llc llc1; u_int16_t ether_type; snap = 0; error = -1; /* Default error if not error == 0 */ if ((sc_filter_flags & IFBF_FILT_MEMBER) == 0) { return 0; /* filtering is disabled */ } i = min((*mp)->m_pkthdr.len, max_protohdr); if ((*mp)->m_len < i) { *mp = m_pullup(*mp, i); if (*mp == NULL) { BRIDGE_LOG(LOG_NOTICE, 0, "m_pullup failed"); return -1; } } eh1 = mtod(*mp, struct ether_header *); ether_type = ntohs(eh1->ether_type); /* * Check for SNAP/LLC. */ if (ether_type < ETHERMTU) { struct llc *llc2 = (struct llc *)(eh1 + 1); if ((*mp)->m_len >= ETHER_HDR_LEN + 8 && llc2->llc_dsap == LLC_SNAP_LSAP && llc2->llc_ssap == LLC_SNAP_LSAP && llc2->llc_control == LLC_UI) { ether_type = htons(llc2->llc_un.type_snap.ether_type); snap = 1; } } /* * If we're trying to filter bridge traffic, don't look at anything * other than IP and ARP traffic. If the filter doesn't understand * IPv6, don't allow IPv6 through the bridge either. This is lame * since if we really wanted, say, an AppleTalk filter, we are hosed, * but of course we don't have an AppleTalk filter to begin with. * (Note that since pfil doesn't understand ARP it will pass *ALL* * ARP traffic.) */ switch (ether_type) { case ETHERTYPE_ARP: case ETHERTYPE_REVARP: return 0; /* Automatically pass */ case ETHERTYPE_IP: case ETHERTYPE_IPV6: break; default: /* * Check to see if the user wants to pass non-ip * packets, these will not be checked by pf and * passed unconditionally so the default is to drop. */ if ((sc_filter_flags & IFBF_FILT_ONLYIP)) { goto bad; } break; } /* Strip off the Ethernet header and keep a copy. */ m_copydata(*mp, 0, ETHER_HDR_LEN, (caddr_t)&eh2); m_adj(*mp, ETHER_HDR_LEN); /* Strip off snap header, if present */ if (snap) { m_copydata(*mp, 0, sizeof(struct llc), (caddr_t)&llc1); m_adj(*mp, sizeof(struct llc)); } /* * Check the IP header for alignment and errors */ switch (ether_type) { case ETHERTYPE_IP: error = bridge_ip_checkbasic(mp); break; case ETHERTYPE_IPV6: error = bridge_ip6_checkbasic(mp); break; default: error = 0; break; } if (error) { goto bad; } error = 0; /* * Run the packet through pf rules */ switch (ether_type) { case ETHERTYPE_IP: /* * before calling the firewall, swap fields the same as * IP does. here we assume the header is contiguous */ ip = mtod(*mp, struct ip *); ip->ip_len = ntohs(ip->ip_len); ip->ip_off = ntohs(ip->ip_off); if (ifp != NULL) { error = pf_af_hook(ifp, 0, mp, AF_INET, input, NULL); } if (*mp == NULL || error != 0) { /* filter may consume */ break; } /* Recalculate the ip checksum and restore byte ordering */ ip = mtod(*mp, struct ip *); hlen = IP_VHL_HL(ip->ip_vhl) << 2; if (hlen < (int)sizeof(struct ip)) { goto bad; } if (hlen > (*mp)->m_len) { if ((*mp = m_pullup(*mp, hlen)) == 0) { goto bad; } ip = mtod(*mp, struct ip *); if (ip == NULL) { goto bad; } } ip->ip_len = htons(ip->ip_len); ip->ip_off = htons(ip->ip_off); ip->ip_sum = 0; if (hlen == sizeof(struct ip)) { ip->ip_sum = in_cksum_hdr(ip); } else { ip->ip_sum = in_cksum(*mp, hlen); } break; case ETHERTYPE_IPV6: if (ifp != NULL) { error = pf_af_hook(ifp, 0, mp, AF_INET6, input, NULL); } if (*mp == NULL || error != 0) { /* filter may consume */ break; } break; default: error = 0; break; } if (*mp == NULL) { return error; } if (error != 0) { goto bad; } error = -1; /* * Finally, put everything back the way it was and return */ if (snap) { M_PREPEND(*mp, sizeof(struct llc), M_DONTWAIT, 0); if (*mp == NULL) { return error; } bcopy(&llc1, mtod(*mp, caddr_t), sizeof(struct llc)); } M_PREPEND(*mp, ETHER_HDR_LEN, M_DONTWAIT, 0); if (*mp == NULL) { return error; } bcopy(&eh2, mtod(*mp, caddr_t), ETHER_HDR_LEN); return 0; bad: m_freem(*mp); *mp = NULL; return error; } /* * Copyright (C) 2014, Stefano Garzarella - Universita` di Pisa. * 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * XXX-ste: Maybe this function must be moved into kern/uipc_mbuf.c * * Create a queue of packets/segments which fit the given mss + hdr_len. * m0 points to mbuf chain to be segmented. * This function splits the payload (m0-> m_pkthdr.len - hdr_len) * into segments of length MSS bytes and then copy the first hdr_len bytes * from m0 at the top of each segment. * If hdr2_buf is not NULL (hdr2_len is the buf length), it is copied * in each segment after the first hdr_len bytes * * Return the new queue with the segments on success, NULL on failure. * (the mbuf queue is freed in this case). * nsegs contains the number of segments generated. */ static struct mbuf * m_seg(struct mbuf *m0, int hdr_len, int mss, int *nsegs, char * hdr2_buf, int hdr2_len) { int off = 0, n, firstlen; struct mbuf **mnext, *mseg; int total_len = m0->m_pkthdr.len; /* * Segmentation useless */ if (total_len <= hdr_len + mss) { return m0; } if (hdr2_buf == NULL || hdr2_len <= 0) { hdr2_buf = NULL; hdr2_len = 0; } off = hdr_len + mss; firstlen = mss; /* first segment stored in the original mbuf */ mnext = &(m0->m_nextpkt); /* pointer to next packet */ for (n = 1; off < total_len; off += mss, n++) { struct mbuf *m; /* * Copy the header from the original packet * and create a new mbuf chain */ if (MHLEN < hdr_len) { m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); } else { m = m_gethdr(M_NOWAIT, MT_DATA); } if (m == NULL) { #ifdef GSO_DEBUG D("MGETHDR error\n"); #endif goto err; } m_copydata(m0, 0, hdr_len, mtod(m, caddr_t)); m->m_len = hdr_len; /* * if the optional header is present, copy it */ if (hdr2_buf != NULL) { m_copyback(m, hdr_len, hdr2_len, hdr2_buf); } m->m_flags |= (m0->m_flags & M_COPYFLAGS); if (off + mss >= total_len) { /* last segment */ mss = total_len - off; } /* * Copy the payload from original packet */ mseg = m_copym(m0, off, mss, M_NOWAIT); if (mseg == NULL) { m_freem(m); #ifdef GSO_DEBUG D("m_copym error\n"); #endif goto err; } m_cat(m, mseg); m->m_pkthdr.len = hdr_len + hdr2_len + mss; m->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; /* * Copy the checksum flags and data (in_cksum() need this) */ m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; m->m_pkthdr.csum_data = m0->m_pkthdr.csum_data; m->m_pkthdr.tso_segsz = m0->m_pkthdr.tso_segsz; *mnext = m; mnext = &(m->m_nextpkt); } /* * Update first segment. * If the optional header is present, is necessary * to insert it into the first segment. */ if (hdr2_buf == NULL) { m_adj(m0, hdr_len + firstlen - total_len); m0->m_pkthdr.len = hdr_len + firstlen; } else { mseg = m_copym(m0, hdr_len, firstlen, M_NOWAIT); if (mseg == NULL) { #ifdef GSO_DEBUG D("m_copym error\n"); #endif goto err; } m_adj(m0, hdr_len - total_len); m_copyback(m0, hdr_len, hdr2_len, hdr2_buf); m_cat(m0, mseg); m0->m_pkthdr.len = hdr_len + hdr2_len + firstlen; } if (nsegs != NULL) { *nsegs = n; } return m0; err: while (m0 != NULL) { mseg = m0->m_nextpkt; m0->m_nextpkt = NULL; m_freem(m0); m0 = mseg; } return NULL; } /* * Wrappers of IPv4 checksum functions */ static inline void gso_ipv4_data_cksum(struct mbuf *m, struct ip *ip, int mac_hlen) { m->m_data += mac_hlen; m->m_len -= mac_hlen; m->m_pkthdr.len -= mac_hlen; #if __FreeBSD_version < 1000000 ip->ip_len = ntohs(ip->ip_len); /* needed for in_delayed_cksum() */ #endif in_delayed_cksum(m); #if __FreeBSD_version < 1000000 ip->ip_len = htons(ip->ip_len); #endif m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; m->m_len += mac_hlen; m->m_pkthdr.len += mac_hlen; m->m_data -= mac_hlen; } static inline void gso_ipv4_hdr_cksum(struct mbuf *m, struct ip *ip, int mac_hlen, int ip_hlen) { m->m_data += mac_hlen; ip->ip_sum = in_cksum(m, ip_hlen); m->m_pkthdr.csum_flags &= ~CSUM_IP; m->m_data -= mac_hlen; } /* * Structure that contains the state during the TCP segmentation */ struct gso_ip_tcp_state { void (*update) (struct gso_ip_tcp_state*, struct mbuf*); void (*internal) (struct gso_ip_tcp_state*, struct mbuf*); union iphdr hdr; struct tcphdr *tcp; int mac_hlen; int ip_hlen; int tcp_hlen; int hlen; int pay_len; int sw_csum; uint32_t tcp_seq; uint16_t ip_id; boolean_t is_tx; }; /* * Update the pointers to TCP and IPv4 headers */ static inline void gso_ipv4_tcp_update(struct gso_ip_tcp_state *state, struct mbuf *m) { state->hdr.ip = (struct ip *)(void *)(mtod(m, uint8_t *) + state->mac_hlen); state->tcp = (struct tcphdr *)(void *)((caddr_t)(state->hdr.ip) + state->ip_hlen); state->pay_len = m->m_pkthdr.len - state->hlen; } /* * Set properly the TCP and IPv4 headers */ static inline void gso_ipv4_tcp_internal(struct gso_ip_tcp_state *state, struct mbuf *m) { /* * Update IP header */ state->hdr.ip->ip_id = htons((state->ip_id)++); state->hdr.ip->ip_len = htons(m->m_pkthdr.len - state->mac_hlen); /* * TCP Checksum */ state->tcp->th_sum = 0; state->tcp->th_sum = in_pseudo(state->hdr.ip->ip_src.s_addr, state->hdr.ip->ip_dst.s_addr, htons(state->tcp_hlen + IPPROTO_TCP + state->pay_len)); /* * Checksum HW not supported (TCP) */ if (state->sw_csum & CSUM_DELAY_DATA) { gso_ipv4_data_cksum(m, state->hdr.ip, state->mac_hlen); } state->tcp_seq += state->pay_len; /* * IP Checksum */ state->hdr.ip->ip_sum = 0; /* * Checksum HW not supported (IP) */ if (state->sw_csum & CSUM_IP) { gso_ipv4_hdr_cksum(m, state->hdr.ip, state->mac_hlen, state->ip_hlen); } } /* * Updates the pointers to TCP and IPv6 headers */ static inline void gso_ipv6_tcp_update(struct gso_ip_tcp_state *state, struct mbuf *m) { state->hdr.ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + state->mac_hlen); state->tcp = (struct tcphdr *)(void *)((caddr_t)(state->hdr.ip6) + state->ip_hlen); state->pay_len = m->m_pkthdr.len - state->hlen; } /* * Sets properly the TCP and IPv6 headers */ static inline void gso_ipv6_tcp_internal(struct gso_ip_tcp_state *state, struct mbuf *m) { state->hdr.ip6->ip6_plen = htons(m->m_pkthdr.len - state->mac_hlen - state->ip_hlen); /* * TCP Checksum */ state->tcp->th_sum = 0; state->tcp->th_sum = in6_pseudo(&state->hdr.ip6->ip6_src, &state->hdr.ip6->ip6_dst, htonl(state->tcp_hlen + state->pay_len + IPPROTO_TCP)); /* * Checksum HW not supported (TCP) */ if (state->sw_csum & CSUM_DELAY_IPV6_DATA) { (void)in6_finalize_cksum(m, state->mac_hlen, -1, -1, state->sw_csum); m->m_pkthdr.csum_flags &= ~CSUM_DELAY_IPV6_DATA; } state->tcp_seq += state->pay_len; } /* * Init the state during the TCP segmentation */ static void gso_ip_tcp_init_state(struct gso_ip_tcp_state *state, struct ifnet *ifp, bool is_ipv4, int mac_hlen, int ip_hlen, void * ip_hdr, struct tcphdr * tcp_hdr) { #pragma unused(ifp) state->hdr.ptr = ip_hdr; state->tcp = tcp_hdr; if (is_ipv4) { state->ip_id = ntohs(state->hdr.ip->ip_id); state->update = gso_ipv4_tcp_update; state->internal = gso_ipv4_tcp_internal; state->sw_csum = CSUM_DELAY_DATA | CSUM_IP; /* XXX */ } else { state->update = gso_ipv6_tcp_update; state->internal = gso_ipv6_tcp_internal; state->sw_csum = CSUM_DELAY_IPV6_DATA; /* XXX */ } state->mac_hlen = mac_hlen; state->ip_hlen = ip_hlen; state->tcp_hlen = state->tcp->th_off << 2; state->hlen = mac_hlen + ip_hlen + state->tcp_hlen; state->tcp_seq = ntohl(state->tcp->th_seq); //state->sw_csum = m->m_pkthdr.csum_flags & ~IF_HWASSIST_CSUM_FLAGS(ifp->if_hwassist); return; } /* * GSO on TCP/IP (v4 or v6) * * If is_tx is TRUE, segmented packets are transmitted after they are * segmented. * * If is_tx is FALSE, the segmented packets are returned as a chain in *mp. */ static int gso_ip_tcp(struct ifnet *ifp, struct mbuf **mp, struct gso_ip_tcp_state *state, boolean_t is_tx) { struct mbuf *m, *m_tx; int error = 0; int mss = 0; int nsegs = 0; struct mbuf *m0 = *mp; #ifdef GSO_STATS int total_len = m0->m_pkthdr.len; #endif /* GSO_STATS */ #if 1 u_int reduce_mss; reduce_mss = is_tx ? if_bridge_tso_reduce_mss_tx : if_bridge_tso_reduce_mss_forwarding; mss = ifp->if_mtu - state->ip_hlen - state->tcp_hlen - reduce_mss; assert(mss > 0); #else if (m0->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) {/* TSO with GSO */ mss = ifp->if_hw_tsomax - state->ip_hlen - state->tcp_hlen; } else { mss = m0->m_pkthdr.tso_segsz; } #endif *mp = m0 = m_seg(m0, state->hlen, mss, &nsegs, 0, 0); if (m0 == NULL) { return ENOBUFS; /* XXX ok? */ } BRIDGE_LOG(LOG_DEBUG, BR_DBGF_CHECKSUM, "%s %s mss %d nsegs %d", ifp->if_xname, is_tx ? "TX" : "RX", mss, nsegs); /* * XXX-ste: can this happen? */ if (m0->m_nextpkt == NULL) { #ifdef GSO_DEBUG D("only 1 segment"); #endif if (is_tx) { error = bridge_transmit(ifp, m0); } return error; } #ifdef GSO_STATS GSOSTAT_SET_MAX(tcp.gsos_max_mss, mss); GSOSTAT_SET_MIN(tcp.gsos_min_mss, mss); GSOSTAT_ADD(tcp.gsos_osegments, nsegs); #endif /* GSO_STATS */ /* first pkt */ m = m0; state->update(state, m); do { state->tcp->th_flags &= ~(TH_FIN | TH_PUSH); state->internal(state, m); m_tx = m; m = m->m_nextpkt; if (is_tx) { m_tx->m_nextpkt = NULL; if ((error = bridge_transmit(ifp, m_tx)) != 0) { /* * XXX: If a segment can not be sent, discard the following * segments and propagate the error to the upper levels. * In this way the TCP retransmits all the initial packet. */ #ifdef GSO_DEBUG D("if_transmit error\n"); #endif goto err; } } state->update(state, m); state->tcp->th_flags &= ~TH_CWR; state->tcp->th_seq = htonl(state->tcp_seq); } while (m->m_nextpkt); /* last pkt */ state->internal(state, m); if (is_tx) { error = bridge_transmit(ifp, m); #ifdef GSO_DEBUG if (error) { D("last if_transmit error\n"); D("error - type = %d \n", error); } #endif } #ifdef GSO_STATS if (!error) { GSOSTAT_INC(tcp.gsos_segmented); GSOSTAT_SET_MAX(tcp.gsos_maxsegmented, total_len); GSOSTAT_SET_MIN(tcp.gsos_minsegmented, total_len); GSOSTAT_ADD(tcp.gsos_totalbyteseg, total_len); } #endif /* GSO_STATS */ return error; err: #ifdef GSO_DEBUG D("error - type = %d \n", error); #endif while (m != NULL) { m_tx = m->m_nextpkt; m->m_nextpkt = NULL; m_freem(m); m = m_tx; } return error; } /* * GSO for TCP/IPv[46] */ static int gso_tcp(struct ifnet *ifp, struct mbuf **mp, u_int mac_hlen, bool is_ipv4, boolean_t is_tx) { int error; ip_packet_info info; uint32_t csum_flags; struct gso_ip_tcp_state state; struct bripstats stats; /* XXX ignored */ struct tcphdr *tcp; if (!is_tx && ipforwarding == 0) { /* no need to segment if the packet will not be forwarded */ return 0; } error = bridge_get_tcp_header(mp, mac_hlen, is_ipv4, &info, &stats); if (error != 0) { if (*mp != NULL) { m_freem(*mp); *mp = NULL; } return error; } if (info.ip_proto_hdr == NULL) { /* not a TCP packet */ return 0; } tcp = (struct tcphdr *)(void *)info.ip_proto_hdr; gso_ip_tcp_init_state(&state, ifp, is_ipv4, mac_hlen, info.ip_hlen, info.ip_hdr.ptr, tcp); if (is_ipv4) { csum_flags = CSUM_DELAY_DATA; /* XXX */ if (!is_tx) { /* if RX to our local IP address, don't segment */ struct in_addr dst_ip; bcopy(&state.hdr.ip->ip_dst, &dst_ip, sizeof(dst_ip)); if (in_addr_is_ours(dst_ip)) { return 0; } } } else { csum_flags = CSUM_DELAY_IPV6_DATA; /* XXX */ if (!is_tx) { /* if RX to our local IP address, don't segment */ if (in6_addr_is_ours(&state.hdr.ip6->ip6_dst, ifp->if_index)) { /* local IP address, no need to segment */ return 0; } } } (*mp)->m_pkthdr.csum_flags = csum_flags; (*mp)->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); return gso_ip_tcp(ifp, mp, &state, is_tx); } |