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