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