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