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