1 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 /* Kernel module for IP set management */
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
16 #include <linux/skbuff.h>
17 #include <linux/spinlock.h>
18 #include <linux/rculist.h>
19 #include <net/netlink.h>
20 #include <net/net_namespace.h>
21 #include <net/netns/generic.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter/x_tables.h>
25 #include <linux/netfilter/nfnetlink.h>
26 #include <linux/netfilter/ipset/ip_set.h>
28 static LIST_HEAD(ip_set_type_list); /* all registered set types */
29 static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */
30 static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */
33 struct ip_set * __rcu *ip_set_list; /* all individual sets */
34 ip_set_id_t ip_set_max; /* max number of sets */
35 bool is_deleted; /* deleted by ip_set_net_exit */
36 bool is_destroyed; /* all sets are destroyed */
39 static unsigned int ip_set_net_id __read_mostly;
41 static inline struct ip_set_net *ip_set_pernet(struct net *net)
43 return net_generic(net, ip_set_net_id);
47 #define STRNCMP(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
49 static unsigned int max_sets;
51 #define _IP_SET_CORE_MODULE_DESC(a) \
52 MODULE_DESCRIPTION("core IP set support (v" a ")")
53 #define IP_SET_CORE_MODULE_DESC(a) \
54 _IP_SET_CORE_MODULE_DESC(__stringify(a))
56 module_param(max_sets, int, 0600);
57 MODULE_PARM_DESC(max_sets, "maximal number of sets");
58 MODULE_LICENSE("GPL");
59 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
60 IP_SET_CORE_MODULE_DESC(PACKAGE_VERSION);
61 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
63 /* When the nfnl mutex is held: */
64 #define ip_set_dereference(p) \
65 rcu_dereference_protected(p, lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET))
66 #define ip_set(inst, id) \
67 ip_set_dereference((inst)->ip_set_list)[id]
69 /* The set types are implemented in modules and registered set types
70 * can be found in ip_set_type_list. Adding/deleting types is
71 * serialized by ip_set_type_mutex.
75 ip_set_type_lock(void)
77 mutex_lock(&ip_set_type_mutex);
81 ip_set_type_unlock(void)
83 mutex_unlock(&ip_set_type_mutex);
86 /* Register and deregister settype */
88 static struct ip_set_type *
89 find_set_type(const char *name, u8 family, u8 revision)
91 struct ip_set_type *type;
93 list_for_each_entry_rcu(type, &ip_set_type_list, list)
94 if (STRNCMP(type->name, name) &&
95 (type->family == family ||
96 type->family == NFPROTO_UNSPEC) &&
97 revision >= type->revision_min &&
98 revision <= type->revision_max)
103 /* Unlock, try to load a set type module and lock again */
105 load_settype(const char *name)
107 nfnl_unlock(NFNL_SUBSYS_IPSET);
108 pr_debug("try to load ip_set_%s\n", name);
109 if (request_module("ip_set_%s", name) < 0) {
110 pr_warn("Can't find ip_set type %s\n", name);
111 nfnl_lock(NFNL_SUBSYS_IPSET);
114 nfnl_lock(NFNL_SUBSYS_IPSET);
118 /* Find a set type and reference it */
119 #define find_set_type_get(name, family, revision, found) \
120 __find_set_type_get(name, family, revision, found, false)
123 __find_set_type_get(const char *name, u8 family, u8 revision,
124 struct ip_set_type **found, bool retry)
126 struct ip_set_type *type;
129 if (retry && !load_settype(name))
130 return -IPSET_ERR_FIND_TYPE;
133 *found = find_set_type(name, family, revision);
135 err = !try_module_get((*found)->me) ? -EFAULT : 0;
138 /* Make sure the type is already loaded
139 * but we don't support the revision
141 list_for_each_entry_rcu(type, &ip_set_type_list, list)
142 if (STRNCMP(type->name, name)) {
143 err = -IPSET_ERR_FIND_TYPE;
148 return retry ? -IPSET_ERR_FIND_TYPE :
149 __find_set_type_get(name, family, revision, found, true);
156 /* Find a given set type by name and family.
157 * If we succeeded, the supported minimal and maximum revisions are
160 #define find_set_type_minmax(name, family, min, max) \
161 __find_set_type_minmax(name, family, min, max, false)
164 __find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max,
167 struct ip_set_type *type;
170 if (retry && !load_settype(name))
171 return -IPSET_ERR_FIND_TYPE;
173 *min = 255; *max = 0;
175 list_for_each_entry_rcu(type, &ip_set_type_list, list)
176 if (STRNCMP(type->name, name) &&
177 (type->family == family ||
178 type->family == NFPROTO_UNSPEC)) {
180 if (type->revision_min < *min)
181 *min = type->revision_min;
182 if (type->revision_max > *max)
183 *max = type->revision_max;
189 return retry ? -IPSET_ERR_FIND_TYPE :
190 __find_set_type_minmax(name, family, min, max, true);
193 #define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \
194 (f) == NFPROTO_IPV6 ? "inet6" : "any")
196 /* Register a set type structure. The type is identified by
197 * the unique triple of name, family and revision.
200 ip_set_type_register(struct ip_set_type *type)
204 if (type->protocol != IPSET_PROTOCOL) {
205 pr_warn("ip_set type %s, family %s, revision %u:%u uses wrong protocol version %u (want %u)\n",
206 type->name, family_name(type->family),
207 type->revision_min, type->revision_max,
208 type->protocol, IPSET_PROTOCOL);
213 if (find_set_type(type->name, type->family, type->revision_min)) {
215 pr_warn("ip_set type %s, family %s with revision min %u already registered!\n",
216 type->name, family_name(type->family),
218 ip_set_type_unlock();
221 list_add_rcu(&type->list, &ip_set_type_list);
222 pr_debug("type %s, family %s, revision %u:%u registered.\n",
223 type->name, family_name(type->family),
224 type->revision_min, type->revision_max);
225 ip_set_type_unlock();
229 EXPORT_SYMBOL_GPL(ip_set_type_register);
231 /* Unregister a set type. There's a small race with ip_set_create */
233 ip_set_type_unregister(struct ip_set_type *type)
236 if (!find_set_type(type->name, type->family, type->revision_min)) {
237 pr_warn("ip_set type %s, family %s with revision min %u not registered\n",
238 type->name, family_name(type->family),
240 ip_set_type_unlock();
243 list_del_rcu(&type->list);
244 pr_debug("type %s, family %s with revision min %u unregistered.\n",
245 type->name, family_name(type->family), type->revision_min);
246 ip_set_type_unlock();
250 EXPORT_SYMBOL_GPL(ip_set_type_unregister);
252 /* Utility functions */
254 ip_set_alloc(size_t size)
256 void *members = NULL;
258 if (size < KMALLOC_MAX_SIZE)
259 members = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
262 pr_debug("%p: allocated with kmalloc\n", members);
266 members = vzalloc(size);
269 pr_debug("%p: allocated with vmalloc\n", members);
273 EXPORT_SYMBOL_GPL(ip_set_alloc);
276 ip_set_free(void *members)
278 pr_debug("%p: free with %s\n", members,
279 is_vmalloc_addr(members) ? "vfree" : "kfree");
282 EXPORT_SYMBOL_GPL(ip_set_free);
285 flag_nested(const struct nlattr *nla)
287 return nla->nla_type & NLA_F_NESTED;
290 static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
291 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
292 [IPSET_ATTR_IPADDR_IPV6] = { .type = NLA_BINARY,
293 .len = sizeof(struct in6_addr) },
297 ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
299 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
301 if (unlikely(!flag_nested(nla)))
302 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_IPV4)))
307 return -IPSET_ERR_PROTOCOL;
309 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
312 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
315 ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
317 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
319 if (unlikely(!flag_nested(nla)))
320 return -IPSET_ERR_PROTOCOL;
322 if (NLA_PARSE_NESTED(tb, IPSET_ATTR_IPADDR_MAX, nla,
323 ipaddr_policy, NULL))
324 return -IPSET_ERR_PROTOCOL;
325 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
326 return -IPSET_ERR_PROTOCOL;
328 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
329 sizeof(struct in6_addr));
332 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
334 typedef void (*destroyer)(struct ip_set *, void *);
335 /* ipset data extension types, in size order */
337 const struct ip_set_ext_type ip_set_extensions[] = {
338 [IPSET_EXT_ID_COUNTER] = {
339 .type = IPSET_EXT_COUNTER,
340 .flag = IPSET_FLAG_WITH_COUNTERS,
341 .len = sizeof(struct ip_set_counter),
342 .align = __alignof__(struct ip_set_counter),
344 [IPSET_EXT_ID_TIMEOUT] = {
345 .type = IPSET_EXT_TIMEOUT,
346 .len = sizeof(unsigned long),
347 .align = __alignof__(unsigned long),
349 [IPSET_EXT_ID_SKBINFO] = {
350 .type = IPSET_EXT_SKBINFO,
351 .flag = IPSET_FLAG_WITH_SKBINFO,
352 .len = sizeof(struct ip_set_skbinfo),
353 .align = __alignof__(struct ip_set_skbinfo),
355 [IPSET_EXT_ID_COMMENT] = {
356 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
357 .flag = IPSET_FLAG_WITH_COMMENT,
358 .len = sizeof(struct ip_set_comment),
359 .align = __alignof__(struct ip_set_comment),
360 .destroy = (destroyer) ip_set_comment_free,
363 EXPORT_SYMBOL_GPL(ip_set_extensions);
366 add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
368 return ip_set_extensions[id].flag ?
369 (flags & ip_set_extensions[id].flag) :
370 !!tb[IPSET_ATTR_TIMEOUT];
374 ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len,
377 enum ip_set_ext_id id;
380 if (tb[IPSET_ATTR_CADT_FLAGS])
381 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
382 if (cadt_flags & IPSET_FLAG_WITH_FORCEADD)
383 set->flags |= IPSET_CREATE_FLAG_FORCEADD;
386 for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
387 if (!add_extension(id, cadt_flags, tb))
389 len = ALIGN(len, ip_set_extensions[id].align);
390 set->offset[id] = len;
391 set->extensions |= ip_set_extensions[id].type;
392 len += ip_set_extensions[id].len;
394 return ALIGN(len, align);
396 EXPORT_SYMBOL_GPL(ip_set_elem_len);
399 ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
400 struct ip_set_ext *ext)
404 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
405 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
406 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
407 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
408 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
409 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
410 return -IPSET_ERR_PROTOCOL;
412 if (tb[IPSET_ATTR_TIMEOUT]) {
413 if (!SET_WITH_TIMEOUT(set))
414 return -IPSET_ERR_TIMEOUT;
415 ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
417 if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) {
418 if (!SET_WITH_COUNTER(set))
419 return -IPSET_ERR_COUNTER;
420 if (tb[IPSET_ATTR_BYTES])
421 ext->bytes = be64_to_cpu(nla_get_be64(
422 tb[IPSET_ATTR_BYTES]));
423 if (tb[IPSET_ATTR_PACKETS])
424 ext->packets = be64_to_cpu(nla_get_be64(
425 tb[IPSET_ATTR_PACKETS]));
427 if (tb[IPSET_ATTR_COMMENT]) {
428 if (!SET_WITH_COMMENT(set))
429 return -IPSET_ERR_COMMENT;
430 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
432 if (tb[IPSET_ATTR_SKBMARK]) {
433 if (!SET_WITH_SKBINFO(set))
434 return -IPSET_ERR_SKBINFO;
435 fullmark = be64_to_cpu(nla_get_be64(tb[IPSET_ATTR_SKBMARK]));
436 ext->skbinfo.skbmark = fullmark >> 32;
437 ext->skbinfo.skbmarkmask = fullmark & 0xffffffff;
439 if (tb[IPSET_ATTR_SKBPRIO]) {
440 if (!SET_WITH_SKBINFO(set))
441 return -IPSET_ERR_SKBINFO;
442 ext->skbinfo.skbprio =
443 be32_to_cpu(nla_get_be32(tb[IPSET_ATTR_SKBPRIO]));
445 if (tb[IPSET_ATTR_SKBQUEUE]) {
446 if (!SET_WITH_SKBINFO(set))
447 return -IPSET_ERR_SKBINFO;
448 ext->skbinfo.skbqueue =
449 be16_to_cpu(nla_get_be16(tb[IPSET_ATTR_SKBQUEUE]));
453 EXPORT_SYMBOL_GPL(ip_set_get_extensions);
456 ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
457 const void *e, bool active)
459 if (SET_WITH_TIMEOUT(set)) {
460 unsigned long *timeout = ext_timeout(e, set);
462 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
463 htonl(active ? ip_set_timeout_get(timeout)
467 if (SET_WITH_COUNTER(set) &&
468 ip_set_put_counter(skb, ext_counter(e, set)))
470 if (SET_WITH_COMMENT(set) &&
471 ip_set_put_comment(skb, ext_comment(e, set)))
473 if (SET_WITH_SKBINFO(set) &&
474 ip_set_put_skbinfo(skb, ext_skbinfo(e, set)))
478 EXPORT_SYMBOL_GPL(ip_set_put_extensions);
481 ip_set_match_extensions(struct ip_set *set, const struct ip_set_ext *ext,
482 struct ip_set_ext *mext, u32 flags, void *data)
484 if (SET_WITH_TIMEOUT(set) &&
485 ip_set_timeout_expired(ext_timeout(data, set)))
487 if (SET_WITH_COUNTER(set)) {
488 struct ip_set_counter *counter = ext_counter(data, set);
490 if (flags & IPSET_FLAG_MATCH_COUNTERS &&
491 !(ip_set_match_counter(ip_set_get_packets(counter),
492 mext->packets, mext->packets_op) &&
493 ip_set_match_counter(ip_set_get_bytes(counter),
494 mext->bytes, mext->bytes_op)))
496 ip_set_update_counter(counter, ext, flags);
498 if (SET_WITH_SKBINFO(set))
499 ip_set_get_skbinfo(ext_skbinfo(data, set),
503 EXPORT_SYMBOL_GPL(ip_set_match_extensions);
505 /* Creating/destroying/renaming/swapping affect the existence and
506 * the properties of a set. All of these can be executed from userspace
507 * only and serialized by the nfnl mutex indirectly from nfnetlink.
509 * Sets are identified by their index in ip_set_list and the index
510 * is used by the external references (set/SET netfilter modules).
512 * The set behind an index may change by swapping only, from userspace.
516 __ip_set_get(struct ip_set *set)
518 write_lock_bh(&ip_set_ref_lock);
520 write_unlock_bh(&ip_set_ref_lock);
524 __ip_set_put(struct ip_set *set)
526 write_lock_bh(&ip_set_ref_lock);
527 BUG_ON(set->ref == 0);
529 write_unlock_bh(&ip_set_ref_lock);
532 /* set->ref can be swapped out by ip_set_swap, netlink events (like dump) need
533 * a separate reference counter
536 __ip_set_put_netlink(struct ip_set *set)
538 write_lock_bh(&ip_set_ref_lock);
539 BUG_ON(set->ref_netlink == 0);
541 write_unlock_bh(&ip_set_ref_lock);
544 /* Add, del and test set entries from kernel.
546 * The set behind the index must exist and must be referenced
547 * so it can't be destroyed (or changed) under our foot.
550 static inline struct ip_set *
551 ip_set_rcu_get(struct net *net, ip_set_id_t index)
554 struct ip_set_net *inst = ip_set_pernet(net);
557 /* ip_set_list itself needs to be protected */
558 set = rcu_dereference(inst->ip_set_list)[index];
565 ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
566 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
568 struct ip_set *set = ip_set_rcu_get(IPSET_DEV_NET(par), index);
572 pr_debug("set %s, index %u\n", set->name, index);
574 if (opt->dim < set->type->dimension ||
575 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
579 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
580 rcu_read_unlock_bh();
582 if (ret == -EAGAIN) {
583 /* Type requests element to be completed */
584 pr_debug("element must be completed, ADD is triggered\n");
585 spin_lock_bh(&set->lock);
586 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
587 spin_unlock_bh(&set->lock);
590 /* --return-nomatch: invert matched element */
591 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
592 (set->type->features & IPSET_TYPE_NOMATCH) &&
593 (ret > 0 || ret == -ENOTEMPTY))
597 /* Convert error codes to nomatch */
598 return (ret < 0 ? 0 : ret);
600 EXPORT_SYMBOL_GPL(ip_set_test);
603 ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
604 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
606 struct ip_set *set = ip_set_rcu_get(IPSET_DEV_NET(par), index);
610 pr_debug("set %s, index %u\n", set->name, index);
612 if (opt->dim < set->type->dimension ||
613 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
614 return -IPSET_ERR_TYPE_MISMATCH;
616 spin_lock_bh(&set->lock);
617 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
618 spin_unlock_bh(&set->lock);
622 EXPORT_SYMBOL_GPL(ip_set_add);
625 ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
626 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
628 struct ip_set *set = ip_set_rcu_get(IPSET_DEV_NET(par), index);
632 pr_debug("set %s, index %u\n", set->name, index);
634 if (opt->dim < set->type->dimension ||
635 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
636 return -IPSET_ERR_TYPE_MISMATCH;
638 spin_lock_bh(&set->lock);
639 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
640 spin_unlock_bh(&set->lock);
644 EXPORT_SYMBOL_GPL(ip_set_del);
646 /* Find set by name, reference it once. The reference makes sure the
647 * thing pointed to, does not go away under our feet.
651 ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
653 ip_set_id_t i, index = IPSET_INVALID_ID;
655 struct ip_set_net *inst = ip_set_pernet(net);
658 for (i = 0; i < inst->ip_set_max; i++) {
659 s = rcu_dereference(inst->ip_set_list)[i];
660 if (s && STRNCMP(s->name, name)) {
671 EXPORT_SYMBOL_GPL(ip_set_get_byname);
673 /* If the given set pointer points to a valid set, decrement
674 * reference count by 1. The caller shall not assume the index
675 * to be valid, after calling this function.
680 __ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
685 set = rcu_dereference(inst->ip_set_list)[index];
692 ip_set_put_byindex(struct net *net, ip_set_id_t index)
694 struct ip_set_net *inst = ip_set_pernet(net);
696 __ip_set_put_byindex(inst, index);
698 EXPORT_SYMBOL_GPL(ip_set_put_byindex);
700 /* Get the name of a set behind a set index.
701 * We assume the set is referenced, so it does exist and
702 * can't be destroyed. The set cannot be renamed due to
703 * the referencing either.
707 ip_set_name_byindex(struct net *net, ip_set_id_t index)
709 const struct ip_set *set = ip_set_rcu_get(net, index);
712 BUG_ON(set->ref == 0);
714 /* Referenced, so it's safe */
717 EXPORT_SYMBOL_GPL(ip_set_name_byindex);
719 /* Routines to call by external subsystems, which do not
720 * call nfnl_lock for us.
723 /* Find set by index, reference it once. The reference makes sure the
724 * thing pointed to, does not go away under our feet.
726 * The nfnl mutex is used in the function.
729 ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
732 struct ip_set_net *inst = ip_set_pernet(net);
734 if (index >= inst->ip_set_max)
735 return IPSET_INVALID_ID;
737 nfnl_lock(NFNL_SUBSYS_IPSET);
738 set = ip_set(inst, index);
742 index = IPSET_INVALID_ID;
743 nfnl_unlock(NFNL_SUBSYS_IPSET);
747 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
749 /* If the given set pointer points to a valid set, decrement
750 * reference count by 1. The caller shall not assume the index
751 * to be valid, after calling this function.
753 * The nfnl mutex is used in the function.
756 ip_set_nfnl_put(struct net *net, ip_set_id_t index)
759 struct ip_set_net *inst = ip_set_pernet(net);
761 nfnl_lock(NFNL_SUBSYS_IPSET);
762 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
763 set = ip_set(inst, index);
767 nfnl_unlock(NFNL_SUBSYS_IPSET);
769 EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
771 /* Communication protocol with userspace over netlink.
773 * The commands are serialized by the nfnl mutex.
777 protocol_failed(const struct nlattr * const tb[])
779 return !tb[IPSET_ATTR_PROTOCOL] ||
780 nla_get_u8(tb[IPSET_ATTR_PROTOCOL]) != IPSET_PROTOCOL;
784 flag_exist(const struct nlmsghdr *nlh)
786 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
789 static struct nlmsghdr *
790 start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
793 struct nlmsghdr *nlh;
794 struct nfgenmsg *nfmsg;
796 nlh = nlmsg_put(skb, portid, seq, nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd),
797 sizeof(*nfmsg), flags);
801 nfmsg = nlmsg_data(nlh);
802 nfmsg->nfgen_family = NFPROTO_IPV4;
803 nfmsg->version = NFNETLINK_V0;
811 static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
812 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
813 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
814 .len = IPSET_MAXNAMELEN - 1 },
815 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
816 .len = IPSET_MAXNAMELEN - 1},
817 [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
818 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
819 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
822 static struct ip_set *
823 find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
825 struct ip_set *set = NULL;
828 *id = IPSET_INVALID_ID;
829 for (i = 0; i < inst->ip_set_max; i++) {
830 set = ip_set(inst, i);
831 if (set && STRNCMP(set->name, name)) {
836 return (*id == IPSET_INVALID_ID ? NULL : set);
839 static inline struct ip_set *
840 find_set(struct ip_set_net *inst, const char *name)
844 return find_set_and_id(inst, name, &id);
848 find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
854 *index = IPSET_INVALID_ID;
855 for (i = 0; i < inst->ip_set_max; i++) {
858 if (*index == IPSET_INVALID_ID)
860 } else if (STRNCMP(name, s->name)) {
866 if (*index == IPSET_INVALID_ID)
867 /* No free slot remained */
868 return -IPSET_ERR_MAX_SETS;
873 IPSET_CBFN(ip_set_none, struct net *net, struct sock *ctnl,
874 struct sk_buff *skb, const struct nlmsghdr *nlh,
875 const struct nlattr * const attr[],
876 struct netlink_ext_ack *extack)
882 IPSET_CBFN(ip_set_create, struct net *n, struct sock *ctnl,
883 struct sk_buff *skb, const struct nlmsghdr *nlh,
884 const struct nlattr * const attr[],
885 struct netlink_ext_ack *extack)
887 struct net *net = IPSET_SOCK_NET(n, ctnl);
888 struct ip_set_net *inst = ip_set_pernet(net);
889 struct ip_set *set, *clash = NULL;
890 ip_set_id_t index = IPSET_INVALID_ID;
891 struct nlattr *tb[IPSET_ATTR_CREATE_MAX + 1] = {};
892 const char *name, *typename;
894 u32 flags = flag_exist(nlh);
897 if (unlikely(protocol_failed(attr) ||
898 !attr[IPSET_ATTR_SETNAME] ||
899 !attr[IPSET_ATTR_TYPENAME] ||
900 !attr[IPSET_ATTR_REVISION] ||
901 !attr[IPSET_ATTR_FAMILY] ||
902 (attr[IPSET_ATTR_DATA] &&
903 !flag_nested(attr[IPSET_ATTR_DATA]))))
904 return -IPSET_ERR_PROTOCOL;
906 name = nla_data(attr[IPSET_ATTR_SETNAME]);
907 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
908 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
909 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
910 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
911 name, typename, family_name(family), revision);
913 /* First, and without any locks, allocate and initialize
914 * a normal base set structure.
916 set = kzalloc(sizeof(*set), GFP_KERNEL);
919 spin_lock_init(&set->lock);
920 strlcpy(set->name, name, IPSET_MAXNAMELEN);
921 set->family = family;
922 set->revision = revision;
924 /* Next, check that we know the type, and take
925 * a reference on the type, to make sure it stays available
926 * while constructing our new set.
928 * After referencing the type, we try to create the type
929 * specific part of the set without holding any locks.
931 ret = find_set_type_get(typename, family, revision, &set->type);
935 /* Without holding any locks, create private part. */
936 if (attr[IPSET_ATTR_DATA] &&
937 NLA_PARSE_NESTED(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
938 set->type->create_policy, NULL)) {
939 ret = -IPSET_ERR_PROTOCOL;
943 ret = set->type->create(net, set, tb, flags);
947 /* BTW, ret==0 here. */
949 /* Here, we have a valid, constructed set and we are protected
950 * by the nfnl mutex. Find the first free index in ip_set_list
951 * and check clashing.
953 ret = find_free_id(inst, set->name, &index, &clash);
954 if (ret == -EEXIST) {
955 /* If this is the same set and requested, ignore error */
956 if ((flags & IPSET_FLAG_EXIST) &&
957 STRNCMP(set->type->name, clash->type->name) &&
958 set->type->family == clash->type->family &&
959 set->type->revision_min == clash->type->revision_min &&
960 set->type->revision_max == clash->type->revision_max &&
961 set->variant->same_set(set, clash))
964 } else if (ret == -IPSET_ERR_MAX_SETS) {
965 struct ip_set **list, **tmp;
966 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
968 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
972 list = kcalloc(i, sizeof(struct ip_set *), GFP_KERNEL);
975 /* nfnl mutex is held, both lists are valid */
976 tmp = ip_set_dereference(inst->ip_set_list);
977 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
978 rcu_assign_pointer(inst->ip_set_list, list);
979 /* Make sure all current packets have passed through */
982 index = inst->ip_set_max;
983 inst->ip_set_max = i;
990 /* Finally! Add our shiny new set to the list, and be done. */
991 pr_debug("create: '%s' created with index %u!\n", set->name, index);
992 ip_set(inst, index) = set;
997 set->variant->destroy(set);
999 module_put(set->type->me);
1007 static const struct nla_policy
1008 ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
1009 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1010 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1011 .len = IPSET_MAXNAMELEN - 1 },
1015 ip_set_destroy_set(struct ip_set *set)
1017 pr_debug("set: %s\n", set->name);
1019 /* Must call it without holding any lock */
1020 set->variant->destroy(set);
1021 module_put(set->type->me);
1026 IPSET_CBFN(ip_set_destroy, struct net *net, struct sock *ctnl,
1027 struct sk_buff *skb, const struct nlmsghdr *nlh,
1028 const struct nlattr * const attr[],
1029 struct netlink_ext_ack *extack)
1031 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl));
1036 if (unlikely(protocol_failed(attr)))
1037 return -IPSET_ERR_PROTOCOL;
1039 /* Must wait for flush to be really finished in list:set */
1042 /* Commands are serialized and references are
1043 * protected by the ip_set_ref_lock.
1044 * External systems (i.e. xt_set) must call
1045 * ip_set_put|get_nfnl_* functions, that way we
1046 * can safely check references here.
1048 * list:set timer can only decrement the reference
1049 * counter, so if it's already zero, we can proceed
1050 * without holding the lock.
1052 read_lock_bh(&ip_set_ref_lock);
1053 if (!attr[IPSET_ATTR_SETNAME]) {
1054 for (i = 0; i < inst->ip_set_max; i++) {
1055 s = ip_set(inst, i);
1056 if (s && (s->ref || s->ref_netlink)) {
1057 ret = -IPSET_ERR_BUSY;
1061 inst->is_destroyed = true;
1062 read_unlock_bh(&ip_set_ref_lock);
1063 for (i = 0; i < inst->ip_set_max; i++) {
1064 s = ip_set(inst, i);
1066 ip_set(inst, i) = NULL;
1067 ip_set_destroy_set(s);
1070 /* Modified by ip_set_destroy() only, which is serialized */
1071 inst->is_destroyed = false;
1073 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1078 } else if (s->ref || s->ref_netlink) {
1079 ret = -IPSET_ERR_BUSY;
1082 ip_set(inst, i) = NULL;
1083 read_unlock_bh(&ip_set_ref_lock);
1085 ip_set_destroy_set(s);
1089 read_unlock_bh(&ip_set_ref_lock);
1096 ip_set_flush_set(struct ip_set *set)
1098 pr_debug("set: %s\n", set->name);
1100 spin_lock_bh(&set->lock);
1101 set->variant->flush(set);
1102 spin_unlock_bh(&set->lock);
1106 IPSET_CBFN(ip_set_flush, struct net *net, struct sock *ctnl,
1107 struct sk_buff *skb, const struct nlmsghdr *nlh,
1108 const struct nlattr * const attr[],
1109 struct netlink_ext_ack *extack)
1111 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl));
1115 if (unlikely(protocol_failed(attr)))
1116 return -IPSET_ERR_PROTOCOL;
1118 if (!attr[IPSET_ATTR_SETNAME]) {
1119 for (i = 0; i < inst->ip_set_max; i++) {
1120 s = ip_set(inst, i);
1122 ip_set_flush_set(s);
1125 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1129 ip_set_flush_set(s);
1137 static const struct nla_policy
1138 ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
1139 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1140 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1141 .len = IPSET_MAXNAMELEN - 1 },
1142 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
1143 .len = IPSET_MAXNAMELEN - 1 },
1147 IPSET_CBFN(ip_set_rename, struct net *net, struct sock *ctnl,
1148 struct sk_buff *skb, const struct nlmsghdr *nlh,
1149 const struct nlattr * const attr[],
1150 struct netlink_ext_ack *extack)
1152 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl));
1153 struct ip_set *set, *s;
1158 if (unlikely(protocol_failed(attr) ||
1159 !attr[IPSET_ATTR_SETNAME] ||
1160 !attr[IPSET_ATTR_SETNAME2]))
1161 return -IPSET_ERR_PROTOCOL;
1163 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1167 read_lock_bh(&ip_set_ref_lock);
1168 if (set->ref != 0) {
1169 ret = -IPSET_ERR_REFERENCED;
1173 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1174 for (i = 0; i < inst->ip_set_max; i++) {
1175 s = ip_set(inst, i);
1176 if (s && STRNCMP(s->name, name2)) {
1177 ret = -IPSET_ERR_EXIST_SETNAME2;
1181 strncpy(set->name, name2, IPSET_MAXNAMELEN);
1184 read_unlock_bh(&ip_set_ref_lock);
1188 /* Swap two sets so that name/index points to the other.
1189 * References and set names are also swapped.
1191 * The commands are serialized by the nfnl mutex and references are
1192 * protected by the ip_set_ref_lock. The kernel interfaces
1193 * do not hold the mutex but the pointer settings are atomic
1194 * so the ip_set_list always contains valid pointers to the sets.
1198 IPSET_CBFN(ip_set_swap, struct net *net, struct sock *ctnl,
1199 struct sk_buff *skb, const struct nlmsghdr *nlh,
1200 const struct nlattr * const attr[],
1201 struct netlink_ext_ack *extack)
1203 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl));
1204 struct ip_set *from, *to;
1205 ip_set_id_t from_id, to_id;
1206 char from_name[IPSET_MAXNAMELEN];
1208 if (unlikely(protocol_failed(attr) ||
1209 !attr[IPSET_ATTR_SETNAME] ||
1210 !attr[IPSET_ATTR_SETNAME2]))
1211 return -IPSET_ERR_PROTOCOL;
1213 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1218 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1221 return -IPSET_ERR_EXIST_SETNAME2;
1223 /* Features must not change.
1224 * Not an artifical restriction anymore, as we must prevent
1225 * possible loops created by swapping in setlist type of sets.
1227 if (!(from->type->features == to->type->features &&
1228 from->family == to->family))
1229 return -IPSET_ERR_TYPE_MISMATCH;
1231 write_lock_bh(&ip_set_ref_lock);
1233 if (from->ref_netlink || to->ref_netlink) {
1234 write_unlock_bh(&ip_set_ref_lock);
1238 strncpy(from_name, from->name, IPSET_MAXNAMELEN);
1239 strncpy(from->name, to->name, IPSET_MAXNAMELEN);
1240 strncpy(to->name, from_name, IPSET_MAXNAMELEN);
1242 swap(from->ref, to->ref);
1243 ip_set(inst, from_id) = to;
1244 ip_set(inst, to_id) = from;
1245 write_unlock_bh(&ip_set_ref_lock);
1250 /* List/save set data */
1257 #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
1258 #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
1261 ip_set_dump_done(struct netlink_callback *cb)
1263 if (cb->args[IPSET_CB_ARG0]) {
1264 struct ip_set_net *inst =
1265 (struct ip_set_net *)cb->args[IPSET_CB_NET];
1266 ip_set_id_t index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1267 struct ip_set *set = ip_set(inst, index);
1269 if (set->variant->uref)
1270 set->variant->uref(set, cb, false);
1271 pr_debug("release set %s\n", set->name);
1272 __ip_set_put_netlink(set);
1278 dump_attrs(struct nlmsghdr *nlh)
1280 const struct nlattr *attr;
1283 pr_debug("dump nlmsg\n");
1284 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
1285 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
1290 dump_init(struct netlink_callback *cb, struct ip_set_net *inst)
1292 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
1293 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1294 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1295 struct nlattr *attr = (void *)nlh + min_len;
1299 /* Second pass, so parser can't fail */
1300 NLA_PARSE(cda, IPSET_ATTR_CMD_MAX, attr, nlh->nlmsg_len - min_len,
1301 ip_set_setname_policy, NULL);
1303 if (cda[IPSET_ATTR_SETNAME]) {
1306 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
1311 dump_type = DUMP_ONE;
1312 cb->args[IPSET_CB_INDEX] = index;
1314 dump_type = DUMP_ALL;
1317 if (cda[IPSET_ATTR_FLAGS]) {
1318 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1320 dump_type |= (f << 16);
1322 cb->args[IPSET_CB_NET] = (unsigned long)inst;
1323 cb->args[IPSET_CB_DUMP] = dump_type;
1329 ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
1331 ip_set_id_t index = IPSET_INVALID_ID, max;
1332 struct ip_set *set = NULL;
1333 struct nlmsghdr *nlh = NULL;
1334 unsigned int flags = NETLINK_PORTID(cb->skb) ? NLM_F_MULTI : 0;
1335 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1336 u32 dump_type, dump_flags;
1340 if (!cb->args[IPSET_CB_DUMP]) {
1341 ret = dump_init(cb, inst);
1343 nlh = nlmsg_hdr(cb->skb);
1344 /* We have to create and send the error message
1347 if (nlh->nlmsg_flags & NLM_F_ACK)
1348 NETLINK_ACK(cb->skb, nlh, ret, NULL);
1353 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
1356 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1357 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1358 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1361 pr_debug("dump type, flag: %u %u index: %ld\n",
1362 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1363 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
1364 index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1365 write_lock_bh(&ip_set_ref_lock);
1366 set = ip_set(inst, index);
1367 is_destroyed = inst->is_destroyed;
1368 if (!set || is_destroyed) {
1369 write_unlock_bh(&ip_set_ref_lock);
1370 if (dump_type == DUMP_ONE) {
1375 /* All sets are just being destroyed */
1381 /* When dumping all sets, we must dump "sorted"
1382 * so that lists (unions of sets) are dumped last.
1384 if (dump_type != DUMP_ONE &&
1385 ((dump_type == DUMP_ALL) ==
1386 !!(set->type->features & IPSET_DUMP_LAST))) {
1387 write_unlock_bh(&ip_set_ref_lock);
1390 pr_debug("List set: %s\n", set->name);
1391 if (!cb->args[IPSET_CB_ARG0]) {
1392 /* Start listing: make sure set won't be destroyed */
1393 pr_debug("reference set\n");
1396 write_unlock_bh(&ip_set_ref_lock);
1397 nlh = start_msg(skb, NETLINK_PORTID(cb->skb),
1398 cb->nlh->nlmsg_seq, flags,
1402 goto release_refcount;
1404 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1405 nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
1406 goto nla_put_failure;
1407 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1409 switch (cb->args[IPSET_CB_ARG0]) {
1411 /* Core header data */
1412 if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
1414 nla_put_u8(skb, IPSET_ATTR_FAMILY,
1416 nla_put_u8(skb, IPSET_ATTR_REVISION,
1418 goto nla_put_failure;
1419 ret = set->variant->head(set, skb);
1421 goto release_refcount;
1422 if (dump_flags & IPSET_FLAG_LIST_HEADER)
1424 if (set->variant->uref)
1425 set->variant->uref(set, cb, true);
1426 /* Fall through and add elements */
1428 ret = set->variant->list(set, skb, cb);
1429 if (!cb->args[IPSET_CB_ARG0])
1430 /* Set is done, proceed with next one */
1432 goto release_refcount;
1435 /* If we dump all sets, continue with dumping last ones */
1436 if (dump_type == DUMP_ALL) {
1437 dump_type = DUMP_LAST;
1438 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1439 cb->args[IPSET_CB_INDEX] = 0;
1440 if (set && set->variant->uref)
1441 set->variant->uref(set, cb, false);
1449 if (dump_type == DUMP_ONE)
1450 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
1452 cb->args[IPSET_CB_INDEX]++;
1454 /* If there was an error or set is done, release set */
1455 if (ret || !cb->args[IPSET_CB_ARG0]) {
1456 set = ip_set(inst, index);
1457 if (set->variant->uref)
1458 set->variant->uref(set, cb, false);
1459 pr_debug("release set %s\n", set->name);
1460 __ip_set_put_netlink(set);
1461 cb->args[IPSET_CB_ARG0] = 0;
1465 nlmsg_end(skb, nlh);
1466 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1470 return ret < 0 ? ret : skb->len;
1474 IPSET_CBFN(ip_set_dump, struct net *net, struct sock *ctnl,
1475 struct sk_buff *skb, const struct nlmsghdr *nlh,
1476 const struct nlattr * const attr[],
1477 struct netlink_ext_ack *extack)
1479 if (unlikely(protocol_failed(attr)))
1480 return -IPSET_ERR_PROTOCOL;
1482 #if HAVE_NETLINK_DUMP_START_ARGS == 5
1483 return netlink_dump_start(ctnl, skb, nlh,
1486 #elif HAVE_NETLINK_DUMP_START_ARGS == 6
1487 return netlink_dump_start(ctnl, skb, nlh,
1489 ip_set_dump_done, 0);
1492 struct netlink_dump_control c = {
1493 .dump = ip_set_dump_start,
1494 .done = ip_set_dump_done,
1496 return netlink_dump_start(ctnl, skb, nlh, &c);
1501 /* Add, del and test */
1503 static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1504 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1505 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1506 .len = IPSET_MAXNAMELEN - 1 },
1507 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
1508 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1509 [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
1513 call_ad(struct sock *ctnl, struct sk_buff *skb, struct ip_set *set,
1514 struct nlattr *tb[], enum ipset_adt adt,
1515 u32 flags, bool use_lineno)
1519 bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
1522 spin_lock_bh(&set->lock);
1523 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1524 spin_unlock_bh(&set->lock);
1526 } while (ret == -EAGAIN &&
1527 set->variant->resize &&
1528 (ret = set->variant->resize(set, retried)) == 0);
1530 if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1532 if (lineno && use_lineno) {
1533 /* Error in restore/batch mode: send back lineno */
1534 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1535 struct sk_buff *skb2;
1536 struct nlmsgerr *errmsg;
1537 size_t payload = min(SIZE_MAX,
1538 sizeof(*errmsg) + nlmsg_len(nlh));
1539 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1540 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1541 struct nlattr *cmdattr;
1544 skb2 = nlmsg_new(payload, GFP_KERNEL);
1547 rep = __nlmsg_put(skb2, NETLINK_PORTID(skb),
1548 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1549 errmsg = nlmsg_data(rep);
1550 errmsg->error = ret;
1551 memcpy(&errmsg->msg, nlh, nlh->nlmsg_len);
1552 cmdattr = (void *)&errmsg->msg + min_len;
1554 NLA_PARSE(cda, IPSET_ATTR_CMD_MAX, cmdattr,
1555 nlh->nlmsg_len - min_len, ip_set_adt_policy, NULL);
1557 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1561 netlink_unicast(ctnl, skb2, NETLINK_PORTID(skb),
1563 /* Signal netlink not to send its ACK/errmsg. */
1571 IPSET_CBFN(ip_set_uadd, struct net *net, struct sock *ctnl,
1572 struct sk_buff *skb, const struct nlmsghdr *nlh,
1573 const struct nlattr * const attr[],
1574 struct netlink_ext_ack *extack)
1576 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl));
1578 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1579 const struct nlattr *nla;
1580 u32 flags = flag_exist(nlh);
1584 if (unlikely(protocol_failed(attr) ||
1585 !attr[IPSET_ATTR_SETNAME] ||
1586 !((attr[IPSET_ATTR_DATA] != NULL) ^
1587 (attr[IPSET_ATTR_ADT] != NULL)) ||
1588 (attr[IPSET_ATTR_DATA] &&
1589 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1590 (attr[IPSET_ATTR_ADT] &&
1591 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1592 !attr[IPSET_ATTR_LINENO]))))
1593 return -IPSET_ERR_PROTOCOL;
1595 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1599 use_lineno = !!attr[IPSET_ATTR_LINENO];
1600 if (attr[IPSET_ATTR_DATA]) {
1601 if (NLA_PARSE_NESTED(tb, IPSET_ATTR_ADT_MAX,
1602 attr[IPSET_ATTR_DATA],
1603 set->type->adt_policy, NULL))
1604 return -IPSET_ERR_PROTOCOL;
1605 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD, flags,
1610 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1611 memset(tb, 0, sizeof(tb));
1612 if (nla_type(nla) != IPSET_ATTR_DATA ||
1613 !flag_nested(nla) ||
1614 NLA_PARSE_NESTED(tb, IPSET_ATTR_ADT_MAX, nla,
1615 set->type->adt_policy, NULL))
1616 return -IPSET_ERR_PROTOCOL;
1617 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD,
1627 IPSET_CBFN(ip_set_udel, struct net *net, struct sock *ctnl,
1628 struct sk_buff *skb, const struct nlmsghdr *nlh,
1629 const struct nlattr * const attr[],
1630 struct netlink_ext_ack *extack)
1632 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl));
1634 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1635 const struct nlattr *nla;
1636 u32 flags = flag_exist(nlh);
1640 if (unlikely(protocol_failed(attr) ||
1641 !attr[IPSET_ATTR_SETNAME] ||
1642 !((attr[IPSET_ATTR_DATA] != NULL) ^
1643 (attr[IPSET_ATTR_ADT] != NULL)) ||
1644 (attr[IPSET_ATTR_DATA] &&
1645 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1646 (attr[IPSET_ATTR_ADT] &&
1647 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1648 !attr[IPSET_ATTR_LINENO]))))
1649 return -IPSET_ERR_PROTOCOL;
1651 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1655 use_lineno = !!attr[IPSET_ATTR_LINENO];
1656 if (attr[IPSET_ATTR_DATA]) {
1657 if (NLA_PARSE_NESTED(tb, IPSET_ATTR_ADT_MAX,
1658 attr[IPSET_ATTR_DATA],
1659 set->type->adt_policy, NULL))
1660 return -IPSET_ERR_PROTOCOL;
1661 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL, flags,
1666 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1667 memset(tb, 0, sizeof(*tb));
1668 if (nla_type(nla) != IPSET_ATTR_DATA ||
1669 !flag_nested(nla) ||
1670 NLA_PARSE_NESTED(tb, IPSET_ATTR_ADT_MAX, nla,
1671 set->type->adt_policy, NULL))
1672 return -IPSET_ERR_PROTOCOL;
1673 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL,
1683 IPSET_CBFN(ip_set_utest, struct net *net, struct sock *ctnl,
1684 struct sk_buff *skb,
1685 const struct nlmsghdr *nlh,
1686 const struct nlattr * const attr[],
1687 struct netlink_ext_ack *extack)
1689 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl));
1691 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1694 if (unlikely(protocol_failed(attr) ||
1695 !attr[IPSET_ATTR_SETNAME] ||
1696 !attr[IPSET_ATTR_DATA] ||
1697 !flag_nested(attr[IPSET_ATTR_DATA])))
1698 return -IPSET_ERR_PROTOCOL;
1700 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1704 if (NLA_PARSE_NESTED(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1705 set->type->adt_policy, NULL))
1706 return -IPSET_ERR_PROTOCOL;
1709 ret = set->variant->uadt(set, tb, IPSET_TEST, NULL, 0, 0);
1710 rcu_read_unlock_bh();
1711 /* Userspace can't trigger element to be re-added */
1715 return ret > 0 ? 0 : -IPSET_ERR_EXIST;
1718 /* Get headed data of a set */
1721 IPSET_CBFN(ip_set_header, struct net *net, struct sock *ctnl,
1722 struct sk_buff *skb, const struct nlmsghdr *nlh,
1723 const struct nlattr * const attr[],
1724 struct netlink_ext_ack *extack)
1726 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl));
1727 const struct ip_set *set;
1728 struct sk_buff *skb2;
1729 struct nlmsghdr *nlh2;
1732 if (unlikely(protocol_failed(attr) ||
1733 !attr[IPSET_ATTR_SETNAME]))
1734 return -IPSET_ERR_PROTOCOL;
1736 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1740 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1744 nlh2 = start_msg(skb2, NETLINK_PORTID(skb), nlh->nlmsg_seq, 0,
1748 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1749 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
1750 nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
1751 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
1752 nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
1753 goto nla_put_failure;
1754 nlmsg_end(skb2, nlh2);
1756 ret = netlink_unicast(ctnl, skb2, NETLINK_PORTID(skb), MSG_DONTWAIT);
1763 nlmsg_cancel(skb2, nlh2);
1771 static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1772 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1773 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
1774 .len = IPSET_MAXNAMELEN - 1 },
1775 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
1779 IPSET_CBFN(ip_set_type, struct net *net, struct sock *ctnl,
1780 struct sk_buff *skb, const struct nlmsghdr *nlh,
1781 const struct nlattr * const attr[],
1782 struct netlink_ext_ack *extack)
1784 struct sk_buff *skb2;
1785 struct nlmsghdr *nlh2;
1786 u8 family, min, max;
1787 const char *typename;
1790 if (unlikely(protocol_failed(attr) ||
1791 !attr[IPSET_ATTR_TYPENAME] ||
1792 !attr[IPSET_ATTR_FAMILY]))
1793 return -IPSET_ERR_PROTOCOL;
1795 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1796 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1797 ret = find_set_type_minmax(typename, family, &min, &max);
1801 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1805 nlh2 = start_msg(skb2, NETLINK_PORTID(skb), nlh->nlmsg_seq, 0,
1809 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1810 nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
1811 nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
1812 nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
1813 nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
1814 goto nla_put_failure;
1815 nlmsg_end(skb2, nlh2);
1817 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
1818 ret = netlink_unicast(ctnl, skb2, NETLINK_PORTID(skb), MSG_DONTWAIT);
1825 nlmsg_cancel(skb2, nlh2);
1831 /* Get protocol version */
1833 static const struct nla_policy
1834 ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1835 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1839 IPSET_CBFN(ip_set_protocol, struct net *net, struct sock *ctnl,
1840 struct sk_buff *skb, const struct nlmsghdr *nlh,
1841 const struct nlattr * const attr[],
1842 struct netlink_ext_ack *extack)
1844 struct sk_buff *skb2;
1845 struct nlmsghdr *nlh2;
1848 if (unlikely(!attr[IPSET_ATTR_PROTOCOL]))
1849 return -IPSET_ERR_PROTOCOL;
1851 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1855 nlh2 = start_msg(skb2, NETLINK_PORTID(skb), nlh->nlmsg_seq, 0,
1856 IPSET_CMD_PROTOCOL);
1859 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
1860 goto nla_put_failure;
1861 nlmsg_end(skb2, nlh2);
1863 ret = netlink_unicast(ctnl, skb2, NETLINK_PORTID(skb), MSG_DONTWAIT);
1870 nlmsg_cancel(skb2, nlh2);
1876 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
1877 [IPSET_CMD_NONE] = {
1878 .call = ip_set_none,
1879 .attr_count = IPSET_ATTR_CMD_MAX,
1881 [IPSET_CMD_CREATE] = {
1882 .call = ip_set_create,
1883 .attr_count = IPSET_ATTR_CMD_MAX,
1884 .policy = ip_set_create_policy,
1886 [IPSET_CMD_DESTROY] = {
1887 .call = ip_set_destroy,
1888 .attr_count = IPSET_ATTR_CMD_MAX,
1889 .policy = ip_set_setname_policy,
1891 [IPSET_CMD_FLUSH] = {
1892 .call = ip_set_flush,
1893 .attr_count = IPSET_ATTR_CMD_MAX,
1894 .policy = ip_set_setname_policy,
1896 [IPSET_CMD_RENAME] = {
1897 .call = ip_set_rename,
1898 .attr_count = IPSET_ATTR_CMD_MAX,
1899 .policy = ip_set_setname2_policy,
1901 [IPSET_CMD_SWAP] = {
1902 .call = ip_set_swap,
1903 .attr_count = IPSET_ATTR_CMD_MAX,
1904 .policy = ip_set_setname2_policy,
1906 [IPSET_CMD_LIST] = {
1907 .call = ip_set_dump,
1908 .attr_count = IPSET_ATTR_CMD_MAX,
1909 .policy = ip_set_setname_policy,
1911 [IPSET_CMD_SAVE] = {
1912 .call = ip_set_dump,
1913 .attr_count = IPSET_ATTR_CMD_MAX,
1914 .policy = ip_set_setname_policy,
1917 .call = ip_set_uadd,
1918 .attr_count = IPSET_ATTR_CMD_MAX,
1919 .policy = ip_set_adt_policy,
1922 .call = ip_set_udel,
1923 .attr_count = IPSET_ATTR_CMD_MAX,
1924 .policy = ip_set_adt_policy,
1926 [IPSET_CMD_TEST] = {
1927 .call = ip_set_utest,
1928 .attr_count = IPSET_ATTR_CMD_MAX,
1929 .policy = ip_set_adt_policy,
1931 [IPSET_CMD_HEADER] = {
1932 .call = ip_set_header,
1933 .attr_count = IPSET_ATTR_CMD_MAX,
1934 .policy = ip_set_setname_policy,
1936 [IPSET_CMD_TYPE] = {
1937 .call = ip_set_type,
1938 .attr_count = IPSET_ATTR_CMD_MAX,
1939 .policy = ip_set_type_policy,
1941 [IPSET_CMD_PROTOCOL] = {
1942 .call = ip_set_protocol,
1943 .attr_count = IPSET_ATTR_CMD_MAX,
1944 .policy = ip_set_protocol_policy,
1948 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
1950 .subsys_id = NFNL_SUBSYS_IPSET,
1951 .cb_count = IPSET_MSG_MAX,
1952 .cb = ip_set_netlink_subsys_cb,
1955 /* Interface to iptables/ip6tables */
1958 ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
1962 int copylen = *len, ret = 0;
1963 struct net *net = sock_net(sk);
1964 struct ip_set_net *inst = ip_set_pernet(net);
1966 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1968 if (optval != SO_IP_SET)
1970 if (*len < sizeof(unsigned int))
1973 data = vmalloc(*len);
1976 if (copy_from_user(data, user, *len) != 0) {
1982 if (*op < IP_SET_OP_VERSION) {
1983 /* Check the version at the beginning of operations */
1984 struct ip_set_req_version *req_version = data;
1986 if (*len < sizeof(struct ip_set_req_version)) {
1991 if (req_version->version != IPSET_PROTOCOL) {
1998 case IP_SET_OP_VERSION: {
1999 struct ip_set_req_version *req_version = data;
2001 if (*len != sizeof(struct ip_set_req_version)) {
2006 req_version->version = IPSET_PROTOCOL;
2007 ret = copy_to_user(user, req_version,
2008 sizeof(struct ip_set_req_version));
2011 case IP_SET_OP_GET_BYNAME: {
2012 struct ip_set_req_get_set *req_get = data;
2015 if (*len != sizeof(struct ip_set_req_get_set)) {
2019 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2020 nfnl_lock(NFNL_SUBSYS_IPSET);
2021 find_set_and_id(inst, req_get->set.name, &id);
2022 req_get->set.index = id;
2023 nfnl_unlock(NFNL_SUBSYS_IPSET);
2026 case IP_SET_OP_GET_FNAME: {
2027 struct ip_set_req_get_set_family *req_get = data;
2030 if (*len != sizeof(struct ip_set_req_get_set_family)) {
2034 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2035 nfnl_lock(NFNL_SUBSYS_IPSET);
2036 find_set_and_id(inst, req_get->set.name, &id);
2037 req_get->set.index = id;
2038 if (id != IPSET_INVALID_ID)
2039 req_get->family = ip_set(inst, id)->family;
2040 nfnl_unlock(NFNL_SUBSYS_IPSET);
2043 case IP_SET_OP_GET_BYINDEX: {
2044 struct ip_set_req_get_set *req_get = data;
2047 if (*len != sizeof(struct ip_set_req_get_set) ||
2048 req_get->set.index >= inst->ip_set_max) {
2052 nfnl_lock(NFNL_SUBSYS_IPSET);
2053 set = ip_set(inst, req_get->set.index);
2054 strncpy(req_get->set.name, set ? set->name : "",
2056 nfnl_unlock(NFNL_SUBSYS_IPSET);
2062 } /* end of switch(op) */
2065 ret = copy_to_user(user, data, copylen);
2074 static struct nf_sockopt_ops so_set __read_mostly = {
2076 .get_optmin = SO_IP_SET,
2077 .get_optmax = SO_IP_SET + 1,
2078 .get = ip_set_sockfn_get,
2079 .owner = THIS_MODULE,
2082 static int __net_init
2083 ip_set_net_init(struct net *net)
2085 struct ip_set_net *inst;
2086 struct ip_set **list;
2088 #ifdef HAVE_NET_OPS_ID
2089 inst = ip_set_pernet(net);
2093 inst = kzalloc(sizeof(struct ip_set_net), GFP_KERNEL);
2096 err = net_assign_generic(net, ip_set_net_id, inst);
2100 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
2101 if (inst->ip_set_max >= IPSET_INVALID_ID)
2102 inst->ip_set_max = IPSET_INVALID_ID - 1;
2104 list = kcalloc(inst->ip_set_max, sizeof(struct ip_set *), GFP_KERNEL);
2106 #ifdef HAVE_NET_OPS_ID
2111 inst->is_deleted = false;
2112 inst->is_destroyed = false;
2113 rcu_assign_pointer(inst->ip_set_list, list);
2116 #ifndef HAVE_NET_OPS_ID
2123 static void __net_exit
2124 ip_set_net_exit(struct net *net)
2126 struct ip_set_net *inst = ip_set_pernet(net);
2128 struct ip_set *set = NULL;
2131 inst->is_deleted = true; /* flag for ip_set_nfnl_put */
2133 nfnl_lock(NFNL_SUBSYS_IPSET);
2134 for (i = 0; i < inst->ip_set_max; i++) {
2135 set = ip_set(inst, i);
2137 ip_set(inst, i) = NULL;
2138 ip_set_destroy_set(set);
2141 nfnl_unlock(NFNL_SUBSYS_IPSET);
2142 kfree(rcu_dereference_protected(inst->ip_set_list, 1));
2143 #ifndef HAVE_NET_OPS_ID
2148 static struct pernet_operations ip_set_net_ops = {
2149 .init = ip_set_net_init,
2150 .exit = ip_set_net_exit,
2151 #ifdef HAVE_NET_OPS_ID
2152 .id = &ip_set_net_id,
2153 .size = sizeof(struct ip_set_net)
2157 #ifdef HAVE_NET_OPS_ID
2158 #define REGISTER_PERNET_SUBSYS(s) \
2159 register_pernet_subsys(s)
2160 #define UNREGISTER_PERNET_SUBSYS(s) \
2161 unregister_pernet_subsys(s);
2163 #define REGISTER_PERNET_SUBSYS(s) \
2164 register_pernet_gen_device(&ip_set_net_id, s)
2165 #define UNREGISTER_PERNET_SUBSYS(s) \
2166 unregister_pernet_gen_device(ip_set_net_id, s);
2173 int ret = REGISTER_PERNET_SUBSYS(&ip_set_net_ops);
2176 pr_err("ip_set: cannot register pernet_subsys.\n");
2180 ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
2182 pr_err("ip_set: cannot register with nfnetlink.\n");
2183 UNREGISTER_PERNET_SUBSYS(&ip_set_net_ops);
2187 ret = nf_register_sockopt(&so_set);
2189 pr_err("SO_SET registry failed: %d\n", ret);
2190 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2191 UNREGISTER_PERNET_SUBSYS(&ip_set_net_ops);
2195 pr_info("ip_set: protocol %u\n", IPSET_PROTOCOL);
2202 nf_unregister_sockopt(&so_set);
2203 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2205 UNREGISTER_PERNET_SUBSYS(&ip_set_net_ops);
2206 pr_debug("these are the famous last words\n");
2209 module_init(ip_set_init);
2210 module_exit(ip_set_fini);