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
|
/*
* Copyright (c) 2015, 2016 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Conntrack.h"
#include "IpFragment.h"
#include "Ip6Fragment.h"
#include "Jhash.h"
#include "PacketParser.h"
#include "Event.h"
#include "Conntrack-nat.h"
#include "IpHelper.h"
#pragma warning(push)
#pragma warning(disable:4311)
#define WINDOWS_TICK 10000000
#define SEC_TO_UNIX_EPOCH 11644473600LL
#define SEC_TO_NANOSEC 1000000000LL
#define CT_MAX_ZONE (UINT16_MAX + 1)
KSTART_ROUTINE OvsConntrackEntryCleaner;
static PLIST_ENTRY ovsConntrackTable;
static OVS_CT_THREAD_CTX ctThreadCtx;
static PNDIS_RW_LOCK_EX *ovsCtBucketLock = NULL;
static NDIS_SPIN_LOCK ovsCtZoneLock;
static POVS_CT_ZONE_INFO zoneInfo = NULL;
extern POVS_SWITCH_CONTEXT gOvsSwitchContext;
static ULONG ctTotalEntries;
static ULONG defaultCtLimit;
static __inline OvsCtFlush(UINT16 zone, struct ovs_key_ct_tuple_ipv4 *tuple);
static __inline NDIS_STATUS
MapNlToCtTuple(POVS_MESSAGE msgIn, PNL_ATTR attr,
struct ovs_key_ct_tuple_ipv4 *ct_tuple);
/*
*----------------------------------------------------------------------------
* OvsInitConntrack
* Initialize the components used by Connection Tracking
*----------------------------------------------------------------------------
*/
NTSTATUS
OvsInitConntrack(POVS_SWITCH_CONTEXT context)
{
NTSTATUS status = STATUS_SUCCESS;
HANDLE threadHandle = NULL;
ctTotalEntries = 0;
UINT32 numBucketLocks = CT_HASH_TABLE_SIZE;
/* Init the Hash Buffer */
ovsConntrackTable = OvsAllocateMemoryWithTag(sizeof(LIST_ENTRY)
* CT_HASH_TABLE_SIZE,
OVS_CT_POOL_TAG);
if (ovsConntrackTable == NULL) {
return STATUS_INSUFFICIENT_RESOURCES;
}
ovsCtBucketLock = OvsAllocateMemoryWithTag(sizeof(PNDIS_RW_LOCK_EX)
* CT_HASH_TABLE_SIZE,
OVS_CT_POOL_TAG);
if (ovsCtBucketLock == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto freeTable;
}
for (UINT32 i = 0; i < CT_HASH_TABLE_SIZE; i++) {
InitializeListHead(&ovsConntrackTable[i]);
ovsCtBucketLock[i] = NdisAllocateRWLock(context->NdisFilterHandle);
if (ovsCtBucketLock[i] == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
numBucketLocks = i;
goto freeBucketLock;
}
}
/* Init CT Cleaner Thread */
KeInitializeEvent(&ctThreadCtx.event, NotificationEvent, FALSE);
status = PsCreateSystemThread(&threadHandle, SYNCHRONIZE, NULL, NULL,
NULL, OvsConntrackEntryCleaner,
&ctThreadCtx);
if (status != STATUS_SUCCESS) {
goto freeBucketLock;
}
ObReferenceObjectByHandle(threadHandle, SYNCHRONIZE, NULL, KernelMode,
&ctThreadCtx.threadObject, NULL);
ZwClose(threadHandle);
threadHandle = NULL;
zoneInfo = OvsAllocateMemoryWithTag(sizeof(OVS_CT_ZONE_INFO) *
CT_MAX_ZONE, OVS_CT_POOL_TAG);
if (zoneInfo == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto freeBucketLock;
}
NdisAllocateSpinLock(&ovsCtZoneLock);
defaultCtLimit = CT_MAX_ENTRIES;
for (UINT32 i = 0; i < CT_MAX_ZONE; i++) {
zoneInfo[i].entries = 0;
zoneInfo[i].limit = defaultCtLimit;
}
status = OvsNatInit();
if (status != STATUS_SUCCESS) {
OvsCleanupConntrack();
}
return STATUS_SUCCESS;
freeBucketLock:
for (UINT32 i = 0; i < numBucketLocks; i++) {
if (ovsCtBucketLock[i] != NULL) {
NdisFreeRWLock(ovsCtBucketLock[i]);
}
}
OvsFreeMemoryWithTag(ovsCtBucketLock, OVS_CT_POOL_TAG);
ovsCtBucketLock = NULL;
freeTable:
OvsFreeMemoryWithTag(ovsConntrackTable, OVS_CT_POOL_TAG);
ovsConntrackTable = NULL;
return status;
}
/*
*----------------------------------------------------------------------------
* OvsCleanupConntrack
* Cleanup memory and thread that were spawned for Connection tracking
*----------------------------------------------------------------------------
*/
VOID
OvsCleanupConntrack(VOID)
{
ctThreadCtx.exit = 1;
KeSetEvent(&ctThreadCtx.event, 0, FALSE);
KeWaitForSingleObject(ctThreadCtx.threadObject, Executive,
KernelMode, FALSE, NULL);
ObDereferenceObject(ctThreadCtx.threadObject);
/* Force flush all entries before removing */
OvsCtFlush(0, NULL);
if (ovsConntrackTable) {
OvsFreeMemoryWithTag(ovsConntrackTable, OVS_CT_POOL_TAG);
ovsConntrackTable = NULL;
}
for (UINT32 i = 0; i < CT_HASH_TABLE_SIZE; i++) {
/* Disabling the uninitialized memory warning because it should
* always be initialized during OvsInitConntrack */
#pragma warning(suppress: 6001)
if (ovsCtBucketLock[i] != NULL) {
NdisFreeRWLock(ovsCtBucketLock[i]);
}
}
OvsFreeMemoryWithTag(ovsCtBucketLock, OVS_CT_POOL_TAG);
ovsCtBucketLock = NULL;
OvsNatCleanup();
NdisFreeSpinLock(&ovsCtZoneLock);
if (zoneInfo) {
OvsFreeMemoryWithTag(zoneInfo, OVS_CT_POOL_TAG);
}
}
VOID
OvsCtSetZoneLimit(int zone, ULONG value) {
NdisAcquireSpinLock(&ovsCtZoneLock);
if (zone == -1) {
/* Set default limit for all zones. */
defaultCtLimit = value;
for (UINT32 i = 0; i < CT_MAX_ZONE; i++) {
zoneInfo[i].limit = value;
}
} else {
zoneInfo[(UINT16)zone].limit = value;
}
NdisReleaseSpinLock(&ovsCtZoneLock);
}
static uint32_t
OvsCtEndpointHashAdd(uint32_t hash, const struct ct_endpoint *ep)
{
BUILD_ASSERT_DECL(sizeof *ep % 4 == 0);
return OvsJhashBytes((UINT32 *)ep, sizeof *ep, hash);
}
/*
*----------------------------------------------------------------------------
* OvsCtHashKey
* Compute hash using 5-tuple and zone.
*----------------------------------------------------------------------------
*/
UINT32
OvsCtHashKey(const OVS_CT_KEY *key)
{
UINT32 hsrc, hdst, hash;
hsrc = ntohl(key->src.port);
hdst = ntohl(key->dst.port);
hsrc = OvsCtEndpointHashAdd(hsrc, &key->src);
hdst = OvsCtEndpointHashAdd(hdst, &key->dst);
hash = hsrc ^ hdst; /* TO identify reverse traffic */
hash = hash | (key->zone + key->nw_proto);
hash = OvsJhashWords((uint32_t*) &hash, 1, hash);
return hash;
}
static __inline VOID
OvsCtKeyReverse(OVS_CT_KEY *key)
{
struct ct_endpoint tmp;
tmp = key->src;
key->src = key->dst;
key->dst = tmp;
}
static __inline VOID
OvsCtUpdateFlowKey(struct OvsFlowKey *key,
UINT32 state,
UINT16 zone,
UINT32 mark,
struct ovs_key_ct_labels *labels)
{
key->ct.state = state | OVS_CS_F_TRACKED;
key->ct.zone = zone;
key->ct.mark = mark;
if (labels) {
NdisMoveMemory(&key->ct.labels, labels,
sizeof(struct ovs_key_ct_labels));
} else {
memset(&key->ct.labels, 0,
sizeof(struct ovs_key_ct_labels));
}
}
/*
*----------------------------------------------------------------------------
* OvsPostCtEventEntry
* Assumes ct entry lock is acquired
* XXX Refactor OvsPostCtEvent() as it does not require ct entry lock.
*----------------------------------------------------------------------------
*/
static __inline VOID
OvsPostCtEventEntry(POVS_CT_ENTRY entry, UINT8 type)
{
OVS_CT_EVENT_ENTRY ctEventEntry = {0};
NdisMoveMemory(&ctEventEntry.entry, entry, sizeof(OVS_CT_ENTRY));
ctEventEntry.type = type;
OvsPostCtEvent(&ctEventEntry);
}
static __inline VOID
OvsCtIncrementCounters(POVS_CT_ENTRY entry, BOOLEAN reply, PNET_BUFFER_LIST nbl)
{
NdisAcquireSpinLock(&(entry->lock));
if (reply) {
entry->rev_key.byteCount+= OvsPacketLenNBL(nbl);
entry->rev_key.packetCount++;
} else {
entry->key.byteCount += OvsPacketLenNBL(nbl);
entry->key.packetCount++;
}
NdisReleaseSpinLock(&(entry->lock));
}
static __inline BOOLEAN
OvsCtAddEntry(POVS_CT_ENTRY entry,
OvsConntrackKeyLookupCtx *ctx,
PNAT_ACTION_INFO natInfo, UINT64 now)
{
LOCK_STATE_EX lockState;
NdisMoveMemory(&entry->key, &ctx->key, sizeof(OVS_CT_KEY));
NdisMoveMemory(&entry->rev_key, &ctx->key, sizeof(OVS_CT_KEY));
OvsCtKeyReverse(&entry->rev_key);
/* NatInfo is always initialized to be disabled, so that if NAT action
* fails, we will not end up deleting an non-existent NAT entry.
*/
if (natInfo == NULL) {
entry->natInfo.natAction = NAT_ACTION_NONE;
} else {
if (OvsIsForwardNat(natInfo->natAction)) {
entry->natInfo = *natInfo;
if (!OvsNatTranslateCtEntry(entry)) {
return FALSE;
}
ctx->hash = OvsCtHashKey(&entry->key);
} else {
entry->natInfo.natAction = natInfo->natAction;
}
}
entry->timestampStart = now;
NdisAllocateSpinLock(&(entry->lock));
UINT32 bucketIdx = ctx->hash & CT_HASH_TABLE_MASK;
NdisAcquireRWLockWrite(ovsCtBucketLock[bucketIdx], &lockState, 0);
InsertHeadList(&ovsConntrackTable[bucketIdx],
&entry->link);
NdisInterlockedIncrement((PLONG)&ctTotalEntries);
NdisInterlockedIncrement((PLONG)&zoneInfo[ctx->key.zone].entries);
NdisReleaseRWLock(ovsCtBucketLock[bucketIdx], &lockState);
return TRUE;
}
static __inline POVS_CT_ENTRY
OvsCtEntryCreate(OvsForwardingContext *fwdCtx,
UINT8 ipProto,
OVS_PACKET_HDR_INFO *layers,
OvsConntrackKeyLookupCtx *ctx,
OvsFlowKey *key,
PNAT_ACTION_INFO natInfo,
BOOLEAN commit,
UINT64 currentTime,
BOOLEAN *entryCreated)
{
POVS_CT_ENTRY entry = NULL;
UINT32 state = 0;
POVS_CT_ENTRY parentEntry;
PNET_BUFFER_LIST curNbl = fwdCtx->curNbl;
*entryCreated = FALSE;
state |= OVS_CS_F_NEW;
switch (ipProto) {
case IPPROTO_TCP:
{
UINT32 tcpPayloadLen;
TCPHdr tcpStorage;
const TCPHdr *tcp;
tcp = OvsGetTcpHeader(curNbl, layers, &tcpStorage, &tcpPayloadLen);
if (!OvsConntrackValidateTcpPacket(tcp)) {
state = OVS_CS_F_INVALID;
break;
}
if (commit) {
entry = OvsConntrackCreateTcpEntry(tcp, currentTime,
tcpPayloadLen);
}
break;
}
case IPPROTO_ICMPV6:
{
ICMPHdr storage;
const ICMPHdr *icmp;
icmp = OvsGetIcmp(curNbl, layers->l4Offset, &storage);
if (!OvsConntrackValidateIcmp6Packet(icmp)) {
if (icmp) {
OVS_LOG_TRACE("Invalid ICMP6 packet detected, icmp->type %u",
icmp->type);
}
state = OVS_CS_F_INVALID;
break;
}
if (commit) {
entry = OvsConntrackCreateIcmpEntry(currentTime);
}
break;
}
case IPPROTO_ICMP:
{
ICMPHdr storage;
const ICMPHdr *icmp;
icmp = OvsGetIcmp(curNbl, layers->l4Offset, &storage);
if (!OvsConntrackValidateIcmpPacket(icmp)) {
if(icmp) {
OVS_LOG_TRACE("Invalid ICMP packet detected, icmp->type %u",
icmp->type);
}
state = OVS_CS_F_INVALID;
break;
}
if (commit) {
entry = OvsConntrackCreateIcmpEntry(currentTime);
}
break;
}
case IPPROTO_UDP:
{
if (commit) {
entry = OvsConntrackCreateOtherEntry(currentTime);
}
break;
}
default:
OVS_LOG_TRACE("Invalid packet detected, protocol not supported"
" ipProto %u", ipProto);
state = OVS_CS_F_INVALID;
break;
}
parentEntry = OvsCtRelatedLookup(ctx->key, currentTime);
if (parentEntry != NULL && state != OVS_CS_F_INVALID) {
state |= OVS_CS_F_RELATED;
}
if (state != OVS_CS_F_INVALID && commit) {
if (entry) {
entry->parent = parentEntry;
if (OvsCtAddEntry(entry, ctx, natInfo, currentTime)) {
*entryCreated = TRUE;
} else {
/* Unable to add entry to the list */
OvsFreeMemoryWithTag(entry, OVS_CT_POOL_TAG);
state = OVS_CS_F_INVALID;
entry = NULL;
}
} else {
/* OvsAllocateMemoryWithTag returned NULL; treat as invalid */
state = OVS_CS_F_INVALID;
}
}
OvsCtUpdateFlowKey(key, state, ctx->key.zone, 0, NULL);
if (entry) {
OvsCtIncrementCounters(entry, ctx->reply, curNbl);
}
return entry;
}
static enum CT_UPDATE_RES
OvsCtUpdateEntry(OVS_CT_ENTRY* entry,
PNET_BUFFER_LIST nbl,
UINT8 ipProto,
OVS_PACKET_HDR_INFO *layers,
BOOLEAN reply,
UINT64 now)
{
CT_UPDATE_RES status;
switch (ipProto) {
case IPPROTO_TCP:
{
UINT32 tcpPayloadLen;
TCPHdr tcpStorage;
const TCPHdr *tcp;
tcp = OvsGetTcpHeader(nbl, layers, &tcpStorage, &tcpPayloadLen);
if (!tcp) {
status = CT_UPDATE_INVALID;
break;
}
NdisAcquireSpinLock(&(entry->lock));
status = OvsConntrackUpdateTcpEntry(entry, tcp, reply, now,
tcpPayloadLen);
NdisReleaseSpinLock(&(entry->lock));
break;
}
case IPPROTO_ICMP:
{
NdisAcquireSpinLock(&(entry->lock));
status = OvsConntrackUpdateIcmpEntry(entry, reply, now);
NdisReleaseSpinLock(&(entry->lock));
break;
}
case IPPROTO_ICMPV6:
{
NdisAcquireSpinLock(&(entry->lock));
status = OvsConntrackUpdateIcmpEntry(entry, reply, now);
NdisReleaseSpinLock(&(entry->lock));
break;
}
case IPPROTO_UDP:
{
NdisAcquireSpinLock(&(entry->lock));
status = OvsConntrackUpdateOtherEntry(entry, reply, now);
NdisReleaseSpinLock(&(entry->lock));
break;
}
default:
status = CT_UPDATE_INVALID;
break;
}
return status;
}
/*
*----------------------------------------------------------------------------
* OvsCtEntryExpired
* Assumes ct entry lock is acquired
*----------------------------------------------------------------------------
*/
static __inline BOOLEAN
OvsCtEntryExpired(POVS_CT_ENTRY entry)
{
UINT64 currentTime;
NdisGetCurrentSystemTime((LARGE_INTEGER *)¤tTime);
return entry->expiration < currentTime;
}
static __inline VOID
OvsCtEntryDelete(POVS_CT_ENTRY entry, BOOLEAN forceDelete)
{
if (entry == NULL) {
return;
}
KIRQL irql = KeGetCurrentIrql();
OVS_ACQUIRE_SPIN_LOCK(&(entry->lock), irql);
if (forceDelete || OvsCtEntryExpired(entry)) {
if (entry->natInfo.natAction) {
OvsNatDeleteKey(&entry->key);
}
NdisInterlockedDecrement((PLONG)&zoneInfo[entry->key.zone].entries);
OvsPostCtEventEntry(entry, OVS_EVENT_CT_DELETE);
RemoveEntryList(&entry->link);
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
NdisFreeSpinLock(&(entry->lock));
if (entry->helper_name) {
OvsFreeMemoryWithTag(entry->helper_name, OVS_CT_POOL_TAG);
}
OvsFreeMemoryWithTag(entry, OVS_CT_POOL_TAG);
NdisInterlockedDecrement((PLONG)&ctTotalEntries);
return;
}
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
}
static __inline NDIS_STATUS
OvsDetectCtPacket(OvsForwardingContext *fwdCtx,
OvsFlowKey *key)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
OvsFlowKey newFlowKey = { 0 };
switch (ntohs(key->l2.dlType)) {
case ETH_TYPE_IPV4:
if (key->ipKey.nwFrag != OVS_FRAG_TYPE_NONE) {
status = OvsProcessIpv4Fragment(fwdCtx->switchContext,
&fwdCtx->curNbl,
fwdCtx->completionList,
fwdCtx->fwdDetail->SourcePortId,
&fwdCtx->layers,
key->tunKey.tunnelId);
if (status == NDIS_STATUS_SUCCESS) {
/* After the Ipv4 Fragment is reassembled, update flow key as
L3 and L4 headers are not correct */
status = OvsExtractFlow(fwdCtx->curNbl, fwdCtx->srcVportNo,
&newFlowKey, &fwdCtx->layers,
!OvsIphIsZero(&(fwdCtx->tunKey.dst)) ?
&(fwdCtx->tunKey) : NULL);
if (status != NDIS_STATUS_SUCCESS) {
OVS_LOG_ERROR("Extract flow failed Nbl %p", fwdCtx->curNbl);
return status;
}
*key = newFlowKey;
}
return status;
}
if (key->ipKey.nwProto == IPPROTO_TCP
|| key->ipKey.nwProto == IPPROTO_UDP
|| key->ipKey.nwProto == IPPROTO_ICMP) {
return NDIS_STATUS_SUCCESS;
}
return NDIS_STATUS_NOT_SUPPORTED;
case ETH_TYPE_IPV6:
if (key->ipv6Key.nwFrag != OVS_FRAG_TYPE_NONE) {
status = OvsProcessIpv6Fragment(fwdCtx->switchContext,
&fwdCtx->curNbl,
fwdCtx->completionList,
fwdCtx->fwdDetail->SourcePortId,
&fwdCtx->layers,
key->tunKey.tunnelId, key);
if (status == NDIS_STATUS_SUCCESS) {
status = OvsExtractFlow(fwdCtx->curNbl, fwdCtx->srcVportNo,
&newFlowKey, &fwdCtx->layers,
!OvsIphIsZero(&(fwdCtx->tunKey.dst)) ?
&(fwdCtx->tunKey) : NULL);
if (status != NDIS_STATUS_SUCCESS) {
OVS_LOG_ERROR("Extract flow for ipv6 failed Nbl %p",
fwdCtx->curNbl);
return status;
}
*key = newFlowKey;
}
return status;
}
if (key->ipv6Key.nwProto == IPPROTO_ICMPV6
|| key->ipv6Key.nwProto == IPPROTO_TCP
|| key->ipv6Key.nwProto == IPPROTO_UDP) {
return NDIS_STATUS_SUCCESS;
}
return NDIS_STATUS_NOT_SUPPORTED;
}
return NDIS_STATUS_NOT_SUPPORTED;
}
BOOLEAN
OvsCtEndpointsAreSame(OVS_CT_KEY ctxKey, OVS_CT_KEY entryKey)
{
return ((NdisEqualMemory(&ctxKey.src, &entryKey.src,
sizeof(struct ct_endpoint))) &&
(NdisEqualMemory(&ctxKey.dst, &entryKey.dst,
sizeof(struct ct_endpoint))));
}
POVS_CT_ENTRY
OvsCtLookup(OvsConntrackKeyLookupCtx *ctx)
{
PLIST_ENTRY link;
POVS_CT_ENTRY entry;
BOOLEAN reply = FALSE;
POVS_CT_ENTRY found = NULL;
LOCK_STATE_EX lockStateTable;
UINT32 bucketIdx;
if (!ctTotalEntries) {
return found;
}
/* Reverse NAT must be performed before OvsCtLookup, so here
* we simply need to flip the src and dst in key and compare
* they are equal. Note that flipped key is not equal to
* rev_key due to NAT effect.
*/
OVS_CT_KEY revCtxKey = ctx->key;
OvsCtKeyReverse(&revCtxKey);
KIRQL irql = KeGetCurrentIrql();
bucketIdx = ctx->hash & CT_HASH_TABLE_MASK;
NdisAcquireRWLockRead(ovsCtBucketLock[bucketIdx], &lockStateTable, 0);
LIST_FORALL(&ovsConntrackTable[bucketIdx], link) {
entry = CONTAINING_RECORD(link, OVS_CT_ENTRY, link);
OVS_ACQUIRE_SPIN_LOCK(&(entry->lock), irql);
if ((ctx->key.dl_type != entry->key.dl_type) ||
(ctx->key.nw_proto != entry->key.nw_proto) ||
(ctx->key.zone != entry->key.zone)) {
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
continue;
}
if (OvsCtEndpointsAreSame(ctx->key, entry->key)) {
found = entry;
reply = FALSE;
}
if (!found && OvsCtEndpointsAreSame(revCtxKey, entry->key)) {
found = entry;
reply = TRUE;
}
if (found) {
if (OvsCtEntryExpired(found)) {
found = NULL;
} else {
ctx->reply = reply;
}
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
break;
}
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
}
NdisReleaseRWLock(ovsCtBucketLock[bucketIdx], &lockStateTable);
ctx->entry = found;
return found;
}
const TCPHdr*
OvsGetTcpHeader(PNET_BUFFER_LIST nbl,
OVS_PACKET_HDR_INFO *layers,
VOID *storage,
UINT32 *tcpPayloadLen)
{
IPHdr *ipHdr;
IPv6Hdr *ipv6Hdr;
TCPHdr *tcp;
VOID *dest = storage;
uint16_t ipv6ExtLength = 0;
if (layers->isIPv6) {
ipv6Hdr = NdisGetDataBuffer(NET_BUFFER_LIST_FIRST_NB(nbl),
layers->l4Offset + sizeof(TCPHdr),
NULL, 1, 0);
if (ipv6Hdr == NULL) {
return NULL;
}
tcp = (TCPHdr *)((PCHAR)ipv6Hdr + layers->l4Offset);
ipv6Hdr = (IPv6Hdr *)((PCHAR)ipv6Hdr + layers->l3Offset);
if (tcp->doff * 4 >= sizeof *tcp) {
NdisMoveMemory(dest, tcp, sizeof(TCPHdr));
ipv6ExtLength = layers->l4Offset - layers->l3Offset - sizeof(IPv6Hdr);
*tcpPayloadLen = (ntohs(ipv6Hdr->payload_len) - ipv6ExtLength - TCP_HDR_LEN(tcp));
return storage;
}
} else {
ipHdr = NdisGetDataBuffer(NET_BUFFER_LIST_FIRST_NB(nbl),
layers->l4Offset + sizeof(TCPHdr),
NULL, 1 /*no align*/, 0);
if (ipHdr == NULL) {
return NULL;
}
ipHdr = (IPHdr *)((PCHAR)ipHdr + layers->l3Offset);
tcp = (TCPHdr *)((PCHAR)ipHdr + ipHdr->ihl * 4);
if (tcp->doff * 4 >= sizeof *tcp) {
NdisMoveMemory(dest, tcp, sizeof(TCPHdr));
*tcpPayloadLen = TCP_DATA_LENGTH(ipHdr, tcp);
return storage;
}
}
return NULL;
}
static UINT8
OvsReverseIcmpType(UINT8 type)
{
switch (type) {
case ICMP4_ECHO_REQUEST:
return ICMP4_ECHO_REPLY;
case ICMP4_ECHO_REPLY:
return ICMP4_ECHO_REQUEST;
case ICMP4_TIMESTAMP_REQUEST:
return ICMP4_TIMESTAMP_REPLY;
case ICMP4_TIMESTAMP_REPLY:
return ICMP4_TIMESTAMP_REQUEST;
case ICMP4_INFO_REQUEST:
return ICMP4_INFO_REPLY;
case ICMP4_INFO_REPLY:
return ICMP4_INFO_REQUEST;
case ICMP6_ECHO_REQUEST:
return ICMP6_ECHO_REPLY;
case ICMP6_ECHO_REPLY:
return ICMP6_ECHO_REQUEST;
default:
return 0;
}
}
static __inline void
OvsPickupCtTupleAsLookupKey(POVS_CT_KEY ctKey, UINT16 zone, OvsFlowKey *flowKey)
{
UINT32 ipAddrSrc = 0, ipAddrDst = 0;
if (!flowKey || !ctKey) return;
if (flowKey->l2.dlType == htons(ETH_TYPE_IPV4)) {
ipAddrSrc = flowKey->ct.tuple_ipv4.ipv4_src;
ipAddrDst = flowKey->ct.tuple_ipv4.ipv4_dst;
if ((ipAddrSrc > 0 && ipAddrDst > 0) &&
(zone == flowKey->ct.zone)) {
/* if the ct tuple_ipv4 in flowKey is not null and ct.zone is same with
* zone parameter pickup the tuple_ipv4 value as the lookup key
*/
ctKey->src.addr.ipv4 = flowKey->ct.tuple_ipv4.ipv4_src;
ctKey->dst.addr.ipv4 = flowKey->ct.tuple_ipv4.ipv4_dst;
ctKey->nw_proto = flowKey->ct.tuple_ipv4.ipv4_proto;
ctKey->src.port = flowKey->ct.tuple_ipv4.src_port;
ctKey->dst.port = flowKey->ct.tuple_ipv4.dst_port;
}
}
}
static __inline NDIS_STATUS
OvsCtSetupLookupCtx(OvsFlowKey *flowKey,
UINT16 zone,
OvsConntrackKeyLookupCtx *ctx,
PNET_BUFFER_LIST curNbl,
UINT32 l4Offset)
{
const OVS_NAT_ENTRY *natEntry;
ctx->key.zone = zone;
ctx->key.dl_type = flowKey->l2.dlType;
ctx->related = FALSE;
/* Extract L3 and L4*/
if (flowKey->l2.dlType == htons(ETH_TYPE_IPV4)) {
ctx->key.src.addr.ipv4 = flowKey->ipKey.nwSrc;
ctx->key.dst.addr.ipv4 = flowKey->ipKey.nwDst;
ctx->key.nw_proto = flowKey->ipKey.nwProto;
ctx->key.src.port = flowKey->ipKey.l4.tpSrc;
ctx->key.dst.port = flowKey->ipKey.l4.tpDst;
if (flowKey->ipKey.nwProto == IPPROTO_ICMP) {
ICMPHdr icmpStorage;
const ICMPHdr *icmp;
icmp = OvsGetIcmp(curNbl, l4Offset, &icmpStorage);
ASSERT(icmp);
/* Related bit is set when ICMP has an error */
/* XXX parse out the appropriate src and dst from inner pkt */
switch (icmp->type) {
case ICMP4_ECHO_REQUEST:
case ICMP4_ECHO_REPLY:
case ICMP4_TIMESTAMP_REQUEST:
case ICMP4_TIMESTAMP_REPLY:
case ICMP4_INFO_REQUEST:
case ICMP4_INFO_REPLY:
if (icmp->code != 0) {
return NDIS_STATUS_INVALID_PACKET;
}
/* Separate ICMP connection: identified using id */
ctx->key.dst.icmp_id = ntohs(icmp->fields.echo.id);
ctx->key.src.icmp_id = ntohs(icmp->fields.echo.id);
ctx->key.src.icmp_type = icmp->type;
ctx->key.dst.icmp_type = OvsReverseIcmpType(icmp->type);
break;
case ICMP4_DEST_UNREACH:
case ICMP4_TIME_EXCEEDED:
case ICMP4_PARAM_PROB:
case ICMP4_SOURCE_QUENCH:
case ICMP4_REDIRECT: {
ctx->related = TRUE;
break;
}
default:
ctx->related = FALSE;
}
}
} else if (flowKey->l2.dlType == htons(ETH_TYPE_IPV6)) {
ctx->key.src.addr.ipv6 = flowKey->ipv6Key.ipv6Src;
ctx->key.dst.addr.ipv6 = flowKey->ipv6Key.ipv6Dst;
ctx->key.nw_proto = flowKey->ipv6Key.nwProto;
ctx->key.src.port = flowKey->ipv6Key.l4.tpSrc;
ctx->key.dst.port = flowKey->ipv6Key.l4.tpDst;
if (flowKey->ipv6Key.nwProto == IPPROTO_ICMPV6) {
ICMPHdr icmpStorage;
const ICMPHdr *icmp;
icmp = OvsGetIcmp(curNbl, l4Offset, &icmpStorage);
ASSERT(icmp);
switch (icmp->type) {
case ICMP6_ECHO_REQUEST:
case ICMP6_ECHO_REPLY: {
ctx->key.dst.icmp_id = ntohs(icmp->fields.echo.id);
ctx->key.src.icmp_id = ntohs(icmp->fields.echo.id);
ctx->key.src.icmp_type = icmp->type;
ctx->key.dst.icmp_type = OvsReverseIcmpType(icmp->type);
break;
}
case ICMP6_DST_UNREACH:
case ICMP6_TIME_EXCEEDED:
case ICMP6_PARAM_PROB:
case ICMP6_PACKET_TOO_BIG: {
Ipv6Key ipv6Key;
OVS_PACKET_HDR_INFO layers;
OvsExtractLayers(curNbl, &layers);
layers.l3Offset = layers.l7Offset;
NDIS_STATUS status = OvsParseIPv6(curNbl, &ipv6Key, &layers);
if (status != NDIS_STATUS_SUCCESS) {
return NDIS_STATUS_INVALID_PACKET;
}
ctx->key.src.addr.ipv6 = ipv6Key.ipv6Src;
ctx->key.dst.addr.ipv6 = ipv6Key.ipv6Dst;
ctx->key.nw_proto = ipv6Key.nwProto;
if (ipv6Key.nwProto == SOCKET_IPPROTO_TCP) {
OvsParseTcp(curNbl, &(ipv6Key.l4), &layers);
} else if (ipv6Key.nwProto == SOCKET_IPPROTO_UDP) {
OvsParseUdp(curNbl, &(ipv6Key.l4), &layers);
} else if (ipv6Key.nwProto == SOCKET_IPPROTO_SCTP) {
OvsParseSctp(curNbl, &ipv6Key.l4, &layers);
}
ctx->key.src.port = ipv6Key.l4.tpSrc;
ctx->key.dst.port = ipv6Key.l4.tpDst;
OvsCtKeyReverse(&ctx->key);
ctx->related = TRUE;
break;
}
default:
ctx->related = FALSE;
}
}
} else {
return NDIS_STATUS_INVALID_PACKET;
}
/* It's only designed for unNat traffic, when reverse traffic comes,
* find the unNat table, if found the nat entry, based on the nat entry
* restore the conntrack, it will be stored in the ctx->key and then use the
* ctx->key lookup the conntrack table to find the corresponded
* entry with the traffic.*/
natEntry = OvsNatLookup(&ctx->key, TRUE);
if (natEntry) {
/* initial direction 20::1 -> 20::9, reverse direction 21::3 -> 20::1
* 20::9 could be regarded as nat ip, before convert, ctx->key value
* is "21::3 -> 20::1", after convert, ctx->key value is
* "20::9->20::1" */
ctx->key = natEntry->ctEntry->key;
OvsCtKeyReverse(&ctx->key);
} else {
if (OvsNatLookup(&ctx->key, FALSE)) {
/* Do nothing here, this branch here used to exclude traffic
* described in https://github.com/openvswitch/ovs-issues/issues/237
* */
} else if (flowKey->l2.dlType == htons(ETH_TYPE_IPV4)) {
OvsPickupCtTupleAsLookupKey(&(ctx->key), zone, flowKey);
}
}
ctx->hash = OvsCtHashKey(&ctx->key);
return NDIS_STATUS_SUCCESS;
}
static __inline BOOLEAN
OvsDetectFtpPacket(OvsFlowKey *key) {
return (key->ipKey.nwProto == IPPROTO_TCP &&
(ntohs(key->ipKey.l4.tpDst) == IPPORT_FTP ||
ntohs(key->ipKey.l4.tpSrc) == IPPORT_FTP));
}
static __inline BOOLEAN
OvsDetectFtp6Packet(OvsFlowKey *key) {
return (key->ipv6Key.nwProto == IPPROTO_TCP &&
(ntohs(key->ipv6Key.l4.tpDst) == IPPORT_FTP ||
ntohs(key->ipv6Key.l4.tpSrc) == IPPORT_FTP));
}
static __inline BOOLEAN
OvsDetectTftpPacket(OvsFlowKey *key) {
return (key->ipKey.nwProto == IPPROTO_UDP &&
(ntohs(key->ipKey.l4.tpDst) == IPPORT_TFTP));
}
static __inline BOOLEAN
OvsDetectTftp6Packet(OvsFlowKey *key) {
return (key->ipv6Key.nwProto == IPPROTO_UDP &&
(ntohs(key->ipv6Key.l4.tpDst) == IPPORT_TFTP));
}
/*
*----------------------------------------------------------------------------
* OvsProcessConntrackEntry
* Check the TCP flags and set the ct_state of the entry
*----------------------------------------------------------------------------
*/
static __inline POVS_CT_ENTRY
OvsProcessConntrackEntry(OvsForwardingContext *fwdCtx,
OVS_PACKET_HDR_INFO *layers,
OvsConntrackKeyLookupCtx *ctx,
OvsFlowKey *key,
UINT16 zone,
NAT_ACTION_INFO *natInfo,
BOOLEAN commit,
UINT64 currentTime,
BOOLEAN *entryCreated)
{
POVS_CT_ENTRY entry = ctx->entry;
UINT32 state = 0;
PNET_BUFFER_LIST curNbl = fwdCtx->curNbl;
LOCK_STATE_EX lockStateTable;
*entryCreated = FALSE;
/* If an entry was found, update the state based on TCP flags */
if (ctx->related) {
state |= OVS_CS_F_RELATED;
if (ctx->reply) {
state |= OVS_CS_F_REPLY_DIR;
}
} else {
CT_UPDATE_RES result;
UINT32 bucketIdx;
if (layers->isIPv6) {
result = OvsCtUpdateEntry(entry, curNbl, key->ipv6Key.nwProto, layers,
ctx->reply, currentTime);
} else {
result = OvsCtUpdateEntry(entry, curNbl, key->ipKey.nwProto, layers,
ctx->reply, currentTime);
}
switch (result) {
case CT_UPDATE_VALID:
state |= OVS_CS_F_ESTABLISHED;
/** when the ct state is established, at least
* request/reply two packets go through,
* so the ct_state shouldn't contain new.**/
state &= ~OVS_CS_F_NEW;
if (ctx->reply) {
state |= OVS_CS_F_REPLY_DIR;
}
break;
case CT_UPDATE_INVALID:
state |= OVS_CS_F_INVALID;
break;
case CT_UPDATE_NEW:
//Delete and update the Conntrack
bucketIdx = ctx->hash & CT_HASH_TABLE_MASK;
NdisAcquireRWLockWrite(ovsCtBucketLock[bucketIdx], &lockStateTable, 0);
OvsCtEntryDelete(ctx->entry, TRUE);
NdisReleaseRWLock(ovsCtBucketLock[bucketIdx], &lockStateTable);
ctx->entry = NULL;
if (layers->isIPv6) {
entry = OvsCtEntryCreate(fwdCtx, key->ipv6Key.nwProto, layers,
ctx, key, natInfo, commit, currentTime,
entryCreated);
} else {
entry = OvsCtEntryCreate(fwdCtx, key->ipKey.nwProto, layers,
ctx, key, natInfo, commit, currentTime,
entryCreated);
}
if (!entry) {
return NULL;
}
break;
case CT_UPDATE_VALID_NEW:
state |= OVS_CS_F_NEW;
break;
}
}
if (entry) {
NdisAcquireSpinLock(&(entry->lock));
if ((layers->isIPv6 && key->ipv6Key.nwProto == IPPROTO_TCP) ||
(!(layers->isIPv6) && key->ipKey.nwProto == IPPROTO_TCP) ||
(layers->isIPv6 && key->ipv6Key.nwProto == IPPROTO_UDP) ||
(!(layers->isIPv6) && key->ipKey.nwProto == IPPROTO_UDP)) {
/* Update the related bit if there is a parent */
if (entry->parent) {
state |= OVS_CS_F_RELATED;
} else {
POVS_CT_ENTRY parentEntry;
parentEntry = OvsCtRelatedLookup(ctx->key, currentTime);
entry->parent = parentEntry;
if (parentEntry != NULL) {
state |= OVS_CS_F_RELATED;
}
}
}
/* Copy mark and label from entry into flowKey. If actions specify
different mark and label, update the flowKey. */
OvsCtUpdateFlowKey(key, state, zone, entry->mark, &entry->labels);
NdisReleaseSpinLock(&(entry->lock));
} else {
OvsCtUpdateFlowKey(key, state, zone, 0, NULL);
}
return entry;
}
static __inline VOID
OvsConntrackSetMark(OvsFlowKey *key,
POVS_CT_ENTRY entry,
MD_MARK *mark,
BOOLEAN *markChanged)
{
POVS_CT_ENTRY parent = entry->parent;
BOOLEAN changed = FALSE;
UINT32 newMark = 0;
if (parent && parent->mark) {
newMark = parent->mark;
changed = TRUE;
} else if (mark) {
newMark = mark->value | (entry->mark & ~(mark->mask));
changed = TRUE;
}
if (changed && entry->mark != newMark) {
entry->mark = newMark;
key->ct.mark = newMark;
*markChanged = TRUE;
}
}
static __inline BOOLEAN
OvsConntrackIsLabelsNonZero(const struct ovs_key_ct_labels *labels)
{
UINT8 i;
for (i = 0; i < OVS_CT_LABELS_LEN_32; i++) {
if (labels->ct_labels_32[i]) {
return TRUE;
}
}
return FALSE;
}
static __inline void
OvsConntrackSetLabels(OvsFlowKey *key,
POVS_CT_ENTRY entry,
MD_LABELS *labels,
BOOLEAN *labelChanged)
{
POVS_CT_ENTRY parent = entry->parent;
/* Inherit master's labels at labels initialization, if any. */
if (!OvsConntrackIsLabelsNonZero(&entry->labels) &&
parent && OvsConntrackIsLabelsNonZero(&parent->labels)) {
RtlCopyMemory(&entry->labels, &parent->labels, OVS_CT_LABELS_LEN);
*labelChanged = TRUE;
}
/* Update labels according to value of ct_label in ct commit */
if (labels && OvsConntrackIsLabelsNonZero(&labels->mask)) {
UINT8 i;
UINT32 *dst = entry->labels.ct_labels_32;
for (i = 0; i < OVS_CT_LABELS_LEN_32; i++) {
dst[i] = (dst[i] & ~(labels->mask.ct_labels_32[i])) |
(labels->value.ct_labels_32[i] & labels->mask.ct_labels_32[i]);
}
*labelChanged = TRUE;
}
/* Update flow key's ct labels */
NdisMoveMemory(&key->ct.labels, &entry->labels, OVS_CT_LABELS_LEN);
}
static void
OvsCtSetMarkLabel(OvsFlowKey *key,
POVS_CT_ENTRY entry,
MD_MARK *mark,
MD_LABELS *labels,
BOOLEAN *triggerUpdateEvent)
{
OvsConntrackSetMark(key, entry, mark, triggerUpdateEvent);
OvsConntrackSetLabels(key, entry, labels, triggerUpdateEvent);
}
/*
*----------------------------------------------------------------------------
* OvsCtUpdateTuple
* Assumes ct entry lock is acquired
*----------------------------------------------------------------------------
*/
static __inline void
OvsCtUpdateTuple(OvsFlowKey *key, OVS_CT_KEY *ctKey)
{
key->ct.tuple_ipv4.ipv4_src = ctKey->src.addr.ipv4_aligned;
key->ct.tuple_ipv4.ipv4_dst = ctKey->dst.addr.ipv4_aligned;
key->ct.tuple_ipv4.ipv4_proto = ctKey->nw_proto;
/* Orig tuple Port is overloaded to take in ICMP-Type & Code */
/* This mimics the behavior in lib/conntrack.c*/
key->ct.tuple_ipv4.src_port = ctKey->nw_proto != IPPROTO_ICMP ?
ctKey->src.port :
htons(ctKey->src.icmp_type);
key->ct.tuple_ipv4.dst_port = ctKey->nw_proto != IPPROTO_ICMP ?
ctKey->dst.port :
htons(ctKey->src.icmp_code);
}
/*
* --------------------------------------------------------------------------
* OvsCtUpdateTupleV6 name --
* Update origin tuple for ipv6 packet.
* --------------------------------------------------------------------------
*/
static __inline void
OvsCtUpdateTupleV6(OvsFlowKey *key, OVS_CT_KEY *ctKey)
{
RtlCopyMemory(&key->ct.tuple_ipv6.ipv6_src, &ctKey->src.addr.ipv6_aligned, sizeof(key->ct.tuple_ipv6.ipv6_src));
RtlCopyMemory(&key->ct.tuple_ipv6.ipv6_dst, &ctKey->dst.addr.ipv6_aligned, sizeof(key->ct.tuple_ipv6.ipv6_dst));
key->ct.tuple_ipv6.ipv6_proto = ctKey->nw_proto;
/* Orig tuple Port is overloaded to take in ICMP-Type & Code */
/* This mimics the behavior in lib/conntrack.c*/
key->ct.tuple_ipv6.src_port = ctKey->nw_proto != IPPROTO_ICMPV6 ?
ctKey->src.port :
htons(ctKey->src.icmp_type);
key->ct.tuple_ipv6.dst_port = ctKey->nw_proto != IPPROTO_ICMPV6 ?
ctKey->dst.port :
htons(ctKey->src.icmp_code);
}
static __inline NDIS_STATUS
OvsCtExecute_(OvsForwardingContext *fwdCtx,
OvsFlowKey *key,
OVS_PACKET_HDR_INFO *layers,
BOOLEAN commit,
BOOLEAN force,
UINT16 zone,
MD_MARK *mark,
MD_LABELS *labels,
PCHAR helper,
PNAT_ACTION_INFO natInfo,
BOOLEAN postUpdateEvent)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
BOOLEAN triggerUpdateEvent = FALSE;
BOOLEAN entryCreated = FALSE;
POVS_CT_ENTRY entry = NULL;
POVS_CT_ENTRY parent = NULL;
PNET_BUFFER_LIST curNbl = fwdCtx->curNbl;
OvsConntrackKeyLookupCtx ctx = { 0 };
CT_HELPER_METHOD helpMethod = CT_HELPER_NONE;
LOCK_STATE_EX lockStateTable;
UINT64 currentTime;
NdisGetCurrentSystemTime((LARGE_INTEGER *) ¤tTime);
/* Retrieve the Conntrack Key related fields from packet */
OvsCtSetupLookupCtx(key, zone, &ctx, curNbl, layers->l4Offset);
/* Lookup Conntrack entries for a matching entry */
entry = OvsCtLookup(&ctx);
/* Delete entry in reverse direction if 'force' is specified */
if (force && ctx.reply && entry) {
UINT32 bucketIdx = ctx.hash & CT_HASH_TABLE_MASK;
NdisAcquireRWLockWrite(ovsCtBucketLock[bucketIdx], &lockStateTable, 0);
OvsCtEntryDelete(entry, TRUE);
NdisReleaseRWLock(ovsCtBucketLock[bucketIdx], &lockStateTable);
entry = NULL;
}
if (entry) {
/* Increment stats for the entry if it wasn't tracked previously or
* if they are on different zones
*/
if ((entry->key.zone != key->ct.zone ||
(!(key->ct.state & OVS_CS_F_TRACKED)))) {
OvsCtIncrementCounters(entry, ctx.reply, curNbl);
}
/* Process the entry and update CT flags */
entry = OvsProcessConntrackEntry(fwdCtx, layers, &ctx, key,
zone, natInfo, commit, currentTime,
&entryCreated);
} else {
if (commit && (ctTotalEntries >= CT_MAX_ENTRIES ||
zoneInfo[ctx.key.zone].entries >= zoneInfo[ctx.key.zone].limit)) {
/* Don't proceed with processing if the max limit has been hit.
* This blocks only new entries from being created and doesn't
* affect existing connections.
*/
OVS_LOG_ERROR("Conntrack Limit hit: zone(%u), zoneLimit(%lu),"
"zoneEntries(%lu), ctTotalEntries(%lu)",
zone, zoneInfo[ctx.key.zone].limit,
zoneInfo[ctx.key.zone].entries, ctTotalEntries);
return NDIS_STATUS_RESOURCES;
}
/* If no matching entry was found, create one and add New state */
if (key->l2.dlType == htons(ETH_TYPE_IPV6)) {
entry = OvsCtEntryCreate(fwdCtx, key->ipv6Key.nwProto,
layers, &ctx,
key, natInfo, commit, currentTime,
&entryCreated);
} else {
entry = OvsCtEntryCreate(fwdCtx, key->ipKey.nwProto,
layers, &ctx,
key, natInfo, commit, currentTime,
&entryCreated);
}
}
if (entry == NULL) {
return status;
}
/*
* Note that natInfo is not the same as entry->natInfo here. natInfo
* is decided by action in the openflow rule, entry->natInfo is decided
* when the entry is created. In the reverse NAT case, natInfo is
* NAT_ACTION_REVERSE, yet entry->natInfo is NAT_ACTION_SRC or
* NAT_ACTION_DST without NAT_ACTION_REVERSE
*/
KIRQL irql = KeGetCurrentIrql();
OVS_ACQUIRE_SPIN_LOCK(&(entry->lock), irql);
if (natInfo->natAction != NAT_ACTION_NONE) {
OvsNatPacket(fwdCtx, entry, entry->natInfo.natAction,
key, ctx.reply);
}
OvsCtSetMarkLabel(key, entry, mark, labels, &triggerUpdateEvent);
if (helper && RtlEqualMemory(helper, "ftp", sizeof("ftp"))) {
helpMethod = CT_HELPER_FTP;
} else if (helper && RtlEqualMemory(helper, "tftp", sizeof("tftp"))) {
helpMethod = CT_HELPER_TFTP;
}
/* This code section was added to compatible with the old version,
* because old version regard all traffic to port 21 as ftp traffic,
* no need to add alg field in ct. Thus, the code was added to keep the
* same behavior for ftp and tftp.*/
if (layers->isIPv6) {
if (OvsDetectFtp6Packet(key)) {
helpMethod = CT_HELPER_FTP;
}
if (OvsDetectTftp6Packet(key)) {
helpMethod = CT_HELPER_TFTP;
}
} else {
if (OvsDetectFtpPacket(key)) {
helpMethod = CT_HELPER_FTP;
}
if (OvsDetectTftpPacket(key)) {
helpMethod = CT_HELPER_TFTP;
}
}
if (helpMethod == CT_HELPER_FTP) {
status = OvsCtHandleFtp(curNbl, key, layers, currentTime, entry);
if (status != NDIS_STATUS_SUCCESS) {
OVS_LOG_ERROR("Error while parsing the FTP packet");
}
}
if (helpMethod == CT_HELPER_TFTP) {
status = OvsCtHandleTftp(curNbl, key, layers, currentTime, entry);
if (status != NDIS_STATUS_SUCCESS) {
OVS_LOG_ERROR("Error while parsing the TFTP packet");
}
}
parent = entry->parent;
/* The entry should have the same helper name with parent's */
if (!entry->helper_name &&
(helper || (parent && parent->helper_name))) {
helper = helper ? helper : parent->helper_name;
entry->helper_name = OvsAllocateMemoryWithTag(strlen(helper) + 1,
OVS_CT_POOL_TAG);
if (!entry->helper_name) {
OVS_LOG_ERROR("Error while allocating memory");
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
return NDIS_STATUS_RESOURCES;
}
memcpy(entry->helper_name, helper, strlen(helper) + 1);
}
/* Add original tuple information to flow Key */
if (entry->key.dl_type == ntohs(ETH_TYPE_IPV4)) {
if (parent != NULL) {
OVS_ACQUIRE_SPIN_LOCK(&(parent->lock), irql);
OvsCtUpdateTuple(key, &parent->key);
OVS_RELEASE_SPIN_LOCK(&(parent->lock), irql);
} else {
OvsCtUpdateTuple(key, &entry->key);
}
} else if (entry->key.dl_type == ntohs(ETH_TYPE_IPV6)) {
if (parent != NULL) {
OVS_ACQUIRE_SPIN_LOCK(&(parent->lock), irql);
OvsCtUpdateTupleV6(key, &parent->key);
OVS_RELEASE_SPIN_LOCK(&(parent->lock), irql);
} else {
OvsCtUpdateTupleV6(key, &entry->key);
}
}
if (entryCreated) {
OvsPostCtEventEntry(entry, OVS_EVENT_CT_NEW);
} else if (postUpdateEvent && triggerUpdateEvent) {
OvsPostCtEventEntry(entry, OVS_EVENT_CT_UPDATE);
}
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
return status;
}
/*
*---------------------------------------------------------------------------
* OvsExecuteConntrackAction
* Executes Conntrack actions XXX - Add more
* For the Ipv4 fragments, consume the orginal fragment NBL
*---------------------------------------------------------------------------
*/
NDIS_STATUS
OvsExecuteConntrackAction(OvsForwardingContext *fwdCtx,
OvsFlowKey *key,
const PNL_ATTR a)
{
BOOLEAN commit = FALSE;
BOOLEAN force = FALSE;
BOOLEAN postUpdateEvent = FALSE;
UINT16 zone = 0;
UINT32 eventmask = 0;
MD_MARK *mark = NULL;
MD_LABELS *labels = NULL;
PCHAR helper = NULL;
NAT_ACTION_INFO natActionInfo;
OVS_PACKET_HDR_INFO *layers = &fwdCtx->layers;
NDIS_STATUS status;
memset(&natActionInfo, 0, sizeof natActionInfo);
status = OvsDetectCtPacket(fwdCtx, key);
if (status != NDIS_STATUS_SUCCESS) {
return status;
}
PNL_ATTR ctAttr = NULL;
INT left;
NL_NESTED_FOR_EACH (ctAttr, left, a) {
switch(NlAttrType(ctAttr)) {
case OVS_CT_ATTR_ZONE:
zone = NlAttrGetU16(ctAttr);
break;
case OVS_CT_ATTR_COMMIT:
commit = TRUE;
break;
case OVS_CT_ATTR_MARK:
mark = NlAttrGet(ctAttr);
break;
case OVS_CT_ATTR_LABELS:
labels = NlAttrGet(ctAttr);
break;
case OVS_CT_ATTR_HELPER:
helper = NlAttrGetString(ctAttr);
if (helper == NULL) {
return NDIS_STATUS_INVALID_PARAMETER;
}
if (strcmp("ftp", helper) != 0 && strcmp("tftp", helper) != 0) {
/* Only support FTP/TFTP */
return NDIS_STATUS_NOT_SUPPORTED;
}
break;
case OVS_CT_ATTR_FORCE_COMMIT:
force = TRUE;
/* Force implicitly means commit */
commit = TRUE;
break;
case OVS_CT_ATTR_EVENTMASK:
eventmask = NlAttrGetU32(ctAttr);
/* Only mark and label updates are supported. */
if (eventmask & (1 << IPCT_MARK | 1 << IPCT_LABEL))
postUpdateEvent = TRUE;
break;
case OVS_CT_ATTR_NAT:
natActionInfo.natAction = NAT_ACTION_NONE;
/* Pares Nested NAT attributes. */
PNL_ATTR natAttr;
unsigned int natLeft;
BOOLEAN hasMinIp = FALSE;
BOOLEAN hasMinPort = FALSE;
BOOLEAN hasMaxIp = FALSE;
BOOLEAN hasMaxPort = FALSE;
NL_NESTED_FOR_EACH_UNSAFE (natAttr, natLeft, ctAttr) {
enum ovs_nat_attr subtype = NlAttrType(natAttr);
switch(subtype) {
case OVS_NAT_ATTR_SRC:
case OVS_NAT_ATTR_DST:
natActionInfo.natAction |=
((subtype == OVS_NAT_ATTR_SRC)
? NAT_ACTION_SRC : NAT_ACTION_DST);
break;
case OVS_NAT_ATTR_IP_MIN:
memcpy(&natActionInfo.minAddr,
NlAttrData(natAttr), NlAttrGetSize(natAttr));
hasMinIp = TRUE;
break;
case OVS_NAT_ATTR_IP_MAX:
memcpy(&natActionInfo.maxAddr,
NlAttrData(natAttr), NlAttrGetSize(natAttr));
hasMaxIp = TRUE;
break;
case OVS_NAT_ATTR_PROTO_MIN:
natActionInfo.minPort = NlAttrGetU16(natAttr);
hasMinPort = TRUE;
break;
case OVS_NAT_ATTR_PROTO_MAX:
natActionInfo.maxPort = NlAttrGetU16(natAttr);
hasMaxPort = TRUE;
break;
case OVS_NAT_ATTR_PERSISTENT:
case OVS_NAT_ATTR_PROTO_HASH:
case OVS_NAT_ATTR_PROTO_RANDOM:
break;
}
}
if (natActionInfo.natAction == NAT_ACTION_NONE) {
natActionInfo.natAction = NAT_ACTION_REVERSE;
}
if (hasMinIp && !hasMaxIp) {
memcpy(&natActionInfo.maxAddr,
&natActionInfo.minAddr,
sizeof(natActionInfo.maxAddr));
}
if (hasMinPort && !hasMaxPort) {
natActionInfo.maxPort = natActionInfo.minPort;
}
if (hasMinPort || hasMaxPort) {
if (natActionInfo.natAction & NAT_ACTION_SRC) {
natActionInfo.natAction |= NAT_ACTION_SRC_PORT;
} else if (natActionInfo.natAction & NAT_ACTION_DST) {
natActionInfo.natAction |= NAT_ACTION_DST_PORT;
}
}
break;
default:
OVS_LOG_TRACE("Invalid netlink attr type: %u", NlAttrType(ctAttr));
break;
}
}
/* If newNbl is not allocated, use the current Nbl*/
status = OvsCtExecute_(fwdCtx, key, layers,
commit, force, zone, mark, labels, helper, &natActionInfo,
postUpdateEvent);
return status;
}
/*
*----------------------------------------------------------------------------
* OvsConntrackEntryCleaner
* Runs periodically and cleans up the connection tracker
*----------------------------------------------------------------------------
*/
VOID
OvsConntrackEntryCleaner(PVOID data)
{
POVS_CT_THREAD_CTX context = (POVS_CT_THREAD_CTX)data;
PLIST_ENTRY link, next;
POVS_CT_ENTRY entry;
LOCK_STATE_EX lockState;
BOOLEAN success = TRUE;
while (success) {
if (context->exit) {
break;
}
/* Set the timeout for the thread and cleanup */
INT64 threadSleepTimeout = -CT_CLEANUP_INTERVAL;
if (ctTotalEntries) {
for (UINT32 i = 0; i < CT_HASH_TABLE_SIZE; i++) {
NdisAcquireRWLockWrite(ovsCtBucketLock[i], &lockState, 0);
LIST_FORALL_SAFE(&ovsConntrackTable[i], link, next) {
entry = CONTAINING_RECORD(link, OVS_CT_ENTRY, link);
OvsCtEntryDelete(entry, FALSE);
}
NdisReleaseRWLock(ovsCtBucketLock[i], &lockState);
}
}
KeWaitForSingleObject(&context->event, Executive, KernelMode,
FALSE, (LARGE_INTEGER *)&threadSleepTimeout);
}
PsTerminateSystemThread(STATUS_SUCCESS);
}
/*
*----------------------------------------------------------------------------
* OvsCtFlush
* Flushes out all Conntrack Entries that match any of the arguments
*----------------------------------------------------------------------------
*/
static __inline NDIS_STATUS
OvsCtFlush(UINT16 zone, struct ovs_key_ct_tuple_ipv4 *tuple)
{
PLIST_ENTRY link, next;
POVS_CT_ENTRY entry;
LOCK_STATE_EX lockState;
if (ctTotalEntries) {
for (UINT32 i = 0; i < CT_HASH_TABLE_SIZE; i++) {
LIST_FORALL_SAFE(&ovsConntrackTable[i], link, next) {
NdisAcquireRWLockWrite(ovsCtBucketLock[i], &lockState, 0);
entry = CONTAINING_RECORD(link, OVS_CT_ENTRY, link);
if (tuple) {
if (tuple->ipv4_proto != IPPROTO_ICMP &&
tuple->ipv4_src == entry->key.src.addr.ipv4_aligned &&
tuple->ipv4_dst == entry->key.dst.addr.ipv4_aligned &&
tuple->ipv4_proto == entry->key.nw_proto &&
tuple->src_port == entry->key.src.port &&
tuple->dst_port == entry->key.dst.port &&
(zone ? entry->key.zone == zone: TRUE)) {
OvsCtEntryDelete(entry, TRUE);
} else if (tuple->ipv4_src == entry->key.src.addr.ipv4_aligned &&
tuple->ipv4_dst == entry->key.dst.addr.ipv4_aligned &&
tuple->ipv4_proto == entry->key.nw_proto &&
tuple->src_port == entry->key.src.icmp_type &&
tuple->dst_port == entry->key.src.icmp_code &&
(zone ? entry->key.zone == zone: TRUE)) {
OvsCtEntryDelete(entry, TRUE);
}
} else if (!zone || zone == entry->key.zone) {
OvsCtEntryDelete(entry, TRUE);
}
NdisReleaseRWLock(ovsCtBucketLock[i], &lockState);
}
}
}
OvsNatFlush(zone);
return NDIS_STATUS_SUCCESS;
}
NTSTATUS
OvsCtDeleteCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
UINT32 *replyLen)
{
POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
PNL_MSG_HDR nlMsgHdr = &(msgIn->nlMsg);
PNL_ATTR ctAttrs[__CTA_MAX];
UINT32 attrOffset = NLMSG_HDRLEN + NF_GEN_MSG_HDRLEN + OVS_HDRLEN;
NL_ERROR nlError = NL_ERROR_SUCCESS;
NTSTATUS status;
UINT16 zone = 0;
struct ovs_key_ct_tuple_ipv4 *ct_tuple = NULL;
NL_BUFFER nlBuf;
UINT16 nlmsgType;
PNL_MSG_HDR nlMsg;
static const NL_POLICY ctAttrPolicy[] = {
[CTA_TUPLE_ORIG] = {.type = NL_A_NESTED, .optional = TRUE},
[CTA_ZONE] = {.type = NL_A_BE16, .optional = TRUE },
};
if ((NlAttrParse(nlMsgHdr, attrOffset, NlNfMsgAttrsLen(nlMsgHdr),
ctAttrPolicy, ARRAY_SIZE(ctAttrPolicy),
ctAttrs, ARRAY_SIZE(ctAttrs)))
!= TRUE) {
OVS_LOG_ERROR("Ct attr parsing failed for msg: %p", nlMsgHdr);
status = STATUS_INVALID_PARAMETER;
goto done;
}
if (ctAttrs[CTA_ZONE]) {
zone = ntohs(NlAttrGetU16(ctAttrs[CTA_ZONE]));
}
if (ctAttrs[CTA_TUPLE_ORIG]) {
ct_tuple = OvsAllocateMemoryWithTag(sizeof(struct ovs_key_ct_tuple_ipv4),
OVS_CT_POOL_TAG);
if (ct_tuple == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto done;
}
/* Parse ct tuple. */
status = MapNlToCtTuple(msgIn, ctAttrs[CTA_TUPLE_ORIG], ct_tuple);
if (status != STATUS_SUCCESS) {
goto done;
}
}
status = OvsCtFlush(zone, ct_tuple);
if (status == STATUS_SUCCESS) {
nlmsgType = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_DELETE);
NlBufInit(&nlBuf,
usrParamsCtx->outputBuffer,
usrParamsCtx->outputLength);
if (!NlFillOvsMsgForNfGenMsg(&nlBuf, nlmsgType, NLM_F_CREATE,
msgIn->nlMsg.nlmsgSeq,
msgIn->nlMsg.nlmsgPid,
AF_UNSPEC,
msgIn->nfGenMsg.version,
0)) {
status = STATUS_INVALID_PARAMETER;
}
nlMsg = (PNL_MSG_HDR)NlBufAt(&nlBuf, 0, 0);
nlMsg->nlmsgLen = NlBufSize(&nlBuf);
*replyLen = msgOut->nlMsg.nlmsgLen;
}
done:
if (ct_tuple) {
OvsFreeMemoryWithTag(ct_tuple, OVS_CT_POOL_TAG);
}
nlError = NlMapStatusToNlErr(status);
if (nlError != NL_ERROR_SUCCESS) {
POVS_MESSAGE_ERROR msgError = (POVS_MESSAGE_ERROR)
usrParamsCtx->outputBuffer;
ASSERT(msgError);
NlBuildErrorMsg(msgIn, msgError, nlError, replyLen);
ASSERT(*replyLen != 0);
status = STATUS_SUCCESS;
}
return status;
}
static __inline NDIS_STATUS
MapNlToCtTuple(POVS_MESSAGE msgIn, PNL_ATTR ctAttr,
struct ovs_key_ct_tuple_ipv4 *ct_tuple) {
PNL_MSG_HDR nlMsgHdr = &(msgIn->nlMsg);
PNL_ATTR ctTupleAttrs[__CTA_MAX];
UINT32 attrOffset;
static const NL_POLICY ctTuplePolicy[] = {
[CTA_TUPLE_IP] = { .type = NL_A_NESTED, .optional = FALSE },
[CTA_TUPLE_PROTO] = { .type = NL_A_NESTED, .optional = FALSE},
};
static const NL_POLICY ctTupleIpPolicy[] = {
[CTA_IP_V4_SRC] = { .type = NL_A_BE32, .optional = TRUE },
[CTA_IP_V4_DST] = { .type = NL_A_BE32, .optional = TRUE },
[CTA_IP_V6_SRC] = { .type = NL_A_BE32,.optional = TRUE },
[CTA_IP_V6_DST] = { .type = NL_A_BE32,.optional = TRUE },
};
static const NL_POLICY ctTupleProtoPolicy[] = {
[CTA_PROTO_NUM] = { .type = NL_A_U8, .optional = FALSE },
[CTA_PROTO_SRC_PORT] = { .type = NL_A_BE16, .optional = TRUE },
[CTA_PROTO_DST_PORT] = { .type = NL_A_BE16, .optional = TRUE },
[CTA_PROTO_ICMP_TYPE] = { .type = NL_A_U8, .optional = TRUE },
[CTA_PROTO_ICMP_CODE] = { .type = NL_A_U8, .optional = TRUE },
[CTA_PROTO_ICMPV6_ID] = { .type = NL_A_BE16,.optional = TRUE },
[CTA_PROTO_ICMPV6_TYPE] = { .type = NL_A_U8,.optional = TRUE },
[CTA_PROTO_ICMPV6_CODE] = { .type = NL_A_U8,.optional = TRUE },
};
if (!ctAttr) {
return STATUS_INVALID_PARAMETER;
}
attrOffset = (UINT32)((PCHAR) ctAttr - (PCHAR)nlMsgHdr);
if ((NlAttrParseNested(nlMsgHdr, attrOffset, NlAttrLen(ctAttr),
ctTuplePolicy, ARRAY_SIZE(ctTuplePolicy),
ctTupleAttrs, ARRAY_SIZE(ctTupleAttrs)))
!= TRUE) {
OVS_LOG_ERROR("CTA_TUPLE attr parsing failed for msg: %p", nlMsgHdr);
return STATUS_INVALID_PARAMETER;
}
if (ctTupleAttrs[CTA_TUPLE_IP]) {
PNL_ATTR ctTupleIpAttrs[__CTA_MAX];
attrOffset = (UINT32)((PCHAR) ctTupleAttrs[CTA_TUPLE_IP] - (PCHAR)nlMsgHdr);
if ((NlAttrParseNested(nlMsgHdr, attrOffset, NlAttrLen(ctTupleAttrs[CTA_TUPLE_IP]),
ctTupleIpPolicy, ARRAY_SIZE(ctTupleIpPolicy),
ctTupleIpAttrs, ARRAY_SIZE(ctTupleIpAttrs)))
!= TRUE) {
OVS_LOG_ERROR("CTA_TUPLE_IP attr parsing failed for msg: %p", nlMsgHdr);
return STATUS_INVALID_PARAMETER;
}
if (ctTupleIpAttrs[CTA_IP_V4_SRC] && ctTupleIpAttrs[CTA_IP_V4_DST]) {
ct_tuple->ipv4_src = NlAttrGetU32(ctTupleIpAttrs[CTA_IP_V4_SRC]);
ct_tuple->ipv4_dst = NlAttrGetU32(ctTupleIpAttrs[CTA_IP_V4_DST]);
}
}
if (ctTupleAttrs[CTA_TUPLE_PROTO]) {
PNL_ATTR ctTupleProtoAttrs[__CTA_MAX];
attrOffset = (UINT32)((PCHAR) ctTupleAttrs[CTA_TUPLE_PROTO] - (PCHAR)nlMsgHdr);
if ((NlAttrParseNested(nlMsgHdr, attrOffset, NlAttrLen(ctTupleAttrs[CTA_TUPLE_PROTO]),
ctTupleProtoPolicy, ARRAY_SIZE(ctTupleProtoPolicy),
ctTupleProtoAttrs, ARRAY_SIZE(ctTupleProtoAttrs)))
!= TRUE) {
OVS_LOG_ERROR("CTA_TUPLE_PROTO attr parsing failed for msg: %p", nlMsgHdr);
return STATUS_INVALID_PARAMETER;
}
if (ctTupleProtoAttrs[CTA_PROTO_NUM]) {
ct_tuple->ipv4_proto = NlAttrGetU8 (ctTupleProtoAttrs[CTA_PROTO_NUM]);
if (ctTupleProtoAttrs[CTA_PROTO_SRC_PORT] && ctTupleProtoAttrs[CTA_PROTO_DST_PORT]) {
ct_tuple->src_port = NlAttrGetU16(ctTupleProtoAttrs[CTA_PROTO_SRC_PORT]);
ct_tuple->dst_port = NlAttrGetU16(ctTupleProtoAttrs[CTA_PROTO_DST_PORT]);
} else if (ctTupleProtoAttrs[CTA_PROTO_ICMP_TYPE] &&
ctTupleProtoAttrs[CTA_PROTO_ICMP_CODE] ) {
ct_tuple->src_port = NlAttrGetU8(ctTupleProtoAttrs[CTA_PROTO_ICMP_TYPE]);
ct_tuple->dst_port = NlAttrGetU8(ctTupleProtoAttrs[CTA_PROTO_ICMP_CODE]);
} else if (ctTupleProtoAttrs[CTA_PROTO_ICMPV6_TYPE] &&
ctTupleProtoAttrs[CTA_PROTO_ICMPV6_CODE] ) {
ct_tuple->src_port = NlAttrGetU8(ctTupleProtoAttrs[CTA_PROTO_ICMPV6_TYPE]);
ct_tuple->dst_port = NlAttrGetU8(ctTupleProtoAttrs[CTA_PROTO_ICMPV6_CODE]);
}
}
}
return NDIS_STATUS_SUCCESS;
}
static __inline NDIS_STATUS
MapIpTupleToNl(PNL_BUFFER nlBuf, OVS_CT_KEY *key)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
UINT32 offset = 0;
offset = NlMsgStartNested(nlBuf, CTA_TUPLE_IP);
if (!offset) {
return NDIS_STATUS_FAILURE;
}
if (key->dl_type == ntohs(ETH_TYPE_IPV4)) {
if (!NlMsgPutTailU32(nlBuf, CTA_IP_V4_SRC, key->src.addr.ipv4)) {
status = NDIS_STATUS_FAILURE;
goto done;
}
if (!NlMsgPutTailU32(nlBuf, CTA_IP_V4_DST, key->dst.addr.ipv4)) {
status = NDIS_STATUS_FAILURE;
goto done;
}
} else if (key->dl_type == ntohs(ETH_TYPE_IPV6)) {
if (!NlMsgPutTailUnspec(nlBuf, CTA_IP_V6_SRC,
(PCHAR)(&key->src.addr.ipv6),
sizeof(key->src.addr.ipv6))) {
status = NDIS_STATUS_FAILURE;
goto done;
}
if (!NlMsgPutTailUnspec(nlBuf, CTA_IP_V6_DST,
(PCHAR)(&key->dst.addr.ipv6),
sizeof(key->dst.addr.ipv6))) {
status = NDIS_STATUS_FAILURE;
goto done;
}
}
done:
NlMsgEndNested(nlBuf, offset);
return status;
}
static __inline NDIS_STATUS
MapProtoTupleToNl(PNL_BUFFER nlBuf, OVS_CT_KEY *key)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
UINT32 offset = 0;
offset = NlMsgStartNested(nlBuf, CTA_TUPLE_PROTO);
if (!offset) {
return NDIS_STATUS_FAILURE;
}
if (!NlMsgPutTailU8(nlBuf, CTA_PROTO_NUM, key->nw_proto)) {
status = NDIS_STATUS_FAILURE;
goto done;
}
if (key->dl_type == ntohs(ETH_TYPE_IPV4)
|| key->dl_type == ntohs(ETH_TYPE_IPV6)) {
/* ICMP and ICMPv6 Type, Code and ID are currently not tracked */
if (key->nw_proto == IPPROTO_ICMP) {
if (!NlMsgPutTailU16(nlBuf, CTA_PROTO_ICMP_ID,
htons(key->src.icmp_id))) {
status = NDIS_STATUS_FAILURE;
goto done;
}
if (!NlMsgPutTailU8(nlBuf, CTA_PROTO_ICMP_TYPE,
key->src.icmp_type)) {
status = NDIS_STATUS_FAILURE;
goto done;
}
if (!NlMsgPutTailU8(nlBuf, CTA_PROTO_ICMP_CODE,
key->src.icmp_code)) {
status = NDIS_STATUS_FAILURE;
goto done;
}
} else if (key->nw_proto == IPPROTO_ICMPV6) {
if (!NlMsgPutTailU16(nlBuf, CTA_PROTO_ICMPV6_ID, htons(key->src.icmp_id))) {
status = NDIS_STATUS_FAILURE;
goto done;
}
if (!NlMsgPutTailU8(nlBuf, CTA_PROTO_ICMPV6_TYPE, key->src.icmp_type)) {
status = NDIS_STATUS_FAILURE;
goto done;
}
if (!NlMsgPutTailU8(nlBuf, CTA_PROTO_ICMPV6_CODE, key->src.icmp_code)) {
status = NDIS_STATUS_FAILURE;
goto done;
}
} else if (key->nw_proto == IPPROTO_TCP
|| key->nw_proto == IPPROTO_UDP) {
if (!NlMsgPutTailU16(nlBuf, CTA_PROTO_SRC_PORT,
key->src.port)) {
status = NDIS_STATUS_FAILURE;
goto done;
}
if (!NlMsgPutTailU16(nlBuf, CTA_PROTO_DST_PORT,
key->dst.port)) {
status = NDIS_STATUS_FAILURE;
goto done;
}
}
}
done:
NlMsgEndNested(nlBuf, offset);
return status;
}
static __inline NDIS_STATUS
MapCtKeyTupleToNl(PNL_BUFFER nlBuf,
UINT16 tupleType,
OVS_CT_KEY *key)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
UINT32 offset = 0;
offset = NlMsgStartNested(nlBuf, tupleType);
if (!offset) {
return NDIS_STATUS_FAILURE;
}
status = MapIpTupleToNl(nlBuf, key);
if (status != NDIS_STATUS_SUCCESS) {
goto done;
}
status = MapProtoTupleToNl(nlBuf, key);
if (status != NDIS_STATUS_SUCCESS) {
goto done;
}
done:
NlMsgEndNested(nlBuf, offset);
return status;
}
static __inline NDIS_STATUS
MapCtCounterToNl(PNL_BUFFER nlBuf,
UINT16 counterType,
OVS_CT_KEY *key)
{
NDIS_STATUS status = NDIS_STATUS_SUCCESS;
UINT32 offset = 0;
offset = NlMsgStartNested(nlBuf, counterType);
if (!offset) {
return NDIS_STATUS_FAILURE;
}
if (!NlMsgPutTailU64(nlBuf, CTA_COUNTERS_PACKETS,
htonll(key->packetCount))) {
status = NDIS_STATUS_FAILURE;
goto done;
}
if (!NlMsgPutTailU64(nlBuf, CTA_COUNTERS_BYTES,
htonll(key->byteCount))) {
status = NDIS_STATUS_FAILURE;
goto done;
}
done:
NlMsgEndNested(nlBuf, offset);
return status;
}
/* Userspace expects system time to be Unix timestamp in Nano Seconds */
static __inline unsigned
WindowsTickToUnixSeconds(long long windowsTicks)
{
/*
* Windows epoch starts 1601-01-01T00:00:00Z. It's 11644473600 seconds
* before the UNIX/Linux epoch (1970-01-01T00:00:00Z). Windows ticks are
* in 100 nanoseconds
*/
return (unsigned)((windowsTicks / WINDOWS_TICK
- SEC_TO_UNIX_EPOCH));
}
NTSTATUS
OvsCreateNlMsgFromCtEntry(POVS_CT_ENTRY entry,
PVOID outBuffer,
UINT32 outBufLen,
UINT8 eventType,
UINT32 nlmsgSeq,
UINT32 nlmsgPid,
UINT8 nfGenVersion,
UINT32 dpIfIndex)
{
NL_BUFFER nlBuf;
BOOLEAN ok;
PNL_MSG_HDR nlMsg;
UINT32 timeout;
NDIS_STATUS status;
UINT64 currentTime, expiration;
UINT16 nlmsgType;
UINT16 nlmsgFlags = NLM_F_CREATE;
NdisGetCurrentSystemTime((LARGE_INTEGER *)¤tTime);
UINT8 nfgenFamily = 0;
if (entry->key.dl_type == htons(ETH_TYPE_IPV4)) {
nfgenFamily = AF_INET;
} else if (entry->key.dl_type == htons(ETH_TYPE_IPV6)) {
nfgenFamily = AF_INET6;
}
NlBufInit(&nlBuf, outBuffer, outBufLen);
/* Mimic netfilter */
if (eventType == OVS_EVENT_CT_NEW || eventType == OVS_EVENT_CT_UPDATE) {
nlmsgType = (UINT16) (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
} else if (eventType == OVS_EVENT_CT_DELETE) {
nlmsgType = (UINT16) (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_DELETE);
} else {
return STATUS_INVALID_PARAMETER;
}
if (eventType == OVS_EVENT_CT_UPDATE) {
/* In netlink-conntrack.c IPCTNL_MSG_CT_NEW msg type is used to
* differentiate between OVS_EVENT_CT_NEW and OVS_EVENT_CT_UPDATE
* events based on nlmsgFlags, unset it to notify an update event.
*/
nlmsgFlags = 0;
}
ok = NlFillOvsMsgForNfGenMsg(&nlBuf, nlmsgType, nlmsgFlags,
nlmsgSeq, nlmsgPid, nfgenFamily,
nfGenVersion, dpIfIndex);
if (!ok) {
return STATUS_INVALID_BUFFER_SIZE;
}
status = MapCtKeyTupleToNl(&nlBuf, CTA_TUPLE_ORIG, &entry->key);
if (status != NDIS_STATUS_SUCCESS) {
return STATUS_UNSUCCESSFUL;
}
status = MapCtKeyTupleToNl(&nlBuf, CTA_TUPLE_REPLY, &entry->rev_key);
if (status != NDIS_STATUS_SUCCESS) {
return STATUS_UNSUCCESSFUL;
}
status = MapCtCounterToNl(&nlBuf, CTA_COUNTERS_ORIG, &entry->key);
if (status != NDIS_STATUS_SUCCESS) {
return STATUS_UNSUCCESSFUL;
}
status = MapCtCounterToNl(&nlBuf, CTA_COUNTERS_REPLY, &entry->rev_key);
if (status != NDIS_STATUS_SUCCESS) {
return STATUS_UNSUCCESSFUL;
}
if (entry->key.zone) {
if (!NlMsgPutTailU16(&nlBuf, CTA_ZONE, htons(entry->key.zone))) {
return STATUS_INVALID_BUFFER_SIZE;
}
}
if (entry->mark) {
if (!NlMsgPutTailU32(&nlBuf, CTA_MARK, htonl(entry->mark))) {
return STATUS_INVALID_BUFFER_SIZE;
}
}
if (entry->labels.ct_labels) {
ok = NlMsgPutTailUnspec(&nlBuf, CTA_LABELS,
(PCHAR)(&entry->labels),
sizeof(entry->labels));
if (!ok) {
return STATUS_INVALID_BUFFER_SIZE;
}
}
if (entry->expiration > currentTime) {
expiration = entry->expiration - currentTime;
timeout = (UINT32) (expiration / CT_INTERVAL_SEC);
if (!NlMsgPutTailU32(&nlBuf, CTA_TIMEOUT, htonl(timeout))) {
return STATUS_INVALID_BUFFER_SIZE;
}
}
if (entry->key.nw_proto == IPPROTO_TCP) {
/* Add ProtoInfo for TCP */
UINT32 offset;
offset = NlMsgStartNested(&nlBuf, CTA_PROTOINFO);
if (!offset) {
return NDIS_STATUS_FAILURE;
}
status = OvsCtMapTcpProtoInfoToNl(&nlBuf, entry);
NlMsgEndNested(&nlBuf, offset);
if (status != NDIS_STATUS_SUCCESS) {
return STATUS_UNSUCCESSFUL;
}
}
if (entry->helper_name) {
UINT32 offset;
offset = NlMsgStartNested(&nlBuf, CTA_HELP);
if (!offset) {
return NDIS_STATUS_FAILURE;
}
if (!NlMsgPutTailString(&nlBuf, CTA_HELP_NAME, entry->helper_name)) {
return STATUS_INVALID_BUFFER_SIZE;
}
NlMsgEndNested(&nlBuf, offset);
}
if (entry->parent) {
status = MapCtKeyTupleToNl(&nlBuf, CTA_TUPLE_MASTER,
&((POVS_CT_ENTRY)entry->parent)->key);
if (status != NDIS_STATUS_SUCCESS) {
return STATUS_UNSUCCESSFUL;
}
}
/* CTA_STATUS is required but not implemented. Default to 0 */
if (!NlMsgPutTailU32(&nlBuf, CTA_STATUS, 0)) {
return STATUS_INVALID_BUFFER_SIZE;
}
/* Mimic netfilter - nf_conntrack_netlink.c:
*
* int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) {
* NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
* return 0;
* }
*
*/
if(!NlMsgPutTailU32(&nlBuf, CTA_ID, htonl((UINT32) entry))) {
return STATUS_INVALID_BUFFER_SIZE;
}
if (entry->timestampStart) {
UINT32 offset;
offset = NlMsgStartNested(&nlBuf, CTA_TIMESTAMP);
if (!offset) {
return NDIS_STATUS_FAILURE;
}
UINT64 start;
start = WindowsTickToUnixSeconds(entry->timestampStart);
start = start * SEC_TO_NANOSEC;
if (!NlMsgPutTailU64(&nlBuf, CTA_TIMESTAMP_START, htonll(start))) {
NlMsgEndNested(&nlBuf, offset);
return STATUS_INVALID_BUFFER_SIZE;
}
NlMsgEndNested(&nlBuf, offset);
}
nlMsg = (PNL_MSG_HDR)NlBufAt(&nlBuf, 0, 0);
nlMsg->nlmsgLen = NlBufSize(&nlBuf);
return STATUS_SUCCESS;
}
/*
*----------------------------------------------------------------------------
* OvsCtDumpCmdHandler --
* Handler for IPCTNL_MSG_CT_GET command.
*
* XXX - Try to consolidate dump handler patterns around dumpState usage
* The following dumpHandler is similar to one vport.c uses
*----------------------------------------------------------------------------
*/
NTSTATUS
OvsCtDumpCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
UINT32 *replyLen)
{
NTSTATUS rc;
/* Setup Dump Start if it's OVS_WRITE_DEV_OP and return */
if (usrParamsCtx->devOp == OVS_WRITE_DEV_OP) {
*replyLen = 0;
OvsSetupDumpStart(usrParamsCtx);
return STATUS_SUCCESS;
}
POVS_OPEN_INSTANCE instance =
(POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
POVS_MESSAGE msgIn;
ASSERT(usrParamsCtx->devOp == OVS_READ_DEV_OP);
if (instance->dumpState.ovsMsg == NULL) {
ASSERT(FALSE);
return STATUS_INVALID_DEVICE_STATE;
}
/* Output buffer has been validated while validating read dev op. */
ASSERT(usrParamsCtx->outputBuffer != NULL);
msgIn = instance->dumpState.ovsMsg;
UINT32 inBucket = instance->dumpState.index[0];
UINT32 inIndex = instance->dumpState.index[1];
UINT32 i = CT_HASH_TABLE_SIZE;
UINT32 outIndex = 0;
KIRQL irql = KeGetCurrentIrql();
LOCK_STATE_EX lockStateTable;
if (ctTotalEntries) {
for (i = inBucket; i < CT_HASH_TABLE_SIZE; i++) {
PLIST_ENTRY head, link;
NdisAcquireRWLockRead(ovsCtBucketLock[i], &lockStateTable, 0);
head = &ovsConntrackTable[i];
POVS_CT_ENTRY entry = NULL;
outIndex = 0;
LIST_FORALL(head, link) {
/*
* if one or more dumps were previously done on this same
* bucket, inIndex will be > 0, so we'll need to reply with
* the inIndex + 1 ct-entry from the bucket.
*/
if (outIndex >= inIndex) {
entry = CONTAINING_RECORD(link, OVS_CT_ENTRY, link);
OVS_ACQUIRE_SPIN_LOCK(&(entry->lock), irql);
rc = OvsCreateNlMsgFromCtEntry(entry,
usrParamsCtx->outputBuffer,
usrParamsCtx->outputLength,
OVS_EVENT_CT_NEW,
msgIn->nlMsg.nlmsgSeq,
msgIn->nlMsg.nlmsgPid,
msgIn->nfGenMsg.version,
0);
OVS_RELEASE_SPIN_LOCK(&(entry->lock), irql);
if (rc != NDIS_STATUS_SUCCESS) {
NdisReleaseRWLock(ovsCtBucketLock[i], &lockStateTable);
return STATUS_UNSUCCESSFUL;
}
++outIndex;
break;
}
++outIndex;
}
NdisReleaseRWLock(ovsCtBucketLock[i], &lockStateTable);
if (entry) {
break;
}
/*
* if no ct-entry was found above, check the next bucket, beginning
* with the first (i.e. index 0) elem from within that bucket
*/
inIndex = 0;
}
}
instance->dumpState.index[0] = i;
instance->dumpState.index[1] = outIndex;
/* if i < CT_HASH_TABLE_SIZE => entry was found */
if (i < CT_HASH_TABLE_SIZE) {
POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
*replyLen = msgOut->nlMsg.nlmsgLen;
} else {
/* if i >= CT_HASH_TABLE_SIZE => entry was not found => dump done */
*replyLen = 0;
FreeUserDumpState(instance);
}
return STATUS_SUCCESS;
}
static NTSTATUS
OvsCreateNlMsgFromCtLimit(POVS_MESSAGE msgIn,
PVOID outBuffer,
UINT32 outBufLen,
PCHAR attr,
UINT32 numAttrs,
int dpIfIndex)
{
NTSTATUS status = STATUS_SUCCESS;
NL_BUFFER nlBuffer;
PNL_MSG_HDR nlMsg;
PGENL_MSG_HDR genlMsgHdr = &(msgIn->genlMsg);
NlBufInit(&nlBuffer, outBuffer, outBufLen);
if (!NlFillOvsMsg(&nlBuffer, msgIn->nlMsg.nlmsgType, NLM_F_MULTI,
msgIn->nlMsg.nlmsgSeq, msgIn->nlMsg.nlmsgPid,
msgIn->genlMsg.cmd, msgIn->genlMsg.version,
dpIfIndex)) {
return STATUS_INVALID_BUFFER_SIZE;
}
if (genlMsgHdr->cmd == OVS_CT_LIMIT_CMD_GET && numAttrs) {
POVS_CT_ZONE_LIMIT zoneLimitAttr = (POVS_CT_ZONE_LIMIT) attr;
UINT32 offset = NlMsgStartNested(&nlBuffer, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
if (!offset) {
/* Starting the nested attribute failed. */
status = STATUS_INVALID_BUFFER_SIZE;
goto done;
}
/* Insert OVS_CT_ZONE_LIMIT attributes.*/
for (UINT32 i = 0; i < numAttrs; i++) {
if (zoneLimitAttr) {
zoneLimitAttr->limit = zoneInfo[zoneLimitAttr->zone_id].limit;
zoneLimitAttr->count = zoneInfo[zoneLimitAttr->zone_id].entries;
if (zoneLimitAttr->zone_id == -1) {
zoneLimitAttr->limit = defaultCtLimit;
}
NlMsgPutTail(&nlBuffer, (const PCHAR)zoneLimitAttr,
sizeof(OVS_CT_ZONE_LIMIT));
} else {
status = STATUS_INVALID_PARAMETER;
break;
}
zoneLimitAttr = (POVS_CT_ZONE_LIMIT)((PCHAR) zoneLimitAttr +
sizeof(OVS_CT_ZONE_LIMIT));
}
NlMsgEndNested(&nlBuffer, offset);
}
done:
nlMsg = (PNL_MSG_HDR)NlBufAt(&nlBuffer, 0, 0);
nlMsg->nlmsgLen = NlBufSize(&nlBuffer);
return status;
}
NTSTATUS
OvsCtLimitHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
UINT32 *replyLen)
{
NTSTATUS status;
POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
PNL_MSG_HDR nlMsgHdr = &(msgIn->nlMsg);
PGENL_MSG_HDR genlMsgHdr = &(msgIn->genlMsg);
POVS_HDR ovsHdr = &(msgIn->ovsHdr);
PCHAR attr = NULL;
UINT32 numAttrs = 0;
UINT32 attrOffset = NLMSG_HDRLEN + GENL_HDRLEN + OVS_HDRLEN;
static const NL_POLICY ovsCtLimitPolicy[] = {
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NL_A_NESTED, .optional = TRUE }
};
PNL_ATTR nlAttrs[ARRAY_SIZE(ovsCtLimitPolicy)];
if ((NlAttrParse(nlMsgHdr, attrOffset, NlMsgAttrsLen(nlMsgHdr),
ovsCtLimitPolicy, ARRAY_SIZE(ovsCtLimitPolicy),
nlAttrs, ARRAY_SIZE(nlAttrs)))
!= TRUE) {
OVS_LOG_ERROR("Attr Parsing failed for msg: %p", nlMsgHdr);
return STATUS_INVALID_PARAMETER;
}
if (nlAttrs[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
numAttrs = NlAttrGetSize(nlAttrs[OVS_CT_LIMIT_ATTR_ZONE_LIMIT])/sizeof(OVS_CT_ZONE_LIMIT);
attr = NlAttrGet(nlAttrs[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]);
}
if (genlMsgHdr->cmd == OVS_CT_LIMIT_CMD_SET ||
genlMsgHdr->cmd == OVS_CT_LIMIT_CMD_DEL) {
POVS_CT_ZONE_LIMIT zoneLimitAttr = (POVS_CT_ZONE_LIMIT)attr;
for (UINT32 i = 0; i < numAttrs; i++) {
/* Parse zone limit attributes. */
if (zoneLimitAttr) {
if (genlMsgHdr->cmd == OVS_CT_LIMIT_CMD_DEL) {
zoneLimitAttr->limit = CT_MAX_ENTRIES;
}
OvsCtSetZoneLimit(zoneLimitAttr->zone_id, zoneLimitAttr->limit);
} else {
OVS_LOG_ERROR("Failed to get zone limit attribute at index(%u),"
" numAttrs(%u)", i, numAttrs);
return STATUS_INVALID_PARAMETER;
}
zoneLimitAttr = (POVS_CT_ZONE_LIMIT)((PCHAR) zoneLimitAttr +
sizeof(OVS_CT_ZONE_LIMIT));
}
}
/* Output buffer has been validated while validating transact dev op. */
ASSERT(msgOut != NULL && usrParamsCtx->outputLength >= sizeof *msgOut);
status = OvsCreateNlMsgFromCtLimit(msgIn, msgOut,
usrParamsCtx->outputLength,
attr, numAttrs, ovsHdr->dp_ifindex);
*replyLen = msgOut->nlMsg.nlmsgLen;
return status;
}
#pragma warning(pop)
|