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