[kernel] refresh 2.6.26 patches
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.26 / 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 +
3419 +static int
3420 +delip_kernel(struct ip_set *set,
3421 +            const struct sk_buff *skb,
3422 +            ip_set_ip_t *hash_ip,
3423 +            const u_int32_t *flags,
3424 +            unsigned char index)
3425 +{
3426 +       return __delip(set,
3427 +                      ntohl(flags[index] & IPSET_SRC
3428 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3429 +                               ? ip_hdr(skb)->saddr
3430 +                               : ip_hdr(skb)->daddr),
3431 +#else
3432 +                               ? skb->nh.iph->saddr
3433 +                               : skb->nh.iph->daddr),
3434 +#endif
3435 +                      hash_ip);
3436 +}
3437 +
3438 +static int create(struct ip_set *set, const void *data, size_t size)
3439 +{
3440 +       struct ip_set_req_iphash_create *req =
3441 +           (struct ip_set_req_iphash_create *) data;
3442 +       struct ip_set_iphash *map;
3443 +       uint16_t i;
3444 +
3445 +       if (size != sizeof(struct ip_set_req_iphash_create)) {
3446 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3447 +                              sizeof(struct ip_set_req_iphash_create),
3448 +                              size);
3449 +               return -EINVAL;
3450 +       }
3451 +
3452 +       if (req->hashsize < 1) {
3453 +               ip_set_printk("hashsize too small");
3454 +               return -ENOEXEC;
3455 +       }
3456 +
3457 +       if (req->probes < 1) {
3458 +               ip_set_printk("probes too small");
3459 +               return -ENOEXEC;
3460 +       }
3461 +
3462 +       map = kmalloc(sizeof(struct ip_set_iphash)
3463 +                     + req->probes * sizeof(uint32_t), GFP_KERNEL);
3464 +       if (!map) {
3465 +               DP("out of memory for %d bytes",
3466 +                  sizeof(struct ip_set_iphash)
3467 +                  + req->probes * sizeof(uint32_t));
3468 +               return -ENOMEM;
3469 +       }
3470 +       for (i = 0; i < req->probes; i++)
3471 +               get_random_bytes(((uint32_t *) map->initval)+i, 4);
3472 +       map->elements = 0;
3473 +       map->hashsize = req->hashsize;
3474 +       map->probes = req->probes;
3475 +       map->resize = req->resize;
3476 +       map->netmask = req->netmask;
3477 +       map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
3478 +       if (!map->members) {
3479 +               DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
3480 +               kfree(map);
3481 +               return -ENOMEM;
3482 +       }
3483 +
3484 +       set->data = map;
3485 +       return 0;
3486 +}
3487 +
3488 +static void destroy(struct ip_set *set)
3489 +{
3490 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3491 +
3492 +       harray_free(map->members);
3493 +       kfree(map);
3494 +
3495 +       set->data = NULL;
3496 +}
3497 +
3498 +static void flush(struct ip_set *set)
3499 +{
3500 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3501 +       harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
3502 +       map->elements = 0;
3503 +}
3504 +
3505 +static void list_header(const struct ip_set *set, void *data)
3506 +{
3507 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3508 +       struct ip_set_req_iphash_create *header =
3509 +           (struct ip_set_req_iphash_create *) data;
3510 +
3511 +       header->hashsize = map->hashsize;
3512 +       header->probes = map->probes;
3513 +       header->resize = map->resize;
3514 +       header->netmask = map->netmask;
3515 +}
3516 +
3517 +static int list_members_size(const struct ip_set *set)
3518 +{
3519 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3520 +
3521 +       return (map->hashsize * sizeof(ip_set_ip_t));
3522 +}
3523 +
3524 +static void list_members(const struct ip_set *set, void *data)
3525 +{
3526 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3527 +       ip_set_ip_t i, *elem;
3528 +
3529 +       for (i = 0; i < map->hashsize; i++) {
3530 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3531 +               ((ip_set_ip_t *)data)[i] = *elem;
3532 +       }
3533 +}
3534 +
3535 +static struct ip_set_type ip_set_iphash = {
3536 +       .typename               = SETTYPE_NAME,
3537 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
3538 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
3539 +       .create                 = &create,
3540 +       .destroy                = &destroy,
3541 +       .flush                  = &flush,
3542 +       .reqsize                = sizeof(struct ip_set_req_iphash),
3543 +       .addip                  = &addip,
3544 +       .addip_kernel           = &addip_kernel,
3545 +       .retry                  = &retry,
3546 +       .delip                  = &delip,
3547 +       .delip_kernel           = &delip_kernel,
3548 +       .testip                 = &testip,
3549 +       .testip_kernel          = &testip_kernel,
3550 +       .header_size            = sizeof(struct ip_set_req_iphash_create),
3551 +       .list_header            = &list_header,
3552 +       .list_members_size      = &list_members_size,
3553 +       .list_members           = &list_members,
3554 +       .me                     = THIS_MODULE,
3555 +};
3556 +
3557 +MODULE_LICENSE("GPL");
3558 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3559 +MODULE_DESCRIPTION("iphash type of IP sets");
3560 +module_param(limit, int, 0600);
3561 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
3562 +
3563 +static int __init ip_set_iphash_init(void)
3564 +{
3565 +       return ip_set_register_set_type(&ip_set_iphash);
3566 +}
3567 +
3568 +static void __exit ip_set_iphash_fini(void)
3569 +{
3570 +       /* FIXME: possible race with ip_set_create() */
3571 +       ip_set_unregister_set_type(&ip_set_iphash);
3572 +}
3573 +
3574 +module_init(ip_set_iphash_init);
3575 +module_exit(ip_set_iphash_fini);
3576 --- /dev/null
3577 +++ b/net/ipv4/netfilter/ip_set_ipmap.c
3578 @@ -0,0 +1,336 @@
3579 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3580 + *                         Patrick Schaaf <bof@bof.de>
3581 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3582 + *
3583 + * This program is free software; you can redistribute it and/or modify
3584 + * it under the terms of the GNU General Public License version 2 as
3585 + * published by the Free Software Foundation.
3586 + */
3587 +
3588 +/* Kernel module implementing an IP set type: the single bitmap type */
3589 +
3590 +#include <linux/module.h>
3591 +#include <linux/ip.h>
3592 +#include <linux/skbuff.h>
3593 +#include <linux/version.h>
3594 +#include <linux/netfilter_ipv4/ip_tables.h>
3595 +#include <linux/netfilter_ipv4/ip_set.h>
3596 +#include <linux/errno.h>
3597 +#include <asm/uaccess.h>
3598 +#include <asm/bitops.h>
3599 +#include <linux/spinlock.h>
3600 +
3601 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3602 +
3603 +static inline ip_set_ip_t
3604 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3605 +{
3606 +       return (ip - map->first_ip)/map->hosts;
3607 +}
3608 +
3609 +static inline int
3610 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3611 +{
3612 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3613 +
3614 +       if (ip < map->first_ip || ip > map->last_ip)
3615 +               return -ERANGE;
3616 +
3617 +       *hash_ip = ip & map->netmask;
3618 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3619 +          set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3620 +       return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3621 +}
3622 +
3623 +static int
3624 +testip(struct ip_set *set, const void *data, size_t size,
3625 +       ip_set_ip_t *hash_ip)
3626 +{
3627 +       struct ip_set_req_ipmap *req =
3628 +           (struct ip_set_req_ipmap *) data;
3629 +
3630 +       if (size != sizeof(struct ip_set_req_ipmap)) {
3631 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3632 +                             sizeof(struct ip_set_req_ipmap),
3633 +                             size);
3634 +               return -EINVAL;
3635 +       }
3636 +       return __testip(set, req->ip, hash_ip);
3637 +}
3638 +
3639 +static int
3640 +testip_kernel(struct ip_set *set,
3641 +             const struct sk_buff *skb,
3642 +             ip_set_ip_t *hash_ip,
3643 +             const u_int32_t *flags,
3644 +             unsigned char index)
3645 +{
3646 +       int res =  __testip(set,
3647 +                       ntohl(flags[index] & IPSET_SRC
3648 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3649 +                               ? ip_hdr(skb)->saddr
3650 +                               : ip_hdr(skb)->daddr),
3651 +#else
3652 +                               ? skb->nh.iph->saddr
3653 +                               : skb->nh.iph->daddr),
3654 +#endif
3655 +                       hash_ip);
3656 +       return (res < 0 ? 0 : res);
3657 +}
3658 +
3659 +static inline int
3660 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3661 +{
3662 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3663 +
3664 +       if (ip < map->first_ip || ip > map->last_ip)
3665 +               return -ERANGE;
3666 +
3667 +       *hash_ip = ip & map->netmask;
3668 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3669 +       if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3670 +               return -EEXIST;
3671 +
3672 +       return 0;
3673 +}
3674 +
3675 +static int
3676 +addip(struct ip_set *set, const void *data, size_t size,
3677 +      ip_set_ip_t *hash_ip)
3678 +{
3679 +       struct ip_set_req_ipmap *req =
3680 +           (struct ip_set_req_ipmap *) data;
3681 +
3682 +       if (size != sizeof(struct ip_set_req_ipmap)) {
3683 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3684 +                             sizeof(struct ip_set_req_ipmap),
3685 +                             size);
3686 +               return -EINVAL;
3687 +       }
3688 +       DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3689 +       return __addip(set, req->ip, hash_ip);
3690 +}
3691 +
3692 +static int
3693 +addip_kernel(struct ip_set *set,
3694 +            const struct sk_buff *skb,
3695 +            ip_set_ip_t *hash_ip,
3696 +            const u_int32_t *flags,
3697 +            unsigned char index)
3698 +{
3699 +       return __addip(set,
3700 +                      ntohl(flags[index] & IPSET_SRC
3701 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3702 +                               ? ip_hdr(skb)->saddr
3703 +                               : ip_hdr(skb)->daddr),
3704 +#else
3705 +                               ? skb->nh.iph->saddr
3706 +                               : skb->nh.iph->daddr),
3707 +#endif
3708 +                      hash_ip);
3709 +}
3710 +
3711 +static inline int
3712 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3713 +{
3714 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3715 +
3716 +       if (ip < map->first_ip || ip > map->last_ip)
3717 +               return -ERANGE;
3718 +
3719 +       *hash_ip = ip & map->netmask;
3720 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3721 +       if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3722 +               return -EEXIST;
3723 +
3724 +       return 0;
3725 +}
3726 +
3727 +static int
3728 +delip(struct ip_set *set, const void *data, size_t size,
3729 +      ip_set_ip_t *hash_ip)
3730 +{
3731 +       struct ip_set_req_ipmap *req =
3732 +           (struct ip_set_req_ipmap *) data;
3733 +
3734 +       if (size != sizeof(struct ip_set_req_ipmap)) {
3735 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3736 +                             sizeof(struct ip_set_req_ipmap),
3737 +                             size);
3738 +               return -EINVAL;
3739 +       }
3740 +       return __delip(set, req->ip, hash_ip);
3741 +}
3742 +
3743 +static int
3744 +delip_kernel(struct ip_set *set,
3745 +            const struct sk_buff *skb,
3746 +            ip_set_ip_t *hash_ip,
3747 +            const u_int32_t *flags,
3748 +            unsigned char index)
3749 +{
3750 +       return __delip(set,
3751 +                      ntohl(flags[index] & IPSET_SRC
3752 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3753 +                               ? ip_hdr(skb)->saddr
3754 +                               : ip_hdr(skb)->daddr),
3755 +#else
3756 +                               ? skb->nh.iph->saddr
3757 +                               : skb->nh.iph->daddr),
3758 +#endif
3759 +                      hash_ip);
3760 +}
3761 +
3762 +static int create(struct ip_set *set, const void *data, size_t size)
3763 +{
3764 +       int newbytes;
3765 +       struct ip_set_req_ipmap_create *req =
3766 +           (struct ip_set_req_ipmap_create *) data;
3767 +       struct ip_set_ipmap *map;
3768 +
3769 +       if (size != sizeof(struct ip_set_req_ipmap_create)) {
3770 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3771 +                             sizeof(struct ip_set_req_ipmap_create),
3772 +                             size);
3773 +               return -EINVAL;
3774 +       }
3775 +
3776 +       DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3777 +          HIPQUAD(req->from), HIPQUAD(req->to));
3778 +
3779 +       if (req->from > req->to) {
3780 +               DP("bad ip range");
3781 +               return -ENOEXEC;
3782 +       }
3783 +
3784 +       map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3785 +       if (!map) {
3786 +               DP("out of memory for %d bytes",
3787 +                  sizeof(struct ip_set_ipmap));
3788 +               return -ENOMEM;
3789 +       }
3790 +       map->first_ip = req->from;
3791 +       map->last_ip = req->to;
3792 +       map->netmask = req->netmask;
3793 +
3794 +       if (req->netmask == 0xFFFFFFFF) {
3795 +               map->hosts = 1;
3796 +               map->sizeid = map->last_ip - map->first_ip + 1;
3797 +       } else {
3798 +               unsigned int mask_bits, netmask_bits;
3799 +               ip_set_ip_t mask;
3800 +
3801 +               map->first_ip &= map->netmask;  /* Should we better bark? */
3802 +
3803 +               mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3804 +               netmask_bits = mask_to_bits(map->netmask);
3805 +
3806 +               if ((!mask && (map->first_ip || map->last_ip != 0xFFFFFFFF))
3807 +                   || netmask_bits <= mask_bits)
3808 +                       return -ENOEXEC;
3809 +
3810 +               DP("mask_bits %u, netmask_bits %u",
3811 +                  mask_bits, netmask_bits);
3812 +               map->hosts = 2 << (32 - netmask_bits - 1);
3813 +               map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3814 +       }
3815 +       if (map->sizeid > MAX_RANGE + 1) {
3816 +               ip_set_printk("range too big (max %d addresses)",
3817 +                              MAX_RANGE+1);
3818 +               kfree(map);
3819 +               return -ENOEXEC;
3820 +       }
3821 +       DP("hosts %u, sizeid %u", map->hosts, map->sizeid);
3822 +       newbytes = bitmap_bytes(0, map->sizeid - 1);
3823 +       map->members = kmalloc(newbytes, GFP_KERNEL);
3824 +       if (!map->members) {
3825 +               DP("out of memory for %d bytes", newbytes);
3826 +               kfree(map);
3827 +               return -ENOMEM;
3828 +       }
3829 +       memset(map->members, 0, newbytes);
3830 +
3831 +       set->data = map;
3832 +       return 0;
3833 +}
3834 +
3835 +static void destroy(struct ip_set *set)
3836 +{
3837 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3838 +
3839 +       kfree(map->members);
3840 +       kfree(map);
3841 +
3842 +       set->data = NULL;
3843 +}
3844 +
3845 +static void flush(struct ip_set *set)
3846 +{
3847 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3848 +       memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3849 +}
3850 +
3851 +static void list_header(const struct ip_set *set, void *data)
3852 +{
3853 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3854 +       struct ip_set_req_ipmap_create *header =
3855 +           (struct ip_set_req_ipmap_create *) data;
3856 +
3857 +       header->from = map->first_ip;
3858 +       header->to = map->last_ip;
3859 +       header->netmask = map->netmask;
3860 +}
3861 +
3862 +static int list_members_size(const struct ip_set *set)
3863 +{
3864 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3865 +
3866 +       return bitmap_bytes(0, map->sizeid - 1);
3867 +}
3868 +
3869 +static void list_members(const struct ip_set *set, void *data)
3870 +{
3871 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3872 +       int bytes = bitmap_bytes(0, map->sizeid - 1);
3873 +
3874 +       memcpy(data, map->members, bytes);
3875 +}
3876 +
3877 +static struct ip_set_type ip_set_ipmap = {
3878 +       .typename               = SETTYPE_NAME,
3879 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
3880 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
3881 +       .create                 = &create,
3882 +       .destroy                = &destroy,
3883 +       .flush                  = &flush,
3884 +       .reqsize                = sizeof(struct ip_set_req_ipmap),
3885 +       .addip                  = &addip,
3886 +       .addip_kernel           = &addip_kernel,
3887 +       .delip                  = &delip,
3888 +       .delip_kernel           = &delip_kernel,
3889 +       .testip                 = &testip,
3890 +       .testip_kernel          = &testip_kernel,
3891 +       .header_size            = sizeof(struct ip_set_req_ipmap_create),
3892 +       .list_header            = &list_header,
3893 +       .list_members_size      = &list_members_size,
3894 +       .list_members           = &list_members,
3895 +       .me                     = THIS_MODULE,
3896 +};
3897 +
3898 +MODULE_LICENSE("GPL");
3899 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3900 +MODULE_DESCRIPTION("ipmap type of IP sets");
3901 +
3902 +static int __init ip_set_ipmap_init(void)
3903 +{
3904 +       return ip_set_register_set_type(&ip_set_ipmap);
3905 +}
3906 +
3907 +static void __exit ip_set_ipmap_fini(void)
3908 +{
3909 +       /* FIXME: possible race with ip_set_create() */
3910 +       ip_set_unregister_set_type(&ip_set_ipmap);
3911 +}
3912 +
3913 +module_init(ip_set_ipmap_init);
3914 +module_exit(ip_set_ipmap_fini);
3915 --- /dev/null
3916 +++ b/net/ipv4/netfilter/ip_set_ipporthash.c
3917 @@ -0,0 +1,581 @@
3918 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3919 + *
3920 + * This program is free software; you can redistribute it and/or modify
3921 + * it under the terms of the GNU General Public License version 2 as
3922 + * published by the Free Software Foundation.
3923 + */
3924 +
3925 +/* Kernel module implementing an ip+port hash set */
3926 +
3927 +#include <linux/module.h>
3928 +#include <linux/ip.h>
3929 +#include <linux/tcp.h>
3930 +#include <linux/udp.h>
3931 +#include <linux/skbuff.h>
3932 +#include <linux/version.h>
3933 +#include <linux/jhash.h>
3934 +#include <linux/netfilter_ipv4/ip_tables.h>
3935 +#include <linux/netfilter_ipv4/ip_set.h>
3936 +#include <linux/errno.h>
3937 +#include <asm/uaccess.h>
3938 +#include <asm/bitops.h>
3939 +#include <linux/spinlock.h>
3940 +#include <linux/vmalloc.h>
3941 +#include <linux/random.h>
3942 +
3943 +#include <net/ip.h>
3944 +
3945 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3946 +#include <linux/netfilter_ipv4/ip_set_ipporthash.h>
3947 +
3948 +static int limit = MAX_RANGE;
3949 +
3950 +/* We must handle non-linear skbs */
3951 +static inline ip_set_ip_t
3952 +get_port(const struct sk_buff *skb, u_int32_t flags)
3953 +{
3954 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3955 +       struct iphdr *iph = ip_hdr(skb);
3956 +#else
3957 +       struct iphdr *iph = skb->nh.iph;
3958 +#endif
3959 +       u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
3960 +
3961 +       switch (iph->protocol) {
3962 +       case IPPROTO_TCP: {
3963 +               struct tcphdr tcph;
3964 +
3965 +               /* See comments at tcp_match in ip_tables.c */
3966 +               if (offset)
3967 +                       return INVALID_PORT;
3968 +
3969 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3970 +               if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
3971 +#else
3972 +               if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
3973 +#endif
3974 +                       /* No choice either */
3975 +                       return INVALID_PORT;
3976 +
3977 +               return ntohs(flags & IPSET_SRC ?
3978 +                            tcph.source : tcph.dest);
3979 +           }
3980 +       case IPPROTO_UDP: {
3981 +               struct udphdr udph;
3982 +
3983 +               if (offset)
3984 +                       return INVALID_PORT;
3985 +
3986 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3987 +               if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
3988 +#else
3989 +               if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
3990 +#endif
3991 +                       /* No choice either */
3992 +                       return INVALID_PORT;
3993 +
3994 +               return ntohs(flags & IPSET_SRC ?
3995 +                            udph.source : udph.dest);
3996 +           }
3997 +       default:
3998 +               return INVALID_PORT;
3999 +       }
4000 +}
4001 +
4002 +static inline __u32
4003 +jhash_ip(const struct ip_set_ipporthash *map, uint16_t i, ip_set_ip_t ip)
4004 +{
4005 +       return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
4006 +}
4007 +
4008 +#define HASH_IP(map, ip, port) (port + ((ip - ((map)->first_ip)) << 16))
4009 +
4010 +static inline __u32
4011 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4012 +       ip_set_ip_t *hash_ip)
4013 +{
4014 +       struct ip_set_ipporthash *map =
4015 +               (struct ip_set_ipporthash *) set->data;
4016 +       __u32 id;
4017 +       u_int16_t i;
4018 +       ip_set_ip_t *elem;
4019 +
4020 +       *hash_ip = HASH_IP(map, ip, port);
4021 +       DP("set: %s, ipport:%u.%u.%u.%u:%u, %u.%u.%u.%u",
4022 +          set->name, HIPQUAD(ip), port, HIPQUAD(*hash_ip));
4023 +
4024 +       for (i = 0; i < map->probes; i++) {
4025 +               id = jhash_ip(map, i, *hash_ip) % map->hashsize;
4026 +               DP("hash key: %u", id);
4027 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4028 +               if (*elem == *hash_ip)
4029 +                       return id;
4030 +               /* No shortcut at testing - there can be deleted
4031 +                * entries. */
4032 +       }
4033 +       return UINT_MAX;
4034 +}
4035 +
4036 +static inline int
4037 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4038 +        ip_set_ip_t *hash_ip)
4039 +{
4040 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4041 +
4042 +       if (ip < map->first_ip || ip > map->last_ip)
4043 +               return -ERANGE;
4044 +
4045 +       return (hash_id(set, ip, port, hash_ip) != UINT_MAX);
4046 +}
4047 +
4048 +static int
4049 +testip(struct ip_set *set, const void *data, size_t size,
4050 +       ip_set_ip_t *hash_ip)
4051 +{
4052 +       struct ip_set_req_ipporthash *req =
4053 +           (struct ip_set_req_ipporthash *) data;
4054 +
4055 +       if (size != sizeof(struct ip_set_req_ipporthash)) {
4056 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4057 +                             sizeof(struct ip_set_req_ipporthash),
4058 +                             size);
4059 +               return -EINVAL;
4060 +       }
4061 +       return __testip(set, req->ip, req->port, hash_ip);
4062 +}
4063 +
4064 +static int
4065 +testip_kernel(struct ip_set *set,
4066 +             const struct sk_buff *skb,
4067 +             ip_set_ip_t *hash_ip,
4068 +             const u_int32_t *flags,
4069 +             unsigned char index)
4070 +{
4071 +       ip_set_ip_t port;
4072 +       int res;
4073 +
4074 +       if (flags[index+1] == 0)
4075 +               return 0;
4076 +
4077 +       port = get_port(skb, flags[index+1]);
4078 +
4079 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4080 +          flags[index] & IPSET_SRC ? "SRC" : "DST",
4081 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4082 +          NIPQUAD(ip_hdr(skb)->saddr),
4083 +          NIPQUAD(ip_hdr(skb)->daddr));
4084 +#else
4085 +          NIPQUAD(skb->nh.iph->saddr),
4086 +          NIPQUAD(skb->nh.iph->daddr));
4087 +#endif
4088 +       DP("flag %s port %u",
4089 +          flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4090 +          port);
4091 +       if (port == INVALID_PORT)
4092 +               return 0;
4093 +
4094 +       res =  __testip(set,
4095 +                       ntohl(flags[index] & IPSET_SRC
4096 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4097 +                                       ? ip_hdr(skb)->saddr
4098 +                                       : ip_hdr(skb)->daddr),
4099 +#else
4100 +                                       ? skb->nh.iph->saddr
4101 +                                       : skb->nh.iph->daddr),
4102 +#endif
4103 +                       port,
4104 +                       hash_ip);
4105 +       return (res < 0 ? 0 : res);
4106 +
4107 +}
4108 +
4109 +static inline int
4110 +__add_haship(struct ip_set_ipporthash *map, ip_set_ip_t hash_ip)
4111 +{
4112 +       __u32 probe;
4113 +       u_int16_t i;
4114 +       ip_set_ip_t *elem;
4115 +
4116 +       for (i = 0; i < map->probes; i++) {
4117 +               probe = jhash_ip(map, i, hash_ip) % map->hashsize;
4118 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
4119 +               if (*elem == hash_ip)
4120 +                       return -EEXIST;
4121 +               if (!*elem) {
4122 +                       *elem = hash_ip;
4123 +                       map->elements++;
4124 +                       return 0;
4125 +               }
4126 +       }
4127 +       /* Trigger rehashing */
4128 +       return -EAGAIN;
4129 +}
4130 +
4131 +static inline int
4132 +__addip(struct ip_set_ipporthash *map, ip_set_ip_t ip, ip_set_ip_t port,
4133 +       ip_set_ip_t *hash_ip)
4134 +{
4135 +       if (map->elements > limit)
4136 +               return -ERANGE;
4137 +       if (ip < map->first_ip || ip > map->last_ip)
4138 +               return -ERANGE;
4139 +
4140 +       *hash_ip = HASH_IP(map, ip, port);
4141 +
4142 +       return __add_haship(map, *hash_ip);
4143 +}
4144 +
4145 +static int
4146 +addip(struct ip_set *set, const void *data, size_t size,
4147 +        ip_set_ip_t *hash_ip)
4148 +{
4149 +       struct ip_set_req_ipporthash *req =
4150 +           (struct ip_set_req_ipporthash *) data;
4151 +
4152 +       if (size != sizeof(struct ip_set_req_ipporthash)) {
4153 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4154 +                             sizeof(struct ip_set_req_ipporthash),
4155 +                             size);
4156 +               return -EINVAL;
4157 +       }
4158 +       return __addip((struct ip_set_ipporthash *) set->data,
4159 +                       req->ip, req->port, hash_ip);
4160 +}
4161 +
4162 +static int
4163 +addip_kernel(struct ip_set *set,
4164 +            const struct sk_buff *skb,
4165 +            ip_set_ip_t *hash_ip,
4166 +            const u_int32_t *flags,
4167 +            unsigned char index)
4168 +{
4169 +       ip_set_ip_t port;
4170 +
4171 +       if (flags[index+1] == 0)
4172 +               return -EINVAL;
4173 +
4174 +       port = get_port(skb, flags[index+1]);
4175 +
4176 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4177 +          flags[index] & IPSET_SRC ? "SRC" : "DST",
4178 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4179 +          NIPQUAD(ip_hdr(skb)->saddr),
4180 +          NIPQUAD(ip_hdr(skb)->daddr));
4181 +#else
4182 +          NIPQUAD(skb->nh.iph->saddr),
4183 +          NIPQUAD(skb->nh.iph->daddr));
4184 +#endif
4185 +       DP("flag %s port %u",
4186 +          flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4187 +          port);
4188 +       if (port == INVALID_PORT)
4189 +               return -EINVAL;
4190 +
4191 +       return __addip((struct ip_set_ipporthash *) set->data,
4192 +                      ntohl(flags[index] & IPSET_SRC
4193 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4194 +                               ? ip_hdr(skb)->saddr
4195 +                               : ip_hdr(skb)->daddr),
4196 +#else
4197 +                               ? skb->nh.iph->saddr
4198 +                               : skb->nh.iph->daddr),
4199 +#endif
4200 +                      port,
4201 +                      hash_ip);
4202 +}
4203 +
4204 +static int retry(struct ip_set *set)
4205 +{
4206 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4207 +       ip_set_ip_t *elem;
4208 +       void *members;
4209 +       u_int32_t i, hashsize = map->hashsize;
4210 +       int res;
4211 +       struct ip_set_ipporthash *tmp;
4212 +
4213 +       if (map->resize == 0)
4214 +               return -ERANGE;
4215 +
4216 +    again:
4217 +       res = 0;
4218 +
4219 +       /* Calculate new hash size */
4220 +       hashsize += (hashsize * map->resize)/100;
4221 +       if (hashsize == map->hashsize)
4222 +               hashsize++;
4223 +
4224 +       ip_set_printk("rehashing of set %s triggered: "
4225 +                     "hashsize grows from %u to %u",
4226 +                     set->name, map->hashsize, hashsize);
4227 +
4228 +       tmp = kmalloc(sizeof(struct ip_set_ipporthash)
4229 +                     + map->probes * sizeof(uint32_t), GFP_ATOMIC);
4230 +       if (!tmp) {
4231 +               DP("out of memory for %d bytes",
4232 +                  sizeof(struct ip_set_ipporthash)
4233 +                  + map->probes * sizeof(uint32_t));
4234 +               return -ENOMEM;
4235 +       }
4236 +       tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
4237 +       if (!tmp->members) {
4238 +               DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
4239 +               kfree(tmp);
4240 +               return -ENOMEM;
4241 +       }
4242 +       tmp->hashsize = hashsize;
4243 +       tmp->elements = 0;
4244 +       tmp->probes = map->probes;
4245 +       tmp->resize = map->resize;
4246 +       tmp->first_ip = map->first_ip;
4247 +       tmp->last_ip = map->last_ip;
4248 +       memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
4249 +
4250 +       write_lock_bh(&set->lock);
4251 +       map = (struct ip_set_ipporthash *) set->data; /* Play safe */
4252 +       for (i = 0; i < map->hashsize && res == 0; i++) {
4253 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
4254 +               if (*elem)
4255 +                       res = __add_haship(tmp, *elem);
4256 +       }
4257 +       if (res) {
4258 +               /* Failure, try again */
4259 +               write_unlock_bh(&set->lock);
4260 +               harray_free(tmp->members);
4261 +               kfree(tmp);
4262 +               goto again;
4263 +       }
4264 +
4265 +       /* Success at resizing! */
4266 +       members = map->members;
4267 +
4268 +       map->hashsize = tmp->hashsize;
4269 +       map->members = tmp->members;
4270 +       write_unlock_bh(&set->lock);
4271 +
4272 +       harray_free(members);
4273 +       kfree(tmp);
4274 +
4275 +       return 0;
4276 +}
4277 +
4278 +static inline int
4279 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4280 +       ip_set_ip_t *hash_ip)
4281 +{
4282 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4283 +       ip_set_ip_t id;
4284 +       ip_set_ip_t *elem;
4285 +
4286 +       if (ip < map->first_ip || ip > map->last_ip)
4287 +               return -ERANGE;
4288 +
4289 +       id = hash_id(set, ip, port, hash_ip);
4290 +
4291 +       if (id == UINT_MAX)
4292 +               return -EEXIST;
4293 +
4294 +       elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4295 +       *elem = 0;
4296 +       map->elements--;
4297 +
4298 +       return 0;
4299 +}
4300 +
4301 +static int
4302 +delip(struct ip_set *set, const void *data, size_t size,
4303 +        ip_set_ip_t *hash_ip)
4304 +{
4305 +       struct ip_set_req_ipporthash *req =
4306 +           (struct ip_set_req_ipporthash *) data;
4307 +
4308 +       if (size != sizeof(struct ip_set_req_ipporthash)) {
4309 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4310 +                             sizeof(struct ip_set_req_ipporthash),
4311 +                             size);
4312 +               return -EINVAL;
4313 +       }
4314 +       return __delip(set, req->ip, req->port, hash_ip);
4315 +}
4316 +
4317 +static int
4318 +delip_kernel(struct ip_set *set,
4319 +            const struct sk_buff *skb,
4320 +            ip_set_ip_t *hash_ip,
4321 +            const u_int32_t *flags,
4322 +            unsigned char index)
4323 +{
4324 +       ip_set_ip_t port;
4325 +
4326 +       if (flags[index+1] == 0)
4327 +               return -EINVAL;
4328 +
4329 +       port = get_port(skb, flags[index+1]);
4330 +
4331 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4332 +          flags[index] & IPSET_SRC ? "SRC" : "DST",
4333 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4334 +          NIPQUAD(ip_hdr(skb)->saddr),
4335 +          NIPQUAD(ip_hdr(skb)->daddr));
4336 +#else
4337 +          NIPQUAD(skb->nh.iph->saddr),
4338 +          NIPQUAD(skb->nh.iph->daddr));
4339 +#endif
4340 +       DP("flag %s port %u",
4341 +          flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4342 +          port);
4343 +       if (port == INVALID_PORT)
4344 +               return -EINVAL;
4345 +
4346 +       return __delip(set,
4347 +                      ntohl(flags[index] & IPSET_SRC
4348 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4349 +                               ? ip_hdr(skb)->saddr
4350 +                               : ip_hdr(skb)->daddr),
4351 +#else
4352 +                               ? skb->nh.iph->saddr
4353 +                               : skb->nh.iph->daddr),
4354 +#endif
4355 +                      port,
4356 +                      hash_ip);
4357 +}
4358 +
4359 +static int create(struct ip_set *set, const void *data, size_t size)
4360 +{
4361 +       struct ip_set_req_ipporthash_create *req =
4362 +           (struct ip_set_req_ipporthash_create *) data;
4363 +       struct ip_set_ipporthash *map;
4364 +       uint16_t i;
4365 +
4366 +       if (size != sizeof(struct ip_set_req_ipporthash_create)) {
4367 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4368 +                              sizeof(struct ip_set_req_ipporthash_create),
4369 +                              size);
4370 +               return -EINVAL;
4371 +       }
4372 +
4373 +       if (req->hashsize < 1) {
4374 +               ip_set_printk("hashsize too small");
4375 +               return -ENOEXEC;
4376 +       }
4377 +
4378 +       if (req->probes < 1) {
4379 +               ip_set_printk("probes too small");
4380 +               return -ENOEXEC;
4381 +       }
4382 +
4383 +       map = kmalloc(sizeof(struct ip_set_ipporthash)
4384 +                     + req->probes * sizeof(uint32_t), GFP_KERNEL);
4385 +       if (!map) {
4386 +               DP("out of memory for %d bytes",
4387 +                  sizeof(struct ip_set_ipporthash)
4388 +                  + req->probes * sizeof(uint32_t));
4389 +               return -ENOMEM;
4390 +       }
4391 +       for (i = 0; i < req->probes; i++)
4392 +               get_random_bytes(((uint32_t *) map->initval)+i, 4);
4393 +       map->elements = 0;
4394 +       map->hashsize = req->hashsize;
4395 +       map->probes = req->probes;
4396 +       map->resize = req->resize;
4397 +       map->first_ip = req->from;
4398 +       map->last_ip = req->to;
4399 +       map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
4400 +       if (!map->members) {
4401 +               DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
4402 +               kfree(map);
4403 +               return -ENOMEM;
4404 +       }
4405 +
4406 +       set->data = map;
4407 +       return 0;
4408 +}
4409 +
4410 +static void destroy(struct ip_set *set)
4411 +{
4412 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4413 +
4414 +       harray_free(map->members);
4415 +       kfree(map);
4416 +
4417 +       set->data = NULL;
4418 +}
4419 +
4420 +static void flush(struct ip_set *set)
4421 +{
4422 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4423 +       harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
4424 +       map->elements = 0;
4425 +}
4426 +
4427 +static void list_header(const struct ip_set *set, void *data)
4428 +{
4429 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4430 +       struct ip_set_req_ipporthash_create *header =
4431 +           (struct ip_set_req_ipporthash_create *) data;
4432 +
4433 +       header->hashsize = map->hashsize;
4434 +       header->probes = map->probes;
4435 +       header->resize = map->resize;
4436 +       header->from = map->first_ip;
4437 +       header->to = map->last_ip;
4438 +}
4439 +
4440 +static int list_members_size(const struct ip_set *set)
4441 +{
4442 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4443 +
4444 +       return (map->hashsize * sizeof(ip_set_ip_t));
4445 +}
4446 +
4447 +static void list_members(const struct ip_set *set, void *data)
4448 +{
4449 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4450 +       ip_set_ip_t i, *elem;
4451 +
4452 +       for (i = 0; i < map->hashsize; i++) {
4453 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
4454 +               ((ip_set_ip_t *)data)[i] = *elem;
4455 +       }
4456 +}
4457 +
4458 +static struct ip_set_type ip_set_ipporthash = {
4459 +       .typename               = SETTYPE_NAME,
4460 +       .features               = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_DATA_DOUBLE,
4461 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
4462 +       .create                 = &create,
4463 +       .destroy                = &destroy,
4464 +       .flush                  = &flush,
4465 +       .reqsize                = sizeof(struct ip_set_req_ipporthash),
4466 +       .addip                  = &addip,
4467 +       .addip_kernel           = &addip_kernel,
4468 +       .retry                  = &retry,
4469 +       .delip                  = &delip,
4470 +       .delip_kernel           = &delip_kernel,
4471 +       .testip                 = &testip,
4472 +       .testip_kernel          = &testip_kernel,
4473 +       .header_size            = sizeof(struct ip_set_req_ipporthash_create),
4474 +       .list_header            = &list_header,
4475 +       .list_members_size      = &list_members_size,
4476 +       .list_members           = &list_members,
4477 +       .me                     = THIS_MODULE,
4478 +};
4479 +
4480 +MODULE_LICENSE("GPL");
4481 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4482 +MODULE_DESCRIPTION("ipporthash type of IP sets");
4483 +module_param(limit, int, 0600);
4484 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
4485 +
4486 +static int __init ip_set_ipporthash_init(void)
4487 +{
4488 +       return ip_set_register_set_type(&ip_set_ipporthash);
4489 +}
4490 +
4491 +static void __exit ip_set_ipporthash_fini(void)
4492 +{
4493 +       /* FIXME: possible race with ip_set_create() */
4494 +       ip_set_unregister_set_type(&ip_set_ipporthash);
4495 +}
4496 +
4497 +module_init(ip_set_ipporthash_init);
4498 +module_exit(ip_set_ipporthash_fini);
4499 --- /dev/null
4500 +++ b/net/ipv4/netfilter/ip_set_iptree.c
4501 @@ -0,0 +1,612 @@
4502 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4503 + *
4504 + * This program is free software; you can redistribute it and/or modify
4505 + * it under the terms of the GNU General Public License version 2 as
4506 + * published by the Free Software Foundation.
4507 + */
4508 +
4509 +/* Kernel module implementing an IP set type: the iptree type */
4510 +
4511 +#include <linux/version.h>
4512 +#include <linux/module.h>
4513 +#include <linux/ip.h>
4514 +#include <linux/skbuff.h>
4515 +#include <linux/slab.h>
4516 +#include <linux/delay.h>
4517 +#include <linux/netfilter_ipv4/ip_tables.h>
4518 +#include <linux/netfilter_ipv4/ip_set.h>
4519 +#include <linux/errno.h>
4520 +#include <asm/uaccess.h>
4521 +#include <asm/bitops.h>
4522 +#include <linux/spinlock.h>
4523 +
4524 +/* Backward compatibility */
4525 +#ifndef __nocast
4526 +#define __nocast
4527 +#endif
4528 +
4529 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
4530 +
4531 +static int limit = MAX_RANGE;
4532 +
4533 +/* Garbage collection interval in seconds: */
4534 +#define IPTREE_GC_TIME         5*60
4535 +/* Sleep so many milliseconds before trying again
4536 + * to delete the gc timer at destroying/flushing a set */
4537 +#define IPTREE_DESTROY_SLEEP   100
4538 +
4539 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
4540 +static struct kmem_cache *branch_cachep;
4541 +static struct kmem_cache *leaf_cachep;
4542 +#else
4543 +static kmem_cache_t *branch_cachep;
4544 +static kmem_cache_t *leaf_cachep;
4545 +#endif
4546 +
4547 +#if defined(__LITTLE_ENDIAN)
4548 +#define ABCD(a,b,c,d,addrp) do {               \
4549 +       a = ((unsigned char *)addrp)[3];        \
4550 +       b = ((unsigned char *)addrp)[2];        \
4551 +       c = ((unsigned char *)addrp)[1];        \
4552 +       d = ((unsigned char *)addrp)[0];        \
4553 +} while (0)
4554 +#elif defined(__BIG_ENDIAN)
4555 +#define ABCD(a,b,c,d,addrp) do {               \
4556 +       a = ((unsigned char *)addrp)[0];        \
4557 +       b = ((unsigned char *)addrp)[1];        \
4558 +       c = ((unsigned char *)addrp)[2];        \
4559 +       d = ((unsigned char *)addrp)[3];        \
4560 +} while (0)
4561 +#else
4562 +#error "Please fix asm/byteorder.h"
4563 +#endif /* __LITTLE_ENDIAN */
4564 +
4565 +#define TESTIP_WALK(map, elem, branch) do {    \
4566 +       if ((map)->tree[elem]) {                \
4567 +               branch = (map)->tree[elem];     \
4568 +       } else                                  \
4569 +               return 0;                       \
4570 +} while (0)
4571 +
4572 +static inline int
4573 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4574 +{
4575 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4576 +       struct ip_set_iptreeb *btree;
4577 +       struct ip_set_iptreec *ctree;
4578 +       struct ip_set_iptreed *dtree;
4579 +       unsigned char a,b,c,d;
4580 +
4581 +       if (!ip)
4582 +               return -ERANGE;
4583 +
4584 +       *hash_ip = ip;
4585 +       ABCD(a, b, c, d, hash_ip);
4586 +       DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
4587 +       TESTIP_WALK(map, a, btree);
4588 +       TESTIP_WALK(btree, b, ctree);
4589 +       TESTIP_WALK(ctree, c, dtree);
4590 +       DP("%lu %lu", dtree->expires[d], jiffies);
4591 +       return dtree->expires[d]
4592 +              && (!map->timeout
4593 +                  || time_after(dtree->expires[d], jiffies));
4594 +}
4595 +
4596 +static int
4597 +testip(struct ip_set *set, const void *data, size_t size,
4598 +       ip_set_ip_t *hash_ip)
4599 +{
4600 +       struct ip_set_req_iptree *req =
4601 +           (struct ip_set_req_iptree *) data;
4602 +
4603 +       if (size != sizeof(struct ip_set_req_iptree)) {
4604 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4605 +                             sizeof(struct ip_set_req_iptree),
4606 +                             size);
4607 +               return -EINVAL;
4608 +       }
4609 +       return __testip(set, req->ip, hash_ip);
4610 +}
4611 +
4612 +static int
4613 +testip_kernel(struct ip_set *set,
4614 +             const struct sk_buff *skb,
4615 +             ip_set_ip_t *hash_ip,
4616 +             const u_int32_t *flags,
4617 +             unsigned char index)
4618 +{
4619 +       int res;
4620 +
4621 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4622 +          flags[index] & IPSET_SRC ? "SRC" : "DST",
4623 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4624 +          NIPQUAD(ip_hdr(skb)->saddr),
4625 +          NIPQUAD(ip_hdr(skb)->daddr));
4626 +#else
4627 +          NIPQUAD(skb->nh.iph->saddr),
4628 +          NIPQUAD(skb->nh.iph->daddr));
4629 +#endif
4630 +
4631 +       res =  __testip(set,
4632 +                       ntohl(flags[index] & IPSET_SRC
4633 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4634 +                               ? ip_hdr(skb)->saddr
4635 +                               : ip_hdr(skb)->daddr),
4636 +#else
4637 +                               ? skb->nh.iph->saddr
4638 +                               : skb->nh.iph->daddr),
4639 +#endif
4640 +                       hash_ip);
4641 +       return (res < 0 ? 0 : res);
4642 +}
4643 +
4644 +#define ADDIP_WALK(map, elem, branch, type, cachep) do {       \
4645 +       if ((map)->tree[elem]) {                                \
4646 +               DP("found %u", elem);                           \
4647 +               branch = (map)->tree[elem];                     \
4648 +       } else {                                                \
4649 +               branch = (type *)                               \
4650 +                       kmem_cache_alloc(cachep, GFP_ATOMIC);   \
4651 +               if (branch == NULL)                             \
4652 +                       return -ENOMEM;                         \
4653 +               memset(branch, 0, sizeof(*branch));             \
4654 +               (map)->tree[elem] = branch;                     \
4655 +               DP("alloc %u", elem);                           \
4656 +       }                                                       \
4657 +} while (0)
4658 +
4659 +static inline int
4660 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
4661 +       ip_set_ip_t *hash_ip)
4662 +{
4663 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4664 +       struct ip_set_iptreeb *btree;
4665 +       struct ip_set_iptreec *ctree;
4666 +       struct ip_set_iptreed *dtree;
4667 +       unsigned char a,b,c,d;
4668 +       int ret = 0;
4669 +
4670 +       if (!ip || map->elements >= limit)
4671 +               /* We could call the garbage collector
4672 +                * but it's probably overkill */
4673 +               return -ERANGE;
4674 +
4675 +       *hash_ip = ip;
4676 +       ABCD(a, b, c, d, hash_ip);
4677 +       DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
4678 +       ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
4679 +       ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
4680 +       ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
4681 +       if (dtree->expires[d]
4682 +           && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4683 +               ret = -EEXIST;
4684 +       dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
4685 +       /* Lottery: I won! */
4686 +       if (dtree->expires[d] == 0)
4687 +               dtree->expires[d] = 1;
4688 +       DP("%u %lu", d, dtree->expires[d]);
4689 +       if (ret == 0)
4690 +               map->elements++;
4691 +       return ret;
4692 +}
4693 +
4694 +static int
4695 +addip(struct ip_set *set, const void *data, size_t size,
4696 +      ip_set_ip_t *hash_ip)
4697 +{
4698 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4699 +       struct ip_set_req_iptree *req =
4700 +               (struct ip_set_req_iptree *) data;
4701 +
4702 +       if (size != sizeof(struct ip_set_req_iptree)) {
4703 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4704 +                             sizeof(struct ip_set_req_iptree),
4705 +                             size);
4706 +               return -EINVAL;
4707 +       }
4708 +       DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
4709 +       return __addip(set, req->ip,
4710 +                      req->timeout ? req->timeout : map->timeout,
4711 +                      hash_ip);
4712 +}
4713 +
4714 +static int
4715 +addip_kernel(struct ip_set *set,
4716 +            const struct sk_buff *skb,
4717 +            ip_set_ip_t *hash_ip,
4718 +            const u_int32_t *flags,
4719 +            unsigned char index)
4720 +{
4721 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4722 +
4723 +       return __addip(set,
4724 +                      ntohl(flags[index] & IPSET_SRC
4725 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4726 +                               ? ip_hdr(skb)->saddr
4727 +                               : ip_hdr(skb)->daddr),
4728 +#else
4729 +                               ? skb->nh.iph->saddr
4730 +                               : skb->nh.iph->daddr),
4731 +#endif
4732 +                      map->timeout,
4733 +                      hash_ip);
4734 +}
4735 +
4736 +#define DELIP_WALK(map, elem, branch) do {     \
4737 +       if ((map)->tree[elem]) {                \
4738 +               branch = (map)->tree[elem];     \
4739 +       } else                                  \
4740 +               return -EEXIST;                 \
4741 +} while (0)
4742 +
4743 +static inline int
4744 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4745 +{
4746 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4747 +       struct ip_set_iptreeb *btree;
4748 +       struct ip_set_iptreec *ctree;
4749 +       struct ip_set_iptreed *dtree;
4750 +       unsigned char a,b,c,d;
4751 +
4752 +       if (!ip)
4753 +               return -ERANGE;
4754 +
4755 +       *hash_ip = ip;
4756 +       ABCD(a, b, c, d, hash_ip);
4757 +       DELIP_WALK(map, a, btree);
4758 +       DELIP_WALK(btree, b, ctree);
4759 +       DELIP_WALK(ctree, c, dtree);
4760 +
4761 +       if (dtree->expires[d]) {
4762 +               dtree->expires[d] = 0;
4763 +               map->elements--;
4764 +               return 0;
4765 +       }
4766 +       return -EEXIST;
4767 +}
4768 +
4769 +static int
4770 +delip(struct ip_set *set, const void *data, size_t size,
4771 +      ip_set_ip_t *hash_ip)
4772 +{
4773 +       struct ip_set_req_iptree *req =
4774 +           (struct ip_set_req_iptree *) data;
4775 +
4776 +       if (size != sizeof(struct ip_set_req_iptree)) {
4777 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4778 +                             sizeof(struct ip_set_req_iptree),
4779 +                             size);
4780 +               return -EINVAL;
4781 +       }
4782 +       return __delip(set, req->ip, hash_ip);
4783 +}
4784 +
4785 +static int
4786 +delip_kernel(struct ip_set *set,
4787 +            const struct sk_buff *skb,
4788 +            ip_set_ip_t *hash_ip,
4789 +            const u_int32_t *flags,
4790 +            unsigned char index)
4791 +{
4792 +       return __delip(set,
4793 +                      ntohl(flags[index] & IPSET_SRC
4794 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4795 +                               ? ip_hdr(skb)->saddr
4796 +                               : ip_hdr(skb)->daddr),
4797 +#else
4798 +                               ? skb->nh.iph->saddr
4799 +                               : skb->nh.iph->daddr),
4800 +#endif
4801 +                      hash_ip);
4802 +}
4803 +
4804 +#define LOOP_WALK_BEGIN(map, i, branch) \
4805 +       for (i = 0; i < 256; i++) {     \
4806 +               if (!(map)->tree[i])    \
4807 +                       continue;       \
4808 +               branch = (map)->tree[i]
4809 +
4810 +#define LOOP_WALK_END }
4811 +
4812 +static void ip_tree_gc(unsigned long ul_set)
4813 +{
4814 +       struct ip_set *set = (void *) ul_set;
4815 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4816 +       struct ip_set_iptreeb *btree;
4817 +       struct ip_set_iptreec *ctree;
4818 +       struct ip_set_iptreed *dtree;
4819 +       unsigned int a,b,c,d;
4820 +       unsigned char i,j,k;
4821 +
4822 +       i = j = k = 0;
4823 +       DP("gc: %s", set->name);
4824 +       write_lock_bh(&set->lock);
4825 +       LOOP_WALK_BEGIN(map, a, btree);
4826 +       LOOP_WALK_BEGIN(btree, b, ctree);
4827 +       LOOP_WALK_BEGIN(ctree, c, dtree);
4828 +       for (d = 0; d < 256; d++) {
4829 +               if (dtree->expires[d]) {
4830 +                       DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4831 +                           a, b, c, d,
4832 +                           dtree->expires[d], jiffies);
4833 +                       if (map->timeout
4834 +                           && time_before(dtree->expires[d], jiffies)) {
4835 +                               dtree->expires[d] = 0;
4836 +                               map->elements--;
4837 +                       } else
4838 +                               k = 1;
4839 +               }
4840 +       }
4841 +       if (k == 0) {
4842 +               DP("gc: %s: leaf %u %u %u empty",
4843 +                   set->name, a, b, c);
4844 +               kmem_cache_free(leaf_cachep, dtree);
4845 +               ctree->tree[c] = NULL;
4846 +       } else {
4847 +               DP("gc: %s: leaf %u %u %u not empty",
4848 +                   set->name, a, b, c);
4849 +               j = 1;
4850 +               k = 0;
4851 +       }
4852 +       LOOP_WALK_END;
4853 +       if (j == 0) {
4854 +               DP("gc: %s: branch %u %u empty",
4855 +                   set->name, a, b);
4856 +               kmem_cache_free(branch_cachep, ctree);
4857 +               btree->tree[b] = NULL;
4858 +       } else {
4859 +               DP("gc: %s: branch %u %u not empty",
4860 +                   set->name, a, b);
4861 +               i = 1;
4862 +               j = k = 0;
4863 +       }
4864 +       LOOP_WALK_END;
4865 +       if (i == 0) {
4866 +               DP("gc: %s: branch %u empty",
4867 +                   set->name, a);
4868 +               kmem_cache_free(branch_cachep, btree);
4869 +               map->tree[a] = NULL;
4870 +       } else {
4871 +               DP("gc: %s: branch %u not empty",
4872 +                   set->name, a);
4873 +               i = j = k = 0;
4874 +       }
4875 +       LOOP_WALK_END;
4876 +       write_unlock_bh(&set->lock);
4877 +
4878 +       map->gc.expires = jiffies + map->gc_interval * HZ;
4879 +       add_timer(&map->gc);
4880 +}
4881 +
4882 +static inline void init_gc_timer(struct ip_set *set)
4883 +{
4884 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4885 +
4886 +       /* Even if there is no timeout for the entries,
4887 +        * we still have to call gc because delete
4888 +        * do not clean up empty branches */
4889 +       map->gc_interval = IPTREE_GC_TIME;
4890 +       init_timer(&map->gc);
4891 +       map->gc.data = (unsigned long) set;
4892 +       map->gc.function = ip_tree_gc;
4893 +       map->gc.expires = jiffies + map->gc_interval * HZ;
4894 +       add_timer(&map->gc);
4895 +}
4896 +
4897 +static int create(struct ip_set *set, const void *data, size_t size)
4898 +{
4899 +       struct ip_set_req_iptree_create *req =
4900 +           (struct ip_set_req_iptree_create *) data;
4901 +       struct ip_set_iptree *map;
4902 +
4903 +       if (size != sizeof(struct ip_set_req_iptree_create)) {
4904 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4905 +                             sizeof(struct ip_set_req_iptree_create),
4906 +                             size);
4907 +               return -EINVAL;
4908 +       }
4909 +
4910 +       map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4911 +       if (!map) {
4912 +               DP("out of memory for %d bytes",
4913 +                  sizeof(struct ip_set_iptree));
4914 +               return -ENOMEM;
4915 +       }
4916 +       memset(map, 0, sizeof(*map));
4917 +       map->timeout = req->timeout;
4918 +       map->elements = 0;
4919 +       set->data = map;
4920 +
4921 +       init_gc_timer(set);
4922 +
4923 +       return 0;
4924 +}
4925 +
4926 +static void __flush(struct ip_set_iptree *map)
4927 +{
4928 +       struct ip_set_iptreeb *btree;
4929 +       struct ip_set_iptreec *ctree;
4930 +       struct ip_set_iptreed *dtree;
4931 +       unsigned int a,b,c;
4932 +
4933 +       LOOP_WALK_BEGIN(map, a, btree);
4934 +       LOOP_WALK_BEGIN(btree, b, ctree);
4935 +       LOOP_WALK_BEGIN(ctree, c, dtree);
4936 +       kmem_cache_free(leaf_cachep, dtree);
4937 +       LOOP_WALK_END;
4938 +       kmem_cache_free(branch_cachep, ctree);
4939 +       LOOP_WALK_END;
4940 +       kmem_cache_free(branch_cachep, btree);
4941 +       LOOP_WALK_END;
4942 +       map->elements = 0;
4943 +}
4944 +
4945 +static void destroy(struct ip_set *set)
4946 +{
4947 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4948 +
4949 +       /* gc might be running */
4950 +       while (!del_timer(&map->gc))
4951 +               msleep(IPTREE_DESTROY_SLEEP);
4952 +       __flush(map);
4953 +       kfree(map);
4954 +       set->data = NULL;
4955 +}
4956 +
4957 +static void flush(struct ip_set *set)
4958 +{
4959 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4960 +       unsigned int timeout = map->timeout;
4961 +
4962 +       /* gc might be running */
4963 +       while (!del_timer(&map->gc))
4964 +               msleep(IPTREE_DESTROY_SLEEP);
4965 +       __flush(map);
4966 +       memset(map, 0, sizeof(*map));
4967 +       map->timeout = timeout;
4968 +
4969 +       init_gc_timer(set);
4970 +}
4971 +
4972 +static void list_header(const struct ip_set *set, void *data)
4973 +{
4974 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4975 +       struct ip_set_req_iptree_create *header =
4976 +           (struct ip_set_req_iptree_create *) data;
4977 +
4978 +       header->timeout = map->timeout;
4979 +}
4980 +
4981 +static int list_members_size(const struct ip_set *set)
4982 +{
4983 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4984 +       struct ip_set_iptreeb *btree;
4985 +       struct ip_set_iptreec *ctree;
4986 +       struct ip_set_iptreed *dtree;
4987 +       unsigned int a,b,c,d;
4988 +       unsigned int count = 0;
4989 +
4990 +       LOOP_WALK_BEGIN(map, a, btree);
4991 +       LOOP_WALK_BEGIN(btree, b, ctree);
4992 +       LOOP_WALK_BEGIN(ctree, c, dtree);
4993 +       for (d = 0; d < 256; d++) {
4994 +               if (dtree->expires[d]
4995 +                   && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4996 +                       count++;
4997 +       }
4998 +       LOOP_WALK_END;
4999 +       LOOP_WALK_END;
5000 +       LOOP_WALK_END;
5001 +
5002 +       DP("members %u", count);
5003 +       return (count * sizeof(struct ip_set_req_iptree));
5004 +}
5005 +
5006 +static void list_members(const struct ip_set *set, void *data)
5007 +{
5008 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5009 +       struct ip_set_iptreeb *btree;
5010 +       struct ip_set_iptreec *ctree;
5011 +       struct ip_set_iptreed *dtree;
5012 +       unsigned int a,b,c,d;
5013 +       size_t offset = 0;
5014 +       struct ip_set_req_iptree *entry;
5015 +
5016 +       LOOP_WALK_BEGIN(map, a, btree);
5017 +       LOOP_WALK_BEGIN(btree, b, ctree);
5018 +       LOOP_WALK_BEGIN(ctree, c, dtree);
5019 +       for (d = 0; d < 256; d++) {
5020 +               if (dtree->expires[d]
5021 +                   && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
5022 +                       entry = (struct ip_set_req_iptree *)(data + offset);
5023 +                       entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
5024 +                       entry->timeout = !map->timeout ? 0
5025 +                               : (dtree->expires[d] - jiffies)/HZ;
5026 +                       offset += sizeof(struct ip_set_req_iptree);
5027 +               }
5028 +       }
5029 +       LOOP_WALK_END;
5030 +       LOOP_WALK_END;
5031 +       LOOP_WALK_END;
5032 +}
5033 +
5034 +static struct ip_set_type ip_set_iptree = {
5035 +       .typename               = SETTYPE_NAME,
5036 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
5037 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
5038 +       .create                 = &create,
5039 +       .destroy                = &destroy,
5040 +       .flush                  = &flush,
5041 +       .reqsize                = sizeof(struct ip_set_req_iptree),
5042 +       .addip                  = &addip,
5043 +       .addip_kernel           = &addip_kernel,
5044 +       .delip                  = &delip,
5045 +       .delip_kernel           = &delip_kernel,
5046 +       .testip                 = &testip,
5047 +       .testip_kernel          = &testip_kernel,
5048 +       .header_size            = sizeof(struct ip_set_req_iptree_create),
5049 +       .list_header            = &list_header,
5050 +       .list_members_size      = &list_members_size,
5051 +       .list_members           = &list_members,
5052 +       .me                     = THIS_MODULE,
5053 +};
5054 +
5055 +MODULE_LICENSE("GPL");
5056 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5057 +MODULE_DESCRIPTION("iptree type of IP sets");
5058 +module_param(limit, int, 0600);
5059 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
5060 +
5061 +static int __init ip_set_iptree_init(void)
5062 +{
5063 +       int ret;
5064 +
5065 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5066 +       branch_cachep = kmem_cache_create("ip_set_iptreeb",
5067 +                               sizeof(struct ip_set_iptreeb),
5068 +                               0, 0, NULL);
5069 +#else
5070 +       branch_cachep = kmem_cache_create("ip_set_iptreeb",
5071 +                               sizeof(struct ip_set_iptreeb),
5072 +                               0, 0, NULL, NULL);
5073 +#endif
5074 +       if (!branch_cachep) {
5075 +               printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
5076 +               ret = -ENOMEM;
5077 +               goto out;
5078 +       }
5079 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5080 +       leaf_cachep = kmem_cache_create("ip_set_iptreed",
5081 +                               sizeof(struct ip_set_iptreed),
5082 +                               0, 0, NULL);
5083 +#else
5084 +       leaf_cachep = kmem_cache_create("ip_set_iptreed",
5085 +                               sizeof(struct ip_set_iptreed),
5086 +                               0, 0, NULL, NULL);
5087 +#endif
5088 +       if (!leaf_cachep) {
5089 +               printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
5090 +               ret = -ENOMEM;
5091 +               goto free_branch;
5092 +       }
5093 +       ret = ip_set_register_set_type(&ip_set_iptree);
5094 +       if (ret == 0)
5095 +               goto out;
5096 +
5097 +       kmem_cache_destroy(leaf_cachep);
5098 +    free_branch:
5099 +       kmem_cache_destroy(branch_cachep);
5100 +    out:
5101 +       return ret;
5102 +}
5103 +
5104 +static void __exit ip_set_iptree_fini(void)
5105 +{
5106 +       /* FIXME: possible race with ip_set_create() */
5107 +       ip_set_unregister_set_type(&ip_set_iptree);
5108 +       kmem_cache_destroy(leaf_cachep);
5109 +       kmem_cache_destroy(branch_cachep);
5110 +}
5111 +
5112 +module_init(ip_set_iptree_init);
5113 +module_exit(ip_set_iptree_fini);
5114 --- /dev/null
5115 +++ b/net/ipv4/netfilter/ip_set_iptreemap.c
5116 @@ -0,0 +1,829 @@
5117 +/* Copyright (C) 2007 Sven Wegener <sven.wegener@stealer.net>
5118 + *
5119 + * This program is free software; you can redistribute it and/or modify it
5120 + * under the terms of the GNU General Public License version 2 as published by
5121 + * the Free Software Foundation.
5122 + */
5123 +
5124 +/* This modules implements the iptreemap ipset type. It uses bitmaps to
5125 + * represent every single IPv4 address as a single bit. The bitmaps are managed
5126 + * in a tree structure, where the first three octets of an addresses are used
5127 + * as an index to find the bitmap and the last octet is used as the bit number.
5128 + */
5129 +
5130 +#include <linux/version.h>
5131 +#include <linux/module.h>
5132 +#include <linux/ip.h>
5133 +#include <linux/skbuff.h>
5134 +#include <linux/slab.h>
5135 +#include <linux/delay.h>
5136 +#include <linux/netfilter_ipv4/ip_tables.h>
5137 +#include <linux/netfilter_ipv4/ip_set.h>
5138 +#include <linux/errno.h>
5139 +#include <asm/uaccess.h>
5140 +#include <asm/bitops.h>
5141 +#include <linux/spinlock.h>
5142 +
5143 +#include <linux/netfilter_ipv4/ip_set_iptreemap.h>
5144 +
5145 +#define IPTREEMAP_DEFAULT_GC_TIME (5 * 60)
5146 +#define IPTREEMAP_DESTROY_SLEEP (100)
5147 +
5148 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
5149 +static struct kmem_cache *cachep_b;
5150 +static struct kmem_cache *cachep_c;
5151 +static struct kmem_cache *cachep_d;
5152 +#else
5153 +static kmem_cache_t *cachep_b;
5154 +static kmem_cache_t *cachep_c;
5155 +static kmem_cache_t *cachep_d;
5156 +#endif
5157 +
5158 +static struct ip_set_iptreemap_d *fullbitmap_d;
5159 +static struct ip_set_iptreemap_c *fullbitmap_c;
5160 +static struct ip_set_iptreemap_b *fullbitmap_b;
5161 +
5162 +#if defined(__LITTLE_ENDIAN)
5163 +#define ABCD(a, b, c, d, addr) \
5164 +       do { \
5165 +               a = ((unsigned char *)addr)[3]; \
5166 +               b = ((unsigned char *)addr)[2]; \
5167 +               c = ((unsigned char *)addr)[1]; \
5168 +               d = ((unsigned char *)addr)[0]; \
5169 +       } while (0)
5170 +#elif defined(__BIG_ENDIAN)
5171 +#define ABCD(a,b,c,d,addrp) do {               \
5172 +       a = ((unsigned char *)addrp)[0];        \
5173 +       b = ((unsigned char *)addrp)[1];        \
5174 +       c = ((unsigned char *)addrp)[2];        \
5175 +       d = ((unsigned char *)addrp)[3];        \
5176 +} while (0)
5177 +#else
5178 +#error "Please fix asm/byteorder.h"
5179 +#endif /* __LITTLE_ENDIAN */
5180 +
5181 +#define TESTIP_WALK(map, elem, branch, full) \
5182 +       do { \
5183 +               branch = (map)->tree[elem]; \
5184 +               if (!branch) \
5185 +                       return 0; \
5186 +               else if (branch == full) \
5187 +                       return 1; \
5188 +       } while (0)
5189 +
5190 +#define ADDIP_WALK(map, elem, branch, type, cachep, full) \
5191 +       do { \
5192 +               branch = (map)->tree[elem]; \
5193 +               if (!branch) { \
5194 +                       branch = (type *) kmem_cache_alloc(cachep, GFP_ATOMIC); \
5195 +                       if (!branch) \
5196 +                               return -ENOMEM; \
5197 +                       memset(branch, 0, sizeof(*branch)); \
5198 +                       (map)->tree[elem] = branch; \
5199 +               } else if (branch == full) { \
5200 +                       return -EEXIST; \
5201 +               } \
5202 +       } while (0)
5203 +
5204 +#define ADDIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free) \
5205 +       for (a = a1; a <= a2; a++) { \
5206 +               branch = (map)->tree[a]; \
5207 +               if (branch != full) { \
5208 +                       if ((a > a1 && a < a2) || (hint)) { \
5209 +                               if (branch) \
5210 +                                       free(branch); \
5211 +                               (map)->tree[a] = full; \
5212 +                               continue; \
5213 +                       } else if (!branch) { \
5214 +                               branch = kmem_cache_alloc(cachep, GFP_ATOMIC); \
5215 +                               if (!branch) \
5216 +                                       return -ENOMEM; \
5217 +                               memset(branch, 0, sizeof(*branch)); \
5218 +                               (map)->tree[a] = branch; \
5219 +                       }
5220 +
5221 +#define ADDIP_RANGE_LOOP_END() \
5222 +               } \
5223 +       }
5224 +
5225 +#define DELIP_WALK(map, elem, branch, cachep, full, flags) \
5226 +       do { \
5227 +               branch = (map)->tree[elem]; \
5228 +               if (!branch) { \
5229 +                       return -EEXIST; \
5230 +               } else if (branch == full) { \
5231 +                       branch = kmem_cache_alloc(cachep, flags); \
5232 +                       if (!branch) \
5233 +                               return -ENOMEM; \
5234 +                       memcpy(branch, full, sizeof(*full)); \
5235 +                       (map)->tree[elem] = branch; \
5236 +               } \
5237 +       } while (0)
5238 +
5239 +#define DELIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free, flags) \
5240 +       for (a = a1; a <= a2; a++) { \
5241 +               branch = (map)->tree[a]; \
5242 +               if (branch) { \
5243 +                       if ((a > a1 && a < a2) || (hint)) { \
5244 +                               if (branch != full) \
5245 +                                       free(branch); \
5246 +                               (map)->tree[a] = NULL; \
5247 +                               continue; \
5248 +                       } else if (branch == full) { \
5249 +                               branch = kmem_cache_alloc(cachep, flags); \
5250 +                               if (!branch) \
5251 +                                       return -ENOMEM; \
5252 +                               memcpy(branch, full, sizeof(*branch)); \
5253 +                               (map)->tree[a] = branch; \
5254 +                       }
5255 +
5256 +#define DELIP_RANGE_LOOP_END() \
5257 +               } \
5258 +       }
5259 +
5260 +#define LOOP_WALK_BEGIN(map, i, branch) \
5261 +       for (i = 0; i < 256; i++) { \
5262 +               branch = (map)->tree[i]; \
5263 +               if (likely(!branch)) \
5264 +                       continue;
5265 +
5266 +#define LOOP_WALK_END() \
5267 +       }
5268 +
5269 +#define LOOP_WALK_BEGIN_GC(map, i, branch, full, cachep, count) \
5270 +       count = -256; \
5271 +       for (i = 0; i < 256; i++) { \
5272 +               branch = (map)->tree[i]; \
5273 +               if (likely(!branch)) \
5274 +                       continue; \
5275 +               count++; \
5276 +               if (branch == full) { \
5277 +                       count++; \
5278 +                       continue; \
5279 +               }
5280 +
5281 +#define LOOP_WALK_END_GC(map, i, branch, full, cachep, count) \
5282 +               if (-256 == count) { \
5283 +                       kmem_cache_free(cachep, branch); \
5284 +                       (map)->tree[i] = NULL; \
5285 +               } else if (256 == count) { \
5286 +                       kmem_cache_free(cachep, branch); \
5287 +                       (map)->tree[i] = full; \
5288 +               } \
5289 +       }
5290 +
5291 +#define LOOP_WALK_BEGIN_COUNT(map, i, branch, inrange, count) \
5292 +       for (i = 0; i < 256; i++) { \
5293 +               if (!(map)->tree[i]) { \
5294 +                       if (inrange) { \
5295 +                               count++; \
5296 +                               inrange = 0; \
5297 +                       } \
5298 +                       continue; \
5299 +               } \
5300 +               branch = (map)->tree[i];
5301 +
5302 +#define LOOP_WALK_END_COUNT() \
5303 +       }
5304 +
5305 +#define MIN(a, b) (a < b ? a : b)
5306 +#define MAX(a, b) (a > b ? a : b)
5307 +
5308 +#define GETVALUE1(a, a1, b1, r) \
5309 +       (a == a1 ? b1 : r)
5310 +
5311 +#define GETVALUE2(a, b, a1, b1, c1, r) \
5312 +       (a == a1 && b == b1 ? c1 : r)
5313 +
5314 +#define GETVALUE3(a, b, c, a1, b1, c1, d1, r) \
5315 +       (a == a1 && b == b1 && c == c1 ? d1 : r)
5316 +
5317 +#define CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2) \
5318 +       ( \
5319 +               GETVALUE1(a, a1, b1, 0) == 0 \
5320 +               && GETVALUE1(a, a2, b2, 255) == 255 \
5321 +               && c1 == 0 \
5322 +               && c2 == 255 \
5323 +               && d1 == 0 \
5324 +               && d2 == 255 \
5325 +       )
5326 +
5327 +#define CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2) \
5328 +       ( \
5329 +               GETVALUE2(a, b, a1, b1, c1, 0) == 0 \
5330 +               && GETVALUE2(a, b, a2, b2, c2, 255) == 255 \
5331 +               && d1 == 0 \
5332 +               && d2 == 255 \
5333 +       )
5334 +
5335 +#define CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2) \
5336 +       ( \
5337 +               GETVALUE3(a, b, c, a1, b1, c1, d1, 0) == 0 \
5338 +               && GETVALUE3(a, b, c, a2, b2, c2, d2, 255) == 255 \
5339 +       )
5340 +
5341 +
5342 +static inline void
5343 +free_d(struct ip_set_iptreemap_d *map)
5344 +{
5345 +       kmem_cache_free(cachep_d, map);
5346 +}
5347 +
5348 +static inline void
5349 +free_c(struct ip_set_iptreemap_c *map)
5350 +{
5351 +       struct ip_set_iptreemap_d *dtree;
5352 +       unsigned int i;
5353 +
5354 +       LOOP_WALK_BEGIN(map, i, dtree) {
5355 +               if (dtree != fullbitmap_d)
5356 +                       free_d(dtree);
5357 +       } LOOP_WALK_END();
5358 +
5359 +       kmem_cache_free(cachep_c, map);
5360 +}
5361 +
5362 +static inline void
5363 +free_b(struct ip_set_iptreemap_b *map)
5364 +{
5365 +       struct ip_set_iptreemap_c *ctree;
5366 +       unsigned int i;
5367 +
5368 +       LOOP_WALK_BEGIN(map, i, ctree) {
5369 +               if (ctree != fullbitmap_c)
5370 +                       free_c(ctree);
5371 +       } LOOP_WALK_END();
5372 +
5373 +       kmem_cache_free(cachep_b, map);
5374 +}
5375 +
5376 +static inline int
5377 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5378 +{
5379 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5380 +       struct ip_set_iptreemap_b *btree;
5381 +       struct ip_set_iptreemap_c *ctree;
5382 +       struct ip_set_iptreemap_d *dtree;
5383 +       unsigned char a, b, c, d;
5384 +
5385 +       *hash_ip = ip;
5386 +
5387 +       ABCD(a, b, c, d, hash_ip);
5388 +
5389 +       TESTIP_WALK(map, a, btree, fullbitmap_b);
5390 +       TESTIP_WALK(btree, b, ctree, fullbitmap_c);
5391 +       TESTIP_WALK(ctree, c, dtree, fullbitmap_d);
5392 +
5393 +       return !!test_bit(d, (void *) dtree->bitmap);
5394 +}
5395 +
5396 +static int
5397 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5398 +{
5399 +       struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5400 +
5401 +       if (size != sizeof(struct ip_set_req_iptreemap)) {
5402 +               ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5403 +               return -EINVAL;
5404 +       }
5405 +
5406 +       return __testip(set, req->start, hash_ip);
5407 +}
5408 +
5409 +static int
5410 +testip_kernel(struct ip_set *set, const struct sk_buff *skb, ip_set_ip_t *hash_ip, const u_int32_t *flags, unsigned char index)
5411 +{
5412 +       int res;
5413 +
5414 +       res = __testip(set,
5415 +                      ntohl(flags[index] & IPSET_SRC
5416 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5417 +                               ? ip_hdr(skb)->saddr
5418 +                               : ip_hdr(skb)->daddr),
5419 +#else
5420 +                               ? skb->nh.iph->saddr
5421 +                               : skb->nh.iph->daddr),
5422 +#endif
5423 +                      hash_ip);
5424 +
5425 +       return (res < 0 ? 0 : res);
5426 +}
5427 +
5428 +static inline int
5429 +__addip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5430 +{
5431 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5432 +       struct ip_set_iptreemap_b *btree;
5433 +       struct ip_set_iptreemap_c *ctree;
5434 +       struct ip_set_iptreemap_d *dtree;
5435 +       unsigned char a, b, c, d;
5436 +
5437 +       *hash_ip = ip;
5438 +
5439 +       ABCD(a, b, c, d, hash_ip);
5440 +
5441 +       ADDIP_WALK(map, a, btree, struct ip_set_iptreemap_b, cachep_b, fullbitmap_b);
5442 +       ADDIP_WALK(btree, b, ctree, struct ip_set_iptreemap_c, cachep_c, fullbitmap_c);
5443 +       ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreemap_d, cachep_d, fullbitmap_d);
5444 +
5445 +       if (test_and_set_bit(d, (void *) dtree->bitmap))
5446 +               return -EEXIST;
5447 +
5448 +       set_bit(b, (void *) btree->dirty);
5449 +
5450 +       return 0;
5451 +}
5452 +
5453 +static inline int
5454 +__addip_range(struct ip_set *set, ip_set_ip_t start, ip_set_ip_t end, ip_set_ip_t *hash_ip)
5455 +{
5456 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5457 +       struct ip_set_iptreemap_b *btree;
5458 +       struct ip_set_iptreemap_c *ctree;
5459 +       struct ip_set_iptreemap_d *dtree;
5460 +       unsigned int a, b, c, d;
5461 +       unsigned char a1, b1, c1, d1;
5462 +       unsigned char a2, b2, c2, d2;
5463 +
5464 +       if (start == end)
5465 +               return __addip_single(set, start, hash_ip);
5466 +
5467 +       *hash_ip = start;
5468 +
5469 +       ABCD(a1, b1, c1, d1, &start);
5470 +       ABCD(a2, b2, c2, d2, &end);
5471 +
5472 +       /* This is sooo ugly... */
5473 +       ADDIP_RANGE_LOOP(map, a, a1, a2, CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2), btree, fullbitmap_b, cachep_b, free_b) {
5474 +               ADDIP_RANGE_LOOP(btree, b, GETVALUE1(a, a1, b1, 0), GETVALUE1(a, a2, b2, 255), CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2), ctree, fullbitmap_c, cachep_c, free_c) {
5475 +                       ADDIP_RANGE_LOOP(ctree, c, GETVALUE2(a, b, a1, b1, c1, 0), GETVALUE2(a, b, a2, b2, c2, 255), CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2), dtree, fullbitmap_d, cachep_d, free_d) {
5476 +                               for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5477 +                                       set_bit(d, (void *) dtree->bitmap);
5478 +                               set_bit(b, (void *) btree->dirty);
5479 +                       } ADDIP_RANGE_LOOP_END();
5480 +               } ADDIP_RANGE_LOOP_END();
5481 +       } ADDIP_RANGE_LOOP_END();
5482 +
5483 +       return 0;
5484 +}
5485 +
5486 +static int
5487 +addip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5488 +{
5489 +       struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5490 +
5491 +       if (size != sizeof(struct ip_set_req_iptreemap)) {
5492 +               ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5493 +               return -EINVAL;
5494 +       }
5495 +
5496 +       return __addip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip);
5497 +}
5498 +
5499 +static int
5500 +addip_kernel(struct ip_set *set, const struct sk_buff *skb, ip_set_ip_t *hash_ip, const u_int32_t *flags, unsigned char index)
5501 +{
5502 +
5503 +       return __addip_single(set,
5504 +                       ntohl(flags[index] & IPSET_SRC
5505 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5506 +                               ? ip_hdr(skb)->saddr
5507 +                               : ip_hdr(skb)->daddr),
5508 +#else
5509 +                               ? skb->nh.iph->saddr
5510 +                               : skb->nh.iph->daddr),
5511 +#endif
5512 +                       hash_ip);
5513 +}
5514 +
5515 +static inline int
5516 +__delip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip, unsigned int __nocast flags)
5517 +{
5518 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5519 +       struct ip_set_iptreemap_b *btree;
5520 +       struct ip_set_iptreemap_c *ctree;
5521 +       struct ip_set_iptreemap_d *dtree;
5522 +       unsigned char a,b,c,d;
5523 +
5524 +       *hash_ip = ip;
5525 +
5526 +       ABCD(a, b, c, d, hash_ip);
5527 +
5528 +       DELIP_WALK(map, a, btree, cachep_b, fullbitmap_b, flags);
5529 +       DELIP_WALK(btree, b, ctree, cachep_c, fullbitmap_c, flags);
5530 +       DELIP_WALK(ctree, c, dtree, cachep_d, fullbitmap_d, flags);
5531 +
5532 +       if (!test_and_clear_bit(d, (void *) dtree->bitmap))
5533 +               return -EEXIST;
5534 +
5535 +       set_bit(b, (void *) btree->dirty);
5536 +
5537 +       return 0;
5538 +}
5539 +
5540 +static inline int
5541 +__delip_range(struct ip_set *set, ip_set_ip_t start, ip_set_ip_t end, ip_set_ip_t *hash_ip, unsigned int __nocast flags)
5542 +{
5543 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5544 +       struct ip_set_iptreemap_b *btree;
5545 +       struct ip_set_iptreemap_c *ctree;
5546 +       struct ip_set_iptreemap_d *dtree;
5547 +       unsigned int a, b, c, d;
5548 +       unsigned char a1, b1, c1, d1;
5549 +       unsigned char a2, b2, c2, d2;
5550 +
5551 +       if (start == end)
5552 +               return __delip_single(set, start, hash_ip, flags);
5553 +
5554 +       *hash_ip = start;
5555 +
5556 +       ABCD(a1, b1, c1, d1, &start);
5557 +       ABCD(a2, b2, c2, d2, &end);
5558 +
5559 +       /* This is sooo ugly... */
5560 +       DELIP_RANGE_LOOP(map, a, a1, a2, CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2), btree, fullbitmap_b, cachep_b, free_b, flags) {
5561 +               DELIP_RANGE_LOOP(btree, b, GETVALUE1(a, a1, b1, 0), GETVALUE1(a, a2, b2, 255), CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2), ctree, fullbitmap_c, cachep_c, free_c, flags) {
5562 +                       DELIP_RANGE_LOOP(ctree, c, GETVALUE2(a, b, a1, b1, c1, 0), GETVALUE2(a, b, a2, b2, c2, 255), CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2), dtree, fullbitmap_d, cachep_d, free_d, flags) {
5563 +                               for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5564 +                                       clear_bit(d, (void *) dtree->bitmap);
5565 +                               set_bit(b, (void *) btree->dirty);
5566 +                       } DELIP_RANGE_LOOP_END();
5567 +               } DELIP_RANGE_LOOP_END();
5568 +       } DELIP_RANGE_LOOP_END();
5569 +
5570 +       return 0;
5571 +}
5572 +
5573 +static int
5574 +delip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5575 +{
5576 +       struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5577 +
5578 +       if (size != sizeof(struct ip_set_req_iptreemap)) {
5579 +               ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5580 +               return -EINVAL;
5581 +       }
5582 +
5583 +       return __delip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip, GFP_KERNEL);
5584 +}
5585 +
5586 +static int
5587 +delip_kernel(struct ip_set *set, const struct sk_buff *skb, ip_set_ip_t *hash_ip, const u_int32_t *flags, unsigned char index)
5588 +{
5589 +       return __delip_single(set,
5590 +                       ntohl(flags[index] & IPSET_SRC
5591 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5592 +                               ? ip_hdr(skb)->saddr
5593 +                               : ip_hdr(skb)->daddr),
5594 +#else
5595 +                               ? skb->nh.iph->saddr
5596 +                               : skb->nh.iph->daddr),
5597 +#endif
5598 +                       hash_ip,
5599 +                       GFP_ATOMIC);
5600 +}
5601 +
5602 +/* Check the status of the bitmap
5603 + * -1 == all bits cleared
5604 + *  1 == all bits set
5605 + *  0 == anything else
5606 + */
5607 +static inline int
5608 +bitmap_status(struct ip_set_iptreemap_d *dtree)
5609 +{
5610 +       unsigned char first = dtree->bitmap[0];
5611 +       int a;
5612 +
5613 +       for (a = 1; a < 32; a++)
5614 +               if (dtree->bitmap[a] != first)
5615 +                       return 0;
5616 +
5617 +       return (first == 0 ? -1 : (first == 255 ? 1 : 0));
5618 +}
5619 +
5620 +static void
5621 +gc(unsigned long addr)
5622 +{
5623 +       struct ip_set *set = (struct ip_set *) addr;
5624 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5625 +       struct ip_set_iptreemap_b *btree;
5626 +       struct ip_set_iptreemap_c *ctree;
5627 +       struct ip_set_iptreemap_d *dtree;
5628 +       unsigned int a, b, c;
5629 +       int i, j, k;
5630 +
5631 +       write_lock_bh(&set->lock);
5632 +
5633 +       LOOP_WALK_BEGIN_GC(map, a, btree, fullbitmap_b, cachep_b, i) {
5634 +               LOOP_WALK_BEGIN_GC(btree, b, ctree, fullbitmap_c, cachep_c, j) {
5635 +                       if (!test_and_clear_bit(b, (void *) btree->dirty))
5636 +                               continue;
5637 +                       LOOP_WALK_BEGIN_GC(ctree, c, dtree, fullbitmap_d, cachep_d, k) {
5638 +                               switch (bitmap_status(dtree)) {
5639 +                                       case -1:
5640 +                                               kmem_cache_free(cachep_d, dtree);
5641 +                                               ctree->tree[c] = NULL;
5642 +                                               k--;
5643 +                                       break;
5644 +                                       case 1:
5645 +                                               kmem_cache_free(cachep_d, dtree);
5646 +                                               ctree->tree[c] = fullbitmap_d;
5647 +                                               k++;
5648 +                                       break;
5649 +                               }
5650 +                       } LOOP_WALK_END();
5651 +               } LOOP_WALK_END_GC(btree, b, ctree, fullbitmap_c, cachep_c, k);
5652 +       } LOOP_WALK_END_GC(map, a, btree, fullbitmap_b, cachep_b, j);
5653 +
5654 +       write_unlock_bh(&set->lock);
5655 +
5656 +       map->gc.expires = jiffies + map->gc_interval * HZ;
5657 +       add_timer(&map->gc);
5658 +}
5659 +
5660 +static inline void
5661 +init_gc_timer(struct ip_set *set)
5662 +{
5663 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5664 +
5665 +       init_timer(&map->gc);
5666 +       map->gc.data = (unsigned long) set;
5667 +       map->gc.function = gc;
5668 +       map->gc.expires = jiffies + map->gc_interval * HZ;
5669 +       add_timer(&map->gc);
5670 +}
5671 +
5672 +static int create(struct ip_set *set, const void *data, size_t size)
5673 +{
5674 +       struct ip_set_req_iptreemap_create *req = (struct ip_set_req_iptreemap_create *) data;
5675 +       struct ip_set_iptreemap *map;
5676 +
5677 +       if (size != sizeof(struct ip_set_req_iptreemap_create)) {
5678 +               ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap_create), size);
5679 +               return -EINVAL;
5680 +       }
5681 +
5682 +       map = kzalloc(sizeof(*map), GFP_KERNEL);
5683 +       if (!map)
5684 +               return -ENOMEM;
5685 +
5686 +       map->gc_interval = req->gc_interval ? req->gc_interval : IPTREEMAP_DEFAULT_GC_TIME;
5687 +       set->data = map;
5688 +
5689 +       init_gc_timer(set);
5690 +
5691 +       return 0;
5692 +}
5693 +
5694 +static inline void __flush(struct ip_set_iptreemap *map)
5695 +{
5696 +       struct ip_set_iptreemap_b *btree;
5697 +       unsigned int a;
5698 +
5699 +       LOOP_WALK_BEGIN(map, a, btree);
5700 +               if (btree != fullbitmap_b)
5701 +                       free_b(btree);
5702 +       LOOP_WALK_END();
5703 +}
5704 +
5705 +static void destroy(struct ip_set *set)
5706 +{
5707 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5708 +
5709 +       while (!del_timer(&map->gc))
5710 +               msleep(IPTREEMAP_DESTROY_SLEEP);
5711 +
5712 +       __flush(map);
5713 +       kfree(map);
5714 +
5715 +       set->data = NULL;
5716 +}
5717 +
5718 +static void flush(struct ip_set *set)
5719 +{
5720 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5721 +
5722 +       while (!del_timer(&map->gc))
5723 +               msleep(IPTREEMAP_DESTROY_SLEEP);
5724 +
5725 +       __flush(map);
5726 +
5727 +       memset(map, 0, sizeof(*map));
5728 +
5729 +       init_gc_timer(set);
5730 +}
5731 +
5732 +static void list_header(const struct ip_set *set, void *data)
5733 +{
5734 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5735 +       struct ip_set_req_iptreemap_create *header = (struct ip_set_req_iptreemap_create *) data;
5736 +
5737 +       header->gc_interval = map->gc_interval;
5738 +}
5739 +
5740 +static int list_members_size(const struct ip_set *set)
5741 +{
5742 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5743 +       struct ip_set_iptreemap_b *btree;
5744 +       struct ip_set_iptreemap_c *ctree;
5745 +       struct ip_set_iptreemap_d *dtree;
5746 +       unsigned int a, b, c, d, inrange = 0, count = 0;
5747 +
5748 +       LOOP_WALK_BEGIN_COUNT(map, a, btree, inrange, count) {
5749 +               LOOP_WALK_BEGIN_COUNT(btree, b, ctree, inrange, count) {
5750 +                       LOOP_WALK_BEGIN_COUNT(ctree, c, dtree, inrange, count) {
5751 +                               for (d = 0; d < 256; d++) {
5752 +                                       if (test_bit(d, (void *) dtree->bitmap)) {
5753 +                                               inrange = 1;
5754 +                                       } else if (inrange) {
5755 +                                               count++;
5756 +                                               inrange = 0;
5757 +                                       }
5758 +                               }
5759 +                       } LOOP_WALK_END_COUNT();
5760 +               } LOOP_WALK_END_COUNT();
5761 +       } LOOP_WALK_END_COUNT();
5762 +
5763 +       if (inrange)
5764 +               count++;
5765 +
5766 +       return (count * sizeof(struct ip_set_req_iptreemap));
5767 +}
5768 +
5769 +static inline size_t add_member(void *data, size_t offset, ip_set_ip_t start, ip_set_ip_t end)
5770 +{
5771 +       struct ip_set_req_iptreemap *entry = (struct ip_set_req_iptreemap *) (data + offset);
5772 +
5773 +       entry->start = start;
5774 +       entry->end = end;
5775 +
5776 +       return sizeof(*entry);
5777 +}
5778 +
5779 +static void list_members(const struct ip_set *set, void *data)
5780 +{
5781 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5782 +       struct ip_set_iptreemap_b *btree;
5783 +       struct ip_set_iptreemap_c *ctree;
5784 +       struct ip_set_iptreemap_d *dtree;
5785 +       unsigned int a, b, c, d, inrange = 0;
5786 +       size_t offset = 0;
5787 +       ip_set_ip_t start = 0, end = 0, ip;
5788 +
5789 +       LOOP_WALK_BEGIN(map, a, btree) {
5790 +               LOOP_WALK_BEGIN(btree, b, ctree) {
5791 +                       LOOP_WALK_BEGIN(ctree, c, dtree) {
5792 +                               for (d = 0; d < 256; d++) {
5793 +                                       if (test_bit(d, (void *) dtree->bitmap)) {
5794 +                                               ip = ((a << 24) | (b << 16) | (c << 8) | d);
5795 +                                               if (!inrange) {
5796 +                                                       inrange = 1;
5797 +                                                       start = ip;
5798 +                                               } else if (end < ip - 1) {
5799 +                                                       offset += add_member(data, offset, start, end);
5800 +                                                       start = ip;
5801 +                                               }
5802 +                                               end = ip;
5803 +                                       } else if (inrange) {
5804 +                                               offset += add_member(data, offset, start, end);
5805 +                                               inrange = 0;
5806 +                                       }
5807 +                               }
5808 +                       } LOOP_WALK_END();
5809 +               } LOOP_WALK_END();
5810 +       } LOOP_WALK_END();
5811 +
5812 +       if (inrange)
5813 +               add_member(data, offset, start, end);
5814 +}
5815 +
5816 +static struct ip_set_type ip_set_iptreemap = {
5817 +       .typename               = SETTYPE_NAME,
5818 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
5819 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
5820 +       .create                 = create,
5821 +       .destroy                = destroy,
5822 +       .flush                  = flush,
5823 +       .reqsize                = sizeof(struct ip_set_req_iptreemap),
5824 +       .addip                  = addip,
5825 +       .addip_kernel           = addip_kernel,
5826 +       .delip                  = delip,
5827 +       .delip_kernel           = delip_kernel,
5828 +       .testip                 = testip,
5829 +       .testip_kernel          = testip_kernel,
5830 +       .header_size            = sizeof(struct ip_set_req_iptreemap_create),
5831 +       .list_header            = list_header,
5832 +       .list_members_size      = list_members_size,
5833 +       .list_members           = list_members,
5834 +       .me                     = THIS_MODULE,
5835 +};
5836 +
5837 +MODULE_LICENSE("GPL");
5838 +MODULE_AUTHOR("Sven Wegener <sven.wegener@stealer.net>");
5839 +MODULE_DESCRIPTION("iptreemap type of IP sets");
5840 +
5841 +static int __init ip_set_iptreemap_init(void)
5842 +{
5843 +       int ret = -ENOMEM;
5844 +       int a;
5845 +
5846 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5847 +       cachep_b = kmem_cache_create("ip_set_iptreemap_b",
5848 +                                    sizeof(struct ip_set_iptreemap_b),
5849 +                                    0, 0, NULL);
5850 +#else
5851 +       cachep_b = kmem_cache_create("ip_set_iptreemap_b",
5852 +                                    sizeof(struct ip_set_iptreemap_b),
5853 +                                    0, 0, NULL, NULL);
5854 +#endif
5855 +       if (!cachep_b) {
5856 +               ip_set_printk("Unable to create ip_set_iptreemap_b slab cache");
5857 +               goto out;
5858 +       }
5859 +
5860 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5861 +       cachep_c = kmem_cache_create("ip_set_iptreemap_c",
5862 +                                    sizeof(struct ip_set_iptreemap_c),
5863 +                                    0, 0, NULL);
5864 +#else
5865 +       cachep_c = kmem_cache_create("ip_set_iptreemap_c",
5866 +                                    sizeof(struct ip_set_iptreemap_c),
5867 +                                    0, 0, NULL, NULL);
5868 +#endif
5869 +       if (!cachep_c) {
5870 +               ip_set_printk("Unable to create ip_set_iptreemap_c slab cache");
5871 +               goto outb;
5872 +       }
5873 +
5874 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5875 +       cachep_d = kmem_cache_create("ip_set_iptreemap_d",
5876 +                                    sizeof(struct ip_set_iptreemap_d),
5877 +                                    0, 0, NULL);
5878 +#else
5879 +       cachep_d = kmem_cache_create("ip_set_iptreemap_d",
5880 +                                    sizeof(struct ip_set_iptreemap_d),
5881 +                                    0, 0, NULL, NULL);
5882 +#endif
5883 +       if (!cachep_d) {
5884 +               ip_set_printk("Unable to create ip_set_iptreemap_d slab cache");
5885 +               goto outc;
5886 +       }
5887 +
5888 +       fullbitmap_d = kmem_cache_alloc(cachep_d, GFP_KERNEL);
5889 +       if (!fullbitmap_d)
5890 +               goto outd;
5891 +
5892 +       fullbitmap_c = kmem_cache_alloc(cachep_c, GFP_KERNEL);
5893 +       if (!fullbitmap_c)
5894 +               goto outbitmapd;
5895 +
5896 +       fullbitmap_b = kmem_cache_alloc(cachep_b, GFP_KERNEL);
5897 +       if (!fullbitmap_b)
5898 +               goto outbitmapc;
5899 +
5900 +       ret = ip_set_register_set_type(&ip_set_iptreemap);
5901 +       if (0 > ret)
5902 +               goto outbitmapb;
5903 +
5904 +       /* Now init our global bitmaps */
5905 +       memset(fullbitmap_d->bitmap, 0xff, sizeof(fullbitmap_d->bitmap));
5906 +
5907 +       for (a = 0; a < 256; a++)
5908 +               fullbitmap_c->tree[a] = fullbitmap_d;
5909 +
5910 +       for (a = 0; a < 256; a++)
5911 +               fullbitmap_b->tree[a] = fullbitmap_c;
5912 +       memset(fullbitmap_b->dirty, 0, sizeof(fullbitmap_b->dirty));
5913 +
5914 +       return 0;
5915 +
5916 +outbitmapb:
5917 +       kmem_cache_free(cachep_b, fullbitmap_b);
5918 +outbitmapc:
5919 +       kmem_cache_free(cachep_c, fullbitmap_c);
5920 +outbitmapd:
5921 +       kmem_cache_free(cachep_d, fullbitmap_d);
5922 +outd:
5923 +       kmem_cache_destroy(cachep_d);
5924 +outc:
5925 +       kmem_cache_destroy(cachep_c);
5926 +outb:
5927 +       kmem_cache_destroy(cachep_b);
5928 +out:
5929 +
5930 +       return ret;
5931 +}
5932 +
5933 +static void __exit ip_set_iptreemap_fini(void)
5934 +{
5935 +       ip_set_unregister_set_type(&ip_set_iptreemap);
5936 +       kmem_cache_free(cachep_d, fullbitmap_d);
5937 +       kmem_cache_free(cachep_c, fullbitmap_c);
5938 +       kmem_cache_free(cachep_b, fullbitmap_b);
5939 +       kmem_cache_destroy(cachep_d);
5940 +       kmem_cache_destroy(cachep_c);
5941 +       kmem_cache_destroy(cachep_b);
5942 +}
5943 +
5944 +module_init(ip_set_iptreemap_init);
5945 +module_exit(ip_set_iptreemap_fini);
5946 --- /dev/null
5947 +++ b/net/ipv4/netfilter/ip_set_macipmap.c
5948 @@ -0,0 +1,375 @@
5949 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5950 + *                         Patrick Schaaf <bof@bof.de>
5951 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
5952 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5953 + *
5954 + * This program is free software; you can redistribute it and/or modify
5955 + * it under the terms of the GNU General Public License version 2 as
5956 + * published by the Free Software Foundation.
5957 + */
5958 +
5959 +/* Kernel module implementing an IP set type: the macipmap type */
5960 +
5961 +#include <linux/module.h>
5962 +#include <linux/ip.h>
5963 +#include <linux/skbuff.h>
5964 +#include <linux/version.h>
5965 +#include <linux/netfilter_ipv4/ip_tables.h>
5966 +#include <linux/netfilter_ipv4/ip_set.h>
5967 +#include <linux/errno.h>
5968 +#include <asm/uaccess.h>
5969 +#include <asm/bitops.h>
5970 +#include <linux/spinlock.h>
5971 +#include <linux/if_ether.h>
5972 +#include <linux/vmalloc.h>
5973 +
5974 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
5975 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
5976 +
5977 +static int
5978 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5979 +{
5980 +       struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
5981 +       struct ip_set_macip *table = (struct ip_set_macip *) map->members;
5982 +       struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
5983 +
5984 +       if (size != sizeof(struct ip_set_req_macipmap)) {
5985 +               ip_set_printk("data length wrong (want %zu, have %zu)",
5986 +                             sizeof(struct ip_set_req_macipmap),
5987 +                             size);
5988 +               return -EINVAL;
5989 +       }
5990 +
5991 +       if (req->ip < map->first_ip || req->ip > map->last_ip)
5992 +               return -ERANGE;
5993 +
5994 +       *hash_ip = req->ip;
5995 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
5996 +          set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
5997 +       if (test_bit(IPSET_MACIP_ISSET,
5998 +                    (void *) &table[req->ip - map->first_ip].flags)) {
5999 +               return (memcmp(req->ethernet,
6000 +                              &table[req->ip - map->first_ip].ethernet,
6001 +                              ETH_ALEN) == 0);
6002 +       } else {
6003 +               return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6004 +       }
6005 +}
6006 +
6007 +static int
6008 +testip_kernel(struct ip_set *set,
6009 +             const struct sk_buff *skb,
6010 +             ip_set_ip_t *hash_ip,
6011 +             const u_int32_t *flags,
6012 +             unsigned char index)
6013 +{
6014 +       struct ip_set_macipmap *map =
6015 +           (struct ip_set_macipmap *) set->data;
6016 +       struct ip_set_macip *table =
6017 +           (struct ip_set_macip *) map->members;
6018 +       ip_set_ip_t ip;
6019 +
6020 +       ip = ntohl(flags[index] & IPSET_SRC
6021 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6022 +                       ? ip_hdr(skb)->saddr
6023 +                       : ip_hdr(skb)->daddr);
6024 +#else
6025 +                       ? skb->nh.iph->saddr
6026 +                       : skb->nh.iph->daddr);
6027 +#endif
6028 +
6029 +       if (ip < map->first_ip || ip > map->last_ip)
6030 +               return 0;
6031 +
6032 +       *hash_ip = ip;
6033 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
6034 +          set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
6035 +       if (test_bit(IPSET_MACIP_ISSET,
6036 +           (void *) &table[ip - map->first_ip].flags)) {
6037 +               /* Is mac pointer valid?
6038 +                * If so, compare... */
6039 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6040 +               return (skb_mac_header(skb) >= skb->head
6041 +                       && (skb_mac_header(skb) + ETH_HLEN) <= skb->data
6042 +#else
6043 +               return (skb->mac.raw >= skb->head
6044 +                       && (skb->mac.raw + ETH_HLEN) <= skb->data
6045 +#endif
6046 +                       && (memcmp(eth_hdr(skb)->h_source,
6047 +                                  &table[ip - map->first_ip].ethernet,
6048 +                                  ETH_ALEN) == 0));
6049 +       } else {
6050 +               return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6051 +       }
6052 +}
6053 +
6054 +/* returns 0 on success */
6055 +static inline int
6056 +__addip(struct ip_set *set,
6057 +       ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
6058 +{
6059 +       struct ip_set_macipmap *map =
6060 +           (struct ip_set_macipmap *) set->data;
6061 +       struct ip_set_macip *table =
6062 +           (struct ip_set_macip *) map->members;
6063 +
6064 +       if (ip < map->first_ip || ip > map->last_ip)
6065 +               return -ERANGE;
6066 +       if (test_and_set_bit(IPSET_MACIP_ISSET,
6067 +                            (void *) &table[ip - map->first_ip].flags))
6068 +               return -EEXIST;
6069 +
6070 +       *hash_ip = ip;
6071 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6072 +       memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
6073 +       return 0;
6074 +}
6075 +
6076 +static int
6077 +addip(struct ip_set *set, const void *data, size_t size,
6078 +      ip_set_ip_t *hash_ip)
6079 +{
6080 +       struct ip_set_req_macipmap *req =
6081 +           (struct ip_set_req_macipmap *) data;
6082 +
6083 +       if (size != sizeof(struct ip_set_req_macipmap)) {
6084 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6085 +                             sizeof(struct ip_set_req_macipmap),
6086 +                             size);
6087 +               return -EINVAL;
6088 +       }
6089 +       return __addip(set, req->ip, req->ethernet, hash_ip);
6090 +}
6091 +
6092 +static int
6093 +addip_kernel(struct ip_set *set,
6094 +            const struct sk_buff *skb,
6095 +            ip_set_ip_t *hash_ip,
6096 +            const u_int32_t *flags,
6097 +            unsigned char index)
6098 +{
6099 +       ip_set_ip_t ip;
6100 +
6101 +       ip = ntohl(flags[index] & IPSET_SRC
6102 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6103 +                       ? ip_hdr(skb)->saddr
6104 +                       : ip_hdr(skb)->daddr);
6105 +#else
6106 +                       ? skb->nh.iph->saddr
6107 +                       : skb->nh.iph->daddr);
6108 +#endif
6109 +
6110 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6111 +       if (!(skb_mac_header(skb) >= skb->head
6112 +             && (skb_mac_header(skb) + ETH_HLEN) <= skb->data))
6113 +#else
6114 +       if (!(skb->mac.raw >= skb->head
6115 +             && (skb->mac.raw + ETH_HLEN) <= skb->data))
6116 +#endif
6117 +               return -EINVAL;
6118 +
6119 +       return __addip(set, ip, eth_hdr(skb)->h_source, hash_ip);
6120 +}
6121 +
6122 +static inline int
6123 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6124 +{
6125 +       struct ip_set_macipmap *map =
6126 +           (struct ip_set_macipmap *) set->data;
6127 +       struct ip_set_macip *table =
6128 +           (struct ip_set_macip *) map->members;
6129 +
6130 +       if (ip < map->first_ip || ip > map->last_ip)
6131 +               return -ERANGE;
6132 +       if (!test_and_clear_bit(IPSET_MACIP_ISSET,
6133 +                               (void *)&table[ip - map->first_ip].flags))
6134 +               return -EEXIST;
6135 +
6136 +       *hash_ip = ip;
6137 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6138 +       return 0;
6139 +}
6140 +
6141 +static int
6142 +delip(struct ip_set *set, const void *data, size_t size,
6143 +     ip_set_ip_t *hash_ip)
6144 +{
6145 +       struct ip_set_req_macipmap *req =
6146 +           (struct ip_set_req_macipmap *) data;
6147 +
6148 +       if (size != sizeof(struct ip_set_req_macipmap)) {
6149 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6150 +                             sizeof(struct ip_set_req_macipmap),
6151 +                             size);
6152 +               return -EINVAL;
6153 +       }
6154 +       return __delip(set, req->ip, hash_ip);
6155 +}
6156 +
6157 +static int
6158 +delip_kernel(struct ip_set *set,
6159 +            const struct sk_buff *skb,
6160 +            ip_set_ip_t *hash_ip,
6161 +            const u_int32_t *flags,
6162 +            unsigned char index)
6163 +{
6164 +       return __delip(set,
6165 +                      ntohl(flags[index] & IPSET_SRC
6166 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6167 +                               ? ip_hdr(skb)->saddr
6168 +                               : ip_hdr(skb)->daddr),
6169 +#else
6170 +                               ? skb->nh.iph->saddr
6171 +                               : skb->nh.iph->daddr),
6172 +#endif
6173 +                      hash_ip);
6174 +}
6175 +
6176 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
6177 +{
6178 +       return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
6179 +}
6180 +
6181 +static int create(struct ip_set *set, const void *data, size_t size)
6182 +{
6183 +       int newbytes;
6184 +       struct ip_set_req_macipmap_create *req =
6185 +           (struct ip_set_req_macipmap_create *) data;
6186 +       struct ip_set_macipmap *map;
6187 +
6188 +       if (size != sizeof(struct ip_set_req_macipmap_create)) {
6189 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6190 +                             sizeof(struct ip_set_req_macipmap_create),
6191 +                             size);
6192 +               return -EINVAL;
6193 +       }
6194 +
6195 +       DP("from %u.%u.%u.%u to %u.%u.%u.%u",
6196 +          HIPQUAD(req->from), HIPQUAD(req->to));
6197 +
6198 +       if (req->from > req->to) {
6199 +               DP("bad ip range");
6200 +               return -ENOEXEC;
6201 +       }
6202 +
6203 +       if (req->to - req->from > MAX_RANGE) {
6204 +               ip_set_printk("range too big (max %d addresses)",
6205 +                              MAX_RANGE+1);
6206 +               return -ENOEXEC;
6207 +       }
6208 +
6209 +       map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
6210 +       if (!map) {
6211 +               DP("out of memory for %d bytes",
6212 +                  sizeof(struct ip_set_macipmap));
6213 +               return -ENOMEM;
6214 +       }
6215 +       map->flags = req->flags;
6216 +       map->first_ip = req->from;
6217 +       map->last_ip = req->to;
6218 +       newbytes = members_size(map->first_ip, map->last_ip);
6219 +       map->members = ip_set_malloc(newbytes);
6220 +       DP("members: %u %p", newbytes, map->members);
6221 +       if (!map->members) {
6222 +               DP("out of memory for %d bytes", newbytes);
6223 +               kfree(map);
6224 +               return -ENOMEM;
6225 +       }
6226 +       memset(map->members, 0, newbytes);
6227 +
6228 +       set->data = map;
6229 +       return 0;
6230 +}
6231 +
6232 +static void destroy(struct ip_set *set)
6233 +{
6234 +       struct ip_set_macipmap *map =
6235 +           (struct ip_set_macipmap *) set->data;
6236 +
6237 +       ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
6238 +       kfree(map);
6239 +
6240 +       set->data = NULL;
6241 +}
6242 +
6243 +static void flush(struct ip_set *set)
6244 +{
6245 +       struct ip_set_macipmap *map =
6246 +           (struct ip_set_macipmap *) set->data;
6247 +       memset(map->members, 0, members_size(map->first_ip, map->last_ip));
6248 +}
6249 +
6250 +static void list_header(const struct ip_set *set, void *data)
6251 +{
6252 +       struct ip_set_macipmap *map =
6253 +           (struct ip_set_macipmap *) set->data;
6254 +       struct ip_set_req_macipmap_create *header =
6255 +           (struct ip_set_req_macipmap_create *) data;
6256 +
6257 +       DP("list_header %x %x %u", map->first_ip, map->last_ip,
6258 +          map->flags);
6259 +
6260 +       header->from = map->first_ip;
6261 +       header->to = map->last_ip;
6262 +       header->flags = map->flags;
6263 +}
6264 +
6265 +static int list_members_size(const struct ip_set *set)
6266 +{
6267 +       struct ip_set_macipmap *map =
6268 +           (struct ip_set_macipmap *) set->data;
6269 +
6270 +       DP("%u", members_size(map->first_ip, map->last_ip));
6271 +       return members_size(map->first_ip, map->last_ip);
6272 +}
6273 +
6274 +static void list_members(const struct ip_set *set, void *data)
6275 +{
6276 +       struct ip_set_macipmap *map =
6277 +           (struct ip_set_macipmap *) set->data;
6278 +
6279 +       int bytes = members_size(map->first_ip, map->last_ip);
6280 +
6281 +       DP("members: %u %p", bytes, map->members);
6282 +       memcpy(data, map->members, bytes);
6283 +}
6284 +
6285 +static struct ip_set_type ip_set_macipmap = {
6286 +       .typename               = SETTYPE_NAME,
6287 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6288 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
6289 +       .create                 = &create,
6290 +       .destroy                = &destroy,
6291 +       .flush                  = &flush,
6292 +       .reqsize                = sizeof(struct ip_set_req_macipmap),
6293 +       .addip                  = &addip,
6294 +       .addip_kernel           = &addip_kernel,
6295 +       .delip                  = &delip,
6296 +       .delip_kernel           = &delip_kernel,
6297 +       .testip                 = &testip,
6298 +       .testip_kernel          = &testip_kernel,
6299 +       .header_size            = sizeof(struct ip_set_req_macipmap_create),
6300 +       .list_header            = &list_header,
6301 +       .list_members_size      = &list_members_size,
6302 +       .list_members           = &list_members,
6303 +       .me                     = THIS_MODULE,
6304 +};
6305 +
6306 +MODULE_LICENSE("GPL");
6307 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6308 +MODULE_DESCRIPTION("macipmap type of IP sets");
6309 +
6310 +static int __init ip_set_macipmap_init(void)
6311 +{
6312 +       init_max_malloc_size();
6313 +       return ip_set_register_set_type(&ip_set_macipmap);
6314 +}
6315 +
6316 +static void __exit ip_set_macipmap_fini(void)
6317 +{
6318 +       /* FIXME: possible race with ip_set_create() */
6319 +       ip_set_unregister_set_type(&ip_set_macipmap);
6320 +}
6321 +
6322 +module_init(ip_set_macipmap_init);
6323 +module_exit(ip_set_macipmap_fini);
6324 --- /dev/null
6325 +++ b/net/ipv4/netfilter/ip_set_nethash.c
6326 @@ -0,0 +1,497 @@
6327 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6328 + *
6329 + * This program is free software; you can redistribute it and/or modify
6330 + * it under the terms of the GNU General Public License version 2 as
6331 + * published by the Free Software Foundation.
6332 + */
6333 +
6334 +/* Kernel module implementing a cidr nethash set */
6335 +
6336 +#include <linux/module.h>
6337 +#include <linux/ip.h>
6338 +#include <linux/skbuff.h>
6339 +#include <linux/version.h>
6340 +#include <linux/jhash.h>
6341 +#include <linux/netfilter_ipv4/ip_tables.h>
6342 +#include <linux/netfilter_ipv4/ip_set.h>
6343 +#include <linux/errno.h>
6344 +#include <asm/uaccess.h>
6345 +#include <asm/bitops.h>
6346 +#include <linux/spinlock.h>
6347 +#include <linux/vmalloc.h>
6348 +#include <linux/random.h>
6349 +
6350 +#include <net/ip.h>
6351 +
6352 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
6353 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
6354 +
6355 +static int limit = MAX_RANGE;
6356 +
6357 +static inline __u32
6358 +jhash_ip(const struct ip_set_nethash *map, uint16_t i, ip_set_ip_t ip)
6359 +{
6360 +       return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
6361 +}
6362 +
6363 +static inline __u32
6364 +hash_id_cidr(struct ip_set_nethash *map,
6365 +            ip_set_ip_t ip,
6366 +            unsigned char cidr,
6367 +            ip_set_ip_t *hash_ip)
6368 +{
6369 +       __u32 id;
6370 +       u_int16_t i;
6371 +       ip_set_ip_t *elem;
6372 +
6373 +       *hash_ip = pack(ip, cidr);
6374 +
6375 +       for (i = 0; i < map->probes; i++) {
6376 +               id = jhash_ip(map, i, *hash_ip) % map->hashsize;
6377 +               DP("hash key: %u", id);
6378 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6379 +               if (*elem == *hash_ip)
6380 +                       return id;
6381 +       }
6382 +       return UINT_MAX;
6383 +}
6384 +
6385 +static inline __u32
6386 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6387 +{
6388 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6389 +       __u32 id = UINT_MAX;
6390 +       int i;
6391 +
6392 +       for (i = 0; i < 30 && map->cidr[i]; i++) {
6393 +               id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
6394 +               if (id != UINT_MAX)
6395 +                       break;
6396 +       }
6397 +       return id;
6398 +}
6399 +
6400 +static inline int
6401 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
6402 +             ip_set_ip_t *hash_ip)
6403 +{
6404 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6405 +
6406 +       return (ip && hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
6407 +}
6408 +
6409 +static inline int
6410 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6411 +{
6412 +       return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
6413 +}
6414 +
6415 +static int
6416 +testip(struct ip_set *set, const void *data, size_t size,
6417 +       ip_set_ip_t *hash_ip)
6418 +{
6419 +       struct ip_set_req_nethash *req =
6420 +           (struct ip_set_req_nethash *) data;
6421 +
6422 +       if (size != sizeof(struct ip_set_req_nethash)) {
6423 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6424 +                             sizeof(struct ip_set_req_nethash),
6425 +                             size);
6426 +               return -EINVAL;
6427 +       }
6428 +       return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
6429 +               : __testip_cidr(set, req->ip, req->cidr, hash_ip));
6430 +}
6431 +
6432 +static int
6433 +testip_kernel(struct ip_set *set,
6434 +             const struct sk_buff *skb,
6435 +             ip_set_ip_t *hash_ip,
6436 +             const u_int32_t *flags,
6437 +             unsigned char index)
6438 +{
6439 +       return __testip(set,
6440 +                       ntohl(flags[index] & IPSET_SRC
6441 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6442 +                               ? ip_hdr(skb)->saddr
6443 +                               : ip_hdr(skb)->daddr),
6444 +#else
6445 +                               ? skb->nh.iph->saddr
6446 +                               : skb->nh.iph->daddr),
6447 +#endif
6448 +                       hash_ip);
6449 +}
6450 +
6451 +static inline int
6452 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
6453 +{
6454 +       __u32 probe;
6455 +       u_int16_t i;
6456 +       ip_set_ip_t *elem;
6457 +
6458 +       for (i = 0; i < map->probes; i++) {
6459 +               probe = jhash_ip(map, i, ip) % map->hashsize;
6460 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
6461 +               if (*elem == ip)
6462 +                       return -EEXIST;
6463 +               if (!*elem) {
6464 +                       *elem = ip;
6465 +                       map->elements++;
6466 +                       return 0;
6467 +               }
6468 +       }
6469 +       /* Trigger rehashing */
6470 +       return -EAGAIN;
6471 +}
6472 +
6473 +static inline int
6474 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6475 +       ip_set_ip_t *hash_ip)
6476 +{
6477 +       if (!ip || map->elements >= limit)
6478 +               return -ERANGE;
6479 +
6480 +       *hash_ip = pack(ip, cidr);
6481 +       DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
6482 +
6483 +       return __addip_base(map, *hash_ip);
6484 +}
6485 +
6486 +static void
6487 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
6488 +{
6489 +       unsigned char next;
6490 +       int i;
6491 +
6492 +       for (i = 0; i < 30 && map->cidr[i]; i++) {
6493 +               if (map->cidr[i] == cidr) {
6494 +                       return;
6495 +               } else if (map->cidr[i] < cidr) {
6496 +                       next = map->cidr[i];
6497 +                       map->cidr[i] = cidr;
6498 +                       cidr = next;
6499 +               }
6500 +       }
6501 +       if (i < 30)
6502 +               map->cidr[i] = cidr;
6503 +}
6504 +
6505 +static int
6506 +addip(struct ip_set *set, const void *data, size_t size,
6507 +        ip_set_ip_t *hash_ip)
6508 +{
6509 +       struct ip_set_req_nethash *req =
6510 +           (struct ip_set_req_nethash *) data;
6511 +       int ret;
6512 +
6513 +       if (size != sizeof(struct ip_set_req_nethash)) {
6514 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6515 +                             sizeof(struct ip_set_req_nethash),
6516 +                             size);
6517 +               return -EINVAL;
6518 +       }
6519 +       ret = __addip((struct ip_set_nethash *) set->data,
6520 +                     req->ip, req->cidr, hash_ip);
6521 +
6522 +       if (ret == 0)
6523 +               update_cidr_sizes((struct ip_set_nethash *) set->data,
6524 +                                 req->cidr);
6525 +
6526 +       return ret;
6527 +}
6528 +
6529 +static int
6530 +addip_kernel(struct ip_set *set,
6531 +            const struct sk_buff *skb,
6532 +            ip_set_ip_t *hash_ip,
6533 +            const u_int32_t *flags,
6534 +            unsigned char index)
6535 +{
6536 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6537 +       int ret = -ERANGE;
6538 +       ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC
6539 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6540 +                                       ? ip_hdr(skb)->saddr
6541 +                                       : ip_hdr(skb)->daddr);
6542 +#else
6543 +                                       ? skb->nh.iph->saddr
6544 +                                       : skb->nh.iph->daddr);
6545 +#endif
6546 +
6547 +       if (map->cidr[0])
6548 +               ret = __addip(map, ip, map->cidr[0], hash_ip);
6549 +
6550 +       return ret;
6551 +}
6552 +
6553 +static int retry(struct ip_set *set)
6554 +{
6555 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6556 +       ip_set_ip_t *elem;
6557 +       void *members;
6558 +       u_int32_t i, hashsize = map->hashsize;
6559 +       int res;
6560 +       struct ip_set_nethash *tmp;
6561 +
6562 +       if (map->resize == 0)
6563 +               return -ERANGE;
6564 +
6565 +    again:
6566 +       res = 0;
6567 +
6568 +       /* Calculate new parameters */
6569 +       hashsize += (hashsize * map->resize)/100;
6570 +       if (hashsize == map->hashsize)
6571 +               hashsize++;
6572 +
6573 +       ip_set_printk("rehashing of set %s triggered: "
6574 +                     "hashsize grows from %u to %u",
6575 +                     set->name, map->hashsize, hashsize);
6576 +
6577 +       tmp = kmalloc(sizeof(struct ip_set_nethash)
6578 +                     + map->probes * sizeof(uint32_t), GFP_ATOMIC);
6579 +       if (!tmp) {
6580 +               DP("out of memory for %d bytes",
6581 +                  sizeof(struct ip_set_nethash)
6582 +                  + map->probes * sizeof(uint32_t));
6583 +               return -ENOMEM;
6584 +       }
6585 +       tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
6586 +       if (!tmp->members) {
6587 +               DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
6588 +               kfree(tmp);
6589 +               return -ENOMEM;
6590 +       }
6591 +       tmp->hashsize = hashsize;
6592 +       tmp->elements = 0;
6593 +       tmp->probes = map->probes;
6594 +       tmp->resize = map->resize;
6595 +       memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
6596 +       memcpy(tmp->cidr, map->cidr, 30 * sizeof(unsigned char));
6597 +
6598 +       write_lock_bh(&set->lock);
6599 +       map = (struct ip_set_nethash *) set->data; /* Play safe */
6600 +       for (i = 0; i < map->hashsize && res == 0; i++) {
6601 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
6602 +               if (*elem)
6603 +                       res = __addip_base(tmp, *elem);
6604 +       }
6605 +       if (res) {
6606 +               /* Failure, try again */
6607 +               write_unlock_bh(&set->lock);
6608 +               harray_free(tmp->members);
6609 +               kfree(tmp);
6610 +               goto again;
6611 +       }
6612 +
6613 +       /* Success at resizing! */
6614 +       members = map->members;
6615 +
6616 +       map->hashsize = tmp->hashsize;
6617 +       map->members = tmp->members;
6618 +       write_unlock_bh(&set->lock);
6619 +
6620 +       harray_free(members);
6621 +       kfree(tmp);
6622 +
6623 +       return 0;
6624 +}
6625 +
6626 +static inline int
6627 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6628 +       ip_set_ip_t *hash_ip)
6629 +{
6630 +       ip_set_ip_t id, *elem;
6631 +
6632 +       if (!ip)
6633 +               return -ERANGE;
6634 +
6635 +       id = hash_id_cidr(map, ip, cidr, hash_ip);
6636 +       if (id == UINT_MAX)
6637 +               return -EEXIST;
6638 +
6639 +       elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6640 +       *elem = 0;
6641 +       map->elements--;
6642 +       return 0;
6643 +}
6644 +
6645 +static int
6646 +delip(struct ip_set *set, const void *data, size_t size,
6647 +        ip_set_ip_t *hash_ip)
6648 +{
6649 +       struct ip_set_req_nethash *req =
6650 +           (struct ip_set_req_nethash *) data;
6651 +
6652 +       if (size != sizeof(struct ip_set_req_nethash)) {
6653 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6654 +                             sizeof(struct ip_set_req_nethash),
6655 +                             size);
6656 +               return -EINVAL;
6657 +       }
6658 +       /* TODO: no garbage collection in map->cidr */
6659 +       return __delip((struct ip_set_nethash *) set->data,
6660 +                      req->ip, req->cidr, hash_ip);
6661 +}
6662 +
6663 +static int
6664 +delip_kernel(struct ip_set *set,
6665 +            const struct sk_buff *skb,
6666 +            ip_set_ip_t *hash_ip,
6667 +            const u_int32_t *flags,
6668 +            unsigned char index)
6669 +{
6670 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6671 +       int ret = -ERANGE;
6672 +       ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC
6673 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6674 +                                       ? ip_hdr(skb)->saddr
6675 +                                       : ip_hdr(skb)->daddr);
6676 +#else
6677 +                                       ? skb->nh.iph->saddr
6678 +                                       : skb->nh.iph->daddr);
6679 +#endif
6680 +
6681 +       if (map->cidr[0])
6682 +               ret = __delip(map, ip, map->cidr[0], hash_ip);
6683 +
6684 +       return ret;
6685 +}
6686 +
6687 +static int create(struct ip_set *set, const void *data, size_t size)
6688 +{
6689 +       struct ip_set_req_nethash_create *req =
6690 +           (struct ip_set_req_nethash_create *) data;
6691 +       struct ip_set_nethash *map;
6692 +       uint16_t i;
6693 +
6694 +       if (size != sizeof(struct ip_set_req_nethash_create)) {
6695 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6696 +                              sizeof(struct ip_set_req_nethash_create),
6697 +                              size);
6698 +               return -EINVAL;
6699 +       }
6700 +
6701 +       if (req->hashsize < 1) {
6702 +               ip_set_printk("hashsize too small");
6703 +               return -ENOEXEC;
6704 +       }
6705 +       if (req->probes < 1) {
6706 +               ip_set_printk("probes too small");
6707 +               return -ENOEXEC;
6708 +       }
6709 +
6710 +       map = kmalloc(sizeof(struct ip_set_nethash)
6711 +                     + req->probes * sizeof(uint32_t), GFP_KERNEL);
6712 +       if (!map) {
6713 +               DP("out of memory for %d bytes",
6714 +                  sizeof(struct ip_set_nethash)
6715 +                  + req->probes * sizeof(uint32_t));
6716 +               return -ENOMEM;
6717 +       }
6718 +       for (i = 0; i < req->probes; i++)
6719 +               get_random_bytes(((uint32_t *) map->initval)+i, 4);
6720 +       map->elements = 0;
6721 +       map->hashsize = req->hashsize;
6722 +       map->probes = req->probes;
6723 +       map->resize = req->resize;
6724 +       memset(map->cidr, 0, 30 * sizeof(unsigned char));
6725 +       map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
6726 +       if (!map->members) {
6727 +               DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
6728 +               kfree(map);
6729 +               return -ENOMEM;
6730 +       }
6731 +
6732 +       set->data = map;
6733 +       return 0;
6734 +}
6735 +
6736 +static void destroy(struct ip_set *set)
6737 +{
6738 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6739 +
6740 +       harray_free(map->members);
6741 +       kfree(map);
6742 +
6743 +       set->data = NULL;
6744 +}
6745 +
6746 +static void flush(struct ip_set *set)
6747 +{
6748 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6749 +       harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
6750 +       memset(map->cidr, 0, 30 * sizeof(unsigned char));
6751 +       map->elements = 0;
6752 +}
6753 +
6754 +static void list_header(const struct ip_set *set, void *data)
6755 +{
6756 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6757 +       struct ip_set_req_nethash_create *header =
6758 +           (struct ip_set_req_nethash_create *) data;
6759 +
6760 +       header->hashsize = map->hashsize;
6761 +       header->probes = map->probes;
6762 +       header->resize = map->resize;
6763 +}
6764 +
6765 +static int list_members_size(const struct ip_set *set)
6766 +{
6767 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6768 +
6769 +       return (map->hashsize * sizeof(ip_set_ip_t));
6770 +}
6771 +
6772 +static void list_members(const struct ip_set *set, void *data)
6773 +{
6774 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6775 +       ip_set_ip_t i, *elem;
6776 +
6777 +       for (i = 0; i < map->hashsize; i++) {
6778 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
6779 +               ((ip_set_ip_t *)data)[i] = *elem;
6780 +       }
6781 +}
6782 +
6783 +static struct ip_set_type ip_set_nethash = {
6784 +       .typename               = SETTYPE_NAME,
6785 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6786 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
6787 +       .create                 = &create,
6788 +       .destroy                = &destroy,
6789 +       .flush                  = &flush,
6790 +       .reqsize                = sizeof(struct ip_set_req_nethash),
6791 +       .addip                  = &addip,
6792 +       .addip_kernel           = &addip_kernel,
6793 +       .retry                  = &retry,
6794 +       .delip                  = &delip,
6795 +       .delip_kernel           = &delip_kernel,
6796 +       .testip                 = &testip,
6797 +       .testip_kernel          = &testip_kernel,
6798 +       .header_size            = sizeof(struct ip_set_req_nethash_create),
6799 +       .list_header            = &list_header,
6800 +       .list_members_size      = &list_members_size,
6801 +       .list_members           = &list_members,
6802 +       .me                     = THIS_MODULE,
6803 +};
6804 +
6805 +MODULE_LICENSE("GPL");
6806 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6807 +MODULE_DESCRIPTION("nethash type of IP sets");
6808 +module_param(limit, int, 0600);
6809 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
6810 +
6811 +static int __init ip_set_nethash_init(void)
6812 +{
6813 +       return ip_set_register_set_type(&ip_set_nethash);
6814 +}
6815 +
6816 +static void __exit ip_set_nethash_fini(void)
6817 +{
6818 +       /* FIXME: possible race with ip_set_create() */
6819 +       ip_set_unregister_set_type(&ip_set_nethash);
6820 +}
6821 +
6822 +module_init(ip_set_nethash_init);
6823 +module_exit(ip_set_nethash_fini);
6824 --- /dev/null
6825 +++ b/net/ipv4/netfilter/ip_set_portmap.c
6826 @@ -0,0 +1,346 @@
6827 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6828 + *
6829 + * This program is free software; you can redistribute it and/or modify
6830 + * it under the terms of the GNU General Public License version 2 as
6831 + * published by the Free Software Foundation.
6832 + */
6833 +
6834 +/* Kernel module implementing a port set type as a bitmap */
6835 +
6836 +#include <linux/module.h>
6837 +#include <linux/ip.h>
6838 +#include <linux/tcp.h>
6839 +#include <linux/udp.h>
6840 +#include <linux/skbuff.h>
6841 +#include <linux/version.h>
6842 +#include <linux/netfilter_ipv4/ip_tables.h>
6843 +#include <linux/netfilter_ipv4/ip_set.h>
6844 +#include <linux/errno.h>
6845 +#include <asm/uaccess.h>
6846 +#include <asm/bitops.h>
6847 +#include <linux/spinlock.h>
6848 +
6849 +#include <net/ip.h>
6850 +
6851 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
6852 +
6853 +/* We must handle non-linear skbs */
6854 +static inline ip_set_ip_t
6855 +get_port(const struct sk_buff *skb, u_int32_t flags)
6856 +{
6857 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6858 +       struct iphdr *iph = ip_hdr(skb);
6859 +#else
6860 +       struct iphdr *iph = skb->nh.iph;
6861 +#endif
6862 +       u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
6863 +       switch (iph->protocol) {
6864 +       case IPPROTO_TCP: {
6865 +               struct tcphdr tcph;
6866 +
6867 +               /* See comments at tcp_match in ip_tables.c */
6868 +               if (offset)
6869 +                       return INVALID_PORT;
6870 +
6871 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6872 +               if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
6873 +#else
6874 +               if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
6875 +#endif
6876 +                       /* No choice either */
6877 +                       return INVALID_PORT;
6878 +
6879 +               return ntohs(flags & IPSET_SRC ?
6880 +                            tcph.source : tcph.dest);
6881 +           }
6882 +       case IPPROTO_UDP: {
6883 +               struct udphdr udph;
6884 +
6885 +               if (offset)
6886 +                       return INVALID_PORT;
6887 +
6888 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6889 +               if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
6890 +#else
6891 +               if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
6892 +#endif
6893 +                       /* No choice either */
6894 +                       return INVALID_PORT;
6895 +
6896 +               return ntohs(flags & IPSET_SRC ?
6897 +                            udph.source : udph.dest);
6898 +           }
6899 +       default:
6900 +               return INVALID_PORT;
6901 +       }
6902 +}
6903 +
6904 +static inline int
6905 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
6906 +{
6907 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
6908 +
6909 +       if (port < map->first_port || port > map->last_port)
6910 +               return -ERANGE;
6911 +
6912 +       *hash_port = port;
6913 +       DP("set: %s, port:%u, %u", set->name, port, *hash_port);
6914 +       return !!test_bit(port - map->first_port, map->members);
6915 +}
6916 +
6917 +static int
6918 +testport(struct ip_set *set, const void *data, size_t size,
6919 +         ip_set_ip_t *hash_port)
6920 +{
6921 +       struct ip_set_req_portmap *req =
6922 +           (struct ip_set_req_portmap *) data;
6923 +
6924 +       if (size != sizeof(struct ip_set_req_portmap)) {
6925 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6926 +                             sizeof(struct ip_set_req_portmap),
6927 +                             size);
6928 +               return -EINVAL;
6929 +       }
6930 +       return __testport(set, req->port, hash_port);
6931 +}
6932 +
6933 +static int
6934 +testport_kernel(struct ip_set *set,
6935 +               const struct sk_buff *skb,
6936 +               ip_set_ip_t *hash_port,
6937 +               const u_int32_t *flags,
6938 +               unsigned char index)
6939 +{
6940 +       int res;
6941 +       ip_set_ip_t port = get_port(skb, flags[index]);
6942 +
6943 +       DP("flag %s port %u", flags[index] & IPSET_SRC ? "SRC" : "DST", port);
6944 +       if (port == INVALID_PORT)
6945 +               return 0;
6946 +
6947 +       res =  __testport(set, port, hash_port);
6948 +
6949 +       return (res < 0 ? 0 : res);
6950 +}
6951 +
6952 +static inline int
6953 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
6954 +{
6955 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
6956 +
6957 +       if (port < map->first_port || port > map->last_port)
6958 +               return -ERANGE;
6959 +       if (test_and_set_bit(port - map->first_port, map->members))
6960 +               return -EEXIST;
6961 +
6962 +       *hash_port = port;
6963 +       DP("port %u", port);
6964 +       return 0;
6965 +}
6966 +
6967 +static int
6968 +addport(struct ip_set *set, const void *data, size_t size,
6969 +        ip_set_ip_t *hash_port)
6970 +{
6971 +       struct ip_set_req_portmap *req =
6972 +           (struct ip_set_req_portmap *) data;
6973 +
6974 +       if (size != sizeof(struct ip_set_req_portmap)) {
6975 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6976 +                             sizeof(struct ip_set_req_portmap),
6977 +                             size);
6978 +               return -EINVAL;
6979 +       }
6980 +       return __addport(set, req->port, hash_port);
6981 +}
6982 +
6983 +static int
6984 +addport_kernel(struct ip_set *set,
6985 +              const struct sk_buff *skb,
6986 +              ip_set_ip_t *hash_port,
6987 +              const u_int32_t *flags,
6988 +              unsigned char index)
6989 +{
6990 +       ip_set_ip_t port = get_port(skb, flags[index]);
6991 +
6992 +       if (port == INVALID_PORT)
6993 +               return -EINVAL;
6994 +
6995 +       return __addport(set, port, hash_port);
6996 +}
6997 +
6998 +static inline int
6999 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
7000 +{
7001 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7002 +
7003 +       if (port < map->first_port || port > map->last_port)
7004 +               return -ERANGE;
7005 +       if (!test_and_clear_bit(port - map->first_port, map->members))
7006 +               return -EEXIST;
7007 +
7008 +       *hash_port = port;
7009 +       DP("port %u", port);
7010 +       return 0;
7011 +}
7012 +
7013 +static int
7014 +delport(struct ip_set *set, const void *data, size_t size,
7015 +        ip_set_ip_t *hash_port)
7016 +{
7017 +       struct ip_set_req_portmap *req =
7018 +           (struct ip_set_req_portmap *) data;
7019 +
7020 +       if (size != sizeof(struct ip_set_req_portmap)) {
7021 +               ip_set_printk("data length wrong (want %zu, have %zu)",
7022 +                             sizeof(struct ip_set_req_portmap),
7023 +                             size);
7024 +               return -EINVAL;
7025 +       }
7026 +       return __delport(set, req->port, hash_port);
7027 +}
7028 +
7029 +static int
7030 +delport_kernel(struct ip_set *set,
7031 +              const struct sk_buff *skb,
7032 +              ip_set_ip_t *hash_port,
7033 +              const u_int32_t *flags,
7034 +              unsigned char index)
7035 +{
7036 +       ip_set_ip_t port = get_port(skb, flags[index]);
7037 +
7038 +       if (port == INVALID_PORT)
7039 +               return -EINVAL;
7040 +
7041 +       return __delport(set, port, hash_port);
7042 +}
7043 +
7044 +static int create(struct ip_set *set, const void *data, size_t size)
7045 +{
7046 +       int newbytes;
7047 +       struct ip_set_req_portmap_create *req =
7048 +           (struct ip_set_req_portmap_create *) data;
7049 +       struct ip_set_portmap *map;
7050 +
7051 +       if (size != sizeof(struct ip_set_req_portmap_create)) {
7052 +               ip_set_printk("data length wrong (want %zu, have %zu)",
7053 +                              sizeof(struct ip_set_req_portmap_create),
7054 +                              size);
7055 +               return -EINVAL;
7056 +       }
7057 +
7058 +       DP("from %u to %u", req->from, req->to);
7059 +
7060 +       if (req->from > req->to) {
7061 +               DP("bad port range");
7062 +               return -ENOEXEC;
7063 +       }
7064 +
7065 +       if (req->to - req->from > MAX_RANGE) {
7066 +               ip_set_printk("range too big (max %d ports)",
7067 +                              MAX_RANGE+1);
7068 +               return -ENOEXEC;
7069 +       }
7070 +
7071 +       map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
7072 +       if (!map) {
7073 +               DP("out of memory for %d bytes",
7074 +                  sizeof(struct ip_set_portmap));
7075 +               return -ENOMEM;
7076 +       }
7077 +       map->first_port = req->from;
7078 +       map->last_port = req->to;
7079 +       newbytes = bitmap_bytes(req->from, req->to);
7080 +       map->members = kmalloc(newbytes, GFP_KERNEL);
7081 +       if (!map->members) {
7082 +               DP("out of memory for %d bytes", newbytes);
7083 +               kfree(map);
7084 +               return -ENOMEM;
7085 +       }
7086 +       memset(map->members, 0, newbytes);
7087 +
7088 +       set->data = map;
7089 +       return 0;
7090 +}
7091 +
7092 +static void destroy(struct ip_set *set)
7093 +{
7094 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7095 +
7096 +       kfree(map->members);
7097 +       kfree(map);
7098 +
7099 +       set->data = NULL;
7100 +}
7101 +
7102 +static void flush(struct ip_set *set)
7103 +{
7104 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7105 +       memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
7106 +}
7107 +
7108 +static void list_header(const struct ip_set *set, void *data)
7109 +{
7110 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7111 +       struct ip_set_req_portmap_create *header =
7112 +           (struct ip_set_req_portmap_create *) data;
7113 +
7114 +       DP("list_header %u %u", map->first_port, map->last_port);
7115 +
7116 +       header->from = map->first_port;
7117 +       header->to = map->last_port;
7118 +}
7119 +
7120 +static int list_members_size(const struct ip_set *set)
7121 +{
7122 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7123 +
7124 +       return bitmap_bytes(map->first_port, map->last_port);
7125 +}
7126 +
7127 +static void list_members(const struct ip_set *set, void *data)
7128 +{
7129 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7130 +       int bytes = bitmap_bytes(map->first_port, map->last_port);
7131 +
7132 +       memcpy(data, map->members, bytes);
7133 +}
7134 +
7135 +static struct ip_set_type ip_set_portmap = {
7136 +       .typename               = SETTYPE_NAME,
7137 +       .features               = IPSET_TYPE_PORT | IPSET_DATA_SINGLE,
7138 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
7139 +       .create                 = &create,
7140 +       .destroy                = &destroy,
7141 +       .flush                  = &flush,
7142 +       .reqsize                = sizeof(struct ip_set_req_portmap),
7143 +       .addip                  = &addport,
7144 +       .addip_kernel           = &addport_kernel,
7145 +       .delip                  = &delport,
7146 +       .delip_kernel           = &delport_kernel,
7147 +       .testip                 = &testport,
7148 +       .testip_kernel          = &testport_kernel,
7149 +       .header_size            = sizeof(struct ip_set_req_portmap_create),
7150 +       .list_header            = &list_header,
7151 +       .list_members_size      = &list_members_size,
7152 +       .list_members           = &list_members,
7153 +       .me                     = THIS_MODULE,
7154 +};
7155 +
7156 +MODULE_LICENSE("GPL");
7157 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7158 +MODULE_DESCRIPTION("portmap type of IP sets");
7159 +
7160 +static int __init ip_set_portmap_init(void)
7161 +{
7162 +       return ip_set_register_set_type(&ip_set_portmap);
7163 +}
7164 +
7165 +static void __exit ip_set_portmap_fini(void)
7166 +{
7167 +       /* FIXME: possible race with ip_set_create() */
7168 +       ip_set_unregister_set_type(&ip_set_portmap);
7169 +}
7170 +
7171 +module_init(ip_set_portmap_init);
7172 +module_exit(ip_set_portmap_fini);
7173 --- /dev/null
7174 +++ b/net/ipv4/netfilter/ipt_set.c
7175 @@ -0,0 +1,160 @@
7176 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7177 + *                         Patrick Schaaf <bof@bof.de>
7178 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
7179 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7180 + *
7181 + * This program is free software; you can redistribute it and/or modify
7182 + * it under the terms of the GNU General Public License version 2 as
7183 + * published by the Free Software Foundation.
7184 + */
7185 +
7186 +/* Kernel module to match an IP set. */
7187 +
7188 +#include <linux/module.h>
7189 +#include <linux/ip.h>
7190 +#include <linux/skbuff.h>
7191 +#include <linux/version.h>
7192 +
7193 +#include <linux/netfilter_ipv4/ip_tables.h>
7194 +#include <linux/netfilter_ipv4/ip_set.h>
7195 +#include <linux/netfilter_ipv4/ipt_set.h>
7196 +
7197 +static inline int
7198 +match_set(const struct ipt_set_info *info,
7199 +         const struct sk_buff *skb,
7200 +         int inv)
7201 +{
7202 +       if (ip_set_testip_kernel(info->index, skb, info->flags))
7203 +               inv = !inv;
7204 +       return inv;
7205 +}
7206 +
7207 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7208 +static bool
7209 +#else
7210 +static int
7211 +#endif
7212 +match(const struct sk_buff *skb,
7213 +      const struct net_device *in,
7214 +      const struct net_device *out,
7215 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7216 +      const struct xt_match *match,
7217 +#endif
7218 +      const void *matchinfo,
7219 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7220 +      int offset, unsigned int protoff, bool *hotdrop)
7221 +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7222 +      int offset, unsigned int protoff, int *hotdrop)
7223 +#else
7224 +      int offset, int *hotdrop)
7225 +#endif
7226 +{
7227 +       const struct ipt_set_info_match *info = matchinfo;
7228 +
7229 +       return match_set(&info->match_set,
7230 +                        skb,
7231 +                        info->match_set.flags[0] & IPSET_MATCH_INV);
7232 +}
7233 +
7234 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7235 +bool
7236 +#else
7237 +static int
7238 +#endif
7239 +checkentry(const char *tablename,
7240 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7241 +          const void *inf,
7242 +#else
7243 +          const struct ipt_ip *ip,
7244 +#endif
7245 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7246 +          const struct xt_match *match,
7247 +#endif
7248 +          void *matchinfo,
7249 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7250 +          unsigned int matchsize,
7251 +#endif
7252 +          unsigned int hook_mask)
7253 +{
7254 +       struct ipt_set_info_match *info =
7255 +               (struct ipt_set_info_match *) matchinfo;
7256 +       ip_set_id_t index;
7257 +
7258 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7259 +       if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7260 +               ip_set_printk("invalid matchsize %d", matchsize);
7261 +               return 0;
7262 +       }
7263 +#endif
7264 +
7265 +       index = ip_set_get_byindex(info->match_set.index);
7266 +
7267 +       if (index == IP_SET_INVALID_ID) {
7268 +               ip_set_printk("Cannot find set indentified by id %u to match",
7269 +                             info->match_set.index);
7270 +               return 0;       /* error */
7271 +       }
7272 +       if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7273 +               ip_set_printk("That's nasty!");
7274 +               return 0;       /* error */
7275 +       }
7276 +
7277 +       return 1;
7278 +}
7279 +
7280 +static void destroy(
7281 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7282 +                   const struct xt_match *match,
7283 +#endif
7284 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7285 +                   void *matchinfo, unsigned int matchsize)
7286 +#else
7287 +                   void *matchinfo)
7288 +#endif
7289 +{
7290 +       struct ipt_set_info_match *info = matchinfo;
7291 +
7292 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7293 +       if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7294 +               ip_set_printk("invalid matchsize %d", matchsize);
7295 +               return;
7296 +       }
7297 +#endif
7298 +       ip_set_put(info->match_set.index);
7299 +}
7300 +
7301 +static struct ipt_match set_match = {
7302 +       .name           = "set",
7303 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7304 +       .family         = AF_INET,
7305 +#endif
7306 +       .match          = &match,
7307 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7308 +       .matchsize      = sizeof(struct ipt_set_info_match),
7309 +#endif
7310 +       .checkentry     = &checkentry,
7311 +       .destroy        = &destroy,
7312 +       .me             = THIS_MODULE
7313 +};
7314 +
7315 +MODULE_LICENSE("GPL");
7316 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7317 +MODULE_DESCRIPTION("iptables IP set match module");
7318 +
7319 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7320 +#define ipt_register_match     xt_register_match
7321 +#define ipt_unregister_match   xt_unregister_match
7322 +#endif
7323 +
7324 +static int __init ipt_ipset_init(void)
7325 +{
7326 +       return ipt_register_match(&set_match);
7327 +}
7328 +
7329 +static void __exit ipt_ipset_fini(void)
7330 +{
7331 +       ipt_unregister_match(&set_match);
7332 +}
7333 +
7334 +module_init(ipt_ipset_init);
7335 +module_exit(ipt_ipset_fini);
7336 --- /dev/null
7337 +++ b/net/ipv4/netfilter/ipt_SET.c
7338 @@ -0,0 +1,179 @@
7339 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7340 + *                         Patrick Schaaf <bof@bof.de>
7341 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
7342 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7343 + *
7344 + * This program is free software; you can redistribute it and/or modify
7345 + * it under the terms of the GNU General Public License version 2 as
7346 + * published by the Free Software Foundation.
7347 + */
7348 +
7349 +/* ipt_SET.c - netfilter target to manipulate IP sets */
7350 +
7351 +#include <linux/types.h>
7352 +#include <linux/ip.h>
7353 +#include <linux/timer.h>
7354 +#include <linux/module.h>
7355 +#include <linux/netfilter.h>
7356 +#include <linux/netdevice.h>
7357 +#include <linux/if.h>
7358 +#include <linux/inetdevice.h>
7359 +#include <linux/version.h>
7360 +#include <net/protocol.h>
7361 +#include <net/checksum.h>
7362 +#include <linux/netfilter_ipv4.h>
7363 +#include <linux/netfilter_ipv4/ip_tables.h>
7364 +#include <linux/netfilter_ipv4/ipt_set.h>
7365 +
7366 +static unsigned int
7367 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
7368 +target(struct sk_buff *skb,
7369 +#else
7370 +target(struct sk_buff **pskb,
7371 +#endif
7372 +       const struct net_device *in,
7373 +       const struct net_device *out,
7374 +       unsigned int hooknum,
7375 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7376 +       const struct xt_target *target,
7377 +#endif
7378 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7379 +       const void *targinfo,
7380 +       void *userinfo)
7381 +#else
7382 +       const void *targinfo)
7383 +#endif
7384 +{
7385 +       const struct ipt_set_info_target *info = targinfo;
7386 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
7387 +       struct sk_buff *skb = *pskb;
7388 +#endif
7389 +
7390 +       if (info->add_set.index != IP_SET_INVALID_ID)
7391 +               ip_set_addip_kernel(info->add_set.index,
7392 +                                   skb,
7393 +                                   info->add_set.flags);
7394 +       if (info->del_set.index != IP_SET_INVALID_ID)
7395 +               ip_set_delip_kernel(info->del_set.index,
7396 +                                   skb,
7397 +                                   info->del_set.flags);
7398 +
7399 +       return IPT_CONTINUE;
7400 +}
7401 +
7402 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7403 +static bool
7404 +#else
7405 +static int
7406 +#endif
7407 +checkentry(const char *tablename,
7408 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7409 +          const void *e,
7410 +#else
7411 +          const struct ipt_entry *e,
7412 +#endif
7413 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7414 +          const struct xt_target *target,
7415 +#endif
7416 +          void *targinfo,
7417 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7418 +          unsigned int targinfosize,
7419 +#endif
7420 +          unsigned int hook_mask)
7421 +{
7422 +       struct ipt_set_info_target *info =
7423 +               (struct ipt_set_info_target *) targinfo;
7424 +       ip_set_id_t index;
7425 +
7426 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7427 +       if (targinfosize != IPT_ALIGN(sizeof(*info))) {
7428 +               DP("bad target info size %u", targinfosize);
7429 +               return 0;
7430 +       }
7431 +#endif
7432 +
7433 +       if (info->add_set.index != IP_SET_INVALID_ID) {
7434 +               index = ip_set_get_byindex(info->add_set.index);
7435 +               if (index == IP_SET_INVALID_ID) {
7436 +                       ip_set_printk("cannot find add_set index %u as target",
7437 +                                     info->add_set.index);
7438 +                       return 0;       /* error */
7439 +               }
7440 +       }
7441 +
7442 +       if (info->del_set.index != IP_SET_INVALID_ID) {
7443 +               index = ip_set_get_byindex(info->del_set.index);
7444 +               if (index == IP_SET_INVALID_ID) {
7445 +                       ip_set_printk("cannot find del_set index %u as target",
7446 +                                     info->del_set.index);
7447 +                       return 0;       /* error */
7448 +               }
7449 +       }
7450 +       if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
7451 +           || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7452 +               ip_set_printk("That's nasty!");
7453 +               return 0;       /* error */
7454 +       }
7455 +
7456 +       return 1;
7457 +}
7458 +
7459 +static void destroy(
7460 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7461 +                   const struct xt_target *target,
7462 +#endif
7463 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7464 +                   void *targetinfo, unsigned int targetsize)
7465 +#else
7466 +                   void *targetinfo)
7467 +#endif
7468 +{
7469 +       struct ipt_set_info_target *info = targetinfo;
7470 +
7471 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7472 +       if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
7473 +               ip_set_printk("invalid targetsize %d", targetsize);
7474 +               return;
7475 +       }
7476 +#endif
7477 +       if (info->add_set.index != IP_SET_INVALID_ID)
7478 +               ip_set_put(info->add_set.index);
7479 +       if (info->del_set.index != IP_SET_INVALID_ID)
7480 +               ip_set_put(info->del_set.index);
7481 +}
7482 +
7483 +static struct ipt_target SET_target = {
7484 +       .name           = "SET",
7485 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7486 +       .family         = AF_INET,
7487 +#endif
7488 +       .target         = target,
7489 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7490 +       .targetsize     = sizeof(struct ipt_set_info_target),
7491 +#endif
7492 +       .checkentry     = checkentry,
7493 +       .destroy        = destroy,
7494 +       .me             = THIS_MODULE
7495 +};
7496 +
7497 +MODULE_LICENSE("GPL");
7498 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7499 +MODULE_DESCRIPTION("iptables IP set target module");
7500 +
7501 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7502 +#define ipt_register_target      xt_register_target
7503 +#define ipt_unregister_target    xt_unregister_target
7504 +#endif
7505 +
7506 +static int __init ipt_SET_init(void)
7507 +{
7508 +       return ipt_register_target(&SET_target);
7509 +}
7510 +
7511 +static void __exit ipt_SET_fini(void)
7512 +{
7513 +       ipt_unregister_target(&SET_target);
7514 +}
7515 +
7516 +module_init(ipt_SET_init);
7517 +module_exit(ipt_SET_fini);
7518 --- a/net/ipv4/netfilter/Kconfig
7519 +++ b/net/ipv4/netfilter/Kconfig
7520 @@ -401,5 +401,122 @@ config IP_NF_ARP_MANGLE
7521           Allows altering the ARP packet payload: source and destination
7522           hardware and network addresses.
7523  
7524 +config IP_NF_SET
7525 +       tristate "IP set support"
7526 +       depends on INET && NETFILTER
7527 +       help
7528 +         This option adds IP set support to the kernel.
7529 +         In order to define and use sets, you need the userspace utility
7530 +         ipset(8).
7531 +
7532 +         To compile it as a module, choose M here.  If unsure, say N.
7533 +
7534 +config IP_NF_SET_MAX
7535 +       int "Maximum number of IP sets"
7536 +       default 256
7537 +       range 2 65534
7538 +       depends on IP_NF_SET
7539 +       help
7540 +         You can define here default value of the maximum number 
7541 +         of IP sets for the kernel.
7542 +
7543 +         The value can be overriden by the 'max_sets' module
7544 +         parameter of the 'ip_set' module.
7545 +
7546 +config IP_NF_SET_HASHSIZE
7547 +       int "Hash size for bindings of IP sets"
7548 +       default 1024
7549 +       depends on IP_NF_SET
7550 +       help
7551 +         You can define here default value of the hash size for
7552 +         bindings of IP sets.
7553 +
7554 +         The value can be overriden by the 'hash_size' module
7555 +         parameter of the 'ip_set' module.
7556 +
7557 +config IP_NF_SET_IPMAP
7558 +       tristate "ipmap set support"
7559 +       depends on IP_NF_SET
7560 +       help
7561 +         This option adds the ipmap set type support.
7562 +
7563 +         To compile it as a module, choose M here.  If unsure, say N.
7564 +
7565 +config IP_NF_SET_MACIPMAP
7566 +       tristate "macipmap set support"
7567 +       depends on IP_NF_SET
7568 +       help
7569 +         This option adds the macipmap set type support.
7570 +
7571 +         To compile it as a module, choose M here.  If unsure, say N.
7572 +
7573 +config IP_NF_SET_PORTMAP
7574 +       tristate "portmap set support"
7575 +       depends on IP_NF_SET
7576 +       help
7577 +         This option adds the portmap set type support.
7578 +
7579 +         To compile it as a module, choose M here.  If unsure, say N.
7580 +
7581 +config IP_NF_SET_IPHASH
7582 +       tristate "iphash set support"
7583 +       depends on IP_NF_SET
7584 +       help
7585 +         This option adds the iphash set type support.
7586 +
7587 +         To compile it as a module, choose M here.  If unsure, say N.
7588 +
7589 +config IP_NF_SET_NETHASH
7590 +       tristate "nethash set support"
7591 +       depends on IP_NF_SET
7592 +       help
7593 +         This option adds the nethash set type support.
7594 +
7595 +         To compile it as a module, choose M here.  If unsure, say N.
7596 +
7597 +config IP_NF_SET_IPPORTHASH
7598 +       tristate "ipporthash set support"
7599 +       depends on IP_NF_SET
7600 +       help
7601 +         This option adds the ipporthash set type support.
7602 +
7603 +         To compile it as a module, choose M here.  If unsure, say N.
7604 +
7605 +config IP_NF_SET_IPTREE
7606 +       tristate "iptree set support"
7607 +       depends on IP_NF_SET
7608 +       help
7609 +         This option adds the iptree set type support.
7610 +
7611 +         To compile it as a module, choose M here.  If unsure, say N.
7612 +
7613 +config IP_NF_SET_IPTREEMAP
7614 +       tristate "iptreemap set support"
7615 +       depends on IP_NF_SET
7616 +       help
7617 +         This option adds the iptreemap set type support.
7618 +
7619 +         To compile it as a module, choose M here.  If unsure, say N.
7620 +
7621 +config IP_NF_MATCH_SET
7622 +       tristate "set match support"
7623 +       depends on IP_NF_SET
7624 +       help
7625 +         Set matching matches against given IP sets.
7626 +         You need the ipset utility to create and set up the sets.
7627 +
7628 +         To compile it as a module, choose M here.  If unsure, say N.
7629 +
7630 +config IP_NF_TARGET_SET
7631 +       tristate "SET target support"
7632 +       depends on IP_NF_SET
7633 +       help
7634 +         The SET target makes possible to add/delete entries
7635 +         in IP sets.
7636 +         You need the ipset utility to create and set up the sets.
7637 +
7638 +         To compile it as a module, choose M here.  If unsure, say N.
7639 +
7640 +
7641  endmenu
7642  
7643 --- a/net/ipv4/netfilter/Makefile
7644 +++ b/net/ipv4/netfilter/Makefile
7645 @@ -49,6 +49,7 @@ obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o
7646  obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
7647  obj-$(CONFIG_IP_NF_MATCH_RECENT) += ipt_recent.o
7648  obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
7649 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
7650  
7651  obj-$(CONFIG_IP_NF_MATCH_IPP2P) += ipt_ipp2p.o
7652  
7653 @@ -62,6 +63,18 @@ obj-$(CONFIG_IP_NF_TARGET_REDIRECT) += i
7654  obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
7655  obj-$(CONFIG_IP_NF_TARGET_TTL) += ipt_TTL.o
7656  obj-$(CONFIG_IP_NF_TARGET_ULOG) += ipt_ULOG.o
7657 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
7658 +
7659 +# sets
7660 +obj-$(CONFIG_IP_NF_SET) += ip_set.o
7661 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
7662 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
7663 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
7664 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
7665 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
7666 +obj-$(CONFIG_IP_NF_SET_IPPORTHASH) += ip_set_ipporthash.o
7667 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
7668 +obj-$(CONFIG_IP_NF_SET_IPTREEMAP) += ip_set_iptreemap.o
7669  
7670  # generic ARP tables
7671  obj-$(CONFIG_IP_NF_ARPTABLES) += arp_tables.o