1 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 /* Kernel module for IP set management */
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
16 #include <linux/skbuff.h>
17 #include <linux/spinlock.h>
18 #include <linux/rculist.h>
19 #include <net/netlink.h>
20 #include <net/net_namespace.h>
21 #include <net/netns/generic.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter/x_tables.h>
25 #include <linux/netfilter/nfnetlink.h>
26 #include <linux/netfilter/ipset/ip_set.h>
28 static LIST_HEAD(ip_set_type_list); /* all registered set types */
29 static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */
30 static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */
33 struct ip_set * __rcu *ip_set_list; /* all individual sets */
34 ip_set_id_t ip_set_max; /* max number of sets */
35 int is_deleted; /* deleted by ip_set_net_exit */
37 static int ip_set_net_id __read_mostly;
39 static inline struct ip_set_net *ip_set_pernet(struct net *net)
41 return net_generic(net, ip_set_net_id);
45 #define STREQ(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
47 static unsigned int max_sets;
49 module_param(max_sets, int, 0600);
50 MODULE_PARM_DESC(max_sets, "maximal number of sets");
51 MODULE_LICENSE("GPL");
52 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
53 MODULE_DESCRIPTION("core IP set support");
54 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
56 /* When the nfnl mutex is held: */
57 #define ip_set_dereference(p) \
58 rcu_dereference_protected(p, 1)
59 #define ip_set(inst, id) \
60 ip_set_dereference((inst)->ip_set_list)[id]
63 * The set types are implemented in modules and registered set types
64 * can be found in ip_set_type_list. Adding/deleting types is
65 * serialized by ip_set_type_mutex.
69 ip_set_type_lock(void)
71 mutex_lock(&ip_set_type_mutex);
75 ip_set_type_unlock(void)
77 mutex_unlock(&ip_set_type_mutex);
80 /* Register and deregister settype */
82 static struct ip_set_type *
83 find_set_type(const char *name, u8 family, u8 revision)
85 struct ip_set_type *type;
87 list_for_each_entry_rcu(type, &ip_set_type_list, list)
88 if (STREQ(type->name, name) &&
89 (type->family == family ||
90 type->family == NFPROTO_UNSPEC) &&
91 revision >= type->revision_min &&
92 revision <= type->revision_max)
97 /* Unlock, try to load a set type module and lock again */
99 load_settype(const char *name)
101 nfnl_unlock(NFNL_SUBSYS_IPSET);
102 pr_debug("try to load ip_set_%s\n", name);
103 if (request_module("ip_set_%s", name) < 0) {
104 pr_warn("Can't find ip_set type %s\n", name);
105 nfnl_lock(NFNL_SUBSYS_IPSET);
108 nfnl_lock(NFNL_SUBSYS_IPSET);
112 /* Find a set type and reference it */
113 #define find_set_type_get(name, family, revision, found) \
114 __find_set_type_get(name, family, revision, found, false)
117 __find_set_type_get(const char *name, u8 family, u8 revision,
118 struct ip_set_type **found, bool retry)
120 struct ip_set_type *type;
123 if (retry && !load_settype(name))
124 return -IPSET_ERR_FIND_TYPE;
127 *found = find_set_type(name, family, revision);
129 err = !try_module_get((*found)->me) ? -EFAULT : 0;
132 /* Make sure the type is already loaded
133 * but we don't support the revision */
134 list_for_each_entry_rcu(type, &ip_set_type_list, list)
135 if (STREQ(type->name, name)) {
136 err = -IPSET_ERR_FIND_TYPE;
141 return retry ? -IPSET_ERR_FIND_TYPE :
142 __find_set_type_get(name, family, revision, found, true);
149 /* Find a given set type by name and family.
150 * If we succeeded, the supported minimal and maximum revisions are
153 #define find_set_type_minmax(name, family, min, max) \
154 __find_set_type_minmax(name, family, min, max, false)
157 __find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max,
160 struct ip_set_type *type;
163 if (retry && !load_settype(name))
164 return -IPSET_ERR_FIND_TYPE;
166 *min = 255; *max = 0;
168 list_for_each_entry_rcu(type, &ip_set_type_list, list)
169 if (STREQ(type->name, name) &&
170 (type->family == family ||
171 type->family == NFPROTO_UNSPEC)) {
173 if (type->revision_min < *min)
174 *min = type->revision_min;
175 if (type->revision_max > *max)
176 *max = type->revision_max;
182 return retry ? -IPSET_ERR_FIND_TYPE :
183 __find_set_type_minmax(name, family, min, max, true);
186 #define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \
187 (f) == NFPROTO_IPV6 ? "inet6" : "any")
189 /* Register a set type structure. The type is identified by
190 * the unique triple of name, family and revision.
193 ip_set_type_register(struct ip_set_type *type)
197 if (type->protocol != IPSET_PROTOCOL) {
198 pr_warn("ip_set type %s, family %s, revision %u:%u uses wrong protocol version %u (want %u)\n",
199 type->name, family_name(type->family),
200 type->revision_min, type->revision_max,
201 type->protocol, IPSET_PROTOCOL);
206 if (find_set_type(type->name, type->family, type->revision_min)) {
208 pr_warn("ip_set type %s, family %s with revision min %u already registered!\n",
209 type->name, family_name(type->family),
214 list_add_rcu(&type->list, &ip_set_type_list);
215 pr_debug("type %s, family %s, revision %u:%u registered.\n",
216 type->name, family_name(type->family),
217 type->revision_min, type->revision_max);
219 ip_set_type_unlock();
222 EXPORT_SYMBOL_GPL(ip_set_type_register);
224 /* Unregister a set type. There's a small race with ip_set_create */
226 ip_set_type_unregister(struct ip_set_type *type)
229 if (!find_set_type(type->name, type->family, type->revision_min)) {
230 pr_warn("ip_set type %s, family %s with revision min %u not registered\n",
231 type->name, family_name(type->family),
235 list_del_rcu(&type->list);
236 pr_debug("type %s, family %s with revision min %u unregistered.\n",
237 type->name, family_name(type->family), type->revision_min);
239 ip_set_type_unlock();
243 EXPORT_SYMBOL_GPL(ip_set_type_unregister);
245 /* Utility functions */
247 ip_set_alloc(size_t size)
249 void *members = NULL;
251 if (size < KMALLOC_MAX_SIZE)
252 members = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
255 pr_debug("%p: allocated with kmalloc\n", members);
259 members = vzalloc(size);
262 pr_debug("%p: allocated with vmalloc\n", members);
266 EXPORT_SYMBOL_GPL(ip_set_alloc);
269 ip_set_free(void *members)
271 pr_debug("%p: free with %s\n", members,
272 is_vmalloc_addr(members) ? "vfree" : "kfree");
275 EXPORT_SYMBOL_GPL(ip_set_free);
278 flag_nested(const struct nlattr *nla)
280 return nla->nla_type & NLA_F_NESTED;
283 static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
284 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
285 [IPSET_ATTR_IPADDR_IPV6] = { .type = NLA_BINARY,
286 .len = sizeof(struct in6_addr) },
290 ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
292 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX+1];
294 if (unlikely(!flag_nested(nla)))
295 return -IPSET_ERR_PROTOCOL;
296 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, ipaddr_policy))
297 return -IPSET_ERR_PROTOCOL;
298 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
299 return -IPSET_ERR_PROTOCOL;
301 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
304 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
307 ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
309 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX+1];
311 if (unlikely(!flag_nested(nla)))
312 return -IPSET_ERR_PROTOCOL;
314 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, ipaddr_policy))
315 return -IPSET_ERR_PROTOCOL;
316 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
317 return -IPSET_ERR_PROTOCOL;
319 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
320 sizeof(struct in6_addr));
323 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
325 typedef void (*destroyer)(void *);
326 /* ipset data extension types, in size order */
328 const struct ip_set_ext_type ip_set_extensions[] = {
329 [IPSET_EXT_ID_COUNTER] = {
330 .type = IPSET_EXT_COUNTER,
331 .flag = IPSET_FLAG_WITH_COUNTERS,
332 .len = sizeof(struct ip_set_counter),
333 .align = __alignof__(struct ip_set_counter),
335 [IPSET_EXT_ID_TIMEOUT] = {
336 .type = IPSET_EXT_TIMEOUT,
337 .len = sizeof(unsigned long),
338 .align = __alignof__(unsigned long),
340 [IPSET_EXT_ID_SKBINFO] = {
341 .type = IPSET_EXT_SKBINFO,
342 .flag = IPSET_FLAG_WITH_SKBINFO,
343 .len = sizeof(struct ip_set_skbinfo),
344 .align = __alignof__(struct ip_set_skbinfo),
346 [IPSET_EXT_ID_COMMENT] = {
347 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
348 .flag = IPSET_FLAG_WITH_COMMENT,
349 .len = sizeof(struct ip_set_comment),
350 .align = __alignof__(struct ip_set_comment),
351 .destroy = (destroyer) ip_set_comment_free,
354 EXPORT_SYMBOL_GPL(ip_set_extensions);
357 add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
359 return ip_set_extensions[id].flag ?
360 (flags & ip_set_extensions[id].flag) :
361 !!tb[IPSET_ATTR_TIMEOUT];
365 ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len)
367 enum ip_set_ext_id id;
371 if (tb[IPSET_ATTR_CADT_FLAGS])
372 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
373 if (cadt_flags & IPSET_FLAG_WITH_FORCEADD)
374 set->flags |= IPSET_CREATE_FLAG_FORCEADD;
375 for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
376 if (!add_extension(id, cadt_flags, tb))
378 offset += ALIGN(len + offset, ip_set_extensions[id].align);
379 set->offset[id] = offset;
380 set->extensions |= ip_set_extensions[id].type;
381 offset += ip_set_extensions[id].len;
385 EXPORT_SYMBOL_GPL(ip_set_elem_len);
388 ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
389 struct ip_set_ext *ext)
392 if (tb[IPSET_ATTR_TIMEOUT]) {
393 if (!(set->extensions & IPSET_EXT_TIMEOUT))
394 return -IPSET_ERR_TIMEOUT;
395 ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
397 if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) {
398 if (!(set->extensions & IPSET_EXT_COUNTER))
399 return -IPSET_ERR_COUNTER;
400 if (tb[IPSET_ATTR_BYTES])
401 ext->bytes = be64_to_cpu(nla_get_be64(
402 tb[IPSET_ATTR_BYTES]));
403 if (tb[IPSET_ATTR_PACKETS])
404 ext->packets = be64_to_cpu(nla_get_be64(
405 tb[IPSET_ATTR_PACKETS]));
407 if (tb[IPSET_ATTR_COMMENT]) {
408 if (!(set->extensions & IPSET_EXT_COMMENT))
409 return -IPSET_ERR_COMMENT;
410 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
412 if (tb[IPSET_ATTR_SKBMARK]) {
413 if (!(set->extensions & IPSET_EXT_SKBINFO))
414 return -IPSET_ERR_SKBINFO;
415 fullmark = be64_to_cpu(nla_get_be64(tb[IPSET_ATTR_SKBMARK]));
416 ext->skbmark = fullmark >> 32;
417 ext->skbmarkmask = fullmark & 0xffffffff;
419 if (tb[IPSET_ATTR_SKBPRIO]) {
420 if (!(set->extensions & IPSET_EXT_SKBINFO))
421 return -IPSET_ERR_SKBINFO;
422 ext->skbprio = be32_to_cpu(nla_get_be32(
423 tb[IPSET_ATTR_SKBPRIO]));
425 if (tb[IPSET_ATTR_SKBQUEUE]) {
426 if (!(set->extensions & IPSET_EXT_SKBINFO))
427 return -IPSET_ERR_SKBINFO;
428 ext->skbqueue = be16_to_cpu(nla_get_be16(
429 tb[IPSET_ATTR_SKBQUEUE]));
433 EXPORT_SYMBOL_GPL(ip_set_get_extensions);
436 * Creating/destroying/renaming/swapping affect the existence and
437 * the properties of a set. All of these can be executed from userspace
438 * only and serialized by the nfnl mutex indirectly from nfnetlink.
440 * Sets are identified by their index in ip_set_list and the index
441 * is used by the external references (set/SET netfilter modules).
443 * The set behind an index may change by swapping only, from userspace.
447 __ip_set_get(struct ip_set *set)
449 write_lock_bh(&ip_set_ref_lock);
451 write_unlock_bh(&ip_set_ref_lock);
455 __ip_set_put(struct ip_set *set)
457 write_lock_bh(&ip_set_ref_lock);
458 BUG_ON(set->ref == 0);
460 write_unlock_bh(&ip_set_ref_lock);
464 * Add, del and test set entries from kernel.
466 * The set behind the index must exist and must be referenced
467 * so it can't be destroyed (or changed) under our foot.
470 static inline struct ip_set *
471 ip_set_rcu_get(struct net *net, ip_set_id_t index)
474 struct ip_set_net *inst = ip_set_pernet(net);
477 /* ip_set_list itself needs to be protected */
478 set = rcu_dereference(inst->ip_set_list)[index];
485 ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
486 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
488 struct ip_set *set = ip_set_rcu_get(
489 dev_net(par->in ? par->in : par->out), index);
493 pr_debug("set %s, index %u\n", set->name, index);
495 if (opt->dim < set->type->dimension ||
496 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
499 read_lock_bh(&set->lock);
500 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
501 read_unlock_bh(&set->lock);
503 if (ret == -EAGAIN) {
504 /* Type requests element to be completed */
505 pr_debug("element must be completed, ADD is triggered\n");
506 write_lock_bh(&set->lock);
507 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
508 write_unlock_bh(&set->lock);
511 /* --return-nomatch: invert matched element */
512 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
513 (set->type->features & IPSET_TYPE_NOMATCH) &&
514 (ret > 0 || ret == -ENOTEMPTY))
518 /* Convert error codes to nomatch */
519 return (ret < 0 ? 0 : ret);
521 EXPORT_SYMBOL_GPL(ip_set_test);
524 ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
525 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
527 struct ip_set *set = ip_set_rcu_get(
528 dev_net(par->in ? par->in : par->out), index);
532 pr_debug("set %s, index %u\n", set->name, index);
534 if (opt->dim < set->type->dimension ||
535 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
536 return -IPSET_ERR_TYPE_MISMATCH;
538 write_lock_bh(&set->lock);
539 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
540 write_unlock_bh(&set->lock);
544 EXPORT_SYMBOL_GPL(ip_set_add);
547 ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
548 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
550 struct ip_set *set = ip_set_rcu_get(
551 dev_net(par->in ? par->in : par->out), index);
555 pr_debug("set %s, index %u\n", set->name, index);
557 if (opt->dim < set->type->dimension ||
558 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
559 return -IPSET_ERR_TYPE_MISMATCH;
561 write_lock_bh(&set->lock);
562 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
563 write_unlock_bh(&set->lock);
567 EXPORT_SYMBOL_GPL(ip_set_del);
570 * Find set by name, reference it once. The reference makes sure the
571 * thing pointed to, does not go away under our feet.
575 ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
577 ip_set_id_t i, index = IPSET_INVALID_ID;
579 struct ip_set_net *inst = ip_set_pernet(net);
582 for (i = 0; i < inst->ip_set_max; i++) {
583 s = rcu_dereference(inst->ip_set_list)[i];
584 if (s != NULL && STREQ(s->name, name)) {
595 EXPORT_SYMBOL_GPL(ip_set_get_byname);
598 * If the given set pointer points to a valid set, decrement
599 * reference count by 1. The caller shall not assume the index
600 * to be valid, after calling this function.
605 __ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
610 set = rcu_dereference(inst->ip_set_list)[index];
617 ip_set_put_byindex(struct net *net, ip_set_id_t index)
619 struct ip_set_net *inst = ip_set_pernet(net);
621 __ip_set_put_byindex(inst, index);
623 EXPORT_SYMBOL_GPL(ip_set_put_byindex);
626 * Get the name of a set behind a set index.
627 * We assume the set is referenced, so it does exist and
628 * can't be destroyed. The set cannot be renamed due to
629 * the referencing either.
633 ip_set_name_byindex(struct net *net, ip_set_id_t index)
635 const struct ip_set *set = ip_set_rcu_get(net, index);
638 BUG_ON(set->ref == 0);
640 /* Referenced, so it's safe */
643 EXPORT_SYMBOL_GPL(ip_set_name_byindex);
646 * Routines to call by external subsystems, which do not
647 * call nfnl_lock for us.
651 * Find set by index, reference it once. The reference makes sure the
652 * thing pointed to, does not go away under our feet.
654 * The nfnl mutex is used in the function.
657 ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
660 struct ip_set_net *inst = ip_set_pernet(net);
662 if (index >= inst->ip_set_max)
663 return IPSET_INVALID_ID;
665 nfnl_lock(NFNL_SUBSYS_IPSET);
666 set = ip_set(inst, index);
670 index = IPSET_INVALID_ID;
671 nfnl_unlock(NFNL_SUBSYS_IPSET);
675 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
678 * If the given set pointer points to a valid set, decrement
679 * reference count by 1. The caller shall not assume the index
680 * to be valid, after calling this function.
682 * The nfnl mutex is used in the function.
685 ip_set_nfnl_put(struct net *net, ip_set_id_t index)
688 struct ip_set_net *inst = ip_set_pernet(net);
690 nfnl_lock(NFNL_SUBSYS_IPSET);
691 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
692 set = ip_set(inst, index);
696 nfnl_unlock(NFNL_SUBSYS_IPSET);
698 EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
701 * Communication protocol with userspace over netlink.
703 * The commands are serialized by the nfnl mutex.
707 protocol_failed(const struct nlattr * const tb[])
709 return !tb[IPSET_ATTR_PROTOCOL] ||
710 nla_get_u8(tb[IPSET_ATTR_PROTOCOL]) != IPSET_PROTOCOL;
714 flag_exist(const struct nlmsghdr *nlh)
716 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
719 static struct nlmsghdr *
720 start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
723 struct nlmsghdr *nlh;
724 struct nfgenmsg *nfmsg;
726 nlh = nlmsg_put(skb, portid, seq, cmd | (NFNL_SUBSYS_IPSET << 8),
727 sizeof(*nfmsg), flags);
731 nfmsg = nlmsg_data(nlh);
732 nfmsg->nfgen_family = NFPROTO_IPV4;
733 nfmsg->version = NFNETLINK_V0;
741 static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
742 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
743 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
744 .len = IPSET_MAXNAMELEN - 1 },
745 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
746 .len = IPSET_MAXNAMELEN - 1},
747 [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
748 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
749 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
752 static struct ip_set *
753 find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
755 struct ip_set *set = NULL;
758 *id = IPSET_INVALID_ID;
759 for (i = 0; i < inst->ip_set_max; i++) {
760 set = ip_set(inst, i);
761 if (set != NULL && STREQ(set->name, name)) {
766 return (*id == IPSET_INVALID_ID ? NULL : set);
769 static inline struct ip_set *
770 find_set(struct ip_set_net *inst, const char *name)
774 return find_set_and_id(inst, name, &id);
778 find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
784 *index = IPSET_INVALID_ID;
785 for (i = 0; i < inst->ip_set_max; i++) {
788 if (*index == IPSET_INVALID_ID)
790 } else if (STREQ(name, s->name)) {
796 if (*index == IPSET_INVALID_ID)
797 /* No free slot remained */
798 return -IPSET_ERR_MAX_SETS;
803 ip_set_none(struct sock *ctnl, struct sk_buff *skb,
804 const struct nlmsghdr *nlh,
805 const struct nlattr * const attr[])
811 ip_set_create(struct sock *ctnl, struct sk_buff *skb,
812 const struct nlmsghdr *nlh,
813 const struct nlattr * const attr[])
815 struct net *net = sock_net(ctnl);
816 struct ip_set_net *inst = ip_set_pernet(net);
817 struct ip_set *set, *clash = NULL;
818 ip_set_id_t index = IPSET_INVALID_ID;
819 struct nlattr *tb[IPSET_ATTR_CREATE_MAX+1] = {};
820 const char *name, *typename;
822 u32 flags = flag_exist(nlh);
825 if (unlikely(protocol_failed(attr) ||
826 attr[IPSET_ATTR_SETNAME] == NULL ||
827 attr[IPSET_ATTR_TYPENAME] == NULL ||
828 attr[IPSET_ATTR_REVISION] == NULL ||
829 attr[IPSET_ATTR_FAMILY] == NULL ||
830 (attr[IPSET_ATTR_DATA] != NULL &&
831 !flag_nested(attr[IPSET_ATTR_DATA]))))
832 return -IPSET_ERR_PROTOCOL;
834 name = nla_data(attr[IPSET_ATTR_SETNAME]);
835 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
836 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
837 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
838 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
839 name, typename, family_name(family), revision);
842 * First, and without any locks, allocate and initialize
843 * a normal base set structure.
845 set = kzalloc(sizeof(struct ip_set), GFP_KERNEL);
848 rwlock_init(&set->lock);
849 strlcpy(set->name, name, IPSET_MAXNAMELEN);
850 set->family = family;
851 set->revision = revision;
854 * Next, check that we know the type, and take
855 * a reference on the type, to make sure it stays available
856 * while constructing our new set.
858 * After referencing the type, we try to create the type
859 * specific part of the set without holding any locks.
861 ret = find_set_type_get(typename, family, revision, &(set->type));
866 * Without holding any locks, create private part.
868 if (attr[IPSET_ATTR_DATA] &&
869 nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
870 set->type->create_policy)) {
871 ret = -IPSET_ERR_PROTOCOL;
875 ret = set->type->create(net, set, tb, flags);
879 /* BTW, ret==0 here. */
882 * Here, we have a valid, constructed set and we are protected
883 * by the nfnl mutex. Find the first free index in ip_set_list
884 * and check clashing.
886 ret = find_free_id(inst, set->name, &index, &clash);
887 if (ret == -EEXIST) {
888 /* If this is the same set and requested, ignore error */
889 if ((flags & IPSET_FLAG_EXIST) &&
890 STREQ(set->type->name, clash->type->name) &&
891 set->type->family == clash->type->family &&
892 set->type->revision_min == clash->type->revision_min &&
893 set->type->revision_max == clash->type->revision_max &&
894 set->variant->same_set(set, clash))
897 } else if (ret == -IPSET_ERR_MAX_SETS) {
898 struct ip_set **list, **tmp;
899 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
901 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
905 list = kzalloc(sizeof(struct ip_set *) * i, GFP_KERNEL);
908 /* nfnl mutex is held, both lists are valid */
909 tmp = ip_set_dereference(inst->ip_set_list);
910 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
911 rcu_assign_pointer(inst->ip_set_list, list);
912 /* Make sure all current packets have passed through */
915 index = inst->ip_set_max;
916 inst->ip_set_max = i;
923 * Finally! Add our shiny new set to the list, and be done.
925 pr_debug("create: '%s' created with index %u!\n", set->name, index);
926 ip_set(inst, index) = set;
931 set->variant->destroy(set);
933 module_put(set->type->me);
941 static const struct nla_policy
942 ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
943 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
944 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
945 .len = IPSET_MAXNAMELEN - 1 },
949 ip_set_destroy_set(struct ip_set_net *inst, ip_set_id_t index)
951 struct ip_set *set = ip_set(inst, index);
953 pr_debug("set: %s\n", set->name);
954 ip_set(inst, index) = NULL;
956 /* Must call it without holding any lock */
957 set->variant->destroy(set);
958 module_put(set->type->me);
963 ip_set_destroy(struct sock *ctnl, struct sk_buff *skb,
964 const struct nlmsghdr *nlh,
965 const struct nlattr * const attr[])
967 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
972 if (unlikely(protocol_failed(attr)))
973 return -IPSET_ERR_PROTOCOL;
975 /* Commands are serialized and references are
976 * protected by the ip_set_ref_lock.
977 * External systems (i.e. xt_set) must call
978 * ip_set_put|get_nfnl_* functions, that way we
979 * can safely check references here.
981 * list:set timer can only decrement the reference
982 * counter, so if it's already zero, we can proceed
983 * without holding the lock.
985 read_lock_bh(&ip_set_ref_lock);
986 if (!attr[IPSET_ATTR_SETNAME]) {
987 for (i = 0; i < inst->ip_set_max; i++) {
989 if (s != NULL && s->ref) {
990 ret = -IPSET_ERR_BUSY;
994 read_unlock_bh(&ip_set_ref_lock);
995 for (i = 0; i < inst->ip_set_max; i++) {
998 ip_set_destroy_set(inst, i);
1001 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1006 } else if (s->ref) {
1007 ret = -IPSET_ERR_BUSY;
1010 read_unlock_bh(&ip_set_ref_lock);
1012 ip_set_destroy_set(inst, i);
1016 read_unlock_bh(&ip_set_ref_lock);
1023 ip_set_flush_set(struct ip_set *set)
1025 pr_debug("set: %s\n", set->name);
1027 write_lock_bh(&set->lock);
1028 set->variant->flush(set);
1029 write_unlock_bh(&set->lock);
1033 ip_set_flush(struct sock *ctnl, struct sk_buff *skb,
1034 const struct nlmsghdr *nlh,
1035 const struct nlattr * const attr[])
1037 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1041 if (unlikely(protocol_failed(attr)))
1042 return -IPSET_ERR_PROTOCOL;
1044 if (!attr[IPSET_ATTR_SETNAME]) {
1045 for (i = 0; i < inst->ip_set_max; i++) {
1046 s = ip_set(inst, i);
1048 ip_set_flush_set(s);
1051 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1055 ip_set_flush_set(s);
1063 static const struct nla_policy
1064 ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
1065 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1066 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1067 .len = IPSET_MAXNAMELEN - 1 },
1068 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
1069 .len = IPSET_MAXNAMELEN - 1 },
1073 ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
1074 const struct nlmsghdr *nlh,
1075 const struct nlattr * const attr[])
1077 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1078 struct ip_set *set, *s;
1083 if (unlikely(protocol_failed(attr) ||
1084 attr[IPSET_ATTR_SETNAME] == NULL ||
1085 attr[IPSET_ATTR_SETNAME2] == NULL))
1086 return -IPSET_ERR_PROTOCOL;
1088 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1092 read_lock_bh(&ip_set_ref_lock);
1093 if (set->ref != 0) {
1094 ret = -IPSET_ERR_REFERENCED;
1098 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1099 for (i = 0; i < inst->ip_set_max; i++) {
1100 s = ip_set(inst, i);
1101 if (s != NULL && STREQ(s->name, name2)) {
1102 ret = -IPSET_ERR_EXIST_SETNAME2;
1106 strncpy(set->name, name2, IPSET_MAXNAMELEN);
1109 read_unlock_bh(&ip_set_ref_lock);
1113 /* Swap two sets so that name/index points to the other.
1114 * References and set names are also swapped.
1116 * The commands are serialized by the nfnl mutex and references are
1117 * protected by the ip_set_ref_lock. The kernel interfaces
1118 * do not hold the mutex but the pointer settings are atomic
1119 * so the ip_set_list always contains valid pointers to the sets.
1123 ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
1124 const struct nlmsghdr *nlh,
1125 const struct nlattr * const attr[])
1127 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1128 struct ip_set *from, *to;
1129 ip_set_id_t from_id, to_id;
1130 char from_name[IPSET_MAXNAMELEN];
1132 if (unlikely(protocol_failed(attr) ||
1133 attr[IPSET_ATTR_SETNAME] == NULL ||
1134 attr[IPSET_ATTR_SETNAME2] == NULL))
1135 return -IPSET_ERR_PROTOCOL;
1137 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1142 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1145 return -IPSET_ERR_EXIST_SETNAME2;
1147 /* Features must not change.
1148 * Not an artificial restriction anymore, as we must prevent
1149 * possible loops created by swapping in setlist type of sets. */
1150 if (!(from->type->features == to->type->features &&
1151 from->family == to->family))
1152 return -IPSET_ERR_TYPE_MISMATCH;
1154 strncpy(from_name, from->name, IPSET_MAXNAMELEN);
1155 strncpy(from->name, to->name, IPSET_MAXNAMELEN);
1156 strncpy(to->name, from_name, IPSET_MAXNAMELEN);
1158 write_lock_bh(&ip_set_ref_lock);
1159 swap(from->ref, to->ref);
1160 ip_set(inst, from_id) = to;
1161 ip_set(inst, to_id) = from;
1162 write_unlock_bh(&ip_set_ref_lock);
1167 /* List/save set data */
1174 #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
1175 #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
1178 ip_set_dump_done(struct netlink_callback *cb)
1180 struct ip_set_net *inst = (struct ip_set_net *)cb->args[IPSET_CB_NET];
1181 if (cb->args[IPSET_CB_ARG0]) {
1182 pr_debug("release set %s\n",
1183 ip_set(inst, cb->args[IPSET_CB_INDEX])->name);
1184 __ip_set_put_byindex(inst,
1185 (ip_set_id_t) cb->args[IPSET_CB_INDEX]);
1191 dump_attrs(struct nlmsghdr *nlh)
1193 const struct nlattr *attr;
1196 pr_debug("dump nlmsg\n");
1197 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
1198 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
1203 dump_init(struct netlink_callback *cb, struct ip_set_net *inst)
1205 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
1206 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1207 struct nlattr *cda[IPSET_ATTR_CMD_MAX+1];
1208 struct nlattr *attr = (void *)nlh + min_len;
1212 /* Second pass, so parser can't fail */
1213 nla_parse(cda, IPSET_ATTR_CMD_MAX,
1214 attr, nlh->nlmsg_len - min_len, ip_set_setname_policy);
1216 /* cb->args[IPSET_CB_NET]: net namespace
1217 * [IPSET_CB_DUMP]: dump single set/all sets
1218 * [IPSET_CB_INDEX]: set index
1219 * [IPSET_CB_ARG0]: type specific
1222 if (cda[IPSET_ATTR_SETNAME]) {
1225 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
1230 dump_type = DUMP_ONE;
1231 cb->args[IPSET_CB_INDEX] = index;
1233 dump_type = DUMP_ALL;
1235 if (cda[IPSET_ATTR_FLAGS]) {
1236 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1237 dump_type |= (f << 16);
1239 cb->args[IPSET_CB_NET] = (unsigned long)inst;
1240 cb->args[IPSET_CB_DUMP] = dump_type;
1246 ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
1248 ip_set_id_t index = IPSET_INVALID_ID, max;
1249 struct ip_set *set = NULL;
1250 struct nlmsghdr *nlh = NULL;
1251 unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0;
1252 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1253 u32 dump_type, dump_flags;
1256 if (!cb->args[IPSET_CB_DUMP]) {
1257 ret = dump_init(cb, inst);
1259 nlh = nlmsg_hdr(cb->skb);
1260 /* We have to create and send the error message
1262 if (nlh->nlmsg_flags & NLM_F_ACK)
1263 netlink_ack(cb->skb, nlh, ret);
1268 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
1271 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1272 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1273 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1276 pr_debug("dump type, flag: %u %u index: %ld\n",
1277 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1278 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
1279 index = (ip_set_id_t) cb->args[IPSET_CB_INDEX];
1280 set = ip_set(inst, index);
1282 if (dump_type == DUMP_ONE) {
1288 /* When dumping all sets, we must dump "sorted"
1289 * so that lists (unions of sets) are dumped last.
1291 if (dump_type != DUMP_ONE &&
1292 ((dump_type == DUMP_ALL) ==
1293 !!(set->type->features & IPSET_DUMP_LAST)))
1295 pr_debug("List set: %s\n", set->name);
1296 if (!cb->args[IPSET_CB_ARG0]) {
1297 /* Start listing: make sure set won't be destroyed */
1298 pr_debug("reference set\n");
1301 nlh = start_msg(skb, NETLINK_CB(cb->skb).portid,
1302 cb->nlh->nlmsg_seq, flags,
1306 goto release_refcount;
1308 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1309 nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
1310 goto nla_put_failure;
1311 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1313 switch (cb->args[IPSET_CB_ARG0]) {
1315 /* Core header data */
1316 if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
1318 nla_put_u8(skb, IPSET_ATTR_FAMILY,
1320 nla_put_u8(skb, IPSET_ATTR_REVISION,
1322 goto nla_put_failure;
1323 ret = set->variant->head(set, skb);
1325 goto release_refcount;
1326 if (dump_flags & IPSET_FLAG_LIST_HEADER)
1328 /* Fall through and add elements */
1330 read_lock_bh(&set->lock);
1331 ret = set->variant->list(set, skb, cb);
1332 read_unlock_bh(&set->lock);
1333 if (!cb->args[IPSET_CB_ARG0])
1334 /* Set is done, proceed with next one */
1336 goto release_refcount;
1339 /* If we dump all sets, continue with dumping last ones */
1340 if (dump_type == DUMP_ALL) {
1341 dump_type = DUMP_LAST;
1342 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1343 cb->args[IPSET_CB_INDEX] = 0;
1351 if (dump_type == DUMP_ONE)
1352 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
1354 cb->args[IPSET_CB_INDEX]++;
1356 /* If there was an error or set is done, release set */
1357 if (ret || !cb->args[IPSET_CB_ARG0]) {
1358 pr_debug("release set %s\n", ip_set(inst, index)->name);
1359 __ip_set_put_byindex(inst, index);
1360 cb->args[IPSET_CB_ARG0] = 0;
1364 nlmsg_end(skb, nlh);
1365 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1369 return ret < 0 ? ret : skb->len;
1373 ip_set_dump(struct sock *ctnl, struct sk_buff *skb,
1374 const struct nlmsghdr *nlh,
1375 const struct nlattr * const attr[])
1377 if (unlikely(protocol_failed(attr)))
1378 return -IPSET_ERR_PROTOCOL;
1381 struct netlink_dump_control c = {
1382 .dump = ip_set_dump_start,
1383 .done = ip_set_dump_done,
1385 return netlink_dump_start(ctnl, skb, nlh, &c);
1389 /* Add, del and test */
1391 static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1392 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1393 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1394 .len = IPSET_MAXNAMELEN - 1 },
1395 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
1396 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1397 [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
1401 call_ad(struct sock *ctnl, struct sk_buff *skb, struct ip_set *set,
1402 struct nlattr *tb[], enum ipset_adt adt,
1403 u32 flags, bool use_lineno)
1407 bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
1410 write_lock_bh(&set->lock);
1411 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1412 write_unlock_bh(&set->lock);
1414 } while (ret == -EAGAIN &&
1415 set->variant->resize &&
1416 (ret = set->variant->resize(set, retried)) == 0);
1418 if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1420 if (lineno && use_lineno) {
1421 /* Error in restore/batch mode: send back lineno */
1422 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1423 struct sk_buff *skb2;
1424 struct nlmsgerr *errmsg;
1425 size_t payload = min(SIZE_MAX,
1426 sizeof(*errmsg) + nlmsg_len(nlh));
1427 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1428 struct nlattr *cda[IPSET_ATTR_CMD_MAX+1];
1429 struct nlattr *cmdattr;
1432 skb2 = nlmsg_new(payload, GFP_KERNEL);
1435 rep = __nlmsg_put(skb2, NETLINK_CB(skb).portid,
1436 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1437 errmsg = nlmsg_data(rep);
1438 errmsg->error = ret;
1439 memcpy(&errmsg->msg, nlh, nlh->nlmsg_len);
1440 cmdattr = (void *)&errmsg->msg + min_len;
1442 nla_parse(cda, IPSET_ATTR_CMD_MAX,
1443 cmdattr, nlh->nlmsg_len - min_len,
1446 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1450 netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1451 /* Signal netlink not to send its ACK/errmsg. */
1459 ip_set_uadd(struct sock *ctnl, struct sk_buff *skb,
1460 const struct nlmsghdr *nlh,
1461 const struct nlattr * const attr[])
1463 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1465 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1466 const struct nlattr *nla;
1467 u32 flags = flag_exist(nlh);
1471 if (unlikely(protocol_failed(attr) ||
1472 attr[IPSET_ATTR_SETNAME] == NULL ||
1473 !((attr[IPSET_ATTR_DATA] != NULL) ^
1474 (attr[IPSET_ATTR_ADT] != NULL)) ||
1475 (attr[IPSET_ATTR_DATA] != NULL &&
1476 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1477 (attr[IPSET_ATTR_ADT] != NULL &&
1478 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1479 attr[IPSET_ATTR_LINENO] == NULL))))
1480 return -IPSET_ERR_PROTOCOL;
1482 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1486 use_lineno = !!attr[IPSET_ATTR_LINENO];
1487 if (attr[IPSET_ATTR_DATA]) {
1488 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1489 attr[IPSET_ATTR_DATA],
1490 set->type->adt_policy))
1491 return -IPSET_ERR_PROTOCOL;
1492 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD, flags,
1497 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1498 memset(tb, 0, sizeof(tb));
1499 if (nla_type(nla) != IPSET_ATTR_DATA ||
1500 !flag_nested(nla) ||
1501 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1502 set->type->adt_policy))
1503 return -IPSET_ERR_PROTOCOL;
1504 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD,
1514 ip_set_udel(struct sock *ctnl, struct sk_buff *skb,
1515 const struct nlmsghdr *nlh,
1516 const struct nlattr * const attr[])
1518 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1520 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1521 const struct nlattr *nla;
1522 u32 flags = flag_exist(nlh);
1526 if (unlikely(protocol_failed(attr) ||
1527 attr[IPSET_ATTR_SETNAME] == NULL ||
1528 !((attr[IPSET_ATTR_DATA] != NULL) ^
1529 (attr[IPSET_ATTR_ADT] != NULL)) ||
1530 (attr[IPSET_ATTR_DATA] != NULL &&
1531 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1532 (attr[IPSET_ATTR_ADT] != NULL &&
1533 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1534 attr[IPSET_ATTR_LINENO] == NULL))))
1535 return -IPSET_ERR_PROTOCOL;
1537 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1541 use_lineno = !!attr[IPSET_ATTR_LINENO];
1542 if (attr[IPSET_ATTR_DATA]) {
1543 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1544 attr[IPSET_ATTR_DATA],
1545 set->type->adt_policy))
1546 return -IPSET_ERR_PROTOCOL;
1547 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL, flags,
1552 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1553 memset(tb, 0, sizeof(*tb));
1554 if (nla_type(nla) != IPSET_ATTR_DATA ||
1555 !flag_nested(nla) ||
1556 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1557 set->type->adt_policy))
1558 return -IPSET_ERR_PROTOCOL;
1559 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL,
1569 ip_set_utest(struct sock *ctnl, struct sk_buff *skb,
1570 const struct nlmsghdr *nlh,
1571 const struct nlattr * const attr[])
1573 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1575 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1578 if (unlikely(protocol_failed(attr) ||
1579 attr[IPSET_ATTR_SETNAME] == NULL ||
1580 attr[IPSET_ATTR_DATA] == NULL ||
1581 !flag_nested(attr[IPSET_ATTR_DATA])))
1582 return -IPSET_ERR_PROTOCOL;
1584 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1588 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1589 set->type->adt_policy))
1590 return -IPSET_ERR_PROTOCOL;
1592 read_lock_bh(&set->lock);
1593 ret = set->variant->uadt(set, tb, IPSET_TEST, NULL, 0, 0);
1594 read_unlock_bh(&set->lock);
1595 /* Userspace can't trigger element to be re-added */
1599 return ret > 0 ? 0 : -IPSET_ERR_EXIST;
1602 /* Get headed data of a set */
1605 ip_set_header(struct sock *ctnl, struct sk_buff *skb,
1606 const struct nlmsghdr *nlh,
1607 const struct nlattr * const attr[])
1609 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1610 const struct ip_set *set;
1611 struct sk_buff *skb2;
1612 struct nlmsghdr *nlh2;
1615 if (unlikely(protocol_failed(attr) ||
1616 attr[IPSET_ATTR_SETNAME] == NULL))
1617 return -IPSET_ERR_PROTOCOL;
1619 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1623 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1627 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
1631 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1632 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
1633 nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
1634 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
1635 nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
1636 goto nla_put_failure;
1637 nlmsg_end(skb2, nlh2);
1639 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1646 nlmsg_cancel(skb2, nlh2);
1654 static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1655 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1656 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
1657 .len = IPSET_MAXNAMELEN - 1 },
1658 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
1662 ip_set_type(struct sock *ctnl, struct sk_buff *skb,
1663 const struct nlmsghdr *nlh,
1664 const struct nlattr * const attr[])
1666 struct sk_buff *skb2;
1667 struct nlmsghdr *nlh2;
1668 u8 family, min, max;
1669 const char *typename;
1672 if (unlikely(protocol_failed(attr) ||
1673 attr[IPSET_ATTR_TYPENAME] == NULL ||
1674 attr[IPSET_ATTR_FAMILY] == NULL))
1675 return -IPSET_ERR_PROTOCOL;
1677 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1678 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1679 ret = find_set_type_minmax(typename, family, &min, &max);
1683 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1687 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
1691 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1692 nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
1693 nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
1694 nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
1695 nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
1696 goto nla_put_failure;
1697 nlmsg_end(skb2, nlh2);
1699 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
1700 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1707 nlmsg_cancel(skb2, nlh2);
1713 /* Get protocol version */
1715 static const struct nla_policy
1716 ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1717 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1721 ip_set_protocol(struct sock *ctnl, struct sk_buff *skb,
1722 const struct nlmsghdr *nlh,
1723 const struct nlattr * const attr[])
1725 struct sk_buff *skb2;
1726 struct nlmsghdr *nlh2;
1729 if (unlikely(attr[IPSET_ATTR_PROTOCOL] == NULL))
1730 return -IPSET_ERR_PROTOCOL;
1732 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1736 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
1737 IPSET_CMD_PROTOCOL);
1740 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
1741 goto nla_put_failure;
1742 nlmsg_end(skb2, nlh2);
1744 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1751 nlmsg_cancel(skb2, nlh2);
1757 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
1758 [IPSET_CMD_NONE] = {
1759 .call = ip_set_none,
1760 .attr_count = IPSET_ATTR_CMD_MAX,
1762 [IPSET_CMD_CREATE] = {
1763 .call = ip_set_create,
1764 .attr_count = IPSET_ATTR_CMD_MAX,
1765 .policy = ip_set_create_policy,
1767 [IPSET_CMD_DESTROY] = {
1768 .call = ip_set_destroy,
1769 .attr_count = IPSET_ATTR_CMD_MAX,
1770 .policy = ip_set_setname_policy,
1772 [IPSET_CMD_FLUSH] = {
1773 .call = ip_set_flush,
1774 .attr_count = IPSET_ATTR_CMD_MAX,
1775 .policy = ip_set_setname_policy,
1777 [IPSET_CMD_RENAME] = {
1778 .call = ip_set_rename,
1779 .attr_count = IPSET_ATTR_CMD_MAX,
1780 .policy = ip_set_setname2_policy,
1782 [IPSET_CMD_SWAP] = {
1783 .call = ip_set_swap,
1784 .attr_count = IPSET_ATTR_CMD_MAX,
1785 .policy = ip_set_setname2_policy,
1787 [IPSET_CMD_LIST] = {
1788 .call = ip_set_dump,
1789 .attr_count = IPSET_ATTR_CMD_MAX,
1790 .policy = ip_set_setname_policy,
1792 [IPSET_CMD_SAVE] = {
1793 .call = ip_set_dump,
1794 .attr_count = IPSET_ATTR_CMD_MAX,
1795 .policy = ip_set_setname_policy,
1798 .call = ip_set_uadd,
1799 .attr_count = IPSET_ATTR_CMD_MAX,
1800 .policy = ip_set_adt_policy,
1803 .call = ip_set_udel,
1804 .attr_count = IPSET_ATTR_CMD_MAX,
1805 .policy = ip_set_adt_policy,
1807 [IPSET_CMD_TEST] = {
1808 .call = ip_set_utest,
1809 .attr_count = IPSET_ATTR_CMD_MAX,
1810 .policy = ip_set_adt_policy,
1812 [IPSET_CMD_HEADER] = {
1813 .call = ip_set_header,
1814 .attr_count = IPSET_ATTR_CMD_MAX,
1815 .policy = ip_set_setname_policy,
1817 [IPSET_CMD_TYPE] = {
1818 .call = ip_set_type,
1819 .attr_count = IPSET_ATTR_CMD_MAX,
1820 .policy = ip_set_type_policy,
1822 [IPSET_CMD_PROTOCOL] = {
1823 .call = ip_set_protocol,
1824 .attr_count = IPSET_ATTR_CMD_MAX,
1825 .policy = ip_set_protocol_policy,
1829 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
1831 .subsys_id = NFNL_SUBSYS_IPSET,
1832 .cb_count = IPSET_MSG_MAX,
1833 .cb = ip_set_netlink_subsys_cb,
1836 /* Interface to iptables/ip6tables */
1839 ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
1843 int copylen = *len, ret = 0;
1844 struct net *net = sock_net(sk);
1845 struct ip_set_net *inst = ip_set_pernet(net);
1847 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1849 if (optval != SO_IP_SET)
1851 if (*len < sizeof(unsigned int))
1854 data = vmalloc(*len);
1857 if (copy_from_user(data, user, *len) != 0) {
1861 op = (unsigned int *) data;
1863 if (*op < IP_SET_OP_VERSION) {
1864 /* Check the version at the beginning of operations */
1865 struct ip_set_req_version *req_version = data;
1867 if (*len < sizeof(struct ip_set_req_version)) {
1872 if (req_version->version != IPSET_PROTOCOL) {
1879 case IP_SET_OP_VERSION: {
1880 struct ip_set_req_version *req_version = data;
1882 if (*len != sizeof(struct ip_set_req_version)) {
1887 req_version->version = IPSET_PROTOCOL;
1888 ret = copy_to_user(user, req_version,
1889 sizeof(struct ip_set_req_version));
1892 case IP_SET_OP_GET_BYNAME: {
1893 struct ip_set_req_get_set *req_get = data;
1896 if (*len != sizeof(struct ip_set_req_get_set)) {
1900 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1901 nfnl_lock(NFNL_SUBSYS_IPSET);
1902 find_set_and_id(inst, req_get->set.name, &id);
1903 req_get->set.index = id;
1904 nfnl_unlock(NFNL_SUBSYS_IPSET);
1907 case IP_SET_OP_GET_FNAME: {
1908 struct ip_set_req_get_set_family *req_get = data;
1911 if (*len != sizeof(struct ip_set_req_get_set_family)) {
1915 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1916 nfnl_lock(NFNL_SUBSYS_IPSET);
1917 find_set_and_id(inst, req_get->set.name, &id);
1918 req_get->set.index = id;
1919 if (id != IPSET_INVALID_ID)
1920 req_get->family = ip_set(inst, id)->family;
1921 nfnl_unlock(NFNL_SUBSYS_IPSET);
1924 case IP_SET_OP_GET_BYINDEX: {
1925 struct ip_set_req_get_set *req_get = data;
1928 if (*len != sizeof(struct ip_set_req_get_set) ||
1929 req_get->set.index >= inst->ip_set_max) {
1933 nfnl_lock(NFNL_SUBSYS_IPSET);
1934 set = ip_set(inst, req_get->set.index);
1935 strncpy(req_get->set.name, set ? set->name : "",
1937 nfnl_unlock(NFNL_SUBSYS_IPSET);
1943 } /* end of switch(op) */
1946 ret = copy_to_user(user, data, copylen);
1955 static struct nf_sockopt_ops so_set __read_mostly = {
1957 .get_optmin = SO_IP_SET,
1958 .get_optmax = SO_IP_SET + 1,
1959 .get = &ip_set_sockfn_get,
1960 .owner = THIS_MODULE,
1963 static int __net_init
1964 ip_set_net_init(struct net *net)
1966 struct ip_set_net *inst = ip_set_pernet(net);
1967 struct ip_set **list;
1969 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
1970 if (inst->ip_set_max >= IPSET_INVALID_ID)
1971 inst->ip_set_max = IPSET_INVALID_ID - 1;
1973 list = kzalloc(sizeof(struct ip_set *) * inst->ip_set_max, GFP_KERNEL);
1976 inst->is_deleted = 0;
1977 rcu_assign_pointer(inst->ip_set_list, list);
1981 static void __net_exit
1982 ip_set_net_exit(struct net *net)
1984 struct ip_set_net *inst = ip_set_pernet(net);
1986 struct ip_set *set = NULL;
1989 inst->is_deleted = 1; /* flag for ip_set_nfnl_put */
1991 for (i = 0; i < inst->ip_set_max; i++) {
1992 set = ip_set(inst, i);
1994 ip_set_destroy_set(inst, i);
1996 kfree(rcu_dereference_protected(inst->ip_set_list, 1));
1999 static struct pernet_operations ip_set_net_ops = {
2000 .init = ip_set_net_init,
2001 .exit = ip_set_net_exit,
2002 .id = &ip_set_net_id,
2003 .size = sizeof(struct ip_set_net)
2010 int ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
2012 pr_err("ip_set: cannot register with nfnetlink.\n");
2015 ret = nf_register_sockopt(&so_set);
2017 pr_err("SO_SET registry failed: %d\n", ret);
2018 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2021 ret = register_pernet_subsys(&ip_set_net_ops);
2023 pr_err("ip_set: cannot register pernet_subsys.\n");
2024 nf_unregister_sockopt(&so_set);
2025 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2028 pr_info("ip_set: protocol %u\n", IPSET_PROTOCOL);
2035 unregister_pernet_subsys(&ip_set_net_ops);
2036 nf_unregister_sockopt(&so_set);
2037 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2038 pr_debug("these are the famous last words\n");
2041 module_init(ip_set_init);
2042 module_exit(ip_set_fini);