]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/ipset/ip_set_core.c
iucv: Convert pr_warning to pr_warn
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / ipset / ip_set_core.c
CommitLineData
a7b4f989
JK
1/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
075e64c0 3 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
a7b4f989
JK
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10/* Kernel module for IP set management */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/ip.h>
16#include <linux/skbuff.h>
17#include <linux/spinlock.h>
a7b4f989 18#include <linux/rculist.h>
a7b4f989 19#include <net/netlink.h>
1785e8f4
VL
20#include <net/net_namespace.h>
21#include <net/netns/generic.h>
a7b4f989
JK
22
23#include <linux/netfilter.h>
b66554cf 24#include <linux/netfilter/x_tables.h>
a7b4f989
JK
25#include <linux/netfilter/nfnetlink.h>
26#include <linux/netfilter/ipset/ip_set.h>
27
28static LIST_HEAD(ip_set_type_list); /* all registered set types */
29static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */
2f9f28b2 30static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */
a7b4f989 31
1785e8f4
VL
32struct ip_set_net {
33 struct ip_set * __rcu *ip_set_list; /* all individual sets */
34 ip_set_id_t ip_set_max; /* max number of sets */
35 int is_deleted; /* deleted by ip_set_net_exit */
36};
37static int ip_set_net_id __read_mostly;
38
39static inline struct ip_set_net *ip_set_pernet(struct net *net)
40{
41 return net_generic(net, ip_set_net_id);
42}
a7b4f989 43
9076aea7 44#define IP_SET_INC 64
a7b4f989
JK
45#define STREQ(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
46
47static unsigned int max_sets;
48
49module_param(max_sets, int, 0600);
50MODULE_PARM_DESC(max_sets, "maximal number of sets");
51MODULE_LICENSE("GPL");
52MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
53MODULE_DESCRIPTION("core IP set support");
54MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
55
9076aea7 56/* When the nfnl mutex is held: */
3e90ebd3 57#define ip_set_dereference(p) \
9076aea7 58 rcu_dereference_protected(p, 1)
3e90ebd3
PM
59#define ip_set(inst, id) \
60 ip_set_dereference((inst)->ip_set_list)[id]
9076aea7 61
a7b4f989
JK
62/*
63 * The set types are implemented in modules and registered set types
64 * can be found in ip_set_type_list. Adding/deleting types is
65 * serialized by ip_set_type_mutex.
66 */
67
68static inline void
69ip_set_type_lock(void)
70{
71 mutex_lock(&ip_set_type_mutex);
72}
73
74static inline void
75ip_set_type_unlock(void)
76{
77 mutex_unlock(&ip_set_type_mutex);
78}
79
80/* Register and deregister settype */
81
82static struct ip_set_type *
83find_set_type(const char *name, u8 family, u8 revision)
84{
85 struct ip_set_type *type;
86
87 list_for_each_entry_rcu(type, &ip_set_type_list, list)
88 if (STREQ(type->name, name) &&
3ace95c0
JK
89 (type->family == family ||
90 type->family == NFPROTO_UNSPEC) &&
f1e00b39
JK
91 revision >= type->revision_min &&
92 revision <= type->revision_max)
a7b4f989
JK
93 return type;
94 return NULL;
95}
96
97/* Unlock, try to load a set type module and lock again */
088067f4
JK
98static bool
99load_settype(const char *name)
a7b4f989 100{
c14b78e7 101 nfnl_unlock(NFNL_SUBSYS_IPSET);
a7b4f989
JK
102 pr_debug("try to load ip_set_%s\n", name);
103 if (request_module("ip_set_%s", name) < 0) {
104 pr_warning("Can't find ip_set type %s\n", name);
c14b78e7 105 nfnl_lock(NFNL_SUBSYS_IPSET);
088067f4 106 return false;
a7b4f989 107 }
c14b78e7 108 nfnl_lock(NFNL_SUBSYS_IPSET);
088067f4 109 return true;
a7b4f989
JK
110}
111
112/* Find a set type and reference it */
088067f4
JK
113#define find_set_type_get(name, family, revision, found) \
114 __find_set_type_get(name, family, revision, found, false)
115
a7b4f989 116static int
088067f4
JK
117__find_set_type_get(const char *name, u8 family, u8 revision,
118 struct ip_set_type **found, bool retry)
a7b4f989 119{
5c1aba46
JK
120 struct ip_set_type *type;
121 int err;
122
088067f4
JK
123 if (retry && !load_settype(name))
124 return -IPSET_ERR_FIND_TYPE;
125
a7b4f989
JK
126 rcu_read_lock();
127 *found = find_set_type(name, family, revision);
128 if (*found) {
5c1aba46
JK
129 err = !try_module_get((*found)->me) ? -EFAULT : 0;
130 goto unlock;
a7b4f989 131 }
088067f4
JK
132 /* Make sure the type is already loaded
133 * but we don't support the revision */
5c1aba46
JK
134 list_for_each_entry_rcu(type, &ip_set_type_list, list)
135 if (STREQ(type->name, name)) {
136 err = -IPSET_ERR_FIND_TYPE;
137 goto unlock;
138 }
a7b4f989
JK
139 rcu_read_unlock();
140
088067f4
JK
141 return retry ? -IPSET_ERR_FIND_TYPE :
142 __find_set_type_get(name, family, revision, found, true);
5c1aba46
JK
143
144unlock:
145 rcu_read_unlock();
146 return err;
a7b4f989
JK
147}
148
149/* Find a given set type by name and family.
150 * If we succeeded, the supported minimal and maximum revisions are
151 * filled out.
152 */
088067f4
JK
153#define find_set_type_minmax(name, family, min, max) \
154 __find_set_type_minmax(name, family, min, max, false)
155
a7b4f989 156static int
088067f4
JK
157__find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max,
158 bool retry)
a7b4f989
JK
159{
160 struct ip_set_type *type;
161 bool found = false;
162
088067f4
JK
163 if (retry && !load_settype(name))
164 return -IPSET_ERR_FIND_TYPE;
165
5c1aba46 166 *min = 255; *max = 0;
a7b4f989
JK
167 rcu_read_lock();
168 list_for_each_entry_rcu(type, &ip_set_type_list, list)
169 if (STREQ(type->name, name) &&
3ace95c0
JK
170 (type->family == family ||
171 type->family == NFPROTO_UNSPEC)) {
a7b4f989 172 found = true;
f1e00b39
JK
173 if (type->revision_min < *min)
174 *min = type->revision_min;
175 if (type->revision_max > *max)
176 *max = type->revision_max;
a7b4f989
JK
177 }
178 rcu_read_unlock();
179 if (found)
180 return 0;
181
088067f4
JK
182 return retry ? -IPSET_ERR_FIND_TYPE :
183 __find_set_type_minmax(name, family, min, max, true);
a7b4f989
JK
184}
185
c15f1c83
JE
186#define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \
187 (f) == NFPROTO_IPV6 ? "inet6" : "any")
a7b4f989
JK
188
189/* Register a set type structure. The type is identified by
190 * the unique triple of name, family and revision.
191 */
192int
193ip_set_type_register(struct ip_set_type *type)
194{
195 int ret = 0;
196
197 if (type->protocol != IPSET_PROTOCOL) {
f1e00b39 198 pr_warning("ip_set type %s, family %s, revision %u:%u uses "
a7b4f989
JK
199 "wrong protocol version %u (want %u)\n",
200 type->name, family_name(type->family),
f1e00b39
JK
201 type->revision_min, type->revision_max,
202 type->protocol, IPSET_PROTOCOL);
a7b4f989
JK
203 return -EINVAL;
204 }
205
206 ip_set_type_lock();
f1e00b39 207 if (find_set_type(type->name, type->family, type->revision_min)) {
a7b4f989 208 /* Duplicate! */
f1e00b39 209 pr_warning("ip_set type %s, family %s with revision min %u "
a7b4f989 210 "already registered!\n", type->name,
f1e00b39 211 family_name(type->family), type->revision_min);
a7b4f989
JK
212 ret = -EINVAL;
213 goto unlock;
214 }
215 list_add_rcu(&type->list, &ip_set_type_list);
f1e00b39
JK
216 pr_debug("type %s, family %s, revision %u:%u registered.\n",
217 type->name, family_name(type->family),
218 type->revision_min, type->revision_max);
a7b4f989
JK
219unlock:
220 ip_set_type_unlock();
221 return ret;
222}
223EXPORT_SYMBOL_GPL(ip_set_type_register);
224
225/* Unregister a set type. There's a small race with ip_set_create */
226void
227ip_set_type_unregister(struct ip_set_type *type)
228{
229 ip_set_type_lock();
f1e00b39
JK
230 if (!find_set_type(type->name, type->family, type->revision_min)) {
231 pr_warning("ip_set type %s, family %s with revision min %u "
a7b4f989 232 "not registered\n", type->name,
f1e00b39 233 family_name(type->family), type->revision_min);
a7b4f989
JK
234 goto unlock;
235 }
236 list_del_rcu(&type->list);
f1e00b39
JK
237 pr_debug("type %s, family %s with revision min %u unregistered.\n",
238 type->name, family_name(type->family), type->revision_min);
a7b4f989
JK
239unlock:
240 ip_set_type_unlock();
241
242 synchronize_rcu();
243}
244EXPORT_SYMBOL_GPL(ip_set_type_unregister);
245
246/* Utility functions */
247void *
248ip_set_alloc(size_t size)
249{
250 void *members = NULL;
251
252 if (size < KMALLOC_MAX_SIZE)
253 members = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
254
255 if (members) {
256 pr_debug("%p: allocated with kmalloc\n", members);
257 return members;
258 }
259
260 members = vzalloc(size);
261 if (!members)
262 return NULL;
263 pr_debug("%p: allocated with vmalloc\n", members);
264
265 return members;
266}
267EXPORT_SYMBOL_GPL(ip_set_alloc);
268
269void
270ip_set_free(void *members)
271{
272 pr_debug("%p: free with %s\n", members,
273 is_vmalloc_addr(members) ? "vfree" : "kfree");
4cb28970 274 kvfree(members);
a7b4f989
JK
275}
276EXPORT_SYMBOL_GPL(ip_set_free);
277
278static inline bool
279flag_nested(const struct nlattr *nla)
280{
281 return nla->nla_type & NLA_F_NESTED;
282}
283
284static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
285 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
286 [IPSET_ATTR_IPADDR_IPV6] = { .type = NLA_BINARY,
287 .len = sizeof(struct in6_addr) },
288};
289
290int
291ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
292{
293 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX+1];
294
295 if (unlikely(!flag_nested(nla)))
296 return -IPSET_ERR_PROTOCOL;
8da560ce 297 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, ipaddr_policy))
a7b4f989
JK
298 return -IPSET_ERR_PROTOCOL;
299 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
300 return -IPSET_ERR_PROTOCOL;
301
302 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
303 return 0;
304}
305EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
306
307int
308ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
309{
310 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX+1];
311
312 if (unlikely(!flag_nested(nla)))
313 return -IPSET_ERR_PROTOCOL;
314
8da560ce 315 if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, ipaddr_policy))
a7b4f989
JK
316 return -IPSET_ERR_PROTOCOL;
317 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
318 return -IPSET_ERR_PROTOCOL;
319
320 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
321 sizeof(struct in6_addr));
322 return 0;
323}
324EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
325
68b63f08 326typedef void (*destroyer)(void *);
03c8b234
JK
327/* ipset data extension types, in size order */
328
329const struct ip_set_ext_type ip_set_extensions[] = {
330 [IPSET_EXT_ID_COUNTER] = {
331 .type = IPSET_EXT_COUNTER,
332 .flag = IPSET_FLAG_WITH_COUNTERS,
333 .len = sizeof(struct ip_set_counter),
334 .align = __alignof__(struct ip_set_counter),
335 },
336 [IPSET_EXT_ID_TIMEOUT] = {
337 .type = IPSET_EXT_TIMEOUT,
338 .len = sizeof(unsigned long),
339 .align = __alignof__(unsigned long),
340 },
68b63f08
OS
341 [IPSET_EXT_ID_COMMENT] = {
342 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
343 .flag = IPSET_FLAG_WITH_COMMENT,
344 .len = sizeof(struct ip_set_comment),
345 .align = __alignof__(struct ip_set_comment),
346 .destroy = (destroyer) ip_set_comment_free,
347 },
03c8b234
JK
348};
349EXPORT_SYMBOL_GPL(ip_set_extensions);
350
351static inline bool
352add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
353{
354 return ip_set_extensions[id].flag ?
355 (flags & ip_set_extensions[id].flag) :
356 !!tb[IPSET_ATTR_TIMEOUT];
357}
358
359size_t
360ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len)
361{
362 enum ip_set_ext_id id;
363 size_t offset = 0;
364 u32 cadt_flags = 0;
365
366 if (tb[IPSET_ATTR_CADT_FLAGS])
367 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
07cf8f5a
JH
368 if (cadt_flags & IPSET_FLAG_WITH_FORCEADD)
369 set->flags |= IPSET_CREATE_FLAG_FORCEADD;
03c8b234
JK
370 for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
371 if (!add_extension(id, cadt_flags, tb))
372 continue;
373 offset += ALIGN(len + offset, ip_set_extensions[id].align);
374 set->offset[id] = offset;
375 set->extensions |= ip_set_extensions[id].type;
376 offset += ip_set_extensions[id].len;
377 }
378 return len + offset;
379}
380EXPORT_SYMBOL_GPL(ip_set_elem_len);
381
075e64c0
JK
382int
383ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
384 struct ip_set_ext *ext)
385{
386 if (tb[IPSET_ATTR_TIMEOUT]) {
387 if (!(set->extensions & IPSET_EXT_TIMEOUT))
388 return -IPSET_ERR_TIMEOUT;
389 ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
390 }
34d666d4
JK
391 if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) {
392 if (!(set->extensions & IPSET_EXT_COUNTER))
393 return -IPSET_ERR_COUNTER;
394 if (tb[IPSET_ATTR_BYTES])
395 ext->bytes = be64_to_cpu(nla_get_be64(
396 tb[IPSET_ATTR_BYTES]));
397 if (tb[IPSET_ATTR_PACKETS])
398 ext->packets = be64_to_cpu(nla_get_be64(
399 tb[IPSET_ATTR_PACKETS]));
400 }
68b63f08
OS
401 if (tb[IPSET_ATTR_COMMENT]) {
402 if (!(set->extensions & IPSET_EXT_COMMENT))
403 return -IPSET_ERR_COMMENT;
404 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
405 }
406
075e64c0
JK
407 return 0;
408}
409EXPORT_SYMBOL_GPL(ip_set_get_extensions);
410
a7b4f989
JK
411/*
412 * Creating/destroying/renaming/swapping affect the existence and
413 * the properties of a set. All of these can be executed from userspace
414 * only and serialized by the nfnl mutex indirectly from nfnetlink.
415 *
416 * Sets are identified by their index in ip_set_list and the index
417 * is used by the external references (set/SET netfilter modules).
418 *
419 * The set behind an index may change by swapping only, from userspace.
420 */
421
422static inline void
9076aea7 423__ip_set_get(struct ip_set *set)
a7b4f989 424{
2f9f28b2 425 write_lock_bh(&ip_set_ref_lock);
9076aea7 426 set->ref++;
2f9f28b2 427 write_unlock_bh(&ip_set_ref_lock);
a7b4f989
JK
428}
429
430static inline void
9076aea7 431__ip_set_put(struct ip_set *set)
a7b4f989 432{
2f9f28b2 433 write_lock_bh(&ip_set_ref_lock);
9076aea7
JK
434 BUG_ON(set->ref == 0);
435 set->ref--;
2f9f28b2 436 write_unlock_bh(&ip_set_ref_lock);
a7b4f989
JK
437}
438
439/*
440 * Add, del and test set entries from kernel.
441 *
442 * The set behind the index must exist and must be referenced
443 * so it can't be destroyed (or changed) under our foot.
444 */
445
9076aea7 446static inline struct ip_set *
1785e8f4 447ip_set_rcu_get(struct net *net, ip_set_id_t index)
9076aea7
JK
448{
449 struct ip_set *set;
1785e8f4 450 struct ip_set_net *inst = ip_set_pernet(net);
9076aea7
JK
451
452 rcu_read_lock();
453 /* ip_set_list itself needs to be protected */
1785e8f4 454 set = rcu_dereference(inst->ip_set_list)[index];
9076aea7
JK
455 rcu_read_unlock();
456
457 return set;
458}
459
a7b4f989
JK
460int
461ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
075e64c0 462 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
a7b4f989 463{
1785e8f4
VL
464 struct ip_set *set = ip_set_rcu_get(
465 dev_net(par->in ? par->in : par->out), index);
a7b4f989
JK
466 int ret = 0;
467
2f9f28b2 468 BUG_ON(set == NULL);
a7b4f989
JK
469 pr_debug("set %s, index %u\n", set->name, index);
470
ac8cc925 471 if (opt->dim < set->type->dimension ||
c15f1c83 472 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
a7b4f989
JK
473 return 0;
474
475 read_lock_bh(&set->lock);
b66554cf 476 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
a7b4f989
JK
477 read_unlock_bh(&set->lock);
478
479 if (ret == -EAGAIN) {
480 /* Type requests element to be completed */
481 pr_debug("element must be competed, ADD is triggered\n");
482 write_lock_bh(&set->lock);
b66554cf 483 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
a7b4f989
JK
484 write_unlock_bh(&set->lock);
485 ret = 1;
3e0304a5
JK
486 } else {
487 /* --return-nomatch: invert matched element */
6e01781d 488 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
3e0304a5
JK
489 (set->type->features & IPSET_TYPE_NOMATCH) &&
490 (ret > 0 || ret == -ENOTEMPTY))
491 ret = -ret;
a7b4f989
JK
492 }
493
494 /* Convert error codes to nomatch */
495 return (ret < 0 ? 0 : ret);
496}
497EXPORT_SYMBOL_GPL(ip_set_test);
498
499int
500ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
075e64c0 501 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
a7b4f989 502{
1785e8f4
VL
503 struct ip_set *set = ip_set_rcu_get(
504 dev_net(par->in ? par->in : par->out), index);
a7b4f989
JK
505 int ret;
506
2f9f28b2 507 BUG_ON(set == NULL);
a7b4f989
JK
508 pr_debug("set %s, index %u\n", set->name, index);
509
ac8cc925 510 if (opt->dim < set->type->dimension ||
c15f1c83 511 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
35f6e63a 512 return -IPSET_ERR_TYPE_MISMATCH;
a7b4f989
JK
513
514 write_lock_bh(&set->lock);
b66554cf 515 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
a7b4f989
JK
516 write_unlock_bh(&set->lock);
517
518 return ret;
519}
520EXPORT_SYMBOL_GPL(ip_set_add);
521
522int
523ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
075e64c0 524 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
a7b4f989 525{
1785e8f4
VL
526 struct ip_set *set = ip_set_rcu_get(
527 dev_net(par->in ? par->in : par->out), index);
a7b4f989
JK
528 int ret = 0;
529
2f9f28b2 530 BUG_ON(set == NULL);
a7b4f989
JK
531 pr_debug("set %s, index %u\n", set->name, index);
532
ac8cc925 533 if (opt->dim < set->type->dimension ||
c15f1c83 534 !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
35f6e63a 535 return -IPSET_ERR_TYPE_MISMATCH;
a7b4f989
JK
536
537 write_lock_bh(&set->lock);
b66554cf 538 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
a7b4f989
JK
539 write_unlock_bh(&set->lock);
540
541 return ret;
542}
543EXPORT_SYMBOL_GPL(ip_set_del);
544
545/*
546 * Find set by name, reference it once. The reference makes sure the
547 * thing pointed to, does not go away under our feet.
548 *
a7b4f989
JK
549 */
550ip_set_id_t
1785e8f4 551ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
a7b4f989
JK
552{
553 ip_set_id_t i, index = IPSET_INVALID_ID;
554 struct ip_set *s;
1785e8f4 555 struct ip_set_net *inst = ip_set_pernet(net);
a7b4f989 556
9076aea7 557 rcu_read_lock();
1785e8f4
VL
558 for (i = 0; i < inst->ip_set_max; i++) {
559 s = rcu_dereference(inst->ip_set_list)[i];
a7b4f989 560 if (s != NULL && STREQ(s->name, name)) {
9076aea7 561 __ip_set_get(s);
a7b4f989
JK
562 index = i;
563 *set = s;
9076aea7 564 break;
a7b4f989
JK
565 }
566 }
9076aea7 567 rcu_read_unlock();
a7b4f989
JK
568
569 return index;
570}
571EXPORT_SYMBOL_GPL(ip_set_get_byname);
572
573/*
574 * If the given set pointer points to a valid set, decrement
575 * reference count by 1. The caller shall not assume the index
576 * to be valid, after calling this function.
577 *
a7b4f989 578 */
1785e8f4
VL
579
580static inline void
581__ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
a7b4f989 582{
9076aea7
JK
583 struct ip_set *set;
584
585 rcu_read_lock();
1785e8f4 586 set = rcu_dereference(inst->ip_set_list)[index];
9076aea7
JK
587 if (set != NULL)
588 __ip_set_put(set);
589 rcu_read_unlock();
a7b4f989 590}
1785e8f4
VL
591
592void
593ip_set_put_byindex(struct net *net, ip_set_id_t index)
594{
595 struct ip_set_net *inst = ip_set_pernet(net);
596
597 __ip_set_put_byindex(inst, index);
598}
a7b4f989
JK
599EXPORT_SYMBOL_GPL(ip_set_put_byindex);
600
601/*
602 * Get the name of a set behind a set index.
603 * We assume the set is referenced, so it does exist and
604 * can't be destroyed. The set cannot be renamed due to
605 * the referencing either.
606 *
a7b4f989
JK
607 */
608const char *
1785e8f4 609ip_set_name_byindex(struct net *net, ip_set_id_t index)
a7b4f989 610{
1785e8f4 611 const struct ip_set *set = ip_set_rcu_get(net, index);
a7b4f989
JK
612
613 BUG_ON(set == NULL);
2f9f28b2 614 BUG_ON(set->ref == 0);
a7b4f989
JK
615
616 /* Referenced, so it's safe */
617 return set->name;
618}
619EXPORT_SYMBOL_GPL(ip_set_name_byindex);
620
621/*
622 * Routines to call by external subsystems, which do not
623 * call nfnl_lock for us.
624 */
625
a7b4f989
JK
626/*
627 * Find set by index, reference it once. The reference makes sure the
628 * thing pointed to, does not go away under our feet.
629 *
630 * The nfnl mutex is used in the function.
631 */
632ip_set_id_t
1785e8f4 633ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
a7b4f989 634{
9076aea7 635 struct ip_set *set;
1785e8f4 636 struct ip_set_net *inst = ip_set_pernet(net);
9076aea7 637
1785e8f4 638 if (index > inst->ip_set_max)
a7b4f989
JK
639 return IPSET_INVALID_ID;
640
c14b78e7 641 nfnl_lock(NFNL_SUBSYS_IPSET);
3e90ebd3 642 set = ip_set(inst, index);
9076aea7
JK
643 if (set)
644 __ip_set_get(set);
a7b4f989
JK
645 else
646 index = IPSET_INVALID_ID;
c14b78e7 647 nfnl_unlock(NFNL_SUBSYS_IPSET);
a7b4f989
JK
648
649 return index;
650}
651EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
652
653/*
654 * If the given set pointer points to a valid set, decrement
655 * reference count by 1. The caller shall not assume the index
656 * to be valid, after calling this function.
657 *
658 * The nfnl mutex is used in the function.
659 */
660void
1785e8f4 661ip_set_nfnl_put(struct net *net, ip_set_id_t index)
a7b4f989 662{
9076aea7 663 struct ip_set *set;
1785e8f4
VL
664 struct ip_set_net *inst = ip_set_pernet(net);
665
c14b78e7 666 nfnl_lock(NFNL_SUBSYS_IPSET);
1785e8f4 667 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
3e90ebd3 668 set = ip_set(inst, index);
1785e8f4
VL
669 if (set != NULL)
670 __ip_set_put(set);
671 }
c14b78e7 672 nfnl_unlock(NFNL_SUBSYS_IPSET);
a7b4f989
JK
673}
674EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
675
676/*
677 * Communication protocol with userspace over netlink.
678 *
2f9f28b2 679 * The commands are serialized by the nfnl mutex.
a7b4f989
JK
680 */
681
682static inline bool
683protocol_failed(const struct nlattr * const tb[])
684{
685 return !tb[IPSET_ATTR_PROTOCOL] ||
686 nla_get_u8(tb[IPSET_ATTR_PROTOCOL]) != IPSET_PROTOCOL;
687}
688
689static inline u32
690flag_exist(const struct nlmsghdr *nlh)
691{
692 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
693}
694
695static struct nlmsghdr *
15e47304 696start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
a7b4f989
JK
697 enum ipset_cmd cmd)
698{
699 struct nlmsghdr *nlh;
700 struct nfgenmsg *nfmsg;
701
15e47304 702 nlh = nlmsg_put(skb, portid, seq, cmd | (NFNL_SUBSYS_IPSET << 8),
a7b4f989
JK
703 sizeof(*nfmsg), flags);
704 if (nlh == NULL)
705 return NULL;
706
707 nfmsg = nlmsg_data(nlh);
c15f1c83 708 nfmsg->nfgen_family = NFPROTO_IPV4;
a7b4f989
JK
709 nfmsg->version = NFNETLINK_V0;
710 nfmsg->res_id = 0;
711
712 return nlh;
713}
714
715/* Create a set */
716
717static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
718 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
719 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
720 .len = IPSET_MAXNAMELEN - 1 },
721 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
722 .len = IPSET_MAXNAMELEN - 1},
723 [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
724 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
725 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
726};
727
9076aea7 728static struct ip_set *
1785e8f4 729find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
a7b4f989 730{
9076aea7
JK
731 struct ip_set *set = NULL;
732 ip_set_id_t i;
a7b4f989 733
9076aea7 734 *id = IPSET_INVALID_ID;
1785e8f4 735 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 736 set = ip_set(inst, i);
9076aea7
JK
737 if (set != NULL && STREQ(set->name, name)) {
738 *id = i;
739 break;
740 }
a7b4f989 741 }
9076aea7 742 return (*id == IPSET_INVALID_ID ? NULL : set);
a7b4f989
JK
743}
744
745static inline struct ip_set *
1785e8f4 746find_set(struct ip_set_net *inst, const char *name)
a7b4f989 747{
9076aea7 748 ip_set_id_t id;
a7b4f989 749
1785e8f4 750 return find_set_and_id(inst, name, &id);
a7b4f989
JK
751}
752
753static int
1785e8f4
VL
754find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
755 struct ip_set **set)
a7b4f989 756{
9076aea7 757 struct ip_set *s;
a7b4f989
JK
758 ip_set_id_t i;
759
760 *index = IPSET_INVALID_ID;
1785e8f4 761 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 762 s = ip_set(inst, i);
9076aea7 763 if (s == NULL) {
a7b4f989
JK
764 if (*index == IPSET_INVALID_ID)
765 *index = i;
9076aea7 766 } else if (STREQ(name, s->name)) {
a7b4f989 767 /* Name clash */
9076aea7 768 *set = s;
a7b4f989
JK
769 return -EEXIST;
770 }
771 }
772 if (*index == IPSET_INVALID_ID)
773 /* No free slot remained */
774 return -IPSET_ERR_MAX_SETS;
775 return 0;
776}
777
d31f4d44
TB
778static int
779ip_set_none(struct sock *ctnl, struct sk_buff *skb,
780 const struct nlmsghdr *nlh,
781 const struct nlattr * const attr[])
782{
783 return -EOPNOTSUPP;
784}
785
a7b4f989
JK
786static int
787ip_set_create(struct sock *ctnl, struct sk_buff *skb,
788 const struct nlmsghdr *nlh,
789 const struct nlattr * const attr[])
790{
1785e8f4
VL
791 struct net *net = sock_net(ctnl);
792 struct ip_set_net *inst = ip_set_pernet(net);
9846ada1 793 struct ip_set *set, *clash = NULL;
a7b4f989
JK
794 ip_set_id_t index = IPSET_INVALID_ID;
795 struct nlattr *tb[IPSET_ATTR_CREATE_MAX+1] = {};
796 const char *name, *typename;
797 u8 family, revision;
798 u32 flags = flag_exist(nlh);
799 int ret = 0;
800
801 if (unlikely(protocol_failed(attr) ||
802 attr[IPSET_ATTR_SETNAME] == NULL ||
803 attr[IPSET_ATTR_TYPENAME] == NULL ||
804 attr[IPSET_ATTR_REVISION] == NULL ||
805 attr[IPSET_ATTR_FAMILY] == NULL ||
806 (attr[IPSET_ATTR_DATA] != NULL &&
807 !flag_nested(attr[IPSET_ATTR_DATA]))))
808 return -IPSET_ERR_PROTOCOL;
809
810 name = nla_data(attr[IPSET_ATTR_SETNAME]);
811 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
812 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
813 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
814 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
815 name, typename, family_name(family), revision);
816
817 /*
818 * First, and without any locks, allocate and initialize
819 * a normal base set structure.
820 */
821 set = kzalloc(sizeof(struct ip_set), GFP_KERNEL);
822 if (!set)
823 return -ENOMEM;
824 rwlock_init(&set->lock);
825 strlcpy(set->name, name, IPSET_MAXNAMELEN);
a7b4f989 826 set->family = family;
f1e00b39 827 set->revision = revision;
a7b4f989
JK
828
829 /*
830 * Next, check that we know the type, and take
831 * a reference on the type, to make sure it stays available
832 * while constructing our new set.
833 *
834 * After referencing the type, we try to create the type
835 * specific part of the set without holding any locks.
836 */
837 ret = find_set_type_get(typename, family, revision, &(set->type));
838 if (ret)
839 goto out;
840
841 /*
842 * Without holding any locks, create private part.
843 */
844 if (attr[IPSET_ATTR_DATA] &&
8da560ce
PM
845 nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
846 set->type->create_policy)) {
15b4d93f
JK
847 ret = -IPSET_ERR_PROTOCOL;
848 goto put_out;
a7b4f989
JK
849 }
850
1785e8f4 851 ret = set->type->create(net, set, tb, flags);
a7b4f989
JK
852 if (ret != 0)
853 goto put_out;
854
855 /* BTW, ret==0 here. */
856
857 /*
858 * Here, we have a valid, constructed set and we are protected
2f9f28b2
JK
859 * by the nfnl mutex. Find the first free index in ip_set_list
860 * and check clashing.
a7b4f989 861 */
1785e8f4 862 ret = find_free_id(inst, set->name, &index, &clash);
9076aea7 863 if (ret == -EEXIST) {
a7b4f989 864 /* If this is the same set and requested, ignore error */
9076aea7 865 if ((flags & IPSET_FLAG_EXIST) &&
a7b4f989
JK
866 STREQ(set->type->name, clash->type->name) &&
867 set->type->family == clash->type->family &&
f1e00b39
JK
868 set->type->revision_min == clash->type->revision_min &&
869 set->type->revision_max == clash->type->revision_max &&
a7b4f989
JK
870 set->variant->same_set(set, clash))
871 ret = 0;
872 goto cleanup;
9076aea7
JK
873 } else if (ret == -IPSET_ERR_MAX_SETS) {
874 struct ip_set **list, **tmp;
1785e8f4 875 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
9076aea7 876
1785e8f4 877 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
9076aea7
JK
878 /* Wraparound */
879 goto cleanup;
880
881 list = kzalloc(sizeof(struct ip_set *) * i, GFP_KERNEL);
882 if (!list)
883 goto cleanup;
884 /* nfnl mutex is held, both lists are valid */
3e90ebd3 885 tmp = ip_set_dereference(inst->ip_set_list);
1785e8f4
VL
886 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
887 rcu_assign_pointer(inst->ip_set_list, list);
9076aea7
JK
888 /* Make sure all current packets have passed through */
889 synchronize_net();
890 /* Use new list */
1785e8f4
VL
891 index = inst->ip_set_max;
892 inst->ip_set_max = i;
9076aea7
JK
893 kfree(tmp);
894 ret = 0;
895 } else if (ret)
896 goto cleanup;
a7b4f989
JK
897
898 /*
899 * Finally! Add our shiny new set to the list, and be done.
900 */
901 pr_debug("create: '%s' created with index %u!\n", set->name, index);
3e90ebd3 902 ip_set(inst, index) = set;
a7b4f989
JK
903
904 return ret;
905
906cleanup:
907 set->variant->destroy(set);
908put_out:
909 module_put(set->type->me);
910out:
911 kfree(set);
912 return ret;
913}
914
915/* Destroy sets */
916
917static const struct nla_policy
918ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
919 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
920 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
921 .len = IPSET_MAXNAMELEN - 1 },
922};
923
924static void
1785e8f4 925ip_set_destroy_set(struct ip_set_net *inst, ip_set_id_t index)
a7b4f989 926{
3e90ebd3 927 struct ip_set *set = ip_set(inst, index);
a7b4f989
JK
928
929 pr_debug("set: %s\n", set->name);
3e90ebd3 930 ip_set(inst, index) = NULL;
a7b4f989
JK
931
932 /* Must call it without holding any lock */
933 set->variant->destroy(set);
934 module_put(set->type->me);
935 kfree(set);
936}
937
938static int
939ip_set_destroy(struct sock *ctnl, struct sk_buff *skb,
940 const struct nlmsghdr *nlh,
941 const struct nlattr * const attr[])
942{
1785e8f4 943 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
9076aea7 944 struct ip_set *s;
a7b4f989 945 ip_set_id_t i;
2f9f28b2 946 int ret = 0;
a7b4f989
JK
947
948 if (unlikely(protocol_failed(attr)))
949 return -IPSET_ERR_PROTOCOL;
950
2f9f28b2
JK
951 /* Commands are serialized and references are
952 * protected by the ip_set_ref_lock.
953 * External systems (i.e. xt_set) must call
954 * ip_set_put|get_nfnl_* functions, that way we
955 * can safely check references here.
956 *
957 * list:set timer can only decrement the reference
958 * counter, so if it's already zero, we can proceed
959 * without holding the lock.
960 */
961 read_lock_bh(&ip_set_ref_lock);
a7b4f989 962 if (!attr[IPSET_ATTR_SETNAME]) {
1785e8f4 963 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 964 s = ip_set(inst, i);
9076aea7 965 if (s != NULL && s->ref) {
9d883232 966 ret = -IPSET_ERR_BUSY;
2f9f28b2
JK
967 goto out;
968 }
a7b4f989 969 }
2f9f28b2 970 read_unlock_bh(&ip_set_ref_lock);
1785e8f4 971 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 972 s = ip_set(inst, i);
9076aea7 973 if (s != NULL)
1785e8f4 974 ip_set_destroy_set(inst, i);
a7b4f989
JK
975 }
976 } else {
1785e8f4
VL
977 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
978 &i);
9076aea7 979 if (s == NULL) {
2f9f28b2
JK
980 ret = -ENOENT;
981 goto out;
9076aea7 982 } else if (s->ref) {
2f9f28b2
JK
983 ret = -IPSET_ERR_BUSY;
984 goto out;
985 }
986 read_unlock_bh(&ip_set_ref_lock);
a7b4f989 987
1785e8f4 988 ip_set_destroy_set(inst, i);
a7b4f989
JK
989 }
990 return 0;
2f9f28b2
JK
991out:
992 read_unlock_bh(&ip_set_ref_lock);
993 return ret;
a7b4f989
JK
994}
995
996/* Flush sets */
997
998static void
999ip_set_flush_set(struct ip_set *set)
1000{
1001 pr_debug("set: %s\n", set->name);
1002
1003 write_lock_bh(&set->lock);
1004 set->variant->flush(set);
1005 write_unlock_bh(&set->lock);
1006}
1007
1008static int
1009ip_set_flush(struct sock *ctnl, struct sk_buff *skb,
1010 const struct nlmsghdr *nlh,
1011 const struct nlattr * const attr[])
1012{
1785e8f4 1013 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
9076aea7 1014 struct ip_set *s;
a7b4f989
JK
1015 ip_set_id_t i;
1016
1017 if (unlikely(protocol_failed(attr)))
9184a9cb 1018 return -IPSET_ERR_PROTOCOL;
a7b4f989
JK
1019
1020 if (!attr[IPSET_ATTR_SETNAME]) {
1785e8f4 1021 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 1022 s = ip_set(inst, i);
9076aea7
JK
1023 if (s != NULL)
1024 ip_set_flush_set(s);
1025 }
a7b4f989 1026 } else {
1785e8f4 1027 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
9076aea7 1028 if (s == NULL)
a7b4f989
JK
1029 return -ENOENT;
1030
9076aea7 1031 ip_set_flush_set(s);
a7b4f989
JK
1032 }
1033
1034 return 0;
1035}
1036
1037/* Rename a set */
1038
1039static const struct nla_policy
1040ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
1041 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1042 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1043 .len = IPSET_MAXNAMELEN - 1 },
1044 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
1045 .len = IPSET_MAXNAMELEN - 1 },
1046};
1047
1048static int
1049ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
1050 const struct nlmsghdr *nlh,
1051 const struct nlattr * const attr[])
1052{
1785e8f4 1053 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
9076aea7 1054 struct ip_set *set, *s;
a7b4f989
JK
1055 const char *name2;
1056 ip_set_id_t i;
2f9f28b2 1057 int ret = 0;
a7b4f989
JK
1058
1059 if (unlikely(protocol_failed(attr) ||
1060 attr[IPSET_ATTR_SETNAME] == NULL ||
1061 attr[IPSET_ATTR_SETNAME2] == NULL))
1062 return -IPSET_ERR_PROTOCOL;
1063
1785e8f4 1064 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
a7b4f989
JK
1065 if (set == NULL)
1066 return -ENOENT;
2f9f28b2
JK
1067
1068 read_lock_bh(&ip_set_ref_lock);
1069 if (set->ref != 0) {
1070 ret = -IPSET_ERR_REFERENCED;
1071 goto out;
1072 }
a7b4f989
JK
1073
1074 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1785e8f4 1075 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 1076 s = ip_set(inst, i);
9076aea7 1077 if (s != NULL && STREQ(s->name, name2)) {
2f9f28b2
JK
1078 ret = -IPSET_ERR_EXIST_SETNAME2;
1079 goto out;
1080 }
a7b4f989
JK
1081 }
1082 strncpy(set->name, name2, IPSET_MAXNAMELEN);
1083
2f9f28b2
JK
1084out:
1085 read_unlock_bh(&ip_set_ref_lock);
1086 return ret;
a7b4f989
JK
1087}
1088
1089/* Swap two sets so that name/index points to the other.
1090 * References and set names are also swapped.
1091 *
2f9f28b2
JK
1092 * The commands are serialized by the nfnl mutex and references are
1093 * protected by the ip_set_ref_lock. The kernel interfaces
a7b4f989
JK
1094 * do not hold the mutex but the pointer settings are atomic
1095 * so the ip_set_list always contains valid pointers to the sets.
1096 */
1097
1098static int
1099ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
1100 const struct nlmsghdr *nlh,
1101 const struct nlattr * const attr[])
1102{
1785e8f4 1103 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
a7b4f989
JK
1104 struct ip_set *from, *to;
1105 ip_set_id_t from_id, to_id;
1106 char from_name[IPSET_MAXNAMELEN];
a7b4f989
JK
1107
1108 if (unlikely(protocol_failed(attr) ||
1109 attr[IPSET_ATTR_SETNAME] == NULL ||
1110 attr[IPSET_ATTR_SETNAME2] == NULL))
1111 return -IPSET_ERR_PROTOCOL;
1112
1785e8f4
VL
1113 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1114 &from_id);
9076aea7 1115 if (from == NULL)
a7b4f989
JK
1116 return -ENOENT;
1117
1785e8f4
VL
1118 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1119 &to_id);
9076aea7 1120 if (to == NULL)
a7b4f989
JK
1121 return -IPSET_ERR_EXIST_SETNAME2;
1122
a7b4f989 1123 /* Features must not change.
25985edc 1124 * Not an artificial restriction anymore, as we must prevent
a7b4f989
JK
1125 * possible loops created by swapping in setlist type of sets. */
1126 if (!(from->type->features == to->type->features &&
169faa2e 1127 from->family == to->family))
a7b4f989
JK
1128 return -IPSET_ERR_TYPE_MISMATCH;
1129
a7b4f989 1130 strncpy(from_name, from->name, IPSET_MAXNAMELEN);
a7b4f989 1131 strncpy(from->name, to->name, IPSET_MAXNAMELEN);
a7b4f989 1132 strncpy(to->name, from_name, IPSET_MAXNAMELEN);
a7b4f989 1133
2f9f28b2
JK
1134 write_lock_bh(&ip_set_ref_lock);
1135 swap(from->ref, to->ref);
3e90ebd3
PM
1136 ip_set(inst, from_id) = to;
1137 ip_set(inst, to_id) = from;
2f9f28b2 1138 write_unlock_bh(&ip_set_ref_lock);
a7b4f989
JK
1139
1140 return 0;
1141}
1142
1143/* List/save set data */
1144
c1e2e043
JK
1145#define DUMP_INIT 0
1146#define DUMP_ALL 1
1147#define DUMP_ONE 2
1148#define DUMP_LAST 3
1149
1150#define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
1151#define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
a7b4f989
JK
1152
1153static int
1154ip_set_dump_done(struct netlink_callback *cb)
1155{
93302880
JK
1156 struct ip_set_net *inst = (struct ip_set_net *)cb->args[IPSET_CB_NET];
1157 if (cb->args[IPSET_CB_ARG0]) {
1158 pr_debug("release set %s\n",
3e90ebd3 1159 ip_set(inst, cb->args[IPSET_CB_INDEX])->name);
93302880
JK
1160 __ip_set_put_byindex(inst,
1161 (ip_set_id_t) cb->args[IPSET_CB_INDEX]);
a7b4f989
JK
1162 }
1163 return 0;
1164}
1165
1166static inline void
1167dump_attrs(struct nlmsghdr *nlh)
1168{
1169 const struct nlattr *attr;
1170 int rem;
1171
1172 pr_debug("dump nlmsg\n");
1173 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
1174 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
1175 }
1176}
1177
1178static int
93302880 1179dump_init(struct netlink_callback *cb, struct ip_set_net *inst)
a7b4f989
JK
1180{
1181 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
573ce260 1182 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
a7b4f989
JK
1183 struct nlattr *cda[IPSET_ATTR_CMD_MAX+1];
1184 struct nlattr *attr = (void *)nlh + min_len;
c1e2e043 1185 u32 dump_type;
a7b4f989
JK
1186 ip_set_id_t index;
1187
1188 /* Second pass, so parser can't fail */
1189 nla_parse(cda, IPSET_ATTR_CMD_MAX,
1190 attr, nlh->nlmsg_len - min_len, ip_set_setname_policy);
1191
93302880
JK
1192 /* cb->args[IPSET_CB_NET]: net namespace
1193 * [IPSET_CB_DUMP]: dump single set/all sets
1194 * [IPSET_CB_INDEX]: set index
1195 * [IPSET_CB_ARG0]: type specific
a7b4f989
JK
1196 */
1197
c1e2e043 1198 if (cda[IPSET_ATTR_SETNAME]) {
9076aea7
JK
1199 struct ip_set *set;
1200
1785e8f4 1201 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
9076aea7
JK
1202 &index);
1203 if (set == NULL)
c1e2e043 1204 return -ENOENT;
a7b4f989 1205
c1e2e043 1206 dump_type = DUMP_ONE;
93302880 1207 cb->args[IPSET_CB_INDEX] = index;
c1e2e043
JK
1208 } else
1209 dump_type = DUMP_ALL;
1210
1211 if (cda[IPSET_ATTR_FLAGS]) {
1212 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1213 dump_type |= (f << 16);
1214 }
93302880
JK
1215 cb->args[IPSET_CB_NET] = (unsigned long)inst;
1216 cb->args[IPSET_CB_DUMP] = dump_type;
a7b4f989 1217
a7b4f989
JK
1218 return 0;
1219}
1220
1221static int
1222ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
1223{
1224 ip_set_id_t index = IPSET_INVALID_ID, max;
1225 struct ip_set *set = NULL;
1226 struct nlmsghdr *nlh = NULL;
15e47304 1227 unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0;
93302880 1228 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
c1e2e043 1229 u32 dump_type, dump_flags;
a7b4f989
JK
1230 int ret = 0;
1231
93302880
JK
1232 if (!cb->args[IPSET_CB_DUMP]) {
1233 ret = dump_init(cb, inst);
a7b4f989
JK
1234 if (ret < 0) {
1235 nlh = nlmsg_hdr(cb->skb);
1236 /* We have to create and send the error message
1237 * manually :-( */
1238 if (nlh->nlmsg_flags & NLM_F_ACK)
1239 netlink_ack(cb->skb, nlh, ret);
1240 return ret;
1241 }
1242 }
1243
93302880 1244 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
a7b4f989
JK
1245 goto out;
1246
93302880
JK
1247 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1248 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1249 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1250 : inst->ip_set_max;
a8a8a093 1251dump_last:
93302880
JK
1252 pr_debug("dump type, flag: %u %u index: %ld\n",
1253 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1254 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
1255 index = (ip_set_id_t) cb->args[IPSET_CB_INDEX];
3e90ebd3 1256 set = ip_set(inst, index);
a7b4f989 1257 if (set == NULL) {
c1e2e043 1258 if (dump_type == DUMP_ONE) {
a7b4f989
JK
1259 ret = -ENOENT;
1260 goto out;
1261 }
1262 continue;
1263 }
1264 /* When dumping all sets, we must dump "sorted"
1265 * so that lists (unions of sets) are dumped last.
1266 */
c1e2e043
JK
1267 if (dump_type != DUMP_ONE &&
1268 ((dump_type == DUMP_ALL) ==
a8a8a093 1269 !!(set->type->features & IPSET_DUMP_LAST)))
a7b4f989
JK
1270 continue;
1271 pr_debug("List set: %s\n", set->name);
93302880 1272 if (!cb->args[IPSET_CB_ARG0]) {
a7b4f989
JK
1273 /* Start listing: make sure set won't be destroyed */
1274 pr_debug("reference set\n");
9076aea7 1275 __ip_set_get(set);
a7b4f989 1276 }
15e47304 1277 nlh = start_msg(skb, NETLINK_CB(cb->skb).portid,
a7b4f989
JK
1278 cb->nlh->nlmsg_seq, flags,
1279 IPSET_CMD_LIST);
1280 if (!nlh) {
1281 ret = -EMSGSIZE;
1282 goto release_refcount;
1283 }
7cf7899d
DM
1284 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1285 nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
1286 goto nla_put_failure;
c1e2e043
JK
1287 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1288 goto next_set;
93302880 1289 switch (cb->args[IPSET_CB_ARG0]) {
a7b4f989
JK
1290 case 0:
1291 /* Core header data */
7cf7899d
DM
1292 if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
1293 set->type->name) ||
1294 nla_put_u8(skb, IPSET_ATTR_FAMILY,
1295 set->family) ||
1296 nla_put_u8(skb, IPSET_ATTR_REVISION,
1297 set->revision))
1298 goto nla_put_failure;
a7b4f989
JK
1299 ret = set->variant->head(set, skb);
1300 if (ret < 0)
1301 goto release_refcount;
c1e2e043
JK
1302 if (dump_flags & IPSET_FLAG_LIST_HEADER)
1303 goto next_set;
a7b4f989
JK
1304 /* Fall through and add elements */
1305 default:
1306 read_lock_bh(&set->lock);
1307 ret = set->variant->list(set, skb, cb);
1308 read_unlock_bh(&set->lock);
93302880 1309 if (!cb->args[IPSET_CB_ARG0])
a7b4f989 1310 /* Set is done, proceed with next one */
c1e2e043 1311 goto next_set;
a7b4f989
JK
1312 goto release_refcount;
1313 }
1314 }
a8a8a093 1315 /* If we dump all sets, continue with dumping last ones */
c1e2e043
JK
1316 if (dump_type == DUMP_ALL) {
1317 dump_type = DUMP_LAST;
93302880
JK
1318 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1319 cb->args[IPSET_CB_INDEX] = 0;
a8a8a093
JK
1320 goto dump_last;
1321 }
a7b4f989
JK
1322 goto out;
1323
1324nla_put_failure:
1325 ret = -EFAULT;
c1e2e043
JK
1326next_set:
1327 if (dump_type == DUMP_ONE)
93302880 1328 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
c1e2e043 1329 else
93302880 1330 cb->args[IPSET_CB_INDEX]++;
a7b4f989
JK
1331release_refcount:
1332 /* If there was an error or set is done, release set */
93302880 1333 if (ret || !cb->args[IPSET_CB_ARG0]) {
3e90ebd3 1334 pr_debug("release set %s\n", ip_set(inst, index)->name);
1785e8f4 1335 __ip_set_put_byindex(inst, index);
93302880 1336 cb->args[IPSET_CB_ARG0] = 0;
a7b4f989 1337 }
a7b4f989
JK
1338out:
1339 if (nlh) {
1340 nlmsg_end(skb, nlh);
1341 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1342 dump_attrs(nlh);
1343 }
1344
1345 return ret < 0 ? ret : skb->len;
1346}
1347
1348static int
1349ip_set_dump(struct sock *ctnl, struct sk_buff *skb,
1350 const struct nlmsghdr *nlh,
1351 const struct nlattr * const attr[])
1352{
1353 if (unlikely(protocol_failed(attr)))
1354 return -IPSET_ERR_PROTOCOL;
1355
80d326fa
PNA
1356 {
1357 struct netlink_dump_control c = {
1358 .dump = ip_set_dump_start,
1359 .done = ip_set_dump_done,
1360 };
1361 return netlink_dump_start(ctnl, skb, nlh, &c);
1362 }
a7b4f989
JK
1363}
1364
1365/* Add, del and test */
1366
1367static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1368 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1369 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1370 .len = IPSET_MAXNAMELEN - 1 },
1371 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
1372 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1373 [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
1374};
1375
1376static int
5f52bc3c 1377call_ad(struct sock *ctnl, struct sk_buff *skb, struct ip_set *set,
a7b4f989
JK
1378 struct nlattr *tb[], enum ipset_adt adt,
1379 u32 flags, bool use_lineno)
1380{
3d14b171 1381 int ret;
a7b4f989 1382 u32 lineno = 0;
3d14b171 1383 bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
a7b4f989
JK
1384
1385 do {
1386 write_lock_bh(&set->lock);
3d14b171 1387 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
a7b4f989 1388 write_unlock_bh(&set->lock);
3d14b171 1389 retried = true;
a7b4f989
JK
1390 } while (ret == -EAGAIN &&
1391 set->variant->resize &&
3d14b171 1392 (ret = set->variant->resize(set, retried)) == 0);
a7b4f989
JK
1393
1394 if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1395 return 0;
1396 if (lineno && use_lineno) {
1397 /* Error in restore/batch mode: send back lineno */
5f52bc3c
JK
1398 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1399 struct sk_buff *skb2;
1400 struct nlmsgerr *errmsg;
1401 size_t payload = sizeof(*errmsg) + nlmsg_len(nlh);
573ce260 1402 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
a7b4f989 1403 struct nlattr *cda[IPSET_ATTR_CMD_MAX+1];
5f52bc3c 1404 struct nlattr *cmdattr;
a7b4f989
JK
1405 u32 *errline;
1406
5f52bc3c
JK
1407 skb2 = nlmsg_new(payload, GFP_KERNEL);
1408 if (skb2 == NULL)
1409 return -ENOMEM;
15e47304 1410 rep = __nlmsg_put(skb2, NETLINK_CB(skb).portid,
5f52bc3c
JK
1411 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1412 errmsg = nlmsg_data(rep);
1413 errmsg->error = ret;
1414 memcpy(&errmsg->msg, nlh, nlh->nlmsg_len);
1415 cmdattr = (void *)&errmsg->msg + min_len;
1416
a7b4f989
JK
1417 nla_parse(cda, IPSET_ATTR_CMD_MAX,
1418 cmdattr, nlh->nlmsg_len - min_len,
1419 ip_set_adt_policy);
1420
1421 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1422
1423 *errline = lineno;
5f52bc3c 1424
15e47304 1425 netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
5f52bc3c
JK
1426 /* Signal netlink not to send its ACK/errmsg. */
1427 return -EINTR;
a7b4f989
JK
1428 }
1429
1430 return ret;
1431}
1432
1433static int
1434ip_set_uadd(struct sock *ctnl, struct sk_buff *skb,
1435 const struct nlmsghdr *nlh,
1436 const struct nlattr * const attr[])
1437{
1785e8f4 1438 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
a7b4f989
JK
1439 struct ip_set *set;
1440 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1441 const struct nlattr *nla;
1442 u32 flags = flag_exist(nlh);
1443 bool use_lineno;
1444 int ret = 0;
1445
1446 if (unlikely(protocol_failed(attr) ||
1447 attr[IPSET_ATTR_SETNAME] == NULL ||
1448 !((attr[IPSET_ATTR_DATA] != NULL) ^
1449 (attr[IPSET_ATTR_ADT] != NULL)) ||
1450 (attr[IPSET_ATTR_DATA] != NULL &&
1451 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1452 (attr[IPSET_ATTR_ADT] != NULL &&
1453 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1454 attr[IPSET_ATTR_LINENO] == NULL))))
1455 return -IPSET_ERR_PROTOCOL;
1456
1785e8f4 1457 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
a7b4f989
JK
1458 if (set == NULL)
1459 return -ENOENT;
1460
1461 use_lineno = !!attr[IPSET_ATTR_LINENO];
1462 if (attr[IPSET_ATTR_DATA]) {
8da560ce
PM
1463 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1464 attr[IPSET_ATTR_DATA],
1465 set->type->adt_policy))
a7b4f989 1466 return -IPSET_ERR_PROTOCOL;
5f52bc3c
JK
1467 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD, flags,
1468 use_lineno);
a7b4f989
JK
1469 } else {
1470 int nla_rem;
1471
1472 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1473 memset(tb, 0, sizeof(tb));
1474 if (nla_type(nla) != IPSET_ATTR_DATA ||
1475 !flag_nested(nla) ||
8da560ce
PM
1476 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1477 set->type->adt_policy))
a7b4f989 1478 return -IPSET_ERR_PROTOCOL;
5f52bc3c 1479 ret = call_ad(ctnl, skb, set, tb, IPSET_ADD,
a7b4f989
JK
1480 flags, use_lineno);
1481 if (ret < 0)
1482 return ret;
1483 }
1484 }
1485 return ret;
1486}
1487
1488static int
1489ip_set_udel(struct sock *ctnl, struct sk_buff *skb,
1490 const struct nlmsghdr *nlh,
1491 const struct nlattr * const attr[])
1492{
1785e8f4 1493 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
a7b4f989
JK
1494 struct ip_set *set;
1495 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1496 const struct nlattr *nla;
1497 u32 flags = flag_exist(nlh);
1498 bool use_lineno;
1499 int ret = 0;
1500
1501 if (unlikely(protocol_failed(attr) ||
1502 attr[IPSET_ATTR_SETNAME] == NULL ||
1503 !((attr[IPSET_ATTR_DATA] != NULL) ^
1504 (attr[IPSET_ATTR_ADT] != NULL)) ||
1505 (attr[IPSET_ATTR_DATA] != NULL &&
1506 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1507 (attr[IPSET_ATTR_ADT] != NULL &&
1508 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1509 attr[IPSET_ATTR_LINENO] == NULL))))
1510 return -IPSET_ERR_PROTOCOL;
1511
1785e8f4 1512 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
a7b4f989
JK
1513 if (set == NULL)
1514 return -ENOENT;
1515
1516 use_lineno = !!attr[IPSET_ATTR_LINENO];
1517 if (attr[IPSET_ATTR_DATA]) {
8da560ce
PM
1518 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1519 attr[IPSET_ATTR_DATA],
1520 set->type->adt_policy))
a7b4f989 1521 return -IPSET_ERR_PROTOCOL;
5f52bc3c
JK
1522 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL, flags,
1523 use_lineno);
a7b4f989
JK
1524 } else {
1525 int nla_rem;
1526
1527 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1528 memset(tb, 0, sizeof(*tb));
1529 if (nla_type(nla) != IPSET_ATTR_DATA ||
1530 !flag_nested(nla) ||
8da560ce
PM
1531 nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1532 set->type->adt_policy))
a7b4f989 1533 return -IPSET_ERR_PROTOCOL;
5f52bc3c 1534 ret = call_ad(ctnl, skb, set, tb, IPSET_DEL,
a7b4f989
JK
1535 flags, use_lineno);
1536 if (ret < 0)
1537 return ret;
1538 }
1539 }
1540 return ret;
1541}
1542
1543static int
1544ip_set_utest(struct sock *ctnl, struct sk_buff *skb,
1545 const struct nlmsghdr *nlh,
1546 const struct nlattr * const attr[])
1547{
1785e8f4 1548 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
a7b4f989
JK
1549 struct ip_set *set;
1550 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1551 int ret = 0;
1552
1553 if (unlikely(protocol_failed(attr) ||
1554 attr[IPSET_ATTR_SETNAME] == NULL ||
1555 attr[IPSET_ATTR_DATA] == NULL ||
1556 !flag_nested(attr[IPSET_ATTR_DATA])))
1557 return -IPSET_ERR_PROTOCOL;
1558
1785e8f4 1559 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
a7b4f989
JK
1560 if (set == NULL)
1561 return -ENOENT;
1562
8da560ce
PM
1563 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1564 set->type->adt_policy))
a7b4f989
JK
1565 return -IPSET_ERR_PROTOCOL;
1566
1567 read_lock_bh(&set->lock);
3d14b171 1568 ret = set->variant->uadt(set, tb, IPSET_TEST, NULL, 0, 0);
a7b4f989
JK
1569 read_unlock_bh(&set->lock);
1570 /* Userspace can't trigger element to be re-added */
1571 if (ret == -EAGAIN)
1572 ret = 1;
1573
0f1799ba 1574 return ret > 0 ? 0 : -IPSET_ERR_EXIST;
a7b4f989
JK
1575}
1576
1577/* Get headed data of a set */
1578
1579static int
1580ip_set_header(struct sock *ctnl, struct sk_buff *skb,
1581 const struct nlmsghdr *nlh,
1582 const struct nlattr * const attr[])
1583{
1785e8f4 1584 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
a7b4f989
JK
1585 const struct ip_set *set;
1586 struct sk_buff *skb2;
1587 struct nlmsghdr *nlh2;
a7b4f989
JK
1588 int ret = 0;
1589
1590 if (unlikely(protocol_failed(attr) ||
1591 attr[IPSET_ATTR_SETNAME] == NULL))
1592 return -IPSET_ERR_PROTOCOL;
1593
1785e8f4 1594 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
9076aea7 1595 if (set == NULL)
a7b4f989 1596 return -ENOENT;
a7b4f989
JK
1597
1598 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1599 if (skb2 == NULL)
1600 return -ENOMEM;
1601
15e47304 1602 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
a7b4f989
JK
1603 IPSET_CMD_HEADER);
1604 if (!nlh2)
1605 goto nlmsg_failure;
7cf7899d
DM
1606 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1607 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
1608 nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
1609 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
1610 nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
1611 goto nla_put_failure;
a7b4f989
JK
1612 nlmsg_end(skb2, nlh2);
1613
15e47304 1614 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
a7b4f989
JK
1615 if (ret < 0)
1616 return ret;
1617
1618 return 0;
1619
1620nla_put_failure:
1621 nlmsg_cancel(skb2, nlh2);
1622nlmsg_failure:
1623 kfree_skb(skb2);
1624 return -EMSGSIZE;
1625}
1626
1627/* Get type data */
1628
1629static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1630 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1631 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
1632 .len = IPSET_MAXNAMELEN - 1 },
1633 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
1634};
1635
1636static int
1637ip_set_type(struct sock *ctnl, struct sk_buff *skb,
1638 const struct nlmsghdr *nlh,
1639 const struct nlattr * const attr[])
1640{
1641 struct sk_buff *skb2;
1642 struct nlmsghdr *nlh2;
1643 u8 family, min, max;
1644 const char *typename;
1645 int ret = 0;
1646
1647 if (unlikely(protocol_failed(attr) ||
1648 attr[IPSET_ATTR_TYPENAME] == NULL ||
1649 attr[IPSET_ATTR_FAMILY] == NULL))
1650 return -IPSET_ERR_PROTOCOL;
1651
1652 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1653 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1654 ret = find_set_type_minmax(typename, family, &min, &max);
1655 if (ret)
1656 return ret;
1657
1658 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1659 if (skb2 == NULL)
1660 return -ENOMEM;
1661
15e47304 1662 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
a7b4f989
JK
1663 IPSET_CMD_TYPE);
1664 if (!nlh2)
1665 goto nlmsg_failure;
7cf7899d
DM
1666 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL) ||
1667 nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
1668 nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
1669 nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
1670 nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
1671 goto nla_put_failure;
a7b4f989
JK
1672 nlmsg_end(skb2, nlh2);
1673
1674 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
15e47304 1675 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
a7b4f989
JK
1676 if (ret < 0)
1677 return ret;
1678
1679 return 0;
1680
1681nla_put_failure:
1682 nlmsg_cancel(skb2, nlh2);
1683nlmsg_failure:
1684 kfree_skb(skb2);
1685 return -EMSGSIZE;
1686}
1687
1688/* Get protocol version */
1689
1690static const struct nla_policy
1691ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1692 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1693};
1694
1695static int
1696ip_set_protocol(struct sock *ctnl, struct sk_buff *skb,
1697 const struct nlmsghdr *nlh,
1698 const struct nlattr * const attr[])
1699{
1700 struct sk_buff *skb2;
1701 struct nlmsghdr *nlh2;
1702 int ret = 0;
1703
1704 if (unlikely(attr[IPSET_ATTR_PROTOCOL] == NULL))
1705 return -IPSET_ERR_PROTOCOL;
1706
1707 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1708 if (skb2 == NULL)
1709 return -ENOMEM;
1710
15e47304 1711 nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
a7b4f989
JK
1712 IPSET_CMD_PROTOCOL);
1713 if (!nlh2)
1714 goto nlmsg_failure;
7cf7899d
DM
1715 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
1716 goto nla_put_failure;
a7b4f989
JK
1717 nlmsg_end(skb2, nlh2);
1718
15e47304 1719 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
a7b4f989
JK
1720 if (ret < 0)
1721 return ret;
1722
1723 return 0;
1724
1725nla_put_failure:
1726 nlmsg_cancel(skb2, nlh2);
1727nlmsg_failure:
1728 kfree_skb(skb2);
1729 return -EMSGSIZE;
1730}
1731
1732static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
d31f4d44
TB
1733 [IPSET_CMD_NONE] = {
1734 .call = ip_set_none,
1735 .attr_count = IPSET_ATTR_CMD_MAX,
1736 },
a7b4f989
JK
1737 [IPSET_CMD_CREATE] = {
1738 .call = ip_set_create,
1739 .attr_count = IPSET_ATTR_CMD_MAX,
1740 .policy = ip_set_create_policy,
1741 },
1742 [IPSET_CMD_DESTROY] = {
1743 .call = ip_set_destroy,
1744 .attr_count = IPSET_ATTR_CMD_MAX,
1745 .policy = ip_set_setname_policy,
1746 },
1747 [IPSET_CMD_FLUSH] = {
1748 .call = ip_set_flush,
1749 .attr_count = IPSET_ATTR_CMD_MAX,
1750 .policy = ip_set_setname_policy,
1751 },
1752 [IPSET_CMD_RENAME] = {
1753 .call = ip_set_rename,
1754 .attr_count = IPSET_ATTR_CMD_MAX,
1755 .policy = ip_set_setname2_policy,
1756 },
1757 [IPSET_CMD_SWAP] = {
1758 .call = ip_set_swap,
1759 .attr_count = IPSET_ATTR_CMD_MAX,
1760 .policy = ip_set_setname2_policy,
1761 },
1762 [IPSET_CMD_LIST] = {
1763 .call = ip_set_dump,
1764 .attr_count = IPSET_ATTR_CMD_MAX,
1765 .policy = ip_set_setname_policy,
1766 },
1767 [IPSET_CMD_SAVE] = {
1768 .call = ip_set_dump,
1769 .attr_count = IPSET_ATTR_CMD_MAX,
1770 .policy = ip_set_setname_policy,
1771 },
1772 [IPSET_CMD_ADD] = {
1773 .call = ip_set_uadd,
1774 .attr_count = IPSET_ATTR_CMD_MAX,
1775 .policy = ip_set_adt_policy,
1776 },
1777 [IPSET_CMD_DEL] = {
1778 .call = ip_set_udel,
1779 .attr_count = IPSET_ATTR_CMD_MAX,
1780 .policy = ip_set_adt_policy,
1781 },
1782 [IPSET_CMD_TEST] = {
1783 .call = ip_set_utest,
1784 .attr_count = IPSET_ATTR_CMD_MAX,
1785 .policy = ip_set_adt_policy,
1786 },
1787 [IPSET_CMD_HEADER] = {
1788 .call = ip_set_header,
1789 .attr_count = IPSET_ATTR_CMD_MAX,
1790 .policy = ip_set_setname_policy,
1791 },
1792 [IPSET_CMD_TYPE] = {
1793 .call = ip_set_type,
1794 .attr_count = IPSET_ATTR_CMD_MAX,
1795 .policy = ip_set_type_policy,
1796 },
1797 [IPSET_CMD_PROTOCOL] = {
1798 .call = ip_set_protocol,
1799 .attr_count = IPSET_ATTR_CMD_MAX,
1800 .policy = ip_set_protocol_policy,
1801 },
1802};
1803
1804static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
1805 .name = "ip_set",
1806 .subsys_id = NFNL_SUBSYS_IPSET,
1807 .cb_count = IPSET_MSG_MAX,
1808 .cb = ip_set_netlink_subsys_cb,
1809};
1810
1811/* Interface to iptables/ip6tables */
1812
1813static int
1814ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
1815{
95c96174 1816 unsigned int *op;
a7b4f989
JK
1817 void *data;
1818 int copylen = *len, ret = 0;
1785e8f4
VL
1819 struct net *net = sock_net(sk);
1820 struct ip_set_net *inst = ip_set_pernet(net);
a7b4f989 1821
1785e8f4 1822 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
a7b4f989
JK
1823 return -EPERM;
1824 if (optval != SO_IP_SET)
1825 return -EBADF;
95c96174 1826 if (*len < sizeof(unsigned int))
a7b4f989
JK
1827 return -EINVAL;
1828
1829 data = vmalloc(*len);
1830 if (!data)
1831 return -ENOMEM;
1832 if (copy_from_user(data, user, *len) != 0) {
1833 ret = -EFAULT;
1834 goto done;
1835 }
95c96174 1836 op = (unsigned int *) data;
a7b4f989
JK
1837
1838 if (*op < IP_SET_OP_VERSION) {
1839 /* Check the version at the beginning of operations */
1840 struct ip_set_req_version *req_version = data;
1841 if (req_version->version != IPSET_PROTOCOL) {
1842 ret = -EPROTO;
1843 goto done;
1844 }
1845 }
1846
1847 switch (*op) {
1848 case IP_SET_OP_VERSION: {
1849 struct ip_set_req_version *req_version = data;
1850
1851 if (*len != sizeof(struct ip_set_req_version)) {
1852 ret = -EINVAL;
1853 goto done;
1854 }
1855
1856 req_version->version = IPSET_PROTOCOL;
1857 ret = copy_to_user(user, req_version,
1858 sizeof(struct ip_set_req_version));
1859 goto done;
1860 }
1861 case IP_SET_OP_GET_BYNAME: {
1862 struct ip_set_req_get_set *req_get = data;
9076aea7 1863 ip_set_id_t id;
a7b4f989
JK
1864
1865 if (*len != sizeof(struct ip_set_req_get_set)) {
1866 ret = -EINVAL;
1867 goto done;
1868 }
1869 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
c14b78e7 1870 nfnl_lock(NFNL_SUBSYS_IPSET);
1785e8f4 1871 find_set_and_id(inst, req_get->set.name, &id);
9076aea7 1872 req_get->set.index = id;
c14b78e7 1873 nfnl_unlock(NFNL_SUBSYS_IPSET);
a7b4f989
JK
1874 goto copy;
1875 }
5e04c0c3
JK
1876 case IP_SET_OP_GET_FNAME: {
1877 struct ip_set_req_get_set_family *req_get = data;
1878 ip_set_id_t id;
1879
1880 if (*len != sizeof(struct ip_set_req_get_set_family)) {
1881 ret = -EINVAL;
1882 goto done;
1883 }
1884 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1885 nfnl_lock(NFNL_SUBSYS_IPSET);
1785e8f4 1886 find_set_and_id(inst, req_get->set.name, &id);
5e04c0c3
JK
1887 req_get->set.index = id;
1888 if (id != IPSET_INVALID_ID)
3e90ebd3 1889 req_get->family = ip_set(inst, id)->family;
5e04c0c3
JK
1890 nfnl_unlock(NFNL_SUBSYS_IPSET);
1891 goto copy;
1892 }
a7b4f989
JK
1893 case IP_SET_OP_GET_BYINDEX: {
1894 struct ip_set_req_get_set *req_get = data;
9076aea7 1895 struct ip_set *set;
a7b4f989
JK
1896
1897 if (*len != sizeof(struct ip_set_req_get_set) ||
1785e8f4 1898 req_get->set.index >= inst->ip_set_max) {
a7b4f989
JK
1899 ret = -EINVAL;
1900 goto done;
1901 }
c14b78e7 1902 nfnl_lock(NFNL_SUBSYS_IPSET);
3e90ebd3 1903 set = ip_set(inst, req_get->set.index);
9076aea7 1904 strncpy(req_get->set.name, set ? set->name : "",
a7b4f989 1905 IPSET_MAXNAMELEN);
c14b78e7 1906 nfnl_unlock(NFNL_SUBSYS_IPSET);
a7b4f989
JK
1907 goto copy;
1908 }
1909 default:
1910 ret = -EBADMSG;
1911 goto done;
1912 } /* end of switch(op) */
1913
1914copy:
1915 ret = copy_to_user(user, data, copylen);
1916
1917done:
1918 vfree(data);
1919 if (ret > 0)
1920 ret = 0;
1921 return ret;
1922}
1923
1924static struct nf_sockopt_ops so_set __read_mostly = {
1925 .pf = PF_INET,
1926 .get_optmin = SO_IP_SET,
1927 .get_optmax = SO_IP_SET + 1,
1928 .get = &ip_set_sockfn_get,
1929 .owner = THIS_MODULE,
1930};
1931
1785e8f4
VL
1932static int __net_init
1933ip_set_net_init(struct net *net)
a7b4f989 1934{
1785e8f4 1935 struct ip_set_net *inst = ip_set_pernet(net);
9076aea7 1936 struct ip_set **list;
a7b4f989 1937
1785e8f4
VL
1938 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
1939 if (inst->ip_set_max >= IPSET_INVALID_ID)
1940 inst->ip_set_max = IPSET_INVALID_ID - 1;
a7b4f989 1941
1785e8f4 1942 list = kzalloc(sizeof(struct ip_set *) * inst->ip_set_max, GFP_KERNEL);
9076aea7 1943 if (!list)
a7b4f989 1944 return -ENOMEM;
1785e8f4
VL
1945 inst->is_deleted = 0;
1946 rcu_assign_pointer(inst->ip_set_list, list);
1785e8f4
VL
1947 return 0;
1948}
1949
1950static void __net_exit
1951ip_set_net_exit(struct net *net)
1952{
1953 struct ip_set_net *inst = ip_set_pernet(net);
1954
1955 struct ip_set *set = NULL;
1956 ip_set_id_t i;
1957
1958 inst->is_deleted = 1; /* flag for ip_set_nfnl_put */
1959
1960 for (i = 0; i < inst->ip_set_max; i++) {
3e90ebd3 1961 set = ip_set(inst, i);
1785e8f4
VL
1962 if (set != NULL)
1963 ip_set_destroy_set(inst, i);
1964 }
1965 kfree(rcu_dereference_protected(inst->ip_set_list, 1));
1966}
1967
1968static struct pernet_operations ip_set_net_ops = {
1969 .init = ip_set_net_init,
1970 .exit = ip_set_net_exit,
1971 .id = &ip_set_net_id,
1972 .size = sizeof(struct ip_set_net)
1973};
1974
a7b4f989 1975
1785e8f4
VL
1976static int __init
1977ip_set_init(void)
1978{
1979 int ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
a7b4f989
JK
1980 if (ret != 0) {
1981 pr_err("ip_set: cannot register with nfnetlink.\n");
a7b4f989
JK
1982 return ret;
1983 }
1984 ret = nf_register_sockopt(&so_set);
1985 if (ret != 0) {
1986 pr_err("SO_SET registry failed: %d\n", ret);
1987 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
a7b4f989
JK
1988 return ret;
1989 }
1785e8f4
VL
1990 ret = register_pernet_subsys(&ip_set_net_ops);
1991 if (ret) {
1992 pr_err("ip_set: cannot register pernet_subsys.\n");
1993 nf_unregister_sockopt(&so_set);
1994 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
1995 return ret;
1996 }
6843bc3c 1997 pr_info("ip_set: protocol %u\n", IPSET_PROTOCOL);
a7b4f989
JK
1998 return 0;
1999}
2000
2001static void __exit
2002ip_set_fini(void)
2003{
1785e8f4 2004 unregister_pernet_subsys(&ip_set_net_ops);
a7b4f989
JK
2005 nf_unregister_sockopt(&so_set);
2006 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
a7b4f989
JK
2007 pr_debug("these are the famous last words\n");
2008}
2009
2010module_init(ip_set_init);
2011module_exit(ip_set_fini);