13 #include <linux/module.h>
18 #include <linux/netlink.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter/x_tables.h>
24 #include <linux/netfilter/nfnetlink.h>
25 #include <linux/netfilter/ipset/ip_set.h>
31 static struct ip_set **ip_set_list;
34 #define STREQ(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
36 static unsigned int max_sets;
52 ip_set_type_lock(
void)
58 ip_set_type_unlock(
void)
70 list_for_each_entry_rcu(type, &ip_set_type_list,
list)
72 (type->family == family ||
82 load_settype(
const char *name)
85 pr_debug(
"try to load ip_set_%s\n", name);
86 if (request_module(
"ip_set_%s", name) < 0) {
87 pr_warning(
"Can't find ip_set type %s\n", name);
96 #define find_set_type_get(name, family, revision, found) \
97 __find_set_type_get(name, family, revision, found, false)
100 __find_set_type_get(
const char *name,
u8 family,
u8 revision,
106 if (retry && !load_settype(name))
110 *found = find_set_type(name, family, revision);
112 err = !try_module_get((*found)->me) ? -
EFAULT : 0;
117 list_for_each_entry_rcu(type, &ip_set_type_list,
list)
125 __find_set_type_get(name, family, revision, found,
true);
136 #define find_set_type_minmax(name, family, min, max) \
137 __find_set_type_minmax(name, family, min, max, false)
140 __find_set_type_minmax(
const char *name,
u8 family,
u8 *
min,
u8 *
max,
146 if (retry && !load_settype(name))
149 *min = 255; *max = 0;
151 list_for_each_entry_rcu(type, &ip_set_type_list,
list)
153 (type->
family == family ||
166 __find_set_type_minmax(name, family, min, max,
true);
169 #define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \
170 (f) == NFPROTO_IPV6 ? "inet6" : "any")
181 pr_warning(
"ip_set type %s, family %s, revision %u:%u uses "
182 "wrong protocol version %u (want %u)\n",
192 pr_warning(
"ip_set type %s, family %s with revision min %u "
193 "already registered!\n", type->
name,
198 list_add_rcu(&type->
list, &ip_set_type_list);
199 pr_debug(
"type %s, family %s, revision %u:%u registered.\n",
203 ip_set_type_unlock();
214 pr_warning(
"ip_set type %s, family %s with revision min %u "
215 "not registered\n", type->
name,
219 list_del_rcu(&type->
list);
220 pr_debug(
"type %s, family %s with revision min %u unregistered.\n",
223 ip_set_type_unlock();
233 void *members =
NULL;
239 pr_debug(
"%p: allocated with kmalloc\n", members);
246 pr_debug(
"%p: allocated with vmalloc\n", members);
255 pr_debug(
"%p: free with %s\n", members,
256 is_vmalloc_addr(members) ?
"vfree" :
"kfree");
257 if (is_vmalloc_addr(members))
335 BUG_ON(ip_set_list[index]->ref == 0);
358 if (opt->
dim <
set->type->dimension ||
363 ret =
set->variant->kadt(
set, skb, par,
IPSET_TEST, opt);
368 pr_debug(
"element must be competed, ADD is triggered\n");
370 set->variant->kadt(
set, skb, par,
IPSET_ADD, opt);
382 return (ret < 0 ? 0 : ret);
397 if (opt->
dim <
set->type->dimension ||
402 ret =
set->variant->kadt(
set, skb, par,
IPSET_ADD, opt);
420 if (opt->
dim <
set->type->dimension ||
425 ret =
set->variant->kadt(
set, skb, par,
IPSET_DEL, opt);
443 for (i = 0; i < ip_set_max; i++) {
465 if (ip_set_list[index] != NULL)
524 if (index > ip_set_max)
528 if (ip_set_list[index])
561 protocol_failed(
const struct nlattr *
const tb[])
568 flag_exist(
const struct nlmsghdr *nlh)
581 sizeof(*nfmsg), flags);
585 nfmsg = nlmsg_data(nlh);
607 find_set_id(
const char *name)
613 set = ip_set_list[
i];
614 if (
set != NULL &&
STREQ(
set->name, name))
620 static inline struct ip_set *
621 find_set(
const char *name)
634 for (i = 0; i < ip_set_max; i++) {
635 if (ip_set_list[i] == NULL) {
638 }
else if (
STREQ(name, ip_set_list[i]->name)) {
640 *
set = ip_set_list[
i];
651 ip_set_none(
struct sock *ctnl,
struct sk_buff *skb,
659 ip_set_create(
struct sock *ctnl,
struct sk_buff *skb,
666 const char *
name, *
typename;
668 u32 flags = flag_exist(nlh);
671 if (
unlikely(protocol_failed(attr) ||
684 pr_debug(
"setname: %s, typename: %s, family: %s, revision: %u\n",
714 if (attr[IPSET_ATTR_DATA] &&
716 set->type->create_policy)) {
721 ret =
set->type->create(
set, tb, flags);
732 ret = find_free_id(
set->name, &index, &clash);
738 set->type->family == clash->
type->family &&
739 set->type->revision_min == clash->
type->revision_min &&
740 set->type->revision_max == clash->
type->revision_max &&
741 set->variant->same_set(
set, clash))
749 pr_debug(
"create: '%s' created with index %u!\n",
set->name, index);
750 ip_set_list[
index] =
set;
755 set->variant->destroy(
set);
757 module_put(
set->type->me);
781 set->variant->destroy(
set);
782 module_put(
set->type->me);
787 ip_set_destroy(
struct sock *ctnl,
struct sk_buff *skb,
789 const struct nlattr *
const attr[])
794 if (
unlikely(protocol_failed(attr)))
809 for (i = 0; i < ip_set_max; i++) {
810 if (ip_set_list[i] != NULL && ip_set_list[i]->
ref) {
816 for (i = 0; i < ip_set_max; i++) {
817 if (ip_set_list[i] != NULL)
818 ip_set_destroy_set(i);
821 i = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
825 }
else if (ip_set_list[i]->
ref) {
831 ip_set_destroy_set(i);
842 ip_set_flush_set(
struct ip_set *
set)
847 set->variant->flush(
set);
852 ip_set_flush(
struct sock *ctnl,
struct sk_buff *skb,
854 const struct nlattr *
const attr[])
858 if (
unlikely(protocol_failed(attr)))
861 if (!attr[IPSET_ATTR_SETNAME]) {
862 for (i = 0; i < ip_set_max; i++)
863 if (ip_set_list[i] != NULL)
864 ip_set_flush_set(ip_set_list[i]);
866 i = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
870 ip_set_flush_set(ip_set_list[i]);
888 ip_set_rename(
struct sock *ctnl,
struct sk_buff *skb,
890 const struct nlattr *
const attr[])
897 if (
unlikely(protocol_failed(attr) ||
898 attr[IPSET_ATTR_SETNAME] == NULL ||
902 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
913 for (i = 0; i < ip_set_max; i++) {
914 if (ip_set_list[i] != NULL &&
915 STREQ(ip_set_list[i]->name, name2)) {
937 ip_set_swap(
struct sock *ctnl,
struct sk_buff *skb,
939 const struct nlattr *
const attr[])
945 if (
unlikely(protocol_failed(attr) ||
946 attr[IPSET_ATTR_SETNAME] == NULL ||
950 from_id = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
958 from = ip_set_list[from_id];
959 to = ip_set_list[to_id];
964 if (!(from->
type->features == to->
type->features &&
965 from->
type->family == to->
type->family))
974 ip_set_list[from_id] = to;
975 ip_set_list[to_id] =
from;
988 #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
989 #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
1019 struct nlattr *attr = (
void *)nlh + min_len;
1025 attr, nlh->
nlmsg_len - min_len, ip_set_setname_policy);
1032 if (cda[IPSET_ATTR_SETNAME]) {
1033 index = find_set_id(nla_data(cda[IPSET_ATTR_SETNAME]));
1043 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1044 dump_type |= (f << 16);
1062 ret = dump_init(cb);
1064 nlh = nlmsg_hdr(cb->
skb);
1073 if (cb->
args[1] >= ip_set_max)
1078 max = dump_type ==
DUMP_ONE ? cb->
args[1] + 1 : ip_set_max;
1080 pr_debug(
"args[0]: %u %u args[1]: %ld\n",
1081 dump_type, dump_flags, cb->
args[1]);
1084 set = ip_set_list[index];
1103 __ip_set_get(index);
1106 cb->
nlh->nlmsg_seq, flags,
1110 goto release_refcount;
1113 nla_put_string(skb, IPSET_ATTR_SETNAME,
set->name))
1114 goto nla_put_failure;
1117 switch (cb->
args[2]) {
1126 goto nla_put_failure;
1127 ret =
set->variant->head(
set, skb);
1129 goto release_refcount;
1135 ret =
set->variant->list(
set, skb, cb);
1140 goto release_refcount;
1146 cb->
args[0] = dump_type | (dump_flags << 16);
1161 if (ret || !cb->
args[2]) {
1162 pr_debug(
"release set %s\n", ip_set_list[index]->name);
1168 nlmsg_end(skb, nlh);
1173 return ret < 0 ? ret : skb->
len;
1177 ip_set_dump(
struct sock *ctnl,
struct sk_buff *skb,
1179 const struct nlattr *
const attr[])
1181 if (
unlikely(protocol_failed(attr)))
1186 .dump = ip_set_dump_start,
1187 .done = ip_set_dump_done,
1189 return netlink_dump_start(ctnl, skb, nlh, &c);
1207 u32 flags,
bool use_lineno)
1215 ret =
set->variant->uadt(
set, tb, adt, &lineno, flags, retried);
1218 }
while (ret == -
EAGAIN &&
1219 set->variant->resize &&
1220 (ret =
set->variant->resize(
set, retried)) == 0);
1224 if (lineno && use_lineno) {
1229 size_t payload =
sizeof(*errmsg) + nlmsg_len(nlh);
1240 errmsg = nlmsg_data(rep);
1243 cmdattr = (
void *)&errmsg->
msg + min_len;
1262 ip_set_uadd(
struct sock *ctnl,
struct sk_buff *skb,
1264 const struct nlattr *
const attr[])
1269 u32 flags = flag_exist(nlh);
1273 if (
unlikely(protocol_failed(attr) ||
1274 attr[IPSET_ATTR_SETNAME] == NULL ||
1275 !((attr[IPSET_ATTR_DATA] != NULL) ^
1277 (attr[IPSET_ATTR_DATA] != NULL &&
1278 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1284 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
1289 if (attr[IPSET_ATTR_DATA]) {
1291 attr[IPSET_ATTR_DATA],
1292 set->type->adt_policy))
1294 ret = call_ad(ctnl, skb,
set, tb,
IPSET_ADD, flags,
1300 memset(tb, 0,
sizeof(tb));
1301 if (
nla_type(nla) != IPSET_ATTR_DATA ||
1302 !flag_nested(nla) ||
1304 set->type->adt_policy))
1306 ret = call_ad(ctnl, skb,
set, tb,
IPSET_ADD,
1316 ip_set_udel(
struct sock *ctnl,
struct sk_buff *skb,
1318 const struct nlattr *
const attr[])
1323 u32 flags = flag_exist(nlh);
1327 if (
unlikely(protocol_failed(attr) ||
1328 attr[IPSET_ATTR_SETNAME] == NULL ||
1329 !((attr[IPSET_ATTR_DATA] != NULL) ^
1330 (attr[IPSET_ATTR_ADT] != NULL)) ||
1331 (attr[IPSET_ATTR_DATA] != NULL &&
1332 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1333 (attr[IPSET_ATTR_ADT] != NULL &&
1334 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1338 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
1343 if (attr[IPSET_ATTR_DATA]) {
1345 attr[IPSET_ATTR_DATA],
1346 set->type->adt_policy))
1348 ret = call_ad(ctnl, skb,
set, tb,
IPSET_DEL, flags,
1354 memset(tb, 0,
sizeof(*tb));
1355 if (
nla_type(nla) != IPSET_ATTR_DATA ||
1356 !flag_nested(nla) ||
1358 set->type->adt_policy))
1360 ret = call_ad(ctnl, skb,
set, tb,
IPSET_DEL,
1370 ip_set_utest(
struct sock *ctnl,
struct sk_buff *skb,
1372 const struct nlattr *
const attr[])
1378 if (
unlikely(protocol_failed(attr) ||
1379 attr[IPSET_ATTR_SETNAME] == NULL ||
1380 attr[IPSET_ATTR_DATA] == NULL ||
1381 !flag_nested(attr[IPSET_ATTR_DATA])))
1384 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
1389 set->type->adt_policy))
1393 ret =
set->variant->uadt(
set, tb,
IPSET_TEST, NULL, 0, 0);
1405 ip_set_header(
struct sock *ctnl,
struct sk_buff *skb,
1407 const struct nlattr *
const attr[])
1409 const struct ip_set *
set;
1415 if (
unlikely(protocol_failed(attr) ||
1416 attr[IPSET_ATTR_SETNAME] == NULL))
1419 index = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
1422 set = ip_set_list[
index];
1433 nla_put_string(skb2, IPSET_ATTR_SETNAME,
set->name) ||
1437 goto nla_put_failure;
1438 nlmsg_end(skb2, nlh2);
1447 nlmsg_cancel(skb2, nlh2);
1465 const struct nlattr *
const attr[])
1470 const char *
typename;
1473 if (
unlikely(protocol_failed(attr) ||
1497 goto nla_put_failure;
1498 nlmsg_end(skb2, nlh2);
1508 nlmsg_cancel(skb2, nlh2);
1522 ip_set_protocol(
struct sock *ctnl,
struct sk_buff *skb,
1524 const struct nlattr *
const attr[])
1542 goto nla_put_failure;
1543 nlmsg_end(skb2, nlh2);
1552 nlmsg_cancel(skb2, nlh2);
1560 .call = ip_set_none,
1564 .call = ip_set_create,
1566 .policy = ip_set_create_policy,
1569 .call = ip_set_destroy,
1571 .policy = ip_set_setname_policy,
1574 .call = ip_set_flush,
1576 .policy = ip_set_setname_policy,
1579 .call = ip_set_rename,
1581 .policy = ip_set_setname2_policy,
1584 .call = ip_set_swap,
1586 .policy = ip_set_setname2_policy,
1589 .call = ip_set_dump,
1591 .policy = ip_set_setname_policy,
1594 .call = ip_set_dump,
1596 .policy = ip_set_setname_policy,
1599 .call = ip_set_uadd,
1601 .policy = ip_set_adt_policy,
1604 .call = ip_set_udel,
1606 .policy = ip_set_adt_policy,
1609 .call = ip_set_utest,
1611 .policy = ip_set_adt_policy,
1614 .call = ip_set_header,
1616 .policy = ip_set_setname_policy,
1621 .policy = ip_set_type_policy,
1624 .call = ip_set_protocol,
1626 .policy = ip_set_protocol_policy,
1634 .cb = ip_set_netlink_subsys_cb,
1640 ip_set_sockfn_get(
struct sock *
sk,
int optval,
void __user *
user,
int *len)
1644 int copylen = *len, ret = 0;
1650 if (*len <
sizeof(
unsigned int))
1660 op = (
unsigned int *) data;
1702 req_get->
set.
index >= ip_set_max) {
1729 static struct nf_sockopt_ops so_set __read_mostly = {
1733 .get = &ip_set_sockfn_get,
1743 ip_set_max = max_sets;
1747 ip_set_list = kzalloc(
sizeof(
struct ip_set *) * ip_set_max,
1754 pr_err(
"ip_set: cannot register with nfnetlink.\n");
1760 pr_err(
"SO_SET registry failed: %d\n", ret);
1777 pr_debug(
"these are the famous last words\n");