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