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