1 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set.h
2 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set.h 2005-09-20 13:11:38.782384000 +0200
8 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
9 + * Patrick Schaaf <bof@bof.de>
10 + * Martin Josefsson <gandalf@wlug.westbo.se>
11 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
13 + * This program is free software; you can redistribute it and/or modify
14 + * it under the terms of the GNU General Public License version 2 as
15 + * published by the Free Software Foundation.
19 + * A sockopt of such quality has hardly ever been seen before on the open
20 + * market! This little beauty, hardly ever used: above 64, so it's
21 + * traditionally used for firewalling, not touched (even once!) by the
22 + * 2.0, 2.2 and 2.4 kernels!
24 + * Comes with its own certificate of authenticity, valid anywhere in the
32 + * Heavily modify by Joakim Axelsson 08.03.2002
33 + * - Made it more modulebased
35 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
37 + * - in order to "deal with" backward compatibility, renamed to ipset
41 + * Used so that the kernel module and ipset-binary can match their versions
43 +#define IP_SET_PROTOCOL_VERSION 2
45 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
47 +/* Lets work with our own typedef for representing an IP address.
48 + * We hope to make the code more portable, possibly to IPv6...
50 + * The representation works in HOST byte order, because most set types
51 + * will perform arithmetic operations and compare operations.
53 + * For now the type is an uint32_t.
55 + * Make sure to ONLY use the functions when translating and parsing
56 + * in order to keep the host byte order and make it more portable:
61 + * (Joakim: where are they???)
64 +typedef uint32_t ip_set_ip_t;
66 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
67 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
69 +typedef uint16_t ip_set_id_t;
71 +#define IP_SET_INVALID_ID 65535
73 +/* How deep we follow bindings */
74 +#define IP_SET_MAX_BINDINGS 6
77 + * Option flags for kernel operations (ipt_set_info)
79 +#define IPSET_SRC 0x01 /* Source match/add */
80 +#define IPSET_DST 0x02 /* Destination match/add */
81 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
84 + * Set types (flavours)
86 +#define IPSET_TYPE_IP 0 /* IP address type of set */
87 +#define IPSET_TYPE_PORT 1 /* Port type of set */
89 +/* Reserved keywords */
90 +#define IPSET_TOKEN_DEFAULT ":default:"
91 +#define IPSET_TOKEN_ALL ":all:"
93 +/* SO_IP_SET operation constants, and their request struct types.
96 + * 0-99: commands with version checking
97 + * 100-199: add/del/test/bind/unbind
98 + * 200-299: list, save, restore
101 +/* Single shot operations:
102 + * version, create, destroy, flush, rename and swap
104 + * Sets are identified by name.
107 +#define IP_SET_REQ_STD \
109 + unsigned version; \
110 + char name[IP_SET_MAXNAMELEN]
112 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
113 +struct ip_set_req_create {
115 + char typename[IP_SET_MAXNAMELEN];
118 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
119 +struct ip_set_req_std {
123 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
124 +/* Uses ip_set_req_std */
126 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
127 +/* Uses ip_set_req_create */
129 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
130 +/* Uses ip_set_req_create */
132 +union ip_set_name_index {
133 + char name[IP_SET_MAXNAMELEN];
137 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
138 +struct ip_set_req_get_set {
141 + union ip_set_name_index set;
144 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
145 +/* Uses ip_set_req_get_set */
147 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
148 +struct ip_set_req_version {
153 +/* Double shots operations:
154 + * add, del, test, bind and unbind.
156 + * First we query the kernel to get the index and type of the target set,
157 + * then issue the command. Validity of IP is checked in kernel in order
158 + * to minimalize sockopt operations.
161 +/* Get minimal set data for add/del/test/bind/unbind IP */
162 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
163 +struct ip_set_req_adt_get {
166 + union ip_set_name_index set;
167 + char typename[IP_SET_MAXNAMELEN];
170 +#define IP_SET_REQ_BYINDEX \
174 +struct ip_set_req_adt {
175 + IP_SET_REQ_BYINDEX;
178 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
179 +/* Uses ip_set_req_adt, with type specific addage */
181 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
182 +/* Uses ip_set_req_adt, with type specific addage */
184 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
185 +/* Uses ip_set_req_adt, with type specific addage */
187 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
188 +/* Uses ip_set_req_bind, with type specific addage */
189 +struct ip_set_req_bind {
190 + IP_SET_REQ_BYINDEX;
191 + char binding[IP_SET_MAXNAMELEN];
194 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
195 +/* Uses ip_set_req_bind, with type speficic addage
196 + * index = 0 means unbinding for all sets */
198 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
199 +/* Uses ip_set_req_bind, with type specific addage */
201 +/* Multiple shots operations: list, save, restore.
203 + * - check kernel version and query the max number of sets
204 + * - get the basic information on all sets
205 + * and size required for the next step
206 + * - get actual set data: header, data, bindings
209 +/* Get max_sets and the index of a queried set
211 +#define IP_SET_OP_MAX_SETS 0x00000020
212 +struct ip_set_req_max_sets {
215 + ip_set_id_t max_sets; /* max_sets */
216 + ip_set_id_t sets; /* real number of sets */
217 + union ip_set_name_index set; /* index of set if name used */
220 +/* Get the id and name of the sets plus size for next step */
221 +#define IP_SET_OP_LIST_SIZE 0x00000201
222 +#define IP_SET_OP_SAVE_SIZE 0x00000202
223 +struct ip_set_req_setnames {
225 + ip_set_id_t index; /* set to list/save */
226 + size_t size; /* size to get setdata/bindings */
227 + /* followed by sets number of struct ip_set_name_list */
230 +struct ip_set_name_list {
231 + char name[IP_SET_MAXNAMELEN];
232 + char typename[IP_SET_MAXNAMELEN];
237 +/* The actual list operation */
238 +#define IP_SET_OP_LIST 0x00000203
239 +struct ip_set_req_list {
240 + IP_SET_REQ_BYINDEX;
241 + /* sets number of struct ip_set_list in reply */
244 +struct ip_set_list {
246 + ip_set_id_t binding;
248 + size_t header_size; /* Set header data of header_size */
249 + size_t members_size; /* Set members data of members_size */
250 + size_t bindings_size; /* Set bindings data of bindings_size */
253 +struct ip_set_hash_list {
255 + ip_set_id_t binding;
258 +/* The save operation */
259 +#define IP_SET_OP_SAVE 0x00000204
260 +/* Uses ip_set_req_list, in the reply replaced by
261 + * sets number of struct ip_set_save plus a marker
262 + * ip_set_save followed by ip_set_hash_save structures.
264 +struct ip_set_save {
266 + ip_set_id_t binding;
267 + size_t header_size; /* Set header data of header_size */
268 + size_t members_size; /* Set members data of members_size */
271 +/* At restoring, ip == 0 means default binding for the given set: */
272 +struct ip_set_hash_save {
275 + ip_set_id_t binding;
278 +/* The restore operation */
279 +#define IP_SET_OP_RESTORE 0x00000205
280 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
281 + * plus a marker ip_set_restore, followed by ip_set_hash_save
284 +struct ip_set_restore {
285 + char name[IP_SET_MAXNAMELEN];
286 + char typename[IP_SET_MAXNAMELEN];
288 + size_t header_size; /* Create data of header_size */
289 + size_t members_size; /* Set members data of members_size */
292 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
294 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
299 +#define ip_set_printk(format, args...) \
301 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
302 + printk(format "\n" , ## args); \
305 +#if defined(IP_SET_DEBUG)
306 +#define DP(format, args...) \
308 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
309 + printk(format "\n" , ## args); \
311 +#define IP_SET_ASSERT(x) \
314 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
315 + __FILE__, __LINE__, __FUNCTION__); \
318 +#define DP(format, args...)
319 +#define IP_SET_ASSERT(x)
325 + * The ip_set_type definition - one per set type, e.g. "ipmap".
327 + * Each individual set has a pointer, set->type, going to one
328 + * of these structures. Function pointers inside the structure implement
329 + * the real behaviour of the sets.
331 + * If not mentioned differently, the implementation behind the function
332 + * pointers of a set_type, is expected to return 0 if ok, and a negative
333 + * errno (e.g. -EINVAL) on error.
335 +struct ip_set_type {
336 + struct list_head list; /* next in list of set types */
338 + /* test for IP in set (kernel: iptables -m set src|dst)
339 + * return 0 if not in set, 1 if in set.
341 + int (*testip_kernel) (struct ip_set *set,
342 + const struct sk_buff * skb,
346 + /* test for IP in set (userspace: ipset -T set IP)
347 + * return 0 if not in set, 1 if in set.
349 + int (*testip) (struct ip_set *set,
350 + const void *data, size_t size,
354 + * Size of the data structure passed by when
355 + * adding/deletin/testing an entry.
359 + /* Add IP into set (userspace: ipset -A set IP)
360 + * Return -EEXIST if the address is already in the set,
361 + * and -ERANGE if the address lies outside the set bounds.
362 + * If the address was not already in the set, 0 is returned.
364 + int (*addip) (struct ip_set *set,
365 + const void *data, size_t size,
368 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
369 + * Return -EEXIST if the address is already in the set,
370 + * and -ERANGE if the address lies outside the set bounds.
371 + * If the address was not already in the set, 0 is returned.
373 + int (*addip_kernel) (struct ip_set *set,
374 + const struct sk_buff * skb,
378 + /* remove IP from set (userspace: ipset -D set --entry x)
379 + * Return -EEXIST if the address is NOT in the set,
380 + * and -ERANGE if the address lies outside the set bounds.
381 + * If the address really was in the set, 0 is returned.
383 + int (*delip) (struct ip_set *set,
384 + const void *data, size_t size,
387 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
388 + * Return -EEXIST if the address is NOT in the set,
389 + * and -ERANGE if the address lies outside the set bounds.
390 + * If the address really was in the set, 0 is returned.
392 + int (*delip_kernel) (struct ip_set *set,
393 + const struct sk_buff * skb,
397 + /* new set creation - allocated type specific items
399 + int (*create) (struct ip_set *set,
400 + const void *data, size_t size);
402 + /* retry the operation after successfully tweaking the set
404 + int (*retry) (struct ip_set *set);
406 + /* set destruction - free type specific items
407 + * There is no return value.
408 + * Can be called only when child sets are destroyed.
410 + void (*destroy) (struct ip_set *set);
412 + /* set flushing - reset all bits in the set, or something similar.
413 + * There is no return value.
415 + void (*flush) (struct ip_set *set);
417 + /* Listing: size needed for header
419 + size_t header_size;
421 + /* Listing: Get the header
423 + * Fill in the information in "data".
424 + * This function is always run after list_header_size() under a
425 + * writelock on the set. Therefor is the length of "data" always
428 + void (*list_header) (const struct ip_set *set,
431 + /* Listing: Get the size for the set members
433 + int (*list_members_size) (const struct ip_set *set);
435 + /* Listing: Get the set members
437 + * Fill in the information in "data".
438 + * This function is always run after list_member_size() under a
439 + * writelock on the set. Therefor is the length of "data" always
442 + void (*list_members) (const struct ip_set *set,
445 + char typename[IP_SET_MAXNAMELEN];
447 + int protocol_version;
449 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
453 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
454 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
456 +/* A generic ipset */
458 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
459 + rwlock_t lock; /* lock for concurrency control */
460 + ip_set_id_t id; /* set id for swapping */
461 + ip_set_id_t binding; /* default binding for the set */
462 + atomic_t ref; /* in kernel and in hash references */
463 + struct ip_set_type *type; /* the set types */
464 + void *data; /* pooltype specific data */
467 +/* Structure to bind set elements to sets */
468 +struct ip_set_hash {
469 + struct list_head list; /* list of clashing entries in hash */
470 + ip_set_ip_t ip; /* ip from set */
471 + ip_set_id_t id; /* set id */
472 + ip_set_id_t binding; /* set we bind the element to */
475 +/* register and unregister set references */
476 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
477 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
478 +extern void ip_set_put(ip_set_id_t id);
480 +/* API for iptables set match, and SET target */
481 +extern void ip_set_addip_kernel(ip_set_id_t id,
482 + const struct sk_buff *skb,
483 + const u_int32_t *flags);
484 +extern void ip_set_delip_kernel(ip_set_id_t id,
485 + const struct sk_buff *skb,
486 + const u_int32_t *flags);
487 +extern int ip_set_testip_kernel(ip_set_id_t id,
488 + const struct sk_buff *skb,
489 + const u_int32_t *flags);
491 +#endif /* __KERNEL__ */
493 +#endif /*_IP_SET_H*/
494 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_iphash.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_iphash.h
495 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_iphash.h 1970-01-01 01:00:00.000000000 +0100
496 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_iphash.h 2005-09-20 13:11:38.782384000 +0200
498 +#ifndef __IP_SET_IPHASH_H
499 +#define __IP_SET_IPHASH_H
501 +#include <linux/netfilter_ipv4/ip_set.h>
503 +#define SETTYPE_NAME "iphash"
504 +#define MAX_RANGE 0x0000FFFF
506 +struct ip_set_iphash {
507 + ip_set_ip_t *members; /* the iphash proper */
508 + uint32_t initval; /* initval for jhash_1word */
509 + uint32_t prime; /* prime for double hashing */
510 + uint32_t hashsize; /* hash size */
511 + uint16_t probes; /* max number of probes */
512 + uint16_t resize; /* resize factor in percent */
513 + ip_set_ip_t netmask; /* netmask */
516 +struct ip_set_req_iphash_create {
520 + ip_set_ip_t netmask;
523 +struct ip_set_req_iphash {
527 +#endif /* __IP_SET_IPHASH_H */
528 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_ipmap.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_ipmap.h
529 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_ipmap.h 1970-01-01 01:00:00.000000000 +0100
530 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_ipmap.h 2005-09-20 13:11:38.782384000 +0200
532 +#ifndef __IP_SET_IPMAP_H
533 +#define __IP_SET_IPMAP_H
535 +#include <linux/netfilter_ipv4/ip_set.h>
537 +#define SETTYPE_NAME "ipmap"
538 +#define MAX_RANGE 0x0000FFFF
540 +struct ip_set_ipmap {
541 + void *members; /* the ipmap proper */
542 + ip_set_ip_t first_ip; /* host byte order, included in range */
543 + ip_set_ip_t last_ip; /* host byte order, included in range */
544 + ip_set_ip_t netmask; /* subnet netmask */
545 + ip_set_ip_t sizeid; /* size of set in IPs */
546 + u_int16_t hosts; /* number of hosts in a subnet */
549 +struct ip_set_req_ipmap_create {
552 + ip_set_ip_t netmask;
555 +struct ip_set_req_ipmap {
560 +mask_to_bits(ip_set_ip_t mask)
562 + unsigned int bits = 32;
563 + ip_set_ip_t maskaddr;
565 + if (mask == 0xFFFFFFFF)
568 + maskaddr = 0xFFFFFFFE;
569 + while (--bits >= 0 && maskaddr != mask)
576 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
578 + ip_set_ip_t mask = 0xFFFFFFFE;
581 + while (--(*bits) >= 0 && mask && (to & mask) != from)
587 +#endif /* __IP_SET_IPMAP_H */
588 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_iptree.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_iptree.h
589 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_iptree.h 1970-01-01 01:00:00.000000000 +0100
590 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_iptree.h 2005-09-20 13:11:38.782384000 +0200
592 +#ifndef __IP_SET_IPTREE_H
593 +#define __IP_SET_IPTREE_H
595 +#include <linux/netfilter_ipv4/ip_set.h>
597 +#define SETTYPE_NAME "iptree"
598 +#define MAX_RANGE 0x0000FFFF
600 +struct ip_set_iptreed {
601 + unsigned long expires[255]; /* x.x.x.ADDR */
604 +struct ip_set_iptreec {
605 + struct ip_set_iptreed *tree[255]; /* x.x.ADDR.* */
608 +struct ip_set_iptreeb {
609 + struct ip_set_iptreec *tree[255]; /* x.ADDR.*.* */
612 +struct ip_set_iptree {
613 + unsigned int timeout;
614 + unsigned int gc_interval;
616 + struct timer_list gc;
617 + struct ip_set_iptreeb *tree[255]; /* ADDR.*.*.* */
621 +struct ip_set_req_iptree_create {
622 + unsigned int timeout;
625 +struct ip_set_req_iptree {
627 + unsigned int timeout;
630 +#endif /* __IP_SET_IPTREE_H */
631 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_jhash.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_jhash.h
632 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_jhash.h 1970-01-01 01:00:00.000000000 +0100
633 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_jhash.h 2005-09-20 13:11:38.782384000 +0200
635 +#ifndef _LINUX_IPSET_JHASH_H
636 +#define _LINUX_IPSET_JHASH_H
638 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
639 + * to __u32/__u8 so that the header file can be included into
640 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
643 +/* jhash.h: Jenkins hash support.
645 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
647 + * http://burtleburtle.net/bob/hash/
649 + * These are the credits from Bob's sources:
651 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
652 + * hash(), hash2(), hash3, and mix() are externally useful functions.
653 + * Routines to test the hash are included if SELF_TEST is defined.
654 + * You can use this free for any purpose. It has no warranty.
656 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
658 + * I've modified Bob's hash to be useful in the Linux kernel, and
659 + * any bugs present are surely my fault. -DaveM
662 +/* NOTE: Arguments are modified. */
663 +#define __jhash_mix(a, b, c) \
665 + a -= b; a -= c; a ^= (c>>13); \
666 + b -= c; b -= a; b ^= (a<<8); \
667 + c -= a; c -= b; c ^= (b>>13); \
668 + a -= b; a -= c; a ^= (c>>12); \
669 + b -= c; b -= a; b ^= (a<<16); \
670 + c -= a; c -= b; c ^= (b>>5); \
671 + a -= b; a -= c; a ^= (c>>3); \
672 + b -= c; b -= a; b ^= (a<<10); \
673 + c -= a; c -= b; c ^= (b>>15); \
676 +/* The golden ration: an arbitrary value */
677 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
679 +/* The most generic version, hashes an arbitrary sequence
680 + * of bytes. No alignment or length assumptions are made about
683 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
685 + __u32 a, b, c, len;
689 + a = b = JHASH_GOLDEN_RATIO;
692 + while (len >= 12) {
693 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
694 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
695 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
697 + __jhash_mix(a,b,c);
705 + case 11: c += ((__u32)k[10]<<24);
706 + case 10: c += ((__u32)k[9]<<16);
707 + case 9 : c += ((__u32)k[8]<<8);
708 + case 8 : b += ((__u32)k[7]<<24);
709 + case 7 : b += ((__u32)k[6]<<16);
710 + case 6 : b += ((__u32)k[5]<<8);
711 + case 5 : b += k[4];
712 + case 4 : a += ((__u32)k[3]<<24);
713 + case 3 : a += ((__u32)k[2]<<16);
714 + case 2 : a += ((__u32)k[1]<<8);
715 + case 1 : a += k[0];
718 + __jhash_mix(a,b,c);
723 +/* A special optimized version that handles 1 or more of __u32s.
724 + * The length parameter here is the number of __u32s in the key.
726 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
728 + __u32 a, b, c, len;
730 + a = b = JHASH_GOLDEN_RATIO;
738 + __jhash_mix(a, b, c);
745 + case 2 : b += k[1];
746 + case 1 : a += k[0];
749 + __jhash_mix(a,b,c);
755 +/* A special ultra-optimized versions that knows they are hashing exactly
756 + * 3, 2 or 1 word(s).
758 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
759 + * done at the end is not done here.
761 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
763 + a += JHASH_GOLDEN_RATIO;
764 + b += JHASH_GOLDEN_RATIO;
767 + __jhash_mix(a, b, c);
772 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
774 + return jhash_3words(a, b, 0, initval);
777 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
779 + return jhash_3words(a, 0, 0, initval);
782 +#endif /* _LINUX_IPSET_JHASH_H */
783 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_macipmap.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_macipmap.h
784 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_macipmap.h 1970-01-01 01:00:00.000000000 +0100
785 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_macipmap.h 2005-09-20 13:11:38.782384000 +0200
787 +#ifndef __IP_SET_MACIPMAP_H
788 +#define __IP_SET_MACIPMAP_H
790 +#include <linux/netfilter_ipv4/ip_set.h>
792 +#define SETTYPE_NAME "macipmap"
793 +#define MAX_RANGE 0x0000FFFF
796 +#define IPSET_MACIP_MATCHUNSET 1
799 +#define IPSET_MACIP_ISSET 1
801 +struct ip_set_macipmap {
802 + void *members; /* the macipmap proper */
803 + ip_set_ip_t first_ip; /* host byte order, included in range */
804 + ip_set_ip_t last_ip; /* host byte order, included in range */
808 +struct ip_set_req_macipmap_create {
814 +struct ip_set_req_macipmap {
816 + unsigned char ethernet[ETH_ALEN];
819 +struct ip_set_macip {
820 + unsigned short flags;
821 + unsigned char ethernet[ETH_ALEN];
824 +#endif /* __IP_SET_MACIPMAP_H */
825 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_malloc.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_malloc.h
826 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_malloc.h 1970-01-01 01:00:00.000000000 +0100
827 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_malloc.h 2005-09-20 13:11:38.782384000 +0200
829 +#ifndef _IP_SET_MALLOC_H
830 +#define _IP_SET_MALLOC_H
834 +/* Memory allocation and deallocation */
835 +static size_t max_malloc_size = 0;
837 +static inline void init_max_malloc_size(void)
839 +#define CACHE(x) max_malloc_size = x;
840 +#include <linux/kmalloc_sizes.h>
844 +static inline void * ip_set_malloc_atomic(size_t bytes)
846 + if (bytes > max_malloc_size)
847 + return __vmalloc(bytes, GFP_ATOMIC, PAGE_KERNEL);
849 + return kmalloc(bytes, GFP_ATOMIC);
852 +static inline void * ip_set_malloc(size_t bytes)
854 + if (bytes > max_malloc_size)
855 + return vmalloc(bytes);
857 + return kmalloc(bytes, GFP_KERNEL);
860 +static inline void ip_set_free(void * data, size_t bytes)
862 + if (bytes > max_malloc_size)
868 +#endif /* __KERNEL__ */
870 +#endif /*_IP_SET_MALLOC_H*/
871 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_nethash.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_nethash.h
872 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_nethash.h 1970-01-01 01:00:00.000000000 +0100
873 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_nethash.h 2005-09-20 13:11:38.782384000 +0200
875 +#ifndef __IP_SET_NETHASH_H
876 +#define __IP_SET_NETHASH_H
878 +#include <linux/netfilter_ipv4/ip_set.h>
880 +#define SETTYPE_NAME "nethash"
881 +#define MAX_RANGE 0x0000FFFF
883 +struct ip_set_nethash {
884 + ip_set_ip_t *members; /* the nethash proper */
885 + uint32_t initval; /* initval for jhash_1word */
886 + uint32_t prime; /* prime for double hashing */
887 + uint32_t hashsize; /* hash size */
888 + uint16_t probes; /* max number of probes */
889 + uint16_t resize; /* resize factor in percent */
890 + unsigned char cidr[30]; /* CIDR sizes */
893 +struct ip_set_req_nethash_create {
899 +struct ip_set_req_nethash {
901 + unsigned char cidr;
904 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
906 +static inline ip_set_ip_t
907 +pack(ip_set_ip_t ip, unsigned char cidr)
909 + ip_set_ip_t addr, *paddr = &addr;
910 + unsigned char n, t, *a;
912 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
914 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
918 + a = &((unsigned char *)paddr)[n];
919 + *a = *a /(1 << (8 - t)) + shifts[t];
921 + DP("n: %u, t: %u, a: %u", n, t, *a);
922 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
923 + HIPQUAD(ip), cidr, NIPQUAD(addr));
926 + return ntohl(addr);
929 +#endif /* __IP_SET_NETHASH_H */
930 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_portmap.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_portmap.h
931 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_portmap.h 1970-01-01 01:00:00.000000000 +0100
932 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_portmap.h 2005-09-20 13:11:38.782384000 +0200
934 +#ifndef __IP_SET_PORTMAP_H
935 +#define __IP_SET_PORTMAP_H
937 +#include <linux/netfilter_ipv4/ip_set.h>
939 +#define SETTYPE_NAME "portmap"
940 +#define MAX_RANGE 0x0000FFFF
941 +#define INVALID_PORT (MAX_RANGE + 1)
943 +struct ip_set_portmap {
944 + void *members; /* the portmap proper */
945 + ip_set_ip_t first_port; /* host byte order, included in range */
946 + ip_set_ip_t last_port; /* host byte order, included in range */
949 +struct ip_set_req_portmap_create {
954 +struct ip_set_req_portmap {
958 +#endif /* __IP_SET_PORTMAP_H */
959 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_prime.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_prime.h
960 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ip_set_prime.h 1970-01-01 01:00:00.000000000 +0100
961 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ip_set_prime.h 2005-09-20 13:11:38.782384000 +0200
963 +#ifndef __IP_SET_PRIME_H
964 +#define __IP_SET_PRIME_H
966 +static inline unsigned make_prime_bound(unsigned nr)
968 + unsigned long long nr64 = nr;
969 + unsigned long long x = 1;
971 + while (x <= nr64) { x <<= 2; nr <<= 1; }
975 +static inline int make_prime_check(unsigned nr)
978 + unsigned b = make_prime_bound(nr);
980 + if (0 == (nr % x)) return 0;
986 +static unsigned make_prime(unsigned nr)
988 + if (0 == (nr & 1)) nr--;
990 + if (make_prime_check(nr)) return nr;
996 +#endif /* __IP_SET_PRIME_H */
997 diff -Nur linux-2.6.12.5/include/linux/netfilter_ipv4/ipt_set.h linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ipt_set.h
998 --- linux-2.6.12.5/include/linux/netfilter_ipv4/ipt_set.h 1970-01-01 01:00:00.000000000 +0100
999 +++ linux-2.6.12.5-ipset/include/linux/netfilter_ipv4/ipt_set.h 2005-09-20 13:11:38.786384250 +0200
1004 +#include <linux/netfilter_ipv4/ip_set.h>
1006 +struct ipt_set_info {
1007 + ip_set_id_t index;
1008 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1012 +struct ipt_set_info_match {
1013 + struct ipt_set_info match_set;
1016 +struct ipt_set_info_target {
1017 + struct ipt_set_info add_set;
1018 + struct ipt_set_info del_set;
1021 +#endif /*_IPT_SET_H*/
1022 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/ip_set.c linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set.c
1023 --- linux-2.6.12.5/net/ipv4/netfilter/ip_set.c 1970-01-01 01:00:00.000000000 +0100
1024 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set.c 2005-09-20 13:11:38.786384250 +0200
1026 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1027 + * Patrick Schaaf <bof@bof.de>
1028 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1030 + * This program is free software; you can redistribute it and/or modify
1031 + * it under the terms of the GNU General Public License version 2 as
1032 + * published by the Free Software Foundation.
1035 +/* Kernel module for IP set management */
1037 +#include <linux/config.h>
1038 +#include <linux/module.h>
1039 +#include <linux/moduleparam.h>
1040 +#include <linux/kmod.h>
1041 +#include <linux/ip.h>
1042 +#include <linux/skbuff.h>
1043 +#include <linux/random.h>
1044 +#include <linux/jhash.h>
1045 +#include <linux/netfilter_ipv4/ip_tables.h>
1046 +#include <linux/errno.h>
1047 +#include <asm/uaccess.h>
1048 +#include <asm/bitops.h>
1049 +#include <asm/semaphore.h>
1050 +#include <linux/spinlock.h>
1051 +#include <linux/vmalloc.h>
1053 +#define ASSERT_READ_LOCK(x) /* dont use that */
1054 +#define ASSERT_WRITE_LOCK(x)
1055 +#include <linux/netfilter_ipv4/listhelp.h>
1056 +#include <linux/netfilter_ipv4/ip_set.h>
1058 +static struct list_head set_type_list; /* all registered sets */
1059 +static struct ip_set **ip_set_list; /* all individual sets */
1060 +static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1061 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1062 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1063 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1064 +static struct list_head *ip_set_hash; /* hash of bindings */
1065 +static unsigned int ip_set_hash_random; /* random seed */
1068 + * Sets are identified either by the index in ip_set_list or by id.
1069 + * The id never changes and is used to find a key in the hash.
1070 + * The index may change by swapping and used at all other places
1071 + * (set/SET netfilter modules, binding value, etc.)
1073 + * Userspace requests are serialized by ip_set_mutex and sets can
1074 + * be deleted only from userspace. Therefore ip_set_list locking
1075 + * must obey the following rules:
1077 + * - kernel requests: read and write locking mandatory
1078 + * - user requests: read locking optional, write locking mandatory
1082 +__ip_set_get(ip_set_id_t index)
1084 + atomic_inc(&ip_set_list[index]->ref);
1088 +__ip_set_put(ip_set_id_t index)
1090 + atomic_dec(&ip_set_list[index]->ref);
1094 + * Binding routines
1098 +ip_hash_cmp(const struct ip_set_hash *set_hash,
1099 + ip_set_id_t id, ip_set_ip_t ip)
1101 + return set_hash->id == id && set_hash->ip == ip;
1105 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1107 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1108 + % ip_set_bindings_hash_size;
1109 + struct ip_set_hash *set_hash;
1111 + ASSERT_READ_LOCK(&ip_set_lock);
1112 + IP_SET_ASSERT(ip_set_list[id]);
1113 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1115 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1116 + struct ip_set_hash *, id, ip);
1118 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1120 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1122 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1126 +__set_hash_del(struct ip_set_hash *set_hash)
1128 + ASSERT_WRITE_LOCK(&ip_set_lock);
1129 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1131 + __ip_set_put(set_hash->binding);
1132 + list_del(&set_hash->list);
1137 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1139 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1140 + % ip_set_bindings_hash_size;
1141 + struct ip_set_hash *set_hash;
1143 + IP_SET_ASSERT(ip_set_list[id]);
1144 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1145 + write_lock_bh(&ip_set_lock);
1146 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1147 + struct ip_set_hash *, id, ip);
1148 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1150 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1152 + if (set_hash != NULL)
1153 + __set_hash_del(set_hash);
1154 + write_unlock_bh(&ip_set_lock);
1159 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1161 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1162 + % ip_set_bindings_hash_size;
1163 + struct ip_set_hash *set_hash;
1166 + IP_SET_ASSERT(ip_set_list[id]);
1167 + IP_SET_ASSERT(ip_set_list[binding]);
1168 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1169 + HIPQUAD(ip), ip_set_list[binding]->name);
1170 + write_lock_bh(&ip_set_lock);
1171 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1172 + struct ip_set_hash *, id, ip);
1174 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_KERNEL);
1179 + INIT_LIST_HEAD(&set_hash->list);
1180 + set_hash->id = id;
1181 + set_hash->ip = ip;
1182 + list_add(&ip_set_hash[key], &set_hash->list);
1184 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1185 + DP("overwrite binding: %s",
1186 + ip_set_list[set_hash->binding]->name);
1187 + __ip_set_put(set_hash->binding);
1189 + set_hash->binding = binding;
1190 + __ip_set_get(set_hash->binding);
1192 + write_unlock_bh(&ip_set_lock);
1196 +#define FOREACH_HASH_DO(fn, args...) \
1198 + ip_set_id_t __key; \
1199 + struct ip_set_hash *__set_hash; \
1201 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1202 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1203 + fn(__set_hash , ## args); \
1207 +#define FOREACH_HASH_RW_DO(fn, args...) \
1209 + ip_set_id_t __key; \
1210 + struct ip_set_hash *__set_hash, *__n; \
1212 + ASSERT_WRITE_LOCK(&ip_set_lock); \
1213 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1214 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1215 + fn(__set_hash , ## args); \
1219 +/* Add, del and test set entries from kernel */
1221 +#define follow_bindings(index, set, ip) \
1222 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1223 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1226 +ip_set_testip_kernel(ip_set_id_t index,
1227 + const struct sk_buff *skb,
1228 + const u_int32_t *flags)
1230 + struct ip_set *set;
1234 + IP_SET_ASSERT(flags[i]);
1235 + read_lock_bh(&ip_set_lock);
1237 + set = ip_set_list[index];
1238 + IP_SET_ASSERT(set);
1239 + DP("set %s, index %u", set->name, index);
1240 + read_lock_bh(&set->lock);
1241 + res = set->type->testip_kernel(set, skb, flags[i], &ip);
1242 + read_unlock_bh(&set->lock);
1245 + && follow_bindings(index, set, ip));
1246 + read_unlock_bh(&ip_set_lock);
1252 +ip_set_addip_kernel(ip_set_id_t index,
1253 + const struct sk_buff *skb,
1254 + const u_int32_t *flags)
1256 + struct ip_set *set;
1260 + IP_SET_ASSERT(flags[i]);
1262 + read_lock_bh(&ip_set_lock);
1264 + set = ip_set_list[index];
1265 + IP_SET_ASSERT(set);
1266 + DP("set %s, index %u", set->name, index);
1267 + write_lock_bh(&set->lock);
1268 + res = set->type->addip_kernel(set, skb, flags[i], &ip);
1269 + write_unlock_bh(&set->lock);
1270 + } while ((res == 0 || res == -EEXIST)
1272 + && follow_bindings(index, set, ip));
1273 + read_unlock_bh(&ip_set_lock);
1275 + if (res == -EAGAIN
1276 + && set->type->retry
1277 + && (res = set->type->retry(set)) == 0)
1282 +ip_set_delip_kernel(ip_set_id_t index,
1283 + const struct sk_buff *skb,
1284 + const u_int32_t *flags)
1286 + struct ip_set *set;
1290 + IP_SET_ASSERT(flags[i]);
1291 + read_lock_bh(&ip_set_lock);
1293 + set = ip_set_list[index];
1294 + IP_SET_ASSERT(set);
1295 + DP("set %s, index %u", set->name, index);
1296 + write_lock_bh(&set->lock);
1297 + res = set->type->delip_kernel(set, skb, flags[i], &ip);
1298 + write_unlock_bh(&set->lock);
1299 + } while ((res == 0 || res == -EEXIST)
1301 + && follow_bindings(index, set, ip));
1302 + read_unlock_bh(&ip_set_lock);
1305 +/* Register and deregister settype */
1308 +set_type_equal(const struct ip_set_type *set_type, const char *str2)
1310 + return !strncmp(set_type->typename, str2, IP_SET_MAXNAMELEN - 1);
1313 +static inline struct ip_set_type *
1314 +find_set_type(const char *name)
1316 + return LIST_FIND(&set_type_list,
1318 + struct ip_set_type *,
1323 +ip_set_register_set_type(struct ip_set_type *set_type)
1327 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1328 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1329 + set_type->typename,
1330 + set_type->protocol_version,
1331 + IP_SET_PROTOCOL_VERSION);
1335 + write_lock_bh(&ip_set_lock);
1336 + if (find_set_type(set_type->typename)) {
1338 + ip_set_printk("'%s' already registered!",
1339 + set_type->typename);
1343 + if (!try_module_get(THIS_MODULE)) {
1347 + list_append(&set_type_list, set_type);
1348 + DP("'%s' registered.", set_type->typename);
1350 + write_unlock_bh(&ip_set_lock);
1355 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1357 + write_lock_bh(&ip_set_lock);
1358 + if (!find_set_type(set_type->typename)) {
1359 + ip_set_printk("'%s' not registered?",
1360 + set_type->typename);
1363 + LIST_DELETE(&set_type_list, set_type);
1364 + module_put(THIS_MODULE);
1365 + DP("'%s' unregistered.", set_type->typename);
1367 + write_unlock_bh(&ip_set_lock);
1372 + * Userspace routines
1376 + * Find set by name, reference it once. The reference makes sure the
1377 + * thing pointed to, does not go away under our feet. Drop the reference
1378 + * later, using ip_set_put().
1381 +ip_set_get_byname(const char *name)
1383 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1385 + down(&ip_set_app_mutex);
1386 + for (i = 0; i < ip_set_max; i++) {
1387 + if (ip_set_list[i] != NULL
1388 + && strcmp(ip_set_list[i]->name, name) == 0) {
1394 + up(&ip_set_app_mutex);
1399 + * Find set by index, reference it once. The reference makes sure the
1400 + * thing pointed to, does not go away under our feet. Drop the reference
1401 + * later, using ip_set_put().
1404 +ip_set_get_byindex(ip_set_id_t index)
1406 + down(&ip_set_app_mutex);
1408 + if (index >= ip_set_max)
1409 + return IP_SET_INVALID_ID;
1411 + if (ip_set_list[index])
1412 + __ip_set_get(index);
1414 + index = IP_SET_INVALID_ID;
1416 + up(&ip_set_app_mutex);
1421 + * If the given set pointer points to a valid set, decrement
1422 + * reference count by 1. The caller shall not assume the index
1423 + * to be valid, after calling this function.
1425 +void ip_set_put(ip_set_id_t index)
1427 + down(&ip_set_app_mutex);
1428 + if (ip_set_list[index])
1429 + __ip_set_put(index);
1430 + up(&ip_set_app_mutex);
1433 +/* Find a set by name or index */
1435 +ip_set_find_byname(const char *name)
1437 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1439 + for (i = 0; i < ip_set_max; i++) {
1440 + if (ip_set_list[i] != NULL
1441 + && strcmp(ip_set_list[i]->name, name) == 0) {
1450 +ip_set_find_byindex(ip_set_id_t index)
1452 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1453 + index = IP_SET_INVALID_ID;
1459 + * Add, del, test, bind and unbind
1463 +__ip_set_testip(struct ip_set *set,
1470 + read_lock_bh(&set->lock);
1471 + res = set->type->testip(set, data, size, ip);
1472 + read_unlock_bh(&set->lock);
1478 +__ip_set_addip(ip_set_id_t index,
1482 + struct ip_set *set = ip_set_list[index];
1486 + IP_SET_ASSERT(set);
1488 + write_lock_bh(&set->lock);
1489 + res = set->type->addip(set, data, size, &ip);
1490 + write_unlock_bh(&set->lock);
1491 + } while (res == -EAGAIN
1492 + && set->type->retry
1493 + && (res = set->type->retry(set)) == 0);
1499 +ip_set_addip(ip_set_id_t index,
1504 + return __ip_set_addip(index,
1505 + data + sizeof(struct ip_set_req_adt),
1506 + size - sizeof(struct ip_set_req_adt));
1510 +ip_set_delip(ip_set_id_t index,
1514 + struct ip_set *set = ip_set_list[index];
1518 + IP_SET_ASSERT(set);
1519 + write_lock_bh(&set->lock);
1520 + res = set->type->delip(set,
1521 + data + sizeof(struct ip_set_req_adt),
1522 + size - sizeof(struct ip_set_req_adt),
1524 + write_unlock_bh(&set->lock);
1530 +ip_set_testip(ip_set_id_t index,
1534 + struct ip_set *set = ip_set_list[index];
1538 + IP_SET_ASSERT(set);
1539 + res = __ip_set_testip(set,
1540 + data + sizeof(struct ip_set_req_adt),
1541 + size - sizeof(struct ip_set_req_adt),
1544 + return (res > 0 ? -EEXIST : res);
1548 +ip_set_bindip(ip_set_id_t index,
1552 + struct ip_set *set = ip_set_list[index];
1553 + struct ip_set_req_bind *req_bind;
1554 + ip_set_id_t binding;
1558 + IP_SET_ASSERT(set);
1559 + if (size < sizeof(struct ip_set_req_bind))
1562 + req_bind = (struct ip_set_req_bind *) data;
1563 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1565 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1566 + /* Default binding of a set */
1567 + char *binding_name;
1569 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1572 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1573 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1575 + binding = ip_set_find_byname(binding_name);
1576 + if (binding == IP_SET_INVALID_ID)
1579 + write_lock_bh(&ip_set_lock);
1580 + /* Sets as binding values are referenced */
1581 + if (set->binding != IP_SET_INVALID_ID)
1582 + __ip_set_put(set->binding);
1583 + set->binding = binding;
1584 + __ip_set_get(set->binding);
1585 + write_unlock_bh(&ip_set_lock);
1589 + binding = ip_set_find_byname(req_bind->binding);
1590 + if (binding == IP_SET_INVALID_ID)
1593 + res = __ip_set_testip(set,
1594 + data + sizeof(struct ip_set_req_bind),
1595 + size - sizeof(struct ip_set_req_bind),
1597 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1598 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1601 + res = ip_set_hash_add(set->id, ip, binding);
1606 +#define FOREACH_SET_DO(fn, args...) \
1608 + ip_set_id_t __i; \
1609 + struct ip_set *__set; \
1611 + for (__i = 0; __i < ip_set_max; __i++) { \
1612 + __set = ip_set_list[__i]; \
1613 + if (__set != NULL) \
1614 + fn(__set , ##args); \
1619 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1621 + if (set_hash->id == id)
1622 + __set_hash_del(set_hash);
1626 +__unbind_default(struct ip_set *set)
1628 + if (set->binding != IP_SET_INVALID_ID) {
1629 + /* Sets as binding values are referenced */
1630 + __ip_set_put(set->binding);
1631 + set->binding = IP_SET_INVALID_ID;
1636 +ip_set_unbindip(ip_set_id_t index,
1640 + struct ip_set *set;
1641 + struct ip_set_req_bind *req_bind;
1646 + if (size < sizeof(struct ip_set_req_bind))
1649 + req_bind = (struct ip_set_req_bind *) data;
1650 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1652 + DP("%u %s", index, req_bind->binding);
1653 + if (index == IP_SET_INVALID_ID) {
1654 + /* unbind :all: */
1655 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1656 + /* Default binding of sets */
1657 + write_lock_bh(&ip_set_lock);
1658 + FOREACH_SET_DO(__unbind_default);
1659 + write_unlock_bh(&ip_set_lock);
1661 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1662 + /* Flush all bindings of all sets*/
1663 + write_lock_bh(&ip_set_lock);
1664 + FOREACH_HASH_RW_DO(__set_hash_del);
1665 + write_unlock_bh(&ip_set_lock);
1668 + DP("unreachable reached!");
1672 + set = ip_set_list[index];
1673 + IP_SET_ASSERT(set);
1674 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1675 + /* Default binding of set */
1676 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1678 + if (binding == IP_SET_INVALID_ID)
1681 + write_lock_bh(&ip_set_lock);
1682 + /* Sets in hash values are referenced */
1683 + __ip_set_put(set->binding);
1684 + set->binding = IP_SET_INVALID_ID;
1685 + write_unlock_bh(&ip_set_lock);
1688 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1689 + /* Flush all bindings */
1691 + write_lock_bh(&ip_set_lock);
1692 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1693 + write_unlock_bh(&ip_set_lock);
1697 + res = __ip_set_testip(set,
1698 + data + sizeof(struct ip_set_req_bind),
1699 + size - sizeof(struct ip_set_req_bind),
1702 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1704 + res = ip_set_hash_del(set->id, ip);
1710 +ip_set_testbind(ip_set_id_t index,
1714 + struct ip_set *set = ip_set_list[index];
1715 + struct ip_set_req_bind *req_bind;
1716 + ip_set_id_t binding;
1720 + IP_SET_ASSERT(set);
1721 + if (size < sizeof(struct ip_set_req_bind))
1724 + req_bind = (struct ip_set_req_bind *) data;
1725 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1727 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1728 + /* Default binding of set */
1729 + char *binding_name;
1731 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1734 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1735 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1737 + binding = ip_set_find_byname(binding_name);
1738 + if (binding == IP_SET_INVALID_ID)
1741 + res = (set->binding == binding) ? -EEXIST : 0;
1745 + binding = ip_set_find_byname(req_bind->binding);
1746 + if (binding == IP_SET_INVALID_ID)
1750 + res = __ip_set_testip(set,
1751 + data + sizeof(struct ip_set_req_bind),
1752 + size - sizeof(struct ip_set_req_bind),
1754 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1755 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1758 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1764 +static struct ip_set_type *
1765 +find_set_type_rlock(const char *typename)
1767 + struct ip_set_type *type;
1769 + read_lock_bh(&ip_set_lock);
1770 + type = find_set_type(typename);
1772 + read_unlock_bh(&ip_set_lock);
1778 +find_free_id(const char *name,
1779 + ip_set_id_t *index,
1784 + *id = IP_SET_INVALID_ID;
1785 + for (i = 0; i < ip_set_max; i++) {
1786 + if (ip_set_list[i] == NULL) {
1787 + if (*id == IP_SET_INVALID_ID)
1789 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1793 + if (*id == IP_SET_INVALID_ID)
1794 + /* No free slot remained */
1796 + /* Check that index is usable as id (swapping) */
1798 + for (i = 0; i < ip_set_max; i++) {
1799 + if (ip_set_list[i] != NULL
1800 + && ip_set_list[i]->id == *id) {
1812 +ip_set_create(const char *name,
1813 + const char *typename,
1814 + ip_set_id_t restore,
1818 + struct ip_set *set;
1819 + ip_set_id_t index, id;
1822 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1824 + * First, and without any locks, allocate and initialize
1825 + * a normal base set structure.
1827 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1830 + set->lock = RW_LOCK_UNLOCKED;
1831 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1832 + set->binding = IP_SET_INVALID_ID;
1833 + atomic_set(&set->ref, 0);
1836 + * Next, take the &ip_set_lock, check that we know the type,
1837 + * and take a reference on the type, to make sure it
1838 + * stays available while constructing our new set.
1840 + * After referencing the type, we drop the &ip_set_lock,
1841 + * and let the new set construction run without locks.
1843 + set->type = find_set_type_rlock(typename);
1844 + if (set->type == NULL) {
1845 + /* Try loading the module */
1846 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1847 + strcpy(modulename, "ip_set_");
1848 + strcat(modulename, typename);
1849 + DP("try to load %s", modulename);
1850 + request_module(modulename);
1851 + set->type = find_set_type_rlock(typename);
1853 + if (set->type == NULL) {
1854 + ip_set_printk("no set type '%s', set '%s' not created",
1859 + if (!try_module_get(set->type->me)) {
1860 + read_unlock_bh(&ip_set_lock);
1864 + read_unlock_bh(&ip_set_lock);
1867 + * Without holding any locks, create private part.
1869 + res = set->type->create(set, data, size);
1873 + /* BTW, res==0 here. */
1876 + * Here, we have a valid, constructed set. &ip_set_lock again,
1877 + * find free id/index and check that it is not already in
1880 + write_lock_bh(&ip_set_lock);
1881 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
1882 + DP("no free id!");
1886 + /* Make sure restore gets the same index */
1887 + if (restore != IP_SET_INVALID_ID && index != restore) {
1888 + DP("Can't restore, sets are screwed up");
1894 + * Finally! Add our shiny new set to the list, and be done.
1896 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
1898 + ip_set_list[index] = set;
1899 + write_unlock_bh(&ip_set_lock);
1903 + write_unlock_bh(&ip_set_lock);
1904 + set->type->destroy(set);
1906 + module_put(set->type->me);
1913 + * Destroy a given existing set
1916 +ip_set_destroy_set(ip_set_id_t index)
1918 + struct ip_set *set = ip_set_list[index];
1920 + IP_SET_ASSERT(set);
1921 + DP("set: %s", set->name);
1922 + write_lock_bh(&ip_set_lock);
1923 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1924 + if (set->binding != IP_SET_INVALID_ID)
1925 + __ip_set_put(set->binding);
1926 + ip_set_list[index] = NULL;
1927 + write_unlock_bh(&ip_set_lock);
1929 + /* Must call it without holding any lock */
1930 + set->type->destroy(set);
1931 + module_put(set->type->me);
1936 + * Destroy a set - or all sets
1937 + * Sets must not be referenced/used.
1940 +ip_set_destroy(ip_set_id_t index)
1944 + /* ref modification always protected by the mutex */
1945 + if (index != IP_SET_INVALID_ID) {
1946 + if (atomic_read(&ip_set_list[index]->ref))
1948 + ip_set_destroy_set(index);
1950 + for (i = 0; i < ip_set_max; i++) {
1951 + if (ip_set_list[i] != NULL
1952 + && (atomic_read(&ip_set_list[i]->ref)))
1956 + for (i = 0; i < ip_set_max; i++) {
1957 + if (ip_set_list[i] != NULL)
1958 + ip_set_destroy_set(i);
1965 +ip_set_flush_set(struct ip_set *set)
1967 + DP("set: %s %u", set->name, set->id);
1969 + write_lock_bh(&set->lock);
1970 + set->type->flush(set);
1971 + write_unlock_bh(&set->lock);
1975 + * Flush data in a set - or in all sets
1978 +ip_set_flush(ip_set_id_t index)
1980 + if (index != IP_SET_INVALID_ID) {
1981 + IP_SET_ASSERT(ip_set_list[index]);
1982 + ip_set_flush_set(ip_set_list[index]);
1984 + FOREACH_SET_DO(ip_set_flush_set);
1991 +ip_set_rename(ip_set_id_t index, const char *name)
1993 + struct ip_set *set = ip_set_list[index];
1997 + DP("set: %s to %s", set->name, name);
1998 + write_lock_bh(&ip_set_lock);
1999 + for (i = 0; i < ip_set_max; i++) {
2000 + if (ip_set_list[i] != NULL
2001 + && strncmp(ip_set_list[i]->name,
2003 + IP_SET_MAXNAMELEN - 1) == 0) {
2008 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2010 + write_unlock_bh(&ip_set_lock);
2015 + * Swap two sets so that name/index points to the other.
2016 + * References are also swapped.
2019 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2021 + struct ip_set *from = ip_set_list[from_index];
2022 + struct ip_set *to = ip_set_list[to_index];
2023 + char from_name[IP_SET_MAXNAMELEN];
2024 + u_int32_t from_ref;
2026 + DP("set: %s to %s", from->name, to->name);
2027 + /* Type can't be changed. Artifical restriction. */
2028 + if (from->type->typecode != to->type->typecode)
2031 + /* No magic here: ref munging protected by the mutex */
2032 + write_lock_bh(&ip_set_lock);
2033 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2034 + from_ref = atomic_read(&from->ref);
2036 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2037 + atomic_set(&from->ref, atomic_read(&to->ref));
2038 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2039 + atomic_set(&to->ref, from_ref);
2041 + ip_set_list[from_index] = to;
2042 + ip_set_list[to_index] = from;
2044 + write_unlock_bh(&ip_set_lock);
2053 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2054 + ip_set_id_t id, size_t *size)
2056 + if (set_hash->id == id)
2057 + *size += sizeof(struct ip_set_hash_list);
2061 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2062 + ip_set_id_t id, size_t *size)
2064 + if (set_hash->id == id)
2065 + *size += sizeof(struct ip_set_hash_save);
2069 +__set_hash_bindings(struct ip_set_hash *set_hash,
2070 + ip_set_id_t id, void *data, int *used)
2072 + if (set_hash->id == id) {
2073 + struct ip_set_hash_list *hash_list =
2074 + (struct ip_set_hash_list *)(data + *used);
2076 + hash_list->ip = set_hash->ip;
2077 + hash_list->binding = set_hash->binding;
2078 + *used += sizeof(struct ip_set_hash_list);
2082 +static int ip_set_list_set(ip_set_id_t index,
2087 + struct ip_set *set = ip_set_list[index];
2088 + struct ip_set_list *set_list;
2090 + /* Pointer to our header */
2091 + set_list = (struct ip_set_list *) (data + *used);
2093 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2095 + /* Get and ensure header size */
2096 + if (*used + sizeof(struct ip_set_list) > len)
2097 + goto not_enough_mem;
2098 + *used += sizeof(struct ip_set_list);
2100 + read_lock_bh(&set->lock);
2101 + /* Get and ensure set specific header size */
2102 + set_list->header_size = set->type->header_size;
2103 + if (*used + set_list->header_size > len)
2106 + /* Fill in the header */
2107 + set_list->index = index;
2108 + set_list->binding = set->binding;
2109 + set_list->ref = atomic_read(&set->ref);
2111 + /* Fill in set spefific header data */
2112 + set->type->list_header(set, data + *used);
2113 + *used += set_list->header_size;
2115 + /* Get and ensure set specific members size */
2116 + set_list->members_size = set->type->list_members_size(set);
2117 + if (*used + set_list->members_size > len)
2120 + /* Fill in set spefific members data */
2121 + set->type->list_members(set, data + *used);
2122 + *used += set_list->members_size;
2123 + read_unlock_bh(&set->lock);
2127 + /* Get and ensure set specific bindings size */
2128 + set_list->bindings_size = 0;
2129 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2130 + set->id, &set_list->bindings_size);
2131 + if (*used + set_list->bindings_size > len)
2132 + goto not_enough_mem;
2134 + /* Fill in set spefific bindings data */
2135 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2140 + read_unlock_bh(&set->lock);
2142 + DP("not enough mem, try again");
2149 +static int ip_set_save_set(ip_set_id_t index,
2154 + struct ip_set *set;
2155 + struct ip_set_save *set_save;
2157 + /* Pointer to our header */
2158 + set_save = (struct ip_set_save *) (data + *used);
2160 + /* Get and ensure header size */
2161 + if (*used + sizeof(struct ip_set_save) > len)
2162 + goto not_enough_mem;
2163 + *used += sizeof(struct ip_set_save);
2165 + set = ip_set_list[index];
2166 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2167 + data, data + *used);
2169 + read_lock_bh(&set->lock);
2170 + /* Get and ensure set specific header size */
2171 + set_save->header_size = set->type->header_size;
2172 + if (*used + set_save->header_size > len)
2175 + /* Fill in the header */
2176 + set_save->index = index;
2177 + set_save->binding = set->binding;
2179 + /* Fill in set spefific header data */
2180 + set->type->list_header(set, data + *used);
2181 + *used += set_save->header_size;
2183 + DP("set header filled: %s, used: %u %p %p", set->name, *used,
2184 + data, data + *used);
2185 + /* Get and ensure set specific members size */
2186 + set_save->members_size = set->type->list_members_size(set);
2187 + if (*used + set_save->members_size > len)
2190 + /* Fill in set spefific members data */
2191 + set->type->list_members(set, data + *used);
2192 + *used += set_save->members_size;
2193 + read_unlock_bh(&set->lock);
2194 + DP("set members filled: %s, used: %u %p %p", set->name, *used,
2195 + data, data + *used);
2199 + read_unlock_bh(&set->lock);
2201 + DP("not enough mem, try again");
2206 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2214 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2215 + struct ip_set_hash_save *hash_save =
2216 + (struct ip_set_hash_save *)(data + *used);
2217 + /* Ensure bindings size */
2218 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2222 + hash_save->id = set_hash->id;
2223 + hash_save->ip = set_hash->ip;
2224 + hash_save->binding = set_hash->binding;
2225 + *used += sizeof(struct ip_set_hash_save);
2229 +static int ip_set_save_bindings(ip_set_id_t index,
2235 + struct ip_set_save *set_save;
2237 + DP("used %u, len %u", *used, len);
2238 + /* Get and ensure header size */
2239 + if (*used + sizeof(struct ip_set_save) > len)
2243 + set_save = (struct ip_set_save *) (data + *used);
2244 + set_save->index = IP_SET_INVALID_ID;
2245 + *used += sizeof(struct ip_set_save);
2247 + DP("marker added used %u, len %u", *used, len);
2248 + /* Fill in bindings data */
2249 + if (index != IP_SET_INVALID_ID)
2250 + /* Sets are identified by id in hash */
2251 + index = ip_set_list[index]->id;
2252 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2260 +static int ip_set_restore(void *data,
2264 + int line = 0, used = 0, members_size;
2265 + struct ip_set *set;
2266 + struct ip_set_hash_save *hash_save;
2267 + struct ip_set_restore *set_restore;
2268 + ip_set_id_t index;
2270 + /* Loop to restore sets */
2274 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2275 + /* Get and ensure header size */
2276 + if (used + sizeof(struct ip_set_restore) > len)
2278 + set_restore = (struct ip_set_restore *) (data + used);
2279 + used += sizeof(struct ip_set_restore);
2281 + /* Ensure data size */
2283 + + set_restore->header_size
2284 + + set_restore->members_size > len)
2287 + /* Check marker */
2288 + if (set_restore->index == IP_SET_INVALID_ID) {
2293 + /* Try to create the set */
2294 + DP("restore %s %s", set_restore->name, set_restore->typename);
2295 + res = ip_set_create(set_restore->name,
2296 + set_restore->typename,
2297 + set_restore->index,
2299 + set_restore->header_size);
2303 + used += set_restore->header_size;
2305 + index = ip_set_find_byindex(set_restore->index);
2306 + DP("index %u, restore_index %u", index, set_restore->index);
2307 + if (index != set_restore->index)
2309 + /* Try to restore members data */
2310 + set = ip_set_list[index];
2312 + DP("members_size %u reqsize %u",
2313 + set_restore->members_size, set->type->reqsize);
2314 + while (members_size + set->type->reqsize <=
2315 + set_restore->members_size) {
2317 + DP("members: %u, line %u", members_size, line);
2318 + res = __ip_set_addip(index,
2319 + data + used + members_size,
2320 + set->type->reqsize);
2321 + if (!(res == 0 || res == -EEXIST))
2323 + members_size += set->type->reqsize;
2326 + DP("members_size %u %u",
2327 + set_restore->members_size, members_size);
2328 + if (members_size != set_restore->members_size)
2330 + used += set_restore->members_size;
2334 + /* Loop to restore bindings */
2335 + while (used < len) {
2338 + DP("restore binding, line %u", line);
2339 + /* Get and ensure size */
2340 + if (used + sizeof(struct ip_set_hash_save) > len)
2342 + hash_save = (struct ip_set_hash_save *) (data + used);
2343 + used += sizeof(struct ip_set_hash_save);
2345 + /* hash_save->id is used to store the index */
2346 + index = ip_set_find_byindex(hash_save->id);
2347 + DP("restore binding index %u, id %u, %u -> %u",
2348 + index, hash_save->id, hash_save->ip, hash_save->binding);
2349 + if (index != hash_save->id)
2352 + set = ip_set_list[hash_save->id];
2353 + /* Null valued IP means default binding */
2354 + if (hash_save->ip)
2355 + res = ip_set_hash_add(set->id,
2357 + hash_save->binding);
2359 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2360 + write_lock_bh(&ip_set_lock);
2361 + set->binding = hash_save->binding;
2362 + __ip_set_get(set->binding);
2363 + write_unlock_bh(&ip_set_lock);
2364 + DP("default binding: %u", set->binding);
2376 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2379 + int res = 0; /* Assume OK */
2381 + struct ip_set_req_adt *req_adt;
2382 + ip_set_id_t index = IP_SET_INVALID_ID;
2383 + int (*adtfn)(ip_set_id_t index,
2384 + const void *data, size_t size);
2386 + int (*fn)(ip_set_id_t index,
2387 + const void *data, size_t size);
2389 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2390 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2393 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2394 + if (!capable(CAP_NET_ADMIN))
2396 + if (optval != SO_IP_SET)
2398 + if (len <= sizeof(unsigned)) {
2399 + ip_set_printk("short userdata (want >%zu, got %u)",
2400 + sizeof(unsigned), len);
2403 + data = vmalloc(len);
2405 + DP("out of mem for %u bytes", len);
2408 + if (copy_from_user(data, user, len) != 0) {
2412 + if (down_interruptible(&ip_set_app_mutex)) {
2417 + op = (unsigned *)data;
2420 + if (*op < IP_SET_OP_VERSION) {
2421 + /* Check the version at the beginning of operations */
2422 + struct ip_set_req_version *req_version =
2423 + (struct ip_set_req_version *) data;
2424 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2431 + case IP_SET_OP_CREATE:{
2432 + struct ip_set_req_create *req_create
2433 + = (struct ip_set_req_create *) data;
2435 + if (len <= sizeof(struct ip_set_req_create)) {
2436 + ip_set_printk("short CREATE data (want >%zu, got %u)",
2437 + sizeof(struct ip_set_req_create), len);
2441 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2442 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2443 + res = ip_set_create(req_create->name,
2444 + req_create->typename,
2445 + IP_SET_INVALID_ID,
2446 + data + sizeof(struct ip_set_req_create),
2447 + len - sizeof(struct ip_set_req_create));
2450 + case IP_SET_OP_DESTROY:{
2451 + struct ip_set_req_std *req_destroy
2452 + = (struct ip_set_req_std *) data;
2454 + if (len != sizeof(struct ip_set_req_std)) {
2455 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2456 + sizeof(struct ip_set_req_std), len);
2460 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2461 + /* Destroy all sets */
2462 + index = IP_SET_INVALID_ID;
2464 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2465 + index = ip_set_find_byname(req_destroy->name);
2467 + if (index == IP_SET_INVALID_ID) {
2473 + res = ip_set_destroy(index);
2476 + case IP_SET_OP_FLUSH:{
2477 + struct ip_set_req_std *req_flush =
2478 + (struct ip_set_req_std *) data;
2480 + if (len != sizeof(struct ip_set_req_std)) {
2481 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2482 + sizeof(struct ip_set_req_std), len);
2486 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2487 + /* Flush all sets */
2488 + index = IP_SET_INVALID_ID;
2490 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2491 + index = ip_set_find_byname(req_flush->name);
2493 + if (index == IP_SET_INVALID_ID) {
2498 + res = ip_set_flush(index);
2501 + case IP_SET_OP_RENAME:{
2502 + struct ip_set_req_create *req_rename
2503 + = (struct ip_set_req_create *) data;
2505 + if (len != sizeof(struct ip_set_req_create)) {
2506 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2507 + sizeof(struct ip_set_req_create), len);
2512 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2513 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2515 + index = ip_set_find_byname(req_rename->name);
2516 + if (index == IP_SET_INVALID_ID) {
2520 + res = ip_set_rename(index, req_rename->typename);
2523 + case IP_SET_OP_SWAP:{
2524 + struct ip_set_req_create *req_swap
2525 + = (struct ip_set_req_create *) data;
2526 + ip_set_id_t to_index;
2528 + if (len != sizeof(struct ip_set_req_create)) {
2529 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2530 + sizeof(struct ip_set_req_create), len);
2535 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2536 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2538 + index = ip_set_find_byname(req_swap->name);
2539 + if (index == IP_SET_INVALID_ID) {
2543 + to_index = ip_set_find_byname(req_swap->typename);
2544 + if (to_index == IP_SET_INVALID_ID) {
2548 + res = ip_set_swap(index, to_index);
2552 + break; /* Set identified by id */
2555 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2556 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2560 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2562 + if (len < sizeof(struct ip_set_req_adt)) {
2563 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2564 + sizeof(struct ip_set_req_adt), len);
2568 + req_adt = (struct ip_set_req_adt *) data;
2570 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2571 + if (!(*op == IP_SET_OP_UNBIND_SET
2572 + && req_adt->index == IP_SET_INVALID_ID)) {
2573 + index = ip_set_find_byindex(req_adt->index);
2574 + if (index == IP_SET_INVALID_ID) {
2579 + res = adtfn(index, data, len);
2582 + up(&ip_set_app_mutex);
2586 + DP("final result %d", res);
2591 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2595 + ip_set_id_t index = IP_SET_INVALID_ID;
2597 + int copylen = *len;
2599 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2600 + if (!capable(CAP_NET_ADMIN))
2602 + if (optval != SO_IP_SET)
2604 + if (*len < sizeof(unsigned)) {
2605 + ip_set_printk("short userdata (want >=%zu, got %d)",
2606 + sizeof(unsigned), *len);
2609 + data = vmalloc(*len);
2611 + DP("out of mem for %d bytes", *len);
2614 + if (copy_from_user(data, user, *len) != 0) {
2618 + if (down_interruptible(&ip_set_app_mutex)) {
2623 + op = (unsigned *) data;
2626 + if (*op < IP_SET_OP_VERSION) {
2627 + /* Check the version at the beginning of operations */
2628 + struct ip_set_req_version *req_version =
2629 + (struct ip_set_req_version *) data;
2630 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2637 + case IP_SET_OP_VERSION: {
2638 + struct ip_set_req_version *req_version =
2639 + (struct ip_set_req_version *) data;
2641 + if (*len != sizeof(struct ip_set_req_version)) {
2642 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2643 + sizeof(struct ip_set_req_version),
2649 + req_version->version = IP_SET_PROTOCOL_VERSION;
2650 + res = copy_to_user(user, req_version,
2651 + sizeof(struct ip_set_req_version));
2654 + case IP_SET_OP_GET_BYNAME: {
2655 + struct ip_set_req_get_set *req_get
2656 + = (struct ip_set_req_get_set *) data;
2658 + if (*len != sizeof(struct ip_set_req_get_set)) {
2659 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2660 + sizeof(struct ip_set_req_get_set), *len);
2664 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2665 + index = ip_set_find_byname(req_get->set.name);
2666 + req_get->set.index = index;
2669 + case IP_SET_OP_GET_BYINDEX: {
2670 + struct ip_set_req_get_set *req_get
2671 + = (struct ip_set_req_get_set *) data;
2673 + if (*len != sizeof(struct ip_set_req_get_set)) {
2674 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2675 + sizeof(struct ip_set_req_get_set), *len);
2679 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2680 + index = ip_set_find_byindex(req_get->set.index);
2681 + strncpy(req_get->set.name,
2682 + index == IP_SET_INVALID_ID ? ""
2683 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2686 + case IP_SET_OP_ADT_GET: {
2687 + struct ip_set_req_adt_get *req_get
2688 + = (struct ip_set_req_adt_get *) data;
2690 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2691 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2692 + sizeof(struct ip_set_req_adt_get), *len);
2696 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2697 + index = ip_set_find_byname(req_get->set.name);
2698 + if (index != IP_SET_INVALID_ID) {
2699 + req_get->set.index = index;
2700 + strncpy(req_get->typename,
2701 + ip_set_list[index]->type->typename,
2702 + IP_SET_MAXNAMELEN - 1);
2709 + case IP_SET_OP_MAX_SETS: {
2710 + struct ip_set_req_max_sets *req_max_sets
2711 + = (struct ip_set_req_max_sets *) data;
2714 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2715 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2716 + sizeof(struct ip_set_req_max_sets), *len);
2721 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2722 + req_max_sets->set.index = IP_SET_INVALID_ID;
2724 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2725 + req_max_sets->set.index =
2726 + ip_set_find_byname(req_max_sets->set.name);
2727 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2732 + req_max_sets->max_sets = ip_set_max;
2733 + req_max_sets->sets = 0;
2734 + for (i = 0; i < ip_set_max; i++) {
2735 + if (ip_set_list[i] != NULL)
2736 + req_max_sets->sets++;
2740 + case IP_SET_OP_LIST_SIZE:
2741 + case IP_SET_OP_SAVE_SIZE: {
2742 + struct ip_set_req_setnames *req_setnames
2743 + = (struct ip_set_req_setnames *) data;
2744 + struct ip_set_name_list *name_list;
2745 + struct ip_set *set;
2749 + if (*len < sizeof(struct ip_set_req_setnames)) {
2750 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2751 + sizeof(struct ip_set_req_setnames), *len);
2756 + req_setnames->size = 0;
2757 + used = sizeof(struct ip_set_req_setnames);
2758 + for (i = 0; i < ip_set_max; i++) {
2759 + if (ip_set_list[i] == NULL)
2761 + name_list = (struct ip_set_name_list *)
2763 + used += sizeof(struct ip_set_name_list);
2764 + if (used > copylen) {
2768 + set = ip_set_list[i];
2769 + /* Fill in index, name, etc. */
2770 + name_list->index = i;
2771 + name_list->id = set->id;
2772 + strncpy(name_list->name,
2774 + IP_SET_MAXNAMELEN - 1);
2775 + strncpy(name_list->typename,
2776 + set->type->typename,
2777 + IP_SET_MAXNAMELEN - 1);
2778 + DP("filled %s of type %s, index %u\n",
2779 + name_list->name, name_list->typename,
2780 + name_list->index);
2781 + if (!(req_setnames->index == IP_SET_INVALID_ID
2782 + || req_setnames->index == i))
2786 + case IP_SET_OP_LIST_SIZE: {
2787 + req_setnames->size += sizeof(struct ip_set_list)
2788 + + set->type->header_size
2789 + + set->type->list_members_size(set);
2790 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2791 + i, &req_setnames->size);
2794 + case IP_SET_OP_SAVE_SIZE: {
2795 + req_setnames->size += sizeof(struct ip_set_save)
2796 + + set->type->header_size
2797 + + set->type->list_members_size(set);
2798 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2799 + i, &req_setnames->size);
2806 + if (copylen != used) {
2812 + case IP_SET_OP_LIST: {
2813 + struct ip_set_req_list *req_list
2814 + = (struct ip_set_req_list *) data;
2818 + if (*len < sizeof(struct ip_set_req_list)) {
2819 + ip_set_printk("short LIST (want >=%zu, got %d)",
2820 + sizeof(struct ip_set_req_list), *len);
2824 + index = req_list->index;
2825 + if (index != IP_SET_INVALID_ID
2826 + && ip_set_find_byindex(index) != index) {
2831 + if (index == IP_SET_INVALID_ID) {
2832 + /* List all sets */
2833 + for (i = 0; i < ip_set_max && res == 0; i++) {
2834 + if (ip_set_list[i] != NULL)
2835 + res = ip_set_list_set(i, data, &used, *len);
2838 + /* List an individual set */
2839 + res = ip_set_list_set(index, data, &used, *len);
2843 + else if (copylen != used) {
2849 + case IP_SET_OP_SAVE: {
2850 + struct ip_set_req_list *req_save
2851 + = (struct ip_set_req_list *) data;
2855 + if (*len < sizeof(struct ip_set_req_list)) {
2856 + ip_set_printk("short SAVE (want >=%zu, got %d)",
2857 + sizeof(struct ip_set_req_list), *len);
2861 + index = req_save->index;
2862 + if (index != IP_SET_INVALID_ID
2863 + && ip_set_find_byindex(index) != index) {
2868 + if (index == IP_SET_INVALID_ID) {
2869 + /* Save all sets */
2870 + for (i = 0; i < ip_set_max && res == 0; i++) {
2871 + if (ip_set_list[i] != NULL)
2872 + res = ip_set_save_set(i, data, &used, *len);
2875 + /* Save an individual set */
2876 + res = ip_set_save_set(index, data, &used, *len);
2879 + res = ip_set_save_bindings(index, data, &used, *len);
2883 + else if (copylen != used) {
2889 + case IP_SET_OP_RESTORE: {
2890 + struct ip_set_req_setnames *req_restore
2891 + = (struct ip_set_req_setnames *) data;
2894 + if (*len < sizeof(struct ip_set_req_setnames)
2895 + || *len != req_restore->size) {
2896 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
2897 + req_restore->size, *len);
2901 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
2902 + req_restore->size - sizeof(struct ip_set_req_setnames));
2903 + DP("ip_set_restore: %u", line);
2906 + req_restore->size = line;
2907 + copylen = sizeof(struct ip_set_req_setnames);
2915 + } /* end of switch(op) */
2918 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
2919 + && ip_set_list[index]
2920 + ? ip_set_list[index]->name
2921 + : ":all:", copylen);
2923 + res = copy_to_user(user, data, copylen);
2925 + copy_to_user(user, data, copylen);
2928 + up(&ip_set_app_mutex);
2932 + DP("final result %d", res);
2936 +static struct nf_sockopt_ops so_set = {
2938 + .set_optmin = SO_IP_SET,
2939 + .set_optmax = SO_IP_SET + 1,
2940 + .set = &ip_set_sockfn_set,
2941 + .get_optmin = SO_IP_SET,
2942 + .get_optmax = SO_IP_SET + 1,
2943 + .get = &ip_set_sockfn_get,
2947 +static int max_sets, hash_size;
2948 +module_param(max_sets, int, 0600);
2949 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
2950 +module_param(hash_size, int, 0600);
2951 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
2952 +MODULE_LICENSE("GPL");
2953 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
2954 +MODULE_DESCRIPTION("module implementing core IP set support");
2956 +static int __init init(void)
2961 + get_random_bytes(&ip_set_hash_random, 4);
2963 + ip_set_max = max_sets;
2964 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
2965 + if (!ip_set_list) {
2966 + printk(KERN_ERR "Unable to create ip_set_list\n");
2969 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
2971 + ip_set_bindings_hash_size = hash_size;
2972 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
2973 + if (!ip_set_hash) {
2974 + printk(KERN_ERR "Unable to create ip_set_hash\n");
2975 + vfree(ip_set_list);
2978 + for (i = 0; i < ip_set_bindings_hash_size; i++)
2979 + INIT_LIST_HEAD(&ip_set_hash[i]);
2981 + INIT_LIST_HEAD(&set_type_list);
2983 + res = nf_register_sockopt(&so_set);
2985 + ip_set_printk("SO_SET registry failed: %d", res);
2986 + vfree(ip_set_list);
2987 + vfree(ip_set_hash);
2993 +static void __exit fini(void)
2995 + /* There can't be any existing set or binding */
2996 + nf_unregister_sockopt(&so_set);
2997 + vfree(ip_set_list);
2998 + vfree(ip_set_hash);
2999 + DP("these are the famous last words");
3002 +EXPORT_SYMBOL(ip_set_register_set_type);
3003 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3005 +EXPORT_SYMBOL(ip_set_get_byname);
3006 +EXPORT_SYMBOL(ip_set_get_byindex);
3007 +EXPORT_SYMBOL(ip_set_put);
3009 +EXPORT_SYMBOL(ip_set_addip_kernel);
3010 +EXPORT_SYMBOL(ip_set_delip_kernel);
3011 +EXPORT_SYMBOL(ip_set_testip_kernel);
3015 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/ip_set_iphash.c linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_iphash.c
3016 --- linux-2.6.12.5/net/ipv4/netfilter/ip_set_iphash.c 1970-01-01 01:00:00.000000000 +0100
3017 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_iphash.c 2005-09-20 13:11:38.786384250 +0200
3019 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3021 + * This program is free software; you can redistribute it and/or modify
3022 + * it under the terms of the GNU General Public License version 2 as
3023 + * published by the Free Software Foundation.
3026 +/* Kernel module implementing an ip hash set */
3028 +#include <linux/module.h>
3029 +#include <linux/ip.h>
3030 +#include <linux/skbuff.h>
3031 +#include <linux/netfilter_ipv4/ip_tables.h>
3032 +#include <linux/netfilter_ipv4/ip_set.h>
3033 +#include <linux/errno.h>
3034 +#include <asm/uaccess.h>
3035 +#include <asm/bitops.h>
3036 +#include <linux/spinlock.h>
3037 +#include <linux/vmalloc.h>
3038 +#include <linux/random.h>
3040 +#include <net/ip.h>
3042 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3043 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3044 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
3045 +#include <linux/netfilter_ipv4/ip_set_prime.h>
3047 +static inline __u32
3048 +jhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3050 + return jhash_1word(ip, map->initval);
3053 +static inline __u32
3054 +randhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3056 + return (1 + ip % map->prime);
3059 +static inline __u32
3060 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3062 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3063 + __u32 jhash, randhash, id;
3066 + *hash_ip = ip & map->netmask;
3067 + jhash = jhash_ip(map, *hash_ip);
3068 + randhash = randhash_ip(map, *hash_ip);
3069 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3070 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3072 + for (i = 0; i < map->probes; i++) {
3073 + id = (jhash + i * randhash) % map->hashsize;
3074 + DP("hash key: %u", id);
3075 + if (map->members[id] == *hash_ip)
3077 + /* No shortcut at testing - there can be deleted
3084 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3086 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
3090 +testip(struct ip_set *set, const void *data, size_t size,
3091 + ip_set_ip_t *hash_ip)
3093 + struct ip_set_req_iphash *req =
3094 + (struct ip_set_req_iphash *) data;
3096 + if (size != sizeof(struct ip_set_req_iphash)) {
3097 + ip_set_printk("data length wrong (want %zu, have %zu)",
3098 + sizeof(struct ip_set_req_iphash),
3102 + return __testip(set, req->ip, hash_ip);
3106 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
3107 + u_int32_t flags, ip_set_ip_t *hash_ip)
3109 + return __testip(set,
3110 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3111 + : skb->nh.iph->daddr),
3116 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3118 + __u32 jhash, randhash, probe;
3121 + *hash_ip = ip & map->netmask;
3122 + jhash = jhash_ip(map, *hash_ip);
3123 + randhash = randhash_ip(map, *hash_ip);
3125 + for (i = 0; i < map->probes; i++) {
3126 + probe = (jhash + i * randhash) % map->hashsize;
3127 + if (map->members[probe] == *hash_ip)
3129 + if (!map->members[probe]) {
3130 + map->members[probe] = *hash_ip;
3134 + /* Trigger rehashing */
3139 +addip(struct ip_set *set, const void *data, size_t size,
3140 + ip_set_ip_t *hash_ip)
3142 + struct ip_set_req_iphash *req =
3143 + (struct ip_set_req_iphash *) data;
3145 + if (size != sizeof(struct ip_set_req_iphash)) {
3146 + ip_set_printk("data length wrong (want %zu, have %zu)",
3147 + sizeof(struct ip_set_req_iphash),
3151 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3155 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3156 + u_int32_t flags, ip_set_ip_t *hash_ip)
3158 + return __addip((struct ip_set_iphash *) set->data,
3159 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3160 + : skb->nh.iph->daddr),
3164 +static int retry(struct ip_set *set)
3166 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3167 + ip_set_ip_t hash_ip, *members;
3168 + u_int32_t i, hashsize;
3169 + unsigned newbytes;
3171 + struct ip_set_iphash tmp = {
3172 + .hashsize = map->hashsize,
3173 + .probes = map->probes,
3174 + .resize = map->resize,
3175 + .netmask = map->netmask,
3178 + if (map->resize == 0)
3184 + /* Calculate new parameters */
3185 + get_random_bytes(&tmp.initval, 4);
3186 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
3187 + if (hashsize == tmp.hashsize)
3189 + tmp.prime = make_prime(hashsize);
3191 + ip_set_printk("rehashing of set %s triggered: "
3192 + "hashsize grows from %u to %u",
3193 + set->name, tmp.hashsize, hashsize);
3194 + tmp.hashsize = hashsize;
3196 + newbytes = hashsize * sizeof(ip_set_ip_t);
3197 + tmp.members = ip_set_malloc_atomic(newbytes);
3198 + if (!tmp.members) {
3199 + DP("out of memory for %d bytes", newbytes);
3202 + memset(tmp.members, 0, newbytes);
3204 + write_lock_bh(&set->lock);
3205 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3206 + for (i = 0; i < map->hashsize && res == 0; i++) {
3207 + if (map->members[i])
3208 + res = __addip(&tmp, map->members[i], &hash_ip);
3211 + /* Failure, try again */
3212 + write_unlock_bh(&set->lock);
3213 + ip_set_free(tmp.members, newbytes);
3217 + /* Success at resizing! */
3218 + members = map->members;
3219 + hashsize = map->hashsize;
3221 + map->initval = tmp.initval;
3222 + map->prime = tmp.prime;
3223 + map->hashsize = tmp.hashsize;
3224 + map->members = tmp.members;
3225 + write_unlock_bh(&set->lock);
3227 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
3233 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3235 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3236 + ip_set_ip_t id = hash_id(set, ip, hash_ip);
3238 + if (id == UINT_MAX)
3241 + map->members[id] = 0;
3246 +delip(struct ip_set *set, const void *data, size_t size,
3247 + ip_set_ip_t *hash_ip)
3249 + struct ip_set_req_iphash *req =
3250 + (struct ip_set_req_iphash *) data;
3252 + if (size != sizeof(struct ip_set_req_iphash)) {
3253 + ip_set_printk("data length wrong (want %zu, have %zu)",
3254 + sizeof(struct ip_set_req_iphash),
3258 + return __delip(set, req->ip, hash_ip);
3262 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3263 + u_int32_t flags, ip_set_ip_t *hash_ip)
3265 + return __delip(set,
3266 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3267 + : skb->nh.iph->daddr),
3271 +static int create(struct ip_set *set, const void *data, size_t size)
3273 + unsigned newbytes;
3274 + struct ip_set_req_iphash_create *req =
3275 + (struct ip_set_req_iphash_create *) data;
3276 + struct ip_set_iphash *map;
3278 + if (size != sizeof(struct ip_set_req_iphash_create)) {
3279 + ip_set_printk("data length wrong (want %zu, have %zu)",
3280 + sizeof(struct ip_set_req_iphash_create),
3285 + if (req->hashsize < 1) {
3286 + ip_set_printk("hashsize too small");
3290 + map = kmalloc(sizeof(struct ip_set_iphash), GFP_KERNEL);
3292 + DP("out of memory for %d bytes",
3293 + sizeof(struct ip_set_iphash));
3296 + get_random_bytes(&map->initval, 4);
3297 + map->prime = make_prime(req->hashsize);
3298 + map->hashsize = req->hashsize;
3299 + map->probes = req->probes;
3300 + map->resize = req->resize;
3301 + map->netmask = req->netmask;
3302 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
3303 + map->members = ip_set_malloc(newbytes);
3304 + if (!map->members) {
3305 + DP("out of memory for %d bytes", newbytes);
3309 + memset(map->members, 0, newbytes);
3315 +static void destroy(struct ip_set *set)
3317 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3319 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
3325 +static void flush(struct ip_set *set)
3327 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3328 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
3331 +static void list_header(const struct ip_set *set, void *data)
3333 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3334 + struct ip_set_req_iphash_create *header =
3335 + (struct ip_set_req_iphash_create *) data;
3337 + header->hashsize = map->hashsize;
3338 + header->probes = map->probes;
3339 + header->resize = map->resize;
3340 + header->netmask = map->netmask;
3343 +static int list_members_size(const struct ip_set *set)
3345 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3347 + return (map->hashsize * sizeof(ip_set_ip_t));
3350 +static void list_members(const struct ip_set *set, void *data)
3352 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3353 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
3355 + memcpy(data, map->members, bytes);
3358 +static struct ip_set_type ip_set_iphash = {
3359 + .typename = SETTYPE_NAME,
3360 + .typecode = IPSET_TYPE_IP,
3361 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3362 + .create = &create,
3363 + .destroy = &destroy,
3365 + .reqsize = sizeof(struct ip_set_req_iphash),
3367 + .addip_kernel = &addip_kernel,
3370 + .delip_kernel = &delip_kernel,
3371 + .testip = &testip,
3372 + .testip_kernel = &testip_kernel,
3373 + .header_size = sizeof(struct ip_set_req_iphash_create),
3374 + .list_header = &list_header,
3375 + .list_members_size = &list_members_size,
3376 + .list_members = &list_members,
3377 + .me = THIS_MODULE,
3380 +MODULE_LICENSE("GPL");
3381 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3382 +MODULE_DESCRIPTION("iphash type of IP sets");
3384 +static int __init init(void)
3386 + init_max_malloc_size();
3387 + return ip_set_register_set_type(&ip_set_iphash);
3390 +static void __exit fini(void)
3392 + /* FIXME: possible race with ip_set_create() */
3393 + ip_set_unregister_set_type(&ip_set_iphash);
3398 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/ip_set_ipmap.c linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_ipmap.c
3399 --- linux-2.6.12.5/net/ipv4/netfilter/ip_set_ipmap.c 1970-01-01 01:00:00.000000000 +0100
3400 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_ipmap.c 2005-09-20 13:11:38.802385250 +0200
3402 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3403 + * Patrick Schaaf <bof@bof.de>
3404 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3406 + * This program is free software; you can redistribute it and/or modify
3407 + * it under the terms of the GNU General Public License version 2 as
3408 + * published by the Free Software Foundation.
3411 +/* Kernel module implementing an IP set type: the single bitmap type */
3413 +#include <linux/module.h>
3414 +#include <linux/ip.h>
3415 +#include <linux/skbuff.h>
3416 +#include <linux/netfilter_ipv4/ip_tables.h>
3417 +#include <linux/netfilter_ipv4/ip_set.h>
3418 +#include <linux/errno.h>
3419 +#include <asm/uaccess.h>
3420 +#include <asm/bitops.h>
3421 +#include <linux/spinlock.h>
3423 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3425 +static inline ip_set_ip_t
3426 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3428 + return (ip - map->first_ip)/map->hosts;
3432 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3434 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3436 + if (ip < map->first_ip || ip > map->last_ip)
3439 + *hash_ip = ip & map->netmask;
3440 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3441 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3442 + return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3446 +testip(struct ip_set *set, const void *data, size_t size,
3447 + ip_set_ip_t *hash_ip)
3449 + struct ip_set_req_ipmap *req =
3450 + (struct ip_set_req_ipmap *) data;
3452 + if (size != sizeof(struct ip_set_req_ipmap)) {
3453 + ip_set_printk("data length wrong (want %zu, have %zu)",
3454 + sizeof(struct ip_set_req_ipmap),
3458 + return __testip(set, req->ip, hash_ip);
3462 +testip_kernel(struct ip_set *set,
3463 + const struct sk_buff *skb,
3465 + ip_set_ip_t *hash_ip)
3469 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3470 + flags & IPSET_SRC ? "SRC" : "DST",
3471 + NIPQUAD(skb->nh.iph->saddr),
3472 + NIPQUAD(skb->nh.iph->daddr));
3474 + res = __testip(set,
3475 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3476 + : skb->nh.iph->daddr),
3478 + return (res < 0 ? 0 : res);
3482 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3484 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3486 + if (ip < map->first_ip || ip > map->last_ip)
3489 + *hash_ip = ip & map->netmask;
3490 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3491 + if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3498 +addip(struct ip_set *set, const void *data, size_t size,
3499 + ip_set_ip_t *hash_ip)
3501 + struct ip_set_req_ipmap *req =
3502 + (struct ip_set_req_ipmap *) data;
3504 + if (size != sizeof(struct ip_set_req_ipmap)) {
3505 + ip_set_printk("data length wrong (want %zu, have %zu)",
3506 + sizeof(struct ip_set_req_ipmap),
3510 + DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3511 + return __addip(set, req->ip, hash_ip);
3515 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3516 + u_int32_t flags, ip_set_ip_t *hash_ip)
3518 + return __addip(set,
3519 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3520 + : skb->nh.iph->daddr),
3525 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3527 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3529 + if (ip < map->first_ip || ip > map->last_ip)
3532 + *hash_ip = ip & map->netmask;
3533 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3534 + if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3541 +delip(struct ip_set *set, const void *data, size_t size,
3542 + ip_set_ip_t *hash_ip)
3544 + struct ip_set_req_ipmap *req =
3545 + (struct ip_set_req_ipmap *) data;
3547 + if (size != sizeof(struct ip_set_req_ipmap)) {
3548 + ip_set_printk("data length wrong (want %zu, have %zu)",
3549 + sizeof(struct ip_set_req_ipmap),
3553 + return __delip(set, req->ip, hash_ip);
3557 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3558 + u_int32_t flags, ip_set_ip_t *hash_ip)
3560 + return __delip(set,
3561 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3562 + : skb->nh.iph->daddr),
3566 +static int create(struct ip_set *set, const void *data, size_t size)
3569 + struct ip_set_req_ipmap_create *req =
3570 + (struct ip_set_req_ipmap_create *) data;
3571 + struct ip_set_ipmap *map;
3573 + if (size != sizeof(struct ip_set_req_ipmap_create)) {
3574 + ip_set_printk("data length wrong (want %zu, have %zu)",
3575 + sizeof(struct ip_set_req_ipmap_create),
3580 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3581 + HIPQUAD(req->from), HIPQUAD(req->to));
3583 + if (req->from > req->to) {
3584 + DP("bad ip range");
3588 + if (req->to - req->from > MAX_RANGE) {
3589 + ip_set_printk("range too big (max %d addresses)",
3594 + map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3596 + DP("out of memory for %d bytes",
3597 + sizeof(struct ip_set_ipmap));
3600 + map->first_ip = req->from;
3601 + map->last_ip = req->to;
3602 + map->netmask = req->netmask;
3604 + if (req->netmask == 0xFFFFFFFF) {
3606 + map->sizeid = map->last_ip - map->first_ip + 1;
3608 + unsigned int mask_bits, netmask_bits;
3611 + map->first_ip &= map->netmask; /* Should we better bark? */
3613 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3614 + netmask_bits = mask_to_bits(map->netmask);
3616 + if (!mask || netmask_bits <= mask_bits)
3619 + map->hosts = 2 << (32 - netmask_bits - 1);
3620 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3622 + newbytes = bitmap_bytes(0, map->sizeid - 1);
3623 + map->members = kmalloc(newbytes, GFP_KERNEL);
3624 + if (!map->members) {
3625 + DP("out of memory for %d bytes", newbytes);
3629 + memset(map->members, 0, newbytes);
3635 +static void destroy(struct ip_set *set)
3637 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3639 + kfree(map->members);
3645 +static void flush(struct ip_set *set)
3647 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3648 + memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3651 +static void list_header(const struct ip_set *set, void *data)
3653 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3654 + struct ip_set_req_ipmap_create *header =
3655 + (struct ip_set_req_ipmap_create *) data;
3657 + header->from = map->first_ip;
3658 + header->to = map->last_ip;
3659 + header->netmask = map->netmask;
3662 +static int list_members_size(const struct ip_set *set)
3664 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3666 + return bitmap_bytes(0, map->sizeid - 1);
3669 +static void list_members(const struct ip_set *set, void *data)
3671 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3672 + int bytes = bitmap_bytes(0, map->sizeid - 1);
3674 + memcpy(data, map->members, bytes);
3677 +static struct ip_set_type ip_set_ipmap = {
3678 + .typename = SETTYPE_NAME,
3679 + .typecode = IPSET_TYPE_IP,
3680 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3681 + .create = &create,
3682 + .destroy = &destroy,
3684 + .reqsize = sizeof(struct ip_set_req_ipmap),
3686 + .addip_kernel = &addip_kernel,
3688 + .delip_kernel = &delip_kernel,
3689 + .testip = &testip,
3690 + .testip_kernel = &testip_kernel,
3691 + .header_size = sizeof(struct ip_set_req_ipmap_create),
3692 + .list_header = &list_header,
3693 + .list_members_size = &list_members_size,
3694 + .list_members = &list_members,
3695 + .me = THIS_MODULE,
3698 +MODULE_LICENSE("GPL");
3699 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3700 +MODULE_DESCRIPTION("ipmap type of IP sets");
3702 +static int __init init(void)
3704 + return ip_set_register_set_type(&ip_set_ipmap);
3707 +static void __exit fini(void)
3709 + /* FIXME: possible race with ip_set_create() */
3710 + ip_set_unregister_set_type(&ip_set_ipmap);
3715 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/ip_set_iptree.c linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_iptree.c
3716 --- linux-2.6.12.5/net/ipv4/netfilter/ip_set_iptree.c 1970-01-01 01:00:00.000000000 +0100
3717 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_iptree.c 2005-09-20 13:11:38.802385250 +0200
3719 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3721 + * This program is free software; you can redistribute it and/or modify
3722 + * it under the terms of the GNU General Public License version 2 as
3723 + * published by the Free Software Foundation.
3726 +/* Kernel module implementing an IP set type: the iptree type */
3728 +#include <linux/module.h>
3729 +#include <linux/ip.h>
3730 +#include <linux/skbuff.h>
3731 +#include <linux/slab.h>
3732 +#include <linux/delay.h>
3733 +#include <linux/netfilter_ipv4/ip_tables.h>
3734 +#include <linux/netfilter_ipv4/ip_set.h>
3735 +#include <linux/errno.h>
3736 +#include <asm/uaccess.h>
3737 +#include <asm/bitops.h>
3738 +#include <linux/spinlock.h>
3740 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
3742 +/* Garbage collection interval in seconds: */
3743 +#define IPTREE_GC_TIME 5*60
3744 +/* Sleep so many milliseconds before trying again
3745 + * to delete the gc timer at destroying a set */
3746 +#define IPTREE_DESTROY_SLEEP 100
3748 +static kmem_cache_t *branch_cachep;
3749 +static kmem_cache_t *leaf_cachep;
3751 +#define ABCD(a,b,c,d,addrp) do { \
3752 + a = ((unsigned char *)addrp)[3]; \
3753 + b = ((unsigned char *)addrp)[2]; \
3754 + c = ((unsigned char *)addrp)[1]; \
3755 + d = ((unsigned char *)addrp)[0]; \
3758 +#define TESTIP_WALK(map, elem, branch) do { \
3759 + if ((map)->tree[elem]) { \
3760 + branch = (map)->tree[elem]; \
3766 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3768 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3769 + struct ip_set_iptreeb *btree;
3770 + struct ip_set_iptreec *ctree;
3771 + struct ip_set_iptreed *dtree;
3772 + unsigned char a,b,c,d;
3775 + ABCD(a, b, c, d, hash_ip);
3776 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
3777 + TESTIP_WALK(map, a, btree);
3778 + TESTIP_WALK(btree, b, ctree);
3779 + TESTIP_WALK(ctree, c, dtree);
3780 + DP("%lu %lu", dtree->expires[d], jiffies);
3781 + return !!(map->timeout ? (time_after(dtree->expires[d], jiffies))
3782 + : dtree->expires[d]);
3786 +testip(struct ip_set *set, const void *data, size_t size,
3787 + ip_set_ip_t *hash_ip)
3789 + struct ip_set_req_iptree *req =
3790 + (struct ip_set_req_iptree *) data;
3792 + if (size != sizeof(struct ip_set_req_iptree)) {
3793 + ip_set_printk("data length wrong (want %zu, have %zu)",
3794 + sizeof(struct ip_set_req_iptree),
3798 + return __testip(set, req->ip, hash_ip);
3802 +testip_kernel(struct ip_set *set,
3803 + const struct sk_buff *skb,
3805 + ip_set_ip_t *hash_ip)
3809 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3810 + flags & IPSET_SRC ? "SRC" : "DST",
3811 + NIPQUAD(skb->nh.iph->saddr),
3812 + NIPQUAD(skb->nh.iph->daddr));
3814 + res = __testip(set,
3815 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3816 + : skb->nh.iph->daddr),
3818 + return (res < 0 ? 0 : res);
3821 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
3822 + if ((map)->tree[elem]) { \
3823 + DP("found %u", elem); \
3824 + branch = (map)->tree[elem]; \
3826 + branch = (type *) \
3827 + kmem_cache_alloc(cachep, GFP_KERNEL); \
3828 + if (branch == NULL) \
3830 + memset(branch, 0, sizeof(*branch)); \
3831 + (map)->tree[elem] = branch; \
3832 + DP("alloc %u", elem); \
3837 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
3838 + ip_set_ip_t *hash_ip)
3840 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3841 + struct ip_set_iptreeb *btree;
3842 + struct ip_set_iptreec *ctree;
3843 + struct ip_set_iptreed *dtree;
3844 + unsigned char a,b,c,d;
3848 + ABCD(a, b, c, d, hash_ip);
3849 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
3850 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
3851 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
3852 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
3853 + if (dtree->expires[d]
3854 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
3856 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
3857 + DP("%u %lu", d, dtree->expires[d]);
3862 +addip(struct ip_set *set, const void *data, size_t size,
3863 + ip_set_ip_t *hash_ip)
3865 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3866 + struct ip_set_req_iptree *req =
3867 + (struct ip_set_req_iptree *) data;
3869 + if (size != sizeof(struct ip_set_req_iptree)) {
3870 + ip_set_printk("data length wrong (want %zu, have %zu)",
3871 + sizeof(struct ip_set_req_iptree),
3875 + DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
3876 + return __addip(set, req->ip,
3877 + req->timeout ? req->timeout : map->timeout,
3882 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3883 + u_int32_t flags, ip_set_ip_t *hash_ip)
3885 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3887 + return __addip(set,
3888 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3889 + : skb->nh.iph->daddr),
3894 +#define DELIP_WALK(map, elem, branch) do { \
3895 + if ((map)->tree[elem]) { \
3896 + branch = (map)->tree[elem]; \
3902 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3904 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3905 + struct ip_set_iptreeb *btree;
3906 + struct ip_set_iptreec *ctree;
3907 + struct ip_set_iptreed *dtree;
3908 + unsigned char a,b,c,d;
3911 + ABCD(a, b, c, d, hash_ip);
3912 + DELIP_WALK(map, a, btree);
3913 + DELIP_WALK(btree, b, ctree);
3914 + DELIP_WALK(ctree, c, dtree);
3916 + if (dtree->expires[d]) {
3917 + dtree->expires[d] = 0;
3924 +delip(struct ip_set *set, const void *data, size_t size,
3925 + ip_set_ip_t *hash_ip)
3927 + struct ip_set_req_iptree *req =
3928 + (struct ip_set_req_iptree *) data;
3930 + if (size != sizeof(struct ip_set_req_iptree)) {
3931 + ip_set_printk("data length wrong (want %zu, have %zu)",
3932 + sizeof(struct ip_set_req_iptree),
3936 + return __delip(set, req->ip, hash_ip);
3940 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3941 + u_int32_t flags, ip_set_ip_t *hash_ip)
3943 + return __delip(set,
3944 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3945 + : skb->nh.iph->daddr),
3949 +#define LOOP_WALK_BEGIN(map, i, branch) \
3950 + for (i = 0; i < 255; i++) { \
3951 + if (!(map)->tree[i]) \
3953 + branch = (map)->tree[i]
3955 +#define LOOP_WALK_END }
3957 +static void ip_tree_gc(unsigned long ul_set)
3959 + struct ip_set *set = (void *) ul_set;
3960 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3961 + struct ip_set_iptreeb *btree;
3962 + struct ip_set_iptreec *ctree;
3963 + struct ip_set_iptreed *dtree;
3964 + unsigned char a,b,c,d;
3965 + unsigned char i,j,k;
3968 + DP("gc: %s", set->name);
3969 + write_lock_bh(&set->lock);
3970 + LOOP_WALK_BEGIN(map, a, btree);
3971 + LOOP_WALK_BEGIN(btree, b, ctree);
3972 + LOOP_WALK_BEGIN(ctree, c, dtree);
3973 + for (d = 0; d < 255; d++) {
3974 + if (dtree->expires[d]) {
3975 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
3977 + dtree->expires[d], jiffies);
3979 + && time_before(dtree->expires[d], jiffies))
3980 + dtree->expires[d] = 0;
3986 + DP("gc: %s: leaf %u %u %u empty",
3987 + set->name, a, b, c);
3988 + kmem_cache_free(leaf_cachep, dtree);
3989 + ctree->tree[c] = NULL;
3991 + DP("gc: %s: leaf %u %u %u not empty",
3992 + set->name, a, b, c);
3998 + DP("gc: %s: branch %u %u empty",
4000 + kmem_cache_free(branch_cachep, ctree);
4001 + btree->tree[b] = NULL;
4003 + DP("gc: %s: branch %u %u not empty",
4010 + DP("gc: %s: branch %u empty",
4012 + kmem_cache_free(branch_cachep, btree);
4013 + map->tree[a] = NULL;
4015 + DP("gc: %s: branch %u not empty",
4020 + write_unlock_bh(&set->lock);
4022 + map->gc.expires = jiffies + map->gc_interval * HZ;
4023 + add_timer(&map->gc);
4026 +static int create(struct ip_set *set, const void *data, size_t size)
4028 + struct ip_set_req_iptree_create *req =
4029 + (struct ip_set_req_iptree_create *) data;
4030 + struct ip_set_iptree *map;
4032 + if (size != sizeof(struct ip_set_req_iptree_create)) {
4033 + ip_set_printk("data length wrong (want %zu, have %zu)",
4034 + sizeof(struct ip_set_req_iptree_create),
4039 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4041 + DP("out of memory for %d bytes",
4042 + sizeof(struct ip_set_iptree));
4045 + memset(map, 0, sizeof(*map));
4046 + map->timeout = req->timeout;
4049 + /* If there is no timeout for the entries,
4050 + * we still have to call gc because delete
4051 + * do not clean up empty branches */
4052 + map->gc_interval = IPTREE_GC_TIME;
4053 + init_timer(&map->gc);
4054 + map->gc.data = (unsigned long) set;
4055 + map->gc.function = ip_tree_gc;
4056 + map->gc.expires = jiffies + map->gc_interval * HZ;
4057 + add_timer(&map->gc);
4062 +static void __flush(struct ip_set_iptree *map)
4064 + struct ip_set_iptreeb *btree;
4065 + struct ip_set_iptreec *ctree;
4066 + struct ip_set_iptreed *dtree;
4067 + unsigned int a,b,c;
4069 + LOOP_WALK_BEGIN(map, a, btree);
4070 + LOOP_WALK_BEGIN(btree, b, ctree);
4071 + LOOP_WALK_BEGIN(ctree, c, dtree);
4072 + kmem_cache_free(leaf_cachep, dtree);
4074 + kmem_cache_free(branch_cachep, ctree);
4076 + kmem_cache_free(branch_cachep, btree);
4080 +static void destroy(struct ip_set *set)
4082 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4084 + while (!del_timer(&map->gc))
4085 + msleep(IPTREE_DESTROY_SLEEP);
4091 +static void flush(struct ip_set *set)
4093 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4094 + unsigned int timeout = map->timeout;
4097 + memset(map, 0, sizeof(*map));
4098 + map->timeout = timeout;
4101 +static void list_header(const struct ip_set *set, void *data)
4103 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4104 + struct ip_set_req_iptree_create *header =
4105 + (struct ip_set_req_iptree_create *) data;
4107 + header->timeout = map->timeout;
4110 +static int list_members_size(const struct ip_set *set)
4112 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4113 + struct ip_set_iptreeb *btree;
4114 + struct ip_set_iptreec *ctree;
4115 + struct ip_set_iptreed *dtree;
4116 + unsigned char a,b,c,d;
4117 + unsigned int count = 0;
4119 + LOOP_WALK_BEGIN(map, a, btree);
4120 + LOOP_WALK_BEGIN(btree, b, ctree);
4121 + LOOP_WALK_BEGIN(ctree, c, dtree);
4122 + for (d = 0; d < 255; d++) {
4123 + if (dtree->expires[d]
4124 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4131 + DP("members %u", count);
4132 + return (count * sizeof(struct ip_set_req_iptree));
4135 +static void list_members(const struct ip_set *set, void *data)
4137 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4138 + struct ip_set_iptreeb *btree;
4139 + struct ip_set_iptreec *ctree;
4140 + struct ip_set_iptreed *dtree;
4141 + unsigned char a,b,c,d;
4142 + size_t offset = 0;
4143 + struct ip_set_req_iptree *entry;
4145 + LOOP_WALK_BEGIN(map, a, btree);
4146 + LOOP_WALK_BEGIN(btree, b, ctree);
4147 + LOOP_WALK_BEGIN(ctree, c, dtree);
4148 + for (d = 0; d < 255; d++) {
4149 + if (dtree->expires[d]
4150 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
4151 + entry = (struct ip_set_req_iptree *)(data + offset);
4152 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
4153 + entry->timeout = !map->timeout ? 0
4154 + : (dtree->expires[d] - jiffies)/HZ;
4155 + offset += sizeof(struct ip_set_req_iptree);
4163 +static struct ip_set_type ip_set_iptree = {
4164 + .typename = SETTYPE_NAME,
4165 + .typecode = IPSET_TYPE_IP,
4166 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4167 + .create = &create,
4168 + .destroy = &destroy,
4170 + .reqsize = sizeof(struct ip_set_req_iptree),
4172 + .addip_kernel = &addip_kernel,
4174 + .delip_kernel = &delip_kernel,
4175 + .testip = &testip,
4176 + .testip_kernel = &testip_kernel,
4177 + .header_size = sizeof(struct ip_set_req_iptree_create),
4178 + .list_header = &list_header,
4179 + .list_members_size = &list_members_size,
4180 + .list_members = &list_members,
4181 + .me = THIS_MODULE,
4184 +MODULE_LICENSE("GPL");
4185 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4186 +MODULE_DESCRIPTION("iptree type of IP sets");
4188 +static int __init init(void)
4192 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
4193 + sizeof(struct ip_set_iptreeb),
4194 + 0, 0, NULL, NULL);
4195 + if (!branch_cachep) {
4196 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
4200 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
4201 + sizeof(struct ip_set_iptreed),
4202 + 0, 0, NULL, NULL);
4203 + if (!leaf_cachep) {
4204 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
4208 + ret = ip_set_register_set_type(&ip_set_iptree);
4212 + kmem_cache_destroy(leaf_cachep);
4214 + kmem_cache_destroy(branch_cachep);
4219 +static void __exit fini(void)
4221 + /* FIXME: possible race with ip_set_create() */
4222 + ip_set_unregister_set_type(&ip_set_iptree);
4223 + kmem_cache_destroy(leaf_cachep);
4224 + kmem_cache_destroy(branch_cachep);
4229 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/ip_set_macipmap.c linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_macipmap.c
4230 --- linux-2.6.12.5/net/ipv4/netfilter/ip_set_macipmap.c 1970-01-01 01:00:00.000000000 +0100
4231 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_macipmap.c 2005-09-20 13:11:38.802385250 +0200
4233 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
4234 + * Patrick Schaaf <bof@bof.de>
4235 + * Martin Josefsson <gandalf@wlug.westbo.se>
4236 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4238 + * This program is free software; you can redistribute it and/or modify
4239 + * it under the terms of the GNU General Public License version 2 as
4240 + * published by the Free Software Foundation.
4243 +/* Kernel module implementing an IP set type: the macipmap type */
4245 +#include <linux/module.h>
4246 +#include <linux/ip.h>
4247 +#include <linux/skbuff.h>
4248 +#include <linux/netfilter_ipv4/ip_tables.h>
4249 +#include <linux/netfilter_ipv4/ip_set.h>
4250 +#include <linux/errno.h>
4251 +#include <asm/uaccess.h>
4252 +#include <asm/bitops.h>
4253 +#include <linux/spinlock.h>
4254 +#include <linux/if_ether.h>
4255 +#include <linux/vmalloc.h>
4257 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4258 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
4261 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
4263 + struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
4264 + struct ip_set_macip *table = (struct ip_set_macip *) map->members;
4265 + struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
4267 + if (size != sizeof(struct ip_set_req_macipmap)) {
4268 + ip_set_printk("data length wrong (want %zu, have %zu)",
4269 + sizeof(struct ip_set_req_macipmap),
4274 + if (req->ip < map->first_ip || req->ip > map->last_ip)
4277 + *hash_ip = req->ip;
4278 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4279 + set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
4280 + if (test_bit(IPSET_MACIP_ISSET,
4281 + (void *) &table[req->ip - map->first_ip].flags)) {
4282 + return (memcmp(req->ethernet,
4283 + &table[req->ip - map->first_ip].ethernet,
4286 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4291 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4292 + u_int32_t flags, ip_set_ip_t *hash_ip)
4294 + struct ip_set_macipmap *map =
4295 + (struct ip_set_macipmap *) set->data;
4296 + struct ip_set_macip *table =
4297 + (struct ip_set_macip *) map->members;
4300 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4301 + : skb->nh.iph->daddr);
4302 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4303 + flags & IPSET_SRC ? "SRC" : "DST",
4304 + NIPQUAD(skb->nh.iph->saddr),
4305 + NIPQUAD(skb->nh.iph->daddr));
4307 + if (ip < map->first_ip || ip > map->last_ip)
4311 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4312 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
4313 + if (test_bit(IPSET_MACIP_ISSET,
4314 + (void *) &table[ip - map->first_ip].flags)) {
4315 + /* Is mac pointer valid?
4316 + * If so, compare... */
4317 + return (skb->mac.raw >= skb->head
4318 + && (skb->mac.raw + ETH_HLEN) <= skb->data
4319 + && (memcmp(eth_hdr(skb)->h_source,
4320 + &table[ip - map->first_ip].ethernet,
4323 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4327 +/* returns 0 on success */
4329 +__addip(struct ip_set *set,
4330 + ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
4332 + struct ip_set_macipmap *map =
4333 + (struct ip_set_macipmap *) set->data;
4334 + struct ip_set_macip *table =
4335 + (struct ip_set_macip *) map->members;
4337 + if (ip < map->first_ip || ip > map->last_ip)
4339 + if (test_and_set_bit(IPSET_MACIP_ISSET,
4340 + (void *) &table[ip - map->first_ip].flags))
4344 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4345 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
4350 +addip(struct ip_set *set, const void *data, size_t size,
4351 + ip_set_ip_t *hash_ip)
4353 + struct ip_set_req_macipmap *req =
4354 + (struct ip_set_req_macipmap *) data;
4356 + if (size != sizeof(struct ip_set_req_macipmap)) {
4357 + ip_set_printk("data length wrong (want %zu, have %zu)",
4358 + sizeof(struct ip_set_req_macipmap),
4362 + return __addip(set, req->ip, req->ethernet, hash_ip);
4366 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4367 + u_int32_t flags, ip_set_ip_t *hash_ip)
4371 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4372 + : skb->nh.iph->daddr);
4374 + if (!(skb->mac.raw >= skb->head
4375 + && (skb->mac.raw + ETH_HLEN) <= skb->data))
4378 + return __addip(set, ip, eth_hdr(skb)->h_source, hash_ip);
4382 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4384 + struct ip_set_macipmap *map =
4385 + (struct ip_set_macipmap *) set->data;
4386 + struct ip_set_macip *table =
4387 + (struct ip_set_macip *) map->members;
4389 + if (ip < map->first_ip || ip > map->last_ip)
4391 + if (!test_and_clear_bit(IPSET_MACIP_ISSET,
4392 + (void *)&table[ip - map->first_ip].flags))
4396 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4401 +delip(struct ip_set *set, const void *data, size_t size,
4402 + ip_set_ip_t *hash_ip)
4404 + struct ip_set_req_macipmap *req =
4405 + (struct ip_set_req_macipmap *) data;
4407 + if (size != sizeof(struct ip_set_req_macipmap)) {
4408 + ip_set_printk("data length wrong (want %zu, have %zu)",
4409 + sizeof(struct ip_set_req_macipmap),
4413 + return __delip(set, req->ip, hash_ip);
4417 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4418 + u_int32_t flags, ip_set_ip_t *hash_ip)
4420 + return __delip(set,
4421 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4422 + : skb->nh.iph->daddr),
4426 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
4428 + return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
4431 +static int create(struct ip_set *set, const void *data, size_t size)
4434 + struct ip_set_req_macipmap_create *req =
4435 + (struct ip_set_req_macipmap_create *) data;
4436 + struct ip_set_macipmap *map;
4438 + if (size != sizeof(struct ip_set_req_macipmap_create)) {
4439 + ip_set_printk("data length wrong (want %zu, have %zu)",
4440 + sizeof(struct ip_set_req_macipmap_create),
4445 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
4446 + HIPQUAD(req->from), HIPQUAD(req->to));
4448 + if (req->from > req->to) {
4449 + DP("bad ip range");
4453 + if (req->to - req->from > MAX_RANGE) {
4454 + ip_set_printk("range too big (max %d addresses)",
4459 + map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
4461 + DP("out of memory for %d bytes",
4462 + sizeof(struct ip_set_macipmap));
4465 + map->flags = req->flags;
4466 + map->first_ip = req->from;
4467 + map->last_ip = req->to;
4468 + newbytes = members_size(map->first_ip, map->last_ip);
4469 + map->members = ip_set_malloc(newbytes);
4470 + if (!map->members) {
4471 + DP("out of memory for %d bytes", newbytes);
4475 + memset(map->members, 0, newbytes);
4481 +static void destroy(struct ip_set *set)
4483 + struct ip_set_macipmap *map =
4484 + (struct ip_set_macipmap *) set->data;
4486 + ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
4492 +static void flush(struct ip_set *set)
4494 + struct ip_set_macipmap *map =
4495 + (struct ip_set_macipmap *) set->data;
4496 + memset(map->members, 0, members_size(map->first_ip, map->last_ip));
4499 +static void list_header(const struct ip_set *set, void *data)
4501 + struct ip_set_macipmap *map =
4502 + (struct ip_set_macipmap *) set->data;
4503 + struct ip_set_req_macipmap_create *header =
4504 + (struct ip_set_req_macipmap_create *) data;
4506 + DP("list_header %x %x %u", map->first_ip, map->last_ip,
4509 + header->from = map->first_ip;
4510 + header->to = map->last_ip;
4511 + header->flags = map->flags;
4514 +static int list_members_size(const struct ip_set *set)
4516 + struct ip_set_macipmap *map =
4517 + (struct ip_set_macipmap *) set->data;
4519 + return members_size(map->first_ip, map->last_ip);
4522 +static void list_members(const struct ip_set *set, void *data)
4524 + struct ip_set_macipmap *map =
4525 + (struct ip_set_macipmap *) set->data;
4527 + int bytes = members_size(map->first_ip, map->last_ip);
4529 + memcpy(data, map->members, bytes);
4532 +static struct ip_set_type ip_set_macipmap = {
4533 + .typename = SETTYPE_NAME,
4534 + .typecode = IPSET_TYPE_IP,
4535 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4536 + .create = &create,
4537 + .destroy = &destroy,
4539 + .reqsize = sizeof(struct ip_set_req_macipmap),
4541 + .addip_kernel = &addip_kernel,
4543 + .delip_kernel = &delip_kernel,
4544 + .testip = &testip,
4545 + .testip_kernel = &testip_kernel,
4546 + .header_size = sizeof(struct ip_set_req_macipmap_create),
4547 + .list_header = &list_header,
4548 + .list_members_size = &list_members_size,
4549 + .list_members = &list_members,
4550 + .me = THIS_MODULE,
4553 +MODULE_LICENSE("GPL");
4554 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4555 +MODULE_DESCRIPTION("macipmap type of IP sets");
4557 +static int __init init(void)
4559 + init_max_malloc_size();
4560 + return ip_set_register_set_type(&ip_set_macipmap);
4563 +static void __exit fini(void)
4565 + /* FIXME: possible race with ip_set_create() */
4566 + ip_set_unregister_set_type(&ip_set_macipmap);
4571 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/ip_set_nethash.c linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_nethash.c
4572 --- linux-2.6.12.5/net/ipv4/netfilter/ip_set_nethash.c 1970-01-01 01:00:00.000000000 +0100
4573 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_nethash.c 2005-09-20 13:11:38.802385250 +0200
4575 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4577 + * This program is free software; you can redistribute it and/or modify
4578 + * it under the terms of the GNU General Public License version 2 as
4579 + * published by the Free Software Foundation.
4582 +/* Kernel module implementing a cidr nethash set */
4584 +#include <linux/module.h>
4585 +#include <linux/ip.h>
4586 +#include <linux/skbuff.h>
4587 +#include <linux/netfilter_ipv4/ip_tables.h>
4588 +#include <linux/netfilter_ipv4/ip_set.h>
4589 +#include <linux/errno.h>
4590 +#include <asm/uaccess.h>
4591 +#include <asm/bitops.h>
4592 +#include <linux/spinlock.h>
4593 +#include <linux/vmalloc.h>
4594 +#include <linux/random.h>
4596 +#include <net/ip.h>
4598 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4599 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
4600 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
4601 +#include <linux/netfilter_ipv4/ip_set_prime.h>
4603 +static inline __u32
4604 +jhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4606 + return jhash_1word(ip, map->initval);
4609 +static inline __u32
4610 +randhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4612 + return (1 + ip % map->prime);
4615 +static inline __u32
4616 +hash_id_cidr(struct ip_set_nethash *map,
4618 + unsigned char cidr,
4619 + ip_set_ip_t *hash_ip)
4621 + __u32 jhash, randhash, id;
4624 + *hash_ip = pack(ip, cidr);
4625 + jhash = jhash_ip(map, *hash_ip);
4626 + randhash = randhash_ip(map, *hash_ip);
4628 + for (i = 0; i < map->probes; i++) {
4629 + id = (jhash + i * randhash) % map->hashsize;
4630 + DP("hash key: %u", id);
4631 + if (map->members[id] == *hash_ip)
4637 +static inline __u32
4638 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4640 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4641 + __u32 id = UINT_MAX;
4644 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4645 + id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
4646 + if (id != UINT_MAX)
4653 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
4654 + ip_set_ip_t *hash_ip)
4656 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4658 + return (hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
4662 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4664 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
4668 +testip(struct ip_set *set, const void *data, size_t size,
4669 + ip_set_ip_t *hash_ip)
4671 + struct ip_set_req_nethash *req =
4672 + (struct ip_set_req_nethash *) data;
4674 + if (size != sizeof(struct ip_set_req_nethash)) {
4675 + ip_set_printk("data length wrong (want %zu, have %zu)",
4676 + sizeof(struct ip_set_req_nethash),
4680 + return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
4681 + : __testip_cidr(set, req->ip, req->cidr, hash_ip));
4685 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4686 + u_int32_t flags, ip_set_ip_t *hash_ip)
4688 + return __testip(set,
4689 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4690 + : skb->nh.iph->daddr),
4695 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
4697 + __u32 jhash, randhash, probe;
4700 + jhash = jhash_ip(map, ip);
4701 + randhash = randhash_ip(map, ip);
4703 + for (i = 0; i < map->probes; i++) {
4704 + probe = (jhash + i * randhash) % map->hashsize;
4705 + if (map->members[probe] == ip)
4707 + if (!map->members[probe]) {
4708 + map->members[probe] = ip;
4712 + /* Trigger rehashing */
4717 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4718 + ip_set_ip_t *hash_ip)
4720 + *hash_ip = pack(ip, cidr);
4721 + DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
4723 + return __addip_base(map, *hash_ip);
4727 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
4729 + unsigned char next;
4732 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4733 + if (map->cidr[i] == cidr) {
4735 + } else if (map->cidr[i] < cidr) {
4736 + next = map->cidr[i];
4737 + map->cidr[i] = cidr;
4742 + map->cidr[i] = cidr;
4746 +addip(struct ip_set *set, const void *data, size_t size,
4747 + ip_set_ip_t *hash_ip)
4749 + struct ip_set_req_nethash *req =
4750 + (struct ip_set_req_nethash *) data;
4753 + if (size != sizeof(struct ip_set_req_nethash)) {
4754 + ip_set_printk("data length wrong (want %zu, have %zu)",
4755 + sizeof(struct ip_set_req_nethash),
4759 + ret = __addip((struct ip_set_nethash *) set->data,
4760 + req->ip, req->cidr, hash_ip);
4763 + update_cidr_sizes((struct ip_set_nethash *) set->data,
4770 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4771 + u_int32_t flags, ip_set_ip_t *hash_ip)
4773 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4774 + int ret = -ERANGE;
4775 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4776 + : skb->nh.iph->daddr);
4779 + ret = __addip(map, ip, map->cidr[0], hash_ip);
4784 +static int retry(struct ip_set *set)
4786 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4787 + ip_set_ip_t *members;
4788 + u_int32_t i, hashsize;
4789 + unsigned newbytes;
4791 + struct ip_set_nethash tmp = {
4792 + .hashsize = map->hashsize,
4793 + .probes = map->probes,
4794 + .resize = map->resize
4797 + if (map->resize == 0)
4800 + memcpy(tmp.cidr, map->cidr, 30 * sizeof(unsigned char));
4804 + /* Calculate new parameters */
4805 + get_random_bytes(&tmp.initval, 4);
4806 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
4807 + if (hashsize == tmp.hashsize)
4809 + tmp.prime = make_prime(hashsize);
4811 + ip_set_printk("rehashing of set %s triggered: "
4812 + "hashsize grows from %u to %u",
4813 + set->name, tmp.hashsize, hashsize);
4814 + tmp.hashsize = hashsize;
4816 + newbytes = hashsize * sizeof(ip_set_ip_t);
4817 + tmp.members = ip_set_malloc_atomic(newbytes);
4818 + if (!tmp.members) {
4819 + DP("out of memory for %d bytes", newbytes);
4822 + memset(tmp.members, 0, newbytes);
4824 + write_lock_bh(&set->lock);
4825 + map = (struct ip_set_nethash *) set->data; /* Play safe */
4826 + for (i = 0; i < map->hashsize && res == 0; i++) {
4827 + if (map->members[i])
4828 + res = __addip_base(&tmp, map->members[i]);
4831 + /* Failure, try again */
4832 + write_unlock_bh(&set->lock);
4833 + ip_set_free(tmp.members, newbytes);
4837 + /* Success at resizing! */
4838 + members = map->members;
4839 + hashsize = map->hashsize;
4841 + map->initval = tmp.initval;
4842 + map->prime = tmp.prime;
4843 + map->hashsize = tmp.hashsize;
4844 + map->members = tmp.members;
4845 + write_unlock_bh(&set->lock);
4847 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
4853 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4854 + ip_set_ip_t *hash_ip)
4856 + ip_set_ip_t id = hash_id_cidr(map, ip, cidr, hash_ip);
4858 + if (id == UINT_MAX)
4861 + map->members[id] = 0;
4866 +delip(struct ip_set *set, const void *data, size_t size,
4867 + ip_set_ip_t *hash_ip)
4869 + struct ip_set_req_nethash *req =
4870 + (struct ip_set_req_nethash *) data;
4872 + if (size != sizeof(struct ip_set_req_nethash)) {
4873 + ip_set_printk("data length wrong (want %zu, have %zu)",
4874 + sizeof(struct ip_set_req_nethash),
4878 + /* TODO: no garbage collection in map->cidr */
4879 + return __delip((struct ip_set_nethash *) set->data,
4880 + req->ip, req->cidr, hash_ip);
4884 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4885 + u_int32_t flags, ip_set_ip_t *hash_ip)
4887 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4888 + int ret = -ERANGE;
4889 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4890 + : skb->nh.iph->daddr);
4893 + ret = __delip(map, ip, map->cidr[0], hash_ip);
4898 +static int create(struct ip_set *set, const void *data, size_t size)
4900 + unsigned newbytes;
4901 + struct ip_set_req_nethash_create *req =
4902 + (struct ip_set_req_nethash_create *) data;
4903 + struct ip_set_nethash *map;
4905 + if (size != sizeof(struct ip_set_req_nethash_create)) {
4906 + ip_set_printk("data length wrong (want %zu, have %zu)",
4907 + sizeof(struct ip_set_req_nethash_create),
4912 + if (req->hashsize < 1) {
4913 + ip_set_printk("hashsize too small");
4917 + map = kmalloc(sizeof(struct ip_set_nethash), GFP_KERNEL);
4919 + DP("out of memory for %d bytes",
4920 + sizeof(struct ip_set_nethash));
4923 + get_random_bytes(&map->initval, 4);
4924 + map->prime = make_prime(req->hashsize);
4925 + map->hashsize = req->hashsize;
4926 + map->probes = req->probes;
4927 + map->resize = req->resize;
4928 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
4929 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
4930 + map->members = ip_set_malloc(newbytes);
4931 + if (!map->members) {
4932 + DP("out of memory for %d bytes", newbytes);
4936 + memset(map->members, 0, newbytes);
4942 +static void destroy(struct ip_set *set)
4944 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4946 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
4952 +static void flush(struct ip_set *set)
4954 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4955 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
4956 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
4959 +static void list_header(const struct ip_set *set, void *data)
4961 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4962 + struct ip_set_req_nethash_create *header =
4963 + (struct ip_set_req_nethash_create *) data;
4965 + header->hashsize = map->hashsize;
4966 + header->probes = map->probes;
4967 + header->resize = map->resize;
4970 +static int list_members_size(const struct ip_set *set)
4972 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4974 + return (map->hashsize * sizeof(ip_set_ip_t));
4977 +static void list_members(const struct ip_set *set, void *data)
4979 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4980 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
4982 + memcpy(data, map->members, bytes);
4985 +static struct ip_set_type ip_set_nethash = {
4986 + .typename = SETTYPE_NAME,
4987 + .typecode = IPSET_TYPE_IP,
4988 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4989 + .create = &create,
4990 + .destroy = &destroy,
4992 + .reqsize = sizeof(struct ip_set_req_nethash),
4994 + .addip_kernel = &addip_kernel,
4997 + .delip_kernel = &delip_kernel,
4998 + .testip = &testip,
4999 + .testip_kernel = &testip_kernel,
5000 + .header_size = sizeof(struct ip_set_req_nethash_create),
5001 + .list_header = &list_header,
5002 + .list_members_size = &list_members_size,
5003 + .list_members = &list_members,
5004 + .me = THIS_MODULE,
5007 +MODULE_LICENSE("GPL");
5008 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5009 +MODULE_DESCRIPTION("nethash type of IP sets");
5011 +static int __init init(void)
5013 + return ip_set_register_set_type(&ip_set_nethash);
5016 +static void __exit fini(void)
5018 + /* FIXME: possible race with ip_set_create() */
5019 + ip_set_unregister_set_type(&ip_set_nethash);
5024 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/ip_set_portmap.c linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_portmap.c
5025 --- linux-2.6.12.5/net/ipv4/netfilter/ip_set_portmap.c 1970-01-01 01:00:00.000000000 +0100
5026 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/ip_set_portmap.c 2005-09-20 13:11:38.802385250 +0200
5028 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5030 + * This program is free software; you can redistribute it and/or modify
5031 + * it under the terms of the GNU General Public License version 2 as
5032 + * published by the Free Software Foundation.
5035 +/* Kernel module implementing a port set type as a bitmap */
5037 +#include <linux/module.h>
5038 +#include <linux/ip.h>
5039 +#include <linux/tcp.h>
5040 +#include <linux/udp.h>
5041 +#include <linux/skbuff.h>
5042 +#include <linux/netfilter_ipv4/ip_tables.h>
5043 +#include <linux/netfilter_ipv4/ip_set.h>
5044 +#include <linux/errno.h>
5045 +#include <asm/uaccess.h>
5046 +#include <asm/bitops.h>
5047 +#include <linux/spinlock.h>
5049 +#include <net/ip.h>
5051 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
5053 +/* We must handle non-linear skbs */
5054 +static inline ip_set_ip_t
5055 +get_port(const struct sk_buff *skb, u_int32_t flags)
5057 + struct iphdr *iph = skb->nh.iph;
5058 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
5060 + switch (iph->protocol) {
5061 + case IPPROTO_TCP: {
5062 + struct tcphdr tcph;
5064 + /* See comments at tcp_match in ip_tables.c */
5066 + return INVALID_PORT;
5068 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
5069 + /* No choice either */
5070 + return INVALID_PORT;
5072 + return ntohs(flags & IPSET_SRC ?
5073 + tcph.source : tcph.dest);
5075 + case IPPROTO_UDP: {
5076 + struct udphdr udph;
5079 + return INVALID_PORT;
5081 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
5082 + /* No choice either */
5083 + return INVALID_PORT;
5085 + return ntohs(flags & IPSET_SRC ?
5086 + udph.source : udph.dest);
5089 + return INVALID_PORT;
5094 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5096 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5098 + if (port < map->first_port || port > map->last_port)
5101 + *hash_port = port;
5102 + DP("set: %s, port:%u, %u", set->name, port, *hash_port);
5103 + return !!test_bit(port - map->first_port, map->members);
5107 +testport(struct ip_set *set, const void *data, size_t size,
5108 + ip_set_ip_t *hash_port)
5110 + struct ip_set_req_portmap *req =
5111 + (struct ip_set_req_portmap *) data;
5113 + if (size != sizeof(struct ip_set_req_portmap)) {
5114 + ip_set_printk("data length wrong (want %zu, have %zu)",
5115 + sizeof(struct ip_set_req_portmap),
5119 + return __testport(set, req->port, hash_port);
5123 +testport_kernel(struct ip_set *set, const struct sk_buff *skb,
5124 + u_int32_t flags, ip_set_ip_t *hash_port)
5127 + ip_set_ip_t port = get_port(skb, flags);
5129 + DP("flag %s port %u", flags & IPSET_SRC ? "SRC" : "DST", port);
5130 + if (port == INVALID_PORT)
5133 + res = __testport(set, port, hash_port);
5135 + return (res < 0 ? 0 : res);
5139 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5141 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5143 + if (port < map->first_port || port > map->last_port)
5145 + if (test_and_set_bit(port - map->first_port, map->members))
5148 + *hash_port = port;
5149 + DP("port %u", port);
5154 +addport(struct ip_set *set, const void *data, size_t size,
5155 + ip_set_ip_t *hash_port)
5157 + struct ip_set_req_portmap *req =
5158 + (struct ip_set_req_portmap *) data;
5160 + if (size != sizeof(struct ip_set_req_portmap)) {
5161 + ip_set_printk("data length wrong (want %zu, have %zu)",
5162 + sizeof(struct ip_set_req_portmap),
5166 + return __addport(set, req->port, hash_port);
5170 +addport_kernel(struct ip_set *set, const struct sk_buff *skb,
5171 + u_int32_t flags, ip_set_ip_t *hash_port)
5173 + ip_set_ip_t port = get_port(skb, flags);
5175 + if (port == INVALID_PORT)
5178 + return __addport(set, port, hash_port);
5182 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5184 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5186 + if (port < map->first_port || port > map->last_port)
5188 + if (!test_and_clear_bit(port - map->first_port, map->members))
5191 + *hash_port = port;
5192 + DP("port %u", port);
5197 +delport(struct ip_set *set, const void *data, size_t size,
5198 + ip_set_ip_t *hash_port)
5200 + struct ip_set_req_portmap *req =
5201 + (struct ip_set_req_portmap *) data;
5203 + if (size != sizeof(struct ip_set_req_portmap)) {
5204 + ip_set_printk("data length wrong (want %zu, have %zu)",
5205 + sizeof(struct ip_set_req_portmap),
5209 + return __delport(set, req->port, hash_port);
5213 +delport_kernel(struct ip_set *set, const struct sk_buff *skb,
5214 + u_int32_t flags, ip_set_ip_t *hash_port)
5216 + ip_set_ip_t port = get_port(skb, flags);
5218 + if (port == INVALID_PORT)
5221 + return __delport(set, port, hash_port);
5224 +static int create(struct ip_set *set, const void *data, size_t size)
5227 + struct ip_set_req_portmap_create *req =
5228 + (struct ip_set_req_portmap_create *) data;
5229 + struct ip_set_portmap *map;
5231 + if (size != sizeof(struct ip_set_req_portmap_create)) {
5232 + ip_set_printk("data length wrong (want %zu, have %zu)",
5233 + sizeof(struct ip_set_req_portmap_create),
5238 + DP("from %u to %u", req->from, req->to);
5240 + if (req->from > req->to) {
5241 + DP("bad port range");
5245 + if (req->to - req->from > MAX_RANGE) {
5246 + ip_set_printk("range too big (max %d ports)",
5251 + map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
5253 + DP("out of memory for %d bytes",
5254 + sizeof(struct ip_set_portmap));
5257 + map->first_port = req->from;
5258 + map->last_port = req->to;
5259 + newbytes = bitmap_bytes(req->from, req->to);
5260 + map->members = kmalloc(newbytes, GFP_KERNEL);
5261 + if (!map->members) {
5262 + DP("out of memory for %d bytes", newbytes);
5266 + memset(map->members, 0, newbytes);
5272 +static void destroy(struct ip_set *set)
5274 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5276 + kfree(map->members);
5282 +static void flush(struct ip_set *set)
5284 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5285 + memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
5288 +static void list_header(const struct ip_set *set, void *data)
5290 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5291 + struct ip_set_req_portmap_create *header =
5292 + (struct ip_set_req_portmap_create *) data;
5294 + DP("list_header %u %u", map->first_port, map->last_port);
5296 + header->from = map->first_port;
5297 + header->to = map->last_port;
5300 +static int list_members_size(const struct ip_set *set)
5302 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5304 + return bitmap_bytes(map->first_port, map->last_port);
5307 +static void list_members(const struct ip_set *set, void *data)
5309 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5310 + int bytes = bitmap_bytes(map->first_port, map->last_port);
5312 + memcpy(data, map->members, bytes);
5315 +static struct ip_set_type ip_set_portmap = {
5316 + .typename = SETTYPE_NAME,
5317 + .typecode = IPSET_TYPE_PORT,
5318 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5319 + .create = &create,
5320 + .destroy = &destroy,
5322 + .reqsize = sizeof(struct ip_set_req_portmap),
5323 + .addip = &addport,
5324 + .addip_kernel = &addport_kernel,
5325 + .delip = &delport,
5326 + .delip_kernel = &delport_kernel,
5327 + .testip = &testport,
5328 + .testip_kernel = &testport_kernel,
5329 + .header_size = sizeof(struct ip_set_req_portmap_create),
5330 + .list_header = &list_header,
5331 + .list_members_size = &list_members_size,
5332 + .list_members = &list_members,
5333 + .me = THIS_MODULE,
5336 +MODULE_LICENSE("GPL");
5337 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5338 +MODULE_DESCRIPTION("portmap type of IP sets");
5340 +static int __init init(void)
5342 + return ip_set_register_set_type(&ip_set_portmap);
5345 +static void __exit fini(void)
5347 + /* FIXME: possible race with ip_set_create() */
5348 + ip_set_unregister_set_type(&ip_set_portmap);
5353 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/ipt_set.c linux-2.6.12.5-ipset/net/ipv4/netfilter/ipt_set.c
5354 --- linux-2.6.12.5/net/ipv4/netfilter/ipt_set.c 1970-01-01 01:00:00.000000000 +0100
5355 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/ipt_set.c 2005-09-20 13:11:38.802385250 +0200
5357 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5358 + * Patrick Schaaf <bof@bof.de>
5359 + * Martin Josefsson <gandalf@wlug.westbo.se>
5360 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5362 + * This program is free software; you can redistribute it and/or modify
5363 + * it under the terms of the GNU General Public License version 2 as
5364 + * published by the Free Software Foundation.
5367 +/* Kernel module to match an IP set. */
5369 +#include <linux/module.h>
5370 +#include <linux/ip.h>
5371 +#include <linux/skbuff.h>
5373 +#include <linux/netfilter_ipv4/ip_tables.h>
5374 +#include <linux/netfilter_ipv4/ip_set.h>
5375 +#include <linux/netfilter_ipv4/ipt_set.h>
5378 +match_set(const struct ipt_set_info *info,
5379 + const struct sk_buff *skb,
5382 + if (ip_set_testip_kernel(info->index, skb, info->flags))
5388 +match(const struct sk_buff *skb,
5389 + const struct net_device *in,
5390 + const struct net_device *out,
5391 + const void *matchinfo,
5395 + const struct ipt_set_info_match *info = matchinfo;
5397 + return match_set(&info->match_set,
5399 + info->match_set.flags[0] & IPSET_MATCH_INV);
5403 +checkentry(const char *tablename,
5404 + const struct ipt_ip *ip,
5406 + unsigned int matchsize,
5407 + unsigned int hook_mask)
5409 + struct ipt_set_info_match *info =
5410 + (struct ipt_set_info_match *) matchinfo;
5411 + ip_set_id_t index;
5413 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5414 + ip_set_printk("invalid matchsize %d", matchsize);
5418 + index = ip_set_get_byindex(info->match_set.index);
5420 + if (index == IP_SET_INVALID_ID) {
5421 + ip_set_printk("Cannot find set indentified by id %u to match",
5422 + info->match_set.index);
5423 + return 0; /* error */
5425 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5426 + ip_set_printk("That's nasty!");
5427 + return 0; /* error */
5433 +static void destroy(void *matchinfo, unsigned int matchsize)
5435 + struct ipt_set_info_match *info = matchinfo;
5437 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5438 + ip_set_printk("invalid matchsize %d", matchsize);
5442 + ip_set_put(info->match_set.index);
5445 +static struct ipt_match set_match = {
5448 + .checkentry = &checkentry,
5449 + .destroy = &destroy,
5453 +MODULE_LICENSE("GPL");
5454 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5455 +MODULE_DESCRIPTION("iptables IP set match module");
5457 +static int __init init(void)
5459 + return ipt_register_match(&set_match);
5462 +static void __exit fini(void)
5464 + ipt_unregister_match(&set_match);
5469 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/ipt_SET.c linux-2.6.12.5-ipset/net/ipv4/netfilter/ipt_SET.c
5470 --- linux-2.6.12.5/net/ipv4/netfilter/ipt_SET.c 1970-01-01 01:00:00.000000000 +0100
5471 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/ipt_SET.c 2005-09-20 13:11:38.802385250 +0200
5473 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5474 + * Patrick Schaaf <bof@bof.de>
5475 + * Martin Josefsson <gandalf@wlug.westbo.se>
5476 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5478 + * This program is free software; you can redistribute it and/or modify
5479 + * it under the terms of the GNU General Public License version 2 as
5480 + * published by the Free Software Foundation.
5483 +/* ipt_SET.c - netfilter target to manipulate IP sets */
5485 +#include <linux/types.h>
5486 +#include <linux/ip.h>
5487 +#include <linux/timer.h>
5488 +#include <linux/module.h>
5489 +#include <linux/netfilter.h>
5490 +#include <linux/netdevice.h>
5491 +#include <linux/if.h>
5492 +#include <linux/inetdevice.h>
5493 +#include <net/protocol.h>
5494 +#include <net/checksum.h>
5495 +#include <linux/netfilter_ipv4.h>
5496 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
5497 +#include <linux/netfilter_ipv4/ipt_set.h>
5499 +static unsigned int
5500 +target(struct sk_buff **pskb,
5501 + const struct net_device *in,
5502 + const struct net_device *out,
5503 + unsigned int hooknum,
5504 + const void *targinfo,
5507 + const struct ipt_set_info_target *info = targinfo;
5509 + if (info->add_set.index != IP_SET_INVALID_ID)
5510 + ip_set_addip_kernel(info->add_set.index,
5512 + info->add_set.flags);
5513 + if (info->del_set.index != IP_SET_INVALID_ID)
5514 + ip_set_delip_kernel(info->del_set.index,
5516 + info->del_set.flags);
5518 + return IPT_CONTINUE;
5522 +checkentry(const char *tablename,
5523 + const struct ipt_entry *e,
5525 + unsigned int targinfosize, unsigned int hook_mask)
5527 + struct ipt_set_info_target *info =
5528 + (struct ipt_set_info_target *) targinfo;
5529 + ip_set_id_t index;
5531 + if (targinfosize != IPT_ALIGN(sizeof(*info))) {
5532 + DP("bad target info size %u", targinfosize);
5536 + if (info->add_set.index != IP_SET_INVALID_ID) {
5537 + index = ip_set_get_byindex(info->add_set.index);
5538 + if (index == IP_SET_INVALID_ID) {
5539 + ip_set_printk("cannot find add_set index %u as target",
5540 + info->add_set.index);
5541 + return 0; /* error */
5545 + if (info->del_set.index != IP_SET_INVALID_ID) {
5546 + index = ip_set_get_byindex(info->del_set.index);
5547 + if (index == IP_SET_INVALID_ID) {
5548 + ip_set_printk("cannot find del_set index %u as target",
5549 + info->del_set.index);
5550 + return 0; /* error */
5553 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
5554 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5555 + ip_set_printk("That's nasty!");
5556 + return 0; /* error */
5562 +static void destroy(void *targetinfo, unsigned int targetsize)
5564 + struct ipt_set_info_target *info = targetinfo;
5566 + if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
5567 + ip_set_printk("invalid targetsize %d", targetsize);
5571 + if (info->add_set.index != IP_SET_INVALID_ID)
5572 + ip_set_put(info->add_set.index);
5573 + if (info->del_set.index != IP_SET_INVALID_ID)
5574 + ip_set_put(info->del_set.index);
5577 +static struct ipt_target SET_target = {
5580 + .checkentry = checkentry,
5581 + .destroy = destroy,
5585 +MODULE_LICENSE("GPL");
5586 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5587 +MODULE_DESCRIPTION("iptables IP set target module");
5589 +static int __init init(void)
5591 + return ipt_register_target(&SET_target);
5594 +static void __exit fini(void)
5596 + ipt_unregister_target(&SET_target);
5601 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/Kconfig linux-2.6.12.5-ipset/net/ipv4/netfilter/Kconfig
5602 --- linux-2.6.12.5/net/ipv4/netfilter/Kconfig 2005-08-15 02:20:18.000000000 +0200
5603 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/Kconfig 2005-09-20 13:11:38.802385250 +0200
5604 @@ -692,5 +692,106 @@
5605 Allows altering the ARP packet payload: source and destination
5606 hardware and network addresses.
5609 + tristate "IP set support"
5610 + depends on INET && NETFILTER
5612 + This option adds IP set support to the kernel.
5613 + In order to define and use sets, you need the userspace utility
5616 + To compile it as a module, choose M here. If unsure, say N.
5618 +config IP_NF_SET_MAX
5619 + int "Maximum number of IP sets"
5622 + depends on IP_NF_SET
5624 + You can define here default value of the maximum number
5625 + of IP sets for the kernel.
5627 + The value can be overriden by the 'max_sets' module
5628 + parameter of the 'ip_set' module.
5630 +config IP_NF_SET_HASHSIZE
5631 + int "Hash size for bindings of IP sets"
5633 + depends on IP_NF_SET
5635 + You can define here default value of the hash size for
5636 + bindings of IP sets.
5638 + The value can be overriden by the 'hash_size' module
5639 + parameter of the 'ip_set' module.
5641 +config IP_NF_SET_IPMAP
5642 + tristate "ipmap set support"
5643 + depends on IP_NF_SET
5645 + This option adds the ipmap set type support.
5647 + To compile it as a module, choose M here. If unsure, say N.
5649 +config IP_NF_SET_MACIPMAP
5650 + tristate "macipmap set support"
5651 + depends on IP_NF_SET
5653 + This option adds the macipmap set type support.
5655 + To compile it as a module, choose M here. If unsure, say N.
5657 +config IP_NF_SET_PORTMAP
5658 + tristate "portmap set support"
5659 + depends on IP_NF_SET
5661 + This option adds the portmap set type support.
5663 + To compile it as a module, choose M here. If unsure, say N.
5665 +config IP_NF_SET_IPHASH
5666 + tristate "iphash set support"
5667 + depends on IP_NF_SET
5669 + This option adds the iphash set type support.
5671 + To compile it as a module, choose M here. If unsure, say N.
5673 +config IP_NF_SET_NETHASH
5674 + tristate "nethash set support"
5675 + depends on IP_NF_SET
5677 + This option adds the nethash set type support.
5679 + To compile it as a module, choose M here. If unsure, say N.
5681 +config IP_NF_SET_IPTREE
5682 + tristate "iptree set support"
5683 + depends on IP_NF_SET
5685 + This option adds the iptree set type support.
5687 + To compile it as a module, choose M here. If unsure, say N.
5689 +config IP_NF_MATCH_SET
5690 + tristate "set match support"
5691 + depends on IP_NF_SET
5693 + Set matching matches against given IP sets.
5694 + You need the ipset utility to create and set up the sets.
5696 + To compile it as a module, choose M here. If unsure, say N.
5698 +config IP_NF_TARGET_SET
5699 + tristate "SET target support"
5700 + depends on IP_NF_SET
5702 + The SET target makes possible to add/delete entries
5704 + You need the ipset utility to create and set up the sets.
5706 + To compile it as a module, choose M here. If unsure, say N.
5711 diff -Nur linux-2.6.12.5/net/ipv4/netfilter/Makefile linux-2.6.12.5-ipset/net/ipv4/netfilter/Makefile
5712 --- linux-2.6.12.5/net/ipv4/netfilter/Makefile 2005-08-15 02:20:18.000000000 +0200
5713 +++ linux-2.6.12.5-ipset/net/ipv4/netfilter/Makefile 2005-09-20 13:11:38.818386250 +0200
5715 obj-$(CONFIG_IP_NF_MATCH_HASHLIMIT) += ipt_hashlimit.o
5716 obj-$(CONFIG_IP_NF_MATCH_SCTP) += ipt_sctp.o
5717 obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
5718 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
5719 obj-$(CONFIG_IP_NF_MATCH_MAC) += ipt_mac.o
5720 obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o
5721 obj-$(CONFIG_IP_NF_MATCH_PKTTYPE) += ipt_pkttype.o
5723 obj-$(CONFIG_IP_NF_TARGET_ULOG) += ipt_ULOG.o
5724 obj-$(CONFIG_IP_NF_TARGET_TCPMSS) += ipt_TCPMSS.o
5725 obj-$(CONFIG_IP_NF_TARGET_NOTRACK) += ipt_NOTRACK.o
5726 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
5729 +obj-$(CONFIG_IP_NF_SET) += ip_set.o
5730 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
5731 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
5732 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
5733 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
5734 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
5735 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
5736 obj-$(CONFIG_IP_NF_TARGET_CLUSTERIP) += ipt_CLUSTERIP.o
5738 # generic ARP tables