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