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 module_param(max_sets, int, 0600);
52 MODULE_PARM_DESC(max_sets, "maximal number of sets");
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
55 MODULE_DESCRIPTION("core IP set support");
56 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
58 /* When the nfnl mutex is held: */
59 #define ip_set_dereference(p) \
60 rcu_dereference_protected(p, 1)
61 #define ip_set(inst, id) \
62 ip_set_dereference((inst)->ip_set_list)[id]
64 /* The set types are implemented in modules and registered set types
65 * can be found in ip_set_type_list. Adding/deleting types is
66 * serialized by ip_set_type_mutex.
70 ip_set_type_lock(void)
72 mutex_lock(&ip_set_type_mutex);
76 ip_set_type_unlock(void)
78 mutex_unlock(&ip_set_type_mutex);
81 /* Register and deregister settype */
83 static struct ip_set_type *
84 find_set_type(const char *name, u8 family, u8 revision)
86 struct ip_set_type *type;
88 list_for_each_entry_rcu(type, &ip_set_type_list, list)
89 if (STRNCMP(type->name, name) &&
90 (type->family == family ||
91 type->family == NFPROTO_UNSPEC) &&
92 revision >= type->revision_min &&
93 revision <= type->revision_max)
98 /* Unlock, try to load a set type module and lock again */
100 load_settype(const char *name)
102 nfnl_unlock(NFNL_SUBSYS_IPSET);
103 pr_debug("try to load ip_set_%s\n", name);
104 if (request_module("ip_set_%s", name) < 0) {
105 pr_warn("Can't find ip_set type %s\n", name);
106 nfnl_lock(NFNL_SUBSYS_IPSET);
109 nfnl_lock(NFNL_SUBSYS_IPSET);
113 /* Find a set type and reference it */
114 #define find_set_type_get(name, family, revision, found) \
115 __find_set_type_get(name, family, revision, found, false)
118 __find_set_type_get(const char *name, u8 family, u8 revision,
119 struct ip_set_type **found, bool retry)
121 struct ip_set_type *type;
124 if (retry && !load_settype(name))
125 return -IPSET_ERR_FIND_TYPE;
128 *found = find_set_type(name, family, revision);
130 err = !try_module_get((*found)->me) ? -EFAULT : 0;
133 /* Make sure the type is already loaded
134 * but we don't support the revision
136 list_for_each_entry_rcu(type, &ip_set_type_list, list)
137 if (STRNCMP(type->name, name)) {
138 err = -IPSET_ERR_FIND_TYPE;
143 return retry ? -IPSET_ERR_FIND_TYPE :
144 __find_set_type_get(name, family, revision, found, true);
151 /* Find a given set type by name and family.
152 * If we succeeded, the supported minimal and maximum revisions are
155 #define find_set_type_minmax(name, family, min, max) \
156 __find_set_type_minmax(name, family, min, max, false)
159 __find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max,
162 struct ip_set_type *type;
165 if (retry && !load_settype(name))
166 return -IPSET_ERR_FIND_TYPE;
168 *min = 255; *max = 0;
170 list_for_each_entry_rcu(type, &ip_set_type_list, list)
171 if (STRNCMP(type->name, name) &&
172 (type->family == family ||
173 type->family == NFPROTO_UNSPEC)) {
175 if (type->revision_min < *min)
176 *min = type->revision_min;
177 if (type->revision_max > *max)
178 *max = type->revision_max;
184 return retry ? -IPSET_ERR_FIND_TYPE :
185 __find_set_type_minmax(name, family, min, max, true);
188 #define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \
189 (f) == NFPROTO_IPV6 ? "inet6" : "any")
191 /* Register a set type structure. The type is identified by
192 * the unique triple of name, family and revision.
195 ip_set_type_register(struct ip_set_type *type)
199 if (type->protocol != IPSET_PROTOCOL) {
200 pr_warn("ip_set type %s, family %s, revision %u:%u uses wrong protocol version %u (want %u)\n",
201 type->name, family_name(type->family),
202 type->revision_min, type->revision_max,
203 type->protocol, IPSET_PROTOCOL);
208 if (find_set_type(type->name, type->family, type->revision_min)) {
210 pr_warn("ip_set type %s, family %s with revision min %u already registered!\n",
211 type->name, family_name(type->family),
213 ip_set_type_unlock();
216 list_add_rcu(&type->list, &ip_set_type_list);
217 pr_debug("type %s, family %s, revision %u:%u registered.\n",
218 type->name, family_name(type->family),
219 type->revision_min, type->revision_max);
220 ip_set_type_unlock();
224 EXPORT_SYMBOL_GPL(ip_set_type_register);
226 /* Unregister a set type. There's a small race with ip_set_create */
228 ip_set_type_unregister(struct ip_set_type *type)
231 if (!find_set_type(type->name, type->family, type->revision_min)) {
232 pr_warn("ip_set type %s, family %s with revision min %u not registered\n",
233 type->name, family_name(type->family),
235 ip_set_type_unlock();
238 list_del_rcu(&type->list);
239 pr_debug("type %s, family %s with revision min %u unregistered.\n",
240 type->name, family_name(type->family), type->revision_min);
241 ip_set_type_unlock();
245 EXPORT_SYMBOL_GPL(ip_set_type_unregister);
247 /* Utility functions */
249 ip_set_alloc(size_t size)
251 void *members = NULL;
253 if (size < KMALLOC_MAX_SIZE)
254 members = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
257 pr_debug("%p: allocated with kmalloc\n", members);
261 members = vzalloc(size);
264 pr_debug("%p: allocated with vmalloc\n", members);
268 EXPORT_SYMBOL_GPL(ip_set_alloc);
271 ip_set_free(void *members)
273 pr_debug("%p: free with %s\n", members,
274 is_vmalloc_addr(members) ? "vfree" : "kfree");
277 EXPORT_SYMBOL_GPL(ip_set_free);
280 flag_nested(const struct nlattr *nla)
282 return nla->nla_type & NLA_F_NESTED;
285 static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
286 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
287 [IPSET_ATTR_IPADDR_IPV6] = { .type = NLA_BINARY,
288 .len = sizeof(struct in6_addr) },
292 ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
294 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
296 if (unlikely(!flag_nested(nla)))
297 return -IPSET_ERR_PROTOCOL;
298 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla,
299 ipaddr_policy, NULL))
300 return -IPSET_ERR_PROTOCOL;
301 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
302 return -IPSET_ERR_PROTOCOL;
304 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
307 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
310 ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
312 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
314 if (unlikely(!flag_nested(nla)))
315 return -IPSET_ERR_PROTOCOL;
317 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla,
318 ipaddr_policy, NULL))
319 return -IPSET_ERR_PROTOCOL;
320 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
321 return -IPSET_ERR_PROTOCOL;
323 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
324 sizeof(struct in6_addr));
327 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
329 typedef void (*destroyer)(struct ip_set *, void *);
330 /* ipset data extension types, in size order */
332 const struct ip_set_ext_type ip_set_extensions[] = {
333 [IPSET_EXT_ID_COUNTER] = {
334 .type = IPSET_EXT_COUNTER,
335 .flag = IPSET_FLAG_WITH_COUNTERS,
336 .len = sizeof(struct ip_set_counter),
337 .align = __alignof__(struct ip_set_counter),
339 [IPSET_EXT_ID_TIMEOUT] = {
340 .type = IPSET_EXT_TIMEOUT,
341 .len = sizeof(unsigned long),
342 .align = __alignof__(unsigned long),
344 [IPSET_EXT_ID_SKBINFO] = {
345 .type = IPSET_EXT_SKBINFO,
346 .flag = IPSET_FLAG_WITH_SKBINFO,
347 .len = sizeof(struct ip_set_skbinfo),
348 .align = __alignof__(struct ip_set_skbinfo),
350 [IPSET_EXT_ID_COMMENT] = {
351 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
352 .flag = IPSET_FLAG_WITH_COMMENT,
353 .len = sizeof(struct ip_set_comment),
354 .align = __alignof__(struct ip_set_comment),
355 .destroy = (destroyer) ip_set_comment_free,
358 EXPORT_SYMBOL_GPL(ip_set_extensions);
361 add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
363 return ip_set_extensions[id].flag ?
364 (flags & ip_set_extensions[id].flag) :
365 !!tb[IPSET_ATTR_TIMEOUT];
369 ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len,
372 enum ip_set_ext_id id;
375 if (tb[IPSET_ATTR_CADT_FLAGS])
376 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
377 if (cadt_flags & IPSET_FLAG_WITH_FORCEADD)
378 set->flags |= IPSET_CREATE_FLAG_FORCEADD;
381 for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
382 if (!add_extension(id, cadt_flags, tb))
384 len = ALIGN(len, ip_set_extensions[id].align);
385 set->offset[id] = len;
386 set->extensions |= ip_set_extensions[id].type;
387 len += ip_set_extensions[id].len;
389 return ALIGN(len, align);
391 EXPORT_SYMBOL_GPL(ip_set_elem_len);
394 ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
395 struct ip_set_ext *ext)
399 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
400 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
401 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
402 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
403 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
404 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
405 return -IPSET_ERR_PROTOCOL;
407 if (tb[IPSET_ATTR_TIMEOUT]) {
408 if (!SET_WITH_TIMEOUT(set))
409 return -IPSET_ERR_TIMEOUT;
410 ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
412 if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) {
413 if (!SET_WITH_COUNTER(set))
414 return -IPSET_ERR_COUNTER;
415 if (tb[IPSET_ATTR_BYTES])
416 ext->bytes = be64_to_cpu(nla_get_be64(
417 tb[IPSET_ATTR_BYTES]));
418 if (tb[IPSET_ATTR_PACKETS])
419 ext->packets = be64_to_cpu(nla_get_be64(
420 tb[IPSET_ATTR_PACKETS]));
422 if (tb[IPSET_ATTR_COMMENT]) {
423 if (!SET_WITH_COMMENT(set))
424 return -IPSET_ERR_COMMENT;
425 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
427 if (tb[IPSET_ATTR_SKBMARK]) {
428 if (!SET_WITH_SKBINFO(set))
429 return -IPSET_ERR_SKBINFO;
430 fullmark = be64_to_cpu(nla_get_be64(tb[IPSET_ATTR_SKBMARK]));
431 ext->skbinfo.skbmark = fullmark >> 32;
432 ext->skbinfo.skbmarkmask = fullmark & 0xffffffff;
434 if (tb[IPSET_ATTR_SKBPRIO]) {
435 if (!SET_WITH_SKBINFO(set))
436 return -IPSET_ERR_SKBINFO;
437 ext->skbinfo.skbprio =
438 be32_to_cpu(nla_get_be32(tb[IPSET_ATTR_SKBPRIO]));
440 if (tb[IPSET_ATTR_SKBQUEUE]) {
441 if (!SET_WITH_SKBINFO(set))
442 return -IPSET_ERR_SKBINFO;
443 ext->skbinfo.skbqueue =
444 be16_to_cpu(nla_get_be16(tb[IPSET_ATTR_SKBQUEUE]));
448 EXPORT_SYMBOL_GPL(ip_set_get_extensions);
451 ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
452 const void *e, bool active)
454 if (SET_WITH_TIMEOUT(set)) {
455 unsigned long *timeout = ext_timeout(e, set);
457 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
458 htonl(active ? ip_set_timeout_get(timeout)
462 if (SET_WITH_COUNTER(set) &&
463 ip_set_put_counter(skb, ext_counter(e, set)))
465 if (SET_WITH_COMMENT(set) &&
466 ip_set_put_comment(skb, ext_comment(e, set)))
468 if (SET_WITH_SKBINFO(set) &&
469 ip_set_put_skbinfo(skb, ext_skbinfo(e, set)))
473 EXPORT_SYMBOL_GPL(ip_set_put_extensions);
475 /* Creating/destroying/renaming/swapping affect the existence and
476 * the properties of a set. All of these can be executed from userspace
477 * only and serialized by the nfnl mutex indirectly from nfnetlink.
479 * Sets are identified by their index in ip_set_list and the index
480 * is used by the external references (set/SET netfilter modules).
482 * The set behind an index may change by swapping only, from userspace.
486 __ip_set_get(struct ip_set *set)
488 write_lock_bh(&ip_set_ref_lock);
490 write_unlock_bh(&ip_set_ref_lock);
494 __ip_set_put(struct ip_set *set)
496 write_lock_bh(&ip_set_ref_lock);
497 BUG_ON(set->ref == 0);
499 write_unlock_bh(&ip_set_ref_lock);
502 /* set->ref can be swapped out by ip_set_swap, netlink events (like dump) need
503 * a separate reference counter
506 __ip_set_put_netlink(struct ip_set *set)
508 write_lock_bh(&ip_set_ref_lock);
509 BUG_ON(set->ref_netlink == 0);
511 write_unlock_bh(&ip_set_ref_lock);
514 /* Add, del and test set entries from kernel.
516 * The set behind the index must exist and must be referenced
517 * so it can't be destroyed (or changed) under our foot.
520 static inline struct ip_set *
521 ip_set_rcu_get(struct net *net, ip_set_id_t index)
524 struct ip_set_net *inst = ip_set_pernet(net);
527 /* ip_set_list itself needs to be protected */
528 set = rcu_dereference(inst->ip_set_list)[index];
535 ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
536 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
538 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
542 pr_debug("set %s, index %u\n", set->name, index);
544 if (opt->dim < set->type->dimension ||
545 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
549 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
550 rcu_read_unlock_bh();
552 if (ret == -EAGAIN) {
553 /* Type requests element to be completed */
554 pr_debug("element must be completed, ADD is triggered\n");
555 spin_lock_bh(&set->lock);
556 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
557 spin_unlock_bh(&set->lock);
560 /* --return-nomatch: invert matched element */
561 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
562 (set->type->features & IPSET_TYPE_NOMATCH) &&
563 (ret > 0 || ret == -ENOTEMPTY))
567 /* Convert error codes to nomatch */
568 return (ret < 0 ? 0 : ret);
570 EXPORT_SYMBOL_GPL(ip_set_test);
573 ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
574 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
576 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
580 pr_debug("set %s, index %u\n", set->name, index);
582 if (opt->dim < set->type->dimension ||
583 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
584 return -IPSET_ERR_TYPE_MISMATCH;
586 spin_lock_bh(&set->lock);
587 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
588 spin_unlock_bh(&set->lock);
592 EXPORT_SYMBOL_GPL(ip_set_add);
595 ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
596 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
598 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
602 pr_debug("set %s, index %u\n", set->name, index);
604 if (opt->dim < set->type->dimension ||
605 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
606 return -IPSET_ERR_TYPE_MISMATCH;
608 spin_lock_bh(&set->lock);
609 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
610 spin_unlock_bh(&set->lock);
614 EXPORT_SYMBOL_GPL(ip_set_del);
616 /* Find set by name, reference it once. The reference makes sure the
617 * thing pointed to, does not go away under our feet.
621 ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
623 ip_set_id_t i, index = IPSET_INVALID_ID;
625 struct ip_set_net *inst = ip_set_pernet(net);
628 for (i = 0; i < inst->ip_set_max; i++) {
629 s = rcu_dereference(inst->ip_set_list)[i];
630 if (s && STRNCMP(s->name, name)) {
641 EXPORT_SYMBOL_GPL(ip_set_get_byname);
643 /* If the given set pointer points to a valid set, decrement
644 * reference count by 1. The caller shall not assume the index
645 * to be valid, after calling this function.
650 __ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
655 set = rcu_dereference(inst->ip_set_list)[index];
662 ip_set_put_byindex(struct net *net, ip_set_id_t index)
664 struct ip_set_net *inst = ip_set_pernet(net);
666 __ip_set_put_byindex(inst, index);
668 EXPORT_SYMBOL_GPL(ip_set_put_byindex);
670 /* Get the name of a set behind a set index.
671 * We assume the set is referenced, so it does exist and
672 * can't be destroyed. The set cannot be renamed due to
673 * the referencing either.
677 ip_set_name_byindex(struct net *net, ip_set_id_t index)
679 const struct ip_set *set = ip_set_rcu_get(net, index);
682 BUG_ON(set->ref == 0);
684 /* Referenced, so it's safe */
687 EXPORT_SYMBOL_GPL(ip_set_name_byindex);
689 /* Routines to call by external subsystems, which do not
690 * call nfnl_lock for us.
693 /* Find set by index, reference it once. The reference makes sure the
694 * thing pointed to, does not go away under our feet.
696 * The nfnl mutex is used in the function.
699 ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
702 struct ip_set_net *inst = ip_set_pernet(net);
704 if (index >= inst->ip_set_max)
705 return IPSET_INVALID_ID;
707 nfnl_lock(NFNL_SUBSYS_IPSET);
708 set = ip_set(inst, index);
712 index = IPSET_INVALID_ID;
713 nfnl_unlock(NFNL_SUBSYS_IPSET);
717 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
719 /* If the given set pointer points to a valid set, decrement
720 * reference count by 1. The caller shall not assume the index
721 * to be valid, after calling this function.
723 * The nfnl mutex is used in the function.
726 ip_set_nfnl_put(struct net *net, ip_set_id_t index)
729 struct ip_set_net *inst = ip_set_pernet(net);
731 nfnl_lock(NFNL_SUBSYS_IPSET);
732 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
733 set = ip_set(inst, index);
737 nfnl_unlock(NFNL_SUBSYS_IPSET);
739 EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
741 /* Communication protocol with userspace over netlink.
743 * The commands are serialized by the nfnl mutex.
747 protocol_failed(const struct nlattr * const tb[])
749 return !tb[IPSET_ATTR_PROTOCOL] ||
750 nla_get_u8(tb[IPSET_ATTR_PROTOCOL]) != IPSET_PROTOCOL;
754 flag_exist(const struct nlmsghdr *nlh)
756 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
759 static struct nlmsghdr *
760 start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
763 struct nlmsghdr *nlh;
764 struct nfgenmsg *nfmsg;
766 nlh = nlmsg_put(skb, portid, seq, nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd),
767 sizeof(*nfmsg), flags);
771 nfmsg = nlmsg_data(nlh);
772 nfmsg->nfgen_family = NFPROTO_IPV4;
773 nfmsg->version = NFNETLINK_V0;
781 static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
782 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
783 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
784 .len = IPSET_MAXNAMELEN - 1 },
785 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
786 .len = IPSET_MAXNAMELEN - 1},
787 [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
788 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
789 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
792 static struct ip_set *
793 find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
795 struct ip_set *set = NULL;
798 *id = IPSET_INVALID_ID;
799 for (i = 0; i < inst->ip_set_max; i++) {
800 set = ip_set(inst, i);
801 if (set && STRNCMP(set->name, name)) {
806 return (*id == IPSET_INVALID_ID ? NULL : set);
809 static inline struct ip_set *
810 find_set(struct ip_set_net *inst, const char *name)
814 return find_set_and_id(inst, name, &id);
818 find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
824 *index = IPSET_INVALID_ID;
825 for (i = 0; i < inst->ip_set_max; i++) {
828 if (*index == IPSET_INVALID_ID)
830 } else if (STRNCMP(name, s->name)) {
836 if (*index == IPSET_INVALID_ID)
837 /* No free slot remained */
838 return -IPSET_ERR_MAX_SETS;
842 static int ip_set_none(struct net *net, struct sock *ctnl, struct sk_buff *skb,
843 const struct nlmsghdr *nlh,
844 const struct nlattr * const attr[])
849 static int ip_set_create(struct net *net, struct sock *ctnl,
850 struct sk_buff *skb, const struct nlmsghdr *nlh,
851 const struct nlattr * const attr[])
853 struct ip_set_net *inst = ip_set_pernet(net);
854 struct ip_set *set, *clash = NULL;
855 ip_set_id_t index = IPSET_INVALID_ID;
856 struct nlattr *tb[IPSET_ATTR_CREATE_MAX + 1] = {};
857 const char *name, *typename;
859 u32 flags = flag_exist(nlh);
862 if (unlikely(protocol_failed(attr) ||
863 !attr[IPSET_ATTR_SETNAME] ||
864 !attr[IPSET_ATTR_TYPENAME] ||
865 !attr[IPSET_ATTR_REVISION] ||
866 !attr[IPSET_ATTR_FAMILY] ||
867 (attr[IPSET_ATTR_DATA] &&
868 !flag_nested(attr[IPSET_ATTR_DATA]))))
869 return -IPSET_ERR_PROTOCOL;
871 name = nla_data(attr[IPSET_ATTR_SETNAME]);
872 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
873 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
874 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
875 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
876 name, typename, family_name(family), revision);
878 /* First, and without any locks, allocate and initialize
879 * a normal base set structure.
881 set = kzalloc(sizeof(*set), GFP_KERNEL);
884 spin_lock_init(&set->lock);
885 strlcpy(set->name, name, IPSET_MAXNAMELEN);
886 set->family = family;
887 set->revision = revision;
889 /* Next, check that we know the type, and take
890 * a reference on the type, to make sure it stays available
891 * while constructing our new set.
893 * After referencing the type, we try to create the type
894 * specific part of the set without holding any locks.
896 ret = find_set_type_get(typename, family, revision, &set->type);
900 /* Without holding any locks, create private part. */
901 if (attr[IPSET_ATTR_DATA] &&
902 nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
903 set->type->create_policy, NULL)) {
904 ret = -IPSET_ERR_PROTOCOL;
908 ret = set->type->create(net, set, tb, flags);
912 /* BTW, ret==0 here. */
914 /* Here, we have a valid, constructed set and we are protected
915 * by the nfnl mutex. Find the first free index in ip_set_list
916 * and check clashing.
918 ret = find_free_id(inst, set->name, &index, &clash);
919 if (ret == -EEXIST) {
920 /* If this is the same set and requested, ignore error */
921 if ((flags & IPSET_FLAG_EXIST) &&
922 STRNCMP(set->type->name, clash->type->name) &&
923 set->type->family == clash->type->family &&
924 set->type->revision_min == clash->type->revision_min &&
925 set->type->revision_max == clash->type->revision_max &&
926 set->variant->same_set(set, clash))
929 } else if (ret == -IPSET_ERR_MAX_SETS) {
930 struct ip_set **list, **tmp;
931 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
933 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
937 list = kcalloc(i, sizeof(struct ip_set *), GFP_KERNEL);
940 /* nfnl mutex is held, both lists are valid */
941 tmp = ip_set_dereference(inst->ip_set_list);
942 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
943 rcu_assign_pointer(inst->ip_set_list, list);
944 /* Make sure all current packets have passed through */
947 index = inst->ip_set_max;
948 inst->ip_set_max = i;
955 /* Finally! Add our shiny new set to the list, and be done. */
956 pr_debug("create: '%s' created with index %u!\n", set->name, index);
957 ip_set(inst, index) = set;
962 set->variant->destroy(set);
964 module_put(set->type->me);
972 static const struct nla_policy
973 ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
974 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
975 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
976 .len = IPSET_MAXNAMELEN - 1 },
980 ip_set_destroy_set(struct ip_set *set)
982 pr_debug("set: %s\n", set->name);
984 /* Must call it without holding any lock */
985 set->variant->destroy(set);
986 module_put(set->type->me);
990 static int ip_set_destroy(struct net *net, struct sock *ctnl,
991 struct sk_buff *skb, const struct nlmsghdr *nlh,
992 const struct nlattr * const attr[])
994 struct ip_set_net *inst = ip_set_pernet(net);
999 if (unlikely(protocol_failed(attr)))
1000 return -IPSET_ERR_PROTOCOL;
1002 /* Must wait for flush to be really finished in list:set */
1005 /* Commands are serialized and references are
1006 * protected by the ip_set_ref_lock.
1007 * External systems (i.e. xt_set) must call
1008 * ip_set_put|get_nfnl_* functions, that way we
1009 * can safely check references here.
1011 * list:set timer can only decrement the reference
1012 * counter, so if it's already zero, we can proceed
1013 * without holding the lock.
1015 read_lock_bh(&ip_set_ref_lock);
1016 if (!attr[IPSET_ATTR_SETNAME]) {
1017 for (i = 0; i < inst->ip_set_max; i++) {
1018 s = ip_set(inst, i);
1019 if (s && (s->ref || s->ref_netlink)) {
1020 ret = -IPSET_ERR_BUSY;
1024 inst->is_destroyed = true;
1025 read_unlock_bh(&ip_set_ref_lock);
1026 for (i = 0; i < inst->ip_set_max; i++) {
1027 s = ip_set(inst, i);
1029 ip_set(inst, i) = NULL;
1030 ip_set_destroy_set(s);
1033 /* Modified by ip_set_destroy() only, which is serialized */
1034 inst->is_destroyed = false;
1036 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1041 } else if (s->ref || s->ref_netlink) {
1042 ret = -IPSET_ERR_BUSY;
1045 ip_set(inst, i) = NULL;
1046 read_unlock_bh(&ip_set_ref_lock);
1048 ip_set_destroy_set(s);
1052 read_unlock_bh(&ip_set_ref_lock);
1059 ip_set_flush_set(struct ip_set *set)
1061 pr_debug("set: %s\n", set->name);
1063 spin_lock_bh(&set->lock);
1064 set->variant->flush(set);
1065 spin_unlock_bh(&set->lock);
1068 static int ip_set_flush(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1069 const struct nlmsghdr *nlh,
1070 const struct nlattr * const attr[])
1072 struct ip_set_net *inst = ip_set_pernet(net);
1076 if (unlikely(protocol_failed(attr)))
1077 return -IPSET_ERR_PROTOCOL;
1079 if (!attr[IPSET_ATTR_SETNAME]) {
1080 for (i = 0; i < inst->ip_set_max; i++) {
1081 s = ip_set(inst, i);
1083 ip_set_flush_set(s);
1086 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1090 ip_set_flush_set(s);
1098 static const struct nla_policy
1099 ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
1100 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1101 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1102 .len = IPSET_MAXNAMELEN - 1 },
1103 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
1104 .len = IPSET_MAXNAMELEN - 1 },
1107 static int ip_set_rename(struct net *net, struct sock *ctnl,
1108 struct sk_buff *skb, const struct nlmsghdr *nlh,
1109 const struct nlattr * const attr[])
1111 struct ip_set_net *inst = ip_set_pernet(net);
1112 struct ip_set *set, *s;
1117 if (unlikely(protocol_failed(attr) ||
1118 !attr[IPSET_ATTR_SETNAME] ||
1119 !attr[IPSET_ATTR_SETNAME2]))
1120 return -IPSET_ERR_PROTOCOL;
1122 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1126 read_lock_bh(&ip_set_ref_lock);
1127 if (set->ref != 0) {
1128 ret = -IPSET_ERR_REFERENCED;
1132 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1133 for (i = 0; i < inst->ip_set_max; i++) {
1134 s = ip_set(inst, i);
1135 if (s && STRNCMP(s->name, name2)) {
1136 ret = -IPSET_ERR_EXIST_SETNAME2;
1140 strncpy(set->name, name2, IPSET_MAXNAMELEN);
1143 read_unlock_bh(&ip_set_ref_lock);
1147 /* Swap two sets so that name/index points to the other.
1148 * References and set names are also swapped.
1150 * The commands are serialized by the nfnl mutex and references are
1151 * protected by the ip_set_ref_lock. The kernel interfaces
1152 * do not hold the mutex but the pointer settings are atomic
1153 * so the ip_set_list always contains valid pointers to the sets.
1156 static int ip_set_swap(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1157 const struct nlmsghdr *nlh,
1158 const struct nlattr * const attr[])
1160 struct ip_set_net *inst = ip_set_pernet(net);
1161 struct ip_set *from, *to;
1162 ip_set_id_t from_id, to_id;
1163 char from_name[IPSET_MAXNAMELEN];
1165 if (unlikely(protocol_failed(attr) ||
1166 !attr[IPSET_ATTR_SETNAME] ||
1167 !attr[IPSET_ATTR_SETNAME2]))
1168 return -IPSET_ERR_PROTOCOL;
1170 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1175 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1178 return -IPSET_ERR_EXIST_SETNAME2;
1180 /* Features must not change.
1181 * Not an artifical restriction anymore, as we must prevent
1182 * possible loops created by swapping in setlist type of sets.
1184 if (!(from->type->features == to->type->features &&
1185 from->family == to->family))
1186 return -IPSET_ERR_TYPE_MISMATCH;
1188 if (from->ref_netlink || to->ref_netlink)
1191 strncpy(from_name, from->name, IPSET_MAXNAMELEN);
1192 strncpy(from->name, to->name, IPSET_MAXNAMELEN);
1193 strncpy(to->name, from_name, IPSET_MAXNAMELEN);
1195 write_lock_bh(&ip_set_ref_lock);
1196 swap(from->ref, to->ref);
1197 ip_set(inst, from_id) = to;
1198 ip_set(inst, to_id) = from;
1199 write_unlock_bh(&ip_set_ref_lock);
1204 /* List/save set data */
1211 #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
1212 #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
1215 ip_set_dump_done(struct netlink_callback *cb)
1217 if (cb->args[IPSET_CB_ARG0]) {
1218 struct ip_set_net *inst =
1219 (struct ip_set_net *)cb->args[IPSET_CB_NET];
1220 ip_set_id_t index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1221 struct ip_set *set = ip_set(inst, index);
1223 if (set->variant->uref)
1224 set->variant->uref(set, cb, false);
1225 pr_debug("release set %s\n", set->name);
1226 __ip_set_put_netlink(set);
1232 dump_attrs(struct nlmsghdr *nlh)
1234 const struct nlattr *attr;
1237 pr_debug("dump nlmsg\n");
1238 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
1239 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
1244 dump_init(struct netlink_callback *cb, struct ip_set_net *inst)
1246 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
1247 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1248 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1249 struct nlattr *attr = (void *)nlh + min_len;
1253 /* Second pass, so parser can't fail */
1254 nla_parse(cda, IPSET_ATTR_CMD_MAX, attr, nlh->nlmsg_len - min_len,
1255 ip_set_setname_policy, NULL);
1257 if (cda[IPSET_ATTR_SETNAME]) {
1260 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
1265 dump_type = DUMP_ONE;
1266 cb->args[IPSET_CB_INDEX] = index;
1268 dump_type = DUMP_ALL;
1271 if (cda[IPSET_ATTR_FLAGS]) {
1272 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1274 dump_type |= (f << 16);
1276 cb->args[IPSET_CB_NET] = (unsigned long)inst;
1277 cb->args[IPSET_CB_DUMP] = dump_type;
1283 ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
1285 ip_set_id_t index = IPSET_INVALID_ID, max;
1286 struct ip_set *set = NULL;
1287 struct nlmsghdr *nlh = NULL;
1288 unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0;
1289 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1290 u32 dump_type, dump_flags;
1294 if (!cb->args[IPSET_CB_DUMP]) {
1295 ret = dump_init(cb, inst);
1297 nlh = nlmsg_hdr(cb->skb);
1298 /* We have to create and send the error message
1301 if (nlh->nlmsg_flags & NLM_F_ACK)
1302 netlink_ack(cb->skb, nlh, ret, NULL);
1307 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
1310 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1311 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1312 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1315 pr_debug("dump type, flag: %u %u index: %ld\n",
1316 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1317 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
1318 index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1319 write_lock_bh(&ip_set_ref_lock);
1320 set = ip_set(inst, index);
1321 is_destroyed = inst->is_destroyed;
1322 if (!set || is_destroyed) {
1323 write_unlock_bh(&ip_set_ref_lock);
1324 if (dump_type == DUMP_ONE) {
1329 /* All sets are just being destroyed */
1335 /* When dumping all sets, we must dump "sorted"
1336 * so that lists (unions of sets) are dumped last.
1338 if (dump_type != DUMP_ONE &&
1339 ((dump_type == DUMP_ALL) ==
1340 !!(set->type->features & IPSET_DUMP_LAST))) {
1341 write_unlock_bh(&ip_set_ref_lock);
1344 pr_debug("List set: %s\n", set->name);
1345 if (!cb->args[IPSET_CB_ARG0]) {
1346 /* Start listing: make sure set won't be destroyed */
1347 pr_debug("reference set\n");
1350 write_unlock_bh(&ip_set_ref_lock);
1351 nlh = start_msg(skb, NETLINK_CB(cb->skb).portid,
1352 cb->nlh->nlmsg_seq, flags,
1356 goto release_refcount;
1358 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1359 nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
1360 goto nla_put_failure;
1361 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1363 switch (cb->args[IPSET_CB_ARG0]) {
1365 /* Core header data */
1366 if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
1368 nla_put_u8(skb, IPSET_ATTR_FAMILY,
1370 nla_put_u8(skb, IPSET_ATTR_REVISION,
1372 goto nla_put_failure;
1373 ret = set->variant->head(set, skb);
1375 goto release_refcount;
1376 if (dump_flags & IPSET_FLAG_LIST_HEADER)
1378 if (set->variant->uref)
1379 set->variant->uref(set, cb, true);
1380 /* Fall through and add elements */
1383 ret = set->variant->list(set, skb, cb);
1384 rcu_read_unlock_bh();
1385 if (!cb->args[IPSET_CB_ARG0])
1386 /* Set is done, proceed with next one */
1388 goto release_refcount;
1391 /* If we dump all sets, continue with dumping last ones */
1392 if (dump_type == DUMP_ALL) {
1393 dump_type = DUMP_LAST;
1394 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1395 cb->args[IPSET_CB_INDEX] = 0;
1396 if (set && set->variant->uref)
1397 set->variant->uref(set, cb, false);
1405 if (dump_type == DUMP_ONE)
1406 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
1408 cb->args[IPSET_CB_INDEX]++;
1410 /* If there was an error or set is done, release set */
1411 if (ret || !cb->args[IPSET_CB_ARG0]) {
1412 set = ip_set(inst, index);
1413 if (set->variant->uref)
1414 set->variant->uref(set, cb, false);
1415 pr_debug("release set %s\n", set->name);
1416 __ip_set_put_netlink(set);
1417 cb->args[IPSET_CB_ARG0] = 0;
1421 nlmsg_end(skb, nlh);
1422 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1426 return ret < 0 ? ret : skb->len;
1429 static int ip_set_dump(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1430 const struct nlmsghdr *nlh,
1431 const struct nlattr * const attr[])
1433 if (unlikely(protocol_failed(attr)))
1434 return -IPSET_ERR_PROTOCOL;
1437 struct netlink_dump_control c = {
1438 .dump = ip_set_dump_start,
1439 .done = ip_set_dump_done,
1441 return netlink_dump_start(ctnl, skb, nlh, &c);
1445 /* Add, del and test */
1447 static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1448 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1449 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1450 .len = IPSET_MAXNAMELEN - 1 },
1451 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
1452 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1453 [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
1457 call_ad(struct sock *ctnl, struct sk_buff *skb, struct ip_set *set,
1458 struct nlattr *tb[], enum ipset_adt adt,
1459 u32 flags, bool use_lineno)
1463 bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
1466 spin_lock_bh(&set->lock);
1467 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1468 spin_unlock_bh(&set->lock);
1470 } while (ret == -EAGAIN &&
1471 set->variant->resize &&
1472 (ret = set->variant->resize(set, retried)) == 0);
1474 if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1476 if (lineno && use_lineno) {
1477 /* Error in restore/batch mode: send back lineno */
1478 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1479 struct sk_buff *skb2;
1480 struct nlmsgerr *errmsg;
1481 size_t payload = min(SIZE_MAX,
1482 sizeof(*errmsg) + nlmsg_len(nlh));
1483 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1484 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1485 struct nlattr *cmdattr;
1488 skb2 = nlmsg_new(payload, GFP_KERNEL);
1491 rep = __nlmsg_put(skb2, NETLINK_CB(skb).portid,
1492 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1493 errmsg = nlmsg_data(rep);
1494 errmsg->error = ret;
1495 memcpy(&errmsg->msg, nlh, nlh->nlmsg_len);
1496 cmdattr = (void *)&errmsg->msg + min_len;
1498 nla_parse(cda, IPSET_ATTR_CMD_MAX, cmdattr,
1499 nlh->nlmsg_len - min_len, ip_set_adt_policy, NULL);
1501 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1505 netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid,
1507 /* Signal netlink not to send its ACK/errmsg. */
1514 static int ip_set_uadd(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1515 const struct nlmsghdr *nlh,
1516 const struct nlattr * const attr[])
1518 struct ip_set_net *inst = ip_set_pernet(net);
1520 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1521 const struct nlattr *nla;
1522 u32 flags = flag_exist(nlh);
1526 if (unlikely(protocol_failed(attr) ||
1527 !attr[IPSET_ATTR_SETNAME] ||
1528 !((attr[IPSET_ATTR_DATA] != NULL) ^
1529 (attr[IPSET_ATTR_ADT] != NULL)) ||
1530 (attr[IPSET_ATTR_DATA] &&
1531 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1532 (attr[IPSET_ATTR_ADT] &&
1533 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1534 !attr[IPSET_ATTR_LINENO]))))
1535 return -IPSET_ERR_PROTOCOL;
1537 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1541 use_lineno = !!attr[IPSET_ATTR_LINENO];
1542 if (attr[IPSET_ATTR_DATA]) {
1543 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1544 attr[IPSET_ATTR_DATA],
1545 set->type->adt_policy, NULL))
1546 return -IPSET_ERR_PROTOCOL;
1547 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD, flags,
1552 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1553 memset(tb, 0, sizeof(tb));
1554 if (nla_type(nla) != IPSET_ATTR_DATA ||
1555 !flag_nested(nla) ||
1556 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1557 set->type->adt_policy, NULL))
1558 return -IPSET_ERR_PROTOCOL;
1559 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD,
1568 static int ip_set_udel(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1569 const struct nlmsghdr *nlh,
1570 const struct nlattr * const attr[])
1572 struct ip_set_net *inst = ip_set_pernet(net);
1574 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1575 const struct nlattr *nla;
1576 u32 flags = flag_exist(nlh);
1580 if (unlikely(protocol_failed(attr) ||
1581 !attr[IPSET_ATTR_SETNAME] ||
1582 !((attr[IPSET_ATTR_DATA] != NULL) ^
1583 (attr[IPSET_ATTR_ADT] != NULL)) ||
1584 (attr[IPSET_ATTR_DATA] &&
1585 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1586 (attr[IPSET_ATTR_ADT] &&
1587 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1588 !attr[IPSET_ATTR_LINENO]))))
1589 return -IPSET_ERR_PROTOCOL;
1591 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1595 use_lineno = !!attr[IPSET_ATTR_LINENO];
1596 if (attr[IPSET_ATTR_DATA]) {
1597 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1598 attr[IPSET_ATTR_DATA],
1599 set->type->adt_policy, NULL))
1600 return -IPSET_ERR_PROTOCOL;
1601 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL, flags,
1606 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1607 memset(tb, 0, sizeof(*tb));
1608 if (nla_type(nla) != IPSET_ATTR_DATA ||
1609 !flag_nested(nla) ||
1610 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1611 set->type->adt_policy, NULL))
1612 return -IPSET_ERR_PROTOCOL;
1613 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL,
1622 static int ip_set_utest(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1623 const struct nlmsghdr *nlh,
1624 const struct nlattr * const attr[])
1626 struct ip_set_net *inst = ip_set_pernet(net);
1628 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1631 if (unlikely(protocol_failed(attr) ||
1632 !attr[IPSET_ATTR_SETNAME] ||
1633 !attr[IPSET_ATTR_DATA] ||
1634 !flag_nested(attr[IPSET_ATTR_DATA])))
1635 return -IPSET_ERR_PROTOCOL;
1637 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1641 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1642 set->type->adt_policy, NULL))
1643 return -IPSET_ERR_PROTOCOL;
1646 ret = set->variant->uadt(set, tb, IPSET_TEST, NULL, 0, 0);
1647 rcu_read_unlock_bh();
1648 /* Userspace can't trigger element to be re-added */
1652 return ret > 0 ? 0 : -IPSET_ERR_EXIST;
1655 /* Get headed data of a set */
1657 static int ip_set_header(struct net *net, struct sock *ctnl,
1658 struct sk_buff *skb, const struct nlmsghdr *nlh,
1659 const struct nlattr * const attr[])
1661 struct ip_set_net *inst = ip_set_pernet(net);
1662 const struct ip_set *set;
1663 struct sk_buff *skb2;
1664 struct nlmsghdr *nlh2;
1667 if (unlikely(protocol_failed(attr) ||
1668 !attr[IPSET_ATTR_SETNAME]))
1669 return -IPSET_ERR_PROTOCOL;
1671 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1675 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1679 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
1683 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1684 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
1685 nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
1686 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
1687 nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
1688 goto nla_put_failure;
1689 nlmsg_end(skb2, nlh2);
1691 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1698 nlmsg_cancel(skb2, nlh2);
1706 static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1707 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1708 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
1709 .len = IPSET_MAXNAMELEN - 1 },
1710 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
1713 static int ip_set_type(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1714 const struct nlmsghdr *nlh,
1715 const struct nlattr * const attr[])
1717 struct sk_buff *skb2;
1718 struct nlmsghdr *nlh2;
1719 u8 family, min, max;
1720 const char *typename;
1723 if (unlikely(protocol_failed(attr) ||
1724 !attr[IPSET_ATTR_TYPENAME] ||
1725 !attr[IPSET_ATTR_FAMILY]))
1726 return -IPSET_ERR_PROTOCOL;
1728 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1729 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1730 ret = find_set_type_minmax(typename, family, &min, &max);
1734 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1738 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
1742 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1743 nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
1744 nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
1745 nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
1746 nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
1747 goto nla_put_failure;
1748 nlmsg_end(skb2, nlh2);
1750 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
1751 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1758 nlmsg_cancel(skb2, nlh2);
1764 /* Get protocol version */
1766 static const struct nla_policy
1767 ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1768 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1771 static int ip_set_protocol(struct net *net, struct sock *ctnl,
1772 struct sk_buff *skb, const struct nlmsghdr *nlh,
1773 const struct nlattr * const attr[])
1775 struct sk_buff *skb2;
1776 struct nlmsghdr *nlh2;
1779 if (unlikely(!attr[IPSET_ATTR_PROTOCOL]))
1780 return -IPSET_ERR_PROTOCOL;
1782 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1786 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
1787 IPSET_CMD_PROTOCOL);
1790 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
1791 goto nla_put_failure;
1792 nlmsg_end(skb2, nlh2);
1794 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1801 nlmsg_cancel(skb2, nlh2);
1807 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
1808 [IPSET_CMD_NONE] = {
1809 .call = ip_set_none,
1810 .attr_count = IPSET_ATTR_CMD_MAX,
1812 [IPSET_CMD_CREATE] = {
1813 .call = ip_set_create,
1814 .attr_count = IPSET_ATTR_CMD_MAX,
1815 .policy = ip_set_create_policy,
1817 [IPSET_CMD_DESTROY] = {
1818 .call = ip_set_destroy,
1819 .attr_count = IPSET_ATTR_CMD_MAX,
1820 .policy = ip_set_setname_policy,
1822 [IPSET_CMD_FLUSH] = {
1823 .call = ip_set_flush,
1824 .attr_count = IPSET_ATTR_CMD_MAX,
1825 .policy = ip_set_setname_policy,
1827 [IPSET_CMD_RENAME] = {
1828 .call = ip_set_rename,
1829 .attr_count = IPSET_ATTR_CMD_MAX,
1830 .policy = ip_set_setname2_policy,
1832 [IPSET_CMD_SWAP] = {
1833 .call = ip_set_swap,
1834 .attr_count = IPSET_ATTR_CMD_MAX,
1835 .policy = ip_set_setname2_policy,
1837 [IPSET_CMD_LIST] = {
1838 .call = ip_set_dump,
1839 .attr_count = IPSET_ATTR_CMD_MAX,
1840 .policy = ip_set_setname_policy,
1842 [IPSET_CMD_SAVE] = {
1843 .call = ip_set_dump,
1844 .attr_count = IPSET_ATTR_CMD_MAX,
1845 .policy = ip_set_setname_policy,
1848 .call = ip_set_uadd,
1849 .attr_count = IPSET_ATTR_CMD_MAX,
1850 .policy = ip_set_adt_policy,
1853 .call = ip_set_udel,
1854 .attr_count = IPSET_ATTR_CMD_MAX,
1855 .policy = ip_set_adt_policy,
1857 [IPSET_CMD_TEST] = {
1858 .call = ip_set_utest,
1859 .attr_count = IPSET_ATTR_CMD_MAX,
1860 .policy = ip_set_adt_policy,
1862 [IPSET_CMD_HEADER] = {
1863 .call = ip_set_header,
1864 .attr_count = IPSET_ATTR_CMD_MAX,
1865 .policy = ip_set_setname_policy,
1867 [IPSET_CMD_TYPE] = {
1868 .call = ip_set_type,
1869 .attr_count = IPSET_ATTR_CMD_MAX,
1870 .policy = ip_set_type_policy,
1872 [IPSET_CMD_PROTOCOL] = {
1873 .call = ip_set_protocol,
1874 .attr_count = IPSET_ATTR_CMD_MAX,
1875 .policy = ip_set_protocol_policy,
1879 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
1881 .subsys_id = NFNL_SUBSYS_IPSET,
1882 .cb_count = IPSET_MSG_MAX,
1883 .cb = ip_set_netlink_subsys_cb,
1886 /* Interface to iptables/ip6tables */
1889 ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
1893 int copylen = *len, ret = 0;
1894 struct net *net = sock_net(sk);
1895 struct ip_set_net *inst = ip_set_pernet(net);
1897 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1899 if (optval != SO_IP_SET)
1901 if (*len < sizeof(unsigned int))
1904 data = vmalloc(*len);
1907 if (copy_from_user(data, user, *len) != 0) {
1913 if (*op < IP_SET_OP_VERSION) {
1914 /* Check the version at the beginning of operations */
1915 struct ip_set_req_version *req_version = data;
1917 if (*len < sizeof(struct ip_set_req_version)) {
1922 if (req_version->version != IPSET_PROTOCOL) {
1929 case IP_SET_OP_VERSION: {
1930 struct ip_set_req_version *req_version = data;
1932 if (*len != sizeof(struct ip_set_req_version)) {
1937 req_version->version = IPSET_PROTOCOL;
1938 ret = copy_to_user(user, req_version,
1939 sizeof(struct ip_set_req_version));
1942 case IP_SET_OP_GET_BYNAME: {
1943 struct ip_set_req_get_set *req_get = data;
1946 if (*len != sizeof(struct ip_set_req_get_set)) {
1950 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1951 nfnl_lock(NFNL_SUBSYS_IPSET);
1952 find_set_and_id(inst, req_get->set.name, &id);
1953 req_get->set.index = id;
1954 nfnl_unlock(NFNL_SUBSYS_IPSET);
1957 case IP_SET_OP_GET_FNAME: {
1958 struct ip_set_req_get_set_family *req_get = data;
1961 if (*len != sizeof(struct ip_set_req_get_set_family)) {
1965 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1966 nfnl_lock(NFNL_SUBSYS_IPSET);
1967 find_set_and_id(inst, req_get->set.name, &id);
1968 req_get->set.index = id;
1969 if (id != IPSET_INVALID_ID)
1970 req_get->family = ip_set(inst, id)->family;
1971 nfnl_unlock(NFNL_SUBSYS_IPSET);
1974 case IP_SET_OP_GET_BYINDEX: {
1975 struct ip_set_req_get_set *req_get = data;
1978 if (*len != sizeof(struct ip_set_req_get_set) ||
1979 req_get->set.index >= inst->ip_set_max) {
1983 nfnl_lock(NFNL_SUBSYS_IPSET);
1984 set = ip_set(inst, req_get->set.index);
1985 strncpy(req_get->set.name, set ? set->name : "",
1987 nfnl_unlock(NFNL_SUBSYS_IPSET);
1993 } /* end of switch(op) */
1996 ret = copy_to_user(user, data, copylen);
2005 static struct nf_sockopt_ops so_set __read_mostly = {
2007 .get_optmin = SO_IP_SET,
2008 .get_optmax = SO_IP_SET + 1,
2009 .get = ip_set_sockfn_get,
2010 .owner = THIS_MODULE,
2013 static int __net_init
2014 ip_set_net_init(struct net *net)
2016 struct ip_set_net *inst = ip_set_pernet(net);
2017 struct ip_set **list;
2019 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
2020 if (inst->ip_set_max >= IPSET_INVALID_ID)
2021 inst->ip_set_max = IPSET_INVALID_ID - 1;
2023 list = kcalloc(inst->ip_set_max, sizeof(struct ip_set *), GFP_KERNEL);
2026 inst->is_deleted = false;
2027 inst->is_destroyed = false;
2028 rcu_assign_pointer(inst->ip_set_list, list);
2032 static void __net_exit
2033 ip_set_net_exit(struct net *net)
2035 struct ip_set_net *inst = ip_set_pernet(net);
2037 struct ip_set *set = NULL;
2040 inst->is_deleted = true; /* flag for ip_set_nfnl_put */
2042 for (i = 0; i < inst->ip_set_max; i++) {
2043 set = ip_set(inst, i);
2045 ip_set(inst, i) = NULL;
2046 ip_set_destroy_set(set);
2049 kfree(rcu_dereference_protected(inst->ip_set_list, 1));
2052 static struct pernet_operations ip_set_net_ops = {
2053 .init = ip_set_net_init,
2054 .exit = ip_set_net_exit,
2055 .id = &ip_set_net_id,
2056 .size = sizeof(struct ip_set_net)
2062 int ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
2065 pr_err("ip_set: cannot register with nfnetlink.\n");
2068 ret = nf_register_sockopt(&so_set);
2070 pr_err("SO_SET registry failed: %d\n", ret);
2071 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2074 ret = register_pernet_subsys(&ip_set_net_ops);
2076 pr_err("ip_set: cannot register pernet_subsys.\n");
2077 nf_unregister_sockopt(&so_set);
2078 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2081 pr_info("ip_set: protocol %u\n", IPSET_PROTOCOL);
2088 unregister_pernet_subsys(&ip_set_net_ops);
2089 nf_unregister_sockopt(&so_set);
2090 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2091 pr_debug("these are the famous last words\n");
2094 module_init(ip_set_init);
2095 module_exit(ip_set_fini);