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
|
/* information about connections between hosts and clients
* Copyright (C) 1998-2002 D. Hugh Redelmeier.
*
* 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 <http://www.fsf.org/copyleft/gpl.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.
*
* RCSID $Id: connections.c,v 1.195 2003/09/02 01:14:00 mcr Exp $
*/
#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 <netinet/in.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <arpa/nameser.h> /* missing from <resolv.h> on old systems */
#include <sys/queue.h>
#include <freeswan.h>
#include <freeswan/ipsec_policy.h>
#include "kameipsec.h"
#include "constants.h"
#include "defs.h"
#include "id.h"
#include "connections.h" /* needs id.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 "adns.h" /* needs <resolv.h> */
#include "dnskey.h" /* needs keys.h and adns.h */
#include "whack.h"
static void flush_pending_by_connection(struct connection *c); /* forward */
static struct connection *connections = NULL;
/* struct host_pair: a nexus of information about a pair of hosts.
* A host is an IP address, UDP port pair. This is a debatable choice:
* - should port be considered (no choice of port in standard)?
* - should ID be considered (hard because not always known)?
* - should IP address matter on our end (we don't know our end)?
* Only oriented connections are registered.
* Unoriented connections are kept on the unoriented_connections
* linked list (using hp_next). For them, host_pair is NULL.
*/
struct host_pair {
struct {
ip_address addr;
u_int16_t port; /* host order */
} me, him;
bool initial_connection_sent;
struct connection *connections; /* connections with this pair */
struct pending *pending; /* awaiting Keying Channel */
struct host_pair *next;
};
static struct host_pair *host_pairs = NULL;
static struct connection *unoriented_connections = NULL;
/* check to see that Ids of peers match */
bool
same_peer_ids(const struct connection *c, const struct connection *d
, const struct id *his_id)
{
return same_id(&c->spd.this.id, &d->spd.this.id)
&& same_id(his_id == NULL? &c->spd.that.id : his_id, &d->spd.that.id);
}
static struct host_pair *
find_host_pair(const ip_address *myaddr, u_int16_t myport
, const ip_address *hisaddr, u_int16_t hisport)
{
struct host_pair *p, *prev;
/* default hisaddr to an appropriate any */
if (hisaddr == NULL)
hisaddr = aftoinfo(addrtypeof(myaddr))->any;
for (prev = NULL, p = host_pairs; p != NULL; prev = p, p = p->next)
{
if (sameaddr(&p->me.addr, myaddr) && p->me.port == myport
&& sameaddr(&p->him.addr, hisaddr) && p->him.port == hisport)
{
if (prev != NULL)
{
prev->next = p->next; /* remove p from list */
p->next = host_pairs; /* and stick it on front */
host_pairs = p;
}
break;
}
}
return p;
}
/* find head of list of connections with this pair of hosts */
static struct connection *
find_host_pair_connections(const ip_address *myaddr, u_int16_t myport
, const ip_address *hisaddr, u_int16_t hisport)
{
struct host_pair *hp = find_host_pair(myaddr, myport, hisaddr, hisport);
return hp == NULL? NULL : hp->connections;
}
static void
connect_to_host_pair(struct connection *c)
{
if (oriented(*c))
{
struct host_pair *hp = find_host_pair(&c->spd.this.host_addr, c->spd.this.host_port
, &c->spd.that.host_addr, c->spd.that.host_port);
if (hp == NULL)
{
/* no suitable host_pair -- build one */
hp = alloc_thing(struct host_pair, "host_pair");
hp->me.addr = c->spd.this.host_addr;
hp->me.port = c->spd.this.host_port;
hp->him.addr = c->spd.that.host_addr;
hp->him.port = c->spd.that.host_port;
hp->initial_connection_sent = FALSE;
hp->connections = NULL;
hp->pending = NULL;
hp->next = host_pairs;
host_pairs = hp;
}
c->host_pair = hp;
c->hp_next = hp->connections;
hp->connections = c;
}
else
{
/* since this connection isn't oriented, we place it
* in the unoriented_connections list instead.
*/
c->host_pair = NULL;
c->hp_next = unoriented_connections;
unoriented_connections = c;
}
}
/* find a connection by name.
* If strict, don't accept a CK_INSTANCE.
* Move the winner (if any) to the front.
* If none is found, and strict, a diagnostic is logged to whack.
*/
struct connection *
con_by_name(const char *nm, bool strict)
{
struct connection *p, *prev;
for (prev = NULL, p = connections; ; prev = p, p = p->ac_next)
{
if (p == NULL)
{
if (strict)
whack_log(RC_UNKNOWN_NAME
, "no connection named \"%s\"", nm);
break;
}
if (streq(p->name, nm)
&& (!strict || p->kind != CK_INSTANCE))
{
if (prev != NULL)
{
prev->ac_next = p->ac_next; /* remove p from list */
p->ac_next = connections; /* and stick it on front */
connections = p;
}
break;
}
}
return p;
}
void
release_connection(struct connection *c)
{
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);
}
else
{
flush_pending_by_connection(c);
delete_states_by_connection(c);
unroute_connection(c);
}
}
/* Delete a connection */
#define list_rm(etype, enext, e, ehead) { \
etype **ep; \
for (ep = &(ehead); *ep != (e); ep = &(*ep)->enext) \
passert(*ep != NULL); /* we must not come up empty-handed */ \
*ep = (e)->enext; \
}
void
delete_connection(struct connection *c)
{
struct connection *old_cur_connection
= cur_connection == c? NULL : cur_connection;
#ifdef DEBUG
lset_t old_cur_debugging = cur_debugging;
#endif
set_cur_connection(c);
/* 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)
{
plog("deleting connection \"%s\" instance with peer %s {isakmp=#%lu/ipsec=#%lu}"
, c->name
, ip_str(&c->spd.that.host_addr)
, c->newest_isakmp_sa, c->newest_ipsec_sa);
c->kind = CK_GOING_AWAY;
}
else
{
plog("deleting connection");
}
release_connection(c); /* won't delete c */
if (c->kind == CK_GROUP)
delete_group(c);
/* free up any logging resources */
perpeer_logfree(c);
/* find and delete c from connections list */
list_rm(struct connection, ac_next, c, connections);
cur_connection = old_cur_connection;
/* find and delete c from the host pair list */
if (c->host_pair == NULL)
{
list_rm(struct connection, hp_next, c, unoriented_connections);
}
else
{
struct host_pair *hp = c->host_pair;
list_rm(struct connection, hp_next, c, hp->connections);
c->host_pair = NULL; /* redundant, but safe */
/* if there are no more connections with this host_pair
* and we haven't even made an initial contact, let's delete
* this guy in case we were created by an attempted DOS attack.
*/
if (hp->connections == NULL
&& !hp->initial_connection_sent)
{
passert(hp->pending == NULL); /* ??? must deal with this! */
list_rm(struct host_pair, next, hp, host_pairs);
pfree(hp);
}
}
#ifdef DEBUG
cur_debugging = old_cur_debugging;
#endif
pfreeany(c->name);
free_id_content(&c->spd.this.id);
pfreeany(c->spd.this.updown);
free_id_content(&c->spd.that.id);
pfreeany(c->spd.that.updown);
gw_delref(&c->gw_info);
pfree(c);
}
/* Delete connections with the specified name */
void
delete_connections_by_name(const char *name, bool strict)
{
struct connection *c = con_by_name(name, strict);
for (; c != NULL; c = con_by_name(name, FALSE))
delete_connection(c);
}
void
delete_every_connection(void)
{
while (connections != NULL)
delete_connection(connections);
}
void
release_dead_interfaces(void)
{
struct host_pair *hp;
for (hp = host_pairs; hp != NULL; hp = hp->next)
{
struct connection **pp
, *p;
for (pp = &hp->connections; (p = *pp) != NULL; )
{
if (p->interface->change == IFN_DELETE)
{
/* this connection's interface is going away */
enum connection_kind k = p->kind;
release_connection(p);
if (k <= CK_PERMANENT)
{
/* The connection should have survived release:
* move it to the unoriented_connections list.
*/
passert(p == *pp);
p->interface = NULL;
*pp = p->hp_next; /* advance *pp */
p->host_pair = NULL;
p->hp_next = unoriented_connections;
unoriented_connections = p;
}
else
{
/* The connection should have vanished,
* but the previous connection remains.
*/
passert(p != *pp);
}
}
else
{
pp = &p->hp_next; /* advance pp */
}
}
}
}
/* adjust orientations of connections to reflect newly added interfaces */
void
check_orientations(void)
{
/* try to orient all the unoriented connections */
{
struct connection *c = unoriented_connections;
unoriented_connections = NULL;
while (c != NULL)
{
struct connection *nxt = c->hp_next;
(void)orient(c);
connect_to_host_pair(c);
c = nxt;
}
}
/* Check that no oriented connection has become double-oriented.
* In other words, the far side must not match one of our new interfaces.
*/
{
struct iface *i;
for (i = interfaces; i != NULL; i = i->next)
{
if (i->change == IFN_ADD)
{
struct host_pair *hp;
for (hp = host_pairs; hp != NULL; hp = hp->next)
{
if (sameaddr(&hp->him.addr, &i->addr)
&& (!no_klips || hp->him.port == pluto_port))
{
/* bad news: the whole chain of connections
* hanging off this host pair has both sides
* matching an interface.
* We'll get rid of them, using orient and
* connect_to_host_pair. But we'll be lazy
* and not ditch the host_pair itself (the
* cost of leaving it is slight and cannot
* be induced by a foe).
*/
struct connection *c = hp->connections;
hp->connections = NULL;
while (c != NULL)
{
struct connection *nxt = c->hp_next;
c->interface = NULL;
(void)orient(c);
connect_to_host_pair(c);
c = nxt;
}
}
}
}
}
}
}
static err_t
default_end(struct end *e, ip_address *dflt_nexthop)
{
err_t ugh = NULL;
const struct af_info *afi = aftoinfo(addrtypeof(&e->host_addr));
if (afi == NULL)
return "unknown address family in default_end";
/* default ID to IP (but only if not NO_IP -- WildCard) */
if (e->id.kind == ID_NONE && !isanyaddr(&e->host_addr))
{
e->id.kind = afi->id_addr;
e->id.ip_addr = e->host_addr;
}
/* default nexthop to other side */
if (isanyaddr(&e->host_nexthop))
e->host_nexthop = *dflt_nexthop;
/* default client to subnet containing only self
* XXX This may mean that the client's address family doesn't match
* tunnel_addr_family.
*/
if (!e->has_client)
ugh = addrtosubnet(&e->host_addr, &e->client);
return ugh;
}
/* Format the topology of a connection end, leaving out defaults.
* Largest left end looks like: client === host : port [ host_id ] --- hop
* Note: if that==NULL, skip nexthop
* Returns strlen of formated result (length excludes NUL at end).
*/
size_t
format_end(char *buf
, size_t buf_len
, const struct end *this
, const struct end *that
, bool is_left
, lset_t policy)
{
char client[SUBNETTOT_BUF];
const char *client_sep = "";
const char *host = NULL;
char host_space[ADDRTOT_BUF];
char host_port[sizeof(":65535")];
char host_id[IDTOA_BUF + 2];
char hop[ADDRTOT_BUF];
const char *hop_sep = "";
if (isanyaddr(&this->host_addr))
{
switch (policy & (POLICY_GROUP | POLICY_OPPO))
{
case POLICY_GROUP:
host = "%group";
break;
case POLICY_OPPO:
host = "%opportunistic";
break;
case POLICY_GROUP | POLICY_OPPO:
host = "%opportunisticgroup";
break;
default:
host = "%any";
break;
}
}
client[0] = '\0';
/* [client===] */
if (this->has_client)
{
ip_address client_net, client_mask;
networkof(&this->client, &client_net);
maskof(&this->client, &client_mask);
client_sep = "===";
if (isanyaddr(&client_net) && isanyaddr(&client_mask)
&& (policy & (POLICY_GROUP | POLICY_OPPO)))
client_sep = ""; /* boring case */
else if (subnetisnone(&this->client))
strcpy(client, "?");
else
subnettot(&this->client, 0, client, sizeof(client));
}
/* host */
if (host == NULL)
{
addrtot(&this->host_addr, 0, host_space, sizeof(host_space));
host = host_space;
}
host_port[0] = '\0';
if (this->host_port != IKE_UDP_PORT)
snprintf(host_port, sizeof(host_port), ":%u"
, this->host_port);
/* id, if different from host */
host_id[0] = '\0';
if (this->id.kind == ID_MYID)
{
strcpy(host_id, "[%myid]");
}
else if (!(this->id.kind == ID_NONE
|| (id_is_ipaddr(&this->id) && sameaddr(&this->id.ip_addr, &this->host_addr))))
{
int len = idtoa(&this->id, host_id+1, sizeof(host_id)-2);
host_id[0] = '[';
strcpy(&host_id[len < 0? (ptrdiff_t)sizeof(host_id)-2 : 1 + len], "]");
}
/* [---hop] */
hop[0] = '\0';
hop_sep = "";
if (that != NULL && !sameaddr(&this->host_nexthop, &that->host_addr))
{
addrtot(&this->host_nexthop, 0, hop, sizeof(hop));
hop_sep = "---";
}
if (is_left)
snprintf(buf, buf_len, "%s%s%s%s%s%s%s"
, client, client_sep
, host, host_port, host_id
, hop_sep, hop);
else
snprintf(buf, buf_len, "%s%s%s%s%s%s%s"
, hop, hop_sep
, host, host_port, host_id
, client_sep, client);
return strlen(buf);
}
/* format topology of a connection.
* Two symmetric ends separated by ...
*/
#define CONNECTION_BUF (2 * (END_BUF - 1) + 4)
static size_t
format_connection(char *buf, size_t buf_len
, const struct connection *c
, struct spd_route *sr)
{
size_t w = format_end(buf, buf_len, &sr->this, &sr->that, TRUE, LEMPTY);
w += snprintf(buf + w, buf_len - w, "...");
return w + format_end(buf + w, buf_len - w, &sr->that, &sr->this, FALSE, c->policy);
}
static void
unshare_connection_strings(struct connection *c)
{
c->name = clone_str(c->name, "connection name");
unshare_id_content(&c->spd.this.id);
c->spd.this.updown = clone_str(c->spd.this.updown, "updown");
unshare_id_content(&c->spd.that.id);
c->spd.that.updown = clone_str(c->spd.that.updown, "updown");
}
static void
extract_end(struct end *dst, const struct whack_end *src, const char *which)
{
/* decode id, if any */
if (src->id == NULL)
{
dst->id.kind = ID_NONE;
}
else
{
err_t ugh = atoid(src->id, &dst->id, TRUE);
if (ugh != NULL)
{
loglog(RC_BADID, "bad %s --id: %s (ignored)", which, ugh);
dst->id = empty_id; /* ignore bad one */
}
}
/* the rest is simple copying of corresponding fields */
dst->host_addr = src->host_addr;
dst->host_nexthop = src->host_nexthop;
dst->client = src->client;
dst->key_from_DNS_on_demand = src->key_from_DNS_on_demand;
dst->has_client = src->has_client;
dst->updown = src->updown;
dst->host_port = src->host_port;
}
static bool
check_connection_end(const struct whack_end *this, const struct whack_end *that
, const struct whack_message *wm)
{
if (wm->addr_family != addrtypeof(&this->host_addr)
|| wm->addr_family != addrtypeof(&this->host_nexthop)
|| (this->has_client? wm->tunnel_addr_family : wm->addr_family)
!= subnettypeof(&this->client)
|| subnettypeof(&this->client) != subnettypeof(&that->client))
{
/* this should have been diagnosed by whack, so we need not be clear
* !!! overloaded use of RC_CLASH
*/
loglog(RC_CLASH, "address family inconsistency in connection");
return FALSE;
}
if (isanyaddr(&that->host_addr))
{
/* other side is wildcard: we must check if other conditions met */
if (isanyaddr(&this->host_addr))
{
loglog(RC_ORIENT, "connection must specify host IP address for our side");
return FALSE;
}
else if (!NEVER_NEGOTIATE(wm->policy))
{
/* check that all RW IKE policies agree because we must implement
* them before the correct connection is known.
* We cannot enforce this for other non-RW connections because
* differentiation is possible when a command specifies which
* to initiate.
*/
const struct connection *c = NULL;
c = find_host_pair_connections(&this->host_addr
, this->host_port, (const ip_address *)NULL, that->host_port);
for (; c != NULL; c = c->hp_next)
{
if (!NEVER_NEGOTIATE(c->policy)
&& ((c->policy ^ wm->policy) & (POLICY_PSK | POLICY_RSASIG)))
{
loglog(RC_CLASH
, "authentication method disagrees with \"%s\", which is also for an unspecified peer"
, c->name);
return FALSE;
}
}
}
}
return TRUE; /* happy */
}
struct connection *
find_connection_by_reqid(uint32_t reqid)
{
struct connection *c;
reqid &= ~3;
for (c = connections; c != NULL; c = c->ac_next)
{
if (c->spd.reqid == reqid)
return c;
}
return NULL;
}
static uint32_t
gen_reqid(void)
{
uint32_t start;
static uint32_t reqid = IPSEC_MANUAL_REQID_MAX & ~3;
start = reqid;
do {
reqid += 4;
if (reqid == 0)
reqid = (IPSEC_MANUAL_REQID_MAX & ~3) + 4;
if (!find_connection_by_reqid(reqid))
return reqid;
} while (reqid != start);
exit_log("unable to allocate reqid");
}
void
add_connection(const struct whack_message *wm)
{
if (con_by_name(wm->name, FALSE) != NULL)
{
loglog(RC_DUPNAME, "attempt to redefine connection \"%s\"", wm->name);
}
else if (check_connection_end(&wm->right, &wm->left, wm)
&& check_connection_end(&wm->left, &wm->right, wm))
{
struct connection *c = alloc_thing(struct connection, "struct connection");
c->name = wm->name;
c->policy = wm->policy;
if ((c->policy & POLICY_COMPRESS) && !can_do_IPcomp)
loglog(RC_COMMENT
, "ignoring --compress in \"%s\" because KLIPS is not configured to do IPCOMP"
, c->name);
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->addr_family = wm->addr_family;
c->tunnel_addr_family = wm->tunnel_addr_family;
extract_end(&c->spd.this, &wm->left, "left");
extract_end(&c->spd.that, &wm->right, "right");
default_end(&c->spd.this, &c->spd.that.host_addr);
default_end(&c->spd.that, &c->spd.this.host_addr);
/* force any wildcard host IP address to that end */
if (isanyaddr(&c->spd.this.host_addr))
{
struct end t = c->spd.this;
c->spd.this = c->spd.that;
c->spd.that = t;
}
c->spd.next = NULL;
c->spd.reqid = gen_reqid();
/* set internal fields */
c->instance_serial = 0;
c->ac_next = connections;
connections = c;
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;
if (c->policy & POLICY_GROUP)
{
c->kind = CK_GROUP;
add_group(c);
}
else if (isanyaddr(&c->spd.that.host_addr) && !NEVER_NEGOTIATE(c->policy))
{
/* Opportunistic or Road Warrior */
c->kind = CK_TEMPLATE;
}
else
{
c->kind = CK_PERMANENT;
}
set_policy_prio(c); /* must be after kind is set */
#ifdef DEBUG
c->extra_debugging = wm->debugging;
#endif
c->gw_info = NULL;
unshare_connection_strings(c);
(void)orient(c);
connect_to_host_pair(c);
/* log all about this connection */
plog("added connection description \"%s\"", c->name);
DBG(DBG_CONTROL,
char topo[CONNECTION_BUF];
(void) format_connection(topo, sizeof(topo), c, &c->spd);
DBG_log("%s", topo);
/* Make sure that address families can be correctly inferred
* from printed ends.
*/
passert(c->addr_family == addrtypeof(&c->spd.this.host_addr)
&& c->addr_family == addrtypeof(&c->spd.this.host_nexthop)
&& (c->spd.this.has_client? c->tunnel_addr_family : c->addr_family)
== subnettypeof(&c->spd.this.client)
&& c->addr_family == addrtypeof(&c->spd.that.host_addr)
&& c->addr_family == addrtypeof(&c->spd.that.host_nexthop)
&& (c->spd.that.has_client? c->tunnel_addr_family : c->addr_family)
== subnettypeof(&c->spd.that.client));
DBG_log("ike_life: %lus; ipsec_life: %lus; rekey_margin: %lus;"
" rekey_fuzz: %lu%%; keyingtries: %lu; policy: %s"
, (unsigned long) c->sa_ike_life_seconds
, (unsigned long) c->sa_ipsec_life_seconds
, (unsigned long) c->sa_rekey_margin
, (unsigned long) c->sa_rekey_fuzz
, (unsigned long) c->sa_keying_tries
, prettypolicy(c->policy));
);
}
}
/* Derive a template connection from a group connection and target.
* Similar to instantiate(). Happens at whack --listen.
* Returns name of new connection. May be NULL.
* Caller is responsible for pfreeing.
*/
char *
add_group_instance(struct connection *group, const ip_subnet *target)
{
char namebuf[100]
, targetbuf[SUBNETTOT_BUF];
struct connection *t;
char *name = NULL;
passert(group->kind == CK_GROUP);
passert(oriented(*group));
/* manufacture a unique name for this template */
subnettot(target, 0, targetbuf, sizeof(targetbuf));
snprintf(namebuf, sizeof(namebuf), "%s#%s", group->name, targetbuf);
if (con_by_name(namebuf, FALSE) != NULL)
{
loglog(RC_DUPNAME, "group name + target yields duplicate name \"%s\""
, namebuf);
}
else
{
t = clone_thing(*group, "group instance");
t->name = namebuf;
unshare_connection_strings(t);
name = clone_str(t->name, "group instance name");
t->spd.that.client = *target;
t->policy &= ~(POLICY_GROUP | POLICY_GROUTED);
t->kind = isanyaddr(&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 = gen_reqid();
/* add to connections list */
t->ac_next = connections;
connections = t;
/* same host_pair as parent: stick after parent on list */
group->hp_next = t;
/* route if group is routed */
if (group->policy & POLICY_GROUTED)
{
if (!trap_connection(t))
whack_log(RC_ROUTE, "could not route");
}
}
return name;
}
/* an old target has disappeared for a group: delete instance */
void
remove_group_instance(const struct connection *group USED_BY_DEBUG
, const char *name)
{
passert(group->kind == CK_GROUP);
passert(oriented(*group));
delete_connections_by_name(name, FALSE);
}
/* Common part of instantiating a Road Warrior or Opportunistic connection.
* his_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 his_id.
* If his_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.
*/
static struct connection *
instantiate(struct connection *c, const ip_address *him
, const struct id *his_id)
{
struct connection *d;
passert(c->kind == CK_TEMPLATE);
passert(c->spd.next == NULL);
c->instance_serial++;
d = clone_thing(*c, "temporary connection");
if (his_id != NULL)
{
passert(d->spd.that.id.kind == ID_NONE || same_id(&d->spd.that.id, his_id));
d->spd.that.id = *his_id;
}
unshare_connection_strings(d);
d->kind = CK_INSTANCE;
passert(oriented(*d));
d->spd.that.host_addr = *him;
default_end(&d->spd.that, &d->spd.this.host_addr);
/* 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 him.
* (whack will not allow nexthop to be elided in RW case.)
*/
default_end(&d->spd.this, &d->spd.that.host_addr);
d->spd.next = NULL;
d->spd.reqid = gen_reqid();
/* 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;
connect_to_host_pair(d);
return d;
}
struct connection *
rw_instantiate(struct connection *c
, const ip_address *him
, const struct id *his_id)
{
struct connection *d = instantiate(c, him, his_id);
if (d->policy & POLICY_OPPO)
{
/* 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 = *aftoinfo(subnettypeof(&d->spd.that.client))->none;
}
DBG(DBG_CONTROL
, DBG_log("instantiated \"%s\" for %s" , d->name, ip_str(him)));
return d;
}
struct connection *
oppo_instantiate(struct connection *c
, const ip_address *him
, const struct id *his_id
, struct gw_info *gw
, const ip_address *our_client USED_BY_DEBUG
, const ip_address *peer_client)
{
struct connection *d = instantiate(c, him, his_id);
passert(d->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 the required client is within that subnet.
*/
passert(addrinsubnet(our_client, &d->spd.this.client));
happy(addrtosubnet(our_client, &d->spd.this.client));
}
else
{
/* there was no client in the abstract connection
* so we demand that the required client be the host
*/
passert(sameaddr(our_client, &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_OPPO)
&& addrinsubnet(peer_client, &d->spd.that.client));
happy(addrtosubnet(peer_client, &d->spd.that.client));
if (sameaddr(peer_client, &d->spd.that.host_addr))
d->spd.that.has_client = FALSE;
passert(d->gw_info == NULL);
gw_addref(gw);
d->gw_info = gw;
/* 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;
DBG(DBG_CONTROL,
char topo[CONNECTION_BUF];
(void) format_connection(topo, sizeof(topo), d, &d->spd);
DBG_log("instantiated \"%s\": %s", d->name, topo);
);
return d;
}
/* priority formatting */
void
fmt_policy_prio(policy_prio_t pp, char buf[POLICY_PRIO_BUF])
{
if (pp == BOTTOM_PRIO)
snprintf(buf, POLICY_PRIO_BUF, "0");
else
snprintf(buf, POLICY_PRIO_BUF, "%lu,%lu"
, pp>>16, (pp & ~(~(policy_prio_t)0 << 16)) >> 8);
}
/* 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 ["===" hisclient] '\0'
*/
static size_t
fmt_client(const ip_subnet *client, const ip_address *gw, const char *prefix, char buf[ADDRTOT_BUF])
{
if (subnetisaddr(client, gw))
{
buf[0] = '\0'; /* compact denotation for "self" */
}
else
{
char *ap;
strcpy(buf, prefix);
ap = buf + strlen(prefix);
if (subnetisnone(client))
strcpy(ap, "?"); /* unknown */
else
subnettot(client, 0, ap, SUBNETTOT_BUF);
}
return strlen(buf);
}
void
fmt_conn_instance(const struct connection *c, char buf[CONN_INST_BUF])
{
char *p = buf;
*p = '\0';
if (c->kind == CK_INSTANCE)
{
if (c->instance_serial != 0)
{
snprintf(p, CONN_INST_BUF, "[%lu]", c->instance_serial);
p += strlen(p);
}
if (c->policy & POLICY_OPPO)
{
size_t w = fmt_client(&c->spd.this.client, &c->spd.this.host_addr, " ", p);
p += w;
strcpy(p, w == 0? " ..." : "=== ...");
p += strlen(p);
addrtot(&c->spd.that.host_addr, 0, p, ADDRTOT_BUF);
p += strlen(p);
(void) fmt_client(&c->spd.that.client, &c->spd.that.host_addr, "===", p);
}
else
{
*p++ = ' ';
addrtot(&c->spd.that.host_addr, 0, p, ADDRTOT_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_address *our_client,
const ip_address *peer_client)
{
struct connection *c = connections
, *best = NULL;
policy_prio_t best_prio = BOTTOM_PRIO;
struct spd_route *sr;
struct spd_route *best_sr;
passert(!isanyaddr(our_client) && !isanyaddr(peer_client));
#ifdef DEBUG
if (DBGP(DBG_CONTROL))
{
char ocb[ADDRTOT_BUF], pcb[ADDRTOT_BUF];
addrtot(our_client, 0, ocb, sizeof(ocb));
addrtot(peer_client, 0, pcb, sizeof(pcb));
DBG_log("find_connection: looking for policy for connection: %s -> %s"
, ocb, pcb);
}
#endif /* DEBUG */
for (c = connections; c != NULL; c = c->ac_next)
{
if (c->kind == CK_GROUP)
continue;
for (sr = &c->spd; best!=c && sr; sr = sr->next)
{
if ((routed(sr->routing) || c->instance_initiation_ok)
&& addrinsubnet(our_client, &sr->this.client)
&& addrinsubnet(peer_client, &sr->that.client))
{
char cib[CONN_INST_BUF];
char cib2[CONN_INST_BUF];
policy_prio_t prio = c->prio + (c->kind == CK_INSTANCE);
#ifdef DEBUG
if (DBGP(DBG_CONTROL|DBG_CONTROLMORE))
{
char c_ocb[SUBNETTOT_BUF], c_pcb[SUBNETTOT_BUF];
subnettot(&c->spd.this.client, 0, c_ocb, sizeof(c_ocb));
subnettot(&c->spd.that.client, 0, c_pcb, sizeof(c_pcb));
DBG_log("find_connection: conn \"%s\"%s has compatible peers: %s->%s (pri: %ld)"
, c->name
, (fmt_conn_instance(c, cib), cib)
, c_ocb, c_pcb, prio);
}
#endif /* DEBUG */
if (best == NULL)
{
best = c;
best_sr = sr;
best_prio = prio;
}
DBG(DBG_CONTROLMORE,
DBG_log("find_connection: comparing best \"%s\"%s [pri:%ld]{%p} (child %s) to \"%s\"%s [pri:%ld]{%p} (child %s)"
, best->name
, (fmt_conn_instance(best, cib), cib)
, best_prio
, best
, (best->policy_next ? best->policy_next->name : "none")
, c->name
, (fmt_conn_instance(c, cib2), cib2)
, prio
, c
, (c->policy_next ? c->policy_next->name : "none")));
if (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;
#ifdef DEBUG
if (DBGP(DBG_CONTROL))
{
if (best)
{
char cib[CONN_INST_BUF];
DBG_log("find_connection: concluding with \"%s\"%s [pri:%ld]{%p} kind=%s"
, best->name
, (fmt_conn_instance(best, cib), cib)
, best_prio
, best
, enum_name(&connection_kind_names, best->kind));
} else {
DBG_log("find_connection: concluding with empty");
}
}
#endif /* DEBUG */
return best;
}
/* Find and instantiate a connection for an outgoing Opportunistic connection.
* We've already discovered its gateway.
* We look for a the 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(struct gw_info *gw
,const ip_address *our_client
,const ip_address *peer_client)
{
struct iface *p;
struct connection *best = NULL;
struct spd_route *sr, *bestsr;
char ocb[ADDRTOT_BUF], pcb[ADDRTOT_BUF];
addrtot(our_client, 0, ocb, sizeof(ocb));
addrtot(peer_client, 0, pcb, sizeof(pcb));
passert(!isanyaddr(our_client) && !isanyaddr(peer_client));
/* We don't know his ID yet, so gw id must be an ipaddr */
passert(gw->key != NULL);
passert(id_is_ipaddr(&gw->gw_id));
/* for each of our addresses... */
for (p = interfaces; p != NULL; p = p->next)
{
/* 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).
*/
struct connection *c = find_host_pair_connections(&p->addr
, pluto_port, (ip_address *)NULL, pluto_port);
for (; c != NULL; c = c->hp_next)
{
DBG(DBG_OPPO,
DBG_log("checking %s", c->name));
if (c->kind == CK_GROUP)
{
continue;
}
for (sr = &c->spd; best!=c && sr; sr = sr->next)
{
if (routed(sr->routing)
&& addrinsubnet(our_client, &sr->this.client)
&& addrinsubnet(peer_client, &sr->that.client))
{
if (best == NULL)
{
best = c;
break;
}
DBG(DBG_OPPO,
DBG_log("comparing best %s to %s"
, best->name, c->name));
for (bestsr = &best->spd; best!=c && bestsr; bestsr=bestsr->next)
{
if (!subnetinsubnet(&bestsr->this.client, &sr->this.client)
|| (samesubnet(&bestsr->this.client, &sr->this.client)
&& !subnetinsubnet(&bestsr->that.client
, &sr->that.client)))
{
best = c;
}
}
}
}
}
}
if (best == NULL
|| NEVER_NEGOTIATE(best->policy)
|| (best->policy & POLICY_OPPO) == LEMPTY
|| best->kind != CK_TEMPLATE)
return NULL;
else
return oppo_instantiate(best, &gw->gw_id.ip_addr, NULL, gw
, our_client, peer_client);
}
bool
orient(struct connection *c)
{
struct spd_route *sr;
if (!oriented(*c))
{
struct iface *p;
for (sr = &c->spd; sr; sr = sr->next)
{
/* Note: this loop does not stop when it finds a match:
* it continues checking to catch any ambiguity.
*/
for (p = interfaces; p != NULL; p = p->next)
{
for (;;)
{
/* check if this interface matches this end */
if (sameaddr(&sr->this.host_addr, &p->addr)
&& (!no_klips || sr->this.host_port == pluto_port))
{
if (oriented(*c))
{
if (c->interface == p)
loglog(RC_LOG_SERIOUS
, "both sides of \"%s\" are our interface %s!"
, c->name, p->rname);
else
loglog(RC_LOG_SERIOUS, "two interfaces match \"%s\" (%s, %s)"
, c->name, c->interface->rname, p->rname);
c->interface = NULL; /* withdraw orientation */
return FALSE;
}
c->interface = p;
}
/* done with this interface if it doesn't match that end */
if (!(sameaddr(&sr->that.host_addr, &p->addr)
&& (!no_klips || sr->that.host_port == pluto_port)))
break;
/* swap ends and try again.
* It is a little tricky to see that this loop will stop.
* Only continue if the far side matches.
* If both sides match, there is an error-out.
*/
{
struct end t = sr->this;
sr->this = sr->that;
sr->that = t;
}
}
}
}
}
return oriented(*c);
}
void
initiate_connection(const char *name, int whackfd)
{
struct connection *c = con_by_name(name, TRUE);
if (c != NULL)
{
set_cur_connection(c);
if (!oriented(*c))
{
loglog(RC_ORIENT, "we have no ipsecN interface for either end of this connection");
}
else if (NEVER_NEGOTIATE(c->policy))
{
loglog(RC_INITSHUNT
, "cannot initiate an authby=never connection");
}
else if (c->kind != CK_PERMANENT)
{
loglog(RC_NOPEERIP, "cannot initiate connection without knowing peer IP address");
}
else
{
/* We will only request an IPsec SA if policy isn't empty
* (ignoring Main Mode items).
* This is a fudge, but not yet important.
* If we are to proceed asynchronously, whackfd will be NULL_FD.
*/
c->policy |= POLICY_UP;
ipsecdoi_initiate(whackfd, c, c->policy, 1, SOS_NOBODY);
whackfd = NULL_FD; /* protect from close */
}
reset_cur_connection();
}
close_any(whackfd);
}
/* (Possibly) Opportunistic Initiation:
* Knowing clients (single IP addresses), try to build an tunnel.
* This may involve discovering a gateway and instantiating an
* Opportunistic connection. Called when a packet is caught by
* a %trap, or when whack --oppohere --oppothere is used.
* It may turn out that an existing or non-opporunistic connnection
* can handle the traffic.
*
* Most of the code will be restarted if an ADNS request is made
* to discover the gateway. The only difference between the first
* and second entry is whether gateways_from_dns is NULL or not.
* initiate_opportunistic: initial entrypoint
* continue_oppo: where we pickup when ADNS result arrives
* initiate_opportunistic_body: main body shared by above routines
* cannot_oppo: a helper function to log a diagnostic
* This structure repeats a lot of code when the ADNS result arrives.
* This seems like a waste, but anything learned the first time through
* may no longer be true!
*
* After the first IKE message is sent, the regular state machinery
* carries negotiation forward.
*/
enum find_oppo_step {
fos_start,
fos_myid_ip_txt,
fos_myid_hostname_txt,
fos_myid_ip_key,
fos_myid_hostname_key,
fos_our_client,
fos_our_txt,
#ifdef USE_KEYRR
fos_our_key,
#endif /* USE_KEYRR */
fos_his_client,
fos_done
};
#ifdef DEBUG
static const char *const oppo_step_name[] = {
"fos_start",
"fos_myid_ip_txt",
"fos_myid_hostname_txt",
"fos_myid_ip_key",
"fos_myid_hostname_key",
"fos_our_client",
"fos_our_txt",
#ifdef USE_KEYRR
"fos_our_key",
#endif /* USE_KEYRR */
"fos_his_client",
"fos_done"
};
#endif /* DEBUG */
struct find_oppo_bundle {
enum find_oppo_step step;
err_t want;
bool failure_ok; /* if true, continue_oppo should not die on DNS failure */
ip_address our_client; /* not pointer! */
ip_address peer_client;
bool held;
policy_prio_t policy_prio;
ipsec_spi_t failure_shunt; /* in host order! 0 for delete. */
int whackfd;
};
struct find_oppo_continuation {
struct adns_continuation ac; /* common prefix */
struct find_oppo_bundle b;
};
static void
cannot_oppo(struct connection *c
, struct find_oppo_bundle *b
, err_t ugh)
{
char pcb[ADDRTOT_BUF];
char ocb[ADDRTOT_BUF];
addrtot(&b->peer_client, 0, pcb, sizeof(pcb));
addrtot(&b->our_client, 0, ocb, sizeof(ocb));
DBG(DBG_DNS | DBG_OPPO, DBG_log("Can't Opportunistically initiate for %s to %s: %s"
, ocb, pcb, ugh));
whack_log(RC_OPPOFAILURE
, "Can't Opportunistically initiate for %s to %s: %s"
, ocb, pcb, ugh);
if (c != NULL && c->policy_next != NULL)
{
/* there is some policy that comes afterwards */
struct spd_route *shunt_spd;
struct connection *nc = c->policy_next;
struct state *st;
passert(c->kind == CK_TEMPLATE);
passert(c->policy_next->kind == CK_PERMANENT);
DBG(DBG_OPPO, DBG_log("OE failed for %s to %s, but %s overrides shunt"
, ocb, pcb, c->policy_next->name));
/*
* okay, here we need add to the "next" policy, which is ought
* to be an instance.
* We will add another entry to the spd_route list for the specific
* situation that we have.
*/
shunt_spd = clone_thing(nc->spd, "shunt eroute policy");
shunt_spd->next = nc->spd.next;
nc->spd.next = shunt_spd;
happy(addrtosubnet(&b->peer_client, &shunt_spd->that.client));
if (sameaddr(&b->peer_client, &shunt_spd->that.host_addr))
shunt_spd->that.has_client = FALSE;
/*
* override the tunnel destination with the one from the secondaried
* policy
*/
shunt_spd->that.host_addr = nc->spd.that.host_addr;
/* now, lookup the state, and poke it up.
*/
st = state_with_serialno(nc->newest_ipsec_sa);
/* XXX what to do if the IPSEC SA has died? */
passert(st != NULL);
/* link the new connection instance to the state's list of
* connections
*/
DBG(DBG_OPPO, DBG_log("installing state: %ld for %s to %s"
, nc->newest_ipsec_sa
, ocb, pcb));
#ifdef DEBUG
if (DBGP(DBG_OPPO | DBG_CONTROLMORE))
{
char state_buf[LOG_WIDTH];
char state_buf2[LOG_WIDTH];
time_t n = now();
fmt_state(st, n, state_buf, sizeof(state_buf)
, state_buf2, sizeof(state_buf2));
DBG_log("cannot_oppo, failure SA1: %s", state_buf);
DBG_log("cannot_oppo, failure SA2: %s", state_buf2);
}
#endif /* DEBUG */
if (!route_and_eroute(c, shunt_spd, st))
{
whack_log(RC_OPPOFAILURE
, "failed to instantiate shunt policy %s for %s to %s"
, c->name
, ocb, pcb);
}
return;
}
#ifdef KLIPS
if (b->held)
{
/* Replace HOLD with b->failure_shunt.
* If no b->failure_shunt specified, use SPI_PASS -- THIS MAY CHANGE.
*/
if (b->failure_shunt == 0)
{
DBG(DBG_OPPO, DBG_log("no explicit failure shunt for %s to %s; installing %%pass"
, ocb, pcb));
}
(void) replace_bare_shunt(&b->our_client, &b->peer_client
, b->policy_prio
, b->failure_shunt
, b->failure_shunt != 0
, ugh);
}
#endif
}
static void initiate_opportunistic_body(struct find_oppo_bundle *b
, struct adns_continuation *ac, err_t ac_ugh); /* forward */
void
initiate_opportunistic(const ip_address *our_client
, const ip_address *peer_client
, bool held
, int whackfd)
{
struct find_oppo_bundle b;
b.want = (whackfd == NULL_FD ? "whack" : "acquire");
b.failure_ok = FALSE;
b.our_client = *our_client;
b.peer_client = *peer_client;
b.held = held;
b.policy_prio = BOTTOM_PRIO;
b.failure_shunt = 0;
b.whackfd = whackfd;
b.step = fos_start;
initiate_opportunistic_body(&b, NULL, NULL);
}
static void
continue_oppo(struct adns_continuation *acr, err_t ugh)
{
struct find_oppo_continuation *cr = (void *)acr; /* inherit, damn you! */
struct connection *c;
bool was_held = cr->b.held;
int whackfd = cr->b.whackfd;
/* note: cr->id has no resources; cr->sgw_id is id_none:
* neither need freeing.
*/
whack_log_fd = whackfd;
#ifdef KLIPS
/* Discover and record whether %hold has gone away.
* This could have happened while we were awaiting DNS.
* We must check BEFORE any call to cannot_oppo.
*/
if (was_held)
cr->b.held = has_bare_hold(&cr->b.our_client, &cr->b.peer_client);
#endif
#ifdef DEBUG
/* if we're going to ignore the error, at least note it in debugging log */
if (cr->b.failure_ok && ugh != NULL)
{
DBG(DBG_CONTROL | DBG_DNS,
{
char ocb[ADDRTOT_BUF];
char pcb[ADDRTOT_BUF];
addrtot(&cr->b.our_client, 0, ocb, sizeof(ocb));
addrtot(&cr->b.peer_client, 0, pcb, sizeof(pcb));
DBG_log("continuing from failed DNS lookup for %s, %s to %s: %s"
, cr->b.want, ocb, pcb, ugh);
});
}
#endif
if (!cr->b.failure_ok && ugh != NULL)
{
c = find_connection_for_clients(NULL, &cr->b.our_client, &cr->b.peer_client);
cannot_oppo(c, &cr->b
, builddiag("%s: %s", cr->b.want, ugh));
}
else if (was_held && !cr->b.held)
{
/* was_held indicates we were started due to a %trap firing
* (as opposed to a "whack --oppohere --oppothere").
* Since the %hold has gone, we can assume that somebody else
* has beaten us to the punch. We can go home. But lets log it.
*/
char ocb[ADDRTOT_BUF];
char pcb[ADDRTOT_BUF];
addrtot(&cr->b.our_client, 0, ocb, sizeof(ocb));
addrtot(&cr->b.peer_client, 0, pcb, sizeof(pcb));
loglog(RC_COMMENT
, "%%hold otherwise handled during DNS lookup for Opportunistic Initiation for %s to %s"
, ocb, pcb);
}
else
{
initiate_opportunistic_body(&cr->b, &cr->ac, ugh);
whackfd = NULL_FD; /* was handed off */
}
whack_log_fd = NULL_FD;
close_any(whackfd);
}
#ifdef USE_KEYRR
static err_t
check_key_recs(enum myid_state try_state
, const struct connection *c
, struct adns_continuation *ac)
{
/* Check if KEY lookup yielded good results.
* Looking up based on our ID. Used if
* client is ourself, or if TXT had no public key.
* Note: if c is different this time, there is
* a chance that we did the wrong query.
* If so, treat as a kind of failure.
*/
enum myid_state old_myid_state = myid_state;
const struct RSA_private_key *our_RSA_pri;
err_t ugh = NULL;
myid_state = try_state;
if (old_myid_state != myid_state
&& old_myid_state == MYID_SPECIFIED)
{
ugh = "%myid was specified while we were guessing";
}
else if ((our_RSA_pri = get_RSA_private_key(c)) == NULL)
{
ugh = "we don't know our own RSA key";
}
else if (!same_id(&ac->id, &c->spd.this.id))
{
ugh = "our ID changed underfoot";
}
else
{
/* Similar to code in RSA_check_signature
* for checking the other side.
*/
struct pubkey_list *kr;
ugh = "no KEY RR found for us";
for (kr = ac->keys_from_dns; kr != NULL; kr = kr->next)
{
ugh = "all our KEY RRs have the wrong public key";
if (kr->key->alg == PUBKEY_ALG_RSA
&& same_RSA_public_key(&our_RSA_pri->pub, &kr->key->u.rsa))
{
ugh = NULL; /* good! */
break;
}
}
}
if (ugh != NULL)
myid_state = old_myid_state;
return ugh;
}
#endif /* USE_KEYRR */
static err_t
check_txt_recs(enum myid_state try_state
, const struct connection *c
, struct adns_continuation *ac)
{
/* Check if TXT lookup yielded good results.
* Looking up based on our ID. Used if
* client is ourself, or if TXT had no public key.
* Note: if c is different this time, there is
* a chance that we did the wrong query.
* If so, treat as a kind of failure.
*/
enum myid_state old_myid_state = myid_state;
const struct RSA_private_key *our_RSA_pri;
err_t ugh = NULL;
myid_state = try_state;
if (old_myid_state != myid_state
&& old_myid_state == MYID_SPECIFIED)
{
ugh = "%myid was specified while we were guessing";
}
else if ((our_RSA_pri = get_RSA_private_key(c)) == NULL)
{
ugh = "we don't know our own RSA key";
}
else if (!same_id(&ac->id, &c->spd.this.id))
{
ugh = "our ID changed underfoot";
}
else
{
/* Similar to code in RSA_check_signature
* for checking the other side.
*/
struct gw_info *gwp;
ugh = "no TXT RR found for us";
for (gwp = ac->gateways_from_dns; gwp != NULL; gwp = gwp->next)
{
ugh = "all our TXT RRs have the wrong public key";
if (gwp->key->alg == PUBKEY_ALG_RSA
&& same_RSA_public_key(&our_RSA_pri->pub, &gwp->key->u.rsa))
{
ugh = NULL; /* good! */
break;
}
}
}
if (ugh != NULL)
myid_state = old_myid_state;
return ugh;
}
/* note: gateways_from_dns must be NULL iff this is the first call */
static void
initiate_opportunistic_body(struct find_oppo_bundle *b
, struct adns_continuation *ac
, err_t ac_ugh)
{
struct connection *c;
struct spd_route *sr;
/* What connection shall we use?
* First try for one that explicitly handles the clients.
*/
DBG(DBG_CONTROL,
{
char ours[ADDRTOT_BUF];
char his[ADDRTOT_BUF];
addrtot(&b->our_client, 0, ours, sizeof(ours));
addrtot(&b->peer_client, 0, his, sizeof(his));
DBG_log("initiate on demand from %s to %s state: %s because: %s"
, ours, his, oppo_step_name[b->step], b->want);
});
if (isanyaddr(&b->our_client) || isanyaddr(&b->peer_client))
{
cannot_oppo(NULL, b, "impossible IP address");
}
else if ((c = find_connection_for_clients(&sr
, &b->our_client
, &b->peer_client)) == NULL)
{
/* No connection explicitly handles the clients and there
* are no Opportunistic connections -- whine and give up.
* The failure policy cannot be gotten from a connection; we pick %pass.
*/
cannot_oppo(NULL, b, "no routed Opportunistic template covers this pair");
}
else if (c->kind != CK_TEMPLATE)
{
/* We've found a connection that can serve.
* Do we have to initiate it?
* Not if there is currently an IPSEC SA.
* But if there is an IPSEC SA, then KLIPS would not
* have generated the acquire. So we assume that there isn't one.
* This may be redundant if a non-opportunistic
* negotiation is already being attempted.
*/
/* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
if(c->kind == CK_INSTANCE)
{
char cib[CONN_INST_BUF];
/* there is already an instance being negotiated, no nothing */
DBG(DBG_CONTROL, DBG_log("found existing instance \"%s\"%s, rekeying it"
, c->name
, (fmt_conn_instance(c, cib), cib)));
/* XXX-mcr - return; */
}
/* otherwise, there is some kind of static conn that can handle
* this connection, so we initiate it */
#ifdef KLIPS
if (b->held)
{
/* what should we do on failure? */
(void) assign_hold(c, sr, &b->our_client, &b->peer_client);
}
#endif
ipsecdoi_initiate(b->whackfd, c, c->policy, 1, SOS_NOBODY);
b->whackfd = NULL_FD; /* protect from close */
}
else
{
/* We are handling an opportunistic situation.
* This involves several DNS lookup steps that require suspension.
* Note: many facts might change while we're suspended.
* Here be dragons.
*
* The first chunk of code handles the result of the previous
* DNS query (if any). It also selects the kind of the next step.
* The second chunk initiates the next DNS query (if any).
*/
enum find_oppo_step next_step;
err_t ugh = ac_ugh;
char mycredentialstr[IDTOA_BUF];
char cib[CONN_INST_BUF];
DBG(DBG_CONTROL, DBG_log("creating new instance from \"%s\"%s"
, c->name
, (fmt_conn_instance(c, cib), cib)));
idtoa(&sr->this.id, mycredentialstr, sizeof(mycredentialstr));
passert(c->policy & POLICY_OPPO); /* can't initiate Road Warrior connections */
/* handle any DNS answer; select next step */
switch (b->step)
{
case fos_start:
/* just starting out: select first query step */
next_step = fos_myid_ip_txt;
break;
case fos_myid_ip_txt: /* TXT for our default IP address as %myid */
ugh = check_txt_recs(MYID_IP, c, ac);
if (ugh != NULL)
{
/* cannot use our IP as OE identitiy for initiation */
DBG(DBG_OPPO, DBG_log("can not use our IP (%s:TXT) as identity: %s"
, myid_str[MYID_IP]
, ugh));
if (!logged_myid_ip_txt_warning)
{
loglog(RC_LOG_SERIOUS
, "can not use our IP (%s:TXT) as identity: %s"
, myid_str[MYID_IP]
, ugh);
logged_myid_ip_txt_warning = TRUE;
}
next_step = fos_myid_hostname_txt;
ugh = NULL; /* failure can be recovered from */
}
else
{
/* we can use our IP as OE identity for initiation */
if (!logged_myid_ip_txt_warning)
{
loglog(RC_LOG_SERIOUS
, "using our IP (%s:TXT) as identity!"
, myid_str[MYID_IP]);
logged_myid_ip_txt_warning = TRUE;
}
next_step = fos_our_client;
}
break;
case fos_myid_hostname_txt: /* TXT for our hostname as %myid */
ugh = check_txt_recs(MYID_HOSTNAME, c, ac);
if (ugh != NULL)
{
/* cannot use our hostname as OE identitiy for initiation */
DBG(DBG_OPPO, DBG_log("can not use our hostname (%s:TXT) as identity: %s"
, myid_str[MYID_HOSTNAME]
, ugh));
if (!logged_myid_fqdn_txt_warning)
{
loglog(RC_LOG_SERIOUS
, "can not use our hostname (%s:TXT) as identity: %s"
, myid_str[MYID_HOSTNAME]
, ugh);
logged_myid_fqdn_txt_warning = TRUE;
}
#ifdef USE_KEYRR
next_step = fos_myid_ip_key;
ugh = NULL; /* failure can be recovered from */
#endif
}
else
{
/* we can use our hostname as OE identity for initiation */
if (!logged_myid_fqdn_txt_warning)
{
loglog(RC_LOG_SERIOUS
, "using our hostname (%s:TXT) as identity!"
, myid_str[MYID_HOSTNAME]);
logged_myid_fqdn_txt_warning = TRUE;
}
next_step = fos_our_client;
}
break;
#ifdef USE_KEYRR
case fos_myid_ip_key: /* KEY for our default IP address as %myid */
ugh = check_key_recs(MYID_IP, c, ac);
if (ugh != NULL)
{
/* cannot use our IP as OE identitiy for initiation */
DBG(DBG_OPPO, DBG_log("can not use our IP (%s:KEY) as identity: %s"
, myid_str[MYID_IP]
, ugh));
if (!logged_myid_ip_key_warning)
{
loglog(RC_LOG_SERIOUS
, "can not use our IP (%s:KEY) as identity: %s"
, myid_str[MYID_IP]
, ugh);
logged_myid_ip_key_warning = TRUE;
}
next_step = fos_myid_hostname_key;
ugh = NULL; /* failure can be recovered from */
}
else
{
/* we can use our IP as OE identity for initiation */
if (!logged_myid_ip_key_warning)
{
loglog(RC_LOG_SERIOUS
, "using our IP (%s:KEY) as identity!"
, myid_str[MYID_IP]);
logged_myid_ip_key_warning = TRUE;
}
next_step = fos_our_client;
}
break;
case fos_myid_hostname_key: /* KEY for our hostname as %myid */
ugh = check_key_recs(MYID_HOSTNAME, c, ac);
if (ugh != NULL)
{
/* cannot use our IP as OE identitiy for initiation */
DBG(DBG_OPPO, DBG_log("can not use our hostname (%s:KEY) as identity: %s"
, myid_str[MYID_HOSTNAME]
, ugh));
if (!logged_myid_fqdn_key_warning)
{
loglog(RC_LOG_SERIOUS
, "can not use our hostname (%s:KEY) as identity: %s"
, myid_str[MYID_HOSTNAME]
, ugh);
logged_myid_fqdn_key_warning = TRUE;
}
next_step = fos_myid_hostname_key;
ugh = NULL; /* failure can be recovered from */
}
else
{
/* we can use our IP as OE identity for initiation */
if (!logged_myid_fqdn_key_warning)
{
loglog(RC_LOG_SERIOUS
, "using our hostname (%s:KEY) as identity!"
, myid_str[MYID_HOSTNAME]);
logged_myid_fqdn_key_warning = TRUE;
}
next_step = fos_our_client;
}
break;
#endif
case fos_our_client: /* TXT for our client */
{
/* Our client is not us: we must check the TXT records.
* Note: if c is different this time, there is
* a chance that we did the wrong query.
* If so, treat as a kind of failure.
*/
const struct RSA_private_key *our_RSA_pri = get_RSA_private_key(c);
next_step = fos_his_client; /* normal situation */
passert(sr != NULL);
if (our_RSA_pri == NULL)
{
ugh = "we don't know our own RSA key";
}
else if (sameaddr(&sr->this.host_addr, &b->our_client))
{
/* this wasn't true when we started -- bail */
ugh = "our IP address changed underfoot";
}
else if (!same_id(&ac->sgw_id, &sr->this.id))
{
/* this wasn't true when we started -- bail */
ugh = "our ID changed underfoot";
}
else
{
/* Similar to code in quick_inI1_outR1_tail
* for checking the other side.
*/
struct gw_info *gwp;
ugh = "no TXT RR for our client delegates us";
for (gwp = ac->gateways_from_dns; gwp != NULL; gwp = gwp->next)
{
passert(same_id(&gwp->gw_id, &sr->this.id));
ugh = "TXT RR for our client has wrong key";
/* If there is a key from the TXT record,
* we count it as a win if we match the key.
* If there was no key, we have a tentative win:
* we need to check our KEY record to be sure.
*/
if (!gwp->gw_key_present)
{
/* Success, but the TXT had no key
* so we must check our our own KEY records.
*/
next_step = fos_our_txt;
ugh = NULL; /* good! */
break;
}
if (same_RSA_public_key(&our_RSA_pri->pub, &gwp->key->u.rsa))
{
ugh = NULL; /* good! */
break;
}
}
}
}
break;
case fos_our_txt: /* TXT for us */
{
/* Check if TXT lookup yielded good results.
* Looking up based on our ID. Used if
* client is ourself, or if TXT had no public key.
* Note: if c is different this time, there is
* a chance that we did the wrong query.
* If so, treat as a kind of failure.
*/
const struct RSA_private_key *our_RSA_pri = get_RSA_private_key(c);
next_step = fos_his_client; /* unless we decide to look for KEY RR */
if (our_RSA_pri == NULL)
{
ugh = "we don't know our own RSA key";
}
else if (!same_id(&ac->id, &c->spd.this.id))
{
ugh = "our ID changed underfoot";
}
else
{
/* Similar to code in RSA_check_signature
* for checking the other side.
*/
struct gw_info *gwp;
ugh = "no TXT RR for us";
for (gwp = ac->gateways_from_dns; gwp != NULL; gwp = gwp->next)
{
passert(same_id(&gwp->gw_id, &sr->this.id));
ugh = "TXT RR for us has wrong key";
if (gwp->gw_key_present
&& same_RSA_public_key(&our_RSA_pri->pub, &gwp->key->u.rsa))
{
DBG(DBG_CONTROL,
DBG_log("initiate on demand found TXT with right public key at: %s"
, mycredentialstr));
ugh = NULL;
break;
}
}
#ifdef USE_KEYRR
if (ugh != NULL)
{
/* if no TXT with right key, try KEY */
DBG(DBG_CONTROL,
DBG_log("will try for KEY RR since initiate on demand found %s: %s"
, ugh, mycredentialstr));
next_step = fos_our_key;
ugh = NULL;
}
#endif
}
}
break;
#ifdef USE_KEYRR
case fos_our_key: /* KEY for us */
{
/* Check if KEY lookup yielded good results.
* Looking up based on our ID. Used if
* client is ourself, or if TXT had no public key.
* Note: if c is different this time, there is
* a chance that we did the wrong query.
* If so, treat as a kind of failure.
*/
const struct RSA_private_key *our_RSA_pri = get_RSA_private_key(c);
next_step = fos_his_client; /* always */
if (our_RSA_pri == NULL)
{
ugh = "we don't know our own RSA key";
}
else if (!same_id(&ac->id, &c->spd.this.id))
{
ugh = "our ID changed underfoot";
}
else
{
/* Similar to code in RSA_check_signature
* for checking the other side.
*/
struct pubkey_list *kr;
ugh = "no KEY RR found for us (and no good TXT RR)";
for (kr = ac->keys_from_dns; kr != NULL; kr = kr->next)
{
ugh = "all our KEY RRs have the wrong public key (and no good TXT RR)";
if (kr->key->alg == PUBKEY_ALG_RSA
&& same_RSA_public_key(&our_RSA_pri->pub, &kr->key->u.rsa))
{
/* do this only once a day */
if (!logged_txt_warning)
{
loglog(RC_LOG_SERIOUS
, "found KEY RR but not TXT RR for %s. See http://www.freeswan.org/err/txt-change.html."
, mycredentialstr);
logged_txt_warning = TRUE;
}
ugh = NULL; /* good! */
break;
}
}
}
}
break;
#endif /* USE_KEYRR */
case fos_his_client: /* TXT for his client */
{
/* We've finished last DNS queries: TXT for his client.
* Using the information, try to instantiate a connection
* and start negotiating.
* We now know the peer. The chosing of "c" ignored this,
* so we will disregard its current value.
* !!! We need to randomize the entry in gw that we choose.
*/
next_step = fos_done; /* no more queries */
c = build_outgoing_opportunistic_connection(ac->gateways_from_dns
, &b->our_client
, &b->peer_client);
if (c == NULL)
{
/* We cannot seem to instantiate a suitable connection:
* complain clearly.
*/
char ocb[ADDRTOT_BUF]
, pcb[ADDRTOT_BUF]
, pb[ADDRTOT_BUF];
addrtot(&b->our_client, 0, ocb, sizeof(ocb));
addrtot(&b->peer_client, 0, pcb, sizeof(pcb));
passert(id_is_ipaddr(&ac->gateways_from_dns->gw_id));
addrtot(&ac->gateways_from_dns->gw_id.ip_addr, 0, pb, sizeof(pb));
loglog(RC_OPPOFAILURE
, "no suitable connection for opportunism"
" between %s and %s with %s as peer"
, ocb, pcb, pb);
#ifdef KLIPS
if (b->held)
{
/* Replace HOLD with PASS.
* The type of replacement *ought* to be
* specified by policy.
*/
(void) replace_bare_shunt(&b->our_client, &b->peer_client
, BOTTOM_PRIO
, SPI_PASS /* fail into PASS */
, TRUE, "no suitable connection");
}
#endif
}
else
{
/* If we are to proceed asynchronously, b->whackfd will be NULL_FD. */
passert(c->kind == CK_INSTANCE);
passert(c->gw_info != NULL);
passert(HAS_IPSEC_POLICY(c->policy));
passert(LHAS(LELEM(RT_UNROUTED) | LELEM(RT_ROUTED_PROSPECTIVE), c->spd.routing));
#ifdef KLIPS
if (b->held)
{
/* what should we do on failure? */
(void) assign_hold(c, &c->spd
, &b->our_client, &b->peer_client);
}
#endif
c->gw_info->key->last_tried_time = now();
ipsecdoi_initiate(b->whackfd, c, c->policy, 1, SOS_NOBODY);
b->whackfd = NULL_FD; /* protect from close */
}
}
break;
default:
bad_case(b->step);
}
/* the second chunk: initiate the next DNS query (if any) */
DBG(DBG_CONTROL,
{
char ours[ADDRTOT_BUF];
char his[ADDRTOT_BUF];
addrtot(&b->our_client, 0, ours, sizeof(ours));
addrtot(&b->peer_client, 0, his, sizeof(his));
DBG_log("initiate on demand from %s to %s new state: %s with ugh: %s"
, ours, his, oppo_step_name[b->step], ugh ? ugh : "ok");
});
if (ugh != NULL)
{
b->policy_prio = c->prio;
b->failure_shunt = shunt_policy_spi(c, FALSE);
cannot_oppo(c, b, ugh);
}
else if (next_step == fos_done)
{
/* nothing to do */
}
else
{
/* set up the next query */
struct find_oppo_continuation *cr = alloc_thing(struct find_oppo_continuation
, "opportunistic continuation");
struct id id;
b->policy_prio = c->prio;
b->failure_shunt = shunt_policy_spi(c, FALSE);
cr->b = *b; /* copy; start hand off of whackfd */
cr->b.failure_ok = FALSE;
cr->b.step = next_step;
for (sr = &c->spd
; sr!=NULL && !sameaddr(&sr->this.host_addr, &b->our_client)
; sr = sr->next)
;
if (sr == NULL)
sr = &c->spd;
/* If a %hold shunt has replaced the eroute for this template,
* record this fact.
*/
if (b->held
&& sr->routing == RT_ROUTED_PROSPECTIVE && eclipsable(sr))
{
sr->routing = RT_ROUTED_ECLIPSED;
eclipse_count++;
}
/* Switch to issue next query.
* A case may turn out to be unnecessary. If so, it falls
* through to the next case.
* Figuring out what %myid can stand for must be done before
* our client credentials are looked up: we must know what
* the client credentials may use to identify us.
* On the other hand, our own credentials should be looked
* up after our clients in case our credentials are not
* needed at all.
* XXX this is a wasted effort if we don't have credentials
* BUT they are not needed.
*/
switch (next_step)
{
case fos_myid_ip_txt:
if (c->spd.this.id.kind == ID_MYID
&& myid_state != MYID_SPECIFIED)
{
cr->b.failure_ok = TRUE;
cr->b.want = b->want = "TXT record for IP address as %myid";
ugh = start_adns_query(&myids[MYID_IP]
, &myids[MYID_IP]
, T_TXT
, continue_oppo
, &cr->ac);
break;
}
cr->b.step = fos_myid_hostname_txt;
/* fall through */
case fos_myid_hostname_txt:
if (c->spd.this.id.kind == ID_MYID
&& myid_state != MYID_SPECIFIED)
{
#ifdef USE_KEYRR
cr->b.failure_ok = TRUE;
#else
cr->b.failure_ok = FALSE;
#endif
cr->b.want = b->want = "TXT record for hostname as %myid";
ugh = start_adns_query(&myids[MYID_HOSTNAME]
, &myids[MYID_HOSTNAME]
, T_TXT
, continue_oppo
, &cr->ac);
break;
}
#ifdef USE_KEYRR
cr->b.step = fos_myid_ip_key;
/* fall through */
case fos_myid_ip_key:
if (c->spd.this.id.kind == ID_MYID
&& myid_state != MYID_SPECIFIED)
{
cr->b.failure_ok = TRUE;
cr->b.want = b->want = "KEY record for IP address as %myid (no good TXT)";
ugh = start_adns_query(&myids[MYID_IP]
, (const struct id *) NULL /* security gateway meaningless */
, T_KEY
, continue_oppo
, &cr->ac);
break;
}
cr->b.step = fos_myid_hostname_key;
/* fall through */
case fos_myid_hostname_key:
if (c->spd.this.id.kind == ID_MYID
&& myid_state != MYID_SPECIFIED)
{
cr->b.failure_ok = FALSE; /* last attempt! */
cr->b.want = b->want = "KEY record for hostname as %myid (no good TXT)";
ugh = start_adns_query(&myids[MYID_HOSTNAME]
, (const struct id *) NULL /* security gateway meaningless */
, T_KEY
, continue_oppo
, &cr->ac);
break;
}
#endif
cr->b.step = fos_our_client;
/* fall through */
case fos_our_client: /* TXT for our client */
if (!sameaddr(&c->spd.this.host_addr, &b->our_client))
{
/* Check that at least one TXT(reverse(b->our_client)) is workable.
* Note: {unshare|free}_id_content not needed for id: ephemeral.
*/
cr->b.want = b->want = "our client's TXT record";
iptoid(&b->our_client, &id);
ugh = start_adns_query(&id
, &c->spd.this.id /* we are the security gateway */
, T_TXT
, continue_oppo
, &cr->ac);
break;
}
cr->b.step = fos_our_txt;
/* fall through */
case fos_our_txt: /* TXT for us */
cr->b.failure_ok = b->failure_ok = TRUE;
cr->b.want = b->want = "our TXT record";
ugh = start_adns_query(&sr->this.id
, &sr->this.id /* we are the security gateway XXX - maybe ignore? mcr */
, T_TXT
, continue_oppo
, &cr->ac);
break;
#ifdef USE_KEYRR
case fos_our_key: /* KEY for us */
cr->b.want = b->want = "our KEY record";
cr->b.failure_ok = b->failure_ok = FALSE;
ugh = start_adns_query(&sr->this.id
, (const struct id *) NULL /* security gateway meaningless */
, T_KEY
, continue_oppo
, &cr->ac);
break;
#endif /* USE_KEYRR */
case fos_his_client: /* TXT for his client */
/* note: {unshare|free}_id_content not needed for id: ephemeral */
cr->b.want = b->want = "target's TXT record";
cr->b.failure_ok = b->failure_ok = FALSE;
iptoid(&b->peer_client, &id);
ugh = start_adns_query(&id
, (const struct id *) NULL /* security gateway unconstrained */
, T_TXT
, continue_oppo
, &cr->ac);
break;
default:
bad_case(next_step);
}
if (ugh == NULL)
b->whackfd = NULL_FD; /* complete hand-off */
else
cannot_oppo(c, b, ugh);
}
}
close_any(b->whackfd);
}
void
terminate_connection(const char *nm)
{
/* Loop because more than one may match (master and instances)
* But at least one is required (enforced by con_by_name).
*/
struct connection *c, *n;
for (c = con_by_name(nm, TRUE); c != NULL; c = n)
{
n = c->ac_next; /* grab this before c might disappear */
if (streq(c->name, nm)
&& c->kind >= CK_PERMANENT
&& !NEVER_NEGOTIATE(c->policy))
{
set_cur_connection(c);
plog("terminating SAs using this connection");
c->policy &= ~POLICY_UP;
flush_pending_by_connection(c);
delete_states_by_connection(c);
reset_cur_connection();
}
}
}
/* check nexthop safety
* Our nexthop must not be within a routed client subnet, and vice versa.
* Note: we don't think this is true. We think that KLIPS will
* not process a packet output by an eroute.
*/
#ifdef NEVER
//bool
//check_nexthop(const struct connection *c)
//{
// struct connection *d;
//
// if (addrinsubnet(&c->spd.this.host_nexthop, &c->spd.that.client))
// {
// loglog(RC_LOG_SERIOUS, "cannot perform routing for connection \"%s\""
// " because nexthop is within peer's client network",
// c->name);
// return FALSE;
// }
//
// for (d = connections; d != NULL; d = d->next)
// {
// if (d->routing != RT_UNROUTED)
// {
// if (addrinsubnet(&c->spd.this.host_nexthop, &d->spd.that.client))
// {
// loglog(RC_LOG_SERIOUS, "cannot do routing for connection \"%s\"
// " because nexthop is contained in"
// " existing routing for connection \"%s\"",
// c->name, d->name);
// return FALSE;
// }
// if (addrinsubnet(&d->spd.this.host_nexthop, &c->spd.that.client))
// {
// loglog(RC_LOG_SERIOUS, "cannot do routing for connection \"%s\"
// " because it contains nexthop of"
// " existing routing for connection \"%s\"",
// c->name, d->name);
// return FALSE;
// }
// }
// }
// return TRUE;
//}
#endif /* NEVER */
/* an ISAKMP SA has been established.
* Note the serial number, and release any connections with
* the same peer ID but different peer IP address.
*/
bool uniqueIDs = FALSE; /* --uniqueids? */
void
ISAKMP_SA_established(struct connection *c, so_serial_t serial)
{
c->newest_isakmp_sa = serial;
if (uniqueIDs)
{
/* for all connections: if the same Phase 1 peer ID is used
* for a different IP address, unorient that connection.
*/
struct connection *d;
for (d = connections; d != NULL; )
{
struct connection *next = d->ac_next; /* might move underneath us */
if (d->kind >= CK_PERMANENT
&& same_id(&c->spd.that.id, &d->spd.that.id)
&& !sameaddr(&c->spd.that.host_addr, &d->spd.that.host_addr))
{
release_connection(d);
}
d = next;
}
}
}
/* 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
, struct spd_route **srp
, struct connection **erop
, struct spd_route **esrp)
{
struct connection *d
, *best_ro = c
, *best_ero = c;
struct spd_route *srd, *src;
struct spd_route *best_sr, *best_esr;
enum routing_t best_routing, best_erouting;
passert(oriented(*c));
best_sr = NULL;
best_esr = NULL;
best_routing = c->spd.routing;
best_erouting = best_routing;
for (d = connections; d != NULL; d = d->ac_next)
{
for (srd = &d->spd; srd; srd = srd->next)
{
if (srd->routing == RT_UNROUTED)
continue;
for (src = &c->spd; src; src=src->next)
{
if (samesubnet(&src->that.client, &srd->that.client))
{
passert(oriented(*d));
if (srd->routing > best_routing)
{
best_ro = d;
best_sr = srd;
best_routing = srd->routing;
}
if (srd->routing > best_erouting
&& samesubnet(&src->this.client, &srd->this.client))
{
best_ero = d;
best_esr = srd;
best_erouting = srd->routing;
}
}
}
}
}
DBG(DBG_CONTROL,
{
char cib[CONN_INST_BUF];
err_t m = builddiag("route owner of \"%s\"%s %s:"
, c->name
, (fmt_conn_instance(c, cib), cib)
, enum_name(&routing_story, c->spd.routing));
if (!routed(best_ro->spd.routing))
m = builddiag("%s NULL", m);
else if (best_ro == c)
m = builddiag("%s self", m);
else
m = builddiag("%s \"%s\"%s %s", m
, best_ro->name
, (fmt_conn_instance(best_ro, cib), cib)
, enum_name(&routing_story, best_ro->spd.routing));
if (erop != NULL)
{
m = builddiag("%s; eroute owner:", m);
if (!erouted(best_ero->spd.routing))
m = builddiag("%s NULL", m);
else if (best_ero == c)
m = builddiag("%s self", m);
else
m = builddiag("%s \"%s\"%s %s", m
, best_ero->name
, (fmt_conn_instance(best_ero, cib), cib)
, enum_name(&routing_story, best_ero->spd.routing));
}
DBG_log("%s", m);
});
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;
}
/* Find a connection that owns the shunt eroute between subnets.
* There ought to be only one.
* This might get to be a bottleneck -- try hashing if it does.
*/
struct connection *
shunt_owner(const ip_subnet *ours, const ip_subnet *his)
{
struct connection *c;
struct spd_route *sr;
for (c = connections; c != NULL; c = c->ac_next)
{
for (sr = &c->spd; sr; sr = sr->next)
{
if (shunt_erouted(sr->routing)
&& samesubnet(ours, &sr->this.client)
&& samesubnet(his, &sr->that.client))
return c;
}
}
return NULL;
}
/* Find some connection with this pair of hosts.
* We don't know enough to chose amongst those available.
* ??? no longer usefully different from find_host_pair_connections
*/
struct connection *
find_host_connection(const ip_address *me, u_int16_t my_port
, const ip_address *him, u_int16_t his_port)
{
return find_host_pair_connections(me, my_port, him, his_port);
}
/* 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
, bool initiator)
{
struct connection *c = st->st_connection;
u_int16_t auth = st->st_oakley.auth;
struct connection *d;
lset_t auth_policy;
const chunk_t *psk;
const struct RSA_private_key *my_RSA_pri = NULL;
bool wcpip; /* wildcard Peer IP? */
if (same_id(&c->spd.that.id, peer_id))
return c; /* peer ID matches current connection -- look no further */
switch (auth)
{
case OAKLEY_PRESHARED_KEY:
auth_policy = POLICY_PSK;
psk = get_preshared_secret(c);
/* It should be virtually impossible to fail to find PSK:
* we just used it to decode the current message!
*/
if (psk == NULL)
return NULL; /* cannot determine PSK! */
break;
case OAKLEY_RSA_SIG:
auth_policy = POLICY_RSASIG;
if (initiator)
{
/* at this point, we've committed to our RSA private key:
* we used it in our previous message.
*/
my_RSA_pri = get_RSA_private_key(c);
if (my_RSA_pri == NULL)
return NULL; /* cannot determine my RSA private key! */
}
break;
default:
bad_case(auth);
}
/* 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)
*/
d = c->host_pair->connections;
for (wcpip = FALSE; ; wcpip = TRUE)
{
struct connection *best_found = NULL;
for (; d != NULL; d = d->hp_next)
{
bool exact = same_id(peer_id, &d->spd.that.id); /* exact peer ID match? */
/* ignore group connections */
if (d->policy & POLICY_GROUP)
continue;
/* check if peer_id matches, exactly or after instantiation */
if (!exact && !(wcpip && d->spd.that.id.kind == ID_NONE))
continue;
/* if initiator, our ID must match exactly */
if (initiator && !same_id(&c->spd.this.id, &d->spd.this.id))
continue;
/* authentication used must fit policy of this connection */
if ((d->policy & auth_policy) == LEMPTY)
continue; /* our auth isn't OK for this connection */
switch (auth)
{
case OAKLEY_PRESHARED_KEY:
/* secret must match the one we already used */
{
const chunk_t *dpsk = get_preshared_secret(d);
if (dpsk == NULL)
continue; /* no secret */
if (psk != dpsk)
if (psk->len != dpsk->len
|| memcmp(psk->ptr, dpsk->ptr, psk->len) != 0)
continue; /* different secret */
}
break;
case OAKLEY_RSA_SIG:
/* We must at least be able to find our private key.
* If we initiated, it must match the one we
* used in the SIG_I payload that we sent previously.
*/
{
const struct RSA_private_key *pri
= get_RSA_private_key(d);
if (pri == NULL
|| (initiator
&& !same_RSA_public_key(&my_RSA_pri->pub, &pri->pub)))
continue;
}
break;
default:
bad_case(auth);
}
/* d has passed all the tests.
* We'll go with it if the Peer ID was an exact match.
*/
if (exact)
return d;
/* We'll remember it as best_found in case an exact
* match doesn't come along.
*/
best_found = d;
}
if (wcpip)
return best_found; /* been around twice already */
/* 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
*/
d = find_host_pair_connections(&c->spd.this.host_addr, c->spd.this.host_port
, (ip_address *)NULL, c->spd.that.host_port);
}
}
/* 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.
*/
/* fc_try: a helper function for find_client_connection */
static struct connection *
fc_try(const struct connection *c
, struct host_pair *hp
, const struct id *peer_id
, const ip_subnet *our_net
, const ip_subnet *peer_net)
{
struct connection *d;
struct connection *best = NULL;
const bool peer_net_is_host = subnetisaddr(peer_net, &c->spd.that.host_addr);
for (d = hp->connections; d != NULL; d = d->hp_next)
{
struct spd_route *sr;
if (d->policy & POLICY_GROUP)
continue;
if (!same_peer_ids(c, d, peer_id))
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.
*/
for (sr = &d->spd; best != d && sr != NULL; sr = sr->next)
{
#ifdef DEBUG
if (DBGP(DBG_CONTROLMORE))
{
char s1[SUBNETTOT_BUF],d1[SUBNETTOT_BUF];
char s3[SUBNETTOT_BUF],d3[SUBNETTOT_BUF];
subnettot(our_net, 0, s1, sizeof(s1));
subnettot(peer_net, 0, d1, sizeof(d1));
subnettot(&sr->this.client, 0, s3, sizeof(s3));
subnettot(&sr->that.client, 0, d3, sizeof(d3));
DBG_log(" fc_try trying %s:%s->%s vs %s:%s->%s"
, c->name, s1, d1, d->name, s3, d3);
}
#endif /* DEBUG */
if (!samesubnet(&sr->this.client, our_net))
continue;
if (sr->that.has_client)
{
if (!samesubnet(&sr->that.client, peer_net))
continue;
}
else
{
if (!peer_net_is_host)
continue;
}
/* We've run the gauntlet -- success:
* We've got an exact match of subnets.
* If this isn't routed, keep looking in case we
* find a routed one.
*/
if (best == NULL || routed(sr->routing))
{
best = d;
break;
}
}
}
if (best != NULL && NEVER_NEGOTIATE(best->policy))
best = NULL;
DBG(DBG_CONTROLMORE, DBG_log(" fc_try concluding with %s"
, (d ? d->name : "none")));
return best;
}
static struct connection *
fc_try_oppo(const struct connection *c
, struct host_pair *hp
, const struct id *peer_id
, const ip_subnet *our_net
, const ip_subnet *peer_net)
{
struct connection *d;
struct connection *best = NULL;
policy_prio_t best_prio = BOTTOM_PRIO;
for (d = hp->connections; d != NULL; d = d->hp_next)
{
struct spd_route *sr;
policy_prio_t prio;
if (d->policy & POLICY_GROUP)
continue;
if (!same_peer_ids(c, d, peer_id))
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.
*/
for (sr = &d->spd; sr != NULL; sr = sr->next)
{
#ifdef DEBUG
if (DBGP(DBG_CONTROLMORE))
{
char s1[SUBNETTOT_BUF],d1[SUBNETTOT_BUF];
char s3[SUBNETTOT_BUF],d3[SUBNETTOT_BUF];
subnettot(our_net, 0, s1, sizeof(s1));
subnettot(peer_net, 0, d1, sizeof(d1));
subnettot(&sr->this.client, 0, s3, sizeof(s3));
subnettot(&sr->that.client, 0, d3, sizeof(d3));
DBG_log(" fc_try_oppo trying %s:%s->%s vs %s:%s->%s"
, c->name, s1, d1, d->name, s3, d3);
}
#endif /* DEBUG */
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, his smallest client subnet is preferred
* - given that, a routed connection is preferrred
*/
prio = d->prio + routed(sr->routing);
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_OPPO) == LEMPTY))
{
best = NULL;
}
DBG(DBG_CONTROLMORE, DBG_log(" fc_try_oppo concluding with %s"
, (d ? d->name : "none")));
return best;
}
struct connection *
find_client_connection(struct connection *c
, const ip_subnet *our_net, const ip_subnet *peer_net)
{
struct connection *d;
struct spd_route *sr;
#ifdef DEBUG
if (DBGP(DBG_CONTROLMORE))
{
char s1[SUBNETTOT_BUF],d1[SUBNETTOT_BUF];
subnettot(our_net, 0, s1, sizeof(s1));
subnettot(peer_net, 0, d1, sizeof(d1));
DBG_log("find_client_connection starting with %s"
, (c ? c->name : "(none)"));
DBG_log(" looking for %s -> %s", s1, d1);
}
#endif /* DEBUG */
/* give priority to current connection
* but even greater priority to a routed concrete connection
*/
{
struct connection *unrouted = NULL;
int srnum = -1;
for (sr = &c->spd; unrouted == NULL && sr != NULL; sr = sr->next)
{
srnum++;
#ifdef DEBUG
if (DBGP(DBG_CONTROLMORE))
{
char s2[SUBNETTOT_BUF],d2[SUBNETTOT_BUF];
subnettot(&sr->this.client, 0, s2, sizeof(s2));
subnettot(&sr->that.client, 0, d2, sizeof(d2));
DBG_log(" concrete checking against sr#%d %s->%s"
, srnum, s2, d2);
}
#endif /* DEBUG */
if (samesubnet(&sr->this.client, our_net)
&& samesubnet(&sr->that.client, peer_net))
{
passert(oriented(*c));
if (routed(sr->routing))
return c;
unrouted = c;
}
}
/* exact match? */
d = fc_try(c, c->host_pair, NULL, our_net, peer_net);
DBG(DBG_CONTROLMORE, DBG_log(" 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 */
struct spd_route *sr;
struct host_pair *hp = NULL;
for (sr = &c->spd; hp==NULL && sr != NULL; sr = sr->next)
{
hp = find_host_pair(&sr->this.host_addr
, sr->this.host_port
, NULL
, sr->that.host_port);
#ifdef DEBUG
if (DBGP(DBG_CONTROLMORE))
{
char s2[SUBNETTOT_BUF],d2[SUBNETTOT_BUF];
subnettot(&sr->this.client, 0, s2, sizeof(s2));
subnettot(&sr->that.client, 0, d2, sizeof(d2));
DBG_log(" checking hostpair %s->%s is %s"
, s2, d2
, (hp ? "found" : "not found"));
}
#endif /* DEBUG */
}
if (hp != NULL)
{
struct id abstract_peer_id;
abstract_peer_id.kind = ID_NONE;
/* RW match with actual peer_id? */
d = fc_try(c, hp, NULL, our_net, peer_net);
if (d == NULL && his_id_was_instantiated(c))
{
/* RW match with abstract peer_id?
* Note that later instantiation will result in the same peer_id.
*/
d = fc_try(c, hp, &abstract_peer_id, our_net, peer_net);
}
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, &abstract_peer_id, our_net, peer_net);
}
}
}
DBG(DBG_CONTROLMORE, DBG_log(" concluding with d= %s"
, (d ? d->name : "none")));
return d;
}
int
connection_compare(const struct connection *ca
, const struct connection *cb)
{
int ret;
/* DBG_log("comparing %s to %s", ca->name, cb->name); */
ret = strcasecmp(ca->name, cb->name);
if (ret != 0)
return ret;
ret = ca->kind - cb->kind; /* note: enum connection_kind behaves like int */
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->prio < cb->prio ? -1
: ca->prio > cb->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);
}
void
show_connections_status(void)
{
struct connection *c;
int count, i;
struct connection **array;
/* make an array of connections, and sort it */
count=0;
for (c = connections; c != NULL; c = c->ac_next)
{
count++;
}
array = alloc_bytes(sizeof(struct connection *)*count, "connection array");
count=0;
for (c = connections; c != NULL; c = c->ac_next)
{
array[count++]=c;
}
/* sort it! */
qsort(array, count, sizeof(struct connection *), connection_compare_qsort);
for (i=0; i<count; i++)
{
const char *ifn;
char instance[1 + 10 + 1];
char prio[POLICY_PRIO_BUF];
c = array[i];
ifn = oriented(*c)? c->interface->rname : "";
instance[0] = '\0';
if (c->kind == CK_INSTANCE && c->instance_serial != 0)
snprintf(instance, sizeof(instance), "[%lu]", c->instance_serial);
/* show topology */
{
char topo[CONNECTION_BUF];
struct spd_route *sr = &c->spd;
int num=0;
while (sr != NULL)
{
(void) format_connection(topo, sizeof(topo), c, sr);
whack_log(RC_COMMENT, "\"%s\"%s: %s; %s; eroute owner: #%lu"
, c->name, instance, topo
, enum_name(&routing_story, sr->routing)
, sr->eroute_owner);
sr = sr->next;
num++;
}
}
whack_log(RC_COMMENT
, "\"%s\"%s: ike_life: %lus; ipsec_life: %lus;"
" rekey_margin: %lus; rekey_fuzz: %lu%%; keyingtries: %lu"
, c->name
, instance
, (unsigned long) c->sa_ike_life_seconds
, (unsigned long) c->sa_ipsec_life_seconds
, (unsigned long) c->sa_rekey_margin
, (unsigned long) c->sa_rekey_fuzz
, (unsigned long) c->sa_keying_tries);
if (c->policy_next)
{
whack_log(RC_COMMENT
, "\"%s\"%s: policy_next: %s"
, c->name, instance, c->policy_next->name);
}
/* Note: we display key_from_DNS_on_demand as if policy [lr]KOD */
fmt_policy_prio(c->prio, prio);
whack_log(RC_COMMENT
, "\"%s\"%s: policy: %s%s%s; prio: %s; interface: %s; "
, c->name
, instance
, prettypolicy(c->policy)
, c->spd.this.key_from_DNS_on_demand? "+lKOD" : ""
, c->spd.that.key_from_DNS_on_demand? "+rKOD" : ""
, prio
, ifn);
whack_log(RC_COMMENT
, "\"%s\"%s: newest ISAKMP SA: #%ld; newest IPsec SA: #%ld; "
, c->name
, instance
, c->newest_isakmp_sa
, c->newest_ipsec_sa);
}
pfree(array);
}
/* struct pending, the structure representing Quick Mode
* negotiations delayed until a Keying Channel has been negotiated.
* Essentially, a pending call to quick_outI1.
*/
struct pending {
int whack_sock;
struct state *isakmp_sa;
struct connection *connection;
lset_t policy;
unsigned long try;
so_serial_t replacing;
struct pending *next;
};
/* queue a Quick Mode negotiation pending completion of a suitable Main Mode */
void
add_pending(int whack_sock
, struct state *isakmp_sa
, struct connection *c
, lset_t policy
, unsigned long try
, so_serial_t replacing)
{
struct pending *p = alloc_thing(struct pending, "struct pending");
DBG(DBG_CONTROL, DBG_log("Queuing pending Quick Mode with %s \"%s\""
, ip_str(&c->spd.that.host_addr)
, c->name));
p->whack_sock = whack_sock;
p->isakmp_sa = isakmp_sa;
p->connection = c;
p->policy = policy;
p->try = try;
p->replacing = replacing;
p->next = c->host_pair->pending;
c->host_pair->pending = p;
}
/* Release all the whacks awaiting the completion of this state.
* This is accomplished by closing all the whack socket file descriptors.
* We go to a lot of trouble to tell each whack, but to not tell it twice.
*/
void
release_pending_whacks(struct state *st, err_t story)
{
struct pending *p;
struct stat stst;
if (st->st_whack_sock == NULL_FD || fstat(st->st_whack_sock, &stst) != 0)
zero(&stst); /* resulting st_dev/st_ino ought to be distinct */
release_whack(st);
for (p = st->st_connection->host_pair->pending; p != NULL; p = p->next)
{
if (p->isakmp_sa == st && p->whack_sock != NULL_FD)
{
struct stat pst;
if (fstat(p->whack_sock, &pst) == 0
&& (stst.st_dev != pst.st_dev || stst.st_ino != pst.st_ino))
{
passert(whack_log_fd == NULL_FD);
whack_log_fd = p->whack_sock;
whack_log(RC_COMMENT
, "%s for ISAKMP SA, but releasing whack for pending IPSEC SA"
, story);
whack_log_fd = NULL_FD;
}
close(p->whack_sock);
p->whack_sock = NULL_FD;
}
}
}
static void
delete_pending(struct pending **pp)
{
struct pending *p = *pp;
*pp = p->next;
if (p->connection != NULL)
connection_discard(p->connection);
close_any(p->whack_sock);
pfree(p);
}
void
unpend(struct state *st)
{
struct pending **pp
, *p;
for (pp = &st->st_connection->host_pair->pending; (p = *pp) != NULL; )
{
if (p->isakmp_sa == st)
{
DBG(DBG_CONTROL, DBG_log("unqueuing pending Quick Mode with %s \"%s\""
, ip_str(&p->connection->spd.that.host_addr)
, p->connection->name));
(void) quick_outI1(p->whack_sock, st, p->connection, p->policy
, p->try, p->replacing);
p->whack_sock = NULL_FD; /* ownership transferred */
p->connection = NULL; /* ownership transferred */
delete_pending(pp);
}
else
{
pp = &p->next;
}
}
}
/* a Main Mode negotiation has been replaced; update any pending */
void
update_pending(struct state *os, struct state *ns)
{
struct pending *p;
for (p = os->st_connection->host_pair->pending; p != NULL; p = p->next)
if (p->isakmp_sa == os)
p->isakmp_sa = ns;
}
/* a Main Mode negotiation has failed; discard any pending */
void
flush_pending_by_state(struct state *st)
{
struct host_pair *hp = st->st_connection->host_pair;
if (hp != NULL)
{
struct pending **pp
, *p;
for (pp = &hp->pending; (p = *pp) != NULL; )
{
if (p->isakmp_sa == st)
delete_pending(pp);
else
pp = &p->next;
}
}
}
/* a connection has been deleted; discard any related pending */
static void
flush_pending_by_connection(struct connection *c)
{
if (c->host_pair != NULL)
{
struct pending **pp
, *p;
for (pp = &c->host_pair->pending; (p = *pp) != NULL; )
{
if (p->connection == c)
{
p->connection = NULL; /* prevent delete_pending from releasing */
delete_pending(pp);
}
else
{
pp = &p->next;
}
}
}
}
void
show_pending_phase2(const struct host_pair *hp, const struct state *st)
{
const struct pending *p;
for (p = hp->pending; p != NULL; p = p->next)
{
if (p->isakmp_sa == st)
{
/* connection-name state-number [replacing state-number] */
char cip[CONN_INST_BUF];
fmt_conn_instance(p->connection, cip);
whack_log(RC_COMMENT, "#%lu: pending Phase 2 for \"%s\"%s replacing #%lu"
, p->isakmp_sa->st_serialno
, p->connection->name
, cip
, p->replacing);
}
}
}
/* Delete a connection if it is an instance and it is no longer in use.
* We must be careful to avoid circularity:
* we don't touch it if it is CK_GOING_AWAY.
*/
void
connection_discard(struct connection *c)
{
if (c->kind == CK_INSTANCE)
{
/* see if it is being used by a pending */
struct pending *p;
for (p = c->host_pair->pending; p != NULL; p = p->next)
if (p->connection == c)
return; /* in use, so we're done */
if (!states_use_connection(c))
delete_connection(c);
}
}
/* 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(struct connection *c, struct spd_route **esrp)
{
struct connection *ue;
struct spd_route *sr1 = &c->spd;
ue = NULL;
while (sr1 != NULL && ue != NULL)
{
for (ue = connections; ue != NULL; ue = ue->ac_next)
{
struct spd_route *srue = &ue->spd;
while (srue != NULL
&& srue->routing == RT_ROUTED_ECLIPSED
&& !(samesubnet(&sr1->this.client, &srue->this.client)
&& samesubnet(&sr1->that.client, &srue->that.client)))
{
srue = srue->next;
}
if (srue != NULL && srue->routing==RT_ROUTED_ECLIPSED)
{
*esrp = srue;
break;
}
}
}
return ue;
}
/*
* Local Variables:
* c-basic-offset:4
* c-style: pluto
* End:
*/
|