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
|
package network
import (
"context"
"database/sql"
"errors"
"fmt"
"io/fs"
"maps"
"net"
"net/http"
"os"
"os/exec"
"slices"
"strconv"
"strings"
"time"
"github.com/mdlayher/netx/eui64"
incus "github.com/lxc/incus/v6/client"
"github.com/lxc/incus/v6/internal/server/apparmor"
"github.com/lxc/incus/v6/internal/server/cluster"
"github.com/lxc/incus/v6/internal/server/cluster/request"
"github.com/lxc/incus/v6/internal/server/daemon"
"github.com/lxc/incus/v6/internal/server/db"
dbCluster "github.com/lxc/incus/v6/internal/server/db/cluster"
"github.com/lxc/incus/v6/internal/server/db/warningtype"
"github.com/lxc/incus/v6/internal/server/dnsmasq"
"github.com/lxc/incus/v6/internal/server/dnsmasq/dhcpalloc"
firewallDrivers "github.com/lxc/incus/v6/internal/server/firewall/drivers"
"github.com/lxc/incus/v6/internal/server/ip"
"github.com/lxc/incus/v6/internal/server/network/acl"
"github.com/lxc/incus/v6/internal/server/project"
localUtil "github.com/lxc/incus/v6/internal/server/util"
"github.com/lxc/incus/v6/internal/server/warnings"
internalUtil "github.com/lxc/incus/v6/internal/util"
"github.com/lxc/incus/v6/internal/version"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/revert"
"github.com/lxc/incus/v6/shared/subprocess"
"github.com/lxc/incus/v6/shared/util"
"github.com/lxc/incus/v6/shared/validate"
)
// Default MTU for bridge interface.
const bridgeMTUDefault = 1500
// bridge represents a bridge network.
type bridge struct {
common
}
// DBType returns the network type DB ID.
func (n *bridge) DBType() db.NetworkType {
return db.NetworkTypeBridge
}
// Config returns the network driver info.
func (n *bridge) Info() Info {
info := n.common.Info()
info.AddressForwards = true
return info
}
// checkClusterWideMACSafe returns whether it is safe to use the same MAC address for the bridge interface on all
// cluster nodes. It is not suitable to use a static MAC address when "bridge.external_interfaces" is non-empty and
// the bridge interface has no IPv4 or IPv6 address set. This is because in a clustered environment the same bridge
// config is applied to all nodes, and if the bridge is being used to connect multiple nodes to the same network
// segment it would cause MAC conflicts to use the same MAC on all nodes. If an IP address is specified then
// connecting multiple nodes to the same network segment would also cause IP conflicts, so if an IP is defined
// then we assume this is not being done. However if IP addresses are explicitly set to "none" and
// "bridge.external_interfaces" is set then it may not be safe to use a the same MAC address on all nodes.
func (n *bridge) checkClusterWideMACSafe(config map[string]string) error {
// We can't be sure that multiple clustered nodes aren't connected to the same network segment so don't
// use a static MAC address for the bridge interface to avoid introducing a MAC conflict.
if config["bridge.external_interfaces"] != "" && config["ipv4.address"] == "none" && config["ipv6.address"] == "none" {
return errors.New(`Cannot use static "bridge.hwaddr" MAC address when bridge has no IP addresses and has external interfaces set`)
}
// We may have MAC conflicts if tunnels are in use.
for k := range config {
if strings.HasPrefix(k, "tunnel.") {
return errors.New(`Cannot use static "bridge.hwaddr" MAC address when bridge has tunnels connected`)
}
}
// If using a generated IPv6 address, we need a unique MAC.
if config["ipv6.address"] != "none" && validate.IsNetworkV6(config["ipv6.address"]) == nil {
return errors.New(`Cannot use static "bridge.hwaddr" MAC address when bridge uses a host-specific IPv6 address`)
}
return nil
}
// FillConfig fills requested config with any default values.
func (n *bridge) FillConfig(config map[string]string) error {
// Set some default values where needed.
if config["ipv4.address"] == "" {
config["ipv4.address"] = "auto"
}
if config["ipv4.address"] == "auto" && config["ipv4.nat"] == "" {
config["ipv4.nat"] = "true"
}
if config["ipv6.address"] == "" {
content, err := os.ReadFile("/proc/sys/net/ipv6/conf/default/disable_ipv6")
if err == nil && string(content) == "0\n" {
config["ipv6.address"] = "auto"
}
}
if config["ipv6.address"] == "auto" && config["ipv6.nat"] == "" {
config["ipv6.nat"] = "true"
}
// Now replace any "auto" keys with generated values.
err := n.populateAutoConfig(config)
if err != nil {
return fmt.Errorf("Failed generating auto config: %w", err)
}
return nil
}
// populateAutoConfig replaces "auto" in config with generated values.
func (n *bridge) populateAutoConfig(config map[string]string) error {
changedConfig := false
// Now populate "auto" values where needed.
if config["ipv4.address"] == "auto" {
subnet, err := randomSubnetV4()
if err != nil {
return err
}
config["ipv4.address"] = subnet
changedConfig = true
}
if config["ipv6.address"] == "auto" {
subnet, err := randomSubnetV6()
if err != nil {
return err
}
config["ipv6.address"] = subnet
changedConfig = true
}
// Re-validate config if changed.
if changedConfig && n.state != nil {
return n.Validate(config, request.ClientTypeNormal)
}
return nil
}
// ValidateName validates network name.
func (n *bridge) ValidateName(name string) error {
err := validate.IsInterfaceName(name)
if err != nil {
return err
}
// Apply common name validation that applies to all network types.
return n.common.ValidateName(name)
}
// Validate network config.
func (n *bridge) Validate(config map[string]string, clientType request.ClientType) error {
// Build driver specific rules dynamically.
rules := map[string]func(value string) error{
// gendoc:generate(entity=network_bridge, group=common, key=bgp.ipv4.nexthop)
//
// ---
// type: string
// condition: BGP server
// default: local address
// shortdesc: Override the next-hop for advertised prefixes
"bgp.ipv4.nexthop": validate.Optional(validate.IsNetworkAddressV4),
// gendoc:generate(entity=network_bridge, group=common, key=bgp.ipv6.nexthop)
//
// ---
// type: string
// condition: BGP server
// default: local address
// shortdesc: Override the next-hop for advertised prefixes
"bgp.ipv6.nexthop": validate.Optional(validate.IsNetworkAddressV6),
// gendoc:generate(entity=network_bridge, group=common, key=bridge.driver)
//
// ---
// type: string
// condition: -
// default: `native`
// shortdesc: Bridge driver: `native` or `openvswitch`
"bridge.driver": validate.Optional(validate.IsOneOf("native", "openvswitch")),
// gendoc:generate(entity=network_bridge, group=common, key=bridge.external_interfaces)
//
// ---
// type: string
// condition: -
// default: -
// shortdesc: Comma-separated list of unconfigured network interfaces to include in the bridge
"bridge.external_interfaces": validate.Optional(validateExternalInterfaces),
// gendoc:generate(entity=network_bridge, group=common, key=bridge.hwaddr)
//
// ---
// type: string
// condition: -
// default: -
// shortdesc: MAC address for the bridge
"bridge.hwaddr": validate.Optional(validate.IsNetworkMAC),
// gendoc:generate(entity=network_bridge, group=common, key=bridge.mtu)
//
// ---
// type: integer
// condition: -
// default: `1500`
// shortdesc: Bridge MTU (default varies if tunnel in use)
"bridge.mtu": validate.Optional(validate.IsNetworkMTU),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.address)
//
// ---
// type: string
// condition: standard mode
// default: - (initial value on creation: `auto`)
// shortdesc: IPv4 address for the bridge (use `none` to turn off IPv4 or `auto` to generate a new random unused subnet) (CIDR)
"ipv4.address": validate.Optional(func(value string) error {
if validate.IsOneOf("none", "auto")(value) == nil {
return nil
}
return validate.IsNetworkAddressCIDRV4(value)
}),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.firewall)
//
// ---
// type: bool
// condition: IPv4 address
// default: `true`
// shortdesc: Whether to generate filtering firewall rules for this network
"ipv4.firewall": validate.Optional(validate.IsBool),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.nat)
//
// ---
// type: bool
// condition: IPv4 address
// default: `false`(initial value on creation if `ipv4.address` is set to `auto`: `true`)
// shortdesc: Whether to NAT
"ipv4.nat": validate.Optional(validate.IsBool),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.nat.order)
//
// ---
// type: string
// condition: IPv4 address
// default: `before`
// shortdesc: Whether to add the required NAT rules before or after any pre-existing rules
"ipv4.nat.order": validate.Optional(validate.IsOneOf("before", "after")),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.nat.address)
//
// ---
// type: string
// condition: IPv4 address
// default: -
// shortdesc: The source address used for outbound traffic from the bridge
"ipv4.nat.address": validate.Optional(validate.IsNetworkAddressV4),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.dhcp)
//
// ---
// type: bool
// condition: IPv4 address
// default: `true`
// shortdesc: Whether to allocate addresses using DHCP
"ipv4.dhcp": validate.Optional(validate.IsBool),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.dhcp.gateway)
//
// ---
// type: string
// condition: IPv4 DHCP
// default: IPv4 address
// shortdesc: Address of the gateway for the subnet
"ipv4.dhcp.gateway": validate.Optional(validate.IsNetworkAddressV4),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.dhcp.expiry)
//
// ---
// type: string
// condition: IPv4 DHCP
// default: `1h`
// shortdesc: When to expire DHCP leases
"ipv4.dhcp.expiry": validate.IsAny,
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.dhcp.ranges)
//
// ---
// type: string
// condition: IPv4 DHCP
// default: all addresses
// shortdesc: Comma-separated list of IP ranges to use for DHCP (FIRST-LAST format)
"ipv4.dhcp.ranges": validate.Optional(validate.IsListOf(validate.IsNetworkRangeV4)),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.dhcp.routes)
//
// ---
// type: string
// condition: IPv4 DHCP
// default: -
// shortdesc: Static routes to provide via DHCP option 121, as a comma-separated list of alternating subnets (CIDR) and gateway addresses (same syntax as dnsmasq)
"ipv4.dhcp.routes": validate.Optional(validate.IsDHCPRouteList),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.routes)
//
// ---
// type: string
// condition: IPv4 address
// default: -
// shortdesc: Comma-separated list of additional IPv4 CIDR subnets to route to the bridge
"ipv4.routes": validate.Optional(validate.IsListOf(validate.IsNetworkV4)),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.routing)
//
// ---
// type: bool
// condition: IPv4 DHCP
// default: `true`
// shortdesc: Whether to route traffic in and out of the bridge
"ipv4.routing": validate.Optional(validate.IsBool),
// gendoc:generate(entity=network_bridge, group=common, key=ipv4.ovn.ranges)
//
// ---
// type: string
// condition: -
// default: -
// shortdesc: Comma-separated list of IPv4 ranges to use for child OVN network routers (FIRST-LAST format)
"ipv4.ovn.ranges": validate.Optional(validate.IsListOf(validate.IsNetworkRangeV4)),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.address)
//
// ---
// type: string
// condition: standard mode
// default: - (initial value on creation: `auto`)
// shortdesc: IPv6 address for the bridge (use `none` to turn off IPv6 or `auto` to generate a new random unused subnet) (CIDR)
"ipv6.address": validate.Optional(func(value string) error {
if validate.IsOneOf("none", "auto")(value) == nil {
return nil
}
return validate.Or(validate.IsNetworkAddressCIDRV6, validate.IsNetworkV6)(value)
}),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.firewall)
//
// ---
// type: bool
// condition: IPv6 address
// default: `true`
// shortdesc: Whether to generate filtering firewall rules for this network
"ipv6.firewall": validate.Optional(validate.IsBool),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.nat)
//
// ---
// type: bool
// condition: IPv6 address
// default: `false` (initial value on creation if `ipv6.address` is set to `auto`: `true`)
// shortdesc: Whether to NAT
"ipv6.nat": validate.Optional(validate.IsBool),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.nat.order)
//
// ---
// type: string
// condition: IPv6 address
// default: `before`
// shortdesc: Whether to add the required NAT rules before or after any pre-existing rules
"ipv6.nat.order": validate.Optional(validate.IsOneOf("before", "after")),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.nat.address)
//
// ---
// type: string
// condition: IPv6 address
// default: -
// shortdesc: The source address used for outbound traffic from the bridge
"ipv6.nat.address": validate.Optional(validate.IsNetworkAddressV6),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.dhcp)
//
// ---
// type: bool
// condition: IPv6 DHCP
// default: `true`
// shortdesc: Whether to provide additional network configuration over DHCP
"ipv6.dhcp": validate.Optional(validate.IsBool),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.dhcp.expiry)
//
// ---
// type: string
// condition: IPv6 DHCP
// default: `1h`
// shortdesc: When to expire DHCP leases
"ipv6.dhcp.expiry": validate.IsAny,
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.dhcp.stateful)
//
// ---
// type: bool
// condition: IPv6 DHCP
// default: `false`
// shortdesc: Whether to allocate addresses using DHCP
"ipv6.dhcp.stateful": validate.Optional(validate.IsBool),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.dhcp.ranges)
//
// ---
// type: string
// condition: IPv6 stateful DHCP
// default: all addresses
// shortdesc: Comma-separated list of IPv6 ranges to use for DHCP (FIRST-LAST format)
"ipv6.dhcp.ranges": validate.Optional(validate.IsListOf(validate.IsNetworkRangeV6)),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.routes)
//
// ---
// type: string
// condition: IPv6 address
// default: -
// shortdesc: Comma-separated list of additional IPv6 CIDR subnets to route to the bridge
"ipv6.routes": validate.Optional(validate.IsListOf(validate.IsNetworkV6)),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.routing)
//
// ---
// type: bool
// condition: IPv6 address
// default: `true`
// shortdesc: Whether to route traffic in and out of the bridge
"ipv6.routing": validate.Optional(validate.IsBool),
// gendoc:generate(entity=network_bridge, group=common, key=ipv6.ovn.ranges)
//
// ---
// type: string
// condition: -
// default: -
// shortdesc: Comma-separated list of IPv6 ranges to use for child OVN network routers (FIRST-LAST format)
"ipv6.ovn.ranges": validate.Optional(validate.IsListOf(validate.IsNetworkRangeV6)),
// gendoc:generate(entity=network_bridge, group=common, key=dns.nameservers)
//
// ---
// type: string
// condition: -
// default: IPv4 and IPv6 address
// shortdesc: DNS server IPs to advertise to DHCP clients and via Router Advertisements. Both IPv4 and IPv6 addresses get pushed via DHCP, and IPv6 addresses are also advertised as RDNSS via RA.
"dns.nameservers": validate.Optional(validate.IsListOf(validate.IsNetworkAddress)),
// gendoc:generate(entity=network_bridge, group=common, key=dns.domain)
//
// ---
// type: string
// condition: -
// default: `incus`
// shortdesc: Domain to advertise to DHCP clients and use for DNS resolution
"dns.domain": validate.IsAny,
// gendoc:generate(entity=network_bridge, group=common, key=dns.mode)
//
// ---
// type: string
// condition: -
// default: `managed`
// shortdesc: DNS registration mode: none for no DNS record, managed for Incus-generated static records or dynamic for client-generated records
"dns.mode": validate.Optional(validate.IsOneOf("dynamic", "managed", "none")),
// gendoc:generate(entity=network_bridge, group=common, key=dns.search)
//
// ---
// type: string
// condition: -
// default: -
// shortdesc: Full comma-separated domain search list, defaulting to `dns.domain` value
"dns.search": validate.IsAny,
// gendoc:generate(entity=network_bridge, group=common, key=dns.zone.forward)
//
// ---
// type: string
// condition: -
// default: `managed`
// shortdesc: Comma-separated list of DNS zone names for forward DNS records
"dns.zone.forward": validate.IsAny,
// gendoc:generate(entity=network_bridge, group=common, key=dns.zone.reverse.ipv4)
//
// ---
// type: string
// condition: -
// default: `managed`
// shortdesc: DNS zone name for IPv4 reverse DNS records
"dns.zone.reverse.ipv4": validate.IsAny,
// gendoc:generate(entity=network_bridge, group=common, key=dns.zone.reverse.ipv6)
//
// ---
// type: string
// condition: -
// default: `managed`
// shortdesc: DNS zone name for IPv6 reverse DNS records
"dns.zone.reverse.ipv6": validate.IsAny,
// gendoc:generate(entity=network_bridge, group=common, key=raw.dnsmasq)
//
// ---
// type: string
// condition: -
// default: -
// shortdesc: Additional dnsmasq configuration to append to the configuration file
"raw.dnsmasq": validate.IsAny,
// gendoc:generate(entity=network_bridge, group=common, key=security.acls)
//
// ---
// type: string
// condition: -
// default: -
// shortdesc: Comma-separated list of Network ACLs to apply to NICs connected to this network (see {ref}`network-acls-bridge-limitations`)
"security.acls": validate.IsAny,
// gendoc:generate(entity=network_bridge, group=common, key=security.acls.default.ingress.action)
//
// ---
// type: string
// condition: `security.acls`
// default: `reject`
// shortdesc: Action to use for ingress traffic that doesn't match any ACL rule
"security.acls.default.ingress.action": validate.Optional(validate.IsOneOf(acl.ValidActions...)),
// gendoc:generate(entity=network_bridge, group=common, key=security.acls.default.egress.action)
//
// ---
// type: string
// condition: `security.acls`
// default: `reject`
// shortdesc: Action to use for egress traffic that doesn't match any ACL rule
"security.acls.default.egress.action": validate.Optional(validate.IsOneOf(acl.ValidActions...)),
// gendoc:generate(entity=network_bridge, group=common, key=security.acls.default.ingress.logged)
//
// ---
// type: bool
// condition: `security.acls`
// default: `false`
// shortdesc: Whether to log ingress traffic that doesn't match any ACL rule
"security.acls.default.ingress.logged": validate.Optional(validate.IsBool),
// gendoc:generate(entity=network_bridge, group=common, key=security.acls.default.egress.logged)
//
// ---
// type: bool
// condition: `security.acls`
// default: `false`
// shortdesc: Whether to log egress traffic that doesn't match any ACL rule
"security.acls.default.egress.logged": validate.Optional(validate.IsBool),
}
// Add dynamic validation rules.
for k := range config {
// Tunnel keys have the remote name in their name, extract the suffix.
if strings.HasPrefix(k, "tunnel.") {
// Validate remote name in key.
fields := strings.Split(k, ".")
if len(fields) != 3 {
return fmt.Errorf("Invalid network configuration key: %s", k)
}
if len(n.name)+len(fields[1]) > 14 {
return fmt.Errorf("Network name too long for tunnel interface: %s-%s", n.name, fields[1])
}
tunnelKey := fields[2]
// Add the correct validation rule for the dynamic field based on last part of key.
switch tunnelKey {
case "protocol":
// gendoc:generate(entity=network_bridge, group=common, key=tunnel.NAME.protocol)
//
// ---
// type: string
// condition: standard mode
// default: -
// shortdesc: Tunneling protocol: `vxlan` or `gre`
rules[k] = validate.Optional(validate.IsOneOf("gre", "vxlan"))
case "local":
// gendoc:generate(entity=network_bridge, group=common, key=tunnel.NAME.local)
//
// ---
// type: string
// condition: `gre` or `vxlan`
// default: -
// shortdesc: Local address for the tunnel (not necessary for multicast `vxlan`)
rules[k] = validate.Optional(validate.IsNetworkAddress)
case "remote":
// gendoc:generate(entity=network_bridge, group=common, key=tunnel.NAME.remote)
//
// ---
// type: string
// condition: `gre` or `vxlan`
// default: -
// shortdesc: Remote address for the tunnel (not necessary for multicast `vxlan`)
rules[k] = validate.Optional(validate.IsNetworkAddress)
case "port":
// gendoc:generate(entity=network_bridge, group=common, key=tunnel.NAME.port)
//
// ---
// type: integer
// condition: `vxlan`
// default: `0`
// shortdesc: Specific port to use for the `vxlan` tunnel
rules[k] = networkValidPort
case "group":
// gendoc:generate(entity=network_bridge, group=common, key=tunnel.NAME.group)
//
// ---
// type: string
// condition: `vxlan`
// default: `239.0.0.1`
// shortdesc: Multicast address for `vxlan` (used if local and remote aren't set)
rules[k] = validate.Optional(validate.IsNetworkAddress)
case "id":
// gendoc:generate(entity=network_bridge, group=common, key=tunnel.NAME.id)
//
// ---
// type: integer
// condition: `vxlan`
// default: `0`
// shortdesc: Specific tunnel ID to use for the `vxlan` tunnel
rules[k] = validate.Optional(validate.IsInt64)
case "interface":
// gendoc:generate(entity=network_bridge, group=common, key=tunnel.NAME.interface)
//
// ---
// type: string
// condition: `vxlan`
// default: -
// shortdesc: Specific host interface to use for the tunnel
rules[k] = validate.IsInterfaceName
case "ttl":
// gendoc:generate(entity=network_bridge, group=common, key=tunnel.NAME.ttl)
//
// ---
// type: integer
// condition: `vxlan`
// default: `1`
// shortdesc: Specific TTL to use for multicast routing topologies
rules[k] = validate.Optional(validate.IsUint8)
}
}
}
// gendoc:generate(entity=network_bridge, group=bgp, key=bgp.peers.NAME.address)
//
// ---
// type: string
// condition: BGP server
// defaultdesc: -
// shortdesc: Peer address (IPv4 or IPv6) for use by `ovn` downstream networks
// gendoc:generate(entity=network_bridge, group=bgp, key=bgp.peers.NAME.asn)
//
// ---
// type: integer
// condition: BGP server
// defaultdesc: -
// shortdesc: Peer AS number for use by `ovn` downstream networks
// gendoc:generate(entity=network_bridge, group=bgp, key=bgp.peers.NAME.password)
//
// ---
// type: string
// condition: BGP server
// defaultdesc: - (no password)
// shortdesc: Peer session password (optional) for use by `ovn` downstream networks
// gendoc:generate(entity=network_bridge, group=bgp, key=bgp.peers.NAME.holdtime)
//
// ---
// type: integer
// condition: BGP server
// defaultdesc: `180`
// shortdesc: Peer session hold time (in seconds; optional)
// Add the BGP validation rules.
bgpRules, err := n.bgpValidationRules(config)
if err != nil {
return err
}
maps.Copy(rules, bgpRules)
// gendoc:generate(entity=network_bridge, group=common, key=user.*)
//
// ---
// type: string
// condition: -
// default: -
// shortdesc: User-provided free-form key/value pairs
// Validate the configuration.
err = n.validate(config, rules)
if err != nil {
return err
}
// Perform composite key checks after per-key validation.
// Validate DNS zone names.
err = n.validateZoneNames(config)
if err != nil {
return err
}
for k, v := range config {
key := k
// MTU checks
if key == "bridge.mtu" && v != "" {
mtu, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return fmt.Errorf("Invalid value for an integer: %s", v)
}
ipv6 := config["ipv6.address"]
if ipv6 != "" && ipv6 != "none" && mtu < 1280 {
return errors.New("The minimum MTU for an IPv6 network is 1280")
}
ipv4 := config["ipv4.address"]
if ipv4 != "" && ipv4 != "none" && mtu < 68 {
return errors.New("The minimum MTU for an IPv4 network is 68")
}
}
}
// Check using same MAC address on every cluster node is safe.
if config["bridge.hwaddr"] != "" {
err = n.checkClusterWideMACSafe(config)
if err != nil {
return err
}
}
// Check IPv4 OVN ranges.
if config["ipv4.ovn.ranges"] != "" && util.IsTrueOrEmpty(config["ipv4.dhcp"]) {
dhcpSubnet := n.DHCPv4Subnet()
allowedNets := []*net.IPNet{}
if dhcpSubnet != nil {
if config["ipv4.dhcp.ranges"] == "" {
return errors.New(`"ipv4.ovn.ranges" must be used in conjunction with non-overlapping "ipv4.dhcp.ranges" when DHCPv4 is enabled`)
}
allowedNets = append(allowedNets, dhcpSubnet)
}
ovnRanges, err := parseIPRanges(config["ipv4.ovn.ranges"], allowedNets...)
if err != nil {
return fmt.Errorf("Failed parsing ipv4.ovn.ranges: %w", err)
}
dhcpRanges, err := parseIPRanges(config["ipv4.dhcp.ranges"], allowedNets...)
if err != nil {
return fmt.Errorf("Failed parsing ipv4.dhcp.ranges: %w", err)
}
for _, ovnRange := range ovnRanges {
for _, dhcpRange := range dhcpRanges {
if IPRangesOverlap(ovnRange, dhcpRange) {
return fmt.Errorf(`The range specified in "ipv4.ovn.ranges" (%q) cannot overlap with "ipv4.dhcp.ranges"`, ovnRange)
}
}
}
}
// Check IPv6 OVN ranges.
if config["ipv6.ovn.ranges"] != "" && util.IsTrueOrEmpty(config["ipv6.dhcp"]) {
dhcpSubnet := n.DHCPv6Subnet()
allowedNets := []*net.IPNet{}
if dhcpSubnet != nil {
if config["ipv6.dhcp.ranges"] == "" && util.IsTrue(config["ipv6.dhcp.stateful"]) {
return errors.New(`"ipv6.ovn.ranges" must be used in conjunction with non-overlapping "ipv6.dhcp.ranges" when stateful DHCPv6 is enabled`)
}
allowedNets = append(allowedNets, dhcpSubnet)
}
ovnRanges, err := parseIPRanges(config["ipv6.ovn.ranges"], allowedNets...)
if err != nil {
return fmt.Errorf("Failed parsing ipv6.ovn.ranges: %w", err)
}
// If stateful DHCPv6 is enabled, check OVN ranges don't overlap with DHCPv6 stateful ranges.
// Otherwise SLAAC will be being used to generate client IPs and predefined ranges aren't used.
if dhcpSubnet != nil && util.IsTrue(config["ipv6.dhcp.stateful"]) {
dhcpRanges, err := parseIPRanges(config["ipv6.dhcp.ranges"], allowedNets...)
if err != nil {
return fmt.Errorf("Failed parsing ipv6.dhcp.ranges: %w", err)
}
for _, ovnRange := range ovnRanges {
for _, dhcpRange := range dhcpRanges {
if IPRangesOverlap(ovnRange, dhcpRange) {
return fmt.Errorf(`The range specified in "ipv6.ovn.ranges" (%q) cannot overlap with "ipv6.dhcp.ranges"`, ovnRange)
}
}
}
}
}
// Check Security ACLs are supported and exist.
if config["security.acls"] != "" {
err = acl.Exists(n.state, n.Project(), util.SplitNTrimSpace(config["security.acls"], ",", -1, true)...)
if err != nil {
return err
}
}
return nil
}
// Create checks whether the bridge interface name is used already.
func (n *bridge) Create(clientType request.ClientType) error {
n.logger.Debug("Create", logger.Ctx{"clientType": clientType, "config": n.config})
if InterfaceExists(n.name) {
return fmt.Errorf("Network interface %q already exists", n.name)
}
return nil
}
// isRunning returns whether the network is up.
func (n *bridge) isRunning() bool {
return InterfaceExists(n.name)
}
// Delete deletes a network.
func (n *bridge) Delete(clientType request.ClientType) error {
n.logger.Debug("Delete", logger.Ctx{"clientType": clientType})
if n.isRunning() {
err := n.Stop()
if err != nil {
return err
}
}
// Clean up extended external interfaces.
if n.config["bridge.external_interfaces"] != "" {
for _, entry := range strings.Split(n.config["bridge.external_interfaces"], ",") {
entry = strings.TrimSpace(entry)
entryParts := strings.Split(entry, "/")
if len(entryParts) == 3 {
ifName := strings.TrimSpace(entryParts[0])
_, err := net.InterfaceByName(ifName)
if err == nil {
err = InterfaceRemove(ifName)
if err != nil {
return err
}
}
}
}
}
// Delete apparmor profiles.
err := apparmor.NetworkDelete(n.state.OS, n)
if err != nil {
return err
}
return n.common.delete(clientType)
}
// Rename renames a network.
func (n *bridge) Rename(newName string) error {
n.logger.Debug("Rename", logger.Ctx{"newName": newName})
if InterfaceExists(newName) {
return fmt.Errorf("Network interface %q already exists", newName)
}
// Bring the network down.
if n.isRunning() {
err := n.Stop()
if err != nil {
return err
}
}
// Rename common steps.
err := n.common.rename(newName)
if err != nil {
return err
}
// Bring the network up.
err = n.Start()
if err != nil {
return err
}
return nil
}
// Start starts the network.
func (n *bridge) Start() error {
n.logger.Debug("Start")
reverter := revert.New()
defer reverter.Fail()
reverter.Add(func() { n.setUnavailable() })
err := n.setup(nil)
if err != nil {
return err
}
reverter.Success()
// Ensure network is marked as available now its started.
n.setAvailable()
return nil
}
// setup restarts the network.
func (n *bridge) setup(oldConfig map[string]string) error {
// If we are in mock mode, just no-op.
if n.state.OS.MockMode {
return nil
}
n.logger.Debug("Setting up network")
reverter := revert.New()
defer reverter.Fail()
// Create directory.
if !util.PathExists(internalUtil.VarPath("networks", n.name)) {
err := os.MkdirAll(internalUtil.VarPath("networks", n.name), 0o711)
if err != nil {
return err
}
}
var err error
// Build up the bridge interface's settings.
bridge := ip.Bridge{
Link: ip.Link{
Name: n.name,
MTU: bridgeMTUDefault,
},
}
// Get a list of tunnels.
tunnels := n.getTunnels()
// Decide the MTU for the bridge interface.
if n.config["bridge.mtu"] != "" {
mtuInt, err := strconv.ParseUint(n.config["bridge.mtu"], 10, 32)
if err != nil {
return fmt.Errorf("Invalid MTU %q: %w", n.config["bridge.mtu"], err)
}
bridge.MTU = uint32(mtuInt)
} else if len(tunnels) > 0 {
bridge.MTU = 1400
}
// Decide the MAC address of bridge interface.
if n.config["bridge.hwaddr"] != "" {
bridge.Address, err = net.ParseMAC(n.config["bridge.hwaddr"])
if err != nil {
return fmt.Errorf("Failed parsing MAC address %q: %w", n.config["bridge.hwaddr"], err)
}
} else {
// If no cluster wide static MAC address set, then generate one.
var seedNodeID int64
if n.checkClusterWideMACSafe(n.config) != nil {
// If not safe to use a cluster wide MAC, then use cluster node's ID to
// generate a stable per-node & network derived random MAC.
seedNodeID = n.state.DB.Cluster.GetNodeID()
} else {
// If safe to use a cluster wide MAC, then use a static cluster node of 0 to generate a
// stable per-network derived random MAC.
seedNodeID = 0
}
// Load server certificate. This is needs to be the same certificate for all nodes in a cluster.
cert, err := internalUtil.LoadCert(n.state.OS.VarDir)
if err != nil {
return err
}
// Generate the random seed, this uses the server certificate fingerprint (to ensure that multiple
// standalone nodes with the same network ID connected to the same external network don't generate
// the same MAC for their networks). It relies on the certificate being the same for all nodes in a
// cluster to allow the same MAC to be generated on each bridge interface in the network when
// seedNodeID is 0 (when safe to do so).
seed := fmt.Sprintf("%s.%d.%d", cert.Fingerprint(), seedNodeID, n.ID())
r, err := localUtil.GetStableRandomGenerator(seed)
if err != nil {
return fmt.Errorf("Failed generating stable random bridge MAC: %w", err)
}
randomHwaddr := randomHwaddr(r)
bridge.Address, err = net.ParseMAC(randomHwaddr)
if err != nil {
return fmt.Errorf("Failed parsing MAC address %q: %w", randomHwaddr, err)
}
n.logger.Debug("Stable MAC generated", logger.Ctx{"seed": seed, "hwAddr": bridge.Address.String()})
}
// Create the bridge interface if doesn't exist.
if !n.isRunning() {
if n.config["bridge.driver"] == "openvswitch" {
vswitch, err := n.state.OVS()
if err != nil {
return fmt.Errorf("Couldn't connect to OpenVSwitch: %v", err)
}
// Add and configure the interface in one operation to reduce the number of executions and
// to avoid systemd-udevd from applying the default MACAddressPolicy=persistent policy.
err = vswitch.CreateBridge(context.TODO(), n.name, false, bridge.Address, bridge.MTU)
if err != nil {
return err
}
reverter.Add(func() { _ = vswitch.DeleteBridge(context.Background(), n.name) })
} else {
// Add and configure the interface in one operation to reduce the number of executions and
// to avoid systemd-udevd from applying the default MACAddressPolicy=persistent policy.
err := bridge.Add()
if err != nil {
return err
}
reverter.Add(func() { _ = bridge.Delete() })
}
} else {
// If bridge already exists then re-apply settings. If we just created a bridge then we don't
// need to do this as the settings will have been applied as part of the add operation.
// Set the MTU on the bridge interface.
err := bridge.SetMTU(bridge.MTU)
if err != nil {
return err
}
// Set the MAC address on the bridge interface if specified.
if bridge.Address != nil {
err = bridge.SetAddress(bridge.Address)
if err != nil {
return err
}
}
}
// IPv6 bridge configuration.
if !util.IsNoneOrEmpty(n.config["ipv6.address"]) {
if !util.PathExists("/proc/sys/net/ipv6") {
return errors.New("Network has ipv6.address but kernel IPv6 support is missing")
}
err := localUtil.SysctlSet(fmt.Sprintf("net/ipv6/conf/%s/disable_ipv6", n.name), "0")
if err != nil {
return err
}
err = localUtil.SysctlSet(fmt.Sprintf("net/ipv6/conf/%s/autoconf", n.name), "0")
if err != nil {
return err
}
err = localUtil.SysctlSet(fmt.Sprintf("net/ipv6/conf/%s/accept_dad", n.name), "0")
if err != nil {
return err
}
} else {
// Disable IPv6 if no address is specified. This prevents the
// host being reachable over a guessable link-local address as well as it
// auto-configuring an address should an instance operate an IPv6 router.
if util.PathExists("/proc/sys/net/ipv6") {
err := localUtil.SysctlSet(fmt.Sprintf("net/ipv6/conf/%s/disable_ipv6", n.name), "1")
if err != nil {
return err
}
}
}
err = n.deleteChildren()
if err != nil {
return fmt.Errorf("Failed to delete bridge children interfaces: %w", err)
}
// Attempt to add a dummy device to the bridge to force the MTU.
if bridge.MTU != bridgeMTUDefault && n.config["bridge.driver"] != "openvswitch" {
dummy := &ip.Dummy{
Link: ip.Link{
Name: fmt.Sprintf("%s-mtu", n.name),
MTU: bridge.MTU,
},
}
err = dummy.Add()
if err == nil {
reverter.Add(func() { _ = dummy.Delete() })
err = dummy.SetUp()
if err == nil {
_ = AttachInterface(n.state, n.name, fmt.Sprintf("%s-mtu", n.name))
}
}
}
// Enable VLAN filtering for Linux bridges.
if n.config["bridge.driver"] != "openvswitch" {
// Enable filtering.
err = BridgeVLANFilterSetStatus(n.name, "1")
if err != nil {
n.logger.Warn(fmt.Sprintf("Failed enabling VLAN filtering: %v", err))
}
}
// Bring it up.
err = bridge.SetUp()
if err != nil {
return err
}
// Add any listed existing external interface.
if n.config["bridge.external_interfaces"] != "" {
for _, entry := range strings.Split(n.config["bridge.external_interfaces"], ",") {
entry = strings.TrimSpace(entry)
// Test for extended configuration of external interface.
entryParts := strings.Split(entry, "/")
ifParent := ""
vlanID := 0
if len(entryParts) == 3 {
vlanID, err = strconv.Atoi(entryParts[2])
if err != nil || vlanID < 1 || vlanID > 4094 {
vlanID = 0
n.logger.Warn("Ignoring invalid VLAN ID", logger.Ctx{"interface": entry, "vlanID": entryParts[2]})
} else {
entry = strings.TrimSpace(entryParts[0])
ifParent = strings.TrimSpace(entryParts[1])
}
}
iface, err := net.InterfaceByName(entry)
if err != nil {
if vlanID == 0 {
n.logger.Warn("Skipping attaching missing external interface", logger.Ctx{"interface": entry})
continue
}
// If the interface doesn't exist and VLAN ID was provided, create the missing interface.
ok, err := VLANInterfaceCreate(ifParent, entry, strconv.Itoa(vlanID), false)
if ok {
iface, err = net.InterfaceByName(entry)
}
if !ok || err != nil {
return fmt.Errorf("Failed to create external interface %q", entry)
}
} else if vlanID > 0 {
// If the interface exists and VLAN ID was provided, ensure it has the same parent and VLAN ID and is not attached to a different network.
linkInfo, err := ip.LinkByName(entry)
if err != nil {
return fmt.Errorf("Failed to get link info for external interface %q", entry)
}
if linkInfo.Kind != "vlan" || linkInfo.Parent != ifParent || linkInfo.VlanID != vlanID || (linkInfo.Master != "" && linkInfo.Master != n.name) {
return fmt.Errorf("External interface %q already in use", entry)
}
}
unused := true
addrs, err := iface.Addrs()
if err == nil {
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if ip != nil && err == nil && ip.IsGlobalUnicast() {
unused = false
break
}
}
}
if !unused {
return errors.New("Only unconfigured network interfaces can be bridged")
}
err = AttachInterface(n.state, n.name, entry)
if err != nil {
return err
}
// Make sure the port is up.
link := &ip.Link{Name: entry}
err = link.SetUp()
if err != nil {
return fmt.Errorf("Failed to bring up the host interface %s: %w", entry, err)
}
}
}
// Remove any existing firewall rules.
fwClearIPVersions := []uint{}
if usesIPv4Firewall(n.config) || usesIPv4Firewall(oldConfig) {
fwClearIPVersions = append(fwClearIPVersions, 4)
}
if usesIPv6Firewall(n.config) || usesIPv6Firewall(oldConfig) {
fwClearIPVersions = append(fwClearIPVersions, 6)
}
if len(fwClearIPVersions) > 0 {
n.logger.Debug("Clearing firewall")
err = n.state.Firewall.NetworkClear(n.name, false, fwClearIPVersions)
if err != nil {
return fmt.Errorf("Failed clearing firewall: %w", err)
}
}
// Initialize a new firewall option set.
fwOpts := firewallDrivers.Opts{}
if n.hasIPv4Firewall() {
fwOpts.FeaturesV4 = &firewallDrivers.FeatureOpts{}
}
if n.hasIPv6Firewall() {
fwOpts.FeaturesV6 = &firewallDrivers.FeatureOpts{}
}
if n.config["security.acls"] != "" {
fwOpts.ACL = true
}
// Snapshot container specific IPv4 routes (added with boot proto) before removing IPv4 addresses.
// This is because the kernel removes any static routes on an interface when all addresses removed.
ctRoutes, err := n.bootRoutesV4()
if err != nil {
return err
}
// Flush all IPv4 addresses and routes.
addr := &ip.Addr{
DevName: n.name,
Scope: "global",
Family: ip.FamilyV4,
}
err = addr.Flush()
if err != nil {
return err
}
r := &ip.Route{
DevName: n.name,
Proto: "static",
Family: ip.FamilyV4,
}
err = r.Flush()
if err != nil {
return err
}
// Configure IPv4 firewall.
if !util.IsNoneOrEmpty(n.config["ipv4.address"]) {
if n.hasDHCPv4() && n.hasIPv4Firewall() {
fwOpts.FeaturesV4.ICMPDHCPDNSAccess = true
}
// Allow forwarding.
if util.IsTrueOrEmpty(n.config["ipv4.routing"]) {
err = localUtil.SysctlSet("net/ipv4/ip_forward", "1")
if err != nil {
return err
}
if n.hasIPv4Firewall() {
fwOpts.FeaturesV4.ForwardingAllow = true
}
}
}
// Start building process using subprocess package.
command := "dnsmasq"
dnsmasqCmd := []string{
"--keep-in-foreground", "--strict-order", "--bind-interfaces",
"--except-interface=lo",
"--pid-file=", // Disable attempt at writing a PID file.
"--no-ping", // --no-ping is very important to prevent delays to lease file updates.
fmt.Sprintf("--interface=%s", n.name),
}
dnsmasqVersion, err := dnsmasq.GetVersion()
if err != nil {
return err
}
// --dhcp-rapid-commit option is only supported on >2.79.
minVer, _ := version.NewDottedVersion("2.79")
if dnsmasqVersion.Compare(minVer) > 0 {
dnsmasqCmd = append(dnsmasqCmd, "--dhcp-rapid-commit")
}
// --no-negcache option is only supported on >2.47.
minVer, _ = version.NewDottedVersion("2.47")
if dnsmasqVersion.Compare(minVer) > 0 {
dnsmasqCmd = append(dnsmasqCmd, "--no-negcache")
}
if !daemon.Debug {
// --quiet options are only supported on >2.67.
minVer, _ := version.NewDottedVersion("2.67")
if dnsmasqVersion.Compare(minVer) > 0 {
dnsmasqCmd = append(dnsmasqCmd, []string{"--quiet-dhcp", "--quiet-dhcp6", "--quiet-ra"}...)
}
}
var dnsIPv4 []string
var dnsIPv6 []string
for _, s := range util.SplitNTrimSpace(n.config["dns.nameservers"], ",", -1, false) {
if net.ParseIP(s).To4() != nil {
dnsIPv4 = append(dnsIPv4, s)
} else {
dnsIPv6 = append(dnsIPv6, s)
}
}
// Configure IPv4.
if !util.IsNoneOrEmpty(n.config["ipv4.address"]) {
// Parse the subnet.
ipAddress, subnet, err := net.ParseCIDR(n.config["ipv4.address"])
if err != nil {
return fmt.Errorf("Failed parsing ipv4.address: %w", err)
}
// Update the dnsmasq config.
dnsmasqCmd = append(dnsmasqCmd, fmt.Sprintf("--listen-address=%s", ipAddress.String()))
if n.DHCPv4Subnet() != nil {
if !slices.Contains(dnsmasqCmd, "--dhcp-no-override") {
dnsmasqCmd = append(dnsmasqCmd, []string{"--dhcp-no-override", "--dhcp-authoritative", fmt.Sprintf("--dhcp-leasefile=%s", internalUtil.VarPath("networks", n.name, "dnsmasq.leases")), fmt.Sprintf("--dhcp-hostsfile=%s", internalUtil.VarPath("networks", n.name, "dnsmasq.hosts"))}...)
}
if n.config["ipv4.dhcp.gateway"] != "" {
dnsmasqCmd = append(dnsmasqCmd, fmt.Sprintf("--dhcp-option-force=3,%s", n.config["ipv4.dhcp.gateway"]))
}
if n.config["dns.nameservers"] != "" {
if len(dnsIPv4) == 0 {
dnsmasqCmd = append(dnsmasqCmd, "--dhcp-option-force=6")
} else {
dnsmasqCmd = append(dnsmasqCmd, fmt.Sprintf("--dhcp-option-force=6,%s", strings.Join(dnsIPv4, ",")))
}
}
if bridge.MTU != bridgeMTUDefault {
dnsmasqCmd = append(dnsmasqCmd, fmt.Sprintf("--dhcp-option-force=26,%d", bridge.MTU))
}
dnsSearch := n.config["dns.search"]
if dnsSearch != "" {
dnsmasqCmd = append(dnsmasqCmd, fmt.Sprintf("--dhcp-option-force=119,%s", strings.Trim(dnsSearch, " ")))
}
if n.config["ipv4.dhcp.routes"] != "" {
dnsmasqCmd = append(dnsmasqCmd, fmt.Sprintf("--dhcp-option-force=121,%s", strings.ReplaceAll(n.config["ipv4.dhcp.routes"], " ", "")))
}
expiry := "1h"
if n.config["ipv4.dhcp.expiry"] != "" {
expiry = n.config["ipv4.dhcp.expiry"]
}
if n.config["ipv4.dhcp.ranges"] != "" {
for _, dhcpRange := range strings.Split(n.config["ipv4.dhcp.ranges"], ",") {
dhcpRange = strings.TrimSpace(dhcpRange)
dnsmasqCmd = append(dnsmasqCmd, []string{"--dhcp-range", fmt.Sprintf("%s,%s", strings.ReplaceAll(dhcpRange, "-", ","), expiry)}...)
}
} else {
dnsmasqCmd = append(dnsmasqCmd, []string{"--dhcp-range", fmt.Sprintf("%s,%s,%s", dhcpalloc.GetIP(subnet, 2).String(), dhcpalloc.GetIP(subnet, -2).String(), expiry)}...)
}
}
// Add the address.
addr := &ip.Addr{
DevName: n.name,
Address: &net.IPNet{
IP: ipAddress,
Mask: subnet.Mask,
},
Family: ip.FamilyV4,
}
err = addr.Add()
if err != nil {
return err
}
// Configure NAT.
if util.IsTrue(n.config["ipv4.nat"]) {
// If a SNAT source address is specified, use that, otherwise default to MASQUERADE mode.
var srcIP net.IP
if n.config["ipv4.nat.address"] != "" {
srcIP = net.ParseIP(n.config["ipv4.nat.address"])
}
fwOpts.SNATV4 = &firewallDrivers.SNATOpts{
SNATAddress: srcIP,
Subnet: subnet,
}
if n.config["ipv4.nat.order"] == "after" {
fwOpts.SNATV4.Append = true
}
}
// Add additional routes.
if n.config["ipv4.routes"] != "" {
for _, route := range strings.Split(n.config["ipv4.routes"], ",") {
route, err := ip.ParseIPNet(strings.TrimSpace(route))
if err != nil {
return err
}
r := &ip.Route{
DevName: n.name,
Route: route,
Proto: "static",
Family: ip.FamilyV4,
}
err = r.Add()
if err != nil {
return err
}
}
}
// Restore container specific IPv4 routes to interface.
n.applyBootRoutesV4(ctRoutes)
}
// Snapshot container specific IPv6 routes (added with boot proto) before removing IPv6 addresses.
// This is because the kernel removes any static routes on an interface when all addresses removed.
ctRoutes, err = n.bootRoutesV6()
if err != nil {
return err
}
// Flush all IPv6 addresses and routes.
addr = &ip.Addr{
DevName: n.name,
Scope: "global",
Family: ip.FamilyV6,
}
err = addr.Flush()
if err != nil {
return err
}
r = &ip.Route{
DevName: n.name,
Proto: "static",
Family: ip.FamilyV6,
}
err = r.Flush()
if err != nil {
return err
}
// Configure IPv6.
if !util.IsNoneOrEmpty(n.config["ipv6.address"]) {
// Enable IPv6 for the subnet.
err := localUtil.SysctlSet(fmt.Sprintf("net/ipv6/conf/%s/disable_ipv6", n.name), "0")
if err != nil {
return err
}
// Parse the subnet.
ipAddress, subnet, err := net.ParseCIDR(n.config["ipv6.address"])
if err != nil {
return fmt.Errorf("Failed parsing ipv6.address: %w", err)
}
subnetSize, _ := subnet.Mask.Size()
// Check if we need to generate a host-specific address.
if ipAddress.String() == subnet.IP.String() {
if subnetSize != 64 {
return errors.New("Can't generate an EUI64 derived IPv6 address with a mask other than /64")
}
ipAddress, err = eui64.ParseMAC(subnet.IP, bridge.Address)
if err != nil {
return fmt.Errorf("Failed generating EUI64 value for ipv6.address: %w", err)
}
}
if subnetSize > 64 {
n.logger.Warn("IPv6 networks with a prefix larger than 64 aren't properly supported by dnsmasq")
err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.UpsertWarningLocalNode(ctx, n.project, dbCluster.TypeNetwork, int(n.id), warningtype.LargerIPv6PrefixThanSupported, "")
})
if err != nil {
n.logger.Warn("Failed to create warning", logger.Ctx{"err": err})
}
} else {
err = warnings.ResolveWarningsByLocalNodeAndProjectAndTypeAndEntity(n.state.DB.Cluster, n.project, warningtype.LargerIPv6PrefixThanSupported, dbCluster.TypeNetwork, int(n.id))
if err != nil {
n.logger.Warn("Failed to resolve warning", logger.Ctx{"err": err})
}
}
// Update the dnsmasq config.
dnsmasqCmd = append(dnsmasqCmd, []string{fmt.Sprintf("--listen-address=%s", ipAddress.String()), "--enable-ra"}...)
if n.DHCPv6Subnet() != nil {
if n.hasIPv6Firewall() {
fwOpts.FeaturesV6.ICMPDHCPDNSAccess = true
}
// Build DHCP configuration.
if !slices.Contains(dnsmasqCmd, "--dhcp-no-override") {
dnsmasqCmd = append(dnsmasqCmd, []string{"--dhcp-no-override", "--dhcp-authoritative", fmt.Sprintf("--dhcp-leasefile=%s", internalUtil.VarPath("networks", n.name, "dnsmasq.leases")), fmt.Sprintf("--dhcp-hostsfile=%s", internalUtil.VarPath("networks", n.name, "dnsmasq.hosts"))}...)
}
expiry := "1h"
if n.config["ipv6.dhcp.expiry"] != "" {
expiry = n.config["ipv6.dhcp.expiry"]
}
if util.IsTrue(n.config["ipv6.dhcp.stateful"]) {
if n.config["ipv6.dhcp.ranges"] != "" {
for _, dhcpRange := range strings.Split(n.config["ipv6.dhcp.ranges"], ",") {
dhcpRange = strings.TrimSpace(dhcpRange)
dnsmasqCmd = append(dnsmasqCmd, []string{"--dhcp-range", fmt.Sprintf("%s,%d,%s", strings.ReplaceAll(dhcpRange, "-", ","), subnetSize, expiry)}...)
}
} else {
dnsmasqCmd = append(dnsmasqCmd, []string{"--dhcp-range", fmt.Sprintf("%s,%s,%d,%s", dhcpalloc.GetIP(subnet, 2), dhcpalloc.GetIP(subnet, -1), subnetSize, expiry)}...)
}
} else {
dnsmasqCmd = append(dnsmasqCmd, []string{"--dhcp-range", fmt.Sprintf("::,constructor:%s,ra-stateless,ra-names", n.name)}...)
}
} else {
dnsmasqCmd = append(dnsmasqCmd, []string{"--dhcp-range", fmt.Sprintf("::,constructor:%s,ra-only", n.name)}...)
}
if n.config["dns.nameservers"] != "" {
if len(dnsIPv6) == 0 {
dnsmasqCmd = append(dnsmasqCmd, "--dhcp-option-force=option6:dns-server")
} else {
dnsmasqCmd = append(dnsmasqCmd, fmt.Sprintf("--dhcp-option-force=option6:dns-server,[%s]", strings.Join(dnsIPv6, ",")))
}
} else {
dnsmasqCmd = append(dnsmasqCmd, fmt.Sprintf("--dhcp-option-force=option6:dns-server,[%s]", ipAddress.String()))
}
// Allow forwarding.
if util.IsTrueOrEmpty(n.config["ipv6.routing"]) {
// Get a list of proc entries.
entries, err := os.ReadDir("/proc/sys/net/ipv6/conf/")
if err != nil {
return err
}
// First set accept_ra to 2 for all interfaces (if not disabled).
// This ensures that the host can still receive IPv6 router advertisements even with
// forwarding enabled (which enable below), as the default is to ignore router adverts
// when forward is enabled, and this could render the host unreachable if it uses
// SLAAC generated IPs.
for _, entry := range entries {
// Check that IPv6 router advertisement acceptance is enabled currently.
// If its set to 0 then we don't want to enable, and if its already set to 2 then
// we don't need to do anything.
content, err := os.ReadFile(fmt.Sprintf("/proc/sys/net/ipv6/conf/%s/accept_ra", entry.Name()))
if err == nil && string(content) != "1\n" {
continue
}
// If IPv6 router acceptance is enabled (set to 1) then we now set it to 2.
err = localUtil.SysctlSet(fmt.Sprintf("net/ipv6/conf/%s/accept_ra", entry.Name()), "2")
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
}
// Then set forwarding for all of them.
for _, entry := range entries {
err = localUtil.SysctlSet(fmt.Sprintf("net/ipv6/conf/%s/forwarding", entry.Name()), "1")
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
}
if n.hasIPv6Firewall() {
fwOpts.FeaturesV6.ForwardingAllow = true
}
}
// Add the address.
addr := &ip.Addr{
DevName: n.name,
Address: &net.IPNet{
IP: ipAddress,
Mask: subnet.Mask,
},
Family: ip.FamilyV6,
}
err = addr.Add()
if err != nil {
return err
}
// Configure NAT.
if util.IsTrue(n.config["ipv6.nat"]) {
// If a SNAT source address is specified, use that, otherwise default to MASQUERADE mode.
var srcIP net.IP
if n.config["ipv6.nat.address"] != "" {
srcIP = net.ParseIP(n.config["ipv6.nat.address"])
}
fwOpts.SNATV6 = &firewallDrivers.SNATOpts{
SNATAddress: srcIP,
Subnet: subnet,
}
if n.config["ipv6.nat.order"] == "after" {
fwOpts.SNATV6.Append = true
}
}
// Add additional routes.
if n.config["ipv6.routes"] != "" {
for _, route := range strings.Split(n.config["ipv6.routes"], ",") {
route, err := ip.ParseIPNet(route)
if err != nil {
return err
}
r := &ip.Route{
DevName: n.name,
Route: route,
Proto: "static",
Family: ip.FamilyV6,
}
err = r.Add()
if err != nil {
return err
}
}
}
// Restore container specific IPv6 routes to interface.
n.applyBootRoutesV6(ctRoutes)
}
// Configure tunnels.
for _, tunnel := range tunnels {
getConfig := func(key string) string {
return n.config[fmt.Sprintf("tunnel.%s.%s", tunnel, key)]
}
tunProtocol := getConfig("protocol")
tunLocal := net.ParseIP(getConfig("local"))
tunRemote := net.ParseIP(getConfig("remote"))
tunName := fmt.Sprintf("%s-%s", n.name, tunnel)
// Configure the tunnel.
if tunProtocol == "gre" {
// Skip partial configs.
if tunLocal == nil || tunRemote == nil {
continue
}
gretap := &ip.Gretap{
Link: ip.Link{Name: tunName},
Local: tunLocal,
Remote: tunRemote,
}
err := gretap.Add()
if err != nil {
return err
}
} else if tunProtocol == "vxlan" {
tunGroup := net.ParseIP(getConfig("group"))
tunInterface := getConfig("interface")
vxlan := &ip.Vxlan{
Link: ip.Link{Name: tunName},
Local: tunLocal,
}
if tunRemote != nil {
// Skip partial configs.
if tunLocal == nil {
continue
}
vxlan.Remote = tunRemote
} else {
if tunGroup == nil {
tunGroup = net.IPv4(239, 0, 0, 1) // 239.0.0.1
}
devName := tunInterface
if devName == "" {
_, devName, err = DefaultGatewaySubnetV4()
if err != nil {
return err
}
}
vxlan.Group = tunGroup
vxlan.DevName = devName
}
tunPort := getConfig("port")
if tunPort != "" {
vxlan.DstPort, err = strconv.Atoi(tunPort)
if err != nil {
return err
}
}
tunID := getConfig("id")
if tunID == "" {
vxlan.VxlanID = 1
} else {
vxlan.VxlanID, err = strconv.Atoi(tunID)
if err != nil {
return err
}
}
tunTTL := getConfig("ttl")
if tunTTL == "" {
vxlan.TTL = 1
} else {
vxlan.TTL, err = strconv.Atoi(tunTTL)
if err != nil {
return err
}
}
err := vxlan.Add()
if err != nil {
return err
}
}
// Bridge it and bring up.
err = AttachInterface(n.state, n.name, tunName)
if err != nil {
return err
}
tunLink := &ip.Link{Name: tunName}
err = tunLink.SetMTU(bridge.MTU)
if err != nil {
return err
}
// Bring up tunnel interface.
err = tunLink.SetUp()
if err != nil {
return err
}
// Bring up network interface.
err = bridge.SetUp()
if err != nil {
return err
}
}
// Generate and load apparmor profiles.
err = apparmor.NetworkLoad(n.state.OS, n)
if err != nil {
return err
}
// Kill any existing dnsmasq daemon for this network.
err = dnsmasq.Kill(n.name, false)
if err != nil {
return err
}
// Configure dnsmasq.
if n.UsesDNSMasq() {
// Setup the dnsmasq domain.
dnsDomain := n.config["dns.domain"]
if dnsDomain == "" {
dnsDomain = "incus"
}
if n.config["dns.mode"] != "none" {
dnsmasqCmd = append(dnsmasqCmd, "-s", dnsDomain)
dnsmasqCmd = append(dnsmasqCmd, "--interface-name", fmt.Sprintf("_gateway.%s,%s", dnsDomain, n.name))
dnsmasqCmd = append(dnsmasqCmd, "-S", fmt.Sprintf("/%s/", dnsDomain))
}
// Create a config file to contain additional config (and to prevent dnsmasq from reading /etc/dnsmasq.conf)
err = os.WriteFile(internalUtil.VarPath("networks", n.name, "dnsmasq.raw"), fmt.Appendf(nil, "%s\n", n.config["raw.dnsmasq"]), 0o644)
if err != nil {
return err
}
dnsmasqCmd = append(dnsmasqCmd, fmt.Sprintf("--conf-file=%s", internalUtil.VarPath("networks", n.name, "dnsmasq.raw")))
// Attempt to drop privileges.
if n.state.OS.UnprivUser != "" {
dnsmasqCmd = append(dnsmasqCmd, []string{"-u", n.state.OS.UnprivUser}...)
}
if n.state.OS.UnprivGroup != "" {
dnsmasqCmd = append(dnsmasqCmd, []string{"-g", n.state.OS.UnprivGroup}...)
}
// Create DHCP hosts directory.
if !util.PathExists(internalUtil.VarPath("networks", n.name, "dnsmasq.hosts")) {
err = os.MkdirAll(internalUtil.VarPath("networks", n.name, "dnsmasq.hosts"), 0o755)
if err != nil {
return err
}
}
// Check for dnsmasq.
_, err := exec.LookPath("dnsmasq")
if err != nil {
return errors.New("dnsmasq is required for managed bridges")
}
// Update the static leases.
err = UpdateDNSMasqStatic(n.state, n.name)
if err != nil {
return err
}
// Create subprocess object dnsmasq.
dnsmasqLogPath := internalUtil.LogPath(fmt.Sprintf("dnsmasq.%s.log", n.name))
p, err := subprocess.NewProcess(command, dnsmasqCmd, "", dnsmasqLogPath)
if err != nil {
return fmt.Errorf("Failed to create subprocess: %s", err)
}
// Apply AppArmor confinement.
if n.config["raw.dnsmasq"] == "" {
p.SetApparmor(apparmor.DnsmasqProfileName(n))
err = warnings.ResolveWarningsByLocalNodeAndProjectAndTypeAndEntity(n.state.DB.Cluster, n.project, warningtype.AppArmorDisabledDueToRawDnsmasq, dbCluster.TypeNetwork, int(n.id))
if err != nil {
n.logger.Warn("Failed to resolve warning", logger.Ctx{"err": err})
}
} else {
n.logger.Warn("Skipping AppArmor for dnsmasq due to raw.dnsmasq being set", logger.Ctx{"name": n.name})
err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.UpsertWarningLocalNode(ctx, n.project, dbCluster.TypeNetwork, int(n.id), warningtype.AppArmorDisabledDueToRawDnsmasq, "")
})
if err != nil {
n.logger.Warn("Failed to create warning", logger.Ctx{"err": err})
}
}
// Start dnsmasq.
err = p.Start(context.Background())
if err != nil {
return fmt.Errorf("Failed to run: %s %s: %w", command, strings.Join(dnsmasqCmd, " "), err)
}
// Check dnsmasq started OK.
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*time.Duration(500)))
_, err = p.Wait(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
stderr, _ := os.ReadFile(dnsmasqLogPath)
cancel()
return fmt.Errorf("The DNS and DHCP service exited prematurely: %w (%q)", err, strings.TrimSpace(string(stderr)))
}
cancel()
err = p.Save(internalUtil.VarPath("networks", n.name, "dnsmasq.pid"))
if err != nil {
// Kill Process if started, but could not save the file.
err2 := p.Stop()
if err2 != nil {
return fmt.Errorf("Could not kill subprocess while handling saving error: %s: %s", err, err2)
}
return fmt.Errorf("Failed to save subprocess details: %s", err)
}
} else {
// Clean up old dnsmasq config if exists and we are not starting dnsmasq.
leasesPath := internalUtil.VarPath("networks", n.name, "dnsmasq.leases")
if util.PathExists(leasesPath) {
err := os.Remove(leasesPath)
if err != nil {
return fmt.Errorf("Failed to remove old dnsmasq leases file %q: %w", leasesPath, err)
}
}
// Clean up old dnsmasq PID file.
pidPath := internalUtil.VarPath("networks", n.name, "dnsmasq.pid")
if util.PathExists(pidPath) {
err := os.Remove(pidPath)
if err != nil {
return fmt.Errorf("Failed to remove old dnsmasq pid file %q: %w", pidPath, err)
}
}
}
// Setup firewall.
n.logger.Debug("Setting up firewall")
err = n.state.Firewall.NetworkSetup(n.name, fwOpts)
if err != nil {
return fmt.Errorf("Failed to setup firewall: %w", err)
}
if fwOpts.ACL {
aclNet := acl.NetworkACLUsage{
Name: n.Name(),
Type: n.Type(),
ID: n.ID(),
Config: n.Config(),
}
n.logger.Debug("Applying up firewall ACLs")
err = acl.FirewallApplyACLRules(n.state, n.logger, n.Project(), aclNet)
if err != nil {
return err
}
}
// Setup network address forwards.
err = n.forwardSetupFirewall()
if err != nil {
return err
}
// Setup BGP.
err = n.bgpSetup(oldConfig)
if err != nil {
return err
}
reverter.Success()
return nil
}
// Stop stops the network.
func (n *bridge) Stop() error {
n.logger.Debug("Stop")
if !n.isRunning() {
return nil
}
// Clear BGP.
err := n.bgpClear(n.config)
if err != nil {
return err
}
err = n.deleteChildren()
if err != nil {
return fmt.Errorf("Failed to delete bridge children interfaces: %w", err)
}
// Destroy the bridge interface
if n.config["bridge.driver"] == "openvswitch" {
vswitch, err := n.state.OVS()
if err != nil {
return err
}
err = vswitch.DeleteBridge(context.TODO(), n.name)
if err != nil {
return err
}
} else {
bridgeLink := &ip.Link{Name: n.name}
err := bridgeLink.Delete()
if err != nil {
return err
}
}
// Fully clear firewall setup.
fwClearIPVersions := []uint{}
if usesIPv4Firewall(n.config) {
fwClearIPVersions = append(fwClearIPVersions, 4)
}
if usesIPv6Firewall(n.config) {
fwClearIPVersions = append(fwClearIPVersions, 6)
}
if len(fwClearIPVersions) > 0 {
n.logger.Debug("Deleting firewall")
err := n.state.Firewall.NetworkClear(n.name, true, fwClearIPVersions)
if err != nil {
return fmt.Errorf("Failed deleting firewall: %w", err)
}
}
// Kill any existing dnsmasq daemon for this network
err = dnsmasq.Kill(n.name, false)
if err != nil {
return err
}
// Unload apparmor profiles.
err = apparmor.NetworkUnload(n.state.OS, n)
if err != nil {
return err
}
return nil
}
// Update updates the network. Accepts notification boolean indicating if this update request is coming from a
// cluster notification, in which case do not update the database, just apply local changes needed.
func (n *bridge) Update(newNetwork api.NetworkPut, targetNode string, clientType request.ClientType) error {
n.logger.Debug("Update", logger.Ctx{"clientType": clientType, "newNetwork": newNetwork})
err := n.populateAutoConfig(newNetwork.Config)
if err != nil {
return fmt.Errorf("Failed generating auto config: %w", err)
}
dbUpdateNeeded, changedKeys, oldNetwork, err := n.common.configChanged(newNetwork)
if err != nil {
return err
}
if !dbUpdateNeeded {
return nil // Nothing changed.
}
// If the network as a whole has not had any previous creation attempts, or the node itself is still
// pending, then don't apply the new settings to the node, just to the database record (ready for the
// actual global create request to be initiated).
if n.Status() == api.NetworkStatusPending || n.LocalStatus() == api.NetworkStatusPending {
return n.common.update(newNetwork, targetNode, clientType)
}
reverter := revert.New()
defer reverter.Fail()
// Perform any pre-update cleanup needed if local member network was already created.
if len(changedKeys) > 0 {
// Define a function which reverts everything.
reverter.Add(func() {
// Reset changes to all nodes and database.
_ = n.common.update(oldNetwork, targetNode, clientType)
// Reset any change that was made to local bridge.
_ = n.setup(newNetwork.Config)
})
// Bring the bridge down entirely if the driver has changed.
if slices.Contains(changedKeys, "bridge.driver") && n.isRunning() {
err = n.Stop()
if err != nil {
return err
}
}
// Detach any external interfaces should no longer be attached.
if slices.Contains(changedKeys, "bridge.external_interfaces") && n.isRunning() {
devices := []string{}
for _, dev := range strings.Split(newNetwork.Config["bridge.external_interfaces"], ",") {
dev = strings.TrimSpace(dev)
devices = append(devices, dev)
}
for _, dev := range strings.Split(oldNetwork.Config["bridge.external_interfaces"], ",") {
dev = strings.TrimSpace(dev)
if dev == "" {
continue
}
// Test for extended configuration of external interface.
ifName := dev
devParts := strings.Split(dev, "/")
if len(devParts) == 3 {
ifName = strings.TrimSpace(devParts[0])
}
if !slices.Contains(devices, dev) && InterfaceExists(ifName) {
err = DetachInterface(n.state, n.name, ifName)
if err != nil {
return err
}
// Remove the interface if it exists (and we created it).
if len(devParts) == 3 {
_, err := net.InterfaceByName(ifName)
if err == nil {
err = InterfaceRemove(ifName)
if err != nil {
return err
}
}
}
}
}
}
}
// Apply changes to all nodes and database.
err = n.common.update(newNetwork, targetNode, clientType)
if err != nil {
return err
}
// Restart the network if needed.
if len(changedKeys) > 0 {
err = n.setup(oldNetwork.Config)
if err != nil {
return err
}
}
reverter.Success()
return nil
}
func (n *bridge) getTunnels() []string {
tunnels := []string{}
for k := range n.config {
if !strings.HasPrefix(k, "tunnel.") {
continue
}
fields := strings.Split(k, ".")
if !slices.Contains(tunnels, fields[1]) {
tunnels = append(tunnels, fields[1])
}
}
return tunnels
}
// bootRoutesV4 returns a list of IPv4 boot routes on the network's device.
func (n *bridge) bootRoutesV4() ([]ip.Route, error) {
r := &ip.Route{
DevName: n.name,
Proto: "boot",
Family: ip.FamilyV4,
}
routes, err := r.List()
if err != nil {
return nil, err
}
return routes, nil
}
// bootRoutesV6 returns a list of IPv6 boot routes on the network's device.
func (n *bridge) bootRoutesV6() ([]ip.Route, error) {
r := &ip.Route{
DevName: n.name,
Proto: "boot",
Family: ip.FamilyV6,
}
routes, err := r.List()
if err != nil {
return nil, err
}
return routes, nil
}
// applyBootRoutesV4 applies a list of IPv4 boot routes to the network's device.
func (n *bridge) applyBootRoutesV4(routes []ip.Route) {
for _, route := range routes {
err := route.Replace()
if err != nil {
// If it fails, then we can't stop as the route has already gone, so just log and continue.
n.logger.Error("Failed to restore route", logger.Ctx{"err": err})
}
}
}
// applyBootRoutesV6 applies a list of IPv6 boot routes to the network's device.
func (n *bridge) applyBootRoutesV6(routes []ip.Route) {
for _, route := range routes {
err := route.Replace()
if err != nil {
// If it fails, then we can't stop as the route has already gone, so just log and continue.
n.logger.Error("Failed to restore route", logger.Ctx{"err": err})
}
}
}
// hasIPv4Firewall indicates whether the network has IPv4 firewall enabled.
func (n *bridge) hasIPv4Firewall() bool {
// IPv4 firewall is only enabled if there is a bridge ipv4.address and ipv4.firewall enabled.
if !util.IsNoneOrEmpty(n.config["ipv4.address"]) && util.IsTrueOrEmpty(n.config["ipv4.firewall"]) {
return true
}
return false
}
// hasIPv6Firewall indicates whether the network has IPv6 firewall enabled.
func (n *bridge) hasIPv6Firewall() bool {
// IPv6 firewall is only enabled if there is a bridge ipv6.address and ipv6.firewall enabled.
if !util.IsNoneOrEmpty(n.config["ipv6.address"]) && util.IsTrueOrEmpty(n.config["ipv6.firewall"]) {
return true
}
return false
}
// hasDHCPv4 indicates whether the network has DHCPv4 enabled.
// An empty ipv4.dhcp setting indicates enabled by default.
func (n *bridge) hasDHCPv4() bool {
return util.IsTrueOrEmpty(n.config["ipv4.dhcp"])
}
// hasDHCPv6 indicates whether the network has DHCPv6 enabled.
// An empty ipv6.dhcp setting indicates enabled by default.
func (n *bridge) hasDHCPv6() bool {
return util.IsTrueOrEmpty(n.config["ipv6.dhcp"])
}
// DHCPv4Subnet returns the DHCPv4 subnet (if DHCP is enabled on network).
func (n *bridge) DHCPv4Subnet() *net.IPNet {
// DHCP is disabled on this network.
if !n.hasDHCPv4() {
return nil
}
// Return configured bridge subnet directly.
_, subnet, err := net.ParseCIDR(n.config["ipv4.address"])
if err != nil {
return nil
}
return subnet
}
// DHCPv6Subnet returns the DHCPv6 subnet (if DHCP or SLAAC is enabled on network).
func (n *bridge) DHCPv6Subnet() *net.IPNet {
// DHCP is disabled on this network.
if !n.hasDHCPv6() {
return nil
}
_, subnet, err := net.ParseCIDR(n.config["ipv6.address"])
if err != nil {
return nil
}
return subnet
}
// forwardConvertToFirewallForward converts forwards into format compatible with the firewall package.
func (n *bridge) forwardConvertToFirewallForwards(listenAddress net.IP, defaultTargetAddress net.IP, portMaps []*forwardPortMap) []firewallDrivers.AddressForward {
var vips []firewallDrivers.AddressForward
if defaultTargetAddress != nil {
vips = append(vips, firewallDrivers.AddressForward{
ListenAddress: listenAddress,
TargetAddress: defaultTargetAddress,
})
}
for _, portMap := range portMaps {
vips = append(vips, firewallDrivers.AddressForward{
ListenAddress: listenAddress,
Protocol: portMap.protocol,
TargetAddress: portMap.target.address,
ListenPorts: portMap.listenPorts,
TargetPorts: portMap.target.ports,
SNAT: portMap.snat,
})
}
return vips
}
// bridgeProjectNetworks takes a map of all networks in all projects and returns a filtered map of bridge networks.
func (n *bridge) bridgeProjectNetworks(projectNetworks map[string]map[int64]api.Network) map[string][]*api.Network {
bridgeProjectNetworks := make(map[string][]*api.Network)
for netProject, networks := range projectNetworks {
for _, ni := range networks {
network := ni // Local var creating pointer to rather than iterator.
// Skip non-bridge networks.
if network.Type != "bridge" {
continue
}
if bridgeProjectNetworks[netProject] == nil {
bridgeProjectNetworks[netProject] = []*api.Network{&network}
} else {
bridgeProjectNetworks[netProject] = append(bridgeProjectNetworks[netProject], &network)
}
}
}
return bridgeProjectNetworks
}
// bridgeNetworkExternalSubnets returns a list of external subnets used by bridge networks. Networks are considered
// to be using external subnets for their ipv4.address and/or ipv6.address if they have NAT disabled, and/or if
// they have external NAT addresses specified.
func (n *bridge) bridgeNetworkExternalSubnets(bridgeProjectNetworks map[string][]*api.Network) ([]externalSubnetUsage, error) {
externalSubnets := make([]externalSubnetUsage, 0)
for netProject, networks := range bridgeProjectNetworks {
for _, netInfo := range networks {
for _, keyPrefix := range []string{"ipv4", "ipv6"} {
// If NAT is disabled, then network subnet is an external subnet.
if util.IsFalseOrEmpty(netInfo.Config[fmt.Sprintf("%s.nat", keyPrefix)]) {
key := fmt.Sprintf("%s.address", keyPrefix)
_, ipNet, err := net.ParseCIDR(netInfo.Config[key])
if err != nil {
continue // Skip invalid/unspecified network addresses.
}
externalSubnets = append(externalSubnets, externalSubnetUsage{
subnet: *ipNet,
networkProject: netProject,
networkName: netInfo.Name,
usageType: subnetUsageNetwork,
})
}
// Find any external subnets used for network SNAT.
if netInfo.Config[fmt.Sprintf("%s.nat.address", keyPrefix)] != "" {
key := fmt.Sprintf("%s.nat.address", keyPrefix)
subnetSize := 128
if keyPrefix == "ipv4" {
subnetSize = 32
}
_, ipNet, err := net.ParseCIDR(fmt.Sprintf("%s/%d", netInfo.Config[key], subnetSize))
if err != nil {
return nil, fmt.Errorf("Failed parsing %q of %q in project %q: %w", key, netInfo.Name, netProject, err)
}
externalSubnets = append(externalSubnets, externalSubnetUsage{
subnet: *ipNet,
networkProject: netProject,
networkName: netInfo.Name,
usageType: subnetUsageNetworkSNAT,
})
}
// Find any routes being used by the network.
for _, cidr := range util.SplitNTrimSpace(netInfo.Config[fmt.Sprintf("%s.routes", keyPrefix)], ",", -1, true) {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
continue // Skip invalid/unspecified network addresses.
}
externalSubnets = append(externalSubnets, externalSubnetUsage{
subnet: *ipNet,
networkProject: netProject,
networkName: netInfo.Name,
usageType: subnetUsageNetwork,
})
}
}
}
}
return externalSubnets, nil
}
// bridgedNICExternalRoutes returns a list of external routes currently used by bridged NICs that are connected to
// networks specified.
func (n *bridge) bridgedNICExternalRoutes(bridgeProjectNetworks map[string][]*api.Network) ([]externalSubnetUsage, error) {
externalRoutes := make([]externalSubnetUsage, 0)
err := n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.InstanceList(ctx, func(inst db.InstanceArgs, p api.Project) error {
// Get the instance's effective network project name.
instNetworkProject := project.NetworkProjectFromRecord(&p)
if instNetworkProject != api.ProjectDefaultName {
return nil // Managed bridge networks can only exist in default project.
}
devices := db.ExpandInstanceDevices(inst.Devices, inst.Profiles)
// Iterate through each of the instance's devices, looking for bridged NICs that are linked to
// networks specified.
for devName, devConfig := range devices {
if devConfig["type"] != "nic" {
continue
}
// Check whether the NIC device references one of the networks supplied.
if !NICUsesNetwork(devConfig, bridgeProjectNetworks[instNetworkProject]...) {
continue
}
// For bridged NICs that are connected to networks specified, check if they have any
// routes or external routes configured, and if so add them to the list to return.
for _, key := range []string{"ipv4.routes", "ipv6.routes", "ipv4.routes.external", "ipv6.routes.external"} {
for _, cidr := range util.SplitNTrimSpace(devConfig[key], ",", -1, true) {
_, ipNet, _ := net.ParseCIDR(cidr)
if ipNet == nil {
// Skip if NIC device doesn't have a valid route.
continue
}
externalRoutes = append(externalRoutes, externalSubnetUsage{
subnet: *ipNet,
networkProject: instNetworkProject,
networkName: devConfig["network"],
instanceProject: inst.Project,
instanceName: inst.Name,
instanceDevice: devName,
usageType: subnetUsageInstance,
})
}
}
}
return nil
})
})
if err != nil {
return nil, err
}
return externalRoutes, nil
}
// getExternalSubnetInUse returns information about usage of external subnets by bridge networks (and NICs
// connected to them) on this member.
func (n *bridge) getExternalSubnetInUse() ([]externalSubnetUsage, error) {
var err error
var projectNetworks map[string]map[int64]api.Network
var projectNetworksForwardsOnUplink map[string]map[int64][]string
var externalSubnets []externalSubnetUsage
err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
// Get all managed networks across all projects.
projectNetworks, err = tx.GetCreatedNetworks(ctx)
if err != nil {
return fmt.Errorf("Failed to load all networks: %w", err)
}
// Get all network forward listen addresses for forwards assigned to this specific cluster member.
networksByProjects, err := tx.GetNetworksAllProjects(ctx)
if err != nil {
return fmt.Errorf("Failed loading network forward listen addresses: %w", err)
}
for projectName, networks := range networksByProjects {
for _, networkName := range networks {
networkID, err := tx.GetNetworkID(ctx, projectName, networkName)
if err != nil {
return fmt.Errorf("Failed loading network forward listen addresses: %w", err)
}
// Get all network forward listen addresses for all networks (of any type) connected to our uplink.
networkForwards, err := dbCluster.GetNetworkForwards(ctx, tx.Tx(), dbCluster.NetworkForwardFilter{
NetworkID: &networkID,
})
if err != nil {
return fmt.Errorf("Failed loading network forward listen addresses: %w", err)
}
projectNetworksForwardsOnUplink = make(map[string]map[int64][]string)
for _, forward := range networkForwards {
// Filter network forwards that belong to this specific cluster member
if forward.NodeID.Valid && (forward.NodeID.Int64 == tx.GetNodeID()) {
if projectNetworksForwardsOnUplink[projectName] == nil {
projectNetworksForwardsOnUplink[projectName] = make(map[int64][]string)
}
projectNetworksForwardsOnUplink[projectName][networkID] = append(projectNetworksForwardsOnUplink[projectName][networkID], forward.ListenAddress)
}
}
}
}
externalSubnets, err = n.common.getExternalSubnetInUse(ctx, tx, n.name, true)
if err != nil {
return fmt.Errorf("Failed getting external subnets in use: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
// Get managed bridge networks.
bridgeProjectNetworks := n.bridgeProjectNetworks(projectNetworks)
// Get external subnets used by other managed bridge networks.
bridgeNetworkExternalSubnets, err := n.bridgeNetworkExternalSubnets(bridgeProjectNetworks)
if err != nil {
return nil, err
}
// Get external routes configured on bridged NICs.
bridgedNICExternalRoutes, err := n.bridgedNICExternalRoutes(bridgeProjectNetworks)
if err != nil {
return nil, err
}
externalSubnets = append(externalSubnets, bridgeNetworkExternalSubnets...)
externalSubnets = append(externalSubnets, bridgedNICExternalRoutes...)
err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
// Detect if there are any conflicting proxy devices on all instances with the to be created network forward
return tx.InstanceList(ctx, func(inst db.InstanceArgs, p api.Project) error {
devices := db.ExpandInstanceDevices(inst.Devices, inst.Profiles)
for devName, devConfig := range devices {
if devConfig["type"] != "proxy" {
continue
}
proxyListenAddr, err := ProxyParseAddr(devConfig["listen"])
if err != nil {
return err
}
proxySubnet, err := ParseIPToNet(proxyListenAddr.Address)
if err != nil {
continue // If proxy listen isn't a valid IP it can't conflict.
}
externalSubnets = append(externalSubnets, externalSubnetUsage{
usageType: subnetUsageProxy,
subnet: *proxySubnet,
instanceProject: inst.Project,
instanceName: inst.Name,
instanceDevice: devName,
})
}
return nil
})
})
if err != nil {
return nil, err
}
// Add forward listen addresses to this list.
for projectName, networks := range projectNetworksForwardsOnUplink {
for networkID, listenAddresses := range networks {
for _, listenAddress := range listenAddresses {
// Convert listen address to subnet.
listenAddressNet, err := ParseIPToNet(listenAddress)
if err != nil {
return nil, fmt.Errorf("Invalid existing forward listen address %q", listenAddress)
}
// Create an externalSubnetUsage for the listen address by using the network ID
// of the listen address to retrieve the already loaded network name from the
// projectNetworks map.
externalSubnets = append(externalSubnets, externalSubnetUsage{
subnet: *listenAddressNet,
networkProject: projectName,
networkName: projectNetworks[projectName][networkID].Name,
usageType: subnetUsageNetworkForward,
})
}
}
}
return externalSubnets, nil
}
// ForwardCreate creates a network forward.
func (n *bridge) ForwardCreate(forward api.NetworkForwardsPost, clientType request.ClientType) error {
memberSpecific := true // bridge supports per-member forwards.
err := n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
// Check if there is an existing forward using the same listen address.
networkID := n.ID()
dbRecords, err := dbCluster.GetNetworkForwards(ctx, tx.Tx(), dbCluster.NetworkForwardFilter{
NetworkID: &networkID,
ListenAddress: &forward.ListenAddress,
})
if err != nil {
return err
}
filteredRecords := make([]dbCluster.NetworkForward, 0, len(dbRecords))
for _, dbRecord := range dbRecords {
// bridge supports per-member forwards so do memberSpecific filtering
if !dbRecord.NodeID.Valid || (dbRecord.NodeID.Int64 == tx.GetNodeID()) {
filteredRecords = append(filteredRecords, dbRecord)
}
}
if len(filteredRecords) == 0 {
return api.StatusErrorf(http.StatusNotFound, "Network forward not found")
}
if len(filteredRecords) > 1 {
return api.StatusErrorf(http.StatusConflict, "Network forward found on more than one cluster member. Please target a specific member")
}
_, err = filteredRecords[0].ToAPI(ctx, tx.Tx())
if err != nil {
return err
}
return nil
})
if err == nil {
return api.StatusErrorf(http.StatusConflict, "A forward for that listen address already exists")
}
// Convert listen address to subnet so we can check its valid and can be used.
listenAddressNet, err := ParseIPToNet(forward.ListenAddress)
if err != nil {
return fmt.Errorf("Failed parsing address forward listen address %q: %w", forward.ListenAddress, err)
}
_, err = n.forwardValidate(listenAddressNet.IP, &forward.NetworkForwardPut)
if err != nil {
return err
}
externalSubnetsInUse, err := n.getExternalSubnetInUse()
if err != nil {
return err
}
// Check the listen address subnet doesn't fall within any existing network external subnets.
for _, externalSubnetUser := range externalSubnetsInUse {
// Check if usage is from our own network.
if externalSubnetUser.networkProject == n.project && externalSubnetUser.networkName == n.name {
// Skip checking conflict with our own network's subnet or SNAT address.
// But do not allow other conflict with other usage types within our own network.
if externalSubnetUser.usageType == subnetUsageNetwork || externalSubnetUser.usageType == subnetUsageNetworkSNAT {
continue
}
}
if SubnetContains(&externalSubnetUser.subnet, listenAddressNet) || SubnetContains(listenAddressNet, &externalSubnetUser.subnet) {
// This error is purposefully vague so that it doesn't reveal any names of
// resources potentially outside of the network.
return fmt.Errorf("Forward listen address %q overlaps with another network or NIC", listenAddressNet.String())
}
}
reverter := revert.New()
defer reverter.Fail()
var forwardID int64
err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
// Create forward DB record.
nodeID := sql.NullInt64{
Valid: memberSpecific,
Int64: tx.GetNodeID(),
}
dbRecord := dbCluster.NetworkForward{
NetworkID: n.ID(),
NodeID: nodeID,
ListenAddress: forward.ListenAddress,
Description: forward.Description,
Ports: forward.Ports,
}
if forward.Ports == nil {
dbRecord.Ports = []api.NetworkForwardPort{}
}
forwardID, err = dbCluster.CreateNetworkForward(ctx, tx.Tx(), dbRecord)
if err != nil {
return err
}
err = dbCluster.CreateNetworkForwardConfig(ctx, tx.Tx(), forwardID, forward.Config)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
reverter.Add(func() {
_ = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return dbCluster.DeleteNetworkForward(ctx, tx.Tx(), n.ID(), forwardID)
})
_ = n.forwardSetupFirewall()
_ = n.forwardBGPSetupPrefixes()
})
err = n.forwardSetupFirewall()
if err != nil {
return err
}
// Check if hairpin mode needs to be enabled on active NIC bridge ports.
if n.config["bridge.driver"] != "openvswitch" {
brNetfilterEnabled := false
for _, ipVersion := range []uint{4, 6} {
if BridgeNetfilterEnabled(ipVersion) == nil {
brNetfilterEnabled = true
break
}
}
// If br_netfilter is enabled and bridge has forwards, we enable hairpin mode on each NIC's bridge
// port in case any of the forwards target the NIC and the instance attempts to connect to the
// forward's listener. Without hairpin mode on the target of the forward will not be able to
// connect to the listener.
if brNetfilterEnabled {
var listenAddresses map[int64]string
err := n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
networkID := n.ID()
dbRecords, err := dbCluster.GetNetworkForwards(ctx, tx.Tx(), dbCluster.NetworkForwardFilter{
NetworkID: &networkID,
})
if err != nil {
return err
}
listenAddresses = make(map[int64]string)
for _, dbRecord := range dbRecords {
if !dbRecord.NodeID.Valid || (dbRecord.NodeID.Int64 == tx.GetNodeID()) {
listenAddresses[dbRecord.ID] = dbRecord.ListenAddress
}
}
return err
})
if err != nil {
return fmt.Errorf("Failed loading network forwards: %w", err)
}
// If we are the first forward on this bridge, enable hairpin mode on active NIC ports.
if len(listenAddresses) <= 1 {
filter := dbCluster.InstanceFilter{Node: &n.state.ServerName}
err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.InstanceList(ctx, func(inst db.InstanceArgs, p api.Project) error {
// Get the instance's effective network project name.
instNetworkProject := project.NetworkProjectFromRecord(&p)
if instNetworkProject != api.ProjectDefaultName {
return nil // Managed bridge networks can only exist in default project.
}
devices := db.ExpandInstanceDevices(inst.Devices.Clone(), inst.Profiles)
// Iterate through each of the instance's devices, looking for bridged NICs
// that are linked to this network.
for devName, devConfig := range devices {
if devConfig["type"] != "nic" {
continue
}
// Check whether the NIC device references our network..
if !NICUsesNetwork(devConfig, &api.Network{Name: n.Name()}) {
continue
}
hostName := inst.Config[fmt.Sprintf("volatile.%s.host_name", devName)]
if InterfaceExists(hostName) {
link := &ip.Link{Name: hostName}
err = link.BridgeLinkSetHairpin(true)
if err != nil {
return fmt.Errorf("Error enabling hairpin mode on bridge port %q: %w", link.Name, err)
}
n.logger.Debug("Enabled hairpin mode on NIC bridge port", logger.Ctx{"inst": inst.Name, "project": inst.Project, "device": devName, "dev": link.Name})
}
}
return nil
}, filter)
})
if err != nil {
return err
}
}
}
}
// Refresh exported BGP prefixes on local member.
err = n.forwardBGPSetupPrefixes()
if err != nil {
return fmt.Errorf("Failed applying BGP prefixes for address forwards: %w", err)
}
reverter.Success()
return nil
}
// ForwardUpdate updates a network forward.
func (n *bridge) ForwardUpdate(listenAddress string, req api.NetworkForwardPut, clientType request.ClientType) error {
var curForwardID int64
var curForward *api.NetworkForward
var curNodeID sql.NullInt64
err := n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
var err error
networkID := n.ID()
dbRecords, err := dbCluster.GetNetworkForwards(ctx, tx.Tx(), dbCluster.NetworkForwardFilter{
NetworkID: &networkID,
ListenAddress: &listenAddress,
})
if err != nil {
return err
}
filteredRecords := make([]dbCluster.NetworkForward, 0, len(dbRecords))
for _, dbRecord := range dbRecords {
// bridge supports per-member forwards so do memberSpecific filtering
if !dbRecord.NodeID.Valid || (dbRecord.NodeID.Int64 == tx.GetNodeID()) {
filteredRecords = append(filteredRecords, dbRecord)
}
}
if len(filteredRecords) == 0 {
return api.StatusErrorf(http.StatusNotFound, "Network forward not found")
}
if len(filteredRecords) > 1 {
return api.StatusErrorf(http.StatusConflict, "Network forward found on more than one cluster member. Please target a specific member")
}
curForwardID = filteredRecords[0].ID
curNodeID = filteredRecords[0].NodeID
curForward, err = filteredRecords[0].ToAPI(ctx, tx.Tx())
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
_, err = n.forwardValidate(net.ParseIP(curForward.ListenAddress), &req)
if err != nil {
return err
}
curForwardEtagHash, err := localUtil.EtagHash(curForward.Etag())
if err != nil {
return err
}
newForward := api.NetworkForward{
ListenAddress: curForward.ListenAddress,
NetworkForwardPut: req,
}
newForwardEtagHash, err := localUtil.EtagHash(newForward.Etag())
if err != nil {
return err
}
if curForwardEtagHash == newForwardEtagHash {
return nil // Nothing has changed.
}
reverter := revert.New()
defer reverter.Fail()
err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
fwd := dbCluster.NetworkForward{
NetworkID: n.ID(),
NodeID: curNodeID,
ListenAddress: listenAddress,
Description: newForward.Description,
Ports: newForward.Ports,
}
err = dbCluster.UpdateNetworkForward(ctx, tx.Tx(), n.ID(), listenAddress, fwd)
if err != nil {
return err
}
err = dbCluster.UpdateNetworkForwardConfig(ctx, tx.Tx(), curForwardID, newForward.Config)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
reverter.Add(func() {
_ = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
fwd := dbCluster.NetworkForward{
NetworkID: n.ID(),
NodeID: curNodeID,
ListenAddress: listenAddress,
Description: curForward.Description,
Ports: curForward.Ports,
}
err = dbCluster.UpdateNetworkForward(ctx, tx.Tx(), n.ID(), listenAddress, fwd)
if err != nil {
return err
}
err = dbCluster.UpdateNetworkForwardConfig(ctx, tx.Tx(), curForwardID, curForward.Config)
if err != nil {
return err
}
return nil
})
_ = n.forwardSetupFirewall()
_ = n.forwardBGPSetupPrefixes()
})
err = n.forwardSetupFirewall()
if err != nil {
return err
}
reverter.Success()
return nil
}
// ForwardDelete deletes a network forward.
func (n *bridge) ForwardDelete(listenAddress string, clientType request.ClientType) error {
memberSpecific := true // bridge supports per-member forwards.
var forwardID int64
var forward *api.NetworkForward
err := n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
var err error
networkID := n.ID()
dbRecords, err := dbCluster.GetNetworkForwards(ctx, tx.Tx(), dbCluster.NetworkForwardFilter{
NetworkID: &networkID,
ListenAddress: &listenAddress,
})
if err != nil {
return err
}
filteredRecords := make([]dbCluster.NetworkForward, 0, len(dbRecords))
for _, dbRecord := range dbRecords {
// bridge supports per-member forwards so do memberSpecific filtering
if !dbRecord.NodeID.Valid || (dbRecord.NodeID.Int64 == tx.GetNodeID()) {
filteredRecords = append(filteredRecords, dbRecord)
}
}
if len(filteredRecords) == 0 {
return api.StatusErrorf(http.StatusNotFound, "Network forward not found")
}
if len(filteredRecords) > 1 {
return api.StatusErrorf(http.StatusConflict, "Network forward found on more than one cluster member. Please target a specific member")
}
forwardID = filteredRecords[0].ID
forward, err = filteredRecords[0].ToAPI(ctx, tx.Tx())
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
reverter := revert.New()
defer reverter.Fail()
err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return dbCluster.DeleteNetworkForward(ctx, tx.Tx(), n.ID(), forwardID)
})
if err != nil {
return err
}
reverter.Add(func() {
_ = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
nodeID := sql.NullInt64{
Valid: memberSpecific,
Int64: tx.GetNodeID(),
}
dbRecord := dbCluster.NetworkForward{
NetworkID: n.ID(),
NodeID: nodeID,
ListenAddress: forward.ListenAddress,
Description: forward.Description,
Ports: forward.Ports,
}
if forward.Ports == nil {
dbRecord.Ports = []api.NetworkForwardPort{}
}
forwardID, err = dbCluster.CreateNetworkForward(ctx, tx.Tx(), dbRecord)
if err != nil {
return err
}
err = dbCluster.CreateNetworkForwardConfig(ctx, tx.Tx(), forwardID, forward.Config)
if err != nil {
return err
}
return nil
})
_ = n.forwardSetupFirewall()
_ = n.forwardBGPSetupPrefixes()
})
err = n.forwardSetupFirewall()
if err != nil {
return err
}
// Refresh exported BGP prefixes on local member.
err = n.forwardBGPSetupPrefixes()
if err != nil {
return fmt.Errorf("Failed applying BGP prefixes for address forwards: %w", err)
}
reverter.Success()
return nil
}
// forwardSetupFirewall applies all network address forwards defined for this network and this member.
func (n *bridge) forwardSetupFirewall() error {
var forwards map[int64]*api.NetworkForward
err := n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
var err error
networkID := n.ID()
dbRecords, err := dbCluster.GetNetworkForwards(ctx, tx.Tx(), dbCluster.NetworkForwardFilter{
NetworkID: &networkID,
})
if err != nil {
return err
}
forwards = make(map[int64]*api.NetworkForward)
for _, dbRecord := range dbRecords {
if !dbRecord.NodeID.Valid || (dbRecord.NodeID.Int64 == tx.GetNodeID()) {
forward, err := dbRecord.ToAPI(ctx, tx.Tx())
if err != nil {
return err
}
forwards[dbRecord.ID] = forward
}
}
return err
})
if err != nil {
return fmt.Errorf("Failed loading network forwards: %w", err)
}
var fwForwards []firewallDrivers.AddressForward
ipVersions := make(map[uint]struct{})
for _, forward := range forwards {
// Convert listen address to subnet so we can check its valid and can be used.
listenAddressNet, err := ParseIPToNet(forward.ListenAddress)
if err != nil {
return fmt.Errorf("Failed parsing address forward listen address %q: %w", forward.ListenAddress, err)
}
// Track which IP versions we are using.
if listenAddressNet.IP.To4() == nil {
ipVersions[6] = struct{}{}
} else {
ipVersions[4] = struct{}{}
}
portMaps, err := n.forwardValidate(listenAddressNet.IP, &forward.NetworkForwardPut)
if err != nil {
return fmt.Errorf("Failed validating firewall address forward for listen address %q: %w", forward.ListenAddress, err)
}
fwForwards = append(fwForwards, n.forwardConvertToFirewallForwards(listenAddressNet.IP, net.ParseIP(forward.Config["target_address"]), portMaps)...)
}
if len(forwards) > 0 {
// Check if br_netfilter is enabled to, and warn if not.
brNetfilterWarning := false
for ipVersion := range ipVersions {
err = BridgeNetfilterEnabled(ipVersion)
if err != nil {
brNetfilterWarning = true
msg := fmt.Sprintf("IPv%d bridge netfilter not enabled. Instances using the bridge will not be able to connect to the forward listen IPs", ipVersion)
n.logger.Warn(msg, logger.Ctx{"err": err})
err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.UpsertWarningLocalNode(ctx, n.project, dbCluster.TypeNetwork, int(n.id), warningtype.ProxyBridgeNetfilterNotEnabled, fmt.Sprintf("%s: %v", msg, err))
})
if err != nil {
n.logger.Warn("Failed to create warning", logger.Ctx{"err": err})
}
}
}
if !brNetfilterWarning {
err = warnings.ResolveWarningsByLocalNodeAndProjectAndTypeAndEntity(n.state.DB.Cluster, n.project, warningtype.ProxyBridgeNetfilterNotEnabled, dbCluster.TypeNetwork, int(n.id))
if err != nil {
n.logger.Warn("Failed to resolve warning", logger.Ctx{"err": err})
}
}
}
err = n.state.Firewall.NetworkApplyForwards(n.name, fwForwards)
if err != nil {
return fmt.Errorf("Failed applying firewall address forwards: %w", err)
}
return nil
}
// Leases returns a list of leases for the bridged network. It will reach out to other cluster members as needed.
// The projectName passed here refers to the initial project from the API request which may differ from the network's project.
func (n *bridge) Leases(projectName string, clientType request.ClientType) ([]api.NetworkLease, error) {
var err error
var projectMacs []string
leases := []api.NetworkLease{}
// Get all static leases.
if clientType == request.ClientTypeNormal {
// If requested project matches network's project then include gateway and downstream uplink IPs.
if projectName == n.project {
// Add our own gateway IPs.
for _, addr := range []string{n.config["ipv4.address"], n.config["ipv6.address"]} {
ip, _, _ := net.ParseCIDR(addr)
if ip != nil {
leases = append(leases, api.NetworkLease{
Hostname: fmt.Sprintf("%s.gw", n.Name()),
Address: ip.String(),
Type: "gateway",
})
}
}
// Include downstream OVN routers using the network as an uplink.
var projectNetworks map[string]map[int64]api.Network
err = n.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
projectNetworks, err = tx.GetCreatedNetworks(ctx)
return err
})
if err != nil {
return nil, err
}
// Look for networks using the current network as an uplink.
for projectName, networks := range projectNetworks {
for _, network := range networks {
if network.Config["network"] != n.name {
continue
}
// Found a network, add leases.
for _, k := range []string{"volatile.network.ipv4.address", "volatile.network.ipv6.address"} {
v := network.Config[k]
if v != "" {
leases = append(leases, api.NetworkLease{
Hostname: fmt.Sprintf("%s-%s.uplink", projectName, network.Name),
Address: v,
Type: "uplink",
})
}
}
}
}
}
// Get all the instances in the requested project that are connected to this network.
filter := dbCluster.InstanceFilter{Project: &projectName}
err = UsedByInstanceDevices(n.state, n.Project(), n.Name(), n.Type(), func(inst db.InstanceArgs, nicName string, nicConfig map[string]string) error {
// Fill in the hwaddr from volatile.
if nicConfig["hwaddr"] == "" {
nicConfig["hwaddr"] = inst.Config[fmt.Sprintf("volatile.%s.hwaddr", nicName)]
}
// Record the MAC.
hwAddr, _ := net.ParseMAC(nicConfig["hwaddr"])
if hwAddr != nil {
projectMacs = append(projectMacs, hwAddr.String())
}
// Add the lease.
nicIP4 := net.ParseIP(nicConfig["ipv4.address"])
if nicIP4 != nil {
leases = append(leases, api.NetworkLease{
Hostname: inst.Name,
Address: nicIP4.String(),
Hwaddr: hwAddr.String(),
Type: "static",
Location: inst.Node,
})
}
nicIP6 := net.ParseIP(nicConfig["ipv6.address"])
if nicIP6 != nil {
leases = append(leases, api.NetworkLease{
Hostname: inst.Name,
Address: nicIP6.String(),
Hwaddr: hwAddr.String(),
Type: "static",
Location: inst.Node,
})
}
// Add EUI64 records.
_, netIP6, _ := net.ParseCIDR(n.config["ipv6.address"])
if netIP6 != nil && hwAddr != nil && util.IsFalseOrEmpty(n.config["ipv6.dhcp.stateful"]) {
eui64IP6, err := eui64.ParseMAC(netIP6.IP, hwAddr)
if err == nil {
leases = append(leases, api.NetworkLease{
Hostname: inst.Name,
Address: eui64IP6.String(),
Hwaddr: hwAddr.String(),
Type: "dynamic",
Location: inst.Node,
})
}
}
return nil
}, filter)
if err != nil {
return nil, err
}
}
// Get dynamic leases.
leaseFile := internalUtil.VarPath("networks", n.name, "dnsmasq.leases")
if !util.PathExists(leaseFile) {
return leases, nil
}
content, err := os.ReadFile(leaseFile)
if err != nil {
return nil, err
}
for _, lease := range strings.Split(string(content), "\n") {
fields := strings.Fields(lease)
if len(fields) >= 5 {
// Parse the MAC.
mac := GetMACSlice(fields[1])
macStr := strings.Join(mac, ":")
if len(macStr) < 17 && fields[4] != "" {
macStr = fields[4][len(fields[4])-17:]
}
// Look for an existing static entry.
found := false
for _, entry := range leases {
if entry.Hwaddr == macStr && entry.Address == fields[2] {
found = true
break
}
}
if found {
continue
}
// DHCPv6 leases can't be tracked down to a MAC so clear the field.
// This means that instance project filtering will not work on IPv6 leases.
if strings.Contains(fields[2], ":") {
macStr = ""
}
// Skip leases that don't match any of the instance MACs from the project (only when we
// have populated the projectMacs list in ClientTypeNormal mode). Otherwise get all local
// leases and they will be filtered on the server handling the end user request.
if clientType == request.ClientTypeNormal && macStr != "" && !slices.Contains(projectMacs, macStr) {
continue
}
// Add the lease to the list.
leases = append(leases, api.NetworkLease{
Hostname: fields[3],
Address: fields[2],
Hwaddr: macStr,
Type: "dynamic",
Location: n.state.ServerName,
})
}
}
// Collect leases from other servers.
if clientType == request.ClientTypeNormal {
notifier, err := cluster.NewNotifier(n.state, n.state.Endpoints.NetworkCert(), n.state.ServerCert(), cluster.NotifyAll)
if err != nil {
return nil, err
}
err = notifier(func(client incus.InstanceServer) error {
memberLeases, err := client.GetNetworkLeases(n.name)
if err != nil {
return err
}
// Add local leases from other members, filtering them for MACs that belong to the project.
for _, lease := range memberLeases {
if lease.Hwaddr != "" && slices.Contains(projectMacs, lease.Hwaddr) {
leases = append(leases, lease)
}
}
return nil
})
if err != nil {
return nil, err
}
}
return leases, nil
}
// UsesDNSMasq indicates if network's config indicates if it needs to use dnsmasq.
func (n *bridge) UsesDNSMasq() bool {
// Skip dnsmasq when no connectivity is configured.
if util.IsNoneOrEmpty(n.config["ipv4.address"]) && util.IsNoneOrEmpty(n.config["ipv6.address"]) {
return false
}
// Start dnsmasq if providing instance DNS records.
if n.config["dns.mode"] != "none" {
return true
}
// Start dnsmassq if IPv6 is used (needed for SLAAC or DHCPv6).
if !util.IsNoneOrEmpty(n.config["ipv6.address"]) {
ipAddress, _, err := net.ParseCIDR(n.config["ipv6.address"])
if err != nil {
return true
}
// Only require dnsmasq if using a global address.
if !ipAddress.IsLinkLocalUnicast() {
return true
}
}
// Start dnsmasq if IPv4 DHCP is used.
if !util.IsNoneOrEmpty(n.config["ipv4.address"]) && n.hasDHCPv4() {
return true
}
return false
}
func (n *bridge) deleteChildren() error {
// Get a list of interfaces
ifaces, err := net.Interfaces()
if err != nil {
return err
}
var externalInterfaces []string
if n.config["bridge.external_interfaces"] != "" {
for _, entry := range strings.Split(n.config["bridge.external_interfaces"], ",") {
entry = strings.Split(strings.TrimSpace(entry), "/")[0]
externalInterfaces = append(externalInterfaces, entry)
}
}
kinds := []string{
"vxlan",
"gretap",
"dummy",
}
for _, iface := range ifaces {
l, err := ip.LinkByName(iface.Name)
if err != nil {
// If we can't load the link, chances are the interface isn't one that we should be deleting.
continue
}
if l.Master != n.name || slices.Contains(externalInterfaces, iface.Name) || !slices.Contains(kinds, l.Kind) {
continue
}
err = l.Delete()
if err != nil {
return err
}
}
return nil
}
|