[8.09] kernel: refresh patches
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches-2.6.24 / 130-netfilter_ipset.patch
1 --- /dev/null
2 +++ b/include/linux/netfilter_ipv4/ip_set.h
3 @@ -0,0 +1,498 @@
4 +#ifndef _IP_SET_H
5 +#define _IP_SET_H
6 +
7 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
8 + * Patrick Schaaf <bof@bof.de>
9 + * Martin Josefsson <gandalf@wlug.westbo.se>
10 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
11 + *
12 + * This program is free software; you can redistribute it and/or modify
13 + * it under the terms of the GNU General Public License version 2 as
14 + * published by the Free Software Foundation.
15 + */
16 +
17 +#if 0
18 +#define IP_SET_DEBUG
19 +#endif
20 +
21 +/*
22 + * A sockopt of such quality has hardly ever been seen before on the open
23 + * market! This little beauty, hardly ever used: above 64, so it's
24 + * traditionally used for firewalling, not touched (even once!) by the
25 + * 2.0, 2.2 and 2.4 kernels!
26 + *
27 + * Comes with its own certificate of authenticity, valid anywhere in the
28 + * Free world!
29 + *
30 + * Rusty, 19.4.2000
31 + */
32 +#define SO_IP_SET 83
33 +
34 +/*
35 + * Heavily modify by Joakim Axelsson 08.03.2002
36 + * - Made it more modulebased
37 + *
38 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
39 + * - bindings added
40 + * - in order to "deal with" backward compatibility, renamed to ipset
41 + */
42 +
43 +/*
44 + * Used so that the kernel module and ipset-binary can match their versions
45 + */
46 +#define IP_SET_PROTOCOL_VERSION 2
47 +
48 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
49 +
50 +/* Lets work with our own typedef for representing an IP address.
51 + * We hope to make the code more portable, possibly to IPv6...
52 + *
53 + * The representation works in HOST byte order, because most set types
54 + * will perform arithmetic operations and compare operations.
55 + *
56 + * For now the type is an uint32_t.
57 + *
58 + * Make sure to ONLY use the functions when translating and parsing
59 + * in order to keep the host byte order and make it more portable:
60 + * parse_ip()
61 + * parse_mask()
62 + * parse_ipandmask()
63 + * ip_tostring()
64 + * (Joakim: where are they???)
65 + */
66 +
67 +typedef uint32_t ip_set_ip_t;
68 +
69 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
70 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
71 + */
72 +typedef uint16_t ip_set_id_t;
73 +
74 +#define IP_SET_INVALID_ID 65535
75 +
76 +/* How deep we follow bindings */
77 +#define IP_SET_MAX_BINDINGS 6
78 +
79 +/*
80 + * Option flags for kernel operations (ipt_set_info)
81 + */
82 +#define IPSET_SRC 0x01 /* Source match/add */
83 +#define IPSET_DST 0x02 /* Destination match/add */
84 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
85 +
86 +/*
87 + * Set features
88 + */
89 +#define IPSET_TYPE_IP 0x01 /* IP address type of set */
90 +#define IPSET_TYPE_PORT 0x02 /* Port type of set */
91 +#define IPSET_DATA_SINGLE 0x04 /* Single data storage */
92 +#define IPSET_DATA_DOUBLE 0x08 /* Double data storage */
93 +
94 +/* Reserved keywords */
95 +#define IPSET_TOKEN_DEFAULT ":default:"
96 +#define IPSET_TOKEN_ALL ":all:"
97 +
98 +/* SO_IP_SET operation constants, and their request struct types.
99 + *
100 + * Operation ids:
101 + * 0-99: commands with version checking
102 + * 100-199: add/del/test/bind/unbind
103 + * 200-299: list, save, restore
104 + */
105 +
106 +/* Single shot operations:
107 + * version, create, destroy, flush, rename and swap
108 + *
109 + * Sets are identified by name.
110 + */
111 +
112 +#define IP_SET_REQ_STD \
113 + unsigned op; \
114 + unsigned version; \
115 + char name[IP_SET_MAXNAMELEN]
116 +
117 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
118 +struct ip_set_req_create {
119 + IP_SET_REQ_STD;
120 + char typename[IP_SET_MAXNAMELEN];
121 +};
122 +
123 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
124 +struct ip_set_req_std {
125 + IP_SET_REQ_STD;
126 +};
127 +
128 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
129 +/* Uses ip_set_req_std */
130 +
131 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
132 +/* Uses ip_set_req_create */
133 +
134 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
135 +/* Uses ip_set_req_create */
136 +
137 +union ip_set_name_index {
138 + char name[IP_SET_MAXNAMELEN];
139 + ip_set_id_t index;
140 +};
141 +
142 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
143 +struct ip_set_req_get_set {
144 + unsigned op;
145 + unsigned version;
146 + union ip_set_name_index set;
147 +};
148 +
149 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
150 +/* Uses ip_set_req_get_set */
151 +
152 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
153 +struct ip_set_req_version {
154 + unsigned op;
155 + unsigned version;
156 +};
157 +
158 +/* Double shots operations:
159 + * add, del, test, bind and unbind.
160 + *
161 + * First we query the kernel to get the index and type of the target set,
162 + * then issue the command. Validity of IP is checked in kernel in order
163 + * to minimalize sockopt operations.
164 + */
165 +
166 +/* Get minimal set data for add/del/test/bind/unbind IP */
167 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
168 +struct ip_set_req_adt_get {
169 + unsigned op;
170 + unsigned version;
171 + union ip_set_name_index set;
172 + char typename[IP_SET_MAXNAMELEN];
173 +};
174 +
175 +#define IP_SET_REQ_BYINDEX \
176 + unsigned op; \
177 + ip_set_id_t index;
178 +
179 +struct ip_set_req_adt {
180 + IP_SET_REQ_BYINDEX;
181 +};
182 +
183 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
184 +/* Uses ip_set_req_adt, with type specific addage */
185 +
186 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
187 +/* Uses ip_set_req_adt, with type specific addage */
188 +
189 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
190 +/* Uses ip_set_req_adt, with type specific addage */
191 +
192 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
193 +/* Uses ip_set_req_bind, with type specific addage */
194 +struct ip_set_req_bind {
195 + IP_SET_REQ_BYINDEX;
196 + char binding[IP_SET_MAXNAMELEN];
197 +};
198 +
199 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
200 +/* Uses ip_set_req_bind, with type speficic addage
201 + * index = 0 means unbinding for all sets */
202 +
203 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
204 +/* Uses ip_set_req_bind, with type specific addage */
205 +
206 +/* Multiple shots operations: list, save, restore.
207 + *
208 + * - check kernel version and query the max number of sets
209 + * - get the basic information on all sets
210 + * and size required for the next step
211 + * - get actual set data: header, data, bindings
212 + */
213 +
214 +/* Get max_sets and the index of a queried set
215 + */
216 +#define IP_SET_OP_MAX_SETS 0x00000020
217 +struct ip_set_req_max_sets {
218 + unsigned op;
219 + unsigned version;
220 + ip_set_id_t max_sets; /* max_sets */
221 + ip_set_id_t sets; /* real number of sets */
222 + union ip_set_name_index set; /* index of set if name used */
223 +};
224 +
225 +/* Get the id and name of the sets plus size for next step */
226 +#define IP_SET_OP_LIST_SIZE 0x00000201
227 +#define IP_SET_OP_SAVE_SIZE 0x00000202
228 +struct ip_set_req_setnames {
229 + unsigned op;
230 + ip_set_id_t index; /* set to list/save */
231 + size_t size; /* size to get setdata/bindings */
232 + /* followed by sets number of struct ip_set_name_list */
233 +};
234 +
235 +struct ip_set_name_list {
236 + char name[IP_SET_MAXNAMELEN];
237 + char typename[IP_SET_MAXNAMELEN];
238 + ip_set_id_t index;
239 + ip_set_id_t id;
240 +};
241 +
242 +/* The actual list operation */
243 +#define IP_SET_OP_LIST 0x00000203
244 +struct ip_set_req_list {
245 + IP_SET_REQ_BYINDEX;
246 + /* sets number of struct ip_set_list in reply */
247 +};
248 +
249 +struct ip_set_list {
250 + ip_set_id_t index;
251 + ip_set_id_t binding;
252 + u_int32_t ref;
253 + size_t header_size; /* Set header data of header_size */
254 + size_t members_size; /* Set members data of members_size */
255 + size_t bindings_size; /* Set bindings data of bindings_size */
256 +};
257 +
258 +struct ip_set_hash_list {
259 + ip_set_ip_t ip;
260 + ip_set_id_t binding;
261 +};
262 +
263 +/* The save operation */
264 +#define IP_SET_OP_SAVE 0x00000204
265 +/* Uses ip_set_req_list, in the reply replaced by
266 + * sets number of struct ip_set_save plus a marker
267 + * ip_set_save followed by ip_set_hash_save structures.
268 + */
269 +struct ip_set_save {
270 + ip_set_id_t index;
271 + ip_set_id_t binding;
272 + size_t header_size; /* Set header data of header_size */
273 + size_t members_size; /* Set members data of members_size */
274 +};
275 +
276 +/* At restoring, ip == 0 means default binding for the given set: */
277 +struct ip_set_hash_save {
278 + ip_set_ip_t ip;
279 + ip_set_id_t id;
280 + ip_set_id_t binding;
281 +};
282 +
283 +/* The restore operation */
284 +#define IP_SET_OP_RESTORE 0x00000205
285 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
286 + * plus a marker ip_set_restore, followed by ip_set_hash_save
287 + * structures.
288 + */
289 +struct ip_set_restore {
290 + char name[IP_SET_MAXNAMELEN];
291 + char typename[IP_SET_MAXNAMELEN];
292 + ip_set_id_t index;
293 + size_t header_size; /* Create data of header_size */
294 + size_t members_size; /* Set members data of members_size */
295 +};
296 +
297 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
298 +{
299 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
300 +}
301 +
302 +#ifdef __KERNEL__
303 +
304 +#define ip_set_printk(format, args...) \
305 + do { \
306 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
307 + printk(format "\n" , ## args); \
308 + } while (0)
309 +
310 +#if defined(IP_SET_DEBUG)
311 +#define DP(format, args...) \
312 + do { \
313 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
314 + printk(format "\n" , ## args); \
315 + } while (0)
316 +#define IP_SET_ASSERT(x) \
317 + do { \
318 + if (!(x)) \
319 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
320 + __FILE__, __LINE__, __FUNCTION__); \
321 + } while (0)
322 +#else
323 +#define DP(format, args...)
324 +#define IP_SET_ASSERT(x)
325 +#endif
326 +
327 +struct ip_set;
328 +
329 +/*
330 + * The ip_set_type definition - one per set type, e.g. "ipmap".
331 + *
332 + * Each individual set has a pointer, set->type, going to one
333 + * of these structures. Function pointers inside the structure implement
334 + * the real behaviour of the sets.
335 + *
336 + * If not mentioned differently, the implementation behind the function
337 + * pointers of a set_type, is expected to return 0 if ok, and a negative
338 + * errno (e.g. -EINVAL) on error.
339 + */
340 +struct ip_set_type {
341 + struct list_head list; /* next in list of set types */
342 +
343 + /* test for IP in set (kernel: iptables -m set src|dst)
344 + * return 0 if not in set, 1 if in set.
345 + */
346 + int (*testip_kernel) (struct ip_set *set,
347 + const struct sk_buff * skb,
348 + ip_set_ip_t *ip,
349 + const u_int32_t *flags,
350 + unsigned char index);
351 +
352 + /* test for IP in set (userspace: ipset -T set IP)
353 + * return 0 if not in set, 1 if in set.
354 + */
355 + int (*testip) (struct ip_set *set,
356 + const void *data, size_t size,
357 + ip_set_ip_t *ip);
358 +
359 + /*
360 + * Size of the data structure passed by when
361 + * adding/deletin/testing an entry.
362 + */
363 + size_t reqsize;
364 +
365 + /* Add IP into set (userspace: ipset -A set IP)
366 + * Return -EEXIST if the address is already in the set,
367 + * and -ERANGE if the address lies outside the set bounds.
368 + * If the address was not already in the set, 0 is returned.
369 + */
370 + int (*addip) (struct ip_set *set,
371 + const void *data, size_t size,
372 + ip_set_ip_t *ip);
373 +
374 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
375 + * Return -EEXIST if the address is already in the set,
376 + * and -ERANGE if the address lies outside the set bounds.
377 + * If the address was not already in the set, 0 is returned.
378 + */
379 + int (*addip_kernel) (struct ip_set *set,
380 + const struct sk_buff * skb,
381 + ip_set_ip_t *ip,
382 + const u_int32_t *flags,
383 + unsigned char index);
384 +
385 + /* remove IP from set (userspace: ipset -D set --entry x)
386 + * Return -EEXIST if the address is NOT in the set,
387 + * and -ERANGE if the address lies outside the set bounds.
388 + * If the address really was in the set, 0 is returned.
389 + */
390 + int (*delip) (struct ip_set *set,
391 + const void *data, size_t size,
392 + ip_set_ip_t *ip);
393 +
394 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
395 + * Return -EEXIST if the address is NOT in the set,
396 + * and -ERANGE if the address lies outside the set bounds.
397 + * If the address really was in the set, 0 is returned.
398 + */
399 + int (*delip_kernel) (struct ip_set *set,
400 + const struct sk_buff * skb,
401 + ip_set_ip_t *ip,
402 + const u_int32_t *flags,
403 + unsigned char index);
404 +
405 + /* new set creation - allocated type specific items
406 + */
407 + int (*create) (struct ip_set *set,
408 + const void *data, size_t size);
409 +
410 + /* retry the operation after successfully tweaking the set
411 + */
412 + int (*retry) (struct ip_set *set);
413 +
414 + /* set destruction - free type specific items
415 + * There is no return value.
416 + * Can be called only when child sets are destroyed.
417 + */
418 + void (*destroy) (struct ip_set *set);
419 +
420 + /* set flushing - reset all bits in the set, or something similar.
421 + * There is no return value.
422 + */
423 + void (*flush) (struct ip_set *set);
424 +
425 + /* Listing: size needed for header
426 + */
427 + size_t header_size;
428 +
429 + /* Listing: Get the header
430 + *
431 + * Fill in the information in "data".
432 + * This function is always run after list_header_size() under a
433 + * writelock on the set. Therefor is the length of "data" always
434 + * correct.
435 + */
436 + void (*list_header) (const struct ip_set *set,
437 + void *data);
438 +
439 + /* Listing: Get the size for the set members
440 + */
441 + int (*list_members_size) (const struct ip_set *set);
442 +
443 + /* Listing: Get the set members
444 + *
445 + * Fill in the information in "data".
446 + * This function is always run after list_member_size() under a
447 + * writelock on the set. Therefor is the length of "data" always
448 + * correct.
449 + */
450 + void (*list_members) (const struct ip_set *set,
451 + void *data);
452 +
453 + char typename[IP_SET_MAXNAMELEN];
454 + unsigned char features;
455 + int protocol_version;
456 +
457 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
458 + struct module *me;
459 +};
460 +
461 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
462 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
463 +
464 +/* A generic ipset */
465 +struct ip_set {
466 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
467 + rwlock_t lock; /* lock for concurrency control */
468 + ip_set_id_t id; /* set id for swapping */
469 + ip_set_id_t binding; /* default binding for the set */
470 + atomic_t ref; /* in kernel and in hash references */
471 + struct ip_set_type *type; /* the set types */
472 + void *data; /* pooltype specific data */
473 +};
474 +
475 +/* Structure to bind set elements to sets */
476 +struct ip_set_hash {
477 + struct list_head list; /* list of clashing entries in hash */
478 + ip_set_ip_t ip; /* ip from set */
479 + ip_set_id_t id; /* set id */
480 + ip_set_id_t binding; /* set we bind the element to */
481 +};
482 +
483 +/* register and unregister set references */
484 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
485 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
486 +extern void ip_set_put(ip_set_id_t id);
487 +
488 +/* API for iptables set match, and SET target */
489 +extern void ip_set_addip_kernel(ip_set_id_t id,
490 + const struct sk_buff *skb,
491 + const u_int32_t *flags);
492 +extern void ip_set_delip_kernel(ip_set_id_t id,
493 + const struct sk_buff *skb,
494 + const u_int32_t *flags);
495 +extern int ip_set_testip_kernel(ip_set_id_t id,
496 + const struct sk_buff *skb,
497 + const u_int32_t *flags);
498 +
499 +#endif /* __KERNEL__ */
500 +
501 +#endif /*_IP_SET_H*/
502 --- /dev/null
503 +++ b/include/linux/netfilter_ipv4/ip_set_iphash.h
504 @@ -0,0 +1,30 @@
505 +#ifndef __IP_SET_IPHASH_H
506 +#define __IP_SET_IPHASH_H
507 +
508 +#include <linux/netfilter_ipv4/ip_set.h>
509 +
510 +#define SETTYPE_NAME "iphash"
511 +#define MAX_RANGE 0x0000FFFF
512 +
513 +struct ip_set_iphash {
514 + ip_set_ip_t *members; /* the iphash proper */
515 + uint32_t elements; /* number of elements */
516 + uint32_t hashsize; /* hash size */
517 + uint16_t probes; /* max number of probes */
518 + uint16_t resize; /* resize factor in percent */
519 + ip_set_ip_t netmask; /* netmask */
520 + void *initval[0]; /* initvals for jhash_1word */
521 +};
522 +
523 +struct ip_set_req_iphash_create {
524 + uint32_t hashsize;
525 + uint16_t probes;
526 + uint16_t resize;
527 + ip_set_ip_t netmask;
528 +};
529 +
530 +struct ip_set_req_iphash {
531 + ip_set_ip_t ip;
532 +};
533 +
534 +#endif /* __IP_SET_IPHASH_H */
535 --- /dev/null
536 +++ b/include/linux/netfilter_ipv4/ip_set_ipmap.h
537 @@ -0,0 +1,56 @@
538 +#ifndef __IP_SET_IPMAP_H
539 +#define __IP_SET_IPMAP_H
540 +
541 +#include <linux/netfilter_ipv4/ip_set.h>
542 +
543 +#define SETTYPE_NAME "ipmap"
544 +#define MAX_RANGE 0x0000FFFF
545 +
546 +struct ip_set_ipmap {
547 + void *members; /* the ipmap proper */
548 + ip_set_ip_t first_ip; /* host byte order, included in range */
549 + ip_set_ip_t last_ip; /* host byte order, included in range */
550 + ip_set_ip_t netmask; /* subnet netmask */
551 + ip_set_ip_t sizeid; /* size of set in IPs */
552 + ip_set_ip_t hosts; /* number of hosts in a subnet */
553 +};
554 +
555 +struct ip_set_req_ipmap_create {
556 + ip_set_ip_t from;
557 + ip_set_ip_t to;
558 + ip_set_ip_t netmask;
559 +};
560 +
561 +struct ip_set_req_ipmap {
562 + ip_set_ip_t ip;
563 +};
564 +
565 +unsigned int
566 +mask_to_bits(ip_set_ip_t mask)
567 +{
568 + unsigned int bits = 32;
569 + ip_set_ip_t maskaddr;
570 +
571 + if (mask == 0xFFFFFFFF)
572 + return bits;
573 +
574 + maskaddr = 0xFFFFFFFE;
575 + while (--bits >= 0 && maskaddr != mask)
576 + maskaddr <<= 1;
577 +
578 + return bits;
579 +}
580 +
581 +ip_set_ip_t
582 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
583 +{
584 + ip_set_ip_t mask = 0xFFFFFFFE;
585 +
586 + *bits = 32;
587 + while (--(*bits) >= 0 && mask && (to & mask) != from)
588 + mask <<= 1;
589 +
590 + return mask;
591 +}
592 +
593 +#endif /* __IP_SET_IPMAP_H */
594 --- /dev/null
595 +++ b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
596 @@ -0,0 +1,34 @@
597 +#ifndef __IP_SET_IPPORTHASH_H
598 +#define __IP_SET_IPPORTHASH_H
599 +
600 +#include <linux/netfilter_ipv4/ip_set.h>
601 +
602 +#define SETTYPE_NAME "ipporthash"
603 +#define MAX_RANGE 0x0000FFFF
604 +#define INVALID_PORT (MAX_RANGE + 1)
605 +
606 +struct ip_set_ipporthash {
607 + ip_set_ip_t *members; /* the ipporthash proper */
608 + uint32_t elements; /* number of elements */
609 + uint32_t hashsize; /* hash size */
610 + uint16_t probes; /* max number of probes */
611 + uint16_t resize; /* resize factor in percent */
612 + ip_set_ip_t first_ip; /* host byte order, included in range */
613 + ip_set_ip_t last_ip; /* host byte order, included in range */
614 + void *initval[0]; /* initvals for jhash_1word */
615 +};
616 +
617 +struct ip_set_req_ipporthash_create {
618 + uint32_t hashsize;
619 + uint16_t probes;
620 + uint16_t resize;
621 + ip_set_ip_t from;
622 + ip_set_ip_t to;
623 +};
624 +
625 +struct ip_set_req_ipporthash {
626 + ip_set_ip_t ip;
627 + ip_set_ip_t port;
628 +};
629 +
630 +#endif /* __IP_SET_IPPORTHASH_H */
631 --- /dev/null
632 +++ b/include/linux/netfilter_ipv4/ip_set_iptree.h
633 @@ -0,0 +1,40 @@
634 +#ifndef __IP_SET_IPTREE_H
635 +#define __IP_SET_IPTREE_H
636 +
637 +#include <linux/netfilter_ipv4/ip_set.h>
638 +
639 +#define SETTYPE_NAME "iptree"
640 +#define MAX_RANGE 0x0000FFFF
641 +
642 +struct ip_set_iptreed {
643 + unsigned long expires[256]; /* x.x.x.ADDR */
644 +};
645 +
646 +struct ip_set_iptreec {
647 + struct ip_set_iptreed *tree[256]; /* x.x.ADDR.* */
648 +};
649 +
650 +struct ip_set_iptreeb {
651 + struct ip_set_iptreec *tree[256]; /* x.ADDR.*.* */
652 +};
653 +
654 +struct ip_set_iptree {
655 + unsigned int timeout;
656 + unsigned int gc_interval;
657 +#ifdef __KERNEL__
658 + uint32_t elements; /* number of elements */
659 + struct timer_list gc;
660 + struct ip_set_iptreeb *tree[256]; /* ADDR.*.*.* */
661 +#endif
662 +};
663 +
664 +struct ip_set_req_iptree_create {
665 + unsigned int timeout;
666 +};
667 +
668 +struct ip_set_req_iptree {
669 + ip_set_ip_t ip;
670 + unsigned int timeout;
671 +};
672 +
673 +#endif /* __IP_SET_IPTREE_H */
674 --- /dev/null
675 +++ b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
676 @@ -0,0 +1,40 @@
677 +#ifndef __IP_SET_IPTREEMAP_H
678 +#define __IP_SET_IPTREEMAP_H
679 +
680 +#include <linux/netfilter_ipv4/ip_set.h>
681 +
682 +#define SETTYPE_NAME "iptreemap"
683 +
684 +#ifdef __KERNEL__
685 +struct ip_set_iptreemap_d {
686 + unsigned char bitmap[32]; /* x.x.x.y */
687 +};
688 +
689 +struct ip_set_iptreemap_c {
690 + struct ip_set_iptreemap_d *tree[256]; /* x.x.y.x */
691 +};
692 +
693 +struct ip_set_iptreemap_b {
694 + struct ip_set_iptreemap_c *tree[256]; /* x.y.x.x */
695 + unsigned char dirty[32];
696 +};
697 +#endif
698 +
699 +struct ip_set_iptreemap {
700 + unsigned int gc_interval;
701 +#ifdef __KERNEL__
702 + struct timer_list gc;
703 + struct ip_set_iptreemap_b *tree[256]; /* y.x.x.x */
704 +#endif
705 +};
706 +
707 +struct ip_set_req_iptreemap_create {
708 + unsigned int gc_interval;
709 +};
710 +
711 +struct ip_set_req_iptreemap {
712 + ip_set_ip_t start;
713 + ip_set_ip_t end;
714 +};
715 +
716 +#endif /* __IP_SET_IPTREEMAP_H */
717 --- /dev/null
718 +++ b/include/linux/netfilter_ipv4/ip_set_jhash.h
719 @@ -0,0 +1,148 @@
720 +#ifndef _LINUX_IPSET_JHASH_H
721 +#define _LINUX_IPSET_JHASH_H
722 +
723 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
724 + * to __u32/__u8 so that the header file can be included into
725 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
726 + */
727 +
728 +/* jhash.h: Jenkins hash support.
729 + *
730 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
731 + *
732 + * http://burtleburtle.net/bob/hash/
733 + *
734 + * These are the credits from Bob's sources:
735 + *
736 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
737 + * hash(), hash2(), hash3, and mix() are externally useful functions.
738 + * Routines to test the hash are included if SELF_TEST is defined.
739 + * You can use this free for any purpose. It has no warranty.
740 + *
741 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
742 + *
743 + * I've modified Bob's hash to be useful in the Linux kernel, and
744 + * any bugs present are surely my fault. -DaveM
745 + */
746 +
747 +/* NOTE: Arguments are modified. */
748 +#define __jhash_mix(a, b, c) \
749 +{ \
750 + a -= b; a -= c; a ^= (c>>13); \
751 + b -= c; b -= a; b ^= (a<<8); \
752 + c -= a; c -= b; c ^= (b>>13); \
753 + a -= b; a -= c; a ^= (c>>12); \
754 + b -= c; b -= a; b ^= (a<<16); \
755 + c -= a; c -= b; c ^= (b>>5); \
756 + a -= b; a -= c; a ^= (c>>3); \
757 + b -= c; b -= a; b ^= (a<<10); \
758 + c -= a; c -= b; c ^= (b>>15); \
759 +}
760 +
761 +/* The golden ration: an arbitrary value */
762 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
763 +
764 +/* The most generic version, hashes an arbitrary sequence
765 + * of bytes. No alignment or length assumptions are made about
766 + * the input key.
767 + */
768 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
769 +{
770 + __u32 a, b, c, len;
771 + __u8 *k = key;
772 +
773 + len = length;
774 + a = b = JHASH_GOLDEN_RATIO;
775 + c = initval;
776 +
777 + while (len >= 12) {
778 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
779 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
780 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
781 +
782 + __jhash_mix(a,b,c);
783 +
784 + k += 12;
785 + len -= 12;
786 + }
787 +
788 + c += length;
789 + switch (len) {
790 + case 11: c += ((__u32)k[10]<<24);
791 + case 10: c += ((__u32)k[9]<<16);
792 + case 9 : c += ((__u32)k[8]<<8);
793 + case 8 : b += ((__u32)k[7]<<24);
794 + case 7 : b += ((__u32)k[6]<<16);
795 + case 6 : b += ((__u32)k[5]<<8);
796 + case 5 : b += k[4];
797 + case 4 : a += ((__u32)k[3]<<24);
798 + case 3 : a += ((__u32)k[2]<<16);
799 + case 2 : a += ((__u32)k[1]<<8);
800 + case 1 : a += k[0];
801 + };
802 +
803 + __jhash_mix(a,b,c);
804 +
805 + return c;
806 +}
807 +
808 +/* A special optimized version that handles 1 or more of __u32s.
809 + * The length parameter here is the number of __u32s in the key.
810 + */
811 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
812 +{
813 + __u32 a, b, c, len;
814 +
815 + a = b = JHASH_GOLDEN_RATIO;
816 + c = initval;
817 + len = length;
818 +
819 + while (len >= 3) {
820 + a += k[0];
821 + b += k[1];
822 + c += k[2];
823 + __jhash_mix(a, b, c);
824 + k += 3; len -= 3;
825 + }
826 +
827 + c += length * 4;
828 +
829 + switch (len) {
830 + case 2 : b += k[1];
831 + case 1 : a += k[0];
832 + };
833 +
834 + __jhash_mix(a,b,c);
835 +
836 + return c;
837 +}
838 +
839 +
840 +/* A special ultra-optimized versions that knows they are hashing exactly
841 + * 3, 2 or 1 word(s).
842 + *
843 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
844 + * done at the end is not done here.
845 + */
846 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
847 +{
848 + a += JHASH_GOLDEN_RATIO;
849 + b += JHASH_GOLDEN_RATIO;
850 + c += initval;
851 +
852 + __jhash_mix(a, b, c);
853 +
854 + return c;
855 +}
856 +
857 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
858 +{
859 + return jhash_3words(a, b, 0, initval);
860 +}
861 +
862 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
863 +{
864 + return jhash_3words(a, 0, 0, initval);
865 +}
866 +
867 +#endif /* _LINUX_IPSET_JHASH_H */
868 --- /dev/null
869 +++ b/include/linux/netfilter_ipv4/ip_set_macipmap.h
870 @@ -0,0 +1,38 @@
871 +#ifndef __IP_SET_MACIPMAP_H
872 +#define __IP_SET_MACIPMAP_H
873 +
874 +#include <linux/netfilter_ipv4/ip_set.h>
875 +
876 +#define SETTYPE_NAME "macipmap"
877 +#define MAX_RANGE 0x0000FFFF
878 +
879 +/* general flags */
880 +#define IPSET_MACIP_MATCHUNSET 1
881 +
882 +/* per ip flags */
883 +#define IPSET_MACIP_ISSET 1
884 +
885 +struct ip_set_macipmap {
886 + void *members; /* the macipmap proper */
887 + ip_set_ip_t first_ip; /* host byte order, included in range */
888 + ip_set_ip_t last_ip; /* host byte order, included in range */
889 + u_int32_t flags;
890 +};
891 +
892 +struct ip_set_req_macipmap_create {
893 + ip_set_ip_t from;
894 + ip_set_ip_t to;
895 + u_int32_t flags;
896 +};
897 +
898 +struct ip_set_req_macipmap {
899 + ip_set_ip_t ip;
900 + unsigned char ethernet[ETH_ALEN];
901 +};
902 +
903 +struct ip_set_macip {
904 + unsigned short flags;
905 + unsigned char ethernet[ETH_ALEN];
906 +};
907 +
908 +#endif /* __IP_SET_MACIPMAP_H */
909 --- /dev/null
910 +++ b/include/linux/netfilter_ipv4/ip_set_malloc.h
911 @@ -0,0 +1,116 @@
912 +#ifndef _IP_SET_MALLOC_H
913 +#define _IP_SET_MALLOC_H
914 +
915 +#ifdef __KERNEL__
916 +
917 +/* Memory allocation and deallocation */
918 +static size_t max_malloc_size = 0;
919 +
920 +static inline void init_max_malloc_size(void)
921 +{
922 +#define CACHE(x) max_malloc_size = x;
923 +#include <linux/kmalloc_sizes.h>
924 +#undef CACHE
925 +}
926 +
927 +static inline void * ip_set_malloc(size_t bytes)
928 +{
929 + if (bytes > max_malloc_size)
930 + return vmalloc(bytes);
931 + else
932 + return kmalloc(bytes, GFP_KERNEL);
933 +}
934 +
935 +static inline void ip_set_free(void * data, size_t bytes)
936 +{
937 + if (bytes > max_malloc_size)
938 + vfree(data);
939 + else
940 + kfree(data);
941 +}
942 +
943 +struct harray {
944 + size_t max_elements;
945 + void *arrays[0];
946 +};
947 +
948 +static inline void *
949 +harray_malloc(size_t hashsize, size_t typesize, int flags)
950 +{
951 + struct harray *harray;
952 + size_t max_elements, size, i, j;
953 +
954 + if (!max_malloc_size)
955 + init_max_malloc_size();
956 +
957 + if (typesize > max_malloc_size)
958 + return NULL;
959 +
960 + max_elements = max_malloc_size/typesize;
961 + size = hashsize/max_elements;
962 + if (hashsize % max_elements)
963 + size++;
964 +
965 + /* Last pointer signals end of arrays */
966 + harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
967 + flags);
968 +
969 + if (!harray)
970 + return NULL;
971 +
972 + for (i = 0; i < size - 1; i++) {
973 + harray->arrays[i] = kmalloc(max_elements * typesize, flags);
974 + if (!harray->arrays[i])
975 + goto undo;
976 + memset(harray->arrays[i], 0, max_elements * typesize);
977 + }
978 + harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize,
979 + flags);
980 + if (!harray->arrays[i])
981 + goto undo;
982 + memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
983 +
984 + harray->max_elements = max_elements;
985 + harray->arrays[size] = NULL;
986 +
987 + return (void *)harray;
988 +
989 + undo:
990 + for (j = 0; j < i; j++) {
991 + kfree(harray->arrays[j]);
992 + }
993 + kfree(harray);
994 + return NULL;
995 +}
996 +
997 +static inline void harray_free(void *h)
998 +{
999 + struct harray *harray = (struct harray *) h;
1000 + size_t i;
1001 +
1002 + for (i = 0; harray->arrays[i] != NULL; i++)
1003 + kfree(harray->arrays[i]);
1004 + kfree(harray);
1005 +}
1006 +
1007 +static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
1008 +{
1009 + struct harray *harray = (struct harray *) h;
1010 + size_t i;
1011 +
1012 + for (i = 0; harray->arrays[i+1] != NULL; i++)
1013 + memset(harray->arrays[i], 0, harray->max_elements * typesize);
1014 + memset(harray->arrays[i], 0,
1015 + (hashsize - i * harray->max_elements) * typesize);
1016 +}
1017 +
1018 +#define HARRAY_ELEM(h, type, which) \
1019 +({ \
1020 + struct harray *__h = (struct harray *)(h); \
1021 + ((type)((__h)->arrays[(which)/(__h)->max_elements]) \
1022 + + (which)%(__h)->max_elements); \
1023 +})
1024 +
1025 +#endif /* __KERNEL__ */
1026 +
1027 +#endif /*_IP_SET_MALLOC_H*/
1028 --- /dev/null
1029 +++ b/include/linux/netfilter_ipv4/ip_set_nethash.h
1030 @@ -0,0 +1,55 @@
1031 +#ifndef __IP_SET_NETHASH_H
1032 +#define __IP_SET_NETHASH_H
1033 +
1034 +#include <linux/netfilter_ipv4/ip_set.h>
1035 +
1036 +#define SETTYPE_NAME "nethash"
1037 +#define MAX_RANGE 0x0000FFFF
1038 +
1039 +struct ip_set_nethash {
1040 + ip_set_ip_t *members; /* the nethash proper */
1041 + uint32_t elements; /* number of elements */
1042 + uint32_t hashsize; /* hash size */
1043 + uint16_t probes; /* max number of probes */
1044 + uint16_t resize; /* resize factor in percent */
1045 + unsigned char cidr[30]; /* CIDR sizes */
1046 + void *initval[0]; /* initvals for jhash_1word */
1047 +};
1048 +
1049 +struct ip_set_req_nethash_create {
1050 + uint32_t hashsize;
1051 + uint16_t probes;
1052 + uint16_t resize;
1053 +};
1054 +
1055 +struct ip_set_req_nethash {
1056 + ip_set_ip_t ip;
1057 + unsigned char cidr;
1058 +};
1059 +
1060 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
1061 +
1062 +static inline ip_set_ip_t
1063 +pack(ip_set_ip_t ip, unsigned char cidr)
1064 +{
1065 + ip_set_ip_t addr, *paddr = &addr;
1066 + unsigned char n, t, *a;
1067 +
1068 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
1069 +#ifdef __KERNEL__
1070 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
1071 +#endif
1072 + n = cidr / 8;
1073 + t = cidr % 8;
1074 + a = &((unsigned char *)paddr)[n];
1075 + *a = *a /(1 << (8 - t)) + shifts[t];
1076 +#ifdef __KERNEL__
1077 + DP("n: %u, t: %u, a: %u", n, t, *a);
1078 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
1079 + HIPQUAD(ip), cidr, NIPQUAD(addr));
1080 +#endif
1081 +
1082 + return ntohl(addr);
1083 +}
1084 +
1085 +#endif /* __IP_SET_NETHASH_H */
1086 --- /dev/null
1087 +++ b/include/linux/netfilter_ipv4/ip_set_portmap.h
1088 @@ -0,0 +1,25 @@
1089 +#ifndef __IP_SET_PORTMAP_H
1090 +#define __IP_SET_PORTMAP_H
1091 +
1092 +#include <linux/netfilter_ipv4/ip_set.h>
1093 +
1094 +#define SETTYPE_NAME "portmap"
1095 +#define MAX_RANGE 0x0000FFFF
1096 +#define INVALID_PORT (MAX_RANGE + 1)
1097 +
1098 +struct ip_set_portmap {
1099 + void *members; /* the portmap proper */
1100 + ip_set_ip_t first_port; /* host byte order, included in range */
1101 + ip_set_ip_t last_port; /* host byte order, included in range */
1102 +};
1103 +
1104 +struct ip_set_req_portmap_create {
1105 + ip_set_ip_t from;
1106 + ip_set_ip_t to;
1107 +};
1108 +
1109 +struct ip_set_req_portmap {
1110 + ip_set_ip_t port;
1111 +};
1112 +
1113 +#endif /* __IP_SET_PORTMAP_H */
1114 --- /dev/null
1115 +++ b/include/linux/netfilter_ipv4/ipt_set.h
1116 @@ -0,0 +1,21 @@
1117 +#ifndef _IPT_SET_H
1118 +#define _IPT_SET_H
1119 +
1120 +#include <linux/netfilter_ipv4/ip_set.h>
1121 +
1122 +struct ipt_set_info {
1123 + ip_set_id_t index;
1124 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1125 +};
1126 +
1127 +/* match info */
1128 +struct ipt_set_info_match {
1129 + struct ipt_set_info match_set;
1130 +};
1131 +
1132 +struct ipt_set_info_target {
1133 + struct ipt_set_info add_set;
1134 + struct ipt_set_info del_set;
1135 +};
1136 +
1137 +#endif /*_IPT_SET_H*/
1138 --- /dev/null
1139 +++ b/net/ipv4/netfilter/ip_set.c
1140 @@ -0,0 +1,2003 @@
1141 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1142 + * Patrick Schaaf <bof@bof.de>
1143 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1144 + *
1145 + * This program is free software; you can redistribute it and/or modify
1146 + * it under the terms of the GNU General Public License version 2 as
1147 + * published by the Free Software Foundation.
1148 + */
1149 +
1150 +/* Kernel module for IP set management */
1151 +
1152 +#include <linux/version.h>
1153 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
1154 +#include <linux/config.h>
1155 +#endif
1156 +#include <linux/module.h>
1157 +#include <linux/moduleparam.h>
1158 +#include <linux/kmod.h>
1159 +#include <linux/ip.h>
1160 +#include <linux/skbuff.h>
1161 +#include <linux/random.h>
1162 +#include <linux/jhash.h>
1163 +#include <linux/netfilter_ipv4/ip_tables.h>
1164 +#include <linux/errno.h>
1165 +#include <asm/uaccess.h>
1166 +#include <asm/bitops.h>
1167 +#include <asm/semaphore.h>
1168 +#include <linux/spinlock.h>
1169 +#include <linux/vmalloc.h>
1170 +
1171 +#define ASSERT_READ_LOCK(x)
1172 +#define ASSERT_WRITE_LOCK(x)
1173 +#include <linux/netfilter_ipv4/ip_set.h>
1174 +
1175 +static struct list_head set_type_list; /* all registered sets */
1176 +static struct ip_set **ip_set_list; /* all individual sets */
1177 +static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1178 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1179 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1180 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1181 +static struct list_head *ip_set_hash; /* hash of bindings */
1182 +static unsigned int ip_set_hash_random; /* random seed */
1183 +
1184 +/*
1185 + * Sets are identified either by the index in ip_set_list or by id.
1186 + * The id never changes and is used to find a key in the hash.
1187 + * The index may change by swapping and used at all other places
1188 + * (set/SET netfilter modules, binding value, etc.)
1189 + *
1190 + * Userspace requests are serialized by ip_set_mutex and sets can
1191 + * be deleted only from userspace. Therefore ip_set_list locking
1192 + * must obey the following rules:
1193 + *
1194 + * - kernel requests: read and write locking mandatory
1195 + * - user requests: read locking optional, write locking mandatory
1196 + */
1197 +
1198 +static inline void
1199 +__ip_set_get(ip_set_id_t index)
1200 +{
1201 + atomic_inc(&ip_set_list[index]->ref);
1202 +}
1203 +
1204 +static inline void
1205 +__ip_set_put(ip_set_id_t index)
1206 +{
1207 + atomic_dec(&ip_set_list[index]->ref);
1208 +}
1209 +
1210 +/*
1211 + * Binding routines
1212 + */
1213 +
1214 +static inline struct ip_set_hash *
1215 +__ip_set_find(u_int32_t key, ip_set_id_t id, ip_set_ip_t ip)
1216 +{
1217 + struct ip_set_hash *set_hash;
1218 +
1219 + list_for_each_entry(set_hash, &ip_set_hash[key], list)
1220 + if (set_hash->id == id && set_hash->ip == ip)
1221 + return set_hash;
1222 +
1223 + return NULL;
1224 +}
1225 +
1226 +static ip_set_id_t
1227 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1228 +{
1229 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1230 + % ip_set_bindings_hash_size;
1231 + struct ip_set_hash *set_hash;
1232 +
1233 + ASSERT_READ_LOCK(&ip_set_lock);
1234 + IP_SET_ASSERT(ip_set_list[id]);
1235 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1236 +
1237 + set_hash = __ip_set_find(key, id, ip);
1238 +
1239 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1240 + HIPQUAD(ip),
1241 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1242 +
1243 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1244 +}
1245 +
1246 +static inline void
1247 +__set_hash_del(struct ip_set_hash *set_hash)
1248 +{
1249 + ASSERT_WRITE_LOCK(&ip_set_lock);
1250 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1251 +
1252 + __ip_set_put(set_hash->binding);
1253 + list_del(&set_hash->list);
1254 + kfree(set_hash);
1255 +}
1256 +
1257 +static int
1258 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1259 +{
1260 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1261 + % ip_set_bindings_hash_size;
1262 + struct ip_set_hash *set_hash;
1263 +
1264 + IP_SET_ASSERT(ip_set_list[id]);
1265 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1266 + write_lock_bh(&ip_set_lock);
1267 + set_hash = __ip_set_find(key, id, ip);
1268 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1269 + HIPQUAD(ip),
1270 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1271 +
1272 + if (set_hash != NULL)
1273 + __set_hash_del(set_hash);
1274 + write_unlock_bh(&ip_set_lock);
1275 + return 0;
1276 +}
1277 +
1278 +static int
1279 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1280 +{
1281 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1282 + % ip_set_bindings_hash_size;
1283 + struct ip_set_hash *set_hash;
1284 + int ret = 0;
1285 +
1286 + IP_SET_ASSERT(ip_set_list[id]);
1287 + IP_SET_ASSERT(ip_set_list[binding]);
1288 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1289 + HIPQUAD(ip), ip_set_list[binding]->name);
1290 + write_lock_bh(&ip_set_lock);
1291 + set_hash = __ip_set_find(key, id, ip);
1292 + if (!set_hash) {
1293 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_ATOMIC);
1294 + if (!set_hash) {
1295 + ret = -ENOMEM;
1296 + goto unlock;
1297 + }
1298 + INIT_LIST_HEAD(&set_hash->list);
1299 + set_hash->id = id;
1300 + set_hash->ip = ip;
1301 + list_add(&set_hash->list, &ip_set_hash[key]);
1302 + } else {
1303 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1304 + DP("overwrite binding: %s",
1305 + ip_set_list[set_hash->binding]->name);
1306 + __ip_set_put(set_hash->binding);
1307 + }
1308 + set_hash->binding = binding;
1309 + __ip_set_get(set_hash->binding);
1310 + DP("stored: key %u, id %u (%s), ip %u.%u.%u.%u, binding %u (%s)",
1311 + key, id, ip_set_list[id]->name,
1312 + HIPQUAD(ip), binding, ip_set_list[binding]->name);
1313 + unlock:
1314 + write_unlock_bh(&ip_set_lock);
1315 + return ret;
1316 +}
1317 +
1318 +#define FOREACH_HASH_DO(fn, args...) \
1319 +({ \
1320 + ip_set_id_t __key; \
1321 + struct ip_set_hash *__set_hash; \
1322 + \
1323 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1324 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1325 + fn(__set_hash , ## args); \
1326 + } \
1327 +})
1328 +
1329 +#define FOREACH_HASH_RW_DO(fn, args...) \
1330 +({ \
1331 + ip_set_id_t __key; \
1332 + struct ip_set_hash *__set_hash, *__n; \
1333 + \
1334 + ASSERT_WRITE_LOCK(&ip_set_lock); \
1335 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1336 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1337 + fn(__set_hash , ## args); \
1338 + } \
1339 +})
1340 +
1341 +/* Add, del and test set entries from kernel */
1342 +
1343 +#define follow_bindings(index, set, ip) \
1344 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1345 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1346 +
1347 +int
1348 +ip_set_testip_kernel(ip_set_id_t index,
1349 + const struct sk_buff *skb,
1350 + const u_int32_t *flags)
1351 +{
1352 + struct ip_set *set;
1353 + ip_set_ip_t ip;
1354 + int res;
1355 + unsigned char i = 0;
1356 +
1357 + IP_SET_ASSERT(flags[i]);
1358 + read_lock_bh(&ip_set_lock);
1359 + do {
1360 + set = ip_set_list[index];
1361 + IP_SET_ASSERT(set);
1362 + DP("set %s, index %u", set->name, index);
1363 + read_lock_bh(&set->lock);
1364 + res = set->type->testip_kernel(set, skb, &ip, flags, i++);
1365 + read_unlock_bh(&set->lock);
1366 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1367 + } while (res > 0
1368 + && flags[i]
1369 + && follow_bindings(index, set, ip));
1370 + read_unlock_bh(&ip_set_lock);
1371 +
1372 + return res;
1373 +}
1374 +
1375 +void
1376 +ip_set_addip_kernel(ip_set_id_t index,
1377 + const struct sk_buff *skb,
1378 + const u_int32_t *flags)
1379 +{
1380 + struct ip_set *set;
1381 + ip_set_ip_t ip;
1382 + int res;
1383 + unsigned char i = 0;
1384 +
1385 + IP_SET_ASSERT(flags[i]);
1386 + retry:
1387 + read_lock_bh(&ip_set_lock);
1388 + do {
1389 + set = ip_set_list[index];
1390 + IP_SET_ASSERT(set);
1391 + DP("set %s, index %u", set->name, index);
1392 + write_lock_bh(&set->lock);
1393 + res = set->type->addip_kernel(set, skb, &ip, flags, i++);
1394 + write_unlock_bh(&set->lock);
1395 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1396 + } while ((res == 0 || res == -EEXIST)
1397 + && flags[i]
1398 + && follow_bindings(index, set, ip));
1399 + read_unlock_bh(&ip_set_lock);
1400 +
1401 + if (res == -EAGAIN
1402 + && set->type->retry
1403 + && (res = set->type->retry(set)) == 0)
1404 + goto retry;
1405 +}
1406 +
1407 +void
1408 +ip_set_delip_kernel(ip_set_id_t index,
1409 + const struct sk_buff *skb,
1410 + const u_int32_t *flags)
1411 +{
1412 + struct ip_set *set;
1413 + ip_set_ip_t ip;
1414 + int res;
1415 + unsigned char i = 0;
1416 +
1417 + IP_SET_ASSERT(flags[i]);
1418 + read_lock_bh(&ip_set_lock);
1419 + do {
1420 + set = ip_set_list[index];
1421 + IP_SET_ASSERT(set);
1422 + DP("set %s, index %u", set->name, index);
1423 + write_lock_bh(&set->lock);
1424 + res = set->type->delip_kernel(set, skb, &ip, flags, i++);
1425 + write_unlock_bh(&set->lock);
1426 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1427 + } while ((res == 0 || res == -EEXIST)
1428 + && flags[i]
1429 + && follow_bindings(index, set, ip));
1430 + read_unlock_bh(&ip_set_lock);
1431 +}
1432 +
1433 +/* Register and deregister settype */
1434 +
1435 +static inline struct ip_set_type *
1436 +find_set_type(const char *name)
1437 +{
1438 + struct ip_set_type *set_type;
1439 +
1440 + list_for_each_entry(set_type, &set_type_list, list)
1441 + if (!strncmp(set_type->typename, name, IP_SET_MAXNAMELEN - 1))
1442 + return set_type;
1443 + return NULL;
1444 +}
1445 +
1446 +int
1447 +ip_set_register_set_type(struct ip_set_type *set_type)
1448 +{
1449 + int ret = 0;
1450 +
1451 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1452 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1453 + set_type->typename,
1454 + set_type->protocol_version,
1455 + IP_SET_PROTOCOL_VERSION);
1456 + return -EINVAL;
1457 + }
1458 +
1459 + write_lock_bh(&ip_set_lock);
1460 + if (find_set_type(set_type->typename)) {
1461 + /* Duplicate! */
1462 + ip_set_printk("'%s' already registered!",
1463 + set_type->typename);
1464 + ret = -EINVAL;
1465 + goto unlock;
1466 + }
1467 + if (!try_module_get(THIS_MODULE)) {
1468 + ret = -EFAULT;
1469 + goto unlock;
1470 + }
1471 + list_add(&set_type->list, &set_type_list);
1472 + DP("'%s' registered.", set_type->typename);
1473 + unlock:
1474 + write_unlock_bh(&ip_set_lock);
1475 + return ret;
1476 +}
1477 +
1478 +void
1479 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1480 +{
1481 + write_lock_bh(&ip_set_lock);
1482 + if (!find_set_type(set_type->typename)) {
1483 + ip_set_printk("'%s' not registered?",
1484 + set_type->typename);
1485 + goto unlock;
1486 + }
1487 + list_del(&set_type->list);
1488 + module_put(THIS_MODULE);
1489 + DP("'%s' unregistered.", set_type->typename);
1490 + unlock:
1491 + write_unlock_bh(&ip_set_lock);
1492 +
1493 +}
1494 +
1495 +/*
1496 + * Userspace routines
1497 + */
1498 +
1499 +/*
1500 + * Find set by name, reference it once. The reference makes sure the
1501 + * thing pointed to, does not go away under our feet. Drop the reference
1502 + * later, using ip_set_put().
1503 + */
1504 +ip_set_id_t
1505 +ip_set_get_byname(const char *name)
1506 +{
1507 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1508 +
1509 + down(&ip_set_app_mutex);
1510 + for (i = 0; i < ip_set_max; i++) {
1511 + if (ip_set_list[i] != NULL
1512 + && strcmp(ip_set_list[i]->name, name) == 0) {
1513 + __ip_set_get(i);
1514 + index = i;
1515 + break;
1516 + }
1517 + }
1518 + up(&ip_set_app_mutex);
1519 + return index;
1520 +}
1521 +
1522 +/*
1523 + * Find set by index, reference it once. The reference makes sure the
1524 + * thing pointed to, does not go away under our feet. Drop the reference
1525 + * later, using ip_set_put().
1526 + */
1527 +ip_set_id_t
1528 +ip_set_get_byindex(ip_set_id_t index)
1529 +{
1530 + down(&ip_set_app_mutex);
1531 +
1532 + if (index >= ip_set_max)
1533 + return IP_SET_INVALID_ID;
1534 +
1535 + if (ip_set_list[index])
1536 + __ip_set_get(index);
1537 + else
1538 + index = IP_SET_INVALID_ID;
1539 +
1540 + up(&ip_set_app_mutex);
1541 + return index;
1542 +}
1543 +
1544 +/*
1545 + * If the given set pointer points to a valid set, decrement
1546 + * reference count by 1. The caller shall not assume the index
1547 + * to be valid, after calling this function.
1548 + */
1549 +void ip_set_put(ip_set_id_t index)
1550 +{
1551 + down(&ip_set_app_mutex);
1552 + if (ip_set_list[index])
1553 + __ip_set_put(index);
1554 + up(&ip_set_app_mutex);
1555 +}
1556 +
1557 +/* Find a set by name or index */
1558 +static ip_set_id_t
1559 +ip_set_find_byname(const char *name)
1560 +{
1561 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1562 +
1563 + for (i = 0; i < ip_set_max; i++) {
1564 + if (ip_set_list[i] != NULL
1565 + && strcmp(ip_set_list[i]->name, name) == 0) {
1566 + index = i;
1567 + break;
1568 + }
1569 + }
1570 + return index;
1571 +}
1572 +
1573 +static ip_set_id_t
1574 +ip_set_find_byindex(ip_set_id_t index)
1575 +{
1576 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1577 + index = IP_SET_INVALID_ID;
1578 +
1579 + return index;
1580 +}
1581 +
1582 +/*
1583 + * Add, del, test, bind and unbind
1584 + */
1585 +
1586 +static inline int
1587 +__ip_set_testip(struct ip_set *set,
1588 + const void *data,
1589 + size_t size,
1590 + ip_set_ip_t *ip)
1591 +{
1592 + int res;
1593 +
1594 + read_lock_bh(&set->lock);
1595 + res = set->type->testip(set, data, size, ip);
1596 + read_unlock_bh(&set->lock);
1597 +
1598 + return res;
1599 +}
1600 +
1601 +static int
1602 +__ip_set_addip(ip_set_id_t index,
1603 + const void *data,
1604 + size_t size)
1605 +{
1606 + struct ip_set *set = ip_set_list[index];
1607 + ip_set_ip_t ip;
1608 + int res;
1609 +
1610 + IP_SET_ASSERT(set);
1611 + do {
1612 + write_lock_bh(&set->lock);
1613 + res = set->type->addip(set, data, size, &ip);
1614 + write_unlock_bh(&set->lock);
1615 + } while (res == -EAGAIN
1616 + && set->type->retry
1617 + && (res = set->type->retry(set)) == 0);
1618 +
1619 + return res;
1620 +}
1621 +
1622 +static int
1623 +ip_set_addip(ip_set_id_t index,
1624 + const void *data,
1625 + size_t size)
1626 +{
1627 +
1628 + return __ip_set_addip(index,
1629 + data + sizeof(struct ip_set_req_adt),
1630 + size - sizeof(struct ip_set_req_adt));
1631 +}
1632 +
1633 +static int
1634 +ip_set_delip(ip_set_id_t index,
1635 + const void *data,
1636 + size_t size)
1637 +{
1638 + struct ip_set *set = ip_set_list[index];
1639 + ip_set_ip_t ip;
1640 + int res;
1641 +
1642 + IP_SET_ASSERT(set);
1643 + write_lock_bh(&set->lock);
1644 + res = set->type->delip(set,
1645 + data + sizeof(struct ip_set_req_adt),
1646 + size - sizeof(struct ip_set_req_adt),
1647 + &ip);
1648 + write_unlock_bh(&set->lock);
1649 +
1650 + return res;
1651 +}
1652 +
1653 +static int
1654 +ip_set_testip(ip_set_id_t index,
1655 + const void *data,
1656 + size_t size)
1657 +{
1658 + struct ip_set *set = ip_set_list[index];
1659 + ip_set_ip_t ip;
1660 + int res;
1661 +
1662 + IP_SET_ASSERT(set);
1663 + res = __ip_set_testip(set,
1664 + data + sizeof(struct ip_set_req_adt),
1665 + size - sizeof(struct ip_set_req_adt),
1666 + &ip);
1667 +
1668 + return (res > 0 ? -EEXIST : res);
1669 +}
1670 +
1671 +static int
1672 +ip_set_bindip(ip_set_id_t index,
1673 + const void *data,
1674 + size_t size)
1675 +{
1676 + struct ip_set *set = ip_set_list[index];
1677 + struct ip_set_req_bind *req_bind;
1678 + ip_set_id_t binding;
1679 + ip_set_ip_t ip;
1680 + int res;
1681 +
1682 + IP_SET_ASSERT(set);
1683 + if (size < sizeof(struct ip_set_req_bind))
1684 + return -EINVAL;
1685 +
1686 + req_bind = (struct ip_set_req_bind *) data;
1687 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1688 +
1689 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1690 + /* Default binding of a set */
1691 + char *binding_name;
1692 +
1693 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1694 + return -EINVAL;
1695 +
1696 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1697 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1698 +
1699 + binding = ip_set_find_byname(binding_name);
1700 + if (binding == IP_SET_INVALID_ID)
1701 + return -ENOENT;
1702 +
1703 + write_lock_bh(&ip_set_lock);
1704 + /* Sets as binding values are referenced */
1705 + if (set->binding != IP_SET_INVALID_ID)
1706 + __ip_set_put(set->binding);
1707 + set->binding = binding;
1708 + __ip_set_get(set->binding);
1709 + write_unlock_bh(&ip_set_lock);
1710 +
1711 + return 0;
1712 + }
1713 + binding = ip_set_find_byname(req_bind->binding);
1714 + if (binding == IP_SET_INVALID_ID)
1715 + return -ENOENT;
1716 +
1717 + res = __ip_set_testip(set,
1718 + data + sizeof(struct ip_set_req_bind),
1719 + size - sizeof(struct ip_set_req_bind),
1720 + &ip);
1721 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1722 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1723 +
1724 + if (res >= 0)
1725 + res = ip_set_hash_add(set->id, ip, binding);
1726 +
1727 + return res;
1728 +}
1729 +
1730 +#define FOREACH_SET_DO(fn, args...) \
1731 +({ \
1732 + ip_set_id_t __i; \
1733 + struct ip_set *__set; \
1734 + \
1735 + for (__i = 0; __i < ip_set_max; __i++) { \
1736 + __set = ip_set_list[__i]; \
1737 + if (__set != NULL) \
1738 + fn(__set , ##args); \
1739 + } \
1740 +})
1741 +
1742 +static inline void
1743 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1744 +{
1745 + if (set_hash->id == id)
1746 + __set_hash_del(set_hash);
1747 +}
1748 +
1749 +static inline void
1750 +__unbind_default(struct ip_set *set)
1751 +{
1752 + if (set->binding != IP_SET_INVALID_ID) {
1753 + /* Sets as binding values are referenced */
1754 + __ip_set_put(set->binding);
1755 + set->binding = IP_SET_INVALID_ID;
1756 + }
1757 +}
1758 +
1759 +static int
1760 +ip_set_unbindip(ip_set_id_t index,
1761 + const void *data,
1762 + size_t size)
1763 +{
1764 + struct ip_set *set;
1765 + struct ip_set_req_bind *req_bind;
1766 + ip_set_ip_t ip;
1767 + int res;
1768 +
1769 + DP("");
1770 + if (size < sizeof(struct ip_set_req_bind))
1771 + return -EINVAL;
1772 +
1773 + req_bind = (struct ip_set_req_bind *) data;
1774 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1775 +
1776 + DP("%u %s", index, req_bind->binding);
1777 + if (index == IP_SET_INVALID_ID) {
1778 + /* unbind :all: */
1779 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1780 + /* Default binding of sets */
1781 + write_lock_bh(&ip_set_lock);
1782 + FOREACH_SET_DO(__unbind_default);
1783 + write_unlock_bh(&ip_set_lock);
1784 + return 0;
1785 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1786 + /* Flush all bindings of all sets*/
1787 + write_lock_bh(&ip_set_lock);
1788 + FOREACH_HASH_RW_DO(__set_hash_del);
1789 + write_unlock_bh(&ip_set_lock);
1790 + return 0;
1791 + }
1792 + DP("unreachable reached!");
1793 + return -EINVAL;
1794 + }
1795 +
1796 + set = ip_set_list[index];
1797 + IP_SET_ASSERT(set);
1798 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1799 + /* Default binding of set */
1800 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1801 +
1802 + if (binding == IP_SET_INVALID_ID)
1803 + return -ENOENT;
1804 +
1805 + write_lock_bh(&ip_set_lock);
1806 + /* Sets in hash values are referenced */
1807 + __ip_set_put(set->binding);
1808 + set->binding = IP_SET_INVALID_ID;
1809 + write_unlock_bh(&ip_set_lock);
1810 +
1811 + return 0;
1812 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1813 + /* Flush all bindings */
1814 +
1815 + write_lock_bh(&ip_set_lock);
1816 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1817 + write_unlock_bh(&ip_set_lock);
1818 + return 0;
1819 + }
1820 +
1821 + res = __ip_set_testip(set,
1822 + data + sizeof(struct ip_set_req_bind),
1823 + size - sizeof(struct ip_set_req_bind),
1824 + &ip);
1825 +
1826 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1827 + if (res >= 0)
1828 + res = ip_set_hash_del(set->id, ip);
1829 +
1830 + return res;
1831 +}
1832 +
1833 +static int
1834 +ip_set_testbind(ip_set_id_t index,
1835 + const void *data,
1836 + size_t size)
1837 +{
1838 + struct ip_set *set = ip_set_list[index];
1839 + struct ip_set_req_bind *req_bind;
1840 + ip_set_id_t binding;
1841 + ip_set_ip_t ip;
1842 + int res;
1843 +
1844 + IP_SET_ASSERT(set);
1845 + if (size < sizeof(struct ip_set_req_bind))
1846 + return -EINVAL;
1847 +
1848 + req_bind = (struct ip_set_req_bind *) data;
1849 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1850 +
1851 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1852 + /* Default binding of set */
1853 + char *binding_name;
1854 +
1855 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1856 + return -EINVAL;
1857 +
1858 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1859 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1860 +
1861 + binding = ip_set_find_byname(binding_name);
1862 + if (binding == IP_SET_INVALID_ID)
1863 + return -ENOENT;
1864 +
1865 + res = (set->binding == binding) ? -EEXIST : 0;
1866 +
1867 + return res;
1868 + }
1869 + binding = ip_set_find_byname(req_bind->binding);
1870 + if (binding == IP_SET_INVALID_ID)
1871 + return -ENOENT;
1872 +
1873 +
1874 + res = __ip_set_testip(set,
1875 + data + sizeof(struct ip_set_req_bind),
1876 + size - sizeof(struct ip_set_req_bind),
1877 + &ip);
1878 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1879 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1880 +
1881 + if (res >= 0)
1882 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1883 + ? -EEXIST : 0;
1884 +
1885 + return res;
1886 +}
1887 +
1888 +static struct ip_set_type *
1889 +find_set_type_rlock(const char *typename)
1890 +{
1891 + struct ip_set_type *type;
1892 +
1893 + read_lock_bh(&ip_set_lock);
1894 + type = find_set_type(typename);
1895 + if (type == NULL)
1896 + read_unlock_bh(&ip_set_lock);
1897 +
1898 + return type;
1899 +}
1900 +
1901 +static int
1902 +find_free_id(const char *name,
1903 + ip_set_id_t *index,
1904 + ip_set_id_t *id)
1905 +{
1906 + ip_set_id_t i;
1907 +
1908 + *id = IP_SET_INVALID_ID;
1909 + for (i = 0; i < ip_set_max; i++) {
1910 + if (ip_set_list[i] == NULL) {
1911 + if (*id == IP_SET_INVALID_ID)
1912 + *id = *index = i;
1913 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1914 + /* Name clash */
1915 + return -EEXIST;
1916 + }
1917 + if (*id == IP_SET_INVALID_ID)
1918 + /* No free slot remained */
1919 + return -ERANGE;
1920 + /* Check that index is usable as id (swapping) */
1921 + check:
1922 + for (i = 0; i < ip_set_max; i++) {
1923 + if (ip_set_list[i] != NULL
1924 + && ip_set_list[i]->id == *id) {
1925 + *id = i;
1926 + goto check;
1927 + }
1928 + }
1929 + return 0;
1930 +}
1931 +
1932 +/*
1933 + * Create a set
1934 + */
1935 +static int
1936 +ip_set_create(const char *name,
1937 + const char *typename,
1938 + ip_set_id_t restore,
1939 + const void *data,
1940 + size_t size)
1941 +{
1942 + struct ip_set *set;
1943 + ip_set_id_t index = 0, id;
1944 + int res = 0;
1945 +
1946 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1947 + /*
1948 + * First, and without any locks, allocate and initialize
1949 + * a normal base set structure.
1950 + */
1951 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1952 + if (!set)
1953 + return -ENOMEM;
1954 + set->lock = RW_LOCK_UNLOCKED;
1955 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1956 + set->binding = IP_SET_INVALID_ID;
1957 + atomic_set(&set->ref, 0);
1958 +
1959 + /*
1960 + * Next, take the &ip_set_lock, check that we know the type,
1961 + * and take a reference on the type, to make sure it
1962 + * stays available while constructing our new set.
1963 + *
1964 + * After referencing the type, we drop the &ip_set_lock,
1965 + * and let the new set construction run without locks.
1966 + */
1967 + set->type = find_set_type_rlock(typename);
1968 + if (set->type == NULL) {
1969 + /* Try loading the module */
1970 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1971 + strcpy(modulename, "ip_set_");
1972 + strcat(modulename, typename);
1973 + DP("try to load %s", modulename);
1974 + request_module(modulename);
1975 + set->type = find_set_type_rlock(typename);
1976 + }
1977 + if (set->type == NULL) {
1978 + ip_set_printk("no set type '%s', set '%s' not created",
1979 + typename, name);
1980 + res = -ENOENT;
1981 + goto out;
1982 + }
1983 + if (!try_module_get(set->type->me)) {
1984 + read_unlock_bh(&ip_set_lock);
1985 + res = -EFAULT;
1986 + goto out;
1987 + }
1988 + read_unlock_bh(&ip_set_lock);
1989 +
1990 + /*
1991 + * Without holding any locks, create private part.
1992 + */
1993 + res = set->type->create(set, data, size);
1994 + if (res != 0)
1995 + goto put_out;
1996 +
1997 + /* BTW, res==0 here. */
1998 +
1999 + /*
2000 + * Here, we have a valid, constructed set. &ip_set_lock again,
2001 + * find free id/index and check that it is not already in
2002 + * ip_set_list.
2003 + */
2004 + write_lock_bh(&ip_set_lock);
2005 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
2006 + DP("no free id!");
2007 + goto cleanup;
2008 + }
2009 +
2010 + /* Make sure restore gets the same index */
2011 + if (restore != IP_SET_INVALID_ID && index != restore) {
2012 + DP("Can't restore, sets are screwed up");
2013 + res = -ERANGE;
2014 + goto cleanup;
2015 + }
2016 +
2017 + /*
2018 + * Finally! Add our shiny new set to the list, and be done.
2019 + */
2020 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2021 + set->id = id;
2022 + ip_set_list[index] = set;
2023 + write_unlock_bh(&ip_set_lock);
2024 + return res;
2025 +
2026 + cleanup:
2027 + write_unlock_bh(&ip_set_lock);
2028 + set->type->destroy(set);
2029 + put_out:
2030 + module_put(set->type->me);
2031 + out:
2032 + kfree(set);
2033 + return res;
2034 +}
2035 +
2036 +/*
2037 + * Destroy a given existing set
2038 + */
2039 +static void
2040 +ip_set_destroy_set(ip_set_id_t index)
2041 +{
2042 + struct ip_set *set = ip_set_list[index];
2043 +
2044 + IP_SET_ASSERT(set);
2045 + DP("set: %s", set->name);
2046 + write_lock_bh(&ip_set_lock);
2047 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2048 + if (set->binding != IP_SET_INVALID_ID)
2049 + __ip_set_put(set->binding);
2050 + ip_set_list[index] = NULL;
2051 + write_unlock_bh(&ip_set_lock);
2052 +
2053 + /* Must call it without holding any lock */
2054 + set->type->destroy(set);
2055 + module_put(set->type->me);
2056 + kfree(set);
2057 +}
2058 +
2059 +/*
2060 + * Destroy a set - or all sets
2061 + * Sets must not be referenced/used.
2062 + */
2063 +static int
2064 +ip_set_destroy(ip_set_id_t index)
2065 +{
2066 + ip_set_id_t i;
2067 +
2068 + /* ref modification always protected by the mutex */
2069 + if (index != IP_SET_INVALID_ID) {
2070 + if (atomic_read(&ip_set_list[index]->ref))
2071 + return -EBUSY;
2072 + ip_set_destroy_set(index);
2073 + } else {
2074 + for (i = 0; i < ip_set_max; i++) {
2075 + if (ip_set_list[i] != NULL
2076 + && (atomic_read(&ip_set_list[i]->ref)))
2077 + return -EBUSY;
2078 + }
2079 +
2080 + for (i = 0; i < ip_set_max; i++) {
2081 + if (ip_set_list[i] != NULL)
2082 + ip_set_destroy_set(i);
2083 + }
2084 + }
2085 + return 0;
2086 +}
2087 +
2088 +static void
2089 +ip_set_flush_set(struct ip_set *set)
2090 +{
2091 + DP("set: %s %u", set->name, set->id);
2092 +
2093 + write_lock_bh(&set->lock);
2094 + set->type->flush(set);
2095 + write_unlock_bh(&set->lock);
2096 +}
2097 +
2098 +/*
2099 + * Flush data in a set - or in all sets
2100 + */
2101 +static int
2102 +ip_set_flush(ip_set_id_t index)
2103 +{
2104 + if (index != IP_SET_INVALID_ID) {
2105 + IP_SET_ASSERT(ip_set_list[index]);
2106 + ip_set_flush_set(ip_set_list[index]);
2107 + } else
2108 + FOREACH_SET_DO(ip_set_flush_set);
2109 +
2110 + return 0;
2111 +}
2112 +
2113 +/* Rename a set */
2114 +static int
2115 +ip_set_rename(ip_set_id_t index, const char *name)
2116 +{
2117 + struct ip_set *set = ip_set_list[index];
2118 + ip_set_id_t i;
2119 + int res = 0;
2120 +
2121 + DP("set: %s to %s", set->name, name);
2122 + write_lock_bh(&ip_set_lock);
2123 + for (i = 0; i < ip_set_max; i++) {
2124 + if (ip_set_list[i] != NULL
2125 + && strncmp(ip_set_list[i]->name,
2126 + name,
2127 + IP_SET_MAXNAMELEN - 1) == 0) {
2128 + res = -EEXIST;
2129 + goto unlock;
2130 + }
2131 + }
2132 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2133 + unlock:
2134 + write_unlock_bh(&ip_set_lock);
2135 + return res;
2136 +}
2137 +
2138 +/*
2139 + * Swap two sets so that name/index points to the other.
2140 + * References are also swapped.
2141 + */
2142 +static int
2143 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2144 +{
2145 + struct ip_set *from = ip_set_list[from_index];
2146 + struct ip_set *to = ip_set_list[to_index];
2147 + char from_name[IP_SET_MAXNAMELEN];
2148 + u_int32_t from_ref;
2149 +
2150 + DP("set: %s to %s", from->name, to->name);
2151 + /* Features must not change. Artifical restriction. */
2152 + if (from->type->features != to->type->features)
2153 + return -ENOEXEC;
2154 +
2155 + /* No magic here: ref munging protected by the mutex */
2156 + write_lock_bh(&ip_set_lock);
2157 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2158 + from_ref = atomic_read(&from->ref);
2159 +
2160 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2161 + atomic_set(&from->ref, atomic_read(&to->ref));
2162 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2163 + atomic_set(&to->ref, from_ref);
2164 +
2165 + ip_set_list[from_index] = to;
2166 + ip_set_list[to_index] = from;
2167 +
2168 + write_unlock_bh(&ip_set_lock);
2169 + return 0;
2170 +}
2171 +
2172 +/*
2173 + * List set data
2174 + */
2175 +
2176 +static inline void
2177 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2178 + ip_set_id_t id, size_t *size)
2179 +{
2180 + if (set_hash->id == id)
2181 + *size += sizeof(struct ip_set_hash_list);
2182 +}
2183 +
2184 +static inline void
2185 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2186 + ip_set_id_t id, size_t *size)
2187 +{
2188 + if (set_hash->id == id)
2189 + *size += sizeof(struct ip_set_hash_save);
2190 +}
2191 +
2192 +static inline void
2193 +__set_hash_bindings(struct ip_set_hash *set_hash,
2194 + ip_set_id_t id, void *data, int *used)
2195 +{
2196 + if (set_hash->id == id) {
2197 + struct ip_set_hash_list *hash_list =
2198 + (struct ip_set_hash_list *)(data + *used);
2199 +
2200 + hash_list->ip = set_hash->ip;
2201 + hash_list->binding = set_hash->binding;
2202 + *used += sizeof(struct ip_set_hash_list);
2203 + }
2204 +}
2205 +
2206 +static int ip_set_list_set(ip_set_id_t index,
2207 + void *data,
2208 + int *used,
2209 + int len)
2210 +{
2211 + struct ip_set *set = ip_set_list[index];
2212 + struct ip_set_list *set_list;
2213 +
2214 + /* Pointer to our header */
2215 + set_list = (struct ip_set_list *) (data + *used);
2216 +
2217 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2218 +
2219 + /* Get and ensure header size */
2220 + if (*used + sizeof(struct ip_set_list) > len)
2221 + goto not_enough_mem;
2222 + *used += sizeof(struct ip_set_list);
2223 +
2224 + read_lock_bh(&set->lock);
2225 + /* Get and ensure set specific header size */
2226 + set_list->header_size = set->type->header_size;
2227 + if (*used + set_list->header_size > len)
2228 + goto unlock_set;
2229 +
2230 + /* Fill in the header */
2231 + set_list->index = index;
2232 + set_list->binding = set->binding;
2233 + set_list->ref = atomic_read(&set->ref);
2234 +
2235 + /* Fill in set spefific header data */
2236 + set->type->list_header(set, data + *used);
2237 + *used += set_list->header_size;
2238 +
2239 + /* Get and ensure set specific members size */
2240 + set_list->members_size = set->type->list_members_size(set);
2241 + if (*used + set_list->members_size > len)
2242 + goto unlock_set;
2243 +
2244 + /* Fill in set spefific members data */
2245 + set->type->list_members(set, data + *used);
2246 + *used += set_list->members_size;
2247 + read_unlock_bh(&set->lock);
2248 +
2249 + /* Bindings */
2250 +
2251 + /* Get and ensure set specific bindings size */
2252 + set_list->bindings_size = 0;
2253 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2254 + set->id, &set_list->bindings_size);
2255 + if (*used + set_list->bindings_size > len)
2256 + goto not_enough_mem;
2257 +
2258 + /* Fill in set spefific bindings data */
2259 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2260 +
2261 + return 0;
2262 +
2263 + unlock_set:
2264 + read_unlock_bh(&set->lock);
2265 + not_enough_mem:
2266 + DP("not enough mem, try again");
2267 + return -EAGAIN;
2268 +}
2269 +
2270 +/*
2271 + * Save sets
2272 + */
2273 +static int ip_set_save_set(ip_set_id_t index,
2274 + void *data,
2275 + int *used,
2276 + int len)
2277 +{
2278 + struct ip_set *set;
2279 + struct ip_set_save *set_save;
2280 +
2281 + /* Pointer to our header */
2282 + set_save = (struct ip_set_save *) (data + *used);
2283 +
2284 + /* Get and ensure header size */
2285 + if (*used + sizeof(struct ip_set_save) > len)
2286 + goto not_enough_mem;
2287 + *used += sizeof(struct ip_set_save);
2288 +
2289 + set = ip_set_list[index];
2290 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2291 + data, data + *used);
2292 +
2293 + read_lock_bh(&set->lock);
2294 + /* Get and ensure set specific header size */
2295 + set_save->header_size = set->type->header_size;
2296 + if (*used + set_save->header_size > len)
2297 + goto unlock_set;
2298 +
2299 + /* Fill in the header */
2300 + set_save->index = index;
2301 + set_save->binding = set->binding;
2302 +
2303 + /* Fill in set spefific header data */
2304 + set->type->list_header(set, data + *used);
2305 + *used += set_save->header_size;
2306 +
2307 + DP("set header filled: %s, used: %u(%u) %p %p", set->name, *used,
2308 + set_save->header_size, data, data + *used);
2309 + /* Get and ensure set specific members size */
2310 + set_save->members_size = set->type->list_members_size(set);
2311 + if (*used + set_save->members_size > len)
2312 + goto unlock_set;
2313 +
2314 + /* Fill in set spefific members data */
2315 + set->type->list_members(set, data + *used);
2316 + *used += set_save->members_size;
2317 + read_unlock_bh(&set->lock);
2318 + DP("set members filled: %s, used: %u(%u) %p %p", set->name, *used,
2319 + set_save->members_size, data, data + *used);
2320 + return 0;
2321 +
2322 + unlock_set:
2323 + read_unlock_bh(&set->lock);
2324 + not_enough_mem:
2325 + DP("not enough mem, try again");
2326 + return -EAGAIN;
2327 +}
2328 +
2329 +static inline void
2330 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2331 + ip_set_id_t id,
2332 + void *data,
2333 + int *used,
2334 + int len,
2335 + int *res)
2336 +{
2337 + if (*res == 0
2338 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2339 + struct ip_set_hash_save *hash_save =
2340 + (struct ip_set_hash_save *)(data + *used);
2341 + /* Ensure bindings size */
2342 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2343 + *res = -ENOMEM;
2344 + return;
2345 + }
2346 + hash_save->id = set_hash->id;
2347 + hash_save->ip = set_hash->ip;
2348 + hash_save->binding = set_hash->binding;
2349 + *used += sizeof(struct ip_set_hash_save);
2350 + }
2351 +}
2352 +
2353 +static int ip_set_save_bindings(ip_set_id_t index,
2354 + void *data,
2355 + int *used,
2356 + int len)
2357 +{
2358 + int res = 0;
2359 + struct ip_set_save *set_save;
2360 +
2361 + DP("used %u, len %u", *used, len);
2362 + /* Get and ensure header size */
2363 + if (*used + sizeof(struct ip_set_save) > len)
2364 + return -ENOMEM;
2365 +
2366 + /* Marker */
2367 + set_save = (struct ip_set_save *) (data + *used);
2368 + set_save->index = IP_SET_INVALID_ID;
2369 + set_save->header_size = 0;
2370 + set_save->members_size = 0;
2371 + *used += sizeof(struct ip_set_save);
2372 +
2373 + DP("marker added used %u, len %u", *used, len);
2374 + /* Fill in bindings data */
2375 + if (index != IP_SET_INVALID_ID)
2376 + /* Sets are identified by id in hash */
2377 + index = ip_set_list[index]->id;
2378 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2379 +
2380 + return res;
2381 +}
2382 +
2383 +/*
2384 + * Restore sets
2385 + */
2386 +static int ip_set_restore(void *data,
2387 + int len)
2388 +{
2389 + int res = 0;
2390 + int line = 0, used = 0, members_size;
2391 + struct ip_set *set;
2392 + struct ip_set_hash_save *hash_save;
2393 + struct ip_set_restore *set_restore;
2394 + ip_set_id_t index;
2395 +
2396 + /* Loop to restore sets */
2397 + while (1) {
2398 + line++;
2399 +
2400 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2401 + /* Get and ensure header size */
2402 + if (used + sizeof(struct ip_set_restore) > len)
2403 + return line;
2404 + set_restore = (struct ip_set_restore *) (data + used);
2405 + used += sizeof(struct ip_set_restore);
2406 +
2407 + /* Ensure data size */
2408 + if (used
2409 + + set_restore->header_size
2410 + + set_restore->members_size > len)
2411 + return line;
2412 +
2413 + /* Check marker */
2414 + if (set_restore->index == IP_SET_INVALID_ID) {
2415 + line--;
2416 + goto bindings;
2417 + }
2418 +
2419 + /* Try to create the set */
2420 + DP("restore %s %s", set_restore->name, set_restore->typename);
2421 + res = ip_set_create(set_restore->name,
2422 + set_restore->typename,
2423 + set_restore->index,
2424 + data + used,
2425 + set_restore->header_size);
2426 +
2427 + if (res != 0)
2428 + return line;
2429 + used += set_restore->header_size;
2430 +
2431 + index = ip_set_find_byindex(set_restore->index);
2432 + DP("index %u, restore_index %u", index, set_restore->index);
2433 + if (index != set_restore->index)
2434 + return line;
2435 + /* Try to restore members data */
2436 + set = ip_set_list[index];
2437 + members_size = 0;
2438 + DP("members_size %u reqsize %u",
2439 + set_restore->members_size, set->type->reqsize);
2440 + while (members_size + set->type->reqsize <=
2441 + set_restore->members_size) {
2442 + line++;
2443 + DP("members: %u, line %u", members_size, line);
2444 + res = __ip_set_addip(index,
2445 + data + used + members_size,
2446 + set->type->reqsize);
2447 + if (!(res == 0 || res == -EEXIST))
2448 + return line;
2449 + members_size += set->type->reqsize;
2450 + }
2451 +
2452 + DP("members_size %u %u",
2453 + set_restore->members_size, members_size);
2454 + if (members_size != set_restore->members_size)
2455 + return line++;
2456 + used += set_restore->members_size;
2457 + }
2458 +
2459 + bindings:
2460 + /* Loop to restore bindings */
2461 + while (used < len) {
2462 + line++;
2463 +
2464 + DP("restore binding, line %u", line);
2465 + /* Get and ensure size */
2466 + if (used + sizeof(struct ip_set_hash_save) > len)
2467 + return line;
2468 + hash_save = (struct ip_set_hash_save *) (data + used);
2469 + used += sizeof(struct ip_set_hash_save);
2470 +
2471 + /* hash_save->id is used to store the index */
2472 + index = ip_set_find_byindex(hash_save->id);
2473 + DP("restore binding index %u, id %u, %u -> %u",
2474 + index, hash_save->id, hash_save->ip, hash_save->binding);
2475 + if (index != hash_save->id)
2476 + return line;
2477 + if (ip_set_find_byindex(hash_save->binding) == IP_SET_INVALID_ID) {
2478 + DP("corrupt binding set index %u", hash_save->binding);
2479 + return line;
2480 + }
2481 + set = ip_set_list[hash_save->id];
2482 + /* Null valued IP means default binding */
2483 + if (hash_save->ip)
2484 + res = ip_set_hash_add(set->id,
2485 + hash_save->ip,
2486 + hash_save->binding);
2487 + else {
2488 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2489 + write_lock_bh(&ip_set_lock);
2490 + set->binding = hash_save->binding;
2491 + __ip_set_get(set->binding);
2492 + write_unlock_bh(&ip_set_lock);
2493 + DP("default binding: %u", set->binding);
2494 + }
2495 + if (res != 0)
2496 + return line;
2497 + }
2498 + if (used != len)
2499 + return line;
2500 +
2501 + return 0;
2502 +}
2503 +
2504 +static int
2505 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2506 +{
2507 + void *data;
2508 + int res = 0; /* Assume OK */
2509 + unsigned *op;
2510 + struct ip_set_req_adt *req_adt;
2511 + ip_set_id_t index = IP_SET_INVALID_ID;
2512 + int (*adtfn)(ip_set_id_t index,
2513 + const void *data, size_t size);
2514 + struct fn_table {
2515 + int (*fn)(ip_set_id_t index,
2516 + const void *data, size_t size);
2517 + } adtfn_table[] =
2518 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2519 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2520 + };
2521 +
2522 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2523 + if (!capable(CAP_NET_ADMIN))
2524 + return -EPERM;
2525 + if (optval != SO_IP_SET)
2526 + return -EBADF;
2527 + if (len <= sizeof(unsigned)) {
2528 + ip_set_printk("short userdata (want >%zu, got %u)",
2529 + sizeof(unsigned), len);
2530 + return -EINVAL;
2531 + }
2532 + data = vmalloc(len);
2533 + if (!data) {
2534 + DP("out of mem for %u bytes", len);
2535 + return -ENOMEM;
2536 + }
2537 + if (copy_from_user(data, user, len) != 0) {
2538 + res = -EFAULT;
2539 + goto done;
2540 + }
2541 + if (down_interruptible(&ip_set_app_mutex)) {
2542 + res = -EINTR;
2543 + goto done;
2544 + }
2545 +
2546 + op = (unsigned *)data;
2547 + DP("op=%x", *op);
2548 +
2549 + if (*op < IP_SET_OP_VERSION) {
2550 + /* Check the version at the beginning of operations */
2551 + struct ip_set_req_version *req_version =
2552 + (struct ip_set_req_version *) data;
2553 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2554 + res = -EPROTO;
2555 + goto done;
2556 + }
2557 + }
2558 +
2559 + switch (*op) {
2560 + case IP_SET_OP_CREATE:{
2561 + struct ip_set_req_create *req_create
2562 + = (struct ip_set_req_create *) data;
2563 +
2564 + if (len < sizeof(struct ip_set_req_create)) {
2565 + ip_set_printk("short CREATE data (want >=%zu, got %u)",
2566 + sizeof(struct ip_set_req_create), len);
2567 + res = -EINVAL;
2568 + goto done;
2569 + }
2570 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2571 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2572 + res = ip_set_create(req_create->name,
2573 + req_create->typename,
2574 + IP_SET_INVALID_ID,
2575 + data + sizeof(struct ip_set_req_create),
2576 + len - sizeof(struct ip_set_req_create));
2577 + goto done;
2578 + }
2579 + case IP_SET_OP_DESTROY:{
2580 + struct ip_set_req_std *req_destroy
2581 + = (struct ip_set_req_std *) data;
2582 +
2583 + if (len != sizeof(struct ip_set_req_std)) {
2584 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2585 + sizeof(struct ip_set_req_std), len);
2586 + res = -EINVAL;
2587 + goto done;
2588 + }
2589 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2590 + /* Destroy all sets */
2591 + index = IP_SET_INVALID_ID;
2592 + } else {
2593 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2594 + index = ip_set_find_byname(req_destroy->name);
2595 +
2596 + if (index == IP_SET_INVALID_ID) {
2597 + res = -ENOENT;
2598 + goto done;
2599 + }
2600 + }
2601 +
2602 + res = ip_set_destroy(index);
2603 + goto done;
2604 + }
2605 + case IP_SET_OP_FLUSH:{
2606 + struct ip_set_req_std *req_flush =
2607 + (struct ip_set_req_std *) data;
2608 +
2609 + if (len != sizeof(struct ip_set_req_std)) {
2610 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2611 + sizeof(struct ip_set_req_std), len);
2612 + res = -EINVAL;
2613 + goto done;
2614 + }
2615 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2616 + /* Flush all sets */
2617 + index = IP_SET_INVALID_ID;
2618 + } else {
2619 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2620 + index = ip_set_find_byname(req_flush->name);
2621 +
2622 + if (index == IP_SET_INVALID_ID) {
2623 + res = -ENOENT;
2624 + goto done;
2625 + }
2626 + }
2627 + res = ip_set_flush(index);
2628 + goto done;
2629 + }
2630 + case IP_SET_OP_RENAME:{
2631 + struct ip_set_req_create *req_rename
2632 + = (struct ip_set_req_create *) data;
2633 +
2634 + if (len != sizeof(struct ip_set_req_create)) {
2635 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2636 + sizeof(struct ip_set_req_create), len);
2637 + res = -EINVAL;
2638 + goto done;
2639 + }
2640 +
2641 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2642 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2643 +
2644 + index = ip_set_find_byname(req_rename->name);
2645 + if (index == IP_SET_INVALID_ID) {
2646 + res = -ENOENT;
2647 + goto done;
2648 + }
2649 + res = ip_set_rename(index, req_rename->typename);
2650 + goto done;
2651 + }
2652 + case IP_SET_OP_SWAP:{
2653 + struct ip_set_req_create *req_swap
2654 + = (struct ip_set_req_create *) data;
2655 + ip_set_id_t to_index;
2656 +
2657 + if (len != sizeof(struct ip_set_req_create)) {
2658 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2659 + sizeof(struct ip_set_req_create), len);
2660 + res = -EINVAL;
2661 + goto done;
2662 + }
2663 +
2664 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2665 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2666 +
2667 + index = ip_set_find_byname(req_swap->name);
2668 + if (index == IP_SET_INVALID_ID) {
2669 + res = -ENOENT;
2670 + goto done;
2671 + }
2672 + to_index = ip_set_find_byname(req_swap->typename);
2673 + if (to_index == IP_SET_INVALID_ID) {
2674 + res = -ENOENT;
2675 + goto done;
2676 + }
2677 + res = ip_set_swap(index, to_index);
2678 + goto done;
2679 + }
2680 + default:
2681 + break; /* Set identified by id */
2682 + }
2683 +
2684 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2685 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2686 + res = -EBADMSG;
2687 + goto done;
2688 + }
2689 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2690 +
2691 + if (len < sizeof(struct ip_set_req_adt)) {
2692 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2693 + sizeof(struct ip_set_req_adt), len);
2694 + res = -EINVAL;
2695 + goto done;
2696 + }
2697 + req_adt = (struct ip_set_req_adt *) data;
2698 +
2699 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2700 + if (!(*op == IP_SET_OP_UNBIND_SET
2701 + && req_adt->index == IP_SET_INVALID_ID)) {
2702 + index = ip_set_find_byindex(req_adt->index);
2703 + if (index == IP_SET_INVALID_ID) {
2704 + res = -ENOENT;
2705 + goto done;
2706 + }
2707 + }
2708 + res = adtfn(index, data, len);
2709 +
2710 + done:
2711 + up(&ip_set_app_mutex);
2712 + vfree(data);
2713 + if (res > 0)
2714 + res = 0;
2715 + DP("final result %d", res);
2716 + return res;
2717 +}
2718 +
2719 +static int
2720 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2721 +{
2722 + int res = 0;
2723 + unsigned *op;
2724 + ip_set_id_t index = IP_SET_INVALID_ID;
2725 + void *data;
2726 + int copylen = *len;
2727 +
2728 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2729 + if (!capable(CAP_NET_ADMIN))
2730 + return -EPERM;
2731 + if (optval != SO_IP_SET)
2732 + return -EBADF;
2733 + if (*len < sizeof(unsigned)) {
2734 + ip_set_printk("short userdata (want >=%zu, got %d)",
2735 + sizeof(unsigned), *len);
2736 + return -EINVAL;
2737 + }
2738 + data = vmalloc(*len);
2739 + if (!data) {
2740 + DP("out of mem for %d bytes", *len);
2741 + return -ENOMEM;
2742 + }
2743 + if (copy_from_user(data, user, *len) != 0) {
2744 + res = -EFAULT;
2745 + goto done;
2746 + }
2747 + if (down_interruptible(&ip_set_app_mutex)) {
2748 + res = -EINTR;
2749 + goto done;
2750 + }
2751 +
2752 + op = (unsigned *) data;
2753 + DP("op=%x", *op);
2754 +
2755 + if (*op < IP_SET_OP_VERSION) {
2756 + /* Check the version at the beginning of operations */
2757 + struct ip_set_req_version *req_version =
2758 + (struct ip_set_req_version *) data;
2759 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2760 + res = -EPROTO;
2761 + goto done;
2762 + }
2763 + }
2764 +
2765 + switch (*op) {
2766 + case IP_SET_OP_VERSION: {
2767 + struct ip_set_req_version *req_version =
2768 + (struct ip_set_req_version *) data;
2769 +
2770 + if (*len != sizeof(struct ip_set_req_version)) {
2771 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2772 + sizeof(struct ip_set_req_version),
2773 + *len);
2774 + res = -EINVAL;
2775 + goto done;
2776 + }
2777 +
2778 + req_version->version = IP_SET_PROTOCOL_VERSION;
2779 + res = copy_to_user(user, req_version,
2780 + sizeof(struct ip_set_req_version));
2781 + goto done;
2782 + }
2783 + case IP_SET_OP_GET_BYNAME: {
2784 + struct ip_set_req_get_set *req_get
2785 + = (struct ip_set_req_get_set *) data;
2786 +
2787 + if (*len != sizeof(struct ip_set_req_get_set)) {
2788 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2789 + sizeof(struct ip_set_req_get_set), *len);
2790 + res = -EINVAL;
2791 + goto done;
2792 + }
2793 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2794 + index = ip_set_find_byname(req_get->set.name);
2795 + req_get->set.index = index;
2796 + goto copy;
2797 + }
2798 + case IP_SET_OP_GET_BYINDEX: {
2799 + struct ip_set_req_get_set *req_get
2800 + = (struct ip_set_req_get_set *) data;
2801 +
2802 + if (*len != sizeof(struct ip_set_req_get_set)) {
2803 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2804 + sizeof(struct ip_set_req_get_set), *len);
2805 + res = -EINVAL;
2806 + goto done;
2807 + }
2808 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2809 + index = ip_set_find_byindex(req_get->set.index);
2810 + strncpy(req_get->set.name,
2811 + index == IP_SET_INVALID_ID ? ""
2812 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2813 + goto copy;
2814 + }
2815 + case IP_SET_OP_ADT_GET: {
2816 + struct ip_set_req_adt_get *req_get
2817 + = (struct ip_set_req_adt_get *) data;
2818 +
2819 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2820 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2821 + sizeof(struct ip_set_req_adt_get), *len);
2822 + res = -EINVAL;
2823 + goto done;
2824 + }
2825 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2826 + index = ip_set_find_byname(req_get->set.name);
2827 + if (index != IP_SET_INVALID_ID) {
2828 + req_get->set.index = index;
2829 + strncpy(req_get->typename,
2830 + ip_set_list[index]->type->typename,
2831 + IP_SET_MAXNAMELEN - 1);
2832 + } else {
2833 + res = -ENOENT;
2834 + goto done;
2835 + }
2836 + goto copy;
2837 + }
2838 + case IP_SET_OP_MAX_SETS: {
2839 + struct ip_set_req_max_sets *req_max_sets
2840 + = (struct ip_set_req_max_sets *) data;
2841 + ip_set_id_t i;
2842 +
2843 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2844 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2845 + sizeof(struct ip_set_req_max_sets), *len);
2846 + res = -EINVAL;
2847 + goto done;
2848 + }
2849 +
2850 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2851 + req_max_sets->set.index = IP_SET_INVALID_ID;
2852 + } else {
2853 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2854 + req_max_sets->set.index =
2855 + ip_set_find_byname(req_max_sets->set.name);
2856 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2857 + res = -ENOENT;
2858 + goto done;
2859 + }
2860 + }
2861 + req_max_sets->max_sets = ip_set_max;
2862 + req_max_sets->sets = 0;
2863 + for (i = 0; i < ip_set_max; i++) {
2864 + if (ip_set_list[i] != NULL)
2865 + req_max_sets->sets++;
2866 + }
2867 + goto copy;
2868 + }
2869 + case IP_SET_OP_LIST_SIZE:
2870 + case IP_SET_OP_SAVE_SIZE: {
2871 + struct ip_set_req_setnames *req_setnames
2872 + = (struct ip_set_req_setnames *) data;
2873 + struct ip_set_name_list *name_list;
2874 + struct ip_set *set;
2875 + ip_set_id_t i;
2876 + int used;
2877 +
2878 + if (*len < sizeof(struct ip_set_req_setnames)) {
2879 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2880 + sizeof(struct ip_set_req_setnames), *len);
2881 + res = -EINVAL;
2882 + goto done;
2883 + }
2884 +
2885 + req_setnames->size = 0;
2886 + used = sizeof(struct ip_set_req_setnames);
2887 + for (i = 0; i < ip_set_max; i++) {
2888 + if (ip_set_list[i] == NULL)
2889 + continue;
2890 + name_list = (struct ip_set_name_list *)
2891 + (data + used);
2892 + used += sizeof(struct ip_set_name_list);
2893 + if (used > copylen) {
2894 + res = -EAGAIN;
2895 + goto done;
2896 + }
2897 + set = ip_set_list[i];
2898 + /* Fill in index, name, etc. */
2899 + name_list->index = i;
2900 + name_list->id = set->id;
2901 + strncpy(name_list->name,
2902 + set->name,
2903 + IP_SET_MAXNAMELEN - 1);
2904 + strncpy(name_list->typename,
2905 + set->type->typename,
2906 + IP_SET_MAXNAMELEN - 1);
2907 + DP("filled %s of type %s, index %u\n",
2908 + name_list->name, name_list->typename,
2909 + name_list->index);
2910 + if (!(req_setnames->index == IP_SET_INVALID_ID
2911 + || req_setnames->index == i))
2912 + continue;
2913 + /* Update size */
2914 + switch (*op) {
2915 + case IP_SET_OP_LIST_SIZE: {
2916 + req_setnames->size += sizeof(struct ip_set_list)
2917 + + set->type->header_size
2918 + + set->type->list_members_size(set);
2919 + /* Sets are identified by id in the hash */
2920 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2921 + set->id, &req_setnames->size);
2922 + break;
2923 + }
2924 + case IP_SET_OP_SAVE_SIZE: {
2925 + req_setnames->size += sizeof(struct ip_set_save)
2926 + + set->type->header_size
2927 + + set->type->list_members_size(set);
2928 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2929 + set->id, &req_setnames->size);
2930 + break;
2931 + }
2932 + default:
2933 + break;
2934 + }
2935 + }
2936 + if (copylen != used) {
2937 + res = -EAGAIN;
2938 + goto done;
2939 + }
2940 + goto copy;
2941 + }
2942 + case IP_SET_OP_LIST: {
2943 + struct ip_set_req_list *req_list
2944 + = (struct ip_set_req_list *) data;
2945 + ip_set_id_t i;
2946 + int used;
2947 +
2948 + if (*len < sizeof(struct ip_set_req_list)) {
2949 + ip_set_printk("short LIST (want >=%zu, got %d)",
2950 + sizeof(struct ip_set_req_list), *len);
2951 + res = -EINVAL;
2952 + goto done;
2953 + }
2954 + index = req_list->index;
2955 + if (index != IP_SET_INVALID_ID
2956 + && ip_set_find_byindex(index) != index) {
2957 + res = -ENOENT;
2958 + goto done;
2959 + }
2960 + used = 0;
2961 + if (index == IP_SET_INVALID_ID) {
2962 + /* List all sets */
2963 + for (i = 0; i < ip_set_max && res == 0; i++) {
2964 + if (ip_set_list[i] != NULL)
2965 + res = ip_set_list_set(i, data, &used, *len);
2966 + }
2967 + } else {
2968 + /* List an individual set */
2969 + res = ip_set_list_set(index, data, &used, *len);
2970 + }
2971 + if (res != 0)
2972 + goto done;
2973 + else if (copylen != used) {
2974 + res = -EAGAIN;
2975 + goto done;
2976 + }
2977 + goto copy;
2978 + }
2979 + case IP_SET_OP_SAVE: {
2980 + struct ip_set_req_list *req_save
2981 + = (struct ip_set_req_list *) data;
2982 + ip_set_id_t i;
2983 + int used;
2984 +
2985 + if (*len < sizeof(struct ip_set_req_list)) {
2986 + ip_set_printk("short SAVE (want >=%zu, got %d)",
2987 + sizeof(struct ip_set_req_list), *len);
2988 + res = -EINVAL;
2989 + goto done;
2990 + }
2991 + index = req_save->index;
2992 + if (index != IP_SET_INVALID_ID
2993 + && ip_set_find_byindex(index) != index) {
2994 + res = -ENOENT;
2995 + goto done;
2996 + }
2997 + used = 0;
2998 + if (index == IP_SET_INVALID_ID) {
2999 + /* Save all sets */
3000 + for (i = 0; i < ip_set_max && res == 0; i++) {
3001 + if (ip_set_list[i] != NULL)
3002 + res = ip_set_save_set(i, data, &used, *len);
3003 + }
3004 + } else {
3005 + /* Save an individual set */
3006 + res = ip_set_save_set(index, data, &used, *len);
3007 + }
3008 + if (res == 0)
3009 + res = ip_set_save_bindings(index, data, &used, *len);
3010 +
3011 + if (res != 0)
3012 + goto done;
3013 + else if (copylen != used) {
3014 + res = -EAGAIN;
3015 + goto done;
3016 + }
3017 + goto copy;
3018 + }
3019 + case IP_SET_OP_RESTORE: {
3020 + struct ip_set_req_setnames *req_restore
3021 + = (struct ip_set_req_setnames *) data;
3022 + int line;
3023 +
3024 + if (*len < sizeof(struct ip_set_req_setnames)
3025 + || *len != req_restore->size) {
3026 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3027 + req_restore->size, *len);
3028 + res = -EINVAL;
3029 + goto done;
3030 + }
3031 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3032 + req_restore->size - sizeof(struct ip_set_req_setnames));
3033 + DP("ip_set_restore: %u", line);
3034 + if (line != 0) {
3035 + res = -EAGAIN;
3036 + req_restore->size = line;
3037 + copylen = sizeof(struct ip_set_req_setnames);
3038 + goto copy;
3039 + }
3040 + goto done;
3041 + }
3042 + default:
3043 + res = -EBADMSG;
3044 + goto done;
3045 + } /* end of switch(op) */
3046 +
3047 + copy:
3048 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3049 + && ip_set_list[index]
3050 + ? ip_set_list[index]->name
3051 + : ":all:", copylen);
3052 + res = copy_to_user(user, data, copylen);
3053 +
3054 + done:
3055 + up(&ip_set_app_mutex);
3056 + vfree(data);
3057 + if (res > 0)
3058 + res = 0;
3059 + DP("final result %d", res);
3060 + return res;
3061 +}
3062 +
3063 +static struct nf_sockopt_ops so_set = {
3064 + .pf = PF_INET,
3065 + .set_optmin = SO_IP_SET,
3066 + .set_optmax = SO_IP_SET + 1,
3067 + .set = &ip_set_sockfn_set,
3068 + .get_optmin = SO_IP_SET,
3069 + .get_optmax = SO_IP_SET + 1,
3070 + .get = &ip_set_sockfn_get,
3071 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
3072 + .owner = THIS_MODULE,
3073 +#endif
3074 +};
3075 +
3076 +static int max_sets, hash_size;
3077 +module_param(max_sets, int, 0600);
3078 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3079 +module_param(hash_size, int, 0600);
3080 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3081 +MODULE_LICENSE("GPL");
3082 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3083 +MODULE_DESCRIPTION("module implementing core IP set support");
3084 +
3085 +static int __init ip_set_init(void)
3086 +{
3087 + int res;
3088 + ip_set_id_t i;
3089 +
3090 + get_random_bytes(&ip_set_hash_random, 4);
3091 + if (max_sets)
3092 + ip_set_max = max_sets;
3093 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3094 + if (!ip_set_list) {
3095 + printk(KERN_ERR "Unable to create ip_set_list\n");
3096 + return -ENOMEM;
3097 + }
3098 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3099 + if (hash_size)
3100 + ip_set_bindings_hash_size = hash_size;
3101 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3102 + if (!ip_set_hash) {
3103 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3104 + vfree(ip_set_list);
3105 + return -ENOMEM;
3106 + }
3107 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3108 + INIT_LIST_HEAD(&ip_set_hash[i]);
3109 +
3110 + INIT_LIST_HEAD(&set_type_list);
3111 +
3112 + res = nf_register_sockopt(&so_set);
3113 + if (res != 0) {
3114 + ip_set_printk("SO_SET registry failed: %d", res);
3115 + vfree(ip_set_list);
3116 + vfree(ip_set_hash);
3117 + return res;
3118 + }
3119 + return 0;
3120 +}
3121 +
3122 +static void __exit ip_set_fini(void)
3123 +{
3124 + /* There can't be any existing set or binding */
3125 + nf_unregister_sockopt(&so_set);
3126 + vfree(ip_set_list);
3127 + vfree(ip_set_hash);
3128 + DP("these are the famous last words");
3129 +}
3130 +
3131 +EXPORT_SYMBOL(ip_set_register_set_type);
3132 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3133 +
3134 +EXPORT_SYMBOL(ip_set_get_byname);
3135 +EXPORT_SYMBOL(ip_set_get_byindex);
3136 +EXPORT_SYMBOL(ip_set_put);
3137 +
3138 +EXPORT_SYMBOL(ip_set_addip_kernel);
3139 +EXPORT_SYMBOL(ip_set_delip_kernel);
3140 +EXPORT_SYMBOL(ip_set_testip_kernel);
3141 +
3142 +module_init(ip_set_init);
3143 +module_exit(ip_set_fini);
3144 --- /dev/null
3145 +++ b/net/ipv4/netfilter/ip_set_iphash.c
3146 @@ -0,0 +1,429 @@
3147 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3148 + *
3149 + * This program is free software; you can redistribute it and/or modify
3150 + * it under the terms of the GNU General Public License version 2 as
3151 + * published by the Free Software Foundation.
3152 + */
3153 +
3154 +/* Kernel module implementing an ip hash set */
3155 +
3156 +#include <linux/module.h>
3157 +#include <linux/ip.h>
3158 +#include <linux/skbuff.h>
3159 +#include <linux/version.h>
3160 +#include <linux/jhash.h>
3161 +#include <linux/netfilter_ipv4/ip_tables.h>
3162 +#include <linux/netfilter_ipv4/ip_set.h>
3163 +#include <linux/errno.h>
3164 +#include <asm/uaccess.h>
3165 +#include <asm/bitops.h>
3166 +#include <linux/spinlock.h>
3167 +#include <linux/vmalloc.h>
3168 +#include <linux/random.h>
3169 +
3170 +#include <net/ip.h>
3171 +
3172 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3173 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3174 +
3175 +static int limit = MAX_RANGE;
3176 +
3177 +static inline __u32
3178 +jhash_ip(const struct ip_set_iphash *map, uint16_t i, ip_set_ip_t ip)
3179 +{
3180 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
3181 +}
3182 +
3183 +static inline __u32
3184 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3185 +{
3186 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3187 + __u32 id;
3188 + u_int16_t i;
3189 + ip_set_ip_t *elem;
3190 +
3191 + *hash_ip = ip & map->netmask;
3192 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3193 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3194 +
3195 + for (i = 0; i < map->probes; i++) {
3196 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
3197 + DP("hash key: %u", id);
3198 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3199 + if (*elem == *hash_ip)
3200 + return id;
3201 + /* No shortcut at testing - there can be deleted
3202 + * entries. */
3203 + }
3204 + return UINT_MAX;
3205 +}
3206 +
3207 +static inline int
3208 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3209 +{
3210 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
3211 +}
3212 +
3213 +static int
3214 +testip(struct ip_set *set, const void *data, size_t size,
3215 + ip_set_ip_t *hash_ip)
3216 +{
3217 + struct ip_set_req_iphash *req =
3218 + (struct ip_set_req_iphash *) data;
3219 +
3220 + if (size != sizeof(struct ip_set_req_iphash)) {
3221 + ip_set_printk("data length wrong (want %zu, have %zu)",
3222 + sizeof(struct ip_set_req_iphash),
3223 + size);
3224 + return -EINVAL;
3225 + }
3226 + return __testip(set, req->ip, hash_ip);
3227 +}
3228 +
3229 +static int
3230 +testip_kernel(struct ip_set *set,
3231 + const struct sk_buff *skb,
3232 + ip_set_ip_t *hash_ip,
3233 + const u_int32_t *flags,
3234 + unsigned char index)
3235 +{
3236 + return __testip(set,
3237 + ntohl(flags[index] & IPSET_SRC
3238 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3239 + ? ip_hdr(skb)->saddr
3240 + : ip_hdr(skb)->daddr),
3241 +#else
3242 + ? skb->nh.iph->saddr
3243 + : skb->nh.iph->daddr),
3244 +#endif
3245 + hash_ip);
3246 +}
3247 +
3248 +static inline int
3249 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3250 +{
3251 + __u32 probe;
3252 + u_int16_t i;
3253 + ip_set_ip_t *elem;
3254 +
3255 + if (!ip || map->elements >= limit)
3256 + return -ERANGE;
3257 +
3258 + *hash_ip = ip & map->netmask;
3259 +
3260 + for (i = 0; i < map->probes; i++) {
3261 + probe = jhash_ip(map, i, *hash_ip) % map->hashsize;
3262 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
3263 + if (*elem == *hash_ip)
3264 + return -EEXIST;
3265 + if (!*elem) {
3266 + *elem = *hash_ip;
3267 + map->elements++;
3268 + return 0;
3269 + }
3270 + }
3271 + /* Trigger rehashing */
3272 + return -EAGAIN;
3273 +}
3274 +
3275 +static int
3276 +addip(struct ip_set *set, const void *data, size_t size,
3277 + ip_set_ip_t *hash_ip)
3278 +{
3279 + struct ip_set_req_iphash *req =
3280 + (struct ip_set_req_iphash *) data;
3281 +
3282 + if (size != sizeof(struct ip_set_req_iphash)) {
3283 + ip_set_printk("data length wrong (want %zu, have %zu)",
3284 + sizeof(struct ip_set_req_iphash),
3285 + size);
3286 + return -EINVAL;
3287 + }
3288 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3289 +}
3290 +
3291 +static int
3292 +addip_kernel(struct ip_set *set,
3293 + const struct sk_buff *skb,
3294 + ip_set_ip_t *hash_ip,
3295 + const u_int32_t *flags,
3296 + unsigned char index)
3297 +{
3298 + return __addip((struct ip_set_iphash *) set->data,
3299 + ntohl(flags[index] & IPSET_SRC
3300 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3301 + ? ip_hdr(skb)->saddr
3302 + : ip_hdr(skb)->daddr),
3303 +#else
3304 + ? skb->nh.iph->saddr
3305 + : skb->nh.iph->daddr),
3306 +#endif
3307 + hash_ip);
3308 +}
3309 +
3310 +static int retry(struct ip_set *set)
3311 +{
3312 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3313 + ip_set_ip_t hash_ip, *elem;
3314 + void *members;
3315 + u_int32_t i, hashsize = map->hashsize;
3316 + int res;
3317 + struct ip_set_iphash *tmp;
3318 +
3319 + if (map->resize == 0)
3320 + return -ERANGE;
3321 +
3322 + again:
3323 + res = 0;
3324 +
3325 + /* Calculate new hash size */
3326 + hashsize += (hashsize * map->resize)/100;
3327 + if (hashsize == map->hashsize)
3328 + hashsize++;
3329 +
3330 + ip_set_printk("rehashing of set %s triggered: "
3331 + "hashsize grows from %u to %u",
3332 + set->name, map->hashsize, hashsize);
3333 +
3334 + tmp = kmalloc(sizeof(struct ip_set_iphash)
3335 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
3336 + if (!tmp) {
3337 + DP("out of memory for %d bytes",
3338 + sizeof(struct ip_set_iphash)
3339 + + map->probes * sizeof(uint32_t));
3340 + return -ENOMEM;
3341 + }
3342 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
3343 + if (!tmp->members) {
3344 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
3345 + kfree(tmp);
3346 + return -ENOMEM;
3347 + }
3348 + tmp->hashsize = hashsize;
3349 + tmp->elements = 0;
3350 + tmp->probes = map->probes;
3351 + tmp->resize = map->resize;
3352 + tmp->netmask = map->netmask;
3353 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
3354 +
3355 + write_lock_bh(&set->lock);
3356 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3357 + for (i = 0; i < map->hashsize && res == 0; i++) {
3358 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3359 + if (*elem)
3360 + res = __addip(tmp, *elem, &hash_ip);
3361 + }
3362 + if (res) {
3363 + /* Failure, try again */
3364 + write_unlock_bh(&set->lock);
3365 + harray_free(tmp->members);
3366 + kfree(tmp);
3367 + goto again;
3368 + }
3369 +
3370 + /* Success at resizing! */
3371 + members = map->members;
3372 +
3373 + map->hashsize = tmp->hashsize;
3374 + map->members = tmp->members;
3375 + write_unlock_bh(&set->lock);
3376 +
3377 + harray_free(members);
3378 + kfree(tmp);
3379 +
3380 + return 0;
3381 +}
3382 +
3383 +static inline int
3384 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3385 +{
3386 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3387 + ip_set_ip_t id, *elem;
3388 +
3389 + if (!ip)
3390 + return -ERANGE;
3391 +
3392 + id = hash_id(set, ip, hash_ip);
3393 + if (id == UINT_MAX)
3394 + return -EEXIST;
3395 +
3396 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3397 + *elem = 0;
3398 + map->elements--;
3399 +
3400 + return 0;
3401 +}
3402 +
3403 +static int
3404 +delip(struct ip_set *set, const void *data, size_t size,
3405 + ip_set_ip_t *hash_ip)
3406 +{
3407 + struct ip_set_req_iphash *req =
3408 + (struct ip_set_req_iphash *) data;
3409 +
3410 + if (size != sizeof(struct ip_set_req_iphash)) {
3411 + ip_set_printk("data length wrong (want %zu, have %zu)",
3412 + sizeof(struct ip_set_req_iphash),
3413 + size);
3414 + return -EINVAL;
3415 + }
3416 + return __delip(set, req->ip, hash_ip);
3417 +}
3418 +