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