OSDN Git Service

Linux 6.6-rc1
[tomoyo/tomoyo-test1.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         strscpy(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         ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
743
744         if (ret == -EAGAIN) {
745                 /* Type requests element to be completed */
746                 pr_debug("element must be completed, ADD is triggered\n");
747                 ip_set_lock(set);
748                 set->variant->kadt(set, skb, par, IPSET_ADD, opt);
749                 ip_set_unlock(set);
750                 ret = 1;
751         } else {
752                 /* --return-nomatch: invert matched element */
753                 if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
754                     (set->type->features & IPSET_TYPE_NOMATCH) &&
755                     (ret > 0 || ret == -ENOTEMPTY))
756                         ret = -ret;
757         }
758
759         /* Convert error codes to nomatch */
760         return (ret < 0 ? 0 : ret);
761 }
762 EXPORT_SYMBOL_GPL(ip_set_test);
763
764 int
765 ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
766            const struct xt_action_param *par, struct ip_set_adt_opt *opt)
767 {
768         struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
769         int ret;
770
771         BUG_ON(!set);
772         pr_debug("set %s, index %u\n", set->name, index);
773
774         if (opt->dim < set->type->dimension ||
775             !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
776                 return -IPSET_ERR_TYPE_MISMATCH;
777
778         ip_set_lock(set);
779         ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
780         ip_set_unlock(set);
781
782         return ret;
783 }
784 EXPORT_SYMBOL_GPL(ip_set_add);
785
786 int
787 ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
788            const struct xt_action_param *par, struct ip_set_adt_opt *opt)
789 {
790         struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
791         int ret = 0;
792
793         BUG_ON(!set);
794         pr_debug("set %s, index %u\n", set->name, index);
795
796         if (opt->dim < set->type->dimension ||
797             !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
798                 return -IPSET_ERR_TYPE_MISMATCH;
799
800         ip_set_lock(set);
801         ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
802         ip_set_unlock(set);
803
804         return ret;
805 }
806 EXPORT_SYMBOL_GPL(ip_set_del);
807
808 /* Find set by name, reference it once. The reference makes sure the
809  * thing pointed to, does not go away under our feet.
810  *
811  */
812 ip_set_id_t
813 ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
814 {
815         ip_set_id_t i, index = IPSET_INVALID_ID;
816         struct ip_set *s;
817         struct ip_set_net *inst = ip_set_pernet(net);
818
819         rcu_read_lock();
820         for (i = 0; i < inst->ip_set_max; i++) {
821                 s = rcu_dereference(inst->ip_set_list)[i];
822                 if (s && STRNCMP(s->name, name)) {
823                         __ip_set_get(s);
824                         index = i;
825                         *set = s;
826                         break;
827                 }
828         }
829         rcu_read_unlock();
830
831         return index;
832 }
833 EXPORT_SYMBOL_GPL(ip_set_get_byname);
834
835 /* If the given set pointer points to a valid set, decrement
836  * reference count by 1. The caller shall not assume the index
837  * to be valid, after calling this function.
838  *
839  */
840
841 static void
842 __ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
843 {
844         struct ip_set *set;
845
846         rcu_read_lock();
847         set = rcu_dereference(inst->ip_set_list)[index];
848         if (set)
849                 __ip_set_put(set);
850         rcu_read_unlock();
851 }
852
853 void
854 ip_set_put_byindex(struct net *net, ip_set_id_t index)
855 {
856         struct ip_set_net *inst = ip_set_pernet(net);
857
858         __ip_set_put_byindex(inst, index);
859 }
860 EXPORT_SYMBOL_GPL(ip_set_put_byindex);
861
862 /* Get the name of a set behind a set index.
863  * Set itself is protected by RCU, but its name isn't: to protect against
864  * renaming, grab ip_set_ref_lock as reader (see ip_set_rename()) and copy the
865  * name.
866  */
867 void
868 ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name)
869 {
870         struct ip_set *set = ip_set_rcu_get(net, index);
871
872         BUG_ON(!set);
873
874         read_lock_bh(&ip_set_ref_lock);
875         strscpy_pad(name, set->name, IPSET_MAXNAMELEN);
876         read_unlock_bh(&ip_set_ref_lock);
877 }
878 EXPORT_SYMBOL_GPL(ip_set_name_byindex);
879
880 /* Routines to call by external subsystems, which do not
881  * call nfnl_lock for us.
882  */
883
884 /* Find set by index, reference it once. The reference makes sure the
885  * thing pointed to, does not go away under our feet.
886  *
887  * The nfnl mutex is used in the function.
888  */
889 ip_set_id_t
890 ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
891 {
892         struct ip_set *set;
893         struct ip_set_net *inst = ip_set_pernet(net);
894
895         if (index >= inst->ip_set_max)
896                 return IPSET_INVALID_ID;
897
898         nfnl_lock(NFNL_SUBSYS_IPSET);
899         set = ip_set(inst, index);
900         if (set)
901                 __ip_set_get(set);
902         else
903                 index = IPSET_INVALID_ID;
904         nfnl_unlock(NFNL_SUBSYS_IPSET);
905
906         return index;
907 }
908 EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
909
910 /* If the given set pointer points to a valid set, decrement
911  * reference count by 1. The caller shall not assume the index
912  * to be valid, after calling this function.
913  *
914  * The nfnl mutex is used in the function.
915  */
916 void
917 ip_set_nfnl_put(struct net *net, ip_set_id_t index)
918 {
919         struct ip_set *set;
920         struct ip_set_net *inst = ip_set_pernet(net);
921
922         nfnl_lock(NFNL_SUBSYS_IPSET);
923         if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
924                 set = ip_set(inst, index);
925                 if (set)
926                         __ip_set_put(set);
927         }
928         nfnl_unlock(NFNL_SUBSYS_IPSET);
929 }
930 EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
931
932 /* Communication protocol with userspace over netlink.
933  *
934  * The commands are serialized by the nfnl mutex.
935  */
936
937 static inline u8 protocol(const struct nlattr * const tb[])
938 {
939         return nla_get_u8(tb[IPSET_ATTR_PROTOCOL]);
940 }
941
942 static inline bool
943 protocol_failed(const struct nlattr * const tb[])
944 {
945         return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) != IPSET_PROTOCOL;
946 }
947
948 static inline bool
949 protocol_min_failed(const struct nlattr * const tb[])
950 {
951         return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) < IPSET_PROTOCOL_MIN;
952 }
953
954 static inline u32
955 flag_exist(const struct nlmsghdr *nlh)
956 {
957         return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
958 }
959
960 static struct nlmsghdr *
961 start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
962           enum ipset_cmd cmd)
963 {
964         return nfnl_msg_put(skb, portid, seq,
965                             nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd), flags,
966                             NFPROTO_IPV4, NFNETLINK_V0, 0);
967 }
968
969 /* Create a set */
970
971 static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
972         [IPSET_ATTR_PROTOCOL]   = { .type = NLA_U8 },
973         [IPSET_ATTR_SETNAME]    = { .type = NLA_NUL_STRING,
974                                     .len = IPSET_MAXNAMELEN - 1 },
975         [IPSET_ATTR_TYPENAME]   = { .type = NLA_NUL_STRING,
976                                     .len = IPSET_MAXNAMELEN - 1},
977         [IPSET_ATTR_REVISION]   = { .type = NLA_U8 },
978         [IPSET_ATTR_FAMILY]     = { .type = NLA_U8 },
979         [IPSET_ATTR_DATA]       = { .type = NLA_NESTED },
980 };
981
982 static struct ip_set *
983 find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
984 {
985         struct ip_set *set = NULL;
986         ip_set_id_t i;
987
988         *id = IPSET_INVALID_ID;
989         for (i = 0; i < inst->ip_set_max; i++) {
990                 set = ip_set(inst, i);
991                 if (set && STRNCMP(set->name, name)) {
992                         *id = i;
993                         break;
994                 }
995         }
996         return (*id == IPSET_INVALID_ID ? NULL : set);
997 }
998
999 static inline struct ip_set *
1000 find_set(struct ip_set_net *inst, const char *name)
1001 {
1002         ip_set_id_t id;
1003
1004         return find_set_and_id(inst, name, &id);
1005 }
1006
1007 static int
1008 find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
1009              struct ip_set **set)
1010 {
1011         struct ip_set *s;
1012         ip_set_id_t i;
1013
1014         *index = IPSET_INVALID_ID;
1015         for (i = 0;  i < inst->ip_set_max; i++) {
1016                 s = ip_set(inst, i);
1017                 if (!s) {
1018                         if (*index == IPSET_INVALID_ID)
1019                                 *index = i;
1020                 } else if (STRNCMP(name, s->name)) {
1021                         /* Name clash */
1022                         *set = s;
1023                         return -EEXIST;
1024                 }
1025         }
1026         if (*index == IPSET_INVALID_ID)
1027                 /* No free slot remained */
1028                 return -IPSET_ERR_MAX_SETS;
1029         return 0;
1030 }
1031
1032 static int ip_set_none(struct sk_buff *skb, const struct nfnl_info *info,
1033                        const struct nlattr * const attr[])
1034 {
1035         return -EOPNOTSUPP;
1036 }
1037
1038 static int ip_set_create(struct sk_buff *skb, const struct nfnl_info *info,
1039                          const struct nlattr * const attr[])
1040 {
1041         struct ip_set_net *inst = ip_set_pernet(info->net);
1042         struct ip_set *set, *clash = NULL;
1043         ip_set_id_t index = IPSET_INVALID_ID;
1044         struct nlattr *tb[IPSET_ATTR_CREATE_MAX + 1] = {};
1045         const char *name, *typename;
1046         u8 family, revision;
1047         u32 flags = flag_exist(info->nlh);
1048         int ret = 0;
1049
1050         if (unlikely(protocol_min_failed(attr) ||
1051                      !attr[IPSET_ATTR_SETNAME] ||
1052                      !attr[IPSET_ATTR_TYPENAME] ||
1053                      !attr[IPSET_ATTR_REVISION] ||
1054                      !attr[IPSET_ATTR_FAMILY] ||
1055                      (attr[IPSET_ATTR_DATA] &&
1056                       !flag_nested(attr[IPSET_ATTR_DATA]))))
1057                 return -IPSET_ERR_PROTOCOL;
1058
1059         name = nla_data(attr[IPSET_ATTR_SETNAME]);
1060         typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1061         family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1062         revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
1063         pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
1064                  name, typename, family_name(family), revision);
1065
1066         /* First, and without any locks, allocate and initialize
1067          * a normal base set structure.
1068          */
1069         set = kzalloc(sizeof(*set), GFP_KERNEL);
1070         if (!set)
1071                 return -ENOMEM;
1072         spin_lock_init(&set->lock);
1073         strscpy(set->name, name, IPSET_MAXNAMELEN);
1074         set->family = family;
1075         set->revision = revision;
1076
1077         /* Next, check that we know the type, and take
1078          * a reference on the type, to make sure it stays available
1079          * while constructing our new set.
1080          *
1081          * After referencing the type, we try to create the type
1082          * specific part of the set without holding any locks.
1083          */
1084         ret = find_set_type_get(typename, family, revision, &set->type);
1085         if (ret)
1086                 goto out;
1087
1088         /* Without holding any locks, create private part. */
1089         if (attr[IPSET_ATTR_DATA] &&
1090             nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
1091                              set->type->create_policy, NULL)) {
1092                 ret = -IPSET_ERR_PROTOCOL;
1093                 goto put_out;
1094         }
1095         /* Set create flags depending on the type revision */
1096         set->flags |= set->type->create_flags[revision];
1097
1098         ret = set->type->create(info->net, set, tb, flags);
1099         if (ret != 0)
1100                 goto put_out;
1101
1102         /* BTW, ret==0 here. */
1103
1104         /* Here, we have a valid, constructed set and we are protected
1105          * by the nfnl mutex. Find the first free index in ip_set_list
1106          * and check clashing.
1107          */
1108         ret = find_free_id(inst, set->name, &index, &clash);
1109         if (ret == -EEXIST) {
1110                 /* If this is the same set and requested, ignore error */
1111                 if ((flags & IPSET_FLAG_EXIST) &&
1112                     STRNCMP(set->type->name, clash->type->name) &&
1113                     set->type->family == clash->type->family &&
1114                     set->type->revision_min == clash->type->revision_min &&
1115                     set->type->revision_max == clash->type->revision_max &&
1116                     set->variant->same_set(set, clash))
1117                         ret = 0;
1118                 goto cleanup;
1119         } else if (ret == -IPSET_ERR_MAX_SETS) {
1120                 struct ip_set **list, **tmp;
1121                 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
1122
1123                 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
1124                         /* Wraparound */
1125                         goto cleanup;
1126
1127                 list = kvcalloc(i, sizeof(struct ip_set *), GFP_KERNEL);
1128                 if (!list)
1129                         goto cleanup;
1130                 /* nfnl mutex is held, both lists are valid */
1131                 tmp = ip_set_dereference(inst->ip_set_list);
1132                 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
1133                 rcu_assign_pointer(inst->ip_set_list, list);
1134                 /* Make sure all current packets have passed through */
1135                 synchronize_net();
1136                 /* Use new list */
1137                 index = inst->ip_set_max;
1138                 inst->ip_set_max = i;
1139                 kvfree(tmp);
1140                 ret = 0;
1141         } else if (ret) {
1142                 goto cleanup;
1143         }
1144
1145         /* Finally! Add our shiny new set to the list, and be done. */
1146         pr_debug("create: '%s' created with index %u!\n", set->name, index);
1147         ip_set(inst, index) = set;
1148
1149         return ret;
1150
1151 cleanup:
1152         set->variant->destroy(set);
1153 put_out:
1154         module_put(set->type->me);
1155 out:
1156         kfree(set);
1157         return ret;
1158 }
1159
1160 /* Destroy sets */
1161
1162 static const struct nla_policy
1163 ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
1164         [IPSET_ATTR_PROTOCOL]   = { .type = NLA_U8 },
1165         [IPSET_ATTR_SETNAME]    = { .type = NLA_NUL_STRING,
1166                                     .len = IPSET_MAXNAMELEN - 1 },
1167 };
1168
1169 static void
1170 ip_set_destroy_set(struct ip_set *set)
1171 {
1172         pr_debug("set: %s\n",  set->name);
1173
1174         /* Must call it without holding any lock */
1175         set->variant->destroy(set);
1176         module_put(set->type->me);
1177         kfree(set);
1178 }
1179
1180 static int ip_set_destroy(struct sk_buff *skb, const struct nfnl_info *info,
1181                           const struct nlattr * const attr[])
1182 {
1183         struct ip_set_net *inst = ip_set_pernet(info->net);
1184         struct ip_set *s;
1185         ip_set_id_t i;
1186         int ret = 0;
1187
1188         if (unlikely(protocol_min_failed(attr)))
1189                 return -IPSET_ERR_PROTOCOL;
1190
1191         /* Must wait for flush to be really finished in list:set */
1192         rcu_barrier();
1193
1194         /* Commands are serialized and references are
1195          * protected by the ip_set_ref_lock.
1196          * External systems (i.e. xt_set) must call
1197          * ip_set_put|get_nfnl_* functions, that way we
1198          * can safely check references here.
1199          *
1200          * list:set timer can only decrement the reference
1201          * counter, so if it's already zero, we can proceed
1202          * without holding the lock.
1203          */
1204         read_lock_bh(&ip_set_ref_lock);
1205         if (!attr[IPSET_ATTR_SETNAME]) {
1206                 for (i = 0; i < inst->ip_set_max; i++) {
1207                         s = ip_set(inst, i);
1208                         if (s && (s->ref || s->ref_netlink)) {
1209                                 ret = -IPSET_ERR_BUSY;
1210                                 goto out;
1211                         }
1212                 }
1213                 inst->is_destroyed = true;
1214                 read_unlock_bh(&ip_set_ref_lock);
1215                 for (i = 0; i < inst->ip_set_max; i++) {
1216                         s = ip_set(inst, i);
1217                         if (s) {
1218                                 ip_set(inst, i) = NULL;
1219                                 ip_set_destroy_set(s);
1220                         }
1221                 }
1222                 /* Modified by ip_set_destroy() only, which is serialized */
1223                 inst->is_destroyed = false;
1224         } else {
1225                 u32 flags = flag_exist(info->nlh);
1226                 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1227                                     &i);
1228                 if (!s) {
1229                         if (!(flags & IPSET_FLAG_EXIST))
1230                                 ret = -ENOENT;
1231                         goto out;
1232                 } else if (s->ref || s->ref_netlink) {
1233                         ret = -IPSET_ERR_BUSY;
1234                         goto out;
1235                 }
1236                 ip_set(inst, i) = NULL;
1237                 read_unlock_bh(&ip_set_ref_lock);
1238
1239                 ip_set_destroy_set(s);
1240         }
1241         return 0;
1242 out:
1243         read_unlock_bh(&ip_set_ref_lock);
1244         return ret;
1245 }
1246
1247 /* Flush sets */
1248
1249 static void
1250 ip_set_flush_set(struct ip_set *set)
1251 {
1252         pr_debug("set: %s\n",  set->name);
1253
1254         ip_set_lock(set);
1255         set->variant->flush(set);
1256         ip_set_unlock(set);
1257 }
1258
1259 static int ip_set_flush(struct sk_buff *skb, const struct nfnl_info *info,
1260                         const struct nlattr * const attr[])
1261 {
1262         struct ip_set_net *inst = ip_set_pernet(info->net);
1263         struct ip_set *s;
1264         ip_set_id_t i;
1265
1266         if (unlikely(protocol_min_failed(attr)))
1267                 return -IPSET_ERR_PROTOCOL;
1268
1269         if (!attr[IPSET_ATTR_SETNAME]) {
1270                 for (i = 0; i < inst->ip_set_max; i++) {
1271                         s = ip_set(inst, i);
1272                         if (s)
1273                                 ip_set_flush_set(s);
1274                 }
1275         } else {
1276                 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1277                 if (!s)
1278                         return -ENOENT;
1279
1280                 ip_set_flush_set(s);
1281         }
1282
1283         return 0;
1284 }
1285
1286 /* Rename a set */
1287
1288 static const struct nla_policy
1289 ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
1290         [IPSET_ATTR_PROTOCOL]   = { .type = NLA_U8 },
1291         [IPSET_ATTR_SETNAME]    = { .type = NLA_NUL_STRING,
1292                                     .len = IPSET_MAXNAMELEN - 1 },
1293         [IPSET_ATTR_SETNAME2]   = { .type = NLA_NUL_STRING,
1294                                     .len = IPSET_MAXNAMELEN - 1 },
1295 };
1296
1297 static int ip_set_rename(struct sk_buff *skb, const struct nfnl_info *info,
1298                          const struct nlattr * const attr[])
1299 {
1300         struct ip_set_net *inst = ip_set_pernet(info->net);
1301         struct ip_set *set, *s;
1302         const char *name2;
1303         ip_set_id_t i;
1304         int ret = 0;
1305
1306         if (unlikely(protocol_min_failed(attr) ||
1307                      !attr[IPSET_ATTR_SETNAME] ||
1308                      !attr[IPSET_ATTR_SETNAME2]))
1309                 return -IPSET_ERR_PROTOCOL;
1310
1311         set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1312         if (!set)
1313                 return -ENOENT;
1314
1315         write_lock_bh(&ip_set_ref_lock);
1316         if (set->ref != 0 || set->ref_netlink != 0) {
1317                 ret = -IPSET_ERR_REFERENCED;
1318                 goto out;
1319         }
1320
1321         name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1322         for (i = 0; i < inst->ip_set_max; i++) {
1323                 s = ip_set(inst, i);
1324                 if (s && STRNCMP(s->name, name2)) {
1325                         ret = -IPSET_ERR_EXIST_SETNAME2;
1326                         goto out;
1327                 }
1328         }
1329         strscpy_pad(set->name, name2, IPSET_MAXNAMELEN);
1330
1331 out:
1332         write_unlock_bh(&ip_set_ref_lock);
1333         return ret;
1334 }
1335
1336 /* Swap two sets so that name/index points to the other.
1337  * References and set names are also swapped.
1338  *
1339  * The commands are serialized by the nfnl mutex and references are
1340  * protected by the ip_set_ref_lock. The kernel interfaces
1341  * do not hold the mutex but the pointer settings are atomic
1342  * so the ip_set_list always contains valid pointers to the sets.
1343  */
1344
1345 static int ip_set_swap(struct sk_buff *skb, const struct nfnl_info *info,
1346                        const struct nlattr * const attr[])
1347 {
1348         struct ip_set_net *inst = ip_set_pernet(info->net);
1349         struct ip_set *from, *to;
1350         ip_set_id_t from_id, to_id;
1351         char from_name[IPSET_MAXNAMELEN];
1352
1353         if (unlikely(protocol_min_failed(attr) ||
1354                      !attr[IPSET_ATTR_SETNAME] ||
1355                      !attr[IPSET_ATTR_SETNAME2]))
1356                 return -IPSET_ERR_PROTOCOL;
1357
1358         from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1359                                &from_id);
1360         if (!from)
1361                 return -ENOENT;
1362
1363         to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1364                              &to_id);
1365         if (!to)
1366                 return -IPSET_ERR_EXIST_SETNAME2;
1367
1368         /* Features must not change.
1369          * Not an artifical restriction anymore, as we must prevent
1370          * possible loops created by swapping in setlist type of sets.
1371          */
1372         if (!(from->type->features == to->type->features &&
1373               from->family == to->family))
1374                 return -IPSET_ERR_TYPE_MISMATCH;
1375
1376         write_lock_bh(&ip_set_ref_lock);
1377
1378         if (from->ref_netlink || to->ref_netlink) {
1379                 write_unlock_bh(&ip_set_ref_lock);
1380                 return -EBUSY;
1381         }
1382
1383         strscpy_pad(from_name, from->name, IPSET_MAXNAMELEN);
1384         strscpy_pad(from->name, to->name, IPSET_MAXNAMELEN);
1385         strscpy_pad(to->name, from_name, IPSET_MAXNAMELEN);
1386
1387         swap(from->ref, to->ref);
1388         ip_set(inst, from_id) = to;
1389         ip_set(inst, to_id) = from;
1390         write_unlock_bh(&ip_set_ref_lock);
1391
1392         return 0;
1393 }
1394
1395 /* List/save set data */
1396
1397 #define DUMP_INIT       0
1398 #define DUMP_ALL        1
1399 #define DUMP_ONE        2
1400 #define DUMP_LAST       3
1401
1402 #define DUMP_TYPE(arg)          (((u32)(arg)) & 0x0000FFFF)
1403 #define DUMP_FLAGS(arg)         (((u32)(arg)) >> 16)
1404
1405 int
1406 ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
1407 {
1408         u32 cadt_flags = 0;
1409
1410         if (SET_WITH_TIMEOUT(set))
1411                 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
1412                                            htonl(set->timeout))))
1413                         return -EMSGSIZE;
1414         if (SET_WITH_COUNTER(set))
1415                 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
1416         if (SET_WITH_COMMENT(set))
1417                 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
1418         if (SET_WITH_SKBINFO(set))
1419                 cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
1420         if (SET_WITH_FORCEADD(set))
1421                 cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
1422
1423         if (!cadt_flags)
1424                 return 0;
1425         return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
1426 }
1427 EXPORT_SYMBOL_GPL(ip_set_put_flags);
1428
1429 static int
1430 ip_set_dump_done(struct netlink_callback *cb)
1431 {
1432         if (cb->args[IPSET_CB_ARG0]) {
1433                 struct ip_set_net *inst =
1434                         (struct ip_set_net *)cb->args[IPSET_CB_NET];
1435                 ip_set_id_t index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1436                 struct ip_set *set = ip_set_ref_netlink(inst, index);
1437
1438                 if (set->variant->uref)
1439                         set->variant->uref(set, cb, false);
1440                 pr_debug("release set %s\n", set->name);
1441                 __ip_set_put_netlink(set);
1442         }
1443         return 0;
1444 }
1445
1446 static inline void
1447 dump_attrs(struct nlmsghdr *nlh)
1448 {
1449         const struct nlattr *attr;
1450         int rem;
1451
1452         pr_debug("dump nlmsg\n");
1453         nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
1454                 pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
1455         }
1456 }
1457
1458 static const struct nla_policy
1459 ip_set_dump_policy[IPSET_ATTR_CMD_MAX + 1] = {
1460         [IPSET_ATTR_PROTOCOL]   = { .type = NLA_U8 },
1461         [IPSET_ATTR_SETNAME]    = { .type = NLA_NUL_STRING,
1462                                     .len = IPSET_MAXNAMELEN - 1 },
1463         [IPSET_ATTR_FLAGS]      = { .type = NLA_U32 },
1464 };
1465
1466 static int
1467 ip_set_dump_start(struct netlink_callback *cb)
1468 {
1469         struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
1470         int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1471         struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1472         struct nlattr *attr = (void *)nlh + min_len;
1473         struct sk_buff *skb = cb->skb;
1474         struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1475         u32 dump_type;
1476         int ret;
1477
1478         ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, attr,
1479                         nlh->nlmsg_len - min_len,
1480                         ip_set_dump_policy, NULL);
1481         if (ret)
1482                 goto error;
1483
1484         cb->args[IPSET_CB_PROTO] = nla_get_u8(cda[IPSET_ATTR_PROTOCOL]);
1485         if (cda[IPSET_ATTR_SETNAME]) {
1486                 ip_set_id_t index;
1487                 struct ip_set *set;
1488
1489                 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
1490                                       &index);
1491                 if (!set) {
1492                         ret = -ENOENT;
1493                         goto error;
1494                 }
1495                 dump_type = DUMP_ONE;
1496                 cb->args[IPSET_CB_INDEX] = index;
1497         } else {
1498                 dump_type = DUMP_ALL;
1499         }
1500
1501         if (cda[IPSET_ATTR_FLAGS]) {
1502                 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1503
1504                 dump_type |= (f << 16);
1505         }
1506         cb->args[IPSET_CB_NET] = (unsigned long)inst;
1507         cb->args[IPSET_CB_DUMP] = dump_type;
1508
1509         return 0;
1510
1511 error:
1512         /* We have to create and send the error message manually :-( */
1513         if (nlh->nlmsg_flags & NLM_F_ACK) {
1514                 netlink_ack(cb->skb, nlh, ret, NULL);
1515         }
1516         return ret;
1517 }
1518
1519 static int
1520 ip_set_dump_do(struct sk_buff *skb, struct netlink_callback *cb)
1521 {
1522         ip_set_id_t index = IPSET_INVALID_ID, max;
1523         struct ip_set *set = NULL;
1524         struct nlmsghdr *nlh = NULL;
1525         unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0;
1526         struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1527         u32 dump_type, dump_flags;
1528         bool is_destroyed;
1529         int ret = 0;
1530
1531         if (!cb->args[IPSET_CB_DUMP])
1532                 return -EINVAL;
1533
1534         if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
1535                 goto out;
1536
1537         dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1538         dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1539         max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1540                                     : inst->ip_set_max;
1541 dump_last:
1542         pr_debug("dump type, flag: %u %u index: %ld\n",
1543                  dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1544         for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
1545                 index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
1546                 write_lock_bh(&ip_set_ref_lock);
1547                 set = ip_set(inst, index);
1548                 is_destroyed = inst->is_destroyed;
1549                 if (!set || is_destroyed) {
1550                         write_unlock_bh(&ip_set_ref_lock);
1551                         if (dump_type == DUMP_ONE) {
1552                                 ret = -ENOENT;
1553                                 goto out;
1554                         }
1555                         if (is_destroyed) {
1556                                 /* All sets are just being destroyed */
1557                                 ret = 0;
1558                                 goto out;
1559                         }
1560                         continue;
1561                 }
1562                 /* When dumping all sets, we must dump "sorted"
1563                  * so that lists (unions of sets) are dumped last.
1564                  */
1565                 if (dump_type != DUMP_ONE &&
1566                     ((dump_type == DUMP_ALL) ==
1567                      !!(set->type->features & IPSET_DUMP_LAST))) {
1568                         write_unlock_bh(&ip_set_ref_lock);
1569                         continue;
1570                 }
1571                 pr_debug("List set: %s\n", set->name);
1572                 if (!cb->args[IPSET_CB_ARG0]) {
1573                         /* Start listing: make sure set won't be destroyed */
1574                         pr_debug("reference set\n");
1575                         set->ref_netlink++;
1576                 }
1577                 write_unlock_bh(&ip_set_ref_lock);
1578                 nlh = start_msg(skb, NETLINK_CB(cb->skb).portid,
1579                                 cb->nlh->nlmsg_seq, flags,
1580                                 IPSET_CMD_LIST);
1581                 if (!nlh) {
1582                         ret = -EMSGSIZE;
1583                         goto release_refcount;
1584                 }
1585                 if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL,
1586                                cb->args[IPSET_CB_PROTO]) ||
1587                     nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
1588                         goto nla_put_failure;
1589                 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1590                         goto next_set;
1591                 switch (cb->args[IPSET_CB_ARG0]) {
1592                 case 0:
1593                         /* Core header data */
1594                         if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
1595                                            set->type->name) ||
1596                             nla_put_u8(skb, IPSET_ATTR_FAMILY,
1597                                        set->family) ||
1598                             nla_put_u8(skb, IPSET_ATTR_REVISION,
1599                                        set->revision))
1600                                 goto nla_put_failure;
1601                         if (cb->args[IPSET_CB_PROTO] > IPSET_PROTOCOL_MIN &&
1602                             nla_put_net16(skb, IPSET_ATTR_INDEX, htons(index)))
1603                                 goto nla_put_failure;
1604                         ret = set->variant->head(set, skb);
1605                         if (ret < 0)
1606                                 goto release_refcount;
1607                         if (dump_flags & IPSET_FLAG_LIST_HEADER)
1608                                 goto next_set;
1609                         if (set->variant->uref)
1610                                 set->variant->uref(set, cb, true);
1611                         fallthrough;
1612                 default:
1613                         ret = set->variant->list(set, skb, cb);
1614                         if (!cb->args[IPSET_CB_ARG0])
1615                                 /* Set is done, proceed with next one */
1616                                 goto next_set;
1617                         goto release_refcount;
1618                 }
1619         }
1620         /* If we dump all sets, continue with dumping last ones */
1621         if (dump_type == DUMP_ALL) {
1622                 dump_type = DUMP_LAST;
1623                 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1624                 cb->args[IPSET_CB_INDEX] = 0;
1625                 if (set && set->variant->uref)
1626                         set->variant->uref(set, cb, false);
1627                 goto dump_last;
1628         }
1629         goto out;
1630
1631 nla_put_failure:
1632         ret = -EFAULT;
1633 next_set:
1634         if (dump_type == DUMP_ONE)
1635                 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
1636         else
1637                 cb->args[IPSET_CB_INDEX]++;
1638 release_refcount:
1639         /* If there was an error or set is done, release set */
1640         if (ret || !cb->args[IPSET_CB_ARG0]) {
1641                 set = ip_set_ref_netlink(inst, index);
1642                 if (set->variant->uref)
1643                         set->variant->uref(set, cb, false);
1644                 pr_debug("release set %s\n", set->name);
1645                 __ip_set_put_netlink(set);
1646                 cb->args[IPSET_CB_ARG0] = 0;
1647         }
1648 out:
1649         if (nlh) {
1650                 nlmsg_end(skb, nlh);
1651                 pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1652                 dump_attrs(nlh);
1653         }
1654
1655         return ret < 0 ? ret : skb->len;
1656 }
1657
1658 static int ip_set_dump(struct sk_buff *skb, const struct nfnl_info *info,
1659                        const struct nlattr * const attr[])
1660 {
1661         if (unlikely(protocol_min_failed(attr)))
1662                 return -IPSET_ERR_PROTOCOL;
1663
1664         {
1665                 struct netlink_dump_control c = {
1666                         .start = ip_set_dump_start,
1667                         .dump = ip_set_dump_do,
1668                         .done = ip_set_dump_done,
1669                 };
1670                 return netlink_dump_start(info->sk, skb, info->nlh, &c);
1671         }
1672 }
1673
1674 /* Add, del and test */
1675
1676 static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1677         [IPSET_ATTR_PROTOCOL]   = { .type = NLA_U8 },
1678         [IPSET_ATTR_SETNAME]    = { .type = NLA_NUL_STRING,
1679                                     .len = IPSET_MAXNAMELEN - 1 },
1680         [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
1681         [IPSET_ATTR_DATA]       = { .type = NLA_NESTED },
1682         [IPSET_ATTR_ADT]        = { .type = NLA_NESTED },
1683 };
1684
1685 static int
1686 call_ad(struct net *net, struct sock *ctnl, struct sk_buff *skb,
1687         struct ip_set *set, struct nlattr *tb[], enum ipset_adt adt,
1688         u32 flags, bool use_lineno)
1689 {
1690         int ret;
1691         u32 lineno = 0;
1692         bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
1693
1694         do {
1695                 if (retried) {
1696                         __ip_set_get(set);
1697                         nfnl_unlock(NFNL_SUBSYS_IPSET);
1698                         cond_resched();
1699                         nfnl_lock(NFNL_SUBSYS_IPSET);
1700                         __ip_set_put(set);
1701                 }
1702
1703                 ip_set_lock(set);
1704                 ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1705                 ip_set_unlock(set);
1706                 retried = true;
1707         } while (ret == -ERANGE ||
1708                  (ret == -EAGAIN &&
1709                   set->variant->resize &&
1710                   (ret = set->variant->resize(set, retried)) == 0));
1711
1712         if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1713                 return 0;
1714         if (lineno && use_lineno) {
1715                 /* Error in restore/batch mode: send back lineno */
1716                 struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1717                 struct sk_buff *skb2;
1718                 struct nlmsgerr *errmsg;
1719                 size_t payload = min(SIZE_MAX,
1720                                      sizeof(*errmsg) + nlmsg_len(nlh));
1721                 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
1722                 struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
1723                 struct nlattr *cmdattr;
1724                 u32 *errline;
1725
1726                 skb2 = nlmsg_new(payload, GFP_KERNEL);
1727                 if (!skb2)
1728                         return -ENOMEM;
1729                 rep = nlmsg_put(skb2, NETLINK_CB(skb).portid,
1730                                 nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1731                 errmsg = nlmsg_data(rep);
1732                 errmsg->error = ret;
1733                 unsafe_memcpy(&errmsg->msg, nlh, nlh->nlmsg_len,
1734                               /* Bounds checked by the skb layer. */);
1735
1736                 cmdattr = (void *)&errmsg->msg + min_len;
1737
1738                 ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, cmdattr,
1739                                 nlh->nlmsg_len - min_len, ip_set_adt_policy,
1740                                 NULL);
1741
1742                 if (ret) {
1743                         nlmsg_free(skb2);
1744                         return ret;
1745                 }
1746                 errline = nla_data(cda[IPSET_ATTR_LINENO]);
1747
1748                 *errline = lineno;
1749
1750                 nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
1751                 /* Signal netlink not to send its ACK/errmsg.  */
1752                 return -EINTR;
1753         }
1754
1755         return ret;
1756 }
1757
1758 static int ip_set_ad(struct net *net, struct sock *ctnl,
1759                      struct sk_buff *skb,
1760                      enum ipset_adt adt,
1761                      const struct nlmsghdr *nlh,
1762                      const struct nlattr * const attr[],
1763                      struct netlink_ext_ack *extack)
1764 {
1765         struct ip_set_net *inst = ip_set_pernet(net);
1766         struct ip_set *set;
1767         struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1768         const struct nlattr *nla;
1769         u32 flags = flag_exist(nlh);
1770         bool use_lineno;
1771         int ret = 0;
1772
1773         if (unlikely(protocol_min_failed(attr) ||
1774                      !attr[IPSET_ATTR_SETNAME] ||
1775                      !((attr[IPSET_ATTR_DATA] != NULL) ^
1776                        (attr[IPSET_ATTR_ADT] != NULL)) ||
1777                      (attr[IPSET_ATTR_DATA] &&
1778                       !flag_nested(attr[IPSET_ATTR_DATA])) ||
1779                      (attr[IPSET_ATTR_ADT] &&
1780                       (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1781                        !attr[IPSET_ATTR_LINENO]))))
1782                 return -IPSET_ERR_PROTOCOL;
1783
1784         set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1785         if (!set)
1786                 return -ENOENT;
1787
1788         use_lineno = !!attr[IPSET_ATTR_LINENO];
1789         if (attr[IPSET_ATTR_DATA]) {
1790                 if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1791                                      attr[IPSET_ATTR_DATA],
1792                                      set->type->adt_policy, NULL))
1793                         return -IPSET_ERR_PROTOCOL;
1794                 ret = call_ad(net, ctnl, skb, set, tb, adt, flags,
1795                               use_lineno);
1796         } else {
1797                 int nla_rem;
1798
1799                 nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1800                         if (nla_type(nla) != IPSET_ATTR_DATA ||
1801                             !flag_nested(nla) ||
1802                             nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1803                                              set->type->adt_policy, NULL))
1804                                 return -IPSET_ERR_PROTOCOL;
1805                         ret = call_ad(net, ctnl, skb, set, tb, adt,
1806                                       flags, use_lineno);
1807                         if (ret < 0)
1808                                 return ret;
1809                 }
1810         }
1811         return ret;
1812 }
1813
1814 static int ip_set_uadd(struct sk_buff *skb, const struct nfnl_info *info,
1815                        const struct nlattr * const attr[])
1816 {
1817         return ip_set_ad(info->net, info->sk, skb,
1818                          IPSET_ADD, info->nlh, attr, info->extack);
1819 }
1820
1821 static int ip_set_udel(struct sk_buff *skb, const struct nfnl_info *info,
1822                        const struct nlattr * const attr[])
1823 {
1824         return ip_set_ad(info->net, info->sk, skb,
1825                          IPSET_DEL, info->nlh, attr, info->extack);
1826 }
1827
1828 static int ip_set_utest(struct sk_buff *skb, const struct nfnl_info *info,
1829                         const struct nlattr * const attr[])
1830 {
1831         struct ip_set_net *inst = ip_set_pernet(info->net);
1832         struct ip_set *set;
1833         struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
1834         int ret = 0;
1835         u32 lineno;
1836
1837         if (unlikely(protocol_min_failed(attr) ||
1838                      !attr[IPSET_ATTR_SETNAME] ||
1839                      !attr[IPSET_ATTR_DATA] ||
1840                      !flag_nested(attr[IPSET_ATTR_DATA])))
1841                 return -IPSET_ERR_PROTOCOL;
1842
1843         set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1844         if (!set)
1845                 return -ENOENT;
1846
1847         if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1848                              set->type->adt_policy, NULL))
1849                 return -IPSET_ERR_PROTOCOL;
1850
1851         rcu_read_lock_bh();
1852         ret = set->variant->uadt(set, tb, IPSET_TEST, &lineno, 0, 0);
1853         rcu_read_unlock_bh();
1854         /* Userspace can't trigger element to be re-added */
1855         if (ret == -EAGAIN)
1856                 ret = 1;
1857
1858         return ret > 0 ? 0 : -IPSET_ERR_EXIST;
1859 }
1860
1861 /* Get headed data of a set */
1862
1863 static int ip_set_header(struct sk_buff *skb, const struct nfnl_info *info,
1864                          const struct nlattr * const attr[])
1865 {
1866         struct ip_set_net *inst = ip_set_pernet(info->net);
1867         const struct ip_set *set;
1868         struct sk_buff *skb2;
1869         struct nlmsghdr *nlh2;
1870
1871         if (unlikely(protocol_min_failed(attr) ||
1872                      !attr[IPSET_ATTR_SETNAME]))
1873                 return -IPSET_ERR_PROTOCOL;
1874
1875         set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1876         if (!set)
1877                 return -ENOENT;
1878
1879         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1880         if (!skb2)
1881                 return -ENOMEM;
1882
1883         nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
1884                          IPSET_CMD_HEADER);
1885         if (!nlh2)
1886                 goto nlmsg_failure;
1887         if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
1888             nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
1889             nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
1890             nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
1891             nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
1892                 goto nla_put_failure;
1893         nlmsg_end(skb2, nlh2);
1894
1895         return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
1896
1897 nla_put_failure:
1898         nlmsg_cancel(skb2, nlh2);
1899 nlmsg_failure:
1900         kfree_skb(skb2);
1901         return -EMSGSIZE;
1902 }
1903
1904 /* Get type data */
1905
1906 static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1907         [IPSET_ATTR_PROTOCOL]   = { .type = NLA_U8 },
1908         [IPSET_ATTR_TYPENAME]   = { .type = NLA_NUL_STRING,
1909                                     .len = IPSET_MAXNAMELEN - 1 },
1910         [IPSET_ATTR_FAMILY]     = { .type = NLA_U8 },
1911 };
1912
1913 static int ip_set_type(struct sk_buff *skb, const struct nfnl_info *info,
1914                        const struct nlattr * const attr[])
1915 {
1916         struct sk_buff *skb2;
1917         struct nlmsghdr *nlh2;
1918         u8 family, min, max;
1919         const char *typename;
1920         int ret = 0;
1921
1922         if (unlikely(protocol_min_failed(attr) ||
1923                      !attr[IPSET_ATTR_TYPENAME] ||
1924                      !attr[IPSET_ATTR_FAMILY]))
1925                 return -IPSET_ERR_PROTOCOL;
1926
1927         family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1928         typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1929         ret = find_set_type_minmax(typename, family, &min, &max);
1930         if (ret)
1931                 return ret;
1932
1933         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1934         if (!skb2)
1935                 return -ENOMEM;
1936
1937         nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
1938                          IPSET_CMD_TYPE);
1939         if (!nlh2)
1940                 goto nlmsg_failure;
1941         if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
1942             nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
1943             nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
1944             nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
1945             nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
1946                 goto nla_put_failure;
1947         nlmsg_end(skb2, nlh2);
1948
1949         pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
1950         return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
1951
1952 nla_put_failure:
1953         nlmsg_cancel(skb2, nlh2);
1954 nlmsg_failure:
1955         kfree_skb(skb2);
1956         return -EMSGSIZE;
1957 }
1958
1959 /* Get protocol version */
1960
1961 static const struct nla_policy
1962 ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1963         [IPSET_ATTR_PROTOCOL]   = { .type = NLA_U8 },
1964 };
1965
1966 static int ip_set_protocol(struct sk_buff *skb, const struct nfnl_info *info,
1967                            const struct nlattr * const attr[])
1968 {
1969         struct sk_buff *skb2;
1970         struct nlmsghdr *nlh2;
1971
1972         if (unlikely(!attr[IPSET_ATTR_PROTOCOL]))
1973                 return -IPSET_ERR_PROTOCOL;
1974
1975         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1976         if (!skb2)
1977                 return -ENOMEM;
1978
1979         nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
1980                          IPSET_CMD_PROTOCOL);
1981         if (!nlh2)
1982                 goto nlmsg_failure;
1983         if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
1984                 goto nla_put_failure;
1985         if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL_MIN, IPSET_PROTOCOL_MIN))
1986                 goto nla_put_failure;
1987         nlmsg_end(skb2, nlh2);
1988
1989         return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
1990
1991 nla_put_failure:
1992         nlmsg_cancel(skb2, nlh2);
1993 nlmsg_failure:
1994         kfree_skb(skb2);
1995         return -EMSGSIZE;
1996 }
1997
1998 /* Get set by name or index, from userspace */
1999
2000 static int ip_set_byname(struct sk_buff *skb, const struct nfnl_info *info,
2001                          const struct nlattr * const attr[])
2002 {
2003         struct ip_set_net *inst = ip_set_pernet(info->net);
2004         struct sk_buff *skb2;
2005         struct nlmsghdr *nlh2;
2006         ip_set_id_t id = IPSET_INVALID_ID;
2007         const struct ip_set *set;
2008
2009         if (unlikely(protocol_failed(attr) ||
2010                      !attr[IPSET_ATTR_SETNAME]))
2011                 return -IPSET_ERR_PROTOCOL;
2012
2013         set = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]), &id);
2014         if (id == IPSET_INVALID_ID)
2015                 return -ENOENT;
2016
2017         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2018         if (!skb2)
2019                 return -ENOMEM;
2020
2021         nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
2022                          IPSET_CMD_GET_BYNAME);
2023         if (!nlh2)
2024                 goto nlmsg_failure;
2025         if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
2026             nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
2027             nla_put_net16(skb2, IPSET_ATTR_INDEX, htons(id)))
2028                 goto nla_put_failure;
2029         nlmsg_end(skb2, nlh2);
2030
2031         return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
2032
2033 nla_put_failure:
2034         nlmsg_cancel(skb2, nlh2);
2035 nlmsg_failure:
2036         kfree_skb(skb2);
2037         return -EMSGSIZE;
2038 }
2039
2040 static const struct nla_policy ip_set_index_policy[IPSET_ATTR_CMD_MAX + 1] = {
2041         [IPSET_ATTR_PROTOCOL]   = { .type = NLA_U8 },
2042         [IPSET_ATTR_INDEX]      = { .type = NLA_U16 },
2043 };
2044
2045 static int ip_set_byindex(struct sk_buff *skb, const struct nfnl_info *info,
2046                           const struct nlattr * const attr[])
2047 {
2048         struct ip_set_net *inst = ip_set_pernet(info->net);
2049         struct sk_buff *skb2;
2050         struct nlmsghdr *nlh2;
2051         ip_set_id_t id = IPSET_INVALID_ID;
2052         const struct ip_set *set;
2053
2054         if (unlikely(protocol_failed(attr) ||
2055                      !attr[IPSET_ATTR_INDEX]))
2056                 return -IPSET_ERR_PROTOCOL;
2057
2058         id = ip_set_get_h16(attr[IPSET_ATTR_INDEX]);
2059         if (id >= inst->ip_set_max)
2060                 return -ENOENT;
2061         set = ip_set(inst, id);
2062         if (set == NULL)
2063                 return -ENOENT;
2064
2065         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2066         if (!skb2)
2067                 return -ENOMEM;
2068
2069         nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, info->nlh->nlmsg_seq, 0,
2070                          IPSET_CMD_GET_BYINDEX);
2071         if (!nlh2)
2072                 goto nlmsg_failure;
2073         if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
2074             nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name))
2075                 goto nla_put_failure;
2076         nlmsg_end(skb2, nlh2);
2077
2078         return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
2079
2080 nla_put_failure:
2081         nlmsg_cancel(skb2, nlh2);
2082 nlmsg_failure:
2083         kfree_skb(skb2);
2084         return -EMSGSIZE;
2085 }
2086
2087 static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
2088         [IPSET_CMD_NONE]        = {
2089                 .call           = ip_set_none,
2090                 .type           = NFNL_CB_MUTEX,
2091                 .attr_count     = IPSET_ATTR_CMD_MAX,
2092         },
2093         [IPSET_CMD_CREATE]      = {
2094                 .call           = ip_set_create,
2095                 .type           = NFNL_CB_MUTEX,
2096                 .attr_count     = IPSET_ATTR_CMD_MAX,
2097                 .policy         = ip_set_create_policy,
2098         },
2099         [IPSET_CMD_DESTROY]     = {
2100                 .call           = ip_set_destroy,
2101                 .type           = NFNL_CB_MUTEX,
2102                 .attr_count     = IPSET_ATTR_CMD_MAX,
2103                 .policy         = ip_set_setname_policy,
2104         },
2105         [IPSET_CMD_FLUSH]       = {
2106                 .call           = ip_set_flush,
2107                 .type           = NFNL_CB_MUTEX,
2108                 .attr_count     = IPSET_ATTR_CMD_MAX,
2109                 .policy         = ip_set_setname_policy,
2110         },
2111         [IPSET_CMD_RENAME]      = {
2112                 .call           = ip_set_rename,
2113                 .type           = NFNL_CB_MUTEX,
2114                 .attr_count     = IPSET_ATTR_CMD_MAX,
2115                 .policy         = ip_set_setname2_policy,
2116         },
2117         [IPSET_CMD_SWAP]        = {
2118                 .call           = ip_set_swap,
2119                 .type           = NFNL_CB_MUTEX,
2120                 .attr_count     = IPSET_ATTR_CMD_MAX,
2121                 .policy         = ip_set_setname2_policy,
2122         },
2123         [IPSET_CMD_LIST]        = {
2124                 .call           = ip_set_dump,
2125                 .type           = NFNL_CB_MUTEX,
2126                 .attr_count     = IPSET_ATTR_CMD_MAX,
2127                 .policy         = ip_set_dump_policy,
2128         },
2129         [IPSET_CMD_SAVE]        = {
2130                 .call           = ip_set_dump,
2131                 .type           = NFNL_CB_MUTEX,
2132                 .attr_count     = IPSET_ATTR_CMD_MAX,
2133                 .policy         = ip_set_setname_policy,
2134         },
2135         [IPSET_CMD_ADD] = {
2136                 .call           = ip_set_uadd,
2137                 .type           = NFNL_CB_MUTEX,
2138                 .attr_count     = IPSET_ATTR_CMD_MAX,
2139                 .policy         = ip_set_adt_policy,
2140         },
2141         [IPSET_CMD_DEL] = {
2142                 .call           = ip_set_udel,
2143                 .type           = NFNL_CB_MUTEX,
2144                 .attr_count     = IPSET_ATTR_CMD_MAX,
2145                 .policy         = ip_set_adt_policy,
2146         },
2147         [IPSET_CMD_TEST]        = {
2148                 .call           = ip_set_utest,
2149                 .type           = NFNL_CB_MUTEX,
2150                 .attr_count     = IPSET_ATTR_CMD_MAX,
2151                 .policy         = ip_set_adt_policy,
2152         },
2153         [IPSET_CMD_HEADER]      = {
2154                 .call           = ip_set_header,
2155                 .type           = NFNL_CB_MUTEX,
2156                 .attr_count     = IPSET_ATTR_CMD_MAX,
2157                 .policy         = ip_set_setname_policy,
2158         },
2159         [IPSET_CMD_TYPE]        = {
2160                 .call           = ip_set_type,
2161                 .type           = NFNL_CB_MUTEX,
2162                 .attr_count     = IPSET_ATTR_CMD_MAX,
2163                 .policy         = ip_set_type_policy,
2164         },
2165         [IPSET_CMD_PROTOCOL]    = {
2166                 .call           = ip_set_protocol,
2167                 .type           = NFNL_CB_MUTEX,
2168                 .attr_count     = IPSET_ATTR_CMD_MAX,
2169                 .policy         = ip_set_protocol_policy,
2170         },
2171         [IPSET_CMD_GET_BYNAME]  = {
2172                 .call           = ip_set_byname,
2173                 .type           = NFNL_CB_MUTEX,
2174                 .attr_count     = IPSET_ATTR_CMD_MAX,
2175                 .policy         = ip_set_setname_policy,
2176         },
2177         [IPSET_CMD_GET_BYINDEX] = {
2178                 .call           = ip_set_byindex,
2179                 .type           = NFNL_CB_MUTEX,
2180                 .attr_count     = IPSET_ATTR_CMD_MAX,
2181                 .policy         = ip_set_index_policy,
2182         },
2183 };
2184
2185 static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
2186         .name           = "ip_set",
2187         .subsys_id      = NFNL_SUBSYS_IPSET,
2188         .cb_count       = IPSET_MSG_MAX,
2189         .cb             = ip_set_netlink_subsys_cb,
2190 };
2191
2192 /* Interface to iptables/ip6tables */
2193
2194 static int
2195 ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
2196 {
2197         unsigned int *op;
2198         void *data;
2199         int copylen = *len, ret = 0;
2200         struct net *net = sock_net(sk);
2201         struct ip_set_net *inst = ip_set_pernet(net);
2202
2203         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2204                 return -EPERM;
2205         if (optval != SO_IP_SET)
2206                 return -EBADF;
2207         if (*len < sizeof(unsigned int))
2208                 return -EINVAL;
2209
2210         data = vmalloc(*len);
2211         if (!data)
2212                 return -ENOMEM;
2213         if (copy_from_user(data, user, *len) != 0) {
2214                 ret = -EFAULT;
2215                 goto done;
2216         }
2217         op = data;
2218
2219         if (*op < IP_SET_OP_VERSION) {
2220                 /* Check the version at the beginning of operations */
2221                 struct ip_set_req_version *req_version = data;
2222
2223                 if (*len < sizeof(struct ip_set_req_version)) {
2224                         ret = -EINVAL;
2225                         goto done;
2226                 }
2227
2228                 if (req_version->version < IPSET_PROTOCOL_MIN) {
2229                         ret = -EPROTO;
2230                         goto done;
2231                 }
2232         }
2233
2234         switch (*op) {
2235         case IP_SET_OP_VERSION: {
2236                 struct ip_set_req_version *req_version = data;
2237
2238                 if (*len != sizeof(struct ip_set_req_version)) {
2239                         ret = -EINVAL;
2240                         goto done;
2241                 }
2242
2243                 req_version->version = IPSET_PROTOCOL;
2244                 if (copy_to_user(user, req_version,
2245                                  sizeof(struct ip_set_req_version)))
2246                         ret = -EFAULT;
2247                 goto done;
2248         }
2249         case IP_SET_OP_GET_BYNAME: {
2250                 struct ip_set_req_get_set *req_get = data;
2251                 ip_set_id_t id;
2252
2253                 if (*len != sizeof(struct ip_set_req_get_set)) {
2254                         ret = -EINVAL;
2255                         goto done;
2256                 }
2257                 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2258                 nfnl_lock(NFNL_SUBSYS_IPSET);
2259                 find_set_and_id(inst, req_get->set.name, &id);
2260                 req_get->set.index = id;
2261                 nfnl_unlock(NFNL_SUBSYS_IPSET);
2262                 goto copy;
2263         }
2264         case IP_SET_OP_GET_FNAME: {
2265                 struct ip_set_req_get_set_family *req_get = data;
2266                 ip_set_id_t id;
2267
2268                 if (*len != sizeof(struct ip_set_req_get_set_family)) {
2269                         ret = -EINVAL;
2270                         goto done;
2271                 }
2272                 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
2273                 nfnl_lock(NFNL_SUBSYS_IPSET);
2274                 find_set_and_id(inst, req_get->set.name, &id);
2275                 req_get->set.index = id;
2276                 if (id != IPSET_INVALID_ID)
2277                         req_get->family = ip_set(inst, id)->family;
2278                 nfnl_unlock(NFNL_SUBSYS_IPSET);
2279                 goto copy;
2280         }
2281         case IP_SET_OP_GET_BYINDEX: {
2282                 struct ip_set_req_get_set *req_get = data;
2283                 struct ip_set *set;
2284
2285                 if (*len != sizeof(struct ip_set_req_get_set) ||
2286                     req_get->set.index >= inst->ip_set_max) {
2287                         ret = -EINVAL;
2288                         goto done;
2289                 }
2290                 nfnl_lock(NFNL_SUBSYS_IPSET);
2291                 set = ip_set(inst, req_get->set.index);
2292                 ret = strscpy(req_get->set.name, set ? set->name : "",
2293                               IPSET_MAXNAMELEN);
2294                 nfnl_unlock(NFNL_SUBSYS_IPSET);
2295                 if (ret < 0)
2296                         goto done;
2297                 goto copy;
2298         }
2299         default:
2300                 ret = -EBADMSG;
2301                 goto done;
2302         }       /* end of switch(op) */
2303
2304 copy:
2305         if (copy_to_user(user, data, copylen))
2306                 ret = -EFAULT;
2307
2308 done:
2309         vfree(data);
2310         if (ret > 0)
2311                 ret = 0;
2312         return ret;
2313 }
2314
2315 static struct nf_sockopt_ops so_set __read_mostly = {
2316         .pf             = PF_INET,
2317         .get_optmin     = SO_IP_SET,
2318         .get_optmax     = SO_IP_SET + 1,
2319         .get            = ip_set_sockfn_get,
2320         .owner          = THIS_MODULE,
2321 };
2322
2323 static int __net_init
2324 ip_set_net_init(struct net *net)
2325 {
2326         struct ip_set_net *inst = ip_set_pernet(net);
2327         struct ip_set **list;
2328
2329         inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
2330         if (inst->ip_set_max >= IPSET_INVALID_ID)
2331                 inst->ip_set_max = IPSET_INVALID_ID - 1;
2332
2333         list = kvcalloc(inst->ip_set_max, sizeof(struct ip_set *), GFP_KERNEL);
2334         if (!list)
2335                 return -ENOMEM;
2336         inst->is_deleted = false;
2337         inst->is_destroyed = false;
2338         rcu_assign_pointer(inst->ip_set_list, list);
2339         return 0;
2340 }
2341
2342 static void __net_exit
2343 ip_set_net_exit(struct net *net)
2344 {
2345         struct ip_set_net *inst = ip_set_pernet(net);
2346
2347         struct ip_set *set = NULL;
2348         ip_set_id_t i;
2349
2350         inst->is_deleted = true; /* flag for ip_set_nfnl_put */
2351
2352         nfnl_lock(NFNL_SUBSYS_IPSET);
2353         for (i = 0; i < inst->ip_set_max; i++) {
2354                 set = ip_set(inst, i);
2355                 if (set) {
2356                         ip_set(inst, i) = NULL;
2357                         ip_set_destroy_set(set);
2358                 }
2359         }
2360         nfnl_unlock(NFNL_SUBSYS_IPSET);
2361         kvfree(rcu_dereference_protected(inst->ip_set_list, 1));
2362 }
2363
2364 static struct pernet_operations ip_set_net_ops = {
2365         .init   = ip_set_net_init,
2366         .exit   = ip_set_net_exit,
2367         .id     = &ip_set_net_id,
2368         .size   = sizeof(struct ip_set_net),
2369 };
2370
2371 static int __init
2372 ip_set_init(void)
2373 {
2374         int ret = register_pernet_subsys(&ip_set_net_ops);
2375
2376         if (ret) {
2377                 pr_err("ip_set: cannot register pernet_subsys.\n");
2378                 return ret;
2379         }
2380
2381         ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
2382         if (ret != 0) {
2383                 pr_err("ip_set: cannot register with nfnetlink.\n");
2384                 unregister_pernet_subsys(&ip_set_net_ops);
2385                 return ret;
2386         }
2387
2388         ret = nf_register_sockopt(&so_set);
2389         if (ret != 0) {
2390                 pr_err("SO_SET registry failed: %d\n", ret);
2391                 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2392                 unregister_pernet_subsys(&ip_set_net_ops);
2393                 return ret;
2394         }
2395
2396         return 0;
2397 }
2398
2399 static void __exit
2400 ip_set_fini(void)
2401 {
2402         nf_unregister_sockopt(&so_set);
2403         nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2404
2405         unregister_pernet_subsys(&ip_set_net_ops);
2406         pr_debug("these are the famous last words\n");
2407 }
2408
2409 module_init(ip_set_init);
2410 module_exit(ip_set_fini);
2411
2412 MODULE_DESCRIPTION("ip_set: protocol " __stringify(IPSET_PROTOCOL));