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