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