ip_set_core.c revision f1e00b39797944bf25addaf543839feeb25fbdc5
1/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 *                         Patrick Schaaf <bof@bof.de>
3 * Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10/* Kernel module for IP set management */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/ip.h>
16#include <linux/skbuff.h>
17#include <linux/spinlock.h>
18#include <linux/netlink.h>
19#include <linux/rculist.h>
20#include <linux/version.h>
21#include <net/netlink.h>
22
23#include <linux/netfilter.h>
24#include <linux/netfilter/nfnetlink.h>
25#include <linux/netfilter/ipset/ip_set.h>
26
27static LIST_HEAD(ip_set_type_list);		/* all registered set types */
28static DEFINE_MUTEX(ip_set_type_mutex);		/* protects ip_set_type_list */
29static DEFINE_RWLOCK(ip_set_ref_lock);		/* protects the set refs */
30
31static struct ip_set **ip_set_list;		/* all individual sets */
32static ip_set_id_t ip_set_max = CONFIG_IP_SET_MAX; /* max number of sets */
33
34#define STREQ(a, b)	(strncmp(a, b, IPSET_MAXNAMELEN) == 0)
35
36static unsigned int max_sets;
37
38module_param(max_sets, int, 0600);
39MODULE_PARM_DESC(max_sets, "maximal number of sets");
40MODULE_LICENSE("GPL");
41MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
42MODULE_DESCRIPTION("core IP set support");
43MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
44
45/*
46 * The set types are implemented in modules and registered set types
47 * can be found in ip_set_type_list. Adding/deleting types is
48 * serialized by ip_set_type_mutex.
49 */
50
51static inline void
52ip_set_type_lock(void)
53{
54	mutex_lock(&ip_set_type_mutex);
55}
56
57static inline void
58ip_set_type_unlock(void)
59{
60	mutex_unlock(&ip_set_type_mutex);
61}
62
63/* Register and deregister settype */
64
65static struct ip_set_type *
66find_set_type(const char *name, u8 family, u8 revision)
67{
68	struct ip_set_type *type;
69
70	list_for_each_entry_rcu(type, &ip_set_type_list, list)
71		if (STREQ(type->name, name) &&
72		    (type->family == family || type->family == AF_UNSPEC) &&
73		    revision >= type->revision_min &&
74		    revision <= type->revision_max)
75			return type;
76	return NULL;
77}
78
79/* Unlock, try to load a set type module and lock again */
80static int
81try_to_load_type(const char *name)
82{
83	nfnl_unlock();
84	pr_debug("try to load ip_set_%s\n", name);
85	if (request_module("ip_set_%s", name) < 0) {
86		pr_warning("Can't find ip_set type %s\n", name);
87		nfnl_lock();
88		return -IPSET_ERR_FIND_TYPE;
89	}
90	nfnl_lock();
91	return -EAGAIN;
92}
93
94/* Find a set type and reference it */
95static int
96find_set_type_get(const char *name, u8 family, u8 revision,
97		  struct ip_set_type **found)
98{
99	struct ip_set_type *type;
100	int err;
101
102	rcu_read_lock();
103	*found = find_set_type(name, family, revision);
104	if (*found) {
105		err = !try_module_get((*found)->me) ? -EFAULT : 0;
106		goto unlock;
107	}
108	/* Make sure the type is loaded but we don't support the revision */
109	list_for_each_entry_rcu(type, &ip_set_type_list, list)
110		if (STREQ(type->name, name)) {
111			err = -IPSET_ERR_FIND_TYPE;
112			goto unlock;
113		}
114	rcu_read_unlock();
115
116	return try_to_load_type(name);
117
118unlock:
119	rcu_read_unlock();
120	return err;
121}
122
123/* Find a given set type by name and family.
124 * If we succeeded, the supported minimal and maximum revisions are
125 * filled out.
126 */
127static int
128find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max)
129{
130	struct ip_set_type *type;
131	bool found = false;
132
133	*min = 255; *max = 0;
134	rcu_read_lock();
135	list_for_each_entry_rcu(type, &ip_set_type_list, list)
136		if (STREQ(type->name, name) &&
137		    (type->family == family || type->family == AF_UNSPEC)) {
138			found = true;
139			if (type->revision_min < *min)
140				*min = type->revision_min;
141			if (type->revision_max > *max)
142				*max = type->revision_max;
143		}
144	rcu_read_unlock();
145	if (found)
146		return 0;
147
148	return try_to_load_type(name);
149}
150
151#define family_name(f)	((f) == AF_INET ? "inet" : \
152			 (f) == AF_INET6 ? "inet6" : "any")
153
154/* Register a set type structure. The type is identified by
155 * the unique triple of name, family and revision.
156 */
157int
158ip_set_type_register(struct ip_set_type *type)
159{
160	int ret = 0;
161
162	if (type->protocol != IPSET_PROTOCOL) {
163		pr_warning("ip_set type %s, family %s, revision %u:%u uses "
164			   "wrong protocol version %u (want %u)\n",
165			   type->name, family_name(type->family),
166			   type->revision_min, type->revision_max,
167			   type->protocol, IPSET_PROTOCOL);
168		return -EINVAL;
169	}
170
171	ip_set_type_lock();
172	if (find_set_type(type->name, type->family, type->revision_min)) {
173		/* Duplicate! */
174		pr_warning("ip_set type %s, family %s with revision min %u "
175			   "already registered!\n", type->name,
176			   family_name(type->family), type->revision_min);
177		ret = -EINVAL;
178		goto unlock;
179	}
180	list_add_rcu(&type->list, &ip_set_type_list);
181	pr_debug("type %s, family %s, revision %u:%u registered.\n",
182		 type->name, family_name(type->family),
183		 type->revision_min, type->revision_max);
184unlock:
185	ip_set_type_unlock();
186	return ret;
187}
188EXPORT_SYMBOL_GPL(ip_set_type_register);
189
190/* Unregister a set type. There's a small race with ip_set_create */
191void
192ip_set_type_unregister(struct ip_set_type *type)
193{
194	ip_set_type_lock();
195	if (!find_set_type(type->name, type->family, type->revision_min)) {
196		pr_warning("ip_set type %s, family %s with revision min %u "
197			   "not registered\n", type->name,
198			   family_name(type->family), type->revision_min);
199		goto unlock;
200	}
201	list_del_rcu(&type->list);
202	pr_debug("type %s, family %s with revision min %u unregistered.\n",
203		 type->name, family_name(type->family), type->revision_min);
204unlock:
205	ip_set_type_unlock();
206
207	synchronize_rcu();
208}
209EXPORT_SYMBOL_GPL(ip_set_type_unregister);
210
211/* Utility functions */
212void *
213ip_set_alloc(size_t size)
214{
215	void *members = NULL;
216
217	if (size < KMALLOC_MAX_SIZE)
218		members = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
219
220	if (members) {
221		pr_debug("%p: allocated with kmalloc\n", members);
222		return members;
223	}
224
225	members = vzalloc(size);
226	if (!members)
227		return NULL;
228	pr_debug("%p: allocated with vmalloc\n", members);
229
230	return members;
231}
232EXPORT_SYMBOL_GPL(ip_set_alloc);
233
234void
235ip_set_free(void *members)
236{
237	pr_debug("%p: free with %s\n", members,
238		 is_vmalloc_addr(members) ? "vfree" : "kfree");
239	if (is_vmalloc_addr(members))
240		vfree(members);
241	else
242		kfree(members);
243}
244EXPORT_SYMBOL_GPL(ip_set_free);
245
246static inline bool
247flag_nested(const struct nlattr *nla)
248{
249	return nla->nla_type & NLA_F_NESTED;
250}
251
252static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
253	[IPSET_ATTR_IPADDR_IPV4]	= { .type = NLA_U32 },
254	[IPSET_ATTR_IPADDR_IPV6]	= { .type = NLA_BINARY,
255					    .len = sizeof(struct in6_addr) },
256};
257
258int
259ip_set_get_ipaddr4(struct nlattr *nla,  __be32 *ipaddr)
260{
261	struct nlattr *tb[IPSET_ATTR_IPADDR_MAX+1];
262
263	if (unlikely(!flag_nested(nla)))
264		return -IPSET_ERR_PROTOCOL;
265	if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, ipaddr_policy))
266		return -IPSET_ERR_PROTOCOL;
267	if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
268		return -IPSET_ERR_PROTOCOL;
269
270	*ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
271	return 0;
272}
273EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
274
275int
276ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
277{
278	struct nlattr *tb[IPSET_ATTR_IPADDR_MAX+1];
279
280	if (unlikely(!flag_nested(nla)))
281		return -IPSET_ERR_PROTOCOL;
282
283	if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla, ipaddr_policy))
284		return -IPSET_ERR_PROTOCOL;
285	if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
286		return -IPSET_ERR_PROTOCOL;
287
288	memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
289		sizeof(struct in6_addr));
290	return 0;
291}
292EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
293
294/*
295 * Creating/destroying/renaming/swapping affect the existence and
296 * the properties of a set. All of these can be executed from userspace
297 * only and serialized by the nfnl mutex indirectly from nfnetlink.
298 *
299 * Sets are identified by their index in ip_set_list and the index
300 * is used by the external references (set/SET netfilter modules).
301 *
302 * The set behind an index may change by swapping only, from userspace.
303 */
304
305static inline void
306__ip_set_get(ip_set_id_t index)
307{
308	write_lock_bh(&ip_set_ref_lock);
309	ip_set_list[index]->ref++;
310	write_unlock_bh(&ip_set_ref_lock);
311}
312
313static inline void
314__ip_set_put(ip_set_id_t index)
315{
316	write_lock_bh(&ip_set_ref_lock);
317	BUG_ON(ip_set_list[index]->ref == 0);
318	ip_set_list[index]->ref--;
319	write_unlock_bh(&ip_set_ref_lock);
320}
321
322/*
323 * Add, del and test set entries from kernel.
324 *
325 * The set behind the index must exist and must be referenced
326 * so it can't be destroyed (or changed) under our foot.
327 */
328
329int
330ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
331	    const struct ip_set_adt_opt *opt)
332{
333	struct ip_set *set = ip_set_list[index];
334	int ret = 0;
335
336	BUG_ON(set == NULL);
337	pr_debug("set %s, index %u\n", set->name, index);
338
339	if (opt->dim < set->type->dimension ||
340	    !(opt->family == set->family || set->family == AF_UNSPEC))
341		return 0;
342
343	read_lock_bh(&set->lock);
344	ret = set->variant->kadt(set, skb, IPSET_TEST, opt);
345	read_unlock_bh(&set->lock);
346
347	if (ret == -EAGAIN) {
348		/* Type requests element to be completed */
349		pr_debug("element must be competed, ADD is triggered\n");
350		write_lock_bh(&set->lock);
351		set->variant->kadt(set, skb, IPSET_ADD, opt);
352		write_unlock_bh(&set->lock);
353		ret = 1;
354	}
355
356	/* Convert error codes to nomatch */
357	return (ret < 0 ? 0 : ret);
358}
359EXPORT_SYMBOL_GPL(ip_set_test);
360
361int
362ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
363	   const struct ip_set_adt_opt *opt)
364{
365	struct ip_set *set = ip_set_list[index];
366	int ret;
367
368	BUG_ON(set == NULL);
369	pr_debug("set %s, index %u\n", set->name, index);
370
371	if (opt->dim < set->type->dimension ||
372	    !(opt->family == set->family || set->family == AF_UNSPEC))
373		return 0;
374
375	write_lock_bh(&set->lock);
376	ret = set->variant->kadt(set, skb, IPSET_ADD, opt);
377	write_unlock_bh(&set->lock);
378
379	return ret;
380}
381EXPORT_SYMBOL_GPL(ip_set_add);
382
383int
384ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
385	   const struct ip_set_adt_opt *opt)
386{
387	struct ip_set *set = ip_set_list[index];
388	int ret = 0;
389
390	BUG_ON(set == NULL);
391	pr_debug("set %s, index %u\n", set->name, index);
392
393	if (opt->dim < set->type->dimension ||
394	    !(opt->family == set->family || set->family == AF_UNSPEC))
395		return 0;
396
397	write_lock_bh(&set->lock);
398	ret = set->variant->kadt(set, skb, IPSET_DEL, opt);
399	write_unlock_bh(&set->lock);
400
401	return ret;
402}
403EXPORT_SYMBOL_GPL(ip_set_del);
404
405/*
406 * Find set by name, reference it once. The reference makes sure the
407 * thing pointed to, does not go away under our feet.
408 *
409 */
410ip_set_id_t
411ip_set_get_byname(const char *name, struct ip_set **set)
412{
413	ip_set_id_t i, index = IPSET_INVALID_ID;
414	struct ip_set *s;
415
416	for (i = 0; i < ip_set_max; i++) {
417		s = ip_set_list[i];
418		if (s != NULL && STREQ(s->name, name)) {
419			__ip_set_get(i);
420			index = i;
421			*set = s;
422		}
423	}
424
425	return index;
426}
427EXPORT_SYMBOL_GPL(ip_set_get_byname);
428
429/*
430 * If the given set pointer points to a valid set, decrement
431 * reference count by 1. The caller shall not assume the index
432 * to be valid, after calling this function.
433 *
434 */
435void
436ip_set_put_byindex(ip_set_id_t index)
437{
438	if (ip_set_list[index] != NULL)
439		__ip_set_put(index);
440}
441EXPORT_SYMBOL_GPL(ip_set_put_byindex);
442
443/*
444 * Get the name of a set behind a set index.
445 * We assume the set is referenced, so it does exist and
446 * can't be destroyed. The set cannot be renamed due to
447 * the referencing either.
448 *
449 */
450const char *
451ip_set_name_byindex(ip_set_id_t index)
452{
453	const struct ip_set *set = ip_set_list[index];
454
455	BUG_ON(set == NULL);
456	BUG_ON(set->ref == 0);
457
458	/* Referenced, so it's safe */
459	return set->name;
460}
461EXPORT_SYMBOL_GPL(ip_set_name_byindex);
462
463/*
464 * Routines to call by external subsystems, which do not
465 * call nfnl_lock for us.
466 */
467
468/*
469 * Find set by name, reference it once. The reference makes sure the
470 * thing pointed to, does not go away under our feet.
471 *
472 * The nfnl mutex is used in the function.
473 */
474ip_set_id_t
475ip_set_nfnl_get(const char *name)
476{
477	struct ip_set *s;
478	ip_set_id_t index;
479
480	nfnl_lock();
481	index = ip_set_get_byname(name, &s);
482	nfnl_unlock();
483
484	return index;
485}
486EXPORT_SYMBOL_GPL(ip_set_nfnl_get);
487
488/*
489 * Find set by index, reference it once. The reference makes sure the
490 * thing pointed to, does not go away under our feet.
491 *
492 * The nfnl mutex is used in the function.
493 */
494ip_set_id_t
495ip_set_nfnl_get_byindex(ip_set_id_t index)
496{
497	if (index > ip_set_max)
498		return IPSET_INVALID_ID;
499
500	nfnl_lock();
501	if (ip_set_list[index])
502		__ip_set_get(index);
503	else
504		index = IPSET_INVALID_ID;
505	nfnl_unlock();
506
507	return index;
508}
509EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
510
511/*
512 * If the given set pointer points to a valid set, decrement
513 * reference count by 1. The caller shall not assume the index
514 * to be valid, after calling this function.
515 *
516 * The nfnl mutex is used in the function.
517 */
518void
519ip_set_nfnl_put(ip_set_id_t index)
520{
521	nfnl_lock();
522	ip_set_put_byindex(index);
523	nfnl_unlock();
524}
525EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
526
527/*
528 * Communication protocol with userspace over netlink.
529 *
530 * The commands are serialized by the nfnl mutex.
531 */
532
533static inline bool
534protocol_failed(const struct nlattr * const tb[])
535{
536	return !tb[IPSET_ATTR_PROTOCOL] ||
537	       nla_get_u8(tb[IPSET_ATTR_PROTOCOL]) != IPSET_PROTOCOL;
538}
539
540static inline u32
541flag_exist(const struct nlmsghdr *nlh)
542{
543	return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
544}
545
546static struct nlmsghdr *
547start_msg(struct sk_buff *skb, u32 pid, u32 seq, unsigned int flags,
548	  enum ipset_cmd cmd)
549{
550	struct nlmsghdr *nlh;
551	struct nfgenmsg *nfmsg;
552
553	nlh = nlmsg_put(skb, pid, seq, cmd | (NFNL_SUBSYS_IPSET << 8),
554			sizeof(*nfmsg), flags);
555	if (nlh == NULL)
556		return NULL;
557
558	nfmsg = nlmsg_data(nlh);
559	nfmsg->nfgen_family = AF_INET;
560	nfmsg->version = NFNETLINK_V0;
561	nfmsg->res_id = 0;
562
563	return nlh;
564}
565
566/* Create a set */
567
568static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
569	[IPSET_ATTR_PROTOCOL]	= { .type = NLA_U8 },
570	[IPSET_ATTR_SETNAME]	= { .type = NLA_NUL_STRING,
571				    .len = IPSET_MAXNAMELEN - 1 },
572	[IPSET_ATTR_TYPENAME]	= { .type = NLA_NUL_STRING,
573				    .len = IPSET_MAXNAMELEN - 1},
574	[IPSET_ATTR_REVISION]	= { .type = NLA_U8 },
575	[IPSET_ATTR_FAMILY]	= { .type = NLA_U8 },
576	[IPSET_ATTR_DATA]	= { .type = NLA_NESTED },
577};
578
579static ip_set_id_t
580find_set_id(const char *name)
581{
582	ip_set_id_t i, index = IPSET_INVALID_ID;
583	const struct ip_set *set;
584
585	for (i = 0; index == IPSET_INVALID_ID && i < ip_set_max; i++) {
586		set = ip_set_list[i];
587		if (set != NULL && STREQ(set->name, name))
588			index = i;
589	}
590	return index;
591}
592
593static inline struct ip_set *
594find_set(const char *name)
595{
596	ip_set_id_t index = find_set_id(name);
597
598	return index == IPSET_INVALID_ID ? NULL : ip_set_list[index];
599}
600
601static int
602find_free_id(const char *name, ip_set_id_t *index, struct ip_set **set)
603{
604	ip_set_id_t i;
605
606	*index = IPSET_INVALID_ID;
607	for (i = 0;  i < ip_set_max; i++) {
608		if (ip_set_list[i] == NULL) {
609			if (*index == IPSET_INVALID_ID)
610				*index = i;
611		} else if (STREQ(name, ip_set_list[i]->name)) {
612			/* Name clash */
613			*set = ip_set_list[i];
614			return -EEXIST;
615		}
616	}
617	if (*index == IPSET_INVALID_ID)
618		/* No free slot remained */
619		return -IPSET_ERR_MAX_SETS;
620	return 0;
621}
622
623static int
624ip_set_create(struct sock *ctnl, struct sk_buff *skb,
625	      const struct nlmsghdr *nlh,
626	      const struct nlattr * const attr[])
627{
628	struct ip_set *set, *clash = NULL;
629	ip_set_id_t index = IPSET_INVALID_ID;
630	struct nlattr *tb[IPSET_ATTR_CREATE_MAX+1] = {};
631	const char *name, *typename;
632	u8 family, revision;
633	u32 flags = flag_exist(nlh);
634	int ret = 0;
635
636	if (unlikely(protocol_failed(attr) ||
637		     attr[IPSET_ATTR_SETNAME] == NULL ||
638		     attr[IPSET_ATTR_TYPENAME] == NULL ||
639		     attr[IPSET_ATTR_REVISION] == NULL ||
640		     attr[IPSET_ATTR_FAMILY] == NULL ||
641		     (attr[IPSET_ATTR_DATA] != NULL &&
642		      !flag_nested(attr[IPSET_ATTR_DATA]))))
643		return -IPSET_ERR_PROTOCOL;
644
645	name = nla_data(attr[IPSET_ATTR_SETNAME]);
646	typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
647	family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
648	revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
649	pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
650		 name, typename, family_name(family), revision);
651
652	/*
653	 * First, and without any locks, allocate and initialize
654	 * a normal base set structure.
655	 */
656	set = kzalloc(sizeof(struct ip_set), GFP_KERNEL);
657	if (!set)
658		return -ENOMEM;
659	rwlock_init(&set->lock);
660	strlcpy(set->name, name, IPSET_MAXNAMELEN);
661	set->family = family;
662	set->revision = revision;
663
664	/*
665	 * Next, check that we know the type, and take
666	 * a reference on the type, to make sure it stays available
667	 * while constructing our new set.
668	 *
669	 * After referencing the type, we try to create the type
670	 * specific part of the set without holding any locks.
671	 */
672	ret = find_set_type_get(typename, family, revision, &(set->type));
673	if (ret)
674		goto out;
675
676	/*
677	 * Without holding any locks, create private part.
678	 */
679	if (attr[IPSET_ATTR_DATA] &&
680	    nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
681			     set->type->create_policy)) {
682	    	ret = -IPSET_ERR_PROTOCOL;
683	    	goto put_out;
684	}
685
686	ret = set->type->create(set, tb, flags);
687	if (ret != 0)
688		goto put_out;
689
690	/* BTW, ret==0 here. */
691
692	/*
693	 * Here, we have a valid, constructed set and we are protected
694	 * by the nfnl mutex. Find the first free index in ip_set_list
695	 * and check clashing.
696	 */
697	if ((ret = find_free_id(set->name, &index, &clash)) != 0) {
698		/* If this is the same set and requested, ignore error */
699		if (ret == -EEXIST &&
700		    (flags & IPSET_FLAG_EXIST) &&
701		    STREQ(set->type->name, clash->type->name) &&
702		    set->type->family == clash->type->family &&
703		    set->type->revision_min == clash->type->revision_min &&
704		    set->type->revision_max == clash->type->revision_max &&
705		    set->variant->same_set(set, clash))
706			ret = 0;
707		goto cleanup;
708	}
709
710	/*
711	 * Finally! Add our shiny new set to the list, and be done.
712	 */
713	pr_debug("create: '%s' created with index %u!\n", set->name, index);
714	ip_set_list[index] = set;
715
716	return ret;
717
718cleanup:
719	set->variant->destroy(set);
720put_out:
721	module_put(set->type->me);
722out:
723	kfree(set);
724	return ret;
725}
726
727/* Destroy sets */
728
729static const struct nla_policy
730ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
731	[IPSET_ATTR_PROTOCOL]	= { .type = NLA_U8 },
732	[IPSET_ATTR_SETNAME]	= { .type = NLA_NUL_STRING,
733				    .len = IPSET_MAXNAMELEN - 1 },
734};
735
736static void
737ip_set_destroy_set(ip_set_id_t index)
738{
739	struct ip_set *set = ip_set_list[index];
740
741	pr_debug("set: %s\n",  set->name);
742	ip_set_list[index] = NULL;
743
744	/* Must call it without holding any lock */
745	set->variant->destroy(set);
746	module_put(set->type->me);
747	kfree(set);
748}
749
750static int
751ip_set_destroy(struct sock *ctnl, struct sk_buff *skb,
752	       const struct nlmsghdr *nlh,
753	       const struct nlattr * const attr[])
754{
755	ip_set_id_t i;
756	int ret = 0;
757
758	if (unlikely(protocol_failed(attr)))
759		return -IPSET_ERR_PROTOCOL;
760
761	/* Commands are serialized and references are
762	 * protected by the ip_set_ref_lock.
763	 * External systems (i.e. xt_set) must call
764	 * ip_set_put|get_nfnl_* functions, that way we
765	 * can safely check references here.
766	 *
767	 * list:set timer can only decrement the reference
768	 * counter, so if it's already zero, we can proceed
769	 * without holding the lock.
770	 */
771	read_lock_bh(&ip_set_ref_lock);
772	if (!attr[IPSET_ATTR_SETNAME]) {
773		for (i = 0; i < ip_set_max; i++) {
774			if (ip_set_list[i] != NULL && ip_set_list[i]->ref) {
775				ret = IPSET_ERR_BUSY;
776				goto out;
777			}
778		}
779		read_unlock_bh(&ip_set_ref_lock);
780		for (i = 0; i < ip_set_max; i++) {
781			if (ip_set_list[i] != NULL)
782				ip_set_destroy_set(i);
783		}
784	} else {
785		i = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
786		if (i == IPSET_INVALID_ID) {
787			ret = -ENOENT;
788			goto out;
789		} else if (ip_set_list[i]->ref) {
790			ret = -IPSET_ERR_BUSY;
791			goto out;
792		}
793		read_unlock_bh(&ip_set_ref_lock);
794
795		ip_set_destroy_set(i);
796	}
797	return 0;
798out:
799	read_unlock_bh(&ip_set_ref_lock);
800	return ret;
801}
802
803/* Flush sets */
804
805static void
806ip_set_flush_set(struct ip_set *set)
807{
808	pr_debug("set: %s\n",  set->name);
809
810	write_lock_bh(&set->lock);
811	set->variant->flush(set);
812	write_unlock_bh(&set->lock);
813}
814
815static int
816ip_set_flush(struct sock *ctnl, struct sk_buff *skb,
817	     const struct nlmsghdr *nlh,
818	     const struct nlattr * const attr[])
819{
820	ip_set_id_t i;
821
822	if (unlikely(protocol_failed(attr)))
823		return -IPSET_ERR_PROTOCOL;
824
825	if (!attr[IPSET_ATTR_SETNAME]) {
826		for (i = 0; i < ip_set_max; i++)
827			if (ip_set_list[i] != NULL)
828				ip_set_flush_set(ip_set_list[i]);
829	} else {
830		i = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
831		if (i == IPSET_INVALID_ID)
832			return -ENOENT;
833
834		ip_set_flush_set(ip_set_list[i]);
835	}
836
837	return 0;
838}
839
840/* Rename a set */
841
842static const struct nla_policy
843ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
844	[IPSET_ATTR_PROTOCOL]	= { .type = NLA_U8 },
845	[IPSET_ATTR_SETNAME]	= { .type = NLA_NUL_STRING,
846				    .len = IPSET_MAXNAMELEN - 1 },
847	[IPSET_ATTR_SETNAME2]	= { .type = NLA_NUL_STRING,
848				    .len = IPSET_MAXNAMELEN - 1 },
849};
850
851static int
852ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
853	      const struct nlmsghdr *nlh,
854	      const struct nlattr * const attr[])
855{
856	struct ip_set *set;
857	const char *name2;
858	ip_set_id_t i;
859	int ret = 0;
860
861	if (unlikely(protocol_failed(attr) ||
862		     attr[IPSET_ATTR_SETNAME] == NULL ||
863		     attr[IPSET_ATTR_SETNAME2] == NULL))
864		return -IPSET_ERR_PROTOCOL;
865
866	set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
867	if (set == NULL)
868		return -ENOENT;
869
870	read_lock_bh(&ip_set_ref_lock);
871	if (set->ref != 0) {
872		ret = -IPSET_ERR_REFERENCED;
873		goto out;
874	}
875
876	name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
877	for (i = 0; i < ip_set_max; i++) {
878		if (ip_set_list[i] != NULL &&
879		    STREQ(ip_set_list[i]->name, name2)) {
880			ret = -IPSET_ERR_EXIST_SETNAME2;
881			goto out;
882		}
883	}
884	strncpy(set->name, name2, IPSET_MAXNAMELEN);
885
886out:
887	read_unlock_bh(&ip_set_ref_lock);
888	return ret;
889}
890
891/* Swap two sets so that name/index points to the other.
892 * References and set names are also swapped.
893 *
894 * The commands are serialized by the nfnl mutex and references are
895 * protected by the ip_set_ref_lock. The kernel interfaces
896 * do not hold the mutex but the pointer settings are atomic
897 * so the ip_set_list always contains valid pointers to the sets.
898 */
899
900static int
901ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
902	    const struct nlmsghdr *nlh,
903	    const struct nlattr * const attr[])
904{
905	struct ip_set *from, *to;
906	ip_set_id_t from_id, to_id;
907	char from_name[IPSET_MAXNAMELEN];
908
909	if (unlikely(protocol_failed(attr) ||
910		     attr[IPSET_ATTR_SETNAME] == NULL ||
911		     attr[IPSET_ATTR_SETNAME2] == NULL))
912		return -IPSET_ERR_PROTOCOL;
913
914	from_id = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
915	if (from_id == IPSET_INVALID_ID)
916		return -ENOENT;
917
918	to_id = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME2]));
919	if (to_id == IPSET_INVALID_ID)
920		return -IPSET_ERR_EXIST_SETNAME2;
921
922	from = ip_set_list[from_id];
923	to = ip_set_list[to_id];
924
925	/* Features must not change.
926	 * Not an artificial restriction anymore, as we must prevent
927	 * possible loops created by swapping in setlist type of sets. */
928	if (!(from->type->features == to->type->features &&
929	      from->type->family == to->type->family))
930		return -IPSET_ERR_TYPE_MISMATCH;
931
932	strncpy(from_name, from->name, IPSET_MAXNAMELEN);
933	strncpy(from->name, to->name, IPSET_MAXNAMELEN);
934	strncpy(to->name, from_name, IPSET_MAXNAMELEN);
935
936	write_lock_bh(&ip_set_ref_lock);
937	swap(from->ref, to->ref);
938	ip_set_list[from_id] = to;
939	ip_set_list[to_id] = from;
940	write_unlock_bh(&ip_set_ref_lock);
941
942	return 0;
943}
944
945/* List/save set data */
946
947#define DUMP_INIT	0
948#define DUMP_ALL	1
949#define DUMP_ONE	2
950#define DUMP_LAST	3
951
952#define DUMP_TYPE(arg)		(((u32)(arg)) & 0x0000FFFF)
953#define DUMP_FLAGS(arg)		(((u32)(arg)) >> 16)
954
955static int
956ip_set_dump_done(struct netlink_callback *cb)
957{
958	if (cb->args[2]) {
959		pr_debug("release set %s\n", ip_set_list[cb->args[1]]->name);
960		ip_set_put_byindex((ip_set_id_t) cb->args[1]);
961	}
962	return 0;
963}
964
965static inline void
966dump_attrs(struct nlmsghdr *nlh)
967{
968	const struct nlattr *attr;
969	int rem;
970
971	pr_debug("dump nlmsg\n");
972	nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
973		pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
974	}
975}
976
977static int
978dump_init(struct netlink_callback *cb)
979{
980	struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
981	int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
982	struct nlattr *cda[IPSET_ATTR_CMD_MAX+1];
983	struct nlattr *attr = (void *)nlh + min_len;
984	u32 dump_type;
985	ip_set_id_t index;
986
987	/* Second pass, so parser can't fail */
988	nla_parse(cda, IPSET_ATTR_CMD_MAX,
989		  attr, nlh->nlmsg_len - min_len, ip_set_setname_policy);
990
991	/* cb->args[0] : dump single set/all sets
992	 *         [1] : set index
993	 *         [..]: type specific
994	 */
995
996	if (cda[IPSET_ATTR_SETNAME]) {
997		index = find_set_id(nla_data(cda[IPSET_ATTR_SETNAME]));
998		if (index == IPSET_INVALID_ID)
999			return -ENOENT;
1000
1001		dump_type = DUMP_ONE;
1002		cb->args[1] = index;
1003	} else
1004		dump_type = DUMP_ALL;
1005
1006	if (cda[IPSET_ATTR_FLAGS]) {
1007		u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1008		dump_type |= (f << 16);
1009	}
1010	cb->args[0] = dump_type;
1011
1012	return 0;
1013}
1014
1015static int
1016ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
1017{
1018	ip_set_id_t index = IPSET_INVALID_ID, max;
1019	struct ip_set *set = NULL;
1020	struct nlmsghdr *nlh = NULL;
1021	unsigned int flags = NETLINK_CB(cb->skb).pid ? NLM_F_MULTI : 0;
1022	u32 dump_type, dump_flags;
1023	int ret = 0;
1024
1025	if (!cb->args[0]) {
1026		ret = dump_init(cb);
1027		if (ret < 0) {
1028			nlh = nlmsg_hdr(cb->skb);
1029			/* We have to create and send the error message
1030			 * manually :-( */
1031			if (nlh->nlmsg_flags & NLM_F_ACK)
1032				netlink_ack(cb->skb, nlh, ret);
1033			return ret;
1034		}
1035	}
1036
1037	if (cb->args[1] >= ip_set_max)
1038		goto out;
1039
1040	dump_type = DUMP_TYPE(cb->args[0]);
1041	dump_flags = DUMP_FLAGS(cb->args[0]);
1042	max = dump_type == DUMP_ONE ? cb->args[1] + 1 : ip_set_max;
1043dump_last:
1044	pr_debug("args[0]: %u %u args[1]: %ld\n",
1045		 dump_type, dump_flags, cb->args[1]);
1046	for (; cb->args[1] < max; cb->args[1]++) {
1047		index = (ip_set_id_t) cb->args[1];
1048		set = ip_set_list[index];
1049		if (set == NULL) {
1050			if (dump_type == DUMP_ONE) {
1051				ret = -ENOENT;
1052				goto out;
1053			}
1054			continue;
1055		}
1056		/* When dumping all sets, we must dump "sorted"
1057		 * so that lists (unions of sets) are dumped last.
1058		 */
1059		if (dump_type != DUMP_ONE &&
1060		    ((dump_type == DUMP_ALL) ==
1061		     !!(set->type->features & IPSET_DUMP_LAST)))
1062			continue;
1063		pr_debug("List set: %s\n", set->name);
1064		if (!cb->args[2]) {
1065			/* Start listing: make sure set won't be destroyed */
1066			pr_debug("reference set\n");
1067			__ip_set_get(index);
1068		}
1069		nlh = start_msg(skb, NETLINK_CB(cb->skb).pid,
1070				cb->nlh->nlmsg_seq, flags,
1071				IPSET_CMD_LIST);
1072		if (!nlh) {
1073			ret = -EMSGSIZE;
1074			goto release_refcount;
1075		}
1076		NLA_PUT_U8(skb, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL);
1077		NLA_PUT_STRING(skb, IPSET_ATTR_SETNAME, set->name);
1078		if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1079			goto next_set;
1080		switch (cb->args[2]) {
1081		case 0:
1082			/* Core header data */
1083			NLA_PUT_STRING(skb, IPSET_ATTR_TYPENAME,
1084				       set->type->name);
1085			NLA_PUT_U8(skb, IPSET_ATTR_FAMILY,
1086				   set->family);
1087			NLA_PUT_U8(skb, IPSET_ATTR_REVISION,
1088				   set->revision);
1089			ret = set->variant->head(set, skb);
1090			if (ret < 0)
1091				goto release_refcount;
1092			if (dump_flags & IPSET_FLAG_LIST_HEADER)
1093				goto next_set;
1094			/* Fall through and add elements */
1095		default:
1096			read_lock_bh(&set->lock);
1097			ret = set->variant->list(set, skb, cb);
1098			read_unlock_bh(&set->lock);
1099			if (!cb->args[2])
1100				/* Set is done, proceed with next one */
1101				goto next_set;
1102			goto release_refcount;
1103		}
1104	}
1105	/* If we dump all sets, continue with dumping last ones */
1106	if (dump_type == DUMP_ALL) {
1107		dump_type = DUMP_LAST;
1108		cb->args[0] = dump_type | (dump_flags << 16);
1109		cb->args[1] = 0;
1110		goto dump_last;
1111	}
1112	goto out;
1113
1114nla_put_failure:
1115	ret = -EFAULT;
1116next_set:
1117	if (dump_type == DUMP_ONE)
1118		cb->args[1] = IPSET_INVALID_ID;
1119	else
1120		cb->args[1]++;
1121release_refcount:
1122	/* If there was an error or set is done, release set */
1123	if (ret || !cb->args[2]) {
1124		pr_debug("release set %s\n", ip_set_list[index]->name);
1125		ip_set_put_byindex(index);
1126	}
1127out:
1128	if (nlh) {
1129		nlmsg_end(skb, nlh);
1130		pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
1131		dump_attrs(nlh);
1132	}
1133
1134	return ret < 0 ? ret : skb->len;
1135}
1136
1137static int
1138ip_set_dump(struct sock *ctnl, struct sk_buff *skb,
1139	    const struct nlmsghdr *nlh,
1140	    const struct nlattr * const attr[])
1141{
1142	if (unlikely(protocol_failed(attr)))
1143		return -IPSET_ERR_PROTOCOL;
1144
1145	return netlink_dump_start(ctnl, skb, nlh,
1146				  ip_set_dump_start,
1147				  ip_set_dump_done, 0);
1148}
1149
1150/* Add, del and test */
1151
1152static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
1153	[IPSET_ATTR_PROTOCOL]	= { .type = NLA_U8 },
1154	[IPSET_ATTR_SETNAME]	= { .type = NLA_NUL_STRING,
1155				    .len = IPSET_MAXNAMELEN - 1 },
1156	[IPSET_ATTR_LINENO]	= { .type = NLA_U32 },
1157	[IPSET_ATTR_DATA]	= { .type = NLA_NESTED },
1158	[IPSET_ATTR_ADT]	= { .type = NLA_NESTED },
1159};
1160
1161static int
1162call_ad(struct sock *ctnl, struct sk_buff *skb, struct ip_set *set,
1163	struct nlattr *tb[], enum ipset_adt adt,
1164	u32 flags, bool use_lineno)
1165{
1166	int ret;
1167	u32 lineno = 0;
1168	bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
1169
1170	do {
1171		write_lock_bh(&set->lock);
1172		ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1173		write_unlock_bh(&set->lock);
1174		retried = true;
1175	} while (ret == -EAGAIN &&
1176		 set->variant->resize &&
1177		 (ret = set->variant->resize(set, retried)) == 0);
1178
1179	if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
1180		return 0;
1181	if (lineno && use_lineno) {
1182		/* Error in restore/batch mode: send back lineno */
1183		struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
1184		struct sk_buff *skb2;
1185		struct nlmsgerr *errmsg;
1186		size_t payload = sizeof(*errmsg) + nlmsg_len(nlh);
1187		int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
1188		struct nlattr *cda[IPSET_ATTR_CMD_MAX+1];
1189		struct nlattr *cmdattr;
1190		u32 *errline;
1191
1192		skb2 = nlmsg_new(payload, GFP_KERNEL);
1193		if (skb2 == NULL)
1194			return -ENOMEM;
1195		rep = __nlmsg_put(skb2, NETLINK_CB(skb).pid,
1196				  nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
1197		errmsg = nlmsg_data(rep);
1198		errmsg->error = ret;
1199		memcpy(&errmsg->msg, nlh, nlh->nlmsg_len);
1200		cmdattr = (void *)&errmsg->msg + min_len;
1201
1202		nla_parse(cda, IPSET_ATTR_CMD_MAX,
1203			  cmdattr, nlh->nlmsg_len - min_len,
1204			  ip_set_adt_policy);
1205
1206		errline = nla_data(cda[IPSET_ATTR_LINENO]);
1207
1208		*errline = lineno;
1209
1210		netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1211		/* Signal netlink not to send its ACK/errmsg.  */
1212		return -EINTR;
1213	}
1214
1215	return ret;
1216}
1217
1218static int
1219ip_set_uadd(struct sock *ctnl, struct sk_buff *skb,
1220	    const struct nlmsghdr *nlh,
1221	    const struct nlattr * const attr[])
1222{
1223	struct ip_set *set;
1224	struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1225	const struct nlattr *nla;
1226	u32 flags = flag_exist(nlh);
1227	bool use_lineno;
1228	int ret = 0;
1229
1230	if (unlikely(protocol_failed(attr) ||
1231		     attr[IPSET_ATTR_SETNAME] == NULL ||
1232		     !((attr[IPSET_ATTR_DATA] != NULL) ^
1233		       (attr[IPSET_ATTR_ADT] != NULL)) ||
1234		     (attr[IPSET_ATTR_DATA] != NULL &&
1235		      !flag_nested(attr[IPSET_ATTR_DATA])) ||
1236		     (attr[IPSET_ATTR_ADT] != NULL &&
1237		      (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1238		       attr[IPSET_ATTR_LINENO] == NULL))))
1239		return -IPSET_ERR_PROTOCOL;
1240
1241	set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
1242	if (set == NULL)
1243		return -ENOENT;
1244
1245	use_lineno = !!attr[IPSET_ATTR_LINENO];
1246	if (attr[IPSET_ATTR_DATA]) {
1247		if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1248				     attr[IPSET_ATTR_DATA],
1249				     set->type->adt_policy))
1250			return -IPSET_ERR_PROTOCOL;
1251		ret = call_ad(ctnl, skb, set, tb, IPSET_ADD, flags,
1252			      use_lineno);
1253	} else {
1254		int nla_rem;
1255
1256		nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1257			memset(tb, 0, sizeof(tb));
1258			if (nla_type(nla) != IPSET_ATTR_DATA ||
1259			    !flag_nested(nla) ||
1260			    nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1261					     set->type->adt_policy))
1262				return -IPSET_ERR_PROTOCOL;
1263			ret = call_ad(ctnl, skb, set, tb, IPSET_ADD,
1264				      flags, use_lineno);
1265			if (ret < 0)
1266				return ret;
1267		}
1268	}
1269	return ret;
1270}
1271
1272static int
1273ip_set_udel(struct sock *ctnl, struct sk_buff *skb,
1274	    const struct nlmsghdr *nlh,
1275	    const struct nlattr * const attr[])
1276{
1277	struct ip_set *set;
1278	struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1279	const struct nlattr *nla;
1280	u32 flags = flag_exist(nlh);
1281	bool use_lineno;
1282	int ret = 0;
1283
1284	if (unlikely(protocol_failed(attr) ||
1285		     attr[IPSET_ATTR_SETNAME] == NULL ||
1286		     !((attr[IPSET_ATTR_DATA] != NULL) ^
1287		       (attr[IPSET_ATTR_ADT] != NULL)) ||
1288		     (attr[IPSET_ATTR_DATA] != NULL &&
1289		      !flag_nested(attr[IPSET_ATTR_DATA])) ||
1290		     (attr[IPSET_ATTR_ADT] != NULL &&
1291		      (!flag_nested(attr[IPSET_ATTR_ADT]) ||
1292		       attr[IPSET_ATTR_LINENO] == NULL))))
1293		return -IPSET_ERR_PROTOCOL;
1294
1295	set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
1296	if (set == NULL)
1297		return -ENOENT;
1298
1299	use_lineno = !!attr[IPSET_ATTR_LINENO];
1300	if (attr[IPSET_ATTR_DATA]) {
1301		if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
1302				     attr[IPSET_ATTR_DATA],
1303				     set->type->adt_policy))
1304			return -IPSET_ERR_PROTOCOL;
1305		ret = call_ad(ctnl, skb, set, tb, IPSET_DEL, flags,
1306			      use_lineno);
1307	} else {
1308		int nla_rem;
1309
1310		nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
1311			memset(tb, 0, sizeof(*tb));
1312			if (nla_type(nla) != IPSET_ATTR_DATA ||
1313			    !flag_nested(nla) ||
1314			    nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
1315					     set->type->adt_policy))
1316				return -IPSET_ERR_PROTOCOL;
1317			ret = call_ad(ctnl, skb, set, tb, IPSET_DEL,
1318				      flags, use_lineno);
1319			if (ret < 0)
1320				return ret;
1321		}
1322	}
1323	return ret;
1324}
1325
1326static int
1327ip_set_utest(struct sock *ctnl, struct sk_buff *skb,
1328	     const struct nlmsghdr *nlh,
1329	     const struct nlattr * const attr[])
1330{
1331	struct ip_set *set;
1332	struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1333	int ret = 0;
1334
1335	if (unlikely(protocol_failed(attr) ||
1336		     attr[IPSET_ATTR_SETNAME] == NULL ||
1337		     attr[IPSET_ATTR_DATA] == NULL ||
1338		     !flag_nested(attr[IPSET_ATTR_DATA])))
1339		return -IPSET_ERR_PROTOCOL;
1340
1341	set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
1342	if (set == NULL)
1343		return -ENOENT;
1344
1345	if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
1346			     set->type->adt_policy))
1347		return -IPSET_ERR_PROTOCOL;
1348
1349	read_lock_bh(&set->lock);
1350	ret = set->variant->uadt(set, tb, IPSET_TEST, NULL, 0, 0);
1351	read_unlock_bh(&set->lock);
1352	/* Userspace can't trigger element to be re-added */
1353	if (ret == -EAGAIN)
1354		ret = 1;
1355
1356	return ret < 0 ? ret : ret > 0 ? 0 : -IPSET_ERR_EXIST;
1357}
1358
1359/* Get headed data of a set */
1360
1361static int
1362ip_set_header(struct sock *ctnl, struct sk_buff *skb,
1363	      const struct nlmsghdr *nlh,
1364	      const struct nlattr * const attr[])
1365{
1366	const struct ip_set *set;
1367	struct sk_buff *skb2;
1368	struct nlmsghdr *nlh2;
1369	ip_set_id_t index;
1370	int ret = 0;
1371
1372	if (unlikely(protocol_failed(attr) ||
1373		     attr[IPSET_ATTR_SETNAME] == NULL))
1374		return -IPSET_ERR_PROTOCOL;
1375
1376	index = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
1377	if (index == IPSET_INVALID_ID)
1378		return -ENOENT;
1379	set = ip_set_list[index];
1380
1381	skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1382	if (skb2 == NULL)
1383		return -ENOMEM;
1384
1385	nlh2 = start_msg(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0,
1386			 IPSET_CMD_HEADER);
1387	if (!nlh2)
1388		goto nlmsg_failure;
1389	NLA_PUT_U8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL);
1390	NLA_PUT_STRING(skb2, IPSET_ATTR_SETNAME, set->name);
1391	NLA_PUT_STRING(skb2, IPSET_ATTR_TYPENAME, set->type->name);
1392	NLA_PUT_U8(skb2, IPSET_ATTR_FAMILY, set->family);
1393	NLA_PUT_U8(skb2, IPSET_ATTR_REVISION, set->revision);
1394	nlmsg_end(skb2, nlh2);
1395
1396	ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1397	if (ret < 0)
1398		return ret;
1399
1400	return 0;
1401
1402nla_put_failure:
1403	nlmsg_cancel(skb2, nlh2);
1404nlmsg_failure:
1405	kfree_skb(skb2);
1406	return -EMSGSIZE;
1407}
1408
1409/* Get type data */
1410
1411static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
1412	[IPSET_ATTR_PROTOCOL]	= { .type = NLA_U8 },
1413	[IPSET_ATTR_TYPENAME]	= { .type = NLA_NUL_STRING,
1414				    .len = IPSET_MAXNAMELEN - 1 },
1415	[IPSET_ATTR_FAMILY]	= { .type = NLA_U8 },
1416};
1417
1418static int
1419ip_set_type(struct sock *ctnl, struct sk_buff *skb,
1420	    const struct nlmsghdr *nlh,
1421	    const struct nlattr * const attr[])
1422{
1423	struct sk_buff *skb2;
1424	struct nlmsghdr *nlh2;
1425	u8 family, min, max;
1426	const char *typename;
1427	int ret = 0;
1428
1429	if (unlikely(protocol_failed(attr) ||
1430		     attr[IPSET_ATTR_TYPENAME] == NULL ||
1431		     attr[IPSET_ATTR_FAMILY] == NULL))
1432		return -IPSET_ERR_PROTOCOL;
1433
1434	family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
1435	typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
1436	ret = find_set_type_minmax(typename, family, &min, &max);
1437	if (ret)
1438		return ret;
1439
1440	skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1441	if (skb2 == NULL)
1442		return -ENOMEM;
1443
1444	nlh2 = start_msg(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0,
1445			 IPSET_CMD_TYPE);
1446	if (!nlh2)
1447		goto nlmsg_failure;
1448	NLA_PUT_U8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL);
1449	NLA_PUT_STRING(skb2, IPSET_ATTR_TYPENAME, typename);
1450	NLA_PUT_U8(skb2, IPSET_ATTR_FAMILY, family);
1451	NLA_PUT_U8(skb2, IPSET_ATTR_REVISION, max);
1452	NLA_PUT_U8(skb2, IPSET_ATTR_REVISION_MIN, min);
1453	nlmsg_end(skb2, nlh2);
1454
1455	pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
1456	ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1457	if (ret < 0)
1458		return ret;
1459
1460	return 0;
1461
1462nla_put_failure:
1463	nlmsg_cancel(skb2, nlh2);
1464nlmsg_failure:
1465	kfree_skb(skb2);
1466	return -EMSGSIZE;
1467}
1468
1469/* Get protocol version */
1470
1471static const struct nla_policy
1472ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
1473	[IPSET_ATTR_PROTOCOL]	= { .type = NLA_U8 },
1474};
1475
1476static int
1477ip_set_protocol(struct sock *ctnl, struct sk_buff *skb,
1478		const struct nlmsghdr *nlh,
1479		const struct nlattr * const attr[])
1480{
1481	struct sk_buff *skb2;
1482	struct nlmsghdr *nlh2;
1483	int ret = 0;
1484
1485	if (unlikely(attr[IPSET_ATTR_PROTOCOL] == NULL))
1486		return -IPSET_ERR_PROTOCOL;
1487
1488	skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1489	if (skb2 == NULL)
1490		return -ENOMEM;
1491
1492	nlh2 = start_msg(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0,
1493			 IPSET_CMD_PROTOCOL);
1494	if (!nlh2)
1495		goto nlmsg_failure;
1496	NLA_PUT_U8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL);
1497	nlmsg_end(skb2, nlh2);
1498
1499	ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1500	if (ret < 0)
1501		return ret;
1502
1503	return 0;
1504
1505nla_put_failure:
1506	nlmsg_cancel(skb2, nlh2);
1507nlmsg_failure:
1508	kfree_skb(skb2);
1509	return -EMSGSIZE;
1510}
1511
1512static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
1513	[IPSET_CMD_CREATE]	= {
1514		.call		= ip_set_create,
1515		.attr_count	= IPSET_ATTR_CMD_MAX,
1516		.policy		= ip_set_create_policy,
1517	},
1518	[IPSET_CMD_DESTROY]	= {
1519		.call		= ip_set_destroy,
1520		.attr_count	= IPSET_ATTR_CMD_MAX,
1521		.policy		= ip_set_setname_policy,
1522	},
1523	[IPSET_CMD_FLUSH]	= {
1524		.call		= ip_set_flush,
1525		.attr_count	= IPSET_ATTR_CMD_MAX,
1526		.policy		= ip_set_setname_policy,
1527	},
1528	[IPSET_CMD_RENAME]	= {
1529		.call		= ip_set_rename,
1530		.attr_count	= IPSET_ATTR_CMD_MAX,
1531		.policy		= ip_set_setname2_policy,
1532	},
1533	[IPSET_CMD_SWAP]	= {
1534		.call		= ip_set_swap,
1535		.attr_count	= IPSET_ATTR_CMD_MAX,
1536		.policy		= ip_set_setname2_policy,
1537	},
1538	[IPSET_CMD_LIST]	= {
1539		.call		= ip_set_dump,
1540		.attr_count	= IPSET_ATTR_CMD_MAX,
1541		.policy		= ip_set_setname_policy,
1542	},
1543	[IPSET_CMD_SAVE]	= {
1544		.call		= ip_set_dump,
1545		.attr_count	= IPSET_ATTR_CMD_MAX,
1546		.policy		= ip_set_setname_policy,
1547	},
1548	[IPSET_CMD_ADD]	= {
1549		.call		= ip_set_uadd,
1550		.attr_count	= IPSET_ATTR_CMD_MAX,
1551		.policy		= ip_set_adt_policy,
1552	},
1553	[IPSET_CMD_DEL]	= {
1554		.call		= ip_set_udel,
1555		.attr_count	= IPSET_ATTR_CMD_MAX,
1556		.policy		= ip_set_adt_policy,
1557	},
1558	[IPSET_CMD_TEST]	= {
1559		.call		= ip_set_utest,
1560		.attr_count	= IPSET_ATTR_CMD_MAX,
1561		.policy		= ip_set_adt_policy,
1562	},
1563	[IPSET_CMD_HEADER]	= {
1564		.call		= ip_set_header,
1565		.attr_count	= IPSET_ATTR_CMD_MAX,
1566		.policy		= ip_set_setname_policy,
1567	},
1568	[IPSET_CMD_TYPE]	= {
1569		.call		= ip_set_type,
1570		.attr_count	= IPSET_ATTR_CMD_MAX,
1571		.policy		= ip_set_type_policy,
1572	},
1573	[IPSET_CMD_PROTOCOL]	= {
1574		.call		= ip_set_protocol,
1575		.attr_count	= IPSET_ATTR_CMD_MAX,
1576		.policy		= ip_set_protocol_policy,
1577	},
1578};
1579
1580static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
1581	.name		= "ip_set",
1582	.subsys_id	= NFNL_SUBSYS_IPSET,
1583	.cb_count	= IPSET_MSG_MAX,
1584	.cb		= ip_set_netlink_subsys_cb,
1585};
1586
1587/* Interface to iptables/ip6tables */
1588
1589static int
1590ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
1591{
1592	unsigned *op;
1593	void *data;
1594	int copylen = *len, ret = 0;
1595
1596	if (!capable(CAP_NET_ADMIN))
1597		return -EPERM;
1598	if (optval != SO_IP_SET)
1599		return -EBADF;
1600	if (*len < sizeof(unsigned))
1601		return -EINVAL;
1602
1603	data = vmalloc(*len);
1604	if (!data)
1605		return -ENOMEM;
1606	if (copy_from_user(data, user, *len) != 0) {
1607		ret = -EFAULT;
1608		goto done;
1609	}
1610	op = (unsigned *) data;
1611
1612	if (*op < IP_SET_OP_VERSION) {
1613		/* Check the version at the beginning of operations */
1614		struct ip_set_req_version *req_version = data;
1615		if (req_version->version != IPSET_PROTOCOL) {
1616			ret = -EPROTO;
1617			goto done;
1618		}
1619	}
1620
1621	switch (*op) {
1622	case IP_SET_OP_VERSION: {
1623		struct ip_set_req_version *req_version = data;
1624
1625		if (*len != sizeof(struct ip_set_req_version)) {
1626			ret = -EINVAL;
1627			goto done;
1628		}
1629
1630		req_version->version = IPSET_PROTOCOL;
1631		ret = copy_to_user(user, req_version,
1632				   sizeof(struct ip_set_req_version));
1633		goto done;
1634	}
1635	case IP_SET_OP_GET_BYNAME: {
1636		struct ip_set_req_get_set *req_get = data;
1637
1638		if (*len != sizeof(struct ip_set_req_get_set)) {
1639			ret = -EINVAL;
1640			goto done;
1641		}
1642		req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1643		nfnl_lock();
1644		req_get->set.index = find_set_id(req_get->set.name);
1645		nfnl_unlock();
1646		goto copy;
1647	}
1648	case IP_SET_OP_GET_BYINDEX: {
1649		struct ip_set_req_get_set *req_get = data;
1650
1651		if (*len != sizeof(struct ip_set_req_get_set) ||
1652		    req_get->set.index >= ip_set_max) {
1653			ret = -EINVAL;
1654			goto done;
1655		}
1656		nfnl_lock();
1657		strncpy(req_get->set.name,
1658			ip_set_list[req_get->set.index]
1659				? ip_set_list[req_get->set.index]->name : "",
1660			IPSET_MAXNAMELEN);
1661		nfnl_unlock();
1662		goto copy;
1663	}
1664	default:
1665		ret = -EBADMSG;
1666		goto done;
1667	}	/* end of switch(op) */
1668
1669copy:
1670	ret = copy_to_user(user, data, copylen);
1671
1672done:
1673	vfree(data);
1674	if (ret > 0)
1675		ret = 0;
1676	return ret;
1677}
1678
1679static struct nf_sockopt_ops so_set __read_mostly = {
1680	.pf		= PF_INET,
1681	.get_optmin	= SO_IP_SET,
1682	.get_optmax	= SO_IP_SET + 1,
1683	.get		= &ip_set_sockfn_get,
1684	.owner		= THIS_MODULE,
1685};
1686
1687static int __init
1688ip_set_init(void)
1689{
1690	int ret;
1691
1692	if (max_sets)
1693		ip_set_max = max_sets;
1694	if (ip_set_max >= IPSET_INVALID_ID)
1695		ip_set_max = IPSET_INVALID_ID - 1;
1696
1697	ip_set_list = kzalloc(sizeof(struct ip_set *) * ip_set_max,
1698			      GFP_KERNEL);
1699	if (!ip_set_list) {
1700		pr_err("ip_set: Unable to create ip_set_list\n");
1701		return -ENOMEM;
1702	}
1703
1704	ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
1705	if (ret != 0) {
1706		pr_err("ip_set: cannot register with nfnetlink.\n");
1707		kfree(ip_set_list);
1708		return ret;
1709	}
1710	ret = nf_register_sockopt(&so_set);
1711	if (ret != 0) {
1712		pr_err("SO_SET registry failed: %d\n", ret);
1713		nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
1714		kfree(ip_set_list);
1715		return ret;
1716	}
1717
1718	pr_notice("ip_set: protocol %u\n", IPSET_PROTOCOL);
1719	return 0;
1720}
1721
1722static void __exit
1723ip_set_fini(void)
1724{
1725	/* There can't be any existing set */
1726	nf_unregister_sockopt(&so_set);
1727	nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
1728	kfree(ip_set_list);
1729	pr_debug("these are the famous last words\n");
1730}
1731
1732module_init(ip_set_init);
1733module_exit(ip_set_fini);
1734