]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-pom-ng-set.patch
- updated for 2.6.22.19
[packages/kernel.git] / kernel-pom-ng-set.patch
1 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set.h linux-2.6/include/linux/netfilter_ipv4/ip_set.h
2 --- linux-2.6./include/linux/netfilter_ipv4/ip_set.h    1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set.h     2005-10-13 10:53:58.000000000 +0200
4 @@ -0,0 +1,498 @@
5 +#ifndef _IP_SET_H
6 +#define _IP_SET_H
7 +
8 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
9 + *                         Patrick Schaaf <bof@bof.de>
10 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
11 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 + *
13 + * This program is free software; you can redistribute it and/or modify
14 + * it under the terms of the GNU General Public License version 2 as
15 + * published by the Free Software Foundation.  
16 + */
17 +
18 +#if 0
19 +#define IP_SET_DEBUG
20 +#endif
21 +
22 +/*
23 + * A sockopt of such quality has hardly ever been seen before on the open
24 + * market!  This little beauty, hardly ever used: above 64, so it's
25 + * traditionally used for firewalling, not touched (even once!) by the
26 + * 2.0, 2.2 and 2.4 kernels!
27 + *
28 + * Comes with its own certificate of authenticity, valid anywhere in the
29 + * Free world!
30 + *
31 + * Rusty, 19.4.2000
32 + */
33 +#define SO_IP_SET              83
34 +
35 +/*
36 + * Heavily modify by Joakim Axelsson 08.03.2002
37 + * - Made it more modulebased
38 + *
39 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
40 + * - bindings added
41 + * - in order to "deal with" backward compatibility, renamed to ipset
42 + */
43 +
44 +/* 
45 + * Used so that the kernel module and ipset-binary can match their versions 
46 + */
47 +#define IP_SET_PROTOCOL_VERSION 2
48 +
49 +#define IP_SET_MAXNAMELEN 32   /* set names and set typenames */
50 +
51 +/* Lets work with our own typedef for representing an IP address.
52 + * We hope to make the code more portable, possibly to IPv6...
53 + *
54 + * The representation works in HOST byte order, because most set types
55 + * will perform arithmetic operations and compare operations.
56 + * 
57 + * For now the type is an uint32_t.
58 + *
59 + * Make sure to ONLY use the functions when translating and parsing
60 + * in order to keep the host byte order and make it more portable:
61 + *  parse_ip()
62 + *  parse_mask()
63 + *  parse_ipandmask()
64 + *  ip_tostring()
65 + * (Joakim: where are they???)
66 + */
67 +
68 +typedef uint32_t ip_set_ip_t;
69 +
70 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
71 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
72 + */
73 +typedef uint16_t ip_set_id_t;
74 +
75 +#define IP_SET_INVALID_ID      65535
76 +
77 +/* How deep we follow bindings */
78 +#define IP_SET_MAX_BINDINGS    6
79 +
80 +/*
81 + * Option flags for kernel operations (ipt_set_info)
82 + */
83 +#define IPSET_SRC              0x01    /* Source match/add */
84 +#define IPSET_DST              0x02    /* Destination match/add */
85 +#define IPSET_MATCH_INV                0x04    /* Inverse matching */
86 +
87 +/*
88 + * Set features
89 + */
90 +#define IPSET_TYPE_IP          0x01    /* IP address type of set */
91 +#define IPSET_TYPE_PORT                0x02    /* Port type of set */
92 +#define IPSET_DATA_SINGLE      0x04    /* Single data storage */
93 +#define IPSET_DATA_DOUBLE      0x08    /* Double data storage */
94 +
95 +/* Reserved keywords */
96 +#define IPSET_TOKEN_DEFAULT    ":default:"
97 +#define IPSET_TOKEN_ALL                ":all:"
98 +
99 +/* SO_IP_SET operation constants, and their request struct types.
100 + *
101 + * Operation ids:
102 + *       0-99:  commands with version checking
103 + *     100-199: add/del/test/bind/unbind
104 + *     200-299: list, save, restore
105 + */
106 +
107 +/* Single shot operations: 
108 + * version, create, destroy, flush, rename and swap 
109 + *
110 + * Sets are identified by name.
111 + */
112 +
113 +#define IP_SET_REQ_STD         \
114 +       unsigned op;            \
115 +       unsigned version;       \
116 +       char name[IP_SET_MAXNAMELEN]
117 +
118 +#define IP_SET_OP_CREATE       0x00000001      /* Create a new (empty) set */
119 +struct ip_set_req_create {
120 +       IP_SET_REQ_STD;
121 +       char typename[IP_SET_MAXNAMELEN];
122 +};
123 +
124 +#define IP_SET_OP_DESTROY      0x00000002      /* Remove a (empty) set */
125 +struct ip_set_req_std {
126 +       IP_SET_REQ_STD;
127 +};
128 +
129 +#define IP_SET_OP_FLUSH                0x00000003      /* Remove all IPs in a set */
130 +/* Uses ip_set_req_std */
131 +
132 +#define IP_SET_OP_RENAME       0x00000004      /* Rename a set */
133 +/* Uses ip_set_req_create */
134 +
135 +#define IP_SET_OP_SWAP         0x00000005      /* Swap two sets */
136 +/* Uses ip_set_req_create */
137 +
138 +union ip_set_name_index {
139 +       char name[IP_SET_MAXNAMELEN];
140 +       ip_set_id_t index;
141 +};
142 +
143 +#define IP_SET_OP_GET_BYNAME   0x00000006      /* Get set index by name */
144 +struct ip_set_req_get_set {
145 +       unsigned op;
146 +       unsigned version;
147 +       union ip_set_name_index set;
148 +};
149 +
150 +#define IP_SET_OP_GET_BYINDEX  0x00000007      /* Get set name by index */
151 +/* Uses ip_set_req_get_set */
152 +
153 +#define IP_SET_OP_VERSION      0x00000100      /* Ask kernel version */
154 +struct ip_set_req_version {
155 +       unsigned op;
156 +       unsigned version;
157 +};
158 +
159 +/* Double shots operations: 
160 + * add, del, test, bind and unbind.
161 + *
162 + * First we query the kernel to get the index and type of the target set,
163 + * then issue the command. Validity of IP is checked in kernel in order
164 + * to minimalize sockopt operations.
165 + */
166 +
167 +/* Get minimal set data for add/del/test/bind/unbind IP */
168 +#define IP_SET_OP_ADT_GET      0x00000010      /* Get set and type */
169 +struct ip_set_req_adt_get {
170 +       unsigned op;
171 +       unsigned version;
172 +       union ip_set_name_index set;
173 +       char typename[IP_SET_MAXNAMELEN];
174 +};
175 +
176 +#define IP_SET_REQ_BYINDEX     \
177 +       unsigned op;            \
178 +       ip_set_id_t index;
179 +
180 +struct ip_set_req_adt {
181 +       IP_SET_REQ_BYINDEX;
182 +};
183 +
184 +#define IP_SET_OP_ADD_IP       0x00000101      /* Add an IP to a set */
185 +/* Uses ip_set_req_adt, with type specific addage */
186 +
187 +#define IP_SET_OP_DEL_IP       0x00000102      /* Remove an IP from a set */
188 +/* Uses ip_set_req_adt, with type specific addage */
189 +
190 +#define IP_SET_OP_TEST_IP      0x00000103      /* Test an IP in a set */
191 +/* Uses ip_set_req_adt, with type specific addage */
192 +
193 +#define IP_SET_OP_BIND_SET     0x00000104      /* Bind an IP to a set */
194 +/* Uses ip_set_req_bind, with type specific addage */
195 +struct ip_set_req_bind {
196 +       IP_SET_REQ_BYINDEX;
197 +       char binding[IP_SET_MAXNAMELEN];
198 +};
199 +
200 +#define IP_SET_OP_UNBIND_SET   0x00000105      /* Unbind an IP from a set */
201 +/* Uses ip_set_req_bind, with type speficic addage 
202 + * index = 0 means unbinding for all sets */
203 +
204 +#define IP_SET_OP_TEST_BIND_SET        0x00000106      /* Test binding an IP to a set */
205 +/* Uses ip_set_req_bind, with type specific addage */
206 +
207 +/* Multiple shots operations: list, save, restore.
208 + *
209 + * - check kernel version and query the max number of sets
210 + * - get the basic information on all sets
211 + *   and size required for the next step
212 + * - get actual set data: header, data, bindings
213 + */
214 +
215 +/* Get max_sets and the index of a queried set
216 + */
217 +#define IP_SET_OP_MAX_SETS     0x00000020
218 +struct ip_set_req_max_sets {
219 +       unsigned op;
220 +       unsigned version;
221 +       ip_set_id_t max_sets;           /* max_sets */
222 +       ip_set_id_t sets;               /* real number of sets */
223 +       union ip_set_name_index set;    /* index of set if name used */
224 +};
225 +
226 +/* Get the id and name of the sets plus size for next step */
227 +#define IP_SET_OP_LIST_SIZE    0x00000201
228 +#define IP_SET_OP_SAVE_SIZE    0x00000202
229 +struct ip_set_req_setnames {
230 +       unsigned op;
231 +       ip_set_id_t index;              /* set to list/save */
232 +       size_t size;                    /* size to get setdata/bindings */
233 +       /* followed by sets number of struct ip_set_name_list */
234 +};
235 +
236 +struct ip_set_name_list {
237 +       char name[IP_SET_MAXNAMELEN];
238 +       char typename[IP_SET_MAXNAMELEN];
239 +       ip_set_id_t index;
240 +       ip_set_id_t id;
241 +};
242 +
243 +/* The actual list operation */
244 +#define IP_SET_OP_LIST         0x00000203
245 +struct ip_set_req_list {
246 +       IP_SET_REQ_BYINDEX;
247 +       /* sets number of struct ip_set_list in reply */ 
248 +};
249 +
250 +struct ip_set_list {
251 +       ip_set_id_t index;
252 +       ip_set_id_t binding;
253 +       u_int32_t ref;
254 +       size_t header_size;     /* Set header data of header_size */
255 +       size_t members_size;    /* Set members data of members_size */
256 +       size_t bindings_size;   /* Set bindings data of bindings_size */
257 +};
258 +
259 +struct ip_set_hash_list {
260 +       ip_set_ip_t ip;
261 +       ip_set_id_t binding;
262 +};
263 +
264 +/* The save operation */
265 +#define IP_SET_OP_SAVE         0x00000204
266 +/* Uses ip_set_req_list, in the reply replaced by
267 + * sets number of struct ip_set_save plus a marker
268 + * ip_set_save followed by ip_set_hash_save structures.
269 + */
270 +struct ip_set_save {
271 +       ip_set_id_t index;
272 +       ip_set_id_t binding;
273 +       size_t header_size;     /* Set header data of header_size */
274 +       size_t members_size;    /* Set members data of members_size */
275 +};
276 +
277 +/* At restoring, ip == 0 means default binding for the given set: */
278 +struct ip_set_hash_save {
279 +       ip_set_ip_t ip;
280 +       ip_set_id_t id;
281 +       ip_set_id_t binding;
282 +};
283 +
284 +/* The restore operation */
285 +#define IP_SET_OP_RESTORE      0x00000205
286 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
287 + * plus a marker ip_set_restore, followed by ip_set_hash_save 
288 + * structures.
289 + */
290 +struct ip_set_restore {
291 +       char name[IP_SET_MAXNAMELEN];
292 +       char typename[IP_SET_MAXNAMELEN];
293 +       ip_set_id_t index;
294 +       size_t header_size;     /* Create data of header_size */
295 +       size_t members_size;    /* Set members data of members_size */
296 +};
297 +
298 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
299 +{
300 +       return 4 * ((((b - a + 8) / 8) + 3) / 4);
301 +}
302 +
303 +#ifdef __KERNEL__
304 +
305 +#define ip_set_printk(format, args...)                         \
306 +       do {                                                    \
307 +               printk("%s: %s: ", __FILE__, __FUNCTION__);     \
308 +               printk(format "\n" , ## args);                  \
309 +       } while (0)
310 +
311 +#if defined(IP_SET_DEBUG)
312 +#define DP(format, args...)                                    \
313 +       do {                                                    \
314 +               printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
315 +               printk(format "\n" , ## args);                  \
316 +       } while (0)
317 +#define IP_SET_ASSERT(x)                                       \
318 +       do {                                                    \
319 +               if (!(x))                                       \
320 +                       printk("IP_SET_ASSERT: %s:%i(%s)\n",    \
321 +                               __FILE__, __LINE__, __FUNCTION__); \
322 +       } while (0)
323 +#else
324 +#define DP(format, args...)
325 +#define IP_SET_ASSERT(x)
326 +#endif
327 +
328 +struct ip_set;
329 +
330 +/*
331 + * The ip_set_type definition - one per set type, e.g. "ipmap".
332 + *
333 + * Each individual set has a pointer, set->type, going to one
334 + * of these structures. Function pointers inside the structure implement
335 + * the real behaviour of the sets.
336 + *
337 + * If not mentioned differently, the implementation behind the function
338 + * pointers of a set_type, is expected to return 0 if ok, and a negative
339 + * errno (e.g. -EINVAL) on error.
340 + */
341 +struct ip_set_type {
342 +       struct list_head list;  /* next in list of set types */
343 +
344 +       /* test for IP in set (kernel: iptables -m set src|dst)
345 +        * return 0 if not in set, 1 if in set.
346 +        */
347 +       int (*testip_kernel) (struct ip_set *set,
348 +                             const struct sk_buff * skb, 
349 +                             ip_set_ip_t *ip,
350 +                             const u_int32_t *flags,
351 +                             unsigned char index);
352 +
353 +       /* test for IP in set (userspace: ipset -T set IP)
354 +        * return 0 if not in set, 1 if in set.
355 +        */
356 +       int (*testip) (struct ip_set *set,
357 +                      const void *data, size_t size,
358 +                      ip_set_ip_t *ip);
359 +
360 +       /*
361 +        * Size of the data structure passed by when
362 +        * adding/deletin/testing an entry.
363 +        */
364 +       size_t reqsize;
365 +
366 +       /* Add IP into set (userspace: ipset -A set IP)
367 +        * Return -EEXIST if the address is already in the set,
368 +        * and -ERANGE if the address lies outside the set bounds.
369 +        * If the address was not already in the set, 0 is returned.
370 +        */
371 +       int (*addip) (struct ip_set *set, 
372 +                     const void *data, size_t size,
373 +                     ip_set_ip_t *ip);
374 +
375 +       /* Add IP into set (kernel: iptables ... -j SET set src|dst)
376 +        * Return -EEXIST if the address is already in the set,
377 +        * and -ERANGE if the address lies outside the set bounds.
378 +        * If the address was not already in the set, 0 is returned.
379 +        */
380 +       int (*addip_kernel) (struct ip_set *set,
381 +                            const struct sk_buff * skb, 
382 +                            ip_set_ip_t *ip,
383 +                            const u_int32_t *flags,
384 +                            unsigned char index);
385 +
386 +       /* remove IP from set (userspace: ipset -D set --entry x)
387 +        * Return -EEXIST if the address is NOT in the set,
388 +        * and -ERANGE if the address lies outside the set bounds.
389 +        * If the address really was in the set, 0 is returned.
390 +        */
391 +       int (*delip) (struct ip_set *set, 
392 +                     const void *data, size_t size,
393 +                     ip_set_ip_t *ip);
394 +
395 +       /* remove IP from set (kernel: iptables ... -j SET --entry x)
396 +        * Return -EEXIST if the address is NOT in the set,
397 +        * and -ERANGE if the address lies outside the set bounds.
398 +        * If the address really was in the set, 0 is returned.
399 +        */
400 +       int (*delip_kernel) (struct ip_set *set,
401 +                            const struct sk_buff * skb, 
402 +                            ip_set_ip_t *ip,
403 +                            const u_int32_t *flags,
404 +                            unsigned char index);
405 +
406 +       /* new set creation - allocated type specific items
407 +        */
408 +       int (*create) (struct ip_set *set,
409 +                      const void *data, size_t size);
410 +
411 +       /* retry the operation after successfully tweaking the set
412 +        */
413 +       int (*retry) (struct ip_set *set);
414 +
415 +       /* set destruction - free type specific items
416 +        * There is no return value.
417 +        * Can be called only when child sets are destroyed.
418 +        */
419 +       void (*destroy) (struct ip_set *set);
420 +
421 +       /* set flushing - reset all bits in the set, or something similar.
422 +        * There is no return value.
423 +        */
424 +       void (*flush) (struct ip_set *set);
425 +
426 +       /* Listing: size needed for header
427 +        */
428 +       size_t header_size;
429 +
430 +       /* Listing: Get the header
431 +        *
432 +        * Fill in the information in "data".
433 +        * This function is always run after list_header_size() under a 
434 +        * writelock on the set. Therefor is the length of "data" always 
435 +        * correct. 
436 +        */
437 +       void (*list_header) (const struct ip_set *set, 
438 +                            void *data);
439 +
440 +       /* Listing: Get the size for the set members
441 +        */
442 +       int (*list_members_size) (const struct ip_set *set);
443 +
444 +       /* Listing: Get the set members
445 +        *
446 +        * Fill in the information in "data".
447 +        * This function is always run after list_member_size() under a 
448 +        * writelock on the set. Therefor is the length of "data" always 
449 +        * correct. 
450 +        */
451 +       void (*list_members) (const struct ip_set *set,
452 +                             void *data);
453 +
454 +       char typename[IP_SET_MAXNAMELEN];
455 +       unsigned char features;
456 +       int protocol_version;
457 +
458 +       /* Set this to THIS_MODULE if you are a module, otherwise NULL */
459 +       struct module *me;
460 +};
461 +
462 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
463 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
464 +
465 +/* A generic ipset */
466 +struct ip_set {
467 +       char name[IP_SET_MAXNAMELEN];   /* the name of the set */
468 +       rwlock_t lock;                  /* lock for concurrency control */
469 +       ip_set_id_t id;                 /* set id for swapping */
470 +       ip_set_id_t binding;            /* default binding for the set */
471 +       atomic_t ref;                   /* in kernel and in hash references */
472 +       struct ip_set_type *type;       /* the set types */
473 +       void *data;                     /* pooltype specific data */
474 +};
475 +
476 +/* Structure to bind set elements to sets */
477 +struct ip_set_hash {
478 +       struct list_head list;          /* list of clashing entries in hash */
479 +       ip_set_ip_t ip;                 /* ip from set */
480 +       ip_set_id_t id;                 /* set id */
481 +       ip_set_id_t binding;            /* set we bind the element to */
482 +};
483 +
484 +/* register and unregister set references */
485 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
486 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
487 +extern void ip_set_put(ip_set_id_t id);
488 +
489 +/* API for iptables set match, and SET target */
490 +extern void ip_set_addip_kernel(ip_set_id_t id,
491 +                               const struct sk_buff *skb,
492 +                               const u_int32_t *flags);
493 +extern void ip_set_delip_kernel(ip_set_id_t id,
494 +                               const struct sk_buff *skb,
495 +                               const u_int32_t *flags);
496 +extern int ip_set_testip_kernel(ip_set_id_t id,
497 +                               const struct sk_buff *skb,
498 +                               const u_int32_t *flags);
499 +
500 +#endif                         /* __KERNEL__ */
501 +
502 +#endif /*_IP_SET_H*/
503 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set_iphash.h linux-2.6/include/linux/netfilter_ipv4/ip_set_iphash.h
504 --- linux-2.6./include/linux/netfilter_ipv4/ip_set_iphash.h     1970-01-01 01:00:00.000000000 +0100
505 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set_iphash.h      2006-05-08 11:52:48.000000000 +0200
506 @@ -0,0 +1,30 @@
507 +#ifndef __IP_SET_IPHASH_H
508 +#define __IP_SET_IPHASH_H
509 +
510 +#include <linux/netfilter_ipv4/ip_set.h>
511 +
512 +#define SETTYPE_NAME "iphash"
513 +#define MAX_RANGE 0x0000FFFF
514 +
515 +struct ip_set_iphash {
516 +       ip_set_ip_t *members;           /* the iphash proper */
517 +       uint32_t elements;              /* number of elements */
518 +       uint32_t hashsize;              /* hash size */
519 +       uint16_t probes;                /* max number of probes  */
520 +       uint16_t resize;                /* resize factor in percent */
521 +       ip_set_ip_t netmask;            /* netmask */
522 +       void *initval[0];               /* initvals for jhash_1word */
523 +};
524 +
525 +struct ip_set_req_iphash_create {
526 +       uint32_t hashsize;
527 +       uint16_t probes;
528 +       uint16_t resize;
529 +       ip_set_ip_t netmask;
530 +};
531 +
532 +struct ip_set_req_iphash {
533 +       ip_set_ip_t ip;
534 +};
535 +
536 +#endif /* __IP_SET_IPHASH_H */
537 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set_ipmap.h linux-2.6/include/linux/netfilter_ipv4/ip_set_ipmap.h
538 --- linux-2.6./include/linux/netfilter_ipv4/ip_set_ipmap.h      1970-01-01 01:00:00.000000000 +0100
539 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set_ipmap.h       2005-09-30 18:44:08.000000000 +0200
540 @@ -0,0 +1,56 @@
541 +#ifndef __IP_SET_IPMAP_H
542 +#define __IP_SET_IPMAP_H
543 +
544 +#include <linux/netfilter_ipv4/ip_set.h>
545 +
546 +#define SETTYPE_NAME "ipmap"
547 +#define MAX_RANGE 0x0000FFFF
548 +
549 +struct ip_set_ipmap {
550 +       void *members;                  /* the ipmap proper */
551 +       ip_set_ip_t first_ip;           /* host byte order, included in range */
552 +       ip_set_ip_t last_ip;            /* host byte order, included in range */
553 +       ip_set_ip_t netmask;            /* subnet netmask */
554 +       ip_set_ip_t sizeid;             /* size of set in IPs */
555 +       ip_set_ip_t hosts;              /* number of hosts in a subnet */
556 +};
557 +
558 +struct ip_set_req_ipmap_create {
559 +       ip_set_ip_t from;
560 +       ip_set_ip_t to;
561 +       ip_set_ip_t netmask;
562 +};
563 +
564 +struct ip_set_req_ipmap {
565 +       ip_set_ip_t ip;
566 +};
567 +
568 +unsigned int
569 +mask_to_bits(ip_set_ip_t mask)
570 +{
571 +       unsigned int bits = 32;
572 +       ip_set_ip_t maskaddr;
573 +       
574 +       if (mask == 0xFFFFFFFF)
575 +               return bits;
576 +       
577 +       maskaddr = 0xFFFFFFFE;
578 +       while (--bits >= 0 && maskaddr != mask)
579 +               maskaddr <<= 1;
580 +       
581 +       return bits;
582 +}
583 +
584 +ip_set_ip_t
585 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
586 +{
587 +       ip_set_ip_t mask = 0xFFFFFFFE;
588 +       
589 +       *bits = 32;
590 +       while (--(*bits) >= 0 && mask && (to & mask) != from)
591 +               mask <<= 1;
592 +               
593 +       return mask;
594 +}
595 +       
596 +#endif /* __IP_SET_IPMAP_H */
597 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set_ipporthash.h linux-2.6/include/linux/netfilter_ipv4/ip_set_ipporthash.h
598 --- linux-2.6./include/linux/netfilter_ipv4/ip_set_ipporthash.h 1970-01-01 01:00:00.000000000 +0100
599 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set_ipporthash.h  2006-05-08 11:52:48.000000000 +0200
600 @@ -0,0 +1,34 @@
601 +#ifndef __IP_SET_IPPORTHASH_H
602 +#define __IP_SET_IPPORTHASH_H
603 +
604 +#include <linux/netfilter_ipv4/ip_set.h>
605 +
606 +#define SETTYPE_NAME "ipporthash"
607 +#define MAX_RANGE 0x0000FFFF
608 +#define INVALID_PORT   (MAX_RANGE + 1)
609 +
610 +struct ip_set_ipporthash {
611 +       ip_set_ip_t *members;           /* the ipporthash proper */
612 +       uint32_t elements;              /* number of elements */
613 +       uint32_t hashsize;              /* hash size */
614 +       uint16_t probes;                /* max number of probes  */
615 +       uint16_t resize;                /* resize factor in percent */
616 +       ip_set_ip_t first_ip;           /* host byte order, included in range */
617 +       ip_set_ip_t last_ip;            /* host byte order, included in range */
618 +       void *initval[0];               /* initvals for jhash_1word */
619 +};
620 +
621 +struct ip_set_req_ipporthash_create {
622 +       uint32_t hashsize;
623 +       uint16_t probes;
624 +       uint16_t resize;
625 +       ip_set_ip_t from;
626 +       ip_set_ip_t to;
627 +};
628 +
629 +struct ip_set_req_ipporthash {
630 +       ip_set_ip_t ip;
631 +       ip_set_ip_t port;
632 +};
633 +
634 +#endif /* __IP_SET_IPPORTHASH_H */
635 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set_iptree.h linux-2.6/include/linux/netfilter_ipv4/ip_set_iptree.h
636 --- linux-2.6./include/linux/netfilter_ipv4/ip_set_iptree.h     1970-01-01 01:00:00.000000000 +0100
637 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set_iptree.h      2006-05-08 11:52:48.000000000 +0200
638 @@ -0,0 +1,40 @@
639 +#ifndef __IP_SET_IPTREE_H
640 +#define __IP_SET_IPTREE_H
641 +
642 +#include <linux/netfilter_ipv4/ip_set.h>
643 +
644 +#define SETTYPE_NAME "iptree"
645 +#define MAX_RANGE 0x0000FFFF
646 +
647 +struct ip_set_iptreed {
648 +       unsigned long expires[256];             /* x.x.x.ADDR */
649 +};
650 +
651 +struct ip_set_iptreec {
652 +       struct ip_set_iptreed *tree[256];       /* x.x.ADDR.* */
653 +};
654 +
655 +struct ip_set_iptreeb {
656 +       struct ip_set_iptreec *tree[256];       /* x.ADDR.*.* */
657 +};
658 +
659 +struct ip_set_iptree {
660 +       unsigned int timeout;
661 +       unsigned int gc_interval;
662 +#ifdef __KERNEL__
663 +       uint32_t elements;              /* number of elements */
664 +       struct timer_list gc;
665 +       struct ip_set_iptreeb *tree[256];       /* ADDR.*.*.* */
666 +#endif
667 +};
668 +
669 +struct ip_set_req_iptree_create {
670 +       unsigned int timeout;
671 +};
672 +
673 +struct ip_set_req_iptree {
674 +       ip_set_ip_t ip;
675 +       unsigned int timeout;
676 +};
677 +
678 +#endif /* __IP_SET_IPTREE_H */
679 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set_iptreemap.h linux-2.6/include/linux/netfilter_ipv4/ip_set_iptreemap.h
680 --- linux-2.6./include/linux/netfilter_ipv4/ip_set_iptreemap.h  1970-01-01 01:00:00.000000000 +0100
681 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set_iptreemap.h   2007-08-28 13:29:12.000000000 +0200
682 @@ -0,0 +1,40 @@
683 +#ifndef __IP_SET_IPTREEMAP_H
684 +#define __IP_SET_IPTREEMAP_H
685 +
686 +#include <linux/netfilter_ipv4/ip_set.h>
687 +
688 +#define SETTYPE_NAME "iptreemap"
689 +
690 +#ifdef __KERNEL__
691 +struct ip_set_iptreemap_d {
692 +       unsigned char bitmap[32]; /* x.x.x.y */
693 +};
694 +
695 +struct ip_set_iptreemap_c {
696 +       struct ip_set_iptreemap_d *tree[256]; /* x.x.y.x */
697 +};
698 +
699 +struct ip_set_iptreemap_b {
700 +       struct ip_set_iptreemap_c *tree[256]; /* x.y.x.x */
701 +       unsigned char dirty[32];
702 +};
703 +#endif
704 +
705 +struct ip_set_iptreemap {
706 +       unsigned int gc_interval;
707 +#ifdef __KERNEL__
708 +       struct timer_list gc;
709 +       struct ip_set_iptreemap_b *tree[256]; /* y.x.x.x */
710 +#endif
711 +};
712 +
713 +struct ip_set_req_iptreemap_create {
714 +       unsigned int gc_interval;
715 +};
716 +
717 +struct ip_set_req_iptreemap {
718 +       ip_set_ip_t start;
719 +       ip_set_ip_t end;
720 +};
721 +
722 +#endif /* __IP_SET_IPTREEMAP_H */
723 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set_jhash.h linux-2.6/include/linux/netfilter_ipv4/ip_set_jhash.h
724 --- linux-2.6./include/linux/netfilter_ipv4/ip_set_jhash.h      1970-01-01 01:00:00.000000000 +0100
725 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set_jhash.h       2004-12-01 10:49:36.000000000 +0100
726 @@ -0,0 +1,148 @@
727 +#ifndef _LINUX_IPSET_JHASH_H
728 +#define _LINUX_IPSET_JHASH_H
729 +
730 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
731 + * to __u32/__u8 so that the header file can be included into
732 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
733 + */
734 +
735 +/* jhash.h: Jenkins hash support.
736 + *
737 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
738 + *
739 + * http://burtleburtle.net/bob/hash/
740 + *
741 + * These are the credits from Bob's sources:
742 + *
743 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
744 + * hash(), hash2(), hash3, and mix() are externally useful functions.
745 + * Routines to test the hash are included if SELF_TEST is defined.
746 + * You can use this free for any purpose.  It has no warranty.
747 + *
748 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
749 + *
750 + * I've modified Bob's hash to be useful in the Linux kernel, and
751 + * any bugs present are surely my fault.  -DaveM
752 + */
753 +
754 +/* NOTE: Arguments are modified. */
755 +#define __jhash_mix(a, b, c) \
756 +{ \
757 +  a -= b; a -= c; a ^= (c>>13); \
758 +  b -= c; b -= a; b ^= (a<<8); \
759 +  c -= a; c -= b; c ^= (b>>13); \
760 +  a -= b; a -= c; a ^= (c>>12);  \
761 +  b -= c; b -= a; b ^= (a<<16); \
762 +  c -= a; c -= b; c ^= (b>>5); \
763 +  a -= b; a -= c; a ^= (c>>3);  \
764 +  b -= c; b -= a; b ^= (a<<10); \
765 +  c -= a; c -= b; c ^= (b>>15); \
766 +}
767 +
768 +/* The golden ration: an arbitrary value */
769 +#define JHASH_GOLDEN_RATIO     0x9e3779b9
770 +
771 +/* The most generic version, hashes an arbitrary sequence
772 + * of bytes.  No alignment or length assumptions are made about
773 + * the input key.
774 + */
775 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
776 +{
777 +       __u32 a, b, c, len;
778 +       __u8 *k = key;
779 +
780 +       len = length;
781 +       a = b = JHASH_GOLDEN_RATIO;
782 +       c = initval;
783 +
784 +       while (len >= 12) {
785 +               a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
786 +               b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
787 +               c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
788 +
789 +               __jhash_mix(a,b,c);
790 +
791 +               k += 12;
792 +               len -= 12;
793 +       }
794 +
795 +       c += length;
796 +       switch (len) {
797 +       case 11: c += ((__u32)k[10]<<24);
798 +       case 10: c += ((__u32)k[9]<<16);
799 +       case 9 : c += ((__u32)k[8]<<8);
800 +       case 8 : b += ((__u32)k[7]<<24);
801 +       case 7 : b += ((__u32)k[6]<<16);
802 +       case 6 : b += ((__u32)k[5]<<8);
803 +       case 5 : b += k[4];
804 +       case 4 : a += ((__u32)k[3]<<24);
805 +       case 3 : a += ((__u32)k[2]<<16);
806 +       case 2 : a += ((__u32)k[1]<<8);
807 +       case 1 : a += k[0];
808 +       };
809 +
810 +       __jhash_mix(a,b,c);
811 +
812 +       return c;
813 +}
814 +
815 +/* A special optimized version that handles 1 or more of __u32s.
816 + * The length parameter here is the number of __u32s in the key.
817 + */
818 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
819 +{
820 +       __u32 a, b, c, len;
821 +
822 +       a = b = JHASH_GOLDEN_RATIO;
823 +       c = initval;
824 +       len = length;
825 +
826 +       while (len >= 3) {
827 +               a += k[0];
828 +               b += k[1];
829 +               c += k[2];
830 +               __jhash_mix(a, b, c);
831 +               k += 3; len -= 3;
832 +       }
833 +
834 +       c += length * 4;
835 +
836 +       switch (len) {
837 +       case 2 : b += k[1];
838 +       case 1 : a += k[0];
839 +       };
840 +
841 +       __jhash_mix(a,b,c);
842 +
843 +       return c;
844 +}
845 +
846 +
847 +/* A special ultra-optimized versions that knows they are hashing exactly
848 + * 3, 2 or 1 word(s).
849 + *
850 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
851 + *       done at the end is not done here.
852 + */
853 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
854 +{
855 +       a += JHASH_GOLDEN_RATIO;
856 +       b += JHASH_GOLDEN_RATIO;
857 +       c += initval;
858 +
859 +       __jhash_mix(a, b, c);
860 +
861 +       return c;
862 +}
863 +
864 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
865 +{
866 +       return jhash_3words(a, b, 0, initval);
867 +}
868 +
869 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
870 +{
871 +       return jhash_3words(a, 0, 0, initval);
872 +}
873 +
874 +#endif /* _LINUX_IPSET_JHASH_H */
875 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set_macipmap.h linux-2.6/include/linux/netfilter_ipv4/ip_set_macipmap.h
876 --- linux-2.6./include/linux/netfilter_ipv4/ip_set_macipmap.h   1970-01-01 01:00:00.000000000 +0100
877 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set_macipmap.h    2004-12-01 10:49:36.000000000 +0100
878 @@ -0,0 +1,38 @@
879 +#ifndef __IP_SET_MACIPMAP_H
880 +#define __IP_SET_MACIPMAP_H
881 +
882 +#include <linux/netfilter_ipv4/ip_set.h>
883 +
884 +#define SETTYPE_NAME "macipmap"
885 +#define MAX_RANGE 0x0000FFFF
886 +
887 +/* general flags */
888 +#define IPSET_MACIP_MATCHUNSET 1
889 +
890 +/* per ip flags */
891 +#define IPSET_MACIP_ISSET      1
892 +
893 +struct ip_set_macipmap {
894 +       void *members;                  /* the macipmap proper */
895 +       ip_set_ip_t first_ip;           /* host byte order, included in range */
896 +       ip_set_ip_t last_ip;            /* host byte order, included in range */
897 +       u_int32_t flags;
898 +};
899 +
900 +struct ip_set_req_macipmap_create {
901 +       ip_set_ip_t from;
902 +       ip_set_ip_t to;
903 +       u_int32_t flags;
904 +};
905 +
906 +struct ip_set_req_macipmap {
907 +       ip_set_ip_t ip;
908 +       unsigned char ethernet[ETH_ALEN];
909 +};
910 +
911 +struct ip_set_macip {
912 +       unsigned short flags;
913 +       unsigned char ethernet[ETH_ALEN];
914 +};
915 +
916 +#endif /* __IP_SET_MACIPMAP_H */
917 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set_malloc.h linux-2.6/include/linux/netfilter_ipv4/ip_set_malloc.h
918 --- linux-2.6./include/linux/netfilter_ipv4/ip_set_malloc.h     1970-01-01 01:00:00.000000000 +0100
919 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set_malloc.h      2005-10-13 10:53:58.000000000 +0200
920 @@ -0,0 +1,116 @@
921 +#ifndef _IP_SET_MALLOC_H
922 +#define _IP_SET_MALLOC_H
923 +
924 +#ifdef __KERNEL__
925 +
926 +/* Memory allocation and deallocation */
927 +static size_t max_malloc_size = 0;
928 +
929 +static inline void init_max_malloc_size(void)
930 +{
931 +#define CACHE(x) max_malloc_size = x;
932 +#include <linux/kmalloc_sizes.h>
933 +#undef CACHE
934 +}
935 +
936 +static inline void * ip_set_malloc(size_t bytes)
937 +{
938 +       if (bytes > max_malloc_size)
939 +               return vmalloc(bytes);
940 +       else
941 +               return kmalloc(bytes, GFP_KERNEL);
942 +}
943 +
944 +static inline void ip_set_free(void * data, size_t bytes)
945 +{
946 +       if (bytes > max_malloc_size)
947 +               vfree(data);
948 +       else
949 +               kfree(data);
950 +}
951 +
952 +struct harray {
953 +       size_t max_elements;
954 +       void *arrays[0];
955 +};
956 +
957 +static inline void * 
958 +harray_malloc(size_t hashsize, size_t typesize, int flags)
959 +{
960 +       struct harray *harray;
961 +       size_t max_elements, size, i, j;
962 +
963 +       if (!max_malloc_size)
964 +               init_max_malloc_size();
965 +
966 +       if (typesize > max_malloc_size)
967 +               return NULL;
968 +
969 +       max_elements = max_malloc_size/typesize;
970 +       size = hashsize/max_elements;
971 +       if (hashsize % max_elements)
972 +               size++;
973 +       
974 +       /* Last pointer signals end of arrays */
975 +       harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
976 +                        flags);
977 +
978 +       if (!harray)
979 +               return NULL;
980 +       
981 +       for (i = 0; i < size - 1; i++) {
982 +               harray->arrays[i] = kmalloc(max_elements * typesize, flags);
983 +               if (!harray->arrays[i])
984 +                       goto undo;
985 +               memset(harray->arrays[i], 0, max_elements * typesize);
986 +       }
987 +       harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize, 
988 +                                   flags);
989 +       if (!harray->arrays[i])
990 +               goto undo;
991 +       memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
992 +
993 +       harray->max_elements = max_elements;
994 +       harray->arrays[size] = NULL;
995 +       
996 +       return (void *)harray;
997 +
998 +    undo:
999 +       for (j = 0; j < i; j++) {
1000 +               kfree(harray->arrays[j]);
1001 +       }
1002 +       kfree(harray);
1003 +       return NULL;
1004 +}
1005 +
1006 +static inline void harray_free(void *h)
1007 +{
1008 +       struct harray *harray = (struct harray *) h;
1009 +       size_t i;
1010 +       
1011 +       for (i = 0; harray->arrays[i] != NULL; i++)
1012 +               kfree(harray->arrays[i]);
1013 +       kfree(harray);
1014 +}
1015 +
1016 +static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
1017 +{
1018 +       struct harray *harray = (struct harray *) h;
1019 +       size_t i;
1020 +       
1021 +       for (i = 0; harray->arrays[i+1] != NULL; i++)
1022 +               memset(harray->arrays[i], 0, harray->max_elements * typesize);
1023 +       memset(harray->arrays[i], 0, 
1024 +              (hashsize - i * harray->max_elements) * typesize);
1025 +}
1026 +
1027 +#define HARRAY_ELEM(h, type, which)                            \
1028 +({                                                             \
1029 +       struct harray *__h = (struct harray *)(h);              \
1030 +       ((type)((__h)->arrays[(which)/(__h)->max_elements])     \
1031 +               + (which)%(__h)->max_elements);                 \
1032 +})
1033 +
1034 +#endif                         /* __KERNEL__ */
1035 +
1036 +#endif /*_IP_SET_MALLOC_H*/
1037 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set_nethash.h linux-2.6/include/linux/netfilter_ipv4/ip_set_nethash.h
1038 --- linux-2.6./include/linux/netfilter_ipv4/ip_set_nethash.h    1970-01-01 01:00:00.000000000 +0100
1039 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set_nethash.h     2006-05-08 11:52:48.000000000 +0200
1040 @@ -0,0 +1,55 @@
1041 +#ifndef __IP_SET_NETHASH_H
1042 +#define __IP_SET_NETHASH_H
1043 +
1044 +#include <linux/netfilter_ipv4/ip_set.h>
1045 +
1046 +#define SETTYPE_NAME "nethash"
1047 +#define MAX_RANGE 0x0000FFFF
1048 +
1049 +struct ip_set_nethash {
1050 +       ip_set_ip_t *members;           /* the nethash proper */
1051 +       uint32_t elements;              /* number of elements */
1052 +       uint32_t hashsize;              /* hash size */
1053 +       uint16_t probes;                /* max number of probes  */
1054 +       uint16_t resize;                /* resize factor in percent */
1055 +       unsigned char cidr[30];         /* CIDR sizes */
1056 +       void *initval[0];               /* initvals for jhash_1word */
1057 +};
1058 +
1059 +struct ip_set_req_nethash_create {
1060 +       uint32_t hashsize;
1061 +       uint16_t probes;
1062 +       uint16_t resize;
1063 +};
1064 +
1065 +struct ip_set_req_nethash {
1066 +       ip_set_ip_t ip;
1067 +       unsigned char cidr;
1068 +};
1069 +
1070 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
1071 +
1072 +static inline ip_set_ip_t 
1073 +pack(ip_set_ip_t ip, unsigned char cidr)
1074 +{
1075 +       ip_set_ip_t addr, *paddr = &addr;
1076 +       unsigned char n, t, *a;
1077 +
1078 +       addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
1079 +#ifdef __KERNEL__
1080 +       DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
1081 +#endif
1082 +       n = cidr / 8;
1083 +       t = cidr % 8;   
1084 +       a = &((unsigned char *)paddr)[n];
1085 +       *a = *a /(1 << (8 - t)) + shifts[t];
1086 +#ifdef __KERNEL__
1087 +       DP("n: %u, t: %u, a: %u", n, t, *a);
1088 +       DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
1089 +          HIPQUAD(ip), cidr, NIPQUAD(addr));
1090 +#endif
1091 +
1092 +       return ntohl(addr);
1093 +}
1094 +
1095 +#endif /* __IP_SET_NETHASH_H */
1096 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ip_set_portmap.h linux-2.6/include/linux/netfilter_ipv4/ip_set_portmap.h
1097 --- linux-2.6./include/linux/netfilter_ipv4/ip_set_portmap.h    1970-01-01 01:00:00.000000000 +0100
1098 +++ linux-2.6/include/linux/netfilter_ipv4/ip_set_portmap.h     2004-12-01 10:49:36.000000000 +0100
1099 @@ -0,0 +1,25 @@
1100 +#ifndef __IP_SET_PORTMAP_H
1101 +#define __IP_SET_PORTMAP_H
1102 +
1103 +#include <linux/netfilter_ipv4/ip_set.h>
1104 +
1105 +#define SETTYPE_NAME   "portmap"
1106 +#define MAX_RANGE      0x0000FFFF
1107 +#define INVALID_PORT   (MAX_RANGE + 1)
1108 +
1109 +struct ip_set_portmap {
1110 +       void *members;                  /* the portmap proper */
1111 +       ip_set_ip_t first_port;         /* host byte order, included in range */
1112 +       ip_set_ip_t last_port;          /* host byte order, included in range */
1113 +};
1114 +
1115 +struct ip_set_req_portmap_create {
1116 +       ip_set_ip_t from;
1117 +       ip_set_ip_t to;
1118 +};
1119 +
1120 +struct ip_set_req_portmap {
1121 +       ip_set_ip_t port;
1122 +};
1123 +
1124 +#endif /* __IP_SET_PORTMAP_H */
1125 diff -uNrp linux-2.6./include/linux/netfilter_ipv4/ipt_set.h linux-2.6/include/linux/netfilter_ipv4/ipt_set.h
1126 --- linux-2.6./include/linux/netfilter_ipv4/ipt_set.h   1970-01-01 01:00:00.000000000 +0100
1127 +++ linux-2.6/include/linux/netfilter_ipv4/ipt_set.h    2004-12-06 10:32:59.000000000 +0100
1128 @@ -0,0 +1,21 @@
1129 +#ifndef _IPT_SET_H
1130 +#define _IPT_SET_H
1131 +
1132 +#include <linux/netfilter_ipv4/ip_set.h>
1133 +
1134 +struct ipt_set_info {
1135 +       ip_set_id_t index;
1136 +       u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1137 +};
1138 +
1139 +/* match info */
1140 +struct ipt_set_info_match {
1141 +       struct ipt_set_info match_set;
1142 +};
1143 +
1144 +struct ipt_set_info_target {
1145 +       struct ipt_set_info add_set;
1146 +       struct ipt_set_info del_set;
1147 +};
1148 +
1149 +#endif /*_IPT_SET_H*/
1150 diff -uNrp linux-2.6./net/ipv4/netfilter/ip_set.c linux-2.6/net/ipv4/netfilter/ip_set.c
1151 --- linux-2.6./net/ipv4/netfilter/ip_set.c      1970-01-01 01:00:00.000000000 +0100
1152 +++ linux-2.6/net/ipv4/netfilter/ip_set.c       2007-08-28 13:29:12.000000000 +0200
1153 @@ -0,0 +1,2001 @@
1154 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1155 + *                         Patrick Schaaf <bof@bof.de>
1156 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1157 + *
1158 + * This program is free software; you can redistribute it and/or modify
1159 + * it under the terms of the GNU General Public License version 2 as
1160 + * published by the Free Software Foundation.  
1161 + */
1162 +
1163 +/* Kernel module for IP set management */
1164 +
1165 +#include <linux/version.h>
1166 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
1167 +#include <linux/config.h>
1168 +#endif
1169 +#include <linux/module.h>
1170 +#include <linux/moduleparam.h>
1171 +#include <linux/kmod.h>
1172 +#include <linux/ip.h>
1173 +#include <linux/skbuff.h>
1174 +#include <linux/random.h>
1175 +#include <linux/jhash.h>
1176 +#include <linux/netfilter_ipv4/ip_tables.h>
1177 +#include <linux/errno.h>
1178 +#include <asm/uaccess.h>
1179 +#include <asm/bitops.h>
1180 +#include <asm/semaphore.h>
1181 +#include <linux/spinlock.h>
1182 +#include <linux/vmalloc.h>
1183 +
1184 +#define ASSERT_READ_LOCK(x)
1185 +#define ASSERT_WRITE_LOCK(x)
1186 +#include <linux/netfilter_ipv4/ip_set.h>
1187 +
1188 +static struct list_head set_type_list;         /* all registered sets */
1189 +static struct ip_set **ip_set_list;            /* all individual sets */
1190 +static DEFINE_RWLOCK(ip_set_lock);             /* protects the lists and the hash */
1191 +static DECLARE_MUTEX(ip_set_app_mutex);                /* serializes user access */
1192 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1193 +static ip_set_id_t ip_set_bindings_hash_size =  CONFIG_IP_NF_SET_HASHSIZE;
1194 +static struct list_head *ip_set_hash;          /* hash of bindings */
1195 +static unsigned int ip_set_hash_random;                /* random seed */
1196 +
1197 +/*
1198 + * Sets are identified either by the index in ip_set_list or by id.
1199 + * The id never changes and is used to find a key in the hash. 
1200 + * The index may change by swapping and used at all other places 
1201 + * (set/SET netfilter modules, binding value, etc.)
1202 + *
1203 + * Userspace requests are serialized by ip_set_mutex and sets can
1204 + * be deleted only from userspace. Therefore ip_set_list locking 
1205 + * must obey the following rules:
1206 + *
1207 + * - kernel requests: read and write locking mandatory
1208 + * - user requests: read locking optional, write locking mandatory
1209 + */
1210 +
1211 +static inline void
1212 +__ip_set_get(ip_set_id_t index)
1213 +{
1214 +       atomic_inc(&ip_set_list[index]->ref);
1215 +}
1216 +
1217 +static inline void
1218 +__ip_set_put(ip_set_id_t index)
1219 +{
1220 +       atomic_dec(&ip_set_list[index]->ref);
1221 +}
1222 +
1223 +/*
1224 + * Binding routines
1225 + */
1226 +
1227 +static inline struct ip_set_hash *
1228 +__ip_set_find(u_int32_t key, ip_set_id_t id, ip_set_ip_t ip)
1229 +{
1230 +       struct ip_set_hash *set_hash;
1231 +
1232 +       list_for_each_entry(set_hash, &ip_set_hash[key], list)
1233 +               if (set_hash->id == id && set_hash->ip == ip)
1234 +                       return set_hash;
1235 +                       
1236 +       return NULL;
1237 +}
1238 +
1239 +static ip_set_id_t
1240 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1241 +{
1242 +       u_int32_t key = jhash_2words(id, ip, ip_set_hash_random) 
1243 +                               % ip_set_bindings_hash_size;
1244 +       struct ip_set_hash *set_hash;
1245 +
1246 +       ASSERT_READ_LOCK(&ip_set_lock);
1247 +       IP_SET_ASSERT(ip_set_list[id]);
1248 +       DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));     
1249 +       
1250 +       set_hash = __ip_set_find(key, id, ip);
1251 +       
1252 +       DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name, 
1253 +          HIPQUAD(ip),
1254 +          set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1255 +
1256 +       return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1257 +}
1258 +
1259 +static inline void 
1260 +__set_hash_del(struct ip_set_hash *set_hash)
1261 +{
1262 +       ASSERT_WRITE_LOCK(&ip_set_lock);
1263 +       IP_SET_ASSERT(ip_set_list[set_hash->binding]);  
1264 +
1265 +       __ip_set_put(set_hash->binding);
1266 +       list_del(&set_hash->list);
1267 +       kfree(set_hash);
1268 +}
1269 +
1270 +static int
1271 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1272 +{
1273 +       u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1274 +                               % ip_set_bindings_hash_size;
1275 +       struct ip_set_hash *set_hash;
1276 +       
1277 +       IP_SET_ASSERT(ip_set_list[id]);
1278 +       DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));     
1279 +       write_lock_bh(&ip_set_lock);
1280 +       set_hash = __ip_set_find(key, id, ip);
1281 +       DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1282 +          HIPQUAD(ip),
1283 +          set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1284 +
1285 +       if (set_hash != NULL)
1286 +               __set_hash_del(set_hash);
1287 +       write_unlock_bh(&ip_set_lock);
1288 +       return 0;
1289 +}
1290 +
1291 +static int 
1292 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1293 +{
1294 +       u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1295 +                               % ip_set_bindings_hash_size;
1296 +       struct ip_set_hash *set_hash;
1297 +       int ret = 0;
1298 +       
1299 +       IP_SET_ASSERT(ip_set_list[id]);
1300 +       IP_SET_ASSERT(ip_set_list[binding]);
1301 +       DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name, 
1302 +          HIPQUAD(ip), ip_set_list[binding]->name);
1303 +       write_lock_bh(&ip_set_lock);
1304 +       set_hash = __ip_set_find(key, id, ip);
1305 +       if (!set_hash) {
1306 +               set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_ATOMIC);
1307 +               if (!set_hash) {
1308 +                       ret = -ENOMEM;
1309 +                       goto unlock;
1310 +               }
1311 +               INIT_LIST_HEAD(&set_hash->list);
1312 +               set_hash->id = id;
1313 +               set_hash->ip = ip;
1314 +               list_add(&set_hash->list, &ip_set_hash[key]);
1315 +       } else {
1316 +               IP_SET_ASSERT(ip_set_list[set_hash->binding]);  
1317 +               DP("overwrite binding: %s",
1318 +                  ip_set_list[set_hash->binding]->name);
1319 +               __ip_set_put(set_hash->binding);
1320 +       }
1321 +       set_hash->binding = binding;
1322 +       __ip_set_get(set_hash->binding);
1323 +       DP("stored: key %u, id %u (%s), ip %u.%u.%u.%u, binding %u (%s)",
1324 +          key, id, ip_set_list[id]->name,
1325 +          HIPQUAD(ip), binding, ip_set_list[binding]->name);
1326 +    unlock:
1327 +       write_unlock_bh(&ip_set_lock);
1328 +       return ret;
1329 +}
1330 +
1331 +#define FOREACH_HASH_DO(fn, args...)                                           \
1332 +({                                                                             \
1333 +       ip_set_id_t __key;                                                      \
1334 +       struct ip_set_hash *__set_hash;                                         \
1335 +                                                                               \
1336 +       for (__key = 0; __key < ip_set_bindings_hash_size; __key++) {           \
1337 +               list_for_each_entry(__set_hash, &ip_set_hash[__key], list)      \
1338 +                       fn(__set_hash , ## args);                               \
1339 +       }                                                                       \
1340 +})
1341 +
1342 +#define FOREACH_HASH_RW_DO(fn, args...)                                                \
1343 +({                                                                             \
1344 +       ip_set_id_t __key;                                                      \
1345 +       struct ip_set_hash *__set_hash, *__n;                                   \
1346 +                                                                               \
1347 +       ASSERT_WRITE_LOCK(&ip_set_lock);                                        \
1348 +       for (__key = 0; __key < ip_set_bindings_hash_size; __key++) {           \
1349 +               list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1350 +                       fn(__set_hash , ## args);                               \
1351 +       }                                                                       \
1352 +})
1353 +
1354 +/* Add, del and test set entries from kernel */
1355 +
1356 +#define follow_bindings(index, set, ip)                                        \
1357 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID     \
1358 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1359 +
1360 +int
1361 +ip_set_testip_kernel(ip_set_id_t index,
1362 +                    const struct sk_buff *skb,
1363 +                    const u_int32_t *flags)
1364 +{
1365 +       struct ip_set *set;
1366 +       ip_set_ip_t ip;
1367 +       int res;
1368 +       unsigned char i = 0;
1369 +       
1370 +       IP_SET_ASSERT(flags[i]);
1371 +       read_lock_bh(&ip_set_lock);
1372 +       do {
1373 +               set = ip_set_list[index];
1374 +               IP_SET_ASSERT(set);
1375 +               DP("set %s, index %u", set->name, index);
1376 +               read_lock_bh(&set->lock);
1377 +               res = set->type->testip_kernel(set, skb, &ip, flags, i++);
1378 +               read_unlock_bh(&set->lock);
1379 +               i += !!(set->type->features & IPSET_DATA_DOUBLE);
1380 +       } while (res > 0 
1381 +                && flags[i] 
1382 +                && follow_bindings(index, set, ip));
1383 +       read_unlock_bh(&ip_set_lock);
1384 +
1385 +       return res;
1386 +}
1387 +
1388 +void
1389 +ip_set_addip_kernel(ip_set_id_t index,
1390 +                   const struct sk_buff *skb,
1391 +                   const u_int32_t *flags)
1392 +{
1393 +       struct ip_set *set;
1394 +       ip_set_ip_t ip;
1395 +       int res;
1396 +       unsigned char i = 0;
1397 +
1398 +       IP_SET_ASSERT(flags[i]);
1399 +   retry:
1400 +       read_lock_bh(&ip_set_lock);
1401 +       do {
1402 +               set = ip_set_list[index];
1403 +               IP_SET_ASSERT(set);
1404 +               DP("set %s, index %u", set->name, index);
1405 +               write_lock_bh(&set->lock);
1406 +               res = set->type->addip_kernel(set, skb, &ip, flags, i++);
1407 +               write_unlock_bh(&set->lock);
1408 +               i += !!(set->type->features & IPSET_DATA_DOUBLE);
1409 +       } while ((res == 0 || res == -EEXIST)
1410 +                && flags[i] 
1411 +                && follow_bindings(index, set, ip));
1412 +       read_unlock_bh(&ip_set_lock);
1413 +
1414 +       if (res == -EAGAIN
1415 +           && set->type->retry
1416 +           && (res = set->type->retry(set)) == 0)
1417 +               goto retry;
1418 +}
1419 +
1420 +void
1421 +ip_set_delip_kernel(ip_set_id_t index,
1422 +                   const struct sk_buff *skb,
1423 +                   const u_int32_t *flags)
1424 +{
1425 +       struct ip_set *set;
1426 +       ip_set_ip_t ip;
1427 +       int res;
1428 +       unsigned char i = 0;
1429 +
1430 +       IP_SET_ASSERT(flags[i]);
1431 +       read_lock_bh(&ip_set_lock);
1432 +       do {
1433 +               set = ip_set_list[index];
1434 +               IP_SET_ASSERT(set);
1435 +               DP("set %s, index %u", set->name, index);
1436 +               write_lock_bh(&set->lock);
1437 +               res = set->type->delip_kernel(set, skb, &ip, flags, i++);
1438 +               write_unlock_bh(&set->lock);
1439 +               i += !!(set->type->features & IPSET_DATA_DOUBLE);
1440 +       } while ((res == 0 || res == -EEXIST)
1441 +                && flags[i] 
1442 +                && follow_bindings(index, set, ip));
1443 +       read_unlock_bh(&ip_set_lock);
1444 +}
1445 +
1446 +/* Register and deregister settype */
1447 +
1448 +static inline struct ip_set_type *
1449 +find_set_type(const char *name)
1450 +{
1451 +       struct ip_set_type *set_type;
1452 +
1453 +       list_for_each_entry(set_type, &set_type_list, list)
1454 +               if (!strncmp(set_type->typename, name, IP_SET_MAXNAMELEN - 1))
1455 +                       return set_type;
1456 +       return NULL;
1457 +}
1458 +
1459 +int 
1460 +ip_set_register_set_type(struct ip_set_type *set_type)
1461 +{
1462 +       int ret = 0;
1463 +       
1464 +       if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1465 +               ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1466 +                             set_type->typename,
1467 +                             set_type->protocol_version,
1468 +                             IP_SET_PROTOCOL_VERSION);
1469 +               return -EINVAL;
1470 +       }
1471 +
1472 +       write_lock_bh(&ip_set_lock);
1473 +       if (find_set_type(set_type->typename)) {
1474 +               /* Duplicate! */
1475 +               ip_set_printk("'%s' already registered!", 
1476 +                             set_type->typename);
1477 +               ret = -EINVAL;
1478 +               goto unlock;
1479 +       }
1480 +       if (!try_module_get(THIS_MODULE)) {
1481 +               ret = -EFAULT;
1482 +               goto unlock;
1483 +       }
1484 +       list_add(&set_type->list, &set_type_list);
1485 +       DP("'%s' registered.", set_type->typename);
1486 +   unlock:
1487 +       write_unlock_bh(&ip_set_lock);
1488 +       return ret;
1489 +}
1490 +
1491 +void
1492 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1493 +{
1494 +       write_lock_bh(&ip_set_lock);
1495 +       if (!find_set_type(set_type->typename)) {
1496 +               ip_set_printk("'%s' not registered?",
1497 +                             set_type->typename);
1498 +               goto unlock;
1499 +       }
1500 +       list_del(&set_type->list);
1501 +       module_put(THIS_MODULE);
1502 +       DP("'%s' unregistered.", set_type->typename);
1503 +   unlock:
1504 +       write_unlock_bh(&ip_set_lock);
1505 +
1506 +}
1507 +
1508 +/*
1509 + * Userspace routines
1510 + */
1511 +
1512 +/*
1513 + * Find set by name, reference it once. The reference makes sure the
1514 + * thing pointed to, does not go away under our feet. Drop the reference
1515 + * later, using ip_set_put().
1516 + */
1517 +ip_set_id_t
1518 +ip_set_get_byname(const char *name)
1519 +{
1520 +       ip_set_id_t i, index = IP_SET_INVALID_ID;
1521 +       
1522 +       down(&ip_set_app_mutex);
1523 +       for (i = 0; i < ip_set_max; i++) {
1524 +               if (ip_set_list[i] != NULL
1525 +                   && strcmp(ip_set_list[i]->name, name) == 0) {
1526 +                       __ip_set_get(i);
1527 +                       index = i;
1528 +                       break;
1529 +               }
1530 +       }
1531 +       up(&ip_set_app_mutex);
1532 +       return index;
1533 +}
1534 +
1535 +/*
1536 + * Find set by index, reference it once. The reference makes sure the
1537 + * thing pointed to, does not go away under our feet. Drop the reference
1538 + * later, using ip_set_put().
1539 + */
1540 +ip_set_id_t
1541 +ip_set_get_byindex(ip_set_id_t index)
1542 +{
1543 +       down(&ip_set_app_mutex);
1544 +
1545 +       if (index >= ip_set_max)
1546 +               return IP_SET_INVALID_ID;
1547 +       
1548 +       if (ip_set_list[index])
1549 +               __ip_set_get(index);
1550 +       else
1551 +               index = IP_SET_INVALID_ID;
1552 +               
1553 +       up(&ip_set_app_mutex);
1554 +       return index;
1555 +}
1556 +
1557 +/*
1558 + * If the given set pointer points to a valid set, decrement
1559 + * reference count by 1. The caller shall not assume the index
1560 + * to be valid, after calling this function.
1561 + */
1562 +void ip_set_put(ip_set_id_t index)
1563 +{
1564 +       down(&ip_set_app_mutex);
1565 +       if (ip_set_list[index])
1566 +               __ip_set_put(index);
1567 +       up(&ip_set_app_mutex);
1568 +}
1569 +
1570 +/* Find a set by name or index */
1571 +static ip_set_id_t
1572 +ip_set_find_byname(const char *name)
1573 +{
1574 +       ip_set_id_t i, index = IP_SET_INVALID_ID;
1575 +       
1576 +       for (i = 0; i < ip_set_max; i++) {
1577 +               if (ip_set_list[i] != NULL
1578 +                   && strcmp(ip_set_list[i]->name, name) == 0) {
1579 +                       index = i;
1580 +                       break;
1581 +               }
1582 +       }
1583 +       return index;
1584 +}
1585 +
1586 +static ip_set_id_t
1587 +ip_set_find_byindex(ip_set_id_t index)
1588 +{
1589 +       if (index >= ip_set_max || ip_set_list[index] == NULL)
1590 +               index = IP_SET_INVALID_ID;
1591 +       
1592 +       return index;
1593 +}
1594 +
1595 +/*
1596 + * Add, del, test, bind and unbind
1597 + */
1598 +
1599 +static inline int
1600 +__ip_set_testip(struct ip_set *set,
1601 +               const void *data,
1602 +               size_t size,
1603 +               ip_set_ip_t *ip)
1604 +{
1605 +       int res;
1606 +
1607 +       read_lock_bh(&set->lock);
1608 +       res = set->type->testip(set, data, size, ip);
1609 +       read_unlock_bh(&set->lock);
1610 +
1611 +       return res;
1612 +}
1613 +
1614 +static int
1615 +__ip_set_addip(ip_set_id_t index,
1616 +              const void *data,
1617 +              size_t size)
1618 +{
1619 +       struct ip_set *set = ip_set_list[index];
1620 +       ip_set_ip_t ip;
1621 +       int res;
1622 +       
1623 +       IP_SET_ASSERT(set);
1624 +       do {
1625 +               write_lock_bh(&set->lock);
1626 +               res = set->type->addip(set, data, size, &ip);
1627 +               write_unlock_bh(&set->lock);
1628 +       } while (res == -EAGAIN
1629 +                && set->type->retry
1630 +                && (res = set->type->retry(set)) == 0);
1631 +
1632 +       return res;
1633 +}
1634 +
1635 +static int
1636 +ip_set_addip(ip_set_id_t index,
1637 +            const void *data,
1638 +            size_t size)
1639 +{
1640 +
1641 +       return __ip_set_addip(index,
1642 +                             data + sizeof(struct ip_set_req_adt),
1643 +                             size - sizeof(struct ip_set_req_adt));
1644 +}
1645 +
1646 +static int
1647 +ip_set_delip(ip_set_id_t index,
1648 +            const void *data,
1649 +            size_t size)
1650 +{
1651 +       struct ip_set *set = ip_set_list[index];
1652 +       ip_set_ip_t ip;
1653 +       int res;
1654 +       
1655 +       IP_SET_ASSERT(set);
1656 +       write_lock_bh(&set->lock);
1657 +       res = set->type->delip(set,
1658 +                              data + sizeof(struct ip_set_req_adt),
1659 +                              size - sizeof(struct ip_set_req_adt),
1660 +                              &ip);
1661 +       write_unlock_bh(&set->lock);
1662 +
1663 +       return res;
1664 +}
1665 +
1666 +static int
1667 +ip_set_testip(ip_set_id_t index,
1668 +             const void *data,
1669 +             size_t size)
1670 +{
1671 +       struct ip_set *set = ip_set_list[index];
1672 +       ip_set_ip_t ip;
1673 +       int res;
1674 +
1675 +       IP_SET_ASSERT(set);
1676 +       res = __ip_set_testip(set,
1677 +                             data + sizeof(struct ip_set_req_adt),
1678 +                             size - sizeof(struct ip_set_req_adt),
1679 +                             &ip);
1680 +
1681 +       return (res > 0 ? -EEXIST : res);
1682 +}
1683 +
1684 +static int
1685 +ip_set_bindip(ip_set_id_t index,
1686 +             const void *data,
1687 +             size_t size)
1688 +{
1689 +       struct ip_set *set = ip_set_list[index];
1690 +       struct ip_set_req_bind *req_bind;
1691 +       ip_set_id_t binding;
1692 +       ip_set_ip_t ip;
1693 +       int res;
1694 +
1695 +       IP_SET_ASSERT(set);
1696 +       if (size < sizeof(struct ip_set_req_bind))
1697 +               return -EINVAL;
1698 +               
1699 +       req_bind = (struct ip_set_req_bind *) data;
1700 +       req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1701 +
1702 +       if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1703 +               /* Default binding of a set */
1704 +               char *binding_name;
1705 +               
1706 +               if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1707 +                       return -EINVAL;
1708 +
1709 +               binding_name = (char *)(data + sizeof(struct ip_set_req_bind)); 
1710 +               binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1711 +
1712 +               binding = ip_set_find_byname(binding_name);
1713 +               if (binding == IP_SET_INVALID_ID)
1714 +                       return -ENOENT;
1715 +
1716 +               write_lock_bh(&ip_set_lock);
1717 +               /* Sets as binding values are referenced */
1718 +               if (set->binding != IP_SET_INVALID_ID)
1719 +                       __ip_set_put(set->binding);
1720 +               set->binding = binding;
1721 +               __ip_set_get(set->binding);
1722 +               write_unlock_bh(&ip_set_lock);
1723 +
1724 +               return 0;
1725 +       }
1726 +       binding = ip_set_find_byname(req_bind->binding);
1727 +       if (binding == IP_SET_INVALID_ID)
1728 +               return -ENOENT;
1729 +
1730 +       res = __ip_set_testip(set,
1731 +                             data + sizeof(struct ip_set_req_bind),
1732 +                             size - sizeof(struct ip_set_req_bind),
1733 +                             &ip);
1734 +       DP("set %s, ip: %u.%u.%u.%u, binding %s",
1735 +          set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1736 +       
1737 +       if (res >= 0)
1738 +               res = ip_set_hash_add(set->id, ip, binding);
1739 +
1740 +       return res;
1741 +}
1742 +
1743 +#define FOREACH_SET_DO(fn, args...)                            \
1744 +({                                                             \
1745 +       ip_set_id_t __i;                                        \
1746 +       struct ip_set *__set;                                   \
1747 +                                                               \
1748 +       for (__i = 0; __i < ip_set_max; __i++) {                \
1749 +               __set = ip_set_list[__i];                       \
1750 +               if (__set != NULL)                              \
1751 +                       fn(__set , ##args);                     \
1752 +       }                                                       \
1753 +})
1754 +
1755 +static inline void
1756 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1757 +{
1758 +       if (set_hash->id == id)
1759 +               __set_hash_del(set_hash);
1760 +}
1761 +
1762 +static inline void
1763 +__unbind_default(struct ip_set *set)
1764 +{
1765 +       if (set->binding != IP_SET_INVALID_ID) {
1766 +               /* Sets as binding values are referenced */
1767 +               __ip_set_put(set->binding);
1768 +               set->binding = IP_SET_INVALID_ID;
1769 +       }
1770 +}
1771 +
1772 +static int
1773 +ip_set_unbindip(ip_set_id_t index,
1774 +               const void *data,
1775 +               size_t size)
1776 +{
1777 +       struct ip_set *set;
1778 +       struct ip_set_req_bind *req_bind;
1779 +       ip_set_ip_t ip;
1780 +       int res;
1781 +
1782 +       DP("");
1783 +       if (size < sizeof(struct ip_set_req_bind))
1784 +               return -EINVAL;
1785 +               
1786 +       req_bind = (struct ip_set_req_bind *) data;
1787 +       req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1788 +       
1789 +       DP("%u %s", index, req_bind->binding);
1790 +       if (index == IP_SET_INVALID_ID) {
1791 +               /* unbind :all: */
1792 +               if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1793 +                       /* Default binding of sets */
1794 +                       write_lock_bh(&ip_set_lock);
1795 +                       FOREACH_SET_DO(__unbind_default);
1796 +                       write_unlock_bh(&ip_set_lock);
1797 +                       return 0;
1798 +               } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1799 +                       /* Flush all bindings of all sets*/
1800 +                       write_lock_bh(&ip_set_lock);
1801 +                       FOREACH_HASH_RW_DO(__set_hash_del);
1802 +                       write_unlock_bh(&ip_set_lock);
1803 +                       return 0;
1804 +               }
1805 +               DP("unreachable reached!");
1806 +               return -EINVAL;
1807 +       }
1808 +       
1809 +       set = ip_set_list[index];
1810 +       IP_SET_ASSERT(set);
1811 +       if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1812 +               /* Default binding of set */
1813 +               ip_set_id_t binding = ip_set_find_byindex(set->binding);
1814 +
1815 +               if (binding == IP_SET_INVALID_ID)
1816 +                       return -ENOENT;
1817 +                       
1818 +               write_lock_bh(&ip_set_lock);
1819 +               /* Sets in hash values are referenced */
1820 +               __ip_set_put(set->binding);
1821 +               set->binding = IP_SET_INVALID_ID;
1822 +               write_unlock_bh(&ip_set_lock);
1823 +
1824 +               return 0;
1825 +       } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1826 +               /* Flush all bindings */
1827 +
1828 +               write_lock_bh(&ip_set_lock);
1829 +               FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1830 +               write_unlock_bh(&ip_set_lock);
1831 +               return 0;
1832 +       }
1833 +       
1834 +       res = __ip_set_testip(set,
1835 +                             data + sizeof(struct ip_set_req_bind),
1836 +                             size - sizeof(struct ip_set_req_bind),
1837 +                             &ip);
1838 +
1839 +       DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1840 +       if (res >= 0)
1841 +               res = ip_set_hash_del(set->id, ip);
1842 +
1843 +       return res;
1844 +}
1845 +
1846 +static int
1847 +ip_set_testbind(ip_set_id_t index,
1848 +               const void *data,
1849 +               size_t size)
1850 +{
1851 +       struct ip_set *set = ip_set_list[index];
1852 +       struct ip_set_req_bind *req_bind;
1853 +       ip_set_id_t binding;
1854 +       ip_set_ip_t ip;
1855 +       int res;
1856 +
1857 +       IP_SET_ASSERT(set);
1858 +       if (size < sizeof(struct ip_set_req_bind))
1859 +               return -EINVAL;
1860 +               
1861 +       req_bind = (struct ip_set_req_bind *) data;
1862 +       req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1863 +
1864 +       if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1865 +               /* Default binding of set */
1866 +               char *binding_name;
1867 +               
1868 +               if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1869 +                       return -EINVAL;
1870 +
1871 +               binding_name = (char *)(data + sizeof(struct ip_set_req_bind)); 
1872 +               binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1873 +
1874 +               binding = ip_set_find_byname(binding_name);
1875 +               if (binding == IP_SET_INVALID_ID)
1876 +                       return -ENOENT;
1877 +               
1878 +               res = (set->binding == binding) ? -EEXIST : 0;
1879 +
1880 +               return res;
1881 +       }
1882 +       binding = ip_set_find_byname(req_bind->binding);
1883 +       if (binding == IP_SET_INVALID_ID)
1884 +               return -ENOENT;
1885 +               
1886 +       
1887 +       res = __ip_set_testip(set,
1888 +                             data + sizeof(struct ip_set_req_bind),
1889 +                             size - sizeof(struct ip_set_req_bind),
1890 +                             &ip);
1891 +       DP("set %s, ip: %u.%u.%u.%u, binding %s",
1892 +          set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1893 +          
1894 +       if (res >= 0)
1895 +               res = (ip_set_find_in_hash(set->id, ip) == binding)
1896 +                       ? -EEXIST : 0;
1897 +
1898 +       return res;
1899 +}
1900 +
1901 +static struct ip_set_type *
1902 +find_set_type_rlock(const char *typename)
1903 +{
1904 +       struct ip_set_type *type;
1905 +       
1906 +       read_lock_bh(&ip_set_lock);
1907 +       type = find_set_type(typename);
1908 +       if (type == NULL)
1909 +               read_unlock_bh(&ip_set_lock);
1910 +
1911 +       return type;
1912 +}
1913 +
1914 +static int
1915 +find_free_id(const char *name,
1916 +            ip_set_id_t *index,
1917 +            ip_set_id_t *id)
1918 +{
1919 +       ip_set_id_t i;
1920 +
1921 +       *id = IP_SET_INVALID_ID;
1922 +       for (i = 0;  i < ip_set_max; i++) {
1923 +               if (ip_set_list[i] == NULL) {
1924 +                       if (*id == IP_SET_INVALID_ID)
1925 +                               *id = *index = i;
1926 +               } else if (strcmp(name, ip_set_list[i]->name) == 0)
1927 +                       /* Name clash */
1928 +                       return -EEXIST;
1929 +       }
1930 +       if (*id == IP_SET_INVALID_ID)
1931 +               /* No free slot remained */
1932 +               return -ERANGE;
1933 +       /* Check that index is usable as id (swapping) */
1934 +    check:     
1935 +       for (i = 0;  i < ip_set_max; i++) {
1936 +               if (ip_set_list[i] != NULL
1937 +                   && ip_set_list[i]->id == *id) {
1938 +                   *id = i;
1939 +                   goto check;
1940 +               }
1941 +       }
1942 +       return 0;
1943 +}
1944 +
1945 +/*
1946 + * Create a set
1947 + */
1948 +static int
1949 +ip_set_create(const char *name,
1950 +             const char *typename,
1951 +             ip_set_id_t restore,
1952 +             const void *data,
1953 +             size_t size)
1954 +{
1955 +       struct ip_set *set;
1956 +       ip_set_id_t index = 0, id;
1957 +       int res = 0;
1958 +
1959 +       DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1960 +       /*
1961 +        * First, and without any locks, allocate and initialize
1962 +        * a normal base set structure.
1963 +        */
1964 +       set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1965 +       if (!set)
1966 +               return -ENOMEM;
1967 +       set->lock = RW_LOCK_UNLOCKED;
1968 +       strncpy(set->name, name, IP_SET_MAXNAMELEN);
1969 +       set->binding = IP_SET_INVALID_ID;
1970 +       atomic_set(&set->ref, 0);
1971 +
1972 +       /*
1973 +        * Next, take the &ip_set_lock, check that we know the type,
1974 +        * and take a reference on the type, to make sure it
1975 +        * stays available while constructing our new set.
1976 +        *
1977 +        * After referencing the type, we drop the &ip_set_lock,
1978 +        * and let the new set construction run without locks.
1979 +        */
1980 +       set->type = find_set_type_rlock(typename);
1981 +       if (set->type == NULL) {
1982 +               /* Try loading the module */
1983 +               char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1984 +               strcpy(modulename, "ip_set_");
1985 +               strcat(modulename, typename);
1986 +               DP("try to load %s", modulename);
1987 +               request_module(modulename);
1988 +               set->type = find_set_type_rlock(typename);
1989 +       }
1990 +       if (set->type == NULL) {
1991 +               ip_set_printk("no set type '%s', set '%s' not created",
1992 +                             typename, name);
1993 +               res = -ENOENT;
1994 +               goto out;
1995 +       }
1996 +       if (!try_module_get(set->type->me)) {
1997 +               read_unlock_bh(&ip_set_lock);
1998 +               res = -EFAULT;
1999 +               goto out;
2000 +       }
2001 +       read_unlock_bh(&ip_set_lock);
2002 +
2003 +       /*
2004 +        * Without holding any locks, create private part.
2005 +        */
2006 +       res = set->type->create(set, data, size);
2007 +       if (res != 0)
2008 +               goto put_out;
2009 +
2010 +       /* BTW, res==0 here. */
2011 +
2012 +       /*
2013 +        * Here, we have a valid, constructed set. &ip_set_lock again,
2014 +        * find free id/index and check that it is not already in 
2015 +        * ip_set_list.
2016 +        */
2017 +       write_lock_bh(&ip_set_lock);
2018 +       if ((res = find_free_id(set->name, &index, &id)) != 0) {
2019 +               DP("no free id!");
2020 +               goto cleanup;
2021 +       }
2022 +
2023 +       /* Make sure restore gets the same index */
2024 +       if (restore != IP_SET_INVALID_ID && index != restore) {
2025 +               DP("Can't restore, sets are screwed up");
2026 +               res = -ERANGE;
2027 +               goto cleanup;
2028 +       }
2029 +        
2030 +       /*
2031 +        * Finally! Add our shiny new set to the list, and be done.
2032 +        */
2033 +       DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2034 +       set->id = id;
2035 +       ip_set_list[index] = set;
2036 +       write_unlock_bh(&ip_set_lock);
2037 +       return res;
2038 +       
2039 +    cleanup:
2040 +       write_unlock_bh(&ip_set_lock);
2041 +       set->type->destroy(set);
2042 +    put_out:
2043 +       module_put(set->type->me);
2044 +    out:
2045 +       kfree(set);
2046 +       return res;
2047 +}
2048 +
2049 +/*
2050 + * Destroy a given existing set
2051 + */
2052 +static void
2053 +ip_set_destroy_set(ip_set_id_t index)
2054 +{
2055 +       struct ip_set *set = ip_set_list[index];
2056 +
2057 +       IP_SET_ASSERT(set);
2058 +       DP("set: %s",  set->name);
2059 +       write_lock_bh(&ip_set_lock);
2060 +       FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2061 +       if (set->binding != IP_SET_INVALID_ID)
2062 +               __ip_set_put(set->binding);
2063 +       ip_set_list[index] = NULL;
2064 +       write_unlock_bh(&ip_set_lock);
2065 +
2066 +       /* Must call it without holding any lock */
2067 +       set->type->destroy(set);
2068 +       module_put(set->type->me);
2069 +       kfree(set);
2070 +}
2071 +
2072 +/*
2073 + * Destroy a set - or all sets
2074 + * Sets must not be referenced/used.
2075 + */
2076 +static int
2077 +ip_set_destroy(ip_set_id_t index)
2078 +{
2079 +       ip_set_id_t i;
2080 +
2081 +       /* ref modification always protected by the mutex */
2082 +       if (index != IP_SET_INVALID_ID) {
2083 +               if (atomic_read(&ip_set_list[index]->ref))
2084 +                       return -EBUSY;
2085 +               ip_set_destroy_set(index);
2086 +       } else {
2087 +               for (i = 0; i < ip_set_max; i++) {
2088 +                       if (ip_set_list[i] != NULL 
2089 +                           && (atomic_read(&ip_set_list[i]->ref)))
2090 +                               return -EBUSY;
2091 +               }
2092 +
2093 +               for (i = 0; i < ip_set_max; i++) {
2094 +                       if (ip_set_list[i] != NULL)
2095 +                               ip_set_destroy_set(i);
2096 +               }
2097 +       }
2098 +       return 0;
2099 +}
2100 +
2101 +static void
2102 +ip_set_flush_set(struct ip_set *set)
2103 +{
2104 +       DP("set: %s %u",  set->name, set->id);
2105 +
2106 +       write_lock_bh(&set->lock);
2107 +       set->type->flush(set);
2108 +       write_unlock_bh(&set->lock);
2109 +}
2110 +
2111 +/* 
2112 + * Flush data in a set - or in all sets
2113 + */
2114 +static int
2115 +ip_set_flush(ip_set_id_t index)
2116 +{
2117 +       if (index != IP_SET_INVALID_ID) {
2118 +               IP_SET_ASSERT(ip_set_list[index]);
2119 +               ip_set_flush_set(ip_set_list[index]);
2120 +       } else
2121 +               FOREACH_SET_DO(ip_set_flush_set);
2122 +
2123 +       return 0;
2124 +}
2125 +
2126 +/* Rename a set */
2127 +static int
2128 +ip_set_rename(ip_set_id_t index, const char *name)
2129 +{
2130 +       struct ip_set *set = ip_set_list[index];
2131 +       ip_set_id_t i;
2132 +       int res = 0;
2133 +
2134 +       DP("set: %s to %s",  set->name, name);
2135 +       write_lock_bh(&ip_set_lock);
2136 +       for (i = 0; i < ip_set_max; i++) {
2137 +               if (ip_set_list[i] != NULL
2138 +                   && strncmp(ip_set_list[i]->name, 
2139 +                              name,
2140 +                              IP_SET_MAXNAMELEN - 1) == 0) {
2141 +                       res = -EEXIST;
2142 +                       goto unlock;
2143 +               }
2144 +       }
2145 +       strncpy(set->name, name, IP_SET_MAXNAMELEN);
2146 +    unlock:
2147 +       write_unlock_bh(&ip_set_lock);
2148 +       return res;
2149 +}
2150 +
2151 +/*
2152 + * Swap two sets so that name/index points to the other.
2153 + * References are also swapped.
2154 + */
2155 +static int
2156 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2157 +{
2158 +       struct ip_set *from = ip_set_list[from_index];
2159 +       struct ip_set *to = ip_set_list[to_index];
2160 +       char from_name[IP_SET_MAXNAMELEN];
2161 +       u_int32_t from_ref;
2162 +
2163 +       DP("set: %s to %s",  from->name, to->name);
2164 +       /* Features must not change. Artifical restriction. */
2165 +       if (from->type->features != to->type->features)
2166 +               return -ENOEXEC;
2167 +
2168 +       /* No magic here: ref munging protected by the mutex */ 
2169 +       write_lock_bh(&ip_set_lock);
2170 +       strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2171 +       from_ref = atomic_read(&from->ref);
2172 +
2173 +       strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2174 +       atomic_set(&from->ref, atomic_read(&to->ref));
2175 +       strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2176 +       atomic_set(&to->ref, from_ref);
2177 +       
2178 +       ip_set_list[from_index] = to;
2179 +       ip_set_list[to_index] = from;
2180 +       
2181 +       write_unlock_bh(&ip_set_lock);
2182 +       return 0;
2183 +}
2184 +
2185 +/*
2186 + * List set data
2187 + */
2188 +
2189 +static inline void
2190 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2191 +                             ip_set_id_t id, size_t *size)
2192 +{
2193 +       if (set_hash->id == id)
2194 +               *size += sizeof(struct ip_set_hash_list);
2195 +}
2196 +
2197 +static inline void
2198 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2199 +                             ip_set_id_t id, size_t *size)
2200 +{
2201 +       if (set_hash->id == id)
2202 +               *size += sizeof(struct ip_set_hash_save);
2203 +}
2204 +
2205 +static inline void
2206 +__set_hash_bindings(struct ip_set_hash *set_hash,
2207 +                   ip_set_id_t id, void *data, int *used)
2208 +{
2209 +       if (set_hash->id == id) {
2210 +               struct ip_set_hash_list *hash_list = 
2211 +                       (struct ip_set_hash_list *)(data + *used);
2212 +
2213 +               hash_list->ip = set_hash->ip;
2214 +               hash_list->binding = set_hash->binding;
2215 +               *used += sizeof(struct ip_set_hash_list);
2216 +       }
2217 +}
2218 +
2219 +static int ip_set_list_set(ip_set_id_t index,
2220 +                          void *data,
2221 +                          int *used,
2222 +                          int len)
2223 +{
2224 +       struct ip_set *set = ip_set_list[index];
2225 +       struct ip_set_list *set_list;
2226 +
2227 +       /* Pointer to our header */
2228 +       set_list = (struct ip_set_list *) (data + *used);
2229 +
2230 +       DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2231 +
2232 +       /* Get and ensure header size */
2233 +       if (*used + sizeof(struct ip_set_list) > len)
2234 +               goto not_enough_mem;
2235 +       *used += sizeof(struct ip_set_list);
2236 +
2237 +       read_lock_bh(&set->lock);
2238 +       /* Get and ensure set specific header size */
2239 +       set_list->header_size = set->type->header_size;
2240 +       if (*used + set_list->header_size > len)
2241 +               goto unlock_set;
2242 +
2243 +       /* Fill in the header */
2244 +       set_list->index = index;
2245 +       set_list->binding = set->binding;
2246 +       set_list->ref = atomic_read(&set->ref);
2247 +
2248 +       /* Fill in set spefific header data */
2249 +       set->type->list_header(set, data + *used);
2250 +       *used += set_list->header_size;
2251 +
2252 +       /* Get and ensure set specific members size */
2253 +       set_list->members_size = set->type->list_members_size(set);
2254 +       if (*used + set_list->members_size > len)
2255 +               goto unlock_set;
2256 +
2257 +       /* Fill in set spefific members data */
2258 +       set->type->list_members(set, data + *used);
2259 +       *used += set_list->members_size;
2260 +       read_unlock_bh(&set->lock);
2261 +
2262 +       /* Bindings */
2263 +
2264 +       /* Get and ensure set specific bindings size */
2265 +       set_list->bindings_size = 0;
2266 +       FOREACH_HASH_DO(__set_hash_bindings_size_list,
2267 +                       set->id, &set_list->bindings_size);
2268 +       if (*used + set_list->bindings_size > len)
2269 +               goto not_enough_mem;
2270 +
2271 +       /* Fill in set spefific bindings data */
2272 +       FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2273 +       
2274 +       return 0;
2275 +
2276 +    unlock_set:
2277 +       read_unlock_bh(&set->lock);
2278 +    not_enough_mem:
2279 +       DP("not enough mem, try again");
2280 +       return -EAGAIN;
2281 +}
2282 +
2283 +/*
2284 + * Save sets
2285 + */
2286 +static int ip_set_save_set(ip_set_id_t index,
2287 +                          void *data,
2288 +                          int *used,
2289 +                          int len)
2290 +{
2291 +       struct ip_set *set;
2292 +       struct ip_set_save *set_save;
2293 +
2294 +       /* Pointer to our header */
2295 +       set_save = (struct ip_set_save *) (data + *used);
2296 +
2297 +       /* Get and ensure header size */
2298 +       if (*used + sizeof(struct ip_set_save) > len)
2299 +               goto not_enough_mem;
2300 +       *used += sizeof(struct ip_set_save);
2301 +
2302 +       set = ip_set_list[index];
2303 +       DP("set: %s, used: %u(%u) %p %p", set->name, *used, len, 
2304 +          data, data + *used);
2305 +
2306 +       read_lock_bh(&set->lock);
2307 +       /* Get and ensure set specific header size */
2308 +       set_save->header_size = set->type->header_size;
2309 +       if (*used + set_save->header_size > len)
2310 +               goto unlock_set;
2311 +
2312 +       /* Fill in the header */
2313 +       set_save->index = index;
2314 +       set_save->binding = set->binding;
2315 +
2316 +       /* Fill in set spefific header data */
2317 +       set->type->list_header(set, data + *used);
2318 +       *used += set_save->header_size;
2319 +
2320 +       DP("set header filled: %s, used: %u(%u) %p %p", set->name, *used,
2321 +          set_save->header_size, data, data + *used);
2322 +       /* Get and ensure set specific members size */
2323 +       set_save->members_size = set->type->list_members_size(set);
2324 +       if (*used + set_save->members_size > len)
2325 +               goto unlock_set;
2326 +
2327 +       /* Fill in set spefific members data */
2328 +       set->type->list_members(set, data + *used);
2329 +       *used += set_save->members_size;
2330 +       read_unlock_bh(&set->lock);
2331 +       DP("set members filled: %s, used: %u(%u) %p %p", set->name, *used,
2332 +          set_save->members_size, data, data + *used);
2333 +       return 0;
2334 +
2335 +    unlock_set:
2336 +       read_unlock_bh(&set->lock);
2337 +    not_enough_mem:
2338 +       DP("not enough mem, try again");
2339 +       return -EAGAIN;
2340 +}
2341 +
2342 +static inline void
2343 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2344 +                        ip_set_id_t id,
2345 +                        void *data,
2346 +                        int *used,
2347 +                        int len,
2348 +                        int *res)
2349 +{
2350 +       if (*res == 0
2351 +           && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2352 +               struct ip_set_hash_save *hash_save = 
2353 +                       (struct ip_set_hash_save *)(data + *used);
2354 +               /* Ensure bindings size */
2355 +               if (*used + sizeof(struct ip_set_hash_save) > len) {
2356 +                       *res = -ENOMEM;
2357 +                       return;
2358 +               }
2359 +               hash_save->id = set_hash->id;
2360 +               hash_save->ip = set_hash->ip;
2361 +               hash_save->binding = set_hash->binding;
2362 +               *used += sizeof(struct ip_set_hash_save);
2363 +       }
2364 +}
2365 +
2366 +static int ip_set_save_bindings(ip_set_id_t index,
2367 +                               void *data,
2368 +                               int *used,
2369 +                               int len)
2370 +{
2371 +       int res = 0;
2372 +       struct ip_set_save *set_save;
2373 +
2374 +       DP("used %u, len %u", *used, len);
2375 +       /* Get and ensure header size */
2376 +       if (*used + sizeof(struct ip_set_save) > len)
2377 +               return -ENOMEM;
2378 +
2379 +       /* Marker */
2380 +       set_save = (struct ip_set_save *) (data + *used);
2381 +       set_save->index = IP_SET_INVALID_ID;
2382 +       set_save->header_size = 0;
2383 +       set_save->members_size = 0;
2384 +       *used += sizeof(struct ip_set_save);
2385 +
2386 +       DP("marker added used %u, len %u", *used, len);
2387 +       /* Fill in bindings data */
2388 +       if (index != IP_SET_INVALID_ID)
2389 +               /* Sets are identified by id in hash */
2390 +               index = ip_set_list[index]->id;
2391 +       FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2392 +
2393 +       return res;     
2394 +}
2395 +
2396 +/*
2397 + * Restore sets
2398 + */
2399 +static int ip_set_restore(void *data,
2400 +                         int len)
2401 +{
2402 +       int res = 0;
2403 +       int line = 0, used = 0, members_size;
2404 +       struct ip_set *set;
2405 +       struct ip_set_hash_save *hash_save;
2406 +       struct ip_set_restore *set_restore;
2407 +       ip_set_id_t index;
2408 +
2409 +       /* Loop to restore sets */
2410 +       while (1) {
2411 +               line++;
2412 +               
2413 +               DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2414 +               /* Get and ensure header size */
2415 +               if (used + sizeof(struct ip_set_restore) > len)
2416 +                       return line;
2417 +               set_restore = (struct ip_set_restore *) (data + used);
2418 +               used += sizeof(struct ip_set_restore);
2419 +
2420 +               /* Ensure data size */
2421 +               if (used 
2422 +                   + set_restore->header_size 
2423 +                   + set_restore->members_size > len)
2424 +                       return line;
2425 +
2426 +               /* Check marker */
2427 +               if (set_restore->index == IP_SET_INVALID_ID) {
2428 +                       line--;
2429 +                       goto bindings;
2430 +               }
2431 +               
2432 +               /* Try to create the set */
2433 +               DP("restore %s %s", set_restore->name, set_restore->typename);
2434 +               res = ip_set_create(set_restore->name,
2435 +                                   set_restore->typename,
2436 +                                   set_restore->index,
2437 +                                   data + used,
2438 +                                   set_restore->header_size);
2439 +               
2440 +               if (res != 0)
2441 +                       return line;
2442 +               used += set_restore->header_size;
2443 +
2444 +               index = ip_set_find_byindex(set_restore->index);
2445 +               DP("index %u, restore_index %u", index, set_restore->index);
2446 +               if (index != set_restore->index)
2447 +                       return line;
2448 +               /* Try to restore members data */
2449 +               set = ip_set_list[index];
2450 +               members_size = 0;
2451 +               DP("members_size %u reqsize %u",
2452 +                  set_restore->members_size, set->type->reqsize);
2453 +               while (members_size + set->type->reqsize <=
2454 +                      set_restore->members_size) {
2455 +                       line++;
2456 +                       DP("members: %u, line %u", members_size, line);
2457 +                       res = __ip_set_addip(index,
2458 +                                          data + used + members_size,
2459 +                                          set->type->reqsize);
2460 +                       if (!(res == 0 || res == -EEXIST)) 
2461 +                               return line;
2462 +                       members_size += set->type->reqsize;
2463 +               }
2464 +
2465 +               DP("members_size %u  %u",
2466 +                  set_restore->members_size, members_size);
2467 +               if (members_size != set_restore->members_size)
2468 +                       return line++;
2469 +               used += set_restore->members_size;              
2470 +       }
2471 +       
2472 +   bindings:
2473 +       /* Loop to restore bindings */
2474 +       while (used < len) {
2475 +               line++;
2476 +
2477 +               DP("restore binding, line %u", line);           
2478 +               /* Get and ensure size */
2479 +               if (used + sizeof(struct ip_set_hash_save) > len)
2480 +                       return line;
2481 +               hash_save = (struct ip_set_hash_save *) (data + used);
2482 +               used += sizeof(struct ip_set_hash_save);
2483 +               
2484 +               /* hash_save->id is used to store the index */
2485 +               index = ip_set_find_byindex(hash_save->id);
2486 +               DP("restore binding index %u, id %u, %u -> %u",
2487 +                  index, hash_save->id, hash_save->ip, hash_save->binding);            
2488 +               if (index != hash_save->id)
2489 +                       return line;
2490 +               if (ip_set_find_byindex(hash_save->binding) == IP_SET_INVALID_ID) {
2491 +                       DP("corrupt binding set index %u", hash_save->binding);
2492 +                       return line;
2493 +               }
2494 +               set = ip_set_list[hash_save->id];
2495 +               /* Null valued IP means default binding */
2496 +               if (hash_save->ip)
2497 +                       res = ip_set_hash_add(set->id, 
2498 +                                             hash_save->ip,
2499 +                                             hash_save->binding);
2500 +               else {
2501 +                       IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2502 +                       write_lock_bh(&ip_set_lock);
2503 +                       set->binding = hash_save->binding;
2504 +                       __ip_set_get(set->binding);
2505 +                       write_unlock_bh(&ip_set_lock);
2506 +                       DP("default binding: %u", set->binding);
2507 +               }
2508 +               if (res != 0)
2509 +                       return line;
2510 +       }
2511 +       if (used != len)
2512 +               return line;
2513 +       
2514 +       return 0;       
2515 +}
2516 +
2517 +static int
2518 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2519 +{
2520 +       void *data;
2521 +       int res = 0;            /* Assume OK */
2522 +       unsigned *op;
2523 +       struct ip_set_req_adt *req_adt;
2524 +       ip_set_id_t index = IP_SET_INVALID_ID;
2525 +       int (*adtfn)(ip_set_id_t index,
2526 +                    const void *data, size_t size);
2527 +       struct fn_table {
2528 +               int (*fn)(ip_set_id_t index,
2529 +                         const void *data, size_t size);
2530 +       } adtfn_table[] =
2531 +       { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2532 +         { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2533 +       };
2534 +
2535 +       DP("optval=%d, user=%p, len=%d", optval, user, len);
2536 +       if (!capable(CAP_NET_ADMIN))
2537 +               return -EPERM;
2538 +       if (optval != SO_IP_SET)
2539 +               return -EBADF;
2540 +       if (len <= sizeof(unsigned)) {
2541 +               ip_set_printk("short userdata (want >%zu, got %u)",
2542 +                             sizeof(unsigned), len);
2543 +               return -EINVAL;
2544 +       }
2545 +       data = vmalloc(len);
2546 +       if (!data) {
2547 +               DP("out of mem for %u bytes", len);
2548 +               return -ENOMEM;
2549 +       }
2550 +       if (copy_from_user(data, user, len) != 0) {
2551 +               res = -EFAULT;
2552 +               goto done;
2553 +       }
2554 +       if (down_interruptible(&ip_set_app_mutex)) {
2555 +               res = -EINTR;
2556 +               goto done;
2557 +       }
2558 +
2559 +       op = (unsigned *)data;
2560 +       DP("op=%x", *op);
2561 +       
2562 +       if (*op < IP_SET_OP_VERSION) {
2563 +               /* Check the version at the beginning of operations */
2564 +               struct ip_set_req_version *req_version =
2565 +                       (struct ip_set_req_version *) data;
2566 +               if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2567 +                       res = -EPROTO;
2568 +                       goto done;
2569 +               }
2570 +       }
2571 +
2572 +       switch (*op) {
2573 +       case IP_SET_OP_CREATE:{
2574 +               struct ip_set_req_create *req_create
2575 +                       = (struct ip_set_req_create *) data;
2576 +               
2577 +               if (len < sizeof(struct ip_set_req_create)) {
2578 +                       ip_set_printk("short CREATE data (want >=%zu, got %u)",
2579 +                                     sizeof(struct ip_set_req_create), len);
2580 +                       res = -EINVAL;
2581 +                       goto done;
2582 +               }
2583 +               req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2584 +               req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2585 +               res = ip_set_create(req_create->name,
2586 +                                   req_create->typename,
2587 +                                   IP_SET_INVALID_ID,
2588 +                                   data + sizeof(struct ip_set_req_create),
2589 +                                   len - sizeof(struct ip_set_req_create));
2590 +               goto done;
2591 +       }
2592 +       case IP_SET_OP_DESTROY:{
2593 +               struct ip_set_req_std *req_destroy
2594 +                       = (struct ip_set_req_std *) data;
2595 +               
2596 +               if (len != sizeof(struct ip_set_req_std)) {
2597 +                       ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2598 +                                     sizeof(struct ip_set_req_std), len);
2599 +                       res = -EINVAL;
2600 +                       goto done;
2601 +               }
2602 +               if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2603 +                       /* Destroy all sets */
2604 +                       index = IP_SET_INVALID_ID;
2605 +               } else {
2606 +                       req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2607 +                       index = ip_set_find_byname(req_destroy->name);
2608 +
2609 +                       if (index == IP_SET_INVALID_ID) {
2610 +                               res = -ENOENT;
2611 +                               goto done;
2612 +                       }
2613 +               }
2614 +                       
2615 +               res = ip_set_destroy(index);
2616 +               goto done;
2617 +       }
2618 +       case IP_SET_OP_FLUSH:{
2619 +               struct ip_set_req_std *req_flush =
2620 +                       (struct ip_set_req_std *) data;
2621 +
2622 +               if (len != sizeof(struct ip_set_req_std)) {
2623 +                       ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2624 +                                     sizeof(struct ip_set_req_std), len);
2625 +                       res = -EINVAL;
2626 +                       goto done;
2627 +               }
2628 +               if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2629 +                       /* Flush all sets */
2630 +                       index = IP_SET_INVALID_ID;
2631 +               } else {
2632 +                       req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2633 +                       index = ip_set_find_byname(req_flush->name);
2634 +
2635 +                       if (index == IP_SET_INVALID_ID) {
2636 +                               res = -ENOENT;
2637 +                               goto done;
2638 +                       }
2639 +               }
2640 +               res = ip_set_flush(index);
2641 +               goto done;
2642 +       }
2643 +       case IP_SET_OP_RENAME:{
2644 +               struct ip_set_req_create *req_rename
2645 +                       = (struct ip_set_req_create *) data;
2646 +
2647 +               if (len != sizeof(struct ip_set_req_create)) {
2648 +                       ip_set_printk("invalid RENAME data (want %zu, got %u)",
2649 +                                     sizeof(struct ip_set_req_create), len);
2650 +                       res = -EINVAL;
2651 +                       goto done;
2652 +               }
2653 +
2654 +               req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2655 +               req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2656 +                       
2657 +               index = ip_set_find_byname(req_rename->name);
2658 +               if (index == IP_SET_INVALID_ID) {
2659 +                       res = -ENOENT;
2660 +                       goto done;
2661 +               }
2662 +               res = ip_set_rename(index, req_rename->typename);
2663 +               goto done;
2664 +       }
2665 +       case IP_SET_OP_SWAP:{
2666 +               struct ip_set_req_create *req_swap
2667 +                       = (struct ip_set_req_create *) data;
2668 +               ip_set_id_t to_index;
2669 +
2670 +               if (len != sizeof(struct ip_set_req_create)) {
2671 +                       ip_set_printk("invalid SWAP data (want %zu, got %u)",
2672 +                                     sizeof(struct ip_set_req_create), len);
2673 +                       res = -EINVAL;
2674 +                       goto done;
2675 +               }
2676 +
2677 +               req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2678 +               req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2679 +
2680 +               index = ip_set_find_byname(req_swap->name);
2681 +               if (index == IP_SET_INVALID_ID) {
2682 +                       res = -ENOENT;
2683 +                       goto done;
2684 +               }
2685 +               to_index = ip_set_find_byname(req_swap->typename);
2686 +               if (to_index == IP_SET_INVALID_ID) {
2687 +                       res = -ENOENT;
2688 +                       goto done;
2689 +               }
2690 +               res = ip_set_swap(index, to_index);
2691 +               goto done;
2692 +       }
2693 +       default: 
2694 +               break;  /* Set identified by id */
2695 +       }
2696 +       
2697 +       /* There we may have add/del/test/bind/unbind/test_bind operations */
2698 +       if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2699 +               res = -EBADMSG;
2700 +               goto done;
2701 +       }
2702 +       adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2703 +
2704 +       if (len < sizeof(struct ip_set_req_adt)) {
2705 +               ip_set_printk("short data in adt request (want >=%zu, got %u)",
2706 +                             sizeof(struct ip_set_req_adt), len);
2707 +               res = -EINVAL;
2708 +               goto done;
2709 +       }
2710 +       req_adt = (struct ip_set_req_adt *) data;
2711 +
2712 +       /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2713 +       if (!(*op == IP_SET_OP_UNBIND_SET 
2714 +             && req_adt->index == IP_SET_INVALID_ID)) {
2715 +               index = ip_set_find_byindex(req_adt->index);
2716 +               if (index == IP_SET_INVALID_ID) {
2717 +                       res = -ENOENT;
2718 +                       goto done;
2719 +               }
2720 +       }
2721 +       res = adtfn(index, data, len);
2722 +
2723 +    done:
2724 +       up(&ip_set_app_mutex);
2725 +       vfree(data);
2726 +       if (res > 0)
2727 +               res = 0;
2728 +       DP("final result %d", res);
2729 +       return res;
2730 +}
2731 +
2732 +static int 
2733 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2734 +{
2735 +       int res = 0;
2736 +       unsigned *op;
2737 +       ip_set_id_t index = IP_SET_INVALID_ID;
2738 +       void *data;
2739 +       int copylen = *len;
2740 +
2741 +       DP("optval=%d, user=%p, len=%d", optval, user, *len);
2742 +       if (!capable(CAP_NET_ADMIN))
2743 +               return -EPERM;
2744 +       if (optval != SO_IP_SET)
2745 +               return -EBADF;
2746 +       if (*len < sizeof(unsigned)) {
2747 +               ip_set_printk("short userdata (want >=%zu, got %d)",
2748 +                             sizeof(unsigned), *len);
2749 +               return -EINVAL;
2750 +       }
2751 +       data = vmalloc(*len);
2752 +       if (!data) {
2753 +               DP("out of mem for %d bytes", *len);
2754 +               return -ENOMEM;
2755 +       }
2756 +       if (copy_from_user(data, user, *len) != 0) {
2757 +               res = -EFAULT;
2758 +               goto done;
2759 +       }
2760 +       if (down_interruptible(&ip_set_app_mutex)) {
2761 +               res = -EINTR;
2762 +               goto done;
2763 +       }
2764 +
2765 +       op = (unsigned *) data;
2766 +       DP("op=%x", *op);
2767 +
2768 +       if (*op < IP_SET_OP_VERSION) {
2769 +               /* Check the version at the beginning of operations */
2770 +               struct ip_set_req_version *req_version =
2771 +                       (struct ip_set_req_version *) data;
2772 +               if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2773 +                       res = -EPROTO;
2774 +                       goto done;
2775 +               }
2776 +       }
2777 +
2778 +       switch (*op) {
2779 +       case IP_SET_OP_VERSION: {
2780 +               struct ip_set_req_version *req_version =
2781 +                   (struct ip_set_req_version *) data;
2782 +
2783 +               if (*len != sizeof(struct ip_set_req_version)) {
2784 +                       ip_set_printk("invalid VERSION (want %zu, got %d)",
2785 +                                     sizeof(struct ip_set_req_version),
2786 +                                     *len);
2787 +                       res = -EINVAL;
2788 +                       goto done;
2789 +               }
2790 +
2791 +               req_version->version = IP_SET_PROTOCOL_VERSION;
2792 +               res = copy_to_user(user, req_version,
2793 +                                  sizeof(struct ip_set_req_version));
2794 +               goto done;
2795 +       }
2796 +       case IP_SET_OP_GET_BYNAME: {
2797 +               struct ip_set_req_get_set *req_get
2798 +                       = (struct ip_set_req_get_set *) data;
2799 +
2800 +               if (*len != sizeof(struct ip_set_req_get_set)) {
2801 +                       ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2802 +                                     sizeof(struct ip_set_req_get_set), *len);
2803 +                       res = -EINVAL;
2804 +                       goto done;
2805 +               }
2806 +               req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2807 +               index = ip_set_find_byname(req_get->set.name);
2808 +               req_get->set.index = index;
2809 +               goto copy;
2810 +       }
2811 +       case IP_SET_OP_GET_BYINDEX: {
2812 +               struct ip_set_req_get_set *req_get
2813 +                       = (struct ip_set_req_get_set *) data;
2814 +
2815 +               if (*len != sizeof(struct ip_set_req_get_set)) {
2816 +                       ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2817 +                                     sizeof(struct ip_set_req_get_set), *len);
2818 +                       res = -EINVAL;
2819 +                       goto done;
2820 +               }
2821 +               req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2822 +               index = ip_set_find_byindex(req_get->set.index);
2823 +               strncpy(req_get->set.name,
2824 +                       index == IP_SET_INVALID_ID ? ""
2825 +                       : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2826 +               goto copy;
2827 +       }
2828 +       case IP_SET_OP_ADT_GET: {
2829 +               struct ip_set_req_adt_get *req_get
2830 +                       = (struct ip_set_req_adt_get *) data;
2831 +
2832 +               if (*len != sizeof(struct ip_set_req_adt_get)) {
2833 +                       ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2834 +                                     sizeof(struct ip_set_req_adt_get), *len);
2835 +                       res = -EINVAL;
2836 +                       goto done;
2837 +               }
2838 +               req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2839 +               index = ip_set_find_byname(req_get->set.name);
2840 +               if (index != IP_SET_INVALID_ID) {
2841 +                       req_get->set.index = index;
2842 +                       strncpy(req_get->typename,
2843 +                               ip_set_list[index]->type->typename,
2844 +                               IP_SET_MAXNAMELEN - 1);
2845 +               } else {
2846 +                       res = -ENOENT;
2847 +                       goto done;
2848 +               }
2849 +               goto copy;
2850 +       }
2851 +       case IP_SET_OP_MAX_SETS: {
2852 +               struct ip_set_req_max_sets *req_max_sets
2853 +                       = (struct ip_set_req_max_sets *) data;
2854 +               ip_set_id_t i;
2855 +
2856 +               if (*len != sizeof(struct ip_set_req_max_sets)) {
2857 +                       ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2858 +                                     sizeof(struct ip_set_req_max_sets), *len);
2859 +                       res = -EINVAL;
2860 +                       goto done;
2861 +               }
2862 +
2863 +               if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2864 +                       req_max_sets->set.index = IP_SET_INVALID_ID;
2865 +               } else {
2866 +                       req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2867 +                       req_max_sets->set.index = 
2868 +                               ip_set_find_byname(req_max_sets->set.name);
2869 +                       if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2870 +                               res = -ENOENT;
2871 +                               goto done;
2872 +                       }
2873 +               }
2874 +               req_max_sets->max_sets = ip_set_max;
2875 +               req_max_sets->sets = 0;
2876 +               for (i = 0; i < ip_set_max; i++) {
2877 +                       if (ip_set_list[i] != NULL)
2878 +                               req_max_sets->sets++;
2879 +               }
2880 +               goto copy;
2881 +       }
2882 +       case IP_SET_OP_LIST_SIZE: 
2883 +       case IP_SET_OP_SAVE_SIZE: {
2884 +               struct ip_set_req_setnames *req_setnames
2885 +                       = (struct ip_set_req_setnames *) data;
2886 +               struct ip_set_name_list *name_list;
2887 +               struct ip_set *set;
2888 +               ip_set_id_t i;
2889 +               int used;
2890 +
2891 +               if (*len < sizeof(struct ip_set_req_setnames)) {
2892 +                       ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2893 +                                     sizeof(struct ip_set_req_setnames), *len);
2894 +                       res = -EINVAL;
2895 +                       goto done;
2896 +               }
2897 +
2898 +               req_setnames->size = 0;
2899 +               used = sizeof(struct ip_set_req_setnames);
2900 +               for (i = 0; i < ip_set_max; i++) {
2901 +                       if (ip_set_list[i] == NULL)
2902 +                               continue;
2903 +                       name_list = (struct ip_set_name_list *) 
2904 +                               (data + used);
2905 +                       used += sizeof(struct ip_set_name_list);
2906 +                       if (used > copylen) {
2907 +                               res = -EAGAIN;
2908 +                               goto done;
2909 +                       }
2910 +                       set = ip_set_list[i];
2911 +                       /* Fill in index, name, etc. */
2912 +                       name_list->index = i;
2913 +                       name_list->id = set->id;
2914 +                       strncpy(name_list->name,
2915 +                               set->name,
2916 +                               IP_SET_MAXNAMELEN - 1);
2917 +                       strncpy(name_list->typename,
2918 +                               set->type->typename,
2919 +                               IP_SET_MAXNAMELEN - 1);
2920 +                       DP("filled %s of type %s, index %u\n",
2921 +                          name_list->name, name_list->typename,
2922 +                          name_list->index);
2923 +                       if (!(req_setnames->index == IP_SET_INVALID_ID
2924 +                             || req_setnames->index == i))
2925 +                             continue;
2926 +                       /* Update size */
2927 +                       switch (*op) {
2928 +                       case IP_SET_OP_LIST_SIZE: {
2929 +                               req_setnames->size += sizeof(struct ip_set_list)
2930 +                                       + set->type->header_size
2931 +                                       + set->type->list_members_size(set);
2932 +                               /* Sets are identified by id in the hash */
2933 +                               FOREACH_HASH_DO(__set_hash_bindings_size_list, 
2934 +                                               set->id, &req_setnames->size);
2935 +                               break;
2936 +                       }
2937 +                       case IP_SET_OP_SAVE_SIZE: {
2938 +                               req_setnames->size += sizeof(struct ip_set_save)
2939 +                                       + set->type->header_size
2940 +                                       + set->type->list_members_size(set);
2941 +                               FOREACH_HASH_DO(__set_hash_bindings_size_save,
2942 +                                               set->id, &req_setnames->size);
2943 +                               break;
2944 +                       }
2945 +                       default:
2946 +                               break;
2947 +                       }
2948 +               }
2949 +               if (copylen != used) {
2950 +                       res = -EAGAIN;
2951 +                       goto done;
2952 +               }
2953 +               goto copy;
2954 +       }
2955 +       case IP_SET_OP_LIST: {
2956 +               struct ip_set_req_list *req_list
2957 +                       = (struct ip_set_req_list *) data;
2958 +               ip_set_id_t i;
2959 +               int used;
2960 +
2961 +               if (*len < sizeof(struct ip_set_req_list)) {
2962 +                       ip_set_printk("short LIST (want >=%zu, got %d)",
2963 +                                     sizeof(struct ip_set_req_list), *len);
2964 +                       res = -EINVAL;
2965 +                       goto done;
2966 +               }
2967 +               index = req_list->index;
2968 +               if (index != IP_SET_INVALID_ID
2969 +                   && ip_set_find_byindex(index) != index) {
2970 +                       res = -ENOENT;
2971 +                       goto done;
2972 +               }
2973 +               used = 0;
2974 +               if (index == IP_SET_INVALID_ID) {
2975 +                       /* List all sets */
2976 +                       for (i = 0; i < ip_set_max && res == 0; i++) {
2977 +                               if (ip_set_list[i] != NULL)
2978 +                                       res = ip_set_list_set(i, data, &used, *len);
2979 +                       }
2980 +               } else {
2981 +                       /* List an individual set */
2982 +                       res = ip_set_list_set(index, data, &used, *len);
2983 +               }
2984 +               if (res != 0)
2985 +                       goto done;
2986 +               else if (copylen != used) {
2987 +                       res = -EAGAIN;
2988 +                       goto done;
2989 +               }
2990 +               goto copy;
2991 +       }
2992 +       case IP_SET_OP_SAVE: {
2993 +               struct ip_set_req_list *req_save
2994 +                       = (struct ip_set_req_list *) data;
2995 +               ip_set_id_t i;
2996 +               int used;
2997 +
2998 +               if (*len < sizeof(struct ip_set_req_list)) {
2999 +                       ip_set_printk("short SAVE (want >=%zu, got %d)",
3000 +                                     sizeof(struct ip_set_req_list), *len);
3001 +                       res = -EINVAL;
3002 +                       goto done;
3003 +               }
3004 +               index = req_save->index;
3005 +               if (index != IP_SET_INVALID_ID
3006 +                   && ip_set_find_byindex(index) != index) {
3007 +                       res = -ENOENT;
3008 +                       goto done;
3009 +               }
3010 +               used = 0;
3011 +               if (index == IP_SET_INVALID_ID) {
3012 +                       /* Save all sets */
3013 +                       for (i = 0; i < ip_set_max && res == 0; i++) {
3014 +                               if (ip_set_list[i] != NULL)
3015 +                                       res = ip_set_save_set(i, data, &used, *len);
3016 +                       }
3017 +               } else {
3018 +                       /* Save an individual set */
3019 +                       res = ip_set_save_set(index, data, &used, *len);
3020 +               }
3021 +               if (res == 0)
3022 +                       res = ip_set_save_bindings(index, data, &used, *len);
3023 +                       
3024 +               if (res != 0)
3025 +                       goto done;
3026 +               else if (copylen != used) {
3027 +                       res = -EAGAIN;
3028 +                       goto done;
3029 +               }
3030 +               goto copy;
3031 +       }
3032 +       case IP_SET_OP_RESTORE: {
3033 +               struct ip_set_req_setnames *req_restore
3034 +                       = (struct ip_set_req_setnames *) data;
3035 +               int line;
3036 +
3037 +               if (*len < sizeof(struct ip_set_req_setnames)
3038 +                   || *len != req_restore->size) {
3039 +                       ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3040 +                                     req_restore->size, *len);
3041 +                       res = -EINVAL;
3042 +                       goto done;
3043 +               }
3044 +               line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3045 +                                     req_restore->size - sizeof(struct ip_set_req_setnames));
3046 +               DP("ip_set_restore: %u", line);
3047 +               if (line != 0) {
3048 +                       res = -EAGAIN;
3049 +                       req_restore->size = line;
3050 +                       copylen = sizeof(struct ip_set_req_setnames);
3051 +                       goto copy;
3052 +               }
3053 +               goto done;
3054 +       }
3055 +       default:
3056 +               res = -EBADMSG;
3057 +               goto done;
3058 +       }       /* end of switch(op) */
3059 +
3060 +    copy:
3061 +       DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3062 +                                && ip_set_list[index]
3063 +                    ? ip_set_list[index]->name
3064 +                    : ":all:", copylen);
3065 +       res = copy_to_user(user, data, copylen);
3066 +       
3067 +    done:
3068 +       up(&ip_set_app_mutex);
3069 +       vfree(data);
3070 +       if (res > 0)
3071 +               res = 0;
3072 +       DP("final result %d", res);
3073 +       return res;
3074 +}
3075 +
3076 +static struct nf_sockopt_ops so_set = {
3077 +       .pf             = PF_INET,
3078 +       .set_optmin     = SO_IP_SET,
3079 +       .set_optmax     = SO_IP_SET + 1,
3080 +       .set            = &ip_set_sockfn_set,
3081 +       .get_optmin     = SO_IP_SET,
3082 +       .get_optmax     = SO_IP_SET + 1,
3083 +       .get            = &ip_set_sockfn_get,
3084 +       .use            = 0
3085 +};
3086 +
3087 +static int max_sets, hash_size;
3088 +module_param(max_sets, int, 0600);
3089 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3090 +module_param(hash_size, int, 0600);
3091 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3092 +MODULE_LICENSE("GPL");
3093 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3094 +MODULE_DESCRIPTION("module implementing core IP set support");
3095 +
3096 +static int __init ip_set_init(void)
3097 +{
3098 +       int res;
3099 +       ip_set_id_t i;
3100 +
3101 +       get_random_bytes(&ip_set_hash_random, 4);
3102 +       if (max_sets)
3103 +               ip_set_max = max_sets;
3104 +       ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3105 +       if (!ip_set_list) {
3106 +               printk(KERN_ERR "Unable to create ip_set_list\n");
3107 +               return -ENOMEM;
3108 +       }
3109 +       memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3110 +       if (hash_size)
3111 +               ip_set_bindings_hash_size = hash_size;
3112 +       ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3113 +       if (!ip_set_hash) {
3114 +               printk(KERN_ERR "Unable to create ip_set_hash\n");
3115 +               vfree(ip_set_list);
3116 +               return -ENOMEM;
3117 +       }
3118 +       for (i = 0; i < ip_set_bindings_hash_size; i++)
3119 +               INIT_LIST_HEAD(&ip_set_hash[i]);
3120 +
3121 +       INIT_LIST_HEAD(&set_type_list);
3122 +
3123 +       res = nf_register_sockopt(&so_set);
3124 +       if (res != 0) {
3125 +               ip_set_printk("SO_SET registry failed: %d", res);
3126 +               vfree(ip_set_list);
3127 +               vfree(ip_set_hash);
3128 +               return res;
3129 +       }
3130 +       return 0;
3131 +}
3132 +
3133 +static void __exit ip_set_fini(void)
3134 +{
3135 +       /* There can't be any existing set or binding */
3136 +       nf_unregister_sockopt(&so_set);
3137 +       vfree(ip_set_list);
3138 +       vfree(ip_set_hash);
3139 +       DP("these are the famous last words");
3140 +}
3141 +
3142 +EXPORT_SYMBOL(ip_set_register_set_type);
3143 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3144 +
3145 +EXPORT_SYMBOL(ip_set_get_byname);
3146 +EXPORT_SYMBOL(ip_set_get_byindex);
3147 +EXPORT_SYMBOL(ip_set_put);
3148 +
3149 +EXPORT_SYMBOL(ip_set_addip_kernel);
3150 +EXPORT_SYMBOL(ip_set_delip_kernel);
3151 +EXPORT_SYMBOL(ip_set_testip_kernel);
3152 +
3153 +module_init(ip_set_init);
3154 +module_exit(ip_set_fini);
3155 diff -uNrp linux-2.6./net/ipv4/netfilter/ip_set_iphash.c linux-2.6/net/ipv4/netfilter/ip_set_iphash.c
3156 --- linux-2.6./net/ipv4/netfilter/ip_set_iphash.c       1970-01-01 01:00:00.000000000 +0100
3157 +++ linux-2.6/net/ipv4/netfilter/ip_set_iphash.c        2007-08-28 13:29:12.000000000 +0200
3158 @@ -0,0 +1,429 @@
3159 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3160 + *
3161 + * This program is free software; you can redistribute it and/or modify
3162 + * it under the terms of the GNU General Public License version 2 as
3163 + * published by the Free Software Foundation.  
3164 + */
3165 +
3166 +/* Kernel module implementing an ip hash set */
3167 +
3168 +#include <linux/module.h>
3169 +#include <linux/ip.h>
3170 +#include <linux/skbuff.h>
3171 +#include <linux/version.h>
3172 +#include <linux/jhash.h>
3173 +#include <linux/netfilter_ipv4/ip_tables.h>
3174 +#include <linux/netfilter_ipv4/ip_set.h>
3175 +#include <linux/errno.h>
3176 +#include <asm/uaccess.h>
3177 +#include <asm/bitops.h>
3178 +#include <linux/spinlock.h>
3179 +#include <linux/vmalloc.h>
3180 +#include <linux/random.h>
3181 +
3182 +#include <net/ip.h>
3183 +
3184 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3185 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3186 +
3187 +static int limit = MAX_RANGE;
3188 +
3189 +static inline __u32
3190 +jhash_ip(const struct ip_set_iphash *map, uint16_t i, ip_set_ip_t ip)
3191 +{
3192 +       return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
3193 +}
3194 +
3195 +static inline __u32
3196 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3197 +{
3198 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3199 +       __u32 id;
3200 +       u_int16_t i;
3201 +       ip_set_ip_t *elem;
3202 +
3203 +       *hash_ip = ip & map->netmask;
3204 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3205 +          set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3206 +       
3207 +       for (i = 0; i < map->probes; i++) {
3208 +               id = jhash_ip(map, i, *hash_ip) % map->hashsize;
3209 +               DP("hash key: %u", id);
3210 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3211 +               if (*elem == *hash_ip)
3212 +                       return id;
3213 +               /* No shortcut at testing - there can be deleted
3214 +                * entries. */
3215 +       }
3216 +       return UINT_MAX;
3217 +}
3218 +
3219 +static inline int
3220 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3221 +{
3222 +       return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
3223 +}
3224 +
3225 +static int
3226 +testip(struct ip_set *set, const void *data, size_t size,
3227 +       ip_set_ip_t *hash_ip)
3228 +{
3229 +       struct ip_set_req_iphash *req = 
3230 +           (struct ip_set_req_iphash *) data;
3231 +
3232 +       if (size != sizeof(struct ip_set_req_iphash)) {
3233 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3234 +                             sizeof(struct ip_set_req_iphash),
3235 +                             size);
3236 +               return -EINVAL;
3237 +       }
3238 +       return __testip(set, req->ip, hash_ip);
3239 +}
3240 +
3241 +static int
3242 +testip_kernel(struct ip_set *set, 
3243 +             const struct sk_buff *skb,
3244 +             ip_set_ip_t *hash_ip,
3245 +             const u_int32_t *flags,
3246 +             unsigned char index)
3247 +{
3248 +       return __testip(set,
3249 +                       ntohl(flags[index] & IPSET_SRC 
3250 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3251 +                               ? ip_hdr(skb)->saddr 
3252 +                               : ip_hdr(skb)->daddr),
3253 +#else
3254 +                               ? skb->nh.iph->saddr 
3255 +                               : skb->nh.iph->daddr),
3256 +#endif
3257 +                       hash_ip);
3258 +}
3259 +
3260 +static inline int
3261 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3262 +{
3263 +       __u32 probe;
3264 +       u_int16_t i;
3265 +       ip_set_ip_t *elem;
3266 +       
3267 +       if (!ip || map->elements >= limit)
3268 +               return -ERANGE;
3269 +
3270 +       *hash_ip = ip & map->netmask;
3271 +       
3272 +       for (i = 0; i < map->probes; i++) {
3273 +               probe = jhash_ip(map, i, *hash_ip) % map->hashsize;
3274 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
3275 +               if (*elem == *hash_ip)
3276 +                       return -EEXIST;
3277 +               if (!*elem) {
3278 +                       *elem = *hash_ip;
3279 +                       map->elements++;
3280 +                       return 0;
3281 +               }
3282 +       }
3283 +       /* Trigger rehashing */
3284 +       return -EAGAIN;
3285 +}
3286 +
3287 +static int
3288 +addip(struct ip_set *set, const void *data, size_t size,
3289 +        ip_set_ip_t *hash_ip)
3290 +{
3291 +       struct ip_set_req_iphash *req = 
3292 +           (struct ip_set_req_iphash *) data;
3293 +
3294 +       if (size != sizeof(struct ip_set_req_iphash)) {
3295 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3296 +                             sizeof(struct ip_set_req_iphash),
3297 +                             size);
3298 +               return -EINVAL;
3299 +       }
3300 +       return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3301 +}
3302 +
3303 +static int
3304 +addip_kernel(struct ip_set *set, 
3305 +            const struct sk_buff *skb,
3306 +            ip_set_ip_t *hash_ip,
3307 +            const u_int32_t *flags,
3308 +            unsigned char index)
3309 +{
3310 +       return __addip((struct ip_set_iphash *) set->data,
3311 +                      ntohl(flags[index] & IPSET_SRC 
3312 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3313 +                               ? ip_hdr(skb)->saddr 
3314 +                               : ip_hdr(skb)->daddr),
3315 +#else
3316 +                               ? skb->nh.iph->saddr 
3317 +                               : skb->nh.iph->daddr),
3318 +#endif
3319 +                      hash_ip);
3320 +}
3321 +
3322 +static int retry(struct ip_set *set)
3323 +{
3324 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3325 +       ip_set_ip_t hash_ip, *elem;
3326 +       void *members;
3327 +       u_int32_t i, hashsize = map->hashsize;
3328 +       int res;
3329 +       struct ip_set_iphash *tmp;
3330 +       
3331 +       if (map->resize == 0)
3332 +               return -ERANGE;
3333 +
3334 +    again:
3335 +       res = 0;
3336 +       
3337 +       /* Calculate new hash size */
3338 +       hashsize += (hashsize * map->resize)/100;
3339 +       if (hashsize == map->hashsize)
3340 +               hashsize++;
3341 +       
3342 +       ip_set_printk("rehashing of set %s triggered: "
3343 +                     "hashsize grows from %u to %u",
3344 +                     set->name, map->hashsize, hashsize);
3345 +
3346 +       tmp = kmalloc(sizeof(struct ip_set_iphash) 
3347 +                     + map->probes * sizeof(uint32_t), GFP_ATOMIC);
3348 +       if (!tmp) {
3349 +               DP("out of memory for %d bytes",
3350 +                  sizeof(struct ip_set_iphash)
3351 +                  + map->probes * sizeof(uint32_t));
3352 +               return -ENOMEM;
3353 +       }
3354 +       tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
3355 +       if (!tmp->members) {
3356 +               DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
3357 +               kfree(tmp);
3358 +               return -ENOMEM;
3359 +       }
3360 +       tmp->hashsize = hashsize;
3361 +       tmp->elements = 0;
3362 +       tmp->probes = map->probes;
3363 +       tmp->resize = map->resize;
3364 +       tmp->netmask = map->netmask;
3365 +       memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
3366 +       
3367 +       write_lock_bh(&set->lock);
3368 +       map = (struct ip_set_iphash *) set->data; /* Play safe */
3369 +       for (i = 0; i < map->hashsize && res == 0; i++) {
3370 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);     
3371 +               if (*elem)
3372 +                       res = __addip(tmp, *elem, &hash_ip);
3373 +       }
3374 +       if (res) {
3375 +               /* Failure, try again */
3376 +               write_unlock_bh(&set->lock);
3377 +               harray_free(tmp->members);
3378 +               kfree(tmp);
3379 +               goto again;
3380 +       }
3381 +       
3382 +       /* Success at resizing! */
3383 +       members = map->members;
3384 +
3385 +       map->hashsize = tmp->hashsize;
3386 +       map->members = tmp->members;
3387 +       write_unlock_bh(&set->lock);
3388 +
3389 +       harray_free(members);
3390 +       kfree(tmp);
3391 +
3392 +       return 0;
3393 +}
3394 +
3395 +static inline int
3396 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3397 +{
3398 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3399 +       ip_set_ip_t id, *elem;
3400 +
3401 +       if (!ip)
3402 +               return -ERANGE;
3403 +
3404 +       id = hash_id(set, ip, hash_ip);
3405 +       if (id == UINT_MAX)
3406 +               return -EEXIST;
3407 +               
3408 +       elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3409 +       *elem = 0;
3410 +       map->elements--;
3411 +
3412 +       return 0;
3413 +}
3414 +
3415 +static int
3416 +delip(struct ip_set *set, const void *data, size_t size,
3417 +        ip_set_ip_t *hash_ip)
3418 +{
3419 +       struct ip_set_req_iphash *req =
3420 +           (struct ip_set_req_iphash *) data;
3421 +
3422 +       if (size != sizeof(struct ip_set_req_iphash)) {
3423 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3424 +                             sizeof(struct ip_set_req_iphash),
3425 +                             size);
3426 +               return -EINVAL;
3427 +       }
3428 +       return __delip(set, req->ip, hash_ip);
3429 +}
3430 +
3431 +static int
3432 +delip_kernel(struct ip_set *set, 
3433 +            const struct sk_buff *skb,
3434 +            ip_set_ip_t *hash_ip,
3435 +            const u_int32_t *flags,
3436 +            unsigned char index)
3437 +{
3438 +       return __delip(set,
3439 +                      ntohl(flags[index] & IPSET_SRC 
3440 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3441 +                               ? ip_hdr(skb)->saddr 
3442 +                               : ip_hdr(skb)->daddr),
3443 +#else
3444 +                               ? skb->nh.iph->saddr 
3445 +                               : skb->nh.iph->daddr),
3446 +#endif
3447 +                      hash_ip);
3448 +}
3449 +
3450 +static int create(struct ip_set *set, const void *data, size_t size)
3451 +{
3452 +       struct ip_set_req_iphash_create *req =
3453 +           (struct ip_set_req_iphash_create *) data;
3454 +       struct ip_set_iphash *map;
3455 +       uint16_t i;
3456 +
3457 +       if (size != sizeof(struct ip_set_req_iphash_create)) {
3458 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3459 +                              sizeof(struct ip_set_req_iphash_create),
3460 +                              size);
3461 +               return -EINVAL;
3462 +       }
3463 +
3464 +       if (req->hashsize < 1) {
3465 +               ip_set_printk("hashsize too small");
3466 +               return -ENOEXEC;
3467 +       }
3468 +
3469 +       if (req->probes < 1) {
3470 +               ip_set_printk("probes too small");
3471 +               return -ENOEXEC;
3472 +       }
3473 +
3474 +       map = kmalloc(sizeof(struct ip_set_iphash) 
3475 +                     + req->probes * sizeof(uint32_t), GFP_KERNEL);
3476 +       if (!map) {
3477 +               DP("out of memory for %d bytes",
3478 +                  sizeof(struct ip_set_iphash)
3479 +                  + req->probes * sizeof(uint32_t));
3480 +               return -ENOMEM;
3481 +       }
3482 +       for (i = 0; i < req->probes; i++)
3483 +               get_random_bytes(((uint32_t *) map->initval)+i, 4);
3484 +       map->elements = 0;
3485 +       map->hashsize = req->hashsize;
3486 +       map->probes = req->probes;
3487 +       map->resize = req->resize;
3488 +       map->netmask = req->netmask;
3489 +       map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
3490 +       if (!map->members) {
3491 +               DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
3492 +               kfree(map);
3493 +               return -ENOMEM;
3494 +       }
3495 +
3496 +       set->data = map;
3497 +       return 0;
3498 +}
3499 +
3500 +static void destroy(struct ip_set *set)
3501 +{
3502 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3503 +
3504 +       harray_free(map->members);
3505 +       kfree(map);
3506 +
3507 +       set->data = NULL;
3508 +}
3509 +
3510 +static void flush(struct ip_set *set)
3511 +{
3512 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3513 +       harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
3514 +       map->elements = 0;
3515 +}
3516 +
3517 +static void list_header(const struct ip_set *set, void *data)
3518 +{
3519 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3520 +       struct ip_set_req_iphash_create *header =
3521 +           (struct ip_set_req_iphash_create *) data;
3522 +
3523 +       header->hashsize = map->hashsize;
3524 +       header->probes = map->probes;
3525 +       header->resize = map->resize;
3526 +       header->netmask = map->netmask;
3527 +}
3528 +
3529 +static int list_members_size(const struct ip_set *set)
3530 +{
3531 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3532 +
3533 +       return (map->hashsize * sizeof(ip_set_ip_t));
3534 +}
3535 +
3536 +static void list_members(const struct ip_set *set, void *data)
3537 +{
3538 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3539 +       ip_set_ip_t i, *elem;
3540 +
3541 +       for (i = 0; i < map->hashsize; i++) {
3542 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);     
3543 +               ((ip_set_ip_t *)data)[i] = *elem;
3544 +       }
3545 +}
3546 +
3547 +static struct ip_set_type ip_set_iphash = {
3548 +       .typename               = SETTYPE_NAME,
3549 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
3550 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
3551 +       .create                 = &create,
3552 +       .destroy                = &destroy,
3553 +       .flush                  = &flush,
3554 +       .reqsize                = sizeof(struct ip_set_req_iphash),
3555 +       .addip                  = &addip,
3556 +       .addip_kernel           = &addip_kernel,
3557 +       .retry                  = &retry,
3558 +       .delip                  = &delip,
3559 +       .delip_kernel           = &delip_kernel,
3560 +       .testip                 = &testip,
3561 +       .testip_kernel          = &testip_kernel,
3562 +       .header_size            = sizeof(struct ip_set_req_iphash_create),
3563 +       .list_header            = &list_header,
3564 +       .list_members_size      = &list_members_size,
3565 +       .list_members           = &list_members,
3566 +       .me                     = THIS_MODULE,
3567 +};
3568 +
3569 +MODULE_LICENSE("GPL");
3570 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3571 +MODULE_DESCRIPTION("iphash type of IP sets");
3572 +module_param(limit, int, 0600);
3573 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
3574 +
3575 +static int __init ip_set_iphash_init(void)
3576 +{
3577 +       return ip_set_register_set_type(&ip_set_iphash);
3578 +}
3579 +
3580 +static void __exit ip_set_iphash_fini(void)
3581 +{
3582 +       /* FIXME: possible race with ip_set_create() */
3583 +       ip_set_unregister_set_type(&ip_set_iphash);
3584 +}
3585 +
3586 +module_init(ip_set_iphash_init);
3587 +module_exit(ip_set_iphash_fini);
3588 diff -uNrp linux-2.6./net/ipv4/netfilter/ip_set_ipmap.c linux-2.6/net/ipv4/netfilter/ip_set_ipmap.c
3589 --- linux-2.6./net/ipv4/netfilter/ip_set_ipmap.c        1970-01-01 01:00:00.000000000 +0100
3590 +++ linux-2.6/net/ipv4/netfilter/ip_set_ipmap.c 2007-08-28 13:29:12.000000000 +0200
3591 @@ -0,0 +1,336 @@
3592 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3593 + *                         Patrick Schaaf <bof@bof.de>
3594 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3595 + *
3596 + * This program is free software; you can redistribute it and/or modify
3597 + * it under the terms of the GNU General Public License version 2 as
3598 + * published by the Free Software Foundation.  
3599 + */
3600 +
3601 +/* Kernel module implementing an IP set type: the single bitmap type */
3602 +
3603 +#include <linux/module.h>
3604 +#include <linux/ip.h>
3605 +#include <linux/skbuff.h>
3606 +#include <linux/version.h>
3607 +#include <linux/netfilter_ipv4/ip_tables.h>
3608 +#include <linux/netfilter_ipv4/ip_set.h>
3609 +#include <linux/errno.h>
3610 +#include <asm/uaccess.h>
3611 +#include <asm/bitops.h>
3612 +#include <linux/spinlock.h>
3613 +
3614 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3615 +
3616 +static inline ip_set_ip_t
3617 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3618 +{
3619 +       return (ip - map->first_ip)/map->hosts;
3620 +}
3621 +
3622 +static inline int
3623 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3624 +{
3625 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3626 +       
3627 +       if (ip < map->first_ip || ip > map->last_ip)
3628 +               return -ERANGE;
3629 +
3630 +       *hash_ip = ip & map->netmask;
3631 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3632 +          set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3633 +       return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3634 +}
3635 +
3636 +static int
3637 +testip(struct ip_set *set, const void *data, size_t size,
3638 +       ip_set_ip_t *hash_ip)
3639 +{
3640 +       struct ip_set_req_ipmap *req = 
3641 +           (struct ip_set_req_ipmap *) data;
3642 +
3643 +       if (size != sizeof(struct ip_set_req_ipmap)) {
3644 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3645 +                             sizeof(struct ip_set_req_ipmap),
3646 +                             size);
3647 +               return -EINVAL;
3648 +       }
3649 +       return __testip(set, req->ip, hash_ip);
3650 +}
3651 +
3652 +static int
3653 +testip_kernel(struct ip_set *set, 
3654 +             const struct sk_buff *skb,
3655 +             ip_set_ip_t *hash_ip,
3656 +             const u_int32_t *flags,
3657 +             unsigned char index)
3658 +{
3659 +       int res =  __testip(set,
3660 +                       ntohl(flags[index] & IPSET_SRC
3661 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3662 +                               ? ip_hdr(skb)->saddr 
3663 +                               : ip_hdr(skb)->daddr),
3664 +#else
3665 +                               ? skb->nh.iph->saddr 
3666 +                               : skb->nh.iph->daddr),
3667 +#endif
3668 +                       hash_ip);
3669 +       return (res < 0 ? 0 : res);
3670 +}
3671 +
3672 +static inline int
3673 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3674 +{
3675 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3676 +
3677 +       if (ip < map->first_ip || ip > map->last_ip)
3678 +               return -ERANGE;
3679 +
3680 +       *hash_ip = ip & map->netmask;
3681 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3682 +       if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3683 +               return -EEXIST;
3684 +
3685 +       return 0;
3686 +}
3687 +
3688 +static int
3689 +addip(struct ip_set *set, const void *data, size_t size,
3690 +      ip_set_ip_t *hash_ip)
3691 +{
3692 +       struct ip_set_req_ipmap *req = 
3693 +           (struct ip_set_req_ipmap *) data;
3694 +
3695 +       if (size != sizeof(struct ip_set_req_ipmap)) {
3696 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3697 +                             sizeof(struct ip_set_req_ipmap),
3698 +                             size);
3699 +               return -EINVAL;
3700 +       }
3701 +       DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3702 +       return __addip(set, req->ip, hash_ip);
3703 +}
3704 +
3705 +static int
3706 +addip_kernel(struct ip_set *set, 
3707 +            const struct sk_buff *skb,
3708 +            ip_set_ip_t *hash_ip,
3709 +            const u_int32_t *flags,
3710 +            unsigned char index)
3711 +{
3712 +       return __addip(set,
3713 +                      ntohl(flags[index] & IPSET_SRC 
3714 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3715 +                               ? ip_hdr(skb)->saddr 
3716 +                               : ip_hdr(skb)->daddr),
3717 +#else
3718 +                               ? skb->nh.iph->saddr 
3719 +                               : skb->nh.iph->daddr),
3720 +#endif
3721 +                      hash_ip);
3722 +}
3723 +
3724 +static inline int 
3725 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3726 +{
3727 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3728 +
3729 +       if (ip < map->first_ip || ip > map->last_ip)
3730 +               return -ERANGE;
3731 +
3732 +       *hash_ip = ip & map->netmask;
3733 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3734 +       if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3735 +               return -EEXIST;
3736 +       
3737 +       return 0;
3738 +}
3739 +
3740 +static int
3741 +delip(struct ip_set *set, const void *data, size_t size,
3742 +      ip_set_ip_t *hash_ip)
3743 +{
3744 +       struct ip_set_req_ipmap *req =
3745 +           (struct ip_set_req_ipmap *) data;
3746 +
3747 +       if (size != sizeof(struct ip_set_req_ipmap)) {
3748 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3749 +                             sizeof(struct ip_set_req_ipmap),
3750 +                             size);
3751 +               return -EINVAL;
3752 +       }
3753 +       return __delip(set, req->ip, hash_ip);
3754 +}
3755 +
3756 +static int
3757 +delip_kernel(struct ip_set *set,
3758 +            const struct sk_buff *skb,
3759 +            ip_set_ip_t *hash_ip,
3760 +            const u_int32_t *flags,
3761 +            unsigned char index)
3762 +{
3763 +       return __delip(set,
3764 +                      ntohl(flags[index] & IPSET_SRC 
3765 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3766 +                               ? ip_hdr(skb)->saddr 
3767 +                               : ip_hdr(skb)->daddr),
3768 +#else
3769 +                               ? skb->nh.iph->saddr 
3770 +                               : skb->nh.iph->daddr),
3771 +#endif
3772 +                      hash_ip);
3773 +}
3774 +
3775 +static int create(struct ip_set *set, const void *data, size_t size)
3776 +{
3777 +       int newbytes;
3778 +       struct ip_set_req_ipmap_create *req =
3779 +           (struct ip_set_req_ipmap_create *) data;
3780 +       struct ip_set_ipmap *map;
3781 +
3782 +       if (size != sizeof(struct ip_set_req_ipmap_create)) {
3783 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3784 +                             sizeof(struct ip_set_req_ipmap_create),
3785 +                             size);
3786 +               return -EINVAL;
3787 +       }
3788 +
3789 +       DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3790 +          HIPQUAD(req->from), HIPQUAD(req->to));
3791 +
3792 +       if (req->from > req->to) {
3793 +               DP("bad ip range");
3794 +               return -ENOEXEC;
3795 +       }
3796 +
3797 +       map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3798 +       if (!map) {
3799 +               DP("out of memory for %d bytes",
3800 +                  sizeof(struct ip_set_ipmap));
3801 +               return -ENOMEM;
3802 +       }
3803 +       map->first_ip = req->from;
3804 +       map->last_ip = req->to;
3805 +       map->netmask = req->netmask;
3806 +
3807 +       if (req->netmask == 0xFFFFFFFF) {
3808 +               map->hosts = 1;
3809 +               map->sizeid = map->last_ip - map->first_ip + 1;
3810 +       } else {
3811 +               unsigned int mask_bits, netmask_bits;
3812 +               ip_set_ip_t mask;
3813 +               
3814 +               map->first_ip &= map->netmask;  /* Should we better bark? */
3815 +               
3816 +               mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3817 +               netmask_bits = mask_to_bits(map->netmask);
3818 +               
3819 +               if ((!mask && (map->first_ip || map->last_ip != 0xFFFFFFFF))
3820 +                   || netmask_bits <= mask_bits)
3821 +                       return -ENOEXEC;
3822 +
3823 +               DP("mask_bits %u, netmask_bits %u",
3824 +                  mask_bits, netmask_bits);
3825 +               map->hosts = 2 << (32 - netmask_bits - 1);
3826 +               map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3827 +       }
3828 +       if (map->sizeid > MAX_RANGE + 1) {
3829 +               ip_set_printk("range too big (max %d addresses)",
3830 +                              MAX_RANGE+1);
3831 +               kfree(map);
3832 +               return -ENOEXEC;
3833 +       }
3834 +       DP("hosts %u, sizeid %u", map->hosts, map->sizeid);
3835 +       newbytes = bitmap_bytes(0, map->sizeid - 1);
3836 +       map->members = kmalloc(newbytes, GFP_KERNEL);
3837 +       if (!map->members) {
3838 +               DP("out of memory for %d bytes", newbytes);
3839 +               kfree(map);
3840 +               return -ENOMEM;
3841 +       }
3842 +       memset(map->members, 0, newbytes);
3843 +       
3844 +       set->data = map;
3845 +       return 0;
3846 +}
3847 +
3848 +static void destroy(struct ip_set *set)
3849 +{
3850 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3851 +       
3852 +       kfree(map->members);
3853 +       kfree(map);
3854 +       
3855 +       set->data = NULL;
3856 +}
3857 +
3858 +static void flush(struct ip_set *set)
3859 +{
3860 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3861 +       memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3862 +}
3863 +
3864 +static void list_header(const struct ip_set *set, void *data)
3865 +{
3866 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3867 +       struct ip_set_req_ipmap_create *header =
3868 +           (struct ip_set_req_ipmap_create *) data;
3869 +
3870 +       header->from = map->first_ip;
3871 +       header->to = map->last_ip;
3872 +       header->netmask = map->netmask;
3873 +}
3874 +
3875 +static int list_members_size(const struct ip_set *set)
3876 +{
3877 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3878 +
3879 +       return bitmap_bytes(0, map->sizeid - 1);
3880 +}
3881 +
3882 +static void list_members(const struct ip_set *set, void *data)
3883 +{
3884 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3885 +       int bytes = bitmap_bytes(0, map->sizeid - 1);
3886 +
3887 +       memcpy(data, map->members, bytes);
3888 +}
3889 +
3890 +static struct ip_set_type ip_set_ipmap = {
3891 +       .typename               = SETTYPE_NAME,
3892 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
3893 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
3894 +       .create                 = &create,
3895 +       .destroy                = &destroy,
3896 +       .flush                  = &flush,
3897 +       .reqsize                = sizeof(struct ip_set_req_ipmap),
3898 +       .addip                  = &addip,
3899 +       .addip_kernel           = &addip_kernel,
3900 +       .delip                  = &delip,
3901 +       .delip_kernel           = &delip_kernel,
3902 +       .testip                 = &testip,
3903 +       .testip_kernel          = &testip_kernel,
3904 +       .header_size            = sizeof(struct ip_set_req_ipmap_create),
3905 +       .list_header            = &list_header,
3906 +       .list_members_size      = &list_members_size,
3907 +       .list_members           = &list_members,
3908 +       .me                     = THIS_MODULE,
3909 +};
3910 +
3911 +MODULE_LICENSE("GPL");
3912 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3913 +MODULE_DESCRIPTION("ipmap type of IP sets");
3914 +
3915 +static int __init ip_set_ipmap_init(void)
3916 +{
3917 +       return ip_set_register_set_type(&ip_set_ipmap);
3918 +}
3919 +
3920 +static void __exit ip_set_ipmap_fini(void)
3921 +{
3922 +       /* FIXME: possible race with ip_set_create() */
3923 +       ip_set_unregister_set_type(&ip_set_ipmap);
3924 +}
3925 +
3926 +module_init(ip_set_ipmap_init);
3927 +module_exit(ip_set_ipmap_fini);
3928 diff -uNrp linux-2.6./net/ipv4/netfilter/ip_set_ipporthash.c linux-2.6/net/ipv4/netfilter/ip_set_ipporthash.c
3929 --- linux-2.6./net/ipv4/netfilter/ip_set_ipporthash.c   1970-01-01 01:00:00.000000000 +0100
3930 +++ linux-2.6/net/ipv4/netfilter/ip_set_ipporthash.c    2007-09-05 13:41:12.000000000 +0200
3931 @@ -0,0 +1,581 @@
3932 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3933 + *
3934 + * This program is free software; you can redistribute it and/or modify
3935 + * it under the terms of the GNU General Public License version 2 as
3936 + * published by the Free Software Foundation.  
3937 + */
3938 +
3939 +/* Kernel module implementing an ip+port hash set */
3940 +
3941 +#include <linux/module.h>
3942 +#include <linux/ip.h>
3943 +#include <linux/tcp.h>
3944 +#include <linux/udp.h>
3945 +#include <linux/skbuff.h>
3946 +#include <linux/version.h>
3947 +#include <linux/jhash.h>
3948 +#include <linux/netfilter_ipv4/ip_tables.h>
3949 +#include <linux/netfilter_ipv4/ip_set.h>
3950 +#include <linux/errno.h>
3951 +#include <asm/uaccess.h>
3952 +#include <asm/bitops.h>
3953 +#include <linux/spinlock.h>
3954 +#include <linux/vmalloc.h>
3955 +#include <linux/random.h>
3956 +
3957 +#include <net/ip.h>
3958 +
3959 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3960 +#include <linux/netfilter_ipv4/ip_set_ipporthash.h>
3961 +
3962 +static int limit = MAX_RANGE;
3963 +
3964 +/* We must handle non-linear skbs */
3965 +static inline ip_set_ip_t
3966 +get_port(const struct sk_buff *skb, u_int32_t flags)
3967 +{
3968 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3969 +       struct iphdr *iph = ip_hdr(skb);
3970 +#else
3971 +       struct iphdr *iph = skb->nh.iph;
3972 +#endif
3973 +       u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
3974 +
3975 +       switch (iph->protocol) {
3976 +       case IPPROTO_TCP: {
3977 +               struct tcphdr tcph;
3978 +               
3979 +               /* See comments at tcp_match in ip_tables.c */
3980 +               if (offset)
3981 +                       return INVALID_PORT;
3982 +
3983 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3984 +               if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
3985 +#else
3986 +               if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
3987 +#endif
3988 +                       /* No choice either */
3989 +                       return INVALID_PORT;
3990 +               
3991 +               return ntohs(flags & IPSET_SRC ?
3992 +                            tcph.source : tcph.dest);
3993 +           }
3994 +       case IPPROTO_UDP: {
3995 +               struct udphdr udph;
3996 +
3997 +               if (offset)
3998 +                       return INVALID_PORT;
3999 +
4000 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4001 +               if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
4002 +#else
4003 +               if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
4004 +#endif
4005 +                       /* No choice either */
4006 +                       return INVALID_PORT;
4007 +               
4008 +               return ntohs(flags & IPSET_SRC ?
4009 +                            udph.source : udph.dest);
4010 +           }
4011 +       default:
4012 +               return INVALID_PORT;
4013 +       }
4014 +}
4015 +
4016 +static inline __u32
4017 +jhash_ip(const struct ip_set_ipporthash *map, uint16_t i, ip_set_ip_t ip)
4018 +{
4019 +       return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
4020 +}
4021 +
4022 +#define HASH_IP(map, ip, port) (port + ((ip - ((map)->first_ip)) << 16))
4023 +
4024 +static inline __u32
4025 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4026 +       ip_set_ip_t *hash_ip)
4027 +{
4028 +       struct ip_set_ipporthash *map = 
4029 +               (struct ip_set_ipporthash *) set->data;
4030 +       __u32 id;
4031 +       u_int16_t i;
4032 +       ip_set_ip_t *elem;
4033 +
4034 +       *hash_ip = HASH_IP(map, ip, port);
4035 +       DP("set: %s, ipport:%u.%u.%u.%u:%u, %u.%u.%u.%u",
4036 +          set->name, HIPQUAD(ip), port, HIPQUAD(*hash_ip));
4037 +       
4038 +       for (i = 0; i < map->probes; i++) {
4039 +               id = jhash_ip(map, i, *hash_ip) % map->hashsize;
4040 +               DP("hash key: %u", id);
4041 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4042 +               if (*elem == *hash_ip)
4043 +                       return id;
4044 +               /* No shortcut at testing - there can be deleted
4045 +                * entries. */
4046 +       }
4047 +       return UINT_MAX;
4048 +}
4049 +
4050 +static inline int
4051 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4052 +        ip_set_ip_t *hash_ip)
4053 +{
4054 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4055 +       
4056 +       if (ip < map->first_ip || ip > map->last_ip)
4057 +               return -ERANGE;
4058 +
4059 +       return (hash_id(set, ip, port, hash_ip) != UINT_MAX);
4060 +}
4061 +
4062 +static int
4063 +testip(struct ip_set *set, const void *data, size_t size,
4064 +       ip_set_ip_t *hash_ip)
4065 +{
4066 +       struct ip_set_req_ipporthash *req = 
4067 +           (struct ip_set_req_ipporthash *) data;
4068 +
4069 +       if (size != sizeof(struct ip_set_req_ipporthash)) {
4070 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4071 +                             sizeof(struct ip_set_req_ipporthash),
4072 +                             size);
4073 +               return -EINVAL;
4074 +       }
4075 +       return __testip(set, req->ip, req->port, hash_ip);
4076 +}
4077 +
4078 +static int
4079 +testip_kernel(struct ip_set *set, 
4080 +             const struct sk_buff *skb,
4081 +             ip_set_ip_t *hash_ip,
4082 +             const u_int32_t *flags,
4083 +             unsigned char index)
4084 +{
4085 +       ip_set_ip_t port;
4086 +       int res;
4087 +
4088 +       if (flags[index+1] == 0)
4089 +               return 0;
4090 +               
4091 +       port = get_port(skb, flags[index+1]);
4092 +
4093 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4094 +          flags[index] & IPSET_SRC ? "SRC" : "DST",
4095 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4096 +          NIPQUAD(ip_hdr(skb)->saddr),
4097 +          NIPQUAD(ip_hdr(skb)->daddr));
4098 +#else
4099 +          NIPQUAD(skb->nh.iph->saddr),
4100 +          NIPQUAD(skb->nh.iph->daddr));
4101 +#endif
4102 +       DP("flag %s port %u",
4103 +          flags[index+1] & IPSET_SRC ? "SRC" : "DST", 
4104 +          port);       
4105 +       if (port == INVALID_PORT)
4106 +               return 0;       
4107 +
4108 +       res =  __testip(set,
4109 +                       ntohl(flags[index] & IPSET_SRC 
4110 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4111 +                                       ? ip_hdr(skb)->saddr 
4112 +                                       : ip_hdr(skb)->daddr),
4113 +#else
4114 +                                       ? skb->nh.iph->saddr 
4115 +                                       : skb->nh.iph->daddr),
4116 +#endif
4117 +                       port,
4118 +                       hash_ip);
4119 +       return (res < 0 ? 0 : res);
4120 +       
4121 +}
4122 +
4123 +static inline int
4124 +__add_haship(struct ip_set_ipporthash *map, ip_set_ip_t hash_ip)
4125 +{
4126 +       __u32 probe;
4127 +       u_int16_t i;
4128 +       ip_set_ip_t *elem;
4129 +
4130 +       for (i = 0; i < map->probes; i++) {
4131 +               probe = jhash_ip(map, i, hash_ip) % map->hashsize;
4132 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
4133 +               if (*elem == hash_ip)
4134 +                       return -EEXIST;
4135 +               if (!*elem) {
4136 +                       *elem = hash_ip;
4137 +                       map->elements++;
4138 +                       return 0;
4139 +               }
4140 +       }
4141 +       /* Trigger rehashing */
4142 +       return -EAGAIN;
4143 +}
4144 +
4145 +static inline int
4146 +__addip(struct ip_set_ipporthash *map, ip_set_ip_t ip, ip_set_ip_t port,
4147 +       ip_set_ip_t *hash_ip)
4148 +{
4149 +       if (map->elements > limit)
4150 +               return -ERANGE;
4151 +       if (ip < map->first_ip || ip > map->last_ip)
4152 +               return -ERANGE;
4153 +
4154 +       *hash_ip = HASH_IP(map, ip, port);
4155 +       
4156 +       return __add_haship(map, *hash_ip);
4157 +}
4158 +
4159 +static int
4160 +addip(struct ip_set *set, const void *data, size_t size,
4161 +        ip_set_ip_t *hash_ip)
4162 +{
4163 +       struct ip_set_req_ipporthash *req = 
4164 +           (struct ip_set_req_ipporthash *) data;
4165 +
4166 +       if (size != sizeof(struct ip_set_req_ipporthash)) {
4167 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4168 +                             sizeof(struct ip_set_req_ipporthash),
4169 +                             size);
4170 +               return -EINVAL;
4171 +       }
4172 +       return __addip((struct ip_set_ipporthash *) set->data, 
4173 +                       req->ip, req->port, hash_ip);
4174 +}
4175 +
4176 +static int
4177 +addip_kernel(struct ip_set *set, 
4178 +            const struct sk_buff *skb,
4179 +            ip_set_ip_t *hash_ip,
4180 +            const u_int32_t *flags,
4181 +            unsigned char index)
4182 +{
4183 +       ip_set_ip_t port;
4184 +
4185 +       if (flags[index+1] == 0)
4186 +               return -EINVAL;
4187 +               
4188 +       port = get_port(skb, flags[index+1]);
4189 +
4190 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4191 +          flags[index] & IPSET_SRC ? "SRC" : "DST",
4192 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4193 +          NIPQUAD(ip_hdr(skb)->saddr),
4194 +          NIPQUAD(ip_hdr(skb)->daddr));
4195 +#else
4196 +          NIPQUAD(skb->nh.iph->saddr),
4197 +          NIPQUAD(skb->nh.iph->daddr));
4198 +#endif
4199 +       DP("flag %s port %u", 
4200 +          flags[index+1] & IPSET_SRC ? "SRC" : "DST", 
4201 +          port);       
4202 +       if (port == INVALID_PORT)
4203 +               return -EINVAL; 
4204 +
4205 +       return __addip((struct ip_set_ipporthash *) set->data,
4206 +                      ntohl(flags[index] & IPSET_SRC 
4207 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4208 +                               ? ip_hdr(skb)->saddr 
4209 +                               : ip_hdr(skb)->daddr),
4210 +#else
4211 +                               ? skb->nh.iph->saddr 
4212 +                               : skb->nh.iph->daddr),
4213 +#endif
4214 +                      port,
4215 +                      hash_ip);
4216 +}
4217 +
4218 +static int retry(struct ip_set *set)
4219 +{
4220 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4221 +       ip_set_ip_t *elem;
4222 +       void *members;
4223 +       u_int32_t i, hashsize = map->hashsize;
4224 +       int res;
4225 +       struct ip_set_ipporthash *tmp;
4226 +       
4227 +       if (map->resize == 0)
4228 +               return -ERANGE;
4229 +
4230 +    again:
4231 +       res = 0;
4232 +       
4233 +       /* Calculate new hash size */
4234 +       hashsize += (hashsize * map->resize)/100;
4235 +       if (hashsize == map->hashsize)
4236 +               hashsize++;
4237 +       
4238 +       ip_set_printk("rehashing of set %s triggered: "
4239 +                     "hashsize grows from %u to %u",
4240 +                     set->name, map->hashsize, hashsize);
4241 +
4242 +       tmp = kmalloc(sizeof(struct ip_set_ipporthash) 
4243 +                     + map->probes * sizeof(uint32_t), GFP_ATOMIC);
4244 +       if (!tmp) {
4245 +               DP("out of memory for %d bytes",
4246 +                  sizeof(struct ip_set_ipporthash)
4247 +                  + map->probes * sizeof(uint32_t));
4248 +               return -ENOMEM;
4249 +       }
4250 +       tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
4251 +       if (!tmp->members) {
4252 +               DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
4253 +               kfree(tmp);
4254 +               return -ENOMEM;
4255 +       }
4256 +       tmp->hashsize = hashsize;
4257 +       tmp->elements = 0;
4258 +       tmp->probes = map->probes;
4259 +       tmp->resize = map->resize;
4260 +       tmp->first_ip = map->first_ip;
4261 +       tmp->last_ip = map->last_ip;
4262 +       memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
4263 +       
4264 +       write_lock_bh(&set->lock);
4265 +       map = (struct ip_set_ipporthash *) set->data; /* Play safe */
4266 +       for (i = 0; i < map->hashsize && res == 0; i++) {
4267 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);     
4268 +               if (*elem)
4269 +                       res = __add_haship(tmp, *elem);
4270 +       }
4271 +       if (res) {
4272 +               /* Failure, try again */
4273 +               write_unlock_bh(&set->lock);
4274 +               harray_free(tmp->members);
4275 +               kfree(tmp);
4276 +               goto again;
4277 +       }
4278 +       
4279 +       /* Success at resizing! */
4280 +       members = map->members;
4281 +
4282 +       map->hashsize = tmp->hashsize;
4283 +       map->members = tmp->members;
4284 +       write_unlock_bh(&set->lock);
4285 +
4286 +       harray_free(members);
4287 +       kfree(tmp);
4288 +
4289 +       return 0;
4290 +}
4291 +
4292 +static inline int
4293 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4294 +       ip_set_ip_t *hash_ip)
4295 +{
4296 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4297 +       ip_set_ip_t id;
4298 +       ip_set_ip_t *elem;
4299 +
4300 +       if (ip < map->first_ip || ip > map->last_ip)
4301 +               return -ERANGE;
4302 +
4303 +       id = hash_id(set, ip, port, hash_ip);
4304 +
4305 +       if (id == UINT_MAX)
4306 +               return -EEXIST;
4307 +               
4308 +       elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4309 +       *elem = 0;
4310 +       map->elements--;
4311 +
4312 +       return 0;
4313 +}
4314 +
4315 +static int
4316 +delip(struct ip_set *set, const void *data, size_t size,
4317 +        ip_set_ip_t *hash_ip)
4318 +{
4319 +       struct ip_set_req_ipporthash *req =
4320 +           (struct ip_set_req_ipporthash *) data;
4321 +
4322 +       if (size != sizeof(struct ip_set_req_ipporthash)) {
4323 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4324 +                             sizeof(struct ip_set_req_ipporthash),
4325 +                             size);
4326 +               return -EINVAL;
4327 +       }
4328 +       return __delip(set, req->ip, req->port, hash_ip);
4329 +}
4330 +
4331 +static int
4332 +delip_kernel(struct ip_set *set, 
4333 +            const struct sk_buff *skb,
4334 +            ip_set_ip_t *hash_ip,
4335 +            const u_int32_t *flags,
4336 +            unsigned char index)
4337 +{
4338 +       ip_set_ip_t port;
4339 +
4340 +       if (flags[index+1] == 0)
4341 +               return -EINVAL;
4342 +               
4343 +       port = get_port(skb, flags[index+1]);
4344 +
4345 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4346 +          flags[index] & IPSET_SRC ? "SRC" : "DST",
4347 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4348 +          NIPQUAD(ip_hdr(skb)->saddr),
4349 +          NIPQUAD(ip_hdr(skb)->daddr));
4350 +#else
4351 +          NIPQUAD(skb->nh.iph->saddr),
4352 +          NIPQUAD(skb->nh.iph->daddr));
4353 +#endif
4354 +       DP("flag %s port %u",
4355 +          flags[index+1] & IPSET_SRC ? "SRC" : "DST", 
4356 +          port);       
4357 +       if (port == INVALID_PORT)
4358 +               return -EINVAL; 
4359 +
4360 +       return __delip(set,
4361 +                      ntohl(flags[index] & IPSET_SRC 
4362 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4363 +                               ? ip_hdr(skb)->saddr 
4364 +                               : ip_hdr(skb)->daddr),
4365 +#else
4366 +                               ? skb->nh.iph->saddr 
4367 +                               : skb->nh.iph->daddr),
4368 +#endif
4369 +                      port,
4370 +                      hash_ip);
4371 +}
4372 +
4373 +static int create(struct ip_set *set, const void *data, size_t size)
4374 +{
4375 +       struct ip_set_req_ipporthash_create *req =
4376 +           (struct ip_set_req_ipporthash_create *) data;
4377 +       struct ip_set_ipporthash *map;
4378 +       uint16_t i;
4379 +
4380 +       if (size != sizeof(struct ip_set_req_ipporthash_create)) {
4381 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4382 +                              sizeof(struct ip_set_req_ipporthash_create),
4383 +                              size);
4384 +               return -EINVAL;
4385 +       }
4386 +
4387 +       if (req->hashsize < 1) {
4388 +               ip_set_printk("hashsize too small");
4389 +               return -ENOEXEC;
4390 +       }
4391 +
4392 +       if (req->probes < 1) {
4393 +               ip_set_printk("probes too small");
4394 +               return -ENOEXEC;
4395 +       }
4396 +
4397 +       map = kmalloc(sizeof(struct ip_set_ipporthash) 
4398 +                     + req->probes * sizeof(uint32_t), GFP_KERNEL);
4399 +       if (!map) {
4400 +               DP("out of memory for %d bytes",
4401 +                  sizeof(struct ip_set_ipporthash)
4402 +                  + req->probes * sizeof(uint32_t));
4403 +               return -ENOMEM;
4404 +       }
4405 +       for (i = 0; i < req->probes; i++)
4406 +               get_random_bytes(((uint32_t *) map->initval)+i, 4);
4407 +       map->elements = 0;
4408 +       map->hashsize = req->hashsize;
4409 +       map->probes = req->probes;
4410 +       map->resize = req->resize;
4411 +       map->first_ip = req->from;
4412 +       map->last_ip = req->to;
4413 +       map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
4414 +       if (!map->members) {
4415 +               DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
4416 +               kfree(map);
4417 +               return -ENOMEM;
4418 +       }
4419 +
4420 +       set->data = map;
4421 +       return 0;
4422 +}
4423 +
4424 +static void destroy(struct ip_set *set)
4425 +{
4426 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4427 +
4428 +       harray_free(map->members);
4429 +       kfree(map);
4430 +
4431 +       set->data = NULL;
4432 +}
4433 +
4434 +static void flush(struct ip_set *set)
4435 +{
4436 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4437 +       harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
4438 +       map->elements = 0;
4439 +}
4440 +
4441 +static void list_header(const struct ip_set *set, void *data)
4442 +{
4443 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4444 +       struct ip_set_req_ipporthash_create *header =
4445 +           (struct ip_set_req_ipporthash_create *) data;
4446 +
4447 +       header->hashsize = map->hashsize;
4448 +       header->probes = map->probes;
4449 +       header->resize = map->resize;
4450 +       header->from = map->first_ip;
4451 +       header->to = map->last_ip;
4452 +}
4453 +
4454 +static int list_members_size(const struct ip_set *set)
4455 +{
4456 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4457 +
4458 +       return (map->hashsize * sizeof(ip_set_ip_t));
4459 +}
4460 +
4461 +static void list_members(const struct ip_set *set, void *data)
4462 +{
4463 +       struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4464 +       ip_set_ip_t i, *elem;
4465 +
4466 +       for (i = 0; i < map->hashsize; i++) {
4467 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);     
4468 +               ((ip_set_ip_t *)data)[i] = *elem;
4469 +       }
4470 +}
4471 +
4472 +static struct ip_set_type ip_set_ipporthash = {
4473 +       .typename               = SETTYPE_NAME,
4474 +       .features               = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_DATA_DOUBLE,
4475 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
4476 +       .create                 = &create,
4477 +       .destroy                = &destroy,
4478 +       .flush                  = &flush,
4479 +       .reqsize                = sizeof(struct ip_set_req_ipporthash),
4480 +       .addip                  = &addip,
4481 +       .addip_kernel           = &addip_kernel,
4482 +       .retry                  = &retry,
4483 +       .delip                  = &delip,
4484 +       .delip_kernel           = &delip_kernel,
4485 +       .testip                 = &testip,
4486 +       .testip_kernel          = &testip_kernel,
4487 +       .header_size            = sizeof(struct ip_set_req_ipporthash_create),
4488 +       .list_header            = &list_header,
4489 +       .list_members_size      = &list_members_size,
4490 +       .list_members           = &list_members,
4491 +       .me                     = THIS_MODULE,
4492 +};
4493 +
4494 +MODULE_LICENSE("GPL");
4495 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4496 +MODULE_DESCRIPTION("ipporthash type of IP sets");
4497 +module_param(limit, int, 0600);
4498 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
4499 +
4500 +static int __init ip_set_ipporthash_init(void)
4501 +{
4502 +       return ip_set_register_set_type(&ip_set_ipporthash);
4503 +}
4504 +
4505 +static void __exit ip_set_ipporthash_fini(void)
4506 +{
4507 +       /* FIXME: possible race with ip_set_create() */
4508 +       ip_set_unregister_set_type(&ip_set_ipporthash);
4509 +}
4510 +
4511 +module_init(ip_set_ipporthash_init);
4512 +module_exit(ip_set_ipporthash_fini);
4513 diff -uNrp linux-2.6./net/ipv4/netfilter/ip_set_iptree.c linux-2.6/net/ipv4/netfilter/ip_set_iptree.c
4514 --- linux-2.6./net/ipv4/netfilter/ip_set_iptree.c       1970-01-01 01:00:00.000000000 +0100
4515 +++ linux-2.6/net/ipv4/netfilter/ip_set_iptree.c        2007-08-28 13:29:12.000000000 +0200
4516 @@ -0,0 +1,612 @@
4517 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4518 + *
4519 + * This program is free software; you can redistribute it and/or modify
4520 + * it under the terms of the GNU General Public License version 2 as
4521 + * published by the Free Software Foundation.  
4522 + */
4523 +
4524 +/* Kernel module implementing an IP set type: the iptree type */
4525 +
4526 +#include <linux/version.h>
4527 +#include <linux/module.h>
4528 +#include <linux/ip.h>
4529 +#include <linux/skbuff.h>
4530 +#include <linux/slab.h>
4531 +#include <linux/delay.h>
4532 +#include <linux/netfilter_ipv4/ip_tables.h>
4533 +#include <linux/netfilter_ipv4/ip_set.h>
4534 +#include <linux/errno.h>
4535 +#include <asm/uaccess.h>
4536 +#include <asm/bitops.h>
4537 +#include <linux/spinlock.h>
4538 +
4539 +/* Backward compatibility */
4540 +#ifndef __nocast
4541 +#define __nocast
4542 +#endif
4543 +
4544 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
4545 +
4546 +static int limit = MAX_RANGE;
4547 +
4548 +/* Garbage collection interval in seconds: */
4549 +#define IPTREE_GC_TIME         5*60
4550 +/* Sleep so many milliseconds before trying again 
4551 + * to delete the gc timer at destroying/flushing a set */ 
4552 +#define IPTREE_DESTROY_SLEEP   100
4553 +
4554 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
4555 +static struct kmem_cache *branch_cachep;
4556 +static struct kmem_cache *leaf_cachep;
4557 +#else
4558 +static kmem_cache_t *branch_cachep;
4559 +static kmem_cache_t *leaf_cachep;
4560 +#endif
4561 +
4562 +#if defined(__LITTLE_ENDIAN)
4563 +#define ABCD(a,b,c,d,addrp) do {               \
4564 +       a = ((unsigned char *)addrp)[3];        \
4565 +       b = ((unsigned char *)addrp)[2];        \
4566 +       c = ((unsigned char *)addrp)[1];        \
4567 +       d = ((unsigned char *)addrp)[0];        \
4568 +} while (0)
4569 +#elif defined(__BIG_ENDIAN)
4570 +#define ABCD(a,b,c,d,addrp) do {               \
4571 +       a = ((unsigned char *)addrp)[0];        \
4572 +       b = ((unsigned char *)addrp)[1];        \
4573 +       c = ((unsigned char *)addrp)[2];        \
4574 +       d = ((unsigned char *)addrp)[3];        \
4575 +} while (0)
4576 +#else
4577 +#error "Please fix asm/byteorder.h"
4578 +#endif /* __LITTLE_ENDIAN */
4579 +
4580 +#define TESTIP_WALK(map, elem, branch) do {    \
4581 +       if ((map)->tree[elem]) {                \
4582 +               branch = (map)->tree[elem];     \
4583 +       } else                                  \
4584 +               return 0;                       \
4585 +} while (0)
4586 +
4587 +static inline int
4588 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4589 +{
4590 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4591 +       struct ip_set_iptreeb *btree;
4592 +       struct ip_set_iptreec *ctree;
4593 +       struct ip_set_iptreed *dtree;
4594 +       unsigned char a,b,c,d;
4595 +
4596 +       if (!ip)
4597 +               return -ERANGE;
4598 +       
4599 +       *hash_ip = ip;
4600 +       ABCD(a, b, c, d, hash_ip);
4601 +       DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
4602 +       TESTIP_WALK(map, a, btree);
4603 +       TESTIP_WALK(btree, b, ctree);
4604 +       TESTIP_WALK(ctree, c, dtree);
4605 +       DP("%lu %lu", dtree->expires[d], jiffies);
4606 +       return dtree->expires[d]
4607 +              && (!map->timeout
4608 +                  || time_after(dtree->expires[d], jiffies));
4609 +}
4610 +
4611 +static int
4612 +testip(struct ip_set *set, const void *data, size_t size,
4613 +       ip_set_ip_t *hash_ip)
4614 +{
4615 +       struct ip_set_req_iptree *req = 
4616 +           (struct ip_set_req_iptree *) data;
4617 +
4618 +       if (size != sizeof(struct ip_set_req_iptree)) {
4619 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4620 +                             sizeof(struct ip_set_req_iptree),
4621 +                             size);
4622 +               return -EINVAL;
4623 +       }
4624 +       return __testip(set, req->ip, hash_ip);
4625 +}
4626 +
4627 +static int
4628 +testip_kernel(struct ip_set *set, 
4629 +             const struct sk_buff *skb,
4630 +             ip_set_ip_t *hash_ip,
4631 +             const u_int32_t *flags,
4632 +             unsigned char index)
4633 +{
4634 +       int res;
4635 +       
4636 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4637 +          flags[index] & IPSET_SRC ? "SRC" : "DST",
4638 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4639 +          NIPQUAD(ip_hdr(skb)->saddr),
4640 +          NIPQUAD(ip_hdr(skb)->daddr));
4641 +#else
4642 +          NIPQUAD(skb->nh.iph->saddr),
4643 +          NIPQUAD(skb->nh.iph->daddr));
4644 +#endif
4645 +
4646 +       res =  __testip(set,
4647 +                       ntohl(flags[index] & IPSET_SRC 
4648 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4649 +                               ? ip_hdr(skb)->saddr 
4650 +                               : ip_hdr(skb)->daddr),
4651 +#else
4652 +                               ? skb->nh.iph->saddr 
4653 +                               : skb->nh.iph->daddr),
4654 +#endif
4655 +                       hash_ip);
4656 +       return (res < 0 ? 0 : res);
4657 +}
4658 +
4659 +#define ADDIP_WALK(map, elem, branch, type, cachep) do {       \
4660 +       if ((map)->tree[elem]) {                                \
4661 +               DP("found %u", elem);                           \
4662 +               branch = (map)->tree[elem];                     \
4663 +       } else {                                                \
4664 +               branch = (type *)                               \
4665 +                       kmem_cache_alloc(cachep, GFP_ATOMIC);   \
4666 +               if (branch == NULL)                             \
4667 +                       return -ENOMEM;                         \
4668 +               memset(branch, 0, sizeof(*branch));             \
4669 +               (map)->tree[elem] = branch;                     \
4670 +               DP("alloc %u", elem);                           \
4671 +       }                                                       \
4672 +} while (0)    
4673 +
4674 +static inline int
4675 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
4676 +       ip_set_ip_t *hash_ip)
4677 +{
4678 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4679 +       struct ip_set_iptreeb *btree;
4680 +       struct ip_set_iptreec *ctree;
4681 +       struct ip_set_iptreed *dtree;
4682 +       unsigned char a,b,c,d;
4683 +       int ret = 0;
4684 +       
4685 +       if (!ip || map->elements >= limit)
4686 +               /* We could call the garbage collector
4687 +                * but it's probably overkill */
4688 +               return -ERANGE;
4689 +       
4690 +       *hash_ip = ip;
4691 +       ABCD(a, b, c, d, hash_ip);
4692 +       DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
4693 +       ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
4694 +       ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
4695 +       ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
4696 +       if (dtree->expires[d]
4697 +           && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4698 +               ret = -EEXIST;
4699 +       dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
4700 +       /* Lottery: I won! */
4701 +       if (dtree->expires[d] == 0)
4702 +               dtree->expires[d] = 1;
4703 +       DP("%u %lu", d, dtree->expires[d]);
4704 +       if (ret == 0)
4705 +               map->elements++;
4706 +       return ret;
4707 +}
4708 +
4709 +static int
4710 +addip(struct ip_set *set, const void *data, size_t size,
4711 +      ip_set_ip_t *hash_ip)
4712 +{
4713 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4714 +       struct ip_set_req_iptree *req = 
4715 +               (struct ip_set_req_iptree *) data;
4716 +
4717 +       if (size != sizeof(struct ip_set_req_iptree)) {
4718 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4719 +                             sizeof(struct ip_set_req_iptree),
4720 +                             size);
4721 +               return -EINVAL;
4722 +       }
4723 +       DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
4724 +       return __addip(set, req->ip,
4725 +                      req->timeout ? req->timeout : map->timeout,
4726 +                      hash_ip);
4727 +}
4728 +
4729 +static int
4730 +addip_kernel(struct ip_set *set, 
4731 +            const struct sk_buff *skb,
4732 +            ip_set_ip_t *hash_ip,
4733 +            const u_int32_t *flags,
4734 +            unsigned char index)
4735 +{
4736 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4737 +
4738 +       return __addip(set,
4739 +                      ntohl(flags[index] & IPSET_SRC 
4740 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4741 +                               ? ip_hdr(skb)->saddr 
4742 +                               : ip_hdr(skb)->daddr),
4743 +#else
4744 +                               ? skb->nh.iph->saddr 
4745 +                               : skb->nh.iph->daddr),
4746 +#endif
4747 +                      map->timeout,
4748 +                      hash_ip);
4749 +}
4750 +
4751 +#define DELIP_WALK(map, elem, branch) do {     \
4752 +       if ((map)->tree[elem]) {                \
4753 +               branch = (map)->tree[elem];     \
4754 +       } else                                  \
4755 +               return -EEXIST;                 \
4756 +} while (0)
4757 +
4758 +static inline int 
4759 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4760 +{
4761 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4762 +       struct ip_set_iptreeb *btree;
4763 +       struct ip_set_iptreec *ctree;
4764 +       struct ip_set_iptreed *dtree;
4765 +       unsigned char a,b,c,d;
4766 +       
4767 +       if (!ip)
4768 +               return -ERANGE;
4769 +               
4770 +       *hash_ip = ip;
4771 +       ABCD(a, b, c, d, hash_ip);
4772 +       DELIP_WALK(map, a, btree);
4773 +       DELIP_WALK(btree, b, ctree);
4774 +       DELIP_WALK(ctree, c, dtree);
4775 +
4776 +       if (dtree->expires[d]) {
4777 +               dtree->expires[d] = 0;
4778 +               map->elements--;
4779 +               return 0;
4780 +       }
4781 +       return -EEXIST;
4782 +}
4783 +
4784 +static int
4785 +delip(struct ip_set *set, const void *data, size_t size,
4786 +      ip_set_ip_t *hash_ip)
4787 +{
4788 +       struct ip_set_req_iptree *req =
4789 +           (struct ip_set_req_iptree *) data;
4790 +
4791 +       if (size != sizeof(struct ip_set_req_iptree)) {
4792 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4793 +                             sizeof(struct ip_set_req_iptree),
4794 +                             size);
4795 +               return -EINVAL;
4796 +       }
4797 +       return __delip(set, req->ip, hash_ip);
4798 +}
4799 +
4800 +static int
4801 +delip_kernel(struct ip_set *set, 
4802 +            const struct sk_buff *skb,
4803 +            ip_set_ip_t *hash_ip,
4804 +            const u_int32_t *flags,
4805 +            unsigned char index)
4806 +{
4807 +       return __delip(set,
4808 +                      ntohl(flags[index] & IPSET_SRC 
4809 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4810 +                               ? ip_hdr(skb)->saddr 
4811 +                               : ip_hdr(skb)->daddr),
4812 +#else
4813 +                               ? skb->nh.iph->saddr 
4814 +                               : skb->nh.iph->daddr),
4815 +#endif
4816 +                      hash_ip);
4817 +}
4818 +
4819 +#define LOOP_WALK_BEGIN(map, i, branch) \
4820 +       for (i = 0; i < 256; i++) {     \
4821 +               if (!(map)->tree[i])    \
4822 +                       continue;       \
4823 +               branch = (map)->tree[i]
4824 +
4825 +#define LOOP_WALK_END }
4826 +
4827 +static void ip_tree_gc(unsigned long ul_set)
4828 +{
4829 +       struct ip_set *set = (void *) ul_set;
4830 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4831 +       struct ip_set_iptreeb *btree;
4832 +       struct ip_set_iptreec *ctree;
4833 +       struct ip_set_iptreed *dtree;
4834 +       unsigned int a,b,c,d;
4835 +       unsigned char i,j,k;
4836 +
4837 +       i = j = k = 0;
4838 +       DP("gc: %s", set->name);
4839 +       write_lock_bh(&set->lock);
4840 +       LOOP_WALK_BEGIN(map, a, btree);
4841 +       LOOP_WALK_BEGIN(btree, b, ctree);
4842 +       LOOP_WALK_BEGIN(ctree, c, dtree);
4843 +       for (d = 0; d < 256; d++) {
4844 +               if (dtree->expires[d]) {
4845 +                       DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4846 +                           a, b, c, d,
4847 +                           dtree->expires[d], jiffies);
4848 +                       if (map->timeout
4849 +                           && time_before(dtree->expires[d], jiffies)) {
4850 +                               dtree->expires[d] = 0;
4851 +                               map->elements--;
4852 +                       } else
4853 +                               k = 1;
4854 +               }
4855 +       }
4856 +       if (k == 0) {
4857 +               DP("gc: %s: leaf %u %u %u empty",
4858 +                   set->name, a, b, c);
4859 +               kmem_cache_free(leaf_cachep, dtree);
4860 +               ctree->tree[c] = NULL;
4861 +       } else {
4862 +               DP("gc: %s: leaf %u %u %u not empty",
4863 +                   set->name, a, b, c);
4864 +               j = 1;
4865 +               k = 0;
4866 +       }
4867 +       LOOP_WALK_END;
4868 +       if (j == 0) {
4869 +               DP("gc: %s: branch %u %u empty",
4870 +                   set->name, a, b);
4871 +               kmem_cache_free(branch_cachep, ctree);
4872 +               btree->tree[b] = NULL;
4873 +       } else {
4874 +               DP("gc: %s: branch %u %u not empty",
4875 +                   set->name, a, b);
4876 +               i = 1;
4877 +               j = k = 0;
4878 +       }
4879 +       LOOP_WALK_END;
4880 +       if (i == 0) {
4881 +               DP("gc: %s: branch %u empty",
4882 +                   set->name, a);
4883 +               kmem_cache_free(branch_cachep, btree);
4884 +               map->tree[a] = NULL;
4885 +       } else {
4886 +               DP("gc: %s: branch %u not empty",
4887 +                   set->name, a);
4888 +               i = j = k = 0;
4889 +       }
4890 +       LOOP_WALK_END;
4891 +       write_unlock_bh(&set->lock);
4892 +       
4893 +       map->gc.expires = jiffies + map->gc_interval * HZ;
4894 +       add_timer(&map->gc);
4895 +}
4896 +
4897 +static inline void init_gc_timer(struct ip_set *set)
4898 +{
4899 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4900 +
4901 +       /* Even if there is no timeout for the entries,
4902 +        * we still have to call gc because delete
4903 +        * do not clean up empty branches */
4904 +       map->gc_interval = IPTREE_GC_TIME;
4905 +       init_timer(&map->gc);
4906 +       map->gc.data = (unsigned long) set;
4907 +       map->gc.function = ip_tree_gc;
4908 +       map->gc.expires = jiffies + map->gc_interval * HZ;
4909 +       add_timer(&map->gc);
4910 +}
4911 +
4912 +static int create(struct ip_set *set, const void *data, size_t size)
4913 +{
4914 +       struct ip_set_req_iptree_create *req =
4915 +           (struct ip_set_req_iptree_create *) data;
4916 +       struct ip_set_iptree *map;
4917 +
4918 +       if (size != sizeof(struct ip_set_req_iptree_create)) {
4919 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4920 +                             sizeof(struct ip_set_req_iptree_create),
4921 +                             size);
4922 +               return -EINVAL;
4923 +       }
4924 +
4925 +       map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4926 +       if (!map) {
4927 +               DP("out of memory for %d bytes",
4928 +                  sizeof(struct ip_set_iptree));
4929 +               return -ENOMEM;
4930 +       }
4931 +       memset(map, 0, sizeof(*map));
4932 +       map->timeout = req->timeout;
4933 +       map->elements = 0;
4934 +       set->data = map;
4935 +
4936 +       init_gc_timer(set);
4937 +
4938 +       return 0;
4939 +}
4940 +
4941 +static void __flush(struct ip_set_iptree *map)
4942 +{
4943 +       struct ip_set_iptreeb *btree;
4944 +       struct ip_set_iptreec *ctree;
4945 +       struct ip_set_iptreed *dtree;
4946 +       unsigned int a,b,c;
4947 +
4948 +       LOOP_WALK_BEGIN(map, a, btree);
4949 +       LOOP_WALK_BEGIN(btree, b, ctree);
4950 +       LOOP_WALK_BEGIN(ctree, c, dtree);
4951 +       kmem_cache_free(leaf_cachep, dtree);
4952 +       LOOP_WALK_END;
4953 +       kmem_cache_free(branch_cachep, ctree);
4954 +       LOOP_WALK_END;
4955 +       kmem_cache_free(branch_cachep, btree);
4956 +       LOOP_WALK_END;
4957 +       map->elements = 0;
4958 +}
4959 +
4960 +static void destroy(struct ip_set *set)
4961 +{
4962 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4963 +
4964 +       /* gc might be running */
4965 +       while (!del_timer(&map->gc))
4966 +               msleep(IPTREE_DESTROY_SLEEP);
4967 +       __flush(map);
4968 +       kfree(map);
4969 +       set->data = NULL;
4970 +}
4971 +
4972 +static void flush(struct ip_set *set)
4973 +{
4974 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4975 +       unsigned int timeout = map->timeout;
4976 +       
4977 +       /* gc might be running */
4978 +       while (!del_timer(&map->gc))
4979 +               msleep(IPTREE_DESTROY_SLEEP);
4980 +       __flush(map);
4981 +       memset(map, 0, sizeof(*map));
4982 +       map->timeout = timeout;
4983 +
4984 +       init_gc_timer(set);
4985 +}
4986 +
4987 +static void list_header(const struct ip_set *set, void *data)
4988 +{
4989 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4990 +       struct ip_set_req_iptree_create *header =
4991 +           (struct ip_set_req_iptree_create *) data;
4992 +
4993 +       header->timeout = map->timeout;
4994 +}
4995 +
4996 +static int list_members_size(const struct ip_set *set)
4997 +{
4998 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4999 +       struct ip_set_iptreeb *btree;
5000 +       struct ip_set_iptreec *ctree;
5001 +       struct ip_set_iptreed *dtree;
5002 +       unsigned int a,b,c,d;
5003 +       unsigned int count = 0;
5004 +
5005 +       LOOP_WALK_BEGIN(map, a, btree);
5006 +       LOOP_WALK_BEGIN(btree, b, ctree);
5007 +       LOOP_WALK_BEGIN(ctree, c, dtree);
5008 +       for (d = 0; d < 256; d++) {
5009 +               if (dtree->expires[d]
5010 +                   && (!map->timeout || time_after(dtree->expires[d], jiffies)))
5011 +                       count++;
5012 +       }
5013 +       LOOP_WALK_END;
5014 +       LOOP_WALK_END;
5015 +       LOOP_WALK_END;
5016 +
5017 +       DP("members %u", count);
5018 +       return (count * sizeof(struct ip_set_req_iptree));
5019 +}
5020 +
5021 +static void list_members(const struct ip_set *set, void *data)
5022 +{
5023 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5024 +       struct ip_set_iptreeb *btree;
5025 +       struct ip_set_iptreec *ctree;
5026 +       struct ip_set_iptreed *dtree;
5027 +       unsigned int a,b,c,d;
5028 +       size_t offset = 0;
5029 +       struct ip_set_req_iptree *entry;
5030 +
5031 +       LOOP_WALK_BEGIN(map, a, btree);
5032 +       LOOP_WALK_BEGIN(btree, b, ctree);
5033 +       LOOP_WALK_BEGIN(ctree, c, dtree);
5034 +       for (d = 0; d < 256; d++) {
5035 +               if (dtree->expires[d]
5036 +                   && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
5037 +                       entry = (struct ip_set_req_iptree *)(data + offset);
5038 +                       entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
5039 +                       entry->timeout = !map->timeout ? 0 
5040 +                               : (dtree->expires[d] - jiffies)/HZ;
5041 +                       offset += sizeof(struct ip_set_req_iptree);
5042 +               }
5043 +       }
5044 +       LOOP_WALK_END;
5045 +       LOOP_WALK_END;
5046 +       LOOP_WALK_END;
5047 +}
5048 +
5049 +static struct ip_set_type ip_set_iptree = {
5050 +       .typename               = SETTYPE_NAME,
5051 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
5052 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
5053 +       .create                 = &create,
5054 +       .destroy                = &destroy,
5055 +       .flush                  = &flush,
5056 +       .reqsize                = sizeof(struct ip_set_req_iptree),
5057 +       .addip                  = &addip,
5058 +       .addip_kernel           = &addip_kernel,
5059 +       .delip                  = &delip,
5060 +       .delip_kernel           = &delip_kernel,
5061 +       .testip                 = &testip,
5062 +       .testip_kernel          = &testip_kernel,
5063 +       .header_size            = sizeof(struct ip_set_req_iptree_create),
5064 +       .list_header            = &list_header,
5065 +       .list_members_size      = &list_members_size,
5066 +       .list_members           = &list_members,
5067 +       .me                     = THIS_MODULE,
5068 +};
5069 +
5070 +MODULE_LICENSE("GPL");
5071 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5072 +MODULE_DESCRIPTION("iptree type of IP sets");
5073 +module_param(limit, int, 0600);
5074 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
5075 +
5076 +static int __init ip_set_iptree_init(void)
5077 +{
5078 +       int ret;
5079 +       
5080 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5081 +       branch_cachep = kmem_cache_create("ip_set_iptreeb",
5082 +                               sizeof(struct ip_set_iptreeb),
5083 +                               0, 0, NULL);
5084 +#else
5085 +       branch_cachep = kmem_cache_create("ip_set_iptreeb",
5086 +                               sizeof(struct ip_set_iptreeb),
5087 +                               0, 0, NULL, NULL);
5088 +#endif
5089 +       if (!branch_cachep) {
5090 +               printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
5091 +               ret = -ENOMEM;
5092 +               goto out;
5093 +       }
5094 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5095 +       leaf_cachep = kmem_cache_create("ip_set_iptreed",
5096 +                               sizeof(struct ip_set_iptreed),
5097 +                               0, 0, NULL);
5098 +#else
5099 +       leaf_cachep = kmem_cache_create("ip_set_iptreed",
5100 +                               sizeof(struct ip_set_iptreed),
5101 +                               0, 0, NULL, NULL);
5102 +#endif
5103 +       if (!leaf_cachep) {
5104 +               printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
5105 +               ret = -ENOMEM;
5106 +               goto free_branch;
5107 +       }
5108 +       ret = ip_set_register_set_type(&ip_set_iptree);
5109 +       if (ret == 0)
5110 +               goto out;
5111 +
5112 +       kmem_cache_destroy(leaf_cachep);
5113 +    free_branch:       
5114 +       kmem_cache_destroy(branch_cachep);
5115 +    out:
5116 +       return ret;
5117 +}
5118 +
5119 +static void __exit ip_set_iptree_fini(void)
5120 +{
5121 +       /* FIXME: possible race with ip_set_create() */
5122 +       ip_set_unregister_set_type(&ip_set_iptree);
5123 +       kmem_cache_destroy(leaf_cachep);
5124 +       kmem_cache_destroy(branch_cachep);
5125 +}
5126 +
5127 +module_init(ip_set_iptree_init);
5128 +module_exit(ip_set_iptree_fini);
5129 diff -uNrp linux-2.6./net/ipv4/netfilter/ip_set_iptreemap.c linux-2.6/net/ipv4/netfilter/ip_set_iptreemap.c
5130 --- linux-2.6./net/ipv4/netfilter/ip_set_iptreemap.c    1970-01-01 01:00:00.000000000 +0100
5131 +++ linux-2.6/net/ipv4/netfilter/ip_set_iptreemap.c     2007-08-28 13:29:12.000000000 +0200
5132 @@ -0,0 +1,829 @@
5133 +/* Copyright (C) 2007 Sven Wegener <sven.wegener@stealer.net>
5134 + *
5135 + * This program is free software; you can redistribute it and/or modify it
5136 + * under the terms of the GNU General Public License version 2 as published by
5137 + * the Free Software Foundation.
5138 + */
5139 +
5140 +/* This modules implements the iptreemap ipset type. It uses bitmaps to
5141 + * represent every single IPv4 address as a single bit. The bitmaps are managed
5142 + * in a tree structure, where the first three octets of an addresses are used
5143 + * as an index to find the bitmap and the last octet is used as the bit number.
5144 + */
5145 +
5146 +#include <linux/version.h>
5147 +#include <linux/module.h>
5148 +#include <linux/ip.h>
5149 +#include <linux/skbuff.h>
5150 +#include <linux/slab.h>
5151 +#include <linux/delay.h>
5152 +#include <linux/netfilter_ipv4/ip_tables.h>
5153 +#include <linux/netfilter_ipv4/ip_set.h>
5154 +#include <linux/errno.h>
5155 +#include <asm/uaccess.h>
5156 +#include <asm/bitops.h>
5157 +#include <linux/spinlock.h>
5158 +
5159 +#include <linux/netfilter_ipv4/ip_set_iptreemap.h>
5160 +
5161 +#define IPTREEMAP_DEFAULT_GC_TIME (5 * 60)
5162 +#define IPTREEMAP_DESTROY_SLEEP (100)
5163 +
5164 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
5165 +static struct kmem_cache *cachep_b;
5166 +static struct kmem_cache *cachep_c;
5167 +static struct kmem_cache *cachep_d;
5168 +#else
5169 +static kmem_cache_t *cachep_b;
5170 +static kmem_cache_t *cachep_c;
5171 +static kmem_cache_t *cachep_d;
5172 +#endif
5173 +
5174 +static struct ip_set_iptreemap_d *fullbitmap_d;
5175 +static struct ip_set_iptreemap_c *fullbitmap_c;
5176 +static struct ip_set_iptreemap_b *fullbitmap_b;
5177 +
5178 +#if defined(__LITTLE_ENDIAN)
5179 +#define ABCD(a, b, c, d, addr) \
5180 +       do { \
5181 +               a = ((unsigned char *)addr)[3]; \
5182 +               b = ((unsigned char *)addr)[2]; \
5183 +               c = ((unsigned char *)addr)[1]; \
5184 +               d = ((unsigned char *)addr)[0]; \
5185 +       } while (0)
5186 +#elif defined(__BIG_ENDIAN)
5187 +#define ABCD(a,b,c,d,addrp) do {               \
5188 +       a = ((unsigned char *)addrp)[0];        \
5189 +       b = ((unsigned char *)addrp)[1];        \
5190 +       c = ((unsigned char *)addrp)[2];        \
5191 +       d = ((unsigned char *)addrp)[3];        \
5192 +} while (0)
5193 +#else
5194 +#error "Please fix asm/byteorder.h"
5195 +#endif /* __LITTLE_ENDIAN */
5196 +
5197 +#define TESTIP_WALK(map, elem, branch, full) \
5198 +       do { \
5199 +               branch = (map)->tree[elem]; \
5200 +               if (!branch) \
5201 +                       return 0; \
5202 +               else if (branch == full) \
5203 +                       return 1; \
5204 +       } while (0)
5205 +
5206 +#define ADDIP_WALK(map, elem, branch, type, cachep, full) \
5207 +       do { \
5208 +               branch = (map)->tree[elem]; \
5209 +               if (!branch) { \
5210 +                       branch = (type *) kmem_cache_alloc(cachep, GFP_ATOMIC); \
5211 +                       if (!branch) \
5212 +                               return -ENOMEM; \
5213 +                       memset(branch, 0, sizeof(*branch)); \
5214 +                       (map)->tree[elem] = branch; \
5215 +               } else if (branch == full) { \
5216 +                       return -EEXIST; \
5217 +               } \
5218 +       } while (0)
5219 +
5220 +#define ADDIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free) \
5221 +       for (a = a1; a <= a2; a++) { \
5222 +               branch = (map)->tree[a]; \
5223 +               if (branch != full) { \
5224 +                       if ((a > a1 && a < a2) || (hint)) { \
5225 +                               if (branch) \
5226 +                                       free(branch); \
5227 +                               (map)->tree[a] = full; \
5228 +                               continue; \
5229 +                       } else if (!branch) { \
5230 +                               branch = kmem_cache_alloc(cachep, GFP_ATOMIC); \
5231 +                               if (!branch) \
5232 +                                       return -ENOMEM; \
5233 +                               memset(branch, 0, sizeof(*branch)); \
5234 +                               (map)->tree[a] = branch; \
5235 +                       }
5236 +
5237 +#define ADDIP_RANGE_LOOP_END() \
5238 +               } \
5239 +       }
5240 +
5241 +#define DELIP_WALK(map, elem, branch, cachep, full, flags) \
5242 +       do { \
5243 +               branch = (map)->tree[elem]; \
5244 +               if (!branch) { \
5245 +                       return -EEXIST; \
5246 +               } else if (branch == full) { \
5247 +                       branch = kmem_cache_alloc(cachep, flags); \
5248 +                       if (!branch) \
5249 +                               return -ENOMEM; \
5250 +                       memcpy(branch, full, sizeof(*full)); \
5251 +                       (map)->tree[elem] = branch; \
5252 +               } \
5253 +       } while (0)
5254 +
5255 +#define DELIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free, flags) \
5256 +       for (a = a1; a <= a2; a++) { \
5257 +               branch = (map)->tree[a]; \
5258 +               if (branch) { \
5259 +                       if ((a > a1 && a < a2) || (hint)) { \
5260 +                               if (branch != full) \
5261 +                                       free(branch); \
5262 +                               (map)->tree[a] = NULL; \
5263 +                               continue; \
5264 +                       } else if (branch == full) { \
5265 +                               branch = kmem_cache_alloc(cachep, flags); \
5266 +                               if (!branch) \
5267 +                                       return -ENOMEM; \
5268 +                               memcpy(branch, full, sizeof(*branch)); \
5269 +                               (map)->tree[a] = branch; \
5270 +                       }
5271 +
5272 +#define DELIP_RANGE_LOOP_END() \
5273 +               } \
5274 +       }
5275 +
5276 +#define LOOP_WALK_BEGIN(map, i, branch) \
5277 +       for (i = 0; i < 256; i++) { \
5278 +               branch = (map)->tree[i]; \
5279 +               if (likely(!branch)) \
5280 +                       continue;
5281 +
5282 +#define LOOP_WALK_END() \
5283 +       }
5284 +
5285 +#define LOOP_WALK_BEGIN_GC(map, i, branch, full, cachep, count) \
5286 +       count = -256; \
5287 +       for (i = 0; i < 256; i++) { \
5288 +               branch = (map)->tree[i]; \
5289 +               if (likely(!branch)) \
5290 +                       continue; \
5291 +               count++; \
5292 +               if (branch == full) { \
5293 +                       count++; \
5294 +                       continue; \
5295 +               }
5296 +
5297 +#define LOOP_WALK_END_GC(map, i, branch, full, cachep, count) \
5298 +               if (-256 == count) { \
5299 +                       kmem_cache_free(cachep, branch); \
5300 +                       (map)->tree[i] = NULL; \
5301 +               } else if (256 == count) { \
5302 +                       kmem_cache_free(cachep, branch); \
5303 +                       (map)->tree[i] = full; \
5304 +               } \
5305 +       }
5306 +
5307 +#define LOOP_WALK_BEGIN_COUNT(map, i, branch, inrange, count) \
5308 +       for (i = 0; i < 256; i++) { \
5309 +               if (!(map)->tree[i]) { \
5310 +                       if (inrange) { \
5311 +                               count++; \
5312 +                               inrange = 0; \
5313 +                       } \
5314 +                       continue; \
5315 +               } \
5316 +               branch = (map)->tree[i];
5317 +
5318 +#define LOOP_WALK_END_COUNT() \
5319 +       }
5320 +
5321 +#define MIN(a, b) (a < b ? a : b)
5322 +#define MAX(a, b) (a > b ? a : b)
5323 +
5324 +#define GETVALUE1(a, a1, b1, r) \
5325 +       (a == a1 ? b1 : r)
5326 +
5327 +#define GETVALUE2(a, b, a1, b1, c1, r) \
5328 +       (a == a1 && b == b1 ? c1 : r)
5329 +
5330 +#define GETVALUE3(a, b, c, a1, b1, c1, d1, r) \
5331 +       (a == a1 && b == b1 && c == c1 ? d1 : r)
5332 +
5333 +#define CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2) \
5334 +       ( \
5335 +               GETVALUE1(a, a1, b1, 0) == 0 \
5336 +               && GETVALUE1(a, a2, b2, 255) == 255 \
5337 +               && c1 == 0 \
5338 +               && c2 == 255 \
5339 +               && d1 == 0 \
5340 +               && d2 == 255 \
5341 +       )
5342 +
5343 +#define CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2) \
5344 +       ( \
5345 +               GETVALUE2(a, b, a1, b1, c1, 0) == 0 \
5346 +               && GETVALUE2(a, b, a2, b2, c2, 255) == 255 \
5347 +               && d1 == 0 \
5348 +               && d2 == 255 \
5349 +       )
5350 +
5351 +#define CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2) \
5352 +       ( \
5353 +               GETVALUE3(a, b, c, a1, b1, c1, d1, 0) == 0 \
5354 +               && GETVALUE3(a, b, c, a2, b2, c2, d2, 255) == 255 \
5355 +       )
5356 +
5357 +
5358 +static inline void
5359 +free_d(struct ip_set_iptreemap_d *map)
5360 +{
5361 +       kmem_cache_free(cachep_d, map);
5362 +}
5363 +
5364 +static inline void
5365 +free_c(struct ip_set_iptreemap_c *map)
5366 +{
5367 +       struct ip_set_iptreemap_d *dtree;
5368 +       unsigned int i;
5369 +
5370 +       LOOP_WALK_BEGIN(map, i, dtree) {
5371 +               if (dtree != fullbitmap_d)
5372 +                       free_d(dtree);
5373 +       } LOOP_WALK_END();
5374 +
5375 +       kmem_cache_free(cachep_c, map);
5376 +}
5377 +
5378 +static inline void
5379 +free_b(struct ip_set_iptreemap_b *map)
5380 +{
5381 +       struct ip_set_iptreemap_c *ctree;
5382 +       unsigned int i;
5383 +
5384 +       LOOP_WALK_BEGIN(map, i, ctree) {
5385 +               if (ctree != fullbitmap_c)
5386 +                       free_c(ctree);
5387 +       } LOOP_WALK_END();
5388 +
5389 +       kmem_cache_free(cachep_b, map);
5390 +}
5391 +
5392 +static inline int
5393 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5394 +{
5395 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5396 +       struct ip_set_iptreemap_b *btree;
5397 +       struct ip_set_iptreemap_c *ctree;
5398 +       struct ip_set_iptreemap_d *dtree;
5399 +       unsigned char a, b, c, d;
5400 +
5401 +       *hash_ip = ip;
5402 +
5403 +       ABCD(a, b, c, d, hash_ip);
5404 +
5405 +       TESTIP_WALK(map, a, btree, fullbitmap_b);
5406 +       TESTIP_WALK(btree, b, ctree, fullbitmap_c);
5407 +       TESTIP_WALK(ctree, c, dtree, fullbitmap_d);
5408 +
5409 +       return !!test_bit(d, (void *) dtree->bitmap);
5410 +}
5411 +
5412 +static int
5413 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5414 +{
5415 +       struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5416 +
5417 +       if (size != sizeof(struct ip_set_req_iptreemap)) {
5418 +               ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5419 +               return -EINVAL;
5420 +       }
5421 +
5422 +       return __testip(set, req->start, hash_ip);
5423 +}
5424 +
5425 +static int
5426 +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)
5427 +{
5428 +       int res;
5429 +
5430 +       res = __testip(set, 
5431 +                      ntohl(flags[index] & IPSET_SRC 
5432 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5433 +                               ? ip_hdr(skb)->saddr 
5434 +                               : ip_hdr(skb)->daddr),
5435 +#else
5436 +                               ? skb->nh.iph->saddr 
5437 +                               : skb->nh.iph->daddr),
5438 +#endif
5439 +                      hash_ip);
5440 +
5441 +       return (res < 0 ? 0 : res);
5442 +}
5443 +
5444 +static inline int
5445 +__addip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5446 +{
5447 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5448 +       struct ip_set_iptreemap_b *btree;
5449 +       struct ip_set_iptreemap_c *ctree;
5450 +       struct ip_set_iptreemap_d *dtree;
5451 +       unsigned char a, b, c, d;
5452 +
5453 +       *hash_ip = ip;
5454 +
5455 +       ABCD(a, b, c, d, hash_ip);
5456 +
5457 +       ADDIP_WALK(map, a, btree, struct ip_set_iptreemap_b, cachep_b, fullbitmap_b);
5458 +       ADDIP_WALK(btree, b, ctree, struct ip_set_iptreemap_c, cachep_c, fullbitmap_c);
5459 +       ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreemap_d, cachep_d, fullbitmap_d);
5460 +
5461 +       if (test_and_set_bit(d, (void *) dtree->bitmap))
5462 +               return -EEXIST;
5463 +
5464 +       set_bit(b, (void *) btree->dirty);
5465 +
5466 +       return 0;
5467 +}
5468 +
5469 +static inline int
5470 +__addip_range(struct ip_set *set, ip_set_ip_t start, ip_set_ip_t end, ip_set_ip_t *hash_ip)
5471 +{
5472 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5473 +       struct ip_set_iptreemap_b *btree;
5474 +       struct ip_set_iptreemap_c *ctree;
5475 +       struct ip_set_iptreemap_d *dtree;
5476 +       unsigned int a, b, c, d;
5477 +       unsigned char a1, b1, c1, d1;
5478 +       unsigned char a2, b2, c2, d2;
5479 +
5480 +       if (start == end)
5481 +               return __addip_single(set, start, hash_ip);
5482 +
5483 +       *hash_ip = start;
5484 +
5485 +       ABCD(a1, b1, c1, d1, &start);
5486 +       ABCD(a2, b2, c2, d2, &end);
5487 +
5488 +       /* This is sooo ugly... */
5489 +       ADDIP_RANGE_LOOP(map, a, a1, a2, CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2), btree, fullbitmap_b, cachep_b, free_b) {
5490 +               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) {
5491 +                       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) {
5492 +                               for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5493 +                                       set_bit(d, (void *) dtree->bitmap);
5494 +                               set_bit(b, (void *) btree->dirty);
5495 +                       } ADDIP_RANGE_LOOP_END();
5496 +               } ADDIP_RANGE_LOOP_END();
5497 +       } ADDIP_RANGE_LOOP_END();
5498 +
5499 +       return 0;
5500 +}
5501 +
5502 +static int
5503 +addip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5504 +{
5505 +       struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5506 +
5507 +       if (size != sizeof(struct ip_set_req_iptreemap)) {
5508 +               ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5509 +               return -EINVAL;
5510 +       }
5511 +
5512 +       return __addip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip);
5513 +}
5514 +
5515 +static int
5516 +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)
5517 +{
5518 +
5519 +       return __addip_single(set,
5520 +                       ntohl(flags[index] & IPSET_SRC 
5521 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5522 +                               ? ip_hdr(skb)->saddr 
5523 +                               : ip_hdr(skb)->daddr),
5524 +#else
5525 +                               ? skb->nh.iph->saddr 
5526 +                               : skb->nh.iph->daddr),
5527 +#endif
5528 +                       hash_ip);
5529 +}
5530 +
5531 +static inline int
5532 +__delip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip, unsigned int __nocast flags)
5533 +{
5534 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5535 +       struct ip_set_iptreemap_b *btree;
5536 +       struct ip_set_iptreemap_c *ctree;
5537 +       struct ip_set_iptreemap_d *dtree;
5538 +       unsigned char a,b,c,d;
5539 +
5540 +       *hash_ip = ip;
5541 +
5542 +       ABCD(a, b, c, d, hash_ip);
5543 +
5544 +       DELIP_WALK(map, a, btree, cachep_b, fullbitmap_b, flags);
5545 +       DELIP_WALK(btree, b, ctree, cachep_c, fullbitmap_c, flags);
5546 +       DELIP_WALK(ctree, c, dtree, cachep_d, fullbitmap_d, flags);
5547 +
5548 +       if (!test_and_clear_bit(d, (void *) dtree->bitmap))
5549 +               return -EEXIST;
5550 +
5551 +       set_bit(b, (void *) btree->dirty);
5552 +
5553 +       return 0;
5554 +}
5555 +
5556 +static inline int
5557 +__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)
5558 +{
5559 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5560 +       struct ip_set_iptreemap_b *btree;
5561 +       struct ip_set_iptreemap_c *ctree;
5562 +       struct ip_set_iptreemap_d *dtree;
5563 +       unsigned int a, b, c, d;
5564 +       unsigned char a1, b1, c1, d1;
5565 +       unsigned char a2, b2, c2, d2;
5566 +
5567 +       if (start == end)
5568 +               return __delip_single(set, start, hash_ip, flags);
5569 +
5570 +       *hash_ip = start;
5571 +
5572 +       ABCD(a1, b1, c1, d1, &start);
5573 +       ABCD(a2, b2, c2, d2, &end);
5574 +
5575 +       /* This is sooo ugly... */
5576 +       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) {
5577 +               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) {
5578 +                       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) {
5579 +                               for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5580 +                                       clear_bit(d, (void *) dtree->bitmap);
5581 +                               set_bit(b, (void *) btree->dirty);
5582 +                       } DELIP_RANGE_LOOP_END();
5583 +               } DELIP_RANGE_LOOP_END();
5584 +       } DELIP_RANGE_LOOP_END();
5585 +
5586 +       return 0;
5587 +}
5588 +
5589 +static int
5590 +delip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5591 +{
5592 +       struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5593 +
5594 +       if (size != sizeof(struct ip_set_req_iptreemap)) {
5595 +               ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5596 +               return -EINVAL;
5597 +       }
5598 +
5599 +       return __delip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip, GFP_KERNEL);
5600 +}
5601 +
5602 +static int
5603 +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)
5604 +{
5605 +       return __delip_single(set, 
5606 +                       ntohl(flags[index] & IPSET_SRC 
5607 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5608 +                               ? ip_hdr(skb)->saddr 
5609 +                               : ip_hdr(skb)->daddr),
5610 +#else
5611 +                               ? skb->nh.iph->saddr 
5612 +                               : skb->nh.iph->daddr),
5613 +#endif
5614 +                       hash_ip,
5615 +                       GFP_ATOMIC);
5616 +}
5617 +
5618 +/* Check the status of the bitmap
5619 + * -1 == all bits cleared
5620 + *  1 == all bits set
5621 + *  0 == anything else
5622 + */
5623 +static inline int
5624 +bitmap_status(struct ip_set_iptreemap_d *dtree)
5625 +{
5626 +       unsigned char first = dtree->bitmap[0];
5627 +       int a;
5628 +
5629 +       for (a = 1; a < 32; a++)
5630 +               if (dtree->bitmap[a] != first)
5631 +                       return 0;
5632 +
5633 +       return (first == 0 ? -1 : (first == 255 ? 1 : 0));
5634 +}
5635 +
5636 +static void
5637 +gc(unsigned long addr)
5638 +{
5639 +       struct ip_set *set = (struct ip_set *) addr;
5640 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5641 +       struct ip_set_iptreemap_b *btree;
5642 +       struct ip_set_iptreemap_c *ctree;
5643 +       struct ip_set_iptreemap_d *dtree;
5644 +       unsigned int a, b, c;
5645 +       int i, j, k;
5646 +
5647 +       write_lock_bh(&set->lock);
5648 +
5649 +       LOOP_WALK_BEGIN_GC(map, a, btree, fullbitmap_b, cachep_b, i) {
5650 +               LOOP_WALK_BEGIN_GC(btree, b, ctree, fullbitmap_c, cachep_c, j) {
5651 +                       if (!test_and_clear_bit(b, (void *) btree->dirty))
5652 +                               continue;
5653 +                       LOOP_WALK_BEGIN_GC(ctree, c, dtree, fullbitmap_d, cachep_d, k) {
5654 +                               switch (bitmap_status(dtree)) {
5655 +                                       case -1:
5656 +                                               kmem_cache_free(cachep_d, dtree);
5657 +                                               ctree->tree[c] = NULL;
5658 +                                               k--;
5659 +                                       break;
5660 +                                       case 1:
5661 +                                               kmem_cache_free(cachep_d, dtree);
5662 +                                               ctree->tree[c] = fullbitmap_d;
5663 +                                               k++;
5664 +                                       break;
5665 +                               }
5666 +                       } LOOP_WALK_END();
5667 +               } LOOP_WALK_END_GC(btree, b, ctree, fullbitmap_c, cachep_c, k);
5668 +       } LOOP_WALK_END_GC(map, a, btree, fullbitmap_b, cachep_b, j);
5669 +
5670 +       write_unlock_bh(&set->lock);
5671 +
5672 +       map->gc.expires = jiffies + map->gc_interval * HZ;
5673 +       add_timer(&map->gc);
5674 +}
5675 +
5676 +static inline void
5677 +init_gc_timer(struct ip_set *set)
5678 +{
5679 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5680 +
5681 +       init_timer(&map->gc);
5682 +       map->gc.data = (unsigned long) set;
5683 +       map->gc.function = gc;
5684 +       map->gc.expires = jiffies + map->gc_interval * HZ;
5685 +       add_timer(&map->gc);
5686 +}
5687 +
5688 +static int create(struct ip_set *set, const void *data, size_t size)
5689 +{
5690 +       struct ip_set_req_iptreemap_create *req = (struct ip_set_req_iptreemap_create *) data;
5691 +       struct ip_set_iptreemap *map;
5692 +
5693 +       if (size != sizeof(struct ip_set_req_iptreemap_create)) {
5694 +               ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap_create), size);
5695 +               return -EINVAL;
5696 +       }
5697 +
5698 +       map = kzalloc(sizeof(*map), GFP_KERNEL);
5699 +       if (!map)
5700 +               return -ENOMEM;
5701 +
5702 +       map->gc_interval = req->gc_interval ? req->gc_interval : IPTREEMAP_DEFAULT_GC_TIME;
5703 +       set->data = map;
5704 +
5705 +       init_gc_timer(set);
5706 +
5707 +       return 0;
5708 +}
5709 +
5710 +static inline void __flush(struct ip_set_iptreemap *map)
5711 +{
5712 +       struct ip_set_iptreemap_b *btree;
5713 +       unsigned int a;
5714 +
5715 +       LOOP_WALK_BEGIN(map, a, btree);
5716 +               if (btree != fullbitmap_b)
5717 +                       free_b(btree);
5718 +       LOOP_WALK_END();
5719 +}
5720 +
5721 +static void destroy(struct ip_set *set)
5722 +{
5723 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5724 +
5725 +       while (!del_timer(&map->gc))
5726 +               msleep(IPTREEMAP_DESTROY_SLEEP);
5727 +
5728 +       __flush(map);
5729 +       kfree(map);
5730 +
5731 +       set->data = NULL;
5732 +}
5733 +
5734 +static void flush(struct ip_set *set)
5735 +{
5736 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5737 +
5738 +       while (!del_timer(&map->gc))
5739 +               msleep(IPTREEMAP_DESTROY_SLEEP);
5740 +
5741 +       __flush(map);
5742 +
5743 +       memset(map, 0, sizeof(*map));
5744 +
5745 +       init_gc_timer(set);
5746 +}
5747 +
5748 +static void list_header(const struct ip_set *set, void *data)
5749 +{
5750 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5751 +       struct ip_set_req_iptreemap_create *header = (struct ip_set_req_iptreemap_create *) data;
5752 +
5753 +       header->gc_interval = map->gc_interval;
5754 +}
5755 +
5756 +static int list_members_size(const struct ip_set *set)
5757 +{
5758 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5759 +       struct ip_set_iptreemap_b *btree;
5760 +       struct ip_set_iptreemap_c *ctree;
5761 +       struct ip_set_iptreemap_d *dtree;
5762 +       unsigned int a, b, c, d, inrange = 0, count = 0;
5763 +
5764 +       LOOP_WALK_BEGIN_COUNT(map, a, btree, inrange, count) {
5765 +               LOOP_WALK_BEGIN_COUNT(btree, b, ctree, inrange, count) {
5766 +                       LOOP_WALK_BEGIN_COUNT(ctree, c, dtree, inrange, count) {
5767 +                               for (d = 0; d < 256; d++) {
5768 +                                       if (test_bit(d, (void *) dtree->bitmap)) {
5769 +                                               inrange = 1;
5770 +                                       } else if (inrange) {
5771 +                                               count++;
5772 +                                               inrange = 0;
5773 +                                       }
5774 +                               }
5775 +                       } LOOP_WALK_END_COUNT();
5776 +               } LOOP_WALK_END_COUNT();
5777 +       } LOOP_WALK_END_COUNT();
5778 +
5779 +       if (inrange)
5780 +               count++;
5781 +
5782 +       return (count * sizeof(struct ip_set_req_iptreemap));
5783 +}
5784 +
5785 +static inline size_t add_member(void *data, size_t offset, ip_set_ip_t start, ip_set_ip_t end)
5786 +{
5787 +       struct ip_set_req_iptreemap *entry = (struct ip_set_req_iptreemap *) (data + offset);
5788 +
5789 +       entry->start = start;
5790 +       entry->end = end;
5791 +
5792 +       return sizeof(*entry);
5793 +}
5794 +
5795 +static void list_members(const struct ip_set *set, void *data)
5796 +{
5797 +       struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5798 +       struct ip_set_iptreemap_b *btree;
5799 +       struct ip_set_iptreemap_c *ctree;
5800 +       struct ip_set_iptreemap_d *dtree;
5801 +       unsigned int a, b, c, d, inrange = 0;
5802 +       size_t offset = 0;
5803 +       ip_set_ip_t start = 0, end = 0, ip;
5804 +
5805 +       LOOP_WALK_BEGIN(map, a, btree) {
5806 +               LOOP_WALK_BEGIN(btree, b, ctree) {
5807 +                       LOOP_WALK_BEGIN(ctree, c, dtree) {
5808 +                               for (d = 0; d < 256; d++) {
5809 +                                       if (test_bit(d, (void *) dtree->bitmap)) {
5810 +                                               ip = ((a << 24) | (b << 16) | (c << 8) | d);
5811 +                                               if (!inrange) {
5812 +                                                       inrange = 1;
5813 +                                                       start = ip;
5814 +                                               } else if (end < ip - 1) {
5815 +                                                       offset += add_member(data, offset, start, end);
5816 +                                                       start = ip;
5817 +                                               }
5818 +                                               end = ip;
5819 +                                       } else if (inrange) {
5820 +                                               offset += add_member(data, offset, start, end);
5821 +                                               inrange = 0;
5822 +                                       }
5823 +                               }
5824 +                       } LOOP_WALK_END();
5825 +               } LOOP_WALK_END();
5826 +       } LOOP_WALK_END();
5827 +
5828 +       if (inrange)
5829 +               add_member(data, offset, start, end);
5830 +}
5831 +
5832 +static struct ip_set_type ip_set_iptreemap = {
5833 +       .typename               = SETTYPE_NAME,
5834 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
5835 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
5836 +       .create                 = create,
5837 +       .destroy                = destroy,
5838 +       .flush                  = flush,
5839 +       .reqsize                = sizeof(struct ip_set_req_iptreemap),
5840 +       .addip                  = addip,
5841 +       .addip_kernel           = addip_kernel,
5842 +       .delip                  = delip,
5843 +       .delip_kernel           = delip_kernel,
5844 +       .testip                 = testip,
5845 +       .testip_kernel          = testip_kernel,
5846 +       .header_size            = sizeof(struct ip_set_req_iptreemap_create),
5847 +       .list_header            = list_header,
5848 +       .list_members_size      = list_members_size,
5849 +       .list_members           = list_members,
5850 +       .me                     = THIS_MODULE,
5851 +};
5852 +
5853 +MODULE_LICENSE("GPL");
5854 +MODULE_AUTHOR("Sven Wegener <sven.wegener@stealer.net>");
5855 +MODULE_DESCRIPTION("iptreemap type of IP sets");
5856 +
5857 +static int __init ip_set_iptreemap_init(void)
5858 +{
5859 +       int ret = -ENOMEM;
5860 +       int a;
5861 +
5862 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5863 +       cachep_b = kmem_cache_create("ip_set_iptreemap_b", 
5864 +                                    sizeof(struct ip_set_iptreemap_b), 
5865 +                                    0, 0, NULL);
5866 +#else
5867 +       cachep_b = kmem_cache_create("ip_set_iptreemap_b", 
5868 +                                    sizeof(struct ip_set_iptreemap_b), 
5869 +                                    0, 0, NULL, NULL);
5870 +#endif
5871 +       if (!cachep_b) {
5872 +               ip_set_printk("Unable to create ip_set_iptreemap_b slab cache");
5873 +               goto out;
5874 +       }
5875 +
5876 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5877 +       cachep_c = kmem_cache_create("ip_set_iptreemap_c", 
5878 +                                    sizeof(struct ip_set_iptreemap_c),
5879 +                                    0, 0, NULL);
5880 +#else
5881 +       cachep_c = kmem_cache_create("ip_set_iptreemap_c", 
5882 +                                    sizeof(struct ip_set_iptreemap_c),
5883 +                                    0, 0, NULL, NULL);
5884 +#endif
5885 +       if (!cachep_c) {
5886 +               ip_set_printk("Unable to create ip_set_iptreemap_c slab cache");
5887 +               goto outb;
5888 +       }
5889 +
5890 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5891 +       cachep_d = kmem_cache_create("ip_set_iptreemap_d",
5892 +                                    sizeof(struct ip_set_iptreemap_d),
5893 +                                    0, 0, NULL);
5894 +#else
5895 +       cachep_d = kmem_cache_create("ip_set_iptreemap_d",
5896 +                                    sizeof(struct ip_set_iptreemap_d),
5897 +                                    0, 0, NULL, NULL);
5898 +#endif
5899 +       if (!cachep_d) {
5900 +               ip_set_printk("Unable to create ip_set_iptreemap_d slab cache");
5901 +               goto outc;
5902 +       }
5903 +
5904 +       fullbitmap_d = kmem_cache_alloc(cachep_d, GFP_KERNEL);
5905 +       if (!fullbitmap_d)
5906 +               goto outd;
5907 +
5908 +       fullbitmap_c = kmem_cache_alloc(cachep_c, GFP_KERNEL);
5909 +       if (!fullbitmap_c)
5910 +               goto outbitmapd;
5911 +
5912 +       fullbitmap_b = kmem_cache_alloc(cachep_b, GFP_KERNEL);
5913 +       if (!fullbitmap_b)
5914 +               goto outbitmapc;
5915 +
5916 +       ret = ip_set_register_set_type(&ip_set_iptreemap);
5917 +       if (0 > ret)
5918 +               goto outbitmapb;
5919 +
5920 +       /* Now init our global bitmaps */
5921 +       memset(fullbitmap_d->bitmap, 0xff, sizeof(fullbitmap_d->bitmap));
5922 +
5923 +       for (a = 0; a < 256; a++)
5924 +               fullbitmap_c->tree[a] = fullbitmap_d;
5925 +
5926 +       for (a = 0; a < 256; a++)
5927 +               fullbitmap_b->tree[a] = fullbitmap_c;
5928 +       memset(fullbitmap_b->dirty, 0, sizeof(fullbitmap_b->dirty));
5929 +
5930 +       return 0;
5931 +
5932 +outbitmapb:
5933 +       kmem_cache_free(cachep_b, fullbitmap_b);
5934 +outbitmapc:
5935 +       kmem_cache_free(cachep_c, fullbitmap_c);
5936 +outbitmapd:
5937 +       kmem_cache_free(cachep_d, fullbitmap_d);
5938 +outd:
5939 +       kmem_cache_destroy(cachep_d);
5940 +outc:
5941 +       kmem_cache_destroy(cachep_c);
5942 +outb:
5943 +       kmem_cache_destroy(cachep_b);
5944 +out:
5945 +
5946 +       return ret;
5947 +}
5948 +
5949 +static void __exit ip_set_iptreemap_fini(void)
5950 +{
5951 +       ip_set_unregister_set_type(&ip_set_iptreemap);
5952 +       kmem_cache_free(cachep_d, fullbitmap_d);
5953 +       kmem_cache_free(cachep_c, fullbitmap_c);
5954 +       kmem_cache_free(cachep_b, fullbitmap_b);
5955 +       kmem_cache_destroy(cachep_d);
5956 +       kmem_cache_destroy(cachep_c);
5957 +       kmem_cache_destroy(cachep_b);
5958 +}
5959 +
5960 +module_init(ip_set_iptreemap_init);
5961 +module_exit(ip_set_iptreemap_fini);
5962 diff -uNrp linux-2.6./net/ipv4/netfilter/ip_set_macipmap.c linux-2.6/net/ipv4/netfilter/ip_set_macipmap.c
5963 --- linux-2.6./net/ipv4/netfilter/ip_set_macipmap.c     1970-01-01 01:00:00.000000000 +0100
5964 +++ linux-2.6/net/ipv4/netfilter/ip_set_macipmap.c      2007-08-28 13:29:12.000000000 +0200
5965 @@ -0,0 +1,375 @@
5966 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5967 + *                         Patrick Schaaf <bof@bof.de>
5968 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
5969 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5970 + *
5971 + * This program is free software; you can redistribute it and/or modify
5972 + * it under the terms of the GNU General Public License version 2 as
5973 + * published by the Free Software Foundation.  
5974 + */
5975 +
5976 +/* Kernel module implementing an IP set type: the macipmap type */
5977 +
5978 +#include <linux/module.h>
5979 +#include <linux/ip.h>
5980 +#include <linux/skbuff.h>
5981 +#include <linux/version.h>
5982 +#include <linux/netfilter_ipv4/ip_tables.h>
5983 +#include <linux/netfilter_ipv4/ip_set.h>
5984 +#include <linux/errno.h>
5985 +#include <asm/uaccess.h>
5986 +#include <asm/bitops.h>
5987 +#include <linux/spinlock.h>
5988 +#include <linux/if_ether.h>
5989 +#include <linux/vmalloc.h>
5990 +
5991 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
5992 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
5993 +
5994 +static int
5995 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5996 +{
5997 +       struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
5998 +       struct ip_set_macip *table = (struct ip_set_macip *) map->members;      
5999 +       struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
6000 +
6001 +       if (size != sizeof(struct ip_set_req_macipmap)) {
6002 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6003 +                             sizeof(struct ip_set_req_macipmap),
6004 +                             size);
6005 +               return -EINVAL;
6006 +       }
6007 +
6008 +       if (req->ip < map->first_ip || req->ip > map->last_ip)
6009 +               return -ERANGE;
6010 +
6011 +       *hash_ip = req->ip;
6012 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
6013 +          set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));             
6014 +       if (test_bit(IPSET_MACIP_ISSET,
6015 +                    (void *) &table[req->ip - map->first_ip].flags)) {
6016 +               return (memcmp(req->ethernet,
6017 +                              &table[req->ip - map->first_ip].ethernet,
6018 +                              ETH_ALEN) == 0);
6019 +       } else {
6020 +               return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6021 +       }
6022 +}
6023 +
6024 +static int
6025 +testip_kernel(struct ip_set *set, 
6026 +             const struct sk_buff *skb,
6027 +             ip_set_ip_t *hash_ip,
6028 +             const u_int32_t *flags,
6029 +             unsigned char index)
6030 +{
6031 +       struct ip_set_macipmap *map =
6032 +           (struct ip_set_macipmap *) set->data;
6033 +       struct ip_set_macip *table =
6034 +           (struct ip_set_macip *) map->members;
6035 +       ip_set_ip_t ip;
6036 +       
6037 +       ip = ntohl(flags[index] & IPSET_SRC
6038 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6039 +                       ? ip_hdr(skb)->saddr 
6040 +                       : ip_hdr(skb)->daddr);
6041 +#else
6042 +                       ? skb->nh.iph->saddr
6043 +                       : skb->nh.iph->daddr);
6044 +#endif
6045 +
6046 +       if (ip < map->first_ip || ip > map->last_ip)
6047 +               return 0;
6048 +
6049 +       *hash_ip = ip;  
6050 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
6051 +          set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));          
6052 +       if (test_bit(IPSET_MACIP_ISSET,
6053 +           (void *) &table[ip - map->first_ip].flags)) {
6054 +               /* Is mac pointer valid?
6055 +                * If so, compare... */
6056 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6057 +               return (skb_mac_header(skb) >= skb->head
6058 +                       && (skb_mac_header(skb) + ETH_HLEN) <= skb->data
6059 +#else
6060 +               return (skb->mac.raw >= skb->head
6061 +                       && (skb->mac.raw + ETH_HLEN) <= skb->data
6062 +#endif
6063 +                       && (memcmp(eth_hdr(skb)->h_source,
6064 +                                  &table[ip - map->first_ip].ethernet,
6065 +                                  ETH_ALEN) == 0));
6066 +       } else {
6067 +               return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6068 +       }
6069 +}
6070 +
6071 +/* returns 0 on success */
6072 +static inline int
6073 +__addip(struct ip_set *set, 
6074 +       ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
6075 +{
6076 +       struct ip_set_macipmap *map =
6077 +           (struct ip_set_macipmap *) set->data;
6078 +       struct ip_set_macip *table =
6079 +           (struct ip_set_macip *) map->members;
6080 +
6081 +       if (ip < map->first_ip || ip > map->last_ip)
6082 +               return -ERANGE;
6083 +       if (test_and_set_bit(IPSET_MACIP_ISSET, 
6084 +                            (void *) &table[ip - map->first_ip].flags))
6085 +               return -EEXIST;
6086 +
6087 +       *hash_ip = ip;
6088 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6089 +       memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
6090 +       return 0;
6091 +}
6092 +
6093 +static int
6094 +addip(struct ip_set *set, const void *data, size_t size,
6095 +      ip_set_ip_t *hash_ip)
6096 +{
6097 +       struct ip_set_req_macipmap *req =
6098 +           (struct ip_set_req_macipmap *) data;
6099 +
6100 +       if (size != sizeof(struct ip_set_req_macipmap)) {
6101 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6102 +                             sizeof(struct ip_set_req_macipmap),
6103 +                             size);
6104 +               return -EINVAL;
6105 +       }
6106 +       return __addip(set, req->ip, req->ethernet, hash_ip);
6107 +}
6108 +
6109 +static int
6110 +addip_kernel(struct ip_set *set, 
6111 +            const struct sk_buff *skb,
6112 +            ip_set_ip_t *hash_ip,
6113 +            const u_int32_t *flags,
6114 +            unsigned char index)
6115 +{
6116 +       ip_set_ip_t ip;
6117 +       
6118 +       ip = ntohl(flags[index] & IPSET_SRC
6119 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6120 +                       ? ip_hdr(skb)->saddr 
6121 +                       : ip_hdr(skb)->daddr);
6122 +#else
6123 +                       ? skb->nh.iph->saddr
6124 +                       : skb->nh.iph->daddr);
6125 +#endif
6126 +
6127 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6128 +       if (!(skb_mac_header(skb) >= skb->head
6129 +             && (skb_mac_header(skb) + ETH_HLEN) <= skb->data))
6130 +#else
6131 +       if (!(skb->mac.raw >= skb->head
6132 +             && (skb->mac.raw + ETH_HLEN) <= skb->data))
6133 +#endif
6134 +               return -EINVAL;
6135 +
6136 +       return __addip(set, ip, eth_hdr(skb)->h_source, hash_ip);
6137 +}
6138 +
6139 +static inline int
6140 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6141 +{
6142 +       struct ip_set_macipmap *map =
6143 +           (struct ip_set_macipmap *) set->data;
6144 +       struct ip_set_macip *table =
6145 +           (struct ip_set_macip *) map->members;
6146 +
6147 +       if (ip < map->first_ip || ip > map->last_ip)
6148 +               return -ERANGE;
6149 +       if (!test_and_clear_bit(IPSET_MACIP_ISSET, 
6150 +                               (void *)&table[ip - map->first_ip].flags))
6151 +               return -EEXIST;
6152 +
6153 +       *hash_ip = ip;
6154 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6155 +       return 0;
6156 +}
6157 +
6158 +static int
6159 +delip(struct ip_set *set, const void *data, size_t size,
6160 +     ip_set_ip_t *hash_ip)
6161 +{
6162 +       struct ip_set_req_macipmap *req =
6163 +           (struct ip_set_req_macipmap *) data;
6164 +
6165 +       if (size != sizeof(struct ip_set_req_macipmap)) {
6166 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6167 +                             sizeof(struct ip_set_req_macipmap),
6168 +                             size);
6169 +               return -EINVAL;
6170 +       }
6171 +       return __delip(set, req->ip, hash_ip);
6172 +}
6173 +
6174 +static int
6175 +delip_kernel(struct ip_set *set,
6176 +            const struct sk_buff *skb,
6177 +            ip_set_ip_t *hash_ip,
6178 +            const u_int32_t *flags,
6179 +            unsigned char index)
6180 +{
6181 +       return __delip(set,
6182 +                      ntohl(flags[index] & IPSET_SRC 
6183 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6184 +                               ? ip_hdr(skb)->saddr 
6185 +                               : ip_hdr(skb)->daddr),
6186 +#else
6187 +                               ? skb->nh.iph->saddr 
6188 +                               : skb->nh.iph->daddr),
6189 +#endif
6190 +                      hash_ip);
6191 +}
6192 +
6193 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
6194 +{
6195 +       return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
6196 +}
6197 +
6198 +static int create(struct ip_set *set, const void *data, size_t size)
6199 +{
6200 +       int newbytes;
6201 +       struct ip_set_req_macipmap_create *req =
6202 +           (struct ip_set_req_macipmap_create *) data;
6203 +       struct ip_set_macipmap *map;
6204 +
6205 +       if (size != sizeof(struct ip_set_req_macipmap_create)) {
6206 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6207 +                             sizeof(struct ip_set_req_macipmap_create),
6208 +                             size);
6209 +               return -EINVAL;
6210 +       }
6211 +
6212 +       DP("from %u.%u.%u.%u to %u.%u.%u.%u",
6213 +          HIPQUAD(req->from), HIPQUAD(req->to));
6214 +
6215 +       if (req->from > req->to) {
6216 +               DP("bad ip range");
6217 +               return -ENOEXEC;
6218 +       }
6219 +
6220 +       if (req->to - req->from > MAX_RANGE) {
6221 +               ip_set_printk("range too big (max %d addresses)",
6222 +                              MAX_RANGE+1);
6223 +               return -ENOEXEC;
6224 +       }
6225 +
6226 +       map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
6227 +       if (!map) {
6228 +               DP("out of memory for %d bytes",
6229 +                  sizeof(struct ip_set_macipmap));
6230 +               return -ENOMEM;
6231 +       }
6232 +       map->flags = req->flags;
6233 +       map->first_ip = req->from;
6234 +       map->last_ip = req->to;
6235 +       newbytes = members_size(map->first_ip, map->last_ip);
6236 +       map->members = ip_set_malloc(newbytes);
6237 +       DP("members: %u %p", newbytes, map->members);
6238 +       if (!map->members) {
6239 +               DP("out of memory for %d bytes", newbytes);
6240 +               kfree(map);
6241 +               return -ENOMEM;
6242 +       }
6243 +       memset(map->members, 0, newbytes);
6244 +       
6245 +       set->data = map;
6246 +       return 0;
6247 +}
6248 +
6249 +static void destroy(struct ip_set *set)
6250 +{
6251 +       struct ip_set_macipmap *map =
6252 +           (struct ip_set_macipmap *) set->data;
6253 +
6254 +       ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
6255 +       kfree(map);
6256 +
6257 +       set->data = NULL;
6258 +}
6259 +
6260 +static void flush(struct ip_set *set)
6261 +{
6262 +       struct ip_set_macipmap *map =
6263 +           (struct ip_set_macipmap *) set->data;
6264 +       memset(map->members, 0, members_size(map->first_ip, map->last_ip));
6265 +}
6266 +
6267 +static void list_header(const struct ip_set *set, void *data)
6268 +{
6269 +       struct ip_set_macipmap *map =
6270 +           (struct ip_set_macipmap *) set->data;
6271 +       struct ip_set_req_macipmap_create *header =
6272 +           (struct ip_set_req_macipmap_create *) data;
6273 +
6274 +       DP("list_header %x %x %u", map->first_ip, map->last_ip,
6275 +          map->flags);
6276 +
6277 +       header->from = map->first_ip;
6278 +       header->to = map->last_ip;
6279 +       header->flags = map->flags;
6280 +}
6281 +
6282 +static int list_members_size(const struct ip_set *set)
6283 +{
6284 +       struct ip_set_macipmap *map =
6285 +           (struct ip_set_macipmap *) set->data;
6286 +
6287 +       DP("%u", members_size(map->first_ip, map->last_ip));
6288 +       return members_size(map->first_ip, map->last_ip);
6289 +}
6290 +
6291 +static void list_members(const struct ip_set *set, void *data)
6292 +{
6293 +       struct ip_set_macipmap *map =
6294 +           (struct ip_set_macipmap *) set->data;
6295 +
6296 +       int bytes = members_size(map->first_ip, map->last_ip);
6297 +
6298 +       DP("members: %u %p", bytes, map->members);
6299 +       memcpy(data, map->members, bytes);
6300 +}
6301 +
6302 +static struct ip_set_type ip_set_macipmap = {
6303 +       .typename               = SETTYPE_NAME,
6304 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6305 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
6306 +       .create                 = &create,
6307 +       .destroy                = &destroy,
6308 +       .flush                  = &flush,
6309 +       .reqsize                = sizeof(struct ip_set_req_macipmap),
6310 +       .addip                  = &addip,
6311 +       .addip_kernel           = &addip_kernel,
6312 +       .delip                  = &delip,
6313 +       .delip_kernel           = &delip_kernel,
6314 +       .testip                 = &testip,
6315 +       .testip_kernel          = &testip_kernel,
6316 +       .header_size            = sizeof(struct ip_set_req_macipmap_create),
6317 +       .list_header            = &list_header,
6318 +       .list_members_size      = &list_members_size,
6319 +       .list_members           = &list_members,
6320 +       .me                     = THIS_MODULE,
6321 +};
6322 +
6323 +MODULE_LICENSE("GPL");
6324 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6325 +MODULE_DESCRIPTION("macipmap type of IP sets");
6326 +
6327 +static int __init ip_set_macipmap_init(void)
6328 +{
6329 +       init_max_malloc_size();
6330 +       return ip_set_register_set_type(&ip_set_macipmap);
6331 +}
6332 +
6333 +static void __exit ip_set_macipmap_fini(void)
6334 +{
6335 +       /* FIXME: possible race with ip_set_create() */
6336 +       ip_set_unregister_set_type(&ip_set_macipmap);
6337 +}
6338 +
6339 +module_init(ip_set_macipmap_init);
6340 +module_exit(ip_set_macipmap_fini);
6341 diff -uNrp linux-2.6./net/ipv4/netfilter/ip_set_nethash.c linux-2.6/net/ipv4/netfilter/ip_set_nethash.c
6342 --- linux-2.6./net/ipv4/netfilter/ip_set_nethash.c      1970-01-01 01:00:00.000000000 +0100
6343 +++ linux-2.6/net/ipv4/netfilter/ip_set_nethash.c       2007-08-28 13:29:12.000000000 +0200
6344 @@ -0,0 +1,497 @@
6345 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6346 + *
6347 + * This program is free software; you can redistribute it and/or modify
6348 + * it under the terms of the GNU General Public License version 2 as
6349 + * published by the Free Software Foundation.  
6350 + */
6351 +
6352 +/* Kernel module implementing a cidr nethash set */
6353 +
6354 +#include <linux/module.h>
6355 +#include <linux/ip.h>
6356 +#include <linux/skbuff.h>
6357 +#include <linux/version.h>
6358 +#include <linux/jhash.h>
6359 +#include <linux/netfilter_ipv4/ip_tables.h>
6360 +#include <linux/netfilter_ipv4/ip_set.h>
6361 +#include <linux/errno.h>
6362 +#include <asm/uaccess.h>
6363 +#include <asm/bitops.h>
6364 +#include <linux/spinlock.h>
6365 +#include <linux/vmalloc.h>
6366 +#include <linux/random.h>
6367 +
6368 +#include <net/ip.h>
6369 +
6370 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
6371 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
6372 +
6373 +static int limit = MAX_RANGE;
6374 +
6375 +static inline __u32
6376 +jhash_ip(const struct ip_set_nethash *map, uint16_t i, ip_set_ip_t ip)
6377 +{
6378 +       return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
6379 +}
6380 +
6381 +static inline __u32
6382 +hash_id_cidr(struct ip_set_nethash *map,
6383 +            ip_set_ip_t ip,
6384 +            unsigned char cidr,
6385 +            ip_set_ip_t *hash_ip)
6386 +{
6387 +       __u32 id;
6388 +       u_int16_t i;
6389 +       ip_set_ip_t *elem;
6390 +
6391 +       *hash_ip = pack(ip, cidr);
6392 +       
6393 +       for (i = 0; i < map->probes; i++) {
6394 +               id = jhash_ip(map, i, *hash_ip) % map->hashsize;
6395 +               DP("hash key: %u", id);
6396 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6397 +               if (*elem == *hash_ip)
6398 +                       return id;
6399 +       }
6400 +       return UINT_MAX;
6401 +}
6402 +
6403 +static inline __u32
6404 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6405 +{
6406 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6407 +       __u32 id = UINT_MAX;
6408 +       int i;
6409 +
6410 +       for (i = 0; i < 30 && map->cidr[i]; i++) {
6411 +               id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
6412 +               if (id != UINT_MAX)
6413 +                       break;
6414 +       }
6415 +       return id;
6416 +}
6417 +
6418 +static inline int
6419 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
6420 +             ip_set_ip_t *hash_ip)
6421 +{
6422 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6423 +
6424 +       return (ip && hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
6425 +}
6426 +
6427 +static inline int
6428 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6429 +{
6430 +       return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
6431 +}
6432 +
6433 +static int
6434 +testip(struct ip_set *set, const void *data, size_t size,
6435 +       ip_set_ip_t *hash_ip)
6436 +{
6437 +       struct ip_set_req_nethash *req = 
6438 +           (struct ip_set_req_nethash *) data;
6439 +
6440 +       if (size != sizeof(struct ip_set_req_nethash)) {
6441 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6442 +                             sizeof(struct ip_set_req_nethash),
6443 +                             size);
6444 +               return -EINVAL;
6445 +       }
6446 +       return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
6447 +               : __testip_cidr(set, req->ip, req->cidr, hash_ip));
6448 +}
6449 +
6450 +static int
6451 +testip_kernel(struct ip_set *set, 
6452 +             const struct sk_buff *skb,
6453 +             ip_set_ip_t *hash_ip,
6454 +             const u_int32_t *flags,
6455 +             unsigned char index)
6456 +{
6457 +       return __testip(set,
6458 +                       ntohl(flags[index] & IPSET_SRC 
6459 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6460 +                               ? ip_hdr(skb)->saddr 
6461 +                               : ip_hdr(skb)->daddr),
6462 +#else
6463 +                               ? skb->nh.iph->saddr 
6464 +                               : skb->nh.iph->daddr),
6465 +#endif
6466 +                       hash_ip);
6467 +}
6468 +
6469 +static inline int
6470 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
6471 +{
6472 +       __u32 probe;
6473 +       u_int16_t i;
6474 +       ip_set_ip_t *elem;
6475 +       
6476 +       for (i = 0; i < map->probes; i++) {
6477 +               probe = jhash_ip(map, i, ip) % map->hashsize;
6478 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
6479 +               if (*elem == ip)
6480 +                       return -EEXIST;
6481 +               if (!*elem) {
6482 +                       *elem = ip;
6483 +                       map->elements++;
6484 +                       return 0;
6485 +               }
6486 +       }
6487 +       /* Trigger rehashing */
6488 +       return -EAGAIN;
6489 +}
6490 +
6491 +static inline int
6492 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6493 +       ip_set_ip_t *hash_ip)
6494 +{
6495 +       if (!ip || map->elements >= limit)
6496 +               return -ERANGE;
6497 +       
6498 +       *hash_ip = pack(ip, cidr);
6499 +       DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
6500 +       
6501 +       return __addip_base(map, *hash_ip);
6502 +}
6503 +
6504 +static void
6505 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
6506 +{
6507 +       unsigned char next;
6508 +       int i;
6509 +       
6510 +       for (i = 0; i < 30 && map->cidr[i]; i++) {
6511 +               if (map->cidr[i] == cidr) {
6512 +                       return;
6513 +               } else if (map->cidr[i] < cidr) {
6514 +                       next = map->cidr[i];
6515 +                       map->cidr[i] = cidr;
6516 +                       cidr = next;
6517 +               }
6518 +       }
6519 +       if (i < 30)
6520 +               map->cidr[i] = cidr;
6521 +}
6522 +
6523 +static int
6524 +addip(struct ip_set *set, const void *data, size_t size,
6525 +        ip_set_ip_t *hash_ip)
6526 +{
6527 +       struct ip_set_req_nethash *req = 
6528 +           (struct ip_set_req_nethash *) data;
6529 +       int ret;
6530 +
6531 +       if (size != sizeof(struct ip_set_req_nethash)) {
6532 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6533 +                             sizeof(struct ip_set_req_nethash),
6534 +                             size);
6535 +               return -EINVAL;
6536 +       }
6537 +       ret = __addip((struct ip_set_nethash *) set->data, 
6538 +                     req->ip, req->cidr, hash_ip);
6539 +       
6540 +       if (ret == 0)
6541 +               update_cidr_sizes((struct ip_set_nethash *) set->data,
6542 +                                 req->cidr);
6543 +       
6544 +       return ret;
6545 +}
6546 +
6547 +static int
6548 +addip_kernel(struct ip_set *set, 
6549 +            const struct sk_buff *skb,
6550 +            ip_set_ip_t *hash_ip,
6551 +            const u_int32_t *flags,
6552 +            unsigned char index)
6553 +{
6554 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6555 +       int ret = -ERANGE;
6556 +       ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC 
6557 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6558 +                                       ? ip_hdr(skb)->saddr 
6559 +                                       : ip_hdr(skb)->daddr);
6560 +#else
6561 +                                       ? skb->nh.iph->saddr
6562 +                                       : skb->nh.iph->daddr);
6563 +#endif
6564 +       
6565 +       if (map->cidr[0])
6566 +               ret = __addip(map, ip, map->cidr[0], hash_ip);
6567 +               
6568 +       return ret;
6569 +}
6570 +
6571 +static int retry(struct ip_set *set)
6572 +{
6573 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6574 +       ip_set_ip_t *elem;
6575 +       void *members;
6576 +       u_int32_t i, hashsize = map->hashsize;
6577 +       int res;
6578 +       struct ip_set_nethash *tmp;
6579 +       
6580 +       if (map->resize == 0)
6581 +               return -ERANGE;
6582 +
6583 +    again:
6584 +       res = 0;
6585 +       
6586 +       /* Calculate new parameters */
6587 +       hashsize += (hashsize * map->resize)/100;
6588 +       if (hashsize == map->hashsize)
6589 +               hashsize++;
6590 +       
6591 +       ip_set_printk("rehashing of set %s triggered: "
6592 +                     "hashsize grows from %u to %u",
6593 +                     set->name, map->hashsize, hashsize);
6594 +
6595 +       tmp = kmalloc(sizeof(struct ip_set_nethash) 
6596 +                     + map->probes * sizeof(uint32_t), GFP_ATOMIC);
6597 +       if (!tmp) {
6598 +               DP("out of memory for %d bytes",
6599 +                  sizeof(struct ip_set_nethash)
6600 +                  + map->probes * sizeof(uint32_t));
6601 +               return -ENOMEM;
6602 +       }
6603 +       tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
6604 +       if (!tmp->members) {
6605 +               DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
6606 +               kfree(tmp);
6607 +               return -ENOMEM;
6608 +       }
6609 +       tmp->hashsize = hashsize;
6610 +       tmp->elements = 0;
6611 +       tmp->probes = map->probes;
6612 +       tmp->resize = map->resize;
6613 +       memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
6614 +       memcpy(tmp->cidr, map->cidr, 30 * sizeof(unsigned char));
6615 +       
6616 +       write_lock_bh(&set->lock);
6617 +       map = (struct ip_set_nethash *) set->data; /* Play safe */
6618 +       for (i = 0; i < map->hashsize && res == 0; i++) {
6619 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);     
6620 +               if (*elem)
6621 +                       res = __addip_base(tmp, *elem);
6622 +       }
6623 +       if (res) {
6624 +               /* Failure, try again */
6625 +               write_unlock_bh(&set->lock);
6626 +               harray_free(tmp->members);
6627 +               kfree(tmp);
6628 +               goto again;
6629 +       }
6630 +       
6631 +       /* Success at resizing! */
6632 +       members = map->members;
6633 +       
6634 +       map->hashsize = tmp->hashsize;
6635 +       map->members = tmp->members;
6636 +       write_unlock_bh(&set->lock);
6637 +
6638 +       harray_free(members);
6639 +       kfree(tmp);
6640 +
6641 +       return 0;
6642 +}
6643 +
6644 +static inline int
6645 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6646 +       ip_set_ip_t *hash_ip)
6647 +{
6648 +       ip_set_ip_t id, *elem;
6649 +
6650 +       if (!ip)
6651 +               return -ERANGE;
6652 +       
6653 +       id = hash_id_cidr(map, ip, cidr, hash_ip);
6654 +       if (id == UINT_MAX)
6655 +               return -EEXIST;
6656 +               
6657 +       elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6658 +       *elem = 0;
6659 +       map->elements--;
6660 +       return 0;
6661 +}
6662 +
6663 +static int
6664 +delip(struct ip_set *set, const void *data, size_t size,
6665 +        ip_set_ip_t *hash_ip)
6666 +{
6667 +       struct ip_set_req_nethash *req =
6668 +           (struct ip_set_req_nethash *) data;
6669 +
6670 +       if (size != sizeof(struct ip_set_req_nethash)) {
6671 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6672 +                             sizeof(struct ip_set_req_nethash),
6673 +                             size);
6674 +               return -EINVAL;
6675 +       }
6676 +       /* TODO: no garbage collection in map->cidr */          
6677 +       return __delip((struct ip_set_nethash *) set->data, 
6678 +                      req->ip, req->cidr, hash_ip);
6679 +}
6680 +
6681 +static int
6682 +delip_kernel(struct ip_set *set, 
6683 +            const struct sk_buff *skb,
6684 +            ip_set_ip_t *hash_ip,
6685 +            const u_int32_t *flags,
6686 +            unsigned char index)
6687 +{
6688 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6689 +       int ret = -ERANGE;
6690 +       ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC 
6691 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6692 +                                       ? ip_hdr(skb)->saddr 
6693 +                                       : ip_hdr(skb)->daddr);
6694 +#else
6695 +                                       ? skb->nh.iph->saddr
6696 +                                       : skb->nh.iph->daddr);
6697 +#endif
6698 +       
6699 +       if (map->cidr[0])
6700 +               ret = __delip(map, ip, map->cidr[0], hash_ip);
6701 +       
6702 +       return ret;
6703 +}
6704 +
6705 +static int create(struct ip_set *set, const void *data, size_t size)
6706 +{
6707 +       struct ip_set_req_nethash_create *req =
6708 +           (struct ip_set_req_nethash_create *) data;
6709 +       struct ip_set_nethash *map;
6710 +       uint16_t i;
6711 +
6712 +       if (size != sizeof(struct ip_set_req_nethash_create)) {
6713 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6714 +                              sizeof(struct ip_set_req_nethash_create),
6715 +                              size);
6716 +               return -EINVAL;
6717 +       }
6718 +
6719 +       if (req->hashsize < 1) {
6720 +               ip_set_printk("hashsize too small");
6721 +               return -ENOEXEC;
6722 +       }
6723 +       if (req->probes < 1) {
6724 +               ip_set_printk("probes too small");
6725 +               return -ENOEXEC;
6726 +       }
6727 +
6728 +       map = kmalloc(sizeof(struct ip_set_nethash)
6729 +                     + req->probes * sizeof(uint32_t), GFP_KERNEL);
6730 +       if (!map) {
6731 +               DP("out of memory for %d bytes",
6732 +                  sizeof(struct ip_set_nethash)
6733 +                  + req->probes * sizeof(uint32_t));
6734 +               return -ENOMEM;
6735 +       }
6736 +       for (i = 0; i < req->probes; i++)
6737 +               get_random_bytes(((uint32_t *) map->initval)+i, 4);
6738 +       map->elements = 0;
6739 +       map->hashsize = req->hashsize;
6740 +       map->probes = req->probes;
6741 +       map->resize = req->resize;
6742 +       memset(map->cidr, 0, 30 * sizeof(unsigned char));
6743 +       map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
6744 +       if (!map->members) {
6745 +               DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
6746 +               kfree(map);
6747 +               return -ENOMEM;
6748 +       }
6749 +       
6750 +       set->data = map;
6751 +       return 0;
6752 +}
6753 +
6754 +static void destroy(struct ip_set *set)
6755 +{
6756 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6757 +
6758 +       harray_free(map->members);
6759 +       kfree(map);
6760 +
6761 +       set->data = NULL;
6762 +}
6763 +
6764 +static void flush(struct ip_set *set)
6765 +{
6766 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6767 +       harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
6768 +       memset(map->cidr, 0, 30 * sizeof(unsigned char));
6769 +       map->elements = 0;
6770 +}
6771 +
6772 +static void list_header(const struct ip_set *set, void *data)
6773 +{
6774 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6775 +       struct ip_set_req_nethash_create *header =
6776 +           (struct ip_set_req_nethash_create *) data;
6777 +
6778 +       header->hashsize = map->hashsize;
6779 +       header->probes = map->probes;
6780 +       header->resize = map->resize;
6781 +}
6782 +
6783 +static int list_members_size(const struct ip_set *set)
6784 +{
6785 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6786 +
6787 +       return (map->hashsize * sizeof(ip_set_ip_t));
6788 +}
6789 +
6790 +static void list_members(const struct ip_set *set, void *data)
6791 +{
6792 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6793 +       ip_set_ip_t i, *elem;
6794 +
6795 +       for (i = 0; i < map->hashsize; i++) {
6796 +               elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);     
6797 +               ((ip_set_ip_t *)data)[i] = *elem;
6798 +       }
6799 +}
6800 +
6801 +static struct ip_set_type ip_set_nethash = {
6802 +       .typename               = SETTYPE_NAME,
6803 +       .features               = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6804 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
6805 +       .create                 = &create,
6806 +       .destroy                = &destroy,
6807 +       .flush                  = &flush,
6808 +       .reqsize                = sizeof(struct ip_set_req_nethash),
6809 +       .addip                  = &addip,
6810 +       .addip_kernel           = &addip_kernel,
6811 +       .retry                  = &retry,
6812 +       .delip                  = &delip,
6813 +       .delip_kernel           = &delip_kernel,
6814 +       .testip                 = &testip,
6815 +       .testip_kernel          = &testip_kernel,
6816 +       .header_size            = sizeof(struct ip_set_req_nethash_create),
6817 +       .list_header            = &list_header,
6818 +       .list_members_size      = &list_members_size,
6819 +       .list_members           = &list_members,
6820 +       .me                     = THIS_MODULE,
6821 +};
6822 +
6823 +MODULE_LICENSE("GPL");
6824 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6825 +MODULE_DESCRIPTION("nethash type of IP sets");
6826 +module_param(limit, int, 0600);
6827 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
6828 +
6829 +static int __init ip_set_nethash_init(void)
6830 +{
6831 +       return ip_set_register_set_type(&ip_set_nethash);
6832 +}
6833 +
6834 +static void __exit ip_set_nethash_fini(void)
6835 +{
6836 +       /* FIXME: possible race with ip_set_create() */
6837 +       ip_set_unregister_set_type(&ip_set_nethash);
6838 +}
6839 +
6840 +module_init(ip_set_nethash_init);
6841 +module_exit(ip_set_nethash_fini);
6842 diff -uNrp linux-2.6./net/ipv4/netfilter/ip_set_portmap.c linux-2.6/net/ipv4/netfilter/ip_set_portmap.c
6843 --- linux-2.6./net/ipv4/netfilter/ip_set_portmap.c      1970-01-01 01:00:00.000000000 +0100
6844 +++ linux-2.6/net/ipv4/netfilter/ip_set_portmap.c       2007-08-28 13:29:12.000000000 +0200
6845 @@ -0,0 +1,346 @@
6846 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6847 + *
6848 + * This program is free software; you can redistribute it and/or modify
6849 + * it under the terms of the GNU General Public License version 2 as
6850 + * published by the Free Software Foundation.  
6851 + */
6852 +
6853 +/* Kernel module implementing a port set type as a bitmap */
6854 +
6855 +#include <linux/module.h>
6856 +#include <linux/ip.h>
6857 +#include <linux/tcp.h>
6858 +#include <linux/udp.h>
6859 +#include <linux/skbuff.h>
6860 +#include <linux/version.h>
6861 +#include <linux/netfilter_ipv4/ip_tables.h>
6862 +#include <linux/netfilter_ipv4/ip_set.h>
6863 +#include <linux/errno.h>
6864 +#include <asm/uaccess.h>
6865 +#include <asm/bitops.h>
6866 +#include <linux/spinlock.h>
6867 +
6868 +#include <net/ip.h>
6869 +
6870 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
6871 +
6872 +/* We must handle non-linear skbs */
6873 +static inline ip_set_ip_t
6874 +get_port(const struct sk_buff *skb, u_int32_t flags)
6875 +{
6876 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6877 +       struct iphdr *iph = ip_hdr(skb);
6878 +#else
6879 +       struct iphdr *iph = skb->nh.iph;
6880 +#endif
6881 +       u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
6882 +       switch (iph->protocol) {
6883 +       case IPPROTO_TCP: {
6884 +               struct tcphdr tcph;
6885 +               
6886 +               /* See comments at tcp_match in ip_tables.c */
6887 +               if (offset)
6888 +                       return INVALID_PORT;
6889 +
6890 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6891 +               if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
6892 +#else
6893 +               if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
6894 +#endif
6895 +                       /* No choice either */
6896 +                       return INVALID_PORT;
6897 +               
6898 +               return ntohs(flags & IPSET_SRC ?
6899 +                            tcph.source : tcph.dest);
6900 +           }
6901 +       case IPPROTO_UDP: {
6902 +               struct udphdr udph;
6903 +
6904 +               if (offset)
6905 +                       return INVALID_PORT;
6906 +
6907 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6908 +               if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
6909 +#else
6910 +               if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
6911 +#endif
6912 +                       /* No choice either */
6913 +                       return INVALID_PORT;
6914 +               
6915 +               return ntohs(flags & IPSET_SRC ?
6916 +                            udph.source : udph.dest);
6917 +           }
6918 +       default:
6919 +               return INVALID_PORT;
6920 +       }
6921 +}
6922 +
6923 +static inline int
6924 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
6925 +{
6926 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
6927 +
6928 +       if (port < map->first_port || port > map->last_port)
6929 +               return -ERANGE;
6930 +               
6931 +       *hash_port = port;
6932 +       DP("set: %s, port:%u, %u", set->name, port, *hash_port);
6933 +       return !!test_bit(port - map->first_port, map->members);
6934 +}
6935 +
6936 +static int
6937 +testport(struct ip_set *set, const void *data, size_t size,
6938 +         ip_set_ip_t *hash_port)
6939 +{
6940 +       struct ip_set_req_portmap *req = 
6941 +           (struct ip_set_req_portmap *) data;
6942 +
6943 +       if (size != sizeof(struct ip_set_req_portmap)) {
6944 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6945 +                             sizeof(struct ip_set_req_portmap),
6946 +                             size);
6947 +               return -EINVAL;
6948 +       }
6949 +       return __testport(set, req->port, hash_port);
6950 +}
6951 +
6952 +static int
6953 +testport_kernel(struct ip_set *set, 
6954 +               const struct sk_buff *skb,
6955 +               ip_set_ip_t *hash_port,
6956 +               const u_int32_t *flags,
6957 +               unsigned char index)
6958 +{
6959 +       int res;
6960 +       ip_set_ip_t port = get_port(skb, flags[index]);
6961 +
6962 +       DP("flag %s port %u", flags[index] & IPSET_SRC ? "SRC" : "DST", port);  
6963 +       if (port == INVALID_PORT)
6964 +               return 0;       
6965 +
6966 +       res =  __testport(set, port, hash_port);
6967 +       
6968 +       return (res < 0 ? 0 : res);
6969 +}
6970 +
6971 +static inline int
6972 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
6973 +{
6974 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
6975 +
6976 +       if (port < map->first_port || port > map->last_port)
6977 +               return -ERANGE;
6978 +       if (test_and_set_bit(port - map->first_port, map->members))
6979 +               return -EEXIST;
6980 +               
6981 +       *hash_port = port;
6982 +       DP("port %u", port);
6983 +       return 0;
6984 +}
6985 +
6986 +static int
6987 +addport(struct ip_set *set, const void *data, size_t size,
6988 +        ip_set_ip_t *hash_port)
6989 +{
6990 +       struct ip_set_req_portmap *req = 
6991 +           (struct ip_set_req_portmap *) data;
6992 +
6993 +       if (size != sizeof(struct ip_set_req_portmap)) {
6994 +               ip_set_printk("data length wrong (want %zu, have %zu)",
6995 +                             sizeof(struct ip_set_req_portmap),
6996 +                             size);
6997 +               return -EINVAL;
6998 +       }
6999 +       return __addport(set, req->port, hash_port);
7000 +}
7001 +
7002 +static int
7003 +addport_kernel(struct ip_set *set, 
7004 +              const struct sk_buff *skb,
7005 +              ip_set_ip_t *hash_port,
7006 +              const u_int32_t *flags,
7007 +              unsigned char index)
7008 +{
7009 +       ip_set_ip_t port = get_port(skb, flags[index]);
7010 +       
7011 +       if (port == INVALID_PORT)
7012 +               return -EINVAL;
7013 +
7014 +       return __addport(set, port, hash_port);
7015 +}
7016 +
7017 +static inline int
7018 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
7019 +{
7020 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7021 +
7022 +       if (port < map->first_port || port > map->last_port)
7023 +               return -ERANGE;
7024 +       if (!test_and_clear_bit(port - map->first_port, map->members))
7025 +               return -EEXIST;
7026 +               
7027 +       *hash_port = port;
7028 +       DP("port %u", port);
7029 +       return 0;
7030 +}
7031 +
7032 +static int
7033 +delport(struct ip_set *set, const void *data, size_t size,
7034 +        ip_set_ip_t *hash_port)
7035 +{
7036 +       struct ip_set_req_portmap *req =
7037 +           (struct ip_set_req_portmap *) data;
7038 +
7039 +       if (size != sizeof(struct ip_set_req_portmap)) {
7040 +               ip_set_printk("data length wrong (want %zu, have %zu)",
7041 +                             sizeof(struct ip_set_req_portmap),
7042 +                             size);
7043 +               return -EINVAL;
7044 +       }
7045 +       return __delport(set, req->port, hash_port);
7046 +}
7047 +
7048 +static int
7049 +delport_kernel(struct ip_set *set, 
7050 +              const struct sk_buff *skb,
7051 +              ip_set_ip_t *hash_port,
7052 +              const u_int32_t *flags,
7053 +              unsigned char index)
7054 +{
7055 +       ip_set_ip_t port = get_port(skb, flags[index]);
7056 +       
7057 +       if (port == INVALID_PORT)
7058 +               return -EINVAL;
7059 +
7060 +       return __delport(set, port, hash_port);
7061 +}
7062 +
7063 +static int create(struct ip_set *set, const void *data, size_t size)
7064 +{
7065 +       int newbytes;
7066 +       struct ip_set_req_portmap_create *req =
7067 +           (struct ip_set_req_portmap_create *) data;
7068 +       struct ip_set_portmap *map;
7069 +
7070 +       if (size != sizeof(struct ip_set_req_portmap_create)) {
7071 +               ip_set_printk("data length wrong (want %zu, have %zu)",
7072 +                              sizeof(struct ip_set_req_portmap_create),
7073 +                              size);
7074 +               return -EINVAL;
7075 +       }
7076 +
7077 +       DP("from %u to %u", req->from, req->to);
7078 +
7079 +       if (req->from > req->to) {
7080 +               DP("bad port range");
7081 +               return -ENOEXEC;
7082 +       }
7083 +
7084 +       if (req->to - req->from > MAX_RANGE) {
7085 +               ip_set_printk("range too big (max %d ports)",
7086 +                              MAX_RANGE+1);
7087 +               return -ENOEXEC;
7088 +       }
7089 +
7090 +       map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
7091 +       if (!map) {
7092 +               DP("out of memory for %d bytes",
7093 +                  sizeof(struct ip_set_portmap));
7094 +               return -ENOMEM;
7095 +       }
7096 +       map->first_port = req->from;
7097 +       map->last_port = req->to;
7098 +       newbytes = bitmap_bytes(req->from, req->to);
7099 +       map->members = kmalloc(newbytes, GFP_KERNEL);
7100 +       if (!map->members) {
7101 +               DP("out of memory for %d bytes", newbytes);
7102 +               kfree(map);
7103 +               return -ENOMEM;
7104 +       }
7105 +       memset(map->members, 0, newbytes);
7106 +
7107 +       set->data = map;
7108 +       return 0;
7109 +}
7110 +
7111 +static void destroy(struct ip_set *set)
7112 +{
7113 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7114 +
7115 +       kfree(map->members);
7116 +       kfree(map);
7117 +
7118 +       set->data = NULL;
7119 +}
7120 +
7121 +static void flush(struct ip_set *set)
7122 +{
7123 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7124 +       memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
7125 +}
7126 +
7127 +static void list_header(const struct ip_set *set, void *data)
7128 +{
7129 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7130 +       struct ip_set_req_portmap_create *header =
7131 +           (struct ip_set_req_portmap_create *) data;
7132 +
7133 +       DP("list_header %u %u", map->first_port, map->last_port);
7134 +
7135 +       header->from = map->first_port;
7136 +       header->to = map->last_port;
7137 +}
7138 +
7139 +static int list_members_size(const struct ip_set *set)
7140 +{
7141 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7142 +
7143 +       return bitmap_bytes(map->first_port, map->last_port);
7144 +}
7145 +
7146 +static void list_members(const struct ip_set *set, void *data)
7147 +{
7148 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7149 +       int bytes = bitmap_bytes(map->first_port, map->last_port);
7150 +
7151 +       memcpy(data, map->members, bytes);
7152 +}
7153 +
7154 +static struct ip_set_type ip_set_portmap = {
7155 +       .typename               = SETTYPE_NAME,
7156 +       .features               = IPSET_TYPE_PORT | IPSET_DATA_SINGLE,
7157 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
7158 +       .create                 = &create,
7159 +       .destroy                = &destroy,
7160 +       .flush                  = &flush,
7161 +       .reqsize                = sizeof(struct ip_set_req_portmap),
7162 +       .addip                  = &addport,
7163 +       .addip_kernel           = &addport_kernel,
7164 +       .delip                  = &delport,
7165 +       .delip_kernel           = &delport_kernel,
7166 +       .testip                 = &testport,
7167 +       .testip_kernel          = &testport_kernel,
7168 +       .header_size            = sizeof(struct ip_set_req_portmap_create),
7169 +       .list_header            = &list_header,
7170 +       .list_members_size      = &list_members_size,
7171 +       .list_members           = &list_members,
7172 +       .me                     = THIS_MODULE,
7173 +};
7174 +
7175 +MODULE_LICENSE("GPL");
7176 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7177 +MODULE_DESCRIPTION("portmap type of IP sets");
7178 +
7179 +static int __init ip_set_portmap_init(void)
7180 +{
7181 +       return ip_set_register_set_type(&ip_set_portmap);
7182 +}
7183 +
7184 +static void __exit ip_set_portmap_fini(void)
7185 +{
7186 +       /* FIXME: possible race with ip_set_create() */
7187 +       ip_set_unregister_set_type(&ip_set_portmap);
7188 +}
7189 +
7190 +module_init(ip_set_portmap_init);
7191 +module_exit(ip_set_portmap_fini);
7192 diff -uNrp linux-2.6./net/ipv4/netfilter/ipt_set.c linux-2.6/net/ipv4/netfilter/ipt_set.c
7193 --- linux-2.6./net/ipv4/netfilter/ipt_set.c     1970-01-01 01:00:00.000000000 +0100
7194 +++ linux-2.6/net/ipv4/netfilter/ipt_set.c      2007-08-28 13:29:12.000000000 +0200
7195 @@ -0,0 +1,160 @@
7196 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7197 + *                         Patrick Schaaf <bof@bof.de>
7198 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
7199 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7200 + *
7201 + * This program is free software; you can redistribute it and/or modify
7202 + * it under the terms of the GNU General Public License version 2 as
7203 + * published by the Free Software Foundation.  
7204 + */
7205 +
7206 +/* Kernel module to match an IP set. */
7207 +
7208 +#include <linux/module.h>
7209 +#include <linux/ip.h>
7210 +#include <linux/skbuff.h>
7211 +#include <linux/version.h>
7212 +
7213 +#include <linux/netfilter_ipv4/ip_tables.h>
7214 +#include <linux/netfilter_ipv4/ip_set.h>
7215 +#include <linux/netfilter_ipv4/ipt_set.h>
7216 +
7217 +static inline int
7218 +match_set(const struct ipt_set_info *info,
7219 +         const struct sk_buff *skb,
7220 +         int inv)
7221 +{      
7222 +       if (ip_set_testip_kernel(info->index, skb, info->flags))
7223 +               inv = !inv;
7224 +       return inv;
7225 +}
7226 +
7227 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7228 +static bool
7229 +#else
7230 +static int
7231 +#endif
7232 +match(const struct sk_buff *skb,
7233 +      const struct net_device *in,
7234 +      const struct net_device *out,
7235 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7236 +      const struct xt_match *match,
7237 +#endif
7238 +      const void *matchinfo,
7239 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7240 +      int offset, unsigned int protoff, bool *hotdrop)
7241 +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7242 +      int offset, unsigned int protoff, int *hotdrop)
7243 +#else
7244 +      int offset, int *hotdrop)
7245 +#endif
7246 +{
7247 +       const struct ipt_set_info_match *info = matchinfo;
7248 +               
7249 +       return match_set(&info->match_set,
7250 +                        skb,
7251 +                        info->match_set.flags[0] & IPSET_MATCH_INV);
7252 +}
7253 +
7254 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7255 +bool
7256 +#else
7257 +static int
7258 +#endif
7259 +checkentry(const char *tablename,
7260 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7261 +          const void *inf,
7262 +#else
7263 +          const struct ipt_ip *ip,
7264 +#endif
7265 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7266 +          const struct xt_match *match,
7267 +#endif
7268 +          void *matchinfo,
7269 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7270 +          unsigned int matchsize,
7271 +#endif
7272 +          unsigned int hook_mask)
7273 +{
7274 +       struct ipt_set_info_match *info = 
7275 +               (struct ipt_set_info_match *) matchinfo;
7276 +       ip_set_id_t index;
7277 +
7278 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7279 +       if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7280 +               ip_set_printk("invalid matchsize %d", matchsize);
7281 +               return 0;
7282 +       }
7283 +#endif
7284 +
7285 +       index = ip_set_get_byindex(info->match_set.index);
7286 +               
7287 +       if (index == IP_SET_INVALID_ID) {
7288 +               ip_set_printk("Cannot find set indentified by id %u to match",
7289 +                             info->match_set.index);
7290 +               return 0;       /* error */
7291 +       }
7292 +       if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7293 +               ip_set_printk("That's nasty!");
7294 +               return 0;       /* error */
7295 +       }
7296 +
7297 +       return 1;
7298 +}
7299 +
7300 +static void destroy(
7301 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7302 +                   const struct xt_match *match,
7303 +#endif
7304 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7305 +                   void *matchinfo, unsigned int matchsize)
7306 +#else
7307 +                   void *matchinfo)
7308 +#endif
7309 +{
7310 +       struct ipt_set_info_match *info = matchinfo;
7311 +
7312 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7313 +       if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7314 +               ip_set_printk("invalid matchsize %d", matchsize);
7315 +               return;
7316 +       }
7317 +#endif
7318 +       ip_set_put(info->match_set.index);
7319 +}
7320 +
7321 +static struct ipt_match set_match = {
7322 +       .name           = "set",
7323 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7324 +       .family         = AF_INET,
7325 +#endif
7326 +       .match          = &match,
7327 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7328 +       .matchsize      = sizeof(struct ipt_set_info_match),
7329 +#endif
7330 +       .checkentry     = &checkentry,
7331 +       .destroy        = &destroy,
7332 +       .me             = THIS_MODULE
7333 +};
7334 +
7335 +MODULE_LICENSE("GPL");
7336 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7337 +MODULE_DESCRIPTION("iptables IP set match module");
7338 +
7339 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7340 +#define ipt_register_match     xt_register_match
7341 +#define ipt_unregister_match   xt_unregister_match
7342 +#endif
7343 +
7344 +static int __init ipt_ipset_init(void)
7345 +{
7346 +       return ipt_register_match(&set_match);
7347 +}
7348 +
7349 +static void __exit ipt_ipset_fini(void)
7350 +{
7351 +       ipt_unregister_match(&set_match);
7352 +}
7353 +
7354 +module_init(ipt_ipset_init);
7355 +module_exit(ipt_ipset_fini);
7356 diff -uNrp linux-2.6./net/ipv4/netfilter/ipt_SET.c linux-2.6/net/ipv4/netfilter/ipt_SET.c
7357 --- linux-2.6./net/ipv4/netfilter/ipt_SET.c     1970-01-01 01:00:00.000000000 +0100
7358 +++ linux-2.6/net/ipv4/netfilter/ipt_SET.c      2007-08-28 13:29:12.000000000 +0200
7359 @@ -0,0 +1,172 @@
7360 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7361 + *                         Patrick Schaaf <bof@bof.de>
7362 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
7363 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7364 + *
7365 + * This program is free software; you can redistribute it and/or modify
7366 + * it under the terms of the GNU General Public License version 2 as
7367 + * published by the Free Software Foundation.  
7368 + */
7369 +
7370 +/* ipt_SET.c - netfilter target to manipulate IP sets */
7371 +
7372 +#include <linux/types.h>
7373 +#include <linux/ip.h>
7374 +#include <linux/timer.h>
7375 +#include <linux/module.h>
7376 +#include <linux/netfilter.h>
7377 +#include <linux/netdevice.h>
7378 +#include <linux/if.h>
7379 +#include <linux/inetdevice.h>
7380 +#include <linux/version.h>
7381 +#include <net/protocol.h>
7382 +#include <net/checksum.h>
7383 +#include <linux/netfilter_ipv4.h>
7384 +#include <linux/netfilter_ipv4/ip_tables.h>
7385 +#include <linux/netfilter_ipv4/ipt_set.h>
7386 +
7387 +static unsigned int
7388 +target(struct sk_buff **pskb,
7389 +       const struct net_device *in,
7390 +       const struct net_device *out,
7391 +       unsigned int hooknum,
7392 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7393 +       const struct xt_target *target,
7394 +#endif
7395 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7396 +       const void *targinfo,
7397 +       void *userinfo)
7398 +#else
7399 +       const void *targinfo)
7400 +#endif
7401 +{
7402 +       const struct ipt_set_info_target *info = targinfo;
7403 +       
7404 +       if (info->add_set.index != IP_SET_INVALID_ID)
7405 +               ip_set_addip_kernel(info->add_set.index,
7406 +                                   *pskb,
7407 +                                   info->add_set.flags);
7408 +       if (info->del_set.index != IP_SET_INVALID_ID)
7409 +               ip_set_delip_kernel(info->del_set.index,
7410 +                                   *pskb,
7411 +                                   info->del_set.flags);
7412 +
7413 +       return IPT_CONTINUE;
7414 +}
7415 +
7416 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7417 +static bool
7418 +#else
7419 +static int
7420 +#endif
7421 +checkentry(const char *tablename,
7422 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7423 +          const void *e,
7424 +#else
7425 +          const struct ipt_entry *e,
7426 +#endif
7427 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7428 +          const struct xt_target *target,
7429 +#endif
7430 +          void *targinfo,
7431 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7432 +          unsigned int targinfosize, 
7433 +#endif
7434 +          unsigned int hook_mask)
7435 +{
7436 +       struct ipt_set_info_target *info = 
7437 +               (struct ipt_set_info_target *) targinfo;
7438 +       ip_set_id_t index;
7439 +
7440 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7441 +       if (targinfosize != IPT_ALIGN(sizeof(*info))) {
7442 +               DP("bad target info size %u", targinfosize);
7443 +               return 0;
7444 +       }
7445 +#endif
7446 +
7447 +       if (info->add_set.index != IP_SET_INVALID_ID) {
7448 +               index = ip_set_get_byindex(info->add_set.index);
7449 +               if (index == IP_SET_INVALID_ID) {
7450 +                       ip_set_printk("cannot find add_set index %u as target",
7451 +                                     info->add_set.index);
7452 +                       return 0;       /* error */
7453 +               }
7454 +       }
7455 +
7456 +       if (info->del_set.index != IP_SET_INVALID_ID) {
7457 +               index = ip_set_get_byindex(info->del_set.index);
7458 +               if (index == IP_SET_INVALID_ID) {
7459 +                       ip_set_printk("cannot find del_set index %u as target",
7460 +                                     info->del_set.index);
7461 +                       return 0;       /* error */
7462 +               }
7463 +       }
7464 +       if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
7465 +           || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7466 +               ip_set_printk("That's nasty!");
7467 +               return 0;       /* error */
7468 +       }
7469 +
7470 +       return 1;
7471 +}
7472 +
7473 +static void destroy(
7474 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7475 +                   const struct xt_target *target,
7476 +#endif
7477 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7478 +                   void *targetinfo, unsigned int targetsize)
7479 +#else
7480 +                   void *targetinfo)
7481 +#endif
7482 +{
7483 +       struct ipt_set_info_target *info = targetinfo;
7484 +
7485 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7486 +       if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
7487 +               ip_set_printk("invalid targetsize %d", targetsize);
7488 +               return;
7489 +       }
7490 +#endif
7491 +       if (info->add_set.index != IP_SET_INVALID_ID)
7492 +               ip_set_put(info->add_set.index);
7493 +       if (info->del_set.index != IP_SET_INVALID_ID)
7494 +               ip_set_put(info->del_set.index);
7495 +}
7496 +
7497 +static struct ipt_target SET_target = {
7498 +       .name           = "SET",
7499 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7500 +       .family         = AF_INET,
7501 +#endif
7502 +       .target         = target,
7503 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7504 +       .targetsize     = sizeof(struct ipt_set_info_target),
7505 +#endif
7506 +       .checkentry     = checkentry,
7507 +       .destroy        = destroy,
7508 +       .me             = THIS_MODULE
7509 +};
7510 +
7511 +MODULE_LICENSE("GPL");
7512 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7513 +MODULE_DESCRIPTION("iptables IP set target module");
7514 +
7515 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7516 +#define ipt_register_target      xt_register_target
7517 +#define ipt_unregister_target    xt_unregister_target
7518 +#endif
7519 +
7520 +static int __init ipt_SET_init(void)
7521 +{
7522 +       return ipt_register_target(&SET_target);
7523 +}
7524 +
7525 +static void __exit ipt_SET_fini(void)
7526 +{
7527 +       ipt_unregister_target(&SET_target);
7528 +}
7529 +
7530 +module_init(ipt_SET_init);
7531 +module_exit(ipt_SET_fini);
7532 diff -uNrp linux-2.6./net/ipv4/netfilter/Kconfig linux-2.6/net/ipv4/netfilter/Kconfig
7533 --- linux-2.6./net/ipv4/netfilter/Kconfig       1971-01-01 01:00:00.000000000 +0100
7534 +++ linux-2.6/net/ipv4/netfilter/Kconfig        2007-08-28 13:29:12.000000000 +0200
7535 @@ -0,0 +1,116 @@
7536 +config IP_NF_SET
7537 +       tristate "IP set support"
7538 +       depends on INET && NETFILTER
7539 +       help
7540 +         This option adds IP set support to the kernel.
7541 +         In order to define and use sets, you need the userspace utility
7542 +         ipset(8).
7543 +
7544 +         To compile it as a module, choose M here.  If unsure, say N.
7545 +
7546 +config IP_NF_SET_MAX
7547 +       int "Maximum number of IP sets"
7548 +       default 256
7549 +       range 2 65534
7550 +       depends on IP_NF_SET
7551 +       help
7552 +         You can define here default value of the maximum number 
7553 +         of IP sets for the kernel.
7554 +
7555 +         The value can be overriden by the 'max_sets' module
7556 +         parameter of the 'ip_set' module.
7557 +
7558 +config IP_NF_SET_HASHSIZE
7559 +       int "Hash size for bindings of IP sets"
7560 +       default 1024
7561 +       depends on IP_NF_SET
7562 +       help
7563 +         You can define here default value of the hash size for
7564 +         bindings of IP sets.
7565 +
7566 +         The value can be overriden by the 'hash_size' module
7567 +         parameter of the 'ip_set' module.
7568 +
7569 +config IP_NF_SET_IPMAP
7570 +       tristate "ipmap set support"
7571 +       depends on IP_NF_SET
7572 +       help
7573 +         This option adds the ipmap set type support.
7574 +
7575 +         To compile it as a module, choose M here.  If unsure, say N.
7576 +
7577 +config IP_NF_SET_MACIPMAP
7578 +       tristate "macipmap set support"
7579 +       depends on IP_NF_SET
7580 +       help
7581 +         This option adds the macipmap set type support.
7582 +
7583 +         To compile it as a module, choose M here.  If unsure, say N.
7584 +
7585 +config IP_NF_SET_PORTMAP
7586 +       tristate "portmap set support"
7587 +       depends on IP_NF_SET
7588 +       help
7589 +         This option adds the portmap set type support.
7590 +
7591 +         To compile it as a module, choose M here.  If unsure, say N.
7592 +
7593 +config IP_NF_SET_IPHASH
7594 +       tristate "iphash set support"
7595 +       depends on IP_NF_SET
7596 +       help
7597 +         This option adds the iphash set type support.
7598 +
7599 +         To compile it as a module, choose M here.  If unsure, say N.
7600 +
7601 +config IP_NF_SET_NETHASH
7602 +       tristate "nethash set support"
7603 +       depends on IP_NF_SET
7604 +       help
7605 +         This option adds the nethash set type support.
7606 +
7607 +         To compile it as a module, choose M here.  If unsure, say N.
7608 +
7609 +config IP_NF_SET_IPPORTHASH
7610 +       tristate "ipporthash set support"
7611 +       depends on IP_NF_SET
7612 +       help
7613 +         This option adds the ipporthash set type support.
7614 +
7615 +         To compile it as a module, choose M here.  If unsure, say N.
7616 +
7617 +config IP_NF_SET_IPTREE
7618 +       tristate "iptree set support"
7619 +       depends on IP_NF_SET
7620 +       help
7621 +         This option adds the iptree set type support.
7622 +
7623 +         To compile it as a module, choose M here.  If unsure, say N.
7624 +
7625 +config IP_NF_SET_IPTREEMAP
7626 +       tristate "iptreemap set support"
7627 +       depends on IP_NF_SET
7628 +       help
7629 +         This option adds the iptreemap set type support.
7630 +
7631 +         To compile it as a module, choose M here.  If unsure, say N.
7632 +
7633 +config IP_NF_MATCH_SET
7634 +       tristate "set match support"
7635 +       depends on IP_NF_SET
7636 +       help
7637 +         Set matching matches against given IP sets.
7638 +         You need the ipset utility to create and set up the sets.
7639 +
7640 +         To compile it as a module, choose M here.  If unsure, say N.
7641 +
7642 +config IP_NF_TARGET_SET
7643 +       tristate "SET target support"
7644 +       depends on IP_NF_SET
7645 +       help
7646 +         The SET target makes possible to add/delete entries
7647 +         in IP sets.
7648 +         You need the ipset utility to create and set up the sets.
7649 +
7650 +         To compile it as a module, choose M here.  If unsure, say N.
7651 +
7652 diff -uNrp linux-2.6./net/ipv4/netfilter/Makefile linux-2.6/net/ipv4/netfilter/Makefile
7653 --- linux-2.6./net/ipv4/netfilter/Makefile      1971-01-01 01:00:00.000000000 +0100
7654 +++ linux-2.6/net/ipv4/netfilter/Makefile       2006-03-03 10:29:46.000000000 +0100
7655 @@ -0,0 +1,2 @@
7656 +obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
7657 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
7658 diff -uNrp linux-2.6./net/ipv4/netfilter/Makefile linux-2.6/net/ipv4/netfilter/Makefile
7659 --- linux-2.6./net/ipv4/netfilter/Makefile      1971-01-01 01:00:00.000000000 +0100
7660 +++ linux-2.6/net/ipv4/netfilter/Makefile       2007-08-28 13:29:12.000000000 +0200
7661 @@ -0,0 +1,13 @@
7662 +obj-$(CONFIG_IP_NF_TARGET_TTL) += ipt_TTL.o
7663 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
7664 +
7665 +# sets
7666 +obj-$(CONFIG_IP_NF_SET) += ip_set.o
7667 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
7668 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
7669 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
7670 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
7671 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
7672 +obj-$(CONFIG_IP_NF_SET_IPPORTHASH) += ip_set_ipporthash.o
7673 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
7674 +obj-$(CONFIG_IP_NF_SET_IPTREEMAP) += ip_set_iptreemap.o
This page took 0.633007 seconds and 3 git commands to generate.