1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474
|
/*
* OpenVPN -- An application to securely tunnel IP networks
* over a single TCP/UDP port, with support for SSL/TLS-based
* session authentication and key exchange,
* packet encryption, packet authentication, and
* packet compression.
*
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Support routines for configuring and accessing TUN/TAP
* virtual network adapters.
*
* This file is based on the TUN/TAP driver interface routines
* from VTun by Maxim Krasnyansky <max_mk@yahoo.com>.
*/
#include "syshead.h"
#include "tun.h"
#include "fdmisc.h"
#include "common.h"
#include "misc.h"
#include "socket.h"
#include "manage.h"
#include "route.h"
#include "win32.h"
#include "memdbg.h"
#ifdef WIN32
/* #define SIMULATE_DHCP_FAILED */ /* simulate bad DHCP negotiation */
#define NI_TEST_FIRST (1<<0)
#define NI_IP_NETMASK (1<<1)
#define NI_OPTIONS (1<<2)
static void netsh_ifconfig (const struct tuntap_options *to,
const char *flex_name,
const in_addr_t ip,
const in_addr_t netmask,
const unsigned int flags);
static const char *netsh_get_id (const char *dev_node, struct gc_arena *gc);
#endif
#ifdef TARGET_SOLARIS
static void solaris_error_close (struct tuntap *tt, const struct env_set *es, const char *actual);
#endif
bool
is_dev_type (const char *dev, const char *dev_type, const char *match_type)
{
ASSERT (match_type);
if (!dev)
return false;
if (dev_type)
return !strcmp (dev_type, match_type);
else
return !strncmp (dev, match_type, strlen (match_type));
}
int
dev_type_enum (const char *dev, const char *dev_type)
{
if (is_dev_type (dev, dev_type, "tun"))
return DEV_TYPE_TUN;
else if (is_dev_type (dev, dev_type, "tap"))
return DEV_TYPE_TAP;
else if (is_dev_type (dev, dev_type, "null"))
return DEV_TYPE_NULL;
else
return DEV_TYPE_UNDEF;
}
const char *
dev_type_string (const char *dev, const char *dev_type)
{
switch (dev_type_enum (dev, dev_type))
{
case DEV_TYPE_TUN:
return "tun";
case DEV_TYPE_TAP:
return "tap";
case DEV_TYPE_NULL:
return "null";
default:
return "[unknown-dev-type]";
}
}
/*
* Try to predict the actual TUN/TAP device instance name,
* before the device is actually opened.
*/
const char *
guess_tuntap_dev (const char *dev,
const char *dev_type,
const char *dev_node,
struct gc_arena *gc)
{
#ifdef WIN32
const int dt = dev_type_enum (dev, dev_type);
if (dt == DEV_TYPE_TUN || dt == DEV_TYPE_TAP)
{
return netsh_get_id (dev_node, gc);
}
#endif
/* default case */
return dev;
}
/*
* Called by the open_tun function of OSes to check if we
* explicitly support IPv6.
*
* In this context, explicit means that the OS expects us to
* do something special to the tun socket in order to support
* IPv6, i.e. it is not transparent.
*
* ipv6_explicitly_supported should be set to false if we don't
* have any explicit IPv6 code in the tun device handler.
*
* If ipv6_explicitly_supported is true, then we have explicit
* OS-specific tun dev code for handling IPv6. If so, tt->ipv6
* is set according to the --tun-ipv6 command line option.
*/
static void
ipv6_support (bool ipv6, bool ipv6_explicitly_supported, struct tuntap* tt)
{
tt->ipv6 = false;
if (ipv6_explicitly_supported)
tt->ipv6 = ipv6;
else if (ipv6)
msg (M_WARN, "NOTE: explicit support for IPv6 tun devices is not provided for this OS");
}
/* --ifconfig-nowarn disables some options sanity checking */
static const char ifconfig_warn_how_to_silence[] = "(silence this warning with --ifconfig-nowarn)";
/*
* If !tun, make sure ifconfig_remote_netmask looks
* like a netmask.
*
* If tun, make sure ifconfig_remote_netmask looks
* like an IPv4 address.
*/
static void
ifconfig_sanity_check (bool tun, in_addr_t addr, int topology)
{
struct gc_arena gc = gc_new ();
const bool looks_like_netmask = ((addr & 0xFF000000) == 0xFF000000);
if (tun)
{
if (looks_like_netmask && (topology == TOP_NET30 || topology == TOP_P2P))
msg (M_WARN, "WARNING: Since you are using --dev tun with a point-to-point topology, the second argument to --ifconfig must be an IP address. You are using something (%s) that looks more like a netmask. %s",
print_in_addr_t (addr, 0, &gc),
ifconfig_warn_how_to_silence);
}
else /* tap */
{
if (!looks_like_netmask)
msg (M_WARN, "WARNING: Since you are using --dev tap, the second argument to --ifconfig must be a netmask, for example something like 255.255.255.0. %s",
ifconfig_warn_how_to_silence);
}
gc_free (&gc);
}
/*
* For TAP-style devices, generate a broadcast address.
*/
static in_addr_t
generate_ifconfig_broadcast_addr (in_addr_t local,
in_addr_t netmask)
{
return local | ~netmask;
}
/*
* Check that --local and --remote addresses do not
* clash with ifconfig addresses or subnet.
*/
static void
check_addr_clash (const char *name,
int type,
in_addr_t public,
in_addr_t local,
in_addr_t remote_netmask)
{
struct gc_arena gc = gc_new ();
#if 0
msg (M_INFO, "CHECK_ADDR_CLASH type=%d public=%s local=%s, remote_netmask=%s",
type,
print_in_addr_t (public, 0, &gc),
print_in_addr_t (local, 0, &gc),
print_in_addr_t (remote_netmask, 0, &gc));
#endif
if (public)
{
if (type == DEV_TYPE_TUN)
{
const in_addr_t test_netmask = 0xFFFFFF00;
const in_addr_t public_net = public & test_netmask;
const in_addr_t local_net = local & test_netmask;
const in_addr_t remote_net = remote_netmask & test_netmask;
if (public == local || public == remote_netmask)
msg (M_WARN,
"WARNING: --%s address [%s] conflicts with --ifconfig address pair [%s, %s]. %s",
name,
print_in_addr_t (public, 0, &gc),
print_in_addr_t (local, 0, &gc),
print_in_addr_t (remote_netmask, 0, &gc),
ifconfig_warn_how_to_silence);
if (public_net == local_net || public_net == remote_net)
msg (M_WARN,
"WARNING: potential conflict between --%s address [%s] and --ifconfig address pair [%s, %s] -- this is a warning only that is triggered when local/remote addresses exist within the same /24 subnet as --ifconfig endpoints. %s",
name,
print_in_addr_t (public, 0, &gc),
print_in_addr_t (local, 0, &gc),
print_in_addr_t (remote_netmask, 0, &gc),
ifconfig_warn_how_to_silence);
}
else if (type == DEV_TYPE_TAP)
{
const in_addr_t public_network = public & remote_netmask;
const in_addr_t virtual_network = local & remote_netmask;
if (public_network == virtual_network)
msg (M_WARN,
"WARNING: --%s address [%s] conflicts with --ifconfig subnet [%s, %s] -- local and remote addresses cannot be inside of the --ifconfig subnet. %s",
name,
print_in_addr_t (public, 0, &gc),
print_in_addr_t (local, 0, &gc),
print_in_addr_t (remote_netmask, 0, &gc),
ifconfig_warn_how_to_silence);
}
}
gc_free (&gc);
}
/*
* Issue a warning if ip/netmask (on the virtual IP network) conflicts with
* the settings on the local LAN. This is designed to flag issues where
* (for example) the OpenVPN server LAN is running on 192.168.1.x, but then
* an OpenVPN client tries to connect from a public location that is also running
* off of a router set to 192.168.1.x.
*/
void
check_subnet_conflict (const in_addr_t ip,
const in_addr_t netmask,
const char *prefix)
{
struct gc_arena gc = gc_new ();
in_addr_t lan_gw = 0;
in_addr_t lan_netmask = 0;
if (get_default_gateway (&lan_gw, &lan_netmask))
{
const in_addr_t lan_network = lan_gw & lan_netmask;
const in_addr_t network = ip & netmask;
/* do the two subnets defined by network/netmask and lan_network/lan_netmask intersect? */
if ((network & lan_netmask) == lan_network
|| (lan_network & netmask) == network)
{
msg (M_WARN, "WARNING: potential %s subnet conflict between local LAN [%s/%s] and remote VPN [%s/%s]",
prefix,
print_in_addr_t (lan_network, 0, &gc),
print_in_addr_t (lan_netmask, 0, &gc),
print_in_addr_t (network, 0, &gc),
print_in_addr_t (netmask, 0, &gc));
}
}
gc_free (&gc);
}
void
warn_on_use_of_common_subnets (void)
{
struct gc_arena gc = gc_new ();
in_addr_t lan_gw = 0;
in_addr_t lan_netmask = 0;
if (get_default_gateway (&lan_gw, &lan_netmask))
{
const in_addr_t lan_network = lan_gw & lan_netmask;
if (lan_network == 0xC0A80000 || lan_network == 0xC0A80100)
msg (M_WARN, "NOTE: your local LAN uses the extremely common subnet address 192.168.0.x or 192.168.1.x. Be aware that this might create routing conflicts if you connect to the VPN server from public locations such as internet cafes that use the same subnet.");
}
gc_free (&gc);
}
/*
* Complain if --dev tap and --ifconfig is used on an OS for which
* we don't have a custom tap ifconfig template below.
*/
static void
no_tap_ifconfig ()
{
msg (M_FATAL, "Sorry but you cannot use --dev tap and --ifconfig together on this OS because I have not yet been programmed to understand the appropriate ifconfig syntax to use for TAP-style devices on this OS. Your best alternative is to use an --up script and do the ifconfig command manually.");
}
/*
* Return a string to be used for options compatibility check
* between peers.
*/
const char *
ifconfig_options_string (const struct tuntap* tt, bool remote, bool disable, struct gc_arena *gc)
{
struct buffer out = alloc_buf_gc (256, gc);
if (tt->did_ifconfig_setup && !disable)
{
if (tt->type == DEV_TYPE_TAP || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET))
{
buf_printf (&out, "%s %s",
print_in_addr_t (tt->local & tt->remote_netmask, 0, gc),
print_in_addr_t (tt->remote_netmask, 0, gc));
}
else if (tt->type == DEV_TYPE_TUN)
{
const char *l, *r;
if (remote)
{
r = print_in_addr_t (tt->local, 0, gc);
l = print_in_addr_t (tt->remote_netmask, 0, gc);
}
else
{
l = print_in_addr_t (tt->local, 0, gc);
r = print_in_addr_t (tt->remote_netmask, 0, gc);
}
buf_printf (&out, "%s %s", r, l);
}
else
buf_printf (&out, "[undef]");
}
return BSTR (&out);
}
/*
* Return a status string describing wait state.
*/
const char *
tun_stat (const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc)
{
struct buffer out = alloc_buf_gc (64, gc);
if (tt)
{
if (rwflags & EVENT_READ)
{
buf_printf (&out, "T%s",
(tt->rwflags_debug & EVENT_READ) ? "R" : "r");
#ifdef WIN32
buf_printf (&out, "%s",
overlapped_io_state_ascii (&tt->reads));
#endif
}
if (rwflags & EVENT_WRITE)
{
buf_printf (&out, "T%s",
(tt->rwflags_debug & EVENT_WRITE) ? "W" : "w");
#ifdef WIN32
buf_printf (&out, "%s",
overlapped_io_state_ascii (&tt->writes));
#endif
}
}
else
{
buf_printf (&out, "T?");
}
return BSTR (&out);
}
/*
* Return true for point-to-point topology, false for subnet topology
*/
bool
is_tun_p2p (const struct tuntap *tt)
{
bool tun = false;
if (tt->type == DEV_TYPE_TAP || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET))
tun = false;
else if (tt->type == DEV_TYPE_TUN)
tun = true;
else
msg (M_FATAL, "Error: problem with tun vs. tap setting"); /* JYFIXME -- needs to be caught earlier, in init_tun? */
return tun;
}
/*
* Init tun/tap object.
*
* Set up tuntap structure for ifconfig,
* but don't execute yet.
*/
struct tuntap *
init_tun (const char *dev, /* --dev option */
const char *dev_type, /* --dev-type option */
int topology, /* one of the TOP_x values */
const char *ifconfig_local_parm, /* --ifconfig parm 1 */
const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
in_addr_t local_public,
in_addr_t remote_public,
const bool strict_warn,
struct env_set *es)
{
struct gc_arena gc = gc_new ();
struct tuntap *tt;
ALLOC_OBJ (tt, struct tuntap);
clear_tuntap (tt);
tt->type = dev_type_enum (dev, dev_type);
tt->topology = topology;
if (ifconfig_local_parm && ifconfig_remote_netmask_parm)
{
bool tun = false;
const char *ifconfig_local = NULL;
const char *ifconfig_remote_netmask = NULL;
const char *ifconfig_broadcast = NULL;
/*
* We only handle TUN/TAP devices here, not --dev null devices.
*/
tun = is_tun_p2p (tt);
/*
* Convert arguments to binary IPv4 addresses.
*/
tt->local = getaddr (
GETADDR_RESOLVE
| GETADDR_HOST_ORDER
| GETADDR_FATAL_ON_SIGNAL
| GETADDR_FATAL,
ifconfig_local_parm,
0,
NULL,
NULL);
tt->remote_netmask = getaddr (
(tun ? GETADDR_RESOLVE : 0)
| GETADDR_HOST_ORDER
| GETADDR_FATAL_ON_SIGNAL
| GETADDR_FATAL,
ifconfig_remote_netmask_parm,
0,
NULL,
NULL);
/*
* Look for common errors in --ifconfig parms
*/
if (strict_warn)
{
ifconfig_sanity_check (tt->type == DEV_TYPE_TUN, tt->remote_netmask, tt->topology);
/*
* If local_public or remote_public addresses are defined,
* make sure they do not clash with our virtual subnet.
*/
check_addr_clash ("local",
tt->type,
local_public,
tt->local,
tt->remote_netmask);
check_addr_clash ("remote",
tt->type,
remote_public,
tt->local,
tt->remote_netmask);
if (tt->type == DEV_TYPE_TAP || (tt->type == DEV_TYPE_TUN && tt->topology == TOP_SUBNET))
check_subnet_conflict (tt->local, tt->remote_netmask, "TUN/TAP adapter");
else if (tt->type == DEV_TYPE_TUN)
check_subnet_conflict (tt->local, ~0, "TUN/TAP adapter");
}
/*
* Set ifconfig parameters
*/
ifconfig_local = print_in_addr_t (tt->local, 0, &gc);
ifconfig_remote_netmask = print_in_addr_t (tt->remote_netmask, 0, &gc);
/*
* If TAP-style interface, generate broadcast address.
*/
if (!tun)
{
tt->broadcast = generate_ifconfig_broadcast_addr (tt->local, tt->remote_netmask);
ifconfig_broadcast = print_in_addr_t (tt->broadcast, 0, &gc);
}
/*
* Set environmental variables with ifconfig parameters.
*/
if (es)
{
setenv_str (es, "ifconfig_local", ifconfig_local);
if (tun)
{
setenv_str (es, "ifconfig_remote", ifconfig_remote_netmask);
}
else
{
setenv_str (es, "ifconfig_netmask", ifconfig_remote_netmask);
setenv_str (es, "ifconfig_broadcast", ifconfig_broadcast);
}
}
tt->did_ifconfig_setup = true;
}
gc_free (&gc);
return tt;
}
/*
* Platform specific tun initializations
*/
void
init_tun_post (struct tuntap *tt,
const struct frame *frame,
const struct tuntap_options *options)
{
tt->options = *options;
#ifdef WIN32
overlapped_io_init (&tt->reads, frame, FALSE, true);
overlapped_io_init (&tt->writes, frame, TRUE, true);
tt->rw_handle.read = tt->reads.overlapped.hEvent;
tt->rw_handle.write = tt->writes.overlapped.hEvent;
tt->adapter_index = ~0;
#endif
}
/* execute the ifconfig command through the shell */
void
do_ifconfig (struct tuntap *tt,
const char *actual, /* actual device name */
int tun_mtu,
const struct env_set *es)
{
struct gc_arena gc = gc_new ();
if (tt->did_ifconfig_setup)
{
bool tun = false;
const char *ifconfig_local = NULL;
const char *ifconfig_remote_netmask = NULL;
const char *ifconfig_broadcast = NULL;
struct argv argv;
argv_init (&argv);
/*
* We only handle TUN/TAP devices here, not --dev null devices.
*/
tun = is_tun_p2p (tt);
/*
* Set ifconfig parameters
*/
ifconfig_local = print_in_addr_t (tt->local, 0, &gc);
ifconfig_remote_netmask = print_in_addr_t (tt->remote_netmask, 0, &gc);
/*
* If TAP-style device, generate broadcast address.
*/
if (!tun)
ifconfig_broadcast = print_in_addr_t (tt->broadcast, 0, &gc);
#ifdef ENABLE_MANAGEMENT
if (management)
{
management_set_state (management,
OPENVPN_STATE_ASSIGN_IP,
NULL,
tt->local,
0);
}
#endif
#if defined(TARGET_LINUX)
#ifdef CONFIG_FEATURE_IPROUTE
/*
* Set the MTU for the device
*/
argv_printf (&argv,
"%s link set dev %s up mtu %d",
iproute_path,
actual,
tun_mtu
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Linux ip link set failed");
if (tun) {
/*
* Set the address for the device
*/
argv_printf (&argv,
"%s addr add dev %s local %s peer %s",
iproute_path,
actual,
ifconfig_local,
ifconfig_remote_netmask
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Linux ip addr add failed");
} else {
argv_printf (&argv,
"%s addr add dev %s %s/%d broadcast %s",
iproute_path,
actual,
ifconfig_local,
count_netmask_bits(ifconfig_remote_netmask),
ifconfig_broadcast
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Linux ip addr add failed");
}
tt->did_ifconfig = true;
#else
if (tun)
argv_printf (&argv,
"%s %s %s pointopoint %s mtu %d",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
else
argv_printf (&argv,
"%s %s %s netmask %s mtu %d broadcast %s",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu,
ifconfig_broadcast
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Linux ifconfig failed");
tt->did_ifconfig = true;
#endif /*CONFIG_FEATURE_IPROUTE*/
#elif defined(TARGET_SOLARIS)
/* Solaris 2.6 (and 7?) cannot set all parameters in one go...
* example:
* ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 up
* ifconfig tun2 netmask 255.255.255.255
*/
if (tun)
{
argv_printf (&argv,
"%s %s %s %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
argv_msg (M_INFO, &argv);
if (!openvpn_execve_check (&argv, es, 0, "Solaris ifconfig phase-1 failed"))
solaris_error_close (tt, es, actual);
argv_printf (&argv,
"%s %s netmask 255.255.255.255",
IFCONFIG_PATH,
actual
);
}
else
no_tap_ifconfig ();
argv_msg (M_INFO, &argv);
if (!openvpn_execve_check (&argv, es, 0, "Solaris ifconfig phase-2 failed"))
solaris_error_close (tt, es, actual);
tt->did_ifconfig = true;
#elif defined(TARGET_OPENBSD)
/*
* OpenBSD tun devices appear to be persistent by default. It seems in order
* to make this work correctly, we need to delete the previous instance
* (if it exists), and re-ifconfig. Let me know if you know a better way.
*/
argv_printf (&argv,
"%s %s destroy",
IFCONFIG_PATH,
actual);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, 0, NULL);
argv_printf (&argv,
"%s %s create",
IFCONFIG_PATH,
actual);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, 0, NULL);
msg (M_INFO, "NOTE: Tried to delete pre-existing tun/tap instance -- No Problem if failure");
/* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
if (tun)
argv_printf (&argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
else
argv_printf (&argv,
"%s %s %s netmask %s mtu %d broadcast %s link0",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu,
ifconfig_broadcast
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "OpenBSD ifconfig failed");
tt->did_ifconfig = true;
#elif defined(TARGET_NETBSD)
if (tun)
argv_printf (&argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
else
/*
* NetBSD has distinct tun and tap devices
* so we don't need the "link0" extra parameter to specify we want to do
* tunneling at the ethernet level
*/
argv_printf (&argv,
"%s %s %s netmask %s mtu %d broadcast %s",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu,
ifconfig_broadcast
);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "NetBSD ifconfig failed");
tt->did_ifconfig = true;
#elif defined(TARGET_DARWIN)
/*
* Darwin (i.e. Mac OS X) seems to exhibit similar behaviour to OpenBSD...
*/
argv_printf (&argv,
"%s %s delete",
IFCONFIG_PATH,
actual);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, 0, NULL);
msg (M_INFO, "NOTE: Tried to delete pre-existing tun/tap instance -- No Problem if failure");
/* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
if (tun)
argv_printf (&argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
else
{
if (tt->topology == TOP_SUBNET)
argv_printf (&argv,
"%s %s %s %s netmask %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
else
argv_printf (&argv,
"%s %s %s netmask %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "Mac OS X ifconfig failed");
tt->did_ifconfig = true;
/* Add a network route for the local tun interface */
if (!tun && tt->topology == TOP_SUBNET)
{
struct route r;
CLEAR (r);
r.defined = true;
r.network = tt->local & tt->remote_netmask;
r.netmask = tt->remote_netmask;
r.gateway = tt->local;
add_route (&r, tt, 0, es);
}
#elif defined(TARGET_FREEBSD)||defined(TARGET_DRAGONFLY)
/* example: ifconfig tun2 10.2.0.2 10.2.0.1 mtu 1450 netmask 255.255.255.255 up */
if (tun)
argv_printf (&argv,
"%s %s %s %s mtu %d netmask 255.255.255.255 up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
else {
if (tt->topology == TOP_SUBNET)
argv_printf (&argv,
"%s %s %s netmask %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
else
argv_printf (&argv,
"%s %s %s netmask %s mtu %d up",
IFCONFIG_PATH,
actual,
ifconfig_local,
ifconfig_remote_netmask,
tun_mtu
);
}
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, S_FATAL, "FreeBSD ifconfig failed");
tt->did_ifconfig = true;
/* Add a network route for the local tun interface */
if (!tun && tt->topology == TOP_SUBNET)
{
struct route r;
CLEAR (r);
r.defined = true;
r.network = tt->local & tt->remote_netmask;
r.netmask = tt->remote_netmask;
r.gateway = tt->local;
add_route (&r, tt, 0, es);
}
#elif defined (WIN32)
{
/*
* Make sure that both ifconfig addresses are part of the
* same .252 subnet.
*/
if (tun)
{
verify_255_255_255_252 (tt->local, tt->remote_netmask);
tt->adapter_netmask = ~3;
}
else
{
tt->adapter_netmask = tt->remote_netmask;
}
switch (tt->options.ip_win32_type)
{
case IPW32_SET_MANUAL:
msg (M_INFO, "******** NOTE: Please manually set the IP/netmask of '%s' to %s/%s (if it is not already set)",
actual,
ifconfig_local,
print_in_addr_t (tt->adapter_netmask, 0, &gc));
break;
case IPW32_SET_NETSH:
if (!strcmp (actual, "NULL"))
msg (M_FATAL, "Error: When using --ip-win32 netsh, if you have more than one TAP-Win32 adapter, you must also specify --dev-node");
netsh_ifconfig (&tt->options,
actual,
tt->local,
tt->adapter_netmask,
NI_IP_NETMASK|NI_OPTIONS);
break;
}
tt->did_ifconfig = true;
}
#else
msg (M_FATAL, "Sorry, but I don't know how to do 'ifconfig' commands on this operating system. You should ifconfig your TUN/TAP device manually or use an --up script.");
#endif
argv_reset (&argv);
}
gc_free (&gc);
}
void
clear_tuntap (struct tuntap *tuntap)
{
CLEAR (*tuntap);
#ifdef WIN32
tuntap->hand = NULL;
#else
tuntap->fd = -1;
#endif
#ifdef TARGET_SOLARIS
tuntap->ip_fd = -1;
#endif
tuntap->ipv6 = false;
}
static void
open_null (struct tuntap *tt)
{
tt->actual_name = string_alloc ("null", NULL);
}
#ifndef WIN32
static void
open_tun_generic (const char *dev, const char *dev_type, const char *dev_node,
bool ipv6, bool ipv6_explicitly_supported, bool dynamic,
struct tuntap *tt)
{
char tunname[256];
char dynamic_name[256];
bool dynamic_opened = false;
ipv6_support (ipv6, ipv6_explicitly_supported, tt);
if (tt->type == DEV_TYPE_NULL)
{
open_null (tt);
}
else
{
/*
* --dev-node specified, so open an explicit device node
*/
if (dev_node)
{
openvpn_snprintf (tunname, sizeof (tunname), "%s", dev_node);
}
else
{
/*
* dynamic open is indicated by --dev specified without
* explicit unit number. Try opening /dev/[dev]n
* where n = [0, 255].
*/
if (dynamic && !has_digit((unsigned char *)dev))
{
int i;
for (i = 0; i < 256; ++i)
{
openvpn_snprintf (tunname, sizeof (tunname),
"/dev/%s%d", dev, i);
openvpn_snprintf (dynamic_name, sizeof (dynamic_name),
"%s%d", dev, i);
if ((tt->fd = open (tunname, O_RDWR)) > 0)
{
dynamic_opened = true;
break;
}
msg (D_READ_WRITE | M_ERRNO, "Tried opening %s (failed)", tunname);
}
if (!dynamic_opened)
msg (M_FATAL, "Cannot allocate TUN/TAP dev dynamically");
}
/*
* explicit unit number specified
*/
else
{
openvpn_snprintf (tunname, sizeof (tunname), "/dev/%s", dev);
}
}
if (!dynamic_opened)
{
if ((tt->fd = open (tunname, O_RDWR)) < 0)
msg (M_ERR, "Cannot open TUN/TAP dev %s", tunname);
}
set_nonblock (tt->fd);
set_cloexec (tt->fd); /* don't pass fd to scripts */
msg (M_INFO, "TUN/TAP device %s opened", tunname);
/* tt->actual_name is passed to up and down scripts and used as the ifconfig dev name */
tt->actual_name = string_alloc (dynamic_opened ? dynamic_name : dev, NULL);
}
}
static void
close_tun_generic (struct tuntap *tt)
{
if (tt->fd >= 0)
close (tt->fd);
if (tt->actual_name)
free (tt->actual_name);
clear_tuntap (tt);
}
#endif
#if defined(TARGET_LINUX)
#ifdef HAVE_LINUX_IF_TUN_H /* New driver support */
#ifndef HAVE_LINUX_SOCKIOS_H
#error header file linux/sockios.h required
#endif
#if defined(HAVE_TUN_PI) && defined(HAVE_IPHDR) && defined(HAVE_IOVEC) && defined(ETH_P_IPV6) && defined(ETH_P_IP) && defined(HAVE_READV) && defined(HAVE_WRITEV)
#define LINUX_IPV6 1
/* #warning IPv6 ON */
#else
#define LINUX_IPV6 0
/* #warning IPv6 OFF */
#endif
#if !PEDANTIC
void
open_tun (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, struct tuntap *tt)
{
struct ifreq ifr;
/*
* Set tt->ipv6 to true if
* (a) we have the capability of supporting --tun-ipv6, and
* (b) --tun-ipv6 was specified.
*/
ipv6_support (ipv6, LINUX_IPV6, tt);
/*
* We handle --dev null specially, we do not open /dev/null for this.
*/
if (tt->type == DEV_TYPE_NULL)
{
open_null (tt);
}
else
{
/*
* Process --dev-node
*/
const char *node = dev_node;
if (!node)
node = "/dev/net/tun";
/*
* Open the interface
*/
if ((tt->fd = open (node, O_RDWR)) < 0)
{
msg (M_WARN | M_ERRNO, "Note: Cannot open TUN/TAP dev %s", node);
goto linux_2_2_fallback;
}
/*
* Process --tun-ipv6
*/
CLEAR (ifr);
if (!tt->ipv6)
ifr.ifr_flags = IFF_NO_PI;
#if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
ifr.ifr_flags |= IFF_ONE_QUEUE;
#endif
/*
* Figure out if tun or tap device
*/
if (tt->type == DEV_TYPE_TUN)
{
ifr.ifr_flags |= IFF_TUN;
}
else if (tt->type == DEV_TYPE_TAP)
{
ifr.ifr_flags |= IFF_TAP;
}
else
{
msg (M_FATAL, "I don't recognize device %s as a tun or tap device",
dev);
}
/*
* Set an explicit name, if --dev is not tun or tap
*/
if (strcmp(dev, "tun") && strcmp(dev, "tap"))
strncpynt (ifr.ifr_name, dev, IFNAMSIZ);
/*
* Use special ioctl that configures tun/tap device with the parms
* we set in ifr
*/
if (ioctl (tt->fd, TUNSETIFF, (void *) &ifr) < 0)
{
msg (M_WARN | M_ERRNO, "Note: Cannot ioctl TUNSETIFF %s", dev);
goto linux_2_2_fallback;
}
msg (M_INFO, "TUN/TAP device %s opened", ifr.ifr_name);
/*
* Try making the TX send queue bigger
*/
#if defined(IFF_ONE_QUEUE) && defined(SIOCSIFTXQLEN)
{
struct ifreq netifr;
int ctl_fd;
if ((ctl_fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
{
CLEAR (netifr);
strncpynt (netifr.ifr_name, ifr.ifr_name, IFNAMSIZ);
netifr.ifr_qlen = tt->options.txqueuelen;
if (ioctl (ctl_fd, SIOCSIFTXQLEN, (void *) &netifr) >= 0)
msg (D_OSBUF, "TUN/TAP TX queue length set to %d", tt->options.txqueuelen);
else
msg (M_WARN | M_ERRNO, "Note: Cannot set tx queue length on %s", ifr.ifr_name);
close (ctl_fd);
}
else
{
msg (M_WARN | M_ERRNO, "Note: Cannot open control socket on %s", ifr.ifr_name);
}
}
#endif
set_nonblock (tt->fd);
set_cloexec (tt->fd);
tt->actual_name = string_alloc (ifr.ifr_name, NULL);
}
return;
linux_2_2_fallback:
msg (M_INFO, "Note: Attempting fallback to kernel 2.2 TUN/TAP interface");
if (tt->fd >= 0)
{
close (tt->fd);
tt->fd = -1;
}
open_tun_generic (dev, dev_type, dev_node, ipv6, false, true, tt);
}
#else
void
open_tun (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, struct tuntap *tt)
{
ASSERT (0);
}
#endif
#else
void
open_tun (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, struct tuntap *tt)
{
open_tun_generic (dev, dev_type, dev_node, ipv6, false, true, tt);
}
#endif /* HAVE_LINUX_IF_TUN_H */
#ifdef TUNSETPERSIST
/*
* This can be removed in future
* when all systems will use newer
* linux-headers
*/
#ifndef TUNSETOWNER
#define TUNSETOWNER _IOW('T', 204, int)
#endif
#ifndef TUNSETGROUP
#define TUNSETGROUP _IOW('T', 206, int)
#endif
void
tuncfg (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, int persist_mode, const char *username, const char *groupname, const struct tuntap_options *options)
{
struct tuntap *tt;
ALLOC_OBJ (tt, struct tuntap);
clear_tuntap (tt);
tt->type = dev_type_enum (dev, dev_type);
tt->options = *options;
open_tun (dev, dev_type, dev_node, ipv6, tt);
if (ioctl (tt->fd, TUNSETPERSIST, persist_mode) < 0)
msg (M_ERR, "Cannot ioctl TUNSETPERSIST(%d) %s", persist_mode, dev);
if (username != NULL)
{
struct user_state user_state;
if (!get_user (username, &user_state))
msg (M_ERR, "Cannot get user entry for %s", username);
else
if (ioctl (tt->fd, TUNSETOWNER, user_state.pw->pw_uid) < 0)
msg (M_ERR, "Cannot ioctl TUNSETOWNER(%s) %s", username, dev);
}
if (groupname != NULL)
{
struct group_state group_state;
if (!get_group (groupname, &group_state))
msg (M_ERR, "Cannot get group entry for %s", groupname);
else
if (ioctl (tt->fd, TUNSETGROUP, group_state.gr->gr_gid) < 0)
msg (M_ERR, "Cannot ioctl TUNSETOWNER(%s) %s", groupname, dev);
}
close_tun (tt);
msg (M_INFO, "Persist state set to: %s", (persist_mode ? "ON" : "OFF"));
}
#endif /* TUNSETPERSIST */
void
close_tun (struct tuntap *tt)
{
if (tt)
{
if (tt->type != DEV_TYPE_NULL && tt->did_ifconfig)
{
struct argv argv;
struct gc_arena gc = gc_new ();
argv_init (&argv);
#ifdef CONFIG_FEATURE_IPROUTE
if (is_tun_p2p (tt))
{
argv_printf (&argv,
"%s addr del dev %s local %s peer %s",
iproute_path,
tt->actual_name,
print_in_addr_t (tt->local, 0, &gc),
print_in_addr_t (tt->remote_netmask, 0, &gc)
);
}
else
{
argv_printf (&argv,
"%s addr del dev %s %s/%d",
iproute_path,
tt->actual_name,
print_in_addr_t (tt->local, 0, &gc),
count_netmask_bits(print_in_addr_t (tt->remote_netmask, 0, &gc))
);
}
#else
argv_printf (&argv,
"%s %s 0.0.0.0",
IFCONFIG_PATH,
tt->actual_name
);
#endif
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, NULL, 0, "Linux ip addr del failed");
argv_reset (&argv);
gc_free (&gc);
}
close_tun_generic (tt);
free (tt);
}
}
int
write_tun (struct tuntap* tt, uint8_t *buf, int len)
{
#if LINUX_IPV6
if (tt->ipv6)
{
struct tun_pi pi;
struct iphdr *iph;
struct iovec vect[2];
int ret;
iph = (struct iphdr *)buf;
pi.flags = 0;
if(iph->version == 6)
pi.proto = htons(ETH_P_IPV6);
else
pi.proto = htons(ETH_P_IP);
vect[0].iov_len = sizeof(pi);
vect[0].iov_base = π
vect[1].iov_len = len;
vect[1].iov_base = buf;
ret = writev(tt->fd, vect, 2);
return(ret - sizeof(pi));
}
else
#endif
return write (tt->fd, buf, len);
}
int
read_tun (struct tuntap* tt, uint8_t *buf, int len)
{
#if LINUX_IPV6
if (tt->ipv6)
{
struct iovec vect[2];
struct tun_pi pi;
int ret;
vect[0].iov_len = sizeof(pi);
vect[0].iov_base = π
vect[1].iov_len = len;
vect[1].iov_base = buf;
ret = readv(tt->fd, vect, 2);
return(ret - sizeof(pi));
}
else
#endif
return read (tt->fd, buf, len);
}
#elif defined(TARGET_SOLARIS)
#ifndef TUNNEWPPA
#error I need the symbol TUNNEWPPA from net/if_tun.h
#endif
void
open_tun (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, struct tuntap *tt)
{
int if_fd, muxid, ppa = -1;
struct ifreq ifr;
const char *ptr;
const char *ip_node;
const char *dev_tuntap_type;
int link_type;
bool is_tun;
ipv6_support (ipv6, false, tt);
if (tt->type == DEV_TYPE_NULL)
{
open_null (tt);
return;
}
if (tt->type == DEV_TYPE_TUN)
{
ip_node = "/dev/udp";
if (!dev_node)
dev_node = "/dev/tun";
dev_tuntap_type = "tun";
link_type = I_PLINK;
is_tun = true;
}
else if (tt->type == DEV_TYPE_TAP)
{
ip_node = "/dev/ip";
if (!dev_node)
dev_node = "/dev/tap";
dev_tuntap_type = "tap";
link_type = I_PLINK; /* was: I_LINK */
is_tun = false;
}
else
{
msg (M_FATAL, "I don't recognize device %s as a tun or tap device",
dev);
}
/* get unit number */
if (*dev)
{
ptr = dev;
while (*ptr && !isdigit ((int) *ptr))
ptr++;
ppa = atoi (ptr);
}
if ((tt->ip_fd = open (ip_node, O_RDWR, 0)) < 0)
msg (M_ERR, "Can't open %s", ip_node);
if ((tt->fd = open (dev_node, O_RDWR, 0)) < 0)
msg (M_ERR, "Can't open %s", dev_node);
/* Assign a new PPA and get its unit number. */
if ((ppa = ioctl (tt->fd, TUNNEWPPA, ppa)) < 0)
msg (M_ERR, "Can't assign new interface");
if ((if_fd = open (dev_node, O_RDWR, 0)) < 0)
msg (M_ERR, "Can't open %s (2)", dev_node);
if (ioctl (if_fd, I_PUSH, "ip") < 0)
msg (M_ERR, "Can't push IP module");
/* Assign ppa according to the unit number returned by tun device */
if (ioctl (if_fd, IF_UNITSEL, (char *) &ppa) < 0)
msg (M_ERR, "Can't set PPA %d", ppa);
if ((muxid = ioctl (tt->ip_fd, link_type, if_fd)) < 0)
msg (M_ERR, "Can't link %s device to IP", dev_tuntap_type);
close (if_fd);
tt->actual_name = (char *) malloc (32);
check_malloc_return (tt->actual_name);
openvpn_snprintf (tt->actual_name, 32, "%s%d", dev_tuntap_type, ppa);
CLEAR (ifr);
strncpynt (ifr.ifr_name, tt->actual_name, sizeof (ifr.ifr_name));
ifr.ifr_ip_muxid = muxid;
if (ioctl (tt->ip_fd, SIOCSIFMUXID, &ifr) < 0)
{
ioctl (tt->ip_fd, I_PUNLINK, muxid);
msg (M_ERR, "Can't set multiplexor id");
}
set_nonblock (tt->fd);
set_cloexec (tt->fd);
set_cloexec (tt->ip_fd);
msg (M_INFO, "TUN/TAP device %s opened", tt->actual_name);
}
static void
solaris_close_tun (struct tuntap *tt)
{
if (tt)
{
if (tt->ip_fd >= 0)
{
struct ifreq ifr;
CLEAR (ifr);
strncpynt (ifr.ifr_name, tt->actual_name, sizeof (ifr.ifr_name));
if (ioctl (tt->ip_fd, SIOCGIFFLAGS, &ifr) < 0)
msg (M_WARN | M_ERRNO, "Can't get iface flags");
if (ioctl (tt->ip_fd, SIOCGIFMUXID, &ifr) < 0)
msg (M_WARN | M_ERRNO, "Can't get multiplexor id");
if (ioctl (tt->ip_fd, I_PUNLINK, ifr.ifr_ip_muxid) < 0)
msg (M_WARN | M_ERRNO, "Can't unlink interface");
close (tt->ip_fd);
tt->ip_fd = -1;
}
if (tt->fd >= 0)
{
close (tt->fd);
tt->fd = -1;
}
}
}
/*
* Close TUN device.
*/
void
close_tun (struct tuntap *tt)
{
if (tt)
{
solaris_close_tun (tt);
if (tt->actual_name)
free (tt->actual_name);
clear_tuntap (tt);
free (tt);
}
}
static void
solaris_error_close (struct tuntap *tt, const struct env_set *es, const char *actual)
{
struct argv argv;
argv_init (&argv);
argv_printf (&argv,
"%s %s unplumb",
IFCONFIG_PATH,
actual);
argv_msg (M_INFO, &argv);
openvpn_execve_check (&argv, es, 0, "Solaris ifconfig unplumb failed");
close_tun (tt);
msg (M_FATAL, "Solaris ifconfig failed");
argv_reset (&argv);
}
int
write_tun (struct tuntap* tt, uint8_t *buf, int len)
{
struct strbuf sbuf;
sbuf.len = len;
sbuf.buf = (char *)buf;
return putmsg (tt->fd, NULL, &sbuf, 0) >= 0 ? sbuf.len : -1;
}
int
read_tun (struct tuntap* tt, uint8_t *buf, int len)
{
struct strbuf sbuf;
int f = 0;
sbuf.maxlen = len;
sbuf.buf = (char *)buf;
return getmsg (tt->fd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
}
#elif defined(TARGET_OPENBSD)
#if !defined(HAVE_READV) || !defined(HAVE_WRITEV)
#error openbsd build requires readv & writev library functions
#endif
/*
* OpenBSD has a slightly incompatible TUN device from
* the rest of the world, in that it prepends a
* uint32 to the beginning of the IP header
* to designate the protocol (why not just
* look at the version field in the IP header to
* determine v4 or v6?).
*
* We strip off this field on reads and
* put it back on writes.
*
* I have not tested TAP devices on OpenBSD,
* but I have conditionalized the special
* TUN handling code described above to
* go away for TAP devices.
*/
void
open_tun (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, struct tuntap *tt)
{
open_tun_generic (dev, dev_type, dev_node, ipv6, true, true, tt);
/* Enable multicast on the interface */
if (tt->fd >= 0)
{
struct tuninfo info;
if (ioctl (tt->fd, TUNGIFINFO, &info) < 0) {
msg (M_WARN | M_ERRNO, "Can't get interface info: %s",
strerror(errno));
}
#ifdef IFF_MULTICAST /* openbsd 4.x doesn't have this */
info.flags |= IFF_MULTICAST;
#endif
if (ioctl (tt->fd, TUNSIFINFO, &info) < 0) {
msg (M_WARN | M_ERRNO, "Can't set interface info: %s",
strerror(errno));
}
}
}
void
close_tun (struct tuntap* tt)
{
if (tt)
{
close_tun_generic (tt);
free (tt);
}
}
static inline int
openbsd_modify_read_write_return (int len)
{
if (len > 0)
return len > sizeof (u_int32_t) ? len - sizeof (u_int32_t) : 0;
else
return len;
}
int
write_tun (struct tuntap* tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
struct ip *iph;
iph = (struct ip *) buf;
if (tt->ipv6 && iph->ip_v == 6)
type = htonl (AF_INET6);
else
type = htonl (AF_INET);
iv[0].iov_base = &type;
iv[0].iov_len = sizeof (type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return openbsd_modify_read_write_return (writev (tt->fd, iv, 2));
}
else
return write (tt->fd, buf, len);
}
int
read_tun (struct tuntap* tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
iv[0].iov_base = &type;
iv[0].iov_len = sizeof (type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return openbsd_modify_read_write_return (readv (tt->fd, iv, 2));
}
else
return read (tt->fd, buf, len);
}
#elif defined(TARGET_NETBSD)
/*
* NetBSD does not support IPv6 on tun out of the box,
* but there exists a patch. When this patch is applied,
* only two things are left to openvpn:
* 1. Activate multicasting (this has already been done
* before by the kernel, but we make sure that nobody
* has deactivated multicasting inbetween.
* 2. Deactivate "link layer mode" (otherwise NetBSD
* prepends the address family to the packet, and we
* would run into the same trouble as with OpenBSD.
*/
void
open_tun (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, struct tuntap *tt)
{
open_tun_generic (dev, dev_type, dev_node, ipv6, true, true, tt);
if (tt->fd >= 0)
{
int i = IFF_POINTOPOINT|IFF_MULTICAST;
ioctl (tt->fd, TUNSIFMODE, &i); /* multicast on */
i = 0;
ioctl (tt->fd, TUNSLMODE, &i); /* link layer mode off */
}
}
void
close_tun (struct tuntap *tt)
{
if (tt)
{
close_tun_generic (tt);
free (tt);
}
}
int
write_tun (struct tuntap* tt, uint8_t *buf, int len)
{
return write (tt->fd, buf, len);
}
int
read_tun (struct tuntap* tt, uint8_t *buf, int len)
{
return read (tt->fd, buf, len);
}
#elif defined(TARGET_FREEBSD)
static inline int
freebsd_modify_read_write_return (int len)
{
if (len > 0)
return len > sizeof (u_int32_t) ? len - sizeof (u_int32_t) : 0;
else
return len;
}
void
open_tun (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, struct tuntap *tt)
{
open_tun_generic (dev, dev_type, dev_node, ipv6, true, true, tt);
if (tt->fd >= 0 && tt->type == DEV_TYPE_TUN)
{
int i = 0;
i = tt->topology == TOP_SUBNET ? IFF_BROADCAST : IFF_POINTOPOINT;
i |= IFF_MULTICAST;
if (ioctl (tt->fd, TUNSIFMODE, &i) < 0) {
msg (M_WARN | M_ERRNO, "ioctl(TUNSIFMODE): %s", strerror(errno));
}
i = 1;
if (ioctl (tt->fd, TUNSIFHEAD, &i) < 0) {
msg (M_WARN | M_ERRNO, "ioctl(TUNSIFHEAD): %s", strerror(errno));
}
}
}
void
close_tun (struct tuntap *tt)
{
if (tt)
{
close_tun_generic (tt);
free (tt);
}
}
int
write_tun (struct tuntap* tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
struct ip *iph;
iph = (struct ip *) buf;
if (tt->ipv6 && iph->ip_v == 6)
type = htonl (AF_INET6);
else
type = htonl (AF_INET);
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof (type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return freebsd_modify_read_write_return (writev (tt->fd, iv, 2));
}
else
return write (tt->fd, buf, len);
}
int
read_tun (struct tuntap* tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof (type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return freebsd_modify_read_write_return (readv (tt->fd, iv, 2));
}
else
return read (tt->fd, buf, len);
}
#elif defined(TARGET_DRAGONFLY)
static inline int
dragonfly_modify_read_write_return (int len)
{
if (len > 0)
return len > sizeof (u_int32_t) ? len - sizeof (u_int32_t) : 0;
else
return len;
}
void
open_tun (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, struct tuntap *tt)
{
open_tun_generic (dev, dev_type, dev_node, ipv6, true, true, tt);
if (tt->fd >= 0)
{
int i = 0;
/* Disable extended modes */
ioctl (tt->fd, TUNSLMODE, &i);
i = 1;
ioctl (tt->fd, TUNSIFHEAD, &i);
}
}
void
close_tun (struct tuntap *tt)
{
if (tt)
{
close_tun_generic (tt);
free (tt);
}
}
int
write_tun (struct tuntap* tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
struct ip *iph;
iph = (struct ip *) buf;
if (tt->ipv6 && iph->ip_v == 6)
type = htonl (AF_INET6);
else
type = htonl (AF_INET);
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof (type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return dragonfly_modify_read_write_return (writev (tt->fd, iv, 2));
}
else
return write (tt->fd, buf, len);
}
int
read_tun (struct tuntap* tt, uint8_t *buf, int len)
{
if (tt->type == DEV_TYPE_TUN)
{
u_int32_t type;
struct iovec iv[2];
iv[0].iov_base = (char *)&type;
iv[0].iov_len = sizeof (type);
iv[1].iov_base = buf;
iv[1].iov_len = len;
return dragonfly_modify_read_write_return (readv (tt->fd, iv, 2));
}
else
return read (tt->fd, buf, len);
}
#elif defined(WIN32)
int
tun_read_queue (struct tuntap *tt, int maxsize)
{
if (tt->reads.iostate == IOSTATE_INITIAL)
{
DWORD len;
BOOL status;
int err;
/* reset buf to its initial state */
tt->reads.buf = tt->reads.buf_init;
len = maxsize ? maxsize : BLEN (&tt->reads.buf);
ASSERT (len <= BLEN (&tt->reads.buf));
/* the overlapped read will signal this event on I/O completion */
ASSERT (ResetEvent (tt->reads.overlapped.hEvent));
status = ReadFile(
tt->hand,
BPTR (&tt->reads.buf),
len,
&tt->reads.size,
&tt->reads.overlapped
);
if (status) /* operation completed immediately? */
{
/* since we got an immediate return, we must signal the event object ourselves */
ASSERT (SetEvent (tt->reads.overlapped.hEvent));
tt->reads.iostate = IOSTATE_IMMEDIATE_RETURN;
tt->reads.status = 0;
dmsg (D_WIN32_IO, "WIN32 I/O: TAP Read immediate return [%d,%d]",
(int) len,
(int) tt->reads.size);
}
else
{
err = GetLastError ();
if (err == ERROR_IO_PENDING) /* operation queued? */
{
tt->reads.iostate = IOSTATE_QUEUED;
tt->reads.status = err;
dmsg (D_WIN32_IO, "WIN32 I/O: TAP Read queued [%d]",
(int) len);
}
else /* error occurred */
{
struct gc_arena gc = gc_new ();
ASSERT (SetEvent (tt->reads.overlapped.hEvent));
tt->reads.iostate = IOSTATE_IMMEDIATE_RETURN;
tt->reads.status = err;
dmsg (D_WIN32_IO, "WIN32 I/O: TAP Read error [%d] : %s",
(int) len,
strerror_win32 (status, &gc));
gc_free (&gc);
}
}
}
return tt->reads.iostate;
}
int
tun_write_queue (struct tuntap *tt, struct buffer *buf)
{
if (tt->writes.iostate == IOSTATE_INITIAL)
{
BOOL status;
int err;
/* make a private copy of buf */
tt->writes.buf = tt->writes.buf_init;
tt->writes.buf.len = 0;
ASSERT (buf_copy (&tt->writes.buf, buf));
/* the overlapped write will signal this event on I/O completion */
ASSERT (ResetEvent (tt->writes.overlapped.hEvent));
status = WriteFile(
tt->hand,
BPTR (&tt->writes.buf),
BLEN (&tt->writes.buf),
&tt->writes.size,
&tt->writes.overlapped
);
if (status) /* operation completed immediately? */
{
tt->writes.iostate = IOSTATE_IMMEDIATE_RETURN;
/* since we got an immediate return, we must signal the event object ourselves */
ASSERT (SetEvent (tt->writes.overlapped.hEvent));
tt->writes.status = 0;
dmsg (D_WIN32_IO, "WIN32 I/O: TAP Write immediate return [%d,%d]",
BLEN (&tt->writes.buf),
(int) tt->writes.size);
}
else
{
err = GetLastError ();
if (err == ERROR_IO_PENDING) /* operation queued? */
{
tt->writes.iostate = IOSTATE_QUEUED;
tt->writes.status = err;
dmsg (D_WIN32_IO, "WIN32 I/O: TAP Write queued [%d]",
BLEN (&tt->writes.buf));
}
else /* error occurred */
{
struct gc_arena gc = gc_new ();
ASSERT (SetEvent (tt->writes.overlapped.hEvent));
tt->writes.iostate = IOSTATE_IMMEDIATE_RETURN;
tt->writes.status = err;
dmsg (D_WIN32_IO, "WIN32 I/O: TAP Write error [%d] : %s",
BLEN (&tt->writes.buf),
strerror_win32 (err, &gc));
gc_free (&gc);
}
}
}
return tt->writes.iostate;
}
int
tun_finalize (
HANDLE h,
struct overlapped_io *io,
struct buffer *buf)
{
int ret = -1;
BOOL status;
switch (io->iostate)
{
case IOSTATE_QUEUED:
status = GetOverlappedResult(
h,
&io->overlapped,
&io->size,
FALSE
);
if (status)
{
/* successful return for a queued operation */
if (buf)
*buf = io->buf;
ret = io->size;
io->iostate = IOSTATE_INITIAL;
ASSERT (ResetEvent (io->overlapped.hEvent));
dmsg (D_WIN32_IO, "WIN32 I/O: TAP Completion success [%d]", ret);
}
else
{
/* error during a queued operation */
ret = -1;
if (GetLastError() != ERROR_IO_INCOMPLETE)
{
/* if no error (i.e. just not finished yet),
then DON'T execute this code */
io->iostate = IOSTATE_INITIAL;
ASSERT (ResetEvent (io->overlapped.hEvent));
msg (D_WIN32_IO | M_ERRNO, "WIN32 I/O: TAP Completion error");
}
}
break;
case IOSTATE_IMMEDIATE_RETURN:
io->iostate = IOSTATE_INITIAL;
ASSERT (ResetEvent (io->overlapped.hEvent));
if (io->status)
{
/* error return for a non-queued operation */
SetLastError (io->status);
ret = -1;
msg (D_WIN32_IO | M_ERRNO, "WIN32 I/O: TAP Completion non-queued error");
}
else
{
/* successful return for a non-queued operation */
if (buf)
*buf = io->buf;
ret = io->size;
dmsg (D_WIN32_IO, "WIN32 I/O: TAP Completion non-queued success [%d]", ret);
}
break;
case IOSTATE_INITIAL: /* were we called without proper queueing? */
SetLastError (ERROR_INVALID_FUNCTION);
ret = -1;
dmsg (D_WIN32_IO, "WIN32 I/O: TAP Completion BAD STATE");
break;
default:
ASSERT (0);
}
if (buf)
buf->len = ret;
return ret;
}
const struct tap_reg *
get_tap_reg (struct gc_arena *gc)
{
HKEY adapter_key;
LONG status;
DWORD len;
struct tap_reg *first = NULL;
struct tap_reg *last = NULL;
int i = 0;
status = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
ADAPTER_KEY,
0,
KEY_READ,
&adapter_key);
if (status != ERROR_SUCCESS)
msg (M_FATAL, "Error opening registry key: %s", ADAPTER_KEY);
while (true)
{
char enum_name[256];
char unit_string[256];
HKEY unit_key;
char component_id_string[] = "ComponentId";
char component_id[256];
char net_cfg_instance_id_string[] = "NetCfgInstanceId";
char net_cfg_instance_id[256];
DWORD data_type;
len = sizeof (enum_name);
status = RegEnumKeyEx(
adapter_key,
i,
enum_name,
&len,
NULL,
NULL,
NULL,
NULL);
if (status == ERROR_NO_MORE_ITEMS)
break;
else if (status != ERROR_SUCCESS)
msg (M_FATAL, "Error enumerating registry subkeys of key: %s",
ADAPTER_KEY);
openvpn_snprintf (unit_string, sizeof(unit_string), "%s\\%s",
ADAPTER_KEY, enum_name);
status = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
unit_string,
0,
KEY_READ,
&unit_key);
if (status != ERROR_SUCCESS)
dmsg (D_REGISTRY, "Error opening registry key: %s", unit_string);
else
{
len = sizeof (component_id);
status = RegQueryValueEx(
unit_key,
component_id_string,
NULL,
&data_type,
component_id,
&len);
if (status != ERROR_SUCCESS || data_type != REG_SZ)
dmsg (D_REGISTRY, "Error opening registry key: %s\\%s",
unit_string, component_id_string);
else
{
len = sizeof (net_cfg_instance_id);
status = RegQueryValueEx(
unit_key,
net_cfg_instance_id_string,
NULL,
&data_type,
net_cfg_instance_id,
&len);
if (status == ERROR_SUCCESS && data_type == REG_SZ)
{
if (!strcmp (component_id, TAP_COMPONENT_ID))
{
struct tap_reg *reg;
ALLOC_OBJ_CLEAR_GC (reg, struct tap_reg, gc);
reg->guid = string_alloc (net_cfg_instance_id, gc);
/* link into return list */
if (!first)
first = reg;
if (last)
last->next = reg;
last = reg;
}
}
}
RegCloseKey (unit_key);
}
++i;
}
RegCloseKey (adapter_key);
return first;
}
const struct panel_reg *
get_panel_reg (struct gc_arena *gc)
{
LONG status;
HKEY network_connections_key;
DWORD len;
struct panel_reg *first = NULL;
struct panel_reg *last = NULL;
int i = 0;
status = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
NETWORK_CONNECTIONS_KEY,
0,
KEY_READ,
&network_connections_key);
if (status != ERROR_SUCCESS)
msg (M_FATAL, "Error opening registry key: %s", NETWORK_CONNECTIONS_KEY);
while (true)
{
char enum_name[256];
char connection_string[256];
HKEY connection_key;
char name_data[256];
DWORD name_type;
const char name_string[] = "Name";
len = sizeof (enum_name);
status = RegEnumKeyEx(
network_connections_key,
i,
enum_name,
&len,
NULL,
NULL,
NULL,
NULL);
if (status == ERROR_NO_MORE_ITEMS)
break;
else if (status != ERROR_SUCCESS)
msg (M_FATAL, "Error enumerating registry subkeys of key: %s",
NETWORK_CONNECTIONS_KEY);
openvpn_snprintf (connection_string, sizeof(connection_string),
"%s\\%s\\Connection",
NETWORK_CONNECTIONS_KEY, enum_name);
status = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
connection_string,
0,
KEY_READ,
&connection_key);
if (status != ERROR_SUCCESS)
dmsg (D_REGISTRY, "Error opening registry key: %s", connection_string);
else
{
len = sizeof (name_data);
status = RegQueryValueEx(
connection_key,
name_string,
NULL,
&name_type,
name_data,
&len);
if (status != ERROR_SUCCESS || name_type != REG_SZ)
dmsg (D_REGISTRY, "Error opening registry key: %s\\%s\\%s",
NETWORK_CONNECTIONS_KEY, connection_string, name_string);
else
{
struct panel_reg *reg;
ALLOC_OBJ_CLEAR_GC (reg, struct panel_reg, gc);
reg->name = string_alloc (name_data, gc);
reg->guid = string_alloc (enum_name, gc);
/* link into return list */
if (!first)
first = reg;
if (last)
last->next = reg;
last = reg;
}
RegCloseKey (connection_key);
}
++i;
}
RegCloseKey (network_connections_key);
return first;
}
/*
* Check that two addresses are part of the same 255.255.255.252 subnet.
*/
void
verify_255_255_255_252 (in_addr_t local, in_addr_t remote)
{
struct gc_arena gc = gc_new ();
const unsigned int mask = 3;
const char *err = NULL;
if (local == remote)
{
err = "must be different";
goto error;
}
if ((local & (~mask)) != (remote & (~mask)))
{
err = "must exist within the same 255.255.255.252 subnet. This is a limitation of --dev tun when used with the TAP-WIN32 driver";
goto error;
}
if ((local & mask) == 0
|| (local & mask) == 3
|| (remote & mask) == 0
|| (remote & mask) == 3)
{
err = "cannot use the first or last address within a given 255.255.255.252 subnet. This is a limitation of --dev tun when used with the TAP-WIN32 driver";
goto error;
}
gc_free (&gc);
return;
error:
msg (M_FATAL, "There is a problem in your selection of --ifconfig endpoints [local=%s, remote=%s]. The local and remote VPN endpoints %s. Try '" PACKAGE " --show-valid-subnets' option for more info.",
print_in_addr_t (local, 0, &gc),
print_in_addr_t (remote, 0, &gc),
err);
gc_free (&gc);
}
void show_valid_win32_tun_subnets (void)
{
int i;
int col = 0;
printf ("On Windows, point-to-point IP support (i.e. --dev tun)\n");
printf ("is emulated by the TAP-Win32 driver. The major limitation\n");
printf ("imposed by this approach is that the --ifconfig local and\n");
printf ("remote endpoints must be part of the same 255.255.255.252\n");
printf ("subnet. The following list shows examples of endpoint\n");
printf ("pairs which satisfy this requirement. Only the final\n");
printf ("component of the IP address pairs is at issue.\n\n");
printf ("As an example, the following option would be correct:\n");
printf (" --ifconfig 10.7.0.5 10.7.0.6 (on host A)\n");
printf (" --ifconfig 10.7.0.6 10.7.0.5 (on host B)\n");
printf ("because [5,6] is part of the below list.\n\n");
for (i = 0; i < 256; i += 4)
{
printf("[%3d,%3d] ", i+1, i+2);
if (++col > 4)
{
col = 0;
printf ("\n");
}
}
if (col)
printf ("\n");
}
void
show_tap_win32_adapters (int msglev, int warnlev)
{
struct gc_arena gc = gc_new ();
bool warn_panel_null = false;
bool warn_panel_dup = false;
bool warn_tap_dup = false;
int links;
const struct tap_reg *tr;
const struct tap_reg *tr1;
const struct panel_reg *pr;
const struct tap_reg *tap_reg = get_tap_reg (&gc);
const struct panel_reg *panel_reg = get_panel_reg (&gc);
msg (msglev, "Available TAP-WIN32 adapters [name, GUID]:");
/* loop through each TAP-Win32 adapter registry entry */
for (tr = tap_reg; tr != NULL; tr = tr->next)
{
links = 0;
/* loop through each network connections entry in the control panel */
for (pr = panel_reg; pr != NULL; pr = pr->next)
{
if (!strcmp (tr->guid, pr->guid))
{
msg (msglev, "'%s' %s", pr->name, tr->guid);
++links;
}
}
if (links > 1)
{
warn_panel_dup = true;
}
else if (links == 0)
{
/* a TAP adapter exists without a link from the network
connections control panel */
warn_panel_null = true;
msg (msglev, "[NULL] %s", tr->guid);
}
}
/* check for TAP-Win32 adapter duplicated GUIDs */
for (tr = tap_reg; tr != NULL; tr = tr->next)
{
for (tr1 = tap_reg; tr1 != NULL; tr1 = tr1->next)
{
if (tr != tr1 && !strcmp (tr->guid, tr1->guid))
warn_tap_dup = true;
}
}
/* warn on registry inconsistencies */
if (warn_tap_dup)
msg (warnlev, "WARNING: Some TAP-Win32 adapters have duplicate GUIDs");
if (warn_panel_dup)
msg (warnlev, "WARNING: Some TAP-Win32 adapters have duplicate links from the Network Connections control panel");
if (warn_panel_null)
msg (warnlev, "WARNING: Some TAP-Win32 adapters have no link from the Network Connections control panel");
gc_free (&gc);
}
/*
* Confirm that GUID is a TAP-Win32 adapter.
*/
static bool
is_tap_win32 (const char *guid, const struct tap_reg *tap_reg)
{
const struct tap_reg *tr;
for (tr = tap_reg; tr != NULL; tr = tr->next)
{
if (guid && !strcmp (tr->guid, guid))
return true;
}
return false;
}
static const char *
guid_to_name (const char *guid, const struct panel_reg *panel_reg)
{
const struct panel_reg *pr;
for (pr = panel_reg; pr != NULL; pr = pr->next)
{
if (guid && !strcmp (pr->guid, guid))
return pr->name;
}
return NULL;
}
static const char *
name_to_guid (const char *name, const struct tap_reg *tap_reg, const struct panel_reg *panel_reg)
{
const struct panel_reg *pr;
for (pr = panel_reg; pr != NULL; pr = pr->next)
{
if (name && !strcmp (pr->name, name) && is_tap_win32 (pr->guid, tap_reg))
return pr->guid;
}
return NULL;
}
static void
at_least_one_tap_win32 (const struct tap_reg *tap_reg)
{
if (!tap_reg)
msg (M_FATAL, "There are no TAP-Win32 adapters on this system. You should be able to create a TAP-Win32 adapter by going to Start -> All Programs -> " PACKAGE_NAME " -> Add a new TAP-Win32 virtual ethernet adapter.");
}
/*
* Get an adapter GUID and optional actual_name from the
* registry for the TAP device # = device_number.
*/
static const char *
get_unspecified_device_guid (const int device_number,
char *actual_name,
int actual_name_size,
const struct tap_reg *tap_reg_src,
const struct panel_reg *panel_reg_src,
struct gc_arena *gc)
{
const struct tap_reg *tap_reg = tap_reg_src;
struct buffer ret = clear_buf ();
struct buffer actual = clear_buf ();
int i;
ASSERT (device_number >= 0);
/* Make sure we have at least one TAP adapter */
if (!tap_reg)
return NULL;
/* The actual_name output buffer may be NULL */
if (actual_name)
{
ASSERT (actual_name_size > 0);
buf_set_write (&actual, actual_name, actual_name_size);
}
/* Move on to specified device number */
for (i = 0; i < device_number; i++)
{
tap_reg = tap_reg->next;
if (!tap_reg)
return NULL;
}
/* Save Network Panel name (if exists) in actual_name */
if (actual_name)
{
const char *act = guid_to_name (tap_reg->guid, panel_reg_src);
if (act)
buf_printf (&actual, "%s", act);
else
buf_printf (&actual, "%s", tap_reg->guid);
}
/* Save GUID for return value */
ret = alloc_buf_gc (256, gc);
buf_printf (&ret, "%s", tap_reg->guid);
return BSTR (&ret);
}
/*
* Lookup a --dev-node adapter name in the registry
* returning the GUID and optional actual_name.
*/
static const char *
get_device_guid (const char *name,
char *actual_name,
int actual_name_size,
const struct tap_reg *tap_reg,
const struct panel_reg *panel_reg,
struct gc_arena *gc)
{
struct buffer ret = alloc_buf_gc (256, gc);
struct buffer actual = clear_buf ();
/* Make sure we have at least one TAP adapter */
if (!tap_reg)
return NULL;
/* The actual_name output buffer may be NULL */
if (actual_name)
{
ASSERT (actual_name_size > 0);
buf_set_write (&actual, actual_name, actual_name_size);
}
/* Check if GUID was explicitly specified as --dev-node parameter */
if (is_tap_win32 (name, tap_reg))
{
const char *act = guid_to_name (name, panel_reg);
buf_printf (&ret, "%s", name);
if (act)
buf_printf (&actual, "%s", act);
else
buf_printf (&actual, "%s", name);
return BSTR (&ret);
}
/* Lookup TAP adapter in network connections list */
{
const char *guid = name_to_guid (name, tap_reg, panel_reg);
if (guid)
{
buf_printf (&actual, "%s", name);
buf_printf (&ret, "%s", guid);
return BSTR (&ret);
}
}
return NULL;
}
/*
* Get adapter info list
*/
const IP_ADAPTER_INFO *
get_adapter_info_list (struct gc_arena *gc)
{
ULONG size = 0;
IP_ADAPTER_INFO *pi = NULL;
DWORD status;
if ((status = GetAdaptersInfo (NULL, &size)) != ERROR_BUFFER_OVERFLOW)
{
msg (M_INFO, "GetAdaptersInfo #1 failed (status=%u) : %s",
(unsigned int)status,
strerror_win32 (status, gc));
}
else
{
pi = (PIP_ADAPTER_INFO) gc_malloc (size, false, gc);
if ((status = GetAdaptersInfo (pi, &size)) == NO_ERROR)
return pi;
else
{
msg (M_INFO, "GetAdaptersInfo #2 failed (status=%u) : %s",
(unsigned int)status,
strerror_win32 (status, gc));
}
}
return pi;
}
const IP_PER_ADAPTER_INFO *
get_per_adapter_info (const DWORD index, struct gc_arena *gc)
{
ULONG size = 0;
IP_PER_ADAPTER_INFO *pi = NULL;
DWORD status;
if (index != ~0)
{
if ((status = GetPerAdapterInfo (index, NULL, &size)) != ERROR_BUFFER_OVERFLOW)
{
msg (M_INFO, "GetPerAdapterInfo #1 failed (status=%u) : %s",
(unsigned int)status,
strerror_win32 (status, gc));
}
else
{
pi = (PIP_PER_ADAPTER_INFO) gc_malloc (size, false, gc);
if ((status = GetPerAdapterInfo ((ULONG)index, pi, &size)) == ERROR_SUCCESS)
return pi;
else
{
msg (M_INFO, "GetPerAdapterInfo #2 failed (status=%u) : %s",
(unsigned int)status,
strerror_win32 (status, gc));
}
}
}
return pi;
}
static const IP_INTERFACE_INFO *
get_interface_info_list (struct gc_arena *gc)
{
ULONG size = 0;
IP_INTERFACE_INFO *ii = NULL;
DWORD status;
if ((status = GetInterfaceInfo (NULL, &size)) != ERROR_INSUFFICIENT_BUFFER)
{
msg (M_INFO, "GetInterfaceInfo #1 failed (status=%u) : %s",
(unsigned int)status,
strerror_win32 (status, gc));
}
else
{
ii = (PIP_INTERFACE_INFO) gc_malloc (size, false, gc);
if ((status = GetInterfaceInfo (ii, &size)) == NO_ERROR)
return ii;
else
{
msg (M_INFO, "GetInterfaceInfo #2 failed (status=%u) : %s",
(unsigned int)status,
strerror_win32 (status, gc));
}
}
return ii;
}
static const IP_ADAPTER_INDEX_MAP *
get_interface_info (DWORD index, struct gc_arena *gc)
{
const IP_INTERFACE_INFO *list = get_interface_info_list (gc);
if (list)
{
int i;
for (i = 0; i < list->NumAdapters; ++i)
{
const IP_ADAPTER_INDEX_MAP *inter = &list->Adapter[i];
if (index == inter->Index)
return inter;
}
}
return NULL;
}
/*
* Given an adapter index, return a pointer to the
* IP_ADAPTER_INFO structure for that adapter.
*/
const IP_ADAPTER_INFO *
get_adapter (const IP_ADAPTER_INFO *ai, DWORD index)
{
if (ai && index != (DWORD)~0)
{
const IP_ADAPTER_INFO *a;
/* find index in the linked list */
for (a = ai; a != NULL; a = a->Next)
{
if (a->Index == index)
return a;
}
}
return NULL;
}
const IP_ADAPTER_INFO *
get_adapter_info (DWORD index, struct gc_arena *gc)
{
return get_adapter (get_adapter_info_list (gc), index);
}
static int
get_adapter_n_ip_netmask (const IP_ADAPTER_INFO *ai)
{
if (ai)
{
int n = 0;
const IP_ADDR_STRING *ip = &ai->IpAddressList;
while (ip)
{
++n;
ip = ip->Next;
}
return n;
}
else
return 0;
}
static bool
get_adapter_ip_netmask (const IP_ADAPTER_INFO *ai, const int n, in_addr_t *ip, in_addr_t *netmask)
{
bool ret = false;
*ip = 0;
*netmask = 0;
if (ai)
{
const IP_ADDR_STRING *iplist = &ai->IpAddressList;
int i = 0;
while (iplist)
{
if (i == n)
break;
++i;
iplist = iplist->Next;
}
if (iplist)
{
const unsigned int getaddr_flags = GETADDR_HOST_ORDER;
const char *ip_str = iplist->IpAddress.String;
const char *netmask_str = iplist->IpMask.String;
bool succeed1 = false;
bool succeed2 = false;
if (ip_str && netmask_str && strlen (ip_str) && strlen (netmask_str))
{
*ip = getaddr (getaddr_flags, ip_str, 0, &succeed1, NULL);
*netmask = getaddr (getaddr_flags, netmask_str, 0, &succeed2, NULL);
ret = (succeed1 == true && succeed2 == true);
}
}
}
return ret;
}
static bool
test_adapter_ip_netmask (const IP_ADAPTER_INFO *ai, const in_addr_t ip, const in_addr_t netmask)
{
if (ai)
{
in_addr_t ip_adapter = 0;
in_addr_t netmask_adapter = 0;
const bool status = get_adapter_ip_netmask (ai, 0, &ip_adapter, &netmask_adapter);
return (status && ip_adapter == ip && netmask_adapter == netmask);
}
else
return false;
}
const IP_ADAPTER_INFO *
get_tun_adapter (const struct tuntap *tt, const IP_ADAPTER_INFO *list)
{
if (list && tt)
return get_adapter (list, tt->adapter_index);
else
return NULL;
}
bool
is_adapter_up (const struct tuntap *tt, const IP_ADAPTER_INFO *list)
{
int i;
bool ret = false;
const IP_ADAPTER_INFO *ai = get_tun_adapter (tt, list);
if (ai)
{
const int n = get_adapter_n_ip_netmask (ai);
/* loop once for every IP/netmask assigned to adapter */
for (i = 0; i < n; ++i)
{
in_addr_t ip, netmask;
if (get_adapter_ip_netmask (ai, i, &ip, &netmask))
{
if (tt->local && tt->adapter_netmask)
{
/* wait for our --ifconfig parms to match the actual adapter parms */
if (tt->local == ip && tt->adapter_netmask == netmask)
ret = true;
}
else
{
/* --ifconfig was not defined, maybe using a real DHCP server */
if (ip && netmask)
ret = true;
}
}
}
}
else
ret = true; /* this can occur when TAP adapter is bridged */
return ret;
}
bool
is_ip_in_adapter_subnet (const IP_ADAPTER_INFO *ai, const in_addr_t ip, in_addr_t *highest_netmask)
{
int i;
bool ret = false;
if (highest_netmask)
*highest_netmask = 0;
if (ai)
{
const int n = get_adapter_n_ip_netmask (ai);
for (i = 0; i < n; ++i)
{
in_addr_t adapter_ip, adapter_netmask;
if (get_adapter_ip_netmask (ai, i, &adapter_ip, &adapter_netmask))
{
if (adapter_ip && adapter_netmask && (ip & adapter_netmask) == (adapter_ip & adapter_netmask))
{
if (highest_netmask && adapter_netmask > *highest_netmask)
*highest_netmask = adapter_netmask;
ret = true;
}
}
}
}
return ret;
}
DWORD
adapter_index_of_ip (const IP_ADAPTER_INFO *list,
const in_addr_t ip,
int *count,
in_addr_t *netmask)
{
struct gc_arena gc = gc_new ();
DWORD ret = ~0;
in_addr_t highest_netmask = 0;
bool first = true;
if (count)
*count = 0;
while (list)
{
in_addr_t hn;
if (is_ip_in_adapter_subnet (list, ip, &hn))
{
if (first || hn > highest_netmask)
{
highest_netmask = hn;
if (count)
*count = 1;
ret = list->Index;
first = false;
}
else if (hn == highest_netmask)
{
if (count)
++*count;
}
}
list = list->Next;
}
dmsg (D_ROUTE_DEBUG, "DEBUG: IP Locate: ip=%s nm=%s index=%d count=%d",
print_in_addr_t (ip, 0, &gc),
print_in_addr_t (highest_netmask, 0, &gc),
(int)ret,
count ? *count : -1);
if (ret == ~0 && count)
*count = 0;
if (netmask)
*netmask = highest_netmask;
gc_free (&gc);
return ret;
}
/*
* Given an adapter index, return true if the adapter
* is DHCP disabled.
*/
#define DHCP_STATUS_UNDEF 0
#define DHCP_STATUS_ENABLED 1
#define DHCP_STATUS_DISABLED 2
static int
dhcp_status (DWORD index)
{
struct gc_arena gc = gc_new ();
int ret = DHCP_STATUS_UNDEF;
if (index != ~0)
{
const IP_ADAPTER_INFO *ai = get_adapter_info (index, &gc);
if (ai)
{
if (ai->DhcpEnabled)
ret = DHCP_STATUS_ENABLED;
else
ret = DHCP_STATUS_DISABLED;
}
}
gc_free (&gc);
return ret;
}
/*
* Delete all temporary address/netmask pairs which were added
* to adapter (given by index) by previous calls to AddIPAddress.
*/
static void
delete_temp_addresses (DWORD index)
{
struct gc_arena gc = gc_new ();
const IP_ADAPTER_INFO *a = get_adapter_info (index, &gc);
if (a)
{
const IP_ADDR_STRING *ip = &a->IpAddressList;
while (ip)
{
DWORD status;
const DWORD context = ip->Context;
if ((status = DeleteIPAddress ((ULONG) context)) == NO_ERROR)
{
msg (M_INFO, "Successfully deleted previously set dynamic IP/netmask: %s/%s",
ip->IpAddress.String,
ip->IpMask.String);
}
else
{
const char *empty = "0.0.0.0";
if (strcmp (ip->IpAddress.String, empty)
|| strcmp (ip->IpMask.String, empty))
msg (M_INFO, "NOTE: could not delete previously set dynamic IP/netmask: %s/%s (status=%u)",
ip->IpAddress.String,
ip->IpMask.String,
(unsigned int)status);
}
ip = ip->Next;
}
}
gc_free (&gc);
}
/*
* Get interface index for use with IP Helper API functions.
*/
static DWORD
get_adapter_index_method_1 (const char *guid)
{
struct gc_arena gc = gc_new ();
ULONG index = ~0;
DWORD status;
wchar_t wbuf[256];
snwprintf (wbuf, SIZE (wbuf), L"\\DEVICE\\TCPIP_%S", guid);
wbuf [SIZE(wbuf) - 1] = 0;
if ((status = GetAdapterIndex (wbuf, &index)) != NO_ERROR)
index = ~0;
gc_free (&gc);
return index;
}
static DWORD
get_adapter_index_method_2 (const char *guid)
{
struct gc_arena gc = gc_new ();
DWORD index = ~0;
const IP_ADAPTER_INFO *list = get_adapter_info_list (&gc);
while (list)
{
if (!strcmp (guid, list->AdapterName))
{
index = list->Index;
break;
}
list = list->Next;
}
gc_free (&gc);
return index;
}
static DWORD
get_adapter_index (const char *guid)
{
DWORD index;
index = get_adapter_index_method_1 (guid);
if (index == ~0)
index = get_adapter_index_method_2 (guid);
if (index == ~0)
msg (M_INFO, "NOTE: could not get adapter index for %s", guid);
return index;
}
static DWORD
get_adapter_index_flexible (const char *name) /* actual name or GUID */
{
struct gc_arena gc = gc_new ();
DWORD index;
index = get_adapter_index_method_1 (name);
if (index == ~0)
index = get_adapter_index_method_2 (name);
if (index == ~0)
{
const struct tap_reg *tap_reg = get_tap_reg (&gc);
const struct panel_reg *panel_reg = get_panel_reg (&gc);
const char *guid = name_to_guid (name, tap_reg, panel_reg);
index = get_adapter_index_method_1 (guid);
if (index == ~0)
index = get_adapter_index_method_2 (guid);
}
if (index == ~0)
msg (M_INFO, "NOTE: could not get adapter index for name/GUID '%s'", name);
gc_free (&gc);
return index;
}
/*
* Return a string representing a PIP_ADDR_STRING
*/
static const char *
format_ip_addr_string (const IP_ADDR_STRING *ip, struct gc_arena *gc)
{
struct buffer out = alloc_buf_gc (256, gc);
while (ip)
{
buf_printf (&out, "%s", ip->IpAddress.String);
if (strlen (ip->IpMask.String))
{
buf_printf (&out, "/");
buf_printf (&out, "%s", ip->IpMask.String);
}
buf_printf (&out, " ");
ip = ip->Next;
}
return BSTR (&out);
}
/*
* Show info for a single adapter
*/
static void
show_adapter (int msglev, const IP_ADAPTER_INFO *a, struct gc_arena *gc)
{
msg (msglev, "%s", a->Description);
msg (msglev, " Index = %d", (int)a->Index);
msg (msglev, " GUID = %s", a->AdapterName);
msg (msglev, " IP = %s", format_ip_addr_string (&a->IpAddressList, gc));
msg (msglev, " MAC = %s", format_hex_ex (a->Address, a->AddressLength, 0, 1, ":", gc));
msg (msglev, " GATEWAY = %s", format_ip_addr_string (&a->GatewayList, gc));
if (a->DhcpEnabled)
{
msg (msglev, " DHCP SERV = %s", format_ip_addr_string (&a->DhcpServer, gc));
msg (msglev, " DHCP LEASE OBTAINED = %s", time_string (a->LeaseObtained, 0, false, gc));
msg (msglev, " DHCP LEASE EXPIRES = %s", time_string (a->LeaseExpires, 0, false, gc));
}
if (a->HaveWins)
{
msg (msglev, " PRI WINS = %s", format_ip_addr_string (&a->PrimaryWinsServer, gc));
msg (msglev, " SEC WINS = %s", format_ip_addr_string (&a->SecondaryWinsServer, gc));
}
{
const IP_PER_ADAPTER_INFO *pai = get_per_adapter_info (a->Index, gc);
if (pai)
{
msg (msglev, " DNS SERV = %s", format_ip_addr_string (&pai->DnsServerList, gc));
}
}
}
/*
* Show current adapter list
*/
void
show_adapters (int msglev)
{
struct gc_arena gc = gc_new ();
const IP_ADAPTER_INFO *ai = get_adapter_info_list (&gc);
msg (msglev, "SYSTEM ADAPTER LIST");
if (ai)
{
const IP_ADAPTER_INFO *a;
/* find index in the linked list */
for (a = ai; a != NULL; a = a->Next)
{
show_adapter (msglev, a, &gc);
}
}
gc_free (&gc);
}
/*
* Set a particular TAP-Win32 adapter (or all of them if
* adapter_name == NULL) to allow it to be opened from
* a non-admin account. This setting will only persist
* for the lifetime of the device object.
*/
static void
tap_allow_nonadmin_access_handle (const char *device_path, HANDLE hand)
{
struct security_attributes sa;
BOOL status;
if (!init_security_attributes_allow_all (&sa))
msg (M_ERR, "Error: init SA failed");
status = SetKernelObjectSecurity (hand, DACL_SECURITY_INFORMATION, &sa.sd);
if (!status)
{
msg (M_ERRNO, "Error: SetKernelObjectSecurity failed on %s", device_path);
}
else
{
msg (M_INFO|M_NOPREFIX, "TAP-Win32 device: %s [Non-admin access allowed]", device_path);
}
}
void
tap_allow_nonadmin_access (const char *dev_node)
{
struct gc_arena gc = gc_new ();
const struct tap_reg *tap_reg = get_tap_reg (&gc);
const struct panel_reg *panel_reg = get_panel_reg (&gc);
const char *device_guid = NULL;
HANDLE hand;
char actual_buffer[256];
char device_path[256];
at_least_one_tap_win32 (tap_reg);
if (dev_node)
{
/* Get the device GUID for the device specified with --dev-node. */
device_guid = get_device_guid (dev_node, actual_buffer, sizeof (actual_buffer), tap_reg, panel_reg, &gc);
if (!device_guid)
msg (M_FATAL, "TAP-Win32 adapter '%s' not found", dev_node);
/* Open Windows TAP-Win32 adapter */
openvpn_snprintf (device_path, sizeof(device_path), "%s%s%s",
USERMODEDEVICEDIR,
device_guid,
TAPSUFFIX);
hand = CreateFile (
device_path,
MAXIMUM_ALLOWED,
0, /* was: FILE_SHARE_READ */
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
0
);
if (hand == INVALID_HANDLE_VALUE)
msg (M_ERR, "CreateFile failed on TAP device: %s", device_path);
tap_allow_nonadmin_access_handle (device_path, hand);
CloseHandle (hand);
}
else
{
int device_number = 0;
/* Try opening all TAP devices */
while (true)
{
device_guid = get_unspecified_device_guid (device_number,
actual_buffer,
sizeof (actual_buffer),
tap_reg,
panel_reg,
&gc);
if (!device_guid)
break;
/* Open Windows TAP-Win32 adapter */
openvpn_snprintf (device_path, sizeof(device_path), "%s%s%s",
USERMODEDEVICEDIR,
device_guid,
TAPSUFFIX);
hand = CreateFile (
device_path,
MAXIMUM_ALLOWED,
0, /* was: FILE_SHARE_READ */
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
0
);
if (hand == INVALID_HANDLE_VALUE)
msg (M_WARN, "CreateFile failed on TAP device: %s", device_path);
else
{
tap_allow_nonadmin_access_handle (device_path, hand);
CloseHandle (hand);
}
device_number++;
}
}
gc_free (&gc);
}
/*
* DHCP release/renewal
*/
bool
dhcp_release_by_adapter_index(const DWORD adapter_index)
{
struct gc_arena gc = gc_new ();
bool ret = false;
const IP_ADAPTER_INDEX_MAP *inter = get_interface_info (adapter_index, &gc);
if (inter)
{
DWORD status = IpReleaseAddress ((IP_ADAPTER_INDEX_MAP *)inter);
if (status == NO_ERROR)
{
msg (D_TUNTAP_INFO, "TAP: DHCP address released");
ret = true;
}
else
msg (M_WARN, "NOTE: Release of DHCP-assigned IP address lease on TAP-Win32 adapter failed: %s (code=%u)",
strerror_win32 (status, &gc),
(unsigned int)status);
}
gc_free (&gc);
return ret;
}
static bool
dhcp_release (const struct tuntap *tt)
{
if (tt && tt->options.ip_win32_type == IPW32_SET_DHCP_MASQ && tt->adapter_index != ~0)
return dhcp_release_by_adapter_index (tt->adapter_index);
else
return false;
}
bool
dhcp_renew_by_adapter_index (const DWORD adapter_index)
{
struct gc_arena gc = gc_new ();
bool ret = false;
const IP_ADAPTER_INDEX_MAP *inter = get_interface_info (adapter_index, &gc);
if (inter)
{
DWORD status = IpRenewAddress ((IP_ADAPTER_INDEX_MAP *)inter);
if (status == NO_ERROR)
{
msg (D_TUNTAP_INFO, "TAP: DHCP address renewal succeeded");
ret = true;
}
else
msg (M_WARN, "WARNING: Failed to renew DHCP IP address lease on TAP-Win32 adapter: %s (code=%u)",
strerror_win32 (status, &gc),
(unsigned int)status);
}
gc_free (&gc);
return ret;
}
static bool
dhcp_renew (const struct tuntap *tt)
{
if (tt && tt->options.ip_win32_type == IPW32_SET_DHCP_MASQ && tt->adapter_index != ~0)
return dhcp_renew_by_adapter_index (tt->adapter_index);
else
return false;
}
/*
* netsh functions
*/
static void
netsh_command (const struct argv *a, int n)
{
int i;
for (i = 0; i < n; ++i)
{
bool status;
openvpn_sleep (1);
netcmd_semaphore_lock ();
argv_msg_prefix (M_INFO, a, "NETSH");
status = openvpn_execve_check (a, NULL, 0, "ERROR: netsh command failed");
netcmd_semaphore_release ();
if (status)
return;
openvpn_sleep (4);
}
msg (M_FATAL, "NETSH: command failed");
}
void
ipconfig_register_dns (const struct env_set *es)
{
struct argv argv;
bool status;
const char err[] = "ERROR: Windows ipconfig command failed";
netcmd_semaphore_lock ();
argv_init (&argv);
argv_printf (&argv, "%s%sc stop dnscache",
get_win_sys_path(),
WIN_NET_PATH_SUFFIX);
argv_msg (D_TUNTAP_INFO, &argv);
status = openvpn_execve_check (&argv, es, 0, err);
argv_reset(&argv);
argv_printf (&argv, "%s%sc start dnscache",
get_win_sys_path(),
WIN_NET_PATH_SUFFIX);
argv_msg (D_TUNTAP_INFO, &argv);
status = openvpn_execve_check (&argv, es, 0, err);
argv_reset(&argv);
argv_printf (&argv, "%s%sc /flushdns",
get_win_sys_path(),
WIN_IPCONFIG_PATH_SUFFIX);
argv_msg (D_TUNTAP_INFO, &argv);
status = openvpn_execve_check (&argv, es, 0, err);
argv_reset(&argv);
argv_printf (&argv, "%s%sc /registerdns",
get_win_sys_path(),
WIN_IPCONFIG_PATH_SUFFIX);
argv_msg (D_TUNTAP_INFO, &argv);
status = openvpn_execve_check (&argv, es, 0, err);
argv_reset(&argv);
netcmd_semaphore_release ();
}
void
ip_addr_string_to_array (in_addr_t *dest, int *dest_len, const IP_ADDR_STRING *src)
{
int i = 0;
while (src)
{
const unsigned int getaddr_flags = GETADDR_HOST_ORDER;
const char *ip_str = src->IpAddress.String;
in_addr_t ip = 0;
bool succeed = false;
if (i >= *dest_len)
break;
if (!ip_str || !strlen (ip_str))
break;
ip = getaddr (getaddr_flags, ip_str, 0, &succeed, NULL);
if (!succeed)
break;
dest[i++] = ip;
src = src->Next;
}
*dest_len = i;
#if 0
{
struct gc_arena gc = gc_new ();
msg (M_INFO, "ip_addr_string_to_array [%d]", *dest_len);
for (i = 0; i < *dest_len; ++i)
{
msg (M_INFO, "%s", print_in_addr_t (dest[i], 0, &gc));
}
gc_free (&gc);
}
#endif
}
static bool
ip_addr_one_to_one (const in_addr_t *a1, const int a1len, const IP_ADDR_STRING *ias)
{
in_addr_t a2[8];
int a2len = SIZE(a2);
int i;
ip_addr_string_to_array (a2, &a2len, ias);
/*msg (M_INFO, "a1len=%d a2len=%d", a1len, a2len);*/
if (a1len != a2len)
return false;
for (i = 0; i < a1len; ++i)
{
if (a1[i] != a2[i])
return false;
}
return true;
}
static bool
ip_addr_member_of (const in_addr_t addr, const IP_ADDR_STRING *ias)
{
in_addr_t aa[8];
int len = SIZE(aa);
int i;
ip_addr_string_to_array (aa, &len, ias);
for (i = 0; i < len; ++i)
{
if (addr == aa[i])
return true;
}
return false;
}
static void
netsh_ifconfig_options (const char *type,
const in_addr_t *addr_list,
const int addr_len,
const IP_ADDR_STRING *current,
const char *flex_name,
const bool test_first)
{
struct gc_arena gc = gc_new ();
struct argv argv = argv_new ();
bool delete_first = false;
/* first check if we should delete existing DNS/WINS settings from TAP interface */
if (test_first)
{
if (!ip_addr_one_to_one (addr_list, addr_len, current))
delete_first = true;
}
else
delete_first = true;
/* delete existing DNS/WINS settings from TAP interface */
if (delete_first)
{
argv_printf (&argv, "%s%sc interface ip delete %s %s all",
get_win_sys_path(),
NETSH_PATH_SUFFIX,
type,
flex_name);
netsh_command (&argv, 2);
}
/* add new DNS/WINS settings to TAP interface */
{
int count = 0;
int i;
for (i = 0; i < addr_len; ++i)
{
if (delete_first || !test_first || !ip_addr_member_of (addr_list[i], current))
{
const char *fmt = count ?
"%s%sc interface ip add %s %s %s"
: "%s%sc interface ip set %s %s static %s";
argv_printf (&argv, fmt,
get_win_sys_path(),
NETSH_PATH_SUFFIX,
type,
flex_name,
print_in_addr_t (addr_list[i], 0, &gc));
netsh_command (&argv, 2);
++count;
}
else
{
msg (M_INFO, "NETSH: \"%s\" %s %s [already set]",
flex_name,
type,
print_in_addr_t (addr_list[i], 0, &gc));
}
}
}
argv_reset (&argv);
gc_free (&gc);
}
static void
init_ip_addr_string2 (IP_ADDR_STRING *dest, const IP_ADDR_STRING *src1, const IP_ADDR_STRING *src2)
{
CLEAR (dest[0]);
CLEAR (dest[1]);
if (src1)
{
dest[0] = *src1;
dest[0].Next = NULL;
}
if (src2)
{
dest[1] = *src2;
dest[0].Next = &dest[1];
dest[1].Next = NULL;
}
}
static void
netsh_ifconfig (const struct tuntap_options *to,
const char *flex_name,
const in_addr_t ip,
const in_addr_t netmask,
const unsigned int flags)
{
struct gc_arena gc = gc_new ();
struct argv argv = argv_new ();
const IP_ADAPTER_INFO *ai = NULL;
const IP_PER_ADAPTER_INFO *pai = NULL;
if (flags & NI_TEST_FIRST)
{
const IP_ADAPTER_INFO *list = get_adapter_info_list (&gc);
const int index = get_adapter_index_flexible (flex_name);
ai = get_adapter (list, index);
pai = get_per_adapter_info (index, &gc);
}
if (flags & NI_IP_NETMASK)
{
if (test_adapter_ip_netmask (ai, ip, netmask))
{
msg (M_INFO, "NETSH: \"%s\" %s/%s [already set]",
flex_name,
print_in_addr_t (ip, 0, &gc),
print_in_addr_t (netmask, 0, &gc));
}
else
{
/* example: netsh interface ip set address my-tap static 10.3.0.1 255.255.255.0 */
argv_printf (&argv, "%s%sc interface ip set address %s static %s %s",
get_win_sys_path(),
NETSH_PATH_SUFFIX,
flex_name,
print_in_addr_t (ip, 0, &gc),
print_in_addr_t (netmask, 0, &gc));
netsh_command (&argv, 4);
}
}
/* set WINS/DNS options */
if (flags & NI_OPTIONS)
{
IP_ADDR_STRING wins[2];
CLEAR (wins[0]);
CLEAR (wins[1]);
netsh_ifconfig_options ("dns",
to->dns,
to->dns_len,
pai ? &pai->DnsServerList : NULL,
flex_name,
BOOL_CAST (flags & NI_TEST_FIRST));
if (ai && ai->HaveWins)
init_ip_addr_string2 (wins, &ai->PrimaryWinsServer, &ai->SecondaryWinsServer);
netsh_ifconfig_options ("wins",
to->wins,
to->wins_len,
ai ? wins : NULL,
flex_name,
BOOL_CAST (flags & NI_TEST_FIRST));
}
argv_reset (&argv);
gc_free (&gc);
}
static void
netsh_enable_dhcp (const struct tuntap_options *to,
const char *actual_name)
{
struct argv argv;
argv_init (&argv);
/* example: netsh interface ip set address my-tap dhcp */
argv_printf (&argv,
"%s%sc interface ip set address %s dhcp",
get_win_sys_path(),
NETSH_PATH_SUFFIX,
actual_name);
netsh_command (&argv, 4);
argv_reset (&argv);
}
/*
* Return a TAP name for netsh commands.
*/
static const char *
netsh_get_id (const char *dev_node, struct gc_arena *gc)
{
const struct tap_reg *tap_reg = get_tap_reg (gc);
const struct panel_reg *panel_reg = get_panel_reg (gc);
struct buffer actual = alloc_buf_gc (256, gc);
const char *guid;
at_least_one_tap_win32 (tap_reg);
if (dev_node)
{
guid = get_device_guid (dev_node, BPTR (&actual), BCAP (&actual), tap_reg, panel_reg, gc);
}
else
{
guid = get_unspecified_device_guid (0, BPTR (&actual), BCAP (&actual), tap_reg, panel_reg, gc);
if (get_unspecified_device_guid (1, NULL, 0, tap_reg, panel_reg, gc)) /* ambiguous if more than one TAP-Win32 adapter */
guid = NULL;
}
if (!guid)
return "NULL"; /* not found */
else if (strcmp (BPTR (&actual), "NULL"))
return BPTR (&actual); /* control panel name */
else
return guid; /* no control panel name, return GUID instead */
}
/*
* Called iteratively on TAP-Win32 wait-for-initialization polling loop
*/
void
tun_standby_init (struct tuntap *tt)
{
tt->standby_iter = 0;
}
bool
tun_standby (struct tuntap *tt)
{
bool ret = true;
++tt->standby_iter;
if (tt->options.ip_win32_type == IPW32_SET_ADAPTIVE)
{
if (tt->standby_iter == IPW32_SET_ADAPTIVE_TRY_NETSH)
{
msg (M_INFO, "NOTE: now trying netsh (this may take some time)");
netsh_ifconfig (&tt->options,
tt->actual_name,
tt->local,
tt->adapter_netmask,
NI_TEST_FIRST|NI_IP_NETMASK|NI_OPTIONS);
}
else if (tt->standby_iter >= IPW32_SET_ADAPTIVE_TRY_NETSH*2)
{
ret = false;
}
}
return ret;
}
/*
* Convert DHCP options from the command line / config file
* into a raw DHCP-format options string.
*/
static void
write_dhcp_u8 (struct buffer *buf, const int type, const int data, bool *error)
{
if (!buf_safe (buf, 3))
{
*error = true;
msg (M_WARN, "write_dhcp_u8: buffer overflow building DHCP options");
return;
}
buf_write_u8 (buf, type);
buf_write_u8 (buf, 1);
buf_write_u8 (buf, data);
}
static void
write_dhcp_u32_array (struct buffer *buf, const int type, const uint32_t *data, const unsigned int len, bool *error)
{
if (len > 0)
{
int i;
const int size = len * sizeof (uint32_t);
if (!buf_safe (buf, 2 + size))
{
*error = true;
msg (M_WARN, "write_dhcp_u32_array: buffer overflow building DHCP options");
return;
}
if (size < 1 || size > 255)
{
*error = true;
msg (M_WARN, "write_dhcp_u32_array: size (%d) must be > 0 and <= 255", size);
return;
}
buf_write_u8 (buf, type);
buf_write_u8 (buf, size);
for (i = 0; i < len; ++i)
buf_write_u32 (buf, data[i]);
}
}
static void
write_dhcp_str (struct buffer *buf, const int type, const char *str, bool *error)
{
const int len = strlen (str);
if (!buf_safe (buf, 2 + len))
{
*error = true;
msg (M_WARN, "write_dhcp_str: buffer overflow building DHCP options");
return;
}
if (len < 1 || len > 255)
{
*error = true;
msg (M_WARN, "write_dhcp_str: string '%s' must be > 0 bytes and <= 255 bytes", str);
return;
}
buf_write_u8 (buf, type);
buf_write_u8 (buf, len);
buf_write (buf, str, len);
}
static bool
build_dhcp_options_string (struct buffer *buf, const struct tuntap_options *o)
{
bool error = false;
if (o->domain)
write_dhcp_str (buf, 15, o->domain, &error);
if (o->netbios_scope)
write_dhcp_str (buf, 47, o->netbios_scope, &error);
if (o->netbios_node_type)
write_dhcp_u8 (buf, 46, o->netbios_node_type, &error);
write_dhcp_u32_array (buf, 6, (uint32_t*)o->dns, o->dns_len, &error);
write_dhcp_u32_array (buf, 44, (uint32_t*)o->wins, o->wins_len, &error);
write_dhcp_u32_array (buf, 42, (uint32_t*)o->ntp, o->ntp_len, &error);
write_dhcp_u32_array (buf, 45, (uint32_t*)o->nbdd, o->nbdd_len, &error);
/* the MS DHCP server option 'Disable Netbios-over-TCP/IP
is implemented as vendor option 001, value 002.
A value of 001 means 'leave NBT alone' which is the default */
if (o->disable_nbt)
{
if (!buf_safe (buf, 8))
{
msg (M_WARN, "build_dhcp_options_string: buffer overflow building DHCP options");
return false;
}
buf_write_u8 (buf, 43);
buf_write_u8 (buf, 6); /* total length field */
buf_write_u8 (buf, 0x001);
buf_write_u8 (buf, 4); /* length of the vendor specified field */
buf_write_u32 (buf, 0x002);
}
return !error;
}
static void
fork_dhcp_action (struct tuntap *tt)
{
if (tt->options.dhcp_pre_release || tt->options.dhcp_renew)
{
struct gc_arena gc = gc_new ();
struct buffer cmd = alloc_buf_gc (256, &gc);
const int verb = 3;
const int pre_sleep = 1;
buf_printf (&cmd, "openvpn --verb %d --tap-sleep %d", verb, pre_sleep);
if (tt->options.dhcp_pre_release)
buf_printf (&cmd, " --dhcp-pre-release");
if (tt->options.dhcp_renew)
buf_printf (&cmd, " --dhcp-renew");
buf_printf (&cmd, " --dhcp-internal %u", (unsigned int)tt->adapter_index);
fork_to_self (BSTR (&cmd));
gc_free (&gc);
}
}
void
fork_register_dns_action (struct tuntap *tt)
{
if (tt && tt->options.register_dns)
{
struct gc_arena gc = gc_new ();
struct buffer cmd = alloc_buf_gc (256, &gc);
const int verb = 3;
buf_printf (&cmd, "openvpn --verb %d --register-dns --rdns-internal", verb);
fork_to_self (BSTR (&cmd));
gc_free (&gc);
}
}
void
open_tun (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, struct tuntap *tt)
{
struct gc_arena gc = gc_new ();
char device_path[256];
const char *device_guid = NULL;
DWORD len;
bool dhcp_masq = false;
bool dhcp_masq_post = false;
/*netcmd_semaphore_lock ();*/
ipv6_support (ipv6, false, tt);
if (tt->type == DEV_TYPE_NULL)
{
open_null (tt);
gc_free (&gc);
return;
}
else if (tt->type == DEV_TYPE_TAP || tt->type == DEV_TYPE_TUN)
{
;
}
else
{
msg (M_FATAL|M_NOPREFIX, "Unknown virtual device type: '%s'", dev);
}
/*
* Lookup the device name in the registry, using the --dev-node high level name.
*/
{
const struct tap_reg *tap_reg = get_tap_reg (&gc);
const struct panel_reg *panel_reg = get_panel_reg (&gc);
char actual_buffer[256];
at_least_one_tap_win32 (tap_reg);
if (dev_node)
{
/* Get the device GUID for the device specified with --dev-node. */
device_guid = get_device_guid (dev_node, actual_buffer, sizeof (actual_buffer), tap_reg, panel_reg, &gc);
if (!device_guid)
msg (M_FATAL, "TAP-Win32 adapter '%s' not found", dev_node);
/* Open Windows TAP-Win32 adapter */
openvpn_snprintf (device_path, sizeof(device_path), "%s%s%s",
USERMODEDEVICEDIR,
device_guid,
TAPSUFFIX);
tt->hand = CreateFile (
device_path,
GENERIC_READ | GENERIC_WRITE,
0, /* was: FILE_SHARE_READ */
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
0
);
if (tt->hand == INVALID_HANDLE_VALUE)
msg (M_ERR, "CreateFile failed on TAP device: %s", device_path);
}
else
{
int device_number = 0;
/* Try opening all TAP devices until we find one available */
while (true)
{
device_guid = get_unspecified_device_guid (device_number,
actual_buffer,
sizeof (actual_buffer),
tap_reg,
panel_reg,
&gc);
if (!device_guid)
msg (M_FATAL, "All TAP-Win32 adapters on this system are currently in use.");
/* Open Windows TAP-Win32 adapter */
openvpn_snprintf (device_path, sizeof(device_path), "%s%s%s",
USERMODEDEVICEDIR,
device_guid,
TAPSUFFIX);
tt->hand = CreateFile (
device_path,
GENERIC_READ | GENERIC_WRITE,
0, /* was: FILE_SHARE_READ */
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
0
);
if (tt->hand == INVALID_HANDLE_VALUE)
msg (D_TUNTAP_INFO, "CreateFile failed on TAP device: %s", device_path);
else
break;
device_number++;
}
}
/* translate high-level device name into a device instance
GUID using the registry */
tt->actual_name = string_alloc (actual_buffer, NULL);
}
msg (M_INFO, "TAP-WIN32 device [%s] opened: %s", tt->actual_name, device_path);
tt->adapter_index = get_adapter_index (device_guid);
/* get driver version info */
{
ULONG info[3];
CLEAR (info);
if (DeviceIoControl (tt->hand, TAP_IOCTL_GET_VERSION,
&info, sizeof (info),
&info, sizeof (info), &len, NULL))
{
msg (D_TUNTAP_INFO, "TAP-Win32 Driver Version %d.%d %s",
(int) info[0],
(int) info[1],
(info[2] ? "(DEBUG)" : ""));
}
if (!(info[0] == TAP_WIN32_MIN_MAJOR && info[1] >= TAP_WIN32_MIN_MINOR))
msg (M_FATAL, "ERROR: This version of " PACKAGE_NAME " requires a TAP-Win32 driver that is at least version %d.%d -- If you recently upgraded your " PACKAGE_NAME " distribution, a reboot is probably required at this point to get Windows to see the new driver.",
TAP_WIN32_MIN_MAJOR,
TAP_WIN32_MIN_MINOR);
}
/* get driver MTU */
{
ULONG mtu;
if (DeviceIoControl (tt->hand, TAP_IOCTL_GET_MTU,
&mtu, sizeof (mtu),
&mtu, sizeof (mtu), &len, NULL))
{
tt->post_open_mtu = (int) mtu;
msg (D_MTU_INFO, "TAP-Win32 MTU=%d", (int) mtu);
}
}
/*
* Preliminaries for setting TAP-Win32 adapter TCP/IP
* properties via --ip-win32 dynamic or --ip-win32 adaptive.
*/
if (tt->did_ifconfig_setup)
{
if (tt->options.ip_win32_type == IPW32_SET_DHCP_MASQ)
{
/*
* If adapter is set to non-DHCP, set to DHCP mode.
*/
if (dhcp_status (tt->adapter_index) == DHCP_STATUS_DISABLED)
netsh_enable_dhcp (&tt->options, tt->actual_name);
dhcp_masq = true;
dhcp_masq_post = true;
}
else if (tt->options.ip_win32_type == IPW32_SET_ADAPTIVE)
{
/*
* If adapter is set to non-DHCP, use netsh right away.
*/
if (dhcp_status (tt->adapter_index) != DHCP_STATUS_ENABLED)
{
netsh_ifconfig (&tt->options,
tt->actual_name,
tt->local,
tt->adapter_netmask,
NI_TEST_FIRST|NI_IP_NETMASK|NI_OPTIONS);
}
else
{
dhcp_masq = true;
}
}
}
/* set point-to-point mode if TUN device */
if (tt->type == DEV_TYPE_TUN)
{
if (!tt->did_ifconfig_setup)
{
msg (M_FATAL, "ERROR: --dev tun also requires --ifconfig");
}
if (tt->topology == TOP_SUBNET)
{
in_addr_t ep[3];
BOOL status;
ep[0] = htonl (tt->local);
ep[1] = htonl (tt->local & tt->remote_netmask);
ep[2] = htonl (tt->remote_netmask);
status = DeviceIoControl (tt->hand, TAP_IOCTL_CONFIG_TUN,
ep, sizeof (ep),
ep, sizeof (ep), &len, NULL);
msg (status ? M_INFO : M_FATAL, "Set TAP-Win32 TUN subnet mode network/local/netmask = %s/%s/%s [%s]",
print_in_addr_t (ep[1], IA_NET_ORDER, &gc),
print_in_addr_t (ep[0], IA_NET_ORDER, &gc),
print_in_addr_t (ep[2], IA_NET_ORDER, &gc),
status ? "SUCCEEDED" : "FAILED");
} else {
in_addr_t ep[2];
ep[0] = htonl (tt->local);
ep[1] = htonl (tt->remote_netmask);
if (!DeviceIoControl (tt->hand, TAP_IOCTL_CONFIG_POINT_TO_POINT,
ep, sizeof (ep),
ep, sizeof (ep), &len, NULL))
msg (M_FATAL, "ERROR: The TAP-Win32 driver rejected a DeviceIoControl call to set Point-to-Point mode, which is required for --dev tun");
}
}
/* should we tell the TAP-Win32 driver to masquerade as a DHCP server as a means
of setting the adapter address? */
if (dhcp_masq)
{
uint32_t ep[4];
/* We will answer DHCP requests with a reply to set IP/subnet to these values */
ep[0] = htonl (tt->local);
ep[1] = htonl (tt->adapter_netmask);
/* At what IP address should the DHCP server masquerade at? */
if (tt->type == DEV_TYPE_TUN)
{
if (tt->topology == TOP_SUBNET)
{
const in_addr_t netmask_inv = ~tt->remote_netmask;
ep[2] = netmask_inv ? htonl ((tt->local | netmask_inv) - 1) : 0;
}
else
ep[2] = htonl (tt->remote_netmask);
if (tt->options.dhcp_masq_custom_offset)
msg (M_WARN, "WARNING: because you are using '--dev tun' mode, the '--ip-win32 dynamic [offset]' option is ignoring the offset parameter");
}
else
{
in_addr_t dsa; /* DHCP server addr */
ASSERT (tt->type == DEV_TYPE_TAP);
if (tt->options.dhcp_masq_offset < 0)
dsa = (tt->local | (~tt->adapter_netmask)) + tt->options.dhcp_masq_offset;
else
dsa = (tt->local & tt->adapter_netmask) + tt->options.dhcp_masq_offset;
if (dsa == tt->local)
msg (M_FATAL, "ERROR: There is a clash between the --ifconfig local address and the internal DHCP server address -- both are set to %s -- please use the --ip-win32 dynamic option to choose a different free address from the --ifconfig subnet for the internal DHCP server", print_in_addr_t (dsa, 0, &gc));
if ((tt->local & tt->adapter_netmask) != (dsa & tt->adapter_netmask))
msg (M_FATAL, "ERROR: --tap-win32 dynamic [offset] : offset is outside of --ifconfig subnet");
ep[2] = htonl (dsa);
}
/* lease time in seconds */
ep[3] = (uint32_t) tt->options.dhcp_lease_time;
ASSERT (ep[3] > 0);
#ifndef SIMULATE_DHCP_FAILED /* this code is disabled to simulate bad DHCP negotiation */
if (!DeviceIoControl (tt->hand, TAP_IOCTL_CONFIG_DHCP_MASQ,
ep, sizeof (ep),
ep, sizeof (ep), &len, NULL))
msg (M_FATAL, "ERROR: The TAP-Win32 driver rejected a DeviceIoControl call to set TAP_IOCTL_CONFIG_DHCP_MASQ mode");
msg (M_INFO, "Notified TAP-Win32 driver to set a DHCP IP/netmask of %s/%s on interface %s [DHCP-serv: %s, lease-time: %d]",
print_in_addr_t (tt->local, 0, &gc),
print_in_addr_t (tt->adapter_netmask, 0, &gc),
device_guid,
print_in_addr_t (ep[2], IA_NET_ORDER, &gc),
ep[3]
);
/* user-supplied DHCP options capability */
if (tt->options.dhcp_options)
{
struct buffer buf = alloc_buf (256);
if (build_dhcp_options_string (&buf, &tt->options))
{
msg (D_DHCP_OPT, "DHCP option string: %s", format_hex (BPTR (&buf), BLEN (&buf), 0, &gc));
if (!DeviceIoControl (tt->hand, TAP_IOCTL_CONFIG_DHCP_SET_OPT,
BPTR (&buf), BLEN (&buf),
BPTR (&buf), BLEN (&buf), &len, NULL))
msg (M_FATAL, "ERROR: The TAP-Win32 driver rejected a TAP_IOCTL_CONFIG_DHCP_SET_OPT DeviceIoControl call");
}
else
msg (M_WARN, "DHCP option string not set due to error");
free_buf (&buf);
}
#endif
}
/* set driver media status to 'connected' */
{
ULONG status = TRUE;
if (!DeviceIoControl (tt->hand, TAP_IOCTL_SET_MEDIA_STATUS,
&status, sizeof (status),
&status, sizeof (status), &len, NULL))
msg (M_WARN, "WARNING: The TAP-Win32 driver rejected a TAP_IOCTL_SET_MEDIA_STATUS DeviceIoControl call.");
}
/* possible wait for adapter to come up */
{
int s = tt->options.tap_sleep;
if (s > 0)
{
msg (M_INFO, "Sleeping for %d seconds...", s);
openvpn_sleep (s);
}
}
/* possibly use IP Helper API to set IP address on adapter */
{
const DWORD index = tt->adapter_index;
/* flush arp cache */
if (index != (DWORD)~0)
{
DWORD status;
if ((status = FlushIpNetTable (index)) == NO_ERROR)
msg (M_INFO, "Successful ARP Flush on interface [%u] %s",
(unsigned int)index,
device_guid);
else
msg (D_TUNTAP_INFO, "NOTE: FlushIpNetTable failed on interface [%u] %s (status=%u) : %s",
(unsigned int)index,
device_guid,
(unsigned int)status,
strerror_win32 (status, &gc));
}
/*
* If the TAP-Win32 driver is masquerading as a DHCP server
* make sure the TCP/IP properties for the adapter are
* set correctly.
*/
if (dhcp_masq_post)
{
/* check dhcp enable status */
if (dhcp_status (index) == DHCP_STATUS_DISABLED)
msg (M_WARN, "WARNING: You have selected '--ip-win32 dynamic', which will not work unless the TAP-Win32 TCP/IP properties are set to 'Obtain an IP address automatically'");
/* force an explicit DHCP lease renewal on TAP adapter? */
if (tt->options.dhcp_pre_release)
dhcp_release (tt);
if (tt->options.dhcp_renew)
dhcp_renew (tt);
}
else
fork_dhcp_action (tt);
if (tt->did_ifconfig_setup && tt->options.ip_win32_type == IPW32_SET_IPAPI)
{
DWORD status;
const char *error_suffix = "I am having trouble using the Windows 'IP helper API' to automatically set the IP address -- consider using other --ip-win32 methods (not 'ipapi')";
/* couldn't get adapter index */
if (index == (DWORD)~0)
{
msg (M_FATAL, "ERROR: unable to get adapter index for interface %s -- %s",
device_guid,
error_suffix);
}
/* check dhcp enable status */
if (dhcp_status (index) == DHCP_STATUS_DISABLED)
msg (M_WARN, "NOTE: You have selected (explicitly or by default) '--ip-win32 ipapi', which has a better chance of working correctly if the TAP-Win32 TCP/IP properties are set to 'Obtain an IP address automatically'");
/* delete previously added IP addresses which were not
correctly deleted */
delete_temp_addresses (index);
/* add a new IP address */
if ((status = AddIPAddress (htonl(tt->local),
htonl(tt->adapter_netmask),
index,
&tt->ipapi_context,
&tt->ipapi_instance)) == NO_ERROR)
msg (M_INFO, "Succeeded in adding a temporary IP/netmask of %s/%s to interface %s using the Win32 IP Helper API",
print_in_addr_t (tt->local, 0, &gc),
print_in_addr_t (tt->adapter_netmask, 0, &gc),
device_guid
);
else
msg (M_FATAL, "ERROR: AddIPAddress %s/%s failed on interface %s, index=%d, status=%u (windows error: '%s') -- %s",
print_in_addr_t (tt->local, 0, &gc),
print_in_addr_t (tt->adapter_netmask, 0, &gc),
device_guid,
(int)index,
(unsigned int)status,
strerror_win32 (status, &gc),
error_suffix);
tt->ipapi_context_defined = true;
}
}
/*netcmd_semaphore_release ();*/
gc_free (&gc);
}
const char *
tap_win32_getinfo (const struct tuntap *tt, struct gc_arena *gc)
{
if (tt && tt->hand != NULL)
{
struct buffer out = alloc_buf_gc (256, gc);
DWORD len;
if (DeviceIoControl (tt->hand, TAP_IOCTL_GET_INFO,
BSTR (&out), BCAP (&out),
BSTR (&out), BCAP (&out),
&len, NULL))
{
return BSTR (&out);
}
}
return NULL;
}
void
tun_show_debug (struct tuntap *tt)
{
if (tt && tt->hand != NULL)
{
struct buffer out = alloc_buf (1024);
DWORD len;
while (DeviceIoControl (tt->hand, TAP_IOCTL_GET_LOG_LINE,
BSTR (&out), BCAP (&out),
BSTR (&out), BCAP (&out),
&len, NULL))
{
msg (D_TAP_WIN32_DEBUG, "TAP-Win32: %s", BSTR (&out));
}
free_buf (&out);
}
}
void
close_tun (struct tuntap *tt)
{
struct gc_arena gc = gc_new ();
if (tt)
{
#if 1
if (tt->ipapi_context_defined)
{
DWORD status;
if ((status = DeleteIPAddress (tt->ipapi_context)) != NO_ERROR)
{
msg (M_WARN, "Warning: DeleteIPAddress[%u] failed on TAP-Win32 adapter, status=%u : %s",
(unsigned int)tt->ipapi_context,
(unsigned int)status,
strerror_win32 (status, &gc));
}
}
#endif
if (tt->options.dhcp_release)
dhcp_release (tt);
if (tt->hand != NULL)
{
dmsg (D_WIN32_IO_LOW, "Attempting CancelIO on TAP-Win32 adapter");
if (!CancelIo (tt->hand))
msg (M_WARN | M_ERRNO, "Warning: CancelIO failed on TAP-Win32 adapter");
}
dmsg (D_WIN32_IO_LOW, "Attempting close of overlapped read event on TAP-Win32 adapter");
overlapped_io_close (&tt->reads);
dmsg (D_WIN32_IO_LOW, "Attempting close of overlapped write event on TAP-Win32 adapter");
overlapped_io_close (&tt->writes);
if (tt->hand != NULL)
{
dmsg (D_WIN32_IO_LOW, "Attempting CloseHandle on TAP-Win32 adapter");
if (!CloseHandle (tt->hand))
msg (M_WARN | M_ERRNO, "Warning: CloseHandle failed on TAP-Win32 adapter");
}
if (tt->actual_name)
free (tt->actual_name);
clear_tuntap (tt);
free (tt);
}
gc_free (&gc);
}
/*
* Convert --ip-win32 constants between index and ascii form.
*/
struct ipset_names {
const char *short_form;
};
/* Indexed by IPW32_SET_x */
static const struct ipset_names ipset_names[] = {
{"manual"},
{"netsh"},
{"ipapi"},
{"dynamic"},
{"adaptive"}
};
int
ascii2ipset (const char* name)
{
int i;
ASSERT (IPW32_SET_N == SIZE (ipset_names));
for (i = 0; i < IPW32_SET_N; ++i)
if (!strcmp (name, ipset_names[i].short_form))
return i;
return -1;
}
const char *
ipset2ascii (int index)
{
ASSERT (IPW32_SET_N == SIZE (ipset_names));
if (index < 0 || index >= IPW32_SET_N)
return "[unknown --ip-win32 type]";
else
return ipset_names[index].short_form;
}
const char *
ipset2ascii_all (struct gc_arena *gc)
{
struct buffer out = alloc_buf_gc (256, gc);
int i;
ASSERT (IPW32_SET_N == SIZE (ipset_names));
for (i = 0; i < IPW32_SET_N; ++i)
{
if (i)
buf_printf(&out, " ");
buf_printf(&out, "[%s]", ipset2ascii(i));
}
return BSTR (&out);
}
#else /* generic */
void
open_tun (const char *dev, const char *dev_type, const char *dev_node, bool ipv6, struct tuntap *tt)
{
open_tun_generic (dev, dev_type, dev_node, ipv6, false, true, tt);
}
void
close_tun (struct tuntap* tt)
{
if (tt)
{
close_tun_generic (tt);
free (tt);
}
}
int
write_tun (struct tuntap* tt, uint8_t *buf, int len)
{
return write (tt->fd, buf, len);
}
int
read_tun (struct tuntap* tt, uint8_t *buf, int len)
{
return read (tt->fd, buf, len);
}
#endif
|