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