1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634
|
/* information about connections between hosts and clients
*
* Copyright (C) 1998-2002,2010,2013,2018 D. Hugh Redelmeier <hugh@mimosa.com>
* Copyright (C) 2003-2008 Michael Richardson <mcr@xelerance.com>
* Copyright (C) 2003-2011 Paul Wouters <paul@xelerance.com>
* Copyright (C) 2008-2009 David McCullough <david_mccullough@securecomputing.com>
* Copyright (C) 2009-2011 Avesh Agarwal <avagarwa@redhat.com>
* Copyright (C) 2010 Bart Trojanowski <bart@jukie.net>
* Copyright (C) 2010 Shinichi Furuso <Shinichi.Furuso@jp.sony.com>
* Copyright (C) 2010,2013 Tuomo Soini <tis@foobar.fi>
* Copyright (C) 2012-2017 Paul Wouters <paul@libreswan.org>
* Copyright (C) 2012 Philippe Vouters <Philippe.Vouters@laposte.net>
* Copyright (C) 2012 Bram <bram-bcrafjna-erqzvar@spam.wizbit.be>
* Copyright (C) 2013 Kim B. Heino <b@bbbs.net>
* Copyright (C) 2013,2017 Antony Antony <antony@phenome.org>
* Copyright (C) 2013,2018 Matt Rogers <mrogers@redhat.com>
* Copyright (C) 2013 Florian Weimer <fweimer@redhat.com>
* Copyright (C) 2015-2020 Paul Wouters <pwouters@redhat.com>
* Copyright (C) 2016-2020 Andrew Cagney <cagney@gnu.org>
* Copyright (C) 2017 Mayank Totale <mtotale@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <errno.h>
#include <limits.h>
#include "sysdep.h"
#include "constants.h"
#include "lswalloc.h"
#include "lswconf.h"
#include "id.h"
#include "x509.h"
#include "certs.h"
#include "secrets.h"
#include "defs.h"
#include "connections.h" /* needs id.h */
#include "pending.h"
#include "foodgroups.h"
#include "packet.h"
#include "demux.h" /* needs packet.h */
#include "state.h"
#include "timer.h"
#include "ipsec_doi.h" /* needs demux.h and state.h */
#include "server.h"
#include "kernel.h" /* needs connections.h */
#include "log.h"
#include "keys.h"
#include "whack.h"
#include "ike_alg.h"
#include "kernel_alg.h"
#include "plutoalg.h"
#include "ikev1_xauth.h"
#include "addresspool.h"
#include "nat_traversal.h"
#include "pluto_x509.h"
#include "nss_cert_verify.h" /* for cert_VerifySubjectAltName() */
#include "nss_cert_load.h"
#include "ikev2.h"
#include "virtual_ip.h" /* needs connections.h */
#include "hostpair.h"
#include "lswfips.h"
#include "crypto.h"
#include "kernel_xfrm.h"
#include "ip_address.h"
#include "ip_info.h"
#include "keyhi.h" /* for SECKEY_DestroyPublicKey */
#include "state_db.h"
# include "kernel_xfrm_interface.h"
#include "iface.h"
#include "ip_selector.h"
#include "nss_cert_reread.h"
#include "security_selinux.h"
struct connection *connections = NULL;
#define MINIMUM_IPSEC_SA_RANDOM_MARK 65536
static uint32_t global_marks = MINIMUM_IPSEC_SA_RANDOM_MARK;
static bool idr_wildmatch(const struct end *this, const struct id *b, struct logger *logger);
/*
* Find a connection by name.
*
* no_inst: don't accept a CK_INSTANCE.
*
* If none is found, and strict&&!queit, a diagnostic is logged to
* whack.
*
* XXX: Fun fact: this function re-orders the list, moving the entry
* to the front as a side effect (ulgh)!.
*/
struct connection *conn_by_name(const char *nm, bool no_inst)
{
struct connection *p, *prev;
dbg("FOR_EACH_CONNECTION_... in %s", __func__);
for (prev = NULL, p = connections; ; prev = p, p = p->ac_next) {
if (p == NULL) {
break;
}
if (streq(p->name, nm) &&
(!no_inst || p->kind != CK_INSTANCE)) {
if (prev != NULL) {
/* remove p from list */
prev->ac_next = p->ac_next;
/* and stick it on front */
p->ac_next = connections;
connections = p;
}
break;
}
}
return p;
}
void release_connection(struct connection *c, bool relations, struct fd *whackfd)
{
if (c->kind == CK_INSTANCE) {
/*
* This does everything we need.
* Note that we will be called recursively by delete_connection,
* but kind will be CK_GOING_AWAY.
*/
delete_connection(c, relations);
} else {
flush_pending_by_connection(c);
delete_states_by_connection(c, relations, whackfd);
unroute_connection(c);
}
}
/* Delete a connection */
static void delete_end(struct end *e)
{
free_id_content(&e->id);
if (e->cert.u.nss_cert != NULL)
CERT_DestroyCertificate(e->cert.u.nss_cert);
free_chunk_content(&e->ca);
pfreeany(e->updown);
pfreeany(e->host_addr_name);
pfreeany(e->xauth_password);
pfreeany(e->xauth_username);
pfreeany(e->ckaid);
free_chunk_content(&e->sec_label);
}
static void delete_sr(struct spd_route *sr)
{
delete_end(&sr->this);
delete_end(&sr->that);
}
/*
* delete_connection -- removes a connection by pointer
*
* @c - the connection pointer
* @relations - whether to delete any instances as well.
* @connection_valid - apply sanity checks
*
*/
static void discard_connection(struct connection *c,
bool connection_valid,
struct logger *connection_logger);
void delete_connection(struct connection *c, bool relations)
{
struct fd *whackfd = whack_log_fd;
/* XXX: something better? */
close_any(&c->logger->global_whackfd);
c->logger->global_whackfd = dup_any(whackfd); /* freed by discard_conection() */
/*
* Must be careful to avoid circularity:
* we mark c as going away so it won't get deleted recursively.
*/
passert(c->kind != CK_GOING_AWAY);
if (c->kind == CK_INSTANCE) {
if ((c->policy & POLICY_OPPORTUNISTIC) == LEMPTY) {
address_buf b;
llog(RC_LOG, c->logger,
"deleting connection instance with peer %s {isakmp=#%lu/ipsec=#%lu}",
str_address_sensitive(&c->spd.that.host_addr, &b),
c->newest_isakmp_sa, c->newest_ipsec_sa);
}
c->kind = CK_GOING_AWAY;
if (c->pool != NULL) {
free_that_address_lease(c);
}
}
release_connection(c, relations, whackfd); /* won't delete c */
/* make a copy of the logger so it works even after the delete */
struct logger *logger = clone_logger(c->logger, HERE); /* must-free */
discard_connection(c, true/*connection_valid*/, logger);
free_logger(&logger, HERE);
}
static void discard_connection(struct connection *c,
bool connection_valid,
struct logger *connection_logger)
{
if (c->kind == CK_GROUP)
delete_group(c);
if (c->pool != NULL)
unreference_addresspool(c);
if (IS_XFRMI && c->xfrmi != NULL)
unreference_xfrmi(c, connection_logger);
/* find and delete c from connections list */
/* XXX: if in list, remove_list_entry(c->ac_next); */
for (struct connection **head = &connections;
*head != NULL; head = &(*head)->ac_next) {
if (*head == c) {
*head = c->ac_next;
c->ac_next = NULL;
break;
}
}
/* find and delete c from the host pair list */
host_pair_remove_connection(c, connection_valid);
flush_revival(c);
pfreeany(c->name);
pfreeany(c->foodgroup);
pfreeany(c->connalias);
pfreeany(c->vti_iface);
pfreeany(c->modecfg_dns);
pfreeany(c->modecfg_domains);
pfreeany(c->modecfg_banner);
pfreeany(c->dnshostname);
pfreeany(c->redirect_to);
pfreeany(c->accept_redirect_to);
free_logger(&c->logger, HERE);
/* deal with top spd_route and then the rest */
passert(c->spd.this.virt == NULL);
virtual_ip_delref(&c->spd.this.virt, HERE);
virtual_ip_delref(&c->spd.that.virt, HERE);
struct spd_route *sr = c->spd.spd_next;
delete_sr(&c->spd);
while (sr != NULL) {
struct spd_route *next_sr = sr->spd_next;
passert(sr->this.virt == NULL);
passert(sr->that.virt == NULL);
delete_sr(sr);
/* ??? should we: pfree(sr); */
sr = next_sr;
}
proposals_delref(&c->ike_proposals.p);
proposals_delref(&c->child_proposals.p);
free_ikev2_proposals(&c->v2_ike_proposals);
free_ikev2_proposals(&c->v2_ike_auth_child_proposals);
free_ikev2_proposals(&c->v2_create_child_proposals);
c->v2_create_child_proposals_default_dh = NULL; /* static pointer */
remove_connection_from_db(c);
pfree(c);
}
int foreach_connection_by_alias(const char *alias, struct fd *whackfd,
int (*f)(struct connection *c,
struct fd *whackfd,
void *arg),
void *arg)
{
struct connection *p, *pnext;
int count = 0;
dbg("FOR_EACH_CONNECTION_... in %s", __func__);
for (p = connections; p != NULL; p = pnext) {
pnext = p->ac_next;
if (lsw_alias_cmp(alias, p->connalias))
count += (*f)(p, whackfd, arg);
}
return count;
}
static int delete_connection_wrap(struct connection *c,
struct fd *unused_whackfd UNUSED,
void *arg)
{
bool *barg = (bool *)arg;
delete_connection(c, *barg);
return 1;
}
/* Delete connections with the specified name */
void delete_connections_by_name(const char *name, bool strict, struct fd *whackfd)
{
bool f = FALSE;
passert(name != NULL);
struct connection *c = conn_by_name(name, strict);
if (c == NULL) {
(void)foreach_connection_by_alias(name, whackfd,
delete_connection_wrap,
&f);
} else {
for (; c != NULL; c = conn_by_name(name, false/*!strict*/))
delete_connection(c, FALSE);
}
}
void delete_every_connection(void)
{
while (connections != NULL)
delete_connection(connections, TRUE);
}
ip_port end_host_port(const struct end *end, const struct end *other)
{
unsigned port;
if (end->raw.host.ikeport != 0) {
/*
* The END's IKEPORT was specified in the config file.
* Use that.
*/
port = end->raw.host.ikeport;
} else if (other->raw.host.ikeport != 0) {
/*
* The other end's IKEPORT was specified in the config
* file. Since specifying an IKEPORT implies ESP
* encapsulation (i.e. IKE packets must include the
* ESP=0 prefix), send packets from the encapsulating
* NAT_IKE_UDP_PORT.
*/
port = NAT_IKE_UDP_PORT;
} else if (other->host_encap) {
/*
* See above. Presumably an instance which previously
* had a natted port and is being revived.
*/
port = NAT_IKE_UDP_PORT;
} else {
port = IKE_UDP_PORT;
}
return ip_hport(port);
}
void update_ends_from_this_host_addr(struct end *this, struct end *that)
{
const struct ip_info *afi = address_type(&this->host_addr);
if (afi == NULL) {
dbg("%s.host_addr's address family is unknown; skipping default_end()",
this->leftright);
return;
}
if (address_is_any(&this->host_addr)) {
dbg("%s.host_addr's is %%any; skipping default_end()",
this->leftright);
return;
}
dbg("updating connection from %s.host_addr", this->leftright);
/* Default ID to IP (but only if not NO_IP -- WildCard) */
if (this->id.kind == ID_NONE && address_is_specified(&this->host_addr)) {
this->id.kind = afi->id_ip_addr;
this->id.ip_addr = this->host_addr;
this->has_id_wildcards = FALSE;
}
/* propagate this HOST_ADDR to that. */
if (address_is_unset(&that->host_nexthop) ||
address_is_any(&that->host_nexthop)) {
that->host_nexthop = this->host_addr;
address_buf ab;
dbg("%s host_nexthop %s",
that->leftright, str_address(&that->host_nexthop, &ab));
}
/*
* If THAT has an IKEPORT (which means messages are ESP=0
* prefixed), then THIS must send from either IKEPORT or the
* NAT port (and also ESP=0 prefix messages).
*/
unsigned host_port = hport(end_host_port(this, that));
dbg("%s() %s.host_port: %u->%u", __func__, this->leftright,
this->host_port, host_port);
this->host_port = host_port;
/* Default client to subnet containing only self */
if (!this->has_client) {
/*
* Default client to a subnet containing only self.
*
* For instance, the config file omitted subnet, but
* specified protoport; merge that.
*/
this->client = selector_from_address_protoport(&this->host_addr,
&this->raw.client.protoport);
}
if (this->sendcert == 0) {
/* uninitialized (ugly hack) */
this->sendcert = CERT_SENDIFASKED;
}
}
/*
* Format the topology of a connection end, leaving out defaults.
* Used to construct strings of the form:
*
* LOCAL_END ...END_REMOTE
*
* where END_REMOTE is roughly formatted as the mirror image of
* LOCAL_END. IS_LEFT (confusing name given connection left/right)
* determines if the LHS or RHS string is being emitted.. LOCAL_END's
* longest string is:
*
* client === host : port [ host_id ] --- HOP
*
* Note: if that == NULL, skip nexthop Returns strlen of formatted
* result (length excludes NUL at end).
*/
static void jam_end_host(struct jambuf *buf, const struct end *this, lset_t policy)
{
/* HOST */
bool dohost_name;
bool dohost_port;
if (address_is_unset(&this->host_addr) ||
address_is_any(&this->host_addr)) {
dohost_port = false;
if (this->host_type == KH_IPHOSTNAME) {
dohost_name = true;
jam_string(buf, "%dns");
} else {
dohost_name = false;
switch (policy & (POLICY_GROUP | POLICY_OPPORTUNISTIC)) {
case POLICY_GROUP:
jam_string(buf, "%group");
break;
case POLICY_OPPORTUNISTIC:
jam_string(buf, "%opportunistic");
break;
case POLICY_GROUP | POLICY_OPPORTUNISTIC:
jam_string(buf, "%opportunisticgroup");
break;
default:
jam_string(buf, "%any");
break;
}
}
} else if (is_virtual_end(this)) {
dohost_name = false;
dohost_port = false;
jam_string(buf, "%virtual");
} else {
dohost_name = true;
dohost_port = true;
jam_address_sensitive(buf, &this->host_addr);
}
/* <NAME> */
if (dohost_name && this->host_addr_name != NULL) {
jam(buf, "<%s>", this->host_addr_name);
}
/*
* XXX: only print anomalies: when the host address is
* "non-zero", a non-IKE_UDP_PORT; and when zero, any non-zero
* port.
*/
if (dohost_port ? (this->raw.host.ikeport != 0 || this->host_port != IKE_UDP_PORT) :
this->host_port != 0) {
/*
* XXX: Part of the problem is that code is stomping
* on the HOST_ADDR's port setting it to the CLIENT's
* port.
*
* XXX: Format this as ADDRESS:PORT<name> not
* ADDRESS<name>:PORT? Or always emit the PORT?
*/
jam(buf, ":%u", this->host_port);
}
}
static void jam_end_client(struct jambuf *buf, const struct end *this,
lset_t policy, bool is_left)
{
/* [CLIENT===] or [===CLIENT] */
if (!this->has_client) {
return;
}
if (subnet_is_unset(&this->client)) {
return;
}
bool boring = (subnet_contains_all_addresses(&this->client) &&
(policy & (POLICY_GROUP | POLICY_OPPORTUNISTIC)));
if (!boring && !is_left) {
jam_string(buf, "===");
}
if (boring) {
/* boring case */
} else if (is_virtual_end(this)) {
if (is_virtual_vhost(this))
jam_string(buf, "vhost:?");
else
jam_string(buf, "vnet:?");
} else if (subnet_contains_no_addresses(&this->client)) {
jam_string(buf, "?");
} else {
jam_subnet(buf, &this->client);
}
if (!boring && is_left) {
jam_string(buf, "===");
}
}
static void jam_end_protoport(struct jambuf *buf, const struct end *this)
{
/* payload portocol and port */
if (this->has_port_wildcard) {
jam(buf, ":%u/%%any", this->protocol);
} else if (this->port || this->protocol) {
jam(buf, ":%u/%u", this->protocol, this->port);
}
}
static void jam_end_id(struct jambuf *buf, const struct end *this)
{
/* id, if different from host */
bool open_paren = false;
if (!(this->id.kind == ID_NONE ||
(id_is_ipaddr(&this->id) &&
sameaddr(&this->id.ip_addr, &this->host_addr)))) {
open_paren = true;
jam_string(buf, "[");
jam_id(buf, &this->id, jam_sanitized_bytes);
}
if (this->modecfg_server || this->modecfg_client ||
this->xauth_server || this->xauth_client ||
this->sendcert != cert_defaultcertpolicy) {
if (open_paren) {
jam_string(buf, ",");
} else {
open_paren = true;
jam_string(buf, "[");
}
if (this->modecfg_server)
jam_string(buf, "MS");
if (this->modecfg_client)
jam_string(buf, "+MC");
if (this->cat)
jam_string(buf, "+CAT");
if (this->xauth_server)
jam_string(buf, "+XS");
if (this->xauth_client)
jam_string(buf, "+XC");
switch (this->sendcert) {
case CERT_NEVERSEND:
jam(buf, "+S-C");
break;
case CERT_SENDIFASKED:
jam(buf, "+S?C");
break;
case CERT_ALWAYSSEND:
jam(buf, "+S=C");
break;
default:
jam(buf, "+UNKNOWN");
}
}
if (open_paren) {
jam_string(buf, "]");
}
}
static void jam_end_nexthop(struct jambuf *buf, const struct end *this,
const struct end *that, bool filter_rnh, bool is_left)
{
/* [---hop] */
if (that != NULL &&
!filter_rnh &&
!sameaddr(&this->host_nexthop, &that->host_addr)) {
if (is_left) {
jam_string(buf, "---");
}
jam_address(buf, &this->host_nexthop);
if (!is_left) {
jam_string(buf, "---");
}
}
}
static void jam_end(struct jambuf *buf,
const struct end *this,
const struct end *that,
bool is_left,
lset_t policy,
bool filter_rnh)
{
if (is_left) {
/* CLIENT=== */
jam_end_client(buf, this, policy, is_left);
/* HOST */
jam_end_host(buf, this, policy);
/* [ID+OPTS] */
jam_end_id(buf, this);
/* /PROTOCOL:PORT */
jam_end_protoport(buf, this);
/* ---NEXTHOP */
jam_end_nexthop(buf, this, that, filter_rnh, is_left);
} else {
/* HOPNEXT--- */
jam_end_nexthop(buf, this, that, filter_rnh, is_left);
/* HOST */
jam_end_host(buf, this, policy);
/* [ID+OPTS] */
jam_end_id(buf, this);
/* /PROTOCOL:PORT */
jam_end_protoport(buf, this);
/* ===CLIENT */
jam_end_client(buf, this, policy, is_left);
}
}
size_t format_end(char *buf,
size_t buf_len,
const struct end *this,
const struct end *that,
bool is_left,
lset_t policy,
bool filter_rnh)
{
struct jambuf b = array_as_jambuf(buf, buf_len);
jam_end(&b, this, that, is_left, policy, filter_rnh);
return strlen(buf);
}
/*
* format topology of a connection.
* Two symmetric ends separated by ...
*/
#define CONN_BUF_LEN (2 * (END_BUF - 1) + 4)
static char *format_connection(char *buf, size_t buf_len,
const struct connection *c,
const struct spd_route *sr)
{
size_t w =
format_end(buf, buf_len, &sr->this, &sr->that, TRUE, LEMPTY, FALSE);
snprintf(buf + w, buf_len - w, "...");
w += strlen(buf + w);
(void) format_end(buf + w, buf_len - w, &sr->that, &sr->this, FALSE, c->policy,
oriented(*c));
return buf;
}
/* spd_route's with end's get copied in xauth.c */
void unshare_connection_end(struct end *e)
{
e->id = clone_id(&e->id, "unshare connection id");
if (e->cert.u.nss_cert != NULL) {
e->cert.u.nss_cert = CERT_DupCertificate(e->cert.u.nss_cert);
passert(e->cert.u.nss_cert != NULL);
}
e->ca = clone_hunk(e->ca, "ca string");
e->updown = clone_str(e->updown, "updown");
e->xauth_username = clone_str(e->xauth_username, "xauth username");
e->xauth_password = clone_str(e->xauth_password, "xauth password");
e->host_addr_name = clone_str(e->host_addr_name, "host ip");
e->virt = virtual_ip_addref(e->virt, HERE);
if (e->ckaid != NULL) {
e->ckaid = clone_thing(*e->ckaid, "ckaid");
}
}
/*
* unshare_connection: after a struct connection has been copied,
* duplicate anything it references so that unshareable resources
* are no longer shared. Typically strings, but some other things too.
*
* Think of this as converting a shallow copy to a deep copy
*
* XXX: unshare_connection() and the shallow clone should be merged
* into a routine that allocates a new connection and then explicitly
* copy over the data. Cloning pointers and then trying to fix them
* up after the event, a guaranteed way to create use-after-free
* problems.
*/
static void unshare_connection(struct connection *c)
{
c->foodgroup = clone_str(c->foodgroup, "connection foodgroup");
c->modecfg_dns = clone_str(c->modecfg_dns,
"connection modecfg_dns");
c->modecfg_domains = clone_str(c->modecfg_domains,
"connection modecfg_domains");
c->modecfg_banner = clone_str(c->modecfg_banner,
"connection modecfg_banner");
c->dnshostname = clone_str(c->dnshostname, "connection dnshostname");
/* duplicate any alias, adding spaces to the beginning and end */
c->connalias = clone_str(c->connalias, "connection alias");
c->vti_iface = clone_str(c->vti_iface, "connection vti_iface");
c->redirect_to = clone_str(c->redirect_to,\
"connection redirect_to");
c->accept_redirect_to = clone_str(c->accept_redirect_to,\
"connection accept_redirect_to");
struct spd_route *sr;
for (sr = &c->spd; sr != NULL; sr = sr->spd_next) {
unshare_connection_end(&sr->this);
unshare_connection_end(&sr->that);
}
/* increment references to algo's, if any */
proposals_addref(&c->ike_proposals.p);
proposals_addref(&c->child_proposals.p);
if (c->pool != NULL)
reference_addresspool(c);
if (IS_XFRMI && c->xfrmi != NULL)
reference_xfrmi(c);
}
static int extract_end(struct end *dst,
const struct whack_end *src,
const char *leftright,
struct logger *logger/*connection "..."*/)
{
dst->leftright = leftright;
bool same_ca = 0;
/*
* decode id, if any
*
* For %fromcert, the load_end_cert*() call will update it.
*/
if (src->id == NULL) {
dst->id.kind = ID_NONE;
} else {
/*
* Cannot report errors due to low level nesting of functions,
* since it will try literal IP string conversions first. But
* atoid() will log real failures like illegal DNS chars already,
* and for @string ID's all chars are valid without processing.
*/
atoid(src->id, &dst->id);
}
if (src->sec_label != NULL) {
dst->sec_label = clone_bytes_as_chunk(src->sec_label, strlen(src->sec_label)+1, "struct end sec_label");
dbg("received sec_label '%s' from whack", src->sec_label);
}
/* decode CA distinguished name, if any */
dst->ca = EMPTY_CHUNK;
if (src->ca != NULL) {
if (streq(src->ca, "%same")) {
same_ca = 1;
} else if (!streq(src->ca, "%any")) {
err_t ugh;
/* convert the CA into a DN blob */
ugh = atodn(src->ca, &dst->ca); /* static result! */
if (ugh != NULL) {
llog(RC_LOG, logger,
"bad %s CA string '%s': %s (ignored)",
leftright, src->ca, ugh);
dst->ca = EMPTY_CHUNK;
} else {
dst->ca = clone_hunk(dst->ca, "ca string");
/* now try converting it back; isn't failing this a bug? */
ugh = parse_dn(dst->ca);
if (ugh != NULL) {
llog(RC_LOG, logger,
"error parsing %s CA converted to DN: %s",
leftright, ugh);
DBG_dump_hunk(NULL, dst->ca);
}
}
}
}
/*
* Try to find the cert / private key.
*
* XXX: Be lazy and simply warn about combinations such as
* cert+ckaid.
*
* Should this instead cross check?
*/
if (src->cert != NULL) {
if (src->ckaid != NULL) {
llog(RC_LOG, logger,
"warning: ignoring %s ckaid '%s' and using %s certificate '%s'",
dst->leftright, src->cert,
dst->leftright, src->cert);
}
if (src->rsasigkey != NULL) {
llog(RC_LOG, logger,
"warning: ignoring %s rsasigkey '%s' and using %s certificate '%s'",
dst->leftright, src->cert,
dst->leftright, src->cert);
}
CERTCertificate *cert = get_cert_by_nickname_from_nss(src->cert, logger);
if (cert == NULL) {
llog(RC_FATAL, logger,
"failed to add connection: %s certificate '%s' not found in the NSS database",
dst->leftright, src->cert);
return -1; /* fatal */
}
diag_t diag = add_end_cert_and_preload_private_key(cert, dst,
same_ca/*preserve_ca*/,
logger);
if (diag != NULL) {
log_diag(RC_FATAL, logger, &diag, "failed to add connection: ");
CERT_DestroyCertificate(cert);
return -1;
}
} else if (src->rsasigkey != NULL) {
if (src->ckaid != NULL) {
llog(RC_LOG, logger,
"warning: ignoring %s ckaid '%s' and using %s rsasigkey",
dst->leftright, src->ckaid, dst->leftright);
}
/*
* XXX: hack: whack will load the rsasigkey in a
* second message, this code just extracts the ckaid.
*/
const struct pubkey_type *type = &pubkey_type_rsa;
/* XXX: lifted from starter_whack_add_pubkey() */
char err_buf[TTODATAV_BUF];
char keyspace[1024 + 4];
size_t keylen;
/* ??? this value of err isn't used */
err_t err = ttodatav(src->rsasigkey, 0, 0,
keyspace, sizeof(keyspace), &keylen,
err_buf, sizeof(err_buf), 0);
union pubkey_content pkc;
keyid_t pubkey;
ckaid_t ckaid;
size_t size;
err = type->unpack_pubkey_content(&pkc, &pubkey, &ckaid, &size,
chunk2(keyspace, keylen));
if (err != NULL) {
llog(RC_FATAL, logger,
"failed to add connection: %s raw public key invalid: %s",
dst->leftright, err);
return -1;
}
ckaid_buf ckb;
dbg("saving %s CKAID %s extracted from raw %s public key",
dst->leftright, str_ckaid(&ckaid, &ckb), type->name);
dst->ckaid = clone_const_thing(ckaid, "raw pubkey's ckaid");
type->free_pubkey_content(&pkc);
/* try to pre-load the private key */
bool load_needed;
err = preload_private_key_by_ckaid(&ckaid, &load_needed, logger);
if (err != NULL) {
ckaid_buf ckb;
dbg("no private key matching %s CKAID %s: %s",
dst->leftright, str_ckaid(dst->ckaid, &ckb), err);
} else if (load_needed) {
ckaid_buf ckb;
llog(RC_LOG|LOG_STREAM/*not-whack-for-now*/, logger,
"loaded private key matching %s CKAID %s",
dst->leftright, str_ckaid(dst->ckaid, &ckb));
}
} else if (src->ckaid != NULL) {
ckaid_t ckaid;
err_t err = string_to_ckaid(src->ckaid, &ckaid);
if (err != NULL) {
/* should have been rejected by whack? */
/* XXX: don't trust whack */
llog(RC_FATAL, logger,
"failed to add connection: %s CKAID '%s' invalid: %s",
dst->leftright, src->ckaid, err);
return -1; /* fatal */
}
/*
* Always save the CKAID so lazy load of the private
* key will work.
*/
dst->ckaid = clone_thing(ckaid, "end ckaid");
/*
* See if there's a certificate matching the CKAID, if
* not assume things will later find the private key.
*/
CERTCertificate *cert = get_cert_by_ckaid_from_nss(&ckaid, logger);
if (cert != NULL) {
diag_t diag = add_end_cert_and_preload_private_key(cert, dst,
same_ca/*preserve_ca*/,
logger);
if (diag != NULL) {
log_diag(RC_FATAL, logger, &diag, "failed to add connection: ");
CERT_DestroyCertificate(cert);
return -1;
}
} else {
dbg("%s CKAID '%s' did not match a certificate in the NSS database",
dst->leftright, src->ckaid);
/* try to pre-load the private key */
bool load_needed;
err_t err = preload_private_key_by_ckaid(&ckaid, &load_needed, logger);
if (err != NULL) {
ckaid_buf ckb;
dbg("no private key matching %s CKAID %s: %s",
dst->leftright, str_ckaid(dst->ckaid, &ckb), err);
} else {
ckaid_buf ckb;
llog(RC_LOG|LOG_STREAM/*not-whack-for-now*/, logger,
"loaded private key matching %s CKAID %s",
dst->leftright, str_ckaid(dst->ckaid, &ckb));
}
}
}
/* does id have wildcards? */
dst->has_id_wildcards = id_count_wildcards(&dst->id) > 0;
/* the rest is simple copying of corresponding fields */
dst->host_type = src->host_type;
dst->host_addr = src->host_addr;
dst->host_addr_name = clone_str(src->host_addr_name, "host ip");
dst->host_nexthop = src->host_nexthop;
dst->host_srcip = src->host_srcip;
dst->host_vtiip = src->host_vtiip;
dst->ifaceip = src->ifaceip;
dst->modecfg_server = src->modecfg_server;
dst->modecfg_client = src->modecfg_client;
dst->cat = src->cat;
dst->pool_range = src->pool_range;
dst->xauth_server = src->xauth_server;
dst->xauth_client = src->xauth_client;
dst->xauth_username = clone_str(src->xauth_username, "xauth username");
dst->authby = src->authby;
/* save some defaults */
dst->raw.client.subnet = src->client;
dst->raw.client.protoport = src->protoport;
/*
* .has_client means that .client contains a hardwired value,
* if it doesn't then it is filled in later (for instance by
* instantiate() calling default_end() after host_addr is
* known).
*/
dst->has_client = src->has_client;
if (src->has_client) {
pexpect(!subnet_is_unset(&src->client));
dst->client = selector_from_subnet_protoport(&src->client,
&src->protoport);
}
dst->protocol = src->protoport.ipproto;
dst->port = src->protoport.hport;
dst->has_port_wildcard = protoport_has_any_port(&src->protoport);
dst->key_from_DNS_on_demand = src->key_from_DNS_on_demand;
dst->updown = clone_str(src->updown, "updown");
dst->sendcert = src->sendcert;
dst->raw.host.ikeport = src->host_ikeport;
if (src->host_ikeport > 65535) {
llog(RC_BADID, logger,
"%sikeport=%u must be between 1..65535, ignored",
leftright, src->host_ikeport);
dst->raw.host.ikeport = 0;
}
/*
* see if we can resolve the DNS name right now
* XXX this is WRONG, we should do this asynchronously, as part of
* the normal loading process
*/
switch (dst->host_type) {
case KH_IPHOSTNAME:
{
err_t er = domain_to_address(shunk1(dst->host_addr_name),
address_type(&dst->host_addr),
&dst->host_addr);
if (er != NULL) {
llog(RC_COMMENT, logger,
"failed to convert '%s' at load time: %s",
dst->host_addr_name, er);
}
break;
}
default:
break;
}
return same_ca;
}
static bool check_connection_end(const struct whack_end *this,
const struct whack_end *that,
const struct whack_message *wm,
struct logger *logger)
{
/*
* This should have been diagnosed by whack,
* so we need not be clear.
*
* XXX: don't trust whack.
* XXX: don't assume values were set (defaulted).
* XXX: don't assume unset's type is NULL.
* XXX: because both directions are tested some checks are redundant.
*
* !!! overloaded use of RC_CLASH
*/
/*
* Find a type for the host addresses. Order search by what
* was most liklely specified.
*/
const struct ip_info *type = (!address_is_unset(&this->host_addr) ? address_type(&this->host_addr) :
!address_is_unset(&this->host_nexthop) ? address_type(&this->host_nexthop) :
NULL);
if (type != NULL) {
if (!address_is_unset(&this->host_nexthop) &&
address_type(&this->host_nexthop) != type) {
llog(RC_CLASH, logger,
"host address family inconsistent: expecting %s but %snexthop is %s",
type->ip_name, this->leftright, address_type(&this->host_nexthop)->ip_name);
return false;
}
if (!address_is_unset(&that->host_addr) &&
address_type(&that->host_addr) != type) {
llog(RC_CLASH, logger,
"host address family inconsistent: expecting %s but %shost is %s",
type->ip_name, that->leftright, address_type(&that->host_addr)->ip_name);
return false;
}
if (!address_is_unset(&that->host_nexthop) &&
address_type(&that->host_nexthop) != type) {
llog(RC_CLASH, logger,
"host address family inconsistent: expecting %s but %snexthop is %s",
type->ip_name, that->leftright, address_type(&that->host_nexthop)->ip_name);
return false;
}
}
/* ??? seems like a nasty test (in-band, low-level) */
/* XXX: still nasty; just less low-level */
if (range_is_specified(&this->pool_range)) {
struct ip_pool *pool;
if (!find_addresspool(&this->pool_range, &pool, logger)) {
/* already logged */
return false;
}
}
if (subnet_is_specified(&this->client) &&
subnet_is_specified(&that->client)) {
/* IPv4 vs IPv6? */
if (subnet_type(&this->client) != subnet_type(&that->client)) {
/*
* !!! overloaded use of RC_CLASH
*/
llog(RC_CLASH, logger,
"address family inconsistency in this/that connection");
return FALSE;
}
}
/* MAKE this more sane in the face of unresolved IP addresses */
if (that->host_type != KH_IPHOSTNAME &&
(address_is_unset(&that->host_addr) || address_is_any(&that->host_addr))) {
/*
* Other side is wildcard: we must check if other conditions
* met.
*/
if (this->host_type != KH_IPHOSTNAME &&
(address_is_unset(&this->host_addr) || address_is_any(&this->host_addr))) {
llog(RC_ORIENT, logger,
"connection %s must specify host IP address for our side",
wm->name);
return FALSE;
}
}
if (this->protoport.ipproto == 0 && this->protoport.hport != 0) {
llog(RC_ORIENT, logger,
"connection %s cannot specify non-zero port %d for prototcol 0",
wm->name, this->protoport.hport);
return FALSE;
}
if (this->id != NULL && streq(this->id, "%fromcert")) {
lset_t auth_pol = (wm->policy & POLICY_ID_AUTH_MASK);
if (this->authby == AUTHBY_PSK || this->authby == AUTHBY_NULL ||
auth_pol == POLICY_PSK || auth_pol == POLICY_AUTH_NULL) {
llog(RC_FATAL, logger,
"ID cannot be specified as %%fromcert if PSK or AUTH-NULL is used");
return false;
}
}
return TRUE; /* happy */
}
diag_t add_end_cert_and_preload_private_key(CERTCertificate *cert,
struct end *dst_end,
bool preserve_ca,
struct logger *logger)
{
passert(cert != NULL);
dst_end->cert.ty = CERT_NONE;
dst_end->cert.u.nss_cert = NULL;
const char *nickname = cert->nickname;
/*
* A copy of this code lives in nss_cert_verify.c :/
* Currently only a check for RSA is needed, as the only ECDSA
* key size not allowed in FIPS mode (p192 curve), is not implemented
* by NSS.
* See also RSA_secret_sane() and ECDSA_secret_sane()
*/
if (libreswan_fipsmode()) {
SECKEYPublicKey *pk = CERT_ExtractPublicKey(cert);
passert(pk != NULL);
if (pk->keyType == rsaKey &&
((pk->u.rsa.modulus.len * BITS_PER_BYTE) < FIPS_MIN_RSA_KEY_SIZE)) {
SECKEY_DestroyPublicKey(pk);
return diag("FIPS: rejecting %s certificate '%s' with key size %d which is under %d",
dst_end->leftright, nickname,
pk->u.rsa.modulus.len * BITS_PER_BYTE,
FIPS_MIN_RSA_KEY_SIZE);
}
/* TODO FORCE MINIMUM SIZE ECDSA KEY */
SECKEY_DestroyPublicKey(pk);
}
/* XXX: should this be after validity check? */
select_nss_cert_id(cert, &dst_end->id);
/* check validity of cert */
if (CERT_CheckCertValidTimes(cert, PR_Now(), FALSE) !=
secCertTimeValid) {
return diag("%s certificate '%s' is expired or not yet valid",
dst_end->leftright, nickname);
}
dbg("loading %s certificate \'%s\' pubkey", dst_end->leftright, nickname);
if (!add_pubkey_from_nss_cert(&pluto_pubkeys, &dst_end->id, cert, logger)) {
/* XXX: push diag_t into add_pubkey_from_nss_cert()? */
return diag("%s certificate \'%s\' pubkey could not be loaded",
dst_end->leftright, nickname);
}
dst_end->cert.ty = CERT_X509_SIGNATURE;
dst_end->cert.u.nss_cert = cert;
/*
* If no CA is defined, use issuer as default; but only when
* update is ok.
*
*/
if (preserve_ca || dst_end->ca.ptr != NULL) {
dbg("preserving existing %s ca", dst_end->leftright);
} else {
dst_end->ca = clone_secitem_as_chunk(cert->derIssuer, "issuer ca");
}
/*
* Try to pre-load the certificate's secret (private key) into
* the local cache (see keys.c).
*
* This can fail. For instance, this end may only have the
* peer's certificate
*
* This could also fail because a needed secret is missing.
* That case is handled by refine_host_connection /
* get_psk.
*/
dbg("preload cert/secret for connection: %s", cert->nickname);
bool load_needed;
err_t ugh = preload_private_key_by_cert(&dst_end->cert, &load_needed, logger);
if (ugh != NULL) {
dbg("no private key matching %s certificate %s: %s",
dst_end->leftright, nickname, ugh);
} else if (load_needed) {
llog(RC_LOG|LOG_STREAM/*not-whack-for-now*/, logger,
"loaded private key matching %s certificate '%s'",
dst_end->leftright, nickname);
}
return NULL;
}
/* only used by add_connection() */
static void mark_parse(/*const*/ char *wmmark,
struct sa_mark *sa_mark,
struct logger *logger/*connection "...":*/)
{
/*const*/ char *val_end;
sa_mark->unique = FALSE;
sa_mark->val = 0xffffffff;
sa_mark->mask = 0xffffffff;
if (streq(wmmark, "-1") || startswith(wmmark, "-1/")) {
sa_mark->unique = TRUE;
val_end = wmmark + strlen("-1");
} else {
errno = 0;
unsigned long v = strtoul(wmmark, &val_end, 0);
if (errno != 0 || v > 0xffffffff ||
(*val_end != '\0' && *val_end != '/'))
{
/* ??? should be detected and reported by confread and whack */
/* XXX: don't trust whack */
llog(RC_LOG_SERIOUS, logger,
"bad mark value \"%s\"", wmmark);
} else {
sa_mark->val = v;
}
}
if (*val_end == '/') {
/*const*/ char *mask_end;
errno = 0;
unsigned long v = strtoul(val_end+1, &mask_end, 0);
if (errno != 0 || v > 0xffffffff || *mask_end != '\0') {
/* ??? should be detected and reported by confread and whack */
/* XXX: don't trust whack */
llog(RC_LOG_SERIOUS, logger,
"bad mark mask \"%s\"", mask_end);
} else {
sa_mark->mask = v;
}
}
if ((sa_mark->val & ~sa_mark->mask) != 0) {
/* ??? should be detected and reported by confread and whack */
/* XXX: don't trust whack */
llog(RC_LOG_SERIOUS, logger,
"mark value %#08" PRIx32 " has bits outside mask %#08" PRIx32,
sa_mark->val, sa_mark->mask);
}
}
/*
* Extract the connection detail from the whack message WM and store
* them in the connection C.
*
* This code is responsible for cloning strings and other structures
* so that they out live the whack message. When things go wrong,
* return false, the caller will then use delete_connection() to free
* the partially constructed connection.
*
* Checks from confread/whack should be moved here so it is similar
* for all methods of loading a connection.
*
* XXX: at one point this code was populating the connection with
* pointer's to the whack message's strings and then trying to use
* unshare_connection() to create local copies. Bad idea. For
* instance, it duplicated the proposal pointers yet here the pointer
* was freshy allocated so no duplication should be needed (or at
* least shouldn't be) (look for strange free() vs delref() sequence).
*/
static bool extract_connection(const struct whack_message *wm,
struct connection *c)
{
passert(c->name != NULL); /* see alloc_connection() */
if (conn_by_name(wm->name, false/*!strict*/) != NULL) {
llog(RC_DUPNAME, c->logger,
"attempt to redefine connection \"%s\"", wm->name);
return false;
}
if ((wm->policy & POLICY_COMPRESS) && !can_do_IPcomp) {
llog(RC_FATAL, c->logger,
"failed to add connection with compress because kernel is not configured to do IPCOMP");
return false;
}
if ((wm->policy & POLICY_TUNNEL) == LEMPTY) {
if (wm->sa_tfcpad != 0) {
llog(RC_FATAL, c->logger,
"failed to add connection: connection with type=transport cannot specify tfc=");
return false;
}
if (wm->vti_iface != NULL) {
llog(RC_FATAL, c->logger,
"failed to add connection: VTI requires tunnel mode but connection specifies type=transport");
return false;
}
}
if (LIN(POLICY_AUTHENTICATE, wm->policy)) {
if (wm->sa_tfcpad != 0) {
llog(RC_FATAL, c->logger,
"failed to add connection: connection with phase2=ah cannot specify tfc=");
return false;
}
}
if (LIN(POLICY_AUTH_NEVER, wm->policy)) {
if ((wm->policy & POLICY_SHUNT_MASK) == POLICY_SHUNT_TRAP) {
llog(RC_FATAL, c->logger,
"failed to add connection: connection with authby=never must specify shunt type via type=");
return false;
}
}
if ((wm->policy & POLICY_SHUNT_MASK) != POLICY_SHUNT_TRAP) {
if ((wm->policy & (POLICY_ID_AUTH_MASK & ~POLICY_AUTH_NEVER)) != LEMPTY) {
llog(RC_FATAL, c->logger,
"failed to add connection: shunt connection cannot have authentication method other then authby=never");
return false;
}
} else {
switch (wm->policy & (POLICY_AUTHENTICATE | POLICY_ENCRYPT)) {
case LEMPTY:
if (!LIN(POLICY_AUTH_NEVER, wm->policy)) {
llog(RC_FATAL, c->logger,
"failed to add connection: non-shunt connection must have AH or ESP");
return false;
}
break;
case POLICY_AUTHENTICATE | POLICY_ENCRYPT:
llog(RC_FATAL, c->logger,
"failed to add connection: non-shunt connection must not specify both AH and ESP");
return false;
}
}
if (wm->ike_version == IKEv1) {
#ifdef USE_IKEv1
if (pluto_ikev1_pol != GLOBAL_IKEv1_ACCEPT) {
llog(RC_FATAL, c->logger,
"failed to add IKEv1 connection: global ikev1-policy does not allow IKEv1 connections");
return false;
}
#else
llog(RC_FATAL, c->logger, "failed to add IKEv1 connection: IKEv1 support not compiled in");
return false;
#endif
}
c->ike_version = wm->ike_version;
if (wm->policy & POLICY_OPPORTUNISTIC &&
c->ike_version == IKEv1) {
llog(RC_FATAL, c->logger,
"failed to add connection: opportunistic connection MUST have IKEv2");
return false;
}
if (wm->policy & POLICY_MOBIKE &&
c->ike_version == IKEv1) {
llog(RC_FATAL, c->logger,
"failed to add connection: MOBIKE requires IKEv2");
return false;
}
if (wm->policy & POLICY_IKEV2_ALLOW_NARROWING &&
c->ike_version == IKEv1) {
llog(RC_FATAL, c->logger,
"failed to add connection: narrowing=yes requires IKEv2");
return false;
}
if (wm->iketcp != IKE_TCP_NO &&
c->ike_version != IKEv2) {
llog(RC_FATAL, c->logger,
"failed to add connection: enable-tcp= requires IKEv2");
return false;
}
if (wm->policy & POLICY_MOBIKE) {
if (kernel_ops->migrate_sa_check == NULL) {
llog(RC_FATAL, c->logger,
"failed to add connection: MOBIKE not supported by %s interface",
kernel_ops->kern_name);
return false;
}
/* probe the interface */
err_t err = kernel_ops->migrate_sa_check(c->logger);
if (err != NULL) {
llog(RC_FATAL, c->logger,
"failed to add connection: MOBIKE kernel support missing for %s interface: %s",
kernel_ops->kern_name, err);
return false;
}
}
if (wm->iketcp != IKE_TCP_NO && (wm->remote_tcpport == 0 || wm->remote_tcpport == 500)) {
llog(RC_FATAL, c->logger,
"failed to add connection: tcp-remoteport cannot be 0 or 500");
return false;
}
/* we could complain about a lot more whack strings */
if (NEVER_NEGOTIATE(wm->policy)) {
if (wm->ike != NULL) {
llog(RC_INFORMATIONAL, c->logger,
"ignored ike= option for type=passthrough connection");
}
if (wm->esp != NULL) {
llog(RC_INFORMATIONAL, c->logger,
"ignored esp= option for type=passthrough connection");
}
if (wm->iketcp != IKE_TCP_NO) {
llog(RC_INFORMATIONAL, c->logger,
"ignored enable-tcp= option for type=passthrough connection");
}
if (wm->left.authby != AUTHBY_UNSET || wm->right.authby != AUTHBY_UNSET) {
llog(RC_FATAL, c->logger,
"failed to add connection: leftauth= / rightauth= options are invalid for type=passthrough connection");
return false;
}
} else {
/* reject all bad combinations of authby with leftauth=/rightauth= */
if (wm->left.authby != AUTHBY_UNSET || wm->right.authby != AUTHBY_UNSET) {
if (c->ike_version == IKEv1) {
llog(RC_FATAL, c->logger,
"failed to add connection: leftauth= and rightauth= require ikev2");
return false;
}
if (wm->left.authby == AUTHBY_UNSET || wm->right.authby == AUTHBY_UNSET) {
llog(RC_FATAL, c->logger,
"failed to add connection: leftauth= and rightauth= must both be set or both be unset");
return false;
}
/* ensure no conflicts of set left/rightauth with (set or unset) authby= */
if (wm->left.authby == wm->right.authby) {
lset_t auth_pol = (wm->policy & POLICY_ID_AUTH_MASK);
const char *conflict = NULL;
switch (wm->left.authby) {
case AUTHBY_PSK:
if (auth_pol != POLICY_PSK && auth_pol != LEMPTY) {
conflict = "leftauthby=secret but authby= is not secret";
}
break;
case AUTHBY_NULL:
if (auth_pol != POLICY_AUTH_NULL && auth_pol != LEMPTY) {
conflict = "leftauthby=null but authby= is not null";
}
break;
case AUTHBY_NEVER:
if ((wm->policy & POLICY_ID_AUTH_MASK) != LEMPTY) {
conflict = "leftauthby=never but authby= is not never - double huh?";
}
break;
case AUTHBY_RSASIG:
case AUTHBY_ECDSA:
/* will be fixed later below */
break;
default:
bad_case(wm->left.authby);
}
if (conflict != NULL) {
policy_buf pb;
llog(RC_FATAL, c->logger,
"failed to add connection: leftauth=%s and rightauth=%s conflict with authby=%s, %s",
enum_name(&keyword_authby_names, wm->left.authby),
enum_name(&keyword_authby_names, wm->right.authby),
str_policy(wm->policy & POLICY_ID_AUTH_MASK, &pb),
conflict);
return false;
}
} else {
if ((wm->left.authby == AUTHBY_PSK && wm->right.authby == AUTHBY_NULL) ||
(wm->left.authby == AUTHBY_NULL && wm->right.authby == AUTHBY_PSK)) {
llog(RC_FATAL, c->logger,
"failed to add connection: cannot mix PSK and NULL authentication (leftauth=%s and rightauth=%s)",
enum_name(&keyword_authby_names, wm->left.authby),
enum_name(&keyword_authby_names, wm->right.authby));
return false;
}
}
}
}
if (protoport_has_any_port(&wm->right.protoport) &&
protoport_has_any_port(&wm->left.protoport)) {
llog(RC_FATAL, c->logger,
"failed to add connection: cannot have protoport with %%any on both sides");
return false;
}
if (!check_connection_end(&wm->right, &wm->left, wm, c->logger) ||
!check_connection_end(&wm->left, &wm->right, wm, c->logger)) {
/* XXX: shouldn't check_connection_end() log the error? */
llog(RC_FATAL, c->logger,
"failed to add connection: attempt to load incomplete connection");
return false;
}
if (subnet_type(&wm->left.client) != subnet_type(&wm->right.client)) {
llog(RC_FATAL, c->logger,
"failed to add connection: subnets must have the same address family");
return false;
}
/* duplicate any alias, adding spaces to the beginning and end */
c->connalias = clone_str(wm->connalias, "connection alias");
c->dnshostname = clone_str(wm->dnshostname, "connection dnshostname");
c->policy = wm->policy;
/* ignore IKEv2 ECDSA and legacy RSA policies for IKEv1 connections */
if (c->ike_version == IKEv1)
c->policy = (c->policy & ~(POLICY_ECDSA | POLICY_RSASIG_v1_5));
/* ignore symmetrical defaults if we got left/rightauth */
if (wm->left.authby != wm->right.authby)
c->policy = c->policy & ~POLICY_ID_AUTH_MASK;
/* remove default pubkey policy if left/rightauth is specified */
if (wm->left.authby == wm->right.authby) {
if (wm->left.authby == AUTHBY_RSASIG)
c->policy = (c->policy & ~(POLICY_ECDSA));
if (wm->left.authby == AUTHBY_ECDSA)
c->policy = (c->policy & ~(POLICY_RSASIG | POLICY_RSASIG_v1_5));
}
/* ignore supplied sighash and ECDSA (from defaults) for IKEv1 */
if (c->ike_version == IKEv2)
c->sighash_policy = wm->sighash_policy;
if (NEVER_NEGOTIATE(c->policy)) {
/* cleanup inherited default */
c->ike_version = 0;
c->iketcp = IKE_TCP_NO;
c->remote_tcpport = 0;
}
if (libreswan_fipsmode()) {
if (c->policy & POLICY_NEGO_PASS) {
c->policy &= ~POLICY_NEGO_PASS;
llog(RC_LOG_SERIOUS, c->logger,
"FIPS: ignored negotiationshunt=passthrough - packets MUST be blocked in FIPS mode");
}
if ((c->policy & POLICY_FAIL_MASK) == POLICY_FAIL_PASS) {
c->policy &= ~POLICY_FAIL_MASK;
c->policy |= POLICY_FAIL_NONE;
llog(RC_LOG_SERIOUS, c->logger,
"FIPS: ignored failureshunt=passthrough - packets MUST be blocked in FIPS mode");
}
}
policy_buf pb;
dbg("added new %s connection %s with policy %s%s",
enum_name(&ike_version_names, c->ike_version),
c->name, str_policy(c->policy, &pb),
NEVER_NEGOTIATE(c->policy) ? "+NEVER_NEGOTIATE" : "");
if (NEVER_NEGOTIATE(wm->policy)) {
/* set default to AUTHBY_NEVER if unset and we do not expect to do IKE */
if (wm->left.authby == AUTHBY_UNSET && wm->right.authby == AUTHBY_UNSET) {
if ((c->policy & POLICY_ID_AUTH_MASK) == LEMPTY) {
/* authby= was also not specified - fill in default */
c->policy |= POLICY_AUTH_NEVER;
policy_buf pb;
dbg("no AUTH policy was set for type=passthrough - defaulting to %s",
str_policy(c->policy & POLICY_ID_AUTH_MASK, &pb));
}
}
} else {
/* set default to RSASIG if unset and we expect to do IKE */
if (wm->left.authby == AUTHBY_UNSET && wm->right.authby == AUTHBY_UNSET) {
if ((c->policy & POLICY_ID_AUTH_MASK) == LEMPTY) {
/* authby= was also not specified - fill in default */
c->policy |= POLICY_DEFAULT;
policy_buf pb;
dbg("no AUTH policy was set - defaulting to %s",
str_policy(c->policy & POLICY_ID_AUTH_MASK, &pb));
}
}
/* fixup symmetric policy flags based on asymmetric ones */
if ((wm->left.authby == AUTHBY_NULL && wm->right.authby == AUTHBY_RSASIG) ||
(wm->left.authby == AUTHBY_RSASIG && wm->right.authby == AUTHBY_NULL)) {
c->policy |= POLICY_RSASIG;
}
if ((wm->left.authby == AUTHBY_NULL && wm->right.authby == AUTHBY_ECDSA) ||
(wm->left.authby == AUTHBY_ECDSA && wm->right.authby == AUTHBY_NULL)) {
c->policy |= POLICY_ECDSA;
}
/* IKE cipher suites */
if (!LIN(POLICY_AUTH_NEVER, wm->policy) &&
(wm->ike != NULL || c->ike_version == IKEv2)) {
const struct proposal_policy proposal_policy = {
/* logic needs to match pick_initiator() */
.version = c->ike_version,
.alg_is_ok = ike_alg_is_ike,
.pfs = LIN(POLICY_PFS, wm->policy),
.check_pfs_vs_dh = false,
.logger_rc_flags = ALL_STREAMS|RC_LOG,
.logger = c->logger, /* on-stack */
/* let defaults stumble on regardless */
.ignore_parser_errors = (wm->ike == NULL),
};
struct proposal_parser *parser = ike_proposal_parser(&proposal_policy);
c->ike_proposals.p = proposals_from_str(parser, wm->ike);
if (c->ike_proposals.p == NULL) {
pexpect(parser->diag != NULL); /* something */
log_diag(RC_FATAL, c->logger, &parser->diag,
"failed to add connection: ");
free_proposal_parser(&parser);
/* caller will free C */
return false;
}
free_proposal_parser(&parser);
/* from here on, error returns should alg_info_free(&c->ike_proposals->ai); */
LSWDBGP(DBG_BASE, buf) {
jam_string(buf, "ike (phase1) algorithm values: ");
jam_proposals(buf, c->ike_proposals.p);
}
}
/* ESP or AH cipher suites (but not both) */
if (wm->esp != NULL ||
(c->ike_version == IKEv2 &&
(c->policy & (POLICY_ENCRYPT|POLICY_AUTHENTICATE)))) {
const char *esp = wm->esp != NULL ? wm->esp : "";
dbg("from whack: got --esp=%s", esp);
const struct proposal_policy proposal_policy = {
/*
* logic needs to match pick_initiator()
*
* XXX: Once pluto is changed to IKEv1 XOR
* IKEv2 it should be possible to move this
* magic into pluto proper and instead pass a
* simple boolean.
*/
.version = c->ike_version,
.alg_is_ok = kernel_alg_is_ok,
.pfs = LIN(POLICY_PFS, wm->policy),
.check_pfs_vs_dh = true,
.logger_rc_flags = ALL_STREAMS|RC_LOG,
.logger = c->logger, /* on-stack */
/* let defaults stumble on regardless */
.ignore_parser_errors = (wm->esp == NULL),
};
/*
* We checked above that exactly one of
* POLICY_ENCRYPT and POLICY_AUTHENTICATE is on.
* The only difference in processing is which
* function is called (and those functions are
* almost identical).
*/
struct proposal_parser *(*fn)(const struct proposal_policy *policy) =
(c->policy & POLICY_ENCRYPT) ? esp_proposal_parser :
(c->policy & POLICY_AUTHENTICATE) ? ah_proposal_parser :
NULL;
passert(fn != NULL);
struct proposal_parser *parser = fn(&proposal_policy);
c->child_proposals.p = proposals_from_str(parser, wm->esp);
if (c->child_proposals.p == NULL) {
pexpect(parser->diag != NULL);
log_diag(RC_FATAL, c->logger, &parser->diag,
"failed to add connection: ");
free_proposal_parser(&parser);
/* caller will free C */
return false;
}
free_proposal_parser(&parser);
/* from here on, error returns should alg_info_free(&c->child_proposals->ai); */
LSWDBGP(DBG_BASE, buf) {
jam_string(buf, "ESP/AH string values: ");
jam_proposals(buf, c->child_proposals.p);
};
}
c->nic_offload = wm->nic_offload;
c->sa_ike_life_seconds = wm->sa_ike_life_seconds;
c->sa_ipsec_life_seconds = wm->sa_ipsec_life_seconds;
c->sa_rekey_margin = wm->sa_rekey_margin;
c->sa_rekey_fuzz = wm->sa_rekey_fuzz;
c->sa_keying_tries = wm->sa_keying_tries;
c->sa_replay_window = wm->sa_replay_window;
c->r_timeout = wm->r_timeout;
c->r_interval = wm->r_interval;
if (deltatime_cmp(c->sa_rekey_margin, >=, c->sa_ipsec_life_seconds)) {
deltatime_t new_rkm = deltatimescale(1, 2, c->sa_ipsec_life_seconds);
llog(RC_LOG, c->logger,
"rekeymargin (%jds) >= salifetime (%jds); reducing rekeymargin to %jds seconds",
deltasecs(c->sa_rekey_margin),
deltasecs(c->sa_ipsec_life_seconds),
deltasecs(new_rkm));
c->sa_rekey_margin = new_rkm;
}
{
/* http://csrc.nist.gov/publications/nistpubs/800-77/sp800-77.pdf */
time_t max_ike = libreswan_fipsmode() ? FIPS_IKE_SA_LIFETIME_MAXIMUM : IKE_SA_LIFETIME_MAXIMUM;
time_t max_ipsec = libreswan_fipsmode() ? FIPS_IPSEC_SA_LIFETIME_MAXIMUM : IPSEC_SA_LIFETIME_MAXIMUM;
if (deltasecs(c->sa_ike_life_seconds) > max_ike) {
llog(RC_LOG_SERIOUS, c->logger,
"IKE lifetime limited to the maximum allowed %jds",
(intmax_t) max_ike);
c->sa_ike_life_seconds = deltatime(max_ike);
}
if (deltasecs(c->sa_ipsec_life_seconds) > max_ipsec) {
llog(RC_LOG_SERIOUS, c->logger,
"IPsec lifetime limited to the maximum allowed %jds",
(intmax_t) max_ipsec);
c->sa_ipsec_life_seconds = deltatime(max_ipsec);
}
}
/* RFC 3706 DPD */
c->dpd_delay = wm->dpd_delay;
c->dpd_timeout = wm->dpd_timeout;
c->dpd_action = wm->dpd_action;
/* Cisco interop: remote peer type */
c->remotepeertype = wm->remotepeertype;
c->metric = wm->metric;
c->connmtu = wm->connmtu;
c->encaps = wm->encaps;
c->nat_keepalive = wm->nat_keepalive;
c->ikev1_natt = wm->ikev1_natt;
c->initial_contact = wm->initial_contact;
c->cisco_unity = wm->cisco_unity;
c->fake_strongswan = wm->fake_strongswan;
c->send_vendorid = wm->send_vendorid;
c->send_ca = wm->send_ca;
c->xauthby = wm->xauthby;
c->xauthfail = wm->xauthfail;
c->modecfg_dns = clone_str(wm->modecfg_dns, "connection modecfg_dns");
c->modecfg_domains = clone_str(wm->modecfg_domains, "connection modecfg_domains");
c->modecfg_banner = clone_str(wm->modecfg_banner, "connection modecfg_banner");
/* RFC 5685 - IKEv2 Redirect mechanism */
c->redirect_to = clone_str(wm->redirect_to, "connection redirect_to");
c->accept_redirect_to = clone_str(wm->accept_redirect_to, "connection accept_redirect_to");
/* RFC 8229 TCP encap*/
c->remote_tcpport = wm->remote_tcpport;
c->iketcp = wm->iketcp;
/*
* parse mark and mask values form the mark/mask string
* acceptable string formats are
* ( -1 | <nat> | <hex> ) [ / ( <nat> | <hex> ) ]
* examples:
* 10
* 10/0xffffffff
* 0xA/0xFFFFFFFF
*
* defaults:
* if mark is provided and mask is not mask will default to 0xFFFFFFFF
* if nothing is provided mark and mask are set to 0;
*/
/* mark-in= and mark-out= overwrite mark= */
if (wm->conn_mark_both != NULL) {
mark_parse(wm->conn_mark_both, &c->sa_marks.in, c->logger);
mark_parse(wm->conn_mark_both, &c->sa_marks.out, c->logger);
if (wm->conn_mark_in != NULL || wm->conn_mark_out != NULL) {
llog(RC_LOG_SERIOUS, c->logger,
"conflicting mark specifications");
}
}
if (wm->conn_mark_in != NULL)
mark_parse(wm->conn_mark_in, &c->sa_marks.in, c->logger);
if (wm->conn_mark_out != NULL)
mark_parse(wm->conn_mark_out, &c->sa_marks.out, c->logger);
c->vti_iface = clone_str(wm->vti_iface, "connection vti_iface");
c->vti_routing = wm->vti_routing;
c->vti_shared = wm->vti_shared;
#ifdef USE_XFRM_INTERFACE
if (wm->xfrm_if_id != UINT32_MAX) {
err_t err = xfrm_iface_supported(c->logger);
if (err == NULL) {
if (!setup_xfrm_interface(c, wm->xfrm_if_id == 0 ?
PLUTO_XFRMI_REMAP_IF_ID_ZERO : wm->xfrm_if_id ))
return false;
} else {
llog(RC_FATAL, c->logger,
"failed to add connection: ipsec-interface=%u not supported. %s",
wm->xfrm_if_id, err);
return false;
}
}
#endif
}
#ifdef HAVE_NM
c->nmconfigured = wm->nmconfigured;
#endif
c->nflog_group = wm->nflog_group;
c->sa_priority = wm->sa_priority;
c->sa_tfcpad = wm->sa_tfcpad;
c->send_no_esp_tfc = wm->send_no_esp_tfc;
c->sa_reqid = wm->sa_reqid;
/*
* Since at this point 'this' and 'that' are disoriented their
* names are pretty much meaningless. Hence the strange
* combination if 'this' and 'left' and 'that' and 'right.
*
* XXX: This is all too confusing - wouldn't it be simpler if
* there was a '.left' and '.right' (or even .end[2] - this
* code seems to be crying out for a for loop) and then having
* orient() set up .local and .remote pointers or indexes
* accordingly?
*/
int same_leftca = extract_end(&c->spd.this, &wm->left, "left", c->logger);
if (same_leftca < 0) {
return false;
}
int same_rightca = extract_end(&c->spd.that, &wm->right, "right", c->logger);
if (same_rightca < 0) {
return false;
}
if (same_rightca == 1) {
c->spd.that.ca = clone_hunk(c->spd.this.ca, "same rightca");
} else if (same_leftca == 1) {
c->spd.this.ca = clone_hunk(c->spd.that.ca, "same leftca");
}
/*
* How to add addresspool only for responder?
* It is not necessary on the initiator
*/
if ((range_type(&wm->left.pool_range) == &ipv4_info ||
range_type(&wm->left.pool_range) == &ipv6_info) &&
range_is_specified(&wm->left.pool_range)) {
/* there is address pool range add to the global list */
c->pool = install_addresspool(&wm->left.pool_range, c->logger);
c->spd.that.modecfg_server = TRUE;
c->spd.this.modecfg_client = TRUE;
}
if ((range_type(&wm->right.pool_range) == &ipv4_info ||
range_type(&wm->right.pool_range) == &ipv6_info) &&
range_is_specified(&wm->right.pool_range)) {
/* there is address pool range add to the global list */
c->pool = install_addresspool(&wm->right.pool_range, c->logger);
c->spd.that.modecfg_client = TRUE;
c->spd.this.modecfg_server = TRUE;
}
if (c->spd.this.xauth_server || c->spd.that.xauth_server)
c->policy |= POLICY_XAUTH;
update_ends_from_this_host_addr(&c->spd.this, &c->spd.that);
update_ends_from_this_host_addr(&c->spd.that, &c->spd.this);
/*
* If both left/rightauth is unset, fill it in with (preferred) symmetric policy
*/
if (wm->left.authby == AUTHBY_UNSET && wm->right.authby == AUTHBY_UNSET) {
if (c->policy & POLICY_RSASIG)
c->spd.this.authby = c->spd.that.authby = AUTHBY_RSASIG;
else if (c->policy & POLICY_ECDSA)
c->spd.this.authby = c->spd.that.authby = AUTHBY_ECDSA;
else if (c->policy & POLICY_PSK)
c->spd.this.authby = c->spd.that.authby = AUTHBY_PSK;
else if (c->policy & POLICY_AUTH_NULL)
c->spd.this.authby = c->spd.that.authby = AUTHBY_NULL;
}
/* if left/rightauth are set, but symmetric policy is not, fill it in */
if (wm->left.authby == wm->right.authby) {
switch (wm->left.authby) {
case AUTHBY_RSASIG:
c->policy |= POLICY_RSASIG;
break;
case AUTHBY_ECDSA:
c->policy |= POLICY_ECDSA;
break;
case AUTHBY_PSK:
c->policy |= POLICY_PSK;
break;
case AUTHBY_NULL:
c->policy |= POLICY_AUTH_NULL;
break;
default:
break;
}
}
/*
* force any wildcard host IP address, any wildcard subnet
* or any wildcard ID to _that_ end
*/
if (address_is_unset(&c->spd.this.host_addr) ||
address_is_any(&c->spd.this.host_addr) ||
c->spd.this.has_port_wildcard ||
c->spd.this.has_id_wildcards) {
struct end t = c->spd.this;
c->spd.this = c->spd.that;
c->spd.that = t;
}
c->spd.spd_next = NULL;
/*
* XXX: Install the connection. Yes right in the middle of
* the struct being constructed! Why? Because that's the way
* it's always been done.
*/
c->ac_next = connections;
connections = c;
/* set internal fields */
c->instance_serial = 0;
c->interface = NULL;
c->spd.routing = RT_UNROUTED;
c->newest_isakmp_sa = SOS_NOBODY;
c->newest_ipsec_sa = SOS_NOBODY;
c->spd.eroute_owner = SOS_NOBODY;
c->temp_vars.num_redirects = 0;
/*
* is spd.reqid necessary for all c? CK_INSTANCE or CK_PERMANENT
* need one. Does CK_TEMPLATE need one?
*/
c->spd.reqid = c->sa_reqid == 0 ? gen_reqid() : c->sa_reqid;
/* force all oppo connections to have a client */
if (c->policy & POLICY_OPPORTUNISTIC) {
c->spd.that.has_client = TRUE;
c->spd.that.client.maskbits = 0; /* ??? shouldn't this be 32 for v4? */
/*
* We cannot have unlimited keyingtries for Opportunistic, or else
* we gain infinite partial IKE SA's. But also, more than one makes
* no sense, since it will be installing a failureshunt (not
* negotiationshunt) on the 2nd keyingtry, and try to re-install another
* negotiation or failure shunt
*/
if (c->sa_keying_tries == 0) {
c->sa_keying_tries = 1;
llog(RC_LOG, c->logger,
"the connection is Opportunistic, but used keyingtries=0. The specified value was changed to 1");
}
}
if (c->policy & POLICY_GROUP) {
c->kind = CK_GROUP;
add_group(c);
} else if (((address_is_unset(&c->spd.that.host_addr) || address_is_any(&c->spd.that.host_addr)) &&
!NEVER_NEGOTIATE(c->policy)) ||
c->spd.that.has_port_wildcard ||
((c->policy & POLICY_SHUNT_MASK) == POLICY_SHUNT_TRAP &&
c->spd.that.has_id_wildcards )) {
dbg("based upon policy, the connection is a template.");
/*
* Opportunistic or Road Warrior or wildcard client
* subnet
* or wildcard ID
*/
c->kind = CK_TEMPLATE;
} else if (wm->left.virt != NULL || wm->right.virt != NULL) {
/*
* If we have a subnet=vnet: needing instantiation
* so we can accept multiple subnets from
* the remote peer.
*/
c->kind = CK_TEMPLATE;
} else if (c->policy & POLICY_IKEV2_ALLOW_NARROWING) {
dbg("based upon policy narrowing=yes, the connection is a template.");
c->kind = CK_TEMPLATE;
} else {
c->kind = CK_PERMANENT;
}
set_policy_prio(c); /* must be after kind is set */
c->extra_debugging = wm->debugging;
/* at most one virt can be present */
passert(wm->left.virt == NULL || wm->right.virt == NULL);
if (wm->left.virt != NULL || wm->right.virt != NULL) {
/*
* This now happens with wildcards on
* non-instantiations, such as rightsubnet=vnet:%priv
* or rightprotoport=17/%any
* passert(address_is_unset(&c->spd.that.host_addr) || address_is_any(&c->spd.that.host_addr));
*/
passert(c->spd.that.virt == NULL);
c->spd.that.virt = create_virtual(wm->left.virt != NULL ?
wm->left.virt :
wm->right.virt,
c->logger);
if (c->spd.that.virt != NULL)
c->spd.that.has_client = TRUE;
}
if (c->pool != NULL)
reference_addresspool(c);
(void)orient(c);
connect_to_host_pair(c);
/* non configurable */
c->ike_window = IKE_V2_OVERLAPPING_WINDOW_SIZE;
return true;
}
/* slightly different names compared to pluto_constants.c */
static const char *const policy_shunt_names[4] = {
"trap[should not happen]",
"passthrough",
"drop",
"reject",
};
void add_connection(struct fd *whackfd, const struct whack_message *wm)
{
struct connection *c = alloc_connection(wm->name, HERE);
/* XXX: something better? */
close_any(&c->logger->global_whackfd);
c->logger->global_whackfd = dup_any(whackfd);
if (!extract_connection(wm, c)) {
/* already logged */
struct logger *logger = clone_logger(c->logger, HERE);
discard_connection(c, false/*not-valid*/, logger);
free_logger(&logger, HERE);
return;
}
/* log all about this connection */
const char *what = (NEVER_NEGOTIATE(c->policy) ? policy_shunt_names[(c->policy & POLICY_SHUNT_MASK) >> POLICY_SHUNT_SHIFT] :
c->ike_version == IKEv1 ? "IKEv1" :
c->ike_version == IKEv2 ? "IKEv2" :
"IKEv?");
/* connection is good-to-go: log against it */
llog(RC_LOG, c->logger, "added %s connection", what);
policy_buf pb;
dbg("ike_life: %jd; ipsec_life: %jds; rekey_margin: %jds; rekey_fuzz: %lu%%; keyingtries: %lu; replay_window: %u; policy: %s%s",
deltasecs(c->sa_ike_life_seconds),
deltasecs(c->sa_ipsec_life_seconds),
deltasecs(c->sa_rekey_margin),
c->sa_rekey_fuzz,
c->sa_keying_tries,
c->sa_replay_window,
str_policy(c->policy, &pb),
NEVER_NEGOTIATE(c->policy) ? "+NEVER_NEGOTIATE" : "");
char topo[CONN_BUF_LEN];
dbg("%s", format_connection(topo, sizeof(topo), c, &c->spd));
/* XXX: something better? */
close_any(&c->logger->global_whackfd);
}
/*
* Derive a template connection from a group connection and target.
* Similar to instantiate(). Happens at whack --listen.
* Returns name of new connection. NULL on failure (duplicated name).
* Caller is responsible for pfreeing name.
*/
struct connection *add_group_instance(struct connection *group, const ip_subnet *target,
uint8_t proto , uint16_t sport , uint16_t dport)
{
passert(group->kind == CK_GROUP);
passert(oriented(*group));
/*
* Manufacture a unique name for this template.
*/
char *namebuf; /* must free */
subnet_buf targetbuf;
str_subnet(target, &targetbuf);
if (proto == 0) {
namebuf = alloc_printf("%s#%s", group->name, targetbuf.buf);
} else {
namebuf = alloc_printf("%s#%s-(%d--%d--%d)", group->name,
targetbuf.buf, sport, proto, dport);
}
if (conn_by_name(namebuf, false/*!strict*/) != NULL) {
llog(RC_DUPNAME, group->logger,
"group name + target yields duplicate name \"%s\"", namebuf);
pfreeany(namebuf);
return NULL;
}
struct connection *t = clone_connection(namebuf, group, HERE);
passert(namebuf != t->name); /* see clone_connection() */
pfreeany(namebuf);
t->foodgroup = t->name; /* XXX: DANGER: unshare_connection() will clone this */
/* suppress virt before unsharing */
passert(t->spd.this.virt == NULL);
pexpect(t->spd.spd_next == NULL); /* we only handle top spd */
if (t->spd.that.virt != NULL) {
DBG_log("virtual_ip not supported in group instance; ignored");
virtual_ip_delref(&t->spd.that.virt, HERE);
}
unshare_connection(t);
passert(t->foodgroup != t->name); /* XXX: see DANGER above */
t->spd.that.client = *target;
if (proto != 0) {
/* if foodgroup entry specifies protoport, override protoport= settings */
t->spd.this.protocol = proto;
t->spd.that.protocol = proto;
t->spd.this.port = sport;
t->spd.that.port = dport;
}
t->policy &= ~(POLICY_GROUP | POLICY_GROUTED);
t->policy |= POLICY_GROUPINSTANCE; /* mark as group instance for later */
t->kind = (address_is_unset(&t->spd.that.host_addr) || address_is_any(&t->spd.that.host_addr)) &&
!NEVER_NEGOTIATE(t->policy) ?
CK_TEMPLATE : CK_INSTANCE;
/* reset log file info */
t->log_file_name = NULL;
t->log_file = NULL;
t->log_file_err = FALSE;
t->spd.reqid = group->sa_reqid == 0 ?
gen_reqid() : group->sa_reqid;
/* add to connections list */
t->ac_next = connections;
connections = t;
/* same host_pair as parent: stick after parent on list */
/* t->hp_next = group->hp_next; */ /* done by clone_thing */
group->hp_next = t;
/* route if group is routed */
if (group->policy & POLICY_GROUTED) {
/* XXX: something better? */
close_any(&t->logger->global_whackfd);
t->logger->global_whackfd = dup_any(group->logger->global_whackfd);
if (!trap_connection(t)) {
llog(WHACK_STREAM|RC_ROUTE, group->logger,
"could not route");
}
/* XXX: something better? */
close_any(&t->logger->global_whackfd);
}
return t;
}
/* An old target has disappeared for a group: delete instance. */
void remove_group_instance(const struct connection *group,
const char *name)
{
struct fd *whackfd = whack_log_fd; /* placeholder */
passert(group->kind == CK_GROUP);
delete_connections_by_name(name, false, whackfd);
}
/*
* Common part of instantiating a Road Warrior or Opportunistic connection.
* peers_id can be used to carry over an ID discovered in Phase 1.
* It must not disagree with the one in c, but if that is unspecified,
* the new connection will use peers_id.
* If peers_id is NULL, and c.that.id is uninstantiated (ID_NONE), the
* new connection will continue to have an uninstantiated that.id.
* Note: instantiation does not affect port numbers.
*
* Note that instantiate can only deal with a single SPD/eroute.
*/
struct connection *instantiate(struct connection *c,
const ip_address *peer_addr,
const struct id *peer_id)
{
passert(c->kind == CK_TEMPLATE);
passert(c->spd.spd_next == NULL);
c->instance_serial++;
struct connection *d = clone_connection(c->name, c, HERE);
passert(c->name != d->name); /* see clone_connection() */
if (peer_id != NULL) {
int wildcards; /* value ignored */
passert(d->spd.that.id.kind == ID_FROMCERT || match_id(peer_id, &d->spd.that.id, &wildcards));
d->spd.that.id = *peer_id;
d->spd.that.has_id_wildcards = FALSE;
}
unshare_connection(d);
d->kind = CK_INSTANCE;
passert(oriented(*d));
if (peer_addr != NULL) {
d->spd.that.host_addr = *peer_addr;
}
update_ends_from_this_host_addr(&d->spd.that, &d->spd.this);
/*
* We cannot guess what our next_hop should be, but if it was
* explicitly specified as 0.0.0.0, we set it to be peer.
* (whack will not allow nexthop to be elided in RW case.)
*/
update_ends_from_this_host_addr(&d->spd.this, &d->spd.that);
d->spd.spd_next = NULL;
d->spd.reqid = c->sa_reqid == 0 ? gen_reqid() : c->sa_reqid;
/* since both ends updated; presumably already oriented? */
set_policy_prio(d);
/* set internal fields */
d->ac_next = connections;
connections = d;
d->spd.routing = RT_UNROUTED;
d->newest_isakmp_sa = SOS_NOBODY;
d->newest_ipsec_sa = SOS_NOBODY;
d->spd.eroute_owner = SOS_NOBODY;
/* reset log file info */
d->log_file_name = NULL;
d->log_file = NULL;
d->log_file_err = FALSE;
if (c->sa_marks.in.unique) {
d->sa_marks.in.val = global_marks;
d->sa_marks.out.val = global_marks;
global_marks++;
if (global_marks == UINT_MAX - 1) {
/* we hope 2^32 connections ago are no longer around */
global_marks = MINIMUM_IPSEC_SA_RANDOM_MARK;
}
}
/* assumption: orientation is the same as c's */
connect_to_host_pair(d);
return d;
}
struct connection *rw_instantiate(struct connection *c,
const ip_address *peer_addr,
const ip_subnet *peer_subnet,
const struct id *peer_id)
{
struct connection *d = instantiate(c, peer_addr, peer_id);
if (peer_subnet != NULL && is_virtual_connection(c)) {
d->spd.that.client = *peer_subnet;
if (subnetishost(peer_subnet) && addrinsubnet(peer_addr, peer_subnet))
d->spd.that.has_client = FALSE;
}
if (d->policy & POLICY_OPPORTUNISTIC) {
/*
* This must be before we know the client addresses.
* Fill in one that is impossible. This prevents anyone else
* from trying to use this connection to get to a particular
* client
*/
d->spd.that.client = subnet_type(&d->spd.that.client)->no_addresses;
}
connection_buf inst;
address_buf b;
dbg("rw_instantiate() instantiated "PRI_CONNECTION" for %s",
pri_connection(d, &inst),
str_address(peer_addr, &b));
return d;
}
/* priority formatting */
size_t jam_policy_prio(struct jambuf *buf, policy_prio_t pp)
{
if (pp == BOTTOM_PRIO) {
return jam_string(buf, "0");
}
return jam(buf, "%" PRIu32 ",%" PRIu32,
pp >> 17, (pp & ~(~(policy_prio_t)0 << 17)) >> 8);
}
const char *str_policy_prio(policy_prio_t pp, policy_prio_buf *buf)
{
struct jambuf jb = ARRAY_AS_JAMBUF(buf->buf);
jam_policy_prio(&jb, pp);
return buf->buf;
}
void set_policy_prio(struct connection *c)
{
c->policy_prio = (((policy_prio_t)c->spd.this.client.maskbits << 17) |
((policy_prio_t)c->spd.that.client.maskbits << 8) |
((policy_prio_t)1));
}
/*
* Format any information needed to identify an instance of a connection.
* Fills any needed information into buf which MUST be big enough.
* Road Warrior: peer's IP address
* Opportunistic: [" " myclient "==="] " ..." peer ["===" peer_client] '\0'
*/
static size_t jam_connection_client(struct jambuf *b,
const char *prefix, const char *suffix,
const ip_subnet *client, const ip_address *gw)
{
size_t s = 0;
if (subnetisaddr(client, gw)) {
/* compact denotation for "self" */
} else {
s += jam_string(b, prefix);
if (subnet_contains_no_addresses(client)) {
s += jam_string(b, "?"); /* unknown */
} else {
s += jam_subnet(b, client);
}
s += jam_string(b, suffix);
}
return s;
}
size_t jam_connection_instance(struct jambuf *buf, const struct connection *c)
{
if (!pexpect(c->kind == CK_INSTANCE ||
c->kind == CK_GOING_AWAY)) {
return 0;
}
size_t s = 0;
if (c->instance_serial != 0) {
s += jam(buf, "[%lu]", c->instance_serial);
}
if (c->policy & POLICY_OPPORTUNISTIC) {
s += jam_connection_client(buf, " ", "===",
&c->spd.this.client,
&c->spd.this.host_addr);
s += jam_string(buf, " ...");
s += jam_address(buf, &c->spd.that.host_addr);
s += jam_connection_client(buf, "===", "",
&c->spd.that.client,
&c->spd.that.host_addr);
} else {
s += jam_string(buf, " ");
s += jam_address_sensitive(buf, &c->spd.that.host_addr);
}
return s;
}
size_t jam_connection(struct jambuf *buf, const struct connection *c)
{
size_t s = 0;
s += jam(buf, "\"%s\"", c->name);
if (c->kind == CK_INSTANCE || c->kind == CK_GOING_AWAY) {
s += jam_connection_instance(buf, c);
}
return s;
}
const char *str_connection_instance(const struct connection *c, connection_buf *buf)
{
struct jambuf p = ARRAY_AS_JAMBUF(buf->buf);
if (c->kind == CK_INSTANCE) {
jam_connection_instance(&p, c);
}
return buf->buf;
}
/*
* Find an existing connection for a trapped outbound packet.
*
* This is attempted before we bother with gateway discovery.
* + this connection is routed or instance_of_routed_template
* (i.e. approved for on-demand)
* + this subnet contains our_client (or we are our_client)
* + that subnet contains peer_client (or peer is peer_client)
* + don't care about Phase 1 IDs (we don't know)
* Note: result may still need to be instantiated.
* The winner has the highest policy priority.
*
* If there are several with that priority, we give preference to
* the first one that is an instance.
*
* See also build_outgoing_opportunistic_connection.
*/
struct connection *find_connection_for_clients(struct spd_route **srp,
const ip_endpoint *local_client,
const ip_endpoint *remote_client,
chunk_t sec_label,
struct logger *logger UNUSED)
{
passert(endpoint_is_specified(local_client));
passert(endpoint_is_specified(remote_client));
passert(endpoint_protocol(local_client) == endpoint_protocol(remote_client));
int local_port = endpoint_hport(local_client);
int remote_port = endpoint_hport(remote_client);
struct connection *best = NULL;
policy_prio_t best_prio = BOTTOM_PRIO;
struct spd_route *best_sr = NULL;
endpoints_buf eb;
dbg("find_connection: looking for policy for connection: %s",
str_endpoints(local_client, remote_client, &eb));
struct connection *c;
dbg("FOR_EACH_CONNECTION_... in %s", __func__);
for (c = connections; c != NULL; c = c->ac_next) {
if (c->kind == CK_GROUP)
continue;
struct spd_route *sr;
for (sr = &c->spd; best != c && sr; sr = sr->spd_next) {
if ( ((routed(sr->routing) || c->instance_initiation_ok) || sec_label.ptr != NULL) &&
endpoint_in_selector(local_client, &sr->this.client) &&
endpoint_in_selector(remote_client, &sr->that.client)
#ifdef HAVE_LABELED_IPSEC
&& ((sec_label.ptr == NULL &&
sr->this.sec_label.ptr == NULL) ||
/* don't call with NULL, it confuses it */
within_range((const char *)sec_label.ptr,
(const char *)sr->this.sec_label.ptr, logger))
#endif
) {
unsigned ipproto = endpoint_protocol(local_client)->ipproto;
policy_prio_t prio =
(8 * (c->policy_prio + (c->kind == CK_INSTANCE)) +
2 * (sr->this.port == local_port) +
2 * (sr->that.port == remote_port) +
1 * (sr->this.protocol == ipproto));
if (DBGP(DBG_BASE)) {
connection_buf cib;
selectors_buf sb;
DBG_log("find_connection: conn "PRI_CONNECTION" has compatible peers: %s [pri: %" PRIu32 "]",
pri_connection(c, &cib),
str_selectors(&c->spd.this.client, &c->spd.that.client, &sb),
prio);
if (best == NULL) {
connection_buf cib2;
DBG_log("find_connection: first OK "PRI_CONNECTION" [pri:%" PRIu32 "]{%p} (child %s)",
pri_connection(c, &cib2),
prio, c,
c->policy_next ?
c->policy_next->name :
"none");
} else {
connection_buf cib;
connection_buf cib2;
DBG_log("find_connection: comparing best "PRI_CONNECTION" [pri:%" PRIu32 "]{%p} (child %s) to "PRI_CONNECTION" [pri:%" PRIu32 "]{%p} (child %s)",
pri_connection(best, &cib),
best_prio,
best,
best->policy_next ?
best->policy_next->name :
"none",
pri_connection(c, &cib2),
prio, c,
c->policy_next ?
c->policy_next->name :
"none");
}
}
if (best == NULL || prio > best_prio) {
best = c;
best_sr = sr;
best_prio = prio;
}
}
}
}
if (best != NULL && NEVER_NEGOTIATE(best->policy))
best = NULL;
if (srp != NULL && best != NULL)
*srp = best_sr;
if (DBGP(DBG_BASE)) {
if (best != NULL) {
connection_buf cib;
DBG_log("find_connection: concluding with "PRI_CONNECTION" [pri:%" PRIu32 "]{%p} kind=%s",
pri_connection(best, &cib),
best_prio, best,
enum_name(&connection_kind_names, best->kind));
} else {
DBG_log("find_connection: concluding with empty");
}
}
return best;
}
struct connection *oppo_instantiate(struct connection *c,
const struct id *remote_id,
/* both host and client */
const ip_address *local_address,
const ip_address *remote_address)
{
address_buf lb, rb;
dbg("oppo instantiating c=\"%s\" with c->routing %s between %s -> %s",
c->name, enum_name(&routing_story, c->spd.routing),
str_address(local_address, &lb), str_address(remote_address, &rb));
struct connection *d = instantiate(c, remote_address, remote_id);
passert(d->spd.spd_next == NULL);
/* fill in our client side */
if (d->spd.this.has_client) {
/*
* There was a client in the abstract connection so we
* demand that either ...
*/
/* opportunistic connections do not use port selectors */
if (address_in_selector(local_address, &d->spd.this.client)) {
/*
* the required client is within that subnet
* narrow it(?), ...
*/
d->spd.this.client = selector_from_address(local_address);
} else if (address_eq(local_address, &d->spd.this.host_addr)) {
/*
* or that it is our private ip in case we are
* behind a port forward.
*/
update_selector_hport(&d->spd.this.client, 0);
} else {
passert_fail(c->logger, HERE,
"local address does not match the host or client");
}
} else {
/*
* There was no client in the abstract connection so
* we demand that the required client be the host.
*/
passert(address_eq(local_address, &d->spd.this.host_addr));
}
/*
* Fill in peer's client side.
* If the client is the peer, excise the client from the connection.
*/
passert(d->policy & POLICY_OPPORTUNISTIC);
passert(address_in_selector(remote_address, &d->spd.that.client));
d->spd.that.client = selector_from_address(remote_address);
if (address_eq(remote_address, &d->spd.that.host_addr))
d->spd.that.has_client = false;
/*
* Adjust routing if something is eclipsing c.
* It must be a %hold for us (hard to passert this).
* If there was another instance eclipsing, we'd be using it.
*/
if (c->spd.routing == RT_ROUTED_ECLIPSED)
d->spd.routing = RT_ROUTED_PROSPECTIVE;
/*
* Remember if the template is routed:
* if so, this instance applies for initiation
* even if it is created for responding.
*/
if (routed(c->spd.routing))
d->instance_initiation_ok = true;
if (DBGP(DBG_BASE)) {
char topo[CONN_BUF_LEN];
connection_buf inst;
DBG_log("oppo_instantiate() instantiated "PRI_CONNECTION" with routing %s: %s",
pri_connection(d, &inst),
enum_name(&routing_story, d->spd.routing),
format_connection(topo, sizeof(topo), d, &d->spd));
}
return d;
}
/*
* Outgoing opportunistic connection.
*
* Find and instantiate a connection for an outgoing Opportunistic connection.
* We've already discovered its gateway.
* We look for a connection such that:
* + this is one of our interfaces
* + this subnet contains our_client (or we are our_client)
* (we will specialize the client). We prefer the smallest such subnet.
* + that subnet contains peer_clent (we will specialize the client).
* We prefer the smallest such subnet.
* + is opportunistic
* + that peer is NO_IP
* + don't care about Phase 1 IDs (probably should be default)
* We could look for a connection that already had the desired peer
* (rather than NO_IP) specified, but it doesn't seem worth the
* bother.
*
* We look for the routed policy applying to the narrowest subnets.
* We only succeed if we find such a policy AND it is satisfactory.
*
* The body of the inner loop is a lot like that in
* find_connection_for_clients. In this case, we know the gateways
* that we need to instantiate an opportunistic connection.
*/
struct connection *build_outgoing_opportunistic_connection(const ip_endpoint *local_client,
const ip_endpoint *remote_client)
{
/*
* Did the caller do their job?
*
* Where the protocol includes a port, the endpoint ports
* don't need to match but they do need to be defined.
*
* Unfortunately rcv_whack.c calls this function with a very
* flimsy looking local/remote endpoints - both the protocol
* and the port are missing. Hence some of the fuzzy checks
* below.
*/
pendpoint(local_client);
pendpoint(remote_client);
pexpect(endpoint_protocol(local_client) == endpoint_protocol(remote_client));
/*
* Go through all the "half" oriented connections (remote
* address is unset) looking for client that matches the
* local/remote endpoint.
*
* Unfortunately there's no good data structure for doing
* this, so ...
*
* Big hack: get the list of local addresses by iterating over
* the interface endpoints, and then feed the endpoint's
* address into FOR_EACH_HOST_PAIR_CONNECTION(LOCAL,UNSET).
*/
struct connection *best = NULL;
struct spd_route *best_spd_route = NULL;
struct iface_dev *last_iface_device = NULL;
for (struct iface_endpoint *p = interfaces; p != NULL; p = p->next) {
/*
* Bigger hack: assume the interface endpoints
* (ADDRESS:500 ADDRESS:4500) for a device are grouped
* (mostly true, TCP, custom port?) and only search
* when a new interface device is found.
*/
if (p->ip_dev == last_iface_device) {
continue;
}
last_iface_device = p->ip_dev;
/*
* Go through those connections with our address and
* NO_IP as hosts.
*
* We cannot know what port the peer would use, so we
* assume that it is pluto_port (makes debugging
* easier).
*
* XXX: the port doesn't matter!
*/
FOR_EACH_HOST_PAIR_CONNECTION(&p->ip_dev->id_address, &unset_address, c) {
dbg("checking %s", c->name);
if (c->kind == CK_GROUP)
continue;
/*
* for each sr of c, see if we have a new best
*
* Paul: while this code can reject unmatched
* conns, it does not find the most narrow
* match!
*/
for (struct spd_route *sr = &c->spd; sr != NULL; sr = sr->spd_next) {
if (!routed(sr->routing)) {
continue;
}
if (!endpoint_in_selector(local_client, &sr->this.client)) {
continue;
}
if (!endpoint_in_selector(remote_client, &sr->that.client)) {
continue;
}
/*
* First or better solution.
*
* The test for better is:
* sr's .this is narrower, or
* sr's .this is same and sr's .that is narrower.
* ??? not elegant, not symmetric.
* Possible replacement test:
* best_spd_route->this.client.maskbits + best_spd_route->that.client.maskbits >
* sr->this.client.maskbits + sr->that.client.maskbits
* but this knows too much about the representation of ip_subnet.
* What is the correct semantics?
*
* XXX: selector_in_selector() is
* exclusive - it excludes
* selector_eq().
*/
if (best_spd_route != NULL &&
selector_in_selector(&best_spd_route->this.client, &sr->this.client)) {
/*
* BEST_SPD_ROUTE is better.
*
* BEST_SPD_ROUTE's .this is
* narrower than .SR's.
*/
continue;
}
if (best_spd_route != NULL &&
selector_eq(&best_spd_route->this.client, &sr->this.client) &&
selector_in_selector(&best_spd_route->that.client, &sr->that.client)) {
/*
* BEST_SPD_ROUTE is better.
*
* Since BEST_SPD_ROUTE's
* .this matches SR's,
* tie-break with
* BEST_SPD_ROUTE's .that
* being narrower than .SR's.
*/
continue;
}
best = c;
best_spd_route = sr;
}
}
}
if (best == NULL ||
NEVER_NEGOTIATE(best->policy) ||
(best->policy & POLICY_OPPORTUNISTIC) == LEMPTY ||
best->kind != CK_TEMPLATE) {
return NULL;
}
/* XXX we might not yet know the ID! */
ip_address local_address = endpoint_address(local_client);
ip_address remote_address = endpoint_address(remote_client);
return oppo_instantiate(best, NULL, &local_address, &remote_address);
}
/*
* Find the connection to connection c's peer's client with the
* largest value of .routing. All other things being equal,
* preference is given to c. If none is routed, return NULL.
*
* If erop is non-null, set *erop to a connection sharing both
* our client subnet and peer's client subnet with the largest value
* of .routing. If none is erouted, set *erop to NULL.
*
* The return value is used to find other connections sharing a route.
* *erop is used to find other connections sharing an eroute.
*/
struct connection *route_owner(struct connection *c,
const struct spd_route *cur_spd,
struct spd_route **srp,
struct connection **erop,
struct spd_route **esrp)
{
if (!oriented(*c)) {
llog(RC_LOG, c->logger,
"route_owner: connection no longer oriented - system interface change?");
return NULL;
}
struct connection
*best_ro = c,
*best_ero = c;
struct spd_route *best_sr = NULL,
*best_esr = NULL;
enum routing_t best_routing = cur_spd->routing,
best_erouting = best_routing;
dbg("FOR_EACH_CONNECTION_... in %s", __func__);
for (struct connection *d = connections; d != NULL; d = d->ac_next) {
if (!oriented(*d))
continue;
/*
* consider policies different if the either in or out marks
* differ (after masking)
*/
if (DBGP(DBG_BASE)) {
DBG_log(" conn %s mark %" PRIu32 "/%#08" PRIx32 ", %" PRIu32 "/%#08" PRIx32 " vs",
c->name, c->sa_marks.in.val, c->sa_marks.in.mask,
c->sa_marks.out.val, c->sa_marks.out.mask);
DBG_log(" conn %s mark %" PRIu32 "/%#08" PRIx32 ", %" PRIu32 "/%#08" PRIx32,
d->name, d->sa_marks.in.val, d->sa_marks.in.mask,
d->sa_marks.out.val, d->sa_marks.out.mask);
}
if ( (c->sa_marks.in.val & c->sa_marks.in.mask) != (d->sa_marks.in.val & d->sa_marks.in.mask) ||
(c->sa_marks.out.val & c->sa_marks.out.mask) != (d->sa_marks.out.val & d->sa_marks.out.mask) )
continue;
struct spd_route *srd;
for (srd = &d->spd; srd != NULL; srd = srd->spd_next) {
if (srd->routing == RT_UNROUTED)
continue;
const struct spd_route *src;
for (src = &c->spd; src != NULL; src = src->spd_next) {
if (src == srd)
continue;
if (!samesubnet(&src->that.client,
&srd->that.client) ||
src->that.protocol != srd->that.protocol ||
src->that.port != srd->that.port ||
!sameaddr(&src->this.host_addr,
&srd->this.host_addr))
continue;
if (srd->routing > best_routing) {
best_ro = d;
best_sr = srd;
best_routing = srd->routing;
}
if (samesubnet(&src->this.client,
&srd->this.client) &&
src->this.protocol == srd->this.protocol &&
src->this.port == srd->this.port &&
srd->routing > best_erouting)
{
best_ero = d;
best_esr = srd;
best_erouting = srd->routing;
}
}
}
}
LSWDBGP(DBG_BASE, buf) {
connection_buf cib;
jam(buf, "route owner of \"%s\"%s %s: ",
pri_connection(c, &cib),
enum_name(&routing_story, cur_spd->routing));
if (!routed(best_routing)) {
jam(buf, "NULL");
} else if (best_ro == c) {
jam(buf, "self");
} else {
connection_buf cib;
jam(buf, ""PRI_CONNECTION" %s",
pri_connection(best_ro, &cib),
enum_name(&routing_story, best_routing));
}
if (erop != NULL) {
jam(buf, "; eroute owner: ");
if (!erouted(best_ero->spd.routing)) {
jam(buf, "NULL");
} else if (best_ero == c) {
jam(buf, "self");
} else {
connection_buf cib;
jam(buf, ""PRI_CONNECTION" %s",
pri_connection(best_ero, &cib),
enum_name(&routing_story, best_ero->spd.routing));
}
}
}
if (erop != NULL)
*erop = erouted(best_erouting) ? best_ero : NULL;
if (srp != NULL ) {
*srp = best_sr;
if (esrp != NULL )
*esrp = best_esr;
}
return routed(best_routing) ? best_ro : NULL;
}
/*
* Extracts the peer's ca from the chained list of public keys.
*/
static chunk_t get_peer_ca(struct pubkey_list *const *pubkey_db,
const struct id *peer_id)
{
struct pubkey_list *p;
for (p = *pubkey_db; p != NULL; p = p->next) {
struct pubkey *key = p->key;
if (key->type == &pubkey_type_rsa && same_id(peer_id, &key->id))
return key->issuer;
}
return EMPTY_CHUNK;
}
/*
* ??? NOTE: THESE IMPORTANT COMMENTS DO NOT REFLECT ANY CHANGES MADE AFTER FreeS/WAN.
*
* Given an up-until-now satisfactory connection, find the best connection
* now that we just got the Phase 1 Id Payload from the peer.
*
* Comments in the code describe the (tricky!) matching criteria.
* Although this routine could handle the initiator case,
* it isn't currently called in this case.
* If it were, it could "upgrade" an Opportunistic Connection
* to a Road Warrior Connection if a suitable Peer ID were found.
*
* In RFC 2409 "The Internet Key Exchange (IKE)",
* in 5.1 "IKE Phase 1 Authenticated With Signatures", describing Main
* Mode:
*
* Initiator Responder
* ----------- -----------
* HDR, SA -->
* <-- HDR, SA
* HDR, KE, Ni -->
* <-- HDR, KE, Nr
* HDR*, IDii, [ CERT, ] SIG_I -->
* <-- HDR*, IDir, [ CERT, ] SIG_R
*
* In 5.4 "Phase 1 Authenticated With a Pre-Shared Key":
*
* HDR, SA -->
* <-- HDR, SA
* HDR, KE, Ni -->
* <-- HDR, KE, Nr
* HDR*, IDii, HASH_I -->
* <-- HDR*, IDir, HASH_R
*
* refine_host_connection could be called in two case:
*
* - the Responder receives the IDii payload:
* + [PSK] after using PSK to decode this message
* + before sending its IDir payload
* + before using its ID in HASH_R computation
* + [DSig] before using its private key to sign SIG_R
* + before using the Initiator's ID in HASH_I calculation
* + [DSig] before using the Initiator's public key to check SIG_I
*
* - the Initiator receives the IDir payload:
* + [PSK] after using PSK to encode previous message and decode this message
* + after sending its IDii payload
* + after using its ID in HASH_I computation
* + [DSig] after using its private key to sign SIG_I
* + before using the Responder's ID to compute HASH_R
* + [DSig] before using Responder's public key to check SIG_R
*
* refine_host_connection can choose a different connection, as long as
* nothing already used is changed.
*
* In the Initiator case, the particular connection might have been
* specified by whatever provoked Pluto to initiate. For example:
* whack --initiate connection-name
* The advantages of switching connections when we're the Initiator seem
* less important than the disadvantages, so after FreeS/WAN 1.9, we
* don't do this.
*/
struct connection *refine_host_connection(const struct state *st,
const struct id *peer_id,
const struct id *tarzan_id,
bool initiator,
lset_t auth_policy /* used by ikev1 */,
enum keyword_authby this_authby /* used by ikev2 */,
bool *fromcert)
{
struct connection *c = st->st_connection;
const generalName_t *requested_ca = st->st_requested_ca;
/* Ensure the caller and we know the IKE version we are looking for */
bool ikev1 = auth_policy != LEMPTY;
bool ikev2 = this_authby != AUTHBY_UNSET;
*fromcert = FALSE;
passert(ikev1 != ikev2 && ikev2 == (st->st_ike_version == IKEv2));
passert(this_authby != AUTHBY_NEVER);
/*
* Translate the IKEv1 policy onto an IKEv2 policy.
* Saves duplicating the checks for v1 and v2, and the
* v1 policy is a subset of the v2 policy. Use the ikev2
* bool for IKEv2-only feature checks.
*/
if (ikev1) {
/* ??? are these cases mutually exclusive? */
if (LIN(POLICY_RSASIG, auth_policy))
this_authby = AUTHBY_RSASIG;
if (LIN(POLICY_PSK, auth_policy))
this_authby = AUTHBY_PSK;
passert(this_authby != AUTHBY_UNSET);
}
/* from here on, auth_policy must only be used to check POLICY_AGGRESSIVE */
connection_buf cib;
dbg("refine_host_connection for %s: starting with "PRI_CONNECTION"",
enum_name(&ike_version_names, st->st_ike_version),
pri_connection(c, &cib));
/*
* Find the PEER's CA, check the per-state DB first.
*/
pexpect(st->st_remote_certs.processed);
chunk_t peer_ca = get_peer_ca(&st->st_remote_certs.pubkey_db, peer_id);
if (hunk_isempty(peer_ca)) {
peer_ca = get_peer_ca(&pluto_pubkeys, peer_id);
}
{
int opl;
int ppl;
if (same_id(&c->spd.that.id, peer_id) &&
peer_ca.ptr != NULL &&
trusted_ca_nss(peer_ca, c->spd.that.ca, &ppl) &&
ppl == 0 &&
match_requested_ca(requested_ca, c->spd.this.ca, &opl) &&
opl == 0) {
connection_buf cib;
dbg("refine_host_connection: happy with starting point: "PRI_CONNECTION"",
pri_connection(c, &cib));
/* peer ID matches current connection -- check for "you Tarzan, me Jane" */
if (!initiator && tarzan_id != NULL) {
/* ??? pexpect(c->spd.spd_next == NULL); */
if (idr_wildmatch(&c->spd.this, tarzan_id, st->st_logger)) {
dbg("the remote specified our ID in its IDr payload");
return c;
} else {
dbg("the remote specified an IDr that is not our ID for this connection");
}
} else {
dbg("the remote did not specify an IDr and our current connection is good enough");
return c;
}
}
}
const chunk_t *psk = NULL;
const struct RSA_private_key *my_RSA_pri = NULL;
if (initiator)
{
switch (this_authby) {
case AUTHBY_PSK:
psk = get_connection_psk(c, st->st_logger);
/*
* It should be virtually impossible to fail to find
* PSK: we just used it to decode the current message!
* Paul: only true for IKEv1
*/
if (psk == NULL) {
log_state(RC_LOG_SERIOUS, st, "cannot find PSK");
return c; /* cannot determine PSK, so not switching */
}
break;
case AUTHBY_NULL:
/* we know our AUTH_NULL key :) */
break;
case AUTHBY_RSASIG:
{
/*
* At this point, we've committed to our
* private key: we used it in our previous
* message. Paul: only true for IKEv1
*/
const struct pubkey_type *type = &pubkey_type_rsa;
if (get_connection_private_key(c, type, st->st_logger) == NULL) {
log_state(RC_LOG_SERIOUS, st, "cannot find %s key", type->name);
/* cannot determine my private key, so not switching */
return c;
}
break;
}
#if 0
case AUTHBY_ECDSA:
{
/*
* At this point, we've committed to our
* private key: we used it in our previous
* message. Paul: only true for IKEv1
*/
const struct pubkey_type *type = &pubkey_type_ecdsa;
if (get_connection_private_key(c, type) == NULL) {
log_state(RC_LOG_SERIOUS, st, "cannot find %s key", type->name);
/* cannot determine my private key, so not switching */
return c;
}
break;
}
#endif
default:
/* don't die on bad_case(auth); */
/* ??? why not dies? How could this happen? */
log_state(RC_LOG_SERIOUS, st,
"refine_host_connection: unexpected auth policy (%s): only handling PSK, NULL or RSA",
enum_name(&keyword_authby_names, this_authby));
return c;
}
}
/*
* The current connection won't do: search for one that will.
* First search for one with the same pair of hosts.
* If that fails, search for a suitable Road Warrior or Opportunistic
* connection (i.e. wildcard peer IP).
* We need to match:
* - peer_id (slightly complicated by instantiation)
* - if PSK auth, the key must not change (we used it to decode message)
* - policy-as-used must be acceptable to new connection
* - if initiator, also:
* + our ID must not change (we sent it in previous message)
* + our RSA key must not change (we used in in previous message)
*/
passert(c != NULL);
int best_our_pathlen = 0;
int best_peer_pathlen = 0;
struct connection *best_found = NULL;
int best_wildcards = 0;
/* wcpip stands for: wildcard Peer IP? */
for (unsigned wcpip = 1; wcpip <= 2; wcpip++) {
/*
* When starting second time around we're willing to
* settle for a connection that needs Peer IP
* instantiated: Road Warrior or Opportunistic. Look
* on list of connections for host pair with wildcard
* Peer IP.
*/
ip_address remote = wcpip == 2 ? unset_address : endpoint_address(&st->st_remote_endpoint);
FOR_EACH_HOST_PAIR_CONNECTION(&c->interface->ip_dev->id_address, &remote, d) {
int wildcards;
bool matching_peer_id = match_id(peer_id,
&d->spd.that.id,
&wildcards);
int peer_pathlen;
bool matching_peer_ca = trusted_ca_nss(peer_ca,
d->spd.that.ca,
&peer_pathlen);
int our_pathlen;
bool matching_requested_ca = match_requested_ca(requested_ca,
d->spd.this.ca,
&our_pathlen);
if (DBGP(DBG_BASE)) {
connection_buf b1, b2;
DBG_log("refine_host_connection: checking "PRI_CONNECTION" against "PRI_CONNECTION", best=%s with match=%d(id=%d(%d)/ca=%d(%d)/reqca=%d(%d))",
pri_connection(c, &b1), pri_connection(d, &b2),
best_found != NULL ? best_found->name : "(none)",
matching_peer_id && matching_peer_ca && matching_requested_ca,
matching_peer_id, wildcards,
matching_peer_ca, peer_pathlen,
matching_requested_ca, our_pathlen);
DBG_log("warning: not switching back to template of current instance");
}
/* 'You Tarzan, me Jane' check based on received IDr */
if (!initiator && tarzan_id != NULL) {
id_buf tzb;
dbg("peer expects us to be %s (%s) according to its IDr payload",
str_id(tarzan_id, &tzb),
enum_show(&ike_idtype_names, tarzan_id->kind));
id_buf usb;
dbg("this connection's local id is %s (%s)",
str_id(&d->spd.this.id, &usb),
enum_show(&ike_idtype_names, d->spd.this.id.kind));
/* ??? pexpect(d->spd.spd_next == NULL); */
if (!idr_wildmatch(&d->spd.this, tarzan_id, st->st_logger)) {
dbg("peer IDr payload does not match our expected ID, this connection will not do");
continue;
}
} else {
dbg("no IDr payload received from peer");
}
/* ignore group connections */
if (d->policy & POLICY_GROUP) {
dbg("skipping group connection");
continue;
}
/* matching_peer_ca and matching_requested_ca are required */
if (!matching_peer_ca || !matching_requested_ca) {
dbg("skipping !match2 || !match3");
continue;
}
/*
* Check if peer_id matches, exactly or after
* instantiation.
* Check for the match but also check to see if it's
* the %fromcert + peer id match result. - matt
*/
bool d_fromcert = FALSE;
if (!matching_peer_id) {
d_fromcert = d->spd.that.id.kind == ID_FROMCERT;
if (!d_fromcert) {
dbg("skipping because peer_id does not match");
continue;
}
}
/* if initiator, our ID must match exactly */
if (initiator &&
!same_id(&c->spd.this.id, &d->spd.this.id)) {
dbg("skipping because initiator id does not match");
continue;
}
if (d->ike_version != st->st_ike_version) {
/* IKE version has to match */
dbg("skipping because mismatching IKE version");
continue;
}
/*
* Authentication used must fit policy of this
* connection.
*/
if (ikev1) {
if ((auth_policy ^ d->policy) & POLICY_AGGRESSIVE)
continue; /* differ about aggressive mode */
if ((d->policy & auth_policy & ~POLICY_AGGRESSIVE) == LEMPTY) {
/* Our auth isn't OK for this connection. */
dbg("skipping because AUTH isn't right");
continue;
}
} else {
/*
* We need to check if leftauth and rightauth match, but we only know
* what the remote end will send IKE_AUTH message..
* Note with IKEv2 we are guaranteed to be a RESPONDER
* this_authby is the received AUTH payload type in IKE_AUTH reply.
* This also means, we have already sent out AUTH payload, so we cannot
* switch away from previously used this.authby.
*/
pexpect(!initiator);
if (this_authby != d->spd.that.authby) {
dbg("skipping because mismatched authby");
continue;
}
if (c->spd.this.authby != d->spd.this.authby) {
dbg("skipping because mismatched this authby");
continue;
}
}
if (d->spd.this.xauth_server != c->spd.this.xauth_server) {
/* Disallow IKEv2 CP or IKEv1 XAUTH mismatch */
dbg("skipping because mismatched xauthserver");
continue;
}
if (d->spd.this.xauth_client != c->spd.this.xauth_client) {
/* Disallow IKEv2 CP or IKEv1 XAUTH mismatch */
dbg("skipping because mismatched xauthclient");
continue;
}
connection_buf b1, b2;
dbg("refine_host_connection: checked "PRI_CONNECTION" against "PRI_CONNECTION", now for see if best",
pri_connection(c, &b1), pri_connection(d, &b2));
if (this_authby == AUTHBY_PSK) {
/* secret must match the one we already used */
const chunk_t *dpsk = get_connection_psk(d, st->st_logger);
/*
* We can change PSK mid-way in IKEv2 or aggressive mode.
* If we initiated, the key we used and the key
* we would have used with d must match.
*/
if (!((st->st_ike_version == IKEv2) || (auth_policy & POLICY_AGGRESSIVE))) {
if (dpsk == NULL)
continue; /* no secret */
if (initiator &&
!(psk->len == dpsk->len &&
memeq(psk->ptr, dpsk->ptr, psk->len)))
{
continue; /* different secret */
}
}
}
if (this_authby == AUTHBY_RSASIG) {
/*
* We must at least be able to find our private key.
* If we initiated, it must match the one we used in
* the IKEv1 SIG_I payload or IKEv2 AUTH payload that
* we sent previously.
*/
const struct pubkey_type *type = &pubkey_type_rsa;
const struct private_key_stuff *pks = get_connection_private_key(d, type, st->st_logger);
if (pks == NULL)
continue; /* no key */
/* XXX: same_{RSA,ECDSA}_public_key()? */
const struct RSA_private_key *pri = &pks->u.RSA_private_key;
if (initiator &&
!same_RSA_public_key(&my_RSA_pri->pub, &pri->pub)) {
dbg("skipping because mismatched pubkey");
continue; /* different key */
}
}
/*
* Paul: We need to check all the other relevant
* policy bits, like compression, pfs, etc
*/
/*
* d has passed all the tests.
* We'll go with it if the Peer ID was an exact match.
*/
if (matching_peer_id && wildcards == 0 &&
peer_pathlen == 0 && our_pathlen == 0)
{
*fromcert = d_fromcert;
dbg("returning because exact peer id match");
return d;
}
/*
* If it was a non-exact (wildcard) match, we'll
* remember it as best_found in case an exact match
* doesn't come along.
* ??? the logic involving *_pathlen looks wrong.
* ??? which matters more peer_pathlen or our_pathlen minimization?
*/
if (best_found == NULL || wildcards < best_wildcards ||
((wildcards == best_wildcards &&
peer_pathlen < best_peer_pathlen) ||
(peer_pathlen == best_peer_pathlen &&
our_pathlen < best_our_pathlen))) {
connection_buf cib;
dbg("refine_host_connection: picking new best "PRI_CONNECTION" (wild=%d, peer_pathlen=%d/our=%d)",
pri_connection(d, &cib),
wildcards, peer_pathlen,
our_pathlen);
*fromcert = d_fromcert;
best_found = d;
best_wildcards = wildcards;
best_peer_pathlen = peer_pathlen;
best_our_pathlen = our_pathlen;
}
}
}
return best_found;
}
/*
* With virtual addressing, we must not allow someone to use an already
* used (by another id) addr/net.
*/
static bool is_virtual_net_used(struct connection *c,
const ip_subnet *peer_net,
const struct id *peer_id)
{
dbg("FOR_EACH_CONNECTION_... in %s", __func__);
for (struct connection *d = connections; d != NULL; d = d->ac_next) {
switch (d->kind) {
case CK_PERMANENT:
case CK_TEMPLATE:
case CK_INSTANCE:
if ((subnetinsubnet(peer_net, &d->spd.that.client) ||
subnetinsubnet(&d->spd.that.client,
peer_net)) &&
!same_id(&d->spd.that.id, peer_id))
{
id_buf idb;
connection_buf cbuf;
subnet_buf client;
llog(RC_LOG, c->logger,
"Virtual IP %s overlaps with connection "PRI_CONNECTION" (kind=%s) '%s'",
str_subnet(peer_net, &client),
pri_connection(d, &cbuf),
enum_name(&connection_kind_names, d->kind),
str_id(&d->spd.that.id, &idb));
if (!kernel_ops->overlap_supported) {
llog(RC_LOG, c->logger,
"Kernel method '%s' does not support overlapping IP ranges",
kernel_ops->kern_name);
return TRUE;
}
if (LIN(POLICY_OVERLAPIP, c->policy & d->policy)) {
llog(RC_LOG, c->logger,
"overlap is okay by mutual consent");
/*
* Look for another overlap to report
* on.
*/
break;
}
/* We're not allowed to overlap. Carefully report. */
const struct connection *x =
LIN(POLICY_OVERLAPIP, c->policy) ? d :
LIN(POLICY_OVERLAPIP, d->policy) ? c :
NULL;
if (x == NULL) {
llog(RC_LOG, c->logger,
"overlap is forbidden (neither agrees to overlap)");
} else {
llog(RC_LOG, c->logger,
"overlap is forbidden ("PRI_CONNECTION" does not agree to overlap)",
pri_connection(x, &cbuf));
}
/* ??? why is this a separate log line? */
llog(RC_LOG, c->logger,
"Your ID is '%s'", str_id(peer_id, &idb));
return TRUE; /* already used by another one */
}
break;
case CK_GOING_AWAY:
default:
break;
}
}
return FALSE; /* you can safely use it */
}
/*
* find_client_connection: given a connection suitable for ISAKMP
* (i.e. the hosts match), find a one suitable for IPSEC
* (i.e. with matching clients).
*
* If we don't find an exact match (not even our current connection),
* we try for one that still needs instantiation. Try Road Warrior
* abstract connections and the Opportunistic abstract connections.
* This requires inverse instantiation: abstraction.
*
* After failing to find an exact match, we abstract the peer
* to be NO_IP (the wildcard value). This enables matches with
* Road Warrior and Opportunistic abstract connections.
*
* After failing that search, we also abstract the Phase 1 peer ID
* if possible. If the peer's ID was the peer's IP address, we make
* it NO_ID; instantiation will make it the peer's IP address again.
*
* If searching for a Road Warrior abstract connection fails,
* and conditions are suitable, we search for the best Opportunistic
* abstract connection.
*
* Note: in the end, both Phase 1 IDs must be preserved, after any
* instantiation. They are the IDs that have been authenticated.
*/
#define PATH_WEIGHT 1
#define WILD_WEIGHT (MAX_CA_PATH_LEN + 1)
#define PRIO_WEIGHT ((MAX_WILDCARDS + 1) * WILD_WEIGHT)
/* fc_try: a helper function for find_client_connection */
static struct connection *fc_try(const struct connection *c,
const struct host_pair *hp,
const ip_subnet *our_net,
const ip_subnet *peer_net,
const uint8_t our_protocol,
const uint16_t our_port,
const uint8_t peer_protocol,
const uint16_t peer_port)
{
struct connection *best = NULL;
policy_prio_t best_prio = BOTTOM_PRIO;
const bool peer_net_is_host = subnetisaddr(peer_net,
&c->spd.that.host_addr);
err_t virtualwhy = NULL;
struct connection *d;
for (d = hp->connections; d != NULL; d = d->hp_next) {
if (d->policy & POLICY_GROUP)
continue;
int wildcards, pathlen;
if (!(same_id(&c->spd.this.id, &d->spd.this.id) &&
match_id(&c->spd.that.id, &d->spd.that.id, &wildcards) &&
trusted_ca_nss(c->spd.that.ca, d->spd.that.ca, &pathlen)))
{
continue;
}
/* compare protocol and ports */
if (!(d->spd.this.protocol == our_protocol &&
d->spd.that.protocol == peer_protocol &&
(d->spd.this.port == 0 || d->spd.this.port == our_port) &&
(d->spd.that.has_port_wildcard || d->spd.that.port == peer_port)))
{
continue;
}
/*
* non-Opportunistic case:
* our_client must match.
*
* So must peer_client, but the testing is complicated
* by the fact that the peer might be a wildcard
* and if so, the default value of that.client
* won't match the default peer_net. The appropriate test:
*
* If d has a peer client, it must match peer_net.
* If d has no peer client, peer_net must just have peer itself.
*/
const struct spd_route *sr;
for (sr = &d->spd; best != d && sr != NULL; sr = sr->spd_next) {
if (DBGP(DBG_BASE)) {
selector_buf s1, d1;
selector_buf s3, d3;
DBG_log(" fc_try trying %s:%s:%d/%d -> %s:%d/%d%s vs %s:%s:%d/%d -> %s:%d/%d%s",
c->name, str_selector(our_net, &s1),
c->spd.this.protocol, c->spd.this.port,
str_selector(peer_net, &d1),
c->spd.that.protocol, c->spd.that.port,
is_virtual_connection(c) ?
"(virt)" : "", d->name,
str_selector(&sr->this.client, &s3),
sr->this.protocol, sr->this.port,
str_selector(&sr->that.client, &d3),
sr->that.protocol, sr->that.port,
is_virtual_sr(sr) ? "(virt)" : "");
}
if (!samesubnet(&sr->this.client, our_net)) {
if (DBGP(DBG_BASE)) {
selector_buf s1, s3;
DBG_log(" our client (%s) not in our_net (%s)",
str_selector(&sr->this.client, &s3),
str_selector(our_net, &s1));
}
continue;
}
if (sr->that.has_client) {
if (!samesubnet(&sr->that.client, peer_net) &&
!is_virtual_sr(sr)) {
if (DBGP(DBG_BASE)) {
selector_buf d1, d3;
DBG_log(" their client (%s) not in same peer_net (%s)",
str_selector(&sr->that.client, &d3),
str_selector(peer_net, &d1));
}
continue;
}
virtualwhy = check_virtual_net_allowed(
d,
peer_net,
&sr->that.host_addr);
if (is_virtual_sr(sr) &&
(virtualwhy != NULL ||
is_virtual_net_used(
d,
peer_net,
&sr->that.id)))
{
dbg(" virtual net not allowed");
continue;
}
} else if (!peer_net_is_host) {
continue;
}
/*
* We've run the gauntlet -- success:
* We've got an exact match of subnets.
* The connection is feasible, but we continue looking
* for the best.
* The highest priority wins, implementing eroute-like
* rule.
* - a routed connection is preferrred
* - given that, the smallest number of ID wildcards
* are preferred
* - given that, the shortest CA pathlength is preferred
* - given that, not switching is preferred
*/
policy_prio_t prio =
PRIO_WEIGHT * routed(sr->routing) +
WILD_WEIGHT * (MAX_WILDCARDS - wildcards) +
PATH_WEIGHT * (MAX_CA_PATH_LEN - pathlen) +
(c == d ? 1 : 0) +
1;
if (prio > best_prio) {
best = d;
best_prio = prio;
}
}
}
if (best != NULL && NEVER_NEGOTIATE(best->policy))
best = NULL;
dbg(" fc_try concluding with %s [%" PRIu32 "]",
(best ? best->name : "none"), best_prio);
if (best == NULL && virtualwhy != NULL) {
llog(RC_LOG, c->logger,
"peer proposal was rejected in a virtual connection policy: %s",
virtualwhy);
}
return best;
}
static struct connection *fc_try_oppo(const struct connection *c,
const struct host_pair *hp,
const ip_subnet *our_net,
const ip_subnet *peer_net,
const uint8_t our_protocol,
const uint16_t our_port,
const uint8_t peer_protocol,
const uint16_t peer_port)
{
struct connection *best = NULL;
policy_prio_t best_prio = BOTTOM_PRIO;
struct connection *d;
for (d = hp->connections; d != NULL; d = d->hp_next) {
if (d->policy & POLICY_GROUP)
continue;
int wildcards, pathlen;
if (!(same_id(&c->spd.this.id, &d->spd.this.id) &&
match_id(&c->spd.that.id, &d->spd.that.id, &wildcards) &&
trusted_ca_nss(c->spd.that.ca, d->spd.that.ca, &pathlen)))
{
continue;
}
/* compare protocol and ports */
if (d->spd.this.protocol != our_protocol ||
(d->spd.this.port && d->spd.this.port != our_port) ||
d->spd.that.protocol != peer_protocol ||
(d->spd.that.port != peer_port &&
!d->spd.that.has_port_wildcard))
continue;
/*
* Opportunistic case:
* our_net must be inside d->spd.this.client
* and peer_net must be inside d->spd.that.client
* Note: this host_pair chain also has shunt
* eroute conns (clear, drop), but they won't
* be marked as opportunistic.
*/
const struct spd_route *sr;
for (sr = &d->spd; sr != NULL; sr = sr->spd_next) {
if (DBGP(DBG_BASE)) {
selector_buf s1;
selector_buf d1;
selector_buf s3;
selector_buf d3;
DBG_log(" fc_try_oppo trying %s:%s -> %s vs %s:%s -> %s",
c->name, str_selector(our_net, &s1),
str_selector(peer_net, &d1),
d->name, str_selector(&sr->this.client, &s3),
str_selector(&sr->that.client, &d3));
}
if (!subnetinsubnet(our_net, &sr->this.client) ||
!subnetinsubnet(peer_net, &sr->that.client))
continue;
/*
* The connection is feasible, but we continue looking
* for the best.
* The highest priority wins, implementing eroute-like
* rule.
* - our smallest client subnet is preferred (longest
* mask)
* - given that, peers smallest client subnet is preferred
* - given that, a routed connection is preferrred
* - given that, the smallest number of ID wildcards
* are preferred
* - given that, the shortest CA pathlength is preferred
*/
policy_prio_t prio =
PRIO_WEIGHT * (d->policy_prio + routed(sr->routing)) +
WILD_WEIGHT * (MAX_WILDCARDS - wildcards) +
PATH_WEIGHT * (MAX_CA_PATH_LEN - pathlen);
if (prio > best_prio) {
best = d;
best_prio = prio;
}
}
}
/* if the best wasn't opportunistic, we fail: it must be a shunt */
if (best != NULL &&
(NEVER_NEGOTIATE(best->policy) ||
(best->policy & POLICY_OPPORTUNISTIC) == LEMPTY))
best = NULL;
dbg(" fc_try_oppo concluding with %s [%" PRIu32 "]",
(best ? best->name : "none"), best_prio);
return best;
}
struct connection *find_client_connection(struct connection *const c,
const ip_subnet *our_net,
const ip_subnet *peer_net,
const uint8_t our_protocol,
const uint16_t our_port,
const uint8_t peer_protocol,
const uint16_t peer_port)
{
struct connection *d;
/* weird things can happen to our interfaces */
if (!oriented(*c)) {
return NULL;
}
if (DBGP(DBG_BASE)) {
selector_buf s1;
selector_buf d1;
DBG_log("find_client_connection starting with %s",
c->name);
DBG_log(" looking for %s:%d/%d -> %s:%d/%d",
str_selector(our_net, &s1), our_protocol, our_port,
str_selector(peer_net, &d1), peer_protocol, peer_port);
}
/*
* Give priority to current connection
* but even greater priority to a routed concrete connection.
*/
{
struct connection *unrouted = NULL;
int srnum = -1;
const struct spd_route *sr;
for (sr = &c->spd; unrouted == NULL && sr != NULL;
sr = sr->spd_next) {
srnum++;
if (DBGP(DBG_BASE)) {
selector_buf s2;
selector_buf d2;
DBG_log(" concrete checking against sr#%d %s -> %s", srnum,
str_selector(&sr->this.client, &s2),
str_selector(&sr->that.client, &d2));
}
if (samesubnet(&sr->this.client, our_net) &&
samesubnet(&sr->that.client, peer_net) &&
sr->this.protocol == our_protocol &&
(!sr->this.port ||
sr->this.port == our_port) &&
(sr->that.protocol == peer_protocol) &&
(!sr->that.port ||
sr->that.port == peer_port)) {
if (routed(sr->routing))
return c;
unrouted = c;
}
}
/* exact match? */
/*
* clang 3.4 says: warning: Access to field 'host_pair' results in a dereference of a null pointer (loaded from variable 'c')
* If so, the caller must have passed NULL for it
* and earlier references would be wrong (segfault).
*/
d = fc_try(c, c->host_pair, our_net, peer_net,
our_protocol, our_port, peer_protocol, peer_port);
dbg(" fc_try %s gives %s", c->name, (d ? d->name : "none"));
if (d == NULL)
d = unrouted;
}
if (d == NULL) {
/* look for an abstract connection to match */
const struct host_pair *hp = NULL;
const struct spd_route *sra;
for (sra = &c->spd; hp == NULL &&
sra != NULL; sra = sra->spd_next) {
hp = find_host_pair(&sra->this.host_addr, NULL);
if (DBGP(DBG_BASE)) {
selector_buf s2;
selector_buf d2;
DBG_log(" checking hostpair %s -> %s is %s",
str_selector(&sra->this.client, &s2),
str_selector(&sra->that.client, &d2),
(hp ? "found" : "not found"));
}
}
if (hp != NULL) {
/* RW match with actual peer_id or abstract peer_id? */
d = fc_try(c, hp, our_net, peer_net,
our_protocol, our_port, peer_protocol,
peer_port);
if (d == NULL &&
subnetishost(our_net) &&
subnetishost(peer_net)) {
/*
* Opportunistic match?
* Always use abstract peer_id.
* Note that later instantiation will result
* in the same peer_id.
*/
d = fc_try_oppo(c, hp, our_net, peer_net,
our_protocol, our_port,
peer_protocol, peer_port);
}
}
}
dbg(" concluding with d = %s", (d ? d->name : "none"));
return d;
}
/* signed result suitable for quicksort */
int connection_compare(const struct connection *ca,
const struct connection *cb)
{
int ret;
ret = strcmp(ca->name, cb->name);
if (ret != 0)
return ret;
/* note: enum connection_kind behaves like int */
ret = ca->kind - cb->kind;
if (ret != 0)
return ret;
/* same name, and same type */
switch (ca->kind) {
case CK_INSTANCE:
return ca->instance_serial < cb->instance_serial ? -1 :
ca->instance_serial > cb-> instance_serial ? 1 : 0;
default:
return (ca->policy_prio < cb->policy_prio ? -1 :
ca->policy_prio > cb->policy_prio ? 1 : 0);
}
}
static int connection_compare_qsort(const void *a, const void *b)
{
return connection_compare(*(const struct connection *const *)a,
*(const struct connection *const *)b);
}
static void show_one_sr(struct show *s,
const struct connection *c,
const struct spd_route *sr,
const char *instance)
{
char topo[CONN_BUF_LEN];
ipstr_buf thisipb, thatipb;
show_comment(s, "\"%s\"%s: %s; %s; eroute owner: #%lu",
c->name, instance,
format_connection(topo, sizeof(topo), c, sr),
enum_name(&routing_story, sr->routing),
sr->eroute_owner);
#define OPT_HOST(h, ipb) (address_is_specified(h) ? str_address(h, &ipb) : "unset")
/* note: this macro generates a pair of arguments */
#define OPT_PREFIX_STR(pre, s) (s) == NULL ? "" : (pre), (s) == NULL? "" : (s)
show_comment(s,
"\"%s\"%s: %s; my_ip=%s; their_ip=%s%s%s%s%s; my_updown=%s;",
c->name, instance,
oriented(*c) ? "oriented" : "unoriented",
OPT_HOST(&c->spd.this.host_srcip, thisipb),
OPT_HOST(&c->spd.that.host_srcip, thatipb),
OPT_PREFIX_STR("; mycert=", cert_nickname(&sr->this.cert)),
OPT_PREFIX_STR("; peercert=", cert_nickname(&sr->that.cert)),
(sr->this.updown == NULL || streq(sr->this.updown, "%disabled")) ?
"<disabled>" : sr->this.updown
);
#undef OPT_HOST
#undef OPT_PREFIX_STR
/*
* Both should not be set, but if they are, we want
* to know
*/
#define COMBO(END, SERVER, CLIENT) \
((END).SERVER ? \
((END).CLIENT ? "BOTH??" : "server") : \
((END).CLIENT ? "client" : "none"))
show_comment(s,
"\"%s\"%s: xauth us:%s, xauth them:%s, %s my_username=%s; their_username=%s",
c->name, instance,
/*
* Both should not be set, but if they are, we want to
* know
*/
COMBO(sr->this, xauth_server, xauth_client),
COMBO(sr->that, xauth_server, xauth_client),
/* should really be an enum name */
sr->this.xauth_server ?
c->xauthby == XAUTHBY_FILE ?
"xauthby:file;" :
c->xauthby == XAUTHBY_PAM ?
"xauthby:pam;" :
"xauthby:alwaysok;" :
"",
sr->this.xauth_username != NULL ? sr->this.xauth_username : "[any]",
sr->that.xauth_username != NULL ? sr->that.xauth_username : "[any]");
struct esb_buf auth1, auth2;
show_comment(s,
"\"%s\"%s: our auth:%s, their auth:%s",
c->name, instance,
enum_show_shortb(&keyword_authby_names, sr->this.authby, &auth1),
enum_show_shortb(&keyword_authby_names, sr->that.authby, &auth2));
show_comment(s,
"\"%s\"%s: modecfg info: us:%s, them:%s, modecfg policy:%s, dns:%s, domains:%s, cat:%s;",
c->name, instance,
COMBO(sr->this, modecfg_server, modecfg_client),
COMBO(sr->that, modecfg_server, modecfg_client),
(c->policy & POLICY_MODECFG_PULL) ? "pull" : "push",
(c->modecfg_dns == NULL) ? "unset" : c->modecfg_dns,
(c->modecfg_domains == NULL) ? "unset" : c->modecfg_domains,
sr->this.cat ? "set" : "unset");
#undef COMBO
if (c->modecfg_banner != NULL) {
show_comment(s, "\"%s\"%s: banner:%s;",
c->name, instance, c->modecfg_banner);
}
/* we only support symmetric labels, but store it in struct end - pick one */
if (sr->this.sec_label.len == 0)
show_comment(s, "\"%s\"%s: sec_label:unset;", c->name, instance);
else
show_comment(s, "\"%s\"%s: sec_label:%.*s;",
c->name, instance,
(int)sr->this.sec_label.len, sr->this.sec_label.ptr);
}
void show_one_connection(struct show *s,
const struct connection *c)
{
const char *ifn;
char ifnstr[2 * IFNAMSIZ + 2]; /* id_rname@id_vname\0 */
char instance[32];
char mtustr[8];
char sapriostr[13];
char satfcstr[13];
char nflogstr[8];
char markstr[2 * (2 * strlen("0xffffffff") + strlen("/")) + strlen(", ") ];
if (oriented(*c)) {
if (c->xfrmi != NULL && c->xfrmi->name != NULL) {
char *n = jam_str(ifnstr, sizeof(ifnstr),
c->xfrmi->name);
add_str(ifnstr, sizeof(ifnstr), n, "@");
add_str(ifnstr, sizeof(ifnstr), n,
c->interface->ip_dev->id_rname);
ifn = ifnstr;
} else {
ifn = c->interface->ip_dev->id_rname;
}
} else {
ifn = "";
};
instance[0] = '\0';
if (c->kind == CK_INSTANCE && c->instance_serial != 0)
snprintf(instance, sizeof(instance), "[%lu]",
c->instance_serial);
/* Show topology. */
{
const struct spd_route *sr = &c->spd;
while (sr != NULL) {
show_one_sr(s, c, sr, instance);
sr = sr->spd_next;
}
}
/* Show CAs */
if (c->spd.this.ca.ptr != NULL || c->spd.that.ca.ptr != NULL) {
dn_buf this_ca, that_ca;
show_comment(s,
"\"%s\"%s: CAs: '%s'...'%s'",
c->name,
instance,
str_dn_or_null(c->spd.this.ca, "%any", &this_ca),
str_dn_or_null(c->spd.that.ca, "%any", &that_ca));
}
show_comment(s,
"\"%s\"%s: ike_life: %jds; ipsec_life: %jds; replay_window: %u; rekey_margin: %jds; rekey_fuzz: %lu%%; keyingtries: %lu;",
c->name,
instance,
deltasecs(c->sa_ike_life_seconds),
deltasecs(c->sa_ipsec_life_seconds),
c->sa_replay_window,
deltasecs(c->sa_rekey_margin),
c->sa_rekey_fuzz,
c->sa_keying_tries);
show_comment(s,
"\"%s\"%s: retransmit-interval: %jdms; retransmit-timeout: %jds; iketcp:%s; iketcp-port:%d;",
c->name,
instance,
deltamillisecs(c->r_interval),
deltasecs(c->r_timeout),
c->iketcp == IKE_TCP_NO ? "no" : c->iketcp == IKE_TCP_ONLY ? "yes" :
c->iketcp == IKE_TCP_FALLBACK ? "fallback" : "<BAD VALUE>",
c->remote_tcpport);
show_comment(s,
"\"%s\"%s: initial-contact:%s; cisco-unity:%s; fake-strongswan:%s; send-vendorid:%s; send-no-esp-tfc:%s;",
c->name, instance,
bool_str(c->initial_contact),
bool_str(c->cisco_unity),
bool_str(c->fake_strongswan),
bool_str(c->send_vendorid),
bool_str(c->send_no_esp_tfc));
if (c->policy_next != NULL) {
show_comment(s,
"\"%s\"%s: policy_next: %s",
c->name, instance, c->policy_next->name);
}
lset_t policy = c->policy;
policy_buf pb;
show_comment(s, "\"%s\"%s: policy: %s%s%s%s%s%s%s;",
c->name, instance,
c->ike_version > 0 ? enum_name(&ike_version_names, c->ike_version) : "",
c->ike_version > 0 && policy != LEMPTY ? "+" : "",
str_policy(policy, &pb),
NEVER_NEGOTIATE(policy) ? "+NEVER_NEGOTIATE" : "",
(c->spd.this.key_from_DNS_on_demand ||
c->spd.that.key_from_DNS_on_demand) ? "; " : "",
c->spd.this.key_from_DNS_on_demand ? "+lKOD" : "",
c->spd.that.key_from_DNS_on_demand ? "+rKOD" : "");
if (c->ike_version == IKEv2) {
lset_buf hashpolbuf;
const char *hashstr = str_lset(&sighash_policy_bit_names,
c->sighash_policy,
&hashpolbuf);
show_comment(s, "\"%s\"%s: v2-auth-hash-policy: %s;",
c->name, instance, hashstr);
}
if (c->connmtu != 0)
snprintf(mtustr, sizeof(mtustr), "%d", c->connmtu);
else
strcpy(mtustr, "unset");
if (c->sa_priority != 0)
snprintf(sapriostr, sizeof(sapriostr), "%" PRIu32, c->sa_priority);
else
strcpy(sapriostr, "auto");
if (c->sa_tfcpad != 0)
snprintf(satfcstr, sizeof(satfcstr), "%u", c->sa_tfcpad);
else
strcpy(satfcstr, "none");
policy_prio_buf prio;
show_comment(s,
"\"%s\"%s: conn_prio: %s; interface: %s; metric: %u; mtu: %s; sa_prio:%s; sa_tfc:%s;",
c->name, instance,
str_policy_prio(c->policy_prio, &prio),
ifn,
c->metric,
mtustr, sapriostr, satfcstr);
if (c->nflog_group != 0)
snprintf(nflogstr, sizeof(nflogstr), "%d", c->nflog_group);
else
strcpy(nflogstr, "unset");
if (c->sa_marks.in.val != 0 || c->sa_marks.out.val != 0 ) {
snprintf(markstr, sizeof(markstr), "%" PRIu32 "/%#08" PRIx32 ", %" PRIu32 "/%#08" PRIx32,
c->sa_marks.in.val, c->sa_marks.in.mask,
c->sa_marks.out.val, c->sa_marks.out.mask);
} else {
strcpy(markstr, "unset");
}
show_comment(s,
"\"%s\"%s: nflog-group: %s; mark: %s; vti-iface:%s; "
"vti-routing:%s; vti-shared:%s;"
" nic-offload:%s;"
,
c->name, instance, nflogstr, markstr,
c->vti_iface == NULL ? "unset" : c->vti_iface,
bool_str(c->vti_routing),
bool_str(c->vti_shared),
(c->nic_offload == yna_auto) ? "auto" :
bool_str(c->nic_offload == yna_yes)
);
{
id_buf thisidb;
id_buf thatidb;
show_comment(s,
"\"%s\"%s: our idtype: %s; our id=%s; their idtype: %s; their id=%s",
c->name, instance,
enum_name(&ike_idtype_names_extended, c->spd.this.id.kind),
str_id(&c->spd.this.id, &thisidb),
enum_name(&ike_idtype_names_extended, c->spd.that.id.kind),
str_id(&c->spd.that.id, &thatidb));
}
/* slightly complicated stuff to avoid extra crap */
show_comment(s,
"\"%s\"%s: dpd: %s; delay:%ld; timeout:%ld; nat-t: encaps:%s; nat_keepalive:%s; ikev1_natt:%s",
c->name, instance,
enum_name(&dpd_action_names, c->dpd_action),
(long) deltasecs(c->dpd_delay),
(long) deltasecs(c->dpd_timeout),
(c->encaps == yna_auto) ? "auto" :
bool_str(c->encaps == yna_yes),
bool_str(c->nat_keepalive),
(c->ikev1_natt == NATT_BOTH) ? "both" :
(c->ikev1_natt == NATT_RFC) ? "rfc" :
(c->ikev1_natt == NATT_DRAFTS) ? "drafts" : "none"
);
if (!lmod_empty(c->extra_debugging)) {
SHOW_JAMBUF(RC_COMMENT, s, buf) {
jam(buf, "\"%s\"%s: debug: ",
c->name, instance);
jam_lmod(buf, &debug_names, "+", c->extra_debugging);
}
}
SHOW_JAMBUF(RC_COMMENT, s, buf) {
jam(buf, "\"%s\"%s: newest ISAKMP SA: #%lu; newest IPsec SA: #%lu; conn serial: "PRI_CO"",
c->name,
instance,
c->newest_isakmp_sa,
c->newest_ipsec_sa,
pri_co(c->serialno));
if (c->serial_from.co/*oops*/ != 0) {
jam(buf, ", instantiated from: "PRI_CO";",
pri_co(c->serial_from));
} else {
jam(buf, ";");
}
}
if (c->connalias != NULL) {
show_comment(s, "\"%s\"%s: aliases: %s",
c->name,
instance,
c->connalias);
}
show_ike_alg_connection(s, c, instance);
show_kernel_alg_connection(s, c, instance);
}
void show_connections_status(struct show *s)
{
int count = 0;
int active = 0;
struct connection *c;
show_separator(s);
show_comment(s, "Connection list:");
show_separator(s);
dbg("FOR_EACH_CONNECTION_... in %s", __func__);
for (c = connections; c != NULL; c = c->ac_next) {
count++;
if (c->spd.routing == RT_ROUTED_TUNNEL)
active++;
}
if (count != 0) {
/* make an array of connections, sort it, and report it */
struct connection **array =
alloc_bytes(sizeof(struct connection *) * count,
"connection array");
int i = 0;
dbg("FOR_EACH_CONNECTION_... in %s", __func__);
for (c = connections; c != NULL; c = c->ac_next)
array[i++] = c;
/* sort it! */
qsort(array, count, sizeof(struct connection *),
connection_compare_qsort);
for (i = 0; i < count; i++)
show_one_connection(s, array[i]);
pfree(array);
show_separator(s);
}
show_comment(s, "Total IPsec connections: loaded %d, active %d",
count, active);
}
/*
* Delete a connection if
* - it is an instance and it is no longer in use.
* - the ike state is not shared with another connection
* We must be careful to avoid circularity:
* we don't touch it if it is CK_GOING_AWAY.
*/
void connection_delete_unused_instance(struct connection **cp,
struct state *old_state,
struct fd *whackfd)
{
struct connection *c = (*cp);
*cp = NULL;
if (c->kind != CK_INSTANCE) {
dbg("connection %s is not an instance, skipping delete-unused", c->name);
return;
}
if (connection_is_pending(c)) {
dbg("connection instance %s is pending, skipping delete-unused",
c->name);
return;
}
if (LIN(POLICY_UP, c->policy) &&
old_state != NULL && IS_IKE_SA_ESTABLISHED(old_state)) {
/*
* If this connection instance was previously for an
* established sa planning to revive, don't delete.
*/
dbg("connection instance %s with serial "PRI_CO" is being revived, skipping delete-unused",
c->name, pri_co(c->serialno));
return;
}
/* find the first */
struct state *st = state_by_connection(c, NULL, NULL, __func__);
if (DBGP(DBG_BASE)) {
/*
* Cross check that the state DB has been kept
* up-to-date.
*/
struct state *dst = NULL;
FOR_EACH_STATE_NEW2OLD(dst) {
if (dst->st_connection == c) {
break;
}
}
/* found a state, may not be the same */
pexpect((dst == NULL) == (st == NULL));
st = dst; /* let the truth be free */
}
if (st != NULL) {
dbg("connection instance %s in use by #%lu, skipping delete-unused",
c->name, st->st_serialno);
return;
}
dbg("connection instance %s is not being used, deleting", c->name);
/* XXX: something better? */
close_any(&c->logger->global_whackfd);
c->logger->global_whackfd = dup_any(whackfd);
delete_connection(c, false);
}
/*
* Every time a state's connection is changed, the following need to happen:
*
* - update the connection->state hash table
*
* - discard the old connection when not in use
*/
void update_state_connection(struct state *st, struct connection *new)
{
struct connection *old = st->st_connection;
passert(old != NULL);
passert(new != NULL);
if (old != new) {
st->st_connection = new;
st->st_peer_alt_id = FALSE; /* must be rechecked against new 'that' */
rehash_state_connection(st);
if (old != NULL) {
connection_delete_unused_instance(&old, st,
st->st_logger->global_whackfd);
}
}
}
/*
* A template connection's eroute can be eclipsed by
* either a %hold or an eroute for an instance iff
* the template is a /32 -> /32. This requires some special casing.
*/
long eclipse_count = 0;
struct connection *eclipsed(const struct connection *c, struct spd_route **esrp /*OUT*/)
{
/*
* This function was changed in freeswan 2.02 and since
* then has never worked because it always returned NULL.
* It should be caught by the testing/pluto/co-terminal test cases.
* ??? DHR doesn't know how much of this is true.
*/
/* ??? this logic seems broken: it doesn't try all spd_routes of c */
/* XXX This logic also predates support for protoports, which isn't handled below */
struct connection *ue;
dbg("FOR_EACH_CONNECTION_... in %s", __func__);
for (ue = connections; ue != NULL; ue = ue->ac_next) {
struct spd_route *srue;
for (srue = &ue->spd; srue != NULL; srue =srue->spd_next) {
const struct spd_route *src;
for (src = &c->spd; src != NULL; src = src->spd_next) {
if (srue->routing == RT_ROUTED_ECLIPSED &&
samesubnet(&src->this.client, &srue->this.client) &&
samesubnet(&src->that.client, &srue->that.client))
{
dbg("%s eclipsed %s", c->name, ue->name);
*esrp = srue;
return ue;
}
}
}
}
*esrp = NULL;
return NULL;
}
void liveness_clear_connection(struct connection *c, const char *v)
{
/*
* For CK_INSTANCE, delete_states_by_connection() will clear
* Note that delete_states_by_connection changes c->kind but we need
* to remember what it was to know if we still need to unroute after delete
*/
if (c->kind == CK_INSTANCE) {
delete_states_by_connection(c, TRUE, null_fd/*no-whack?*/);
} else {
flush_pending_by_connection(c); /* remove any partial negotiations that are failing */
delete_states_by_connection(c, TRUE, null_fd/*no-whack?*/);
dbg("%s: unrouting connection %s action - clearing",
enum_name(&connection_kind_names, c->kind), v);
unroute_connection(c); /* --unroute */
}
}
void liveness_action(struct state *st)
{
struct connection *c = st->st_connection;
const char *ikev = enum_name(&ike_version_liveness_names, st->st_ike_version);
passert(ikev != NULL);
switch (c->dpd_action) {
case DPD_ACTION_CLEAR:
log_state(RC_LOG, st,
"%s action - clearing connection kind %s", ikev,
enum_name(&connection_kind_names, c->kind));
liveness_clear_connection(c, ikev);
break;
case DPD_ACTION_RESTART:
log_state(RC_LOG, st,
"%s action - restarting all connections that share this peer",
ikev);
restart_connections_by_peer(c);
break;
case DPD_ACTION_HOLD:
log_state(RC_LOG, st,
"%s action - putting connection into hold", ikev);
if (c->kind == CK_INSTANCE) {
dbg("%s warning dpdaction=hold on instance futile - will be deleted", ikev);
}
delete_states_by_connection(c, TRUE, null_fd/*no-whack?*/);
break;
default:
bad_case(c->dpd_action);
}
}
/*
* This is to support certificates with SAN using wildcard, eg SAN
* contains DNS:*.vpnservice.com where our leftid=*.vpnservice.com
*/
static bool idr_wildmatch(const struct end *this, const struct id *idr, struct logger *logger)
{
/* check if received IDr is a valid SAN of our cert */
/* cert_VerifySubjectAltName, if called, will [debug]log any errors */
/* XXX: calling cert_VerifySubjectAltName with ID_DER_ASN1_DN futile? */
/* ??? if cert matches we don't actually do any further ID matching, wildcard or not */
if (this->cert.ty != CERT_NONE &&
(idr->kind == ID_FQDN || idr->kind == ID_DER_ASN1_DN) &&
cert_VerifySubjectAltName(this->cert.u.nss_cert, idr, logger))
return true;
const struct id *wild = &this->id;
/* if not both ID_FQDN, fall back to same_id (no wildcarding possible) */
if (idr->kind != ID_FQDN || wild->kind != ID_FQDN)
return same_id(wild, idr);
size_t wl = wild->name.len;
const char *wp = (const char *) wild->name.ptr;
/* if wild has no *, fall back to same_id (no wildcard present) */
if (wl == 0 || wp[0] != '*')
return same_id(wild, idr);
while (wp[wl - 1] == '.')
wl--; /* strip trailing dot */
size_t il = idr->name.len;
const char *ip = (const char *) idr->name.ptr;
while (il > 0 && ip[il - 1] == '.')
il--; /* strip trailing dot */
/*
* ??? should we require that the * match only whole components?
* wl-1 == il || // total match
* wl > 1 && wp[1] == '.' || // wild included leading "."
* ip[il-(wl-1) - 1] == '.' // match preceded by "."
*/
return wl-1 <= il && strncaseeq(&wp[1], &ip[il-(wl-1)], wl-1);
}
/* sa priority and type should really go into kernel_sa */
uint32_t calculate_sa_prio(const struct connection *c, bool oe_shunt)
{
if (c->sa_priority != 0) {
dbg("priority calculation of connection \"%s\" overruled by connection specification of %"PRIu32" (%#"PRIx32")",
c->name, c->sa_priority, c->sa_priority);
return c->sa_priority;
}
if (LIN(POLICY_GROUP, c->policy)) {
dbg("priority calculation of connection \"%s\" skipped - group template does not install SPDs",
c->name);
return 0;
}
uint32_t pmax =
(LIN(POLICY_GROUPINSTANCE, c->policy)) ?
(LIN(POLICY_AUTH_NULL, c->policy)) ?
PLUTO_SPD_OPPO_ANON_MAX :
PLUTO_SPD_OPPO_MAX :
PLUTO_SPD_STATIC_MAX;
uint32_t portsw = /* max 2 (2 bits) */
(c->spd.this.port == 0 ? 0 : 1) +
(c->spd.that.port == 0 ? 0 : 1);
uint32_t protow = c->spd.this.protocol == 0 ? 0 : 1; /* (1 bit) */
/*
* For transport mode or /32 to /32, the client mask bits are
* set based on the host_addr parameters.
*/
uint32_t srcw, dstw; /* each max 128 (8 bits) */
srcw = c->spd.this.client.maskbits;
dstw = c->spd.that.client.maskbits;
/* if opportunistic, override the template destination mask with /32 or /128 */
if (oe_shunt) {
dstw = (addrtypeof(&c->spd.that.host_addr) == AF_INET) ? 32 : 128;
}
/* ensure an instance of a template/OE-group always has preference */
uint32_t instw = (c->kind == CK_INSTANCE ? 0u : 1u);
uint32_t prio = pmax - (portsw << 18 | protow << 17 | srcw << 9 | dstw << 1 | instw);
dbg("priority calculation of connection \"%s\" is %"PRIu32" (%#"PRIx32")",
c->name, prio, prio);
return prio;
}
/*
* If the connection contains a newer SA, return it.
*/
so_serial_t get_newer_sa_from_connection(struct state *st)
{
struct connection *c = st->st_connection;
so_serial_t newest;
if (IS_IKE_SA(st)) {
newest = c->newest_isakmp_sa;
dbg("picked newest_isakmp_sa #%lu for #%lu",
newest, st->st_serialno);
} else {
newest = c->newest_ipsec_sa;
dbg("picked newest_ipsec_sa #%lu for #%lu",
newest, st->st_serialno);
}
if (newest != SOS_NOBODY && newest != st->st_serialno) {
return newest;
} else {
return SOS_NOBODY;
}
}
/* check to see that Ids of peers match */
bool same_peer_ids(const struct connection *c, const struct connection *d,
const struct id *peer_id)
{
return same_id(&c->spd.this.id, &d->spd.this.id) &&
same_id(peer_id == NULL ? &c->spd.that.id : peer_id,
&d->spd.that.id);
}
/* reread all left/right certificates from NSS DB */
void reread_cert_connections(struct fd *whackfd)
{
struct connection *c;
dbg("FOR_EACH_CONNECTION_... in %s", __func__);
for (c = connections; c != NULL; c = c->ac_next) {
reread_cert(whackfd, c);
}
}
|