915f39f089d8f1b9c728760608b35f9fca148a2e
[openwrt.git] / target / linux / generic-2.4 / patches / 608-netfilter_ipset.patch
1 Index: linux-2.4.35.4/Documentation/Configure.help
2 ===================================================================
3 --- linux-2.4.35.4.orig/Documentation/Configure.help    2007-12-15 05:20:07.284255349 +0100
4 +++ linux-2.4.35.4/Documentation/Configure.help 2007-12-15 05:20:07.880289314 +0100
5 @@ -3220,6 +3220,75 @@
6    If you want to compile it as a module, say M here and read
7    <file:Documentation/modules.txt>.  If unsure, say `N'.
8  
9 +IP set support
10 +CONFIG_IP_NF_SET
11 +  This option adds IP set support to the kernel.
12 +
13 +  In order to define and use sets, you need userlevel utilities: an
14 +  iptables binary which knows about IP sets and the program ipset(8), 
15 +  by which you can define and setup the sets themselves. 
16 +
17 +  If you want to compile it as a module, say M here and read
18 +  <file:Documentation/modules.txt>.  If unsure, say `N'.
19 +
20 +set match support
21 +CONFIG_IP_NF_MATCH_SET
22 +  This option adds IP set match support.
23 +  You need the ipset utility to create and set up the sets.
24 +
25 +  If you want to compile it as a module, say M here and read
26 +  <file:Documentation/modules.txt>.  If unsure, say `N'.
27 +
28 +SET target support
29 +CONFIG_IP_NF_TARGET_SET
30 +  This option adds IP set target support.
31 +  You need the ipset utility to create and set up the sets.
32 +
33 +  If you want to compile it as a module, say M here and read
34 +  <file:Documentation/modules.txt>.  If unsure, say `N'.
35 +
36 +ipmap set type support
37 +CONFIG_IP_NF_SET_IPMAP
38 +  This option adds the ipmap set type support.
39 +
40 +  If you want to compile it as a module, say M here and read
41 +  <file:Documentation/modules.txt>.  If unsure, say `N'.
42 +
43 +macipmap set type support
44 +CONFIG_IP_NF_SET_MACIPMAP
45 +  This option adds the macipmap set type support.
46 +
47 +  If you want to compile it as a module, say M here and read
48 +  <file:Documentation/modules.txt>.  If unsure, say `N'.
49 +
50 +portmap set type support
51 +CONFIG_IP_NF_SET_PORTMAP
52 +  This option adds the portmap set type support.
53 +
54 +  If you want to compile it as a module, say M here and read
55 +  <file:Documentation/modules.txt>.  If unsure, say `N'.
56 +
57 +iphash set type support
58 +CONFIG_IP_NF_SET_IPHASH
59 +  This option adds the iphash set type support.
60 +
61 +  If you want to compile it as a module, say M here and read
62 +  <file:Documentation/modules.txt>.  If unsure, say `N'.
63 +
64 +nethash set type support
65 +CONFIG_IP_NF_SET_NETHASH
66 +  This option adds the nethash set type support.
67 +
68 +  If you want to compile it as a module, say M here and read
69 +  <file:Documentation/modules.txt>.  If unsure, say `N'.
70 +
71 +iptree set type support
72 +CONFIG_IP_NF_SET_IPTREE
73 +  This option adds the iptree set type support.
74 +
75 +  If you want to compile it as a module, say M here and read
76 +  <file:Documentation/modules.txt>.  If unsure, say `N'.
77 +
78  TTL target support
79  CONFIG_IP_NF_TARGET_TTL
80    This option adds a `TTL' target, which enables the user to set
81 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set.h
82 ===================================================================
83 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
84 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set.h        2007-12-15 05:20:07.884289543 +0100
85 @@ -0,0 +1,489 @@
86 +#ifndef _IP_SET_H
87 +#define _IP_SET_H
88 +
89 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
90 + *                         Patrick Schaaf <bof@bof.de>
91 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
92 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
93 + *
94 + * This program is free software; you can redistribute it and/or modify
95 + * it under the terms of the GNU General Public License version 2 as
96 + * published by the Free Software Foundation.  
97 + */
98 +
99 +/*
100 + * A sockopt of such quality has hardly ever been seen before on the open
101 + * market!  This little beauty, hardly ever used: above 64, so it's
102 + * traditionally used for firewalling, not touched (even once!) by the
103 + * 2.0, 2.2 and 2.4 kernels!
104 + *
105 + * Comes with its own certificate of authenticity, valid anywhere in the
106 + * Free world!
107 + *
108 + * Rusty, 19.4.2000
109 + */
110 +#define SO_IP_SET              83
111 +
112 +/*
113 + * Heavily modify by Joakim Axelsson 08.03.2002
114 + * - Made it more modulebased
115 + *
116 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
117 + * - bindings added
118 + * - in order to "deal with" backward compatibility, renamed to ipset
119 + */
120 +
121 +/* 
122 + * Used so that the kernel module and ipset-binary can match their versions 
123 + */
124 +#define IP_SET_PROTOCOL_VERSION 2
125 +
126 +#define IP_SET_MAXNAMELEN 32   /* set names and set typenames */
127 +
128 +/* Lets work with our own typedef for representing an IP address.
129 + * We hope to make the code more portable, possibly to IPv6...
130 + *
131 + * The representation works in HOST byte order, because most set types
132 + * will perform arithmetic operations and compare operations.
133 + * 
134 + * For now the type is an uint32_t.
135 + *
136 + * Make sure to ONLY use the functions when translating and parsing
137 + * in order to keep the host byte order and make it more portable:
138 + *  parse_ip()
139 + *  parse_mask()
140 + *  parse_ipandmask()
141 + *  ip_tostring()
142 + * (Joakim: where are they???)
143 + */
144 +
145 +typedef uint32_t ip_set_ip_t;
146 +
147 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
148 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
149 + */
150 +typedef uint16_t ip_set_id_t;
151 +
152 +#define IP_SET_INVALID_ID      65535
153 +
154 +/* How deep we follow bindings  */
155 +#define IP_SET_MAX_BINDINGS    6
156 +
157 +/*
158 + * Option flags for kernel operations (ipt_set_info)
159 + */
160 +#define IPSET_SRC              0x01    /* Source match/add */
161 +#define IPSET_DST              0x02    /* Destination match/add */
162 +#define IPSET_MATCH_INV                0x04    /* Inverse matching */
163 +
164 +/*
165 + * Set types (flavours)
166 + */
167 +#define IPSET_TYPE_IP          0       /* IP address type of set */
168 +#define IPSET_TYPE_PORT                1       /* Port type of set */
169 +
170 +/* Reserved keywords */
171 +#define IPSET_TOKEN_DEFAULT    ":default:"
172 +#define IPSET_TOKEN_ALL                ":all:"
173 +
174 +/* SO_IP_SET operation constants, and their request struct types.
175 + *
176 + * Operation ids:
177 + *       0-99:  commands with version checking
178 + *     100-199: add/del/test/bind/unbind
179 + *     200-299: list, save, restore
180 + */
181 +
182 +/* Single shot operations: 
183 + * version, create, destroy, flush, rename and swap 
184 + *
185 + * Sets are identified by name.
186 + */
187 +
188 +#define IP_SET_REQ_STD         \
189 +       unsigned op;            \
190 +       unsigned version;       \
191 +       char name[IP_SET_MAXNAMELEN]
192 +
193 +#define IP_SET_OP_CREATE       0x00000001      /* Create a new (empty) set */
194 +struct ip_set_req_create {
195 +       IP_SET_REQ_STD;
196 +       char typename[IP_SET_MAXNAMELEN];
197 +};
198 +
199 +#define IP_SET_OP_DESTROY      0x00000002      /* Remove a (empty) set */
200 +struct ip_set_req_std {
201 +       IP_SET_REQ_STD;
202 +};
203 +
204 +#define IP_SET_OP_FLUSH                0x00000003      /* Remove all IPs in a set */
205 +/* Uses ip_set_req_std */
206 +
207 +#define IP_SET_OP_RENAME       0x00000004      /* Rename a set */
208 +/* Uses ip_set_req_create */
209 +
210 +#define IP_SET_OP_SWAP         0x00000005      /* Swap two sets */
211 +/* Uses ip_set_req_create */
212 +
213 +union ip_set_name_index {
214 +       char name[IP_SET_MAXNAMELEN];
215 +       ip_set_id_t index;
216 +};
217 +
218 +#define IP_SET_OP_GET_BYNAME   0x00000006      /* Get set index by name */
219 +struct ip_set_req_get_set {
220 +       unsigned op;
221 +       unsigned version;
222 +       union ip_set_name_index set;
223 +};
224 +
225 +#define IP_SET_OP_GET_BYINDEX  0x00000007      /* Get set name by index */
226 +/* Uses ip_set_req_get_set */
227 +
228 +#define IP_SET_OP_VERSION      0x00000100      /* Ask kernel version */
229 +struct ip_set_req_version {
230 +       unsigned op;
231 +       unsigned version;
232 +};
233 +
234 +/* Double shots operations: 
235 + * add, del, test, bind and unbind.
236 + *
237 + * First we query the kernel to get the index and type of the target set,
238 + * then issue the command. Validity of IP is checked in kernel in order
239 + * to minimalize sockopt operations.
240 + */
241 +
242 +/* Get minimal set data for add/del/test/bind/unbind IP */
243 +#define IP_SET_OP_ADT_GET      0x00000010      /* Get set and type */
244 +struct ip_set_req_adt_get {
245 +       unsigned op;
246 +       unsigned version;
247 +       union ip_set_name_index set;
248 +       char typename[IP_SET_MAXNAMELEN];
249 +};
250 +
251 +#define IP_SET_REQ_BYINDEX     \
252 +       unsigned op;            \
253 +       ip_set_id_t index;
254 +
255 +struct ip_set_req_adt {
256 +       IP_SET_REQ_BYINDEX;
257 +};
258 +
259 +#define IP_SET_OP_ADD_IP       0x00000101      /* Add an IP to a set */
260 +/* Uses ip_set_req_adt, with type specific addage */
261 +
262 +#define IP_SET_OP_DEL_IP       0x00000102      /* Remove an IP from a set */
263 +/* Uses ip_set_req_adt, with type specific addage */
264 +
265 +#define IP_SET_OP_TEST_IP      0x00000103      /* Test an IP in a set */
266 +/* Uses ip_set_req_adt, with type specific addage */
267 +
268 +#define IP_SET_OP_BIND_SET     0x00000104      /* Bind an IP to a set */
269 +/* Uses ip_set_req_bind, with type specific addage */
270 +struct ip_set_req_bind {
271 +       IP_SET_REQ_BYINDEX;
272 +       char binding[IP_SET_MAXNAMELEN];
273 +};
274 +
275 +#define IP_SET_OP_UNBIND_SET   0x00000105      /* Unbind an IP from a set */
276 +/* Uses ip_set_req_bind, with type speficic addage 
277 + * index = 0 means unbinding for all sets */
278 +
279 +#define IP_SET_OP_TEST_BIND_SET        0x00000106      /* Test binding an IP to a set */
280 +/* Uses ip_set_req_bind, with type specific addage */
281 +
282 +/* Multiple shots operations: list, save, restore.
283 + *
284 + * - check kernel version and query the max number of sets
285 + * - get the basic information on all sets
286 + *   and size required for the next step
287 + * - get actual set data: header, data, bindings
288 + */
289 +
290 +/* Get max_sets and the index of a queried set
291 + */
292 +#define IP_SET_OP_MAX_SETS     0x00000020
293 +struct ip_set_req_max_sets {
294 +       unsigned op;
295 +       unsigned version;
296 +       ip_set_id_t max_sets;           /* max_sets */
297 +       ip_set_id_t sets;               /* real number of sets */
298 +       union ip_set_name_index set;    /* index of set if name used */
299 +};
300 +
301 +/* Get the id and name of the sets plus size for next step */
302 +#define IP_SET_OP_LIST_SIZE    0x00000201
303 +#define IP_SET_OP_SAVE_SIZE    0x00000202
304 +struct ip_set_req_setnames {
305 +       unsigned op;
306 +       ip_set_id_t index;              /* set to list/save */
307 +       size_t size;                    /* size to get setdata/bindings */
308 +       /* followed by sets number of struct ip_set_name_list */
309 +};
310 +
311 +struct ip_set_name_list {
312 +       char name[IP_SET_MAXNAMELEN];
313 +       char typename[IP_SET_MAXNAMELEN];
314 +       ip_set_id_t index;
315 +       ip_set_id_t id;
316 +};
317 +
318 +/* The actual list operation */
319 +#define IP_SET_OP_LIST         0x00000203
320 +struct ip_set_req_list {
321 +       IP_SET_REQ_BYINDEX;
322 +       /* sets number of struct ip_set_list in reply */ 
323 +};
324 +
325 +struct ip_set_list {
326 +       ip_set_id_t index;
327 +       ip_set_id_t binding;
328 +       u_int32_t ref;
329 +       size_t header_size;     /* Set header data of header_size */
330 +       size_t members_size;    /* Set members data of members_size */
331 +       size_t bindings_size;   /* Set bindings data of bindings_size */
332 +};
333 +
334 +struct ip_set_hash_list {
335 +       ip_set_ip_t ip;
336 +       ip_set_id_t binding;
337 +};
338 +
339 +/* The save operation */
340 +#define IP_SET_OP_SAVE         0x00000204
341 +/* Uses ip_set_req_list, in the reply replaced by
342 + * sets number of struct ip_set_save plus a marker
343 + * ip_set_save followed by ip_set_hash_save structures.
344 + */
345 +struct ip_set_save {
346 +       ip_set_id_t index;
347 +       ip_set_id_t binding;
348 +       size_t header_size;     /* Set header data of header_size */
349 +       size_t members_size;    /* Set members data of members_size */
350 +};
351 +
352 +/* At restoring, ip == 0 means default binding for the given set: */
353 +struct ip_set_hash_save {
354 +       ip_set_ip_t ip;
355 +       ip_set_id_t id;
356 +       ip_set_id_t binding;
357 +};
358 +
359 +/* The restore operation */
360 +#define IP_SET_OP_RESTORE      0x00000205
361 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
362 + * plus a marker ip_set_restore, followed by ip_set_hash_save 
363 + * structures.
364 + */
365 +struct ip_set_restore {
366 +       char name[IP_SET_MAXNAMELEN];
367 +       char typename[IP_SET_MAXNAMELEN];
368 +       ip_set_id_t index;
369 +       size_t header_size;     /* Create data of header_size */
370 +       size_t members_size;    /* Set members data of members_size */
371 +};
372 +
373 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
374 +{
375 +       return 4 * ((((b - a + 8) / 8) + 3) / 4);
376 +}
377 +
378 +#ifdef __KERNEL__
379 +
380 +#define ip_set_printk(format, args...)                         \
381 +       do {                                                    \
382 +               printk("%s: %s: ", __FILE__, __FUNCTION__);     \
383 +               printk(format "\n" , ## args);                  \
384 +       } while (0)
385 +
386 +#if defined(IP_SET_DEBUG)
387 +#define DP(format, args...)                                    \
388 +       do {                                                    \
389 +               printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
390 +               printk(format "\n" , ## args);                  \
391 +       } while (0)
392 +#define IP_SET_ASSERT(x)                                       \
393 +       do {                                                    \
394 +               if (!(x))                                       \
395 +                       printk("IP_SET_ASSERT: %s:%i(%s)\n",    \
396 +                               __FILE__, __LINE__, __FUNCTION__); \
397 +       } while (0)
398 +#else
399 +#define DP(format, args...)
400 +#define IP_SET_ASSERT(x)
401 +#endif
402 +
403 +struct ip_set;
404 +
405 +/*
406 + * The ip_set_type definition - one per set type, e.g. "ipmap".
407 + *
408 + * Each individual set has a pointer, set->type, going to one
409 + * of these structures. Function pointers inside the structure implement
410 + * the real behaviour of the sets.
411 + *
412 + * If not mentioned differently, the implementation behind the function
413 + * pointers of a set_type, is expected to return 0 if ok, and a negative
414 + * errno (e.g. -EINVAL) on error.
415 + */
416 +struct ip_set_type {
417 +       struct list_head list;  /* next in list of set types */
418 +
419 +       /* test for IP in set (kernel: iptables -m set src|dst)
420 +        * return 0 if not in set, 1 if in set.
421 +        */
422 +       int (*testip_kernel) (struct ip_set *set,
423 +                             const struct sk_buff * skb, 
424 +                             u_int32_t flags,
425 +                             ip_set_ip_t *ip);
426 +
427 +       /* test for IP in set (userspace: ipset -T set IP)
428 +        * return 0 if not in set, 1 if in set.
429 +        */
430 +       int (*testip) (struct ip_set *set,
431 +                      const void *data, size_t size,
432 +                      ip_set_ip_t *ip);
433 +
434 +       /*
435 +        * Size of the data structure passed by when
436 +        * adding/deletin/testing an entry.
437 +        */
438 +       size_t reqsize;
439 +
440 +       /* Add IP into set (userspace: ipset -A set IP)
441 +        * Return -EEXIST if the address is already in the set,
442 +        * and -ERANGE if the address lies outside the set bounds.
443 +        * If the address was not already in the set, 0 is returned.
444 +        */
445 +       int (*addip) (struct ip_set *set, 
446 +                     const void *data, size_t size,
447 +                     ip_set_ip_t *ip);
448 +
449 +       /* Add IP into set (kernel: iptables ... -j SET set src|dst)
450 +        * Return -EEXIST if the address is already in the set,
451 +        * and -ERANGE if the address lies outside the set bounds.
452 +        * If the address was not already in the set, 0 is returned.
453 +        */
454 +       int (*addip_kernel) (struct ip_set *set,
455 +                            const struct sk_buff * skb, 
456 +                            u_int32_t flags,
457 +                            ip_set_ip_t *ip);
458 +
459 +       /* remove IP from set (userspace: ipset -D set --entry x)
460 +        * Return -EEXIST if the address is NOT in the set,
461 +        * and -ERANGE if the address lies outside the set bounds.
462 +        * If the address really was in the set, 0 is returned.
463 +        */
464 +       int (*delip) (struct ip_set *set, 
465 +                     const void *data, size_t size,
466 +                     ip_set_ip_t *ip);
467 +
468 +       /* remove IP from set (kernel: iptables ... -j SET --entry x)
469 +        * Return -EEXIST if the address is NOT in the set,
470 +        * and -ERANGE if the address lies outside the set bounds.
471 +        * If the address really was in the set, 0 is returned.
472 +        */
473 +       int (*delip_kernel) (struct ip_set *set,
474 +                            const struct sk_buff * skb, 
475 +                            u_int32_t flags,
476 +                            ip_set_ip_t *ip);
477 +
478 +       /* new set creation - allocated type specific items
479 +        */
480 +       int (*create) (struct ip_set *set,
481 +                      const void *data, size_t size);
482 +
483 +       /* retry the operation after successfully tweaking the set
484 +        */
485 +       int (*retry) (struct ip_set *set);
486 +
487 +       /* set destruction - free type specific items
488 +        * There is no return value.
489 +        * Can be called only when child sets are destroyed.
490 +        */
491 +       void (*destroy) (struct ip_set *set);
492 +
493 +       /* set flushing - reset all bits in the set, or something similar.
494 +        * There is no return value.
495 +        */
496 +       void (*flush) (struct ip_set *set);
497 +
498 +       /* Listing: size needed for header
499 +        */
500 +       size_t header_size;
501 +
502 +       /* Listing: Get the header
503 +        *
504 +        * Fill in the information in "data".
505 +        * This function is always run after list_header_size() under a 
506 +        * writelock on the set. Therefor is the length of "data" always 
507 +        * correct. 
508 +        */
509 +       void (*list_header) (const struct ip_set *set, 
510 +                            void *data);
511 +
512 +       /* Listing: Get the size for the set members
513 +        */
514 +       int (*list_members_size) (const struct ip_set *set);
515 +
516 +       /* Listing: Get the set members
517 +        *
518 +        * Fill in the information in "data".
519 +        * This function is always run after list_member_size() under a 
520 +        * writelock on the set. Therefor is the length of "data" always 
521 +        * correct. 
522 +        */
523 +       void (*list_members) (const struct ip_set *set,
524 +                             void *data);
525 +
526 +       char typename[IP_SET_MAXNAMELEN];
527 +       char typecode;
528 +       int protocol_version;
529 +
530 +       /* Set this to THIS_MODULE if you are a module, otherwise NULL */
531 +       struct module *me;
532 +};
533 +
534 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
535 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
536 +
537 +/* A generic ipset */
538 +struct ip_set {
539 +       char name[IP_SET_MAXNAMELEN];   /* the name of the set */
540 +       rwlock_t lock;                  /* lock for concurrency control */
541 +       ip_set_id_t id;                 /* set id for swapping */
542 +       ip_set_id_t binding;            /* default binding for the set */
543 +       atomic_t ref;                   /* in kernel and in hash references */
544 +       struct ip_set_type *type;       /* the set types */
545 +       void *data;                     /* pooltype specific data */
546 +};
547 +
548 +/* Structure to bind set elements to sets */
549 +struct ip_set_hash {
550 +       struct list_head list;          /* list of clashing entries in hash */
551 +       ip_set_ip_t ip;                 /* ip from set */
552 +       ip_set_id_t id;                 /* set id */
553 +       ip_set_id_t binding;            /* set we bind the element to */
554 +};
555 +
556 +/* register and unregister set references */
557 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
558 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
559 +extern void ip_set_put(ip_set_id_t id);
560 +
561 +/* API for iptables set match, and SET target */
562 +extern void ip_set_addip_kernel(ip_set_id_t id,
563 +                               const struct sk_buff *skb,
564 +                               const u_int32_t *flags);
565 +extern void ip_set_delip_kernel(ip_set_id_t id,
566 +                               const struct sk_buff *skb,
567 +                               const u_int32_t *flags);
568 +extern int ip_set_testip_kernel(ip_set_id_t id,
569 +                               const struct sk_buff *skb,
570 +                               const u_int32_t *flags);
571 +
572 +#endif                         /* __KERNEL__ */
573 +
574 +#endif /*_IP_SET_H*/
575 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_iphash.h
576 ===================================================================
577 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
578 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_iphash.h 2007-12-15 05:20:07.884289543 +0100
579 @@ -0,0 +1,30 @@
580 +#ifndef __IP_SET_IPHASH_H
581 +#define __IP_SET_IPHASH_H
582 +
583 +#include <linux/netfilter_ipv4/ip_set.h>
584 +
585 +#define SETTYPE_NAME "iphash"
586 +#define MAX_RANGE 0x0000FFFF
587 +
588 +struct ip_set_iphash {
589 +       ip_set_ip_t *members;           /* the iphash proper */
590 +       uint32_t initval;               /* initval for jhash_1word */
591 +       uint32_t prime;                 /* prime for double hashing */
592 +       uint32_t hashsize;              /* hash size */
593 +       uint16_t probes;                /* max number of probes  */
594 +       uint16_t resize;                /* resize factor in percent */
595 +       ip_set_ip_t netmask;            /* netmask */
596 +};
597 +
598 +struct ip_set_req_iphash_create {
599 +       uint32_t hashsize;
600 +       uint16_t probes;
601 +       uint16_t resize;
602 +       ip_set_ip_t netmask;
603 +};
604 +
605 +struct ip_set_req_iphash {
606 +       ip_set_ip_t ip;
607 +};
608 +
609 +#endif /* __IP_SET_IPHASH_H */
610 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_ipmap.h
611 ===================================================================
612 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
613 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_ipmap.h  2007-12-15 05:20:07.884289543 +0100
614 @@ -0,0 +1,56 @@
615 +#ifndef __IP_SET_IPMAP_H
616 +#define __IP_SET_IPMAP_H
617 +
618 +#include <linux/netfilter_ipv4/ip_set.h>
619 +
620 +#define SETTYPE_NAME "ipmap"
621 +#define MAX_RANGE 0x0000FFFF
622 +
623 +struct ip_set_ipmap {
624 +       void *members;                  /* the ipmap proper */
625 +       ip_set_ip_t first_ip;           /* host byte order, included in range */
626 +       ip_set_ip_t last_ip;            /* host byte order, included in range */
627 +       ip_set_ip_t netmask;            /* subnet netmask */
628 +       ip_set_ip_t sizeid;             /* size of set in IPs */
629 +       u_int16_t hosts;                /* number of hosts in a subnet */
630 +};
631 +
632 +struct ip_set_req_ipmap_create {
633 +       ip_set_ip_t from;
634 +       ip_set_ip_t to;
635 +       ip_set_ip_t netmask;
636 +};
637 +
638 +struct ip_set_req_ipmap {
639 +       ip_set_ip_t ip;
640 +};
641 +
642 +unsigned int
643 +mask_to_bits(ip_set_ip_t mask)
644 +{
645 +       unsigned int bits = 32;
646 +       ip_set_ip_t maskaddr;
647 +       
648 +       if (mask == 0xFFFFFFFF)
649 +               return bits;
650 +       
651 +       maskaddr = 0xFFFFFFFE;
652 +       while (--bits >= 0 && maskaddr != mask)
653 +               maskaddr <<= 1;
654 +       
655 +       return bits;
656 +}
657 +
658 +ip_set_ip_t
659 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
660 +{
661 +       ip_set_ip_t mask = 0xFFFFFFFE;
662 +       
663 +       *bits = 32;
664 +       while (--(*bits) >= 0 && mask && (to & mask) != from)
665 +               mask <<= 1;
666 +               
667 +       return mask;
668 +}
669 +       
670 +#endif /* __IP_SET_IPMAP_H */
671 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_iptree.h
672 ===================================================================
673 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
674 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_iptree.h 2007-12-15 05:20:07.884289543 +0100
675 @@ -0,0 +1,39 @@
676 +#ifndef __IP_SET_IPTREE_H
677 +#define __IP_SET_IPTREE_H
678 +
679 +#include <linux/netfilter_ipv4/ip_set.h>
680 +
681 +#define SETTYPE_NAME "iptree"
682 +#define MAX_RANGE 0x0000FFFF
683 +
684 +struct ip_set_iptreed {
685 +       unsigned long expires[255];             /* x.x.x.ADDR */
686 +};
687 +
688 +struct ip_set_iptreec {
689 +       struct ip_set_iptreed *tree[255];       /* x.x.ADDR.* */
690 +};
691 +
692 +struct ip_set_iptreeb {
693 +       struct ip_set_iptreec *tree[255];       /* x.ADDR.*.* */
694 +};
695 +
696 +struct ip_set_iptree {
697 +       unsigned int timeout;
698 +       unsigned int gc_interval;
699 +#ifdef __KERNEL__
700 +       struct timer_list gc;
701 +       struct ip_set_iptreeb *tree[255];       /* ADDR.*.*.* */
702 +#endif
703 +};
704 +
705 +struct ip_set_req_iptree_create {
706 +       unsigned int timeout;
707 +};
708 +
709 +struct ip_set_req_iptree {
710 +       ip_set_ip_t ip;
711 +       unsigned int timeout;
712 +};
713 +
714 +#endif /* __IP_SET_IPTREE_H */
715 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_jhash.h
716 ===================================================================
717 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
718 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_jhash.h  2007-12-15 05:20:07.884289543 +0100
719 @@ -0,0 +1,148 @@
720 +#ifndef _LINUX_IPSET_JHASH_H
721 +#define _LINUX_IPSET_JHASH_H
722 +
723 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
724 + * to __u32/__u8 so that the header file can be included into
725 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
726 + */
727 +
728 +/* jhash.h: Jenkins hash support.
729 + *
730 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
731 + *
732 + * http://burtleburtle.net/bob/hash/
733 + *
734 + * These are the credits from Bob's sources:
735 + *
736 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
737 + * hash(), hash2(), hash3, and mix() are externally useful functions.
738 + * Routines to test the hash are included if SELF_TEST is defined.
739 + * You can use this free for any purpose.  It has no warranty.
740 + *
741 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
742 + *
743 + * I've modified Bob's hash to be useful in the Linux kernel, and
744 + * any bugs present are surely my fault.  -DaveM
745 + */
746 +
747 +/* NOTE: Arguments are modified. */
748 +#define __jhash_mix(a, b, c) \
749 +{ \
750 +  a -= b; a -= c; a ^= (c>>13); \
751 +  b -= c; b -= a; b ^= (a<<8); \
752 +  c -= a; c -= b; c ^= (b>>13); \
753 +  a -= b; a -= c; a ^= (c>>12);  \
754 +  b -= c; b -= a; b ^= (a<<16); \
755 +  c -= a; c -= b; c ^= (b>>5); \
756 +  a -= b; a -= c; a ^= (c>>3);  \
757 +  b -= c; b -= a; b ^= (a<<10); \
758 +  c -= a; c -= b; c ^= (b>>15); \
759 +}
760 +
761 +/* The golden ration: an arbitrary value */
762 +#define JHASH_GOLDEN_RATIO     0x9e3779b9
763 +
764 +/* The most generic version, hashes an arbitrary sequence
765 + * of bytes.  No alignment or length assumptions are made about
766 + * the input key.
767 + */
768 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
769 +{
770 +       __u32 a, b, c, len;
771 +       __u8 *k = key;
772 +
773 +       len = length;
774 +       a = b = JHASH_GOLDEN_RATIO;
775 +       c = initval;
776 +
777 +       while (len >= 12) {
778 +               a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
779 +               b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
780 +               c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
781 +
782 +               __jhash_mix(a,b,c);
783 +
784 +               k += 12;
785 +               len -= 12;
786 +       }
787 +
788 +       c += length;
789 +       switch (len) {
790 +       case 11: c += ((__u32)k[10]<<24);
791 +       case 10: c += ((__u32)k[9]<<16);
792 +       case 9 : c += ((__u32)k[8]<<8);
793 +       case 8 : b += ((__u32)k[7]<<24);
794 +       case 7 : b += ((__u32)k[6]<<16);
795 +       case 6 : b += ((__u32)k[5]<<8);
796 +       case 5 : b += k[4];
797 +       case 4 : a += ((__u32)k[3]<<24);
798 +       case 3 : a += ((__u32)k[2]<<16);
799 +       case 2 : a += ((__u32)k[1]<<8);
800 +       case 1 : a += k[0];
801 +       };
802 +
803 +       __jhash_mix(a,b,c);
804 +
805 +       return c;
806 +}
807 +
808 +/* A special optimized version that handles 1 or more of __u32s.
809 + * The length parameter here is the number of __u32s in the key.
810 + */
811 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
812 +{
813 +       __u32 a, b, c, len;
814 +
815 +       a = b = JHASH_GOLDEN_RATIO;
816 +       c = initval;
817 +       len = length;
818 +
819 +       while (len >= 3) {
820 +               a += k[0];
821 +               b += k[1];
822 +               c += k[2];
823 +               __jhash_mix(a, b, c);
824 +               k += 3; len -= 3;
825 +       }
826 +
827 +       c += length * 4;
828 +
829 +       switch (len) {
830 +       case 2 : b += k[1];
831 +       case 1 : a += k[0];
832 +       };
833 +
834 +       __jhash_mix(a,b,c);
835 +
836 +       return c;
837 +}
838 +
839 +
840 +/* A special ultra-optimized versions that knows they are hashing exactly
841 + * 3, 2 or 1 word(s).
842 + *
843 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
844 + *       done at the end is not done here.
845 + */
846 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
847 +{
848 +       a += JHASH_GOLDEN_RATIO;
849 +       b += JHASH_GOLDEN_RATIO;
850 +       c += initval;
851 +
852 +       __jhash_mix(a, b, c);
853 +
854 +       return c;
855 +}
856 +
857 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
858 +{
859 +       return jhash_3words(a, b, 0, initval);
860 +}
861 +
862 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
863 +{
864 +       return jhash_3words(a, 0, 0, initval);
865 +}
866 +
867 +#endif /* _LINUX_IPSET_JHASH_H */
868 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_macipmap.h
869 ===================================================================
870 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
871 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_macipmap.h       2007-12-15 05:20:07.884289543 +0100
872 @@ -0,0 +1,38 @@
873 +#ifndef __IP_SET_MACIPMAP_H
874 +#define __IP_SET_MACIPMAP_H
875 +
876 +#include <linux/netfilter_ipv4/ip_set.h>
877 +
878 +#define SETTYPE_NAME "macipmap"
879 +#define MAX_RANGE 0x0000FFFF
880 +
881 +/* general flags */
882 +#define IPSET_MACIP_MATCHUNSET 1
883 +
884 +/* per ip flags */
885 +#define IPSET_MACIP_ISSET      1
886 +
887 +struct ip_set_macipmap {
888 +       void *members;                  /* the macipmap proper */
889 +       ip_set_ip_t first_ip;           /* host byte order, included in range */
890 +       ip_set_ip_t last_ip;            /* host byte order, included in range */
891 +       u_int32_t flags;
892 +};
893 +
894 +struct ip_set_req_macipmap_create {
895 +       ip_set_ip_t from;
896 +       ip_set_ip_t to;
897 +       u_int32_t flags;
898 +};
899 +
900 +struct ip_set_req_macipmap {
901 +       ip_set_ip_t ip;
902 +       unsigned char ethernet[ETH_ALEN];
903 +};
904 +
905 +struct ip_set_macip {
906 +       unsigned short flags;
907 +       unsigned char ethernet[ETH_ALEN];
908 +};
909 +
910 +#endif /* __IP_SET_MACIPMAP_H */
911 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_malloc.h
912 ===================================================================
913 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
914 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_malloc.h 2007-12-15 05:20:07.888289773 +0100
915 @@ -0,0 +1,27 @@
916 +#ifndef _IP_SET_MALLOC_H
917 +#define _IP_SET_MALLOC_H
918 +
919 +#ifdef __KERNEL__
920 +
921 +/* Memory allocation and deallocation */
922 +static size_t max_malloc_size = 131072;                /* Guaranteed: slab.c */
923 +
924 +static inline void * ip_set_malloc(size_t bytes)
925 +{
926 +       if (bytes > max_malloc_size)
927 +               return vmalloc(bytes);
928 +       else
929 +               return kmalloc(bytes, GFP_KERNEL);
930 +}
931 +
932 +static inline void ip_set_free(void * data, size_t bytes)
933 +{
934 +       if (bytes > max_malloc_size)
935 +               vfree(data);
936 +       else
937 +               kfree(data);
938 +}
939 +
940 +#endif                         /* __KERNEL__ */
941 +
942 +#endif /*_IP_SET_MALLOC_H*/
943 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_nethash.h
944 ===================================================================
945 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
946 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_nethash.h        2007-12-15 05:20:07.888289773 +0100
947 @@ -0,0 +1,55 @@
948 +#ifndef __IP_SET_NETHASH_H
949 +#define __IP_SET_NETHASH_H
950 +
951 +#include <linux/netfilter_ipv4/ip_set.h>
952 +
953 +#define SETTYPE_NAME "nethash"
954 +#define MAX_RANGE 0x0000FFFF
955 +
956 +struct ip_set_nethash {
957 +       ip_set_ip_t *members;           /* the nethash proper */
958 +       uint32_t initval;               /* initval for jhash_1word */
959 +       uint32_t prime;                 /* prime for double hashing */
960 +       uint32_t hashsize;              /* hash size */
961 +       uint16_t probes;                /* max number of probes  */
962 +       uint16_t resize;                /* resize factor in percent */
963 +       unsigned char cidr[30];         /* CIDR sizes */
964 +};
965 +
966 +struct ip_set_req_nethash_create {
967 +       uint32_t hashsize;
968 +       uint16_t probes;
969 +       uint16_t resize;
970 +};
971 +
972 +struct ip_set_req_nethash {
973 +       ip_set_ip_t ip;
974 +       unsigned char cidr;
975 +};
976 +
977 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
978 +
979 +static inline ip_set_ip_t 
980 +pack(ip_set_ip_t ip, unsigned char cidr)
981 +{
982 +       ip_set_ip_t addr, *paddr = &addr;
983 +       unsigned char n, t, *a;
984 +
985 +       addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
986 +#ifdef __KERNEL__
987 +       DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
988 +#endif
989 +       n = cidr / 8;
990 +       t = cidr % 8;   
991 +       a = &((unsigned char *)paddr)[n];
992 +       *a = *a /(1 << (8 - t)) + shifts[t];
993 +#ifdef __KERNEL__
994 +       DP("n: %u, t: %u, a: %u", n, t, *a);
995 +       DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
996 +          HIPQUAD(ip), cidr, NIPQUAD(addr));
997 +#endif
998 +
999 +       return ntohl(addr);
1000 +}
1001 +
1002 +#endif /* __IP_SET_NETHASH_H */
1003 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_portmap.h
1004 ===================================================================
1005 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1006 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_portmap.h        2007-12-15 05:20:07.888289773 +0100
1007 @@ -0,0 +1,25 @@
1008 +#ifndef __IP_SET_PORTMAP_H
1009 +#define __IP_SET_PORTMAP_H
1010 +
1011 +#include <linux/netfilter_ipv4/ip_set.h>
1012 +
1013 +#define SETTYPE_NAME   "portmap"
1014 +#define MAX_RANGE      0x0000FFFF
1015 +#define INVALID_PORT   (MAX_RANGE + 1)
1016 +
1017 +struct ip_set_portmap {
1018 +       void *members;                  /* the portmap proper */
1019 +       ip_set_ip_t first_port;         /* host byte order, included in range */
1020 +       ip_set_ip_t last_port;          /* host byte order, included in range */
1021 +};
1022 +
1023 +struct ip_set_req_portmap_create {
1024 +       ip_set_ip_t from;
1025 +       ip_set_ip_t to;
1026 +};
1027 +
1028 +struct ip_set_req_portmap {
1029 +       ip_set_ip_t port;
1030 +};
1031 +
1032 +#endif /* __IP_SET_PORTMAP_H */
1033 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_prime.h
1034 ===================================================================
1035 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1036 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_prime.h  2007-12-15 05:20:07.888289773 +0100
1037 @@ -0,0 +1,34 @@
1038 +#ifndef __IP_SET_PRIME_H
1039 +#define __IP_SET_PRIME_H
1040 +
1041 +static inline unsigned make_prime_bound(unsigned nr)
1042 +{
1043 +       unsigned long long nr64 = nr;
1044 +       unsigned long long x = 1;
1045 +       nr = 1;
1046 +       while (x <= nr64) { x <<= 2; nr <<= 1; }
1047 +       return nr;
1048 +}
1049 +
1050 +static inline int make_prime_check(unsigned nr)
1051 +{
1052 +       unsigned x = 3;
1053 +       unsigned b = make_prime_bound(nr);
1054 +       while (x <= b) {
1055 +               if (0 == (nr % x)) return 0;
1056 +               x += 2;
1057 +       }
1058 +       return 1;
1059 +}
1060 +
1061 +static unsigned make_prime(unsigned nr)
1062 +{
1063 +       if (0 == (nr & 1)) nr--;
1064 +       while (nr > 1) {
1065 +               if (make_prime_check(nr)) return nr;
1066 +               nr -= 2;
1067 +       }
1068 +       return 2;
1069 +}
1070 +
1071 +#endif /* __IP_SET_PRIME_H */
1072 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ipt_set.h
1073 ===================================================================
1074 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1075 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ipt_set.h       2007-12-15 05:20:07.888289773 +0100
1076 @@ -0,0 +1,21 @@
1077 +#ifndef _IPT_SET_H
1078 +#define _IPT_SET_H
1079 +
1080 +#include <linux/netfilter_ipv4/ip_set.h>
1081 +
1082 +struct ipt_set_info {
1083 +       ip_set_id_t index;
1084 +       u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1085 +};
1086 +
1087 +/* match info */
1088 +struct ipt_set_info_match {
1089 +       struct ipt_set_info match_set;
1090 +};
1091 +
1092 +struct ipt_set_info_target {
1093 +       struct ipt_set_info add_set;
1094 +       struct ipt_set_info del_set;
1095 +};
1096 +
1097 +#endif /*_IPT_SET_H*/
1098 Index: linux-2.4.35.4/net/ipv4/netfilter/Config.in
1099 ===================================================================
1100 --- linux-2.4.35.4.orig/net/ipv4/netfilter/Config.in    2007-12-15 05:20:07.568271536 +0100
1101 +++ linux-2.4.35.4/net/ipv4/netfilter/Config.in 2007-12-15 05:20:07.892290000 +0100
1102 @@ -22,6 +22,20 @@
1103  if [ "$CONFIG_IP_NF_IPTABLES" != "n" ]; then
1104  # The simple matches.
1105    dep_tristate '  limit match support' CONFIG_IP_NF_MATCH_LIMIT $CONFIG_IP_NF_IPTABLES
1106 +
1107 +  dep_tristate '  IP set support' CONFIG_IP_NF_SET $CONFIG_IP_NF_IPTABLES
1108 +  if [ "$CONFIG_IP_NF_SET" != "n" ]; then
1109 +    int '     Maximum number of sets' CONFIG_IP_NF_SET_MAX 256
1110 +    int '     Hash size for bindings of IP sets' CONFIG_IP_NF_SET_HASHSIZE 1024
1111 +    dep_tristate '    set match support' CONFIG_IP_NF_MATCH_SET $CONFIG_IP_NF_SET
1112 +    dep_tristate '    SET target support' CONFIG_IP_NF_TARGET_SET $CONFIG_IP_NF_SET
1113 +    dep_tristate '    ipmap set type support' CONFIG_IP_NF_SET_IPMAP $CONFIG_IP_NF_SET
1114 +    dep_tristate '    portmap set type support' CONFIG_IP_NF_SET_PORTMAP $CONFIG_IP_NF_SET
1115 +    dep_tristate '    macipmap set type support' CONFIG_IP_NF_SET_MACIPMAP $CONFIG_IP_NF_SET
1116 +    dep_tristate '    iphash set type support' CONFIG_IP_NF_SET_IPHASH $CONFIG_IP_NF_SET
1117 +    dep_tristate '    nethash set type support' CONFIG_IP_NF_SET_NETHASH $CONFIG_IP_NF_SET
1118 +    dep_tristate '    iptree set type support' CONFIG_IP_NF_SET_IPTREE $CONFIG_IP_NF_SET
1119 +  fi
1120    dep_tristate '  MAC address match support' CONFIG_IP_NF_MATCH_MAC $CONFIG_IP_NF_IPTABLES
1121    dep_tristate '  Packet type match support' CONFIG_IP_NF_MATCH_PKTTYPE $CONFIG_IP_NF_IPTABLES
1122    dep_tristate '  netfilter MARK match support' CONFIG_IP_NF_MATCH_MARK $CONFIG_IP_NF_IPTABLES
1123 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set.c
1124 ===================================================================
1125 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1126 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set.c  2007-12-15 05:20:07.892290000 +0100
1127 @@ -0,0 +1,2002 @@
1128 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1129 + *                         Patrick Schaaf <bof@bof.de>
1130 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1131 + *
1132 + * This program is free software; you can redistribute it and/or modify
1133 + * it under the terms of the GNU General Public License version 2 as
1134 + * published by the Free Software Foundation.  
1135 + */
1136 +
1137 +/* Kernel module for IP set management */
1138 +
1139 +#include <linux/config.h>
1140 +#include <linux/module.h>
1141 +#include <linux/kmod.h>
1142 +#include <linux/ip.h>
1143 +#include <linux/skbuff.h>
1144 +#include <linux/random.h>
1145 +#include <linux/jhash.h>
1146 +#include <linux/netfilter_ipv4/ip_tables.h>
1147 +#include <linux/errno.h>
1148 +#include <asm/uaccess.h>
1149 +#include <asm/bitops.h>
1150 +#include <asm/softirq.h>
1151 +#include <asm/semaphore.h>
1152 +#include <linux/spinlock.h>
1153 +#include <linux/vmalloc.h>
1154 +
1155 +#define ASSERT_READ_LOCK(x)    /* dont use that */
1156 +#define ASSERT_WRITE_LOCK(x)
1157 +#include <linux/netfilter_ipv4/listhelp.h>
1158 +#include <linux/netfilter_ipv4/ip_set.h>
1159 +
1160 +static struct list_head set_type_list;         /* all registered sets */
1161 +static struct ip_set **ip_set_list;            /* all individual sets */
1162 +static DECLARE_RWLOCK(ip_set_lock);            /* protects the lists and the hash */
1163 +static DECLARE_MUTEX(ip_set_app_mutex);                /* serializes user access */
1164 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1165 +static ip_set_id_t ip_set_bindings_hash_size =  CONFIG_IP_NF_SET_HASHSIZE;
1166 +static struct list_head *ip_set_hash;          /* hash of bindings */
1167 +static unsigned int ip_set_hash_random;                /* random seed */
1168 +
1169 +/* Arrgh */
1170 +#ifdef MODULE
1171 +#define __MOD_INC(foo)                 __MOD_INC_USE_COUNT(foo)
1172 +#define __MOD_DEC(foo)                 __MOD_DEC_USE_COUNT(foo)
1173 +#define __MOD_INC_SELF         MOD_INC_USE_COUNT
1174 +#define __MOD_DEC_SELF         MOD_DEC_USE_COUNT
1175 +#else
1176 +#define __MOD_INC(foo)
1177 +#define __MOD_DEC(foo)
1178 +#define __MOD_INC_SELF
1179 +#define __MOD_DEC_SELF
1180 +#endif
1181 +
1182 +/*
1183 + * Sets are identified either by the index in ip_set_list or by id.
1184 + * The id never changes and is used to find a key in the hash. 
1185 + * The index may change by swapping and used at all other places 
1186 + * (set/SET netfilter modules, binding value, etc.)
1187 + *
1188 + * Userspace requests are serialized by ip_set_mutex and sets can
1189 + * be deleted only from userspace. Therefore ip_set_list locking 
1190 + * must obey the following rules:
1191 + *
1192 + * - kernel requests: read and write locking mandatory
1193 + * - user requests: read locking optional, write locking mandatory
1194 + */
1195 +
1196 +static inline void
1197 +__ip_set_get(ip_set_id_t index)
1198 +{
1199 +       atomic_inc(&ip_set_list[index]->ref);
1200 +}
1201 +
1202 +static inline void
1203 +__ip_set_put(ip_set_id_t index)
1204 +{
1205 +       atomic_dec(&ip_set_list[index]->ref);
1206 +}
1207 +
1208 +/*
1209 + * Binding routines
1210 + */
1211 +
1212 +static inline int
1213 +ip_hash_cmp(const struct ip_set_hash *set_hash,
1214 +           ip_set_id_t id, ip_set_ip_t ip)
1215 +{
1216 +       return set_hash->id == id && set_hash->ip == ip;
1217 +}
1218 +
1219 +static ip_set_id_t
1220 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1221 +{
1222 +       u_int32_t key = jhash_2words(id, ip, ip_set_hash_random) 
1223 +                               % ip_set_bindings_hash_size;
1224 +       struct ip_set_hash *set_hash;
1225 +
1226 +       MUST_BE_READ_LOCKED(&ip_set_lock);
1227 +       IP_SET_ASSERT(ip_set_list[id]);
1228 +       DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));     
1229 +       
1230 +       set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1231 +                            struct ip_set_hash *, id, ip);
1232 +       
1233 +       DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name, 
1234 +          HIPQUAD(ip),
1235 +          set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1236 +
1237 +       return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1238 +}
1239 +
1240 +static inline void 
1241 +__set_hash_del(struct ip_set_hash *set_hash)
1242 +{
1243 +       MUST_BE_WRITE_LOCKED(&ip_set_lock);
1244 +       IP_SET_ASSERT(ip_set_list[set_hash->binding]);  
1245 +
1246 +       __ip_set_put(set_hash->binding);
1247 +       list_del(&set_hash->list);
1248 +       kfree(set_hash);
1249 +}
1250 +
1251 +static int
1252 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1253 +{
1254 +       u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1255 +                               % ip_set_bindings_hash_size;
1256 +       struct ip_set_hash *set_hash;
1257 +       
1258 +       IP_SET_ASSERT(ip_set_list[id]);
1259 +       DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));     
1260 +       WRITE_LOCK(&ip_set_lock);
1261 +       set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1262 +                            struct ip_set_hash *, id, ip);
1263 +       DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1264 +          HIPQUAD(ip),
1265 +          set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1266 +
1267 +       if (set_hash != NULL)
1268 +               __set_hash_del(set_hash);
1269 +       WRITE_UNLOCK(&ip_set_lock);
1270 +       return 0;
1271 +}
1272 +
1273 +static int 
1274 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1275 +{
1276 +       u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1277 +                               % ip_set_bindings_hash_size;
1278 +       struct ip_set_hash *set_hash;
1279 +       int ret = 0;
1280 +       
1281 +       IP_SET_ASSERT(ip_set_list[id]);
1282 +       IP_SET_ASSERT(ip_set_list[binding]);
1283 +       DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name, 
1284 +          HIPQUAD(ip), ip_set_list[binding]->name);
1285 +       WRITE_LOCK(&ip_set_lock);
1286 +       set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1287 +                            struct ip_set_hash *, id, ip);
1288 +       if (!set_hash) {
1289 +               set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_KERNEL);
1290 +               if (!set_hash) {
1291 +                       ret = -ENOMEM;
1292 +                       goto unlock;
1293 +               }
1294 +               INIT_LIST_HEAD(&set_hash->list);
1295 +               set_hash->id = id;
1296 +               set_hash->ip = ip;
1297 +               list_add(&ip_set_hash[key], &set_hash->list);
1298 +       } else {
1299 +               IP_SET_ASSERT(ip_set_list[set_hash->binding]);  
1300 +               DP("overwrite binding: %s",
1301 +                  ip_set_list[set_hash->binding]->name);
1302 +               __ip_set_put(set_hash->binding);
1303 +       }
1304 +       set_hash->binding = binding;
1305 +       __ip_set_get(set_hash->binding);
1306 +    unlock:
1307 +       WRITE_UNLOCK(&ip_set_lock);
1308 +       return ret;
1309 +}
1310 +
1311 +#define FOREACH_HASH_DO(fn, args...)                                           \
1312 +({                                                                             \
1313 +       ip_set_id_t __key;                                                      \
1314 +       struct ip_set_hash *__set_hash;                                         \
1315 +                                                                               \
1316 +       for (__key = 0; __key < ip_set_bindings_hash_size; __key++) {           \
1317 +               list_for_each_entry(__set_hash, &ip_set_hash[__key], list)      \
1318 +                       fn(__set_hash , ## args);                               \
1319 +       }                                                                       \
1320 +})
1321 +
1322 +#define FOREACH_HASH_RW_DO(fn, args...)                                                \
1323 +({                                                                             \
1324 +       ip_set_id_t __key;                                                      \
1325 +       struct ip_set_hash *__set_hash, *__n;                                   \
1326 +                                                                               \
1327 +       MUST_BE_WRITE_LOCKED(&ip_set_lock);                                     \
1328 +       for (__key = 0; __key < ip_set_bindings_hash_size; __key++) {           \
1329 +               list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1330 +                       fn(__set_hash , ## args);                               \
1331 +       }                                                                       \
1332 +})
1333 +
1334 +/* Add, del and test set entries from kernel */
1335 +
1336 +#define follow_bindings(index, set, ip)                                        \
1337 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID     \
1338 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1339 +
1340 +int
1341 +ip_set_testip_kernel(ip_set_id_t index,
1342 +                    const struct sk_buff *skb,
1343 +                    const u_int32_t *flags)
1344 +{
1345 +       struct ip_set *set;
1346 +       ip_set_ip_t ip;
1347 +       int res, i = 0;
1348 +       
1349 +       IP_SET_ASSERT(flags[i]);
1350 +       READ_LOCK(&ip_set_lock);
1351 +       do {
1352 +               set = ip_set_list[index];
1353 +               IP_SET_ASSERT(set);
1354 +               DP("set %s, index %u", set->name, index);
1355 +               read_lock_bh(&set->lock);
1356 +               res = set->type->testip_kernel(set, skb, flags[i], &ip);
1357 +               read_unlock_bh(&set->lock);
1358 +       } while (res > 0 
1359 +                && flags[++i] 
1360 +                && follow_bindings(index, set, ip));
1361 +       READ_UNLOCK(&ip_set_lock);
1362 +
1363 +       return res;
1364 +}
1365 +
1366 +void
1367 +ip_set_addip_kernel(ip_set_id_t index,
1368 +                   const struct sk_buff *skb,
1369 +                   const u_int32_t *flags)
1370 +{
1371 +       struct ip_set *set;
1372 +       ip_set_ip_t ip;
1373 +       int res, i= 0;
1374 +
1375 +       IP_SET_ASSERT(flags[i]);
1376 +    retry:
1377 +       READ_LOCK(&ip_set_lock);
1378 +       do {
1379 +               set = ip_set_list[index];
1380 +               IP_SET_ASSERT(set);
1381 +               DP("set %s, index %u", set->name, index);
1382 +               write_lock_bh(&set->lock);
1383 +               res = set->type->addip_kernel(set, skb, flags[i], &ip);
1384 +               write_unlock_bh(&set->lock);
1385 +       } while ((res == 0 || res == -EEXIST)
1386 +                && flags[++i] 
1387 +                && follow_bindings(index, set, ip));
1388 +       READ_UNLOCK(&ip_set_lock);
1389 +       
1390 +       if (res == -EAGAIN
1391 +           && set->type->retry
1392 +           && (res = set->type->retry(set)) == 0)
1393 +               goto retry;
1394 +}
1395 +
1396 +void
1397 +ip_set_delip_kernel(ip_set_id_t index,
1398 +                   const struct sk_buff *skb,
1399 +                   const u_int32_t *flags)
1400 +{
1401 +       struct ip_set *set;
1402 +       ip_set_ip_t ip;
1403 +       int res, i = 0;
1404 +
1405 +       IP_SET_ASSERT(flags[i]);
1406 +       READ_LOCK(&ip_set_lock);
1407 +       do {
1408 +               set = ip_set_list[index];
1409 +               IP_SET_ASSERT(set);
1410 +               DP("set %s, index %u", set->name, index);
1411 +               write_lock_bh(&set->lock);
1412 +               res = set->type->delip_kernel(set, skb, flags[i], &ip);
1413 +               write_unlock_bh(&set->lock);
1414 +       } while ((res == 0 || res == -EEXIST)
1415 +                && flags[++i] 
1416 +                && follow_bindings(index, set, ip));
1417 +       READ_UNLOCK(&ip_set_lock);
1418 +}
1419 +
1420 +/* Register and deregister settype */
1421 +
1422 +static inline int
1423 +set_type_equal(const struct ip_set_type *set_type, const char *str2)
1424 +{
1425 +       return !strncmp(set_type->typename, str2, IP_SET_MAXNAMELEN - 1);
1426 +}
1427 +
1428 +static inline struct ip_set_type *
1429 +find_set_type(const char *name)
1430 +{
1431 +       return LIST_FIND(&set_type_list,
1432 +                        set_type_equal,
1433 +                        struct ip_set_type *,
1434 +                        name);
1435 +}
1436 +
1437 +int 
1438 +ip_set_register_set_type(struct ip_set_type *set_type)
1439 +{
1440 +       int ret = 0;
1441 +       
1442 +       if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1443 +               ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1444 +                             set_type->typename,
1445 +                             set_type->protocol_version,
1446 +                             IP_SET_PROTOCOL_VERSION);
1447 +               return -EINVAL;
1448 +       }
1449 +
1450 +       WRITE_LOCK(&ip_set_lock);
1451 +       if (find_set_type(set_type->typename)) {
1452 +               /* Duplicate! */
1453 +               ip_set_printk("'%s' already registered!", 
1454 +                             set_type->typename);
1455 +               ret = -EINVAL;
1456 +               goto unlock;
1457 +       }
1458 +       __MOD_INC_SELF;
1459 +       list_append(&set_type_list, set_type);
1460 +       DP("'%s' registered.", set_type->typename);
1461 +   unlock:
1462 +       WRITE_UNLOCK(&ip_set_lock);
1463 +       return ret;
1464 +}
1465 +
1466 +void
1467 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1468 +{
1469 +       WRITE_LOCK(&ip_set_lock);
1470 +       if (!find_set_type(set_type->typename)) {
1471 +               ip_set_printk("'%s' not registered?",
1472 +                             set_type->typename);
1473 +               goto unlock;
1474 +       }
1475 +       LIST_DELETE(&set_type_list, set_type);
1476 +       __MOD_DEC_SELF;
1477 +       DP("'%s' unregistered.", set_type->typename);
1478 +   unlock:
1479 +       WRITE_UNLOCK(&ip_set_lock);
1480 +
1481 +}
1482 +
1483 +/*
1484 + * Userspace routines
1485 + */
1486 +
1487 +/*
1488 + * Find set by name, reference it once. The reference makes sure the
1489 + * thing pointed to, does not go away under our feet. Drop the reference
1490 + * later, using ip_set_put().
1491 + */
1492 +ip_set_id_t
1493 +ip_set_get_byname(const char *name)
1494 +{
1495 +       ip_set_id_t i, index = IP_SET_INVALID_ID;
1496 +       
1497 +       down(&ip_set_app_mutex);
1498 +       for (i = 0; i < ip_set_max; i++) {
1499 +               if (ip_set_list[i] != NULL
1500 +                   && strcmp(ip_set_list[i]->name, name) == 0) {
1501 +                       __ip_set_get(i);
1502 +                       index = i;
1503 +                       break;
1504 +               }
1505 +       }
1506 +       up(&ip_set_app_mutex);
1507 +       return index;
1508 +}
1509 +
1510 +/*
1511 + * Find set by index, reference it once. The reference makes sure the
1512 + * thing pointed to, does not go away under our feet. Drop the reference
1513 + * later, using ip_set_put().
1514 + */
1515 +ip_set_id_t
1516 +ip_set_get_byindex(ip_set_id_t index)
1517 +{
1518 +       down(&ip_set_app_mutex);
1519 +
1520 +       if (index >= ip_set_max)
1521 +               return IP_SET_INVALID_ID;
1522 +       
1523 +       if (ip_set_list[index])
1524 +               __ip_set_get(index);
1525 +       else
1526 +               index = IP_SET_INVALID_ID;
1527 +               
1528 +       up(&ip_set_app_mutex);
1529 +       return index;
1530 +}
1531 +
1532 +/*
1533 + * If the given set pointer points to a valid set, decrement
1534 + * reference count by 1. The caller shall not assume the index
1535 + * to be valid, after calling this function.
1536 + */
1537 +void ip_set_put(ip_set_id_t index)
1538 +{
1539 +       down(&ip_set_app_mutex);
1540 +       if (ip_set_list[index])
1541 +               __ip_set_put(index);
1542 +       up(&ip_set_app_mutex);
1543 +}
1544 +
1545 +/* Find a set by name or index */
1546 +static ip_set_id_t
1547 +ip_set_find_byname(const char *name)
1548 +{
1549 +       ip_set_id_t i, index = IP_SET_INVALID_ID;
1550 +       
1551 +       for (i = 0; i < ip_set_max; i++) {
1552 +               if (ip_set_list[i] != NULL
1553 +                   && strcmp(ip_set_list[i]->name, name) == 0) {
1554 +                       index = i;
1555 +                       break;
1556 +               }
1557 +       }
1558 +       return index;
1559 +}
1560 +
1561 +static ip_set_id_t
1562 +ip_set_find_byindex(ip_set_id_t index)
1563 +{
1564 +       if (index >= ip_set_max || ip_set_list[index] == NULL)
1565 +               index = IP_SET_INVALID_ID;
1566 +       
1567 +       return index;
1568 +}
1569 +
1570 +/*
1571 + * Add, del, test, bind and unbind
1572 + */
1573 +
1574 +static inline int
1575 +__ip_set_testip(struct ip_set *set,
1576 +               const void *data,
1577 +               size_t size,
1578 +               ip_set_ip_t *ip)
1579 +{
1580 +       int res;
1581 +
1582 +       read_lock_bh(&set->lock);
1583 +       res = set->type->testip(set, data, size, ip);
1584 +       read_unlock_bh(&set->lock);
1585 +
1586 +       return res;
1587 +}
1588 +
1589 +static int
1590 +__ip_set_addip(ip_set_id_t index,
1591 +              const void *data,
1592 +              size_t size)
1593 +{
1594 +       struct ip_set *set = ip_set_list[index];
1595 +       ip_set_ip_t ip;
1596 +       int res;
1597 +       
1598 +       IP_SET_ASSERT(set);
1599 +       do {
1600 +               write_lock_bh(&set->lock);
1601 +               res = set->type->addip(set, data, size, &ip);
1602 +               write_unlock_bh(&set->lock);
1603 +       } while (res == -EAGAIN
1604 +                && set->type->retry
1605 +                && (res = set->type->retry(set)) == 0);
1606 +
1607 +       return res;
1608 +}
1609 +
1610 +static int
1611 +ip_set_addip(ip_set_id_t index,
1612 +            const void *data,
1613 +            size_t size)
1614 +{
1615 +
1616 +       return __ip_set_addip(index,
1617 +                             data + sizeof(struct ip_set_req_adt),
1618 +                             size - sizeof(struct ip_set_req_adt));
1619 +}
1620 +
1621 +static int
1622 +ip_set_delip(ip_set_id_t index,
1623 +            const void *data,
1624 +            size_t size)
1625 +{
1626 +       struct ip_set *set = ip_set_list[index];
1627 +       ip_set_ip_t ip;
1628 +       int res;
1629 +       
1630 +       IP_SET_ASSERT(set);
1631 +       write_lock_bh(&set->lock);
1632 +       res = set->type->delip(set,
1633 +                              data + sizeof(struct ip_set_req_adt),
1634 +                              size - sizeof(struct ip_set_req_adt),
1635 +                              &ip);
1636 +       write_unlock_bh(&set->lock);
1637 +
1638 +       return res;
1639 +}
1640 +
1641 +static int
1642 +ip_set_testip(ip_set_id_t index,
1643 +             const void *data,
1644 +             size_t size)
1645 +{
1646 +       struct ip_set *set = ip_set_list[index];
1647 +       ip_set_ip_t ip;
1648 +       int res;
1649 +
1650 +       IP_SET_ASSERT(set);
1651 +       res = __ip_set_testip(set,
1652 +                             data + sizeof(struct ip_set_req_adt),
1653 +                             size - sizeof(struct ip_set_req_adt),
1654 +                             &ip);
1655 +
1656 +       return (res > 0 ? -EEXIST : res);
1657 +}
1658 +
1659 +static int
1660 +ip_set_bindip(ip_set_id_t index,
1661 +             const void *data,
1662 +             size_t size)
1663 +{
1664 +       struct ip_set *set = ip_set_list[index];
1665 +       struct ip_set_req_bind *req_bind;
1666 +       ip_set_id_t binding;
1667 +       ip_set_ip_t ip;
1668 +       int res;
1669 +
1670 +       IP_SET_ASSERT(set);
1671 +       if (size < sizeof(struct ip_set_req_bind))
1672 +               return -EINVAL;
1673 +               
1674 +       req_bind = (struct ip_set_req_bind *) data;
1675 +       req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1676 +
1677 +       if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1678 +               /* Default binding of a set */
1679 +               char *binding_name;
1680 +               
1681 +               if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1682 +                       return -EINVAL;
1683 +
1684 +               binding_name = (char *)(data + sizeof(struct ip_set_req_bind)); 
1685 +               binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1686 +
1687 +               binding = ip_set_find_byname(binding_name);
1688 +               if (binding == IP_SET_INVALID_ID)
1689 +                       return -ENOENT;
1690 +
1691 +               WRITE_LOCK(&ip_set_lock);
1692 +               /* Sets as binding values are referenced */
1693 +               if (set->binding != IP_SET_INVALID_ID)
1694 +                       __ip_set_put(set->binding);
1695 +               set->binding = binding;
1696 +               __ip_set_get(set->binding);
1697 +               WRITE_UNLOCK(&ip_set_lock);
1698 +
1699 +               return 0;
1700 +       }
1701 +       binding = ip_set_find_byname(req_bind->binding);
1702 +       if (binding == IP_SET_INVALID_ID)
1703 +               return -ENOENT;
1704 +
1705 +       res = __ip_set_testip(set,
1706 +                             data + sizeof(struct ip_set_req_bind),
1707 +                             size - sizeof(struct ip_set_req_bind),
1708 +                             &ip);
1709 +       DP("set %s, ip: %u.%u.%u.%u, binding %s",
1710 +          set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1711 +       
1712 +       if (res >= 0)
1713 +               res = ip_set_hash_add(set->id, ip, binding);
1714 +
1715 +       return res;
1716 +}
1717 +
1718 +#define FOREACH_SET_DO(fn, args...)                            \
1719 +({                                                             \
1720 +       ip_set_id_t __i;                                        \
1721 +       struct ip_set *__set;                                   \
1722 +                                                               \
1723 +       for (__i = 0; __i < ip_set_max; __i++) {                \
1724 +               __set = ip_set_list[__i];                       \
1725 +               if (__set != NULL)                              \
1726 +                       fn(__set , ##args);                     \
1727 +       }                                                       \
1728 +})
1729 +
1730 +static inline void
1731 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1732 +{
1733 +       if (set_hash->id == id)
1734 +               __set_hash_del(set_hash);
1735 +}
1736 +
1737 +static inline void
1738 +__unbind_default(struct ip_set *set)
1739 +{
1740 +       if (set->binding != IP_SET_INVALID_ID) {
1741 +               /* Sets as binding values are referenced */
1742 +               __ip_set_put(set->binding);
1743 +               set->binding = IP_SET_INVALID_ID;
1744 +       }
1745 +}
1746 +
1747 +static int
1748 +ip_set_unbindip(ip_set_id_t index,
1749 +               const void *data,
1750 +               size_t size)
1751 +{
1752 +       struct ip_set *set;
1753 +       struct ip_set_req_bind *req_bind;
1754 +       ip_set_ip_t ip;
1755 +       int res;
1756 +
1757 +       DP("");
1758 +       if (size < sizeof(struct ip_set_req_bind))
1759 +               return -EINVAL;
1760 +               
1761 +       req_bind = (struct ip_set_req_bind *) data;
1762 +       req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1763 +       
1764 +       DP("%u %s", index, req_bind->binding);
1765 +       if (index == IP_SET_INVALID_ID) {
1766 +               /* unbind :all: */
1767 +               if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1768 +                       /* Default binding of sets */
1769 +                       WRITE_LOCK(&ip_set_lock);
1770 +                       FOREACH_SET_DO(__unbind_default);
1771 +                       WRITE_UNLOCK(&ip_set_lock);
1772 +                       return 0;
1773 +               } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1774 +                       /* Flush all bindings of all sets*/
1775 +                       WRITE_LOCK(&ip_set_lock);
1776 +                       FOREACH_HASH_RW_DO(__set_hash_del);
1777 +                       WRITE_UNLOCK(&ip_set_lock);
1778 +                       return 0;
1779 +               }
1780 +               DP("unreachable reached!");
1781 +               return -EINVAL;
1782 +       }
1783 +       
1784 +       set = ip_set_list[index];
1785 +       IP_SET_ASSERT(set);
1786 +       if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1787 +               /* Default binding of set */
1788 +               ip_set_id_t binding = ip_set_find_byindex(set->binding);
1789 +
1790 +               if (binding == IP_SET_INVALID_ID)
1791 +                       return -ENOENT;
1792 +                       
1793 +               WRITE_LOCK(&ip_set_lock);
1794 +               /* Sets in hash values are referenced */
1795 +               __ip_set_put(set->binding);
1796 +               set->binding = IP_SET_INVALID_ID;
1797 +               WRITE_UNLOCK(&ip_set_lock);
1798 +
1799 +               return 0;
1800 +       } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1801 +               /* Flush all bindings */
1802 +
1803 +               WRITE_LOCK(&ip_set_lock);
1804 +               FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1805 +               WRITE_UNLOCK(&ip_set_lock);
1806 +               return 0;
1807 +       }
1808 +       
1809 +       res = __ip_set_testip(set,
1810 +                             data + sizeof(struct ip_set_req_bind),
1811 +                             size - sizeof(struct ip_set_req_bind),
1812 +                             &ip);
1813 +
1814 +       DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1815 +       if (res >= 0)
1816 +               res = ip_set_hash_del(set->id, ip);
1817 +
1818 +       return res;
1819 +}
1820 +
1821 +static int
1822 +ip_set_testbind(ip_set_id_t index,
1823 +               const void *data,
1824 +               size_t size)
1825 +{
1826 +       struct ip_set *set = ip_set_list[index];
1827 +       struct ip_set_req_bind *req_bind;
1828 +       ip_set_id_t binding;
1829 +       ip_set_ip_t ip;
1830 +       int res;
1831 +
1832 +       IP_SET_ASSERT(set);
1833 +       if (size < sizeof(struct ip_set_req_bind))
1834 +               return -EINVAL;
1835 +               
1836 +       req_bind = (struct ip_set_req_bind *) data;
1837 +       req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1838 +
1839 +       if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1840 +               /* Default binding of set */
1841 +               char *binding_name;
1842 +               
1843 +               if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1844 +                       return -EINVAL;
1845 +
1846 +               binding_name = (char *)(data + sizeof(struct ip_set_req_bind)); 
1847 +               binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1848 +
1849 +               binding = ip_set_find_byname(binding_name);
1850 +               if (binding == IP_SET_INVALID_ID)
1851 +                       return -ENOENT;
1852 +               
1853 +               res = (set->binding == binding) ? -EEXIST : 0;
1854 +
1855 +               return res;
1856 +       }
1857 +       binding = ip_set_find_byname(req_bind->binding);
1858 +       if (binding == IP_SET_INVALID_ID)
1859 +               return -ENOENT;
1860 +               
1861 +       
1862 +       res = __ip_set_testip(set,
1863 +                             data + sizeof(struct ip_set_req_bind),
1864 +                             size - sizeof(struct ip_set_req_bind),
1865 +                             &ip);
1866 +       DP("set %s, ip: %u.%u.%u.%u, binding %s",
1867 +          set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1868 +       
1869 +       if (res >= 0)
1870 +               res = (ip_set_find_in_hash(set->id, ip) == binding)
1871 +                       ? -EEXIST : 0;
1872 +
1873 +       return res;
1874 +}
1875 +
1876 +static struct ip_set_type *
1877 +find_set_type_rlock(const char *typename)
1878 +{
1879 +       struct ip_set_type *type;
1880 +       
1881 +       READ_LOCK(&ip_set_lock);
1882 +       type = find_set_type(typename);
1883 +       if (type == NULL)
1884 +               READ_UNLOCK(&ip_set_lock);
1885 +
1886 +       return type;
1887 +}
1888 +
1889 +static int
1890 +find_free_id(const char *name,
1891 +            ip_set_id_t *index,
1892 +            ip_set_id_t *id)
1893 +{
1894 +       ip_set_id_t i;
1895 +
1896 +       *id = IP_SET_INVALID_ID;
1897 +       for (i = 0;  i < ip_set_max; i++) {
1898 +               if (ip_set_list[i] == NULL) {
1899 +                       if (*id == IP_SET_INVALID_ID)
1900 +                               *id = *index = i;
1901 +               } else if (strcmp(name, ip_set_list[i]->name) == 0)
1902 +                       /* Name clash */
1903 +                       return -EEXIST;
1904 +       }
1905 +       if (*id == IP_SET_INVALID_ID)
1906 +               /* No free slot remained */
1907 +               return -ERANGE;
1908 +       /* Check that index is usable as id (swapping) */
1909 +    check:     
1910 +       for (i = 0;  i < ip_set_max; i++) {
1911 +               if (ip_set_list[i] != NULL
1912 +                   && ip_set_list[i]->id == *id) {
1913 +                   *id = i;
1914 +                   goto check;
1915 +               }
1916 +       }
1917 +       return 0;
1918 +}
1919 +
1920 +/*
1921 + * Create a set
1922 + */
1923 +static int
1924 +ip_set_create(const char *name,
1925 +             const char *typename,
1926 +             ip_set_id_t restore,
1927 +             const void *data,
1928 +             size_t size)
1929 +{
1930 +       struct ip_set *set;
1931 +       ip_set_id_t index, id;
1932 +       int res = 0;
1933 +
1934 +       DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1935 +       /*
1936 +        * First, and without any locks, allocate and initialize
1937 +        * a normal base set structure.
1938 +        */
1939 +       set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1940 +       if (!set)
1941 +               return -ENOMEM;
1942 +       set->lock = RW_LOCK_UNLOCKED;
1943 +       strncpy(set->name, name, IP_SET_MAXNAMELEN);
1944 +       set->binding = IP_SET_INVALID_ID;
1945 +       atomic_set(&set->ref, 0);
1946 +
1947 +       /*
1948 +        * Next, take the &ip_set_lock, check that we know the type,
1949 +        * and take a reference on the type, to make sure it
1950 +        * stays available while constructing our new set.
1951 +        *
1952 +        * After referencing the type, we drop the &ip_set_lock,
1953 +        * and let the new set construction run without locks.
1954 +        */
1955 +       set->type = find_set_type_rlock(typename);
1956 +       if (set->type == NULL) {
1957 +               /* Try loading the module */
1958 +               char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1959 +               strcpy(modulename, "ip_set_");
1960 +               strcat(modulename, typename);
1961 +               DP("try to load %s", modulename);
1962 +               request_module(modulename);
1963 +               set->type = find_set_type_rlock(typename);
1964 +       }
1965 +       if (set->type == NULL) {
1966 +               ip_set_printk("no set type '%s', set '%s' not created",
1967 +                             typename, name);
1968 +               kfree(set);
1969 +               return -ENOENT;
1970 +       }
1971 +       __MOD_INC(set->type->me);
1972 +       READ_UNLOCK(&ip_set_lock);
1973 +
1974 +       /*
1975 +        * Without holding any locks, create private part.
1976 +        */
1977 +       res = set->type->create(set, data, size);
1978 +       if (res != 0) {
1979 +               __MOD_DEC(set->type->me);
1980 +               kfree(set);
1981 +               return res;
1982 +       }
1983 +
1984 +       /* BTW, res==0 here. */
1985 +
1986 +       /*
1987 +        * Here, we have a valid, constructed set. &ip_set_lock again,
1988 +        * find free id/index and check that it is not already in 
1989 +        * ip_set_list.
1990 +        */
1991 +       WRITE_LOCK(&ip_set_lock);
1992 +       if ((res = find_free_id(set->name, &index, &id)) != 0) {
1993 +               DP("no free id!");
1994 +               goto cleanup;
1995 +       }
1996 +
1997 +       /* Make sure restore gets the same index */
1998 +       if (restore != IP_SET_INVALID_ID && index != restore) {
1999 +               DP("Can't restore, sets are screwed up");
2000 +               res = -ERANGE;
2001 +               goto cleanup;
2002 +       }
2003 +        
2004 +       /*
2005 +        * Finally! Add our shiny new set to the list, and be done.
2006 +        */
2007 +       DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2008 +       set->id = id;
2009 +       ip_set_list[index] = set;
2010 +       WRITE_UNLOCK(&ip_set_lock);
2011 +       return res;
2012 +       
2013 +    cleanup:
2014 +       WRITE_UNLOCK(&ip_set_lock);
2015 +       set->type->destroy(set);
2016 +       __MOD_DEC(set->type->me);
2017 +       kfree(set);
2018 +       return res;
2019 +}
2020 +
2021 +/*
2022 + * Destroy a given existing set
2023 + */
2024 +static void
2025 +ip_set_destroy_set(ip_set_id_t index)
2026 +{
2027 +       struct ip_set *set = ip_set_list[index];
2028 +
2029 +       IP_SET_ASSERT(set);
2030 +       DP("set: %s",  set->name);
2031 +       WRITE_LOCK(&ip_set_lock);
2032 +       FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2033 +       if (set->binding != IP_SET_INVALID_ID)
2034 +               __ip_set_put(set->binding);
2035 +       ip_set_list[index] = NULL;
2036 +       WRITE_UNLOCK(&ip_set_lock);
2037 +
2038 +       /* Must call it without holding any lock */
2039 +       set->type->destroy(set);
2040 +       __MOD_DEC(set->type->me);
2041 +       kfree(set);
2042 +}
2043 +
2044 +/*
2045 + * Destroy a set - or all sets
2046 + * Sets must not be referenced/used.
2047 + */
2048 +static int
2049 +ip_set_destroy(ip_set_id_t index)
2050 +{
2051 +       ip_set_id_t i;
2052 +
2053 +       /* ref modification always protected by the mutex */
2054 +       if (index != IP_SET_INVALID_ID) {
2055 +               if (atomic_read(&ip_set_list[index]->ref))
2056 +                       return -EBUSY;
2057 +               ip_set_destroy_set(index);
2058 +       } else {
2059 +               for (i = 0; i < ip_set_max; i++) {
2060 +                       if (ip_set_list[i] != NULL 
2061 +                           && (atomic_read(&ip_set_list[i]->ref)))
2062 +                               return -EBUSY;
2063 +               }
2064 +
2065 +               for (i = 0; i < ip_set_max; i++) {
2066 +                       if (ip_set_list[i] != NULL)
2067 +                               ip_set_destroy_set(i);
2068 +               }
2069 +       }
2070 +       return 0;
2071 +}
2072 +
2073 +static void
2074 +ip_set_flush_set(struct ip_set *set)
2075 +{
2076 +       DP("set: %s %u",  set->name, set->id);
2077 +
2078 +       write_lock_bh(&set->lock);
2079 +       set->type->flush(set);
2080 +       write_unlock_bh(&set->lock);
2081 +}
2082 +
2083 +/* 
2084 + * Flush data in a set - or in all sets
2085 + */
2086 +static int
2087 +ip_set_flush(ip_set_id_t index)
2088 +{
2089 +       if (index != IP_SET_INVALID_ID) {
2090 +               IP_SET_ASSERT(ip_set_list[index]);
2091 +               ip_set_flush_set(ip_set_list[index]);
2092 +       } else
2093 +               FOREACH_SET_DO(ip_set_flush_set);
2094 +
2095 +       return 0;
2096 +}
2097 +
2098 +/* Rename a set */
2099 +static int
2100 +ip_set_rename(ip_set_id_t index, const char *name)
2101 +{
2102 +       struct ip_set *set = ip_set_list[index];
2103 +       ip_set_id_t i;
2104 +       int res = 0;
2105 +
2106 +       DP("set: %s to %s",  set->name, name);
2107 +       WRITE_LOCK(&ip_set_lock);
2108 +       for (i = 0; i < ip_set_max; i++) {
2109 +               if (ip_set_list[i] != NULL
2110 +                   && strncmp(ip_set_list[i]->name, 
2111 +                              name,
2112 +                              IP_SET_MAXNAMELEN - 1) == 0) {
2113 +                       res = -EEXIST;
2114 +                       goto unlock;
2115 +               }
2116 +       }
2117 +       strncpy(set->name, name, IP_SET_MAXNAMELEN);
2118 +    unlock:
2119 +       WRITE_UNLOCK(&ip_set_lock);
2120 +       return res;
2121 +}
2122 +
2123 +/*
2124 + * Swap two sets so that name/index points to the other.
2125 + * References are also swapped.
2126 + */
2127 +static int
2128 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2129 +{
2130 +       struct ip_set *from = ip_set_list[from_index];
2131 +       struct ip_set *to = ip_set_list[to_index];
2132 +       char from_name[IP_SET_MAXNAMELEN];
2133 +       u_int32_t from_ref;
2134 +
2135 +       DP("set: %s to %s",  from->name, to->name);
2136 +       /* Type can't be changed. Artifical restriction. */
2137 +       if (from->type->typecode != to->type->typecode)
2138 +               return -ENOEXEC;
2139 +
2140 +       /* No magic here: ref munging protected by the mutex */ 
2141 +       WRITE_LOCK(&ip_set_lock);
2142 +       strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2143 +       from_ref = atomic_read(&from->ref);
2144 +
2145 +       strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2146 +       atomic_set(&from->ref, atomic_read(&to->ref));
2147 +       strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2148 +       atomic_set(&to->ref, from_ref);
2149 +       
2150 +       ip_set_list[from_index] = to;
2151 +       ip_set_list[to_index] = from;
2152 +       
2153 +       WRITE_UNLOCK(&ip_set_lock);
2154 +       return 0;
2155 +}
2156 +
2157 +/*
2158 + * List set data
2159 + */
2160 +
2161 +static inline void
2162 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2163 +                             ip_set_id_t id, size_t *size)
2164 +{
2165 +       if (set_hash->id == id)
2166 +               *size += sizeof(struct ip_set_hash_list);
2167 +}
2168 +
2169 +static inline void
2170 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2171 +                             ip_set_id_t id, size_t *size)
2172 +{
2173 +       if (set_hash->id == id)
2174 +               *size += sizeof(struct ip_set_hash_save);
2175 +}
2176 +
2177 +static inline void
2178 +__set_hash_bindings(struct ip_set_hash *set_hash,
2179 +                   ip_set_id_t id, void *data, int *used)
2180 +{
2181 +       if (set_hash->id == id) {
2182 +               struct ip_set_hash_list *hash_list = 
2183 +                       (struct ip_set_hash_list *)(data + *used);
2184 +
2185 +               hash_list->ip = set_hash->ip;
2186 +               hash_list->binding = set_hash->binding;
2187 +               *used += sizeof(struct ip_set_hash_list);
2188 +       }
2189 +}
2190 +
2191 +static int ip_set_list_set(ip_set_id_t index,
2192 +                          void *data,
2193 +                          int *used,
2194 +                          int len)
2195 +{
2196 +       struct ip_set *set = ip_set_list[index];
2197 +       struct ip_set_list *set_list;
2198 +
2199 +       /* Pointer to our header */
2200 +       set_list = (struct ip_set_list *) (data + *used);
2201 +
2202 +       DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2203 +
2204 +       /* Get and ensure header size */
2205 +       if (*used + sizeof(struct ip_set_list) > len)
2206 +               goto not_enough_mem;
2207 +       *used += sizeof(struct ip_set_list);
2208 +
2209 +       read_lock_bh(&set->lock);
2210 +       /* Get and ensure set specific header size */
2211 +       set_list->header_size = set->type->header_size;
2212 +       if (*used + set_list->header_size > len)
2213 +               goto unlock_set;
2214 +
2215 +       /* Fill in the header */
2216 +       set_list->index = index;
2217 +       set_list->binding = set->binding;
2218 +       set_list->ref = atomic_read(&set->ref);
2219 +
2220 +       /* Fill in set spefific header data */
2221 +       DP("call list_header");
2222 +       set->type->list_header(set, data + *used);
2223 +       DP("call list_header, done");
2224 +       *used += set_list->header_size;
2225 +
2226 +       /* Get and ensure set specific members size */
2227 +       DP("call list_members_size");
2228 +       set_list->members_size = set->type->list_members_size(set);
2229 +       DP("call list_members_size, done");
2230 +       if (*used + set_list->members_size > len)
2231 +               goto unlock_set;
2232 +
2233 +       /* Fill in set spefific members data */
2234 +       DP("call list_members");
2235 +       set->type->list_members(set, data + *used);
2236 +       DP("call list_members, done");
2237 +       *used += set_list->members_size;
2238 +       read_unlock_bh(&set->lock);
2239 +
2240 +       /* Bindings */
2241 +
2242 +       /* Get and ensure set specific bindings size */
2243 +       set_list->bindings_size = 0;
2244 +       FOREACH_HASH_DO(__set_hash_bindings_size_list,
2245 +                       set->id, &set_list->bindings_size);
2246 +       if (*used + set_list->bindings_size > len)
2247 +               goto not_enough_mem;
2248 +
2249 +       /* Fill in set spefific bindings data */
2250 +       FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2251 +       
2252 +       return 0;
2253 +
2254 +    unlock_set:
2255 +       read_unlock_bh(&set->lock);
2256 +    not_enough_mem:
2257 +       DP("not enough mem, try again");
2258 +       return -EAGAIN;
2259 +}
2260 +
2261 +/*
2262 + * Save sets
2263 + */
2264 +static int ip_set_save_set(ip_set_id_t index,
2265 +                          void *data,
2266 +                          int *used,
2267 +                          int len)
2268 +{
2269 +       struct ip_set *set;
2270 +       struct ip_set_save *set_save;
2271 +
2272 +       /* Pointer to our header */
2273 +       set_save = (struct ip_set_save *) (data + *used);
2274 +
2275 +       /* Get and ensure header size */
2276 +       if (*used + sizeof(struct ip_set_save) > len)
2277 +               goto not_enough_mem;
2278 +       *used += sizeof(struct ip_set_save);
2279 +
2280 +       set = ip_set_list[index];
2281 +       DP("set: %s, used: %u(%u) %p %p", set->name, *used, len, 
2282 +          data, data + *used);
2283 +
2284 +       read_lock_bh(&set->lock);
2285 +       /* Get and ensure set specific header size */
2286 +       set_save->header_size = set->type->header_size;
2287 +       if (*used + set_save->header_size > len)
2288 +               goto unlock_set;
2289 +
2290 +       /* Fill in the header */
2291 +       set_save->index = index;
2292 +       set_save->binding = set->binding;
2293 +
2294 +       /* Fill in set spefific header data */
2295 +       set->type->list_header(set, data + *used);
2296 +       *used += set_save->header_size;
2297 +
2298 +       DP("set header filled: %s, used: %u %p %p", set->name, *used,
2299 +          data, data + *used);
2300 +       /* Get and ensure set specific members size */
2301 +       set_save->members_size = set->type->list_members_size(set);
2302 +       if (*used + set_save->members_size > len)
2303 +               goto unlock_set;
2304 +
2305 +       /* Fill in set spefific members data */
2306 +       set->type->list_members(set, data + *used);
2307 +       *used += set_save->members_size;
2308 +       read_unlock_bh(&set->lock);
2309 +       DP("set members filled: %s, used: %u %p %p", set->name, *used,
2310 +          data, data + *used);
2311 +       return 0;
2312 +
2313 +    unlock_set:
2314 +       read_unlock_bh(&set->lock);
2315 +    not_enough_mem:
2316 +       DP("not enough mem, try again");
2317 +       return -EAGAIN;
2318 +}
2319 +
2320 +static inline void
2321 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2322 +                        ip_set_id_t id,
2323 +                        void *data,
2324 +                        int *used,
2325 +                        int len,
2326 +                        int *res)
2327 +{
2328 +       if (*res == 0
2329 +           && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2330 +               struct ip_set_hash_save *hash_save = 
2331 +                       (struct ip_set_hash_save *)(data + *used);
2332 +               /* Ensure bindings size */
2333 +               if (*used + sizeof(struct ip_set_hash_save) > len) {
2334 +                       *res = -ENOMEM;
2335 +                       return;
2336 +               }
2337 +               hash_save->id = set_hash->id;
2338 +               hash_save->ip = set_hash->ip;
2339 +               hash_save->binding = set_hash->binding;
2340 +               *used += sizeof(struct ip_set_hash_save);
2341 +       }
2342 +}
2343 +
2344 +static int ip_set_save_bindings(ip_set_id_t index,
2345 +                               void *data,
2346 +                               int *used,
2347 +                               int len)
2348 +{
2349 +       int res = 0;
2350 +       struct ip_set_save *set_save;
2351 +
2352 +       DP("used %u, len %u", *used, len);
2353 +       /* Get and ensure header size */
2354 +       if (*used + sizeof(struct ip_set_save) > len)
2355 +               return -ENOMEM;
2356 +
2357 +       /* Marker */
2358 +       set_save = (struct ip_set_save *) (data + *used);
2359 +       set_save->index = IP_SET_INVALID_ID;
2360 +       *used += sizeof(struct ip_set_save);
2361 +
2362 +       DP("marker added used %u, len %u", *used, len);
2363 +       /* Fill in bindings data */
2364 +       if (index != IP_SET_INVALID_ID)
2365 +               /* Sets are identified by id in hash */
2366 +               index = ip_set_list[index]->id;
2367 +       FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2368 +
2369 +       return res;     
2370 +}
2371 +
2372 +/*
2373 + * Restore sets
2374 + */
2375 +static int ip_set_restore(void *data,
2376 +                         int len)
2377 +{
2378 +       int res = 0;
2379 +       int line = 0, used = 0, members_size;
2380 +       struct ip_set *set;
2381 +       struct ip_set_hash_save *hash_save;
2382 +       struct ip_set_restore *set_restore;
2383 +       ip_set_id_t index;
2384 +
2385 +       /* Loop to restore sets */
2386 +       while (1) {
2387 +               line++;
2388 +               
2389 +               DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2390 +               /* Get and ensure header size */
2391 +               if (used + sizeof(struct ip_set_restore) > len)
2392 +                       return line;
2393 +               set_restore = (struct ip_set_restore *) (data + used);
2394 +               used += sizeof(struct ip_set_restore);
2395 +
2396 +               /* Ensure data size */
2397 +               if (used 
2398 +                   + set_restore->header_size 
2399 +                   + set_restore->members_size > len)
2400 +                       return line;
2401 +
2402 +               /* Check marker */
2403 +               if (set_restore->index == IP_SET_INVALID_ID) {
2404 +                       line--;
2405 +                       goto bindings;
2406 +               }
2407 +               
2408 +               /* Try to create the set */
2409 +               DP("restore %s %s", set_restore->name, set_restore->typename);
2410 +               res = ip_set_create(set_restore->name,
2411 +                                   set_restore->typename,
2412 +                                   set_restore->index,
2413 +                                   data + used,
2414 +                                   set_restore->header_size);
2415 +               
2416 +               if (res != 0)
2417 +                       return line;
2418 +               used += set_restore->header_size;
2419 +
2420 +               index = ip_set_find_byindex(set_restore->index);
2421 +               DP("index %u, restore_index %u", index, set_restore->index);
2422 +               if (index != set_restore->index)
2423 +                       return line;
2424 +               /* Try to restore members data */
2425 +               set = ip_set_list[index];
2426 +               members_size = 0;
2427 +               DP("members_size %u reqsize %u",
2428 +                  set_restore->members_size, set->type->reqsize);
2429 +               while (members_size + set->type->reqsize <=
2430 +                      set_restore->members_size) {
2431 +                       line++;
2432 +                       DP("members: %u, line %u", members_size, line);
2433 +                       res = __ip_set_addip(index,
2434 +                                          data + used + members_size,
2435 +                                          set->type->reqsize);
2436 +                       if (!(res == 0 || res == -EEXIST))
2437 +                               return line;
2438 +                       members_size += set->type->reqsize;
2439 +               }
2440 +
2441 +               DP("members_size %u  %u",
2442 +                  set_restore->members_size, members_size);
2443 +               if (members_size != set_restore->members_size)
2444 +                       return line++;
2445 +               used += set_restore->members_size;              
2446 +       }
2447 +       
2448 +   bindings:
2449 +       /* Loop to restore bindings */
2450 +       while (used < len) {
2451 +               line++;
2452 +
2453 +               DP("restore binding, line %u", line);           
2454 +               /* Get and ensure size */
2455 +               if (used + sizeof(struct ip_set_hash_save) > len)
2456 +                       return line;
2457 +               hash_save = (struct ip_set_hash_save *) (data + used);
2458 +               used += sizeof(struct ip_set_hash_save);
2459 +               
2460 +               /* hash_save->id is used to store the index */
2461 +               index = ip_set_find_byindex(hash_save->id);
2462 +               DP("restore binding index %u, id %u, %u -> %u",
2463 +                  index, hash_save->id, hash_save->ip, hash_save->binding);            
2464 +               if (index != hash_save->id)
2465 +                       return line;
2466 +                       
2467 +               set = ip_set_list[hash_save->id];
2468 +               /* Null valued IP means default binding */
2469 +               if (hash_save->ip)
2470 +                       res = ip_set_hash_add(set->id, 
2471 +                                             hash_save->ip,
2472 +                                             hash_save->binding);
2473 +               else {
2474 +                       IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2475 +                       WRITE_LOCK(&ip_set_lock);
2476 +                       set->binding = hash_save->binding;
2477 +                       __ip_set_get(set->binding);
2478 +                       WRITE_UNLOCK(&ip_set_lock);
2479 +                       DP("default binding: %u", set->binding);
2480 +               }
2481 +               if (res != 0)
2482 +                       return line;
2483 +       }
2484 +       if (used != len)
2485 +               return line;
2486 +       
2487 +       return 0;       
2488 +}
2489 +
2490 +static int
2491 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2492 +{
2493 +       void *data;
2494 +       int res = 0;            /* Assume OK */
2495 +       unsigned *op;
2496 +       struct ip_set_req_adt *req_adt;
2497 +       ip_set_id_t index = IP_SET_INVALID_ID;
2498 +       int (*adtfn)(ip_set_id_t index,
2499 +                    const void *data, size_t size);
2500 +       struct fn_table {
2501 +               int (*fn)(ip_set_id_t index,
2502 +                         const void *data, size_t size);
2503 +       } adtfn_table[] =
2504 +       { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2505 +         { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2506 +       };
2507 +
2508 +       DP("optval=%d, user=%p, len=%d", optval, user, len);
2509 +       if (!capable(CAP_NET_ADMIN))
2510 +               return -EPERM;
2511 +       if (optval != SO_IP_SET)
2512 +               return -EBADF;
2513 +       if (len <= sizeof(unsigned)) {
2514 +               ip_set_printk("short userdata (want >%zu, got %u)",
2515 +                             sizeof(unsigned), len);
2516 +               return -EINVAL;
2517 +       }
2518 +       data = vmalloc(len);
2519 +       if (!data) {
2520 +               DP("out of mem for %u bytes", len);
2521 +               return -ENOMEM;
2522 +       }
2523 +       if (copy_from_user(data, user, len) != 0) {
2524 +               res = -EFAULT;
2525 +               goto done;
2526 +       }
2527 +       if (down_interruptible(&ip_set_app_mutex)) {
2528 +               res = -EINTR;
2529 +               goto done;
2530 +       }
2531 +
2532 +       op = (unsigned *)data;
2533 +       DP("op=%x", *op);
2534 +       
2535 +       if (*op < IP_SET_OP_VERSION) {
2536 +               /* Check the version at the beginning of operations */
2537 +               struct ip_set_req_version *req_version =
2538 +                       (struct ip_set_req_version *) data;
2539 +               if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2540 +                       res = -EPROTO;
2541 +                       goto done;
2542 +               }
2543 +       }
2544 +
2545 +       switch (*op) {
2546 +       case IP_SET_OP_CREATE:{
2547 +               struct ip_set_req_create *req_create
2548 +                       = (struct ip_set_req_create *) data;
2549 +               
2550 +               if (len <= sizeof(struct ip_set_req_create)) {
2551 +                       ip_set_printk("short CREATE data (want >%zu, got %u)",
2552 +                                     sizeof(struct ip_set_req_create), len);
2553 +                       res = -EINVAL;
2554 +                       goto done;
2555 +               }
2556 +               req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2557 +               req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2558 +               res = ip_set_create(req_create->name,
2559 +                                   req_create->typename,
2560 +                                   IP_SET_INVALID_ID,
2561 +                                   data + sizeof(struct ip_set_req_create),
2562 +                                   len - sizeof(struct ip_set_req_create));
2563 +               goto done;
2564 +       }
2565 +       case IP_SET_OP_DESTROY:{
2566 +               struct ip_set_req_std *req_destroy
2567 +                       = (struct ip_set_req_std *) data;
2568 +               
2569 +               if (len != sizeof(struct ip_set_req_std)) {
2570 +                       ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2571 +                                     sizeof(struct ip_set_req_std), len);
2572 +                       res = -EINVAL;
2573 +                       goto done;
2574 +               }
2575 +               if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2576 +                       /* Destroy all sets */
2577 +                       index = IP_SET_INVALID_ID;
2578 +               } else {
2579 +                       req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2580 +                       index = ip_set_find_byname(req_destroy->name);
2581 +
2582 +                       if (index == IP_SET_INVALID_ID) {
2583 +                               res = -ENOENT;
2584 +                               goto done;
2585 +                       }
2586 +               }
2587 +                       
2588 +               res = ip_set_destroy(index);
2589 +               goto done;
2590 +       }
2591 +       case IP_SET_OP_FLUSH:{
2592 +               struct ip_set_req_std *req_flush =
2593 +                       (struct ip_set_req_std *) data;
2594 +
2595 +               if (len != sizeof(struct ip_set_req_std)) {
2596 +                       ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2597 +                                     sizeof(struct ip_set_req_std), len);
2598 +                       res = -EINVAL;
2599 +                       goto done;
2600 +               }
2601 +               if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2602 +                       /* Flush all sets */
2603 +                       index = IP_SET_INVALID_ID;
2604 +               } else {
2605 +                       req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2606 +                       index = ip_set_find_byname(req_flush->name);
2607 +
2608 +                       if (index == IP_SET_INVALID_ID) {
2609 +                               res = -ENOENT;
2610 +                               goto done;
2611 +                       }
2612 +               }
2613 +               res = ip_set_flush(index);
2614 +               goto done;
2615 +       }
2616 +       case IP_SET_OP_RENAME:{
2617 +               struct ip_set_req_create *req_rename
2618 +                       = (struct ip_set_req_create *) data;
2619 +
2620 +               if (len != sizeof(struct ip_set_req_create)) {
2621 +                       ip_set_printk("invalid RENAME data (want %zu, got %u)",
2622 +                                     sizeof(struct ip_set_req_create), len);
2623 +                       res = -EINVAL;
2624 +                       goto done;
2625 +               }
2626 +
2627 +               req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2628 +               req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2629 +                       
2630 +               index = ip_set_find_byname(req_rename->name);
2631 +               if (index == IP_SET_INVALID_ID) {
2632 +                       res = -ENOENT;
2633 +                       goto done;
2634 +               }
2635 +               res = ip_set_rename(index, req_rename->typename);
2636 +               goto done;
2637 +       }
2638 +       case IP_SET_OP_SWAP:{
2639 +               struct ip_set_req_create *req_swap
2640 +                       = (struct ip_set_req_create *) data;
2641 +               ip_set_id_t to_index;
2642 +
2643 +               if (len != sizeof(struct ip_set_req_create)) {
2644 +                       ip_set_printk("invalid SWAP data (want %zu, got %u)",
2645 +                                     sizeof(struct ip_set_req_create), len);
2646 +                       res = -EINVAL;
2647 +                       goto done;
2648 +               }
2649 +
2650 +               req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2651 +               req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2652 +
2653 +               index = ip_set_find_byname(req_swap->name);
2654 +               if (index == IP_SET_INVALID_ID) {
2655 +                       res = -ENOENT;
2656 +                       goto done;
2657 +               }
2658 +               to_index = ip_set_find_byname(req_swap->typename);
2659 +               if (to_index == IP_SET_INVALID_ID) {
2660 +                       res = -ENOENT;
2661 +                       goto done;
2662 +               }
2663 +               res = ip_set_swap(index, to_index);
2664 +               goto done;
2665 +       }
2666 +       default: 
2667 +               break;  /* Set identified by id */
2668 +       }
2669 +       
2670 +       /* There we may have add/del/test/bind/unbind/test_bind operations */
2671 +       if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2672 +               res = -EBADMSG;
2673 +               goto done;
2674 +       }
2675 +       adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2676 +
2677 +       if (len < sizeof(struct ip_set_req_adt)) {
2678 +               ip_set_printk("short data in adt request (want >=%zu, got %u)",
2679 +                             sizeof(struct ip_set_req_adt), len);
2680 +               res = -EINVAL;
2681 +               goto done;
2682 +       }
2683 +       req_adt = (struct ip_set_req_adt *) data;
2684 +
2685 +       /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2686 +       if (!(*op == IP_SET_OP_UNBIND_SET 
2687 +             && req_adt->index == IP_SET_INVALID_ID)) {
2688 +               index = ip_set_find_byindex(req_adt->index);
2689 +               if (index == IP_SET_INVALID_ID) {
2690 +                       res = -ENOENT;
2691 +                       goto done;
2692 +               }
2693 +       }
2694 +       res = adtfn(index, data, len);
2695 +
2696 +    done:
2697 +       up(&ip_set_app_mutex);
2698 +       vfree(data);
2699 +       if (res > 0)
2700 +               res = 0;
2701 +       DP("final result %d", res);
2702 +       return res;
2703 +}
2704 +
2705 +static int 
2706 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2707 +{
2708 +       int res = 0;
2709 +       unsigned *op;
2710 +       ip_set_id_t index = IP_SET_INVALID_ID;
2711 +       void *data;
2712 +       int copylen = *len;
2713 +
2714 +       DP("optval=%d, user=%p, len=%d", optval, user, *len);
2715 +       if (!capable(CAP_NET_ADMIN))
2716 +               return -EPERM;
2717 +       if (optval != SO_IP_SET)
2718 +               return -EBADF;
2719 +       if (*len < sizeof(unsigned)) {
2720 +               ip_set_printk("short userdata (want >=%zu, got %d)",
2721 +                             sizeof(unsigned), *len);
2722 +               return -EINVAL;
2723 +       }
2724 +       data = vmalloc(*len);
2725 +       if (!data) {
2726 +               DP("out of mem for %d bytes", *len);
2727 +               return -ENOMEM;
2728 +       }
2729 +       if (copy_from_user(data, user, *len) != 0) {
2730 +               res = -EFAULT;
2731 +               goto done;
2732 +       }
2733 +       if (down_interruptible(&ip_set_app_mutex)) {
2734 +               res = -EINTR;
2735 +               goto done;
2736 +       }
2737 +
2738 +       op = (unsigned *) data;
2739 +       DP("op=%x", *op);
2740 +
2741 +       if (*op < IP_SET_OP_VERSION) {
2742 +               /* Check the version at the beginning of operations */
2743 +               struct ip_set_req_version *req_version =
2744 +                       (struct ip_set_req_version *) data;
2745 +               if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2746 +                       res = -EPROTO;
2747 +                       goto done;
2748 +               }
2749 +       }
2750 +
2751 +       switch (*op) {
2752 +       case IP_SET_OP_VERSION: {
2753 +               struct ip_set_req_version *req_version =
2754 +                   (struct ip_set_req_version *) data;
2755 +
2756 +               if (*len != sizeof(struct ip_set_req_version)) {
2757 +                       ip_set_printk("invalid VERSION (want %zu, got %d)",
2758 +                                     sizeof(struct ip_set_req_version),
2759 +                                     *len);
2760 +                       res = -EINVAL;
2761 +                       goto done;
2762 +               }
2763 +
2764 +               req_version->version = IP_SET_PROTOCOL_VERSION;
2765 +               res = copy_to_user(user, req_version,
2766 +                                  sizeof(struct ip_set_req_version));
2767 +               goto done;
2768 +       }
2769 +       case IP_SET_OP_GET_BYNAME: {
2770 +               struct ip_set_req_get_set *req_get
2771 +                       = (struct ip_set_req_get_set *) data;
2772 +
2773 +               if (*len != sizeof(struct ip_set_req_get_set)) {
2774 +                       ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2775 +                                     sizeof(struct ip_set_req_get_set), *len);
2776 +                       res = -EINVAL;
2777 +                       goto done;
2778 +               }
2779 +               req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2780 +               index = ip_set_find_byname(req_get->set.name);
2781 +               req_get->set.index = index;
2782 +               goto copy;
2783 +       }
2784 +       case IP_SET_OP_GET_BYINDEX: {
2785 +               struct ip_set_req_get_set *req_get
2786 +                       = (struct ip_set_req_get_set *) data;
2787 +
2788 +               if (*len != sizeof(struct ip_set_req_get_set)) {
2789 +                       ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2790 +                                     sizeof(struct ip_set_req_get_set), *len);
2791 +                       res = -EINVAL;
2792 +                       goto done;
2793 +               }
2794 +               req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2795 +               index = ip_set_find_byindex(req_get->set.index);
2796 +               strncpy(req_get->set.name,
2797 +                       index == IP_SET_INVALID_ID ? ""
2798 +                       : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2799 +               goto copy;
2800 +       }
2801 +       case IP_SET_OP_ADT_GET: {
2802 +               struct ip_set_req_adt_get *req_get
2803 +                       = (struct ip_set_req_adt_get *) data;
2804 +
2805 +               if (*len != sizeof(struct ip_set_req_adt_get)) {
2806 +                       ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2807 +                                     sizeof(struct ip_set_req_adt_get), *len);
2808 +                       res = -EINVAL;
2809 +                       goto done;
2810 +               }
2811 +               req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2812 +               index = ip_set_find_byname(req_get->set.name);
2813 +               if (index != IP_SET_INVALID_ID) {
2814 +                       req_get->set.index = index;
2815 +                       strncpy(req_get->typename,
2816 +                               ip_set_list[index]->type->typename,
2817 +                               IP_SET_MAXNAMELEN - 1);
2818 +               } else {
2819 +                       res = -ENOENT;
2820 +                       goto done;
2821 +               }
2822 +               goto copy;
2823 +       }
2824 +       case IP_SET_OP_MAX_SETS: {
2825 +               struct ip_set_req_max_sets *req_max_sets
2826 +                       = (struct ip_set_req_max_sets *) data;
2827 +               ip_set_id_t i;
2828 +
2829 +               if (*len != sizeof(struct ip_set_req_max_sets)) {
2830 +                       ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2831 +                                     sizeof(struct ip_set_req_max_sets), *len);
2832 +                       res = -EINVAL;
2833 +                       goto done;
2834 +               }
2835 +
2836 +               if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2837 +                       req_max_sets->set.index = IP_SET_INVALID_ID;
2838 +               } else {
2839 +                       req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2840 +                       req_max_sets->set.index = 
2841 +                               ip_set_find_byname(req_max_sets->set.name);
2842 +                       if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2843 +                               res = -ENOENT;
2844 +                               goto done;
2845 +                       }
2846 +               }
2847 +               req_max_sets->max_sets = ip_set_max;
2848 +               req_max_sets->sets = 0;
2849 +               for (i = 0; i < ip_set_max; i++) {
2850 +                       if (ip_set_list[i] != NULL)
2851 +                               req_max_sets->sets++;
2852 +               }
2853 +               goto copy;
2854 +       }
2855 +       case IP_SET_OP_LIST_SIZE: 
2856 +       case IP_SET_OP_SAVE_SIZE: {
2857 +               struct ip_set_req_setnames *req_setnames
2858 +                       = (struct ip_set_req_setnames *) data;
2859 +               struct ip_set_name_list *name_list;
2860 +               struct ip_set *set;
2861 +               ip_set_id_t i;
2862 +               int used;
2863 +
2864 +               if (*len < sizeof(struct ip_set_req_setnames)) {
2865 +                       ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2866 +                                     sizeof(struct ip_set_req_setnames), *len);
2867 +                       res = -EINVAL;
2868 +                       goto done;
2869 +               }
2870 +
2871 +               req_setnames->size = 0;
2872 +               used = sizeof(struct ip_set_req_setnames);
2873 +               for (i = 0; i < ip_set_max; i++) {
2874 +                       if (ip_set_list[i] == NULL)
2875 +                               continue;
2876 +                       name_list = (struct ip_set_name_list *) 
2877 +                               (data + used);
2878 +                       used += sizeof(struct ip_set_name_list);
2879 +                       if (used > copylen) {
2880 +                               res = -EAGAIN;
2881 +                               goto done;
2882 +                       }
2883 +                       set = ip_set_list[i];
2884 +                       /* Fill in index, name, etc. */
2885 +                       name_list->index = i;
2886 +                       name_list->id = set->id;
2887 +                       strncpy(name_list->name,
2888 +                               set->name,
2889 +                               IP_SET_MAXNAMELEN - 1);
2890 +                       strncpy(name_list->typename,
2891 +                               set->type->typename,
2892 +                               IP_SET_MAXNAMELEN - 1);
2893 +                       DP("filled %s of type %s, index %u\n",
2894 +                          name_list->name, name_list->typename,
2895 +                          name_list->index);
2896 +                       if (!(req_setnames->index == IP_SET_INVALID_ID
2897 +                             || req_setnames->index == i))
2898 +                             continue;
2899 +                       /* Update size */
2900 +                       switch (*op) {
2901 +                       case IP_SET_OP_LIST_SIZE: {
2902 +                               req_setnames->size += sizeof(struct ip_set_list)
2903 +                                       + set->type->header_size
2904 +                                       + set->type->list_members_size(set);
2905 +                               FOREACH_HASH_DO(__set_hash_bindings_size_list, 
2906 +                                               i, &req_setnames->size);
2907 +                               break;
2908 +                       }
2909 +                       case IP_SET_OP_SAVE_SIZE: {
2910 +                               req_setnames->size += sizeof(struct ip_set_save)
2911 +                                       + set->type->header_size
2912 +                                       + set->type->list_members_size(set);
2913 +                               FOREACH_HASH_DO(__set_hash_bindings_size_save,
2914 +                                               i, &req_setnames->size);
2915 +                               break;
2916 +                       }
2917 +                       default:
2918 +                               break;
2919 +                       }
2920 +               }
2921 +               if (copylen != used) {
2922 +                       res = -EAGAIN;
2923 +                       goto done;
2924 +               }
2925 +               goto copy;
2926 +       }
2927 +       case IP_SET_OP_LIST: {
2928 +               struct ip_set_req_list *req_list
2929 +                       = (struct ip_set_req_list *) data;
2930 +               ip_set_id_t i;
2931 +               int used;
2932 +
2933 +               if (*len < sizeof(struct ip_set_req_list)) {
2934 +                       ip_set_printk("short LIST (want >=%zu, got %d)",
2935 +                                     sizeof(struct ip_set_req_list), *len);
2936 +                       res = -EINVAL;
2937 +                       goto done;
2938 +               }
2939 +               index = req_list->index;
2940 +               if (index != IP_SET_INVALID_ID
2941 +                   && ip_set_find_byindex(index) != index) {
2942 +                       res = -ENOENT;
2943 +                       goto done;
2944 +               }
2945 +               used = 0;
2946 +               if (index == IP_SET_INVALID_ID) {
2947 +                       /* List all sets */
2948 +                       for (i = 0; i < ip_set_max && res == 0; i++) {
2949 +                               if (ip_set_list[i] != NULL)
2950 +                                       res = ip_set_list_set(i, data, &used, *len);
2951 +                       }
2952 +               } else {
2953 +                       /* List an individual set */
2954 +                       res = ip_set_list_set(index, data, &used, *len);
2955 +               }
2956 +               if (res != 0)
2957 +                       goto done;
2958 +               else if (copylen != used) {
2959 +                       res = -EAGAIN;
2960 +                       goto done;
2961 +               }
2962 +               goto copy;
2963 +       }
2964 +       case IP_SET_OP_SAVE: {
2965 +               struct ip_set_req_list *req_save
2966 +                       = (struct ip_set_req_list *) data;
2967 +               ip_set_id_t i;
2968 +               int used;
2969 +
2970 +               if (*len < sizeof(struct ip_set_req_list)) {
2971 +                       ip_set_printk("short SAVE (want >=%zu, got %d)",
2972 +                                     sizeof(struct ip_set_req_list), *len);
2973 +                       res = -EINVAL;
2974 +                       goto done;
2975 +               }
2976 +               index = req_save->index;
2977 +               if (index != IP_SET_INVALID_ID
2978 +                   && ip_set_find_byindex(index) != index) {
2979 +                       res = -ENOENT;
2980 +                       goto done;
2981 +               }
2982 +               used = 0;
2983 +               if (index == IP_SET_INVALID_ID) {
2984 +                       /* Save all sets */
2985 +                       for (i = 0; i < ip_set_max && res == 0; i++) {
2986 +                               if (ip_set_list[i] != NULL)
2987 +                                       res = ip_set_save_set(i, data, &used, *len);
2988 +                       }
2989 +               } else {
2990 +                       /* Save an individual set */
2991 +                       res = ip_set_save_set(index, data, &used, *len);
2992 +               }
2993 +               if (res == 0)
2994 +                       res = ip_set_save_bindings(index, data, &used, *len);
2995 +                       
2996 +               if (res != 0)
2997 +                       goto done;
2998 +               else if (copylen != used) {
2999 +                       res = -EAGAIN;
3000 +                       goto done;
3001 +               }
3002 +               goto copy;
3003 +       }
3004 +       case IP_SET_OP_RESTORE: {
3005 +               struct ip_set_req_setnames *req_restore
3006 +                       = (struct ip_set_req_setnames *) data;
3007 +               int line;
3008 +
3009 +               if (*len < sizeof(struct ip_set_req_setnames)
3010 +                   || *len != req_restore->size) {
3011 +                       ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3012 +                                     req_restore->size, *len);
3013 +                       res = -EINVAL;
3014 +                       goto done;
3015 +               }
3016 +               line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3017 +                                     req_restore->size - sizeof(struct ip_set_req_setnames));
3018 +               DP("ip_set_restore: %u", line);
3019 +               if (line != 0) {
3020 +                       res = -EAGAIN;
3021 +                       req_restore->size = line;
3022 +                       copylen = sizeof(struct ip_set_req_setnames);
3023 +                       goto copy;
3024 +               }
3025 +               goto done;
3026 +       }
3027 +       default:
3028 +               res = -EBADMSG;
3029 +               goto done;
3030 +       }       /* end of switch(op) */
3031 +
3032 +    copy:
3033 +       DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3034 +                                && ip_set_list[index]
3035 +                    ? ip_set_list[index]->name
3036 +                    : ":all:", copylen);
3037 +       if (res == 0)
3038 +               res = copy_to_user(user, data, copylen);
3039 +       else
3040 +               copy_to_user(user, data, copylen);
3041 +       
3042 +    done:
3043 +       up(&ip_set_app_mutex);
3044 +       vfree(data);
3045 +       if (res > 0)
3046 +               res = 0;
3047 +       DP("final result %d", res);
3048 +       return res;
3049 +}
3050 +
3051 +static struct nf_sockopt_ops so_set = {
3052 +       .pf             = PF_INET,
3053 +       .set_optmin     = SO_IP_SET,
3054 +       .set_optmax     = SO_IP_SET + 1,
3055 +       .set            = &ip_set_sockfn_set,
3056 +       .get_optmin     = SO_IP_SET,
3057 +       .get_optmax     = SO_IP_SET + 1,
3058 +       .get            = &ip_set_sockfn_get,
3059 +       .use            = 0
3060 +};
3061 +
3062 +static int max_sets, hash_size;
3063 +MODULE_PARM(max_sets, "i");
3064 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3065 +MODULE_PARM(hash_size, "i");
3066 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3067 +MODULE_LICENSE("GPL");
3068 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3069 +MODULE_DESCRIPTION("module implementing core IP set support");
3070 +
3071 +static int __init init(void)
3072 +{
3073 +       int res;
3074 +       ip_set_id_t i;
3075 +
3076 +       get_random_bytes(&ip_set_hash_random, 4);
3077 +       if (max_sets)
3078 +               ip_set_max = max_sets;
3079 +       ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3080 +       if (!ip_set_list) {
3081 +               printk(KERN_ERR "Unable to create ip_set_list\n");
3082 +               return -ENOMEM;
3083 +       }
3084 +       memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3085 +       if (hash_size)
3086 +               ip_set_bindings_hash_size = hash_size;
3087 +       ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3088 +       if (!ip_set_hash) {
3089 +               printk(KERN_ERR "Unable to create ip_set_hash\n");
3090 +               vfree(ip_set_list);
3091 +               return -ENOMEM;
3092 +       }
3093 +       for (i = 0; i < ip_set_bindings_hash_size; i++)
3094 +               INIT_LIST_HEAD(&ip_set_hash[i]);
3095 +
3096 +       INIT_LIST_HEAD(&set_type_list);
3097 +
3098 +       res = nf_register_sockopt(&so_set);
3099 +       if (res != 0) {
3100 +               ip_set_printk("SO_SET registry failed: %d", res);
3101 +               vfree(ip_set_list);
3102 +               vfree(ip_set_hash);
3103 +               return res;
3104 +       }
3105 +       return 0;
3106 +}
3107 +
3108 +static void __exit fini(void)
3109 +{
3110 +       /* There can't be any existing set or binding. Racy. */
3111 +       nf_unregister_sockopt(&so_set);
3112 +       vfree(ip_set_list);
3113 +       vfree(ip_set_hash);
3114 +       DP("these are the famous last words");
3115 +}
3116 +
3117 +EXPORT_SYMBOL(ip_set_register_set_type);
3118 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3119 +
3120 +EXPORT_SYMBOL(ip_set_get_byname);
3121 +EXPORT_SYMBOL(ip_set_get_byindex);
3122 +EXPORT_SYMBOL(ip_set_put);
3123 +
3124 +EXPORT_SYMBOL(ip_set_addip_kernel);
3125 +EXPORT_SYMBOL(ip_set_delip_kernel);
3126 +EXPORT_SYMBOL(ip_set_testip_kernel);
3127 +
3128 +module_init(init);
3129 +module_exit(fini);
3130 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_iphash.c
3131 ===================================================================
3132 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
3133 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_iphash.c   2007-12-15 05:20:07.896290225 +0100
3134 @@ -0,0 +1,379 @@
3135 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3136 + *
3137 + * This program is free software; you can redistribute it and/or modify
3138 + * it under the terms of the GNU General Public License version 2 as
3139 + * published by the Free Software Foundation.  
3140 + */
3141 +
3142 +/* Kernel module implementing an ip hash set */
3143 +
3144 +#include <linux/module.h>
3145 +#include <linux/ip.h>
3146 +#include <linux/skbuff.h>
3147 +#include <linux/netfilter_ipv4/ip_tables.h>
3148 +#include <linux/netfilter_ipv4/ip_set.h>
3149 +#include <linux/errno.h>
3150 +#include <asm/uaccess.h>
3151 +#include <asm/bitops.h>
3152 +#include <asm/softirq.h>
3153 +#include <linux/spinlock.h>
3154 +#include <linux/vmalloc.h>
3155 +#include <linux/random.h>
3156 +
3157 +#include <net/ip.h>
3158 +
3159 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3160 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3161 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
3162 +#include <linux/netfilter_ipv4/ip_set_prime.h>
3163 +
3164 +static inline __u32
3165 +jhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3166 +{
3167 +       return jhash_1word(ip, map->initval);
3168 +}
3169 +
3170 +static inline __u32
3171 +randhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3172 +{
3173 +       return (1 + ip % map->prime);
3174 +}
3175 +
3176 +static inline __u32
3177 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3178 +{
3179 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3180 +       __u32 jhash, randhash, id;
3181 +       u_int16_t i;
3182 +
3183 +       *hash_ip = ip & map->netmask;
3184 +       jhash = jhash_ip(map, *hash_ip);
3185 +       randhash = randhash_ip(map, *hash_ip);
3186 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3187 +          set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3188 +       
3189 +       for (i = 0; i < map->probes; i++) {
3190 +               id = (jhash + i * randhash) % map->hashsize;
3191 +               DP("hash key: %u", id);
3192 +               if (map->members[id] == *hash_ip)
3193 +                       return id;
3194 +               /* No shortcut at testing - there can be deleted
3195 +                * entries. */
3196 +       }
3197 +       return UINT_MAX;
3198 +}
3199 +
3200 +static inline int
3201 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3202 +{
3203 +       return (hash_id(set, ip, hash_ip) != UINT_MAX);
3204 +}
3205 +
3206 +static int
3207 +testip(struct ip_set *set, const void *data, size_t size,
3208 +       ip_set_ip_t *hash_ip)
3209 +{
3210 +       struct ip_set_req_iphash *req = 
3211 +           (struct ip_set_req_iphash *) data;
3212 +
3213 +       if (size != sizeof(struct ip_set_req_iphash)) {
3214 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3215 +                             sizeof(struct ip_set_req_iphash),
3216 +                             size);
3217 +               return -EINVAL;
3218 +       }
3219 +       return __testip(set, req->ip, hash_ip);
3220 +}
3221 +
3222 +static int
3223 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
3224 +               u_int32_t flags, ip_set_ip_t *hash_ip)
3225 +{
3226 +       return __testip(set,
3227 +                       ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
3228 +                                               : skb->nh.iph->daddr),
3229 +                       hash_ip);
3230 +}
3231 +
3232 +static inline int
3233 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3234 +{
3235 +       __u32 jhash, randhash, probe;
3236 +       u_int16_t i;
3237 +
3238 +       *hash_ip = ip & map->netmask;
3239 +       jhash = jhash_ip(map, *hash_ip);
3240 +       randhash = randhash_ip(map, *hash_ip);
3241 +       
3242 +       for (i = 0; i < map->probes; i++) {
3243 +               probe = (jhash + i * randhash) % map->hashsize;
3244 +               if (map->members[probe] == *hash_ip)
3245 +                       return -EEXIST;
3246 +               if (!map->members[probe]) {
3247 +                       map->members[probe] = *hash_ip;
3248 +                       return 0;
3249 +               }
3250 +       }
3251 +       /* Trigger rehashing */
3252 +       return -EAGAIN;
3253 +}
3254 +
3255 +static int
3256 +addip(struct ip_set *set, const void *data, size_t size,
3257 +        ip_set_ip_t *hash_ip)
3258 +{
3259 +       struct ip_set_req_iphash *req = 
3260 +           (struct ip_set_req_iphash *) data;
3261 +
3262 +       if (size != sizeof(struct ip_set_req_iphash)) {
3263 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3264 +                             sizeof(struct ip_set_req_iphash),
3265 +                             size);
3266 +               return -EINVAL;
3267 +       }
3268 +       return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3269 +}
3270 +
3271 +static int
3272 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3273 +            u_int32_t flags, ip_set_ip_t *hash_ip)
3274 +{
3275 +       return __addip((struct ip_set_iphash *) set->data,
3276 +                      ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
3277 +                                              : skb->nh.iph->daddr),
3278 +                      hash_ip);
3279 +}
3280 +
3281 +static int retry(struct ip_set *set)
3282 +{
3283 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3284 +       ip_set_ip_t hash_ip, *members;
3285 +       u_int32_t i, hashsize;
3286 +       unsigned newbytes;
3287 +       int res;
3288 +       struct ip_set_iphash tmp = {
3289 +               .hashsize = map->hashsize,
3290 +               .probes = map->probes,
3291 +               .resize = map->resize,
3292 +               .netmask = map->netmask,
3293 +       };
3294 +       
3295 +       if (map->resize == 0)
3296 +               return -ERANGE;
3297 +
3298 +    again:
3299 +       res = 0;
3300 +       
3301 +       /* Calculate new parameters */
3302 +       get_random_bytes(&tmp.initval, 4);
3303 +       hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
3304 +       if (hashsize == tmp.hashsize)
3305 +               hashsize++;
3306 +       tmp.prime = make_prime(hashsize);
3307 +       
3308 +       ip_set_printk("rehashing of set %s triggered: "
3309 +                     "hashsize grows from %u to %u",
3310 +                     set->name, tmp.hashsize, hashsize);
3311 +       tmp.hashsize = hashsize;
3312 +       
3313 +       newbytes = hashsize * sizeof(ip_set_ip_t);
3314 +       tmp.members = ip_set_malloc(newbytes);
3315 +       if (!tmp.members) {
3316 +               DP("out of memory for %d bytes", newbytes);
3317 +               return -ENOMEM;
3318 +       }
3319 +       memset(tmp.members, 0, newbytes);
3320 +       
3321 +       write_lock_bh(&set->lock);
3322 +       map = (struct ip_set_iphash *) set->data; /* Play safe */
3323 +       for (i = 0; i < map->hashsize && res == 0; i++) {
3324 +               if (map->members[i])
3325 +                       res = __addip(&tmp, map->members[i], &hash_ip);
3326 +       }
3327 +       if (res) {
3328 +               /* Failure, try again */
3329 +               write_unlock_bh(&set->lock);
3330 +               ip_set_free(tmp.members, newbytes);
3331 +               goto again;
3332 +       }
3333 +       
3334 +       /* Success at resizing! */
3335 +       members = map->members;
3336 +       hashsize = map->hashsize;
3337 +       
3338 +       map->initval = tmp.initval;
3339 +       map->prime = tmp.prime;
3340 +       map->hashsize = tmp.hashsize;
3341 +       map->members = tmp.members;
3342 +       write_unlock_bh(&set->lock);
3343 +
3344 +       ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
3345 +
3346 +       return 0;
3347 +}
3348 +
3349 +static inline int
3350 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3351 +{
3352 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3353 +       ip_set_ip_t id = hash_id(set, ip, hash_ip);
3354 +
3355 +       if (id == UINT_MAX)
3356 +               return -EEXIST;
3357 +               
3358 +       map->members[id] = 0;
3359 +       return 0;
3360 +}
3361 +
3362 +static int
3363 +delip(struct ip_set *set, const void *data, size_t size,
3364 +        ip_set_ip_t *hash_ip)
3365 +{
3366 +       struct ip_set_req_iphash *req =
3367 +           (struct ip_set_req_iphash *) data;
3368 +
3369 +       if (size != sizeof(struct ip_set_req_iphash)) {
3370 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3371 +                             sizeof(struct ip_set_req_iphash),
3372 +                             size);
3373 +               return -EINVAL;
3374 +       }
3375 +       return __delip(set, req->ip, hash_ip);
3376 +}
3377 +
3378 +static int
3379 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3380 +              u_int32_t flags, ip_set_ip_t *hash_ip)
3381 +{
3382 +       return __delip(set,
3383 +                      ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
3384 +                                              : skb->nh.iph->daddr),
3385 +                      hash_ip);
3386 +}
3387 +
3388 +static int create(struct ip_set *set, const void *data, size_t size)
3389 +{
3390 +       unsigned newbytes;
3391 +       struct ip_set_req_iphash_create *req =
3392 +           (struct ip_set_req_iphash_create *) data;
3393 +       struct ip_set_iphash *map;
3394 +
3395 +       if (size != sizeof(struct ip_set_req_iphash_create)) {
3396 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3397 +                              sizeof(struct ip_set_req_iphash_create),
3398 +                              size);
3399 +               return -EINVAL;
3400 +       }
3401 +
3402 +       if (req->hashsize < 1) {
3403 +               ip_set_printk("hashsize too small");
3404 +               return -ENOEXEC;
3405 +       }
3406 +
3407 +       map = kmalloc(sizeof(struct ip_set_iphash), GFP_KERNEL);
3408 +       if (!map) {
3409 +               DP("out of memory for %d bytes",
3410 +                  sizeof(struct ip_set_iphash));
3411 +               return -ENOMEM;
3412 +       }
3413 +       get_random_bytes(&map->initval, 4);
3414 +       map->prime = make_prime(req->hashsize);
3415 +       map->hashsize = req->hashsize;
3416 +       map->probes = req->probes;
3417 +       map->resize = req->resize;
3418 +       map->netmask = req->netmask;
3419 +       newbytes = map->hashsize * sizeof(ip_set_ip_t);
3420 +       map->members = ip_set_malloc(newbytes);
3421 +       if (!map->members) {
3422 +               DP("out of memory for %d bytes", newbytes);
3423 +               kfree(map);
3424 +               return -ENOMEM;
3425 +       }
3426 +       memset(map->members, 0, newbytes);
3427 +
3428 +       set->data = map;
3429 +       return 0;
3430 +}
3431 +
3432 +static void destroy(struct ip_set *set)
3433 +{
3434 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3435 +
3436 +       ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
3437 +       kfree(map);
3438 +
3439 +       set->data = NULL;
3440 +}
3441 +
3442 +static void flush(struct ip_set *set)
3443 +{
3444 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3445 +       memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
3446 +}
3447 +
3448 +static void list_header(const struct ip_set *set, void *data)
3449 +{
3450 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3451 +       struct ip_set_req_iphash_create *header =
3452 +           (struct ip_set_req_iphash_create *) data;
3453 +
3454 +       header->hashsize = map->hashsize;
3455 +       header->probes = map->probes;
3456 +       header->resize = map->resize;
3457 +       header->netmask = map->netmask;
3458 +}
3459 +
3460 +static int list_members_size(const struct ip_set *set)
3461 +{
3462 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3463 +
3464 +       return (map->hashsize * sizeof(ip_set_ip_t));
3465 +}
3466 +
3467 +static void list_members(const struct ip_set *set, void *data)
3468 +{
3469 +       struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3470 +       int bytes = map->hashsize * sizeof(ip_set_ip_t);
3471 +
3472 +       memcpy(data, map->members, bytes);
3473 +}
3474 +
3475 +static struct ip_set_type ip_set_iphash = {
3476 +       .typename               = SETTYPE_NAME,
3477 +       .typecode               = IPSET_TYPE_IP,
3478 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
3479 +       .create                 = &create,
3480 +       .destroy                = &destroy,
3481 +       .flush                  = &flush,
3482 +       .reqsize                = sizeof(struct ip_set_req_iphash),
3483 +       .addip                  = &addip,
3484 +       .addip_kernel           = &addip_kernel,
3485 +       .retry                  = &retry,
3486 +       .delip                  = &delip,
3487 +       .delip_kernel           = &delip_kernel,
3488 +       .testip                 = &testip,
3489 +       .testip_kernel          = &testip_kernel,
3490 +       .header_size            = sizeof(struct ip_set_req_iphash_create),
3491 +       .list_header            = &list_header,
3492 +       .list_members_size      = &list_members_size,
3493 +       .list_members           = &list_members,
3494 +       .me                     = THIS_MODULE,
3495 +};
3496 +
3497 +MODULE_LICENSE("GPL");
3498 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3499 +MODULE_DESCRIPTION("iphash type of IP sets");
3500 +
3501 +static int __init init(void)
3502 +{
3503 +       return ip_set_register_set_type(&ip_set_iphash);
3504 +}
3505 +
3506 +static void __exit fini(void)
3507 +{
3508 +       /* FIXME: possible race with ip_set_create() */
3509 +       ip_set_unregister_set_type(&ip_set_iphash);
3510 +}
3511 +
3512 +module_init(init);
3513 +module_exit(fini);
3514 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_ipmap.c
3515 ===================================================================
3516 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
3517 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_ipmap.c    2007-12-15 05:20:07.896290225 +0100
3518 @@ -0,0 +1,314 @@
3519 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3520 + *                         Patrick Schaaf <bof@bof.de>
3521 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3522 + *
3523 + * This program is free software; you can redistribute it and/or modify
3524 + * it under the terms of the GNU General Public License version 2 as
3525 + * published by the Free Software Foundation.  
3526 + */
3527 +
3528 +/* Kernel module implementing an IP set type: the single bitmap type */
3529 +
3530 +#include <linux/module.h>
3531 +#include <linux/ip.h>
3532 +#include <linux/skbuff.h>
3533 +#include <linux/netfilter_ipv4/ip_tables.h>
3534 +#include <linux/netfilter_ipv4/ip_set.h>
3535 +#include <linux/errno.h>
3536 +#include <asm/uaccess.h>
3537 +#include <asm/bitops.h>
3538 +#include <asm/softirq.h>
3539 +#include <linux/spinlock.h>
3540 +
3541 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3542 +
3543 +static inline ip_set_ip_t
3544 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3545 +{
3546 +       return (ip - map->first_ip)/map->hosts;
3547 +}
3548 +
3549 +static inline int
3550 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3551 +{
3552 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3553 +       
3554 +       if (ip < map->first_ip || ip > map->last_ip)
3555 +               return -ERANGE;
3556 +
3557 +       *hash_ip = ip & map->netmask;
3558 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3559 +          set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3560 +       return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3561 +}
3562 +
3563 +static int
3564 +testip(struct ip_set *set, const void *data, size_t size,
3565 +       ip_set_ip_t *hash_ip)
3566 +{
3567 +       struct ip_set_req_ipmap *req = 
3568 +           (struct ip_set_req_ipmap *) data;
3569 +
3570 +       if (size != sizeof(struct ip_set_req_ipmap)) {
3571 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3572 +                             sizeof(struct ip_set_req_ipmap),
3573 +                             size);
3574 +               return -EINVAL;
3575 +       }
3576 +       return __testip(set, req->ip, hash_ip);
3577 +}
3578 +
3579 +static int
3580 +testip_kernel(struct ip_set *set, 
3581 +             const struct sk_buff *skb,
3582 +             u_int32_t flags,
3583 +             ip_set_ip_t *hash_ip)
3584 +{
3585 +       int res;
3586 +       
3587 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3588 +          flags & IPSET_SRC ? "SRC" : "DST",
3589 +          NIPQUAD(skb->nh.iph->saddr),
3590 +          NIPQUAD(skb->nh.iph->daddr));
3591 +
3592 +       res =  __testip(set,
3593 +                       ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
3594 +                                               : skb->nh.iph->daddr),
3595 +                       hash_ip);
3596 +       return (res < 0 ? 0 : res);
3597 +}
3598 +
3599 +static inline int
3600 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3601 +{
3602 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3603 +
3604 +       if (ip < map->first_ip || ip > map->last_ip)
3605 +               return -ERANGE;
3606 +
3607 +       *hash_ip = ip & map->netmask;
3608 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3609 +       if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3610 +               return -EEXIST;
3611 +
3612 +       return 0;
3613 +}
3614 +
3615 +static int
3616 +addip(struct ip_set *set, const void *data, size_t size,
3617 +      ip_set_ip_t *hash_ip)
3618 +{
3619 +       struct ip_set_req_ipmap *req = 
3620 +           (struct ip_set_req_ipmap *) data;
3621 +
3622 +       if (size != sizeof(struct ip_set_req_ipmap)) {
3623 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3624 +                             sizeof(struct ip_set_req_ipmap),
3625 +                             size);
3626 +               return -EINVAL;
3627 +       }
3628 +       DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3629 +       return __addip(set, req->ip, hash_ip);
3630 +}
3631 +
3632 +static int
3633 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3634 +            u_int32_t flags, ip_set_ip_t *hash_ip)
3635 +{
3636 +       return __addip(set,
3637 +                      ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
3638 +                                              : skb->nh.iph->daddr),
3639 +                      hash_ip);
3640 +}
3641 +
3642 +static inline int 
3643 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3644 +{
3645 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3646 +
3647 +       if (ip < map->first_ip || ip > map->last_ip)
3648 +               return -ERANGE;
3649 +
3650 +       *hash_ip = ip & map->netmask;
3651 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3652 +       if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3653 +               return -EEXIST;
3654 +       
3655 +       return 0;
3656 +}
3657 +
3658 +static int
3659 +delip(struct ip_set *set, const void *data, size_t size,
3660 +      ip_set_ip_t *hash_ip)
3661 +{
3662 +       struct ip_set_req_ipmap *req =
3663 +           (struct ip_set_req_ipmap *) data;
3664 +
3665 +       if (size != sizeof(struct ip_set_req_ipmap)) {
3666 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3667 +                             sizeof(struct ip_set_req_ipmap),
3668 +                             size);
3669 +               return -EINVAL;
3670 +       }
3671 +       return __delip(set, req->ip, hash_ip);
3672 +}
3673 +
3674 +static int
3675 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3676 +            u_int32_t flags, ip_set_ip_t *hash_ip)
3677 +{
3678 +       return __delip(set,
3679 +                      ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
3680 +                                              : skb->nh.iph->daddr),
3681 +                      hash_ip);
3682 +}
3683 +
3684 +static int create(struct ip_set *set, const void *data, size_t size)
3685 +{
3686 +       int newbytes;
3687 +       struct ip_set_req_ipmap_create *req =
3688 +           (struct ip_set_req_ipmap_create *) data;
3689 +       struct ip_set_ipmap *map;
3690 +
3691 +       if (size != sizeof(struct ip_set_req_ipmap_create)) {
3692 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3693 +                             sizeof(struct ip_set_req_ipmap_create),
3694 +                             size);
3695 +               return -EINVAL;
3696 +       }
3697 +
3698 +       DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3699 +          HIPQUAD(req->from), HIPQUAD(req->to));
3700 +
3701 +       if (req->from > req->to) {
3702 +               DP("bad ip range");
3703 +               return -ENOEXEC;
3704 +       }
3705 +
3706 +       if (req->to - req->from > MAX_RANGE) {
3707 +               ip_set_printk("range too big (max %d addresses)",
3708 +                              MAX_RANGE);
3709 +               return -ENOEXEC;
3710 +       }
3711 +
3712 +       map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3713 +       if (!map) {
3714 +               DP("out of memory for %d bytes",
3715 +                  sizeof(struct ip_set_ipmap));
3716 +               return -ENOMEM;
3717 +       }
3718 +       map->first_ip = req->from;
3719 +       map->last_ip = req->to;
3720 +       map->netmask = req->netmask;
3721 +
3722 +       if (req->netmask == 0xFFFFFFFF) {
3723 +               map->hosts = 1;
3724 +               map->sizeid = map->last_ip - map->first_ip + 1;
3725 +       } else {
3726 +               unsigned int mask_bits, netmask_bits;
3727 +               ip_set_ip_t mask;
3728 +               
3729 +               map->first_ip &= map->netmask;  /* Should we better bark? */
3730 +               
3731 +               mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3732 +               netmask_bits = mask_to_bits(map->netmask);
3733 +               
3734 +               if (!mask || netmask_bits <= mask_bits)
3735 +                       return -ENOEXEC;
3736 +
3737 +               map->hosts = 2 << (32 - netmask_bits - 1);
3738 +               map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3739 +       }
3740 +       newbytes = bitmap_bytes(0, map->sizeid - 1);
3741 +       map->members = kmalloc(newbytes, GFP_KERNEL);
3742 +       if (!map->members) {
3743 +               DP("out of memory for %d bytes", newbytes);
3744 +               kfree(map);
3745 +               return -ENOMEM;
3746 +       }
3747 +       memset(map->members, 0, newbytes);
3748 +       
3749 +       set->data = map;
3750 +       return 0;
3751 +}
3752 +
3753 +static void destroy(struct ip_set *set)
3754 +{
3755 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3756 +       
3757 +       kfree(map->members);
3758 +       kfree(map);
3759 +       
3760 +       set->data = NULL;
3761 +}
3762 +
3763 +static void flush(struct ip_set *set)
3764 +{
3765 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3766 +       memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3767 +}
3768 +
3769 +static void list_header(const struct ip_set *set, void *data)
3770 +{
3771 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3772 +       struct ip_set_req_ipmap_create *header =
3773 +           (struct ip_set_req_ipmap_create *) data;
3774 +
3775 +       header->from = map->first_ip;
3776 +       header->to = map->last_ip;
3777 +       header->netmask = map->netmask;
3778 +}
3779 +
3780 +static int list_members_size(const struct ip_set *set)
3781 +{
3782 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3783 +
3784 +       return bitmap_bytes(0, map->sizeid - 1);
3785 +}
3786 +
3787 +static void list_members(const struct ip_set *set, void *data)
3788 +{
3789 +       struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3790 +       int bytes = bitmap_bytes(0, map->sizeid - 1);
3791 +
3792 +       memcpy(data, map->members, bytes);
3793 +}
3794 +
3795 +static struct ip_set_type ip_set_ipmap = {
3796 +       .typename               = SETTYPE_NAME,
3797 +       .typecode               = IPSET_TYPE_IP,
3798 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
3799 +       .create                 = &create,
3800 +       .destroy                = &destroy,
3801 +       .flush                  = &flush,
3802 +       .reqsize                = sizeof(struct ip_set_req_ipmap),
3803 +       .addip                  = &addip,
3804 +       .addip_kernel           = &addip_kernel,
3805 +       .delip                  = &delip,
3806 +       .delip_kernel           = &delip_kernel,
3807 +       .testip                 = &testip,
3808 +       .testip_kernel          = &testip_kernel,
3809 +       .header_size            = sizeof(struct ip_set_req_ipmap_create),
3810 +       .list_header            = &list_header,
3811 +       .list_members_size      = &list_members_size,
3812 +       .list_members           = &list_members,
3813 +       .me                     = THIS_MODULE,
3814 +};
3815 +
3816 +MODULE_LICENSE("GPL");
3817 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3818 +MODULE_DESCRIPTION("ipmap type of IP sets");
3819 +
3820 +static int __init init(void)
3821 +{
3822 +       return ip_set_register_set_type(&ip_set_ipmap);
3823 +}
3824 +
3825 +static void __exit fini(void)
3826 +{
3827 +       /* FIXME: possible race with ip_set_create() */
3828 +       ip_set_unregister_set_type(&ip_set_ipmap);
3829 +}
3830 +
3831 +module_init(init);
3832 +module_exit(fini);
3833 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_iptree.c
3834 ===================================================================
3835 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
3836 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_iptree.c   2007-12-15 05:20:07.896290225 +0100
3837 @@ -0,0 +1,511 @@
3838 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3839 + *
3840 + * This program is free software; you can redistribute it and/or modify
3841 + * it under the terms of the GNU General Public License version 2 as
3842 + * published by the Free Software Foundation.  
3843 + */
3844 +
3845 +/* Kernel module implementing an IP set type: the iptree type */
3846 +
3847 +#include <linux/module.h>
3848 +#include <linux/ip.h>
3849 +#include <linux/skbuff.h>
3850 +#include <linux/slab.h>
3851 +#include <linux/delay.h>
3852 +#include <linux/netfilter_ipv4/ip_tables.h>
3853 +#include <linux/netfilter_ipv4/ip_set.h>
3854 +#include <linux/errno.h>
3855 +#include <asm/uaccess.h>
3856 +#include <asm/bitops.h>
3857 +#include <linux/interrupt.h>
3858 +#include <linux/spinlock.h>
3859 +
3860 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
3861 +
3862 +/* Garbage collection interval in seconds: */
3863 +#define IPTREE_GC_TIME         5*60
3864 +/* Sleep so many milliseconds before trying again 
3865 + * to delete the gc timer at destroying a set */ 
3866 +#define IPTREE_DESTROY_SLEEP   100
3867 +
3868 +static kmem_cache_t *branch_cachep;
3869 +static kmem_cache_t *leaf_cachep;
3870 +
3871 +#define ABCD(a,b,c,d,addrp) do {               \
3872 +       a = ((unsigned char *)addrp)[3];        \
3873 +       b = ((unsigned char *)addrp)[2];        \
3874 +       c = ((unsigned char *)addrp)[1];        \
3875 +       d = ((unsigned char *)addrp)[0];        \
3876 +} while (0)
3877 +
3878 +#define TESTIP_WALK(map, elem, branch) do {    \
3879 +       if ((map)->tree[elem]) {                \
3880 +               branch = (map)->tree[elem];     \
3881 +       } else                                  \
3882 +               return 0;                       \
3883 +} while (0)
3884 +
3885 +static inline int
3886 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3887 +{
3888 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3889 +       struct ip_set_iptreeb *btree;
3890 +       struct ip_set_iptreec *ctree;
3891 +       struct ip_set_iptreed *dtree;
3892 +       unsigned char a,b,c,d;
3893 +       
3894 +       *hash_ip = ip;
3895 +       ABCD(a, b, c, d, hash_ip);
3896 +       DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
3897 +       TESTIP_WALK(map, a, btree);
3898 +       TESTIP_WALK(btree, b, ctree);
3899 +       TESTIP_WALK(ctree, c, dtree);
3900 +       DP("%lu %lu", dtree->expires[d], jiffies);
3901 +       return !!(map->timeout ? (time_after(dtree->expires[d], jiffies))
3902 +                              : dtree->expires[d]);
3903 +}
3904 +
3905 +static int
3906 +testip(struct ip_set *set, const void *data, size_t size,
3907 +       ip_set_ip_t *hash_ip)
3908 +{
3909 +       struct ip_set_req_iptree *req = 
3910 +           (struct ip_set_req_iptree *) data;
3911 +
3912 +       if (size != sizeof(struct ip_set_req_iptree)) {
3913 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3914 +                             sizeof(struct ip_set_req_iptree),
3915 +                             size);
3916 +               return -EINVAL;
3917 +       }
3918 +       return __testip(set, req->ip, hash_ip);
3919 +}
3920 +
3921 +static int
3922 +testip_kernel(struct ip_set *set, 
3923 +             const struct sk_buff *skb,
3924 +             u_int32_t flags,
3925 +             ip_set_ip_t *hash_ip)
3926 +{
3927 +       int res;
3928 +       
3929 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3930 +          flags & IPSET_SRC ? "SRC" : "DST",
3931 +          NIPQUAD(skb->nh.iph->saddr),
3932 +          NIPQUAD(skb->nh.iph->daddr));
3933 +
3934 +       res =  __testip(set,
3935 +                       ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
3936 +                                               : skb->nh.iph->daddr),
3937 +                       hash_ip);
3938 +       return (res < 0 ? 0 : res);
3939 +}
3940 +
3941 +#define ADDIP_WALK(map, elem, branch, type, cachep) do {       \
3942 +       if ((map)->tree[elem]) {                                \
3943 +               DP("found %u", elem);                           \
3944 +               branch = (map)->tree[elem];                     \
3945 +       } else {                                                \
3946 +               branch = (type *)                               \
3947 +                       kmem_cache_alloc(cachep, GFP_KERNEL);   \
3948 +               if (branch == NULL)                             \
3949 +                       return -ENOMEM;                         \
3950 +               memset(branch, 0, sizeof(*branch));             \
3951 +               (map)->tree[elem] = branch;                     \
3952 +               DP("alloc %u", elem);                           \
3953 +       }                                                       \
3954 +} while (0)    
3955 +
3956 +static inline int
3957 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
3958 +       ip_set_ip_t *hash_ip)
3959 +{
3960 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3961 +       struct ip_set_iptreeb *btree;
3962 +       struct ip_set_iptreec *ctree;
3963 +       struct ip_set_iptreed *dtree;
3964 +       unsigned char a,b,c,d;
3965 +       int ret = 0;
3966 +       
3967 +       *hash_ip = ip;
3968 +       ABCD(a, b, c, d, hash_ip);
3969 +       DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
3970 +       ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
3971 +       ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
3972 +       ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
3973 +       if (dtree->expires[d]
3974 +           && (!map->timeout || time_after(dtree->expires[d], jiffies)))
3975 +               ret = -EEXIST;
3976 +       dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
3977 +       DP("%u %lu", d, dtree->expires[d]);
3978 +       return ret;
3979 +}
3980 +
3981 +static int
3982 +addip(struct ip_set *set, const void *data, size_t size,
3983 +      ip_set_ip_t *hash_ip)
3984 +{
3985 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3986 +       struct ip_set_req_iptree *req = 
3987 +               (struct ip_set_req_iptree *) data;
3988 +
3989 +       if (size != sizeof(struct ip_set_req_iptree)) {
3990 +               ip_set_printk("data length wrong (want %zu, have %zu)",
3991 +                             sizeof(struct ip_set_req_iptree),
3992 +                             size);
3993 +               return -EINVAL;
3994 +       }
3995 +       DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
3996 +       return __addip(set, req->ip,
3997 +                      req->timeout ? req->timeout : map->timeout,
3998 +                      hash_ip);
3999 +}
4000 +
4001 +static int
4002 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4003 +            u_int32_t flags, ip_set_ip_t *hash_ip)
4004 +{
4005 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4006 +
4007 +       return __addip(set,
4008 +                      ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
4009 +                                              : skb->nh.iph->daddr),
4010 +                      map->timeout,
4011 +                      hash_ip);
4012 +}
4013 +
4014 +#define DELIP_WALK(map, elem, branch) do {     \
4015 +       if ((map)->tree[elem]) {                \
4016 +               branch = (map)->tree[elem];     \
4017 +       } else                                  \
4018 +               return -EEXIST;                 \
4019 +} while (0)
4020 +
4021 +static inline int 
4022 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4023 +{
4024 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4025 +       struct ip_set_iptreeb *btree;
4026 +       struct ip_set_iptreec *ctree;
4027 +       struct ip_set_iptreed *dtree;
4028 +       unsigned char a,b,c,d;
4029 +       
4030 +       *hash_ip = ip;
4031 +       ABCD(a, b, c, d, hash_ip);
4032 +       DELIP_WALK(map, a, btree);
4033 +       DELIP_WALK(btree, b, ctree);
4034 +       DELIP_WALK(ctree, c, dtree);
4035 +
4036 +       if (dtree->expires[d]) {
4037 +               dtree->expires[d] = 0;
4038 +               return 0;
4039 +       }
4040 +       return -EEXIST;
4041 +}
4042 +
4043 +static int
4044 +delip(struct ip_set *set, const void *data, size_t size,
4045 +      ip_set_ip_t *hash_ip)
4046 +{
4047 +       struct ip_set_req_iptree *req =
4048 +           (struct ip_set_req_iptree *) data;
4049 +
4050 +       if (size != sizeof(struct ip_set_req_iptree)) {
4051 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4052 +                             sizeof(struct ip_set_req_iptree),
4053 +                             size);
4054 +               return -EINVAL;
4055 +       }
4056 +       return __delip(set, req->ip, hash_ip);
4057 +}
4058 +
4059 +static int
4060 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4061 +            u_int32_t flags, ip_set_ip_t *hash_ip)
4062 +{
4063 +       return __delip(set,
4064 +                      ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
4065 +                                              : skb->nh.iph->daddr),
4066 +                      hash_ip);
4067 +}
4068 +
4069 +#define LOOP_WALK_BEGIN(map, i, branch) \
4070 +       for (i = 0; i < 255; i++) {     \
4071 +               if (!(map)->tree[i])    \
4072 +                       continue;       \
4073 +               branch = (map)->tree[i]
4074 +
4075 +#define LOOP_WALK_END }
4076 +
4077 +static void ip_tree_gc(unsigned long ul_set)
4078 +{
4079 +       struct ip_set *set = (void *) ul_set;
4080 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4081 +       struct ip_set_iptreeb *btree;
4082 +       struct ip_set_iptreec *ctree;
4083 +       struct ip_set_iptreed *dtree;
4084 +       unsigned char a,b,c,d;
4085 +       unsigned char i,j,k;
4086 +
4087 +       i = j = k = 0;
4088 +       DP("gc: %s", set->name);
4089 +       write_lock_bh(&set->lock);
4090 +       LOOP_WALK_BEGIN(map, a, btree);
4091 +       LOOP_WALK_BEGIN(btree, b, ctree);
4092 +       LOOP_WALK_BEGIN(ctree, c, dtree);
4093 +       for (d = 0; d < 255; d++) {
4094 +               if (dtree->expires[d]) {
4095 +                       DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4096 +                           a, b, c, d,
4097 +                           dtree->expires[d], jiffies);
4098 +                       if (map->timeout
4099 +                           && time_before(dtree->expires[d], jiffies))
4100 +                               dtree->expires[d] = 0;
4101 +                       else
4102 +                               k = 1;
4103 +               }
4104 +       }
4105 +       if (k == 0) {
4106 +               DP("gc: %s: leaf %u %u %u empty",
4107 +                   set->name, a, b, c);
4108 +               kmem_cache_free(leaf_cachep, dtree);
4109 +               ctree->tree[c] = NULL;
4110 +       } else {
4111 +               DP("gc: %s: leaf %u %u %u not empty",
4112 +                   set->name, a, b, c);
4113 +               j = 1;
4114 +               k = 0;
4115 +       }
4116 +       LOOP_WALK_END;
4117 +       if (j == 0) {
4118 +               DP("gc: %s: branch %u %u empty",
4119 +                   set->name, a, b);
4120 +               kmem_cache_free(branch_cachep, ctree);
4121 +               btree->tree[b] = NULL;
4122 +       } else {
4123 +               DP("gc: %s: branch %u %u not empty",
4124 +                   set->name, a, b);
4125 +               i = 1;
4126 +               j = k = 0;
4127 +       }
4128 +       LOOP_WALK_END;
4129 +       if (i == 0) {
4130 +               DP("gc: %s: branch %u empty",
4131 +                   set->name, a);
4132 +               kmem_cache_free(branch_cachep, btree);
4133 +               map->tree[a] = NULL;
4134 +       } else {
4135 +               DP("gc: %s: branch %u not empty",
4136 +                   set->name, a);
4137 +               i = j = k = 0;
4138 +       }
4139 +       LOOP_WALK_END;
4140 +       write_unlock_bh(&set->lock);
4141 +       
4142 +       map->gc.expires = jiffies + map->gc_interval * HZ;
4143 +       add_timer(&map->gc);
4144 +}
4145 +
4146 +static int create(struct ip_set *set, const void *data, size_t size)
4147 +{
4148 +       struct ip_set_req_iptree_create *req =
4149 +           (struct ip_set_req_iptree_create *) data;
4150 +       struct ip_set_iptree *map;
4151 +
4152 +       if (size != sizeof(struct ip_set_req_iptree_create)) {
4153 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4154 +                             sizeof(struct ip_set_req_iptree_create),
4155 +                             size);
4156 +               return -EINVAL;
4157 +       }
4158 +
4159 +       map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4160 +       if (!map) {
4161 +               DP("out of memory for %d bytes",
4162 +                  sizeof(struct ip_set_iptree));
4163 +               return -ENOMEM;
4164 +       }
4165 +       memset(map, 0, sizeof(*map));
4166 +       map->timeout = req->timeout;
4167 +       set->data = map;
4168 +
4169 +       /* If there is no timeout for the entries,
4170 +        * we still have to call gc because delete
4171 +        * do not clean up empty branches */
4172 +       map->gc_interval = IPTREE_GC_TIME;
4173 +       init_timer(&map->gc);
4174 +       map->gc.data = (unsigned long) set;
4175 +       map->gc.function = ip_tree_gc;
4176 +       map->gc.expires = jiffies + map->gc_interval * HZ;
4177 +       add_timer(&map->gc);
4178 +       
4179 +       return 0;
4180 +}
4181 +
4182 +static void __flush(struct ip_set_iptree *map)
4183 +{
4184 +       struct ip_set_iptreeb *btree;
4185 +       struct ip_set_iptreec *ctree;
4186 +       struct ip_set_iptreed *dtree;
4187 +       unsigned int a,b,c;
4188 +
4189 +       LOOP_WALK_BEGIN(map, a, btree);
4190 +       LOOP_WALK_BEGIN(btree, b, ctree);
4191 +       LOOP_WALK_BEGIN(ctree, c, dtree);
4192 +       kmem_cache_free(leaf_cachep, dtree);
4193 +       LOOP_WALK_END;
4194 +       kmem_cache_free(branch_cachep, ctree);
4195 +       LOOP_WALK_END;
4196 +       kmem_cache_free(branch_cachep, btree);
4197 +       LOOP_WALK_END;
4198 +}
4199 +
4200 +static void destroy(struct ip_set *set)
4201 +{
4202 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4203 +
4204 +       while (!del_timer(&map->gc))
4205 +               msleep(IPTREE_DESTROY_SLEEP);
4206 +       __flush(map);
4207 +       kfree(map);
4208 +       set->data = NULL;
4209 +}
4210 +
4211 +static void flush(struct ip_set *set)
4212 +{
4213 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4214 +       unsigned int timeout = map->timeout;
4215 +       
4216 +       __flush(map);
4217 +       memset(map, 0, sizeof(*map));
4218 +       map->timeout = timeout;
4219 +}
4220 +
4221 +static void list_header(const struct ip_set *set, void *data)
4222 +{
4223 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4224 +       struct ip_set_req_iptree_create *header =
4225 +           (struct ip_set_req_iptree_create *) data;
4226 +
4227 +       header->timeout = map->timeout;
4228 +}
4229 +
4230 +static int list_members_size(const struct ip_set *set)
4231 +{
4232 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4233 +       struct ip_set_iptreeb *btree;
4234 +       struct ip_set_iptreec *ctree;
4235 +       struct ip_set_iptreed *dtree;
4236 +       unsigned char a,b,c,d;
4237 +       unsigned int count = 0;
4238 +
4239 +       LOOP_WALK_BEGIN(map, a, btree);
4240 +       LOOP_WALK_BEGIN(btree, b, ctree);
4241 +       LOOP_WALK_BEGIN(ctree, c, dtree);
4242 +       for (d = 0; d < 255; d++) {
4243 +               if (dtree->expires[d]
4244 +                   && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4245 +                       count++;
4246 +       }
4247 +       LOOP_WALK_END;
4248 +       LOOP_WALK_END;
4249 +       LOOP_WALK_END;
4250 +
4251 +       DP("members %u", count);
4252 +       return (count * sizeof(struct ip_set_req_iptree));
4253 +}
4254 +
4255 +static void list_members(const struct ip_set *set, void *data)
4256 +{
4257 +       struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4258 +       struct ip_set_iptreeb *btree;
4259 +       struct ip_set_iptreec *ctree;
4260 +       struct ip_set_iptreed *dtree;
4261 +       unsigned char a,b,c,d;
4262 +       size_t offset = 0;
4263 +       struct ip_set_req_iptree *entry;
4264 +
4265 +       LOOP_WALK_BEGIN(map, a, btree);
4266 +       LOOP_WALK_BEGIN(btree, b, ctree);
4267 +       LOOP_WALK_BEGIN(ctree, c, dtree);
4268 +       for (d = 0; d < 255; d++) {
4269 +               if (dtree->expires[d]
4270 +                   && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
4271 +                       entry = (struct ip_set_req_iptree *)(data + offset);
4272 +                       entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
4273 +                       entry->timeout = !map->timeout ? 0 
4274 +                               : (dtree->expires[d] - jiffies)/HZ;
4275 +                       offset += sizeof(struct ip_set_req_iptree);
4276 +               }
4277 +       }
4278 +       LOOP_WALK_END;
4279 +       LOOP_WALK_END;
4280 +       LOOP_WALK_END;
4281 +}
4282 +
4283 +static struct ip_set_type ip_set_iptree = {
4284 +       .typename               = SETTYPE_NAME,
4285 +       .typecode               = IPSET_TYPE_IP,
4286 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
4287 +       .create                 = &create,
4288 +       .destroy                = &destroy,
4289 +       .flush                  = &flush,
4290 +       .reqsize                = sizeof(struct ip_set_req_iptree),
4291 +       .addip                  = &addip,
4292 +       .addip_kernel           = &addip_kernel,
4293 +       .delip                  = &delip,
4294 +       .delip_kernel           = &delip_kernel,
4295 +       .testip                 = &testip,
4296 +       .testip_kernel          = &testip_kernel,
4297 +       .header_size            = sizeof(struct ip_set_req_iptree_create),
4298 +       .list_header            = &list_header,
4299 +       .list_members_size      = &list_members_size,
4300 +       .list_members           = &list_members,
4301 +       .me                     = THIS_MODULE,
4302 +};
4303 +
4304 +MODULE_LICENSE("GPL");
4305 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4306 +MODULE_DESCRIPTION("iptree type of IP sets");
4307 +
4308 +static int __init init(void)
4309 +{
4310 +       int ret;
4311 +       
4312 +       branch_cachep = kmem_cache_create("ip_set_iptreeb",
4313 +                               sizeof(struct ip_set_iptreeb),
4314 +                               0, 0, NULL, NULL);
4315 +       if (!branch_cachep) {
4316 +               printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
4317 +               ret = -ENOMEM;
4318 +               goto out;
4319 +       }
4320 +       leaf_cachep = kmem_cache_create("ip_set_iptreed",
4321 +                               sizeof(struct ip_set_iptreed),
4322 +                               0, 0, NULL, NULL);
4323 +       if (!leaf_cachep) {
4324 +               printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
4325 +               ret = -ENOMEM;
4326 +               goto free_branch;
4327 +       }
4328 +       ret = ip_set_register_set_type(&ip_set_iptree);
4329 +       if (ret == 0)
4330 +               goto out;
4331 +
4332 +       kmem_cache_destroy(leaf_cachep);
4333 +    free_branch:       
4334 +       kmem_cache_destroy(branch_cachep);
4335 +    out:
4336 +       return ret;
4337 +}
4338 +
4339 +static void __exit fini(void)
4340 +{
4341 +       /* FIXME: possible race with ip_set_create() */
4342 +       ip_set_unregister_set_type(&ip_set_iptree);
4343 +       kmem_cache_destroy(leaf_cachep);
4344 +       kmem_cache_destroy(branch_cachep);
4345 +}
4346 +
4347 +module_init(init);
4348 +module_exit(fini);
4349 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_macipmap.c
4350 ===================================================================
4351 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
4352 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_macipmap.c 2007-12-15 05:20:07.896290225 +0100
4353 @@ -0,0 +1,340 @@
4354 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
4355 + *                         Patrick Schaaf <bof@bof.de>
4356 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
4357 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4358 + *
4359 + * This program is free software; you can redistribute it and/or modify
4360 + * it under the terms of the GNU General Public License version 2 as
4361 + * published by the Free Software Foundation.  
4362 + */
4363 +
4364 +/* Kernel module implementing an IP set type: the macipmap type */
4365 +
4366 +#include <linux/module.h>
4367 +#include <linux/ip.h>
4368 +#include <linux/skbuff.h>
4369 +#include <linux/netfilter_ipv4/ip_tables.h>
4370 +#include <linux/netfilter_ipv4/ip_set.h>
4371 +#include <linux/errno.h>
4372 +#include <asm/uaccess.h>
4373 +#include <asm/bitops.h>
4374 +#include <asm/softirq.h>
4375 +#include <linux/spinlock.h>
4376 +#include <linux/if_ether.h>
4377 +#include <linux/vmalloc.h>
4378 +
4379 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4380 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
4381 +
4382 +static int
4383 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
4384 +{
4385 +       struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
4386 +       struct ip_set_macip *table = (struct ip_set_macip *) map->members;      
4387 +       struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
4388 +
4389 +       if (size != sizeof(struct ip_set_req_macipmap)) {
4390 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4391 +                             sizeof(struct ip_set_req_macipmap),
4392 +                             size);
4393 +               return -EINVAL;
4394 +       }
4395 +
4396 +       if (req->ip < map->first_ip || req->ip > map->last_ip)
4397 +               return -ERANGE;
4398 +
4399 +       *hash_ip = req->ip;
4400 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4401 +          set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));             
4402 +       if (test_bit(IPSET_MACIP_ISSET,
4403 +                    &table[req->ip - map->first_ip].flags)) {
4404 +               return (memcmp(req->ethernet,
4405 +                              &table[req->ip - map->first_ip].ethernet,
4406 +                              ETH_ALEN) == 0);
4407 +       } else {
4408 +               return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4409 +       }
4410 +}
4411 +
4412 +static int
4413 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4414 +             u_int32_t flags, ip_set_ip_t *hash_ip)
4415 +{
4416 +       struct ip_set_macipmap *map =
4417 +           (struct ip_set_macipmap *) set->data;
4418 +       struct ip_set_macip *table =
4419 +           (struct ip_set_macip *) map->members;
4420 +       ip_set_ip_t ip;
4421 +       
4422 +       ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4423 +                                    : skb->nh.iph->daddr);
4424 +       DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4425 +          flags & IPSET_SRC ? "SRC" : "DST",
4426 +          NIPQUAD(skb->nh.iph->saddr),
4427 +          NIPQUAD(skb->nh.iph->daddr));
4428 +
4429 +       if (ip < map->first_ip || ip > map->last_ip)
4430 +               return 0;
4431 +
4432 +       *hash_ip = ip;  
4433 +       DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4434 +          set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));          
4435 +       if (test_bit(IPSET_MACIP_ISSET, &table[ip - map->first_ip].flags)) {
4436 +               /* Is mac pointer valid?
4437 +                * If so, compare... */
4438 +               return (skb->mac.raw >= skb->head
4439 +                       && (skb->mac.raw + ETH_HLEN) <= skb->data
4440 +                       && (memcmp(skb->mac.ethernet->h_source,
4441 +                                  &table[ip - map->first_ip].ethernet,
4442 +                                  ETH_ALEN) == 0));
4443 +       } else {
4444 +               return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4445 +       }
4446 +}
4447 +
4448 +/* returns 0 on success */
4449 +static inline int
4450 +__addip(struct ip_set *set, 
4451 +       ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
4452 +{
4453 +       struct ip_set_macipmap *map =
4454 +           (struct ip_set_macipmap *) set->data;
4455 +       struct ip_set_macip *table =
4456 +           (struct ip_set_macip *) map->members;
4457 +
4458 +       if (ip < map->first_ip || ip > map->last_ip)
4459 +               return -ERANGE;
4460 +       if (test_and_set_bit(IPSET_MACIP_ISSET, 
4461 +                            &table[ip - map->first_ip].flags))
4462 +               return -EEXIST;
4463 +
4464 +       *hash_ip = ip;
4465 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4466 +       memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
4467 +       return 0;
4468 +}
4469 +
4470 +static int
4471 +addip(struct ip_set *set, const void *data, size_t size,
4472 +      ip_set_ip_t *hash_ip)
4473 +{
4474 +       struct ip_set_req_macipmap *req =
4475 +           (struct ip_set_req_macipmap *) data;
4476 +
4477 +       if (size != sizeof(struct ip_set_req_macipmap)) {
4478 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4479 +                             sizeof(struct ip_set_req_macipmap),
4480 +                             size);
4481 +               return -EINVAL;
4482 +       }
4483 +       return __addip(set, req->ip, req->ethernet, hash_ip);
4484 +}
4485 +
4486 +static int
4487 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4488 +            u_int32_t flags, ip_set_ip_t *hash_ip)
4489 +{
4490 +       ip_set_ip_t ip;
4491 +       
4492 +       ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4493 +                                    : skb->nh.iph->daddr);
4494 +
4495 +       if (!(skb->mac.raw >= skb->head
4496 +             && (skb->mac.raw + ETH_HLEN) <= skb->data))
4497 +               return -EINVAL;
4498 +
4499 +       return __addip(set, ip, skb->mac.ethernet->h_source, hash_ip);
4500 +}
4501 +
4502 +static inline int
4503 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4504 +{
4505 +       struct ip_set_macipmap *map =
4506 +           (struct ip_set_macipmap *) set->data;
4507 +       struct ip_set_macip *table =
4508 +           (struct ip_set_macip *) map->members;
4509 +
4510 +       if (ip < map->first_ip || ip > map->last_ip)
4511 +               return -ERANGE;
4512 +       if (!test_and_clear_bit(IPSET_MACIP_ISSET, 
4513 +                               &table[ip - map->first_ip].flags))
4514 +               return -EEXIST;
4515 +
4516 +       *hash_ip = ip;
4517 +       DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4518 +       return 0;
4519 +}
4520 +
4521 +static int
4522 +delip(struct ip_set *set, const void *data, size_t size,
4523 +     ip_set_ip_t *hash_ip)
4524 +{
4525 +       struct ip_set_req_macipmap *req =
4526 +           (struct ip_set_req_macipmap *) data;
4527 +
4528 +       if (size != sizeof(struct ip_set_req_macipmap)) {
4529 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4530 +                             sizeof(struct ip_set_req_macipmap),
4531 +                             size);
4532 +               return -EINVAL;
4533 +       }
4534 +       return __delip(set, req->ip, hash_ip);
4535 +}
4536 +
4537 +static int
4538 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4539 +            u_int32_t flags, ip_set_ip_t *hash_ip)
4540 +{
4541 +       return __delip(set,
4542 +                      ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
4543 +                                              : skb->nh.iph->daddr),
4544 +                      hash_ip);
4545 +}
4546 +
4547 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
4548 +{
4549 +       return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
4550 +}
4551 +
4552 +static int create(struct ip_set *set, const void *data, size_t size)
4553 +{
4554 +       int newbytes;
4555 +       struct ip_set_req_macipmap_create *req =
4556 +           (struct ip_set_req_macipmap_create *) data;
4557 +       struct ip_set_macipmap *map;
4558 +
4559 +       if (size != sizeof(struct ip_set_req_macipmap_create)) {
4560 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4561 +                             sizeof(struct ip_set_req_macipmap_create),
4562 +                             size);
4563 +               return -EINVAL;
4564 +       }
4565 +
4566 +       DP("from %u.%u.%u.%u to %u.%u.%u.%u",
4567 +          HIPQUAD(req->from), HIPQUAD(req->to));
4568 +
4569 +       if (req->from > req->to) {
4570 +               DP("bad ip range");
4571 +               return -ENOEXEC;
4572 +       }
4573 +
4574 +       if (req->to - req->from > MAX_RANGE) {
4575 +               ip_set_printk("range too big (max %d addresses)",
4576 +                              MAX_RANGE);
4577 +               return -ENOEXEC;
4578 +       }
4579 +
4580 +       map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
4581 +       if (!map) {
4582 +               DP("out of memory for %d bytes",
4583 +                  sizeof(struct ip_set_macipmap));
4584 +               return -ENOMEM;
4585 +       }
4586 +       map->flags = req->flags;
4587 +       map->first_ip = req->from;
4588 +       map->last_ip = req->to;
4589 +       newbytes = members_size(map->first_ip, map->last_ip);
4590 +       map->members = ip_set_malloc(newbytes);
4591 +       DP("members: %u %p", newbytes, map->members);
4592 +       if (!map->members) {
4593 +               DP("out of memory for %d bytes", newbytes);
4594 +               kfree(map);
4595 +               return -ENOMEM;
4596 +       }
4597 +       memset(map->members, 0, newbytes);
4598 +       
4599 +       set->data = map;
4600 +       return 0;
4601 +}
4602 +
4603 +static void destroy(struct ip_set *set)
4604 +{
4605 +       struct ip_set_macipmap *map =
4606 +           (struct ip_set_macipmap *) set->data;
4607 +
4608 +       ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
4609 +       kfree(map);
4610 +
4611 +       set->data = NULL;
4612 +}
4613 +
4614 +static void flush(struct ip_set *set)
4615 +{
4616 +       struct ip_set_macipmap *map =
4617 +           (struct ip_set_macipmap *) set->data;
4618 +       memset(map->members, 0, members_size(map->first_ip, map->last_ip));
4619 +}
4620 +
4621 +static void list_header(const struct ip_set *set, void *data)
4622 +{
4623 +       struct ip_set_macipmap *map =
4624 +           (struct ip_set_macipmap *) set->data;
4625 +       struct ip_set_req_macipmap_create *header =
4626 +           (struct ip_set_req_macipmap_create *) data;
4627 +
4628 +       DP("list_header %x %x %u", map->first_ip, map->last_ip,
4629 +          map->flags);
4630 +
4631 +       header->from = map->first_ip;
4632 +       header->to = map->last_ip;
4633 +       header->flags = map->flags;
4634 +}
4635 +
4636 +static int list_members_size(const struct ip_set *set)
4637 +{
4638 +       struct ip_set_macipmap *map =
4639 +           (struct ip_set_macipmap *) set->data;
4640 +
4641 +       DP("%u", members_size(map->first_ip, map->last_ip));
4642 +       return members_size(map->first_ip, map->last_ip);
4643 +}
4644 +
4645 +static void list_members(const struct ip_set *set, void *data)
4646 +{
4647 +       struct ip_set_macipmap *map =
4648 +           (struct ip_set_macipmap *) set->data;
4649 +
4650 +       int bytes = members_size(map->first_ip, map->last_ip);
4651 +
4652 +       DP("members: %u %p", bytes, map->members);
4653 +       memcpy(data, map->members, bytes);
4654 +}
4655 +
4656 +static struct ip_set_type ip_set_macipmap = {
4657 +       .typename               = SETTYPE_NAME,
4658 +       .typecode               = IPSET_TYPE_IP,
4659 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
4660 +       .create                 = &create,
4661 +       .destroy                = &destroy,
4662 +       .flush                  = &flush,
4663 +       .reqsize                = sizeof(struct ip_set_req_macipmap),
4664 +       .addip                  = &addip,
4665 +       .addip_kernel           = &addip_kernel,
4666 +       .delip                  = &delip,
4667 +       .delip_kernel           = &delip_kernel,
4668 +       .testip                 = &testip,
4669 +       .testip_kernel          = &testip_kernel,
4670 +       .header_size            = sizeof(struct ip_set_req_macipmap_create),
4671 +       .list_header            = &list_header,
4672 +       .list_members_size      = &list_members_size,
4673 +       .list_members           = &list_members,
4674 +       .me                     = THIS_MODULE,
4675 +};
4676 +
4677 +MODULE_LICENSE("GPL");
4678 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4679 +MODULE_DESCRIPTION("macipmap type of IP sets");
4680 +
4681 +static int __init init(void)
4682 +{
4683 +       return ip_set_register_set_type(&ip_set_macipmap);
4684 +}
4685 +
4686 +static void __exit fini(void)
4687 +{
4688 +       /* FIXME: possible race with ip_set_create() */
4689 +       ip_set_unregister_set_type(&ip_set_macipmap);
4690 +}
4691 +
4692 +module_init(init);
4693 +module_exit(fini);
4694 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_nethash.c
4695 ===================================================================
4696 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
4697 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_nethash.c  2007-12-15 05:20:07.900290454 +0100
4698 @@ -0,0 +1,450 @@
4699 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4700 + *
4701 + * This program is free software; you can redistribute it and/or modify
4702 + * it under the terms of the GNU General Public License version 2 as
4703 + * published by the Free Software Foundation.  
4704 + */
4705 +
4706 +/* Kernel module implementing a cidr nethash set */
4707 +
4708 +#include <linux/module.h>
4709 +#include <linux/ip.h>
4710 +#include <linux/skbuff.h>
4711 +#include <linux/netfilter_ipv4/ip_tables.h>
4712 +#include <linux/netfilter_ipv4/ip_set.h>
4713 +#include <linux/errno.h>
4714 +#include <asm/uaccess.h>
4715 +#include <asm/bitops.h>
4716 +#include <asm/softirq.h>
4717 +#include <linux/spinlock.h>
4718 +#include <linux/vmalloc.h>
4719 +#include <linux/random.h>
4720 +
4721 +#include <net/ip.h>
4722 +
4723 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4724 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
4725 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
4726 +#include <linux/netfilter_ipv4/ip_set_prime.h>
4727 +
4728 +static inline __u32
4729 +jhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4730 +{
4731 +       return jhash_1word(ip, map->initval);
4732 +}
4733 +
4734 +static inline __u32
4735 +randhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4736 +{
4737 +       return (1 + ip % map->prime);
4738 +}
4739 +
4740 +static inline __u32
4741 +hash_id_cidr(struct ip_set_nethash *map,
4742 +            ip_set_ip_t ip,
4743 +            unsigned char cidr,
4744 +            ip_set_ip_t *hash_ip)
4745 +{
4746 +       __u32 jhash, randhash, id;
4747 +       u_int16_t i;
4748 +
4749 +       *hash_ip = pack(ip, cidr);
4750 +       jhash = jhash_ip(map, *hash_ip);
4751 +       randhash = randhash_ip(map, *hash_ip);
4752 +       
4753 +       for (i = 0; i < map->probes; i++) {
4754 +               id = (jhash + i * randhash) % map->hashsize;
4755 +               DP("hash key: %u", id);
4756 +               if (map->members[id] == *hash_ip)
4757 +                       return id;
4758 +       }
4759 +       return UINT_MAX;
4760 +}
4761 +
4762 +static inline __u32
4763 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4764 +{
4765 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4766 +       __u32 id = UINT_MAX;
4767 +       int i;
4768 +
4769 +       for (i = 0; i < 30 && map->cidr[i]; i++) {
4770 +               id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
4771 +               if (id != UINT_MAX)
4772 +                       break;
4773 +       }
4774 +       return id;
4775 +}
4776 +
4777 +static inline int
4778 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
4779 +             ip_set_ip_t *hash_ip)
4780 +{
4781 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4782 +
4783 +       return (hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
4784 +}
4785 +
4786 +static inline int
4787 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4788 +{
4789 +       return (hash_id(set, ip, hash_ip) != UINT_MAX);
4790 +}
4791 +
4792 +static int
4793 +testip(struct ip_set *set, const void *data, size_t size,
4794 +       ip_set_ip_t *hash_ip)
4795 +{
4796 +       struct ip_set_req_nethash *req = 
4797 +           (struct ip_set_req_nethash *) data;
4798 +
4799 +       if (size != sizeof(struct ip_set_req_nethash)) {
4800 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4801 +                             sizeof(struct ip_set_req_nethash),
4802 +                             size);
4803 +               return -EINVAL;
4804 +       }
4805 +       return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
4806 +               : __testip_cidr(set, req->ip, req->cidr, hash_ip));
4807 +}
4808 +
4809 +static int
4810 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4811 +               u_int32_t flags, ip_set_ip_t *hash_ip)
4812 +{
4813 +       return __testip(set,
4814 +                       ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr 
4815 +                                               : skb->nh.iph->daddr),
4816 +                       hash_ip);
4817 +}
4818 +
4819 +static inline int
4820 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
4821 +{
4822 +       __u32 jhash, randhash, probe;
4823 +       u_int16_t i;
4824 +
4825 +       jhash = jhash_ip(map, ip);
4826 +       randhash = randhash_ip(map, ip);
4827 +       
4828 +       for (i = 0; i < map->probes; i++) {
4829 +               probe = (jhash + i * randhash) % map->hashsize;
4830 +               if (map->members[probe] == ip)
4831 +                       return -EEXIST;
4832 +               if (!map->members[probe]) {
4833 +                       map->members[probe] = ip;
4834 +                       return 0;
4835 +               }
4836 +       }
4837 +       /* Trigger rehashing */
4838 +       return -EAGAIN;
4839 +}
4840 +
4841 +static inline int
4842 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4843 +       ip_set_ip_t *hash_ip)
4844 +{
4845 +       *hash_ip = pack(ip, cidr);
4846 +       DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
4847 +       
4848 +       return __addip_base(map, *hash_ip);
4849 +}
4850 +
4851 +static void
4852 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
4853 +{
4854 +       unsigned char next;
4855 +       int i;
4856 +       
4857 +       for (i = 0; i < 30 && map->cidr[i]; i++) {
4858 +               if (map->cidr[i] == cidr) {
4859 +                       return;
4860 +               } else if (map->cidr[i] < cidr) {
4861 +                       next = map->cidr[i];
4862 +                       map->cidr[i] = cidr;
4863 +                       cidr = next;
4864 +               }
4865 +       }
4866 +       if (i < 30)
4867 +               map->cidr[i] = cidr;
4868 +}
4869 +
4870 +static int
4871 +addip(struct ip_set *set, const void *data, size_t size,
4872 +        ip_set_ip_t *hash_ip)
4873 +{
4874 +       struct ip_set_req_nethash *req = 
4875 +           (struct ip_set_req_nethash *) data;
4876 +       int ret;
4877 +
4878 +       if (size != sizeof(struct ip_set_req_nethash)) {
4879 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4880 +                             sizeof(struct ip_set_req_nethash),
4881 +                             size);
4882 +               return -EINVAL;
4883 +       }
4884 +       ret = __addip((struct ip_set_nethash *) set->data, 
4885 +                     req->ip, req->cidr, hash_ip);
4886 +       
4887 +       if (ret == 0)
4888 +               update_cidr_sizes((struct ip_set_nethash *) set->data,
4889 +                                 req->cidr);
4890 +       
4891 +       return ret;
4892 +}
4893 +
4894 +static int
4895 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4896 +            u_int32_t flags, ip_set_ip_t *hash_ip)
4897 +{
4898 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4899 +       int ret = -ERANGE;
4900 +       ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4901 +                                                : skb->nh.iph->daddr);
4902 +       
4903 +       if (map->cidr[0])
4904 +               ret = __addip(map, ip, map->cidr[0], hash_ip);
4905 +               
4906 +       return ret;
4907 +}
4908 +
4909 +static int retry(struct ip_set *set)
4910 +{
4911 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4912 +       ip_set_ip_t *members;
4913 +       u_int32_t i, hashsize;
4914 +       unsigned newbytes;
4915 +       int res;
4916 +       struct ip_set_nethash tmp = {
4917 +               .hashsize = map->hashsize,
4918 +               .probes = map->probes,
4919 +               .resize = map->resize
4920 +       };
4921 +       
4922 +       if (map->resize == 0)
4923 +               return -ERANGE;
4924 +
4925 +       memcpy(tmp.cidr, map->cidr, 30 * sizeof(unsigned char));
4926 +    again:
4927 +       res = 0;
4928 +       
4929 +       /* Calculate new parameters */
4930 +       get_random_bytes(&tmp.initval, 4);
4931 +       hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
4932 +       if (hashsize == tmp.hashsize)
4933 +               hashsize++;
4934 +       tmp.prime = make_prime(hashsize);
4935 +       
4936 +       ip_set_printk("rehashing of set %s triggered: "
4937 +                     "hashsize grows from %u to %u",
4938 +                     set->name, tmp.hashsize, hashsize);
4939 +       tmp.hashsize = hashsize;
4940 +       
4941 +       newbytes = hashsize * sizeof(ip_set_ip_t);
4942 +       tmp.members = ip_set_malloc(newbytes);
4943 +       if (!tmp.members) {
4944 +               DP("out of memory for %d bytes", newbytes);
4945 +               return -ENOMEM;
4946 +       }
4947 +       memset(tmp.members, 0, newbytes);
4948 +       
4949 +       write_lock_bh(&set->lock);
4950 +       map = (struct ip_set_nethash *) set->data; /* Play safe */
4951 +       for (i = 0; i < map->hashsize && res == 0; i++) {
4952 +               if (map->members[i])
4953 +                       res = __addip_base(&tmp, map->members[i]);
4954 +       }
4955 +       if (res) {
4956 +               /* Failure, try again */
4957 +               write_unlock_bh(&set->lock);
4958 +               ip_set_free(tmp.members, newbytes);
4959 +               goto again;
4960 +       }
4961 +       
4962 +       /* Success at resizing! */
4963 +       members = map->members;
4964 +       hashsize = map->hashsize;
4965 +       
4966 +       map->initval = tmp.initval;
4967 +       map->prime = tmp.prime;
4968 +       map->hashsize = tmp.hashsize;
4969 +       map->members = tmp.members;
4970 +       write_unlock_bh(&set->lock);
4971 +
4972 +       ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
4973 +
4974 +       return 0;
4975 +}
4976 +
4977 +static inline int
4978 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4979 +       ip_set_ip_t *hash_ip)
4980 +{
4981 +       ip_set_ip_t id = hash_id_cidr(map, ip, cidr, hash_ip);
4982 +
4983 +       if (id == UINT_MAX)
4984 +               return -EEXIST;
4985 +               
4986 +       map->members[id] = 0;
4987 +       return 0;
4988 +}
4989 +
4990 +static int
4991 +delip(struct ip_set *set, const void *data, size_t size,
4992 +        ip_set_ip_t *hash_ip)
4993 +{
4994 +       struct ip_set_req_nethash *req =
4995 +           (struct ip_set_req_nethash *) data;
4996 +
4997 +       if (size != sizeof(struct ip_set_req_nethash)) {
4998 +               ip_set_printk("data length wrong (want %zu, have %zu)",
4999 +                             sizeof(struct ip_set_req_nethash),
5000 +                             size);
5001 +               return -EINVAL;
5002 +       }
5003 +       /* TODO: no garbage collection in map->cidr */          
5004 +       return __delip((struct ip_set_nethash *) set->data, 
5005 +                      req->ip, req->cidr, hash_ip);
5006 +}
5007 +
5008 +static int
5009 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
5010 +              u_int32_t flags, ip_set_ip_t *hash_ip)
5011 +{
5012 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5013 +       int ret = -ERANGE;
5014 +       ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
5015 +                                                : skb->nh.iph->daddr);
5016 +       
5017 +       if (map->cidr[0])
5018 +               ret = __delip(map, ip, map->cidr[0], hash_ip);
5019 +       
5020 +       return ret;
5021 +}
5022 +
5023 +static int create(struct ip_set *set, const void *data, size_t size)
5024 +{
5025 +       unsigned newbytes;
5026 +       struct ip_set_req_nethash_create *req =
5027 +           (struct ip_set_req_nethash_create *) data;
5028 +       struct ip_set_nethash *map;
5029 +
5030 +       if (size != sizeof(struct ip_set_req_nethash_create)) {
5031 +               ip_set_printk("data length wrong (want %zu, have %zu)",
5032 +                              sizeof(struct ip_set_req_nethash_create),
5033 +                              size);
5034 +               return -EINVAL;
5035 +       }
5036 +
5037 +       if (req->hashsize < 1) {
5038 +               ip_set_printk("hashsize too small");
5039 +               return -ENOEXEC;
5040 +       }
5041 +
5042 +       map = kmalloc(sizeof(struct ip_set_nethash), GFP_KERNEL);
5043 +       if (!map) {
5044 +               DP("out of memory for %d bytes",
5045 +                  sizeof(struct ip_set_nethash));
5046 +               return -ENOMEM;
5047 +       }
5048 +       get_random_bytes(&map->initval, 4);
5049 +       map->prime = make_prime(req->hashsize);
5050 +       map->hashsize = req->hashsize;
5051 +       map->probes = req->probes;
5052 +       map->resize = req->resize;
5053 +       memset(map->cidr, 0, 30 * sizeof(unsigned char));
5054 +       newbytes = map->hashsize * sizeof(ip_set_ip_t);
5055 +       map->members = ip_set_malloc(newbytes);
5056 +       if (!map->members) {
5057 +               DP("out of memory for %d bytes", newbytes);
5058 +               kfree(map);
5059 +               return -ENOMEM;
5060 +       }
5061 +       memset(map->members, 0, newbytes);
5062 +
5063 +       set->data = map;
5064 +       return 0;
5065 +}
5066 +
5067 +static void destroy(struct ip_set *set)
5068 +{
5069 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5070 +
5071 +       ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
5072 +       kfree(map);
5073 +
5074 +       set->data = NULL;
5075 +}
5076 +
5077 +static void flush(struct ip_set *set)
5078 +{
5079 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5080 +       memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
5081 +       memset(map->cidr, 0, 30 * sizeof(unsigned char));
5082 +}
5083 +
5084 +static void list_header(const struct ip_set *set, void *data)
5085 +{
5086 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5087 +       struct ip_set_req_nethash_create *header =
5088 +           (struct ip_set_req_nethash_create *) data;
5089 +
5090 +       header->hashsize = map->hashsize;
5091 +       header->probes = map->probes;
5092 +       header->resize = map->resize;
5093 +}
5094 +
5095 +static int list_members_size(const struct ip_set *set)
5096 +{
5097 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5098 +
5099 +       return (map->hashsize * sizeof(ip_set_ip_t));
5100 +}
5101 +
5102 +static void list_members(const struct ip_set *set, void *data)
5103 +{
5104 +       struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5105 +       int bytes = map->hashsize * sizeof(ip_set_ip_t);
5106 +
5107 +       memcpy(data, map->members, bytes);
5108 +}
5109 +
5110 +static struct ip_set_type ip_set_nethash = {
5111 +       .typename               = SETTYPE_NAME,
5112 +       .typecode               = IPSET_TYPE_IP,
5113 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
5114 +       .create                 = &create,
5115 +       .destroy                = &destroy,
5116 +       .flush                  = &flush,
5117 +       .reqsize                = sizeof(struct ip_set_req_nethash),
5118 +       .addip                  = &addip,
5119 +       .addip_kernel           = &addip_kernel,
5120 +       .retry                  = &retry,
5121 +       .delip                  = &delip,
5122 +       .delip_kernel           = &delip_kernel,
5123 +       .testip                 = &testip,
5124 +       .testip_kernel          = &testip_kernel,
5125 +       .header_size            = sizeof(struct ip_set_req_nethash_create),
5126 +       .list_header            = &list_header,
5127 +       .list_members_size      = &list_members_size,
5128 +       .list_members           = &list_members,
5129 +       .me                     = THIS_MODULE,
5130 +};
5131 +
5132 +MODULE_LICENSE("GPL");
5133 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5134 +MODULE_DESCRIPTION("nethash type of IP sets");
5135 +
5136 +static int __init init(void)
5137 +{
5138 +       return ip_set_register_set_type(&ip_set_nethash);
5139 +}
5140 +
5141 +static void __exit fini(void)
5142 +{
5143 +       /* FIXME: possible race with ip_set_create() */
5144 +       ip_set_unregister_set_type(&ip_set_nethash);
5145 +}
5146 +
5147 +module_init(init);
5148 +module_exit(fini);
5149 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_portmap.c
5150 ===================================================================
5151 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
5152 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_portmap.c  2007-12-15 05:20:07.900290454 +0100
5153 @@ -0,0 +1,321 @@
5154 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5155 + *
5156 + * This program is free software; you can redistribute it and/or modify
5157 + * it under the terms of the GNU General Public License version 2 as
5158 + * published by the Free Software Foundation.  
5159 + */
5160 +
5161 +/* Kernel module implementing a port set type as a bitmap */
5162 +
5163 +#include <linux/module.h>
5164 +#include <linux/ip.h>
5165 +#include <linux/tcp.h>
5166 +#include <linux/udp.h>
5167 +#include <linux/skbuff.h>
5168 +#include <linux/netfilter_ipv4/ip_tables.h>
5169 +#include <linux/netfilter_ipv4/ip_set.h>
5170 +#include <linux/errno.h>
5171 +#include <asm/uaccess.h>
5172 +#include <asm/bitops.h>
5173 +#include <asm/softirq.h>
5174 +#include <linux/spinlock.h>
5175 +
5176 +#include <net/ip.h>
5177 +
5178 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
5179 +
5180 +static inline ip_set_ip_t
5181 +get_port(const struct sk_buff *skb, u_int32_t flags)
5182 +{
5183 +       struct iphdr *iph = skb->nh.iph;
5184 +       u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
5185 +
5186 +       switch (iph->protocol) {
5187 +       case IPPROTO_TCP: {
5188 +               struct tcphdr *tcph = (struct tcphdr *)((u_int32_t *)iph + iph->ihl);
5189 +               
5190 +               /* See comments at tcp_match in ip_tables.c */
5191 +               if (offset != 0
5192 +                   || (offset == 0
5193 +                       && (skb->len - iph->ihl * 4) < sizeof(struct tcphdr)))
5194 +                       return INVALID_PORT;
5195 +
5196 +               return ntohs(flags & IPSET_SRC ?
5197 +                            tcph->source : tcph->dest);
5198 +           }
5199 +       case IPPROTO_UDP: {
5200 +               struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
5201 +
5202 +               if (offset != 0
5203 +                   || (offset == 0
5204 +                       && (skb->len - iph->ihl * 4) < sizeof(struct udphdr)))
5205 +                       return INVALID_PORT;
5206 +
5207 +               return ntohs(flags & IPSET_SRC ?
5208 +                            udph->source : udph->dest);
5209 +           }
5210 +       default:
5211 +               return INVALID_PORT;
5212 +       }
5213 +}
5214 +
5215 +static inline int
5216 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5217 +{
5218 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5219 +
5220 +       if (port < map->first_port || port > map->last_port)
5221 +               return -ERANGE;
5222 +               
5223 +       *hash_port = port;
5224 +       DP("set: %s, port:%u, %u", set->name, port, *hash_port);
5225 +       return !!test_bit(port - map->first_port, map->members);
5226 +}
5227 +
5228 +static int
5229 +testport(struct ip_set *set, const void *data, size_t size,
5230 +         ip_set_ip_t *hash_port)
5231 +{
5232 +       struct ip_set_req_portmap *req = 
5233 +           (struct ip_set_req_portmap *) data;
5234 +
5235 +       if (size != sizeof(struct ip_set_req_portmap)) {
5236 +               ip_set_printk("data length wrong (want %zu, have %zu)",
5237 +                             sizeof(struct ip_set_req_portmap),
5238 +                             size);
5239 +               return -EINVAL;
5240 +       }
5241 +       return __testport(set, req->port, hash_port);
5242 +}
5243 +
5244 +static int
5245 +testport_kernel(struct ip_set *set, const struct sk_buff *skb,
5246 +               u_int32_t flags, ip_set_ip_t *hash_port)
5247 +{
5248 +       int res;
5249 +       ip_set_ip_t port = get_port(skb, flags);
5250 +
5251 +       DP("flag %s port %u", flags & IPSET_SRC ? "SRC" : "DST", port); 
5252 +       if (port == INVALID_PORT)
5253 +               return 0;       
5254 +
5255 +       res =  __testport(set, port, hash_port);
5256 +       
5257 +       return (res < 0 ? 0 : res);
5258 +}
5259 +
5260 +static inline int
5261 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5262 +{
5263 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5264 +
5265 +       if (port < map->first_port || port > map->last_port)
5266 +               return -ERANGE;
5267 +       if (test_and_set_bit(port - map->first_port, map->members))
5268 +               return -EEXIST;
5269 +               
5270 +       *hash_port = port;
5271 +       DP("port %u", port);
5272 +       return 0;
5273 +}
5274 +
5275 +static int
5276 +addport(struct ip_set *set, const void *data, size_t size,
5277 +        ip_set_ip_t *hash_port)
5278 +{
5279 +       struct ip_set_req_portmap *req = 
5280 +           (struct ip_set_req_portmap *) data;
5281 +
5282 +       if (size != sizeof(struct ip_set_req_portmap)) {
5283 +               ip_set_printk("data length wrong (want %zu, have %zu)",
5284 +                             sizeof(struct ip_set_req_portmap),
5285 +                             size);
5286 +               return -EINVAL;
5287 +       }
5288 +       return __addport(set, req->port, hash_port);
5289 +}
5290 +
5291 +static int
5292 +addport_kernel(struct ip_set *set, const struct sk_buff *skb,
5293 +              u_int32_t flags, ip_set_ip_t *hash_port)
5294 +{
5295 +       ip_set_ip_t port = get_port(skb, flags);
5296 +       
5297 +       if (port == INVALID_PORT)
5298 +               return -EINVAL;
5299 +
5300 +       return __addport(set, port, hash_port);
5301 +}
5302 +
5303 +static inline int
5304 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5305 +{
5306 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5307 +
5308 +       if (port < map->first_port || port > map->last_port)
5309 +               return -ERANGE;
5310 +       if (!test_and_clear_bit(port - map->first_port, map->members))
5311 +               return -EEXIST;
5312 +               
5313 +       *hash_port = port;
5314 +       DP("port %u", port);
5315 +       return 0;
5316 +}
5317 +
5318 +static int
5319 +delport(struct ip_set *set, const void *data, size_t size,
5320 +        ip_set_ip_t *hash_port)
5321 +{
5322 +       struct ip_set_req_portmap *req =
5323 +           (struct ip_set_req_portmap *) data;
5324 +
5325 +       if (size != sizeof(struct ip_set_req_portmap)) {
5326 +               ip_set_printk("data length wrong (want %zu, have %zu)",
5327 +                             sizeof(struct ip_set_req_portmap),
5328 +                             size);
5329 +               return -EINVAL;
5330 +       }
5331 +       return __delport(set, req->port, hash_port);
5332 +}
5333 +
5334 +static int
5335 +delport_kernel(struct ip_set *set, const struct sk_buff *skb,
5336 +              u_int32_t flags, ip_set_ip_t *hash_port)
5337 +{
5338 +       ip_set_ip_t port = get_port(skb, flags);
5339 +       
5340 +       if (port == INVALID_PORT)
5341 +               return -EINVAL;
5342 +
5343 +       return __delport(set, port, hash_port);
5344 +}
5345 +
5346 +static int create(struct ip_set *set, const void *data, size_t size)
5347 +{
5348 +       int newbytes;
5349 +       struct ip_set_req_portmap_create *req =
5350 +           (struct ip_set_req_portmap_create *) data;
5351 +       struct ip_set_portmap *map;
5352 +
5353 +       if (size != sizeof(struct ip_set_req_portmap_create)) {
5354 +               ip_set_printk("data length wrong (want %zu, have %zu)",
5355 +                              sizeof(struct ip_set_req_portmap_create),
5356 +                              size);
5357 +               return -EINVAL;
5358 +       }
5359 +
5360 +       DP("from %u to %u", req->from, req->to);
5361 +
5362 +       if (req->from > req->to) {
5363 +               DP("bad port range");
5364 +               return -ENOEXEC;
5365 +       }
5366 +
5367 +       if (req->to - req->from > MAX_RANGE) {
5368 +               ip_set_printk("range too big (max %d ports)",
5369 +                              MAX_RANGE);
5370 +               return -ENOEXEC;
5371 +       }
5372 +
5373 +       map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
5374 +       if (!map) {
5375 +               DP("out of memory for %d bytes",
5376 +                  sizeof(struct ip_set_portmap));
5377 +               return -ENOMEM;
5378 +       }
5379 +       map->first_port = req->from;
5380 +       map->last_port = req->to;
5381 +       newbytes = bitmap_bytes(req->from, req->to);
5382 +       map->members = kmalloc(newbytes, GFP_KERNEL);
5383 +       if (!map->members) {
5384 +               DP("out of memory for %d bytes", newbytes);
5385 +               kfree(map);
5386 +               return -ENOMEM;
5387 +       }
5388 +       memset(map->members, 0, newbytes);
5389 +
5390 +       set->data = map;
5391 +       return 0;
5392 +}
5393 +
5394 +static void destroy(struct ip_set *set)
5395 +{
5396 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5397 +
5398 +       kfree(map->members);
5399 +       kfree(map);
5400 +
5401 +       set->data = NULL;
5402 +}
5403 +
5404 +static void flush(struct ip_set *set)
5405 +{
5406 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5407 +       memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
5408 +}
5409 +
5410 +static void list_header(const struct ip_set *set, void *data)
5411 +{
5412 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5413 +       struct ip_set_req_portmap_create *header =
5414 +           (struct ip_set_req_portmap_create *) data;
5415 +
5416 +       DP("list_header %u %u", map->first_port, map->last_port);
5417 +
5418 +       header->from = map->first_port;
5419 +       header->to = map->last_port;
5420 +}
5421 +
5422 +static int list_members_size(const struct ip_set *set)
5423 +{
5424 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5425 +
5426 +       return bitmap_bytes(map->first_port, map->last_port);
5427 +}
5428 +
5429 +static void list_members(const struct ip_set *set, void *data)
5430 +{
5431 +       struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5432 +       int bytes = bitmap_bytes(map->first_port, map->last_port);
5433 +
5434 +       memcpy(data, map->members, bytes);
5435 +}
5436 +
5437 +static struct ip_set_type ip_set_portmap = {
5438 +       .typename               = SETTYPE_NAME,
5439 +       .typecode               = IPSET_TYPE_PORT,
5440 +       .protocol_version       = IP_SET_PROTOCOL_VERSION,
5441 +       .create                 = &create,
5442 +       .destroy                = &destroy,
5443 +       .flush                  = &flush,
5444 +       .reqsize                = sizeof(struct ip_set_req_portmap),
5445 +       .addip                  = &addport,
5446 +       .addip_kernel           = &addport_kernel,
5447 +       .delip                  = &delport,
5448 +       .delip_kernel           = &delport_kernel,
5449 +       .testip                 = &testport,
5450 +       .testip_kernel          = &testport_kernel,
5451 +       .header_size            = sizeof(struct ip_set_req_portmap_create),
5452 +       .list_header            = &list_header,
5453 +       .list_members_size      = &list_members_size,
5454 +       .list_members           = &list_members,
5455 +       .me                     = THIS_MODULE,
5456 +};
5457 +
5458 +MODULE_LICENSE("GPL");
5459 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5460 +MODULE_DESCRIPTION("portmap type of IP sets");
5461 +
5462 +static int __init init(void)
5463 +{
5464 +       return ip_set_register_set_type(&ip_set_portmap);
5465 +}
5466 +
5467 +static void __exit fini(void)
5468 +{
5469 +       /* FIXME: possible race with ip_set_create() */
5470 +       ip_set_unregister_set_type(&ip_set_portmap);
5471 +}
5472 +
5473 +module_init(init);
5474 +module_exit(fini);
5475 Index: linux-2.4.35.4/net/ipv4/netfilter/ipt_set.c
5476 ===================================================================
5477 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
5478 +++ linux-2.4.35.4/net/ipv4/netfilter/ipt_set.c 2007-12-15 05:20:07.900290454 +0100
5479 @@ -0,0 +1,114 @@
5480 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5481 + *                         Patrick Schaaf <bof@bof.de>
5482 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
5483 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5484 + *
5485 + * This program is free software; you can redistribute it and/or modify
5486 + * it under the terms of the GNU General Public License version 2 as
5487 + * published by the Free Software Foundation.  
5488 + */
5489 +
5490 +/* Kernel module to match an IP set. */
5491 +
5492 +#include <linux/module.h>
5493 +#include <linux/ip.h>
5494 +#include <linux/skbuff.h>
5495 +
5496 +#include <linux/netfilter_ipv4/ip_tables.h>
5497 +#include <linux/netfilter_ipv4/ip_set.h>
5498 +#include <linux/netfilter_ipv4/ipt_set.h>
5499 +
5500 +static inline int
5501 +match_set(const struct ipt_set_info *info,
5502 +         const struct sk_buff *skb,
5503 +         int inv)
5504 +{      
5505 +       if (ip_set_testip_kernel(info->index, skb, info->flags))
5506 +               inv = !inv;
5507 +       return inv;
5508 +}
5509 +
5510 +static int
5511 +match(const struct sk_buff *skb,
5512 +      const struct net_device *in,
5513 +      const struct net_device *out,
5514 +      const void *matchinfo,
5515 +      int offset,
5516 +      const void *hdr,
5517 +      u_int16_t datalen,
5518 +      int *hotdrop)
5519 +{
5520 +       const struct ipt_set_info_match *info = matchinfo;
5521 +               
5522 +       return match_set(&info->match_set,
5523 +                        skb,
5524 +                        info->match_set.flags[0] & IPSET_MATCH_INV);
5525 +}
5526 +
5527 +static int
5528 +checkentry(const char *tablename,
5529 +          const struct ipt_ip *ip,
5530 +          void *matchinfo,
5531 +          unsigned int matchsize,
5532 +          unsigned int hook_mask)
5533 +{
5534 +       struct ipt_set_info_match *info = 
5535 +               (struct ipt_set_info_match *) matchinfo;
5536 +       ip_set_id_t index;
5537 +
5538 +       if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5539 +               ip_set_printk("invalid matchsize %d", matchsize);
5540 +               return 0;
5541 +       }
5542 +
5543 +       index = ip_set_get_byindex(info->match_set.index);
5544 +               
5545 +       if (index == IP_SET_INVALID_ID) {
5546 +               ip_set_printk("Cannot find set indentified by id %u to match",
5547 +                             info->match_set.index);
5548 +               return 0;       /* error */
5549 +       }
5550 +       if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5551 +               ip_set_printk("That's nasty!");
5552 +               return 0;       /* error */
5553 +       }
5554 +
5555 +       return 1;
5556 +}
5557 +
5558 +static void destroy(void *matchinfo, unsigned int matchsize)
5559 +{
5560 +       struct ipt_set_info_match *info = matchinfo;
5561 +
5562 +       if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5563 +               ip_set_printk("invalid matchsize %d", matchsize);
5564 +               return;
5565 +       }
5566 +
5567 +       ip_set_put(info->match_set.index);
5568 +}
5569 +
5570 +static struct ipt_match set_match = {
5571 +       .name           = "set",
5572 +       .match          = &match,
5573 +       .checkentry     = &checkentry,
5574 +       .destroy        = &destroy,
5575 +       .me             = THIS_MODULE
5576 +};
5577 +
5578 +MODULE_LICENSE("GPL");
5579 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5580 +MODULE_DESCRIPTION("iptables IP set match module");
5581 +
5582 +static int __init init(void)
5583 +{
5584 +       return ipt_register_match(&set_match);
5585 +}
5586 +
5587 +static void __exit fini(void)
5588 +{
5589 +       ipt_unregister_match(&set_match);
5590 +}
5591 +
5592 +module_init(init);
5593 +module_exit(fini);
5594 Index: linux-2.4.35.4/net/ipv4/netfilter/ipt_SET.c
5595 ===================================================================
5596 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
5597 +++ linux-2.4.35.4/net/ipv4/netfilter/ipt_SET.c 2007-12-15 05:20:07.900290454 +0100
5598 @@ -0,0 +1,127 @@
5599 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5600 + *                         Patrick Schaaf <bof@bof.de>
5601 + *                         Martin Josefsson <gandalf@wlug.westbo.se>
5602 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5603 + *
5604 + * This program is free software; you can redistribute it and/or modify
5605 + * it under the terms of the GNU General Public License version 2 as
5606 + * published by the Free Software Foundation.  
5607 + */
5608 +
5609 +/* ipt_SET.c - netfilter target to manipulate IP sets */
5610 +
5611 +#include <linux/types.h>
5612 +#include <linux/ip.h>
5613 +#include <linux/timer.h>
5614 +#include <linux/module.h>
5615 +#include <linux/netfilter.h>
5616 +#include <linux/netdevice.h>
5617 +#include <linux/if.h>
5618 +#include <linux/inetdevice.h>
5619 +#include <net/protocol.h>
5620 +#include <net/checksum.h>
5621 +#include <linux/netfilter_ipv4.h>
5622 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
5623 +#include <linux/netfilter_ipv4/ipt_set.h>
5624 +
5625 +static unsigned int
5626 +target(struct sk_buff **pskb,
5627 +       unsigned int hooknum,
5628 +       const struct net_device *in,
5629 +       const struct net_device *out,
5630 +       const void *targinfo,
5631 +       void *userinfo)
5632 +{
5633 +       const struct ipt_set_info_target *info = targinfo;
5634 +       
5635 +       if (info->add_set.index != IP_SET_INVALID_ID)
5636 +               ip_set_addip_kernel(info->add_set.index,
5637 +                                   *pskb,
5638 +                                   info->add_set.flags);
5639 +       if (info->del_set.index != IP_SET_INVALID_ID)
5640 +               ip_set_delip_kernel(info->del_set.index,
5641 +                                   *pskb,
5642 +                                   info->del_set.flags);
5643 +
5644 +       return IPT_CONTINUE;
5645 +}
5646 +
5647 +static int
5648 +checkentry(const char *tablename,
5649 +          const struct ipt_entry *e,
5650 +          void *targinfo,
5651 +          unsigned int targinfosize, unsigned int hook_mask)
5652 +{
5653 +       struct ipt_set_info_target *info = 
5654 +               (struct ipt_set_info_target *) targinfo;
5655 +       ip_set_id_t index;
5656 +
5657 +       if (targinfosize != IPT_ALIGN(sizeof(*info))) {
5658 +               DP("bad target info size %u", targinfosize);
5659 +               return 0;
5660 +       }
5661 +
5662 +       if (info->add_set.index != IP_SET_INVALID_ID) {
5663 +               index = ip_set_get_byindex(info->add_set.index);
5664 +               if (index == IP_SET_INVALID_ID) {
5665 +                       ip_set_printk("cannot find add_set index %u as target",
5666 +                                     info->add_set.index);
5667 +                       return 0;       /* error */
5668 +               }
5669 +       }
5670 +
5671 +       if (info->del_set.index != IP_SET_INVALID_ID) {
5672 +               index = ip_set_get_byindex(info->del_set.index);
5673 +               if (index == IP_SET_INVALID_ID) {
5674 +                       ip_set_printk("cannot find del_set index %u as target",
5675 +                                     info->del_set.index);
5676 +                       return 0;       /* error */
5677 +               }
5678 +       }
5679 +       if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
5680 +           || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5681 +               ip_set_printk("That's nasty!");
5682 +               return 0;       /* error */
5683 +       }
5684 +       return 1;
5685 +}
5686 +
5687 +static void destroy(void *targetinfo, unsigned int targetsize)
5688 +{
5689 +       struct ipt_set_info_target *info = targetinfo;
5690 +
5691 +       if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
5692 +               ip_set_printk("invalid targetsize %d", targetsize);
5693 +               return;
5694 +       }
5695 +
5696 +       if (info->add_set.index != IP_SET_INVALID_ID)
5697 +               ip_set_put(info->add_set.index);
5698 +       if (info->del_set.index != IP_SET_INVALID_ID)
5699 +               ip_set_put(info->del_set.index);
5700 +}
5701 +
5702 +static struct ipt_target SET_target = {
5703 +       .name           = "SET",
5704 +       .target         = target,
5705 +       .checkentry     = checkentry,
5706 +       .destroy        = destroy,
5707 +       .me             = THIS_MODULE
5708 +};
5709 +
5710 +MODULE_LICENSE("GPL");
5711 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5712 +MODULE_DESCRIPTION("iptables IP set target module");
5713 +
5714 +static int __init init(void)
5715 +{
5716 +       return ipt_register_target(&SET_target);
5717 +}
5718 +
5719 +static void __exit fini(void)
5720 +{
5721 +       ipt_unregister_target(&SET_target);
5722 +}
5723 +
5724 +module_init(init);
5725 +module_exit(fini);
5726 Index: linux-2.4.35.4/net/ipv4/netfilter/Makefile
5727 ===================================================================
5728 --- linux-2.4.35.4.orig/net/ipv4/netfilter/Makefile     2007-12-15 05:20:07.568271536 +0100
5729 +++ linux-2.4.35.4/net/ipv4/netfilter/Makefile  2007-12-15 05:20:07.900290454 +0100
5730 @@ -75,6 +75,18 @@
5731  obj-$(CONFIG_IP_NF_MATCH_HELPER) += ipt_helper.o
5732  obj-$(CONFIG_IP_NF_MATCH_LIMIT) += ipt_limit.o
5733  obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
5734 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
5735 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
5736 +ifdef CONFIG_IP_NF_SET
5737 +       obj-$(CONFIG_IP_NF_SET) += ip_set.o
5738 +       export-objs += ip_set.o
5739 +endif
5740 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
5741 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
5742 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
5743 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
5744 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
5745 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
5746  obj-$(CONFIG_IP_NF_MATCH_MAC) += ipt_mac.o
5747  obj-$(CONFIG_IP_NF_MATCH_IPP2P) += ipt_ipp2p.o
5748