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
|
/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
* Patrick Schaaf <bof@bof.de>
* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
/* Kernel module for IP set management */
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
#include <linux/config.h>
#endif
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kmod.h>
#include <linux/ip.h>
#include <linux/skbuff.h>
#include <linux/random.h>
#include <linux/netfilter_ipv4/ip_set_jhash.h>
#include <linux/errno.h>
#include <linux/capability.h>
#include <asm/uaccess.h>
#include <asm/bitops.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
#include <asm/semaphore.h>
#else
#include <linux/semaphore.h>
#endif
#include <linux/spinlock.h>
#define ASSERT_READ_LOCK(x)
#define ASSERT_WRITE_LOCK(x)
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4/ip_set.h>
static struct list_head set_type_list; /* all registered sets */
static struct ip_set **ip_set_list; /* all individual sets */
static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
static struct list_head *ip_set_hash; /* hash of bindings */
static unsigned int ip_set_hash_random; /* random seed */
#define SETNAME_EQ(a,b) (strncmp(a,b,IP_SET_MAXNAMELEN) == 0)
/*
* Sets are identified either by the index in ip_set_list or by id.
* The id never changes and is used to find a key in the hash.
* The index may change by swapping and used at all other places
* (set/SET netfilter modules, binding value, etc.)
*
* Userspace requests are serialized by ip_set_mutex and sets can
* be deleted only from userspace. Therefore ip_set_list locking
* must obey the following rules:
*
* - kernel requests: read and write locking mandatory
* - user requests: read locking optional, write locking mandatory
*/
static inline void
__ip_set_get(ip_set_id_t index)
{
atomic_inc(&ip_set_list[index]->ref);
}
static inline void
__ip_set_put(ip_set_id_t index)
{
atomic_dec(&ip_set_list[index]->ref);
}
/*
* Binding routines
*/
static inline struct ip_set_hash *
__ip_set_find(u_int32_t key, ip_set_id_t id, ip_set_ip_t ip)
{
struct ip_set_hash *set_hash;
list_for_each_entry(set_hash, &ip_set_hash[key], list)
if (set_hash->id == id && set_hash->ip == ip)
return set_hash;
return NULL;
}
static ip_set_id_t
ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
{
u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
% ip_set_bindings_hash_size;
struct ip_set_hash *set_hash;
ASSERT_READ_LOCK(&ip_set_lock);
IP_SET_ASSERT(ip_set_list[id]);
DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
set_hash = __ip_set_find(key, id, ip);
DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
HIPQUAD(ip),
set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
}
static inline void
__set_hash_del(struct ip_set_hash *set_hash)
{
ASSERT_WRITE_LOCK(&ip_set_lock);
IP_SET_ASSERT(ip_set_list[set_hash->binding]);
__ip_set_put(set_hash->binding);
list_del(&set_hash->list);
kfree(set_hash);
}
static int
ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
{
u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
% ip_set_bindings_hash_size;
struct ip_set_hash *set_hash;
IP_SET_ASSERT(ip_set_list[id]);
DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
write_lock_bh(&ip_set_lock);
set_hash = __ip_set_find(key, id, ip);
DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
HIPQUAD(ip),
set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
if (set_hash != NULL)
__set_hash_del(set_hash);
write_unlock_bh(&ip_set_lock);
return 0;
}
static int
ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
{
u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
% ip_set_bindings_hash_size;
struct ip_set_hash *set_hash;
int ret = 0;
IP_SET_ASSERT(ip_set_list[id]);
IP_SET_ASSERT(ip_set_list[binding]);
DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
HIPQUAD(ip), ip_set_list[binding]->name);
write_lock_bh(&ip_set_lock);
set_hash = __ip_set_find(key, id, ip);
if (!set_hash) {
set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_ATOMIC);
if (!set_hash) {
ret = -ENOMEM;
goto unlock;
}
INIT_LIST_HEAD(&set_hash->list);
set_hash->id = id;
set_hash->ip = ip;
list_add(&set_hash->list, &ip_set_hash[key]);
} else {
IP_SET_ASSERT(ip_set_list[set_hash->binding]);
DP("overwrite binding: %s",
ip_set_list[set_hash->binding]->name);
__ip_set_put(set_hash->binding);
}
set_hash->binding = binding;
__ip_set_get(set_hash->binding);
DP("stored: key %u, id %u (%s), ip %u.%u.%u.%u, binding %u (%s)",
key, id, ip_set_list[id]->name,
HIPQUAD(ip), binding, ip_set_list[binding]->name);
unlock:
write_unlock_bh(&ip_set_lock);
return ret;
}
#define FOREACH_HASH_DO(fn, args...) \
({ \
ip_set_id_t __key; \
struct ip_set_hash *__set_hash; \
\
for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
fn(__set_hash , ## args); \
} \
})
#define FOREACH_HASH_RW_DO(fn, args...) \
({ \
ip_set_id_t __key; \
struct ip_set_hash *__set_hash, *__n; \
\
ASSERT_WRITE_LOCK(&ip_set_lock); \
for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
fn(__set_hash , ## args); \
} \
})
/* Add, del and test set entries from kernel */
#define follow_bindings(index, set, ip) \
((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
|| (index = (set)->binding) != IP_SET_INVALID_ID)
int
ip_set_testip_kernel(ip_set_id_t index,
const struct sk_buff *skb,
const u_int32_t *flags)
{
struct ip_set *set;
ip_set_ip_t ip;
int res;
unsigned char i = 0;
IP_SET_ASSERT(flags[i]);
read_lock_bh(&ip_set_lock);
do {
set = ip_set_list[index];
IP_SET_ASSERT(set);
DP("set %s, index %u", set->name, index);
read_lock_bh(&set->lock);
res = set->type->testip_kernel(set, skb, &ip, flags, i++);
read_unlock_bh(&set->lock);
i += !!(set->type->features & IPSET_DATA_DOUBLE);
} while (res > 0
&& flags[i]
&& follow_bindings(index, set, ip));
read_unlock_bh(&ip_set_lock);
return (res < 0 ? 0 : res);
}
int
ip_set_addip_kernel(ip_set_id_t index,
const struct sk_buff *skb,
const u_int32_t *flags)
{
struct ip_set *set;
ip_set_ip_t ip;
int res;
unsigned char i = 0;
IP_SET_ASSERT(flags[i]);
retry:
read_lock_bh(&ip_set_lock);
do {
set = ip_set_list[index];
IP_SET_ASSERT(set);
DP("set %s, index %u", set->name, index);
write_lock_bh(&set->lock);
res = set->type->addip_kernel(set, skb, &ip, flags, i++);
write_unlock_bh(&set->lock);
i += !!(set->type->features & IPSET_DATA_DOUBLE);
} while ((res == 0 || res == -EEXIST)
&& flags[i]
&& follow_bindings(index, set, ip));
read_unlock_bh(&ip_set_lock);
if (res == -EAGAIN
&& set->type->retry
&& (res = set->type->retry(set)) == 0)
goto retry;
return res;
}
int
ip_set_delip_kernel(ip_set_id_t index,
const struct sk_buff *skb,
const u_int32_t *flags)
{
struct ip_set *set;
ip_set_ip_t ip;
int res;
unsigned char i = 0;
IP_SET_ASSERT(flags[i]);
read_lock_bh(&ip_set_lock);
do {
set = ip_set_list[index];
IP_SET_ASSERT(set);
DP("set %s, index %u", set->name, index);
write_lock_bh(&set->lock);
res = set->type->delip_kernel(set, skb, &ip, flags, i++);
write_unlock_bh(&set->lock);
i += !!(set->type->features & IPSET_DATA_DOUBLE);
} while ((res == 0 || res == -EEXIST)
&& flags[i]
&& follow_bindings(index, set, ip));
read_unlock_bh(&ip_set_lock);
return res;
}
/* Register and deregister settype */
static inline struct ip_set_type *
find_set_type(const char *name)
{
struct ip_set_type *set_type;
list_for_each_entry(set_type, &set_type_list, list)
if (!strncmp(set_type->typename, name, IP_SET_MAXNAMELEN - 1))
return set_type;
return NULL;
}
int
ip_set_register_set_type(struct ip_set_type *set_type)
{
int ret = 0;
if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
set_type->typename,
set_type->protocol_version,
IP_SET_PROTOCOL_VERSION);
return -EINVAL;
}
write_lock_bh(&ip_set_lock);
if (find_set_type(set_type->typename)) {
/* Duplicate! */
ip_set_printk("'%s' already registered!",
set_type->typename);
ret = -EINVAL;
goto unlock;
}
if (!try_module_get(THIS_MODULE)) {
ret = -EFAULT;
goto unlock;
}
list_add(&set_type->list, &set_type_list);
DP("'%s' registered.", set_type->typename);
unlock:
write_unlock_bh(&ip_set_lock);
return ret;
}
void
ip_set_unregister_set_type(struct ip_set_type *set_type)
{
write_lock_bh(&ip_set_lock);
if (!find_set_type(set_type->typename)) {
ip_set_printk("'%s' not registered?",
set_type->typename);
goto unlock;
}
list_del(&set_type->list);
module_put(THIS_MODULE);
DP("'%s' unregistered.", set_type->typename);
unlock:
write_unlock_bh(&ip_set_lock);
}
ip_set_id_t
__ip_set_get_byname(const char *name, struct ip_set **set)
{
ip_set_id_t i, index = IP_SET_INVALID_ID;
for (i = 0; i < ip_set_max; i++) {
if (ip_set_list[i] != NULL
&& SETNAME_EQ(ip_set_list[i]->name, name)) {
__ip_set_get(i);
index = i;
*set = ip_set_list[i];
break;
}
}
return index;
}
void __ip_set_put_byindex(ip_set_id_t index)
{
if (ip_set_list[index])
__ip_set_put(index);
}
/*
* Userspace routines
*/
/*
* Find set by name, reference it once. The reference makes sure the
* thing pointed to, does not go away under our feet. Drop the reference
* later, using ip_set_put().
*/
ip_set_id_t
ip_set_get_byname(const char *name)
{
ip_set_id_t i, index = IP_SET_INVALID_ID;
down(&ip_set_app_mutex);
for (i = 0; i < ip_set_max; i++) {
if (ip_set_list[i] != NULL
&& SETNAME_EQ(ip_set_list[i]->name, name)) {
__ip_set_get(i);
index = i;
break;
}
}
up(&ip_set_app_mutex);
return index;
}
/*
* Find set by index, reference it once. The reference makes sure the
* thing pointed to, does not go away under our feet. Drop the reference
* later, using ip_set_put().
*/
ip_set_id_t
ip_set_get_byindex(ip_set_id_t index)
{
down(&ip_set_app_mutex);
if (index >= ip_set_max)
return IP_SET_INVALID_ID;
if (ip_set_list[index])
__ip_set_get(index);
else
index = IP_SET_INVALID_ID;
up(&ip_set_app_mutex);
return index;
}
/*
* Find the set id belonging to the index.
* We are protected by the mutex, so we do not need to use
* ip_set_lock. There is no need to reference the sets either.
*/
ip_set_id_t
ip_set_id(ip_set_id_t index)
{
if (index >= ip_set_max || !ip_set_list[index])
return IP_SET_INVALID_ID;
return ip_set_list[index]->id;
}
/*
* If the given set pointer points to a valid set, decrement
* reference count by 1. The caller shall not assume the index
* to be valid, after calling this function.
*/
void ip_set_put_byindex(ip_set_id_t index)
{
down(&ip_set_app_mutex);
if (ip_set_list[index])
__ip_set_put(index);
up(&ip_set_app_mutex);
}
/* Find a set by name or index */
static ip_set_id_t
ip_set_find_byname(const char *name)
{
ip_set_id_t i, index = IP_SET_INVALID_ID;
for (i = 0; i < ip_set_max; i++) {
if (ip_set_list[i] != NULL
&& SETNAME_EQ(ip_set_list[i]->name, name)) {
index = i;
break;
}
}
return index;
}
static ip_set_id_t
ip_set_find_byindex(ip_set_id_t index)
{
if (index >= ip_set_max || ip_set_list[index] == NULL)
index = IP_SET_INVALID_ID;
return index;
}
/*
* Add, del, test, bind and unbind
*/
static inline int
__ip_set_testip(struct ip_set *set,
const void *data,
size_t size,
ip_set_ip_t *ip)
{
int res;
read_lock_bh(&set->lock);
res = set->type->testip(set, data, size, ip);
read_unlock_bh(&set->lock);
return res;
}
static int
__ip_set_addip(ip_set_id_t index,
const void *data,
size_t size)
{
struct ip_set *set = ip_set_list[index];
ip_set_ip_t ip;
int res;
IP_SET_ASSERT(set);
do {
write_lock_bh(&set->lock);
res = set->type->addip(set, data, size, &ip);
write_unlock_bh(&set->lock);
} while (res == -EAGAIN
&& set->type->retry
&& (res = set->type->retry(set)) == 0);
return res;
}
static int
ip_set_addip(ip_set_id_t index,
const void *data,
size_t size)
{
struct ip_set *set = ip_set_list[index];
IP_SET_ASSERT(set);
if (size - sizeof(struct ip_set_req_adt) != set->type->reqsize) {
ip_set_printk("data length wrong (want %zu, have %zu)",
set->type->reqsize,
size - sizeof(struct ip_set_req_adt));
return -EINVAL;
}
return __ip_set_addip(index,
data + sizeof(struct ip_set_req_adt),
size - sizeof(struct ip_set_req_adt));
}
static int
ip_set_delip(ip_set_id_t index,
const void *data,
size_t size)
{
struct ip_set *set = ip_set_list[index];
ip_set_ip_t ip;
int res;
IP_SET_ASSERT(set);
if (size - sizeof(struct ip_set_req_adt) != set->type->reqsize) {
ip_set_printk("data length wrong (want %zu, have %zu)",
set->type->reqsize,
size - sizeof(struct ip_set_req_adt));
return -EINVAL;
}
write_lock_bh(&set->lock);
res = set->type->delip(set,
data + sizeof(struct ip_set_req_adt),
size - sizeof(struct ip_set_req_adt),
&ip);
write_unlock_bh(&set->lock);
return res;
}
static int
ip_set_testip(ip_set_id_t index,
const void *data,
size_t size)
{
struct ip_set *set = ip_set_list[index];
ip_set_ip_t ip;
int res;
IP_SET_ASSERT(set);
if (size - sizeof(struct ip_set_req_adt) != set->type->reqsize) {
ip_set_printk("data length wrong (want %zu, have %zu)",
set->type->reqsize,
size - sizeof(struct ip_set_req_adt));
return -EINVAL;
}
res = __ip_set_testip(set,
data + sizeof(struct ip_set_req_adt),
size - sizeof(struct ip_set_req_adt),
&ip);
return (res > 0 ? -EEXIST : res);
}
static int
ip_set_bindip(ip_set_id_t index,
const void *data,
size_t size)
{
struct ip_set *set = ip_set_list[index];
const struct ip_set_req_bind *req_bind;
ip_set_id_t binding;
ip_set_ip_t ip;
int res;
IP_SET_ASSERT(set);
if (size < sizeof(struct ip_set_req_bind))
return -EINVAL;
req_bind = data;
if (SETNAME_EQ(req_bind->binding, IPSET_TOKEN_DEFAULT)) {
/* Default binding of a set */
const char *binding_name;
if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
return -EINVAL;
binding_name = data + sizeof(struct ip_set_req_bind);
binding = ip_set_find_byname(binding_name);
if (binding == IP_SET_INVALID_ID)
return -ENOENT;
write_lock_bh(&ip_set_lock);
/* Sets as binding values are referenced */
if (set->binding != IP_SET_INVALID_ID)
__ip_set_put(set->binding);
set->binding = binding;
__ip_set_get(set->binding);
write_unlock_bh(&ip_set_lock);
return 0;
}
binding = ip_set_find_byname(req_bind->binding);
if (binding == IP_SET_INVALID_ID)
return -ENOENT;
res = __ip_set_testip(set,
data + sizeof(struct ip_set_req_bind),
size - sizeof(struct ip_set_req_bind),
&ip);
DP("set %s, ip: %u.%u.%u.%u, binding %s",
set->name, HIPQUAD(ip), ip_set_list[binding]->name);
if (res >= 0)
res = ip_set_hash_add(set->id, ip, binding);
return res;
}
#define FOREACH_SET_DO(fn, args...) \
({ \
ip_set_id_t __i; \
struct ip_set *__set; \
\
for (__i = 0; __i < ip_set_max; __i++) { \
__set = ip_set_list[__i]; \
if (__set != NULL) \
fn(__set , ##args); \
} \
})
static inline void
__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
{
if (set_hash->id == id)
__set_hash_del(set_hash);
}
static inline void
__unbind_default(struct ip_set *set)
{
if (set->binding != IP_SET_INVALID_ID) {
/* Sets as binding values are referenced */
__ip_set_put(set->binding);
set->binding = IP_SET_INVALID_ID;
}
}
static int
ip_set_unbindip(ip_set_id_t index,
const void *data,
size_t size)
{
struct ip_set *set;
const struct ip_set_req_bind *req_bind;
ip_set_ip_t ip;
int res;
DP("");
if (size < sizeof(struct ip_set_req_bind))
return -EINVAL;
req_bind = data;
DP("%u %s", index, req_bind->binding);
if (index == IP_SET_INVALID_ID) {
/* unbind :all: */
if (SETNAME_EQ(req_bind->binding, IPSET_TOKEN_DEFAULT)) {
/* Default binding of sets */
write_lock_bh(&ip_set_lock);
FOREACH_SET_DO(__unbind_default);
write_unlock_bh(&ip_set_lock);
return 0;
} else if (SETNAME_EQ(req_bind->binding, IPSET_TOKEN_ALL)) {
/* Flush all bindings of all sets*/
write_lock_bh(&ip_set_lock);
FOREACH_HASH_RW_DO(__set_hash_del);
write_unlock_bh(&ip_set_lock);
return 0;
}
DP("unreachable reached!");
return -EINVAL;
}
set = ip_set_list[index];
IP_SET_ASSERT(set);
if (SETNAME_EQ(req_bind->binding, IPSET_TOKEN_DEFAULT)) {
/* Default binding of set */
ip_set_id_t binding = ip_set_find_byindex(set->binding);
if (binding == IP_SET_INVALID_ID)
return -ENOENT;
write_lock_bh(&ip_set_lock);
/* Sets in hash values are referenced */
__ip_set_put(set->binding);
set->binding = IP_SET_INVALID_ID;
write_unlock_bh(&ip_set_lock);
return 0;
} else if (SETNAME_EQ(req_bind->binding, IPSET_TOKEN_ALL)) {
/* Flush all bindings */
write_lock_bh(&ip_set_lock);
FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
write_unlock_bh(&ip_set_lock);
return 0;
}
res = __ip_set_testip(set,
data + sizeof(struct ip_set_req_bind),
size - sizeof(struct ip_set_req_bind),
&ip);
DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
if (res >= 0)
res = ip_set_hash_del(set->id, ip);
return res;
}
static int
ip_set_testbind(ip_set_id_t index,
const void *data,
size_t size)
{
struct ip_set *set = ip_set_list[index];
const struct ip_set_req_bind *req_bind;
ip_set_id_t binding;
ip_set_ip_t ip;
int res;
IP_SET_ASSERT(set);
if (size < sizeof(struct ip_set_req_bind))
return -EINVAL;
req_bind = data;
if (SETNAME_EQ(req_bind->binding, IPSET_TOKEN_DEFAULT)) {
/* Default binding of set */
const char *binding_name;
if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
return -EINVAL;
binding_name = data + sizeof(struct ip_set_req_bind);
binding = ip_set_find_byname(binding_name);
if (binding == IP_SET_INVALID_ID)
return -ENOENT;
res = (set->binding == binding) ? -EEXIST : 0;
return res;
}
binding = ip_set_find_byname(req_bind->binding);
if (binding == IP_SET_INVALID_ID)
return -ENOENT;
res = __ip_set_testip(set,
data + sizeof(struct ip_set_req_bind),
size - sizeof(struct ip_set_req_bind),
&ip);
DP("set %s, ip: %u.%u.%u.%u, binding %s",
set->name, HIPQUAD(ip), ip_set_list[binding]->name);
if (res >= 0)
res = (ip_set_find_in_hash(set->id, ip) == binding)
? -EEXIST : 0;
return res;
}
static struct ip_set_type *
find_set_type_rlock(const char *typename)
{
struct ip_set_type *type;
read_lock_bh(&ip_set_lock);
type = find_set_type(typename);
if (type == NULL)
read_unlock_bh(&ip_set_lock);
return type;
}
static int
find_free_id(const char *name,
ip_set_id_t *index,
ip_set_id_t *id)
{
ip_set_id_t i;
*id = IP_SET_INVALID_ID;
for (i = 0; i < ip_set_max; i++) {
if (ip_set_list[i] == NULL) {
if (*id == IP_SET_INVALID_ID)
*id = *index = i;
} else if (SETNAME_EQ(name, ip_set_list[i]->name))
/* Name clash */
return -EEXIST;
}
if (*id == IP_SET_INVALID_ID)
/* No free slot remained */
return -ERANGE;
/* Check that index is usable as id (swapping) */
check:
for (i = 0; i < ip_set_max; i++) {
if (ip_set_list[i] != NULL
&& ip_set_list[i]->id == *id) {
*id = i;
goto check;
}
}
return 0;
}
/*
* Create a set
*/
static int
ip_set_create(const char *name,
const char *typename,
ip_set_id_t restore,
const void *data,
size_t size)
{
struct ip_set *set;
ip_set_id_t index = 0, id;
int res = 0;
DP("setname: %s, typename: %s, id: %u", name, typename, restore);
/*
* First, and without any locks, allocate and initialize
* a normal base set structure.
*/
set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
if (!set)
return -ENOMEM;
rwlock_init(&set->lock);
strncpy(set->name, name, IP_SET_MAXNAMELEN);
set->binding = IP_SET_INVALID_ID;
atomic_set(&set->ref, 0);
/*
* Next, take the &ip_set_lock, check that we know the type,
* and take a reference on the type, to make sure it
* stays available while constructing our new set.
*
* After referencing the type, we drop the &ip_set_lock,
* and let the new set construction run without locks.
*/
set->type = find_set_type_rlock(typename);
if (set->type == NULL) {
/* Try loading the module */
char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
strcpy(modulename, "ip_set_");
strcat(modulename, typename);
DP("try to load %s", modulename);
request_module(modulename);
set->type = find_set_type_rlock(typename);
}
if (set->type == NULL) {
ip_set_printk("no set type '%s', set '%s' not created",
typename, name);
res = -ENOENT;
goto out;
}
if (!try_module_get(set->type->me)) {
read_unlock_bh(&ip_set_lock);
res = -EFAULT;
goto out;
}
read_unlock_bh(&ip_set_lock);
/* Check request size */
if (size != set->type->header_size) {
ip_set_printk("data length wrong (want %zu, have %zu)",
set->type->header_size,
size);
goto put_out;
}
/*
* Without holding any locks, create private part.
*/
res = set->type->create(set, data, size);
if (res != 0)
goto put_out;
/* BTW, res==0 here. */
/*
* Here, we have a valid, constructed set. &ip_set_lock again,
* find free id/index and check that it is not already in
* ip_set_list.
*/
write_lock_bh(&ip_set_lock);
if ((res = find_free_id(set->name, &index, &id)) != 0) {
DP("no free id!");
goto cleanup;
}
/* Make sure restore gets the same index */
if (restore != IP_SET_INVALID_ID && index != restore) {
DP("Can't restore, sets are screwed up");
res = -ERANGE;
goto cleanup;
}
/*
* Finally! Add our shiny new set to the list, and be done.
*/
DP("create: '%s' created with index %u, id %u!", set->name, index, id);
set->id = id;
ip_set_list[index] = set;
write_unlock_bh(&ip_set_lock);
return res;
cleanup:
write_unlock_bh(&ip_set_lock);
set->type->destroy(set);
put_out:
module_put(set->type->me);
out:
kfree(set);
return res;
}
/*
* Destroy a given existing set
*/
static void
ip_set_destroy_set(ip_set_id_t index)
{
struct ip_set *set = ip_set_list[index];
IP_SET_ASSERT(set);
DP("set: %s", set->name);
write_lock_bh(&ip_set_lock);
FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
if (set->binding != IP_SET_INVALID_ID)
__ip_set_put(set->binding);
ip_set_list[index] = NULL;
write_unlock_bh(&ip_set_lock);
/* Must call it without holding any lock */
set->type->destroy(set);
module_put(set->type->me);
kfree(set);
}
/*
* Destroy a set - or all sets
* Sets must not be referenced/used.
*/
static int
ip_set_destroy(ip_set_id_t index)
{
ip_set_id_t i;
/* ref modification always protected by the mutex */
if (index != IP_SET_INVALID_ID) {
if (atomic_read(&ip_set_list[index]->ref))
return -EBUSY;
ip_set_destroy_set(index);
} else {
for (i = 0; i < ip_set_max; i++) {
if (ip_set_list[i] != NULL
&& (atomic_read(&ip_set_list[i]->ref)))
return -EBUSY;
}
for (i = 0; i < ip_set_max; i++) {
if (ip_set_list[i] != NULL)
ip_set_destroy_set(i);
}
}
return 0;
}
static void
ip_set_flush_set(struct ip_set *set)
{
DP("set: %s %u", set->name, set->id);
write_lock_bh(&set->lock);
set->type->flush(set);
write_unlock_bh(&set->lock);
}
/*
* Flush data in a set - or in all sets
*/
static int
ip_set_flush(ip_set_id_t index)
{
if (index != IP_SET_INVALID_ID) {
IP_SET_ASSERT(ip_set_list[index]);
ip_set_flush_set(ip_set_list[index]);
} else
FOREACH_SET_DO(ip_set_flush_set);
return 0;
}
/* Rename a set */
static int
ip_set_rename(ip_set_id_t index, const char *name)
{
struct ip_set *set = ip_set_list[index];
ip_set_id_t i;
int res = 0;
DP("set: %s to %s", set->name, name);
write_lock_bh(&ip_set_lock);
for (i = 0; i < ip_set_max; i++) {
if (ip_set_list[i] != NULL
&& SETNAME_EQ(ip_set_list[i]->name, name)) {
res = -EEXIST;
goto unlock;
}
}
strncpy(set->name, name, IP_SET_MAXNAMELEN);
unlock:
write_unlock_bh(&ip_set_lock);
return res;
}
/*
* Swap two sets so that name/index points to the other.
* References are also swapped.
*/
static int
ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
{
struct ip_set *from = ip_set_list[from_index];
struct ip_set *to = ip_set_list[to_index];
char from_name[IP_SET_MAXNAMELEN];
u_int32_t from_ref;
DP("set: %s to %s", from->name, to->name);
/* Features must not change.
* Not an artifical restriction anymore, as we must prevent
* possible loops created by swapping in setlist type of sets. */
if (from->type->features != to->type->features)
return -ENOEXEC;
/* No magic here: ref munging protected by the mutex */
write_lock_bh(&ip_set_lock);
strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
from_ref = atomic_read(&from->ref);
strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
atomic_set(&from->ref, atomic_read(&to->ref));
strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
atomic_set(&to->ref, from_ref);
ip_set_list[from_index] = to;
ip_set_list[to_index] = from;
write_unlock_bh(&ip_set_lock);
return 0;
}
/*
* List set data
*/
static inline void
__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
ip_set_id_t id, size_t *size)
{
if (set_hash->id == id)
*size += sizeof(struct ip_set_hash_list);
}
static inline void
__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
ip_set_id_t id, size_t *size)
{
if (set_hash->id == id)
*size += sizeof(struct ip_set_hash_save);
}
static inline void
__set_hash_bindings(struct ip_set_hash *set_hash,
ip_set_id_t id, void *data, int *used)
{
if (set_hash->id == id) {
struct ip_set_hash_list *hash_list = data + *used;
hash_list->ip = set_hash->ip;
hash_list->binding = set_hash->binding;
*used += sizeof(struct ip_set_hash_list);
}
}
static int ip_set_list_set(ip_set_id_t index,
void *data,
int *used,
int len)
{
struct ip_set *set = ip_set_list[index];
struct ip_set_list *set_list;
/* Pointer to our header */
set_list = data + *used;
DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
/* Get and ensure header size */
if (*used + sizeof(struct ip_set_list) > len)
goto not_enough_mem;
*used += sizeof(struct ip_set_list);
read_lock_bh(&set->lock);
/* Get and ensure set specific header size */
set_list->header_size = set->type->header_size;
if (*used + set_list->header_size > len)
goto unlock_set;
/* Fill in the header */
set_list->index = index;
set_list->binding = set->binding;
set_list->ref = atomic_read(&set->ref);
/* Fill in set spefific header data */
set->type->list_header(set, data + *used);
*used += set_list->header_size;
/* Get and ensure set specific members size */
set_list->members_size = set->type->list_members_size(set);
if (*used + set_list->members_size > len)
goto unlock_set;
/* Fill in set spefific members data */
set->type->list_members(set, data + *used);
*used += set_list->members_size;
read_unlock_bh(&set->lock);
/* Bindings */
/* Get and ensure set specific bindings size */
set_list->bindings_size = 0;
FOREACH_HASH_DO(__set_hash_bindings_size_list,
set->id, &set_list->bindings_size);
if (*used + set_list->bindings_size > len)
goto not_enough_mem;
/* Fill in set spefific bindings data */
FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
return 0;
unlock_set:
read_unlock_bh(&set->lock);
not_enough_mem:
DP("not enough mem, try again");
return -EAGAIN;
}
/*
* Save sets
*/
static int ip_set_save_set(ip_set_id_t index,
void *data,
int *used,
int len)
{
struct ip_set *set;
struct ip_set_save *set_save;
/* Pointer to our header */
set_save = data + *used;
/* Get and ensure header size */
if (*used + sizeof(struct ip_set_save) > len)
goto not_enough_mem;
*used += sizeof(struct ip_set_save);
set = ip_set_list[index];
DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
data, data + *used);
read_lock_bh(&set->lock);
/* Get and ensure set specific header size */
set_save->header_size = set->type->header_size;
if (*used + set_save->header_size > len)
goto unlock_set;
/* Fill in the header */
set_save->index = index;
set_save->binding = set->binding;
/* Fill in set spefific header data */
set->type->list_header(set, data + *used);
*used += set_save->header_size;
DP("set header filled: %s, used: %u(%u) %p %p", set->name, *used,
set_save->header_size, data, data + *used);
/* Get and ensure set specific members size */
set_save->members_size = set->type->list_members_size(set);
if (*used + set_save->members_size > len)
goto unlock_set;
/* Fill in set spefific members data */
set->type->list_members(set, data + *used);
*used += set_save->members_size;
read_unlock_bh(&set->lock);
DP("set members filled: %s, used: %u(%u) %p %p", set->name, *used,
set_save->members_size, data, data + *used);
return 0;
unlock_set:
read_unlock_bh(&set->lock);
not_enough_mem:
DP("not enough mem, try again");
return -EAGAIN;
}
static inline void
__set_hash_save_bindings(struct ip_set_hash *set_hash,
ip_set_id_t id,
void *data,
int *used,
int len,
int *res)
{
if (*res == 0
&& (id == IP_SET_INVALID_ID || set_hash->id == id)) {
struct ip_set_hash_save *hash_save = data + *used;
/* Ensure bindings size */
if (*used + sizeof(struct ip_set_hash_save) > len) {
*res = -ENOMEM;
return;
}
hash_save->id = set_hash->id;
hash_save->ip = set_hash->ip;
hash_save->binding = set_hash->binding;
*used += sizeof(struct ip_set_hash_save);
}
}
static int ip_set_save_bindings(ip_set_id_t index,
void *data,
int *used,
int len)
{
int res = 0;
struct ip_set_save *set_save;
DP("used %u, len %u", *used, len);
/* Get and ensure header size */
if (*used + sizeof(struct ip_set_save) > len)
return -ENOMEM;
/* Marker */
set_save = data + *used;
set_save->index = IP_SET_INVALID_ID;
set_save->header_size = 0;
set_save->members_size = 0;
*used += sizeof(struct ip_set_save);
DP("marker added used %u, len %u", *used, len);
/* Fill in bindings data */
if (index != IP_SET_INVALID_ID)
/* Sets are identified by id in hash */
index = ip_set_list[index]->id;
FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
return res;
}
/*
* Restore sets
*/
static int ip_set_restore(void *data,
int len)
{
int res = 0;
int line = 0, used = 0, members_size;
struct ip_set *set;
struct ip_set_hash_save *hash_save;
struct ip_set_restore *set_restore;
ip_set_id_t index;
/* Loop to restore sets */
while (1) {
line++;
DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
/* Get and ensure header size */
if (used + sizeof(struct ip_set_restore) > len)
return line;
set_restore = data + used;
used += sizeof(struct ip_set_restore);
/* Ensure data size */
if (used
+ set_restore->header_size
+ set_restore->members_size > len)
return line;
/* Check marker */
if (set_restore->index == IP_SET_INVALID_ID) {
line--;
goto bindings;
}
/* Try to create the set */
DP("restore %s %s", set_restore->name, set_restore->typename);
res = ip_set_create(set_restore->name,
set_restore->typename,
set_restore->index,
data + used,
set_restore->header_size);
if (res != 0)
return line;
used += set_restore->header_size;
index = ip_set_find_byindex(set_restore->index);
DP("index %u, restore_index %u", index, set_restore->index);
if (index != set_restore->index)
return line;
/* Try to restore members data */
set = ip_set_list[index];
members_size = 0;
DP("members_size %u reqsize %u",
set_restore->members_size, set->type->reqsize);
while (members_size + set->type->reqsize <=
set_restore->members_size) {
line++;
DP("members: %u, line %u", members_size, line);
res = __ip_set_addip(index,
data + used + members_size,
set->type->reqsize);
if (!(res == 0 || res == -EEXIST))
return line;
members_size += set->type->reqsize;
}
DP("members_size %u %u",
set_restore->members_size, members_size);
if (members_size != set_restore->members_size)
return line++;
used += set_restore->members_size;
}
bindings:
/* Loop to restore bindings */
while (used < len) {
line++;
DP("restore binding, line %u", line);
/* Get and ensure size */
if (used + sizeof(struct ip_set_hash_save) > len)
return line;
hash_save = data + used;
used += sizeof(struct ip_set_hash_save);
/* hash_save->id is used to store the index */
index = ip_set_find_byindex(hash_save->id);
DP("restore binding index %u, id %u, %u -> %u",
index, hash_save->id, hash_save->ip, hash_save->binding);
if (index != hash_save->id)
return line;
if (ip_set_find_byindex(hash_save->binding) == IP_SET_INVALID_ID) {
DP("corrupt binding set index %u", hash_save->binding);
return line;
}
set = ip_set_list[hash_save->id];
/* Null valued IP means default binding */
if (hash_save->ip)
res = ip_set_hash_add(set->id,
hash_save->ip,
hash_save->binding);
else {
IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
write_lock_bh(&ip_set_lock);
set->binding = hash_save->binding;
__ip_set_get(set->binding);
write_unlock_bh(&ip_set_lock);
DP("default binding: %u", set->binding);
}
if (res != 0)
return line;
}
if (used != len)
return line;
return 0;
}
static int
ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
{
void *data;
int res = 0; /* Assume OK */
unsigned *op;
struct ip_set_req_adt *req_adt;
ip_set_id_t index = IP_SET_INVALID_ID;
int (*adtfn)(ip_set_id_t index,
const void *data, size_t size);
struct fn_table {
int (*fn)(ip_set_id_t index,
const void *data, size_t size);
} adtfn_table[] =
{ { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
{ ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
};
DP("optval=%d, user=%p, len=%d", optval, user, len);
if (!capable(CAP_NET_ADMIN))
return -EPERM;
if (optval != SO_IP_SET)
return -EBADF;
if (len <= sizeof(unsigned)) {
ip_set_printk("short userdata (want >%zu, got %u)",
sizeof(unsigned), len);
return -EINVAL;
}
data = vmalloc(len);
if (!data) {
DP("out of mem for %u bytes", len);
return -ENOMEM;
}
if (copy_from_user(data, user, len) != 0) {
res = -EFAULT;
goto done;
}
if (down_interruptible(&ip_set_app_mutex)) {
res = -EINTR;
goto done;
}
op = (unsigned *)data;
DP("op=%x", *op);
if (*op < IP_SET_OP_VERSION) {
/* Check the version at the beginning of operations */
struct ip_set_req_version *req_version = data;
if (req_version->version != IP_SET_PROTOCOL_VERSION) {
res = -EPROTO;
goto done;
}
}
switch (*op) {
case IP_SET_OP_CREATE:{
struct ip_set_req_create *req_create = data;
if (len < sizeof(struct ip_set_req_create)) {
ip_set_printk("short CREATE data (want >=%zu, got %u)",
sizeof(struct ip_set_req_create), len);
res = -EINVAL;
goto done;
}
req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
res = ip_set_create(req_create->name,
req_create->typename,
IP_SET_INVALID_ID,
data + sizeof(struct ip_set_req_create),
len - sizeof(struct ip_set_req_create));
goto done;
}
case IP_SET_OP_DESTROY:{
struct ip_set_req_std *req_destroy = data;
if (len != sizeof(struct ip_set_req_std)) {
ip_set_printk("invalid DESTROY data (want %zu, got %u)",
sizeof(struct ip_set_req_std), len);
res = -EINVAL;
goto done;
}
if (SETNAME_EQ(req_destroy->name, IPSET_TOKEN_ALL)) {
/* Destroy all sets */
index = IP_SET_INVALID_ID;
} else {
req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
index = ip_set_find_byname(req_destroy->name);
if (index == IP_SET_INVALID_ID) {
res = -ENOENT;
goto done;
}
}
res = ip_set_destroy(index);
goto done;
}
case IP_SET_OP_FLUSH:{
struct ip_set_req_std *req_flush = data;
if (len != sizeof(struct ip_set_req_std)) {
ip_set_printk("invalid FLUSH data (want %zu, got %u)",
sizeof(struct ip_set_req_std), len);
res = -EINVAL;
goto done;
}
if (SETNAME_EQ(req_flush->name, IPSET_TOKEN_ALL)) {
/* Flush all sets */
index = IP_SET_INVALID_ID;
} else {
req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
index = ip_set_find_byname(req_flush->name);
if (index == IP_SET_INVALID_ID) {
res = -ENOENT;
goto done;
}
}
res = ip_set_flush(index);
goto done;
}
case IP_SET_OP_RENAME:{
struct ip_set_req_create *req_rename = data;
if (len != sizeof(struct ip_set_req_create)) {
ip_set_printk("invalid RENAME data (want %zu, got %u)",
sizeof(struct ip_set_req_create), len);
res = -EINVAL;
goto done;
}
req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
index = ip_set_find_byname(req_rename->name);
if (index == IP_SET_INVALID_ID) {
res = -ENOENT;
goto done;
}
res = ip_set_rename(index, req_rename->typename);
goto done;
}
case IP_SET_OP_SWAP:{
struct ip_set_req_create *req_swap = data;
ip_set_id_t to_index;
if (len != sizeof(struct ip_set_req_create)) {
ip_set_printk("invalid SWAP data (want %zu, got %u)",
sizeof(struct ip_set_req_create), len);
res = -EINVAL;
goto done;
}
req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
index = ip_set_find_byname(req_swap->name);
if (index == IP_SET_INVALID_ID) {
res = -ENOENT;
goto done;
}
to_index = ip_set_find_byname(req_swap->typename);
if (to_index == IP_SET_INVALID_ID) {
res = -ENOENT;
goto done;
}
res = ip_set_swap(index, to_index);
goto done;
}
default:
break; /* Set identified by id */
}
/* There we may have add/del/test/bind/unbind/test_bind operations */
if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
res = -EBADMSG;
goto done;
}
adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
if (len < sizeof(struct ip_set_req_adt)) {
ip_set_printk("short data in adt request (want >=%zu, got %u)",
sizeof(struct ip_set_req_adt), len);
res = -EINVAL;
goto done;
}
req_adt = data;
/* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
if (!(*op == IP_SET_OP_UNBIND_SET
&& req_adt->index == IP_SET_INVALID_ID)) {
index = ip_set_find_byindex(req_adt->index);
if (index == IP_SET_INVALID_ID) {
res = -ENOENT;
goto done;
}
}
res = adtfn(index, data, len);
done:
up(&ip_set_app_mutex);
vfree(data);
if (res > 0)
res = 0;
DP("final result %d", res);
return res;
}
static int
ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
{
int res = 0;
unsigned *op;
ip_set_id_t index = IP_SET_INVALID_ID;
void *data;
int copylen = *len;
DP("optval=%d, user=%p, len=%d", optval, user, *len);
if (!capable(CAP_NET_ADMIN))
return -EPERM;
if (optval != SO_IP_SET)
return -EBADF;
if (*len < sizeof(unsigned)) {
ip_set_printk("short userdata (want >=%zu, got %d)",
sizeof(unsigned), *len);
return -EINVAL;
}
data = vmalloc(*len);
if (!data) {
DP("out of mem for %d bytes", *len);
return -ENOMEM;
}
if (copy_from_user(data, user, *len) != 0) {
res = -EFAULT;
goto done;
}
if (down_interruptible(&ip_set_app_mutex)) {
res = -EINTR;
goto done;
}
op = (unsigned *) data;
DP("op=%x", *op);
if (*op < IP_SET_OP_VERSION) {
/* Check the version at the beginning of operations */
struct ip_set_req_version *req_version = data;
if (req_version->version != IP_SET_PROTOCOL_VERSION) {
res = -EPROTO;
goto done;
}
}
switch (*op) {
case IP_SET_OP_VERSION: {
struct ip_set_req_version *req_version = data;
if (*len != sizeof(struct ip_set_req_version)) {
ip_set_printk("invalid VERSION (want %zu, got %d)",
sizeof(struct ip_set_req_version),
*len);
res = -EINVAL;
goto done;
}
req_version->version = IP_SET_PROTOCOL_VERSION;
res = copy_to_user(user, req_version,
sizeof(struct ip_set_req_version));
goto done;
}
case IP_SET_OP_GET_BYNAME: {
struct ip_set_req_get_set *req_get = data;
if (*len != sizeof(struct ip_set_req_get_set)) {
ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
sizeof(struct ip_set_req_get_set), *len);
res = -EINVAL;
goto done;
}
req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
index = ip_set_find_byname(req_get->set.name);
req_get->set.index = index;
goto copy;
}
case IP_SET_OP_GET_BYINDEX: {
struct ip_set_req_get_set *req_get = data;
if (*len != sizeof(struct ip_set_req_get_set)) {
ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
sizeof(struct ip_set_req_get_set), *len);
res = -EINVAL;
goto done;
}
req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
index = ip_set_find_byindex(req_get->set.index);
strncpy(req_get->set.name,
index == IP_SET_INVALID_ID ? ""
: ip_set_list[index]->name, IP_SET_MAXNAMELEN);
goto copy;
}
case IP_SET_OP_ADT_GET: {
struct ip_set_req_adt_get *req_get = data;
if (*len != sizeof(struct ip_set_req_adt_get)) {
ip_set_printk("invalid ADT_GET (want %zu, got %d)",
sizeof(struct ip_set_req_adt_get), *len);
res = -EINVAL;
goto done;
}
req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
index = ip_set_find_byname(req_get->set.name);
if (index != IP_SET_INVALID_ID) {
req_get->set.index = index;
strncpy(req_get->typename,
ip_set_list[index]->type->typename,
IP_SET_MAXNAMELEN - 1);
} else {
res = -ENOENT;
goto done;
}
goto copy;
}
case IP_SET_OP_MAX_SETS: {
struct ip_set_req_max_sets *req_max_sets = data;
ip_set_id_t i;
if (*len != sizeof(struct ip_set_req_max_sets)) {
ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
sizeof(struct ip_set_req_max_sets), *len);
res = -EINVAL;
goto done;
}
if (SETNAME_EQ(req_max_sets->set.name, IPSET_TOKEN_ALL)) {
req_max_sets->set.index = IP_SET_INVALID_ID;
} else {
req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
req_max_sets->set.index =
ip_set_find_byname(req_max_sets->set.name);
if (req_max_sets->set.index == IP_SET_INVALID_ID) {
res = -ENOENT;
goto done;
}
}
req_max_sets->max_sets = ip_set_max;
req_max_sets->sets = 0;
for (i = 0; i < ip_set_max; i++) {
if (ip_set_list[i] != NULL)
req_max_sets->sets++;
}
goto copy;
}
case IP_SET_OP_LIST_SIZE:
case IP_SET_OP_SAVE_SIZE: {
struct ip_set_req_setnames *req_setnames = data;
struct ip_set_name_list *name_list;
struct ip_set *set;
ip_set_id_t i;
int used;
if (*len < sizeof(struct ip_set_req_setnames)) {
ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
sizeof(struct ip_set_req_setnames), *len);
res = -EINVAL;
goto done;
}
req_setnames->size = 0;
used = sizeof(struct ip_set_req_setnames);
for (i = 0; i < ip_set_max; i++) {
if (ip_set_list[i] == NULL)
continue;
name_list = data + used;
used += sizeof(struct ip_set_name_list);
if (used > copylen) {
res = -EAGAIN;
goto done;
}
set = ip_set_list[i];
/* Fill in index, name, etc. */
name_list->index = i;
name_list->id = set->id;
strncpy(name_list->name,
set->name,
IP_SET_MAXNAMELEN - 1);
strncpy(name_list->typename,
set->type->typename,
IP_SET_MAXNAMELEN - 1);
DP("filled %s of type %s, index %u\n",
name_list->name, name_list->typename,
name_list->index);
if (!(req_setnames->index == IP_SET_INVALID_ID
|| req_setnames->index == i))
continue;
/* Update size */
switch (*op) {
case IP_SET_OP_LIST_SIZE: {
req_setnames->size += sizeof(struct ip_set_list)
+ set->type->header_size
+ set->type->list_members_size(set);
/* Sets are identified by id in the hash */
FOREACH_HASH_DO(__set_hash_bindings_size_list,
set->id, &req_setnames->size);
break;
}
case IP_SET_OP_SAVE_SIZE: {
req_setnames->size += sizeof(struct ip_set_save)
+ set->type->header_size
+ set->type->list_members_size(set);
FOREACH_HASH_DO(__set_hash_bindings_size_save,
set->id, &req_setnames->size);
break;
}
default:
break;
}
}
if (copylen != used) {
res = -EAGAIN;
goto done;
}
goto copy;
}
case IP_SET_OP_LIST: {
struct ip_set_req_list *req_list = data;
ip_set_id_t i;
int used;
if (*len < sizeof(struct ip_set_req_list)) {
ip_set_printk("short LIST (want >=%zu, got %d)",
sizeof(struct ip_set_req_list), *len);
res = -EINVAL;
goto done;
}
index = req_list->index;
if (index != IP_SET_INVALID_ID
&& ip_set_find_byindex(index) != index) {
res = -ENOENT;
goto done;
}
used = 0;
if (index == IP_SET_INVALID_ID) {
/* List all sets */
for (i = 0; i < ip_set_max && res == 0; i++) {
if (ip_set_list[i] != NULL)
res = ip_set_list_set(i, data, &used, *len);
}
} else {
/* List an individual set */
res = ip_set_list_set(index, data, &used, *len);
}
if (res != 0)
goto done;
else if (copylen != used) {
res = -EAGAIN;
goto done;
}
goto copy;
}
case IP_SET_OP_SAVE: {
struct ip_set_req_list *req_save = data;
ip_set_id_t i;
int used;
if (*len < sizeof(struct ip_set_req_list)) {
ip_set_printk("short SAVE (want >=%zu, got %d)",
sizeof(struct ip_set_req_list), *len);
res = -EINVAL;
goto done;
}
index = req_save->index;
if (index != IP_SET_INVALID_ID
&& ip_set_find_byindex(index) != index) {
res = -ENOENT;
goto done;
}
used = 0;
if (index == IP_SET_INVALID_ID) {
/* Save all sets */
for (i = 0; i < ip_set_max && res == 0; i++) {
if (ip_set_list[i] != NULL)
res = ip_set_save_set(i, data, &used, *len);
}
} else {
/* Save an individual set */
res = ip_set_save_set(index, data, &used, *len);
}
if (res == 0)
res = ip_set_save_bindings(index, data, &used, *len);
if (res != 0)
goto done;
else if (copylen != used) {
res = -EAGAIN;
goto done;
}
goto copy;
}
case IP_SET_OP_RESTORE: {
struct ip_set_req_setnames *req_restore = data;
int line;
if (*len < sizeof(struct ip_set_req_setnames)
|| *len != req_restore->size) {
ip_set_printk("invalid RESTORE (want =%zu, got %d)",
req_restore->size, *len);
res = -EINVAL;
goto done;
}
line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
req_restore->size - sizeof(struct ip_set_req_setnames));
DP("ip_set_restore: %u", line);
if (line != 0) {
res = -EAGAIN;
req_restore->size = line;
copylen = sizeof(struct ip_set_req_setnames);
goto copy;
}
goto done;
}
default:
res = -EBADMSG;
goto done;
} /* end of switch(op) */
copy:
DP("set %s, copylen %u", index != IP_SET_INVALID_ID
&& ip_set_list[index]
? ip_set_list[index]->name
: ":all:", copylen);
res = copy_to_user(user, data, copylen);
done:
up(&ip_set_app_mutex);
vfree(data);
if (res > 0)
res = 0;
DP("final result %d", res);
return res;
}
static struct nf_sockopt_ops so_set = {
.pf = PF_INET,
.set_optmin = SO_IP_SET,
.set_optmax = SO_IP_SET + 1,
.set = &ip_set_sockfn_set,
.get_optmin = SO_IP_SET,
.get_optmax = SO_IP_SET + 1,
.get = &ip_set_sockfn_get,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
.use = 0,
#else
.owner = THIS_MODULE,
#endif
};
static int max_sets, hash_size;
module_param(max_sets, int, 0600);
MODULE_PARM_DESC(max_sets, "maximal number of sets");
module_param(hash_size, int, 0600);
MODULE_PARM_DESC(hash_size, "hash size for bindings");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
MODULE_DESCRIPTION("module implementing core IP set support");
static int __init ip_set_init(void)
{
int res;
ip_set_id_t i;
get_random_bytes(&ip_set_hash_random, 4);
if (max_sets)
ip_set_max = max_sets;
ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
if (!ip_set_list) {
printk(KERN_ERR "Unable to create ip_set_list\n");
return -ENOMEM;
}
memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
if (hash_size)
ip_set_bindings_hash_size = hash_size;
ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
if (!ip_set_hash) {
printk(KERN_ERR "Unable to create ip_set_hash\n");
vfree(ip_set_list);
return -ENOMEM;
}
for (i = 0; i < ip_set_bindings_hash_size; i++)
INIT_LIST_HEAD(&ip_set_hash[i]);
INIT_LIST_HEAD(&set_type_list);
res = nf_register_sockopt(&so_set);
if (res != 0) {
ip_set_printk("SO_SET registry failed: %d", res);
vfree(ip_set_list);
vfree(ip_set_hash);
return res;
}
return 0;
}
static void __exit ip_set_fini(void)
{
/* There can't be any existing set or binding */
nf_unregister_sockopt(&so_set);
vfree(ip_set_list);
vfree(ip_set_hash);
DP("these are the famous last words");
}
EXPORT_SYMBOL(ip_set_register_set_type);
EXPORT_SYMBOL(ip_set_unregister_set_type);
EXPORT_SYMBOL(ip_set_get_byname);
EXPORT_SYMBOL(ip_set_get_byindex);
EXPORT_SYMBOL(ip_set_put_byindex);
EXPORT_SYMBOL(ip_set_id);
EXPORT_SYMBOL(__ip_set_get_byname);
EXPORT_SYMBOL(__ip_set_put_byindex);
EXPORT_SYMBOL(ip_set_addip_kernel);
EXPORT_SYMBOL(ip_set_delip_kernel);
EXPORT_SYMBOL(ip_set_testip_kernel);
module_init(ip_set_init);
module_exit(ip_set_fini);
|