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