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