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