1 diff -Naur linux-old/Documentation/Configure.help linux-new/Documentation/Configure.help
2 --- linux-old/Documentation/Configure.help 2005-07-23 23:34:46.000000000 +0200
3 +++ linux-new/Documentation/Configure.help 2005-08-10 22:13:01.000000000 +0200
5 If you want to compile it as a module, say M here and read
6 <file:Documentation/modules.txt>. If unsure, say `N'.
10 + This option adds IP set support to the kernel.
12 + In order to define and use sets, you need userlevel utilities: an
13 + iptables binary which knows about IP sets and the program ipset(8),
14 + by which you can define and setup the sets themselves.
16 + If you want to compile it as a module, say M here and read
17 + <file:Documentation/modules.txt>. If unsure, say `N'.
20 +CONFIG_IP_NF_MATCH_SET
21 + This option adds IP set match support.
22 + You need the ipset utility to create and set up the sets.
24 + If you want to compile it as a module, say M here and read
25 + <file:Documentation/modules.txt>. If unsure, say `N'.
28 +CONFIG_IP_NF_TARGET_SET
29 + This option adds IP set target support.
30 + You need the ipset utility to create and set up the sets.
32 + If you want to compile it as a module, say M here and read
33 + <file:Documentation/modules.txt>. If unsure, say `N'.
35 +ipmap set type support
36 +CONFIG_IP_NF_SET_IPMAP
37 + This option adds the ipmap set type support.
39 + If you want to compile it as a module, say M here and read
40 + <file:Documentation/modules.txt>. If unsure, say `N'.
42 +macipmap set type support
43 +CONFIG_IP_NF_SET_MACIPMAP
44 + This option adds the macipmap set type support.
46 + If you want to compile it as a module, say M here and read
47 + <file:Documentation/modules.txt>. If unsure, say `N'.
49 +portmap set type support
50 +CONFIG_IP_NF_SET_PORTMAP
51 + This option adds the portmap set type support.
53 + If you want to compile it as a module, say M here and read
54 + <file:Documentation/modules.txt>. If unsure, say `N'.
56 +iphash set type support
57 +CONFIG_IP_NF_SET_IPHASH
58 + This option adds the iphash set type support.
60 + If you want to compile it as a module, say M here and read
61 + <file:Documentation/modules.txt>. If unsure, say `N'.
63 +nethash set type support
64 +CONFIG_IP_NF_SET_NETHASH
65 + This option adds the nethash set type support.
67 + If you want to compile it as a module, say M here and read
68 + <file:Documentation/modules.txt>. If unsure, say `N'.
70 +iptree set type support
71 +CONFIG_IP_NF_SET_IPTREE
72 + This option adds the iptree set type support.
74 + If you want to compile it as a module, say M here and read
75 + <file:Documentation/modules.txt>. If unsure, say `N'.
78 CONFIG_IP_NF_TARGET_TTL
79 This option adds a `TTL' target, which enables the user to set
80 diff -Naur linux-old/include/linux/netfilter_ipv4/ip_set.h linux-new/include/linux/netfilter_ipv4/ip_set.h
81 --- linux-old/include/linux/netfilter_ipv4/ip_set.h 1970-01-01 01:00:00.000000000 +0100
82 +++ linux-new/include/linux/netfilter_ipv4/ip_set.h 2005-08-10 22:13:00.000000000 +0200
87 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
88 + * Patrick Schaaf <bof@bof.de>
89 + * Martin Josefsson <gandalf@wlug.westbo.se>
90 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
92 + * This program is free software; you can redistribute it and/or modify
93 + * it under the terms of the GNU General Public License version 2 as
94 + * published by the Free Software Foundation.
98 + * A sockopt of such quality has hardly ever been seen before on the open
99 + * market! This little beauty, hardly ever used: above 64, so it's
100 + * traditionally used for firewalling, not touched (even once!) by the
101 + * 2.0, 2.2 and 2.4 kernels!
103 + * Comes with its own certificate of authenticity, valid anywhere in the
108 +#define SO_IP_SET 83
111 + * Heavily modify by Joakim Axelsson 08.03.2002
112 + * - Made it more modulebased
114 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
116 + * - in order to "deal with" backward compatibility, renamed to ipset
120 + * Used so that the kernel module and ipset-binary can match their versions
122 +#define IP_SET_PROTOCOL_VERSION 2
124 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
126 +/* Lets work with our own typedef for representing an IP address.
127 + * We hope to make the code more portable, possibly to IPv6...
129 + * The representation works in HOST byte order, because most set types
130 + * will perform arithmetic operations and compare operations.
132 + * For now the type is an uint32_t.
134 + * Make sure to ONLY use the functions when translating and parsing
135 + * in order to keep the host byte order and make it more portable:
138 + * parse_ipandmask()
140 + * (Joakim: where are they???)
143 +typedef uint32_t ip_set_ip_t;
145 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
146 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
148 +typedef uint16_t ip_set_id_t;
150 +#define IP_SET_INVALID_ID 65535
152 +/* How deep we follow bindings */
153 +#define IP_SET_MAX_BINDINGS 6
156 + * Option flags for kernel operations (ipt_set_info)
158 +#define IPSET_SRC 0x01 /* Source match/add */
159 +#define IPSET_DST 0x02 /* Destination match/add */
160 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
163 + * Set types (flavours)
165 +#define IPSET_TYPE_IP 0 /* IP address type of set */
166 +#define IPSET_TYPE_PORT 1 /* Port type of set */
168 +/* Reserved keywords */
169 +#define IPSET_TOKEN_DEFAULT ":default:"
170 +#define IPSET_TOKEN_ALL ":all:"
172 +/* SO_IP_SET operation constants, and their request struct types.
175 + * 0-99: commands with version checking
176 + * 100-199: add/del/test/bind/unbind
177 + * 200-299: list, save, restore
180 +/* Single shot operations:
181 + * version, create, destroy, flush, rename and swap
183 + * Sets are identified by name.
186 +#define IP_SET_REQ_STD \
188 + unsigned version; \
189 + char name[IP_SET_MAXNAMELEN]
191 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
192 +struct ip_set_req_create {
194 + char typename[IP_SET_MAXNAMELEN];
197 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
198 +struct ip_set_req_std {
202 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
203 +/* Uses ip_set_req_std */
205 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
206 +/* Uses ip_set_req_create */
208 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
209 +/* Uses ip_set_req_create */
211 +union ip_set_name_index {
212 + char name[IP_SET_MAXNAMELEN];
216 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
217 +struct ip_set_req_get_set {
220 + union ip_set_name_index set;
223 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
224 +/* Uses ip_set_req_get_set */
226 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
227 +struct ip_set_req_version {
232 +/* Double shots operations:
233 + * add, del, test, bind and unbind.
235 + * First we query the kernel to get the index and type of the target set,
236 + * then issue the command. Validity of IP is checked in kernel in order
237 + * to minimalize sockopt operations.
240 +/* Get minimal set data for add/del/test/bind/unbind IP */
241 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
242 +struct ip_set_req_adt_get {
245 + union ip_set_name_index set;
246 + char typename[IP_SET_MAXNAMELEN];
249 +#define IP_SET_REQ_BYINDEX \
253 +struct ip_set_req_adt {
254 + IP_SET_REQ_BYINDEX;
257 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
258 +/* Uses ip_set_req_adt, with type specific addage */
260 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
261 +/* Uses ip_set_req_adt, with type specific addage */
263 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
264 +/* Uses ip_set_req_adt, with type specific addage */
266 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
267 +/* Uses ip_set_req_bind, with type specific addage */
268 +struct ip_set_req_bind {
269 + IP_SET_REQ_BYINDEX;
270 + char binding[IP_SET_MAXNAMELEN];
273 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
274 +/* Uses ip_set_req_bind, with type speficic addage
275 + * index = 0 means unbinding for all sets */
277 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
278 +/* Uses ip_set_req_bind, with type specific addage */
280 +/* Multiple shots operations: list, save, restore.
282 + * - check kernel version and query the max number of sets
283 + * - get the basic information on all sets
284 + * and size required for the next step
285 + * - get actual set data: header, data, bindings
288 +/* Get max_sets and the index of a queried set
290 +#define IP_SET_OP_MAX_SETS 0x00000020
291 +struct ip_set_req_max_sets {
294 + ip_set_id_t max_sets; /* max_sets */
295 + ip_set_id_t sets; /* real number of sets */
296 + union ip_set_name_index set; /* index of set if name used */
299 +/* Get the id and name of the sets plus size for next step */
300 +#define IP_SET_OP_LIST_SIZE 0x00000201
301 +#define IP_SET_OP_SAVE_SIZE 0x00000202
302 +struct ip_set_req_setnames {
304 + ip_set_id_t index; /* set to list/save */
305 + size_t size; /* size to get setdata/bindings */
306 + /* followed by sets number of struct ip_set_name_list */
309 +struct ip_set_name_list {
310 + char name[IP_SET_MAXNAMELEN];
311 + char typename[IP_SET_MAXNAMELEN];
316 +/* The actual list operation */
317 +#define IP_SET_OP_LIST 0x00000203
318 +struct ip_set_req_list {
319 + IP_SET_REQ_BYINDEX;
320 + /* sets number of struct ip_set_list in reply */
323 +struct ip_set_list {
325 + ip_set_id_t binding;
327 + size_t header_size; /* Set header data of header_size */
328 + size_t members_size; /* Set members data of members_size */
329 + size_t bindings_size; /* Set bindings data of bindings_size */
332 +struct ip_set_hash_list {
334 + ip_set_id_t binding;
337 +/* The save operation */
338 +#define IP_SET_OP_SAVE 0x00000204
339 +/* Uses ip_set_req_list, in the reply replaced by
340 + * sets number of struct ip_set_save plus a marker
341 + * ip_set_save followed by ip_set_hash_save structures.
343 +struct ip_set_save {
345 + ip_set_id_t binding;
346 + size_t header_size; /* Set header data of header_size */
347 + size_t members_size; /* Set members data of members_size */
350 +/* At restoring, ip == 0 means default binding for the given set: */
351 +struct ip_set_hash_save {
354 + ip_set_id_t binding;
357 +/* The restore operation */
358 +#define IP_SET_OP_RESTORE 0x00000205
359 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
360 + * plus a marker ip_set_restore, followed by ip_set_hash_save
363 +struct ip_set_restore {
364 + char name[IP_SET_MAXNAMELEN];
365 + char typename[IP_SET_MAXNAMELEN];
367 + size_t header_size; /* Create data of header_size */
368 + size_t members_size; /* Set members data of members_size */
371 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
373 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
378 +#define ip_set_printk(format, args...) \
380 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
381 + printk(format "\n" , ## args); \
384 +#if defined(IP_SET_DEBUG)
385 +#define DP(format, args...) \
387 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
388 + printk(format "\n" , ## args); \
390 +#define IP_SET_ASSERT(x) \
393 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
394 + __FILE__, __LINE__, __FUNCTION__); \
397 +#define DP(format, args...)
398 +#define IP_SET_ASSERT(x)
404 + * The ip_set_type definition - one per set type, e.g. "ipmap".
406 + * Each individual set has a pointer, set->type, going to one
407 + * of these structures. Function pointers inside the structure implement
408 + * the real behaviour of the sets.
410 + * If not mentioned differently, the implementation behind the function
411 + * pointers of a set_type, is expected to return 0 if ok, and a negative
412 + * errno (e.g. -EINVAL) on error.
414 +struct ip_set_type {
415 + struct list_head list; /* next in list of set types */
417 + /* test for IP in set (kernel: iptables -m set src|dst)
418 + * return 0 if not in set, 1 if in set.
420 + int (*testip_kernel) (struct ip_set *set,
421 + const struct sk_buff * skb,
425 + /* test for IP in set (userspace: ipset -T set IP)
426 + * return 0 if not in set, 1 if in set.
428 + int (*testip) (struct ip_set *set,
429 + const void *data, size_t size,
433 + * Size of the data structure passed by when
434 + * adding/deletin/testing an entry.
438 + /* Add IP into set (userspace: ipset -A set IP)
439 + * Return -EEXIST if the address is already in the set,
440 + * and -ERANGE if the address lies outside the set bounds.
441 + * If the address was not already in the set, 0 is returned.
443 + int (*addip) (struct ip_set *set,
444 + const void *data, size_t size,
447 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
448 + * Return -EEXIST if the address is already in the set,
449 + * and -ERANGE if the address lies outside the set bounds.
450 + * If the address was not already in the set, 0 is returned.
452 + int (*addip_kernel) (struct ip_set *set,
453 + const struct sk_buff * skb,
457 + /* remove IP from set (userspace: ipset -D set --entry x)
458 + * Return -EEXIST if the address is NOT in the set,
459 + * and -ERANGE if the address lies outside the set bounds.
460 + * If the address really was in the set, 0 is returned.
462 + int (*delip) (struct ip_set *set,
463 + const void *data, size_t size,
466 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
467 + * Return -EEXIST if the address is NOT in the set,
468 + * and -ERANGE if the address lies outside the set bounds.
469 + * If the address really was in the set, 0 is returned.
471 + int (*delip_kernel) (struct ip_set *set,
472 + const struct sk_buff * skb,
476 + /* new set creation - allocated type specific items
478 + int (*create) (struct ip_set *set,
479 + const void *data, size_t size);
481 + /* retry the operation after successfully tweaking the set
483 + int (*retry) (struct ip_set *set);
485 + /* set destruction - free type specific items
486 + * There is no return value.
487 + * Can be called only when child sets are destroyed.
489 + void (*destroy) (struct ip_set *set);
491 + /* set flushing - reset all bits in the set, or something similar.
492 + * There is no return value.
494 + void (*flush) (struct ip_set *set);
496 + /* Listing: size needed for header
498 + size_t header_size;
500 + /* Listing: Get the header
502 + * Fill in the information in "data".
503 + * This function is always run after list_header_size() under a
504 + * writelock on the set. Therefor is the length of "data" always
507 + void (*list_header) (const struct ip_set *set,
510 + /* Listing: Get the size for the set members
512 + int (*list_members_size) (const struct ip_set *set);
514 + /* Listing: Get the set members
516 + * Fill in the information in "data".
517 + * This function is always run after list_member_size() under a
518 + * writelock on the set. Therefor is the length of "data" always
521 + void (*list_members) (const struct ip_set *set,
524 + char typename[IP_SET_MAXNAMELEN];
526 + int protocol_version;
528 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
532 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
533 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
535 +/* A generic ipset */
537 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
538 + rwlock_t lock; /* lock for concurrency control */
539 + ip_set_id_t id; /* set id for swapping */
540 + ip_set_id_t binding; /* default binding for the set */
541 + atomic_t ref; /* in kernel and in hash references */
542 + struct ip_set_type *type; /* the set types */
543 + void *data; /* pooltype specific data */
546 +/* Structure to bind set elements to sets */
547 +struct ip_set_hash {
548 + struct list_head list; /* list of clashing entries in hash */
549 + ip_set_ip_t ip; /* ip from set */
550 + ip_set_id_t id; /* set id */
551 + ip_set_id_t binding; /* set we bind the element to */
554 +/* register and unregister set references */
555 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
556 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
557 +extern void ip_set_put(ip_set_id_t id);
559 +/* API for iptables set match, and SET target */
560 +extern void ip_set_addip_kernel(ip_set_id_t id,
561 + const struct sk_buff *skb,
562 + const u_int32_t *flags);
563 +extern void ip_set_delip_kernel(ip_set_id_t id,
564 + const struct sk_buff *skb,
565 + const u_int32_t *flags);
566 +extern int ip_set_testip_kernel(ip_set_id_t id,
567 + const struct sk_buff *skb,
568 + const u_int32_t *flags);
570 +#endif /* __KERNEL__ */
572 +#endif /*_IP_SET_H*/
573 diff -Naur linux-old/include/linux/netfilter_ipv4/ip_set_iphash.h linux-new/include/linux/netfilter_ipv4/ip_set_iphash.h
574 --- linux-old/include/linux/netfilter_ipv4/ip_set_iphash.h 1970-01-01 01:00:00.000000000 +0100
575 +++ linux-new/include/linux/netfilter_ipv4/ip_set_iphash.h 2005-08-10 22:13:00.000000000 +0200
577 +#ifndef __IP_SET_IPHASH_H
578 +#define __IP_SET_IPHASH_H
580 +#include <linux/netfilter_ipv4/ip_set.h>
582 +#define SETTYPE_NAME "iphash"
583 +#define MAX_RANGE 0x0000FFFF
585 +struct ip_set_iphash {
586 + ip_set_ip_t *members; /* the iphash proper */
587 + uint32_t initval; /* initval for jhash_1word */
588 + uint32_t prime; /* prime for double hashing */
589 + uint32_t hashsize; /* hash size */
590 + uint16_t probes; /* max number of probes */
591 + uint16_t resize; /* resize factor in percent */
592 + ip_set_ip_t netmask; /* netmask */
595 +struct ip_set_req_iphash_create {
599 + ip_set_ip_t netmask;
602 +struct ip_set_req_iphash {
606 +#endif /* __IP_SET_IPHASH_H */
607 diff -Naur linux-old/include/linux/netfilter_ipv4/ip_set_ipmap.h linux-new/include/linux/netfilter_ipv4/ip_set_ipmap.h
608 --- linux-old/include/linux/netfilter_ipv4/ip_set_ipmap.h 1970-01-01 01:00:00.000000000 +0100
609 +++ linux-new/include/linux/netfilter_ipv4/ip_set_ipmap.h 2005-08-10 22:13:00.000000000 +0200
611 +#ifndef __IP_SET_IPMAP_H
612 +#define __IP_SET_IPMAP_H
614 +#include <linux/netfilter_ipv4/ip_set.h>
616 +#define SETTYPE_NAME "ipmap"
617 +#define MAX_RANGE 0x0000FFFF
619 +struct ip_set_ipmap {
620 + void *members; /* the ipmap proper */
621 + ip_set_ip_t first_ip; /* host byte order, included in range */
622 + ip_set_ip_t last_ip; /* host byte order, included in range */
623 + ip_set_ip_t netmask; /* subnet netmask */
624 + ip_set_ip_t sizeid; /* size of set in IPs */
625 + u_int16_t hosts; /* number of hosts in a subnet */
628 +struct ip_set_req_ipmap_create {
631 + ip_set_ip_t netmask;
634 +struct ip_set_req_ipmap {
639 +mask_to_bits(ip_set_ip_t mask)
641 + unsigned int bits = 32;
642 + ip_set_ip_t maskaddr;
644 + if (mask == 0xFFFFFFFF)
647 + maskaddr = 0xFFFFFFFE;
648 + while (--bits >= 0 && maskaddr != mask)
655 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
657 + ip_set_ip_t mask = 0xFFFFFFFE;
660 + while (--(*bits) >= 0 && mask && (to & mask) != from)
666 +#endif /* __IP_SET_IPMAP_H */
667 diff -Naur linux-old/include/linux/netfilter_ipv4/ip_set_iptree.h linux-new/include/linux/netfilter_ipv4/ip_set_iptree.h
668 --- linux-old/include/linux/netfilter_ipv4/ip_set_iptree.h 1970-01-01 01:00:00.000000000 +0100
669 +++ linux-new/include/linux/netfilter_ipv4/ip_set_iptree.h 2005-08-10 22:13:00.000000000 +0200
671 +#ifndef __IP_SET_IPTREE_H
672 +#define __IP_SET_IPTREE_H
674 +#include <linux/netfilter_ipv4/ip_set.h>
676 +#define SETTYPE_NAME "iptree"
677 +#define MAX_RANGE 0x0000FFFF
679 +struct ip_set_iptreed {
680 + unsigned long expires[255]; /* x.x.x.ADDR */
683 +struct ip_set_iptreec {
684 + struct ip_set_iptreed *tree[255]; /* x.x.ADDR.* */
687 +struct ip_set_iptreeb {
688 + struct ip_set_iptreec *tree[255]; /* x.ADDR.*.* */
691 +struct ip_set_iptree {
692 + unsigned int timeout;
693 + unsigned int gc_interval;
695 + struct timer_list gc;
696 + struct ip_set_iptreeb *tree[255]; /* ADDR.*.*.* */
700 +struct ip_set_req_iptree_create {
701 + unsigned int timeout;
704 +struct ip_set_req_iptree {
706 + unsigned int timeout;
709 +#endif /* __IP_SET_IPTREE_H */
710 diff -Naur linux-old/include/linux/netfilter_ipv4/ip_set_jhash.h linux-new/include/linux/netfilter_ipv4/ip_set_jhash.h
711 --- linux-old/include/linux/netfilter_ipv4/ip_set_jhash.h 1970-01-01 01:00:00.000000000 +0100
712 +++ linux-new/include/linux/netfilter_ipv4/ip_set_jhash.h 2005-08-10 22:13:00.000000000 +0200
714 +#ifndef _LINUX_IPSET_JHASH_H
715 +#define _LINUX_IPSET_JHASH_H
717 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
718 + * to __u32/__u8 so that the header file can be included into
719 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
722 +/* jhash.h: Jenkins hash support.
724 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
726 + * http://burtleburtle.net/bob/hash/
728 + * These are the credits from Bob's sources:
730 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
731 + * hash(), hash2(), hash3, and mix() are externally useful functions.
732 + * Routines to test the hash are included if SELF_TEST is defined.
733 + * You can use this free for any purpose. It has no warranty.
735 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
737 + * I've modified Bob's hash to be useful in the Linux kernel, and
738 + * any bugs present are surely my fault. -DaveM
741 +/* NOTE: Arguments are modified. */
742 +#define __jhash_mix(a, b, c) \
744 + a -= b; a -= c; a ^= (c>>13); \
745 + b -= c; b -= a; b ^= (a<<8); \
746 + c -= a; c -= b; c ^= (b>>13); \
747 + a -= b; a -= c; a ^= (c>>12); \
748 + b -= c; b -= a; b ^= (a<<16); \
749 + c -= a; c -= b; c ^= (b>>5); \
750 + a -= b; a -= c; a ^= (c>>3); \
751 + b -= c; b -= a; b ^= (a<<10); \
752 + c -= a; c -= b; c ^= (b>>15); \
755 +/* The golden ration: an arbitrary value */
756 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
758 +/* The most generic version, hashes an arbitrary sequence
759 + * of bytes. No alignment or length assumptions are made about
762 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
764 + __u32 a, b, c, len;
768 + a = b = JHASH_GOLDEN_RATIO;
771 + while (len >= 12) {
772 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
773 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
774 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
776 + __jhash_mix(a,b,c);
784 + case 11: c += ((__u32)k[10]<<24);
785 + case 10: c += ((__u32)k[9]<<16);
786 + case 9 : c += ((__u32)k[8]<<8);
787 + case 8 : b += ((__u32)k[7]<<24);
788 + case 7 : b += ((__u32)k[6]<<16);
789 + case 6 : b += ((__u32)k[5]<<8);
790 + case 5 : b += k[4];
791 + case 4 : a += ((__u32)k[3]<<24);
792 + case 3 : a += ((__u32)k[2]<<16);
793 + case 2 : a += ((__u32)k[1]<<8);
794 + case 1 : a += k[0];
797 + __jhash_mix(a,b,c);
802 +/* A special optimized version that handles 1 or more of __u32s.
803 + * The length parameter here is the number of __u32s in the key.
805 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
807 + __u32 a, b, c, len;
809 + a = b = JHASH_GOLDEN_RATIO;
817 + __jhash_mix(a, b, c);
824 + case 2 : b += k[1];
825 + case 1 : a += k[0];
828 + __jhash_mix(a,b,c);
834 +/* A special ultra-optimized versions that knows they are hashing exactly
835 + * 3, 2 or 1 word(s).
837 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
838 + * done at the end is not done here.
840 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
842 + a += JHASH_GOLDEN_RATIO;
843 + b += JHASH_GOLDEN_RATIO;
846 + __jhash_mix(a, b, c);
851 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
853 + return jhash_3words(a, b, 0, initval);
856 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
858 + return jhash_3words(a, 0, 0, initval);
861 +#endif /* _LINUX_IPSET_JHASH_H */
862 diff -Naur linux-old/include/linux/netfilter_ipv4/ip_set_macipmap.h linux-new/include/linux/netfilter_ipv4/ip_set_macipmap.h
863 --- linux-old/include/linux/netfilter_ipv4/ip_set_macipmap.h 1970-01-01 01:00:00.000000000 +0100
864 +++ linux-new/include/linux/netfilter_ipv4/ip_set_macipmap.h 2005-08-10 22:13:00.000000000 +0200
866 +#ifndef __IP_SET_MACIPMAP_H
867 +#define __IP_SET_MACIPMAP_H
869 +#include <linux/netfilter_ipv4/ip_set.h>
871 +#define SETTYPE_NAME "macipmap"
872 +#define MAX_RANGE 0x0000FFFF
875 +#define IPSET_MACIP_MATCHUNSET 1
878 +#define IPSET_MACIP_ISSET 1
880 +struct ip_set_macipmap {
881 + void *members; /* the macipmap proper */
882 + ip_set_ip_t first_ip; /* host byte order, included in range */
883 + ip_set_ip_t last_ip; /* host byte order, included in range */
887 +struct ip_set_req_macipmap_create {
893 +struct ip_set_req_macipmap {
895 + unsigned char ethernet[ETH_ALEN];
898 +struct ip_set_macip {
899 + unsigned short flags;
900 + unsigned char ethernet[ETH_ALEN];
903 +#endif /* __IP_SET_MACIPMAP_H */
904 diff -Naur linux-old/include/linux/netfilter_ipv4/ip_set_malloc.h linux-new/include/linux/netfilter_ipv4/ip_set_malloc.h
905 --- linux-old/include/linux/netfilter_ipv4/ip_set_malloc.h 1970-01-01 01:00:00.000000000 +0100
906 +++ linux-new/include/linux/netfilter_ipv4/ip_set_malloc.h 2005-08-10 22:13:00.000000000 +0200
908 +#ifndef _IP_SET_MALLOC_H
909 +#define _IP_SET_MALLOC_H
913 +/* Memory allocation and deallocation */
914 +static size_t max_malloc_size = 131072; /* Guaranteed: slab.c */
916 +static inline void * ip_set_malloc(size_t bytes)
918 + if (bytes > max_malloc_size)
919 + return vmalloc(bytes);
921 + return kmalloc(bytes, GFP_KERNEL);
924 +static inline void ip_set_free(void * data, size_t bytes)
926 + if (bytes > max_malloc_size)
932 +#endif /* __KERNEL__ */
934 +#endif /*_IP_SET_MALLOC_H*/
935 diff -Naur linux-old/include/linux/netfilter_ipv4/ip_set_nethash.h linux-new/include/linux/netfilter_ipv4/ip_set_nethash.h
936 --- linux-old/include/linux/netfilter_ipv4/ip_set_nethash.h 1970-01-01 01:00:00.000000000 +0100
937 +++ linux-new/include/linux/netfilter_ipv4/ip_set_nethash.h 2005-08-10 22:13:00.000000000 +0200
939 +#ifndef __IP_SET_NETHASH_H
940 +#define __IP_SET_NETHASH_H
942 +#include <linux/netfilter_ipv4/ip_set.h>
944 +#define SETTYPE_NAME "nethash"
945 +#define MAX_RANGE 0x0000FFFF
947 +struct ip_set_nethash {
948 + ip_set_ip_t *members; /* the nethash proper */
949 + uint32_t initval; /* initval for jhash_1word */
950 + uint32_t prime; /* prime for double hashing */
951 + uint32_t hashsize; /* hash size */
952 + uint16_t probes; /* max number of probes */
953 + uint16_t resize; /* resize factor in percent */
954 + unsigned char cidr[30]; /* CIDR sizes */
957 +struct ip_set_req_nethash_create {
963 +struct ip_set_req_nethash {
965 + unsigned char cidr;
968 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
970 +static inline ip_set_ip_t
971 +pack(ip_set_ip_t ip, unsigned char cidr)
973 + ip_set_ip_t addr, *paddr = &addr;
974 + unsigned char n, t, *a;
976 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
978 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
982 + a = &((unsigned char *)paddr)[n];
983 + *a = *a /(1 << (8 - t)) + shifts[t];
985 + DP("n: %u, t: %u, a: %u", n, t, *a);
986 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
987 + HIPQUAD(ip), cidr, NIPQUAD(addr));
990 + return ntohl(addr);
993 +#endif /* __IP_SET_NETHASH_H */
994 diff -Naur linux-old/include/linux/netfilter_ipv4/ip_set_portmap.h linux-new/include/linux/netfilter_ipv4/ip_set_portmap.h
995 --- linux-old/include/linux/netfilter_ipv4/ip_set_portmap.h 1970-01-01 01:00:00.000000000 +0100
996 +++ linux-new/include/linux/netfilter_ipv4/ip_set_portmap.h 2005-08-10 22:13:00.000000000 +0200
998 +#ifndef __IP_SET_PORTMAP_H
999 +#define __IP_SET_PORTMAP_H
1001 +#include <linux/netfilter_ipv4/ip_set.h>
1003 +#define SETTYPE_NAME "portmap"
1004 +#define MAX_RANGE 0x0000FFFF
1005 +#define INVALID_PORT (MAX_RANGE + 1)
1007 +struct ip_set_portmap {
1008 + void *members; /* the portmap proper */
1009 + ip_set_ip_t first_port; /* host byte order, included in range */
1010 + ip_set_ip_t last_port; /* host byte order, included in range */
1013 +struct ip_set_req_portmap_create {
1018 +struct ip_set_req_portmap {
1022 +#endif /* __IP_SET_PORTMAP_H */
1023 diff -Naur linux-old/include/linux/netfilter_ipv4/ip_set_prime.h linux-new/include/linux/netfilter_ipv4/ip_set_prime.h
1024 --- linux-old/include/linux/netfilter_ipv4/ip_set_prime.h 1970-01-01 01:00:00.000000000 +0100
1025 +++ linux-new/include/linux/netfilter_ipv4/ip_set_prime.h 2005-08-10 22:13:00.000000000 +0200
1027 +#ifndef __IP_SET_PRIME_H
1028 +#define __IP_SET_PRIME_H
1030 +static inline unsigned make_prime_bound(unsigned nr)
1032 + unsigned long long nr64 = nr;
1033 + unsigned long long x = 1;
1035 + while (x <= nr64) { x <<= 2; nr <<= 1; }
1039 +static inline int make_prime_check(unsigned nr)
1042 + unsigned b = make_prime_bound(nr);
1044 + if (0 == (nr % x)) return 0;
1050 +static unsigned make_prime(unsigned nr)
1052 + if (0 == (nr & 1)) nr--;
1054 + if (make_prime_check(nr)) return nr;
1060 +#endif /* __IP_SET_PRIME_H */
1061 diff -Naur linux-old/include/linux/netfilter_ipv4/ipt_set.h linux-new/include/linux/netfilter_ipv4/ipt_set.h
1062 --- linux-old/include/linux/netfilter_ipv4/ipt_set.h 1970-01-01 01:00:00.000000000 +0100
1063 +++ linux-new/include/linux/netfilter_ipv4/ipt_set.h 2005-08-10 22:13:00.000000000 +0200
1068 +#include <linux/netfilter_ipv4/ip_set.h>
1070 +struct ipt_set_info {
1071 + ip_set_id_t index;
1072 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1076 +struct ipt_set_info_match {
1077 + struct ipt_set_info match_set;
1080 +struct ipt_set_info_target {
1081 + struct ipt_set_info add_set;
1082 + struct ipt_set_info del_set;
1085 +#endif /*_IPT_SET_H*/
1086 diff -Naur linux-old/net/ipv4/netfilter/Config.in linux-new/net/ipv4/netfilter/Config.in
1087 --- linux-old/net/ipv4/netfilter/Config.in 2005-07-23 23:34:46.000000000 +0200
1088 +++ linux-new/net/ipv4/netfilter/Config.in 2005-08-10 22:13:01.000000000 +0200
1090 if [ "$CONFIG_IP_NF_IPTABLES" != "n" ]; then
1091 # The simple matches.
1092 dep_tristate ' limit match support' CONFIG_IP_NF_MATCH_LIMIT $CONFIG_IP_NF_IPTABLES
1094 + dep_tristate ' IP set support' CONFIG_IP_NF_SET $CONFIG_IP_NF_IPTABLES
1095 + if [ "$CONFIG_IP_NF_SET" != "n" ]; then
1096 + int ' Maximum number of sets' CONFIG_IP_NF_SET_MAX 256
1097 + int ' Hash size for bindings of IP sets' CONFIG_IP_NF_SET_HASHSIZE 1024
1098 + dep_tristate ' set match support' CONFIG_IP_NF_MATCH_SET $CONFIG_IP_NF_SET
1099 + dep_tristate ' SET target support' CONFIG_IP_NF_TARGET_SET $CONFIG_IP_NF_SET
1100 + dep_tristate ' ipmap set type support' CONFIG_IP_NF_SET_IPMAP $CONFIG_IP_NF_SET
1101 + dep_tristate ' portmap set type support' CONFIG_IP_NF_SET_PORTMAP $CONFIG_IP_NF_SET
1102 + dep_tristate ' macipmap set type support' CONFIG_IP_NF_SET_MACIPMAP $CONFIG_IP_NF_SET
1103 + dep_tristate ' iphash set type support' CONFIG_IP_NF_SET_IPHASH $CONFIG_IP_NF_SET
1104 + dep_tristate ' nethash set type support' CONFIG_IP_NF_SET_NETHASH $CONFIG_IP_NF_SET
1105 + dep_tristate ' iptree set type support' CONFIG_IP_NF_SET_IPTREE $CONFIG_IP_NF_SET
1107 dep_tristate ' MAC address match support' CONFIG_IP_NF_MATCH_MAC $CONFIG_IP_NF_IPTABLES
1108 dep_tristate ' Packet type match support' CONFIG_IP_NF_MATCH_PKTTYPE $CONFIG_IP_NF_IPTABLES
1109 dep_tristate ' netfilter MARK match support' CONFIG_IP_NF_MATCH_MARK $CONFIG_IP_NF_IPTABLES
1110 diff -Naur linux-old/net/ipv4/netfilter/ip_set.c linux-new/net/ipv4/netfilter/ip_set.c
1111 --- linux-old/net/ipv4/netfilter/ip_set.c 1970-01-01 01:00:00.000000000 +0100
1112 +++ linux-new/net/ipv4/netfilter/ip_set.c 2005-08-10 22:13:00.000000000 +0200
1114 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1115 + * Patrick Schaaf <bof@bof.de>
1116 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1118 + * This program is free software; you can redistribute it and/or modify
1119 + * it under the terms of the GNU General Public License version 2 as
1120 + * published by the Free Software Foundation.
1123 +/* Kernel module for IP set management */
1125 +#include <linux/config.h>
1126 +#include <linux/module.h>
1127 +#include <linux/kmod.h>
1128 +#include <linux/ip.h>
1129 +#include <linux/skbuff.h>
1130 +#include <linux/random.h>
1131 +#include <linux/jhash.h>
1132 +#include <linux/netfilter_ipv4/ip_tables.h>
1133 +#include <linux/errno.h>
1134 +#include <asm/uaccess.h>
1135 +#include <asm/bitops.h>
1136 +#include <asm/softirq.h>
1137 +#include <asm/semaphore.h>
1138 +#include <linux/spinlock.h>
1139 +#include <linux/vmalloc.h>
1141 +#define ASSERT_READ_LOCK(x) /* dont use that */
1142 +#define ASSERT_WRITE_LOCK(x)
1143 +#include <linux/netfilter_ipv4/listhelp.h>
1144 +#include <linux/netfilter_ipv4/ip_set.h>
1146 +static struct list_head set_type_list; /* all registered sets */
1147 +static struct ip_set **ip_set_list; /* all individual sets */
1148 +static DECLARE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1149 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1150 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1151 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1152 +static struct list_head *ip_set_hash; /* hash of bindings */
1153 +static unsigned int ip_set_hash_random; /* random seed */
1157 +#define __MOD_INC(foo) __MOD_INC_USE_COUNT(foo)
1158 +#define __MOD_DEC(foo) __MOD_DEC_USE_COUNT(foo)
1159 +#define __MOD_INC_SELF MOD_INC_USE_COUNT
1160 +#define __MOD_DEC_SELF MOD_DEC_USE_COUNT
1162 +#define __MOD_INC(foo)
1163 +#define __MOD_DEC(foo)
1164 +#define __MOD_INC_SELF
1165 +#define __MOD_DEC_SELF
1169 + * Sets are identified either by the index in ip_set_list or by id.
1170 + * The id never changes and is used to find a key in the hash.
1171 + * The index may change by swapping and used at all other places
1172 + * (set/SET netfilter modules, binding value, etc.)
1174 + * Userspace requests are serialized by ip_set_mutex and sets can
1175 + * be deleted only from userspace. Therefore ip_set_list locking
1176 + * must obey the following rules:
1178 + * - kernel requests: read and write locking mandatory
1179 + * - user requests: read locking optional, write locking mandatory
1183 +__ip_set_get(ip_set_id_t index)
1185 + atomic_inc(&ip_set_list[index]->ref);
1189 +__ip_set_put(ip_set_id_t index)
1191 + atomic_dec(&ip_set_list[index]->ref);
1195 + * Binding routines
1199 +ip_hash_cmp(const struct ip_set_hash *set_hash,
1200 + ip_set_id_t id, ip_set_ip_t ip)
1202 + return set_hash->id == id && set_hash->ip == ip;
1206 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1208 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1209 + % ip_set_bindings_hash_size;
1210 + struct ip_set_hash *set_hash;
1212 + MUST_BE_READ_LOCKED(&ip_set_lock);
1213 + IP_SET_ASSERT(ip_set_list[id]);
1214 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1216 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1217 + struct ip_set_hash *, id, ip);
1219 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1221 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1223 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1227 +__set_hash_del(struct ip_set_hash *set_hash)
1229 + MUST_BE_WRITE_LOCKED(&ip_set_lock);
1230 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1232 + __ip_set_put(set_hash->binding);
1233 + list_del(&set_hash->list);
1238 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1240 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1241 + % ip_set_bindings_hash_size;
1242 + struct ip_set_hash *set_hash;
1244 + IP_SET_ASSERT(ip_set_list[id]);
1245 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1246 + WRITE_LOCK(&ip_set_lock);
1247 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1248 + struct ip_set_hash *, id, ip);
1249 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1251 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1253 + if (set_hash != NULL)
1254 + __set_hash_del(set_hash);
1255 + WRITE_UNLOCK(&ip_set_lock);
1260 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1262 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1263 + % ip_set_bindings_hash_size;
1264 + struct ip_set_hash *set_hash;
1267 + IP_SET_ASSERT(ip_set_list[id]);
1268 + IP_SET_ASSERT(ip_set_list[binding]);
1269 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1270 + HIPQUAD(ip), ip_set_list[binding]->name);
1271 + WRITE_LOCK(&ip_set_lock);
1272 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1273 + struct ip_set_hash *, id, ip);
1275 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_KERNEL);
1280 + INIT_LIST_HEAD(&set_hash->list);
1281 + set_hash->id = id;
1282 + set_hash->ip = ip;
1283 + list_add(&ip_set_hash[key], &set_hash->list);
1285 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1286 + DP("overwrite binding: %s",
1287 + ip_set_list[set_hash->binding]->name);
1288 + __ip_set_put(set_hash->binding);
1290 + set_hash->binding = binding;
1291 + __ip_set_get(set_hash->binding);
1293 + WRITE_UNLOCK(&ip_set_lock);
1297 +#define FOREACH_HASH_DO(fn, args...) \
1299 + ip_set_id_t __key; \
1300 + struct ip_set_hash *__set_hash; \
1302 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1303 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1304 + fn(__set_hash , ## args); \
1308 +#define FOREACH_HASH_RW_DO(fn, args...) \
1310 + ip_set_id_t __key; \
1311 + struct ip_set_hash *__set_hash, *__n; \
1313 + MUST_BE_WRITE_LOCKED(&ip_set_lock); \
1314 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1315 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1316 + fn(__set_hash , ## args); \
1320 +/* Add, del and test set entries from kernel */
1322 +#define follow_bindings(index, set, ip) \
1323 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1324 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1327 +ip_set_testip_kernel(ip_set_id_t index,
1328 + const struct sk_buff *skb,
1329 + const u_int32_t *flags)
1331 + struct ip_set *set;
1335 + IP_SET_ASSERT(flags[i]);
1336 + READ_LOCK(&ip_set_lock);
1338 + set = ip_set_list[index];
1339 + IP_SET_ASSERT(set);
1340 + DP("set %s, index %u", set->name, index);
1341 + read_lock_bh(&set->lock);
1342 + res = set->type->testip_kernel(set, skb, flags[i], &ip);
1343 + read_unlock_bh(&set->lock);
1346 + && follow_bindings(index, set, ip));
1347 + READ_UNLOCK(&ip_set_lock);
1353 +ip_set_addip_kernel(ip_set_id_t index,
1354 + const struct sk_buff *skb,
1355 + const u_int32_t *flags)
1357 + struct ip_set *set;
1361 + IP_SET_ASSERT(flags[i]);
1363 + READ_LOCK(&ip_set_lock);
1365 + set = ip_set_list[index];
1366 + IP_SET_ASSERT(set);
1367 + DP("set %s, index %u", set->name, index);
1368 + write_lock_bh(&set->lock);
1369 + res = set->type->addip_kernel(set, skb, flags[i], &ip);
1370 + write_unlock_bh(&set->lock);
1371 + } while ((res == 0 || res == -EEXIST)
1373 + && follow_bindings(index, set, ip));
1374 + READ_UNLOCK(&ip_set_lock);
1376 + if (res == -EAGAIN
1377 + && set->type->retry
1378 + && (res = set->type->retry(set)) == 0)
1383 +ip_set_delip_kernel(ip_set_id_t index,
1384 + const struct sk_buff *skb,
1385 + const u_int32_t *flags)
1387 + struct ip_set *set;
1391 + IP_SET_ASSERT(flags[i]);
1392 + READ_LOCK(&ip_set_lock);
1394 + set = ip_set_list[index];
1395 + IP_SET_ASSERT(set);
1396 + DP("set %s, index %u", set->name, index);
1397 + write_lock_bh(&set->lock);
1398 + res = set->type->delip_kernel(set, skb, flags[i], &ip);
1399 + write_unlock_bh(&set->lock);
1400 + } while ((res == 0 || res == -EEXIST)
1402 + && follow_bindings(index, set, ip));
1403 + READ_UNLOCK(&ip_set_lock);
1406 +/* Register and deregister settype */
1409 +set_type_equal(const struct ip_set_type *set_type, const char *str2)
1411 + return !strncmp(set_type->typename, str2, IP_SET_MAXNAMELEN - 1);
1414 +static inline struct ip_set_type *
1415 +find_set_type(const char *name)
1417 + return LIST_FIND(&set_type_list,
1419 + struct ip_set_type *,
1424 +ip_set_register_set_type(struct ip_set_type *set_type)
1428 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1429 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1430 + set_type->typename,
1431 + set_type->protocol_version,
1432 + IP_SET_PROTOCOL_VERSION);
1436 + WRITE_LOCK(&ip_set_lock);
1437 + if (find_set_type(set_type->typename)) {
1439 + ip_set_printk("'%s' already registered!",
1440 + set_type->typename);
1445 + list_append(&set_type_list, set_type);
1446 + DP("'%s' registered.", set_type->typename);
1448 + WRITE_UNLOCK(&ip_set_lock);
1453 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1455 + WRITE_LOCK(&ip_set_lock);
1456 + if (!find_set_type(set_type->typename)) {
1457 + ip_set_printk("'%s' not registered?",
1458 + set_type->typename);
1461 + LIST_DELETE(&set_type_list, set_type);
1463 + DP("'%s' unregistered.", set_type->typename);
1465 + WRITE_UNLOCK(&ip_set_lock);
1470 + * Userspace routines
1474 + * Find set by name, reference it once. The reference makes sure the
1475 + * thing pointed to, does not go away under our feet. Drop the reference
1476 + * later, using ip_set_put().
1479 +ip_set_get_byname(const char *name)
1481 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1483 + down(&ip_set_app_mutex);
1484 + for (i = 0; i < ip_set_max; i++) {
1485 + if (ip_set_list[i] != NULL
1486 + && strcmp(ip_set_list[i]->name, name) == 0) {
1492 + up(&ip_set_app_mutex);
1497 + * Find set by index, reference it once. The reference makes sure the
1498 + * thing pointed to, does not go away under our feet. Drop the reference
1499 + * later, using ip_set_put().
1502 +ip_set_get_byindex(ip_set_id_t index)
1504 + down(&ip_set_app_mutex);
1506 + if (index >= ip_set_max)
1507 + return IP_SET_INVALID_ID;
1509 + if (ip_set_list[index])
1510 + __ip_set_get(index);
1512 + index = IP_SET_INVALID_ID;
1514 + up(&ip_set_app_mutex);
1519 + * If the given set pointer points to a valid set, decrement
1520 + * reference count by 1. The caller shall not assume the index
1521 + * to be valid, after calling this function.
1523 +void ip_set_put(ip_set_id_t index)
1525 + down(&ip_set_app_mutex);
1526 + if (ip_set_list[index])
1527 + __ip_set_put(index);
1528 + up(&ip_set_app_mutex);
1531 +/* Find a set by name or index */
1533 +ip_set_find_byname(const char *name)
1535 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1537 + for (i = 0; i < ip_set_max; i++) {
1538 + if (ip_set_list[i] != NULL
1539 + && strcmp(ip_set_list[i]->name, name) == 0) {
1548 +ip_set_find_byindex(ip_set_id_t index)
1550 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1551 + index = IP_SET_INVALID_ID;
1557 + * Add, del, test, bind and unbind
1561 +__ip_set_testip(struct ip_set *set,
1568 + read_lock_bh(&set->lock);
1569 + res = set->type->testip(set, data, size, ip);
1570 + read_unlock_bh(&set->lock);
1576 +__ip_set_addip(ip_set_id_t index,
1580 + struct ip_set *set = ip_set_list[index];
1584 + IP_SET_ASSERT(set);
1586 + write_lock_bh(&set->lock);
1587 + res = set->type->addip(set, data, size, &ip);
1588 + write_unlock_bh(&set->lock);
1589 + } while (res == -EAGAIN
1590 + && set->type->retry
1591 + && (res = set->type->retry(set)) == 0);
1597 +ip_set_addip(ip_set_id_t index,
1602 + return __ip_set_addip(index,
1603 + data + sizeof(struct ip_set_req_adt),
1604 + size - sizeof(struct ip_set_req_adt));
1608 +ip_set_delip(ip_set_id_t index,
1612 + struct ip_set *set = ip_set_list[index];
1616 + IP_SET_ASSERT(set);
1617 + write_lock_bh(&set->lock);
1618 + res = set->type->delip(set,
1619 + data + sizeof(struct ip_set_req_adt),
1620 + size - sizeof(struct ip_set_req_adt),
1622 + write_unlock_bh(&set->lock);
1628 +ip_set_testip(ip_set_id_t index,
1632 + struct ip_set *set = ip_set_list[index];
1636 + IP_SET_ASSERT(set);
1637 + res = __ip_set_testip(set,
1638 + data + sizeof(struct ip_set_req_adt),
1639 + size - sizeof(struct ip_set_req_adt),
1642 + return (res > 0 ? -EEXIST : res);
1646 +ip_set_bindip(ip_set_id_t index,
1650 + struct ip_set *set = ip_set_list[index];
1651 + struct ip_set_req_bind *req_bind;
1652 + ip_set_id_t binding;
1656 + IP_SET_ASSERT(set);
1657 + if (size < sizeof(struct ip_set_req_bind))
1660 + req_bind = (struct ip_set_req_bind *) data;
1661 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1663 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1664 + /* Default binding of a set */
1665 + char *binding_name;
1667 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1670 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1671 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1673 + binding = ip_set_find_byname(binding_name);
1674 + if (binding == IP_SET_INVALID_ID)
1677 + WRITE_LOCK(&ip_set_lock);
1678 + /* Sets as binding values are referenced */
1679 + if (set->binding != IP_SET_INVALID_ID)
1680 + __ip_set_put(set->binding);
1681 + set->binding = binding;
1682 + __ip_set_get(set->binding);
1683 + WRITE_UNLOCK(&ip_set_lock);
1687 + binding = ip_set_find_byname(req_bind->binding);
1688 + if (binding == IP_SET_INVALID_ID)
1691 + res = __ip_set_testip(set,
1692 + data + sizeof(struct ip_set_req_bind),
1693 + size - sizeof(struct ip_set_req_bind),
1695 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1696 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1699 + res = ip_set_hash_add(set->id, ip, binding);
1704 +#define FOREACH_SET_DO(fn, args...) \
1706 + ip_set_id_t __i; \
1707 + struct ip_set *__set; \
1709 + for (__i = 0; __i < ip_set_max; __i++) { \
1710 + __set = ip_set_list[__i]; \
1711 + if (__set != NULL) \
1712 + fn(__set , ##args); \
1717 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1719 + if (set_hash->id == id)
1720 + __set_hash_del(set_hash);
1724 +__unbind_default(struct ip_set *set)
1726 + if (set->binding != IP_SET_INVALID_ID) {
1727 + /* Sets as binding values are referenced */
1728 + __ip_set_put(set->binding);
1729 + set->binding = IP_SET_INVALID_ID;
1734 +ip_set_unbindip(ip_set_id_t index,
1738 + struct ip_set *set;
1739 + struct ip_set_req_bind *req_bind;
1744 + if (size < sizeof(struct ip_set_req_bind))
1747 + req_bind = (struct ip_set_req_bind *) data;
1748 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1750 + DP("%u %s", index, req_bind->binding);
1751 + if (index == IP_SET_INVALID_ID) {
1752 + /* unbind :all: */
1753 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1754 + /* Default binding of sets */
1755 + WRITE_LOCK(&ip_set_lock);
1756 + FOREACH_SET_DO(__unbind_default);
1757 + WRITE_UNLOCK(&ip_set_lock);
1759 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1760 + /* Flush all bindings of all sets*/
1761 + WRITE_LOCK(&ip_set_lock);
1762 + FOREACH_HASH_RW_DO(__set_hash_del);
1763 + WRITE_UNLOCK(&ip_set_lock);
1766 + DP("unreachable reached!");
1770 + set = ip_set_list[index];
1771 + IP_SET_ASSERT(set);
1772 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1773 + /* Default binding of set */
1774 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1776 + if (binding == IP_SET_INVALID_ID)
1779 + WRITE_LOCK(&ip_set_lock);
1780 + /* Sets in hash values are referenced */
1781 + __ip_set_put(set->binding);
1782 + set->binding = IP_SET_INVALID_ID;
1783 + WRITE_UNLOCK(&ip_set_lock);
1786 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1787 + /* Flush all bindings */
1789 + WRITE_LOCK(&ip_set_lock);
1790 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1791 + WRITE_UNLOCK(&ip_set_lock);
1795 + res = __ip_set_testip(set,
1796 + data + sizeof(struct ip_set_req_bind),
1797 + size - sizeof(struct ip_set_req_bind),
1800 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1802 + res = ip_set_hash_del(set->id, ip);
1808 +ip_set_testbind(ip_set_id_t index,
1812 + struct ip_set *set = ip_set_list[index];
1813 + struct ip_set_req_bind *req_bind;
1814 + ip_set_id_t binding;
1818 + IP_SET_ASSERT(set);
1819 + if (size < sizeof(struct ip_set_req_bind))
1822 + req_bind = (struct ip_set_req_bind *) data;
1823 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1825 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1826 + /* Default binding of set */
1827 + char *binding_name;
1829 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1832 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1833 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1835 + binding = ip_set_find_byname(binding_name);
1836 + if (binding == IP_SET_INVALID_ID)
1839 + res = (set->binding == binding) ? -EEXIST : 0;
1843 + binding = ip_set_find_byname(req_bind->binding);
1844 + if (binding == IP_SET_INVALID_ID)
1848 + res = __ip_set_testip(set,
1849 + data + sizeof(struct ip_set_req_bind),
1850 + size - sizeof(struct ip_set_req_bind),
1852 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1853 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1856 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1862 +static struct ip_set_type *
1863 +find_set_type_rlock(const char *typename)
1865 + struct ip_set_type *type;
1867 + READ_LOCK(&ip_set_lock);
1868 + type = find_set_type(typename);
1870 + READ_UNLOCK(&ip_set_lock);
1876 +find_free_id(const char *name,
1877 + ip_set_id_t *index,
1882 + *id = IP_SET_INVALID_ID;
1883 + for (i = 0; i < ip_set_max; i++) {
1884 + if (ip_set_list[i] == NULL) {
1885 + if (*id == IP_SET_INVALID_ID)
1887 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1891 + if (*id == IP_SET_INVALID_ID)
1892 + /* No free slot remained */
1894 + /* Check that index is usable as id (swapping) */
1896 + for (i = 0; i < ip_set_max; i++) {
1897 + if (ip_set_list[i] != NULL
1898 + && ip_set_list[i]->id == *id) {
1910 +ip_set_create(const char *name,
1911 + const char *typename,
1912 + ip_set_id_t restore,
1916 + struct ip_set *set;
1917 + ip_set_id_t index, id;
1920 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1922 + * First, and without any locks, allocate and initialize
1923 + * a normal base set structure.
1925 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1928 + set->lock = RW_LOCK_UNLOCKED;
1929 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1930 + set->binding = IP_SET_INVALID_ID;
1931 + atomic_set(&set->ref, 0);
1934 + * Next, take the &ip_set_lock, check that we know the type,
1935 + * and take a reference on the type, to make sure it
1936 + * stays available while constructing our new set.
1938 + * After referencing the type, we drop the &ip_set_lock,
1939 + * and let the new set construction run without locks.
1941 + set->type = find_set_type_rlock(typename);
1942 + if (set->type == NULL) {
1943 + /* Try loading the module */
1944 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1945 + strcpy(modulename, "ip_set_");
1946 + strcat(modulename, typename);
1947 + DP("try to load %s", modulename);
1948 + request_module(modulename);
1949 + set->type = find_set_type_rlock(typename);
1951 + if (set->type == NULL) {
1952 + ip_set_printk("no set type '%s', set '%s' not created",
1957 + __MOD_INC(set->type->me);
1958 + READ_UNLOCK(&ip_set_lock);
1961 + * Without holding any locks, create private part.
1963 + res = set->type->create(set, data, size);
1965 + __MOD_DEC(set->type->me);
1970 + /* BTW, res==0 here. */
1973 + * Here, we have a valid, constructed set. &ip_set_lock again,
1974 + * find free id/index and check that it is not already in
1977 + WRITE_LOCK(&ip_set_lock);
1978 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
1979 + DP("no free id!");
1983 + /* Make sure restore gets the same index */
1984 + if (restore != IP_SET_INVALID_ID && index != restore) {
1985 + DP("Can't restore, sets are screwed up");
1991 + * Finally! Add our shiny new set to the list, and be done.
1993 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
1995 + ip_set_list[index] = set;
1996 + WRITE_UNLOCK(&ip_set_lock);
2000 + WRITE_UNLOCK(&ip_set_lock);
2001 + set->type->destroy(set);
2002 + __MOD_DEC(set->type->me);
2008 + * Destroy a given existing set
2011 +ip_set_destroy_set(ip_set_id_t index)
2013 + struct ip_set *set = ip_set_list[index];
2015 + IP_SET_ASSERT(set);
2016 + DP("set: %s", set->name);
2017 + WRITE_LOCK(&ip_set_lock);
2018 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2019 + if (set->binding != IP_SET_INVALID_ID)
2020 + __ip_set_put(set->binding);
2021 + ip_set_list[index] = NULL;
2022 + WRITE_UNLOCK(&ip_set_lock);
2024 + /* Must call it without holding any lock */
2025 + set->type->destroy(set);
2026 + __MOD_DEC(set->type->me);
2031 + * Destroy a set - or all sets
2032 + * Sets must not be referenced/used.
2035 +ip_set_destroy(ip_set_id_t index)
2039 + /* ref modification always protected by the mutex */
2040 + if (index != IP_SET_INVALID_ID) {
2041 + if (atomic_read(&ip_set_list[index]->ref))
2043 + ip_set_destroy_set(index);
2045 + for (i = 0; i < ip_set_max; i++) {
2046 + if (ip_set_list[i] != NULL
2047 + && (atomic_read(&ip_set_list[i]->ref)))
2051 + for (i = 0; i < ip_set_max; i++) {
2052 + if (ip_set_list[i] != NULL)
2053 + ip_set_destroy_set(i);
2060 +ip_set_flush_set(struct ip_set *set)
2062 + DP("set: %s %u", set->name, set->id);
2064 + write_lock_bh(&set->lock);
2065 + set->type->flush(set);
2066 + write_unlock_bh(&set->lock);
2070 + * Flush data in a set - or in all sets
2073 +ip_set_flush(ip_set_id_t index)
2075 + if (index != IP_SET_INVALID_ID) {
2076 + IP_SET_ASSERT(ip_set_list[index]);
2077 + ip_set_flush_set(ip_set_list[index]);
2079 + FOREACH_SET_DO(ip_set_flush_set);
2086 +ip_set_rename(ip_set_id_t index, const char *name)
2088 + struct ip_set *set = ip_set_list[index];
2092 + DP("set: %s to %s", set->name, name);
2093 + WRITE_LOCK(&ip_set_lock);
2094 + for (i = 0; i < ip_set_max; i++) {
2095 + if (ip_set_list[i] != NULL
2096 + && strncmp(ip_set_list[i]->name,
2098 + IP_SET_MAXNAMELEN - 1) == 0) {
2103 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2105 + WRITE_UNLOCK(&ip_set_lock);
2110 + * Swap two sets so that name/index points to the other.
2111 + * References are also swapped.
2114 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2116 + struct ip_set *from = ip_set_list[from_index];
2117 + struct ip_set *to = ip_set_list[to_index];
2118 + char from_name[IP_SET_MAXNAMELEN];
2119 + u_int32_t from_ref;
2121 + DP("set: %s to %s", from->name, to->name);
2122 + /* Type can't be changed. Artifical restriction. */
2123 + if (from->type->typecode != to->type->typecode)
2126 + /* No magic here: ref munging protected by the mutex */
2127 + WRITE_LOCK(&ip_set_lock);
2128 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2129 + from_ref = atomic_read(&from->ref);
2131 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2132 + atomic_set(&from->ref, atomic_read(&to->ref));
2133 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2134 + atomic_set(&to->ref, from_ref);
2136 + ip_set_list[from_index] = to;
2137 + ip_set_list[to_index] = from;
2139 + WRITE_UNLOCK(&ip_set_lock);
2148 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2149 + ip_set_id_t id, size_t *size)
2151 + if (set_hash->id == id)
2152 + *size += sizeof(struct ip_set_hash_list);
2156 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2157 + ip_set_id_t id, size_t *size)
2159 + if (set_hash->id == id)
2160 + *size += sizeof(struct ip_set_hash_save);
2164 +__set_hash_bindings(struct ip_set_hash *set_hash,
2165 + ip_set_id_t id, void *data, int *used)
2167 + if (set_hash->id == id) {
2168 + struct ip_set_hash_list *hash_list =
2169 + (struct ip_set_hash_list *)(data + *used);
2171 + hash_list->ip = set_hash->ip;
2172 + hash_list->binding = set_hash->binding;
2173 + *used += sizeof(struct ip_set_hash_list);
2177 +static int ip_set_list_set(ip_set_id_t index,
2182 + struct ip_set *set = ip_set_list[index];
2183 + struct ip_set_list *set_list;
2185 + /* Pointer to our header */
2186 + set_list = (struct ip_set_list *) (data + *used);
2188 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2190 + /* Get and ensure header size */
2191 + if (*used + sizeof(struct ip_set_list) > len)
2192 + goto not_enough_mem;
2193 + *used += sizeof(struct ip_set_list);
2195 + read_lock_bh(&set->lock);
2196 + /* Get and ensure set specific header size */
2197 + set_list->header_size = set->type->header_size;
2198 + if (*used + set_list->header_size > len)
2201 + /* Fill in the header */
2202 + set_list->index = index;
2203 + set_list->binding = set->binding;
2204 + set_list->ref = atomic_read(&set->ref);
2206 + /* Fill in set spefific header data */
2207 + DP("call list_header");
2208 + set->type->list_header(set, data + *used);
2209 + DP("call list_header, done");
2210 + *used += set_list->header_size;
2212 + /* Get and ensure set specific members size */
2213 + DP("call list_members_size");
2214 + set_list->members_size = set->type->list_members_size(set);
2215 + DP("call list_members_size, done");
2216 + if (*used + set_list->members_size > len)
2219 + /* Fill in set spefific members data */
2220 + DP("call list_members");
2221 + set->type->list_members(set, data + *used);
2222 + DP("call list_members, done");
2223 + *used += set_list->members_size;
2224 + read_unlock_bh(&set->lock);
2228 + /* Get and ensure set specific bindings size */
2229 + set_list->bindings_size = 0;
2230 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2231 + set->id, &set_list->bindings_size);
2232 + if (*used + set_list->bindings_size > len)
2233 + goto not_enough_mem;
2235 + /* Fill in set spefific bindings data */
2236 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2241 + read_unlock_bh(&set->lock);
2243 + DP("not enough mem, try again");
2250 +static int ip_set_save_set(ip_set_id_t index,
2255 + struct ip_set *set;
2256 + struct ip_set_save *set_save;
2258 + /* Pointer to our header */
2259 + set_save = (struct ip_set_save *) (data + *used);
2261 + /* Get and ensure header size */
2262 + if (*used + sizeof(struct ip_set_save) > len)
2263 + goto not_enough_mem;
2264 + *used += sizeof(struct ip_set_save);
2266 + set = ip_set_list[index];
2267 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2268 + data, data + *used);
2270 + read_lock_bh(&set->lock);
2271 + /* Get and ensure set specific header size */
2272 + set_save->header_size = set->type->header_size;
2273 + if (*used + set_save->header_size > len)
2276 + /* Fill in the header */
2277 + set_save->index = index;
2278 + set_save->binding = set->binding;
2280 + /* Fill in set spefific header data */
2281 + set->type->list_header(set, data + *used);
2282 + *used += set_save->header_size;
2284 + DP("set header filled: %s, used: %u %p %p", set->name, *used,
2285 + data, data + *used);
2286 + /* Get and ensure set specific members size */
2287 + set_save->members_size = set->type->list_members_size(set);
2288 + if (*used + set_save->members_size > len)
2291 + /* Fill in set spefific members data */
2292 + set->type->list_members(set, data + *used);
2293 + *used += set_save->members_size;
2294 + read_unlock_bh(&set->lock);
2295 + DP("set members filled: %s, used: %u %p %p", set->name, *used,
2296 + data, data + *used);
2300 + read_unlock_bh(&set->lock);
2302 + DP("not enough mem, try again");
2307 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2315 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2316 + struct ip_set_hash_save *hash_save =
2317 + (struct ip_set_hash_save *)(data + *used);
2318 + /* Ensure bindings size */
2319 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2323 + hash_save->id = set_hash->id;
2324 + hash_save->ip = set_hash->ip;
2325 + hash_save->binding = set_hash->binding;
2326 + *used += sizeof(struct ip_set_hash_save);
2330 +static int ip_set_save_bindings(ip_set_id_t index,
2336 + struct ip_set_save *set_save;
2338 + DP("used %u, len %u", *used, len);
2339 + /* Get and ensure header size */
2340 + if (*used + sizeof(struct ip_set_save) > len)
2344 + set_save = (struct ip_set_save *) (data + *used);
2345 + set_save->index = IP_SET_INVALID_ID;
2346 + *used += sizeof(struct ip_set_save);
2348 + DP("marker added used %u, len %u", *used, len);
2349 + /* Fill in bindings data */
2350 + if (index != IP_SET_INVALID_ID)
2351 + /* Sets are identified by id in hash */
2352 + index = ip_set_list[index]->id;
2353 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2361 +static int ip_set_restore(void *data,
2365 + int line = 0, used = 0, members_size;
2366 + struct ip_set *set;
2367 + struct ip_set_hash_save *hash_save;
2368 + struct ip_set_restore *set_restore;
2369 + ip_set_id_t index;
2371 + /* Loop to restore sets */
2375 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2376 + /* Get and ensure header size */
2377 + if (used + sizeof(struct ip_set_restore) > len)
2379 + set_restore = (struct ip_set_restore *) (data + used);
2380 + used += sizeof(struct ip_set_restore);
2382 + /* Ensure data size */
2384 + + set_restore->header_size
2385 + + set_restore->members_size > len)
2388 + /* Check marker */
2389 + if (set_restore->index == IP_SET_INVALID_ID) {
2394 + /* Try to create the set */
2395 + DP("restore %s %s", set_restore->name, set_restore->typename);
2396 + res = ip_set_create(set_restore->name,
2397 + set_restore->typename,
2398 + set_restore->index,
2400 + set_restore->header_size);
2404 + used += set_restore->header_size;
2406 + index = ip_set_find_byindex(set_restore->index);
2407 + DP("index %u, restore_index %u", index, set_restore->index);
2408 + if (index != set_restore->index)
2410 + /* Try to restore members data */
2411 + set = ip_set_list[index];
2413 + DP("members_size %u reqsize %u",
2414 + set_restore->members_size, set->type->reqsize);
2415 + while (members_size + set->type->reqsize <=
2416 + set_restore->members_size) {
2418 + DP("members: %u, line %u", members_size, line);
2419 + res = __ip_set_addip(index,
2420 + data + used + members_size,
2421 + set->type->reqsize);
2422 + if (!(res == 0 || res == -EEXIST))
2424 + members_size += set->type->reqsize;
2427 + DP("members_size %u %u",
2428 + set_restore->members_size, members_size);
2429 + if (members_size != set_restore->members_size)
2431 + used += set_restore->members_size;
2435 + /* Loop to restore bindings */
2436 + while (used < len) {
2439 + DP("restore binding, line %u", line);
2440 + /* Get and ensure size */
2441 + if (used + sizeof(struct ip_set_hash_save) > len)
2443 + hash_save = (struct ip_set_hash_save *) (data + used);
2444 + used += sizeof(struct ip_set_hash_save);
2446 + /* hash_save->id is used to store the index */
2447 + index = ip_set_find_byindex(hash_save->id);
2448 + DP("restore binding index %u, id %u, %u -> %u",
2449 + index, hash_save->id, hash_save->ip, hash_save->binding);
2450 + if (index != hash_save->id)
2453 + set = ip_set_list[hash_save->id];
2454 + /* Null valued IP means default binding */
2455 + if (hash_save->ip)
2456 + res = ip_set_hash_add(set->id,
2458 + hash_save->binding);
2460 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2461 + WRITE_LOCK(&ip_set_lock);
2462 + set->binding = hash_save->binding;
2463 + __ip_set_get(set->binding);
2464 + WRITE_UNLOCK(&ip_set_lock);
2465 + DP("default binding: %u", set->binding);
2477 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2480 + int res = 0; /* Assume OK */
2482 + struct ip_set_req_adt *req_adt;
2483 + ip_set_id_t index = IP_SET_INVALID_ID;
2484 + int (*adtfn)(ip_set_id_t index,
2485 + const void *data, size_t size);
2487 + int (*fn)(ip_set_id_t index,
2488 + const void *data, size_t size);
2490 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2491 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2494 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2495 + if (!capable(CAP_NET_ADMIN))
2497 + if (optval != SO_IP_SET)
2499 + if (len <= sizeof(unsigned)) {
2500 + ip_set_printk("short userdata (want >%zu, got %u)",
2501 + sizeof(unsigned), len);
2504 + data = vmalloc(len);
2506 + DP("out of mem for %u bytes", len);
2509 + if (copy_from_user(data, user, len) != 0) {
2513 + if (down_interruptible(&ip_set_app_mutex)) {
2518 + op = (unsigned *)data;
2521 + if (*op < IP_SET_OP_VERSION) {
2522 + /* Check the version at the beginning of operations */
2523 + struct ip_set_req_version *req_version =
2524 + (struct ip_set_req_version *) data;
2525 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2532 + case IP_SET_OP_CREATE:{
2533 + struct ip_set_req_create *req_create
2534 + = (struct ip_set_req_create *) data;
2536 + if (len <= sizeof(struct ip_set_req_create)) {
2537 + ip_set_printk("short CREATE data (want >%zu, got %u)",
2538 + sizeof(struct ip_set_req_create), len);
2542 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2543 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2544 + res = ip_set_create(req_create->name,
2545 + req_create->typename,
2546 + IP_SET_INVALID_ID,
2547 + data + sizeof(struct ip_set_req_create),
2548 + len - sizeof(struct ip_set_req_create));
2551 + case IP_SET_OP_DESTROY:{
2552 + struct ip_set_req_std *req_destroy
2553 + = (struct ip_set_req_std *) data;
2555 + if (len != sizeof(struct ip_set_req_std)) {
2556 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2557 + sizeof(struct ip_set_req_std), len);
2561 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2562 + /* Destroy all sets */
2563 + index = IP_SET_INVALID_ID;
2565 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2566 + index = ip_set_find_byname(req_destroy->name);
2568 + if (index == IP_SET_INVALID_ID) {
2574 + res = ip_set_destroy(index);
2577 + case IP_SET_OP_FLUSH:{
2578 + struct ip_set_req_std *req_flush =
2579 + (struct ip_set_req_std *) data;
2581 + if (len != sizeof(struct ip_set_req_std)) {
2582 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2583 + sizeof(struct ip_set_req_std), len);
2587 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2588 + /* Flush all sets */
2589 + index = IP_SET_INVALID_ID;
2591 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2592 + index = ip_set_find_byname(req_flush->name);
2594 + if (index == IP_SET_INVALID_ID) {
2599 + res = ip_set_flush(index);
2602 + case IP_SET_OP_RENAME:{
2603 + struct ip_set_req_create *req_rename
2604 + = (struct ip_set_req_create *) data;
2606 + if (len != sizeof(struct ip_set_req_create)) {
2607 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2608 + sizeof(struct ip_set_req_create), len);
2613 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2614 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2616 + index = ip_set_find_byname(req_rename->name);
2617 + if (index == IP_SET_INVALID_ID) {
2621 + res = ip_set_rename(index, req_rename->typename);
2624 + case IP_SET_OP_SWAP:{
2625 + struct ip_set_req_create *req_swap
2626 + = (struct ip_set_req_create *) data;
2627 + ip_set_id_t to_index;
2629 + if (len != sizeof(struct ip_set_req_create)) {
2630 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2631 + sizeof(struct ip_set_req_create), len);
2636 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2637 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2639 + index = ip_set_find_byname(req_swap->name);
2640 + if (index == IP_SET_INVALID_ID) {
2644 + to_index = ip_set_find_byname(req_swap->typename);
2645 + if (to_index == IP_SET_INVALID_ID) {
2649 + res = ip_set_swap(index, to_index);
2653 + break; /* Set identified by id */
2656 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2657 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2661 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2663 + if (len < sizeof(struct ip_set_req_adt)) {
2664 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2665 + sizeof(struct ip_set_req_adt), len);
2669 + req_adt = (struct ip_set_req_adt *) data;
2671 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2672 + if (!(*op == IP_SET_OP_UNBIND_SET
2673 + && req_adt->index == IP_SET_INVALID_ID)) {
2674 + index = ip_set_find_byindex(req_adt->index);
2675 + if (index == IP_SET_INVALID_ID) {
2680 + res = adtfn(index, data, len);
2683 + up(&ip_set_app_mutex);
2687 + DP("final result %d", res);
2692 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2696 + ip_set_id_t index = IP_SET_INVALID_ID;
2698 + int copylen = *len;
2700 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2701 + if (!capable(CAP_NET_ADMIN))
2703 + if (optval != SO_IP_SET)
2705 + if (*len < sizeof(unsigned)) {
2706 + ip_set_printk("short userdata (want >=%zu, got %d)",
2707 + sizeof(unsigned), *len);
2710 + data = vmalloc(*len);
2712 + DP("out of mem for %d bytes", *len);
2715 + if (copy_from_user(data, user, *len) != 0) {
2719 + if (down_interruptible(&ip_set_app_mutex)) {
2724 + op = (unsigned *) data;
2727 + if (*op < IP_SET_OP_VERSION) {
2728 + /* Check the version at the beginning of operations */
2729 + struct ip_set_req_version *req_version =
2730 + (struct ip_set_req_version *) data;
2731 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2738 + case IP_SET_OP_VERSION: {
2739 + struct ip_set_req_version *req_version =
2740 + (struct ip_set_req_version *) data;
2742 + if (*len != sizeof(struct ip_set_req_version)) {
2743 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2744 + sizeof(struct ip_set_req_version),
2750 + req_version->version = IP_SET_PROTOCOL_VERSION;
2751 + res = copy_to_user(user, req_version,
2752 + sizeof(struct ip_set_req_version));
2755 + case IP_SET_OP_GET_BYNAME: {
2756 + struct ip_set_req_get_set *req_get
2757 + = (struct ip_set_req_get_set *) data;
2759 + if (*len != sizeof(struct ip_set_req_get_set)) {
2760 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2761 + sizeof(struct ip_set_req_get_set), *len);
2765 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2766 + index = ip_set_find_byname(req_get->set.name);
2767 + req_get->set.index = index;
2770 + case IP_SET_OP_GET_BYINDEX: {
2771 + struct ip_set_req_get_set *req_get
2772 + = (struct ip_set_req_get_set *) data;
2774 + if (*len != sizeof(struct ip_set_req_get_set)) {
2775 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2776 + sizeof(struct ip_set_req_get_set), *len);
2780 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2781 + index = ip_set_find_byindex(req_get->set.index);
2782 + strncpy(req_get->set.name,
2783 + index == IP_SET_INVALID_ID ? ""
2784 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2787 + case IP_SET_OP_ADT_GET: {
2788 + struct ip_set_req_adt_get *req_get
2789 + = (struct ip_set_req_adt_get *) data;
2791 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2792 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2793 + sizeof(struct ip_set_req_adt_get), *len);
2797 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2798 + index = ip_set_find_byname(req_get->set.name);
2799 + if (index != IP_SET_INVALID_ID) {
2800 + req_get->set.index = index;
2801 + strncpy(req_get->typename,
2802 + ip_set_list[index]->type->typename,
2803 + IP_SET_MAXNAMELEN - 1);
2810 + case IP_SET_OP_MAX_SETS: {
2811 + struct ip_set_req_max_sets *req_max_sets
2812 + = (struct ip_set_req_max_sets *) data;
2815 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2816 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2817 + sizeof(struct ip_set_req_max_sets), *len);
2822 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2823 + req_max_sets->set.index = IP_SET_INVALID_ID;
2825 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2826 + req_max_sets->set.index =
2827 + ip_set_find_byname(req_max_sets->set.name);
2828 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2833 + req_max_sets->max_sets = ip_set_max;
2834 + req_max_sets->sets = 0;
2835 + for (i = 0; i < ip_set_max; i++) {
2836 + if (ip_set_list[i] != NULL)
2837 + req_max_sets->sets++;
2841 + case IP_SET_OP_LIST_SIZE:
2842 + case IP_SET_OP_SAVE_SIZE: {
2843 + struct ip_set_req_setnames *req_setnames
2844 + = (struct ip_set_req_setnames *) data;
2845 + struct ip_set_name_list *name_list;
2846 + struct ip_set *set;
2850 + if (*len < sizeof(struct ip_set_req_setnames)) {
2851 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2852 + sizeof(struct ip_set_req_setnames), *len);
2857 + req_setnames->size = 0;
2858 + used = sizeof(struct ip_set_req_setnames);
2859 + for (i = 0; i < ip_set_max; i++) {
2860 + if (ip_set_list[i] == NULL)
2862 + name_list = (struct ip_set_name_list *)
2864 + used += sizeof(struct ip_set_name_list);
2865 + if (used > copylen) {
2869 + set = ip_set_list[i];
2870 + /* Fill in index, name, etc. */
2871 + name_list->index = i;
2872 + name_list->id = set->id;
2873 + strncpy(name_list->name,
2875 + IP_SET_MAXNAMELEN - 1);
2876 + strncpy(name_list->typename,
2877 + set->type->typename,
2878 + IP_SET_MAXNAMELEN - 1);
2879 + DP("filled %s of type %s, index %u\n",
2880 + name_list->name, name_list->typename,
2881 + name_list->index);
2882 + if (!(req_setnames->index == IP_SET_INVALID_ID
2883 + || req_setnames->index == i))
2887 + case IP_SET_OP_LIST_SIZE: {
2888 + req_setnames->size += sizeof(struct ip_set_list)
2889 + + set->type->header_size
2890 + + set->type->list_members_size(set);
2891 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2892 + i, &req_setnames->size);
2895 + case IP_SET_OP_SAVE_SIZE: {
2896 + req_setnames->size += sizeof(struct ip_set_save)
2897 + + set->type->header_size
2898 + + set->type->list_members_size(set);
2899 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2900 + i, &req_setnames->size);
2907 + if (copylen != used) {
2913 + case IP_SET_OP_LIST: {
2914 + struct ip_set_req_list *req_list
2915 + = (struct ip_set_req_list *) data;
2919 + if (*len < sizeof(struct ip_set_req_list)) {
2920 + ip_set_printk("short LIST (want >=%zu, got %d)",
2921 + sizeof(struct ip_set_req_list), *len);
2925 + index = req_list->index;
2926 + if (index != IP_SET_INVALID_ID
2927 + && ip_set_find_byindex(index) != index) {
2932 + if (index == IP_SET_INVALID_ID) {
2933 + /* List all sets */
2934 + for (i = 0; i < ip_set_max && res == 0; i++) {
2935 + if (ip_set_list[i] != NULL)
2936 + res = ip_set_list_set(i, data, &used, *len);
2939 + /* List an individual set */
2940 + res = ip_set_list_set(index, data, &used, *len);
2944 + else if (copylen != used) {
2950 + case IP_SET_OP_SAVE: {
2951 + struct ip_set_req_list *req_save
2952 + = (struct ip_set_req_list *) data;
2956 + if (*len < sizeof(struct ip_set_req_list)) {
2957 + ip_set_printk("short SAVE (want >=%zu, got %d)",
2958 + sizeof(struct ip_set_req_list), *len);
2962 + index = req_save->index;
2963 + if (index != IP_SET_INVALID_ID
2964 + && ip_set_find_byindex(index) != index) {
2969 + if (index == IP_SET_INVALID_ID) {
2970 + /* Save all sets */
2971 + for (i = 0; i < ip_set_max && res == 0; i++) {
2972 + if (ip_set_list[i] != NULL)
2973 + res = ip_set_save_set(i, data, &used, *len);
2976 + /* Save an individual set */
2977 + res = ip_set_save_set(index, data, &used, *len);
2980 + res = ip_set_save_bindings(index, data, &used, *len);
2984 + else if (copylen != used) {
2990 + case IP_SET_OP_RESTORE: {
2991 + struct ip_set_req_setnames *req_restore
2992 + = (struct ip_set_req_setnames *) data;
2995 + if (*len < sizeof(struct ip_set_req_setnames)
2996 + || *len != req_restore->size) {
2997 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
2998 + req_restore->size, *len);
3002 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3003 + req_restore->size - sizeof(struct ip_set_req_setnames));
3004 + DP("ip_set_restore: %u", line);
3007 + req_restore->size = line;
3008 + copylen = sizeof(struct ip_set_req_setnames);
3016 + } /* end of switch(op) */
3019 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3020 + && ip_set_list[index]
3021 + ? ip_set_list[index]->name
3022 + : ":all:", copylen);
3024 + res = copy_to_user(user, data, copylen);
3026 + copy_to_user(user, data, copylen);
3029 + up(&ip_set_app_mutex);
3033 + DP("final result %d", res);
3037 +static struct nf_sockopt_ops so_set = {
3039 + .set_optmin = SO_IP_SET,
3040 + .set_optmax = SO_IP_SET + 1,
3041 + .set = &ip_set_sockfn_set,
3042 + .get_optmin = SO_IP_SET,
3043 + .get_optmax = SO_IP_SET + 1,
3044 + .get = &ip_set_sockfn_get,
3048 +static int max_sets, hash_size;
3049 +MODULE_PARM(max_sets, "i");
3050 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3051 +MODULE_PARM(hash_size, "i");
3052 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3053 +MODULE_LICENSE("GPL");
3054 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3055 +MODULE_DESCRIPTION("module implementing core IP set support");
3057 +static int __init init(void)
3062 + get_random_bytes(&ip_set_hash_random, 4);
3064 + ip_set_max = max_sets;
3065 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3066 + if (!ip_set_list) {
3067 + printk(KERN_ERR "Unable to create ip_set_list\n");
3070 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3072 + ip_set_bindings_hash_size = hash_size;
3073 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3074 + if (!ip_set_hash) {
3075 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3076 + vfree(ip_set_list);
3079 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3080 + INIT_LIST_HEAD(&ip_set_hash[i]);
3082 + INIT_LIST_HEAD(&set_type_list);
3084 + res = nf_register_sockopt(&so_set);
3086 + ip_set_printk("SO_SET registry failed: %d", res);
3087 + vfree(ip_set_list);
3088 + vfree(ip_set_hash);
3094 +static void __exit fini(void)
3096 + /* There can't be any existing set or binding. Racy. */
3097 + nf_unregister_sockopt(&so_set);
3098 + vfree(ip_set_list);
3099 + vfree(ip_set_hash);
3100 + DP("these are the famous last words");
3103 +EXPORT_SYMBOL(ip_set_register_set_type);
3104 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3106 +EXPORT_SYMBOL(ip_set_get_byname);
3107 +EXPORT_SYMBOL(ip_set_get_byindex);
3108 +EXPORT_SYMBOL(ip_set_put);
3110 +EXPORT_SYMBOL(ip_set_addip_kernel);
3111 +EXPORT_SYMBOL(ip_set_delip_kernel);
3112 +EXPORT_SYMBOL(ip_set_testip_kernel);
3116 diff -Naur linux-old/net/ipv4/netfilter/ip_set_iphash.c linux-new/net/ipv4/netfilter/ip_set_iphash.c
3117 --- linux-old/net/ipv4/netfilter/ip_set_iphash.c 1970-01-01 01:00:00.000000000 +0100
3118 +++ linux-new/net/ipv4/netfilter/ip_set_iphash.c 2005-08-10 22:13:00.000000000 +0200
3120 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3122 + * This program is free software; you can redistribute it and/or modify
3123 + * it under the terms of the GNU General Public License version 2 as
3124 + * published by the Free Software Foundation.
3127 +/* Kernel module implementing an ip hash set */
3129 +#include <linux/module.h>
3130 +#include <linux/ip.h>
3131 +#include <linux/skbuff.h>
3132 +#include <linux/netfilter_ipv4/ip_tables.h>
3133 +#include <linux/netfilter_ipv4/ip_set.h>
3134 +#include <linux/errno.h>
3135 +#include <asm/uaccess.h>
3136 +#include <asm/bitops.h>
3137 +#include <asm/softirq.h>
3138 +#include <linux/spinlock.h>
3139 +#include <linux/vmalloc.h>
3140 +#include <linux/random.h>
3142 +#include <net/ip.h>
3144 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3145 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3146 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
3147 +#include <linux/netfilter_ipv4/ip_set_prime.h>
3149 +static inline __u32
3150 +jhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3152 + return jhash_1word(ip, map->initval);
3155 +static inline __u32
3156 +randhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3158 + return (1 + ip % map->prime);
3161 +static inline __u32
3162 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3164 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3165 + __u32 jhash, randhash, id;
3168 + *hash_ip = ip & map->netmask;
3169 + jhash = jhash_ip(map, *hash_ip);
3170 + randhash = randhash_ip(map, *hash_ip);
3171 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3172 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3174 + for (i = 0; i < map->probes; i++) {
3175 + id = (jhash + i * randhash) % map->hashsize;
3176 + DP("hash key: %u", id);
3177 + if (map->members[id] == *hash_ip)
3179 + /* No shortcut at testing - there can be deleted
3186 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3188 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
3192 +testip(struct ip_set *set, const void *data, size_t size,
3193 + ip_set_ip_t *hash_ip)
3195 + struct ip_set_req_iphash *req =
3196 + (struct ip_set_req_iphash *) data;
3198 + if (size != sizeof(struct ip_set_req_iphash)) {
3199 + ip_set_printk("data length wrong (want %zu, have %zu)",
3200 + sizeof(struct ip_set_req_iphash),
3204 + return __testip(set, req->ip, hash_ip);
3208 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
3209 + u_int32_t flags, ip_set_ip_t *hash_ip)
3211 + return __testip(set,
3212 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3213 + : skb->nh.iph->daddr),
3218 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3220 + __u32 jhash, randhash, probe;
3223 + *hash_ip = ip & map->netmask;
3224 + jhash = jhash_ip(map, *hash_ip);
3225 + randhash = randhash_ip(map, *hash_ip);
3227 + for (i = 0; i < map->probes; i++) {
3228 + probe = (jhash + i * randhash) % map->hashsize;
3229 + if (map->members[probe] == *hash_ip)
3231 + if (!map->members[probe]) {
3232 + map->members[probe] = *hash_ip;
3236 + /* Trigger rehashing */
3241 +addip(struct ip_set *set, const void *data, size_t size,
3242 + ip_set_ip_t *hash_ip)
3244 + struct ip_set_req_iphash *req =
3245 + (struct ip_set_req_iphash *) data;
3247 + if (size != sizeof(struct ip_set_req_iphash)) {
3248 + ip_set_printk("data length wrong (want %zu, have %zu)",
3249 + sizeof(struct ip_set_req_iphash),
3253 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3257 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3258 + u_int32_t flags, ip_set_ip_t *hash_ip)
3260 + return __addip((struct ip_set_iphash *) set->data,
3261 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3262 + : skb->nh.iph->daddr),
3266 +static int retry(struct ip_set *set)
3268 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3269 + ip_set_ip_t hash_ip, *members;
3270 + u_int32_t i, hashsize;
3271 + unsigned newbytes;
3273 + struct ip_set_iphash tmp = {
3274 + .hashsize = map->hashsize,
3275 + .probes = map->probes,
3276 + .resize = map->resize,
3277 + .netmask = map->netmask,
3280 + if (map->resize == 0)
3286 + /* Calculate new parameters */
3287 + get_random_bytes(&tmp.initval, 4);
3288 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
3289 + if (hashsize == tmp.hashsize)
3291 + tmp.prime = make_prime(hashsize);
3293 + ip_set_printk("rehashing of set %s triggered: "
3294 + "hashsize grows from %u to %u",
3295 + set->name, tmp.hashsize, hashsize);
3296 + tmp.hashsize = hashsize;
3298 + newbytes = hashsize * sizeof(ip_set_ip_t);
3299 + tmp.members = ip_set_malloc(newbytes);
3300 + if (!tmp.members) {
3301 + DP("out of memory for %d bytes", newbytes);
3304 + memset(tmp.members, 0, newbytes);
3306 + write_lock_bh(&set->lock);
3307 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3308 + for (i = 0; i < map->hashsize && res == 0; i++) {
3309 + if (map->members[i])
3310 + res = __addip(&tmp, map->members[i], &hash_ip);
3313 + /* Failure, try again */
3314 + write_unlock_bh(&set->lock);
3315 + ip_set_free(tmp.members, newbytes);
3319 + /* Success at resizing! */
3320 + members = map->members;
3321 + hashsize = map->hashsize;
3323 + map->initval = tmp.initval;
3324 + map->prime = tmp.prime;
3325 + map->hashsize = tmp.hashsize;
3326 + map->members = tmp.members;
3327 + write_unlock_bh(&set->lock);
3329 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
3335 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3337 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3338 + ip_set_ip_t id = hash_id(set, ip, hash_ip);
3340 + if (id == UINT_MAX)
3343 + map->members[id] = 0;
3348 +delip(struct ip_set *set, const void *data, size_t size,
3349 + ip_set_ip_t *hash_ip)
3351 + struct ip_set_req_iphash *req =
3352 + (struct ip_set_req_iphash *) data;
3354 + if (size != sizeof(struct ip_set_req_iphash)) {
3355 + ip_set_printk("data length wrong (want %zu, have %zu)",
3356 + sizeof(struct ip_set_req_iphash),
3360 + return __delip(set, req->ip, hash_ip);
3364 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3365 + u_int32_t flags, ip_set_ip_t *hash_ip)
3367 + return __delip(set,
3368 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3369 + : skb->nh.iph->daddr),
3373 +static int create(struct ip_set *set, const void *data, size_t size)
3375 + unsigned newbytes;
3376 + struct ip_set_req_iphash_create *req =
3377 + (struct ip_set_req_iphash_create *) data;
3378 + struct ip_set_iphash *map;
3380 + if (size != sizeof(struct ip_set_req_iphash_create)) {
3381 + ip_set_printk("data length wrong (want %zu, have %zu)",
3382 + sizeof(struct ip_set_req_iphash_create),
3387 + if (req->hashsize < 1) {
3388 + ip_set_printk("hashsize too small");
3392 + map = kmalloc(sizeof(struct ip_set_iphash), GFP_KERNEL);
3394 + DP("out of memory for %d bytes",
3395 + sizeof(struct ip_set_iphash));
3398 + get_random_bytes(&map->initval, 4);
3399 + map->prime = make_prime(req->hashsize);
3400 + map->hashsize = req->hashsize;
3401 + map->probes = req->probes;
3402 + map->resize = req->resize;
3403 + map->netmask = req->netmask;
3404 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
3405 + map->members = ip_set_malloc(newbytes);
3406 + if (!map->members) {
3407 + DP("out of memory for %d bytes", newbytes);
3411 + memset(map->members, 0, newbytes);
3417 +static void destroy(struct ip_set *set)
3419 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3421 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
3427 +static void flush(struct ip_set *set)
3429 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3430 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
3433 +static void list_header(const struct ip_set *set, void *data)
3435 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3436 + struct ip_set_req_iphash_create *header =
3437 + (struct ip_set_req_iphash_create *) data;
3439 + header->hashsize = map->hashsize;
3440 + header->probes = map->probes;
3441 + header->resize = map->resize;
3442 + header->netmask = map->netmask;
3445 +static int list_members_size(const struct ip_set *set)
3447 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3449 + return (map->hashsize * sizeof(ip_set_ip_t));
3452 +static void list_members(const struct ip_set *set, void *data)
3454 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3455 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
3457 + memcpy(data, map->members, bytes);
3460 +static struct ip_set_type ip_set_iphash = {
3461 + .typename = SETTYPE_NAME,
3462 + .typecode = IPSET_TYPE_IP,
3463 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3464 + .create = &create,
3465 + .destroy = &destroy,
3467 + .reqsize = sizeof(struct ip_set_req_iphash),
3469 + .addip_kernel = &addip_kernel,
3472 + .delip_kernel = &delip_kernel,
3473 + .testip = &testip,
3474 + .testip_kernel = &testip_kernel,
3475 + .header_size = sizeof(struct ip_set_req_iphash_create),
3476 + .list_header = &list_header,
3477 + .list_members_size = &list_members_size,
3478 + .list_members = &list_members,
3479 + .me = THIS_MODULE,
3482 +MODULE_LICENSE("GPL");
3483 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3484 +MODULE_DESCRIPTION("iphash type of IP sets");
3486 +static int __init init(void)
3488 + return ip_set_register_set_type(&ip_set_iphash);
3491 +static void __exit fini(void)
3493 + /* FIXME: possible race with ip_set_create() */
3494 + ip_set_unregister_set_type(&ip_set_iphash);
3499 diff -Naur linux-old/net/ipv4/netfilter/ip_set_ipmap.c linux-new/net/ipv4/netfilter/ip_set_ipmap.c
3500 --- linux-old/net/ipv4/netfilter/ip_set_ipmap.c 1970-01-01 01:00:00.000000000 +0100
3501 +++ linux-new/net/ipv4/netfilter/ip_set_ipmap.c 2005-08-10 22:13:00.000000000 +0200
3503 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3504 + * Patrick Schaaf <bof@bof.de>
3505 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3507 + * This program is free software; you can redistribute it and/or modify
3508 + * it under the terms of the GNU General Public License version 2 as
3509 + * published by the Free Software Foundation.
3512 +/* Kernel module implementing an IP set type: the single bitmap type */
3514 +#include <linux/module.h>
3515 +#include <linux/ip.h>
3516 +#include <linux/skbuff.h>
3517 +#include <linux/netfilter_ipv4/ip_tables.h>
3518 +#include <linux/netfilter_ipv4/ip_set.h>
3519 +#include <linux/errno.h>
3520 +#include <asm/uaccess.h>
3521 +#include <asm/bitops.h>
3522 +#include <asm/softirq.h>
3523 +#include <linux/spinlock.h>
3525 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3527 +static inline ip_set_ip_t
3528 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3530 + return (ip - map->first_ip)/map->hosts;
3534 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3536 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3538 + if (ip < map->first_ip || ip > map->last_ip)
3541 + *hash_ip = ip & map->netmask;
3542 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3543 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3544 + return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3548 +testip(struct ip_set *set, const void *data, size_t size,
3549 + ip_set_ip_t *hash_ip)
3551 + struct ip_set_req_ipmap *req =
3552 + (struct ip_set_req_ipmap *) data;
3554 + if (size != sizeof(struct ip_set_req_ipmap)) {
3555 + ip_set_printk("data length wrong (want %zu, have %zu)",
3556 + sizeof(struct ip_set_req_ipmap),
3560 + return __testip(set, req->ip, hash_ip);
3564 +testip_kernel(struct ip_set *set,
3565 + const struct sk_buff *skb,
3567 + ip_set_ip_t *hash_ip)
3571 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3572 + flags & IPSET_SRC ? "SRC" : "DST",
3573 + NIPQUAD(skb->nh.iph->saddr),
3574 + NIPQUAD(skb->nh.iph->daddr));
3576 + res = __testip(set,
3577 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3578 + : skb->nh.iph->daddr),
3580 + return (res < 0 ? 0 : res);
3584 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3586 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3588 + if (ip < map->first_ip || ip > map->last_ip)
3591 + *hash_ip = ip & map->netmask;
3592 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3593 + if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3600 +addip(struct ip_set *set, const void *data, size_t size,
3601 + ip_set_ip_t *hash_ip)
3603 + struct ip_set_req_ipmap *req =
3604 + (struct ip_set_req_ipmap *) data;
3606 + if (size != sizeof(struct ip_set_req_ipmap)) {
3607 + ip_set_printk("data length wrong (want %zu, have %zu)",
3608 + sizeof(struct ip_set_req_ipmap),
3612 + DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3613 + return __addip(set, req->ip, hash_ip);
3617 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3618 + u_int32_t flags, ip_set_ip_t *hash_ip)
3620 + return __addip(set,
3621 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3622 + : skb->nh.iph->daddr),
3627 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3629 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3631 + if (ip < map->first_ip || ip > map->last_ip)
3634 + *hash_ip = ip & map->netmask;
3635 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3636 + if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3643 +delip(struct ip_set *set, const void *data, size_t size,
3644 + ip_set_ip_t *hash_ip)
3646 + struct ip_set_req_ipmap *req =
3647 + (struct ip_set_req_ipmap *) data;
3649 + if (size != sizeof(struct ip_set_req_ipmap)) {
3650 + ip_set_printk("data length wrong (want %zu, have %zu)",
3651 + sizeof(struct ip_set_req_ipmap),
3655 + return __delip(set, req->ip, hash_ip);
3659 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3660 + u_int32_t flags, ip_set_ip_t *hash_ip)
3662 + return __delip(set,
3663 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3664 + : skb->nh.iph->daddr),
3668 +static int create(struct ip_set *set, const void *data, size_t size)
3671 + struct ip_set_req_ipmap_create *req =
3672 + (struct ip_set_req_ipmap_create *) data;
3673 + struct ip_set_ipmap *map;
3675 + if (size != sizeof(struct ip_set_req_ipmap_create)) {
3676 + ip_set_printk("data length wrong (want %zu, have %zu)",
3677 + sizeof(struct ip_set_req_ipmap_create),
3682 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3683 + HIPQUAD(req->from), HIPQUAD(req->to));
3685 + if (req->from > req->to) {
3686 + DP("bad ip range");
3690 + if (req->to - req->from > MAX_RANGE) {
3691 + ip_set_printk("range too big (max %d addresses)",
3696 + map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3698 + DP("out of memory for %d bytes",
3699 + sizeof(struct ip_set_ipmap));
3702 + map->first_ip = req->from;
3703 + map->last_ip = req->to;
3704 + map->netmask = req->netmask;
3706 + if (req->netmask == 0xFFFFFFFF) {
3708 + map->sizeid = map->last_ip - map->first_ip + 1;
3710 + unsigned int mask_bits, netmask_bits;
3713 + map->first_ip &= map->netmask; /* Should we better bark? */
3715 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3716 + netmask_bits = mask_to_bits(map->netmask);
3718 + if (!mask || netmask_bits <= mask_bits)
3721 + map->hosts = 2 << (32 - netmask_bits - 1);
3722 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3724 + newbytes = bitmap_bytes(0, map->sizeid - 1);
3725 + map->members = kmalloc(newbytes, GFP_KERNEL);
3726 + if (!map->members) {
3727 + DP("out of memory for %d bytes", newbytes);
3731 + memset(map->members, 0, newbytes);
3737 +static void destroy(struct ip_set *set)
3739 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3741 + kfree(map->members);
3747 +static void flush(struct ip_set *set)
3749 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3750 + memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3753 +static void list_header(const struct ip_set *set, void *data)
3755 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3756 + struct ip_set_req_ipmap_create *header =
3757 + (struct ip_set_req_ipmap_create *) data;
3759 + header->from = map->first_ip;
3760 + header->to = map->last_ip;
3761 + header->netmask = map->netmask;
3764 +static int list_members_size(const struct ip_set *set)
3766 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3768 + return bitmap_bytes(0, map->sizeid - 1);
3771 +static void list_members(const struct ip_set *set, void *data)
3773 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3774 + int bytes = bitmap_bytes(0, map->sizeid - 1);
3776 + memcpy(data, map->members, bytes);
3779 +static struct ip_set_type ip_set_ipmap = {
3780 + .typename = SETTYPE_NAME,
3781 + .typecode = IPSET_TYPE_IP,
3782 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3783 + .create = &create,
3784 + .destroy = &destroy,
3786 + .reqsize = sizeof(struct ip_set_req_ipmap),
3788 + .addip_kernel = &addip_kernel,
3790 + .delip_kernel = &delip_kernel,
3791 + .testip = &testip,
3792 + .testip_kernel = &testip_kernel,
3793 + .header_size = sizeof(struct ip_set_req_ipmap_create),
3794 + .list_header = &list_header,
3795 + .list_members_size = &list_members_size,
3796 + .list_members = &list_members,
3797 + .me = THIS_MODULE,
3800 +MODULE_LICENSE("GPL");
3801 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3802 +MODULE_DESCRIPTION("ipmap type of IP sets");
3804 +static int __init init(void)
3806 + return ip_set_register_set_type(&ip_set_ipmap);
3809 +static void __exit fini(void)
3811 + /* FIXME: possible race with ip_set_create() */
3812 + ip_set_unregister_set_type(&ip_set_ipmap);
3817 diff -Naur linux-old/net/ipv4/netfilter/ip_set_iptree.c linux-new/net/ipv4/netfilter/ip_set_iptree.c
3818 --- linux-old/net/ipv4/netfilter/ip_set_iptree.c 1970-01-01 01:00:00.000000000 +0100
3819 +++ linux-new/net/ipv4/netfilter/ip_set_iptree.c 2005-08-10 22:13:00.000000000 +0200
3821 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3823 + * This program is free software; you can redistribute it and/or modify
3824 + * it under the terms of the GNU General Public License version 2 as
3825 + * published by the Free Software Foundation.
3828 +/* Kernel module implementing an IP set type: the iptree type */
3830 +#include <linux/module.h>
3831 +#include <linux/ip.h>
3832 +#include <linux/skbuff.h>
3833 +#include <linux/slab.h>
3834 +#include <linux/delay.h>
3835 +#include <linux/netfilter_ipv4/ip_tables.h>
3836 +#include <linux/netfilter_ipv4/ip_set.h>
3837 +#include <linux/errno.h>
3838 +#include <asm/uaccess.h>
3839 +#include <asm/bitops.h>
3840 +#include <linux/interrupt.h>
3841 +#include <linux/spinlock.h>
3843 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
3845 +/* Garbage collection interval in seconds: */
3846 +#define IPTREE_GC_TIME 5*60
3847 +/* Sleep so many milliseconds before trying again
3848 + * to delete the gc timer at destroying a set */
3849 +#define IPTREE_DESTROY_SLEEP 100
3851 +static kmem_cache_t *branch_cachep;
3852 +static kmem_cache_t *leaf_cachep;
3854 +#define ABCD(a,b,c,d,addrp) do { \
3855 + a = ((unsigned char *)addrp)[3]; \
3856 + b = ((unsigned char *)addrp)[2]; \
3857 + c = ((unsigned char *)addrp)[1]; \
3858 + d = ((unsigned char *)addrp)[0]; \
3861 +#define TESTIP_WALK(map, elem, branch) do { \
3862 + if ((map)->tree[elem]) { \
3863 + branch = (map)->tree[elem]; \
3869 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3871 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3872 + struct ip_set_iptreeb *btree;
3873 + struct ip_set_iptreec *ctree;
3874 + struct ip_set_iptreed *dtree;
3875 + unsigned char a,b,c,d;
3878 + ABCD(a, b, c, d, hash_ip);
3879 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
3880 + TESTIP_WALK(map, a, btree);
3881 + TESTIP_WALK(btree, b, ctree);
3882 + TESTIP_WALK(ctree, c, dtree);
3883 + DP("%lu %lu", dtree->expires[d], jiffies);
3884 + return !!(map->timeout ? (time_after(dtree->expires[d], jiffies))
3885 + : dtree->expires[d]);
3889 +testip(struct ip_set *set, const void *data, size_t size,
3890 + ip_set_ip_t *hash_ip)
3892 + struct ip_set_req_iptree *req =
3893 + (struct ip_set_req_iptree *) data;
3895 + if (size != sizeof(struct ip_set_req_iptree)) {
3896 + ip_set_printk("data length wrong (want %zu, have %zu)",
3897 + sizeof(struct ip_set_req_iptree),
3901 + return __testip(set, req->ip, hash_ip);
3905 +testip_kernel(struct ip_set *set,
3906 + const struct sk_buff *skb,
3908 + ip_set_ip_t *hash_ip)
3912 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3913 + flags & IPSET_SRC ? "SRC" : "DST",
3914 + NIPQUAD(skb->nh.iph->saddr),
3915 + NIPQUAD(skb->nh.iph->daddr));
3917 + res = __testip(set,
3918 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3919 + : skb->nh.iph->daddr),
3921 + return (res < 0 ? 0 : res);
3924 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
3925 + if ((map)->tree[elem]) { \
3926 + DP("found %u", elem); \
3927 + branch = (map)->tree[elem]; \
3929 + branch = (type *) \
3930 + kmem_cache_alloc(cachep, GFP_KERNEL); \
3931 + if (branch == NULL) \
3933 + memset(branch, 0, sizeof(*branch)); \
3934 + (map)->tree[elem] = branch; \
3935 + DP("alloc %u", elem); \
3940 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
3941 + ip_set_ip_t *hash_ip)
3943 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3944 + struct ip_set_iptreeb *btree;
3945 + struct ip_set_iptreec *ctree;
3946 + struct ip_set_iptreed *dtree;
3947 + unsigned char a,b,c,d;
3951 + ABCD(a, b, c, d, hash_ip);
3952 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
3953 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
3954 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
3955 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
3956 + if (dtree->expires[d]
3957 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
3959 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
3960 + DP("%u %lu", d, dtree->expires[d]);
3965 +addip(struct ip_set *set, const void *data, size_t size,
3966 + ip_set_ip_t *hash_ip)
3968 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3969 + struct ip_set_req_iptree *req =
3970 + (struct ip_set_req_iptree *) data;
3972 + if (size != sizeof(struct ip_set_req_iptree)) {
3973 + ip_set_printk("data length wrong (want %zu, have %zu)",
3974 + sizeof(struct ip_set_req_iptree),
3978 + DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
3979 + return __addip(set, req->ip,
3980 + req->timeout ? req->timeout : map->timeout,
3985 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3986 + u_int32_t flags, ip_set_ip_t *hash_ip)
3988 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3990 + return __addip(set,
3991 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3992 + : skb->nh.iph->daddr),
3997 +#define DELIP_WALK(map, elem, branch) do { \
3998 + if ((map)->tree[elem]) { \
3999 + branch = (map)->tree[elem]; \
4005 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4007 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4008 + struct ip_set_iptreeb *btree;
4009 + struct ip_set_iptreec *ctree;
4010 + struct ip_set_iptreed *dtree;
4011 + unsigned char a,b,c,d;
4014 + ABCD(a, b, c, d, hash_ip);
4015 + DELIP_WALK(map, a, btree);
4016 + DELIP_WALK(btree, b, ctree);
4017 + DELIP_WALK(ctree, c, dtree);
4019 + if (dtree->expires[d]) {
4020 + dtree->expires[d] = 0;
4027 +delip(struct ip_set *set, const void *data, size_t size,
4028 + ip_set_ip_t *hash_ip)
4030 + struct ip_set_req_iptree *req =
4031 + (struct ip_set_req_iptree *) data;
4033 + if (size != sizeof(struct ip_set_req_iptree)) {
4034 + ip_set_printk("data length wrong (want %zu, have %zu)",
4035 + sizeof(struct ip_set_req_iptree),
4039 + return __delip(set, req->ip, hash_ip);
4043 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4044 + u_int32_t flags, ip_set_ip_t *hash_ip)
4046 + return __delip(set,
4047 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4048 + : skb->nh.iph->daddr),
4052 +#define LOOP_WALK_BEGIN(map, i, branch) \
4053 + for (i = 0; i < 255; i++) { \
4054 + if (!(map)->tree[i]) \
4056 + branch = (map)->tree[i]
4058 +#define LOOP_WALK_END }
4060 +static void ip_tree_gc(unsigned long ul_set)
4062 + struct ip_set *set = (void *) ul_set;
4063 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4064 + struct ip_set_iptreeb *btree;
4065 + struct ip_set_iptreec *ctree;
4066 + struct ip_set_iptreed *dtree;
4067 + unsigned char a,b,c,d;
4068 + unsigned char i,j,k;
4071 + DP("gc: %s", set->name);
4072 + write_lock_bh(&set->lock);
4073 + LOOP_WALK_BEGIN(map, a, btree);
4074 + LOOP_WALK_BEGIN(btree, b, ctree);
4075 + LOOP_WALK_BEGIN(ctree, c, dtree);
4076 + for (d = 0; d < 255; d++) {
4077 + if (dtree->expires[d]) {
4078 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4080 + dtree->expires[d], jiffies);
4082 + && time_before(dtree->expires[d], jiffies))
4083 + dtree->expires[d] = 0;
4089 + DP("gc: %s: leaf %u %u %u empty",
4090 + set->name, a, b, c);
4091 + kmem_cache_free(leaf_cachep, dtree);
4092 + ctree->tree[c] = NULL;
4094 + DP("gc: %s: leaf %u %u %u not empty",
4095 + set->name, a, b, c);
4101 + DP("gc: %s: branch %u %u empty",
4103 + kmem_cache_free(branch_cachep, ctree);
4104 + btree->tree[b] = NULL;
4106 + DP("gc: %s: branch %u %u not empty",
4113 + DP("gc: %s: branch %u empty",
4115 + kmem_cache_free(branch_cachep, btree);
4116 + map->tree[a] = NULL;
4118 + DP("gc: %s: branch %u not empty",
4123 + write_unlock_bh(&set->lock);
4125 + map->gc.expires = jiffies + map->gc_interval * HZ;
4126 + add_timer(&map->gc);
4129 +static int create(struct ip_set *set, const void *data, size_t size)
4131 + struct ip_set_req_iptree_create *req =
4132 + (struct ip_set_req_iptree_create *) data;
4133 + struct ip_set_iptree *map;
4135 + if (size != sizeof(struct ip_set_req_iptree_create)) {
4136 + ip_set_printk("data length wrong (want %zu, have %zu)",
4137 + sizeof(struct ip_set_req_iptree_create),
4142 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4144 + DP("out of memory for %d bytes",
4145 + sizeof(struct ip_set_iptree));
4148 + memset(map, 0, sizeof(*map));
4149 + map->timeout = req->timeout;
4152 + /* If there is no timeout for the entries,
4153 + * we still have to call gc because delete
4154 + * do not clean up empty branches */
4155 + map->gc_interval = IPTREE_GC_TIME;
4156 + init_timer(&map->gc);
4157 + map->gc.data = (unsigned long) set;
4158 + map->gc.function = ip_tree_gc;
4159 + map->gc.expires = jiffies + map->gc_interval * HZ;
4160 + add_timer(&map->gc);
4165 +static void __flush(struct ip_set_iptree *map)
4167 + struct ip_set_iptreeb *btree;
4168 + struct ip_set_iptreec *ctree;
4169 + struct ip_set_iptreed *dtree;
4170 + unsigned int a,b,c;
4172 + LOOP_WALK_BEGIN(map, a, btree);
4173 + LOOP_WALK_BEGIN(btree, b, ctree);
4174 + LOOP_WALK_BEGIN(ctree, c, dtree);
4175 + kmem_cache_free(leaf_cachep, dtree);
4177 + kmem_cache_free(branch_cachep, ctree);
4179 + kmem_cache_free(branch_cachep, btree);
4183 +static void destroy(struct ip_set *set)
4185 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4187 + while (!del_timer(&map->gc))
4188 + msleep(IPTREE_DESTROY_SLEEP);
4194 +static void flush(struct ip_set *set)
4196 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4197 + unsigned int timeout = map->timeout;
4200 + memset(map, 0, sizeof(*map));
4201 + map->timeout = timeout;
4204 +static void list_header(const struct ip_set *set, void *data)
4206 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4207 + struct ip_set_req_iptree_create *header =
4208 + (struct ip_set_req_iptree_create *) data;
4210 + header->timeout = map->timeout;
4213 +static int list_members_size(const struct ip_set *set)
4215 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4216 + struct ip_set_iptreeb *btree;
4217 + struct ip_set_iptreec *ctree;
4218 + struct ip_set_iptreed *dtree;
4219 + unsigned char a,b,c,d;
4220 + unsigned int count = 0;
4222 + LOOP_WALK_BEGIN(map, a, btree);
4223 + LOOP_WALK_BEGIN(btree, b, ctree);
4224 + LOOP_WALK_BEGIN(ctree, c, dtree);
4225 + for (d = 0; d < 255; d++) {
4226 + if (dtree->expires[d]
4227 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4234 + DP("members %u", count);
4235 + return (count * sizeof(struct ip_set_req_iptree));
4238 +static void list_members(const struct ip_set *set, void *data)
4240 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4241 + struct ip_set_iptreeb *btree;
4242 + struct ip_set_iptreec *ctree;
4243 + struct ip_set_iptreed *dtree;
4244 + unsigned char a,b,c,d;
4245 + size_t offset = 0;
4246 + struct ip_set_req_iptree *entry;
4248 + LOOP_WALK_BEGIN(map, a, btree);
4249 + LOOP_WALK_BEGIN(btree, b, ctree);
4250 + LOOP_WALK_BEGIN(ctree, c, dtree);
4251 + for (d = 0; d < 255; d++) {
4252 + if (dtree->expires[d]
4253 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
4254 + entry = (struct ip_set_req_iptree *)(data + offset);
4255 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
4256 + entry->timeout = !map->timeout ? 0
4257 + : (dtree->expires[d] - jiffies)/HZ;
4258 + offset += sizeof(struct ip_set_req_iptree);
4266 +static struct ip_set_type ip_set_iptree = {
4267 + .typename = SETTYPE_NAME,
4268 + .typecode = IPSET_TYPE_IP,
4269 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4270 + .create = &create,
4271 + .destroy = &destroy,
4273 + .reqsize = sizeof(struct ip_set_req_iptree),
4275 + .addip_kernel = &addip_kernel,
4277 + .delip_kernel = &delip_kernel,
4278 + .testip = &testip,
4279 + .testip_kernel = &testip_kernel,
4280 + .header_size = sizeof(struct ip_set_req_iptree_create),
4281 + .list_header = &list_header,
4282 + .list_members_size = &list_members_size,
4283 + .list_members = &list_members,
4284 + .me = THIS_MODULE,
4287 +MODULE_LICENSE("GPL");
4288 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4289 +MODULE_DESCRIPTION("iptree type of IP sets");
4291 +static int __init init(void)
4295 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
4296 + sizeof(struct ip_set_iptreeb),
4297 + 0, 0, NULL, NULL);
4298 + if (!branch_cachep) {
4299 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
4303 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
4304 + sizeof(struct ip_set_iptreed),
4305 + 0, 0, NULL, NULL);
4306 + if (!leaf_cachep) {
4307 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
4311 + ret = ip_set_register_set_type(&ip_set_iptree);
4315 + kmem_cache_destroy(leaf_cachep);
4317 + kmem_cache_destroy(branch_cachep);
4322 +static void __exit fini(void)
4324 + /* FIXME: possible race with ip_set_create() */
4325 + ip_set_unregister_set_type(&ip_set_iptree);
4326 + kmem_cache_destroy(leaf_cachep);
4327 + kmem_cache_destroy(branch_cachep);
4332 diff -Naur linux-old/net/ipv4/netfilter/ip_set_macipmap.c linux-new/net/ipv4/netfilter/ip_set_macipmap.c
4333 --- linux-old/net/ipv4/netfilter/ip_set_macipmap.c 1970-01-01 01:00:00.000000000 +0100
4334 +++ linux-new/net/ipv4/netfilter/ip_set_macipmap.c 2005-08-10 22:13:00.000000000 +0200
4336 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
4337 + * Patrick Schaaf <bof@bof.de>
4338 + * Martin Josefsson <gandalf@wlug.westbo.se>
4339 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4341 + * This program is free software; you can redistribute it and/or modify
4342 + * it under the terms of the GNU General Public License version 2 as
4343 + * published by the Free Software Foundation.
4346 +/* Kernel module implementing an IP set type: the macipmap type */
4348 +#include <linux/module.h>
4349 +#include <linux/ip.h>
4350 +#include <linux/skbuff.h>
4351 +#include <linux/netfilter_ipv4/ip_tables.h>
4352 +#include <linux/netfilter_ipv4/ip_set.h>
4353 +#include <linux/errno.h>
4354 +#include <asm/uaccess.h>
4355 +#include <asm/bitops.h>
4356 +#include <asm/softirq.h>
4357 +#include <linux/spinlock.h>
4358 +#include <linux/if_ether.h>
4359 +#include <linux/vmalloc.h>
4361 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4362 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
4365 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
4367 + struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
4368 + struct ip_set_macip *table = (struct ip_set_macip *) map->members;
4369 + struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
4371 + if (size != sizeof(struct ip_set_req_macipmap)) {
4372 + ip_set_printk("data length wrong (want %zu, have %zu)",
4373 + sizeof(struct ip_set_req_macipmap),
4378 + if (req->ip < map->first_ip || req->ip > map->last_ip)
4381 + *hash_ip = req->ip;
4382 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4383 + set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
4384 + if (test_bit(IPSET_MACIP_ISSET,
4385 + &table[req->ip - map->first_ip].flags)) {
4386 + return (memcmp(req->ethernet,
4387 + &table[req->ip - map->first_ip].ethernet,
4390 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4395 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4396 + u_int32_t flags, ip_set_ip_t *hash_ip)
4398 + struct ip_set_macipmap *map =
4399 + (struct ip_set_macipmap *) set->data;
4400 + struct ip_set_macip *table =
4401 + (struct ip_set_macip *) map->members;
4404 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4405 + : skb->nh.iph->daddr);
4406 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4407 + flags & IPSET_SRC ? "SRC" : "DST",
4408 + NIPQUAD(skb->nh.iph->saddr),
4409 + NIPQUAD(skb->nh.iph->daddr));
4411 + if (ip < map->first_ip || ip > map->last_ip)
4415 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4416 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
4417 + if (test_bit(IPSET_MACIP_ISSET, &table[ip - map->first_ip].flags)) {
4418 + /* Is mac pointer valid?
4419 + * If so, compare... */
4420 + return (skb->mac.raw >= skb->head
4421 + && (skb->mac.raw + ETH_HLEN) <= skb->data
4422 + && (memcmp(skb->mac.ethernet->h_source,
4423 + &table[ip - map->first_ip].ethernet,
4426 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4430 +/* returns 0 on success */
4432 +__addip(struct ip_set *set,
4433 + ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
4435 + struct ip_set_macipmap *map =
4436 + (struct ip_set_macipmap *) set->data;
4437 + struct ip_set_macip *table =
4438 + (struct ip_set_macip *) map->members;
4440 + if (ip < map->first_ip || ip > map->last_ip)
4442 + if (test_and_set_bit(IPSET_MACIP_ISSET,
4443 + &table[ip - map->first_ip].flags))
4447 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4448 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
4453 +addip(struct ip_set *set, const void *data, size_t size,
4454 + ip_set_ip_t *hash_ip)
4456 + struct ip_set_req_macipmap *req =
4457 + (struct ip_set_req_macipmap *) data;
4459 + if (size != sizeof(struct ip_set_req_macipmap)) {
4460 + ip_set_printk("data length wrong (want %zu, have %zu)",
4461 + sizeof(struct ip_set_req_macipmap),
4465 + return __addip(set, req->ip, req->ethernet, hash_ip);
4469 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4470 + u_int32_t flags, ip_set_ip_t *hash_ip)
4474 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4475 + : skb->nh.iph->daddr);
4477 + if (!(skb->mac.raw >= skb->head
4478 + && (skb->mac.raw + ETH_HLEN) <= skb->data))
4481 + return __addip(set, ip, skb->mac.ethernet->h_source, hash_ip);
4485 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4487 + struct ip_set_macipmap *map =
4488 + (struct ip_set_macipmap *) set->data;
4489 + struct ip_set_macip *table =
4490 + (struct ip_set_macip *) map->members;
4492 + if (ip < map->first_ip || ip > map->last_ip)
4494 + if (!test_and_clear_bit(IPSET_MACIP_ISSET,
4495 + &table[ip - map->first_ip].flags))
4499 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4504 +delip(struct ip_set *set, const void *data, size_t size,
4505 + ip_set_ip_t *hash_ip)
4507 + struct ip_set_req_macipmap *req =
4508 + (struct ip_set_req_macipmap *) data;
4510 + if (size != sizeof(struct ip_set_req_macipmap)) {
4511 + ip_set_printk("data length wrong (want %zu, have %zu)",
4512 + sizeof(struct ip_set_req_macipmap),
4516 + return __delip(set, req->ip, hash_ip);
4520 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4521 + u_int32_t flags, ip_set_ip_t *hash_ip)
4523 + return __delip(set,
4524 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4525 + : skb->nh.iph->daddr),
4529 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
4531 + return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
4534 +static int create(struct ip_set *set, const void *data, size_t size)
4537 + struct ip_set_req_macipmap_create *req =
4538 + (struct ip_set_req_macipmap_create *) data;
4539 + struct ip_set_macipmap *map;
4541 + if (size != sizeof(struct ip_set_req_macipmap_create)) {
4542 + ip_set_printk("data length wrong (want %zu, have %zu)",
4543 + sizeof(struct ip_set_req_macipmap_create),
4548 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
4549 + HIPQUAD(req->from), HIPQUAD(req->to));
4551 + if (req->from > req->to) {
4552 + DP("bad ip range");
4556 + if (req->to - req->from > MAX_RANGE) {
4557 + ip_set_printk("range too big (max %d addresses)",
4562 + map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
4564 + DP("out of memory for %d bytes",
4565 + sizeof(struct ip_set_macipmap));
4568 + map->flags = req->flags;
4569 + map->first_ip = req->from;
4570 + map->last_ip = req->to;
4571 + newbytes = members_size(map->first_ip, map->last_ip);
4572 + map->members = ip_set_malloc(newbytes);
4573 + DP("members: %u %p", newbytes, map->members);
4574 + if (!map->members) {
4575 + DP("out of memory for %d bytes", newbytes);
4579 + memset(map->members, 0, newbytes);
4585 +static void destroy(struct ip_set *set)
4587 + struct ip_set_macipmap *map =
4588 + (struct ip_set_macipmap *) set->data;
4590 + ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
4596 +static void flush(struct ip_set *set)
4598 + struct ip_set_macipmap *map =
4599 + (struct ip_set_macipmap *) set->data;
4600 + memset(map->members, 0, members_size(map->first_ip, map->last_ip));
4603 +static void list_header(const struct ip_set *set, void *data)
4605 + struct ip_set_macipmap *map =
4606 + (struct ip_set_macipmap *) set->data;
4607 + struct ip_set_req_macipmap_create *header =
4608 + (struct ip_set_req_macipmap_create *) data;
4610 + DP("list_header %x %x %u", map->first_ip, map->last_ip,
4613 + header->from = map->first_ip;
4614 + header->to = map->last_ip;
4615 + header->flags = map->flags;
4618 +static int list_members_size(const struct ip_set *set)
4620 + struct ip_set_macipmap *map =
4621 + (struct ip_set_macipmap *) set->data;
4623 + DP("%u", members_size(map->first_ip, map->last_ip));
4624 + return members_size(map->first_ip, map->last_ip);
4627 +static void list_members(const struct ip_set *set, void *data)
4629 + struct ip_set_macipmap *map =
4630 + (struct ip_set_macipmap *) set->data;
4632 + int bytes = members_size(map->first_ip, map->last_ip);
4634 + DP("members: %u %p", bytes, map->members);
4635 + memcpy(data, map->members, bytes);
4638 +static struct ip_set_type ip_set_macipmap = {
4639 + .typename = SETTYPE_NAME,
4640 + .typecode = IPSET_TYPE_IP,
4641 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4642 + .create = &create,
4643 + .destroy = &destroy,
4645 + .reqsize = sizeof(struct ip_set_req_macipmap),
4647 + .addip_kernel = &addip_kernel,
4649 + .delip_kernel = &delip_kernel,
4650 + .testip = &testip,
4651 + .testip_kernel = &testip_kernel,
4652 + .header_size = sizeof(struct ip_set_req_macipmap_create),
4653 + .list_header = &list_header,
4654 + .list_members_size = &list_members_size,
4655 + .list_members = &list_members,
4656 + .me = THIS_MODULE,
4659 +MODULE_LICENSE("GPL");
4660 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4661 +MODULE_DESCRIPTION("macipmap type of IP sets");
4663 +static int __init init(void)
4665 + return ip_set_register_set_type(&ip_set_macipmap);
4668 +static void __exit fini(void)
4670 + /* FIXME: possible race with ip_set_create() */
4671 + ip_set_unregister_set_type(&ip_set_macipmap);
4676 diff -Naur linux-old/net/ipv4/netfilter/ip_set_nethash.c linux-new/net/ipv4/netfilter/ip_set_nethash.c
4677 --- linux-old/net/ipv4/netfilter/ip_set_nethash.c 1970-01-01 01:00:00.000000000 +0100
4678 +++ linux-new/net/ipv4/netfilter/ip_set_nethash.c 2005-08-10 22:13:00.000000000 +0200
4680 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4682 + * This program is free software; you can redistribute it and/or modify
4683 + * it under the terms of the GNU General Public License version 2 as
4684 + * published by the Free Software Foundation.
4687 +/* Kernel module implementing a cidr nethash set */
4689 +#include <linux/module.h>
4690 +#include <linux/ip.h>
4691 +#include <linux/skbuff.h>
4692 +#include <linux/netfilter_ipv4/ip_tables.h>
4693 +#include <linux/netfilter_ipv4/ip_set.h>
4694 +#include <linux/errno.h>
4695 +#include <asm/uaccess.h>
4696 +#include <asm/bitops.h>
4697 +#include <asm/softirq.h>
4698 +#include <linux/spinlock.h>
4699 +#include <linux/vmalloc.h>
4700 +#include <linux/random.h>
4702 +#include <net/ip.h>
4704 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4705 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
4706 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
4707 +#include <linux/netfilter_ipv4/ip_set_prime.h>
4709 +static inline __u32
4710 +jhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4712 + return jhash_1word(ip, map->initval);
4715 +static inline __u32
4716 +randhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4718 + return (1 + ip % map->prime);
4721 +static inline __u32
4722 +hash_id_cidr(struct ip_set_nethash *map,
4724 + unsigned char cidr,
4725 + ip_set_ip_t *hash_ip)
4727 + __u32 jhash, randhash, id;
4730 + *hash_ip = pack(ip, cidr);
4731 + jhash = jhash_ip(map, *hash_ip);
4732 + randhash = randhash_ip(map, *hash_ip);
4734 + for (i = 0; i < map->probes; i++) {
4735 + id = (jhash + i * randhash) % map->hashsize;
4736 + DP("hash key: %u", id);
4737 + if (map->members[id] == *hash_ip)
4743 +static inline __u32
4744 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4746 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4747 + __u32 id = UINT_MAX;
4750 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4751 + id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
4752 + if (id != UINT_MAX)
4759 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
4760 + ip_set_ip_t *hash_ip)
4762 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4764 + return (hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
4768 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4770 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
4774 +testip(struct ip_set *set, const void *data, size_t size,
4775 + ip_set_ip_t *hash_ip)
4777 + struct ip_set_req_nethash *req =
4778 + (struct ip_set_req_nethash *) data;
4780 + if (size != sizeof(struct ip_set_req_nethash)) {
4781 + ip_set_printk("data length wrong (want %zu, have %zu)",
4782 + sizeof(struct ip_set_req_nethash),
4786 + return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
4787 + : __testip_cidr(set, req->ip, req->cidr, hash_ip));
4791 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4792 + u_int32_t flags, ip_set_ip_t *hash_ip)
4794 + return __testip(set,
4795 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4796 + : skb->nh.iph->daddr),
4801 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
4803 + __u32 jhash, randhash, probe;
4806 + jhash = jhash_ip(map, ip);
4807 + randhash = randhash_ip(map, ip);
4809 + for (i = 0; i < map->probes; i++) {
4810 + probe = (jhash + i * randhash) % map->hashsize;
4811 + if (map->members[probe] == ip)
4813 + if (!map->members[probe]) {
4814 + map->members[probe] = ip;
4818 + /* Trigger rehashing */
4823 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4824 + ip_set_ip_t *hash_ip)
4826 + *hash_ip = pack(ip, cidr);
4827 + DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
4829 + return __addip_base(map, *hash_ip);
4833 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
4835 + unsigned char next;
4838 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4839 + if (map->cidr[i] == cidr) {
4841 + } else if (map->cidr[i] < cidr) {
4842 + next = map->cidr[i];
4843 + map->cidr[i] = cidr;
4848 + map->cidr[i] = cidr;
4852 +addip(struct ip_set *set, const void *data, size_t size,
4853 + ip_set_ip_t *hash_ip)
4855 + struct ip_set_req_nethash *req =
4856 + (struct ip_set_req_nethash *) data;
4859 + if (size != sizeof(struct ip_set_req_nethash)) {
4860 + ip_set_printk("data length wrong (want %zu, have %zu)",
4861 + sizeof(struct ip_set_req_nethash),
4865 + ret = __addip((struct ip_set_nethash *) set->data,
4866 + req->ip, req->cidr, hash_ip);
4869 + update_cidr_sizes((struct ip_set_nethash *) set->data,
4876 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4877 + u_int32_t flags, ip_set_ip_t *hash_ip)
4879 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4880 + int ret = -ERANGE;
4881 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4882 + : skb->nh.iph->daddr);
4885 + ret = __addip(map, ip, map->cidr[0], hash_ip);
4890 +static int retry(struct ip_set *set)
4892 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4893 + ip_set_ip_t *members;
4894 + u_int32_t i, hashsize;
4895 + unsigned newbytes;
4897 + struct ip_set_nethash tmp = {
4898 + .hashsize = map->hashsize,
4899 + .probes = map->probes,
4900 + .resize = map->resize
4903 + if (map->resize == 0)
4906 + memcpy(tmp.cidr, map->cidr, 30 * sizeof(unsigned char));
4910 + /* Calculate new parameters */
4911 + get_random_bytes(&tmp.initval, 4);
4912 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
4913 + if (hashsize == tmp.hashsize)
4915 + tmp.prime = make_prime(hashsize);
4917 + ip_set_printk("rehashing of set %s triggered: "
4918 + "hashsize grows from %u to %u",
4919 + set->name, tmp.hashsize, hashsize);
4920 + tmp.hashsize = hashsize;
4922 + newbytes = hashsize * sizeof(ip_set_ip_t);
4923 + tmp.members = ip_set_malloc(newbytes);
4924 + if (!tmp.members) {
4925 + DP("out of memory for %d bytes", newbytes);
4928 + memset(tmp.members, 0, newbytes);
4930 + write_lock_bh(&set->lock);
4931 + map = (struct ip_set_nethash *) set->data; /* Play safe */
4932 + for (i = 0; i < map->hashsize && res == 0; i++) {
4933 + if (map->members[i])
4934 + res = __addip_base(&tmp, map->members[i]);
4937 + /* Failure, try again */
4938 + write_unlock_bh(&set->lock);
4939 + ip_set_free(tmp.members, newbytes);
4943 + /* Success at resizing! */
4944 + members = map->members;
4945 + hashsize = map->hashsize;
4947 + map->initval = tmp.initval;
4948 + map->prime = tmp.prime;
4949 + map->hashsize = tmp.hashsize;
4950 + map->members = tmp.members;
4951 + write_unlock_bh(&set->lock);
4953 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
4959 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4960 + ip_set_ip_t *hash_ip)
4962 + ip_set_ip_t id = hash_id_cidr(map, ip, cidr, hash_ip);
4964 + if (id == UINT_MAX)
4967 + map->members[id] = 0;
4972 +delip(struct ip_set *set, const void *data, size_t size,
4973 + ip_set_ip_t *hash_ip)
4975 + struct ip_set_req_nethash *req =
4976 + (struct ip_set_req_nethash *) data;
4978 + if (size != sizeof(struct ip_set_req_nethash)) {
4979 + ip_set_printk("data length wrong (want %zu, have %zu)",
4980 + sizeof(struct ip_set_req_nethash),
4984 + /* TODO: no garbage collection in map->cidr */
4985 + return __delip((struct ip_set_nethash *) set->data,
4986 + req->ip, req->cidr, hash_ip);
4990 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4991 + u_int32_t flags, ip_set_ip_t *hash_ip)
4993 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4994 + int ret = -ERANGE;
4995 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4996 + : skb->nh.iph->daddr);
4999 + ret = __delip(map, ip, map->cidr[0], hash_ip);
5004 +static int create(struct ip_set *set, const void *data, size_t size)
5006 + unsigned newbytes;
5007 + struct ip_set_req_nethash_create *req =
5008 + (struct ip_set_req_nethash_create *) data;
5009 + struct ip_set_nethash *map;
5011 + if (size != sizeof(struct ip_set_req_nethash_create)) {
5012 + ip_set_printk("data length wrong (want %zu, have %zu)",
5013 + sizeof(struct ip_set_req_nethash_create),
5018 + if (req->hashsize < 1) {
5019 + ip_set_printk("hashsize too small");
5023 + map = kmalloc(sizeof(struct ip_set_nethash), GFP_KERNEL);
5025 + DP("out of memory for %d bytes",
5026 + sizeof(struct ip_set_nethash));
5029 + get_random_bytes(&map->initval, 4);
5030 + map->prime = make_prime(req->hashsize);
5031 + map->hashsize = req->hashsize;
5032 + map->probes = req->probes;
5033 + map->resize = req->resize;
5034 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
5035 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
5036 + map->members = ip_set_malloc(newbytes);
5037 + if (!map->members) {
5038 + DP("out of memory for %d bytes", newbytes);
5042 + memset(map->members, 0, newbytes);
5048 +static void destroy(struct ip_set *set)
5050 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5052 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
5058 +static void flush(struct ip_set *set)
5060 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5061 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
5062 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
5065 +static void list_header(const struct ip_set *set, void *data)
5067 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5068 + struct ip_set_req_nethash_create *header =
5069 + (struct ip_set_req_nethash_create *) data;
5071 + header->hashsize = map->hashsize;
5072 + header->probes = map->probes;
5073 + header->resize = map->resize;
5076 +static int list_members_size(const struct ip_set *set)
5078 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5080 + return (map->hashsize * sizeof(ip_set_ip_t));
5083 +static void list_members(const struct ip_set *set, void *data)
5085 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5086 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
5088 + memcpy(data, map->members, bytes);
5091 +static struct ip_set_type ip_set_nethash = {
5092 + .typename = SETTYPE_NAME,
5093 + .typecode = IPSET_TYPE_IP,
5094 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5095 + .create = &create,
5096 + .destroy = &destroy,
5098 + .reqsize = sizeof(struct ip_set_req_nethash),
5100 + .addip_kernel = &addip_kernel,
5103 + .delip_kernel = &delip_kernel,
5104 + .testip = &testip,
5105 + .testip_kernel = &testip_kernel,
5106 + .header_size = sizeof(struct ip_set_req_nethash_create),
5107 + .list_header = &list_header,
5108 + .list_members_size = &list_members_size,
5109 + .list_members = &list_members,
5110 + .me = THIS_MODULE,
5113 +MODULE_LICENSE("GPL");
5114 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5115 +MODULE_DESCRIPTION("nethash type of IP sets");
5117 +static int __init init(void)
5119 + return ip_set_register_set_type(&ip_set_nethash);
5122 +static void __exit fini(void)
5124 + /* FIXME: possible race with ip_set_create() */
5125 + ip_set_unregister_set_type(&ip_set_nethash);
5130 diff -Naur linux-old/net/ipv4/netfilter/ip_set_portmap.c linux-new/net/ipv4/netfilter/ip_set_portmap.c
5131 --- linux-old/net/ipv4/netfilter/ip_set_portmap.c 1970-01-01 01:00:00.000000000 +0100
5132 +++ linux-new/net/ipv4/netfilter/ip_set_portmap.c 2005-08-10 22:13:00.000000000 +0200
5134 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5136 + * This program is free software; you can redistribute it and/or modify
5137 + * it under the terms of the GNU General Public License version 2 as
5138 + * published by the Free Software Foundation.
5141 +/* Kernel module implementing a port set type as a bitmap */
5143 +#include <linux/module.h>
5144 +#include <linux/ip.h>
5145 +#include <linux/tcp.h>
5146 +#include <linux/udp.h>
5147 +#include <linux/skbuff.h>
5148 +#include <linux/netfilter_ipv4/ip_tables.h>
5149 +#include <linux/netfilter_ipv4/ip_set.h>
5150 +#include <linux/errno.h>
5151 +#include <asm/uaccess.h>
5152 +#include <asm/bitops.h>
5153 +#include <asm/softirq.h>
5154 +#include <linux/spinlock.h>
5156 +#include <net/ip.h>
5158 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
5160 +static inline ip_set_ip_t
5161 +get_port(const struct sk_buff *skb, u_int32_t flags)
5163 + struct iphdr *iph = skb->nh.iph;
5164 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
5166 + switch (iph->protocol) {
5167 + case IPPROTO_TCP: {
5168 + struct tcphdr *tcph = (struct tcphdr *)((u_int32_t *)iph + iph->ihl);
5170 + /* See comments at tcp_match in ip_tables.c */
5173 + && (skb->len - iph->ihl * 4) < sizeof(struct tcphdr)))
5174 + return INVALID_PORT;
5176 + return ntohs(flags & IPSET_SRC ?
5177 + tcph->source : tcph->dest);
5179 + case IPPROTO_UDP: {
5180 + struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
5184 + && (skb->len - iph->ihl * 4) < sizeof(struct udphdr)))
5185 + return INVALID_PORT;
5187 + return ntohs(flags & IPSET_SRC ?
5188 + udph->source : udph->dest);
5191 + return INVALID_PORT;
5196 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5198 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5200 + if (port < map->first_port || port > map->last_port)
5203 + *hash_port = port;
5204 + DP("set: %s, port:%u, %u", set->name, port, *hash_port);
5205 + return !!test_bit(port - map->first_port, map->members);
5209 +testport(struct ip_set *set, const void *data, size_t size,
5210 + ip_set_ip_t *hash_port)
5212 + struct ip_set_req_portmap *req =
5213 + (struct ip_set_req_portmap *) data;
5215 + if (size != sizeof(struct ip_set_req_portmap)) {
5216 + ip_set_printk("data length wrong (want %zu, have %zu)",
5217 + sizeof(struct ip_set_req_portmap),
5221 + return __testport(set, req->port, hash_port);
5225 +testport_kernel(struct ip_set *set, const struct sk_buff *skb,
5226 + u_int32_t flags, ip_set_ip_t *hash_port)
5229 + ip_set_ip_t port = get_port(skb, flags);
5231 + DP("flag %s port %u", flags & IPSET_SRC ? "SRC" : "DST", port);
5232 + if (port == INVALID_PORT)
5235 + res = __testport(set, port, hash_port);
5237 + return (res < 0 ? 0 : res);
5241 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5243 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5245 + if (port < map->first_port || port > map->last_port)
5247 + if (test_and_set_bit(port - map->first_port, map->members))
5250 + *hash_port = port;
5251 + DP("port %u", port);
5256 +addport(struct ip_set *set, const void *data, size_t size,
5257 + ip_set_ip_t *hash_port)
5259 + struct ip_set_req_portmap *req =
5260 + (struct ip_set_req_portmap *) data;
5262 + if (size != sizeof(struct ip_set_req_portmap)) {
5263 + ip_set_printk("data length wrong (want %zu, have %zu)",
5264 + sizeof(struct ip_set_req_portmap),
5268 + return __addport(set, req->port, hash_port);
5272 +addport_kernel(struct ip_set *set, const struct sk_buff *skb,
5273 + u_int32_t flags, ip_set_ip_t *hash_port)
5275 + ip_set_ip_t port = get_port(skb, flags);
5277 + if (port == INVALID_PORT)
5280 + return __addport(set, port, hash_port);
5284 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5286 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5288 + if (port < map->first_port || port > map->last_port)
5290 + if (!test_and_clear_bit(port - map->first_port, map->members))
5293 + *hash_port = port;
5294 + DP("port %u", port);
5299 +delport(struct ip_set *set, const void *data, size_t size,
5300 + ip_set_ip_t *hash_port)
5302 + struct ip_set_req_portmap *req =
5303 + (struct ip_set_req_portmap *) data;
5305 + if (size != sizeof(struct ip_set_req_portmap)) {
5306 + ip_set_printk("data length wrong (want %zu, have %zu)",
5307 + sizeof(struct ip_set_req_portmap),
5311 + return __delport(set, req->port, hash_port);
5315 +delport_kernel(struct ip_set *set, const struct sk_buff *skb,
5316 + u_int32_t flags, ip_set_ip_t *hash_port)
5318 + ip_set_ip_t port = get_port(skb, flags);
5320 + if (port == INVALID_PORT)
5323 + return __delport(set, port, hash_port);
5326 +static int create(struct ip_set *set, const void *data, size_t size)
5329 + struct ip_set_req_portmap_create *req =
5330 + (struct ip_set_req_portmap_create *) data;
5331 + struct ip_set_portmap *map;
5333 + if (size != sizeof(struct ip_set_req_portmap_create)) {
5334 + ip_set_printk("data length wrong (want %zu, have %zu)",
5335 + sizeof(struct ip_set_req_portmap_create),
5340 + DP("from %u to %u", req->from, req->to);
5342 + if (req->from > req->to) {
5343 + DP("bad port range");
5347 + if (req->to - req->from > MAX_RANGE) {
5348 + ip_set_printk("range too big (max %d ports)",
5353 + map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
5355 + DP("out of memory for %d bytes",
5356 + sizeof(struct ip_set_portmap));
5359 + map->first_port = req->from;
5360 + map->last_port = req->to;
5361 + newbytes = bitmap_bytes(req->from, req->to);
5362 + map->members = kmalloc(newbytes, GFP_KERNEL);
5363 + if (!map->members) {
5364 + DP("out of memory for %d bytes", newbytes);
5368 + memset(map->members, 0, newbytes);
5374 +static void destroy(struct ip_set *set)
5376 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5378 + kfree(map->members);
5384 +static void flush(struct ip_set *set)
5386 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5387 + memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
5390 +static void list_header(const struct ip_set *set, void *data)
5392 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5393 + struct ip_set_req_portmap_create *header =
5394 + (struct ip_set_req_portmap_create *) data;
5396 + DP("list_header %u %u", map->first_port, map->last_port);
5398 + header->from = map->first_port;
5399 + header->to = map->last_port;
5402 +static int list_members_size(const struct ip_set *set)
5404 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5406 + return bitmap_bytes(map->first_port, map->last_port);
5409 +static void list_members(const struct ip_set *set, void *data)
5411 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5412 + int bytes = bitmap_bytes(map->first_port, map->last_port);
5414 + memcpy(data, map->members, bytes);
5417 +static struct ip_set_type ip_set_portmap = {
5418 + .typename = SETTYPE_NAME,
5419 + .typecode = IPSET_TYPE_PORT,
5420 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5421 + .create = &create,
5422 + .destroy = &destroy,
5424 + .reqsize = sizeof(struct ip_set_req_portmap),
5425 + .addip = &addport,
5426 + .addip_kernel = &addport_kernel,
5427 + .delip = &delport,
5428 + .delip_kernel = &delport_kernel,
5429 + .testip = &testport,
5430 + .testip_kernel = &testport_kernel,
5431 + .header_size = sizeof(struct ip_set_req_portmap_create),
5432 + .list_header = &list_header,
5433 + .list_members_size = &list_members_size,
5434 + .list_members = &list_members,
5435 + .me = THIS_MODULE,
5438 +MODULE_LICENSE("GPL");
5439 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5440 +MODULE_DESCRIPTION("portmap type of IP sets");
5442 +static int __init init(void)
5444 + return ip_set_register_set_type(&ip_set_portmap);
5447 +static void __exit fini(void)
5449 + /* FIXME: possible race with ip_set_create() */
5450 + ip_set_unregister_set_type(&ip_set_portmap);
5455 diff -Naur linux-old/net/ipv4/netfilter/ipt_set.c linux-new/net/ipv4/netfilter/ipt_set.c
5456 --- linux-old/net/ipv4/netfilter/ipt_set.c 1970-01-01 01:00:00.000000000 +0100
5457 +++ linux-new/net/ipv4/netfilter/ipt_set.c 2005-08-10 22:13:00.000000000 +0200
5459 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5460 + * Patrick Schaaf <bof@bof.de>
5461 + * Martin Josefsson <gandalf@wlug.westbo.se>
5462 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5464 + * This program is free software; you can redistribute it and/or modify
5465 + * it under the terms of the GNU General Public License version 2 as
5466 + * published by the Free Software Foundation.
5469 +/* Kernel module to match an IP set. */
5471 +#include <linux/module.h>
5472 +#include <linux/ip.h>
5473 +#include <linux/skbuff.h>
5475 +#include <linux/netfilter_ipv4/ip_tables.h>
5476 +#include <linux/netfilter_ipv4/ip_set.h>
5477 +#include <linux/netfilter_ipv4/ipt_set.h>
5480 +match_set(const struct ipt_set_info *info,
5481 + const struct sk_buff *skb,
5484 + if (ip_set_testip_kernel(info->index, skb, info->flags))
5490 +match(const struct sk_buff *skb,
5491 + const struct net_device *in,
5492 + const struct net_device *out,
5493 + const void *matchinfo,
5496 + u_int16_t datalen,
5499 + const struct ipt_set_info_match *info = matchinfo;
5501 + return match_set(&info->match_set,
5503 + info->match_set.flags[0] & IPSET_MATCH_INV);
5507 +checkentry(const char *tablename,
5508 + const struct ipt_ip *ip,
5510 + unsigned int matchsize,
5511 + unsigned int hook_mask)
5513 + struct ipt_set_info_match *info =
5514 + (struct ipt_set_info_match *) matchinfo;
5515 + ip_set_id_t index;
5517 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5518 + ip_set_printk("invalid matchsize %d", matchsize);
5522 + index = ip_set_get_byindex(info->match_set.index);
5524 + if (index == IP_SET_INVALID_ID) {
5525 + ip_set_printk("Cannot find set indentified by id %u to match",
5526 + info->match_set.index);
5527 + return 0; /* error */
5529 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5530 + ip_set_printk("That's nasty!");
5531 + return 0; /* error */
5537 +static void destroy(void *matchinfo, unsigned int matchsize)
5539 + struct ipt_set_info_match *info = matchinfo;
5541 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5542 + ip_set_printk("invalid matchsize %d", matchsize);
5546 + ip_set_put(info->match_set.index);
5549 +static struct ipt_match set_match = {
5552 + .checkentry = &checkentry,
5553 + .destroy = &destroy,
5557 +MODULE_LICENSE("GPL");
5558 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5559 +MODULE_DESCRIPTION("iptables IP set match module");
5561 +static int __init init(void)
5563 + return ipt_register_match(&set_match);
5566 +static void __exit fini(void)
5568 + ipt_unregister_match(&set_match);
5573 diff -Naur linux-old/net/ipv4/netfilter/ipt_SET.c linux-new/net/ipv4/netfilter/ipt_SET.c
5574 --- linux-old/net/ipv4/netfilter/ipt_SET.c 1970-01-01 01:00:00.000000000 +0100
5575 +++ linux-new/net/ipv4/netfilter/ipt_SET.c 2005-08-10 22:13:00.000000000 +0200
5577 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5578 + * Patrick Schaaf <bof@bof.de>
5579 + * Martin Josefsson <gandalf@wlug.westbo.se>
5580 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5582 + * This program is free software; you can redistribute it and/or modify
5583 + * it under the terms of the GNU General Public License version 2 as
5584 + * published by the Free Software Foundation.
5587 +/* ipt_SET.c - netfilter target to manipulate IP sets */
5589 +#include <linux/types.h>
5590 +#include <linux/ip.h>
5591 +#include <linux/timer.h>
5592 +#include <linux/module.h>
5593 +#include <linux/netfilter.h>
5594 +#include <linux/netdevice.h>
5595 +#include <linux/if.h>
5596 +#include <linux/inetdevice.h>
5597 +#include <net/protocol.h>
5598 +#include <net/checksum.h>
5599 +#include <linux/netfilter_ipv4.h>
5600 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
5601 +#include <linux/netfilter_ipv4/ipt_set.h>
5603 +static unsigned int
5604 +target(struct sk_buff **pskb,
5605 + unsigned int hooknum,
5606 + const struct net_device *in,
5607 + const struct net_device *out,
5608 + const void *targinfo,
5611 + const struct ipt_set_info_target *info = targinfo;
5613 + if (info->add_set.index != IP_SET_INVALID_ID)
5614 + ip_set_addip_kernel(info->add_set.index,
5616 + info->add_set.flags);
5617 + if (info->del_set.index != IP_SET_INVALID_ID)
5618 + ip_set_delip_kernel(info->del_set.index,
5620 + info->del_set.flags);
5622 + return IPT_CONTINUE;
5626 +checkentry(const char *tablename,
5627 + const struct ipt_entry *e,
5629 + unsigned int targinfosize, unsigned int hook_mask)
5631 + struct ipt_set_info_target *info =
5632 + (struct ipt_set_info_target *) targinfo;
5633 + ip_set_id_t index;
5635 + if (targinfosize != IPT_ALIGN(sizeof(*info))) {
5636 + DP("bad target info size %u", targinfosize);
5640 + if (info->add_set.index != IP_SET_INVALID_ID) {
5641 + index = ip_set_get_byindex(info->add_set.index);
5642 + if (index == IP_SET_INVALID_ID) {
5643 + ip_set_printk("cannot find add_set index %u as target",
5644 + info->add_set.index);
5645 + return 0; /* error */
5649 + if (info->del_set.index != IP_SET_INVALID_ID) {
5650 + index = ip_set_get_byindex(info->del_set.index);
5651 + if (index == IP_SET_INVALID_ID) {
5652 + ip_set_printk("cannot find del_set index %u as target",
5653 + info->del_set.index);
5654 + return 0; /* error */
5657 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
5658 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5659 + ip_set_printk("That's nasty!");
5660 + return 0; /* error */
5665 +static void destroy(void *targetinfo, unsigned int targetsize)
5667 + struct ipt_set_info_target *info = targetinfo;
5669 + if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
5670 + ip_set_printk("invalid targetsize %d", targetsize);
5674 + if (info->add_set.index != IP_SET_INVALID_ID)
5675 + ip_set_put(info->add_set.index);
5676 + if (info->del_set.index != IP_SET_INVALID_ID)
5677 + ip_set_put(info->del_set.index);
5680 +static struct ipt_target SET_target = {
5683 + .checkentry = checkentry,
5684 + .destroy = destroy,
5688 +MODULE_LICENSE("GPL");
5689 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5690 +MODULE_DESCRIPTION("iptables IP set target module");
5692 +static int __init init(void)
5694 + return ipt_register_target(&SET_target);
5697 +static void __exit fini(void)
5699 + ipt_unregister_target(&SET_target);
5704 diff -Naur linux-old/net/ipv4/netfilter/Makefile linux-new/net/ipv4/netfilter/Makefile
5705 --- linux-old/net/ipv4/netfilter/Makefile 2005-07-23 23:34:46.000000000 +0200
5706 +++ linux-new/net/ipv4/netfilter/Makefile 2005-08-10 22:13:01.000000000 +0200
5708 obj-$(CONFIG_IP_NF_MATCH_HELPER) += ipt_helper.o
5709 obj-$(CONFIG_IP_NF_MATCH_LIMIT) += ipt_limit.o
5710 obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
5711 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
5712 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
5713 +ifdef CONFIG_IP_NF_SET
5714 + obj-$(CONFIG_IP_NF_SET) += ip_set.o
5715 + export-objs += ip_set.o
5717 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
5718 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
5719 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
5720 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
5721 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
5722 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
5723 obj-$(CONFIG_IP_NF_MATCH_MAC) += ipt_mac.o
5724 obj-$(CONFIG_IP_NF_MATCH_IPP2P) += ipt_ipp2p.o