1 Index: linux-2.4.35.4/Documentation/Configure.help
2 ===================================================================
3 --- linux-2.4.35.4.orig/Documentation/Configure.help 2007-12-15 05:20:07.284255349 +0100
4 +++ linux-2.4.35.4/Documentation/Configure.help 2007-12-15 05:20:07.880289314 +0100
6 If you want to compile it as a module, say M here and read
7 <file:Documentation/modules.txt>. If unsure, say `N'.
11 + This option adds IP set support to the kernel.
13 + In order to define and use sets, you need userlevel utilities: an
14 + iptables binary which knows about IP sets and the program ipset(8),
15 + by which you can define and setup the sets themselves.
17 + If you want to compile it as a module, say M here and read
18 + <file:Documentation/modules.txt>. If unsure, say `N'.
21 +CONFIG_IP_NF_MATCH_SET
22 + This option adds IP set match support.
23 + You need the ipset utility to create and set up the sets.
25 + If you want to compile it as a module, say M here and read
26 + <file:Documentation/modules.txt>. If unsure, say `N'.
29 +CONFIG_IP_NF_TARGET_SET
30 + This option adds IP set target support.
31 + You need the ipset utility to create and set up the sets.
33 + If you want to compile it as a module, say M here and read
34 + <file:Documentation/modules.txt>. If unsure, say `N'.
36 +ipmap set type support
37 +CONFIG_IP_NF_SET_IPMAP
38 + This option adds the ipmap set type support.
40 + If you want to compile it as a module, say M here and read
41 + <file:Documentation/modules.txt>. If unsure, say `N'.
43 +macipmap set type support
44 +CONFIG_IP_NF_SET_MACIPMAP
45 + This option adds the macipmap set type support.
47 + If you want to compile it as a module, say M here and read
48 + <file:Documentation/modules.txt>. If unsure, say `N'.
50 +portmap set type support
51 +CONFIG_IP_NF_SET_PORTMAP
52 + This option adds the portmap set type support.
54 + If you want to compile it as a module, say M here and read
55 + <file:Documentation/modules.txt>. If unsure, say `N'.
57 +iphash set type support
58 +CONFIG_IP_NF_SET_IPHASH
59 + This option adds the iphash set type support.
61 + If you want to compile it as a module, say M here and read
62 + <file:Documentation/modules.txt>. If unsure, say `N'.
64 +nethash set type support
65 +CONFIG_IP_NF_SET_NETHASH
66 + This option adds the nethash set type support.
68 + If you want to compile it as a module, say M here and read
69 + <file:Documentation/modules.txt>. If unsure, say `N'.
71 +iptree set type support
72 +CONFIG_IP_NF_SET_IPTREE
73 + This option adds the iptree set type support.
75 + If you want to compile it as a module, say M here and read
76 + <file:Documentation/modules.txt>. If unsure, say `N'.
79 CONFIG_IP_NF_TARGET_TTL
80 This option adds a `TTL' target, which enables the user to set
81 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set.h
82 ===================================================================
83 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
84 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set.h 2007-12-15 05:20:07.884289543 +0100
89 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
90 + * Patrick Schaaf <bof@bof.de>
91 + * Martin Josefsson <gandalf@wlug.westbo.se>
92 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
94 + * This program is free software; you can redistribute it and/or modify
95 + * it under the terms of the GNU General Public License version 2 as
96 + * published by the Free Software Foundation.
100 + * A sockopt of such quality has hardly ever been seen before on the open
101 + * market! This little beauty, hardly ever used: above 64, so it's
102 + * traditionally used for firewalling, not touched (even once!) by the
103 + * 2.0, 2.2 and 2.4 kernels!
105 + * Comes with its own certificate of authenticity, valid anywhere in the
110 +#define SO_IP_SET 83
113 + * Heavily modify by Joakim Axelsson 08.03.2002
114 + * - Made it more modulebased
116 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
118 + * - in order to "deal with" backward compatibility, renamed to ipset
122 + * Used so that the kernel module and ipset-binary can match their versions
124 +#define IP_SET_PROTOCOL_VERSION 2
126 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
128 +/* Lets work with our own typedef for representing an IP address.
129 + * We hope to make the code more portable, possibly to IPv6...
131 + * The representation works in HOST byte order, because most set types
132 + * will perform arithmetic operations and compare operations.
134 + * For now the type is an uint32_t.
136 + * Make sure to ONLY use the functions when translating and parsing
137 + * in order to keep the host byte order and make it more portable:
140 + * parse_ipandmask()
142 + * (Joakim: where are they???)
145 +typedef uint32_t ip_set_ip_t;
147 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
148 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
150 +typedef uint16_t ip_set_id_t;
152 +#define IP_SET_INVALID_ID 65535
154 +/* How deep we follow bindings */
155 +#define IP_SET_MAX_BINDINGS 6
158 + * Option flags for kernel operations (ipt_set_info)
160 +#define IPSET_SRC 0x01 /* Source match/add */
161 +#define IPSET_DST 0x02 /* Destination match/add */
162 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
165 + * Set types (flavours)
167 +#define IPSET_TYPE_IP 0 /* IP address type of set */
168 +#define IPSET_TYPE_PORT 1 /* Port type of set */
170 +/* Reserved keywords */
171 +#define IPSET_TOKEN_DEFAULT ":default:"
172 +#define IPSET_TOKEN_ALL ":all:"
174 +/* SO_IP_SET operation constants, and their request struct types.
177 + * 0-99: commands with version checking
178 + * 100-199: add/del/test/bind/unbind
179 + * 200-299: list, save, restore
182 +/* Single shot operations:
183 + * version, create, destroy, flush, rename and swap
185 + * Sets are identified by name.
188 +#define IP_SET_REQ_STD \
190 + unsigned version; \
191 + char name[IP_SET_MAXNAMELEN]
193 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
194 +struct ip_set_req_create {
196 + char typename[IP_SET_MAXNAMELEN];
199 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
200 +struct ip_set_req_std {
204 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
205 +/* Uses ip_set_req_std */
207 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
208 +/* Uses ip_set_req_create */
210 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
211 +/* Uses ip_set_req_create */
213 +union ip_set_name_index {
214 + char name[IP_SET_MAXNAMELEN];
218 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
219 +struct ip_set_req_get_set {
222 + union ip_set_name_index set;
225 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
226 +/* Uses ip_set_req_get_set */
228 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
229 +struct ip_set_req_version {
234 +/* Double shots operations:
235 + * add, del, test, bind and unbind.
237 + * First we query the kernel to get the index and type of the target set,
238 + * then issue the command. Validity of IP is checked in kernel in order
239 + * to minimalize sockopt operations.
242 +/* Get minimal set data for add/del/test/bind/unbind IP */
243 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
244 +struct ip_set_req_adt_get {
247 + union ip_set_name_index set;
248 + char typename[IP_SET_MAXNAMELEN];
251 +#define IP_SET_REQ_BYINDEX \
255 +struct ip_set_req_adt {
256 + IP_SET_REQ_BYINDEX;
259 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
260 +/* Uses ip_set_req_adt, with type specific addage */
262 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
263 +/* Uses ip_set_req_adt, with type specific addage */
265 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
266 +/* Uses ip_set_req_adt, with type specific addage */
268 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
269 +/* Uses ip_set_req_bind, with type specific addage */
270 +struct ip_set_req_bind {
271 + IP_SET_REQ_BYINDEX;
272 + char binding[IP_SET_MAXNAMELEN];
275 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
276 +/* Uses ip_set_req_bind, with type speficic addage
277 + * index = 0 means unbinding for all sets */
279 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
280 +/* Uses ip_set_req_bind, with type specific addage */
282 +/* Multiple shots operations: list, save, restore.
284 + * - check kernel version and query the max number of sets
285 + * - get the basic information on all sets
286 + * and size required for the next step
287 + * - get actual set data: header, data, bindings
290 +/* Get max_sets and the index of a queried set
292 +#define IP_SET_OP_MAX_SETS 0x00000020
293 +struct ip_set_req_max_sets {
296 + ip_set_id_t max_sets; /* max_sets */
297 + ip_set_id_t sets; /* real number of sets */
298 + union ip_set_name_index set; /* index of set if name used */
301 +/* Get the id and name of the sets plus size for next step */
302 +#define IP_SET_OP_LIST_SIZE 0x00000201
303 +#define IP_SET_OP_SAVE_SIZE 0x00000202
304 +struct ip_set_req_setnames {
306 + ip_set_id_t index; /* set to list/save */
307 + size_t size; /* size to get setdata/bindings */
308 + /* followed by sets number of struct ip_set_name_list */
311 +struct ip_set_name_list {
312 + char name[IP_SET_MAXNAMELEN];
313 + char typename[IP_SET_MAXNAMELEN];
318 +/* The actual list operation */
319 +#define IP_SET_OP_LIST 0x00000203
320 +struct ip_set_req_list {
321 + IP_SET_REQ_BYINDEX;
322 + /* sets number of struct ip_set_list in reply */
325 +struct ip_set_list {
327 + ip_set_id_t binding;
329 + size_t header_size; /* Set header data of header_size */
330 + size_t members_size; /* Set members data of members_size */
331 + size_t bindings_size; /* Set bindings data of bindings_size */
334 +struct ip_set_hash_list {
336 + ip_set_id_t binding;
339 +/* The save operation */
340 +#define IP_SET_OP_SAVE 0x00000204
341 +/* Uses ip_set_req_list, in the reply replaced by
342 + * sets number of struct ip_set_save plus a marker
343 + * ip_set_save followed by ip_set_hash_save structures.
345 +struct ip_set_save {
347 + ip_set_id_t binding;
348 + size_t header_size; /* Set header data of header_size */
349 + size_t members_size; /* Set members data of members_size */
352 +/* At restoring, ip == 0 means default binding for the given set: */
353 +struct ip_set_hash_save {
356 + ip_set_id_t binding;
359 +/* The restore operation */
360 +#define IP_SET_OP_RESTORE 0x00000205
361 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
362 + * plus a marker ip_set_restore, followed by ip_set_hash_save
365 +struct ip_set_restore {
366 + char name[IP_SET_MAXNAMELEN];
367 + char typename[IP_SET_MAXNAMELEN];
369 + size_t header_size; /* Create data of header_size */
370 + size_t members_size; /* Set members data of members_size */
373 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
375 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
380 +#define ip_set_printk(format, args...) \
382 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
383 + printk(format "\n" , ## args); \
386 +#if defined(IP_SET_DEBUG)
387 +#define DP(format, args...) \
389 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
390 + printk(format "\n" , ## args); \
392 +#define IP_SET_ASSERT(x) \
395 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
396 + __FILE__, __LINE__, __FUNCTION__); \
399 +#define DP(format, args...)
400 +#define IP_SET_ASSERT(x)
406 + * The ip_set_type definition - one per set type, e.g. "ipmap".
408 + * Each individual set has a pointer, set->type, going to one
409 + * of these structures. Function pointers inside the structure implement
410 + * the real behaviour of the sets.
412 + * If not mentioned differently, the implementation behind the function
413 + * pointers of a set_type, is expected to return 0 if ok, and a negative
414 + * errno (e.g. -EINVAL) on error.
416 +struct ip_set_type {
417 + struct list_head list; /* next in list of set types */
419 + /* test for IP in set (kernel: iptables -m set src|dst)
420 + * return 0 if not in set, 1 if in set.
422 + int (*testip_kernel) (struct ip_set *set,
423 + const struct sk_buff * skb,
427 + /* test for IP in set (userspace: ipset -T set IP)
428 + * return 0 if not in set, 1 if in set.
430 + int (*testip) (struct ip_set *set,
431 + const void *data, size_t size,
435 + * Size of the data structure passed by when
436 + * adding/deletin/testing an entry.
440 + /* Add IP into set (userspace: ipset -A set IP)
441 + * Return -EEXIST if the address is already in the set,
442 + * and -ERANGE if the address lies outside the set bounds.
443 + * If the address was not already in the set, 0 is returned.
445 + int (*addip) (struct ip_set *set,
446 + const void *data, size_t size,
449 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
450 + * Return -EEXIST if the address is already in the set,
451 + * and -ERANGE if the address lies outside the set bounds.
452 + * If the address was not already in the set, 0 is returned.
454 + int (*addip_kernel) (struct ip_set *set,
455 + const struct sk_buff * skb,
459 + /* remove IP from set (userspace: ipset -D set --entry x)
460 + * Return -EEXIST if the address is NOT in the set,
461 + * and -ERANGE if the address lies outside the set bounds.
462 + * If the address really was in the set, 0 is returned.
464 + int (*delip) (struct ip_set *set,
465 + const void *data, size_t size,
468 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
469 + * Return -EEXIST if the address is NOT in the set,
470 + * and -ERANGE if the address lies outside the set bounds.
471 + * If the address really was in the set, 0 is returned.
473 + int (*delip_kernel) (struct ip_set *set,
474 + const struct sk_buff * skb,
478 + /* new set creation - allocated type specific items
480 + int (*create) (struct ip_set *set,
481 + const void *data, size_t size);
483 + /* retry the operation after successfully tweaking the set
485 + int (*retry) (struct ip_set *set);
487 + /* set destruction - free type specific items
488 + * There is no return value.
489 + * Can be called only when child sets are destroyed.
491 + void (*destroy) (struct ip_set *set);
493 + /* set flushing - reset all bits in the set, or something similar.
494 + * There is no return value.
496 + void (*flush) (struct ip_set *set);
498 + /* Listing: size needed for header
500 + size_t header_size;
502 + /* Listing: Get the header
504 + * Fill in the information in "data".
505 + * This function is always run after list_header_size() under a
506 + * writelock on the set. Therefor is the length of "data" always
509 + void (*list_header) (const struct ip_set *set,
512 + /* Listing: Get the size for the set members
514 + int (*list_members_size) (const struct ip_set *set);
516 + /* Listing: Get the set members
518 + * Fill in the information in "data".
519 + * This function is always run after list_member_size() under a
520 + * writelock on the set. Therefor is the length of "data" always
523 + void (*list_members) (const struct ip_set *set,
526 + char typename[IP_SET_MAXNAMELEN];
528 + int protocol_version;
530 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
534 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
535 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
537 +/* A generic ipset */
539 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
540 + rwlock_t lock; /* lock for concurrency control */
541 + ip_set_id_t id; /* set id for swapping */
542 + ip_set_id_t binding; /* default binding for the set */
543 + atomic_t ref; /* in kernel and in hash references */
544 + struct ip_set_type *type; /* the set types */
545 + void *data; /* pooltype specific data */
548 +/* Structure to bind set elements to sets */
549 +struct ip_set_hash {
550 + struct list_head list; /* list of clashing entries in hash */
551 + ip_set_ip_t ip; /* ip from set */
552 + ip_set_id_t id; /* set id */
553 + ip_set_id_t binding; /* set we bind the element to */
556 +/* register and unregister set references */
557 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
558 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
559 +extern void ip_set_put(ip_set_id_t id);
561 +/* API for iptables set match, and SET target */
562 +extern void ip_set_addip_kernel(ip_set_id_t id,
563 + const struct sk_buff *skb,
564 + const u_int32_t *flags);
565 +extern void ip_set_delip_kernel(ip_set_id_t id,
566 + const struct sk_buff *skb,
567 + const u_int32_t *flags);
568 +extern int ip_set_testip_kernel(ip_set_id_t id,
569 + const struct sk_buff *skb,
570 + const u_int32_t *flags);
572 +#endif /* __KERNEL__ */
574 +#endif /*_IP_SET_H*/
575 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_iphash.h
576 ===================================================================
577 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
578 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_iphash.h 2007-12-15 05:20:07.884289543 +0100
580 +#ifndef __IP_SET_IPHASH_H
581 +#define __IP_SET_IPHASH_H
583 +#include <linux/netfilter_ipv4/ip_set.h>
585 +#define SETTYPE_NAME "iphash"
586 +#define MAX_RANGE 0x0000FFFF
588 +struct ip_set_iphash {
589 + ip_set_ip_t *members; /* the iphash proper */
590 + uint32_t initval; /* initval for jhash_1word */
591 + uint32_t prime; /* prime for double hashing */
592 + uint32_t hashsize; /* hash size */
593 + uint16_t probes; /* max number of probes */
594 + uint16_t resize; /* resize factor in percent */
595 + ip_set_ip_t netmask; /* netmask */
598 +struct ip_set_req_iphash_create {
602 + ip_set_ip_t netmask;
605 +struct ip_set_req_iphash {
609 +#endif /* __IP_SET_IPHASH_H */
610 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_ipmap.h
611 ===================================================================
612 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
613 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_ipmap.h 2007-12-15 05:20:07.884289543 +0100
615 +#ifndef __IP_SET_IPMAP_H
616 +#define __IP_SET_IPMAP_H
618 +#include <linux/netfilter_ipv4/ip_set.h>
620 +#define SETTYPE_NAME "ipmap"
621 +#define MAX_RANGE 0x0000FFFF
623 +struct ip_set_ipmap {
624 + void *members; /* the ipmap proper */
625 + ip_set_ip_t first_ip; /* host byte order, included in range */
626 + ip_set_ip_t last_ip; /* host byte order, included in range */
627 + ip_set_ip_t netmask; /* subnet netmask */
628 + ip_set_ip_t sizeid; /* size of set in IPs */
629 + u_int16_t hosts; /* number of hosts in a subnet */
632 +struct ip_set_req_ipmap_create {
635 + ip_set_ip_t netmask;
638 +struct ip_set_req_ipmap {
643 +mask_to_bits(ip_set_ip_t mask)
645 + unsigned int bits = 32;
646 + ip_set_ip_t maskaddr;
648 + if (mask == 0xFFFFFFFF)
651 + maskaddr = 0xFFFFFFFE;
652 + while (--bits >= 0 && maskaddr != mask)
659 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
661 + ip_set_ip_t mask = 0xFFFFFFFE;
664 + while (--(*bits) >= 0 && mask && (to & mask) != from)
670 +#endif /* __IP_SET_IPMAP_H */
671 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_iptree.h
672 ===================================================================
673 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
674 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_iptree.h 2007-12-15 05:20:07.884289543 +0100
676 +#ifndef __IP_SET_IPTREE_H
677 +#define __IP_SET_IPTREE_H
679 +#include <linux/netfilter_ipv4/ip_set.h>
681 +#define SETTYPE_NAME "iptree"
682 +#define MAX_RANGE 0x0000FFFF
684 +struct ip_set_iptreed {
685 + unsigned long expires[255]; /* x.x.x.ADDR */
688 +struct ip_set_iptreec {
689 + struct ip_set_iptreed *tree[255]; /* x.x.ADDR.* */
692 +struct ip_set_iptreeb {
693 + struct ip_set_iptreec *tree[255]; /* x.ADDR.*.* */
696 +struct ip_set_iptree {
697 + unsigned int timeout;
698 + unsigned int gc_interval;
700 + struct timer_list gc;
701 + struct ip_set_iptreeb *tree[255]; /* ADDR.*.*.* */
705 +struct ip_set_req_iptree_create {
706 + unsigned int timeout;
709 +struct ip_set_req_iptree {
711 + unsigned int timeout;
714 +#endif /* __IP_SET_IPTREE_H */
715 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_jhash.h
716 ===================================================================
717 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
718 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_jhash.h 2007-12-15 05:20:07.884289543 +0100
720 +#ifndef _LINUX_IPSET_JHASH_H
721 +#define _LINUX_IPSET_JHASH_H
723 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
724 + * to __u32/__u8 so that the header file can be included into
725 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
728 +/* jhash.h: Jenkins hash support.
730 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
732 + * http://burtleburtle.net/bob/hash/
734 + * These are the credits from Bob's sources:
736 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
737 + * hash(), hash2(), hash3, and mix() are externally useful functions.
738 + * Routines to test the hash are included if SELF_TEST is defined.
739 + * You can use this free for any purpose. It has no warranty.
741 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
743 + * I've modified Bob's hash to be useful in the Linux kernel, and
744 + * any bugs present are surely my fault. -DaveM
747 +/* NOTE: Arguments are modified. */
748 +#define __jhash_mix(a, b, c) \
750 + a -= b; a -= c; a ^= (c>>13); \
751 + b -= c; b -= a; b ^= (a<<8); \
752 + c -= a; c -= b; c ^= (b>>13); \
753 + a -= b; a -= c; a ^= (c>>12); \
754 + b -= c; b -= a; b ^= (a<<16); \
755 + c -= a; c -= b; c ^= (b>>5); \
756 + a -= b; a -= c; a ^= (c>>3); \
757 + b -= c; b -= a; b ^= (a<<10); \
758 + c -= a; c -= b; c ^= (b>>15); \
761 +/* The golden ration: an arbitrary value */
762 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
764 +/* The most generic version, hashes an arbitrary sequence
765 + * of bytes. No alignment or length assumptions are made about
768 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
770 + __u32 a, b, c, len;
774 + a = b = JHASH_GOLDEN_RATIO;
777 + while (len >= 12) {
778 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
779 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
780 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
782 + __jhash_mix(a,b,c);
790 + case 11: c += ((__u32)k[10]<<24);
791 + case 10: c += ((__u32)k[9]<<16);
792 + case 9 : c += ((__u32)k[8]<<8);
793 + case 8 : b += ((__u32)k[7]<<24);
794 + case 7 : b += ((__u32)k[6]<<16);
795 + case 6 : b += ((__u32)k[5]<<8);
796 + case 5 : b += k[4];
797 + case 4 : a += ((__u32)k[3]<<24);
798 + case 3 : a += ((__u32)k[2]<<16);
799 + case 2 : a += ((__u32)k[1]<<8);
800 + case 1 : a += k[0];
803 + __jhash_mix(a,b,c);
808 +/* A special optimized version that handles 1 or more of __u32s.
809 + * The length parameter here is the number of __u32s in the key.
811 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
813 + __u32 a, b, c, len;
815 + a = b = JHASH_GOLDEN_RATIO;
823 + __jhash_mix(a, b, c);
830 + case 2 : b += k[1];
831 + case 1 : a += k[0];
834 + __jhash_mix(a,b,c);
840 +/* A special ultra-optimized versions that knows they are hashing exactly
841 + * 3, 2 or 1 word(s).
843 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
844 + * done at the end is not done here.
846 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
848 + a += JHASH_GOLDEN_RATIO;
849 + b += JHASH_GOLDEN_RATIO;
852 + __jhash_mix(a, b, c);
857 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
859 + return jhash_3words(a, b, 0, initval);
862 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
864 + return jhash_3words(a, 0, 0, initval);
867 +#endif /* _LINUX_IPSET_JHASH_H */
868 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_macipmap.h
869 ===================================================================
870 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
871 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_macipmap.h 2007-12-15 05:20:07.884289543 +0100
873 +#ifndef __IP_SET_MACIPMAP_H
874 +#define __IP_SET_MACIPMAP_H
876 +#include <linux/netfilter_ipv4/ip_set.h>
878 +#define SETTYPE_NAME "macipmap"
879 +#define MAX_RANGE 0x0000FFFF
882 +#define IPSET_MACIP_MATCHUNSET 1
885 +#define IPSET_MACIP_ISSET 1
887 +struct ip_set_macipmap {
888 + void *members; /* the macipmap proper */
889 + ip_set_ip_t first_ip; /* host byte order, included in range */
890 + ip_set_ip_t last_ip; /* host byte order, included in range */
894 +struct ip_set_req_macipmap_create {
900 +struct ip_set_req_macipmap {
902 + unsigned char ethernet[ETH_ALEN];
905 +struct ip_set_macip {
906 + unsigned short flags;
907 + unsigned char ethernet[ETH_ALEN];
910 +#endif /* __IP_SET_MACIPMAP_H */
911 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_malloc.h
912 ===================================================================
913 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
914 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_malloc.h 2007-12-15 05:20:07.888289773 +0100
916 +#ifndef _IP_SET_MALLOC_H
917 +#define _IP_SET_MALLOC_H
921 +/* Memory allocation and deallocation */
922 +static size_t max_malloc_size = 131072; /* Guaranteed: slab.c */
924 +static inline void * ip_set_malloc(size_t bytes)
926 + if (bytes > max_malloc_size)
927 + return vmalloc(bytes);
929 + return kmalloc(bytes, GFP_KERNEL);
932 +static inline void ip_set_free(void * data, size_t bytes)
934 + if (bytes > max_malloc_size)
940 +#endif /* __KERNEL__ */
942 +#endif /*_IP_SET_MALLOC_H*/
943 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_nethash.h
944 ===================================================================
945 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
946 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_nethash.h 2007-12-15 05:20:07.888289773 +0100
948 +#ifndef __IP_SET_NETHASH_H
949 +#define __IP_SET_NETHASH_H
951 +#include <linux/netfilter_ipv4/ip_set.h>
953 +#define SETTYPE_NAME "nethash"
954 +#define MAX_RANGE 0x0000FFFF
956 +struct ip_set_nethash {
957 + ip_set_ip_t *members; /* the nethash proper */
958 + uint32_t initval; /* initval for jhash_1word */
959 + uint32_t prime; /* prime for double hashing */
960 + uint32_t hashsize; /* hash size */
961 + uint16_t probes; /* max number of probes */
962 + uint16_t resize; /* resize factor in percent */
963 + unsigned char cidr[30]; /* CIDR sizes */
966 +struct ip_set_req_nethash_create {
972 +struct ip_set_req_nethash {
974 + unsigned char cidr;
977 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
979 +static inline ip_set_ip_t
980 +pack(ip_set_ip_t ip, unsigned char cidr)
982 + ip_set_ip_t addr, *paddr = &addr;
983 + unsigned char n, t, *a;
985 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
987 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
991 + a = &((unsigned char *)paddr)[n];
992 + *a = *a /(1 << (8 - t)) + shifts[t];
994 + DP("n: %u, t: %u, a: %u", n, t, *a);
995 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
996 + HIPQUAD(ip), cidr, NIPQUAD(addr));
999 + return ntohl(addr);
1002 +#endif /* __IP_SET_NETHASH_H */
1003 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_portmap.h
1004 ===================================================================
1005 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1006 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_portmap.h 2007-12-15 05:20:07.888289773 +0100
1008 +#ifndef __IP_SET_PORTMAP_H
1009 +#define __IP_SET_PORTMAP_H
1011 +#include <linux/netfilter_ipv4/ip_set.h>
1013 +#define SETTYPE_NAME "portmap"
1014 +#define MAX_RANGE 0x0000FFFF
1015 +#define INVALID_PORT (MAX_RANGE + 1)
1017 +struct ip_set_portmap {
1018 + void *members; /* the portmap proper */
1019 + ip_set_ip_t first_port; /* host byte order, included in range */
1020 + ip_set_ip_t last_port; /* host byte order, included in range */
1023 +struct ip_set_req_portmap_create {
1028 +struct ip_set_req_portmap {
1032 +#endif /* __IP_SET_PORTMAP_H */
1033 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_prime.h
1034 ===================================================================
1035 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1036 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_set_prime.h 2007-12-15 05:20:07.888289773 +0100
1038 +#ifndef __IP_SET_PRIME_H
1039 +#define __IP_SET_PRIME_H
1041 +static inline unsigned make_prime_bound(unsigned nr)
1043 + unsigned long long nr64 = nr;
1044 + unsigned long long x = 1;
1046 + while (x <= nr64) { x <<= 2; nr <<= 1; }
1050 +static inline int make_prime_check(unsigned nr)
1053 + unsigned b = make_prime_bound(nr);
1055 + if (0 == (nr % x)) return 0;
1061 +static unsigned make_prime(unsigned nr)
1063 + if (0 == (nr & 1)) nr--;
1065 + if (make_prime_check(nr)) return nr;
1071 +#endif /* __IP_SET_PRIME_H */
1072 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ipt_set.h
1073 ===================================================================
1074 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1075 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ipt_set.h 2007-12-15 05:20:07.888289773 +0100
1080 +#include <linux/netfilter_ipv4/ip_set.h>
1082 +struct ipt_set_info {
1083 + ip_set_id_t index;
1084 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1088 +struct ipt_set_info_match {
1089 + struct ipt_set_info match_set;
1092 +struct ipt_set_info_target {
1093 + struct ipt_set_info add_set;
1094 + struct ipt_set_info del_set;
1097 +#endif /*_IPT_SET_H*/
1098 Index: linux-2.4.35.4/net/ipv4/netfilter/Config.in
1099 ===================================================================
1100 --- linux-2.4.35.4.orig/net/ipv4/netfilter/Config.in 2007-12-15 05:20:07.568271536 +0100
1101 +++ linux-2.4.35.4/net/ipv4/netfilter/Config.in 2007-12-15 05:20:07.892290000 +0100
1103 if [ "$CONFIG_IP_NF_IPTABLES" != "n" ]; then
1104 # The simple matches.
1105 dep_tristate ' limit match support' CONFIG_IP_NF_MATCH_LIMIT $CONFIG_IP_NF_IPTABLES
1107 + dep_tristate ' IP set support' CONFIG_IP_NF_SET $CONFIG_IP_NF_IPTABLES
1108 + if [ "$CONFIG_IP_NF_SET" != "n" ]; then
1109 + int ' Maximum number of sets' CONFIG_IP_NF_SET_MAX 256
1110 + int ' Hash size for bindings of IP sets' CONFIG_IP_NF_SET_HASHSIZE 1024
1111 + dep_tristate ' set match support' CONFIG_IP_NF_MATCH_SET $CONFIG_IP_NF_SET
1112 + dep_tristate ' SET target support' CONFIG_IP_NF_TARGET_SET $CONFIG_IP_NF_SET
1113 + dep_tristate ' ipmap set type support' CONFIG_IP_NF_SET_IPMAP $CONFIG_IP_NF_SET
1114 + dep_tristate ' portmap set type support' CONFIG_IP_NF_SET_PORTMAP $CONFIG_IP_NF_SET
1115 + dep_tristate ' macipmap set type support' CONFIG_IP_NF_SET_MACIPMAP $CONFIG_IP_NF_SET
1116 + dep_tristate ' iphash set type support' CONFIG_IP_NF_SET_IPHASH $CONFIG_IP_NF_SET
1117 + dep_tristate ' nethash set type support' CONFIG_IP_NF_SET_NETHASH $CONFIG_IP_NF_SET
1118 + dep_tristate ' iptree set type support' CONFIG_IP_NF_SET_IPTREE $CONFIG_IP_NF_SET
1120 dep_tristate ' MAC address match support' CONFIG_IP_NF_MATCH_MAC $CONFIG_IP_NF_IPTABLES
1121 dep_tristate ' Packet type match support' CONFIG_IP_NF_MATCH_PKTTYPE $CONFIG_IP_NF_IPTABLES
1122 dep_tristate ' netfilter MARK match support' CONFIG_IP_NF_MATCH_MARK $CONFIG_IP_NF_IPTABLES
1123 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set.c
1124 ===================================================================
1125 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1126 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set.c 2007-12-15 05:20:07.892290000 +0100
1128 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1129 + * Patrick Schaaf <bof@bof.de>
1130 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1132 + * This program is free software; you can redistribute it and/or modify
1133 + * it under the terms of the GNU General Public License version 2 as
1134 + * published by the Free Software Foundation.
1137 +/* Kernel module for IP set management */
1139 +#include <linux/config.h>
1140 +#include <linux/module.h>
1141 +#include <linux/kmod.h>
1142 +#include <linux/ip.h>
1143 +#include <linux/skbuff.h>
1144 +#include <linux/random.h>
1145 +#include <linux/jhash.h>
1146 +#include <linux/netfilter_ipv4/ip_tables.h>
1147 +#include <linux/errno.h>
1148 +#include <asm/uaccess.h>
1149 +#include <asm/bitops.h>
1150 +#include <asm/softirq.h>
1151 +#include <asm/semaphore.h>
1152 +#include <linux/spinlock.h>
1153 +#include <linux/vmalloc.h>
1155 +#define ASSERT_READ_LOCK(x) /* dont use that */
1156 +#define ASSERT_WRITE_LOCK(x)
1157 +#include <linux/netfilter_ipv4/listhelp.h>
1158 +#include <linux/netfilter_ipv4/ip_set.h>
1160 +static struct list_head set_type_list; /* all registered sets */
1161 +static struct ip_set **ip_set_list; /* all individual sets */
1162 +static DECLARE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1163 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1164 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1165 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1166 +static struct list_head *ip_set_hash; /* hash of bindings */
1167 +static unsigned int ip_set_hash_random; /* random seed */
1171 +#define __MOD_INC(foo) __MOD_INC_USE_COUNT(foo)
1172 +#define __MOD_DEC(foo) __MOD_DEC_USE_COUNT(foo)
1173 +#define __MOD_INC_SELF MOD_INC_USE_COUNT
1174 +#define __MOD_DEC_SELF MOD_DEC_USE_COUNT
1176 +#define __MOD_INC(foo)
1177 +#define __MOD_DEC(foo)
1178 +#define __MOD_INC_SELF
1179 +#define __MOD_DEC_SELF
1183 + * Sets are identified either by the index in ip_set_list or by id.
1184 + * The id never changes and is used to find a key in the hash.
1185 + * The index may change by swapping and used at all other places
1186 + * (set/SET netfilter modules, binding value, etc.)
1188 + * Userspace requests are serialized by ip_set_mutex and sets can
1189 + * be deleted only from userspace. Therefore ip_set_list locking
1190 + * must obey the following rules:
1192 + * - kernel requests: read and write locking mandatory
1193 + * - user requests: read locking optional, write locking mandatory
1197 +__ip_set_get(ip_set_id_t index)
1199 + atomic_inc(&ip_set_list[index]->ref);
1203 +__ip_set_put(ip_set_id_t index)
1205 + atomic_dec(&ip_set_list[index]->ref);
1209 + * Binding routines
1213 +ip_hash_cmp(const struct ip_set_hash *set_hash,
1214 + ip_set_id_t id, ip_set_ip_t ip)
1216 + return set_hash->id == id && set_hash->ip == ip;
1220 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1222 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1223 + % ip_set_bindings_hash_size;
1224 + struct ip_set_hash *set_hash;
1226 + MUST_BE_READ_LOCKED(&ip_set_lock);
1227 + IP_SET_ASSERT(ip_set_list[id]);
1228 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1230 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1231 + struct ip_set_hash *, id, ip);
1233 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1235 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1237 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1241 +__set_hash_del(struct ip_set_hash *set_hash)
1243 + MUST_BE_WRITE_LOCKED(&ip_set_lock);
1244 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1246 + __ip_set_put(set_hash->binding);
1247 + list_del(&set_hash->list);
1252 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1254 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1255 + % ip_set_bindings_hash_size;
1256 + struct ip_set_hash *set_hash;
1258 + IP_SET_ASSERT(ip_set_list[id]);
1259 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1260 + WRITE_LOCK(&ip_set_lock);
1261 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1262 + struct ip_set_hash *, id, ip);
1263 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1265 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1267 + if (set_hash != NULL)
1268 + __set_hash_del(set_hash);
1269 + WRITE_UNLOCK(&ip_set_lock);
1274 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1276 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1277 + % ip_set_bindings_hash_size;
1278 + struct ip_set_hash *set_hash;
1281 + IP_SET_ASSERT(ip_set_list[id]);
1282 + IP_SET_ASSERT(ip_set_list[binding]);
1283 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1284 + HIPQUAD(ip), ip_set_list[binding]->name);
1285 + WRITE_LOCK(&ip_set_lock);
1286 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1287 + struct ip_set_hash *, id, ip);
1289 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_KERNEL);
1294 + INIT_LIST_HEAD(&set_hash->list);
1295 + set_hash->id = id;
1296 + set_hash->ip = ip;
1297 + list_add(&ip_set_hash[key], &set_hash->list);
1299 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1300 + DP("overwrite binding: %s",
1301 + ip_set_list[set_hash->binding]->name);
1302 + __ip_set_put(set_hash->binding);
1304 + set_hash->binding = binding;
1305 + __ip_set_get(set_hash->binding);
1307 + WRITE_UNLOCK(&ip_set_lock);
1311 +#define FOREACH_HASH_DO(fn, args...) \
1313 + ip_set_id_t __key; \
1314 + struct ip_set_hash *__set_hash; \
1316 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1317 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1318 + fn(__set_hash , ## args); \
1322 +#define FOREACH_HASH_RW_DO(fn, args...) \
1324 + ip_set_id_t __key; \
1325 + struct ip_set_hash *__set_hash, *__n; \
1327 + MUST_BE_WRITE_LOCKED(&ip_set_lock); \
1328 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1329 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1330 + fn(__set_hash , ## args); \
1334 +/* Add, del and test set entries from kernel */
1336 +#define follow_bindings(index, set, ip) \
1337 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1338 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1341 +ip_set_testip_kernel(ip_set_id_t index,
1342 + const struct sk_buff *skb,
1343 + const u_int32_t *flags)
1345 + struct ip_set *set;
1349 + IP_SET_ASSERT(flags[i]);
1350 + READ_LOCK(&ip_set_lock);
1352 + set = ip_set_list[index];
1353 + IP_SET_ASSERT(set);
1354 + DP("set %s, index %u", set->name, index);
1355 + read_lock_bh(&set->lock);
1356 + res = set->type->testip_kernel(set, skb, flags[i], &ip);
1357 + read_unlock_bh(&set->lock);
1360 + && follow_bindings(index, set, ip));
1361 + READ_UNLOCK(&ip_set_lock);
1367 +ip_set_addip_kernel(ip_set_id_t index,
1368 + const struct sk_buff *skb,
1369 + const u_int32_t *flags)
1371 + struct ip_set *set;
1375 + IP_SET_ASSERT(flags[i]);
1377 + READ_LOCK(&ip_set_lock);
1379 + set = ip_set_list[index];
1380 + IP_SET_ASSERT(set);
1381 + DP("set %s, index %u", set->name, index);
1382 + write_lock_bh(&set->lock);
1383 + res = set->type->addip_kernel(set, skb, flags[i], &ip);
1384 + write_unlock_bh(&set->lock);
1385 + } while ((res == 0 || res == -EEXIST)
1387 + && follow_bindings(index, set, ip));
1388 + READ_UNLOCK(&ip_set_lock);
1390 + if (res == -EAGAIN
1391 + && set->type->retry
1392 + && (res = set->type->retry(set)) == 0)
1397 +ip_set_delip_kernel(ip_set_id_t index,
1398 + const struct sk_buff *skb,
1399 + const u_int32_t *flags)
1401 + struct ip_set *set;
1405 + IP_SET_ASSERT(flags[i]);
1406 + READ_LOCK(&ip_set_lock);
1408 + set = ip_set_list[index];
1409 + IP_SET_ASSERT(set);
1410 + DP("set %s, index %u", set->name, index);
1411 + write_lock_bh(&set->lock);
1412 + res = set->type->delip_kernel(set, skb, flags[i], &ip);
1413 + write_unlock_bh(&set->lock);
1414 + } while ((res == 0 || res == -EEXIST)
1416 + && follow_bindings(index, set, ip));
1417 + READ_UNLOCK(&ip_set_lock);
1420 +/* Register and deregister settype */
1423 +set_type_equal(const struct ip_set_type *set_type, const char *str2)
1425 + return !strncmp(set_type->typename, str2, IP_SET_MAXNAMELEN - 1);
1428 +static inline struct ip_set_type *
1429 +find_set_type(const char *name)
1431 + return LIST_FIND(&set_type_list,
1433 + struct ip_set_type *,
1438 +ip_set_register_set_type(struct ip_set_type *set_type)
1442 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1443 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1444 + set_type->typename,
1445 + set_type->protocol_version,
1446 + IP_SET_PROTOCOL_VERSION);
1450 + WRITE_LOCK(&ip_set_lock);
1451 + if (find_set_type(set_type->typename)) {
1453 + ip_set_printk("'%s' already registered!",
1454 + set_type->typename);
1459 + list_append(&set_type_list, set_type);
1460 + DP("'%s' registered.", set_type->typename);
1462 + WRITE_UNLOCK(&ip_set_lock);
1467 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1469 + WRITE_LOCK(&ip_set_lock);
1470 + if (!find_set_type(set_type->typename)) {
1471 + ip_set_printk("'%s' not registered?",
1472 + set_type->typename);
1475 + LIST_DELETE(&set_type_list, set_type);
1477 + DP("'%s' unregistered.", set_type->typename);
1479 + WRITE_UNLOCK(&ip_set_lock);
1484 + * Userspace routines
1488 + * Find set by name, reference it once. The reference makes sure the
1489 + * thing pointed to, does not go away under our feet. Drop the reference
1490 + * later, using ip_set_put().
1493 +ip_set_get_byname(const char *name)
1495 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1497 + down(&ip_set_app_mutex);
1498 + for (i = 0; i < ip_set_max; i++) {
1499 + if (ip_set_list[i] != NULL
1500 + && strcmp(ip_set_list[i]->name, name) == 0) {
1506 + up(&ip_set_app_mutex);
1511 + * Find set by index, reference it once. The reference makes sure the
1512 + * thing pointed to, does not go away under our feet. Drop the reference
1513 + * later, using ip_set_put().
1516 +ip_set_get_byindex(ip_set_id_t index)
1518 + down(&ip_set_app_mutex);
1520 + if (index >= ip_set_max)
1521 + return IP_SET_INVALID_ID;
1523 + if (ip_set_list[index])
1524 + __ip_set_get(index);
1526 + index = IP_SET_INVALID_ID;
1528 + up(&ip_set_app_mutex);
1533 + * If the given set pointer points to a valid set, decrement
1534 + * reference count by 1. The caller shall not assume the index
1535 + * to be valid, after calling this function.
1537 +void ip_set_put(ip_set_id_t index)
1539 + down(&ip_set_app_mutex);
1540 + if (ip_set_list[index])
1541 + __ip_set_put(index);
1542 + up(&ip_set_app_mutex);
1545 +/* Find a set by name or index */
1547 +ip_set_find_byname(const char *name)
1549 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1551 + for (i = 0; i < ip_set_max; i++) {
1552 + if (ip_set_list[i] != NULL
1553 + && strcmp(ip_set_list[i]->name, name) == 0) {
1562 +ip_set_find_byindex(ip_set_id_t index)
1564 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1565 + index = IP_SET_INVALID_ID;
1571 + * Add, del, test, bind and unbind
1575 +__ip_set_testip(struct ip_set *set,
1582 + read_lock_bh(&set->lock);
1583 + res = set->type->testip(set, data, size, ip);
1584 + read_unlock_bh(&set->lock);
1590 +__ip_set_addip(ip_set_id_t index,
1594 + struct ip_set *set = ip_set_list[index];
1598 + IP_SET_ASSERT(set);
1600 + write_lock_bh(&set->lock);
1601 + res = set->type->addip(set, data, size, &ip);
1602 + write_unlock_bh(&set->lock);
1603 + } while (res == -EAGAIN
1604 + && set->type->retry
1605 + && (res = set->type->retry(set)) == 0);
1611 +ip_set_addip(ip_set_id_t index,
1616 + return __ip_set_addip(index,
1617 + data + sizeof(struct ip_set_req_adt),
1618 + size - sizeof(struct ip_set_req_adt));
1622 +ip_set_delip(ip_set_id_t index,
1626 + struct ip_set *set = ip_set_list[index];
1630 + IP_SET_ASSERT(set);
1631 + write_lock_bh(&set->lock);
1632 + res = set->type->delip(set,
1633 + data + sizeof(struct ip_set_req_adt),
1634 + size - sizeof(struct ip_set_req_adt),
1636 + write_unlock_bh(&set->lock);
1642 +ip_set_testip(ip_set_id_t index,
1646 + struct ip_set *set = ip_set_list[index];
1650 + IP_SET_ASSERT(set);
1651 + res = __ip_set_testip(set,
1652 + data + sizeof(struct ip_set_req_adt),
1653 + size - sizeof(struct ip_set_req_adt),
1656 + return (res > 0 ? -EEXIST : res);
1660 +ip_set_bindip(ip_set_id_t index,
1664 + struct ip_set *set = ip_set_list[index];
1665 + struct ip_set_req_bind *req_bind;
1666 + ip_set_id_t binding;
1670 + IP_SET_ASSERT(set);
1671 + if (size < sizeof(struct ip_set_req_bind))
1674 + req_bind = (struct ip_set_req_bind *) data;
1675 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1677 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1678 + /* Default binding of a set */
1679 + char *binding_name;
1681 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1684 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1685 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1687 + binding = ip_set_find_byname(binding_name);
1688 + if (binding == IP_SET_INVALID_ID)
1691 + WRITE_LOCK(&ip_set_lock);
1692 + /* Sets as binding values are referenced */
1693 + if (set->binding != IP_SET_INVALID_ID)
1694 + __ip_set_put(set->binding);
1695 + set->binding = binding;
1696 + __ip_set_get(set->binding);
1697 + WRITE_UNLOCK(&ip_set_lock);
1701 + binding = ip_set_find_byname(req_bind->binding);
1702 + if (binding == IP_SET_INVALID_ID)
1705 + res = __ip_set_testip(set,
1706 + data + sizeof(struct ip_set_req_bind),
1707 + size - sizeof(struct ip_set_req_bind),
1709 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1710 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1713 + res = ip_set_hash_add(set->id, ip, binding);
1718 +#define FOREACH_SET_DO(fn, args...) \
1720 + ip_set_id_t __i; \
1721 + struct ip_set *__set; \
1723 + for (__i = 0; __i < ip_set_max; __i++) { \
1724 + __set = ip_set_list[__i]; \
1725 + if (__set != NULL) \
1726 + fn(__set , ##args); \
1731 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1733 + if (set_hash->id == id)
1734 + __set_hash_del(set_hash);
1738 +__unbind_default(struct ip_set *set)
1740 + if (set->binding != IP_SET_INVALID_ID) {
1741 + /* Sets as binding values are referenced */
1742 + __ip_set_put(set->binding);
1743 + set->binding = IP_SET_INVALID_ID;
1748 +ip_set_unbindip(ip_set_id_t index,
1752 + struct ip_set *set;
1753 + struct ip_set_req_bind *req_bind;
1758 + if (size < sizeof(struct ip_set_req_bind))
1761 + req_bind = (struct ip_set_req_bind *) data;
1762 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1764 + DP("%u %s", index, req_bind->binding);
1765 + if (index == IP_SET_INVALID_ID) {
1766 + /* unbind :all: */
1767 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1768 + /* Default binding of sets */
1769 + WRITE_LOCK(&ip_set_lock);
1770 + FOREACH_SET_DO(__unbind_default);
1771 + WRITE_UNLOCK(&ip_set_lock);
1773 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1774 + /* Flush all bindings of all sets*/
1775 + WRITE_LOCK(&ip_set_lock);
1776 + FOREACH_HASH_RW_DO(__set_hash_del);
1777 + WRITE_UNLOCK(&ip_set_lock);
1780 + DP("unreachable reached!");
1784 + set = ip_set_list[index];
1785 + IP_SET_ASSERT(set);
1786 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1787 + /* Default binding of set */
1788 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1790 + if (binding == IP_SET_INVALID_ID)
1793 + WRITE_LOCK(&ip_set_lock);
1794 + /* Sets in hash values are referenced */
1795 + __ip_set_put(set->binding);
1796 + set->binding = IP_SET_INVALID_ID;
1797 + WRITE_UNLOCK(&ip_set_lock);
1800 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1801 + /* Flush all bindings */
1803 + WRITE_LOCK(&ip_set_lock);
1804 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1805 + WRITE_UNLOCK(&ip_set_lock);
1809 + res = __ip_set_testip(set,
1810 + data + sizeof(struct ip_set_req_bind),
1811 + size - sizeof(struct ip_set_req_bind),
1814 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1816 + res = ip_set_hash_del(set->id, ip);
1822 +ip_set_testbind(ip_set_id_t index,
1826 + struct ip_set *set = ip_set_list[index];
1827 + struct ip_set_req_bind *req_bind;
1828 + ip_set_id_t binding;
1832 + IP_SET_ASSERT(set);
1833 + if (size < sizeof(struct ip_set_req_bind))
1836 + req_bind = (struct ip_set_req_bind *) data;
1837 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1839 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1840 + /* Default binding of set */
1841 + char *binding_name;
1843 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1846 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1847 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1849 + binding = ip_set_find_byname(binding_name);
1850 + if (binding == IP_SET_INVALID_ID)
1853 + res = (set->binding == binding) ? -EEXIST : 0;
1857 + binding = ip_set_find_byname(req_bind->binding);
1858 + if (binding == IP_SET_INVALID_ID)
1862 + res = __ip_set_testip(set,
1863 + data + sizeof(struct ip_set_req_bind),
1864 + size - sizeof(struct ip_set_req_bind),
1866 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1867 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1870 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1876 +static struct ip_set_type *
1877 +find_set_type_rlock(const char *typename)
1879 + struct ip_set_type *type;
1881 + READ_LOCK(&ip_set_lock);
1882 + type = find_set_type(typename);
1884 + READ_UNLOCK(&ip_set_lock);
1890 +find_free_id(const char *name,
1891 + ip_set_id_t *index,
1896 + *id = IP_SET_INVALID_ID;
1897 + for (i = 0; i < ip_set_max; i++) {
1898 + if (ip_set_list[i] == NULL) {
1899 + if (*id == IP_SET_INVALID_ID)
1901 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1905 + if (*id == IP_SET_INVALID_ID)
1906 + /* No free slot remained */
1908 + /* Check that index is usable as id (swapping) */
1910 + for (i = 0; i < ip_set_max; i++) {
1911 + if (ip_set_list[i] != NULL
1912 + && ip_set_list[i]->id == *id) {
1924 +ip_set_create(const char *name,
1925 + const char *typename,
1926 + ip_set_id_t restore,
1930 + struct ip_set *set;
1931 + ip_set_id_t index, id;
1934 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1936 + * First, and without any locks, allocate and initialize
1937 + * a normal base set structure.
1939 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1942 + set->lock = RW_LOCK_UNLOCKED;
1943 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1944 + set->binding = IP_SET_INVALID_ID;
1945 + atomic_set(&set->ref, 0);
1948 + * Next, take the &ip_set_lock, check that we know the type,
1949 + * and take a reference on the type, to make sure it
1950 + * stays available while constructing our new set.
1952 + * After referencing the type, we drop the &ip_set_lock,
1953 + * and let the new set construction run without locks.
1955 + set->type = find_set_type_rlock(typename);
1956 + if (set->type == NULL) {
1957 + /* Try loading the module */
1958 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1959 + strcpy(modulename, "ip_set_");
1960 + strcat(modulename, typename);
1961 + DP("try to load %s", modulename);
1962 + request_module(modulename);
1963 + set->type = find_set_type_rlock(typename);
1965 + if (set->type == NULL) {
1966 + ip_set_printk("no set type '%s', set '%s' not created",
1971 + __MOD_INC(set->type->me);
1972 + READ_UNLOCK(&ip_set_lock);
1975 + * Without holding any locks, create private part.
1977 + res = set->type->create(set, data, size);
1979 + __MOD_DEC(set->type->me);
1984 + /* BTW, res==0 here. */
1987 + * Here, we have a valid, constructed set. &ip_set_lock again,
1988 + * find free id/index and check that it is not already in
1991 + WRITE_LOCK(&ip_set_lock);
1992 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
1993 + DP("no free id!");
1997 + /* Make sure restore gets the same index */
1998 + if (restore != IP_SET_INVALID_ID && index != restore) {
1999 + DP("Can't restore, sets are screwed up");
2005 + * Finally! Add our shiny new set to the list, and be done.
2007 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2009 + ip_set_list[index] = set;
2010 + WRITE_UNLOCK(&ip_set_lock);
2014 + WRITE_UNLOCK(&ip_set_lock);
2015 + set->type->destroy(set);
2016 + __MOD_DEC(set->type->me);
2022 + * Destroy a given existing set
2025 +ip_set_destroy_set(ip_set_id_t index)
2027 + struct ip_set *set = ip_set_list[index];
2029 + IP_SET_ASSERT(set);
2030 + DP("set: %s", set->name);
2031 + WRITE_LOCK(&ip_set_lock);
2032 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2033 + if (set->binding != IP_SET_INVALID_ID)
2034 + __ip_set_put(set->binding);
2035 + ip_set_list[index] = NULL;
2036 + WRITE_UNLOCK(&ip_set_lock);
2038 + /* Must call it without holding any lock */
2039 + set->type->destroy(set);
2040 + __MOD_DEC(set->type->me);
2045 + * Destroy a set - or all sets
2046 + * Sets must not be referenced/used.
2049 +ip_set_destroy(ip_set_id_t index)
2053 + /* ref modification always protected by the mutex */
2054 + if (index != IP_SET_INVALID_ID) {
2055 + if (atomic_read(&ip_set_list[index]->ref))
2057 + ip_set_destroy_set(index);
2059 + for (i = 0; i < ip_set_max; i++) {
2060 + if (ip_set_list[i] != NULL
2061 + && (atomic_read(&ip_set_list[i]->ref)))
2065 + for (i = 0; i < ip_set_max; i++) {
2066 + if (ip_set_list[i] != NULL)
2067 + ip_set_destroy_set(i);
2074 +ip_set_flush_set(struct ip_set *set)
2076 + DP("set: %s %u", set->name, set->id);
2078 + write_lock_bh(&set->lock);
2079 + set->type->flush(set);
2080 + write_unlock_bh(&set->lock);
2084 + * Flush data in a set - or in all sets
2087 +ip_set_flush(ip_set_id_t index)
2089 + if (index != IP_SET_INVALID_ID) {
2090 + IP_SET_ASSERT(ip_set_list[index]);
2091 + ip_set_flush_set(ip_set_list[index]);
2093 + FOREACH_SET_DO(ip_set_flush_set);
2100 +ip_set_rename(ip_set_id_t index, const char *name)
2102 + struct ip_set *set = ip_set_list[index];
2106 + DP("set: %s to %s", set->name, name);
2107 + WRITE_LOCK(&ip_set_lock);
2108 + for (i = 0; i < ip_set_max; i++) {
2109 + if (ip_set_list[i] != NULL
2110 + && strncmp(ip_set_list[i]->name,
2112 + IP_SET_MAXNAMELEN - 1) == 0) {
2117 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2119 + WRITE_UNLOCK(&ip_set_lock);
2124 + * Swap two sets so that name/index points to the other.
2125 + * References are also swapped.
2128 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2130 + struct ip_set *from = ip_set_list[from_index];
2131 + struct ip_set *to = ip_set_list[to_index];
2132 + char from_name[IP_SET_MAXNAMELEN];
2133 + u_int32_t from_ref;
2135 + DP("set: %s to %s", from->name, to->name);
2136 + /* Type can't be changed. Artifical restriction. */
2137 + if (from->type->typecode != to->type->typecode)
2140 + /* No magic here: ref munging protected by the mutex */
2141 + WRITE_LOCK(&ip_set_lock);
2142 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2143 + from_ref = atomic_read(&from->ref);
2145 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2146 + atomic_set(&from->ref, atomic_read(&to->ref));
2147 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2148 + atomic_set(&to->ref, from_ref);
2150 + ip_set_list[from_index] = to;
2151 + ip_set_list[to_index] = from;
2153 + WRITE_UNLOCK(&ip_set_lock);
2162 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2163 + ip_set_id_t id, size_t *size)
2165 + if (set_hash->id == id)
2166 + *size += sizeof(struct ip_set_hash_list);
2170 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2171 + ip_set_id_t id, size_t *size)
2173 + if (set_hash->id == id)
2174 + *size += sizeof(struct ip_set_hash_save);
2178 +__set_hash_bindings(struct ip_set_hash *set_hash,
2179 + ip_set_id_t id, void *data, int *used)
2181 + if (set_hash->id == id) {
2182 + struct ip_set_hash_list *hash_list =
2183 + (struct ip_set_hash_list *)(data + *used);
2185 + hash_list->ip = set_hash->ip;
2186 + hash_list->binding = set_hash->binding;
2187 + *used += sizeof(struct ip_set_hash_list);
2191 +static int ip_set_list_set(ip_set_id_t index,
2196 + struct ip_set *set = ip_set_list[index];
2197 + struct ip_set_list *set_list;
2199 + /* Pointer to our header */
2200 + set_list = (struct ip_set_list *) (data + *used);
2202 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2204 + /* Get and ensure header size */
2205 + if (*used + sizeof(struct ip_set_list) > len)
2206 + goto not_enough_mem;
2207 + *used += sizeof(struct ip_set_list);
2209 + read_lock_bh(&set->lock);
2210 + /* Get and ensure set specific header size */
2211 + set_list->header_size = set->type->header_size;
2212 + if (*used + set_list->header_size > len)
2215 + /* Fill in the header */
2216 + set_list->index = index;
2217 + set_list->binding = set->binding;
2218 + set_list->ref = atomic_read(&set->ref);
2220 + /* Fill in set spefific header data */
2221 + DP("call list_header");
2222 + set->type->list_header(set, data + *used);
2223 + DP("call list_header, done");
2224 + *used += set_list->header_size;
2226 + /* Get and ensure set specific members size */
2227 + DP("call list_members_size");
2228 + set_list->members_size = set->type->list_members_size(set);
2229 + DP("call list_members_size, done");
2230 + if (*used + set_list->members_size > len)
2233 + /* Fill in set spefific members data */
2234 + DP("call list_members");
2235 + set->type->list_members(set, data + *used);
2236 + DP("call list_members, done");
2237 + *used += set_list->members_size;
2238 + read_unlock_bh(&set->lock);
2242 + /* Get and ensure set specific bindings size */
2243 + set_list->bindings_size = 0;
2244 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2245 + set->id, &set_list->bindings_size);
2246 + if (*used + set_list->bindings_size > len)
2247 + goto not_enough_mem;
2249 + /* Fill in set spefific bindings data */
2250 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2255 + read_unlock_bh(&set->lock);
2257 + DP("not enough mem, try again");
2264 +static int ip_set_save_set(ip_set_id_t index,
2269 + struct ip_set *set;
2270 + struct ip_set_save *set_save;
2272 + /* Pointer to our header */
2273 + set_save = (struct ip_set_save *) (data + *used);
2275 + /* Get and ensure header size */
2276 + if (*used + sizeof(struct ip_set_save) > len)
2277 + goto not_enough_mem;
2278 + *used += sizeof(struct ip_set_save);
2280 + set = ip_set_list[index];
2281 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2282 + data, data + *used);
2284 + read_lock_bh(&set->lock);
2285 + /* Get and ensure set specific header size */
2286 + set_save->header_size = set->type->header_size;
2287 + if (*used + set_save->header_size > len)
2290 + /* Fill in the header */
2291 + set_save->index = index;
2292 + set_save->binding = set->binding;
2294 + /* Fill in set spefific header data */
2295 + set->type->list_header(set, data + *used);
2296 + *used += set_save->header_size;
2298 + DP("set header filled: %s, used: %u %p %p", set->name, *used,
2299 + data, data + *used);
2300 + /* Get and ensure set specific members size */
2301 + set_save->members_size = set->type->list_members_size(set);
2302 + if (*used + set_save->members_size > len)
2305 + /* Fill in set spefific members data */
2306 + set->type->list_members(set, data + *used);
2307 + *used += set_save->members_size;
2308 + read_unlock_bh(&set->lock);
2309 + DP("set members filled: %s, used: %u %p %p", set->name, *used,
2310 + data, data + *used);
2314 + read_unlock_bh(&set->lock);
2316 + DP("not enough mem, try again");
2321 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2329 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2330 + struct ip_set_hash_save *hash_save =
2331 + (struct ip_set_hash_save *)(data + *used);
2332 + /* Ensure bindings size */
2333 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2337 + hash_save->id = set_hash->id;
2338 + hash_save->ip = set_hash->ip;
2339 + hash_save->binding = set_hash->binding;
2340 + *used += sizeof(struct ip_set_hash_save);
2344 +static int ip_set_save_bindings(ip_set_id_t index,
2350 + struct ip_set_save *set_save;
2352 + DP("used %u, len %u", *used, len);
2353 + /* Get and ensure header size */
2354 + if (*used + sizeof(struct ip_set_save) > len)
2358 + set_save = (struct ip_set_save *) (data + *used);
2359 + set_save->index = IP_SET_INVALID_ID;
2360 + *used += sizeof(struct ip_set_save);
2362 + DP("marker added used %u, len %u", *used, len);
2363 + /* Fill in bindings data */
2364 + if (index != IP_SET_INVALID_ID)
2365 + /* Sets are identified by id in hash */
2366 + index = ip_set_list[index]->id;
2367 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2375 +static int ip_set_restore(void *data,
2379 + int line = 0, used = 0, members_size;
2380 + struct ip_set *set;
2381 + struct ip_set_hash_save *hash_save;
2382 + struct ip_set_restore *set_restore;
2383 + ip_set_id_t index;
2385 + /* Loop to restore sets */
2389 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2390 + /* Get and ensure header size */
2391 + if (used + sizeof(struct ip_set_restore) > len)
2393 + set_restore = (struct ip_set_restore *) (data + used);
2394 + used += sizeof(struct ip_set_restore);
2396 + /* Ensure data size */
2398 + + set_restore->header_size
2399 + + set_restore->members_size > len)
2402 + /* Check marker */
2403 + if (set_restore->index == IP_SET_INVALID_ID) {
2408 + /* Try to create the set */
2409 + DP("restore %s %s", set_restore->name, set_restore->typename);
2410 + res = ip_set_create(set_restore->name,
2411 + set_restore->typename,
2412 + set_restore->index,
2414 + set_restore->header_size);
2418 + used += set_restore->header_size;
2420 + index = ip_set_find_byindex(set_restore->index);
2421 + DP("index %u, restore_index %u", index, set_restore->index);
2422 + if (index != set_restore->index)
2424 + /* Try to restore members data */
2425 + set = ip_set_list[index];
2427 + DP("members_size %u reqsize %u",
2428 + set_restore->members_size, set->type->reqsize);
2429 + while (members_size + set->type->reqsize <=
2430 + set_restore->members_size) {
2432 + DP("members: %u, line %u", members_size, line);
2433 + res = __ip_set_addip(index,
2434 + data + used + members_size,
2435 + set->type->reqsize);
2436 + if (!(res == 0 || res == -EEXIST))
2438 + members_size += set->type->reqsize;
2441 + DP("members_size %u %u",
2442 + set_restore->members_size, members_size);
2443 + if (members_size != set_restore->members_size)
2445 + used += set_restore->members_size;
2449 + /* Loop to restore bindings */
2450 + while (used < len) {
2453 + DP("restore binding, line %u", line);
2454 + /* Get and ensure size */
2455 + if (used + sizeof(struct ip_set_hash_save) > len)
2457 + hash_save = (struct ip_set_hash_save *) (data + used);
2458 + used += sizeof(struct ip_set_hash_save);
2460 + /* hash_save->id is used to store the index */
2461 + index = ip_set_find_byindex(hash_save->id);
2462 + DP("restore binding index %u, id %u, %u -> %u",
2463 + index, hash_save->id, hash_save->ip, hash_save->binding);
2464 + if (index != hash_save->id)
2467 + set = ip_set_list[hash_save->id];
2468 + /* Null valued IP means default binding */
2469 + if (hash_save->ip)
2470 + res = ip_set_hash_add(set->id,
2472 + hash_save->binding);
2474 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2475 + WRITE_LOCK(&ip_set_lock);
2476 + set->binding = hash_save->binding;
2477 + __ip_set_get(set->binding);
2478 + WRITE_UNLOCK(&ip_set_lock);
2479 + DP("default binding: %u", set->binding);
2491 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2494 + int res = 0; /* Assume OK */
2496 + struct ip_set_req_adt *req_adt;
2497 + ip_set_id_t index = IP_SET_INVALID_ID;
2498 + int (*adtfn)(ip_set_id_t index,
2499 + const void *data, size_t size);
2501 + int (*fn)(ip_set_id_t index,
2502 + const void *data, size_t size);
2504 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2505 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2508 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2509 + if (!capable(CAP_NET_ADMIN))
2511 + if (optval != SO_IP_SET)
2513 + if (len <= sizeof(unsigned)) {
2514 + ip_set_printk("short userdata (want >%zu, got %u)",
2515 + sizeof(unsigned), len);
2518 + data = vmalloc(len);
2520 + DP("out of mem for %u bytes", len);
2523 + if (copy_from_user(data, user, len) != 0) {
2527 + if (down_interruptible(&ip_set_app_mutex)) {
2532 + op = (unsigned *)data;
2535 + if (*op < IP_SET_OP_VERSION) {
2536 + /* Check the version at the beginning of operations */
2537 + struct ip_set_req_version *req_version =
2538 + (struct ip_set_req_version *) data;
2539 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2546 + case IP_SET_OP_CREATE:{
2547 + struct ip_set_req_create *req_create
2548 + = (struct ip_set_req_create *) data;
2550 + if (len <= sizeof(struct ip_set_req_create)) {
2551 + ip_set_printk("short CREATE data (want >%zu, got %u)",
2552 + sizeof(struct ip_set_req_create), len);
2556 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2557 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2558 + res = ip_set_create(req_create->name,
2559 + req_create->typename,
2560 + IP_SET_INVALID_ID,
2561 + data + sizeof(struct ip_set_req_create),
2562 + len - sizeof(struct ip_set_req_create));
2565 + case IP_SET_OP_DESTROY:{
2566 + struct ip_set_req_std *req_destroy
2567 + = (struct ip_set_req_std *) data;
2569 + if (len != sizeof(struct ip_set_req_std)) {
2570 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2571 + sizeof(struct ip_set_req_std), len);
2575 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2576 + /* Destroy all sets */
2577 + index = IP_SET_INVALID_ID;
2579 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2580 + index = ip_set_find_byname(req_destroy->name);
2582 + if (index == IP_SET_INVALID_ID) {
2588 + res = ip_set_destroy(index);
2591 + case IP_SET_OP_FLUSH:{
2592 + struct ip_set_req_std *req_flush =
2593 + (struct ip_set_req_std *) data;
2595 + if (len != sizeof(struct ip_set_req_std)) {
2596 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2597 + sizeof(struct ip_set_req_std), len);
2601 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2602 + /* Flush all sets */
2603 + index = IP_SET_INVALID_ID;
2605 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2606 + index = ip_set_find_byname(req_flush->name);
2608 + if (index == IP_SET_INVALID_ID) {
2613 + res = ip_set_flush(index);
2616 + case IP_SET_OP_RENAME:{
2617 + struct ip_set_req_create *req_rename
2618 + = (struct ip_set_req_create *) data;
2620 + if (len != sizeof(struct ip_set_req_create)) {
2621 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2622 + sizeof(struct ip_set_req_create), len);
2627 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2628 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2630 + index = ip_set_find_byname(req_rename->name);
2631 + if (index == IP_SET_INVALID_ID) {
2635 + res = ip_set_rename(index, req_rename->typename);
2638 + case IP_SET_OP_SWAP:{
2639 + struct ip_set_req_create *req_swap
2640 + = (struct ip_set_req_create *) data;
2641 + ip_set_id_t to_index;
2643 + if (len != sizeof(struct ip_set_req_create)) {
2644 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2645 + sizeof(struct ip_set_req_create), len);
2650 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2651 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2653 + index = ip_set_find_byname(req_swap->name);
2654 + if (index == IP_SET_INVALID_ID) {
2658 + to_index = ip_set_find_byname(req_swap->typename);
2659 + if (to_index == IP_SET_INVALID_ID) {
2663 + res = ip_set_swap(index, to_index);
2667 + break; /* Set identified by id */
2670 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2671 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2675 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2677 + if (len < sizeof(struct ip_set_req_adt)) {
2678 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2679 + sizeof(struct ip_set_req_adt), len);
2683 + req_adt = (struct ip_set_req_adt *) data;
2685 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2686 + if (!(*op == IP_SET_OP_UNBIND_SET
2687 + && req_adt->index == IP_SET_INVALID_ID)) {
2688 + index = ip_set_find_byindex(req_adt->index);
2689 + if (index == IP_SET_INVALID_ID) {
2694 + res = adtfn(index, data, len);
2697 + up(&ip_set_app_mutex);
2701 + DP("final result %d", res);
2706 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2710 + ip_set_id_t index = IP_SET_INVALID_ID;
2712 + int copylen = *len;
2714 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2715 + if (!capable(CAP_NET_ADMIN))
2717 + if (optval != SO_IP_SET)
2719 + if (*len < sizeof(unsigned)) {
2720 + ip_set_printk("short userdata (want >=%zu, got %d)",
2721 + sizeof(unsigned), *len);
2724 + data = vmalloc(*len);
2726 + DP("out of mem for %d bytes", *len);
2729 + if (copy_from_user(data, user, *len) != 0) {
2733 + if (down_interruptible(&ip_set_app_mutex)) {
2738 + op = (unsigned *) data;
2741 + if (*op < IP_SET_OP_VERSION) {
2742 + /* Check the version at the beginning of operations */
2743 + struct ip_set_req_version *req_version =
2744 + (struct ip_set_req_version *) data;
2745 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2752 + case IP_SET_OP_VERSION: {
2753 + struct ip_set_req_version *req_version =
2754 + (struct ip_set_req_version *) data;
2756 + if (*len != sizeof(struct ip_set_req_version)) {
2757 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2758 + sizeof(struct ip_set_req_version),
2764 + req_version->version = IP_SET_PROTOCOL_VERSION;
2765 + res = copy_to_user(user, req_version,
2766 + sizeof(struct ip_set_req_version));
2769 + case IP_SET_OP_GET_BYNAME: {
2770 + struct ip_set_req_get_set *req_get
2771 + = (struct ip_set_req_get_set *) data;
2773 + if (*len != sizeof(struct ip_set_req_get_set)) {
2774 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2775 + sizeof(struct ip_set_req_get_set), *len);
2779 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2780 + index = ip_set_find_byname(req_get->set.name);
2781 + req_get->set.index = index;
2784 + case IP_SET_OP_GET_BYINDEX: {
2785 + struct ip_set_req_get_set *req_get
2786 + = (struct ip_set_req_get_set *) data;
2788 + if (*len != sizeof(struct ip_set_req_get_set)) {
2789 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2790 + sizeof(struct ip_set_req_get_set), *len);
2794 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2795 + index = ip_set_find_byindex(req_get->set.index);
2796 + strncpy(req_get->set.name,
2797 + index == IP_SET_INVALID_ID ? ""
2798 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2801 + case IP_SET_OP_ADT_GET: {
2802 + struct ip_set_req_adt_get *req_get
2803 + = (struct ip_set_req_adt_get *) data;
2805 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2806 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2807 + sizeof(struct ip_set_req_adt_get), *len);
2811 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2812 + index = ip_set_find_byname(req_get->set.name);
2813 + if (index != IP_SET_INVALID_ID) {
2814 + req_get->set.index = index;
2815 + strncpy(req_get->typename,
2816 + ip_set_list[index]->type->typename,
2817 + IP_SET_MAXNAMELEN - 1);
2824 + case IP_SET_OP_MAX_SETS: {
2825 + struct ip_set_req_max_sets *req_max_sets
2826 + = (struct ip_set_req_max_sets *) data;
2829 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2830 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2831 + sizeof(struct ip_set_req_max_sets), *len);
2836 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2837 + req_max_sets->set.index = IP_SET_INVALID_ID;
2839 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2840 + req_max_sets->set.index =
2841 + ip_set_find_byname(req_max_sets->set.name);
2842 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2847 + req_max_sets->max_sets = ip_set_max;
2848 + req_max_sets->sets = 0;
2849 + for (i = 0; i < ip_set_max; i++) {
2850 + if (ip_set_list[i] != NULL)
2851 + req_max_sets->sets++;
2855 + case IP_SET_OP_LIST_SIZE:
2856 + case IP_SET_OP_SAVE_SIZE: {
2857 + struct ip_set_req_setnames *req_setnames
2858 + = (struct ip_set_req_setnames *) data;
2859 + struct ip_set_name_list *name_list;
2860 + struct ip_set *set;
2864 + if (*len < sizeof(struct ip_set_req_setnames)) {
2865 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2866 + sizeof(struct ip_set_req_setnames), *len);
2871 + req_setnames->size = 0;
2872 + used = sizeof(struct ip_set_req_setnames);
2873 + for (i = 0; i < ip_set_max; i++) {
2874 + if (ip_set_list[i] == NULL)
2876 + name_list = (struct ip_set_name_list *)
2878 + used += sizeof(struct ip_set_name_list);
2879 + if (used > copylen) {
2883 + set = ip_set_list[i];
2884 + /* Fill in index, name, etc. */
2885 + name_list->index = i;
2886 + name_list->id = set->id;
2887 + strncpy(name_list->name,
2889 + IP_SET_MAXNAMELEN - 1);
2890 + strncpy(name_list->typename,
2891 + set->type->typename,
2892 + IP_SET_MAXNAMELEN - 1);
2893 + DP("filled %s of type %s, index %u\n",
2894 + name_list->name, name_list->typename,
2895 + name_list->index);
2896 + if (!(req_setnames->index == IP_SET_INVALID_ID
2897 + || req_setnames->index == i))
2901 + case IP_SET_OP_LIST_SIZE: {
2902 + req_setnames->size += sizeof(struct ip_set_list)
2903 + + set->type->header_size
2904 + + set->type->list_members_size(set);
2905 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2906 + i, &req_setnames->size);
2909 + case IP_SET_OP_SAVE_SIZE: {
2910 + req_setnames->size += sizeof(struct ip_set_save)
2911 + + set->type->header_size
2912 + + set->type->list_members_size(set);
2913 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2914 + i, &req_setnames->size);
2921 + if (copylen != used) {
2927 + case IP_SET_OP_LIST: {
2928 + struct ip_set_req_list *req_list
2929 + = (struct ip_set_req_list *) data;
2933 + if (*len < sizeof(struct ip_set_req_list)) {
2934 + ip_set_printk("short LIST (want >=%zu, got %d)",
2935 + sizeof(struct ip_set_req_list), *len);
2939 + index = req_list->index;
2940 + if (index != IP_SET_INVALID_ID
2941 + && ip_set_find_byindex(index) != index) {
2946 + if (index == IP_SET_INVALID_ID) {
2947 + /* List all sets */
2948 + for (i = 0; i < ip_set_max && res == 0; i++) {
2949 + if (ip_set_list[i] != NULL)
2950 + res = ip_set_list_set(i, data, &used, *len);
2953 + /* List an individual set */
2954 + res = ip_set_list_set(index, data, &used, *len);
2958 + else if (copylen != used) {
2964 + case IP_SET_OP_SAVE: {
2965 + struct ip_set_req_list *req_save
2966 + = (struct ip_set_req_list *) data;
2970 + if (*len < sizeof(struct ip_set_req_list)) {
2971 + ip_set_printk("short SAVE (want >=%zu, got %d)",
2972 + sizeof(struct ip_set_req_list), *len);
2976 + index = req_save->index;
2977 + if (index != IP_SET_INVALID_ID
2978 + && ip_set_find_byindex(index) != index) {
2983 + if (index == IP_SET_INVALID_ID) {
2984 + /* Save all sets */
2985 + for (i = 0; i < ip_set_max && res == 0; i++) {
2986 + if (ip_set_list[i] != NULL)
2987 + res = ip_set_save_set(i, data, &used, *len);
2990 + /* Save an individual set */
2991 + res = ip_set_save_set(index, data, &used, *len);
2994 + res = ip_set_save_bindings(index, data, &used, *len);
2998 + else if (copylen != used) {
3004 + case IP_SET_OP_RESTORE: {
3005 + struct ip_set_req_setnames *req_restore
3006 + = (struct ip_set_req_setnames *) data;
3009 + if (*len < sizeof(struct ip_set_req_setnames)
3010 + || *len != req_restore->size) {
3011 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3012 + req_restore->size, *len);
3016 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3017 + req_restore->size - sizeof(struct ip_set_req_setnames));
3018 + DP("ip_set_restore: %u", line);
3021 + req_restore->size = line;
3022 + copylen = sizeof(struct ip_set_req_setnames);
3030 + } /* end of switch(op) */
3033 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3034 + && ip_set_list[index]
3035 + ? ip_set_list[index]->name
3036 + : ":all:", copylen);
3038 + res = copy_to_user(user, data, copylen);
3040 + copy_to_user(user, data, copylen);
3043 + up(&ip_set_app_mutex);
3047 + DP("final result %d", res);
3051 +static struct nf_sockopt_ops so_set = {
3053 + .set_optmin = SO_IP_SET,
3054 + .set_optmax = SO_IP_SET + 1,
3055 + .set = &ip_set_sockfn_set,
3056 + .get_optmin = SO_IP_SET,
3057 + .get_optmax = SO_IP_SET + 1,
3058 + .get = &ip_set_sockfn_get,
3062 +static int max_sets, hash_size;
3063 +MODULE_PARM(max_sets, "i");
3064 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3065 +MODULE_PARM(hash_size, "i");
3066 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3067 +MODULE_LICENSE("GPL");
3068 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3069 +MODULE_DESCRIPTION("module implementing core IP set support");
3071 +static int __init init(void)
3076 + get_random_bytes(&ip_set_hash_random, 4);
3078 + ip_set_max = max_sets;
3079 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3080 + if (!ip_set_list) {
3081 + printk(KERN_ERR "Unable to create ip_set_list\n");
3084 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3086 + ip_set_bindings_hash_size = hash_size;
3087 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3088 + if (!ip_set_hash) {
3089 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3090 + vfree(ip_set_list);
3093 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3094 + INIT_LIST_HEAD(&ip_set_hash[i]);
3096 + INIT_LIST_HEAD(&set_type_list);
3098 + res = nf_register_sockopt(&so_set);
3100 + ip_set_printk("SO_SET registry failed: %d", res);
3101 + vfree(ip_set_list);
3102 + vfree(ip_set_hash);
3108 +static void __exit fini(void)
3110 + /* There can't be any existing set or binding. Racy. */
3111 + nf_unregister_sockopt(&so_set);
3112 + vfree(ip_set_list);
3113 + vfree(ip_set_hash);
3114 + DP("these are the famous last words");
3117 +EXPORT_SYMBOL(ip_set_register_set_type);
3118 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3120 +EXPORT_SYMBOL(ip_set_get_byname);
3121 +EXPORT_SYMBOL(ip_set_get_byindex);
3122 +EXPORT_SYMBOL(ip_set_put);
3124 +EXPORT_SYMBOL(ip_set_addip_kernel);
3125 +EXPORT_SYMBOL(ip_set_delip_kernel);
3126 +EXPORT_SYMBOL(ip_set_testip_kernel);
3130 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_iphash.c
3131 ===================================================================
3132 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
3133 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_iphash.c 2007-12-15 05:20:07.896290225 +0100
3135 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3137 + * This program is free software; you can redistribute it and/or modify
3138 + * it under the terms of the GNU General Public License version 2 as
3139 + * published by the Free Software Foundation.
3142 +/* Kernel module implementing an ip hash set */
3144 +#include <linux/module.h>
3145 +#include <linux/ip.h>
3146 +#include <linux/skbuff.h>
3147 +#include <linux/netfilter_ipv4/ip_tables.h>
3148 +#include <linux/netfilter_ipv4/ip_set.h>
3149 +#include <linux/errno.h>
3150 +#include <asm/uaccess.h>
3151 +#include <asm/bitops.h>
3152 +#include <asm/softirq.h>
3153 +#include <linux/spinlock.h>
3154 +#include <linux/vmalloc.h>
3155 +#include <linux/random.h>
3157 +#include <net/ip.h>
3159 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3160 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3161 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
3162 +#include <linux/netfilter_ipv4/ip_set_prime.h>
3164 +static inline __u32
3165 +jhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3167 + return jhash_1word(ip, map->initval);
3170 +static inline __u32
3171 +randhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3173 + return (1 + ip % map->prime);
3176 +static inline __u32
3177 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3179 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3180 + __u32 jhash, randhash, id;
3183 + *hash_ip = ip & map->netmask;
3184 + jhash = jhash_ip(map, *hash_ip);
3185 + randhash = randhash_ip(map, *hash_ip);
3186 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3187 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3189 + for (i = 0; i < map->probes; i++) {
3190 + id = (jhash + i * randhash) % map->hashsize;
3191 + DP("hash key: %u", id);
3192 + if (map->members[id] == *hash_ip)
3194 + /* No shortcut at testing - there can be deleted
3201 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3203 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
3207 +testip(struct ip_set *set, const void *data, size_t size,
3208 + ip_set_ip_t *hash_ip)
3210 + struct ip_set_req_iphash *req =
3211 + (struct ip_set_req_iphash *) data;
3213 + if (size != sizeof(struct ip_set_req_iphash)) {
3214 + ip_set_printk("data length wrong (want %zu, have %zu)",
3215 + sizeof(struct ip_set_req_iphash),
3219 + return __testip(set, req->ip, hash_ip);
3223 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
3224 + u_int32_t flags, ip_set_ip_t *hash_ip)
3226 + return __testip(set,
3227 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3228 + : skb->nh.iph->daddr),
3233 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3235 + __u32 jhash, randhash, probe;
3238 + *hash_ip = ip & map->netmask;
3239 + jhash = jhash_ip(map, *hash_ip);
3240 + randhash = randhash_ip(map, *hash_ip);
3242 + for (i = 0; i < map->probes; i++) {
3243 + probe = (jhash + i * randhash) % map->hashsize;
3244 + if (map->members[probe] == *hash_ip)
3246 + if (!map->members[probe]) {
3247 + map->members[probe] = *hash_ip;
3251 + /* Trigger rehashing */
3256 +addip(struct ip_set *set, const void *data, size_t size,
3257 + ip_set_ip_t *hash_ip)
3259 + struct ip_set_req_iphash *req =
3260 + (struct ip_set_req_iphash *) data;
3262 + if (size != sizeof(struct ip_set_req_iphash)) {
3263 + ip_set_printk("data length wrong (want %zu, have %zu)",
3264 + sizeof(struct ip_set_req_iphash),
3268 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3272 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3273 + u_int32_t flags, ip_set_ip_t *hash_ip)
3275 + return __addip((struct ip_set_iphash *) set->data,
3276 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3277 + : skb->nh.iph->daddr),
3281 +static int retry(struct ip_set *set)
3283 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3284 + ip_set_ip_t hash_ip, *members;
3285 + u_int32_t i, hashsize;
3286 + unsigned newbytes;
3288 + struct ip_set_iphash tmp = {
3289 + .hashsize = map->hashsize,
3290 + .probes = map->probes,
3291 + .resize = map->resize,
3292 + .netmask = map->netmask,
3295 + if (map->resize == 0)
3301 + /* Calculate new parameters */
3302 + get_random_bytes(&tmp.initval, 4);
3303 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
3304 + if (hashsize == tmp.hashsize)
3306 + tmp.prime = make_prime(hashsize);
3308 + ip_set_printk("rehashing of set %s triggered: "
3309 + "hashsize grows from %u to %u",
3310 + set->name, tmp.hashsize, hashsize);
3311 + tmp.hashsize = hashsize;
3313 + newbytes = hashsize * sizeof(ip_set_ip_t);
3314 + tmp.members = ip_set_malloc(newbytes);
3315 + if (!tmp.members) {
3316 + DP("out of memory for %d bytes", newbytes);
3319 + memset(tmp.members, 0, newbytes);
3321 + write_lock_bh(&set->lock);
3322 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3323 + for (i = 0; i < map->hashsize && res == 0; i++) {
3324 + if (map->members[i])
3325 + res = __addip(&tmp, map->members[i], &hash_ip);
3328 + /* Failure, try again */
3329 + write_unlock_bh(&set->lock);
3330 + ip_set_free(tmp.members, newbytes);
3334 + /* Success at resizing! */
3335 + members = map->members;
3336 + hashsize = map->hashsize;
3338 + map->initval = tmp.initval;
3339 + map->prime = tmp.prime;
3340 + map->hashsize = tmp.hashsize;
3341 + map->members = tmp.members;
3342 + write_unlock_bh(&set->lock);
3344 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
3350 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3352 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3353 + ip_set_ip_t id = hash_id(set, ip, hash_ip);
3355 + if (id == UINT_MAX)
3358 + map->members[id] = 0;
3363 +delip(struct ip_set *set, const void *data, size_t size,
3364 + ip_set_ip_t *hash_ip)
3366 + struct ip_set_req_iphash *req =
3367 + (struct ip_set_req_iphash *) data;
3369 + if (size != sizeof(struct ip_set_req_iphash)) {
3370 + ip_set_printk("data length wrong (want %zu, have %zu)",
3371 + sizeof(struct ip_set_req_iphash),
3375 + return __delip(set, req->ip, hash_ip);
3379 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3380 + u_int32_t flags, ip_set_ip_t *hash_ip)
3382 + return __delip(set,
3383 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3384 + : skb->nh.iph->daddr),
3388 +static int create(struct ip_set *set, const void *data, size_t size)
3390 + unsigned newbytes;
3391 + struct ip_set_req_iphash_create *req =
3392 + (struct ip_set_req_iphash_create *) data;
3393 + struct ip_set_iphash *map;
3395 + if (size != sizeof(struct ip_set_req_iphash_create)) {
3396 + ip_set_printk("data length wrong (want %zu, have %zu)",
3397 + sizeof(struct ip_set_req_iphash_create),
3402 + if (req->hashsize < 1) {
3403 + ip_set_printk("hashsize too small");
3407 + map = kmalloc(sizeof(struct ip_set_iphash), GFP_KERNEL);
3409 + DP("out of memory for %d bytes",
3410 + sizeof(struct ip_set_iphash));
3413 + get_random_bytes(&map->initval, 4);
3414 + map->prime = make_prime(req->hashsize);
3415 + map->hashsize = req->hashsize;
3416 + map->probes = req->probes;
3417 + map->resize = req->resize;
3418 + map->netmask = req->netmask;
3419 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
3420 + map->members = ip_set_malloc(newbytes);
3421 + if (!map->members) {
3422 + DP("out of memory for %d bytes", newbytes);
3426 + memset(map->members, 0, newbytes);
3432 +static void destroy(struct ip_set *set)
3434 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3436 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
3442 +static void flush(struct ip_set *set)
3444 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3445 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
3448 +static void list_header(const struct ip_set *set, void *data)
3450 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3451 + struct ip_set_req_iphash_create *header =
3452 + (struct ip_set_req_iphash_create *) data;
3454 + header->hashsize = map->hashsize;
3455 + header->probes = map->probes;
3456 + header->resize = map->resize;
3457 + header->netmask = map->netmask;
3460 +static int list_members_size(const struct ip_set *set)
3462 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3464 + return (map->hashsize * sizeof(ip_set_ip_t));
3467 +static void list_members(const struct ip_set *set, void *data)
3469 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3470 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
3472 + memcpy(data, map->members, bytes);
3475 +static struct ip_set_type ip_set_iphash = {
3476 + .typename = SETTYPE_NAME,
3477 + .typecode = IPSET_TYPE_IP,
3478 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3479 + .create = &create,
3480 + .destroy = &destroy,
3482 + .reqsize = sizeof(struct ip_set_req_iphash),
3484 + .addip_kernel = &addip_kernel,
3487 + .delip_kernel = &delip_kernel,
3488 + .testip = &testip,
3489 + .testip_kernel = &testip_kernel,
3490 + .header_size = sizeof(struct ip_set_req_iphash_create),
3491 + .list_header = &list_header,
3492 + .list_members_size = &list_members_size,
3493 + .list_members = &list_members,
3494 + .me = THIS_MODULE,
3497 +MODULE_LICENSE("GPL");
3498 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3499 +MODULE_DESCRIPTION("iphash type of IP sets");
3501 +static int __init init(void)
3503 + return ip_set_register_set_type(&ip_set_iphash);
3506 +static void __exit fini(void)
3508 + /* FIXME: possible race with ip_set_create() */
3509 + ip_set_unregister_set_type(&ip_set_iphash);
3514 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_ipmap.c
3515 ===================================================================
3516 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
3517 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_ipmap.c 2007-12-15 05:20:07.896290225 +0100
3519 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3520 + * Patrick Schaaf <bof@bof.de>
3521 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3523 + * This program is free software; you can redistribute it and/or modify
3524 + * it under the terms of the GNU General Public License version 2 as
3525 + * published by the Free Software Foundation.
3528 +/* Kernel module implementing an IP set type: the single bitmap type */
3530 +#include <linux/module.h>
3531 +#include <linux/ip.h>
3532 +#include <linux/skbuff.h>
3533 +#include <linux/netfilter_ipv4/ip_tables.h>
3534 +#include <linux/netfilter_ipv4/ip_set.h>
3535 +#include <linux/errno.h>
3536 +#include <asm/uaccess.h>
3537 +#include <asm/bitops.h>
3538 +#include <asm/softirq.h>
3539 +#include <linux/spinlock.h>
3541 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3543 +static inline ip_set_ip_t
3544 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3546 + return (ip - map->first_ip)/map->hosts;
3550 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3552 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3554 + if (ip < map->first_ip || ip > map->last_ip)
3557 + *hash_ip = ip & map->netmask;
3558 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3559 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3560 + return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3564 +testip(struct ip_set *set, const void *data, size_t size,
3565 + ip_set_ip_t *hash_ip)
3567 + struct ip_set_req_ipmap *req =
3568 + (struct ip_set_req_ipmap *) data;
3570 + if (size != sizeof(struct ip_set_req_ipmap)) {
3571 + ip_set_printk("data length wrong (want %zu, have %zu)",
3572 + sizeof(struct ip_set_req_ipmap),
3576 + return __testip(set, req->ip, hash_ip);
3580 +testip_kernel(struct ip_set *set,
3581 + const struct sk_buff *skb,
3583 + ip_set_ip_t *hash_ip)
3587 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3588 + flags & IPSET_SRC ? "SRC" : "DST",
3589 + NIPQUAD(skb->nh.iph->saddr),
3590 + NIPQUAD(skb->nh.iph->daddr));
3592 + res = __testip(set,
3593 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3594 + : skb->nh.iph->daddr),
3596 + return (res < 0 ? 0 : res);
3600 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3602 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3604 + if (ip < map->first_ip || ip > map->last_ip)
3607 + *hash_ip = ip & map->netmask;
3608 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3609 + if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3616 +addip(struct ip_set *set, const void *data, size_t size,
3617 + ip_set_ip_t *hash_ip)
3619 + struct ip_set_req_ipmap *req =
3620 + (struct ip_set_req_ipmap *) data;
3622 + if (size != sizeof(struct ip_set_req_ipmap)) {
3623 + ip_set_printk("data length wrong (want %zu, have %zu)",
3624 + sizeof(struct ip_set_req_ipmap),
3628 + DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3629 + return __addip(set, req->ip, hash_ip);
3633 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3634 + u_int32_t flags, ip_set_ip_t *hash_ip)
3636 + return __addip(set,
3637 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3638 + : skb->nh.iph->daddr),
3643 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3645 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3647 + if (ip < map->first_ip || ip > map->last_ip)
3650 + *hash_ip = ip & map->netmask;
3651 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3652 + if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3659 +delip(struct ip_set *set, const void *data, size_t size,
3660 + ip_set_ip_t *hash_ip)
3662 + struct ip_set_req_ipmap *req =
3663 + (struct ip_set_req_ipmap *) data;
3665 + if (size != sizeof(struct ip_set_req_ipmap)) {
3666 + ip_set_printk("data length wrong (want %zu, have %zu)",
3667 + sizeof(struct ip_set_req_ipmap),
3671 + return __delip(set, req->ip, hash_ip);
3675 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3676 + u_int32_t flags, ip_set_ip_t *hash_ip)
3678 + return __delip(set,
3679 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3680 + : skb->nh.iph->daddr),
3684 +static int create(struct ip_set *set, const void *data, size_t size)
3687 + struct ip_set_req_ipmap_create *req =
3688 + (struct ip_set_req_ipmap_create *) data;
3689 + struct ip_set_ipmap *map;
3691 + if (size != sizeof(struct ip_set_req_ipmap_create)) {
3692 + ip_set_printk("data length wrong (want %zu, have %zu)",
3693 + sizeof(struct ip_set_req_ipmap_create),
3698 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3699 + HIPQUAD(req->from), HIPQUAD(req->to));
3701 + if (req->from > req->to) {
3702 + DP("bad ip range");
3706 + if (req->to - req->from > MAX_RANGE) {
3707 + ip_set_printk("range too big (max %d addresses)",
3712 + map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3714 + DP("out of memory for %d bytes",
3715 + sizeof(struct ip_set_ipmap));
3718 + map->first_ip = req->from;
3719 + map->last_ip = req->to;
3720 + map->netmask = req->netmask;
3722 + if (req->netmask == 0xFFFFFFFF) {
3724 + map->sizeid = map->last_ip - map->first_ip + 1;
3726 + unsigned int mask_bits, netmask_bits;
3729 + map->first_ip &= map->netmask; /* Should we better bark? */
3731 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3732 + netmask_bits = mask_to_bits(map->netmask);
3734 + if (!mask || netmask_bits <= mask_bits)
3737 + map->hosts = 2 << (32 - netmask_bits - 1);
3738 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3740 + newbytes = bitmap_bytes(0, map->sizeid - 1);
3741 + map->members = kmalloc(newbytes, GFP_KERNEL);
3742 + if (!map->members) {
3743 + DP("out of memory for %d bytes", newbytes);
3747 + memset(map->members, 0, newbytes);
3753 +static void destroy(struct ip_set *set)
3755 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3757 + kfree(map->members);
3763 +static void flush(struct ip_set *set)
3765 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3766 + memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3769 +static void list_header(const struct ip_set *set, void *data)
3771 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3772 + struct ip_set_req_ipmap_create *header =
3773 + (struct ip_set_req_ipmap_create *) data;
3775 + header->from = map->first_ip;
3776 + header->to = map->last_ip;
3777 + header->netmask = map->netmask;
3780 +static int list_members_size(const struct ip_set *set)
3782 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3784 + return bitmap_bytes(0, map->sizeid - 1);
3787 +static void list_members(const struct ip_set *set, void *data)
3789 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3790 + int bytes = bitmap_bytes(0, map->sizeid - 1);
3792 + memcpy(data, map->members, bytes);
3795 +static struct ip_set_type ip_set_ipmap = {
3796 + .typename = SETTYPE_NAME,
3797 + .typecode = IPSET_TYPE_IP,
3798 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3799 + .create = &create,
3800 + .destroy = &destroy,
3802 + .reqsize = sizeof(struct ip_set_req_ipmap),
3804 + .addip_kernel = &addip_kernel,
3806 + .delip_kernel = &delip_kernel,
3807 + .testip = &testip,
3808 + .testip_kernel = &testip_kernel,
3809 + .header_size = sizeof(struct ip_set_req_ipmap_create),
3810 + .list_header = &list_header,
3811 + .list_members_size = &list_members_size,
3812 + .list_members = &list_members,
3813 + .me = THIS_MODULE,
3816 +MODULE_LICENSE("GPL");
3817 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3818 +MODULE_DESCRIPTION("ipmap type of IP sets");
3820 +static int __init init(void)
3822 + return ip_set_register_set_type(&ip_set_ipmap);
3825 +static void __exit fini(void)
3827 + /* FIXME: possible race with ip_set_create() */
3828 + ip_set_unregister_set_type(&ip_set_ipmap);
3833 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_iptree.c
3834 ===================================================================
3835 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
3836 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_iptree.c 2007-12-15 05:20:07.896290225 +0100
3838 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3840 + * This program is free software; you can redistribute it and/or modify
3841 + * it under the terms of the GNU General Public License version 2 as
3842 + * published by the Free Software Foundation.
3845 +/* Kernel module implementing an IP set type: the iptree type */
3847 +#include <linux/module.h>
3848 +#include <linux/ip.h>
3849 +#include <linux/skbuff.h>
3850 +#include <linux/slab.h>
3851 +#include <linux/delay.h>
3852 +#include <linux/netfilter_ipv4/ip_tables.h>
3853 +#include <linux/netfilter_ipv4/ip_set.h>
3854 +#include <linux/errno.h>
3855 +#include <asm/uaccess.h>
3856 +#include <asm/bitops.h>
3857 +#include <linux/interrupt.h>
3858 +#include <linux/spinlock.h>
3860 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
3862 +/* Garbage collection interval in seconds: */
3863 +#define IPTREE_GC_TIME 5*60
3864 +/* Sleep so many milliseconds before trying again
3865 + * to delete the gc timer at destroying a set */
3866 +#define IPTREE_DESTROY_SLEEP 100
3868 +static kmem_cache_t *branch_cachep;
3869 +static kmem_cache_t *leaf_cachep;
3871 +#define ABCD(a,b,c,d,addrp) do { \
3872 + a = ((unsigned char *)addrp)[3]; \
3873 + b = ((unsigned char *)addrp)[2]; \
3874 + c = ((unsigned char *)addrp)[1]; \
3875 + d = ((unsigned char *)addrp)[0]; \
3878 +#define TESTIP_WALK(map, elem, branch) do { \
3879 + if ((map)->tree[elem]) { \
3880 + branch = (map)->tree[elem]; \
3886 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3888 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3889 + struct ip_set_iptreeb *btree;
3890 + struct ip_set_iptreec *ctree;
3891 + struct ip_set_iptreed *dtree;
3892 + unsigned char a,b,c,d;
3895 + ABCD(a, b, c, d, hash_ip);
3896 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
3897 + TESTIP_WALK(map, a, btree);
3898 + TESTIP_WALK(btree, b, ctree);
3899 + TESTIP_WALK(ctree, c, dtree);
3900 + DP("%lu %lu", dtree->expires[d], jiffies);
3901 + return !!(map->timeout ? (time_after(dtree->expires[d], jiffies))
3902 + : dtree->expires[d]);
3906 +testip(struct ip_set *set, const void *data, size_t size,
3907 + ip_set_ip_t *hash_ip)
3909 + struct ip_set_req_iptree *req =
3910 + (struct ip_set_req_iptree *) data;
3912 + if (size != sizeof(struct ip_set_req_iptree)) {
3913 + ip_set_printk("data length wrong (want %zu, have %zu)",
3914 + sizeof(struct ip_set_req_iptree),
3918 + return __testip(set, req->ip, hash_ip);
3922 +testip_kernel(struct ip_set *set,
3923 + const struct sk_buff *skb,
3925 + ip_set_ip_t *hash_ip)
3929 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3930 + flags & IPSET_SRC ? "SRC" : "DST",
3931 + NIPQUAD(skb->nh.iph->saddr),
3932 + NIPQUAD(skb->nh.iph->daddr));
3934 + res = __testip(set,
3935 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3936 + : skb->nh.iph->daddr),
3938 + return (res < 0 ? 0 : res);
3941 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
3942 + if ((map)->tree[elem]) { \
3943 + DP("found %u", elem); \
3944 + branch = (map)->tree[elem]; \
3946 + branch = (type *) \
3947 + kmem_cache_alloc(cachep, GFP_KERNEL); \
3948 + if (branch == NULL) \
3950 + memset(branch, 0, sizeof(*branch)); \
3951 + (map)->tree[elem] = branch; \
3952 + DP("alloc %u", elem); \
3957 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
3958 + ip_set_ip_t *hash_ip)
3960 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3961 + struct ip_set_iptreeb *btree;
3962 + struct ip_set_iptreec *ctree;
3963 + struct ip_set_iptreed *dtree;
3964 + unsigned char a,b,c,d;
3968 + ABCD(a, b, c, d, hash_ip);
3969 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
3970 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
3971 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
3972 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
3973 + if (dtree->expires[d]
3974 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
3976 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
3977 + DP("%u %lu", d, dtree->expires[d]);
3982 +addip(struct ip_set *set, const void *data, size_t size,
3983 + ip_set_ip_t *hash_ip)
3985 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3986 + struct ip_set_req_iptree *req =
3987 + (struct ip_set_req_iptree *) data;
3989 + if (size != sizeof(struct ip_set_req_iptree)) {
3990 + ip_set_printk("data length wrong (want %zu, have %zu)",
3991 + sizeof(struct ip_set_req_iptree),
3995 + DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
3996 + return __addip(set, req->ip,
3997 + req->timeout ? req->timeout : map->timeout,
4002 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4003 + u_int32_t flags, ip_set_ip_t *hash_ip)
4005 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4007 + return __addip(set,
4008 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4009 + : skb->nh.iph->daddr),
4014 +#define DELIP_WALK(map, elem, branch) do { \
4015 + if ((map)->tree[elem]) { \
4016 + branch = (map)->tree[elem]; \
4022 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4024 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4025 + struct ip_set_iptreeb *btree;
4026 + struct ip_set_iptreec *ctree;
4027 + struct ip_set_iptreed *dtree;
4028 + unsigned char a,b,c,d;
4031 + ABCD(a, b, c, d, hash_ip);
4032 + DELIP_WALK(map, a, btree);
4033 + DELIP_WALK(btree, b, ctree);
4034 + DELIP_WALK(ctree, c, dtree);
4036 + if (dtree->expires[d]) {
4037 + dtree->expires[d] = 0;
4044 +delip(struct ip_set *set, const void *data, size_t size,
4045 + ip_set_ip_t *hash_ip)
4047 + struct ip_set_req_iptree *req =
4048 + (struct ip_set_req_iptree *) data;
4050 + if (size != sizeof(struct ip_set_req_iptree)) {
4051 + ip_set_printk("data length wrong (want %zu, have %zu)",
4052 + sizeof(struct ip_set_req_iptree),
4056 + return __delip(set, req->ip, hash_ip);
4060 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4061 + u_int32_t flags, ip_set_ip_t *hash_ip)
4063 + return __delip(set,
4064 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4065 + : skb->nh.iph->daddr),
4069 +#define LOOP_WALK_BEGIN(map, i, branch) \
4070 + for (i = 0; i < 255; i++) { \
4071 + if (!(map)->tree[i]) \
4073 + branch = (map)->tree[i]
4075 +#define LOOP_WALK_END }
4077 +static void ip_tree_gc(unsigned long ul_set)
4079 + struct ip_set *set = (void *) ul_set;
4080 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4081 + struct ip_set_iptreeb *btree;
4082 + struct ip_set_iptreec *ctree;
4083 + struct ip_set_iptreed *dtree;
4084 + unsigned char a,b,c,d;
4085 + unsigned char i,j,k;
4088 + DP("gc: %s", set->name);
4089 + write_lock_bh(&set->lock);
4090 + LOOP_WALK_BEGIN(map, a, btree);
4091 + LOOP_WALK_BEGIN(btree, b, ctree);
4092 + LOOP_WALK_BEGIN(ctree, c, dtree);
4093 + for (d = 0; d < 255; d++) {
4094 + if (dtree->expires[d]) {
4095 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4097 + dtree->expires[d], jiffies);
4099 + && time_before(dtree->expires[d], jiffies))
4100 + dtree->expires[d] = 0;
4106 + DP("gc: %s: leaf %u %u %u empty",
4107 + set->name, a, b, c);
4108 + kmem_cache_free(leaf_cachep, dtree);
4109 + ctree->tree[c] = NULL;
4111 + DP("gc: %s: leaf %u %u %u not empty",
4112 + set->name, a, b, c);
4118 + DP("gc: %s: branch %u %u empty",
4120 + kmem_cache_free(branch_cachep, ctree);
4121 + btree->tree[b] = NULL;
4123 + DP("gc: %s: branch %u %u not empty",
4130 + DP("gc: %s: branch %u empty",
4132 + kmem_cache_free(branch_cachep, btree);
4133 + map->tree[a] = NULL;
4135 + DP("gc: %s: branch %u not empty",
4140 + write_unlock_bh(&set->lock);
4142 + map->gc.expires = jiffies + map->gc_interval * HZ;
4143 + add_timer(&map->gc);
4146 +static int create(struct ip_set *set, const void *data, size_t size)
4148 + struct ip_set_req_iptree_create *req =
4149 + (struct ip_set_req_iptree_create *) data;
4150 + struct ip_set_iptree *map;
4152 + if (size != sizeof(struct ip_set_req_iptree_create)) {
4153 + ip_set_printk("data length wrong (want %zu, have %zu)",
4154 + sizeof(struct ip_set_req_iptree_create),
4159 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4161 + DP("out of memory for %d bytes",
4162 + sizeof(struct ip_set_iptree));
4165 + memset(map, 0, sizeof(*map));
4166 + map->timeout = req->timeout;
4169 + /* If there is no timeout for the entries,
4170 + * we still have to call gc because delete
4171 + * do not clean up empty branches */
4172 + map->gc_interval = IPTREE_GC_TIME;
4173 + init_timer(&map->gc);
4174 + map->gc.data = (unsigned long) set;
4175 + map->gc.function = ip_tree_gc;
4176 + map->gc.expires = jiffies + map->gc_interval * HZ;
4177 + add_timer(&map->gc);
4182 +static void __flush(struct ip_set_iptree *map)
4184 + struct ip_set_iptreeb *btree;
4185 + struct ip_set_iptreec *ctree;
4186 + struct ip_set_iptreed *dtree;
4187 + unsigned int a,b,c;
4189 + LOOP_WALK_BEGIN(map, a, btree);
4190 + LOOP_WALK_BEGIN(btree, b, ctree);
4191 + LOOP_WALK_BEGIN(ctree, c, dtree);
4192 + kmem_cache_free(leaf_cachep, dtree);
4194 + kmem_cache_free(branch_cachep, ctree);
4196 + kmem_cache_free(branch_cachep, btree);
4200 +static void destroy(struct ip_set *set)
4202 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4204 + while (!del_timer(&map->gc))
4205 + msleep(IPTREE_DESTROY_SLEEP);
4211 +static void flush(struct ip_set *set)
4213 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4214 + unsigned int timeout = map->timeout;
4217 + memset(map, 0, sizeof(*map));
4218 + map->timeout = timeout;
4221 +static void list_header(const struct ip_set *set, void *data)
4223 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4224 + struct ip_set_req_iptree_create *header =
4225 + (struct ip_set_req_iptree_create *) data;
4227 + header->timeout = map->timeout;
4230 +static int list_members_size(const struct ip_set *set)
4232 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4233 + struct ip_set_iptreeb *btree;
4234 + struct ip_set_iptreec *ctree;
4235 + struct ip_set_iptreed *dtree;
4236 + unsigned char a,b,c,d;
4237 + unsigned int count = 0;
4239 + LOOP_WALK_BEGIN(map, a, btree);
4240 + LOOP_WALK_BEGIN(btree, b, ctree);
4241 + LOOP_WALK_BEGIN(ctree, c, dtree);
4242 + for (d = 0; d < 255; d++) {
4243 + if (dtree->expires[d]
4244 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4251 + DP("members %u", count);
4252 + return (count * sizeof(struct ip_set_req_iptree));
4255 +static void list_members(const struct ip_set *set, void *data)
4257 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4258 + struct ip_set_iptreeb *btree;
4259 + struct ip_set_iptreec *ctree;
4260 + struct ip_set_iptreed *dtree;
4261 + unsigned char a,b,c,d;
4262 + size_t offset = 0;
4263 + struct ip_set_req_iptree *entry;
4265 + LOOP_WALK_BEGIN(map, a, btree);
4266 + LOOP_WALK_BEGIN(btree, b, ctree);
4267 + LOOP_WALK_BEGIN(ctree, c, dtree);
4268 + for (d = 0; d < 255; d++) {
4269 + if (dtree->expires[d]
4270 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
4271 + entry = (struct ip_set_req_iptree *)(data + offset);
4272 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
4273 + entry->timeout = !map->timeout ? 0
4274 + : (dtree->expires[d] - jiffies)/HZ;
4275 + offset += sizeof(struct ip_set_req_iptree);
4283 +static struct ip_set_type ip_set_iptree = {
4284 + .typename = SETTYPE_NAME,
4285 + .typecode = IPSET_TYPE_IP,
4286 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4287 + .create = &create,
4288 + .destroy = &destroy,
4290 + .reqsize = sizeof(struct ip_set_req_iptree),
4292 + .addip_kernel = &addip_kernel,
4294 + .delip_kernel = &delip_kernel,
4295 + .testip = &testip,
4296 + .testip_kernel = &testip_kernel,
4297 + .header_size = sizeof(struct ip_set_req_iptree_create),
4298 + .list_header = &list_header,
4299 + .list_members_size = &list_members_size,
4300 + .list_members = &list_members,
4301 + .me = THIS_MODULE,
4304 +MODULE_LICENSE("GPL");
4305 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4306 +MODULE_DESCRIPTION("iptree type of IP sets");
4308 +static int __init init(void)
4312 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
4313 + sizeof(struct ip_set_iptreeb),
4314 + 0, 0, NULL, NULL);
4315 + if (!branch_cachep) {
4316 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
4320 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
4321 + sizeof(struct ip_set_iptreed),
4322 + 0, 0, NULL, NULL);
4323 + if (!leaf_cachep) {
4324 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
4328 + ret = ip_set_register_set_type(&ip_set_iptree);
4332 + kmem_cache_destroy(leaf_cachep);
4334 + kmem_cache_destroy(branch_cachep);
4339 +static void __exit fini(void)
4341 + /* FIXME: possible race with ip_set_create() */
4342 + ip_set_unregister_set_type(&ip_set_iptree);
4343 + kmem_cache_destroy(leaf_cachep);
4344 + kmem_cache_destroy(branch_cachep);
4349 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_macipmap.c
4350 ===================================================================
4351 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4352 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_macipmap.c 2007-12-15 05:20:07.896290225 +0100
4354 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
4355 + * Patrick Schaaf <bof@bof.de>
4356 + * Martin Josefsson <gandalf@wlug.westbo.se>
4357 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4359 + * This program is free software; you can redistribute it and/or modify
4360 + * it under the terms of the GNU General Public License version 2 as
4361 + * published by the Free Software Foundation.
4364 +/* Kernel module implementing an IP set type: the macipmap type */
4366 +#include <linux/module.h>
4367 +#include <linux/ip.h>
4368 +#include <linux/skbuff.h>
4369 +#include <linux/netfilter_ipv4/ip_tables.h>
4370 +#include <linux/netfilter_ipv4/ip_set.h>
4371 +#include <linux/errno.h>
4372 +#include <asm/uaccess.h>
4373 +#include <asm/bitops.h>
4374 +#include <asm/softirq.h>
4375 +#include <linux/spinlock.h>
4376 +#include <linux/if_ether.h>
4377 +#include <linux/vmalloc.h>
4379 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4380 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
4383 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
4385 + struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
4386 + struct ip_set_macip *table = (struct ip_set_macip *) map->members;
4387 + struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
4389 + if (size != sizeof(struct ip_set_req_macipmap)) {
4390 + ip_set_printk("data length wrong (want %zu, have %zu)",
4391 + sizeof(struct ip_set_req_macipmap),
4396 + if (req->ip < map->first_ip || req->ip > map->last_ip)
4399 + *hash_ip = req->ip;
4400 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4401 + set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
4402 + if (test_bit(IPSET_MACIP_ISSET,
4403 + &table[req->ip - map->first_ip].flags)) {
4404 + return (memcmp(req->ethernet,
4405 + &table[req->ip - map->first_ip].ethernet,
4408 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4413 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4414 + u_int32_t flags, ip_set_ip_t *hash_ip)
4416 + struct ip_set_macipmap *map =
4417 + (struct ip_set_macipmap *) set->data;
4418 + struct ip_set_macip *table =
4419 + (struct ip_set_macip *) map->members;
4422 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4423 + : skb->nh.iph->daddr);
4424 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4425 + flags & IPSET_SRC ? "SRC" : "DST",
4426 + NIPQUAD(skb->nh.iph->saddr),
4427 + NIPQUAD(skb->nh.iph->daddr));
4429 + if (ip < map->first_ip || ip > map->last_ip)
4433 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4434 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
4435 + if (test_bit(IPSET_MACIP_ISSET, &table[ip - map->first_ip].flags)) {
4436 + /* Is mac pointer valid?
4437 + * If so, compare... */
4438 + return (skb->mac.raw >= skb->head
4439 + && (skb->mac.raw + ETH_HLEN) <= skb->data
4440 + && (memcmp(skb->mac.ethernet->h_source,
4441 + &table[ip - map->first_ip].ethernet,
4444 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4448 +/* returns 0 on success */
4450 +__addip(struct ip_set *set,
4451 + ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
4453 + struct ip_set_macipmap *map =
4454 + (struct ip_set_macipmap *) set->data;
4455 + struct ip_set_macip *table =
4456 + (struct ip_set_macip *) map->members;
4458 + if (ip < map->first_ip || ip > map->last_ip)
4460 + if (test_and_set_bit(IPSET_MACIP_ISSET,
4461 + &table[ip - map->first_ip].flags))
4465 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4466 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
4471 +addip(struct ip_set *set, const void *data, size_t size,
4472 + ip_set_ip_t *hash_ip)
4474 + struct ip_set_req_macipmap *req =
4475 + (struct ip_set_req_macipmap *) data;
4477 + if (size != sizeof(struct ip_set_req_macipmap)) {
4478 + ip_set_printk("data length wrong (want %zu, have %zu)",
4479 + sizeof(struct ip_set_req_macipmap),
4483 + return __addip(set, req->ip, req->ethernet, hash_ip);
4487 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4488 + u_int32_t flags, ip_set_ip_t *hash_ip)
4492 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4493 + : skb->nh.iph->daddr);
4495 + if (!(skb->mac.raw >= skb->head
4496 + && (skb->mac.raw + ETH_HLEN) <= skb->data))
4499 + return __addip(set, ip, skb->mac.ethernet->h_source, hash_ip);
4503 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4505 + struct ip_set_macipmap *map =
4506 + (struct ip_set_macipmap *) set->data;
4507 + struct ip_set_macip *table =
4508 + (struct ip_set_macip *) map->members;
4510 + if (ip < map->first_ip || ip > map->last_ip)
4512 + if (!test_and_clear_bit(IPSET_MACIP_ISSET,
4513 + &table[ip - map->first_ip].flags))
4517 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4522 +delip(struct ip_set *set, const void *data, size_t size,
4523 + ip_set_ip_t *hash_ip)
4525 + struct ip_set_req_macipmap *req =
4526 + (struct ip_set_req_macipmap *) data;
4528 + if (size != sizeof(struct ip_set_req_macipmap)) {
4529 + ip_set_printk("data length wrong (want %zu, have %zu)",
4530 + sizeof(struct ip_set_req_macipmap),
4534 + return __delip(set, req->ip, hash_ip);
4538 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4539 + u_int32_t flags, ip_set_ip_t *hash_ip)
4541 + return __delip(set,
4542 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4543 + : skb->nh.iph->daddr),
4547 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
4549 + return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
4552 +static int create(struct ip_set *set, const void *data, size_t size)
4555 + struct ip_set_req_macipmap_create *req =
4556 + (struct ip_set_req_macipmap_create *) data;
4557 + struct ip_set_macipmap *map;
4559 + if (size != sizeof(struct ip_set_req_macipmap_create)) {
4560 + ip_set_printk("data length wrong (want %zu, have %zu)",
4561 + sizeof(struct ip_set_req_macipmap_create),
4566 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
4567 + HIPQUAD(req->from), HIPQUAD(req->to));
4569 + if (req->from > req->to) {
4570 + DP("bad ip range");
4574 + if (req->to - req->from > MAX_RANGE) {
4575 + ip_set_printk("range too big (max %d addresses)",
4580 + map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
4582 + DP("out of memory for %d bytes",
4583 + sizeof(struct ip_set_macipmap));
4586 + map->flags = req->flags;
4587 + map->first_ip = req->from;
4588 + map->last_ip = req->to;
4589 + newbytes = members_size(map->first_ip, map->last_ip);
4590 + map->members = ip_set_malloc(newbytes);
4591 + DP("members: %u %p", newbytes, map->members);
4592 + if (!map->members) {
4593 + DP("out of memory for %d bytes", newbytes);
4597 + memset(map->members, 0, newbytes);
4603 +static void destroy(struct ip_set *set)
4605 + struct ip_set_macipmap *map =
4606 + (struct ip_set_macipmap *) set->data;
4608 + ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
4614 +static void flush(struct ip_set *set)
4616 + struct ip_set_macipmap *map =
4617 + (struct ip_set_macipmap *) set->data;
4618 + memset(map->members, 0, members_size(map->first_ip, map->last_ip));
4621 +static void list_header(const struct ip_set *set, void *data)
4623 + struct ip_set_macipmap *map =
4624 + (struct ip_set_macipmap *) set->data;
4625 + struct ip_set_req_macipmap_create *header =
4626 + (struct ip_set_req_macipmap_create *) data;
4628 + DP("list_header %x %x %u", map->first_ip, map->last_ip,
4631 + header->from = map->first_ip;
4632 + header->to = map->last_ip;
4633 + header->flags = map->flags;
4636 +static int list_members_size(const struct ip_set *set)
4638 + struct ip_set_macipmap *map =
4639 + (struct ip_set_macipmap *) set->data;
4641 + DP("%u", members_size(map->first_ip, map->last_ip));
4642 + return members_size(map->first_ip, map->last_ip);
4645 +static void list_members(const struct ip_set *set, void *data)
4647 + struct ip_set_macipmap *map =
4648 + (struct ip_set_macipmap *) set->data;
4650 + int bytes = members_size(map->first_ip, map->last_ip);
4652 + DP("members: %u %p", bytes, map->members);
4653 + memcpy(data, map->members, bytes);
4656 +static struct ip_set_type ip_set_macipmap = {
4657 + .typename = SETTYPE_NAME,
4658 + .typecode = IPSET_TYPE_IP,
4659 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4660 + .create = &create,
4661 + .destroy = &destroy,
4663 + .reqsize = sizeof(struct ip_set_req_macipmap),
4665 + .addip_kernel = &addip_kernel,
4667 + .delip_kernel = &delip_kernel,
4668 + .testip = &testip,
4669 + .testip_kernel = &testip_kernel,
4670 + .header_size = sizeof(struct ip_set_req_macipmap_create),
4671 + .list_header = &list_header,
4672 + .list_members_size = &list_members_size,
4673 + .list_members = &list_members,
4674 + .me = THIS_MODULE,
4677 +MODULE_LICENSE("GPL");
4678 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4679 +MODULE_DESCRIPTION("macipmap type of IP sets");
4681 +static int __init init(void)
4683 + return ip_set_register_set_type(&ip_set_macipmap);
4686 +static void __exit fini(void)
4688 + /* FIXME: possible race with ip_set_create() */
4689 + ip_set_unregister_set_type(&ip_set_macipmap);
4694 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_nethash.c
4695 ===================================================================
4696 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4697 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_nethash.c 2007-12-15 05:20:07.900290454 +0100
4699 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4701 + * This program is free software; you can redistribute it and/or modify
4702 + * it under the terms of the GNU General Public License version 2 as
4703 + * published by the Free Software Foundation.
4706 +/* Kernel module implementing a cidr nethash set */
4708 +#include <linux/module.h>
4709 +#include <linux/ip.h>
4710 +#include <linux/skbuff.h>
4711 +#include <linux/netfilter_ipv4/ip_tables.h>
4712 +#include <linux/netfilter_ipv4/ip_set.h>
4713 +#include <linux/errno.h>
4714 +#include <asm/uaccess.h>
4715 +#include <asm/bitops.h>
4716 +#include <asm/softirq.h>
4717 +#include <linux/spinlock.h>
4718 +#include <linux/vmalloc.h>
4719 +#include <linux/random.h>
4721 +#include <net/ip.h>
4723 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4724 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
4725 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
4726 +#include <linux/netfilter_ipv4/ip_set_prime.h>
4728 +static inline __u32
4729 +jhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4731 + return jhash_1word(ip, map->initval);
4734 +static inline __u32
4735 +randhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4737 + return (1 + ip % map->prime);
4740 +static inline __u32
4741 +hash_id_cidr(struct ip_set_nethash *map,
4743 + unsigned char cidr,
4744 + ip_set_ip_t *hash_ip)
4746 + __u32 jhash, randhash, id;
4749 + *hash_ip = pack(ip, cidr);
4750 + jhash = jhash_ip(map, *hash_ip);
4751 + randhash = randhash_ip(map, *hash_ip);
4753 + for (i = 0; i < map->probes; i++) {
4754 + id = (jhash + i * randhash) % map->hashsize;
4755 + DP("hash key: %u", id);
4756 + if (map->members[id] == *hash_ip)
4762 +static inline __u32
4763 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4765 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4766 + __u32 id = UINT_MAX;
4769 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4770 + id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
4771 + if (id != UINT_MAX)
4778 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
4779 + ip_set_ip_t *hash_ip)
4781 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4783 + return (hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
4787 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4789 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
4793 +testip(struct ip_set *set, const void *data, size_t size,
4794 + ip_set_ip_t *hash_ip)
4796 + struct ip_set_req_nethash *req =
4797 + (struct ip_set_req_nethash *) data;
4799 + if (size != sizeof(struct ip_set_req_nethash)) {
4800 + ip_set_printk("data length wrong (want %zu, have %zu)",
4801 + sizeof(struct ip_set_req_nethash),
4805 + return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
4806 + : __testip_cidr(set, req->ip, req->cidr, hash_ip));
4810 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4811 + u_int32_t flags, ip_set_ip_t *hash_ip)
4813 + return __testip(set,
4814 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4815 + : skb->nh.iph->daddr),
4820 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
4822 + __u32 jhash, randhash, probe;
4825 + jhash = jhash_ip(map, ip);
4826 + randhash = randhash_ip(map, ip);
4828 + for (i = 0; i < map->probes; i++) {
4829 + probe = (jhash + i * randhash) % map->hashsize;
4830 + if (map->members[probe] == ip)
4832 + if (!map->members[probe]) {
4833 + map->members[probe] = ip;
4837 + /* Trigger rehashing */
4842 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4843 + ip_set_ip_t *hash_ip)
4845 + *hash_ip = pack(ip, cidr);
4846 + DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
4848 + return __addip_base(map, *hash_ip);
4852 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
4854 + unsigned char next;
4857 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4858 + if (map->cidr[i] == cidr) {
4860 + } else if (map->cidr[i] < cidr) {
4861 + next = map->cidr[i];
4862 + map->cidr[i] = cidr;
4867 + map->cidr[i] = cidr;
4871 +addip(struct ip_set *set, const void *data, size_t size,
4872 + ip_set_ip_t *hash_ip)
4874 + struct ip_set_req_nethash *req =
4875 + (struct ip_set_req_nethash *) data;
4878 + if (size != sizeof(struct ip_set_req_nethash)) {
4879 + ip_set_printk("data length wrong (want %zu, have %zu)",
4880 + sizeof(struct ip_set_req_nethash),
4884 + ret = __addip((struct ip_set_nethash *) set->data,
4885 + req->ip, req->cidr, hash_ip);
4888 + update_cidr_sizes((struct ip_set_nethash *) set->data,
4895 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4896 + u_int32_t flags, ip_set_ip_t *hash_ip)
4898 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4899 + int ret = -ERANGE;
4900 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4901 + : skb->nh.iph->daddr);
4904 + ret = __addip(map, ip, map->cidr[0], hash_ip);
4909 +static int retry(struct ip_set *set)
4911 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4912 + ip_set_ip_t *members;
4913 + u_int32_t i, hashsize;
4914 + unsigned newbytes;
4916 + struct ip_set_nethash tmp = {
4917 + .hashsize = map->hashsize,
4918 + .probes = map->probes,
4919 + .resize = map->resize
4922 + if (map->resize == 0)
4925 + memcpy(tmp.cidr, map->cidr, 30 * sizeof(unsigned char));
4929 + /* Calculate new parameters */
4930 + get_random_bytes(&tmp.initval, 4);
4931 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
4932 + if (hashsize == tmp.hashsize)
4934 + tmp.prime = make_prime(hashsize);
4936 + ip_set_printk("rehashing of set %s triggered: "
4937 + "hashsize grows from %u to %u",
4938 + set->name, tmp.hashsize, hashsize);
4939 + tmp.hashsize = hashsize;
4941 + newbytes = hashsize * sizeof(ip_set_ip_t);
4942 + tmp.members = ip_set_malloc(newbytes);
4943 + if (!tmp.members) {
4944 + DP("out of memory for %d bytes", newbytes);
4947 + memset(tmp.members, 0, newbytes);
4949 + write_lock_bh(&set->lock);
4950 + map = (struct ip_set_nethash *) set->data; /* Play safe */
4951 + for (i = 0; i < map->hashsize && res == 0; i++) {
4952 + if (map->members[i])
4953 + res = __addip_base(&tmp, map->members[i]);
4956 + /* Failure, try again */
4957 + write_unlock_bh(&set->lock);
4958 + ip_set_free(tmp.members, newbytes);
4962 + /* Success at resizing! */
4963 + members = map->members;
4964 + hashsize = map->hashsize;
4966 + map->initval = tmp.initval;
4967 + map->prime = tmp.prime;
4968 + map->hashsize = tmp.hashsize;
4969 + map->members = tmp.members;
4970 + write_unlock_bh(&set->lock);
4972 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
4978 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4979 + ip_set_ip_t *hash_ip)
4981 + ip_set_ip_t id = hash_id_cidr(map, ip, cidr, hash_ip);
4983 + if (id == UINT_MAX)
4986 + map->members[id] = 0;
4991 +delip(struct ip_set *set, const void *data, size_t size,
4992 + ip_set_ip_t *hash_ip)
4994 + struct ip_set_req_nethash *req =
4995 + (struct ip_set_req_nethash *) data;
4997 + if (size != sizeof(struct ip_set_req_nethash)) {
4998 + ip_set_printk("data length wrong (want %zu, have %zu)",
4999 + sizeof(struct ip_set_req_nethash),
5003 + /* TODO: no garbage collection in map->cidr */
5004 + return __delip((struct ip_set_nethash *) set->data,
5005 + req->ip, req->cidr, hash_ip);
5009 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
5010 + u_int32_t flags, ip_set_ip_t *hash_ip)
5012 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5013 + int ret = -ERANGE;
5014 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
5015 + : skb->nh.iph->daddr);
5018 + ret = __delip(map, ip, map->cidr[0], hash_ip);
5023 +static int create(struct ip_set *set, const void *data, size_t size)
5025 + unsigned newbytes;
5026 + struct ip_set_req_nethash_create *req =
5027 + (struct ip_set_req_nethash_create *) data;
5028 + struct ip_set_nethash *map;
5030 + if (size != sizeof(struct ip_set_req_nethash_create)) {
5031 + ip_set_printk("data length wrong (want %zu, have %zu)",
5032 + sizeof(struct ip_set_req_nethash_create),
5037 + if (req->hashsize < 1) {
5038 + ip_set_printk("hashsize too small");
5042 + map = kmalloc(sizeof(struct ip_set_nethash), GFP_KERNEL);
5044 + DP("out of memory for %d bytes",
5045 + sizeof(struct ip_set_nethash));
5048 + get_random_bytes(&map->initval, 4);
5049 + map->prime = make_prime(req->hashsize);
5050 + map->hashsize = req->hashsize;
5051 + map->probes = req->probes;
5052 + map->resize = req->resize;
5053 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
5054 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
5055 + map->members = ip_set_malloc(newbytes);
5056 + if (!map->members) {
5057 + DP("out of memory for %d bytes", newbytes);
5061 + memset(map->members, 0, newbytes);
5067 +static void destroy(struct ip_set *set)
5069 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5071 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
5077 +static void flush(struct ip_set *set)
5079 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5080 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
5081 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
5084 +static void list_header(const struct ip_set *set, void *data)
5086 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5087 + struct ip_set_req_nethash_create *header =
5088 + (struct ip_set_req_nethash_create *) data;
5090 + header->hashsize = map->hashsize;
5091 + header->probes = map->probes;
5092 + header->resize = map->resize;
5095 +static int list_members_size(const struct ip_set *set)
5097 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5099 + return (map->hashsize * sizeof(ip_set_ip_t));
5102 +static void list_members(const struct ip_set *set, void *data)
5104 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5105 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
5107 + memcpy(data, map->members, bytes);
5110 +static struct ip_set_type ip_set_nethash = {
5111 + .typename = SETTYPE_NAME,
5112 + .typecode = IPSET_TYPE_IP,
5113 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5114 + .create = &create,
5115 + .destroy = &destroy,
5117 + .reqsize = sizeof(struct ip_set_req_nethash),
5119 + .addip_kernel = &addip_kernel,
5122 + .delip_kernel = &delip_kernel,
5123 + .testip = &testip,
5124 + .testip_kernel = &testip_kernel,
5125 + .header_size = sizeof(struct ip_set_req_nethash_create),
5126 + .list_header = &list_header,
5127 + .list_members_size = &list_members_size,
5128 + .list_members = &list_members,
5129 + .me = THIS_MODULE,
5132 +MODULE_LICENSE("GPL");
5133 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5134 +MODULE_DESCRIPTION("nethash type of IP sets");
5136 +static int __init init(void)
5138 + return ip_set_register_set_type(&ip_set_nethash);
5141 +static void __exit fini(void)
5143 + /* FIXME: possible race with ip_set_create() */
5144 + ip_set_unregister_set_type(&ip_set_nethash);
5149 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_set_portmap.c
5150 ===================================================================
5151 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5152 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_set_portmap.c 2007-12-15 05:20:07.900290454 +0100
5154 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5156 + * This program is free software; you can redistribute it and/or modify
5157 + * it under the terms of the GNU General Public License version 2 as
5158 + * published by the Free Software Foundation.
5161 +/* Kernel module implementing a port set type as a bitmap */
5163 +#include <linux/module.h>
5164 +#include <linux/ip.h>
5165 +#include <linux/tcp.h>
5166 +#include <linux/udp.h>
5167 +#include <linux/skbuff.h>
5168 +#include <linux/netfilter_ipv4/ip_tables.h>
5169 +#include <linux/netfilter_ipv4/ip_set.h>
5170 +#include <linux/errno.h>
5171 +#include <asm/uaccess.h>
5172 +#include <asm/bitops.h>
5173 +#include <asm/softirq.h>
5174 +#include <linux/spinlock.h>
5176 +#include <net/ip.h>
5178 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
5180 +static inline ip_set_ip_t
5181 +get_port(const struct sk_buff *skb, u_int32_t flags)
5183 + struct iphdr *iph = skb->nh.iph;
5184 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
5186 + switch (iph->protocol) {
5187 + case IPPROTO_TCP: {
5188 + struct tcphdr *tcph = (struct tcphdr *)((u_int32_t *)iph + iph->ihl);
5190 + /* See comments at tcp_match in ip_tables.c */
5193 + && (skb->len - iph->ihl * 4) < sizeof(struct tcphdr)))
5194 + return INVALID_PORT;
5196 + return ntohs(flags & IPSET_SRC ?
5197 + tcph->source : tcph->dest);
5199 + case IPPROTO_UDP: {
5200 + struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
5204 + && (skb->len - iph->ihl * 4) < sizeof(struct udphdr)))
5205 + return INVALID_PORT;
5207 + return ntohs(flags & IPSET_SRC ?
5208 + udph->source : udph->dest);
5211 + return INVALID_PORT;
5216 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5218 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5220 + if (port < map->first_port || port > map->last_port)
5223 + *hash_port = port;
5224 + DP("set: %s, port:%u, %u", set->name, port, *hash_port);
5225 + return !!test_bit(port - map->first_port, map->members);
5229 +testport(struct ip_set *set, const void *data, size_t size,
5230 + ip_set_ip_t *hash_port)
5232 + struct ip_set_req_portmap *req =
5233 + (struct ip_set_req_portmap *) data;
5235 + if (size != sizeof(struct ip_set_req_portmap)) {
5236 + ip_set_printk("data length wrong (want %zu, have %zu)",
5237 + sizeof(struct ip_set_req_portmap),
5241 + return __testport(set, req->port, hash_port);
5245 +testport_kernel(struct ip_set *set, const struct sk_buff *skb,
5246 + u_int32_t flags, ip_set_ip_t *hash_port)
5249 + ip_set_ip_t port = get_port(skb, flags);
5251 + DP("flag %s port %u", flags & IPSET_SRC ? "SRC" : "DST", port);
5252 + if (port == INVALID_PORT)
5255 + res = __testport(set, port, hash_port);
5257 + return (res < 0 ? 0 : res);
5261 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5263 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5265 + if (port < map->first_port || port > map->last_port)
5267 + if (test_and_set_bit(port - map->first_port, map->members))
5270 + *hash_port = port;
5271 + DP("port %u", port);
5276 +addport(struct ip_set *set, const void *data, size_t size,
5277 + ip_set_ip_t *hash_port)
5279 + struct ip_set_req_portmap *req =
5280 + (struct ip_set_req_portmap *) data;
5282 + if (size != sizeof(struct ip_set_req_portmap)) {
5283 + ip_set_printk("data length wrong (want %zu, have %zu)",
5284 + sizeof(struct ip_set_req_portmap),
5288 + return __addport(set, req->port, hash_port);
5292 +addport_kernel(struct ip_set *set, const struct sk_buff *skb,
5293 + u_int32_t flags, ip_set_ip_t *hash_port)
5295 + ip_set_ip_t port = get_port(skb, flags);
5297 + if (port == INVALID_PORT)
5300 + return __addport(set, port, hash_port);
5304 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5306 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5308 + if (port < map->first_port || port > map->last_port)
5310 + if (!test_and_clear_bit(port - map->first_port, map->members))
5313 + *hash_port = port;
5314 + DP("port %u", port);
5319 +delport(struct ip_set *set, const void *data, size_t size,
5320 + ip_set_ip_t *hash_port)
5322 + struct ip_set_req_portmap *req =
5323 + (struct ip_set_req_portmap *) data;
5325 + if (size != sizeof(struct ip_set_req_portmap)) {
5326 + ip_set_printk("data length wrong (want %zu, have %zu)",
5327 + sizeof(struct ip_set_req_portmap),
5331 + return __delport(set, req->port, hash_port);
5335 +delport_kernel(struct ip_set *set, const struct sk_buff *skb,
5336 + u_int32_t flags, ip_set_ip_t *hash_port)
5338 + ip_set_ip_t port = get_port(skb, flags);
5340 + if (port == INVALID_PORT)
5343 + return __delport(set, port, hash_port);
5346 +static int create(struct ip_set *set, const void *data, size_t size)
5349 + struct ip_set_req_portmap_create *req =
5350 + (struct ip_set_req_portmap_create *) data;
5351 + struct ip_set_portmap *map;
5353 + if (size != sizeof(struct ip_set_req_portmap_create)) {
5354 + ip_set_printk("data length wrong (want %zu, have %zu)",
5355 + sizeof(struct ip_set_req_portmap_create),
5360 + DP("from %u to %u", req->from, req->to);
5362 + if (req->from > req->to) {
5363 + DP("bad port range");
5367 + if (req->to - req->from > MAX_RANGE) {
5368 + ip_set_printk("range too big (max %d ports)",
5373 + map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
5375 + DP("out of memory for %d bytes",
5376 + sizeof(struct ip_set_portmap));
5379 + map->first_port = req->from;
5380 + map->last_port = req->to;
5381 + newbytes = bitmap_bytes(req->from, req->to);
5382 + map->members = kmalloc(newbytes, GFP_KERNEL);
5383 + if (!map->members) {
5384 + DP("out of memory for %d bytes", newbytes);
5388 + memset(map->members, 0, newbytes);
5394 +static void destroy(struct ip_set *set)
5396 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5398 + kfree(map->members);
5404 +static void flush(struct ip_set *set)
5406 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5407 + memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
5410 +static void list_header(const struct ip_set *set, void *data)
5412 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5413 + struct ip_set_req_portmap_create *header =
5414 + (struct ip_set_req_portmap_create *) data;
5416 + DP("list_header %u %u", map->first_port, map->last_port);
5418 + header->from = map->first_port;
5419 + header->to = map->last_port;
5422 +static int list_members_size(const struct ip_set *set)
5424 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5426 + return bitmap_bytes(map->first_port, map->last_port);
5429 +static void list_members(const struct ip_set *set, void *data)
5431 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5432 + int bytes = bitmap_bytes(map->first_port, map->last_port);
5434 + memcpy(data, map->members, bytes);
5437 +static struct ip_set_type ip_set_portmap = {
5438 + .typename = SETTYPE_NAME,
5439 + .typecode = IPSET_TYPE_PORT,
5440 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5441 + .create = &create,
5442 + .destroy = &destroy,
5444 + .reqsize = sizeof(struct ip_set_req_portmap),
5445 + .addip = &addport,
5446 + .addip_kernel = &addport_kernel,
5447 + .delip = &delport,
5448 + .delip_kernel = &delport_kernel,
5449 + .testip = &testport,
5450 + .testip_kernel = &testport_kernel,
5451 + .header_size = sizeof(struct ip_set_req_portmap_create),
5452 + .list_header = &list_header,
5453 + .list_members_size = &list_members_size,
5454 + .list_members = &list_members,
5455 + .me = THIS_MODULE,
5458 +MODULE_LICENSE("GPL");
5459 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5460 +MODULE_DESCRIPTION("portmap type of IP sets");
5462 +static int __init init(void)
5464 + return ip_set_register_set_type(&ip_set_portmap);
5467 +static void __exit fini(void)
5469 + /* FIXME: possible race with ip_set_create() */
5470 + ip_set_unregister_set_type(&ip_set_portmap);
5475 Index: linux-2.4.35.4/net/ipv4/netfilter/ipt_set.c
5476 ===================================================================
5477 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5478 +++ linux-2.4.35.4/net/ipv4/netfilter/ipt_set.c 2007-12-15 05:20:07.900290454 +0100
5480 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5481 + * Patrick Schaaf <bof@bof.de>
5482 + * Martin Josefsson <gandalf@wlug.westbo.se>
5483 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5485 + * This program is free software; you can redistribute it and/or modify
5486 + * it under the terms of the GNU General Public License version 2 as
5487 + * published by the Free Software Foundation.
5490 +/* Kernel module to match an IP set. */
5492 +#include <linux/module.h>
5493 +#include <linux/ip.h>
5494 +#include <linux/skbuff.h>
5496 +#include <linux/netfilter_ipv4/ip_tables.h>
5497 +#include <linux/netfilter_ipv4/ip_set.h>
5498 +#include <linux/netfilter_ipv4/ipt_set.h>
5501 +match_set(const struct ipt_set_info *info,
5502 + const struct sk_buff *skb,
5505 + if (ip_set_testip_kernel(info->index, skb, info->flags))
5511 +match(const struct sk_buff *skb,
5512 + const struct net_device *in,
5513 + const struct net_device *out,
5514 + const void *matchinfo,
5517 + u_int16_t datalen,
5520 + const struct ipt_set_info_match *info = matchinfo;
5522 + return match_set(&info->match_set,
5524 + info->match_set.flags[0] & IPSET_MATCH_INV);
5528 +checkentry(const char *tablename,
5529 + const struct ipt_ip *ip,
5531 + unsigned int matchsize,
5532 + unsigned int hook_mask)
5534 + struct ipt_set_info_match *info =
5535 + (struct ipt_set_info_match *) matchinfo;
5536 + ip_set_id_t index;
5538 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5539 + ip_set_printk("invalid matchsize %d", matchsize);
5543 + index = ip_set_get_byindex(info->match_set.index);
5545 + if (index == IP_SET_INVALID_ID) {
5546 + ip_set_printk("Cannot find set indentified by id %u to match",
5547 + info->match_set.index);
5548 + return 0; /* error */
5550 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5551 + ip_set_printk("That's nasty!");
5552 + return 0; /* error */
5558 +static void destroy(void *matchinfo, unsigned int matchsize)
5560 + struct ipt_set_info_match *info = matchinfo;
5562 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5563 + ip_set_printk("invalid matchsize %d", matchsize);
5567 + ip_set_put(info->match_set.index);
5570 +static struct ipt_match set_match = {
5573 + .checkentry = &checkentry,
5574 + .destroy = &destroy,
5578 +MODULE_LICENSE("GPL");
5579 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5580 +MODULE_DESCRIPTION("iptables IP set match module");
5582 +static int __init init(void)
5584 + return ipt_register_match(&set_match);
5587 +static void __exit fini(void)
5589 + ipt_unregister_match(&set_match);
5594 Index: linux-2.4.35.4/net/ipv4/netfilter/ipt_SET.c
5595 ===================================================================
5596 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5597 +++ linux-2.4.35.4/net/ipv4/netfilter/ipt_SET.c 2007-12-15 05:20:07.900290454 +0100
5599 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5600 + * Patrick Schaaf <bof@bof.de>
5601 + * Martin Josefsson <gandalf@wlug.westbo.se>
5602 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5604 + * This program is free software; you can redistribute it and/or modify
5605 + * it under the terms of the GNU General Public License version 2 as
5606 + * published by the Free Software Foundation.
5609 +/* ipt_SET.c - netfilter target to manipulate IP sets */
5611 +#include <linux/types.h>
5612 +#include <linux/ip.h>
5613 +#include <linux/timer.h>
5614 +#include <linux/module.h>
5615 +#include <linux/netfilter.h>
5616 +#include <linux/netdevice.h>
5617 +#include <linux/if.h>
5618 +#include <linux/inetdevice.h>
5619 +#include <net/protocol.h>
5620 +#include <net/checksum.h>
5621 +#include <linux/netfilter_ipv4.h>
5622 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
5623 +#include <linux/netfilter_ipv4/ipt_set.h>
5625 +static unsigned int
5626 +target(struct sk_buff **pskb,
5627 + unsigned int hooknum,
5628 + const struct net_device *in,
5629 + const struct net_device *out,
5630 + const void *targinfo,
5633 + const struct ipt_set_info_target *info = targinfo;
5635 + if (info->add_set.index != IP_SET_INVALID_ID)
5636 + ip_set_addip_kernel(info->add_set.index,
5638 + info->add_set.flags);
5639 + if (info->del_set.index != IP_SET_INVALID_ID)
5640 + ip_set_delip_kernel(info->del_set.index,
5642 + info->del_set.flags);
5644 + return IPT_CONTINUE;
5648 +checkentry(const char *tablename,
5649 + const struct ipt_entry *e,
5651 + unsigned int targinfosize, unsigned int hook_mask)
5653 + struct ipt_set_info_target *info =
5654 + (struct ipt_set_info_target *) targinfo;
5655 + ip_set_id_t index;
5657 + if (targinfosize != IPT_ALIGN(sizeof(*info))) {
5658 + DP("bad target info size %u", targinfosize);
5662 + if (info->add_set.index != IP_SET_INVALID_ID) {
5663 + index = ip_set_get_byindex(info->add_set.index);
5664 + if (index == IP_SET_INVALID_ID) {
5665 + ip_set_printk("cannot find add_set index %u as target",
5666 + info->add_set.index);
5667 + return 0; /* error */
5671 + if (info->del_set.index != IP_SET_INVALID_ID) {
5672 + index = ip_set_get_byindex(info->del_set.index);
5673 + if (index == IP_SET_INVALID_ID) {
5674 + ip_set_printk("cannot find del_set index %u as target",
5675 + info->del_set.index);
5676 + return 0; /* error */
5679 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
5680 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5681 + ip_set_printk("That's nasty!");
5682 + return 0; /* error */
5687 +static void destroy(void *targetinfo, unsigned int targetsize)
5689 + struct ipt_set_info_target *info = targetinfo;
5691 + if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
5692 + ip_set_printk("invalid targetsize %d", targetsize);
5696 + if (info->add_set.index != IP_SET_INVALID_ID)
5697 + ip_set_put(info->add_set.index);
5698 + if (info->del_set.index != IP_SET_INVALID_ID)
5699 + ip_set_put(info->del_set.index);
5702 +static struct ipt_target SET_target = {
5705 + .checkentry = checkentry,
5706 + .destroy = destroy,
5710 +MODULE_LICENSE("GPL");
5711 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5712 +MODULE_DESCRIPTION("iptables IP set target module");
5714 +static int __init init(void)
5716 + return ipt_register_target(&SET_target);
5719 +static void __exit fini(void)
5721 + ipt_unregister_target(&SET_target);
5726 Index: linux-2.4.35.4/net/ipv4/netfilter/Makefile
5727 ===================================================================
5728 --- linux-2.4.35.4.orig/net/ipv4/netfilter/Makefile 2007-12-15 05:20:07.568271536 +0100
5729 +++ linux-2.4.35.4/net/ipv4/netfilter/Makefile 2007-12-15 05:20:07.900290454 +0100
5731 obj-$(CONFIG_IP_NF_MATCH_HELPER) += ipt_helper.o
5732 obj-$(CONFIG_IP_NF_MATCH_LIMIT) += ipt_limit.o
5733 obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
5734 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
5735 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
5736 +ifdef CONFIG_IP_NF_SET
5737 + obj-$(CONFIG_IP_NF_SET) += ip_set.o
5738 + export-objs += ip_set.o
5740 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
5741 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
5742 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
5743 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
5744 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
5745 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
5746 obj-$(CONFIG_IP_NF_MATCH_MAC) += ipt_mac.o
5747 obj-$(CONFIG_IP_NF_MATCH_IPP2P) += ipt_ipp2p.o