1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3 * Patrick Schaaf <bof@bof.de>
4 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
7 /* Kernel module for IP set management */
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
13 #include <linux/skbuff.h>
14 #include <linux/spinlock.h>
15 #include <linux/rculist.h>
16 #include <net/netlink.h>
17 #include <net/net_namespace.h>
18 #include <net/netns/generic.h>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/x_tables.h>
22 #include <linux/netfilter/nfnetlink.h>
23 #include <linux/netfilter/ipset/ip_set.h>
25 static LIST_HEAD(ip_set_type_list); /* all registered set types */
26 static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */
27 static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */
30 struct ip_set * __rcu *ip_set_list; /* all individual sets */
31 ip_set_id_t ip_set_max; /* max number of sets */
32 bool is_deleted; /* deleted by ip_set_net_exit */
33 bool is_destroyed; /* all sets are destroyed */
36 static unsigned int ip_set_net_id __read_mostly;
38 static struct ip_set_net *ip_set_pernet(struct net *net)
40 return net_generic(net, ip_set_net_id);
44 #define STRNCMP(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
46 static unsigned int max_sets;
48 module_param(max_sets, int, 0600);
49 MODULE_PARM_DESC(max_sets, "maximal number of sets");
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
52 MODULE_DESCRIPTION("core IP set support");
53 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
55 /* When the nfnl mutex or ip_set_ref_lock is held: */
56 #define ip_set_dereference(p) \
57 rcu_dereference_protected(p, \
58 lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET) || \
59 lockdep_is_held(&ip_set_ref_lock))
60 #define ip_set(inst, id) \
61 ip_set_dereference((inst)->ip_set_list)[id]
62 #define ip_set_ref_netlink(inst,id) \
63 rcu_dereference_raw((inst)->ip_set_list)[id]
64 #define ip_set_dereference_nfnl(p) \
65 rcu_dereference_check(p, lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET))
67 /* The set types are implemented in modules and registered set types
68 * can be found in ip_set_type_list. Adding/deleting types is
69 * serialized by ip_set_type_mutex.
73 ip_set_type_lock(void)
75 mutex_lock(&ip_set_type_mutex);
79 ip_set_type_unlock(void)
81 mutex_unlock(&ip_set_type_mutex);
84 /* Register and deregister settype */
86 static struct ip_set_type *
87 find_set_type(const char *name, u8 family, u8 revision)
89 struct ip_set_type *type;
91 list_for_each_entry_rcu(type, &ip_set_type_list, list,
92 lockdep_is_held(&ip_set_type_mutex))
93 if (STRNCMP(type->name, name) &&
94 (type->family == family ||
95 type->family == NFPROTO_UNSPEC) &&
96 revision >= type->revision_min &&
97 revision <= type->revision_max)
102 /* Unlock, try to load a set type module and lock again */
104 load_settype(const char *name)
106 nfnl_unlock(NFNL_SUBSYS_IPSET);
107 pr_debug("try to load ip_set_%s\n", name);
108 if (request_module("ip_set_%s", name) < 0) {
109 pr_warn("Can't find ip_set type %s\n", name);
110 nfnl_lock(NFNL_SUBSYS_IPSET);
113 nfnl_lock(NFNL_SUBSYS_IPSET);
117 /* Find a set type and reference it */
118 #define find_set_type_get(name, family, revision, found) \
119 __find_set_type_get(name, family, revision, found, false)
122 __find_set_type_get(const char *name, u8 family, u8 revision,
123 struct ip_set_type **found, bool retry)
125 struct ip_set_type *type;
128 if (retry && !load_settype(name))
129 return -IPSET_ERR_FIND_TYPE;
132 *found = find_set_type(name, family, revision);
134 err = !try_module_get((*found)->me) ? -EFAULT : 0;
137 /* Make sure the type is already loaded
138 * but we don't support the revision
140 list_for_each_entry_rcu(type, &ip_set_type_list, list)
141 if (STRNCMP(type->name, name)) {
142 err = -IPSET_ERR_FIND_TYPE;
147 return retry ? -IPSET_ERR_FIND_TYPE :
148 __find_set_type_get(name, family, revision, found, true);
155 /* Find a given set type by name and family.
156 * If we succeeded, the supported minimal and maximum revisions are
159 #define find_set_type_minmax(name, family, min, max) \
160 __find_set_type_minmax(name, family, min, max, false)
163 __find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max,
166 struct ip_set_type *type;
169 if (retry && !load_settype(name))
170 return -IPSET_ERR_FIND_TYPE;
172 *min = 255; *max = 0;
174 list_for_each_entry_rcu(type, &ip_set_type_list, list)
175 if (STRNCMP(type->name, name) &&
176 (type->family == family ||
177 type->family == NFPROTO_UNSPEC)) {
179 if (type->revision_min < *min)
180 *min = type->revision_min;
181 if (type->revision_max > *max)
182 *max = type->revision_max;
188 return retry ? -IPSET_ERR_FIND_TYPE :
189 __find_set_type_minmax(name, family, min, max, true);
192 #define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \
193 (f) == NFPROTO_IPV6 ? "inet6" : "any")
195 /* Register a set type structure. The type is identified by
196 * the unique triple of name, family and revision.
199 ip_set_type_register(struct ip_set_type *type)
203 if (type->protocol != IPSET_PROTOCOL) {
204 pr_warn("ip_set type %s, family %s, revision %u:%u uses wrong protocol version %u (want %u)\n",
205 type->name, family_name(type->family),
206 type->revision_min, type->revision_max,
207 type->protocol, IPSET_PROTOCOL);
212 if (find_set_type(type->name, type->family, type->revision_min)) {
214 pr_warn("ip_set type %s, family %s with revision min %u already registered!\n",
215 type->name, family_name(type->family),
217 ip_set_type_unlock();
220 list_add_rcu(&type->list, &ip_set_type_list);
221 pr_debug("type %s, family %s, revision %u:%u registered.\n",
222 type->name, family_name(type->family),
223 type->revision_min, type->revision_max);
224 ip_set_type_unlock();
228 EXPORT_SYMBOL_GPL(ip_set_type_register);
230 /* Unregister a set type. There's a small race with ip_set_create */
232 ip_set_type_unregister(struct ip_set_type *type)
235 if (!find_set_type(type->name, type->family, type->revision_min)) {
236 pr_warn("ip_set type %s, family %s with revision min %u not registered\n",
237 type->name, family_name(type->family),
239 ip_set_type_unlock();
242 list_del_rcu(&type->list);
243 pr_debug("type %s, family %s with revision min %u unregistered.\n",
244 type->name, family_name(type->family), type->revision_min);
245 ip_set_type_unlock();
249 EXPORT_SYMBOL_GPL(ip_set_type_unregister);
251 /* Utility functions */
253 ip_set_alloc(size_t size)
255 return kvzalloc(size, GFP_KERNEL_ACCOUNT);
257 EXPORT_SYMBOL_GPL(ip_set_alloc);
260 ip_set_free(void *members)
262 pr_debug("%p: free with %s\n", members,
263 is_vmalloc_addr(members) ? "vfree" : "kfree");
266 EXPORT_SYMBOL_GPL(ip_set_free);
269 flag_nested(const struct nlattr *nla)
271 return nla->nla_type & NLA_F_NESTED;
274 static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
275 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
276 [IPSET_ATTR_IPADDR_IPV6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
280 ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
282 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
284 if (unlikely(!flag_nested(nla)))
285 return -IPSET_ERR_PROTOCOL;
286 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla,
287 ipaddr_policy, NULL))
288 return -IPSET_ERR_PROTOCOL;
289 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
290 return -IPSET_ERR_PROTOCOL;
292 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
295 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
298 ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
300 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
302 if (unlikely(!flag_nested(nla)))
303 return -IPSET_ERR_PROTOCOL;
305 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla,
306 ipaddr_policy, NULL))
307 return -IPSET_ERR_PROTOCOL;
308 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
309 return -IPSET_ERR_PROTOCOL;
311 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
312 sizeof(struct in6_addr));
315 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
318 ip_set_timeout_get(const unsigned long *timeout)
322 if (*timeout == IPSET_ELEM_PERMANENT)
325 t = jiffies_to_msecs(*timeout - jiffies) / MSEC_PER_SEC;
326 /* Zero value in userspace means no timeout */
327 return t == 0 ? 1 : t;
331 ip_set_comment_uget(struct nlattr *tb)
336 /* Called from uadd only, protected by the set spinlock.
337 * The kadt functions don't use the comment extensions in any way.
340 ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
341 const struct ip_set_ext *ext)
343 struct ip_set_comment_rcu *c = rcu_dereference_protected(comment->c, 1);
344 size_t len = ext->comment ? strlen(ext->comment) : 0;
347 set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
349 rcu_assign_pointer(comment->c, NULL);
353 if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
354 len = IPSET_MAX_COMMENT_SIZE;
355 c = kmalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
358 strscpy(c->str, ext->comment, len + 1);
359 set->ext_size += sizeof(*c) + strlen(c->str) + 1;
360 rcu_assign_pointer(comment->c, c);
362 EXPORT_SYMBOL_GPL(ip_set_init_comment);
364 /* Used only when dumping a set, protected by rcu_read_lock() */
366 ip_set_put_comment(struct sk_buff *skb, const struct ip_set_comment *comment)
368 struct ip_set_comment_rcu *c = rcu_dereference(comment->c);
372 return nla_put_string(skb, IPSET_ATTR_COMMENT, c->str);
375 /* Called from uadd/udel, flush or the garbage collectors protected
376 * by the set spinlock.
377 * Called when the set is destroyed and when there can't be any user
378 * of the set data anymore.
381 ip_set_comment_free(struct ip_set *set, void *ptr)
383 struct ip_set_comment *comment = ptr;
384 struct ip_set_comment_rcu *c;
386 c = rcu_dereference_protected(comment->c, 1);
389 set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
391 rcu_assign_pointer(comment->c, NULL);
394 typedef void (*destroyer)(struct ip_set *, void *);
395 /* ipset data extension types, in size order */
397 const struct ip_set_ext_type ip_set_extensions[] = {
398 [IPSET_EXT_ID_COUNTER] = {
399 .type = IPSET_EXT_COUNTER,
400 .flag = IPSET_FLAG_WITH_COUNTERS,
401 .len = sizeof(struct ip_set_counter),
402 .align = __alignof__(struct ip_set_counter),
404 [IPSET_EXT_ID_TIMEOUT] = {
405 .type = IPSET_EXT_TIMEOUT,
406 .len = sizeof(unsigned long),
407 .align = __alignof__(unsigned long),
409 [IPSET_EXT_ID_SKBINFO] = {
410 .type = IPSET_EXT_SKBINFO,
411 .flag = IPSET_FLAG_WITH_SKBINFO,
412 .len = sizeof(struct ip_set_skbinfo),
413 .align = __alignof__(struct ip_set_skbinfo),
415 [IPSET_EXT_ID_COMMENT] = {
416 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
417 .flag = IPSET_FLAG_WITH_COMMENT,
418 .len = sizeof(struct ip_set_comment),
419 .align = __alignof__(struct ip_set_comment),
420 .destroy = ip_set_comment_free,
423 EXPORT_SYMBOL_GPL(ip_set_extensions);
426 add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
428 return ip_set_extensions[id].flag ?
429 (flags & ip_set_extensions[id].flag) :
430 !!tb[IPSET_ATTR_TIMEOUT];
434 ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len,
437 enum ip_set_ext_id id;
440 if (tb[IPSET_ATTR_CADT_FLAGS])
441 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
442 if (cadt_flags & IPSET_FLAG_WITH_FORCEADD)
443 set->flags |= IPSET_CREATE_FLAG_FORCEADD;
446 for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
447 if (!add_extension(id, cadt_flags, tb))
449 if (align < ip_set_extensions[id].align)
450 align = ip_set_extensions[id].align;
451 len = ALIGN(len, ip_set_extensions[id].align);
452 set->offset[id] = len;
453 set->extensions |= ip_set_extensions[id].type;
454 len += ip_set_extensions[id].len;
456 return ALIGN(len, align);
458 EXPORT_SYMBOL_GPL(ip_set_elem_len);
461 ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
462 struct ip_set_ext *ext)
466 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
467 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
468 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
469 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
470 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
471 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
472 return -IPSET_ERR_PROTOCOL;
474 if (tb[IPSET_ATTR_TIMEOUT]) {
475 if (!SET_WITH_TIMEOUT(set))
476 return -IPSET_ERR_TIMEOUT;
477 ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
479 if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) {
480 if (!SET_WITH_COUNTER(set))
481 return -IPSET_ERR_COUNTER;
482 if (tb[IPSET_ATTR_BYTES])
483 ext->bytes = be64_to_cpu(nla_get_be64(
484 tb[IPSET_ATTR_BYTES]));
485 if (tb[IPSET_ATTR_PACKETS])
486 ext->packets = be64_to_cpu(nla_get_be64(
487 tb[IPSET_ATTR_PACKETS]));
489 if (tb[IPSET_ATTR_COMMENT]) {
490 if (!SET_WITH_COMMENT(set))
491 return -IPSET_ERR_COMMENT;
492 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
494 if (tb[IPSET_ATTR_SKBMARK]) {
495 if (!SET_WITH_SKBINFO(set))
496 return -IPSET_ERR_SKBINFO;
497 fullmark = be64_to_cpu(nla_get_be64(tb[IPSET_ATTR_SKBMARK]));
498 ext->skbinfo.skbmark = fullmark >> 32;
499 ext->skbinfo.skbmarkmask = fullmark & 0xffffffff;
501 if (tb[IPSET_ATTR_SKBPRIO]) {
502 if (!SET_WITH_SKBINFO(set))
503 return -IPSET_ERR_SKBINFO;
504 ext->skbinfo.skbprio =
505 be32_to_cpu(nla_get_be32(tb[IPSET_ATTR_SKBPRIO]));
507 if (tb[IPSET_ATTR_SKBQUEUE]) {
508 if (!SET_WITH_SKBINFO(set))
509 return -IPSET_ERR_SKBINFO;
510 ext->skbinfo.skbqueue =
511 be16_to_cpu(nla_get_be16(tb[IPSET_ATTR_SKBQUEUE]));
515 EXPORT_SYMBOL_GPL(ip_set_get_extensions);
518 ip_set_get_bytes(const struct ip_set_counter *counter)
520 return (u64)atomic64_read(&(counter)->bytes);
524 ip_set_get_packets(const struct ip_set_counter *counter)
526 return (u64)atomic64_read(&(counter)->packets);
530 ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
532 return nla_put_net64(skb, IPSET_ATTR_BYTES,
533 cpu_to_be64(ip_set_get_bytes(counter)),
535 nla_put_net64(skb, IPSET_ATTR_PACKETS,
536 cpu_to_be64(ip_set_get_packets(counter)),
541 ip_set_put_skbinfo(struct sk_buff *skb, const struct ip_set_skbinfo *skbinfo)
543 /* Send nonzero parameters only */
544 return ((skbinfo->skbmark || skbinfo->skbmarkmask) &&
545 nla_put_net64(skb, IPSET_ATTR_SKBMARK,
546 cpu_to_be64((u64)skbinfo->skbmark << 32 |
547 skbinfo->skbmarkmask),
550 nla_put_net32(skb, IPSET_ATTR_SKBPRIO,
551 cpu_to_be32(skbinfo->skbprio))) ||
552 (skbinfo->skbqueue &&
553 nla_put_net16(skb, IPSET_ATTR_SKBQUEUE,
554 cpu_to_be16(skbinfo->skbqueue)));
558 ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
559 const void *e, bool active)
561 if (SET_WITH_TIMEOUT(set)) {
562 unsigned long *timeout = ext_timeout(e, set);
564 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
565 htonl(active ? ip_set_timeout_get(timeout)
569 if (SET_WITH_COUNTER(set) &&
570 ip_set_put_counter(skb, ext_counter(e, set)))
572 if (SET_WITH_COMMENT(set) &&
573 ip_set_put_comment(skb, ext_comment(e, set)))
575 if (SET_WITH_SKBINFO(set) &&
576 ip_set_put_skbinfo(skb, ext_skbinfo(e, set)))
580 EXPORT_SYMBOL_GPL(ip_set_put_extensions);
583 ip_set_match_counter(u64 counter, u64 match, u8 op)
586 case IPSET_COUNTER_NONE:
588 case IPSET_COUNTER_EQ:
589 return counter == match;
590 case IPSET_COUNTER_NE:
591 return counter != match;
592 case IPSET_COUNTER_LT:
593 return counter < match;
594 case IPSET_COUNTER_GT:
595 return counter > match;
601 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
603 atomic64_add((long long)bytes, &(counter)->bytes);
607 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
609 atomic64_add((long long)packets, &(counter)->packets);
613 ip_set_update_counter(struct ip_set_counter *counter,
614 const struct ip_set_ext *ext, u32 flags)
616 if (ext->packets != ULLONG_MAX &&
617 !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
618 ip_set_add_bytes(ext->bytes, counter);
619 ip_set_add_packets(ext->packets, counter);
624 ip_set_get_skbinfo(struct ip_set_skbinfo *skbinfo,
625 const struct ip_set_ext *ext,
626 struct ip_set_ext *mext, u32 flags)
628 mext->skbinfo = *skbinfo;
632 ip_set_match_extensions(struct ip_set *set, const struct ip_set_ext *ext,
633 struct ip_set_ext *mext, u32 flags, void *data)
635 if (SET_WITH_TIMEOUT(set) &&
636 ip_set_timeout_expired(ext_timeout(data, set)))
638 if (SET_WITH_COUNTER(set)) {
639 struct ip_set_counter *counter = ext_counter(data, set);
641 ip_set_update_counter(counter, ext, flags);
643 if (flags & IPSET_FLAG_MATCH_COUNTERS &&
644 !(ip_set_match_counter(ip_set_get_packets(counter),
645 mext->packets, mext->packets_op) &&
646 ip_set_match_counter(ip_set_get_bytes(counter),
647 mext->bytes, mext->bytes_op)))
650 if (SET_WITH_SKBINFO(set))
651 ip_set_get_skbinfo(ext_skbinfo(data, set),
655 EXPORT_SYMBOL_GPL(ip_set_match_extensions);
657 /* Creating/destroying/renaming/swapping affect the existence and
658 * the properties of a set. All of these can be executed from userspace
659 * only and serialized by the nfnl mutex indirectly from nfnetlink.
661 * Sets are identified by their index in ip_set_list and the index
662 * is used by the external references (set/SET netfilter modules).
664 * The set behind an index may change by swapping only, from userspace.
668 __ip_set_get(struct ip_set *set)
670 write_lock_bh(&ip_set_ref_lock);
672 write_unlock_bh(&ip_set_ref_lock);
676 __ip_set_put(struct ip_set *set)
678 write_lock_bh(&ip_set_ref_lock);
679 BUG_ON(set->ref == 0);
681 write_unlock_bh(&ip_set_ref_lock);
684 /* set->ref can be swapped out by ip_set_swap, netlink events (like dump) need
685 * a separate reference counter
688 __ip_set_get_netlink(struct ip_set *set)
690 write_lock_bh(&ip_set_ref_lock);
692 write_unlock_bh(&ip_set_ref_lock);
696 __ip_set_put_netlink(struct ip_set *set)
698 write_lock_bh(&ip_set_ref_lock);
699 BUG_ON(set->ref_netlink == 0);
701 write_unlock_bh(&ip_set_ref_lock);
704 /* Add, del and test set entries from kernel.
706 * The set behind the index must exist and must be referenced
707 * so it can't be destroyed (or changed) under our foot.
710 static struct ip_set *
711 ip_set_rcu_get(struct net *net, ip_set_id_t index)
713 struct ip_set_net *inst = ip_set_pernet(net);
715 /* ip_set_list and the set pointer need to be protected */
716 return ip_set_dereference_nfnl(inst->ip_set_list)[index];
720 ip_set_lock(struct ip_set *set)
722 if (!set->variant->region_lock)
723 spin_lock_bh(&set->lock);
727 ip_set_unlock(struct ip_set *set)
729 if (!set->variant->region_lock)
730 spin_unlock_bh(&set->lock);
734 ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
735 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
737 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
741 pr_debug("set %s, index %u\n", set->name, index);
743 if (opt->dim < set->type->dimension ||
744 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
747 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
749 if (ret == -EAGAIN) {
750 /* Type requests element to be completed */
751 pr_debug("element must be completed, ADD is triggered\n");
753 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
757 /* --return-nomatch: invert matched element */
758 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
759 (set->type->features & IPSET_TYPE_NOMATCH) &&
760 (ret > 0 || ret == -ENOTEMPTY))
764 /* Convert error codes to nomatch */
765 return (ret < 0 ? 0 : ret);
767 EXPORT_SYMBOL_GPL(ip_set_test);
770 ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
771 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
773 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
777 pr_debug("set %s, index %u\n", set->name, index);
779 if (opt->dim < set->type->dimension ||
780 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
781 return -IPSET_ERR_TYPE_MISMATCH;
784 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
789 EXPORT_SYMBOL_GPL(ip_set_add);
792 ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
793 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
795 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
799 pr_debug("set %s, index %u\n", set->name, index);
801 if (opt->dim < set->type->dimension ||
802 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
803 return -IPSET_ERR_TYPE_MISMATCH;
806 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
811 EXPORT_SYMBOL_GPL(ip_set_del);
813 /* Find set by name, reference it once. The reference makes sure the
814 * thing pointed to, does not go away under our feet.
818 ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
820 ip_set_id_t i, index = IPSET_INVALID_ID;
822 struct ip_set_net *inst = ip_set_pernet(net);
825 for (i = 0; i < inst->ip_set_max; i++) {
826 s = rcu_dereference(inst->ip_set_list)[i];
827 if (s && STRNCMP(s->name, name)) {
838 EXPORT_SYMBOL_GPL(ip_set_get_byname);
840 /* If the given set pointer points to a valid set, decrement
841 * reference count by 1. The caller shall not assume the index
842 * to be valid, after calling this function.
847 __ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
852 set = rcu_dereference(inst->ip_set_list)[index];
859 ip_set_put_byindex(struct net *net, ip_set_id_t index)
861 struct ip_set_net *inst = ip_set_pernet(net);
863 __ip_set_put_byindex(inst, index);
865 EXPORT_SYMBOL_GPL(ip_set_put_byindex);
867 /* Get the name of a set behind a set index.
868 * Set itself is protected by RCU, but its name isn't: to protect against
869 * renaming, grab ip_set_ref_lock as reader (see ip_set_rename()) and copy the
873 ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name)
875 struct ip_set *set = ip_set_rcu_get(net, index);
879 read_lock_bh(&ip_set_ref_lock);
880 strscpy_pad(name, set->name, IPSET_MAXNAMELEN);
881 read_unlock_bh(&ip_set_ref_lock);
883 EXPORT_SYMBOL_GPL(ip_set_name_byindex);
885 /* Routines to call by external subsystems, which do not
886 * call nfnl_lock for us.
889 /* Find set by index, reference it once. The reference makes sure the
890 * thing pointed to, does not go away under our feet.
892 * The nfnl mutex is used in the function.
895 ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
898 struct ip_set_net *inst = ip_set_pernet(net);
900 if (index >= inst->ip_set_max)
901 return IPSET_INVALID_ID;
903 nfnl_lock(NFNL_SUBSYS_IPSET);
904 set = ip_set(inst, index);
908 index = IPSET_INVALID_ID;
909 nfnl_unlock(NFNL_SUBSYS_IPSET);
913 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
915 /* If the given set pointer points to a valid set, decrement
916 * reference count by 1. The caller shall not assume the index
917 * to be valid, after calling this function.
919 * The nfnl mutex is used in the function.
922 ip_set_nfnl_put(struct net *net, ip_set_id_t index)
925 struct ip_set_net *inst = ip_set_pernet(net);
927 nfnl_lock(NFNL_SUBSYS_IPSET);
928 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
929 set = ip_set(inst, index);
933 nfnl_unlock(NFNL_SUBSYS_IPSET);
935 EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
937 /* Communication protocol with userspace over netlink.
939 * The commands are serialized by the nfnl mutex.
942 static inline u8 protocol(const struct nlattr * const tb[])
944 return nla_get_u8(tb[IPSET_ATTR_PROTOCOL]);
948 protocol_failed(const struct nlattr * const tb[])
950 return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) != IPSET_PROTOCOL;
954 protocol_min_failed(const struct nlattr * const tb[])
956 return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) < IPSET_PROTOCOL_MIN;
960 flag_exist(const struct nlmsghdr *nlh)
962 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
965 static struct nlmsghdr *
966 start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
969 return nfnl_msg_put(skb, portid, seq,
970 nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd), flags,
971 NFPROTO_IPV4, NFNETLINK_V0, 0);
976 static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
977 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
978 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
979 .len = IPSET_MAXNAMELEN - 1 },
980 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
981 .len = IPSET_MAXNAMELEN - 1},
982 [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
983 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
984 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
987 static struct ip_set *
988 find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
990 struct ip_set *set = NULL;
993 *id = IPSET_INVALID_ID;
994 for (i = 0; i < inst->ip_set_max; i++) {
995 set = ip_set(inst, i);
996 if (set && STRNCMP(set->name, name)) {
1001 return (*id == IPSET_INVALID_ID ? NULL : set);
1004 static inline struct ip_set *
1005 find_set(struct ip_set_net *inst, const char *name)
1009 return find_set_and_id(inst, name, &id);
1013 find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
1014 struct ip_set **set)
1019 *index = IPSET_INVALID_ID;
1020 for (i = 0; i < inst->ip_set_max; i++) {
1021 s = ip_set(inst, i);
1023 if (*index == IPSET_INVALID_ID)
1025 } else if (STRNCMP(name, s->name)) {
1031 if (*index == IPSET_INVALID_ID)
1032 /* No free slot remained */
1033 return -IPSET_ERR_MAX_SETS;
1037 static int ip_set_none(struct sk_buff *skb, const struct nfnl_info *info,
1038 const struct nlattr * const attr[])
1043 static int ip_set_create(struct sk_buff *skb, const struct nfnl_info *info,
1044 const struct nlattr * const attr[])
1046 struct ip_set_net *inst = ip_set_pernet(info->net);
1047 struct ip_set *set, *clash = NULL;
1048 ip_set_id_t index = IPSET_INVALID_ID;
1049 struct nlattr *tb[IPSET_ATTR_CREATE_MAX + 1] = {};
1050 const char *name, *typename;
1051 u8 family, revision;
1052 u32 flags = flag_exist(info->nlh);
1055 if (unlikely(protocol_min_failed(attr) ||
1056 !attr[IPSET_ATTR_SETNAME] ||
1057 !attr[IPSET_ATTR_TYPENAME] ||
1058 !attr[IPSET_ATTR_REVISION] ||
1059 !attr[IPSET_ATTR_FAMILY] ||
1060 (attr[IPSET_ATTR_DATA] &&
1061 !flag_nested(attr[IPSET_ATTR_DATA]))))
1062 return -IPSET_ERR_PROTOCOL;
1064 name = nla_data(attr[IPSET_ATTR_SETNAME]);
1065 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1066 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1067 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
1068 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
1069 name, typename, family_name(family), revision);
1071 /* First, and without any locks, allocate and initialize
1072 * a normal base set structure.
1074 set = kzalloc(sizeof(*set), GFP_KERNEL);
1077 spin_lock_init(&set->lock);
1078 strscpy(set->name, name, IPSET_MAXNAMELEN);
1079 set->family = family;
1080 set->revision = revision;
1082 /* Next, check that we know the type, and take
1083 * a reference on the type, to make sure it stays available
1084 * while constructing our new set.
1086 * After referencing the type, we try to create the type
1087 * specific part of the set without holding any locks.
1089 ret = find_set_type_get(typename, family, revision, &set->type);
1093 /* Without holding any locks, create private part. */
1094 if (attr[IPSET_ATTR_DATA] &&
1095 nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
1096 set->type->create_policy, NULL)) {
1097 ret = -IPSET_ERR_PROTOCOL;
1100 /* Set create flags depending on the type revision */
1101 set->flags |= set->type->create_flags[revision];
1103 ret = set->type->create(info->net, set, tb, flags);
1107 /* BTW, ret==0 here. */
1109 /* Here, we have a valid, constructed set and we are protected
1110 * by the nfnl mutex. Find the first free index in ip_set_list
1111 * and check clashing.
1113 ret = find_free_id(inst, set->name, &index, &clash);
1114 if (ret == -EEXIST) {
1115 /* If this is the same set and requested, ignore error */
1116 if ((flags & IPSET_FLAG_EXIST) &&
1117 STRNCMP(set->type->name, clash->type->name) &&
1118 set->type->family == clash->type->family &&
1119 set->type->revision_min == clash->type->revision_min &&
1120 set->type->revision_max == clash->type->revision_max &&
1121 set->variant->same_set(set, clash))
1124 } else if (ret == -IPSET_ERR_MAX_SETS) {
1125 struct ip_set **list, **tmp;
1126 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
1128 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
1132 list = kvcalloc(i, sizeof(struct ip_set *), GFP_KERNEL);
1135 /* nfnl mutex is held, both lists are valid */
1136 tmp = ip_set_dereference(inst->ip_set_list);
1137 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
1138 rcu_assign_pointer(inst->ip_set_list, list);
1139 /* Make sure all current packets have passed through */
1142 index = inst->ip_set_max;
1143 inst->ip_set_max = i;
1150 /* Finally! Add our shiny new set to the list, and be done. */
1151 pr_debug("create: '%s' created with index %u!\n", set->name, index);
1152 ip_set(inst, index) = set;
1157 set->variant->cancel_gc(set);
1158 set->variant->destroy(set);
1160 module_put(set->type->me);
1168 static const struct nla_policy
1169 ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
1170 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1171 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1172 .len = IPSET_MAXNAMELEN - 1 },
1176 ip_set_destroy_set(struct ip_set *set)
1178 pr_debug("set: %s\n", set->name);
1180 /* Must call it without holding any lock */
1181 set->variant->destroy(set);
1182 module_put(set->type->me);
1187 ip_set_destroy_set_rcu(struct rcu_head *head)
1189 struct ip_set *set = container_of(head, struct ip_set, rcu);
1191 ip_set_destroy_set(set);
1194 static int ip_set_destroy(struct sk_buff *skb, const struct nfnl_info *info,
1195 const struct nlattr * const attr[])
1197 struct ip_set_net *inst = ip_set_pernet(info->net);
1202 if (unlikely(protocol_min_failed(attr)))
1203 return -IPSET_ERR_PROTOCOL;
1206 /* Commands are serialized and references are
1207 * protected by the ip_set_ref_lock.
1208 * External systems (i.e. xt_set) must call
1209 * ip_set_put|get_nfnl_* functions, that way we
1210 * can safely check references here.
1212 * list:set timer can only decrement the reference
1213 * counter, so if it's already zero, we can proceed
1214 * without holding the lock.
1216 if (!attr[IPSET_ATTR_SETNAME]) {
1217 /* Must wait for flush to be really finished in list:set */
1219 read_lock_bh(&ip_set_ref_lock);
1220 for (i = 0; i < inst->ip_set_max; i++) {
1221 s = ip_set(inst, i);
1222 if (s && (s->ref || s->ref_netlink)) {
1223 ret = -IPSET_ERR_BUSY;
1227 inst->is_destroyed = true;
1228 read_unlock_bh(&ip_set_ref_lock);
1229 for (i = 0; i < inst->ip_set_max; i++) {
1230 s = ip_set(inst, i);
1232 ip_set(inst, i) = NULL;
1233 /* Must cancel garbage collectors */
1234 s->variant->cancel_gc(s);
1235 ip_set_destroy_set(s);
1238 /* Modified by ip_set_destroy() only, which is serialized */
1239 inst->is_destroyed = false;
1241 u32 flags = flag_exist(info->nlh);
1244 read_lock_bh(&ip_set_ref_lock);
1245 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1248 if (!(flags & IPSET_FLAG_EXIST))
1251 } else if (s->ref || s->ref_netlink) {
1252 ret = -IPSET_ERR_BUSY;
1255 features = s->type->features;
1256 ip_set(inst, i) = NULL;
1257 read_unlock_bh(&ip_set_ref_lock);
1258 if (features & IPSET_TYPE_NAME) {
1259 /* Must wait for flush to be really finished */
1262 /* Must cancel garbage collectors */
1263 s->variant->cancel_gc(s);
1264 call_rcu(&s->rcu, ip_set_destroy_set_rcu);
1268 read_unlock_bh(&ip_set_ref_lock);
1275 ip_set_flush_set(struct ip_set *set)
1277 pr_debug("set: %s\n", set->name);
1280 set->variant->flush(set);
1284 static int ip_set_flush(struct sk_buff *skb, const struct nfnl_info *info,
1285 const struct nlattr * const attr[])
1287 struct ip_set_net *inst = ip_set_pernet(info->net);
1291 if (unlikely(protocol_min_failed(attr)))
1292 return -IPSET_ERR_PROTOCOL;
1294 if (!attr[IPSET_ATTR_SETNAME]) {
1295 for (i = 0; i < inst->ip_set_max; i++) {
1296 s = ip_set(inst, i);
1298 ip_set_flush_set(s);
1301 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1305 ip_set_flush_set(s);
1313 static const struct nla_policy
1314 ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
1315 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1316 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1317 .len = IPSET_MAXNAMELEN - 1 },
1318 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
1319 .len = IPSET_MAXNAMELEN - 1 },
1322 static int ip_set_rename(struct sk_buff *skb, const struct nfnl_info *info,
1323 const struct nlattr * const attr[])
1325 struct ip_set_net *inst = ip_set_pernet(info->net);
1326 struct ip_set *set, *s;
1331 if (unlikely(protocol_min_failed(attr) ||
1332 !attr[IPSET_ATTR_SETNAME] ||
1333 !attr[IPSET_ATTR_SETNAME2]))
1334 return -IPSET_ERR_PROTOCOL;
1336 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1340 write_lock_bh(&ip_set_ref_lock);
1341 if (set->ref != 0 || set->ref_netlink != 0) {
1342 ret = -IPSET_ERR_REFERENCED;
1346 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1347 for (i = 0; i < inst->ip_set_max; i++) {
1348 s = ip_set(inst, i);
1349 if (s && STRNCMP(s->name, name2)) {
1350 ret = -IPSET_ERR_EXIST_SETNAME2;
1354 strscpy_pad(set->name, name2, IPSET_MAXNAMELEN);
1357 write_unlock_bh(&ip_set_ref_lock);
1361 /* Swap two sets so that name/index points to the other.
1362 * References and set names are also swapped.
1364 * The commands are serialized by the nfnl mutex and references are
1365 * protected by the ip_set_ref_lock. The kernel interfaces
1366 * do not hold the mutex but the pointer settings are atomic
1367 * so the ip_set_list always contains valid pointers to the sets.
1370 static int ip_set_swap(struct sk_buff *skb, const struct nfnl_info *info,
1371 const struct nlattr * const attr[])
1373 struct ip_set_net *inst = ip_set_pernet(info->net);
1374 struct ip_set *from, *to;
1375 ip_set_id_t from_id, to_id;
1376 char from_name[IPSET_MAXNAMELEN];
1378 if (unlikely(protocol_min_failed(attr) ||
1379 !attr[IPSET_ATTR_SETNAME] ||
1380 !attr[IPSET_ATTR_SETNAME2]))
1381 return -IPSET_ERR_PROTOCOL;
1383 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1388 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1391 return -IPSET_ERR_EXIST_SETNAME2;
1393 /* Features must not change.
1394 * Not an artifical restriction anymore, as we must prevent
1395 * possible loops created by swapping in setlist type of sets.
1397 if (!(from->type->features == to->type->features &&
1398 from->family == to->family))
1399 return -IPSET_ERR_TYPE_MISMATCH;
1401 write_lock_bh(&ip_set_ref_lock);
1403 if (from->ref_netlink || to->ref_netlink) {
1404 write_unlock_bh(&ip_set_ref_lock);
1408 strscpy_pad(from_name, from->name, IPSET_MAXNAMELEN);
1409 strscpy_pad(from->name, to->name, IPSET_MAXNAMELEN);
1410 strscpy_pad(to->name, from_name, IPSET_MAXNAMELEN);
1412 swap(from->ref, to->ref);
1413 ip_set(inst, from_id) = to;
1414 ip_set(inst, to_id) = from;
1415 write_unlock_bh(&ip_set_ref_lock);
1420 /* List/save set data */
1427 #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
1428 #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
1431 ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
1435 if (SET_WITH_TIMEOUT(set))
1436 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
1437 htonl(set->timeout))))
1439 if (SET_WITH_COUNTER(set))
1440 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
1441 if (SET_WITH_COMMENT(set))
1442 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
1443 if (SET_WITH_SKBINFO(set))
1444 cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
1445 if (SET_WITH_FORCEADD(set))
1446 cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
1450 return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
1452 EXPORT_SYMBOL_GPL(ip_set_put_flags);
1455 ip_set_dump_done(struct netlink_callback *cb)
1457 if (cb->args[IPSET_CB_ARG0]) {
1458 struct ip_set_net *inst =
1459 (struct ip_set_net *)cb->args[IPSET_CB_NET];
1460 ip_set_id_t index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1461 struct ip_set *set = ip_set_ref_netlink(inst, index);
1463 if (set->variant->uref)
1464 set->variant->uref(set, cb, false);
1465 pr_debug("release set %s\n", set->name);
1466 __ip_set_put_netlink(set);
1472 dump_attrs(struct nlmsghdr *nlh)
1474 const struct nlattr *attr;
1477 pr_debug("dump nlmsg\n");
1478 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
1479 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
1483 static const struct nla_policy
1484 ip_set_dump_policy[IPSET_ATTR_CMD_MAX + 1] = {
1485 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1486 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1487 .len = IPSET_MAXNAMELEN - 1 },
1488 [IPSET_ATTR_FLAGS] = { .type = NLA_U32 },
1492 ip_set_dump_start(struct netlink_callback *cb)
1494 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
1495 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1496 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1497 struct nlattr *attr = (void *)nlh + min_len;
1498 struct sk_buff *skb = cb->skb;
1499 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1503 ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, attr,
1504 nlh->nlmsg_len - min_len,
1505 ip_set_dump_policy, NULL);
1509 cb->args[IPSET_CB_PROTO] = nla_get_u8(cda[IPSET_ATTR_PROTOCOL]);
1510 if (cda[IPSET_ATTR_SETNAME]) {
1514 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
1520 dump_type = DUMP_ONE;
1521 cb->args[IPSET_CB_INDEX] = index;
1523 dump_type = DUMP_ALL;
1526 if (cda[IPSET_ATTR_FLAGS]) {
1527 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1529 dump_type |= (f << 16);
1531 cb->args[IPSET_CB_NET] = (unsigned long)inst;
1532 cb->args[IPSET_CB_DUMP] = dump_type;
1537 /* We have to create and send the error message manually :-( */
1538 if (nlh->nlmsg_flags & NLM_F_ACK) {
1539 netlink_ack(cb->skb, nlh, ret, NULL);
1545 ip_set_dump_do(struct sk_buff *skb, struct netlink_callback *cb)
1547 ip_set_id_t index = IPSET_INVALID_ID, max;
1548 struct ip_set *set = NULL;
1549 struct nlmsghdr *nlh = NULL;
1550 unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0;
1551 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1552 u32 dump_type, dump_flags;
1556 if (!cb->args[IPSET_CB_DUMP])
1559 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
1562 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1563 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1564 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1567 pr_debug("dump type, flag: %u %u index: %ld\n",
1568 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1569 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
1570 index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1571 write_lock_bh(&ip_set_ref_lock);
1572 set = ip_set(inst, index);
1573 is_destroyed = inst->is_destroyed;
1574 if (!set || is_destroyed) {
1575 write_unlock_bh(&ip_set_ref_lock);
1576 if (dump_type == DUMP_ONE) {
1581 /* All sets are just being destroyed */
1587 /* When dumping all sets, we must dump "sorted"
1588 * so that lists (unions of sets) are dumped last.
1590 if (dump_type != DUMP_ONE &&
1591 ((dump_type == DUMP_ALL) ==
1592 !!(set->type->features & IPSET_DUMP_LAST))) {
1593 write_unlock_bh(&ip_set_ref_lock);
1596 pr_debug("List set: %s\n", set->name);
1597 if (!cb->args[IPSET_CB_ARG0]) {
1598 /* Start listing: make sure set won't be destroyed */
1599 pr_debug("reference set\n");
1602 write_unlock_bh(&ip_set_ref_lock);
1603 nlh = start_msg(skb, NETLINK_CB(cb->skb).portid,
1604 cb->nlh->nlmsg_seq, flags,
1608 goto release_refcount;
1610 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL,
1611 cb->args[IPSET_CB_PROTO]) ||
1612 nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
1613 goto nla_put_failure;
1614 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1616 switch (cb->args[IPSET_CB_ARG0]) {
1618 /* Core header data */
1619 if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
1621 nla_put_u8(skb, IPSET_ATTR_FAMILY,
1623 nla_put_u8(skb, IPSET_ATTR_REVISION,
1625 goto nla_put_failure;
1626 if (cb->args[IPSET_CB_PROTO] > IPSET_PROTOCOL_MIN &&
1627 nla_put_net16(skb, IPSET_ATTR_INDEX, htons(index)))
1628 goto nla_put_failure;
1629 ret = set->variant->head(set, skb);
1631 goto release_refcount;
1632 if (dump_flags & IPSET_FLAG_LIST_HEADER)
1634 if (set->variant->uref)
1635 set->variant->uref(set, cb, true);
1638 ret = set->variant->list(set, skb, cb);
1639 if (!cb->args[IPSET_CB_ARG0])
1640 /* Set is done, proceed with next one */
1642 goto release_refcount;
1645 /* If we dump all sets, continue with dumping last ones */
1646 if (dump_type == DUMP_ALL) {
1647 dump_type = DUMP_LAST;
1648 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1649 cb->args[IPSET_CB_INDEX] = 0;
1650 if (set && set->variant->uref)
1651 set->variant->uref(set, cb, false);
1659 if (dump_type == DUMP_ONE)
1660 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
1662 cb->args[IPSET_CB_INDEX]++;
1664 /* If there was an error or set is done, release set */
1665 if (ret || !cb->args[IPSET_CB_ARG0]) {
1666 set = ip_set_ref_netlink(inst, index);
1667 if (set->variant->uref)
1668 set->variant->uref(set, cb, false);
1669 pr_debug("release set %s\n", set->name);
1670 __ip_set_put_netlink(set);
1671 cb->args[IPSET_CB_ARG0] = 0;
1675 nlmsg_end(skb, nlh);
1676 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1680 return ret < 0 ? ret : skb->len;
1683 static int ip_set_dump(struct sk_buff *skb, const struct nfnl_info *info,
1684 const struct nlattr * const attr[])
1686 if (unlikely(protocol_min_failed(attr)))
1687 return -IPSET_ERR_PROTOCOL;
1690 struct netlink_dump_control c = {
1691 .start = ip_set_dump_start,
1692 .dump = ip_set_dump_do,
1693 .done = ip_set_dump_done,
1695 return netlink_dump_start(info->sk, skb, info->nlh, &c);
1699 /* Add, del and test */
1701 static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1702 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1703 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1704 .len = IPSET_MAXNAMELEN - 1 },
1705 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
1706 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1707 [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
1711 call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1712 struct ip_set *set, struct nlattr *tb[], enum ipset_adt adt,
1713 u32 flags, bool use_lineno)
1717 bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
1721 __ip_set_get_netlink(set);
1722 nfnl_unlock(NFNL_SUBSYS_IPSET);
1724 nfnl_lock(NFNL_SUBSYS_IPSET);
1725 __ip_set_put_netlink(set);
1729 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1732 } while (ret == -ERANGE ||
1734 set->variant->resize &&
1735 (ret = set->variant->resize(set, retried)) == 0));
1737 if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1739 if (lineno && use_lineno) {
1740 /* Error in restore/batch mode: send back lineno */
1741 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1742 struct sk_buff *skb2;
1743 struct nlmsgerr *errmsg;
1744 size_t payload = min(SIZE_MAX,
1745 sizeof(*errmsg) + nlmsg_len(nlh));
1746 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1747 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1748 struct nlattr *cmdattr;
1751 skb2 = nlmsg_new(payload, GFP_KERNEL);
1754 rep = nlmsg_put(skb2, NETLINK_CB(skb).portid,
1755 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1756 errmsg = nlmsg_data(rep);
1757 errmsg->error = ret;
1758 unsafe_memcpy(&errmsg->msg, nlh, nlh->nlmsg_len,
1759 /* Bounds checked by the skb layer. */);
1761 cmdattr = (void *)&errmsg->msg + min_len;
1763 ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, cmdattr,
1764 nlh->nlmsg_len - min_len, ip_set_adt_policy,
1771 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1775 nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
1776 /* Signal netlink not to send its ACK/errmsg. */
1783 static int ip_set_ad(struct net *net, struct sock *ctnl,
1784 struct sk_buff *skb,
1786 const struct nlmsghdr *nlh,
1787 const struct nlattr * const attr[],
1788 struct netlink_ext_ack *extack)
1790 struct ip_set_net *inst = ip_set_pernet(net);
1792 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1793 const struct nlattr *nla;
1794 u32 flags = flag_exist(nlh);
1798 if (unlikely(protocol_min_failed(attr) ||
1799 !attr[IPSET_ATTR_SETNAME] ||
1800 !((attr[IPSET_ATTR_DATA] != NULL) ^
1801 (attr[IPSET_ATTR_ADT] != NULL)) ||
1802 (attr[IPSET_ATTR_DATA] &&
1803 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1804 (attr[IPSET_ATTR_ADT] &&
1805 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1806 !attr[IPSET_ATTR_LINENO]))))
1807 return -IPSET_ERR_PROTOCOL;
1809 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1813 use_lineno = !!attr[IPSET_ATTR_LINENO];
1814 if (attr[IPSET_ATTR_DATA]) {
1815 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1816 attr[IPSET_ATTR_DATA],
1817 set->type->adt_policy, NULL))
1818 return -IPSET_ERR_PROTOCOL;
1819 ret = call_ad(net, ctnl, skb, set, tb, adt, flags,
1824 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1825 if (nla_type(nla) != IPSET_ATTR_DATA ||
1826 !flag_nested(nla) ||
1827 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1828 set->type->adt_policy, NULL))
1829 return -IPSET_ERR_PROTOCOL;
1830 ret = call_ad(net, ctnl, skb, set, tb, adt,
1839 static int ip_set_uadd(struct sk_buff *skb, const struct nfnl_info *info,
1840 const struct nlattr * const attr[])
1842 return ip_set_ad(info->net, info->sk, skb,
1843 IPSET_ADD, info->nlh, attr, info->extack);
1846 static int ip_set_udel(struct sk_buff *skb, const struct nfnl_info *info,
1847 const struct nlattr * const attr[])
1849 return ip_set_ad(info->net, info->sk, skb,
1850 IPSET_DEL, info->nlh, attr, info->extack);
1853 static int ip_set_utest(struct sk_buff *skb, const struct nfnl_info *info,
1854 const struct nlattr * const attr[])
1856 struct ip_set_net *inst = ip_set_pernet(info->net);
1858 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1862 if (unlikely(protocol_min_failed(attr) ||
1863 !attr[IPSET_ATTR_SETNAME] ||
1864 !attr[IPSET_ATTR_DATA] ||
1865 !flag_nested(attr[IPSET_ATTR_DATA])))
1866 return -IPSET_ERR_PROTOCOL;
1868 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1872 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1873 set->type->adt_policy, NULL))
1874 return -IPSET_ERR_PROTOCOL;
1877 ret = set->variant->uadt(set, tb, IPSET_TEST, &lineno, 0, 0);
1878 rcu_read_unlock_bh();
1879 /* Userspace can't trigger element to be re-added */
1883 return ret > 0 ? 0 : -IPSET_ERR_EXIST;
1886 /* Get headed data of a set */
1888 static int ip_set_header(struct sk_buff *skb, const struct nfnl_info *info,
1889 const struct nlattr * const attr[])
1891 struct ip_set_net *inst = ip_set_pernet(info->net);
1892 const struct ip_set *set;
1893 struct sk_buff *skb2;
1894 struct nlmsghdr *nlh2;
1896 if (unlikely(protocol_min_failed(attr) ||
1897 !attr[IPSET_ATTR_SETNAME]))
1898 return -IPSET_ERR_PROTOCOL;
1900 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1904 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1908 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
1912 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
1913 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
1914 nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
1915 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
1916 nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
1917 goto nla_put_failure;
1918 nlmsg_end(skb2, nlh2);
1920 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
1923 nlmsg_cancel(skb2, nlh2);
1931 static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1932 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1933 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
1934 .len = IPSET_MAXNAMELEN - 1 },
1935 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
1938 static int ip_set_type(struct sk_buff *skb, const struct nfnl_info *info,
1939 const struct nlattr * const attr[])
1941 struct sk_buff *skb2;
1942 struct nlmsghdr *nlh2;
1943 u8 family, min, max;
1944 const char *typename;
1947 if (unlikely(protocol_min_failed(attr) ||
1948 !attr[IPSET_ATTR_TYPENAME] ||
1949 !attr[IPSET_ATTR_FAMILY]))
1950 return -IPSET_ERR_PROTOCOL;
1952 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1953 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1954 ret = find_set_type_minmax(typename, family, &min, &max);
1958 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1962 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
1966 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
1967 nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
1968 nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
1969 nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
1970 nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
1971 goto nla_put_failure;
1972 nlmsg_end(skb2, nlh2);
1974 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
1975 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
1978 nlmsg_cancel(skb2, nlh2);
1984 /* Get protocol version */
1986 static const struct nla_policy
1987 ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1988 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1991 static int ip_set_protocol(struct sk_buff *skb, const struct nfnl_info *info,
1992 const struct nlattr * const attr[])
1994 struct sk_buff *skb2;
1995 struct nlmsghdr *nlh2;
1997 if (unlikely(!attr[IPSET_ATTR_PROTOCOL]))
1998 return -IPSET_ERR_PROTOCOL;
2000 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2004 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
2005 IPSET_CMD_PROTOCOL);
2008 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
2009 goto nla_put_failure;
2010 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL_MIN, IPSET_PROTOCOL_MIN))
2011 goto nla_put_failure;
2012 nlmsg_end(skb2, nlh2);
2014 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
2017 nlmsg_cancel(skb2, nlh2);
2023 /* Get set by name or index, from userspace */
2025 static int ip_set_byname(struct sk_buff *skb, const struct nfnl_info *info,
2026 const struct nlattr * const attr[])
2028 struct ip_set_net *inst = ip_set_pernet(info->net);
2029 struct sk_buff *skb2;
2030 struct nlmsghdr *nlh2;
2031 ip_set_id_t id = IPSET_INVALID_ID;
2032 const struct ip_set *set;
2034 if (unlikely(protocol_failed(attr) ||
2035 !attr[IPSET_ATTR_SETNAME]))
2036 return -IPSET_ERR_PROTOCOL;
2038 set = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]), &id);
2039 if (id == IPSET_INVALID_ID)
2042 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2046 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
2047 IPSET_CMD_GET_BYNAME);
2050 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
2051 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
2052 nla_put_net16(skb2, IPSET_ATTR_INDEX, htons(id)))
2053 goto nla_put_failure;
2054 nlmsg_end(skb2, nlh2);
2056 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
2059 nlmsg_cancel(skb2, nlh2);
2065 static const struct nla_policy ip_set_index_policy[IPSET_ATTR_CMD_MAX + 1] = {
2066 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
2067 [IPSET_ATTR_INDEX] = { .type = NLA_U16 },
2070 static int ip_set_byindex(struct sk_buff *skb, const struct nfnl_info *info,
2071 const struct nlattr * const attr[])
2073 struct ip_set_net *inst = ip_set_pernet(info->net);
2074 struct sk_buff *skb2;
2075 struct nlmsghdr *nlh2;
2076 ip_set_id_t id = IPSET_INVALID_ID;
2077 const struct ip_set *set;
2079 if (unlikely(protocol_failed(attr) ||
2080 !attr[IPSET_ATTR_INDEX]))
2081 return -IPSET_ERR_PROTOCOL;
2083 id = ip_set_get_h16(attr[IPSET_ATTR_INDEX]);
2084 if (id >= inst->ip_set_max)
2086 set = ip_set(inst, id);
2090 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2094 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
2095 IPSET_CMD_GET_BYINDEX);
2098 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
2099 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name))
2100 goto nla_put_failure;
2101 nlmsg_end(skb2, nlh2);
2103 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
2106 nlmsg_cancel(skb2, nlh2);
2112 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
2113 [IPSET_CMD_NONE] = {
2114 .call = ip_set_none,
2115 .type = NFNL_CB_MUTEX,
2116 .attr_count = IPSET_ATTR_CMD_MAX,
2118 [IPSET_CMD_CREATE] = {
2119 .call = ip_set_create,
2120 .type = NFNL_CB_MUTEX,
2121 .attr_count = IPSET_ATTR_CMD_MAX,
2122 .policy = ip_set_create_policy,
2124 [IPSET_CMD_DESTROY] = {
2125 .call = ip_set_destroy,
2126 .type = NFNL_CB_MUTEX,
2127 .attr_count = IPSET_ATTR_CMD_MAX,
2128 .policy = ip_set_setname_policy,
2130 [IPSET_CMD_FLUSH] = {
2131 .call = ip_set_flush,
2132 .type = NFNL_CB_MUTEX,
2133 .attr_count = IPSET_ATTR_CMD_MAX,
2134 .policy = ip_set_setname_policy,
2136 [IPSET_CMD_RENAME] = {
2137 .call = ip_set_rename,
2138 .type = NFNL_CB_MUTEX,
2139 .attr_count = IPSET_ATTR_CMD_MAX,
2140 .policy = ip_set_setname2_policy,
2142 [IPSET_CMD_SWAP] = {
2143 .call = ip_set_swap,
2144 .type = NFNL_CB_MUTEX,
2145 .attr_count = IPSET_ATTR_CMD_MAX,
2146 .policy = ip_set_setname2_policy,
2148 [IPSET_CMD_LIST] = {
2149 .call = ip_set_dump,
2150 .type = NFNL_CB_MUTEX,
2151 .attr_count = IPSET_ATTR_CMD_MAX,
2152 .policy = ip_set_dump_policy,
2154 [IPSET_CMD_SAVE] = {
2155 .call = ip_set_dump,
2156 .type = NFNL_CB_MUTEX,
2157 .attr_count = IPSET_ATTR_CMD_MAX,
2158 .policy = ip_set_setname_policy,
2161 .call = ip_set_uadd,
2162 .type = NFNL_CB_MUTEX,
2163 .attr_count = IPSET_ATTR_CMD_MAX,
2164 .policy = ip_set_adt_policy,
2167 .call = ip_set_udel,
2168 .type = NFNL_CB_MUTEX,
2169 .attr_count = IPSET_ATTR_CMD_MAX,
2170 .policy = ip_set_adt_policy,
2172 [IPSET_CMD_TEST] = {
2173 .call = ip_set_utest,
2174 .type = NFNL_CB_MUTEX,
2175 .attr_count = IPSET_ATTR_CMD_MAX,
2176 .policy = ip_set_adt_policy,
2178 [IPSET_CMD_HEADER] = {
2179 .call = ip_set_header,
2180 .type = NFNL_CB_MUTEX,
2181 .attr_count = IPSET_ATTR_CMD_MAX,
2182 .policy = ip_set_setname_policy,
2184 [IPSET_CMD_TYPE] = {
2185 .call = ip_set_type,
2186 .type = NFNL_CB_MUTEX,
2187 .attr_count = IPSET_ATTR_CMD_MAX,
2188 .policy = ip_set_type_policy,
2190 [IPSET_CMD_PROTOCOL] = {
2191 .call = ip_set_protocol,
2192 .type = NFNL_CB_MUTEX,
2193 .attr_count = IPSET_ATTR_CMD_MAX,
2194 .policy = ip_set_protocol_policy,
2196 [IPSET_CMD_GET_BYNAME] = {
2197 .call = ip_set_byname,
2198 .type = NFNL_CB_MUTEX,
2199 .attr_count = IPSET_ATTR_CMD_MAX,
2200 .policy = ip_set_setname_policy,
2202 [IPSET_CMD_GET_BYINDEX] = {
2203 .call = ip_set_byindex,
2204 .type = NFNL_CB_MUTEX,
2205 .attr_count = IPSET_ATTR_CMD_MAX,
2206 .policy = ip_set_index_policy,
2210 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
2212 .subsys_id = NFNL_SUBSYS_IPSET,
2213 .cb_count = IPSET_MSG_MAX,
2214 .cb = ip_set_netlink_subsys_cb,
2217 /* Interface to iptables/ip6tables */
2220 ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
2224 int copylen = *len, ret = 0;
2225 struct net *net = sock_net(sk);
2226 struct ip_set_net *inst = ip_set_pernet(net);
2228 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2230 if (optval != SO_IP_SET)
2232 if (*len < sizeof(unsigned int))
2235 data = vmalloc(*len);
2238 if (copy_from_user(data, user, *len) != 0) {
2244 if (*op < IP_SET_OP_VERSION) {
2245 /* Check the version at the beginning of operations */
2246 struct ip_set_req_version *req_version = data;
2248 if (*len < sizeof(struct ip_set_req_version)) {
2253 if (req_version->version < IPSET_PROTOCOL_MIN) {
2260 case IP_SET_OP_VERSION: {
2261 struct ip_set_req_version *req_version = data;
2263 if (*len != sizeof(struct ip_set_req_version)) {
2268 req_version->version = IPSET_PROTOCOL;
2269 if (copy_to_user(user, req_version,
2270 sizeof(struct ip_set_req_version)))
2274 case IP_SET_OP_GET_BYNAME: {
2275 struct ip_set_req_get_set *req_get = data;
2278 if (*len != sizeof(struct ip_set_req_get_set)) {
2282 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2283 nfnl_lock(NFNL_SUBSYS_IPSET);
2284 find_set_and_id(inst, req_get->set.name, &id);
2285 req_get->set.index = id;
2286 nfnl_unlock(NFNL_SUBSYS_IPSET);
2289 case IP_SET_OP_GET_FNAME: {
2290 struct ip_set_req_get_set_family *req_get = data;
2293 if (*len != sizeof(struct ip_set_req_get_set_family)) {
2297 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2298 nfnl_lock(NFNL_SUBSYS_IPSET);
2299 find_set_and_id(inst, req_get->set.name, &id);
2300 req_get->set.index = id;
2301 if (id != IPSET_INVALID_ID)
2302 req_get->family = ip_set(inst, id)->family;
2303 nfnl_unlock(NFNL_SUBSYS_IPSET);
2306 case IP_SET_OP_GET_BYINDEX: {
2307 struct ip_set_req_get_set *req_get = data;
2310 if (*len != sizeof(struct ip_set_req_get_set) ||
2311 req_get->set.index >= inst->ip_set_max) {
2315 nfnl_lock(NFNL_SUBSYS_IPSET);
2316 set = ip_set(inst, req_get->set.index);
2317 ret = strscpy(req_get->set.name, set ? set->name : "",
2319 nfnl_unlock(NFNL_SUBSYS_IPSET);
2327 } /* end of switch(op) */
2330 if (copy_to_user(user, data, copylen))
2340 static struct nf_sockopt_ops so_set __read_mostly = {
2342 .get_optmin = SO_IP_SET,
2343 .get_optmax = SO_IP_SET + 1,
2344 .get = ip_set_sockfn_get,
2345 .owner = THIS_MODULE,
2348 static int __net_init
2349 ip_set_net_init(struct net *net)
2351 struct ip_set_net *inst = ip_set_pernet(net);
2352 struct ip_set **list;
2354 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
2355 if (inst->ip_set_max >= IPSET_INVALID_ID)
2356 inst->ip_set_max = IPSET_INVALID_ID - 1;
2358 list = kvcalloc(inst->ip_set_max, sizeof(struct ip_set *), GFP_KERNEL);
2361 inst->is_deleted = false;
2362 inst->is_destroyed = false;
2363 rcu_assign_pointer(inst->ip_set_list, list);
2367 static void __net_exit
2368 ip_set_net_exit(struct net *net)
2370 struct ip_set_net *inst = ip_set_pernet(net);
2372 struct ip_set *set = NULL;
2375 inst->is_deleted = true; /* flag for ip_set_nfnl_put */
2377 nfnl_lock(NFNL_SUBSYS_IPSET);
2378 for (i = 0; i < inst->ip_set_max; i++) {
2379 set = ip_set(inst, i);
2381 ip_set(inst, i) = NULL;
2382 set->variant->cancel_gc(set);
2383 ip_set_destroy_set(set);
2386 nfnl_unlock(NFNL_SUBSYS_IPSET);
2387 kvfree(rcu_dereference_protected(inst->ip_set_list, 1));
2390 static struct pernet_operations ip_set_net_ops = {
2391 .init = ip_set_net_init,
2392 .exit = ip_set_net_exit,
2393 .id = &ip_set_net_id,
2394 .size = sizeof(struct ip_set_net),
2400 int ret = register_pernet_subsys(&ip_set_net_ops);
2403 pr_err("ip_set: cannot register pernet_subsys.\n");
2407 ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
2409 pr_err("ip_set: cannot register with nfnetlink.\n");
2410 unregister_pernet_subsys(&ip_set_net_ops);
2414 ret = nf_register_sockopt(&so_set);
2416 pr_err("SO_SET registry failed: %d\n", ret);
2417 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2418 unregister_pernet_subsys(&ip_set_net_ops);
2428 nf_unregister_sockopt(&so_set);
2429 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2430 unregister_pernet_subsys(&ip_set_net_ops);
2432 /* Wait for call_rcu() in destroy */
2435 pr_debug("these are the famous last words\n");
2438 module_init(ip_set_init);
2439 module_exit(ip_set_fini);
2441 MODULE_DESCRIPTION("ip_set: protocol " __stringify(IPSET_PROTOCOL));