netfilter: ipset: fix race condition between swap/destroy and kernel side add/del...
[linux-block.git] / net / netfilter / ipset / ip_set_core.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
a7b4f989
JK
2/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3 * Patrick Schaaf <bof@bof.de>
fe03d474 4 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
a7b4f989
JK
5 */
6
7/* Kernel module for IP set management */
8
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/ip.h>
13#include <linux/skbuff.h>
14#include <linux/spinlock.h>
a7b4f989 15#include <linux/rculist.h>
a7b4f989 16#include <net/netlink.h>
1785e8f4
VL
17#include <net/net_namespace.h>
18#include <net/netns/generic.h>
a7b4f989
JK
19
20#include <linux/netfilter.h>
b66554cf 21#include <linux/netfilter/x_tables.h>
a7b4f989
JK
22#include <linux/netfilter/nfnetlink.h>
23#include <linux/netfilter/ipset/ip_set.h>
24
25static LIST_HEAD(ip_set_type_list); /* all registered set types */
26static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */
2f9f28b2 27static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */
a7b4f989 28
1785e8f4
VL
29struct ip_set_net {
30 struct ip_set * __rcu *ip_set_list; /* all individual sets */
31 ip_set_id_t ip_set_max; /* max number of sets */
9c1ba5c8
JK
32 bool is_deleted; /* deleted by ip_set_net_exit */
33 bool is_destroyed; /* all sets are destroyed */
1785e8f4 34};
ca0f6a5c 35
c7d03a00 36static unsigned int ip_set_net_id __read_mostly;
1785e8f4 37
8dea982a 38static struct ip_set_net *ip_set_pernet(struct net *net)
1785e8f4
VL
39{
40 return net_generic(net, ip_set_net_id);
41}
a7b4f989 42
9076aea7 43#define IP_SET_INC 64
22496f09 44#define STRNCMP(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
a7b4f989
JK
45
46static unsigned int max_sets;
47
48module_param(max_sets, int, 0600);
49MODULE_PARM_DESC(max_sets, "maximal number of sets");
50MODULE_LICENSE("GPL");
fe03d474 51MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
a7b4f989
JK
52MODULE_DESCRIPTION("core IP set support");
53MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
54
8a02bdd5 55/* When the nfnl mutex or ip_set_ref_lock is held: */
3e90ebd3 56#define ip_set_dereference(p) \
8a02bdd5
JK
57 rcu_dereference_protected(p, \
58 lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET) || \
59 lockdep_is_held(&ip_set_ref_lock))
3e90ebd3
PM
60#define ip_set(inst, id) \
61 ip_set_dereference((inst)->ip_set_list)[id]
8a02bdd5
JK
62#define ip_set_ref_netlink(inst,id) \
63 rcu_dereference_raw((inst)->ip_set_list)[id]
28628fa9
JK
64#define ip_set_dereference_nfnl(p) \
65 rcu_dereference_check(p, lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET))
9076aea7 66
ca0f6a5c 67/* The set types are implemented in modules and registered set types
a7b4f989
JK
68 * can be found in ip_set_type_list. Adding/deleting types is
69 * serialized by ip_set_type_mutex.
70 */
71
8dea982a 72static void
a7b4f989
JK
73ip_set_type_lock(void)
74{
75 mutex_lock(&ip_set_type_mutex);
76}
77
8dea982a 78static void
a7b4f989
JK
79ip_set_type_unlock(void)
80{
81 mutex_unlock(&ip_set_type_mutex);
82}
83
84/* Register and deregister settype */
85
86static struct ip_set_type *
87find_set_type(const char *name, u8 family, u8 revision)
88{
89 struct ip_set_type *type;
90
b135fc08
AG
91 list_for_each_entry_rcu(type, &ip_set_type_list, list,
92 lockdep_is_held(&ip_set_type_mutex))
22496f09 93 if (STRNCMP(type->name, name) &&
3ace95c0
JK
94 (type->family == family ||
95 type->family == NFPROTO_UNSPEC) &&
f1e00b39
JK
96 revision >= type->revision_min &&
97 revision <= type->revision_max)
a7b4f989
JK
98 return type;
99 return NULL;
100}
101
102/* Unlock, try to load a set type module and lock again */
088067f4
JK
103static bool
104load_settype(const char *name)
a7b4f989 105{
c14b78e7 106 nfnl_unlock(NFNL_SUBSYS_IPSET);
a7b4f989
JK
107 pr_debug("try to load ip_set_%s\n", name);
108 if (request_module("ip_set_%s", name) < 0) {
b167a37c 109 pr_warn("Can't find ip_set type %s\n", name);
c14b78e7 110 nfnl_lock(NFNL_SUBSYS_IPSET);
088067f4 111 return false;
a7b4f989 112 }
c14b78e7 113 nfnl_lock(NFNL_SUBSYS_IPSET);
088067f4 114 return true;
a7b4f989
JK
115}
116
117/* Find a set type and reference it */
088067f4
JK
118#define find_set_type_get(name, family, revision, found) \
119 __find_set_type_get(name, family, revision, found, false)
120
a7b4f989 121static int
088067f4
JK
122__find_set_type_get(const char *name, u8 family, u8 revision,
123 struct ip_set_type **found, bool retry)
a7b4f989 124{
5c1aba46
JK
125 struct ip_set_type *type;
126 int err;
127
088067f4
JK
128 if (retry && !load_settype(name))
129 return -IPSET_ERR_FIND_TYPE;
130
a7b4f989
JK
131 rcu_read_lock();
132 *found = find_set_type(name, family, revision);
133 if (*found) {
5c1aba46
JK
134 err = !try_module_get((*found)->me) ? -EFAULT : 0;
135 goto unlock;
a7b4f989 136 }
088067f4 137 /* Make sure the type is already loaded
ca0f6a5c
JK
138 * but we don't support the revision
139 */
5c1aba46 140 list_for_each_entry_rcu(type, &ip_set_type_list, list)
22496f09 141 if (STRNCMP(type->name, name)) {
5c1aba46
JK
142 err = -IPSET_ERR_FIND_TYPE;
143 goto unlock;
144 }
a7b4f989
JK
145 rcu_read_unlock();
146
088067f4
JK
147 return retry ? -IPSET_ERR_FIND_TYPE :
148 __find_set_type_get(name, family, revision, found, true);
5c1aba46
JK
149
150unlock:
151 rcu_read_unlock();
152 return err;
a7b4f989
JK
153}
154
155/* Find a given set type by name and family.
156 * If we succeeded, the supported minimal and maximum revisions are
157 * filled out.
158 */
088067f4
JK
159#define find_set_type_minmax(name, family, min, max) \
160 __find_set_type_minmax(name, family, min, max, false)
161
a7b4f989 162static int
088067f4
JK
163__find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max,
164 bool retry)
a7b4f989
JK
165{
166 struct ip_set_type *type;
167 bool found = false;
168
088067f4
JK
169 if (retry && !load_settype(name))
170 return -IPSET_ERR_FIND_TYPE;
171
5c1aba46 172 *min = 255; *max = 0;
a7b4f989
JK
173 rcu_read_lock();
174 list_for_each_entry_rcu(type, &ip_set_type_list, list)
22496f09 175 if (STRNCMP(type->name, name) &&
3ace95c0
JK
176 (type->family == family ||
177 type->family == NFPROTO_UNSPEC)) {
a7b4f989 178 found = true;
f1e00b39
JK
179 if (type->revision_min < *min)
180 *min = type->revision_min;
181 if (type->revision_max > *max)
182 *max = type->revision_max;
a7b4f989
JK
183 }
184 rcu_read_unlock();
185 if (found)
186 return 0;
187
088067f4
JK
188 return retry ? -IPSET_ERR_FIND_TYPE :
189 __find_set_type_minmax(name, family, min, max, true);
a7b4f989
JK
190}
191
c15f1c83
JE
192#define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \
193 (f) == NFPROTO_IPV6 ? "inet6" : "any")
a7b4f989
JK
194
195/* Register a set type structure. The type is identified by
196 * the unique triple of name, family and revision.
197 */
198int
199ip_set_type_register(struct ip_set_type *type)
200{
201 int ret = 0;
202
203 if (type->protocol != IPSET_PROTOCOL) {
b167a37c
JP
204 pr_warn("ip_set type %s, family %s, revision %u:%u uses wrong protocol version %u (want %u)\n",
205 type->name, family_name(type->family),
206 type->revision_min, type->revision_max,
207 type->protocol, IPSET_PROTOCOL);
a7b4f989
JK
208 return -EINVAL;
209 }
210
211 ip_set_type_lock();
f1e00b39 212 if (find_set_type(type->name, type->family, type->revision_min)) {
a7b4f989 213 /* Duplicate! */
b167a37c
JP
214 pr_warn("ip_set type %s, family %s with revision min %u already registered!\n",
215 type->name, family_name(type->family),
216 type->revision_min);
b57b2d1f
JK
217 ip_set_type_unlock();
218 return -EINVAL;
a7b4f989
JK
219 }
220 list_add_rcu(&type->list, &ip_set_type_list);
f1e00b39
JK
221 pr_debug("type %s, family %s, revision %u:%u registered.\n",
222 type->name, family_name(type->family),
223 type->revision_min, type->revision_max);
a7b4f989 224 ip_set_type_unlock();
b57b2d1f 225
a7b4f989
JK
226 return ret;
227}
228EXPORT_SYMBOL_GPL(ip_set_type_register);
229
230/* Unregister a set type. There's a small race with ip_set_create */
231void
232ip_set_type_unregister(struct ip_set_type *type)
233{
234 ip_set_type_lock();
f1e00b39 235 if (!find_set_type(type->name, type->family, type->revision_min)) {
b167a37c
JP
236 pr_warn("ip_set type %s, family %s with revision min %u not registered\n",
237 type->name, family_name(type->family),
238 type->revision_min);
b57b2d1f
JK
239 ip_set_type_unlock();
240 return;
a7b4f989
JK
241 }
242 list_del_rcu(&type->list);
f1e00b39
JK
243 pr_debug("type %s, family %s with revision min %u unregistered.\n",
244 type->name, family_name(type->family), type->revision_min);
a7b4f989
JK
245 ip_set_type_unlock();
246
247 synchronize_rcu();
248}
249EXPORT_SYMBOL_GPL(ip_set_type_unregister);
250
251/* Utility functions */
252void *
253ip_set_alloc(size_t size)
254{
9446ab34 255 return kvzalloc(size, GFP_KERNEL_ACCOUNT);
a7b4f989
JK
256}
257EXPORT_SYMBOL_GPL(ip_set_alloc);
258
259void
260ip_set_free(void *members)
261{
262 pr_debug("%p: free with %s\n", members,
263 is_vmalloc_addr(members) ? "vfree" : "kfree");
4cb28970 264 kvfree(members);
a7b4f989
JK
265}
266EXPORT_SYMBOL_GPL(ip_set_free);
267
8dea982a 268static bool
a7b4f989
JK
269flag_nested(const struct nlattr *nla)
270{
271 return nla->nla_type & NLA_F_NESTED;
272}
273
274static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
275 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
68ad89de 276 [IPSET_ATTR_IPADDR_IPV6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
a7b4f989
JK
277};
278
279int
280ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
281{
ca0f6a5c 282 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
a7b4f989
JK
283
284 if (unlikely(!flag_nested(nla)))
285 return -IPSET_ERR_PROTOCOL;
12899756
JK
286 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla,
287 ipaddr_policy, NULL))
a7b4f989
JK
288 return -IPSET_ERR_PROTOCOL;
289 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
290 return -IPSET_ERR_PROTOCOL;
291
292 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
293 return 0;
294}
295EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
296
297int
298ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
299{
ca0f6a5c 300 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
a7b4f989
JK
301
302 if (unlikely(!flag_nested(nla)))
303 return -IPSET_ERR_PROTOCOL;
304
12899756
JK
305 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla,
306 ipaddr_policy, NULL))
a7b4f989
JK
307 return -IPSET_ERR_PROTOCOL;
308 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
309 return -IPSET_ERR_PROTOCOL;
310
311 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
ca0f6a5c 312 sizeof(struct in6_addr));
a7b4f989
JK
313 return 0;
314}
315EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
316
2398a976
JS
317static u32
318ip_set_timeout_get(const unsigned long *timeout)
319{
320 u32 t;
321
322 if (*timeout == IPSET_ELEM_PERMANENT)
323 return 0;
324
325 t = jiffies_to_msecs(*timeout - jiffies) / MSEC_PER_SEC;
326 /* Zero value in userspace means no timeout */
327 return t == 0 ? 1 : t;
328}
329
94177f6e
JS
330static char *
331ip_set_comment_uget(struct nlattr *tb)
332{
333 return nla_data(tb);
334}
335
336/* Called from uadd only, protected by the set spinlock.
337 * The kadt functions don't use the comment extensions in any way.
338 */
339void
340ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
341 const struct ip_set_ext *ext)
342{
343 struct ip_set_comment_rcu *c = rcu_dereference_protected(comment->c, 1);
344 size_t len = ext->comment ? strlen(ext->comment) : 0;
345
346 if (unlikely(c)) {
347 set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
348 kfree_rcu(c, rcu);
349 rcu_assign_pointer(comment->c, NULL);
350 }
351 if (!len)
352 return;
353 if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
354 len = IPSET_MAX_COMMENT_SIZE;
355 c = kmalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
356 if (unlikely(!c))
357 return;
8556bceb 358 strscpy(c->str, ext->comment, len + 1);
94177f6e
JS
359 set->ext_size += sizeof(*c) + strlen(c->str) + 1;
360 rcu_assign_pointer(comment->c, c);
361}
362EXPORT_SYMBOL_GPL(ip_set_init_comment);
363
364/* Used only when dumping a set, protected by rcu_read_lock() */
365static int
366ip_set_put_comment(struct sk_buff *skb, const struct ip_set_comment *comment)
367{
368 struct ip_set_comment_rcu *c = rcu_dereference(comment->c);
369
370 if (!c)
371 return 0;
372 return nla_put_string(skb, IPSET_ATTR_COMMENT, c->str);
373}
374
375/* Called from uadd/udel, flush or the garbage collectors protected
376 * by the set spinlock.
377 * Called when the set is destroyed and when there can't be any user
378 * of the set data anymore.
379 */
380static void
381ip_set_comment_free(struct ip_set *set, void *ptr)
382{
383 struct ip_set_comment *comment = ptr;
384 struct ip_set_comment_rcu *c;
385
386 c = rcu_dereference_protected(comment->c, 1);
387 if (unlikely(!c))
388 return;
389 set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
390 kfree_rcu(c, rcu);
391 rcu_assign_pointer(comment->c, NULL);
392}
393
9e41f26a 394typedef void (*destroyer)(struct ip_set *, void *);
03c8b234
JK
395/* ipset data extension types, in size order */
396
397const struct ip_set_ext_type ip_set_extensions[] = {
398 [IPSET_EXT_ID_COUNTER] = {
399 .type = IPSET_EXT_COUNTER,
400 .flag = IPSET_FLAG_WITH_COUNTERS,
401 .len = sizeof(struct ip_set_counter),
402 .align = __alignof__(struct ip_set_counter),
403 },
404 [IPSET_EXT_ID_TIMEOUT] = {
405 .type = IPSET_EXT_TIMEOUT,
406 .len = sizeof(unsigned long),
407 .align = __alignof__(unsigned long),
408 },
0e9871e3
AD
409 [IPSET_EXT_ID_SKBINFO] = {
410 .type = IPSET_EXT_SKBINFO,
411 .flag = IPSET_FLAG_WITH_SKBINFO,
412 .len = sizeof(struct ip_set_skbinfo),
413 .align = __alignof__(struct ip_set_skbinfo),
414 },
68b63f08
OS
415 [IPSET_EXT_ID_COMMENT] = {
416 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
417 .flag = IPSET_FLAG_WITH_COMMENT,
418 .len = sizeof(struct ip_set_comment),
419 .align = __alignof__(struct ip_set_comment),
94177f6e 420 .destroy = ip_set_comment_free,
68b63f08 421 },
03c8b234
JK
422};
423EXPORT_SYMBOL_GPL(ip_set_extensions);
424
8dea982a 425static bool
03c8b234
JK
426add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
427{
428 return ip_set_extensions[id].flag ?
429 (flags & ip_set_extensions[id].flag) :
430 !!tb[IPSET_ATTR_TIMEOUT];
431}
432
433size_t
95ad1f4a
JK
434ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len,
435 size_t align)
03c8b234
JK
436{
437 enum ip_set_ext_id id;
03c8b234
JK
438 u32 cadt_flags = 0;
439
440 if (tb[IPSET_ATTR_CADT_FLAGS])
441 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
07cf8f5a
JH
442 if (cadt_flags & IPSET_FLAG_WITH_FORCEADD)
443 set->flags |= IPSET_CREATE_FLAG_FORCEADD;
95ad1f4a
JK
444 if (!align)
445 align = 1;
03c8b234
JK
446 for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
447 if (!add_extension(id, cadt_flags, tb))
448 continue;
71502846
RK
449 if (align < ip_set_extensions[id].align)
450 align = ip_set_extensions[id].align;
95ad1f4a
JK
451 len = ALIGN(len, ip_set_extensions[id].align);
452 set->offset[id] = len;
03c8b234 453 set->extensions |= ip_set_extensions[id].type;
95ad1f4a 454 len += ip_set_extensions[id].len;
03c8b234 455 }
95ad1f4a 456 return ALIGN(len, align);
03c8b234
JK
457}
458EXPORT_SYMBOL_GPL(ip_set_elem_len);
459
075e64c0
JK
460int
461ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
462 struct ip_set_ext *ext)
463{
0e9871e3 464 u64 fullmark;
7dd37bc8
SP
465
466 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
467 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
468 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
469 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
470 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
471 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
472 return -IPSET_ERR_PROTOCOL;
473
075e64c0 474 if (tb[IPSET_ATTR_TIMEOUT]) {
edda0791 475 if (!SET_WITH_TIMEOUT(set))
075e64c0
JK
476 return -IPSET_ERR_TIMEOUT;
477 ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
478 }
34d666d4 479 if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) {
edda0791 480 if (!SET_WITH_COUNTER(set))
34d666d4
JK
481 return -IPSET_ERR_COUNTER;
482 if (tb[IPSET_ATTR_BYTES])
483 ext->bytes = be64_to_cpu(nla_get_be64(
484 tb[IPSET_ATTR_BYTES]));
485 if (tb[IPSET_ATTR_PACKETS])
486 ext->packets = be64_to_cpu(nla_get_be64(
487 tb[IPSET_ATTR_PACKETS]));
488 }
68b63f08 489 if (tb[IPSET_ATTR_COMMENT]) {
edda0791 490 if (!SET_WITH_COMMENT(set))
68b63f08
OS
491 return -IPSET_ERR_COMMENT;
492 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
493 }
0e9871e3 494 if (tb[IPSET_ATTR_SKBMARK]) {
edda0791 495 if (!SET_WITH_SKBINFO(set))
0e9871e3
AD
496 return -IPSET_ERR_SKBINFO;
497 fullmark = be64_to_cpu(nla_get_be64(tb[IPSET_ATTR_SKBMARK]));
bec810d9
JK
498 ext->skbinfo.skbmark = fullmark >> 32;
499 ext->skbinfo.skbmarkmask = fullmark & 0xffffffff;
0e9871e3
AD
500 }
501 if (tb[IPSET_ATTR_SKBPRIO]) {
edda0791 502 if (!SET_WITH_SKBINFO(set))
0e9871e3 503 return -IPSET_ERR_SKBINFO;
bec810d9
JK
504 ext->skbinfo.skbprio =
505 be32_to_cpu(nla_get_be32(tb[IPSET_ATTR_SKBPRIO]));
0e9871e3
AD
506 }
507 if (tb[IPSET_ATTR_SKBQUEUE]) {
edda0791 508 if (!SET_WITH_SKBINFO(set))
0e9871e3 509 return -IPSET_ERR_SKBINFO;
bec810d9
JK
510 ext->skbinfo.skbqueue =
511 be16_to_cpu(nla_get_be16(tb[IPSET_ATTR_SKBQUEUE]));
0e9871e3 512 }
075e64c0
JK
513 return 0;
514}
515EXPORT_SYMBOL_GPL(ip_set_get_extensions);
516
2398a976
JS
517static u64
518ip_set_get_bytes(const struct ip_set_counter *counter)
519{
520 return (u64)atomic64_read(&(counter)->bytes);
521}
522
523static u64
524ip_set_get_packets(const struct ip_set_counter *counter)
525{
526 return (u64)atomic64_read(&(counter)->packets);
527}
528
529static bool
530ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
531{
532 return nla_put_net64(skb, IPSET_ATTR_BYTES,
533 cpu_to_be64(ip_set_get_bytes(counter)),
534 IPSET_ATTR_PAD) ||
535 nla_put_net64(skb, IPSET_ATTR_PACKETS,
536 cpu_to_be64(ip_set_get_packets(counter)),
537 IPSET_ATTR_PAD);
538}
539
540static bool
541ip_set_put_skbinfo(struct sk_buff *skb, const struct ip_set_skbinfo *skbinfo)
542{
543 /* Send nonzero parameters only */
544 return ((skbinfo->skbmark || skbinfo->skbmarkmask) &&
545 nla_put_net64(skb, IPSET_ATTR_SKBMARK,
546 cpu_to_be64((u64)skbinfo->skbmark << 32 |
547 skbinfo->skbmarkmask),
548 IPSET_ATTR_PAD)) ||
549 (skbinfo->skbprio &&
550 nla_put_net32(skb, IPSET_ATTR_SKBPRIO,
551 cpu_to_be32(skbinfo->skbprio))) ||
552 (skbinfo->skbqueue &&
553 nla_put_net16(skb, IPSET_ATTR_SKBQUEUE,
554 cpu_to_be16(skbinfo->skbqueue)));
555}
556
a3b1c1eb
DV
557int
558ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
559 const void *e, bool active)
560{
561 if (SET_WITH_TIMEOUT(set)) {
562 unsigned long *timeout = ext_timeout(e, set);
563
564 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
565 htonl(active ? ip_set_timeout_get(timeout)
566 : *timeout)))
567 return -EMSGSIZE;
568 }
569 if (SET_WITH_COUNTER(set) &&
570 ip_set_put_counter(skb, ext_counter(e, set)))
571 return -EMSGSIZE;
572 if (SET_WITH_COMMENT(set) &&
573 ip_set_put_comment(skb, ext_comment(e, set)))
574 return -EMSGSIZE;
575 if (SET_WITH_SKBINFO(set) &&
576 ip_set_put_skbinfo(skb, ext_skbinfo(e, set)))
577 return -EMSGSIZE;
578 return 0;
579}
580EXPORT_SYMBOL_GPL(ip_set_put_extensions);
581
2398a976
JS
582static bool
583ip_set_match_counter(u64 counter, u64 match, u8 op)
584{
585 switch (op) {
586 case IPSET_COUNTER_NONE:
587 return true;
588 case IPSET_COUNTER_EQ:
589 return counter == match;
590 case IPSET_COUNTER_NE:
591 return counter != match;
592 case IPSET_COUNTER_LT:
593 return counter < match;
594 case IPSET_COUNTER_GT:
595 return counter > match;
596 }
597 return false;
598}
599
600static void
601ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
602{
603 atomic64_add((long long)bytes, &(counter)->bytes);
604}
605
606static void
607ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
608{
609 atomic64_add((long long)packets, &(counter)->packets);
610}
611
612static void
613ip_set_update_counter(struct ip_set_counter *counter,
614 const struct ip_set_ext *ext, u32 flags)
615{
616 if (ext->packets != ULLONG_MAX &&
617 !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
618 ip_set_add_bytes(ext->bytes, counter);
619 ip_set_add_packets(ext->packets, counter);
620 }
621}
622
623static void
624ip_set_get_skbinfo(struct ip_set_skbinfo *skbinfo,
625 const struct ip_set_ext *ext,
626 struct ip_set_ext *mext, u32 flags)
627{
628 mext->skbinfo = *skbinfo;
629}
630
4750005a
JK
631bool
632ip_set_match_extensions(struct ip_set *set, const struct ip_set_ext *ext,
633 struct ip_set_ext *mext, u32 flags, void *data)
634{
635 if (SET_WITH_TIMEOUT(set) &&
636 ip_set_timeout_expired(ext_timeout(data, set)))
637 return false;
638 if (SET_WITH_COUNTER(set)) {
639 struct ip_set_counter *counter = ext_counter(data, set);
640
7d10e62c
SB
641 ip_set_update_counter(counter, ext, flags);
642
4750005a
JK
643 if (flags & IPSET_FLAG_MATCH_COUNTERS &&
644 !(ip_set_match_counter(ip_set_get_packets(counter),
645 mext->packets, mext->packets_op) &&
646 ip_set_match_counter(ip_set_get_bytes(counter),
647 mext->bytes, mext->bytes_op)))
648 return false;
4750005a
JK
649 }
650 if (SET_WITH_SKBINFO(set))
651 ip_set_get_skbinfo(ext_skbinfo(data, set),
652 ext, mext, flags);
653 return true;
654}
655EXPORT_SYMBOL_GPL(ip_set_match_extensions);
656
ca0f6a5c 657/* Creating/destroying/renaming/swapping affect the existence and
a7b4f989
JK
658 * the properties of a set. All of these can be executed from userspace
659 * only and serialized by the nfnl mutex indirectly from nfnetlink.
660 *
661 * Sets are identified by their index in ip_set_list and the index
662 * is used by the external references (set/SET netfilter modules).
663 *
664 * The set behind an index may change by swapping only, from userspace.
665 */
666
8dea982a 667static void
9076aea7 668__ip_set_get(struct ip_set *set)
a7b4f989 669{
2f9f28b2 670 write_lock_bh(&ip_set_ref_lock);
9076aea7 671 set->ref++;
2f9f28b2 672 write_unlock_bh(&ip_set_ref_lock);
a7b4f989
JK
673}
674
8dea982a 675static void
9076aea7 676__ip_set_put(struct ip_set *set)
a7b4f989 677{
2f9f28b2 678 write_lock_bh(&ip_set_ref_lock);
9076aea7
JK
679 BUG_ON(set->ref == 0);
680 set->ref--;
2f9f28b2 681 write_unlock_bh(&ip_set_ref_lock);
a7b4f989
JK
682}
683
596cf3fe
VP
684/* set->ref can be swapped out by ip_set_swap, netlink events (like dump) need
685 * a separate reference counter
686 */
7433b6d2
JK
687static void
688__ip_set_get_netlink(struct ip_set *set)
689{
690 write_lock_bh(&ip_set_ref_lock);
691 set->ref_netlink++;
692 write_unlock_bh(&ip_set_ref_lock);
693}
694
8dea982a 695static void
596cf3fe
VP
696__ip_set_put_netlink(struct ip_set *set)
697{
698 write_lock_bh(&ip_set_ref_lock);
699 BUG_ON(set->ref_netlink == 0);
700 set->ref_netlink--;
701 write_unlock_bh(&ip_set_ref_lock);
702}
703
ca0f6a5c 704/* Add, del and test set entries from kernel.
a7b4f989
JK
705 *
706 * The set behind the index must exist and must be referenced
707 * so it can't be destroyed (or changed) under our foot.
708 */
709
8dea982a 710static struct ip_set *
1785e8f4 711ip_set_rcu_get(struct net *net, ip_set_id_t index)
9076aea7 712{
1785e8f4 713 struct ip_set_net *inst = ip_set_pernet(net);
9076aea7 714
28628fa9
JK
715 /* ip_set_list and the set pointer need to be protected */
716 return ip_set_dereference_nfnl(inst->ip_set_list)[index];
9076aea7
JK
717}
718
f66ee041
JK
719static inline void
720ip_set_lock(struct ip_set *set)
721{
722 if (!set->variant->region_lock)
723 spin_lock_bh(&set->lock);
724}
725
726static inline void
727ip_set_unlock(struct ip_set *set)
728{
729 if (!set->variant->region_lock)
730 spin_unlock_bh(&set->lock);
731}
732
a7b4f989
JK
733int
734ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
075e64c0 735 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
a7b4f989 736{
613dbd95 737 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
a7b4f989
JK
738 int ret = 0;
739
ca0f6a5c 740 BUG_ON(!set);
a7b4f989
JK
741 pr_debug("set %s, index %u\n", set->name, index);
742
ac8cc925 743 if (opt->dim < set->type->dimension ||
c15f1c83 744 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
a7b4f989
JK
745 return 0;
746
b66554cf 747 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
a7b4f989
JK
748
749 if (ret == -EAGAIN) {
750 /* Type requests element to be completed */
1a84db56 751 pr_debug("element must be completed, ADD is triggered\n");
f66ee041 752 ip_set_lock(set);
b66554cf 753 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
f66ee041 754 ip_set_unlock(set);
a7b4f989 755 ret = 1;
3e0304a5
JK
756 } else {
757 /* --return-nomatch: invert matched element */
6e01781d 758 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
3e0304a5
JK
759 (set->type->features & IPSET_TYPE_NOMATCH) &&
760 (ret > 0 || ret == -ENOTEMPTY))
761 ret = -ret;
a7b4f989
JK
762 }
763
764 /* Convert error codes to nomatch */
765 return (ret < 0 ? 0 : ret);
766}
767EXPORT_SYMBOL_GPL(ip_set_test);
768
769int
770ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
075e64c0 771 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
a7b4f989 772{
613dbd95 773 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
a7b4f989
JK
774 int ret;
775
ca0f6a5c 776 BUG_ON(!set);
a7b4f989
JK
777 pr_debug("set %s, index %u\n", set->name, index);
778
ac8cc925 779 if (opt->dim < set->type->dimension ||
c15f1c83 780 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
35f6e63a 781 return -IPSET_ERR_TYPE_MISMATCH;
a7b4f989 782
f66ee041 783 ip_set_lock(set);
b66554cf 784 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
f66ee041 785 ip_set_unlock(set);
a7b4f989
JK
786
787 return ret;
788}
789EXPORT_SYMBOL_GPL(ip_set_add);
790
791int
792ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
075e64c0 793 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
a7b4f989 794{
613dbd95 795 struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
a7b4f989
JK
796 int ret = 0;
797
ca0f6a5c 798 BUG_ON(!set);
a7b4f989
JK
799 pr_debug("set %s, index %u\n", set->name, index);
800
ac8cc925 801 if (opt->dim < set->type->dimension ||
c15f1c83 802 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
35f6e63a 803 return -IPSET_ERR_TYPE_MISMATCH;
a7b4f989 804
f66ee041 805 ip_set_lock(set);
b66554cf 806 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
f66ee041 807 ip_set_unlock(set);
a7b4f989
JK
808
809 return ret;
810}
811EXPORT_SYMBOL_GPL(ip_set_del);
812
ca0f6a5c 813/* Find set by name, reference it once. The reference makes sure the
a7b4f989
JK
814 * thing pointed to, does not go away under our feet.
815 *
a7b4f989
JK
816 */
817ip_set_id_t
1785e8f4 818ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
a7b4f989
JK
819{
820 ip_set_id_t i, index = IPSET_INVALID_ID;
821 struct ip_set *s;
1785e8f4 822 struct ip_set_net *inst = ip_set_pernet(net);
a7b4f989 823
9076aea7 824 rcu_read_lock();
1785e8f4
VL
825 for (i = 0; i < inst->ip_set_max; i++) {
826 s = rcu_dereference(inst->ip_set_list)[i];
ca0f6a5c 827 if (s && STRNCMP(s->name, name)) {
9076aea7 828 __ip_set_get(s);
a7b4f989
JK
829 index = i;
830 *set = s;
9076aea7 831 break;
a7b4f989
JK
832 }
833 }
9076aea7 834 rcu_read_unlock();
a7b4f989
JK
835
836 return index;
837}
838EXPORT_SYMBOL_GPL(ip_set_get_byname);
839
ca0f6a5c 840/* If the given set pointer points to a valid set, decrement
a7b4f989
JK
841 * reference count by 1. The caller shall not assume the index
842 * to be valid, after calling this function.
843 *
a7b4f989 844 */
1785e8f4 845
8dea982a 846static void
1785e8f4 847__ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
a7b4f989 848{
9076aea7
JK
849 struct ip_set *set;
850
851 rcu_read_lock();
1785e8f4 852 set = rcu_dereference(inst->ip_set_list)[index];
ca0f6a5c 853 if (set)
9076aea7
JK
854 __ip_set_put(set);
855 rcu_read_unlock();
a7b4f989 856}
1785e8f4
VL
857
858void
859ip_set_put_byindex(struct net *net, ip_set_id_t index)
860{
861 struct ip_set_net *inst = ip_set_pernet(net);
862
863 __ip_set_put_byindex(inst, index);
864}
a7b4f989
JK
865EXPORT_SYMBOL_GPL(ip_set_put_byindex);
866
ca0f6a5c 867/* Get the name of a set behind a set index.
439cd39e
SB
868 * Set itself is protected by RCU, but its name isn't: to protect against
869 * renaming, grab ip_set_ref_lock as reader (see ip_set_rename()) and copy the
870 * name.
a7b4f989 871 */
439cd39e
SB
872void
873ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name)
a7b4f989 874{
439cd39e 875 struct ip_set *set = ip_set_rcu_get(net, index);
a7b4f989 876
ca0f6a5c 877 BUG_ON(!set);
a7b4f989 878
439cd39e 879 read_lock_bh(&ip_set_ref_lock);
e5331403 880 strscpy_pad(name, set->name, IPSET_MAXNAMELEN);
439cd39e 881 read_unlock_bh(&ip_set_ref_lock);
a7b4f989
JK
882}
883EXPORT_SYMBOL_GPL(ip_set_name_byindex);
884
ca0f6a5c 885/* Routines to call by external subsystems, which do not
a7b4f989
JK
886 * call nfnl_lock for us.
887 */
888
ca0f6a5c 889/* Find set by index, reference it once. The reference makes sure the
a7b4f989
JK
890 * thing pointed to, does not go away under our feet.
891 *
892 * The nfnl mutex is used in the function.
893 */
894ip_set_id_t
1785e8f4 895ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
a7b4f989 896{
9076aea7 897 struct ip_set *set;
1785e8f4 898 struct ip_set_net *inst = ip_set_pernet(net);
9076aea7 899
0f9f5e1b 900 if (index >= inst->ip_set_max)
a7b4f989
JK
901 return IPSET_INVALID_ID;
902
c14b78e7 903 nfnl_lock(NFNL_SUBSYS_IPSET);
3e90ebd3 904 set = ip_set(inst, index);
9076aea7
JK
905 if (set)
906 __ip_set_get(set);
a7b4f989
JK
907 else
908 index = IPSET_INVALID_ID;
c14b78e7 909 nfnl_unlock(NFNL_SUBSYS_IPSET);
a7b4f989
JK
910
911 return index;
912}
913EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
914
ca0f6a5c 915/* If the given set pointer points to a valid set, decrement
a7b4f989
JK
916 * reference count by 1. The caller shall not assume the index
917 * to be valid, after calling this function.
918 *
919 * The nfnl mutex is used in the function.
920 */
921void
1785e8f4 922ip_set_nfnl_put(struct net *net, ip_set_id_t index)
a7b4f989 923{
9076aea7 924 struct ip_set *set;
1785e8f4
VL
925 struct ip_set_net *inst = ip_set_pernet(net);
926
c14b78e7 927 nfnl_lock(NFNL_SUBSYS_IPSET);
1785e8f4 928 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
3e90ebd3 929 set = ip_set(inst, index);
ca0f6a5c 930 if (set)
1785e8f4
VL
931 __ip_set_put(set);
932 }
c14b78e7 933 nfnl_unlock(NFNL_SUBSYS_IPSET);
a7b4f989
JK
934}
935EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
936
ca0f6a5c 937/* Communication protocol with userspace over netlink.
a7b4f989 938 *
2f9f28b2 939 * The commands are serialized by the nfnl mutex.
a7b4f989
JK
940 */
941
23c42a40
JK
942static inline u8 protocol(const struct nlattr * const tb[])
943{
944 return nla_get_u8(tb[IPSET_ATTR_PROTOCOL]);
945}
946
a7b4f989
JK
947static inline bool
948protocol_failed(const struct nlattr * const tb[])
949{
23c42a40
JK
950 return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) != IPSET_PROTOCOL;
951}
952
953static inline bool
954protocol_min_failed(const struct nlattr * const tb[])
955{
956 return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) < IPSET_PROTOCOL_MIN;
a7b4f989
JK
957}
958
959static inline u32
960flag_exist(const struct nlmsghdr *nlh)
961{
962 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
963}
964
965static struct nlmsghdr *
15e47304 966start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
a7b4f989
JK
967 enum ipset_cmd cmd)
968{
19c28b13
PNA
969 return nfnl_msg_put(skb, portid, seq,
970 nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd), flags,
971 NFPROTO_IPV4, NFNETLINK_V0, 0);
a7b4f989
JK
972}
973
974/* Create a set */
975
976static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
977 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
978 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
979 .len = IPSET_MAXNAMELEN - 1 },
980 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
981 .len = IPSET_MAXNAMELEN - 1},
982 [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
983 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
984 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
985};
986
9076aea7 987static struct ip_set *
1785e8f4 988find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
a7b4f989 989{
9076aea7
JK
990 struct ip_set *set = NULL;
991 ip_set_id_t i;
a7b4f989 992
9076aea7 993 *id = IPSET_INVALID_ID;
1785e8f4 994 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 995 set = ip_set(inst, i);
ca0f6a5c 996 if (set && STRNCMP(set->name, name)) {
9076aea7
JK
997 *id = i;
998 break;
999 }
a7b4f989 1000 }
9076aea7 1001 return (*id == IPSET_INVALID_ID ? NULL : set);
a7b4f989
JK
1002}
1003
1004static inline struct ip_set *
1785e8f4 1005find_set(struct ip_set_net *inst, const char *name)
a7b4f989 1006{
9076aea7 1007 ip_set_id_t id;
a7b4f989 1008
1785e8f4 1009 return find_set_and_id(inst, name, &id);
a7b4f989
JK
1010}
1011
1012static int
1785e8f4
VL
1013find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
1014 struct ip_set **set)
a7b4f989 1015{
9076aea7 1016 struct ip_set *s;
a7b4f989
JK
1017 ip_set_id_t i;
1018
1019 *index = IPSET_INVALID_ID;
1785e8f4 1020 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 1021 s = ip_set(inst, i);
ca0f6a5c 1022 if (!s) {
a7b4f989
JK
1023 if (*index == IPSET_INVALID_ID)
1024 *index = i;
22496f09 1025 } else if (STRNCMP(name, s->name)) {
a7b4f989 1026 /* Name clash */
9076aea7 1027 *set = s;
a7b4f989
JK
1028 return -EEXIST;
1029 }
1030 }
1031 if (*index == IPSET_INVALID_ID)
1032 /* No free slot remained */
1033 return -IPSET_ERR_MAX_SETS;
1034 return 0;
1035}
1036
a6555365
PNA
1037static int ip_set_none(struct sk_buff *skb, const struct nfnl_info *info,
1038 const struct nlattr * const attr[])
d31f4d44
TB
1039{
1040 return -EOPNOTSUPP;
1041}
1042
a6555365
PNA
1043static int ip_set_create(struct sk_buff *skb, const struct nfnl_info *info,
1044 const struct nlattr * const attr[])
a7b4f989 1045{
a6555365 1046 struct ip_set_net *inst = ip_set_pernet(info->net);
9846ada1 1047 struct ip_set *set, *clash = NULL;
a7b4f989 1048 ip_set_id_t index = IPSET_INVALID_ID;
ca0f6a5c 1049 struct nlattr *tb[IPSET_ATTR_CREATE_MAX + 1] = {};
a7b4f989
JK
1050 const char *name, *typename;
1051 u8 family, revision;
a6555365 1052 u32 flags = flag_exist(info->nlh);
a7b4f989
JK
1053 int ret = 0;
1054
23c42a40 1055 if (unlikely(protocol_min_failed(attr) ||
ca0f6a5c
JK
1056 !attr[IPSET_ATTR_SETNAME] ||
1057 !attr[IPSET_ATTR_TYPENAME] ||
1058 !attr[IPSET_ATTR_REVISION] ||
1059 !attr[IPSET_ATTR_FAMILY] ||
1060 (attr[IPSET_ATTR_DATA] &&
a7b4f989
JK
1061 !flag_nested(attr[IPSET_ATTR_DATA]))))
1062 return -IPSET_ERR_PROTOCOL;
1063
1064 name = nla_data(attr[IPSET_ATTR_SETNAME]);
1065 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1066 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1067 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
1068 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
1069 name, typename, family_name(family), revision);
1070
ca0f6a5c 1071 /* First, and without any locks, allocate and initialize
a7b4f989
JK
1072 * a normal base set structure.
1073 */
ca0f6a5c 1074 set = kzalloc(sizeof(*set), GFP_KERNEL);
a7b4f989
JK
1075 if (!set)
1076 return -ENOMEM;
b57b2d1f 1077 spin_lock_init(&set->lock);
8556bceb 1078 strscpy(set->name, name, IPSET_MAXNAMELEN);
a7b4f989 1079 set->family = family;
f1e00b39 1080 set->revision = revision;
a7b4f989 1081
ca0f6a5c 1082 /* Next, check that we know the type, and take
a7b4f989
JK
1083 * a reference on the type, to make sure it stays available
1084 * while constructing our new set.
1085 *
1086 * After referencing the type, we try to create the type
1087 * specific part of the set without holding any locks.
1088 */
ca0f6a5c 1089 ret = find_set_type_get(typename, family, revision, &set->type);
a7b4f989
JK
1090 if (ret)
1091 goto out;
1092
ca0f6a5c 1093 /* Without holding any locks, create private part. */
a7b4f989 1094 if (attr[IPSET_ATTR_DATA] &&
12899756
JK
1095 nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
1096 set->type->create_policy, NULL)) {
15b4d93f
JK
1097 ret = -IPSET_ERR_PROTOCOL;
1098 goto put_out;
a7b4f989 1099 }
ccf0a4b7
JK
1100 /* Set create flags depending on the type revision */
1101 set->flags |= set->type->create_flags[revision];
a7b4f989 1102
a6555365 1103 ret = set->type->create(info->net, set, tb, flags);
a7b4f989
JK
1104 if (ret != 0)
1105 goto put_out;
1106
1107 /* BTW, ret==0 here. */
1108
ca0f6a5c 1109 /* Here, we have a valid, constructed set and we are protected
2f9f28b2
JK
1110 * by the nfnl mutex. Find the first free index in ip_set_list
1111 * and check clashing.
a7b4f989 1112 */
1785e8f4 1113 ret = find_free_id(inst, set->name, &index, &clash);
9076aea7 1114 if (ret == -EEXIST) {
a7b4f989 1115 /* If this is the same set and requested, ignore error */
9076aea7 1116 if ((flags & IPSET_FLAG_EXIST) &&
22496f09 1117 STRNCMP(set->type->name, clash->type->name) &&
a7b4f989 1118 set->type->family == clash->type->family &&
f1e00b39
JK
1119 set->type->revision_min == clash->type->revision_min &&
1120 set->type->revision_max == clash->type->revision_max &&
a7b4f989
JK
1121 set->variant->same_set(set, clash))
1122 ret = 0;
1123 goto cleanup;
9076aea7
JK
1124 } else if (ret == -IPSET_ERR_MAX_SETS) {
1125 struct ip_set **list, **tmp;
1785e8f4 1126 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
9076aea7 1127
1785e8f4 1128 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
9076aea7
JK
1129 /* Wraparound */
1130 goto cleanup;
1131
ed956f39 1132 list = kvcalloc(i, sizeof(struct ip_set *), GFP_KERNEL);
9076aea7
JK
1133 if (!list)
1134 goto cleanup;
1135 /* nfnl mutex is held, both lists are valid */
3e90ebd3 1136 tmp = ip_set_dereference(inst->ip_set_list);
1785e8f4
VL
1137 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
1138 rcu_assign_pointer(inst->ip_set_list, list);
9076aea7
JK
1139 /* Make sure all current packets have passed through */
1140 synchronize_net();
1141 /* Use new list */
1785e8f4
VL
1142 index = inst->ip_set_max;
1143 inst->ip_set_max = i;
ed956f39 1144 kvfree(tmp);
9076aea7 1145 ret = 0;
ca0f6a5c 1146 } else if (ret) {
9076aea7 1147 goto cleanup;
ca0f6a5c 1148 }
a7b4f989 1149
ca0f6a5c 1150 /* Finally! Add our shiny new set to the list, and be done. */
a7b4f989 1151 pr_debug("create: '%s' created with index %u!\n", set->name, index);
3e90ebd3 1152 ip_set(inst, index) = set;
a7b4f989
JK
1153
1154 return ret;
1155
1156cleanup:
1157 set->variant->destroy(set);
1158put_out:
1159 module_put(set->type->me);
1160out:
1161 kfree(set);
1162 return ret;
1163}
1164
1165/* Destroy sets */
1166
1167static const struct nla_policy
1168ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
1169 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1170 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1171 .len = IPSET_MAXNAMELEN - 1 },
1172};
1173
1174static void
9c1ba5c8 1175ip_set_destroy_set(struct ip_set *set)
a7b4f989 1176{
a7b4f989 1177 pr_debug("set: %s\n", set->name);
a7b4f989
JK
1178
1179 /* Must call it without holding any lock */
1180 set->variant->destroy(set);
1181 module_put(set->type->me);
1182 kfree(set);
1183}
1184
a6555365
PNA
1185static int ip_set_destroy(struct sk_buff *skb, const struct nfnl_info *info,
1186 const struct nlattr * const attr[])
a7b4f989 1187{
a6555365 1188 struct ip_set_net *inst = ip_set_pernet(info->net);
9076aea7 1189 struct ip_set *s;
a7b4f989 1190 ip_set_id_t i;
2f9f28b2 1191 int ret = 0;
a7b4f989 1192
23c42a40 1193 if (unlikely(protocol_min_failed(attr)))
a7b4f989
JK
1194 return -IPSET_ERR_PROTOCOL;
1195
45040978
JK
1196 /* Must wait for flush to be really finished in list:set */
1197 rcu_barrier();
1198
2f9f28b2
JK
1199 /* Commands are serialized and references are
1200 * protected by the ip_set_ref_lock.
1201 * External systems (i.e. xt_set) must call
1202 * ip_set_put|get_nfnl_* functions, that way we
1203 * can safely check references here.
1204 *
1205 * list:set timer can only decrement the reference
1206 * counter, so if it's already zero, we can proceed
1207 * without holding the lock.
1208 */
1209 read_lock_bh(&ip_set_ref_lock);
a7b4f989 1210 if (!attr[IPSET_ATTR_SETNAME]) {
1785e8f4 1211 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 1212 s = ip_set(inst, i);
596cf3fe 1213 if (s && (s->ref || s->ref_netlink)) {
9d883232 1214 ret = -IPSET_ERR_BUSY;
2f9f28b2
JK
1215 goto out;
1216 }
a7b4f989 1217 }
9c1ba5c8 1218 inst->is_destroyed = true;
2f9f28b2 1219 read_unlock_bh(&ip_set_ref_lock);
1785e8f4 1220 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 1221 s = ip_set(inst, i);
9c1ba5c8
JK
1222 if (s) {
1223 ip_set(inst, i) = NULL;
1224 ip_set_destroy_set(s);
1225 }
a7b4f989 1226 }
9c1ba5c8
JK
1227 /* Modified by ip_set_destroy() only, which is serialized */
1228 inst->is_destroyed = false;
a7b4f989 1229 } else {
a6555365 1230 u32 flags = flag_exist(info->nlh);
1785e8f4
VL
1231 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1232 &i);
ca0f6a5c 1233 if (!s) {
a304ea7d
JK
1234 if (!(flags & IPSET_FLAG_EXIST))
1235 ret = -ENOENT;
2f9f28b2 1236 goto out;
596cf3fe 1237 } else if (s->ref || s->ref_netlink) {
2f9f28b2
JK
1238 ret = -IPSET_ERR_BUSY;
1239 goto out;
1240 }
9c1ba5c8 1241 ip_set(inst, i) = NULL;
2f9f28b2 1242 read_unlock_bh(&ip_set_ref_lock);
a7b4f989 1243
9c1ba5c8 1244 ip_set_destroy_set(s);
a7b4f989
JK
1245 }
1246 return 0;
2f9f28b2
JK
1247out:
1248 read_unlock_bh(&ip_set_ref_lock);
1249 return ret;
a7b4f989
JK
1250}
1251
1252/* Flush sets */
1253
1254static void
1255ip_set_flush_set(struct ip_set *set)
1256{
1257 pr_debug("set: %s\n", set->name);
1258
f66ee041 1259 ip_set_lock(set);
a7b4f989 1260 set->variant->flush(set);
f66ee041 1261 ip_set_unlock(set);
a7b4f989
JK
1262}
1263
a6555365
PNA
1264static int ip_set_flush(struct sk_buff *skb, const struct nfnl_info *info,
1265 const struct nlattr * const attr[])
a7b4f989 1266{
a6555365 1267 struct ip_set_net *inst = ip_set_pernet(info->net);
9076aea7 1268 struct ip_set *s;
a7b4f989
JK
1269 ip_set_id_t i;
1270
23c42a40 1271 if (unlikely(protocol_min_failed(attr)))
9184a9cb 1272 return -IPSET_ERR_PROTOCOL;
a7b4f989
JK
1273
1274 if (!attr[IPSET_ATTR_SETNAME]) {
1785e8f4 1275 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 1276 s = ip_set(inst, i);
ca0f6a5c 1277 if (s)
9076aea7
JK
1278 ip_set_flush_set(s);
1279 }
a7b4f989 1280 } else {
1785e8f4 1281 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
ca0f6a5c 1282 if (!s)
a7b4f989
JK
1283 return -ENOENT;
1284
9076aea7 1285 ip_set_flush_set(s);
a7b4f989
JK
1286 }
1287
1288 return 0;
1289}
1290
1291/* Rename a set */
1292
1293static const struct nla_policy
1294ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
1295 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1296 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1297 .len = IPSET_MAXNAMELEN - 1 },
1298 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
1299 .len = IPSET_MAXNAMELEN - 1 },
1300};
1301
a6555365
PNA
1302static int ip_set_rename(struct sk_buff *skb, const struct nfnl_info *info,
1303 const struct nlattr * const attr[])
a7b4f989 1304{
a6555365 1305 struct ip_set_net *inst = ip_set_pernet(info->net);
9076aea7 1306 struct ip_set *set, *s;
a7b4f989
JK
1307 const char *name2;
1308 ip_set_id_t i;
2f9f28b2 1309 int ret = 0;
a7b4f989 1310
23c42a40 1311 if (unlikely(protocol_min_failed(attr) ||
ca0f6a5c
JK
1312 !attr[IPSET_ATTR_SETNAME] ||
1313 !attr[IPSET_ATTR_SETNAME2]))
a7b4f989
JK
1314 return -IPSET_ERR_PROTOCOL;
1315
1785e8f4 1316 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
ca0f6a5c 1317 if (!set)
a7b4f989 1318 return -ENOENT;
2f9f28b2 1319
439cd39e 1320 write_lock_bh(&ip_set_ref_lock);
6c1f7e2c 1321 if (set->ref != 0 || set->ref_netlink != 0) {
2f9f28b2
JK
1322 ret = -IPSET_ERR_REFERENCED;
1323 goto out;
1324 }
a7b4f989
JK
1325
1326 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1785e8f4 1327 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 1328 s = ip_set(inst, i);
ca0f6a5c 1329 if (s && STRNCMP(s->name, name2)) {
2f9f28b2
JK
1330 ret = -IPSET_ERR_EXIST_SETNAME2;
1331 goto out;
1332 }
a7b4f989 1333 }
e5331403 1334 strscpy_pad(set->name, name2, IPSET_MAXNAMELEN);
a7b4f989 1335
2f9f28b2 1336out:
439cd39e 1337 write_unlock_bh(&ip_set_ref_lock);
2f9f28b2 1338 return ret;
a7b4f989
JK
1339}
1340
1341/* Swap two sets so that name/index points to the other.
1342 * References and set names are also swapped.
1343 *
2f9f28b2
JK
1344 * The commands are serialized by the nfnl mutex and references are
1345 * protected by the ip_set_ref_lock. The kernel interfaces
a7b4f989
JK
1346 * do not hold the mutex but the pointer settings are atomic
1347 * so the ip_set_list always contains valid pointers to the sets.
1348 */
1349
a6555365
PNA
1350static int ip_set_swap(struct sk_buff *skb, const struct nfnl_info *info,
1351 const struct nlattr * const attr[])
a7b4f989 1352{
a6555365 1353 struct ip_set_net *inst = ip_set_pernet(info->net);
a7b4f989
JK
1354 struct ip_set *from, *to;
1355 ip_set_id_t from_id, to_id;
1356 char from_name[IPSET_MAXNAMELEN];
a7b4f989 1357
23c42a40 1358 if (unlikely(protocol_min_failed(attr) ||
ca0f6a5c
JK
1359 !attr[IPSET_ATTR_SETNAME] ||
1360 !attr[IPSET_ATTR_SETNAME2]))
a7b4f989
JK
1361 return -IPSET_ERR_PROTOCOL;
1362
1785e8f4
VL
1363 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1364 &from_id);
ca0f6a5c 1365 if (!from)
a7b4f989
JK
1366 return -ENOENT;
1367
1785e8f4
VL
1368 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1369 &to_id);
ca0f6a5c 1370 if (!to)
a7b4f989
JK
1371 return -IPSET_ERR_EXIST_SETNAME2;
1372
a7b4f989 1373 /* Features must not change.
ca0f6a5c
JK
1374 * Not an artifical restriction anymore, as we must prevent
1375 * possible loops created by swapping in setlist type of sets.
1376 */
a7b4f989 1377 if (!(from->type->features == to->type->features &&
169faa2e 1378 from->family == to->family))
a7b4f989
JK
1379 return -IPSET_ERR_TYPE_MISMATCH;
1380
e5173418
RL
1381 write_lock_bh(&ip_set_ref_lock);
1382
1383 if (from->ref_netlink || to->ref_netlink) {
1384 write_unlock_bh(&ip_set_ref_lock);
596cf3fe 1385 return -EBUSY;
e5173418 1386 }
596cf3fe 1387
e5331403
JS
1388 strscpy_pad(from_name, from->name, IPSET_MAXNAMELEN);
1389 strscpy_pad(from->name, to->name, IPSET_MAXNAMELEN);
1390 strscpy_pad(to->name, from_name, IPSET_MAXNAMELEN);
a7b4f989 1391
2f9f28b2 1392 swap(from->ref, to->ref);
3e90ebd3
PM
1393 ip_set(inst, from_id) = to;
1394 ip_set(inst, to_id) = from;
2f9f28b2 1395 write_unlock_bh(&ip_set_ref_lock);
a7b4f989 1396
28628fa9
JK
1397 /* Make sure all readers of the old set pointers are completed. */
1398 synchronize_rcu();
1399
a7b4f989
JK
1400 return 0;
1401}
1402
1403/* List/save set data */
1404
c1e2e043
JK
1405#define DUMP_INIT 0
1406#define DUMP_ALL 1
1407#define DUMP_ONE 2
1408#define DUMP_LAST 3
1409
1410#define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
1411#define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
a7b4f989 1412
85639185
JS
1413int
1414ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
1415{
1416 u32 cadt_flags = 0;
1417
1418 if (SET_WITH_TIMEOUT(set))
1419 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
1420 htonl(set->timeout))))
1421 return -EMSGSIZE;
1422 if (SET_WITH_COUNTER(set))
1423 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
1424 if (SET_WITH_COMMENT(set))
1425 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
1426 if (SET_WITH_SKBINFO(set))
1427 cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
1428 if (SET_WITH_FORCEADD(set))
1429 cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
1430
1431 if (!cadt_flags)
1432 return 0;
1433 return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
1434}
1435EXPORT_SYMBOL_GPL(ip_set_put_flags);
1436
a7b4f989
JK
1437static int
1438ip_set_dump_done(struct netlink_callback *cb)
1439{
93302880 1440 if (cb->args[IPSET_CB_ARG0]) {
c4c99783
JK
1441 struct ip_set_net *inst =
1442 (struct ip_set_net *)cb->args[IPSET_CB_NET];
1443 ip_set_id_t index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
8a02bdd5 1444 struct ip_set *set = ip_set_ref_netlink(inst, index);
c4c99783
JK
1445
1446 if (set->variant->uref)
1447 set->variant->uref(set, cb, false);
1448 pr_debug("release set %s\n", set->name);
596cf3fe 1449 __ip_set_put_netlink(set);
a7b4f989
JK
1450 }
1451 return 0;
1452}
1453
1454static inline void
1455dump_attrs(struct nlmsghdr *nlh)
1456{
1457 const struct nlattr *attr;
1458 int rem;
1459
1460 pr_debug("dump nlmsg\n");
1461 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
1462 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
1463 }
1464}
1465
12899756
JK
1466static const struct nla_policy
1467ip_set_dump_policy[IPSET_ATTR_CMD_MAX + 1] = {
1468 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1469 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1470 .len = IPSET_MAXNAMELEN - 1 },
1471 [IPSET_ATTR_FLAGS] = { .type = NLA_U32 },
1472};
1473
a7b4f989 1474static int
50385171 1475ip_set_dump_start(struct netlink_callback *cb)
a7b4f989
JK
1476{
1477 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
573ce260 1478 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
ca0f6a5c 1479 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
a7b4f989 1480 struct nlattr *attr = (void *)nlh + min_len;
50385171
KJ
1481 struct sk_buff *skb = cb->skb;
1482 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
c1e2e043 1483 u32 dump_type;
13c6ba1f 1484 int ret;
a7b4f989 1485
12899756
JK
1486 ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, attr,
1487 nlh->nlmsg_len - min_len,
1488 ip_set_dump_policy, NULL);
13c6ba1f 1489 if (ret)
50385171 1490 goto error;
a7b4f989 1491
23c42a40 1492 cb->args[IPSET_CB_PROTO] = nla_get_u8(cda[IPSET_ATTR_PROTOCOL]);
c1e2e043 1493 if (cda[IPSET_ATTR_SETNAME]) {
50385171 1494 ip_set_id_t index;
9076aea7
JK
1495 struct ip_set *set;
1496
1785e8f4 1497 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
9076aea7 1498 &index);
50385171
KJ
1499 if (!set) {
1500 ret = -ENOENT;
1501 goto error;
1502 }
c1e2e043 1503 dump_type = DUMP_ONE;
93302880 1504 cb->args[IPSET_CB_INDEX] = index;
ca0f6a5c 1505 } else {
c1e2e043 1506 dump_type = DUMP_ALL;
ca0f6a5c 1507 }
c1e2e043
JK
1508
1509 if (cda[IPSET_ATTR_FLAGS]) {
1510 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
ca0f6a5c 1511
c1e2e043
JK
1512 dump_type |= (f << 16);
1513 }
93302880
JK
1514 cb->args[IPSET_CB_NET] = (unsigned long)inst;
1515 cb->args[IPSET_CB_DUMP] = dump_type;
a7b4f989 1516
a7b4f989 1517 return 0;
50385171
KJ
1518
1519error:
1520 /* We have to create and send the error message manually :-( */
1521 if (nlh->nlmsg_flags & NLM_F_ACK) {
1522 netlink_ack(cb->skb, nlh, ret, NULL);
1523 }
1524 return ret;
a7b4f989
JK
1525}
1526
1527static int
50385171 1528ip_set_dump_do(struct sk_buff *skb, struct netlink_callback *cb)
a7b4f989
JK
1529{
1530 ip_set_id_t index = IPSET_INVALID_ID, max;
1531 struct ip_set *set = NULL;
1532 struct nlmsghdr *nlh = NULL;
15e47304 1533 unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0;
93302880 1534 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
c1e2e043 1535 u32 dump_type, dump_flags;
9c1ba5c8 1536 bool is_destroyed;
a7b4f989
JK
1537 int ret = 0;
1538
50385171
KJ
1539 if (!cb->args[IPSET_CB_DUMP])
1540 return -EINVAL;
a7b4f989 1541
93302880 1542 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
a7b4f989
JK
1543 goto out;
1544
93302880
JK
1545 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1546 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1547 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1548 : inst->ip_set_max;
a8a8a093 1549dump_last:
93302880
JK
1550 pr_debug("dump type, flag: %u %u index: %ld\n",
1551 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1552 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
ca0f6a5c 1553 index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
9c1ba5c8 1554 write_lock_bh(&ip_set_ref_lock);
3e90ebd3 1555 set = ip_set(inst, index);
9c1ba5c8
JK
1556 is_destroyed = inst->is_destroyed;
1557 if (!set || is_destroyed) {
1558 write_unlock_bh(&ip_set_ref_lock);
c1e2e043 1559 if (dump_type == DUMP_ONE) {
a7b4f989
JK
1560 ret = -ENOENT;
1561 goto out;
1562 }
9c1ba5c8
JK
1563 if (is_destroyed) {
1564 /* All sets are just being destroyed */
1565 ret = 0;
1566 goto out;
1567 }
a7b4f989
JK
1568 continue;
1569 }
1570 /* When dumping all sets, we must dump "sorted"
1571 * so that lists (unions of sets) are dumped last.
1572 */
c1e2e043
JK
1573 if (dump_type != DUMP_ONE &&
1574 ((dump_type == DUMP_ALL) ==
9c1ba5c8
JK
1575 !!(set->type->features & IPSET_DUMP_LAST))) {
1576 write_unlock_bh(&ip_set_ref_lock);
a7b4f989 1577 continue;
9c1ba5c8 1578 }
a7b4f989 1579 pr_debug("List set: %s\n", set->name);
93302880 1580 if (!cb->args[IPSET_CB_ARG0]) {
a7b4f989
JK
1581 /* Start listing: make sure set won't be destroyed */
1582 pr_debug("reference set\n");
596cf3fe 1583 set->ref_netlink++;
a7b4f989 1584 }
9c1ba5c8 1585 write_unlock_bh(&ip_set_ref_lock);
15e47304 1586 nlh = start_msg(skb, NETLINK_CB(cb->skb).portid,
a7b4f989
JK
1587 cb->nlh->nlmsg_seq, flags,
1588 IPSET_CMD_LIST);
1589 if (!nlh) {
1590 ret = -EMSGSIZE;
1591 goto release_refcount;
1592 }
23c42a40
JK
1593 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL,
1594 cb->args[IPSET_CB_PROTO]) ||
7cf7899d
DM
1595 nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
1596 goto nla_put_failure;
c1e2e043
JK
1597 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1598 goto next_set;
93302880 1599 switch (cb->args[IPSET_CB_ARG0]) {
a7b4f989
JK
1600 case 0:
1601 /* Core header data */
7cf7899d
DM
1602 if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
1603 set->type->name) ||
1604 nla_put_u8(skb, IPSET_ATTR_FAMILY,
1605 set->family) ||
1606 nla_put_u8(skb, IPSET_ATTR_REVISION,
1607 set->revision))
1608 goto nla_put_failure;
23c42a40
JK
1609 if (cb->args[IPSET_CB_PROTO] > IPSET_PROTOCOL_MIN &&
1610 nla_put_net16(skb, IPSET_ATTR_INDEX, htons(index)))
1611 goto nla_put_failure;
a7b4f989
JK
1612 ret = set->variant->head(set, skb);
1613 if (ret < 0)
1614 goto release_refcount;
c1e2e043
JK
1615 if (dump_flags & IPSET_FLAG_LIST_HEADER)
1616 goto next_set;
c4c99783
JK
1617 if (set->variant->uref)
1618 set->variant->uref(set, cb, true);
954d8297 1619 fallthrough;
a7b4f989 1620 default:
a7b4f989 1621 ret = set->variant->list(set, skb, cb);
93302880 1622 if (!cb->args[IPSET_CB_ARG0])
a7b4f989 1623 /* Set is done, proceed with next one */
c1e2e043 1624 goto next_set;
a7b4f989
JK
1625 goto release_refcount;
1626 }
1627 }
a8a8a093 1628 /* If we dump all sets, continue with dumping last ones */
c1e2e043
JK
1629 if (dump_type == DUMP_ALL) {
1630 dump_type = DUMP_LAST;
93302880
JK
1631 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1632 cb->args[IPSET_CB_INDEX] = 0;
c4c99783
JK
1633 if (set && set->variant->uref)
1634 set->variant->uref(set, cb, false);
a8a8a093
JK
1635 goto dump_last;
1636 }
a7b4f989
JK
1637 goto out;
1638
1639nla_put_failure:
1640 ret = -EFAULT;
c1e2e043
JK
1641next_set:
1642 if (dump_type == DUMP_ONE)
93302880 1643 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
c1e2e043 1644 else
93302880 1645 cb->args[IPSET_CB_INDEX]++;
a7b4f989
JK
1646release_refcount:
1647 /* If there was an error or set is done, release set */
93302880 1648 if (ret || !cb->args[IPSET_CB_ARG0]) {
8a02bdd5 1649 set = ip_set_ref_netlink(inst, index);
c4c99783
JK
1650 if (set->variant->uref)
1651 set->variant->uref(set, cb, false);
1652 pr_debug("release set %s\n", set->name);
596cf3fe 1653 __ip_set_put_netlink(set);
93302880 1654 cb->args[IPSET_CB_ARG0] = 0;
a7b4f989 1655 }
a7b4f989
JK
1656out:
1657 if (nlh) {
1658 nlmsg_end(skb, nlh);
1659 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1660 dump_attrs(nlh);
1661 }
1662
1663 return ret < 0 ? ret : skb->len;
1664}
1665
a6555365
PNA
1666static int ip_set_dump(struct sk_buff *skb, const struct nfnl_info *info,
1667 const struct nlattr * const attr[])
a7b4f989 1668{
23c42a40 1669 if (unlikely(protocol_min_failed(attr)))
a7b4f989
JK
1670 return -IPSET_ERR_PROTOCOL;
1671
80d326fa
PNA
1672 {
1673 struct netlink_dump_control c = {
50385171
KJ
1674 .start = ip_set_dump_start,
1675 .dump = ip_set_dump_do,
80d326fa
PNA
1676 .done = ip_set_dump_done,
1677 };
a6555365 1678 return netlink_dump_start(info->sk, skb, info->nlh, &c);
80d326fa 1679 }
a7b4f989
JK
1680}
1681
1682/* Add, del and test */
1683
1684static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1685 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1686 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1687 .len = IPSET_MAXNAMELEN - 1 },
1688 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
1689 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1690 [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
1691};
1692
1693static int
e0241ae6
PNA
1694call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1695 struct ip_set *set, struct nlattr *tb[], enum ipset_adt adt,
a7b4f989
JK
1696 u32 flags, bool use_lineno)
1697{
3d14b171 1698 int ret;
a7b4f989 1699 u32 lineno = 0;
3d14b171 1700 bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
a7b4f989
JK
1701
1702 do {
24e22789 1703 if (retried) {
7433b6d2 1704 __ip_set_get_netlink(set);
24e22789
KI
1705 nfnl_unlock(NFNL_SUBSYS_IPSET);
1706 cond_resched();
1707 nfnl_lock(NFNL_SUBSYS_IPSET);
7433b6d2 1708 __ip_set_put_netlink(set);
24e22789
KI
1709 }
1710
f66ee041 1711 ip_set_lock(set);
3d14b171 1712 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
f66ee041 1713 ip_set_unlock(set);
3d14b171 1714 retried = true;
5e29dc36
JK
1715 } while (ret == -ERANGE ||
1716 (ret == -EAGAIN &&
1717 set->variant->resize &&
1718 (ret = set->variant->resize(set, retried)) == 0));
a7b4f989
JK
1719
1720 if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1721 return 0;
1722 if (lineno && use_lineno) {
1723 /* Error in restore/batch mode: send back lineno */
5f52bc3c
JK
1724 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1725 struct sk_buff *skb2;
1726 struct nlmsgerr *errmsg;
73e64e18
JK
1727 size_t payload = min(SIZE_MAX,
1728 sizeof(*errmsg) + nlmsg_len(nlh));
573ce260 1729 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
ca0f6a5c 1730 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
5f52bc3c 1731 struct nlattr *cmdattr;
a7b4f989
JK
1732 u32 *errline;
1733
5f52bc3c 1734 skb2 = nlmsg_new(payload, GFP_KERNEL);
ca0f6a5c 1735 if (!skb2)
5f52bc3c 1736 return -ENOMEM;
710d21fd
KC
1737 rep = nlmsg_put(skb2, NETLINK_CB(skb).portid,
1738 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
5f52bc3c
JK
1739 errmsg = nlmsg_data(rep);
1740 errmsg->error = ret;
710d21fd
KC
1741 unsafe_memcpy(&errmsg->msg, nlh, nlh->nlmsg_len,
1742 /* Bounds checked by the skb layer. */);
1743
5f52bc3c
JK
1744 cmdattr = (void *)&errmsg->msg + min_len;
1745
12899756
JK
1746 ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, cmdattr,
1747 nlh->nlmsg_len - min_len, ip_set_adt_policy,
1748 NULL);
a7b4f989 1749
f4f5748b
AP
1750 if (ret) {
1751 nlmsg_free(skb2);
1752 return ret;
1753 }
a7b4f989
JK
1754 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1755
1756 *errline = lineno;
5f52bc3c 1757
e0241ae6 1758 nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
5f52bc3c
JK
1759 /* Signal netlink not to send its ACK/errmsg. */
1760 return -EINTR;
a7b4f989
JK
1761 }
1762
1763 return ret;
1764}
1765
f0cb8390
FF
1766static int ip_set_ad(struct net *net, struct sock *ctnl,
1767 struct sk_buff *skb,
1768 enum ipset_adt adt,
1769 const struct nlmsghdr *nlh,
1770 const struct nlattr * const attr[],
1771 struct netlink_ext_ack *extack)
a7b4f989 1772{
7b8002a1 1773 struct ip_set_net *inst = ip_set_pernet(net);
a7b4f989 1774 struct ip_set *set;
ca0f6a5c 1775 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
a7b4f989
JK
1776 const struct nlattr *nla;
1777 u32 flags = flag_exist(nlh);
1778 bool use_lineno;
1779 int ret = 0;
1780
23c42a40 1781 if (unlikely(protocol_min_failed(attr) ||
ca0f6a5c 1782 !attr[IPSET_ATTR_SETNAME] ||
a7b4f989
JK
1783 !((attr[IPSET_ATTR_DATA] != NULL) ^
1784 (attr[IPSET_ATTR_ADT] != NULL)) ||
ca0f6a5c 1785 (attr[IPSET_ATTR_DATA] &&
a7b4f989 1786 !flag_nested(attr[IPSET_ATTR_DATA])) ||
ca0f6a5c 1787 (attr[IPSET_ATTR_ADT] &&
a7b4f989 1788 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
ca0f6a5c 1789 !attr[IPSET_ATTR_LINENO]))))
a7b4f989
JK
1790 return -IPSET_ERR_PROTOCOL;
1791
1785e8f4 1792 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
ca0f6a5c 1793 if (!set)
a7b4f989
JK
1794 return -ENOENT;
1795
1796 use_lineno = !!attr[IPSET_ATTR_LINENO];
1797 if (attr[IPSET_ATTR_DATA]) {
12899756
JK
1798 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1799 attr[IPSET_ATTR_DATA],
1800 set->type->adt_policy, NULL))
a7b4f989 1801 return -IPSET_ERR_PROTOCOL;
e0241ae6 1802 ret = call_ad(net, ctnl, skb, set, tb, adt, flags,
5f52bc3c 1803 use_lineno);
a7b4f989
JK
1804 } else {
1805 int nla_rem;
1806
1807 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
a7b4f989
JK
1808 if (nla_type(nla) != IPSET_ATTR_DATA ||
1809 !flag_nested(nla) ||
12899756
JK
1810 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1811 set->type->adt_policy, NULL))
a7b4f989 1812 return -IPSET_ERR_PROTOCOL;
e0241ae6 1813 ret = call_ad(net, ctnl, skb, set, tb, adt,
a7b4f989
JK
1814 flags, use_lineno);
1815 if (ret < 0)
1816 return ret;
1817 }
1818 }
1819 return ret;
1820}
1821
a6555365
PNA
1822static int ip_set_uadd(struct sk_buff *skb, const struct nfnl_info *info,
1823 const struct nlattr * const attr[])
a7b4f989 1824{
a6555365
PNA
1825 return ip_set_ad(info->net, info->sk, skb,
1826 IPSET_ADD, info->nlh, attr, info->extack);
f0cb8390 1827}
a7b4f989 1828
a6555365
PNA
1829static int ip_set_udel(struct sk_buff *skb, const struct nfnl_info *info,
1830 const struct nlattr * const attr[])
f0cb8390 1831{
a6555365
PNA
1832 return ip_set_ad(info->net, info->sk, skb,
1833 IPSET_DEL, info->nlh, attr, info->extack);
a7b4f989
JK
1834}
1835
a6555365
PNA
1836static int ip_set_utest(struct sk_buff *skb, const struct nfnl_info *info,
1837 const struct nlattr * const attr[])
a7b4f989 1838{
a6555365 1839 struct ip_set_net *inst = ip_set_pernet(info->net);
a7b4f989 1840 struct ip_set *set;
ca0f6a5c 1841 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
a7b4f989 1842 int ret = 0;
22dad713 1843 u32 lineno;
a7b4f989 1844
23c42a40 1845 if (unlikely(protocol_min_failed(attr) ||
ca0f6a5c
JK
1846 !attr[IPSET_ATTR_SETNAME] ||
1847 !attr[IPSET_ATTR_DATA] ||
a7b4f989
JK
1848 !flag_nested(attr[IPSET_ATTR_DATA])))
1849 return -IPSET_ERR_PROTOCOL;
1850
1785e8f4 1851 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
ca0f6a5c 1852 if (!set)
a7b4f989
JK
1853 return -ENOENT;
1854
12899756
JK
1855 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1856 set->type->adt_policy, NULL))
a7b4f989
JK
1857 return -IPSET_ERR_PROTOCOL;
1858
b57b2d1f 1859 rcu_read_lock_bh();
22dad713 1860 ret = set->variant->uadt(set, tb, IPSET_TEST, &lineno, 0, 0);
b57b2d1f 1861 rcu_read_unlock_bh();
a7b4f989
JK
1862 /* Userspace can't trigger element to be re-added */
1863 if (ret == -EAGAIN)
1864 ret = 1;
1865
0f1799ba 1866 return ret > 0 ? 0 : -IPSET_ERR_EXIST;
a7b4f989
JK
1867}
1868
1869/* Get headed data of a set */
1870
a6555365
PNA
1871static int ip_set_header(struct sk_buff *skb, const struct nfnl_info *info,
1872 const struct nlattr * const attr[])
a7b4f989 1873{
a6555365 1874 struct ip_set_net *inst = ip_set_pernet(info->net);
a7b4f989
JK
1875 const struct ip_set *set;
1876 struct sk_buff *skb2;
1877 struct nlmsghdr *nlh2;
a7b4f989 1878
23c42a40 1879 if (unlikely(protocol_min_failed(attr) ||
ca0f6a5c 1880 !attr[IPSET_ATTR_SETNAME]))
a7b4f989
JK
1881 return -IPSET_ERR_PROTOCOL;
1882
1785e8f4 1883 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
ca0f6a5c 1884 if (!set)
a7b4f989 1885 return -ENOENT;
a7b4f989
JK
1886
1887 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
ca0f6a5c 1888 if (!skb2)
a7b4f989
JK
1889 return -ENOMEM;
1890
a6555365 1891 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
a7b4f989
JK
1892 IPSET_CMD_HEADER);
1893 if (!nlh2)
1894 goto nlmsg_failure;
23c42a40 1895 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
7cf7899d
DM
1896 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
1897 nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
1898 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
1899 nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
1900 goto nla_put_failure;
a7b4f989
JK
1901 nlmsg_end(skb2, nlh2);
1902
e0241ae6 1903 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
a7b4f989
JK
1904
1905nla_put_failure:
1906 nlmsg_cancel(skb2, nlh2);
1907nlmsg_failure:
1908 kfree_skb(skb2);
1909 return -EMSGSIZE;
1910}
1911
1912/* Get type data */
1913
1914static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1915 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1916 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
1917 .len = IPSET_MAXNAMELEN - 1 },
1918 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
1919};
1920
a6555365
PNA
1921static int ip_set_type(struct sk_buff *skb, const struct nfnl_info *info,
1922 const struct nlattr * const attr[])
a7b4f989
JK
1923{
1924 struct sk_buff *skb2;
1925 struct nlmsghdr *nlh2;
1926 u8 family, min, max;
1927 const char *typename;
1928 int ret = 0;
1929
23c42a40 1930 if (unlikely(protocol_min_failed(attr) ||
ca0f6a5c
JK
1931 !attr[IPSET_ATTR_TYPENAME] ||
1932 !attr[IPSET_ATTR_FAMILY]))
a7b4f989
JK
1933 return -IPSET_ERR_PROTOCOL;
1934
1935 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1936 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1937 ret = find_set_type_minmax(typename, family, &min, &max);
1938 if (ret)
1939 return ret;
1940
1941 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
ca0f6a5c 1942 if (!skb2)
a7b4f989
JK
1943 return -ENOMEM;
1944
a6555365 1945 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
a7b4f989
JK
1946 IPSET_CMD_TYPE);
1947 if (!nlh2)
1948 goto nlmsg_failure;
23c42a40 1949 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
7cf7899d
DM
1950 nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
1951 nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
1952 nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
1953 nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
1954 goto nla_put_failure;
a7b4f989
JK
1955 nlmsg_end(skb2, nlh2);
1956
1957 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
e0241ae6 1958 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
a7b4f989
JK
1959
1960nla_put_failure:
1961 nlmsg_cancel(skb2, nlh2);
1962nlmsg_failure:
1963 kfree_skb(skb2);
1964 return -EMSGSIZE;
1965}
1966
1967/* Get protocol version */
1968
1969static const struct nla_policy
1970ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1971 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1972};
1973
a6555365
PNA
1974static int ip_set_protocol(struct sk_buff *skb, const struct nfnl_info *info,
1975 const struct nlattr * const attr[])
a7b4f989
JK
1976{
1977 struct sk_buff *skb2;
1978 struct nlmsghdr *nlh2;
a7b4f989 1979
ca0f6a5c 1980 if (unlikely(!attr[IPSET_ATTR_PROTOCOL]))
a7b4f989
JK
1981 return -IPSET_ERR_PROTOCOL;
1982
1983 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
ca0f6a5c 1984 if (!skb2)
a7b4f989
JK
1985 return -ENOMEM;
1986
a6555365 1987 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
a7b4f989
JK
1988 IPSET_CMD_PROTOCOL);
1989 if (!nlh2)
1990 goto nlmsg_failure;
7cf7899d
DM
1991 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
1992 goto nla_put_failure;
23c42a40
JK
1993 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL_MIN, IPSET_PROTOCOL_MIN))
1994 goto nla_put_failure;
1995 nlmsg_end(skb2, nlh2);
1996
e0241ae6 1997 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
23c42a40
JK
1998
1999nla_put_failure:
2000 nlmsg_cancel(skb2, nlh2);
2001nlmsg_failure:
2002 kfree_skb(skb2);
2003 return -EMSGSIZE;
2004}
2005
2006/* Get set by name or index, from userspace */
2007
a6555365
PNA
2008static int ip_set_byname(struct sk_buff *skb, const struct nfnl_info *info,
2009 const struct nlattr * const attr[])
23c42a40 2010{
a6555365 2011 struct ip_set_net *inst = ip_set_pernet(info->net);
23c42a40
JK
2012 struct sk_buff *skb2;
2013 struct nlmsghdr *nlh2;
2014 ip_set_id_t id = IPSET_INVALID_ID;
2015 const struct ip_set *set;
23c42a40
JK
2016
2017 if (unlikely(protocol_failed(attr) ||
2018 !attr[IPSET_ATTR_SETNAME]))
2019 return -IPSET_ERR_PROTOCOL;
2020
2021 set = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]), &id);
2022 if (id == IPSET_INVALID_ID)
2023 return -ENOENT;
2024
2025 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2026 if (!skb2)
2027 return -ENOMEM;
2028
a6555365 2029 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
23c42a40
JK
2030 IPSET_CMD_GET_BYNAME);
2031 if (!nlh2)
2032 goto nlmsg_failure;
2033 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
2034 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
2035 nla_put_net16(skb2, IPSET_ATTR_INDEX, htons(id)))
2036 goto nla_put_failure;
2037 nlmsg_end(skb2, nlh2);
2038
e0241ae6 2039 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
23c42a40
JK
2040
2041nla_put_failure:
2042 nlmsg_cancel(skb2, nlh2);
2043nlmsg_failure:
2044 kfree_skb(skb2);
2045 return -EMSGSIZE;
2046}
2047
2048static const struct nla_policy ip_set_index_policy[IPSET_ATTR_CMD_MAX + 1] = {
2049 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
2050 [IPSET_ATTR_INDEX] = { .type = NLA_U16 },
2051};
2052
a6555365
PNA
2053static int ip_set_byindex(struct sk_buff *skb, const struct nfnl_info *info,
2054 const struct nlattr * const attr[])
23c42a40 2055{
a6555365 2056 struct ip_set_net *inst = ip_set_pernet(info->net);
23c42a40
JK
2057 struct sk_buff *skb2;
2058 struct nlmsghdr *nlh2;
2059 ip_set_id_t id = IPSET_INVALID_ID;
2060 const struct ip_set *set;
23c42a40
JK
2061
2062 if (unlikely(protocol_failed(attr) ||
2063 !attr[IPSET_ATTR_INDEX]))
2064 return -IPSET_ERR_PROTOCOL;
2065
2066 id = ip_set_get_h16(attr[IPSET_ATTR_INDEX]);
2067 if (id >= inst->ip_set_max)
2068 return -ENOENT;
2069 set = ip_set(inst, id);
2070 if (set == NULL)
2071 return -ENOENT;
2072
2073 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2074 if (!skb2)
2075 return -ENOMEM;
2076
a6555365 2077 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
23c42a40
JK
2078 IPSET_CMD_GET_BYINDEX);
2079 if (!nlh2)
2080 goto nlmsg_failure;
2081 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
8e350ce1 2082 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name))
23c42a40 2083 goto nla_put_failure;
a7b4f989
JK
2084 nlmsg_end(skb2, nlh2);
2085
e0241ae6 2086 return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
a7b4f989
JK
2087
2088nla_put_failure:
2089 nlmsg_cancel(skb2, nlh2);
2090nlmsg_failure:
2091 kfree_skb(skb2);
2092 return -EMSGSIZE;
2093}
2094
2095static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
d31f4d44
TB
2096 [IPSET_CMD_NONE] = {
2097 .call = ip_set_none,
50f2db9e 2098 .type = NFNL_CB_MUTEX,
d31f4d44
TB
2099 .attr_count = IPSET_ATTR_CMD_MAX,
2100 },
a7b4f989
JK
2101 [IPSET_CMD_CREATE] = {
2102 .call = ip_set_create,
50f2db9e 2103 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2104 .attr_count = IPSET_ATTR_CMD_MAX,
2105 .policy = ip_set_create_policy,
2106 },
2107 [IPSET_CMD_DESTROY] = {
2108 .call = ip_set_destroy,
50f2db9e 2109 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2110 .attr_count = IPSET_ATTR_CMD_MAX,
2111 .policy = ip_set_setname_policy,
2112 },
2113 [IPSET_CMD_FLUSH] = {
2114 .call = ip_set_flush,
50f2db9e 2115 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2116 .attr_count = IPSET_ATTR_CMD_MAX,
2117 .policy = ip_set_setname_policy,
2118 },
2119 [IPSET_CMD_RENAME] = {
2120 .call = ip_set_rename,
50f2db9e 2121 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2122 .attr_count = IPSET_ATTR_CMD_MAX,
2123 .policy = ip_set_setname2_policy,
2124 },
2125 [IPSET_CMD_SWAP] = {
2126 .call = ip_set_swap,
50f2db9e 2127 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2128 .attr_count = IPSET_ATTR_CMD_MAX,
2129 .policy = ip_set_setname2_policy,
2130 },
2131 [IPSET_CMD_LIST] = {
2132 .call = ip_set_dump,
50f2db9e 2133 .type = NFNL_CB_MUTEX,
a7b4f989 2134 .attr_count = IPSET_ATTR_CMD_MAX,
12899756 2135 .policy = ip_set_dump_policy,
a7b4f989
JK
2136 },
2137 [IPSET_CMD_SAVE] = {
2138 .call = ip_set_dump,
50f2db9e 2139 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2140 .attr_count = IPSET_ATTR_CMD_MAX,
2141 .policy = ip_set_setname_policy,
2142 },
2143 [IPSET_CMD_ADD] = {
2144 .call = ip_set_uadd,
50f2db9e 2145 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2146 .attr_count = IPSET_ATTR_CMD_MAX,
2147 .policy = ip_set_adt_policy,
2148 },
2149 [IPSET_CMD_DEL] = {
2150 .call = ip_set_udel,
50f2db9e 2151 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2152 .attr_count = IPSET_ATTR_CMD_MAX,
2153 .policy = ip_set_adt_policy,
2154 },
2155 [IPSET_CMD_TEST] = {
2156 .call = ip_set_utest,
50f2db9e 2157 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2158 .attr_count = IPSET_ATTR_CMD_MAX,
2159 .policy = ip_set_adt_policy,
2160 },
2161 [IPSET_CMD_HEADER] = {
2162 .call = ip_set_header,
50f2db9e 2163 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2164 .attr_count = IPSET_ATTR_CMD_MAX,
2165 .policy = ip_set_setname_policy,
2166 },
2167 [IPSET_CMD_TYPE] = {
2168 .call = ip_set_type,
50f2db9e 2169 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2170 .attr_count = IPSET_ATTR_CMD_MAX,
2171 .policy = ip_set_type_policy,
2172 },
2173 [IPSET_CMD_PROTOCOL] = {
2174 .call = ip_set_protocol,
50f2db9e 2175 .type = NFNL_CB_MUTEX,
a7b4f989
JK
2176 .attr_count = IPSET_ATTR_CMD_MAX,
2177 .policy = ip_set_protocol_policy,
2178 },
23c42a40
JK
2179 [IPSET_CMD_GET_BYNAME] = {
2180 .call = ip_set_byname,
50f2db9e 2181 .type = NFNL_CB_MUTEX,
23c42a40
JK
2182 .attr_count = IPSET_ATTR_CMD_MAX,
2183 .policy = ip_set_setname_policy,
2184 },
2185 [IPSET_CMD_GET_BYINDEX] = {
2186 .call = ip_set_byindex,
50f2db9e 2187 .type = NFNL_CB_MUTEX,
23c42a40
JK
2188 .attr_count = IPSET_ATTR_CMD_MAX,
2189 .policy = ip_set_index_policy,
2190 },
a7b4f989
JK
2191};
2192
2193static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
2194 .name = "ip_set",
2195 .subsys_id = NFNL_SUBSYS_IPSET,
2196 .cb_count = IPSET_MSG_MAX,
2197 .cb = ip_set_netlink_subsys_cb,
2198};
2199
2200/* Interface to iptables/ip6tables */
2201
2202static int
2203ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
2204{
95c96174 2205 unsigned int *op;
a7b4f989
JK
2206 void *data;
2207 int copylen = *len, ret = 0;
1785e8f4
VL
2208 struct net *net = sock_net(sk);
2209 struct ip_set_net *inst = ip_set_pernet(net);
a7b4f989 2210
1785e8f4 2211 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
a7b4f989
JK
2212 return -EPERM;
2213 if (optval != SO_IP_SET)
2214 return -EBADF;
95c96174 2215 if (*len < sizeof(unsigned int))
a7b4f989
JK
2216 return -EINVAL;
2217
2218 data = vmalloc(*len);
2219 if (!data)
2220 return -ENOMEM;
2221 if (copy_from_user(data, user, *len) != 0) {
2222 ret = -EFAULT;
2223 goto done;
2224 }
68ad546a 2225 op = data;
a7b4f989
JK
2226
2227 if (*op < IP_SET_OP_VERSION) {
2228 /* Check the version at the beginning of operations */
2229 struct ip_set_req_version *req_version = data;
2196937e
DC
2230
2231 if (*len < sizeof(struct ip_set_req_version)) {
2232 ret = -EINVAL;
2233 goto done;
2234 }
2235
23c42a40 2236 if (req_version->version < IPSET_PROTOCOL_MIN) {
a7b4f989
JK
2237 ret = -EPROTO;
2238 goto done;
2239 }
2240 }
2241
2242 switch (*op) {
2243 case IP_SET_OP_VERSION: {
2244 struct ip_set_req_version *req_version = data;
2245
2246 if (*len != sizeof(struct ip_set_req_version)) {
2247 ret = -EINVAL;
2248 goto done;
2249 }
2250
2251 req_version->version = IPSET_PROTOCOL;
30b7244d
DC
2252 if (copy_to_user(user, req_version,
2253 sizeof(struct ip_set_req_version)))
2254 ret = -EFAULT;
a7b4f989
JK
2255 goto done;
2256 }
2257 case IP_SET_OP_GET_BYNAME: {
2258 struct ip_set_req_get_set *req_get = data;
9076aea7 2259 ip_set_id_t id;
a7b4f989
JK
2260
2261 if (*len != sizeof(struct ip_set_req_get_set)) {
2262 ret = -EINVAL;
2263 goto done;
2264 }
2265 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
c14b78e7 2266 nfnl_lock(NFNL_SUBSYS_IPSET);
1785e8f4 2267 find_set_and_id(inst, req_get->set.name, &id);
9076aea7 2268 req_get->set.index = id;
c14b78e7 2269 nfnl_unlock(NFNL_SUBSYS_IPSET);
a7b4f989
JK
2270 goto copy;
2271 }
5e04c0c3
JK
2272 case IP_SET_OP_GET_FNAME: {
2273 struct ip_set_req_get_set_family *req_get = data;
2274 ip_set_id_t id;
2275
2276 if (*len != sizeof(struct ip_set_req_get_set_family)) {
2277 ret = -EINVAL;
2278 goto done;
2279 }
2280 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2281 nfnl_lock(NFNL_SUBSYS_IPSET);
1785e8f4 2282 find_set_and_id(inst, req_get->set.name, &id);
5e04c0c3
JK
2283 req_get->set.index = id;
2284 if (id != IPSET_INVALID_ID)
3e90ebd3 2285 req_get->family = ip_set(inst, id)->family;
5e04c0c3
JK
2286 nfnl_unlock(NFNL_SUBSYS_IPSET);
2287 goto copy;
2288 }
a7b4f989
JK
2289 case IP_SET_OP_GET_BYINDEX: {
2290 struct ip_set_req_get_set *req_get = data;
9076aea7 2291 struct ip_set *set;
a7b4f989
JK
2292
2293 if (*len != sizeof(struct ip_set_req_get_set) ||
1785e8f4 2294 req_get->set.index >= inst->ip_set_max) {
a7b4f989
JK
2295 ret = -EINVAL;
2296 goto done;
2297 }
c14b78e7 2298 nfnl_lock(NFNL_SUBSYS_IPSET);
3e90ebd3 2299 set = ip_set(inst, req_get->set.index);
00ec3ab0
QC
2300 ret = strscpy(req_get->set.name, set ? set->name : "",
2301 IPSET_MAXNAMELEN);
c14b78e7 2302 nfnl_unlock(NFNL_SUBSYS_IPSET);
00ec3ab0
QC
2303 if (ret < 0)
2304 goto done;
a7b4f989
JK
2305 goto copy;
2306 }
2307 default:
2308 ret = -EBADMSG;
2309 goto done;
2310 } /* end of switch(op) */
2311
2312copy:
30b7244d
DC
2313 if (copy_to_user(user, data, copylen))
2314 ret = -EFAULT;
a7b4f989
JK
2315
2316done:
2317 vfree(data);
2318 if (ret > 0)
2319 ret = 0;
2320 return ret;
2321}
2322
2323static struct nf_sockopt_ops so_set __read_mostly = {
2324 .pf = PF_INET,
2325 .get_optmin = SO_IP_SET,
2326 .get_optmax = SO_IP_SET + 1,
d4ef3835 2327 .get = ip_set_sockfn_get,
a7b4f989
JK
2328 .owner = THIS_MODULE,
2329};
2330
1785e8f4
VL
2331static int __net_init
2332ip_set_net_init(struct net *net)
a7b4f989 2333{
1785e8f4 2334 struct ip_set_net *inst = ip_set_pernet(net);
9076aea7 2335 struct ip_set **list;
a7b4f989 2336
1785e8f4
VL
2337 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
2338 if (inst->ip_set_max >= IPSET_INVALID_ID)
2339 inst->ip_set_max = IPSET_INVALID_ID - 1;
a7b4f989 2340
ed956f39 2341 list = kvcalloc(inst->ip_set_max, sizeof(struct ip_set *), GFP_KERNEL);
9076aea7 2342 if (!list)
a7b4f989 2343 return -ENOMEM;
9c1ba5c8
JK
2344 inst->is_deleted = false;
2345 inst->is_destroyed = false;
1785e8f4 2346 rcu_assign_pointer(inst->ip_set_list, list);
1785e8f4
VL
2347 return 0;
2348}
2349
2350static void __net_exit
2351ip_set_net_exit(struct net *net)
2352{
2353 struct ip_set_net *inst = ip_set_pernet(net);
2354
2355 struct ip_set *set = NULL;
2356 ip_set_id_t i;
2357
9c1ba5c8 2358 inst->is_deleted = true; /* flag for ip_set_nfnl_put */
1785e8f4 2359
f998b6b1 2360 nfnl_lock(NFNL_SUBSYS_IPSET);
1785e8f4 2361 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 2362 set = ip_set(inst, i);
9c1ba5c8
JK
2363 if (set) {
2364 ip_set(inst, i) = NULL;
2365 ip_set_destroy_set(set);
2366 }
1785e8f4 2367 }
f998b6b1 2368 nfnl_unlock(NFNL_SUBSYS_IPSET);
ed956f39 2369 kvfree(rcu_dereference_protected(inst->ip_set_list, 1));
1785e8f4
VL
2370}
2371
2372static struct pernet_operations ip_set_net_ops = {
2373 .init = ip_set_net_init,
2374 .exit = ip_set_net_exit,
2375 .id = &ip_set_net_id,
a5a179b6 2376 .size = sizeof(struct ip_set_net),
1785e8f4
VL
2377};
2378
1785e8f4
VL
2379static int __init
2380ip_set_init(void)
2381{
e23ed762 2382 int ret = register_pernet_subsys(&ip_set_net_ops);
ca0f6a5c 2383
e23ed762
FW
2384 if (ret) {
2385 pr_err("ip_set: cannot register pernet_subsys.\n");
2386 return ret;
2387 }
2388
2389 ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
a7b4f989
JK
2390 if (ret != 0) {
2391 pr_err("ip_set: cannot register with nfnetlink.\n");
e23ed762 2392 unregister_pernet_subsys(&ip_set_net_ops);
a7b4f989
JK
2393 return ret;
2394 }
e23ed762 2395
a7b4f989
JK
2396 ret = nf_register_sockopt(&so_set);
2397 if (ret != 0) {
2398 pr_err("SO_SET registry failed: %d\n", ret);
2399 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
e23ed762 2400 unregister_pernet_subsys(&ip_set_net_ops);
a7b4f989
JK
2401 return ret;
2402 }
e23ed762 2403
a7b4f989
JK
2404 return 0;
2405}
2406
2407static void __exit
2408ip_set_fini(void)
2409{
a7b4f989
JK
2410 nf_unregister_sockopt(&so_set);
2411 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
e23ed762
FW
2412
2413 unregister_pernet_subsys(&ip_set_net_ops);
a7b4f989
JK
2414 pr_debug("these are the famous last words\n");
2415}
2416
2417module_init(ip_set_init);
2418module_exit(ip_set_fini);
e5531166
PNA
2419
2420MODULE_DESCRIPTION("ip_set: protocol " __stringify(IPSET_PROTOCOL));