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]
65 /* The set types are implemented in modules and registered set types
66 * can be found in ip_set_type_list. Adding/deleting types is
67 * serialized by ip_set_type_mutex.
71 ip_set_type_lock(void)
73 mutex_lock(&ip_set_type_mutex);
77 ip_set_type_unlock(void)
79 mutex_unlock(&ip_set_type_mutex);
82 /* Register and deregister settype */
84 static struct ip_set_type *
85 find_set_type(const char *name, u8 family, u8 revision)
87 struct ip_set_type *type;
89 list_for_each_entry_rcu(type, &ip_set_type_list, list,
90 lockdep_is_held(&ip_set_type_mutex))
91 if (STRNCMP(type->name, name) &&
92 (type->family == family ||
93 type->family == NFPROTO_UNSPEC) &&
94 revision >= type->revision_min &&
95 revision <= type->revision_max)
100 /* Unlock, try to load a set type module and lock again */
102 load_settype(const char *name)
104 nfnl_unlock(NFNL_SUBSYS_IPSET);
105 pr_debug("try to load ip_set_%s\n", name);
106 if (request_module("ip_set_%s", name) < 0) {
107 pr_warn("Can't find ip_set type %s\n", name);
108 nfnl_lock(NFNL_SUBSYS_IPSET);
111 nfnl_lock(NFNL_SUBSYS_IPSET);
115 /* Find a set type and reference it */
116 #define find_set_type_get(name, family, revision, found) \
117 __find_set_type_get(name, family, revision, found, false)
120 __find_set_type_get(const char *name, u8 family, u8 revision,
121 struct ip_set_type **found, bool retry)
123 struct ip_set_type *type;
126 if (retry && !load_settype(name))
127 return -IPSET_ERR_FIND_TYPE;
130 *found = find_set_type(name, family, revision);
132 err = !try_module_get((*found)->me) ? -EFAULT : 0;
135 /* Make sure the type is already loaded
136 * but we don't support the revision
138 list_for_each_entry_rcu(type, &ip_set_type_list, list)
139 if (STRNCMP(type->name, name)) {
140 err = -IPSET_ERR_FIND_TYPE;
145 return retry ? -IPSET_ERR_FIND_TYPE :
146 __find_set_type_get(name, family, revision, found, true);
153 /* Find a given set type by name and family.
154 * If we succeeded, the supported minimal and maximum revisions are
157 #define find_set_type_minmax(name, family, min, max) \
158 __find_set_type_minmax(name, family, min, max, false)
161 __find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max,
164 struct ip_set_type *type;
167 if (retry && !load_settype(name))
168 return -IPSET_ERR_FIND_TYPE;
170 *min = 255; *max = 0;
172 list_for_each_entry_rcu(type, &ip_set_type_list, list)
173 if (STRNCMP(type->name, name) &&
174 (type->family == family ||
175 type->family == NFPROTO_UNSPEC)) {
177 if (type->revision_min < *min)
178 *min = type->revision_min;
179 if (type->revision_max > *max)
180 *max = type->revision_max;
186 return retry ? -IPSET_ERR_FIND_TYPE :
187 __find_set_type_minmax(name, family, min, max, true);
190 #define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \
191 (f) == NFPROTO_IPV6 ? "inet6" : "any")
193 /* Register a set type structure. The type is identified by
194 * the unique triple of name, family and revision.
197 ip_set_type_register(struct ip_set_type *type)
201 if (type->protocol != IPSET_PROTOCOL) {
202 pr_warn("ip_set type %s, family %s, revision %u:%u uses wrong protocol version %u (want %u)\n",
203 type->name, family_name(type->family),
204 type->revision_min, type->revision_max,
205 type->protocol, IPSET_PROTOCOL);
210 if (find_set_type(type->name, type->family, type->revision_min)) {
212 pr_warn("ip_set type %s, family %s with revision min %u already registered!\n",
213 type->name, family_name(type->family),
215 ip_set_type_unlock();
218 list_add_rcu(&type->list, &ip_set_type_list);
219 pr_debug("type %s, family %s, revision %u:%u registered.\n",
220 type->name, family_name(type->family),
221 type->revision_min, type->revision_max);
222 ip_set_type_unlock();
226 EXPORT_SYMBOL_GPL(ip_set_type_register);
228 /* Unregister a set type. There's a small race with ip_set_create */
230 ip_set_type_unregister(struct ip_set_type *type)
233 if (!find_set_type(type->name, type->family, type->revision_min)) {
234 pr_warn("ip_set type %s, family %s with revision min %u not registered\n",
235 type->name, family_name(type->family),
237 ip_set_type_unlock();
240 list_del_rcu(&type->list);
241 pr_debug("type %s, family %s with revision min %u unregistered.\n",
242 type->name, family_name(type->family), type->revision_min);
243 ip_set_type_unlock();
247 EXPORT_SYMBOL_GPL(ip_set_type_unregister);
249 /* Utility functions */
251 ip_set_alloc(size_t size)
253 return kvzalloc(size, GFP_KERNEL_ACCOUNT);
255 EXPORT_SYMBOL_GPL(ip_set_alloc);
258 ip_set_free(void *members)
260 pr_debug("%p: free with %s\n", members,
261 is_vmalloc_addr(members) ? "vfree" : "kfree");
264 EXPORT_SYMBOL_GPL(ip_set_free);
267 flag_nested(const struct nlattr *nla)
269 return nla->nla_type & NLA_F_NESTED;
272 static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
273 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
274 [IPSET_ATTR_IPADDR_IPV6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
278 ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
280 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
282 if (unlikely(!flag_nested(nla)))
283 return -IPSET_ERR_PROTOCOL;
284 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla,
285 ipaddr_policy, NULL))
286 return -IPSET_ERR_PROTOCOL;
287 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
288 return -IPSET_ERR_PROTOCOL;
290 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
293 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
296 ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
298 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
300 if (unlikely(!flag_nested(nla)))
301 return -IPSET_ERR_PROTOCOL;
303 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla,
304 ipaddr_policy, NULL))
305 return -IPSET_ERR_PROTOCOL;
306 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
307 return -IPSET_ERR_PROTOCOL;
309 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
310 sizeof(struct in6_addr));
313 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
316 ip_set_timeout_get(const unsigned long *timeout)
320 if (*timeout == IPSET_ELEM_PERMANENT)
323 t = jiffies_to_msecs(*timeout - jiffies) / MSEC_PER_SEC;
324 /* Zero value in userspace means no timeout */
325 return t == 0 ? 1 : t;
329 ip_set_comment_uget(struct nlattr *tb)
334 /* Called from uadd only, protected by the set spinlock.
335 * The kadt functions don't use the comment extensions in any way.
338 ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
339 const struct ip_set_ext *ext)
341 struct ip_set_comment_rcu *c = rcu_dereference_protected(comment->c, 1);
342 size_t len = ext->comment ? strlen(ext->comment) : 0;
345 set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
347 rcu_assign_pointer(comment->c, NULL);
351 if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
352 len = IPSET_MAX_COMMENT_SIZE;
353 c = kmalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
356 strscpy(c->str, ext->comment, len + 1);
357 set->ext_size += sizeof(*c) + strlen(c->str) + 1;
358 rcu_assign_pointer(comment->c, c);
360 EXPORT_SYMBOL_GPL(ip_set_init_comment);
362 /* Used only when dumping a set, protected by rcu_read_lock() */
364 ip_set_put_comment(struct sk_buff *skb, const struct ip_set_comment *comment)
366 struct ip_set_comment_rcu *c = rcu_dereference(comment->c);
370 return nla_put_string(skb, IPSET_ATTR_COMMENT, c->str);
373 /* Called from uadd/udel, flush or the garbage collectors protected
374 * by the set spinlock.
375 * Called when the set is destroyed and when there can't be any user
376 * of the set data anymore.
379 ip_set_comment_free(struct ip_set *set, void *ptr)
381 struct ip_set_comment *comment = ptr;
382 struct ip_set_comment_rcu *c;
384 c = rcu_dereference_protected(comment->c, 1);
387 set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
389 rcu_assign_pointer(comment->c, NULL);
392 typedef void (*destroyer)(struct ip_set *, void *);
393 /* ipset data extension types, in size order */
395 const struct ip_set_ext_type ip_set_extensions[] = {
396 [IPSET_EXT_ID_COUNTER] = {
397 .type = IPSET_EXT_COUNTER,
398 .flag = IPSET_FLAG_WITH_COUNTERS,
399 .len = sizeof(struct ip_set_counter),
400 .align = __alignof__(struct ip_set_counter),
402 [IPSET_EXT_ID_TIMEOUT] = {
403 .type = IPSET_EXT_TIMEOUT,
404 .len = sizeof(unsigned long),
405 .align = __alignof__(unsigned long),
407 [IPSET_EXT_ID_SKBINFO] = {
408 .type = IPSET_EXT_SKBINFO,
409 .flag = IPSET_FLAG_WITH_SKBINFO,
410 .len = sizeof(struct ip_set_skbinfo),
411 .align = __alignof__(struct ip_set_skbinfo),
413 [IPSET_EXT_ID_COMMENT] = {
414 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
415 .flag = IPSET_FLAG_WITH_COMMENT,
416 .len = sizeof(struct ip_set_comment),
417 .align = __alignof__(struct ip_set_comment),
418 .destroy = ip_set_comment_free,
421 EXPORT_SYMBOL_GPL(ip_set_extensions);
424 add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
426 return ip_set_extensions[id].flag ?
427 (flags & ip_set_extensions[id].flag) :
428 !!tb[IPSET_ATTR_TIMEOUT];
432 ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len,
435 enum ip_set_ext_id id;
438 if (tb[IPSET_ATTR_CADT_FLAGS])
439 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
440 if (cadt_flags & IPSET_FLAG_WITH_FORCEADD)
441 set->flags |= IPSET_CREATE_FLAG_FORCEADD;
444 for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
445 if (!add_extension(id, cadt_flags, tb))
447 if (align < ip_set_extensions[id].align)
448 align = ip_set_extensions[id].align;
449 len = ALIGN(len, ip_set_extensions[id].align);
450 set->offset[id] = len;
451 set->extensions |= ip_set_extensions[id].type;
452 len += ip_set_extensions[id].len;
454 return ALIGN(len, align);
456 EXPORT_SYMBOL_GPL(ip_set_elem_len);
459 ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
460 struct ip_set_ext *ext)
464 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
465 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
466 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
467 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
468 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
469 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
470 return -IPSET_ERR_PROTOCOL;
472 if (tb[IPSET_ATTR_TIMEOUT]) {
473 if (!SET_WITH_TIMEOUT(set))
474 return -IPSET_ERR_TIMEOUT;
475 ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
477 if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) {
478 if (!SET_WITH_COUNTER(set))
479 return -IPSET_ERR_COUNTER;
480 if (tb[IPSET_ATTR_BYTES])
481 ext->bytes = be64_to_cpu(nla_get_be64(
482 tb[IPSET_ATTR_BYTES]));
483 if (tb[IPSET_ATTR_PACKETS])
484 ext->packets = be64_to_cpu(nla_get_be64(
485 tb[IPSET_ATTR_PACKETS]));
487 if (tb[IPSET_ATTR_COMMENT]) {
488 if (!SET_WITH_COMMENT(set))
489 return -IPSET_ERR_COMMENT;
490 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
492 if (tb[IPSET_ATTR_SKBMARK]) {
493 if (!SET_WITH_SKBINFO(set))
494 return -IPSET_ERR_SKBINFO;
495 fullmark = be64_to_cpu(nla_get_be64(tb[IPSET_ATTR_SKBMARK]));
496 ext->skbinfo.skbmark = fullmark >> 32;
497 ext->skbinfo.skbmarkmask = fullmark & 0xffffffff;
499 if (tb[IPSET_ATTR_SKBPRIO]) {
500 if (!SET_WITH_SKBINFO(set))
501 return -IPSET_ERR_SKBINFO;
502 ext->skbinfo.skbprio =
503 be32_to_cpu(nla_get_be32(tb[IPSET_ATTR_SKBPRIO]));
505 if (tb[IPSET_ATTR_SKBQUEUE]) {
506 if (!SET_WITH_SKBINFO(set))
507 return -IPSET_ERR_SKBINFO;
508 ext->skbinfo.skbqueue =
509 be16_to_cpu(nla_get_be16(tb[IPSET_ATTR_SKBQUEUE]));
513 EXPORT_SYMBOL_GPL(ip_set_get_extensions);
516 ip_set_get_bytes(const struct ip_set_counter *counter)
518 return (u64)atomic64_read(&(counter)->bytes);
522 ip_set_get_packets(const struct ip_set_counter *counter)
524 return (u64)atomic64_read(&(counter)->packets);
528 ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
530 return nla_put_net64(skb, IPSET_ATTR_BYTES,
531 cpu_to_be64(ip_set_get_bytes(counter)),
533 nla_put_net64(skb, IPSET_ATTR_PACKETS,
534 cpu_to_be64(ip_set_get_packets(counter)),
539 ip_set_put_skbinfo(struct sk_buff *skb, const struct ip_set_skbinfo *skbinfo)
541 /* Send nonzero parameters only */
542 return ((skbinfo->skbmark || skbinfo->skbmarkmask) &&
543 nla_put_net64(skb, IPSET_ATTR_SKBMARK,
544 cpu_to_be64((u64)skbinfo->skbmark << 32 |
545 skbinfo->skbmarkmask),
548 nla_put_net32(skb, IPSET_ATTR_SKBPRIO,
549 cpu_to_be32(skbinfo->skbprio))) ||
550 (skbinfo->skbqueue &&
551 nla_put_net16(skb, IPSET_ATTR_SKBQUEUE,
552 cpu_to_be16(skbinfo->skbqueue)));
556 ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
557 const void *e, bool active)
559 if (SET_WITH_TIMEOUT(set)) {
560 unsigned long *timeout = ext_timeout(e, set);
562 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
563 htonl(active ? ip_set_timeout_get(timeout)
567 if (SET_WITH_COUNTER(set) &&
568 ip_set_put_counter(skb, ext_counter(e, set)))
570 if (SET_WITH_COMMENT(set) &&
571 ip_set_put_comment(skb, ext_comment(e, set)))
573 if (SET_WITH_SKBINFO(set) &&
574 ip_set_put_skbinfo(skb, ext_skbinfo(e, set)))
578 EXPORT_SYMBOL_GPL(ip_set_put_extensions);
581 ip_set_match_counter(u64 counter, u64 match, u8 op)
584 case IPSET_COUNTER_NONE:
586 case IPSET_COUNTER_EQ:
587 return counter == match;
588 case IPSET_COUNTER_NE:
589 return counter != match;
590 case IPSET_COUNTER_LT:
591 return counter < match;
592 case IPSET_COUNTER_GT:
593 return counter > match;
599 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
601 atomic64_add((long long)bytes, &(counter)->bytes);
605 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
607 atomic64_add((long long)packets, &(counter)->packets);
611 ip_set_update_counter(struct ip_set_counter *counter,
612 const struct ip_set_ext *ext, u32 flags)
614 if (ext->packets != ULLONG_MAX &&
615 !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
616 ip_set_add_bytes(ext->bytes, counter);
617 ip_set_add_packets(ext->packets, counter);
622 ip_set_get_skbinfo(struct ip_set_skbinfo *skbinfo,
623 const struct ip_set_ext *ext,
624 struct ip_set_ext *mext, u32 flags)
626 mext->skbinfo = *skbinfo;
630 ip_set_match_extensions(struct ip_set *set, const struct ip_set_ext *ext,
631 struct ip_set_ext *mext, u32 flags, void *data)
633 if (SET_WITH_TIMEOUT(set) &&
634 ip_set_timeout_expired(ext_timeout(data, set)))
636 if (SET_WITH_COUNTER(set)) {
637 struct ip_set_counter *counter = ext_counter(data, set);
639 ip_set_update_counter(counter, ext, flags);
641 if (flags & IPSET_FLAG_MATCH_COUNTERS &&
642 !(ip_set_match_counter(ip_set_get_packets(counter),
643 mext->packets, mext->packets_op) &&
644 ip_set_match_counter(ip_set_get_bytes(counter),
645 mext->bytes, mext->bytes_op)))
648 if (SET_WITH_SKBINFO(set))
649 ip_set_get_skbinfo(ext_skbinfo(data, set),
653 EXPORT_SYMBOL_GPL(ip_set_match_extensions);
655 /* Creating/destroying/renaming/swapping affect the existence and
656 * the properties of a set. All of these can be executed from userspace
657 * only and serialized by the nfnl mutex indirectly from nfnetlink.
659 * Sets are identified by their index in ip_set_list and the index
660 * is used by the external references (set/SET netfilter modules).
662 * The set behind an index may change by swapping only, from userspace.
666 __ip_set_get(struct ip_set *set)
668 write_lock_bh(&ip_set_ref_lock);
670 write_unlock_bh(&ip_set_ref_lock);
674 __ip_set_put(struct ip_set *set)
676 write_lock_bh(&ip_set_ref_lock);
677 BUG_ON(set->ref == 0);
679 write_unlock_bh(&ip_set_ref_lock);
682 /* set->ref can be swapped out by ip_set_swap, netlink events (like dump) need
683 * a separate reference counter
686 __ip_set_get_netlink(struct ip_set *set)
688 write_lock_bh(&ip_set_ref_lock);
690 write_unlock_bh(&ip_set_ref_lock);
694 __ip_set_put_netlink(struct ip_set *set)
696 write_lock_bh(&ip_set_ref_lock);
697 BUG_ON(set->ref_netlink == 0);
699 write_unlock_bh(&ip_set_ref_lock);
702 /* Add, del and test set entries from kernel.
704 * The set behind the index must exist and must be referenced
705 * so it can't be destroyed (or changed) under our foot.
708 static struct ip_set *
709 ip_set_rcu_get(struct net *net, ip_set_id_t index)
712 struct ip_set_net *inst = ip_set_pernet(net);
715 /* ip_set_list itself needs to be protected */
716 set = rcu_dereference(inst->ip_set_list)[index];
723 ip_set_lock(struct ip_set *set)
725 if (!set->variant->region_lock)
726 spin_lock_bh(&set->lock);
730 ip_set_unlock(struct ip_set *set)
732 if (!set->variant->region_lock)
733 spin_unlock_bh(&set->lock);
737 ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
738 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
740 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
744 pr_debug("set %s, index %u\n", set->name, index);
746 if (opt->dim < set->type->dimension ||
747 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
750 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
752 if (ret == -EAGAIN) {
753 /* Type requests element to be completed */
754 pr_debug("element must be completed, ADD is triggered\n");
756 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
760 /* --return-nomatch: invert matched element */
761 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
762 (set->type->features & IPSET_TYPE_NOMATCH) &&
763 (ret > 0 || ret == -ENOTEMPTY))
767 /* Convert error codes to nomatch */
768 return (ret < 0 ? 0 : ret);
770 EXPORT_SYMBOL_GPL(ip_set_test);
773 ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
774 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
776 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
780 pr_debug("set %s, index %u\n", set->name, index);
782 if (opt->dim < set->type->dimension ||
783 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
784 return -IPSET_ERR_TYPE_MISMATCH;
787 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
792 EXPORT_SYMBOL_GPL(ip_set_add);
795 ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
796 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
798 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
802 pr_debug("set %s, index %u\n", set->name, index);
804 if (opt->dim < set->type->dimension ||
805 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
806 return -IPSET_ERR_TYPE_MISMATCH;
809 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
814 EXPORT_SYMBOL_GPL(ip_set_del);
816 /* Find set by name, reference it once. The reference makes sure the
817 * thing pointed to, does not go away under our feet.
821 ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
823 ip_set_id_t i, index = IPSET_INVALID_ID;
825 struct ip_set_net *inst = ip_set_pernet(net);
828 for (i = 0; i < inst->ip_set_max; i++) {
829 s = rcu_dereference(inst->ip_set_list)[i];
830 if (s && STRNCMP(s->name, name)) {
841 EXPORT_SYMBOL_GPL(ip_set_get_byname);
843 /* If the given set pointer points to a valid set, decrement
844 * reference count by 1. The caller shall not assume the index
845 * to be valid, after calling this function.
850 __ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
855 set = rcu_dereference(inst->ip_set_list)[index];
862 ip_set_put_byindex(struct net *net, ip_set_id_t index)
864 struct ip_set_net *inst = ip_set_pernet(net);
866 __ip_set_put_byindex(inst, index);
868 EXPORT_SYMBOL_GPL(ip_set_put_byindex);
870 /* Get the name of a set behind a set index.
871 * Set itself is protected by RCU, but its name isn't: to protect against
872 * renaming, grab ip_set_ref_lock as reader (see ip_set_rename()) and copy the
876 ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name)
878 struct ip_set *set = ip_set_rcu_get(net, index);
882 read_lock_bh(&ip_set_ref_lock);
883 strscpy_pad(name, set->name, IPSET_MAXNAMELEN);
884 read_unlock_bh(&ip_set_ref_lock);
886 EXPORT_SYMBOL_GPL(ip_set_name_byindex);
888 /* Routines to call by external subsystems, which do not
889 * call nfnl_lock for us.
892 /* Find set by index, reference it once. The reference makes sure the
893 * thing pointed to, does not go away under our feet.
895 * The nfnl mutex is used in the function.
898 ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
901 struct ip_set_net *inst = ip_set_pernet(net);
903 if (index >= inst->ip_set_max)
904 return IPSET_INVALID_ID;
906 nfnl_lock(NFNL_SUBSYS_IPSET);
907 set = ip_set(inst, index);
911 index = IPSET_INVALID_ID;
912 nfnl_unlock(NFNL_SUBSYS_IPSET);
916 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
918 /* If the given set pointer points to a valid set, decrement
919 * reference count by 1. The caller shall not assume the index
920 * to be valid, after calling this function.
922 * The nfnl mutex is used in the function.
925 ip_set_nfnl_put(struct net *net, ip_set_id_t index)
928 struct ip_set_net *inst = ip_set_pernet(net);
930 nfnl_lock(NFNL_SUBSYS_IPSET);
931 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
932 set = ip_set(inst, index);
936 nfnl_unlock(NFNL_SUBSYS_IPSET);
938 EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
940 /* Communication protocol with userspace over netlink.
942 * The commands are serialized by the nfnl mutex.
945 static inline u8 protocol(const struct nlattr * const tb[])
947 return nla_get_u8(tb[IPSET_ATTR_PROTOCOL]);
951 protocol_failed(const struct nlattr * const tb[])
953 return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) != IPSET_PROTOCOL;
957 protocol_min_failed(const struct nlattr * const tb[])
959 return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) < IPSET_PROTOCOL_MIN;
963 flag_exist(const struct nlmsghdr *nlh)
965 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
968 static struct nlmsghdr *
969 start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
972 return nfnl_msg_put(skb, portid, seq,
973 nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd), flags,
974 NFPROTO_IPV4, NFNETLINK_V0, 0);
979 static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
980 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
981 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
982 .len = IPSET_MAXNAMELEN - 1 },
983 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
984 .len = IPSET_MAXNAMELEN - 1},
985 [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
986 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
987 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
990 static struct ip_set *
991 find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
993 struct ip_set *set = NULL;
996 *id = IPSET_INVALID_ID;
997 for (i = 0; i < inst->ip_set_max; i++) {
998 set = ip_set(inst, i);
999 if (set && STRNCMP(set->name, name)) {
1004 return (*id == IPSET_INVALID_ID ? NULL : set);
1007 static inline struct ip_set *
1008 find_set(struct ip_set_net *inst, const char *name)
1012 return find_set_and_id(inst, name, &id);
1016 find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
1017 struct ip_set **set)
1022 *index = IPSET_INVALID_ID;
1023 for (i = 0; i < inst->ip_set_max; i++) {
1024 s = ip_set(inst, i);
1026 if (*index == IPSET_INVALID_ID)
1028 } else if (STRNCMP(name, s->name)) {
1034 if (*index == IPSET_INVALID_ID)
1035 /* No free slot remained */
1036 return -IPSET_ERR_MAX_SETS;
1040 static int ip_set_none(struct sk_buff *skb, const struct nfnl_info *info,
1041 const struct nlattr * const attr[])
1046 static int ip_set_create(struct sk_buff *skb, const struct nfnl_info *info,
1047 const struct nlattr * const attr[])
1049 struct ip_set_net *inst = ip_set_pernet(info->net);
1050 struct ip_set *set, *clash = NULL;
1051 ip_set_id_t index = IPSET_INVALID_ID;
1052 struct nlattr *tb[IPSET_ATTR_CREATE_MAX + 1] = {};
1053 const char *name, *typename;
1054 u8 family, revision;
1055 u32 flags = flag_exist(info->nlh);
1058 if (unlikely(protocol_min_failed(attr) ||
1059 !attr[IPSET_ATTR_SETNAME] ||
1060 !attr[IPSET_ATTR_TYPENAME] ||
1061 !attr[IPSET_ATTR_REVISION] ||
1062 !attr[IPSET_ATTR_FAMILY] ||
1063 (attr[IPSET_ATTR_DATA] &&
1064 !flag_nested(attr[IPSET_ATTR_DATA]))))
1065 return -IPSET_ERR_PROTOCOL;
1067 name = nla_data(attr[IPSET_ATTR_SETNAME]);
1068 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1069 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1070 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
1071 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
1072 name, typename, family_name(family), revision);
1074 /* First, and without any locks, allocate and initialize
1075 * a normal base set structure.
1077 set = kzalloc(sizeof(*set), GFP_KERNEL);
1080 spin_lock_init(&set->lock);
1081 strscpy(set->name, name, IPSET_MAXNAMELEN);
1082 set->family = family;
1083 set->revision = revision;
1085 /* Next, check that we know the type, and take
1086 * a reference on the type, to make sure it stays available
1087 * while constructing our new set.
1089 * After referencing the type, we try to create the type
1090 * specific part of the set without holding any locks.
1092 ret = find_set_type_get(typename, family, revision, &set->type);
1096 /* Without holding any locks, create private part. */
1097 if (attr[IPSET_ATTR_DATA] &&
1098 nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
1099 set->type->create_policy, NULL)) {
1100 ret = -IPSET_ERR_PROTOCOL;
1103 /* Set create flags depending on the type revision */
1104 set->flags |= set->type->create_flags[revision];
1106 ret = set->type->create(info->net, set, tb, flags);
1110 /* BTW, ret==0 here. */
1112 /* Here, we have a valid, constructed set and we are protected
1113 * by the nfnl mutex. Find the first free index in ip_set_list
1114 * and check clashing.
1116 ret = find_free_id(inst, set->name, &index, &clash);
1117 if (ret == -EEXIST) {
1118 /* If this is the same set and requested, ignore error */
1119 if ((flags & IPSET_FLAG_EXIST) &&
1120 STRNCMP(set->type->name, clash->type->name) &&
1121 set->type->family == clash->type->family &&
1122 set->type->revision_min == clash->type->revision_min &&
1123 set->type->revision_max == clash->type->revision_max &&
1124 set->variant->same_set(set, clash))
1127 } else if (ret == -IPSET_ERR_MAX_SETS) {
1128 struct ip_set **list, **tmp;
1129 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
1131 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
1135 list = kvcalloc(i, sizeof(struct ip_set *), GFP_KERNEL);
1138 /* nfnl mutex is held, both lists are valid */
1139 tmp = ip_set_dereference(inst->ip_set_list);
1140 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
1141 rcu_assign_pointer(inst->ip_set_list, list);
1142 /* Make sure all current packets have passed through */
1145 index = inst->ip_set_max;
1146 inst->ip_set_max = i;
1153 /* Finally! Add our shiny new set to the list, and be done. */
1154 pr_debug("create: '%s' created with index %u!\n", set->name, index);
1155 ip_set(inst, index) = set;
1160 set->variant->destroy(set);
1162 module_put(set->type->me);
1170 static const struct nla_policy
1171 ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
1172 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1173 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1174 .len = IPSET_MAXNAMELEN - 1 },
1178 ip_set_destroy_set(struct ip_set *set)
1180 pr_debug("set: %s\n", set->name);
1182 /* Must call it without holding any lock */
1183 set->variant->destroy(set);
1184 module_put(set->type->me);
1188 static int ip_set_destroy(struct sk_buff *skb, const struct nfnl_info *info,
1189 const struct nlattr * const attr[])
1191 struct ip_set_net *inst = ip_set_pernet(info->net);
1196 if (unlikely(protocol_min_failed(attr)))
1197 return -IPSET_ERR_PROTOCOL;
1199 /* Must wait for flush to be really finished in list:set */
1202 /* Commands are serialized and references are
1203 * protected by the ip_set_ref_lock.
1204 * External systems (i.e. xt_set) must call
1205 * ip_set_put|get_nfnl_* functions, that way we
1206 * can safely check references here.
1208 * list:set timer can only decrement the reference
1209 * counter, so if it's already zero, we can proceed
1210 * without holding the lock.
1212 read_lock_bh(&ip_set_ref_lock);
1213 if (!attr[IPSET_ATTR_SETNAME]) {
1214 for (i = 0; i < inst->ip_set_max; i++) {
1215 s = ip_set(inst, i);
1216 if (s && (s->ref || s->ref_netlink)) {
1217 ret = -IPSET_ERR_BUSY;
1221 inst->is_destroyed = true;
1222 read_unlock_bh(&ip_set_ref_lock);
1223 for (i = 0; i < inst->ip_set_max; i++) {
1224 s = ip_set(inst, i);
1226 ip_set(inst, i) = NULL;
1227 ip_set_destroy_set(s);
1230 /* Modified by ip_set_destroy() only, which is serialized */
1231 inst->is_destroyed = false;
1233 u32 flags = flag_exist(info->nlh);
1234 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1237 if (!(flags & IPSET_FLAG_EXIST))
1240 } else if (s->ref || s->ref_netlink) {
1241 ret = -IPSET_ERR_BUSY;
1244 ip_set(inst, i) = NULL;
1245 read_unlock_bh(&ip_set_ref_lock);
1247 ip_set_destroy_set(s);
1251 read_unlock_bh(&ip_set_ref_lock);
1258 ip_set_flush_set(struct ip_set *set)
1260 pr_debug("set: %s\n", set->name);
1263 set->variant->flush(set);
1267 static int ip_set_flush(struct sk_buff *skb, const struct nfnl_info *info,
1268 const struct nlattr * const attr[])
1270 struct ip_set_net *inst = ip_set_pernet(info->net);
1274 if (unlikely(protocol_min_failed(attr)))
1275 return -IPSET_ERR_PROTOCOL;
1277 if (!attr[IPSET_ATTR_SETNAME]) {
1278 for (i = 0; i < inst->ip_set_max; i++) {
1279 s = ip_set(inst, i);
1281 ip_set_flush_set(s);
1284 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1288 ip_set_flush_set(s);
1296 static const struct nla_policy
1297 ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
1298 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1299 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1300 .len = IPSET_MAXNAMELEN - 1 },
1301 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
1302 .len = IPSET_MAXNAMELEN - 1 },
1305 static int ip_set_rename(struct sk_buff *skb, const struct nfnl_info *info,
1306 const struct nlattr * const attr[])
1308 struct ip_set_net *inst = ip_set_pernet(info->net);
1309 struct ip_set *set, *s;
1314 if (unlikely(protocol_min_failed(attr) ||
1315 !attr[IPSET_ATTR_SETNAME] ||
1316 !attr[IPSET_ATTR_SETNAME2]))
1317 return -IPSET_ERR_PROTOCOL;
1319 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1323 write_lock_bh(&ip_set_ref_lock);
1324 if (set->ref != 0 || set->ref_netlink != 0) {
1325 ret = -IPSET_ERR_REFERENCED;
1329 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1330 for (i = 0; i < inst->ip_set_max; i++) {
1331 s = ip_set(inst, i);
1332 if (s && STRNCMP(s->name, name2)) {
1333 ret = -IPSET_ERR_EXIST_SETNAME2;
1337 strscpy_pad(set->name, name2, IPSET_MAXNAMELEN);
1340 write_unlock_bh(&ip_set_ref_lock);
1344 /* Swap two sets so that name/index points to the other.
1345 * References and set names are also swapped.
1347 * The commands are serialized by the nfnl mutex and references are
1348 * protected by the ip_set_ref_lock. The kernel interfaces
1349 * do not hold the mutex but the pointer settings are atomic
1350 * so the ip_set_list always contains valid pointers to the sets.
1353 static int ip_set_swap(struct sk_buff *skb, const struct nfnl_info *info,
1354 const struct nlattr * const attr[])
1356 struct ip_set_net *inst = ip_set_pernet(info->net);
1357 struct ip_set *from, *to;
1358 ip_set_id_t from_id, to_id;
1359 char from_name[IPSET_MAXNAMELEN];
1361 if (unlikely(protocol_min_failed(attr) ||
1362 !attr[IPSET_ATTR_SETNAME] ||
1363 !attr[IPSET_ATTR_SETNAME2]))
1364 return -IPSET_ERR_PROTOCOL;
1366 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1371 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1374 return -IPSET_ERR_EXIST_SETNAME2;
1376 /* Features must not change.
1377 * Not an artifical restriction anymore, as we must prevent
1378 * possible loops created by swapping in setlist type of sets.
1380 if (!(from->type->features == to->type->features &&
1381 from->family == to->family))
1382 return -IPSET_ERR_TYPE_MISMATCH;
1384 write_lock_bh(&ip_set_ref_lock);
1386 if (from->ref_netlink || to->ref_netlink) {
1387 write_unlock_bh(&ip_set_ref_lock);
1391 strscpy_pad(from_name, from->name, IPSET_MAXNAMELEN);
1392 strscpy_pad(from->name, to->name, IPSET_MAXNAMELEN);
1393 strscpy_pad(to->name, from_name, IPSET_MAXNAMELEN);
1395 swap(from->ref, to->ref);
1396 ip_set(inst, from_id) = to;
1397 ip_set(inst, to_id) = from;
1398 write_unlock_bh(&ip_set_ref_lock);
1403 /* List/save set data */
1410 #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
1411 #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
1414 ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
1418 if (SET_WITH_TIMEOUT(set))
1419 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
1420 htonl(set->timeout))))
1422 if (SET_WITH_COUNTER(set))
1423 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
1424 if (SET_WITH_COMMENT(set))
1425 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
1426 if (SET_WITH_SKBINFO(set))
1427 cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
1428 if (SET_WITH_FORCEADD(set))
1429 cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
1433 return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
1435 EXPORT_SYMBOL_GPL(ip_set_put_flags);
1438 ip_set_dump_done(struct netlink_callback *cb)
1440 if (cb->args[IPSET_CB_ARG0]) {
1441 struct ip_set_net *inst =
1442 (struct ip_set_net *)cb->args[IPSET_CB_NET];
1443 ip_set_id_t index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1444 struct ip_set *set = ip_set_ref_netlink(inst, index);
1446 if (set->variant->uref)
1447 set->variant->uref(set, cb, false);
1448 pr_debug("release set %s\n", set->name);
1449 __ip_set_put_netlink(set);
1455 dump_attrs(struct nlmsghdr *nlh)
1457 const struct nlattr *attr;
1460 pr_debug("dump nlmsg\n");
1461 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
1462 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
1466 static const struct nla_policy
1467 ip_set_dump_policy[IPSET_ATTR_CMD_MAX + 1] = {
1468 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1469 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1470 .len = IPSET_MAXNAMELEN - 1 },
1471 [IPSET_ATTR_FLAGS] = { .type = NLA_U32 },
1475 ip_set_dump_start(struct netlink_callback *cb)
1477 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
1478 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1479 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1480 struct nlattr *attr = (void *)nlh + min_len;
1481 struct sk_buff *skb = cb->skb;
1482 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1486 ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, attr,
1487 nlh->nlmsg_len - min_len,
1488 ip_set_dump_policy, NULL);
1492 cb->args[IPSET_CB_PROTO] = nla_get_u8(cda[IPSET_ATTR_PROTOCOL]);
1493 if (cda[IPSET_ATTR_SETNAME]) {
1497 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
1503 dump_type = DUMP_ONE;
1504 cb->args[IPSET_CB_INDEX] = index;
1506 dump_type = DUMP_ALL;
1509 if (cda[IPSET_ATTR_FLAGS]) {
1510 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1512 dump_type |= (f << 16);
1514 cb->args[IPSET_CB_NET] = (unsigned long)inst;
1515 cb->args[IPSET_CB_DUMP] = dump_type;
1520 /* We have to create and send the error message manually :-( */
1521 if (nlh->nlmsg_flags & NLM_F_ACK) {
1522 netlink_ack(cb->skb, nlh, ret, NULL);
1528 ip_set_dump_do(struct sk_buff *skb, struct netlink_callback *cb)
1530 ip_set_id_t index = IPSET_INVALID_ID, max;
1531 struct ip_set *set = NULL;
1532 struct nlmsghdr *nlh = NULL;
1533 unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0;
1534 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1535 u32 dump_type, dump_flags;
1539 if (!cb->args[IPSET_CB_DUMP])
1542 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
1545 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1546 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1547 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1550 pr_debug("dump type, flag: %u %u index: %ld\n",
1551 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1552 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
1553 index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1554 write_lock_bh(&ip_set_ref_lock);
1555 set = ip_set(inst, index);
1556 is_destroyed = inst->is_destroyed;
1557 if (!set || is_destroyed) {
1558 write_unlock_bh(&ip_set_ref_lock);
1559 if (dump_type == DUMP_ONE) {
1564 /* All sets are just being destroyed */
1570 /* When dumping all sets, we must dump "sorted"
1571 * so that lists (unions of sets) are dumped last.
1573 if (dump_type != DUMP_ONE &&
1574 ((dump_type == DUMP_ALL) ==
1575 !!(set->type->features & IPSET_DUMP_LAST))) {
1576 write_unlock_bh(&ip_set_ref_lock);
1579 pr_debug("List set: %s\n", set->name);
1580 if (!cb->args[IPSET_CB_ARG0]) {
1581 /* Start listing: make sure set won't be destroyed */
1582 pr_debug("reference set\n");
1585 write_unlock_bh(&ip_set_ref_lock);
1586 nlh = start_msg(skb, NETLINK_CB(cb->skb).portid,
1587 cb->nlh->nlmsg_seq, flags,
1591 goto release_refcount;
1593 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL,
1594 cb->args[IPSET_CB_PROTO]) ||
1595 nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
1596 goto nla_put_failure;
1597 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1599 switch (cb->args[IPSET_CB_ARG0]) {
1601 /* Core header data */
1602 if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
1604 nla_put_u8(skb, IPSET_ATTR_FAMILY,
1606 nla_put_u8(skb, IPSET_ATTR_REVISION,
1608 goto nla_put_failure;
1609 if (cb->args[IPSET_CB_PROTO] > IPSET_PROTOCOL_MIN &&
1610 nla_put_net16(skb, IPSET_ATTR_INDEX, htons(index)))
1611 goto nla_put_failure;
1612 ret = set->variant->head(set, skb);
1614 goto release_refcount;
1615 if (dump_flags & IPSET_FLAG_LIST_HEADER)
1617 if (set->variant->uref)
1618 set->variant->uref(set, cb, true);
1621 ret = set->variant->list(set, skb, cb);
1622 if (!cb->args[IPSET_CB_ARG0])
1623 /* Set is done, proceed with next one */
1625 goto release_refcount;
1628 /* If we dump all sets, continue with dumping last ones */
1629 if (dump_type == DUMP_ALL) {
1630 dump_type = DUMP_LAST;
1631 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1632 cb->args[IPSET_CB_INDEX] = 0;
1633 if (set && set->variant->uref)
1634 set->variant->uref(set, cb, false);
1642 if (dump_type == DUMP_ONE)
1643 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
1645 cb->args[IPSET_CB_INDEX]++;
1647 /* If there was an error or set is done, release set */
1648 if (ret || !cb->args[IPSET_CB_ARG0]) {
1649 set = ip_set_ref_netlink(inst, index);
1650 if (set->variant->uref)
1651 set->variant->uref(set, cb, false);
1652 pr_debug("release set %s\n", set->name);
1653 __ip_set_put_netlink(set);
1654 cb->args[IPSET_CB_ARG0] = 0;
1658 nlmsg_end(skb, nlh);
1659 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1663 return ret < 0 ? ret : skb->len;
1666 static int ip_set_dump(struct sk_buff *skb, const struct nfnl_info *info,
1667 const struct nlattr * const attr[])
1669 if (unlikely(protocol_min_failed(attr)))
1670 return -IPSET_ERR_PROTOCOL;
1673 struct netlink_dump_control c = {
1674 .start = ip_set_dump_start,
1675 .dump = ip_set_dump_do,
1676 .done = ip_set_dump_done,
1678 return netlink_dump_start(info->sk, skb, info->nlh, &c);
1682 /* Add, del and test */
1684 static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1685 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1686 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1687 .len = IPSET_MAXNAMELEN - 1 },
1688 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
1689 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1690 [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
1694 call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1695 struct ip_set *set, struct nlattr *tb[], enum ipset_adt adt,
1696 u32 flags, bool use_lineno)
1700 bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
1704 __ip_set_get_netlink(set);
1705 nfnl_unlock(NFNL_SUBSYS_IPSET);
1707 nfnl_lock(NFNL_SUBSYS_IPSET);
1708 __ip_set_put_netlink(set);
1712 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1715 } while (ret == -ERANGE ||
1717 set->variant->resize &&
1718 (ret = set->variant->resize(set, retried)) == 0));
1720 if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1722 if (lineno && use_lineno) {
1723 /* Error in restore/batch mode: send back lineno */
1724 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1725 struct sk_buff *skb2;
1726 struct nlmsgerr *errmsg;
1727 size_t payload = min(SIZE_MAX,
1728 sizeof(*errmsg) + nlmsg_len(nlh));
1729 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1730 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1731 struct nlattr *cmdattr;
1734 skb2 = nlmsg_new(payload, GFP_KERNEL);
1737 rep = nlmsg_put(skb2, NETLINK_CB(skb).portid,
1738 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1739 errmsg = nlmsg_data(rep);
1740 errmsg->error = ret;
1741 unsafe_memcpy(&errmsg->msg, nlh, nlh->nlmsg_len,
1742 /* Bounds checked by the skb layer. */);
1744 cmdattr = (void *)&errmsg->msg + min_len;
1746 ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, cmdattr,
1747 nlh->nlmsg_len - min_len, ip_set_adt_policy,
1754 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1758 nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
1759 /* Signal netlink not to send its ACK/errmsg. */
1766 static int ip_set_ad(struct net *net, struct sock *ctnl,
1767 struct sk_buff *skb,
1769 const struct nlmsghdr *nlh,
1770 const struct nlattr * const attr[],
1771 struct netlink_ext_ack *extack)
1773 struct ip_set_net *inst = ip_set_pernet(net);
1775 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1776 const struct nlattr *nla;
1777 u32 flags = flag_exist(nlh);
1781 if (unlikely(protocol_min_failed(attr) ||
1782 !attr[IPSET_ATTR_SETNAME] ||
1783 !((attr[IPSET_ATTR_DATA] != NULL) ^
1784 (attr[IPSET_ATTR_ADT] != NULL)) ||
1785 (attr[IPSET_ATTR_DATA] &&
1786 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1787 (attr[IPSET_ATTR_ADT] &&
1788 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1789 !attr[IPSET_ATTR_LINENO]))))
1790 return -IPSET_ERR_PROTOCOL;
1792 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1796 use_lineno = !!attr[IPSET_ATTR_LINENO];
1797 if (attr[IPSET_ATTR_DATA]) {
1798 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1799 attr[IPSET_ATTR_DATA],
1800 set->type->adt_policy, NULL))
1801 return -IPSET_ERR_PROTOCOL;
1802 ret = call_ad(net, ctnl, skb, set, tb, adt, flags,
1807 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1808 if (nla_type(nla) != IPSET_ATTR_DATA ||
1809 !flag_nested(nla) ||
1810 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1811 set->type->adt_policy, NULL))
1812 return -IPSET_ERR_PROTOCOL;
1813 ret = call_ad(net, ctnl, skb, set, tb, adt,
1822 static int ip_set_uadd(struct sk_buff *skb, const struct nfnl_info *info,
1823 const struct nlattr * const attr[])
1825 return ip_set_ad(info->net, info->sk, skb,
1826 IPSET_ADD, info->nlh, attr, info->extack);
1829 static int ip_set_udel(struct sk_buff *skb, const struct nfnl_info *info,
1830 const struct nlattr * const attr[])
1832 return ip_set_ad(info->net, info->sk, skb,
1833 IPSET_DEL, info->nlh, attr, info->extack);
1836 static int ip_set_utest(struct sk_buff *skb, const struct nfnl_info *info,
1837 const struct nlattr * const attr[])
1839 struct ip_set_net *inst = ip_set_pernet(info->net);
1841 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1845 if (unlikely(protocol_min_failed(attr) ||
1846 !attr[IPSET_ATTR_SETNAME] ||
1847 !attr[IPSET_ATTR_DATA] ||
1848 !flag_nested(attr[IPSET_ATTR_DATA])))
1849 return -IPSET_ERR_PROTOCOL;
1851 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1855 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1856 set->type->adt_policy, NULL))
1857 return -IPSET_ERR_PROTOCOL;
1860 ret = set->variant->uadt(set, tb, IPSET_TEST, &lineno, 0, 0);
1861 rcu_read_unlock_bh();
1862 /* Userspace can't trigger element to be re-added */
1866 return ret > 0 ? 0 : -IPSET_ERR_EXIST;
1869 /* Get headed data of a set */
1871 static int ip_set_header(struct sk_buff *skb, const struct nfnl_info *info,
1872 const struct nlattr * const attr[])
1874 struct ip_set_net *inst = ip_set_pernet(info->net);
1875 const struct ip_set *set;
1876 struct sk_buff *skb2;
1877 struct nlmsghdr *nlh2;
1879 if (unlikely(protocol_min_failed(attr) ||
1880 !attr[IPSET_ATTR_SETNAME]))
1881 return -IPSET_ERR_PROTOCOL;
1883 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1887 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1891 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
1895 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
1896 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
1897 nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
1898 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
1899 nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
1900 goto nla_put_failure;
1901 nlmsg_end(skb2, nlh2);
1903 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
1906 nlmsg_cancel(skb2, nlh2);
1914 static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1915 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1916 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
1917 .len = IPSET_MAXNAMELEN - 1 },
1918 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
1921 static int ip_set_type(struct sk_buff *skb, const struct nfnl_info *info,
1922 const struct nlattr * const attr[])
1924 struct sk_buff *skb2;
1925 struct nlmsghdr *nlh2;
1926 u8 family, min, max;
1927 const char *typename;
1930 if (unlikely(protocol_min_failed(attr) ||
1931 !attr[IPSET_ATTR_TYPENAME] ||
1932 !attr[IPSET_ATTR_FAMILY]))
1933 return -IPSET_ERR_PROTOCOL;
1935 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1936 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1937 ret = find_set_type_minmax(typename, family, &min, &max);
1941 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1945 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
1949 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
1950 nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
1951 nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
1952 nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
1953 nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
1954 goto nla_put_failure;
1955 nlmsg_end(skb2, nlh2);
1957 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
1958 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
1961 nlmsg_cancel(skb2, nlh2);
1967 /* Get protocol version */
1969 static const struct nla_policy
1970 ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1971 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1974 static int ip_set_protocol(struct sk_buff *skb, const struct nfnl_info *info,
1975 const struct nlattr * const attr[])
1977 struct sk_buff *skb2;
1978 struct nlmsghdr *nlh2;
1980 if (unlikely(!attr[IPSET_ATTR_PROTOCOL]))
1981 return -IPSET_ERR_PROTOCOL;
1983 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1987 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
1988 IPSET_CMD_PROTOCOL);
1991 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
1992 goto nla_put_failure;
1993 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL_MIN, IPSET_PROTOCOL_MIN))
1994 goto nla_put_failure;
1995 nlmsg_end(skb2, nlh2);
1997 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
2000 nlmsg_cancel(skb2, nlh2);
2006 /* Get set by name or index, from userspace */
2008 static int ip_set_byname(struct sk_buff *skb, const struct nfnl_info *info,
2009 const struct nlattr * const attr[])
2011 struct ip_set_net *inst = ip_set_pernet(info->net);
2012 struct sk_buff *skb2;
2013 struct nlmsghdr *nlh2;
2014 ip_set_id_t id = IPSET_INVALID_ID;
2015 const struct ip_set *set;
2017 if (unlikely(protocol_failed(attr) ||
2018 !attr[IPSET_ATTR_SETNAME]))
2019 return -IPSET_ERR_PROTOCOL;
2021 set = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]), &id);
2022 if (id == IPSET_INVALID_ID)
2025 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2029 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
2030 IPSET_CMD_GET_BYNAME);
2033 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
2034 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
2035 nla_put_net16(skb2, IPSET_ATTR_INDEX, htons(id)))
2036 goto nla_put_failure;
2037 nlmsg_end(skb2, nlh2);
2039 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
2042 nlmsg_cancel(skb2, nlh2);
2048 static const struct nla_policy ip_set_index_policy[IPSET_ATTR_CMD_MAX + 1] = {
2049 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
2050 [IPSET_ATTR_INDEX] = { .type = NLA_U16 },
2053 static int ip_set_byindex(struct sk_buff *skb, const struct nfnl_info *info,
2054 const struct nlattr * const attr[])
2056 struct ip_set_net *inst = ip_set_pernet(info->net);
2057 struct sk_buff *skb2;
2058 struct nlmsghdr *nlh2;
2059 ip_set_id_t id = IPSET_INVALID_ID;
2060 const struct ip_set *set;
2062 if (unlikely(protocol_failed(attr) ||
2063 !attr[IPSET_ATTR_INDEX]))
2064 return -IPSET_ERR_PROTOCOL;
2066 id = ip_set_get_h16(attr[IPSET_ATTR_INDEX]);
2067 if (id >= inst->ip_set_max)
2069 set = ip_set(inst, id);
2073 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2077 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
2078 IPSET_CMD_GET_BYINDEX);
2081 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
2082 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name))
2083 goto nla_put_failure;
2084 nlmsg_end(skb2, nlh2);
2086 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
2089 nlmsg_cancel(skb2, nlh2);
2095 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
2096 [IPSET_CMD_NONE] = {
2097 .call = ip_set_none,
2098 .type = NFNL_CB_MUTEX,
2099 .attr_count = IPSET_ATTR_CMD_MAX,
2101 [IPSET_CMD_CREATE] = {
2102 .call = ip_set_create,
2103 .type = NFNL_CB_MUTEX,
2104 .attr_count = IPSET_ATTR_CMD_MAX,
2105 .policy = ip_set_create_policy,
2107 [IPSET_CMD_DESTROY] = {
2108 .call = ip_set_destroy,
2109 .type = NFNL_CB_MUTEX,
2110 .attr_count = IPSET_ATTR_CMD_MAX,
2111 .policy = ip_set_setname_policy,
2113 [IPSET_CMD_FLUSH] = {
2114 .call = ip_set_flush,
2115 .type = NFNL_CB_MUTEX,
2116 .attr_count = IPSET_ATTR_CMD_MAX,
2117 .policy = ip_set_setname_policy,
2119 [IPSET_CMD_RENAME] = {
2120 .call = ip_set_rename,
2121 .type = NFNL_CB_MUTEX,
2122 .attr_count = IPSET_ATTR_CMD_MAX,
2123 .policy = ip_set_setname2_policy,
2125 [IPSET_CMD_SWAP] = {
2126 .call = ip_set_swap,
2127 .type = NFNL_CB_MUTEX,
2128 .attr_count = IPSET_ATTR_CMD_MAX,
2129 .policy = ip_set_setname2_policy,
2131 [IPSET_CMD_LIST] = {
2132 .call = ip_set_dump,
2133 .type = NFNL_CB_MUTEX,
2134 .attr_count = IPSET_ATTR_CMD_MAX,
2135 .policy = ip_set_dump_policy,
2137 [IPSET_CMD_SAVE] = {
2138 .call = ip_set_dump,
2139 .type = NFNL_CB_MUTEX,
2140 .attr_count = IPSET_ATTR_CMD_MAX,
2141 .policy = ip_set_setname_policy,
2144 .call = ip_set_uadd,
2145 .type = NFNL_CB_MUTEX,
2146 .attr_count = IPSET_ATTR_CMD_MAX,
2147 .policy = ip_set_adt_policy,
2150 .call = ip_set_udel,
2151 .type = NFNL_CB_MUTEX,
2152 .attr_count = IPSET_ATTR_CMD_MAX,
2153 .policy = ip_set_adt_policy,
2155 [IPSET_CMD_TEST] = {
2156 .call = ip_set_utest,
2157 .type = NFNL_CB_MUTEX,
2158 .attr_count = IPSET_ATTR_CMD_MAX,
2159 .policy = ip_set_adt_policy,
2161 [IPSET_CMD_HEADER] = {
2162 .call = ip_set_header,
2163 .type = NFNL_CB_MUTEX,
2164 .attr_count = IPSET_ATTR_CMD_MAX,
2165 .policy = ip_set_setname_policy,
2167 [IPSET_CMD_TYPE] = {
2168 .call = ip_set_type,
2169 .type = NFNL_CB_MUTEX,
2170 .attr_count = IPSET_ATTR_CMD_MAX,
2171 .policy = ip_set_type_policy,
2173 [IPSET_CMD_PROTOCOL] = {
2174 .call = ip_set_protocol,
2175 .type = NFNL_CB_MUTEX,
2176 .attr_count = IPSET_ATTR_CMD_MAX,
2177 .policy = ip_set_protocol_policy,
2179 [IPSET_CMD_GET_BYNAME] = {
2180 .call = ip_set_byname,
2181 .type = NFNL_CB_MUTEX,
2182 .attr_count = IPSET_ATTR_CMD_MAX,
2183 .policy = ip_set_setname_policy,
2185 [IPSET_CMD_GET_BYINDEX] = {
2186 .call = ip_set_byindex,
2187 .type = NFNL_CB_MUTEX,
2188 .attr_count = IPSET_ATTR_CMD_MAX,
2189 .policy = ip_set_index_policy,
2193 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
2195 .subsys_id = NFNL_SUBSYS_IPSET,
2196 .cb_count = IPSET_MSG_MAX,
2197 .cb = ip_set_netlink_subsys_cb,
2200 /* Interface to iptables/ip6tables */
2203 ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
2207 int copylen = *len, ret = 0;
2208 struct net *net = sock_net(sk);
2209 struct ip_set_net *inst = ip_set_pernet(net);
2211 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2213 if (optval != SO_IP_SET)
2215 if (*len < sizeof(unsigned int))
2218 data = vmalloc(*len);
2221 if (copy_from_user(data, user, *len) != 0) {
2227 if (*op < IP_SET_OP_VERSION) {
2228 /* Check the version at the beginning of operations */
2229 struct ip_set_req_version *req_version = data;
2231 if (*len < sizeof(struct ip_set_req_version)) {
2236 if (req_version->version < IPSET_PROTOCOL_MIN) {
2243 case IP_SET_OP_VERSION: {
2244 struct ip_set_req_version *req_version = data;
2246 if (*len != sizeof(struct ip_set_req_version)) {
2251 req_version->version = IPSET_PROTOCOL;
2252 if (copy_to_user(user, req_version,
2253 sizeof(struct ip_set_req_version)))
2257 case IP_SET_OP_GET_BYNAME: {
2258 struct ip_set_req_get_set *req_get = data;
2261 if (*len != sizeof(struct ip_set_req_get_set)) {
2265 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2266 nfnl_lock(NFNL_SUBSYS_IPSET);
2267 find_set_and_id(inst, req_get->set.name, &id);
2268 req_get->set.index = id;
2269 nfnl_unlock(NFNL_SUBSYS_IPSET);
2272 case IP_SET_OP_GET_FNAME: {
2273 struct ip_set_req_get_set_family *req_get = data;
2276 if (*len != sizeof(struct ip_set_req_get_set_family)) {
2280 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2281 nfnl_lock(NFNL_SUBSYS_IPSET);
2282 find_set_and_id(inst, req_get->set.name, &id);
2283 req_get->set.index = id;
2284 if (id != IPSET_INVALID_ID)
2285 req_get->family = ip_set(inst, id)->family;
2286 nfnl_unlock(NFNL_SUBSYS_IPSET);
2289 case IP_SET_OP_GET_BYINDEX: {
2290 struct ip_set_req_get_set *req_get = data;
2293 if (*len != sizeof(struct ip_set_req_get_set) ||
2294 req_get->set.index >= inst->ip_set_max) {
2298 nfnl_lock(NFNL_SUBSYS_IPSET);
2299 set = ip_set(inst, req_get->set.index);
2300 ret = strscpy(req_get->set.name, set ? set->name : "",
2302 nfnl_unlock(NFNL_SUBSYS_IPSET);
2310 } /* end of switch(op) */
2313 if (copy_to_user(user, data, copylen))
2323 static struct nf_sockopt_ops so_set __read_mostly = {
2325 .get_optmin = SO_IP_SET,
2326 .get_optmax = SO_IP_SET + 1,
2327 .get = ip_set_sockfn_get,
2328 .owner = THIS_MODULE,
2331 static int __net_init
2332 ip_set_net_init(struct net *net)
2334 struct ip_set_net *inst = ip_set_pernet(net);
2335 struct ip_set **list;
2337 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
2338 if (inst->ip_set_max >= IPSET_INVALID_ID)
2339 inst->ip_set_max = IPSET_INVALID_ID - 1;
2341 list = kvcalloc(inst->ip_set_max, sizeof(struct ip_set *), GFP_KERNEL);
2344 inst->is_deleted = false;
2345 inst->is_destroyed = false;
2346 rcu_assign_pointer(inst->ip_set_list, list);
2350 static void __net_exit
2351 ip_set_net_exit(struct net *net)
2353 struct ip_set_net *inst = ip_set_pernet(net);
2355 struct ip_set *set = NULL;
2358 inst->is_deleted = true; /* flag for ip_set_nfnl_put */
2360 nfnl_lock(NFNL_SUBSYS_IPSET);
2361 for (i = 0; i < inst->ip_set_max; i++) {
2362 set = ip_set(inst, i);
2364 ip_set(inst, i) = NULL;
2365 ip_set_destroy_set(set);
2368 nfnl_unlock(NFNL_SUBSYS_IPSET);
2369 kvfree(rcu_dereference_protected(inst->ip_set_list, 1));
2372 static struct pernet_operations ip_set_net_ops = {
2373 .init = ip_set_net_init,
2374 .exit = ip_set_net_exit,
2375 .id = &ip_set_net_id,
2376 .size = sizeof(struct ip_set_net),
2382 int ret = register_pernet_subsys(&ip_set_net_ops);
2385 pr_err("ip_set: cannot register pernet_subsys.\n");
2389 ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
2391 pr_err("ip_set: cannot register with nfnetlink.\n");
2392 unregister_pernet_subsys(&ip_set_net_ops);
2396 ret = nf_register_sockopt(&so_set);
2398 pr_err("SO_SET registry failed: %d\n", ret);
2399 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2400 unregister_pernet_subsys(&ip_set_net_ops);
2410 nf_unregister_sockopt(&so_set);
2411 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2413 unregister_pernet_subsys(&ip_set_net_ops);
2414 pr_debug("these are the famous last words\n");
2417 module_init(ip_set_init);
2418 module_exit(ip_set_fini);
2420 MODULE_DESCRIPTION("ip_set: protocol " __stringify(IPSET_PROTOCOL));