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