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_warning("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_warning("ip_set type %s, family %s, revision %u:%u uses "
199 "wrong protocol version %u (want %u)\n",
200 type->name, family_name(type->family),
201 type->revision_min, type->revision_max,
202 type->protocol, IPSET_PROTOCOL);
207 if (find_set_type(type->name, type->family, type->revision_min)) {
209 pr_warning("ip_set type %s, family %s with revision min %u "
210 "already registered!\n", type->name,
211 family_name(type->family), type->revision_min);
215 list_add_rcu(&type->list, &ip_set_type_list);
216 pr_debug("type %s, family %s, revision %u:%u registered.\n",
217 type->name, family_name(type->family),
218 type->revision_min, type->revision_max);
220 ip_set_type_unlock();
223 EXPORT_SYMBOL_GPL(ip_set_type_register);
225 /* Unregister a set type. There's a small race with ip_set_create */
227 ip_set_type_unregister(struct ip_set_type *type)
230 if (!find_set_type(type->name, type->family, type->revision_min)) {
231 pr_warning("ip_set type %s, family %s with revision min %u "
232 "not registered\n", type->name,
233 family_name(type->family), type->revision_min);
236 list_del_rcu(&type->list);
237 pr_debug("type %s, family %s with revision min %u unregistered.\n",
238 type->name, family_name(type->family), type->revision_min);
240 ip_set_type_unlock();
244 EXPORT_SYMBOL_GPL(ip_set_type_unregister);
246 /* Utility functions */
248 ip_set_alloc(size_t size)
250 void *members = NULL;
252 if (size < KMALLOC_MAX_SIZE)
253 members = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
256 pr_debug("%p: allocated with kmalloc\n", members);
260 members = vzalloc(size);
263 pr_debug("%p: allocated with vmalloc\n", members);
267 EXPORT_SYMBOL_GPL(ip_set_alloc);
270 ip_set_free(void *members)
272 pr_debug("%p: free with %s\n", members,
273 is_vmalloc_addr(members) ? "vfree" : "kfree");
276 EXPORT_SYMBOL_GPL(ip_set_free);
279 flag_nested(const struct nlattr *nla)
281 return nla->nla_type & NLA_F_NESTED;
284 static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
285 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
286 [IPSET_ATTR_IPADDR_IPV6] = { .type = NLA_BINARY,
287 .len = sizeof(struct in6_addr) },
291 ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
293 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX+1];
295 if (unlikely(!flag_nested(nla)))
296 return -IPSET_ERR_PROTOCOL;
297 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, ipaddr_policy))
298 return -IPSET_ERR_PROTOCOL;
299 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
300 return -IPSET_ERR_PROTOCOL;
302 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
305 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
308 ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
310 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX+1];
312 if (unlikely(!flag_nested(nla)))
313 return -IPSET_ERR_PROTOCOL;
315 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, ipaddr_policy))
316 return -IPSET_ERR_PROTOCOL;
317 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
318 return -IPSET_ERR_PROTOCOL;
320 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
321 sizeof(struct in6_addr));
324 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
326 typedef void (*destroyer)(void *);
327 /* ipset data extension types, in size order */
329 const struct ip_set_ext_type ip_set_extensions[] = {
330 [IPSET_EXT_ID_COUNTER] = {
331 .type = IPSET_EXT_COUNTER,
332 .flag = IPSET_FLAG_WITH_COUNTERS,
333 .len = sizeof(struct ip_set_counter),
334 .align = __alignof__(struct ip_set_counter),
336 [IPSET_EXT_ID_TIMEOUT] = {
337 .type = IPSET_EXT_TIMEOUT,
338 .len = sizeof(unsigned long),
339 .align = __alignof__(unsigned long),
341 [IPSET_EXT_ID_COMMENT] = {
342 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
343 .flag = IPSET_FLAG_WITH_COMMENT,
344 .len = sizeof(struct ip_set_comment),
345 .align = __alignof__(struct ip_set_comment),
346 .destroy = (destroyer) ip_set_comment_free,
349 EXPORT_SYMBOL_GPL(ip_set_extensions);
352 add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
354 return ip_set_extensions[id].flag ?
355 (flags & ip_set_extensions[id].flag) :
356 !!tb[IPSET_ATTR_TIMEOUT];
360 ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len)
362 enum ip_set_ext_id id;
366 if (tb[IPSET_ATTR_CADT_FLAGS])
367 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
368 if (cadt_flags & IPSET_FLAG_WITH_FORCEADD)
369 set->flags |= IPSET_CREATE_FLAG_FORCEADD;
370 for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
371 if (!add_extension(id, cadt_flags, tb))
373 offset += ALIGN(len + offset, ip_set_extensions[id].align);
374 set->offset[id] = offset;
375 set->extensions |= ip_set_extensions[id].type;
376 offset += ip_set_extensions[id].len;
380 EXPORT_SYMBOL_GPL(ip_set_elem_len);
383 ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
384 struct ip_set_ext *ext)
386 if (tb[IPSET_ATTR_TIMEOUT]) {
387 if (!(set->extensions & IPSET_EXT_TIMEOUT))
388 return -IPSET_ERR_TIMEOUT;
389 ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
391 if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) {
392 if (!(set->extensions & IPSET_EXT_COUNTER))
393 return -IPSET_ERR_COUNTER;
394 if (tb[IPSET_ATTR_BYTES])
395 ext->bytes = be64_to_cpu(nla_get_be64(
396 tb[IPSET_ATTR_BYTES]));
397 if (tb[IPSET_ATTR_PACKETS])
398 ext->packets = be64_to_cpu(nla_get_be64(
399 tb[IPSET_ATTR_PACKETS]));
401 if (tb[IPSET_ATTR_COMMENT]) {
402 if (!(set->extensions & IPSET_EXT_COMMENT))
403 return -IPSET_ERR_COMMENT;
404 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
409 EXPORT_SYMBOL_GPL(ip_set_get_extensions);
412 * Creating/destroying/renaming/swapping affect the existence and
413 * the properties of a set. All of these can be executed from userspace
414 * only and serialized by the nfnl mutex indirectly from nfnetlink.
416 * Sets are identified by their index in ip_set_list and the index
417 * is used by the external references (set/SET netfilter modules).
419 * The set behind an index may change by swapping only, from userspace.
423 __ip_set_get(struct ip_set *set)
425 write_lock_bh(&ip_set_ref_lock);
427 write_unlock_bh(&ip_set_ref_lock);
431 __ip_set_put(struct ip_set *set)
433 write_lock_bh(&ip_set_ref_lock);
434 BUG_ON(set->ref == 0);
436 write_unlock_bh(&ip_set_ref_lock);
440 * Add, del and test set entries from kernel.
442 * The set behind the index must exist and must be referenced
443 * so it can't be destroyed (or changed) under our foot.
446 static inline struct ip_set *
447 ip_set_rcu_get(struct net *net, ip_set_id_t index)
450 struct ip_set_net *inst = ip_set_pernet(net);
453 /* ip_set_list itself needs to be protected */
454 set = rcu_dereference(inst->ip_set_list)[index];
461 ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
462 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
464 struct ip_set *set = ip_set_rcu_get(
465 dev_net(par->in ? par->in : par->out), index);
469 pr_debug("set %s, index %u\n", set->name, index);
471 if (opt->dim < set->type->dimension ||
472 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
475 read_lock_bh(&set->lock);
476 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
477 read_unlock_bh(&set->lock);
479 if (ret == -EAGAIN) {
480 /* Type requests element to be completed */
481 pr_debug("element must be competed, ADD is triggered\n");
482 write_lock_bh(&set->lock);
483 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
484 write_unlock_bh(&set->lock);
487 /* --return-nomatch: invert matched element */
488 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
489 (set->type->features & IPSET_TYPE_NOMATCH) &&
490 (ret > 0 || ret == -ENOTEMPTY))
494 /* Convert error codes to nomatch */
495 return (ret < 0 ? 0 : ret);
497 EXPORT_SYMBOL_GPL(ip_set_test);
500 ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
501 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
503 struct ip_set *set = ip_set_rcu_get(
504 dev_net(par->in ? par->in : par->out), index);
508 pr_debug("set %s, index %u\n", set->name, index);
510 if (opt->dim < set->type->dimension ||
511 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
512 return -IPSET_ERR_TYPE_MISMATCH;
514 write_lock_bh(&set->lock);
515 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
516 write_unlock_bh(&set->lock);
520 EXPORT_SYMBOL_GPL(ip_set_add);
523 ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
524 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
526 struct ip_set *set = ip_set_rcu_get(
527 dev_net(par->in ? par->in : par->out), index);
531 pr_debug("set %s, index %u\n", set->name, index);
533 if (opt->dim < set->type->dimension ||
534 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
535 return -IPSET_ERR_TYPE_MISMATCH;
537 write_lock_bh(&set->lock);
538 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
539 write_unlock_bh(&set->lock);
543 EXPORT_SYMBOL_GPL(ip_set_del);
546 * Find set by name, reference it once. The reference makes sure the
547 * thing pointed to, does not go away under our feet.
551 ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
553 ip_set_id_t i, index = IPSET_INVALID_ID;
555 struct ip_set_net *inst = ip_set_pernet(net);
558 for (i = 0; i < inst->ip_set_max; i++) {
559 s = rcu_dereference(inst->ip_set_list)[i];
560 if (s != NULL && STREQ(s->name, name)) {
571 EXPORT_SYMBOL_GPL(ip_set_get_byname);
574 * If the given set pointer points to a valid set, decrement
575 * reference count by 1. The caller shall not assume the index
576 * to be valid, after calling this function.
581 __ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
586 set = rcu_dereference(inst->ip_set_list)[index];
593 ip_set_put_byindex(struct net *net, ip_set_id_t index)
595 struct ip_set_net *inst = ip_set_pernet(net);
597 __ip_set_put_byindex(inst, index);
599 EXPORT_SYMBOL_GPL(ip_set_put_byindex);
602 * Get the name of a set behind a set index.
603 * We assume the set is referenced, so it does exist and
604 * can't be destroyed. The set cannot be renamed due to
605 * the referencing either.
609 ip_set_name_byindex(struct net *net, ip_set_id_t index)
611 const struct ip_set *set = ip_set_rcu_get(net, index);
614 BUG_ON(set->ref == 0);
616 /* Referenced, so it's safe */
619 EXPORT_SYMBOL_GPL(ip_set_name_byindex);
622 * Routines to call by external subsystems, which do not
623 * call nfnl_lock for us.
627 * Find set by index, reference it once. The reference makes sure the
628 * thing pointed to, does not go away under our feet.
630 * The nfnl mutex is used in the function.
633 ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
636 struct ip_set_net *inst = ip_set_pernet(net);
638 if (index > inst->ip_set_max)
639 return IPSET_INVALID_ID;
641 nfnl_lock(NFNL_SUBSYS_IPSET);
642 set = ip_set(inst, index);
646 index = IPSET_INVALID_ID;
647 nfnl_unlock(NFNL_SUBSYS_IPSET);
651 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
654 * If the given set pointer points to a valid set, decrement
655 * reference count by 1. The caller shall not assume the index
656 * to be valid, after calling this function.
658 * The nfnl mutex is used in the function.
661 ip_set_nfnl_put(struct net *net, ip_set_id_t index)
664 struct ip_set_net *inst = ip_set_pernet(net);
666 nfnl_lock(NFNL_SUBSYS_IPSET);
667 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
668 set = ip_set(inst, index);
672 nfnl_unlock(NFNL_SUBSYS_IPSET);
674 EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
677 * Communication protocol with userspace over netlink.
679 * The commands are serialized by the nfnl mutex.
683 protocol_failed(const struct nlattr * const tb[])
685 return !tb[IPSET_ATTR_PROTOCOL] ||
686 nla_get_u8(tb[IPSET_ATTR_PROTOCOL]) != IPSET_PROTOCOL;
690 flag_exist(const struct nlmsghdr *nlh)
692 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
695 static struct nlmsghdr *
696 start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
699 struct nlmsghdr *nlh;
700 struct nfgenmsg *nfmsg;
702 nlh = nlmsg_put(skb, portid, seq, cmd | (NFNL_SUBSYS_IPSET << 8),
703 sizeof(*nfmsg), flags);
707 nfmsg = nlmsg_data(nlh);
708 nfmsg->nfgen_family = NFPROTO_IPV4;
709 nfmsg->version = NFNETLINK_V0;
717 static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
718 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
719 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
720 .len = IPSET_MAXNAMELEN - 1 },
721 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
722 .len = IPSET_MAXNAMELEN - 1},
723 [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
724 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
725 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
728 static struct ip_set *
729 find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
731 struct ip_set *set = NULL;
734 *id = IPSET_INVALID_ID;
735 for (i = 0; i < inst->ip_set_max; i++) {
736 set = ip_set(inst, i);
737 if (set != NULL && STREQ(set->name, name)) {
742 return (*id == IPSET_INVALID_ID ? NULL : set);
745 static inline struct ip_set *
746 find_set(struct ip_set_net *inst, const char *name)
750 return find_set_and_id(inst, name, &id);
754 find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
760 *index = IPSET_INVALID_ID;
761 for (i = 0; i < inst->ip_set_max; i++) {
764 if (*index == IPSET_INVALID_ID)
766 } else if (STREQ(name, s->name)) {
772 if (*index == IPSET_INVALID_ID)
773 /* No free slot remained */
774 return -IPSET_ERR_MAX_SETS;
779 ip_set_none(struct sock *ctnl, struct sk_buff *skb,
780 const struct nlmsghdr *nlh,
781 const struct nlattr * const attr[])
787 ip_set_create(struct sock *ctnl, struct sk_buff *skb,
788 const struct nlmsghdr *nlh,
789 const struct nlattr * const attr[])
791 struct net *net = sock_net(ctnl);
792 struct ip_set_net *inst = ip_set_pernet(net);
793 struct ip_set *set, *clash = NULL;
794 ip_set_id_t index = IPSET_INVALID_ID;
795 struct nlattr *tb[IPSET_ATTR_CREATE_MAX+1] = {};
796 const char *name, *typename;
798 u32 flags = flag_exist(nlh);
801 if (unlikely(protocol_failed(attr) ||
802 attr[IPSET_ATTR_SETNAME] == NULL ||
803 attr[IPSET_ATTR_TYPENAME] == NULL ||
804 attr[IPSET_ATTR_REVISION] == NULL ||
805 attr[IPSET_ATTR_FAMILY] == NULL ||
806 (attr[IPSET_ATTR_DATA] != NULL &&
807 !flag_nested(attr[IPSET_ATTR_DATA]))))
808 return -IPSET_ERR_PROTOCOL;
810 name = nla_data(attr[IPSET_ATTR_SETNAME]);
811 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
812 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
813 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
814 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
815 name, typename, family_name(family), revision);
818 * First, and without any locks, allocate and initialize
819 * a normal base set structure.
821 set = kzalloc(sizeof(struct ip_set), GFP_KERNEL);
824 rwlock_init(&set->lock);
825 strlcpy(set->name, name, IPSET_MAXNAMELEN);
826 set->family = family;
827 set->revision = revision;
830 * Next, check that we know the type, and take
831 * a reference on the type, to make sure it stays available
832 * while constructing our new set.
834 * After referencing the type, we try to create the type
835 * specific part of the set without holding any locks.
837 ret = find_set_type_get(typename, family, revision, &(set->type));
842 * Without holding any locks, create private part.
844 if (attr[IPSET_ATTR_DATA] &&
845 nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
846 set->type->create_policy)) {
847 ret = -IPSET_ERR_PROTOCOL;
851 ret = set->type->create(net, set, tb, flags);
855 /* BTW, ret==0 here. */
858 * Here, we have a valid, constructed set and we are protected
859 * by the nfnl mutex. Find the first free index in ip_set_list
860 * and check clashing.
862 ret = find_free_id(inst, set->name, &index, &clash);
863 if (ret == -EEXIST) {
864 /* If this is the same set and requested, ignore error */
865 if ((flags & IPSET_FLAG_EXIST) &&
866 STREQ(set->type->name, clash->type->name) &&
867 set->type->family == clash->type->family &&
868 set->type->revision_min == clash->type->revision_min &&
869 set->type->revision_max == clash->type->revision_max &&
870 set->variant->same_set(set, clash))
873 } else if (ret == -IPSET_ERR_MAX_SETS) {
874 struct ip_set **list, **tmp;
875 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
877 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
881 list = kzalloc(sizeof(struct ip_set *) * i, GFP_KERNEL);
884 /* nfnl mutex is held, both lists are valid */
885 tmp = ip_set_dereference(inst->ip_set_list);
886 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
887 rcu_assign_pointer(inst->ip_set_list, list);
888 /* Make sure all current packets have passed through */
891 index = inst->ip_set_max;
892 inst->ip_set_max = i;
899 * Finally! Add our shiny new set to the list, and be done.
901 pr_debug("create: '%s' created with index %u!\n", set->name, index);
902 ip_set(inst, index) = set;
907 set->variant->destroy(set);
909 module_put(set->type->me);
917 static const struct nla_policy
918 ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
919 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
920 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
921 .len = IPSET_MAXNAMELEN - 1 },
925 ip_set_destroy_set(struct ip_set_net *inst, ip_set_id_t index)
927 struct ip_set *set = ip_set(inst, index);
929 pr_debug("set: %s\n", set->name);
930 ip_set(inst, index) = NULL;
932 /* Must call it without holding any lock */
933 set->variant->destroy(set);
934 module_put(set->type->me);
939 ip_set_destroy(struct sock *ctnl, struct sk_buff *skb,
940 const struct nlmsghdr *nlh,
941 const struct nlattr * const attr[])
943 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
948 if (unlikely(protocol_failed(attr)))
949 return -IPSET_ERR_PROTOCOL;
951 /* Commands are serialized and references are
952 * protected by the ip_set_ref_lock.
953 * External systems (i.e. xt_set) must call
954 * ip_set_put|get_nfnl_* functions, that way we
955 * can safely check references here.
957 * list:set timer can only decrement the reference
958 * counter, so if it's already zero, we can proceed
959 * without holding the lock.
961 read_lock_bh(&ip_set_ref_lock);
962 if (!attr[IPSET_ATTR_SETNAME]) {
963 for (i = 0; i < inst->ip_set_max; i++) {
965 if (s != NULL && s->ref) {
966 ret = -IPSET_ERR_BUSY;
970 read_unlock_bh(&ip_set_ref_lock);
971 for (i = 0; i < inst->ip_set_max; i++) {
974 ip_set_destroy_set(inst, i);
977 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
983 ret = -IPSET_ERR_BUSY;
986 read_unlock_bh(&ip_set_ref_lock);
988 ip_set_destroy_set(inst, i);
992 read_unlock_bh(&ip_set_ref_lock);
999 ip_set_flush_set(struct ip_set *set)
1001 pr_debug("set: %s\n", set->name);
1003 write_lock_bh(&set->lock);
1004 set->variant->flush(set);
1005 write_unlock_bh(&set->lock);
1009 ip_set_flush(struct sock *ctnl, struct sk_buff *skb,
1010 const struct nlmsghdr *nlh,
1011 const struct nlattr * const attr[])
1013 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1017 if (unlikely(protocol_failed(attr)))
1018 return -IPSET_ERR_PROTOCOL;
1020 if (!attr[IPSET_ATTR_SETNAME]) {
1021 for (i = 0; i < inst->ip_set_max; i++) {
1022 s = ip_set(inst, i);
1024 ip_set_flush_set(s);
1027 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1031 ip_set_flush_set(s);
1039 static const struct nla_policy
1040 ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
1041 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1042 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1043 .len = IPSET_MAXNAMELEN - 1 },
1044 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
1045 .len = IPSET_MAXNAMELEN - 1 },
1049 ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
1050 const struct nlmsghdr *nlh,
1051 const struct nlattr * const attr[])
1053 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1054 struct ip_set *set, *s;
1059 if (unlikely(protocol_failed(attr) ||
1060 attr[IPSET_ATTR_SETNAME] == NULL ||
1061 attr[IPSET_ATTR_SETNAME2] == NULL))
1062 return -IPSET_ERR_PROTOCOL;
1064 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1068 read_lock_bh(&ip_set_ref_lock);
1069 if (set->ref != 0) {
1070 ret = -IPSET_ERR_REFERENCED;
1074 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1075 for (i = 0; i < inst->ip_set_max; i++) {
1076 s = ip_set(inst, i);
1077 if (s != NULL && STREQ(s->name, name2)) {
1078 ret = -IPSET_ERR_EXIST_SETNAME2;
1082 strncpy(set->name, name2, IPSET_MAXNAMELEN);
1085 read_unlock_bh(&ip_set_ref_lock);
1089 /* Swap two sets so that name/index points to the other.
1090 * References and set names are also swapped.
1092 * The commands are serialized by the nfnl mutex and references are
1093 * protected by the ip_set_ref_lock. The kernel interfaces
1094 * do not hold the mutex but the pointer settings are atomic
1095 * so the ip_set_list always contains valid pointers to the sets.
1099 ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
1100 const struct nlmsghdr *nlh,
1101 const struct nlattr * const attr[])
1103 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1104 struct ip_set *from, *to;
1105 ip_set_id_t from_id, to_id;
1106 char from_name[IPSET_MAXNAMELEN];
1108 if (unlikely(protocol_failed(attr) ||
1109 attr[IPSET_ATTR_SETNAME] == NULL ||
1110 attr[IPSET_ATTR_SETNAME2] == NULL))
1111 return -IPSET_ERR_PROTOCOL;
1113 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1118 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1121 return -IPSET_ERR_EXIST_SETNAME2;
1123 /* Features must not change.
1124 * Not an artificial restriction anymore, as we must prevent
1125 * possible loops created by swapping in setlist type of sets. */
1126 if (!(from->type->features == to->type->features &&
1127 from->family == to->family))
1128 return -IPSET_ERR_TYPE_MISMATCH;
1130 strncpy(from_name, from->name, IPSET_MAXNAMELEN);
1131 strncpy(from->name, to->name, IPSET_MAXNAMELEN);
1132 strncpy(to->name, from_name, IPSET_MAXNAMELEN);
1134 write_lock_bh(&ip_set_ref_lock);
1135 swap(from->ref, to->ref);
1136 ip_set(inst, from_id) = to;
1137 ip_set(inst, to_id) = from;
1138 write_unlock_bh(&ip_set_ref_lock);
1143 /* List/save set data */
1150 #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
1151 #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
1154 ip_set_dump_done(struct netlink_callback *cb)
1156 struct ip_set_net *inst = (struct ip_set_net *)cb->args[IPSET_CB_NET];
1157 if (cb->args[IPSET_CB_ARG0]) {
1158 pr_debug("release set %s\n",
1159 ip_set(inst, cb->args[IPSET_CB_INDEX])->name);
1160 __ip_set_put_byindex(inst,
1161 (ip_set_id_t) cb->args[IPSET_CB_INDEX]);
1167 dump_attrs(struct nlmsghdr *nlh)
1169 const struct nlattr *attr;
1172 pr_debug("dump nlmsg\n");
1173 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
1174 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
1179 dump_init(struct netlink_callback *cb, struct ip_set_net *inst)
1181 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
1182 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1183 struct nlattr *cda[IPSET_ATTR_CMD_MAX+1];
1184 struct nlattr *attr = (void *)nlh + min_len;
1188 /* Second pass, so parser can't fail */
1189 nla_parse(cda, IPSET_ATTR_CMD_MAX,
1190 attr, nlh->nlmsg_len - min_len, ip_set_setname_policy);
1192 /* cb->args[IPSET_CB_NET]: net namespace
1193 * [IPSET_CB_DUMP]: dump single set/all sets
1194 * [IPSET_CB_INDEX]: set index
1195 * [IPSET_CB_ARG0]: type specific
1198 if (cda[IPSET_ATTR_SETNAME]) {
1201 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
1206 dump_type = DUMP_ONE;
1207 cb->args[IPSET_CB_INDEX] = index;
1209 dump_type = DUMP_ALL;
1211 if (cda[IPSET_ATTR_FLAGS]) {
1212 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1213 dump_type |= (f << 16);
1215 cb->args[IPSET_CB_NET] = (unsigned long)inst;
1216 cb->args[IPSET_CB_DUMP] = dump_type;
1222 ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
1224 ip_set_id_t index = IPSET_INVALID_ID, max;
1225 struct ip_set *set = NULL;
1226 struct nlmsghdr *nlh = NULL;
1227 unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0;
1228 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1229 u32 dump_type, dump_flags;
1232 if (!cb->args[IPSET_CB_DUMP]) {
1233 ret = dump_init(cb, inst);
1235 nlh = nlmsg_hdr(cb->skb);
1236 /* We have to create and send the error message
1238 if (nlh->nlmsg_flags & NLM_F_ACK)
1239 netlink_ack(cb->skb, nlh, ret);
1244 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
1247 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1248 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1249 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1252 pr_debug("dump type, flag: %u %u index: %ld\n",
1253 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1254 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
1255 index = (ip_set_id_t) cb->args[IPSET_CB_INDEX];
1256 set = ip_set(inst, index);
1258 if (dump_type == DUMP_ONE) {
1264 /* When dumping all sets, we must dump "sorted"
1265 * so that lists (unions of sets) are dumped last.
1267 if (dump_type != DUMP_ONE &&
1268 ((dump_type == DUMP_ALL) ==
1269 !!(set->type->features & IPSET_DUMP_LAST)))
1271 pr_debug("List set: %s\n", set->name);
1272 if (!cb->args[IPSET_CB_ARG0]) {
1273 /* Start listing: make sure set won't be destroyed */
1274 pr_debug("reference set\n");
1277 nlh = start_msg(skb, NETLINK_CB(cb->skb).portid,
1278 cb->nlh->nlmsg_seq, flags,
1282 goto release_refcount;
1284 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1285 nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
1286 goto nla_put_failure;
1287 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1289 switch (cb->args[IPSET_CB_ARG0]) {
1291 /* Core header data */
1292 if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
1294 nla_put_u8(skb, IPSET_ATTR_FAMILY,
1296 nla_put_u8(skb, IPSET_ATTR_REVISION,
1298 goto nla_put_failure;
1299 ret = set->variant->head(set, skb);
1301 goto release_refcount;
1302 if (dump_flags & IPSET_FLAG_LIST_HEADER)
1304 /* Fall through and add elements */
1306 read_lock_bh(&set->lock);
1307 ret = set->variant->list(set, skb, cb);
1308 read_unlock_bh(&set->lock);
1309 if (!cb->args[IPSET_CB_ARG0])
1310 /* Set is done, proceed with next one */
1312 goto release_refcount;
1315 /* If we dump all sets, continue with dumping last ones */
1316 if (dump_type == DUMP_ALL) {
1317 dump_type = DUMP_LAST;
1318 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1319 cb->args[IPSET_CB_INDEX] = 0;
1327 if (dump_type == DUMP_ONE)
1328 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
1330 cb->args[IPSET_CB_INDEX]++;
1332 /* If there was an error or set is done, release set */
1333 if (ret || !cb->args[IPSET_CB_ARG0]) {
1334 pr_debug("release set %s\n", ip_set(inst, index)->name);
1335 __ip_set_put_byindex(inst, index);
1336 cb->args[IPSET_CB_ARG0] = 0;
1340 nlmsg_end(skb, nlh);
1341 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1345 return ret < 0 ? ret : skb->len;
1349 ip_set_dump(struct sock *ctnl, struct sk_buff *skb,
1350 const struct nlmsghdr *nlh,
1351 const struct nlattr * const attr[])
1353 if (unlikely(protocol_failed(attr)))
1354 return -IPSET_ERR_PROTOCOL;
1357 struct netlink_dump_control c = {
1358 .dump = ip_set_dump_start,
1359 .done = ip_set_dump_done,
1361 return netlink_dump_start(ctnl, skb, nlh, &c);
1365 /* Add, del and test */
1367 static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1368 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1369 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1370 .len = IPSET_MAXNAMELEN - 1 },
1371 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
1372 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1373 [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
1377 call_ad(struct sock *ctnl, struct sk_buff *skb, struct ip_set *set,
1378 struct nlattr *tb[], enum ipset_adt adt,
1379 u32 flags, bool use_lineno)
1383 bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
1386 write_lock_bh(&set->lock);
1387 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1388 write_unlock_bh(&set->lock);
1390 } while (ret == -EAGAIN &&
1391 set->variant->resize &&
1392 (ret = set->variant->resize(set, retried)) == 0);
1394 if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1396 if (lineno && use_lineno) {
1397 /* Error in restore/batch mode: send back lineno */
1398 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1399 struct sk_buff *skb2;
1400 struct nlmsgerr *errmsg;
1401 size_t payload = sizeof(*errmsg) + nlmsg_len(nlh);
1402 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1403 struct nlattr *cda[IPSET_ATTR_CMD_MAX+1];
1404 struct nlattr *cmdattr;
1407 skb2 = nlmsg_new(payload, GFP_KERNEL);
1410 rep = __nlmsg_put(skb2, NETLINK_CB(skb).portid,
1411 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1412 errmsg = nlmsg_data(rep);
1413 errmsg->error = ret;
1414 memcpy(&errmsg->msg, nlh, nlh->nlmsg_len);
1415 cmdattr = (void *)&errmsg->msg + min_len;
1417 nla_parse(cda, IPSET_ATTR_CMD_MAX,
1418 cmdattr, nlh->nlmsg_len - min_len,
1421 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1425 netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1426 /* Signal netlink not to send its ACK/errmsg. */
1434 ip_set_uadd(struct sock *ctnl, struct sk_buff *skb,
1435 const struct nlmsghdr *nlh,
1436 const struct nlattr * const attr[])
1438 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1440 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1441 const struct nlattr *nla;
1442 u32 flags = flag_exist(nlh);
1446 if (unlikely(protocol_failed(attr) ||
1447 attr[IPSET_ATTR_SETNAME] == NULL ||
1448 !((attr[IPSET_ATTR_DATA] != NULL) ^
1449 (attr[IPSET_ATTR_ADT] != NULL)) ||
1450 (attr[IPSET_ATTR_DATA] != NULL &&
1451 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1452 (attr[IPSET_ATTR_ADT] != NULL &&
1453 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1454 attr[IPSET_ATTR_LINENO] == NULL))))
1455 return -IPSET_ERR_PROTOCOL;
1457 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1461 use_lineno = !!attr[IPSET_ATTR_LINENO];
1462 if (attr[IPSET_ATTR_DATA]) {
1463 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1464 attr[IPSET_ATTR_DATA],
1465 set->type->adt_policy))
1466 return -IPSET_ERR_PROTOCOL;
1467 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD, flags,
1472 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1473 memset(tb, 0, sizeof(tb));
1474 if (nla_type(nla) != IPSET_ATTR_DATA ||
1475 !flag_nested(nla) ||
1476 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1477 set->type->adt_policy))
1478 return -IPSET_ERR_PROTOCOL;
1479 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD,
1489 ip_set_udel(struct sock *ctnl, struct sk_buff *skb,
1490 const struct nlmsghdr *nlh,
1491 const struct nlattr * const attr[])
1493 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1495 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1496 const struct nlattr *nla;
1497 u32 flags = flag_exist(nlh);
1501 if (unlikely(protocol_failed(attr) ||
1502 attr[IPSET_ATTR_SETNAME] == NULL ||
1503 !((attr[IPSET_ATTR_DATA] != NULL) ^
1504 (attr[IPSET_ATTR_ADT] != NULL)) ||
1505 (attr[IPSET_ATTR_DATA] != NULL &&
1506 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1507 (attr[IPSET_ATTR_ADT] != NULL &&
1508 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1509 attr[IPSET_ATTR_LINENO] == NULL))))
1510 return -IPSET_ERR_PROTOCOL;
1512 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1516 use_lineno = !!attr[IPSET_ATTR_LINENO];
1517 if (attr[IPSET_ATTR_DATA]) {
1518 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1519 attr[IPSET_ATTR_DATA],
1520 set->type->adt_policy))
1521 return -IPSET_ERR_PROTOCOL;
1522 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL, flags,
1527 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1528 memset(tb, 0, sizeof(*tb));
1529 if (nla_type(nla) != IPSET_ATTR_DATA ||
1530 !flag_nested(nla) ||
1531 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1532 set->type->adt_policy))
1533 return -IPSET_ERR_PROTOCOL;
1534 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL,
1544 ip_set_utest(struct sock *ctnl, struct sk_buff *skb,
1545 const struct nlmsghdr *nlh,
1546 const struct nlattr * const attr[])
1548 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1550 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1553 if (unlikely(protocol_failed(attr) ||
1554 attr[IPSET_ATTR_SETNAME] == NULL ||
1555 attr[IPSET_ATTR_DATA] == NULL ||
1556 !flag_nested(attr[IPSET_ATTR_DATA])))
1557 return -IPSET_ERR_PROTOCOL;
1559 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1563 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1564 set->type->adt_policy))
1565 return -IPSET_ERR_PROTOCOL;
1567 read_lock_bh(&set->lock);
1568 ret = set->variant->uadt(set, tb, IPSET_TEST, NULL, 0, 0);
1569 read_unlock_bh(&set->lock);
1570 /* Userspace can't trigger element to be re-added */
1574 return ret > 0 ? 0 : -IPSET_ERR_EXIST;
1577 /* Get headed data of a set */
1580 ip_set_header(struct sock *ctnl, struct sk_buff *skb,
1581 const struct nlmsghdr *nlh,
1582 const struct nlattr * const attr[])
1584 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1585 const struct ip_set *set;
1586 struct sk_buff *skb2;
1587 struct nlmsghdr *nlh2;
1590 if (unlikely(protocol_failed(attr) ||
1591 attr[IPSET_ATTR_SETNAME] == NULL))
1592 return -IPSET_ERR_PROTOCOL;
1594 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1598 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1602 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
1606 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1607 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
1608 nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
1609 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
1610 nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
1611 goto nla_put_failure;
1612 nlmsg_end(skb2, nlh2);
1614 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1621 nlmsg_cancel(skb2, nlh2);
1629 static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1630 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1631 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
1632 .len = IPSET_MAXNAMELEN - 1 },
1633 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
1637 ip_set_type(struct sock *ctnl, struct sk_buff *skb,
1638 const struct nlmsghdr *nlh,
1639 const struct nlattr * const attr[])
1641 struct sk_buff *skb2;
1642 struct nlmsghdr *nlh2;
1643 u8 family, min, max;
1644 const char *typename;
1647 if (unlikely(protocol_failed(attr) ||
1648 attr[IPSET_ATTR_TYPENAME] == NULL ||
1649 attr[IPSET_ATTR_FAMILY] == NULL))
1650 return -IPSET_ERR_PROTOCOL;
1652 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1653 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1654 ret = find_set_type_minmax(typename, family, &min, &max);
1658 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1662 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
1666 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1667 nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
1668 nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
1669 nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
1670 nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
1671 goto nla_put_failure;
1672 nlmsg_end(skb2, nlh2);
1674 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
1675 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1682 nlmsg_cancel(skb2, nlh2);
1688 /* Get protocol version */
1690 static const struct nla_policy
1691 ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1692 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1696 ip_set_protocol(struct sock *ctnl, struct sk_buff *skb,
1697 const struct nlmsghdr *nlh,
1698 const struct nlattr * const attr[])
1700 struct sk_buff *skb2;
1701 struct nlmsghdr *nlh2;
1704 if (unlikely(attr[IPSET_ATTR_PROTOCOL] == NULL))
1705 return -IPSET_ERR_PROTOCOL;
1707 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1711 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
1712 IPSET_CMD_PROTOCOL);
1715 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
1716 goto nla_put_failure;
1717 nlmsg_end(skb2, nlh2);
1719 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1726 nlmsg_cancel(skb2, nlh2);
1732 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
1733 [IPSET_CMD_NONE] = {
1734 .call = ip_set_none,
1735 .attr_count = IPSET_ATTR_CMD_MAX,
1737 [IPSET_CMD_CREATE] = {
1738 .call = ip_set_create,
1739 .attr_count = IPSET_ATTR_CMD_MAX,
1740 .policy = ip_set_create_policy,
1742 [IPSET_CMD_DESTROY] = {
1743 .call = ip_set_destroy,
1744 .attr_count = IPSET_ATTR_CMD_MAX,
1745 .policy = ip_set_setname_policy,
1747 [IPSET_CMD_FLUSH] = {
1748 .call = ip_set_flush,
1749 .attr_count = IPSET_ATTR_CMD_MAX,
1750 .policy = ip_set_setname_policy,
1752 [IPSET_CMD_RENAME] = {
1753 .call = ip_set_rename,
1754 .attr_count = IPSET_ATTR_CMD_MAX,
1755 .policy = ip_set_setname2_policy,
1757 [IPSET_CMD_SWAP] = {
1758 .call = ip_set_swap,
1759 .attr_count = IPSET_ATTR_CMD_MAX,
1760 .policy = ip_set_setname2_policy,
1762 [IPSET_CMD_LIST] = {
1763 .call = ip_set_dump,
1764 .attr_count = IPSET_ATTR_CMD_MAX,
1765 .policy = ip_set_setname_policy,
1767 [IPSET_CMD_SAVE] = {
1768 .call = ip_set_dump,
1769 .attr_count = IPSET_ATTR_CMD_MAX,
1770 .policy = ip_set_setname_policy,
1773 .call = ip_set_uadd,
1774 .attr_count = IPSET_ATTR_CMD_MAX,
1775 .policy = ip_set_adt_policy,
1778 .call = ip_set_udel,
1779 .attr_count = IPSET_ATTR_CMD_MAX,
1780 .policy = ip_set_adt_policy,
1782 [IPSET_CMD_TEST] = {
1783 .call = ip_set_utest,
1784 .attr_count = IPSET_ATTR_CMD_MAX,
1785 .policy = ip_set_adt_policy,
1787 [IPSET_CMD_HEADER] = {
1788 .call = ip_set_header,
1789 .attr_count = IPSET_ATTR_CMD_MAX,
1790 .policy = ip_set_setname_policy,
1792 [IPSET_CMD_TYPE] = {
1793 .call = ip_set_type,
1794 .attr_count = IPSET_ATTR_CMD_MAX,
1795 .policy = ip_set_type_policy,
1797 [IPSET_CMD_PROTOCOL] = {
1798 .call = ip_set_protocol,
1799 .attr_count = IPSET_ATTR_CMD_MAX,
1800 .policy = ip_set_protocol_policy,
1804 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
1806 .subsys_id = NFNL_SUBSYS_IPSET,
1807 .cb_count = IPSET_MSG_MAX,
1808 .cb = ip_set_netlink_subsys_cb,
1811 /* Interface to iptables/ip6tables */
1814 ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
1818 int copylen = *len, ret = 0;
1819 struct net *net = sock_net(sk);
1820 struct ip_set_net *inst = ip_set_pernet(net);
1822 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1824 if (optval != SO_IP_SET)
1826 if (*len < sizeof(unsigned int))
1829 data = vmalloc(*len);
1832 if (copy_from_user(data, user, *len) != 0) {
1836 op = (unsigned int *) data;
1838 if (*op < IP_SET_OP_VERSION) {
1839 /* Check the version at the beginning of operations */
1840 struct ip_set_req_version *req_version = data;
1841 if (req_version->version != IPSET_PROTOCOL) {
1848 case IP_SET_OP_VERSION: {
1849 struct ip_set_req_version *req_version = data;
1851 if (*len != sizeof(struct ip_set_req_version)) {
1856 req_version->version = IPSET_PROTOCOL;
1857 ret = copy_to_user(user, req_version,
1858 sizeof(struct ip_set_req_version));
1861 case IP_SET_OP_GET_BYNAME: {
1862 struct ip_set_req_get_set *req_get = data;
1865 if (*len != sizeof(struct ip_set_req_get_set)) {
1869 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1870 nfnl_lock(NFNL_SUBSYS_IPSET);
1871 find_set_and_id(inst, req_get->set.name, &id);
1872 req_get->set.index = id;
1873 nfnl_unlock(NFNL_SUBSYS_IPSET);
1876 case IP_SET_OP_GET_FNAME: {
1877 struct ip_set_req_get_set_family *req_get = data;
1880 if (*len != sizeof(struct ip_set_req_get_set_family)) {
1884 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1885 nfnl_lock(NFNL_SUBSYS_IPSET);
1886 find_set_and_id(inst, req_get->set.name, &id);
1887 req_get->set.index = id;
1888 if (id != IPSET_INVALID_ID)
1889 req_get->family = ip_set(inst, id)->family;
1890 nfnl_unlock(NFNL_SUBSYS_IPSET);
1893 case IP_SET_OP_GET_BYINDEX: {
1894 struct ip_set_req_get_set *req_get = data;
1897 if (*len != sizeof(struct ip_set_req_get_set) ||
1898 req_get->set.index >= inst->ip_set_max) {
1902 nfnl_lock(NFNL_SUBSYS_IPSET);
1903 set = ip_set(inst, req_get->set.index);
1904 strncpy(req_get->set.name, set ? set->name : "",
1906 nfnl_unlock(NFNL_SUBSYS_IPSET);
1912 } /* end of switch(op) */
1915 ret = copy_to_user(user, data, copylen);
1924 static struct nf_sockopt_ops so_set __read_mostly = {
1926 .get_optmin = SO_IP_SET,
1927 .get_optmax = SO_IP_SET + 1,
1928 .get = &ip_set_sockfn_get,
1929 .owner = THIS_MODULE,
1932 static int __net_init
1933 ip_set_net_init(struct net *net)
1935 struct ip_set_net *inst = ip_set_pernet(net);
1936 struct ip_set **list;
1938 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
1939 if (inst->ip_set_max >= IPSET_INVALID_ID)
1940 inst->ip_set_max = IPSET_INVALID_ID - 1;
1942 list = kzalloc(sizeof(struct ip_set *) * inst->ip_set_max, GFP_KERNEL);
1945 inst->is_deleted = 0;
1946 rcu_assign_pointer(inst->ip_set_list, list);
1950 static void __net_exit
1951 ip_set_net_exit(struct net *net)
1953 struct ip_set_net *inst = ip_set_pernet(net);
1955 struct ip_set *set = NULL;
1958 inst->is_deleted = 1; /* flag for ip_set_nfnl_put */
1960 for (i = 0; i < inst->ip_set_max; i++) {
1961 set = ip_set(inst, i);
1963 ip_set_destroy_set(inst, i);
1965 kfree(rcu_dereference_protected(inst->ip_set_list, 1));
1968 static struct pernet_operations ip_set_net_ops = {
1969 .init = ip_set_net_init,
1970 .exit = ip_set_net_exit,
1971 .id = &ip_set_net_id,
1972 .size = sizeof(struct ip_set_net)
1979 int ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
1981 pr_err("ip_set: cannot register with nfnetlink.\n");
1984 ret = nf_register_sockopt(&so_set);
1986 pr_err("SO_SET registry failed: %d\n", ret);
1987 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
1990 ret = register_pernet_subsys(&ip_set_net_ops);
1992 pr_err("ip_set: cannot register pernet_subsys.\n");
1993 nf_unregister_sockopt(&so_set);
1994 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
1997 pr_info("ip_set: protocol %u\n", IPSET_PROTOCOL);
2004 unregister_pernet_subsys(&ip_set_net_ops);
2005 nf_unregister_sockopt(&so_set);
2006 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2007 pr_debug("these are the famous last words\n");
2010 module_init(ip_set_init);
2011 module_exit(ip_set_fini);