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