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
|
/*
* netlink interface to the kernel's IPsec mechanism
*
* Copyright (C) 2003-2008 Herbert Xu
* Copyright (C) 2006-2008 Michael Richardson <mcr@xelerance.com>
* Copyright (C) 2006 Ken Bantoft <ken@xelerance.com>
* Copyright (C) 2007 Bart Trojanowski <bart@jukie.net>
* Copyright (C) 2007 Ilia Sotnikov
* Copyright (C) 2009 Carsten Schlote <c.schlote@konzeptpark.de>
* Copyright (C) 2008 Andreas Steffen
* Copyright (C) 2008 Neil Horman <nhorman@redhat.com>
* Copyright (C) 2008-2010 David McCullough <david_mccullough@securecomputing.com>
* Copyright (C) 2006-2010 Paul Wouters <paul@xelerance.com>
* Copyright (C) 2010-2017 Tuomo Soini <tis@foobar.fi>
* Copyright (C) 2010 Mika Ilmaranta <ilmis@foobar.fi>
* Copyright (C) 2010 Roman Hoog Antink <rha@open.ch>
* Copyright (C) 2010 D. Hugh Redelmeier
* Copyright (C) 2012 Avesh Agarwal <avagarwa@redhat.com>
* Copyright (C) 2013 Kim B. Heino <b@bbbs.net>
* Copyright (C) 2012-2013 Paul Wouters <paul@libreswan.org>
* Copyright (C) 2013-2019 D. Hugh Redelmeier <hugh@mimosa.com>
* Copyright (C) 2017 Richard Guy Briggs <rgb@tricolour.ca>
* Copyright (C) 2016-2019 Andrew Cagney <cagney@gnu.org>
* Copyright (C) 2019 Paul Wouters <pwouters@redhat.com>
* Copyright (C) 2019 Antony Antony <antony@phenome.org>
* Copyright (C) 2017 Mayank Totale <mtotale@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <linux/udp.h> /* for TCP_ENCAP_ESPINTCP and UDP_ENCAP_ESPINUDP */
#ifndef TCP_ENCAP_ESPINTCP
#define TCP_ENCAP_ESPINTCP 7
#endif
#include <unistd.h>
#include <sys/stat.h>
#include <linux/rtnetlink.h>
#include <linux/if_addr.h>
#include <linux/if_link.h>
/* work around weird combo's of glibc and kernel header conflicts */
#ifndef GLIBC_KERN_FLIP_HEADERS
# include "linux/xfrm.h" /* local (if configured) or system copy */
# include "libreswan.h"
#else
# include "libreswan.h"
# include "linux/xfrm.h" /* local (if configured) or system copy */
#endif
#include "lsw-pfkeyv2.h" /* for SADB_X_CALG_DEFLATE et.al., grrr */
#include "sysdep.h"
#include "socketwrapper.h"
#include "constants.h"
#include "defs.h"
#include "id.h"
#include "state.h"
#include "connections.h"
#include "kernel.h"
#include "server.h"
#include "nat_traversal.h"
#include "state.h"
#include "kernel_xfrm.h"
#include "netlink_attrib.h"
#include "log.h"
#include "whack.h" /* for RC_LOG_SERIOUS */
#include "kernel_alg.h"
#include "ike_alg.h"
#include "ike_alg_integ.h"
#include "ike_alg_encrypt.h"
#include "ip_address.h"
#include "ip_info.h"
# include "kernel_xfrm_interface.h"
#include "iface.h"
#include "ip_selector.h"
#include "ip_encap.h"
#include "labeled_ipsec.h" /* TEMP for MAX_SECCTX_LEN */
/* required for Linux 2.6.26 kernel and later */
#ifndef XFRM_STATE_AF_UNSPEC
#define XFRM_STATE_AF_UNSPEC 32
#endif
static int nl_send_fd = NULL_FD; /* to send to NETLINK_XFRM */
static int nl_xfrm_fd = NULL_FD; /* listen to NETLINK_XFRM broadcast */
static int nl_route_fd = NULL_FD; /* listen to NETLINK_ROUTE broadcast */
static int kernel_mobike_supprt ; /* kernel xfrm_migrate_support */
#define NE(x) { x, #x } /* Name Entry -- shorthand for sparse_names */
enum {
IPSEC_POLICY_DISCARD = 0,
IPSEC_POLICY_NONE = 1,
IPSEC_POLICY_IPSEC = 2,
IPSEC_POLICY_ENTRUST = 3,
IPSEC_POLICY_BYPASS = 4
};
static sparse_names xfrm_type_names = {
NE(NLMSG_NOOP),
NE(NLMSG_ERROR),
NE(NLMSG_DONE),
NE(NLMSG_OVERRUN),
NE(XFRM_MSG_NEWSA),
NE(XFRM_MSG_DELSA),
NE(XFRM_MSG_GETSA),
NE(XFRM_MSG_NEWPOLICY),
NE(XFRM_MSG_DELPOLICY),
NE(XFRM_MSG_GETPOLICY),
NE(XFRM_MSG_ALLOCSPI),
NE(XFRM_MSG_ACQUIRE),
NE(XFRM_MSG_EXPIRE),
NE(XFRM_MSG_UPDPOLICY),
NE(XFRM_MSG_UPDSA),
NE(XFRM_MSG_POLEXPIRE),
NE(XFRM_MSG_MAX),
{ 0, sparse_end }
};
static sparse_names rtm_type_names = {
NE(RTM_BASE),
NE(RTM_NEWADDR),
NE(RTM_DELADDR),
NE(RTM_MAX),
{ 0, sparse_end }
};
#undef NE
#define RTA_TAIL(rta) ((struct rtattr *) (((void *) (rta)) + \
RTA_ALIGN((rta)->rta_len)))
#define NLMSG_TAIL(nmsg) \
((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
/* Compress Algs */
static sparse_names calg_list = {
{ SADB_X_CALG_DEFLATE, "deflate" },
{ SADB_X_CALG_LZS, "lzs" },
{ SADB_X_CALG_LZJH, "lzjh" },
{ 0, sparse_end }
};
/*
* xfrm2ip - Take an xfrm and convert to an IP address
*
* @param xaddr xfrm_address_t
* @param addr ip_address IPv[46] Address from addr is copied here.
*/
static void xfrm2ip(const xfrm_address_t *xaddr, ip_address *addr, const sa_family_t family)
{
shunk_t x = THING_AS_SHUNK(*xaddr);
const struct ip_info *afi = aftoinfo(family);
passert(afi != NULL);
*addr = afi->any_address; /* initialize dst type and zero */
chunk_t a = address_as_chunk(addr);
/* a = x */
passert(x.len >= a.len);
memcpy(a.ptr, x.ptr, a.len);
}
/*
* xfrm_from-address - Take an IP address and convert to an xfrm.
*/
static xfrm_address_t xfrm_from_address(const ip_address *addr)
{
xfrm_address_t xaddr;
zero(&xaddr);
shunk_t a = address_as_shunk(addr);
/* .len == ipv6 len */
chunk_t x = THING_AS_CHUNK(xaddr);
/* x = a */
passert(x.len >= a.len);
memcpy(x.ptr, a.ptr, a.len);
return xaddr;
}
#define SELECTOR_TO_XFRM(CLIENT, REQ, L) \
{ \
ip_selector client_ = *(CLIENT); \
ip_address address = selector_prefix(&client_); \
(REQ).L##addr = xfrm_from_address(&address); \
(REQ).prefixlen_##L = selector_maskbits(&client_); \
(REQ).L##port = nport(selector_port(&client_)); \
}
static void init_netlink_route_fd(struct logger *logger)
{
nl_route_fd = safe_socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (nl_route_fd < 0) {
fatal_errno(PLUTO_EXIT_FAIL, logger, errno, "socket()");
}
if (fcntl(nl_route_fd, F_SETFD, FD_CLOEXEC) != 0) {
fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
"fcntl(FD_CLOEXEC) for bcast NETLINK_ROUTE ");
}
if (fcntl(nl_route_fd, F_SETFL, O_NONBLOCK) != 0) {
fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
"fcntl(O_NONBLOCK) for bcast NETLINK_ROUTE");
}
struct sockaddr_nl addr = {
.nl_family = AF_NETLINK,
.nl_pid = getpid(),
.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE | RTMGRP_LINK,
};
if (bind(nl_route_fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
"failed to bind NETLINK_ROUTE bcast socket - Perhaps kernel was not compiled with CONFIG_XFRM");
}
}
/*
* init_netlink - Initialize the netlink interface. Opens the sockets and
* then binds to the broadcast socket.
*/
static void init_netlink(struct logger *logger)
{
#define XFRM_ACQ_EXPIRES "/proc/sys/net/core/xfrm_acq_expires"
struct stat buf;
if (stat(XFRM_ACQ_EXPIRES, &buf) != 0) {
fatal_errno(PLUTO_EXIT_KERNEL_FAIL, logger, errno,
"no XFRM kernel support detected, missing "XFRM_ACQ_EXPIRES);
}
struct sockaddr_nl addr;
nl_send_fd = safe_socket(AF_NETLINK, SOCK_DGRAM, NETLINK_XFRM);
if (nl_send_fd < 0) {
fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
"socket() in init_netlink()");
}
if (fcntl(nl_send_fd, F_SETFD, FD_CLOEXEC) != 0) {
fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
"fcntl(FD_CLOEXEC) in init_netlink()");
}
nl_xfrm_fd = safe_socket(AF_NETLINK, SOCK_DGRAM, NETLINK_XFRM);
if (nl_xfrm_fd < 0) {
fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
"socket() for bcast in init_netlink()");
}
if (fcntl(nl_xfrm_fd, F_SETFD, FD_CLOEXEC) != 0) {
fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
"fcntl(FD_CLOEXEC) for bcast in init_netlink()");
}
if (fcntl(nl_xfrm_fd, F_SETFL, O_NONBLOCK) != 0) {
fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
"fcntl(O_NONBLOCK) for bcast in init_netlink()");
}
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
addr.nl_pad = 0; /* make coverity happy */
addr.nl_groups = XFRMGRP_ACQUIRE | XFRMGRP_EXPIRE;
if (bind(nl_xfrm_fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
"Failed to bind bcast socket in init_netlink() - Perhaps kernel was not compiled with CONFIG_XFRM");
}
init_netlink_route_fd(logger);
/*
* pfkey_register_response() does not register an entry for
* msg->sadb_msg_satype=10 to indicate IPCOMP, so we override
* detection here. Seems the PF_KEY API in Linux with netkey
* is a joke that should be abandoned for a "linux children"
* native netlink query/response
*
* XXX: Given KLIPS defines K_SADB_X_SATYPE_COMP=9, and
* IPIP=10 which conflicts with the aboe, that might be the
* source of the problem?
*/
can_do_IPcomp = TRUE;
/*
* Just assume any algorithm with a NETLINK_XFRM name works.
*
* Kind of lame since pluto should query the kernel for what
* it supports. OTOH, the query might happen before the
* crypto module gets loaded.
*/
dbg("Hard-wiring algorithms");
for (const struct encrypt_desc **algp = next_encrypt_desc(NULL);
algp != NULL; algp = next_encrypt_desc(algp)) {
const struct encrypt_desc *alg = *algp;
if (alg->encrypt_netlink_xfrm_name != NULL) {
kernel_encrypt_add(alg);
}
}
for (const struct integ_desc **algp = next_integ_desc(NULL);
algp != NULL; algp = next_integ_desc(algp)) {
const struct integ_desc *alg = *algp;
if (alg->integ_netlink_xfrm_name != NULL) {
kernel_integ_add(alg);
}
}
}
/*
* send_netlink_msg
*
* @param hdr - Data to be sent.
* @param expected_resp_type - type of message expected from netlink
* @param rbuf - Return Buffer - contains data returned from the send.
* @param description - String - user friendly description of what is
* being attempted. Used for diagnostics
* @param text_said - String
* @return bool True if the message was successfully sent.
*/
static int netlink_errno; /* side-channel result of send_netlink_msg */
static bool send_netlink_msg(struct nlmsghdr *hdr,
unsigned expected_resp_type, struct nlm_resp *rbuf,
const char *description, const char *text_said,
struct logger *logger)
{
struct nlm_resp rsp;
size_t len;
ssize_t r;
struct sockaddr_nl addr;
static uint32_t seq = 0; /* STATIC */
netlink_errno = 0;
hdr->nlmsg_seq = ++seq;
len = hdr->nlmsg_len;
do {
r = write(nl_send_fd, hdr, len);
} while (r < 0 && errno == EINTR);
if (r < 0) {
log_errno(logger, errno,
"netlink write() of %s message for %s %s failed",
sparse_val_show(xfrm_type_names,
hdr->nlmsg_type),
description, text_said);
return FALSE;
} else if ((size_t)r != len) {
llog(RC_LOG_SERIOUS, logger,
"ERROR: netlink write() of %s message for %s %s truncated: %zd instead of %zu",
sparse_val_show(xfrm_type_names, hdr->nlmsg_type),
description, text_said, r, len);
return FALSE;
}
for (;;) {
socklen_t alen = sizeof(addr);
r = recvfrom(nl_send_fd, &rsp, sizeof(rsp), 0,
(struct sockaddr *)&addr, &alen);
if (r < 0) {
if (errno == EINTR)
continue;
netlink_errno = errno;
log_errno(logger, errno,
"netlink recvfrom() of response to our %s message for %s %s failed",
sparse_val_show(xfrm_type_names,
hdr->nlmsg_type),
description, text_said);
return FALSE;
} else if ((size_t) r < sizeof(rsp.n)) {
llog(RC_LOG, logger,
"netlink read truncated message: %zd bytes; ignore message", r);
continue;
} else if (addr.nl_pid != 0) {
/* not for us: ignore */
dbg("netlink: ignoring %s message from process %u",
sparse_val_show(xfrm_type_names, rsp.n.nlmsg_type),
addr.nl_pid);
continue;
} else if (rsp.n.nlmsg_seq != seq) {
dbg("netlink: ignoring out of sequence (%u/%u) message %s",
rsp.n.nlmsg_seq, seq,
sparse_val_show(xfrm_type_names, rsp.n.nlmsg_type));
continue;
}
break;
}
if (rsp.n.nlmsg_len > (size_t) r) {
llog(RC_LOG_SERIOUS, logger,
"netlink recvfrom() of response to our %s message for %s %s was truncated: %zd instead of %zu",
sparse_val_show(xfrm_type_names, hdr->nlmsg_type),
description, text_said,
len, (size_t) rsp.n.nlmsg_len);
return FALSE;
}
if (rsp.n.nlmsg_type != expected_resp_type && rsp.n.nlmsg_type == NLMSG_ERROR) {
if (rsp.u.e.error != 0) {
llog(RC_LOG_SERIOUS, logger,
"ERROR: netlink response for %s %s included errno %d: %s",
description, text_said, -rsp.u.e.error,
strerror(-rsp.u.e.error));
return FALSE;
}
/*
* What the heck does a 0 error mean?
* Since the caller doesn't depend on the result
* we'll let it pass.
* This really happens for netlink_add_sa().
*/
dbg("netlink response for %s %s included non-error error",
description, text_said);
/* ignore */
}
if (rbuf == NULL) {
return TRUE;
}
if (rsp.n.nlmsg_type != expected_resp_type) {
llog(RC_LOG_SERIOUS, logger,
"netlink recvfrom() of response to our %s message for %s %s was of wrong type (%s)",
sparse_val_show(xfrm_type_names, hdr->nlmsg_type),
description, text_said,
sparse_val_show(xfrm_type_names, rsp.n.nlmsg_type));
return FALSE;
}
memcpy(rbuf, &rsp, r);
return TRUE;
}
/*
* netlink_policy -
*
* @param hdr - Data to check
* @param enoent_ok - Boolean - OK or not OK.
* @param text_said - String
* @return boolean
*/
static bool netlink_policy(struct nlmsghdr *hdr, bool enoent_ok,
const char *text_said, struct logger *logger)
{
struct nlm_resp rsp;
if (!send_netlink_msg(hdr, NLMSG_ERROR, &rsp,
"policy", text_said, logger))
return FALSE;
/* kind of surprising: we get here by success which implies an error structure! */
int error = -rsp.u.e.error;
if (error == 0 || (error == ENOENT && enoent_ok))
return TRUE;
llog(RC_LOG_SERIOUS, logger,
"ERROR: netlink %s response for flow %s included errno %d: %s",
sparse_val_show(xfrm_type_names, hdr->nlmsg_type),
text_said, error, strerror(error));
return FALSE;
}
/*
* netlink_raw_eroute
*
* @param this_host ip_address
* @param this_client ip_subnet
* @param that_host ip_address
* @param that_client ip_subnet
* @param spi
* @param sa_proto int (4=tunnel, 50=esp, 108=ipcomp, etc ...)
* @param transport_proto unsigned int Contains protocol
* (6=tcp, 17=udp, etc...)
* @param esatype int
* @param pfkey_proto_info proto_info
* @param use_lifetime monotime_t (Currently unused)
* @param pluto_sadb_opterations sadb_op (operation - ie: ERO_DELETE)
* @param text_said char
* @return boolean True if successful
*/
static bool netlink_raw_eroute(const ip_address *this_host,
const ip_subnet *this_client,
const ip_address *that_host,
const ip_subnet *that_client,
ipsec_spi_t cur_spi, /* current SPI */
ipsec_spi_t new_spi, /* new SPI */
const struct ip_protocol *sa_proto,
unsigned int transport_proto,
enum eroute_type esatype,
const struct pfkey_proto_info *proto_info,
deltatime_t use_lifetime UNUSED,
uint32_t sa_priority,
const struct sa_marks *sa_marks,
const uint32_t xfrm_if_id,
enum pluto_sadb_operations sadb_op,
const char *text_said,
const chunk_t *sec_label,
struct logger *logger)
{
struct {
struct nlmsghdr n;
union {
struct xfrm_userpolicy_info p;
struct xfrm_userpolicy_id id;
} u;
char data[MAX_NETLINK_DATA_SIZE];
} req;
int policy = IPSEC_POLICY_IPSEC;
if (sadb_op == ERO_DELETE && proto_info[0].reqid == 0 &&
(ntohl(new_spi) == SPI_PASS || ntohl(new_spi) == SPI_HOLD) &&
strstr("IGNORE_ON_XFRM", text_said) != NULL) {
dbg("request to delete an opportunistic bare shunt ignored - XFRM already deleted it when it installed IPsec SA, text_said:%s", text_said);
return TRUE;
}
switch (esatype) {
case ET_UNSPEC:
case ET_AH:
case ET_ESP:
case ET_IPCOMP:
case ET_IPIP:
break;
case ET_INT:
/* shunt route */
switch (ntohl(new_spi)) {
case SPI_PASS:
dbg("netlink_raw_eroute: SPI_PASS");
policy = IPSEC_POLICY_NONE;
break;
case SPI_HOLD:
/*
* We don't know how to implement %hold, but it is okay.
* When we need a hold, the kernel XFRM acquire state
* will do the job (by dropping or holding the packet)
* until this entry expires. See /proc/sys/net/core/xfrm_acq_expires
* After expiration, the underlying policy causing the original acquire
* will fire again, dropping further packets.
*/
dbg("netlink_raw_eroute: SPI_HOLD implemented as no-op");
return TRUE; /* yes really */
case SPI_DROP:
case SPI_REJECT:
case 0: /* used with type=passthrough - can it not use SPI_PASS ?? */
policy = IPSEC_POLICY_DISCARD;
break;
case SPI_TRAP:
if (sadb_op == ERO_ADD_INBOUND ||
sadb_op == ERO_DEL_INBOUND)
return TRUE;
break;
case SPI_TRAPSUBNET: /* unused in our code */
default:
bad_case(ntohl(new_spi));
}
break;
default:
bad_case(esatype);
}
const int dir = (sadb_op == ERO_ADD_INBOUND || sadb_op == ERO_DEL_INBOUND) ?
XFRM_POLICY_IN : XFRM_POLICY_OUT;
/*
* Bug #1004 fix.
* There really isn't "client" with XFRM and transport mode
* so eroute must be done to natted, visible ip. If we don't hide
* internal IP, communication doesn't work.
*/
ip_selector local_client;
if (esatype == ET_ESP || esatype == ET_IPCOMP || sa_proto == &ip_protocol_esp) {
/*
* Variable "that" should be remote, but here it's not.
* We must check "dir" to find out remote address.
*/
int local_port;
if (dir == XFRM_POLICY_OUT) {
local_port = selector_hport(that_client);
local_client = subnet_from_address(that_host);
that_client = &local_client;
} else {
local_port = selector_hport(this_client);
local_client = subnet_from_address(this_host);
this_client = &local_client;
}
update_selector_hport(&local_client, local_port);
dbg("%s: using host address instead of client subnet", __func__);
}
zero(&req);
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
const int family = subnet_type(that_client)->af;
/* .[sd]addr, .prefixlen_[sd], .[sd]port */
SELECTOR_TO_XFRM(this_client, req.u.p.sel, s);
SELECTOR_TO_XFRM(that_client, req.u.p.sel, d);
/*
* Munge .[sd]port?
*
* As per RFC 4301/5996, icmp type is put in the most significant
* 8 bits and icmp code is in the least significant 8 bits of port
* field.
* Although Libreswan does not have any configuration options for
* icmp type/code values, it is possible to specify icmp type and code
* using protoport option. For example, icmp echo request
* (type 8/code 0) needs to be encoded as 0x0800 in the port field
* and can be specified as left/rightprotoport=icmp/2048. Now with
* XFRM, icmp type and code need to be passed as source and
* destination ports, respectively. Therefore, this code extracts
* upper 8 bits and lower 8 bits and puts into source and destination
* ports before passing to XFRM.
*/
if (transport_proto == IPPROTO_ICMP ||
transport_proto == IPPROTO_ICMPV6) {
uint16_t icmp_type;
uint16_t icmp_code;
icmp_type = ntohs(req.u.p.sel.sport) >> 8;
icmp_code = ntohs(req.u.p.sel.sport) & 0xFF;
req.u.p.sel.sport = htons(icmp_type);
req.u.p.sel.dport = htons(icmp_code);
}
req.u.p.sel.sport_mask = req.u.p.sel.sport == 0 ? 0 : ~0;
req.u.p.sel.dport_mask = req.u.p.sel.dport == 0 ? 0 : ~0;
req.u.p.sel.proto = transport_proto;
req.u.p.sel.family = family;
if (sadb_op == ERO_DELETE || sadb_op == ERO_DEL_INBOUND) {
req.u.id.dir = dir;
req.n.nlmsg_type = XFRM_MSG_DELPOLICY;
req.n.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.u.id)));
} else {
req.u.p.dir = dir;
/* The caller should have set the proper priority by now */
req.u.p.priority = sa_priority;
dbg("IPsec SA SPD priority set to %d", req.u.p.priority);
req.u.p.action = XFRM_POLICY_ALLOW;
if (policy == IPSEC_POLICY_DISCARD)
req.u.p.action = XFRM_POLICY_BLOCK;
/* req.u.p.lft.soft_use_expires_seconds = deltasecs(use_lifetime); */
req.u.p.lft.soft_byte_limit = XFRM_INF;
req.u.p.lft.soft_packet_limit = XFRM_INF;
req.u.p.lft.hard_byte_limit = XFRM_INF;
req.u.p.lft.hard_packet_limit = XFRM_INF;
/*
* NEW will fail when an existing policy, UPD always works.
* This seems to happen in cases with NAT'ed XP clients, or
* quick recycling/resurfacing of roadwarriors on the same IP.
*
* UPD is also needed for two separate tunnels with same end
* subnets
* Like A = B = C config where both A - B and B - C have
* tunnel A = C configured.
*/
req.n.nlmsg_type = XFRM_MSG_UPDPOLICY;
if (sadb_op == ERO_REPLACE)
req.n.nlmsg_type = XFRM_MSG_UPDPOLICY;
req.n.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.u.p)));
}
if (policy == IPSEC_POLICY_IPSEC || policy == IPSEC_POLICY_DISCARD) {
if (sadb_op != ERO_DELETE) {
struct rtattr *attr;
struct xfrm_user_tmpl tmpl[4];
int i;
zero(&tmpl);
for (i = 0; proto_info[i].proto; i++) {
tmpl[i].reqid = proto_info[i].reqid;
tmpl[i].id.proto = proto_info[i].proto;
tmpl[i].optional = proto_info[i].proto == IPPROTO_COMP && dir != XFRM_POLICY_OUT;
tmpl[i].aalgos = tmpl[i].ealgos = tmpl[i].calgos = ~0;
tmpl[i].family = addrtypeof(that_host);
tmpl[i].mode = proto_info[i].mode == ENCAPSULATION_MODE_TUNNEL;
if (!tmpl[i].mode)
continue;
tmpl[i].saddr = xfrm_from_address(this_host);
tmpl[i].id.daddr = xfrm_from_address(that_host);
}
attr = (struct rtattr *)((char *)&req + req.n.nlmsg_len);
attr->rta_type = XFRMA_TMPL;
attr->rta_len = i * sizeof(tmpl[0]);
memcpy(RTA_DATA(attr), tmpl, attr->rta_len);
attr->rta_len = RTA_LENGTH(attr->rta_len);
req.n.nlmsg_len += attr->rta_len;
}
/* mark policy extension */
{
struct sa_mark sa_mark = (dir == XFRM_POLICY_IN) ? sa_marks->in : sa_marks->out;
if (sa_mark.val != 0 && sa_mark.mask != 0 && xfrm_if_id == 0) {
struct xfrm_mark xfrm_mark;
struct rtattr* mark_attr;
xfrm_mark.v = sa_mark.val;
xfrm_mark.m = sa_mark.mask;
mark_attr = (struct rtattr *)((char *)&req + req.n.nlmsg_len);
mark_attr->rta_type = XFRMA_MARK;
mark_attr->rta_len = sizeof(xfrm_mark);
memcpy(RTA_DATA(mark_attr), &xfrm_mark, mark_attr->rta_len);
mark_attr->rta_len = RTA_LENGTH(mark_attr->rta_len);
req.n.nlmsg_len += mark_attr->rta_len;
}
}
#ifdef USE_XFRM_INTERFACE
if (xfrm_if_id != 0) {
dbg("%s netlink: XFRMA_IF_ID %" PRIu32 " req.n.nlmsg_type=%" PRIu32,
__func__, xfrm_if_id, req.n.nlmsg_type);
nl_addattr32(&req.n, sizeof(req.data), XFRMA_IF_ID, xfrm_if_id);
if (sa_marks->out.val == 0 && sa_marks->out.mask == 0) {
/* XFRMA_SET_MARK = XFRMA_IF_ID */
nl_addattr32(&req.n, sizeof(req.data), XFRMA_SET_MARK, xfrm_if_id);
} else {
/* manually configured mark-out=mark/mask */
nl_addattr32(&req.n, sizeof(req.data),
XFRMA_SET_MARK, sa_marks->out.val);
nl_addattr32(&req.n, sizeof(req.data),
XFRMA_SET_MARK_MASK, sa_marks->out.mask);
}
}
#endif
}
if (sec_label != NULL && sec_label->len > 0) {
struct rtattr *attr = (struct rtattr *)
((char *)&req + req.n.nlmsg_len);
struct xfrm_user_sec_ctx *uctx;
passert(sec_label->len <= MAX_SECCTX_LEN);
attr->rta_type = XFRMA_SEC_CTX;
dbg("passing security label \"%.*s\" to kernel", (int)sec_label->len, sec_label->ptr);
attr->rta_len = RTA_LENGTH(sizeof(struct xfrm_user_sec_ctx) + sec_label->len);
uctx = RTA_DATA(attr);
uctx->exttype = XFRMA_SEC_CTX;
uctx->len = sizeof(struct xfrm_user_sec_ctx) + sec_label->len;
uctx->ctx_doi = XFRM_SC_DOI_LSM;
uctx->ctx_alg = XFRM_SC_ALG_SELINUX;
uctx->ctx_len = sec_label->len;
memcpy(uctx + 1, sec_label->ptr, sec_label->len);
req.n.nlmsg_len += attr->rta_len;
}
bool enoent_ok = sadb_op == ERO_DEL_INBOUND ||
(sadb_op == ERO_DELETE && ntohl(cur_spi) == SPI_HOLD);
bool ok = netlink_policy(&req.n, enoent_ok, text_said, logger);
/* ??? deal with any forwarding policy */
switch (dir) {
case XFRM_POLICY_IN:
if (req.n.nlmsg_type == XFRM_MSG_DELPOLICY) {
/* ??? we will call netlink_policy even if !ok. */
req.u.id.dir = XFRM_POLICY_FWD;
} else if (!ok) {
break;
} else if (proto_info[0].mode != ENCAPSULATION_MODE_TUNNEL &&
esatype != ET_INT) {
break;
} else {
req.u.p.dir = XFRM_POLICY_FWD;
}
ok &= netlink_policy(&req.n, enoent_ok, text_said, logger);
break;
}
return ok;
}
static void set_migration_attr(const struct kernel_sa *sa,
struct xfrm_user_migrate *m)
{
m->old_saddr = xfrm_from_address(sa->src.address);
m->old_daddr = xfrm_from_address(sa->dst.address);
m->new_saddr = xfrm_from_address(&sa->src.new_address);
m->new_daddr = xfrm_from_address(&sa->dst.new_address);
if (sa->mode == ENCAPSULATION_MODE_TUNNEL)
m->mode = XFRM_MODE_TUNNEL;
else
m->mode = XFRM_MODE_TRANSPORT;
m->proto = sa->proto->ipproto;
m->reqid = sa->reqid;
m->old_family = m->new_family = address_type(sa->src.address)->af;
}
static bool create_xfrm_migrate_sa(struct state *st, const int dir,
struct kernel_sa *ret_sa, char *text_said)
{
const struct connection *const c = st->st_connection;
const struct ip_encap *encap_type =
(st->st_interface->protocol == &ip_protocol_tcp) ? &ip_encap_esp_in_tcp :
(st->hidden_variables.st_nat_traversal & NAT_T_DETECTED) ? &ip_encap_esp_in_udp :
NULL;
dbg("TCP/NAT: encap type "PRI_IP_ENCAP, pri_ip_encap(encap_type));
const struct ip_protocol *proto;
struct ipsec_proto_info *proto_info;
if (st->st_esp.present) {
proto = &ip_protocol_esp;
proto_info = &st->st_esp;
} else if (st->st_ah.present) {
proto = &ip_protocol_ah;
proto_info = &st->st_ah;
} else {
return FALSE;
}
struct kernel_sa sa = {
.xfrm_dir = dir,
.proto = proto,
.reqid = reqid_esp(c->spd.reqid),
.encap_type = encap_type,
/* WWW what about sec_label? */
};
if (st->st_ah.attrs.mode == ENCAPSULATION_MODE_TUNNEL ||
st->st_esp.attrs.mode == ENCAPSULATION_MODE_TUNNEL)
sa.mode = ENCAPSULATION_MODE_TUNNEL;
else
sa.mode = ENCAPSULATION_MODE_TRANSPORT;
ip_endpoint new_endpoint;
uint16_t old_port;
uint16_t encap_sport = 0;
uint16_t encap_dport = 0;
const ip_address *src, *dst;
const ip_subnet *src_client, *dst_client;
if (endpoint_is_specified(&st->st_mobike_local_endpoint)) {
char *n = jam_str(text_said, SAMIGTOT_BUF, "initiator migrate kernel SA ");
passert((SAMIGTOT_BUF - strlen(text_said)) > SATOT_BUF);
old_port = endpoint_hport(&st->st_interface->local_endpoint);
new_endpoint = st->st_mobike_local_endpoint;
if (dir == XFRM_POLICY_IN || dir == XFRM_POLICY_FWD) {
src = &c->spd.that.host_addr;
dst = &c->spd.this.host_addr;
src_client = &c->spd.that.client;
dst_client = &c->spd.this.client;
sa.src.new_address = *src;
sa.dst.new_address = endpoint_address(&st->st_mobike_local_endpoint);
sa.spi = proto_info->our_spi;
set_text_said(n, dst, sa.spi, proto);
if (encap_type != NULL) {
encap_sport = endpoint_hport(&st->st_remote_endpoint);
encap_dport = endpoint_hport(&st->st_mobike_local_endpoint);
}
} else {
src = &c->spd.this.host_addr;
dst = &c->spd.that.host_addr;
src_client = &c->spd.this.client;
dst_client = &c->spd.that.client;
sa.src.new_address = endpoint_address(&st->st_mobike_local_endpoint);
sa.dst.new_address = *dst;
sa.spi = proto_info->attrs.spi;
set_text_said(n, src, sa.spi, proto);
if (encap_type != NULL) {
encap_sport = endpoint_hport(&st->st_mobike_local_endpoint);
encap_dport = endpoint_hport(&st->st_remote_endpoint);
}
}
} else {
char *n = jam_str(text_said, SAMIGTOT_BUF, "responder migrate kernel SA ");
passert((SAMIGTOT_BUF - strlen(text_said)) > SATOT_BUF);
old_port = endpoint_hport(&st->st_remote_endpoint);
new_endpoint = st->st_mobike_remote_endpoint;
if (dir == XFRM_POLICY_IN || dir == XFRM_POLICY_FWD) {
src = &c->spd.that.host_addr;
dst = &c->spd.this.host_addr;
src_client = &c->spd.that.client;
dst_client = &c->spd.this.client;
sa.src.new_address = endpoint_address(&st->st_mobike_remote_endpoint);
sa.dst.new_address = c->spd.this.host_addr;
sa.spi = proto_info->our_spi;
set_text_said(n, src, sa.spi, proto);
if (encap_type != NULL) {
encap_sport = endpoint_hport(&st->st_mobike_remote_endpoint);
encap_dport = endpoint_hport(&st->st_interface->local_endpoint);
}
} else {
src = &c->spd.this.host_addr;
dst = &c->spd.that.host_addr;
src_client = &c->spd.this.client;
dst_client = &c->spd.that.client;
sa.src.new_address = c->spd.this.host_addr;
sa.dst.new_address = endpoint_address(&st->st_mobike_remote_endpoint);
sa.spi = proto_info->attrs.spi;
set_text_said(n, dst, sa.spi, proto);
if (encap_type != NULL) {
encap_sport = endpoint_hport(&st->st_interface->local_endpoint);
encap_dport = endpoint_hport(&st->st_mobike_remote_endpoint);
}
}
}
sa.src.address = src;
sa.dst.address = dst;
sa.text_said = text_said;
sa.src.client = src_client;
sa.dst.client = dst_client;
sa.src.encap_port = encap_sport;
sa.dst.encap_port = encap_dport;
char reqid_buf[ULTOT_BUF + 32];
endpoint_buf ra;
snprintf(reqid_buf, sizeof(reqid_buf), ":%u to %s reqid=%u %s",
old_port,
str_endpoint(&new_endpoint, &ra),
sa.reqid,
enum_name(&netkey_sa_dir_names, dir));
add_str(text_said, SAMIGTOT_BUF, text_said, reqid_buf);
dbg("%s", text_said);
*ret_sa = sa;
return TRUE;
}
static bool migrate_xfrm_sa(const struct kernel_sa *sa,
struct logger *logger)
{
struct {
struct nlmsghdr n;
struct xfrm_userpolicy_id id;
char data[MAX_NETLINK_DATA_SIZE];
} req;
struct nlm_resp rsp;
struct rtattr *attr;
zero(&req);
req.id.dir = sa->xfrm_dir;
req.id.sel.family = address_type(sa->src.address)->af;
/* .[sd]addr, .prefixlen_[sd], .[sd]port */
SELECTOR_TO_XFRM(sa->src.client, req.id.sel, s);
SELECTOR_TO_XFRM(sa->dst.client, req.id.sel, d);
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
req.n.nlmsg_type = XFRM_MSG_MIGRATE;
req.n.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.id)));
/* add attrs[XFRM_MSG_MIGRATE] */
{
struct xfrm_user_migrate migrate;
zero(&migrate);
attr = (struct rtattr *)((char *)&req + req.n.nlmsg_len);
attr->rta_type = XFRMA_MIGRATE;
attr->rta_len = sizeof(migrate);
set_migration_attr(sa, &migrate);
memcpy(RTA_DATA(attr), &migrate, attr->rta_len);
attr->rta_len = RTA_LENGTH(attr->rta_len);
req.n.nlmsg_len += attr->rta_len;
}
if (sa->encap_type != NULL) {
dbg("adding xfrm_encap_templ when migrating sa encap_type="PRI_IP_ENCAP" sport=%d dport=%d",
pri_ip_encap(sa->encap_type),
sa->src.encap_port, sa->dst.encap_port);
attr = (struct rtattr *)((char *)&req + req.n.nlmsg_len);
struct xfrm_encap_tmpl natt;
natt.encap_type = sa->encap_type->encap_type;
natt.encap_sport = ntohs(sa->src.encap_port);
natt.encap_dport = ntohs(sa->dst.encap_port);
zero(&natt.encap_oa);
attr->rta_type = XFRMA_ENCAP;
attr->rta_len = RTA_LENGTH(sizeof(natt));
memcpy(RTA_DATA(attr), &natt, sizeof(natt));
req.n.nlmsg_len += attr->rta_len;
}
bool r = send_netlink_msg(&req.n, NLMSG_ERROR, &rsp,
"mobike", sa->text_said, logger);
if (!r)
return FALSE;
if (rsp.u.e.error < 0) {
/* error is already logged */
return FALSE;
}
return TRUE;
}
static bool netlink_migrate_sa(struct state *st)
{
struct kernel_sa sa;
char mig_said[SAMIGTOT_BUF];
return
create_xfrm_migrate_sa(st, XFRM_POLICY_OUT, &sa, mig_said) &&
migrate_xfrm_sa(&sa, st->st_logger) &&
create_xfrm_migrate_sa(st, XFRM_POLICY_IN, &sa, mig_said) &&
migrate_xfrm_sa(&sa, st->st_logger) &&
create_xfrm_migrate_sa(st, XFRM_POLICY_FWD, &sa, mig_said) &&
migrate_xfrm_sa(&sa, st->st_logger);
}
/* see /usr/include/linux/ethtool.h */
enum nic_offload_state {
NIC_OFFLOAD_UNKNOWN,
NIC_OFFLOAD_UNSUPPORTED,
NIC_OFFLOAD_SUPPORTED
};
static struct {
unsigned int bit;
unsigned int total_blocks;
enum nic_offload_state state;
} netlink_esp_hw_offload;
static bool siocethtool(const char *ifname, void *data, const char *action, struct logger *logger)
{
struct ifreq ifr = { .ifr_data = data };
jam_str(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
if (ioctl(nl_send_fd, SIOCETHTOOL, &ifr) != 0) {
/* EOPNOTSUPP is expected if kernel doesn't support this */
if (errno == EOPNOTSUPP) {
dbg("cannot offload to %s because SIOCETHTOOL %s failed: %s",
ifname, action, strerror(errno));
} else {
log_errno(logger, errno, "can't offload to %s because SIOCETHTOOL %s failed",
ifname, action);
}
return false;
} else {
return true;
}
}
static void netlink_find_offload_feature(const char *ifname,
struct logger *logger)
{
netlink_esp_hw_offload.state = NIC_OFFLOAD_UNSUPPORTED;
/* Determine number of device-features */
struct ethtool_sset_info *sset_info = alloc_bytes(
sizeof(*sset_info) + sizeof(sset_info->data[0]),
"ethtool_sset_info");
sset_info->cmd = ETHTOOL_GSSET_INFO;
sset_info->sset_mask = 1ULL << ETH_SS_FEATURES;
if (!siocethtool(ifname, sset_info, "ETHTOOL_GSSET_INFO", logger) ||
sset_info->sset_mask != 1ULL << ETH_SS_FEATURES) {
pfree(sset_info);
llog(RC_LOG, logger, "Kernel does not support NIC esp-hw-offload (ETHTOOL_GSSET_INFO failed)");
return;
}
uint32_t sset_len = sset_info->data[0];
pfree(sset_info);
/* Retrieve names of device-features */
struct ethtool_gstrings *cmd = alloc_bytes(
sizeof(*cmd) + ETH_GSTRING_LEN * sset_len, "ethtool_gstrings");
cmd->cmd = ETHTOOL_GSTRINGS;
cmd->string_set = ETH_SS_FEATURES;
if (siocethtool(ifname, cmd, "ETHTOOL_GSTRINGS", logger)) {
/* Look for the ESP_HW feature bit */
char *str = (char *)cmd->data;
for (uint32_t i = 0; i < cmd->len; i++) {
if (strneq(str, "esp-hw-offload", ETH_GSTRING_LEN)) {
netlink_esp_hw_offload.bit = i;
netlink_esp_hw_offload.total_blocks = (sset_len + 31) / 32;
netlink_esp_hw_offload.state = NIC_OFFLOAD_SUPPORTED;
break;
}
str += ETH_GSTRING_LEN;
}
}
pfree(cmd);
if (netlink_esp_hw_offload.state == NIC_OFFLOAD_SUPPORTED) {
llog(RC_LOG, logger, "Kernel supports NIC esp-hw-offload");
} else {
llog(RC_LOG, logger, "Kernel does not support NIC esp-hw-offload");
}
}
static bool netlink_detect_offload(const struct raw_iface *ifp, struct logger *logger)
{
const char *ifname = ifp->name;
/*
* Kernel requires a real interface in order to query the kernel-wide
* capability, so we do it here on first invocation.
*/
if (netlink_esp_hw_offload.state == NIC_OFFLOAD_UNKNOWN)
netlink_find_offload_feature(ifname, logger);
if (netlink_esp_hw_offload.state == NIC_OFFLOAD_UNSUPPORTED) {
return false;
}
/* Feature is supported by kernel. Query device features */
struct ethtool_gfeatures *cmd = alloc_bytes(
sizeof(*cmd) + sizeof(cmd->features[0]) * netlink_esp_hw_offload.total_blocks,
"ethtool_gfeatures");
cmd->cmd = ETHTOOL_GFEATURES;
cmd->size = netlink_esp_hw_offload.total_blocks;
bool ret = false;
if (siocethtool(ifname, cmd, "ETHTOOL_GFEATURES", logger)) {
int block = netlink_esp_hw_offload.bit / 32;
uint32_t feature_bit = 1U << (netlink_esp_hw_offload.bit % 32);
if (cmd->features[block].active & feature_bit)
ret = true;
}
pfree(cmd);
return ret;
}
/*
* netlink_add_sa - Add an SA into the kernel SPDB via netlink
*
* @param sa Kernel SA to add/modify
* @param replace boolean - true if this replaces an existing SA
* @return bool True if successful
*/
static bool netlink_add_sa(const struct kernel_sa *sa, bool replace,
struct logger *logger)
{
struct {
struct nlmsghdr n;
struct xfrm_usersa_info p;
char data[MAX_NETLINK_DATA_SIZE];
} req;
struct rtattr *attr;
int ret;
zero(&req);
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
req.n.nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
req.p.saddr = xfrm_from_address(sa->src.address);
req.p.id.daddr = xfrm_from_address(sa->dst.address);
req.p.id.spi = sa->spi;
req.p.id.proto = esatype2proto(sa->esatype);
req.p.family = addrtypeof(sa->src.address);
/*
* This requires ipv6 modules. It is required to support 6in4 and 4in6
* tunnels in linux 2.6.25+
*/
if (sa->mode == ENCAPSULATION_MODE_TUNNEL) {
dbg("netlink: enabling tunnel mode");
req.p.mode = XFRM_MODE_TUNNEL;
req.p.flags |= XFRM_STATE_AF_UNSPEC;
} else {
dbg("netlink: enabling transport mode");
req.p.mode = XFRM_MODE_TRANSPORT;
}
/*
* We only add traffic selectors for transport mode. The problem is
* that Tunnel mode ipsec with ipcomp is layered so that ipcomp
* tunnel is protected with transport mode ipsec but in this case we
* shouldn't any more add traffic selectors. Caller function will
* inform us if we need or don't need selectors.
*/
if (sa->add_selector) {
ip_selector src = *sa->src.client;
ip_selector dst = *sa->dst.client;
const ip_protocol *protocol = protocol_by_ipproto(sa->transport_proto);
/*
* With XFRM/NETKEY and transport mode with nat-traversal we
* need to change outbound IPsec SA to point to external ip of
* the peer. Here we substitute real client ip with NATD ip.
*
* XXX: unset_protoport is technically wrong - the
* protocol is sa->transport_proto(?) and . Code
* further down will fix up the .sport / .dport in the
* xfrm structure.
*
* XXX: is .src.address / .dst.address an address or
* endpoint in disguise?
*/
if (sa->inbound) {
/* inbound; fix this end */
ip_port port = selector_port(sa->src.client);
src = selector_from_address_protocol_port(sa->src.address,
protocol, port);
} else {
/* outbound; fix other end */
ip_port port = selector_port(sa->dst.client);
dst = selector_from_address_protocol_port(sa->dst.address,
protocol, port);
}
/* .[sd]addr, .prefixlen_[sd], .[sd]port */
SELECTOR_TO_XFRM(&src, req.p.sel, s);
SELECTOR_TO_XFRM(&dst, req.p.sel, d);
/*
* Munge .[sd]port?
*
* As per RFC 4301/5996, icmp type is put in the most
* significant 8 bits and icmp code is in the least
* significant 8 bits of port field. Although Libreswan does
* not have any configuration options for
* icmp type/code values, it is possible to specify icmp type
* and code using protoport option. For example,
* icmp echo request (type 8/code 0) needs to be encoded as
* 0x0800 in the port field and can be specified
* as left/rightprotoport=icmp/2048. Now with XFRM,
* icmp type and code need to be passed as source and
* destination ports, respectively. Therefore, this code
* extracts upper 8 bits and lower 8 bits and puts
* into source and destination ports before passing to XFRM.
*/
if (IPPROTO_ICMP == sa->transport_proto ||
IPPROTO_ICMPV6 == sa->transport_proto) {
uint16_t icmp_type;
uint16_t icmp_code;
icmp_type = ntohs(req.p.sel.sport) >> 8;
icmp_code = ntohs(req.p.sel.sport) & 0xFF;
req.p.sel.sport = htons(icmp_type);
req.p.sel.dport = htons(icmp_code);
}
req.p.sel.sport_mask = req.p.sel.sport == 0 ? 0 : ~0;
req.p.sel.dport_mask = req.p.sel.dport == 0 ? 0 : ~0;
req.p.sel.proto = sa->transport_proto;
req.p.sel.family = subnet_type(&src)->af;
}
req.p.reqid = sa->reqid;
dbg("XFRM: adding IPsec SA with reqid %d", sa->reqid);
/* TODO expose limits to kernel_sa via config */
req.p.lft.soft_byte_limit = XFRM_INF;
req.p.lft.soft_packet_limit = XFRM_INF;
req.p.lft.hard_byte_limit = XFRM_INF;
req.p.lft.hard_packet_limit = XFRM_INF;
req.n.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.p)));
attr = (struct rtattr *)((char *)&req + req.n.nlmsg_len);
/*
* The Linux IPv4 AH stack aligns the AH header on a 64 bit boundary
* (like in IPv6). This is not RFC compliant (see RFC4302, Section
* 3.3.3.2.1), it should be aligned on 32 bits.
*
* For most of the authentication algorithms, the ICV size is 96 bits.
* The AH header alignment on 32 or 64 bits gives the same results.
*
* However for SHA-256-128 for instance, the wrong 64 bit alignment results
* in adding useless padding in IPv4 AH, which is forbidden by the RFC.
*
* To avoid breaking backward compatibility, we use a new flag
* (XFRM_STATE_ALIGN4) do change original behavior.
*/
if (sa->esatype == ET_AH && addrtypeof(sa->src.address) == AF_INET) {
dbg("netlink: aligning IPv4 AH to 32bits as per RFC-4302, Section 3.3.3.2.1");
req.p.flags |= XFRM_STATE_ALIGN4;
}
if (sa->esatype != ET_IPCOMP) {
if (sa->esn) {
dbg("netlink: enabling ESN");
req.p.flags |= XFRM_STATE_ESN;
}
if (sa->decap_dscp) {
dbg("netlink: enabling Decap DSCP");
req.p.flags |= XFRM_STATE_DECAP_DSCP;
}
if (sa->nopmtudisc) {
dbg("netlink: disabling Path MTU Discovery");
req.p.flags |= XFRM_STATE_NOPMTUDISC;
}
if (sa->replay_window <= 32 && !sa->esn) {
/* this only works up to 32, for > 32 and for ESN, we need struct xfrm_replay_state_esn */
req.p.replay_window = sa->replay_window;
dbg("netlink: setting IPsec SA replay-window to %d using old-style req",
req.p.replay_window);
} else {
uint32_t bmp_size = BYTES_FOR_BITS(sa->replay_window +
pad_up(sa->replay_window, sizeof(uint32_t) * BITS_PER_BYTE) );
/* this is where we could fill in sequence numbers for this SA */
struct xfrm_replay_state_esn xre = {
/* replay_window must be multiple of 8 */
.replay_window = sa->replay_window,
.bmp_len = bmp_size / sizeof(uint32_t),
};
dbg("netlink: setting IPsec SA replay-window to %" PRIu32 " using xfrm_replay_state_esn",
xre.replay_window);
attr->rta_type = XFRMA_REPLAY_ESN_VAL;
attr->rta_len = RTA_LENGTH(sizeof(xre) + bmp_size);
memcpy(RTA_DATA(attr), &xre, sizeof(xre));
req.n.nlmsg_len += attr->rta_len;
attr = (struct rtattr *)((char *)&req + req.n.nlmsg_len);
}
}
if (sa->authkeylen != 0) {
const char *name = sa->integ->integ_netlink_xfrm_name;
if (name == NULL) {
llog(RC_LOG_SERIOUS, logger,
"XFRM: unknown authentication algorithm: %s",
sa->integ->common.fqn);
return FALSE;
}
/*
* According to RFC-4868 the hash should be nnn/2, so
* 128 bits for SHA256 and 256 for SHA512. The XFRM
* kernel uses a default of 96, which was the value in
* an earlier draft. The kernel then introduced a new struct
* xfrm_algo_auth to replace struct xfrm_algo to deal with
* this.
*/
struct xfrm_algo_auth algo = {
.alg_key_len = sa->integ->integ_keymat_size * BITS_PER_BYTE,
.alg_trunc_len = sa->integ->integ_output_size * BITS_PER_BYTE,
};
attr->rta_type = XFRMA_ALG_AUTH_TRUNC;
attr->rta_len = RTA_LENGTH(sizeof(algo) + sa->authkeylen);
fill_and_terminate(algo.alg_name, name, sizeof(algo.alg_name));
memcpy(RTA_DATA(attr), &algo, sizeof(algo));
memcpy((char *)RTA_DATA(attr) + sizeof(algo),
sa->authkey, sa->authkeylen);
req.n.nlmsg_len += attr->rta_len;
attr = (struct rtattr *)((char *)attr + attr->rta_len);
}
/*
* ??? why does IPCOMP trump aead and ESP?
* Shouldn't all be bundled?
*/
if (sa->esatype == ET_IPCOMP) {
struct xfrm_algo algo;
const char *name = sparse_name(calg_list, sa->compalg);
if (name == NULL) {
llog(RC_LOG_SERIOUS, logger,
"unknown compression algorithm: %u",
sa->compalg);
return FALSE;
}
fill_and_terminate(algo.alg_name, name, sizeof(algo.alg_name));
algo.alg_key_len = 0;
attr->rta_type = XFRMA_ALG_COMP;
attr->rta_len = RTA_LENGTH(sizeof(algo));
memcpy(RTA_DATA(attr), &algo, sizeof(algo));
req.n.nlmsg_len += attr->rta_len;
attr = (struct rtattr *)((char *)attr + attr->rta_len);
} else if (sa->esatype == ET_ESP) {
const char *name = sa->encrypt->encrypt_netlink_xfrm_name;
if (name == NULL) {
llog(RC_LOG_SERIOUS, logger,
"unknown encryption algorithm: %s",
sa->encrypt->common.fqn);
return FALSE;
}
if (encrypt_desc_is_aead(sa->encrypt)) {
struct xfrm_algo_aead algo;
fill_and_terminate(algo.alg_name, name,
sizeof(algo.alg_name));
algo.alg_key_len = sa->enckeylen * BITS_PER_BYTE;
algo.alg_icv_len = sa->encrypt->aead_tag_size * BITS_PER_BYTE;
attr->rta_type = XFRMA_ALG_AEAD;
attr->rta_len = RTA_LENGTH(sizeof(algo) + sa->enckeylen);
memcpy(RTA_DATA(attr), &algo, sizeof(algo));
memcpy((char *)RTA_DATA(attr) + sizeof(algo),
sa->enckey, sa->enckeylen);
req.n.nlmsg_len += attr->rta_len;
attr = (struct rtattr *)((char *)attr + attr->rta_len);
} else {
struct xfrm_algo algo;
fill_and_terminate(algo.alg_name, name,
sizeof(algo.alg_name));
algo.alg_key_len = sa->enckeylen * BITS_PER_BYTE;
attr->rta_type = XFRMA_ALG_CRYPT;
attr->rta_len = RTA_LENGTH(sizeof(algo) + sa->enckeylen);
memcpy(RTA_DATA(attr), &algo, sizeof(algo));
memcpy((char *)RTA_DATA(attr) + sizeof(algo),
sa->enckey,
sa->enckeylen);
req.n.nlmsg_len += attr->rta_len;
attr = (struct rtattr *)((char *)attr + attr->rta_len);
/* Traffic Flow Confidentiality is only for ESP tunnel mode */
if (sa->tfcpad != 0 &&
sa->mode == ENCAPSULATION_MODE_TUNNEL) {
dbg("netlink: setting TFC to %" PRIu32 " (up to PMTU)",
sa->tfcpad);
attr->rta_type = XFRMA_TFCPAD;
attr->rta_len = RTA_LENGTH(sizeof(sa->tfcpad));
memcpy(RTA_DATA(attr), &sa->tfcpad, sizeof(sa->tfcpad));
req.n.nlmsg_len += attr->rta_len;
attr = (struct rtattr *)((char *)attr + attr->rta_len);
}
}
}
if (sa->encap_type != NULL) {
dbg("adding xfrm-encap-tmpl when adding sa encap_type="PRI_IP_ENCAP" sport=%d dport=%d",
pri_ip_encap(sa->encap_type),
sa->src.encap_port, sa->dst.encap_port);
struct xfrm_encap_tmpl natt;
natt.encap_type = sa->encap_type->encap_type;
natt.encap_sport = ntohs(sa->src.encap_port);
natt.encap_dport = ntohs(sa->dst.encap_port);
zero(&natt.encap_oa);
attr->rta_type = XFRMA_ENCAP;
attr->rta_len = RTA_LENGTH(sizeof(natt));
memcpy(RTA_DATA(attr), &natt, sizeof(natt));
req.n.nlmsg_len += attr->rta_len;
attr = (struct rtattr *)((char *)attr + attr->rta_len);
}
#ifdef USE_XFRM_INTERFACE
if (sa->xfrm_if_id != 0) {
dbg("%s netlink: XFRMA_IF_ID %" PRIu32 " req.n.nlmsg_type=%" PRIu32,
__func__, sa->xfrm_if_id, req.n.nlmsg_type);
nl_addattr32(&req.n, sizeof(req.data), XFRMA_IF_ID, sa->xfrm_if_id);
if (sa->mark_set.val != 0 || sa->mark_set.mask != 0) {
/* manually configured mark-out=mark/mask */
nl_addattr32(&req.n, sizeof(req.data), XFRMA_SET_MARK, sa->mark_set.val);
nl_addattr32(&req.n, sizeof(req.data), XFRMA_SET_MARK_MASK, sa->mark_set.mask);
} else {
/* XFRMA_SET_MARK = XFRMA_IF_ID */
nl_addattr32(&req.n, sizeof(req.data), XFRMA_SET_MARK, sa->xfrm_if_id);
}
attr = (struct rtattr *)((char *)&req + req.n.nlmsg_len);
}
#endif
if (sa->nic_offload_dev) {
struct xfrm_user_offload xuo = {
.flags = (sa->inbound ? XFRM_OFFLOAD_INBOUND : 0) |
(addrtypeof(sa->src.address) == AF_INET6 ? XFRM_OFFLOAD_IPV6 : 0),
.ifindex = if_nametoindex(sa->nic_offload_dev),
};
attr->rta_type = XFRMA_OFFLOAD_DEV;
attr->rta_len = RTA_LENGTH(sizeof(xuo));
memcpy(RTA_DATA(attr), &xuo, sizeof(xuo));
req.n.nlmsg_len += attr->rta_len;
attr = (struct rtattr *)((char *)attr + attr->rta_len);
dbg("netlink: esp-hw-offload set via interface %s for IPsec SA", sa->nic_offload_dev);
} else {
dbg("netlink: esp-hw-offload not set for IPsec SA");
}
if (sa->sec_label.len != 0) {
struct xfrm_user_sec_ctx xuctx;
xuctx.len = sizeof(struct xfrm_user_sec_ctx) + sa->sec_label.len;
xuctx.exttype = XFRMA_SEC_CTX;
xuctx.ctx_alg = XFRM_SC_ALG_SELINUX; /* 1 */
xuctx.ctx_doi = XFRM_SC_DOI_LSM; /* 1 */
xuctx.ctx_len = sa->sec_label.len;
attr->rta_type = XFRMA_SEC_CTX;
attr->rta_len = RTA_LENGTH(xuctx.len);
memcpy(RTA_DATA(attr), &xuctx, sizeof(xuctx));
memcpy((char *)RTA_DATA(attr) + sizeof(xuctx),
sa->sec_label.ptr, sa->sec_label.len);
req.n.nlmsg_len += attr->rta_len;
/* attr not subsequently used */
attr = (struct rtattr *)((char *)attr + attr->rta_len);
}
ret = send_netlink_msg(&req.n, NLMSG_NOOP, NULL,
"Add SA", sa->text_said, logger);
if (!ret && netlink_errno == ESRCH &&
req.n.nlmsg_type == XFRM_MSG_UPDSA) {
llog(RC_LOG_SERIOUS, logger,
"Warning: kernel expired our reserved IPsec SA SPI - negotiation took too long? Try increasing /proc/sys/net/core/xfrm_acq_expires");
}
return ret;
}
/*
* netlink_del_sa - Delete an SA from the Kernel
*
* @param sa Kernel SA to be deleted
* @return bool True if successful
*/
static bool netlink_del_sa(const struct kernel_sa *sa,
struct logger *logger)
{
struct {
struct nlmsghdr n;
struct xfrm_usersa_id id;
char data[MAX_NETLINK_DATA_SIZE];
} req;
zero(&req);
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
req.n.nlmsg_type = XFRM_MSG_DELSA;
req.id.daddr = xfrm_from_address(sa->dst.address);
req.id.spi = sa->spi;
req.id.family = addrtypeof(sa->src.address);
req.id.proto = sa->proto->ipproto;
req.n.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.id)));
dbg("XFRM: deleting IPsec SA with reqid %d", sa->reqid);
return send_netlink_msg(&req.n, NLMSG_NOOP, NULL,
"Del SA", sa->text_said, logger);
}
/*
* Create ip_address out of xfrm_address_t.
*
* @param family
* @param src xfrm formatted IP address
* @param dst ip_address formatted destination
* @return err_t NULL if okay, otherwise an error
*/
static ip_address address_from_xfrm(const struct ip_info *afi,
const xfrm_address_t *xaddr)
{
/* .len == ipv6 size */
shunk_t x = THING_AS_SHUNK(*xaddr);
ip_address addr = afi->any_address; /* "zero" it & set type */
chunk_t a = address_as_chunk(&addr);
/* a = x */
passert(a.len <= x.len);
memcpy(a.ptr, x.ptr, a.len);
return addr;
}
/*
* Create the client's ip_endpoint from xfrm_address_t:NPORT.
*/
static ip_endpoint endpoint_from_xfrm(const struct ip_info *afi,
const ip_protocol *protocol,
const xfrm_address_t *src,
uint16_t nport)
{
ip_address address = address_from_xfrm(afi, src);
ip_port port = ip_nport(nport);
return endpoint3(protocol, &address, port);
}
static void netlink_acquire(struct nlmsghdr *n, struct logger *logger)
{
struct xfrm_user_acquire *acquire;
struct xfrm_user_sec_ctx_ike *uctx = NULL;
chunk_t sec_label = {
.ptr = NULL,
.len = 0
};
dbg("xfrm netlink msg len %zu", (size_t) n->nlmsg_len);
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*acquire))) {
llog(RC_LOG, logger,
"netlink_acquire got message with length %zu < %zu bytes; ignore message",
(size_t) n->nlmsg_len,
sizeof(*acquire));
return;
}
/*
* WARNING: netlink only guarantees 32-bit alignment.
* See NLMSG_ALIGNTO in the kernel's include/uapi/linux/netlink.h.
* BUT some fields in struct xfrm_user_acquire are 64-bit and so access
* may be improperly aligned. This will fail on a few strict
* architectures (it does break C rules).
*
* WARNING: this code's understanding to the XFRM netlink
* messages is from programs/pluto/linux26/xfrm.h.
* There is no guarantee that this matches the kernel's
* understanding.
*
* Many things are defined to be int or unsigned int.
* This isn't safe when the kernel and userland may
* be compiled with different models.
*/
acquire = NLMSG_DATA(n); /* insufficiently aligned */
const struct ip_info *afi = aftoinfo(acquire->policy.sel.family);
if (afi == NULL) {
llog(RC_LOG, logger,
"XFRM_MSG_ACQUIRE message from kernel malformed: family %u unknown",
acquire->policy.sel.family);
return;
}
const ip_protocol *protocol = protocol_by_ipproto(acquire->sel.proto);
if (protocol == NULL) {
llog(RC_LOG, logger,
"XFRM_MSG_ACQUIRE message from kernel malformed: protocol %u unknown",
acquire->policy.sel.proto);
return;
}
ip_endpoint local = endpoint_from_xfrm(afi, protocol,
&acquire->sel.saddr,
acquire->sel.sport);
ip_endpoint remote = endpoint_from_xfrm(afi, protocol,
&acquire->sel.daddr,
acquire->sel.dport);
/*
* Run through rtattributes looking for XFRMA_SEC_CTX
* Instead, it should loop through all (known rtattributes
* and use/log them.
*/
struct rtattr *attr = (struct rtattr *)
((char*) NLMSG_DATA(n) +
NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)));
size_t remaining = n->nlmsg_len -
NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
while (remaining > 0) {
dbg("xfrm acquire rtattribute type %u ...", attr->rta_type);
switch (attr->rta_type) {
case XFRMA_TMPL:
{
struct xfrm_user_tmpl* tmpl = (struct xfrm_user_tmpl *) RTA_DATA(attr);
dbg("... xfrm template attribute with reqid:%d, spi:%d, proto:%d",
tmpl->reqid, tmpl->id.spi, tmpl->id.proto);
break;
}
case XFRMA_POLICY_TYPE:
/* discard */
dbg("... xfrm policy type ignored");
break;
case XFRMA_SEC_CTX:
{
struct xfrm_user_sec_ctx *xuctx = (struct xfrm_user_sec_ctx *) RTA_DATA(attr);
/* length of text of label */
size_t len = xuctx->ctx_len;
dbg("... xfrm xuctx: exttype=%d, len=%d, ctx_doi=%d, ctx_alg=%d, ctx_len=%zu",
xuctx->exttype, xuctx->len,
xuctx->ctx_doi, xuctx->ctx_alg,
len);
if (xuctx->ctx_doi != XFRM_SC_DOI_LSM) {
llog(RC_LOG, logger,
"Acquire message for unknown sec_label DOI %d; ignoring Acquire message",
xuctx->ctx_doi);
return;
}
if (xuctx->ctx_alg != XFRM_SC_ALG_SELINUX) {
llog(RC_LOG, logger,
"Acquire message for unknown sec_label LSM %d; ignoring Acquire message",
xuctx->ctx_alg);
return;
}
if (uctx != NULL) {
llog(RC_LOG, logger,
"Second sec_label in a single Acquire message; ignoring Acquire message");
return;
}
if (len > MAX_SECCTX_LEN) {
llog(RC_LOG, logger,
"length %zu of sec_label is longer than MAX_SECCTX_LEN; ignoring Acquire message",
len);
return;
}
/*
* note: xuctx + 1 is tricky:
* first byte after header
*/
sec_label.ptr = (uint8_t *)(xuctx + 1);
sec_label.len = xuctx->ctx_len;
dbg("xfrm: xuctx security context value: %.*s",
xuctx->ctx_len,
(const char *) (xuctx + 1));
if (strlen((char *)sec_label.ptr) + 1 != xuctx->ctx_len) {
llog(RC_LOG, logger,
"received sec_label contains embedded NULl ignoring Acquire message");
return;
}
break;
}
default:
dbg("... ignoring unknown xfrm acquire payload type %u",
attr->rta_type);
break;
}
/* updates remaining too */
attr = RTA_NEXT(attr, remaining);
}
record_and_initiate_opportunistic(&local, &remote, sec_label,
"%acquire-netlink");
}
static void netlink_shunt_expire(struct xfrm_userpolicy_info *pol,
struct logger *logger)
{
const xfrm_address_t *srcx = &pol->sel.saddr;
const xfrm_address_t *dstx = &pol->sel.daddr;
unsigned transport_proto = pol->sel.proto;
const struct ip_info *afi = aftoinfo(pol->sel.family);
if (afi == NULL) {
llog(RC_LOG, logger,
"XFRM_MSG_POLEXPIRE message from kernel malformed: address family %u unknown",
pol->sel.family);
return;
}
ip_address src = address_from_xfrm(afi, srcx);
ip_address dst = address_from_xfrm(afi, dstx);
if (delete_bare_shunt(&src, &dst,
transport_proto, SPI_HOLD /* why spi to use? */,
/*skip_xfrm_raw_eroute_delete?*/false,
"delete expired bare shunt", logger)) {
dbg("netlink_shunt_expire() called delete_bare_shunt() with success");
} else {
llog(RC_LOG, logger,
"netlink_shunt_expire() called delete_bare_shunt() which failed!");
}
}
static void process_addr_chage(struct nlmsghdr *n, struct logger *logger)
{
struct ifaddrmsg *nl_msg = NLMSG_DATA(n);
struct rtattr *rta = IFLA_RTA(nl_msg);
size_t msg_size = IFA_PAYLOAD (n);
ip_address ip;
dbg("xfrm netlink address change %s msg len %zu",
sparse_val_show(rtm_type_names, n->nlmsg_type),
(size_t) n->nlmsg_len);
while (RTA_OK(rta, msg_size)) {
err_t ugh;
switch (rta->rta_type) {
case IFA_LOCAL:
ugh = data_to_address(RTA_DATA(rta), RTA_PAYLOAD(rta)/*size*/,
aftoinfo(nl_msg->ifa_family), &ip);
if (ugh != NULL) {
llog(RC_LOG, logger,
"ERROR IFA_LOCAL invalid %s", ugh);
} else {
if (n->nlmsg_type == RTM_DELADDR)
record_deladdr(&ip, "IFA_LOCAL");
else if (n->nlmsg_type == RTM_NEWADDR)
record_newaddr(&ip, "IFA_LOCAL");
}
break;
case IFA_ADDRESS:
ugh = data_to_address(RTA_DATA(rta), RTA_PAYLOAD(rta)/*size*/,
aftoinfo(nl_msg->ifa_family), &ip);
if (ugh != NULL) {
llog(RC_LOG, logger,
"ERROR IFA_ADDRESS invalid %s", ugh);
} else {
address_buf ip_str;
dbg("XFRM IFA_ADDRESS %s IFA_ADDRESS is this PPP?",
str_address(&ip, &ip_str));
}
break;
default:
dbg("IKEv2 received address %s type %u",
sparse_val_show(rtm_type_names, n->nlmsg_type),
rta->rta_type);
break;
}
rta = RTA_NEXT(rta, msg_size);
}
}
static void netlink_policy_expire(struct nlmsghdr *n, struct logger *logger)
{
struct xfrm_user_polexpire *upe;
ip_address src, dst;
struct {
struct nlmsghdr n;
struct xfrm_userpolicy_id id;
} req;
struct nlm_resp rsp;
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*upe))) {
llog(RC_LOG, logger,
"netlink_policy_expire got message with length %zu < %zu bytes; ignore message",
(size_t) n->nlmsg_len,
sizeof(*upe));
return;
}
upe = NLMSG_DATA(n);
xfrm2ip(&upe->pol.sel.saddr, &src, upe->pol.sel.family);
xfrm2ip(&upe->pol.sel.daddr, &dst, upe->pol.sel.family);
address_buf a;
address_buf b;
dbg("%s src %s/%u dst %s/%u dir %d index %d",
__func__,
str_address(&src, &a), upe->pol.sel.prefixlen_s,
str_address(&dst, &b), upe->pol.sel.prefixlen_d,
upe->pol.dir, upe->pol.index);
req.id.dir = upe->pol.dir;
req.id.index = upe->pol.index;
req.n.nlmsg_flags = NLM_F_REQUEST;
req.n.nlmsg_type = XFRM_MSG_GETPOLICY;
req.n.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.id)));
if (!send_netlink_msg(&req.n, XFRM_MSG_NEWPOLICY, &rsp,
"Get policy", "?", logger)) {
dbg("netlink_policy_expire: policy died on us: dir=%d, index=%d",
req.id.dir, req.id.index);
} else if (rsp.n.nlmsg_len < NLMSG_LENGTH(sizeof(rsp.u.pol))) {
llog(RC_LOG, logger,
"netlink_policy_expire: XFRM_MSG_GETPOLICY returned message with length %zu < %zu bytes; ignore message",
(size_t) rsp.n.nlmsg_len,
sizeof(rsp.u.pol));
} else if (req.id.index != rsp.u.pol.index) {
dbg("netlink_policy_expire: policy was replaced: dir=%d, oldindex=%d, newindex=%d",
req.id.dir, req.id.index, rsp.u.pol.index);
} else if (upe->pol.curlft.add_time != rsp.u.pol.curlft.add_time) {
dbg("netlink_policy_expire: policy was replaced and you have won the lottery: dir=%d, index=%d",
req.id.dir, req.id.index);
} else {
switch (upe->pol.dir) {
case XFRM_POLICY_OUT:
netlink_shunt_expire(&rsp.u.pol, logger);
break;
}
}
}
/* returns FALSE iff EAGAIN */
static bool netlink_get(int fd, struct logger *logger)
{
struct nlm_resp rsp;
struct sockaddr_nl addr;
socklen_t alen = sizeof(addr);
ssize_t r = recvfrom(fd, &rsp, sizeof(rsp), 0,
(struct sockaddr *)&addr, &alen);
if (r < 0) {
if (errno == EAGAIN)
return FALSE;
if (errno != EINTR) {
log_errno(logger, errno,
"recvfrom() failed in netlink_get: errno(%d): %s",
errno, strerror(errno));
}
return TRUE;
} else if ((size_t)r < sizeof(rsp.n)) {
llog(RC_LOG, logger,
"netlink_get read truncated message: %zd bytes; ignore message",
r);
return true;
} else if (addr.nl_pid != 0) {
/* not for us: ignore */
dbg("netlink_get: ignoring %s message from process %u",
sparse_val_show(xfrm_type_names, rsp.n.nlmsg_type),
addr.nl_pid);
return TRUE;
} else if ((size_t)r != rsp.n.nlmsg_len) {
llog(RC_LOG, logger,
"netlink_get read message with length %zd that doesn't equal nlmsg_len %zu bytes; ignore message",
r, (size_t) rsp.n.nlmsg_len);
return true;
}
dbg("netlink_get: %s message",
sparse_val_show(xfrm_type_names, rsp.n.nlmsg_type));
switch (rsp.n.nlmsg_type) {
case XFRM_MSG_ACQUIRE:
netlink_acquire(&rsp.n, logger);
break;
case XFRM_MSG_POLEXPIRE:
netlink_policy_expire(&rsp.n, logger);
break;
case RTM_NEWADDR:
process_addr_chage(&rsp.n, logger);
break;
case RTM_DELADDR:
process_addr_chage(&rsp.n, logger);
break;
default:
/* ignored */
break;
}
return TRUE;
}
static void netlink_process_msg(int fd, struct logger *logger)
{
do {} while (netlink_get(fd, logger));
}
static ipsec_spi_t netlink_get_spi(const ip_address *src,
const ip_address *dst,
const struct ip_protocol *proto,
bool tunnel_mode,
reqid_t reqid,
ipsec_spi_t min,
ipsec_spi_t max,
const char *text_said,
struct logger *logger)
{
struct {
struct nlmsghdr n;
struct xfrm_userspi_info spi;
} req;
struct nlm_resp rsp;
zero(&req);
req.n.nlmsg_flags = NLM_F_REQUEST;
req.n.nlmsg_type = XFRM_MSG_ALLOCSPI;
req.spi.info.saddr = xfrm_from_address(src);
req.spi.info.id.daddr = xfrm_from_address(dst);
req.spi.info.mode = tunnel_mode;
req.spi.info.reqid = reqid;
req.spi.info.id.proto = proto->ipproto;
req.spi.info.family = addrtypeof(src);
req.n.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.spi)));
req.spi.min = min;
req.spi.max = max;
if (!send_netlink_msg(&req.n, XFRM_MSG_NEWSA, &rsp,
"Get SPI", text_said, logger)) {
return 0;
}
if (rsp.n.nlmsg_len < NLMSG_LENGTH(sizeof(rsp.u.sa))) {
llog(RC_LOG, logger,
"netlink_get_spi: XFRM_MSG_ALLOCSPI returned message with length %zu < %zu bytes; ignore message",
(size_t) rsp.n.nlmsg_len,
sizeof(rsp.u.sa));
return 0;
}
dbg("netlink_get_spi: allocated 0x%x for %s",
ntohl(rsp.u.sa.id.spi), text_said);
return rsp.u.sa.id.spi;
}
/*
* install or remove eroute for SA Group
*
* (identical to KLIPS version, but refactoring isn't waranteed yet
*/
static bool netlink_sag_eroute(const struct state *st, const struct spd_route *sr,
unsigned op, const char *opname)
{
struct connection *c = st->st_connection;
enum eroute_type inner_esatype;
ipsec_spi_t inner_spi;
struct pfkey_proto_info proto_info[4];
int i;
bool tunnel;
/*
* figure out the SPI and protocol (in two forms)
* for the innermost transformation.
*/
i = elemsof(proto_info) - 1;
proto_info[i].proto = 0;
tunnel = FALSE;
const struct ip_protocol *inner_proto = NULL;
inner_esatype = ET_UNSPEC;
inner_spi = 0;
if (st->st_ah.present) {
inner_spi = st->st_ah.attrs.spi;
inner_proto = &ip_protocol_ah;
inner_esatype = ET_AH;
i--;
proto_info[i].proto = IPPROTO_AH;
proto_info[i].mode = st->st_ah.attrs.mode;
tunnel |= proto_info[i].mode ==
ENCAPSULATION_MODE_TUNNEL;
proto_info[i].reqid = reqid_ah(sr->reqid);
}
if (st->st_esp.present) {
inner_spi = st->st_esp.attrs.spi;
inner_proto = &ip_protocol_esp;
inner_esatype = ET_ESP;
i--;
proto_info[i].proto = IPPROTO_ESP;
proto_info[i].mode = st->st_esp.attrs.mode;
tunnel |= proto_info[i].mode ==
ENCAPSULATION_MODE_TUNNEL;
proto_info[i].reqid = reqid_esp(sr->reqid);
}
if (st->st_ipcomp.present) {
inner_spi = st->st_ipcomp.attrs.spi;
inner_proto = &ip_protocol_comp;
inner_esatype = ET_IPCOMP;
i--;
proto_info[i].proto = IPPROTO_COMP;
proto_info[i].mode =
st->st_ipcomp.attrs.mode;
tunnel |= proto_info[i].mode ==
ENCAPSULATION_MODE_TUNNEL;
proto_info[i].reqid = reqid_ipcomp(sr->reqid);
}
/* check for no transform at all */
passert(st->st_ipcomp.present || st->st_esp.present ||
st->st_ah.present);
if (tunnel) {
int j;
inner_spi = st->st_tunnel_out_spi;
inner_proto = &ip_protocol_ipip;
inner_esatype = ET_IPIP;
proto_info[i].mode = ENCAPSULATION_MODE_TUNNEL;
for (j = i + 1; proto_info[j].proto; j++)
proto_info[j].mode =
ENCAPSULATION_MODE_TRANSPORT;
}
uint32_t xfrm_if_id = c->xfrmi != NULL ? c->xfrmi->if_id : 0;
return eroute_connection(sr, inner_spi, inner_spi, inner_proto,
inner_esatype, proto_info + i,
calculate_sa_prio(c, FALSE), &c->sa_marks,
xfrm_if_id, op, opname, st->st_logger);
}
/* Check if there was traffic on given SA during the last idle_max
* seconds. If TRUE, the SA was idle and DPD exchange should be performed.
* If FALSE, DPD is not necessary. We also return TRUE for errors, as they
* could mean that the SA is broken and needs to be replace anyway.
*
* note: this mutates *st by calling get_sa_info
*/
static bool netlink_eroute_idle(struct state *st, deltatime_t idle_max)
{
deltatime_t idle_time;
passert(st != NULL);
return !get_sa_info(st, TRUE, &idle_time) ||
deltatime_cmp(idle_time, >=, idle_max);
}
static bool netlink_shunt_eroute(const struct connection *c,
const struct spd_route *sr,
enum routing_t rt_kind,
enum pluto_sadb_operations op,
const char *opname,
struct logger *logger)
{
ipsec_spi_t spi;
/*
* We are constructing a special SAID for the eroute.
* The destination doesn't seem to matter, but the family does.
* The protocol is &ip_protocol_internal -- mark this as shunt.
* The satype has no meaning, but is required for PF_KEY header!
* The SPI signifies the kind of shunt.
*/
spi = shunt_policy_spi(c, rt_kind == RT_ROUTED_PROSPECTIVE);
if (DBGP(DBG_BASE)) {
selector_buf this_buf, that_buf;
DBG_log("netlink_shunt_eroute for proto %d, and source %s dest %s",
sr->this.protocol,
str_selector(&sr->this.client, &this_buf),
str_selector(&sr->that.client, &that_buf));
}
if (spi == 0) {
/*
* we're supposed to end up with no eroute: rejig op and
* opname
*/
switch (op) {
case ERO_REPLACE:
/* replace with nothing == delete */
op = ERO_DELETE;
opname = "delete";
break;
case ERO_ADD:
/* add nothing == do nothing */
return TRUE;
case ERO_DELETE:
/* delete remains delete */
break;
case ERO_ADD_INBOUND:
break;
case ERO_DEL_INBOUND:
break;
default:
bad_case(op);
}
}
if (sr->routing == RT_ROUTED_ECLIPSED && c->kind == CK_TEMPLATE) {
/*
* We think that we have an eroute, but we don't.
* Adjust the request and account for eclipses.
*/
passert(eclipsable(sr));
switch (op) {
case ERO_REPLACE:
/* really an add */
op = ERO_ADD;
opname = "replace eclipsed";
eclipse_count--;
break;
case ERO_DELETE:
/*
* delete unnecessary:
* we don't actually have an eroute
*/
eclipse_count--;
return TRUE;
case ERO_ADD:
default:
bad_case(op);
}
} else if (eclipse_count > 0 && op == ERO_DELETE && eclipsable(sr)) {
/* maybe we are uneclipsing something */
struct spd_route *esr;
struct connection *ue = eclipsed(c, &esr);
if (ue != NULL) {
esr->routing = RT_ROUTED_PROSPECTIVE;
return netlink_shunt_eroute(ue, esr,
RT_ROUTED_PROSPECTIVE,
ERO_REPLACE,
"restoring eclipsed",
logger);
}
}
char buf2[256];
snprintf(buf2, sizeof(buf2), "eroute_connection %s", opname);
/*
* XXX: the two calls below to netlink_raw_eroute() (not
* raw_eroute()) seems to be the only place where SA_PROTO and
* ESATYPE disagree - when ENCAPSULATION_MODE_TRANSPORT
* SA_PROTO==&ip_protocol_esp and ESATYPE==ET_INT!?! Looking in the
* function there's a weird test involving both SA_PROTO and
* ESATYPE.
*/
const struct ip_protocol *sa_proto = c->ipsec_mode == ENCAPSULATION_MODE_TRANSPORT ?
&ip_protocol_esp : &ip_protocol_internal;
if (!netlink_raw_eroute(&sr->this.host_addr, &sr->this.client,
&sr->that.host_addr, &sr->that.client,
htonl(spi), htonl(spi),
sa_proto,
sr->this.protocol,
ET_INT,
null_proto_info,
deltatime(0),
calculate_sa_prio(c, FALSE),
&c->sa_marks,
0 /* xfrm_if_id needed for shunt? */,
op, buf2,
&sr->this.sec_label,
logger))
return false;
switch (op) {
case ERO_ADD:
op = ERO_ADD_INBOUND;
break;
case ERO_DELETE:
op = ERO_DEL_INBOUND;
break;
default:
return TRUE;
}
snprintf(buf2, sizeof(buf2), "eroute_connection %s inbound", opname);
return netlink_raw_eroute(&sr->that.host_addr, &sr->that.client,
&sr->this.host_addr, &sr->this.client,
htonl(spi), htonl(spi),
sa_proto,
sr->this.protocol,
ET_INT,
null_proto_info,
deltatime(0),
calculate_sa_prio(c, FALSE),
&c->sa_marks,
0, /* xfrm_if_id needed for shunt? */
op, buf2,
&sr->this.sec_label,
logger);
}
static void netlink_process_raw_ifaces(struct raw_iface *rifaces, struct logger *logger)
{
struct raw_iface *ifp;
ip_address lip; /* --listen filter option */
if (pluto_listen) {
err_t e = ttoaddr_num(pluto_listen, 0, AF_UNSPEC, &lip);
if (e != NULL) {
DBG_log("invalid listen= option ignored: %s", e);
pluto_listen = NULL;
}
address_buf b;
dbg("Only looking to listen on %s", str_address(&lip, &b));
}
/*
* Find all virtual/real interface pairs.
* For each real interface...
*/
for (ifp = rifaces; ifp != NULL; ifp = ifp->next) {
struct raw_iface *v = NULL; /* matching ipsecX interface */
bool after = FALSE; /* has vfp passed ifp on the list? */
bool bad = FALSE;
struct raw_iface *vfp;
/* ignore if virtual (ipsec*) interface */
if (startswith(ifp->name, IPSECDEVPREFIX))
continue;
/* ignore if virtual (mast*) interface */
if (startswith(ifp->name, MASTDEVPREFIX))
continue;
for (vfp = rifaces; vfp != NULL; vfp = vfp->next) {
if (vfp == ifp) {
after = true;
} else if (sameaddr(&ifp->addr, &vfp->addr)) {
/*
* Different entries with matching IP
* addresses.
*
* Many interesting cases.
*/
if (startswith(vfp->name, IPSECDEVPREFIX)) {
if (v != NULL) {
ipstr_buf b;
llog(RC_LOG_SERIOUS, logger,
"ipsec interfaces %s and %s share same address %s",
v->name, vfp->name,
ipstr(&ifp->addr, &b));
bad = true;
} else {
/* current winner */
v = vfp;
}
} else {
/*
* ugh: a second real interface with
* the same IP address "after" allows
* us to avoid double reporting.
*/
/* XXX: isn't this always true? */
if (kernel_ops->type == USE_XFRM) {
if (after) {
bad = TRUE;
break;
}
continue;
}
if (after) {
ipstr_buf b;
llog(RC_LOG_SERIOUS, logger,
"IP interfaces %s and %s share address %s!",
ifp->name, vfp->name,
ipstr(&ifp->addr, &b));
}
bad = TRUE;
}
}
}
if (bad)
continue;
/* XXX: isn't this always true? */
if (kernel_ops->type == USE_XFRM) {
v = ifp;
}
/* what if we didn't find a virtual interface? */
if (v == NULL) {
address_buf b;
dbg("IP interface %s %s has no matching ipsec* interface -- ignored",
ifp->name, str_address(&ifp->addr, &b));
continue;
}
/*
* We've got all we need; see if this is a new thing:
* search old interfaces list.
*/
/*
* last check before we actually add the entry.
*
* ignore if --listen is specified and we do not match
*/
if (pluto_listen != NULL && !sameaddr(&lip, &ifp->addr)) {
ipstr_buf b;
llog(RC_LOG, logger,
"skipping interface %s with %s",
ifp->name, ipstr(&ifp->addr, &b));
continue;
}
add_or_keep_iface_dev(ifp, logger);
}
/* delete the raw interfaces list */
while (rifaces != NULL) {
struct raw_iface *t = rifaces;
rifaces = t->next;
pfree(t);
}
}
/*
* netlink_get_sa - Get SA information from the kernel
*
* @param sa Kernel SA to be queried
* @param bytes octets processed by IPsec SA
* @param add_time timestamp when IPsec SA added
* @return bool True if successful
*/
static bool netlink_get_sa(const struct kernel_sa *sa, uint64_t *bytes,
uint64_t *add_time, struct logger *logger)
{
struct {
struct nlmsghdr n;
struct xfrm_usersa_id id;
} req;
struct nlm_resp rsp;
zero(&req);
req.n.nlmsg_flags = NLM_F_REQUEST;
req.n.nlmsg_type = XFRM_MSG_GETSA;
req.id.daddr = xfrm_from_address(sa->dst.address);
req.id.spi = sa->spi;
req.id.family = addrtypeof(sa->src.address);
req.id.proto = sa->proto->ipproto;
req.n.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.id)));
if (!send_netlink_msg(&req.n, XFRM_MSG_NEWSA, &rsp,
"Get SA", sa->text_said, logger))
return FALSE;
*bytes = rsp.u.info.curlft.bytes;
*add_time = rsp.u.info.curlft.add_time;
return TRUE;
}
/* add bypass policies/holes icmp */
static bool netlink_bypass_policy(int family, int proto, int port,
struct logger *logger)
{
struct {
struct nlmsghdr n;
union {
struct xfrm_userpolicy_info p;
struct xfrm_userpolicy_id id;
} u;
char data[MAX_NETLINK_DATA_SIZE];
} req;
zero(&req);
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
req.n.nlmsg_type = XFRM_MSG_UPDPOLICY;
req.n.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.u.p)));
req.u.p.dir = XFRM_POLICY_IN;
req.u.p.priority = 1; /* give admin prio 0 as override */
req.u.p.action = XFRM_POLICY_ALLOW;
req.u.p.share = XFRM_SHARE_ANY;
req.u.p.lft.soft_byte_limit = XFRM_INF;
req.u.p.lft.soft_packet_limit = XFRM_INF;
req.u.p.lft.hard_byte_limit = XFRM_INF;
req.u.p.lft.hard_packet_limit = XFRM_INF;
req.u.p.sel.proto = proto;
req.u.p.sel.family = family;
const char* text = "add port bypass";
if (proto == IPPROTO_ICMPV6) {
uint16_t icmp_type;
uint16_t icmp_code;
icmp_type = port >> 8;
icmp_code = port & 0xFF;
req.u.p.sel.sport = htons(icmp_type);
req.u.p.sel.dport = htons(icmp_code);
req.u.p.sel.sport_mask = 0xffff;
if (!netlink_policy(&req.n, 1, text, logger))
return FALSE;
req.u.p.dir = XFRM_POLICY_FWD;
if (!netlink_policy(&req.n, 1, text, logger))
return FALSE;
req.u.p.dir = XFRM_POLICY_OUT;
if (!netlink_policy(&req.n, 1, text, logger))
return FALSE;
} else {
req.u.p.sel.dport = htons(port);
req.u.p.sel.dport_mask = 0xffff;
if (!netlink_policy(&req.n, 1, text, logger))
return FALSE;
req.u.p.dir = XFRM_POLICY_OUT;
req.u.p.sel.sport = htons(port);
req.u.p.sel.sport_mask = 0xffff;
req.u.p.sel.dport = 0;
req.u.p.sel.dport_mask = 0;
if (!netlink_policy(&req.n, 1, text, logger))
return FALSE;
}
return TRUE;
}
static void netlink_v6holes(struct logger *logger)
{
/* this could be per interface specific too */
const char proc_f[] = "/proc/sys/net/ipv6/conf/all/disable_ipv6";
struct stat sts;
if (stat(proc_f, &sts) != 0) {
/* not error */
llog(RC_LOG, logger,
"kernel: starting without ipv6 support! could not stat \"%s\"" PRI_ERRNO,
proc_f, pri_errno(errno));
/*
* pretend success, do not exit pluto, likely IPv6 is
* disabled in kernel at compile time. e.g. OpenWRT.
*/
return;
}
/*
* If the IPv6 enabled file is present, insist on being able
* to read it.
*/
FILE *f = fopen(proc_f, "r");
if (f == NULL) {
fatal_errno(PLUTO_EXIT_KERNEL_FAIL, logger, errno,
"kernel: could not open \"%s\"", proc_f);
}
char buf[64];
if (fgets(buf, sizeof(buf), f) == NULL) {
(void) fclose(f);
fatal_errno(PLUTO_EXIT_KERNEL_FAIL, logger, errno,
"kernel: could not read \"%s\"", proc_f);
}
(void) fclose(f);
int disable_ipv6 = atoi(buf);
if (disable_ipv6 == 1) {
llog(RC_LOG, logger, "kernel: %s=1 ignore ipv6 holes", proc_f);
return;
}
if (!netlink_bypass_policy(AF_INET6, IPPROTO_ICMPV6,
ICMP_NEIGHBOR_DISCOVERY,
logger)) {
fatal(PLUTO_EXIT_KERNEL_FAIL, logger,
"kernel: could not insert ICMP_NEIGHBOUR_DISCOVERY bypass policy");
}
if (!netlink_bypass_policy(AF_INET6, IPPROTO_ICMPV6,
ICMP_NEIGHBOR_SOLICITATION,
logger)) {
fatal(PLUTO_EXIT_KERNEL_FAIL, logger,
"kernel: could not insert ICMP_NEIGHBOUR_SOLICITATION bypass policy");
}
}
static bool qry_xfrm_mirgrate_support(struct nlmsghdr *hdr, struct logger *logger)
{
struct nlm_resp rsp;
size_t len;
ssize_t r;
struct sockaddr_nl addr;
int nl_fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_XFRM);
if (nl_fd < 0) {
log_errno(logger, errno,
"socket() in qry_xfrm_mirgrate_support()");
return false;
}
if (fcntl(nl_fd, F_SETFL, O_NONBLOCK) != 0) {
log_errno(logger, errno,
"fcntl(O_NONBLOCK in qry_xfrm_mirgrate_support()");
close(nl_fd);
return FALSE;
}
/* hdr->nlmsg_seq = ++seq; */
len = hdr->nlmsg_len;
do {
r = write(nl_fd, hdr, len);
} while (r < 0 && errno == EINTR);
if (r < 0) {
log_errno(logger, errno,
"netlink write() xfrm_migrate_support lookup");
close(nl_fd);
return FALSE;
} else if ((size_t)r != len) {
llog(RC_LOG_SERIOUS, logger,
"ERROR: netlink write() xfrm_migrate_support message truncated: %zd instead of %zu",
r, len);
close(nl_fd);
return FALSE;
}
for (;;) {
socklen_t alen = sizeof(addr);
r = recvfrom(nl_fd, &rsp, sizeof(rsp), 0,
(struct sockaddr *)&addr, &alen);
if (r < 0) {
if (errno == EINTR) {
continue;
} else if (errno == EAGAIN) {
/* old kernel F22 - dos not return proper error ??? */
dbg("ignore EAGAIN in %s assume MOBIKE migration is supported", __func__);
break;
}
}
break;
}
close(nl_fd);
if (rsp.n.nlmsg_type == NLMSG_ERROR && rsp.u.e.error == -ENOPROTOOPT) {
dbg("MOBIKE will fail got ENOPROTOOPT");
return FALSE;
}
return TRUE;
}
static err_t netlink_migrate_sa_check(struct logger *logger)
{
if (kernel_mobike_supprt == 0) {
/* check the kernel */
struct {
struct nlmsghdr n;
struct xfrm_userpolicy_id id;
char data[MAX_NETLINK_DATA_SIZE];
} req;
zero(&req);
req.n.nlmsg_flags = NLM_F_REQUEST;
req.n.nlmsg_type = XFRM_MSG_MIGRATE;
req.n.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.id)));
/* add attrs[XFRM_MSG_MIGRATE] */
struct rtattr *attr;
struct xfrm_user_migrate migrate;
zero(&migrate);
attr = (struct rtattr *)((char *)&req + req.n.nlmsg_len);
attr->rta_type = XFRMA_MIGRATE;
attr->rta_len = sizeof(migrate);
memcpy(RTA_DATA(attr), &migrate, attr->rta_len);
attr->rta_len = RTA_LENGTH(attr->rta_len);
req.n.nlmsg_len += attr->rta_len;
bool ret = qry_xfrm_mirgrate_support(&req.n, logger);
kernel_mobike_supprt = ret ? 1 : -1;
}
if (kernel_mobike_supprt > 0) {
return NULL;
} else {
return "CONFIG_XFRM_MIGRATE";
}
}
static bool netlink_poke_ipsec_policy_hole(const struct iface_dev *ifd, int fd, struct logger *logger)
{
const struct ip_info *type = address_type(&ifd->id_address);
struct xfrm_userpolicy_info policy = {
.action = XFRM_POLICY_ALLOW,
.sel = {
.family = type->af,
}
};
int opt, sol;
if (type == &ipv6_info) {
sol = IPPROTO_IPV6;
opt = IPV6_XFRM_POLICY;
} else {
sol = SOL_IP;
opt = IP_XFRM_POLICY;
}
policy.dir = XFRM_POLICY_IN;
if (setsockopt(fd, sol, opt, &policy, sizeof(policy)) < 0) {
log_errno(logger, errno,
"setsockopt IP_XFRM_POLICY XFRM_POLICY_IN in process_raw_ifaces()");
return false;
}
policy.dir = XFRM_POLICY_OUT;
if (setsockopt(fd, sol, opt, &policy, sizeof(policy)) < 0) {
log_errno(logger, errno,
"setsockopt IP_XFRM_POLICY XFRM_POLICY_OUT in process_raw_ifaces()");
return false;
}
return true;
}
const struct kernel_ops xfrm_kernel_ops = {
.kern_name = "xfrm",
.type = USE_XFRM,
.async_fdp = &nl_xfrm_fd,
.route_fdp = &nl_route_fd,
.replay_window = IPSEC_SA_DEFAULT_REPLAY_WINDOW,
.init = init_netlink,
#ifdef USE_XFRM_INTERFACE
.shutdown = free_xfrmi_ipsec1,
#else
.shutdown = NULL,
#endif
.process_msg = netlink_process_msg,
.raw_eroute = netlink_raw_eroute,
.add_sa = netlink_add_sa,
.del_sa = netlink_del_sa,
.get_sa = netlink_get_sa,
.process_queue = NULL,
.grp_sa = NULL,
.get_spi = netlink_get_spi,
.exceptsocket = NULL,
.process_raw_ifaces = netlink_process_raw_ifaces,
.shunt_eroute = netlink_shunt_eroute,
.sag_eroute = netlink_sag_eroute,
.eroute_idle = netlink_eroute_idle,
.migrate_sa_check = netlink_migrate_sa_check,
.migrate_sa = netlink_migrate_sa,
/*
* We should implement netlink_remove_orphaned_holds
* if netlink specific changes are needed.
*/
.remove_orphaned_holds = NULL, /* only used for klips /proc scanner */
.overlap_supported = FALSE,
.sha2_truncbug_support = TRUE,
.v6holes = netlink_v6holes,
.poke_ipsec_policy_hole = netlink_poke_ipsec_policy_hole,
.detect_offload = netlink_detect_offload,
};
|