1 --- a/Documentation/Configure.help
2 +++ b/Documentation/Configure.help
3 @@ -3231,6 +3231,75 @@ CONFIG_IP_NF_TARGET_CLASSIFY
4 If you want to compile it as a module, say M here and read
5 Documentation/modules.txt. If unsure, say `N'.
9 + This option adds IP set support to the kernel.
11 + In order to define and use sets, you need userlevel utilities: an
12 + iptables binary which knows about IP sets and the program ipset(8),
13 + by which you can define and setup the sets themselves.
15 + If you want to compile it as a module, say M here and read
16 + <file:Documentation/modules.txt>. If unsure, say `N'.
19 +CONFIG_IP_NF_MATCH_SET
20 + This option adds IP set match support.
21 + You need the ipset utility to create and set up the sets.
23 + If you want to compile it as a module, say M here and read
24 + <file:Documentation/modules.txt>. If unsure, say `N'.
27 +CONFIG_IP_NF_TARGET_SET
28 + This option adds IP set target support.
29 + You need the ipset utility to create and set up the sets.
31 + If you want to compile it as a module, say M here and read
32 + <file:Documentation/modules.txt>. If unsure, say `N'.
34 +ipmap set type support
35 +CONFIG_IP_NF_SET_IPMAP
36 + This option adds the ipmap set type support.
38 + If you want to compile it as a module, say M here and read
39 + <file:Documentation/modules.txt>. If unsure, say `N'.
41 +macipmap set type support
42 +CONFIG_IP_NF_SET_MACIPMAP
43 + This option adds the macipmap set type support.
45 + If you want to compile it as a module, say M here and read
46 + <file:Documentation/modules.txt>. If unsure, say `N'.
48 +portmap set type support
49 +CONFIG_IP_NF_SET_PORTMAP
50 + This option adds the portmap set type support.
52 + If you want to compile it as a module, say M here and read
53 + <file:Documentation/modules.txt>. If unsure, say `N'.
55 +iphash set type support
56 +CONFIG_IP_NF_SET_IPHASH
57 + This option adds the iphash set type support.
59 + If you want to compile it as a module, say M here and read
60 + <file:Documentation/modules.txt>. If unsure, say `N'.
62 +nethash set type support
63 +CONFIG_IP_NF_SET_NETHASH
64 + This option adds the nethash set type support.
66 + If you want to compile it as a module, say M here and read
67 + <file:Documentation/modules.txt>. If unsure, say `N'.
69 +iptree set type support
70 +CONFIG_IP_NF_SET_IPTREE
71 + This option adds the iptree set type support.
73 + If you want to compile it as a module, say M here and read
74 + <file:Documentation/modules.txt>. If unsure, say `N'.
77 CONFIG_IP_NF_TARGET_TTL
78 This option adds a `TTL' target, which enables the user to set
80 +++ b/include/linux/netfilter_ipv4/ip_set.h
85 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
86 + * Patrick Schaaf <bof@bof.de>
87 + * Martin Josefsson <gandalf@wlug.westbo.se>
88 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
90 + * This program is free software; you can redistribute it and/or modify
91 + * it under the terms of the GNU General Public License version 2 as
92 + * published by the Free Software Foundation.
96 + * A sockopt of such quality has hardly ever been seen before on the open
97 + * market! This little beauty, hardly ever used: above 64, so it's
98 + * traditionally used for firewalling, not touched (even once!) by the
99 + * 2.0, 2.2 and 2.4 kernels!
101 + * Comes with its own certificate of authenticity, valid anywhere in the
106 +#define SO_IP_SET 83
109 + * Heavily modify by Joakim Axelsson 08.03.2002
110 + * - Made it more modulebased
112 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
114 + * - in order to "deal with" backward compatibility, renamed to ipset
118 + * Used so that the kernel module and ipset-binary can match their versions
120 +#define IP_SET_PROTOCOL_VERSION 2
122 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
124 +/* Lets work with our own typedef for representing an IP address.
125 + * We hope to make the code more portable, possibly to IPv6...
127 + * The representation works in HOST byte order, because most set types
128 + * will perform arithmetic operations and compare operations.
130 + * For now the type is an uint32_t.
132 + * Make sure to ONLY use the functions when translating and parsing
133 + * in order to keep the host byte order and make it more portable:
136 + * parse_ipandmask()
138 + * (Joakim: where are they???)
141 +typedef uint32_t ip_set_ip_t;
143 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
144 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
146 +typedef uint16_t ip_set_id_t;
148 +#define IP_SET_INVALID_ID 65535
150 +/* How deep we follow bindings */
151 +#define IP_SET_MAX_BINDINGS 6
154 + * Option flags for kernel operations (ipt_set_info)
156 +#define IPSET_SRC 0x01 /* Source match/add */
157 +#define IPSET_DST 0x02 /* Destination match/add */
158 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
161 + * Set types (flavours)
163 +#define IPSET_TYPE_IP 0 /* IP address type of set */
164 +#define IPSET_TYPE_PORT 1 /* Port type of set */
166 +/* Reserved keywords */
167 +#define IPSET_TOKEN_DEFAULT ":default:"
168 +#define IPSET_TOKEN_ALL ":all:"
170 +/* SO_IP_SET operation constants, and their request struct types.
173 + * 0-99: commands with version checking
174 + * 100-199: add/del/test/bind/unbind
175 + * 200-299: list, save, restore
178 +/* Single shot operations:
179 + * version, create, destroy, flush, rename and swap
181 + * Sets are identified by name.
184 +#define IP_SET_REQ_STD \
186 + unsigned version; \
187 + char name[IP_SET_MAXNAMELEN]
189 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
190 +struct ip_set_req_create {
192 + char typename[IP_SET_MAXNAMELEN];
195 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
196 +struct ip_set_req_std {
200 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
201 +/* Uses ip_set_req_std */
203 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
204 +/* Uses ip_set_req_create */
206 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
207 +/* Uses ip_set_req_create */
209 +union ip_set_name_index {
210 + char name[IP_SET_MAXNAMELEN];
214 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
215 +struct ip_set_req_get_set {
218 + union ip_set_name_index set;
221 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
222 +/* Uses ip_set_req_get_set */
224 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
225 +struct ip_set_req_version {
230 +/* Double shots operations:
231 + * add, del, test, bind and unbind.
233 + * First we query the kernel to get the index and type of the target set,
234 + * then issue the command. Validity of IP is checked in kernel in order
235 + * to minimalize sockopt operations.
238 +/* Get minimal set data for add/del/test/bind/unbind IP */
239 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
240 +struct ip_set_req_adt_get {
243 + union ip_set_name_index set;
244 + char typename[IP_SET_MAXNAMELEN];
247 +#define IP_SET_REQ_BYINDEX \
251 +struct ip_set_req_adt {
252 + IP_SET_REQ_BYINDEX;
255 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
256 +/* Uses ip_set_req_adt, with type specific addage */
258 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
259 +/* Uses ip_set_req_adt, with type specific addage */
261 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
262 +/* Uses ip_set_req_adt, with type specific addage */
264 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
265 +/* Uses ip_set_req_bind, with type specific addage */
266 +struct ip_set_req_bind {
267 + IP_SET_REQ_BYINDEX;
268 + char binding[IP_SET_MAXNAMELEN];
271 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
272 +/* Uses ip_set_req_bind, with type speficic addage
273 + * index = 0 means unbinding for all sets */
275 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
276 +/* Uses ip_set_req_bind, with type specific addage */
278 +/* Multiple shots operations: list, save, restore.
280 + * - check kernel version and query the max number of sets
281 + * - get the basic information on all sets
282 + * and size required for the next step
283 + * - get actual set data: header, data, bindings
286 +/* Get max_sets and the index of a queried set
288 +#define IP_SET_OP_MAX_SETS 0x00000020
289 +struct ip_set_req_max_sets {
292 + ip_set_id_t max_sets; /* max_sets */
293 + ip_set_id_t sets; /* real number of sets */
294 + union ip_set_name_index set; /* index of set if name used */
297 +/* Get the id and name of the sets plus size for next step */
298 +#define IP_SET_OP_LIST_SIZE 0x00000201
299 +#define IP_SET_OP_SAVE_SIZE 0x00000202
300 +struct ip_set_req_setnames {
302 + ip_set_id_t index; /* set to list/save */
303 + size_t size; /* size to get setdata/bindings */
304 + /* followed by sets number of struct ip_set_name_list */
307 +struct ip_set_name_list {
308 + char name[IP_SET_MAXNAMELEN];
309 + char typename[IP_SET_MAXNAMELEN];
314 +/* The actual list operation */
315 +#define IP_SET_OP_LIST 0x00000203
316 +struct ip_set_req_list {
317 + IP_SET_REQ_BYINDEX;
318 + /* sets number of struct ip_set_list in reply */
321 +struct ip_set_list {
323 + ip_set_id_t binding;
325 + size_t header_size; /* Set header data of header_size */
326 + size_t members_size; /* Set members data of members_size */
327 + size_t bindings_size; /* Set bindings data of bindings_size */
330 +struct ip_set_hash_list {
332 + ip_set_id_t binding;
335 +/* The save operation */
336 +#define IP_SET_OP_SAVE 0x00000204
337 +/* Uses ip_set_req_list, in the reply replaced by
338 + * sets number of struct ip_set_save plus a marker
339 + * ip_set_save followed by ip_set_hash_save structures.
341 +struct ip_set_save {
343 + ip_set_id_t binding;
344 + size_t header_size; /* Set header data of header_size */
345 + size_t members_size; /* Set members data of members_size */
348 +/* At restoring, ip == 0 means default binding for the given set: */
349 +struct ip_set_hash_save {
352 + ip_set_id_t binding;
355 +/* The restore operation */
356 +#define IP_SET_OP_RESTORE 0x00000205
357 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
358 + * plus a marker ip_set_restore, followed by ip_set_hash_save
361 +struct ip_set_restore {
362 + char name[IP_SET_MAXNAMELEN];
363 + char typename[IP_SET_MAXNAMELEN];
365 + size_t header_size; /* Create data of header_size */
366 + size_t members_size; /* Set members data of members_size */
369 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
371 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
376 +#define ip_set_printk(format, args...) \
378 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
379 + printk(format "\n" , ## args); \
382 +#if defined(IP_SET_DEBUG)
383 +#define DP(format, args...) \
385 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
386 + printk(format "\n" , ## args); \
388 +#define IP_SET_ASSERT(x) \
391 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
392 + __FILE__, __LINE__, __FUNCTION__); \
395 +#define DP(format, args...)
396 +#define IP_SET_ASSERT(x)
402 + * The ip_set_type definition - one per set type, e.g. "ipmap".
404 + * Each individual set has a pointer, set->type, going to one
405 + * of these structures. Function pointers inside the structure implement
406 + * the real behaviour of the sets.
408 + * If not mentioned differently, the implementation behind the function
409 + * pointers of a set_type, is expected to return 0 if ok, and a negative
410 + * errno (e.g. -EINVAL) on error.
412 +struct ip_set_type {
413 + struct list_head list; /* next in list of set types */
415 + /* test for IP in set (kernel: iptables -m set src|dst)
416 + * return 0 if not in set, 1 if in set.
418 + int (*testip_kernel) (struct ip_set *set,
419 + const struct sk_buff * skb,
423 + /* test for IP in set (userspace: ipset -T set IP)
424 + * return 0 if not in set, 1 if in set.
426 + int (*testip) (struct ip_set *set,
427 + const void *data, size_t size,
431 + * Size of the data structure passed by when
432 + * adding/deletin/testing an entry.
436 + /* Add IP into set (userspace: ipset -A set IP)
437 + * Return -EEXIST if the address is already in the set,
438 + * and -ERANGE if the address lies outside the set bounds.
439 + * If the address was not already in the set, 0 is returned.
441 + int (*addip) (struct ip_set *set,
442 + const void *data, size_t size,
445 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
446 + * Return -EEXIST if the address is already in the set,
447 + * and -ERANGE if the address lies outside the set bounds.
448 + * If the address was not already in the set, 0 is returned.
450 + int (*addip_kernel) (struct ip_set *set,
451 + const struct sk_buff * skb,
455 + /* remove IP from set (userspace: ipset -D set --entry x)
456 + * Return -EEXIST if the address is NOT in the set,
457 + * and -ERANGE if the address lies outside the set bounds.
458 + * If the address really was in the set, 0 is returned.
460 + int (*delip) (struct ip_set *set,
461 + const void *data, size_t size,
464 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
465 + * Return -EEXIST if the address is NOT in the set,
466 + * and -ERANGE if the address lies outside the set bounds.
467 + * If the address really was in the set, 0 is returned.
469 + int (*delip_kernel) (struct ip_set *set,
470 + const struct sk_buff * skb,
474 + /* new set creation - allocated type specific items
476 + int (*create) (struct ip_set *set,
477 + const void *data, size_t size);
479 + /* retry the operation after successfully tweaking the set
481 + int (*retry) (struct ip_set *set);
483 + /* set destruction - free type specific items
484 + * There is no return value.
485 + * Can be called only when child sets are destroyed.
487 + void (*destroy) (struct ip_set *set);
489 + /* set flushing - reset all bits in the set, or something similar.
490 + * There is no return value.
492 + void (*flush) (struct ip_set *set);
494 + /* Listing: size needed for header
496 + size_t header_size;
498 + /* Listing: Get the header
500 + * Fill in the information in "data".
501 + * This function is always run after list_header_size() under a
502 + * writelock on the set. Therefor is the length of "data" always
505 + void (*list_header) (const struct ip_set *set,
508 + /* Listing: Get the size for the set members
510 + int (*list_members_size) (const struct ip_set *set);
512 + /* Listing: Get the set members
514 + * Fill in the information in "data".
515 + * This function is always run after list_member_size() under a
516 + * writelock on the set. Therefor is the length of "data" always
519 + void (*list_members) (const struct ip_set *set,
522 + char typename[IP_SET_MAXNAMELEN];
524 + int protocol_version;
526 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
530 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
531 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
533 +/* A generic ipset */
535 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
536 + rwlock_t lock; /* lock for concurrency control */
537 + ip_set_id_t id; /* set id for swapping */
538 + ip_set_id_t binding; /* default binding for the set */
539 + atomic_t ref; /* in kernel and in hash references */
540 + struct ip_set_type *type; /* the set types */
541 + void *data; /* pooltype specific data */
544 +/* Structure to bind set elements to sets */
545 +struct ip_set_hash {
546 + struct list_head list; /* list of clashing entries in hash */
547 + ip_set_ip_t ip; /* ip from set */
548 + ip_set_id_t id; /* set id */
549 + ip_set_id_t binding; /* set we bind the element to */
552 +/* register and unregister set references */
553 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
554 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
555 +extern void ip_set_put(ip_set_id_t id);
557 +/* API for iptables set match, and SET target */
558 +extern void ip_set_addip_kernel(ip_set_id_t id,
559 + const struct sk_buff *skb,
560 + const u_int32_t *flags);
561 +extern void ip_set_delip_kernel(ip_set_id_t id,
562 + const struct sk_buff *skb,
563 + const u_int32_t *flags);
564 +extern int ip_set_testip_kernel(ip_set_id_t id,
565 + const struct sk_buff *skb,
566 + const u_int32_t *flags);
568 +#endif /* __KERNEL__ */
570 +#endif /*_IP_SET_H*/
572 +++ b/include/linux/netfilter_ipv4/ip_set_iphash.h
574 +#ifndef __IP_SET_IPHASH_H
575 +#define __IP_SET_IPHASH_H
577 +#include <linux/netfilter_ipv4/ip_set.h>
579 +#define SETTYPE_NAME "iphash"
580 +#define MAX_RANGE 0x0000FFFF
582 +struct ip_set_iphash {
583 + ip_set_ip_t *members; /* the iphash proper */
584 + uint32_t initval; /* initval for jhash_1word */
585 + uint32_t prime; /* prime for double hashing */
586 + uint32_t hashsize; /* hash size */
587 + uint16_t probes; /* max number of probes */
588 + uint16_t resize; /* resize factor in percent */
589 + ip_set_ip_t netmask; /* netmask */
592 +struct ip_set_req_iphash_create {
596 + ip_set_ip_t netmask;
599 +struct ip_set_req_iphash {
603 +#endif /* __IP_SET_IPHASH_H */
605 +++ b/include/linux/netfilter_ipv4/ip_set_ipmap.h
607 +#ifndef __IP_SET_IPMAP_H
608 +#define __IP_SET_IPMAP_H
610 +#include <linux/netfilter_ipv4/ip_set.h>
612 +#define SETTYPE_NAME "ipmap"
613 +#define MAX_RANGE 0x0000FFFF
615 +struct ip_set_ipmap {
616 + void *members; /* the ipmap proper */
617 + ip_set_ip_t first_ip; /* host byte order, included in range */
618 + ip_set_ip_t last_ip; /* host byte order, included in range */
619 + ip_set_ip_t netmask; /* subnet netmask */
620 + ip_set_ip_t sizeid; /* size of set in IPs */
621 + u_int16_t hosts; /* number of hosts in a subnet */
624 +struct ip_set_req_ipmap_create {
627 + ip_set_ip_t netmask;
630 +struct ip_set_req_ipmap {
635 +mask_to_bits(ip_set_ip_t mask)
637 + unsigned int bits = 32;
638 + ip_set_ip_t maskaddr;
640 + if (mask == 0xFFFFFFFF)
643 + maskaddr = 0xFFFFFFFE;
644 + while (--bits >= 0 && maskaddr != mask)
651 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
653 + ip_set_ip_t mask = 0xFFFFFFFE;
656 + while (--(*bits) >= 0 && mask && (to & mask) != from)
662 +#endif /* __IP_SET_IPMAP_H */
664 +++ b/include/linux/netfilter_ipv4/ip_set_iptree.h
666 +#ifndef __IP_SET_IPTREE_H
667 +#define __IP_SET_IPTREE_H
669 +#include <linux/netfilter_ipv4/ip_set.h>
671 +#define SETTYPE_NAME "iptree"
672 +#define MAX_RANGE 0x0000FFFF
674 +struct ip_set_iptreed {
675 + unsigned long expires[255]; /* x.x.x.ADDR */
678 +struct ip_set_iptreec {
679 + struct ip_set_iptreed *tree[255]; /* x.x.ADDR.* */
682 +struct ip_set_iptreeb {
683 + struct ip_set_iptreec *tree[255]; /* x.ADDR.*.* */
686 +struct ip_set_iptree {
687 + unsigned int timeout;
688 + unsigned int gc_interval;
690 + struct timer_list gc;
691 + struct ip_set_iptreeb *tree[255]; /* ADDR.*.*.* */
695 +struct ip_set_req_iptree_create {
696 + unsigned int timeout;
699 +struct ip_set_req_iptree {
701 + unsigned int timeout;
704 +#endif /* __IP_SET_IPTREE_H */
706 +++ b/include/linux/netfilter_ipv4/ip_set_jhash.h
708 +#ifndef _LINUX_IPSET_JHASH_H
709 +#define _LINUX_IPSET_JHASH_H
711 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
712 + * to __u32/__u8 so that the header file can be included into
713 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
716 +/* jhash.h: Jenkins hash support.
718 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
720 + * http://burtleburtle.net/bob/hash/
722 + * These are the credits from Bob's sources:
724 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
725 + * hash(), hash2(), hash3, and mix() are externally useful functions.
726 + * Routines to test the hash are included if SELF_TEST is defined.
727 + * You can use this free for any purpose. It has no warranty.
729 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
731 + * I've modified Bob's hash to be useful in the Linux kernel, and
732 + * any bugs present are surely my fault. -DaveM
735 +/* NOTE: Arguments are modified. */
736 +#define __jhash_mix(a, b, c) \
738 + a -= b; a -= c; a ^= (c>>13); \
739 + b -= c; b -= a; b ^= (a<<8); \
740 + c -= a; c -= b; c ^= (b>>13); \
741 + a -= b; a -= c; a ^= (c>>12); \
742 + b -= c; b -= a; b ^= (a<<16); \
743 + c -= a; c -= b; c ^= (b>>5); \
744 + a -= b; a -= c; a ^= (c>>3); \
745 + b -= c; b -= a; b ^= (a<<10); \
746 + c -= a; c -= b; c ^= (b>>15); \
749 +/* The golden ration: an arbitrary value */
750 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
752 +/* The most generic version, hashes an arbitrary sequence
753 + * of bytes. No alignment or length assumptions are made about
756 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
758 + __u32 a, b, c, len;
762 + a = b = JHASH_GOLDEN_RATIO;
765 + while (len >= 12) {
766 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
767 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
768 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
770 + __jhash_mix(a,b,c);
778 + case 11: c += ((__u32)k[10]<<24);
779 + case 10: c += ((__u32)k[9]<<16);
780 + case 9 : c += ((__u32)k[8]<<8);
781 + case 8 : b += ((__u32)k[7]<<24);
782 + case 7 : b += ((__u32)k[6]<<16);
783 + case 6 : b += ((__u32)k[5]<<8);
784 + case 5 : b += k[4];
785 + case 4 : a += ((__u32)k[3]<<24);
786 + case 3 : a += ((__u32)k[2]<<16);
787 + case 2 : a += ((__u32)k[1]<<8);
788 + case 1 : a += k[0];
791 + __jhash_mix(a,b,c);
796 +/* A special optimized version that handles 1 or more of __u32s.
797 + * The length parameter here is the number of __u32s in the key.
799 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
801 + __u32 a, b, c, len;
803 + a = b = JHASH_GOLDEN_RATIO;
811 + __jhash_mix(a, b, c);
818 + case 2 : b += k[1];
819 + case 1 : a += k[0];
822 + __jhash_mix(a,b,c);
828 +/* A special ultra-optimized versions that knows they are hashing exactly
829 + * 3, 2 or 1 word(s).
831 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
832 + * done at the end is not done here.
834 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
836 + a += JHASH_GOLDEN_RATIO;
837 + b += JHASH_GOLDEN_RATIO;
840 + __jhash_mix(a, b, c);
845 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
847 + return jhash_3words(a, b, 0, initval);
850 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
852 + return jhash_3words(a, 0, 0, initval);
855 +#endif /* _LINUX_IPSET_JHASH_H */
857 +++ b/include/linux/netfilter_ipv4/ip_set_macipmap.h
859 +#ifndef __IP_SET_MACIPMAP_H
860 +#define __IP_SET_MACIPMAP_H
862 +#include <linux/netfilter_ipv4/ip_set.h>
864 +#define SETTYPE_NAME "macipmap"
865 +#define MAX_RANGE 0x0000FFFF
868 +#define IPSET_MACIP_MATCHUNSET 1
871 +#define IPSET_MACIP_ISSET 1
873 +struct ip_set_macipmap {
874 + void *members; /* the macipmap proper */
875 + ip_set_ip_t first_ip; /* host byte order, included in range */
876 + ip_set_ip_t last_ip; /* host byte order, included in range */
880 +struct ip_set_req_macipmap_create {
886 +struct ip_set_req_macipmap {
888 + unsigned char ethernet[ETH_ALEN];
891 +struct ip_set_macip {
892 + unsigned short flags;
893 + unsigned char ethernet[ETH_ALEN];
896 +#endif /* __IP_SET_MACIPMAP_H */
898 +++ b/include/linux/netfilter_ipv4/ip_set_malloc.h
900 +#ifndef _IP_SET_MALLOC_H
901 +#define _IP_SET_MALLOC_H
905 +/* Memory allocation and deallocation */
906 +static size_t max_malloc_size = 131072; /* Guaranteed: slab.c */
908 +static inline void * ip_set_malloc(size_t bytes)
910 + if (bytes > max_malloc_size)
911 + return vmalloc(bytes);
913 + return kmalloc(bytes, GFP_KERNEL);
916 +static inline void ip_set_free(void * data, size_t bytes)
918 + if (bytes > max_malloc_size)
924 +#endif /* __KERNEL__ */
926 +#endif /*_IP_SET_MALLOC_H*/
928 +++ b/include/linux/netfilter_ipv4/ip_set_nethash.h
930 +#ifndef __IP_SET_NETHASH_H
931 +#define __IP_SET_NETHASH_H
933 +#include <linux/netfilter_ipv4/ip_set.h>
935 +#define SETTYPE_NAME "nethash"
936 +#define MAX_RANGE 0x0000FFFF
938 +struct ip_set_nethash {
939 + ip_set_ip_t *members; /* the nethash proper */
940 + uint32_t initval; /* initval for jhash_1word */
941 + uint32_t prime; /* prime for double hashing */
942 + uint32_t hashsize; /* hash size */
943 + uint16_t probes; /* max number of probes */
944 + uint16_t resize; /* resize factor in percent */
945 + unsigned char cidr[30]; /* CIDR sizes */
948 +struct ip_set_req_nethash_create {
954 +struct ip_set_req_nethash {
956 + unsigned char cidr;
959 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
961 +static inline ip_set_ip_t
962 +pack(ip_set_ip_t ip, unsigned char cidr)
964 + ip_set_ip_t addr, *paddr = &addr;
965 + unsigned char n, t, *a;
967 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
969 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
973 + a = &((unsigned char *)paddr)[n];
974 + *a = *a /(1 << (8 - t)) + shifts[t];
976 + DP("n: %u, t: %u, a: %u", n, t, *a);
977 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
978 + HIPQUAD(ip), cidr, NIPQUAD(addr));
981 + return ntohl(addr);
984 +#endif /* __IP_SET_NETHASH_H */
986 +++ b/include/linux/netfilter_ipv4/ip_set_portmap.h
988 +#ifndef __IP_SET_PORTMAP_H
989 +#define __IP_SET_PORTMAP_H
991 +#include <linux/netfilter_ipv4/ip_set.h>
993 +#define SETTYPE_NAME "portmap"
994 +#define MAX_RANGE 0x0000FFFF
995 +#define INVALID_PORT (MAX_RANGE + 1)
997 +struct ip_set_portmap {
998 + void *members; /* the portmap proper */
999 + ip_set_ip_t first_port; /* host byte order, included in range */
1000 + ip_set_ip_t last_port; /* host byte order, included in range */
1003 +struct ip_set_req_portmap_create {
1008 +struct ip_set_req_portmap {
1012 +#endif /* __IP_SET_PORTMAP_H */
1014 +++ b/include/linux/netfilter_ipv4/ip_set_prime.h
1016 +#ifndef __IP_SET_PRIME_H
1017 +#define __IP_SET_PRIME_H
1019 +static inline unsigned make_prime_bound(unsigned nr)
1021 + unsigned long long nr64 = nr;
1022 + unsigned long long x = 1;
1024 + while (x <= nr64) { x <<= 2; nr <<= 1; }
1028 +static inline int make_prime_check(unsigned nr)
1031 + unsigned b = make_prime_bound(nr);
1033 + if (0 == (nr % x)) return 0;
1039 +static unsigned make_prime(unsigned nr)
1041 + if (0 == (nr & 1)) nr--;
1043 + if (make_prime_check(nr)) return nr;
1049 +#endif /* __IP_SET_PRIME_H */
1051 +++ b/include/linux/netfilter_ipv4/ipt_set.h
1056 +#include <linux/netfilter_ipv4/ip_set.h>
1058 +struct ipt_set_info {
1059 + ip_set_id_t index;
1060 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1064 +struct ipt_set_info_match {
1065 + struct ipt_set_info match_set;
1068 +struct ipt_set_info_target {
1069 + struct ipt_set_info add_set;
1070 + struct ipt_set_info del_set;
1073 +#endif /*_IPT_SET_H*/
1074 --- a/net/ipv4/netfilter/Config.in
1075 +++ b/net/ipv4/netfilter/Config.in
1076 @@ -22,6 +22,20 @@ tristate 'IP tables support (required fo
1077 if [ "$CONFIG_IP_NF_IPTABLES" != "n" ]; then
1078 # The simple matches.
1079 dep_tristate ' limit match support' CONFIG_IP_NF_MATCH_LIMIT $CONFIG_IP_NF_IPTABLES
1081 + dep_tristate ' IP set support' CONFIG_IP_NF_SET $CONFIG_IP_NF_IPTABLES
1082 + if [ "$CONFIG_IP_NF_SET" != "n" ]; then
1083 + int ' Maximum number of sets' CONFIG_IP_NF_SET_MAX 256
1084 + int ' Hash size for bindings of IP sets' CONFIG_IP_NF_SET_HASHSIZE 1024
1085 + dep_tristate ' set match support' CONFIG_IP_NF_MATCH_SET $CONFIG_IP_NF_SET
1086 + dep_tristate ' SET target support' CONFIG_IP_NF_TARGET_SET $CONFIG_IP_NF_SET
1087 + dep_tristate ' ipmap set type support' CONFIG_IP_NF_SET_IPMAP $CONFIG_IP_NF_SET
1088 + dep_tristate ' portmap set type support' CONFIG_IP_NF_SET_PORTMAP $CONFIG_IP_NF_SET
1089 + dep_tristate ' macipmap set type support' CONFIG_IP_NF_SET_MACIPMAP $CONFIG_IP_NF_SET
1090 + dep_tristate ' iphash set type support' CONFIG_IP_NF_SET_IPHASH $CONFIG_IP_NF_SET
1091 + dep_tristate ' nethash set type support' CONFIG_IP_NF_SET_NETHASH $CONFIG_IP_NF_SET
1092 + dep_tristate ' iptree set type support' CONFIG_IP_NF_SET_IPTREE $CONFIG_IP_NF_SET
1094 dep_tristate ' MAC address match support' CONFIG_IP_NF_MATCH_MAC $CONFIG_IP_NF_IPTABLES
1095 dep_tristate ' Packet type match support' CONFIG_IP_NF_MATCH_PKTTYPE $CONFIG_IP_NF_IPTABLES
1096 dep_tristate ' netfilter MARK match support' CONFIG_IP_NF_MATCH_MARK $CONFIG_IP_NF_IPTABLES
1098 +++ b/net/ipv4/netfilter/ip_set.c
1100 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1101 + * Patrick Schaaf <bof@bof.de>
1102 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1104 + * This program is free software; you can redistribute it and/or modify
1105 + * it under the terms of the GNU General Public License version 2 as
1106 + * published by the Free Software Foundation.
1109 +/* Kernel module for IP set management */
1111 +#include <linux/config.h>
1112 +#include <linux/module.h>
1113 +#include <linux/kmod.h>
1114 +#include <linux/ip.h>
1115 +#include <linux/skbuff.h>
1116 +#include <linux/random.h>
1117 +#include <linux/jhash.h>
1118 +#include <linux/netfilter_ipv4/ip_tables.h>
1119 +#include <linux/errno.h>
1120 +#include <asm/uaccess.h>
1121 +#include <asm/bitops.h>
1122 +#include <asm/softirq.h>
1123 +#include <asm/semaphore.h>
1124 +#include <linux/spinlock.h>
1125 +#include <linux/vmalloc.h>
1127 +#define ASSERT_READ_LOCK(x) /* dont use that */
1128 +#define ASSERT_WRITE_LOCK(x)
1129 +#include <linux/netfilter_ipv4/listhelp.h>
1130 +#include <linux/netfilter_ipv4/ip_set.h>
1132 +static struct list_head set_type_list; /* all registered sets */
1133 +static struct ip_set **ip_set_list; /* all individual sets */
1134 +static DECLARE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1135 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1136 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1137 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1138 +static struct list_head *ip_set_hash; /* hash of bindings */
1139 +static unsigned int ip_set_hash_random; /* random seed */
1143 +#define __MOD_INC(foo) __MOD_INC_USE_COUNT(foo)
1144 +#define __MOD_DEC(foo) __MOD_DEC_USE_COUNT(foo)
1145 +#define __MOD_INC_SELF MOD_INC_USE_COUNT
1146 +#define __MOD_DEC_SELF MOD_DEC_USE_COUNT
1148 +#define __MOD_INC(foo)
1149 +#define __MOD_DEC(foo)
1150 +#define __MOD_INC_SELF
1151 +#define __MOD_DEC_SELF
1155 + * Sets are identified either by the index in ip_set_list or by id.
1156 + * The id never changes and is used to find a key in the hash.
1157 + * The index may change by swapping and used at all other places
1158 + * (set/SET netfilter modules, binding value, etc.)
1160 + * Userspace requests are serialized by ip_set_mutex and sets can
1161 + * be deleted only from userspace. Therefore ip_set_list locking
1162 + * must obey the following rules:
1164 + * - kernel requests: read and write locking mandatory
1165 + * - user requests: read locking optional, write locking mandatory
1169 +__ip_set_get(ip_set_id_t index)
1171 + atomic_inc(&ip_set_list[index]->ref);
1175 +__ip_set_put(ip_set_id_t index)
1177 + atomic_dec(&ip_set_list[index]->ref);
1181 + * Binding routines
1185 +ip_hash_cmp(const struct ip_set_hash *set_hash,
1186 + ip_set_id_t id, ip_set_ip_t ip)
1188 + return set_hash->id == id && set_hash->ip == ip;
1192 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1194 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1195 + % ip_set_bindings_hash_size;
1196 + struct ip_set_hash *set_hash;
1198 + MUST_BE_READ_LOCKED(&ip_set_lock);
1199 + IP_SET_ASSERT(ip_set_list[id]);
1200 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1202 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1203 + struct ip_set_hash *, id, ip);
1205 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1207 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1209 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1213 +__set_hash_del(struct ip_set_hash *set_hash)
1215 + MUST_BE_WRITE_LOCKED(&ip_set_lock);
1216 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1218 + __ip_set_put(set_hash->binding);
1219 + list_del(&set_hash->list);
1224 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1226 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1227 + % ip_set_bindings_hash_size;
1228 + struct ip_set_hash *set_hash;
1230 + IP_SET_ASSERT(ip_set_list[id]);
1231 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1232 + WRITE_LOCK(&ip_set_lock);
1233 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1234 + struct ip_set_hash *, id, ip);
1235 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1237 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1239 + if (set_hash != NULL)
1240 + __set_hash_del(set_hash);
1241 + WRITE_UNLOCK(&ip_set_lock);
1246 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1248 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1249 + % ip_set_bindings_hash_size;
1250 + struct ip_set_hash *set_hash;
1253 + IP_SET_ASSERT(ip_set_list[id]);
1254 + IP_SET_ASSERT(ip_set_list[binding]);
1255 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1256 + HIPQUAD(ip), ip_set_list[binding]->name);
1257 + WRITE_LOCK(&ip_set_lock);
1258 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1259 + struct ip_set_hash *, id, ip);
1261 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_KERNEL);
1266 + INIT_LIST_HEAD(&set_hash->list);
1267 + set_hash->id = id;
1268 + set_hash->ip = ip;
1269 + list_add(&ip_set_hash[key], &set_hash->list);
1271 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1272 + DP("overwrite binding: %s",
1273 + ip_set_list[set_hash->binding]->name);
1274 + __ip_set_put(set_hash->binding);
1276 + set_hash->binding = binding;
1277 + __ip_set_get(set_hash->binding);
1279 + WRITE_UNLOCK(&ip_set_lock);
1283 +#define FOREACH_HASH_DO(fn, args...) \
1285 + ip_set_id_t __key; \
1286 + struct ip_set_hash *__set_hash; \
1288 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1289 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1290 + fn(__set_hash , ## args); \
1294 +#define FOREACH_HASH_RW_DO(fn, args...) \
1296 + ip_set_id_t __key; \
1297 + struct ip_set_hash *__set_hash, *__n; \
1299 + MUST_BE_WRITE_LOCKED(&ip_set_lock); \
1300 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1301 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1302 + fn(__set_hash , ## args); \
1306 +/* Add, del and test set entries from kernel */
1308 +#define follow_bindings(index, set, ip) \
1309 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1310 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1313 +ip_set_testip_kernel(ip_set_id_t index,
1314 + const struct sk_buff *skb,
1315 + const u_int32_t *flags)
1317 + struct ip_set *set;
1321 + IP_SET_ASSERT(flags[i]);
1322 + READ_LOCK(&ip_set_lock);
1324 + set = ip_set_list[index];
1325 + IP_SET_ASSERT(set);
1326 + DP("set %s, index %u", set->name, index);
1327 + read_lock_bh(&set->lock);
1328 + res = set->type->testip_kernel(set, skb, flags[i], &ip);
1329 + read_unlock_bh(&set->lock);
1332 + && follow_bindings(index, set, ip));
1333 + READ_UNLOCK(&ip_set_lock);
1339 +ip_set_addip_kernel(ip_set_id_t index,
1340 + const struct sk_buff *skb,
1341 + const u_int32_t *flags)
1343 + struct ip_set *set;
1347 + IP_SET_ASSERT(flags[i]);
1349 + READ_LOCK(&ip_set_lock);
1351 + set = ip_set_list[index];
1352 + IP_SET_ASSERT(set);
1353 + DP("set %s, index %u", set->name, index);
1354 + write_lock_bh(&set->lock);
1355 + res = set->type->addip_kernel(set, skb, flags[i], &ip);
1356 + write_unlock_bh(&set->lock);
1357 + } while ((res == 0 || res == -EEXIST)
1359 + && follow_bindings(index, set, ip));
1360 + READ_UNLOCK(&ip_set_lock);
1362 + if (res == -EAGAIN
1363 + && set->type->retry
1364 + && (res = set->type->retry(set)) == 0)
1369 +ip_set_delip_kernel(ip_set_id_t index,
1370 + const struct sk_buff *skb,
1371 + const u_int32_t *flags)
1373 + struct ip_set *set;
1377 + IP_SET_ASSERT(flags[i]);
1378 + READ_LOCK(&ip_set_lock);
1380 + set = ip_set_list[index];
1381 + IP_SET_ASSERT(set);
1382 + DP("set %s, index %u", set->name, index);
1383 + write_lock_bh(&set->lock);
1384 + res = set->type->delip_kernel(set, skb, flags[i], &ip);
1385 + write_unlock_bh(&set->lock);
1386 + } while ((res == 0 || res == -EEXIST)
1388 + && follow_bindings(index, set, ip));
1389 + READ_UNLOCK(&ip_set_lock);
1392 +/* Register and deregister settype */
1395 +set_type_equal(const struct ip_set_type *set_type, const char *str2)
1397 + return !strncmp(set_type->typename, str2, IP_SET_MAXNAMELEN - 1);
1400 +static inline struct ip_set_type *
1401 +find_set_type(const char *name)
1403 + return LIST_FIND(&set_type_list,
1405 + struct ip_set_type *,
1410 +ip_set_register_set_type(struct ip_set_type *set_type)
1414 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1415 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1416 + set_type->typename,
1417 + set_type->protocol_version,
1418 + IP_SET_PROTOCOL_VERSION);
1422 + WRITE_LOCK(&ip_set_lock);
1423 + if (find_set_type(set_type->typename)) {
1425 + ip_set_printk("'%s' already registered!",
1426 + set_type->typename);
1431 + list_append(&set_type_list, set_type);
1432 + DP("'%s' registered.", set_type->typename);
1434 + WRITE_UNLOCK(&ip_set_lock);
1439 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1441 + WRITE_LOCK(&ip_set_lock);
1442 + if (!find_set_type(set_type->typename)) {
1443 + ip_set_printk("'%s' not registered?",
1444 + set_type->typename);
1447 + LIST_DELETE(&set_type_list, set_type);
1449 + DP("'%s' unregistered.", set_type->typename);
1451 + WRITE_UNLOCK(&ip_set_lock);
1456 + * Userspace routines
1460 + * Find set by name, reference it once. The reference makes sure the
1461 + * thing pointed to, does not go away under our feet. Drop the reference
1462 + * later, using ip_set_put().
1465 +ip_set_get_byname(const char *name)
1467 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1469 + down(&ip_set_app_mutex);
1470 + for (i = 0; i < ip_set_max; i++) {
1471 + if (ip_set_list[i] != NULL
1472 + && strcmp(ip_set_list[i]->name, name) == 0) {
1478 + up(&ip_set_app_mutex);
1483 + * Find set by index, reference it once. The reference makes sure the
1484 + * thing pointed to, does not go away under our feet. Drop the reference
1485 + * later, using ip_set_put().
1488 +ip_set_get_byindex(ip_set_id_t index)
1490 + down(&ip_set_app_mutex);
1492 + if (index >= ip_set_max)
1493 + return IP_SET_INVALID_ID;
1495 + if (ip_set_list[index])
1496 + __ip_set_get(index);
1498 + index = IP_SET_INVALID_ID;
1500 + up(&ip_set_app_mutex);
1505 + * If the given set pointer points to a valid set, decrement
1506 + * reference count by 1. The caller shall not assume the index
1507 + * to be valid, after calling this function.
1509 +void ip_set_put(ip_set_id_t index)
1511 + down(&ip_set_app_mutex);
1512 + if (ip_set_list[index])
1513 + __ip_set_put(index);
1514 + up(&ip_set_app_mutex);
1517 +/* Find a set by name or index */
1519 +ip_set_find_byname(const char *name)
1521 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1523 + for (i = 0; i < ip_set_max; i++) {
1524 + if (ip_set_list[i] != NULL
1525 + && strcmp(ip_set_list[i]->name, name) == 0) {
1534 +ip_set_find_byindex(ip_set_id_t index)
1536 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1537 + index = IP_SET_INVALID_ID;
1543 + * Add, del, test, bind and unbind
1547 +__ip_set_testip(struct ip_set *set,
1554 + read_lock_bh(&set->lock);
1555 + res = set->type->testip(set, data, size, ip);
1556 + read_unlock_bh(&set->lock);
1562 +__ip_set_addip(ip_set_id_t index,
1566 + struct ip_set *set = ip_set_list[index];
1570 + IP_SET_ASSERT(set);
1572 + write_lock_bh(&set->lock);
1573 + res = set->type->addip(set, data, size, &ip);
1574 + write_unlock_bh(&set->lock);
1575 + } while (res == -EAGAIN
1576 + && set->type->retry
1577 + && (res = set->type->retry(set)) == 0);
1583 +ip_set_addip(ip_set_id_t index,
1588 + return __ip_set_addip(index,
1589 + data + sizeof(struct ip_set_req_adt),
1590 + size - sizeof(struct ip_set_req_adt));
1594 +ip_set_delip(ip_set_id_t index,
1598 + struct ip_set *set = ip_set_list[index];
1602 + IP_SET_ASSERT(set);
1603 + write_lock_bh(&set->lock);
1604 + res = set->type->delip(set,
1605 + data + sizeof(struct ip_set_req_adt),
1606 + size - sizeof(struct ip_set_req_adt),
1608 + write_unlock_bh(&set->lock);
1614 +ip_set_testip(ip_set_id_t index,
1618 + struct ip_set *set = ip_set_list[index];
1622 + IP_SET_ASSERT(set);
1623 + res = __ip_set_testip(set,
1624 + data + sizeof(struct ip_set_req_adt),
1625 + size - sizeof(struct ip_set_req_adt),
1628 + return (res > 0 ? -EEXIST : res);
1632 +ip_set_bindip(ip_set_id_t index,
1636 + struct ip_set *set = ip_set_list[index];
1637 + struct ip_set_req_bind *req_bind;
1638 + ip_set_id_t binding;
1642 + IP_SET_ASSERT(set);
1643 + if (size < sizeof(struct ip_set_req_bind))
1646 + req_bind = (struct ip_set_req_bind *) data;
1647 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1649 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1650 + /* Default binding of a set */
1651 + char *binding_name;
1653 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1656 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1657 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1659 + binding = ip_set_find_byname(binding_name);
1660 + if (binding == IP_SET_INVALID_ID)
1663 + WRITE_LOCK(&ip_set_lock);
1664 + /* Sets as binding values are referenced */
1665 + if (set->binding != IP_SET_INVALID_ID)
1666 + __ip_set_put(set->binding);
1667 + set->binding = binding;
1668 + __ip_set_get(set->binding);
1669 + WRITE_UNLOCK(&ip_set_lock);
1673 + binding = ip_set_find_byname(req_bind->binding);
1674 + if (binding == IP_SET_INVALID_ID)
1677 + res = __ip_set_testip(set,
1678 + data + sizeof(struct ip_set_req_bind),
1679 + size - sizeof(struct ip_set_req_bind),
1681 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1682 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1685 + res = ip_set_hash_add(set->id, ip, binding);
1690 +#define FOREACH_SET_DO(fn, args...) \
1692 + ip_set_id_t __i; \
1693 + struct ip_set *__set; \
1695 + for (__i = 0; __i < ip_set_max; __i++) { \
1696 + __set = ip_set_list[__i]; \
1697 + if (__set != NULL) \
1698 + fn(__set , ##args); \
1703 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1705 + if (set_hash->id == id)
1706 + __set_hash_del(set_hash);
1710 +__unbind_default(struct ip_set *set)
1712 + if (set->binding != IP_SET_INVALID_ID) {
1713 + /* Sets as binding values are referenced */
1714 + __ip_set_put(set->binding);
1715 + set->binding = IP_SET_INVALID_ID;
1720 +ip_set_unbindip(ip_set_id_t index,
1724 + struct ip_set *set;
1725 + struct ip_set_req_bind *req_bind;
1730 + if (size < sizeof(struct ip_set_req_bind))
1733 + req_bind = (struct ip_set_req_bind *) data;
1734 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1736 + DP("%u %s", index, req_bind->binding);
1737 + if (index == IP_SET_INVALID_ID) {
1738 + /* unbind :all: */
1739 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1740 + /* Default binding of sets */
1741 + WRITE_LOCK(&ip_set_lock);
1742 + FOREACH_SET_DO(__unbind_default);
1743 + WRITE_UNLOCK(&ip_set_lock);
1745 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1746 + /* Flush all bindings of all sets*/
1747 + WRITE_LOCK(&ip_set_lock);
1748 + FOREACH_HASH_RW_DO(__set_hash_del);
1749 + WRITE_UNLOCK(&ip_set_lock);
1752 + DP("unreachable reached!");
1756 + set = ip_set_list[index];
1757 + IP_SET_ASSERT(set);
1758 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1759 + /* Default binding of set */
1760 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1762 + if (binding == IP_SET_INVALID_ID)
1765 + WRITE_LOCK(&ip_set_lock);
1766 + /* Sets in hash values are referenced */
1767 + __ip_set_put(set->binding);
1768 + set->binding = IP_SET_INVALID_ID;
1769 + WRITE_UNLOCK(&ip_set_lock);
1772 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1773 + /* Flush all bindings */
1775 + WRITE_LOCK(&ip_set_lock);
1776 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1777 + WRITE_UNLOCK(&ip_set_lock);
1781 + res = __ip_set_testip(set,
1782 + data + sizeof(struct ip_set_req_bind),
1783 + size - sizeof(struct ip_set_req_bind),
1786 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1788 + res = ip_set_hash_del(set->id, ip);
1794 +ip_set_testbind(ip_set_id_t index,
1798 + struct ip_set *set = ip_set_list[index];
1799 + struct ip_set_req_bind *req_bind;
1800 + ip_set_id_t binding;
1804 + IP_SET_ASSERT(set);
1805 + if (size < sizeof(struct ip_set_req_bind))
1808 + req_bind = (struct ip_set_req_bind *) data;
1809 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1811 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1812 + /* Default binding of set */
1813 + char *binding_name;
1815 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1818 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1819 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1821 + binding = ip_set_find_byname(binding_name);
1822 + if (binding == IP_SET_INVALID_ID)
1825 + res = (set->binding == binding) ? -EEXIST : 0;
1829 + binding = ip_set_find_byname(req_bind->binding);
1830 + if (binding == IP_SET_INVALID_ID)
1834 + res = __ip_set_testip(set,
1835 + data + sizeof(struct ip_set_req_bind),
1836 + size - sizeof(struct ip_set_req_bind),
1838 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1839 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1842 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1848 +static struct ip_set_type *
1849 +find_set_type_rlock(const char *typename)
1851 + struct ip_set_type *type;
1853 + READ_LOCK(&ip_set_lock);
1854 + type = find_set_type(typename);
1856 + READ_UNLOCK(&ip_set_lock);
1862 +find_free_id(const char *name,
1863 + ip_set_id_t *index,
1868 + *id = IP_SET_INVALID_ID;
1869 + for (i = 0; i < ip_set_max; i++) {
1870 + if (ip_set_list[i] == NULL) {
1871 + if (*id == IP_SET_INVALID_ID)
1873 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1877 + if (*id == IP_SET_INVALID_ID)
1878 + /* No free slot remained */
1880 + /* Check that index is usable as id (swapping) */
1882 + for (i = 0; i < ip_set_max; i++) {
1883 + if (ip_set_list[i] != NULL
1884 + && ip_set_list[i]->id == *id) {
1896 +ip_set_create(const char *name,
1897 + const char *typename,
1898 + ip_set_id_t restore,
1902 + struct ip_set *set;
1903 + ip_set_id_t index, id;
1906 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1908 + * First, and without any locks, allocate and initialize
1909 + * a normal base set structure.
1911 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1914 + set->lock = RW_LOCK_UNLOCKED;
1915 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1916 + set->binding = IP_SET_INVALID_ID;
1917 + atomic_set(&set->ref, 0);
1920 + * Next, take the &ip_set_lock, check that we know the type,
1921 + * and take a reference on the type, to make sure it
1922 + * stays available while constructing our new set.
1924 + * After referencing the type, we drop the &ip_set_lock,
1925 + * and let the new set construction run without locks.
1927 + set->type = find_set_type_rlock(typename);
1928 + if (set->type == NULL) {
1929 + /* Try loading the module */
1930 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1931 + strcpy(modulename, "ip_set_");
1932 + strcat(modulename, typename);
1933 + DP("try to load %s", modulename);
1934 + request_module(modulename);
1935 + set->type = find_set_type_rlock(typename);
1937 + if (set->type == NULL) {
1938 + ip_set_printk("no set type '%s', set '%s' not created",
1943 + __MOD_INC(set->type->me);
1944 + READ_UNLOCK(&ip_set_lock);
1947 + * Without holding any locks, create private part.
1949 + res = set->type->create(set, data, size);
1951 + __MOD_DEC(set->type->me);
1956 + /* BTW, res==0 here. */
1959 + * Here, we have a valid, constructed set. &ip_set_lock again,
1960 + * find free id/index and check that it is not already in
1963 + WRITE_LOCK(&ip_set_lock);
1964 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
1965 + DP("no free id!");
1969 + /* Make sure restore gets the same index */
1970 + if (restore != IP_SET_INVALID_ID && index != restore) {
1971 + DP("Can't restore, sets are screwed up");
1977 + * Finally! Add our shiny new set to the list, and be done.
1979 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
1981 + ip_set_list[index] = set;
1982 + WRITE_UNLOCK(&ip_set_lock);
1986 + WRITE_UNLOCK(&ip_set_lock);
1987 + set->type->destroy(set);
1988 + __MOD_DEC(set->type->me);
1994 + * Destroy a given existing set
1997 +ip_set_destroy_set(ip_set_id_t index)
1999 + struct ip_set *set = ip_set_list[index];
2001 + IP_SET_ASSERT(set);
2002 + DP("set: %s", set->name);
2003 + WRITE_LOCK(&ip_set_lock);
2004 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2005 + if (set->binding != IP_SET_INVALID_ID)
2006 + __ip_set_put(set->binding);
2007 + ip_set_list[index] = NULL;
2008 + WRITE_UNLOCK(&ip_set_lock);
2010 + /* Must call it without holding any lock */
2011 + set->type->destroy(set);
2012 + __MOD_DEC(set->type->me);
2017 + * Destroy a set - or all sets
2018 + * Sets must not be referenced/used.
2021 +ip_set_destroy(ip_set_id_t index)
2025 + /* ref modification always protected by the mutex */
2026 + if (index != IP_SET_INVALID_ID) {
2027 + if (atomic_read(&ip_set_list[index]->ref))
2029 + ip_set_destroy_set(index);
2031 + for (i = 0; i < ip_set_max; i++) {
2032 + if (ip_set_list[i] != NULL
2033 + && (atomic_read(&ip_set_list[i]->ref)))
2037 + for (i = 0; i < ip_set_max; i++) {
2038 + if (ip_set_list[i] != NULL)
2039 + ip_set_destroy_set(i);
2046 +ip_set_flush_set(struct ip_set *set)
2048 + DP("set: %s %u", set->name, set->id);
2050 + write_lock_bh(&set->lock);
2051 + set->type->flush(set);
2052 + write_unlock_bh(&set->lock);
2056 + * Flush data in a set - or in all sets
2059 +ip_set_flush(ip_set_id_t index)
2061 + if (index != IP_SET_INVALID_ID) {
2062 + IP_SET_ASSERT(ip_set_list[index]);
2063 + ip_set_flush_set(ip_set_list[index]);
2065 + FOREACH_SET_DO(ip_set_flush_set);
2072 +ip_set_rename(ip_set_id_t index, const char *name)
2074 + struct ip_set *set = ip_set_list[index];
2078 + DP("set: %s to %s", set->name, name);
2079 + WRITE_LOCK(&ip_set_lock);
2080 + for (i = 0; i < ip_set_max; i++) {
2081 + if (ip_set_list[i] != NULL
2082 + && strncmp(ip_set_list[i]->name,
2084 + IP_SET_MAXNAMELEN - 1) == 0) {
2089 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2091 + WRITE_UNLOCK(&ip_set_lock);
2096 + * Swap two sets so that name/index points to the other.
2097 + * References are also swapped.
2100 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2102 + struct ip_set *from = ip_set_list[from_index];
2103 + struct ip_set *to = ip_set_list[to_index];
2104 + char from_name[IP_SET_MAXNAMELEN];
2105 + u_int32_t from_ref;
2107 + DP("set: %s to %s", from->name, to->name);
2108 + /* Type can't be changed. Artifical restriction. */
2109 + if (from->type->typecode != to->type->typecode)
2112 + /* No magic here: ref munging protected by the mutex */
2113 + WRITE_LOCK(&ip_set_lock);
2114 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2115 + from_ref = atomic_read(&from->ref);
2117 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2118 + atomic_set(&from->ref, atomic_read(&to->ref));
2119 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2120 + atomic_set(&to->ref, from_ref);
2122 + ip_set_list[from_index] = to;
2123 + ip_set_list[to_index] = from;
2125 + WRITE_UNLOCK(&ip_set_lock);
2134 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2135 + ip_set_id_t id, size_t *size)
2137 + if (set_hash->id == id)
2138 + *size += sizeof(struct ip_set_hash_list);
2142 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2143 + ip_set_id_t id, size_t *size)
2145 + if (set_hash->id == id)
2146 + *size += sizeof(struct ip_set_hash_save);
2150 +__set_hash_bindings(struct ip_set_hash *set_hash,
2151 + ip_set_id_t id, void *data, int *used)
2153 + if (set_hash->id == id) {
2154 + struct ip_set_hash_list *hash_list =
2155 + (struct ip_set_hash_list *)(data + *used);
2157 + hash_list->ip = set_hash->ip;
2158 + hash_list->binding = set_hash->binding;
2159 + *used += sizeof(struct ip_set_hash_list);
2163 +static int ip_set_list_set(ip_set_id_t index,
2168 + struct ip_set *set = ip_set_list[index];
2169 + struct ip_set_list *set_list;
2171 + /* Pointer to our header */
2172 + set_list = (struct ip_set_list *) (data + *used);
2174 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2176 + /* Get and ensure header size */
2177 + if (*used + sizeof(struct ip_set_list) > len)
2178 + goto not_enough_mem;
2179 + *used += sizeof(struct ip_set_list);
2181 + read_lock_bh(&set->lock);
2182 + /* Get and ensure set specific header size */
2183 + set_list->header_size = set->type->header_size;
2184 + if (*used + set_list->header_size > len)
2187 + /* Fill in the header */
2188 + set_list->index = index;
2189 + set_list->binding = set->binding;
2190 + set_list->ref = atomic_read(&set->ref);
2192 + /* Fill in set spefific header data */
2193 + DP("call list_header");
2194 + set->type->list_header(set, data + *used);
2195 + DP("call list_header, done");
2196 + *used += set_list->header_size;
2198 + /* Get and ensure set specific members size */
2199 + DP("call list_members_size");
2200 + set_list->members_size = set->type->list_members_size(set);
2201 + DP("call list_members_size, done");
2202 + if (*used + set_list->members_size > len)
2205 + /* Fill in set spefific members data */
2206 + DP("call list_members");
2207 + set->type->list_members(set, data + *used);
2208 + DP("call list_members, done");
2209 + *used += set_list->members_size;
2210 + read_unlock_bh(&set->lock);
2214 + /* Get and ensure set specific bindings size */
2215 + set_list->bindings_size = 0;
2216 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2217 + set->id, &set_list->bindings_size);
2218 + if (*used + set_list->bindings_size > len)
2219 + goto not_enough_mem;
2221 + /* Fill in set spefific bindings data */
2222 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2227 + read_unlock_bh(&set->lock);
2229 + DP("not enough mem, try again");
2236 +static int ip_set_save_set(ip_set_id_t index,
2241 + struct ip_set *set;
2242 + struct ip_set_save *set_save;
2244 + /* Pointer to our header */
2245 + set_save = (struct ip_set_save *) (data + *used);
2247 + /* Get and ensure header size */
2248 + if (*used + sizeof(struct ip_set_save) > len)
2249 + goto not_enough_mem;
2250 + *used += sizeof(struct ip_set_save);
2252 + set = ip_set_list[index];
2253 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2254 + data, data + *used);
2256 + read_lock_bh(&set->lock);
2257 + /* Get and ensure set specific header size */
2258 + set_save->header_size = set->type->header_size;
2259 + if (*used + set_save->header_size > len)
2262 + /* Fill in the header */
2263 + set_save->index = index;
2264 + set_save->binding = set->binding;
2266 + /* Fill in set spefific header data */
2267 + set->type->list_header(set, data + *used);
2268 + *used += set_save->header_size;
2270 + DP("set header filled: %s, used: %u %p %p", set->name, *used,
2271 + data, data + *used);
2272 + /* Get and ensure set specific members size */
2273 + set_save->members_size = set->type->list_members_size(set);
2274 + if (*used + set_save->members_size > len)
2277 + /* Fill in set spefific members data */
2278 + set->type->list_members(set, data + *used);
2279 + *used += set_save->members_size;
2280 + read_unlock_bh(&set->lock);
2281 + DP("set members filled: %s, used: %u %p %p", set->name, *used,
2282 + data, data + *used);
2286 + read_unlock_bh(&set->lock);
2288 + DP("not enough mem, try again");
2293 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2301 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2302 + struct ip_set_hash_save *hash_save =
2303 + (struct ip_set_hash_save *)(data + *used);
2304 + /* Ensure bindings size */
2305 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2309 + hash_save->id = set_hash->id;
2310 + hash_save->ip = set_hash->ip;
2311 + hash_save->binding = set_hash->binding;
2312 + *used += sizeof(struct ip_set_hash_save);
2316 +static int ip_set_save_bindings(ip_set_id_t index,
2322 + struct ip_set_save *set_save;
2324 + DP("used %u, len %u", *used, len);
2325 + /* Get and ensure header size */
2326 + if (*used + sizeof(struct ip_set_save) > len)
2330 + set_save = (struct ip_set_save *) (data + *used);
2331 + set_save->index = IP_SET_INVALID_ID;
2332 + *used += sizeof(struct ip_set_save);
2334 + DP("marker added used %u, len %u", *used, len);
2335 + /* Fill in bindings data */
2336 + if (index != IP_SET_INVALID_ID)
2337 + /* Sets are identified by id in hash */
2338 + index = ip_set_list[index]->id;
2339 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2347 +static int ip_set_restore(void *data,
2351 + int line = 0, used = 0, members_size;
2352 + struct ip_set *set;
2353 + struct ip_set_hash_save *hash_save;
2354 + struct ip_set_restore *set_restore;
2355 + ip_set_id_t index;
2357 + /* Loop to restore sets */
2361 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2362 + /* Get and ensure header size */
2363 + if (used + sizeof(struct ip_set_restore) > len)
2365 + set_restore = (struct ip_set_restore *) (data + used);
2366 + used += sizeof(struct ip_set_restore);
2368 + /* Ensure data size */
2370 + + set_restore->header_size
2371 + + set_restore->members_size > len)
2374 + /* Check marker */
2375 + if (set_restore->index == IP_SET_INVALID_ID) {
2380 + /* Try to create the set */
2381 + DP("restore %s %s", set_restore->name, set_restore->typename);
2382 + res = ip_set_create(set_restore->name,
2383 + set_restore->typename,
2384 + set_restore->index,
2386 + set_restore->header_size);
2390 + used += set_restore->header_size;
2392 + index = ip_set_find_byindex(set_restore->index);
2393 + DP("index %u, restore_index %u", index, set_restore->index);
2394 + if (index != set_restore->index)
2396 + /* Try to restore members data */
2397 + set = ip_set_list[index];
2399 + DP("members_size %u reqsize %u",
2400 + set_restore->members_size, set->type->reqsize);
2401 + while (members_size + set->type->reqsize <=
2402 + set_restore->members_size) {
2404 + DP("members: %u, line %u", members_size, line);
2405 + res = __ip_set_addip(index,
2406 + data + used + members_size,
2407 + set->type->reqsize);
2408 + if (!(res == 0 || res == -EEXIST))
2410 + members_size += set->type->reqsize;
2413 + DP("members_size %u %u",
2414 + set_restore->members_size, members_size);
2415 + if (members_size != set_restore->members_size)
2417 + used += set_restore->members_size;
2421 + /* Loop to restore bindings */
2422 + while (used < len) {
2425 + DP("restore binding, line %u", line);
2426 + /* Get and ensure size */
2427 + if (used + sizeof(struct ip_set_hash_save) > len)
2429 + hash_save = (struct ip_set_hash_save *) (data + used);
2430 + used += sizeof(struct ip_set_hash_save);
2432 + /* hash_save->id is used to store the index */
2433 + index = ip_set_find_byindex(hash_save->id);
2434 + DP("restore binding index %u, id %u, %u -> %u",
2435 + index, hash_save->id, hash_save->ip, hash_save->binding);
2436 + if (index != hash_save->id)
2439 + set = ip_set_list[hash_save->id];
2440 + /* Null valued IP means default binding */
2441 + if (hash_save->ip)
2442 + res = ip_set_hash_add(set->id,
2444 + hash_save->binding);
2446 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2447 + WRITE_LOCK(&ip_set_lock);
2448 + set->binding = hash_save->binding;
2449 + __ip_set_get(set->binding);
2450 + WRITE_UNLOCK(&ip_set_lock);
2451 + DP("default binding: %u", set->binding);
2463 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2466 + int res = 0; /* Assume OK */
2468 + struct ip_set_req_adt *req_adt;
2469 + ip_set_id_t index = IP_SET_INVALID_ID;
2470 + int (*adtfn)(ip_set_id_t index,
2471 + const void *data, size_t size);
2473 + int (*fn)(ip_set_id_t index,
2474 + const void *data, size_t size);
2476 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2477 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2480 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2481 + if (!capable(CAP_NET_ADMIN))
2483 + if (optval != SO_IP_SET)
2485 + if (len <= sizeof(unsigned)) {
2486 + ip_set_printk("short userdata (want >%zu, got %u)",
2487 + sizeof(unsigned), len);
2490 + data = vmalloc(len);
2492 + DP("out of mem for %u bytes", len);
2495 + if (copy_from_user(data, user, len) != 0) {
2499 + if (down_interruptible(&ip_set_app_mutex)) {
2504 + op = (unsigned *)data;
2507 + if (*op < IP_SET_OP_VERSION) {
2508 + /* Check the version at the beginning of operations */
2509 + struct ip_set_req_version *req_version =
2510 + (struct ip_set_req_version *) data;
2511 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2518 + case IP_SET_OP_CREATE:{
2519 + struct ip_set_req_create *req_create
2520 + = (struct ip_set_req_create *) data;
2522 + if (len <= sizeof(struct ip_set_req_create)) {
2523 + ip_set_printk("short CREATE data (want >%zu, got %u)",
2524 + sizeof(struct ip_set_req_create), len);
2528 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2529 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2530 + res = ip_set_create(req_create->name,
2531 + req_create->typename,
2532 + IP_SET_INVALID_ID,
2533 + data + sizeof(struct ip_set_req_create),
2534 + len - sizeof(struct ip_set_req_create));
2537 + case IP_SET_OP_DESTROY:{
2538 + struct ip_set_req_std *req_destroy
2539 + = (struct ip_set_req_std *) data;
2541 + if (len != sizeof(struct ip_set_req_std)) {
2542 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2543 + sizeof(struct ip_set_req_std), len);
2547 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2548 + /* Destroy all sets */
2549 + index = IP_SET_INVALID_ID;
2551 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2552 + index = ip_set_find_byname(req_destroy->name);
2554 + if (index == IP_SET_INVALID_ID) {
2560 + res = ip_set_destroy(index);
2563 + case IP_SET_OP_FLUSH:{
2564 + struct ip_set_req_std *req_flush =
2565 + (struct ip_set_req_std *) data;
2567 + if (len != sizeof(struct ip_set_req_std)) {
2568 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2569 + sizeof(struct ip_set_req_std), len);
2573 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2574 + /* Flush all sets */
2575 + index = IP_SET_INVALID_ID;
2577 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2578 + index = ip_set_find_byname(req_flush->name);
2580 + if (index == IP_SET_INVALID_ID) {
2585 + res = ip_set_flush(index);
2588 + case IP_SET_OP_RENAME:{
2589 + struct ip_set_req_create *req_rename
2590 + = (struct ip_set_req_create *) data;
2592 + if (len != sizeof(struct ip_set_req_create)) {
2593 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2594 + sizeof(struct ip_set_req_create), len);
2599 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2600 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2602 + index = ip_set_find_byname(req_rename->name);
2603 + if (index == IP_SET_INVALID_ID) {
2607 + res = ip_set_rename(index, req_rename->typename);
2610 + case IP_SET_OP_SWAP:{
2611 + struct ip_set_req_create *req_swap
2612 + = (struct ip_set_req_create *) data;
2613 + ip_set_id_t to_index;
2615 + if (len != sizeof(struct ip_set_req_create)) {
2616 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2617 + sizeof(struct ip_set_req_create), len);
2622 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2623 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2625 + index = ip_set_find_byname(req_swap->name);
2626 + if (index == IP_SET_INVALID_ID) {
2630 + to_index = ip_set_find_byname(req_swap->typename);
2631 + if (to_index == IP_SET_INVALID_ID) {
2635 + res = ip_set_swap(index, to_index);
2639 + break; /* Set identified by id */
2642 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2643 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2647 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2649 + if (len < sizeof(struct ip_set_req_adt)) {
2650 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2651 + sizeof(struct ip_set_req_adt), len);
2655 + req_adt = (struct ip_set_req_adt *) data;
2657 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2658 + if (!(*op == IP_SET_OP_UNBIND_SET
2659 + && req_adt->index == IP_SET_INVALID_ID)) {
2660 + index = ip_set_find_byindex(req_adt->index);
2661 + if (index == IP_SET_INVALID_ID) {
2666 + res = adtfn(index, data, len);
2669 + up(&ip_set_app_mutex);
2673 + DP("final result %d", res);
2678 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2682 + ip_set_id_t index = IP_SET_INVALID_ID;
2684 + int copylen = *len;
2686 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2687 + if (!capable(CAP_NET_ADMIN))
2689 + if (optval != SO_IP_SET)
2691 + if (*len < sizeof(unsigned)) {
2692 + ip_set_printk("short userdata (want >=%zu, got %d)",
2693 + sizeof(unsigned), *len);
2696 + data = vmalloc(*len);
2698 + DP("out of mem for %d bytes", *len);
2701 + if (copy_from_user(data, user, *len) != 0) {
2705 + if (down_interruptible(&ip_set_app_mutex)) {
2710 + op = (unsigned *) data;
2713 + if (*op < IP_SET_OP_VERSION) {
2714 + /* Check the version at the beginning of operations */
2715 + struct ip_set_req_version *req_version =
2716 + (struct ip_set_req_version *) data;
2717 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2724 + case IP_SET_OP_VERSION: {
2725 + struct ip_set_req_version *req_version =
2726 + (struct ip_set_req_version *) data;
2728 + if (*len != sizeof(struct ip_set_req_version)) {
2729 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2730 + sizeof(struct ip_set_req_version),
2736 + req_version->version = IP_SET_PROTOCOL_VERSION;
2737 + res = copy_to_user(user, req_version,
2738 + sizeof(struct ip_set_req_version));
2741 + case IP_SET_OP_GET_BYNAME: {
2742 + struct ip_set_req_get_set *req_get
2743 + = (struct ip_set_req_get_set *) data;
2745 + if (*len != sizeof(struct ip_set_req_get_set)) {
2746 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2747 + sizeof(struct ip_set_req_get_set), *len);
2751 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2752 + index = ip_set_find_byname(req_get->set.name);
2753 + req_get->set.index = index;
2756 + case IP_SET_OP_GET_BYINDEX: {
2757 + struct ip_set_req_get_set *req_get
2758 + = (struct ip_set_req_get_set *) data;
2760 + if (*len != sizeof(struct ip_set_req_get_set)) {
2761 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2762 + sizeof(struct ip_set_req_get_set), *len);
2766 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2767 + index = ip_set_find_byindex(req_get->set.index);
2768 + strncpy(req_get->set.name,
2769 + index == IP_SET_INVALID_ID ? ""
2770 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2773 + case IP_SET_OP_ADT_GET: {
2774 + struct ip_set_req_adt_get *req_get
2775 + = (struct ip_set_req_adt_get *) data;
2777 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2778 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2779 + sizeof(struct ip_set_req_adt_get), *len);
2783 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2784 + index = ip_set_find_byname(req_get->set.name);
2785 + if (index != IP_SET_INVALID_ID) {
2786 + req_get->set.index = index;
2787 + strncpy(req_get->typename,
2788 + ip_set_list[index]->type->typename,
2789 + IP_SET_MAXNAMELEN - 1);
2796 + case IP_SET_OP_MAX_SETS: {
2797 + struct ip_set_req_max_sets *req_max_sets
2798 + = (struct ip_set_req_max_sets *) data;
2801 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2802 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2803 + sizeof(struct ip_set_req_max_sets), *len);
2808 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2809 + req_max_sets->set.index = IP_SET_INVALID_ID;
2811 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2812 + req_max_sets->set.index =
2813 + ip_set_find_byname(req_max_sets->set.name);
2814 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2819 + req_max_sets->max_sets = ip_set_max;
2820 + req_max_sets->sets = 0;
2821 + for (i = 0; i < ip_set_max; i++) {
2822 + if (ip_set_list[i] != NULL)
2823 + req_max_sets->sets++;
2827 + case IP_SET_OP_LIST_SIZE:
2828 + case IP_SET_OP_SAVE_SIZE: {
2829 + struct ip_set_req_setnames *req_setnames
2830 + = (struct ip_set_req_setnames *) data;
2831 + struct ip_set_name_list *name_list;
2832 + struct ip_set *set;
2836 + if (*len < sizeof(struct ip_set_req_setnames)) {
2837 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2838 + sizeof(struct ip_set_req_setnames), *len);
2843 + req_setnames->size = 0;
2844 + used = sizeof(struct ip_set_req_setnames);
2845 + for (i = 0; i < ip_set_max; i++) {
2846 + if (ip_set_list[i] == NULL)
2848 + name_list = (struct ip_set_name_list *)
2850 + used += sizeof(struct ip_set_name_list);
2851 + if (used > copylen) {
2855 + set = ip_set_list[i];
2856 + /* Fill in index, name, etc. */
2857 + name_list->index = i;
2858 + name_list->id = set->id;
2859 + strncpy(name_list->name,
2861 + IP_SET_MAXNAMELEN - 1);
2862 + strncpy(name_list->typename,
2863 + set->type->typename,
2864 + IP_SET_MAXNAMELEN - 1);
2865 + DP("filled %s of type %s, index %u\n",
2866 + name_list->name, name_list->typename,
2867 + name_list->index);
2868 + if (!(req_setnames->index == IP_SET_INVALID_ID
2869 + || req_setnames->index == i))
2873 + case IP_SET_OP_LIST_SIZE: {
2874 + req_setnames->size += sizeof(struct ip_set_list)
2875 + + set->type->header_size
2876 + + set->type->list_members_size(set);
2877 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2878 + i, &req_setnames->size);
2881 + case IP_SET_OP_SAVE_SIZE: {
2882 + req_setnames->size += sizeof(struct ip_set_save)
2883 + + set->type->header_size
2884 + + set->type->list_members_size(set);
2885 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2886 + i, &req_setnames->size);
2893 + if (copylen != used) {
2899 + case IP_SET_OP_LIST: {
2900 + struct ip_set_req_list *req_list
2901 + = (struct ip_set_req_list *) data;
2905 + if (*len < sizeof(struct ip_set_req_list)) {
2906 + ip_set_printk("short LIST (want >=%zu, got %d)",
2907 + sizeof(struct ip_set_req_list), *len);
2911 + index = req_list->index;
2912 + if (index != IP_SET_INVALID_ID
2913 + && ip_set_find_byindex(index) != index) {
2918 + if (index == IP_SET_INVALID_ID) {
2919 + /* List all sets */
2920 + for (i = 0; i < ip_set_max && res == 0; i++) {
2921 + if (ip_set_list[i] != NULL)
2922 + res = ip_set_list_set(i, data, &used, *len);
2925 + /* List an individual set */
2926 + res = ip_set_list_set(index, data, &used, *len);
2930 + else if (copylen != used) {
2936 + case IP_SET_OP_SAVE: {
2937 + struct ip_set_req_list *req_save
2938 + = (struct ip_set_req_list *) data;
2942 + if (*len < sizeof(struct ip_set_req_list)) {
2943 + ip_set_printk("short SAVE (want >=%zu, got %d)",
2944 + sizeof(struct ip_set_req_list), *len);
2948 + index = req_save->index;
2949 + if (index != IP_SET_INVALID_ID
2950 + && ip_set_find_byindex(index) != index) {
2955 + if (index == IP_SET_INVALID_ID) {
2956 + /* Save all sets */
2957 + for (i = 0; i < ip_set_max && res == 0; i++) {
2958 + if (ip_set_list[i] != NULL)
2959 + res = ip_set_save_set(i, data, &used, *len);
2962 + /* Save an individual set */
2963 + res = ip_set_save_set(index, data, &used, *len);
2966 + res = ip_set_save_bindings(index, data, &used, *len);
2970 + else if (copylen != used) {
2976 + case IP_SET_OP_RESTORE: {
2977 + struct ip_set_req_setnames *req_restore
2978 + = (struct ip_set_req_setnames *) data;
2981 + if (*len < sizeof(struct ip_set_req_setnames)
2982 + || *len != req_restore->size) {
2983 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
2984 + req_restore->size, *len);
2988 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
2989 + req_restore->size - sizeof(struct ip_set_req_setnames));
2990 + DP("ip_set_restore: %u", line);
2993 + req_restore->size = line;
2994 + copylen = sizeof(struct ip_set_req_setnames);
3002 + } /* end of switch(op) */
3005 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3006 + && ip_set_list[index]
3007 + ? ip_set_list[index]->name
3008 + : ":all:", copylen);
3010 + res = copy_to_user(user, data, copylen);
3012 + copy_to_user(user, data, copylen);
3015 + up(&ip_set_app_mutex);
3019 + DP("final result %d", res);
3023 +static struct nf_sockopt_ops so_set = {
3025 + .set_optmin = SO_IP_SET,
3026 + .set_optmax = SO_IP_SET + 1,
3027 + .set = &ip_set_sockfn_set,
3028 + .get_optmin = SO_IP_SET,
3029 + .get_optmax = SO_IP_SET + 1,
3030 + .get = &ip_set_sockfn_get,
3034 +static int max_sets, hash_size;
3035 +MODULE_PARM(max_sets, "i");
3036 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3037 +MODULE_PARM(hash_size, "i");
3038 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3039 +MODULE_LICENSE("GPL");
3040 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3041 +MODULE_DESCRIPTION("module implementing core IP set support");
3043 +static int __init init(void)
3048 + get_random_bytes(&ip_set_hash_random, 4);
3050 + ip_set_max = max_sets;
3051 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3052 + if (!ip_set_list) {
3053 + printk(KERN_ERR "Unable to create ip_set_list\n");
3056 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3058 + ip_set_bindings_hash_size = hash_size;
3059 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3060 + if (!ip_set_hash) {
3061 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3062 + vfree(ip_set_list);
3065 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3066 + INIT_LIST_HEAD(&ip_set_hash[i]);
3068 + INIT_LIST_HEAD(&set_type_list);
3070 + res = nf_register_sockopt(&so_set);
3072 + ip_set_printk("SO_SET registry failed: %d", res);
3073 + vfree(ip_set_list);
3074 + vfree(ip_set_hash);
3080 +static void __exit fini(void)
3082 + /* There can't be any existing set or binding. Racy. */
3083 + nf_unregister_sockopt(&so_set);
3084 + vfree(ip_set_list);
3085 + vfree(ip_set_hash);
3086 + DP("these are the famous last words");
3089 +EXPORT_SYMBOL(ip_set_register_set_type);
3090 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3092 +EXPORT_SYMBOL(ip_set_get_byname);
3093 +EXPORT_SYMBOL(ip_set_get_byindex);
3094 +EXPORT_SYMBOL(ip_set_put);
3096 +EXPORT_SYMBOL(ip_set_addip_kernel);
3097 +EXPORT_SYMBOL(ip_set_delip_kernel);
3098 +EXPORT_SYMBOL(ip_set_testip_kernel);
3103 +++ b/net/ipv4/netfilter/ip_set_iphash.c
3105 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3107 + * This program is free software; you can redistribute it and/or modify
3108 + * it under the terms of the GNU General Public License version 2 as
3109 + * published by the Free Software Foundation.
3112 +/* Kernel module implementing an ip hash set */
3114 +#include <linux/module.h>
3115 +#include <linux/ip.h>
3116 +#include <linux/skbuff.h>
3117 +#include <linux/netfilter_ipv4/ip_tables.h>
3118 +#include <linux/netfilter_ipv4/ip_set.h>
3119 +#include <linux/errno.h>
3120 +#include <asm/uaccess.h>
3121 +#include <asm/bitops.h>
3122 +#include <asm/softirq.h>
3123 +#include <linux/spinlock.h>
3124 +#include <linux/vmalloc.h>
3125 +#include <linux/random.h>
3127 +#include <net/ip.h>
3129 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3130 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3131 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
3132 +#include <linux/netfilter_ipv4/ip_set_prime.h>
3134 +static inline __u32
3135 +jhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3137 + return jhash_1word(ip, map->initval);
3140 +static inline __u32
3141 +randhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3143 + return (1 + ip % map->prime);
3146 +static inline __u32
3147 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3149 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3150 + __u32 jhash, randhash, id;
3153 + *hash_ip = ip & map->netmask;
3154 + jhash = jhash_ip(map, *hash_ip);
3155 + randhash = randhash_ip(map, *hash_ip);
3156 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3157 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3159 + for (i = 0; i < map->probes; i++) {
3160 + id = (jhash + i * randhash) % map->hashsize;
3161 + DP("hash key: %u", id);
3162 + if (map->members[id] == *hash_ip)
3164 + /* No shortcut at testing - there can be deleted
3171 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3173 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
3177 +testip(struct ip_set *set, const void *data, size_t size,
3178 + ip_set_ip_t *hash_ip)
3180 + struct ip_set_req_iphash *req =
3181 + (struct ip_set_req_iphash *) data;
3183 + if (size != sizeof(struct ip_set_req_iphash)) {
3184 + ip_set_printk("data length wrong (want %zu, have %zu)",
3185 + sizeof(struct ip_set_req_iphash),
3189 + return __testip(set, req->ip, hash_ip);
3193 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
3194 + u_int32_t flags, ip_set_ip_t *hash_ip)
3196 + return __testip(set,
3197 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3198 + : skb->nh.iph->daddr),
3203 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3205 + __u32 jhash, randhash, probe;
3208 + *hash_ip = ip & map->netmask;
3209 + jhash = jhash_ip(map, *hash_ip);
3210 + randhash = randhash_ip(map, *hash_ip);
3212 + for (i = 0; i < map->probes; i++) {
3213 + probe = (jhash + i * randhash) % map->hashsize;
3214 + if (map->members[probe] == *hash_ip)
3216 + if (!map->members[probe]) {
3217 + map->members[probe] = *hash_ip;
3221 + /* Trigger rehashing */
3226 +addip(struct ip_set *set, const void *data, size_t size,
3227 + ip_set_ip_t *hash_ip)
3229 + struct ip_set_req_iphash *req =
3230 + (struct ip_set_req_iphash *) data;
3232 + if (size != sizeof(struct ip_set_req_iphash)) {
3233 + ip_set_printk("data length wrong (want %zu, have %zu)",
3234 + sizeof(struct ip_set_req_iphash),
3238 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3242 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3243 + u_int32_t flags, ip_set_ip_t *hash_ip)
3245 + return __addip((struct ip_set_iphash *) set->data,
3246 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3247 + : skb->nh.iph->daddr),
3251 +static int retry(struct ip_set *set)
3253 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3254 + ip_set_ip_t hash_ip, *members;
3255 + u_int32_t i, hashsize;
3256 + unsigned newbytes;
3258 + struct ip_set_iphash tmp = {
3259 + .hashsize = map->hashsize,
3260 + .probes = map->probes,
3261 + .resize = map->resize,
3262 + .netmask = map->netmask,
3265 + if (map->resize == 0)
3271 + /* Calculate new parameters */
3272 + get_random_bytes(&tmp.initval, 4);
3273 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
3274 + if (hashsize == tmp.hashsize)
3276 + tmp.prime = make_prime(hashsize);
3278 + ip_set_printk("rehashing of set %s triggered: "
3279 + "hashsize grows from %u to %u",
3280 + set->name, tmp.hashsize, hashsize);
3281 + tmp.hashsize = hashsize;
3283 + newbytes = hashsize * sizeof(ip_set_ip_t);
3284 + tmp.members = ip_set_malloc(newbytes);
3285 + if (!tmp.members) {
3286 + DP("out of memory for %d bytes", newbytes);
3289 + memset(tmp.members, 0, newbytes);
3291 + write_lock_bh(&set->lock);
3292 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3293 + for (i = 0; i < map->hashsize && res == 0; i++) {
3294 + if (map->members[i])
3295 + res = __addip(&tmp, map->members[i], &hash_ip);
3298 + /* Failure, try again */
3299 + write_unlock_bh(&set->lock);
3300 + ip_set_free(tmp.members, newbytes);
3304 + /* Success at resizing! */
3305 + members = map->members;
3306 + hashsize = map->hashsize;
3308 + map->initval = tmp.initval;
3309 + map->prime = tmp.prime;
3310 + map->hashsize = tmp.hashsize;
3311 + map->members = tmp.members;
3312 + write_unlock_bh(&set->lock);
3314 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
3320 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3322 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3323 + ip_set_ip_t id = hash_id(set, ip, hash_ip);
3325 + if (id == UINT_MAX)
3328 + map->members[id] = 0;
3333 +delip(struct ip_set *set, const void *data, size_t size,
3334 + ip_set_ip_t *hash_ip)
3336 + struct ip_set_req_iphash *req =
3337 + (struct ip_set_req_iphash *) data;
3339 + if (size != sizeof(struct ip_set_req_iphash)) {
3340 + ip_set_printk("data length wrong (want %zu, have %zu)",
3341 + sizeof(struct ip_set_req_iphash),
3345 + return __delip(set, req->ip, hash_ip);
3349 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3350 + u_int32_t flags, ip_set_ip_t *hash_ip)
3352 + return __delip(set,
3353 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3354 + : skb->nh.iph->daddr),
3358 +static int create(struct ip_set *set, const void *data, size_t size)
3360 + unsigned newbytes;
3361 + struct ip_set_req_iphash_create *req =
3362 + (struct ip_set_req_iphash_create *) data;
3363 + struct ip_set_iphash *map;
3365 + if (size != sizeof(struct ip_set_req_iphash_create)) {
3366 + ip_set_printk("data length wrong (want %zu, have %zu)",
3367 + sizeof(struct ip_set_req_iphash_create),
3372 + if (req->hashsize < 1) {
3373 + ip_set_printk("hashsize too small");
3377 + map = kmalloc(sizeof(struct ip_set_iphash), GFP_KERNEL);
3379 + DP("out of memory for %d bytes",
3380 + sizeof(struct ip_set_iphash));
3383 + get_random_bytes(&map->initval, 4);
3384 + map->prime = make_prime(req->hashsize);
3385 + map->hashsize = req->hashsize;
3386 + map->probes = req->probes;
3387 + map->resize = req->resize;
3388 + map->netmask = req->netmask;
3389 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
3390 + map->members = ip_set_malloc(newbytes);
3391 + if (!map->members) {
3392 + DP("out of memory for %d bytes", newbytes);
3396 + memset(map->members, 0, newbytes);
3402 +static void destroy(struct ip_set *set)
3404 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3406 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
3412 +static void flush(struct ip_set *set)
3414 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3415 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
3418 +static void list_header(const struct ip_set *set, void *data)
3420 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3421 + struct ip_set_req_iphash_create *header =
3422 + (struct ip_set_req_iphash_create *) data;
3424 + header->hashsize = map->hashsize;
3425 + header->probes = map->probes;
3426 + header->resize = map->resize;
3427 + header->netmask = map->netmask;
3430 +static int list_members_size(const struct ip_set *set)
3432 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3434 + return (map->hashsize * sizeof(ip_set_ip_t));
3437 +static void list_members(const struct ip_set *set, void *data)
3439 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3440 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
3442 + memcpy(data, map->members, bytes);
3445 +static struct ip_set_type ip_set_iphash = {
3446 + .typename = SETTYPE_NAME,
3447 + .typecode = IPSET_TYPE_IP,
3448 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3449 + .create = &create,
3450 + .destroy = &destroy,
3452 + .reqsize = sizeof(struct ip_set_req_iphash),
3454 + .addip_kernel = &addip_kernel,
3457 + .delip_kernel = &delip_kernel,
3458 + .testip = &testip,
3459 + .testip_kernel = &testip_kernel,
3460 + .header_size = sizeof(struct ip_set_req_iphash_create),
3461 + .list_header = &list_header,
3462 + .list_members_size = &list_members_size,
3463 + .list_members = &list_members,
3464 + .me = THIS_MODULE,
3467 +MODULE_LICENSE("GPL");
3468 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3469 +MODULE_DESCRIPTION("iphash type of IP sets");
3471 +static int __init init(void)
3473 + return ip_set_register_set_type(&ip_set_iphash);
3476 +static void __exit fini(void)
3478 + /* FIXME: possible race with ip_set_create() */
3479 + ip_set_unregister_set_type(&ip_set_iphash);
3485 +++ b/net/ipv4/netfilter/ip_set_ipmap.c
3487 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3488 + * Patrick Schaaf <bof@bof.de>
3489 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3491 + * This program is free software; you can redistribute it and/or modify
3492 + * it under the terms of the GNU General Public License version 2 as
3493 + * published by the Free Software Foundation.
3496 +/* Kernel module implementing an IP set type: the single bitmap type */
3498 +#include <linux/module.h>
3499 +#include <linux/ip.h>
3500 +#include <linux/skbuff.h>
3501 +#include <linux/netfilter_ipv4/ip_tables.h>
3502 +#include <linux/netfilter_ipv4/ip_set.h>
3503 +#include <linux/errno.h>
3504 +#include <asm/uaccess.h>
3505 +#include <asm/bitops.h>
3506 +#include <asm/softirq.h>
3507 +#include <linux/spinlock.h>
3509 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3511 +static inline ip_set_ip_t
3512 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3514 + return (ip - map->first_ip)/map->hosts;
3518 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3520 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3522 + if (ip < map->first_ip || ip > map->last_ip)
3525 + *hash_ip = ip & map->netmask;
3526 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3527 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3528 + return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3532 +testip(struct ip_set *set, const void *data, size_t size,
3533 + ip_set_ip_t *hash_ip)
3535 + struct ip_set_req_ipmap *req =
3536 + (struct ip_set_req_ipmap *) data;
3538 + if (size != sizeof(struct ip_set_req_ipmap)) {
3539 + ip_set_printk("data length wrong (want %zu, have %zu)",
3540 + sizeof(struct ip_set_req_ipmap),
3544 + return __testip(set, req->ip, hash_ip);
3548 +testip_kernel(struct ip_set *set,
3549 + const struct sk_buff *skb,
3551 + ip_set_ip_t *hash_ip)
3555 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3556 + flags & IPSET_SRC ? "SRC" : "DST",
3557 + NIPQUAD(skb->nh.iph->saddr),
3558 + NIPQUAD(skb->nh.iph->daddr));
3560 + res = __testip(set,
3561 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3562 + : skb->nh.iph->daddr),
3564 + return (res < 0 ? 0 : res);
3568 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3570 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3572 + if (ip < map->first_ip || ip > map->last_ip)
3575 + *hash_ip = ip & map->netmask;
3576 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3577 + if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3584 +addip(struct ip_set *set, const void *data, size_t size,
3585 + ip_set_ip_t *hash_ip)
3587 + struct ip_set_req_ipmap *req =
3588 + (struct ip_set_req_ipmap *) data;
3590 + if (size != sizeof(struct ip_set_req_ipmap)) {
3591 + ip_set_printk("data length wrong (want %zu, have %zu)",
3592 + sizeof(struct ip_set_req_ipmap),
3596 + DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3597 + return __addip(set, req->ip, hash_ip);
3601 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3602 + u_int32_t flags, ip_set_ip_t *hash_ip)
3604 + return __addip(set,
3605 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3606 + : skb->nh.iph->daddr),
3611 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3613 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3615 + if (ip < map->first_ip || ip > map->last_ip)
3618 + *hash_ip = ip & map->netmask;
3619 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3620 + if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3627 +delip(struct ip_set *set, const void *data, size_t size,
3628 + ip_set_ip_t *hash_ip)
3630 + struct ip_set_req_ipmap *req =
3631 + (struct ip_set_req_ipmap *) data;
3633 + if (size != sizeof(struct ip_set_req_ipmap)) {
3634 + ip_set_printk("data length wrong (want %zu, have %zu)",
3635 + sizeof(struct ip_set_req_ipmap),
3639 + return __delip(set, req->ip, hash_ip);
3643 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3644 + u_int32_t flags, ip_set_ip_t *hash_ip)
3646 + return __delip(set,
3647 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3648 + : skb->nh.iph->daddr),
3652 +static int create(struct ip_set *set, const void *data, size_t size)
3655 + struct ip_set_req_ipmap_create *req =
3656 + (struct ip_set_req_ipmap_create *) data;
3657 + struct ip_set_ipmap *map;
3659 + if (size != sizeof(struct ip_set_req_ipmap_create)) {
3660 + ip_set_printk("data length wrong (want %zu, have %zu)",
3661 + sizeof(struct ip_set_req_ipmap_create),
3666 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3667 + HIPQUAD(req->from), HIPQUAD(req->to));
3669 + if (req->from > req->to) {
3670 + DP("bad ip range");
3674 + if (req->to - req->from > MAX_RANGE) {
3675 + ip_set_printk("range too big (max %d addresses)",
3680 + map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3682 + DP("out of memory for %d bytes",
3683 + sizeof(struct ip_set_ipmap));
3686 + map->first_ip = req->from;
3687 + map->last_ip = req->to;
3688 + map->netmask = req->netmask;
3690 + if (req->netmask == 0xFFFFFFFF) {
3692 + map->sizeid = map->last_ip - map->first_ip + 1;
3694 + unsigned int mask_bits, netmask_bits;
3697 + map->first_ip &= map->netmask; /* Should we better bark? */
3699 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3700 + netmask_bits = mask_to_bits(map->netmask);
3702 + if (!mask || netmask_bits <= mask_bits)
3705 + map->hosts = 2 << (32 - netmask_bits - 1);
3706 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3708 + newbytes = bitmap_bytes(0, map->sizeid - 1);
3709 + map->members = kmalloc(newbytes, GFP_KERNEL);
3710 + if (!map->members) {
3711 + DP("out of memory for %d bytes", newbytes);
3715 + memset(map->members, 0, newbytes);
3721 +static void destroy(struct ip_set *set)
3723 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3725 + kfree(map->members);
3731 +static void flush(struct ip_set *set)
3733 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3734 + memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3737 +static void list_header(const struct ip_set *set, void *data)
3739 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3740 + struct ip_set_req_ipmap_create *header =
3741 + (struct ip_set_req_ipmap_create *) data;
3743 + header->from = map->first_ip;
3744 + header->to = map->last_ip;
3745 + header->netmask = map->netmask;
3748 +static int list_members_size(const struct ip_set *set)
3750 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3752 + return bitmap_bytes(0, map->sizeid - 1);
3755 +static void list_members(const struct ip_set *set, void *data)
3757 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3758 + int bytes = bitmap_bytes(0, map->sizeid - 1);
3760 + memcpy(data, map->members, bytes);
3763 +static struct ip_set_type ip_set_ipmap = {
3764 + .typename = SETTYPE_NAME,
3765 + .typecode = IPSET_TYPE_IP,
3766 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3767 + .create = &create,
3768 + .destroy = &destroy,
3770 + .reqsize = sizeof(struct ip_set_req_ipmap),
3772 + .addip_kernel = &addip_kernel,
3774 + .delip_kernel = &delip_kernel,
3775 + .testip = &testip,
3776 + .testip_kernel = &testip_kernel,
3777 + .header_size = sizeof(struct ip_set_req_ipmap_create),
3778 + .list_header = &list_header,
3779 + .list_members_size = &list_members_size,
3780 + .list_members = &list_members,
3781 + .me = THIS_MODULE,
3784 +MODULE_LICENSE("GPL");
3785 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3786 +MODULE_DESCRIPTION("ipmap type of IP sets");
3788 +static int __init init(void)
3790 + return ip_set_register_set_type(&ip_set_ipmap);
3793 +static void __exit fini(void)
3795 + /* FIXME: possible race with ip_set_create() */
3796 + ip_set_unregister_set_type(&ip_set_ipmap);
3802 +++ b/net/ipv4/netfilter/ip_set_iptree.c
3804 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3806 + * This program is free software; you can redistribute it and/or modify
3807 + * it under the terms of the GNU General Public License version 2 as
3808 + * published by the Free Software Foundation.
3811 +/* Kernel module implementing an IP set type: the iptree type */
3813 +#include <linux/module.h>
3814 +#include <linux/ip.h>
3815 +#include <linux/skbuff.h>
3816 +#include <linux/slab.h>
3817 +#include <linux/delay.h>
3818 +#include <linux/netfilter_ipv4/ip_tables.h>
3819 +#include <linux/netfilter_ipv4/ip_set.h>
3820 +#include <linux/errno.h>
3821 +#include <asm/uaccess.h>
3822 +#include <asm/bitops.h>
3823 +#include <linux/interrupt.h>
3824 +#include <linux/spinlock.h>
3826 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
3828 +/* Garbage collection interval in seconds: */
3829 +#define IPTREE_GC_TIME 5*60
3830 +/* Sleep so many milliseconds before trying again
3831 + * to delete the gc timer at destroying a set */
3832 +#define IPTREE_DESTROY_SLEEP 100
3834 +static kmem_cache_t *branch_cachep;
3835 +static kmem_cache_t *leaf_cachep;
3837 +#define ABCD(a,b,c,d,addrp) do { \
3838 + a = ((unsigned char *)addrp)[3]; \
3839 + b = ((unsigned char *)addrp)[2]; \
3840 + c = ((unsigned char *)addrp)[1]; \
3841 + d = ((unsigned char *)addrp)[0]; \
3844 +#define TESTIP_WALK(map, elem, branch) do { \
3845 + if ((map)->tree[elem]) { \
3846 + branch = (map)->tree[elem]; \
3852 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3854 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3855 + struct ip_set_iptreeb *btree;
3856 + struct ip_set_iptreec *ctree;
3857 + struct ip_set_iptreed *dtree;
3858 + unsigned char a,b,c,d;
3861 + ABCD(a, b, c, d, hash_ip);
3862 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
3863 + TESTIP_WALK(map, a, btree);
3864 + TESTIP_WALK(btree, b, ctree);
3865 + TESTIP_WALK(ctree, c, dtree);
3866 + DP("%lu %lu", dtree->expires[d], jiffies);
3867 + return !!(map->timeout ? (time_after(dtree->expires[d], jiffies))
3868 + : dtree->expires[d]);
3872 +testip(struct ip_set *set, const void *data, size_t size,
3873 + ip_set_ip_t *hash_ip)
3875 + struct ip_set_req_iptree *req =
3876 + (struct ip_set_req_iptree *) data;
3878 + if (size != sizeof(struct ip_set_req_iptree)) {
3879 + ip_set_printk("data length wrong (want %zu, have %zu)",
3880 + sizeof(struct ip_set_req_iptree),
3884 + return __testip(set, req->ip, hash_ip);
3888 +testip_kernel(struct ip_set *set,
3889 + const struct sk_buff *skb,
3891 + ip_set_ip_t *hash_ip)
3895 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3896 + flags & IPSET_SRC ? "SRC" : "DST",
3897 + NIPQUAD(skb->nh.iph->saddr),
3898 + NIPQUAD(skb->nh.iph->daddr));
3900 + res = __testip(set,
3901 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3902 + : skb->nh.iph->daddr),
3904 + return (res < 0 ? 0 : res);
3907 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
3908 + if ((map)->tree[elem]) { \
3909 + DP("found %u", elem); \
3910 + branch = (map)->tree[elem]; \
3912 + branch = (type *) \
3913 + kmem_cache_alloc(cachep, GFP_KERNEL); \
3914 + if (branch == NULL) \
3916 + memset(branch, 0, sizeof(*branch)); \
3917 + (map)->tree[elem] = branch; \
3918 + DP("alloc %u", elem); \
3923 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
3924 + ip_set_ip_t *hash_ip)
3926 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3927 + struct ip_set_iptreeb *btree;
3928 + struct ip_set_iptreec *ctree;
3929 + struct ip_set_iptreed *dtree;
3930 + unsigned char a,b,c,d;
3934 + ABCD(a, b, c, d, hash_ip);
3935 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
3936 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
3937 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
3938 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
3939 + if (dtree->expires[d]
3940 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
3942 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
3943 + DP("%u %lu", d, dtree->expires[d]);
3948 +addip(struct ip_set *set, const void *data, size_t size,
3949 + ip_set_ip_t *hash_ip)
3951 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3952 + struct ip_set_req_iptree *req =
3953 + (struct ip_set_req_iptree *) data;
3955 + if (size != sizeof(struct ip_set_req_iptree)) {
3956 + ip_set_printk("data length wrong (want %zu, have %zu)",
3957 + sizeof(struct ip_set_req_iptree),
3961 + DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
3962 + return __addip(set, req->ip,
3963 + req->timeout ? req->timeout : map->timeout,
3968 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3969 + u_int32_t flags, ip_set_ip_t *hash_ip)
3971 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3973 + return __addip(set,
3974 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3975 + : skb->nh.iph->daddr),
3980 +#define DELIP_WALK(map, elem, branch) do { \
3981 + if ((map)->tree[elem]) { \
3982 + branch = (map)->tree[elem]; \
3988 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3990 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3991 + struct ip_set_iptreeb *btree;
3992 + struct ip_set_iptreec *ctree;
3993 + struct ip_set_iptreed *dtree;
3994 + unsigned char a,b,c,d;
3997 + ABCD(a, b, c, d, hash_ip);
3998 + DELIP_WALK(map, a, btree);
3999 + DELIP_WALK(btree, b, ctree);
4000 + DELIP_WALK(ctree, c, dtree);
4002 + if (dtree->expires[d]) {
4003 + dtree->expires[d] = 0;
4010 +delip(struct ip_set *set, const void *data, size_t size,
4011 + ip_set_ip_t *hash_ip)
4013 + struct ip_set_req_iptree *req =
4014 + (struct ip_set_req_iptree *) data;
4016 + if (size != sizeof(struct ip_set_req_iptree)) {
4017 + ip_set_printk("data length wrong (want %zu, have %zu)",
4018 + sizeof(struct ip_set_req_iptree),
4022 + return __delip(set, req->ip, hash_ip);
4026 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4027 + u_int32_t flags, ip_set_ip_t *hash_ip)
4029 + return __delip(set,
4030 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4031 + : skb->nh.iph->daddr),
4035 +#define LOOP_WALK_BEGIN(map, i, branch) \
4036 + for (i = 0; i < 255; i++) { \
4037 + if (!(map)->tree[i]) \
4039 + branch = (map)->tree[i]
4041 +#define LOOP_WALK_END }
4043 +static void ip_tree_gc(unsigned long ul_set)
4045 + struct ip_set *set = (void *) ul_set;
4046 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4047 + struct ip_set_iptreeb *btree;
4048 + struct ip_set_iptreec *ctree;
4049 + struct ip_set_iptreed *dtree;
4050 + unsigned char a,b,c,d;
4051 + unsigned char i,j,k;
4054 + DP("gc: %s", set->name);
4055 + write_lock_bh(&set->lock);
4056 + LOOP_WALK_BEGIN(map, a, btree);
4057 + LOOP_WALK_BEGIN(btree, b, ctree);
4058 + LOOP_WALK_BEGIN(ctree, c, dtree);
4059 + for (d = 0; d < 255; d++) {
4060 + if (dtree->expires[d]) {
4061 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4063 + dtree->expires[d], jiffies);
4065 + && time_before(dtree->expires[d], jiffies))
4066 + dtree->expires[d] = 0;
4072 + DP("gc: %s: leaf %u %u %u empty",
4073 + set->name, a, b, c);
4074 + kmem_cache_free(leaf_cachep, dtree);
4075 + ctree->tree[c] = NULL;
4077 + DP("gc: %s: leaf %u %u %u not empty",
4078 + set->name, a, b, c);
4084 + DP("gc: %s: branch %u %u empty",
4086 + kmem_cache_free(branch_cachep, ctree);
4087 + btree->tree[b] = NULL;
4089 + DP("gc: %s: branch %u %u not empty",
4096 + DP("gc: %s: branch %u empty",
4098 + kmem_cache_free(branch_cachep, btree);
4099 + map->tree[a] = NULL;
4101 + DP("gc: %s: branch %u not empty",
4106 + write_unlock_bh(&set->lock);
4108 + map->gc.expires = jiffies + map->gc_interval * HZ;
4109 + add_timer(&map->gc);
4112 +static int create(struct ip_set *set, const void *data, size_t size)
4114 + struct ip_set_req_iptree_create *req =
4115 + (struct ip_set_req_iptree_create *) data;
4116 + struct ip_set_iptree *map;
4118 + if (size != sizeof(struct ip_set_req_iptree_create)) {
4119 + ip_set_printk("data length wrong (want %zu, have %zu)",
4120 + sizeof(struct ip_set_req_iptree_create),
4125 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4127 + DP("out of memory for %d bytes",
4128 + sizeof(struct ip_set_iptree));
4131 + memset(map, 0, sizeof(*map));
4132 + map->timeout = req->timeout;
4135 + /* If there is no timeout for the entries,
4136 + * we still have to call gc because delete
4137 + * do not clean up empty branches */
4138 + map->gc_interval = IPTREE_GC_TIME;
4139 + init_timer(&map->gc);
4140 + map->gc.data = (unsigned long) set;
4141 + map->gc.function = ip_tree_gc;
4142 + map->gc.expires = jiffies + map->gc_interval * HZ;
4143 + add_timer(&map->gc);
4148 +static void __flush(struct ip_set_iptree *map)
4150 + struct ip_set_iptreeb *btree;
4151 + struct ip_set_iptreec *ctree;
4152 + struct ip_set_iptreed *dtree;
4153 + unsigned int a,b,c;
4155 + LOOP_WALK_BEGIN(map, a, btree);
4156 + LOOP_WALK_BEGIN(btree, b, ctree);
4157 + LOOP_WALK_BEGIN(ctree, c, dtree);
4158 + kmem_cache_free(leaf_cachep, dtree);
4160 + kmem_cache_free(branch_cachep, ctree);
4162 + kmem_cache_free(branch_cachep, btree);
4166 +static void destroy(struct ip_set *set)
4168 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4170 + while (!del_timer(&map->gc))
4171 + msleep(IPTREE_DESTROY_SLEEP);
4177 +static void flush(struct ip_set *set)
4179 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4180 + unsigned int timeout = map->timeout;
4183 + memset(map, 0, sizeof(*map));
4184 + map->timeout = timeout;
4187 +static void list_header(const struct ip_set *set, void *data)
4189 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4190 + struct ip_set_req_iptree_create *header =
4191 + (struct ip_set_req_iptree_create *) data;
4193 + header->timeout = map->timeout;
4196 +static int list_members_size(const struct ip_set *set)
4198 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4199 + struct ip_set_iptreeb *btree;
4200 + struct ip_set_iptreec *ctree;
4201 + struct ip_set_iptreed *dtree;
4202 + unsigned char a,b,c,d;
4203 + unsigned int count = 0;
4205 + LOOP_WALK_BEGIN(map, a, btree);
4206 + LOOP_WALK_BEGIN(btree, b, ctree);
4207 + LOOP_WALK_BEGIN(ctree, c, dtree);
4208 + for (d = 0; d < 255; d++) {
4209 + if (dtree->expires[d]
4210 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4217 + DP("members %u", count);
4218 + return (count * sizeof(struct ip_set_req_iptree));
4221 +static void list_members(const struct ip_set *set, void *data)
4223 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4224 + struct ip_set_iptreeb *btree;
4225 + struct ip_set_iptreec *ctree;
4226 + struct ip_set_iptreed *dtree;
4227 + unsigned char a,b,c,d;
4228 + size_t offset = 0;
4229 + struct ip_set_req_iptree *entry;
4231 + LOOP_WALK_BEGIN(map, a, btree);
4232 + LOOP_WALK_BEGIN(btree, b, ctree);
4233 + LOOP_WALK_BEGIN(ctree, c, dtree);
4234 + for (d = 0; d < 255; d++) {
4235 + if (dtree->expires[d]
4236 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
4237 + entry = (struct ip_set_req_iptree *)(data + offset);
4238 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
4239 + entry->timeout = !map->timeout ? 0
4240 + : (dtree->expires[d] - jiffies)/HZ;
4241 + offset += sizeof(struct ip_set_req_iptree);
4249 +static struct ip_set_type ip_set_iptree = {
4250 + .typename = SETTYPE_NAME,
4251 + .typecode = IPSET_TYPE_IP,
4252 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4253 + .create = &create,
4254 + .destroy = &destroy,
4256 + .reqsize = sizeof(struct ip_set_req_iptree),
4258 + .addip_kernel = &addip_kernel,
4260 + .delip_kernel = &delip_kernel,
4261 + .testip = &testip,
4262 + .testip_kernel = &testip_kernel,
4263 + .header_size = sizeof(struct ip_set_req_iptree_create),
4264 + .list_header = &list_header,
4265 + .list_members_size = &list_members_size,
4266 + .list_members = &list_members,
4267 + .me = THIS_MODULE,
4270 +MODULE_LICENSE("GPL");
4271 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4272 +MODULE_DESCRIPTION("iptree type of IP sets");
4274 +static int __init init(void)
4278 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
4279 + sizeof(struct ip_set_iptreeb),
4280 + 0, 0, NULL, NULL);
4281 + if (!branch_cachep) {
4282 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
4286 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
4287 + sizeof(struct ip_set_iptreed),
4288 + 0, 0, NULL, NULL);
4289 + if (!leaf_cachep) {
4290 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
4294 + ret = ip_set_register_set_type(&ip_set_iptree);
4298 + kmem_cache_destroy(leaf_cachep);
4300 + kmem_cache_destroy(branch_cachep);
4305 +static void __exit fini(void)
4307 + /* FIXME: possible race with ip_set_create() */
4308 + ip_set_unregister_set_type(&ip_set_iptree);
4309 + kmem_cache_destroy(leaf_cachep);
4310 + kmem_cache_destroy(branch_cachep);
4316 +++ b/net/ipv4/netfilter/ip_set_macipmap.c
4318 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
4319 + * Patrick Schaaf <bof@bof.de>
4320 + * Martin Josefsson <gandalf@wlug.westbo.se>
4321 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4323 + * This program is free software; you can redistribute it and/or modify
4324 + * it under the terms of the GNU General Public License version 2 as
4325 + * published by the Free Software Foundation.
4328 +/* Kernel module implementing an IP set type: the macipmap type */
4330 +#include <linux/module.h>
4331 +#include <linux/ip.h>
4332 +#include <linux/skbuff.h>
4333 +#include <linux/netfilter_ipv4/ip_tables.h>
4334 +#include <linux/netfilter_ipv4/ip_set.h>
4335 +#include <linux/errno.h>
4336 +#include <asm/uaccess.h>
4337 +#include <asm/bitops.h>
4338 +#include <asm/softirq.h>
4339 +#include <linux/spinlock.h>
4340 +#include <linux/if_ether.h>
4341 +#include <linux/vmalloc.h>
4343 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4344 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
4347 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
4349 + struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
4350 + struct ip_set_macip *table = (struct ip_set_macip *) map->members;
4351 + struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
4353 + if (size != sizeof(struct ip_set_req_macipmap)) {
4354 + ip_set_printk("data length wrong (want %zu, have %zu)",
4355 + sizeof(struct ip_set_req_macipmap),
4360 + if (req->ip < map->first_ip || req->ip > map->last_ip)
4363 + *hash_ip = req->ip;
4364 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4365 + set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
4366 + if (test_bit(IPSET_MACIP_ISSET,
4367 + &table[req->ip - map->first_ip].flags)) {
4368 + return (memcmp(req->ethernet,
4369 + &table[req->ip - map->first_ip].ethernet,
4372 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4377 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4378 + u_int32_t flags, ip_set_ip_t *hash_ip)
4380 + struct ip_set_macipmap *map =
4381 + (struct ip_set_macipmap *) set->data;
4382 + struct ip_set_macip *table =
4383 + (struct ip_set_macip *) map->members;
4386 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4387 + : skb->nh.iph->daddr);
4388 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4389 + flags & IPSET_SRC ? "SRC" : "DST",
4390 + NIPQUAD(skb->nh.iph->saddr),
4391 + NIPQUAD(skb->nh.iph->daddr));
4393 + if (ip < map->first_ip || ip > map->last_ip)
4397 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4398 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
4399 + if (test_bit(IPSET_MACIP_ISSET, &table[ip - map->first_ip].flags)) {
4400 + /* Is mac pointer valid?
4401 + * If so, compare... */
4402 + return (skb->mac.raw >= skb->head
4403 + && (skb->mac.raw + ETH_HLEN) <= skb->data
4404 + && (memcmp(skb->mac.ethernet->h_source,
4405 + &table[ip - map->first_ip].ethernet,
4408 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4412 +/* returns 0 on success */
4414 +__addip(struct ip_set *set,
4415 + ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
4417 + struct ip_set_macipmap *map =
4418 + (struct ip_set_macipmap *) set->data;
4419 + struct ip_set_macip *table =
4420 + (struct ip_set_macip *) map->members;
4422 + if (ip < map->first_ip || ip > map->last_ip)
4424 + if (test_and_set_bit(IPSET_MACIP_ISSET,
4425 + &table[ip - map->first_ip].flags))
4429 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4430 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
4435 +addip(struct ip_set *set, const void *data, size_t size,
4436 + ip_set_ip_t *hash_ip)
4438 + struct ip_set_req_macipmap *req =
4439 + (struct ip_set_req_macipmap *) data;
4441 + if (size != sizeof(struct ip_set_req_macipmap)) {
4442 + ip_set_printk("data length wrong (want %zu, have %zu)",
4443 + sizeof(struct ip_set_req_macipmap),
4447 + return __addip(set, req->ip, req->ethernet, hash_ip);
4451 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4452 + u_int32_t flags, ip_set_ip_t *hash_ip)
4456 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4457 + : skb->nh.iph->daddr);
4459 + if (!(skb->mac.raw >= skb->head
4460 + && (skb->mac.raw + ETH_HLEN) <= skb->data))
4463 + return __addip(set, ip, skb->mac.ethernet->h_source, hash_ip);
4467 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4469 + struct ip_set_macipmap *map =
4470 + (struct ip_set_macipmap *) set->data;
4471 + struct ip_set_macip *table =
4472 + (struct ip_set_macip *) map->members;
4474 + if (ip < map->first_ip || ip > map->last_ip)
4476 + if (!test_and_clear_bit(IPSET_MACIP_ISSET,
4477 + &table[ip - map->first_ip].flags))
4481 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4486 +delip(struct ip_set *set, const void *data, size_t size,
4487 + ip_set_ip_t *hash_ip)
4489 + struct ip_set_req_macipmap *req =
4490 + (struct ip_set_req_macipmap *) data;
4492 + if (size != sizeof(struct ip_set_req_macipmap)) {
4493 + ip_set_printk("data length wrong (want %zu, have %zu)",
4494 + sizeof(struct ip_set_req_macipmap),
4498 + return __delip(set, req->ip, hash_ip);
4502 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4503 + u_int32_t flags, ip_set_ip_t *hash_ip)
4505 + return __delip(set,
4506 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4507 + : skb->nh.iph->daddr),
4511 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
4513 + return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
4516 +static int create(struct ip_set *set, const void *data, size_t size)
4519 + struct ip_set_req_macipmap_create *req =
4520 + (struct ip_set_req_macipmap_create *) data;
4521 + struct ip_set_macipmap *map;
4523 + if (size != sizeof(struct ip_set_req_macipmap_create)) {
4524 + ip_set_printk("data length wrong (want %zu, have %zu)",
4525 + sizeof(struct ip_set_req_macipmap_create),
4530 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
4531 + HIPQUAD(req->from), HIPQUAD(req->to));
4533 + if (req->from > req->to) {
4534 + DP("bad ip range");
4538 + if (req->to - req->from > MAX_RANGE) {
4539 + ip_set_printk("range too big (max %d addresses)",
4544 + map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
4546 + DP("out of memory for %d bytes",
4547 + sizeof(struct ip_set_macipmap));
4550 + map->flags = req->flags;
4551 + map->first_ip = req->from;
4552 + map->last_ip = req->to;
4553 + newbytes = members_size(map->first_ip, map->last_ip);
4554 + map->members = ip_set_malloc(newbytes);
4555 + DP("members: %u %p", newbytes, map->members);
4556 + if (!map->members) {
4557 + DP("out of memory for %d bytes", newbytes);
4561 + memset(map->members, 0, newbytes);
4567 +static void destroy(struct ip_set *set)
4569 + struct ip_set_macipmap *map =
4570 + (struct ip_set_macipmap *) set->data;
4572 + ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
4578 +static void flush(struct ip_set *set)
4580 + struct ip_set_macipmap *map =
4581 + (struct ip_set_macipmap *) set->data;
4582 + memset(map->members, 0, members_size(map->first_ip, map->last_ip));
4585 +static void list_header(const struct ip_set *set, void *data)
4587 + struct ip_set_macipmap *map =
4588 + (struct ip_set_macipmap *) set->data;
4589 + struct ip_set_req_macipmap_create *header =
4590 + (struct ip_set_req_macipmap_create *) data;
4592 + DP("list_header %x %x %u", map->first_ip, map->last_ip,
4595 + header->from = map->first_ip;
4596 + header->to = map->last_ip;
4597 + header->flags = map->flags;
4600 +static int list_members_size(const struct ip_set *set)
4602 + struct ip_set_macipmap *map =
4603 + (struct ip_set_macipmap *) set->data;
4605 + DP("%u", members_size(map->first_ip, map->last_ip));
4606 + return members_size(map->first_ip, map->last_ip);
4609 +static void list_members(const struct ip_set *set, void *data)
4611 + struct ip_set_macipmap *map =
4612 + (struct ip_set_macipmap *) set->data;
4614 + int bytes = members_size(map->first_ip, map->last_ip);
4616 + DP("members: %u %p", bytes, map->members);
4617 + memcpy(data, map->members, bytes);
4620 +static struct ip_set_type ip_set_macipmap = {
4621 + .typename = SETTYPE_NAME,
4622 + .typecode = IPSET_TYPE_IP,
4623 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4624 + .create = &create,
4625 + .destroy = &destroy,
4627 + .reqsize = sizeof(struct ip_set_req_macipmap),
4629 + .addip_kernel = &addip_kernel,
4631 + .delip_kernel = &delip_kernel,
4632 + .testip = &testip,
4633 + .testip_kernel = &testip_kernel,
4634 + .header_size = sizeof(struct ip_set_req_macipmap_create),
4635 + .list_header = &list_header,
4636 + .list_members_size = &list_members_size,
4637 + .list_members = &list_members,
4638 + .me = THIS_MODULE,
4641 +MODULE_LICENSE("GPL");
4642 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4643 +MODULE_DESCRIPTION("macipmap type of IP sets");
4645 +static int __init init(void)
4647 + return ip_set_register_set_type(&ip_set_macipmap);
4650 +static void __exit fini(void)
4652 + /* FIXME: possible race with ip_set_create() */
4653 + ip_set_unregister_set_type(&ip_set_macipmap);
4659 +++ b/net/ipv4/netfilter/ip_set_nethash.c
4661 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4663 + * This program is free software; you can redistribute it and/or modify
4664 + * it under the terms of the GNU General Public License version 2 as
4665 + * published by the Free Software Foundation.
4668 +/* Kernel module implementing a cidr nethash set */
4670 +#include <linux/module.h>
4671 +#include <linux/ip.h>
4672 +#include <linux/skbuff.h>
4673 +#include <linux/netfilter_ipv4/ip_tables.h>
4674 +#include <linux/netfilter_ipv4/ip_set.h>
4675 +#include <linux/errno.h>
4676 +#include <asm/uaccess.h>
4677 +#include <asm/bitops.h>
4678 +#include <asm/softirq.h>
4679 +#include <linux/spinlock.h>
4680 +#include <linux/vmalloc.h>
4681 +#include <linux/random.h>
4683 +#include <net/ip.h>
4685 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4686 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
4687 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
4688 +#include <linux/netfilter_ipv4/ip_set_prime.h>
4690 +static inline __u32
4691 +jhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4693 + return jhash_1word(ip, map->initval);
4696 +static inline __u32
4697 +randhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4699 + return (1 + ip % map->prime);
4702 +static inline __u32
4703 +hash_id_cidr(struct ip_set_nethash *map,
4705 + unsigned char cidr,
4706 + ip_set_ip_t *hash_ip)
4708 + __u32 jhash, randhash, id;
4711 + *hash_ip = pack(ip, cidr);
4712 + jhash = jhash_ip(map, *hash_ip);
4713 + randhash = randhash_ip(map, *hash_ip);
4715 + for (i = 0; i < map->probes; i++) {
4716 + id = (jhash + i * randhash) % map->hashsize;
4717 + DP("hash key: %u", id);
4718 + if (map->members[id] == *hash_ip)
4724 +static inline __u32
4725 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4727 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4728 + __u32 id = UINT_MAX;
4731 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4732 + id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
4733 + if (id != UINT_MAX)
4740 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
4741 + ip_set_ip_t *hash_ip)
4743 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4745 + return (hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
4749 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4751 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
4755 +testip(struct ip_set *set, const void *data, size_t size,
4756 + ip_set_ip_t *hash_ip)
4758 + struct ip_set_req_nethash *req =
4759 + (struct ip_set_req_nethash *) data;
4761 + if (size != sizeof(struct ip_set_req_nethash)) {
4762 + ip_set_printk("data length wrong (want %zu, have %zu)",
4763 + sizeof(struct ip_set_req_nethash),
4767 + return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
4768 + : __testip_cidr(set, req->ip, req->cidr, hash_ip));
4772 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4773 + u_int32_t flags, ip_set_ip_t *hash_ip)
4775 + return __testip(set,
4776 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4777 + : skb->nh.iph->daddr),
4782 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
4784 + __u32 jhash, randhash, probe;
4787 + jhash = jhash_ip(map, ip);
4788 + randhash = randhash_ip(map, ip);
4790 + for (i = 0; i < map->probes; i++) {
4791 + probe = (jhash + i * randhash) % map->hashsize;
4792 + if (map->members[probe] == ip)
4794 + if (!map->members[probe]) {
4795 + map->members[probe] = ip;
4799 + /* Trigger rehashing */
4804 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4805 + ip_set_ip_t *hash_ip)
4807 + *hash_ip = pack(ip, cidr);
4808 + DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
4810 + return __addip_base(map, *hash_ip);
4814 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
4816 + unsigned char next;
4819 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4820 + if (map->cidr[i] == cidr) {
4822 + } else if (map->cidr[i] < cidr) {
4823 + next = map->cidr[i];
4824 + map->cidr[i] = cidr;
4829 + map->cidr[i] = cidr;
4833 +addip(struct ip_set *set, const void *data, size_t size,
4834 + ip_set_ip_t *hash_ip)
4836 + struct ip_set_req_nethash *req =
4837 + (struct ip_set_req_nethash *) data;
4840 + if (size != sizeof(struct ip_set_req_nethash)) {
4841 + ip_set_printk("data length wrong (want %zu, have %zu)",
4842 + sizeof(struct ip_set_req_nethash),
4846 + ret = __addip((struct ip_set_nethash *) set->data,
4847 + req->ip, req->cidr, hash_ip);
4850 + update_cidr_sizes((struct ip_set_nethash *) set->data,
4857 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4858 + u_int32_t flags, ip_set_ip_t *hash_ip)
4860 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4861 + int ret = -ERANGE;
4862 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4863 + : skb->nh.iph->daddr);
4866 + ret = __addip(map, ip, map->cidr[0], hash_ip);
4871 +static int retry(struct ip_set *set)
4873 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4874 + ip_set_ip_t *members;
4875 + u_int32_t i, hashsize;
4876 + unsigned newbytes;
4878 + struct ip_set_nethash tmp = {
4879 + .hashsize = map->hashsize,
4880 + .probes = map->probes,
4881 + .resize = map->resize
4884 + if (map->resize == 0)
4887 + memcpy(tmp.cidr, map->cidr, 30 * sizeof(unsigned char));
4891 + /* Calculate new parameters */
4892 + get_random_bytes(&tmp.initval, 4);
4893 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
4894 + if (hashsize == tmp.hashsize)
4896 + tmp.prime = make_prime(hashsize);
4898 + ip_set_printk("rehashing of set %s triggered: "
4899 + "hashsize grows from %u to %u",
4900 + set->name, tmp.hashsize, hashsize);
4901 + tmp.hashsize = hashsize;
4903 + newbytes = hashsize * sizeof(ip_set_ip_t);
4904 + tmp.members = ip_set_malloc(newbytes);
4905 + if (!tmp.members) {
4906 + DP("out of memory for %d bytes", newbytes);
4909 + memset(tmp.members, 0, newbytes);
4911 + write_lock_bh(&set->lock);
4912 + map = (struct ip_set_nethash *) set->data; /* Play safe */
4913 + for (i = 0; i < map->hashsize && res == 0; i++) {
4914 + if (map->members[i])
4915 + res = __addip_base(&tmp, map->members[i]);
4918 + /* Failure, try again */
4919 + write_unlock_bh(&set->lock);
4920 + ip_set_free(tmp.members, newbytes);
4924 + /* Success at resizing! */
4925 + members = map->members;
4926 + hashsize = map->hashsize;
4928 + map->initval = tmp.initval;
4929 + map->prime = tmp.prime;
4930 + map->hashsize = tmp.hashsize;
4931 + map->members = tmp.members;
4932 + write_unlock_bh(&set->lock);
4934 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
4940 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4941 + ip_set_ip_t *hash_ip)
4943 + ip_set_ip_t id = hash_id_cidr(map, ip, cidr, hash_ip);
4945 + if (id == UINT_MAX)
4948 + map->members[id] = 0;
4953 +delip(struct ip_set *set, const void *data, size_t size,
4954 + ip_set_ip_t *hash_ip)
4956 + struct ip_set_req_nethash *req =
4957 + (struct ip_set_req_nethash *) data;
4959 + if (size != sizeof(struct ip_set_req_nethash)) {
4960 + ip_set_printk("data length wrong (want %zu, have %zu)",
4961 + sizeof(struct ip_set_req_nethash),
4965 + /* TODO: no garbage collection in map->cidr */
4966 + return __delip((struct ip_set_nethash *) set->data,
4967 + req->ip, req->cidr, hash_ip);
4971 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4972 + u_int32_t flags, ip_set_ip_t *hash_ip)
4974 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4975 + int ret = -ERANGE;
4976 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4977 + : skb->nh.iph->daddr);
4980 + ret = __delip(map, ip, map->cidr[0], hash_ip);
4985 +static int create(struct ip_set *set, const void *data, size_t size)
4987 + unsigned newbytes;
4988 + struct ip_set_req_nethash_create *req =
4989 + (struct ip_set_req_nethash_create *) data;
4990 + struct ip_set_nethash *map;
4992 + if (size != sizeof(struct ip_set_req_nethash_create)) {
4993 + ip_set_printk("data length wrong (want %zu, have %zu)",
4994 + sizeof(struct ip_set_req_nethash_create),
4999 + if (req->hashsize < 1) {
5000 + ip_set_printk("hashsize too small");
5004 + map = kmalloc(sizeof(struct ip_set_nethash), GFP_KERNEL);
5006 + DP("out of memory for %d bytes",
5007 + sizeof(struct ip_set_nethash));
5010 + get_random_bytes(&map->initval, 4);
5011 + map->prime = make_prime(req->hashsize);
5012 + map->hashsize = req->hashsize;
5013 + map->probes = req->probes;
5014 + map->resize = req->resize;
5015 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
5016 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
5017 + map->members = ip_set_malloc(newbytes);
5018 + if (!map->members) {
5019 + DP("out of memory for %d bytes", newbytes);
5023 + memset(map->members, 0, newbytes);
5029 +static void destroy(struct ip_set *set)
5031 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5033 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
5039 +static void flush(struct ip_set *set)
5041 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5042 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
5043 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
5046 +static void list_header(const struct ip_set *set, void *data)
5048 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5049 + struct ip_set_req_nethash_create *header =
5050 + (struct ip_set_req_nethash_create *) data;
5052 + header->hashsize = map->hashsize;
5053 + header->probes = map->probes;
5054 + header->resize = map->resize;
5057 +static int list_members_size(const struct ip_set *set)
5059 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5061 + return (map->hashsize * sizeof(ip_set_ip_t));
5064 +static void list_members(const struct ip_set *set, void *data)
5066 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5067 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
5069 + memcpy(data, map->members, bytes);
5072 +static struct ip_set_type ip_set_nethash = {
5073 + .typename = SETTYPE_NAME,
5074 + .typecode = IPSET_TYPE_IP,
5075 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5076 + .create = &create,
5077 + .destroy = &destroy,
5079 + .reqsize = sizeof(struct ip_set_req_nethash),
5081 + .addip_kernel = &addip_kernel,
5084 + .delip_kernel = &delip_kernel,
5085 + .testip = &testip,
5086 + .testip_kernel = &testip_kernel,
5087 + .header_size = sizeof(struct ip_set_req_nethash_create),
5088 + .list_header = &list_header,
5089 + .list_members_size = &list_members_size,
5090 + .list_members = &list_members,
5091 + .me = THIS_MODULE,
5094 +MODULE_LICENSE("GPL");
5095 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5096 +MODULE_DESCRIPTION("nethash type of IP sets");
5098 +static int __init init(void)
5100 + return ip_set_register_set_type(&ip_set_nethash);
5103 +static void __exit fini(void)
5105 + /* FIXME: possible race with ip_set_create() */
5106 + ip_set_unregister_set_type(&ip_set_nethash);
5112 +++ b/net/ipv4/netfilter/ip_set_portmap.c
5114 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5116 + * This program is free software; you can redistribute it and/or modify
5117 + * it under the terms of the GNU General Public License version 2 as
5118 + * published by the Free Software Foundation.
5121 +/* Kernel module implementing a port set type as a bitmap */
5123 +#include <linux/module.h>
5124 +#include <linux/ip.h>
5125 +#include <linux/tcp.h>
5126 +#include <linux/udp.h>
5127 +#include <linux/skbuff.h>
5128 +#include <linux/netfilter_ipv4/ip_tables.h>
5129 +#include <linux/netfilter_ipv4/ip_set.h>
5130 +#include <linux/errno.h>
5131 +#include <asm/uaccess.h>
5132 +#include <asm/bitops.h>
5133 +#include <asm/softirq.h>
5134 +#include <linux/spinlock.h>
5136 +#include <net/ip.h>
5138 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
5140 +static inline ip_set_ip_t
5141 +get_port(const struct sk_buff *skb, u_int32_t flags)
5143 + struct iphdr *iph = skb->nh.iph;
5144 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
5146 + switch (iph->protocol) {
5147 + case IPPROTO_TCP: {
5148 + struct tcphdr *tcph = (struct tcphdr *)((u_int32_t *)iph + iph->ihl);
5150 + /* See comments at tcp_match in ip_tables.c */
5153 + && (skb->len - iph->ihl * 4) < sizeof(struct tcphdr)))
5154 + return INVALID_PORT;
5156 + return ntohs(flags & IPSET_SRC ?
5157 + tcph->source : tcph->dest);
5159 + case IPPROTO_UDP: {
5160 + struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
5164 + && (skb->len - iph->ihl * 4) < sizeof(struct udphdr)))
5165 + return INVALID_PORT;
5167 + return ntohs(flags & IPSET_SRC ?
5168 + udph->source : udph->dest);
5171 + return INVALID_PORT;
5176 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5178 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5180 + if (port < map->first_port || port > map->last_port)
5183 + *hash_port = port;
5184 + DP("set: %s, port:%u, %u", set->name, port, *hash_port);
5185 + return !!test_bit(port - map->first_port, map->members);
5189 +testport(struct ip_set *set, const void *data, size_t size,
5190 + ip_set_ip_t *hash_port)
5192 + struct ip_set_req_portmap *req =
5193 + (struct ip_set_req_portmap *) data;
5195 + if (size != sizeof(struct ip_set_req_portmap)) {
5196 + ip_set_printk("data length wrong (want %zu, have %zu)",
5197 + sizeof(struct ip_set_req_portmap),
5201 + return __testport(set, req->port, hash_port);
5205 +testport_kernel(struct ip_set *set, const struct sk_buff *skb,
5206 + u_int32_t flags, ip_set_ip_t *hash_port)
5209 + ip_set_ip_t port = get_port(skb, flags);
5211 + DP("flag %s port %u", flags & IPSET_SRC ? "SRC" : "DST", port);
5212 + if (port == INVALID_PORT)
5215 + res = __testport(set, port, hash_port);
5217 + return (res < 0 ? 0 : res);
5221 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5223 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5225 + if (port < map->first_port || port > map->last_port)
5227 + if (test_and_set_bit(port - map->first_port, map->members))
5230 + *hash_port = port;
5231 + DP("port %u", port);
5236 +addport(struct ip_set *set, const void *data, size_t size,
5237 + ip_set_ip_t *hash_port)
5239 + struct ip_set_req_portmap *req =
5240 + (struct ip_set_req_portmap *) data;
5242 + if (size != sizeof(struct ip_set_req_portmap)) {
5243 + ip_set_printk("data length wrong (want %zu, have %zu)",
5244 + sizeof(struct ip_set_req_portmap),
5248 + return __addport(set, req->port, hash_port);
5252 +addport_kernel(struct ip_set *set, const struct sk_buff *skb,
5253 + u_int32_t flags, ip_set_ip_t *hash_port)
5255 + ip_set_ip_t port = get_port(skb, flags);
5257 + if (port == INVALID_PORT)
5260 + return __addport(set, port, hash_port);
5264 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5266 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5268 + if (port < map->first_port || port > map->last_port)
5270 + if (!test_and_clear_bit(port - map->first_port, map->members))
5273 + *hash_port = port;
5274 + DP("port %u", port);
5279 +delport(struct ip_set *set, const void *data, size_t size,
5280 + ip_set_ip_t *hash_port)
5282 + struct ip_set_req_portmap *req =
5283 + (struct ip_set_req_portmap *) data;
5285 + if (size != sizeof(struct ip_set_req_portmap)) {
5286 + ip_set_printk("data length wrong (want %zu, have %zu)",
5287 + sizeof(struct ip_set_req_portmap),
5291 + return __delport(set, req->port, hash_port);
5295 +delport_kernel(struct ip_set *set, const struct sk_buff *skb,
5296 + u_int32_t flags, ip_set_ip_t *hash_port)
5298 + ip_set_ip_t port = get_port(skb, flags);
5300 + if (port == INVALID_PORT)
5303 + return __delport(set, port, hash_port);
5306 +static int create(struct ip_set *set, const void *data, size_t size)
5309 + struct ip_set_req_portmap_create *req =
5310 + (struct ip_set_req_portmap_create *) data;
5311 + struct ip_set_portmap *map;
5313 + if (size != sizeof(struct ip_set_req_portmap_create)) {
5314 + ip_set_printk("data length wrong (want %zu, have %zu)",
5315 + sizeof(struct ip_set_req_portmap_create),
5320 + DP("from %u to %u", req->from, req->to);
5322 + if (req->from > req->to) {
5323 + DP("bad port range");
5327 + if (req->to - req->from > MAX_RANGE) {
5328 + ip_set_printk("range too big (max %d ports)",
5333 + map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
5335 + DP("out of memory for %d bytes",
5336 + sizeof(struct ip_set_portmap));
5339 + map->first_port = req->from;
5340 + map->last_port = req->to;
5341 + newbytes = bitmap_bytes(req->from, req->to);
5342 + map->members = kmalloc(newbytes, GFP_KERNEL);
5343 + if (!map->members) {
5344 + DP("out of memory for %d bytes", newbytes);
5348 + memset(map->members, 0, newbytes);
5354 +static void destroy(struct ip_set *set)
5356 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5358 + kfree(map->members);
5364 +static void flush(struct ip_set *set)
5366 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5367 + memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
5370 +static void list_header(const struct ip_set *set, void *data)
5372 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5373 + struct ip_set_req_portmap_create *header =
5374 + (struct ip_set_req_portmap_create *) data;
5376 + DP("list_header %u %u", map->first_port, map->last_port);
5378 + header->from = map->first_port;
5379 + header->to = map->last_port;
5382 +static int list_members_size(const struct ip_set *set)
5384 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5386 + return bitmap_bytes(map->first_port, map->last_port);
5389 +static void list_members(const struct ip_set *set, void *data)
5391 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5392 + int bytes = bitmap_bytes(map->first_port, map->last_port);
5394 + memcpy(data, map->members, bytes);
5397 +static struct ip_set_type ip_set_portmap = {
5398 + .typename = SETTYPE_NAME,
5399 + .typecode = IPSET_TYPE_PORT,
5400 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5401 + .create = &create,
5402 + .destroy = &destroy,
5404 + .reqsize = sizeof(struct ip_set_req_portmap),
5405 + .addip = &addport,
5406 + .addip_kernel = &addport_kernel,
5407 + .delip = &delport,
5408 + .delip_kernel = &delport_kernel,
5409 + .testip = &testport,
5410 + .testip_kernel = &testport_kernel,
5411 + .header_size = sizeof(struct ip_set_req_portmap_create),
5412 + .list_header = &list_header,
5413 + .list_members_size = &list_members_size,
5414 + .list_members = &list_members,
5415 + .me = THIS_MODULE,
5418 +MODULE_LICENSE("GPL");
5419 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5420 +MODULE_DESCRIPTION("portmap type of IP sets");
5422 +static int __init init(void)
5424 + return ip_set_register_set_type(&ip_set_portmap);
5427 +static void __exit fini(void)
5429 + /* FIXME: possible race with ip_set_create() */
5430 + ip_set_unregister_set_type(&ip_set_portmap);
5436 +++ b/net/ipv4/netfilter/ipt_set.c
5438 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5439 + * Patrick Schaaf <bof@bof.de>
5440 + * Martin Josefsson <gandalf@wlug.westbo.se>
5441 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5443 + * This program is free software; you can redistribute it and/or modify
5444 + * it under the terms of the GNU General Public License version 2 as
5445 + * published by the Free Software Foundation.
5448 +/* Kernel module to match an IP set. */
5450 +#include <linux/module.h>
5451 +#include <linux/ip.h>
5452 +#include <linux/skbuff.h>
5454 +#include <linux/netfilter_ipv4/ip_tables.h>
5455 +#include <linux/netfilter_ipv4/ip_set.h>
5456 +#include <linux/netfilter_ipv4/ipt_set.h>
5459 +match_set(const struct ipt_set_info *info,
5460 + const struct sk_buff *skb,
5463 + if (ip_set_testip_kernel(info->index, skb, info->flags))
5469 +match(const struct sk_buff *skb,
5470 + const struct net_device *in,
5471 + const struct net_device *out,
5472 + const void *matchinfo,
5475 + u_int16_t datalen,
5478 + const struct ipt_set_info_match *info = matchinfo;
5480 + return match_set(&info->match_set,
5482 + info->match_set.flags[0] & IPSET_MATCH_INV);
5486 +checkentry(const char *tablename,
5487 + const struct ipt_ip *ip,
5489 + unsigned int matchsize,
5490 + unsigned int hook_mask)
5492 + struct ipt_set_info_match *info =
5493 + (struct ipt_set_info_match *) matchinfo;
5494 + ip_set_id_t index;
5496 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5497 + ip_set_printk("invalid matchsize %d", matchsize);
5501 + index = ip_set_get_byindex(info->match_set.index);
5503 + if (index == IP_SET_INVALID_ID) {
5504 + ip_set_printk("Cannot find set indentified by id %u to match",
5505 + info->match_set.index);
5506 + return 0; /* error */
5508 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5509 + ip_set_printk("That's nasty!");
5510 + return 0; /* error */
5516 +static void destroy(void *matchinfo, unsigned int matchsize)
5518 + struct ipt_set_info_match *info = matchinfo;
5520 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5521 + ip_set_printk("invalid matchsize %d", matchsize);
5525 + ip_set_put(info->match_set.index);
5528 +static struct ipt_match set_match = {
5531 + .checkentry = &checkentry,
5532 + .destroy = &destroy,
5536 +MODULE_LICENSE("GPL");
5537 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5538 +MODULE_DESCRIPTION("iptables IP set match module");
5540 +static int __init init(void)
5542 + return ipt_register_match(&set_match);
5545 +static void __exit fini(void)
5547 + ipt_unregister_match(&set_match);
5553 +++ b/net/ipv4/netfilter/ipt_SET.c
5555 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5556 + * Patrick Schaaf <bof@bof.de>
5557 + * Martin Josefsson <gandalf@wlug.westbo.se>
5558 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5560 + * This program is free software; you can redistribute it and/or modify
5561 + * it under the terms of the GNU General Public License version 2 as
5562 + * published by the Free Software Foundation.
5565 +/* ipt_SET.c - netfilter target to manipulate IP sets */
5567 +#include <linux/types.h>
5568 +#include <linux/ip.h>
5569 +#include <linux/timer.h>
5570 +#include <linux/module.h>
5571 +#include <linux/netfilter.h>
5572 +#include <linux/netdevice.h>
5573 +#include <linux/if.h>
5574 +#include <linux/inetdevice.h>
5575 +#include <net/protocol.h>
5576 +#include <net/checksum.h>
5577 +#include <linux/netfilter_ipv4.h>
5578 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
5579 +#include <linux/netfilter_ipv4/ipt_set.h>
5581 +static unsigned int
5582 +target(struct sk_buff **pskb,
5583 + unsigned int hooknum,
5584 + const struct net_device *in,
5585 + const struct net_device *out,
5586 + const void *targinfo,
5589 + const struct ipt_set_info_target *info = targinfo;
5591 + if (info->add_set.index != IP_SET_INVALID_ID)
5592 + ip_set_addip_kernel(info->add_set.index,
5594 + info->add_set.flags);
5595 + if (info->del_set.index != IP_SET_INVALID_ID)
5596 + ip_set_delip_kernel(info->del_set.index,
5598 + info->del_set.flags);
5600 + return IPT_CONTINUE;
5604 +checkentry(const char *tablename,
5605 + const struct ipt_entry *e,
5607 + unsigned int targinfosize, unsigned int hook_mask)
5609 + struct ipt_set_info_target *info =
5610 + (struct ipt_set_info_target *) targinfo;
5611 + ip_set_id_t index;
5613 + if (targinfosize != IPT_ALIGN(sizeof(*info))) {
5614 + DP("bad target info size %u", targinfosize);
5618 + if (info->add_set.index != IP_SET_INVALID_ID) {
5619 + index = ip_set_get_byindex(info->add_set.index);
5620 + if (index == IP_SET_INVALID_ID) {
5621 + ip_set_printk("cannot find add_set index %u as target",
5622 + info->add_set.index);
5623 + return 0; /* error */
5627 + if (info->del_set.index != IP_SET_INVALID_ID) {
5628 + index = ip_set_get_byindex(info->del_set.index);
5629 + if (index == IP_SET_INVALID_ID) {
5630 + ip_set_printk("cannot find del_set index %u as target",
5631 + info->del_set.index);
5632 + return 0; /* error */
5635 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
5636 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5637 + ip_set_printk("That's nasty!");
5638 + return 0; /* error */
5643 +static void destroy(void *targetinfo, unsigned int targetsize)
5645 + struct ipt_set_info_target *info = targetinfo;
5647 + if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
5648 + ip_set_printk("invalid targetsize %d", targetsize);
5652 + if (info->add_set.index != IP_SET_INVALID_ID)
5653 + ip_set_put(info->add_set.index);
5654 + if (info->del_set.index != IP_SET_INVALID_ID)
5655 + ip_set_put(info->del_set.index);
5658 +static struct ipt_target SET_target = {
5661 + .checkentry = checkentry,
5662 + .destroy = destroy,
5666 +MODULE_LICENSE("GPL");
5667 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5668 +MODULE_DESCRIPTION("iptables IP set target module");
5670 +static int __init init(void)
5672 + return ipt_register_target(&SET_target);
5675 +static void __exit fini(void)
5677 + ipt_unregister_target(&SET_target);
5682 --- a/net/ipv4/netfilter/Makefile
5683 +++ b/net/ipv4/netfilter/Makefile
5684 @@ -75,6 +75,18 @@ obj-$(CONFIG_IP_NF_NAT) += iptable_nat.o
5685 obj-$(CONFIG_IP_NF_MATCH_HELPER) += ipt_helper.o
5686 obj-$(CONFIG_IP_NF_MATCH_LIMIT) += ipt_limit.o
5687 obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
5688 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
5689 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
5690 +ifdef CONFIG_IP_NF_SET
5691 + obj-$(CONFIG_IP_NF_SET) += ip_set.o
5692 + export-objs += ip_set.o
5694 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
5695 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
5696 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
5697 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
5698 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
5699 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
5700 obj-$(CONFIG_IP_NF_MATCH_MAC) += ipt_mac.o
5701 obj-$(CONFIG_IP_NF_MATCH_IPP2P) += ipt_ipp2p.o