]> git.ipfire.org Git - thirdparty/ipset.git/blob - kernel/net/netfilter/ipset/ip_set_core.c
98dd409cd962fc8e998ab3a029afda942aa5071a
[thirdparty/ipset.git] / kernel / net / netfilter / ipset / ip_set_core.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3 * Patrick Schaaf <bof@bof.de>
4 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
5 */
6
7 /* Kernel module for IP set management */
8
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/ip.h>
13 #include <linux/skbuff.h>
14 #include <linux/spinlock.h>
15 #include <linux/rculist.h>
16 #include <net/netlink.h>
17 #include <net/net_namespace.h>
18 #include <net/netns/generic.h>
19
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/x_tables.h>
22 #include <linux/netfilter/nfnetlink.h>
23 #include <linux/netfilter/ipset/ip_set.h>
24 #include <linux/netfilter/ipset/ip_set_compiler.h>
25
26 static LIST_HEAD(ip_set_type_list); /* all registered set types */
27 static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */
28 static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */
29
30 struct ip_set_net {
31 struct ip_set * __rcu *ip_set_list; /* all individual sets */
32 ip_set_id_t ip_set_max; /* max number of sets */
33 bool is_deleted; /* deleted by ip_set_net_exit */
34 bool is_destroyed; /* all sets are destroyed */
35 };
36
37 static unsigned int ip_set_net_id __read_mostly;
38
39 static struct ip_set_net *ip_set_pernet(struct net *net)
40 {
41 return net_generic(net, ip_set_net_id);
42 }
43
44 #define IP_SET_INC 64
45 #define STRNCMP(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
46
47 static unsigned int max_sets;
48
49 module_param(max_sets, int, 0600);
50 MODULE_PARM_DESC(max_sets, "maximal number of sets");
51 MODULE_LICENSE("GPL");
52 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
53 MODULE_DESCRIPTION("ip_set: protocol " __stringify(IPSET_PROTOCOL));
54 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
55
56 /* When the nfnl mutex or ip_set_ref_lock is held: */
57 #define ip_set_dereference(p) \
58 rcu_dereference_protected(p, \
59 lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET) || \
60 lockdep_is_held(&ip_set_ref_lock))
61 #define ip_set(inst, id) \
62 ip_set_dereference((inst)->ip_set_list)[id]
63 #define ip_set_ref_netlink(inst,id) \
64 rcu_dereference_raw((inst)->ip_set_list)[id]
65
66 /* The set types are implemented in modules and registered set types
67 * can be found in ip_set_type_list. Adding/deleting types is
68 * serialized by ip_set_type_mutex.
69 */
70
71 static void
72 ip_set_type_lock(void)
73 {
74 mutex_lock(&ip_set_type_mutex);
75 }
76
77 static void
78 ip_set_type_unlock(void)
79 {
80 mutex_unlock(&ip_set_type_mutex);
81 }
82
83 /* Register and deregister settype */
84
85 static struct ip_set_type *
86 find_set_type(const char *name, u8 family, u8 revision)
87 {
88 struct ip_set_type *type;
89
90 list_for_each_entry_rcu_compat(type, &ip_set_type_list, list,
91 lockdep_is_held(&ip_set_type_mutex))
92 if (STRNCMP(type->name, name) &&
93 (type->family == family ||
94 type->family == NFPROTO_UNSPEC) &&
95 revision >= type->revision_min &&
96 revision <= type->revision_max)
97 return type;
98 return NULL;
99 }
100
101 /* Unlock, try to load a set type module and lock again */
102 static bool
103 load_settype(const char *name)
104 {
105 nfnl_unlock(NFNL_SUBSYS_IPSET);
106 pr_debug("try to load ip_set_%s\n", name);
107 if (request_module("ip_set_%s", name) < 0) {
108 pr_warn("Can't find ip_set type %s\n", name);
109 nfnl_lock(NFNL_SUBSYS_IPSET);
110 return false;
111 }
112 nfnl_lock(NFNL_SUBSYS_IPSET);
113 return true;
114 }
115
116 /* Find a set type and reference it */
117 #define find_set_type_get(name, family, revision, found) \
118 __find_set_type_get(name, family, revision, found, false)
119
120 static int
121 __find_set_type_get(const char *name, u8 family, u8 revision,
122 struct ip_set_type **found, bool retry)
123 {
124 struct ip_set_type *type;
125 int err;
126
127 if (retry && !load_settype(name))
128 return -IPSET_ERR_FIND_TYPE;
129
130 rcu_read_lock();
131 *found = find_set_type(name, family, revision);
132 if (*found) {
133 err = !try_module_get((*found)->me) ? -EFAULT : 0;
134 goto unlock;
135 }
136 /* Make sure the type is already loaded
137 * but we don't support the revision
138 */
139 list_for_each_entry_rcu(type, &ip_set_type_list, list)
140 if (STRNCMP(type->name, name)) {
141 err = -IPSET_ERR_FIND_TYPE;
142 goto unlock;
143 }
144 rcu_read_unlock();
145
146 return retry ? -IPSET_ERR_FIND_TYPE :
147 __find_set_type_get(name, family, revision, found, true);
148
149 unlock:
150 rcu_read_unlock();
151 return err;
152 }
153
154 /* Find a given set type by name and family.
155 * If we succeeded, the supported minimal and maximum revisions are
156 * filled out.
157 */
158 #define find_set_type_minmax(name, family, min, max) \
159 __find_set_type_minmax(name, family, min, max, false)
160
161 static int
162 __find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max,
163 bool retry)
164 {
165 struct ip_set_type *type;
166 bool found = false;
167
168 if (retry && !load_settype(name))
169 return -IPSET_ERR_FIND_TYPE;
170
171 *min = 255; *max = 0;
172 rcu_read_lock();
173 list_for_each_entry_rcu(type, &ip_set_type_list, list)
174 if (STRNCMP(type->name, name) &&
175 (type->family == family ||
176 type->family == NFPROTO_UNSPEC)) {
177 found = true;
178 if (type->revision_min < *min)
179 *min = type->revision_min;
180 if (type->revision_max > *max)
181 *max = type->revision_max;
182 }
183 rcu_read_unlock();
184 if (found)
185 return 0;
186
187 return retry ? -IPSET_ERR_FIND_TYPE :
188 __find_set_type_minmax(name, family, min, max, true);
189 }
190
191 #define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \
192 (f) == NFPROTO_IPV6 ? "inet6" : "any")
193
194 /* Register a set type structure. The type is identified by
195 * the unique triple of name, family and revision.
196 */
197 int
198 ip_set_type_register(struct ip_set_type *type)
199 {
200 int ret = 0;
201
202 if (type->protocol != IPSET_PROTOCOL) {
203 pr_warn("ip_set type %s, family %s, revision %u:%u uses wrong protocol version %u (want %u)\n",
204 type->name, family_name(type->family),
205 type->revision_min, type->revision_max,
206 type->protocol, IPSET_PROTOCOL);
207 return -EINVAL;
208 }
209
210 ip_set_type_lock();
211 if (find_set_type(type->name, type->family, type->revision_min)) {
212 /* Duplicate! */
213 pr_warn("ip_set type %s, family %s with revision min %u already registered!\n",
214 type->name, family_name(type->family),
215 type->revision_min);
216 ip_set_type_unlock();
217 return -EINVAL;
218 }
219 list_add_rcu(&type->list, &ip_set_type_list);
220 pr_debug("type %s, family %s, revision %u:%u registered.\n",
221 type->name, family_name(type->family),
222 type->revision_min, type->revision_max);
223 ip_set_type_unlock();
224
225 return ret;
226 }
227 EXPORT_SYMBOL_GPL(ip_set_type_register);
228
229 /* Unregister a set type. There's a small race with ip_set_create */
230 void
231 ip_set_type_unregister(struct ip_set_type *type)
232 {
233 ip_set_type_lock();
234 if (!find_set_type(type->name, type->family, type->revision_min)) {
235 pr_warn("ip_set type %s, family %s with revision min %u not registered\n",
236 type->name, family_name(type->family),
237 type->revision_min);
238 ip_set_type_unlock();
239 return;
240 }
241 list_del_rcu(&type->list);
242 pr_debug("type %s, family %s with revision min %u unregistered.\n",
243 type->name, family_name(type->family), type->revision_min);
244 ip_set_type_unlock();
245
246 synchronize_rcu();
247 }
248 EXPORT_SYMBOL_GPL(ip_set_type_unregister);
249
250 /* Utility functions */
251 void *
252 ip_set_alloc(size_t size)
253 {
254 return kvzalloc(size, GFP_KERNEL_ACCOUNT);
255 }
256 EXPORT_SYMBOL_GPL(ip_set_alloc);
257
258 void
259 ip_set_free(void *members)
260 {
261 pr_debug("%p: free with %s\n", members,
262 is_vmalloc_addr(members) ? "vfree" : "kfree");
263 kvfree(members);
264 }
265 EXPORT_SYMBOL_GPL(ip_set_free);
266
267 static bool
268 flag_nested(const struct nlattr *nla)
269 {
270 return nla->nla_type & NLA_F_NESTED;
271 }
272
273 static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
274 [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
275 [IPSET_ATTR_IPADDR_IPV6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
276 };
277
278 int
279 ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
280 {
281 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
282
283 if (unlikely(!flag_nested(nla)))
284 return -IPSET_ERR_PROTOCOL;
285 if (NLA_PARSE_NESTED(tb, IPSET_ATTR_IPADDR_MAX, nla,
286 ipaddr_policy, NULL))
287 return -IPSET_ERR_PROTOCOL;
288 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
289 return -IPSET_ERR_PROTOCOL;
290
291 *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
292 return 0;
293 }
294 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
295
296 int
297 ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
298 {
299 struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
300
301 if (unlikely(!flag_nested(nla)))
302 return -IPSET_ERR_PROTOCOL;
303
304 if (NLA_PARSE_NESTED(tb, IPSET_ATTR_IPADDR_MAX, nla,
305 ipaddr_policy, NULL))
306 return -IPSET_ERR_PROTOCOL;
307 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
308 return -IPSET_ERR_PROTOCOL;
309
310 memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
311 sizeof(struct in6_addr));
312 return 0;
313 }
314 EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
315
316 static u32
317 ip_set_timeout_get(const unsigned long *timeout)
318 {
319 u32 t;
320
321 if (*timeout == IPSET_ELEM_PERMANENT)
322 return 0;
323
324 t = jiffies_to_msecs(*timeout - jiffies) / MSEC_PER_SEC;
325 /* Zero value in userspace means no timeout */
326 return t == 0 ? 1 : t;
327 }
328
329 static char *
330 ip_set_comment_uget(struct nlattr *tb)
331 {
332 return nla_data(tb);
333 }
334
335 /* Called from uadd only, protected by the set spinlock.
336 * The kadt functions don't use the comment extensions in any way.
337 */
338 void
339 ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
340 const struct ip_set_ext *ext)
341 {
342 struct ip_set_comment_rcu *c = rcu_dereference_protected(comment->c, 1);
343 size_t len = ext->comment ? strlen(ext->comment) : 0;
344
345 if (unlikely(c)) {
346 set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
347 kfree_rcu(c, rcu);
348 rcu_assign_pointer(comment->c, NULL);
349 }
350 if (!len)
351 return;
352 if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
353 len = IPSET_MAX_COMMENT_SIZE;
354 c = kmalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
355 if (unlikely(!c))
356 return;
357 strscpy(c->str, ext->comment, len + 1);
358 set->ext_size += sizeof(*c) + strlen(c->str) + 1;
359 rcu_assign_pointer(comment->c, c);
360 }
361 EXPORT_SYMBOL_GPL(ip_set_init_comment);
362
363 /* Used only when dumping a set, protected by rcu_read_lock() */
364 static int
365 ip_set_put_comment(struct sk_buff *skb, const struct ip_set_comment *comment)
366 {
367 struct ip_set_comment_rcu *c = rcu_dereference(comment->c);
368
369 if (!c)
370 return 0;
371 return nla_put_string(skb, IPSET_ATTR_COMMENT, c->str);
372 }
373
374 /* Called from uadd/udel, flush or the garbage collectors protected
375 * by the set spinlock.
376 * Called when the set is destroyed and when there can't be any user
377 * of the set data anymore.
378 */
379 static void
380 ip_set_comment_free(struct ip_set *set, void *ptr)
381 {
382 struct ip_set_comment *comment = ptr;
383 struct ip_set_comment_rcu *c;
384
385 c = rcu_dereference_protected(comment->c, 1);
386 if (unlikely(!c))
387 return;
388 set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
389 kfree_rcu(c, rcu);
390 rcu_assign_pointer(comment->c, NULL);
391 }
392
393 typedef void (*destroyer)(struct ip_set *, void *);
394 /* ipset data extension types, in size order */
395
396 const struct ip_set_ext_type ip_set_extensions[] = {
397 [IPSET_EXT_ID_COUNTER] = {
398 .type = IPSET_EXT_COUNTER,
399 .flag = IPSET_FLAG_WITH_COUNTERS,
400 .len = sizeof(struct ip_set_counter),
401 .align = __alignof__(struct ip_set_counter),
402 },
403 [IPSET_EXT_ID_TIMEOUT] = {
404 .type = IPSET_EXT_TIMEOUT,
405 .len = sizeof(unsigned long),
406 .align = __alignof__(unsigned long),
407 },
408 [IPSET_EXT_ID_SKBINFO] = {
409 .type = IPSET_EXT_SKBINFO,
410 .flag = IPSET_FLAG_WITH_SKBINFO,
411 .len = sizeof(struct ip_set_skbinfo),
412 .align = __alignof__(struct ip_set_skbinfo),
413 },
414 [IPSET_EXT_ID_COMMENT] = {
415 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
416 .flag = IPSET_FLAG_WITH_COMMENT,
417 .len = sizeof(struct ip_set_comment),
418 .align = __alignof__(struct ip_set_comment),
419 .destroy = ip_set_comment_free,
420 },
421 };
422 EXPORT_SYMBOL_GPL(ip_set_extensions);
423
424 static bool
425 add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
426 {
427 return ip_set_extensions[id].flag ?
428 (flags & ip_set_extensions[id].flag) :
429 !!tb[IPSET_ATTR_TIMEOUT];
430 }
431
432 size_t
433 ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len,
434 size_t align)
435 {
436 enum ip_set_ext_id id;
437 u32 cadt_flags = 0;
438
439 if (tb[IPSET_ATTR_CADT_FLAGS])
440 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
441 if (cadt_flags & IPSET_FLAG_WITH_FORCEADD)
442 set->flags |= IPSET_CREATE_FLAG_FORCEADD;
443 if (!align)
444 align = 1;
445 for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
446 if (!add_extension(id, cadt_flags, tb))
447 continue;
448 if (align < ip_set_extensions[id].align)
449 align = ip_set_extensions[id].align;
450 len = ALIGN(len, ip_set_extensions[id].align);
451 set->offset[id] = len;
452 set->extensions |= ip_set_extensions[id].type;
453 len += ip_set_extensions[id].len;
454 }
455 return ALIGN(len, align);
456 }
457 EXPORT_SYMBOL_GPL(ip_set_elem_len);
458
459 int
460 ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
461 struct ip_set_ext *ext)
462 {
463 u64 fullmark;
464
465 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
466 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
467 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
468 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
469 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
470 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
471 return -IPSET_ERR_PROTOCOL;
472
473 if (tb[IPSET_ATTR_TIMEOUT]) {
474 if (!SET_WITH_TIMEOUT(set))
475 return -IPSET_ERR_TIMEOUT;
476 ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
477 }
478 if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) {
479 if (!SET_WITH_COUNTER(set))
480 return -IPSET_ERR_COUNTER;
481 if (tb[IPSET_ATTR_BYTES])
482 ext->bytes = be64_to_cpu(nla_get_be64(
483 tb[IPSET_ATTR_BYTES]));
484 if (tb[IPSET_ATTR_PACKETS])
485 ext->packets = be64_to_cpu(nla_get_be64(
486 tb[IPSET_ATTR_PACKETS]));
487 }
488 if (tb[IPSET_ATTR_COMMENT]) {
489 if (!SET_WITH_COMMENT(set))
490 return -IPSET_ERR_COMMENT;
491 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
492 }
493 if (tb[IPSET_ATTR_SKBMARK]) {
494 if (!SET_WITH_SKBINFO(set))
495 return -IPSET_ERR_SKBINFO;
496 fullmark = be64_to_cpu(nla_get_be64(tb[IPSET_ATTR_SKBMARK]));
497 ext->skbinfo.skbmark = fullmark >> 32;
498 ext->skbinfo.skbmarkmask = fullmark & 0xffffffff;
499 }
500 if (tb[IPSET_ATTR_SKBPRIO]) {
501 if (!SET_WITH_SKBINFO(set))
502 return -IPSET_ERR_SKBINFO;
503 ext->skbinfo.skbprio =
504 be32_to_cpu(nla_get_be32(tb[IPSET_ATTR_SKBPRIO]));
505 }
506 if (tb[IPSET_ATTR_SKBQUEUE]) {
507 if (!SET_WITH_SKBINFO(set))
508 return -IPSET_ERR_SKBINFO;
509 ext->skbinfo.skbqueue =
510 be16_to_cpu(nla_get_be16(tb[IPSET_ATTR_SKBQUEUE]));
511 }
512 return 0;
513 }
514 EXPORT_SYMBOL_GPL(ip_set_get_extensions);
515
516 static u64
517 ip_set_get_bytes(const struct ip_set_counter *counter)
518 {
519 return (u64)atomic64_read(&(counter)->bytes);
520 }
521
522 static u64
523 ip_set_get_packets(const struct ip_set_counter *counter)
524 {
525 return (u64)atomic64_read(&(counter)->packets);
526 }
527
528 static bool
529 ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
530 {
531 return IPSET_NLA_PUT_NET64(skb, IPSET_ATTR_BYTES,
532 cpu_to_be64(ip_set_get_bytes(counter)),
533 IPSET_ATTR_PAD) ||
534 IPSET_NLA_PUT_NET64(skb, IPSET_ATTR_PACKETS,
535 cpu_to_be64(ip_set_get_packets(counter)),
536 IPSET_ATTR_PAD);
537 }
538
539 static bool
540 ip_set_put_skbinfo(struct sk_buff *skb, const struct ip_set_skbinfo *skbinfo)
541 {
542 /* Send nonzero parameters only */
543 return ((skbinfo->skbmark || skbinfo->skbmarkmask) &&
544 IPSET_NLA_PUT_NET64(skb, IPSET_ATTR_SKBMARK,
545 cpu_to_be64((u64)skbinfo->skbmark << 32 |
546 skbinfo->skbmarkmask),
547 IPSET_ATTR_PAD)) ||
548 (skbinfo->skbprio &&
549 nla_put_net32(skb, IPSET_ATTR_SKBPRIO,
550 cpu_to_be32(skbinfo->skbprio))) ||
551 (skbinfo->skbqueue &&
552 nla_put_net16(skb, IPSET_ATTR_SKBQUEUE,
553 cpu_to_be16(skbinfo->skbqueue)));
554 }
555
556 int
557 ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
558 const void *e, bool active)
559 {
560 if (SET_WITH_TIMEOUT(set)) {
561 unsigned long *timeout = ext_timeout(e, set);
562
563 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
564 htonl(active ? ip_set_timeout_get(timeout)
565 : *timeout)))
566 return -EMSGSIZE;
567 }
568 if (SET_WITH_COUNTER(set) &&
569 ip_set_put_counter(skb, ext_counter(e, set)))
570 return -EMSGSIZE;
571 if (SET_WITH_COMMENT(set) &&
572 ip_set_put_comment(skb, ext_comment(e, set)))
573 return -EMSGSIZE;
574 if (SET_WITH_SKBINFO(set) &&
575 ip_set_put_skbinfo(skb, ext_skbinfo(e, set)))
576 return -EMSGSIZE;
577 return 0;
578 }
579 EXPORT_SYMBOL_GPL(ip_set_put_extensions);
580
581 static bool
582 ip_set_match_counter(u64 counter, u64 match, u8 op)
583 {
584 switch (op) {
585 case IPSET_COUNTER_NONE:
586 return true;
587 case IPSET_COUNTER_EQ:
588 return counter == match;
589 case IPSET_COUNTER_NE:
590 return counter != match;
591 case IPSET_COUNTER_LT:
592 return counter < match;
593 case IPSET_COUNTER_GT:
594 return counter > match;
595 }
596 return false;
597 }
598
599 static void
600 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
601 {
602 atomic64_add((long long)bytes, &(counter)->bytes);
603 }
604
605 static void
606 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
607 {
608 atomic64_add((long long)packets, &(counter)->packets);
609 }
610
611 static void
612 ip_set_update_counter(struct ip_set_counter *counter,
613 const struct ip_set_ext *ext, u32 flags)
614 {
615 if (ext->packets != ULLONG_MAX &&
616 !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
617 ip_set_add_bytes(ext->bytes, counter);
618 ip_set_add_packets(ext->packets, counter);
619 }
620 }
621
622 static void
623 ip_set_get_skbinfo(struct ip_set_skbinfo *skbinfo,
624 const struct ip_set_ext *ext,
625 struct ip_set_ext *mext, u32 flags)
626 {
627 mext->skbinfo = *skbinfo;
628 }
629
630 bool
631 ip_set_match_extensions(struct ip_set *set, const struct ip_set_ext *ext,
632 struct ip_set_ext *mext, u32 flags, void *data)
633 {
634 if (SET_WITH_TIMEOUT(set) &&
635 ip_set_timeout_expired(ext_timeout(data, set)))
636 return false;
637 if (SET_WITH_COUNTER(set)) {
638 struct ip_set_counter *counter = ext_counter(data, set);
639
640 ip_set_update_counter(counter, ext, flags);
641
642 if (flags & IPSET_FLAG_MATCH_COUNTERS &&
643 !(ip_set_match_counter(ip_set_get_packets(counter),
644 mext->packets, mext->packets_op) &&
645 ip_set_match_counter(ip_set_get_bytes(counter),
646 mext->bytes, mext->bytes_op)))
647 return false;
648 }
649 if (SET_WITH_SKBINFO(set))
650 ip_set_get_skbinfo(ext_skbinfo(data, set),
651 ext, mext, flags);
652 return true;
653 }
654 EXPORT_SYMBOL_GPL(ip_set_match_extensions);
655
656 /* Creating/destroying/renaming/swapping affect the existence and
657 * the properties of a set. All of these can be executed from userspace
658 * only and serialized by the nfnl mutex indirectly from nfnetlink.
659 *
660 * Sets are identified by their index in ip_set_list and the index
661 * is used by the external references (set/SET netfilter modules).
662 *
663 * The set behind an index may change by swapping only, from userspace.
664 */
665
666 static void
667 __ip_set_get(struct ip_set *set)
668 {
669 write_lock_bh(&ip_set_ref_lock);
670 set->ref++;
671 write_unlock_bh(&ip_set_ref_lock);
672 }
673
674 static void
675 __ip_set_put(struct ip_set *set)
676 {
677 write_lock_bh(&ip_set_ref_lock);
678 BUG_ON(set->ref == 0);
679 set->ref--;
680 write_unlock_bh(&ip_set_ref_lock);
681 }
682
683 /* set->ref can be swapped out by ip_set_swap, netlink events (like dump) need
684 * a separate reference counter
685 */
686 static void
687 __ip_set_get_netlink(struct ip_set *set)
688 {
689 write_lock_bh(&ip_set_ref_lock);
690 set->ref_netlink++;
691 write_unlock_bh(&ip_set_ref_lock);
692 }
693
694 static void
695 __ip_set_put_netlink(struct ip_set *set)
696 {
697 write_lock_bh(&ip_set_ref_lock);
698 BUG_ON(set->ref_netlink == 0);
699 set->ref_netlink--;
700 write_unlock_bh(&ip_set_ref_lock);
701 }
702
703 /* Add, del and test set entries from kernel.
704 *
705 * The set behind the index must exist and must be referenced
706 * so it can't be destroyed (or changed) under our foot.
707 */
708
709 static struct ip_set *
710 ip_set_rcu_get(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 rcu_read_lock();
716 /* ip_set_list and the set pointer need to be protected */
717 set = rcu_dereference(inst->ip_set_list)[index];
718
719 return set;
720 }
721
722 static inline void
723 ip_set_rcu_put(struct ip_set *set __always_unused)
724 {
725 rcu_read_unlock();
726 }
727
728 static inline void
729 ip_set_lock(struct ip_set *set)
730 {
731 if (!set->variant->region_lock)
732 spin_lock_bh(&set->lock);
733 }
734
735 static inline void
736 ip_set_unlock(struct ip_set *set)
737 {
738 if (!set->variant->region_lock)
739 spin_unlock_bh(&set->lock);
740 }
741
742 int
743 ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
744 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
745 {
746 struct ip_set *set = ip_set_rcu_get(IPSET_DEV_NET(par), index);
747 int ret = 0;
748
749 BUG_ON(!set);
750 pr_debug("set %s, index %u\n", set->name, index);
751
752 if (opt->dim < set->type->dimension ||
753 !(opt->family == set->family || set->family == NFPROTO_UNSPEC)) {
754 ip_set_rcu_put(set);
755 return 0;
756 }
757
758 ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
759
760 if (ret == -EAGAIN) {
761 /* Type requests element to be completed */
762 pr_debug("element must be completed, ADD is triggered\n");
763 ip_set_lock(set);
764 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
765 ip_set_unlock(set);
766 ret = 1;
767 } else {
768 /* --return-nomatch: invert matched element */
769 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
770 (set->type->features & IPSET_TYPE_NOMATCH) &&
771 (ret > 0 || ret == -ENOTEMPTY))
772 ret = -ret;
773 }
774
775 ip_set_rcu_put(set);
776 /* Convert error codes to nomatch */
777 return (ret < 0 ? 0 : ret);
778 }
779 EXPORT_SYMBOL_GPL(ip_set_test);
780
781 int
782 ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
783 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
784 {
785 struct ip_set *set = ip_set_rcu_get(IPSET_DEV_NET(par), index);
786 int ret;
787
788 BUG_ON(!set);
789 pr_debug("set %s, index %u\n", set->name, index);
790
791 if (opt->dim < set->type->dimension ||
792 !(opt->family == set->family || set->family == NFPROTO_UNSPEC)) {
793 ip_set_rcu_put(set);
794 return -IPSET_ERR_TYPE_MISMATCH;
795 }
796
797 ip_set_lock(set);
798 ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
799 ip_set_unlock(set);
800 ip_set_rcu_put(set);
801
802 return ret;
803 }
804 EXPORT_SYMBOL_GPL(ip_set_add);
805
806 int
807 ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
808 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
809 {
810 struct ip_set *set = ip_set_rcu_get(IPSET_DEV_NET(par), index);
811 int ret = 0;
812
813 BUG_ON(!set);
814 pr_debug("set %s, index %u\n", set->name, index);
815
816 if (opt->dim < set->type->dimension ||
817 !(opt->family == set->family || set->family == NFPROTO_UNSPEC)) {
818 ip_set_rcu_put(set);
819 return -IPSET_ERR_TYPE_MISMATCH;
820 }
821
822 ip_set_lock(set);
823 ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
824 ip_set_unlock(set);
825 ip_set_rcu_put(set);
826
827 return ret;
828 }
829 EXPORT_SYMBOL_GPL(ip_set_del);
830
831 /* Find set by name, reference it once. The reference makes sure the
832 * thing pointed to, does not go away under our feet.
833 *
834 */
835 ip_set_id_t
836 ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
837 {
838 ip_set_id_t i, index = IPSET_INVALID_ID;
839 struct ip_set *s;
840 struct ip_set_net *inst = ip_set_pernet(net);
841
842 rcu_read_lock();
843 for (i = 0; i < inst->ip_set_max; i++) {
844 s = rcu_dereference(inst->ip_set_list)[i];
845 if (s && STRNCMP(s->name, name)) {
846 __ip_set_get(s);
847 index = i;
848 *set = s;
849 break;
850 }
851 }
852 rcu_read_unlock();
853
854 return index;
855 }
856 EXPORT_SYMBOL_GPL(ip_set_get_byname);
857
858 /* If the given set pointer points to a valid set, decrement
859 * reference count by 1. The caller shall not assume the index
860 * to be valid, after calling this function.
861 *
862 */
863
864 static void
865 __ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
866 {
867 struct ip_set *set;
868
869 rcu_read_lock();
870 set = rcu_dereference(inst->ip_set_list)[index];
871 if (set)
872 __ip_set_put(set);
873 rcu_read_unlock();
874 }
875
876 void
877 ip_set_put_byindex(struct net *net, ip_set_id_t index)
878 {
879 struct ip_set_net *inst = ip_set_pernet(net);
880
881 __ip_set_put_byindex(inst, index);
882 }
883 EXPORT_SYMBOL_GPL(ip_set_put_byindex);
884
885 /* Get the name of a set behind a set index.
886 * Set itself is protected by RCU, but its name isn't: to protect against
887 * renaming, grab ip_set_ref_lock as reader (see ip_set_rename()) and copy the
888 * name.
889 */
890 void
891 ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name)
892 {
893 struct ip_set *set = ip_set_rcu_get(net, index);
894
895 BUG_ON(!set);
896
897 read_lock_bh(&ip_set_ref_lock);
898 strscpy_pad(name, set->name, IPSET_MAXNAMELEN);
899 read_unlock_bh(&ip_set_ref_lock);
900 ip_set_rcu_put(set);
901 }
902 EXPORT_SYMBOL_GPL(ip_set_name_byindex);
903
904 /* Routines to call by external subsystems, which do not
905 * call nfnl_lock for us.
906 */
907
908 /* Find set by index, reference it once. The reference makes sure the
909 * thing pointed to, does not go away under our feet.
910 *
911 * The nfnl mutex is used in the function.
912 */
913 ip_set_id_t
914 ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
915 {
916 struct ip_set *set;
917 struct ip_set_net *inst = ip_set_pernet(net);
918
919 if (index >= inst->ip_set_max)
920 return IPSET_INVALID_ID;
921
922 nfnl_lock(NFNL_SUBSYS_IPSET);
923 set = ip_set(inst, index);
924 if (set)
925 __ip_set_get(set);
926 else
927 index = IPSET_INVALID_ID;
928 nfnl_unlock(NFNL_SUBSYS_IPSET);
929
930 return index;
931 }
932 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
933
934 /* If the given set pointer points to a valid set, decrement
935 * reference count by 1. The caller shall not assume the index
936 * to be valid, after calling this function.
937 *
938 * The nfnl mutex is used in the function.
939 */
940 void
941 ip_set_nfnl_put(struct net *net, ip_set_id_t index)
942 {
943 struct ip_set *set;
944 struct ip_set_net *inst = ip_set_pernet(net);
945
946 nfnl_lock(NFNL_SUBSYS_IPSET);
947 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
948 set = ip_set(inst, index);
949 if (set)
950 __ip_set_put(set);
951 }
952 nfnl_unlock(NFNL_SUBSYS_IPSET);
953 }
954 EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
955
956 /* Communication protocol with userspace over netlink.
957 *
958 * The commands are serialized by the nfnl mutex.
959 */
960
961 static inline u8 protocol(const struct nlattr * const tb[])
962 {
963 return nla_get_u8(tb[IPSET_ATTR_PROTOCOL]);
964 }
965
966 static inline bool
967 protocol_failed(const struct nlattr * const tb[])
968 {
969 return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) != IPSET_PROTOCOL;
970 }
971
972 static inline bool
973 protocol_min_failed(const struct nlattr * const tb[])
974 {
975 return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) < IPSET_PROTOCOL_MIN;
976 }
977
978 static inline u32
979 flag_exist(const struct nlmsghdr *nlh)
980 {
981 return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
982 }
983
984 static struct nlmsghdr *
985 start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
986 enum ipset_cmd cmd)
987 {
988 return nfnl_msg_put(skb, portid, seq,
989 nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd), flags,
990 NFPROTO_IPV4, NFNETLINK_V0, 0);
991 }
992
993 /* Create a set */
994
995 static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
996 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
997 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
998 .len = IPSET_MAXNAMELEN - 1 },
999 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
1000 .len = IPSET_MAXNAMELEN - 1},
1001 [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
1002 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
1003 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1004 };
1005
1006 static struct ip_set *
1007 find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
1008 {
1009 struct ip_set *set = NULL;
1010 ip_set_id_t i;
1011
1012 *id = IPSET_INVALID_ID;
1013 for (i = 0; i < inst->ip_set_max; i++) {
1014 set = ip_set(inst, i);
1015 if (set && STRNCMP(set->name, name)) {
1016 *id = i;
1017 break;
1018 }
1019 }
1020 return (*id == IPSET_INVALID_ID ? NULL : set);
1021 }
1022
1023 static inline struct ip_set *
1024 find_set(struct ip_set_net *inst, const char *name)
1025 {
1026 ip_set_id_t id;
1027
1028 return find_set_and_id(inst, name, &id);
1029 }
1030
1031 static int
1032 find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
1033 struct ip_set **set)
1034 {
1035 struct ip_set *s;
1036 ip_set_id_t i;
1037
1038 *index = IPSET_INVALID_ID;
1039 for (i = 0; i < inst->ip_set_max; i++) {
1040 s = ip_set(inst, i);
1041 if (!s) {
1042 if (*index == IPSET_INVALID_ID)
1043 *index = i;
1044 } else if (STRNCMP(name, s->name)) {
1045 /* Name clash */
1046 *set = s;
1047 return -EEXIST;
1048 }
1049 }
1050 if (*index == IPSET_INVALID_ID)
1051 /* No free slot remained */
1052 return -IPSET_ERR_MAX_SETS;
1053 return 0;
1054 }
1055
1056 static int
1057 IPSET_CBFN(ip_set_none, struct net *net, struct sock *ctnl,
1058 struct sk_buff *skb, const struct nlmsghdr *nlh,
1059 const struct nlattr * const attr[],
1060 struct netlink_ext_ack *extack,
1061 const struct nfnl_info *info)
1062 {
1063 return -EOPNOTSUPP;
1064 }
1065
1066 static int
1067 IPSET_CBFN(ip_set_create, struct net *n, struct sock *ctnl,
1068 struct sk_buff *skb, const struct nlmsghdr *nlh,
1069 const struct nlattr * const attr[],
1070 struct netlink_ext_ack *extack,
1071 const struct nfnl_info *info)
1072 {
1073 struct net *net = IPSET_SOCK_NET(n, ctnl, info);
1074 struct ip_set_net *inst = ip_set_pernet(net);
1075 struct ip_set *set, *clash = NULL;
1076 ip_set_id_t index = IPSET_INVALID_ID;
1077 struct nlattr *tb[IPSET_ATTR_CREATE_MAX + 1] = {};
1078 const char *name, *typename;
1079 u8 family, revision;
1080 u32 flags = flag_exist(INFO_NLH(info, nlh));
1081 int ret = 0;
1082
1083 if (unlikely(protocol_min_failed(attr) ||
1084 !attr[IPSET_ATTR_SETNAME] ||
1085 !attr[IPSET_ATTR_TYPENAME] ||
1086 !attr[IPSET_ATTR_REVISION] ||
1087 !attr[IPSET_ATTR_FAMILY] ||
1088 (attr[IPSET_ATTR_DATA] &&
1089 !flag_nested(attr[IPSET_ATTR_DATA]))))
1090 return -IPSET_ERR_PROTOCOL;
1091
1092 name = nla_data(attr[IPSET_ATTR_SETNAME]);
1093 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1094 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1095 revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
1096 pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
1097 name, typename, family_name(family), revision);
1098
1099 /* First, and without any locks, allocate and initialize
1100 * a normal base set structure.
1101 */
1102 set = kzalloc(sizeof(*set), GFP_KERNEL);
1103 if (!set)
1104 return -ENOMEM;
1105 spin_lock_init(&set->lock);
1106 strscpy(set->name, name, IPSET_MAXNAMELEN);
1107 set->family = family;
1108 set->revision = revision;
1109
1110 /* Next, check that we know the type, and take
1111 * a reference on the type, to make sure it stays available
1112 * while constructing our new set.
1113 *
1114 * After referencing the type, we try to create the type
1115 * specific part of the set without holding any locks.
1116 */
1117 ret = find_set_type_get(typename, family, revision, &set->type);
1118 if (ret)
1119 goto out;
1120
1121 /* Without holding any locks, create private part. */
1122 if (attr[IPSET_ATTR_DATA] &&
1123 NLA_PARSE_NESTED(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
1124 set->type->create_policy, NULL)) {
1125 ret = -IPSET_ERR_PROTOCOL;
1126 goto put_out;
1127 }
1128 /* Set create flags depending on the type revision */
1129 set->flags |= set->type->create_flags[revision];
1130
1131 ret = set->type->create(INFO_NET(info, net), set, tb, flags);
1132 if (ret != 0)
1133 goto put_out;
1134
1135 /* BTW, ret==0 here. */
1136
1137 /* Here, we have a valid, constructed set and we are protected
1138 * by the nfnl mutex. Find the first free index in ip_set_list
1139 * and check clashing.
1140 */
1141 ret = find_free_id(inst, set->name, &index, &clash);
1142 if (ret == -EEXIST) {
1143 /* If this is the same set and requested, ignore error */
1144 if ((flags & IPSET_FLAG_EXIST) &&
1145 STRNCMP(set->type->name, clash->type->name) &&
1146 set->type->family == clash->type->family &&
1147 set->type->revision_min == clash->type->revision_min &&
1148 set->type->revision_max == clash->type->revision_max &&
1149 set->variant->same_set(set, clash))
1150 ret = 0;
1151 goto cleanup;
1152 } else if (ret == -IPSET_ERR_MAX_SETS) {
1153 struct ip_set **list, **tmp;
1154 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
1155
1156 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
1157 /* Wraparound */
1158 goto cleanup;
1159
1160 list = kvcalloc(i, sizeof(struct ip_set *), GFP_KERNEL);
1161 if (!list)
1162 goto cleanup;
1163 /* nfnl mutex is held, both lists are valid */
1164 tmp = ip_set_dereference(inst->ip_set_list);
1165 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
1166 rcu_assign_pointer(inst->ip_set_list, list);
1167 /* Make sure all current packets have passed through */
1168 synchronize_net();
1169 /* Use new list */
1170 index = inst->ip_set_max;
1171 inst->ip_set_max = i;
1172 kvfree(tmp);
1173 ret = 0;
1174 } else if (ret) {
1175 goto cleanup;
1176 }
1177
1178 /* Finally! Add our shiny new set to the list, and be done. */
1179 pr_debug("create: '%s' created with index %u!\n", set->name, index);
1180 ip_set(inst, index) = set;
1181
1182 return ret;
1183
1184 cleanup:
1185 set->variant->destroy(set);
1186 put_out:
1187 module_put(set->type->me);
1188 out:
1189 kfree(set);
1190 return ret;
1191 }
1192
1193 /* Destroy sets */
1194
1195 static const struct nla_policy
1196 ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
1197 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1198 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1199 .len = IPSET_MAXNAMELEN - 1 },
1200 };
1201
1202 static void
1203 ip_set_destroy_set(struct ip_set *set)
1204 {
1205 pr_debug("set: %s\n", set->name);
1206
1207 /* Must call it without holding any lock */
1208 set->variant->destroy(set);
1209 module_put(set->type->me);
1210 kfree(set);
1211 }
1212
1213 static int
1214 IPSET_CBFN(ip_set_destroy, struct net *net, struct sock *ctnl,
1215 struct sk_buff *skb, const struct nlmsghdr *nlh,
1216 const struct nlattr * const attr[],
1217 struct netlink_ext_ack *extack,
1218 const struct nfnl_info *info)
1219 {
1220 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl, info));
1221 struct ip_set *s;
1222 ip_set_id_t i;
1223 int ret = 0;
1224
1225 if (unlikely(protocol_min_failed(attr)))
1226 return -IPSET_ERR_PROTOCOL;
1227
1228 /* Make sure all readers of the old set pointers are completed. */
1229 synchronize_rcu();
1230
1231 /* Must wait for flush to be really finished in list:set */
1232 rcu_barrier();
1233
1234 /* Commands are serialized and references are
1235 * protected by the ip_set_ref_lock.
1236 * External systems (i.e. xt_set) must call
1237 * ip_set_put|get_nfnl_* functions, that way we
1238 * can safely check references here.
1239 *
1240 * list:set timer can only decrement the reference
1241 * counter, so if it's already zero, we can proceed
1242 * without holding the lock.
1243 */
1244 read_lock_bh(&ip_set_ref_lock);
1245 if (!attr[IPSET_ATTR_SETNAME]) {
1246 for (i = 0; i < inst->ip_set_max; i++) {
1247 s = ip_set(inst, i);
1248 if (s && (s->ref || s->ref_netlink)) {
1249 ret = -IPSET_ERR_BUSY;
1250 goto out;
1251 }
1252 }
1253 inst->is_destroyed = true;
1254 read_unlock_bh(&ip_set_ref_lock);
1255 for (i = 0; i < inst->ip_set_max; i++) {
1256 s = ip_set(inst, i);
1257 if (s) {
1258 ip_set(inst, i) = NULL;
1259 ip_set_destroy_set(s);
1260 }
1261 }
1262 /* Modified by ip_set_destroy() only, which is serialized */
1263 inst->is_destroyed = false;
1264 } else {
1265 u32 flags = flag_exist(INFO_NLH(info, nlh));
1266 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1267 &i);
1268 if (!s) {
1269 if (!(flags & IPSET_FLAG_EXIST))
1270 ret = -ENOENT;
1271 goto out;
1272 } else if (s->ref || s->ref_netlink) {
1273 ret = -IPSET_ERR_BUSY;
1274 goto out;
1275 }
1276 ip_set(inst, i) = NULL;
1277 read_unlock_bh(&ip_set_ref_lock);
1278
1279 ip_set_destroy_set(s);
1280 }
1281 return 0;
1282 out:
1283 read_unlock_bh(&ip_set_ref_lock);
1284 return ret;
1285 }
1286
1287 /* Flush sets */
1288
1289 static void
1290 ip_set_flush_set(struct ip_set *set)
1291 {
1292 pr_debug("set: %s\n", set->name);
1293
1294 ip_set_lock(set);
1295 set->variant->flush(set);
1296 ip_set_unlock(set);
1297 }
1298
1299 static int
1300 IPSET_CBFN(ip_set_flush, struct net *net, struct sock *ctnl,
1301 struct sk_buff *skb, const struct nlmsghdr *nlh,
1302 const struct nlattr * const attr[],
1303 struct netlink_ext_ack *extack,
1304 const struct nfnl_info *info)
1305 {
1306 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl, info));
1307 struct ip_set *s;
1308 ip_set_id_t i;
1309
1310 if (unlikely(protocol_min_failed(attr)))
1311 return -IPSET_ERR_PROTOCOL;
1312
1313 if (!attr[IPSET_ATTR_SETNAME]) {
1314 for (i = 0; i < inst->ip_set_max; i++) {
1315 s = ip_set(inst, i);
1316 if (s)
1317 ip_set_flush_set(s);
1318 }
1319 } else {
1320 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1321 if (!s)
1322 return -ENOENT;
1323
1324 ip_set_flush_set(s);
1325 }
1326
1327 return 0;
1328 }
1329
1330 /* Rename a set */
1331
1332 static const struct nla_policy
1333 ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
1334 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1335 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1336 .len = IPSET_MAXNAMELEN - 1 },
1337 [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
1338 .len = IPSET_MAXNAMELEN - 1 },
1339 };
1340
1341 static int
1342 IPSET_CBFN(ip_set_rename, struct net *net, struct sock *ctnl,
1343 struct sk_buff *skb, const struct nlmsghdr *nlh,
1344 const struct nlattr * const attr[],
1345 struct netlink_ext_ack *extack,
1346 const struct nfnl_info *info)
1347 {
1348 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl, info));
1349 struct ip_set *set, *s;
1350 const char *name2;
1351 ip_set_id_t i;
1352 int ret = 0;
1353
1354 if (unlikely(protocol_min_failed(attr) ||
1355 !attr[IPSET_ATTR_SETNAME] ||
1356 !attr[IPSET_ATTR_SETNAME2]))
1357 return -IPSET_ERR_PROTOCOL;
1358
1359 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1360 if (!set)
1361 return -ENOENT;
1362
1363 write_lock_bh(&ip_set_ref_lock);
1364 if (set->ref != 0 || set->ref_netlink != 0) {
1365 ret = -IPSET_ERR_REFERENCED;
1366 goto out;
1367 }
1368
1369 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1370 for (i = 0; i < inst->ip_set_max; i++) {
1371 s = ip_set(inst, i);
1372 if (s && STRNCMP(s->name, name2)) {
1373 ret = -IPSET_ERR_EXIST_SETNAME2;
1374 goto out;
1375 }
1376 }
1377 strscpy_pad(set->name, name2, IPSET_MAXNAMELEN);
1378
1379 out:
1380 write_unlock_bh(&ip_set_ref_lock);
1381 return ret;
1382 }
1383
1384 /* Swap two sets so that name/index points to the other.
1385 * References and set names are also swapped.
1386 *
1387 * The commands are serialized by the nfnl mutex and references are
1388 * protected by the ip_set_ref_lock. The kernel interfaces
1389 * do not hold the mutex but the pointer settings are atomic
1390 * so the ip_set_list always contains valid pointers to the sets.
1391 */
1392
1393 static int
1394 IPSET_CBFN(ip_set_swap, struct net *net, struct sock *ctnl,
1395 struct sk_buff *skb, const struct nlmsghdr *nlh,
1396 const struct nlattr * const attr[],
1397 struct netlink_ext_ack *extack,
1398 const struct nfnl_info *info)
1399 {
1400 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl, info));
1401 struct ip_set *from, *to;
1402 ip_set_id_t from_id, to_id;
1403 char from_name[IPSET_MAXNAMELEN];
1404
1405 if (unlikely(protocol_min_failed(attr) ||
1406 !attr[IPSET_ATTR_SETNAME] ||
1407 !attr[IPSET_ATTR_SETNAME2]))
1408 return -IPSET_ERR_PROTOCOL;
1409
1410 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1411 &from_id);
1412 if (!from)
1413 return -ENOENT;
1414
1415 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1416 &to_id);
1417 if (!to)
1418 return -IPSET_ERR_EXIST_SETNAME2;
1419
1420 /* Features must not change.
1421 * Not an artifical restriction anymore, as we must prevent
1422 * possible loops created by swapping in setlist type of sets.
1423 */
1424 if (!(from->type->features == to->type->features &&
1425 from->family == to->family))
1426 return -IPSET_ERR_TYPE_MISMATCH;
1427
1428 write_lock_bh(&ip_set_ref_lock);
1429
1430 if (from->ref_netlink || to->ref_netlink) {
1431 write_unlock_bh(&ip_set_ref_lock);
1432 return -EBUSY;
1433 }
1434
1435 strscpy_pad(from_name, from->name, IPSET_MAXNAMELEN);
1436 strscpy_pad(from->name, to->name, IPSET_MAXNAMELEN);
1437 strscpy_pad(to->name, from_name, IPSET_MAXNAMELEN);
1438
1439 swap(from->ref, to->ref);
1440 ip_set(inst, from_id) = to;
1441 ip_set(inst, to_id) = from;
1442 write_unlock_bh(&ip_set_ref_lock);
1443
1444 return 0;
1445 }
1446
1447 /* List/save set data */
1448
1449 #define DUMP_INIT 0
1450 #define DUMP_ALL 1
1451 #define DUMP_ONE 2
1452 #define DUMP_LAST 3
1453
1454 #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
1455 #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
1456
1457 int
1458 ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
1459 {
1460 u32 cadt_flags = 0;
1461
1462 if (SET_WITH_TIMEOUT(set))
1463 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
1464 htonl(set->timeout))))
1465 return -EMSGSIZE;
1466 if (SET_WITH_COUNTER(set))
1467 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
1468 if (SET_WITH_COMMENT(set))
1469 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
1470 if (SET_WITH_SKBINFO(set))
1471 cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
1472 if (SET_WITH_FORCEADD(set))
1473 cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
1474
1475 if (!cadt_flags)
1476 return 0;
1477 return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
1478 }
1479 EXPORT_SYMBOL_GPL(ip_set_put_flags);
1480
1481 static int
1482 ip_set_dump_done(struct netlink_callback *cb)
1483 {
1484 if (cb->args[IPSET_CB_ARG0]) {
1485 struct ip_set_net *inst =
1486 (struct ip_set_net *)cb->args[IPSET_CB_NET];
1487 ip_set_id_t index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1488 struct ip_set *set = ip_set_ref_netlink(inst, index);
1489
1490 if (set->variant->uref)
1491 set->variant->uref(set, cb, false);
1492 pr_debug("release set %s\n", set->name);
1493 __ip_set_put_netlink(set);
1494 }
1495 return 0;
1496 }
1497
1498 static inline void
1499 dump_attrs(struct nlmsghdr *nlh)
1500 {
1501 const struct nlattr *attr;
1502 int rem;
1503
1504 pr_debug("dump nlmsg\n");
1505 nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
1506 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
1507 }
1508 }
1509
1510 static const struct nla_policy
1511 ip_set_dump_policy[IPSET_ATTR_CMD_MAX + 1] = {
1512 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1513 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1514 .len = IPSET_MAXNAMELEN - 1 },
1515 [IPSET_ATTR_FLAGS] = { .type = NLA_U32 },
1516 };
1517
1518 static int
1519 ip_set_dump_start(struct netlink_callback *cb)
1520 {
1521 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
1522 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1523 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1524 struct nlattr *attr = (void *)nlh + min_len;
1525 struct sk_buff *skb = cb->skb;
1526 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1527 u32 dump_type;
1528 int ret;
1529
1530 ret = NLA_PARSE(cda, IPSET_ATTR_CMD_MAX, attr,
1531 nlh->nlmsg_len - min_len,
1532 ip_set_dump_policy, NULL);
1533 if (ret)
1534 goto error;
1535
1536 cb->args[IPSET_CB_PROTO] = nla_get_u8(cda[IPSET_ATTR_PROTOCOL]);
1537 if (cda[IPSET_ATTR_SETNAME]) {
1538 ip_set_id_t index;
1539 struct ip_set *set;
1540
1541 #if HAVE_NETLINK_DUMP_START_ARGS != 4
1542 read_lock_bh(&ip_set_ref_lock);
1543 #endif
1544 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
1545 &index);
1546 #if HAVE_NETLINK_DUMP_START_ARGS != 4
1547 read_unlock_bh(&ip_set_ref_lock);
1548 #endif
1549 if (!set) {
1550 ret = -ENOENT;
1551 goto error;
1552 }
1553 dump_type = DUMP_ONE;
1554 cb->args[IPSET_CB_INDEX] = index;
1555 } else {
1556 dump_type = DUMP_ALL;
1557 }
1558
1559 if (cda[IPSET_ATTR_FLAGS]) {
1560 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1561
1562 dump_type |= (f << 16);
1563 }
1564 cb->args[IPSET_CB_NET] = (unsigned long)inst;
1565 cb->args[IPSET_CB_DUMP] = dump_type;
1566
1567 return 0;
1568
1569 error:
1570 /* We have to create and send the error message manually :-( */
1571 if (nlh->nlmsg_flags & NLM_F_ACK) {
1572 NETLINK_ACK(cb->skb, nlh, ret, NULL);
1573 }
1574 return ret;
1575 }
1576
1577 static int
1578 ip_set_dump_do(struct sk_buff *skb, struct netlink_callback *cb)
1579 {
1580 ip_set_id_t index = IPSET_INVALID_ID, max;
1581 struct ip_set *set = NULL;
1582 struct nlmsghdr *nlh = NULL;
1583 unsigned int flags = NETLINK_PORTID(cb->skb) ? NLM_F_MULTI : 0;
1584 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1585 u32 dump_type, dump_flags;
1586 bool is_destroyed;
1587 int ret = 0;
1588
1589 if (!cb->args[IPSET_CB_DUMP]) {
1590 #if HAVE_NETLINK_DUMP_START_ARGS == 4
1591 return -EINVAL;
1592 #else
1593 ret = ip_set_dump_start(cb);
1594 if (ret < 0) {
1595 return ret;
1596 }
1597 #endif
1598 }
1599
1600 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
1601 goto out;
1602
1603 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1604 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1605 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1606 : inst->ip_set_max;
1607 dump_last:
1608 pr_debug("dump type, flag: %u %u index: %ld\n",
1609 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1610 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
1611 index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1612 write_lock_bh(&ip_set_ref_lock);
1613 set = ip_set(inst, index);
1614 is_destroyed = inst->is_destroyed;
1615 if (!set || is_destroyed) {
1616 write_unlock_bh(&ip_set_ref_lock);
1617 if (dump_type == DUMP_ONE) {
1618 ret = -ENOENT;
1619 goto out;
1620 }
1621 if (is_destroyed) {
1622 /* All sets are just being destroyed */
1623 ret = 0;
1624 goto out;
1625 }
1626 continue;
1627 }
1628 /* When dumping all sets, we must dump "sorted"
1629 * so that lists (unions of sets) are dumped last.
1630 */
1631 if (dump_type != DUMP_ONE &&
1632 ((dump_type == DUMP_ALL) ==
1633 !!(set->type->features & IPSET_DUMP_LAST))) {
1634 write_unlock_bh(&ip_set_ref_lock);
1635 continue;
1636 }
1637 pr_debug("List set: %s\n", set->name);
1638 if (!cb->args[IPSET_CB_ARG0]) {
1639 /* Start listing: make sure set won't be destroyed */
1640 pr_debug("reference set\n");
1641 set->ref_netlink++;
1642 }
1643 write_unlock_bh(&ip_set_ref_lock);
1644 nlh = start_msg(skb, NETLINK_PORTID(cb->skb),
1645 cb->nlh->nlmsg_seq, flags,
1646 IPSET_CMD_LIST);
1647 if (!nlh) {
1648 ret = -EMSGSIZE;
1649 goto release_refcount;
1650 }
1651 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL,
1652 cb->args[IPSET_CB_PROTO]) ||
1653 nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
1654 goto nla_put_failure;
1655 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1656 goto next_set;
1657 switch (cb->args[IPSET_CB_ARG0]) {
1658 case 0:
1659 /* Core header data */
1660 if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
1661 set->type->name) ||
1662 nla_put_u8(skb, IPSET_ATTR_FAMILY,
1663 set->family) ||
1664 nla_put_u8(skb, IPSET_ATTR_REVISION,
1665 set->revision))
1666 goto nla_put_failure;
1667 if (cb->args[IPSET_CB_PROTO] > IPSET_PROTOCOL_MIN &&
1668 nla_put_net16(skb, IPSET_ATTR_INDEX, htons(index)))
1669 goto nla_put_failure;
1670 ret = set->variant->head(set, skb);
1671 if (ret < 0)
1672 goto release_refcount;
1673 if (dump_flags & IPSET_FLAG_LIST_HEADER)
1674 goto next_set;
1675 if (set->variant->uref)
1676 set->variant->uref(set, cb, true);
1677 fallthrough;
1678 default:
1679 ret = set->variant->list(set, skb, cb);
1680 if (!cb->args[IPSET_CB_ARG0])
1681 /* Set is done, proceed with next one */
1682 goto next_set;
1683 goto release_refcount;
1684 }
1685 }
1686 /* If we dump all sets, continue with dumping last ones */
1687 if (dump_type == DUMP_ALL) {
1688 dump_type = DUMP_LAST;
1689 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1690 cb->args[IPSET_CB_INDEX] = 0;
1691 if (set && set->variant->uref)
1692 set->variant->uref(set, cb, false);
1693 goto dump_last;
1694 }
1695 goto out;
1696
1697 nla_put_failure:
1698 ret = -EFAULT;
1699 next_set:
1700 if (dump_type == DUMP_ONE)
1701 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
1702 else
1703 cb->args[IPSET_CB_INDEX]++;
1704 release_refcount:
1705 /* If there was an error or set is done, release set */
1706 if (ret || !cb->args[IPSET_CB_ARG0]) {
1707 set = ip_set_ref_netlink(inst, index);
1708 if (set->variant->uref)
1709 set->variant->uref(set, cb, false);
1710 pr_debug("release set %s\n", set->name);
1711 __ip_set_put_netlink(set);
1712 cb->args[IPSET_CB_ARG0] = 0;
1713 }
1714 out:
1715 if (nlh) {
1716 nlmsg_end(skb, nlh);
1717 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1718 dump_attrs(nlh);
1719 }
1720
1721 return ret < 0 ? ret : skb->len;
1722 }
1723
1724 static int
1725 IPSET_CBFN(ip_set_dump, struct net *net, struct sock *ctnl,
1726 struct sk_buff *skb, const struct nlmsghdr *nlh,
1727 const struct nlattr * const attr[],
1728 struct netlink_ext_ack *extack,
1729 const struct nfnl_info *info)
1730 {
1731 if (unlikely(protocol_min_failed(attr)))
1732 return -IPSET_ERR_PROTOCOL;
1733
1734 #if HAVE_NETLINK_DUMP_START_ARGS == 5
1735 return netlink_dump_start(ctnl, skb, nlh,
1736 ip_set_dump_do,
1737 ip_set_dump_done);
1738 #elif HAVE_NETLINK_DUMP_START_ARGS == 6
1739 return netlink_dump_start(ctnl, skb, nlh,
1740 ip_set_dump_do,
1741 ip_set_dump_done, 0);
1742 #else
1743 {
1744 struct netlink_dump_control c = {
1745 #if HAVE_NETLINK_DUMP_START_ARGS == 4
1746 .start = ip_set_dump_start,
1747 #endif
1748 .dump = ip_set_dump_do,
1749 .done = ip_set_dump_done,
1750 };
1751 return netlink_dump_start(INFO_SK(info, ctnl), skb, INFO_NLH(info, nlh), &c);
1752 }
1753 #endif
1754 }
1755
1756 /* Add, del and test */
1757
1758 static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1759 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
1760 [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
1761 .len = IPSET_MAXNAMELEN - 1 },
1762 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
1763 [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
1764 [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
1765 };
1766
1767 static int
1768 CALL_AD(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1769 struct ip_set *set, struct nlattr *tb[], enum ipset_adt adt,
1770 u32 flags, bool use_lineno)
1771 {
1772 int ret;
1773 u32 lineno = 0;
1774 bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
1775
1776 do {
1777 if (retried) {
1778 __ip_set_get_netlink(set);
1779 nfnl_unlock(NFNL_SUBSYS_IPSET);
1780 cond_resched();
1781 nfnl_lock(NFNL_SUBSYS_IPSET);
1782 __ip_set_put_netlink(set);
1783 }
1784
1785 ip_set_lock(set);
1786 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1787 ip_set_unlock(set);
1788 retried = true;
1789 } while (ret == -ERANGE ||
1790 (ret == -EAGAIN &&
1791 set->variant->resize &&
1792 (ret = set->variant->resize(set, retried)) == 0));
1793
1794 if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1795 return 0;
1796 if (lineno && use_lineno) {
1797 /* Error in restore/batch mode: send back lineno */
1798 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1799 struct sk_buff *skb2;
1800 struct nlmsgerr *errmsg;
1801 size_t payload = min(SIZE_MAX,
1802 sizeof(*errmsg) + nlmsg_len(nlh));
1803 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1804 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1805 struct nlattr *cmdattr;
1806 u32 *errline;
1807
1808 skb2 = nlmsg_new(payload, GFP_KERNEL);
1809 if (!skb2)
1810 return -ENOMEM;
1811 rep = nlmsg_put(skb2, NETLINK_PORTID(skb),
1812 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1813 errmsg = nlmsg_data(rep);
1814 errmsg->error = ret;
1815 unsafe_memcpy(&errmsg->msg, nlh, nlh->nlmsg_len,
1816 /* Bounds checked by the skb layer. */);
1817 cmdattr = (void *)&errmsg->msg + min_len;
1818
1819 ret = NLA_PARSE(cda, IPSET_ATTR_CMD_MAX, cmdattr,
1820 nlh->nlmsg_len - min_len, ip_set_adt_policy,
1821 NULL);
1822
1823 if (ret) {
1824 nlmsg_free(skb2);
1825 return ret;
1826 }
1827 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1828
1829 *errline = lineno;
1830
1831 NFNETLINK_UNICAST(ctnl, skb2, net, NETLINK_PORTID(skb));
1832 /* Signal netlink not to send its ACK/errmsg. */
1833 return -EINTR;
1834 }
1835
1836 return ret;
1837 }
1838
1839 static int IPSET_CBFN_AD(ip_set_ad, struct net *net, struct sock *ctnl,
1840 struct sk_buff *skb,
1841 enum ipset_adt adt,
1842 const struct nlmsghdr *nlh,
1843 const struct nlattr * const attr[],
1844 struct netlink_ext_ack *extack, const struct nfnl_info *info)
1845 {
1846 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl, info));
1847 struct ip_set *set;
1848 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1849 const struct nlattr *nla;
1850 u32 flags = flag_exist(INFO_NLH(info, nlh));
1851 bool use_lineno;
1852 int ret = 0;
1853
1854 if (unlikely(protocol_min_failed(attr) ||
1855 !attr[IPSET_ATTR_SETNAME] ||
1856 !((attr[IPSET_ATTR_DATA] != NULL) ^
1857 (attr[IPSET_ATTR_ADT] != NULL)) ||
1858 (attr[IPSET_ATTR_DATA] &&
1859 !flag_nested(attr[IPSET_ATTR_DATA])) ||
1860 (attr[IPSET_ATTR_ADT] &&
1861 (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1862 !attr[IPSET_ATTR_LINENO]))))
1863 return -IPSET_ERR_PROTOCOL;
1864
1865 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1866 if (!set)
1867 return -ENOENT;
1868
1869 use_lineno = !!attr[IPSET_ATTR_LINENO];
1870 if (attr[IPSET_ATTR_DATA]) {
1871 if (NLA_PARSE_NESTED(tb, IPSET_ATTR_ADT_MAX,
1872 attr[IPSET_ATTR_DATA],
1873 set->type->adt_policy, NULL))
1874 return -IPSET_ERR_PROTOCOL;
1875 ret = CALL_AD(net, ctnl, skb, set, tb, adt, flags,
1876 use_lineno);
1877 } else {
1878 int nla_rem;
1879
1880 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1881 if (nla_type(nla) != IPSET_ATTR_DATA ||
1882 !flag_nested(nla) ||
1883 NLA_PARSE_NESTED(tb, IPSET_ATTR_ADT_MAX, nla,
1884 set->type->adt_policy, NULL))
1885 return -IPSET_ERR_PROTOCOL;
1886 ret = CALL_AD(net, ctnl, skb, set, tb, adt,
1887 flags, use_lineno);
1888 if (ret < 0)
1889 return ret;
1890 }
1891 }
1892 return ret;
1893 }
1894
1895 static int
1896 IPSET_CBFN(ip_set_uadd, struct net *net, struct sock *ctnl,
1897 struct sk_buff *skb, const struct nlmsghdr *nlh,
1898 const struct nlattr * const attr[],
1899 struct netlink_ext_ack *extack,
1900 const struct nfnl_info *info)
1901 {
1902 return IPSET_CBFN_AD(ip_set_ad, INFO_NET(info, net), INFO_SK(info, ctnl), skb,
1903 IPSET_ADD, INFO_NLH(info, nlh), attr, extack, info);
1904 }
1905
1906 static int
1907 IPSET_CBFN(ip_set_udel, struct net *net, struct sock *ctnl,
1908 struct sk_buff *skb, const struct nlmsghdr *nlh,
1909 const struct nlattr * const attr[],
1910 struct netlink_ext_ack *extack,
1911 const struct nfnl_info *info)
1912 {
1913 return IPSET_CBFN_AD(ip_set_ad, INFO_NET(info, net), INFO_SK(info, ctnl), skb,
1914 IPSET_DEL, INFO_NLH(info, nlh), attr, extack, info);
1915 }
1916
1917 static int
1918 IPSET_CBFN(ip_set_utest, struct net *net, struct sock *ctnl,
1919 struct sk_buff *skb,
1920 const struct nlmsghdr *nlh,
1921 const struct nlattr * const attr[],
1922 struct netlink_ext_ack *extack,
1923 const struct nfnl_info *info)
1924 {
1925 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl, info));
1926 struct ip_set *set;
1927 struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1928 int ret = 0;
1929 u32 lineno;
1930
1931 if (unlikely(protocol_min_failed(attr) ||
1932 !attr[IPSET_ATTR_SETNAME] ||
1933 !attr[IPSET_ATTR_DATA] ||
1934 !flag_nested(attr[IPSET_ATTR_DATA])))
1935 return -IPSET_ERR_PROTOCOL;
1936
1937 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1938 if (!set)
1939 return -ENOENT;
1940
1941 if (NLA_PARSE_NESTED(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1942 set->type->adt_policy, NULL))
1943 return -IPSET_ERR_PROTOCOL;
1944
1945 rcu_read_lock_bh();
1946 ret = set->variant->uadt(set, tb, IPSET_TEST, &lineno, 0, 0);
1947 rcu_read_unlock_bh();
1948 /* Userspace can't trigger element to be re-added */
1949 if (ret == -EAGAIN)
1950 ret = 1;
1951
1952 return ret > 0 ? 0 : -IPSET_ERR_EXIST;
1953 }
1954
1955 /* Get headed data of a set */
1956
1957 static int
1958 IPSET_CBFN(ip_set_header, struct net *net, struct sock *ctnl,
1959 struct sk_buff *skb, const struct nlmsghdr *nlh,
1960 const struct nlattr * const attr[],
1961 struct netlink_ext_ack *extack,
1962 const struct nfnl_info *info)
1963 {
1964 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl, info));
1965 const struct ip_set *set;
1966 struct sk_buff *skb2;
1967 struct nlmsghdr *nlh2;
1968
1969 if (unlikely(protocol_min_failed(attr) ||
1970 !attr[IPSET_ATTR_SETNAME]))
1971 return -IPSET_ERR_PROTOCOL;
1972
1973 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1974 if (!set)
1975 return -ENOENT;
1976
1977 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1978 if (!skb2)
1979 return -ENOMEM;
1980
1981 nlh2 = start_msg(skb2, NETLINK_PORTID(skb), INFO_NLH(info, nlh)->nlmsg_seq, 0,
1982 IPSET_CMD_HEADER);
1983 if (!nlh2)
1984 goto nlmsg_failure;
1985 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
1986 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
1987 nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
1988 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
1989 nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
1990 goto nla_put_failure;
1991 nlmsg_end(skb2, nlh2);
1992
1993 return NFNETLINK_UNICAST(INFO_SK(info, ctnl), skb2, INFO_NET(info, net), NETLINK_PORTID(skb));
1994
1995 nla_put_failure:
1996 nlmsg_cancel(skb2, nlh2);
1997 nlmsg_failure:
1998 kfree_skb(skb2);
1999 return -EMSGSIZE;
2000 }
2001
2002 /* Get type data */
2003
2004 static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
2005 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
2006 [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
2007 .len = IPSET_MAXNAMELEN - 1 },
2008 [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
2009 };
2010
2011 static int
2012 IPSET_CBFN(ip_set_type, struct net *net, struct sock *ctnl,
2013 struct sk_buff *skb, const struct nlmsghdr *nlh,
2014 const struct nlattr * const attr[],
2015 struct netlink_ext_ack *extack,
2016 const struct nfnl_info *info)
2017 {
2018 struct sk_buff *skb2;
2019 struct nlmsghdr *nlh2;
2020 u8 family, min, max;
2021 const char *typename;
2022 int ret = 0;
2023
2024 if (unlikely(protocol_min_failed(attr) ||
2025 !attr[IPSET_ATTR_TYPENAME] ||
2026 !attr[IPSET_ATTR_FAMILY]))
2027 return -IPSET_ERR_PROTOCOL;
2028
2029 family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
2030 typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
2031 ret = find_set_type_minmax(typename, family, &min, &max);
2032 if (ret)
2033 return ret;
2034
2035 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2036 if (!skb2)
2037 return -ENOMEM;
2038
2039 nlh2 = start_msg(skb2, NETLINK_PORTID(skb), INFO_NLH(info, nlh)->nlmsg_seq, 0,
2040 IPSET_CMD_TYPE);
2041 if (!nlh2)
2042 goto nlmsg_failure;
2043 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
2044 nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
2045 nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
2046 nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
2047 nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
2048 goto nla_put_failure;
2049 nlmsg_end(skb2, nlh2);
2050
2051 pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
2052 return NFNETLINK_UNICAST(INFO_SK(info, ctnl), skb2, INFO_NET(info, net), NETLINK_PORTID(skb));
2053
2054 nla_put_failure:
2055 nlmsg_cancel(skb2, nlh2);
2056 nlmsg_failure:
2057 kfree_skb(skb2);
2058 return -EMSGSIZE;
2059 }
2060
2061 /* Get protocol version */
2062
2063 static const struct nla_policy
2064 ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
2065 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
2066 };
2067
2068 static int
2069 IPSET_CBFN(ip_set_protocol, struct net *net, struct sock *ctnl,
2070 struct sk_buff *skb, const struct nlmsghdr *nlh,
2071 const struct nlattr * const attr[],
2072 struct netlink_ext_ack *extack,
2073 const struct nfnl_info *info)
2074 {
2075 struct sk_buff *skb2;
2076 struct nlmsghdr *nlh2;
2077
2078 if (unlikely(!attr[IPSET_ATTR_PROTOCOL]))
2079 return -IPSET_ERR_PROTOCOL;
2080
2081 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2082 if (!skb2)
2083 return -ENOMEM;
2084
2085 nlh2 = start_msg(skb2, NETLINK_PORTID(skb), INFO_NLH(info, nlh)->nlmsg_seq, 0,
2086 IPSET_CMD_PROTOCOL);
2087 if (!nlh2)
2088 goto nlmsg_failure;
2089 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
2090 goto nla_put_failure;
2091 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL_MIN, IPSET_PROTOCOL_MIN))
2092 goto nla_put_failure;
2093 nlmsg_end(skb2, nlh2);
2094
2095 return NFNETLINK_UNICAST(INFO_SK(info, ctnl), skb2, INFO_NET(info, net), NETLINK_PORTID(skb));
2096
2097 nla_put_failure:
2098 nlmsg_cancel(skb2, nlh2);
2099 nlmsg_failure:
2100 kfree_skb(skb2);
2101 return -EMSGSIZE;
2102 }
2103
2104 /* Get set by name or index, from userspace */
2105
2106 static int
2107 IPSET_CBFN(ip_set_byname, struct net *net, struct sock *ctnl,
2108 struct sk_buff *skb, const struct nlmsghdr *nlh,
2109 const struct nlattr * const attr[],
2110 struct netlink_ext_ack *extack,
2111 const struct nfnl_info *info)
2112 {
2113 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl, info));
2114 struct sk_buff *skb2;
2115 struct nlmsghdr *nlh2;
2116 ip_set_id_t id = IPSET_INVALID_ID;
2117 const struct ip_set *set;
2118
2119 if (unlikely(protocol_failed(attr) ||
2120 !attr[IPSET_ATTR_SETNAME]))
2121 return -IPSET_ERR_PROTOCOL;
2122
2123 set = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]), &id);
2124 if (id == IPSET_INVALID_ID)
2125 return -ENOENT;
2126
2127 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2128 if (!skb2)
2129 return -ENOMEM;
2130
2131 nlh2 = start_msg(skb2, NETLINK_PORTID(skb), INFO_NLH(info, nlh)->nlmsg_seq, 0,
2132 IPSET_CMD_GET_BYNAME);
2133 if (!nlh2)
2134 goto nlmsg_failure;
2135 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
2136 nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
2137 nla_put_net16(skb2, IPSET_ATTR_INDEX, htons(id)))
2138 goto nla_put_failure;
2139 nlmsg_end(skb2, nlh2);
2140
2141 return NFNETLINK_UNICAST(INFO_SK(info, ctnl), skb2, INFO_NET(info, net), NETLINK_PORTID(skb));
2142
2143 nla_put_failure:
2144 nlmsg_cancel(skb2, nlh2);
2145 nlmsg_failure:
2146 kfree_skb(skb2);
2147 return -EMSGSIZE;
2148 }
2149
2150 static const struct nla_policy ip_set_index_policy[IPSET_ATTR_CMD_MAX + 1] = {
2151 [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
2152 [IPSET_ATTR_INDEX] = { .type = NLA_U16 },
2153 };
2154
2155 static int
2156 IPSET_CBFN(ip_set_byindex, struct net *net, struct sock *ctnl,
2157 struct sk_buff *skb, const struct nlmsghdr *nlh,
2158 const struct nlattr * const attr[],
2159 struct netlink_ext_ack *extack,
2160 const struct nfnl_info *info)
2161 {
2162 struct ip_set_net *inst = ip_set_pernet(IPSET_SOCK_NET(net, ctnl, info));
2163 struct sk_buff *skb2;
2164 struct nlmsghdr *nlh2;
2165 ip_set_id_t id = IPSET_INVALID_ID;
2166 const struct ip_set *set;
2167
2168 if (unlikely(protocol_failed(attr) ||
2169 !attr[IPSET_ATTR_INDEX]))
2170 return -IPSET_ERR_PROTOCOL;
2171
2172 id = ip_set_get_h16(attr[IPSET_ATTR_INDEX]);
2173 if (id >= inst->ip_set_max)
2174 return -ENOENT;
2175 set = ip_set(inst, id);
2176 if (set == NULL)
2177 return -ENOENT;
2178
2179 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2180 if (!skb2)
2181 return -ENOMEM;
2182
2183 nlh2 = start_msg(skb2, NETLINK_PORTID(skb), INFO_NLH(info, nlh)->nlmsg_seq, 0,
2184 IPSET_CMD_GET_BYINDEX);
2185 if (!nlh2)
2186 goto nlmsg_failure;
2187 if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
2188 nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name))
2189 goto nla_put_failure;
2190 nlmsg_end(skb2, nlh2);
2191
2192 return NFNETLINK_UNICAST(INFO_SK(info, ctnl), skb2, INFO_NET(info, net), NETLINK_PORTID(skb));
2193
2194 nla_put_failure:
2195 nlmsg_cancel(skb2, nlh2);
2196 nlmsg_failure:
2197 kfree_skb(skb2);
2198 return -EMSGSIZE;
2199 }
2200
2201 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
2202 [IPSET_CMD_NONE] = {
2203 .call = ip_set_none,
2204 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2205 .attr_count = IPSET_ATTR_CMD_MAX,
2206 },
2207 [IPSET_CMD_CREATE] = {
2208 .call = ip_set_create,
2209 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2210 .attr_count = IPSET_ATTR_CMD_MAX,
2211 .policy = ip_set_create_policy,
2212 },
2213 [IPSET_CMD_DESTROY] = {
2214 .call = ip_set_destroy,
2215 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2216 .attr_count = IPSET_ATTR_CMD_MAX,
2217 .policy = ip_set_setname_policy,
2218 },
2219 [IPSET_CMD_FLUSH] = {
2220 .call = ip_set_flush,
2221 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2222 .attr_count = IPSET_ATTR_CMD_MAX,
2223 .policy = ip_set_setname_policy,
2224 },
2225 [IPSET_CMD_RENAME] = {
2226 .call = ip_set_rename,
2227 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2228 .attr_count = IPSET_ATTR_CMD_MAX,
2229 .policy = ip_set_setname2_policy,
2230 },
2231 [IPSET_CMD_SWAP] = {
2232 .call = ip_set_swap,
2233 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2234 .attr_count = IPSET_ATTR_CMD_MAX,
2235 .policy = ip_set_setname2_policy,
2236 },
2237 [IPSET_CMD_LIST] = {
2238 .call = ip_set_dump,
2239 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2240 .attr_count = IPSET_ATTR_CMD_MAX,
2241 .policy = ip_set_dump_policy,
2242 },
2243 [IPSET_CMD_SAVE] = {
2244 .call = ip_set_dump,
2245 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2246 .attr_count = IPSET_ATTR_CMD_MAX,
2247 .policy = ip_set_setname_policy,
2248 },
2249 [IPSET_CMD_ADD] = {
2250 .call = ip_set_uadd,
2251 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2252 .attr_count = IPSET_ATTR_CMD_MAX,
2253 .policy = ip_set_adt_policy,
2254 },
2255 [IPSET_CMD_DEL] = {
2256 .call = ip_set_udel,
2257 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2258 .attr_count = IPSET_ATTR_CMD_MAX,
2259 .policy = ip_set_adt_policy,
2260 },
2261 [IPSET_CMD_TEST] = {
2262 .call = ip_set_utest,
2263 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2264 .attr_count = IPSET_ATTR_CMD_MAX,
2265 .policy = ip_set_adt_policy,
2266 },
2267 [IPSET_CMD_HEADER] = {
2268 .call = ip_set_header,
2269 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2270 .attr_count = IPSET_ATTR_CMD_MAX,
2271 .policy = ip_set_setname_policy,
2272 },
2273 [IPSET_CMD_TYPE] = {
2274 .call = ip_set_type,
2275 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2276 .attr_count = IPSET_ATTR_CMD_MAX,
2277 .policy = ip_set_type_policy,
2278 },
2279 [IPSET_CMD_PROTOCOL] = {
2280 .call = ip_set_protocol,
2281 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2282 .attr_count = IPSET_ATTR_CMD_MAX,
2283 .policy = ip_set_protocol_policy,
2284 },
2285 [IPSET_CMD_GET_BYNAME] = {
2286 .call = ip_set_byname,
2287 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2288 .attr_count = IPSET_ATTR_CMD_MAX,
2289 .policy = ip_set_setname_policy,
2290 },
2291 [IPSET_CMD_GET_BYINDEX] = {
2292 .call = ip_set_byindex,
2293 SET_NFNL_CALLBACK_TYPE(NFNL_CB_MUTEX)
2294 .attr_count = IPSET_ATTR_CMD_MAX,
2295 .policy = ip_set_index_policy,
2296 },
2297 };
2298
2299 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
2300 .name = "ip_set",
2301 .subsys_id = NFNL_SUBSYS_IPSET,
2302 .cb_count = IPSET_MSG_MAX,
2303 .cb = ip_set_netlink_subsys_cb,
2304 };
2305
2306 /* Interface to iptables/ip6tables */
2307
2308 static int
2309 ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
2310 {
2311 unsigned int *op;
2312 void *data;
2313 int copylen = *len, ret = 0;
2314 struct net *net = sock_net(sk);
2315 struct ip_set_net *inst = ip_set_pernet(net);
2316
2317 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2318 return -EPERM;
2319 if (optval != SO_IP_SET)
2320 return -EBADF;
2321 if (*len < sizeof(unsigned int))
2322 return -EINVAL;
2323
2324 data = vmalloc(*len);
2325 if (!data)
2326 return -ENOMEM;
2327 if (copy_from_user(data, user, *len) != 0) {
2328 ret = -EFAULT;
2329 goto done;
2330 }
2331 op = data;
2332
2333 if (*op < IP_SET_OP_VERSION) {
2334 /* Check the version at the beginning of operations */
2335 struct ip_set_req_version *req_version = data;
2336
2337 if (*len < sizeof(struct ip_set_req_version)) {
2338 ret = -EINVAL;
2339 goto done;
2340 }
2341
2342 if (req_version->version < IPSET_PROTOCOL_MIN) {
2343 ret = -EPROTO;
2344 goto done;
2345 }
2346 }
2347
2348 switch (*op) {
2349 case IP_SET_OP_VERSION: {
2350 struct ip_set_req_version *req_version = data;
2351
2352 if (*len != sizeof(struct ip_set_req_version)) {
2353 ret = -EINVAL;
2354 goto done;
2355 }
2356
2357 req_version->version = IPSET_PROTOCOL;
2358 if (copy_to_user(user, req_version,
2359 sizeof(struct ip_set_req_version)))
2360 ret = -EFAULT;
2361 goto done;
2362 }
2363 case IP_SET_OP_GET_BYNAME: {
2364 struct ip_set_req_get_set *req_get = data;
2365 ip_set_id_t id;
2366
2367 if (*len != sizeof(struct ip_set_req_get_set)) {
2368 ret = -EINVAL;
2369 goto done;
2370 }
2371 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2372 nfnl_lock(NFNL_SUBSYS_IPSET);
2373 find_set_and_id(inst, req_get->set.name, &id);
2374 req_get->set.index = id;
2375 nfnl_unlock(NFNL_SUBSYS_IPSET);
2376 goto copy;
2377 }
2378 case IP_SET_OP_GET_FNAME: {
2379 struct ip_set_req_get_set_family *req_get = data;
2380 ip_set_id_t id;
2381
2382 if (*len != sizeof(struct ip_set_req_get_set_family)) {
2383 ret = -EINVAL;
2384 goto done;
2385 }
2386 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2387 nfnl_lock(NFNL_SUBSYS_IPSET);
2388 find_set_and_id(inst, req_get->set.name, &id);
2389 req_get->set.index = id;
2390 if (id != IPSET_INVALID_ID)
2391 req_get->family = ip_set(inst, id)->family;
2392 nfnl_unlock(NFNL_SUBSYS_IPSET);
2393 goto copy;
2394 }
2395 case IP_SET_OP_GET_BYINDEX: {
2396 struct ip_set_req_get_set *req_get = data;
2397 struct ip_set *set;
2398
2399 if (*len != sizeof(struct ip_set_req_get_set) ||
2400 req_get->set.index >= inst->ip_set_max) {
2401 ret = -EINVAL;
2402 goto done;
2403 }
2404 nfnl_lock(NFNL_SUBSYS_IPSET);
2405 set = ip_set(inst, req_get->set.index);
2406 ret = strscpy(req_get->set.name, set ? set->name : "",
2407 IPSET_MAXNAMELEN);
2408 nfnl_unlock(NFNL_SUBSYS_IPSET);
2409 if (ret < 0)
2410 goto done;
2411 goto copy;
2412 }
2413 default:
2414 ret = -EBADMSG;
2415 goto done;
2416 } /* end of switch(op) */
2417
2418 copy:
2419 if (copy_to_user(user, data, copylen))
2420 ret = -EFAULT;
2421
2422 done:
2423 vfree(data);
2424 if (ret > 0)
2425 ret = 0;
2426 return ret;
2427 }
2428
2429 static struct nf_sockopt_ops so_set __read_mostly = {
2430 .pf = PF_INET,
2431 .get_optmin = SO_IP_SET,
2432 .get_optmax = SO_IP_SET + 1,
2433 .get = ip_set_sockfn_get,
2434 .owner = THIS_MODULE,
2435 };
2436
2437 static int __net_init
2438 ip_set_net_init(struct net *net)
2439 {
2440 struct ip_set_net *inst;
2441 struct ip_set **list;
2442
2443 #ifdef HAVE_NET_OPS_ID
2444 inst = ip_set_pernet(net);
2445 #else
2446 int err;
2447
2448 inst = kzalloc(sizeof(struct ip_set_net), GFP_KERNEL);
2449 if (!inst)
2450 return -ENOMEM;
2451 err = net_assign_generic(net, ip_set_net_id, inst);
2452 if (err < 0)
2453 goto err_alloc;
2454 #endif
2455 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
2456 if (inst->ip_set_max >= IPSET_INVALID_ID)
2457 inst->ip_set_max = IPSET_INVALID_ID - 1;
2458
2459 list = kvcalloc(inst->ip_set_max, sizeof(struct ip_set *), GFP_KERNEL);
2460 if (!list)
2461 #ifdef HAVE_NET_OPS_ID
2462 return -ENOMEM;
2463 #else
2464 goto err_alloc;
2465 #endif
2466 inst->is_deleted = false;
2467 inst->is_destroyed = false;
2468 rcu_assign_pointer(inst->ip_set_list, list);
2469 return 0;
2470
2471 #ifndef HAVE_NET_OPS_ID
2472 err_alloc:
2473 kfree(inst);
2474 return err;
2475 #endif
2476 }
2477
2478 static void __net_exit
2479 ip_set_net_exit(struct net *net)
2480 {
2481 struct ip_set_net *inst = ip_set_pernet(net);
2482
2483 struct ip_set *set = NULL;
2484 ip_set_id_t i;
2485
2486 inst->is_deleted = true; /* flag for ip_set_nfnl_put */
2487
2488 nfnl_lock(NFNL_SUBSYS_IPSET);
2489 for (i = 0; i < inst->ip_set_max; i++) {
2490 set = ip_set(inst, i);
2491 if (set) {
2492 ip_set(inst, i) = NULL;
2493 ip_set_destroy_set(set);
2494 }
2495 }
2496 nfnl_unlock(NFNL_SUBSYS_IPSET);
2497 kvfree(rcu_dereference_protected(inst->ip_set_list, 1));
2498 #ifndef HAVE_NET_OPS_ID
2499 kvfree(inst);
2500 #endif
2501 }
2502
2503 static struct pernet_operations ip_set_net_ops = {
2504 .init = ip_set_net_init,
2505 .exit = ip_set_net_exit,
2506 #ifdef HAVE_NET_OPS_ID
2507 .id = &ip_set_net_id,
2508 .size = sizeof(struct ip_set_net),
2509 #ifdef HAVE_NET_OPS_ASYNC
2510 .async = true,
2511 #endif
2512 #endif
2513 };
2514
2515 #ifdef HAVE_NET_OPS_ID
2516 #define REGISTER_PERNET_SUBSYS(s) \
2517 register_pernet_subsys(s)
2518 #define UNREGISTER_PERNET_SUBSYS(s) \
2519 unregister_pernet_subsys(s);
2520 #else
2521 #define REGISTER_PERNET_SUBSYS(s) \
2522 register_pernet_gen_device(&ip_set_net_id, s)
2523 #define UNREGISTER_PERNET_SUBSYS(s) \
2524 unregister_pernet_gen_device(ip_set_net_id, s);
2525 #endif
2526
2527
2528 static int __init
2529 ip_set_init(void)
2530 {
2531 int ret = REGISTER_PERNET_SUBSYS(&ip_set_net_ops);
2532
2533 if (ret) {
2534 pr_err("ip_set: cannot register pernet_subsys.\n");
2535 return ret;
2536 }
2537
2538 ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
2539 if (ret != 0) {
2540 pr_err("ip_set: cannot register with nfnetlink.\n");
2541 UNREGISTER_PERNET_SUBSYS(&ip_set_net_ops);
2542 return ret;
2543 }
2544
2545 ret = nf_register_sockopt(&so_set);
2546 if (ret != 0) {
2547 pr_err("SO_SET registry failed: %d\n", ret);
2548 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2549 UNREGISTER_PERNET_SUBSYS(&ip_set_net_ops);
2550 return ret;
2551 }
2552
2553 return 0;
2554 }
2555
2556 static void __exit
2557 ip_set_fini(void)
2558 {
2559 nf_unregister_sockopt(&so_set);
2560 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2561
2562 UNREGISTER_PERNET_SUBSYS(&ip_set_net_ops);
2563 pr_debug("these are the famous last words\n");
2564 }
2565
2566 module_init(ip_set_init);
2567 module_exit(ip_set_fini);