2 +++ b/include/linux/netfilter_ipv4/ip_set.h
7 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
8 + * Patrick Schaaf <bof@bof.de>
9 + * Martin Josefsson <gandalf@wlug.westbo.se>
10 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 + * This program is free software; you can redistribute it and/or modify
13 + * it under the terms of the GNU General Public License version 2 as
14 + * published by the Free Software Foundation.
22 + * A sockopt of such quality has hardly ever been seen before on the open
23 + * market! This little beauty, hardly ever used: above 64, so it's
24 + * traditionally used for firewalling, not touched (even once!) by the
25 + * 2.0, 2.2 and 2.4 kernels!
27 + * Comes with its own certificate of authenticity, valid anywhere in the
35 + * Heavily modify by Joakim Axelsson 08.03.2002
36 + * - Made it more modulebased
38 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
40 + * - in order to "deal with" backward compatibility, renamed to ipset
44 + * Used so that the kernel module and ipset-binary can match their versions
46 +#define IP_SET_PROTOCOL_VERSION 2
48 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
50 +/* Lets work with our own typedef for representing an IP address.
51 + * We hope to make the code more portable, possibly to IPv6...
53 + * The representation works in HOST byte order, because most set types
54 + * will perform arithmetic operations and compare operations.
56 + * For now the type is an uint32_t.
58 + * Make sure to ONLY use the functions when translating and parsing
59 + * in order to keep the host byte order and make it more portable:
64 + * (Joakim: where are they???)
67 +typedef uint32_t ip_set_ip_t;
69 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
70 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
72 +typedef uint16_t ip_set_id_t;
74 +#define IP_SET_INVALID_ID 65535
76 +/* How deep we follow bindings */
77 +#define IP_SET_MAX_BINDINGS 6
80 + * Option flags for kernel operations (ipt_set_info)
82 +#define IPSET_SRC 0x01 /* Source match/add */
83 +#define IPSET_DST 0x02 /* Destination match/add */
84 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
89 +#define IPSET_TYPE_IP 0x01 /* IP address type of set */
90 +#define IPSET_TYPE_PORT 0x02 /* Port type of set */
91 +#define IPSET_DATA_SINGLE 0x04 /* Single data storage */
92 +#define IPSET_DATA_DOUBLE 0x08 /* Double data storage */
94 +/* Reserved keywords */
95 +#define IPSET_TOKEN_DEFAULT ":default:"
96 +#define IPSET_TOKEN_ALL ":all:"
98 +/* SO_IP_SET operation constants, and their request struct types.
101 + * 0-99: commands with version checking
102 + * 100-199: add/del/test/bind/unbind
103 + * 200-299: list, save, restore
106 +/* Single shot operations:
107 + * version, create, destroy, flush, rename and swap
109 + * Sets are identified by name.
112 +#define IP_SET_REQ_STD \
114 + unsigned version; \
115 + char name[IP_SET_MAXNAMELEN]
117 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
118 +struct ip_set_req_create {
120 + char typename[IP_SET_MAXNAMELEN];
123 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
124 +struct ip_set_req_std {
128 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
129 +/* Uses ip_set_req_std */
131 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
132 +/* Uses ip_set_req_create */
134 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
135 +/* Uses ip_set_req_create */
137 +union ip_set_name_index {
138 + char name[IP_SET_MAXNAMELEN];
142 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
143 +struct ip_set_req_get_set {
146 + union ip_set_name_index set;
149 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
150 +/* Uses ip_set_req_get_set */
152 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
153 +struct ip_set_req_version {
158 +/* Double shots operations:
159 + * add, del, test, bind and unbind.
161 + * First we query the kernel to get the index and type of the target set,
162 + * then issue the command. Validity of IP is checked in kernel in order
163 + * to minimalize sockopt operations.
166 +/* Get minimal set data for add/del/test/bind/unbind IP */
167 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
168 +struct ip_set_req_adt_get {
171 + union ip_set_name_index set;
172 + char typename[IP_SET_MAXNAMELEN];
175 +#define IP_SET_REQ_BYINDEX \
179 +struct ip_set_req_adt {
180 + IP_SET_REQ_BYINDEX;
183 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
184 +/* Uses ip_set_req_adt, with type specific addage */
186 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
187 +/* Uses ip_set_req_adt, with type specific addage */
189 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
190 +/* Uses ip_set_req_adt, with type specific addage */
192 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
193 +/* Uses ip_set_req_bind, with type specific addage */
194 +struct ip_set_req_bind {
195 + IP_SET_REQ_BYINDEX;
196 + char binding[IP_SET_MAXNAMELEN];
199 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
200 +/* Uses ip_set_req_bind, with type speficic addage
201 + * index = 0 means unbinding for all sets */
203 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
204 +/* Uses ip_set_req_bind, with type specific addage */
206 +/* Multiple shots operations: list, save, restore.
208 + * - check kernel version and query the max number of sets
209 + * - get the basic information on all sets
210 + * and size required for the next step
211 + * - get actual set data: header, data, bindings
214 +/* Get max_sets and the index of a queried set
216 +#define IP_SET_OP_MAX_SETS 0x00000020
217 +struct ip_set_req_max_sets {
220 + ip_set_id_t max_sets; /* max_sets */
221 + ip_set_id_t sets; /* real number of sets */
222 + union ip_set_name_index set; /* index of set if name used */
225 +/* Get the id and name of the sets plus size for next step */
226 +#define IP_SET_OP_LIST_SIZE 0x00000201
227 +#define IP_SET_OP_SAVE_SIZE 0x00000202
228 +struct ip_set_req_setnames {
230 + ip_set_id_t index; /* set to list/save */
231 + size_t size; /* size to get setdata/bindings */
232 + /* followed by sets number of struct ip_set_name_list */
235 +struct ip_set_name_list {
236 + char name[IP_SET_MAXNAMELEN];
237 + char typename[IP_SET_MAXNAMELEN];
242 +/* The actual list operation */
243 +#define IP_SET_OP_LIST 0x00000203
244 +struct ip_set_req_list {
245 + IP_SET_REQ_BYINDEX;
246 + /* sets number of struct ip_set_list in reply */
249 +struct ip_set_list {
251 + ip_set_id_t binding;
253 + size_t header_size; /* Set header data of header_size */
254 + size_t members_size; /* Set members data of members_size */
255 + size_t bindings_size; /* Set bindings data of bindings_size */
258 +struct ip_set_hash_list {
260 + ip_set_id_t binding;
263 +/* The save operation */
264 +#define IP_SET_OP_SAVE 0x00000204
265 +/* Uses ip_set_req_list, in the reply replaced by
266 + * sets number of struct ip_set_save plus a marker
267 + * ip_set_save followed by ip_set_hash_save structures.
269 +struct ip_set_save {
271 + ip_set_id_t binding;
272 + size_t header_size; /* Set header data of header_size */
273 + size_t members_size; /* Set members data of members_size */
276 +/* At restoring, ip == 0 means default binding for the given set: */
277 +struct ip_set_hash_save {
280 + ip_set_id_t binding;
283 +/* The restore operation */
284 +#define IP_SET_OP_RESTORE 0x00000205
285 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
286 + * plus a marker ip_set_restore, followed by ip_set_hash_save
289 +struct ip_set_restore {
290 + char name[IP_SET_MAXNAMELEN];
291 + char typename[IP_SET_MAXNAMELEN];
293 + size_t header_size; /* Create data of header_size */
294 + size_t members_size; /* Set members data of members_size */
297 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
299 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
304 +#define ip_set_printk(format, args...) \
306 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
307 + printk(format "\n" , ## args); \
310 +#if defined(IP_SET_DEBUG)
311 +#define DP(format, args...) \
313 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
314 + printk(format "\n" , ## args); \
316 +#define IP_SET_ASSERT(x) \
319 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
320 + __FILE__, __LINE__, __FUNCTION__); \
323 +#define DP(format, args...)
324 +#define IP_SET_ASSERT(x)
330 + * The ip_set_type definition - one per set type, e.g. "ipmap".
332 + * Each individual set has a pointer, set->type, going to one
333 + * of these structures. Function pointers inside the structure implement
334 + * the real behaviour of the sets.
336 + * If not mentioned differently, the implementation behind the function
337 + * pointers of a set_type, is expected to return 0 if ok, and a negative
338 + * errno (e.g. -EINVAL) on error.
340 +struct ip_set_type {
341 + struct list_head list; /* next in list of set types */
343 + /* test for IP in set (kernel: iptables -m set src|dst)
344 + * return 0 if not in set, 1 if in set.
346 + int (*testip_kernel) (struct ip_set *set,
347 + const struct sk_buff * skb,
349 + const u_int32_t *flags,
350 + unsigned char index);
352 + /* test for IP in set (userspace: ipset -T set IP)
353 + * return 0 if not in set, 1 if in set.
355 + int (*testip) (struct ip_set *set,
356 + const void *data, size_t size,
360 + * Size of the data structure passed by when
361 + * adding/deletin/testing an entry.
365 + /* Add IP into set (userspace: ipset -A set IP)
366 + * Return -EEXIST if the address is already in the set,
367 + * and -ERANGE if the address lies outside the set bounds.
368 + * If the address was not already in the set, 0 is returned.
370 + int (*addip) (struct ip_set *set,
371 + const void *data, size_t size,
374 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
375 + * Return -EEXIST if the address is already in the set,
376 + * and -ERANGE if the address lies outside the set bounds.
377 + * If the address was not already in the set, 0 is returned.
379 + int (*addip_kernel) (struct ip_set *set,
380 + const struct sk_buff * skb,
382 + const u_int32_t *flags,
383 + unsigned char index);
385 + /* remove IP from set (userspace: ipset -D set --entry x)
386 + * Return -EEXIST if the address is NOT in the set,
387 + * and -ERANGE if the address lies outside the set bounds.
388 + * If the address really was in the set, 0 is returned.
390 + int (*delip) (struct ip_set *set,
391 + const void *data, size_t size,
394 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
395 + * Return -EEXIST if the address is NOT in the set,
396 + * and -ERANGE if the address lies outside the set bounds.
397 + * If the address really was in the set, 0 is returned.
399 + int (*delip_kernel) (struct ip_set *set,
400 + const struct sk_buff * skb,
402 + const u_int32_t *flags,
403 + unsigned char index);
405 + /* new set creation - allocated type specific items
407 + int (*create) (struct ip_set *set,
408 + const void *data, size_t size);
410 + /* retry the operation after successfully tweaking the set
412 + int (*retry) (struct ip_set *set);
414 + /* set destruction - free type specific items
415 + * There is no return value.
416 + * Can be called only when child sets are destroyed.
418 + void (*destroy) (struct ip_set *set);
420 + /* set flushing - reset all bits in the set, or something similar.
421 + * There is no return value.
423 + void (*flush) (struct ip_set *set);
425 + /* Listing: size needed for header
427 + size_t header_size;
429 + /* Listing: Get the header
431 + * Fill in the information in "data".
432 + * This function is always run after list_header_size() under a
433 + * writelock on the set. Therefor is the length of "data" always
436 + void (*list_header) (const struct ip_set *set,
439 + /* Listing: Get the size for the set members
441 + int (*list_members_size) (const struct ip_set *set);
443 + /* Listing: Get the set members
445 + * Fill in the information in "data".
446 + * This function is always run after list_member_size() under a
447 + * writelock on the set. Therefor is the length of "data" always
450 + void (*list_members) (const struct ip_set *set,
453 + char typename[IP_SET_MAXNAMELEN];
454 + unsigned char features;
455 + int protocol_version;
457 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
461 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
462 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
464 +/* A generic ipset */
466 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
467 + rwlock_t lock; /* lock for concurrency control */
468 + ip_set_id_t id; /* set id for swapping */
469 + ip_set_id_t binding; /* default binding for the set */
470 + atomic_t ref; /* in kernel and in hash references */
471 + struct ip_set_type *type; /* the set types */
472 + void *data; /* pooltype specific data */
475 +/* Structure to bind set elements to sets */
476 +struct ip_set_hash {
477 + struct list_head list; /* list of clashing entries in hash */
478 + ip_set_ip_t ip; /* ip from set */
479 + ip_set_id_t id; /* set id */
480 + ip_set_id_t binding; /* set we bind the element to */
483 +/* register and unregister set references */
484 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
485 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
486 +extern void ip_set_put(ip_set_id_t id);
488 +/* API for iptables set match, and SET target */
489 +extern void ip_set_addip_kernel(ip_set_id_t id,
490 + const struct sk_buff *skb,
491 + const u_int32_t *flags);
492 +extern void ip_set_delip_kernel(ip_set_id_t id,
493 + const struct sk_buff *skb,
494 + const u_int32_t *flags);
495 +extern int ip_set_testip_kernel(ip_set_id_t id,
496 + const struct sk_buff *skb,
497 + const u_int32_t *flags);
499 +#endif /* __KERNEL__ */
501 +#endif /*_IP_SET_H*/
503 +++ b/include/linux/netfilter_ipv4/ip_set_iphash.h
505 +#ifndef __IP_SET_IPHASH_H
506 +#define __IP_SET_IPHASH_H
508 +#include <linux/netfilter_ipv4/ip_set.h>
510 +#define SETTYPE_NAME "iphash"
511 +#define MAX_RANGE 0x0000FFFF
513 +struct ip_set_iphash {
514 + ip_set_ip_t *members; /* the iphash proper */
515 + uint32_t elements; /* number of elements */
516 + uint32_t hashsize; /* hash size */
517 + uint16_t probes; /* max number of probes */
518 + uint16_t resize; /* resize factor in percent */
519 + ip_set_ip_t netmask; /* netmask */
520 + void *initval[0]; /* initvals for jhash_1word */
523 +struct ip_set_req_iphash_create {
527 + ip_set_ip_t netmask;
530 +struct ip_set_req_iphash {
534 +#endif /* __IP_SET_IPHASH_H */
536 +++ b/include/linux/netfilter_ipv4/ip_set_ipmap.h
538 +#ifndef __IP_SET_IPMAP_H
539 +#define __IP_SET_IPMAP_H
541 +#include <linux/netfilter_ipv4/ip_set.h>
543 +#define SETTYPE_NAME "ipmap"
544 +#define MAX_RANGE 0x0000FFFF
546 +struct ip_set_ipmap {
547 + void *members; /* the ipmap proper */
548 + ip_set_ip_t first_ip; /* host byte order, included in range */
549 + ip_set_ip_t last_ip; /* host byte order, included in range */
550 + ip_set_ip_t netmask; /* subnet netmask */
551 + ip_set_ip_t sizeid; /* size of set in IPs */
552 + ip_set_ip_t hosts; /* number of hosts in a subnet */
555 +struct ip_set_req_ipmap_create {
558 + ip_set_ip_t netmask;
561 +struct ip_set_req_ipmap {
566 +mask_to_bits(ip_set_ip_t mask)
568 + unsigned int bits = 32;
569 + ip_set_ip_t maskaddr;
571 + if (mask == 0xFFFFFFFF)
574 + maskaddr = 0xFFFFFFFE;
575 + while (--bits >= 0 && maskaddr != mask)
582 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
584 + ip_set_ip_t mask = 0xFFFFFFFE;
587 + while (--(*bits) >= 0 && mask && (to & mask) != from)
593 +#endif /* __IP_SET_IPMAP_H */
595 +++ b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
597 +#ifndef __IP_SET_IPPORTHASH_H
598 +#define __IP_SET_IPPORTHASH_H
600 +#include <linux/netfilter_ipv4/ip_set.h>
602 +#define SETTYPE_NAME "ipporthash"
603 +#define MAX_RANGE 0x0000FFFF
604 +#define INVALID_PORT (MAX_RANGE + 1)
606 +struct ip_set_ipporthash {
607 + ip_set_ip_t *members; /* the ipporthash proper */
608 + uint32_t elements; /* number of elements */
609 + uint32_t hashsize; /* hash size */
610 + uint16_t probes; /* max number of probes */
611 + uint16_t resize; /* resize factor in percent */
612 + ip_set_ip_t first_ip; /* host byte order, included in range */
613 + ip_set_ip_t last_ip; /* host byte order, included in range */
614 + void *initval[0]; /* initvals for jhash_1word */
617 +struct ip_set_req_ipporthash_create {
625 +struct ip_set_req_ipporthash {
630 +#endif /* __IP_SET_IPPORTHASH_H */
632 +++ b/include/linux/netfilter_ipv4/ip_set_iptree.h
634 +#ifndef __IP_SET_IPTREE_H
635 +#define __IP_SET_IPTREE_H
637 +#include <linux/netfilter_ipv4/ip_set.h>
639 +#define SETTYPE_NAME "iptree"
640 +#define MAX_RANGE 0x0000FFFF
642 +struct ip_set_iptreed {
643 + unsigned long expires[256]; /* x.x.x.ADDR */
646 +struct ip_set_iptreec {
647 + struct ip_set_iptreed *tree[256]; /* x.x.ADDR.* */
650 +struct ip_set_iptreeb {
651 + struct ip_set_iptreec *tree[256]; /* x.ADDR.*.* */
654 +struct ip_set_iptree {
655 + unsigned int timeout;
656 + unsigned int gc_interval;
658 + uint32_t elements; /* number of elements */
659 + struct timer_list gc;
660 + struct ip_set_iptreeb *tree[256]; /* ADDR.*.*.* */
664 +struct ip_set_req_iptree_create {
665 + unsigned int timeout;
668 +struct ip_set_req_iptree {
670 + unsigned int timeout;
673 +#endif /* __IP_SET_IPTREE_H */
675 +++ b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
677 +#ifndef __IP_SET_IPTREEMAP_H
678 +#define __IP_SET_IPTREEMAP_H
680 +#include <linux/netfilter_ipv4/ip_set.h>
682 +#define SETTYPE_NAME "iptreemap"
685 +struct ip_set_iptreemap_d {
686 + unsigned char bitmap[32]; /* x.x.x.y */
689 +struct ip_set_iptreemap_c {
690 + struct ip_set_iptreemap_d *tree[256]; /* x.x.y.x */
693 +struct ip_set_iptreemap_b {
694 + struct ip_set_iptreemap_c *tree[256]; /* x.y.x.x */
695 + unsigned char dirty[32];
699 +struct ip_set_iptreemap {
700 + unsigned int gc_interval;
702 + struct timer_list gc;
703 + struct ip_set_iptreemap_b *tree[256]; /* y.x.x.x */
707 +struct ip_set_req_iptreemap_create {
708 + unsigned int gc_interval;
711 +struct ip_set_req_iptreemap {
716 +#endif /* __IP_SET_IPTREEMAP_H */
718 +++ b/include/linux/netfilter_ipv4/ip_set_jhash.h
720 +#ifndef _LINUX_IPSET_JHASH_H
721 +#define _LINUX_IPSET_JHASH_H
723 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
724 + * to __u32/__u8 so that the header file can be included into
725 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
728 +/* jhash.h: Jenkins hash support.
730 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
732 + * http://burtleburtle.net/bob/hash/
734 + * These are the credits from Bob's sources:
736 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
737 + * hash(), hash2(), hash3, and mix() are externally useful functions.
738 + * Routines to test the hash are included if SELF_TEST is defined.
739 + * You can use this free for any purpose. It has no warranty.
741 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
743 + * I've modified Bob's hash to be useful in the Linux kernel, and
744 + * any bugs present are surely my fault. -DaveM
747 +/* NOTE: Arguments are modified. */
748 +#define __jhash_mix(a, b, c) \
750 + a -= b; a -= c; a ^= (c>>13); \
751 + b -= c; b -= a; b ^= (a<<8); \
752 + c -= a; c -= b; c ^= (b>>13); \
753 + a -= b; a -= c; a ^= (c>>12); \
754 + b -= c; b -= a; b ^= (a<<16); \
755 + c -= a; c -= b; c ^= (b>>5); \
756 + a -= b; a -= c; a ^= (c>>3); \
757 + b -= c; b -= a; b ^= (a<<10); \
758 + c -= a; c -= b; c ^= (b>>15); \
761 +/* The golden ration: an arbitrary value */
762 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
764 +/* The most generic version, hashes an arbitrary sequence
765 + * of bytes. No alignment or length assumptions are made about
768 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
770 + __u32 a, b, c, len;
774 + a = b = JHASH_GOLDEN_RATIO;
777 + while (len >= 12) {
778 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
779 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
780 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
782 + __jhash_mix(a,b,c);
790 + case 11: c += ((__u32)k[10]<<24);
791 + case 10: c += ((__u32)k[9]<<16);
792 + case 9 : c += ((__u32)k[8]<<8);
793 + case 8 : b += ((__u32)k[7]<<24);
794 + case 7 : b += ((__u32)k[6]<<16);
795 + case 6 : b += ((__u32)k[5]<<8);
796 + case 5 : b += k[4];
797 + case 4 : a += ((__u32)k[3]<<24);
798 + case 3 : a += ((__u32)k[2]<<16);
799 + case 2 : a += ((__u32)k[1]<<8);
800 + case 1 : a += k[0];
803 + __jhash_mix(a,b,c);
808 +/* A special optimized version that handles 1 or more of __u32s.
809 + * The length parameter here is the number of __u32s in the key.
811 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
813 + __u32 a, b, c, len;
815 + a = b = JHASH_GOLDEN_RATIO;
823 + __jhash_mix(a, b, c);
830 + case 2 : b += k[1];
831 + case 1 : a += k[0];
834 + __jhash_mix(a,b,c);
840 +/* A special ultra-optimized versions that knows they are hashing exactly
841 + * 3, 2 or 1 word(s).
843 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
844 + * done at the end is not done here.
846 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
848 + a += JHASH_GOLDEN_RATIO;
849 + b += JHASH_GOLDEN_RATIO;
852 + __jhash_mix(a, b, c);
857 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
859 + return jhash_3words(a, b, 0, initval);
862 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
864 + return jhash_3words(a, 0, 0, initval);
867 +#endif /* _LINUX_IPSET_JHASH_H */
869 +++ b/include/linux/netfilter_ipv4/ip_set_macipmap.h
871 +#ifndef __IP_SET_MACIPMAP_H
872 +#define __IP_SET_MACIPMAP_H
874 +#include <linux/netfilter_ipv4/ip_set.h>
876 +#define SETTYPE_NAME "macipmap"
877 +#define MAX_RANGE 0x0000FFFF
880 +#define IPSET_MACIP_MATCHUNSET 1
883 +#define IPSET_MACIP_ISSET 1
885 +struct ip_set_macipmap {
886 + void *members; /* the macipmap proper */
887 + ip_set_ip_t first_ip; /* host byte order, included in range */
888 + ip_set_ip_t last_ip; /* host byte order, included in range */
892 +struct ip_set_req_macipmap_create {
898 +struct ip_set_req_macipmap {
900 + unsigned char ethernet[ETH_ALEN];
903 +struct ip_set_macip {
904 + unsigned short flags;
905 + unsigned char ethernet[ETH_ALEN];
908 +#endif /* __IP_SET_MACIPMAP_H */
910 +++ b/include/linux/netfilter_ipv4/ip_set_malloc.h
912 +#ifndef _IP_SET_MALLOC_H
913 +#define _IP_SET_MALLOC_H
917 +/* Memory allocation and deallocation */
918 +static size_t max_malloc_size = 0;
920 +static inline void init_max_malloc_size(void)
922 +#define CACHE(x) max_malloc_size = x;
923 +#include <linux/kmalloc_sizes.h>
927 +static inline void * ip_set_malloc(size_t bytes)
929 + if (bytes > max_malloc_size)
930 + return vmalloc(bytes);
932 + return kmalloc(bytes, GFP_KERNEL);
935 +static inline void ip_set_free(void * data, size_t bytes)
937 + if (bytes > max_malloc_size)
944 + size_t max_elements;
948 +static inline void *
949 +harray_malloc(size_t hashsize, size_t typesize, int flags)
951 + struct harray *harray;
952 + size_t max_elements, size, i, j;
954 + if (!max_malloc_size)
955 + init_max_malloc_size();
957 + if (typesize > max_malloc_size)
960 + max_elements = max_malloc_size/typesize;
961 + size = hashsize/max_elements;
962 + if (hashsize % max_elements)
965 + /* Last pointer signals end of arrays */
966 + harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
972 + for (i = 0; i < size - 1; i++) {
973 + harray->arrays[i] = kmalloc(max_elements * typesize, flags);
974 + if (!harray->arrays[i])
976 + memset(harray->arrays[i], 0, max_elements * typesize);
978 + harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize,
980 + if (!harray->arrays[i])
982 + memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
984 + harray->max_elements = max_elements;
985 + harray->arrays[size] = NULL;
987 + return (void *)harray;
990 + for (j = 0; j < i; j++) {
991 + kfree(harray->arrays[j]);
997 +static inline void harray_free(void *h)
999 + struct harray *harray = (struct harray *) h;
1002 + for (i = 0; harray->arrays[i] != NULL; i++)
1003 + kfree(harray->arrays[i]);
1007 +static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
1009 + struct harray *harray = (struct harray *) h;
1012 + for (i = 0; harray->arrays[i+1] != NULL; i++)
1013 + memset(harray->arrays[i], 0, harray->max_elements * typesize);
1014 + memset(harray->arrays[i], 0,
1015 + (hashsize - i * harray->max_elements) * typesize);
1018 +#define HARRAY_ELEM(h, type, which) \
1020 + struct harray *__h = (struct harray *)(h); \
1021 + ((type)((__h)->arrays[(which)/(__h)->max_elements]) \
1022 + + (which)%(__h)->max_elements); \
1025 +#endif /* __KERNEL__ */
1027 +#endif /*_IP_SET_MALLOC_H*/
1029 +++ b/include/linux/netfilter_ipv4/ip_set_nethash.h
1031 +#ifndef __IP_SET_NETHASH_H
1032 +#define __IP_SET_NETHASH_H
1034 +#include <linux/netfilter_ipv4/ip_set.h>
1036 +#define SETTYPE_NAME "nethash"
1037 +#define MAX_RANGE 0x0000FFFF
1039 +struct ip_set_nethash {
1040 + ip_set_ip_t *members; /* the nethash proper */
1041 + uint32_t elements; /* number of elements */
1042 + uint32_t hashsize; /* hash size */
1043 + uint16_t probes; /* max number of probes */
1044 + uint16_t resize; /* resize factor in percent */
1045 + unsigned char cidr[30]; /* CIDR sizes */
1046 + void *initval[0]; /* initvals for jhash_1word */
1049 +struct ip_set_req_nethash_create {
1050 + uint32_t hashsize;
1055 +struct ip_set_req_nethash {
1057 + unsigned char cidr;
1060 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
1062 +static inline ip_set_ip_t
1063 +pack(ip_set_ip_t ip, unsigned char cidr)
1065 + ip_set_ip_t addr, *paddr = &addr;
1066 + unsigned char n, t, *a;
1068 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
1070 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
1074 + a = &((unsigned char *)paddr)[n];
1075 + *a = *a /(1 << (8 - t)) + shifts[t];
1077 + DP("n: %u, t: %u, a: %u", n, t, *a);
1078 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
1079 + HIPQUAD(ip), cidr, NIPQUAD(addr));
1082 + return ntohl(addr);
1085 +#endif /* __IP_SET_NETHASH_H */
1087 +++ b/include/linux/netfilter_ipv4/ip_set_portmap.h
1089 +#ifndef __IP_SET_PORTMAP_H
1090 +#define __IP_SET_PORTMAP_H
1092 +#include <linux/netfilter_ipv4/ip_set.h>
1094 +#define SETTYPE_NAME "portmap"
1095 +#define MAX_RANGE 0x0000FFFF
1096 +#define INVALID_PORT (MAX_RANGE + 1)
1098 +struct ip_set_portmap {
1099 + void *members; /* the portmap proper */
1100 + ip_set_ip_t first_port; /* host byte order, included in range */
1101 + ip_set_ip_t last_port; /* host byte order, included in range */
1104 +struct ip_set_req_portmap_create {
1109 +struct ip_set_req_portmap {
1113 +#endif /* __IP_SET_PORTMAP_H */
1115 +++ b/include/linux/netfilter_ipv4/ipt_set.h
1120 +#include <linux/netfilter_ipv4/ip_set.h>
1122 +struct ipt_set_info {
1123 + ip_set_id_t index;
1124 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1128 +struct ipt_set_info_match {
1129 + struct ipt_set_info match_set;
1132 +struct ipt_set_info_target {
1133 + struct ipt_set_info add_set;
1134 + struct ipt_set_info del_set;
1137 +#endif /*_IPT_SET_H*/
1139 +++ b/net/ipv4/netfilter/ip_set.c
1141 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1142 + * Patrick Schaaf <bof@bof.de>
1143 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1145 + * This program is free software; you can redistribute it and/or modify
1146 + * it under the terms of the GNU General Public License version 2 as
1147 + * published by the Free Software Foundation.
1150 +/* Kernel module for IP set management */
1152 +#include <linux/version.h>
1153 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
1154 +#include <linux/config.h>
1156 +#include <linux/module.h>
1157 +#include <linux/moduleparam.h>
1158 +#include <linux/kmod.h>
1159 +#include <linux/ip.h>
1160 +#include <linux/skbuff.h>
1161 +#include <linux/random.h>
1162 +#include <linux/jhash.h>
1163 +#include <linux/netfilter_ipv4/ip_tables.h>
1164 +#include <linux/errno.h>
1165 +#include <asm/uaccess.h>
1166 +#include <asm/bitops.h>
1167 +#include <asm/semaphore.h>
1168 +#include <linux/spinlock.h>
1169 +#include <linux/vmalloc.h>
1171 +#define ASSERT_READ_LOCK(x)
1172 +#define ASSERT_WRITE_LOCK(x)
1173 +#include <linux/netfilter_ipv4/ip_set.h>
1175 +static struct list_head set_type_list; /* all registered sets */
1176 +static struct ip_set **ip_set_list; /* all individual sets */
1177 +static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1178 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1179 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1180 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1181 +static struct list_head *ip_set_hash; /* hash of bindings */
1182 +static unsigned int ip_set_hash_random; /* random seed */
1185 + * Sets are identified either by the index in ip_set_list or by id.
1186 + * The id never changes and is used to find a key in the hash.
1187 + * The index may change by swapping and used at all other places
1188 + * (set/SET netfilter modules, binding value, etc.)
1190 + * Userspace requests are serialized by ip_set_mutex and sets can
1191 + * be deleted only from userspace. Therefore ip_set_list locking
1192 + * must obey the following rules:
1194 + * - kernel requests: read and write locking mandatory
1195 + * - user requests: read locking optional, write locking mandatory
1199 +__ip_set_get(ip_set_id_t index)
1201 + atomic_inc(&ip_set_list[index]->ref);
1205 +__ip_set_put(ip_set_id_t index)
1207 + atomic_dec(&ip_set_list[index]->ref);
1211 + * Binding routines
1214 +static inline struct ip_set_hash *
1215 +__ip_set_find(u_int32_t key, ip_set_id_t id, ip_set_ip_t ip)
1217 + struct ip_set_hash *set_hash;
1219 + list_for_each_entry(set_hash, &ip_set_hash[key], list)
1220 + if (set_hash->id == id && set_hash->ip == ip)
1227 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1229 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1230 + % ip_set_bindings_hash_size;
1231 + struct ip_set_hash *set_hash;
1233 + ASSERT_READ_LOCK(&ip_set_lock);
1234 + IP_SET_ASSERT(ip_set_list[id]);
1235 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1237 + set_hash = __ip_set_find(key, id, ip);
1239 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1241 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1243 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1247 +__set_hash_del(struct ip_set_hash *set_hash)
1249 + ASSERT_WRITE_LOCK(&ip_set_lock);
1250 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1252 + __ip_set_put(set_hash->binding);
1253 + list_del(&set_hash->list);
1258 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1260 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1261 + % ip_set_bindings_hash_size;
1262 + struct ip_set_hash *set_hash;
1264 + IP_SET_ASSERT(ip_set_list[id]);
1265 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1266 + write_lock_bh(&ip_set_lock);
1267 + set_hash = __ip_set_find(key, id, ip);
1268 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1270 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1272 + if (set_hash != NULL)
1273 + __set_hash_del(set_hash);
1274 + write_unlock_bh(&ip_set_lock);
1279 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1281 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1282 + % ip_set_bindings_hash_size;
1283 + struct ip_set_hash *set_hash;
1286 + IP_SET_ASSERT(ip_set_list[id]);
1287 + IP_SET_ASSERT(ip_set_list[binding]);
1288 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1289 + HIPQUAD(ip), ip_set_list[binding]->name);
1290 + write_lock_bh(&ip_set_lock);
1291 + set_hash = __ip_set_find(key, id, ip);
1293 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_ATOMIC);
1298 + INIT_LIST_HEAD(&set_hash->list);
1299 + set_hash->id = id;
1300 + set_hash->ip = ip;
1301 + list_add(&set_hash->list, &ip_set_hash[key]);
1303 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1304 + DP("overwrite binding: %s",
1305 + ip_set_list[set_hash->binding]->name);
1306 + __ip_set_put(set_hash->binding);
1308 + set_hash->binding = binding;
1309 + __ip_set_get(set_hash->binding);
1310 + DP("stored: key %u, id %u (%s), ip %u.%u.%u.%u, binding %u (%s)",
1311 + key, id, ip_set_list[id]->name,
1312 + HIPQUAD(ip), binding, ip_set_list[binding]->name);
1314 + write_unlock_bh(&ip_set_lock);
1318 +#define FOREACH_HASH_DO(fn, args...) \
1320 + ip_set_id_t __key; \
1321 + struct ip_set_hash *__set_hash; \
1323 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1324 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1325 + fn(__set_hash , ## args); \
1329 +#define FOREACH_HASH_RW_DO(fn, args...) \
1331 + ip_set_id_t __key; \
1332 + struct ip_set_hash *__set_hash, *__n; \
1334 + ASSERT_WRITE_LOCK(&ip_set_lock); \
1335 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1336 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1337 + fn(__set_hash , ## args); \
1341 +/* Add, del and test set entries from kernel */
1343 +#define follow_bindings(index, set, ip) \
1344 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1345 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1348 +ip_set_testip_kernel(ip_set_id_t index,
1349 + const struct sk_buff *skb,
1350 + const u_int32_t *flags)
1352 + struct ip_set *set;
1355 + unsigned char i = 0;
1357 + IP_SET_ASSERT(flags[i]);
1358 + read_lock_bh(&ip_set_lock);
1360 + set = ip_set_list[index];
1361 + IP_SET_ASSERT(set);
1362 + DP("set %s, index %u", set->name, index);
1363 + read_lock_bh(&set->lock);
1364 + res = set->type->testip_kernel(set, skb, &ip, flags, i++);
1365 + read_unlock_bh(&set->lock);
1366 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1369 + && follow_bindings(index, set, ip));
1370 + read_unlock_bh(&ip_set_lock);
1376 +ip_set_addip_kernel(ip_set_id_t index,
1377 + const struct sk_buff *skb,
1378 + const u_int32_t *flags)
1380 + struct ip_set *set;
1383 + unsigned char i = 0;
1385 + IP_SET_ASSERT(flags[i]);
1387 + read_lock_bh(&ip_set_lock);
1389 + set = ip_set_list[index];
1390 + IP_SET_ASSERT(set);
1391 + DP("set %s, index %u", set->name, index);
1392 + write_lock_bh(&set->lock);
1393 + res = set->type->addip_kernel(set, skb, &ip, flags, i++);
1394 + write_unlock_bh(&set->lock);
1395 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1396 + } while ((res == 0 || res == -EEXIST)
1398 + && follow_bindings(index, set, ip));
1399 + read_unlock_bh(&ip_set_lock);
1401 + if (res == -EAGAIN
1402 + && set->type->retry
1403 + && (res = set->type->retry(set)) == 0)
1408 +ip_set_delip_kernel(ip_set_id_t index,
1409 + const struct sk_buff *skb,
1410 + const u_int32_t *flags)
1412 + struct ip_set *set;
1415 + unsigned char i = 0;
1417 + IP_SET_ASSERT(flags[i]);
1418 + read_lock_bh(&ip_set_lock);
1420 + set = ip_set_list[index];
1421 + IP_SET_ASSERT(set);
1422 + DP("set %s, index %u", set->name, index);
1423 + write_lock_bh(&set->lock);
1424 + res = set->type->delip_kernel(set, skb, &ip, flags, i++);
1425 + write_unlock_bh(&set->lock);
1426 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1427 + } while ((res == 0 || res == -EEXIST)
1429 + && follow_bindings(index, set, ip));
1430 + read_unlock_bh(&ip_set_lock);
1433 +/* Register and deregister settype */
1435 +static inline struct ip_set_type *
1436 +find_set_type(const char *name)
1438 + struct ip_set_type *set_type;
1440 + list_for_each_entry(set_type, &set_type_list, list)
1441 + if (!strncmp(set_type->typename, name, IP_SET_MAXNAMELEN - 1))
1447 +ip_set_register_set_type(struct ip_set_type *set_type)
1451 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1452 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1453 + set_type->typename,
1454 + set_type->protocol_version,
1455 + IP_SET_PROTOCOL_VERSION);
1459 + write_lock_bh(&ip_set_lock);
1460 + if (find_set_type(set_type->typename)) {
1462 + ip_set_printk("'%s' already registered!",
1463 + set_type->typename);
1467 + if (!try_module_get(THIS_MODULE)) {
1471 + list_add(&set_type->list, &set_type_list);
1472 + DP("'%s' registered.", set_type->typename);
1474 + write_unlock_bh(&ip_set_lock);
1479 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1481 + write_lock_bh(&ip_set_lock);
1482 + if (!find_set_type(set_type->typename)) {
1483 + ip_set_printk("'%s' not registered?",
1484 + set_type->typename);
1487 + list_del(&set_type->list);
1488 + module_put(THIS_MODULE);
1489 + DP("'%s' unregistered.", set_type->typename);
1491 + write_unlock_bh(&ip_set_lock);
1496 + * Userspace routines
1500 + * Find set by name, reference it once. The reference makes sure the
1501 + * thing pointed to, does not go away under our feet. Drop the reference
1502 + * later, using ip_set_put().
1505 +ip_set_get_byname(const char *name)
1507 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1509 + down(&ip_set_app_mutex);
1510 + for (i = 0; i < ip_set_max; i++) {
1511 + if (ip_set_list[i] != NULL
1512 + && strcmp(ip_set_list[i]->name, name) == 0) {
1518 + up(&ip_set_app_mutex);
1523 + * Find set by index, reference it once. The reference makes sure the
1524 + * thing pointed to, does not go away under our feet. Drop the reference
1525 + * later, using ip_set_put().
1528 +ip_set_get_byindex(ip_set_id_t index)
1530 + down(&ip_set_app_mutex);
1532 + if (index >= ip_set_max)
1533 + return IP_SET_INVALID_ID;
1535 + if (ip_set_list[index])
1536 + __ip_set_get(index);
1538 + index = IP_SET_INVALID_ID;
1540 + up(&ip_set_app_mutex);
1545 + * If the given set pointer points to a valid set, decrement
1546 + * reference count by 1. The caller shall not assume the index
1547 + * to be valid, after calling this function.
1549 +void ip_set_put(ip_set_id_t index)
1551 + down(&ip_set_app_mutex);
1552 + if (ip_set_list[index])
1553 + __ip_set_put(index);
1554 + up(&ip_set_app_mutex);
1557 +/* Find a set by name or index */
1559 +ip_set_find_byname(const char *name)
1561 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1563 + for (i = 0; i < ip_set_max; i++) {
1564 + if (ip_set_list[i] != NULL
1565 + && strcmp(ip_set_list[i]->name, name) == 0) {
1574 +ip_set_find_byindex(ip_set_id_t index)
1576 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1577 + index = IP_SET_INVALID_ID;
1583 + * Add, del, test, bind and unbind
1587 +__ip_set_testip(struct ip_set *set,
1594 + read_lock_bh(&set->lock);
1595 + res = set->type->testip(set, data, size, ip);
1596 + read_unlock_bh(&set->lock);
1602 +__ip_set_addip(ip_set_id_t index,
1606 + struct ip_set *set = ip_set_list[index];
1610 + IP_SET_ASSERT(set);
1612 + write_lock_bh(&set->lock);
1613 + res = set->type->addip(set, data, size, &ip);
1614 + write_unlock_bh(&set->lock);
1615 + } while (res == -EAGAIN
1616 + && set->type->retry
1617 + && (res = set->type->retry(set)) == 0);
1623 +ip_set_addip(ip_set_id_t index,
1628 + return __ip_set_addip(index,
1629 + data + sizeof(struct ip_set_req_adt),
1630 + size - sizeof(struct ip_set_req_adt));
1634 +ip_set_delip(ip_set_id_t index,
1638 + struct ip_set *set = ip_set_list[index];
1642 + IP_SET_ASSERT(set);
1643 + write_lock_bh(&set->lock);
1644 + res = set->type->delip(set,
1645 + data + sizeof(struct ip_set_req_adt),
1646 + size - sizeof(struct ip_set_req_adt),
1648 + write_unlock_bh(&set->lock);
1654 +ip_set_testip(ip_set_id_t index,
1658 + struct ip_set *set = ip_set_list[index];
1662 + IP_SET_ASSERT(set);
1663 + res = __ip_set_testip(set,
1664 + data + sizeof(struct ip_set_req_adt),
1665 + size - sizeof(struct ip_set_req_adt),
1668 + return (res > 0 ? -EEXIST : res);
1672 +ip_set_bindip(ip_set_id_t index,
1676 + struct ip_set *set = ip_set_list[index];
1677 + struct ip_set_req_bind *req_bind;
1678 + ip_set_id_t binding;
1682 + IP_SET_ASSERT(set);
1683 + if (size < sizeof(struct ip_set_req_bind))
1686 + req_bind = (struct ip_set_req_bind *) data;
1687 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1689 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1690 + /* Default binding of a set */
1691 + char *binding_name;
1693 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1696 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1697 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1699 + binding = ip_set_find_byname(binding_name);
1700 + if (binding == IP_SET_INVALID_ID)
1703 + write_lock_bh(&ip_set_lock);
1704 + /* Sets as binding values are referenced */
1705 + if (set->binding != IP_SET_INVALID_ID)
1706 + __ip_set_put(set->binding);
1707 + set->binding = binding;
1708 + __ip_set_get(set->binding);
1709 + write_unlock_bh(&ip_set_lock);
1713 + binding = ip_set_find_byname(req_bind->binding);
1714 + if (binding == IP_SET_INVALID_ID)
1717 + res = __ip_set_testip(set,
1718 + data + sizeof(struct ip_set_req_bind),
1719 + size - sizeof(struct ip_set_req_bind),
1721 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1722 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1725 + res = ip_set_hash_add(set->id, ip, binding);
1730 +#define FOREACH_SET_DO(fn, args...) \
1732 + ip_set_id_t __i; \
1733 + struct ip_set *__set; \
1735 + for (__i = 0; __i < ip_set_max; __i++) { \
1736 + __set = ip_set_list[__i]; \
1737 + if (__set != NULL) \
1738 + fn(__set , ##args); \
1743 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1745 + if (set_hash->id == id)
1746 + __set_hash_del(set_hash);
1750 +__unbind_default(struct ip_set *set)
1752 + if (set->binding != IP_SET_INVALID_ID) {
1753 + /* Sets as binding values are referenced */
1754 + __ip_set_put(set->binding);
1755 + set->binding = IP_SET_INVALID_ID;
1760 +ip_set_unbindip(ip_set_id_t index,
1764 + struct ip_set *set;
1765 + struct ip_set_req_bind *req_bind;
1770 + if (size < sizeof(struct ip_set_req_bind))
1773 + req_bind = (struct ip_set_req_bind *) data;
1774 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1776 + DP("%u %s", index, req_bind->binding);
1777 + if (index == IP_SET_INVALID_ID) {
1778 + /* unbind :all: */
1779 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1780 + /* Default binding of sets */
1781 + write_lock_bh(&ip_set_lock);
1782 + FOREACH_SET_DO(__unbind_default);
1783 + write_unlock_bh(&ip_set_lock);
1785 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1786 + /* Flush all bindings of all sets*/
1787 + write_lock_bh(&ip_set_lock);
1788 + FOREACH_HASH_RW_DO(__set_hash_del);
1789 + write_unlock_bh(&ip_set_lock);
1792 + DP("unreachable reached!");
1796 + set = ip_set_list[index];
1797 + IP_SET_ASSERT(set);
1798 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1799 + /* Default binding of set */
1800 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1802 + if (binding == IP_SET_INVALID_ID)
1805 + write_lock_bh(&ip_set_lock);
1806 + /* Sets in hash values are referenced */
1807 + __ip_set_put(set->binding);
1808 + set->binding = IP_SET_INVALID_ID;
1809 + write_unlock_bh(&ip_set_lock);
1812 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1813 + /* Flush all bindings */
1815 + write_lock_bh(&ip_set_lock);
1816 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1817 + write_unlock_bh(&ip_set_lock);
1821 + res = __ip_set_testip(set,
1822 + data + sizeof(struct ip_set_req_bind),
1823 + size - sizeof(struct ip_set_req_bind),
1826 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1828 + res = ip_set_hash_del(set->id, ip);
1834 +ip_set_testbind(ip_set_id_t index,
1838 + struct ip_set *set = ip_set_list[index];
1839 + struct ip_set_req_bind *req_bind;
1840 + ip_set_id_t binding;
1844 + IP_SET_ASSERT(set);
1845 + if (size < sizeof(struct ip_set_req_bind))
1848 + req_bind = (struct ip_set_req_bind *) data;
1849 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1851 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1852 + /* Default binding of set */
1853 + char *binding_name;
1855 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1858 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1859 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1861 + binding = ip_set_find_byname(binding_name);
1862 + if (binding == IP_SET_INVALID_ID)
1865 + res = (set->binding == binding) ? -EEXIST : 0;
1869 + binding = ip_set_find_byname(req_bind->binding);
1870 + if (binding == IP_SET_INVALID_ID)
1874 + res = __ip_set_testip(set,
1875 + data + sizeof(struct ip_set_req_bind),
1876 + size - sizeof(struct ip_set_req_bind),
1878 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1879 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1882 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1888 +static struct ip_set_type *
1889 +find_set_type_rlock(const char *typename)
1891 + struct ip_set_type *type;
1893 + read_lock_bh(&ip_set_lock);
1894 + type = find_set_type(typename);
1896 + read_unlock_bh(&ip_set_lock);
1902 +find_free_id(const char *name,
1903 + ip_set_id_t *index,
1908 + *id = IP_SET_INVALID_ID;
1909 + for (i = 0; i < ip_set_max; i++) {
1910 + if (ip_set_list[i] == NULL) {
1911 + if (*id == IP_SET_INVALID_ID)
1913 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1917 + if (*id == IP_SET_INVALID_ID)
1918 + /* No free slot remained */
1920 + /* Check that index is usable as id (swapping) */
1922 + for (i = 0; i < ip_set_max; i++) {
1923 + if (ip_set_list[i] != NULL
1924 + && ip_set_list[i]->id == *id) {
1936 +ip_set_create(const char *name,
1937 + const char *typename,
1938 + ip_set_id_t restore,
1942 + struct ip_set *set;
1943 + ip_set_id_t index = 0, id;
1946 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1948 + * First, and without any locks, allocate and initialize
1949 + * a normal base set structure.
1951 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1954 + set->lock = RW_LOCK_UNLOCKED;
1955 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1956 + set->binding = IP_SET_INVALID_ID;
1957 + atomic_set(&set->ref, 0);
1960 + * Next, take the &ip_set_lock, check that we know the type,
1961 + * and take a reference on the type, to make sure it
1962 + * stays available while constructing our new set.
1964 + * After referencing the type, we drop the &ip_set_lock,
1965 + * and let the new set construction run without locks.
1967 + set->type = find_set_type_rlock(typename);
1968 + if (set->type == NULL) {
1969 + /* Try loading the module */
1970 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1971 + strcpy(modulename, "ip_set_");
1972 + strcat(modulename, typename);
1973 + DP("try to load %s", modulename);
1974 + request_module(modulename);
1975 + set->type = find_set_type_rlock(typename);
1977 + if (set->type == NULL) {
1978 + ip_set_printk("no set type '%s', set '%s' not created",
1983 + if (!try_module_get(set->type->me)) {
1984 + read_unlock_bh(&ip_set_lock);
1988 + read_unlock_bh(&ip_set_lock);
1991 + * Without holding any locks, create private part.
1993 + res = set->type->create(set, data, size);
1997 + /* BTW, res==0 here. */
2000 + * Here, we have a valid, constructed set. &ip_set_lock again,
2001 + * find free id/index and check that it is not already in
2004 + write_lock_bh(&ip_set_lock);
2005 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
2006 + DP("no free id!");
2010 + /* Make sure restore gets the same index */
2011 + if (restore != IP_SET_INVALID_ID && index != restore) {
2012 + DP("Can't restore, sets are screwed up");
2018 + * Finally! Add our shiny new set to the list, and be done.
2020 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2022 + ip_set_list[index] = set;
2023 + write_unlock_bh(&ip_set_lock);
2027 + write_unlock_bh(&ip_set_lock);
2028 + set->type->destroy(set);
2030 + module_put(set->type->me);
2037 + * Destroy a given existing set
2040 +ip_set_destroy_set(ip_set_id_t index)
2042 + struct ip_set *set = ip_set_list[index];
2044 + IP_SET_ASSERT(set);
2045 + DP("set: %s", set->name);
2046 + write_lock_bh(&ip_set_lock);
2047 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2048 + if (set->binding != IP_SET_INVALID_ID)
2049 + __ip_set_put(set->binding);
2050 + ip_set_list[index] = NULL;
2051 + write_unlock_bh(&ip_set_lock);
2053 + /* Must call it without holding any lock */
2054 + set->type->destroy(set);
2055 + module_put(set->type->me);
2060 + * Destroy a set - or all sets
2061 + * Sets must not be referenced/used.
2064 +ip_set_destroy(ip_set_id_t index)
2068 + /* ref modification always protected by the mutex */
2069 + if (index != IP_SET_INVALID_ID) {
2070 + if (atomic_read(&ip_set_list[index]->ref))
2072 + ip_set_destroy_set(index);
2074 + for (i = 0; i < ip_set_max; i++) {
2075 + if (ip_set_list[i] != NULL
2076 + && (atomic_read(&ip_set_list[i]->ref)))
2080 + for (i = 0; i < ip_set_max; i++) {
2081 + if (ip_set_list[i] != NULL)
2082 + ip_set_destroy_set(i);
2089 +ip_set_flush_set(struct ip_set *set)
2091 + DP("set: %s %u", set->name, set->id);
2093 + write_lock_bh(&set->lock);
2094 + set->type->flush(set);
2095 + write_unlock_bh(&set->lock);
2099 + * Flush data in a set - or in all sets
2102 +ip_set_flush(ip_set_id_t index)
2104 + if (index != IP_SET_INVALID_ID) {
2105 + IP_SET_ASSERT(ip_set_list[index]);
2106 + ip_set_flush_set(ip_set_list[index]);
2108 + FOREACH_SET_DO(ip_set_flush_set);
2115 +ip_set_rename(ip_set_id_t index, const char *name)
2117 + struct ip_set *set = ip_set_list[index];
2121 + DP("set: %s to %s", set->name, name);
2122 + write_lock_bh(&ip_set_lock);
2123 + for (i = 0; i < ip_set_max; i++) {
2124 + if (ip_set_list[i] != NULL
2125 + && strncmp(ip_set_list[i]->name,
2127 + IP_SET_MAXNAMELEN - 1) == 0) {
2132 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2134 + write_unlock_bh(&ip_set_lock);
2139 + * Swap two sets so that name/index points to the other.
2140 + * References are also swapped.
2143 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2145 + struct ip_set *from = ip_set_list[from_index];
2146 + struct ip_set *to = ip_set_list[to_index];
2147 + char from_name[IP_SET_MAXNAMELEN];
2148 + u_int32_t from_ref;
2150 + DP("set: %s to %s", from->name, to->name);
2151 + /* Features must not change. Artifical restriction. */
2152 + if (from->type->features != to->type->features)
2155 + /* No magic here: ref munging protected by the mutex */
2156 + write_lock_bh(&ip_set_lock);
2157 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2158 + from_ref = atomic_read(&from->ref);
2160 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2161 + atomic_set(&from->ref, atomic_read(&to->ref));
2162 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2163 + atomic_set(&to->ref, from_ref);
2165 + ip_set_list[from_index] = to;
2166 + ip_set_list[to_index] = from;
2168 + write_unlock_bh(&ip_set_lock);
2177 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2178 + ip_set_id_t id, size_t *size)
2180 + if (set_hash->id == id)
2181 + *size += sizeof(struct ip_set_hash_list);
2185 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2186 + ip_set_id_t id, size_t *size)
2188 + if (set_hash->id == id)
2189 + *size += sizeof(struct ip_set_hash_save);
2193 +__set_hash_bindings(struct ip_set_hash *set_hash,
2194 + ip_set_id_t id, void *data, int *used)
2196 + if (set_hash->id == id) {
2197 + struct ip_set_hash_list *hash_list =
2198 + (struct ip_set_hash_list *)(data + *used);
2200 + hash_list->ip = set_hash->ip;
2201 + hash_list->binding = set_hash->binding;
2202 + *used += sizeof(struct ip_set_hash_list);
2206 +static int ip_set_list_set(ip_set_id_t index,
2211 + struct ip_set *set = ip_set_list[index];
2212 + struct ip_set_list *set_list;
2214 + /* Pointer to our header */
2215 + set_list = (struct ip_set_list *) (data + *used);
2217 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2219 + /* Get and ensure header size */
2220 + if (*used + sizeof(struct ip_set_list) > len)
2221 + goto not_enough_mem;
2222 + *used += sizeof(struct ip_set_list);
2224 + read_lock_bh(&set->lock);
2225 + /* Get and ensure set specific header size */
2226 + set_list->header_size = set->type->header_size;
2227 + if (*used + set_list->header_size > len)
2230 + /* Fill in the header */
2231 + set_list->index = index;
2232 + set_list->binding = set->binding;
2233 + set_list->ref = atomic_read(&set->ref);
2235 + /* Fill in set spefific header data */
2236 + set->type->list_header(set, data + *used);
2237 + *used += set_list->header_size;
2239 + /* Get and ensure set specific members size */
2240 + set_list->members_size = set->type->list_members_size(set);
2241 + if (*used + set_list->members_size > len)
2244 + /* Fill in set spefific members data */
2245 + set->type->list_members(set, data + *used);
2246 + *used += set_list->members_size;
2247 + read_unlock_bh(&set->lock);
2251 + /* Get and ensure set specific bindings size */
2252 + set_list->bindings_size = 0;
2253 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2254 + set->id, &set_list->bindings_size);
2255 + if (*used + set_list->bindings_size > len)
2256 + goto not_enough_mem;
2258 + /* Fill in set spefific bindings data */
2259 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2264 + read_unlock_bh(&set->lock);
2266 + DP("not enough mem, try again");
2273 +static int ip_set_save_set(ip_set_id_t index,
2278 + struct ip_set *set;
2279 + struct ip_set_save *set_save;
2281 + /* Pointer to our header */
2282 + set_save = (struct ip_set_save *) (data + *used);
2284 + /* Get and ensure header size */
2285 + if (*used + sizeof(struct ip_set_save) > len)
2286 + goto not_enough_mem;
2287 + *used += sizeof(struct ip_set_save);
2289 + set = ip_set_list[index];
2290 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2291 + data, data + *used);
2293 + read_lock_bh(&set->lock);
2294 + /* Get and ensure set specific header size */
2295 + set_save->header_size = set->type->header_size;
2296 + if (*used + set_save->header_size > len)
2299 + /* Fill in the header */
2300 + set_save->index = index;
2301 + set_save->binding = set->binding;
2303 + /* Fill in set spefific header data */
2304 + set->type->list_header(set, data + *used);
2305 + *used += set_save->header_size;
2307 + DP("set header filled: %s, used: %u(%u) %p %p", set->name, *used,
2308 + set_save->header_size, data, data + *used);
2309 + /* Get and ensure set specific members size */
2310 + set_save->members_size = set->type->list_members_size(set);
2311 + if (*used + set_save->members_size > len)
2314 + /* Fill in set spefific members data */
2315 + set->type->list_members(set, data + *used);
2316 + *used += set_save->members_size;
2317 + read_unlock_bh(&set->lock);
2318 + DP("set members filled: %s, used: %u(%u) %p %p", set->name, *used,
2319 + set_save->members_size, data, data + *used);
2323 + read_unlock_bh(&set->lock);
2325 + DP("not enough mem, try again");
2330 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2338 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2339 + struct ip_set_hash_save *hash_save =
2340 + (struct ip_set_hash_save *)(data + *used);
2341 + /* Ensure bindings size */
2342 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2346 + hash_save->id = set_hash->id;
2347 + hash_save->ip = set_hash->ip;
2348 + hash_save->binding = set_hash->binding;
2349 + *used += sizeof(struct ip_set_hash_save);
2353 +static int ip_set_save_bindings(ip_set_id_t index,
2359 + struct ip_set_save *set_save;
2361 + DP("used %u, len %u", *used, len);
2362 + /* Get and ensure header size */
2363 + if (*used + sizeof(struct ip_set_save) > len)
2367 + set_save = (struct ip_set_save *) (data + *used);
2368 + set_save->index = IP_SET_INVALID_ID;
2369 + set_save->header_size = 0;
2370 + set_save->members_size = 0;
2371 + *used += sizeof(struct ip_set_save);
2373 + DP("marker added used %u, len %u", *used, len);
2374 + /* Fill in bindings data */
2375 + if (index != IP_SET_INVALID_ID)
2376 + /* Sets are identified by id in hash */
2377 + index = ip_set_list[index]->id;
2378 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2386 +static int ip_set_restore(void *data,
2390 + int line = 0, used = 0, members_size;
2391 + struct ip_set *set;
2392 + struct ip_set_hash_save *hash_save;
2393 + struct ip_set_restore *set_restore;
2394 + ip_set_id_t index;
2396 + /* Loop to restore sets */
2400 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2401 + /* Get and ensure header size */
2402 + if (used + sizeof(struct ip_set_restore) > len)
2404 + set_restore = (struct ip_set_restore *) (data + used);
2405 + used += sizeof(struct ip_set_restore);
2407 + /* Ensure data size */
2409 + + set_restore->header_size
2410 + + set_restore->members_size > len)
2413 + /* Check marker */
2414 + if (set_restore->index == IP_SET_INVALID_ID) {
2419 + /* Try to create the set */
2420 + DP("restore %s %s", set_restore->name, set_restore->typename);
2421 + res = ip_set_create(set_restore->name,
2422 + set_restore->typename,
2423 + set_restore->index,
2425 + set_restore->header_size);
2429 + used += set_restore->header_size;
2431 + index = ip_set_find_byindex(set_restore->index);
2432 + DP("index %u, restore_index %u", index, set_restore->index);
2433 + if (index != set_restore->index)
2435 + /* Try to restore members data */
2436 + set = ip_set_list[index];
2438 + DP("members_size %u reqsize %u",
2439 + set_restore->members_size, set->type->reqsize);
2440 + while (members_size + set->type->reqsize <=
2441 + set_restore->members_size) {
2443 + DP("members: %u, line %u", members_size, line);
2444 + res = __ip_set_addip(index,
2445 + data + used + members_size,
2446 + set->type->reqsize);
2447 + if (!(res == 0 || res == -EEXIST))
2449 + members_size += set->type->reqsize;
2452 + DP("members_size %u %u",
2453 + set_restore->members_size, members_size);
2454 + if (members_size != set_restore->members_size)
2456 + used += set_restore->members_size;
2460 + /* Loop to restore bindings */
2461 + while (used < len) {
2464 + DP("restore binding, line %u", line);
2465 + /* Get and ensure size */
2466 + if (used + sizeof(struct ip_set_hash_save) > len)
2468 + hash_save = (struct ip_set_hash_save *) (data + used);
2469 + used += sizeof(struct ip_set_hash_save);
2471 + /* hash_save->id is used to store the index */
2472 + index = ip_set_find_byindex(hash_save->id);
2473 + DP("restore binding index %u, id %u, %u -> %u",
2474 + index, hash_save->id, hash_save->ip, hash_save->binding);
2475 + if (index != hash_save->id)
2477 + if (ip_set_find_byindex(hash_save->binding) == IP_SET_INVALID_ID) {
2478 + DP("corrupt binding set index %u", hash_save->binding);
2481 + set = ip_set_list[hash_save->id];
2482 + /* Null valued IP means default binding */
2483 + if (hash_save->ip)
2484 + res = ip_set_hash_add(set->id,
2486 + hash_save->binding);
2488 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2489 + write_lock_bh(&ip_set_lock);
2490 + set->binding = hash_save->binding;
2491 + __ip_set_get(set->binding);
2492 + write_unlock_bh(&ip_set_lock);
2493 + DP("default binding: %u", set->binding);
2505 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2508 + int res = 0; /* Assume OK */
2510 + struct ip_set_req_adt *req_adt;
2511 + ip_set_id_t index = IP_SET_INVALID_ID;
2512 + int (*adtfn)(ip_set_id_t index,
2513 + const void *data, size_t size);
2515 + int (*fn)(ip_set_id_t index,
2516 + const void *data, size_t size);
2518 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2519 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2522 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2523 + if (!capable(CAP_NET_ADMIN))
2525 + if (optval != SO_IP_SET)
2527 + if (len <= sizeof(unsigned)) {
2528 + ip_set_printk("short userdata (want >%zu, got %u)",
2529 + sizeof(unsigned), len);
2532 + data = vmalloc(len);
2534 + DP("out of mem for %u bytes", len);
2537 + if (copy_from_user(data, user, len) != 0) {
2541 + if (down_interruptible(&ip_set_app_mutex)) {
2546 + op = (unsigned *)data;
2549 + if (*op < IP_SET_OP_VERSION) {
2550 + /* Check the version at the beginning of operations */
2551 + struct ip_set_req_version *req_version =
2552 + (struct ip_set_req_version *) data;
2553 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2560 + case IP_SET_OP_CREATE:{
2561 + struct ip_set_req_create *req_create
2562 + = (struct ip_set_req_create *) data;
2564 + if (len < sizeof(struct ip_set_req_create)) {
2565 + ip_set_printk("short CREATE data (want >=%zu, got %u)",
2566 + sizeof(struct ip_set_req_create), len);
2570 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2571 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2572 + res = ip_set_create(req_create->name,
2573 + req_create->typename,
2574 + IP_SET_INVALID_ID,
2575 + data + sizeof(struct ip_set_req_create),
2576 + len - sizeof(struct ip_set_req_create));
2579 + case IP_SET_OP_DESTROY:{
2580 + struct ip_set_req_std *req_destroy
2581 + = (struct ip_set_req_std *) data;
2583 + if (len != sizeof(struct ip_set_req_std)) {
2584 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2585 + sizeof(struct ip_set_req_std), len);
2589 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2590 + /* Destroy all sets */
2591 + index = IP_SET_INVALID_ID;
2593 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2594 + index = ip_set_find_byname(req_destroy->name);
2596 + if (index == IP_SET_INVALID_ID) {
2602 + res = ip_set_destroy(index);
2605 + case IP_SET_OP_FLUSH:{
2606 + struct ip_set_req_std *req_flush =
2607 + (struct ip_set_req_std *) data;
2609 + if (len != sizeof(struct ip_set_req_std)) {
2610 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2611 + sizeof(struct ip_set_req_std), len);
2615 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2616 + /* Flush all sets */
2617 + index = IP_SET_INVALID_ID;
2619 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2620 + index = ip_set_find_byname(req_flush->name);
2622 + if (index == IP_SET_INVALID_ID) {
2627 + res = ip_set_flush(index);
2630 + case IP_SET_OP_RENAME:{
2631 + struct ip_set_req_create *req_rename
2632 + = (struct ip_set_req_create *) data;
2634 + if (len != sizeof(struct ip_set_req_create)) {
2635 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2636 + sizeof(struct ip_set_req_create), len);
2641 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2642 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2644 + index = ip_set_find_byname(req_rename->name);
2645 + if (index == IP_SET_INVALID_ID) {
2649 + res = ip_set_rename(index, req_rename->typename);
2652 + case IP_SET_OP_SWAP:{
2653 + struct ip_set_req_create *req_swap
2654 + = (struct ip_set_req_create *) data;
2655 + ip_set_id_t to_index;
2657 + if (len != sizeof(struct ip_set_req_create)) {
2658 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2659 + sizeof(struct ip_set_req_create), len);
2664 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2665 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2667 + index = ip_set_find_byname(req_swap->name);
2668 + if (index == IP_SET_INVALID_ID) {
2672 + to_index = ip_set_find_byname(req_swap->typename);
2673 + if (to_index == IP_SET_INVALID_ID) {
2677 + res = ip_set_swap(index, to_index);
2681 + break; /* Set identified by id */
2684 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2685 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2689 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2691 + if (len < sizeof(struct ip_set_req_adt)) {
2692 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2693 + sizeof(struct ip_set_req_adt), len);
2697 + req_adt = (struct ip_set_req_adt *) data;
2699 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2700 + if (!(*op == IP_SET_OP_UNBIND_SET
2701 + && req_adt->index == IP_SET_INVALID_ID)) {
2702 + index = ip_set_find_byindex(req_adt->index);
2703 + if (index == IP_SET_INVALID_ID) {
2708 + res = adtfn(index, data, len);
2711 + up(&ip_set_app_mutex);
2715 + DP("final result %d", res);
2720 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2724 + ip_set_id_t index = IP_SET_INVALID_ID;
2726 + int copylen = *len;
2728 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2729 + if (!capable(CAP_NET_ADMIN))
2731 + if (optval != SO_IP_SET)
2733 + if (*len < sizeof(unsigned)) {
2734 + ip_set_printk("short userdata (want >=%zu, got %d)",
2735 + sizeof(unsigned), *len);
2738 + data = vmalloc(*len);
2740 + DP("out of mem for %d bytes", *len);
2743 + if (copy_from_user(data, user, *len) != 0) {
2747 + if (down_interruptible(&ip_set_app_mutex)) {
2752 + op = (unsigned *) data;
2755 + if (*op < IP_SET_OP_VERSION) {
2756 + /* Check the version at the beginning of operations */
2757 + struct ip_set_req_version *req_version =
2758 + (struct ip_set_req_version *) data;
2759 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2766 + case IP_SET_OP_VERSION: {
2767 + struct ip_set_req_version *req_version =
2768 + (struct ip_set_req_version *) data;
2770 + if (*len != sizeof(struct ip_set_req_version)) {
2771 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2772 + sizeof(struct ip_set_req_version),
2778 + req_version->version = IP_SET_PROTOCOL_VERSION;
2779 + res = copy_to_user(user, req_version,
2780 + sizeof(struct ip_set_req_version));
2783 + case IP_SET_OP_GET_BYNAME: {
2784 + struct ip_set_req_get_set *req_get
2785 + = (struct ip_set_req_get_set *) data;
2787 + if (*len != sizeof(struct ip_set_req_get_set)) {
2788 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2789 + sizeof(struct ip_set_req_get_set), *len);
2793 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2794 + index = ip_set_find_byname(req_get->set.name);
2795 + req_get->set.index = index;
2798 + case IP_SET_OP_GET_BYINDEX: {
2799 + struct ip_set_req_get_set *req_get
2800 + = (struct ip_set_req_get_set *) data;
2802 + if (*len != sizeof(struct ip_set_req_get_set)) {
2803 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2804 + sizeof(struct ip_set_req_get_set), *len);
2808 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2809 + index = ip_set_find_byindex(req_get->set.index);
2810 + strncpy(req_get->set.name,
2811 + index == IP_SET_INVALID_ID ? ""
2812 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2815 + case IP_SET_OP_ADT_GET: {
2816 + struct ip_set_req_adt_get *req_get
2817 + = (struct ip_set_req_adt_get *) data;
2819 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2820 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2821 + sizeof(struct ip_set_req_adt_get), *len);
2825 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2826 + index = ip_set_find_byname(req_get->set.name);
2827 + if (index != IP_SET_INVALID_ID) {
2828 + req_get->set.index = index;
2829 + strncpy(req_get->typename,
2830 + ip_set_list[index]->type->typename,
2831 + IP_SET_MAXNAMELEN - 1);
2838 + case IP_SET_OP_MAX_SETS: {
2839 + struct ip_set_req_max_sets *req_max_sets
2840 + = (struct ip_set_req_max_sets *) data;
2843 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2844 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2845 + sizeof(struct ip_set_req_max_sets), *len);
2850 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2851 + req_max_sets->set.index = IP_SET_INVALID_ID;
2853 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2854 + req_max_sets->set.index =
2855 + ip_set_find_byname(req_max_sets->set.name);
2856 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2861 + req_max_sets->max_sets = ip_set_max;
2862 + req_max_sets->sets = 0;
2863 + for (i = 0; i < ip_set_max; i++) {
2864 + if (ip_set_list[i] != NULL)
2865 + req_max_sets->sets++;
2869 + case IP_SET_OP_LIST_SIZE:
2870 + case IP_SET_OP_SAVE_SIZE: {
2871 + struct ip_set_req_setnames *req_setnames
2872 + = (struct ip_set_req_setnames *) data;
2873 + struct ip_set_name_list *name_list;
2874 + struct ip_set *set;
2878 + if (*len < sizeof(struct ip_set_req_setnames)) {
2879 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2880 + sizeof(struct ip_set_req_setnames), *len);
2885 + req_setnames->size = 0;
2886 + used = sizeof(struct ip_set_req_setnames);
2887 + for (i = 0; i < ip_set_max; i++) {
2888 + if (ip_set_list[i] == NULL)
2890 + name_list = (struct ip_set_name_list *)
2892 + used += sizeof(struct ip_set_name_list);
2893 + if (used > copylen) {
2897 + set = ip_set_list[i];
2898 + /* Fill in index, name, etc. */
2899 + name_list->index = i;
2900 + name_list->id = set->id;
2901 + strncpy(name_list->name,
2903 + IP_SET_MAXNAMELEN - 1);
2904 + strncpy(name_list->typename,
2905 + set->type->typename,
2906 + IP_SET_MAXNAMELEN - 1);
2907 + DP("filled %s of type %s, index %u\n",
2908 + name_list->name, name_list->typename,
2909 + name_list->index);
2910 + if (!(req_setnames->index == IP_SET_INVALID_ID
2911 + || req_setnames->index == i))
2915 + case IP_SET_OP_LIST_SIZE: {
2916 + req_setnames->size += sizeof(struct ip_set_list)
2917 + + set->type->header_size
2918 + + set->type->list_members_size(set);
2919 + /* Sets are identified by id in the hash */
2920 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2921 + set->id, &req_setnames->size);
2924 + case IP_SET_OP_SAVE_SIZE: {
2925 + req_setnames->size += sizeof(struct ip_set_save)
2926 + + set->type->header_size
2927 + + set->type->list_members_size(set);
2928 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2929 + set->id, &req_setnames->size);
2936 + if (copylen != used) {
2942 + case IP_SET_OP_LIST: {
2943 + struct ip_set_req_list *req_list
2944 + = (struct ip_set_req_list *) data;
2948 + if (*len < sizeof(struct ip_set_req_list)) {
2949 + ip_set_printk("short LIST (want >=%zu, got %d)",
2950 + sizeof(struct ip_set_req_list), *len);
2954 + index = req_list->index;
2955 + if (index != IP_SET_INVALID_ID
2956 + && ip_set_find_byindex(index) != index) {
2961 + if (index == IP_SET_INVALID_ID) {
2962 + /* List all sets */
2963 + for (i = 0; i < ip_set_max && res == 0; i++) {
2964 + if (ip_set_list[i] != NULL)
2965 + res = ip_set_list_set(i, data, &used, *len);
2968 + /* List an individual set */
2969 + res = ip_set_list_set(index, data, &used, *len);
2973 + else if (copylen != used) {
2979 + case IP_SET_OP_SAVE: {
2980 + struct ip_set_req_list *req_save
2981 + = (struct ip_set_req_list *) data;
2985 + if (*len < sizeof(struct ip_set_req_list)) {
2986 + ip_set_printk("short SAVE (want >=%zu, got %d)",
2987 + sizeof(struct ip_set_req_list), *len);
2991 + index = req_save->index;
2992 + if (index != IP_SET_INVALID_ID
2993 + && ip_set_find_byindex(index) != index) {
2998 + if (index == IP_SET_INVALID_ID) {
2999 + /* Save all sets */
3000 + for (i = 0; i < ip_set_max && res == 0; i++) {
3001 + if (ip_set_list[i] != NULL)
3002 + res = ip_set_save_set(i, data, &used, *len);
3005 + /* Save an individual set */
3006 + res = ip_set_save_set(index, data, &used, *len);
3009 + res = ip_set_save_bindings(index, data, &used, *len);
3013 + else if (copylen != used) {
3019 + case IP_SET_OP_RESTORE: {
3020 + struct ip_set_req_setnames *req_restore
3021 + = (struct ip_set_req_setnames *) data;
3024 + if (*len < sizeof(struct ip_set_req_setnames)
3025 + || *len != req_restore->size) {
3026 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3027 + req_restore->size, *len);
3031 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3032 + req_restore->size - sizeof(struct ip_set_req_setnames));
3033 + DP("ip_set_restore: %u", line);
3036 + req_restore->size = line;
3037 + copylen = sizeof(struct ip_set_req_setnames);
3045 + } /* end of switch(op) */
3048 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3049 + && ip_set_list[index]
3050 + ? ip_set_list[index]->name
3051 + : ":all:", copylen);
3052 + res = copy_to_user(user, data, copylen);
3055 + up(&ip_set_app_mutex);
3059 + DP("final result %d", res);
3063 +static struct nf_sockopt_ops so_set = {
3065 + .set_optmin = SO_IP_SET,
3066 + .set_optmax = SO_IP_SET + 1,
3067 + .set = &ip_set_sockfn_set,
3068 + .get_optmin = SO_IP_SET,
3069 + .get_optmax = SO_IP_SET + 1,
3070 + .get = &ip_set_sockfn_get,
3071 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
3072 + .owner = THIS_MODULE,
3076 +static int max_sets, hash_size;
3077 +module_param(max_sets, int, 0600);
3078 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3079 +module_param(hash_size, int, 0600);
3080 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3081 +MODULE_LICENSE("GPL");
3082 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3083 +MODULE_DESCRIPTION("module implementing core IP set support");
3085 +static int __init ip_set_init(void)
3090 + get_random_bytes(&ip_set_hash_random, 4);
3092 + ip_set_max = max_sets;
3093 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3094 + if (!ip_set_list) {
3095 + printk(KERN_ERR "Unable to create ip_set_list\n");
3098 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3100 + ip_set_bindings_hash_size = hash_size;
3101 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3102 + if (!ip_set_hash) {
3103 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3104 + vfree(ip_set_list);
3107 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3108 + INIT_LIST_HEAD(&ip_set_hash[i]);
3110 + INIT_LIST_HEAD(&set_type_list);
3112 + res = nf_register_sockopt(&so_set);
3114 + ip_set_printk("SO_SET registry failed: %d", res);
3115 + vfree(ip_set_list);
3116 + vfree(ip_set_hash);
3122 +static void __exit ip_set_fini(void)
3124 + /* There can't be any existing set or binding */
3125 + nf_unregister_sockopt(&so_set);
3126 + vfree(ip_set_list);
3127 + vfree(ip_set_hash);
3128 + DP("these are the famous last words");
3131 +EXPORT_SYMBOL(ip_set_register_set_type);
3132 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3134 +EXPORT_SYMBOL(ip_set_get_byname);
3135 +EXPORT_SYMBOL(ip_set_get_byindex);
3136 +EXPORT_SYMBOL(ip_set_put);
3138 +EXPORT_SYMBOL(ip_set_addip_kernel);
3139 +EXPORT_SYMBOL(ip_set_delip_kernel);
3140 +EXPORT_SYMBOL(ip_set_testip_kernel);
3142 +module_init(ip_set_init);
3143 +module_exit(ip_set_fini);
3145 +++ b/net/ipv4/netfilter/ip_set_iphash.c
3147 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3149 + * This program is free software; you can redistribute it and/or modify
3150 + * it under the terms of the GNU General Public License version 2 as
3151 + * published by the Free Software Foundation.
3154 +/* Kernel module implementing an ip hash set */
3156 +#include <linux/module.h>
3157 +#include <linux/ip.h>
3158 +#include <linux/skbuff.h>
3159 +#include <linux/version.h>
3160 +#include <linux/jhash.h>
3161 +#include <linux/netfilter_ipv4/ip_tables.h>
3162 +#include <linux/netfilter_ipv4/ip_set.h>
3163 +#include <linux/errno.h>
3164 +#include <asm/uaccess.h>
3165 +#include <asm/bitops.h>
3166 +#include <linux/spinlock.h>
3167 +#include <linux/vmalloc.h>
3168 +#include <linux/random.h>
3170 +#include <net/ip.h>
3172 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3173 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3175 +static int limit = MAX_RANGE;
3177 +static inline __u32
3178 +jhash_ip(const struct ip_set_iphash *map, uint16_t i, ip_set_ip_t ip)
3180 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
3183 +static inline __u32
3184 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3186 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3189 + ip_set_ip_t *elem;
3191 + *hash_ip = ip & map->netmask;
3192 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3193 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3195 + for (i = 0; i < map->probes; i++) {
3196 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
3197 + DP("hash key: %u", id);
3198 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3199 + if (*elem == *hash_ip)
3201 + /* No shortcut at testing - there can be deleted
3208 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3210 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
3214 +testip(struct ip_set *set, const void *data, size_t size,
3215 + ip_set_ip_t *hash_ip)
3217 + struct ip_set_req_iphash *req =
3218 + (struct ip_set_req_iphash *) data;
3220 + if (size != sizeof(struct ip_set_req_iphash)) {
3221 + ip_set_printk("data length wrong (want %zu, have %zu)",
3222 + sizeof(struct ip_set_req_iphash),
3226 + return __testip(set, req->ip, hash_ip);
3230 +testip_kernel(struct ip_set *set,
3231 + const struct sk_buff *skb,
3232 + ip_set_ip_t *hash_ip,
3233 + const u_int32_t *flags,
3234 + unsigned char index)
3236 + return __testip(set,
3237 + ntohl(flags[index] & IPSET_SRC
3238 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3239 + ? ip_hdr(skb)->saddr
3240 + : ip_hdr(skb)->daddr),
3242 + ? skb->nh.iph->saddr
3243 + : skb->nh.iph->daddr),
3249 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3253 + ip_set_ip_t *elem;
3255 + if (!ip || map->elements >= limit)
3258 + *hash_ip = ip & map->netmask;
3260 + for (i = 0; i < map->probes; i++) {
3261 + probe = jhash_ip(map, i, *hash_ip) % map->hashsize;
3262 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
3263 + if (*elem == *hash_ip)
3271 + /* Trigger rehashing */
3276 +addip(struct ip_set *set, const void *data, size_t size,
3277 + ip_set_ip_t *hash_ip)
3279 + struct ip_set_req_iphash *req =
3280 + (struct ip_set_req_iphash *) data;
3282 + if (size != sizeof(struct ip_set_req_iphash)) {
3283 + ip_set_printk("data length wrong (want %zu, have %zu)",
3284 + sizeof(struct ip_set_req_iphash),
3288 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3292 +addip_kernel(struct ip_set *set,
3293 + const struct sk_buff *skb,
3294 + ip_set_ip_t *hash_ip,
3295 + const u_int32_t *flags,
3296 + unsigned char index)
3298 + return __addip((struct ip_set_iphash *) set->data,
3299 + ntohl(flags[index] & IPSET_SRC
3300 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3301 + ? ip_hdr(skb)->saddr
3302 + : ip_hdr(skb)->daddr),
3304 + ? skb->nh.iph->saddr
3305 + : skb->nh.iph->daddr),
3310 +static int retry(struct ip_set *set)
3312 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3313 + ip_set_ip_t hash_ip, *elem;
3315 + u_int32_t i, hashsize = map->hashsize;
3317 + struct ip_set_iphash *tmp;
3319 + if (map->resize == 0)
3325 + /* Calculate new hash size */
3326 + hashsize += (hashsize * map->resize)/100;
3327 + if (hashsize == map->hashsize)
3330 + ip_set_printk("rehashing of set %s triggered: "
3331 + "hashsize grows from %u to %u",
3332 + set->name, map->hashsize, hashsize);
3334 + tmp = kmalloc(sizeof(struct ip_set_iphash)
3335 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
3337 + DP("out of memory for %d bytes",
3338 + sizeof(struct ip_set_iphash)
3339 + + map->probes * sizeof(uint32_t));
3342 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
3343 + if (!tmp->members) {
3344 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
3348 + tmp->hashsize = hashsize;
3349 + tmp->elements = 0;
3350 + tmp->probes = map->probes;
3351 + tmp->resize = map->resize;
3352 + tmp->netmask = map->netmask;
3353 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
3355 + write_lock_bh(&set->lock);
3356 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3357 + for (i = 0; i < map->hashsize && res == 0; i++) {
3358 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3360 + res = __addip(tmp, *elem, &hash_ip);
3363 + /* Failure, try again */
3364 + write_unlock_bh(&set->lock);
3365 + harray_free(tmp->members);
3370 + /* Success at resizing! */
3371 + members = map->members;
3373 + map->hashsize = tmp->hashsize;
3374 + map->members = tmp->members;
3375 + write_unlock_bh(&set->lock);
3377 + harray_free(members);
3384 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3386 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3387 + ip_set_ip_t id, *elem;
3392 + id = hash_id(set, ip, hash_ip);
3393 + if (id == UINT_MAX)
3396 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3404 +delip(struct ip_set *set, const void *data, size_t size,
3405 + ip_set_ip_t *hash_ip)
3407 + struct ip_set_req_iphash *req =
3408 + (struct ip_set_req_iphash *) data;
3410 + if (size != sizeof(struct ip_set_req_iphash)) {
3411 + ip_set_printk("data length wrong (want %zu, have %zu)",
3412 + sizeof(struct ip_set_req_iphash),
3416 + return __delip(set, req->ip, hash_ip);
3420 +delip_kernel(struct ip_set *set,
3421 + const struct sk_buff *skb,
3422 + ip_set_ip_t *hash_ip,
3423 + const u_int32_t *flags,
3424 + unsigned char index)
3426 + return __delip(set,
3427 + ntohl(flags[index] & IPSET_SRC
3428 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3429 + ? ip_hdr(skb)->saddr
3430 + : ip_hdr(skb)->daddr),
3432 + ? skb->nh.iph->saddr
3433 + : skb->nh.iph->daddr),
3438 +static int create(struct ip_set *set, const void *data, size_t size)
3440 + struct ip_set_req_iphash_create *req =
3441 + (struct ip_set_req_iphash_create *) data;
3442 + struct ip_set_iphash *map;
3445 + if (size != sizeof(struct ip_set_req_iphash_create)) {
3446 + ip_set_printk("data length wrong (want %zu, have %zu)",
3447 + sizeof(struct ip_set_req_iphash_create),
3452 + if (req->hashsize < 1) {
3453 + ip_set_printk("hashsize too small");
3457 + if (req->probes < 1) {
3458 + ip_set_printk("probes too small");
3462 + map = kmalloc(sizeof(struct ip_set_iphash)
3463 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
3465 + DP("out of memory for %d bytes",
3466 + sizeof(struct ip_set_iphash)
3467 + + req->probes * sizeof(uint32_t));
3470 + for (i = 0; i < req->probes; i++)
3471 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
3472 + map->elements = 0;
3473 + map->hashsize = req->hashsize;
3474 + map->probes = req->probes;
3475 + map->resize = req->resize;
3476 + map->netmask = req->netmask;
3477 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
3478 + if (!map->members) {
3479 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
3488 +static void destroy(struct ip_set *set)
3490 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3492 + harray_free(map->members);
3498 +static void flush(struct ip_set *set)
3500 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3501 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
3502 + map->elements = 0;
3505 +static void list_header(const struct ip_set *set, void *data)
3507 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3508 + struct ip_set_req_iphash_create *header =
3509 + (struct ip_set_req_iphash_create *) data;
3511 + header->hashsize = map->hashsize;
3512 + header->probes = map->probes;
3513 + header->resize = map->resize;
3514 + header->netmask = map->netmask;
3517 +static int list_members_size(const struct ip_set *set)
3519 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3521 + return (map->hashsize * sizeof(ip_set_ip_t));
3524 +static void list_members(const struct ip_set *set, void *data)
3526 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3527 + ip_set_ip_t i, *elem;
3529 + for (i = 0; i < map->hashsize; i++) {
3530 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3531 + ((ip_set_ip_t *)data)[i] = *elem;
3535 +static struct ip_set_type ip_set_iphash = {
3536 + .typename = SETTYPE_NAME,
3537 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
3538 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3539 + .create = &create,
3540 + .destroy = &destroy,
3542 + .reqsize = sizeof(struct ip_set_req_iphash),
3544 + .addip_kernel = &addip_kernel,
3547 + .delip_kernel = &delip_kernel,
3548 + .testip = &testip,
3549 + .testip_kernel = &testip_kernel,
3550 + .header_size = sizeof(struct ip_set_req_iphash_create),
3551 + .list_header = &list_header,
3552 + .list_members_size = &list_members_size,
3553 + .list_members = &list_members,
3554 + .me = THIS_MODULE,
3557 +MODULE_LICENSE("GPL");
3558 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3559 +MODULE_DESCRIPTION("iphash type of IP sets");
3560 +module_param(limit, int, 0600);
3561 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
3563 +static int __init ip_set_iphash_init(void)
3565 + return ip_set_register_set_type(&ip_set_iphash);
3568 +static void __exit ip_set_iphash_fini(void)
3570 + /* FIXME: possible race with ip_set_create() */
3571 + ip_set_unregister_set_type(&ip_set_iphash);
3574 +module_init(ip_set_iphash_init);
3575 +module_exit(ip_set_iphash_fini);
3577 +++ b/net/ipv4/netfilter/ip_set_ipmap.c
3579 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3580 + * Patrick Schaaf <bof@bof.de>
3581 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3583 + * This program is free software; you can redistribute it and/or modify
3584 + * it under the terms of the GNU General Public License version 2 as
3585 + * published by the Free Software Foundation.
3588 +/* Kernel module implementing an IP set type: the single bitmap type */
3590 +#include <linux/module.h>
3591 +#include <linux/ip.h>
3592 +#include <linux/skbuff.h>
3593 +#include <linux/version.h>
3594 +#include <linux/netfilter_ipv4/ip_tables.h>
3595 +#include <linux/netfilter_ipv4/ip_set.h>
3596 +#include <linux/errno.h>
3597 +#include <asm/uaccess.h>
3598 +#include <asm/bitops.h>
3599 +#include <linux/spinlock.h>
3601 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3603 +static inline ip_set_ip_t
3604 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3606 + return (ip - map->first_ip)/map->hosts;
3610 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3612 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3614 + if (ip < map->first_ip || ip > map->last_ip)
3617 + *hash_ip = ip & map->netmask;
3618 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3619 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3620 + return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3624 +testip(struct ip_set *set, const void *data, size_t size,
3625 + ip_set_ip_t *hash_ip)
3627 + struct ip_set_req_ipmap *req =
3628 + (struct ip_set_req_ipmap *) data;
3630 + if (size != sizeof(struct ip_set_req_ipmap)) {
3631 + ip_set_printk("data length wrong (want %zu, have %zu)",
3632 + sizeof(struct ip_set_req_ipmap),
3636 + return __testip(set, req->ip, hash_ip);
3640 +testip_kernel(struct ip_set *set,
3641 + const struct sk_buff *skb,
3642 + ip_set_ip_t *hash_ip,
3643 + const u_int32_t *flags,
3644 + unsigned char index)
3646 + int res = __testip(set,
3647 + ntohl(flags[index] & IPSET_SRC
3648 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3649 + ? ip_hdr(skb)->saddr
3650 + : ip_hdr(skb)->daddr),
3652 + ? skb->nh.iph->saddr
3653 + : skb->nh.iph->daddr),
3656 + return (res < 0 ? 0 : res);
3660 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3662 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3664 + if (ip < map->first_ip || ip > map->last_ip)
3667 + *hash_ip = ip & map->netmask;
3668 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3669 + if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3676 +addip(struct ip_set *set, const void *data, size_t size,
3677 + ip_set_ip_t *hash_ip)
3679 + struct ip_set_req_ipmap *req =
3680 + (struct ip_set_req_ipmap *) data;
3682 + if (size != sizeof(struct ip_set_req_ipmap)) {
3683 + ip_set_printk("data length wrong (want %zu, have %zu)",
3684 + sizeof(struct ip_set_req_ipmap),
3688 + DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3689 + return __addip(set, req->ip, hash_ip);
3693 +addip_kernel(struct ip_set *set,
3694 + const struct sk_buff *skb,
3695 + ip_set_ip_t *hash_ip,
3696 + const u_int32_t *flags,
3697 + unsigned char index)
3699 + return __addip(set,
3700 + ntohl(flags[index] & IPSET_SRC
3701 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3702 + ? ip_hdr(skb)->saddr
3703 + : ip_hdr(skb)->daddr),
3705 + ? skb->nh.iph->saddr
3706 + : skb->nh.iph->daddr),
3712 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3714 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3716 + if (ip < map->first_ip || ip > map->last_ip)
3719 + *hash_ip = ip & map->netmask;
3720 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3721 + if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3728 +delip(struct ip_set *set, const void *data, size_t size,
3729 + ip_set_ip_t *hash_ip)
3731 + struct ip_set_req_ipmap *req =
3732 + (struct ip_set_req_ipmap *) data;
3734 + if (size != sizeof(struct ip_set_req_ipmap)) {
3735 + ip_set_printk("data length wrong (want %zu, have %zu)",
3736 + sizeof(struct ip_set_req_ipmap),
3740 + return __delip(set, req->ip, hash_ip);
3744 +delip_kernel(struct ip_set *set,
3745 + const struct sk_buff *skb,
3746 + ip_set_ip_t *hash_ip,
3747 + const u_int32_t *flags,
3748 + unsigned char index)
3750 + return __delip(set,
3751 + ntohl(flags[index] & IPSET_SRC
3752 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3753 + ? ip_hdr(skb)->saddr
3754 + : ip_hdr(skb)->daddr),
3756 + ? skb->nh.iph->saddr
3757 + : skb->nh.iph->daddr),
3762 +static int create(struct ip_set *set, const void *data, size_t size)
3765 + struct ip_set_req_ipmap_create *req =
3766 + (struct ip_set_req_ipmap_create *) data;
3767 + struct ip_set_ipmap *map;
3769 + if (size != sizeof(struct ip_set_req_ipmap_create)) {
3770 + ip_set_printk("data length wrong (want %zu, have %zu)",
3771 + sizeof(struct ip_set_req_ipmap_create),
3776 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3777 + HIPQUAD(req->from), HIPQUAD(req->to));
3779 + if (req->from > req->to) {
3780 + DP("bad ip range");
3784 + map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3786 + DP("out of memory for %d bytes",
3787 + sizeof(struct ip_set_ipmap));
3790 + map->first_ip = req->from;
3791 + map->last_ip = req->to;
3792 + map->netmask = req->netmask;
3794 + if (req->netmask == 0xFFFFFFFF) {
3796 + map->sizeid = map->last_ip - map->first_ip + 1;
3798 + unsigned int mask_bits, netmask_bits;
3801 + map->first_ip &= map->netmask; /* Should we better bark? */
3803 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3804 + netmask_bits = mask_to_bits(map->netmask);
3806 + if ((!mask && (map->first_ip || map->last_ip != 0xFFFFFFFF))
3807 + || netmask_bits <= mask_bits)
3810 + DP("mask_bits %u, netmask_bits %u",
3811 + mask_bits, netmask_bits);
3812 + map->hosts = 2 << (32 - netmask_bits - 1);
3813 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3815 + if (map->sizeid > MAX_RANGE + 1) {
3816 + ip_set_printk("range too big (max %d addresses)",
3821 + DP("hosts %u, sizeid %u", map->hosts, map->sizeid);
3822 + newbytes = bitmap_bytes(0, map->sizeid - 1);
3823 + map->members = kmalloc(newbytes, GFP_KERNEL);
3824 + if (!map->members) {
3825 + DP("out of memory for %d bytes", newbytes);
3829 + memset(map->members, 0, newbytes);
3835 +static void destroy(struct ip_set *set)
3837 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3839 + kfree(map->members);
3845 +static void flush(struct ip_set *set)
3847 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3848 + memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3851 +static void list_header(const struct ip_set *set, void *data)
3853 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3854 + struct ip_set_req_ipmap_create *header =
3855 + (struct ip_set_req_ipmap_create *) data;
3857 + header->from = map->first_ip;
3858 + header->to = map->last_ip;
3859 + header->netmask = map->netmask;
3862 +static int list_members_size(const struct ip_set *set)
3864 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3866 + return bitmap_bytes(0, map->sizeid - 1);
3869 +static void list_members(const struct ip_set *set, void *data)
3871 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3872 + int bytes = bitmap_bytes(0, map->sizeid - 1);
3874 + memcpy(data, map->members, bytes);
3877 +static struct ip_set_type ip_set_ipmap = {
3878 + .typename = SETTYPE_NAME,
3879 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
3880 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3881 + .create = &create,
3882 + .destroy = &destroy,
3884 + .reqsize = sizeof(struct ip_set_req_ipmap),
3886 + .addip_kernel = &addip_kernel,
3888 + .delip_kernel = &delip_kernel,
3889 + .testip = &testip,
3890 + .testip_kernel = &testip_kernel,
3891 + .header_size = sizeof(struct ip_set_req_ipmap_create),
3892 + .list_header = &list_header,
3893 + .list_members_size = &list_members_size,
3894 + .list_members = &list_members,
3895 + .me = THIS_MODULE,
3898 +MODULE_LICENSE("GPL");
3899 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3900 +MODULE_DESCRIPTION("ipmap type of IP sets");
3902 +static int __init ip_set_ipmap_init(void)
3904 + return ip_set_register_set_type(&ip_set_ipmap);
3907 +static void __exit ip_set_ipmap_fini(void)
3909 + /* FIXME: possible race with ip_set_create() */
3910 + ip_set_unregister_set_type(&ip_set_ipmap);
3913 +module_init(ip_set_ipmap_init);
3914 +module_exit(ip_set_ipmap_fini);
3916 +++ b/net/ipv4/netfilter/ip_set_ipporthash.c
3918 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3920 + * This program is free software; you can redistribute it and/or modify
3921 + * it under the terms of the GNU General Public License version 2 as
3922 + * published by the Free Software Foundation.
3925 +/* Kernel module implementing an ip+port hash set */
3927 +#include <linux/module.h>
3928 +#include <linux/ip.h>
3929 +#include <linux/tcp.h>
3930 +#include <linux/udp.h>
3931 +#include <linux/skbuff.h>
3932 +#include <linux/version.h>
3933 +#include <linux/jhash.h>
3934 +#include <linux/netfilter_ipv4/ip_tables.h>
3935 +#include <linux/netfilter_ipv4/ip_set.h>
3936 +#include <linux/errno.h>
3937 +#include <asm/uaccess.h>
3938 +#include <asm/bitops.h>
3939 +#include <linux/spinlock.h>
3940 +#include <linux/vmalloc.h>
3941 +#include <linux/random.h>
3943 +#include <net/ip.h>
3945 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3946 +#include <linux/netfilter_ipv4/ip_set_ipporthash.h>
3948 +static int limit = MAX_RANGE;
3950 +/* We must handle non-linear skbs */
3951 +static inline ip_set_ip_t
3952 +get_port(const struct sk_buff *skb, u_int32_t flags)
3954 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3955 + struct iphdr *iph = ip_hdr(skb);
3957 + struct iphdr *iph = skb->nh.iph;
3959 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
3961 + switch (iph->protocol) {
3962 + case IPPROTO_TCP: {
3963 + struct tcphdr tcph;
3965 + /* See comments at tcp_match in ip_tables.c */
3967 + return INVALID_PORT;
3969 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3970 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
3972 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
3974 + /* No choice either */
3975 + return INVALID_PORT;
3977 + return ntohs(flags & IPSET_SRC ?
3978 + tcph.source : tcph.dest);
3980 + case IPPROTO_UDP: {
3981 + struct udphdr udph;
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, &udph, sizeof(udph)) < 0)
3989 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
3991 + /* No choice either */
3992 + return INVALID_PORT;
3994 + return ntohs(flags & IPSET_SRC ?
3995 + udph.source : udph.dest);
3998 + return INVALID_PORT;
4002 +static inline __u32
4003 +jhash_ip(const struct ip_set_ipporthash *map, uint16_t i, ip_set_ip_t ip)
4005 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
4008 +#define HASH_IP(map, ip, port) (port + ((ip - ((map)->first_ip)) << 16))
4010 +static inline __u32
4011 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4012 + ip_set_ip_t *hash_ip)
4014 + struct ip_set_ipporthash *map =
4015 + (struct ip_set_ipporthash *) set->data;
4018 + ip_set_ip_t *elem;
4020 + *hash_ip = HASH_IP(map, ip, port);
4021 + DP("set: %s, ipport:%u.%u.%u.%u:%u, %u.%u.%u.%u",
4022 + set->name, HIPQUAD(ip), port, HIPQUAD(*hash_ip));
4024 + for (i = 0; i < map->probes; i++) {
4025 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
4026 + DP("hash key: %u", id);
4027 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4028 + if (*elem == *hash_ip)
4030 + /* No shortcut at testing - there can be deleted
4037 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4038 + ip_set_ip_t *hash_ip)
4040 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4042 + if (ip < map->first_ip || ip > map->last_ip)
4045 + return (hash_id(set, ip, port, hash_ip) != UINT_MAX);
4049 +testip(struct ip_set *set, const void *data, size_t size,
4050 + ip_set_ip_t *hash_ip)
4052 + struct ip_set_req_ipporthash *req =
4053 + (struct ip_set_req_ipporthash *) data;
4055 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4056 + ip_set_printk("data length wrong (want %zu, have %zu)",
4057 + sizeof(struct ip_set_req_ipporthash),
4061 + return __testip(set, req->ip, req->port, hash_ip);
4065 +testip_kernel(struct ip_set *set,
4066 + const struct sk_buff *skb,
4067 + ip_set_ip_t *hash_ip,
4068 + const u_int32_t *flags,
4069 + unsigned char index)
4074 + if (flags[index+1] == 0)
4077 + port = get_port(skb, flags[index+1]);
4079 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4080 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4081 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4082 + NIPQUAD(ip_hdr(skb)->saddr),
4083 + NIPQUAD(ip_hdr(skb)->daddr));
4085 + NIPQUAD(skb->nh.iph->saddr),
4086 + NIPQUAD(skb->nh.iph->daddr));
4088 + DP("flag %s port %u",
4089 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4091 + if (port == INVALID_PORT)
4094 + res = __testip(set,
4095 + ntohl(flags[index] & IPSET_SRC
4096 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4097 + ? ip_hdr(skb)->saddr
4098 + : ip_hdr(skb)->daddr),
4100 + ? skb->nh.iph->saddr
4101 + : skb->nh.iph->daddr),
4105 + return (res < 0 ? 0 : res);
4110 +__add_haship(struct ip_set_ipporthash *map, ip_set_ip_t hash_ip)
4114 + ip_set_ip_t *elem;
4116 + for (i = 0; i < map->probes; i++) {
4117 + probe = jhash_ip(map, i, hash_ip) % map->hashsize;
4118 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
4119 + if (*elem == hash_ip)
4127 + /* Trigger rehashing */
4132 +__addip(struct ip_set_ipporthash *map, ip_set_ip_t ip, ip_set_ip_t port,
4133 + ip_set_ip_t *hash_ip)
4135 + if (map->elements > limit)
4137 + if (ip < map->first_ip || ip > map->last_ip)
4140 + *hash_ip = HASH_IP(map, ip, port);
4142 + return __add_haship(map, *hash_ip);
4146 +addip(struct ip_set *set, const void *data, size_t size,
4147 + ip_set_ip_t *hash_ip)
4149 + struct ip_set_req_ipporthash *req =
4150 + (struct ip_set_req_ipporthash *) data;
4152 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4153 + ip_set_printk("data length wrong (want %zu, have %zu)",
4154 + sizeof(struct ip_set_req_ipporthash),
4158 + return __addip((struct ip_set_ipporthash *) set->data,
4159 + req->ip, req->port, hash_ip);
4163 +addip_kernel(struct ip_set *set,
4164 + const struct sk_buff *skb,
4165 + ip_set_ip_t *hash_ip,
4166 + const u_int32_t *flags,
4167 + unsigned char index)
4171 + if (flags[index+1] == 0)
4174 + port = get_port(skb, flags[index+1]);
4176 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4177 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4178 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4179 + NIPQUAD(ip_hdr(skb)->saddr),
4180 + NIPQUAD(ip_hdr(skb)->daddr));
4182 + NIPQUAD(skb->nh.iph->saddr),
4183 + NIPQUAD(skb->nh.iph->daddr));
4185 + DP("flag %s port %u",
4186 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4188 + if (port == INVALID_PORT)
4191 + return __addip((struct ip_set_ipporthash *) set->data,
4192 + ntohl(flags[index] & IPSET_SRC
4193 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4194 + ? ip_hdr(skb)->saddr
4195 + : ip_hdr(skb)->daddr),
4197 + ? skb->nh.iph->saddr
4198 + : skb->nh.iph->daddr),
4204 +static int retry(struct ip_set *set)
4206 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4207 + ip_set_ip_t *elem;
4209 + u_int32_t i, hashsize = map->hashsize;
4211 + struct ip_set_ipporthash *tmp;
4213 + if (map->resize == 0)
4219 + /* Calculate new hash size */
4220 + hashsize += (hashsize * map->resize)/100;
4221 + if (hashsize == map->hashsize)
4224 + ip_set_printk("rehashing of set %s triggered: "
4225 + "hashsize grows from %u to %u",
4226 + set->name, map->hashsize, hashsize);
4228 + tmp = kmalloc(sizeof(struct ip_set_ipporthash)
4229 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
4231 + DP("out of memory for %d bytes",
4232 + sizeof(struct ip_set_ipporthash)
4233 + + map->probes * sizeof(uint32_t));
4236 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
4237 + if (!tmp->members) {
4238 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
4242 + tmp->hashsize = hashsize;
4243 + tmp->elements = 0;
4244 + tmp->probes = map->probes;
4245 + tmp->resize = map->resize;
4246 + tmp->first_ip = map->first_ip;
4247 + tmp->last_ip = map->last_ip;
4248 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
4250 + write_lock_bh(&set->lock);
4251 + map = (struct ip_set_ipporthash *) set->data; /* Play safe */
4252 + for (i = 0; i < map->hashsize && res == 0; i++) {
4253 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
4255 + res = __add_haship(tmp, *elem);
4258 + /* Failure, try again */
4259 + write_unlock_bh(&set->lock);
4260 + harray_free(tmp->members);
4265 + /* Success at resizing! */
4266 + members = map->members;
4268 + map->hashsize = tmp->hashsize;
4269 + map->members = tmp->members;
4270 + write_unlock_bh(&set->lock);
4272 + harray_free(members);
4279 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4280 + ip_set_ip_t *hash_ip)
4282 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4284 + ip_set_ip_t *elem;
4286 + if (ip < map->first_ip || ip > map->last_ip)
4289 + id = hash_id(set, ip, port, hash_ip);
4291 + if (id == UINT_MAX)
4294 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4302 +delip(struct ip_set *set, const void *data, size_t size,
4303 + ip_set_ip_t *hash_ip)
4305 + struct ip_set_req_ipporthash *req =
4306 + (struct ip_set_req_ipporthash *) data;
4308 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4309 + ip_set_printk("data length wrong (want %zu, have %zu)",
4310 + sizeof(struct ip_set_req_ipporthash),
4314 + return __delip(set, req->ip, req->port, hash_ip);
4318 +delip_kernel(struct ip_set *set,
4319 + const struct sk_buff *skb,
4320 + ip_set_ip_t *hash_ip,
4321 + const u_int32_t *flags,
4322 + unsigned char index)
4326 + if (flags[index+1] == 0)
4329 + port = get_port(skb, flags[index+1]);
4331 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4332 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4333 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4334 + NIPQUAD(ip_hdr(skb)->saddr),
4335 + NIPQUAD(ip_hdr(skb)->daddr));
4337 + NIPQUAD(skb->nh.iph->saddr),
4338 + NIPQUAD(skb->nh.iph->daddr));
4340 + DP("flag %s port %u",
4341 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4343 + if (port == INVALID_PORT)
4346 + return __delip(set,
4347 + ntohl(flags[index] & IPSET_SRC
4348 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4349 + ? ip_hdr(skb)->saddr
4350 + : ip_hdr(skb)->daddr),
4352 + ? skb->nh.iph->saddr
4353 + : skb->nh.iph->daddr),
4359 +static int create(struct ip_set *set, const void *data, size_t size)
4361 + struct ip_set_req_ipporthash_create *req =
4362 + (struct ip_set_req_ipporthash_create *) data;
4363 + struct ip_set_ipporthash *map;
4366 + if (size != sizeof(struct ip_set_req_ipporthash_create)) {
4367 + ip_set_printk("data length wrong (want %zu, have %zu)",
4368 + sizeof(struct ip_set_req_ipporthash_create),
4373 + if (req->hashsize < 1) {
4374 + ip_set_printk("hashsize too small");
4378 + if (req->probes < 1) {
4379 + ip_set_printk("probes too small");
4383 + map = kmalloc(sizeof(struct ip_set_ipporthash)
4384 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
4386 + DP("out of memory for %d bytes",
4387 + sizeof(struct ip_set_ipporthash)
4388 + + req->probes * sizeof(uint32_t));
4391 + for (i = 0; i < req->probes; i++)
4392 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
4393 + map->elements = 0;
4394 + map->hashsize = req->hashsize;
4395 + map->probes = req->probes;
4396 + map->resize = req->resize;
4397 + map->first_ip = req->from;
4398 + map->last_ip = req->to;
4399 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
4400 + if (!map->members) {
4401 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
4410 +static void destroy(struct ip_set *set)
4412 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4414 + harray_free(map->members);
4420 +static void flush(struct ip_set *set)
4422 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4423 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
4424 + map->elements = 0;
4427 +static void list_header(const struct ip_set *set, void *data)
4429 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4430 + struct ip_set_req_ipporthash_create *header =
4431 + (struct ip_set_req_ipporthash_create *) data;
4433 + header->hashsize = map->hashsize;
4434 + header->probes = map->probes;
4435 + header->resize = map->resize;
4436 + header->from = map->first_ip;
4437 + header->to = map->last_ip;
4440 +static int list_members_size(const struct ip_set *set)
4442 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4444 + return (map->hashsize * sizeof(ip_set_ip_t));
4447 +static void list_members(const struct ip_set *set, void *data)
4449 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4450 + ip_set_ip_t i, *elem;
4452 + for (i = 0; i < map->hashsize; i++) {
4453 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
4454 + ((ip_set_ip_t *)data)[i] = *elem;
4458 +static struct ip_set_type ip_set_ipporthash = {
4459 + .typename = SETTYPE_NAME,
4460 + .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_DATA_DOUBLE,
4461 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4462 + .create = &create,
4463 + .destroy = &destroy,
4465 + .reqsize = sizeof(struct ip_set_req_ipporthash),
4467 + .addip_kernel = &addip_kernel,
4470 + .delip_kernel = &delip_kernel,
4471 + .testip = &testip,
4472 + .testip_kernel = &testip_kernel,
4473 + .header_size = sizeof(struct ip_set_req_ipporthash_create),
4474 + .list_header = &list_header,
4475 + .list_members_size = &list_members_size,
4476 + .list_members = &list_members,
4477 + .me = THIS_MODULE,
4480 +MODULE_LICENSE("GPL");
4481 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4482 +MODULE_DESCRIPTION("ipporthash type of IP sets");
4483 +module_param(limit, int, 0600);
4484 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
4486 +static int __init ip_set_ipporthash_init(void)
4488 + return ip_set_register_set_type(&ip_set_ipporthash);
4491 +static void __exit ip_set_ipporthash_fini(void)
4493 + /* FIXME: possible race with ip_set_create() */
4494 + ip_set_unregister_set_type(&ip_set_ipporthash);
4497 +module_init(ip_set_ipporthash_init);
4498 +module_exit(ip_set_ipporthash_fini);
4500 +++ b/net/ipv4/netfilter/ip_set_iptree.c
4502 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4504 + * This program is free software; you can redistribute it and/or modify
4505 + * it under the terms of the GNU General Public License version 2 as
4506 + * published by the Free Software Foundation.
4509 +/* Kernel module implementing an IP set type: the iptree type */
4511 +#include <linux/version.h>
4512 +#include <linux/module.h>
4513 +#include <linux/ip.h>
4514 +#include <linux/skbuff.h>
4515 +#include <linux/slab.h>
4516 +#include <linux/delay.h>
4517 +#include <linux/netfilter_ipv4/ip_tables.h>
4518 +#include <linux/netfilter_ipv4/ip_set.h>
4519 +#include <linux/errno.h>
4520 +#include <asm/uaccess.h>
4521 +#include <asm/bitops.h>
4522 +#include <linux/spinlock.h>
4524 +/* Backward compatibility */
4529 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
4531 +static int limit = MAX_RANGE;
4533 +/* Garbage collection interval in seconds: */
4534 +#define IPTREE_GC_TIME 5*60
4535 +/* Sleep so many milliseconds before trying again
4536 + * to delete the gc timer at destroying/flushing a set */
4537 +#define IPTREE_DESTROY_SLEEP 100
4539 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
4540 +static struct kmem_cache *branch_cachep;
4541 +static struct kmem_cache *leaf_cachep;
4543 +static kmem_cache_t *branch_cachep;
4544 +static kmem_cache_t *leaf_cachep;
4547 +#if defined(__LITTLE_ENDIAN)
4548 +#define ABCD(a,b,c,d,addrp) do { \
4549 + a = ((unsigned char *)addrp)[3]; \
4550 + b = ((unsigned char *)addrp)[2]; \
4551 + c = ((unsigned char *)addrp)[1]; \
4552 + d = ((unsigned char *)addrp)[0]; \
4554 +#elif defined(__BIG_ENDIAN)
4555 +#define ABCD(a,b,c,d,addrp) do { \
4556 + a = ((unsigned char *)addrp)[0]; \
4557 + b = ((unsigned char *)addrp)[1]; \
4558 + c = ((unsigned char *)addrp)[2]; \
4559 + d = ((unsigned char *)addrp)[3]; \
4562 +#error "Please fix asm/byteorder.h"
4563 +#endif /* __LITTLE_ENDIAN */
4565 +#define TESTIP_WALK(map, elem, branch) do { \
4566 + if ((map)->tree[elem]) { \
4567 + branch = (map)->tree[elem]; \
4573 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4575 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4576 + struct ip_set_iptreeb *btree;
4577 + struct ip_set_iptreec *ctree;
4578 + struct ip_set_iptreed *dtree;
4579 + unsigned char a,b,c,d;
4585 + ABCD(a, b, c, d, hash_ip);
4586 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
4587 + TESTIP_WALK(map, a, btree);
4588 + TESTIP_WALK(btree, b, ctree);
4589 + TESTIP_WALK(ctree, c, dtree);
4590 + DP("%lu %lu", dtree->expires[d], jiffies);
4591 + return dtree->expires[d]
4593 + || time_after(dtree->expires[d], jiffies));
4597 +testip(struct ip_set *set, const void *data, size_t size,
4598 + ip_set_ip_t *hash_ip)
4600 + struct ip_set_req_iptree *req =
4601 + (struct ip_set_req_iptree *) data;
4603 + if (size != sizeof(struct ip_set_req_iptree)) {
4604 + ip_set_printk("data length wrong (want %zu, have %zu)",
4605 + sizeof(struct ip_set_req_iptree),
4609 + return __testip(set, req->ip, hash_ip);
4613 +testip_kernel(struct ip_set *set,
4614 + const struct sk_buff *skb,
4615 + ip_set_ip_t *hash_ip,
4616 + const u_int32_t *flags,
4617 + unsigned char index)
4621 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4622 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4623 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4624 + NIPQUAD(ip_hdr(skb)->saddr),
4625 + NIPQUAD(ip_hdr(skb)->daddr));
4627 + NIPQUAD(skb->nh.iph->saddr),
4628 + NIPQUAD(skb->nh.iph->daddr));
4631 + res = __testip(set,
4632 + ntohl(flags[index] & IPSET_SRC
4633 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4634 + ? ip_hdr(skb)->saddr
4635 + : ip_hdr(skb)->daddr),
4637 + ? skb->nh.iph->saddr
4638 + : skb->nh.iph->daddr),
4641 + return (res < 0 ? 0 : res);
4644 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
4645 + if ((map)->tree[elem]) { \
4646 + DP("found %u", elem); \
4647 + branch = (map)->tree[elem]; \
4649 + branch = (type *) \
4650 + kmem_cache_alloc(cachep, GFP_ATOMIC); \
4651 + if (branch == NULL) \
4653 + memset(branch, 0, sizeof(*branch)); \
4654 + (map)->tree[elem] = branch; \
4655 + DP("alloc %u", elem); \
4660 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
4661 + ip_set_ip_t *hash_ip)
4663 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4664 + struct ip_set_iptreeb *btree;
4665 + struct ip_set_iptreec *ctree;
4666 + struct ip_set_iptreed *dtree;
4667 + unsigned char a,b,c,d;
4670 + if (!ip || map->elements >= limit)
4671 + /* We could call the garbage collector
4672 + * but it's probably overkill */
4676 + ABCD(a, b, c, d, hash_ip);
4677 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
4678 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
4679 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
4680 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
4681 + if (dtree->expires[d]
4682 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4684 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
4685 + /* Lottery: I won! */
4686 + if (dtree->expires[d] == 0)
4687 + dtree->expires[d] = 1;
4688 + DP("%u %lu", d, dtree->expires[d]);
4695 +addip(struct ip_set *set, const void *data, size_t size,
4696 + ip_set_ip_t *hash_ip)
4698 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4699 + struct ip_set_req_iptree *req =
4700 + (struct ip_set_req_iptree *) data;
4702 + if (size != sizeof(struct ip_set_req_iptree)) {
4703 + ip_set_printk("data length wrong (want %zu, have %zu)",
4704 + sizeof(struct ip_set_req_iptree),
4708 + DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
4709 + return __addip(set, req->ip,
4710 + req->timeout ? req->timeout : map->timeout,
4715 +addip_kernel(struct ip_set *set,
4716 + const struct sk_buff *skb,
4717 + ip_set_ip_t *hash_ip,
4718 + const u_int32_t *flags,
4719 + unsigned char index)
4721 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4723 + return __addip(set,
4724 + ntohl(flags[index] & IPSET_SRC
4725 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4726 + ? ip_hdr(skb)->saddr
4727 + : ip_hdr(skb)->daddr),
4729 + ? skb->nh.iph->saddr
4730 + : skb->nh.iph->daddr),
4736 +#define DELIP_WALK(map, elem, branch) do { \
4737 + if ((map)->tree[elem]) { \
4738 + branch = (map)->tree[elem]; \
4744 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4746 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4747 + struct ip_set_iptreeb *btree;
4748 + struct ip_set_iptreec *ctree;
4749 + struct ip_set_iptreed *dtree;
4750 + unsigned char a,b,c,d;
4756 + ABCD(a, b, c, d, hash_ip);
4757 + DELIP_WALK(map, a, btree);
4758 + DELIP_WALK(btree, b, ctree);
4759 + DELIP_WALK(ctree, c, dtree);
4761 + if (dtree->expires[d]) {
4762 + dtree->expires[d] = 0;
4770 +delip(struct ip_set *set, const void *data, size_t size,
4771 + ip_set_ip_t *hash_ip)
4773 + struct ip_set_req_iptree *req =
4774 + (struct ip_set_req_iptree *) data;
4776 + if (size != sizeof(struct ip_set_req_iptree)) {
4777 + ip_set_printk("data length wrong (want %zu, have %zu)",
4778 + sizeof(struct ip_set_req_iptree),
4782 + return __delip(set, req->ip, hash_ip);
4786 +delip_kernel(struct ip_set *set,
4787 + const struct sk_buff *skb,
4788 + ip_set_ip_t *hash_ip,
4789 + const u_int32_t *flags,
4790 + unsigned char index)
4792 + return __delip(set,
4793 + ntohl(flags[index] & IPSET_SRC
4794 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4795 + ? ip_hdr(skb)->saddr
4796 + : ip_hdr(skb)->daddr),
4798 + ? skb->nh.iph->saddr
4799 + : skb->nh.iph->daddr),
4804 +#define LOOP_WALK_BEGIN(map, i, branch) \
4805 + for (i = 0; i < 256; i++) { \
4806 + if (!(map)->tree[i]) \
4808 + branch = (map)->tree[i]
4810 +#define LOOP_WALK_END }
4812 +static void ip_tree_gc(unsigned long ul_set)
4814 + struct ip_set *set = (void *) ul_set;
4815 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4816 + struct ip_set_iptreeb *btree;
4817 + struct ip_set_iptreec *ctree;
4818 + struct ip_set_iptreed *dtree;
4819 + unsigned int a,b,c,d;
4820 + unsigned char i,j,k;
4823 + DP("gc: %s", set->name);
4824 + write_lock_bh(&set->lock);
4825 + LOOP_WALK_BEGIN(map, a, btree);
4826 + LOOP_WALK_BEGIN(btree, b, ctree);
4827 + LOOP_WALK_BEGIN(ctree, c, dtree);
4828 + for (d = 0; d < 256; d++) {
4829 + if (dtree->expires[d]) {
4830 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4832 + dtree->expires[d], jiffies);
4834 + && time_before(dtree->expires[d], jiffies)) {
4835 + dtree->expires[d] = 0;
4842 + DP("gc: %s: leaf %u %u %u empty",
4843 + set->name, a, b, c);
4844 + kmem_cache_free(leaf_cachep, dtree);
4845 + ctree->tree[c] = NULL;
4847 + DP("gc: %s: leaf %u %u %u not empty",
4848 + set->name, a, b, c);
4854 + DP("gc: %s: branch %u %u empty",
4856 + kmem_cache_free(branch_cachep, ctree);
4857 + btree->tree[b] = NULL;
4859 + DP("gc: %s: branch %u %u not empty",
4866 + DP("gc: %s: branch %u empty",
4868 + kmem_cache_free(branch_cachep, btree);
4869 + map->tree[a] = NULL;
4871 + DP("gc: %s: branch %u not empty",
4876 + write_unlock_bh(&set->lock);
4878 + map->gc.expires = jiffies + map->gc_interval * HZ;
4879 + add_timer(&map->gc);
4882 +static inline void init_gc_timer(struct ip_set *set)
4884 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4886 + /* Even if there is no timeout for the entries,
4887 + * we still have to call gc because delete
4888 + * do not clean up empty branches */
4889 + map->gc_interval = IPTREE_GC_TIME;
4890 + init_timer(&map->gc);
4891 + map->gc.data = (unsigned long) set;
4892 + map->gc.function = ip_tree_gc;
4893 + map->gc.expires = jiffies + map->gc_interval * HZ;
4894 + add_timer(&map->gc);
4897 +static int create(struct ip_set *set, const void *data, size_t size)
4899 + struct ip_set_req_iptree_create *req =
4900 + (struct ip_set_req_iptree_create *) data;
4901 + struct ip_set_iptree *map;
4903 + if (size != sizeof(struct ip_set_req_iptree_create)) {
4904 + ip_set_printk("data length wrong (want %zu, have %zu)",
4905 + sizeof(struct ip_set_req_iptree_create),
4910 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4912 + DP("out of memory for %d bytes",
4913 + sizeof(struct ip_set_iptree));
4916 + memset(map, 0, sizeof(*map));
4917 + map->timeout = req->timeout;
4918 + map->elements = 0;
4921 + init_gc_timer(set);
4926 +static void __flush(struct ip_set_iptree *map)
4928 + struct ip_set_iptreeb *btree;
4929 + struct ip_set_iptreec *ctree;
4930 + struct ip_set_iptreed *dtree;
4931 + unsigned int a,b,c;
4933 + LOOP_WALK_BEGIN(map, a, btree);
4934 + LOOP_WALK_BEGIN(btree, b, ctree);
4935 + LOOP_WALK_BEGIN(ctree, c, dtree);
4936 + kmem_cache_free(leaf_cachep, dtree);
4938 + kmem_cache_free(branch_cachep, ctree);
4940 + kmem_cache_free(branch_cachep, btree);
4942 + map->elements = 0;
4945 +static void destroy(struct ip_set *set)
4947 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4949 + /* gc might be running */
4950 + while (!del_timer(&map->gc))
4951 + msleep(IPTREE_DESTROY_SLEEP);
4957 +static void flush(struct ip_set *set)
4959 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4960 + unsigned int timeout = map->timeout;
4962 + /* gc might be running */
4963 + while (!del_timer(&map->gc))
4964 + msleep(IPTREE_DESTROY_SLEEP);
4966 + memset(map, 0, sizeof(*map));
4967 + map->timeout = timeout;
4969 + init_gc_timer(set);
4972 +static void list_header(const struct ip_set *set, void *data)
4974 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4975 + struct ip_set_req_iptree_create *header =
4976 + (struct ip_set_req_iptree_create *) data;
4978 + header->timeout = map->timeout;
4981 +static int list_members_size(const struct ip_set *set)
4983 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4984 + struct ip_set_iptreeb *btree;
4985 + struct ip_set_iptreec *ctree;
4986 + struct ip_set_iptreed *dtree;
4987 + unsigned int a,b,c,d;
4988 + unsigned int count = 0;
4990 + LOOP_WALK_BEGIN(map, a, btree);
4991 + LOOP_WALK_BEGIN(btree, b, ctree);
4992 + LOOP_WALK_BEGIN(ctree, c, dtree);
4993 + for (d = 0; d < 256; d++) {
4994 + if (dtree->expires[d]
4995 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
5002 + DP("members %u", count);
5003 + return (count * sizeof(struct ip_set_req_iptree));
5006 +static void list_members(const struct ip_set *set, void *data)
5008 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5009 + struct ip_set_iptreeb *btree;
5010 + struct ip_set_iptreec *ctree;
5011 + struct ip_set_iptreed *dtree;
5012 + unsigned int a,b,c,d;
5013 + size_t offset = 0;
5014 + struct ip_set_req_iptree *entry;
5016 + LOOP_WALK_BEGIN(map, a, btree);
5017 + LOOP_WALK_BEGIN(btree, b, ctree);
5018 + LOOP_WALK_BEGIN(ctree, c, dtree);
5019 + for (d = 0; d < 256; d++) {
5020 + if (dtree->expires[d]
5021 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
5022 + entry = (struct ip_set_req_iptree *)(data + offset);
5023 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
5024 + entry->timeout = !map->timeout ? 0
5025 + : (dtree->expires[d] - jiffies)/HZ;
5026 + offset += sizeof(struct ip_set_req_iptree);
5034 +static struct ip_set_type ip_set_iptree = {
5035 + .typename = SETTYPE_NAME,
5036 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
5037 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5038 + .create = &create,
5039 + .destroy = &destroy,
5041 + .reqsize = sizeof(struct ip_set_req_iptree),
5043 + .addip_kernel = &addip_kernel,
5045 + .delip_kernel = &delip_kernel,
5046 + .testip = &testip,
5047 + .testip_kernel = &testip_kernel,
5048 + .header_size = sizeof(struct ip_set_req_iptree_create),
5049 + .list_header = &list_header,
5050 + .list_members_size = &list_members_size,
5051 + .list_members = &list_members,
5052 + .me = THIS_MODULE,
5055 +MODULE_LICENSE("GPL");
5056 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5057 +MODULE_DESCRIPTION("iptree type of IP sets");
5058 +module_param(limit, int, 0600);
5059 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
5061 +static int __init ip_set_iptree_init(void)
5065 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5066 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
5067 + sizeof(struct ip_set_iptreeb),
5070 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
5071 + sizeof(struct ip_set_iptreeb),
5072 + 0, 0, NULL, NULL);
5074 + if (!branch_cachep) {
5075 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
5079 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5080 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
5081 + sizeof(struct ip_set_iptreed),
5084 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
5085 + sizeof(struct ip_set_iptreed),
5086 + 0, 0, NULL, NULL);
5088 + if (!leaf_cachep) {
5089 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
5093 + ret = ip_set_register_set_type(&ip_set_iptree);
5097 + kmem_cache_destroy(leaf_cachep);
5099 + kmem_cache_destroy(branch_cachep);
5104 +static void __exit ip_set_iptree_fini(void)
5106 + /* FIXME: possible race with ip_set_create() */
5107 + ip_set_unregister_set_type(&ip_set_iptree);
5108 + kmem_cache_destroy(leaf_cachep);
5109 + kmem_cache_destroy(branch_cachep);
5112 +module_init(ip_set_iptree_init);
5113 +module_exit(ip_set_iptree_fini);
5115 +++ b/net/ipv4/netfilter/ip_set_iptreemap.c
5117 +/* Copyright (C) 2007 Sven Wegener <sven.wegener@stealer.net>
5119 + * This program is free software; you can redistribute it and/or modify it
5120 + * under the terms of the GNU General Public License version 2 as published by
5121 + * the Free Software Foundation.
5124 +/* This modules implements the iptreemap ipset type. It uses bitmaps to
5125 + * represent every single IPv4 address as a single bit. The bitmaps are managed
5126 + * in a tree structure, where the first three octets of an addresses are used
5127 + * as an index to find the bitmap and the last octet is used as the bit number.
5130 +#include <linux/version.h>
5131 +#include <linux/module.h>
5132 +#include <linux/ip.h>
5133 +#include <linux/skbuff.h>
5134 +#include <linux/slab.h>
5135 +#include <linux/delay.h>
5136 +#include <linux/netfilter_ipv4/ip_tables.h>
5137 +#include <linux/netfilter_ipv4/ip_set.h>
5138 +#include <linux/errno.h>
5139 +#include <asm/uaccess.h>
5140 +#include <asm/bitops.h>
5141 +#include <linux/spinlock.h>
5143 +#include <linux/netfilter_ipv4/ip_set_iptreemap.h>
5145 +#define IPTREEMAP_DEFAULT_GC_TIME (5 * 60)
5146 +#define IPTREEMAP_DESTROY_SLEEP (100)
5148 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
5149 +static struct kmem_cache *cachep_b;
5150 +static struct kmem_cache *cachep_c;
5151 +static struct kmem_cache *cachep_d;
5153 +static kmem_cache_t *cachep_b;
5154 +static kmem_cache_t *cachep_c;
5155 +static kmem_cache_t *cachep_d;
5158 +static struct ip_set_iptreemap_d *fullbitmap_d;
5159 +static struct ip_set_iptreemap_c *fullbitmap_c;
5160 +static struct ip_set_iptreemap_b *fullbitmap_b;
5162 +#if defined(__LITTLE_ENDIAN)
5163 +#define ABCD(a, b, c, d, addr) \
5165 + a = ((unsigned char *)addr)[3]; \
5166 + b = ((unsigned char *)addr)[2]; \
5167 + c = ((unsigned char *)addr)[1]; \
5168 + d = ((unsigned char *)addr)[0]; \
5170 +#elif defined(__BIG_ENDIAN)
5171 +#define ABCD(a,b,c,d,addrp) do { \
5172 + a = ((unsigned char *)addrp)[0]; \
5173 + b = ((unsigned char *)addrp)[1]; \
5174 + c = ((unsigned char *)addrp)[2]; \
5175 + d = ((unsigned char *)addrp)[3]; \
5178 +#error "Please fix asm/byteorder.h"
5179 +#endif /* __LITTLE_ENDIAN */
5181 +#define TESTIP_WALK(map, elem, branch, full) \
5183 + branch = (map)->tree[elem]; \
5186 + else if (branch == full) \
5190 +#define ADDIP_WALK(map, elem, branch, type, cachep, full) \
5192 + branch = (map)->tree[elem]; \
5194 + branch = (type *) kmem_cache_alloc(cachep, GFP_ATOMIC); \
5197 + memset(branch, 0, sizeof(*branch)); \
5198 + (map)->tree[elem] = branch; \
5199 + } else if (branch == full) { \
5204 +#define ADDIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free) \
5205 + for (a = a1; a <= a2; a++) { \
5206 + branch = (map)->tree[a]; \
5207 + if (branch != full) { \
5208 + if ((a > a1 && a < a2) || (hint)) { \
5211 + (map)->tree[a] = full; \
5213 + } else if (!branch) { \
5214 + branch = kmem_cache_alloc(cachep, GFP_ATOMIC); \
5217 + memset(branch, 0, sizeof(*branch)); \
5218 + (map)->tree[a] = branch; \
5221 +#define ADDIP_RANGE_LOOP_END() \
5225 +#define DELIP_WALK(map, elem, branch, cachep, full, flags) \
5227 + branch = (map)->tree[elem]; \
5230 + } else if (branch == full) { \
5231 + branch = kmem_cache_alloc(cachep, flags); \
5234 + memcpy(branch, full, sizeof(*full)); \
5235 + (map)->tree[elem] = branch; \
5239 +#define DELIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free, flags) \
5240 + for (a = a1; a <= a2; a++) { \
5241 + branch = (map)->tree[a]; \
5243 + if ((a > a1 && a < a2) || (hint)) { \
5244 + if (branch != full) \
5246 + (map)->tree[a] = NULL; \
5248 + } else if (branch == full) { \
5249 + branch = kmem_cache_alloc(cachep, flags); \
5252 + memcpy(branch, full, sizeof(*branch)); \
5253 + (map)->tree[a] = branch; \
5256 +#define DELIP_RANGE_LOOP_END() \
5260 +#define LOOP_WALK_BEGIN(map, i, branch) \
5261 + for (i = 0; i < 256; i++) { \
5262 + branch = (map)->tree[i]; \
5263 + if (likely(!branch)) \
5266 +#define LOOP_WALK_END() \
5269 +#define LOOP_WALK_BEGIN_GC(map, i, branch, full, cachep, count) \
5271 + for (i = 0; i < 256; i++) { \
5272 + branch = (map)->tree[i]; \
5273 + if (likely(!branch)) \
5276 + if (branch == full) { \
5281 +#define LOOP_WALK_END_GC(map, i, branch, full, cachep, count) \
5282 + if (-256 == count) { \
5283 + kmem_cache_free(cachep, branch); \
5284 + (map)->tree[i] = NULL; \
5285 + } else if (256 == count) { \
5286 + kmem_cache_free(cachep, branch); \
5287 + (map)->tree[i] = full; \
5291 +#define LOOP_WALK_BEGIN_COUNT(map, i, branch, inrange, count) \
5292 + for (i = 0; i < 256; i++) { \
5293 + if (!(map)->tree[i]) { \
5300 + branch = (map)->tree[i];
5302 +#define LOOP_WALK_END_COUNT() \
5305 +#define MIN(a, b) (a < b ? a : b)
5306 +#define MAX(a, b) (a > b ? a : b)
5308 +#define GETVALUE1(a, a1, b1, r) \
5309 + (a == a1 ? b1 : r)
5311 +#define GETVALUE2(a, b, a1, b1, c1, r) \
5312 + (a == a1 && b == b1 ? c1 : r)
5314 +#define GETVALUE3(a, b, c, a1, b1, c1, d1, r) \
5315 + (a == a1 && b == b1 && c == c1 ? d1 : r)
5317 +#define CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2) \
5319 + GETVALUE1(a, a1, b1, 0) == 0 \
5320 + && GETVALUE1(a, a2, b2, 255) == 255 \
5327 +#define CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2) \
5329 + GETVALUE2(a, b, a1, b1, c1, 0) == 0 \
5330 + && GETVALUE2(a, b, a2, b2, c2, 255) == 255 \
5335 +#define CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2) \
5337 + GETVALUE3(a, b, c, a1, b1, c1, d1, 0) == 0 \
5338 + && GETVALUE3(a, b, c, a2, b2, c2, d2, 255) == 255 \
5343 +free_d(struct ip_set_iptreemap_d *map)
5345 + kmem_cache_free(cachep_d, map);
5349 +free_c(struct ip_set_iptreemap_c *map)
5351 + struct ip_set_iptreemap_d *dtree;
5354 + LOOP_WALK_BEGIN(map, i, dtree) {
5355 + if (dtree != fullbitmap_d)
5357 + } LOOP_WALK_END();
5359 + kmem_cache_free(cachep_c, map);
5363 +free_b(struct ip_set_iptreemap_b *map)
5365 + struct ip_set_iptreemap_c *ctree;
5368 + LOOP_WALK_BEGIN(map, i, ctree) {
5369 + if (ctree != fullbitmap_c)
5371 + } LOOP_WALK_END();
5373 + kmem_cache_free(cachep_b, map);
5377 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5379 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5380 + struct ip_set_iptreemap_b *btree;
5381 + struct ip_set_iptreemap_c *ctree;
5382 + struct ip_set_iptreemap_d *dtree;
5383 + unsigned char a, b, c, d;
5387 + ABCD(a, b, c, d, hash_ip);
5389 + TESTIP_WALK(map, a, btree, fullbitmap_b);
5390 + TESTIP_WALK(btree, b, ctree, fullbitmap_c);
5391 + TESTIP_WALK(ctree, c, dtree, fullbitmap_d);
5393 + return !!test_bit(d, (void *) dtree->bitmap);
5397 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5399 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5401 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5402 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5406 + return __testip(set, req->start, hash_ip);
5410 +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)
5414 + res = __testip(set,
5415 + ntohl(flags[index] & IPSET_SRC
5416 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5417 + ? ip_hdr(skb)->saddr
5418 + : ip_hdr(skb)->daddr),
5420 + ? skb->nh.iph->saddr
5421 + : skb->nh.iph->daddr),
5425 + return (res < 0 ? 0 : res);
5429 +__addip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5431 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5432 + struct ip_set_iptreemap_b *btree;
5433 + struct ip_set_iptreemap_c *ctree;
5434 + struct ip_set_iptreemap_d *dtree;
5435 + unsigned char a, b, c, d;
5439 + ABCD(a, b, c, d, hash_ip);
5441 + ADDIP_WALK(map, a, btree, struct ip_set_iptreemap_b, cachep_b, fullbitmap_b);
5442 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreemap_c, cachep_c, fullbitmap_c);
5443 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreemap_d, cachep_d, fullbitmap_d);
5445 + if (test_and_set_bit(d, (void *) dtree->bitmap))
5448 + set_bit(b, (void *) btree->dirty);
5454 +__addip_range(struct ip_set *set, ip_set_ip_t start, ip_set_ip_t end, ip_set_ip_t *hash_ip)
5456 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5457 + struct ip_set_iptreemap_b *btree;
5458 + struct ip_set_iptreemap_c *ctree;
5459 + struct ip_set_iptreemap_d *dtree;
5460 + unsigned int a, b, c, d;
5461 + unsigned char a1, b1, c1, d1;
5462 + unsigned char a2, b2, c2, d2;
5465 + return __addip_single(set, start, hash_ip);
5469 + ABCD(a1, b1, c1, d1, &start);
5470 + ABCD(a2, b2, c2, d2, &end);
5472 + /* This is sooo ugly... */
5473 + ADDIP_RANGE_LOOP(map, a, a1, a2, CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2), btree, fullbitmap_b, cachep_b, free_b) {
5474 + 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) {
5475 + 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) {
5476 + for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5477 + set_bit(d, (void *) dtree->bitmap);
5478 + set_bit(b, (void *) btree->dirty);
5479 + } ADDIP_RANGE_LOOP_END();
5480 + } ADDIP_RANGE_LOOP_END();
5481 + } ADDIP_RANGE_LOOP_END();
5487 +addip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5489 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5491 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5492 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5496 + return __addip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip);
5500 +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)
5503 + return __addip_single(set,
5504 + ntohl(flags[index] & IPSET_SRC
5505 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5506 + ? ip_hdr(skb)->saddr
5507 + : ip_hdr(skb)->daddr),
5509 + ? skb->nh.iph->saddr
5510 + : skb->nh.iph->daddr),
5516 +__delip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip, unsigned int __nocast flags)
5518 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5519 + struct ip_set_iptreemap_b *btree;
5520 + struct ip_set_iptreemap_c *ctree;
5521 + struct ip_set_iptreemap_d *dtree;
5522 + unsigned char a,b,c,d;
5526 + ABCD(a, b, c, d, hash_ip);
5528 + DELIP_WALK(map, a, btree, cachep_b, fullbitmap_b, flags);
5529 + DELIP_WALK(btree, b, ctree, cachep_c, fullbitmap_c, flags);
5530 + DELIP_WALK(ctree, c, dtree, cachep_d, fullbitmap_d, flags);
5532 + if (!test_and_clear_bit(d, (void *) dtree->bitmap))
5535 + set_bit(b, (void *) btree->dirty);
5541 +__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)
5543 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5544 + struct ip_set_iptreemap_b *btree;
5545 + struct ip_set_iptreemap_c *ctree;
5546 + struct ip_set_iptreemap_d *dtree;
5547 + unsigned int a, b, c, d;
5548 + unsigned char a1, b1, c1, d1;
5549 + unsigned char a2, b2, c2, d2;
5552 + return __delip_single(set, start, hash_ip, flags);
5556 + ABCD(a1, b1, c1, d1, &start);
5557 + ABCD(a2, b2, c2, d2, &end);
5559 + /* This is sooo ugly... */
5560 + 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) {
5561 + 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) {
5562 + 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) {
5563 + for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5564 + clear_bit(d, (void *) dtree->bitmap);
5565 + set_bit(b, (void *) btree->dirty);
5566 + } DELIP_RANGE_LOOP_END();
5567 + } DELIP_RANGE_LOOP_END();
5568 + } DELIP_RANGE_LOOP_END();
5574 +delip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5576 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5578 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5579 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5583 + return __delip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip, GFP_KERNEL);
5587 +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)
5589 + return __delip_single(set,
5590 + ntohl(flags[index] & IPSET_SRC
5591 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5592 + ? ip_hdr(skb)->saddr
5593 + : ip_hdr(skb)->daddr),
5595 + ? skb->nh.iph->saddr
5596 + : skb->nh.iph->daddr),
5602 +/* Check the status of the bitmap
5603 + * -1 == all bits cleared
5604 + * 1 == all bits set
5605 + * 0 == anything else
5608 +bitmap_status(struct ip_set_iptreemap_d *dtree)
5610 + unsigned char first = dtree->bitmap[0];
5613 + for (a = 1; a < 32; a++)
5614 + if (dtree->bitmap[a] != first)
5617 + return (first == 0 ? -1 : (first == 255 ? 1 : 0));
5621 +gc(unsigned long addr)
5623 + struct ip_set *set = (struct ip_set *) addr;
5624 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5625 + struct ip_set_iptreemap_b *btree;
5626 + struct ip_set_iptreemap_c *ctree;
5627 + struct ip_set_iptreemap_d *dtree;
5628 + unsigned int a, b, c;
5631 + write_lock_bh(&set->lock);
5633 + LOOP_WALK_BEGIN_GC(map, a, btree, fullbitmap_b, cachep_b, i) {
5634 + LOOP_WALK_BEGIN_GC(btree, b, ctree, fullbitmap_c, cachep_c, j) {
5635 + if (!test_and_clear_bit(b, (void *) btree->dirty))
5637 + LOOP_WALK_BEGIN_GC(ctree, c, dtree, fullbitmap_d, cachep_d, k) {
5638 + switch (bitmap_status(dtree)) {
5640 + kmem_cache_free(cachep_d, dtree);
5641 + ctree->tree[c] = NULL;
5645 + kmem_cache_free(cachep_d, dtree);
5646 + ctree->tree[c] = fullbitmap_d;
5650 + } LOOP_WALK_END();
5651 + } LOOP_WALK_END_GC(btree, b, ctree, fullbitmap_c, cachep_c, k);
5652 + } LOOP_WALK_END_GC(map, a, btree, fullbitmap_b, cachep_b, j);
5654 + write_unlock_bh(&set->lock);
5656 + map->gc.expires = jiffies + map->gc_interval * HZ;
5657 + add_timer(&map->gc);
5661 +init_gc_timer(struct ip_set *set)
5663 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5665 + init_timer(&map->gc);
5666 + map->gc.data = (unsigned long) set;
5667 + map->gc.function = gc;
5668 + map->gc.expires = jiffies + map->gc_interval * HZ;
5669 + add_timer(&map->gc);
5672 +static int create(struct ip_set *set, const void *data, size_t size)
5674 + struct ip_set_req_iptreemap_create *req = (struct ip_set_req_iptreemap_create *) data;
5675 + struct ip_set_iptreemap *map;
5677 + if (size != sizeof(struct ip_set_req_iptreemap_create)) {
5678 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap_create), size);
5682 + map = kzalloc(sizeof(*map), GFP_KERNEL);
5686 + map->gc_interval = req->gc_interval ? req->gc_interval : IPTREEMAP_DEFAULT_GC_TIME;
5689 + init_gc_timer(set);
5694 +static inline void __flush(struct ip_set_iptreemap *map)
5696 + struct ip_set_iptreemap_b *btree;
5699 + LOOP_WALK_BEGIN(map, a, btree);
5700 + if (btree != fullbitmap_b)
5705 +static void destroy(struct ip_set *set)
5707 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5709 + while (!del_timer(&map->gc))
5710 + msleep(IPTREEMAP_DESTROY_SLEEP);
5718 +static void flush(struct ip_set *set)
5720 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5722 + while (!del_timer(&map->gc))
5723 + msleep(IPTREEMAP_DESTROY_SLEEP);
5727 + memset(map, 0, sizeof(*map));
5729 + init_gc_timer(set);
5732 +static void list_header(const struct ip_set *set, void *data)
5734 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5735 + struct ip_set_req_iptreemap_create *header = (struct ip_set_req_iptreemap_create *) data;
5737 + header->gc_interval = map->gc_interval;
5740 +static int list_members_size(const struct ip_set *set)
5742 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5743 + struct ip_set_iptreemap_b *btree;
5744 + struct ip_set_iptreemap_c *ctree;
5745 + struct ip_set_iptreemap_d *dtree;
5746 + unsigned int a, b, c, d, inrange = 0, count = 0;
5748 + LOOP_WALK_BEGIN_COUNT(map, a, btree, inrange, count) {
5749 + LOOP_WALK_BEGIN_COUNT(btree, b, ctree, inrange, count) {
5750 + LOOP_WALK_BEGIN_COUNT(ctree, c, dtree, inrange, count) {
5751 + for (d = 0; d < 256; d++) {
5752 + if (test_bit(d, (void *) dtree->bitmap)) {
5754 + } else if (inrange) {
5759 + } LOOP_WALK_END_COUNT();
5760 + } LOOP_WALK_END_COUNT();
5761 + } LOOP_WALK_END_COUNT();
5766 + return (count * sizeof(struct ip_set_req_iptreemap));
5769 +static inline size_t add_member(void *data, size_t offset, ip_set_ip_t start, ip_set_ip_t end)
5771 + struct ip_set_req_iptreemap *entry = (struct ip_set_req_iptreemap *) (data + offset);
5773 + entry->start = start;
5776 + return sizeof(*entry);
5779 +static void list_members(const struct ip_set *set, void *data)
5781 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5782 + struct ip_set_iptreemap_b *btree;
5783 + struct ip_set_iptreemap_c *ctree;
5784 + struct ip_set_iptreemap_d *dtree;
5785 + unsigned int a, b, c, d, inrange = 0;
5786 + size_t offset = 0;
5787 + ip_set_ip_t start = 0, end = 0, ip;
5789 + LOOP_WALK_BEGIN(map, a, btree) {
5790 + LOOP_WALK_BEGIN(btree, b, ctree) {
5791 + LOOP_WALK_BEGIN(ctree, c, dtree) {
5792 + for (d = 0; d < 256; d++) {
5793 + if (test_bit(d, (void *) dtree->bitmap)) {
5794 + ip = ((a << 24) | (b << 16) | (c << 8) | d);
5798 + } else if (end < ip - 1) {
5799 + offset += add_member(data, offset, start, end);
5803 + } else if (inrange) {
5804 + offset += add_member(data, offset, start, end);
5808 + } LOOP_WALK_END();
5809 + } LOOP_WALK_END();
5810 + } LOOP_WALK_END();
5813 + add_member(data, offset, start, end);
5816 +static struct ip_set_type ip_set_iptreemap = {
5817 + .typename = SETTYPE_NAME,
5818 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
5819 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5821 + .destroy = destroy,
5823 + .reqsize = sizeof(struct ip_set_req_iptreemap),
5825 + .addip_kernel = addip_kernel,
5827 + .delip_kernel = delip_kernel,
5829 + .testip_kernel = testip_kernel,
5830 + .header_size = sizeof(struct ip_set_req_iptreemap_create),
5831 + .list_header = list_header,
5832 + .list_members_size = list_members_size,
5833 + .list_members = list_members,
5834 + .me = THIS_MODULE,
5837 +MODULE_LICENSE("GPL");
5838 +MODULE_AUTHOR("Sven Wegener <sven.wegener@stealer.net>");
5839 +MODULE_DESCRIPTION("iptreemap type of IP sets");
5841 +static int __init ip_set_iptreemap_init(void)
5843 + int ret = -ENOMEM;
5846 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5847 + cachep_b = kmem_cache_create("ip_set_iptreemap_b",
5848 + sizeof(struct ip_set_iptreemap_b),
5851 + cachep_b = kmem_cache_create("ip_set_iptreemap_b",
5852 + sizeof(struct ip_set_iptreemap_b),
5853 + 0, 0, NULL, NULL);
5856 + ip_set_printk("Unable to create ip_set_iptreemap_b slab cache");
5860 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5861 + cachep_c = kmem_cache_create("ip_set_iptreemap_c",
5862 + sizeof(struct ip_set_iptreemap_c),
5865 + cachep_c = kmem_cache_create("ip_set_iptreemap_c",
5866 + sizeof(struct ip_set_iptreemap_c),
5867 + 0, 0, NULL, NULL);
5870 + ip_set_printk("Unable to create ip_set_iptreemap_c slab cache");
5874 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5875 + cachep_d = kmem_cache_create("ip_set_iptreemap_d",
5876 + sizeof(struct ip_set_iptreemap_d),
5879 + cachep_d = kmem_cache_create("ip_set_iptreemap_d",
5880 + sizeof(struct ip_set_iptreemap_d),
5881 + 0, 0, NULL, NULL);
5884 + ip_set_printk("Unable to create ip_set_iptreemap_d slab cache");
5888 + fullbitmap_d = kmem_cache_alloc(cachep_d, GFP_KERNEL);
5889 + if (!fullbitmap_d)
5892 + fullbitmap_c = kmem_cache_alloc(cachep_c, GFP_KERNEL);
5893 + if (!fullbitmap_c)
5896 + fullbitmap_b = kmem_cache_alloc(cachep_b, GFP_KERNEL);
5897 + if (!fullbitmap_b)
5900 + ret = ip_set_register_set_type(&ip_set_iptreemap);
5904 + /* Now init our global bitmaps */
5905 + memset(fullbitmap_d->bitmap, 0xff, sizeof(fullbitmap_d->bitmap));
5907 + for (a = 0; a < 256; a++)
5908 + fullbitmap_c->tree[a] = fullbitmap_d;
5910 + for (a = 0; a < 256; a++)
5911 + fullbitmap_b->tree[a] = fullbitmap_c;
5912 + memset(fullbitmap_b->dirty, 0, sizeof(fullbitmap_b->dirty));
5917 + kmem_cache_free(cachep_b, fullbitmap_b);
5919 + kmem_cache_free(cachep_c, fullbitmap_c);
5921 + kmem_cache_free(cachep_d, fullbitmap_d);
5923 + kmem_cache_destroy(cachep_d);
5925 + kmem_cache_destroy(cachep_c);
5927 + kmem_cache_destroy(cachep_b);
5933 +static void __exit ip_set_iptreemap_fini(void)
5935 + ip_set_unregister_set_type(&ip_set_iptreemap);
5936 + kmem_cache_free(cachep_d, fullbitmap_d);
5937 + kmem_cache_free(cachep_c, fullbitmap_c);
5938 + kmem_cache_free(cachep_b, fullbitmap_b);
5939 + kmem_cache_destroy(cachep_d);
5940 + kmem_cache_destroy(cachep_c);
5941 + kmem_cache_destroy(cachep_b);
5944 +module_init(ip_set_iptreemap_init);
5945 +module_exit(ip_set_iptreemap_fini);
5947 +++ b/net/ipv4/netfilter/ip_set_macipmap.c
5949 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5950 + * Patrick Schaaf <bof@bof.de>
5951 + * Martin Josefsson <gandalf@wlug.westbo.se>
5952 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5954 + * This program is free software; you can redistribute it and/or modify
5955 + * it under the terms of the GNU General Public License version 2 as
5956 + * published by the Free Software Foundation.
5959 +/* Kernel module implementing an IP set type: the macipmap type */
5961 +#include <linux/module.h>
5962 +#include <linux/ip.h>
5963 +#include <linux/skbuff.h>
5964 +#include <linux/version.h>
5965 +#include <linux/netfilter_ipv4/ip_tables.h>
5966 +#include <linux/netfilter_ipv4/ip_set.h>
5967 +#include <linux/errno.h>
5968 +#include <asm/uaccess.h>
5969 +#include <asm/bitops.h>
5970 +#include <linux/spinlock.h>
5971 +#include <linux/if_ether.h>
5972 +#include <linux/vmalloc.h>
5974 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
5975 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
5978 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5980 + struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
5981 + struct ip_set_macip *table = (struct ip_set_macip *) map->members;
5982 + struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
5984 + if (size != sizeof(struct ip_set_req_macipmap)) {
5985 + ip_set_printk("data length wrong (want %zu, have %zu)",
5986 + sizeof(struct ip_set_req_macipmap),
5991 + if (req->ip < map->first_ip || req->ip > map->last_ip)
5994 + *hash_ip = req->ip;
5995 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
5996 + set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
5997 + if (test_bit(IPSET_MACIP_ISSET,
5998 + (void *) &table[req->ip - map->first_ip].flags)) {
5999 + return (memcmp(req->ethernet,
6000 + &table[req->ip - map->first_ip].ethernet,
6003 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6008 +testip_kernel(struct ip_set *set,
6009 + const struct sk_buff *skb,
6010 + ip_set_ip_t *hash_ip,
6011 + const u_int32_t *flags,
6012 + unsigned char index)
6014 + struct ip_set_macipmap *map =
6015 + (struct ip_set_macipmap *) set->data;
6016 + struct ip_set_macip *table =
6017 + (struct ip_set_macip *) map->members;
6020 + ip = ntohl(flags[index] & IPSET_SRC
6021 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6022 + ? ip_hdr(skb)->saddr
6023 + : ip_hdr(skb)->daddr);
6025 + ? skb->nh.iph->saddr
6026 + : skb->nh.iph->daddr);
6029 + if (ip < map->first_ip || ip > map->last_ip)
6033 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
6034 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
6035 + if (test_bit(IPSET_MACIP_ISSET,
6036 + (void *) &table[ip - map->first_ip].flags)) {
6037 + /* Is mac pointer valid?
6038 + * If so, compare... */
6039 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6040 + return (skb_mac_header(skb) >= skb->head
6041 + && (skb_mac_header(skb) + ETH_HLEN) <= skb->data
6043 + return (skb->mac.raw >= skb->head
6044 + && (skb->mac.raw + ETH_HLEN) <= skb->data
6046 + && (memcmp(eth_hdr(skb)->h_source,
6047 + &table[ip - map->first_ip].ethernet,
6050 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6054 +/* returns 0 on success */
6056 +__addip(struct ip_set *set,
6057 + ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
6059 + struct ip_set_macipmap *map =
6060 + (struct ip_set_macipmap *) set->data;
6061 + struct ip_set_macip *table =
6062 + (struct ip_set_macip *) map->members;
6064 + if (ip < map->first_ip || ip > map->last_ip)
6066 + if (test_and_set_bit(IPSET_MACIP_ISSET,
6067 + (void *) &table[ip - map->first_ip].flags))
6071 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6072 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
6077 +addip(struct ip_set *set, const void *data, size_t size,
6078 + ip_set_ip_t *hash_ip)
6080 + struct ip_set_req_macipmap *req =
6081 + (struct ip_set_req_macipmap *) data;
6083 + if (size != sizeof(struct ip_set_req_macipmap)) {
6084 + ip_set_printk("data length wrong (want %zu, have %zu)",
6085 + sizeof(struct ip_set_req_macipmap),
6089 + return __addip(set, req->ip, req->ethernet, hash_ip);
6093 +addip_kernel(struct ip_set *set,
6094 + const struct sk_buff *skb,
6095 + ip_set_ip_t *hash_ip,
6096 + const u_int32_t *flags,
6097 + unsigned char index)
6101 + ip = ntohl(flags[index] & IPSET_SRC
6102 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6103 + ? ip_hdr(skb)->saddr
6104 + : ip_hdr(skb)->daddr);
6106 + ? skb->nh.iph->saddr
6107 + : skb->nh.iph->daddr);
6110 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6111 + if (!(skb_mac_header(skb) >= skb->head
6112 + && (skb_mac_header(skb) + ETH_HLEN) <= skb->data))
6114 + if (!(skb->mac.raw >= skb->head
6115 + && (skb->mac.raw + ETH_HLEN) <= skb->data))
6119 + return __addip(set, ip, eth_hdr(skb)->h_source, hash_ip);
6123 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6125 + struct ip_set_macipmap *map =
6126 + (struct ip_set_macipmap *) set->data;
6127 + struct ip_set_macip *table =
6128 + (struct ip_set_macip *) map->members;
6130 + if (ip < map->first_ip || ip > map->last_ip)
6132 + if (!test_and_clear_bit(IPSET_MACIP_ISSET,
6133 + (void *)&table[ip - map->first_ip].flags))
6137 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6142 +delip(struct ip_set *set, const void *data, size_t size,
6143 + ip_set_ip_t *hash_ip)
6145 + struct ip_set_req_macipmap *req =
6146 + (struct ip_set_req_macipmap *) data;
6148 + if (size != sizeof(struct ip_set_req_macipmap)) {
6149 + ip_set_printk("data length wrong (want %zu, have %zu)",
6150 + sizeof(struct ip_set_req_macipmap),
6154 + return __delip(set, req->ip, hash_ip);
6158 +delip_kernel(struct ip_set *set,
6159 + const struct sk_buff *skb,
6160 + ip_set_ip_t *hash_ip,
6161 + const u_int32_t *flags,
6162 + unsigned char index)
6164 + return __delip(set,
6165 + ntohl(flags[index] & IPSET_SRC
6166 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6167 + ? ip_hdr(skb)->saddr
6168 + : ip_hdr(skb)->daddr),
6170 + ? skb->nh.iph->saddr
6171 + : skb->nh.iph->daddr),
6176 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
6178 + return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
6181 +static int create(struct ip_set *set, const void *data, size_t size)
6184 + struct ip_set_req_macipmap_create *req =
6185 + (struct ip_set_req_macipmap_create *) data;
6186 + struct ip_set_macipmap *map;
6188 + if (size != sizeof(struct ip_set_req_macipmap_create)) {
6189 + ip_set_printk("data length wrong (want %zu, have %zu)",
6190 + sizeof(struct ip_set_req_macipmap_create),
6195 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
6196 + HIPQUAD(req->from), HIPQUAD(req->to));
6198 + if (req->from > req->to) {
6199 + DP("bad ip range");
6203 + if (req->to - req->from > MAX_RANGE) {
6204 + ip_set_printk("range too big (max %d addresses)",
6209 + map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
6211 + DP("out of memory for %d bytes",
6212 + sizeof(struct ip_set_macipmap));
6215 + map->flags = req->flags;
6216 + map->first_ip = req->from;
6217 + map->last_ip = req->to;
6218 + newbytes = members_size(map->first_ip, map->last_ip);
6219 + map->members = ip_set_malloc(newbytes);
6220 + DP("members: %u %p", newbytes, map->members);
6221 + if (!map->members) {
6222 + DP("out of memory for %d bytes", newbytes);
6226 + memset(map->members, 0, newbytes);
6232 +static void destroy(struct ip_set *set)
6234 + struct ip_set_macipmap *map =
6235 + (struct ip_set_macipmap *) set->data;
6237 + ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
6243 +static void flush(struct ip_set *set)
6245 + struct ip_set_macipmap *map =
6246 + (struct ip_set_macipmap *) set->data;
6247 + memset(map->members, 0, members_size(map->first_ip, map->last_ip));
6250 +static void list_header(const struct ip_set *set, void *data)
6252 + struct ip_set_macipmap *map =
6253 + (struct ip_set_macipmap *) set->data;
6254 + struct ip_set_req_macipmap_create *header =
6255 + (struct ip_set_req_macipmap_create *) data;
6257 + DP("list_header %x %x %u", map->first_ip, map->last_ip,
6260 + header->from = map->first_ip;
6261 + header->to = map->last_ip;
6262 + header->flags = map->flags;
6265 +static int list_members_size(const struct ip_set *set)
6267 + struct ip_set_macipmap *map =
6268 + (struct ip_set_macipmap *) set->data;
6270 + DP("%u", members_size(map->first_ip, map->last_ip));
6271 + return members_size(map->first_ip, map->last_ip);
6274 +static void list_members(const struct ip_set *set, void *data)
6276 + struct ip_set_macipmap *map =
6277 + (struct ip_set_macipmap *) set->data;
6279 + int bytes = members_size(map->first_ip, map->last_ip);
6281 + DP("members: %u %p", bytes, map->members);
6282 + memcpy(data, map->members, bytes);
6285 +static struct ip_set_type ip_set_macipmap = {
6286 + .typename = SETTYPE_NAME,
6287 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6288 + .protocol_version = IP_SET_PROTOCOL_VERSION,
6289 + .create = &create,
6290 + .destroy = &destroy,
6292 + .reqsize = sizeof(struct ip_set_req_macipmap),
6294 + .addip_kernel = &addip_kernel,
6296 + .delip_kernel = &delip_kernel,
6297 + .testip = &testip,
6298 + .testip_kernel = &testip_kernel,
6299 + .header_size = sizeof(struct ip_set_req_macipmap_create),
6300 + .list_header = &list_header,
6301 + .list_members_size = &list_members_size,
6302 + .list_members = &list_members,
6303 + .me = THIS_MODULE,
6306 +MODULE_LICENSE("GPL");
6307 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6308 +MODULE_DESCRIPTION("macipmap type of IP sets");
6310 +static int __init ip_set_macipmap_init(void)
6312 + init_max_malloc_size();
6313 + return ip_set_register_set_type(&ip_set_macipmap);
6316 +static void __exit ip_set_macipmap_fini(void)
6318 + /* FIXME: possible race with ip_set_create() */
6319 + ip_set_unregister_set_type(&ip_set_macipmap);
6322 +module_init(ip_set_macipmap_init);
6323 +module_exit(ip_set_macipmap_fini);
6325 +++ b/net/ipv4/netfilter/ip_set_nethash.c
6327 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6329 + * This program is free software; you can redistribute it and/or modify
6330 + * it under the terms of the GNU General Public License version 2 as
6331 + * published by the Free Software Foundation.
6334 +/* Kernel module implementing a cidr nethash set */
6336 +#include <linux/module.h>
6337 +#include <linux/ip.h>
6338 +#include <linux/skbuff.h>
6339 +#include <linux/version.h>
6340 +#include <linux/jhash.h>
6341 +#include <linux/netfilter_ipv4/ip_tables.h>
6342 +#include <linux/netfilter_ipv4/ip_set.h>
6343 +#include <linux/errno.h>
6344 +#include <asm/uaccess.h>
6345 +#include <asm/bitops.h>
6346 +#include <linux/spinlock.h>
6347 +#include <linux/vmalloc.h>
6348 +#include <linux/random.h>
6350 +#include <net/ip.h>
6352 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
6353 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
6355 +static int limit = MAX_RANGE;
6357 +static inline __u32
6358 +jhash_ip(const struct ip_set_nethash *map, uint16_t i, ip_set_ip_t ip)
6360 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
6363 +static inline __u32
6364 +hash_id_cidr(struct ip_set_nethash *map,
6366 + unsigned char cidr,
6367 + ip_set_ip_t *hash_ip)
6371 + ip_set_ip_t *elem;
6373 + *hash_ip = pack(ip, cidr);
6375 + for (i = 0; i < map->probes; i++) {
6376 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
6377 + DP("hash key: %u", id);
6378 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6379 + if (*elem == *hash_ip)
6385 +static inline __u32
6386 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6388 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6389 + __u32 id = UINT_MAX;
6392 + for (i = 0; i < 30 && map->cidr[i]; i++) {
6393 + id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
6394 + if (id != UINT_MAX)
6401 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
6402 + ip_set_ip_t *hash_ip)
6404 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6406 + return (ip && hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
6410 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6412 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
6416 +testip(struct ip_set *set, const void *data, size_t size,
6417 + ip_set_ip_t *hash_ip)
6419 + struct ip_set_req_nethash *req =
6420 + (struct ip_set_req_nethash *) data;
6422 + if (size != sizeof(struct ip_set_req_nethash)) {
6423 + ip_set_printk("data length wrong (want %zu, have %zu)",
6424 + sizeof(struct ip_set_req_nethash),
6428 + return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
6429 + : __testip_cidr(set, req->ip, req->cidr, hash_ip));
6433 +testip_kernel(struct ip_set *set,
6434 + const struct sk_buff *skb,
6435 + ip_set_ip_t *hash_ip,
6436 + const u_int32_t *flags,
6437 + unsigned char index)
6439 + return __testip(set,
6440 + ntohl(flags[index] & IPSET_SRC
6441 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6442 + ? ip_hdr(skb)->saddr
6443 + : ip_hdr(skb)->daddr),
6445 + ? skb->nh.iph->saddr
6446 + : skb->nh.iph->daddr),
6452 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
6456 + ip_set_ip_t *elem;
6458 + for (i = 0; i < map->probes; i++) {
6459 + probe = jhash_ip(map, i, ip) % map->hashsize;
6460 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
6469 + /* Trigger rehashing */
6474 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6475 + ip_set_ip_t *hash_ip)
6477 + if (!ip || map->elements >= limit)
6480 + *hash_ip = pack(ip, cidr);
6481 + DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
6483 + return __addip_base(map, *hash_ip);
6487 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
6489 + unsigned char next;
6492 + for (i = 0; i < 30 && map->cidr[i]; i++) {
6493 + if (map->cidr[i] == cidr) {
6495 + } else if (map->cidr[i] < cidr) {
6496 + next = map->cidr[i];
6497 + map->cidr[i] = cidr;
6502 + map->cidr[i] = cidr;
6506 +addip(struct ip_set *set, const void *data, size_t size,
6507 + ip_set_ip_t *hash_ip)
6509 + struct ip_set_req_nethash *req =
6510 + (struct ip_set_req_nethash *) data;
6513 + if (size != sizeof(struct ip_set_req_nethash)) {
6514 + ip_set_printk("data length wrong (want %zu, have %zu)",
6515 + sizeof(struct ip_set_req_nethash),
6519 + ret = __addip((struct ip_set_nethash *) set->data,
6520 + req->ip, req->cidr, hash_ip);
6523 + update_cidr_sizes((struct ip_set_nethash *) set->data,
6530 +addip_kernel(struct ip_set *set,
6531 + const struct sk_buff *skb,
6532 + ip_set_ip_t *hash_ip,
6533 + const u_int32_t *flags,
6534 + unsigned char index)
6536 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6537 + int ret = -ERANGE;
6538 + ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC
6539 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6540 + ? ip_hdr(skb)->saddr
6541 + : ip_hdr(skb)->daddr);
6543 + ? skb->nh.iph->saddr
6544 + : skb->nh.iph->daddr);
6548 + ret = __addip(map, ip, map->cidr[0], hash_ip);
6553 +static int retry(struct ip_set *set)
6555 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6556 + ip_set_ip_t *elem;
6558 + u_int32_t i, hashsize = map->hashsize;
6560 + struct ip_set_nethash *tmp;
6562 + if (map->resize == 0)
6568 + /* Calculate new parameters */
6569 + hashsize += (hashsize * map->resize)/100;
6570 + if (hashsize == map->hashsize)
6573 + ip_set_printk("rehashing of set %s triggered: "
6574 + "hashsize grows from %u to %u",
6575 + set->name, map->hashsize, hashsize);
6577 + tmp = kmalloc(sizeof(struct ip_set_nethash)
6578 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
6580 + DP("out of memory for %d bytes",
6581 + sizeof(struct ip_set_nethash)
6582 + + map->probes * sizeof(uint32_t));
6585 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
6586 + if (!tmp->members) {
6587 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
6591 + tmp->hashsize = hashsize;
6592 + tmp->elements = 0;
6593 + tmp->probes = map->probes;
6594 + tmp->resize = map->resize;
6595 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
6596 + memcpy(tmp->cidr, map->cidr, 30 * sizeof(unsigned char));
6598 + write_lock_bh(&set->lock);
6599 + map = (struct ip_set_nethash *) set->data; /* Play safe */
6600 + for (i = 0; i < map->hashsize && res == 0; i++) {
6601 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
6603 + res = __addip_base(tmp, *elem);
6606 + /* Failure, try again */
6607 + write_unlock_bh(&set->lock);
6608 + harray_free(tmp->members);
6613 + /* Success at resizing! */
6614 + members = map->members;
6616 + map->hashsize = tmp->hashsize;
6617 + map->members = tmp->members;
6618 + write_unlock_bh(&set->lock);
6620 + harray_free(members);
6627 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6628 + ip_set_ip_t *hash_ip)
6630 + ip_set_ip_t id, *elem;
6635 + id = hash_id_cidr(map, ip, cidr, hash_ip);
6636 + if (id == UINT_MAX)
6639 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6646 +delip(struct ip_set *set, const void *data, size_t size,
6647 + ip_set_ip_t *hash_ip)
6649 + struct ip_set_req_nethash *req =
6650 + (struct ip_set_req_nethash *) data;
6652 + if (size != sizeof(struct ip_set_req_nethash)) {
6653 + ip_set_printk("data length wrong (want %zu, have %zu)",
6654 + sizeof(struct ip_set_req_nethash),
6658 + /* TODO: no garbage collection in map->cidr */
6659 + return __delip((struct ip_set_nethash *) set->data,
6660 + req->ip, req->cidr, hash_ip);
6664 +delip_kernel(struct ip_set *set,
6665 + const struct sk_buff *skb,
6666 + ip_set_ip_t *hash_ip,
6667 + const u_int32_t *flags,
6668 + unsigned char index)
6670 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6671 + int ret = -ERANGE;
6672 + ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC
6673 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6674 + ? ip_hdr(skb)->saddr
6675 + : ip_hdr(skb)->daddr);
6677 + ? skb->nh.iph->saddr
6678 + : skb->nh.iph->daddr);
6682 + ret = __delip(map, ip, map->cidr[0], hash_ip);
6687 +static int create(struct ip_set *set, const void *data, size_t size)
6689 + struct ip_set_req_nethash_create *req =
6690 + (struct ip_set_req_nethash_create *) data;
6691 + struct ip_set_nethash *map;
6694 + if (size != sizeof(struct ip_set_req_nethash_create)) {
6695 + ip_set_printk("data length wrong (want %zu, have %zu)",
6696 + sizeof(struct ip_set_req_nethash_create),
6701 + if (req->hashsize < 1) {
6702 + ip_set_printk("hashsize too small");
6705 + if (req->probes < 1) {
6706 + ip_set_printk("probes too small");
6710 + map = kmalloc(sizeof(struct ip_set_nethash)
6711 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
6713 + DP("out of memory for %d bytes",
6714 + sizeof(struct ip_set_nethash)
6715 + + req->probes * sizeof(uint32_t));
6718 + for (i = 0; i < req->probes; i++)
6719 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
6720 + map->elements = 0;
6721 + map->hashsize = req->hashsize;
6722 + map->probes = req->probes;
6723 + map->resize = req->resize;
6724 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
6725 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
6726 + if (!map->members) {
6727 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
6736 +static void destroy(struct ip_set *set)
6738 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6740 + harray_free(map->members);
6746 +static void flush(struct ip_set *set)
6748 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6749 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
6750 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
6751 + map->elements = 0;
6754 +static void list_header(const struct ip_set *set, void *data)
6756 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6757 + struct ip_set_req_nethash_create *header =
6758 + (struct ip_set_req_nethash_create *) data;
6760 + header->hashsize = map->hashsize;
6761 + header->probes = map->probes;
6762 + header->resize = map->resize;
6765 +static int list_members_size(const struct ip_set *set)
6767 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6769 + return (map->hashsize * sizeof(ip_set_ip_t));
6772 +static void list_members(const struct ip_set *set, void *data)
6774 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6775 + ip_set_ip_t i, *elem;
6777 + for (i = 0; i < map->hashsize; i++) {
6778 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
6779 + ((ip_set_ip_t *)data)[i] = *elem;
6783 +static struct ip_set_type ip_set_nethash = {
6784 + .typename = SETTYPE_NAME,
6785 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6786 + .protocol_version = IP_SET_PROTOCOL_VERSION,
6787 + .create = &create,
6788 + .destroy = &destroy,
6790 + .reqsize = sizeof(struct ip_set_req_nethash),
6792 + .addip_kernel = &addip_kernel,
6795 + .delip_kernel = &delip_kernel,
6796 + .testip = &testip,
6797 + .testip_kernel = &testip_kernel,
6798 + .header_size = sizeof(struct ip_set_req_nethash_create),
6799 + .list_header = &list_header,
6800 + .list_members_size = &list_members_size,
6801 + .list_members = &list_members,
6802 + .me = THIS_MODULE,
6805 +MODULE_LICENSE("GPL");
6806 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6807 +MODULE_DESCRIPTION("nethash type of IP sets");
6808 +module_param(limit, int, 0600);
6809 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
6811 +static int __init ip_set_nethash_init(void)
6813 + return ip_set_register_set_type(&ip_set_nethash);
6816 +static void __exit ip_set_nethash_fini(void)
6818 + /* FIXME: possible race with ip_set_create() */
6819 + ip_set_unregister_set_type(&ip_set_nethash);
6822 +module_init(ip_set_nethash_init);
6823 +module_exit(ip_set_nethash_fini);
6825 +++ b/net/ipv4/netfilter/ip_set_portmap.c
6827 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6829 + * This program is free software; you can redistribute it and/or modify
6830 + * it under the terms of the GNU General Public License version 2 as
6831 + * published by the Free Software Foundation.
6834 +/* Kernel module implementing a port set type as a bitmap */
6836 +#include <linux/module.h>
6837 +#include <linux/ip.h>
6838 +#include <linux/tcp.h>
6839 +#include <linux/udp.h>
6840 +#include <linux/skbuff.h>
6841 +#include <linux/version.h>
6842 +#include <linux/netfilter_ipv4/ip_tables.h>
6843 +#include <linux/netfilter_ipv4/ip_set.h>
6844 +#include <linux/errno.h>
6845 +#include <asm/uaccess.h>
6846 +#include <asm/bitops.h>
6847 +#include <linux/spinlock.h>
6849 +#include <net/ip.h>
6851 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
6853 +/* We must handle non-linear skbs */
6854 +static inline ip_set_ip_t
6855 +get_port(const struct sk_buff *skb, u_int32_t flags)
6857 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6858 + struct iphdr *iph = ip_hdr(skb);
6860 + struct iphdr *iph = skb->nh.iph;
6862 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
6863 + switch (iph->protocol) {
6864 + case IPPROTO_TCP: {
6865 + struct tcphdr tcph;
6867 + /* See comments at tcp_match in ip_tables.c */
6869 + return INVALID_PORT;
6871 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6872 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
6874 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
6876 + /* No choice either */
6877 + return INVALID_PORT;
6879 + return ntohs(flags & IPSET_SRC ?
6880 + tcph.source : tcph.dest);
6882 + case IPPROTO_UDP: {
6883 + struct udphdr udph;
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, &udph, sizeof(udph)) < 0)
6891 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
6893 + /* No choice either */
6894 + return INVALID_PORT;
6896 + return ntohs(flags & IPSET_SRC ?
6897 + udph.source : udph.dest);
6900 + return INVALID_PORT;
6905 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
6907 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
6909 + if (port < map->first_port || port > map->last_port)
6912 + *hash_port = port;
6913 + DP("set: %s, port:%u, %u", set->name, port, *hash_port);
6914 + return !!test_bit(port - map->first_port, map->members);
6918 +testport(struct ip_set *set, const void *data, size_t size,
6919 + ip_set_ip_t *hash_port)
6921 + struct ip_set_req_portmap *req =
6922 + (struct ip_set_req_portmap *) data;
6924 + if (size != sizeof(struct ip_set_req_portmap)) {
6925 + ip_set_printk("data length wrong (want %zu, have %zu)",
6926 + sizeof(struct ip_set_req_portmap),
6930 + return __testport(set, req->port, hash_port);
6934 +testport_kernel(struct ip_set *set,
6935 + const struct sk_buff *skb,
6936 + ip_set_ip_t *hash_port,
6937 + const u_int32_t *flags,
6938 + unsigned char index)
6941 + ip_set_ip_t port = get_port(skb, flags[index]);
6943 + DP("flag %s port %u", flags[index] & IPSET_SRC ? "SRC" : "DST", port);
6944 + if (port == INVALID_PORT)
6947 + res = __testport(set, port, hash_port);
6949 + return (res < 0 ? 0 : res);
6953 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
6955 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
6957 + if (port < map->first_port || port > map->last_port)
6959 + if (test_and_set_bit(port - map->first_port, map->members))
6962 + *hash_port = port;
6963 + DP("port %u", port);
6968 +addport(struct ip_set *set, const void *data, size_t size,
6969 + ip_set_ip_t *hash_port)
6971 + struct ip_set_req_portmap *req =
6972 + (struct ip_set_req_portmap *) data;
6974 + if (size != sizeof(struct ip_set_req_portmap)) {
6975 + ip_set_printk("data length wrong (want %zu, have %zu)",
6976 + sizeof(struct ip_set_req_portmap),
6980 + return __addport(set, req->port, hash_port);
6984 +addport_kernel(struct ip_set *set,
6985 + const struct sk_buff *skb,
6986 + ip_set_ip_t *hash_port,
6987 + const u_int32_t *flags,
6988 + unsigned char index)
6990 + ip_set_ip_t port = get_port(skb, flags[index]);
6992 + if (port == INVALID_PORT)
6995 + return __addport(set, port, hash_port);
6999 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
7001 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7003 + if (port < map->first_port || port > map->last_port)
7005 + if (!test_and_clear_bit(port - map->first_port, map->members))
7008 + *hash_port = port;
7009 + DP("port %u", port);
7014 +delport(struct ip_set *set, const void *data, size_t size,
7015 + ip_set_ip_t *hash_port)
7017 + struct ip_set_req_portmap *req =
7018 + (struct ip_set_req_portmap *) data;
7020 + if (size != sizeof(struct ip_set_req_portmap)) {
7021 + ip_set_printk("data length wrong (want %zu, have %zu)",
7022 + sizeof(struct ip_set_req_portmap),
7026 + return __delport(set, req->port, hash_port);
7030 +delport_kernel(struct ip_set *set,
7031 + const struct sk_buff *skb,
7032 + ip_set_ip_t *hash_port,
7033 + const u_int32_t *flags,
7034 + unsigned char index)
7036 + ip_set_ip_t port = get_port(skb, flags[index]);
7038 + if (port == INVALID_PORT)
7041 + return __delport(set, port, hash_port);
7044 +static int create(struct ip_set *set, const void *data, size_t size)
7047 + struct ip_set_req_portmap_create *req =
7048 + (struct ip_set_req_portmap_create *) data;
7049 + struct ip_set_portmap *map;
7051 + if (size != sizeof(struct ip_set_req_portmap_create)) {
7052 + ip_set_printk("data length wrong (want %zu, have %zu)",
7053 + sizeof(struct ip_set_req_portmap_create),
7058 + DP("from %u to %u", req->from, req->to);
7060 + if (req->from > req->to) {
7061 + DP("bad port range");
7065 + if (req->to - req->from > MAX_RANGE) {
7066 + ip_set_printk("range too big (max %d ports)",
7071 + map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
7073 + DP("out of memory for %d bytes",
7074 + sizeof(struct ip_set_portmap));
7077 + map->first_port = req->from;
7078 + map->last_port = req->to;
7079 + newbytes = bitmap_bytes(req->from, req->to);
7080 + map->members = kmalloc(newbytes, GFP_KERNEL);
7081 + if (!map->members) {
7082 + DP("out of memory for %d bytes", newbytes);
7086 + memset(map->members, 0, newbytes);
7092 +static void destroy(struct ip_set *set)
7094 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7096 + kfree(map->members);
7102 +static void flush(struct ip_set *set)
7104 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7105 + memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
7108 +static void list_header(const struct ip_set *set, void *data)
7110 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7111 + struct ip_set_req_portmap_create *header =
7112 + (struct ip_set_req_portmap_create *) data;
7114 + DP("list_header %u %u", map->first_port, map->last_port);
7116 + header->from = map->first_port;
7117 + header->to = map->last_port;
7120 +static int list_members_size(const struct ip_set *set)
7122 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7124 + return bitmap_bytes(map->first_port, map->last_port);
7127 +static void list_members(const struct ip_set *set, void *data)
7129 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7130 + int bytes = bitmap_bytes(map->first_port, map->last_port);
7132 + memcpy(data, map->members, bytes);
7135 +static struct ip_set_type ip_set_portmap = {
7136 + .typename = SETTYPE_NAME,
7137 + .features = IPSET_TYPE_PORT | IPSET_DATA_SINGLE,
7138 + .protocol_version = IP_SET_PROTOCOL_VERSION,
7139 + .create = &create,
7140 + .destroy = &destroy,
7142 + .reqsize = sizeof(struct ip_set_req_portmap),
7143 + .addip = &addport,
7144 + .addip_kernel = &addport_kernel,
7145 + .delip = &delport,
7146 + .delip_kernel = &delport_kernel,
7147 + .testip = &testport,
7148 + .testip_kernel = &testport_kernel,
7149 + .header_size = sizeof(struct ip_set_req_portmap_create),
7150 + .list_header = &list_header,
7151 + .list_members_size = &list_members_size,
7152 + .list_members = &list_members,
7153 + .me = THIS_MODULE,
7156 +MODULE_LICENSE("GPL");
7157 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7158 +MODULE_DESCRIPTION("portmap type of IP sets");
7160 +static int __init ip_set_portmap_init(void)
7162 + return ip_set_register_set_type(&ip_set_portmap);
7165 +static void __exit ip_set_portmap_fini(void)
7167 + /* FIXME: possible race with ip_set_create() */
7168 + ip_set_unregister_set_type(&ip_set_portmap);
7171 +module_init(ip_set_portmap_init);
7172 +module_exit(ip_set_portmap_fini);
7174 +++ b/net/ipv4/netfilter/ipt_set.c
7176 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7177 + * Patrick Schaaf <bof@bof.de>
7178 + * Martin Josefsson <gandalf@wlug.westbo.se>
7179 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7181 + * This program is free software; you can redistribute it and/or modify
7182 + * it under the terms of the GNU General Public License version 2 as
7183 + * published by the Free Software Foundation.
7186 +/* Kernel module to match an IP set. */
7188 +#include <linux/module.h>
7189 +#include <linux/ip.h>
7190 +#include <linux/skbuff.h>
7191 +#include <linux/version.h>
7193 +#include <linux/netfilter_ipv4/ip_tables.h>
7194 +#include <linux/netfilter_ipv4/ip_set.h>
7195 +#include <linux/netfilter_ipv4/ipt_set.h>
7198 +match_set(const struct ipt_set_info *info,
7199 + const struct sk_buff *skb,
7202 + if (ip_set_testip_kernel(info->index, skb, info->flags))
7207 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7212 +match(const struct sk_buff *skb,
7213 + const struct net_device *in,
7214 + const struct net_device *out,
7215 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7216 + const struct xt_match *match,
7218 + const void *matchinfo,
7219 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7220 + int offset, unsigned int protoff, bool *hotdrop)
7221 +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7222 + int offset, unsigned int protoff, int *hotdrop)
7224 + int offset, int *hotdrop)
7227 + const struct ipt_set_info_match *info = matchinfo;
7229 + return match_set(&info->match_set,
7231 + info->match_set.flags[0] & IPSET_MATCH_INV);
7234 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7239 +checkentry(const char *tablename,
7240 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7243 + const struct ipt_ip *ip,
7245 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7246 + const struct xt_match *match,
7249 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7250 + unsigned int matchsize,
7252 + unsigned int hook_mask)
7254 + struct ipt_set_info_match *info =
7255 + (struct ipt_set_info_match *) matchinfo;
7256 + ip_set_id_t index;
7258 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7259 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7260 + ip_set_printk("invalid matchsize %d", matchsize);
7265 + index = ip_set_get_byindex(info->match_set.index);
7267 + if (index == IP_SET_INVALID_ID) {
7268 + ip_set_printk("Cannot find set indentified by id %u to match",
7269 + info->match_set.index);
7270 + return 0; /* error */
7272 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7273 + ip_set_printk("That's nasty!");
7274 + return 0; /* error */
7280 +static void destroy(
7281 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7282 + const struct xt_match *match,
7284 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7285 + void *matchinfo, unsigned int matchsize)
7290 + struct ipt_set_info_match *info = matchinfo;
7292 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7293 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7294 + ip_set_printk("invalid matchsize %d", matchsize);
7298 + ip_set_put(info->match_set.index);
7301 +static struct ipt_match set_match = {
7303 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7304 + .family = AF_INET,
7307 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7308 + .matchsize = sizeof(struct ipt_set_info_match),
7310 + .checkentry = &checkentry,
7311 + .destroy = &destroy,
7315 +MODULE_LICENSE("GPL");
7316 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7317 +MODULE_DESCRIPTION("iptables IP set match module");
7319 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7320 +#define ipt_register_match xt_register_match
7321 +#define ipt_unregister_match xt_unregister_match
7324 +static int __init ipt_ipset_init(void)
7326 + return ipt_register_match(&set_match);
7329 +static void __exit ipt_ipset_fini(void)
7331 + ipt_unregister_match(&set_match);
7334 +module_init(ipt_ipset_init);
7335 +module_exit(ipt_ipset_fini);
7337 +++ b/net/ipv4/netfilter/ipt_SET.c
7339 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7340 + * Patrick Schaaf <bof@bof.de>
7341 + * Martin Josefsson <gandalf@wlug.westbo.se>
7342 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7344 + * This program is free software; you can redistribute it and/or modify
7345 + * it under the terms of the GNU General Public License version 2 as
7346 + * published by the Free Software Foundation.
7349 +/* ipt_SET.c - netfilter target to manipulate IP sets */
7351 +#include <linux/types.h>
7352 +#include <linux/ip.h>
7353 +#include <linux/timer.h>
7354 +#include <linux/module.h>
7355 +#include <linux/netfilter.h>
7356 +#include <linux/netdevice.h>
7357 +#include <linux/if.h>
7358 +#include <linux/inetdevice.h>
7359 +#include <linux/version.h>
7360 +#include <net/protocol.h>
7361 +#include <net/checksum.h>
7362 +#include <linux/netfilter_ipv4.h>
7363 +#include <linux/netfilter_ipv4/ip_tables.h>
7364 +#include <linux/netfilter_ipv4/ipt_set.h>
7366 +static unsigned int
7367 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
7368 +target(struct sk_buff *skb,
7370 +target(struct sk_buff **pskb,
7372 + const struct net_device *in,
7373 + const struct net_device *out,
7374 + unsigned int hooknum,
7375 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7376 + const struct xt_target *target,
7378 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7379 + const void *targinfo,
7382 + const void *targinfo)
7385 + const struct ipt_set_info_target *info = targinfo;
7386 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
7387 + struct sk_buff *skb = *pskb;
7390 + if (info->add_set.index != IP_SET_INVALID_ID)
7391 + ip_set_addip_kernel(info->add_set.index,
7393 + info->add_set.flags);
7394 + if (info->del_set.index != IP_SET_INVALID_ID)
7395 + ip_set_delip_kernel(info->del_set.index,
7397 + info->del_set.flags);
7399 + return IPT_CONTINUE;
7402 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7407 +checkentry(const char *tablename,
7408 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7411 + const struct ipt_entry *e,
7413 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7414 + const struct xt_target *target,
7417 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7418 + unsigned int targinfosize,
7420 + unsigned int hook_mask)
7422 + struct ipt_set_info_target *info =
7423 + (struct ipt_set_info_target *) targinfo;
7424 + ip_set_id_t index;
7426 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7427 + if (targinfosize != IPT_ALIGN(sizeof(*info))) {
7428 + DP("bad target info size %u", targinfosize);
7433 + if (info->add_set.index != IP_SET_INVALID_ID) {
7434 + index = ip_set_get_byindex(info->add_set.index);
7435 + if (index == IP_SET_INVALID_ID) {
7436 + ip_set_printk("cannot find add_set index %u as target",
7437 + info->add_set.index);
7438 + return 0; /* error */
7442 + if (info->del_set.index != IP_SET_INVALID_ID) {
7443 + index = ip_set_get_byindex(info->del_set.index);
7444 + if (index == IP_SET_INVALID_ID) {
7445 + ip_set_printk("cannot find del_set index %u as target",
7446 + info->del_set.index);
7447 + return 0; /* error */
7450 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
7451 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7452 + ip_set_printk("That's nasty!");
7453 + return 0; /* error */
7459 +static void destroy(
7460 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7461 + const struct xt_target *target,
7463 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7464 + void *targetinfo, unsigned int targetsize)
7469 + struct ipt_set_info_target *info = targetinfo;
7471 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7472 + if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
7473 + ip_set_printk("invalid targetsize %d", targetsize);
7477 + if (info->add_set.index != IP_SET_INVALID_ID)
7478 + ip_set_put(info->add_set.index);
7479 + if (info->del_set.index != IP_SET_INVALID_ID)
7480 + ip_set_put(info->del_set.index);
7483 +static struct ipt_target SET_target = {
7485 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7486 + .family = AF_INET,
7489 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7490 + .targetsize = sizeof(struct ipt_set_info_target),
7492 + .checkentry = checkentry,
7493 + .destroy = destroy,
7497 +MODULE_LICENSE("GPL");
7498 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7499 +MODULE_DESCRIPTION("iptables IP set target module");
7501 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7502 +#define ipt_register_target xt_register_target
7503 +#define ipt_unregister_target xt_unregister_target
7506 +static int __init ipt_SET_init(void)
7508 + return ipt_register_target(&SET_target);
7511 +static void __exit ipt_SET_fini(void)
7513 + ipt_unregister_target(&SET_target);
7516 +module_init(ipt_SET_init);
7517 +module_exit(ipt_SET_fini);
7518 --- a/net/ipv4/netfilter/Kconfig
7519 +++ b/net/ipv4/netfilter/Kconfig
7520 @@ -385,5 +385,122 @@ config IP_NF_ARP_MANGLE
7521 Allows altering the ARP packet payload: source and destination
7522 hardware and network addresses.
7525 + tristate "IP set support"
7526 + depends on INET && NETFILTER
7528 + This option adds IP set support to the kernel.
7529 + In order to define and use sets, you need the userspace utility
7532 + To compile it as a module, choose M here. If unsure, say N.
7534 +config IP_NF_SET_MAX
7535 + int "Maximum number of IP sets"
7538 + depends on IP_NF_SET
7540 + You can define here default value of the maximum number
7541 + of IP sets for the kernel.
7543 + The value can be overriden by the 'max_sets' module
7544 + parameter of the 'ip_set' module.
7546 +config IP_NF_SET_HASHSIZE
7547 + int "Hash size for bindings of IP sets"
7549 + depends on IP_NF_SET
7551 + You can define here default value of the hash size for
7552 + bindings of IP sets.
7554 + The value can be overriden by the 'hash_size' module
7555 + parameter of the 'ip_set' module.
7557 +config IP_NF_SET_IPMAP
7558 + tristate "ipmap set support"
7559 + depends on IP_NF_SET
7561 + This option adds the ipmap set type support.
7563 + To compile it as a module, choose M here. If unsure, say N.
7565 +config IP_NF_SET_MACIPMAP
7566 + tristate "macipmap set support"
7567 + depends on IP_NF_SET
7569 + This option adds the macipmap set type support.
7571 + To compile it as a module, choose M here. If unsure, say N.
7573 +config IP_NF_SET_PORTMAP
7574 + tristate "portmap set support"
7575 + depends on IP_NF_SET
7577 + This option adds the portmap set type support.
7579 + To compile it as a module, choose M here. If unsure, say N.
7581 +config IP_NF_SET_IPHASH
7582 + tristate "iphash set support"
7583 + depends on IP_NF_SET
7585 + This option adds the iphash set type support.
7587 + To compile it as a module, choose M here. If unsure, say N.
7589 +config IP_NF_SET_NETHASH
7590 + tristate "nethash set support"
7591 + depends on IP_NF_SET
7593 + This option adds the nethash set type support.
7595 + To compile it as a module, choose M here. If unsure, say N.
7597 +config IP_NF_SET_IPPORTHASH
7598 + tristate "ipporthash set support"
7599 + depends on IP_NF_SET
7601 + This option adds the ipporthash set type support.
7603 + To compile it as a module, choose M here. If unsure, say N.
7605 +config IP_NF_SET_IPTREE
7606 + tristate "iptree set support"
7607 + depends on IP_NF_SET
7609 + This option adds the iptree set type support.
7611 + To compile it as a module, choose M here. If unsure, say N.
7613 +config IP_NF_SET_IPTREEMAP
7614 + tristate "iptreemap set support"
7615 + depends on IP_NF_SET
7617 + This option adds the iptreemap set type support.
7619 + To compile it as a module, choose M here. If unsure, say N.
7621 +config IP_NF_MATCH_SET
7622 + tristate "set match support"
7623 + depends on IP_NF_SET
7625 + Set matching matches against given IP sets.
7626 + You need the ipset utility to create and set up the sets.
7628 + To compile it as a module, choose M here. If unsure, say N.
7630 +config IP_NF_TARGET_SET
7631 + tristate "SET target support"
7632 + depends on IP_NF_SET
7634 + The SET target makes possible to add/delete entries
7636 + You need the ipset utility to create and set up the sets.
7638 + To compile it as a module, choose M here. If unsure, say N.
7643 --- a/net/ipv4/netfilter/Makefile
7644 +++ b/net/ipv4/netfilter/Makefile
7645 @@ -46,6 +46,7 @@ obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o
7646 obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
7647 obj-$(CONFIG_IP_NF_MATCH_RECENT) += ipt_recent.o
7648 obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
7649 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
7651 obj-$(CONFIG_IP_NF_MATCH_IPP2P) += ipt_ipp2p.o
7653 @@ -59,6 +60,18 @@ obj-$(CONFIG_IP_NF_TARGET_REDIRECT) += i
7654 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
7655 obj-$(CONFIG_IP_NF_TARGET_TTL) += ipt_TTL.o
7656 obj-$(CONFIG_IP_NF_TARGET_ULOG) += ipt_ULOG.o
7657 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
7660 +obj-$(CONFIG_IP_NF_SET) += ip_set.o
7661 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
7662 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
7663 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
7664 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
7665 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
7666 +obj-$(CONFIG_IP_NF_SET_IPPORTHASH) += ip_set_ipporthash.o
7667 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
7668 +obj-$(CONFIG_IP_NF_SET_IPTREEMAP) += ip_set_iptreemap.o
7670 # generic ARP tables
7671 obj-$(CONFIG_IP_NF_ARPTABLES) += arp_tables.o