1 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Copyright (C) 2003-2011 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/netlink.h>
19 #include <linux/rculist.h>
20 #include <linux/version.h>
21 #include <net/netlink.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 */
32 static struct ip_set **ip_set_list; /* all individual sets */
33 static ip_set_id_t ip_set_max = CONFIG_IP_SET_MAX; /* max number of sets */
35 #define STREQ(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
37 static unsigned int max_sets;
39 module_param(max_sets, int, 0600);
40 MODULE_PARM_DESC(max_sets, "maximal number of sets");
41 MODULE_LICENSE("GPL");
42 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
43 MODULE_DESCRIPTION("core IP set support");
44 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
47 * The set types are implemented in modules and registered set types
48 * can be found in ip_set_type_list. Adding/deleting types is
49 * serialized by ip_set_type_mutex.
53 ip_set_type_lock(void)
55 mutex_lock(&ip_set_type_mutex);
59 ip_set_type_unlock(void)
61 mutex_unlock(&ip_set_type_mutex);
64 /* Register and deregister settype */
66 static struct ip_set_type *
67 find_set_type(const char *name, u8 family, u8 revision)
69 struct ip_set_type *type;
71 list_for_each_entry_rcu(type, &ip_set_type_list, list)
72 if (STREQ(type->name, name) &&
73 (type->family == family || type->family == AF_UNSPEC) &&
74 revision >= type->revision_min &&
75 revision <= type->revision_max)
80 /* Unlock, try to load a set type module and lock again */
82 try_to_load_type(const char *name)
85 pr_debug("try to load ip_set_%s\n", name);
86 if (request_module("ip_set_%s", name) < 0) {
87 pr_warning("Can't find ip_set type %s\n", name);
89 return -IPSET_ERR_FIND_TYPE;
95 /* Find a set type and reference it */
97 find_set_type_get(const char *name, u8 family, u8 revision,
98 struct ip_set_type **found)
100 struct ip_set_type *type;
104 *found = find_set_type(name, family, revision);
106 err = !try_module_get((*found)->me) ? -EFAULT : 0;
109 /* Make sure the type is loaded but we don't support the revision */
110 list_for_each_entry_rcu(type, &ip_set_type_list, list)
111 if (STREQ(type->name, name)) {
112 err = -IPSET_ERR_FIND_TYPE;
117 return try_to_load_type(name);
124 /* Find a given set type by name and family.
125 * If we succeeded, the supported minimal and maximum revisions are
129 find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max)
131 struct ip_set_type *type;
134 *min = 255; *max = 0;
136 list_for_each_entry_rcu(type, &ip_set_type_list, list)
137 if (STREQ(type->name, name) &&
138 (type->family == family || type->family == AF_UNSPEC)) {
140 if (type->revision_min < *min)
141 *min = type->revision_min;
142 if (type->revision_max > *max)
143 *max = type->revision_max;
149 return try_to_load_type(name);
152 #define family_name(f) ((f) == AF_INET ? "inet" : \
153 (f) == AF_INET6 ? "inet6" : "any")
155 /* Register a set type structure. The type is identified by
156 * the unique triple of name, family and revision.
159 ip_set_type_register(struct ip_set_type *type)
163 if (type->protocol != IPSET_PROTOCOL) {
164 pr_warning("ip_set type %s, family %s, revision %u:%u uses "
165 "wrong protocol version %u (want %u)\n",
166 type->name, family_name(type->family),
167 type->revision_min, type->revision_max,
168 type->protocol, IPSET_PROTOCOL);
173 if (find_set_type(type->name, type->family, type->revision_min)) {
175 pr_warning("ip_set type %s, family %s with revision min %u "
176 "already registered!\n", type->name,
177 family_name(type->family), type->revision_min);
181 list_add_rcu(&type->list, &ip_set_type_list);
182 pr_debug("type %s, family %s, revision %u:%u registered.\n",
183 type->name, family_name(type->family),
184 type->revision_min, type->revision_max);
186 ip_set_type_unlock();
189 EXPORT_SYMBOL_GPL(ip_set_type_register);
191 /* Unregister a set type. There's a small race with ip_set_create */
193 ip_set_type_unregister(struct ip_set_type *type)
196 if (!find_set_type(type->name, type->family, type->revision_min)) {
197 pr_warning("ip_set type %s, family %s with revision min %u "
198 "not registered\n", type->name,
199 family_name(type->family), type->revision_min);
202 list_del_rcu(&type->list);
203 pr_debug("type %s, family %s with revision min %u unregistered.\n",
204 type->name, family_name(type->family), type->revision_min);
206 ip_set_type_unlock();
210 EXPORT_SYMBOL_GPL(ip_set_type_unregister);
212 /* Utility functions */
214 ip_set_alloc(size_t size)
216 void *members = NULL;
218 if (size < KMALLOC_MAX_SIZE)
219 members = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
222 pr_debug("%p: allocated with kmalloc\n", members);
226 members = vzalloc(size);
229 pr_debug("%p: allocated with vmalloc\n", members);
233 EXPORT_SYMBOL_GPL(ip_set_alloc);
236 ip_set_free(void *members)
238 pr_debug("%p: free with %s\n", members,
239 is_vmalloc_addr(members) ? "vfree" : "kfree");
240 if (is_vmalloc_addr(members))
245 EXPORT_SYMBOL_GPL(ip_set_free);
248 flag_nested(const struct nlattr *nla)
250 return nla->nla_type & NLA_F_NESTED;
253 static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
254 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
255 [IPSET_ATTR_IPADDR_IPV6] = { .type = NLA_BINARY,
256 .len = sizeof(struct in6_addr) },
260 ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
262 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX+1];
264 if (unlikely(!flag_nested(nla)))
265 return -IPSET_ERR_PROTOCOL;
266 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, ipaddr_policy))
267 return -IPSET_ERR_PROTOCOL;
268 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
269 return -IPSET_ERR_PROTOCOL;
271 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
274 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
277 ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
279 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX+1];
281 if (unlikely(!flag_nested(nla)))
282 return -IPSET_ERR_PROTOCOL;
284 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, ipaddr_policy))
285 return -IPSET_ERR_PROTOCOL;
286 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
287 return -IPSET_ERR_PROTOCOL;
289 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
290 sizeof(struct in6_addr));
293 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
296 * Creating/destroying/renaming/swapping affect the existence and
297 * the properties of a set. All of these can be executed from userspace
298 * only and serialized by the nfnl mutex indirectly from nfnetlink.
300 * Sets are identified by their index in ip_set_list and the index
301 * is used by the external references (set/SET netfilter modules).
303 * The set behind an index may change by swapping only, from userspace.
307 __ip_set_get(ip_set_id_t index)
309 write_lock_bh(&ip_set_ref_lock);
310 ip_set_list[index]->ref++;
311 write_unlock_bh(&ip_set_ref_lock);
315 __ip_set_put(ip_set_id_t index)
317 write_lock_bh(&ip_set_ref_lock);
318 BUG_ON(ip_set_list[index]->ref == 0);
319 ip_set_list[index]->ref--;
320 write_unlock_bh(&ip_set_ref_lock);
324 * Add, del and test set entries from kernel.
326 * The set behind the index must exist and must be referenced
327 * so it can't be destroyed (or changed) under our foot.
331 ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
332 const struct xt_action_param *par,
333 const struct ip_set_adt_opt *opt)
335 struct ip_set *set = ip_set_list[index];
339 pr_debug("set %s, index %u\n", set->name, index);
341 if (opt->dim < set->type->dimension ||
342 !(opt->family == set->family || set->family == AF_UNSPEC))
345 read_lock_bh(&set->lock);
346 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
347 read_unlock_bh(&set->lock);
349 if (ret == -EAGAIN) {
350 /* Type requests element to be completed */
351 pr_debug("element must be competed, ADD is triggered\n");
352 write_lock_bh(&set->lock);
353 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
354 write_unlock_bh(&set->lock);
358 /* Convert error codes to nomatch */
359 return (ret < 0 ? 0 : ret);
361 EXPORT_SYMBOL_GPL(ip_set_test);
364 ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
365 const struct xt_action_param *par,
366 const struct ip_set_adt_opt *opt)
368 struct ip_set *set = ip_set_list[index];
372 pr_debug("set %s, index %u\n", set->name, index);
374 if (opt->dim < set->type->dimension ||
375 !(opt->family == set->family || set->family == AF_UNSPEC))
378 write_lock_bh(&set->lock);
379 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
380 write_unlock_bh(&set->lock);
384 EXPORT_SYMBOL_GPL(ip_set_add);
387 ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
388 const struct xt_action_param *par,
389 const struct ip_set_adt_opt *opt)
391 struct ip_set *set = ip_set_list[index];
395 pr_debug("set %s, index %u\n", set->name, index);
397 if (opt->dim < set->type->dimension ||
398 !(opt->family == set->family || set->family == AF_UNSPEC))
401 write_lock_bh(&set->lock);
402 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
403 write_unlock_bh(&set->lock);
407 EXPORT_SYMBOL_GPL(ip_set_del);
410 * Find set by name, reference it once. The reference makes sure the
411 * thing pointed to, does not go away under our feet.
415 ip_set_get_byname(const char *name, struct ip_set **set)
417 ip_set_id_t i, index = IPSET_INVALID_ID;
420 for (i = 0; i < ip_set_max; i++) {
422 if (s != NULL && STREQ(s->name, name)) {
431 EXPORT_SYMBOL_GPL(ip_set_get_byname);
434 * If the given set pointer points to a valid set, decrement
435 * reference count by 1. The caller shall not assume the index
436 * to be valid, after calling this function.
440 ip_set_put_byindex(ip_set_id_t index)
442 if (ip_set_list[index] != NULL)
445 EXPORT_SYMBOL_GPL(ip_set_put_byindex);
448 * Get the name of a set behind a set index.
449 * We assume the set is referenced, so it does exist and
450 * can't be destroyed. The set cannot be renamed due to
451 * the referencing either.
455 ip_set_name_byindex(ip_set_id_t index)
457 const struct ip_set *set = ip_set_list[index];
460 BUG_ON(set->ref == 0);
462 /* Referenced, so it's safe */
465 EXPORT_SYMBOL_GPL(ip_set_name_byindex);
468 * Routines to call by external subsystems, which do not
469 * call nfnl_lock for us.
473 * Find set by name, reference it once. The reference makes sure the
474 * thing pointed to, does not go away under our feet.
476 * The nfnl mutex is used in the function.
479 ip_set_nfnl_get(const char *name)
485 index = ip_set_get_byname(name, &s);
490 EXPORT_SYMBOL_GPL(ip_set_nfnl_get);
493 * Find set by index, reference it once. The reference makes sure the
494 * thing pointed to, does not go away under our feet.
496 * The nfnl mutex is used in the function.
499 ip_set_nfnl_get_byindex(ip_set_id_t index)
501 if (index > ip_set_max)
502 return IPSET_INVALID_ID;
505 if (ip_set_list[index])
508 index = IPSET_INVALID_ID;
513 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
516 * If the given set pointer points to a valid set, decrement
517 * reference count by 1. The caller shall not assume the index
518 * to be valid, after calling this function.
520 * The nfnl mutex is used in the function.
523 ip_set_nfnl_put(ip_set_id_t index)
526 ip_set_put_byindex(index);
529 EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
532 * Communication protocol with userspace over netlink.
534 * The commands are serialized by the nfnl mutex.
538 protocol_failed(const struct nlattr * const tb[])
540 return !tb[IPSET_ATTR_PROTOCOL] ||
541 nla_get_u8(tb[IPSET_ATTR_PROTOCOL]) != IPSET_PROTOCOL;
545 flag_exist(const struct nlmsghdr *nlh)
547 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
550 static struct nlmsghdr *
551 start_msg(struct sk_buff *skb, u32 pid, u32 seq, unsigned int flags,
554 struct nlmsghdr *nlh;
555 struct nfgenmsg *nfmsg;
557 nlh = nlmsg_put(skb, pid, seq, cmd | (NFNL_SUBSYS_IPSET << 8),
558 sizeof(*nfmsg), flags);
562 nfmsg = nlmsg_data(nlh);
563 nfmsg->nfgen_family = AF_INET;
564 nfmsg->version = NFNETLINK_V0;
572 static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
573 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
574 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
575 .len = IPSET_MAXNAMELEN - 1 },
576 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
577 .len = IPSET_MAXNAMELEN - 1},
578 [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
579 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
580 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
584 find_set_id(const char *name)
586 ip_set_id_t i, index = IPSET_INVALID_ID;
587 const struct ip_set *set;
589 for (i = 0; index == IPSET_INVALID_ID && i < ip_set_max; i++) {
590 set = ip_set_list[i];
591 if (set != NULL && STREQ(set->name, name))
597 static inline struct ip_set *
598 find_set(const char *name)
600 ip_set_id_t index = find_set_id(name);
602 return index == IPSET_INVALID_ID ? NULL : ip_set_list[index];
606 find_free_id(const char *name, ip_set_id_t *index, struct ip_set **set)
610 *index = IPSET_INVALID_ID;
611 for (i = 0; i < ip_set_max; i++) {
612 if (ip_set_list[i] == NULL) {
613 if (*index == IPSET_INVALID_ID)
615 } else if (STREQ(name, ip_set_list[i]->name)) {
617 *set = ip_set_list[i];
621 if (*index == IPSET_INVALID_ID)
622 /* No free slot remained */
623 return -IPSET_ERR_MAX_SETS;
628 ip_set_create(struct sock *ctnl, struct sk_buff *skb,
629 const struct nlmsghdr *nlh,
630 const struct nlattr * const attr[])
632 struct ip_set *set, *clash = NULL;
633 ip_set_id_t index = IPSET_INVALID_ID;
634 struct nlattr *tb[IPSET_ATTR_CREATE_MAX+1] = {};
635 const char *name, *typename;
637 u32 flags = flag_exist(nlh);
640 if (unlikely(protocol_failed(attr) ||
641 attr[IPSET_ATTR_SETNAME] == NULL ||
642 attr[IPSET_ATTR_TYPENAME] == NULL ||
643 attr[IPSET_ATTR_REVISION] == NULL ||
644 attr[IPSET_ATTR_FAMILY] == NULL ||
645 (attr[IPSET_ATTR_DATA] != NULL &&
646 !flag_nested(attr[IPSET_ATTR_DATA]))))
647 return -IPSET_ERR_PROTOCOL;
649 name = nla_data(attr[IPSET_ATTR_SETNAME]);
650 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
651 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
652 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
653 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
654 name, typename, family_name(family), revision);
657 * First, and without any locks, allocate and initialize
658 * a normal base set structure.
660 set = kzalloc(sizeof(struct ip_set), GFP_KERNEL);
663 rwlock_init(&set->lock);
664 strlcpy(set->name, name, IPSET_MAXNAMELEN);
665 set->family = family;
666 set->revision = revision;
669 * Next, check that we know the type, and take
670 * a reference on the type, to make sure it stays available
671 * while constructing our new set.
673 * After referencing the type, we try to create the type
674 * specific part of the set without holding any locks.
676 ret = find_set_type_get(typename, family, revision, &(set->type));
681 * Without holding any locks, create private part.
683 if (attr[IPSET_ATTR_DATA] &&
684 nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
685 set->type->create_policy)) {
686 ret = -IPSET_ERR_PROTOCOL;
690 ret = set->type->create(set, tb, flags);
694 /* BTW, ret==0 here. */
697 * Here, we have a valid, constructed set and we are protected
698 * by the nfnl mutex. Find the first free index in ip_set_list
699 * and check clashing.
701 if ((ret = find_free_id(set->name, &index, &clash)) != 0) {
702 /* If this is the same set and requested, ignore error */
703 if (ret == -EEXIST &&
704 (flags & IPSET_FLAG_EXIST) &&
705 STREQ(set->type->name, clash->type->name) &&
706 set->type->family == clash->type->family &&
707 set->type->revision_min == clash->type->revision_min &&
708 set->type->revision_max == clash->type->revision_max &&
709 set->variant->same_set(set, clash))
715 * Finally! Add our shiny new set to the list, and be done.
717 pr_debug("create: '%s' created with index %u!\n", set->name, index);
718 ip_set_list[index] = set;
723 set->variant->destroy(set);
725 module_put(set->type->me);
733 static const struct nla_policy
734 ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
735 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
736 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
737 .len = IPSET_MAXNAMELEN - 1 },
741 ip_set_destroy_set(ip_set_id_t index)
743 struct ip_set *set = ip_set_list[index];
745 pr_debug("set: %s\n", set->name);
746 ip_set_list[index] = NULL;
748 /* Must call it without holding any lock */
749 set->variant->destroy(set);
750 module_put(set->type->me);
755 ip_set_destroy(struct sock *ctnl, struct sk_buff *skb,
756 const struct nlmsghdr *nlh,
757 const struct nlattr * const attr[])
762 if (unlikely(protocol_failed(attr)))
763 return -IPSET_ERR_PROTOCOL;
765 /* Commands are serialized and references are
766 * protected by the ip_set_ref_lock.
767 * External systems (i.e. xt_set) must call
768 * ip_set_put|get_nfnl_* functions, that way we
769 * can safely check references here.
771 * list:set timer can only decrement the reference
772 * counter, so if it's already zero, we can proceed
773 * without holding the lock.
775 read_lock_bh(&ip_set_ref_lock);
776 if (!attr[IPSET_ATTR_SETNAME]) {
777 for (i = 0; i < ip_set_max; i++) {
778 if (ip_set_list[i] != NULL && ip_set_list[i]->ref) {
779 ret = -IPSET_ERR_BUSY;
783 read_unlock_bh(&ip_set_ref_lock);
784 for (i = 0; i < ip_set_max; i++) {
785 if (ip_set_list[i] != NULL)
786 ip_set_destroy_set(i);
789 i = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
790 if (i == IPSET_INVALID_ID) {
793 } else if (ip_set_list[i]->ref) {
794 ret = -IPSET_ERR_BUSY;
797 read_unlock_bh(&ip_set_ref_lock);
799 ip_set_destroy_set(i);
803 read_unlock_bh(&ip_set_ref_lock);
810 ip_set_flush_set(struct ip_set *set)
812 pr_debug("set: %s\n", set->name);
814 write_lock_bh(&set->lock);
815 set->variant->flush(set);
816 write_unlock_bh(&set->lock);
820 ip_set_flush(struct sock *ctnl, struct sk_buff *skb,
821 const struct nlmsghdr *nlh,
822 const struct nlattr * const attr[])
826 if (unlikely(protocol_failed(attr)))
827 return -IPSET_ERR_PROTOCOL;
829 if (!attr[IPSET_ATTR_SETNAME]) {
830 for (i = 0; i < ip_set_max; i++)
831 if (ip_set_list[i] != NULL)
832 ip_set_flush_set(ip_set_list[i]);
834 i = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
835 if (i == IPSET_INVALID_ID)
838 ip_set_flush_set(ip_set_list[i]);
846 static const struct nla_policy
847 ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
848 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
849 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
850 .len = IPSET_MAXNAMELEN - 1 },
851 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
852 .len = IPSET_MAXNAMELEN - 1 },
856 ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
857 const struct nlmsghdr *nlh,
858 const struct nlattr * const attr[])
865 if (unlikely(protocol_failed(attr) ||
866 attr[IPSET_ATTR_SETNAME] == NULL ||
867 attr[IPSET_ATTR_SETNAME2] == NULL))
868 return -IPSET_ERR_PROTOCOL;
870 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
874 read_lock_bh(&ip_set_ref_lock);
876 ret = -IPSET_ERR_REFERENCED;
880 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
881 for (i = 0; i < ip_set_max; i++) {
882 if (ip_set_list[i] != NULL &&
883 STREQ(ip_set_list[i]->name, name2)) {
884 ret = -IPSET_ERR_EXIST_SETNAME2;
888 strncpy(set->name, name2, IPSET_MAXNAMELEN);
891 read_unlock_bh(&ip_set_ref_lock);
895 /* Swap two sets so that name/index points to the other.
896 * References and set names are also swapped.
898 * The commands are serialized by the nfnl mutex and references are
899 * protected by the ip_set_ref_lock. The kernel interfaces
900 * do not hold the mutex but the pointer settings are atomic
901 * so the ip_set_list always contains valid pointers to the sets.
905 ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
906 const struct nlmsghdr *nlh,
907 const struct nlattr * const attr[])
909 struct ip_set *from, *to;
910 ip_set_id_t from_id, to_id;
911 char from_name[IPSET_MAXNAMELEN];
913 if (unlikely(protocol_failed(attr) ||
914 attr[IPSET_ATTR_SETNAME] == NULL ||
915 attr[IPSET_ATTR_SETNAME2] == NULL))
916 return -IPSET_ERR_PROTOCOL;
918 from_id = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
919 if (from_id == IPSET_INVALID_ID)
922 to_id = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME2]));
923 if (to_id == IPSET_INVALID_ID)
924 return -IPSET_ERR_EXIST_SETNAME2;
926 from = ip_set_list[from_id];
927 to = ip_set_list[to_id];
929 /* Features must not change.
930 * Not an artificial restriction anymore, as we must prevent
931 * possible loops created by swapping in setlist type of sets. */
932 if (!(from->type->features == to->type->features &&
933 from->type->family == to->type->family))
934 return -IPSET_ERR_TYPE_MISMATCH;
936 strncpy(from_name, from->name, IPSET_MAXNAMELEN);
937 strncpy(from->name, to->name, IPSET_MAXNAMELEN);
938 strncpy(to->name, from_name, IPSET_MAXNAMELEN);
940 write_lock_bh(&ip_set_ref_lock);
941 swap(from->ref, to->ref);
942 ip_set_list[from_id] = to;
943 ip_set_list[to_id] = from;
944 write_unlock_bh(&ip_set_ref_lock);
949 /* List/save set data */
956 #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
957 #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
960 ip_set_dump_done(struct netlink_callback *cb)
963 pr_debug("release set %s\n", ip_set_list[cb->args[1]]->name);
964 ip_set_put_byindex((ip_set_id_t) cb->args[1]);
970 dump_attrs(struct nlmsghdr *nlh)
972 const struct nlattr *attr;
975 pr_debug("dump nlmsg\n");
976 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
977 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
982 dump_init(struct netlink_callback *cb)
984 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
985 int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
986 struct nlattr *cda[IPSET_ATTR_CMD_MAX+1];
987 struct nlattr *attr = (void *)nlh + min_len;
991 /* Second pass, so parser can't fail */
992 nla_parse(cda, IPSET_ATTR_CMD_MAX,
993 attr, nlh->nlmsg_len - min_len, ip_set_setname_policy);
995 /* cb->args[0] : dump single set/all sets
997 * [..]: type specific
1000 if (cda[IPSET_ATTR_SETNAME]) {
1001 index = find_set_id(nla_data(cda[IPSET_ATTR_SETNAME]));
1002 if (index == IPSET_INVALID_ID)
1005 dump_type = DUMP_ONE;
1006 cb->args[1] = index;
1008 dump_type = DUMP_ALL;
1010 if (cda[IPSET_ATTR_FLAGS]) {
1011 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1012 dump_type |= (f << 16);
1014 cb->args[0] = dump_type;
1020 ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
1022 ip_set_id_t index = IPSET_INVALID_ID, max;
1023 struct ip_set *set = NULL;
1024 struct nlmsghdr *nlh = NULL;
1025 unsigned int flags = NETLINK_CB(cb->skb).pid ? NLM_F_MULTI : 0;
1026 u32 dump_type, dump_flags;
1030 ret = dump_init(cb);
1032 nlh = nlmsg_hdr(cb->skb);
1033 /* We have to create and send the error message
1035 if (nlh->nlmsg_flags & NLM_F_ACK)
1036 netlink_ack(cb->skb, nlh, ret);
1041 if (cb->args[1] >= ip_set_max)
1044 dump_type = DUMP_TYPE(cb->args[0]);
1045 dump_flags = DUMP_FLAGS(cb->args[0]);
1046 max = dump_type == DUMP_ONE ? cb->args[1] + 1 : ip_set_max;
1048 pr_debug("args[0]: %u %u args[1]: %ld\n",
1049 dump_type, dump_flags, cb->args[1]);
1050 for (; cb->args[1] < max; cb->args[1]++) {
1051 index = (ip_set_id_t) cb->args[1];
1052 set = ip_set_list[index];
1054 if (dump_type == DUMP_ONE) {
1060 /* When dumping all sets, we must dump "sorted"
1061 * so that lists (unions of sets) are dumped last.
1063 if (dump_type != DUMP_ONE &&
1064 ((dump_type == DUMP_ALL) ==
1065 !!(set->type->features & IPSET_DUMP_LAST)))
1067 pr_debug("List set: %s\n", set->name);
1069 /* Start listing: make sure set won't be destroyed */
1070 pr_debug("reference set\n");
1071 __ip_set_get(index);
1073 nlh = start_msg(skb, NETLINK_CB(cb->skb).pid,
1074 cb->nlh->nlmsg_seq, flags,
1078 goto release_refcount;
1080 NLA_PUT_U8(skb, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL);
1081 NLA_PUT_STRING(skb, IPSET_ATTR_SETNAME, set->name);
1082 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1084 switch (cb->args[2]) {
1086 /* Core header data */
1087 NLA_PUT_STRING(skb, IPSET_ATTR_TYPENAME,
1089 NLA_PUT_U8(skb, IPSET_ATTR_FAMILY,
1091 NLA_PUT_U8(skb, IPSET_ATTR_REVISION,
1093 ret = set->variant->head(set, skb);
1095 goto release_refcount;
1096 if (dump_flags & IPSET_FLAG_LIST_HEADER)
1098 /* Fall through and add elements */
1100 read_lock_bh(&set->lock);
1101 ret = set->variant->list(set, skb, cb);
1102 read_unlock_bh(&set->lock);
1104 /* Set is done, proceed with next one */
1106 goto release_refcount;
1109 /* If we dump all sets, continue with dumping last ones */
1110 if (dump_type == DUMP_ALL) {
1111 dump_type = DUMP_LAST;
1112 cb->args[0] = dump_type | (dump_flags << 16);
1121 if (dump_type == DUMP_ONE)
1122 cb->args[1] = IPSET_INVALID_ID;
1126 /* If there was an error or set is done, release set */
1127 if (ret || !cb->args[2]) {
1128 pr_debug("release set %s\n", ip_set_list[index]->name);
1129 ip_set_put_byindex(index);
1133 nlmsg_end(skb, nlh);
1134 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1138 return ret < 0 ? ret : skb->len;
1142 ip_set_dump(struct sock *ctnl, struct sk_buff *skb,
1143 const struct nlmsghdr *nlh,
1144 const struct nlattr * const attr[])
1146 if (unlikely(protocol_failed(attr)))
1147 return -IPSET_ERR_PROTOCOL;
1149 return netlink_dump_start(ctnl, skb, nlh,
1151 ip_set_dump_done, 0);
1154 /* Add, del and test */
1156 static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1157 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1158 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1159 .len = IPSET_MAXNAMELEN - 1 },
1160 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
1161 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1162 [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
1166 call_ad(struct sock *ctnl, struct sk_buff *skb, struct ip_set *set,
1167 struct nlattr *tb[], enum ipset_adt adt,
1168 u32 flags, bool use_lineno)
1172 bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
1175 write_lock_bh(&set->lock);
1176 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1177 write_unlock_bh(&set->lock);
1179 } while (ret == -EAGAIN &&
1180 set->variant->resize &&
1181 (ret = set->variant->resize(set, retried)) == 0);
1183 if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1185 if (lineno && use_lineno) {
1186 /* Error in restore/batch mode: send back lineno */
1187 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1188 struct sk_buff *skb2;
1189 struct nlmsgerr *errmsg;
1190 size_t payload = sizeof(*errmsg) + nlmsg_len(nlh);
1191 int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
1192 struct nlattr *cda[IPSET_ATTR_CMD_MAX+1];
1193 struct nlattr *cmdattr;
1196 skb2 = nlmsg_new(payload, GFP_KERNEL);
1199 rep = __nlmsg_put(skb2, NETLINK_CB(skb).pid,
1200 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1201 errmsg = nlmsg_data(rep);
1202 errmsg->error = ret;
1203 memcpy(&errmsg->msg, nlh, nlh->nlmsg_len);
1204 cmdattr = (void *)&errmsg->msg + min_len;
1206 nla_parse(cda, IPSET_ATTR_CMD_MAX,
1207 cmdattr, nlh->nlmsg_len - min_len,
1210 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1214 netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1215 /* Signal netlink not to send its ACK/errmsg. */
1223 ip_set_uadd(struct sock *ctnl, struct sk_buff *skb,
1224 const struct nlmsghdr *nlh,
1225 const struct nlattr * const attr[])
1228 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1229 const struct nlattr *nla;
1230 u32 flags = flag_exist(nlh);
1234 if (unlikely(protocol_failed(attr) ||
1235 attr[IPSET_ATTR_SETNAME] == NULL ||
1236 !((attr[IPSET_ATTR_DATA] != NULL) ^
1237 (attr[IPSET_ATTR_ADT] != NULL)) ||
1238 (attr[IPSET_ATTR_DATA] != NULL &&
1239 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1240 (attr[IPSET_ATTR_ADT] != NULL &&
1241 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1242 attr[IPSET_ATTR_LINENO] == NULL))))
1243 return -IPSET_ERR_PROTOCOL;
1245 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
1249 use_lineno = !!attr[IPSET_ATTR_LINENO];
1250 if (attr[IPSET_ATTR_DATA]) {
1251 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1252 attr[IPSET_ATTR_DATA],
1253 set->type->adt_policy))
1254 return -IPSET_ERR_PROTOCOL;
1255 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD, flags,
1260 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1261 memset(tb, 0, sizeof(tb));
1262 if (nla_type(nla) != IPSET_ATTR_DATA ||
1263 !flag_nested(nla) ||
1264 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1265 set->type->adt_policy))
1266 return -IPSET_ERR_PROTOCOL;
1267 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD,
1277 ip_set_udel(struct sock *ctnl, struct sk_buff *skb,
1278 const struct nlmsghdr *nlh,
1279 const struct nlattr * const attr[])
1282 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1283 const struct nlattr *nla;
1284 u32 flags = flag_exist(nlh);
1288 if (unlikely(protocol_failed(attr) ||
1289 attr[IPSET_ATTR_SETNAME] == NULL ||
1290 !((attr[IPSET_ATTR_DATA] != NULL) ^
1291 (attr[IPSET_ATTR_ADT] != NULL)) ||
1292 (attr[IPSET_ATTR_DATA] != NULL &&
1293 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1294 (attr[IPSET_ATTR_ADT] != NULL &&
1295 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1296 attr[IPSET_ATTR_LINENO] == NULL))))
1297 return -IPSET_ERR_PROTOCOL;
1299 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
1303 use_lineno = !!attr[IPSET_ATTR_LINENO];
1304 if (attr[IPSET_ATTR_DATA]) {
1305 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1306 attr[IPSET_ATTR_DATA],
1307 set->type->adt_policy))
1308 return -IPSET_ERR_PROTOCOL;
1309 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL, flags,
1314 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1315 memset(tb, 0, sizeof(*tb));
1316 if (nla_type(nla) != IPSET_ATTR_DATA ||
1317 !flag_nested(nla) ||
1318 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1319 set->type->adt_policy))
1320 return -IPSET_ERR_PROTOCOL;
1321 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL,
1331 ip_set_utest(struct sock *ctnl, struct sk_buff *skb,
1332 const struct nlmsghdr *nlh,
1333 const struct nlattr * const attr[])
1336 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1339 if (unlikely(protocol_failed(attr) ||
1340 attr[IPSET_ATTR_SETNAME] == NULL ||
1341 attr[IPSET_ATTR_DATA] == NULL ||
1342 !flag_nested(attr[IPSET_ATTR_DATA])))
1343 return -IPSET_ERR_PROTOCOL;
1345 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
1349 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1350 set->type->adt_policy))
1351 return -IPSET_ERR_PROTOCOL;
1353 read_lock_bh(&set->lock);
1354 ret = set->variant->uadt(set, tb, IPSET_TEST, NULL, 0, 0);
1355 read_unlock_bh(&set->lock);
1356 /* Userspace can't trigger element to be re-added */
1360 return ret < 0 ? ret : ret > 0 ? 0 : -IPSET_ERR_EXIST;
1363 /* Get headed data of a set */
1366 ip_set_header(struct sock *ctnl, struct sk_buff *skb,
1367 const struct nlmsghdr *nlh,
1368 const struct nlattr * const attr[])
1370 const struct ip_set *set;
1371 struct sk_buff *skb2;
1372 struct nlmsghdr *nlh2;
1376 if (unlikely(protocol_failed(attr) ||
1377 attr[IPSET_ATTR_SETNAME] == NULL))
1378 return -IPSET_ERR_PROTOCOL;
1380 index = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
1381 if (index == IPSET_INVALID_ID)
1383 set = ip_set_list[index];
1385 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1389 nlh2 = start_msg(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0,
1393 NLA_PUT_U8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL);
1394 NLA_PUT_STRING(skb2, IPSET_ATTR_SETNAME, set->name);
1395 NLA_PUT_STRING(skb2, IPSET_ATTR_TYPENAME, set->type->name);
1396 NLA_PUT_U8(skb2, IPSET_ATTR_FAMILY, set->family);
1397 NLA_PUT_U8(skb2, IPSET_ATTR_REVISION, set->revision);
1398 nlmsg_end(skb2, nlh2);
1400 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1407 nlmsg_cancel(skb2, nlh2);
1415 static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1416 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1417 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
1418 .len = IPSET_MAXNAMELEN - 1 },
1419 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
1423 ip_set_type(struct sock *ctnl, struct sk_buff *skb,
1424 const struct nlmsghdr *nlh,
1425 const struct nlattr * const attr[])
1427 struct sk_buff *skb2;
1428 struct nlmsghdr *nlh2;
1429 u8 family, min, max;
1430 const char *typename;
1433 if (unlikely(protocol_failed(attr) ||
1434 attr[IPSET_ATTR_TYPENAME] == NULL ||
1435 attr[IPSET_ATTR_FAMILY] == NULL))
1436 return -IPSET_ERR_PROTOCOL;
1438 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1439 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1440 ret = find_set_type_minmax(typename, family, &min, &max);
1444 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1448 nlh2 = start_msg(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0,
1452 NLA_PUT_U8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL);
1453 NLA_PUT_STRING(skb2, IPSET_ATTR_TYPENAME, typename);
1454 NLA_PUT_U8(skb2, IPSET_ATTR_FAMILY, family);
1455 NLA_PUT_U8(skb2, IPSET_ATTR_REVISION, max);
1456 NLA_PUT_U8(skb2, IPSET_ATTR_REVISION_MIN, min);
1457 nlmsg_end(skb2, nlh2);
1459 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
1460 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1467 nlmsg_cancel(skb2, nlh2);
1473 /* Get protocol version */
1475 static const struct nla_policy
1476 ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1477 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1481 ip_set_protocol(struct sock *ctnl, struct sk_buff *skb,
1482 const struct nlmsghdr *nlh,
1483 const struct nlattr * const attr[])
1485 struct sk_buff *skb2;
1486 struct nlmsghdr *nlh2;
1489 if (unlikely(attr[IPSET_ATTR_PROTOCOL] == NULL))
1490 return -IPSET_ERR_PROTOCOL;
1492 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1496 nlh2 = start_msg(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0,
1497 IPSET_CMD_PROTOCOL);
1500 NLA_PUT_U8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL);
1501 nlmsg_end(skb2, nlh2);
1503 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1510 nlmsg_cancel(skb2, nlh2);
1516 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
1517 [IPSET_CMD_CREATE] = {
1518 .call = ip_set_create,
1519 .attr_count = IPSET_ATTR_CMD_MAX,
1520 .policy = ip_set_create_policy,
1522 [IPSET_CMD_DESTROY] = {
1523 .call = ip_set_destroy,
1524 .attr_count = IPSET_ATTR_CMD_MAX,
1525 .policy = ip_set_setname_policy,
1527 [IPSET_CMD_FLUSH] = {
1528 .call = ip_set_flush,
1529 .attr_count = IPSET_ATTR_CMD_MAX,
1530 .policy = ip_set_setname_policy,
1532 [IPSET_CMD_RENAME] = {
1533 .call = ip_set_rename,
1534 .attr_count = IPSET_ATTR_CMD_MAX,
1535 .policy = ip_set_setname2_policy,
1537 [IPSET_CMD_SWAP] = {
1538 .call = ip_set_swap,
1539 .attr_count = IPSET_ATTR_CMD_MAX,
1540 .policy = ip_set_setname2_policy,
1542 [IPSET_CMD_LIST] = {
1543 .call = ip_set_dump,
1544 .attr_count = IPSET_ATTR_CMD_MAX,
1545 .policy = ip_set_setname_policy,
1547 [IPSET_CMD_SAVE] = {
1548 .call = ip_set_dump,
1549 .attr_count = IPSET_ATTR_CMD_MAX,
1550 .policy = ip_set_setname_policy,
1553 .call = ip_set_uadd,
1554 .attr_count = IPSET_ATTR_CMD_MAX,
1555 .policy = ip_set_adt_policy,
1558 .call = ip_set_udel,
1559 .attr_count = IPSET_ATTR_CMD_MAX,
1560 .policy = ip_set_adt_policy,
1562 [IPSET_CMD_TEST] = {
1563 .call = ip_set_utest,
1564 .attr_count = IPSET_ATTR_CMD_MAX,
1565 .policy = ip_set_adt_policy,
1567 [IPSET_CMD_HEADER] = {
1568 .call = ip_set_header,
1569 .attr_count = IPSET_ATTR_CMD_MAX,
1570 .policy = ip_set_setname_policy,
1572 [IPSET_CMD_TYPE] = {
1573 .call = ip_set_type,
1574 .attr_count = IPSET_ATTR_CMD_MAX,
1575 .policy = ip_set_type_policy,
1577 [IPSET_CMD_PROTOCOL] = {
1578 .call = ip_set_protocol,
1579 .attr_count = IPSET_ATTR_CMD_MAX,
1580 .policy = ip_set_protocol_policy,
1584 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
1586 .subsys_id = NFNL_SUBSYS_IPSET,
1587 .cb_count = IPSET_MSG_MAX,
1588 .cb = ip_set_netlink_subsys_cb,
1591 /* Interface to iptables/ip6tables */
1594 ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
1598 int copylen = *len, ret = 0;
1600 if (!capable(CAP_NET_ADMIN))
1602 if (optval != SO_IP_SET)
1604 if (*len < sizeof(unsigned))
1607 data = vmalloc(*len);
1610 if (copy_from_user(data, user, *len) != 0) {
1614 op = (unsigned *) data;
1616 if (*op < IP_SET_OP_VERSION) {
1617 /* Check the version at the beginning of operations */
1618 struct ip_set_req_version *req_version = data;
1619 if (req_version->version != IPSET_PROTOCOL) {
1626 case IP_SET_OP_VERSION: {
1627 struct ip_set_req_version *req_version = data;
1629 if (*len != sizeof(struct ip_set_req_version)) {
1634 req_version->version = IPSET_PROTOCOL;
1635 ret = copy_to_user(user, req_version,
1636 sizeof(struct ip_set_req_version));
1639 case IP_SET_OP_GET_BYNAME: {
1640 struct ip_set_req_get_set *req_get = data;
1642 if (*len != sizeof(struct ip_set_req_get_set)) {
1646 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1648 req_get->set.index = find_set_id(req_get->set.name);
1652 case IP_SET_OP_GET_BYINDEX: {
1653 struct ip_set_req_get_set *req_get = data;
1655 if (*len != sizeof(struct ip_set_req_get_set) ||
1656 req_get->set.index >= ip_set_max) {
1661 strncpy(req_get->set.name,
1662 ip_set_list[req_get->set.index]
1663 ? ip_set_list[req_get->set.index]->name : "",
1671 } /* end of switch(op) */
1674 ret = copy_to_user(user, data, copylen);
1683 static struct nf_sockopt_ops so_set __read_mostly = {
1685 .get_optmin = SO_IP_SET,
1686 .get_optmax = SO_IP_SET + 1,
1687 .get = &ip_set_sockfn_get,
1688 .owner = THIS_MODULE,
1697 ip_set_max = max_sets;
1698 if (ip_set_max >= IPSET_INVALID_ID)
1699 ip_set_max = IPSET_INVALID_ID - 1;
1701 ip_set_list = kzalloc(sizeof(struct ip_set *) * ip_set_max,
1704 pr_err("ip_set: Unable to create ip_set_list\n");
1708 ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
1710 pr_err("ip_set: cannot register with nfnetlink.\n");
1714 ret = nf_register_sockopt(&so_set);
1716 pr_err("SO_SET registry failed: %d\n", ret);
1717 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
1722 pr_notice("ip_set: protocol %u\n", IPSET_PROTOCOL);
1729 /* There can't be any existing set */
1730 nf_unregister_sockopt(&so_set);
1731 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
1733 pr_debug("these are the famous last words\n");
1736 module_init(ip_set_init);
1737 module_exit(ip_set_fini);