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/nfnetlink.h>
25 #include <linux/netfilter/ipset/ip_set.h>
27 static LIST_HEAD(ip_set_type_list
); /* all registered set types */
28 static DEFINE_MUTEX(ip_set_type_mutex
); /* protects ip_set_type_list */
29 static DEFINE_RWLOCK(ip_set_ref_lock
); /* protects the set refs */
31 static struct ip_set
**ip_set_list
; /* all individual sets */
32 static ip_set_id_t ip_set_max
= CONFIG_IP_SET_MAX
; /* max number of sets */
34 #define STREQ(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
36 static unsigned int max_sets
;
38 module_param(max_sets
, int, 0600);
39 MODULE_PARM_DESC(max_sets
, "maximal number of sets");
40 MODULE_LICENSE("GPL");
41 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
42 MODULE_DESCRIPTION("core IP set support");
43 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET
);
46 * The set types are implemented in modules and registered set types
47 * can be found in ip_set_type_list. Adding/deleting types is
48 * serialized by ip_set_type_mutex.
52 ip_set_type_lock(void)
54 mutex_lock(&ip_set_type_mutex
);
58 ip_set_type_unlock(void)
60 mutex_unlock(&ip_set_type_mutex
);
63 /* Register and deregister settype */
65 static struct ip_set_type
*
66 find_set_type(const char *name
, u8 family
, u8 revision
)
68 struct ip_set_type
*type
;
70 list_for_each_entry_rcu(type
, &ip_set_type_list
, list
)
71 if (STREQ(type
->name
, name
) &&
72 (type
->family
== family
|| type
->family
== AF_UNSPEC
) &&
73 type
->revision
== revision
)
78 /* Unlock, try to load a set type module and lock again */
80 try_to_load_type(const char *name
)
83 pr_debug("try to load ip_set_%s\n", name
);
84 if (request_module("ip_set_%s", name
) < 0) {
85 pr_warning("Can't find ip_set type %s\n", name
);
87 return -IPSET_ERR_FIND_TYPE
;
93 /* Find a set type and reference it */
95 find_set_type_get(const char *name
, u8 family
, u8 revision
,
96 struct ip_set_type
**found
)
98 struct ip_set_type
*type
;
102 *found
= find_set_type(name
, family
, revision
);
104 err
= !try_module_get((*found
)->me
) ? -EFAULT
: 0;
107 /* Make sure the type is loaded but we don't support the revision */
108 list_for_each_entry_rcu(type
, &ip_set_type_list
, list
)
109 if (STREQ(type
->name
, name
)) {
110 err
= -IPSET_ERR_FIND_TYPE
;
115 return try_to_load_type(name
);
122 /* Find a given set type by name and family.
123 * If we succeeded, the supported minimal and maximum revisions are
127 find_set_type_minmax(const char *name
, u8 family
, u8
*min
, u8
*max
)
129 struct ip_set_type
*type
;
132 *min
= 255; *max
= 0;
134 list_for_each_entry_rcu(type
, &ip_set_type_list
, list
)
135 if (STREQ(type
->name
, name
) &&
136 (type
->family
== family
|| type
->family
== AF_UNSPEC
)) {
138 if (type
->revision
< *min
)
139 *min
= type
->revision
;
140 if (type
->revision
> *max
)
141 *max
= type
->revision
;
147 return try_to_load_type(name
);
150 #define family_name(f) ((f) == AF_INET ? "inet" : \
151 (f) == AF_INET6 ? "inet6" : "any")
153 /* Register a set type structure. The type is identified by
154 * the unique triple of name, family and revision.
157 ip_set_type_register(struct ip_set_type
*type
)
161 if (type
->protocol
!= IPSET_PROTOCOL
) {
162 pr_warning("ip_set type %s, family %s, revision %u uses "
163 "wrong protocol version %u (want %u)\n",
164 type
->name
, family_name(type
->family
),
165 type
->revision
, type
->protocol
, IPSET_PROTOCOL
);
170 if (find_set_type(type
->name
, type
->family
, type
->revision
)) {
172 pr_warning("ip_set type %s, family %s, revision %u "
173 "already registered!\n", type
->name
,
174 family_name(type
->family
), type
->revision
);
178 list_add_rcu(&type
->list
, &ip_set_type_list
);
179 pr_debug("type %s, family %s, revision %u registered.\n",
180 type
->name
, family_name(type
->family
), type
->revision
);
182 ip_set_type_unlock();
185 EXPORT_SYMBOL_GPL(ip_set_type_register
);
187 /* Unregister a set type. There's a small race with ip_set_create */
189 ip_set_type_unregister(struct ip_set_type
*type
)
192 if (!find_set_type(type
->name
, type
->family
, type
->revision
)) {
193 pr_warning("ip_set type %s, family %s, revision %u "
194 "not registered\n", type
->name
,
195 family_name(type
->family
), type
->revision
);
198 list_del_rcu(&type
->list
);
199 pr_debug("type %s, family %s, revision %u unregistered.\n",
200 type
->name
, family_name(type
->family
), type
->revision
);
202 ip_set_type_unlock();
206 EXPORT_SYMBOL_GPL(ip_set_type_unregister
);
208 /* Utility functions */
210 ip_set_alloc(size_t size
)
212 void *members
= NULL
;
214 if (size
< KMALLOC_MAX_SIZE
)
215 members
= kzalloc(size
, GFP_KERNEL
| __GFP_NOWARN
);
218 pr_debug("%p: allocated with kmalloc\n", members
);
222 members
= vzalloc(size
);
225 pr_debug("%p: allocated with vmalloc\n", members
);
229 EXPORT_SYMBOL_GPL(ip_set_alloc
);
232 ip_set_free(void *members
)
234 pr_debug("%p: free with %s\n", members
,
235 is_vmalloc_addr(members
) ? "vfree" : "kfree");
236 if (is_vmalloc_addr(members
))
241 EXPORT_SYMBOL_GPL(ip_set_free
);
244 flag_nested(const struct nlattr
*nla
)
246 return nla
->nla_type
& NLA_F_NESTED
;
249 static const struct nla_policy ipaddr_policy
[IPSET_ATTR_IPADDR_MAX
+ 1] = {
250 [IPSET_ATTR_IPADDR_IPV4
] = { .type
= NLA_U32
},
251 [IPSET_ATTR_IPADDR_IPV6
] = { .type
= NLA_BINARY
,
252 .len
= sizeof(struct in6_addr
) },
256 ip_set_get_ipaddr4(struct nlattr
*nla
, __be32
*ipaddr
)
258 struct nlattr
*tb
[IPSET_ATTR_IPADDR_MAX
+1];
260 if (unlikely(!flag_nested(nla
)))
261 return -IPSET_ERR_PROTOCOL
;
262 if (nla_parse_nested(tb
, IPSET_ATTR_IPADDR_MAX
, nla
, ipaddr_policy
))
263 return -IPSET_ERR_PROTOCOL
;
264 if (unlikely(!ip_set_attr_netorder(tb
, IPSET_ATTR_IPADDR_IPV4
)))
265 return -IPSET_ERR_PROTOCOL
;
267 *ipaddr
= nla_get_be32(tb
[IPSET_ATTR_IPADDR_IPV4
]);
270 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4
);
273 ip_set_get_ipaddr6(struct nlattr
*nla
, union nf_inet_addr
*ipaddr
)
275 struct nlattr
*tb
[IPSET_ATTR_IPADDR_MAX
+1];
277 if (unlikely(!flag_nested(nla
)))
278 return -IPSET_ERR_PROTOCOL
;
280 if (nla_parse_nested(tb
, IPSET_ATTR_IPADDR_MAX
, nla
, ipaddr_policy
))
281 return -IPSET_ERR_PROTOCOL
;
282 if (unlikely(!ip_set_attr_netorder(tb
, IPSET_ATTR_IPADDR_IPV6
)))
283 return -IPSET_ERR_PROTOCOL
;
285 memcpy(ipaddr
, nla_data(tb
[IPSET_ATTR_IPADDR_IPV6
]),
286 sizeof(struct in6_addr
));
289 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6
);
292 * Creating/destroying/renaming/swapping affect the existence and
293 * the properties of a set. All of these can be executed from userspace
294 * only and serialized by the nfnl mutex indirectly from nfnetlink.
296 * Sets are identified by their index in ip_set_list and the index
297 * is used by the external references (set/SET netfilter modules).
299 * The set behind an index may change by swapping only, from userspace.
303 __ip_set_get(ip_set_id_t index
)
305 write_lock_bh(&ip_set_ref_lock
);
306 ip_set_list
[index
]->ref
++;
307 write_unlock_bh(&ip_set_ref_lock
);
311 __ip_set_put(ip_set_id_t index
)
313 write_lock_bh(&ip_set_ref_lock
);
314 BUG_ON(ip_set_list
[index
]->ref
== 0);
315 ip_set_list
[index
]->ref
--;
316 write_unlock_bh(&ip_set_ref_lock
);
320 * Add, del and test set entries from kernel.
322 * The set behind the index must exist and must be referenced
323 * so it can't be destroyed (or changed) under our foot.
327 ip_set_test(ip_set_id_t index
, const struct sk_buff
*skb
,
328 u8 family
, u8 dim
, u8 flags
)
330 struct ip_set
*set
= ip_set_list
[index
];
334 pr_debug("set %s, index %u\n", set
->name
, index
);
336 if (dim
< set
->type
->dimension
||
337 !(family
== set
->family
|| set
->family
== AF_UNSPEC
))
340 read_lock_bh(&set
->lock
);
341 ret
= set
->variant
->kadt(set
, skb
, IPSET_TEST
, family
, dim
, flags
);
342 read_unlock_bh(&set
->lock
);
344 if (ret
== -EAGAIN
) {
345 /* Type requests element to be completed */
346 pr_debug("element must be competed, ADD is triggered\n");
347 write_lock_bh(&set
->lock
);
348 set
->variant
->kadt(set
, skb
, IPSET_ADD
, family
, dim
, flags
);
349 write_unlock_bh(&set
->lock
);
353 /* Convert error codes to nomatch */
354 return (ret
< 0 ? 0 : ret
);
356 EXPORT_SYMBOL_GPL(ip_set_test
);
359 ip_set_add(ip_set_id_t index
, const struct sk_buff
*skb
,
360 u8 family
, u8 dim
, u8 flags
)
362 struct ip_set
*set
= ip_set_list
[index
];
366 pr_debug("set %s, index %u\n", set
->name
, index
);
368 if (dim
< set
->type
->dimension
||
369 !(family
== set
->family
|| set
->family
== AF_UNSPEC
))
372 write_lock_bh(&set
->lock
);
373 ret
= set
->variant
->kadt(set
, skb
, IPSET_ADD
, family
, dim
, flags
);
374 write_unlock_bh(&set
->lock
);
378 EXPORT_SYMBOL_GPL(ip_set_add
);
381 ip_set_del(ip_set_id_t index
, const struct sk_buff
*skb
,
382 u8 family
, u8 dim
, u8 flags
)
384 struct ip_set
*set
= ip_set_list
[index
];
388 pr_debug("set %s, index %u\n", set
->name
, index
);
390 if (dim
< set
->type
->dimension
||
391 !(family
== set
->family
|| set
->family
== AF_UNSPEC
))
394 write_lock_bh(&set
->lock
);
395 ret
= set
->variant
->kadt(set
, skb
, IPSET_DEL
, family
, dim
, flags
);
396 write_unlock_bh(&set
->lock
);
400 EXPORT_SYMBOL_GPL(ip_set_del
);
403 * Find set by name, reference it once. The reference makes sure the
404 * thing pointed to, does not go away under our feet.
408 ip_set_get_byname(const char *name
, struct ip_set
**set
)
410 ip_set_id_t i
, index
= IPSET_INVALID_ID
;
413 for (i
= 0; i
< ip_set_max
; i
++) {
415 if (s
!= NULL
&& STREQ(s
->name
, name
)) {
424 EXPORT_SYMBOL_GPL(ip_set_get_byname
);
427 * If the given set pointer points to a valid set, decrement
428 * reference count by 1. The caller shall not assume the index
429 * to be valid, after calling this function.
433 ip_set_put_byindex(ip_set_id_t index
)
435 if (ip_set_list
[index
] != NULL
)
438 EXPORT_SYMBOL_GPL(ip_set_put_byindex
);
441 * Get the name of a set behind a set index.
442 * We assume the set is referenced, so it does exist and
443 * can't be destroyed. The set cannot be renamed due to
444 * the referencing either.
448 ip_set_name_byindex(ip_set_id_t index
)
450 const struct ip_set
*set
= ip_set_list
[index
];
453 BUG_ON(set
->ref
== 0);
455 /* Referenced, so it's safe */
458 EXPORT_SYMBOL_GPL(ip_set_name_byindex
);
461 * Routines to call by external subsystems, which do not
462 * call nfnl_lock for us.
466 * Find set by name, reference it once. The reference makes sure the
467 * thing pointed to, does not go away under our feet.
469 * The nfnl mutex is used in the function.
472 ip_set_nfnl_get(const char *name
)
478 index
= ip_set_get_byname(name
, &s
);
483 EXPORT_SYMBOL_GPL(ip_set_nfnl_get
);
486 * Find set by index, reference it once. The reference makes sure the
487 * thing pointed to, does not go away under our feet.
489 * The nfnl mutex is used in the function.
492 ip_set_nfnl_get_byindex(ip_set_id_t index
)
494 if (index
> ip_set_max
)
495 return IPSET_INVALID_ID
;
498 if (ip_set_list
[index
])
501 index
= IPSET_INVALID_ID
;
506 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex
);
509 * If the given set pointer points to a valid set, decrement
510 * reference count by 1. The caller shall not assume the index
511 * to be valid, after calling this function.
513 * The nfnl mutex is used in the function.
516 ip_set_nfnl_put(ip_set_id_t index
)
519 ip_set_put_byindex(index
);
522 EXPORT_SYMBOL_GPL(ip_set_nfnl_put
);
525 * Communication protocol with userspace over netlink.
527 * The commands are serialized by the nfnl mutex.
531 protocol_failed(const struct nlattr
* const tb
[])
533 return !tb
[IPSET_ATTR_PROTOCOL
] ||
534 nla_get_u8(tb
[IPSET_ATTR_PROTOCOL
]) != IPSET_PROTOCOL
;
538 flag_exist(const struct nlmsghdr
*nlh
)
540 return nlh
->nlmsg_flags
& NLM_F_EXCL
? 0 : IPSET_FLAG_EXIST
;
543 static struct nlmsghdr
*
544 start_msg(struct sk_buff
*skb
, u32 pid
, u32 seq
, unsigned int flags
,
547 struct nlmsghdr
*nlh
;
548 struct nfgenmsg
*nfmsg
;
550 nlh
= nlmsg_put(skb
, pid
, seq
, cmd
| (NFNL_SUBSYS_IPSET
<< 8),
551 sizeof(*nfmsg
), flags
);
555 nfmsg
= nlmsg_data(nlh
);
556 nfmsg
->nfgen_family
= AF_INET
;
557 nfmsg
->version
= NFNETLINK_V0
;
565 static const struct nla_policy ip_set_create_policy
[IPSET_ATTR_CMD_MAX
+ 1] = {
566 [IPSET_ATTR_PROTOCOL
] = { .type
= NLA_U8
},
567 [IPSET_ATTR_SETNAME
] = { .type
= NLA_NUL_STRING
,
568 .len
= IPSET_MAXNAMELEN
- 1 },
569 [IPSET_ATTR_TYPENAME
] = { .type
= NLA_NUL_STRING
,
570 .len
= IPSET_MAXNAMELEN
- 1},
571 [IPSET_ATTR_REVISION
] = { .type
= NLA_U8
},
572 [IPSET_ATTR_FAMILY
] = { .type
= NLA_U8
},
573 [IPSET_ATTR_DATA
] = { .type
= NLA_NESTED
},
577 find_set_id(const char *name
)
579 ip_set_id_t i
, index
= IPSET_INVALID_ID
;
580 const struct ip_set
*set
;
582 for (i
= 0; index
== IPSET_INVALID_ID
&& i
< ip_set_max
; i
++) {
583 set
= ip_set_list
[i
];
584 if (set
!= NULL
&& STREQ(set
->name
, name
))
590 static inline struct ip_set
*
591 find_set(const char *name
)
593 ip_set_id_t index
= find_set_id(name
);
595 return index
== IPSET_INVALID_ID
? NULL
: ip_set_list
[index
];
599 find_free_id(const char *name
, ip_set_id_t
*index
, struct ip_set
**set
)
603 *index
= IPSET_INVALID_ID
;
604 for (i
= 0; i
< ip_set_max
; i
++) {
605 if (ip_set_list
[i
] == NULL
) {
606 if (*index
== IPSET_INVALID_ID
)
608 } else if (STREQ(name
, ip_set_list
[i
]->name
)) {
610 *set
= ip_set_list
[i
];
614 if (*index
== IPSET_INVALID_ID
)
615 /* No free slot remained */
616 return -IPSET_ERR_MAX_SETS
;
621 ip_set_create(struct sock
*ctnl
, struct sk_buff
*skb
,
622 const struct nlmsghdr
*nlh
,
623 const struct nlattr
* const attr
[])
625 struct ip_set
*set
, *clash
= NULL
;
626 ip_set_id_t index
= IPSET_INVALID_ID
;
627 struct nlattr
*tb
[IPSET_ATTR_CREATE_MAX
+1] = {};
628 const char *name
, *typename
;
630 u32 flags
= flag_exist(nlh
);
633 if (unlikely(protocol_failed(attr
) ||
634 attr
[IPSET_ATTR_SETNAME
] == NULL
||
635 attr
[IPSET_ATTR_TYPENAME
] == NULL
||
636 attr
[IPSET_ATTR_REVISION
] == NULL
||
637 attr
[IPSET_ATTR_FAMILY
] == NULL
||
638 (attr
[IPSET_ATTR_DATA
] != NULL
&&
639 !flag_nested(attr
[IPSET_ATTR_DATA
]))))
640 return -IPSET_ERR_PROTOCOL
;
642 name
= nla_data(attr
[IPSET_ATTR_SETNAME
]);
643 typename
= nla_data(attr
[IPSET_ATTR_TYPENAME
]);
644 family
= nla_get_u8(attr
[IPSET_ATTR_FAMILY
]);
645 revision
= nla_get_u8(attr
[IPSET_ATTR_REVISION
]);
646 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
647 name
, typename
, family_name(family
), revision
);
650 * First, and without any locks, allocate and initialize
651 * a normal base set structure.
653 set
= kzalloc(sizeof(struct ip_set
), GFP_KERNEL
);
656 rwlock_init(&set
->lock
);
657 strlcpy(set
->name
, name
, IPSET_MAXNAMELEN
);
658 set
->family
= family
;
661 * Next, check that we know the type, and take
662 * a reference on the type, to make sure it stays available
663 * while constructing our new set.
665 * After referencing the type, we try to create the type
666 * specific part of the set without holding any locks.
668 ret
= find_set_type_get(typename
, family
, revision
, &(set
->type
));
673 * Without holding any locks, create private part.
675 if (attr
[IPSET_ATTR_DATA
] &&
676 nla_parse_nested(tb
, IPSET_ATTR_CREATE_MAX
, attr
[IPSET_ATTR_DATA
],
677 set
->type
->create_policy
)) {
678 ret
= -IPSET_ERR_PROTOCOL
;
682 ret
= set
->type
->create(set
, tb
, flags
);
686 /* BTW, ret==0 here. */
689 * Here, we have a valid, constructed set and we are protected
690 * by the nfnl mutex. Find the first free index in ip_set_list
691 * and check clashing.
693 if ((ret
= find_free_id(set
->name
, &index
, &clash
)) != 0) {
694 /* If this is the same set and requested, ignore error */
695 if (ret
== -EEXIST
&&
696 (flags
& IPSET_FLAG_EXIST
) &&
697 STREQ(set
->type
->name
, clash
->type
->name
) &&
698 set
->type
->family
== clash
->type
->family
&&
699 set
->type
->revision
== clash
->type
->revision
&&
700 set
->variant
->same_set(set
, clash
))
706 * Finally! Add our shiny new set to the list, and be done.
708 pr_debug("create: '%s' created with index %u!\n", set
->name
, index
);
709 ip_set_list
[index
] = set
;
714 set
->variant
->destroy(set
);
716 module_put(set
->type
->me
);
724 static const struct nla_policy
725 ip_set_setname_policy
[IPSET_ATTR_CMD_MAX
+ 1] = {
726 [IPSET_ATTR_PROTOCOL
] = { .type
= NLA_U8
},
727 [IPSET_ATTR_SETNAME
] = { .type
= NLA_NUL_STRING
,
728 .len
= IPSET_MAXNAMELEN
- 1 },
732 ip_set_destroy_set(ip_set_id_t index
)
734 struct ip_set
*set
= ip_set_list
[index
];
736 pr_debug("set: %s\n", set
->name
);
737 ip_set_list
[index
] = NULL
;
739 /* Must call it without holding any lock */
740 set
->variant
->destroy(set
);
741 module_put(set
->type
->me
);
746 ip_set_destroy(struct sock
*ctnl
, struct sk_buff
*skb
,
747 const struct nlmsghdr
*nlh
,
748 const struct nlattr
* const attr
[])
753 if (unlikely(protocol_failed(attr
)))
754 return -IPSET_ERR_PROTOCOL
;
756 /* Commands are serialized and references are
757 * protected by the ip_set_ref_lock.
758 * External systems (i.e. xt_set) must call
759 * ip_set_put|get_nfnl_* functions, that way we
760 * can safely check references here.
762 * list:set timer can only decrement the reference
763 * counter, so if it's already zero, we can proceed
764 * without holding the lock.
766 read_lock_bh(&ip_set_ref_lock
);
767 if (!attr
[IPSET_ATTR_SETNAME
]) {
768 for (i
= 0; i
< ip_set_max
; i
++) {
769 if (ip_set_list
[i
] != NULL
&& ip_set_list
[i
]->ref
) {
770 ret
= IPSET_ERR_BUSY
;
774 read_unlock_bh(&ip_set_ref_lock
);
775 for (i
= 0; i
< ip_set_max
; i
++) {
776 if (ip_set_list
[i
] != NULL
)
777 ip_set_destroy_set(i
);
780 i
= find_set_id(nla_data(attr
[IPSET_ATTR_SETNAME
]));
781 if (i
== IPSET_INVALID_ID
) {
784 } else if (ip_set_list
[i
]->ref
) {
785 ret
= -IPSET_ERR_BUSY
;
788 read_unlock_bh(&ip_set_ref_lock
);
790 ip_set_destroy_set(i
);
794 read_unlock_bh(&ip_set_ref_lock
);
801 ip_set_flush_set(struct ip_set
*set
)
803 pr_debug("set: %s\n", set
->name
);
805 write_lock_bh(&set
->lock
);
806 set
->variant
->flush(set
);
807 write_unlock_bh(&set
->lock
);
811 ip_set_flush(struct sock
*ctnl
, struct sk_buff
*skb
,
812 const struct nlmsghdr
*nlh
,
813 const struct nlattr
* const attr
[])
817 if (unlikely(protocol_failed(attr
)))
818 return -IPSET_ERR_PROTOCOL
;
820 if (!attr
[IPSET_ATTR_SETNAME
]) {
821 for (i
= 0; i
< ip_set_max
; i
++)
822 if (ip_set_list
[i
] != NULL
)
823 ip_set_flush_set(ip_set_list
[i
]);
825 i
= find_set_id(nla_data(attr
[IPSET_ATTR_SETNAME
]));
826 if (i
== IPSET_INVALID_ID
)
829 ip_set_flush_set(ip_set_list
[i
]);
837 static const struct nla_policy
838 ip_set_setname2_policy
[IPSET_ATTR_CMD_MAX
+ 1] = {
839 [IPSET_ATTR_PROTOCOL
] = { .type
= NLA_U8
},
840 [IPSET_ATTR_SETNAME
] = { .type
= NLA_NUL_STRING
,
841 .len
= IPSET_MAXNAMELEN
- 1 },
842 [IPSET_ATTR_SETNAME2
] = { .type
= NLA_NUL_STRING
,
843 .len
= IPSET_MAXNAMELEN
- 1 },
847 ip_set_rename(struct sock
*ctnl
, struct sk_buff
*skb
,
848 const struct nlmsghdr
*nlh
,
849 const struct nlattr
* const attr
[])
856 if (unlikely(protocol_failed(attr
) ||
857 attr
[IPSET_ATTR_SETNAME
] == NULL
||
858 attr
[IPSET_ATTR_SETNAME2
] == NULL
))
859 return -IPSET_ERR_PROTOCOL
;
861 set
= find_set(nla_data(attr
[IPSET_ATTR_SETNAME
]));
865 read_lock_bh(&ip_set_ref_lock
);
867 ret
= -IPSET_ERR_REFERENCED
;
871 name2
= nla_data(attr
[IPSET_ATTR_SETNAME2
]);
872 for (i
= 0; i
< ip_set_max
; i
++) {
873 if (ip_set_list
[i
] != NULL
&&
874 STREQ(ip_set_list
[i
]->name
, name2
)) {
875 ret
= -IPSET_ERR_EXIST_SETNAME2
;
879 strncpy(set
->name
, name2
, IPSET_MAXNAMELEN
);
882 read_unlock_bh(&ip_set_ref_lock
);
886 /* Swap two sets so that name/index points to the other.
887 * References and set names are also swapped.
889 * The commands are serialized by the nfnl mutex and references are
890 * protected by the ip_set_ref_lock. The kernel interfaces
891 * do not hold the mutex but the pointer settings are atomic
892 * so the ip_set_list always contains valid pointers to the sets.
896 ip_set_swap(struct sock
*ctnl
, struct sk_buff
*skb
,
897 const struct nlmsghdr
*nlh
,
898 const struct nlattr
* const attr
[])
900 struct ip_set
*from
, *to
;
901 ip_set_id_t from_id
, to_id
;
902 char from_name
[IPSET_MAXNAMELEN
];
904 if (unlikely(protocol_failed(attr
) ||
905 attr
[IPSET_ATTR_SETNAME
] == NULL
||
906 attr
[IPSET_ATTR_SETNAME2
] == NULL
))
907 return -IPSET_ERR_PROTOCOL
;
909 from_id
= find_set_id(nla_data(attr
[IPSET_ATTR_SETNAME
]));
910 if (from_id
== IPSET_INVALID_ID
)
913 to_id
= find_set_id(nla_data(attr
[IPSET_ATTR_SETNAME2
]));
914 if (to_id
== IPSET_INVALID_ID
)
915 return -IPSET_ERR_EXIST_SETNAME2
;
917 from
= ip_set_list
[from_id
];
918 to
= ip_set_list
[to_id
];
920 /* Features must not change.
921 * Not an artificial restriction anymore, as we must prevent
922 * possible loops created by swapping in setlist type of sets. */
923 if (!(from
->type
->features
== to
->type
->features
&&
924 from
->type
->family
== to
->type
->family
))
925 return -IPSET_ERR_TYPE_MISMATCH
;
927 strncpy(from_name
, from
->name
, IPSET_MAXNAMELEN
);
928 strncpy(from
->name
, to
->name
, IPSET_MAXNAMELEN
);
929 strncpy(to
->name
, from_name
, IPSET_MAXNAMELEN
);
931 write_lock_bh(&ip_set_ref_lock
);
932 swap(from
->ref
, to
->ref
);
933 ip_set_list
[from_id
] = to
;
934 ip_set_list
[to_id
] = from
;
935 write_unlock_bh(&ip_set_ref_lock
);
940 /* List/save set data */
948 ip_set_dump_done(struct netlink_callback
*cb
)
951 pr_debug("release set %s\n", ip_set_list
[cb
->args
[1]]->name
);
952 ip_set_put_byindex((ip_set_id_t
) cb
->args
[1]);
958 dump_attrs(struct nlmsghdr
*nlh
)
960 const struct nlattr
*attr
;
963 pr_debug("dump nlmsg\n");
964 nlmsg_for_each_attr(attr
, nlh
, sizeof(struct nfgenmsg
), rem
) {
965 pr_debug("type: %u, len %u\n", nla_type(attr
), attr
->nla_len
);
970 dump_init(struct netlink_callback
*cb
)
972 struct nlmsghdr
*nlh
= nlmsg_hdr(cb
->skb
);
973 int min_len
= NLMSG_SPACE(sizeof(struct nfgenmsg
));
974 struct nlattr
*cda
[IPSET_ATTR_CMD_MAX
+1];
975 struct nlattr
*attr
= (void *)nlh
+ min_len
;
978 /* Second pass, so parser can't fail */
979 nla_parse(cda
, IPSET_ATTR_CMD_MAX
,
980 attr
, nlh
->nlmsg_len
- min_len
, ip_set_setname_policy
);
982 /* cb->args[0] : dump single set/all sets
984 * [..]: type specific
987 if (!cda
[IPSET_ATTR_SETNAME
]) {
988 cb
->args
[0] = DUMP_ALL
;
992 index
= find_set_id(nla_data(cda
[IPSET_ATTR_SETNAME
]));
993 if (index
== IPSET_INVALID_ID
)
996 cb
->args
[0] = DUMP_ONE
;
1002 ip_set_dump_start(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1004 ip_set_id_t index
= IPSET_INVALID_ID
, max
;
1005 struct ip_set
*set
= NULL
;
1006 struct nlmsghdr
*nlh
= NULL
;
1007 unsigned int flags
= NETLINK_CB(cb
->skb
).pid
? NLM_F_MULTI
: 0;
1010 if (cb
->args
[0] == DUMP_INIT
) {
1011 ret
= dump_init(cb
);
1013 nlh
= nlmsg_hdr(cb
->skb
);
1014 /* We have to create and send the error message
1016 if (nlh
->nlmsg_flags
& NLM_F_ACK
)
1017 netlink_ack(cb
->skb
, nlh
, ret
);
1022 if (cb
->args
[1] >= ip_set_max
)
1025 max
= cb
->args
[0] == DUMP_ONE
? cb
->args
[1] + 1 : ip_set_max
;
1027 pr_debug("args[0]: %ld args[1]: %ld\n", cb
->args
[0], cb
->args
[1]);
1028 for (; cb
->args
[1] < max
; cb
->args
[1]++) {
1029 index
= (ip_set_id_t
) cb
->args
[1];
1030 set
= ip_set_list
[index
];
1032 if (cb
->args
[0] == DUMP_ONE
) {
1038 /* When dumping all sets, we must dump "sorted"
1039 * so that lists (unions of sets) are dumped last.
1041 if (cb
->args
[0] != DUMP_ONE
&&
1042 ((cb
->args
[0] == DUMP_ALL
) ==
1043 !!(set
->type
->features
& IPSET_DUMP_LAST
)))
1045 pr_debug("List set: %s\n", set
->name
);
1047 /* Start listing: make sure set won't be destroyed */
1048 pr_debug("reference set\n");
1049 __ip_set_get(index
);
1051 nlh
= start_msg(skb
, NETLINK_CB(cb
->skb
).pid
,
1052 cb
->nlh
->nlmsg_seq
, flags
,
1056 goto release_refcount
;
1058 NLA_PUT_U8(skb
, IPSET_ATTR_PROTOCOL
, IPSET_PROTOCOL
);
1059 NLA_PUT_STRING(skb
, IPSET_ATTR_SETNAME
, set
->name
);
1060 switch (cb
->args
[2]) {
1062 /* Core header data */
1063 NLA_PUT_STRING(skb
, IPSET_ATTR_TYPENAME
,
1065 NLA_PUT_U8(skb
, IPSET_ATTR_FAMILY
,
1067 NLA_PUT_U8(skb
, IPSET_ATTR_REVISION
,
1068 set
->type
->revision
);
1069 ret
= set
->variant
->head(set
, skb
);
1071 goto release_refcount
;
1072 /* Fall through and add elements */
1074 read_lock_bh(&set
->lock
);
1075 ret
= set
->variant
->list(set
, skb
, cb
);
1076 read_unlock_bh(&set
->lock
);
1078 /* Set is done, proceed with next one */
1079 if (cb
->args
[0] == DUMP_ONE
)
1080 cb
->args
[1] = IPSET_INVALID_ID
;
1084 goto release_refcount
;
1087 /* If we dump all sets, continue with dumping last ones */
1088 if (cb
->args
[0] == DUMP_ALL
) {
1089 cb
->args
[0] = DUMP_LAST
;
1098 /* If there was an error or set is done, release set */
1099 if (ret
|| !cb
->args
[2]) {
1100 pr_debug("release set %s\n", ip_set_list
[index
]->name
);
1101 ip_set_put_byindex(index
);
1105 nlmsg_end(skb
, nlh
);
1106 pr_debug("nlmsg_len: %u\n", nlh
->nlmsg_len
);
1110 return ret
< 0 ? ret
: skb
->len
;
1114 ip_set_dump(struct sock
*ctnl
, struct sk_buff
*skb
,
1115 const struct nlmsghdr
*nlh
,
1116 const struct nlattr
* const attr
[])
1118 if (unlikely(protocol_failed(attr
)))
1119 return -IPSET_ERR_PROTOCOL
;
1121 return netlink_dump_start(ctnl
, skb
, nlh
,
1123 ip_set_dump_done
, 0);
1126 /* Add, del and test */
1128 static const struct nla_policy ip_set_adt_policy
[IPSET_ATTR_CMD_MAX
+ 1] = {
1129 [IPSET_ATTR_PROTOCOL
] = { .type
= NLA_U8
},
1130 [IPSET_ATTR_SETNAME
] = { .type
= NLA_NUL_STRING
,
1131 .len
= IPSET_MAXNAMELEN
- 1 },
1132 [IPSET_ATTR_LINENO
] = { .type
= NLA_U32
},
1133 [IPSET_ATTR_DATA
] = { .type
= NLA_NESTED
},
1134 [IPSET_ATTR_ADT
] = { .type
= NLA_NESTED
},
1138 call_ad(struct sock
*ctnl
, struct sk_buff
*skb
, struct ip_set
*set
,
1139 struct nlattr
*tb
[], enum ipset_adt adt
,
1140 u32 flags
, bool use_lineno
)
1142 int ret
, retried
= 0;
1144 bool eexist
= flags
& IPSET_FLAG_EXIST
;
1147 write_lock_bh(&set
->lock
);
1148 ret
= set
->variant
->uadt(set
, tb
, adt
, &lineno
, flags
);
1149 write_unlock_bh(&set
->lock
);
1150 } while (ret
== -EAGAIN
&&
1151 set
->variant
->resize
&&
1152 (ret
= set
->variant
->resize(set
, retried
++)) == 0);
1154 if (!ret
|| (ret
== -IPSET_ERR_EXIST
&& eexist
))
1156 if (lineno
&& use_lineno
) {
1157 /* Error in restore/batch mode: send back lineno */
1158 struct nlmsghdr
*rep
, *nlh
= nlmsg_hdr(skb
);
1159 struct sk_buff
*skb2
;
1160 struct nlmsgerr
*errmsg
;
1161 size_t payload
= sizeof(*errmsg
) + nlmsg_len(nlh
);
1162 int min_len
= NLMSG_SPACE(sizeof(struct nfgenmsg
));
1163 struct nlattr
*cda
[IPSET_ATTR_CMD_MAX
+1];
1164 struct nlattr
*cmdattr
;
1167 skb2
= nlmsg_new(payload
, GFP_KERNEL
);
1170 rep
= __nlmsg_put(skb2
, NETLINK_CB(skb
).pid
,
1171 nlh
->nlmsg_seq
, NLMSG_ERROR
, payload
, 0);
1172 errmsg
= nlmsg_data(rep
);
1173 errmsg
->error
= ret
;
1174 memcpy(&errmsg
->msg
, nlh
, nlh
->nlmsg_len
);
1175 cmdattr
= (void *)&errmsg
->msg
+ min_len
;
1177 nla_parse(cda
, IPSET_ATTR_CMD_MAX
,
1178 cmdattr
, nlh
->nlmsg_len
- min_len
,
1181 errline
= nla_data(cda
[IPSET_ATTR_LINENO
]);
1185 netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1186 /* Signal netlink not to send its ACK/errmsg. */
1194 ip_set_uadd(struct sock
*ctnl
, struct sk_buff
*skb
,
1195 const struct nlmsghdr
*nlh
,
1196 const struct nlattr
* const attr
[])
1199 struct nlattr
*tb
[IPSET_ATTR_ADT_MAX
+1] = {};
1200 const struct nlattr
*nla
;
1201 u32 flags
= flag_exist(nlh
);
1205 if (unlikely(protocol_failed(attr
) ||
1206 attr
[IPSET_ATTR_SETNAME
] == NULL
||
1207 !((attr
[IPSET_ATTR_DATA
] != NULL
) ^
1208 (attr
[IPSET_ATTR_ADT
] != NULL
)) ||
1209 (attr
[IPSET_ATTR_DATA
] != NULL
&&
1210 !flag_nested(attr
[IPSET_ATTR_DATA
])) ||
1211 (attr
[IPSET_ATTR_ADT
] != NULL
&&
1212 (!flag_nested(attr
[IPSET_ATTR_ADT
]) ||
1213 attr
[IPSET_ATTR_LINENO
] == NULL
))))
1214 return -IPSET_ERR_PROTOCOL
;
1216 set
= find_set(nla_data(attr
[IPSET_ATTR_SETNAME
]));
1220 use_lineno
= !!attr
[IPSET_ATTR_LINENO
];
1221 if (attr
[IPSET_ATTR_DATA
]) {
1222 if (nla_parse_nested(tb
, IPSET_ATTR_ADT_MAX
,
1223 attr
[IPSET_ATTR_DATA
],
1224 set
->type
->adt_policy
))
1225 return -IPSET_ERR_PROTOCOL
;
1226 ret
= call_ad(ctnl
, skb
, set
, tb
, IPSET_ADD
, flags
,
1231 nla_for_each_nested(nla
, attr
[IPSET_ATTR_ADT
], nla_rem
) {
1232 memset(tb
, 0, sizeof(tb
));
1233 if (nla_type(nla
) != IPSET_ATTR_DATA
||
1234 !flag_nested(nla
) ||
1235 nla_parse_nested(tb
, IPSET_ATTR_ADT_MAX
, nla
,
1236 set
->type
->adt_policy
))
1237 return -IPSET_ERR_PROTOCOL
;
1238 ret
= call_ad(ctnl
, skb
, set
, tb
, IPSET_ADD
,
1248 ip_set_udel(struct sock
*ctnl
, struct sk_buff
*skb
,
1249 const struct nlmsghdr
*nlh
,
1250 const struct nlattr
* const attr
[])
1253 struct nlattr
*tb
[IPSET_ATTR_ADT_MAX
+1] = {};
1254 const struct nlattr
*nla
;
1255 u32 flags
= flag_exist(nlh
);
1259 if (unlikely(protocol_failed(attr
) ||
1260 attr
[IPSET_ATTR_SETNAME
] == NULL
||
1261 !((attr
[IPSET_ATTR_DATA
] != NULL
) ^
1262 (attr
[IPSET_ATTR_ADT
] != NULL
)) ||
1263 (attr
[IPSET_ATTR_DATA
] != NULL
&&
1264 !flag_nested(attr
[IPSET_ATTR_DATA
])) ||
1265 (attr
[IPSET_ATTR_ADT
] != NULL
&&
1266 (!flag_nested(attr
[IPSET_ATTR_ADT
]) ||
1267 attr
[IPSET_ATTR_LINENO
] == NULL
))))
1268 return -IPSET_ERR_PROTOCOL
;
1270 set
= find_set(nla_data(attr
[IPSET_ATTR_SETNAME
]));
1274 use_lineno
= !!attr
[IPSET_ATTR_LINENO
];
1275 if (attr
[IPSET_ATTR_DATA
]) {
1276 if (nla_parse_nested(tb
, IPSET_ATTR_ADT_MAX
,
1277 attr
[IPSET_ATTR_DATA
],
1278 set
->type
->adt_policy
))
1279 return -IPSET_ERR_PROTOCOL
;
1280 ret
= call_ad(ctnl
, skb
, set
, tb
, IPSET_DEL
, flags
,
1285 nla_for_each_nested(nla
, attr
[IPSET_ATTR_ADT
], nla_rem
) {
1286 memset(tb
, 0, sizeof(*tb
));
1287 if (nla_type(nla
) != IPSET_ATTR_DATA
||
1288 !flag_nested(nla
) ||
1289 nla_parse_nested(tb
, IPSET_ATTR_ADT_MAX
, nla
,
1290 set
->type
->adt_policy
))
1291 return -IPSET_ERR_PROTOCOL
;
1292 ret
= call_ad(ctnl
, skb
, set
, tb
, IPSET_DEL
,
1302 ip_set_utest(struct sock
*ctnl
, struct sk_buff
*skb
,
1303 const struct nlmsghdr
*nlh
,
1304 const struct nlattr
* const attr
[])
1307 struct nlattr
*tb
[IPSET_ATTR_ADT_MAX
+1] = {};
1310 if (unlikely(protocol_failed(attr
) ||
1311 attr
[IPSET_ATTR_SETNAME
] == NULL
||
1312 attr
[IPSET_ATTR_DATA
] == NULL
||
1313 !flag_nested(attr
[IPSET_ATTR_DATA
])))
1314 return -IPSET_ERR_PROTOCOL
;
1316 set
= find_set(nla_data(attr
[IPSET_ATTR_SETNAME
]));
1320 if (nla_parse_nested(tb
, IPSET_ATTR_ADT_MAX
, attr
[IPSET_ATTR_DATA
],
1321 set
->type
->adt_policy
))
1322 return -IPSET_ERR_PROTOCOL
;
1324 read_lock_bh(&set
->lock
);
1325 ret
= set
->variant
->uadt(set
, tb
, IPSET_TEST
, NULL
, 0);
1326 read_unlock_bh(&set
->lock
);
1327 /* Userspace can't trigger element to be re-added */
1331 return ret
< 0 ? ret
: ret
> 0 ? 0 : -IPSET_ERR_EXIST
;
1334 /* Get headed data of a set */
1337 ip_set_header(struct sock
*ctnl
, struct sk_buff
*skb
,
1338 const struct nlmsghdr
*nlh
,
1339 const struct nlattr
* const attr
[])
1341 const struct ip_set
*set
;
1342 struct sk_buff
*skb2
;
1343 struct nlmsghdr
*nlh2
;
1347 if (unlikely(protocol_failed(attr
) ||
1348 attr
[IPSET_ATTR_SETNAME
] == NULL
))
1349 return -IPSET_ERR_PROTOCOL
;
1351 index
= find_set_id(nla_data(attr
[IPSET_ATTR_SETNAME
]));
1352 if (index
== IPSET_INVALID_ID
)
1354 set
= ip_set_list
[index
];
1356 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1360 nlh2
= start_msg(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
, 0,
1364 NLA_PUT_U8(skb2
, IPSET_ATTR_PROTOCOL
, IPSET_PROTOCOL
);
1365 NLA_PUT_STRING(skb2
, IPSET_ATTR_SETNAME
, set
->name
);
1366 NLA_PUT_STRING(skb2
, IPSET_ATTR_TYPENAME
, set
->type
->name
);
1367 NLA_PUT_U8(skb2
, IPSET_ATTR_FAMILY
, set
->family
);
1368 NLA_PUT_U8(skb2
, IPSET_ATTR_REVISION
, set
->type
->revision
);
1369 nlmsg_end(skb2
, nlh2
);
1371 ret
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1378 nlmsg_cancel(skb2
, nlh2
);
1386 static const struct nla_policy ip_set_type_policy
[IPSET_ATTR_CMD_MAX
+ 1] = {
1387 [IPSET_ATTR_PROTOCOL
] = { .type
= NLA_U8
},
1388 [IPSET_ATTR_TYPENAME
] = { .type
= NLA_NUL_STRING
,
1389 .len
= IPSET_MAXNAMELEN
- 1 },
1390 [IPSET_ATTR_FAMILY
] = { .type
= NLA_U8
},
1394 ip_set_type(struct sock
*ctnl
, struct sk_buff
*skb
,
1395 const struct nlmsghdr
*nlh
,
1396 const struct nlattr
* const attr
[])
1398 struct sk_buff
*skb2
;
1399 struct nlmsghdr
*nlh2
;
1400 u8 family
, min
, max
;
1401 const char *typename
;
1404 if (unlikely(protocol_failed(attr
) ||
1405 attr
[IPSET_ATTR_TYPENAME
] == NULL
||
1406 attr
[IPSET_ATTR_FAMILY
] == NULL
))
1407 return -IPSET_ERR_PROTOCOL
;
1409 family
= nla_get_u8(attr
[IPSET_ATTR_FAMILY
]);
1410 typename
= nla_data(attr
[IPSET_ATTR_TYPENAME
]);
1411 ret
= find_set_type_minmax(typename
, family
, &min
, &max
);
1415 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1419 nlh2
= start_msg(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
, 0,
1423 NLA_PUT_U8(skb2
, IPSET_ATTR_PROTOCOL
, IPSET_PROTOCOL
);
1424 NLA_PUT_STRING(skb2
, IPSET_ATTR_TYPENAME
, typename
);
1425 NLA_PUT_U8(skb2
, IPSET_ATTR_FAMILY
, family
);
1426 NLA_PUT_U8(skb2
, IPSET_ATTR_REVISION
, max
);
1427 NLA_PUT_U8(skb2
, IPSET_ATTR_REVISION_MIN
, min
);
1428 nlmsg_end(skb2
, nlh2
);
1430 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2
->nlmsg_len
);
1431 ret
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1438 nlmsg_cancel(skb2
, nlh2
);
1444 /* Get protocol version */
1446 static const struct nla_policy
1447 ip_set_protocol_policy
[IPSET_ATTR_CMD_MAX
+ 1] = {
1448 [IPSET_ATTR_PROTOCOL
] = { .type
= NLA_U8
},
1452 ip_set_protocol(struct sock
*ctnl
, struct sk_buff
*skb
,
1453 const struct nlmsghdr
*nlh
,
1454 const struct nlattr
* const attr
[])
1456 struct sk_buff
*skb2
;
1457 struct nlmsghdr
*nlh2
;
1460 if (unlikely(attr
[IPSET_ATTR_PROTOCOL
] == NULL
))
1461 return -IPSET_ERR_PROTOCOL
;
1463 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1467 nlh2
= start_msg(skb2
, NETLINK_CB(skb
).pid
, nlh
->nlmsg_seq
, 0,
1468 IPSET_CMD_PROTOCOL
);
1471 NLA_PUT_U8(skb2
, IPSET_ATTR_PROTOCOL
, IPSET_PROTOCOL
);
1472 nlmsg_end(skb2
, nlh2
);
1474 ret
= netlink_unicast(ctnl
, skb2
, NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1481 nlmsg_cancel(skb2
, nlh2
);
1487 static const struct nfnl_callback ip_set_netlink_subsys_cb
[IPSET_MSG_MAX
] = {
1488 [IPSET_CMD_CREATE
] = {
1489 .call
= ip_set_create
,
1490 .attr_count
= IPSET_ATTR_CMD_MAX
,
1491 .policy
= ip_set_create_policy
,
1493 [IPSET_CMD_DESTROY
] = {
1494 .call
= ip_set_destroy
,
1495 .attr_count
= IPSET_ATTR_CMD_MAX
,
1496 .policy
= ip_set_setname_policy
,
1498 [IPSET_CMD_FLUSH
] = {
1499 .call
= ip_set_flush
,
1500 .attr_count
= IPSET_ATTR_CMD_MAX
,
1501 .policy
= ip_set_setname_policy
,
1503 [IPSET_CMD_RENAME
] = {
1504 .call
= ip_set_rename
,
1505 .attr_count
= IPSET_ATTR_CMD_MAX
,
1506 .policy
= ip_set_setname2_policy
,
1508 [IPSET_CMD_SWAP
] = {
1509 .call
= ip_set_swap
,
1510 .attr_count
= IPSET_ATTR_CMD_MAX
,
1511 .policy
= ip_set_setname2_policy
,
1513 [IPSET_CMD_LIST
] = {
1514 .call
= ip_set_dump
,
1515 .attr_count
= IPSET_ATTR_CMD_MAX
,
1516 .policy
= ip_set_setname_policy
,
1518 [IPSET_CMD_SAVE
] = {
1519 .call
= ip_set_dump
,
1520 .attr_count
= IPSET_ATTR_CMD_MAX
,
1521 .policy
= ip_set_setname_policy
,
1524 .call
= ip_set_uadd
,
1525 .attr_count
= IPSET_ATTR_CMD_MAX
,
1526 .policy
= ip_set_adt_policy
,
1529 .call
= ip_set_udel
,
1530 .attr_count
= IPSET_ATTR_CMD_MAX
,
1531 .policy
= ip_set_adt_policy
,
1533 [IPSET_CMD_TEST
] = {
1534 .call
= ip_set_utest
,
1535 .attr_count
= IPSET_ATTR_CMD_MAX
,
1536 .policy
= ip_set_adt_policy
,
1538 [IPSET_CMD_HEADER
] = {
1539 .call
= ip_set_header
,
1540 .attr_count
= IPSET_ATTR_CMD_MAX
,
1541 .policy
= ip_set_setname_policy
,
1543 [IPSET_CMD_TYPE
] = {
1544 .call
= ip_set_type
,
1545 .attr_count
= IPSET_ATTR_CMD_MAX
,
1546 .policy
= ip_set_type_policy
,
1548 [IPSET_CMD_PROTOCOL
] = {
1549 .call
= ip_set_protocol
,
1550 .attr_count
= IPSET_ATTR_CMD_MAX
,
1551 .policy
= ip_set_protocol_policy
,
1555 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly
= {
1557 .subsys_id
= NFNL_SUBSYS_IPSET
,
1558 .cb_count
= IPSET_MSG_MAX
,
1559 .cb
= ip_set_netlink_subsys_cb
,
1562 /* Interface to iptables/ip6tables */
1565 ip_set_sockfn_get(struct sock
*sk
, int optval
, void __user
*user
, int *len
)
1569 int copylen
= *len
, ret
= 0;
1571 if (!capable(CAP_NET_ADMIN
))
1573 if (optval
!= SO_IP_SET
)
1575 if (*len
< sizeof(unsigned))
1578 data
= vmalloc(*len
);
1581 if (copy_from_user(data
, user
, *len
) != 0) {
1585 op
= (unsigned *) data
;
1587 if (*op
< IP_SET_OP_VERSION
) {
1588 /* Check the version at the beginning of operations */
1589 struct ip_set_req_version
*req_version
= data
;
1590 if (req_version
->version
!= IPSET_PROTOCOL
) {
1597 case IP_SET_OP_VERSION
: {
1598 struct ip_set_req_version
*req_version
= data
;
1600 if (*len
!= sizeof(struct ip_set_req_version
)) {
1605 req_version
->version
= IPSET_PROTOCOL
;
1606 ret
= copy_to_user(user
, req_version
,
1607 sizeof(struct ip_set_req_version
));
1610 case IP_SET_OP_GET_BYNAME
: {
1611 struct ip_set_req_get_set
*req_get
= data
;
1613 if (*len
!= sizeof(struct ip_set_req_get_set
)) {
1617 req_get
->set
.name
[IPSET_MAXNAMELEN
- 1] = '\0';
1619 req_get
->set
.index
= find_set_id(req_get
->set
.name
);
1623 case IP_SET_OP_GET_BYINDEX
: {
1624 struct ip_set_req_get_set
*req_get
= data
;
1626 if (*len
!= sizeof(struct ip_set_req_get_set
) ||
1627 req_get
->set
.index
>= ip_set_max
) {
1632 strncpy(req_get
->set
.name
,
1633 ip_set_list
[req_get
->set
.index
]
1634 ? ip_set_list
[req_get
->set
.index
]->name
: "",
1642 } /* end of switch(op) */
1645 ret
= copy_to_user(user
, data
, copylen
);
1654 static struct nf_sockopt_ops so_set __read_mostly
= {
1656 .get_optmin
= SO_IP_SET
,
1657 .get_optmax
= SO_IP_SET
+ 1,
1658 .get
= &ip_set_sockfn_get
,
1659 .owner
= THIS_MODULE
,
1668 ip_set_max
= max_sets
;
1669 if (ip_set_max
>= IPSET_INVALID_ID
)
1670 ip_set_max
= IPSET_INVALID_ID
- 1;
1672 ip_set_list
= kzalloc(sizeof(struct ip_set
*) * ip_set_max
,
1675 pr_err("ip_set: Unable to create ip_set_list\n");
1679 ret
= nfnetlink_subsys_register(&ip_set_netlink_subsys
);
1681 pr_err("ip_set: cannot register with nfnetlink.\n");
1685 ret
= nf_register_sockopt(&so_set
);
1687 pr_err("SO_SET registry failed: %d\n", ret
);
1688 nfnetlink_subsys_unregister(&ip_set_netlink_subsys
);
1693 pr_notice("ip_set: protocol %u\n", IPSET_PROTOCOL
);
1700 /* There can't be any existing set */
1701 nf_unregister_sockopt(&so_set
);
1702 nfnetlink_subsys_unregister(&ip_set_netlink_subsys
);
1704 pr_debug("these are the famous last words\n");
1707 module_init(ip_set_init
);
1708 module_exit(ip_set_fini
);