1 --- a/include/linux/netfilter_ipv4/Kbuild
2 +++ b/include/linux/netfilter_ipv4/Kbuild
3 @@ -45,3 +45,14 @@ header-y += ipt_ttl.h
5 unifdef-y += ip_queue.h
6 unifdef-y += ip_tables.h
9 +header-y += ip_set_iphash.h
10 +header-y += ip_set_ipmap.h
11 +header-y += ip_set_ipporthash.h
12 +unifdef-y += ip_set_iptree.h
13 +unifdef-y += ip_set_iptreemap.h
14 +header-y += ip_set_jhash.h
15 +header-y += ip_set_macipmap.h
16 +unifdef-y += ip_set_nethash.h
17 +header-y += ip_set_portmap.h
19 +++ b/include/linux/netfilter_ipv4/ip_set.h
24 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
25 + * Patrick Schaaf <bof@bof.de>
26 + * Martin Josefsson <gandalf@wlug.westbo.se>
27 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
29 + * This program is free software; you can redistribute it and/or modify
30 + * it under the terms of the GNU General Public License version 2 as
31 + * published by the Free Software Foundation.
39 + * A sockopt of such quality has hardly ever been seen before on the open
40 + * market! This little beauty, hardly ever used: above 64, so it's
41 + * traditionally used for firewalling, not touched (even once!) by the
42 + * 2.0, 2.2 and 2.4 kernels!
44 + * Comes with its own certificate of authenticity, valid anywhere in the
52 + * Heavily modify by Joakim Axelsson 08.03.2002
53 + * - Made it more modulebased
55 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
57 + * - in order to "deal with" backward compatibility, renamed to ipset
61 + * Used so that the kernel module and ipset-binary can match their versions
63 +#define IP_SET_PROTOCOL_VERSION 2
65 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
67 +/* Lets work with our own typedef for representing an IP address.
68 + * We hope to make the code more portable, possibly to IPv6...
70 + * The representation works in HOST byte order, because most set types
71 + * will perform arithmetic operations and compare operations.
73 + * For now the type is an uint32_t.
75 + * Make sure to ONLY use the functions when translating and parsing
76 + * in order to keep the host byte order and make it more portable:
81 + * (Joakim: where are they???)
84 +typedef uint32_t ip_set_ip_t;
86 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
87 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
89 +typedef uint16_t ip_set_id_t;
91 +#define IP_SET_INVALID_ID 65535
93 +/* How deep we follow bindings */
94 +#define IP_SET_MAX_BINDINGS 6
97 + * Option flags for kernel operations (ipt_set_info)
99 +#define IPSET_SRC 0x01 /* Source match/add */
100 +#define IPSET_DST 0x02 /* Destination match/add */
101 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
106 +#define IPSET_TYPE_IP 0x01 /* IP address type of set */
107 +#define IPSET_TYPE_PORT 0x02 /* Port type of set */
108 +#define IPSET_DATA_SINGLE 0x04 /* Single data storage */
109 +#define IPSET_DATA_DOUBLE 0x08 /* Double data storage */
111 +/* Reserved keywords */
112 +#define IPSET_TOKEN_DEFAULT ":default:"
113 +#define IPSET_TOKEN_ALL ":all:"
115 +/* SO_IP_SET operation constants, and their request struct types.
118 + * 0-99: commands with version checking
119 + * 100-199: add/del/test/bind/unbind
120 + * 200-299: list, save, restore
123 +/* Single shot operations:
124 + * version, create, destroy, flush, rename and swap
126 + * Sets are identified by name.
129 +#define IP_SET_REQ_STD \
131 + unsigned version; \
132 + char name[IP_SET_MAXNAMELEN]
134 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
135 +struct ip_set_req_create {
137 + char typename[IP_SET_MAXNAMELEN];
140 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
141 +struct ip_set_req_std {
145 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
146 +/* Uses ip_set_req_std */
148 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
149 +/* Uses ip_set_req_create */
151 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
152 +/* Uses ip_set_req_create */
154 +union ip_set_name_index {
155 + char name[IP_SET_MAXNAMELEN];
159 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
160 +struct ip_set_req_get_set {
163 + union ip_set_name_index set;
166 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
167 +/* Uses ip_set_req_get_set */
169 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
170 +struct ip_set_req_version {
175 +/* Double shots operations:
176 + * add, del, test, bind and unbind.
178 + * First we query the kernel to get the index and type of the target set,
179 + * then issue the command. Validity of IP is checked in kernel in order
180 + * to minimalize sockopt operations.
183 +/* Get minimal set data for add/del/test/bind/unbind IP */
184 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
185 +struct ip_set_req_adt_get {
188 + union ip_set_name_index set;
189 + char typename[IP_SET_MAXNAMELEN];
192 +#define IP_SET_REQ_BYINDEX \
196 +struct ip_set_req_adt {
197 + IP_SET_REQ_BYINDEX;
200 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
201 +/* Uses ip_set_req_adt, with type specific addage */
203 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
204 +/* Uses ip_set_req_adt, with type specific addage */
206 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
207 +/* Uses ip_set_req_adt, with type specific addage */
209 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
210 +/* Uses ip_set_req_bind, with type specific addage */
211 +struct ip_set_req_bind {
212 + IP_SET_REQ_BYINDEX;
213 + char binding[IP_SET_MAXNAMELEN];
216 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
217 +/* Uses ip_set_req_bind, with type speficic addage
218 + * index = 0 means unbinding for all sets */
220 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
221 +/* Uses ip_set_req_bind, with type specific addage */
223 +/* Multiple shots operations: list, save, restore.
225 + * - check kernel version and query the max number of sets
226 + * - get the basic information on all sets
227 + * and size required for the next step
228 + * - get actual set data: header, data, bindings
231 +/* Get max_sets and the index of a queried set
233 +#define IP_SET_OP_MAX_SETS 0x00000020
234 +struct ip_set_req_max_sets {
237 + ip_set_id_t max_sets; /* max_sets */
238 + ip_set_id_t sets; /* real number of sets */
239 + union ip_set_name_index set; /* index of set if name used */
242 +/* Get the id and name of the sets plus size for next step */
243 +#define IP_SET_OP_LIST_SIZE 0x00000201
244 +#define IP_SET_OP_SAVE_SIZE 0x00000202
245 +struct ip_set_req_setnames {
247 + ip_set_id_t index; /* set to list/save */
248 + size_t size; /* size to get setdata/bindings */
249 + /* followed by sets number of struct ip_set_name_list */
252 +struct ip_set_name_list {
253 + char name[IP_SET_MAXNAMELEN];
254 + char typename[IP_SET_MAXNAMELEN];
259 +/* The actual list operation */
260 +#define IP_SET_OP_LIST 0x00000203
261 +struct ip_set_req_list {
262 + IP_SET_REQ_BYINDEX;
263 + /* sets number of struct ip_set_list in reply */
266 +struct ip_set_list {
268 + ip_set_id_t binding;
270 + size_t header_size; /* Set header data of header_size */
271 + size_t members_size; /* Set members data of members_size */
272 + size_t bindings_size; /* Set bindings data of bindings_size */
275 +struct ip_set_hash_list {
277 + ip_set_id_t binding;
280 +/* The save operation */
281 +#define IP_SET_OP_SAVE 0x00000204
282 +/* Uses ip_set_req_list, in the reply replaced by
283 + * sets number of struct ip_set_save plus a marker
284 + * ip_set_save followed by ip_set_hash_save structures.
286 +struct ip_set_save {
288 + ip_set_id_t binding;
289 + size_t header_size; /* Set header data of header_size */
290 + size_t members_size; /* Set members data of members_size */
293 +/* At restoring, ip == 0 means default binding for the given set: */
294 +struct ip_set_hash_save {
297 + ip_set_id_t binding;
300 +/* The restore operation */
301 +#define IP_SET_OP_RESTORE 0x00000205
302 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
303 + * plus a marker ip_set_restore, followed by ip_set_hash_save
306 +struct ip_set_restore {
307 + char name[IP_SET_MAXNAMELEN];
308 + char typename[IP_SET_MAXNAMELEN];
310 + size_t header_size; /* Create data of header_size */
311 + size_t members_size; /* Set members data of members_size */
314 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
316 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
321 +#define ip_set_printk(format, args...) \
323 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
324 + printk(format "\n" , ## args); \
327 +#if defined(IP_SET_DEBUG)
328 +#define DP(format, args...) \
330 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
331 + printk(format "\n" , ## args); \
333 +#define IP_SET_ASSERT(x) \
336 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
337 + __FILE__, __LINE__, __FUNCTION__); \
340 +#define DP(format, args...)
341 +#define IP_SET_ASSERT(x)
347 + * The ip_set_type definition - one per set type, e.g. "ipmap".
349 + * Each individual set has a pointer, set->type, going to one
350 + * of these structures. Function pointers inside the structure implement
351 + * the real behaviour of the sets.
353 + * If not mentioned differently, the implementation behind the function
354 + * pointers of a set_type, is expected to return 0 if ok, and a negative
355 + * errno (e.g. -EINVAL) on error.
357 +struct ip_set_type {
358 + struct list_head list; /* next in list of set types */
360 + /* test for IP in set (kernel: iptables -m set src|dst)
361 + * return 0 if not in set, 1 if in set.
363 + int (*testip_kernel) (struct ip_set *set,
364 + const struct sk_buff * skb,
366 + const u_int32_t *flags,
367 + unsigned char index);
369 + /* test for IP in set (userspace: ipset -T set IP)
370 + * return 0 if not in set, 1 if in set.
372 + int (*testip) (struct ip_set *set,
373 + const void *data, size_t size,
377 + * Size of the data structure passed by when
378 + * adding/deletin/testing an entry.
382 + /* Add IP into set (userspace: ipset -A set IP)
383 + * Return -EEXIST if the address is already in the set,
384 + * and -ERANGE if the address lies outside the set bounds.
385 + * If the address was not already in the set, 0 is returned.
387 + int (*addip) (struct ip_set *set,
388 + const void *data, size_t size,
391 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
392 + * Return -EEXIST if the address is already in the set,
393 + * and -ERANGE if the address lies outside the set bounds.
394 + * If the address was not already in the set, 0 is returned.
396 + int (*addip_kernel) (struct ip_set *set,
397 + const struct sk_buff * skb,
399 + const u_int32_t *flags,
400 + unsigned char index);
402 + /* remove IP from set (userspace: ipset -D set --entry x)
403 + * Return -EEXIST if the address is NOT in the set,
404 + * and -ERANGE if the address lies outside the set bounds.
405 + * If the address really was in the set, 0 is returned.
407 + int (*delip) (struct ip_set *set,
408 + const void *data, size_t size,
411 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
412 + * Return -EEXIST if the address is NOT in the set,
413 + * and -ERANGE if the address lies outside the set bounds.
414 + * If the address really was in the set, 0 is returned.
416 + int (*delip_kernel) (struct ip_set *set,
417 + const struct sk_buff * skb,
419 + const u_int32_t *flags,
420 + unsigned char index);
422 + /* new set creation - allocated type specific items
424 + int (*create) (struct ip_set *set,
425 + const void *data, size_t size);
427 + /* retry the operation after successfully tweaking the set
429 + int (*retry) (struct ip_set *set);
431 + /* set destruction - free type specific items
432 + * There is no return value.
433 + * Can be called only when child sets are destroyed.
435 + void (*destroy) (struct ip_set *set);
437 + /* set flushing - reset all bits in the set, or something similar.
438 + * There is no return value.
440 + void (*flush) (struct ip_set *set);
442 + /* Listing: size needed for header
444 + size_t header_size;
446 + /* Listing: Get the header
448 + * Fill in the information in "data".
449 + * This function is always run after list_header_size() under a
450 + * writelock on the set. Therefor is the length of "data" always
453 + void (*list_header) (const struct ip_set *set,
456 + /* Listing: Get the size for the set members
458 + int (*list_members_size) (const struct ip_set *set);
460 + /* Listing: Get the set members
462 + * Fill in the information in "data".
463 + * This function is always run after list_member_size() under a
464 + * writelock on the set. Therefor is the length of "data" always
467 + void (*list_members) (const struct ip_set *set,
470 + char typename[IP_SET_MAXNAMELEN];
471 + unsigned char features;
472 + int protocol_version;
474 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
478 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
479 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
481 +/* A generic ipset */
483 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
484 + rwlock_t lock; /* lock for concurrency control */
485 + ip_set_id_t id; /* set id for swapping */
486 + ip_set_id_t binding; /* default binding for the set */
487 + atomic_t ref; /* in kernel and in hash references */
488 + struct ip_set_type *type; /* the set types */
489 + void *data; /* pooltype specific data */
492 +/* Structure to bind set elements to sets */
493 +struct ip_set_hash {
494 + struct list_head list; /* list of clashing entries in hash */
495 + ip_set_ip_t ip; /* ip from set */
496 + ip_set_id_t id; /* set id */
497 + ip_set_id_t binding; /* set we bind the element to */
500 +/* register and unregister set references */
501 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
502 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
503 +extern void ip_set_put(ip_set_id_t id);
505 +/* API for iptables set match, and SET target */
506 +extern void ip_set_addip_kernel(ip_set_id_t id,
507 + const struct sk_buff *skb,
508 + const u_int32_t *flags);
509 +extern void ip_set_delip_kernel(ip_set_id_t id,
510 + const struct sk_buff *skb,
511 + const u_int32_t *flags);
512 +extern int ip_set_testip_kernel(ip_set_id_t id,
513 + const struct sk_buff *skb,
514 + const u_int32_t *flags);
516 +#endif /* __KERNEL__ */
518 +#endif /*_IP_SET_H*/
520 +++ b/include/linux/netfilter_ipv4/ip_set_iphash.h
522 +#ifndef __IP_SET_IPHASH_H
523 +#define __IP_SET_IPHASH_H
525 +#include <linux/netfilter_ipv4/ip_set.h>
527 +#define SETTYPE_NAME "iphash"
528 +#define MAX_RANGE 0x0000FFFF
530 +struct ip_set_iphash {
531 + ip_set_ip_t *members; /* the iphash proper */
532 + uint32_t elements; /* number of elements */
533 + uint32_t hashsize; /* hash size */
534 + uint16_t probes; /* max number of probes */
535 + uint16_t resize; /* resize factor in percent */
536 + ip_set_ip_t netmask; /* netmask */
537 + void *initval[0]; /* initvals for jhash_1word */
540 +struct ip_set_req_iphash_create {
544 + ip_set_ip_t netmask;
547 +struct ip_set_req_iphash {
551 +#endif /* __IP_SET_IPHASH_H */
553 +++ b/include/linux/netfilter_ipv4/ip_set_ipmap.h
555 +#ifndef __IP_SET_IPMAP_H
556 +#define __IP_SET_IPMAP_H
558 +#include <linux/netfilter_ipv4/ip_set.h>
560 +#define SETTYPE_NAME "ipmap"
561 +#define MAX_RANGE 0x0000FFFF
563 +struct ip_set_ipmap {
564 + void *members; /* the ipmap proper */
565 + ip_set_ip_t first_ip; /* host byte order, included in range */
566 + ip_set_ip_t last_ip; /* host byte order, included in range */
567 + ip_set_ip_t netmask; /* subnet netmask */
568 + ip_set_ip_t sizeid; /* size of set in IPs */
569 + ip_set_ip_t hosts; /* number of hosts in a subnet */
572 +struct ip_set_req_ipmap_create {
575 + ip_set_ip_t netmask;
578 +struct ip_set_req_ipmap {
583 +mask_to_bits(ip_set_ip_t mask)
585 + unsigned int bits = 32;
586 + ip_set_ip_t maskaddr;
588 + if (mask == 0xFFFFFFFF)
591 + maskaddr = 0xFFFFFFFE;
592 + while (--bits >= 0 && maskaddr != mask)
599 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
601 + ip_set_ip_t mask = 0xFFFFFFFE;
604 + while (--(*bits) >= 0 && mask && (to & mask) != from)
610 +#endif /* __IP_SET_IPMAP_H */
612 +++ b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
614 +#ifndef __IP_SET_IPPORTHASH_H
615 +#define __IP_SET_IPPORTHASH_H
617 +#include <linux/netfilter_ipv4/ip_set.h>
619 +#define SETTYPE_NAME "ipporthash"
620 +#define MAX_RANGE 0x0000FFFF
621 +#define INVALID_PORT (MAX_RANGE + 1)
623 +struct ip_set_ipporthash {
624 + ip_set_ip_t *members; /* the ipporthash proper */
625 + uint32_t elements; /* number of elements */
626 + uint32_t hashsize; /* hash size */
627 + uint16_t probes; /* max number of probes */
628 + uint16_t resize; /* resize factor in percent */
629 + ip_set_ip_t first_ip; /* host byte order, included in range */
630 + ip_set_ip_t last_ip; /* host byte order, included in range */
631 + void *initval[0]; /* initvals for jhash_1word */
634 +struct ip_set_req_ipporthash_create {
642 +struct ip_set_req_ipporthash {
647 +#endif /* __IP_SET_IPPORTHASH_H */
649 +++ b/include/linux/netfilter_ipv4/ip_set_iptree.h
651 +#ifndef __IP_SET_IPTREE_H
652 +#define __IP_SET_IPTREE_H
654 +#include <linux/netfilter_ipv4/ip_set.h>
656 +#define SETTYPE_NAME "iptree"
657 +#define MAX_RANGE 0x0000FFFF
659 +struct ip_set_iptreed {
660 + unsigned long expires[256]; /* x.x.x.ADDR */
663 +struct ip_set_iptreec {
664 + struct ip_set_iptreed *tree[256]; /* x.x.ADDR.* */
667 +struct ip_set_iptreeb {
668 + struct ip_set_iptreec *tree[256]; /* x.ADDR.*.* */
671 +struct ip_set_iptree {
672 + unsigned int timeout;
673 + unsigned int gc_interval;
675 + uint32_t elements; /* number of elements */
676 + struct timer_list gc;
677 + struct ip_set_iptreeb *tree[256]; /* ADDR.*.*.* */
681 +struct ip_set_req_iptree_create {
682 + unsigned int timeout;
685 +struct ip_set_req_iptree {
687 + unsigned int timeout;
690 +#endif /* __IP_SET_IPTREE_H */
692 +++ b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
694 +#ifndef __IP_SET_IPTREEMAP_H
695 +#define __IP_SET_IPTREEMAP_H
697 +#include <linux/netfilter_ipv4/ip_set.h>
699 +#define SETTYPE_NAME "iptreemap"
702 +struct ip_set_iptreemap_d {
703 + unsigned char bitmap[32]; /* x.x.x.y */
706 +struct ip_set_iptreemap_c {
707 + struct ip_set_iptreemap_d *tree[256]; /* x.x.y.x */
710 +struct ip_set_iptreemap_b {
711 + struct ip_set_iptreemap_c *tree[256]; /* x.y.x.x */
712 + unsigned char dirty[32];
716 +struct ip_set_iptreemap {
717 + unsigned int gc_interval;
719 + struct timer_list gc;
720 + struct ip_set_iptreemap_b *tree[256]; /* y.x.x.x */
724 +struct ip_set_req_iptreemap_create {
725 + unsigned int gc_interval;
728 +struct ip_set_req_iptreemap {
733 +#endif /* __IP_SET_IPTREEMAP_H */
735 +++ b/include/linux/netfilter_ipv4/ip_set_jhash.h
737 +#ifndef _LINUX_IPSET_JHASH_H
738 +#define _LINUX_IPSET_JHASH_H
740 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
741 + * to __u32/__u8 so that the header file can be included into
742 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
745 +/* jhash.h: Jenkins hash support.
747 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
749 + * http://burtleburtle.net/bob/hash/
751 + * These are the credits from Bob's sources:
753 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
754 + * hash(), hash2(), hash3, and mix() are externally useful functions.
755 + * Routines to test the hash are included if SELF_TEST is defined.
756 + * You can use this free for any purpose. It has no warranty.
758 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
760 + * I've modified Bob's hash to be useful in the Linux kernel, and
761 + * any bugs present are surely my fault. -DaveM
764 +/* NOTE: Arguments are modified. */
765 +#define __jhash_mix(a, b, c) \
767 + a -= b; a -= c; a ^= (c>>13); \
768 + b -= c; b -= a; b ^= (a<<8); \
769 + c -= a; c -= b; c ^= (b>>13); \
770 + a -= b; a -= c; a ^= (c>>12); \
771 + b -= c; b -= a; b ^= (a<<16); \
772 + c -= a; c -= b; c ^= (b>>5); \
773 + a -= b; a -= c; a ^= (c>>3); \
774 + b -= c; b -= a; b ^= (a<<10); \
775 + c -= a; c -= b; c ^= (b>>15); \
778 +/* The golden ration: an arbitrary value */
779 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
781 +/* The most generic version, hashes an arbitrary sequence
782 + * of bytes. No alignment or length assumptions are made about
785 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
787 + __u32 a, b, c, len;
791 + a = b = JHASH_GOLDEN_RATIO;
794 + while (len >= 12) {
795 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
796 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
797 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
799 + __jhash_mix(a,b,c);
807 + case 11: c += ((__u32)k[10]<<24);
808 + case 10: c += ((__u32)k[9]<<16);
809 + case 9 : c += ((__u32)k[8]<<8);
810 + case 8 : b += ((__u32)k[7]<<24);
811 + case 7 : b += ((__u32)k[6]<<16);
812 + case 6 : b += ((__u32)k[5]<<8);
813 + case 5 : b += k[4];
814 + case 4 : a += ((__u32)k[3]<<24);
815 + case 3 : a += ((__u32)k[2]<<16);
816 + case 2 : a += ((__u32)k[1]<<8);
817 + case 1 : a += k[0];
820 + __jhash_mix(a,b,c);
825 +/* A special optimized version that handles 1 or more of __u32s.
826 + * The length parameter here is the number of __u32s in the key.
828 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
830 + __u32 a, b, c, len;
832 + a = b = JHASH_GOLDEN_RATIO;
840 + __jhash_mix(a, b, c);
847 + case 2 : b += k[1];
848 + case 1 : a += k[0];
851 + __jhash_mix(a,b,c);
857 +/* A special ultra-optimized versions that knows they are hashing exactly
858 + * 3, 2 or 1 word(s).
860 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
861 + * done at the end is not done here.
863 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
865 + a += JHASH_GOLDEN_RATIO;
866 + b += JHASH_GOLDEN_RATIO;
869 + __jhash_mix(a, b, c);
874 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
876 + return jhash_3words(a, b, 0, initval);
879 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
881 + return jhash_3words(a, 0, 0, initval);
884 +#endif /* _LINUX_IPSET_JHASH_H */
886 +++ b/include/linux/netfilter_ipv4/ip_set_macipmap.h
888 +#ifndef __IP_SET_MACIPMAP_H
889 +#define __IP_SET_MACIPMAP_H
891 +#include <linux/netfilter_ipv4/ip_set.h>
893 +#define SETTYPE_NAME "macipmap"
894 +#define MAX_RANGE 0x0000FFFF
897 +#define IPSET_MACIP_MATCHUNSET 1
900 +#define IPSET_MACIP_ISSET 1
902 +struct ip_set_macipmap {
903 + void *members; /* the macipmap proper */
904 + ip_set_ip_t first_ip; /* host byte order, included in range */
905 + ip_set_ip_t last_ip; /* host byte order, included in range */
909 +struct ip_set_req_macipmap_create {
915 +struct ip_set_req_macipmap {
917 + unsigned char ethernet[ETH_ALEN];
920 +struct ip_set_macip {
921 + unsigned short flags;
922 + unsigned char ethernet[ETH_ALEN];
925 +#endif /* __IP_SET_MACIPMAP_H */
927 +++ b/include/linux/netfilter_ipv4/ip_set_malloc.h
929 +#ifndef _IP_SET_MALLOC_H
930 +#define _IP_SET_MALLOC_H
934 +/* Memory allocation and deallocation */
935 +static size_t max_malloc_size = 0;
937 +static inline void init_max_malloc_size(void)
939 +#define CACHE(x) max_malloc_size = x;
940 +#include <linux/kmalloc_sizes.h>
944 +static inline void * ip_set_malloc(size_t bytes)
946 + if (bytes > max_malloc_size)
947 + return vmalloc(bytes);
949 + return kmalloc(bytes, GFP_KERNEL);
952 +static inline void ip_set_free(void * data, size_t bytes)
954 + if (bytes > max_malloc_size)
961 + size_t max_elements;
965 +static inline void *
966 +harray_malloc(size_t hashsize, size_t typesize, int flags)
968 + struct harray *harray;
969 + size_t max_elements, size, i, j;
971 + if (!max_malloc_size)
972 + init_max_malloc_size();
974 + if (typesize > max_malloc_size)
977 + max_elements = max_malloc_size/typesize;
978 + size = hashsize/max_elements;
979 + if (hashsize % max_elements)
982 + /* Last pointer signals end of arrays */
983 + harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
989 + for (i = 0; i < size - 1; i++) {
990 + harray->arrays[i] = kmalloc(max_elements * typesize, flags);
991 + if (!harray->arrays[i])
993 + memset(harray->arrays[i], 0, max_elements * typesize);
995 + harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize,
997 + if (!harray->arrays[i])
999 + memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
1001 + harray->max_elements = max_elements;
1002 + harray->arrays[size] = NULL;
1004 + return (void *)harray;
1007 + for (j = 0; j < i; j++) {
1008 + kfree(harray->arrays[j]);
1014 +static inline void harray_free(void *h)
1016 + struct harray *harray = (struct harray *) h;
1019 + for (i = 0; harray->arrays[i] != NULL; i++)
1020 + kfree(harray->arrays[i]);
1024 +static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
1026 + struct harray *harray = (struct harray *) h;
1029 + for (i = 0; harray->arrays[i+1] != NULL; i++)
1030 + memset(harray->arrays[i], 0, harray->max_elements * typesize);
1031 + memset(harray->arrays[i], 0,
1032 + (hashsize - i * harray->max_elements) * typesize);
1035 +#define HARRAY_ELEM(h, type, which) \
1037 + struct harray *__h = (struct harray *)(h); \
1038 + ((type)((__h)->arrays[(which)/(__h)->max_elements]) \
1039 + + (which)%(__h)->max_elements); \
1042 +#endif /* __KERNEL__ */
1044 +#endif /*_IP_SET_MALLOC_H*/
1046 +++ b/include/linux/netfilter_ipv4/ip_set_nethash.h
1048 +#ifndef __IP_SET_NETHASH_H
1049 +#define __IP_SET_NETHASH_H
1051 +#include <linux/netfilter_ipv4/ip_set.h>
1053 +#define SETTYPE_NAME "nethash"
1054 +#define MAX_RANGE 0x0000FFFF
1056 +struct ip_set_nethash {
1057 + ip_set_ip_t *members; /* the nethash proper */
1058 + uint32_t elements; /* number of elements */
1059 + uint32_t hashsize; /* hash size */
1060 + uint16_t probes; /* max number of probes */
1061 + uint16_t resize; /* resize factor in percent */
1062 + unsigned char cidr[30]; /* CIDR sizes */
1063 + void *initval[0]; /* initvals for jhash_1word */
1066 +struct ip_set_req_nethash_create {
1067 + uint32_t hashsize;
1072 +struct ip_set_req_nethash {
1074 + unsigned char cidr;
1077 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
1079 +static inline ip_set_ip_t
1080 +pack(ip_set_ip_t ip, unsigned char cidr)
1082 + ip_set_ip_t addr, *paddr = &addr;
1083 + unsigned char n, t, *a;
1085 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
1087 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
1091 + a = &((unsigned char *)paddr)[n];
1092 + *a = *a /(1 << (8 - t)) + shifts[t];
1094 + DP("n: %u, t: %u, a: %u", n, t, *a);
1095 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
1096 + HIPQUAD(ip), cidr, NIPQUAD(addr));
1099 + return ntohl(addr);
1102 +#endif /* __IP_SET_NETHASH_H */
1104 +++ b/include/linux/netfilter_ipv4/ip_set_portmap.h
1106 +#ifndef __IP_SET_PORTMAP_H
1107 +#define __IP_SET_PORTMAP_H
1109 +#include <linux/netfilter_ipv4/ip_set.h>
1111 +#define SETTYPE_NAME "portmap"
1112 +#define MAX_RANGE 0x0000FFFF
1113 +#define INVALID_PORT (MAX_RANGE + 1)
1115 +struct ip_set_portmap {
1116 + void *members; /* the portmap proper */
1117 + ip_set_ip_t first_port; /* host byte order, included in range */
1118 + ip_set_ip_t last_port; /* host byte order, included in range */
1121 +struct ip_set_req_portmap_create {
1126 +struct ip_set_req_portmap {
1130 +#endif /* __IP_SET_PORTMAP_H */
1132 +++ b/include/linux/netfilter_ipv4/ipt_set.h
1137 +#include <linux/netfilter_ipv4/ip_set.h>
1139 +struct ipt_set_info {
1140 + ip_set_id_t index;
1141 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1145 +struct ipt_set_info_match {
1146 + struct ipt_set_info match_set;
1149 +struct ipt_set_info_target {
1150 + struct ipt_set_info add_set;
1151 + struct ipt_set_info del_set;
1154 +#endif /*_IPT_SET_H*/
1156 +++ b/net/ipv4/netfilter/ip_set.c
1158 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1159 + * Patrick Schaaf <bof@bof.de>
1160 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1162 + * This program is free software; you can redistribute it and/or modify
1163 + * it under the terms of the GNU General Public License version 2 as
1164 + * published by the Free Software Foundation.
1167 +/* Kernel module for IP set management */
1169 +#include <linux/version.h>
1170 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
1171 +#include <linux/config.h>
1173 +#include <linux/module.h>
1174 +#include <linux/moduleparam.h>
1175 +#include <linux/kmod.h>
1176 +#include <linux/ip.h>
1177 +#include <linux/skbuff.h>
1178 +#include <linux/random.h>
1179 +#include <linux/jhash.h>
1180 +#include <linux/netfilter_ipv4/ip_tables.h>
1181 +#include <linux/errno.h>
1182 +#include <linux/semaphore.h>
1183 +#include <asm/uaccess.h>
1184 +#include <asm/bitops.h>
1185 +#include <linux/spinlock.h>
1186 +#include <linux/vmalloc.h>
1188 +#define ASSERT_READ_LOCK(x)
1189 +#define ASSERT_WRITE_LOCK(x)
1190 +#include <linux/netfilter_ipv4/ip_set.h>
1192 +static struct list_head set_type_list; /* all registered sets */
1193 +static struct ip_set **ip_set_list; /* all individual sets */
1194 +static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1195 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1196 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1197 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1198 +static struct list_head *ip_set_hash; /* hash of bindings */
1199 +static unsigned int ip_set_hash_random; /* random seed */
1202 + * Sets are identified either by the index in ip_set_list or by id.
1203 + * The id never changes and is used to find a key in the hash.
1204 + * The index may change by swapping and used at all other places
1205 + * (set/SET netfilter modules, binding value, etc.)
1207 + * Userspace requests are serialized by ip_set_mutex and sets can
1208 + * be deleted only from userspace. Therefore ip_set_list locking
1209 + * must obey the following rules:
1211 + * - kernel requests: read and write locking mandatory
1212 + * - user requests: read locking optional, write locking mandatory
1216 +__ip_set_get(ip_set_id_t index)
1218 + atomic_inc(&ip_set_list[index]->ref);
1222 +__ip_set_put(ip_set_id_t index)
1224 + atomic_dec(&ip_set_list[index]->ref);
1228 + * Binding routines
1231 +static inline struct ip_set_hash *
1232 +__ip_set_find(u_int32_t key, ip_set_id_t id, ip_set_ip_t ip)
1234 + struct ip_set_hash *set_hash;
1236 + list_for_each_entry(set_hash, &ip_set_hash[key], list)
1237 + if (set_hash->id == id && set_hash->ip == ip)
1244 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1246 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1247 + % ip_set_bindings_hash_size;
1248 + struct ip_set_hash *set_hash;
1250 + ASSERT_READ_LOCK(&ip_set_lock);
1251 + IP_SET_ASSERT(ip_set_list[id]);
1252 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1254 + set_hash = __ip_set_find(key, id, ip);
1256 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1258 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1260 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1264 +__set_hash_del(struct ip_set_hash *set_hash)
1266 + ASSERT_WRITE_LOCK(&ip_set_lock);
1267 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1269 + __ip_set_put(set_hash->binding);
1270 + list_del(&set_hash->list);
1275 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1277 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1278 + % ip_set_bindings_hash_size;
1279 + struct ip_set_hash *set_hash;
1281 + IP_SET_ASSERT(ip_set_list[id]);
1282 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1283 + write_lock_bh(&ip_set_lock);
1284 + set_hash = __ip_set_find(key, id, ip);
1285 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1287 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1289 + if (set_hash != NULL)
1290 + __set_hash_del(set_hash);
1291 + write_unlock_bh(&ip_set_lock);
1296 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1298 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1299 + % ip_set_bindings_hash_size;
1300 + struct ip_set_hash *set_hash;
1303 + IP_SET_ASSERT(ip_set_list[id]);
1304 + IP_SET_ASSERT(ip_set_list[binding]);
1305 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1306 + HIPQUAD(ip), ip_set_list[binding]->name);
1307 + write_lock_bh(&ip_set_lock);
1308 + set_hash = __ip_set_find(key, id, ip);
1310 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_ATOMIC);
1315 + INIT_LIST_HEAD(&set_hash->list);
1316 + set_hash->id = id;
1317 + set_hash->ip = ip;
1318 + list_add(&set_hash->list, &ip_set_hash[key]);
1320 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1321 + DP("overwrite binding: %s",
1322 + ip_set_list[set_hash->binding]->name);
1323 + __ip_set_put(set_hash->binding);
1325 + set_hash->binding = binding;
1326 + __ip_set_get(set_hash->binding);
1327 + DP("stored: key %u, id %u (%s), ip %u.%u.%u.%u, binding %u (%s)",
1328 + key, id, ip_set_list[id]->name,
1329 + HIPQUAD(ip), binding, ip_set_list[binding]->name);
1331 + write_unlock_bh(&ip_set_lock);
1335 +#define FOREACH_HASH_DO(fn, args...) \
1337 + ip_set_id_t __key; \
1338 + struct ip_set_hash *__set_hash; \
1340 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1341 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1342 + fn(__set_hash , ## args); \
1346 +#define FOREACH_HASH_RW_DO(fn, args...) \
1348 + ip_set_id_t __key; \
1349 + struct ip_set_hash *__set_hash, *__n; \
1351 + ASSERT_WRITE_LOCK(&ip_set_lock); \
1352 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1353 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1354 + fn(__set_hash , ## args); \
1358 +/* Add, del and test set entries from kernel */
1360 +#define follow_bindings(index, set, ip) \
1361 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1362 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1365 +ip_set_testip_kernel(ip_set_id_t index,
1366 + const struct sk_buff *skb,
1367 + const u_int32_t *flags)
1369 + struct ip_set *set;
1372 + unsigned char i = 0;
1374 + IP_SET_ASSERT(flags[i]);
1375 + read_lock_bh(&ip_set_lock);
1377 + set = ip_set_list[index];
1378 + IP_SET_ASSERT(set);
1379 + DP("set %s, index %u", set->name, index);
1380 + read_lock_bh(&set->lock);
1381 + res = set->type->testip_kernel(set, skb, &ip, flags, i++);
1382 + read_unlock_bh(&set->lock);
1383 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1386 + && follow_bindings(index, set, ip));
1387 + read_unlock_bh(&ip_set_lock);
1393 +ip_set_addip_kernel(ip_set_id_t index,
1394 + const struct sk_buff *skb,
1395 + const u_int32_t *flags)
1397 + struct ip_set *set;
1400 + unsigned char i = 0;
1402 + IP_SET_ASSERT(flags[i]);
1404 + read_lock_bh(&ip_set_lock);
1406 + set = ip_set_list[index];
1407 + IP_SET_ASSERT(set);
1408 + DP("set %s, index %u", set->name, index);
1409 + write_lock_bh(&set->lock);
1410 + res = set->type->addip_kernel(set, skb, &ip, flags, i++);
1411 + write_unlock_bh(&set->lock);
1412 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1413 + } while ((res == 0 || res == -EEXIST)
1415 + && follow_bindings(index, set, ip));
1416 + read_unlock_bh(&ip_set_lock);
1418 + if (res == -EAGAIN
1419 + && set->type->retry
1420 + && (res = set->type->retry(set)) == 0)
1425 +ip_set_delip_kernel(ip_set_id_t index,
1426 + const struct sk_buff *skb,
1427 + const u_int32_t *flags)
1429 + struct ip_set *set;
1432 + unsigned char i = 0;
1434 + IP_SET_ASSERT(flags[i]);
1435 + read_lock_bh(&ip_set_lock);
1437 + set = ip_set_list[index];
1438 + IP_SET_ASSERT(set);
1439 + DP("set %s, index %u", set->name, index);
1440 + write_lock_bh(&set->lock);
1441 + res = set->type->delip_kernel(set, skb, &ip, flags, i++);
1442 + write_unlock_bh(&set->lock);
1443 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1444 + } while ((res == 0 || res == -EEXIST)
1446 + && follow_bindings(index, set, ip));
1447 + read_unlock_bh(&ip_set_lock);
1450 +/* Register and deregister settype */
1452 +static inline struct ip_set_type *
1453 +find_set_type(const char *name)
1455 + struct ip_set_type *set_type;
1457 + list_for_each_entry(set_type, &set_type_list, list)
1458 + if (!strncmp(set_type->typename, name, IP_SET_MAXNAMELEN - 1))
1464 +ip_set_register_set_type(struct ip_set_type *set_type)
1468 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1469 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1470 + set_type->typename,
1471 + set_type->protocol_version,
1472 + IP_SET_PROTOCOL_VERSION);
1476 + write_lock_bh(&ip_set_lock);
1477 + if (find_set_type(set_type->typename)) {
1479 + ip_set_printk("'%s' already registered!",
1480 + set_type->typename);
1484 + if (!try_module_get(THIS_MODULE)) {
1488 + list_add(&set_type->list, &set_type_list);
1489 + DP("'%s' registered.", set_type->typename);
1491 + write_unlock_bh(&ip_set_lock);
1496 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1498 + write_lock_bh(&ip_set_lock);
1499 + if (!find_set_type(set_type->typename)) {
1500 + ip_set_printk("'%s' not registered?",
1501 + set_type->typename);
1504 + list_del(&set_type->list);
1505 + module_put(THIS_MODULE);
1506 + DP("'%s' unregistered.", set_type->typename);
1508 + write_unlock_bh(&ip_set_lock);
1513 + * Userspace routines
1517 + * Find set by name, reference it once. The reference makes sure the
1518 + * thing pointed to, does not go away under our feet. Drop the reference
1519 + * later, using ip_set_put().
1522 +ip_set_get_byname(const char *name)
1524 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1526 + down(&ip_set_app_mutex);
1527 + for (i = 0; i < ip_set_max; i++) {
1528 + if (ip_set_list[i] != NULL
1529 + && strcmp(ip_set_list[i]->name, name) == 0) {
1535 + up(&ip_set_app_mutex);
1540 + * Find set by index, reference it once. The reference makes sure the
1541 + * thing pointed to, does not go away under our feet. Drop the reference
1542 + * later, using ip_set_put().
1545 +ip_set_get_byindex(ip_set_id_t index)
1547 + down(&ip_set_app_mutex);
1549 + if (index >= ip_set_max)
1550 + return IP_SET_INVALID_ID;
1552 + if (ip_set_list[index])
1553 + __ip_set_get(index);
1555 + index = IP_SET_INVALID_ID;
1557 + up(&ip_set_app_mutex);
1562 + * If the given set pointer points to a valid set, decrement
1563 + * reference count by 1. The caller shall not assume the index
1564 + * to be valid, after calling this function.
1566 +void ip_set_put(ip_set_id_t index)
1568 + down(&ip_set_app_mutex);
1569 + if (ip_set_list[index])
1570 + __ip_set_put(index);
1571 + up(&ip_set_app_mutex);
1574 +/* Find a set by name or index */
1576 +ip_set_find_byname(const char *name)
1578 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1580 + for (i = 0; i < ip_set_max; i++) {
1581 + if (ip_set_list[i] != NULL
1582 + && strcmp(ip_set_list[i]->name, name) == 0) {
1591 +ip_set_find_byindex(ip_set_id_t index)
1593 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1594 + index = IP_SET_INVALID_ID;
1600 + * Add, del, test, bind and unbind
1604 +__ip_set_testip(struct ip_set *set,
1611 + read_lock_bh(&set->lock);
1612 + res = set->type->testip(set, data, size, ip);
1613 + read_unlock_bh(&set->lock);
1619 +__ip_set_addip(ip_set_id_t index,
1623 + struct ip_set *set = ip_set_list[index];
1627 + IP_SET_ASSERT(set);
1629 + write_lock_bh(&set->lock);
1630 + res = set->type->addip(set, data, size, &ip);
1631 + write_unlock_bh(&set->lock);
1632 + } while (res == -EAGAIN
1633 + && set->type->retry
1634 + && (res = set->type->retry(set)) == 0);
1640 +ip_set_addip(ip_set_id_t index,
1645 + return __ip_set_addip(index,
1646 + data + sizeof(struct ip_set_req_adt),
1647 + size - sizeof(struct ip_set_req_adt));
1651 +ip_set_delip(ip_set_id_t index,
1655 + struct ip_set *set = ip_set_list[index];
1659 + IP_SET_ASSERT(set);
1660 + write_lock_bh(&set->lock);
1661 + res = set->type->delip(set,
1662 + data + sizeof(struct ip_set_req_adt),
1663 + size - sizeof(struct ip_set_req_adt),
1665 + write_unlock_bh(&set->lock);
1671 +ip_set_testip(ip_set_id_t index,
1675 + struct ip_set *set = ip_set_list[index];
1679 + IP_SET_ASSERT(set);
1680 + res = __ip_set_testip(set,
1681 + data + sizeof(struct ip_set_req_adt),
1682 + size - sizeof(struct ip_set_req_adt),
1685 + return (res > 0 ? -EEXIST : res);
1689 +ip_set_bindip(ip_set_id_t index,
1693 + struct ip_set *set = ip_set_list[index];
1694 + struct ip_set_req_bind *req_bind;
1695 + ip_set_id_t binding;
1699 + IP_SET_ASSERT(set);
1700 + if (size < sizeof(struct ip_set_req_bind))
1703 + req_bind = (struct ip_set_req_bind *) data;
1704 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1706 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1707 + /* Default binding of a set */
1708 + char *binding_name;
1710 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1713 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1714 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1716 + binding = ip_set_find_byname(binding_name);
1717 + if (binding == IP_SET_INVALID_ID)
1720 + write_lock_bh(&ip_set_lock);
1721 + /* Sets as binding values are referenced */
1722 + if (set->binding != IP_SET_INVALID_ID)
1723 + __ip_set_put(set->binding);
1724 + set->binding = binding;
1725 + __ip_set_get(set->binding);
1726 + write_unlock_bh(&ip_set_lock);
1730 + binding = ip_set_find_byname(req_bind->binding);
1731 + if (binding == IP_SET_INVALID_ID)
1734 + res = __ip_set_testip(set,
1735 + data + sizeof(struct ip_set_req_bind),
1736 + size - sizeof(struct ip_set_req_bind),
1738 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1739 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1742 + res = ip_set_hash_add(set->id, ip, binding);
1747 +#define FOREACH_SET_DO(fn, args...) \
1749 + ip_set_id_t __i; \
1750 + struct ip_set *__set; \
1752 + for (__i = 0; __i < ip_set_max; __i++) { \
1753 + __set = ip_set_list[__i]; \
1754 + if (__set != NULL) \
1755 + fn(__set , ##args); \
1760 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1762 + if (set_hash->id == id)
1763 + __set_hash_del(set_hash);
1767 +__unbind_default(struct ip_set *set)
1769 + if (set->binding != IP_SET_INVALID_ID) {
1770 + /* Sets as binding values are referenced */
1771 + __ip_set_put(set->binding);
1772 + set->binding = IP_SET_INVALID_ID;
1777 +ip_set_unbindip(ip_set_id_t index,
1781 + struct ip_set *set;
1782 + struct ip_set_req_bind *req_bind;
1787 + if (size < sizeof(struct ip_set_req_bind))
1790 + req_bind = (struct ip_set_req_bind *) data;
1791 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1793 + DP("%u %s", index, req_bind->binding);
1794 + if (index == IP_SET_INVALID_ID) {
1795 + /* unbind :all: */
1796 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1797 + /* Default binding of sets */
1798 + write_lock_bh(&ip_set_lock);
1799 + FOREACH_SET_DO(__unbind_default);
1800 + write_unlock_bh(&ip_set_lock);
1802 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1803 + /* Flush all bindings of all sets*/
1804 + write_lock_bh(&ip_set_lock);
1805 + FOREACH_HASH_RW_DO(__set_hash_del);
1806 + write_unlock_bh(&ip_set_lock);
1809 + DP("unreachable reached!");
1813 + set = ip_set_list[index];
1814 + IP_SET_ASSERT(set);
1815 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1816 + /* Default binding of set */
1817 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1819 + if (binding == IP_SET_INVALID_ID)
1822 + write_lock_bh(&ip_set_lock);
1823 + /* Sets in hash values are referenced */
1824 + __ip_set_put(set->binding);
1825 + set->binding = IP_SET_INVALID_ID;
1826 + write_unlock_bh(&ip_set_lock);
1829 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1830 + /* Flush all bindings */
1832 + write_lock_bh(&ip_set_lock);
1833 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1834 + write_unlock_bh(&ip_set_lock);
1838 + res = __ip_set_testip(set,
1839 + data + sizeof(struct ip_set_req_bind),
1840 + size - sizeof(struct ip_set_req_bind),
1843 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1845 + res = ip_set_hash_del(set->id, ip);
1851 +ip_set_testbind(ip_set_id_t index,
1855 + struct ip_set *set = ip_set_list[index];
1856 + struct ip_set_req_bind *req_bind;
1857 + ip_set_id_t binding;
1861 + IP_SET_ASSERT(set);
1862 + if (size < sizeof(struct ip_set_req_bind))
1865 + req_bind = (struct ip_set_req_bind *) data;
1866 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1868 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1869 + /* Default binding of set */
1870 + char *binding_name;
1872 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1875 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1876 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1878 + binding = ip_set_find_byname(binding_name);
1879 + if (binding == IP_SET_INVALID_ID)
1882 + res = (set->binding == binding) ? -EEXIST : 0;
1886 + binding = ip_set_find_byname(req_bind->binding);
1887 + if (binding == IP_SET_INVALID_ID)
1891 + res = __ip_set_testip(set,
1892 + data + sizeof(struct ip_set_req_bind),
1893 + size - sizeof(struct ip_set_req_bind),
1895 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1896 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1899 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1905 +static struct ip_set_type *
1906 +find_set_type_rlock(const char *typename)
1908 + struct ip_set_type *type;
1910 + read_lock_bh(&ip_set_lock);
1911 + type = find_set_type(typename);
1913 + read_unlock_bh(&ip_set_lock);
1919 +find_free_id(const char *name,
1920 + ip_set_id_t *index,
1925 + *id = IP_SET_INVALID_ID;
1926 + for (i = 0; i < ip_set_max; i++) {
1927 + if (ip_set_list[i] == NULL) {
1928 + if (*id == IP_SET_INVALID_ID)
1930 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1934 + if (*id == IP_SET_INVALID_ID)
1935 + /* No free slot remained */
1937 + /* Check that index is usable as id (swapping) */
1939 + for (i = 0; i < ip_set_max; i++) {
1940 + if (ip_set_list[i] != NULL
1941 + && ip_set_list[i]->id == *id) {
1953 +ip_set_create(const char *name,
1954 + const char *typename,
1955 + ip_set_id_t restore,
1959 + struct ip_set *set;
1960 + ip_set_id_t index = 0, id;
1963 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1965 + * First, and without any locks, allocate and initialize
1966 + * a normal base set structure.
1968 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1971 + set->lock = RW_LOCK_UNLOCKED;
1972 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1973 + set->binding = IP_SET_INVALID_ID;
1974 + atomic_set(&set->ref, 0);
1977 + * Next, take the &ip_set_lock, check that we know the type,
1978 + * and take a reference on the type, to make sure it
1979 + * stays available while constructing our new set.
1981 + * After referencing the type, we drop the &ip_set_lock,
1982 + * and let the new set construction run without locks.
1984 + set->type = find_set_type_rlock(typename);
1985 + if (set->type == NULL) {
1986 + /* Try loading the module */
1987 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1988 + strcpy(modulename, "ip_set_");
1989 + strcat(modulename, typename);
1990 + DP("try to load %s", modulename);
1991 + request_module(modulename);
1992 + set->type = find_set_type_rlock(typename);
1994 + if (set->type == NULL) {
1995 + ip_set_printk("no set type '%s', set '%s' not created",
2000 + if (!try_module_get(set->type->me)) {
2001 + read_unlock_bh(&ip_set_lock);
2005 + read_unlock_bh(&ip_set_lock);
2008 + * Without holding any locks, create private part.
2010 + res = set->type->create(set, data, size);
2014 + /* BTW, res==0 here. */
2017 + * Here, we have a valid, constructed set. &ip_set_lock again,
2018 + * find free id/index and check that it is not already in
2021 + write_lock_bh(&ip_set_lock);
2022 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
2023 + DP("no free id!");
2027 + /* Make sure restore gets the same index */
2028 + if (restore != IP_SET_INVALID_ID && index != restore) {
2029 + DP("Can't restore, sets are screwed up");
2035 + * Finally! Add our shiny new set to the list, and be done.
2037 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2039 + ip_set_list[index] = set;
2040 + write_unlock_bh(&ip_set_lock);
2044 + write_unlock_bh(&ip_set_lock);
2045 + set->type->destroy(set);
2047 + module_put(set->type->me);
2054 + * Destroy a given existing set
2057 +ip_set_destroy_set(ip_set_id_t index)
2059 + struct ip_set *set = ip_set_list[index];
2061 + IP_SET_ASSERT(set);
2062 + DP("set: %s", set->name);
2063 + write_lock_bh(&ip_set_lock);
2064 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2065 + if (set->binding != IP_SET_INVALID_ID)
2066 + __ip_set_put(set->binding);
2067 + ip_set_list[index] = NULL;
2068 + write_unlock_bh(&ip_set_lock);
2070 + /* Must call it without holding any lock */
2071 + set->type->destroy(set);
2072 + module_put(set->type->me);
2077 + * Destroy a set - or all sets
2078 + * Sets must not be referenced/used.
2081 +ip_set_destroy(ip_set_id_t index)
2085 + /* ref modification always protected by the mutex */
2086 + if (index != IP_SET_INVALID_ID) {
2087 + if (atomic_read(&ip_set_list[index]->ref))
2089 + ip_set_destroy_set(index);
2091 + for (i = 0; i < ip_set_max; i++) {
2092 + if (ip_set_list[i] != NULL
2093 + && (atomic_read(&ip_set_list[i]->ref)))
2097 + for (i = 0; i < ip_set_max; i++) {
2098 + if (ip_set_list[i] != NULL)
2099 + ip_set_destroy_set(i);
2106 +ip_set_flush_set(struct ip_set *set)
2108 + DP("set: %s %u", set->name, set->id);
2110 + write_lock_bh(&set->lock);
2111 + set->type->flush(set);
2112 + write_unlock_bh(&set->lock);
2116 + * Flush data in a set - or in all sets
2119 +ip_set_flush(ip_set_id_t index)
2121 + if (index != IP_SET_INVALID_ID) {
2122 + IP_SET_ASSERT(ip_set_list[index]);
2123 + ip_set_flush_set(ip_set_list[index]);
2125 + FOREACH_SET_DO(ip_set_flush_set);
2132 +ip_set_rename(ip_set_id_t index, const char *name)
2134 + struct ip_set *set = ip_set_list[index];
2138 + DP("set: %s to %s", set->name, name);
2139 + write_lock_bh(&ip_set_lock);
2140 + for (i = 0; i < ip_set_max; i++) {
2141 + if (ip_set_list[i] != NULL
2142 + && strncmp(ip_set_list[i]->name,
2144 + IP_SET_MAXNAMELEN - 1) == 0) {
2149 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2151 + write_unlock_bh(&ip_set_lock);
2156 + * Swap two sets so that name/index points to the other.
2157 + * References are also swapped.
2160 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2162 + struct ip_set *from = ip_set_list[from_index];
2163 + struct ip_set *to = ip_set_list[to_index];
2164 + char from_name[IP_SET_MAXNAMELEN];
2165 + u_int32_t from_ref;
2167 + DP("set: %s to %s", from->name, to->name);
2168 + /* Features must not change. Artifical restriction. */
2169 + if (from->type->features != to->type->features)
2172 + /* No magic here: ref munging protected by the mutex */
2173 + write_lock_bh(&ip_set_lock);
2174 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2175 + from_ref = atomic_read(&from->ref);
2177 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2178 + atomic_set(&from->ref, atomic_read(&to->ref));
2179 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2180 + atomic_set(&to->ref, from_ref);
2182 + ip_set_list[from_index] = to;
2183 + ip_set_list[to_index] = from;
2185 + write_unlock_bh(&ip_set_lock);
2194 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2195 + ip_set_id_t id, size_t *size)
2197 + if (set_hash->id == id)
2198 + *size += sizeof(struct ip_set_hash_list);
2202 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2203 + ip_set_id_t id, size_t *size)
2205 + if (set_hash->id == id)
2206 + *size += sizeof(struct ip_set_hash_save);
2210 +__set_hash_bindings(struct ip_set_hash *set_hash,
2211 + ip_set_id_t id, void *data, int *used)
2213 + if (set_hash->id == id) {
2214 + struct ip_set_hash_list *hash_list =
2215 + (struct ip_set_hash_list *)(data + *used);
2217 + hash_list->ip = set_hash->ip;
2218 + hash_list->binding = set_hash->binding;
2219 + *used += sizeof(struct ip_set_hash_list);
2223 +static int ip_set_list_set(ip_set_id_t index,
2228 + struct ip_set *set = ip_set_list[index];
2229 + struct ip_set_list *set_list;
2231 + /* Pointer to our header */
2232 + set_list = (struct ip_set_list *) (data + *used);
2234 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2236 + /* Get and ensure header size */
2237 + if (*used + sizeof(struct ip_set_list) > len)
2238 + goto not_enough_mem;
2239 + *used += sizeof(struct ip_set_list);
2241 + read_lock_bh(&set->lock);
2242 + /* Get and ensure set specific header size */
2243 + set_list->header_size = set->type->header_size;
2244 + if (*used + set_list->header_size > len)
2247 + /* Fill in the header */
2248 + set_list->index = index;
2249 + set_list->binding = set->binding;
2250 + set_list->ref = atomic_read(&set->ref);
2252 + /* Fill in set spefific header data */
2253 + set->type->list_header(set, data + *used);
2254 + *used += set_list->header_size;
2256 + /* Get and ensure set specific members size */
2257 + set_list->members_size = set->type->list_members_size(set);
2258 + if (*used + set_list->members_size > len)
2261 + /* Fill in set spefific members data */
2262 + set->type->list_members(set, data + *used);
2263 + *used += set_list->members_size;
2264 + read_unlock_bh(&set->lock);
2268 + /* Get and ensure set specific bindings size */
2269 + set_list->bindings_size = 0;
2270 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2271 + set->id, &set_list->bindings_size);
2272 + if (*used + set_list->bindings_size > len)
2273 + goto not_enough_mem;
2275 + /* Fill in set spefific bindings data */
2276 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2281 + read_unlock_bh(&set->lock);
2283 + DP("not enough mem, try again");
2290 +static int ip_set_save_set(ip_set_id_t index,
2295 + struct ip_set *set;
2296 + struct ip_set_save *set_save;
2298 + /* Pointer to our header */
2299 + set_save = (struct ip_set_save *) (data + *used);
2301 + /* Get and ensure header size */
2302 + if (*used + sizeof(struct ip_set_save) > len)
2303 + goto not_enough_mem;
2304 + *used += sizeof(struct ip_set_save);
2306 + set = ip_set_list[index];
2307 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2308 + data, data + *used);
2310 + read_lock_bh(&set->lock);
2311 + /* Get and ensure set specific header size */
2312 + set_save->header_size = set->type->header_size;
2313 + if (*used + set_save->header_size > len)
2316 + /* Fill in the header */
2317 + set_save->index = index;
2318 + set_save->binding = set->binding;
2320 + /* Fill in set spefific header data */
2321 + set->type->list_header(set, data + *used);
2322 + *used += set_save->header_size;
2324 + DP("set header filled: %s, used: %u(%u) %p %p", set->name, *used,
2325 + set_save->header_size, data, data + *used);
2326 + /* Get and ensure set specific members size */
2327 + set_save->members_size = set->type->list_members_size(set);
2328 + if (*used + set_save->members_size > len)
2331 + /* Fill in set spefific members data */
2332 + set->type->list_members(set, data + *used);
2333 + *used += set_save->members_size;
2334 + read_unlock_bh(&set->lock);
2335 + DP("set members filled: %s, used: %u(%u) %p %p", set->name, *used,
2336 + set_save->members_size, data, data + *used);
2340 + read_unlock_bh(&set->lock);
2342 + DP("not enough mem, try again");
2347 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2355 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2356 + struct ip_set_hash_save *hash_save =
2357 + (struct ip_set_hash_save *)(data + *used);
2358 + /* Ensure bindings size */
2359 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2363 + hash_save->id = set_hash->id;
2364 + hash_save->ip = set_hash->ip;
2365 + hash_save->binding = set_hash->binding;
2366 + *used += sizeof(struct ip_set_hash_save);
2370 +static int ip_set_save_bindings(ip_set_id_t index,
2376 + struct ip_set_save *set_save;
2378 + DP("used %u, len %u", *used, len);
2379 + /* Get and ensure header size */
2380 + if (*used + sizeof(struct ip_set_save) > len)
2384 + set_save = (struct ip_set_save *) (data + *used);
2385 + set_save->index = IP_SET_INVALID_ID;
2386 + set_save->header_size = 0;
2387 + set_save->members_size = 0;
2388 + *used += sizeof(struct ip_set_save);
2390 + DP("marker added used %u, len %u", *used, len);
2391 + /* Fill in bindings data */
2392 + if (index != IP_SET_INVALID_ID)
2393 + /* Sets are identified by id in hash */
2394 + index = ip_set_list[index]->id;
2395 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2403 +static int ip_set_restore(void *data,
2407 + int line = 0, used = 0, members_size;
2408 + struct ip_set *set;
2409 + struct ip_set_hash_save *hash_save;
2410 + struct ip_set_restore *set_restore;
2411 + ip_set_id_t index;
2413 + /* Loop to restore sets */
2417 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2418 + /* Get and ensure header size */
2419 + if (used + sizeof(struct ip_set_restore) > len)
2421 + set_restore = (struct ip_set_restore *) (data + used);
2422 + used += sizeof(struct ip_set_restore);
2424 + /* Ensure data size */
2426 + + set_restore->header_size
2427 + + set_restore->members_size > len)
2430 + /* Check marker */
2431 + if (set_restore->index == IP_SET_INVALID_ID) {
2436 + /* Try to create the set */
2437 + DP("restore %s %s", set_restore->name, set_restore->typename);
2438 + res = ip_set_create(set_restore->name,
2439 + set_restore->typename,
2440 + set_restore->index,
2442 + set_restore->header_size);
2446 + used += set_restore->header_size;
2448 + index = ip_set_find_byindex(set_restore->index);
2449 + DP("index %u, restore_index %u", index, set_restore->index);
2450 + if (index != set_restore->index)
2452 + /* Try to restore members data */
2453 + set = ip_set_list[index];
2455 + DP("members_size %u reqsize %u",
2456 + set_restore->members_size, set->type->reqsize);
2457 + while (members_size + set->type->reqsize <=
2458 + set_restore->members_size) {
2460 + DP("members: %u, line %u", members_size, line);
2461 + res = __ip_set_addip(index,
2462 + data + used + members_size,
2463 + set->type->reqsize);
2464 + if (!(res == 0 || res == -EEXIST))
2466 + members_size += set->type->reqsize;
2469 + DP("members_size %u %u",
2470 + set_restore->members_size, members_size);
2471 + if (members_size != set_restore->members_size)
2473 + used += set_restore->members_size;
2477 + /* Loop to restore bindings */
2478 + while (used < len) {
2481 + DP("restore binding, line %u", line);
2482 + /* Get and ensure size */
2483 + if (used + sizeof(struct ip_set_hash_save) > len)
2485 + hash_save = (struct ip_set_hash_save *) (data + used);
2486 + used += sizeof(struct ip_set_hash_save);
2488 + /* hash_save->id is used to store the index */
2489 + index = ip_set_find_byindex(hash_save->id);
2490 + DP("restore binding index %u, id %u, %u -> %u",
2491 + index, hash_save->id, hash_save->ip, hash_save->binding);
2492 + if (index != hash_save->id)
2494 + if (ip_set_find_byindex(hash_save->binding) == IP_SET_INVALID_ID) {
2495 + DP("corrupt binding set index %u", hash_save->binding);
2498 + set = ip_set_list[hash_save->id];
2499 + /* Null valued IP means default binding */
2500 + if (hash_save->ip)
2501 + res = ip_set_hash_add(set->id,
2503 + hash_save->binding);
2505 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2506 + write_lock_bh(&ip_set_lock);
2507 + set->binding = hash_save->binding;
2508 + __ip_set_get(set->binding);
2509 + write_unlock_bh(&ip_set_lock);
2510 + DP("default binding: %u", set->binding);
2522 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2525 + int res = 0; /* Assume OK */
2527 + struct ip_set_req_adt *req_adt;
2528 + ip_set_id_t index = IP_SET_INVALID_ID;
2529 + int (*adtfn)(ip_set_id_t index,
2530 + const void *data, size_t size);
2532 + int (*fn)(ip_set_id_t index,
2533 + const void *data, size_t size);
2535 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2536 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2539 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2540 + if (!capable(CAP_NET_ADMIN))
2542 + if (optval != SO_IP_SET)
2544 + if (len <= sizeof(unsigned)) {
2545 + ip_set_printk("short userdata (want >%zu, got %u)",
2546 + sizeof(unsigned), len);
2549 + data = vmalloc(len);
2551 + DP("out of mem for %u bytes", len);
2554 + if (copy_from_user(data, user, len) != 0) {
2558 + if (down_interruptible(&ip_set_app_mutex)) {
2563 + op = (unsigned *)data;
2566 + if (*op < IP_SET_OP_VERSION) {
2567 + /* Check the version at the beginning of operations */
2568 + struct ip_set_req_version *req_version =
2569 + (struct ip_set_req_version *) data;
2570 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2577 + case IP_SET_OP_CREATE:{
2578 + struct ip_set_req_create *req_create
2579 + = (struct ip_set_req_create *) data;
2581 + if (len < sizeof(struct ip_set_req_create)) {
2582 + ip_set_printk("short CREATE data (want >=%zu, got %u)",
2583 + sizeof(struct ip_set_req_create), len);
2587 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2588 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2589 + res = ip_set_create(req_create->name,
2590 + req_create->typename,
2591 + IP_SET_INVALID_ID,
2592 + data + sizeof(struct ip_set_req_create),
2593 + len - sizeof(struct ip_set_req_create));
2596 + case IP_SET_OP_DESTROY:{
2597 + struct ip_set_req_std *req_destroy
2598 + = (struct ip_set_req_std *) data;
2600 + if (len != sizeof(struct ip_set_req_std)) {
2601 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2602 + sizeof(struct ip_set_req_std), len);
2606 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2607 + /* Destroy all sets */
2608 + index = IP_SET_INVALID_ID;
2610 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2611 + index = ip_set_find_byname(req_destroy->name);
2613 + if (index == IP_SET_INVALID_ID) {
2619 + res = ip_set_destroy(index);
2622 + case IP_SET_OP_FLUSH:{
2623 + struct ip_set_req_std *req_flush =
2624 + (struct ip_set_req_std *) data;
2626 + if (len != sizeof(struct ip_set_req_std)) {
2627 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2628 + sizeof(struct ip_set_req_std), len);
2632 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2633 + /* Flush all sets */
2634 + index = IP_SET_INVALID_ID;
2636 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2637 + index = ip_set_find_byname(req_flush->name);
2639 + if (index == IP_SET_INVALID_ID) {
2644 + res = ip_set_flush(index);
2647 + case IP_SET_OP_RENAME:{
2648 + struct ip_set_req_create *req_rename
2649 + = (struct ip_set_req_create *) data;
2651 + if (len != sizeof(struct ip_set_req_create)) {
2652 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2653 + sizeof(struct ip_set_req_create), len);
2658 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2659 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2661 + index = ip_set_find_byname(req_rename->name);
2662 + if (index == IP_SET_INVALID_ID) {
2666 + res = ip_set_rename(index, req_rename->typename);
2669 + case IP_SET_OP_SWAP:{
2670 + struct ip_set_req_create *req_swap
2671 + = (struct ip_set_req_create *) data;
2672 + ip_set_id_t to_index;
2674 + if (len != sizeof(struct ip_set_req_create)) {
2675 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2676 + sizeof(struct ip_set_req_create), len);
2681 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2682 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2684 + index = ip_set_find_byname(req_swap->name);
2685 + if (index == IP_SET_INVALID_ID) {
2689 + to_index = ip_set_find_byname(req_swap->typename);
2690 + if (to_index == IP_SET_INVALID_ID) {
2694 + res = ip_set_swap(index, to_index);
2698 + break; /* Set identified by id */
2701 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2702 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2706 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2708 + if (len < sizeof(struct ip_set_req_adt)) {
2709 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2710 + sizeof(struct ip_set_req_adt), len);
2714 + req_adt = (struct ip_set_req_adt *) data;
2716 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2717 + if (!(*op == IP_SET_OP_UNBIND_SET
2718 + && req_adt->index == IP_SET_INVALID_ID)) {
2719 + index = ip_set_find_byindex(req_adt->index);
2720 + if (index == IP_SET_INVALID_ID) {
2725 + res = adtfn(index, data, len);
2728 + up(&ip_set_app_mutex);
2732 + DP("final result %d", res);
2737 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2741 + ip_set_id_t index = IP_SET_INVALID_ID;
2743 + int copylen = *len;
2745 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2746 + if (!capable(CAP_NET_ADMIN))
2748 + if (optval != SO_IP_SET)
2750 + if (*len < sizeof(unsigned)) {
2751 + ip_set_printk("short userdata (want >=%zu, got %d)",
2752 + sizeof(unsigned), *len);
2755 + data = vmalloc(*len);
2757 + DP("out of mem for %d bytes", *len);
2760 + if (copy_from_user(data, user, *len) != 0) {
2764 + if (down_interruptible(&ip_set_app_mutex)) {
2769 + op = (unsigned *) data;
2772 + if (*op < IP_SET_OP_VERSION) {
2773 + /* Check the version at the beginning of operations */
2774 + struct ip_set_req_version *req_version =
2775 + (struct ip_set_req_version *) data;
2776 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2783 + case IP_SET_OP_VERSION: {
2784 + struct ip_set_req_version *req_version =
2785 + (struct ip_set_req_version *) data;
2787 + if (*len != sizeof(struct ip_set_req_version)) {
2788 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2789 + sizeof(struct ip_set_req_version),
2795 + req_version->version = IP_SET_PROTOCOL_VERSION;
2796 + res = copy_to_user(user, req_version,
2797 + sizeof(struct ip_set_req_version));
2800 + case IP_SET_OP_GET_BYNAME: {
2801 + struct ip_set_req_get_set *req_get
2802 + = (struct ip_set_req_get_set *) data;
2804 + if (*len != sizeof(struct ip_set_req_get_set)) {
2805 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2806 + sizeof(struct ip_set_req_get_set), *len);
2810 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2811 + index = ip_set_find_byname(req_get->set.name);
2812 + req_get->set.index = index;
2815 + case IP_SET_OP_GET_BYINDEX: {
2816 + struct ip_set_req_get_set *req_get
2817 + = (struct ip_set_req_get_set *) data;
2819 + if (*len != sizeof(struct ip_set_req_get_set)) {
2820 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2821 + sizeof(struct ip_set_req_get_set), *len);
2825 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2826 + index = ip_set_find_byindex(req_get->set.index);
2827 + strncpy(req_get->set.name,
2828 + index == IP_SET_INVALID_ID ? ""
2829 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2832 + case IP_SET_OP_ADT_GET: {
2833 + struct ip_set_req_adt_get *req_get
2834 + = (struct ip_set_req_adt_get *) data;
2836 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2837 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2838 + sizeof(struct ip_set_req_adt_get), *len);
2842 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2843 + index = ip_set_find_byname(req_get->set.name);
2844 + if (index != IP_SET_INVALID_ID) {
2845 + req_get->set.index = index;
2846 + strncpy(req_get->typename,
2847 + ip_set_list[index]->type->typename,
2848 + IP_SET_MAXNAMELEN - 1);
2855 + case IP_SET_OP_MAX_SETS: {
2856 + struct ip_set_req_max_sets *req_max_sets
2857 + = (struct ip_set_req_max_sets *) data;
2860 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2861 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2862 + sizeof(struct ip_set_req_max_sets), *len);
2867 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2868 + req_max_sets->set.index = IP_SET_INVALID_ID;
2870 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2871 + req_max_sets->set.index =
2872 + ip_set_find_byname(req_max_sets->set.name);
2873 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2878 + req_max_sets->max_sets = ip_set_max;
2879 + req_max_sets->sets = 0;
2880 + for (i = 0; i < ip_set_max; i++) {
2881 + if (ip_set_list[i] != NULL)
2882 + req_max_sets->sets++;
2886 + case IP_SET_OP_LIST_SIZE:
2887 + case IP_SET_OP_SAVE_SIZE: {
2888 + struct ip_set_req_setnames *req_setnames
2889 + = (struct ip_set_req_setnames *) data;
2890 + struct ip_set_name_list *name_list;
2891 + struct ip_set *set;
2895 + if (*len < sizeof(struct ip_set_req_setnames)) {
2896 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2897 + sizeof(struct ip_set_req_setnames), *len);
2902 + req_setnames->size = 0;
2903 + used = sizeof(struct ip_set_req_setnames);
2904 + for (i = 0; i < ip_set_max; i++) {
2905 + if (ip_set_list[i] == NULL)
2907 + name_list = (struct ip_set_name_list *)
2909 + used += sizeof(struct ip_set_name_list);
2910 + if (used > copylen) {
2914 + set = ip_set_list[i];
2915 + /* Fill in index, name, etc. */
2916 + name_list->index = i;
2917 + name_list->id = set->id;
2918 + strncpy(name_list->name,
2920 + IP_SET_MAXNAMELEN - 1);
2921 + strncpy(name_list->typename,
2922 + set->type->typename,
2923 + IP_SET_MAXNAMELEN - 1);
2924 + DP("filled %s of type %s, index %u\n",
2925 + name_list->name, name_list->typename,
2926 + name_list->index);
2927 + if (!(req_setnames->index == IP_SET_INVALID_ID
2928 + || req_setnames->index == i))
2932 + case IP_SET_OP_LIST_SIZE: {
2933 + req_setnames->size += sizeof(struct ip_set_list)
2934 + + set->type->header_size
2935 + + set->type->list_members_size(set);
2936 + /* Sets are identified by id in the hash */
2937 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2938 + set->id, &req_setnames->size);
2941 + case IP_SET_OP_SAVE_SIZE: {
2942 + req_setnames->size += sizeof(struct ip_set_save)
2943 + + set->type->header_size
2944 + + set->type->list_members_size(set);
2945 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2946 + set->id, &req_setnames->size);
2953 + if (copylen != used) {
2959 + case IP_SET_OP_LIST: {
2960 + struct ip_set_req_list *req_list
2961 + = (struct ip_set_req_list *) data;
2965 + if (*len < sizeof(struct ip_set_req_list)) {
2966 + ip_set_printk("short LIST (want >=%zu, got %d)",
2967 + sizeof(struct ip_set_req_list), *len);
2971 + index = req_list->index;
2972 + if (index != IP_SET_INVALID_ID
2973 + && ip_set_find_byindex(index) != index) {
2978 + if (index == IP_SET_INVALID_ID) {
2979 + /* List all sets */
2980 + for (i = 0; i < ip_set_max && res == 0; i++) {
2981 + if (ip_set_list[i] != NULL)
2982 + res = ip_set_list_set(i, data, &used, *len);
2985 + /* List an individual set */
2986 + res = ip_set_list_set(index, data, &used, *len);
2990 + else if (copylen != used) {
2996 + case IP_SET_OP_SAVE: {
2997 + struct ip_set_req_list *req_save
2998 + = (struct ip_set_req_list *) data;
3002 + if (*len < sizeof(struct ip_set_req_list)) {
3003 + ip_set_printk("short SAVE (want >=%zu, got %d)",
3004 + sizeof(struct ip_set_req_list), *len);
3008 + index = req_save->index;
3009 + if (index != IP_SET_INVALID_ID
3010 + && ip_set_find_byindex(index) != index) {
3015 + if (index == IP_SET_INVALID_ID) {
3016 + /* Save all sets */
3017 + for (i = 0; i < ip_set_max && res == 0; i++) {
3018 + if (ip_set_list[i] != NULL)
3019 + res = ip_set_save_set(i, data, &used, *len);
3022 + /* Save an individual set */
3023 + res = ip_set_save_set(index, data, &used, *len);
3026 + res = ip_set_save_bindings(index, data, &used, *len);
3030 + else if (copylen != used) {
3036 + case IP_SET_OP_RESTORE: {
3037 + struct ip_set_req_setnames *req_restore
3038 + = (struct ip_set_req_setnames *) data;
3041 + if (*len < sizeof(struct ip_set_req_setnames)
3042 + || *len != req_restore->size) {
3043 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3044 + req_restore->size, *len);
3048 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3049 + req_restore->size - sizeof(struct ip_set_req_setnames));
3050 + DP("ip_set_restore: %u", line);
3053 + req_restore->size = line;
3054 + copylen = sizeof(struct ip_set_req_setnames);
3062 + } /* end of switch(op) */
3065 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3066 + && ip_set_list[index]
3067 + ? ip_set_list[index]->name
3068 + : ":all:", copylen);
3069 + res = copy_to_user(user, data, copylen);
3072 + up(&ip_set_app_mutex);
3076 + DP("final result %d", res);
3080 +static struct nf_sockopt_ops so_set = {
3082 + .set_optmin = SO_IP_SET,
3083 + .set_optmax = SO_IP_SET + 1,
3084 + .set = &ip_set_sockfn_set,
3085 + .get_optmin = SO_IP_SET,
3086 + .get_optmax = SO_IP_SET + 1,
3087 + .get = &ip_set_sockfn_get,
3088 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
3089 + .owner = THIS_MODULE,
3093 +static int max_sets, hash_size;
3094 +module_param(max_sets, int, 0600);
3095 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3096 +module_param(hash_size, int, 0600);
3097 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3098 +MODULE_LICENSE("GPL");
3099 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3100 +MODULE_DESCRIPTION("module implementing core IP set support");
3102 +static int __init ip_set_init(void)
3107 + get_random_bytes(&ip_set_hash_random, 4);
3109 + ip_set_max = max_sets;
3110 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3111 + if (!ip_set_list) {
3112 + printk(KERN_ERR "Unable to create ip_set_list\n");
3115 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3117 + ip_set_bindings_hash_size = hash_size;
3118 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3119 + if (!ip_set_hash) {
3120 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3121 + vfree(ip_set_list);
3124 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3125 + INIT_LIST_HEAD(&ip_set_hash[i]);
3127 + INIT_LIST_HEAD(&set_type_list);
3129 + res = nf_register_sockopt(&so_set);
3131 + ip_set_printk("SO_SET registry failed: %d", res);
3132 + vfree(ip_set_list);
3133 + vfree(ip_set_hash);
3139 +static void __exit ip_set_fini(void)
3141 + /* There can't be any existing set or binding */
3142 + nf_unregister_sockopt(&so_set);
3143 + vfree(ip_set_list);
3144 + vfree(ip_set_hash);
3145 + DP("these are the famous last words");
3148 +EXPORT_SYMBOL(ip_set_register_set_type);
3149 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3151 +EXPORT_SYMBOL(ip_set_get_byname);
3152 +EXPORT_SYMBOL(ip_set_get_byindex);
3153 +EXPORT_SYMBOL(ip_set_put);
3155 +EXPORT_SYMBOL(ip_set_addip_kernel);
3156 +EXPORT_SYMBOL(ip_set_delip_kernel);
3157 +EXPORT_SYMBOL(ip_set_testip_kernel);
3159 +module_init(ip_set_init);
3160 +module_exit(ip_set_fini);
3162 +++ b/net/ipv4/netfilter/ip_set_iphash.c
3164 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3166 + * This program is free software; you can redistribute it and/or modify
3167 + * it under the terms of the GNU General Public License version 2 as
3168 + * published by the Free Software Foundation.
3171 +/* Kernel module implementing an ip hash set */
3173 +#include <linux/module.h>
3174 +#include <linux/ip.h>
3175 +#include <linux/skbuff.h>
3176 +#include <linux/version.h>
3177 +#include <linux/jhash.h>
3178 +#include <linux/netfilter_ipv4/ip_tables.h>
3179 +#include <linux/netfilter_ipv4/ip_set.h>
3180 +#include <linux/errno.h>
3181 +#include <asm/uaccess.h>
3182 +#include <asm/bitops.h>
3183 +#include <linux/spinlock.h>
3184 +#include <linux/vmalloc.h>
3185 +#include <linux/random.h>
3187 +#include <net/ip.h>
3189 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3190 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3192 +static int limit = MAX_RANGE;
3194 +static inline __u32
3195 +jhash_ip(const struct ip_set_iphash *map, uint16_t i, ip_set_ip_t ip)
3197 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
3200 +static inline __u32
3201 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3203 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3206 + ip_set_ip_t *elem;
3208 + *hash_ip = ip & map->netmask;
3209 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3210 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3212 + for (i = 0; i < map->probes; i++) {
3213 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
3214 + DP("hash key: %u", id);
3215 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3216 + if (*elem == *hash_ip)
3218 + /* No shortcut at testing - there can be deleted
3225 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3227 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
3231 +testip(struct ip_set *set, const void *data, size_t size,
3232 + ip_set_ip_t *hash_ip)
3234 + struct ip_set_req_iphash *req =
3235 + (struct ip_set_req_iphash *) data;
3237 + if (size != sizeof(struct ip_set_req_iphash)) {
3238 + ip_set_printk("data length wrong (want %zu, have %zu)",
3239 + sizeof(struct ip_set_req_iphash),
3243 + return __testip(set, req->ip, hash_ip);
3247 +testip_kernel(struct ip_set *set,
3248 + const struct sk_buff *skb,
3249 + ip_set_ip_t *hash_ip,
3250 + const u_int32_t *flags,
3251 + unsigned char index)
3253 + return __testip(set,
3254 + ntohl(flags[index] & IPSET_SRC
3255 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3256 + ? ip_hdr(skb)->saddr
3257 + : ip_hdr(skb)->daddr),
3259 + ? skb->nh.iph->saddr
3260 + : skb->nh.iph->daddr),
3266 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3270 + ip_set_ip_t *elem;
3272 + if (!ip || map->elements >= limit)
3275 + *hash_ip = ip & map->netmask;
3277 + for (i = 0; i < map->probes; i++) {
3278 + probe = jhash_ip(map, i, *hash_ip) % map->hashsize;
3279 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
3280 + if (*elem == *hash_ip)
3288 + /* Trigger rehashing */
3293 +addip(struct ip_set *set, const void *data, size_t size,
3294 + ip_set_ip_t *hash_ip)
3296 + struct ip_set_req_iphash *req =
3297 + (struct ip_set_req_iphash *) data;
3299 + if (size != sizeof(struct ip_set_req_iphash)) {
3300 + ip_set_printk("data length wrong (want %zu, have %zu)",
3301 + sizeof(struct ip_set_req_iphash),
3305 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3309 +addip_kernel(struct ip_set *set,
3310 + const struct sk_buff *skb,
3311 + ip_set_ip_t *hash_ip,
3312 + const u_int32_t *flags,
3313 + unsigned char index)
3315 + return __addip((struct ip_set_iphash *) set->data,
3316 + ntohl(flags[index] & IPSET_SRC
3317 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3318 + ? ip_hdr(skb)->saddr
3319 + : ip_hdr(skb)->daddr),
3321 + ? skb->nh.iph->saddr
3322 + : skb->nh.iph->daddr),
3327 +static int retry(struct ip_set *set)
3329 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3330 + ip_set_ip_t hash_ip, *elem;
3332 + u_int32_t i, hashsize = map->hashsize;
3334 + struct ip_set_iphash *tmp;
3336 + if (map->resize == 0)
3342 + /* Calculate new hash size */
3343 + hashsize += (hashsize * map->resize)/100;
3344 + if (hashsize == map->hashsize)
3347 + ip_set_printk("rehashing of set %s triggered: "
3348 + "hashsize grows from %u to %u",
3349 + set->name, map->hashsize, hashsize);
3351 + tmp = kmalloc(sizeof(struct ip_set_iphash)
3352 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
3354 + DP("out of memory for %d bytes",
3355 + sizeof(struct ip_set_iphash)
3356 + + map->probes * sizeof(uint32_t));
3359 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
3360 + if (!tmp->members) {
3361 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
3365 + tmp->hashsize = hashsize;
3366 + tmp->elements = 0;
3367 + tmp->probes = map->probes;
3368 + tmp->resize = map->resize;
3369 + tmp->netmask = map->netmask;
3370 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
3372 + write_lock_bh(&set->lock);
3373 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3374 + for (i = 0; i < map->hashsize && res == 0; i++) {
3375 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3377 + res = __addip(tmp, *elem, &hash_ip);
3380 + /* Failure, try again */
3381 + write_unlock_bh(&set->lock);
3382 + harray_free(tmp->members);
3387 + /* Success at resizing! */
3388 + members = map->members;
3390 + map->hashsize = tmp->hashsize;
3391 + map->members = tmp->members;
3392 + write_unlock_bh(&set->lock);
3394 + harray_free(members);
3401 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3403 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3404 + ip_set_ip_t id, *elem;
3409 + id = hash_id(set, ip, hash_ip);
3410 + if (id == UINT_MAX)
3413 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3421 +delip(struct ip_set *set, const void *data, size_t size,
3422 + ip_set_ip_t *hash_ip)
3424 + struct ip_set_req_iphash *req =
3425 + (struct ip_set_req_iphash *) data;
3427 + if (size != sizeof(struct ip_set_req_iphash)) {
3428 + ip_set_printk("data length wrong (want %zu, have %zu)",
3429 + sizeof(struct ip_set_req_iphash),
3433 + return __delip(set, req->ip, hash_ip);
3437 +delip_kernel(struct ip_set *set,
3438 + const struct sk_buff *skb,
3439 + ip_set_ip_t *hash_ip,
3440 + const u_int32_t *flags,
3441 + unsigned char index)
3443 + return __delip(set,
3444 + ntohl(flags[index] & IPSET_SRC
3445 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3446 + ? ip_hdr(skb)->saddr
3447 + : ip_hdr(skb)->daddr),
3449 + ? skb->nh.iph->saddr
3450 + : skb->nh.iph->daddr),
3455 +static int create(struct ip_set *set, const void *data, size_t size)
3457 + struct ip_set_req_iphash_create *req =
3458 + (struct ip_set_req_iphash_create *) data;
3459 + struct ip_set_iphash *map;
3462 + if (size != sizeof(struct ip_set_req_iphash_create)) {
3463 + ip_set_printk("data length wrong (want %zu, have %zu)",
3464 + sizeof(struct ip_set_req_iphash_create),
3469 + if (req->hashsize < 1) {
3470 + ip_set_printk("hashsize too small");
3474 + if (req->probes < 1) {
3475 + ip_set_printk("probes too small");
3479 + map = kmalloc(sizeof(struct ip_set_iphash)
3480 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
3482 + DP("out of memory for %d bytes",
3483 + sizeof(struct ip_set_iphash)
3484 + + req->probes * sizeof(uint32_t));
3487 + for (i = 0; i < req->probes; i++)
3488 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
3489 + map->elements = 0;
3490 + map->hashsize = req->hashsize;
3491 + map->probes = req->probes;
3492 + map->resize = req->resize;
3493 + map->netmask = req->netmask;
3494 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
3495 + if (!map->members) {
3496 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
3505 +static void destroy(struct ip_set *set)
3507 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3509 + harray_free(map->members);
3515 +static void flush(struct ip_set *set)
3517 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3518 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
3519 + map->elements = 0;
3522 +static void list_header(const struct ip_set *set, void *data)
3524 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3525 + struct ip_set_req_iphash_create *header =
3526 + (struct ip_set_req_iphash_create *) data;
3528 + header->hashsize = map->hashsize;
3529 + header->probes = map->probes;
3530 + header->resize = map->resize;
3531 + header->netmask = map->netmask;
3534 +static int list_members_size(const struct ip_set *set)
3536 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3538 + return (map->hashsize * sizeof(ip_set_ip_t));
3541 +static void list_members(const struct ip_set *set, void *data)
3543 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3544 + ip_set_ip_t i, *elem;
3546 + for (i = 0; i < map->hashsize; i++) {
3547 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3548 + ((ip_set_ip_t *)data)[i] = *elem;
3552 +static struct ip_set_type ip_set_iphash = {
3553 + .typename = SETTYPE_NAME,
3554 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
3555 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3556 + .create = &create,
3557 + .destroy = &destroy,
3559 + .reqsize = sizeof(struct ip_set_req_iphash),
3561 + .addip_kernel = &addip_kernel,
3564 + .delip_kernel = &delip_kernel,
3565 + .testip = &testip,
3566 + .testip_kernel = &testip_kernel,
3567 + .header_size = sizeof(struct ip_set_req_iphash_create),
3568 + .list_header = &list_header,
3569 + .list_members_size = &list_members_size,
3570 + .list_members = &list_members,
3571 + .me = THIS_MODULE,
3574 +MODULE_LICENSE("GPL");
3575 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3576 +MODULE_DESCRIPTION("iphash type of IP sets");
3577 +module_param(limit, int, 0600);
3578 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
3580 +static int __init ip_set_iphash_init(void)
3582 + return ip_set_register_set_type(&ip_set_iphash);
3585 +static void __exit ip_set_iphash_fini(void)
3587 + /* FIXME: possible race with ip_set_create() */
3588 + ip_set_unregister_set_type(&ip_set_iphash);
3591 +module_init(ip_set_iphash_init);
3592 +module_exit(ip_set_iphash_fini);
3594 +++ b/net/ipv4/netfilter/ip_set_ipmap.c
3596 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3597 + * Patrick Schaaf <bof@bof.de>
3598 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3600 + * This program is free software; you can redistribute it and/or modify
3601 + * it under the terms of the GNU General Public License version 2 as
3602 + * published by the Free Software Foundation.
3605 +/* Kernel module implementing an IP set type: the single bitmap type */
3607 +#include <linux/module.h>
3608 +#include <linux/ip.h>
3609 +#include <linux/skbuff.h>
3610 +#include <linux/version.h>
3611 +#include <linux/netfilter_ipv4/ip_tables.h>
3612 +#include <linux/netfilter_ipv4/ip_set.h>
3613 +#include <linux/errno.h>
3614 +#include <asm/uaccess.h>
3615 +#include <asm/bitops.h>
3616 +#include <linux/spinlock.h>
3618 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3620 +static inline ip_set_ip_t
3621 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3623 + return (ip - map->first_ip)/map->hosts;
3627 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3629 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3631 + if (ip < map->first_ip || ip > map->last_ip)
3634 + *hash_ip = ip & map->netmask;
3635 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3636 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3637 + return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3641 +testip(struct ip_set *set, const void *data, size_t size,
3642 + ip_set_ip_t *hash_ip)
3644 + struct ip_set_req_ipmap *req =
3645 + (struct ip_set_req_ipmap *) data;
3647 + if (size != sizeof(struct ip_set_req_ipmap)) {
3648 + ip_set_printk("data length wrong (want %zu, have %zu)",
3649 + sizeof(struct ip_set_req_ipmap),
3653 + return __testip(set, req->ip, hash_ip);
3657 +testip_kernel(struct ip_set *set,
3658 + const struct sk_buff *skb,
3659 + ip_set_ip_t *hash_ip,
3660 + const u_int32_t *flags,
3661 + unsigned char index)
3663 + int res = __testip(set,
3664 + ntohl(flags[index] & IPSET_SRC
3665 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3666 + ? ip_hdr(skb)->saddr
3667 + : ip_hdr(skb)->daddr),
3669 + ? skb->nh.iph->saddr
3670 + : skb->nh.iph->daddr),
3673 + return (res < 0 ? 0 : res);
3677 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3679 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3681 + if (ip < map->first_ip || ip > map->last_ip)
3684 + *hash_ip = ip & map->netmask;
3685 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3686 + if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3693 +addip(struct ip_set *set, const void *data, size_t size,
3694 + ip_set_ip_t *hash_ip)
3696 + struct ip_set_req_ipmap *req =
3697 + (struct ip_set_req_ipmap *) data;
3699 + if (size != sizeof(struct ip_set_req_ipmap)) {
3700 + ip_set_printk("data length wrong (want %zu, have %zu)",
3701 + sizeof(struct ip_set_req_ipmap),
3705 + DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3706 + return __addip(set, req->ip, hash_ip);
3710 +addip_kernel(struct ip_set *set,
3711 + const struct sk_buff *skb,
3712 + ip_set_ip_t *hash_ip,
3713 + const u_int32_t *flags,
3714 + unsigned char index)
3716 + return __addip(set,
3717 + ntohl(flags[index] & IPSET_SRC
3718 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3719 + ? ip_hdr(skb)->saddr
3720 + : ip_hdr(skb)->daddr),
3722 + ? skb->nh.iph->saddr
3723 + : skb->nh.iph->daddr),
3729 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3731 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3733 + if (ip < map->first_ip || ip > map->last_ip)
3736 + *hash_ip = ip & map->netmask;
3737 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3738 + if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3745 +delip(struct ip_set *set, const void *data, size_t size,
3746 + ip_set_ip_t *hash_ip)
3748 + struct ip_set_req_ipmap *req =
3749 + (struct ip_set_req_ipmap *) data;
3751 + if (size != sizeof(struct ip_set_req_ipmap)) {
3752 + ip_set_printk("data length wrong (want %zu, have %zu)",
3753 + sizeof(struct ip_set_req_ipmap),
3757 + return __delip(set, req->ip, hash_ip);
3761 +delip_kernel(struct ip_set *set,
3762 + const struct sk_buff *skb,
3763 + ip_set_ip_t *hash_ip,
3764 + const u_int32_t *flags,
3765 + unsigned char index)
3767 + return __delip(set,
3768 + ntohl(flags[index] & IPSET_SRC
3769 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3770 + ? ip_hdr(skb)->saddr
3771 + : ip_hdr(skb)->daddr),
3773 + ? skb->nh.iph->saddr
3774 + : skb->nh.iph->daddr),
3779 +static int create(struct ip_set *set, const void *data, size_t size)
3782 + struct ip_set_req_ipmap_create *req =
3783 + (struct ip_set_req_ipmap_create *) data;
3784 + struct ip_set_ipmap *map;
3786 + if (size != sizeof(struct ip_set_req_ipmap_create)) {
3787 + ip_set_printk("data length wrong (want %zu, have %zu)",
3788 + sizeof(struct ip_set_req_ipmap_create),
3793 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3794 + HIPQUAD(req->from), HIPQUAD(req->to));
3796 + if (req->from > req->to) {
3797 + DP("bad ip range");
3801 + map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3803 + DP("out of memory for %d bytes",
3804 + sizeof(struct ip_set_ipmap));
3807 + map->first_ip = req->from;
3808 + map->last_ip = req->to;
3809 + map->netmask = req->netmask;
3811 + if (req->netmask == 0xFFFFFFFF) {
3813 + map->sizeid = map->last_ip - map->first_ip + 1;
3815 + unsigned int mask_bits, netmask_bits;
3818 + map->first_ip &= map->netmask; /* Should we better bark? */
3820 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3821 + netmask_bits = mask_to_bits(map->netmask);
3823 + if ((!mask && (map->first_ip || map->last_ip != 0xFFFFFFFF))
3824 + || netmask_bits <= mask_bits)
3827 + DP("mask_bits %u, netmask_bits %u",
3828 + mask_bits, netmask_bits);
3829 + map->hosts = 2 << (32 - netmask_bits - 1);
3830 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3832 + if (map->sizeid > MAX_RANGE + 1) {
3833 + ip_set_printk("range too big (max %d addresses)",
3838 + DP("hosts %u, sizeid %u", map->hosts, map->sizeid);
3839 + newbytes = bitmap_bytes(0, map->sizeid - 1);
3840 + map->members = kmalloc(newbytes, GFP_KERNEL);
3841 + if (!map->members) {
3842 + DP("out of memory for %d bytes", newbytes);
3846 + memset(map->members, 0, newbytes);
3852 +static void destroy(struct ip_set *set)
3854 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3856 + kfree(map->members);
3862 +static void flush(struct ip_set *set)
3864 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3865 + memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3868 +static void list_header(const struct ip_set *set, void *data)
3870 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3871 + struct ip_set_req_ipmap_create *header =
3872 + (struct ip_set_req_ipmap_create *) data;
3874 + header->from = map->first_ip;
3875 + header->to = map->last_ip;
3876 + header->netmask = map->netmask;
3879 +static int list_members_size(const struct ip_set *set)
3881 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3883 + return bitmap_bytes(0, map->sizeid - 1);
3886 +static void list_members(const struct ip_set *set, void *data)
3888 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3889 + int bytes = bitmap_bytes(0, map->sizeid - 1);
3891 + memcpy(data, map->members, bytes);
3894 +static struct ip_set_type ip_set_ipmap = {
3895 + .typename = SETTYPE_NAME,
3896 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
3897 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3898 + .create = &create,
3899 + .destroy = &destroy,
3901 + .reqsize = sizeof(struct ip_set_req_ipmap),
3903 + .addip_kernel = &addip_kernel,
3905 + .delip_kernel = &delip_kernel,
3906 + .testip = &testip,
3907 + .testip_kernel = &testip_kernel,
3908 + .header_size = sizeof(struct ip_set_req_ipmap_create),
3909 + .list_header = &list_header,
3910 + .list_members_size = &list_members_size,
3911 + .list_members = &list_members,
3912 + .me = THIS_MODULE,
3915 +MODULE_LICENSE("GPL");
3916 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3917 +MODULE_DESCRIPTION("ipmap type of IP sets");
3919 +static int __init ip_set_ipmap_init(void)
3921 + return ip_set_register_set_type(&ip_set_ipmap);
3924 +static void __exit ip_set_ipmap_fini(void)
3926 + /* FIXME: possible race with ip_set_create() */
3927 + ip_set_unregister_set_type(&ip_set_ipmap);
3930 +module_init(ip_set_ipmap_init);
3931 +module_exit(ip_set_ipmap_fini);
3933 +++ b/net/ipv4/netfilter/ip_set_ipporthash.c
3935 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3937 + * This program is free software; you can redistribute it and/or modify
3938 + * it under the terms of the GNU General Public License version 2 as
3939 + * published by the Free Software Foundation.
3942 +/* Kernel module implementing an ip+port hash set */
3944 +#include <linux/module.h>
3945 +#include <linux/ip.h>
3946 +#include <linux/tcp.h>
3947 +#include <linux/udp.h>
3948 +#include <linux/skbuff.h>
3949 +#include <linux/version.h>
3950 +#include <linux/jhash.h>
3951 +#include <linux/netfilter_ipv4/ip_tables.h>
3952 +#include <linux/netfilter_ipv4/ip_set.h>
3953 +#include <linux/errno.h>
3954 +#include <asm/uaccess.h>
3955 +#include <asm/bitops.h>
3956 +#include <linux/spinlock.h>
3957 +#include <linux/vmalloc.h>
3958 +#include <linux/random.h>
3960 +#include <net/ip.h>
3962 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3963 +#include <linux/netfilter_ipv4/ip_set_ipporthash.h>
3965 +static int limit = MAX_RANGE;
3967 +/* We must handle non-linear skbs */
3968 +static inline ip_set_ip_t
3969 +get_port(const struct sk_buff *skb, u_int32_t flags)
3971 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3972 + struct iphdr *iph = ip_hdr(skb);
3974 + struct iphdr *iph = skb->nh.iph;
3976 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
3978 + switch (iph->protocol) {
3979 + case IPPROTO_TCP: {
3980 + struct tcphdr tcph;
3982 + /* See comments at tcp_match in ip_tables.c */
3984 + return INVALID_PORT;
3986 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3987 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
3989 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
3991 + /* No choice either */
3992 + return INVALID_PORT;
3994 + return ntohs(flags & IPSET_SRC ?
3995 + tcph.source : tcph.dest);
3997 + case IPPROTO_UDP: {
3998 + struct udphdr udph;
4001 + return INVALID_PORT;
4003 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4004 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
4006 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
4008 + /* No choice either */
4009 + return INVALID_PORT;
4011 + return ntohs(flags & IPSET_SRC ?
4012 + udph.source : udph.dest);
4015 + return INVALID_PORT;
4019 +static inline __u32
4020 +jhash_ip(const struct ip_set_ipporthash *map, uint16_t i, ip_set_ip_t ip)
4022 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
4025 +#define HASH_IP(map, ip, port) (port + ((ip - ((map)->first_ip)) << 16))
4027 +static inline __u32
4028 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4029 + ip_set_ip_t *hash_ip)
4031 + struct ip_set_ipporthash *map =
4032 + (struct ip_set_ipporthash *) set->data;
4035 + ip_set_ip_t *elem;
4037 + *hash_ip = HASH_IP(map, ip, port);
4038 + DP("set: %s, ipport:%u.%u.%u.%u:%u, %u.%u.%u.%u",
4039 + set->name, HIPQUAD(ip), port, HIPQUAD(*hash_ip));
4041 + for (i = 0; i < map->probes; i++) {
4042 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
4043 + DP("hash key: %u", id);
4044 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4045 + if (*elem == *hash_ip)
4047 + /* No shortcut at testing - there can be deleted
4054 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4055 + ip_set_ip_t *hash_ip)
4057 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4059 + if (ip < map->first_ip || ip > map->last_ip)
4062 + return (hash_id(set, ip, port, hash_ip) != UINT_MAX);
4066 +testip(struct ip_set *set, const void *data, size_t size,
4067 + ip_set_ip_t *hash_ip)
4069 + struct ip_set_req_ipporthash *req =
4070 + (struct ip_set_req_ipporthash *) data;
4072 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4073 + ip_set_printk("data length wrong (want %zu, have %zu)",
4074 + sizeof(struct ip_set_req_ipporthash),
4078 + return __testip(set, req->ip, req->port, hash_ip);
4082 +testip_kernel(struct ip_set *set,
4083 + const struct sk_buff *skb,
4084 + ip_set_ip_t *hash_ip,
4085 + const u_int32_t *flags,
4086 + unsigned char index)
4091 + if (flags[index+1] == 0)
4094 + port = get_port(skb, flags[index+1]);
4096 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4097 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4098 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4099 + NIPQUAD(ip_hdr(skb)->saddr),
4100 + NIPQUAD(ip_hdr(skb)->daddr));
4102 + NIPQUAD(skb->nh.iph->saddr),
4103 + NIPQUAD(skb->nh.iph->daddr));
4105 + DP("flag %s port %u",
4106 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4108 + if (port == INVALID_PORT)
4111 + res = __testip(set,
4112 + ntohl(flags[index] & IPSET_SRC
4113 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4114 + ? ip_hdr(skb)->saddr
4115 + : ip_hdr(skb)->daddr),
4117 + ? skb->nh.iph->saddr
4118 + : skb->nh.iph->daddr),
4122 + return (res < 0 ? 0 : res);
4127 +__add_haship(struct ip_set_ipporthash *map, ip_set_ip_t hash_ip)
4131 + ip_set_ip_t *elem;
4133 + for (i = 0; i < map->probes; i++) {
4134 + probe = jhash_ip(map, i, hash_ip) % map->hashsize;
4135 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
4136 + if (*elem == hash_ip)
4144 + /* Trigger rehashing */
4149 +__addip(struct ip_set_ipporthash *map, ip_set_ip_t ip, ip_set_ip_t port,
4150 + ip_set_ip_t *hash_ip)
4152 + if (map->elements > limit)
4154 + if (ip < map->first_ip || ip > map->last_ip)
4157 + *hash_ip = HASH_IP(map, ip, port);
4159 + return __add_haship(map, *hash_ip);
4163 +addip(struct ip_set *set, const void *data, size_t size,
4164 + ip_set_ip_t *hash_ip)
4166 + struct ip_set_req_ipporthash *req =
4167 + (struct ip_set_req_ipporthash *) data;
4169 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4170 + ip_set_printk("data length wrong (want %zu, have %zu)",
4171 + sizeof(struct ip_set_req_ipporthash),
4175 + return __addip((struct ip_set_ipporthash *) set->data,
4176 + req->ip, req->port, hash_ip);
4180 +addip_kernel(struct ip_set *set,
4181 + const struct sk_buff *skb,
4182 + ip_set_ip_t *hash_ip,
4183 + const u_int32_t *flags,
4184 + unsigned char index)
4188 + if (flags[index+1] == 0)
4191 + port = get_port(skb, flags[index+1]);
4193 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4194 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4195 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4196 + NIPQUAD(ip_hdr(skb)->saddr),
4197 + NIPQUAD(ip_hdr(skb)->daddr));
4199 + NIPQUAD(skb->nh.iph->saddr),
4200 + NIPQUAD(skb->nh.iph->daddr));
4202 + DP("flag %s port %u",
4203 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4205 + if (port == INVALID_PORT)
4208 + return __addip((struct ip_set_ipporthash *) set->data,
4209 + ntohl(flags[index] & IPSET_SRC
4210 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4211 + ? ip_hdr(skb)->saddr
4212 + : ip_hdr(skb)->daddr),
4214 + ? skb->nh.iph->saddr
4215 + : skb->nh.iph->daddr),
4221 +static int retry(struct ip_set *set)
4223 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4224 + ip_set_ip_t *elem;
4226 + u_int32_t i, hashsize = map->hashsize;
4228 + struct ip_set_ipporthash *tmp;
4230 + if (map->resize == 0)
4236 + /* Calculate new hash size */
4237 + hashsize += (hashsize * map->resize)/100;
4238 + if (hashsize == map->hashsize)
4241 + ip_set_printk("rehashing of set %s triggered: "
4242 + "hashsize grows from %u to %u",
4243 + set->name, map->hashsize, hashsize);
4245 + tmp = kmalloc(sizeof(struct ip_set_ipporthash)
4246 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
4248 + DP("out of memory for %d bytes",
4249 + sizeof(struct ip_set_ipporthash)
4250 + + map->probes * sizeof(uint32_t));
4253 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
4254 + if (!tmp->members) {
4255 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
4259 + tmp->hashsize = hashsize;
4260 + tmp->elements = 0;
4261 + tmp->probes = map->probes;
4262 + tmp->resize = map->resize;
4263 + tmp->first_ip = map->first_ip;
4264 + tmp->last_ip = map->last_ip;
4265 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
4267 + write_lock_bh(&set->lock);
4268 + map = (struct ip_set_ipporthash *) set->data; /* Play safe */
4269 + for (i = 0; i < map->hashsize && res == 0; i++) {
4270 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
4272 + res = __add_haship(tmp, *elem);
4275 + /* Failure, try again */
4276 + write_unlock_bh(&set->lock);
4277 + harray_free(tmp->members);
4282 + /* Success at resizing! */
4283 + members = map->members;
4285 + map->hashsize = tmp->hashsize;
4286 + map->members = tmp->members;
4287 + write_unlock_bh(&set->lock);
4289 + harray_free(members);
4296 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4297 + ip_set_ip_t *hash_ip)
4299 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4301 + ip_set_ip_t *elem;
4303 + if (ip < map->first_ip || ip > map->last_ip)
4306 + id = hash_id(set, ip, port, hash_ip);
4308 + if (id == UINT_MAX)
4311 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4319 +delip(struct ip_set *set, const void *data, size_t size,
4320 + ip_set_ip_t *hash_ip)
4322 + struct ip_set_req_ipporthash *req =
4323 + (struct ip_set_req_ipporthash *) data;
4325 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4326 + ip_set_printk("data length wrong (want %zu, have %zu)",
4327 + sizeof(struct ip_set_req_ipporthash),
4331 + return __delip(set, req->ip, req->port, hash_ip);
4335 +delip_kernel(struct ip_set *set,
4336 + const struct sk_buff *skb,
4337 + ip_set_ip_t *hash_ip,
4338 + const u_int32_t *flags,
4339 + unsigned char index)
4343 + if (flags[index+1] == 0)
4346 + port = get_port(skb, flags[index+1]);
4348 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4349 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4350 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4351 + NIPQUAD(ip_hdr(skb)->saddr),
4352 + NIPQUAD(ip_hdr(skb)->daddr));
4354 + NIPQUAD(skb->nh.iph->saddr),
4355 + NIPQUAD(skb->nh.iph->daddr));
4357 + DP("flag %s port %u",
4358 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4360 + if (port == INVALID_PORT)
4363 + return __delip(set,
4364 + ntohl(flags[index] & IPSET_SRC
4365 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4366 + ? ip_hdr(skb)->saddr
4367 + : ip_hdr(skb)->daddr),
4369 + ? skb->nh.iph->saddr
4370 + : skb->nh.iph->daddr),
4376 +static int create(struct ip_set *set, const void *data, size_t size)
4378 + struct ip_set_req_ipporthash_create *req =
4379 + (struct ip_set_req_ipporthash_create *) data;
4380 + struct ip_set_ipporthash *map;
4383 + if (size != sizeof(struct ip_set_req_ipporthash_create)) {
4384 + ip_set_printk("data length wrong (want %zu, have %zu)",
4385 + sizeof(struct ip_set_req_ipporthash_create),
4390 + if (req->hashsize < 1) {
4391 + ip_set_printk("hashsize too small");
4395 + if (req->probes < 1) {
4396 + ip_set_printk("probes too small");
4400 + map = kmalloc(sizeof(struct ip_set_ipporthash)
4401 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
4403 + DP("out of memory for %d bytes",
4404 + sizeof(struct ip_set_ipporthash)
4405 + + req->probes * sizeof(uint32_t));
4408 + for (i = 0; i < req->probes; i++)
4409 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
4410 + map->elements = 0;
4411 + map->hashsize = req->hashsize;
4412 + map->probes = req->probes;
4413 + map->resize = req->resize;
4414 + map->first_ip = req->from;
4415 + map->last_ip = req->to;
4416 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
4417 + if (!map->members) {
4418 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
4427 +static void destroy(struct ip_set *set)
4429 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4431 + harray_free(map->members);
4437 +static void flush(struct ip_set *set)
4439 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4440 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
4441 + map->elements = 0;
4444 +static void list_header(const struct ip_set *set, void *data)
4446 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4447 + struct ip_set_req_ipporthash_create *header =
4448 + (struct ip_set_req_ipporthash_create *) data;
4450 + header->hashsize = map->hashsize;
4451 + header->probes = map->probes;
4452 + header->resize = map->resize;
4453 + header->from = map->first_ip;
4454 + header->to = map->last_ip;
4457 +static int list_members_size(const struct ip_set *set)
4459 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4461 + return (map->hashsize * sizeof(ip_set_ip_t));
4464 +static void list_members(const struct ip_set *set, void *data)
4466 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4467 + ip_set_ip_t i, *elem;
4469 + for (i = 0; i < map->hashsize; i++) {
4470 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
4471 + ((ip_set_ip_t *)data)[i] = *elem;
4475 +static struct ip_set_type ip_set_ipporthash = {
4476 + .typename = SETTYPE_NAME,
4477 + .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_DATA_DOUBLE,
4478 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4479 + .create = &create,
4480 + .destroy = &destroy,
4482 + .reqsize = sizeof(struct ip_set_req_ipporthash),
4484 + .addip_kernel = &addip_kernel,
4487 + .delip_kernel = &delip_kernel,
4488 + .testip = &testip,
4489 + .testip_kernel = &testip_kernel,
4490 + .header_size = sizeof(struct ip_set_req_ipporthash_create),
4491 + .list_header = &list_header,
4492 + .list_members_size = &list_members_size,
4493 + .list_members = &list_members,
4494 + .me = THIS_MODULE,
4497 +MODULE_LICENSE("GPL");
4498 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4499 +MODULE_DESCRIPTION("ipporthash type of IP sets");
4500 +module_param(limit, int, 0600);
4501 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
4503 +static int __init ip_set_ipporthash_init(void)
4505 + return ip_set_register_set_type(&ip_set_ipporthash);
4508 +static void __exit ip_set_ipporthash_fini(void)
4510 + /* FIXME: possible race with ip_set_create() */
4511 + ip_set_unregister_set_type(&ip_set_ipporthash);
4514 +module_init(ip_set_ipporthash_init);
4515 +module_exit(ip_set_ipporthash_fini);
4517 +++ b/net/ipv4/netfilter/ip_set_iptree.c
4519 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4521 + * This program is free software; you can redistribute it and/or modify
4522 + * it under the terms of the GNU General Public License version 2 as
4523 + * published by the Free Software Foundation.
4526 +/* Kernel module implementing an IP set type: the iptree type */
4528 +#include <linux/version.h>
4529 +#include <linux/module.h>
4530 +#include <linux/ip.h>
4531 +#include <linux/skbuff.h>
4532 +#include <linux/slab.h>
4533 +#include <linux/delay.h>
4534 +#include <linux/netfilter_ipv4/ip_tables.h>
4535 +#include <linux/netfilter_ipv4/ip_set.h>
4536 +#include <linux/errno.h>
4537 +#include <asm/uaccess.h>
4538 +#include <asm/bitops.h>
4539 +#include <linux/spinlock.h>
4541 +/* Backward compatibility */
4546 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
4548 +static int limit = MAX_RANGE;
4550 +/* Garbage collection interval in seconds: */
4551 +#define IPTREE_GC_TIME 5*60
4552 +/* Sleep so many milliseconds before trying again
4553 + * to delete the gc timer at destroying/flushing a set */
4554 +#define IPTREE_DESTROY_SLEEP 100
4556 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
4557 +static struct kmem_cache *branch_cachep;
4558 +static struct kmem_cache *leaf_cachep;
4560 +static kmem_cache_t *branch_cachep;
4561 +static kmem_cache_t *leaf_cachep;
4564 +#if defined(__LITTLE_ENDIAN)
4565 +#define ABCD(a,b,c,d,addrp) do { \
4566 + a = ((unsigned char *)addrp)[3]; \
4567 + b = ((unsigned char *)addrp)[2]; \
4568 + c = ((unsigned char *)addrp)[1]; \
4569 + d = ((unsigned char *)addrp)[0]; \
4571 +#elif defined(__BIG_ENDIAN)
4572 +#define ABCD(a,b,c,d,addrp) do { \
4573 + a = ((unsigned char *)addrp)[0]; \
4574 + b = ((unsigned char *)addrp)[1]; \
4575 + c = ((unsigned char *)addrp)[2]; \
4576 + d = ((unsigned char *)addrp)[3]; \
4579 +#error "Please fix asm/byteorder.h"
4580 +#endif /* __LITTLE_ENDIAN */
4582 +#define TESTIP_WALK(map, elem, branch) do { \
4583 + if ((map)->tree[elem]) { \
4584 + branch = (map)->tree[elem]; \
4590 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4592 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4593 + struct ip_set_iptreeb *btree;
4594 + struct ip_set_iptreec *ctree;
4595 + struct ip_set_iptreed *dtree;
4596 + unsigned char a,b,c,d;
4602 + ABCD(a, b, c, d, hash_ip);
4603 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
4604 + TESTIP_WALK(map, a, btree);
4605 + TESTIP_WALK(btree, b, ctree);
4606 + TESTIP_WALK(ctree, c, dtree);
4607 + DP("%lu %lu", dtree->expires[d], jiffies);
4608 + return dtree->expires[d]
4610 + || time_after(dtree->expires[d], jiffies));
4614 +testip(struct ip_set *set, const void *data, size_t size,
4615 + ip_set_ip_t *hash_ip)
4617 + struct ip_set_req_iptree *req =
4618 + (struct ip_set_req_iptree *) data;
4620 + if (size != sizeof(struct ip_set_req_iptree)) {
4621 + ip_set_printk("data length wrong (want %zu, have %zu)",
4622 + sizeof(struct ip_set_req_iptree),
4626 + return __testip(set, req->ip, hash_ip);
4630 +testip_kernel(struct ip_set *set,
4631 + const struct sk_buff *skb,
4632 + ip_set_ip_t *hash_ip,
4633 + const u_int32_t *flags,
4634 + unsigned char index)
4638 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4639 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4640 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4641 + NIPQUAD(ip_hdr(skb)->saddr),
4642 + NIPQUAD(ip_hdr(skb)->daddr));
4644 + NIPQUAD(skb->nh.iph->saddr),
4645 + NIPQUAD(skb->nh.iph->daddr));
4648 + res = __testip(set,
4649 + ntohl(flags[index] & IPSET_SRC
4650 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4651 + ? ip_hdr(skb)->saddr
4652 + : ip_hdr(skb)->daddr),
4654 + ? skb->nh.iph->saddr
4655 + : skb->nh.iph->daddr),
4658 + return (res < 0 ? 0 : res);
4661 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
4662 + if ((map)->tree[elem]) { \
4663 + DP("found %u", elem); \
4664 + branch = (map)->tree[elem]; \
4666 + branch = (type *) \
4667 + kmem_cache_alloc(cachep, GFP_ATOMIC); \
4668 + if (branch == NULL) \
4670 + memset(branch, 0, sizeof(*branch)); \
4671 + (map)->tree[elem] = branch; \
4672 + DP("alloc %u", elem); \
4677 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
4678 + ip_set_ip_t *hash_ip)
4680 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4681 + struct ip_set_iptreeb *btree;
4682 + struct ip_set_iptreec *ctree;
4683 + struct ip_set_iptreed *dtree;
4684 + unsigned char a,b,c,d;
4687 + if (!ip || map->elements >= limit)
4688 + /* We could call the garbage collector
4689 + * but it's probably overkill */
4693 + ABCD(a, b, c, d, hash_ip);
4694 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
4695 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
4696 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
4697 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
4698 + if (dtree->expires[d]
4699 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4701 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
4702 + /* Lottery: I won! */
4703 + if (dtree->expires[d] == 0)
4704 + dtree->expires[d] = 1;
4705 + DP("%u %lu", d, dtree->expires[d]);
4712 +addip(struct ip_set *set, const void *data, size_t size,
4713 + ip_set_ip_t *hash_ip)
4715 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4716 + struct ip_set_req_iptree *req =
4717 + (struct ip_set_req_iptree *) data;
4719 + if (size != sizeof(struct ip_set_req_iptree)) {
4720 + ip_set_printk("data length wrong (want %zu, have %zu)",
4721 + sizeof(struct ip_set_req_iptree),
4725 + DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
4726 + return __addip(set, req->ip,
4727 + req->timeout ? req->timeout : map->timeout,
4732 +addip_kernel(struct ip_set *set,
4733 + const struct sk_buff *skb,
4734 + ip_set_ip_t *hash_ip,
4735 + const u_int32_t *flags,
4736 + unsigned char index)
4738 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4740 + return __addip(set,
4741 + ntohl(flags[index] & IPSET_SRC
4742 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4743 + ? ip_hdr(skb)->saddr
4744 + : ip_hdr(skb)->daddr),
4746 + ? skb->nh.iph->saddr
4747 + : skb->nh.iph->daddr),
4753 +#define DELIP_WALK(map, elem, branch) do { \
4754 + if ((map)->tree[elem]) { \
4755 + branch = (map)->tree[elem]; \
4761 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4763 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4764 + struct ip_set_iptreeb *btree;
4765 + struct ip_set_iptreec *ctree;
4766 + struct ip_set_iptreed *dtree;
4767 + unsigned char a,b,c,d;
4773 + ABCD(a, b, c, d, hash_ip);
4774 + DELIP_WALK(map, a, btree);
4775 + DELIP_WALK(btree, b, ctree);
4776 + DELIP_WALK(ctree, c, dtree);
4778 + if (dtree->expires[d]) {
4779 + dtree->expires[d] = 0;
4787 +delip(struct ip_set *set, const void *data, size_t size,
4788 + ip_set_ip_t *hash_ip)
4790 + struct ip_set_req_iptree *req =
4791 + (struct ip_set_req_iptree *) data;
4793 + if (size != sizeof(struct ip_set_req_iptree)) {
4794 + ip_set_printk("data length wrong (want %zu, have %zu)",
4795 + sizeof(struct ip_set_req_iptree),
4799 + return __delip(set, req->ip, hash_ip);
4803 +delip_kernel(struct ip_set *set,
4804 + const struct sk_buff *skb,
4805 + ip_set_ip_t *hash_ip,
4806 + const u_int32_t *flags,
4807 + unsigned char index)
4809 + return __delip(set,
4810 + ntohl(flags[index] & IPSET_SRC
4811 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4812 + ? ip_hdr(skb)->saddr
4813 + : ip_hdr(skb)->daddr),
4815 + ? skb->nh.iph->saddr
4816 + : skb->nh.iph->daddr),
4821 +#define LOOP_WALK_BEGIN(map, i, branch) \
4822 + for (i = 0; i < 256; i++) { \
4823 + if (!(map)->tree[i]) \
4825 + branch = (map)->tree[i]
4827 +#define LOOP_WALK_END }
4829 +static void ip_tree_gc(unsigned long ul_set)
4831 + struct ip_set *set = (void *) ul_set;
4832 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4833 + struct ip_set_iptreeb *btree;
4834 + struct ip_set_iptreec *ctree;
4835 + struct ip_set_iptreed *dtree;
4836 + unsigned int a,b,c,d;
4837 + unsigned char i,j,k;
4840 + DP("gc: %s", set->name);
4841 + write_lock_bh(&set->lock);
4842 + LOOP_WALK_BEGIN(map, a, btree);
4843 + LOOP_WALK_BEGIN(btree, b, ctree);
4844 + LOOP_WALK_BEGIN(ctree, c, dtree);
4845 + for (d = 0; d < 256; d++) {
4846 + if (dtree->expires[d]) {
4847 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4849 + dtree->expires[d], jiffies);
4851 + && time_before(dtree->expires[d], jiffies)) {
4852 + dtree->expires[d] = 0;
4859 + DP("gc: %s: leaf %u %u %u empty",
4860 + set->name, a, b, c);
4861 + kmem_cache_free(leaf_cachep, dtree);
4862 + ctree->tree[c] = NULL;
4864 + DP("gc: %s: leaf %u %u %u not empty",
4865 + set->name, a, b, c);
4871 + DP("gc: %s: branch %u %u empty",
4873 + kmem_cache_free(branch_cachep, ctree);
4874 + btree->tree[b] = NULL;
4876 + DP("gc: %s: branch %u %u not empty",
4883 + DP("gc: %s: branch %u empty",
4885 + kmem_cache_free(branch_cachep, btree);
4886 + map->tree[a] = NULL;
4888 + DP("gc: %s: branch %u not empty",
4893 + write_unlock_bh(&set->lock);
4895 + map->gc.expires = jiffies + map->gc_interval * HZ;
4896 + add_timer(&map->gc);
4899 +static inline void init_gc_timer(struct ip_set *set)
4901 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4903 + /* Even if there is no timeout for the entries,
4904 + * we still have to call gc because delete
4905 + * do not clean up empty branches */
4906 + map->gc_interval = IPTREE_GC_TIME;
4907 + init_timer(&map->gc);
4908 + map->gc.data = (unsigned long) set;
4909 + map->gc.function = ip_tree_gc;
4910 + map->gc.expires = jiffies + map->gc_interval * HZ;
4911 + add_timer(&map->gc);
4914 +static int create(struct ip_set *set, const void *data, size_t size)
4916 + struct ip_set_req_iptree_create *req =
4917 + (struct ip_set_req_iptree_create *) data;
4918 + struct ip_set_iptree *map;
4920 + if (size != sizeof(struct ip_set_req_iptree_create)) {
4921 + ip_set_printk("data length wrong (want %zu, have %zu)",
4922 + sizeof(struct ip_set_req_iptree_create),
4927 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4929 + DP("out of memory for %d bytes",
4930 + sizeof(struct ip_set_iptree));
4933 + memset(map, 0, sizeof(*map));
4934 + map->timeout = req->timeout;
4935 + map->elements = 0;
4938 + init_gc_timer(set);
4943 +static void __flush(struct ip_set_iptree *map)
4945 + struct ip_set_iptreeb *btree;
4946 + struct ip_set_iptreec *ctree;
4947 + struct ip_set_iptreed *dtree;
4948 + unsigned int a,b,c;
4950 + LOOP_WALK_BEGIN(map, a, btree);
4951 + LOOP_WALK_BEGIN(btree, b, ctree);
4952 + LOOP_WALK_BEGIN(ctree, c, dtree);
4953 + kmem_cache_free(leaf_cachep, dtree);
4955 + kmem_cache_free(branch_cachep, ctree);
4957 + kmem_cache_free(branch_cachep, btree);
4959 + map->elements = 0;
4962 +static void destroy(struct ip_set *set)
4964 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4966 + /* gc might be running */
4967 + while (!del_timer(&map->gc))
4968 + msleep(IPTREE_DESTROY_SLEEP);
4974 +static void flush(struct ip_set *set)
4976 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4977 + unsigned int timeout = map->timeout;
4979 + /* gc might be running */
4980 + while (!del_timer(&map->gc))
4981 + msleep(IPTREE_DESTROY_SLEEP);
4983 + memset(map, 0, sizeof(*map));
4984 + map->timeout = timeout;
4986 + init_gc_timer(set);
4989 +static void list_header(const struct ip_set *set, void *data)
4991 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4992 + struct ip_set_req_iptree_create *header =
4993 + (struct ip_set_req_iptree_create *) data;
4995 + header->timeout = map->timeout;
4998 +static int list_members_size(const struct ip_set *set)
5000 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5001 + struct ip_set_iptreeb *btree;
5002 + struct ip_set_iptreec *ctree;
5003 + struct ip_set_iptreed *dtree;
5004 + unsigned int a,b,c,d;
5005 + unsigned int count = 0;
5007 + LOOP_WALK_BEGIN(map, a, btree);
5008 + LOOP_WALK_BEGIN(btree, b, ctree);
5009 + LOOP_WALK_BEGIN(ctree, c, dtree);
5010 + for (d = 0; d < 256; d++) {
5011 + if (dtree->expires[d]
5012 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
5019 + DP("members %u", count);
5020 + return (count * sizeof(struct ip_set_req_iptree));
5023 +static void list_members(const struct ip_set *set, void *data)
5025 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5026 + struct ip_set_iptreeb *btree;
5027 + struct ip_set_iptreec *ctree;
5028 + struct ip_set_iptreed *dtree;
5029 + unsigned int a,b,c,d;
5030 + size_t offset = 0;
5031 + struct ip_set_req_iptree *entry;
5033 + LOOP_WALK_BEGIN(map, a, btree);
5034 + LOOP_WALK_BEGIN(btree, b, ctree);
5035 + LOOP_WALK_BEGIN(ctree, c, dtree);
5036 + for (d = 0; d < 256; d++) {
5037 + if (dtree->expires[d]
5038 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
5039 + entry = (struct ip_set_req_iptree *)(data + offset);
5040 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
5041 + entry->timeout = !map->timeout ? 0
5042 + : (dtree->expires[d] - jiffies)/HZ;
5043 + offset += sizeof(struct ip_set_req_iptree);
5051 +static struct ip_set_type ip_set_iptree = {
5052 + .typename = SETTYPE_NAME,
5053 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
5054 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5055 + .create = &create,
5056 + .destroy = &destroy,
5058 + .reqsize = sizeof(struct ip_set_req_iptree),
5060 + .addip_kernel = &addip_kernel,
5062 + .delip_kernel = &delip_kernel,
5063 + .testip = &testip,
5064 + .testip_kernel = &testip_kernel,
5065 + .header_size = sizeof(struct ip_set_req_iptree_create),
5066 + .list_header = &list_header,
5067 + .list_members_size = &list_members_size,
5068 + .list_members = &list_members,
5069 + .me = THIS_MODULE,
5072 +MODULE_LICENSE("GPL");
5073 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5074 +MODULE_DESCRIPTION("iptree type of IP sets");
5075 +module_param(limit, int, 0600);
5076 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
5078 +static int __init ip_set_iptree_init(void)
5082 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5083 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
5084 + sizeof(struct ip_set_iptreeb),
5087 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
5088 + sizeof(struct ip_set_iptreeb),
5089 + 0, 0, NULL, NULL);
5091 + if (!branch_cachep) {
5092 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
5096 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5097 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
5098 + sizeof(struct ip_set_iptreed),
5101 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
5102 + sizeof(struct ip_set_iptreed),
5103 + 0, 0, NULL, NULL);
5105 + if (!leaf_cachep) {
5106 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
5110 + ret = ip_set_register_set_type(&ip_set_iptree);
5114 + kmem_cache_destroy(leaf_cachep);
5116 + kmem_cache_destroy(branch_cachep);
5121 +static void __exit ip_set_iptree_fini(void)
5123 + /* FIXME: possible race with ip_set_create() */
5124 + ip_set_unregister_set_type(&ip_set_iptree);
5125 + kmem_cache_destroy(leaf_cachep);
5126 + kmem_cache_destroy(branch_cachep);
5129 +module_init(ip_set_iptree_init);
5130 +module_exit(ip_set_iptree_fini);
5132 +++ b/net/ipv4/netfilter/ip_set_iptreemap.c
5134 +/* Copyright (C) 2007 Sven Wegener <sven.wegener@stealer.net>
5136 + * This program is free software; you can redistribute it and/or modify it
5137 + * under the terms of the GNU General Public License version 2 as published by
5138 + * the Free Software Foundation.
5141 +/* This modules implements the iptreemap ipset type. It uses bitmaps to
5142 + * represent every single IPv4 address as a single bit. The bitmaps are managed
5143 + * in a tree structure, where the first three octets of an addresses are used
5144 + * as an index to find the bitmap and the last octet is used as the bit number.
5147 +#include <linux/version.h>
5148 +#include <linux/module.h>
5149 +#include <linux/ip.h>
5150 +#include <linux/skbuff.h>
5151 +#include <linux/slab.h>
5152 +#include <linux/delay.h>
5153 +#include <linux/netfilter_ipv4/ip_tables.h>
5154 +#include <linux/netfilter_ipv4/ip_set.h>
5155 +#include <linux/errno.h>
5156 +#include <asm/uaccess.h>
5157 +#include <asm/bitops.h>
5158 +#include <linux/spinlock.h>
5160 +#include <linux/netfilter_ipv4/ip_set_iptreemap.h>
5162 +#define IPTREEMAP_DEFAULT_GC_TIME (5 * 60)
5163 +#define IPTREEMAP_DESTROY_SLEEP (100)
5165 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
5166 +static struct kmem_cache *cachep_b;
5167 +static struct kmem_cache *cachep_c;
5168 +static struct kmem_cache *cachep_d;
5170 +static kmem_cache_t *cachep_b;
5171 +static kmem_cache_t *cachep_c;
5172 +static kmem_cache_t *cachep_d;
5175 +static struct ip_set_iptreemap_d *fullbitmap_d;
5176 +static struct ip_set_iptreemap_c *fullbitmap_c;
5177 +static struct ip_set_iptreemap_b *fullbitmap_b;
5179 +#if defined(__LITTLE_ENDIAN)
5180 +#define ABCD(a, b, c, d, addr) \
5182 + a = ((unsigned char *)addr)[3]; \
5183 + b = ((unsigned char *)addr)[2]; \
5184 + c = ((unsigned char *)addr)[1]; \
5185 + d = ((unsigned char *)addr)[0]; \
5187 +#elif defined(__BIG_ENDIAN)
5188 +#define ABCD(a,b,c,d,addrp) do { \
5189 + a = ((unsigned char *)addrp)[0]; \
5190 + b = ((unsigned char *)addrp)[1]; \
5191 + c = ((unsigned char *)addrp)[2]; \
5192 + d = ((unsigned char *)addrp)[3]; \
5195 +#error "Please fix asm/byteorder.h"
5196 +#endif /* __LITTLE_ENDIAN */
5198 +#define TESTIP_WALK(map, elem, branch, full) \
5200 + branch = (map)->tree[elem]; \
5203 + else if (branch == full) \
5207 +#define ADDIP_WALK(map, elem, branch, type, cachep, full) \
5209 + branch = (map)->tree[elem]; \
5211 + branch = (type *) kmem_cache_alloc(cachep, GFP_ATOMIC); \
5214 + memset(branch, 0, sizeof(*branch)); \
5215 + (map)->tree[elem] = branch; \
5216 + } else if (branch == full) { \
5221 +#define ADDIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free) \
5222 + for (a = a1; a <= a2; a++) { \
5223 + branch = (map)->tree[a]; \
5224 + if (branch != full) { \
5225 + if ((a > a1 && a < a2) || (hint)) { \
5228 + (map)->tree[a] = full; \
5230 + } else if (!branch) { \
5231 + branch = kmem_cache_alloc(cachep, GFP_ATOMIC); \
5234 + memset(branch, 0, sizeof(*branch)); \
5235 + (map)->tree[a] = branch; \
5238 +#define ADDIP_RANGE_LOOP_END() \
5242 +#define DELIP_WALK(map, elem, branch, cachep, full, flags) \
5244 + branch = (map)->tree[elem]; \
5247 + } else if (branch == full) { \
5248 + branch = kmem_cache_alloc(cachep, flags); \
5251 + memcpy(branch, full, sizeof(*full)); \
5252 + (map)->tree[elem] = branch; \
5256 +#define DELIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free, flags) \
5257 + for (a = a1; a <= a2; a++) { \
5258 + branch = (map)->tree[a]; \
5260 + if ((a > a1 && a < a2) || (hint)) { \
5261 + if (branch != full) \
5263 + (map)->tree[a] = NULL; \
5265 + } else if (branch == full) { \
5266 + branch = kmem_cache_alloc(cachep, flags); \
5269 + memcpy(branch, full, sizeof(*branch)); \
5270 + (map)->tree[a] = branch; \
5273 +#define DELIP_RANGE_LOOP_END() \
5277 +#define LOOP_WALK_BEGIN(map, i, branch) \
5278 + for (i = 0; i < 256; i++) { \
5279 + branch = (map)->tree[i]; \
5280 + if (likely(!branch)) \
5283 +#define LOOP_WALK_END() \
5286 +#define LOOP_WALK_BEGIN_GC(map, i, branch, full, cachep, count) \
5288 + for (i = 0; i < 256; i++) { \
5289 + branch = (map)->tree[i]; \
5290 + if (likely(!branch)) \
5293 + if (branch == full) { \
5298 +#define LOOP_WALK_END_GC(map, i, branch, full, cachep, count) \
5299 + if (-256 == count) { \
5300 + kmem_cache_free(cachep, branch); \
5301 + (map)->tree[i] = NULL; \
5302 + } else if (256 == count) { \
5303 + kmem_cache_free(cachep, branch); \
5304 + (map)->tree[i] = full; \
5308 +#define LOOP_WALK_BEGIN_COUNT(map, i, branch, inrange, count) \
5309 + for (i = 0; i < 256; i++) { \
5310 + if (!(map)->tree[i]) { \
5317 + branch = (map)->tree[i];
5319 +#define LOOP_WALK_END_COUNT() \
5322 +#define MIN(a, b) (a < b ? a : b)
5323 +#define MAX(a, b) (a > b ? a : b)
5325 +#define GETVALUE1(a, a1, b1, r) \
5326 + (a == a1 ? b1 : r)
5328 +#define GETVALUE2(a, b, a1, b1, c1, r) \
5329 + (a == a1 && b == b1 ? c1 : r)
5331 +#define GETVALUE3(a, b, c, a1, b1, c1, d1, r) \
5332 + (a == a1 && b == b1 && c == c1 ? d1 : r)
5334 +#define CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2) \
5336 + GETVALUE1(a, a1, b1, 0) == 0 \
5337 + && GETVALUE1(a, a2, b2, 255) == 255 \
5344 +#define CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2) \
5346 + GETVALUE2(a, b, a1, b1, c1, 0) == 0 \
5347 + && GETVALUE2(a, b, a2, b2, c2, 255) == 255 \
5352 +#define CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2) \
5354 + GETVALUE3(a, b, c, a1, b1, c1, d1, 0) == 0 \
5355 + && GETVALUE3(a, b, c, a2, b2, c2, d2, 255) == 255 \
5360 +free_d(struct ip_set_iptreemap_d *map)
5362 + kmem_cache_free(cachep_d, map);
5366 +free_c(struct ip_set_iptreemap_c *map)
5368 + struct ip_set_iptreemap_d *dtree;
5371 + LOOP_WALK_BEGIN(map, i, dtree) {
5372 + if (dtree != fullbitmap_d)
5374 + } LOOP_WALK_END();
5376 + kmem_cache_free(cachep_c, map);
5380 +free_b(struct ip_set_iptreemap_b *map)
5382 + struct ip_set_iptreemap_c *ctree;
5385 + LOOP_WALK_BEGIN(map, i, ctree) {
5386 + if (ctree != fullbitmap_c)
5388 + } LOOP_WALK_END();
5390 + kmem_cache_free(cachep_b, map);
5394 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5396 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5397 + struct ip_set_iptreemap_b *btree;
5398 + struct ip_set_iptreemap_c *ctree;
5399 + struct ip_set_iptreemap_d *dtree;
5400 + unsigned char a, b, c, d;
5404 + ABCD(a, b, c, d, hash_ip);
5406 + TESTIP_WALK(map, a, btree, fullbitmap_b);
5407 + TESTIP_WALK(btree, b, ctree, fullbitmap_c);
5408 + TESTIP_WALK(ctree, c, dtree, fullbitmap_d);
5410 + return !!test_bit(d, (void *) dtree->bitmap);
5414 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5416 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5418 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5419 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5423 + return __testip(set, req->start, hash_ip);
5427 +testip_kernel(struct ip_set *set, const struct sk_buff *skb, ip_set_ip_t *hash_ip, const u_int32_t *flags, unsigned char index)
5431 + res = __testip(set,
5432 + ntohl(flags[index] & IPSET_SRC
5433 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5434 + ? ip_hdr(skb)->saddr
5435 + : ip_hdr(skb)->daddr),
5437 + ? skb->nh.iph->saddr
5438 + : skb->nh.iph->daddr),
5442 + return (res < 0 ? 0 : res);
5446 +__addip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5448 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5449 + struct ip_set_iptreemap_b *btree;
5450 + struct ip_set_iptreemap_c *ctree;
5451 + struct ip_set_iptreemap_d *dtree;
5452 + unsigned char a, b, c, d;
5456 + ABCD(a, b, c, d, hash_ip);
5458 + ADDIP_WALK(map, a, btree, struct ip_set_iptreemap_b, cachep_b, fullbitmap_b);
5459 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreemap_c, cachep_c, fullbitmap_c);
5460 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreemap_d, cachep_d, fullbitmap_d);
5462 + if (test_and_set_bit(d, (void *) dtree->bitmap))
5465 + set_bit(b, (void *) btree->dirty);
5471 +__addip_range(struct ip_set *set, ip_set_ip_t start, ip_set_ip_t end, ip_set_ip_t *hash_ip)
5473 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5474 + struct ip_set_iptreemap_b *btree;
5475 + struct ip_set_iptreemap_c *ctree;
5476 + struct ip_set_iptreemap_d *dtree;
5477 + unsigned int a, b, c, d;
5478 + unsigned char a1, b1, c1, d1;
5479 + unsigned char a2, b2, c2, d2;
5482 + return __addip_single(set, start, hash_ip);
5486 + ABCD(a1, b1, c1, d1, &start);
5487 + ABCD(a2, b2, c2, d2, &end);
5489 + /* This is sooo ugly... */
5490 + ADDIP_RANGE_LOOP(map, a, a1, a2, CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2), btree, fullbitmap_b, cachep_b, free_b) {
5491 + ADDIP_RANGE_LOOP(btree, b, GETVALUE1(a, a1, b1, 0), GETVALUE1(a, a2, b2, 255), CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2), ctree, fullbitmap_c, cachep_c, free_c) {
5492 + ADDIP_RANGE_LOOP(ctree, c, GETVALUE2(a, b, a1, b1, c1, 0), GETVALUE2(a, b, a2, b2, c2, 255), CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2), dtree, fullbitmap_d, cachep_d, free_d) {
5493 + for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5494 + set_bit(d, (void *) dtree->bitmap);
5495 + set_bit(b, (void *) btree->dirty);
5496 + } ADDIP_RANGE_LOOP_END();
5497 + } ADDIP_RANGE_LOOP_END();
5498 + } ADDIP_RANGE_LOOP_END();
5504 +addip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5506 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5508 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5509 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5513 + return __addip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip);
5517 +addip_kernel(struct ip_set *set, const struct sk_buff *skb, ip_set_ip_t *hash_ip, const u_int32_t *flags, unsigned char index)
5520 + return __addip_single(set,
5521 + ntohl(flags[index] & IPSET_SRC
5522 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5523 + ? ip_hdr(skb)->saddr
5524 + : ip_hdr(skb)->daddr),
5526 + ? skb->nh.iph->saddr
5527 + : skb->nh.iph->daddr),
5533 +__delip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip, unsigned int __nocast flags)
5535 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5536 + struct ip_set_iptreemap_b *btree;
5537 + struct ip_set_iptreemap_c *ctree;
5538 + struct ip_set_iptreemap_d *dtree;
5539 + unsigned char a,b,c,d;
5543 + ABCD(a, b, c, d, hash_ip);
5545 + DELIP_WALK(map, a, btree, cachep_b, fullbitmap_b, flags);
5546 + DELIP_WALK(btree, b, ctree, cachep_c, fullbitmap_c, flags);
5547 + DELIP_WALK(ctree, c, dtree, cachep_d, fullbitmap_d, flags);
5549 + if (!test_and_clear_bit(d, (void *) dtree->bitmap))
5552 + set_bit(b, (void *) btree->dirty);
5558 +__delip_range(struct ip_set *set, ip_set_ip_t start, ip_set_ip_t end, ip_set_ip_t *hash_ip, unsigned int __nocast flags)
5560 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5561 + struct ip_set_iptreemap_b *btree;
5562 + struct ip_set_iptreemap_c *ctree;
5563 + struct ip_set_iptreemap_d *dtree;
5564 + unsigned int a, b, c, d;
5565 + unsigned char a1, b1, c1, d1;
5566 + unsigned char a2, b2, c2, d2;
5569 + return __delip_single(set, start, hash_ip, flags);
5573 + ABCD(a1, b1, c1, d1, &start);
5574 + ABCD(a2, b2, c2, d2, &end);
5576 + /* This is sooo ugly... */
5577 + DELIP_RANGE_LOOP(map, a, a1, a2, CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2), btree, fullbitmap_b, cachep_b, free_b, flags) {
5578 + DELIP_RANGE_LOOP(btree, b, GETVALUE1(a, a1, b1, 0), GETVALUE1(a, a2, b2, 255), CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2), ctree, fullbitmap_c, cachep_c, free_c, flags) {
5579 + DELIP_RANGE_LOOP(ctree, c, GETVALUE2(a, b, a1, b1, c1, 0), GETVALUE2(a, b, a2, b2, c2, 255), CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2), dtree, fullbitmap_d, cachep_d, free_d, flags) {
5580 + for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5581 + clear_bit(d, (void *) dtree->bitmap);
5582 + set_bit(b, (void *) btree->dirty);
5583 + } DELIP_RANGE_LOOP_END();
5584 + } DELIP_RANGE_LOOP_END();
5585 + } DELIP_RANGE_LOOP_END();
5591 +delip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5593 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5595 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5596 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5600 + return __delip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip, GFP_KERNEL);
5604 +delip_kernel(struct ip_set *set, const struct sk_buff *skb, ip_set_ip_t *hash_ip, const u_int32_t *flags, unsigned char index)
5606 + return __delip_single(set,
5607 + ntohl(flags[index] & IPSET_SRC
5608 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5609 + ? ip_hdr(skb)->saddr
5610 + : ip_hdr(skb)->daddr),
5612 + ? skb->nh.iph->saddr
5613 + : skb->nh.iph->daddr),
5619 +/* Check the status of the bitmap
5620 + * -1 == all bits cleared
5621 + * 1 == all bits set
5622 + * 0 == anything else
5625 +bitmap_status(struct ip_set_iptreemap_d *dtree)
5627 + unsigned char first = dtree->bitmap[0];
5630 + for (a = 1; a < 32; a++)
5631 + if (dtree->bitmap[a] != first)
5634 + return (first == 0 ? -1 : (first == 255 ? 1 : 0));
5638 +gc(unsigned long addr)
5640 + struct ip_set *set = (struct ip_set *) addr;
5641 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5642 + struct ip_set_iptreemap_b *btree;
5643 + struct ip_set_iptreemap_c *ctree;
5644 + struct ip_set_iptreemap_d *dtree;
5645 + unsigned int a, b, c;
5648 + write_lock_bh(&set->lock);
5650 + LOOP_WALK_BEGIN_GC(map, a, btree, fullbitmap_b, cachep_b, i) {
5651 + LOOP_WALK_BEGIN_GC(btree, b, ctree, fullbitmap_c, cachep_c, j) {
5652 + if (!test_and_clear_bit(b, (void *) btree->dirty))
5654 + LOOP_WALK_BEGIN_GC(ctree, c, dtree, fullbitmap_d, cachep_d, k) {
5655 + switch (bitmap_status(dtree)) {
5657 + kmem_cache_free(cachep_d, dtree);
5658 + ctree->tree[c] = NULL;
5662 + kmem_cache_free(cachep_d, dtree);
5663 + ctree->tree[c] = fullbitmap_d;
5667 + } LOOP_WALK_END();
5668 + } LOOP_WALK_END_GC(btree, b, ctree, fullbitmap_c, cachep_c, k);
5669 + } LOOP_WALK_END_GC(map, a, btree, fullbitmap_b, cachep_b, j);
5671 + write_unlock_bh(&set->lock);
5673 + map->gc.expires = jiffies + map->gc_interval * HZ;
5674 + add_timer(&map->gc);
5678 +init_gc_timer(struct ip_set *set)
5680 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5682 + init_timer(&map->gc);
5683 + map->gc.data = (unsigned long) set;
5684 + map->gc.function = gc;
5685 + map->gc.expires = jiffies + map->gc_interval * HZ;
5686 + add_timer(&map->gc);
5689 +static int create(struct ip_set *set, const void *data, size_t size)
5691 + struct ip_set_req_iptreemap_create *req = (struct ip_set_req_iptreemap_create *) data;
5692 + struct ip_set_iptreemap *map;
5694 + if (size != sizeof(struct ip_set_req_iptreemap_create)) {
5695 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap_create), size);
5699 + map = kzalloc(sizeof(*map), GFP_KERNEL);
5703 + map->gc_interval = req->gc_interval ? req->gc_interval : IPTREEMAP_DEFAULT_GC_TIME;
5706 + init_gc_timer(set);
5711 +static inline void __flush(struct ip_set_iptreemap *map)
5713 + struct ip_set_iptreemap_b *btree;
5716 + LOOP_WALK_BEGIN(map, a, btree);
5717 + if (btree != fullbitmap_b)
5722 +static void destroy(struct ip_set *set)
5724 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5726 + while (!del_timer(&map->gc))
5727 + msleep(IPTREEMAP_DESTROY_SLEEP);
5735 +static void flush(struct ip_set *set)
5737 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5739 + while (!del_timer(&map->gc))
5740 + msleep(IPTREEMAP_DESTROY_SLEEP);
5744 + memset(map, 0, sizeof(*map));
5746 + init_gc_timer(set);
5749 +static void list_header(const struct ip_set *set, void *data)
5751 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5752 + struct ip_set_req_iptreemap_create *header = (struct ip_set_req_iptreemap_create *) data;
5754 + header->gc_interval = map->gc_interval;
5757 +static int list_members_size(const struct ip_set *set)
5759 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5760 + struct ip_set_iptreemap_b *btree;
5761 + struct ip_set_iptreemap_c *ctree;
5762 + struct ip_set_iptreemap_d *dtree;
5763 + unsigned int a, b, c, d, inrange = 0, count = 0;
5765 + LOOP_WALK_BEGIN_COUNT(map, a, btree, inrange, count) {
5766 + LOOP_WALK_BEGIN_COUNT(btree, b, ctree, inrange, count) {
5767 + LOOP_WALK_BEGIN_COUNT(ctree, c, dtree, inrange, count) {
5768 + for (d = 0; d < 256; d++) {
5769 + if (test_bit(d, (void *) dtree->bitmap)) {
5771 + } else if (inrange) {
5776 + } LOOP_WALK_END_COUNT();
5777 + } LOOP_WALK_END_COUNT();
5778 + } LOOP_WALK_END_COUNT();
5783 + return (count * sizeof(struct ip_set_req_iptreemap));
5786 +static inline size_t add_member(void *data, size_t offset, ip_set_ip_t start, ip_set_ip_t end)
5788 + struct ip_set_req_iptreemap *entry = (struct ip_set_req_iptreemap *) (data + offset);
5790 + entry->start = start;
5793 + return sizeof(*entry);
5796 +static void list_members(const struct ip_set *set, void *data)
5798 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5799 + struct ip_set_iptreemap_b *btree;
5800 + struct ip_set_iptreemap_c *ctree;
5801 + struct ip_set_iptreemap_d *dtree;
5802 + unsigned int a, b, c, d, inrange = 0;
5803 + size_t offset = 0;
5804 + ip_set_ip_t start = 0, end = 0, ip;
5806 + LOOP_WALK_BEGIN(map, a, btree) {
5807 + LOOP_WALK_BEGIN(btree, b, ctree) {
5808 + LOOP_WALK_BEGIN(ctree, c, dtree) {
5809 + for (d = 0; d < 256; d++) {
5810 + if (test_bit(d, (void *) dtree->bitmap)) {
5811 + ip = ((a << 24) | (b << 16) | (c << 8) | d);
5815 + } else if (end < ip - 1) {
5816 + offset += add_member(data, offset, start, end);
5820 + } else if (inrange) {
5821 + offset += add_member(data, offset, start, end);
5825 + } LOOP_WALK_END();
5826 + } LOOP_WALK_END();
5827 + } LOOP_WALK_END();
5830 + add_member(data, offset, start, end);
5833 +static struct ip_set_type ip_set_iptreemap = {
5834 + .typename = SETTYPE_NAME,
5835 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
5836 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5838 + .destroy = destroy,
5840 + .reqsize = sizeof(struct ip_set_req_iptreemap),
5842 + .addip_kernel = addip_kernel,
5844 + .delip_kernel = delip_kernel,
5846 + .testip_kernel = testip_kernel,
5847 + .header_size = sizeof(struct ip_set_req_iptreemap_create),
5848 + .list_header = list_header,
5849 + .list_members_size = list_members_size,
5850 + .list_members = list_members,
5851 + .me = THIS_MODULE,
5854 +MODULE_LICENSE("GPL");
5855 +MODULE_AUTHOR("Sven Wegener <sven.wegener@stealer.net>");
5856 +MODULE_DESCRIPTION("iptreemap type of IP sets");
5858 +static int __init ip_set_iptreemap_init(void)
5860 + int ret = -ENOMEM;
5863 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5864 + cachep_b = kmem_cache_create("ip_set_iptreemap_b",
5865 + sizeof(struct ip_set_iptreemap_b),
5868 + cachep_b = kmem_cache_create("ip_set_iptreemap_b",
5869 + sizeof(struct ip_set_iptreemap_b),
5870 + 0, 0, NULL, NULL);
5873 + ip_set_printk("Unable to create ip_set_iptreemap_b slab cache");
5877 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5878 + cachep_c = kmem_cache_create("ip_set_iptreemap_c",
5879 + sizeof(struct ip_set_iptreemap_c),
5882 + cachep_c = kmem_cache_create("ip_set_iptreemap_c",
5883 + sizeof(struct ip_set_iptreemap_c),
5884 + 0, 0, NULL, NULL);
5887 + ip_set_printk("Unable to create ip_set_iptreemap_c slab cache");
5891 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5892 + cachep_d = kmem_cache_create("ip_set_iptreemap_d",
5893 + sizeof(struct ip_set_iptreemap_d),
5896 + cachep_d = kmem_cache_create("ip_set_iptreemap_d",
5897 + sizeof(struct ip_set_iptreemap_d),
5898 + 0, 0, NULL, NULL);
5901 + ip_set_printk("Unable to create ip_set_iptreemap_d slab cache");
5905 + fullbitmap_d = kmem_cache_alloc(cachep_d, GFP_KERNEL);
5906 + if (!fullbitmap_d)
5909 + fullbitmap_c = kmem_cache_alloc(cachep_c, GFP_KERNEL);
5910 + if (!fullbitmap_c)
5913 + fullbitmap_b = kmem_cache_alloc(cachep_b, GFP_KERNEL);
5914 + if (!fullbitmap_b)
5917 + ret = ip_set_register_set_type(&ip_set_iptreemap);
5921 + /* Now init our global bitmaps */
5922 + memset(fullbitmap_d->bitmap, 0xff, sizeof(fullbitmap_d->bitmap));
5924 + for (a = 0; a < 256; a++)
5925 + fullbitmap_c->tree[a] = fullbitmap_d;
5927 + for (a = 0; a < 256; a++)
5928 + fullbitmap_b->tree[a] = fullbitmap_c;
5929 + memset(fullbitmap_b->dirty, 0, sizeof(fullbitmap_b->dirty));
5934 + kmem_cache_free(cachep_b, fullbitmap_b);
5936 + kmem_cache_free(cachep_c, fullbitmap_c);
5938 + kmem_cache_free(cachep_d, fullbitmap_d);
5940 + kmem_cache_destroy(cachep_d);
5942 + kmem_cache_destroy(cachep_c);
5944 + kmem_cache_destroy(cachep_b);
5950 +static void __exit ip_set_iptreemap_fini(void)
5952 + ip_set_unregister_set_type(&ip_set_iptreemap);
5953 + kmem_cache_free(cachep_d, fullbitmap_d);
5954 + kmem_cache_free(cachep_c, fullbitmap_c);
5955 + kmem_cache_free(cachep_b, fullbitmap_b);
5956 + kmem_cache_destroy(cachep_d);
5957 + kmem_cache_destroy(cachep_c);
5958 + kmem_cache_destroy(cachep_b);
5961 +module_init(ip_set_iptreemap_init);
5962 +module_exit(ip_set_iptreemap_fini);
5964 +++ b/net/ipv4/netfilter/ip_set_macipmap.c
5966 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5967 + * Patrick Schaaf <bof@bof.de>
5968 + * Martin Josefsson <gandalf@wlug.westbo.se>
5969 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5971 + * This program is free software; you can redistribute it and/or modify
5972 + * it under the terms of the GNU General Public License version 2 as
5973 + * published by the Free Software Foundation.
5976 +/* Kernel module implementing an IP set type: the macipmap type */
5978 +#include <linux/module.h>
5979 +#include <linux/ip.h>
5980 +#include <linux/skbuff.h>
5981 +#include <linux/version.h>
5982 +#include <linux/netfilter_ipv4/ip_tables.h>
5983 +#include <linux/netfilter_ipv4/ip_set.h>
5984 +#include <linux/errno.h>
5985 +#include <asm/uaccess.h>
5986 +#include <asm/bitops.h>
5987 +#include <linux/spinlock.h>
5988 +#include <linux/if_ether.h>
5989 +#include <linux/vmalloc.h>
5991 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
5992 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
5995 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5997 + struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
5998 + struct ip_set_macip *table = (struct ip_set_macip *) map->members;
5999 + struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
6001 + if (size != sizeof(struct ip_set_req_macipmap)) {
6002 + ip_set_printk("data length wrong (want %zu, have %zu)",
6003 + sizeof(struct ip_set_req_macipmap),
6008 + if (req->ip < map->first_ip || req->ip > map->last_ip)
6011 + *hash_ip = req->ip;
6012 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
6013 + set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
6014 + if (test_bit(IPSET_MACIP_ISSET,
6015 + (void *) &table[req->ip - map->first_ip].flags)) {
6016 + return (memcmp(req->ethernet,
6017 + &table[req->ip - map->first_ip].ethernet,
6020 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6025 +testip_kernel(struct ip_set *set,
6026 + const struct sk_buff *skb,
6027 + ip_set_ip_t *hash_ip,
6028 + const u_int32_t *flags,
6029 + unsigned char index)
6031 + struct ip_set_macipmap *map =
6032 + (struct ip_set_macipmap *) set->data;
6033 + struct ip_set_macip *table =
6034 + (struct ip_set_macip *) map->members;
6037 + ip = ntohl(flags[index] & IPSET_SRC
6038 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6039 + ? ip_hdr(skb)->saddr
6040 + : ip_hdr(skb)->daddr);
6042 + ? skb->nh.iph->saddr
6043 + : skb->nh.iph->daddr);
6046 + if (ip < map->first_ip || ip > map->last_ip)
6050 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
6051 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
6052 + if (test_bit(IPSET_MACIP_ISSET,
6053 + (void *) &table[ip - map->first_ip].flags)) {
6054 + /* Is mac pointer valid?
6055 + * If so, compare... */
6056 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6057 + return (skb_mac_header(skb) >= skb->head
6058 + && (skb_mac_header(skb) + ETH_HLEN) <= skb->data
6060 + return (skb->mac.raw >= skb->head
6061 + && (skb->mac.raw + ETH_HLEN) <= skb->data
6063 + && (memcmp(eth_hdr(skb)->h_source,
6064 + &table[ip - map->first_ip].ethernet,
6067 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6071 +/* returns 0 on success */
6073 +__addip(struct ip_set *set,
6074 + ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
6076 + struct ip_set_macipmap *map =
6077 + (struct ip_set_macipmap *) set->data;
6078 + struct ip_set_macip *table =
6079 + (struct ip_set_macip *) map->members;
6081 + if (ip < map->first_ip || ip > map->last_ip)
6083 + if (test_and_set_bit(IPSET_MACIP_ISSET,
6084 + (void *) &table[ip - map->first_ip].flags))
6088 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6089 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
6094 +addip(struct ip_set *set, const void *data, size_t size,
6095 + ip_set_ip_t *hash_ip)
6097 + struct ip_set_req_macipmap *req =
6098 + (struct ip_set_req_macipmap *) data;
6100 + if (size != sizeof(struct ip_set_req_macipmap)) {
6101 + ip_set_printk("data length wrong (want %zu, have %zu)",
6102 + sizeof(struct ip_set_req_macipmap),
6106 + return __addip(set, req->ip, req->ethernet, hash_ip);
6110 +addip_kernel(struct ip_set *set,
6111 + const struct sk_buff *skb,
6112 + ip_set_ip_t *hash_ip,
6113 + const u_int32_t *flags,
6114 + unsigned char index)
6118 + ip = ntohl(flags[index] & IPSET_SRC
6119 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6120 + ? ip_hdr(skb)->saddr
6121 + : ip_hdr(skb)->daddr);
6123 + ? skb->nh.iph->saddr
6124 + : skb->nh.iph->daddr);
6127 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6128 + if (!(skb_mac_header(skb) >= skb->head
6129 + && (skb_mac_header(skb) + ETH_HLEN) <= skb->data))
6131 + if (!(skb->mac.raw >= skb->head
6132 + && (skb->mac.raw + ETH_HLEN) <= skb->data))
6136 + return __addip(set, ip, eth_hdr(skb)->h_source, hash_ip);
6140 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6142 + struct ip_set_macipmap *map =
6143 + (struct ip_set_macipmap *) set->data;
6144 + struct ip_set_macip *table =
6145 + (struct ip_set_macip *) map->members;
6147 + if (ip < map->first_ip || ip > map->last_ip)
6149 + if (!test_and_clear_bit(IPSET_MACIP_ISSET,
6150 + (void *)&table[ip - map->first_ip].flags))
6154 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6159 +delip(struct ip_set *set, const void *data, size_t size,
6160 + ip_set_ip_t *hash_ip)
6162 + struct ip_set_req_macipmap *req =
6163 + (struct ip_set_req_macipmap *) data;
6165 + if (size != sizeof(struct ip_set_req_macipmap)) {
6166 + ip_set_printk("data length wrong (want %zu, have %zu)",
6167 + sizeof(struct ip_set_req_macipmap),
6171 + return __delip(set, req->ip, hash_ip);
6175 +delip_kernel(struct ip_set *set,
6176 + const struct sk_buff *skb,
6177 + ip_set_ip_t *hash_ip,
6178 + const u_int32_t *flags,
6179 + unsigned char index)
6181 + return __delip(set,
6182 + ntohl(flags[index] & IPSET_SRC
6183 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6184 + ? ip_hdr(skb)->saddr
6185 + : ip_hdr(skb)->daddr),
6187 + ? skb->nh.iph->saddr
6188 + : skb->nh.iph->daddr),
6193 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
6195 + return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
6198 +static int create(struct ip_set *set, const void *data, size_t size)
6201 + struct ip_set_req_macipmap_create *req =
6202 + (struct ip_set_req_macipmap_create *) data;
6203 + struct ip_set_macipmap *map;
6205 + if (size != sizeof(struct ip_set_req_macipmap_create)) {
6206 + ip_set_printk("data length wrong (want %zu, have %zu)",
6207 + sizeof(struct ip_set_req_macipmap_create),
6212 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
6213 + HIPQUAD(req->from), HIPQUAD(req->to));
6215 + if (req->from > req->to) {
6216 + DP("bad ip range");
6220 + if (req->to - req->from > MAX_RANGE) {
6221 + ip_set_printk("range too big (max %d addresses)",
6226 + map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
6228 + DP("out of memory for %d bytes",
6229 + sizeof(struct ip_set_macipmap));
6232 + map->flags = req->flags;
6233 + map->first_ip = req->from;
6234 + map->last_ip = req->to;
6235 + newbytes = members_size(map->first_ip, map->last_ip);
6236 + map->members = ip_set_malloc(newbytes);
6237 + DP("members: %u %p", newbytes, map->members);
6238 + if (!map->members) {
6239 + DP("out of memory for %d bytes", newbytes);
6243 + memset(map->members, 0, newbytes);
6249 +static void destroy(struct ip_set *set)
6251 + struct ip_set_macipmap *map =
6252 + (struct ip_set_macipmap *) set->data;
6254 + ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
6260 +static void flush(struct ip_set *set)
6262 + struct ip_set_macipmap *map =
6263 + (struct ip_set_macipmap *) set->data;
6264 + memset(map->members, 0, members_size(map->first_ip, map->last_ip));
6267 +static void list_header(const struct ip_set *set, void *data)
6269 + struct ip_set_macipmap *map =
6270 + (struct ip_set_macipmap *) set->data;
6271 + struct ip_set_req_macipmap_create *header =
6272 + (struct ip_set_req_macipmap_create *) data;
6274 + DP("list_header %x %x %u", map->first_ip, map->last_ip,
6277 + header->from = map->first_ip;
6278 + header->to = map->last_ip;
6279 + header->flags = map->flags;
6282 +static int list_members_size(const struct ip_set *set)
6284 + struct ip_set_macipmap *map =
6285 + (struct ip_set_macipmap *) set->data;
6287 + DP("%u", members_size(map->first_ip, map->last_ip));
6288 + return members_size(map->first_ip, map->last_ip);
6291 +static void list_members(const struct ip_set *set, void *data)
6293 + struct ip_set_macipmap *map =
6294 + (struct ip_set_macipmap *) set->data;
6296 + int bytes = members_size(map->first_ip, map->last_ip);
6298 + DP("members: %u %p", bytes, map->members);
6299 + memcpy(data, map->members, bytes);
6302 +static struct ip_set_type ip_set_macipmap = {
6303 + .typename = SETTYPE_NAME,
6304 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6305 + .protocol_version = IP_SET_PROTOCOL_VERSION,
6306 + .create = &create,
6307 + .destroy = &destroy,
6309 + .reqsize = sizeof(struct ip_set_req_macipmap),
6311 + .addip_kernel = &addip_kernel,
6313 + .delip_kernel = &delip_kernel,
6314 + .testip = &testip,
6315 + .testip_kernel = &testip_kernel,
6316 + .header_size = sizeof(struct ip_set_req_macipmap_create),
6317 + .list_header = &list_header,
6318 + .list_members_size = &list_members_size,
6319 + .list_members = &list_members,
6320 + .me = THIS_MODULE,
6323 +MODULE_LICENSE("GPL");
6324 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6325 +MODULE_DESCRIPTION("macipmap type of IP sets");
6327 +static int __init ip_set_macipmap_init(void)
6329 + init_max_malloc_size();
6330 + return ip_set_register_set_type(&ip_set_macipmap);
6333 +static void __exit ip_set_macipmap_fini(void)
6335 + /* FIXME: possible race with ip_set_create() */
6336 + ip_set_unregister_set_type(&ip_set_macipmap);
6339 +module_init(ip_set_macipmap_init);
6340 +module_exit(ip_set_macipmap_fini);
6342 +++ b/net/ipv4/netfilter/ip_set_nethash.c
6344 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6346 + * This program is free software; you can redistribute it and/or modify
6347 + * it under the terms of the GNU General Public License version 2 as
6348 + * published by the Free Software Foundation.
6351 +/* Kernel module implementing a cidr nethash set */
6353 +#include <linux/module.h>
6354 +#include <linux/ip.h>
6355 +#include <linux/skbuff.h>
6356 +#include <linux/version.h>
6357 +#include <linux/jhash.h>
6358 +#include <linux/netfilter_ipv4/ip_tables.h>
6359 +#include <linux/netfilter_ipv4/ip_set.h>
6360 +#include <linux/errno.h>
6361 +#include <asm/uaccess.h>
6362 +#include <asm/bitops.h>
6363 +#include <linux/spinlock.h>
6364 +#include <linux/vmalloc.h>
6365 +#include <linux/random.h>
6367 +#include <net/ip.h>
6369 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
6370 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
6372 +static int limit = MAX_RANGE;
6374 +static inline __u32
6375 +jhash_ip(const struct ip_set_nethash *map, uint16_t i, ip_set_ip_t ip)
6377 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
6380 +static inline __u32
6381 +hash_id_cidr(struct ip_set_nethash *map,
6383 + unsigned char cidr,
6384 + ip_set_ip_t *hash_ip)
6388 + ip_set_ip_t *elem;
6390 + *hash_ip = pack(ip, cidr);
6392 + for (i = 0; i < map->probes; i++) {
6393 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
6394 + DP("hash key: %u", id);
6395 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6396 + if (*elem == *hash_ip)
6402 +static inline __u32
6403 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6405 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6406 + __u32 id = UINT_MAX;
6409 + for (i = 0; i < 30 && map->cidr[i]; i++) {
6410 + id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
6411 + if (id != UINT_MAX)
6418 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
6419 + ip_set_ip_t *hash_ip)
6421 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6423 + return (ip && hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
6427 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6429 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
6433 +testip(struct ip_set *set, const void *data, size_t size,
6434 + ip_set_ip_t *hash_ip)
6436 + struct ip_set_req_nethash *req =
6437 + (struct ip_set_req_nethash *) data;
6439 + if (size != sizeof(struct ip_set_req_nethash)) {
6440 + ip_set_printk("data length wrong (want %zu, have %zu)",
6441 + sizeof(struct ip_set_req_nethash),
6445 + return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
6446 + : __testip_cidr(set, req->ip, req->cidr, hash_ip));
6450 +testip_kernel(struct ip_set *set,
6451 + const struct sk_buff *skb,
6452 + ip_set_ip_t *hash_ip,
6453 + const u_int32_t *flags,
6454 + unsigned char index)
6456 + return __testip(set,
6457 + ntohl(flags[index] & IPSET_SRC
6458 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6459 + ? ip_hdr(skb)->saddr
6460 + : ip_hdr(skb)->daddr),
6462 + ? skb->nh.iph->saddr
6463 + : skb->nh.iph->daddr),
6469 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
6473 + ip_set_ip_t *elem;
6475 + for (i = 0; i < map->probes; i++) {
6476 + probe = jhash_ip(map, i, ip) % map->hashsize;
6477 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
6486 + /* Trigger rehashing */
6491 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6492 + ip_set_ip_t *hash_ip)
6494 + if (!ip || map->elements >= limit)
6497 + *hash_ip = pack(ip, cidr);
6498 + DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
6500 + return __addip_base(map, *hash_ip);
6504 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
6506 + unsigned char next;
6509 + for (i = 0; i < 30 && map->cidr[i]; i++) {
6510 + if (map->cidr[i] == cidr) {
6512 + } else if (map->cidr[i] < cidr) {
6513 + next = map->cidr[i];
6514 + map->cidr[i] = cidr;
6519 + map->cidr[i] = cidr;
6523 +addip(struct ip_set *set, const void *data, size_t size,
6524 + ip_set_ip_t *hash_ip)
6526 + struct ip_set_req_nethash *req =
6527 + (struct ip_set_req_nethash *) data;
6530 + if (size != sizeof(struct ip_set_req_nethash)) {
6531 + ip_set_printk("data length wrong (want %zu, have %zu)",
6532 + sizeof(struct ip_set_req_nethash),
6536 + ret = __addip((struct ip_set_nethash *) set->data,
6537 + req->ip, req->cidr, hash_ip);
6540 + update_cidr_sizes((struct ip_set_nethash *) set->data,
6547 +addip_kernel(struct ip_set *set,
6548 + const struct sk_buff *skb,
6549 + ip_set_ip_t *hash_ip,
6550 + const u_int32_t *flags,
6551 + unsigned char index)
6553 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6554 + int ret = -ERANGE;
6555 + ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC
6556 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6557 + ? ip_hdr(skb)->saddr
6558 + : ip_hdr(skb)->daddr);
6560 + ? skb->nh.iph->saddr
6561 + : skb->nh.iph->daddr);
6565 + ret = __addip(map, ip, map->cidr[0], hash_ip);
6570 +static int retry(struct ip_set *set)
6572 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6573 + ip_set_ip_t *elem;
6575 + u_int32_t i, hashsize = map->hashsize;
6577 + struct ip_set_nethash *tmp;
6579 + if (map->resize == 0)
6585 + /* Calculate new parameters */
6586 + hashsize += (hashsize * map->resize)/100;
6587 + if (hashsize == map->hashsize)
6590 + ip_set_printk("rehashing of set %s triggered: "
6591 + "hashsize grows from %u to %u",
6592 + set->name, map->hashsize, hashsize);
6594 + tmp = kmalloc(sizeof(struct ip_set_nethash)
6595 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
6597 + DP("out of memory for %d bytes",
6598 + sizeof(struct ip_set_nethash)
6599 + + map->probes * sizeof(uint32_t));
6602 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
6603 + if (!tmp->members) {
6604 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
6608 + tmp->hashsize = hashsize;
6609 + tmp->elements = 0;
6610 + tmp->probes = map->probes;
6611 + tmp->resize = map->resize;
6612 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
6613 + memcpy(tmp->cidr, map->cidr, 30 * sizeof(unsigned char));
6615 + write_lock_bh(&set->lock);
6616 + map = (struct ip_set_nethash *) set->data; /* Play safe */
6617 + for (i = 0; i < map->hashsize && res == 0; i++) {
6618 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
6620 + res = __addip_base(tmp, *elem);
6623 + /* Failure, try again */
6624 + write_unlock_bh(&set->lock);
6625 + harray_free(tmp->members);
6630 + /* Success at resizing! */
6631 + members = map->members;
6633 + map->hashsize = tmp->hashsize;
6634 + map->members = tmp->members;
6635 + write_unlock_bh(&set->lock);
6637 + harray_free(members);
6644 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6645 + ip_set_ip_t *hash_ip)
6647 + ip_set_ip_t id, *elem;
6652 + id = hash_id_cidr(map, ip, cidr, hash_ip);
6653 + if (id == UINT_MAX)
6656 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6663 +delip(struct ip_set *set, const void *data, size_t size,
6664 + ip_set_ip_t *hash_ip)
6666 + struct ip_set_req_nethash *req =
6667 + (struct ip_set_req_nethash *) data;
6669 + if (size != sizeof(struct ip_set_req_nethash)) {
6670 + ip_set_printk("data length wrong (want %zu, have %zu)",
6671 + sizeof(struct ip_set_req_nethash),
6675 + /* TODO: no garbage collection in map->cidr */
6676 + return __delip((struct ip_set_nethash *) set->data,
6677 + req->ip, req->cidr, hash_ip);
6681 +delip_kernel(struct ip_set *set,
6682 + const struct sk_buff *skb,
6683 + ip_set_ip_t *hash_ip,
6684 + const u_int32_t *flags,
6685 + unsigned char index)
6687 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6688 + int ret = -ERANGE;
6689 + ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC
6690 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6691 + ? ip_hdr(skb)->saddr
6692 + : ip_hdr(skb)->daddr);
6694 + ? skb->nh.iph->saddr
6695 + : skb->nh.iph->daddr);
6699 + ret = __delip(map, ip, map->cidr[0], hash_ip);
6704 +static int create(struct ip_set *set, const void *data, size_t size)
6706 + struct ip_set_req_nethash_create *req =
6707 + (struct ip_set_req_nethash_create *) data;
6708 + struct ip_set_nethash *map;
6711 + if (size != sizeof(struct ip_set_req_nethash_create)) {
6712 + ip_set_printk("data length wrong (want %zu, have %zu)",
6713 + sizeof(struct ip_set_req_nethash_create),
6718 + if (req->hashsize < 1) {
6719 + ip_set_printk("hashsize too small");
6722 + if (req->probes < 1) {
6723 + ip_set_printk("probes too small");
6727 + map = kmalloc(sizeof(struct ip_set_nethash)
6728 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
6730 + DP("out of memory for %d bytes",
6731 + sizeof(struct ip_set_nethash)
6732 + + req->probes * sizeof(uint32_t));
6735 + for (i = 0; i < req->probes; i++)
6736 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
6737 + map->elements = 0;
6738 + map->hashsize = req->hashsize;
6739 + map->probes = req->probes;
6740 + map->resize = req->resize;
6741 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
6742 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
6743 + if (!map->members) {
6744 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
6753 +static void destroy(struct ip_set *set)
6755 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6757 + harray_free(map->members);
6763 +static void flush(struct ip_set *set)
6765 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6766 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
6767 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
6768 + map->elements = 0;
6771 +static void list_header(const struct ip_set *set, void *data)
6773 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6774 + struct ip_set_req_nethash_create *header =
6775 + (struct ip_set_req_nethash_create *) data;
6777 + header->hashsize = map->hashsize;
6778 + header->probes = map->probes;
6779 + header->resize = map->resize;
6782 +static int list_members_size(const struct ip_set *set)
6784 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6786 + return (map->hashsize * sizeof(ip_set_ip_t));
6789 +static void list_members(const struct ip_set *set, void *data)
6791 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6792 + ip_set_ip_t i, *elem;
6794 + for (i = 0; i < map->hashsize; i++) {
6795 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
6796 + ((ip_set_ip_t *)data)[i] = *elem;
6800 +static struct ip_set_type ip_set_nethash = {
6801 + .typename = SETTYPE_NAME,
6802 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6803 + .protocol_version = IP_SET_PROTOCOL_VERSION,
6804 + .create = &create,
6805 + .destroy = &destroy,
6807 + .reqsize = sizeof(struct ip_set_req_nethash),
6809 + .addip_kernel = &addip_kernel,
6812 + .delip_kernel = &delip_kernel,
6813 + .testip = &testip,
6814 + .testip_kernel = &testip_kernel,
6815 + .header_size = sizeof(struct ip_set_req_nethash_create),
6816 + .list_header = &list_header,
6817 + .list_members_size = &list_members_size,
6818 + .list_members = &list_members,
6819 + .me = THIS_MODULE,
6822 +MODULE_LICENSE("GPL");
6823 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6824 +MODULE_DESCRIPTION("nethash type of IP sets");
6825 +module_param(limit, int, 0600);
6826 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
6828 +static int __init ip_set_nethash_init(void)
6830 + return ip_set_register_set_type(&ip_set_nethash);
6833 +static void __exit ip_set_nethash_fini(void)
6835 + /* FIXME: possible race with ip_set_create() */
6836 + ip_set_unregister_set_type(&ip_set_nethash);
6839 +module_init(ip_set_nethash_init);
6840 +module_exit(ip_set_nethash_fini);
6842 +++ b/net/ipv4/netfilter/ip_set_portmap.c
6844 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6846 + * This program is free software; you can redistribute it and/or modify
6847 + * it under the terms of the GNU General Public License version 2 as
6848 + * published by the Free Software Foundation.
6851 +/* Kernel module implementing a port set type as a bitmap */
6853 +#include <linux/module.h>
6854 +#include <linux/ip.h>
6855 +#include <linux/tcp.h>
6856 +#include <linux/udp.h>
6857 +#include <linux/skbuff.h>
6858 +#include <linux/version.h>
6859 +#include <linux/netfilter_ipv4/ip_tables.h>
6860 +#include <linux/netfilter_ipv4/ip_set.h>
6861 +#include <linux/errno.h>
6862 +#include <asm/uaccess.h>
6863 +#include <asm/bitops.h>
6864 +#include <linux/spinlock.h>
6866 +#include <net/ip.h>
6868 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
6870 +/* We must handle non-linear skbs */
6871 +static inline ip_set_ip_t
6872 +get_port(const struct sk_buff *skb, u_int32_t flags)
6874 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6875 + struct iphdr *iph = ip_hdr(skb);
6877 + struct iphdr *iph = skb->nh.iph;
6879 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
6880 + switch (iph->protocol) {
6881 + case IPPROTO_TCP: {
6882 + struct tcphdr tcph;
6884 + /* See comments at tcp_match in ip_tables.c */
6886 + return INVALID_PORT;
6888 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6889 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
6891 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
6893 + /* No choice either */
6894 + return INVALID_PORT;
6896 + return ntohs(flags & IPSET_SRC ?
6897 + tcph.source : tcph.dest);
6899 + case IPPROTO_UDP: {
6900 + struct udphdr udph;
6903 + return INVALID_PORT;
6905 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6906 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
6908 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
6910 + /* No choice either */
6911 + return INVALID_PORT;
6913 + return ntohs(flags & IPSET_SRC ?
6914 + udph.source : udph.dest);
6917 + return INVALID_PORT;
6922 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
6924 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
6926 + if (port < map->first_port || port > map->last_port)
6929 + *hash_port = port;
6930 + DP("set: %s, port:%u, %u", set->name, port, *hash_port);
6931 + return !!test_bit(port - map->first_port, map->members);
6935 +testport(struct ip_set *set, const void *data, size_t size,
6936 + ip_set_ip_t *hash_port)
6938 + struct ip_set_req_portmap *req =
6939 + (struct ip_set_req_portmap *) data;
6941 + if (size != sizeof(struct ip_set_req_portmap)) {
6942 + ip_set_printk("data length wrong (want %zu, have %zu)",
6943 + sizeof(struct ip_set_req_portmap),
6947 + return __testport(set, req->port, hash_port);
6951 +testport_kernel(struct ip_set *set,
6952 + const struct sk_buff *skb,
6953 + ip_set_ip_t *hash_port,
6954 + const u_int32_t *flags,
6955 + unsigned char index)
6958 + ip_set_ip_t port = get_port(skb, flags[index]);
6960 + DP("flag %s port %u", flags[index] & IPSET_SRC ? "SRC" : "DST", port);
6961 + if (port == INVALID_PORT)
6964 + res = __testport(set, port, hash_port);
6966 + return (res < 0 ? 0 : res);
6970 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
6972 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
6974 + if (port < map->first_port || port > map->last_port)
6976 + if (test_and_set_bit(port - map->first_port, map->members))
6979 + *hash_port = port;
6980 + DP("port %u", port);
6985 +addport(struct ip_set *set, const void *data, size_t size,
6986 + ip_set_ip_t *hash_port)
6988 + struct ip_set_req_portmap *req =
6989 + (struct ip_set_req_portmap *) data;
6991 + if (size != sizeof(struct ip_set_req_portmap)) {
6992 + ip_set_printk("data length wrong (want %zu, have %zu)",
6993 + sizeof(struct ip_set_req_portmap),
6997 + return __addport(set, req->port, hash_port);
7001 +addport_kernel(struct ip_set *set,
7002 + const struct sk_buff *skb,
7003 + ip_set_ip_t *hash_port,
7004 + const u_int32_t *flags,
7005 + unsigned char index)
7007 + ip_set_ip_t port = get_port(skb, flags[index]);
7009 + if (port == INVALID_PORT)
7012 + return __addport(set, port, hash_port);
7016 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
7018 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7020 + if (port < map->first_port || port > map->last_port)
7022 + if (!test_and_clear_bit(port - map->first_port, map->members))
7025 + *hash_port = port;
7026 + DP("port %u", port);
7031 +delport(struct ip_set *set, const void *data, size_t size,
7032 + ip_set_ip_t *hash_port)
7034 + struct ip_set_req_portmap *req =
7035 + (struct ip_set_req_portmap *) data;
7037 + if (size != sizeof(struct ip_set_req_portmap)) {
7038 + ip_set_printk("data length wrong (want %zu, have %zu)",
7039 + sizeof(struct ip_set_req_portmap),
7043 + return __delport(set, req->port, hash_port);
7047 +delport_kernel(struct ip_set *set,
7048 + const struct sk_buff *skb,
7049 + ip_set_ip_t *hash_port,
7050 + const u_int32_t *flags,
7051 + unsigned char index)
7053 + ip_set_ip_t port = get_port(skb, flags[index]);
7055 + if (port == INVALID_PORT)
7058 + return __delport(set, port, hash_port);
7061 +static int create(struct ip_set *set, const void *data, size_t size)
7064 + struct ip_set_req_portmap_create *req =
7065 + (struct ip_set_req_portmap_create *) data;
7066 + struct ip_set_portmap *map;
7068 + if (size != sizeof(struct ip_set_req_portmap_create)) {
7069 + ip_set_printk("data length wrong (want %zu, have %zu)",
7070 + sizeof(struct ip_set_req_portmap_create),
7075 + DP("from %u to %u", req->from, req->to);
7077 + if (req->from > req->to) {
7078 + DP("bad port range");
7082 + if (req->to - req->from > MAX_RANGE) {
7083 + ip_set_printk("range too big (max %d ports)",
7088 + map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
7090 + DP("out of memory for %d bytes",
7091 + sizeof(struct ip_set_portmap));
7094 + map->first_port = req->from;
7095 + map->last_port = req->to;
7096 + newbytes = bitmap_bytes(req->from, req->to);
7097 + map->members = kmalloc(newbytes, GFP_KERNEL);
7098 + if (!map->members) {
7099 + DP("out of memory for %d bytes", newbytes);
7103 + memset(map->members, 0, newbytes);
7109 +static void destroy(struct ip_set *set)
7111 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7113 + kfree(map->members);
7119 +static void flush(struct ip_set *set)
7121 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7122 + memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
7125 +static void list_header(const struct ip_set *set, void *data)
7127 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7128 + struct ip_set_req_portmap_create *header =
7129 + (struct ip_set_req_portmap_create *) data;
7131 + DP("list_header %u %u", map->first_port, map->last_port);
7133 + header->from = map->first_port;
7134 + header->to = map->last_port;
7137 +static int list_members_size(const struct ip_set *set)
7139 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7141 + return bitmap_bytes(map->first_port, map->last_port);
7144 +static void list_members(const struct ip_set *set, void *data)
7146 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7147 + int bytes = bitmap_bytes(map->first_port, map->last_port);
7149 + memcpy(data, map->members, bytes);
7152 +static struct ip_set_type ip_set_portmap = {
7153 + .typename = SETTYPE_NAME,
7154 + .features = IPSET_TYPE_PORT | IPSET_DATA_SINGLE,
7155 + .protocol_version = IP_SET_PROTOCOL_VERSION,
7156 + .create = &create,
7157 + .destroy = &destroy,
7159 + .reqsize = sizeof(struct ip_set_req_portmap),
7160 + .addip = &addport,
7161 + .addip_kernel = &addport_kernel,
7162 + .delip = &delport,
7163 + .delip_kernel = &delport_kernel,
7164 + .testip = &testport,
7165 + .testip_kernel = &testport_kernel,
7166 + .header_size = sizeof(struct ip_set_req_portmap_create),
7167 + .list_header = &list_header,
7168 + .list_members_size = &list_members_size,
7169 + .list_members = &list_members,
7170 + .me = THIS_MODULE,
7173 +MODULE_LICENSE("GPL");
7174 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7175 +MODULE_DESCRIPTION("portmap type of IP sets");
7177 +static int __init ip_set_portmap_init(void)
7179 + return ip_set_register_set_type(&ip_set_portmap);
7182 +static void __exit ip_set_portmap_fini(void)
7184 + /* FIXME: possible race with ip_set_create() */
7185 + ip_set_unregister_set_type(&ip_set_portmap);
7188 +module_init(ip_set_portmap_init);
7189 +module_exit(ip_set_portmap_fini);
7191 +++ b/net/ipv4/netfilter/ipt_set.c
7193 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7194 + * Patrick Schaaf <bof@bof.de>
7195 + * Martin Josefsson <gandalf@wlug.westbo.se>
7196 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7198 + * This program is free software; you can redistribute it and/or modify
7199 + * it under the terms of the GNU General Public License version 2 as
7200 + * published by the Free Software Foundation.
7203 +/* Kernel module to match an IP set. */
7205 +#include <linux/module.h>
7206 +#include <linux/ip.h>
7207 +#include <linux/skbuff.h>
7208 +#include <linux/version.h>
7210 +#include <linux/netfilter_ipv4/ip_tables.h>
7211 +#include <linux/netfilter_ipv4/ip_set.h>
7212 +#include <linux/netfilter_ipv4/ipt_set.h>
7215 +match_set(const struct ipt_set_info *info,
7216 + const struct sk_buff *skb,
7219 + if (ip_set_testip_kernel(info->index, skb, info->flags))
7224 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7229 +match(const struct sk_buff *skb,
7230 + const struct net_device *in,
7231 + const struct net_device *out,
7232 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7233 + const struct xt_match *match,
7235 + const void *matchinfo,
7236 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7237 + int offset, unsigned int protoff, bool *hotdrop)
7238 +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7239 + int offset, unsigned int protoff, int *hotdrop)
7241 + int offset, int *hotdrop)
7244 + const struct ipt_set_info_match *info = matchinfo;
7246 + return match_set(&info->match_set,
7248 + info->match_set.flags[0] & IPSET_MATCH_INV);
7251 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7256 +checkentry(const char *tablename,
7257 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7260 + const struct ipt_ip *ip,
7262 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7263 + const struct xt_match *match,
7266 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7267 + unsigned int matchsize,
7269 + unsigned int hook_mask)
7271 + struct ipt_set_info_match *info =
7272 + (struct ipt_set_info_match *) matchinfo;
7273 + ip_set_id_t index;
7275 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7276 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7277 + ip_set_printk("invalid matchsize %d", matchsize);
7282 + index = ip_set_get_byindex(info->match_set.index);
7284 + if (index == IP_SET_INVALID_ID) {
7285 + ip_set_printk("Cannot find set indentified by id %u to match",
7286 + info->match_set.index);
7287 + return 0; /* error */
7289 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7290 + ip_set_printk("That's nasty!");
7291 + return 0; /* error */
7297 +static void destroy(
7298 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7299 + const struct xt_match *match,
7301 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7302 + void *matchinfo, unsigned int matchsize)
7307 + struct ipt_set_info_match *info = matchinfo;
7309 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7310 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7311 + ip_set_printk("invalid matchsize %d", matchsize);
7315 + ip_set_put(info->match_set.index);
7318 +static struct ipt_match set_match = {
7320 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7321 + .family = AF_INET,
7324 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7325 + .matchsize = sizeof(struct ipt_set_info_match),
7327 + .checkentry = &checkentry,
7328 + .destroy = &destroy,
7332 +MODULE_LICENSE("GPL");
7333 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7334 +MODULE_DESCRIPTION("iptables IP set match module");
7336 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7337 +#define ipt_register_match xt_register_match
7338 +#define ipt_unregister_match xt_unregister_match
7341 +static int __init ipt_ipset_init(void)
7343 + return ipt_register_match(&set_match);
7346 +static void __exit ipt_ipset_fini(void)
7348 + ipt_unregister_match(&set_match);
7351 +module_init(ipt_ipset_init);
7352 +module_exit(ipt_ipset_fini);
7354 +++ b/net/ipv4/netfilter/ipt_SET.c
7356 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7357 + * Patrick Schaaf <bof@bof.de>
7358 + * Martin Josefsson <gandalf@wlug.westbo.se>
7359 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7361 + * This program is free software; you can redistribute it and/or modify
7362 + * it under the terms of the GNU General Public License version 2 as
7363 + * published by the Free Software Foundation.
7366 +/* ipt_SET.c - netfilter target to manipulate IP sets */
7368 +#include <linux/types.h>
7369 +#include <linux/ip.h>
7370 +#include <linux/timer.h>
7371 +#include <linux/module.h>
7372 +#include <linux/netfilter.h>
7373 +#include <linux/netdevice.h>
7374 +#include <linux/if.h>
7375 +#include <linux/inetdevice.h>
7376 +#include <linux/version.h>
7377 +#include <net/protocol.h>
7378 +#include <net/checksum.h>
7379 +#include <linux/netfilter_ipv4.h>
7380 +#include <linux/netfilter_ipv4/ip_tables.h>
7381 +#include <linux/netfilter_ipv4/ipt_set.h>
7383 +static unsigned int
7384 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
7385 +target(struct sk_buff *skb,
7387 +target(struct sk_buff **pskb,
7389 + const struct net_device *in,
7390 + const struct net_device *out,
7391 + unsigned int hooknum,
7392 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7393 + const struct xt_target *target,
7395 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7396 + const void *targinfo,
7399 + const void *targinfo)
7402 + const struct ipt_set_info_target *info = targinfo;
7403 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
7404 + struct sk_buff *skb = *pskb;
7407 + if (info->add_set.index != IP_SET_INVALID_ID)
7408 + ip_set_addip_kernel(info->add_set.index,
7410 + info->add_set.flags);
7411 + if (info->del_set.index != IP_SET_INVALID_ID)
7412 + ip_set_delip_kernel(info->del_set.index,
7414 + info->del_set.flags);
7416 + return IPT_CONTINUE;
7419 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7424 +checkentry(const char *tablename,
7425 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7428 + const struct ipt_entry *e,
7430 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7431 + const struct xt_target *target,
7434 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7435 + unsigned int targinfosize,
7437 + unsigned int hook_mask)
7439 + struct ipt_set_info_target *info =
7440 + (struct ipt_set_info_target *) targinfo;
7441 + ip_set_id_t index;
7443 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7444 + if (targinfosize != IPT_ALIGN(sizeof(*info))) {
7445 + DP("bad target info size %u", targinfosize);
7450 + if (info->add_set.index != IP_SET_INVALID_ID) {
7451 + index = ip_set_get_byindex(info->add_set.index);
7452 + if (index == IP_SET_INVALID_ID) {
7453 + ip_set_printk("cannot find add_set index %u as target",
7454 + info->add_set.index);
7455 + return 0; /* error */
7459 + if (info->del_set.index != IP_SET_INVALID_ID) {
7460 + index = ip_set_get_byindex(info->del_set.index);
7461 + if (index == IP_SET_INVALID_ID) {
7462 + ip_set_printk("cannot find del_set index %u as target",
7463 + info->del_set.index);
7464 + return 0; /* error */
7467 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
7468 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7469 + ip_set_printk("That's nasty!");
7470 + return 0; /* error */
7476 +static void destroy(
7477 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7478 + const struct xt_target *target,
7480 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7481 + void *targetinfo, unsigned int targetsize)
7486 + struct ipt_set_info_target *info = targetinfo;
7488 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7489 + if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
7490 + ip_set_printk("invalid targetsize %d", targetsize);
7494 + if (info->add_set.index != IP_SET_INVALID_ID)
7495 + ip_set_put(info->add_set.index);
7496 + if (info->del_set.index != IP_SET_INVALID_ID)
7497 + ip_set_put(info->del_set.index);
7500 +static struct ipt_target SET_target = {
7502 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7503 + .family = AF_INET,
7506 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7507 + .targetsize = sizeof(struct ipt_set_info_target),
7509 + .checkentry = checkentry,
7510 + .destroy = destroy,
7514 +MODULE_LICENSE("GPL");
7515 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7516 +MODULE_DESCRIPTION("iptables IP set target module");
7518 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7519 +#define ipt_register_target xt_register_target
7520 +#define ipt_unregister_target xt_unregister_target
7523 +static int __init ipt_SET_init(void)
7525 + return ipt_register_target(&SET_target);
7528 +static void __exit ipt_SET_fini(void)
7530 + ipt_unregister_target(&SET_target);
7533 +module_init(ipt_SET_init);
7534 +module_exit(ipt_SET_fini);
7535 --- a/net/ipv4/netfilter/Kconfig
7536 +++ b/net/ipv4/netfilter/Kconfig
7537 @@ -388,5 +388,122 @@ config IP_NF_ARP_MANGLE
7539 endif # IP_NF_ARPTABLES
7542 + tristate "IP set support"
7543 + depends on INET && NETFILTER
7545 + This option adds IP set support to the kernel.
7546 + In order to define and use sets, you need the userspace utility
7549 + To compile it as a module, choose M here. If unsure, say N.
7551 +config IP_NF_SET_MAX
7552 + int "Maximum number of IP sets"
7555 + depends on IP_NF_SET
7557 + You can define here default value of the maximum number
7558 + of IP sets for the kernel.
7560 + The value can be overriden by the 'max_sets' module
7561 + parameter of the 'ip_set' module.
7563 +config IP_NF_SET_HASHSIZE
7564 + int "Hash size for bindings of IP sets"
7566 + depends on IP_NF_SET
7568 + You can define here default value of the hash size for
7569 + bindings of IP sets.
7571 + The value can be overriden by the 'hash_size' module
7572 + parameter of the 'ip_set' module.
7574 +config IP_NF_SET_IPMAP
7575 + tristate "ipmap set support"
7576 + depends on IP_NF_SET
7578 + This option adds the ipmap set type support.
7580 + To compile it as a module, choose M here. If unsure, say N.
7582 +config IP_NF_SET_MACIPMAP
7583 + tristate "macipmap set support"
7584 + depends on IP_NF_SET
7586 + This option adds the macipmap set type support.
7588 + To compile it as a module, choose M here. If unsure, say N.
7590 +config IP_NF_SET_PORTMAP
7591 + tristate "portmap set support"
7592 + depends on IP_NF_SET
7594 + This option adds the portmap set type support.
7596 + To compile it as a module, choose M here. If unsure, say N.
7598 +config IP_NF_SET_IPHASH
7599 + tristate "iphash set support"
7600 + depends on IP_NF_SET
7602 + This option adds the iphash set type support.
7604 + To compile it as a module, choose M here. If unsure, say N.
7606 +config IP_NF_SET_NETHASH
7607 + tristate "nethash set support"
7608 + depends on IP_NF_SET
7610 + This option adds the nethash set type support.
7612 + To compile it as a module, choose M here. If unsure, say N.
7614 +config IP_NF_SET_IPPORTHASH
7615 + tristate "ipporthash set support"
7616 + depends on IP_NF_SET
7618 + This option adds the ipporthash set type support.
7620 + To compile it as a module, choose M here. If unsure, say N.
7622 +config IP_NF_SET_IPTREE
7623 + tristate "iptree set support"
7624 + depends on IP_NF_SET
7626 + This option adds the iptree set type support.
7628 + To compile it as a module, choose M here. If unsure, say N.
7630 +config IP_NF_SET_IPTREEMAP
7631 + tristate "iptreemap set support"
7632 + depends on IP_NF_SET
7634 + This option adds the iptreemap set type support.
7636 + To compile it as a module, choose M here. If unsure, say N.
7638 +config IP_NF_MATCH_SET
7639 + tristate "set match support"
7640 + depends on IP_NF_SET
7642 + Set matching matches against given IP sets.
7643 + You need the ipset utility to create and set up the sets.
7645 + To compile it as a module, choose M here. If unsure, say N.
7647 +config IP_NF_TARGET_SET
7648 + tristate "SET target support"
7649 + depends on IP_NF_SET
7651 + The SET target makes possible to add/delete entries
7653 + You need the ipset utility to create and set up the sets.
7655 + To compile it as a module, choose M here. If unsure, say N.
7660 --- a/net/ipv4/netfilter/Makefile
7661 +++ b/net/ipv4/netfilter/Makefile
7662 @@ -51,6 +51,7 @@ obj-$(CONFIG_IP_NF_SECURITY) += iptable_
7663 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
7664 obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o
7665 obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
7666 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
7669 obj-$(CONFIG_IP_NF_TARGET_CLUSTERIP) += ipt_CLUSTERIP.o
7670 @@ -61,6 +62,18 @@ obj-$(CONFIG_IP_NF_TARGET_NETMAP) += ipt
7671 obj-$(CONFIG_IP_NF_TARGET_REDIRECT) += ipt_REDIRECT.o
7672 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
7673 obj-$(CONFIG_IP_NF_TARGET_ULOG) += ipt_ULOG.o
7674 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
7677 +obj-$(CONFIG_IP_NF_SET) += ip_set.o
7678 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
7679 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
7680 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
7681 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
7682 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
7683 +obj-$(CONFIG_IP_NF_SET_IPPORTHASH) += ip_set_ipporthash.o
7684 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
7685 +obj-$(CONFIG_IP_NF_SET_IPTREEMAP) += ip_set_iptreemap.o
7687 # generic ARP tables
7688 obj-$(CONFIG_IP_NF_ARPTABLES) += arp_tables.o