sys/netinet/ip_carp.c
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 | /* * Copyright (c) 2002 Michael Shalayeff. All rights reserved. * Copyright (c) 2003 Ryan McBride. 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 OR HIS RELATIVES 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 MIND, 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. */ /* * $FreeBSD: src/sys/netinet/ip_carp.c,v 1.48 2007/02/02 09:39:09 glebius Exp $ */ #include "opt_carp.h" #include "opt_inet.h" #include "opt_inet6.h" #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/in_cksum.h> #include <sys/limits.h> #include <sys/malloc.h> #include <sys/mbuf.h> #include <sys/msgport2.h> #include <sys/time.h> #include <sys/proc.h> #include <sys/caps.h> #include <sys/sockio.h> #include <sys/socket.h> #include <sys/sysctl.h> #include <sys/syslog.h> #include <sys/thread.h> #include <machine/stdarg.h> #include <crypto/sha1.h> #include <net/bpf.h> #include <net/ethernet.h> #include <net/if.h> #include <net/if_dl.h> #include <net/if_types.h> #include <net/route.h> #include <net/if_clone.h> #include <net/if_var.h> #include <net/ifq_var.h> #include <net/netmsg2.h> #include <net/netisr2.h> #ifdef INET #include <netinet/in.h> #include <netinet/in_var.h> #include <netinet/in_systm.h> #include <netinet/ip.h> #include <netinet/ip_var.h> #include <netinet/if_ether.h> #endif #ifdef INET6 #include <netinet/icmp6.h> #include <netinet/ip6.h> #include <netinet6/ip6_var.h> #include <netinet6/scope6_var.h> #include <netinet6/nd6.h> #endif #include <netinet/ip_carp.h> /* * Note about carp's MP safe approach: * * Brief: carp_softc (softc), carp_softc_container (scc) * * - All configuration operation, e.g. ioctl, add/delete inet addresses * is serialized by netisr0; not by carp's serializer * * - Backing interface's if_carp and carp_softc's relationship: * * +---------+ * if_carp -->| carp_if | * +---------+ * | * | * V +---------+ * +-----+ | | * | scc |-->| softc | * +-----+ | | * | +---------+ * | * V +---------+ * +-----+ | | * | scc |-->| softc | * +-----+ | | * +---------+ * * - if_carp creation, modification and deletion all happen in netisr0, * as stated previously. Since if_carp is accessed by multiple netisrs, * the modification to if_carp is conducted in the following way: * * Adding carp_softc: * * 1) Duplicate the old carp_if to new carp_if (ncif), and insert the * to-be-added carp_softc to the new carp_if (ncif): * * if_carp ncif * | | * V V * +---------+ +---------+ * | carp_if | | carp_if | * +---------+ +---------+ * | | * | | * V +-------+ V * +-----+ | | +-----+ * | scc |---->| softc |<----| scc | * +-----+ | | +-----+ * | +-------+ | * | | * V +-------+ V * +-----+ | | +-----+ * | scc |---->| softc |<----| scc | * +-----+ | | +-----+ * +-------+ | * | * +-------+ V * | | +-----+ * | softc |<----| scc | * | | +-----+ * +-------+ * * 2) Switch save if_carp into ocif and switch if_carp to ncif: * * ocif if_carp * | | * V V * +---------+ +---------+ * | carp_if | | carp_if | * +---------+ +---------+ * | | * | | * V +-------+ V * +-----+ | | +-----+ * | scc |---->| softc |<----| scc | * +-----+ | | +-----+ * | +-------+ | * | | * V +-------+ V * +-----+ | | +-----+ * | scc |---->| softc |<----| scc | * +-----+ | | +-----+ * +-------+ | * | * +-------+ V * | | +-----+ * | softc |<----| scc | * | | +-----+ * +-------+ * * 3) Run netmsg_service_sync(), which will make sure that * ocif is no longer accessed (all network operations * are happened only in network threads). * 4) Free ocif -- only carp_if and scc are freed. * * * Removing carp_softc: * * 1) Duplicate the old carp_if to new carp_if (ncif); the to-be-deleted * carp_softc will not be duplicated. * * if_carp ncif * | | * V V * +---------+ +---------+ * | carp_if | | carp_if | * +---------+ +---------+ * | | * | | * V +-------+ V * +-----+ | | +-----+ * | scc |---->| softc |<----| scc | * +-----+ | | +-----+ * | +-------+ | * | | * V +-------+ | * +-----+ | | | * | scc |---->| softc | | * +-----+ | | | * | +-------+ | * | | * V +-------+ V * +-----+ | | +-----+ * | scc |---->| softc |<----| scc | * +-----+ | | +-----+ * +-------+ * * 2) Switch save if_carp into ocif and switch if_carp to ncif: * * ocif if_carp * | | * V V * +---------+ +---------+ * | carp_if | | carp_if | * +---------+ +---------+ * | | * | | * V +-------+ V * +-----+ | | +-----+ * | scc |---->| softc |<----| scc | * +-----+ | | +-----+ * | +-------+ | * | | * V +-------+ | * +-----+ | | | * | scc |---->| softc | | * +-----+ | | | * | +-------+ | * | | * V +-------+ V * +-----+ | | +-----+ * | scc |---->| softc |<----| scc | * +-----+ | | +-----+ * +-------+ * * 3) Run netmsg_service_sync(), which will make sure that * ocif is no longer accessed (all network operations * are happened only in network threads). * 4) Free ocif -- only carp_if and scc are freed. * * - if_carp accessing: * The accessing code should cache the if_carp in a local temporary * variable and accessing the temporary variable along the code path * instead of accessing if_carp later on. */ #define CARP_IFNAME "carp" #define CARP_IS_RUNNING(ifp) \ (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) struct carp_softc; struct carp_vhaddr { uint32_t vha_flags; /* CARP_VHAF_ */ struct in_ifaddr *vha_ia; /* carp address */ struct in_ifaddr *vha_iaback; /* backing address */ TAILQ_ENTRY(carp_vhaddr) vha_link; }; TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr); struct netmsg_carp { struct netmsg_base base; struct ifnet *nc_carpdev; struct carp_softc *nc_softc; void *nc_data; size_t nc_datalen; }; struct carp_softc { struct arpcom arpcom; struct ifnet *sc_carpdev; /* parent interface */ struct carp_vhaddr_list sc_vha_list; /* virtual addr list */ const struct in_ifaddr *sc_ia; /* primary iface address v4 */ struct ip_moptions sc_imo; #ifdef INET6 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */ struct ip6_moptions sc_im6o; #endif /* INET6 */ enum { INIT = 0, BACKUP, MASTER } sc_state; boolean_t sc_dead; int sc_suppress; int sc_sendad_errors; #define CARP_SENDAD_MAX_ERRORS 3 int sc_sendad_success; #define CARP_SENDAD_MIN_SUCCESS 3 int sc_vhid; int sc_advskew; int sc_naddrs; /* actually used IPv4 vha */ int sc_naddrs6; int sc_advbase; /* seconds */ int sc_init_counter; uint64_t sc_counter; /* authentication */ #define CARP_HMAC_PAD 64 unsigned char sc_key[CARP_KEY_LEN]; unsigned char sc_pad[CARP_HMAC_PAD]; SHA1_CTX sc_sha1; struct callout sc_ad_tmo; /* advertisement timeout */ struct netmsg_carp sc_ad_msg; /* adv timeout netmsg */ struct callout sc_md_tmo; /* ip4 master down timeout */ struct callout sc_md6_tmo; /* ip6 master down timeout */ struct netmsg_carp sc_md_msg; /* master down timeout netmsg */ LIST_ENTRY(carp_softc) sc_next; /* Interface clue */ }; #define sc_if arpcom.ac_if struct carp_softc_container { TAILQ_ENTRY(carp_softc_container) scc_link; struct carp_softc *scc_softc; }; TAILQ_HEAD(carp_if, carp_softc_container); SYSCTL_DECL(_net_inet_carp); static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0, 1 }; /* XXX for now */ SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW, &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets"); SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW, &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode"); SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW, &carp_opts[CARPCTL_LOG], 0, "log bad carp packets"); SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW, &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses"); SYSCTL_INT(_net_inet_carp, CARPCTL_SETROUTE, setroute, CTLFLAG_RW, &carp_opts[CARPCTL_SETROUTE], 0, "set route"); static int carp_suppress_preempt = 0; SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD, &carp_suppress_preempt, 0, "Preemption is suppressed"); static int carp_prio_ad = 1; SYSCTL_INT(_net_inet_carp, OID_AUTO, prio_ad, CTLFLAG_RD, &carp_prio_ad, 0, "Prioritize advertisement packet"); static struct carpstats carpstats; SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW, &carpstats, carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)"); #define CARP_LOG(...) do { \ if (carp_opts[CARPCTL_LOG] > 0) \ log(LOG_INFO, __VA_ARGS__); \ } while (0) #define CARP_DEBUG(...) do { \ if (carp_opts[CARPCTL_LOG] > 1) \ log(LOG_DEBUG, __VA_ARGS__); \ } while (0) static struct lwkt_token carp_listtok = LWKT_TOKEN_INITIALIZER(carp_list_token); static void carp_hmac_prepare(struct carp_softc *); static void carp_hmac_generate(struct carp_softc *, uint32_t [2], unsigned char [20]); static int carp_hmac_verify(struct carp_softc *, uint32_t [2], unsigned char [20]); static void carp_setroute(struct carp_softc *, int); static void carp_proto_input_c(struct carp_softc *, struct mbuf *, struct carp_header *, sa_family_t); static int carp_clone_create(struct if_clone *, int, caddr_t, caddr_t); static int carp_clone_destroy(struct ifnet *); static void carp_detach(struct carp_softc *, boolean_t, boolean_t); static void carp_prepare_ad(struct carp_softc *, struct carp_header *); static void carp_send_ad_all(void); static void carp_send_ad_timeout(void *); static void carp_send_ad(struct carp_softc *); static void carp_send_arp(struct carp_softc *); static void carp_master_down_timeout(void *); static void carp_master_down(struct carp_softc *); static void carp_setrun(struct carp_softc *, sa_family_t); static void carp_set_state(struct carp_softc *, int); static struct ifnet *carp_forus(struct carp_if *, const uint8_t *); static void carp_init(void *); static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *); static int carp_output(struct ifnet *, struct mbuf *, struct sockaddr *, struct rtentry *); static void carp_start(struct ifnet *, struct ifaltq_subque *); static void carp_multicast_cleanup(struct carp_softc *); static void carp_add_addr(struct carp_softc *, struct ifaddr *); static void carp_del_addr(struct carp_softc *, struct ifaddr *); static void carp_config_addr(struct carp_softc *, struct ifaddr *); static void carp_link_addrs(struct carp_softc *, struct ifnet *, struct ifaddr *); static void carp_unlink_addrs(struct carp_softc *, struct ifnet *, struct ifaddr *); static void carp_update_addrs(struct carp_softc *, struct ifaddr *); static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *, struct in_ifaddr *); static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *, struct ifnet *, struct in_ifaddr *, int); static void carp_deactivate_vhaddr(struct carp_softc *, struct carp_vhaddr *, boolean_t); static int carp_addroute_vhaddr(struct carp_softc *, struct carp_vhaddr *); static void carp_delroute_vhaddr(struct carp_softc *, struct carp_vhaddr *, boolean_t); #ifdef foo static void carp_sc_state(struct carp_softc *); #endif #ifdef INET6 static void carp_send_na(struct carp_softc *); #ifdef notyet static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *); static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *); #endif static void carp_multicast6_cleanup(struct carp_softc *); #endif static void carp_stop(struct carp_softc *, boolean_t); static void carp_suspend(struct carp_softc *, boolean_t); static void carp_ioctl_stop(struct carp_softc *); static int carp_ioctl_setvh(struct carp_softc *, void *, struct ucred *); static void carp_ioctl_ifcap(struct carp_softc *, int); static int carp_ioctl_getvh(struct carp_softc *, void *, struct ucred *); static int carp_ioctl_getdevname(struct carp_softc *, struct ifdrv *); static int carp_ioctl_getvhaddr(struct carp_softc *, struct ifdrv *); static struct carp_if *carp_if_remove(struct carp_if *, struct carp_softc *); static struct carp_if *carp_if_insert(struct carp_if *, struct carp_softc *); static void carp_if_free(struct carp_if *); static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event, struct ifaddr *); static void carp_ifdetach(void *, struct ifnet *); static void carp_ifdetach_dispatch(netmsg_t); static void carp_clone_destroy_dispatch(netmsg_t); static void carp_init_dispatch(netmsg_t); static void carp_ioctl_stop_dispatch(netmsg_t); static void carp_ioctl_setvh_dispatch(netmsg_t); static void carp_ioctl_ifcap_dispatch(netmsg_t); static void carp_ioctl_getvh_dispatch(netmsg_t); static void carp_ioctl_getdevname_dispatch(netmsg_t); static void carp_ioctl_getvhaddr_dispatch(netmsg_t); static void carp_send_ad_timeout_dispatch(netmsg_t); static void carp_master_down_timeout_dispatch(netmsg_t); static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces"); static LIST_HEAD(, carp_softc) carpif_list; static struct if_clone carp_cloner = IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy, 0, IF_MAXUNIT); static const uint8_t carp_etheraddr[ETHER_ADDR_LEN] = { 0, 0, 0x5e, 0, 1, 0 }; static eventhandler_tag carp_ifdetach_event; static eventhandler_tag carp_ifaddr_event; static __inline void carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new) { struct carp_vhaddr *vha; u_long new_addr, addr; KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0); /* * Virtual address list is sorted; smaller one first */ new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr); TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr); if (addr > new_addr) break; } if (vha == NULL) TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link); else TAILQ_INSERT_BEFORE(vha, vha_new, vha_link); vha_new->vha_flags |= CARP_VHAF_ONLIST; } static __inline void carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha) { KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST); vha->vha_flags &= ~CARP_VHAF_ONLIST; TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link); } static void carp_hmac_prepare(struct carp_softc *sc) { uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT; uint8_t vhid = sc->sc_vhid & 0xff; int i; #ifdef INET6 struct ifaddr_container *ifac; struct in6_addr in6; #endif #ifdef INET struct carp_vhaddr *vha; #endif /* XXX: possible race here */ /* compute ipad from key */ bzero(sc->sc_pad, sizeof(sc->sc_pad)); bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key)); for (i = 0; i < sizeof(sc->sc_pad); i++) sc->sc_pad[i] ^= 0x36; /* precompute first part of inner hash */ SHA1Init(&sc->sc_sha1); SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad)); SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version)); SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type)); SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid)); #ifdef INET TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { SHA1Update(&sc->sc_sha1, (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr, sizeof(struct in_addr)); } #endif /* INET */ #ifdef INET6 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { struct ifaddr *ifa = ifac->ifa; if (ifa->ifa_addr->sa_family == AF_INET6) { in6 = ifatoia6(ifa)->ia_addr.sin6_addr; in6_clearscope(&in6); SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6)); } } #endif /* INET6 */ /* convert ipad to opad */ for (i = 0; i < sizeof(sc->sc_pad); i++) sc->sc_pad[i] ^= 0x36 ^ 0x5c; } static void carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2], unsigned char md[20]) { SHA1_CTX sha1ctx; /* fetch first half of inner hash */ bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx)); SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter)); SHA1Final(md, &sha1ctx); /* outer hash */ SHA1Init(&sha1ctx); SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad)); SHA1Update(&sha1ctx, md, 20); SHA1Final(md, &sha1ctx); } static int carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2], unsigned char md[20]) { unsigned char md2[20]; carp_hmac_generate(sc, counter, md2); return (bcmp(md, md2, sizeof(md2))); } static void carp_setroute(struct carp_softc *sc, int cmd) { #ifdef INET6 struct ifaddr_container *ifac; #endif struct carp_vhaddr *vha; KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD); TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { if (vha->vha_iaback == NULL) continue; if (cmd == RTM_DELETE) carp_delroute_vhaddr(sc, vha, FALSE); else carp_addroute_vhaddr(sc, vha); } #ifdef INET6 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { struct ifaddr *ifa = ifac->ifa; if (ifa->ifa_addr->sa_family == AF_INET6) { if (cmd == RTM_ADD) in6_ifaddloop(ifa); else in6_ifremloop(ifa); } } #endif /* INET6 */ } static int carp_clone_create(struct if_clone *ifc, int unit, caddr_t params __unused, caddr_t data __unused) { struct carp_softc *sc; struct ifnet *ifp; sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO); ifp = &sc->sc_if; sc->sc_suppress = 0; sc->sc_advbase = CARP_DFLTINTV; sc->sc_vhid = -1; /* required setting */ sc->sc_advskew = 0; sc->sc_init_counter = 1; sc->sc_naddrs = 0; sc->sc_naddrs6 = 0; TAILQ_INIT(&sc->sc_vha_list); #ifdef INET6 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL; #endif callout_init_mp(&sc->sc_ad_tmo); netmsg_init(&sc->sc_ad_msg.base, NULL, &netisr_adone_rport, MSGF_DROPABLE | MSGF_PRIORITY, carp_send_ad_timeout_dispatch); sc->sc_ad_msg.nc_softc = sc; callout_init_mp(&sc->sc_md_tmo); callout_init_mp(&sc->sc_md6_tmo); netmsg_init(&sc->sc_md_msg.base, NULL, &netisr_adone_rport, MSGF_DROPABLE | MSGF_PRIORITY, carp_master_down_timeout_dispatch); sc->sc_md_msg.nc_softc = sc; if_initname(ifp, CARP_IFNAME, unit); ifp->if_softc = sc; ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; ifp->if_init = carp_init; ifp->if_ioctl = carp_ioctl; ifp->if_start = carp_start; ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_TSO; ifp->if_capenable = ifp->if_capabilities; /* * Leave if_hwassist as it is; if_hwassist will be * setup when this carp interface has parent. */ ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); ifq_set_ready(&ifp->if_snd); ether_ifattach(ifp, carp_etheraddr, NULL); ifp->if_type = IFT_CARP; ifp->if_output = carp_output; lwkt_gettoken(&carp_listtok); LIST_INSERT_HEAD(&carpif_list, sc, sc_next); lwkt_reltoken(&carp_listtok); return (0); } static void carp_clone_destroy_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; sc->sc_dead = TRUE; carp_detach(sc, TRUE, FALSE); callout_cancel(&sc->sc_ad_tmo); callout_cancel(&sc->sc_md_tmo); callout_cancel(&sc->sc_md6_tmo); crit_enter(); lwkt_dropmsg(&sc->sc_ad_msg.base.lmsg); lwkt_dropmsg(&sc->sc_md_msg.base.lmsg); crit_exit(); lwkt_replymsg(&cmsg->base.lmsg, 0); } static int carp_clone_destroy(struct ifnet *ifp) { struct carp_softc *sc = ifp->if_softc; struct netmsg_carp cmsg; bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_clone_destroy_dispatch); cmsg.nc_softc = sc; lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); lwkt_gettoken(&carp_listtok); LIST_REMOVE(sc, sc_next); lwkt_reltoken(&carp_listtok); bpfdetach(ifp); if_detach(ifp); KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active")); kfree(sc, M_CARP); return 0; } static struct carp_if * carp_if_remove(struct carp_if *ocif, struct carp_softc *sc) { struct carp_softc_container *oscc, *scc; struct carp_if *cif; int count = 0; #ifdef INVARIANTS int found = 0; #endif TAILQ_FOREACH(oscc, ocif, scc_link) { ++count; #ifdef INVARIANTS if (oscc->scc_softc == sc) found = 1; #endif } KASSERT(found, ("%s carp_softc is not on carp_if", __func__)); if (count == 1) { /* Last one is going to be unlinked */ return NULL; } cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO); TAILQ_INIT(cif); TAILQ_FOREACH(oscc, ocif, scc_link) { if (oscc->scc_softc == sc) continue; scc = kmalloc(sizeof(*scc), M_CARP, M_WAITOK | M_ZERO); scc->scc_softc = oscc->scc_softc; TAILQ_INSERT_TAIL(cif, scc, scc_link); } return cif; } static struct carp_if * carp_if_insert(struct carp_if *ocif, struct carp_softc *sc) { struct carp_softc_container *oscc; int onlist; onlist = 0; if (ocif != NULL) { TAILQ_FOREACH(oscc, ocif, scc_link) { if (oscc->scc_softc == sc) onlist = 1; } } #ifdef INVARIANTS if (sc->sc_carpdev != NULL) { KASSERT(onlist, ("%s is not on %s carp list", sc->sc_if.if_xname, sc->sc_carpdev->if_xname)); } else { KASSERT(!onlist, ("%s is already on carp list", sc->sc_if.if_xname)); } #endif if (!onlist) { struct carp_if *cif; struct carp_softc_container *new_scc, *scc; int inserted = 0; cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO); TAILQ_INIT(cif); new_scc = kmalloc(sizeof(*new_scc), M_CARP, M_WAITOK | M_ZERO); new_scc->scc_softc = sc; if (ocif != NULL) { TAILQ_FOREACH(oscc, ocif, scc_link) { if (!inserted && oscc->scc_softc->sc_vhid > sc->sc_vhid) { TAILQ_INSERT_TAIL(cif, new_scc, scc_link); inserted = 1; } scc = kmalloc(sizeof(*scc), M_CARP, M_WAITOK | M_ZERO); scc->scc_softc = oscc->scc_softc; TAILQ_INSERT_TAIL(cif, scc, scc_link); } } if (!inserted) TAILQ_INSERT_TAIL(cif, new_scc, scc_link); return cif; } else { return ocif; } } static void carp_if_free(struct carp_if *cif) { struct carp_softc_container *scc; while ((scc = TAILQ_FIRST(cif)) != NULL) { TAILQ_REMOVE(cif, scc, scc_link); kfree(scc, M_CARP); } kfree(cif, M_CARP); } static void carp_detach(struct carp_softc *sc, boolean_t detach, boolean_t del_iaback) { carp_suspend(sc, detach); carp_multicast_cleanup(sc); #ifdef INET6 carp_multicast6_cleanup(sc); #endif if (!sc->sc_dead && detach) { struct carp_vhaddr *vha; TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) carp_deactivate_vhaddr(sc, vha, del_iaback); KKASSERT(sc->sc_naddrs == 0); } if (sc->sc_carpdev != NULL) { struct ifnet *ifp = sc->sc_carpdev; struct carp_if *ocif = ifp->if_carp; ifp->if_carp = carp_if_remove(ocif, sc); KASSERT(ifp->if_carp != ocif, ("%s carp_if_remove failed", __func__)); sc->sc_carpdev = NULL; sc->sc_ia = NULL; sc->arpcom.ac_if.if_hwassist = 0; /* * Make sure that all protocol threads see the * sc_carpdev and if_carp changes */ netmsg_service_sync(); if (ifp->if_carp == NULL) { /* * No more carp interfaces using * ifp as the backing interface, * move it out of promiscous mode. */ ifpromisc(ifp, 0); } /* * The old carp list could be safely free now, * since no one can access it. */ carp_if_free(ocif); } } static void carp_ifdetach_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct ifnet *ifp = cmsg->nc_carpdev; while (ifp->if_carp) { struct carp_softc_container *scc; scc = TAILQ_FIRST((struct carp_if *)(ifp->if_carp)); carp_detach(scc->scc_softc, TRUE, TRUE); } lwkt_replymsg(&cmsg->base.lmsg, 0); } /* Detach an interface from the carp. */ static void carp_ifdetach(void *arg __unused, struct ifnet *ifp) { struct netmsg_carp cmsg; ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ifdetach_dispatch); cmsg.nc_carpdev = ifp; lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); } /* * process input packet. * we have rearranged checks order compared to the rfc, * but it seems more efficient this way or not possible otherwise. */ int carp_proto_input(struct mbuf **mp, int *offp, int proto) { struct mbuf *m = *mp; struct ip *ip = mtod(m, struct ip *); struct ifnet *ifp = m->m_pkthdr.rcvif; struct carp_header *ch; struct carp_softc *sc; int len, iphlen; iphlen = *offp; *mp = NULL; carpstats.carps_ipackets++; if (!carp_opts[CARPCTL_ALLOW]) { m_freem(m); goto back; } /* Check if received on a valid carp interface */ if (ifp->if_type != IFT_CARP) { carpstats.carps_badif++; CARP_LOG("carp_proto_input: packet received on non-carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } if (!CARP_IS_RUNNING(ifp)) { carpstats.carps_badif++; CARP_LOG("carp_proto_input: packet received on stopped carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } sc = ifp->if_softc; if (sc->sc_carpdev == NULL) { carpstats.carps_badif++; CARP_LOG("carp_proto_input: packet received on defunc carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { carpstats.carps_badif++; CARP_LOG("carp_proto_input: non-mcast packet on " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } /* Verify that the IP TTL is CARP_DFLTTL. */ if (ip->ip_ttl != CARP_DFLTTL) { carpstats.carps_badttl++; CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n", ip->ip_ttl, CARP_DFLTTL, ifp->if_xname); m_freem(m); goto back; } /* Minimal CARP packet size */ len = iphlen + sizeof(*ch); /* * Verify that the received packet length is * not less than the CARP header */ if (m->m_pkthdr.len < len) { carpstats.carps_badlen++; CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len, ifp->if_xname); m_freem(m); goto back; } /* Make sure that CARP header is contiguous */ if (len > m->m_len) { m = m_pullup(m, len); if (m == NULL) { carpstats.carps_hdrops++; CARP_LOG("carp_proto_input: m_pullup failed\n"); goto back; } ip = mtod(m, struct ip *); } ch = (struct carp_header *)((uint8_t *)ip + iphlen); /* Verify the CARP checksum */ if (in_cksum_skip(m, len, iphlen)) { carpstats.carps_badsum++; CARP_LOG("carp_proto_input: checksum failed on %s\n", ifp->if_xname); m_freem(m); goto back; } carp_proto_input_c(sc, m, ch, AF_INET); back: return(IPPROTO_DONE); } #ifdef INET6 int carp6_proto_input(struct mbuf **mp, int *offp, int proto) { struct mbuf *m = *mp; struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); struct ifnet *ifp = m->m_pkthdr.rcvif; struct carp_header *ch; struct carp_softc *sc; u_int len; carpstats.carps_ipackets6++; if (!carp_opts[CARPCTL_ALLOW]) { m_freem(m); goto back; } /* check if received on a valid carp interface */ if (ifp->if_type != IFT_CARP) { carpstats.carps_badif++; CARP_LOG("carp6_proto_input: packet received on non-carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } if (!CARP_IS_RUNNING(ifp)) { carpstats.carps_badif++; CARP_LOG("carp_proto_input: packet received on stopped carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } sc = ifp->if_softc; if (sc->sc_carpdev == NULL) { carpstats.carps_badif++; CARP_LOG("carp6_proto_input: packet received on defunc-carp " "interface: %s\n", ifp->if_xname); m_freem(m); goto back; } /* verify that the IP TTL is 255 */ if (ip6->ip6_hlim != CARP_DFLTTL) { carpstats.carps_badttl++; CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n", ip6->ip6_hlim, ifp->if_xname); m_freem(m); goto back; } /* verify that we have a complete carp packet */ len = m->m_len; IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch)); if (ch == NULL) { carpstats.carps_badlen++; CARP_LOG("carp6_proto_input: packet size %u too small\n", len); goto back; } /* verify the CARP checksum */ if (in_cksum_range(m, 0, *offp, sizeof(*ch))) { carpstats.carps_badsum++; CARP_LOG("carp6_proto_input: checksum failed, on %s\n", ifp->if_xname); m_freem(m); goto back; } carp_proto_input_c(sc, m, ch, AF_INET6); back: return (IPPROTO_DONE); } #endif /* INET6 */ static void carp_proto_input_c(struct carp_softc *sc, struct mbuf *m, struct carp_header *ch, sa_family_t af) { struct ifnet *cifp; uint64_t tmp_counter; struct timeval sc_tv, ch_tv; if (sc->sc_vhid != ch->carp_vhid) { /* * CARP uses multicast, however, multicast packets * are tapped to all CARP interfaces on the physical * interface receiving the CARP packets, so we don't * update any stats here. */ m_freem(m); return; } cifp = &sc->sc_if; /* verify the CARP version. */ if (ch->carp_version != CARP_VERSION) { carpstats.carps_badver++; CARP_LOG("%s; invalid version %d\n", cifp->if_xname, ch->carp_version); m_freem(m); return; } /* verify the hash */ if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) { carpstats.carps_badauth++; CARP_LOG("%s: incorrect hash\n", cifp->if_xname); m_freem(m); return; } tmp_counter = ntohl(ch->carp_counter[0]); tmp_counter = tmp_counter<<32; tmp_counter += ntohl(ch->carp_counter[1]); /* XXX Replay protection goes here */ sc->sc_init_counter = 0; sc->sc_counter = tmp_counter; sc_tv.tv_sec = sc->sc_advbase; if (carp_suppress_preempt && sc->sc_advskew < 240) sc_tv.tv_usec = 240 * 1000000 / 256; else sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256; ch_tv.tv_sec = ch->carp_advbase; ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256; switch (sc->sc_state) { case INIT: break; case MASTER: /* * If we receive an advertisement from a master who's going to * be more frequent than us, go into BACKUP state. */ if (timevalcmp(&sc_tv, &ch_tv, >) || timevalcmp(&sc_tv, &ch_tv, ==)) { callout_stop(&sc->sc_ad_tmo); CARP_DEBUG("%s: MASTER -> BACKUP " "(more frequent advertisement received)\n", cifp->if_xname); carp_set_state(sc, BACKUP); carp_setrun(sc, 0); if (carp_opts[CARPCTL_SETROUTE]) carp_setroute(sc, RTM_DELETE); } break; case BACKUP: /* * If we're pre-empting masters who advertise slower than us, * and this one claims to be slower, treat him as down. */ if (carp_opts[CARPCTL_PREEMPT] && timevalcmp(&sc_tv, &ch_tv, <)) { CARP_DEBUG("%s: BACKUP -> MASTER " "(preempting a slower master)\n", cifp->if_xname); carp_master_down(sc); break; } /* * If the master is going to advertise at such a low frequency * that he's guaranteed to time out, we'd might as well just * treat him as timed out now. */ sc_tv.tv_sec = sc->sc_advbase * 3; if (timevalcmp(&sc_tv, &ch_tv, <)) { CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n", cifp->if_xname); carp_master_down(sc); break; } /* * Otherwise, we reset the counter and wait for the next * advertisement. */ carp_setrun(sc, af); break; } m_freem(m); } struct mbuf * carp_input(void *v, struct mbuf *m) { struct carp_if *cif = v; struct ether_header *eh; struct carp_softc_container *scc; struct ifnet *ifp; eh = mtod(m, struct ether_header *); ifp = carp_forus(cif, eh->ether_dhost); if (ifp != NULL) { ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF); return NULL; } if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) return m; /* * XXX Should really check the list of multicast addresses * for each CARP interface _before_ copying. */ TAILQ_FOREACH(scc, cif, scc_link) { struct carp_softc *sc = scc->scc_softc; struct mbuf *m0; if ((sc->sc_if.if_flags & IFF_UP) == 0) continue; m0 = m_dup(m, M_NOWAIT); if (m0 == NULL) continue; ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF); } return m; } static void carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch) { if (sc->sc_init_counter) { /* this could also be seconds since unix epoch */ sc->sc_counter = karc4random(); sc->sc_counter = sc->sc_counter << 32; sc->sc_counter += karc4random(); } else { sc->sc_counter++; } ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff); ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff); carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); } static void carp_send_ad_all(void) { struct carp_softc *sc; LIST_FOREACH(sc, &carpif_list, sc_next) { if (sc->sc_carpdev == NULL) continue; if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER) carp_send_ad(sc); } } static void carp_send_ad_timeout(void *xsc) { struct carp_softc *sc = xsc; struct netmsg_carp *cmsg = &sc->sc_ad_msg; KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d", __func__, mycpuid)); crit_enter(); if (cmsg->base.lmsg.ms_flags & MSGF_DONE) lwkt_sendmsg_oncpu(netisr_cpuport(0), &cmsg->base.lmsg); crit_exit(); } static void carp_send_ad_timeout_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; /* Reply ASAP */ crit_enter(); lwkt_replymsg(&cmsg->base.lmsg, 0); crit_exit(); carp_send_ad(sc); } static void carp_send_ad(struct carp_softc *sc) { struct ifnet *cifp = &sc->sc_if; struct carp_header ch; struct timeval tv; struct carp_header *ch_ptr; struct mbuf *m; int len, advbase, advskew; if (!CARP_IS_RUNNING(cifp)) { /* Bow out */ advbase = 255; advskew = 255; } else { advbase = sc->sc_advbase; if (!carp_suppress_preempt || sc->sc_advskew > 240) advskew = sc->sc_advskew; else advskew = 240; tv.tv_sec = advbase; tv.tv_usec = advskew * 1000000 / 256; } ch.carp_version = CARP_VERSION; ch.carp_type = CARP_ADVERTISEMENT; ch.carp_vhid = sc->sc_vhid; ch.carp_advbase = advbase; ch.carp_advskew = advskew; ch.carp_authlen = 7; /* XXX DEFINE */ ch.carp_pad1 = 0; /* must be zero */ ch.carp_cksum = 0; #ifdef INET if (sc->sc_ia != NULL) { struct ip *ip; MGETHDR(m, M_NOWAIT, MT_HEADER); if (m == NULL) { IFNET_STAT_INC(cifp, oerrors, 1); carpstats.carps_onomem++; /* XXX maybe less ? */ if (advbase != 255 || advskew != 255) callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), carp_send_ad_timeout, sc); return; } len = sizeof(*ip) + sizeof(ch); m->m_pkthdr.len = len; m->m_pkthdr.rcvif = NULL; m->m_len = len; MH_ALIGN(m, m->m_len); m->m_flags |= M_MCAST; if (carp_prio_ad) m->m_flags |= M_PRIO; ip = mtod(m, struct ip *); ip->ip_v = IPVERSION; ip->ip_hl = sizeof(*ip) >> 2; ip->ip_tos = IPTOS_LOWDELAY; ip->ip_len = htons(len); ip->ip_id = ip_newid(); ip->ip_off = htons(IP_DF); ip->ip_ttl = CARP_DFLTTL; ip->ip_p = IPPROTO_CARP; ip->ip_sum = 0; ip->ip_src = sc->sc_ia->ia_addr.sin_addr; ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP); ch_ptr = (struct carp_header *)(&ip[1]); bcopy(&ch, ch_ptr, sizeof(ch)); carp_prepare_ad(sc, ch_ptr); ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip)); getmicrotime(&cifp->if_lastchange); IFNET_STAT_INC(cifp, opackets, 1); IFNET_STAT_INC(cifp, obytes, len); carpstats.carps_opackets++; if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) { IFNET_STAT_INC(cifp, oerrors, 1); if (sc->sc_sendad_errors < INT_MAX) sc->sc_sendad_errors++; if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { carp_suppress_preempt++; if (carp_suppress_preempt == 1) { carp_send_ad_all(); } } sc->sc_sendad_success = 0; } else { if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { if (++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) { carp_suppress_preempt--; sc->sc_sendad_errors = 0; } } else { sc->sc_sendad_errors = 0; } } } #endif /* INET */ #ifdef INET6 if (sc->sc_ia6) { struct ip6_hdr *ip6; MGETHDR(m, M_NOWAIT, MT_HEADER); if (m == NULL) { IFNET_STAT_INC(cifp, oerrors, 1); carpstats.carps_onomem++; /* XXX maybe less ? */ if (advbase != 255 || advskew != 255) callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), carp_send_ad_timeout, sc); return; } len = sizeof(*ip6) + sizeof(ch); m->m_pkthdr.len = len; m->m_pkthdr.rcvif = NULL; m->m_len = len; MH_ALIGN(m, m->m_len); m->m_flags |= M_MCAST; ip6 = mtod(m, struct ip6_hdr *); bzero(ip6, sizeof(*ip6)); ip6->ip6_vfc |= IPV6_VERSION; ip6->ip6_hlim = CARP_DFLTTL; ip6->ip6_nxt = IPPROTO_CARP; bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src, sizeof(struct in6_addr)); /* set the multicast destination */ ip6->ip6_dst.s6_addr16[0] = htons(0xff02); ip6->ip6_dst.s6_addr8[15] = 0x12; if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) { IFNET_STAT_INC(cifp, oerrors, 1); m_freem(m); CARP_LOG("%s: in6_setscope failed\n", __func__); return; } ch_ptr = (struct carp_header *)(&ip6[1]); bcopy(&ch, ch_ptr, sizeof(ch)); carp_prepare_ad(sc, ch_ptr); ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6)); getmicrotime(&cifp->if_lastchange); IFNET_STAT_INC(cifp, opackets, 1); IFNET_STAT_INC(cifp, obytes, len); carpstats.carps_opackets6++; if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) { IFNET_STAT_INC(cifp, oerrors, 1); if (sc->sc_sendad_errors < INT_MAX) sc->sc_sendad_errors++; if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { carp_suppress_preempt++; if (carp_suppress_preempt == 1) { carp_send_ad_all(); } } sc->sc_sendad_success = 0; } else { if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { if (++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) { carp_suppress_preempt--; sc->sc_sendad_errors = 0; } } else { sc->sc_sendad_errors = 0; } } } #endif /* INET6 */ if (advbase != 255 || advskew != 255) callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), carp_send_ad_timeout, sc); } /* * Broadcast a gratuitous ARP request containing * the virtual router MAC address for each IP address * associated with the virtual router. */ static void carp_send_arp(struct carp_softc *sc) { const struct carp_vhaddr *vha; TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { if (vha->vha_iaback == NULL) continue; arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa); } } #ifdef INET6 static void carp_send_na(struct carp_softc *sc) { struct ifaddr_container *ifac; struct in6_addr *in6; static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { struct ifaddr *ifa = ifac->ifa; if (ifa->ifa_addr->sa_family != AF_INET6) continue; in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; nd6_na_output(sc->sc_carpdev, &mcast, in6, ND_NA_FLAG_OVERRIDE, 1, NULL); DELAY(1000); /* XXX */ } } #endif /* INET6 */ #ifdef notyet static __inline const struct carp_vhaddr * carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr) { struct carp_vhaddr *vha; TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { if (vha->vha_iaback == NULL) continue; if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr) return vha; } return NULL; } static int carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr, const struct in_addr *isaddr, uint8_t **enaddr) { const struct carp_softc *vh; int index, count = 0; /* * XXX proof of concept implementation. * We use the source ip to decide which virtual host should * handle the request. If we're master of that virtual host, * then we respond, otherwise, just drop the arp packet on * the floor. */ TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { if (!CARP_IS_RUNNING(&vh->sc_if)) continue; if (carp_find_addr(vh, itaddr) != NULL) count++; } if (count == 0) return 0; /* this should be a hash, like pf_hash() */ index = ntohl(isaddr->s_addr) % count; count = 0; TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { if (!CARP_IS_RUNNING(&vh->sc_if)) continue; if (carp_find_addr(vh, itaddr) == NULL) continue; if (count == index) { if (vh->sc_state == MASTER) { *enaddr = IF_LLADDR(&vh->sc_if); return 1; } else { return 0; } } count++; } return 0; } #endif int carp_iamatch(const struct in_ifaddr *ia) { const struct carp_softc *sc = ia->ia_ifp->if_softc; ASSERT_NETISR0; #ifdef notyet if (carp_opts[CARPCTL_ARPBALANCE]) return carp_iamatch_balance(cif, itaddr, isaddr, enaddr); #endif if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER) return 0; return 1; } #ifdef INET6 struct ifaddr * carp_iamatch6(void *v, struct in6_addr *taddr) { #ifdef foo struct carp_if *cif = v; struct carp_softc *vh; TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { struct ifaddr_container *ifac; TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid], ifa_link) { struct ifaddr *ifa = ifac->ifa; if (IN6_ARE_ADDR_EQUAL(taddr, &ifatoia6(ifa)->ia_addr.sin6_addr) && CARP_IS_RUNNING(&vh->sc_if) && vh->sc_state == MASTER) { return (ifa); } } } #endif return (NULL); } void * carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr) { #ifdef foo struct m_tag *mtag; struct carp_if *cif = v; struct carp_softc *sc; TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { struct ifaddr_container *ifac; TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { struct ifaddr *ifa = ifac->ifa; if (IN6_ARE_ADDR_EQUAL(taddr, &ifatoia6(ifa)->ia_addr.sin6_addr) && CARP_IS_RUNNING(&sc->sc_if)) { struct ifnet *ifp = &sc->sc_if; mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT); if (mtag == NULL) { /* better a bit than nothing */ return (IF_LLADDR(ifp)); } bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *)); m_tag_prepend(m, mtag); return (IF_LLADDR(ifp)); } } } #endif return (NULL); } #endif static struct ifnet * carp_forus(struct carp_if *cif, const uint8_t *dhost) { struct carp_softc_container *scc; if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0) return NULL; TAILQ_FOREACH(scc, cif, scc_link) { struct carp_softc *sc = scc->scc_softc; struct ifnet *ifp = &sc->sc_if; if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER && !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN)) return ifp; } return NULL; } static void carp_master_down_timeout(void *xsc) { struct carp_softc *sc = xsc; struct netmsg_carp *cmsg = &sc->sc_md_msg; KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d", __func__, mycpuid)); crit_enter(); if (cmsg->base.lmsg.ms_flags & MSGF_DONE) lwkt_sendmsg_oncpu(netisr_cpuport(0), &cmsg->base.lmsg); crit_exit(); } static void carp_master_down_timeout_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; /* Reply ASAP */ crit_enter(); lwkt_replymsg(&cmsg->base.lmsg, 0); crit_exit(); CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n", sc->sc_if.if_xname); carp_master_down(sc); } static void carp_master_down(struct carp_softc *sc) { switch (sc->sc_state) { case INIT: kprintf("%s: master_down event in INIT state\n", sc->sc_if.if_xname); break; case MASTER: break; case BACKUP: carp_set_state(sc, MASTER); carp_send_ad(sc); carp_send_arp(sc); #ifdef INET6 carp_send_na(sc); #endif /* INET6 */ carp_setrun(sc, 0); if (carp_opts[CARPCTL_SETROUTE]) carp_setroute(sc, RTM_ADD); break; } } /* * When in backup state, af indicates whether to reset the master down timer * for v4 or v6. If it's set to zero, reset the ones which are already pending. */ static void carp_setrun(struct carp_softc *sc, sa_family_t af) { struct ifnet *cifp = &sc->sc_if; struct timeval tv; if (sc->sc_carpdev == NULL) { carp_set_state(sc, INIT); return; } if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6)) { /* Nothing */ } else { if (carp_opts[CARPCTL_SETROUTE]) carp_setroute(sc, RTM_DELETE); return; } switch (sc->sc_state) { case INIT: if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) { carp_send_ad(sc); carp_send_arp(sc); #ifdef INET6 carp_send_na(sc); #endif /* INET6 */ CARP_DEBUG("%s: INIT -> MASTER (preempting)\n", cifp->if_xname); carp_set_state(sc, MASTER); if (carp_opts[CARPCTL_SETROUTE]) carp_setroute(sc, RTM_ADD); } else { CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname); carp_set_state(sc, BACKUP); if (carp_opts[CARPCTL_SETROUTE]) carp_setroute(sc, RTM_DELETE); carp_setrun(sc, 0); } break; case BACKUP: callout_stop(&sc->sc_ad_tmo); tv.tv_sec = 3 * sc->sc_advbase; tv.tv_usec = sc->sc_advskew * 1000000 / 256; switch (af) { #ifdef INET case AF_INET: callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv), carp_master_down_timeout, sc); break; #endif /* INET */ #ifdef INET6 case AF_INET6: callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv), carp_master_down_timeout, sc); break; #endif /* INET6 */ default: if (sc->sc_naddrs) callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv), carp_master_down_timeout, sc); if (sc->sc_naddrs6) callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv), carp_master_down_timeout, sc); break; } break; case MASTER: tv.tv_sec = sc->sc_advbase; tv.tv_usec = sc->sc_advskew * 1000000 / 256; callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), carp_send_ad_timeout, sc); break; } } static void carp_multicast_cleanup(struct carp_softc *sc) { struct ip_moptions *imo = &sc->sc_imo; if (imo->imo_num_memberships == 0) return; KKASSERT(imo->imo_num_memberships == 1); in_delmulti(imo->imo_membership[0]); imo->imo_membership[0] = NULL; imo->imo_num_memberships = 0; imo->imo_multicast_ifp = NULL; } #ifdef INET6 static void carp_multicast6_cleanup(struct carp_softc *sc) { struct ip6_moptions *im6o = &sc->sc_im6o; while (!LIST_EMPTY(&im6o->im6o_memberships)) { struct in6_multi_mship *imm = LIST_FIRST(&im6o->im6o_memberships); LIST_REMOVE(imm, i6mm_chain); in6_leavegroup(imm); } im6o->im6o_multicast_ifp = NULL; } #endif static void carp_ioctl_getvhaddr_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; const struct carp_vhaddr *vha; struct ifcarpvhaddr *carpa, *carpa0; int count, len, error = 0; count = 0; TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) ++count; if (cmsg->nc_datalen == 0) { cmsg->nc_datalen = count * sizeof(*carpa); goto back; } else if (count == 0 || cmsg->nc_datalen < sizeof(*carpa)) { cmsg->nc_datalen = 0; goto back; } len = min(cmsg->nc_datalen, sizeof(*carpa) * count); KKASSERT(len >= sizeof(*carpa)); carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO); if (carpa == NULL) { error = ENOMEM; goto back; } count = 0; TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { if (len < sizeof(*carpa)) break; carpa->carpa_flags = vha->vha_flags; carpa->carpa_addr.sin_family = AF_INET; carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr; carpa->carpa_baddr.sin_family = AF_INET; if (vha->vha_iaback == NULL) { carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY; } else { carpa->carpa_baddr.sin_addr = vha->vha_iaback->ia_addr.sin_addr; } ++carpa; ++count; len -= sizeof(*carpa); } cmsg->nc_datalen = sizeof(*carpa) * count; KKASSERT(cmsg->nc_datalen > 0); cmsg->nc_data = carpa0; back: lwkt_replymsg(&cmsg->base.lmsg, error); } static int carp_ioctl_getvhaddr(struct carp_softc *sc, struct ifdrv *ifd) { struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; int error; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ioctl_getvhaddr_dispatch); cmsg.nc_softc = sc; cmsg.nc_datalen = ifd->ifd_len; error = lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); if (!error) { if (cmsg.nc_data != NULL) { error = copyout(cmsg.nc_data, ifd->ifd_data, cmsg.nc_datalen); kfree(cmsg.nc_data, M_TEMP); } ifd->ifd_len = cmsg.nc_datalen; } else { KASSERT(cmsg.nc_data == NULL, ("%s temp vhaddr is alloc upon error", __func__)); } ifnet_serialize_all(ifp); return error; } static int carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, struct in_ifaddr *ia_del) { struct ifnet *ifp; struct in_ifaddr *ia_if; const struct in_ifaddr *ia_vha; struct in_ifaddr_container *iac; int own, ia_match_carpdev; KKASSERT(vha->vha_ia != NULL); ia_vha = vha->vha_ia; ia_if = NULL; own = 0; ia_match_carpdev = 0; TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) { struct in_ifaddr *ia = iac->ia; if (ia == ia_del) continue; if (ia->ia_ifp->if_type == IFT_CARP) continue; if ((ia->ia_ifp->if_flags & IFF_UP) == 0) continue; /* and, yeah, we need a multicast-capable iface too */ if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0) continue; if (ia_vha->ia_subnetmask == ia->ia_subnetmask && ia_vha->ia_subnet == ia->ia_subnet) { if (ia_vha->ia_addr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr) own = 1; if (ia_if == NULL) { ia_if = ia; } else if (sc->sc_carpdev != NULL && sc->sc_carpdev == ia->ia_ifp) { ia_if = ia; if (ia_if->ia_flags & IFA_ROUTE) { /* * Address with prefix route * is prefered */ break; } ia_match_carpdev = 1; } else if (!ia_match_carpdev) { if (ia->ia_flags & IFA_ROUTE) { /* * Address with prefix route * is prefered over others. */ ia_if = ia; } } } } carp_deactivate_vhaddr(sc, vha, FALSE); if (!ia_if) return ENOENT; ifp = ia_if->ia_ifp; /* XXX Don't allow parent iface to be changed */ if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) return EEXIST; return carp_activate_vhaddr(sc, vha, ifp, ia_if, own); } static void carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa) { struct carp_vhaddr *vha_new; struct in_ifaddr *carp_ia; #ifdef INVARIANTS struct carp_vhaddr *vha; #endif KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET); carp_ia = ifatoia(carp_ifa); #ifdef INVARIANTS TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia); #endif vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO); vha_new->vha_ia = carp_ia; carp_insert_vhaddr(sc, vha_new); if (carp_config_vhaddr(sc, vha_new, NULL) != 0) { /* * If the above configuration fails, it may only mean * that the new address is problematic. However, the * carp(4) interface may already have several working * addresses. Since the expected behaviour of * SIOC[AS]IFADDR is to put the NIC into working state, * we try starting the state machine manually here with * the hope that the carp(4)'s previously working * addresses still could be brought up. */ carp_hmac_prepare(sc); carp_set_state(sc, INIT); carp_setrun(sc, 0); } } static void carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa) { struct carp_vhaddr *vha; struct in_ifaddr *carp_ia; KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET); carp_ia = ifatoia(carp_ifa); TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { KKASSERT(vha->vha_ia != NULL); if (vha->vha_ia == carp_ia) break; } KASSERT(vha != NULL, ("no corresponding vhaddr %p", carp_ifa)); /* * Remove the vhaddr from the list before deactivating * the vhaddr, so that the HMAC could be correctly * updated in carp_deactivate_vhaddr() */ carp_remove_vhaddr(sc, vha); carp_deactivate_vhaddr(sc, vha, FALSE); kfree(vha, M_CARP); } static void carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa) { struct carp_vhaddr *vha; struct in_ifaddr *carp_ia; KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET); carp_ia = ifatoia(carp_ifa); TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { KKASSERT(vha->vha_ia != NULL); if (vha->vha_ia == carp_ia) break; } KASSERT(vha != NULL, ("no corresponding vhaddr %p", carp_ifa)); /* Remove then reinsert, to keep the vhaddr list sorted */ carp_remove_vhaddr(sc, vha); carp_insert_vhaddr(sc, vha); if (carp_config_vhaddr(sc, vha, NULL) != 0) { /* See the comment in carp_add_addr() */ carp_hmac_prepare(sc); carp_set_state(sc, INIT); carp_setrun(sc, 0); } } #ifdef notyet #ifdef INET6 static int carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) { struct ifnet *ifp; struct carp_if *cif; struct in6_ifaddr *ia, *ia_if; struct ip6_moptions *im6o = &sc->sc_im6o; struct in6_multi_mship *imm; struct in6_addr in6; int own, error; if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { carp_setrun(sc, 0); return (0); } /* we have to do it by hands to check we won't match on us */ ia_if = NULL; own = 0; for (ia = in6_ifaddr; ia; ia = ia->ia_next) { int i; for (i = 0; i < 4; i++) { if ((sin6->sin6_addr.s6_addr32[i] & ia->ia_prefixmask.sin6_addr.s6_addr32[i]) != (ia->ia_addr.sin6_addr.s6_addr32[i] & ia->ia_prefixmask.sin6_addr.s6_addr32[i])) break; } /* and, yeah, we need a multicast-capable iface too */ if (ia->ia_ifp != &sc->sc_if && (ia->ia_ifp->if_flags & IFF_MULTICAST) && (i == 4)) { if (!ia_if) ia_if = ia; if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ia->ia_addr.sin6_addr)) own++; } } if (!ia_if) return (EADDRNOTAVAIL); ia = ia_if; ifp = ia->ia_ifp; if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 || (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp)) return (EADDRNOTAVAIL); if (!sc->sc_naddrs6) { im6o->im6o_multicast_ifp = ifp; /* join CARP multicast address */ bzero(&in6, sizeof(in6)); in6.s6_addr16[0] = htons(0xff02); in6.s6_addr8[15] = 0x12; if (in6_setscope(&in6, ifp, NULL) != 0) goto cleanup; if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL) goto cleanup; LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); /* join solicited multicast address */ bzero(&in6, sizeof(in6)); in6.s6_addr16[0] = htons(0xff02); in6.s6_addr32[1] = 0; in6.s6_addr32[2] = htonl(1); in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3]; in6.s6_addr8[12] = 0xff; if (in6_setscope(&in6, ifp, NULL) != 0) goto cleanup; if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL) goto cleanup; LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); } #ifdef foo if (!ifp->if_carp) { cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO); if ((error = ifpromisc(ifp, 1))) { kfree(cif, M_CARP); goto cleanup; } TAILQ_INIT(&cif->vhif_vrs); ifp->if_carp = cif; } else { struct carp_softc *vr; cif = ifp->if_carp; TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { if (vr != sc && vr->sc_vhid == sc->sc_vhid) { error = EINVAL; goto cleanup; } } } #endif sc->sc_ia6 = ia; sc->sc_carpdev = ifp; #ifdef foo { /* XXX prevent endless loop if already in queue */ struct carp_softc *vr, *after = NULL; int myself = 0; cif = ifp->if_carp; TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { if (vr == sc) myself = 1; if (vr->sc_vhid < sc->sc_vhid) after = vr; } if (!myself) { /* We're trying to keep things in order */ if (after == NULL) TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list); else TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list); } } #endif sc->sc_naddrs6++; if (own) sc->sc_advskew = 0; carp_sc_state(sc); carp_setrun(sc, 0); return (0); cleanup: /* clean up multicast memberships */ if (!sc->sc_naddrs6) { while (!LIST_EMPTY(&im6o->im6o_memberships)) { imm = LIST_FIRST(&im6o->im6o_memberships); LIST_REMOVE(imm, i6mm_chain); in6_leavegroup(imm); } } return (error); } static int carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) { int error = 0; if (!--sc->sc_naddrs6) { struct carp_if *cif = sc->sc_carpdev->if_carp; struct ip6_moptions *im6o = &sc->sc_im6o; callout_stop(&sc->sc_ad_tmo); sc->sc_vhid = -1; while (!LIST_EMPTY(&im6o->im6o_memberships)) { struct in6_multi_mship *imm = LIST_FIRST(&im6o->im6o_memberships); LIST_REMOVE(imm, i6mm_chain); in6_leavegroup(imm); } im6o->im6o_multicast_ifp = NULL; #ifdef foo TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list); if (TAILQ_EMPTY(&cif->vhif_vrs)) { sc->sc_carpdev->if_carp = NULL; kfree(cif, M_IFADDR); } #endif } return (error); } #endif /* INET6 */ #endif static int carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr) { struct carp_softc *sc = ifp->if_softc; struct ifreq *ifr = (struct ifreq *)addr; struct ifdrv *ifd = (struct ifdrv *)addr; int error = 0; ASSERT_IFNET_SERIALIZED_ALL(ifp); switch (cmd) { case SIOCSIFFLAGS: if (ifp->if_flags & IFF_UP) { if ((ifp->if_flags & IFF_RUNNING) == 0) carp_init(sc); } else if (ifp->if_flags & IFF_RUNNING) { carp_ioctl_stop(sc); } break; case SIOCSIFCAP: carp_ioctl_ifcap(sc, ifr->ifr_reqcap); break; case SIOCSVH: error = carp_ioctl_setvh(sc, ifr->ifr_data, cr); break; case SIOCGVH: error = carp_ioctl_getvh(sc, ifr->ifr_data, cr); break; case SIOCGDRVSPEC: switch (ifd->ifd_cmd) { case CARPGDEVNAME: error = carp_ioctl_getdevname(sc, ifd); break; case CARPGVHADDR: error = carp_ioctl_getvhaddr(sc, ifd); break; default: error = EINVAL; break; } break; default: error = ether_ioctl(ifp, cmd, addr); break; } return error; } static void carp_ioctl_stop_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; carp_stop(sc, FALSE); lwkt_replymsg(&cmsg->base.lmsg, 0); } static void carp_ioctl_stop(struct carp_softc *sc) { struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ioctl_stop_dispatch); cmsg.nc_softc = sc; lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); ifnet_serialize_all(ifp); } static void carp_ioctl_setvh_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; struct ifnet *ifp = &sc->arpcom.ac_if; const struct carpreq *carpr = cmsg->nc_data; int error; error = 1; if ((ifp->if_flags & IFF_RUNNING) && sc->sc_state != INIT && carpr->carpr_state != sc->sc_state) { switch (carpr->carpr_state) { case BACKUP: callout_stop(&sc->sc_ad_tmo); carp_set_state(sc, BACKUP); carp_setrun(sc, 0); if (carp_opts[CARPCTL_SETROUTE]) carp_setroute(sc, RTM_DELETE); break; case MASTER: carp_master_down(sc); break; default: break; } } if (carpr->carpr_vhid > 0) { if (carpr->carpr_vhid > 255) { error = EINVAL; goto back; } if (sc->sc_carpdev) { struct carp_if *cif = sc->sc_carpdev->if_carp; struct carp_softc_container *scc; TAILQ_FOREACH(scc, cif, scc_link) { struct carp_softc *vr = scc->scc_softc; if (vr != sc && vr->sc_vhid == carpr->carpr_vhid) { error = EEXIST; goto back; } } } sc->sc_vhid = carpr->carpr_vhid; IF_LLADDR(ifp)[5] = sc->sc_vhid; bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); error--; } if (carpr->carpr_advbase > 0 || carpr->carpr_advskew > 0) { if (carpr->carpr_advskew >= 255) { error = EINVAL; goto back; } if (carpr->carpr_advbase > 255) { error = EINVAL; goto back; } sc->sc_advbase = carpr->carpr_advbase; sc->sc_advskew = carpr->carpr_advskew; error--; } bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key)); if (error > 0) { error = EINVAL; } else { error = 0; carp_setrun(sc, 0); } back: carp_hmac_prepare(sc); lwkt_replymsg(&cmsg->base.lmsg, error); } static int carp_ioctl_setvh(struct carp_softc *sc, void *udata, struct ucred *cr) { struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; struct carpreq carpr; int error; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); error = caps_priv_check(cr, SYSCAP_RESTRICTEDROOT | __SYSCAP_NULLCRED); if (error) goto back; error = copyin(udata, &carpr, sizeof(carpr)); if (error) goto back; bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ioctl_setvh_dispatch); cmsg.nc_softc = sc; cmsg.nc_data = &carpr; error = lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); back: ifnet_serialize_all(ifp); return error; } static void carp_ioctl_ifcap_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; struct ifnet *ifp = &sc->arpcom.ac_if; int reqcap = *((const int *)(cmsg->nc_data)); int mask; mask = reqcap ^ ifp->if_capenable; if (mask & IFCAP_TXCSUM) { ifp->if_capenable ^= IFCAP_TXCSUM; if ((ifp->if_capenable & IFCAP_TXCSUM) && sc->sc_carpdev != NULL) { ifp->if_hwassist |= (sc->sc_carpdev->if_hwassist & (CSUM_IP | CSUM_UDP | CSUM_TCP)); } else { ifp->if_hwassist &= ~(CSUM_IP | CSUM_UDP | CSUM_TCP); } } if (mask & IFCAP_TSO) { ifp->if_capenable ^= IFCAP_TSO; if ((ifp->if_capenable & IFCAP_TSO) && sc->sc_carpdev != NULL) { ifp->if_hwassist |= (sc->sc_carpdev->if_hwassist & CSUM_TSO); } else { ifp->if_hwassist &= ~CSUM_TSO; } } lwkt_replymsg(&cmsg->base.lmsg, 0); } static void carp_ioctl_ifcap(struct carp_softc *sc, int reqcap) { struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ioctl_ifcap_dispatch); cmsg.nc_softc = sc; cmsg.nc_data = &reqcap; lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); ifnet_serialize_all(ifp); } static void carp_ioctl_getvh_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; struct carpreq *carpr = cmsg->nc_data; carpr->carpr_state = sc->sc_state; carpr->carpr_vhid = sc->sc_vhid; carpr->carpr_advbase = sc->sc_advbase; carpr->carpr_advskew = sc->sc_advskew; bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key)); lwkt_replymsg(&cmsg->base.lmsg, 0); } static int carp_ioctl_getvh(struct carp_softc *sc, void *udata, struct ucred *cr) { struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; struct carpreq carpr; int error; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ioctl_getvh_dispatch); cmsg.nc_softc = sc; cmsg.nc_data = &carpr; lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); error = caps_priv_check(cr, SYSCAP_RESTRICTEDROOT | __SYSCAP_NULLCRED); if (error) bzero(carpr.carpr_key, sizeof(carpr.carpr_key)); error = copyout(&carpr, udata, sizeof(carpr)); ifnet_serialize_all(ifp); return error; } static void carp_ioctl_getdevname_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; char *devname = cmsg->nc_data; bzero(devname, IFNAMSIZ); if (sc->sc_carpdev != NULL) strlcpy(devname, sc->sc_carpdev->if_xname, IFNAMSIZ); lwkt_replymsg(&cmsg->base.lmsg, 0); } static int carp_ioctl_getdevname(struct carp_softc *sc, struct ifdrv *ifd) { struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; char devname[IFNAMSIZ]; int error; ASSERT_IFNET_SERIALIZED_ALL(ifp); if (ifd->ifd_len != sizeof(devname)) return EINVAL; ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_ioctl_getdevname_dispatch); cmsg.nc_softc = sc; cmsg.nc_data = devname; lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); error = copyout(devname, ifd->ifd_data, sizeof(devname)); ifnet_serialize_all(ifp); return error; } static void carp_init_dispatch(netmsg_t msg) { struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; struct carp_softc *sc = cmsg->nc_softc; sc->sc_if.if_flags |= IFF_RUNNING; carp_hmac_prepare(sc); carp_set_state(sc, INIT); carp_setrun(sc, 0); lwkt_replymsg(&cmsg->base.lmsg, 0); } static void carp_init(void *xsc) { struct carp_softc *sc = xsc; struct ifnet *ifp = &sc->arpcom.ac_if; struct netmsg_carp cmsg; ASSERT_IFNET_SERIALIZED_ALL(ifp); ifnet_deserialize_all(ifp); bzero(&cmsg, sizeof(cmsg)); netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, carp_init_dispatch); cmsg.nc_softc = sc; lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); ifnet_serialize_all(ifp); } static int carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, struct rtentry *rt) { struct carp_softc *sc = ifp->if_softc; struct ifnet *carpdev; int error = 0; carpdev = sc->sc_carpdev; if (carpdev != NULL) { if (m->m_flags & M_MCAST) IFNET_STAT_INC(ifp, omcasts, 1); IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + ETHER_HDR_LEN); IFNET_STAT_INC(ifp, opackets, 1); /* * NOTE: * CARP's ifp is passed to backing device's * if_output method. */ carpdev->if_output(ifp, m, dst, rt); } else { IFNET_STAT_INC(ifp, oerrors, 1); m_freem(m); error = ENETUNREACH; } return error; } /* * Start output on carp interface. This function should never be called. */ static void carp_start(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused) { panic("%s: start called", ifp->if_xname); } static void carp_set_state(struct carp_softc *sc, int state) { struct ifnet *cifp = &sc->sc_if; if (sc->sc_state == state) return; sc->sc_state = state; switch (sc->sc_state) { case BACKUP: cifp->if_link_state = LINK_STATE_DOWN; break; case MASTER: cifp->if_link_state = LINK_STATE_UP; break; default: cifp->if_link_state = LINK_STATE_UNKNOWN; break; } rt_ifmsg(cifp); } void carp_group_demote_adj(struct ifnet *ifp, int adj) { struct ifg_list *ifgl; int *dm; TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL)) continue; dm = &ifgl->ifgl_group->ifg_carp_demoted; if (*dm + adj >= 0) *dm += adj; else *dm = 0; if (adj > 0 && *dm == 1) carp_send_ad_all(); CARP_LOG("%s demoted group %s to %d", ifp->if_xname, ifgl->ifgl_group->ifg_group, *dm); } } #ifdef foo void carp_carpdev_state(void *v) { struct carp_if *cif = v; struct carp_softc *sc; TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) carp_sc_state(sc); } static void carp_sc_state(struct carp_softc *sc) { if (!(sc->sc_carpdev->if_flags & IFF_UP)) { callout_stop(&sc->sc_ad_tmo); callout_stop(&sc->sc_md_tmo); callout_stop(&sc->sc_md6_tmo); carp_set_state(sc, INIT); carp_setrun(sc, 0); if (!sc->sc_suppress) { carp_suppress_preempt++; if (carp_suppress_preempt == 1) carp_send_ad_all(); } sc->sc_suppress = 1; } else { carp_set_state(sc, INIT); carp_setrun(sc, 0); if (sc->sc_suppress) carp_suppress_preempt--; sc->sc_suppress = 0; } } #endif static void carp_stop(struct carp_softc *sc, boolean_t detach) { sc->sc_if.if_flags &= ~IFF_RUNNING; callout_stop(&sc->sc_ad_tmo); callout_stop(&sc->sc_md_tmo); callout_stop(&sc->sc_md6_tmo); if (!detach && sc->sc_state == MASTER) carp_send_ad(sc); if (sc->sc_suppress) carp_suppress_preempt--; sc->sc_suppress = 0; if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) carp_suppress_preempt--; sc->sc_sendad_errors = 0; sc->sc_sendad_success = 0; carp_set_state(sc, INIT); carp_setrun(sc, 0); } static void carp_suspend(struct carp_softc *sc, boolean_t detach) { struct ifnet *cifp = &sc->sc_if; carp_stop(sc, detach); /* Retain the running state, if we are not dead yet */ if (!sc->sc_dead && (cifp->if_flags & IFF_UP)) cifp->if_flags |= IFF_RUNNING; } static int carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, struct ifnet *ifp, struct in_ifaddr *ia_if, int own) { struct ip_moptions *imo = &sc->sc_imo; struct carp_if *ocif = ifp->if_carp; int error; KKASSERT(vha->vha_ia != NULL); KASSERT(ia_if != NULL, ("NULL backing address")); KASSERT(vha->vha_iaback == NULL, ("%p is already activated", vha)); KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0, ("inactive vhaddr %p is the address owner", vha)); KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp, ("%s is already on %s", sc->sc_if.if_xname, sc->sc_carpdev->if_xname)); if (ocif == NULL) { KASSERT(sc->sc_carpdev == NULL, ("%s is already on %s", sc->sc_if.if_xname, sc->sc_carpdev->if_xname)); error = ifpromisc(ifp, 1); if (error) return error; } else { struct carp_softc_container *scc; TAILQ_FOREACH(scc, ocif, scc_link) { struct carp_softc *vr = scc->scc_softc; if (vr != sc && vr->sc_vhid == sc->sc_vhid) return EINVAL; } } ifp->if_carp = carp_if_insert(ocif, sc); KASSERT(ifp->if_carp != NULL, ("%s carp_if_insert failed", __func__)); sc->sc_ia = ia_if; sc->sc_carpdev = ifp; sc->arpcom.ac_if.if_hwassist = 0; if (sc->arpcom.ac_if.if_capenable & IFCAP_TXCSUM) { sc->arpcom.ac_if.if_hwassist |= (ifp->if_hwassist & (CSUM_IP | CSUM_UDP | CSUM_TCP)); } if (sc->arpcom.ac_if.if_capenable & IFCAP_TSO) sc->arpcom.ac_if.if_hwassist |= (ifp->if_hwassist & CSUM_TSO); /* * Make sure that all protocol threads see the sc_carpdev and * if_carp changes */ netmsg_service_sync(); if (ocif != NULL && ifp->if_carp != ocif) { /* * The old carp list could be safely free now, * since no one can access it. */ carp_if_free(ocif); } vha->vha_iaback = ia_if; sc->sc_naddrs++; if (own) { vha->vha_flags |= CARP_VHAF_OWNER; /* XXX save user configured advskew? */ sc->sc_advskew = 0; } carp_addroute_vhaddr(sc, vha); /* * Join the multicast group only after the backing interface * has been hooked with the CARP interface. */ KASSERT(imo->imo_multicast_ifp == NULL || imo->imo_multicast_ifp == &sc->sc_if, ("%s didn't leave mcast group on %s", sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname)); if (imo->imo_num_memberships == 0) { struct in_addr addr; addr.s_addr = htonl(INADDR_CARP_GROUP); imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if); if (imo->imo_membership[0] == NULL) { carp_deactivate_vhaddr(sc, vha, FALSE); return ENOBUFS; } imo->imo_num_memberships++; imo->imo_multicast_ifp = &sc->sc_if; imo->imo_multicast_ttl = CARP_DFLTTL; imo->imo_multicast_loop = 0; } carp_hmac_prepare(sc); carp_set_state(sc, INIT); carp_setrun(sc, 0); return 0; } static void carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, boolean_t del_iaback) { KKASSERT(vha->vha_ia != NULL); carp_hmac_prepare(sc); if (vha->vha_iaback == NULL) { KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0, ("inactive vhaddr %p is the address owner", vha)); return; } vha->vha_flags &= ~CARP_VHAF_OWNER; carp_delroute_vhaddr(sc, vha, del_iaback); KKASSERT(sc->sc_naddrs > 0); vha->vha_iaback = NULL; sc->sc_naddrs--; if (!sc->sc_naddrs) { if (sc->sc_naddrs6) { carp_multicast_cleanup(sc); sc->sc_ia = NULL; } else { carp_detach(sc, FALSE, del_iaback); } } } static void carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if) { struct carp_vhaddr *vha; struct in_ifaddr *ia_if; KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET); ia_if = ifatoia(ifa_if); /* * Test each inactive vhaddr against the newly added address. * If the newly added address could be the backing address, * then activate the matching vhaddr. */ TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { const struct in_ifaddr *ia; int own; if (vha->vha_iaback != NULL) continue; ia = vha->vha_ia; if (ia->ia_subnetmask != ia_if->ia_subnetmask || ia->ia_subnet != ia_if->ia_subnet) continue; own = 0; if (ia->ia_addr.sin_addr.s_addr == ia_if->ia_addr.sin_addr.s_addr) own = 1; carp_activate_vhaddr(sc, vha, ifp, ia_if, own); } } static void carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if) { struct carp_vhaddr *vha; struct in_ifaddr *ia_if; KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET); ia_if = ifatoia(ifa_if); /* * Ad src address is deleted; set it to NULL. * Following loop will try pick up a new ad src address * if one of the vhaddr could retain its backing address. */ if (sc->sc_ia == ia_if) sc->sc_ia = NULL; /* * Test each active vhaddr against the deleted address. * If the deleted address is vhaddr address's backing * address, then deactivate the vhaddr. */ TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { if (vha->vha_iaback == NULL) continue; if (vha->vha_iaback == ia_if) carp_deactivate_vhaddr(sc, vha, TRUE); else if (sc->sc_ia == NULL) sc->sc_ia = vha->vha_iaback; } } static void carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del) { struct carp_vhaddr *vha; KKASSERT(sc->sc_carpdev == NULL); TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) carp_config_vhaddr(sc, vha, ifatoia(ifa_del)); } static void carp_ifaddr(void *arg __unused, struct ifnet *ifp, enum ifaddr_event event, struct ifaddr *ifa) { struct carp_softc *sc; if (ifa->ifa_addr->sa_family != AF_INET) return; ASSERT_NETISR0; if (ifp->if_type == IFT_CARP) { /* * Address is changed on carp(4) interface */ switch (event) { case IFADDR_EVENT_ADD: carp_add_addr(ifp->if_softc, ifa); break; case IFADDR_EVENT_CHANGE: carp_config_addr(ifp->if_softc, ifa); break; case IFADDR_EVENT_DELETE: carp_del_addr(ifp->if_softc, ifa); break; } return; } /* * Address is changed on non-carp(4) interface */ if ((ifp->if_flags & IFF_MULTICAST) == 0) return; LIST_FOREACH(sc, &carpif_list, sc_next) { if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) { /* Not the parent iface; skip */ continue; } switch (event) { case IFADDR_EVENT_ADD: carp_link_addrs(sc, ifp, ifa); break; case IFADDR_EVENT_DELETE: if (sc->sc_carpdev != NULL) { carp_unlink_addrs(sc, ifp, ifa); if (sc->sc_carpdev == NULL) { /* * We no longer have the parent * interface, however, certain * virtual addresses, which are * not used because they can't * match the previous parent * interface's addresses, may now * match different interface's * addresses. */ carp_update_addrs(sc, ifa); } } else { /* * The carp(4) interface didn't have a * parent iface, so it is not possible * that it will contain any address to * be unlinked. */ } break; case IFADDR_EVENT_CHANGE: if (sc->sc_carpdev == NULL) { /* * The carp(4) interface didn't have a * parent iface, so it is not possible * that it will contain any address to * be updated. */ carp_link_addrs(sc, ifp, ifa); } else { /* * First try breaking tie with the old * address. Then see whether we could * link certain vhaddr to the new address. * If that fails, i.e. carpdev is NULL, * we try a global update. * * NOTE: The above order is critical. */ carp_unlink_addrs(sc, ifp, ifa); carp_link_addrs(sc, ifp, ifa); if (sc->sc_carpdev == NULL) { /* * See the comment in the above * IFADDR_EVENT_DELETE block. */ carp_update_addrs(sc, NULL); } } break; } } } void carp_proto_ctlinput(netmsg_t msg) { int cmd = msg->ctlinput.nm_cmd; struct sockaddr *sa = msg->ctlinput.nm_arg; struct in_ifaddr_container *iac; /* We only process PRC_IFDOWN and PRC_IFUP commands */ if (cmd != PRC_IFDOWN && cmd != PRC_IFUP) goto done; TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) { struct in_ifaddr *ia = iac->ia; struct ifnet *ifp = ia->ia_ifp; if (ifp->if_type == IFT_CARP) continue; if (ia->ia_ifa.ifa_addr == sa) { if (cmd == PRC_IFDOWN) { carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE, &ia->ia_ifa); } else if (cmd == PRC_IFUP) { carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD, &ia->ia_ifa); } break; } } done: lwkt_replymsg(&msg->lmsg, 0); } struct ifnet * carp_parent(struct ifnet *cifp) { struct carp_softc *sc; KKASSERT(cifp->if_type == IFT_CARP); sc = cifp->if_softc; return sc->sc_carpdev; } #define rtinitflags(x) \ (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \ ? RTF_HOST : 0) static int carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha) { struct in_ifaddr *ia, *iaback; if (sc->sc_state != MASTER) return 0; ia = vha->vha_ia; KKASSERT(ia != NULL); iaback = vha->vha_iaback; KKASSERT(iaback != NULL); return rtchange(&iaback->ia_ifa, &ia->ia_ifa); } static void carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, boolean_t del_iaback) { struct in_ifaddr *ia, *iaback; ia = vha->vha_ia; KKASSERT(ia != NULL); iaback = vha->vha_iaback; KKASSERT(iaback != NULL); if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) { rtchange(&ia->ia_ifa, &iaback->ia_ifa); return; } rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia)); in_ifadown_force(&ia->ia_ifa, 1); ia->ia_flags &= ~IFA_ROUTE; } static int carp_modevent(module_t mod, int type, void *data) { switch (type) { case MOD_LOAD: LIST_INIT(&carpif_list); carp_ifdetach_event = EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY); carp_ifaddr_event = EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL, EVENTHANDLER_PRI_FIRST); if_clone_attach(&carp_cloner); break; case MOD_UNLOAD: EVENTHANDLER_DEREGISTER(ifnet_detach_event, carp_ifdetach_event); EVENTHANDLER_DEREGISTER(ifaddr_event, carp_ifaddr_event); if_clone_detach(&carp_cloner); break; default: return (EINVAL); } return (0); } static moduledata_t carp_mod = { "carp", carp_modevent, 0 }; DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); |