1 diff -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set.h
2 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_iphash.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_iphash.h
495 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_iphash.h 1970-01-01 01:00:00.000000000 +0100
496 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_iphash.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_ipmap.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_ipmap.h
529 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_ipmap.h 1970-01-01 01:00:00.000000000 +0100
530 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_ipmap.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_iptree.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_iptree.h
589 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_iptree.h 1970-01-01 01:00:00.000000000 +0100
590 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_iptree.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_jhash.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_jhash.h
632 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_jhash.h 1970-01-01 01:00:00.000000000 +0100
633 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_jhash.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_macipmap.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_macipmap.h
784 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_macipmap.h 1970-01-01 01:00:00.000000000 +0100
785 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_macipmap.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_malloc.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_malloc.h
826 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_malloc.h 1970-01-01 01:00:00.000000000 +0100
827 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_malloc.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_nethash.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_nethash.h
872 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_nethash.h 1970-01-01 01:00:00.000000000 +0100
873 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_nethash.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_portmap.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_portmap.h
931 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_portmap.h 1970-01-01 01:00:00.000000000 +0100
932 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_portmap.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_prime.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_prime.h
960 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ip_set_prime.h 1970-01-01 01:00:00.000000000 +0100
961 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ip_set_prime.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/ipt_set.h linux-2.6.19.dev/include/linux/netfilter_ipv4/ipt_set.h
998 --- linux-2.6.19.old/include/linux/netfilter_ipv4/ipt_set.h 1970-01-01 01:00:00.000000000 +0100
999 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/ipt_set.h 2006-12-14 03:13:43.000000000 +0100
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 -urN linux-2.6.19.old/include/linux/netfilter_ipv4/listhelp.h linux-2.6.19.dev/include/linux/netfilter_ipv4/listhelp.h
1023 --- linux-2.6.19.old/include/linux/netfilter_ipv4/listhelp.h 1970-01-01 01:00:00.000000000 +0100
1024 +++ linux-2.6.19.dev/include/linux/netfilter_ipv4/listhelp.h 2006-12-14 03:13:43.000000000 +0100
1026 +#ifndef _LISTHELP_H
1027 +#define _LISTHELP_H
1028 +#include <linux/list.h>
1030 +/* Header to do more comprehensive job than linux/list.h; assume list
1031 + is first entry in structure. */
1033 +/* Return pointer to first true entry, if any, or NULL. A macro
1034 + required to allow inlining of cmpfn. */
1035 +#define LIST_FIND(head, cmpfn, type, args...) \
1037 + const struct list_head *__i, *__j = NULL; \
1039 + ASSERT_READ_LOCK(head); \
1040 + list_for_each(__i, (head)) \
1041 + if (cmpfn((const type)__i , ## args)) { \
1048 +#define LIST_FIND_W(head, cmpfn, type, args...) \
1050 + const struct list_head *__i, *__j = NULL; \
1052 + ASSERT_WRITE_LOCK(head); \
1053 + list_for_each(__i, (head)) \
1054 + if (cmpfn((type)__i , ## args)) { \
1061 +/* Just like LIST_FIND but we search backwards */
1062 +#define LIST_FIND_B(head, cmpfn, type, args...) \
1064 + const struct list_head *__i, *__j = NULL; \
1066 + ASSERT_READ_LOCK(head); \
1067 + list_for_each_prev(__i, (head)) \
1068 + if (cmpfn((const type)__i , ## args)) { \
1076 +__list_cmp_same(const void *p1, const void *p2) { return p1 == p2; }
1078 +/* Is this entry in the list? */
1080 +list_inlist(struct list_head *head, const void *entry)
1082 + return LIST_FIND(head, __list_cmp_same, void *, entry) != NULL;
1085 +/* Delete from list. */
1086 +#ifdef CONFIG_NETFILTER_DEBUG
1087 +#define LIST_DELETE(head, oldentry) \
1089 + ASSERT_WRITE_LOCK(head); \
1090 + if (!list_inlist(head, oldentry)) \
1091 + printk("LIST_DELETE: %s:%u `%s'(%p) not in %s.\n", \
1092 + __FILE__, __LINE__, #oldentry, oldentry, #head); \
1093 + else list_del((struct list_head *)oldentry); \
1096 +#define LIST_DELETE(head, oldentry) list_del((struct list_head *)oldentry)
1101 +list_append(struct list_head *head, void *new)
1103 + ASSERT_WRITE_LOCK(head);
1104 + list_add((new), (head)->prev);
1109 +list_prepend(struct list_head *head, void *new)
1111 + ASSERT_WRITE_LOCK(head);
1112 + list_add(new, head);
1115 +/* Insert according to ordering function; insert before first true. */
1116 +#define LIST_INSERT(head, new, cmpfn) \
1118 + struct list_head *__i; \
1119 + ASSERT_WRITE_LOCK(head); \
1120 + list_for_each(__i, (head)) \
1121 + if ((new), (typeof (new))__i) \
1123 + list_add((struct list_head *)(new), __i->prev); \
1126 +/* If the field after the list_head is a nul-terminated string, you
1127 + can use these functions. */
1128 +static inline int __list_cmp_name(const void *i, const char *name)
1130 + return strcmp(name, i+sizeof(struct list_head)) == 0;
1133 +/* Returns false if same name already in list, otherwise does insert. */
1135 +list_named_insert(struct list_head *head, void *new)
1137 + if (LIST_FIND(head, __list_cmp_name, void *,
1138 + new + sizeof(struct list_head)))
1140 + list_prepend(head, new);
1144 +/* Find this named element in the list. */
1145 +#define list_named_find(head, name) \
1146 +LIST_FIND(head, __list_cmp_name, void *, name)
1148 +#endif /*_LISTHELP_H*/
1149 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ip_set.c linux-2.6.19.dev/net/ipv4/netfilter/ip_set.c
1150 --- linux-2.6.19.old/net/ipv4/netfilter/ip_set.c 1970-01-01 01:00:00.000000000 +0100
1151 +++ linux-2.6.19.dev/net/ipv4/netfilter/ip_set.c 2006-12-14 03:13:43.000000000 +0100
1153 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1154 + * Patrick Schaaf <bof@bof.de>
1155 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1157 + * This program is free software; you can redistribute it and/or modify
1158 + * it under the terms of the GNU General Public License version 2 as
1159 + * published by the Free Software Foundation.
1162 +/* Kernel module for IP set management */
1164 +#include <linux/autoconf.h>
1165 +#include <linux/module.h>
1166 +#include <linux/moduleparam.h>
1167 +#include <linux/kmod.h>
1168 +#include <linux/ip.h>
1169 +#include <linux/skbuff.h>
1170 +#include <linux/random.h>
1171 +#include <linux/jhash.h>
1172 +#include <linux/netfilter_ipv4/ip_tables.h>
1173 +#include <linux/errno.h>
1174 +#include <asm/uaccess.h>
1175 +#include <asm/bitops.h>
1176 +#include <asm/semaphore.h>
1177 +#include <linux/spinlock.h>
1178 +#include <linux/vmalloc.h>
1180 +#define ASSERT_READ_LOCK(x) /* dont use that */
1181 +#define ASSERT_WRITE_LOCK(x)
1182 +#include <linux/netfilter_ipv4/listhelp.h>
1183 +#include <linux/netfilter_ipv4/ip_set.h>
1185 +static struct list_head set_type_list; /* all registered sets */
1186 +static struct ip_set **ip_set_list; /* all individual sets */
1187 +static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1188 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1189 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1190 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1191 +static struct list_head *ip_set_hash; /* hash of bindings */
1192 +static unsigned int ip_set_hash_random; /* random seed */
1195 + * Sets are identified either by the index in ip_set_list or by id.
1196 + * The id never changes and is used to find a key in the hash.
1197 + * The index may change by swapping and used at all other places
1198 + * (set/SET netfilter modules, binding value, etc.)
1200 + * Userspace requests are serialized by ip_set_mutex and sets can
1201 + * be deleted only from userspace. Therefore ip_set_list locking
1202 + * must obey the following rules:
1204 + * - kernel requests: read and write locking mandatory
1205 + * - user requests: read locking optional, write locking mandatory
1209 +__ip_set_get(ip_set_id_t index)
1211 + atomic_inc(&ip_set_list[index]->ref);
1215 +__ip_set_put(ip_set_id_t index)
1217 + atomic_dec(&ip_set_list[index]->ref);
1221 + * Binding routines
1225 +ip_hash_cmp(const struct ip_set_hash *set_hash,
1226 + ip_set_id_t id, ip_set_ip_t ip)
1228 + return set_hash->id == id && set_hash->ip == ip;
1232 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1234 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1235 + % ip_set_bindings_hash_size;
1236 + struct ip_set_hash *set_hash;
1238 + ASSERT_READ_LOCK(&ip_set_lock);
1239 + IP_SET_ASSERT(ip_set_list[id]);
1240 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1242 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1243 + struct ip_set_hash *, id, ip);
1245 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1247 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1249 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1253 +__set_hash_del(struct ip_set_hash *set_hash)
1255 + ASSERT_WRITE_LOCK(&ip_set_lock);
1256 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1258 + __ip_set_put(set_hash->binding);
1259 + list_del(&set_hash->list);
1264 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1266 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1267 + % ip_set_bindings_hash_size;
1268 + struct ip_set_hash *set_hash;
1270 + IP_SET_ASSERT(ip_set_list[id]);
1271 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1272 + write_lock_bh(&ip_set_lock);
1273 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1274 + struct ip_set_hash *, id, ip);
1275 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1277 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1279 + if (set_hash != NULL)
1280 + __set_hash_del(set_hash);
1281 + write_unlock_bh(&ip_set_lock);
1286 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1288 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1289 + % ip_set_bindings_hash_size;
1290 + struct ip_set_hash *set_hash;
1293 + IP_SET_ASSERT(ip_set_list[id]);
1294 + IP_SET_ASSERT(ip_set_list[binding]);
1295 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1296 + HIPQUAD(ip), ip_set_list[binding]->name);
1297 + write_lock_bh(&ip_set_lock);
1298 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1299 + struct ip_set_hash *, id, ip);
1301 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_KERNEL);
1306 + INIT_LIST_HEAD(&set_hash->list);
1307 + set_hash->id = id;
1308 + set_hash->ip = ip;
1309 + list_add(&ip_set_hash[key], &set_hash->list);
1311 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1312 + DP("overwrite binding: %s",
1313 + ip_set_list[set_hash->binding]->name);
1314 + __ip_set_put(set_hash->binding);
1316 + set_hash->binding = binding;
1317 + __ip_set_get(set_hash->binding);
1319 + write_unlock_bh(&ip_set_lock);
1323 +#define FOREACH_HASH_DO(fn, args...) \
1325 + ip_set_id_t __key; \
1326 + struct ip_set_hash *__set_hash; \
1328 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1329 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1330 + fn(__set_hash , ## args); \
1334 +#define FOREACH_HASH_RW_DO(fn, args...) \
1336 + ip_set_id_t __key; \
1337 + struct ip_set_hash *__set_hash, *__n; \
1339 + ASSERT_WRITE_LOCK(&ip_set_lock); \
1340 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1341 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1342 + fn(__set_hash , ## args); \
1346 +/* Add, del and test set entries from kernel */
1348 +#define follow_bindings(index, set, ip) \
1349 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1350 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1353 +ip_set_testip_kernel(ip_set_id_t index,
1354 + const struct sk_buff *skb,
1355 + const u_int32_t *flags)
1357 + struct ip_set *set;
1361 + IP_SET_ASSERT(flags[i]);
1362 + read_lock_bh(&ip_set_lock);
1364 + set = ip_set_list[index];
1365 + IP_SET_ASSERT(set);
1366 + DP("set %s, index %u", set->name, index);
1367 + read_lock_bh(&set->lock);
1368 + res = set->type->testip_kernel(set, skb, flags[i], &ip);
1369 + read_unlock_bh(&set->lock);
1372 + && follow_bindings(index, set, ip));
1373 + read_unlock_bh(&ip_set_lock);
1379 +ip_set_addip_kernel(ip_set_id_t index,
1380 + const struct sk_buff *skb,
1381 + const u_int32_t *flags)
1383 + struct ip_set *set;
1387 + IP_SET_ASSERT(flags[i]);
1389 + read_lock_bh(&ip_set_lock);
1391 + set = ip_set_list[index];
1392 + IP_SET_ASSERT(set);
1393 + DP("set %s, index %u", set->name, index);
1394 + write_lock_bh(&set->lock);
1395 + res = set->type->addip_kernel(set, skb, flags[i], &ip);
1396 + write_unlock_bh(&set->lock);
1397 + } while ((res == 0 || res == -EEXIST)
1399 + && follow_bindings(index, set, ip));
1400 + read_unlock_bh(&ip_set_lock);
1402 + if (res == -EAGAIN
1403 + && set->type->retry
1404 + && (res = set->type->retry(set)) == 0)
1409 +ip_set_delip_kernel(ip_set_id_t index,
1410 + const struct sk_buff *skb,
1411 + const u_int32_t *flags)
1413 + struct ip_set *set;
1417 + IP_SET_ASSERT(flags[i]);
1418 + read_lock_bh(&ip_set_lock);
1420 + set = ip_set_list[index];
1421 + IP_SET_ASSERT(set);
1422 + DP("set %s, index %u", set->name, index);
1423 + write_lock_bh(&set->lock);
1424 + res = set->type->delip_kernel(set, skb, flags[i], &ip);
1425 + write_unlock_bh(&set->lock);
1426 + } while ((res == 0 || res == -EEXIST)
1428 + && follow_bindings(index, set, ip));
1429 + read_unlock_bh(&ip_set_lock);
1432 +/* Register and deregister settype */
1435 +set_type_equal(const struct ip_set_type *set_type, const char *str2)
1437 + return !strncmp(set_type->typename, str2, IP_SET_MAXNAMELEN - 1);
1440 +static inline struct ip_set_type *
1441 +find_set_type(const char *name)
1443 + return LIST_FIND(&set_type_list,
1445 + struct ip_set_type *,
1450 +ip_set_register_set_type(struct ip_set_type *set_type)
1454 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1455 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1456 + set_type->typename,
1457 + set_type->protocol_version,
1458 + IP_SET_PROTOCOL_VERSION);
1462 + write_lock_bh(&ip_set_lock);
1463 + if (find_set_type(set_type->typename)) {
1465 + ip_set_printk("'%s' already registered!",
1466 + set_type->typename);
1470 + if (!try_module_get(THIS_MODULE)) {
1474 + list_append(&set_type_list, set_type);
1475 + DP("'%s' registered.", set_type->typename);
1477 + write_unlock_bh(&ip_set_lock);
1482 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1484 + write_lock_bh(&ip_set_lock);
1485 + if (!find_set_type(set_type->typename)) {
1486 + ip_set_printk("'%s' not registered?",
1487 + set_type->typename);
1490 + LIST_DELETE(&set_type_list, set_type);
1491 + module_put(THIS_MODULE);
1492 + DP("'%s' unregistered.", set_type->typename);
1494 + write_unlock_bh(&ip_set_lock);
1499 + * Userspace routines
1503 + * Find set by name, reference it once. The reference makes sure the
1504 + * thing pointed to, does not go away under our feet. Drop the reference
1505 + * later, using ip_set_put().
1508 +ip_set_get_byname(const char *name)
1510 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1512 + down(&ip_set_app_mutex);
1513 + for (i = 0; i < ip_set_max; i++) {
1514 + if (ip_set_list[i] != NULL
1515 + && strcmp(ip_set_list[i]->name, name) == 0) {
1521 + up(&ip_set_app_mutex);
1526 + * Find set by index, reference it once. The reference makes sure the
1527 + * thing pointed to, does not go away under our feet. Drop the reference
1528 + * later, using ip_set_put().
1531 +ip_set_get_byindex(ip_set_id_t index)
1533 + down(&ip_set_app_mutex);
1535 + if (index >= ip_set_max)
1536 + return IP_SET_INVALID_ID;
1538 + if (ip_set_list[index])
1539 + __ip_set_get(index);
1541 + index = IP_SET_INVALID_ID;
1543 + up(&ip_set_app_mutex);
1548 + * If the given set pointer points to a valid set, decrement
1549 + * reference count by 1. The caller shall not assume the index
1550 + * to be valid, after calling this function.
1552 +void ip_set_put(ip_set_id_t index)
1554 + down(&ip_set_app_mutex);
1555 + if (ip_set_list[index])
1556 + __ip_set_put(index);
1557 + up(&ip_set_app_mutex);
1560 +/* Find a set by name or index */
1562 +ip_set_find_byname(const char *name)
1564 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1566 + for (i = 0; i < ip_set_max; i++) {
1567 + if (ip_set_list[i] != NULL
1568 + && strcmp(ip_set_list[i]->name, name) == 0) {
1577 +ip_set_find_byindex(ip_set_id_t index)
1579 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1580 + index = IP_SET_INVALID_ID;
1586 + * Add, del, test, bind and unbind
1590 +__ip_set_testip(struct ip_set *set,
1597 + read_lock_bh(&set->lock);
1598 + res = set->type->testip(set, data, size, ip);
1599 + read_unlock_bh(&set->lock);
1605 +__ip_set_addip(ip_set_id_t index,
1609 + struct ip_set *set = ip_set_list[index];
1613 + IP_SET_ASSERT(set);
1615 + write_lock_bh(&set->lock);
1616 + res = set->type->addip(set, data, size, &ip);
1617 + write_unlock_bh(&set->lock);
1618 + } while (res == -EAGAIN
1619 + && set->type->retry
1620 + && (res = set->type->retry(set)) == 0);
1626 +ip_set_addip(ip_set_id_t index,
1631 + return __ip_set_addip(index,
1632 + data + sizeof(struct ip_set_req_adt),
1633 + size - sizeof(struct ip_set_req_adt));
1637 +ip_set_delip(ip_set_id_t index,
1641 + struct ip_set *set = ip_set_list[index];
1645 + IP_SET_ASSERT(set);
1646 + write_lock_bh(&set->lock);
1647 + res = set->type->delip(set,
1648 + data + sizeof(struct ip_set_req_adt),
1649 + size - sizeof(struct ip_set_req_adt),
1651 + write_unlock_bh(&set->lock);
1657 +ip_set_testip(ip_set_id_t index,
1661 + struct ip_set *set = ip_set_list[index];
1665 + IP_SET_ASSERT(set);
1666 + res = __ip_set_testip(set,
1667 + data + sizeof(struct ip_set_req_adt),
1668 + size - sizeof(struct ip_set_req_adt),
1671 + return (res > 0 ? -EEXIST : res);
1675 +ip_set_bindip(ip_set_id_t index,
1679 + struct ip_set *set = ip_set_list[index];
1680 + struct ip_set_req_bind *req_bind;
1681 + ip_set_id_t binding;
1685 + IP_SET_ASSERT(set);
1686 + if (size < sizeof(struct ip_set_req_bind))
1689 + req_bind = (struct ip_set_req_bind *) data;
1690 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1692 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1693 + /* Default binding of a set */
1694 + char *binding_name;
1696 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1699 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1700 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1702 + binding = ip_set_find_byname(binding_name);
1703 + if (binding == IP_SET_INVALID_ID)
1706 + write_lock_bh(&ip_set_lock);
1707 + /* Sets as binding values are referenced */
1708 + if (set->binding != IP_SET_INVALID_ID)
1709 + __ip_set_put(set->binding);
1710 + set->binding = binding;
1711 + __ip_set_get(set->binding);
1712 + write_unlock_bh(&ip_set_lock);
1716 + binding = ip_set_find_byname(req_bind->binding);
1717 + if (binding == IP_SET_INVALID_ID)
1720 + res = __ip_set_testip(set,
1721 + data + sizeof(struct ip_set_req_bind),
1722 + size - sizeof(struct ip_set_req_bind),
1724 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1725 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1728 + res = ip_set_hash_add(set->id, ip, binding);
1733 +#define FOREACH_SET_DO(fn, args...) \
1735 + ip_set_id_t __i; \
1736 + struct ip_set *__set; \
1738 + for (__i = 0; __i < ip_set_max; __i++) { \
1739 + __set = ip_set_list[__i]; \
1740 + if (__set != NULL) \
1741 + fn(__set , ##args); \
1746 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1748 + if (set_hash->id == id)
1749 + __set_hash_del(set_hash);
1753 +__unbind_default(struct ip_set *set)
1755 + if (set->binding != IP_SET_INVALID_ID) {
1756 + /* Sets as binding values are referenced */
1757 + __ip_set_put(set->binding);
1758 + set->binding = IP_SET_INVALID_ID;
1763 +ip_set_unbindip(ip_set_id_t index,
1767 + struct ip_set *set;
1768 + struct ip_set_req_bind *req_bind;
1773 + if (size < sizeof(struct ip_set_req_bind))
1776 + req_bind = (struct ip_set_req_bind *) data;
1777 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1779 + DP("%u %s", index, req_bind->binding);
1780 + if (index == IP_SET_INVALID_ID) {
1781 + /* unbind :all: */
1782 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1783 + /* Default binding of sets */
1784 + write_lock_bh(&ip_set_lock);
1785 + FOREACH_SET_DO(__unbind_default);
1786 + write_unlock_bh(&ip_set_lock);
1788 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1789 + /* Flush all bindings of all sets*/
1790 + write_lock_bh(&ip_set_lock);
1791 + FOREACH_HASH_RW_DO(__set_hash_del);
1792 + write_unlock_bh(&ip_set_lock);
1795 + DP("unreachable reached!");
1799 + set = ip_set_list[index];
1800 + IP_SET_ASSERT(set);
1801 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1802 + /* Default binding of set */
1803 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1805 + if (binding == IP_SET_INVALID_ID)
1808 + write_lock_bh(&ip_set_lock);
1809 + /* Sets in hash values are referenced */
1810 + __ip_set_put(set->binding);
1811 + set->binding = IP_SET_INVALID_ID;
1812 + write_unlock_bh(&ip_set_lock);
1815 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1816 + /* Flush all bindings */
1818 + write_lock_bh(&ip_set_lock);
1819 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1820 + write_unlock_bh(&ip_set_lock);
1824 + res = __ip_set_testip(set,
1825 + data + sizeof(struct ip_set_req_bind),
1826 + size - sizeof(struct ip_set_req_bind),
1829 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1831 + res = ip_set_hash_del(set->id, ip);
1837 +ip_set_testbind(ip_set_id_t index,
1841 + struct ip_set *set = ip_set_list[index];
1842 + struct ip_set_req_bind *req_bind;
1843 + ip_set_id_t binding;
1847 + IP_SET_ASSERT(set);
1848 + if (size < sizeof(struct ip_set_req_bind))
1851 + req_bind = (struct ip_set_req_bind *) data;
1852 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1854 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1855 + /* Default binding of set */
1856 + char *binding_name;
1858 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1861 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1862 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1864 + binding = ip_set_find_byname(binding_name);
1865 + if (binding == IP_SET_INVALID_ID)
1868 + res = (set->binding == binding) ? -EEXIST : 0;
1872 + binding = ip_set_find_byname(req_bind->binding);
1873 + if (binding == IP_SET_INVALID_ID)
1877 + res = __ip_set_testip(set,
1878 + data + sizeof(struct ip_set_req_bind),
1879 + size - sizeof(struct ip_set_req_bind),
1881 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1882 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1885 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1891 +static struct ip_set_type *
1892 +find_set_type_rlock(const char *typename)
1894 + struct ip_set_type *type;
1896 + read_lock_bh(&ip_set_lock);
1897 + type = find_set_type(typename);
1899 + read_unlock_bh(&ip_set_lock);
1905 +find_free_id(const char *name,
1906 + ip_set_id_t *index,
1911 + *id = IP_SET_INVALID_ID;
1912 + for (i = 0; i < ip_set_max; i++) {
1913 + if (ip_set_list[i] == NULL) {
1914 + if (*id == IP_SET_INVALID_ID)
1916 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1920 + if (*id == IP_SET_INVALID_ID)
1921 + /* No free slot remained */
1923 + /* Check that index is usable as id (swapping) */
1925 + for (i = 0; i < ip_set_max; i++) {
1926 + if (ip_set_list[i] != NULL
1927 + && ip_set_list[i]->id == *id) {
1939 +ip_set_create(const char *name,
1940 + const char *typename,
1941 + ip_set_id_t restore,
1945 + struct ip_set *set;
1946 + ip_set_id_t index, id;
1949 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1951 + * First, and without any locks, allocate and initialize
1952 + * a normal base set structure.
1954 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1957 + set->lock = RW_LOCK_UNLOCKED;
1958 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1959 + set->binding = IP_SET_INVALID_ID;
1960 + atomic_set(&set->ref, 0);
1963 + * Next, take the &ip_set_lock, check that we know the type,
1964 + * and take a reference on the type, to make sure it
1965 + * stays available while constructing our new set.
1967 + * After referencing the type, we drop the &ip_set_lock,
1968 + * and let the new set construction run without locks.
1970 + set->type = find_set_type_rlock(typename);
1971 + if (set->type == NULL) {
1972 + /* Try loading the module */
1973 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1974 + strcpy(modulename, "ip_set_");
1975 + strcat(modulename, typename);
1976 + DP("try to load %s", modulename);
1977 + request_module(modulename);
1978 + set->type = find_set_type_rlock(typename);
1980 + if (set->type == NULL) {
1981 + ip_set_printk("no set type '%s', set '%s' not created",
1986 + if (!try_module_get(set->type->me)) {
1987 + read_unlock_bh(&ip_set_lock);
1991 + read_unlock_bh(&ip_set_lock);
1994 + * Without holding any locks, create private part.
1996 + res = set->type->create(set, data, size);
2000 + /* BTW, res==0 here. */
2003 + * Here, we have a valid, constructed set. &ip_set_lock again,
2004 + * find free id/index and check that it is not already in
2007 + write_lock_bh(&ip_set_lock);
2008 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
2009 + DP("no free id!");
2013 + /* Make sure restore gets the same index */
2014 + if (restore != IP_SET_INVALID_ID && index != restore) {
2015 + DP("Can't restore, sets are screwed up");
2021 + * Finally! Add our shiny new set to the list, and be done.
2023 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2025 + ip_set_list[index] = set;
2026 + write_unlock_bh(&ip_set_lock);
2030 + write_unlock_bh(&ip_set_lock);
2031 + set->type->destroy(set);
2033 + module_put(set->type->me);
2040 + * Destroy a given existing set
2043 +ip_set_destroy_set(ip_set_id_t index)
2045 + struct ip_set *set = ip_set_list[index];
2047 + IP_SET_ASSERT(set);
2048 + DP("set: %s", set->name);
2049 + write_lock_bh(&ip_set_lock);
2050 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2051 + if (set->binding != IP_SET_INVALID_ID)
2052 + __ip_set_put(set->binding);
2053 + ip_set_list[index] = NULL;
2054 + write_unlock_bh(&ip_set_lock);
2056 + /* Must call it without holding any lock */
2057 + set->type->destroy(set);
2058 + module_put(set->type->me);
2063 + * Destroy a set - or all sets
2064 + * Sets must not be referenced/used.
2067 +ip_set_destroy(ip_set_id_t index)
2071 + /* ref modification always protected by the mutex */
2072 + if (index != IP_SET_INVALID_ID) {
2073 + if (atomic_read(&ip_set_list[index]->ref))
2075 + ip_set_destroy_set(index);
2077 + for (i = 0; i < ip_set_max; i++) {
2078 + if (ip_set_list[i] != NULL
2079 + && (atomic_read(&ip_set_list[i]->ref)))
2083 + for (i = 0; i < ip_set_max; i++) {
2084 + if (ip_set_list[i] != NULL)
2085 + ip_set_destroy_set(i);
2092 +ip_set_flush_set(struct ip_set *set)
2094 + DP("set: %s %u", set->name, set->id);
2096 + write_lock_bh(&set->lock);
2097 + set->type->flush(set);
2098 + write_unlock_bh(&set->lock);
2102 + * Flush data in a set - or in all sets
2105 +ip_set_flush(ip_set_id_t index)
2107 + if (index != IP_SET_INVALID_ID) {
2108 + IP_SET_ASSERT(ip_set_list[index]);
2109 + ip_set_flush_set(ip_set_list[index]);
2111 + FOREACH_SET_DO(ip_set_flush_set);
2118 +ip_set_rename(ip_set_id_t index, const char *name)
2120 + struct ip_set *set = ip_set_list[index];
2124 + DP("set: %s to %s", set->name, name);
2125 + write_lock_bh(&ip_set_lock);
2126 + for (i = 0; i < ip_set_max; i++) {
2127 + if (ip_set_list[i] != NULL
2128 + && strncmp(ip_set_list[i]->name,
2130 + IP_SET_MAXNAMELEN - 1) == 0) {
2135 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2137 + write_unlock_bh(&ip_set_lock);
2142 + * Swap two sets so that name/index points to the other.
2143 + * References are also swapped.
2146 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2148 + struct ip_set *from = ip_set_list[from_index];
2149 + struct ip_set *to = ip_set_list[to_index];
2150 + char from_name[IP_SET_MAXNAMELEN];
2151 + u_int32_t from_ref;
2153 + DP("set: %s to %s", from->name, to->name);
2154 + /* Type can't be changed. Artifical restriction. */
2155 + if (from->type->typecode != to->type->typecode)
2158 + /* No magic here: ref munging protected by the mutex */
2159 + write_lock_bh(&ip_set_lock);
2160 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2161 + from_ref = atomic_read(&from->ref);
2163 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2164 + atomic_set(&from->ref, atomic_read(&to->ref));
2165 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2166 + atomic_set(&to->ref, from_ref);
2168 + ip_set_list[from_index] = to;
2169 + ip_set_list[to_index] = from;
2171 + write_unlock_bh(&ip_set_lock);
2180 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2181 + ip_set_id_t id, size_t *size)
2183 + if (set_hash->id == id)
2184 + *size += sizeof(struct ip_set_hash_list);
2188 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2189 + ip_set_id_t id, size_t *size)
2191 + if (set_hash->id == id)
2192 + *size += sizeof(struct ip_set_hash_save);
2196 +__set_hash_bindings(struct ip_set_hash *set_hash,
2197 + ip_set_id_t id, void *data, int *used)
2199 + if (set_hash->id == id) {
2200 + struct ip_set_hash_list *hash_list =
2201 + (struct ip_set_hash_list *)(data + *used);
2203 + hash_list->ip = set_hash->ip;
2204 + hash_list->binding = set_hash->binding;
2205 + *used += sizeof(struct ip_set_hash_list);
2209 +static int ip_set_list_set(ip_set_id_t index,
2214 + struct ip_set *set = ip_set_list[index];
2215 + struct ip_set_list *set_list;
2217 + /* Pointer to our header */
2218 + set_list = (struct ip_set_list *) (data + *used);
2220 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2222 + /* Get and ensure header size */
2223 + if (*used + sizeof(struct ip_set_list) > len)
2224 + goto not_enough_mem;
2225 + *used += sizeof(struct ip_set_list);
2227 + read_lock_bh(&set->lock);
2228 + /* Get and ensure set specific header size */
2229 + set_list->header_size = set->type->header_size;
2230 + if (*used + set_list->header_size > len)
2233 + /* Fill in the header */
2234 + set_list->index = index;
2235 + set_list->binding = set->binding;
2236 + set_list->ref = atomic_read(&set->ref);
2238 + /* Fill in set spefific header data */
2239 + set->type->list_header(set, data + *used);
2240 + *used += set_list->header_size;
2242 + /* Get and ensure set specific members size */
2243 + set_list->members_size = set->type->list_members_size(set);
2244 + if (*used + set_list->members_size > len)
2247 + /* Fill in set spefific members data */
2248 + set->type->list_members(set, data + *used);
2249 + *used += set_list->members_size;
2250 + read_unlock_bh(&set->lock);
2254 + /* Get and ensure set specific bindings size */
2255 + set_list->bindings_size = 0;
2256 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2257 + set->id, &set_list->bindings_size);
2258 + if (*used + set_list->bindings_size > len)
2259 + goto not_enough_mem;
2261 + /* Fill in set spefific bindings data */
2262 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2267 + read_unlock_bh(&set->lock);
2269 + DP("not enough mem, try again");
2276 +static int ip_set_save_set(ip_set_id_t index,
2281 + struct ip_set *set;
2282 + struct ip_set_save *set_save;
2284 + /* Pointer to our header */
2285 + set_save = (struct ip_set_save *) (data + *used);
2287 + /* Get and ensure header size */
2288 + if (*used + sizeof(struct ip_set_save) > len)
2289 + goto not_enough_mem;
2290 + *used += sizeof(struct ip_set_save);
2292 + set = ip_set_list[index];
2293 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2294 + data, data + *used);
2296 + read_lock_bh(&set->lock);
2297 + /* Get and ensure set specific header size */
2298 + set_save->header_size = set->type->header_size;
2299 + if (*used + set_save->header_size > len)
2302 + /* Fill in the header */
2303 + set_save->index = index;
2304 + set_save->binding = set->binding;
2306 + /* Fill in set spefific header data */
2307 + set->type->list_header(set, data + *used);
2308 + *used += set_save->header_size;
2310 + DP("set header filled: %s, used: %u %p %p", set->name, *used,
2311 + data, data + *used);
2312 + /* Get and ensure set specific members size */
2313 + set_save->members_size = set->type->list_members_size(set);
2314 + if (*used + set_save->members_size > len)
2317 + /* Fill in set spefific members data */
2318 + set->type->list_members(set, data + *used);
2319 + *used += set_save->members_size;
2320 + read_unlock_bh(&set->lock);
2321 + DP("set members filled: %s, used: %u %p %p", set->name, *used,
2322 + data, data + *used);
2326 + read_unlock_bh(&set->lock);
2328 + DP("not enough mem, try again");
2333 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2341 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2342 + struct ip_set_hash_save *hash_save =
2343 + (struct ip_set_hash_save *)(data + *used);
2344 + /* Ensure bindings size */
2345 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2349 + hash_save->id = set_hash->id;
2350 + hash_save->ip = set_hash->ip;
2351 + hash_save->binding = set_hash->binding;
2352 + *used += sizeof(struct ip_set_hash_save);
2356 +static int ip_set_save_bindings(ip_set_id_t index,
2362 + struct ip_set_save *set_save;
2364 + DP("used %u, len %u", *used, len);
2365 + /* Get and ensure header size */
2366 + if (*used + sizeof(struct ip_set_save) > len)
2370 + set_save = (struct ip_set_save *) (data + *used);
2371 + set_save->index = IP_SET_INVALID_ID;
2372 + *used += sizeof(struct ip_set_save);
2374 + DP("marker added used %u, len %u", *used, len);
2375 + /* Fill in bindings data */
2376 + if (index != IP_SET_INVALID_ID)
2377 + /* Sets are identified by id in hash */
2378 + index = ip_set_list[index]->id;
2379 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2387 +static int ip_set_restore(void *data,
2391 + int line = 0, used = 0, members_size;
2392 + struct ip_set *set;
2393 + struct ip_set_hash_save *hash_save;
2394 + struct ip_set_restore *set_restore;
2395 + ip_set_id_t index;
2397 + /* Loop to restore sets */
2401 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2402 + /* Get and ensure header size */
2403 + if (used + sizeof(struct ip_set_restore) > len)
2405 + set_restore = (struct ip_set_restore *) (data + used);
2406 + used += sizeof(struct ip_set_restore);
2408 + /* Ensure data size */
2410 + + set_restore->header_size
2411 + + set_restore->members_size > len)
2414 + /* Check marker */
2415 + if (set_restore->index == IP_SET_INVALID_ID) {
2420 + /* Try to create the set */
2421 + DP("restore %s %s", set_restore->name, set_restore->typename);
2422 + res = ip_set_create(set_restore->name,
2423 + set_restore->typename,
2424 + set_restore->index,
2426 + set_restore->header_size);
2430 + used += set_restore->header_size;
2432 + index = ip_set_find_byindex(set_restore->index);
2433 + DP("index %u, restore_index %u", index, set_restore->index);
2434 + if (index != set_restore->index)
2436 + /* Try to restore members data */
2437 + set = ip_set_list[index];
2439 + DP("members_size %u reqsize %u",
2440 + set_restore->members_size, set->type->reqsize);
2441 + while (members_size + set->type->reqsize <=
2442 + set_restore->members_size) {
2444 + DP("members: %u, line %u", members_size, line);
2445 + res = __ip_set_addip(index,
2446 + data + used + members_size,
2447 + set->type->reqsize);
2448 + if (!(res == 0 || res == -EEXIST))
2450 + members_size += set->type->reqsize;
2453 + DP("members_size %u %u",
2454 + set_restore->members_size, members_size);
2455 + if (members_size != set_restore->members_size)
2457 + used += set_restore->members_size;
2461 + /* Loop to restore bindings */
2462 + while (used < len) {
2465 + DP("restore binding, line %u", line);
2466 + /* Get and ensure size */
2467 + if (used + sizeof(struct ip_set_hash_save) > len)
2469 + hash_save = (struct ip_set_hash_save *) (data + used);
2470 + used += sizeof(struct ip_set_hash_save);
2472 + /* hash_save->id is used to store the index */
2473 + index = ip_set_find_byindex(hash_save->id);
2474 + DP("restore binding index %u, id %u, %u -> %u",
2475 + index, hash_save->id, hash_save->ip, hash_save->binding);
2476 + if (index != hash_save->id)
2479 + set = ip_set_list[hash_save->id];
2480 + /* Null valued IP means default binding */
2481 + if (hash_save->ip)
2482 + res = ip_set_hash_add(set->id,
2484 + hash_save->binding);
2486 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2487 + write_lock_bh(&ip_set_lock);
2488 + set->binding = hash_save->binding;
2489 + __ip_set_get(set->binding);
2490 + write_unlock_bh(&ip_set_lock);
2491 + DP("default binding: %u", set->binding);
2503 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2506 + int res = 0; /* Assume OK */
2508 + struct ip_set_req_adt *req_adt;
2509 + ip_set_id_t index = IP_SET_INVALID_ID;
2510 + int (*adtfn)(ip_set_id_t index,
2511 + const void *data, size_t size);
2513 + int (*fn)(ip_set_id_t index,
2514 + const void *data, size_t size);
2516 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2517 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2520 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2521 + if (!capable(CAP_NET_ADMIN))
2523 + if (optval != SO_IP_SET)
2525 + if (len <= sizeof(unsigned)) {
2526 + ip_set_printk("short userdata (want >%zu, got %u)",
2527 + sizeof(unsigned), len);
2530 + data = vmalloc(len);
2532 + DP("out of mem for %u bytes", len);
2535 + if (copy_from_user(data, user, len) != 0) {
2539 + if (down_interruptible(&ip_set_app_mutex)) {
2544 + op = (unsigned *)data;
2547 + if (*op < IP_SET_OP_VERSION) {
2548 + /* Check the version at the beginning of operations */
2549 + struct ip_set_req_version *req_version =
2550 + (struct ip_set_req_version *) data;
2551 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2558 + case IP_SET_OP_CREATE:{
2559 + struct ip_set_req_create *req_create
2560 + = (struct ip_set_req_create *) data;
2562 + if (len <= sizeof(struct ip_set_req_create)) {
2563 + ip_set_printk("short CREATE data (want >%zu, got %u)",
2564 + sizeof(struct ip_set_req_create), len);
2568 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2569 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2570 + res = ip_set_create(req_create->name,
2571 + req_create->typename,
2572 + IP_SET_INVALID_ID,
2573 + data + sizeof(struct ip_set_req_create),
2574 + len - sizeof(struct ip_set_req_create));
2577 + case IP_SET_OP_DESTROY:{
2578 + struct ip_set_req_std *req_destroy
2579 + = (struct ip_set_req_std *) data;
2581 + if (len != sizeof(struct ip_set_req_std)) {
2582 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2583 + sizeof(struct ip_set_req_std), len);
2587 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2588 + /* Destroy all sets */
2589 + index = IP_SET_INVALID_ID;
2591 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2592 + index = ip_set_find_byname(req_destroy->name);
2594 + if (index == IP_SET_INVALID_ID) {
2600 + res = ip_set_destroy(index);
2603 + case IP_SET_OP_FLUSH:{
2604 + struct ip_set_req_std *req_flush =
2605 + (struct ip_set_req_std *) data;
2607 + if (len != sizeof(struct ip_set_req_std)) {
2608 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2609 + sizeof(struct ip_set_req_std), len);
2613 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2614 + /* Flush all sets */
2615 + index = IP_SET_INVALID_ID;
2617 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2618 + index = ip_set_find_byname(req_flush->name);
2620 + if (index == IP_SET_INVALID_ID) {
2625 + res = ip_set_flush(index);
2628 + case IP_SET_OP_RENAME:{
2629 + struct ip_set_req_create *req_rename
2630 + = (struct ip_set_req_create *) data;
2632 + if (len != sizeof(struct ip_set_req_create)) {
2633 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2634 + sizeof(struct ip_set_req_create), len);
2639 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2640 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2642 + index = ip_set_find_byname(req_rename->name);
2643 + if (index == IP_SET_INVALID_ID) {
2647 + res = ip_set_rename(index, req_rename->typename);
2650 + case IP_SET_OP_SWAP:{
2651 + struct ip_set_req_create *req_swap
2652 + = (struct ip_set_req_create *) data;
2653 + ip_set_id_t to_index;
2655 + if (len != sizeof(struct ip_set_req_create)) {
2656 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2657 + sizeof(struct ip_set_req_create), len);
2662 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2663 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2665 + index = ip_set_find_byname(req_swap->name);
2666 + if (index == IP_SET_INVALID_ID) {
2670 + to_index = ip_set_find_byname(req_swap->typename);
2671 + if (to_index == IP_SET_INVALID_ID) {
2675 + res = ip_set_swap(index, to_index);
2679 + break; /* Set identified by id */
2682 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2683 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2687 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2689 + if (len < sizeof(struct ip_set_req_adt)) {
2690 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2691 + sizeof(struct ip_set_req_adt), len);
2695 + req_adt = (struct ip_set_req_adt *) data;
2697 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2698 + if (!(*op == IP_SET_OP_UNBIND_SET
2699 + && req_adt->index == IP_SET_INVALID_ID)) {
2700 + index = ip_set_find_byindex(req_adt->index);
2701 + if (index == IP_SET_INVALID_ID) {
2706 + res = adtfn(index, data, len);
2709 + up(&ip_set_app_mutex);
2713 + DP("final result %d", res);
2718 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2722 + ip_set_id_t index = IP_SET_INVALID_ID;
2724 + int copylen = *len;
2726 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2727 + if (!capable(CAP_NET_ADMIN))
2729 + if (optval != SO_IP_SET)
2731 + if (*len < sizeof(unsigned)) {
2732 + ip_set_printk("short userdata (want >=%zu, got %d)",
2733 + sizeof(unsigned), *len);
2736 + data = vmalloc(*len);
2738 + DP("out of mem for %d bytes", *len);
2741 + if (copy_from_user(data, user, *len) != 0) {
2745 + if (down_interruptible(&ip_set_app_mutex)) {
2750 + op = (unsigned *) data;
2753 + if (*op < IP_SET_OP_VERSION) {
2754 + /* Check the version at the beginning of operations */
2755 + struct ip_set_req_version *req_version =
2756 + (struct ip_set_req_version *) data;
2757 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2764 + case IP_SET_OP_VERSION: {
2765 + struct ip_set_req_version *req_version =
2766 + (struct ip_set_req_version *) data;
2768 + if (*len != sizeof(struct ip_set_req_version)) {
2769 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2770 + sizeof(struct ip_set_req_version),
2776 + req_version->version = IP_SET_PROTOCOL_VERSION;
2777 + res = copy_to_user(user, req_version,
2778 + sizeof(struct ip_set_req_version));
2781 + case IP_SET_OP_GET_BYNAME: {
2782 + struct ip_set_req_get_set *req_get
2783 + = (struct ip_set_req_get_set *) data;
2785 + if (*len != sizeof(struct ip_set_req_get_set)) {
2786 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2787 + sizeof(struct ip_set_req_get_set), *len);
2791 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2792 + index = ip_set_find_byname(req_get->set.name);
2793 + req_get->set.index = index;
2796 + case IP_SET_OP_GET_BYINDEX: {
2797 + struct ip_set_req_get_set *req_get
2798 + = (struct ip_set_req_get_set *) data;
2800 + if (*len != sizeof(struct ip_set_req_get_set)) {
2801 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2802 + sizeof(struct ip_set_req_get_set), *len);
2806 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2807 + index = ip_set_find_byindex(req_get->set.index);
2808 + strncpy(req_get->set.name,
2809 + index == IP_SET_INVALID_ID ? ""
2810 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2813 + case IP_SET_OP_ADT_GET: {
2814 + struct ip_set_req_adt_get *req_get
2815 + = (struct ip_set_req_adt_get *) data;
2817 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2818 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2819 + sizeof(struct ip_set_req_adt_get), *len);
2823 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2824 + index = ip_set_find_byname(req_get->set.name);
2825 + if (index != IP_SET_INVALID_ID) {
2826 + req_get->set.index = index;
2827 + strncpy(req_get->typename,
2828 + ip_set_list[index]->type->typename,
2829 + IP_SET_MAXNAMELEN - 1);
2836 + case IP_SET_OP_MAX_SETS: {
2837 + struct ip_set_req_max_sets *req_max_sets
2838 + = (struct ip_set_req_max_sets *) data;
2841 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2842 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2843 + sizeof(struct ip_set_req_max_sets), *len);
2848 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2849 + req_max_sets->set.index = IP_SET_INVALID_ID;
2851 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2852 + req_max_sets->set.index =
2853 + ip_set_find_byname(req_max_sets->set.name);
2854 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2859 + req_max_sets->max_sets = ip_set_max;
2860 + req_max_sets->sets = 0;
2861 + for (i = 0; i < ip_set_max; i++) {
2862 + if (ip_set_list[i] != NULL)
2863 + req_max_sets->sets++;
2867 + case IP_SET_OP_LIST_SIZE:
2868 + case IP_SET_OP_SAVE_SIZE: {
2869 + struct ip_set_req_setnames *req_setnames
2870 + = (struct ip_set_req_setnames *) data;
2871 + struct ip_set_name_list *name_list;
2872 + struct ip_set *set;
2876 + if (*len < sizeof(struct ip_set_req_setnames)) {
2877 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2878 + sizeof(struct ip_set_req_setnames), *len);
2883 + req_setnames->size = 0;
2884 + used = sizeof(struct ip_set_req_setnames);
2885 + for (i = 0; i < ip_set_max; i++) {
2886 + if (ip_set_list[i] == NULL)
2888 + name_list = (struct ip_set_name_list *)
2890 + used += sizeof(struct ip_set_name_list);
2891 + if (used > copylen) {
2895 + set = ip_set_list[i];
2896 + /* Fill in index, name, etc. */
2897 + name_list->index = i;
2898 + name_list->id = set->id;
2899 + strncpy(name_list->name,
2901 + IP_SET_MAXNAMELEN - 1);
2902 + strncpy(name_list->typename,
2903 + set->type->typename,
2904 + IP_SET_MAXNAMELEN - 1);
2905 + DP("filled %s of type %s, index %u\n",
2906 + name_list->name, name_list->typename,
2907 + name_list->index);
2908 + if (!(req_setnames->index == IP_SET_INVALID_ID
2909 + || req_setnames->index == i))
2913 + case IP_SET_OP_LIST_SIZE: {
2914 + req_setnames->size += sizeof(struct ip_set_list)
2915 + + set->type->header_size
2916 + + set->type->list_members_size(set);
2917 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2918 + i, &req_setnames->size);
2921 + case IP_SET_OP_SAVE_SIZE: {
2922 + req_setnames->size += sizeof(struct ip_set_save)
2923 + + set->type->header_size
2924 + + set->type->list_members_size(set);
2925 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2926 + i, &req_setnames->size);
2933 + if (copylen != used) {
2939 + case IP_SET_OP_LIST: {
2940 + struct ip_set_req_list *req_list
2941 + = (struct ip_set_req_list *) data;
2945 + if (*len < sizeof(struct ip_set_req_list)) {
2946 + ip_set_printk("short LIST (want >=%zu, got %d)",
2947 + sizeof(struct ip_set_req_list), *len);
2951 + index = req_list->index;
2952 + if (index != IP_SET_INVALID_ID
2953 + && ip_set_find_byindex(index) != index) {
2958 + if (index == IP_SET_INVALID_ID) {
2959 + /* List all sets */
2960 + for (i = 0; i < ip_set_max && res == 0; i++) {
2961 + if (ip_set_list[i] != NULL)
2962 + res = ip_set_list_set(i, data, &used, *len);
2965 + /* List an individual set */
2966 + res = ip_set_list_set(index, data, &used, *len);
2970 + else if (copylen != used) {
2976 + case IP_SET_OP_SAVE: {
2977 + struct ip_set_req_list *req_save
2978 + = (struct ip_set_req_list *) data;
2982 + if (*len < sizeof(struct ip_set_req_list)) {
2983 + ip_set_printk("short SAVE (want >=%zu, got %d)",
2984 + sizeof(struct ip_set_req_list), *len);
2988 + index = req_save->index;
2989 + if (index != IP_SET_INVALID_ID
2990 + && ip_set_find_byindex(index) != index) {
2995 + if (index == IP_SET_INVALID_ID) {
2996 + /* Save all sets */
2997 + for (i = 0; i < ip_set_max && res == 0; i++) {
2998 + if (ip_set_list[i] != NULL)
2999 + res = ip_set_save_set(i, data, &used, *len);
3002 + /* Save an individual set */
3003 + res = ip_set_save_set(index, data, &used, *len);
3006 + res = ip_set_save_bindings(index, data, &used, *len);
3010 + else if (copylen != used) {
3016 + case IP_SET_OP_RESTORE: {
3017 + struct ip_set_req_setnames *req_restore
3018 + = (struct ip_set_req_setnames *) data;
3021 + if (*len < sizeof(struct ip_set_req_setnames)
3022 + || *len != req_restore->size) {
3023 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3024 + req_restore->size, *len);
3028 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3029 + req_restore->size - sizeof(struct ip_set_req_setnames));
3030 + DP("ip_set_restore: %u", line);
3033 + req_restore->size = line;
3034 + copylen = sizeof(struct ip_set_req_setnames);
3042 + } /* end of switch(op) */
3045 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3046 + && ip_set_list[index]
3047 + ? ip_set_list[index]->name
3048 + : ":all:", copylen);
3050 + res = copy_to_user(user, data, copylen);
3052 + copy_to_user(user, data, copylen);
3055 + up(&ip_set_app_mutex);
3059 + DP("final result %d", res);
3063 +static struct nf_sockopt_ops so_set = {
3065 + .set_optmin = SO_IP_SET,
3066 + .set_optmax = SO_IP_SET + 1,
3067 + .set = &ip_set_sockfn_set,
3068 + .get_optmin = SO_IP_SET,
3069 + .get_optmax = SO_IP_SET + 1,
3070 + .get = &ip_set_sockfn_get,
3074 +static int max_sets, hash_size;
3075 +module_param(max_sets, int, 0600);
3076 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3077 +module_param(hash_size, int, 0600);
3078 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3079 +MODULE_LICENSE("GPL");
3080 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3081 +MODULE_DESCRIPTION("module implementing core IP set support");
3083 +static int __init init(void)
3088 + get_random_bytes(&ip_set_hash_random, 4);
3090 + ip_set_max = max_sets;
3091 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3092 + if (!ip_set_list) {
3093 + printk(KERN_ERR "Unable to create ip_set_list\n");
3096 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3098 + ip_set_bindings_hash_size = hash_size;
3099 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3100 + if (!ip_set_hash) {
3101 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3102 + vfree(ip_set_list);
3105 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3106 + INIT_LIST_HEAD(&ip_set_hash[i]);
3108 + INIT_LIST_HEAD(&set_type_list);
3110 + res = nf_register_sockopt(&so_set);
3112 + ip_set_printk("SO_SET registry failed: %d", res);
3113 + vfree(ip_set_list);
3114 + vfree(ip_set_hash);
3120 +static void __exit fini(void)
3122 + /* There can't be any existing set or binding */
3123 + nf_unregister_sockopt(&so_set);
3124 + vfree(ip_set_list);
3125 + vfree(ip_set_hash);
3126 + DP("these are the famous last words");
3129 +EXPORT_SYMBOL(ip_set_register_set_type);
3130 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3132 +EXPORT_SYMBOL(ip_set_get_byname);
3133 +EXPORT_SYMBOL(ip_set_get_byindex);
3134 +EXPORT_SYMBOL(ip_set_put);
3136 +EXPORT_SYMBOL(ip_set_addip_kernel);
3137 +EXPORT_SYMBOL(ip_set_delip_kernel);
3138 +EXPORT_SYMBOL(ip_set_testip_kernel);
3142 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ip_set_iphash.c linux-2.6.19.dev/net/ipv4/netfilter/ip_set_iphash.c
3143 --- linux-2.6.19.old/net/ipv4/netfilter/ip_set_iphash.c 1970-01-01 01:00:00.000000000 +0100
3144 +++ linux-2.6.19.dev/net/ipv4/netfilter/ip_set_iphash.c 2006-12-14 03:13:43.000000000 +0100
3146 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3148 + * This program is free software; you can redistribute it and/or modify
3149 + * it under the terms of the GNU General Public License version 2 as
3150 + * published by the Free Software Foundation.
3153 +/* Kernel module implementing an ip hash set */
3155 +#include <linux/module.h>
3156 +#include <linux/ip.h>
3157 +#include <linux/skbuff.h>
3158 +#include <linux/netfilter_ipv4/ip_tables.h>
3159 +#include <linux/netfilter_ipv4/ip_set.h>
3160 +#include <linux/errno.h>
3161 +#include <asm/uaccess.h>
3162 +#include <asm/bitops.h>
3163 +#include <linux/spinlock.h>
3164 +#include <linux/vmalloc.h>
3165 +#include <linux/random.h>
3167 +#include <net/ip.h>
3169 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3170 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3171 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
3172 +#include <linux/netfilter_ipv4/ip_set_prime.h>
3174 +static inline __u32
3175 +jhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3177 + return jhash_1word(ip, map->initval);
3180 +static inline __u32
3181 +randhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3183 + return (1 + ip % map->prime);
3186 +static inline __u32
3187 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3189 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3190 + __u32 jhash, randhash, id;
3193 + *hash_ip = ip & map->netmask;
3194 + jhash = jhash_ip(map, *hash_ip);
3195 + randhash = randhash_ip(map, *hash_ip);
3196 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3197 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3199 + for (i = 0; i < map->probes; i++) {
3200 + id = (jhash + i * randhash) % map->hashsize;
3201 + DP("hash key: %u", id);
3202 + if (map->members[id] == *hash_ip)
3204 + /* No shortcut at testing - there can be deleted
3211 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3213 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
3217 +testip(struct ip_set *set, const void *data, size_t size,
3218 + ip_set_ip_t *hash_ip)
3220 + struct ip_set_req_iphash *req =
3221 + (struct ip_set_req_iphash *) data;
3223 + if (size != sizeof(struct ip_set_req_iphash)) {
3224 + ip_set_printk("data length wrong (want %zu, have %zu)",
3225 + sizeof(struct ip_set_req_iphash),
3229 + return __testip(set, req->ip, hash_ip);
3233 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
3234 + u_int32_t flags, ip_set_ip_t *hash_ip)
3236 + return __testip(set,
3237 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3238 + : skb->nh.iph->daddr),
3243 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3245 + __u32 jhash, randhash, probe;
3248 + *hash_ip = ip & map->netmask;
3249 + jhash = jhash_ip(map, *hash_ip);
3250 + randhash = randhash_ip(map, *hash_ip);
3252 + for (i = 0; i < map->probes; i++) {
3253 + probe = (jhash + i * randhash) % map->hashsize;
3254 + if (map->members[probe] == *hash_ip)
3256 + if (!map->members[probe]) {
3257 + map->members[probe] = *hash_ip;
3261 + /* Trigger rehashing */
3266 +addip(struct ip_set *set, const void *data, size_t size,
3267 + ip_set_ip_t *hash_ip)
3269 + struct ip_set_req_iphash *req =
3270 + (struct ip_set_req_iphash *) data;
3272 + if (size != sizeof(struct ip_set_req_iphash)) {
3273 + ip_set_printk("data length wrong (want %zu, have %zu)",
3274 + sizeof(struct ip_set_req_iphash),
3278 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3282 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3283 + u_int32_t flags, ip_set_ip_t *hash_ip)
3285 + return __addip((struct ip_set_iphash *) set->data,
3286 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3287 + : skb->nh.iph->daddr),
3291 +static int retry(struct ip_set *set)
3293 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3294 + ip_set_ip_t hash_ip, *members;
3295 + u_int32_t i, hashsize;
3296 + unsigned newbytes;
3298 + struct ip_set_iphash tmp = {
3299 + .hashsize = map->hashsize,
3300 + .probes = map->probes,
3301 + .resize = map->resize,
3302 + .netmask = map->netmask,
3305 + if (map->resize == 0)
3311 + /* Calculate new parameters */
3312 + get_random_bytes(&tmp.initval, 4);
3313 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
3314 + if (hashsize == tmp.hashsize)
3316 + tmp.prime = make_prime(hashsize);
3318 + ip_set_printk("rehashing of set %s triggered: "
3319 + "hashsize grows from %u to %u",
3320 + set->name, tmp.hashsize, hashsize);
3321 + tmp.hashsize = hashsize;
3323 + newbytes = hashsize * sizeof(ip_set_ip_t);
3324 + tmp.members = ip_set_malloc_atomic(newbytes);
3325 + if (!tmp.members) {
3326 + DP("out of memory for %d bytes", newbytes);
3329 + memset(tmp.members, 0, newbytes);
3331 + write_lock_bh(&set->lock);
3332 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3333 + for (i = 0; i < map->hashsize && res == 0; i++) {
3334 + if (map->members[i])
3335 + res = __addip(&tmp, map->members[i], &hash_ip);
3338 + /* Failure, try again */
3339 + write_unlock_bh(&set->lock);
3340 + ip_set_free(tmp.members, newbytes);
3344 + /* Success at resizing! */
3345 + members = map->members;
3346 + hashsize = map->hashsize;
3348 + map->initval = tmp.initval;
3349 + map->prime = tmp.prime;
3350 + map->hashsize = tmp.hashsize;
3351 + map->members = tmp.members;
3352 + write_unlock_bh(&set->lock);
3354 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
3360 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3362 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3363 + ip_set_ip_t id = hash_id(set, ip, hash_ip);
3365 + if (id == UINT_MAX)
3368 + map->members[id] = 0;
3373 +delip(struct ip_set *set, const void *data, size_t size,
3374 + ip_set_ip_t *hash_ip)
3376 + struct ip_set_req_iphash *req =
3377 + (struct ip_set_req_iphash *) data;
3379 + if (size != sizeof(struct ip_set_req_iphash)) {
3380 + ip_set_printk("data length wrong (want %zu, have %zu)",
3381 + sizeof(struct ip_set_req_iphash),
3385 + return __delip(set, req->ip, hash_ip);
3389 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3390 + u_int32_t flags, ip_set_ip_t *hash_ip)
3392 + return __delip(set,
3393 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3394 + : skb->nh.iph->daddr),
3398 +static int create(struct ip_set *set, const void *data, size_t size)
3400 + unsigned newbytes;
3401 + struct ip_set_req_iphash_create *req =
3402 + (struct ip_set_req_iphash_create *) data;
3403 + struct ip_set_iphash *map;
3405 + if (size != sizeof(struct ip_set_req_iphash_create)) {
3406 + ip_set_printk("data length wrong (want %zu, have %zu)",
3407 + sizeof(struct ip_set_req_iphash_create),
3412 + if (req->hashsize < 1) {
3413 + ip_set_printk("hashsize too small");
3417 + map = kmalloc(sizeof(struct ip_set_iphash), GFP_KERNEL);
3419 + DP("out of memory for %d bytes",
3420 + sizeof(struct ip_set_iphash));
3423 + get_random_bytes(&map->initval, 4);
3424 + map->prime = make_prime(req->hashsize);
3425 + map->hashsize = req->hashsize;
3426 + map->probes = req->probes;
3427 + map->resize = req->resize;
3428 + map->netmask = req->netmask;
3429 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
3430 + map->members = ip_set_malloc(newbytes);
3431 + if (!map->members) {
3432 + DP("out of memory for %d bytes", newbytes);
3436 + memset(map->members, 0, newbytes);
3442 +static void destroy(struct ip_set *set)
3444 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3446 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
3452 +static void flush(struct ip_set *set)
3454 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3455 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
3458 +static void list_header(const struct ip_set *set, void *data)
3460 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3461 + struct ip_set_req_iphash_create *header =
3462 + (struct ip_set_req_iphash_create *) data;
3464 + header->hashsize = map->hashsize;
3465 + header->probes = map->probes;
3466 + header->resize = map->resize;
3467 + header->netmask = map->netmask;
3470 +static int list_members_size(const struct ip_set *set)
3472 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3474 + return (map->hashsize * sizeof(ip_set_ip_t));
3477 +static void list_members(const struct ip_set *set, void *data)
3479 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3480 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
3482 + memcpy(data, map->members, bytes);
3485 +static struct ip_set_type ip_set_iphash = {
3486 + .typename = SETTYPE_NAME,
3487 + .typecode = IPSET_TYPE_IP,
3488 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3489 + .create = &create,
3490 + .destroy = &destroy,
3492 + .reqsize = sizeof(struct ip_set_req_iphash),
3494 + .addip_kernel = &addip_kernel,
3497 + .delip_kernel = &delip_kernel,
3498 + .testip = &testip,
3499 + .testip_kernel = &testip_kernel,
3500 + .header_size = sizeof(struct ip_set_req_iphash_create),
3501 + .list_header = &list_header,
3502 + .list_members_size = &list_members_size,
3503 + .list_members = &list_members,
3504 + .me = THIS_MODULE,
3507 +MODULE_LICENSE("GPL");
3508 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3509 +MODULE_DESCRIPTION("iphash type of IP sets");
3511 +static int __init init(void)
3513 + init_max_malloc_size();
3514 + return ip_set_register_set_type(&ip_set_iphash);
3517 +static void __exit fini(void)
3519 + /* FIXME: possible race with ip_set_create() */
3520 + ip_set_unregister_set_type(&ip_set_iphash);
3525 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ip_set_ipmap.c linux-2.6.19.dev/net/ipv4/netfilter/ip_set_ipmap.c
3526 --- linux-2.6.19.old/net/ipv4/netfilter/ip_set_ipmap.c 1970-01-01 01:00:00.000000000 +0100
3527 +++ linux-2.6.19.dev/net/ipv4/netfilter/ip_set_ipmap.c 2006-12-14 03:13:43.000000000 +0100
3529 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3530 + * Patrick Schaaf <bof@bof.de>
3531 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3533 + * This program is free software; you can redistribute it and/or modify
3534 + * it under the terms of the GNU General Public License version 2 as
3535 + * published by the Free Software Foundation.
3538 +/* Kernel module implementing an IP set type: the single bitmap type */
3540 +#include <linux/module.h>
3541 +#include <linux/ip.h>
3542 +#include <linux/skbuff.h>
3543 +#include <linux/netfilter_ipv4/ip_tables.h>
3544 +#include <linux/netfilter_ipv4/ip_set.h>
3545 +#include <linux/errno.h>
3546 +#include <asm/uaccess.h>
3547 +#include <asm/bitops.h>
3548 +#include <linux/spinlock.h>
3550 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3552 +static inline ip_set_ip_t
3553 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3555 + return (ip - map->first_ip)/map->hosts;
3559 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3561 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3563 + if (ip < map->first_ip || ip > map->last_ip)
3566 + *hash_ip = ip & map->netmask;
3567 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3568 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3569 + return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3573 +testip(struct ip_set *set, const void *data, size_t size,
3574 + ip_set_ip_t *hash_ip)
3576 + struct ip_set_req_ipmap *req =
3577 + (struct ip_set_req_ipmap *) data;
3579 + if (size != sizeof(struct ip_set_req_ipmap)) {
3580 + ip_set_printk("data length wrong (want %zu, have %zu)",
3581 + sizeof(struct ip_set_req_ipmap),
3585 + return __testip(set, req->ip, hash_ip);
3589 +testip_kernel(struct ip_set *set,
3590 + const struct sk_buff *skb,
3592 + ip_set_ip_t *hash_ip)
3596 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3597 + flags & IPSET_SRC ? "SRC" : "DST",
3598 + NIPQUAD(skb->nh.iph->saddr),
3599 + NIPQUAD(skb->nh.iph->daddr));
3601 + res = __testip(set,
3602 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3603 + : skb->nh.iph->daddr),
3605 + return (res < 0 ? 0 : res);
3609 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3611 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3613 + if (ip < map->first_ip || ip > map->last_ip)
3616 + *hash_ip = ip & map->netmask;
3617 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3618 + if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3625 +addip(struct ip_set *set, const void *data, size_t size,
3626 + ip_set_ip_t *hash_ip)
3628 + struct ip_set_req_ipmap *req =
3629 + (struct ip_set_req_ipmap *) data;
3631 + if (size != sizeof(struct ip_set_req_ipmap)) {
3632 + ip_set_printk("data length wrong (want %zu, have %zu)",
3633 + sizeof(struct ip_set_req_ipmap),
3637 + DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3638 + return __addip(set, req->ip, hash_ip);
3642 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3643 + u_int32_t flags, ip_set_ip_t *hash_ip)
3645 + return __addip(set,
3646 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3647 + : skb->nh.iph->daddr),
3652 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3654 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3656 + if (ip < map->first_ip || ip > map->last_ip)
3659 + *hash_ip = ip & map->netmask;
3660 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3661 + if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3668 +delip(struct ip_set *set, const void *data, size_t size,
3669 + ip_set_ip_t *hash_ip)
3671 + struct ip_set_req_ipmap *req =
3672 + (struct ip_set_req_ipmap *) data;
3674 + if (size != sizeof(struct ip_set_req_ipmap)) {
3675 + ip_set_printk("data length wrong (want %zu, have %zu)",
3676 + sizeof(struct ip_set_req_ipmap),
3680 + return __delip(set, req->ip, hash_ip);
3684 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3685 + u_int32_t flags, ip_set_ip_t *hash_ip)
3687 + return __delip(set,
3688 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3689 + : skb->nh.iph->daddr),
3693 +static int create(struct ip_set *set, const void *data, size_t size)
3696 + struct ip_set_req_ipmap_create *req =
3697 + (struct ip_set_req_ipmap_create *) data;
3698 + struct ip_set_ipmap *map;
3700 + if (size != sizeof(struct ip_set_req_ipmap_create)) {
3701 + ip_set_printk("data length wrong (want %zu, have %zu)",
3702 + sizeof(struct ip_set_req_ipmap_create),
3707 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3708 + HIPQUAD(req->from), HIPQUAD(req->to));
3710 + if (req->from > req->to) {
3711 + DP("bad ip range");
3715 + if (req->to - req->from > MAX_RANGE) {
3716 + ip_set_printk("range too big (max %d addresses)",
3721 + map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3723 + DP("out of memory for %d bytes",
3724 + sizeof(struct ip_set_ipmap));
3727 + map->first_ip = req->from;
3728 + map->last_ip = req->to;
3729 + map->netmask = req->netmask;
3731 + if (req->netmask == 0xFFFFFFFF) {
3733 + map->sizeid = map->last_ip - map->first_ip + 1;
3735 + unsigned int mask_bits, netmask_bits;
3738 + map->first_ip &= map->netmask; /* Should we better bark? */
3740 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3741 + netmask_bits = mask_to_bits(map->netmask);
3743 + if (!mask || netmask_bits <= mask_bits)
3746 + map->hosts = 2 << (32 - netmask_bits - 1);
3747 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3749 + newbytes = bitmap_bytes(0, map->sizeid - 1);
3750 + map->members = kmalloc(newbytes, GFP_KERNEL);
3751 + if (!map->members) {
3752 + DP("out of memory for %d bytes", newbytes);
3756 + memset(map->members, 0, newbytes);
3762 +static void destroy(struct ip_set *set)
3764 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3766 + kfree(map->members);
3772 +static void flush(struct ip_set *set)
3774 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3775 + memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3778 +static void list_header(const struct ip_set *set, void *data)
3780 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3781 + struct ip_set_req_ipmap_create *header =
3782 + (struct ip_set_req_ipmap_create *) data;
3784 + header->from = map->first_ip;
3785 + header->to = map->last_ip;
3786 + header->netmask = map->netmask;
3789 +static int list_members_size(const struct ip_set *set)
3791 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3793 + return bitmap_bytes(0, map->sizeid - 1);
3796 +static void list_members(const struct ip_set *set, void *data)
3798 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3799 + int bytes = bitmap_bytes(0, map->sizeid - 1);
3801 + memcpy(data, map->members, bytes);
3804 +static struct ip_set_type ip_set_ipmap = {
3805 + .typename = SETTYPE_NAME,
3806 + .typecode = IPSET_TYPE_IP,
3807 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3808 + .create = &create,
3809 + .destroy = &destroy,
3811 + .reqsize = sizeof(struct ip_set_req_ipmap),
3813 + .addip_kernel = &addip_kernel,
3815 + .delip_kernel = &delip_kernel,
3816 + .testip = &testip,
3817 + .testip_kernel = &testip_kernel,
3818 + .header_size = sizeof(struct ip_set_req_ipmap_create),
3819 + .list_header = &list_header,
3820 + .list_members_size = &list_members_size,
3821 + .list_members = &list_members,
3822 + .me = THIS_MODULE,
3825 +MODULE_LICENSE("GPL");
3826 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3827 +MODULE_DESCRIPTION("ipmap type of IP sets");
3829 +static int __init init(void)
3831 + return ip_set_register_set_type(&ip_set_ipmap);
3834 +static void __exit fini(void)
3836 + /* FIXME: possible race with ip_set_create() */
3837 + ip_set_unregister_set_type(&ip_set_ipmap);
3842 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ip_set_iptree.c linux-2.6.19.dev/net/ipv4/netfilter/ip_set_iptree.c
3843 --- linux-2.6.19.old/net/ipv4/netfilter/ip_set_iptree.c 1970-01-01 01:00:00.000000000 +0100
3844 +++ linux-2.6.19.dev/net/ipv4/netfilter/ip_set_iptree.c 2006-12-14 03:13:43.000000000 +0100
3846 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3848 + * This program is free software; you can redistribute it and/or modify
3849 + * it under the terms of the GNU General Public License version 2 as
3850 + * published by the Free Software Foundation.
3853 +/* Kernel module implementing an IP set type: the iptree type */
3855 +#include <linux/module.h>
3856 +#include <linux/ip.h>
3857 +#include <linux/skbuff.h>
3858 +#include <linux/slab.h>
3859 +#include <linux/delay.h>
3860 +#include <linux/netfilter_ipv4/ip_tables.h>
3861 +#include <linux/netfilter_ipv4/ip_set.h>
3862 +#include <linux/errno.h>
3863 +#include <asm/uaccess.h>
3864 +#include <asm/bitops.h>
3865 +#include <linux/spinlock.h>
3867 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
3869 +/* Garbage collection interval in seconds: */
3870 +#define IPTREE_GC_TIME 5*60
3871 +/* Sleep so many milliseconds before trying again
3872 + * to delete the gc timer at destroying a set */
3873 +#define IPTREE_DESTROY_SLEEP 100
3875 +static kmem_cache_t *branch_cachep;
3876 +static kmem_cache_t *leaf_cachep;
3878 +#define ABCD(a,b,c,d,addrp) do { \
3879 + a = ((unsigned char *)addrp)[3]; \
3880 + b = ((unsigned char *)addrp)[2]; \
3881 + c = ((unsigned char *)addrp)[1]; \
3882 + d = ((unsigned char *)addrp)[0]; \
3885 +#define TESTIP_WALK(map, elem, branch) do { \
3886 + if ((map)->tree[elem]) { \
3887 + branch = (map)->tree[elem]; \
3893 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3895 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3896 + struct ip_set_iptreeb *btree;
3897 + struct ip_set_iptreec *ctree;
3898 + struct ip_set_iptreed *dtree;
3899 + unsigned char a,b,c,d;
3902 + ABCD(a, b, c, d, hash_ip);
3903 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
3904 + TESTIP_WALK(map, a, btree);
3905 + TESTIP_WALK(btree, b, ctree);
3906 + TESTIP_WALK(ctree, c, dtree);
3907 + DP("%lu %lu", dtree->expires[d], jiffies);
3908 + return !!(map->timeout ? (time_after(dtree->expires[d], jiffies))
3909 + : dtree->expires[d]);
3913 +testip(struct ip_set *set, const void *data, size_t size,
3914 + ip_set_ip_t *hash_ip)
3916 + struct ip_set_req_iptree *req =
3917 + (struct ip_set_req_iptree *) data;
3919 + if (size != sizeof(struct ip_set_req_iptree)) {
3920 + ip_set_printk("data length wrong (want %zu, have %zu)",
3921 + sizeof(struct ip_set_req_iptree),
3925 + return __testip(set, req->ip, hash_ip);
3929 +testip_kernel(struct ip_set *set,
3930 + const struct sk_buff *skb,
3932 + ip_set_ip_t *hash_ip)
3936 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3937 + flags & IPSET_SRC ? "SRC" : "DST",
3938 + NIPQUAD(skb->nh.iph->saddr),
3939 + NIPQUAD(skb->nh.iph->daddr));
3941 + res = __testip(set,
3942 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3943 + : skb->nh.iph->daddr),
3945 + return (res < 0 ? 0 : res);
3948 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
3949 + if ((map)->tree[elem]) { \
3950 + DP("found %u", elem); \
3951 + branch = (map)->tree[elem]; \
3953 + branch = (type *) \
3954 + kmem_cache_alloc(cachep, GFP_KERNEL); \
3955 + if (branch == NULL) \
3957 + memset(branch, 0, sizeof(*branch)); \
3958 + (map)->tree[elem] = branch; \
3959 + DP("alloc %u", elem); \
3964 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
3965 + ip_set_ip_t *hash_ip)
3967 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3968 + struct ip_set_iptreeb *btree;
3969 + struct ip_set_iptreec *ctree;
3970 + struct ip_set_iptreed *dtree;
3971 + unsigned char a,b,c,d;
3975 + ABCD(a, b, c, d, hash_ip);
3976 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
3977 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
3978 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
3979 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
3980 + if (dtree->expires[d]
3981 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
3983 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
3984 + DP("%u %lu", d, dtree->expires[d]);
3989 +addip(struct ip_set *set, const void *data, size_t size,
3990 + ip_set_ip_t *hash_ip)
3992 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3993 + struct ip_set_req_iptree *req =
3994 + (struct ip_set_req_iptree *) data;
3996 + if (size != sizeof(struct ip_set_req_iptree)) {
3997 + ip_set_printk("data length wrong (want %zu, have %zu)",
3998 + sizeof(struct ip_set_req_iptree),
4002 + DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
4003 + return __addip(set, req->ip,
4004 + req->timeout ? req->timeout : map->timeout,
4009 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4010 + u_int32_t flags, ip_set_ip_t *hash_ip)
4012 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4014 + return __addip(set,
4015 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4016 + : skb->nh.iph->daddr),
4021 +#define DELIP_WALK(map, elem, branch) do { \
4022 + if ((map)->tree[elem]) { \
4023 + branch = (map)->tree[elem]; \
4029 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4031 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4032 + struct ip_set_iptreeb *btree;
4033 + struct ip_set_iptreec *ctree;
4034 + struct ip_set_iptreed *dtree;
4035 + unsigned char a,b,c,d;
4038 + ABCD(a, b, c, d, hash_ip);
4039 + DELIP_WALK(map, a, btree);
4040 + DELIP_WALK(btree, b, ctree);
4041 + DELIP_WALK(ctree, c, dtree);
4043 + if (dtree->expires[d]) {
4044 + dtree->expires[d] = 0;
4051 +delip(struct ip_set *set, const void *data, size_t size,
4052 + ip_set_ip_t *hash_ip)
4054 + struct ip_set_req_iptree *req =
4055 + (struct ip_set_req_iptree *) data;
4057 + if (size != sizeof(struct ip_set_req_iptree)) {
4058 + ip_set_printk("data length wrong (want %zu, have %zu)",
4059 + sizeof(struct ip_set_req_iptree),
4063 + return __delip(set, req->ip, hash_ip);
4067 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4068 + u_int32_t flags, ip_set_ip_t *hash_ip)
4070 + return __delip(set,
4071 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4072 + : skb->nh.iph->daddr),
4076 +#define LOOP_WALK_BEGIN(map, i, branch) \
4077 + for (i = 0; i < 255; i++) { \
4078 + if (!(map)->tree[i]) \
4080 + branch = (map)->tree[i]
4082 +#define LOOP_WALK_END }
4084 +static void ip_tree_gc(unsigned long ul_set)
4086 + struct ip_set *set = (void *) ul_set;
4087 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4088 + struct ip_set_iptreeb *btree;
4089 + struct ip_set_iptreec *ctree;
4090 + struct ip_set_iptreed *dtree;
4091 + unsigned char a,b,c,d;
4092 + unsigned char i,j,k;
4095 + DP("gc: %s", set->name);
4096 + write_lock_bh(&set->lock);
4097 + LOOP_WALK_BEGIN(map, a, btree);
4098 + LOOP_WALK_BEGIN(btree, b, ctree);
4099 + LOOP_WALK_BEGIN(ctree, c, dtree);
4100 + for (d = 0; d < 255; d++) {
4101 + if (dtree->expires[d]) {
4102 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4104 + dtree->expires[d], jiffies);
4106 + && time_before(dtree->expires[d], jiffies))
4107 + dtree->expires[d] = 0;
4113 + DP("gc: %s: leaf %u %u %u empty",
4114 + set->name, a, b, c);
4115 + kmem_cache_free(leaf_cachep, dtree);
4116 + ctree->tree[c] = NULL;
4118 + DP("gc: %s: leaf %u %u %u not empty",
4119 + set->name, a, b, c);
4125 + DP("gc: %s: branch %u %u empty",
4127 + kmem_cache_free(branch_cachep, ctree);
4128 + btree->tree[b] = NULL;
4130 + DP("gc: %s: branch %u %u not empty",
4137 + DP("gc: %s: branch %u empty",
4139 + kmem_cache_free(branch_cachep, btree);
4140 + map->tree[a] = NULL;
4142 + DP("gc: %s: branch %u not empty",
4147 + write_unlock_bh(&set->lock);
4149 + map->gc.expires = jiffies + map->gc_interval * HZ;
4150 + add_timer(&map->gc);
4153 +static int create(struct ip_set *set, const void *data, size_t size)
4155 + struct ip_set_req_iptree_create *req =
4156 + (struct ip_set_req_iptree_create *) data;
4157 + struct ip_set_iptree *map;
4159 + if (size != sizeof(struct ip_set_req_iptree_create)) {
4160 + ip_set_printk("data length wrong (want %zu, have %zu)",
4161 + sizeof(struct ip_set_req_iptree_create),
4166 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4168 + DP("out of memory for %d bytes",
4169 + sizeof(struct ip_set_iptree));
4172 + memset(map, 0, sizeof(*map));
4173 + map->timeout = req->timeout;
4176 + /* If there is no timeout for the entries,
4177 + * we still have to call gc because delete
4178 + * do not clean up empty branches */
4179 + map->gc_interval = IPTREE_GC_TIME;
4180 + init_timer(&map->gc);
4181 + map->gc.data = (unsigned long) set;
4182 + map->gc.function = ip_tree_gc;
4183 + map->gc.expires = jiffies + map->gc_interval * HZ;
4184 + add_timer(&map->gc);
4189 +static void __flush(struct ip_set_iptree *map)
4191 + struct ip_set_iptreeb *btree;
4192 + struct ip_set_iptreec *ctree;
4193 + struct ip_set_iptreed *dtree;
4194 + unsigned int a,b,c;
4196 + LOOP_WALK_BEGIN(map, a, btree);
4197 + LOOP_WALK_BEGIN(btree, b, ctree);
4198 + LOOP_WALK_BEGIN(ctree, c, dtree);
4199 + kmem_cache_free(leaf_cachep, dtree);
4201 + kmem_cache_free(branch_cachep, ctree);
4203 + kmem_cache_free(branch_cachep, btree);
4207 +static void destroy(struct ip_set *set)
4209 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4211 + while (!del_timer(&map->gc))
4212 + msleep(IPTREE_DESTROY_SLEEP);
4218 +static void flush(struct ip_set *set)
4220 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4221 + unsigned int timeout = map->timeout;
4224 + memset(map, 0, sizeof(*map));
4225 + map->timeout = timeout;
4228 +static void list_header(const struct ip_set *set, void *data)
4230 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4231 + struct ip_set_req_iptree_create *header =
4232 + (struct ip_set_req_iptree_create *) data;
4234 + header->timeout = map->timeout;
4237 +static int list_members_size(const struct ip_set *set)
4239 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4240 + struct ip_set_iptreeb *btree;
4241 + struct ip_set_iptreec *ctree;
4242 + struct ip_set_iptreed *dtree;
4243 + unsigned char a,b,c,d;
4244 + unsigned int count = 0;
4246 + LOOP_WALK_BEGIN(map, a, btree);
4247 + LOOP_WALK_BEGIN(btree, b, ctree);
4248 + LOOP_WALK_BEGIN(ctree, c, dtree);
4249 + for (d = 0; d < 255; d++) {
4250 + if (dtree->expires[d]
4251 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4258 + DP("members %u", count);
4259 + return (count * sizeof(struct ip_set_req_iptree));
4262 +static void list_members(const struct ip_set *set, void *data)
4264 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4265 + struct ip_set_iptreeb *btree;
4266 + struct ip_set_iptreec *ctree;
4267 + struct ip_set_iptreed *dtree;
4268 + unsigned char a,b,c,d;
4269 + size_t offset = 0;
4270 + struct ip_set_req_iptree *entry;
4272 + LOOP_WALK_BEGIN(map, a, btree);
4273 + LOOP_WALK_BEGIN(btree, b, ctree);
4274 + LOOP_WALK_BEGIN(ctree, c, dtree);
4275 + for (d = 0; d < 255; d++) {
4276 + if (dtree->expires[d]
4277 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
4278 + entry = (struct ip_set_req_iptree *)(data + offset);
4279 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
4280 + entry->timeout = !map->timeout ? 0
4281 + : (dtree->expires[d] - jiffies)/HZ;
4282 + offset += sizeof(struct ip_set_req_iptree);
4290 +static struct ip_set_type ip_set_iptree = {
4291 + .typename = SETTYPE_NAME,
4292 + .typecode = IPSET_TYPE_IP,
4293 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4294 + .create = &create,
4295 + .destroy = &destroy,
4297 + .reqsize = sizeof(struct ip_set_req_iptree),
4299 + .addip_kernel = &addip_kernel,
4301 + .delip_kernel = &delip_kernel,
4302 + .testip = &testip,
4303 + .testip_kernel = &testip_kernel,
4304 + .header_size = sizeof(struct ip_set_req_iptree_create),
4305 + .list_header = &list_header,
4306 + .list_members_size = &list_members_size,
4307 + .list_members = &list_members,
4308 + .me = THIS_MODULE,
4311 +MODULE_LICENSE("GPL");
4312 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4313 +MODULE_DESCRIPTION("iptree type of IP sets");
4315 +static int __init init(void)
4319 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
4320 + sizeof(struct ip_set_iptreeb),
4321 + 0, 0, NULL, NULL);
4322 + if (!branch_cachep) {
4323 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
4327 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
4328 + sizeof(struct ip_set_iptreed),
4329 + 0, 0, NULL, NULL);
4330 + if (!leaf_cachep) {
4331 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
4335 + ret = ip_set_register_set_type(&ip_set_iptree);
4339 + kmem_cache_destroy(leaf_cachep);
4341 + kmem_cache_destroy(branch_cachep);
4346 +static void __exit fini(void)
4348 + /* FIXME: possible race with ip_set_create() */
4349 + ip_set_unregister_set_type(&ip_set_iptree);
4350 + kmem_cache_destroy(leaf_cachep);
4351 + kmem_cache_destroy(branch_cachep);
4356 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ip_set_macipmap.c linux-2.6.19.dev/net/ipv4/netfilter/ip_set_macipmap.c
4357 --- linux-2.6.19.old/net/ipv4/netfilter/ip_set_macipmap.c 1970-01-01 01:00:00.000000000 +0100
4358 +++ linux-2.6.19.dev/net/ipv4/netfilter/ip_set_macipmap.c 2006-12-14 03:13:43.000000000 +0100
4360 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
4361 + * Patrick Schaaf <bof@bof.de>
4362 + * Martin Josefsson <gandalf@wlug.westbo.se>
4363 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4365 + * This program is free software; you can redistribute it and/or modify
4366 + * it under the terms of the GNU General Public License version 2 as
4367 + * published by the Free Software Foundation.
4370 +/* Kernel module implementing an IP set type: the macipmap type */
4372 +#include <linux/module.h>
4373 +#include <linux/ip.h>
4374 +#include <linux/skbuff.h>
4375 +#include <linux/netfilter_ipv4/ip_tables.h>
4376 +#include <linux/netfilter_ipv4/ip_set.h>
4377 +#include <linux/errno.h>
4378 +#include <asm/uaccess.h>
4379 +#include <asm/bitops.h>
4380 +#include <linux/spinlock.h>
4381 +#include <linux/if_ether.h>
4382 +#include <linux/vmalloc.h>
4384 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4385 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
4388 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
4390 + struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
4391 + struct ip_set_macip *table = (struct ip_set_macip *) map->members;
4392 + struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
4394 + if (size != sizeof(struct ip_set_req_macipmap)) {
4395 + ip_set_printk("data length wrong (want %zu, have %zu)",
4396 + sizeof(struct ip_set_req_macipmap),
4401 + if (req->ip < map->first_ip || req->ip > map->last_ip)
4404 + *hash_ip = req->ip;
4405 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4406 + set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
4407 + if (test_bit(IPSET_MACIP_ISSET,
4408 + (void *) &table[req->ip - map->first_ip].flags)) {
4409 + return (memcmp(req->ethernet,
4410 + &table[req->ip - map->first_ip].ethernet,
4413 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4418 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4419 + u_int32_t flags, ip_set_ip_t *hash_ip)
4421 + struct ip_set_macipmap *map =
4422 + (struct ip_set_macipmap *) set->data;
4423 + struct ip_set_macip *table =
4424 + (struct ip_set_macip *) map->members;
4427 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4428 + : skb->nh.iph->daddr);
4429 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4430 + flags & IPSET_SRC ? "SRC" : "DST",
4431 + NIPQUAD(skb->nh.iph->saddr),
4432 + NIPQUAD(skb->nh.iph->daddr));
4434 + if (ip < map->first_ip || ip > map->last_ip)
4438 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4439 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
4440 + if (test_bit(IPSET_MACIP_ISSET,
4441 + (void *) &table[ip - map->first_ip].flags)) {
4442 + /* Is mac pointer valid?
4443 + * If so, compare... */
4444 + return (skb->mac.raw >= skb->head
4445 + && (skb->mac.raw + ETH_HLEN) <= skb->data
4446 + && (memcmp(eth_hdr(skb)->h_source,
4447 + &table[ip - map->first_ip].ethernet,
4450 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4454 +/* returns 0 on success */
4456 +__addip(struct ip_set *set,
4457 + ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
4459 + struct ip_set_macipmap *map =
4460 + (struct ip_set_macipmap *) set->data;
4461 + struct ip_set_macip *table =
4462 + (struct ip_set_macip *) map->members;
4464 + if (ip < map->first_ip || ip > map->last_ip)
4466 + if (test_and_set_bit(IPSET_MACIP_ISSET,
4467 + (void *) &table[ip - map->first_ip].flags))
4471 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4472 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
4477 +addip(struct ip_set *set, const void *data, size_t size,
4478 + ip_set_ip_t *hash_ip)
4480 + struct ip_set_req_macipmap *req =
4481 + (struct ip_set_req_macipmap *) data;
4483 + if (size != sizeof(struct ip_set_req_macipmap)) {
4484 + ip_set_printk("data length wrong (want %zu, have %zu)",
4485 + sizeof(struct ip_set_req_macipmap),
4489 + return __addip(set, req->ip, req->ethernet, hash_ip);
4493 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4494 + u_int32_t flags, ip_set_ip_t *hash_ip)
4498 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4499 + : skb->nh.iph->daddr);
4501 + if (!(skb->mac.raw >= skb->head
4502 + && (skb->mac.raw + ETH_HLEN) <= skb->data))
4505 + return __addip(set, ip, eth_hdr(skb)->h_source, hash_ip);
4509 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4511 + struct ip_set_macipmap *map =
4512 + (struct ip_set_macipmap *) set->data;
4513 + struct ip_set_macip *table =
4514 + (struct ip_set_macip *) map->members;
4516 + if (ip < map->first_ip || ip > map->last_ip)
4518 + if (!test_and_clear_bit(IPSET_MACIP_ISSET,
4519 + (void *)&table[ip - map->first_ip].flags))
4523 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4528 +delip(struct ip_set *set, const void *data, size_t size,
4529 + ip_set_ip_t *hash_ip)
4531 + struct ip_set_req_macipmap *req =
4532 + (struct ip_set_req_macipmap *) data;
4534 + if (size != sizeof(struct ip_set_req_macipmap)) {
4535 + ip_set_printk("data length wrong (want %zu, have %zu)",
4536 + sizeof(struct ip_set_req_macipmap),
4540 + return __delip(set, req->ip, hash_ip);
4544 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4545 + u_int32_t flags, ip_set_ip_t *hash_ip)
4547 + return __delip(set,
4548 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4549 + : skb->nh.iph->daddr),
4553 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
4555 + return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
4558 +static int create(struct ip_set *set, const void *data, size_t size)
4561 + struct ip_set_req_macipmap_create *req =
4562 + (struct ip_set_req_macipmap_create *) data;
4563 + struct ip_set_macipmap *map;
4565 + if (size != sizeof(struct ip_set_req_macipmap_create)) {
4566 + ip_set_printk("data length wrong (want %zu, have %zu)",
4567 + sizeof(struct ip_set_req_macipmap_create),
4572 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
4573 + HIPQUAD(req->from), HIPQUAD(req->to));
4575 + if (req->from > req->to) {
4576 + DP("bad ip range");
4580 + if (req->to - req->from > MAX_RANGE) {
4581 + ip_set_printk("range too big (max %d addresses)",
4586 + map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
4588 + DP("out of memory for %d bytes",
4589 + sizeof(struct ip_set_macipmap));
4592 + map->flags = req->flags;
4593 + map->first_ip = req->from;
4594 + map->last_ip = req->to;
4595 + newbytes = members_size(map->first_ip, map->last_ip);
4596 + map->members = ip_set_malloc(newbytes);
4597 + if (!map->members) {
4598 + DP("out of memory for %d bytes", newbytes);
4602 + memset(map->members, 0, newbytes);
4608 +static void destroy(struct ip_set *set)
4610 + struct ip_set_macipmap *map =
4611 + (struct ip_set_macipmap *) set->data;
4613 + ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
4619 +static void flush(struct ip_set *set)
4621 + struct ip_set_macipmap *map =
4622 + (struct ip_set_macipmap *) set->data;
4623 + memset(map->members, 0, members_size(map->first_ip, map->last_ip));
4626 +static void list_header(const struct ip_set *set, void *data)
4628 + struct ip_set_macipmap *map =
4629 + (struct ip_set_macipmap *) set->data;
4630 + struct ip_set_req_macipmap_create *header =
4631 + (struct ip_set_req_macipmap_create *) data;
4633 + DP("list_header %x %x %u", map->first_ip, map->last_ip,
4636 + header->from = map->first_ip;
4637 + header->to = map->last_ip;
4638 + header->flags = map->flags;
4641 +static int list_members_size(const struct ip_set *set)
4643 + struct ip_set_macipmap *map =
4644 + (struct ip_set_macipmap *) set->data;
4646 + return members_size(map->first_ip, map->last_ip);
4649 +static void list_members(const struct ip_set *set, void *data)
4651 + struct ip_set_macipmap *map =
4652 + (struct ip_set_macipmap *) set->data;
4654 + int bytes = members_size(map->first_ip, map->last_ip);
4656 + memcpy(data, map->members, bytes);
4659 +static struct ip_set_type ip_set_macipmap = {
4660 + .typename = SETTYPE_NAME,
4661 + .typecode = IPSET_TYPE_IP,
4662 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4663 + .create = &create,
4664 + .destroy = &destroy,
4666 + .reqsize = sizeof(struct ip_set_req_macipmap),
4668 + .addip_kernel = &addip_kernel,
4670 + .delip_kernel = &delip_kernel,
4671 + .testip = &testip,
4672 + .testip_kernel = &testip_kernel,
4673 + .header_size = sizeof(struct ip_set_req_macipmap_create),
4674 + .list_header = &list_header,
4675 + .list_members_size = &list_members_size,
4676 + .list_members = &list_members,
4677 + .me = THIS_MODULE,
4680 +MODULE_LICENSE("GPL");
4681 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4682 +MODULE_DESCRIPTION("macipmap type of IP sets");
4684 +static int __init init(void)
4686 + init_max_malloc_size();
4687 + return ip_set_register_set_type(&ip_set_macipmap);
4690 +static void __exit fini(void)
4692 + /* FIXME: possible race with ip_set_create() */
4693 + ip_set_unregister_set_type(&ip_set_macipmap);
4698 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ip_set_nethash.c linux-2.6.19.dev/net/ipv4/netfilter/ip_set_nethash.c
4699 --- linux-2.6.19.old/net/ipv4/netfilter/ip_set_nethash.c 1970-01-01 01:00:00.000000000 +0100
4700 +++ linux-2.6.19.dev/net/ipv4/netfilter/ip_set_nethash.c 2006-12-14 03:13:43.000000000 +0100
4702 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4704 + * This program is free software; you can redistribute it and/or modify
4705 + * it under the terms of the GNU General Public License version 2 as
4706 + * published by the Free Software Foundation.
4709 +/* Kernel module implementing a cidr nethash set */
4711 +#include <linux/module.h>
4712 +#include <linux/ip.h>
4713 +#include <linux/skbuff.h>
4714 +#include <linux/netfilter_ipv4/ip_tables.h>
4715 +#include <linux/netfilter_ipv4/ip_set.h>
4716 +#include <linux/errno.h>
4717 +#include <asm/uaccess.h>
4718 +#include <asm/bitops.h>
4719 +#include <linux/spinlock.h>
4720 +#include <linux/vmalloc.h>
4721 +#include <linux/random.h>
4723 +#include <net/ip.h>
4725 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4726 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
4727 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
4728 +#include <linux/netfilter_ipv4/ip_set_prime.h>
4730 +static inline __u32
4731 +jhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4733 + return jhash_1word(ip, map->initval);
4736 +static inline __u32
4737 +randhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4739 + return (1 + ip % map->prime);
4742 +static inline __u32
4743 +hash_id_cidr(struct ip_set_nethash *map,
4745 + unsigned char cidr,
4746 + ip_set_ip_t *hash_ip)
4748 + __u32 jhash, randhash, id;
4751 + *hash_ip = pack(ip, cidr);
4752 + jhash = jhash_ip(map, *hash_ip);
4753 + randhash = randhash_ip(map, *hash_ip);
4755 + for (i = 0; i < map->probes; i++) {
4756 + id = (jhash + i * randhash) % map->hashsize;
4757 + DP("hash key: %u", id);
4758 + if (map->members[id] == *hash_ip)
4764 +static inline __u32
4765 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4767 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4768 + __u32 id = UINT_MAX;
4771 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4772 + id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
4773 + if (id != UINT_MAX)
4780 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
4781 + ip_set_ip_t *hash_ip)
4783 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4785 + return (hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
4789 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4791 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
4795 +testip(struct ip_set *set, const void *data, size_t size,
4796 + ip_set_ip_t *hash_ip)
4798 + struct ip_set_req_nethash *req =
4799 + (struct ip_set_req_nethash *) data;
4801 + if (size != sizeof(struct ip_set_req_nethash)) {
4802 + ip_set_printk("data length wrong (want %zu, have %zu)",
4803 + sizeof(struct ip_set_req_nethash),
4807 + return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
4808 + : __testip_cidr(set, req->ip, req->cidr, hash_ip));
4812 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4813 + u_int32_t flags, ip_set_ip_t *hash_ip)
4815 + return __testip(set,
4816 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4817 + : skb->nh.iph->daddr),
4822 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
4824 + __u32 jhash, randhash, probe;
4827 + jhash = jhash_ip(map, ip);
4828 + randhash = randhash_ip(map, ip);
4830 + for (i = 0; i < map->probes; i++) {
4831 + probe = (jhash + i * randhash) % map->hashsize;
4832 + if (map->members[probe] == ip)
4834 + if (!map->members[probe]) {
4835 + map->members[probe] = ip;
4839 + /* Trigger rehashing */
4844 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4845 + ip_set_ip_t *hash_ip)
4847 + *hash_ip = pack(ip, cidr);
4848 + DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
4850 + return __addip_base(map, *hash_ip);
4854 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
4856 + unsigned char next;
4859 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4860 + if (map->cidr[i] == cidr) {
4862 + } else if (map->cidr[i] < cidr) {
4863 + next = map->cidr[i];
4864 + map->cidr[i] = cidr;
4869 + map->cidr[i] = cidr;
4873 +addip(struct ip_set *set, const void *data, size_t size,
4874 + ip_set_ip_t *hash_ip)
4876 + struct ip_set_req_nethash *req =
4877 + (struct ip_set_req_nethash *) data;
4880 + if (size != sizeof(struct ip_set_req_nethash)) {
4881 + ip_set_printk("data length wrong (want %zu, have %zu)",
4882 + sizeof(struct ip_set_req_nethash),
4886 + ret = __addip((struct ip_set_nethash *) set->data,
4887 + req->ip, req->cidr, hash_ip);
4890 + update_cidr_sizes((struct ip_set_nethash *) set->data,
4897 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4898 + u_int32_t flags, ip_set_ip_t *hash_ip)
4900 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4901 + int ret = -ERANGE;
4902 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4903 + : skb->nh.iph->daddr);
4906 + ret = __addip(map, ip, map->cidr[0], hash_ip);
4911 +static int retry(struct ip_set *set)
4913 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4914 + ip_set_ip_t *members;
4915 + u_int32_t i, hashsize;
4916 + unsigned newbytes;
4918 + struct ip_set_nethash tmp = {
4919 + .hashsize = map->hashsize,
4920 + .probes = map->probes,
4921 + .resize = map->resize
4924 + if (map->resize == 0)
4927 + memcpy(tmp.cidr, map->cidr, 30 * sizeof(unsigned char));
4931 + /* Calculate new parameters */
4932 + get_random_bytes(&tmp.initval, 4);
4933 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
4934 + if (hashsize == tmp.hashsize)
4936 + tmp.prime = make_prime(hashsize);
4938 + ip_set_printk("rehashing of set %s triggered: "
4939 + "hashsize grows from %u to %u",
4940 + set->name, tmp.hashsize, hashsize);
4941 + tmp.hashsize = hashsize;
4943 + newbytes = hashsize * sizeof(ip_set_ip_t);
4944 + tmp.members = ip_set_malloc_atomic(newbytes);
4945 + if (!tmp.members) {
4946 + DP("out of memory for %d bytes", newbytes);
4949 + memset(tmp.members, 0, newbytes);
4951 + write_lock_bh(&set->lock);
4952 + map = (struct ip_set_nethash *) set->data; /* Play safe */
4953 + for (i = 0; i < map->hashsize && res == 0; i++) {
4954 + if (map->members[i])
4955 + res = __addip_base(&tmp, map->members[i]);
4958 + /* Failure, try again */
4959 + write_unlock_bh(&set->lock);
4960 + ip_set_free(tmp.members, newbytes);
4964 + /* Success at resizing! */
4965 + members = map->members;
4966 + hashsize = map->hashsize;
4968 + map->initval = tmp.initval;
4969 + map->prime = tmp.prime;
4970 + map->hashsize = tmp.hashsize;
4971 + map->members = tmp.members;
4972 + write_unlock_bh(&set->lock);
4974 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
4980 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4981 + ip_set_ip_t *hash_ip)
4983 + ip_set_ip_t id = hash_id_cidr(map, ip, cidr, hash_ip);
4985 + if (id == UINT_MAX)
4988 + map->members[id] = 0;
4993 +delip(struct ip_set *set, const void *data, size_t size,
4994 + ip_set_ip_t *hash_ip)
4996 + struct ip_set_req_nethash *req =
4997 + (struct ip_set_req_nethash *) data;
4999 + if (size != sizeof(struct ip_set_req_nethash)) {
5000 + ip_set_printk("data length wrong (want %zu, have %zu)",
5001 + sizeof(struct ip_set_req_nethash),
5005 + /* TODO: no garbage collection in map->cidr */
5006 + return __delip((struct ip_set_nethash *) set->data,
5007 + req->ip, req->cidr, hash_ip);
5011 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
5012 + u_int32_t flags, ip_set_ip_t *hash_ip)
5014 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5015 + int ret = -ERANGE;
5016 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
5017 + : skb->nh.iph->daddr);
5020 + ret = __delip(map, ip, map->cidr[0], hash_ip);
5025 +static int create(struct ip_set *set, const void *data, size_t size)
5027 + unsigned newbytes;
5028 + struct ip_set_req_nethash_create *req =
5029 + (struct ip_set_req_nethash_create *) data;
5030 + struct ip_set_nethash *map;
5032 + if (size != sizeof(struct ip_set_req_nethash_create)) {
5033 + ip_set_printk("data length wrong (want %zu, have %zu)",
5034 + sizeof(struct ip_set_req_nethash_create),
5039 + if (req->hashsize < 1) {
5040 + ip_set_printk("hashsize too small");
5044 + map = kmalloc(sizeof(struct ip_set_nethash), GFP_KERNEL);
5046 + DP("out of memory for %d bytes",
5047 + sizeof(struct ip_set_nethash));
5050 + get_random_bytes(&map->initval, 4);
5051 + map->prime = make_prime(req->hashsize);
5052 + map->hashsize = req->hashsize;
5053 + map->probes = req->probes;
5054 + map->resize = req->resize;
5055 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
5056 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
5057 + map->members = ip_set_malloc(newbytes);
5058 + if (!map->members) {
5059 + DP("out of memory for %d bytes", newbytes);
5063 + memset(map->members, 0, newbytes);
5069 +static void destroy(struct ip_set *set)
5071 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5073 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
5079 +static void flush(struct ip_set *set)
5081 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5082 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
5083 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
5086 +static void list_header(const struct ip_set *set, void *data)
5088 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5089 + struct ip_set_req_nethash_create *header =
5090 + (struct ip_set_req_nethash_create *) data;
5092 + header->hashsize = map->hashsize;
5093 + header->probes = map->probes;
5094 + header->resize = map->resize;
5097 +static int list_members_size(const struct ip_set *set)
5099 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5101 + return (map->hashsize * sizeof(ip_set_ip_t));
5104 +static void list_members(const struct ip_set *set, void *data)
5106 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5107 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
5109 + memcpy(data, map->members, bytes);
5112 +static struct ip_set_type ip_set_nethash = {
5113 + .typename = SETTYPE_NAME,
5114 + .typecode = IPSET_TYPE_IP,
5115 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5116 + .create = &create,
5117 + .destroy = &destroy,
5119 + .reqsize = sizeof(struct ip_set_req_nethash),
5121 + .addip_kernel = &addip_kernel,
5124 + .delip_kernel = &delip_kernel,
5125 + .testip = &testip,
5126 + .testip_kernel = &testip_kernel,
5127 + .header_size = sizeof(struct ip_set_req_nethash_create),
5128 + .list_header = &list_header,
5129 + .list_members_size = &list_members_size,
5130 + .list_members = &list_members,
5131 + .me = THIS_MODULE,
5134 +MODULE_LICENSE("GPL");
5135 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5136 +MODULE_DESCRIPTION("nethash type of IP sets");
5138 +static int __init init(void)
5140 + return ip_set_register_set_type(&ip_set_nethash);
5143 +static void __exit fini(void)
5145 + /* FIXME: possible race with ip_set_create() */
5146 + ip_set_unregister_set_type(&ip_set_nethash);
5151 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ip_set_portmap.c linux-2.6.19.dev/net/ipv4/netfilter/ip_set_portmap.c
5152 --- linux-2.6.19.old/net/ipv4/netfilter/ip_set_portmap.c 1970-01-01 01:00:00.000000000 +0100
5153 +++ linux-2.6.19.dev/net/ipv4/netfilter/ip_set_portmap.c 2006-12-14 03:13:43.000000000 +0100
5155 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5157 + * This program is free software; you can redistribute it and/or modify
5158 + * it under the terms of the GNU General Public License version 2 as
5159 + * published by the Free Software Foundation.
5162 +/* Kernel module implementing a port set type as a bitmap */
5164 +#include <linux/module.h>
5165 +#include <linux/ip.h>
5166 +#include <linux/tcp.h>
5167 +#include <linux/udp.h>
5168 +#include <linux/skbuff.h>
5169 +#include <linux/netfilter_ipv4/ip_tables.h>
5170 +#include <linux/netfilter_ipv4/ip_set.h>
5171 +#include <linux/errno.h>
5172 +#include <asm/uaccess.h>
5173 +#include <asm/bitops.h>
5174 +#include <linux/spinlock.h>
5176 +#include <net/ip.h>
5178 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
5180 +/* We must handle non-linear skbs */
5181 +static inline ip_set_ip_t
5182 +get_port(const struct sk_buff *skb, u_int32_t flags)
5184 + struct iphdr *iph = skb->nh.iph;
5185 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
5187 + switch (iph->protocol) {
5188 + case IPPROTO_TCP: {
5189 + struct tcphdr tcph;
5191 + /* See comments at tcp_match in ip_tables.c */
5193 + return INVALID_PORT;
5195 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
5196 + /* No choice either */
5197 + return INVALID_PORT;
5199 + return ntohs(flags & IPSET_SRC ?
5200 + tcph.source : tcph.dest);
5202 + case IPPROTO_UDP: {
5203 + struct udphdr udph;
5206 + return INVALID_PORT;
5208 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
5209 + /* No choice either */
5210 + return INVALID_PORT;
5212 + return ntohs(flags & IPSET_SRC ?
5213 + udph.source : udph.dest);
5216 + return INVALID_PORT;
5221 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5223 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5225 + if (port < map->first_port || port > map->last_port)
5228 + *hash_port = port;
5229 + DP("set: %s, port:%u, %u", set->name, port, *hash_port);
5230 + return !!test_bit(port - map->first_port, map->members);
5234 +testport(struct ip_set *set, const void *data, size_t size,
5235 + ip_set_ip_t *hash_port)
5237 + struct ip_set_req_portmap *req =
5238 + (struct ip_set_req_portmap *) data;
5240 + if (size != sizeof(struct ip_set_req_portmap)) {
5241 + ip_set_printk("data length wrong (want %zu, have %zu)",
5242 + sizeof(struct ip_set_req_portmap),
5246 + return __testport(set, req->port, hash_port);
5250 +testport_kernel(struct ip_set *set, const struct sk_buff *skb,
5251 + u_int32_t flags, ip_set_ip_t *hash_port)
5254 + ip_set_ip_t port = get_port(skb, flags);
5256 + DP("flag %s port %u", flags & IPSET_SRC ? "SRC" : "DST", port);
5257 + if (port == INVALID_PORT)
5260 + res = __testport(set, port, hash_port);
5262 + return (res < 0 ? 0 : res);
5266 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5268 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5270 + if (port < map->first_port || port > map->last_port)
5272 + if (test_and_set_bit(port - map->first_port, map->members))
5275 + *hash_port = port;
5276 + DP("port %u", port);
5281 +addport(struct ip_set *set, const void *data, size_t size,
5282 + ip_set_ip_t *hash_port)
5284 + struct ip_set_req_portmap *req =
5285 + (struct ip_set_req_portmap *) data;
5287 + if (size != sizeof(struct ip_set_req_portmap)) {
5288 + ip_set_printk("data length wrong (want %zu, have %zu)",
5289 + sizeof(struct ip_set_req_portmap),
5293 + return __addport(set, req->port, hash_port);
5297 +addport_kernel(struct ip_set *set, const struct sk_buff *skb,
5298 + u_int32_t flags, ip_set_ip_t *hash_port)
5300 + ip_set_ip_t port = get_port(skb, flags);
5302 + if (port == INVALID_PORT)
5305 + return __addport(set, port, hash_port);
5309 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5311 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5313 + if (port < map->first_port || port > map->last_port)
5315 + if (!test_and_clear_bit(port - map->first_port, map->members))
5318 + *hash_port = port;
5319 + DP("port %u", port);
5324 +delport(struct ip_set *set, const void *data, size_t size,
5325 + ip_set_ip_t *hash_port)
5327 + struct ip_set_req_portmap *req =
5328 + (struct ip_set_req_portmap *) data;
5330 + if (size != sizeof(struct ip_set_req_portmap)) {
5331 + ip_set_printk("data length wrong (want %zu, have %zu)",
5332 + sizeof(struct ip_set_req_portmap),
5336 + return __delport(set, req->port, hash_port);
5340 +delport_kernel(struct ip_set *set, const struct sk_buff *skb,
5341 + u_int32_t flags, ip_set_ip_t *hash_port)
5343 + ip_set_ip_t port = get_port(skb, flags);
5345 + if (port == INVALID_PORT)
5348 + return __delport(set, port, hash_port);
5351 +static int create(struct ip_set *set, const void *data, size_t size)
5354 + struct ip_set_req_portmap_create *req =
5355 + (struct ip_set_req_portmap_create *) data;
5356 + struct ip_set_portmap *map;
5358 + if (size != sizeof(struct ip_set_req_portmap_create)) {
5359 + ip_set_printk("data length wrong (want %zu, have %zu)",
5360 + sizeof(struct ip_set_req_portmap_create),
5365 + DP("from %u to %u", req->from, req->to);
5367 + if (req->from > req->to) {
5368 + DP("bad port range");
5372 + if (req->to - req->from > MAX_RANGE) {
5373 + ip_set_printk("range too big (max %d ports)",
5378 + map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
5380 + DP("out of memory for %d bytes",
5381 + sizeof(struct ip_set_portmap));
5384 + map->first_port = req->from;
5385 + map->last_port = req->to;
5386 + newbytes = bitmap_bytes(req->from, req->to);
5387 + map->members = kmalloc(newbytes, GFP_KERNEL);
5388 + if (!map->members) {
5389 + DP("out of memory for %d bytes", newbytes);
5393 + memset(map->members, 0, newbytes);
5399 +static void destroy(struct ip_set *set)
5401 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5403 + kfree(map->members);
5409 +static void flush(struct ip_set *set)
5411 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5412 + memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
5415 +static void list_header(const struct ip_set *set, void *data)
5417 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5418 + struct ip_set_req_portmap_create *header =
5419 + (struct ip_set_req_portmap_create *) data;
5421 + DP("list_header %u %u", map->first_port, map->last_port);
5423 + header->from = map->first_port;
5424 + header->to = map->last_port;
5427 +static int list_members_size(const struct ip_set *set)
5429 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5431 + return bitmap_bytes(map->first_port, map->last_port);
5434 +static void list_members(const struct ip_set *set, void *data)
5436 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5437 + int bytes = bitmap_bytes(map->first_port, map->last_port);
5439 + memcpy(data, map->members, bytes);
5442 +static struct ip_set_type ip_set_portmap = {
5443 + .typename = SETTYPE_NAME,
5444 + .typecode = IPSET_TYPE_PORT,
5445 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5446 + .create = &create,
5447 + .destroy = &destroy,
5449 + .reqsize = sizeof(struct ip_set_req_portmap),
5450 + .addip = &addport,
5451 + .addip_kernel = &addport_kernel,
5452 + .delip = &delport,
5453 + .delip_kernel = &delport_kernel,
5454 + .testip = &testport,
5455 + .testip_kernel = &testport_kernel,
5456 + .header_size = sizeof(struct ip_set_req_portmap_create),
5457 + .list_header = &list_header,
5458 + .list_members_size = &list_members_size,
5459 + .list_members = &list_members,
5460 + .me = THIS_MODULE,
5463 +MODULE_LICENSE("GPL");
5464 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5465 +MODULE_DESCRIPTION("portmap type of IP sets");
5467 +static int __init init(void)
5469 + return ip_set_register_set_type(&ip_set_portmap);
5472 +static void __exit fini(void)
5474 + /* FIXME: possible race with ip_set_create() */
5475 + ip_set_unregister_set_type(&ip_set_portmap);
5480 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ipt_set.c linux-2.6.19.dev/net/ipv4/netfilter/ipt_set.c
5481 --- linux-2.6.19.old/net/ipv4/netfilter/ipt_set.c 1970-01-01 01:00:00.000000000 +0100
5482 +++ linux-2.6.19.dev/net/ipv4/netfilter/ipt_set.c 2006-12-14 03:13:43.000000000 +0100
5484 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5485 + * Patrick Schaaf <bof@bof.de>
5486 + * Martin Josefsson <gandalf@wlug.westbo.se>
5487 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5489 + * This program is free software; you can redistribute it and/or modify
5490 + * it under the terms of the GNU General Public License version 2 as
5491 + * published by the Free Software Foundation.
5494 +/* Kernel module to match an IP set. */
5496 +#include <linux/module.h>
5497 +#include <linux/ip.h>
5498 +#include <linux/skbuff.h>
5500 +#include <linux/netfilter_ipv4/ip_tables.h>
5501 +#include <linux/netfilter_ipv4/ip_set.h>
5502 +#include <linux/netfilter_ipv4/ipt_set.h>
5505 +match_set(const struct ipt_set_info *info,
5506 + const struct sk_buff *skb,
5509 + if (ip_set_testip_kernel(info->index, skb, info->flags))
5515 +match(const struct sk_buff *skb,
5516 + const struct net_device *in,
5517 + const struct net_device *out,
5518 + const struct xt_match *match,
5519 + const void *matchinfo,
5521 + unsigned int protoff,
5524 + const struct ipt_set_info_match *info = matchinfo;
5526 + return match_set(&info->match_set,
5528 + info->match_set.flags[0] & IPSET_MATCH_INV);
5532 +checkentry(const char *tablename,
5534 + const struct xt_match *match,
5536 + unsigned int hook_mask)
5538 + struct ipt_set_info_match *info =
5539 + (struct ipt_set_info_match *) matchinfo;
5540 + ip_set_id_t index;
5542 + index = ip_set_get_byindex(info->match_set.index);
5544 + if (index == IP_SET_INVALID_ID) {
5545 + ip_set_printk("Cannot find set indentified by id %u to match",
5546 + info->match_set.index);
5547 + return 0; /* error */
5549 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5550 + ip_set_printk("That's nasty!");
5551 + return 0; /* error */
5557 +static void destroy(const struct xt_match *match, void *matchinfo)
5559 + struct ipt_set_info_match *info = matchinfo;
5561 + ip_set_put(info->match_set.index);
5564 +static struct ipt_match set_match = {
5567 + .matchsize = sizeof(struct ipt_set_info_match),
5568 + .checkentry = &checkentry,
5569 + .destroy = &destroy,
5573 +MODULE_LICENSE("GPL");
5574 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5575 +MODULE_DESCRIPTION("iptables IP set match module");
5577 +static int __init init(void)
5579 + return ipt_register_match(&set_match);
5582 +static void __exit fini(void)
5584 + ipt_unregister_match(&set_match);
5589 diff -urN linux-2.6.19.old/net/ipv4/netfilter/ipt_SET.c linux-2.6.19.dev/net/ipv4/netfilter/ipt_SET.c
5590 --- linux-2.6.19.old/net/ipv4/netfilter/ipt_SET.c 1970-01-01 01:00:00.000000000 +0100
5591 +++ linux-2.6.19.dev/net/ipv4/netfilter/ipt_SET.c 2006-12-14 03:13:43.000000000 +0100
5593 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5594 + * Patrick Schaaf <bof@bof.de>
5595 + * Martin Josefsson <gandalf@wlug.westbo.se>
5596 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5598 + * This program is free software; you can redistribute it and/or modify
5599 + * it under the terms of the GNU General Public License version 2 as
5600 + * published by the Free Software Foundation.
5603 +/* ipt_SET.c - netfilter target to manipulate IP sets */
5605 +#include <linux/types.h>
5606 +#include <linux/ip.h>
5607 +#include <linux/timer.h>
5608 +#include <linux/module.h>
5609 +#include <linux/netfilter.h>
5610 +#include <linux/netdevice.h>
5611 +#include <linux/if.h>
5612 +#include <linux/inetdevice.h>
5613 +#include <net/protocol.h>
5614 +#include <net/checksum.h>
5615 +#include <linux/netfilter_ipv4.h>
5616 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
5617 +#include <linux/netfilter_ipv4/ipt_set.h>
5619 +static unsigned int
5620 +target(struct sk_buff **pskb,
5621 + const struct net_device *in,
5622 + const struct net_device *out,
5623 + unsigned int hooknum,
5624 + const struct xt_target *target,
5625 + const void *targinfo)
5627 + const struct ipt_set_info_target *info = targinfo;
5629 + if (info->add_set.index != IP_SET_INVALID_ID)
5630 + ip_set_addip_kernel(info->add_set.index,
5632 + info->add_set.flags);
5633 + if (info->del_set.index != IP_SET_INVALID_ID)
5634 + ip_set_delip_kernel(info->del_set.index,
5636 + info->del_set.flags);
5638 + return IPT_CONTINUE;
5642 +checkentry(const char *tablename,
5644 + const struct xt_target *target,
5646 + unsigned int hook_mask)
5648 + struct ipt_set_info_target *info =
5649 + (struct ipt_set_info_target *) targinfo;
5650 + ip_set_id_t index;
5652 + if (info->add_set.index != IP_SET_INVALID_ID) {
5653 + index = ip_set_get_byindex(info->add_set.index);
5654 + if (index == IP_SET_INVALID_ID) {
5655 + ip_set_printk("cannot find add_set index %u as target",
5656 + info->add_set.index);
5657 + return 0; /* error */
5661 + if (info->del_set.index != IP_SET_INVALID_ID) {
5662 + index = ip_set_get_byindex(info->del_set.index);
5663 + if (index == IP_SET_INVALID_ID) {
5664 + ip_set_printk("cannot find del_set index %u as target",
5665 + info->del_set.index);
5666 + return 0; /* error */
5669 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
5670 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5671 + ip_set_printk("That's nasty!");
5672 + return 0; /* error */
5678 +static void destroy(const struct xt_target *target, void *targetinfo)
5680 + struct ipt_set_info_target *info = targetinfo;
5682 + if (info->add_set.index != IP_SET_INVALID_ID)
5683 + ip_set_put(info->add_set.index);
5684 + if (info->del_set.index != IP_SET_INVALID_ID)
5685 + ip_set_put(info->del_set.index);
5688 +static struct ipt_target SET_target = {
5691 + .targetsize = sizeof(struct ipt_set_info_target),
5692 + .checkentry = checkentry,
5693 + .destroy = destroy,
5697 +MODULE_LICENSE("GPL");
5698 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5699 +MODULE_DESCRIPTION("iptables IP set target module");
5701 +static int __init init(void)
5703 + return ipt_register_target(&SET_target);
5706 +static void __exit fini(void)
5708 + ipt_unregister_target(&SET_target);
5713 diff -urN linux-2.6.19.old/net/ipv4/netfilter/Kconfig linux-2.6.19.dev/net/ipv4/netfilter/Kconfig
5714 --- linux-2.6.19.old/net/ipv4/netfilter/Kconfig 2006-12-14 03:13:41.000000000 +0100
5715 +++ linux-2.6.19.dev/net/ipv4/netfilter/Kconfig 2006-12-14 03:13:43.000000000 +0100
5716 @@ -647,5 +647,106 @@
5717 Allows altering the ARP packet payload: source and destination
5718 hardware and network addresses.
5721 + tristate "IP set support"
5722 + depends on INET && NETFILTER
5724 + This option adds IP set support to the kernel.
5725 + In order to define and use sets, you need the userspace utility
5728 + To compile it as a module, choose M here. If unsure, say N.
5730 +config IP_NF_SET_MAX
5731 + int "Maximum number of IP sets"
5734 + depends on IP_NF_SET
5736 + You can define here default value of the maximum number
5737 + of IP sets for the kernel.
5739 + The value can be overriden by the 'max_sets' module
5740 + parameter of the 'ip_set' module.
5742 +config IP_NF_SET_HASHSIZE
5743 + int "Hash size for bindings of IP sets"
5745 + depends on IP_NF_SET
5747 + You can define here default value of the hash size for
5748 + bindings of IP sets.
5750 + The value can be overriden by the 'hash_size' module
5751 + parameter of the 'ip_set' module.
5753 +config IP_NF_SET_IPMAP
5754 + tristate "ipmap set support"
5755 + depends on IP_NF_SET
5757 + This option adds the ipmap set type support.
5759 + To compile it as a module, choose M here. If unsure, say N.
5761 +config IP_NF_SET_MACIPMAP
5762 + tristate "macipmap set support"
5763 + depends on IP_NF_SET
5765 + This option adds the macipmap set type support.
5767 + To compile it as a module, choose M here. If unsure, say N.
5769 +config IP_NF_SET_PORTMAP
5770 + tristate "portmap set support"
5771 + depends on IP_NF_SET
5773 + This option adds the portmap set type support.
5775 + To compile it as a module, choose M here. If unsure, say N.
5777 +config IP_NF_SET_IPHASH
5778 + tristate "iphash set support"
5779 + depends on IP_NF_SET
5781 + This option adds the iphash set type support.
5783 + To compile it as a module, choose M here. If unsure, say N.
5785 +config IP_NF_SET_NETHASH
5786 + tristate "nethash set support"
5787 + depends on IP_NF_SET
5789 + This option adds the nethash set type support.
5791 + To compile it as a module, choose M here. If unsure, say N.
5793 +config IP_NF_SET_IPTREE
5794 + tristate "iptree set support"
5795 + depends on IP_NF_SET
5797 + This option adds the iptree set type support.
5799 + To compile it as a module, choose M here. If unsure, say N.
5801 +config IP_NF_MATCH_SET
5802 + tristate "set match support"
5803 + depends on IP_NF_SET
5805 + Set matching matches against given IP sets.
5806 + You need the ipset utility to create and set up the sets.
5808 + To compile it as a module, choose M here. If unsure, say N.
5810 +config IP_NF_TARGET_SET
5811 + tristate "SET target support"
5812 + depends on IP_NF_SET
5814 + The SET target makes possible to add/delete entries
5816 + You need the ipset utility to create and set up the sets.
5818 + To compile it as a module, choose M here. If unsure, say N.
5823 diff -urN linux-2.6.19.old/net/ipv4/netfilter/Makefile linux-2.6.19.dev/net/ipv4/netfilter/Makefile
5824 --- linux-2.6.19.old/net/ipv4/netfilter/Makefile 2006-12-14 03:13:41.000000000 +0100
5825 +++ linux-2.6.19.dev/net/ipv4/netfilter/Makefile 2006-12-14 03:13:43.000000000 +0100
5829 obj-$(CONFIG_IP_NF_MATCH_HASHLIMIT) += ipt_hashlimit.o
5830 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
5831 obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o
5832 obj-$(CONFIG_IP_NF_MATCH_OWNER) += ipt_owner.o
5833 obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o
5835 obj-$(CONFIG_IP_NF_TARGET_LOG) += ipt_LOG.o
5836 obj-$(CONFIG_IP_NF_TARGET_ULOG) += ipt_ULOG.o
5837 obj-$(CONFIG_IP_NF_TARGET_TCPMSS) += ipt_TCPMSS.o
5838 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
5841 +obj-$(CONFIG_IP_NF_SET) += ip_set.o
5842 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
5843 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
5844 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
5845 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
5846 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
5847 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
5849 obj-$(CONFIG_IP_NF_TARGET_CLUSTERIP) += ipt_CLUSTERIP.o
5850 obj-$(CONFIG_IP_NF_TARGET_TTL) += ipt_TTL.o