1 diff --git a/include/linux/netfilter_ipv4/ip_set.h b/include/linux/netfilter_ipv4/ip_set.h
5 +++ b/include/linux/netfilter_ipv4/ip_set.h
10 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
11 + * Patrick Schaaf <bof@bof.de>
12 + * Martin Josefsson <gandalf@wlug.westbo.se>
13 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
15 + * This program is free software; you can redistribute it and/or modify
16 + * it under the terms of the GNU General Public License version 2 as
17 + * published by the Free Software Foundation.
25 + * A sockopt of such quality has hardly ever been seen before on the open
26 + * market! This little beauty, hardly ever used: above 64, so it's
27 + * traditionally used for firewalling, not touched (even once!) by the
28 + * 2.0, 2.2 and 2.4 kernels!
30 + * Comes with its own certificate of authenticity, valid anywhere in the
38 + * Heavily modify by Joakim Axelsson 08.03.2002
39 + * - Made it more modulebased
41 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
43 + * - in order to "deal with" backward compatibility, renamed to ipset
47 + * Used so that the kernel module and ipset-binary can match their versions
49 +#define IP_SET_PROTOCOL_VERSION 2
51 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
53 +/* Lets work with our own typedef for representing an IP address.
54 + * We hope to make the code more portable, possibly to IPv6...
56 + * The representation works in HOST byte order, because most set types
57 + * will perform arithmetic operations and compare operations.
59 + * For now the type is an uint32_t.
61 + * Make sure to ONLY use the functions when translating and parsing
62 + * in order to keep the host byte order and make it more portable:
67 + * (Joakim: where are they???)
70 +typedef uint32_t ip_set_ip_t;
72 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
73 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
75 +typedef uint16_t ip_set_id_t;
77 +#define IP_SET_INVALID_ID 65535
79 +/* How deep we follow bindings */
80 +#define IP_SET_MAX_BINDINGS 6
83 + * Option flags for kernel operations (ipt_set_info)
85 +#define IPSET_SRC 0x01 /* Source match/add */
86 +#define IPSET_DST 0x02 /* Destination match/add */
87 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
92 +#define IPSET_TYPE_IP 0x01 /* IP address type of set */
93 +#define IPSET_TYPE_PORT 0x02 /* Port type of set */
94 +#define IPSET_DATA_SINGLE 0x04 /* Single data storage */
95 +#define IPSET_DATA_DOUBLE 0x08 /* Double data storage */
97 +/* Reserved keywords */
98 +#define IPSET_TOKEN_DEFAULT ":default:"
99 +#define IPSET_TOKEN_ALL ":all:"
101 +/* SO_IP_SET operation constants, and their request struct types.
104 + * 0-99: commands with version checking
105 + * 100-199: add/del/test/bind/unbind
106 + * 200-299: list, save, restore
109 +/* Single shot operations:
110 + * version, create, destroy, flush, rename and swap
112 + * Sets are identified by name.
115 +#define IP_SET_REQ_STD \
117 + unsigned version; \
118 + char name[IP_SET_MAXNAMELEN]
120 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
121 +struct ip_set_req_create {
123 + char typename[IP_SET_MAXNAMELEN];
126 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
127 +struct ip_set_req_std {
131 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
132 +/* Uses ip_set_req_std */
134 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
135 +/* Uses ip_set_req_create */
137 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
138 +/* Uses ip_set_req_create */
140 +union ip_set_name_index {
141 + char name[IP_SET_MAXNAMELEN];
145 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
146 +struct ip_set_req_get_set {
149 + union ip_set_name_index set;
152 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
153 +/* Uses ip_set_req_get_set */
155 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
156 +struct ip_set_req_version {
161 +/* Double shots operations:
162 + * add, del, test, bind and unbind.
164 + * First we query the kernel to get the index and type of the target set,
165 + * then issue the command. Validity of IP is checked in kernel in order
166 + * to minimalize sockopt operations.
169 +/* Get minimal set data for add/del/test/bind/unbind IP */
170 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
171 +struct ip_set_req_adt_get {
174 + union ip_set_name_index set;
175 + char typename[IP_SET_MAXNAMELEN];
178 +#define IP_SET_REQ_BYINDEX \
182 +struct ip_set_req_adt {
183 + IP_SET_REQ_BYINDEX;
186 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
187 +/* Uses ip_set_req_adt, with type specific addage */
189 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
190 +/* Uses ip_set_req_adt, with type specific addage */
192 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
193 +/* Uses ip_set_req_adt, with type specific addage */
195 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
196 +/* Uses ip_set_req_bind, with type specific addage */
197 +struct ip_set_req_bind {
198 + IP_SET_REQ_BYINDEX;
199 + char binding[IP_SET_MAXNAMELEN];
202 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
203 +/* Uses ip_set_req_bind, with type speficic addage
204 + * index = 0 means unbinding for all sets */
206 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
207 +/* Uses ip_set_req_bind, with type specific addage */
209 +/* Multiple shots operations: list, save, restore.
211 + * - check kernel version and query the max number of sets
212 + * - get the basic information on all sets
213 + * and size required for the next step
214 + * - get actual set data: header, data, bindings
217 +/* Get max_sets and the index of a queried set
219 +#define IP_SET_OP_MAX_SETS 0x00000020
220 +struct ip_set_req_max_sets {
223 + ip_set_id_t max_sets; /* max_sets */
224 + ip_set_id_t sets; /* real number of sets */
225 + union ip_set_name_index set; /* index of set if name used */
228 +/* Get the id and name of the sets plus size for next step */
229 +#define IP_SET_OP_LIST_SIZE 0x00000201
230 +#define IP_SET_OP_SAVE_SIZE 0x00000202
231 +struct ip_set_req_setnames {
233 + ip_set_id_t index; /* set to list/save */
234 + size_t size; /* size to get setdata/bindings */
235 + /* followed by sets number of struct ip_set_name_list */
238 +struct ip_set_name_list {
239 + char name[IP_SET_MAXNAMELEN];
240 + char typename[IP_SET_MAXNAMELEN];
245 +/* The actual list operation */
246 +#define IP_SET_OP_LIST 0x00000203
247 +struct ip_set_req_list {
248 + IP_SET_REQ_BYINDEX;
249 + /* sets number of struct ip_set_list in reply */
252 +struct ip_set_list {
254 + ip_set_id_t binding;
256 + size_t header_size; /* Set header data of header_size */
257 + size_t members_size; /* Set members data of members_size */
258 + size_t bindings_size; /* Set bindings data of bindings_size */
261 +struct ip_set_hash_list {
263 + ip_set_id_t binding;
266 +/* The save operation */
267 +#define IP_SET_OP_SAVE 0x00000204
268 +/* Uses ip_set_req_list, in the reply replaced by
269 + * sets number of struct ip_set_save plus a marker
270 + * ip_set_save followed by ip_set_hash_save structures.
272 +struct ip_set_save {
274 + ip_set_id_t binding;
275 + size_t header_size; /* Set header data of header_size */
276 + size_t members_size; /* Set members data of members_size */
279 +/* At restoring, ip == 0 means default binding for the given set: */
280 +struct ip_set_hash_save {
283 + ip_set_id_t binding;
286 +/* The restore operation */
287 +#define IP_SET_OP_RESTORE 0x00000205
288 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
289 + * plus a marker ip_set_restore, followed by ip_set_hash_save
292 +struct ip_set_restore {
293 + char name[IP_SET_MAXNAMELEN];
294 + char typename[IP_SET_MAXNAMELEN];
296 + size_t header_size; /* Create data of header_size */
297 + size_t members_size; /* Set members data of members_size */
300 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
302 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
307 +#define ip_set_printk(format, args...) \
309 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
310 + printk(format "\n" , ## args); \
313 +#if defined(IP_SET_DEBUG)
314 +#define DP(format, args...) \
316 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
317 + printk(format "\n" , ## args); \
319 +#define IP_SET_ASSERT(x) \
322 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
323 + __FILE__, __LINE__, __FUNCTION__); \
326 +#define DP(format, args...)
327 +#define IP_SET_ASSERT(x)
333 + * The ip_set_type definition - one per set type, e.g. "ipmap".
335 + * Each individual set has a pointer, set->type, going to one
336 + * of these structures. Function pointers inside the structure implement
337 + * the real behaviour of the sets.
339 + * If not mentioned differently, the implementation behind the function
340 + * pointers of a set_type, is expected to return 0 if ok, and a negative
341 + * errno (e.g. -EINVAL) on error.
343 +struct ip_set_type {
344 + struct list_head list; /* next in list of set types */
346 + /* test for IP in set (kernel: iptables -m set src|dst)
347 + * return 0 if not in set, 1 if in set.
349 + int (*testip_kernel) (struct ip_set *set,
350 + const struct sk_buff * skb,
352 + const u_int32_t *flags,
353 + unsigned char index);
355 + /* test for IP in set (userspace: ipset -T set IP)
356 + * return 0 if not in set, 1 if in set.
358 + int (*testip) (struct ip_set *set,
359 + const void *data, size_t size,
363 + * Size of the data structure passed by when
364 + * adding/deletin/testing an entry.
368 + /* Add IP into set (userspace: ipset -A set IP)
369 + * Return -EEXIST if the address is already in the set,
370 + * and -ERANGE if the address lies outside the set bounds.
371 + * If the address was not already in the set, 0 is returned.
373 + int (*addip) (struct ip_set *set,
374 + const void *data, size_t size,
377 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
378 + * Return -EEXIST if the address is already in the set,
379 + * and -ERANGE if the address lies outside the set bounds.
380 + * If the address was not already in the set, 0 is returned.
382 + int (*addip_kernel) (struct ip_set *set,
383 + const struct sk_buff * skb,
385 + const u_int32_t *flags,
386 + unsigned char index);
388 + /* remove IP from set (userspace: ipset -D set --entry x)
389 + * Return -EEXIST if the address is NOT in the set,
390 + * and -ERANGE if the address lies outside the set bounds.
391 + * If the address really was in the set, 0 is returned.
393 + int (*delip) (struct ip_set *set,
394 + const void *data, size_t size,
397 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
398 + * Return -EEXIST if the address is NOT in the set,
399 + * and -ERANGE if the address lies outside the set bounds.
400 + * If the address really was in the set, 0 is returned.
402 + int (*delip_kernel) (struct ip_set *set,
403 + const struct sk_buff * skb,
405 + const u_int32_t *flags,
406 + unsigned char index);
408 + /* new set creation - allocated type specific items
410 + int (*create) (struct ip_set *set,
411 + const void *data, size_t size);
413 + /* retry the operation after successfully tweaking the set
415 + int (*retry) (struct ip_set *set);
417 + /* set destruction - free type specific items
418 + * There is no return value.
419 + * Can be called only when child sets are destroyed.
421 + void (*destroy) (struct ip_set *set);
423 + /* set flushing - reset all bits in the set, or something similar.
424 + * There is no return value.
426 + void (*flush) (struct ip_set *set);
428 + /* Listing: size needed for header
430 + size_t header_size;
432 + /* Listing: Get the header
434 + * Fill in the information in "data".
435 + * This function is always run after list_header_size() under a
436 + * writelock on the set. Therefor is the length of "data" always
439 + void (*list_header) (const struct ip_set *set,
442 + /* Listing: Get the size for the set members
444 + int (*list_members_size) (const struct ip_set *set);
446 + /* Listing: Get the set members
448 + * Fill in the information in "data".
449 + * This function is always run after list_member_size() under a
450 + * writelock on the set. Therefor is the length of "data" always
453 + void (*list_members) (const struct ip_set *set,
456 + char typename[IP_SET_MAXNAMELEN];
457 + unsigned char features;
458 + int protocol_version;
460 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
464 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
465 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
467 +/* A generic ipset */
469 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
470 + rwlock_t lock; /* lock for concurrency control */
471 + ip_set_id_t id; /* set id for swapping */
472 + ip_set_id_t binding; /* default binding for the set */
473 + atomic_t ref; /* in kernel and in hash references */
474 + struct ip_set_type *type; /* the set types */
475 + void *data; /* pooltype specific data */
478 +/* Structure to bind set elements to sets */
479 +struct ip_set_hash {
480 + struct list_head list; /* list of clashing entries in hash */
481 + ip_set_ip_t ip; /* ip from set */
482 + ip_set_id_t id; /* set id */
483 + ip_set_id_t binding; /* set we bind the element to */
486 +/* register and unregister set references */
487 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
488 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
489 +extern void ip_set_put(ip_set_id_t id);
491 +/* API for iptables set match, and SET target */
492 +extern void ip_set_addip_kernel(ip_set_id_t id,
493 + const struct sk_buff *skb,
494 + const u_int32_t *flags);
495 +extern void ip_set_delip_kernel(ip_set_id_t id,
496 + const struct sk_buff *skb,
497 + const u_int32_t *flags);
498 +extern int ip_set_testip_kernel(ip_set_id_t id,
499 + const struct sk_buff *skb,
500 + const u_int32_t *flags);
502 +#endif /* __KERNEL__ */
504 +#endif /*_IP_SET_H*/
505 diff --git a/include/linux/netfilter_ipv4/ip_set_iphash.h b/include/linux/netfilter_ipv4/ip_set_iphash.h
507 index 0000000..7de854b
509 +++ b/include/linux/netfilter_ipv4/ip_set_iphash.h
511 +#ifndef __IP_SET_IPHASH_H
512 +#define __IP_SET_IPHASH_H
514 +#include <linux/netfilter_ipv4/ip_set.h>
516 +#define SETTYPE_NAME "iphash"
517 +#define MAX_RANGE 0x0000FFFF
519 +struct ip_set_iphash {
520 + ip_set_ip_t *members; /* the iphash proper */
521 + uint32_t elements; /* number of elements */
522 + uint32_t hashsize; /* hash size */
523 + uint16_t probes; /* max number of probes */
524 + uint16_t resize; /* resize factor in percent */
525 + ip_set_ip_t netmask; /* netmask */
526 + void *initval[0]; /* initvals for jhash_1word */
529 +struct ip_set_req_iphash_create {
533 + ip_set_ip_t netmask;
536 +struct ip_set_req_iphash {
540 +#endif /* __IP_SET_IPHASH_H */
541 diff --git a/include/linux/netfilter_ipv4/ip_set_ipmap.h b/include/linux/netfilter_ipv4/ip_set_ipmap.h
543 index 0000000..2435102
545 +++ b/include/linux/netfilter_ipv4/ip_set_ipmap.h
547 +#ifndef __IP_SET_IPMAP_H
548 +#define __IP_SET_IPMAP_H
550 +#include <linux/netfilter_ipv4/ip_set.h>
552 +#define SETTYPE_NAME "ipmap"
553 +#define MAX_RANGE 0x0000FFFF
555 +struct ip_set_ipmap {
556 + void *members; /* the ipmap proper */
557 + ip_set_ip_t first_ip; /* host byte order, included in range */
558 + ip_set_ip_t last_ip; /* host byte order, included in range */
559 + ip_set_ip_t netmask; /* subnet netmask */
560 + ip_set_ip_t sizeid; /* size of set in IPs */
561 + ip_set_ip_t hosts; /* number of hosts in a subnet */
564 +struct ip_set_req_ipmap_create {
567 + ip_set_ip_t netmask;
570 +struct ip_set_req_ipmap {
575 +mask_to_bits(ip_set_ip_t mask)
577 + unsigned int bits = 32;
578 + ip_set_ip_t maskaddr;
580 + if (mask == 0xFFFFFFFF)
583 + maskaddr = 0xFFFFFFFE;
584 + while (--bits >= 0 && maskaddr != mask)
591 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
593 + ip_set_ip_t mask = 0xFFFFFFFE;
596 + while (--(*bits) >= 0 && mask && (to & mask) != from)
602 +#endif /* __IP_SET_IPMAP_H */
603 diff --git a/include/linux/netfilter_ipv4/ip_set_ipporthash.h b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
605 index 0000000..b715c56
607 +++ b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
609 +#ifndef __IP_SET_IPPORTHASH_H
610 +#define __IP_SET_IPPORTHASH_H
612 +#include <linux/netfilter_ipv4/ip_set.h>
614 +#define SETTYPE_NAME "ipporthash"
615 +#define MAX_RANGE 0x0000FFFF
616 +#define INVALID_PORT (MAX_RANGE + 1)
618 +struct ip_set_ipporthash {
619 + ip_set_ip_t *members; /* the ipporthash proper */
620 + uint32_t elements; /* number of elements */
621 + uint32_t hashsize; /* hash size */
622 + uint16_t probes; /* max number of probes */
623 + uint16_t resize; /* resize factor in percent */
624 + ip_set_ip_t first_ip; /* host byte order, included in range */
625 + ip_set_ip_t last_ip; /* host byte order, included in range */
626 + void *initval[0]; /* initvals for jhash_1word */
629 +struct ip_set_req_ipporthash_create {
637 +struct ip_set_req_ipporthash {
642 +#endif /* __IP_SET_IPPORTHASH_H */
643 diff --git a/include/linux/netfilter_ipv4/ip_set_iptree.h b/include/linux/netfilter_ipv4/ip_set_iptree.h
645 index 0000000..64e716b
647 +++ b/include/linux/netfilter_ipv4/ip_set_iptree.h
649 +#ifndef __IP_SET_IPTREE_H
650 +#define __IP_SET_IPTREE_H
652 +#include <linux/netfilter_ipv4/ip_set.h>
654 +#define SETTYPE_NAME "iptree"
655 +#define MAX_RANGE 0x0000FFFF
657 +struct ip_set_iptreed {
658 + unsigned long expires[256]; /* x.x.x.ADDR */
661 +struct ip_set_iptreec {
662 + struct ip_set_iptreed *tree[256]; /* x.x.ADDR.* */
665 +struct ip_set_iptreeb {
666 + struct ip_set_iptreec *tree[256]; /* x.ADDR.*.* */
669 +struct ip_set_iptree {
670 + unsigned int timeout;
671 + unsigned int gc_interval;
673 + uint32_t elements; /* number of elements */
674 + struct timer_list gc;
675 + struct ip_set_iptreeb *tree[256]; /* ADDR.*.*.* */
679 +struct ip_set_req_iptree_create {
680 + unsigned int timeout;
683 +struct ip_set_req_iptree {
685 + unsigned int timeout;
688 +#endif /* __IP_SET_IPTREE_H */
689 diff --git a/include/linux/netfilter_ipv4/ip_set_iptreemap.h b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
691 index 0000000..bef576a
693 +++ b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
695 +#ifndef __IP_SET_IPTREEMAP_H
696 +#define __IP_SET_IPTREEMAP_H
698 +#include <linux/netfilter_ipv4/ip_set.h>
700 +#define SETTYPE_NAME "iptreemap"
703 +struct ip_set_iptreemap_d {
704 + unsigned char bitmap[32]; /* x.x.x.y */
707 +struct ip_set_iptreemap_c {
708 + struct ip_set_iptreemap_d *tree[256]; /* x.x.y.x */
711 +struct ip_set_iptreemap_b {
712 + struct ip_set_iptreemap_c *tree[256]; /* x.y.x.x */
713 + unsigned char dirty[32];
717 +struct ip_set_iptreemap {
718 + unsigned int gc_interval;
720 + struct timer_list gc;
721 + struct ip_set_iptreemap_b *tree[256]; /* y.x.x.x */
725 +struct ip_set_req_iptreemap_create {
726 + unsigned int gc_interval;
729 +struct ip_set_req_iptreemap {
734 +#endif /* __IP_SET_IPTREEMAP_H */
735 diff --git a/include/linux/netfilter_ipv4/ip_set_jhash.h b/include/linux/netfilter_ipv4/ip_set_jhash.h
737 index 0000000..25c6b97
739 +++ b/include/linux/netfilter_ipv4/ip_set_jhash.h
741 +#ifndef _LINUX_IPSET_JHASH_H
742 +#define _LINUX_IPSET_JHASH_H
744 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
745 + * to __u32/__u8 so that the header file can be included into
746 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
749 +/* jhash.h: Jenkins hash support.
751 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
753 + * http://burtleburtle.net/bob/hash/
755 + * These are the credits from Bob's sources:
757 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
758 + * hash(), hash2(), hash3, and mix() are externally useful functions.
759 + * Routines to test the hash are included if SELF_TEST is defined.
760 + * You can use this free for any purpose. It has no warranty.
762 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
764 + * I've modified Bob's hash to be useful in the Linux kernel, and
765 + * any bugs present are surely my fault. -DaveM
768 +/* NOTE: Arguments are modified. */
769 +#define __jhash_mix(a, b, c) \
771 + a -= b; a -= c; a ^= (c>>13); \
772 + b -= c; b -= a; b ^= (a<<8); \
773 + c -= a; c -= b; c ^= (b>>13); \
774 + a -= b; a -= c; a ^= (c>>12); \
775 + b -= c; b -= a; b ^= (a<<16); \
776 + c -= a; c -= b; c ^= (b>>5); \
777 + a -= b; a -= c; a ^= (c>>3); \
778 + b -= c; b -= a; b ^= (a<<10); \
779 + c -= a; c -= b; c ^= (b>>15); \
782 +/* The golden ration: an arbitrary value */
783 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
785 +/* The most generic version, hashes an arbitrary sequence
786 + * of bytes. No alignment or length assumptions are made about
789 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
791 + __u32 a, b, c, len;
795 + a = b = JHASH_GOLDEN_RATIO;
798 + while (len >= 12) {
799 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
800 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
801 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
803 + __jhash_mix(a,b,c);
811 + case 11: c += ((__u32)k[10]<<24);
812 + case 10: c += ((__u32)k[9]<<16);
813 + case 9 : c += ((__u32)k[8]<<8);
814 + case 8 : b += ((__u32)k[7]<<24);
815 + case 7 : b += ((__u32)k[6]<<16);
816 + case 6 : b += ((__u32)k[5]<<8);
817 + case 5 : b += k[4];
818 + case 4 : a += ((__u32)k[3]<<24);
819 + case 3 : a += ((__u32)k[2]<<16);
820 + case 2 : a += ((__u32)k[1]<<8);
821 + case 1 : a += k[0];
824 + __jhash_mix(a,b,c);
829 +/* A special optimized version that handles 1 or more of __u32s.
830 + * The length parameter here is the number of __u32s in the key.
832 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
834 + __u32 a, b, c, len;
836 + a = b = JHASH_GOLDEN_RATIO;
844 + __jhash_mix(a, b, c);
851 + case 2 : b += k[1];
852 + case 1 : a += k[0];
855 + __jhash_mix(a,b,c);
861 +/* A special ultra-optimized versions that knows they are hashing exactly
862 + * 3, 2 or 1 word(s).
864 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
865 + * done at the end is not done here.
867 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
869 + a += JHASH_GOLDEN_RATIO;
870 + b += JHASH_GOLDEN_RATIO;
873 + __jhash_mix(a, b, c);
878 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
880 + return jhash_3words(a, b, 0, initval);
883 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
885 + return jhash_3words(a, 0, 0, initval);
888 +#endif /* _LINUX_IPSET_JHASH_H */
889 diff --git a/include/linux/netfilter_ipv4/ip_set_macipmap.h b/include/linux/netfilter_ipv4/ip_set_macipmap.h
891 index 0000000..ee34c9b
893 +++ b/include/linux/netfilter_ipv4/ip_set_macipmap.h
895 +#ifndef __IP_SET_MACIPMAP_H
896 +#define __IP_SET_MACIPMAP_H
898 +#include <linux/netfilter_ipv4/ip_set.h>
900 +#define SETTYPE_NAME "macipmap"
901 +#define MAX_RANGE 0x0000FFFF
904 +#define IPSET_MACIP_MATCHUNSET 1
907 +#define IPSET_MACIP_ISSET 1
909 +struct ip_set_macipmap {
910 + void *members; /* the macipmap proper */
911 + ip_set_ip_t first_ip; /* host byte order, included in range */
912 + ip_set_ip_t last_ip; /* host byte order, included in range */
916 +struct ip_set_req_macipmap_create {
922 +struct ip_set_req_macipmap {
924 + unsigned char ethernet[ETH_ALEN];
927 +struct ip_set_macip {
928 + unsigned short flags;
929 + unsigned char ethernet[ETH_ALEN];
932 +#endif /* __IP_SET_MACIPMAP_H */
933 diff --git a/include/linux/netfilter_ipv4/ip_set_malloc.h b/include/linux/netfilter_ipv4/ip_set_malloc.h
935 index 0000000..ab97e14
937 +++ b/include/linux/netfilter_ipv4/ip_set_malloc.h
939 +#ifndef _IP_SET_MALLOC_H
940 +#define _IP_SET_MALLOC_H
944 +/* Memory allocation and deallocation */
945 +static size_t max_malloc_size = 0;
947 +static inline void init_max_malloc_size(void)
949 +#define CACHE(x) max_malloc_size = x;
950 +#include <linux/kmalloc_sizes.h>
954 +static inline void * ip_set_malloc(size_t bytes)
956 + if (bytes > max_malloc_size)
957 + return vmalloc(bytes);
959 + return kmalloc(bytes, GFP_KERNEL);
962 +static inline void ip_set_free(void * data, size_t bytes)
964 + if (bytes > max_malloc_size)
971 + size_t max_elements;
975 +static inline void *
976 +harray_malloc(size_t hashsize, size_t typesize, int flags)
978 + struct harray *harray;
979 + size_t max_elements, size, i, j;
981 + if (!max_malloc_size)
982 + init_max_malloc_size();
984 + if (typesize > max_malloc_size)
987 + max_elements = max_malloc_size/typesize;
988 + size = hashsize/max_elements;
989 + if (hashsize % max_elements)
992 + /* Last pointer signals end of arrays */
993 + harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
999 + for (i = 0; i < size - 1; i++) {
1000 + harray->arrays[i] = kmalloc(max_elements * typesize, flags);
1001 + if (!harray->arrays[i])
1003 + memset(harray->arrays[i], 0, max_elements * typesize);
1005 + harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize,
1007 + if (!harray->arrays[i])
1009 + memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
1011 + harray->max_elements = max_elements;
1012 + harray->arrays[size] = NULL;
1014 + return (void *)harray;
1017 + for (j = 0; j < i; j++) {
1018 + kfree(harray->arrays[j]);
1024 +static inline void harray_free(void *h)
1026 + struct harray *harray = (struct harray *) h;
1029 + for (i = 0; harray->arrays[i] != NULL; i++)
1030 + kfree(harray->arrays[i]);
1034 +static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
1036 + struct harray *harray = (struct harray *) h;
1039 + for (i = 0; harray->arrays[i+1] != NULL; i++)
1040 + memset(harray->arrays[i], 0, harray->max_elements * typesize);
1041 + memset(harray->arrays[i], 0,
1042 + (hashsize - i * harray->max_elements) * typesize);
1045 +#define HARRAY_ELEM(h, type, which) \
1047 + struct harray *__h = (struct harray *)(h); \
1048 + ((type)((__h)->arrays[(which)/(__h)->max_elements]) \
1049 + + (which)%(__h)->max_elements); \
1052 +#endif /* __KERNEL__ */
1054 +#endif /*_IP_SET_MALLOC_H*/
1055 diff --git a/include/linux/netfilter_ipv4/ip_set_nethash.h b/include/linux/netfilter_ipv4/ip_set_nethash.h
1056 new file mode 100644
1057 index 0000000..172ef02
1059 +++ b/include/linux/netfilter_ipv4/ip_set_nethash.h
1061 +#ifndef __IP_SET_NETHASH_H
1062 +#define __IP_SET_NETHASH_H
1064 +#include <linux/netfilter_ipv4/ip_set.h>
1066 +#define SETTYPE_NAME "nethash"
1067 +#define MAX_RANGE 0x0000FFFF
1069 +struct ip_set_nethash {
1070 + ip_set_ip_t *members; /* the nethash proper */
1071 + uint32_t elements; /* number of elements */
1072 + uint32_t hashsize; /* hash size */
1073 + uint16_t probes; /* max number of probes */
1074 + uint16_t resize; /* resize factor in percent */
1075 + unsigned char cidr[30]; /* CIDR sizes */
1076 + void *initval[0]; /* initvals for jhash_1word */
1079 +struct ip_set_req_nethash_create {
1080 + uint32_t hashsize;
1085 +struct ip_set_req_nethash {
1087 + unsigned char cidr;
1090 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
1092 +static inline ip_set_ip_t
1093 +pack(ip_set_ip_t ip, unsigned char cidr)
1095 + ip_set_ip_t addr, *paddr = &addr;
1096 + unsigned char n, t, *a;
1098 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
1100 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
1104 + a = &((unsigned char *)paddr)[n];
1105 + *a = *a /(1 << (8 - t)) + shifts[t];
1107 + DP("n: %u, t: %u, a: %u", n, t, *a);
1108 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
1109 + HIPQUAD(ip), cidr, NIPQUAD(addr));
1112 + return ntohl(addr);
1115 +#endif /* __IP_SET_NETHASH_H */
1116 diff --git a/include/linux/netfilter_ipv4/ip_set_portmap.h b/include/linux/netfilter_ipv4/ip_set_portmap.h
1117 new file mode 100644
1118 index 0000000..c17165c
1120 +++ b/include/linux/netfilter_ipv4/ip_set_portmap.h
1122 +#ifndef __IP_SET_PORTMAP_H
1123 +#define __IP_SET_PORTMAP_H
1125 +#include <linux/netfilter_ipv4/ip_set.h>
1127 +#define SETTYPE_NAME "portmap"
1128 +#define MAX_RANGE 0x0000FFFF
1129 +#define INVALID_PORT (MAX_RANGE + 1)
1131 +struct ip_set_portmap {
1132 + void *members; /* the portmap proper */
1133 + ip_set_ip_t first_port; /* host byte order, included in range */
1134 + ip_set_ip_t last_port; /* host byte order, included in range */
1137 +struct ip_set_req_portmap_create {
1142 +struct ip_set_req_portmap {
1146 +#endif /* __IP_SET_PORTMAP_H */
1147 diff --git a/include/linux/netfilter_ipv4/ipt_set.h b/include/linux/netfilter_ipv4/ipt_set.h
1148 new file mode 100644
1149 index 0000000..2a18b93
1151 +++ b/include/linux/netfilter_ipv4/ipt_set.h
1156 +#include <linux/netfilter_ipv4/ip_set.h>
1158 +struct ipt_set_info {
1159 + ip_set_id_t index;
1160 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1164 +struct ipt_set_info_match {
1165 + struct ipt_set_info match_set;
1168 +struct ipt_set_info_target {
1169 + struct ipt_set_info add_set;
1170 + struct ipt_set_info del_set;
1173 +#endif /*_IPT_SET_H*/
1174 diff --git a/net/ipv4/netfilter/Kconfig b/net/ipv4/netfilter/Kconfig
1175 index 1833bdb..4c315cf 100644
1176 --- a/net/ipv4/netfilter/Kconfig
1177 +++ b/net/ipv4/netfilter/Kconfig
1178 @@ -388,5 +388,122 @@ config IP_NF_ARP_MANGLE
1180 endif # IP_NF_ARPTABLES
1183 + tristate "IP set support"
1184 + depends on INET && NETFILTER
1186 + This option adds IP set support to the kernel.
1187 + In order to define and use sets, you need the userspace utility
1190 + To compile it as a module, choose M here. If unsure, say N.
1192 +config IP_NF_SET_MAX
1193 + int "Maximum number of IP sets"
1196 + depends on IP_NF_SET
1198 + You can define here default value of the maximum number
1199 + of IP sets for the kernel.
1201 + The value can be overriden by the 'max_sets' module
1202 + parameter of the 'ip_set' module.
1204 +config IP_NF_SET_HASHSIZE
1205 + int "Hash size for bindings of IP sets"
1207 + depends on IP_NF_SET
1209 + You can define here default value of the hash size for
1210 + bindings of IP sets.
1212 + The value can be overriden by the 'hash_size' module
1213 + parameter of the 'ip_set' module.
1215 +config IP_NF_SET_IPMAP
1216 + tristate "ipmap set support"
1217 + depends on IP_NF_SET
1219 + This option adds the ipmap set type support.
1221 + To compile it as a module, choose M here. If unsure, say N.
1223 +config IP_NF_SET_MACIPMAP
1224 + tristate "macipmap set support"
1225 + depends on IP_NF_SET
1227 + This option adds the macipmap set type support.
1229 + To compile it as a module, choose M here. If unsure, say N.
1231 +config IP_NF_SET_PORTMAP
1232 + tristate "portmap set support"
1233 + depends on IP_NF_SET
1235 + This option adds the portmap set type support.
1237 + To compile it as a module, choose M here. If unsure, say N.
1239 +config IP_NF_SET_IPHASH
1240 + tristate "iphash set support"
1241 + depends on IP_NF_SET
1243 + This option adds the iphash set type support.
1245 + To compile it as a module, choose M here. If unsure, say N.
1247 +config IP_NF_SET_NETHASH
1248 + tristate "nethash set support"
1249 + depends on IP_NF_SET
1251 + This option adds the nethash set type support.
1253 + To compile it as a module, choose M here. If unsure, say N.
1255 +config IP_NF_SET_IPPORTHASH
1256 + tristate "ipporthash set support"
1257 + depends on IP_NF_SET
1259 + This option adds the ipporthash set type support.
1261 + To compile it as a module, choose M here. If unsure, say N.
1263 +config IP_NF_SET_IPTREE
1264 + tristate "iptree set support"
1265 + depends on IP_NF_SET
1267 + This option adds the iptree set type support.
1269 + To compile it as a module, choose M here. If unsure, say N.
1271 +config IP_NF_SET_IPTREEMAP
1272 + tristate "iptreemap set support"
1273 + depends on IP_NF_SET
1275 + This option adds the iptreemap set type support.
1277 + To compile it as a module, choose M here. If unsure, say N.
1279 +config IP_NF_MATCH_SET
1280 + tristate "set match support"
1281 + depends on IP_NF_SET
1283 + Set matching matches against given IP sets.
1284 + You need the ipset utility to create and set up the sets.
1286 + To compile it as a module, choose M here. If unsure, say N.
1288 +config IP_NF_TARGET_SET
1289 + tristate "SET target support"
1290 + depends on IP_NF_SET
1292 + The SET target makes possible to add/delete entries
1294 + You need the ipset utility to create and set up the sets.
1296 + To compile it as a module, choose M here. If unsure, say N.
1301 diff --git a/net/ipv4/netfilter/Makefile b/net/ipv4/netfilter/Makefile
1302 index 4811159..fd4913a 100644
1303 --- a/net/ipv4/netfilter/Makefile
1304 +++ b/net/ipv4/netfilter/Makefile
1305 @@ -51,6 +51,7 @@ obj-$(CONFIG_IP_NF_SECURITY) += iptable_security.o
1306 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
1307 obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o
1308 obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
1309 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
1312 obj-$(CONFIG_IP_NF_TARGET_CLUSTERIP) += ipt_CLUSTERIP.o
1313 @@ -60,8 +61,20 @@ obj-$(CONFIG_IP_NF_TARGET_MASQUERADE) += ipt_MASQUERADE.o
1314 obj-$(CONFIG_IP_NF_TARGET_NETMAP) += ipt_NETMAP.o
1315 obj-$(CONFIG_IP_NF_TARGET_REDIRECT) += ipt_REDIRECT.o
1316 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
1317 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
1318 obj-$(CONFIG_IP_NF_TARGET_ULOG) += ipt_ULOG.o
1321 +obj-$(CONFIG_IP_NF_SET) += ip_set.o
1322 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
1323 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
1324 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
1325 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
1326 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
1327 +obj-$(CONFIG_IP_NF_SET_IPPORTHASH) += ip_set_ipporthash.o
1328 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
1329 +obj-$(CONFIG_IP_NF_SET_IPTREEMAP) += ip_set_iptreemap.o
1331 # generic ARP tables
1332 obj-$(CONFIG_IP_NF_ARPTABLES) += arp_tables.o
1333 obj-$(CONFIG_IP_NF_ARP_MANGLE) += arpt_mangle.o
1334 diff --git a/net/ipv4/netfilter/ip_set.c b/net/ipv4/netfilter/ip_set.c
1335 new file mode 100644
1336 index 0000000..7d00a14
1338 +++ b/net/ipv4/netfilter/ip_set.c
1340 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1341 + * Patrick Schaaf <bof@bof.de>
1342 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1344 + * This program is free software; you can redistribute it and/or modify
1345 + * it under the terms of the GNU General Public License version 2 as
1346 + * published by the Free Software Foundation.
1349 +/* Kernel module for IP set management */
1351 +#include <linux/version.h>
1352 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
1353 +#include <linux/config.h>
1355 +#include <linux/module.h>
1356 +#include <linux/moduleparam.h>
1357 +#include <linux/kmod.h>
1358 +#include <linux/ip.h>
1359 +#include <linux/skbuff.h>
1360 +#include <linux/random.h>
1361 +#include <linux/jhash.h>
1362 +#include <linux/netfilter_ipv4/ip_tables.h>
1363 +#include <linux/errno.h>
1364 +#include <asm/uaccess.h>
1365 +#include <asm/bitops.h>
1366 +// #include <asm/semaphore.h>
1367 +#include <linux/spinlock.h>
1368 +#include <linux/vmalloc.h>
1370 +#define ASSERT_READ_LOCK(x)
1371 +#define ASSERT_WRITE_LOCK(x)
1372 +#include <linux/netfilter_ipv4/ip_set.h>
1374 +static struct list_head set_type_list; /* all registered sets */
1375 +static struct ip_set **ip_set_list; /* all individual sets */
1376 +static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1377 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1378 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1379 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1380 +static struct list_head *ip_set_hash; /* hash of bindings */
1381 +static unsigned int ip_set_hash_random; /* random seed */
1384 + * Sets are identified either by the index in ip_set_list or by id.
1385 + * The id never changes and is used to find a key in the hash.
1386 + * The index may change by swapping and used at all other places
1387 + * (set/SET netfilter modules, binding value, etc.)
1389 + * Userspace requests are serialized by ip_set_mutex and sets can
1390 + * be deleted only from userspace. Therefore ip_set_list locking
1391 + * must obey the following rules:
1393 + * - kernel requests: read and write locking mandatory
1394 + * - user requests: read locking optional, write locking mandatory
1398 +__ip_set_get(ip_set_id_t index)
1400 + atomic_inc(&ip_set_list[index]->ref);
1404 +__ip_set_put(ip_set_id_t index)
1406 + atomic_dec(&ip_set_list[index]->ref);
1410 + * Binding routines
1413 +static inline struct ip_set_hash *
1414 +__ip_set_find(u_int32_t key, ip_set_id_t id, ip_set_ip_t ip)
1416 + struct ip_set_hash *set_hash;
1418 + list_for_each_entry(set_hash, &ip_set_hash[key], list)
1419 + if (set_hash->id == id && set_hash->ip == ip)
1426 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1428 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1429 + % ip_set_bindings_hash_size;
1430 + struct ip_set_hash *set_hash;
1432 + ASSERT_READ_LOCK(&ip_set_lock);
1433 + IP_SET_ASSERT(ip_set_list[id]);
1434 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1436 + set_hash = __ip_set_find(key, id, ip);
1438 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1440 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1442 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1446 +__set_hash_del(struct ip_set_hash *set_hash)
1448 + ASSERT_WRITE_LOCK(&ip_set_lock);
1449 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1451 + __ip_set_put(set_hash->binding);
1452 + list_del(&set_hash->list);
1457 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1459 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1460 + % ip_set_bindings_hash_size;
1461 + struct ip_set_hash *set_hash;
1463 + IP_SET_ASSERT(ip_set_list[id]);
1464 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1465 + write_lock_bh(&ip_set_lock);
1466 + set_hash = __ip_set_find(key, id, ip);
1467 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1469 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1471 + if (set_hash != NULL)
1472 + __set_hash_del(set_hash);
1473 + write_unlock_bh(&ip_set_lock);
1478 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1480 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1481 + % ip_set_bindings_hash_size;
1482 + struct ip_set_hash *set_hash;
1485 + IP_SET_ASSERT(ip_set_list[id]);
1486 + IP_SET_ASSERT(ip_set_list[binding]);
1487 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1488 + HIPQUAD(ip), ip_set_list[binding]->name);
1489 + write_lock_bh(&ip_set_lock);
1490 + set_hash = __ip_set_find(key, id, ip);
1492 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_ATOMIC);
1497 + INIT_LIST_HEAD(&set_hash->list);
1498 + set_hash->id = id;
1499 + set_hash->ip = ip;
1500 + list_add(&set_hash->list, &ip_set_hash[key]);
1502 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1503 + DP("overwrite binding: %s",
1504 + ip_set_list[set_hash->binding]->name);
1505 + __ip_set_put(set_hash->binding);
1507 + set_hash->binding = binding;
1508 + __ip_set_get(set_hash->binding);
1509 + DP("stored: key %u, id %u (%s), ip %u.%u.%u.%u, binding %u (%s)",
1510 + key, id, ip_set_list[id]->name,
1511 + HIPQUAD(ip), binding, ip_set_list[binding]->name);
1513 + write_unlock_bh(&ip_set_lock);
1517 +#define FOREACH_HASH_DO(fn, args...) \
1519 + ip_set_id_t __key; \
1520 + struct ip_set_hash *__set_hash; \
1522 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1523 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1524 + fn(__set_hash , ## args); \
1528 +#define FOREACH_HASH_RW_DO(fn, args...) \
1530 + ip_set_id_t __key; \
1531 + struct ip_set_hash *__set_hash, *__n; \
1533 + ASSERT_WRITE_LOCK(&ip_set_lock); \
1534 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1535 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1536 + fn(__set_hash , ## args); \
1540 +/* Add, del and test set entries from kernel */
1542 +#define follow_bindings(index, set, ip) \
1543 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1544 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1547 +ip_set_testip_kernel(ip_set_id_t index,
1548 + const struct sk_buff *skb,
1549 + const u_int32_t *flags)
1551 + struct ip_set *set;
1554 + unsigned char i = 0;
1556 + IP_SET_ASSERT(flags[i]);
1557 + read_lock_bh(&ip_set_lock);
1559 + set = ip_set_list[index];
1560 + IP_SET_ASSERT(set);
1561 + DP("set %s, index %u", set->name, index);
1562 + read_lock_bh(&set->lock);
1563 + res = set->type->testip_kernel(set, skb, &ip, flags, i++);
1564 + read_unlock_bh(&set->lock);
1565 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1568 + && follow_bindings(index, set, ip));
1569 + read_unlock_bh(&ip_set_lock);
1575 +ip_set_addip_kernel(ip_set_id_t index,
1576 + const struct sk_buff *skb,
1577 + const u_int32_t *flags)
1579 + struct ip_set *set;
1582 + unsigned char i = 0;
1584 + IP_SET_ASSERT(flags[i]);
1586 + read_lock_bh(&ip_set_lock);
1588 + set = ip_set_list[index];
1589 + IP_SET_ASSERT(set);
1590 + DP("set %s, index %u", set->name, index);
1591 + write_lock_bh(&set->lock);
1592 + res = set->type->addip_kernel(set, skb, &ip, flags, i++);
1593 + write_unlock_bh(&set->lock);
1594 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1595 + } while ((res == 0 || res == -EEXIST)
1597 + && follow_bindings(index, set, ip));
1598 + read_unlock_bh(&ip_set_lock);
1600 + if (res == -EAGAIN
1601 + && set->type->retry
1602 + && (res = set->type->retry(set)) == 0)
1607 +ip_set_delip_kernel(ip_set_id_t index,
1608 + const struct sk_buff *skb,
1609 + const u_int32_t *flags)
1611 + struct ip_set *set;
1614 + unsigned char i = 0;
1616 + IP_SET_ASSERT(flags[i]);
1617 + read_lock_bh(&ip_set_lock);
1619 + set = ip_set_list[index];
1620 + IP_SET_ASSERT(set);
1621 + DP("set %s, index %u", set->name, index);
1622 + write_lock_bh(&set->lock);
1623 + res = set->type->delip_kernel(set, skb, &ip, flags, i++);
1624 + write_unlock_bh(&set->lock);
1625 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1626 + } while ((res == 0 || res == -EEXIST)
1628 + && follow_bindings(index, set, ip));
1629 + read_unlock_bh(&ip_set_lock);
1632 +/* Register and deregister settype */
1634 +static inline struct ip_set_type *
1635 +find_set_type(const char *name)
1637 + struct ip_set_type *set_type;
1639 + list_for_each_entry(set_type, &set_type_list, list)
1640 + if (!strncmp(set_type->typename, name, IP_SET_MAXNAMELEN - 1))
1646 +ip_set_register_set_type(struct ip_set_type *set_type)
1650 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1651 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1652 + set_type->typename,
1653 + set_type->protocol_version,
1654 + IP_SET_PROTOCOL_VERSION);
1658 + write_lock_bh(&ip_set_lock);
1659 + if (find_set_type(set_type->typename)) {
1661 + ip_set_printk("'%s' already registered!",
1662 + set_type->typename);
1666 + if (!try_module_get(THIS_MODULE)) {
1670 + list_add(&set_type->list, &set_type_list);
1671 + DP("'%s' registered.", set_type->typename);
1673 + write_unlock_bh(&ip_set_lock);
1678 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1680 + write_lock_bh(&ip_set_lock);
1681 + if (!find_set_type(set_type->typename)) {
1682 + ip_set_printk("'%s' not registered?",
1683 + set_type->typename);
1686 + list_del(&set_type->list);
1687 + module_put(THIS_MODULE);
1688 + DP("'%s' unregistered.", set_type->typename);
1690 + write_unlock_bh(&ip_set_lock);
1695 + * Userspace routines
1699 + * Find set by name, reference it once. The reference makes sure the
1700 + * thing pointed to, does not go away under our feet. Drop the reference
1701 + * later, using ip_set_put().
1704 +ip_set_get_byname(const char *name)
1706 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1708 + down(&ip_set_app_mutex);
1709 + for (i = 0; i < ip_set_max; i++) {
1710 + if (ip_set_list[i] != NULL
1711 + && strcmp(ip_set_list[i]->name, name) == 0) {
1717 + up(&ip_set_app_mutex);
1722 + * Find set by index, reference it once. The reference makes sure the
1723 + * thing pointed to, does not go away under our feet. Drop the reference
1724 + * later, using ip_set_put().
1727 +ip_set_get_byindex(ip_set_id_t index)
1729 + down(&ip_set_app_mutex);
1731 + if (index >= ip_set_max)
1732 + return IP_SET_INVALID_ID;
1734 + if (ip_set_list[index])
1735 + __ip_set_get(index);
1737 + index = IP_SET_INVALID_ID;
1739 + up(&ip_set_app_mutex);
1744 + * If the given set pointer points to a valid set, decrement
1745 + * reference count by 1. The caller shall not assume the index
1746 + * to be valid, after calling this function.
1748 +void ip_set_put(ip_set_id_t index)
1750 + down(&ip_set_app_mutex);
1751 + if (ip_set_list[index])
1752 + __ip_set_put(index);
1753 + up(&ip_set_app_mutex);
1756 +/* Find a set by name or index */
1758 +ip_set_find_byname(const char *name)
1760 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1762 + for (i = 0; i < ip_set_max; i++) {
1763 + if (ip_set_list[i] != NULL
1764 + && strcmp(ip_set_list[i]->name, name) == 0) {
1773 +ip_set_find_byindex(ip_set_id_t index)
1775 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1776 + index = IP_SET_INVALID_ID;
1782 + * Add, del, test, bind and unbind
1786 +__ip_set_testip(struct ip_set *set,
1793 + read_lock_bh(&set->lock);
1794 + res = set->type->testip(set, data, size, ip);
1795 + read_unlock_bh(&set->lock);
1801 +__ip_set_addip(ip_set_id_t index,
1805 + struct ip_set *set = ip_set_list[index];
1809 + IP_SET_ASSERT(set);
1811 + write_lock_bh(&set->lock);
1812 + res = set->type->addip(set, data, size, &ip);
1813 + write_unlock_bh(&set->lock);
1814 + } while (res == -EAGAIN
1815 + && set->type->retry
1816 + && (res = set->type->retry(set)) == 0);
1822 +ip_set_addip(ip_set_id_t index,
1827 + return __ip_set_addip(index,
1828 + data + sizeof(struct ip_set_req_adt),
1829 + size - sizeof(struct ip_set_req_adt));
1833 +ip_set_delip(ip_set_id_t index,
1837 + struct ip_set *set = ip_set_list[index];
1841 + IP_SET_ASSERT(set);
1842 + write_lock_bh(&set->lock);
1843 + res = set->type->delip(set,
1844 + data + sizeof(struct ip_set_req_adt),
1845 + size - sizeof(struct ip_set_req_adt),
1847 + write_unlock_bh(&set->lock);
1853 +ip_set_testip(ip_set_id_t index,
1857 + struct ip_set *set = ip_set_list[index];
1861 + IP_SET_ASSERT(set);
1862 + res = __ip_set_testip(set,
1863 + data + sizeof(struct ip_set_req_adt),
1864 + size - sizeof(struct ip_set_req_adt),
1867 + return (res > 0 ? -EEXIST : res);
1871 +ip_set_bindip(ip_set_id_t index,
1875 + struct ip_set *set = ip_set_list[index];
1876 + struct ip_set_req_bind *req_bind;
1877 + ip_set_id_t binding;
1881 + IP_SET_ASSERT(set);
1882 + if (size < sizeof(struct ip_set_req_bind))
1885 + req_bind = (struct ip_set_req_bind *) data;
1886 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1888 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1889 + /* Default binding of a set */
1890 + char *binding_name;
1892 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1895 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1896 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1898 + binding = ip_set_find_byname(binding_name);
1899 + if (binding == IP_SET_INVALID_ID)
1902 + write_lock_bh(&ip_set_lock);
1903 + /* Sets as binding values are referenced */
1904 + if (set->binding != IP_SET_INVALID_ID)
1905 + __ip_set_put(set->binding);
1906 + set->binding = binding;
1907 + __ip_set_get(set->binding);
1908 + write_unlock_bh(&ip_set_lock);
1912 + binding = ip_set_find_byname(req_bind->binding);
1913 + if (binding == IP_SET_INVALID_ID)
1916 + res = __ip_set_testip(set,
1917 + data + sizeof(struct ip_set_req_bind),
1918 + size - sizeof(struct ip_set_req_bind),
1920 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1921 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1924 + res = ip_set_hash_add(set->id, ip, binding);
1929 +#define FOREACH_SET_DO(fn, args...) \
1931 + ip_set_id_t __i; \
1932 + struct ip_set *__set; \
1934 + for (__i = 0; __i < ip_set_max; __i++) { \
1935 + __set = ip_set_list[__i]; \
1936 + if (__set != NULL) \
1937 + fn(__set , ##args); \
1942 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1944 + if (set_hash->id == id)
1945 + __set_hash_del(set_hash);
1949 +__unbind_default(struct ip_set *set)
1951 + if (set->binding != IP_SET_INVALID_ID) {
1952 + /* Sets as binding values are referenced */
1953 + __ip_set_put(set->binding);
1954 + set->binding = IP_SET_INVALID_ID;
1959 +ip_set_unbindip(ip_set_id_t index,
1963 + struct ip_set *set;
1964 + struct ip_set_req_bind *req_bind;
1969 + if (size < sizeof(struct ip_set_req_bind))
1972 + req_bind = (struct ip_set_req_bind *) data;
1973 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1975 + DP("%u %s", index, req_bind->binding);
1976 + if (index == IP_SET_INVALID_ID) {
1977 + /* unbind :all: */
1978 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1979 + /* Default binding of sets */
1980 + write_lock_bh(&ip_set_lock);
1981 + FOREACH_SET_DO(__unbind_default);
1982 + write_unlock_bh(&ip_set_lock);
1984 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1985 + /* Flush all bindings of all sets*/
1986 + write_lock_bh(&ip_set_lock);
1987 + FOREACH_HASH_RW_DO(__set_hash_del);
1988 + write_unlock_bh(&ip_set_lock);
1991 + DP("unreachable reached!");
1995 + set = ip_set_list[index];
1996 + IP_SET_ASSERT(set);
1997 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1998 + /* Default binding of set */
1999 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
2001 + if (binding == IP_SET_INVALID_ID)
2004 + write_lock_bh(&ip_set_lock);
2005 + /* Sets in hash values are referenced */
2006 + __ip_set_put(set->binding);
2007 + set->binding = IP_SET_INVALID_ID;
2008 + write_unlock_bh(&ip_set_lock);
2011 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
2012 + /* Flush all bindings */
2014 + write_lock_bh(&ip_set_lock);
2015 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2016 + write_unlock_bh(&ip_set_lock);
2020 + res = __ip_set_testip(set,
2021 + data + sizeof(struct ip_set_req_bind),
2022 + size - sizeof(struct ip_set_req_bind),
2025 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
2027 + res = ip_set_hash_del(set->id, ip);
2033 +ip_set_testbind(ip_set_id_t index,
2037 + struct ip_set *set = ip_set_list[index];
2038 + struct ip_set_req_bind *req_bind;
2039 + ip_set_id_t binding;
2043 + IP_SET_ASSERT(set);
2044 + if (size < sizeof(struct ip_set_req_bind))
2047 + req_bind = (struct ip_set_req_bind *) data;
2048 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
2050 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
2051 + /* Default binding of set */
2052 + char *binding_name;
2054 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
2057 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
2058 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
2060 + binding = ip_set_find_byname(binding_name);
2061 + if (binding == IP_SET_INVALID_ID)
2064 + res = (set->binding == binding) ? -EEXIST : 0;
2068 + binding = ip_set_find_byname(req_bind->binding);
2069 + if (binding == IP_SET_INVALID_ID)
2073 + res = __ip_set_testip(set,
2074 + data + sizeof(struct ip_set_req_bind),
2075 + size - sizeof(struct ip_set_req_bind),
2077 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
2078 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
2081 + res = (ip_set_find_in_hash(set->id, ip) == binding)
2087 +static struct ip_set_type *
2088 +find_set_type_rlock(const char *typename)
2090 + struct ip_set_type *type;
2092 + read_lock_bh(&ip_set_lock);
2093 + type = find_set_type(typename);
2095 + read_unlock_bh(&ip_set_lock);
2101 +find_free_id(const char *name,
2102 + ip_set_id_t *index,
2107 + *id = IP_SET_INVALID_ID;
2108 + for (i = 0; i < ip_set_max; i++) {
2109 + if (ip_set_list[i] == NULL) {
2110 + if (*id == IP_SET_INVALID_ID)
2112 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
2116 + if (*id == IP_SET_INVALID_ID)
2117 + /* No free slot remained */
2119 + /* Check that index is usable as id (swapping) */
2121 + for (i = 0; i < ip_set_max; i++) {
2122 + if (ip_set_list[i] != NULL
2123 + && ip_set_list[i]->id == *id) {
2135 +ip_set_create(const char *name,
2136 + const char *typename,
2137 + ip_set_id_t restore,
2141 + struct ip_set *set;
2142 + ip_set_id_t index = 0, id;
2145 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
2147 + * First, and without any locks, allocate and initialize
2148 + * a normal base set structure.
2150 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
2153 + set->lock = RW_LOCK_UNLOCKED;
2154 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2155 + set->binding = IP_SET_INVALID_ID;
2156 + atomic_set(&set->ref, 0);
2159 + * Next, take the &ip_set_lock, check that we know the type,
2160 + * and take a reference on the type, to make sure it
2161 + * stays available while constructing our new set.
2163 + * After referencing the type, we drop the &ip_set_lock,
2164 + * and let the new set construction run without locks.
2166 + set->type = find_set_type_rlock(typename);
2167 + if (set->type == NULL) {
2168 + /* Try loading the module */
2169 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
2170 + strcpy(modulename, "ip_set_");
2171 + strcat(modulename, typename);
2172 + DP("try to load %s", modulename);
2173 + request_module(modulename);
2174 + set->type = find_set_type_rlock(typename);
2176 + if (set->type == NULL) {
2177 + ip_set_printk("no set type '%s', set '%s' not created",
2182 + if (!try_module_get(set->type->me)) {
2183 + read_unlock_bh(&ip_set_lock);
2187 + read_unlock_bh(&ip_set_lock);
2190 + * Without holding any locks, create private part.
2192 + res = set->type->create(set, data, size);
2196 + /* BTW, res==0 here. */
2199 + * Here, we have a valid, constructed set. &ip_set_lock again,
2200 + * find free id/index and check that it is not already in
2203 + write_lock_bh(&ip_set_lock);
2204 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
2205 + DP("no free id!");
2209 + /* Make sure restore gets the same index */
2210 + if (restore != IP_SET_INVALID_ID && index != restore) {
2211 + DP("Can't restore, sets are screwed up");
2217 + * Finally! Add our shiny new set to the list, and be done.
2219 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2221 + ip_set_list[index] = set;
2222 + write_unlock_bh(&ip_set_lock);
2226 + write_unlock_bh(&ip_set_lock);
2227 + set->type->destroy(set);
2229 + module_put(set->type->me);
2236 + * Destroy a given existing set
2239 +ip_set_destroy_set(ip_set_id_t index)
2241 + struct ip_set *set = ip_set_list[index];
2243 + IP_SET_ASSERT(set);
2244 + DP("set: %s", set->name);
2245 + write_lock_bh(&ip_set_lock);
2246 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2247 + if (set->binding != IP_SET_INVALID_ID)
2248 + __ip_set_put(set->binding);
2249 + ip_set_list[index] = NULL;
2250 + write_unlock_bh(&ip_set_lock);
2252 + /* Must call it without holding any lock */
2253 + set->type->destroy(set);
2254 + module_put(set->type->me);
2259 + * Destroy a set - or all sets
2260 + * Sets must not be referenced/used.
2263 +ip_set_destroy(ip_set_id_t index)
2267 + /* ref modification always protected by the mutex */
2268 + if (index != IP_SET_INVALID_ID) {
2269 + if (atomic_read(&ip_set_list[index]->ref))
2271 + ip_set_destroy_set(index);
2273 + for (i = 0; i < ip_set_max; i++) {
2274 + if (ip_set_list[i] != NULL
2275 + && (atomic_read(&ip_set_list[i]->ref)))
2279 + for (i = 0; i < ip_set_max; i++) {
2280 + if (ip_set_list[i] != NULL)
2281 + ip_set_destroy_set(i);
2288 +ip_set_flush_set(struct ip_set *set)
2290 + DP("set: %s %u", set->name, set->id);
2292 + write_lock_bh(&set->lock);
2293 + set->type->flush(set);
2294 + write_unlock_bh(&set->lock);
2298 + * Flush data in a set - or in all sets
2301 +ip_set_flush(ip_set_id_t index)
2303 + if (index != IP_SET_INVALID_ID) {
2304 + IP_SET_ASSERT(ip_set_list[index]);
2305 + ip_set_flush_set(ip_set_list[index]);
2307 + FOREACH_SET_DO(ip_set_flush_set);
2314 +ip_set_rename(ip_set_id_t index, const char *name)
2316 + struct ip_set *set = ip_set_list[index];
2320 + DP("set: %s to %s", set->name, name);
2321 + write_lock_bh(&ip_set_lock);
2322 + for (i = 0; i < ip_set_max; i++) {
2323 + if (ip_set_list[i] != NULL
2324 + && strncmp(ip_set_list[i]->name,
2326 + IP_SET_MAXNAMELEN - 1) == 0) {
2331 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2333 + write_unlock_bh(&ip_set_lock);
2338 + * Swap two sets so that name/index points to the other.
2339 + * References are also swapped.
2342 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2344 + struct ip_set *from = ip_set_list[from_index];
2345 + struct ip_set *to = ip_set_list[to_index];
2346 + char from_name[IP_SET_MAXNAMELEN];
2347 + u_int32_t from_ref;
2349 + DP("set: %s to %s", from->name, to->name);
2350 + /* Features must not change. Artifical restriction. */
2351 + if (from->type->features != to->type->features)
2354 + /* No magic here: ref munging protected by the mutex */
2355 + write_lock_bh(&ip_set_lock);
2356 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2357 + from_ref = atomic_read(&from->ref);
2359 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2360 + atomic_set(&from->ref, atomic_read(&to->ref));
2361 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2362 + atomic_set(&to->ref, from_ref);
2364 + ip_set_list[from_index] = to;
2365 + ip_set_list[to_index] = from;
2367 + write_unlock_bh(&ip_set_lock);
2376 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2377 + ip_set_id_t id, size_t *size)
2379 + if (set_hash->id == id)
2380 + *size += sizeof(struct ip_set_hash_list);
2384 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2385 + ip_set_id_t id, size_t *size)
2387 + if (set_hash->id == id)
2388 + *size += sizeof(struct ip_set_hash_save);
2392 +__set_hash_bindings(struct ip_set_hash *set_hash,
2393 + ip_set_id_t id, void *data, int *used)
2395 + if (set_hash->id == id) {
2396 + struct ip_set_hash_list *hash_list =
2397 + (struct ip_set_hash_list *)(data + *used);
2399 + hash_list->ip = set_hash->ip;
2400 + hash_list->binding = set_hash->binding;
2401 + *used += sizeof(struct ip_set_hash_list);
2405 +static int ip_set_list_set(ip_set_id_t index,
2410 + struct ip_set *set = ip_set_list[index];
2411 + struct ip_set_list *set_list;
2413 + /* Pointer to our header */
2414 + set_list = (struct ip_set_list *) (data + *used);
2416 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2418 + /* Get and ensure header size */
2419 + if (*used + sizeof(struct ip_set_list) > len)
2420 + goto not_enough_mem;
2421 + *used += sizeof(struct ip_set_list);
2423 + read_lock_bh(&set->lock);
2424 + /* Get and ensure set specific header size */
2425 + set_list->header_size = set->type->header_size;
2426 + if (*used + set_list->header_size > len)
2429 + /* Fill in the header */
2430 + set_list->index = index;
2431 + set_list->binding = set->binding;
2432 + set_list->ref = atomic_read(&set->ref);
2434 + /* Fill in set spefific header data */
2435 + set->type->list_header(set, data + *used);
2436 + *used += set_list->header_size;
2438 + /* Get and ensure set specific members size */
2439 + set_list->members_size = set->type->list_members_size(set);
2440 + if (*used + set_list->members_size > len)
2443 + /* Fill in set spefific members data */
2444 + set->type->list_members(set, data + *used);
2445 + *used += set_list->members_size;
2446 + read_unlock_bh(&set->lock);
2450 + /* Get and ensure set specific bindings size */
2451 + set_list->bindings_size = 0;
2452 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2453 + set->id, &set_list->bindings_size);
2454 + if (*used + set_list->bindings_size > len)
2455 + goto not_enough_mem;
2457 + /* Fill in set spefific bindings data */
2458 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2463 + read_unlock_bh(&set->lock);
2465 + DP("not enough mem, try again");
2472 +static int ip_set_save_set(ip_set_id_t index,
2477 + struct ip_set *set;
2478 + struct ip_set_save *set_save;
2480 + /* Pointer to our header */
2481 + set_save = (struct ip_set_save *) (data + *used);
2483 + /* Get and ensure header size */
2484 + if (*used + sizeof(struct ip_set_save) > len)
2485 + goto not_enough_mem;
2486 + *used += sizeof(struct ip_set_save);
2488 + set = ip_set_list[index];
2489 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2490 + data, data + *used);
2492 + read_lock_bh(&set->lock);
2493 + /* Get and ensure set specific header size */
2494 + set_save->header_size = set->type->header_size;
2495 + if (*used + set_save->header_size > len)
2498 + /* Fill in the header */
2499 + set_save->index = index;
2500 + set_save->binding = set->binding;
2502 + /* Fill in set spefific header data */
2503 + set->type->list_header(set, data + *used);
2504 + *used += set_save->header_size;
2506 + DP("set header filled: %s, used: %u(%u) %p %p", set->name, *used,
2507 + set_save->header_size, data, data + *used);
2508 + /* Get and ensure set specific members size */
2509 + set_save->members_size = set->type->list_members_size(set);
2510 + if (*used + set_save->members_size > len)
2513 + /* Fill in set spefific members data */
2514 + set->type->list_members(set, data + *used);
2515 + *used += set_save->members_size;
2516 + read_unlock_bh(&set->lock);
2517 + DP("set members filled: %s, used: %u(%u) %p %p", set->name, *used,
2518 + set_save->members_size, data, data + *used);
2522 + read_unlock_bh(&set->lock);
2524 + DP("not enough mem, try again");
2529 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2537 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2538 + struct ip_set_hash_save *hash_save =
2539 + (struct ip_set_hash_save *)(data + *used);
2540 + /* Ensure bindings size */
2541 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2545 + hash_save->id = set_hash->id;
2546 + hash_save->ip = set_hash->ip;
2547 + hash_save->binding = set_hash->binding;
2548 + *used += sizeof(struct ip_set_hash_save);
2552 +static int ip_set_save_bindings(ip_set_id_t index,
2558 + struct ip_set_save *set_save;
2560 + DP("used %u, len %u", *used, len);
2561 + /* Get and ensure header size */
2562 + if (*used + sizeof(struct ip_set_save) > len)
2566 + set_save = (struct ip_set_save *) (data + *used);
2567 + set_save->index = IP_SET_INVALID_ID;
2568 + set_save->header_size = 0;
2569 + set_save->members_size = 0;
2570 + *used += sizeof(struct ip_set_save);
2572 + DP("marker added used %u, len %u", *used, len);
2573 + /* Fill in bindings data */
2574 + if (index != IP_SET_INVALID_ID)
2575 + /* Sets are identified by id in hash */
2576 + index = ip_set_list[index]->id;
2577 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2585 +static int ip_set_restore(void *data,
2589 + int line = 0, used = 0, members_size;
2590 + struct ip_set *set;
2591 + struct ip_set_hash_save *hash_save;
2592 + struct ip_set_restore *set_restore;
2593 + ip_set_id_t index;
2595 + /* Loop to restore sets */
2599 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2600 + /* Get and ensure header size */
2601 + if (used + sizeof(struct ip_set_restore) > len)
2603 + set_restore = (struct ip_set_restore *) (data + used);
2604 + used += sizeof(struct ip_set_restore);
2606 + /* Ensure data size */
2608 + + set_restore->header_size
2609 + + set_restore->members_size > len)
2612 + /* Check marker */
2613 + if (set_restore->index == IP_SET_INVALID_ID) {
2618 + /* Try to create the set */
2619 + DP("restore %s %s", set_restore->name, set_restore->typename);
2620 + res = ip_set_create(set_restore->name,
2621 + set_restore->typename,
2622 + set_restore->index,
2624 + set_restore->header_size);
2628 + used += set_restore->header_size;
2630 + index = ip_set_find_byindex(set_restore->index);
2631 + DP("index %u, restore_index %u", index, set_restore->index);
2632 + if (index != set_restore->index)
2634 + /* Try to restore members data */
2635 + set = ip_set_list[index];
2637 + DP("members_size %u reqsize %u",
2638 + set_restore->members_size, set->type->reqsize);
2639 + while (members_size + set->type->reqsize <=
2640 + set_restore->members_size) {
2642 + DP("members: %u, line %u", members_size, line);
2643 + res = __ip_set_addip(index,
2644 + data + used + members_size,
2645 + set->type->reqsize);
2646 + if (!(res == 0 || res == -EEXIST))
2648 + members_size += set->type->reqsize;
2651 + DP("members_size %u %u",
2652 + set_restore->members_size, members_size);
2653 + if (members_size != set_restore->members_size)
2655 + used += set_restore->members_size;
2659 + /* Loop to restore bindings */
2660 + while (used < len) {
2663 + DP("restore binding, line %u", line);
2664 + /* Get and ensure size */
2665 + if (used + sizeof(struct ip_set_hash_save) > len)
2667 + hash_save = (struct ip_set_hash_save *) (data + used);
2668 + used += sizeof(struct ip_set_hash_save);
2670 + /* hash_save->id is used to store the index */
2671 + index = ip_set_find_byindex(hash_save->id);
2672 + DP("restore binding index %u, id %u, %u -> %u",
2673 + index, hash_save->id, hash_save->ip, hash_save->binding);
2674 + if (index != hash_save->id)
2676 + if (ip_set_find_byindex(hash_save->binding) == IP_SET_INVALID_ID) {
2677 + DP("corrupt binding set index %u", hash_save->binding);
2680 + set = ip_set_list[hash_save->id];
2681 + /* Null valued IP means default binding */
2682 + if (hash_save->ip)
2683 + res = ip_set_hash_add(set->id,
2685 + hash_save->binding);
2687 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2688 + write_lock_bh(&ip_set_lock);
2689 + set->binding = hash_save->binding;
2690 + __ip_set_get(set->binding);
2691 + write_unlock_bh(&ip_set_lock);
2692 + DP("default binding: %u", set->binding);
2704 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2707 + int res = 0; /* Assume OK */
2709 + struct ip_set_req_adt *req_adt;
2710 + ip_set_id_t index = IP_SET_INVALID_ID;
2711 + int (*adtfn)(ip_set_id_t index,
2712 + const void *data, size_t size);
2714 + int (*fn)(ip_set_id_t index,
2715 + const void *data, size_t size);
2717 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2718 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2721 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2722 + if (!capable(CAP_NET_ADMIN))
2724 + if (optval != SO_IP_SET)
2726 + if (len <= sizeof(unsigned)) {
2727 + ip_set_printk("short userdata (want >%zu, got %u)",
2728 + sizeof(unsigned), len);
2731 + data = vmalloc(len);
2733 + DP("out of mem for %u bytes", len);
2736 + if (copy_from_user(data, user, len) != 0) {
2740 + if (down_interruptible(&ip_set_app_mutex)) {
2745 + op = (unsigned *)data;
2748 + if (*op < IP_SET_OP_VERSION) {
2749 + /* Check the version at the beginning of operations */
2750 + struct ip_set_req_version *req_version =
2751 + (struct ip_set_req_version *) data;
2752 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2759 + case IP_SET_OP_CREATE:{
2760 + struct ip_set_req_create *req_create
2761 + = (struct ip_set_req_create *) data;
2763 + if (len < sizeof(struct ip_set_req_create)) {
2764 + ip_set_printk("short CREATE data (want >=%zu, got %u)",
2765 + sizeof(struct ip_set_req_create), len);
2769 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2770 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2771 + res = ip_set_create(req_create->name,
2772 + req_create->typename,
2773 + IP_SET_INVALID_ID,
2774 + data + sizeof(struct ip_set_req_create),
2775 + len - sizeof(struct ip_set_req_create));
2778 + case IP_SET_OP_DESTROY:{
2779 + struct ip_set_req_std *req_destroy
2780 + = (struct ip_set_req_std *) data;
2782 + if (len != sizeof(struct ip_set_req_std)) {
2783 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2784 + sizeof(struct ip_set_req_std), len);
2788 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2789 + /* Destroy all sets */
2790 + index = IP_SET_INVALID_ID;
2792 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2793 + index = ip_set_find_byname(req_destroy->name);
2795 + if (index == IP_SET_INVALID_ID) {
2801 + res = ip_set_destroy(index);
2804 + case IP_SET_OP_FLUSH:{
2805 + struct ip_set_req_std *req_flush =
2806 + (struct ip_set_req_std *) data;
2808 + if (len != sizeof(struct ip_set_req_std)) {
2809 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2810 + sizeof(struct ip_set_req_std), len);
2814 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2815 + /* Flush all sets */
2816 + index = IP_SET_INVALID_ID;
2818 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2819 + index = ip_set_find_byname(req_flush->name);
2821 + if (index == IP_SET_INVALID_ID) {
2826 + res = ip_set_flush(index);
2829 + case IP_SET_OP_RENAME:{
2830 + struct ip_set_req_create *req_rename
2831 + = (struct ip_set_req_create *) data;
2833 + if (len != sizeof(struct ip_set_req_create)) {
2834 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2835 + sizeof(struct ip_set_req_create), len);
2840 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2841 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2843 + index = ip_set_find_byname(req_rename->name);
2844 + if (index == IP_SET_INVALID_ID) {
2848 + res = ip_set_rename(index, req_rename->typename);
2851 + case IP_SET_OP_SWAP:{
2852 + struct ip_set_req_create *req_swap
2853 + = (struct ip_set_req_create *) data;
2854 + ip_set_id_t to_index;
2856 + if (len != sizeof(struct ip_set_req_create)) {
2857 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2858 + sizeof(struct ip_set_req_create), len);
2863 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2864 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2866 + index = ip_set_find_byname(req_swap->name);
2867 + if (index == IP_SET_INVALID_ID) {
2871 + to_index = ip_set_find_byname(req_swap->typename);
2872 + if (to_index == IP_SET_INVALID_ID) {
2876 + res = ip_set_swap(index, to_index);
2880 + break; /* Set identified by id */
2883 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2884 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2888 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2890 + if (len < sizeof(struct ip_set_req_adt)) {
2891 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2892 + sizeof(struct ip_set_req_adt), len);
2896 + req_adt = (struct ip_set_req_adt *) data;
2898 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2899 + if (!(*op == IP_SET_OP_UNBIND_SET
2900 + && req_adt->index == IP_SET_INVALID_ID)) {
2901 + index = ip_set_find_byindex(req_adt->index);
2902 + if (index == IP_SET_INVALID_ID) {
2907 + res = adtfn(index, data, len);
2910 + up(&ip_set_app_mutex);
2914 + DP("final result %d", res);
2919 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2923 + ip_set_id_t index = IP_SET_INVALID_ID;
2925 + int copylen = *len;
2927 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2928 + if (!capable(CAP_NET_ADMIN))
2930 + if (optval != SO_IP_SET)
2932 + if (*len < sizeof(unsigned)) {
2933 + ip_set_printk("short userdata (want >=%zu, got %d)",
2934 + sizeof(unsigned), *len);
2937 + data = vmalloc(*len);
2939 + DP("out of mem for %d bytes", *len);
2942 + if (copy_from_user(data, user, *len) != 0) {
2946 + if (down_interruptible(&ip_set_app_mutex)) {
2951 + op = (unsigned *) data;
2954 + if (*op < IP_SET_OP_VERSION) {
2955 + /* Check the version at the beginning of operations */
2956 + struct ip_set_req_version *req_version =
2957 + (struct ip_set_req_version *) data;
2958 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2965 + case IP_SET_OP_VERSION: {
2966 + struct ip_set_req_version *req_version =
2967 + (struct ip_set_req_version *) data;
2969 + if (*len != sizeof(struct ip_set_req_version)) {
2970 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2971 + sizeof(struct ip_set_req_version),
2977 + req_version->version = IP_SET_PROTOCOL_VERSION;
2978 + res = copy_to_user(user, req_version,
2979 + sizeof(struct ip_set_req_version));
2982 + case IP_SET_OP_GET_BYNAME: {
2983 + struct ip_set_req_get_set *req_get
2984 + = (struct ip_set_req_get_set *) data;
2986 + if (*len != sizeof(struct ip_set_req_get_set)) {
2987 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2988 + sizeof(struct ip_set_req_get_set), *len);
2992 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2993 + index = ip_set_find_byname(req_get->set.name);
2994 + req_get->set.index = index;
2997 + case IP_SET_OP_GET_BYINDEX: {
2998 + struct ip_set_req_get_set *req_get
2999 + = (struct ip_set_req_get_set *) data;
3001 + if (*len != sizeof(struct ip_set_req_get_set)) {
3002 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
3003 + sizeof(struct ip_set_req_get_set), *len);
3007 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3008 + index = ip_set_find_byindex(req_get->set.index);
3009 + strncpy(req_get->set.name,
3010 + index == IP_SET_INVALID_ID ? ""
3011 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
3014 + case IP_SET_OP_ADT_GET: {
3015 + struct ip_set_req_adt_get *req_get
3016 + = (struct ip_set_req_adt_get *) data;
3018 + if (*len != sizeof(struct ip_set_req_adt_get)) {
3019 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
3020 + sizeof(struct ip_set_req_adt_get), *len);
3024 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3025 + index = ip_set_find_byname(req_get->set.name);
3026 + if (index != IP_SET_INVALID_ID) {
3027 + req_get->set.index = index;
3028 + strncpy(req_get->typename,
3029 + ip_set_list[index]->type->typename,
3030 + IP_SET_MAXNAMELEN - 1);
3037 + case IP_SET_OP_MAX_SETS: {
3038 + struct ip_set_req_max_sets *req_max_sets
3039 + = (struct ip_set_req_max_sets *) data;
3042 + if (*len != sizeof(struct ip_set_req_max_sets)) {
3043 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
3044 + sizeof(struct ip_set_req_max_sets), *len);
3049 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
3050 + req_max_sets->set.index = IP_SET_INVALID_ID;
3052 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3053 + req_max_sets->set.index =
3054 + ip_set_find_byname(req_max_sets->set.name);
3055 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
3060 + req_max_sets->max_sets = ip_set_max;
3061 + req_max_sets->sets = 0;
3062 + for (i = 0; i < ip_set_max; i++) {
3063 + if (ip_set_list[i] != NULL)
3064 + req_max_sets->sets++;
3068 + case IP_SET_OP_LIST_SIZE:
3069 + case IP_SET_OP_SAVE_SIZE: {
3070 + struct ip_set_req_setnames *req_setnames
3071 + = (struct ip_set_req_setnames *) data;
3072 + struct ip_set_name_list *name_list;
3073 + struct ip_set *set;
3077 + if (*len < sizeof(struct ip_set_req_setnames)) {
3078 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
3079 + sizeof(struct ip_set_req_setnames), *len);
3084 + req_setnames->size = 0;
3085 + used = sizeof(struct ip_set_req_setnames);
3086 + for (i = 0; i < ip_set_max; i++) {
3087 + if (ip_set_list[i] == NULL)
3089 + name_list = (struct ip_set_name_list *)
3091 + used += sizeof(struct ip_set_name_list);
3092 + if (used > copylen) {
3096 + set = ip_set_list[i];
3097 + /* Fill in index, name, etc. */
3098 + name_list->index = i;
3099 + name_list->id = set->id;
3100 + strncpy(name_list->name,
3102 + IP_SET_MAXNAMELEN - 1);
3103 + strncpy(name_list->typename,
3104 + set->type->typename,
3105 + IP_SET_MAXNAMELEN - 1);
3106 + DP("filled %s of type %s, index %u\n",
3107 + name_list->name, name_list->typename,
3108 + name_list->index);
3109 + if (!(req_setnames->index == IP_SET_INVALID_ID
3110 + || req_setnames->index == i))
3114 + case IP_SET_OP_LIST_SIZE: {
3115 + req_setnames->size += sizeof(struct ip_set_list)
3116 + + set->type->header_size
3117 + + set->type->list_members_size(set);
3118 + /* Sets are identified by id in the hash */
3119 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
3120 + set->id, &req_setnames->size);
3123 + case IP_SET_OP_SAVE_SIZE: {
3124 + req_setnames->size += sizeof(struct ip_set_save)
3125 + + set->type->header_size
3126 + + set->type->list_members_size(set);
3127 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
3128 + set->id, &req_setnames->size);
3135 + if (copylen != used) {
3141 + case IP_SET_OP_LIST: {
3142 + struct ip_set_req_list *req_list
3143 + = (struct ip_set_req_list *) data;
3147 + if (*len < sizeof(struct ip_set_req_list)) {
3148 + ip_set_printk("short LIST (want >=%zu, got %d)",
3149 + sizeof(struct ip_set_req_list), *len);
3153 + index = req_list->index;
3154 + if (index != IP_SET_INVALID_ID
3155 + && ip_set_find_byindex(index) != index) {
3160 + if (index == IP_SET_INVALID_ID) {
3161 + /* List all sets */
3162 + for (i = 0; i < ip_set_max && res == 0; i++) {
3163 + if (ip_set_list[i] != NULL)
3164 + res = ip_set_list_set(i, data, &used, *len);
3167 + /* List an individual set */
3168 + res = ip_set_list_set(index, data, &used, *len);
3172 + else if (copylen != used) {
3178 + case IP_SET_OP_SAVE: {
3179 + struct ip_set_req_list *req_save
3180 + = (struct ip_set_req_list *) data;
3184 + if (*len < sizeof(struct ip_set_req_list)) {
3185 + ip_set_printk("short SAVE (want >=%zu, got %d)",
3186 + sizeof(struct ip_set_req_list), *len);
3190 + index = req_save->index;
3191 + if (index != IP_SET_INVALID_ID
3192 + && ip_set_find_byindex(index) != index) {
3197 + if (index == IP_SET_INVALID_ID) {
3198 + /* Save all sets */
3199 + for (i = 0; i < ip_set_max && res == 0; i++) {
3200 + if (ip_set_list[i] != NULL)
3201 + res = ip_set_save_set(i, data, &used, *len);
3204 + /* Save an individual set */
3205 + res = ip_set_save_set(index, data, &used, *len);
3208 + res = ip_set_save_bindings(index, data, &used, *len);
3212 + else if (copylen != used) {
3218 + case IP_SET_OP_RESTORE: {
3219 + struct ip_set_req_setnames *req_restore
3220 + = (struct ip_set_req_setnames *) data;
3223 + if (*len < sizeof(struct ip_set_req_setnames)
3224 + || *len != req_restore->size) {
3225 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3226 + req_restore->size, *len);
3230 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3231 + req_restore->size - sizeof(struct ip_set_req_setnames));
3232 + DP("ip_set_restore: %u", line);
3235 + req_restore->size = line;
3236 + copylen = sizeof(struct ip_set_req_setnames);
3244 + } /* end of switch(op) */
3247 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3248 + && ip_set_list[index]
3249 + ? ip_set_list[index]->name
3250 + : ":all:", copylen);
3251 + res = copy_to_user(user, data, copylen);
3254 + up(&ip_set_app_mutex);
3258 + DP("final result %d", res);
3262 +static struct nf_sockopt_ops so_set = {
3264 + .set_optmin = SO_IP_SET,
3265 + .set_optmax = SO_IP_SET + 1,
3266 + .set = &ip_set_sockfn_set,
3267 + .get_optmin = SO_IP_SET,
3268 + .get_optmax = SO_IP_SET + 1,
3269 + .get = &ip_set_sockfn_get,
3270 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3273 + .owner = THIS_MODULE,
3277 +static int max_sets, hash_size;
3278 +module_param(max_sets, int, 0600);
3279 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3280 +module_param(hash_size, int, 0600);
3281 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3282 +MODULE_LICENSE("GPL");
3283 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3284 +MODULE_DESCRIPTION("module implementing core IP set support");
3286 +static int __init ip_set_init(void)
3291 + get_random_bytes(&ip_set_hash_random, 4);
3293 + ip_set_max = max_sets;
3294 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3295 + if (!ip_set_list) {
3296 + printk(KERN_ERR "Unable to create ip_set_list\n");
3299 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3301 + ip_set_bindings_hash_size = hash_size;
3302 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3303 + if (!ip_set_hash) {
3304 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3305 + vfree(ip_set_list);
3308 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3309 + INIT_LIST_HEAD(&ip_set_hash[i]);
3311 + INIT_LIST_HEAD(&set_type_list);
3313 + res = nf_register_sockopt(&so_set);
3315 + ip_set_printk("SO_SET registry failed: %d", res);
3316 + vfree(ip_set_list);
3317 + vfree(ip_set_hash);
3323 +static void __exit ip_set_fini(void)
3325 + /* There can't be any existing set or binding */
3326 + nf_unregister_sockopt(&so_set);
3327 + vfree(ip_set_list);
3328 + vfree(ip_set_hash);
3329 + DP("these are the famous last words");
3332 +EXPORT_SYMBOL(ip_set_register_set_type);
3333 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3335 +EXPORT_SYMBOL(ip_set_get_byname);
3336 +EXPORT_SYMBOL(ip_set_get_byindex);
3337 +EXPORT_SYMBOL(ip_set_put);
3339 +EXPORT_SYMBOL(ip_set_addip_kernel);
3340 +EXPORT_SYMBOL(ip_set_delip_kernel);
3341 +EXPORT_SYMBOL(ip_set_testip_kernel);
3343 +module_init(ip_set_init);
3344 +module_exit(ip_set_fini);
3345 diff --git a/net/ipv4/netfilter/ip_set_iphash.c b/net/ipv4/netfilter/ip_set_iphash.c
3346 new file mode 100644
3347 index 0000000..63f7ac9
3349 +++ b/net/ipv4/netfilter/ip_set_iphash.c
3351 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3353 + * This program is free software; you can redistribute it and/or modify
3354 + * it under the terms of the GNU General Public License version 2 as
3355 + * published by the Free Software Foundation.
3358 +/* Kernel module implementing an ip hash set */
3360 +#include <linux/module.h>
3361 +#include <linux/ip.h>
3362 +#include <linux/skbuff.h>
3363 +#include <linux/version.h>
3364 +#include <linux/jhash.h>
3365 +#include <linux/netfilter_ipv4/ip_tables.h>
3366 +#include <linux/netfilter_ipv4/ip_set.h>
3367 +#include <linux/errno.h>
3368 +#include <asm/uaccess.h>
3369 +#include <asm/bitops.h>
3370 +#include <linux/spinlock.h>
3371 +#include <linux/vmalloc.h>
3372 +#include <linux/random.h>
3374 +#include <net/ip.h>
3376 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3377 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3379 +static int limit = MAX_RANGE;
3381 +static inline __u32
3382 +jhash_ip(const struct ip_set_iphash *map, uint16_t i, ip_set_ip_t ip)
3384 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
3387 +static inline __u32
3388 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3390 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3393 + ip_set_ip_t *elem;
3395 + *hash_ip = ip & map->netmask;
3396 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3397 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3399 + for (i = 0; i < map->probes; i++) {
3400 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
3401 + DP("hash key: %u", id);
3402 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3403 + if (*elem == *hash_ip)
3405 + /* No shortcut at testing - there can be deleted
3412 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3414 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
3418 +testip(struct ip_set *set, const void *data, size_t size,
3419 + ip_set_ip_t *hash_ip)
3421 + struct ip_set_req_iphash *req =
3422 + (struct ip_set_req_iphash *) data;
3424 + if (size != sizeof(struct ip_set_req_iphash)) {
3425 + ip_set_printk("data length wrong (want %zu, have %zu)",
3426 + sizeof(struct ip_set_req_iphash),
3430 + return __testip(set, req->ip, hash_ip);
3434 +testip_kernel(struct ip_set *set,
3435 + const struct sk_buff *skb,
3436 + ip_set_ip_t *hash_ip,
3437 + const u_int32_t *flags,
3438 + unsigned char index)
3440 + return __testip(set,
3441 + ntohl(flags[index] & IPSET_SRC
3442 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3443 + ? ip_hdr(skb)->saddr
3444 + : ip_hdr(skb)->daddr),
3446 + ? skb->nh.iph->saddr
3447 + : skb->nh.iph->daddr),
3453 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3457 + ip_set_ip_t *elem;
3459 + if (!ip || map->elements >= limit)
3462 + *hash_ip = ip & map->netmask;
3464 + for (i = 0; i < map->probes; i++) {
3465 + probe = jhash_ip(map, i, *hash_ip) % map->hashsize;
3466 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
3467 + if (*elem == *hash_ip)
3475 + /* Trigger rehashing */
3480 +addip(struct ip_set *set, const void *data, size_t size,
3481 + ip_set_ip_t *hash_ip)
3483 + struct ip_set_req_iphash *req =
3484 + (struct ip_set_req_iphash *) data;
3486 + if (size != sizeof(struct ip_set_req_iphash)) {
3487 + ip_set_printk("data length wrong (want %zu, have %zu)",
3488 + sizeof(struct ip_set_req_iphash),
3492 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3496 +addip_kernel(struct ip_set *set,
3497 + const struct sk_buff *skb,
3498 + ip_set_ip_t *hash_ip,
3499 + const u_int32_t *flags,
3500 + unsigned char index)
3502 + return __addip((struct ip_set_iphash *) set->data,
3503 + ntohl(flags[index] & IPSET_SRC
3504 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3505 + ? ip_hdr(skb)->saddr
3506 + : ip_hdr(skb)->daddr),
3508 + ? skb->nh.iph->saddr
3509 + : skb->nh.iph->daddr),
3514 +static int retry(struct ip_set *set)
3516 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3517 + ip_set_ip_t hash_ip, *elem;
3519 + u_int32_t i, hashsize = map->hashsize;
3521 + struct ip_set_iphash *tmp;
3523 + if (map->resize == 0)
3529 + /* Calculate new hash size */
3530 + hashsize += (hashsize * map->resize)/100;
3531 + if (hashsize == map->hashsize)
3534 + ip_set_printk("rehashing of set %s triggered: "
3535 + "hashsize grows from %u to %u",
3536 + set->name, map->hashsize, hashsize);
3538 + tmp = kmalloc(sizeof(struct ip_set_iphash)
3539 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
3541 + DP("out of memory for %d bytes",
3542 + sizeof(struct ip_set_iphash)
3543 + + map->probes * sizeof(uint32_t));
3546 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
3547 + if (!tmp->members) {
3548 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
3552 + tmp->hashsize = hashsize;
3553 + tmp->elements = 0;
3554 + tmp->probes = map->probes;
3555 + tmp->resize = map->resize;
3556 + tmp->netmask = map->netmask;
3557 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
3559 + write_lock_bh(&set->lock);
3560 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3561 + for (i = 0; i < map->hashsize && res == 0; i++) {
3562 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3564 + res = __addip(tmp, *elem, &hash_ip);
3567 + /* Failure, try again */
3568 + write_unlock_bh(&set->lock);
3569 + harray_free(tmp->members);
3574 + /* Success at resizing! */
3575 + members = map->members;
3577 + map->hashsize = tmp->hashsize;
3578 + map->members = tmp->members;
3579 + write_unlock_bh(&set->lock);
3581 + harray_free(members);
3588 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3590 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3591 + ip_set_ip_t id, *elem;
3596 + id = hash_id(set, ip, hash_ip);
3597 + if (id == UINT_MAX)
3600 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3608 +delip(struct ip_set *set, const void *data, size_t size,
3609 + ip_set_ip_t *hash_ip)
3611 + struct ip_set_req_iphash *req =
3612 + (struct ip_set_req_iphash *) data;
3614 + if (size != sizeof(struct ip_set_req_iphash)) {
3615 + ip_set_printk("data length wrong (want %zu, have %zu)",
3616 + sizeof(struct ip_set_req_iphash),
3620 + return __delip(set, req->ip, hash_ip);
3624 +delip_kernel(struct ip_set *set,
3625 + const struct sk_buff *skb,
3626 + ip_set_ip_t *hash_ip,
3627 + const u_int32_t *flags,
3628 + unsigned char index)
3630 + return __delip(set,
3631 + ntohl(flags[index] & IPSET_SRC
3632 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3633 + ? ip_hdr(skb)->saddr
3634 + : ip_hdr(skb)->daddr),
3636 + ? skb->nh.iph->saddr
3637 + : skb->nh.iph->daddr),
3642 +static int create(struct ip_set *set, const void *data, size_t size)
3644 + struct ip_set_req_iphash_create *req =
3645 + (struct ip_set_req_iphash_create *) data;
3646 + struct ip_set_iphash *map;
3649 + if (size != sizeof(struct ip_set_req_iphash_create)) {
3650 + ip_set_printk("data length wrong (want %zu, have %zu)",
3651 + sizeof(struct ip_set_req_iphash_create),
3656 + if (req->hashsize < 1) {
3657 + ip_set_printk("hashsize too small");
3661 + if (req->probes < 1) {
3662 + ip_set_printk("probes too small");
3666 + map = kmalloc(sizeof(struct ip_set_iphash)
3667 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
3669 + DP("out of memory for %d bytes",
3670 + sizeof(struct ip_set_iphash)
3671 + + req->probes * sizeof(uint32_t));
3674 + for (i = 0; i < req->probes; i++)
3675 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
3676 + map->elements = 0;
3677 + map->hashsize = req->hashsize;
3678 + map->probes = req->probes;
3679 + map->resize = req->resize;
3680 + map->netmask = req->netmask;
3681 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
3682 + if (!map->members) {
3683 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
3692 +static void destroy(struct ip_set *set)
3694 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3696 + harray_free(map->members);
3702 +static void flush(struct ip_set *set)
3704 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3705 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
3706 + map->elements = 0;
3709 +static void list_header(const struct ip_set *set, void *data)
3711 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3712 + struct ip_set_req_iphash_create *header =
3713 + (struct ip_set_req_iphash_create *) data;
3715 + header->hashsize = map->hashsize;
3716 + header->probes = map->probes;
3717 + header->resize = map->resize;
3718 + header->netmask = map->netmask;
3721 +static int list_members_size(const struct ip_set *set)
3723 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3725 + return (map->hashsize * sizeof(ip_set_ip_t));
3728 +static void list_members(const struct ip_set *set, void *data)
3730 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3731 + ip_set_ip_t i, *elem;
3733 + for (i = 0; i < map->hashsize; i++) {
3734 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3735 + ((ip_set_ip_t *)data)[i] = *elem;
3739 +static struct ip_set_type ip_set_iphash = {
3740 + .typename = SETTYPE_NAME,
3741 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
3742 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3743 + .create = &create,
3744 + .destroy = &destroy,
3746 + .reqsize = sizeof(struct ip_set_req_iphash),
3748 + .addip_kernel = &addip_kernel,
3751 + .delip_kernel = &delip_kernel,
3752 + .testip = &testip,
3753 + .testip_kernel = &testip_kernel,
3754 + .header_size = sizeof(struct ip_set_req_iphash_create),
3755 + .list_header = &list_header,
3756 + .list_members_size = &list_members_size,
3757 + .list_members = &list_members,
3758 + .me = THIS_MODULE,
3761 +MODULE_LICENSE("GPL");
3762 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3763 +MODULE_DESCRIPTION("iphash type of IP sets");
3764 +module_param(limit, int, 0600);
3765 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
3767 +static int __init ip_set_iphash_init(void)
3769 + return ip_set_register_set_type(&ip_set_iphash);
3772 +static void __exit ip_set_iphash_fini(void)
3774 + /* FIXME: possible race with ip_set_create() */
3775 + ip_set_unregister_set_type(&ip_set_iphash);
3778 +module_init(ip_set_iphash_init);
3779 +module_exit(ip_set_iphash_fini);
3780 diff --git a/net/ipv4/netfilter/ip_set_ipmap.c b/net/ipv4/netfilter/ip_set_ipmap.c
3781 new file mode 100644
3782 index 0000000..0fe8d3c
3784 +++ b/net/ipv4/netfilter/ip_set_ipmap.c
3786 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3787 + * Patrick Schaaf <bof@bof.de>
3788 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3790 + * This program is free software; you can redistribute it and/or modify
3791 + * it under the terms of the GNU General Public License version 2 as
3792 + * published by the Free Software Foundation.
3795 +/* Kernel module implementing an IP set type: the single bitmap type */
3797 +#include <linux/module.h>
3798 +#include <linux/ip.h>
3799 +#include <linux/skbuff.h>
3800 +#include <linux/version.h>
3801 +#include <linux/netfilter_ipv4/ip_tables.h>
3802 +#include <linux/netfilter_ipv4/ip_set.h>
3803 +#include <linux/errno.h>
3804 +#include <asm/uaccess.h>
3805 +#include <asm/bitops.h>
3806 +#include <linux/spinlock.h>
3808 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3810 +static inline ip_set_ip_t
3811 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3813 + return (ip - map->first_ip)/map->hosts;
3817 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3819 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3821 + if (ip < map->first_ip || ip > map->last_ip)
3824 + *hash_ip = ip & map->netmask;
3825 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3826 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3827 + return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3831 +testip(struct ip_set *set, const void *data, size_t size,
3832 + ip_set_ip_t *hash_ip)
3834 + struct ip_set_req_ipmap *req =
3835 + (struct ip_set_req_ipmap *) data;
3837 + if (size != sizeof(struct ip_set_req_ipmap)) {
3838 + ip_set_printk("data length wrong (want %zu, have %zu)",
3839 + sizeof(struct ip_set_req_ipmap),
3843 + return __testip(set, req->ip, hash_ip);
3847 +testip_kernel(struct ip_set *set,
3848 + const struct sk_buff *skb,
3849 + ip_set_ip_t *hash_ip,
3850 + const u_int32_t *flags,
3851 + unsigned char index)
3853 + int res = __testip(set,
3854 + ntohl(flags[index] & IPSET_SRC
3855 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3856 + ? ip_hdr(skb)->saddr
3857 + : ip_hdr(skb)->daddr),
3859 + ? skb->nh.iph->saddr
3860 + : skb->nh.iph->daddr),
3863 + return (res < 0 ? 0 : res);
3867 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3869 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3871 + if (ip < map->first_ip || ip > map->last_ip)
3874 + *hash_ip = ip & map->netmask;
3875 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3876 + if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3883 +addip(struct ip_set *set, const void *data, size_t size,
3884 + ip_set_ip_t *hash_ip)
3886 + struct ip_set_req_ipmap *req =
3887 + (struct ip_set_req_ipmap *) data;
3889 + if (size != sizeof(struct ip_set_req_ipmap)) {
3890 + ip_set_printk("data length wrong (want %zu, have %zu)",
3891 + sizeof(struct ip_set_req_ipmap),
3895 + DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3896 + return __addip(set, req->ip, hash_ip);
3900 +addip_kernel(struct ip_set *set,
3901 + const struct sk_buff *skb,
3902 + ip_set_ip_t *hash_ip,
3903 + const u_int32_t *flags,
3904 + unsigned char index)
3906 + return __addip(set,
3907 + ntohl(flags[index] & IPSET_SRC
3908 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3909 + ? ip_hdr(skb)->saddr
3910 + : ip_hdr(skb)->daddr),
3912 + ? skb->nh.iph->saddr
3913 + : skb->nh.iph->daddr),
3919 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3921 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3923 + if (ip < map->first_ip || ip > map->last_ip)
3926 + *hash_ip = ip & map->netmask;
3927 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3928 + if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3935 +delip(struct ip_set *set, const void *data, size_t size,
3936 + ip_set_ip_t *hash_ip)
3938 + struct ip_set_req_ipmap *req =
3939 + (struct ip_set_req_ipmap *) data;
3941 + if (size != sizeof(struct ip_set_req_ipmap)) {
3942 + ip_set_printk("data length wrong (want %zu, have %zu)",
3943 + sizeof(struct ip_set_req_ipmap),
3947 + return __delip(set, req->ip, hash_ip);
3951 +delip_kernel(struct ip_set *set,
3952 + const struct sk_buff *skb,
3953 + ip_set_ip_t *hash_ip,
3954 + const u_int32_t *flags,
3955 + unsigned char index)
3957 + return __delip(set,
3958 + ntohl(flags[index] & IPSET_SRC
3959 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3960 + ? ip_hdr(skb)->saddr
3961 + : ip_hdr(skb)->daddr),
3963 + ? skb->nh.iph->saddr
3964 + : skb->nh.iph->daddr),
3969 +static int create(struct ip_set *set, const void *data, size_t size)
3972 + struct ip_set_req_ipmap_create *req =
3973 + (struct ip_set_req_ipmap_create *) data;
3974 + struct ip_set_ipmap *map;
3976 + if (size != sizeof(struct ip_set_req_ipmap_create)) {
3977 + ip_set_printk("data length wrong (want %zu, have %zu)",
3978 + sizeof(struct ip_set_req_ipmap_create),
3983 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3984 + HIPQUAD(req->from), HIPQUAD(req->to));
3986 + if (req->from > req->to) {
3987 + DP("bad ip range");
3991 + map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3993 + DP("out of memory for %d bytes",
3994 + sizeof(struct ip_set_ipmap));
3997 + map->first_ip = req->from;
3998 + map->last_ip = req->to;
3999 + map->netmask = req->netmask;
4001 + if (req->netmask == 0xFFFFFFFF) {
4003 + map->sizeid = map->last_ip - map->first_ip + 1;
4005 + unsigned int mask_bits, netmask_bits;
4008 + map->first_ip &= map->netmask; /* Should we better bark? */
4010 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
4011 + netmask_bits = mask_to_bits(map->netmask);
4013 + if ((!mask && (map->first_ip || map->last_ip != 0xFFFFFFFF))
4014 + || netmask_bits <= mask_bits)
4017 + DP("mask_bits %u, netmask_bits %u",
4018 + mask_bits, netmask_bits);
4019 + map->hosts = 2 << (32 - netmask_bits - 1);
4020 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
4022 + if (map->sizeid > MAX_RANGE + 1) {
4023 + ip_set_printk("range too big (max %d addresses)",
4028 + DP("hosts %u, sizeid %u", map->hosts, map->sizeid);
4029 + newbytes = bitmap_bytes(0, map->sizeid - 1);
4030 + map->members = kmalloc(newbytes, GFP_KERNEL);
4031 + if (!map->members) {
4032 + DP("out of memory for %d bytes", newbytes);
4036 + memset(map->members, 0, newbytes);
4042 +static void destroy(struct ip_set *set)
4044 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
4046 + kfree(map->members);
4052 +static void flush(struct ip_set *set)
4054 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
4055 + memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
4058 +static void list_header(const struct ip_set *set, void *data)
4060 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
4061 + struct ip_set_req_ipmap_create *header =
4062 + (struct ip_set_req_ipmap_create *) data;
4064 + header->from = map->first_ip;
4065 + header->to = map->last_ip;
4066 + header->netmask = map->netmask;
4069 +static int list_members_size(const struct ip_set *set)
4071 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
4073 + return bitmap_bytes(0, map->sizeid - 1);
4076 +static void list_members(const struct ip_set *set, void *data)
4078 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
4079 + int bytes = bitmap_bytes(0, map->sizeid - 1);
4081 + memcpy(data, map->members, bytes);
4084 +static struct ip_set_type ip_set_ipmap = {
4085 + .typename = SETTYPE_NAME,
4086 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
4087 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4088 + .create = &create,
4089 + .destroy = &destroy,
4091 + .reqsize = sizeof(struct ip_set_req_ipmap),
4093 + .addip_kernel = &addip_kernel,
4095 + .delip_kernel = &delip_kernel,
4096 + .testip = &testip,
4097 + .testip_kernel = &testip_kernel,
4098 + .header_size = sizeof(struct ip_set_req_ipmap_create),
4099 + .list_header = &list_header,
4100 + .list_members_size = &list_members_size,
4101 + .list_members = &list_members,
4102 + .me = THIS_MODULE,
4105 +MODULE_LICENSE("GPL");
4106 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4107 +MODULE_DESCRIPTION("ipmap type of IP sets");
4109 +static int __init ip_set_ipmap_init(void)
4111 + return ip_set_register_set_type(&ip_set_ipmap);
4114 +static void __exit ip_set_ipmap_fini(void)
4116 + /* FIXME: possible race with ip_set_create() */
4117 + ip_set_unregister_set_type(&ip_set_ipmap);
4120 +module_init(ip_set_ipmap_init);
4121 +module_exit(ip_set_ipmap_fini);
4122 diff --git a/net/ipv4/netfilter/ip_set_ipporthash.c b/net/ipv4/netfilter/ip_set_ipporthash.c
4123 new file mode 100644
4124 index 0000000..0d562b8
4126 +++ b/net/ipv4/netfilter/ip_set_ipporthash.c
4128 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4130 + * This program is free software; you can redistribute it and/or modify
4131 + * it under the terms of the GNU General Public License version 2 as
4132 + * published by the Free Software Foundation.
4135 +/* Kernel module implementing an ip+port hash set */
4137 +#include <linux/module.h>
4138 +#include <linux/ip.h>
4139 +#include <linux/tcp.h>
4140 +#include <linux/udp.h>
4141 +#include <linux/skbuff.h>
4142 +#include <linux/version.h>
4143 +#include <linux/jhash.h>
4144 +#include <linux/netfilter_ipv4/ip_tables.h>
4145 +#include <linux/netfilter_ipv4/ip_set.h>
4146 +#include <linux/errno.h>
4147 +#include <asm/uaccess.h>
4148 +#include <asm/bitops.h>
4149 +#include <linux/spinlock.h>
4150 +#include <linux/vmalloc.h>
4151 +#include <linux/random.h>
4153 +#include <net/ip.h>
4155 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4156 +#include <linux/netfilter_ipv4/ip_set_ipporthash.h>
4158 +static int limit = MAX_RANGE;
4160 +/* We must handle non-linear skbs */
4161 +static inline ip_set_ip_t
4162 +get_port(const struct sk_buff *skb, u_int32_t flags)
4164 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4165 + struct iphdr *iph = ip_hdr(skb);
4167 + struct iphdr *iph = skb->nh.iph;
4169 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
4171 + switch (iph->protocol) {
4172 + case IPPROTO_TCP: {
4173 + struct tcphdr tcph;
4175 + /* See comments at tcp_match in ip_tables.c */
4177 + return INVALID_PORT;
4179 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4180 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
4182 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
4184 + /* No choice either */
4185 + return INVALID_PORT;
4187 + return ntohs(flags & IPSET_SRC ?
4188 + tcph.source : tcph.dest);
4190 + case IPPROTO_UDP: {
4191 + struct udphdr udph;
4194 + return INVALID_PORT;
4196 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4197 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
4199 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
4201 + /* No choice either */
4202 + return INVALID_PORT;
4204 + return ntohs(flags & IPSET_SRC ?
4205 + udph.source : udph.dest);
4208 + return INVALID_PORT;
4212 +static inline __u32
4213 +jhash_ip(const struct ip_set_ipporthash *map, uint16_t i, ip_set_ip_t ip)
4215 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
4218 +#define HASH_IP(map, ip, port) (port + ((ip - ((map)->first_ip)) << 16))
4220 +static inline __u32
4221 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4222 + ip_set_ip_t *hash_ip)
4224 + struct ip_set_ipporthash *map =
4225 + (struct ip_set_ipporthash *) set->data;
4228 + ip_set_ip_t *elem;
4230 + *hash_ip = HASH_IP(map, ip, port);
4231 + DP("set: %s, ipport:%u.%u.%u.%u:%u, %u.%u.%u.%u",
4232 + set->name, HIPQUAD(ip), port, HIPQUAD(*hash_ip));
4234 + for (i = 0; i < map->probes; i++) {
4235 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
4236 + DP("hash key: %u", id);
4237 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4238 + if (*elem == *hash_ip)
4240 + /* No shortcut at testing - there can be deleted
4247 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4248 + ip_set_ip_t *hash_ip)
4250 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4252 + if (ip < map->first_ip || ip > map->last_ip)
4255 + return (hash_id(set, ip, port, hash_ip) != UINT_MAX);
4259 +testip(struct ip_set *set, const void *data, size_t size,
4260 + ip_set_ip_t *hash_ip)
4262 + struct ip_set_req_ipporthash *req =
4263 + (struct ip_set_req_ipporthash *) data;
4265 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4266 + ip_set_printk("data length wrong (want %zu, have %zu)",
4267 + sizeof(struct ip_set_req_ipporthash),
4271 + return __testip(set, req->ip, req->port, hash_ip);
4275 +testip_kernel(struct ip_set *set,
4276 + const struct sk_buff *skb,
4277 + ip_set_ip_t *hash_ip,
4278 + const u_int32_t *flags,
4279 + unsigned char index)
4284 + if (flags[index+1] == 0)
4287 + port = get_port(skb, flags[index+1]);
4289 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4290 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4291 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4292 + NIPQUAD(ip_hdr(skb)->saddr),
4293 + NIPQUAD(ip_hdr(skb)->daddr));
4295 + NIPQUAD(skb->nh.iph->saddr),
4296 + NIPQUAD(skb->nh.iph->daddr));
4298 + DP("flag %s port %u",
4299 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4301 + if (port == INVALID_PORT)
4304 + res = __testip(set,
4305 + ntohl(flags[index] & IPSET_SRC
4306 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4307 + ? ip_hdr(skb)->saddr
4308 + : ip_hdr(skb)->daddr),
4310 + ? skb->nh.iph->saddr
4311 + : skb->nh.iph->daddr),
4315 + return (res < 0 ? 0 : res);
4320 +__add_haship(struct ip_set_ipporthash *map, ip_set_ip_t hash_ip)
4324 + ip_set_ip_t *elem;
4326 + for (i = 0; i < map->probes; i++) {
4327 + probe = jhash_ip(map, i, hash_ip) % map->hashsize;
4328 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
4329 + if (*elem == hash_ip)
4337 + /* Trigger rehashing */
4342 +__addip(struct ip_set_ipporthash *map, ip_set_ip_t ip, ip_set_ip_t port,
4343 + ip_set_ip_t *hash_ip)
4345 + if (map->elements > limit)
4347 + if (ip < map->first_ip || ip > map->last_ip)
4350 + *hash_ip = HASH_IP(map, ip, port);
4352 + return __add_haship(map, *hash_ip);
4356 +addip(struct ip_set *set, const void *data, size_t size,
4357 + ip_set_ip_t *hash_ip)
4359 + struct ip_set_req_ipporthash *req =
4360 + (struct ip_set_req_ipporthash *) data;
4362 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4363 + ip_set_printk("data length wrong (want %zu, have %zu)",
4364 + sizeof(struct ip_set_req_ipporthash),
4368 + return __addip((struct ip_set_ipporthash *) set->data,
4369 + req->ip, req->port, hash_ip);
4373 +addip_kernel(struct ip_set *set,
4374 + const struct sk_buff *skb,
4375 + ip_set_ip_t *hash_ip,
4376 + const u_int32_t *flags,
4377 + unsigned char index)
4381 + if (flags[index+1] == 0)
4384 + port = get_port(skb, flags[index+1]);
4386 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4387 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4388 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4389 + NIPQUAD(ip_hdr(skb)->saddr),
4390 + NIPQUAD(ip_hdr(skb)->daddr));
4392 + NIPQUAD(skb->nh.iph->saddr),
4393 + NIPQUAD(skb->nh.iph->daddr));
4395 + DP("flag %s port %u",
4396 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4398 + if (port == INVALID_PORT)
4401 + return __addip((struct ip_set_ipporthash *) set->data,
4402 + ntohl(flags[index] & IPSET_SRC
4403 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4404 + ? ip_hdr(skb)->saddr
4405 + : ip_hdr(skb)->daddr),
4407 + ? skb->nh.iph->saddr
4408 + : skb->nh.iph->daddr),
4414 +static int retry(struct ip_set *set)
4416 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4417 + ip_set_ip_t *elem;
4419 + u_int32_t i, hashsize = map->hashsize;
4421 + struct ip_set_ipporthash *tmp;
4423 + if (map->resize == 0)
4429 + /* Calculate new hash size */
4430 + hashsize += (hashsize * map->resize)/100;
4431 + if (hashsize == map->hashsize)
4434 + ip_set_printk("rehashing of set %s triggered: "
4435 + "hashsize grows from %u to %u",
4436 + set->name, map->hashsize, hashsize);
4438 + tmp = kmalloc(sizeof(struct ip_set_ipporthash)
4439 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
4441 + DP("out of memory for %d bytes",
4442 + sizeof(struct ip_set_ipporthash)
4443 + + map->probes * sizeof(uint32_t));
4446 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
4447 + if (!tmp->members) {
4448 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
4452 + tmp->hashsize = hashsize;
4453 + tmp->elements = 0;
4454 + tmp->probes = map->probes;
4455 + tmp->resize = map->resize;
4456 + tmp->first_ip = map->first_ip;
4457 + tmp->last_ip = map->last_ip;
4458 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
4460 + write_lock_bh(&set->lock);
4461 + map = (struct ip_set_ipporthash *) set->data; /* Play safe */
4462 + for (i = 0; i < map->hashsize && res == 0; i++) {
4463 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
4465 + res = __add_haship(tmp, *elem);
4468 + /* Failure, try again */
4469 + write_unlock_bh(&set->lock);
4470 + harray_free(tmp->members);
4475 + /* Success at resizing! */
4476 + members = map->members;
4478 + map->hashsize = tmp->hashsize;
4479 + map->members = tmp->members;
4480 + write_unlock_bh(&set->lock);
4482 + harray_free(members);
4489 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4490 + ip_set_ip_t *hash_ip)
4492 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4494 + ip_set_ip_t *elem;
4496 + if (ip < map->first_ip || ip > map->last_ip)
4499 + id = hash_id(set, ip, port, hash_ip);
4501 + if (id == UINT_MAX)
4504 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4512 +delip(struct ip_set *set, const void *data, size_t size,
4513 + ip_set_ip_t *hash_ip)
4515 + struct ip_set_req_ipporthash *req =
4516 + (struct ip_set_req_ipporthash *) data;
4518 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4519 + ip_set_printk("data length wrong (want %zu, have %zu)",
4520 + sizeof(struct ip_set_req_ipporthash),
4524 + return __delip(set, req->ip, req->port, hash_ip);
4528 +delip_kernel(struct ip_set *set,
4529 + const struct sk_buff *skb,
4530 + ip_set_ip_t *hash_ip,
4531 + const u_int32_t *flags,
4532 + unsigned char index)
4536 + if (flags[index+1] == 0)
4539 + port = get_port(skb, flags[index+1]);
4541 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4542 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4543 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4544 + NIPQUAD(ip_hdr(skb)->saddr),
4545 + NIPQUAD(ip_hdr(skb)->daddr));
4547 + NIPQUAD(skb->nh.iph->saddr),
4548 + NIPQUAD(skb->nh.iph->daddr));
4550 + DP("flag %s port %u",
4551 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4553 + if (port == INVALID_PORT)
4556 + return __delip(set,
4557 + ntohl(flags[index] & IPSET_SRC
4558 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4559 + ? ip_hdr(skb)->saddr
4560 + : ip_hdr(skb)->daddr),
4562 + ? skb->nh.iph->saddr
4563 + : skb->nh.iph->daddr),
4569 +static int create(struct ip_set *set, const void *data, size_t size)
4571 + struct ip_set_req_ipporthash_create *req =
4572 + (struct ip_set_req_ipporthash_create *) data;
4573 + struct ip_set_ipporthash *map;
4576 + if (size != sizeof(struct ip_set_req_ipporthash_create)) {
4577 + ip_set_printk("data length wrong (want %zu, have %zu)",
4578 + sizeof(struct ip_set_req_ipporthash_create),
4583 + if (req->hashsize < 1) {
4584 + ip_set_printk("hashsize too small");
4588 + if (req->probes < 1) {
4589 + ip_set_printk("probes too small");
4593 + map = kmalloc(sizeof(struct ip_set_ipporthash)
4594 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
4596 + DP("out of memory for %d bytes",
4597 + sizeof(struct ip_set_ipporthash)
4598 + + req->probes * sizeof(uint32_t));
4601 + for (i = 0; i < req->probes; i++)
4602 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
4603 + map->elements = 0;
4604 + map->hashsize = req->hashsize;
4605 + map->probes = req->probes;
4606 + map->resize = req->resize;
4607 + map->first_ip = req->from;
4608 + map->last_ip = req->to;
4609 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
4610 + if (!map->members) {
4611 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
4620 +static void destroy(struct ip_set *set)
4622 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4624 + harray_free(map->members);
4630 +static void flush(struct ip_set *set)
4632 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4633 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
4634 + map->elements = 0;
4637 +static void list_header(const struct ip_set *set, void *data)
4639 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4640 + struct ip_set_req_ipporthash_create *header =
4641 + (struct ip_set_req_ipporthash_create *) data;
4643 + header->hashsize = map->hashsize;
4644 + header->probes = map->probes;
4645 + header->resize = map->resize;
4646 + header->from = map->first_ip;
4647 + header->to = map->last_ip;
4650 +static int list_members_size(const struct ip_set *set)
4652 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4654 + return (map->hashsize * sizeof(ip_set_ip_t));
4657 +static void list_members(const struct ip_set *set, void *data)
4659 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4660 + ip_set_ip_t i, *elem;
4662 + for (i = 0; i < map->hashsize; i++) {
4663 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
4664 + ((ip_set_ip_t *)data)[i] = *elem;
4668 +static struct ip_set_type ip_set_ipporthash = {
4669 + .typename = SETTYPE_NAME,
4670 + .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_DATA_DOUBLE,
4671 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4672 + .create = &create,
4673 + .destroy = &destroy,
4675 + .reqsize = sizeof(struct ip_set_req_ipporthash),
4677 + .addip_kernel = &addip_kernel,
4680 + .delip_kernel = &delip_kernel,
4681 + .testip = &testip,
4682 + .testip_kernel = &testip_kernel,
4683 + .header_size = sizeof(struct ip_set_req_ipporthash_create),
4684 + .list_header = &list_header,
4685 + .list_members_size = &list_members_size,
4686 + .list_members = &list_members,
4687 + .me = THIS_MODULE,
4690 +MODULE_LICENSE("GPL");
4691 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4692 +MODULE_DESCRIPTION("ipporthash type of IP sets");
4693 +module_param(limit, int, 0600);
4694 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
4696 +static int __init ip_set_ipporthash_init(void)
4698 + return ip_set_register_set_type(&ip_set_ipporthash);
4701 +static void __exit ip_set_ipporthash_fini(void)
4703 + /* FIXME: possible race with ip_set_create() */
4704 + ip_set_unregister_set_type(&ip_set_ipporthash);
4707 +module_init(ip_set_ipporthash_init);
4708 +module_exit(ip_set_ipporthash_fini);
4709 diff --git a/net/ipv4/netfilter/ip_set_iptree.c b/net/ipv4/netfilter/ip_set_iptree.c
4710 new file mode 100644
4711 index 0000000..0180534
4713 +++ b/net/ipv4/netfilter/ip_set_iptree.c
4715 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4717 + * This program is free software; you can redistribute it and/or modify
4718 + * it under the terms of the GNU General Public License version 2 as
4719 + * published by the Free Software Foundation.
4722 +/* Kernel module implementing an IP set type: the iptree type */
4724 +#include <linux/version.h>
4725 +#include <linux/module.h>
4726 +#include <linux/ip.h>
4727 +#include <linux/skbuff.h>
4728 +#include <linux/slab.h>
4729 +#include <linux/delay.h>
4730 +#include <linux/netfilter_ipv4/ip_tables.h>
4731 +#include <linux/netfilter_ipv4/ip_set.h>
4732 +#include <linux/errno.h>
4733 +#include <asm/uaccess.h>
4734 +#include <asm/bitops.h>
4735 +#include <linux/spinlock.h>
4737 +/* Backward compatibility */
4742 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
4744 +static int limit = MAX_RANGE;
4746 +/* Garbage collection interval in seconds: */
4747 +#define IPTREE_GC_TIME 5*60
4748 +/* Sleep so many milliseconds before trying again
4749 + * to delete the gc timer at destroying/flushing a set */
4750 +#define IPTREE_DESTROY_SLEEP 100
4752 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
4753 +static struct kmem_cache *branch_cachep;
4754 +static struct kmem_cache *leaf_cachep;
4756 +static kmem_cache_t *branch_cachep;
4757 +static kmem_cache_t *leaf_cachep;
4760 +#if defined(__LITTLE_ENDIAN)
4761 +#define ABCD(a,b,c,d,addrp) do { \
4762 + a = ((unsigned char *)addrp)[3]; \
4763 + b = ((unsigned char *)addrp)[2]; \
4764 + c = ((unsigned char *)addrp)[1]; \
4765 + d = ((unsigned char *)addrp)[0]; \
4767 +#elif defined(__BIG_ENDIAN)
4768 +#define ABCD(a,b,c,d,addrp) do { \
4769 + a = ((unsigned char *)addrp)[0]; \
4770 + b = ((unsigned char *)addrp)[1]; \
4771 + c = ((unsigned char *)addrp)[2]; \
4772 + d = ((unsigned char *)addrp)[3]; \
4775 +#error "Please fix asm/byteorder.h"
4776 +#endif /* __LITTLE_ENDIAN */
4778 +#define TESTIP_WALK(map, elem, branch) do { \
4779 + if ((map)->tree[elem]) { \
4780 + branch = (map)->tree[elem]; \
4786 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4788 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4789 + struct ip_set_iptreeb *btree;
4790 + struct ip_set_iptreec *ctree;
4791 + struct ip_set_iptreed *dtree;
4792 + unsigned char a,b,c,d;
4798 + ABCD(a, b, c, d, hash_ip);
4799 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
4800 + TESTIP_WALK(map, a, btree);
4801 + TESTIP_WALK(btree, b, ctree);
4802 + TESTIP_WALK(ctree, c, dtree);
4803 + DP("%lu %lu", dtree->expires[d], jiffies);
4804 + return dtree->expires[d]
4806 + || time_after(dtree->expires[d], jiffies));
4810 +testip(struct ip_set *set, const void *data, size_t size,
4811 + ip_set_ip_t *hash_ip)
4813 + struct ip_set_req_iptree *req =
4814 + (struct ip_set_req_iptree *) data;
4816 + if (size != sizeof(struct ip_set_req_iptree)) {
4817 + ip_set_printk("data length wrong (want %zu, have %zu)",
4818 + sizeof(struct ip_set_req_iptree),
4822 + return __testip(set, req->ip, hash_ip);
4826 +testip_kernel(struct ip_set *set,
4827 + const struct sk_buff *skb,
4828 + ip_set_ip_t *hash_ip,
4829 + const u_int32_t *flags,
4830 + unsigned char index)
4834 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4835 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4836 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4837 + NIPQUAD(ip_hdr(skb)->saddr),
4838 + NIPQUAD(ip_hdr(skb)->daddr));
4840 + NIPQUAD(skb->nh.iph->saddr),
4841 + NIPQUAD(skb->nh.iph->daddr));
4844 + res = __testip(set,
4845 + ntohl(flags[index] & IPSET_SRC
4846 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4847 + ? ip_hdr(skb)->saddr
4848 + : ip_hdr(skb)->daddr),
4850 + ? skb->nh.iph->saddr
4851 + : skb->nh.iph->daddr),
4854 + return (res < 0 ? 0 : res);
4857 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
4858 + if ((map)->tree[elem]) { \
4859 + DP("found %u", elem); \
4860 + branch = (map)->tree[elem]; \
4862 + branch = (type *) \
4863 + kmem_cache_alloc(cachep, GFP_ATOMIC); \
4864 + if (branch == NULL) \
4866 + memset(branch, 0, sizeof(*branch)); \
4867 + (map)->tree[elem] = branch; \
4868 + DP("alloc %u", elem); \
4873 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
4874 + ip_set_ip_t *hash_ip)
4876 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4877 + struct ip_set_iptreeb *btree;
4878 + struct ip_set_iptreec *ctree;
4879 + struct ip_set_iptreed *dtree;
4880 + unsigned char a,b,c,d;
4883 + if (!ip || map->elements >= limit)
4884 + /* We could call the garbage collector
4885 + * but it's probably overkill */
4889 + ABCD(a, b, c, d, hash_ip);
4890 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
4891 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
4892 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
4893 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
4894 + if (dtree->expires[d]
4895 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4897 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
4898 + /* Lottery: I won! */
4899 + if (dtree->expires[d] == 0)
4900 + dtree->expires[d] = 1;
4901 + DP("%u %lu", d, dtree->expires[d]);
4908 +addip(struct ip_set *set, const void *data, size_t size,
4909 + ip_set_ip_t *hash_ip)
4911 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4912 + struct ip_set_req_iptree *req =
4913 + (struct ip_set_req_iptree *) data;
4915 + if (size != sizeof(struct ip_set_req_iptree)) {
4916 + ip_set_printk("data length wrong (want %zu, have %zu)",
4917 + sizeof(struct ip_set_req_iptree),
4921 + DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
4922 + return __addip(set, req->ip,
4923 + req->timeout ? req->timeout : map->timeout,
4928 +addip_kernel(struct ip_set *set,
4929 + const struct sk_buff *skb,
4930 + ip_set_ip_t *hash_ip,
4931 + const u_int32_t *flags,
4932 + unsigned char index)
4934 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4936 + return __addip(set,
4937 + ntohl(flags[index] & IPSET_SRC
4938 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4939 + ? ip_hdr(skb)->saddr
4940 + : ip_hdr(skb)->daddr),
4942 + ? skb->nh.iph->saddr
4943 + : skb->nh.iph->daddr),
4949 +#define DELIP_WALK(map, elem, branch) do { \
4950 + if ((map)->tree[elem]) { \
4951 + branch = (map)->tree[elem]; \
4957 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4959 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4960 + struct ip_set_iptreeb *btree;
4961 + struct ip_set_iptreec *ctree;
4962 + struct ip_set_iptreed *dtree;
4963 + unsigned char a,b,c,d;
4969 + ABCD(a, b, c, d, hash_ip);
4970 + DELIP_WALK(map, a, btree);
4971 + DELIP_WALK(btree, b, ctree);
4972 + DELIP_WALK(ctree, c, dtree);
4974 + if (dtree->expires[d]) {
4975 + dtree->expires[d] = 0;
4983 +delip(struct ip_set *set, const void *data, size_t size,
4984 + ip_set_ip_t *hash_ip)
4986 + struct ip_set_req_iptree *req =
4987 + (struct ip_set_req_iptree *) data;
4989 + if (size != sizeof(struct ip_set_req_iptree)) {
4990 + ip_set_printk("data length wrong (want %zu, have %zu)",
4991 + sizeof(struct ip_set_req_iptree),
4995 + return __delip(set, req->ip, hash_ip);
4999 +delip_kernel(struct ip_set *set,
5000 + const struct sk_buff *skb,
5001 + ip_set_ip_t *hash_ip,
5002 + const u_int32_t *flags,
5003 + unsigned char index)
5005 + return __delip(set,
5006 + ntohl(flags[index] & IPSET_SRC
5007 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5008 + ? ip_hdr(skb)->saddr
5009 + : ip_hdr(skb)->daddr),
5011 + ? skb->nh.iph->saddr
5012 + : skb->nh.iph->daddr),
5017 +#define LOOP_WALK_BEGIN(map, i, branch) \
5018 + for (i = 0; i < 256; i++) { \
5019 + if (!(map)->tree[i]) \
5021 + branch = (map)->tree[i]
5023 +#define LOOP_WALK_END }
5025 +static void ip_tree_gc(unsigned long ul_set)
5027 + struct ip_set *set = (void *) ul_set;
5028 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5029 + struct ip_set_iptreeb *btree;
5030 + struct ip_set_iptreec *ctree;
5031 + struct ip_set_iptreed *dtree;
5032 + unsigned int a,b,c,d;
5033 + unsigned char i,j,k;
5036 + DP("gc: %s", set->name);
5037 + write_lock_bh(&set->lock);
5038 + LOOP_WALK_BEGIN(map, a, btree);
5039 + LOOP_WALK_BEGIN(btree, b, ctree);
5040 + LOOP_WALK_BEGIN(ctree, c, dtree);
5041 + for (d = 0; d < 256; d++) {
5042 + if (dtree->expires[d]) {
5043 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
5045 + dtree->expires[d], jiffies);
5047 + && time_before(dtree->expires[d], jiffies)) {
5048 + dtree->expires[d] = 0;
5055 + DP("gc: %s: leaf %u %u %u empty",
5056 + set->name, a, b, c);
5057 + kmem_cache_free(leaf_cachep, dtree);
5058 + ctree->tree[c] = NULL;
5060 + DP("gc: %s: leaf %u %u %u not empty",
5061 + set->name, a, b, c);
5067 + DP("gc: %s: branch %u %u empty",
5069 + kmem_cache_free(branch_cachep, ctree);
5070 + btree->tree[b] = NULL;
5072 + DP("gc: %s: branch %u %u not empty",
5079 + DP("gc: %s: branch %u empty",
5081 + kmem_cache_free(branch_cachep, btree);
5082 + map->tree[a] = NULL;
5084 + DP("gc: %s: branch %u not empty",
5089 + write_unlock_bh(&set->lock);
5091 + map->gc.expires = jiffies + map->gc_interval * HZ;
5092 + add_timer(&map->gc);
5095 +static inline void init_gc_timer(struct ip_set *set)
5097 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5099 + /* Even if there is no timeout for the entries,
5100 + * we still have to call gc because delete
5101 + * do not clean up empty branches */
5102 + map->gc_interval = IPTREE_GC_TIME;
5103 + init_timer(&map->gc);
5104 + map->gc.data = (unsigned long) set;
5105 + map->gc.function = ip_tree_gc;
5106 + map->gc.expires = jiffies + map->gc_interval * HZ;
5107 + add_timer(&map->gc);
5110 +static int create(struct ip_set *set, const void *data, size_t size)
5112 + struct ip_set_req_iptree_create *req =
5113 + (struct ip_set_req_iptree_create *) data;
5114 + struct ip_set_iptree *map;
5116 + if (size != sizeof(struct ip_set_req_iptree_create)) {
5117 + ip_set_printk("data length wrong (want %zu, have %zu)",
5118 + sizeof(struct ip_set_req_iptree_create),
5123 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
5125 + DP("out of memory for %d bytes",
5126 + sizeof(struct ip_set_iptree));
5129 + memset(map, 0, sizeof(*map));
5130 + map->timeout = req->timeout;
5131 + map->elements = 0;
5134 + init_gc_timer(set);
5139 +static void __flush(struct ip_set_iptree *map)
5141 + struct ip_set_iptreeb *btree;
5142 + struct ip_set_iptreec *ctree;
5143 + struct ip_set_iptreed *dtree;
5144 + unsigned int a,b,c;
5146 + LOOP_WALK_BEGIN(map, a, btree);
5147 + LOOP_WALK_BEGIN(btree, b, ctree);
5148 + LOOP_WALK_BEGIN(ctree, c, dtree);
5149 + kmem_cache_free(leaf_cachep, dtree);
5151 + kmem_cache_free(branch_cachep, ctree);
5153 + kmem_cache_free(branch_cachep, btree);
5155 + map->elements = 0;
5158 +static void destroy(struct ip_set *set)
5160 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5162 + /* gc might be running */
5163 + while (!del_timer(&map->gc))
5164 + msleep(IPTREE_DESTROY_SLEEP);
5170 +static void flush(struct ip_set *set)
5172 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5173 + unsigned int timeout = map->timeout;
5175 + /* gc might be running */
5176 + while (!del_timer(&map->gc))
5177 + msleep(IPTREE_DESTROY_SLEEP);
5179 + memset(map, 0, sizeof(*map));
5180 + map->timeout = timeout;
5182 + init_gc_timer(set);
5185 +static void list_header(const struct ip_set *set, void *data)
5187 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5188 + struct ip_set_req_iptree_create *header =
5189 + (struct ip_set_req_iptree_create *) data;
5191 + header->timeout = map->timeout;
5194 +static int list_members_size(const struct ip_set *set)
5196 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5197 + struct ip_set_iptreeb *btree;
5198 + struct ip_set_iptreec *ctree;
5199 + struct ip_set_iptreed *dtree;
5200 + unsigned int a,b,c,d;
5201 + unsigned int count = 0;
5203 + LOOP_WALK_BEGIN(map, a, btree);
5204 + LOOP_WALK_BEGIN(btree, b, ctree);
5205 + LOOP_WALK_BEGIN(ctree, c, dtree);
5206 + for (d = 0; d < 256; d++) {
5207 + if (dtree->expires[d]
5208 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
5215 + DP("members %u", count);
5216 + return (count * sizeof(struct ip_set_req_iptree));
5219 +static void list_members(const struct ip_set *set, void *data)
5221 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5222 + struct ip_set_iptreeb *btree;
5223 + struct ip_set_iptreec *ctree;
5224 + struct ip_set_iptreed *dtree;
5225 + unsigned int a,b,c,d;
5226 + size_t offset = 0;
5227 + struct ip_set_req_iptree *entry;
5229 + LOOP_WALK_BEGIN(map, a, btree);
5230 + LOOP_WALK_BEGIN(btree, b, ctree);
5231 + LOOP_WALK_BEGIN(ctree, c, dtree);
5232 + for (d = 0; d < 256; d++) {
5233 + if (dtree->expires[d]
5234 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
5235 + entry = (struct ip_set_req_iptree *)(data + offset);
5236 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
5237 + entry->timeout = !map->timeout ? 0
5238 + : (dtree->expires[d] - jiffies)/HZ;
5239 + offset += sizeof(struct ip_set_req_iptree);
5247 +static struct ip_set_type ip_set_iptree = {
5248 + .typename = SETTYPE_NAME,
5249 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
5250 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5251 + .create = &create,
5252 + .destroy = &destroy,
5254 + .reqsize = sizeof(struct ip_set_req_iptree),
5256 + .addip_kernel = &addip_kernel,
5258 + .delip_kernel = &delip_kernel,
5259 + .testip = &testip,
5260 + .testip_kernel = &testip_kernel,
5261 + .header_size = sizeof(struct ip_set_req_iptree_create),
5262 + .list_header = &list_header,
5263 + .list_members_size = &list_members_size,
5264 + .list_members = &list_members,
5265 + .me = THIS_MODULE,
5268 +MODULE_LICENSE("GPL");
5269 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5270 +MODULE_DESCRIPTION("iptree type of IP sets");
5271 +module_param(limit, int, 0600);
5272 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
5274 +static int __init ip_set_iptree_init(void)
5278 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5279 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
5280 + sizeof(struct ip_set_iptreeb),
5283 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
5284 + sizeof(struct ip_set_iptreeb),
5285 + 0, 0, NULL, NULL);
5287 + if (!branch_cachep) {
5288 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
5292 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5293 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
5294 + sizeof(struct ip_set_iptreed),
5297 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
5298 + sizeof(struct ip_set_iptreed),
5299 + 0, 0, NULL, NULL);
5301 + if (!leaf_cachep) {
5302 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
5306 + ret = ip_set_register_set_type(&ip_set_iptree);
5310 + kmem_cache_destroy(leaf_cachep);
5312 + kmem_cache_destroy(branch_cachep);
5317 +static void __exit ip_set_iptree_fini(void)
5319 + /* FIXME: possible race with ip_set_create() */
5320 + ip_set_unregister_set_type(&ip_set_iptree);
5321 + kmem_cache_destroy(leaf_cachep);
5322 + kmem_cache_destroy(branch_cachep);
5325 +module_init(ip_set_iptree_init);
5326 +module_exit(ip_set_iptree_fini);
5327 diff --git a/net/ipv4/netfilter/ip_set_iptreemap.c b/net/ipv4/netfilter/ip_set_iptreemap.c
5328 new file mode 100644
5329 index 0000000..3825055
5331 +++ b/net/ipv4/netfilter/ip_set_iptreemap.c
5333 +/* Copyright (C) 2007 Sven Wegener <sven.wegener@stealer.net>
5335 + * This program is free software; you can redistribute it and/or modify it
5336 + * under the terms of the GNU General Public License version 2 as published by
5337 + * the Free Software Foundation.
5340 +/* This modules implements the iptreemap ipset type. It uses bitmaps to
5341 + * represent every single IPv4 address as a single bit. The bitmaps are managed
5342 + * in a tree structure, where the first three octets of an addresses are used
5343 + * as an index to find the bitmap and the last octet is used as the bit number.
5346 +#include <linux/version.h>
5347 +#include <linux/module.h>
5348 +#include <linux/ip.h>
5349 +#include <linux/skbuff.h>
5350 +#include <linux/slab.h>
5351 +#include <linux/delay.h>
5352 +#include <linux/netfilter_ipv4/ip_tables.h>
5353 +#include <linux/netfilter_ipv4/ip_set.h>
5354 +#include <linux/errno.h>
5355 +#include <asm/uaccess.h>
5356 +#include <asm/bitops.h>
5357 +#include <linux/spinlock.h>
5359 +#include <linux/netfilter_ipv4/ip_set_iptreemap.h>
5361 +#define IPTREEMAP_DEFAULT_GC_TIME (5 * 60)
5362 +#define IPTREEMAP_DESTROY_SLEEP (100)
5364 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
5365 +static struct kmem_cache *cachep_b;
5366 +static struct kmem_cache *cachep_c;
5367 +static struct kmem_cache *cachep_d;
5369 +static kmem_cache_t *cachep_b;
5370 +static kmem_cache_t *cachep_c;
5371 +static kmem_cache_t *cachep_d;
5374 +static struct ip_set_iptreemap_d *fullbitmap_d;
5375 +static struct ip_set_iptreemap_c *fullbitmap_c;
5376 +static struct ip_set_iptreemap_b *fullbitmap_b;
5378 +#if defined(__LITTLE_ENDIAN)
5379 +#define ABCD(a, b, c, d, addr) \
5381 + a = ((unsigned char *)addr)[3]; \
5382 + b = ((unsigned char *)addr)[2]; \
5383 + c = ((unsigned char *)addr)[1]; \
5384 + d = ((unsigned char *)addr)[0]; \
5386 +#elif defined(__BIG_ENDIAN)
5387 +#define ABCD(a,b,c,d,addrp) do { \
5388 + a = ((unsigned char *)addrp)[0]; \
5389 + b = ((unsigned char *)addrp)[1]; \
5390 + c = ((unsigned char *)addrp)[2]; \
5391 + d = ((unsigned char *)addrp)[3]; \
5394 +#error "Please fix asm/byteorder.h"
5395 +#endif /* __LITTLE_ENDIAN */
5397 +#define TESTIP_WALK(map, elem, branch, full) \
5399 + branch = (map)->tree[elem]; \
5402 + else if (branch == full) \
5406 +#define ADDIP_WALK(map, elem, branch, type, cachep, full) \
5408 + branch = (map)->tree[elem]; \
5410 + branch = (type *) kmem_cache_alloc(cachep, GFP_ATOMIC); \
5413 + memset(branch, 0, sizeof(*branch)); \
5414 + (map)->tree[elem] = branch; \
5415 + } else if (branch == full) { \
5420 +#define ADDIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free) \
5421 + for (a = a1; a <= a2; a++) { \
5422 + branch = (map)->tree[a]; \
5423 + if (branch != full) { \
5424 + if ((a > a1 && a < a2) || (hint)) { \
5427 + (map)->tree[a] = full; \
5429 + } else if (!branch) { \
5430 + branch = kmem_cache_alloc(cachep, GFP_ATOMIC); \
5433 + memset(branch, 0, sizeof(*branch)); \
5434 + (map)->tree[a] = branch; \
5437 +#define ADDIP_RANGE_LOOP_END() \
5441 +#define DELIP_WALK(map, elem, branch, cachep, full, flags) \
5443 + branch = (map)->tree[elem]; \
5446 + } else if (branch == full) { \
5447 + branch = kmem_cache_alloc(cachep, flags); \
5450 + memcpy(branch, full, sizeof(*full)); \
5451 + (map)->tree[elem] = branch; \
5455 +#define DELIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free, flags) \
5456 + for (a = a1; a <= a2; a++) { \
5457 + branch = (map)->tree[a]; \
5459 + if ((a > a1 && a < a2) || (hint)) { \
5460 + if (branch != full) \
5462 + (map)->tree[a] = NULL; \
5464 + } else if (branch == full) { \
5465 + branch = kmem_cache_alloc(cachep, flags); \
5468 + memcpy(branch, full, sizeof(*branch)); \
5469 + (map)->tree[a] = branch; \
5472 +#define DELIP_RANGE_LOOP_END() \
5476 +#define LOOP_WALK_BEGIN(map, i, branch) \
5477 + for (i = 0; i < 256; i++) { \
5478 + branch = (map)->tree[i]; \
5479 + if (likely(!branch)) \
5482 +#define LOOP_WALK_END() \
5485 +#define LOOP_WALK_BEGIN_GC(map, i, branch, full, cachep, count) \
5487 + for (i = 0; i < 256; i++) { \
5488 + branch = (map)->tree[i]; \
5489 + if (likely(!branch)) \
5492 + if (branch == full) { \
5497 +#define LOOP_WALK_END_GC(map, i, branch, full, cachep, count) \
5498 + if (-256 == count) { \
5499 + kmem_cache_free(cachep, branch); \
5500 + (map)->tree[i] = NULL; \
5501 + } else if (256 == count) { \
5502 + kmem_cache_free(cachep, branch); \
5503 + (map)->tree[i] = full; \
5507 +#define LOOP_WALK_BEGIN_COUNT(map, i, branch, inrange, count) \
5508 + for (i = 0; i < 256; i++) { \
5509 + if (!(map)->tree[i]) { \
5516 + branch = (map)->tree[i];
5518 +#define LOOP_WALK_END_COUNT() \
5521 +#define MIN(a, b) (a < b ? a : b)
5522 +#define MAX(a, b) (a > b ? a : b)
5524 +#define GETVALUE1(a, a1, b1, r) \
5525 + (a == a1 ? b1 : r)
5527 +#define GETVALUE2(a, b, a1, b1, c1, r) \
5528 + (a == a1 && b == b1 ? c1 : r)
5530 +#define GETVALUE3(a, b, c, a1, b1, c1, d1, r) \
5531 + (a == a1 && b == b1 && c == c1 ? d1 : r)
5533 +#define CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2) \
5535 + GETVALUE1(a, a1, b1, 0) == 0 \
5536 + && GETVALUE1(a, a2, b2, 255) == 255 \
5543 +#define CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2) \
5545 + GETVALUE2(a, b, a1, b1, c1, 0) == 0 \
5546 + && GETVALUE2(a, b, a2, b2, c2, 255) == 255 \
5551 +#define CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2) \
5553 + GETVALUE3(a, b, c, a1, b1, c1, d1, 0) == 0 \
5554 + && GETVALUE3(a, b, c, a2, b2, c2, d2, 255) == 255 \
5559 +free_d(struct ip_set_iptreemap_d *map)
5561 + kmem_cache_free(cachep_d, map);
5565 +free_c(struct ip_set_iptreemap_c *map)
5567 + struct ip_set_iptreemap_d *dtree;
5570 + LOOP_WALK_BEGIN(map, i, dtree) {
5571 + if (dtree != fullbitmap_d)
5573 + } LOOP_WALK_END();
5575 + kmem_cache_free(cachep_c, map);
5579 +free_b(struct ip_set_iptreemap_b *map)
5581 + struct ip_set_iptreemap_c *ctree;
5584 + LOOP_WALK_BEGIN(map, i, ctree) {
5585 + if (ctree != fullbitmap_c)
5587 + } LOOP_WALK_END();
5589 + kmem_cache_free(cachep_b, map);
5593 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5595 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5596 + struct ip_set_iptreemap_b *btree;
5597 + struct ip_set_iptreemap_c *ctree;
5598 + struct ip_set_iptreemap_d *dtree;
5599 + unsigned char a, b, c, d;
5603 + ABCD(a, b, c, d, hash_ip);
5605 + TESTIP_WALK(map, a, btree, fullbitmap_b);
5606 + TESTIP_WALK(btree, b, ctree, fullbitmap_c);
5607 + TESTIP_WALK(ctree, c, dtree, fullbitmap_d);
5609 + return !!test_bit(d, (void *) dtree->bitmap);
5613 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5615 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5617 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5618 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5622 + return __testip(set, req->start, hash_ip);
5626 +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)
5630 + res = __testip(set,
5631 + ntohl(flags[index] & IPSET_SRC
5632 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5633 + ? ip_hdr(skb)->saddr
5634 + : ip_hdr(skb)->daddr),
5636 + ? skb->nh.iph->saddr
5637 + : skb->nh.iph->daddr),
5641 + return (res < 0 ? 0 : res);
5645 +__addip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5647 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5648 + struct ip_set_iptreemap_b *btree;
5649 + struct ip_set_iptreemap_c *ctree;
5650 + struct ip_set_iptreemap_d *dtree;
5651 + unsigned char a, b, c, d;
5655 + ABCD(a, b, c, d, hash_ip);
5657 + ADDIP_WALK(map, a, btree, struct ip_set_iptreemap_b, cachep_b, fullbitmap_b);
5658 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreemap_c, cachep_c, fullbitmap_c);
5659 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreemap_d, cachep_d, fullbitmap_d);
5661 + if (test_and_set_bit(d, (void *) dtree->bitmap))
5664 + set_bit(b, (void *) btree->dirty);
5670 +__addip_range(struct ip_set *set, ip_set_ip_t start, ip_set_ip_t end, ip_set_ip_t *hash_ip)
5672 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5673 + struct ip_set_iptreemap_b *btree;
5674 + struct ip_set_iptreemap_c *ctree;
5675 + struct ip_set_iptreemap_d *dtree;
5676 + unsigned int a, b, c, d;
5677 + unsigned char a1, b1, c1, d1;
5678 + unsigned char a2, b2, c2, d2;
5681 + return __addip_single(set, start, hash_ip);
5685 + ABCD(a1, b1, c1, d1, &start);
5686 + ABCD(a2, b2, c2, d2, &end);
5688 + /* This is sooo ugly... */
5689 + ADDIP_RANGE_LOOP(map, a, a1, a2, CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2), btree, fullbitmap_b, cachep_b, free_b) {
5690 + 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) {
5691 + 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) {
5692 + for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5693 + set_bit(d, (void *) dtree->bitmap);
5694 + set_bit(b, (void *) btree->dirty);
5695 + } ADDIP_RANGE_LOOP_END();
5696 + } ADDIP_RANGE_LOOP_END();
5697 + } ADDIP_RANGE_LOOP_END();
5703 +addip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5705 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5707 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5708 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5712 + return __addip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip);
5716 +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)
5719 + return __addip_single(set,
5720 + ntohl(flags[index] & IPSET_SRC
5721 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5722 + ? ip_hdr(skb)->saddr
5723 + : ip_hdr(skb)->daddr),
5725 + ? skb->nh.iph->saddr
5726 + : skb->nh.iph->daddr),
5732 +__delip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip, unsigned int __nocast flags)
5734 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5735 + struct ip_set_iptreemap_b *btree;
5736 + struct ip_set_iptreemap_c *ctree;
5737 + struct ip_set_iptreemap_d *dtree;
5738 + unsigned char a,b,c,d;
5742 + ABCD(a, b, c, d, hash_ip);
5744 + DELIP_WALK(map, a, btree, cachep_b, fullbitmap_b, flags);
5745 + DELIP_WALK(btree, b, ctree, cachep_c, fullbitmap_c, flags);
5746 + DELIP_WALK(ctree, c, dtree, cachep_d, fullbitmap_d, flags);
5748 + if (!test_and_clear_bit(d, (void *) dtree->bitmap))
5751 + set_bit(b, (void *) btree->dirty);
5757 +__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)
5759 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5760 + struct ip_set_iptreemap_b *btree;
5761 + struct ip_set_iptreemap_c *ctree;
5762 + struct ip_set_iptreemap_d *dtree;
5763 + unsigned int a, b, c, d;
5764 + unsigned char a1, b1, c1, d1;
5765 + unsigned char a2, b2, c2, d2;
5768 + return __delip_single(set, start, hash_ip, flags);
5772 + ABCD(a1, b1, c1, d1, &start);
5773 + ABCD(a2, b2, c2, d2, &end);
5775 + /* This is sooo ugly... */
5776 + 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) {
5777 + 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) {
5778 + 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) {
5779 + for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5780 + clear_bit(d, (void *) dtree->bitmap);
5781 + set_bit(b, (void *) btree->dirty);
5782 + } DELIP_RANGE_LOOP_END();
5783 + } DELIP_RANGE_LOOP_END();
5784 + } DELIP_RANGE_LOOP_END();
5790 +delip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5792 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5794 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5795 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5799 + return __delip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip, GFP_KERNEL);
5803 +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)
5805 + return __delip_single(set,
5806 + ntohl(flags[index] & IPSET_SRC
5807 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5808 + ? ip_hdr(skb)->saddr
5809 + : ip_hdr(skb)->daddr),
5811 + ? skb->nh.iph->saddr
5812 + : skb->nh.iph->daddr),
5818 +/* Check the status of the bitmap
5819 + * -1 == all bits cleared
5820 + * 1 == all bits set
5821 + * 0 == anything else
5824 +bitmap_status(struct ip_set_iptreemap_d *dtree)
5826 + unsigned char first = dtree->bitmap[0];
5829 + for (a = 1; a < 32; a++)
5830 + if (dtree->bitmap[a] != first)
5833 + return (first == 0 ? -1 : (first == 255 ? 1 : 0));
5837 +gc(unsigned long addr)
5839 + struct ip_set *set = (struct ip_set *) addr;
5840 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5841 + struct ip_set_iptreemap_b *btree;
5842 + struct ip_set_iptreemap_c *ctree;
5843 + struct ip_set_iptreemap_d *dtree;
5844 + unsigned int a, b, c;
5847 + write_lock_bh(&set->lock);
5849 + LOOP_WALK_BEGIN_GC(map, a, btree, fullbitmap_b, cachep_b, i) {
5850 + LOOP_WALK_BEGIN_GC(btree, b, ctree, fullbitmap_c, cachep_c, j) {
5851 + if (!test_and_clear_bit(b, (void *) btree->dirty))
5853 + LOOP_WALK_BEGIN_GC(ctree, c, dtree, fullbitmap_d, cachep_d, k) {
5854 + switch (bitmap_status(dtree)) {
5856 + kmem_cache_free(cachep_d, dtree);
5857 + ctree->tree[c] = NULL;
5861 + kmem_cache_free(cachep_d, dtree);
5862 + ctree->tree[c] = fullbitmap_d;
5866 + } LOOP_WALK_END();
5867 + } LOOP_WALK_END_GC(btree, b, ctree, fullbitmap_c, cachep_c, k);
5868 + } LOOP_WALK_END_GC(map, a, btree, fullbitmap_b, cachep_b, j);
5870 + write_unlock_bh(&set->lock);
5872 + map->gc.expires = jiffies + map->gc_interval * HZ;
5873 + add_timer(&map->gc);
5877 +init_gc_timer(struct ip_set *set)
5879 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5881 + init_timer(&map->gc);
5882 + map->gc.data = (unsigned long) set;
5883 + map->gc.function = gc;
5884 + map->gc.expires = jiffies + map->gc_interval * HZ;
5885 + add_timer(&map->gc);
5888 +static int create(struct ip_set *set, const void *data, size_t size)
5890 + struct ip_set_req_iptreemap_create *req = (struct ip_set_req_iptreemap_create *) data;
5891 + struct ip_set_iptreemap *map;
5893 + if (size != sizeof(struct ip_set_req_iptreemap_create)) {
5894 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap_create), size);
5898 + map = kzalloc(sizeof(*map), GFP_KERNEL);
5902 + map->gc_interval = req->gc_interval ? req->gc_interval : IPTREEMAP_DEFAULT_GC_TIME;
5905 + init_gc_timer(set);
5910 +static inline void __flush(struct ip_set_iptreemap *map)
5912 + struct ip_set_iptreemap_b *btree;
5915 + LOOP_WALK_BEGIN(map, a, btree);
5916 + if (btree != fullbitmap_b)
5921 +static void destroy(struct ip_set *set)
5923 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5925 + while (!del_timer(&map->gc))
5926 + msleep(IPTREEMAP_DESTROY_SLEEP);
5934 +static void flush(struct ip_set *set)
5936 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5938 + while (!del_timer(&map->gc))
5939 + msleep(IPTREEMAP_DESTROY_SLEEP);
5943 + memset(map, 0, sizeof(*map));
5945 + init_gc_timer(set);
5948 +static void list_header(const struct ip_set *set, void *data)
5950 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5951 + struct ip_set_req_iptreemap_create *header = (struct ip_set_req_iptreemap_create *) data;
5953 + header->gc_interval = map->gc_interval;
5956 +static int list_members_size(const struct ip_set *set)
5958 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5959 + struct ip_set_iptreemap_b *btree;
5960 + struct ip_set_iptreemap_c *ctree;
5961 + struct ip_set_iptreemap_d *dtree;
5962 + unsigned int a, b, c, d, inrange = 0, count = 0;
5964 + LOOP_WALK_BEGIN_COUNT(map, a, btree, inrange, count) {
5965 + LOOP_WALK_BEGIN_COUNT(btree, b, ctree, inrange, count) {
5966 + LOOP_WALK_BEGIN_COUNT(ctree, c, dtree, inrange, count) {
5967 + for (d = 0; d < 256; d++) {
5968 + if (test_bit(d, (void *) dtree->bitmap)) {
5970 + } else if (inrange) {
5975 + } LOOP_WALK_END_COUNT();
5976 + } LOOP_WALK_END_COUNT();
5977 + } LOOP_WALK_END_COUNT();
5982 + return (count * sizeof(struct ip_set_req_iptreemap));
5985 +static inline size_t add_member(void *data, size_t offset, ip_set_ip_t start, ip_set_ip_t end)
5987 + struct ip_set_req_iptreemap *entry = (struct ip_set_req_iptreemap *) (data + offset);
5989 + entry->start = start;
5992 + return sizeof(*entry);
5995 +static void list_members(const struct ip_set *set, void *data)
5997 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5998 + struct ip_set_iptreemap_b *btree;
5999 + struct ip_set_iptreemap_c *ctree;
6000 + struct ip_set_iptreemap_d *dtree;
6001 + unsigned int a, b, c, d, inrange = 0;
6002 + size_t offset = 0;
6003 + ip_set_ip_t start = 0, end = 0, ip;
6005 + LOOP_WALK_BEGIN(map, a, btree) {
6006 + LOOP_WALK_BEGIN(btree, b, ctree) {
6007 + LOOP_WALK_BEGIN(ctree, c, dtree) {
6008 + for (d = 0; d < 256; d++) {
6009 + if (test_bit(d, (void *) dtree->bitmap)) {
6010 + ip = ((a << 24) | (b << 16) | (c << 8) | d);
6014 + } else if (end < ip - 1) {
6015 + offset += add_member(data, offset, start, end);
6019 + } else if (inrange) {
6020 + offset += add_member(data, offset, start, end);
6024 + } LOOP_WALK_END();
6025 + } LOOP_WALK_END();
6026 + } LOOP_WALK_END();
6029 + add_member(data, offset, start, end);
6032 +static struct ip_set_type ip_set_iptreemap = {
6033 + .typename = SETTYPE_NAME,
6034 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6035 + .protocol_version = IP_SET_PROTOCOL_VERSION,
6037 + .destroy = destroy,
6039 + .reqsize = sizeof(struct ip_set_req_iptreemap),
6041 + .addip_kernel = addip_kernel,
6043 + .delip_kernel = delip_kernel,
6045 + .testip_kernel = testip_kernel,
6046 + .header_size = sizeof(struct ip_set_req_iptreemap_create),
6047 + .list_header = list_header,
6048 + .list_members_size = list_members_size,
6049 + .list_members = list_members,
6050 + .me = THIS_MODULE,
6053 +MODULE_LICENSE("GPL");
6054 +MODULE_AUTHOR("Sven Wegener <sven.wegener@stealer.net>");
6055 +MODULE_DESCRIPTION("iptreemap type of IP sets");
6057 +static int __init ip_set_iptreemap_init(void)
6059 + int ret = -ENOMEM;
6062 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
6063 + cachep_b = kmem_cache_create("ip_set_iptreemap_b",
6064 + sizeof(struct ip_set_iptreemap_b),
6067 + cachep_b = kmem_cache_create("ip_set_iptreemap_b",
6068 + sizeof(struct ip_set_iptreemap_b),
6069 + 0, 0, NULL, NULL);
6072 + ip_set_printk("Unable to create ip_set_iptreemap_b slab cache");
6076 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
6077 + cachep_c = kmem_cache_create("ip_set_iptreemap_c",
6078 + sizeof(struct ip_set_iptreemap_c),
6081 + cachep_c = kmem_cache_create("ip_set_iptreemap_c",
6082 + sizeof(struct ip_set_iptreemap_c),
6083 + 0, 0, NULL, NULL);
6086 + ip_set_printk("Unable to create ip_set_iptreemap_c slab cache");
6090 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
6091 + cachep_d = kmem_cache_create("ip_set_iptreemap_d",
6092 + sizeof(struct ip_set_iptreemap_d),
6095 + cachep_d = kmem_cache_create("ip_set_iptreemap_d",
6096 + sizeof(struct ip_set_iptreemap_d),
6097 + 0, 0, NULL, NULL);
6100 + ip_set_printk("Unable to create ip_set_iptreemap_d slab cache");
6104 + fullbitmap_d = kmem_cache_alloc(cachep_d, GFP_KERNEL);
6105 + if (!fullbitmap_d)
6108 + fullbitmap_c = kmem_cache_alloc(cachep_c, GFP_KERNEL);
6109 + if (!fullbitmap_c)
6112 + fullbitmap_b = kmem_cache_alloc(cachep_b, GFP_KERNEL);
6113 + if (!fullbitmap_b)
6116 + ret = ip_set_register_set_type(&ip_set_iptreemap);
6120 + /* Now init our global bitmaps */
6121 + memset(fullbitmap_d->bitmap, 0xff, sizeof(fullbitmap_d->bitmap));
6123 + for (a = 0; a < 256; a++)
6124 + fullbitmap_c->tree[a] = fullbitmap_d;
6126 + for (a = 0; a < 256; a++)
6127 + fullbitmap_b->tree[a] = fullbitmap_c;
6128 + memset(fullbitmap_b->dirty, 0, sizeof(fullbitmap_b->dirty));
6133 + kmem_cache_free(cachep_b, fullbitmap_b);
6135 + kmem_cache_free(cachep_c, fullbitmap_c);
6137 + kmem_cache_free(cachep_d, fullbitmap_d);
6139 + kmem_cache_destroy(cachep_d);
6141 + kmem_cache_destroy(cachep_c);
6143 + kmem_cache_destroy(cachep_b);
6149 +static void __exit ip_set_iptreemap_fini(void)
6151 + ip_set_unregister_set_type(&ip_set_iptreemap);
6152 + kmem_cache_free(cachep_d, fullbitmap_d);
6153 + kmem_cache_free(cachep_c, fullbitmap_c);
6154 + kmem_cache_free(cachep_b, fullbitmap_b);
6155 + kmem_cache_destroy(cachep_d);
6156 + kmem_cache_destroy(cachep_c);
6157 + kmem_cache_destroy(cachep_b);
6160 +module_init(ip_set_iptreemap_init);
6161 +module_exit(ip_set_iptreemap_fini);
6162 diff --git a/net/ipv4/netfilter/ip_set_macipmap.c b/net/ipv4/netfilter/ip_set_macipmap.c
6163 new file mode 100644
6164 index 0000000..8ca2159
6166 +++ b/net/ipv4/netfilter/ip_set_macipmap.c
6168 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
6169 + * Patrick Schaaf <bof@bof.de>
6170 + * Martin Josefsson <gandalf@wlug.westbo.se>
6171 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6173 + * This program is free software; you can redistribute it and/or modify
6174 + * it under the terms of the GNU General Public License version 2 as
6175 + * published by the Free Software Foundation.
6178 +/* Kernel module implementing an IP set type: the macipmap type */
6180 +#include <linux/module.h>
6181 +#include <linux/ip.h>
6182 +#include <linux/skbuff.h>
6183 +#include <linux/version.h>
6184 +#include <linux/netfilter_ipv4/ip_tables.h>
6185 +#include <linux/netfilter_ipv4/ip_set.h>
6186 +#include <linux/errno.h>
6187 +#include <asm/uaccess.h>
6188 +#include <asm/bitops.h>
6189 +#include <linux/spinlock.h>
6190 +#include <linux/if_ether.h>
6191 +#include <linux/vmalloc.h>
6193 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
6194 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
6197 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
6199 + struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
6200 + struct ip_set_macip *table = (struct ip_set_macip *) map->members;
6201 + struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
6203 + if (size != sizeof(struct ip_set_req_macipmap)) {
6204 + ip_set_printk("data length wrong (want %zu, have %zu)",
6205 + sizeof(struct ip_set_req_macipmap),
6210 + if (req->ip < map->first_ip || req->ip > map->last_ip)
6213 + *hash_ip = req->ip;
6214 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
6215 + set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
6216 + if (test_bit(IPSET_MACIP_ISSET,
6217 + (void *) &table[req->ip - map->first_ip].flags)) {
6218 + return (memcmp(req->ethernet,
6219 + &table[req->ip - map->first_ip].ethernet,
6222 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6227 +testip_kernel(struct ip_set *set,
6228 + const struct sk_buff *skb,
6229 + ip_set_ip_t *hash_ip,
6230 + const u_int32_t *flags,
6231 + unsigned char index)
6233 + struct ip_set_macipmap *map =
6234 + (struct ip_set_macipmap *) set->data;
6235 + struct ip_set_macip *table =
6236 + (struct ip_set_macip *) map->members;
6239 + ip = ntohl(flags[index] & IPSET_SRC
6240 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6241 + ? ip_hdr(skb)->saddr
6242 + : ip_hdr(skb)->daddr);
6244 + ? skb->nh.iph->saddr
6245 + : skb->nh.iph->daddr);
6248 + if (ip < map->first_ip || ip > map->last_ip)
6252 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
6253 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
6254 + if (test_bit(IPSET_MACIP_ISSET,
6255 + (void *) &table[ip - map->first_ip].flags)) {
6256 + /* Is mac pointer valid?
6257 + * If so, compare... */
6258 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6259 + return (skb_mac_header(skb) >= skb->head
6260 + && (skb_mac_header(skb) + ETH_HLEN) <= skb->data
6262 + return (skb->mac.raw >= skb->head
6263 + && (skb->mac.raw + ETH_HLEN) <= skb->data
6265 + && (memcmp(eth_hdr(skb)->h_source,
6266 + &table[ip - map->first_ip].ethernet,
6269 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6273 +/* returns 0 on success */
6275 +__addip(struct ip_set *set,
6276 + ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
6278 + struct ip_set_macipmap *map =
6279 + (struct ip_set_macipmap *) set->data;
6280 + struct ip_set_macip *table =
6281 + (struct ip_set_macip *) map->members;
6283 + if (ip < map->first_ip || ip > map->last_ip)
6285 + if (test_and_set_bit(IPSET_MACIP_ISSET,
6286 + (void *) &table[ip - map->first_ip].flags))
6290 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6291 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
6296 +addip(struct ip_set *set, const void *data, size_t size,
6297 + ip_set_ip_t *hash_ip)
6299 + struct ip_set_req_macipmap *req =
6300 + (struct ip_set_req_macipmap *) data;
6302 + if (size != sizeof(struct ip_set_req_macipmap)) {
6303 + ip_set_printk("data length wrong (want %zu, have %zu)",
6304 + sizeof(struct ip_set_req_macipmap),
6308 + return __addip(set, req->ip, req->ethernet, hash_ip);
6312 +addip_kernel(struct ip_set *set,
6313 + const struct sk_buff *skb,
6314 + ip_set_ip_t *hash_ip,
6315 + const u_int32_t *flags,
6316 + unsigned char index)
6320 + ip = ntohl(flags[index] & IPSET_SRC
6321 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6322 + ? ip_hdr(skb)->saddr
6323 + : ip_hdr(skb)->daddr);
6325 + ? skb->nh.iph->saddr
6326 + : skb->nh.iph->daddr);
6329 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6330 + if (!(skb_mac_header(skb) >= skb->head
6331 + && (skb_mac_header(skb) + ETH_HLEN) <= skb->data))
6333 + if (!(skb->mac.raw >= skb->head
6334 + && (skb->mac.raw + ETH_HLEN) <= skb->data))
6338 + return __addip(set, ip, eth_hdr(skb)->h_source, hash_ip);
6342 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6344 + struct ip_set_macipmap *map =
6345 + (struct ip_set_macipmap *) set->data;
6346 + struct ip_set_macip *table =
6347 + (struct ip_set_macip *) map->members;
6349 + if (ip < map->first_ip || ip > map->last_ip)
6351 + if (!test_and_clear_bit(IPSET_MACIP_ISSET,
6352 + (void *)&table[ip - map->first_ip].flags))
6356 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6361 +delip(struct ip_set *set, const void *data, size_t size,
6362 + ip_set_ip_t *hash_ip)
6364 + struct ip_set_req_macipmap *req =
6365 + (struct ip_set_req_macipmap *) data;
6367 + if (size != sizeof(struct ip_set_req_macipmap)) {
6368 + ip_set_printk("data length wrong (want %zu, have %zu)",
6369 + sizeof(struct ip_set_req_macipmap),
6373 + return __delip(set, req->ip, hash_ip);
6377 +delip_kernel(struct ip_set *set,
6378 + const struct sk_buff *skb,
6379 + ip_set_ip_t *hash_ip,
6380 + const u_int32_t *flags,
6381 + unsigned char index)
6383 + return __delip(set,
6384 + ntohl(flags[index] & IPSET_SRC
6385 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6386 + ? ip_hdr(skb)->saddr
6387 + : ip_hdr(skb)->daddr),
6389 + ? skb->nh.iph->saddr
6390 + : skb->nh.iph->daddr),
6395 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
6397 + return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
6400 +static int create(struct ip_set *set, const void *data, size_t size)
6403 + struct ip_set_req_macipmap_create *req =
6404 + (struct ip_set_req_macipmap_create *) data;
6405 + struct ip_set_macipmap *map;
6407 + if (size != sizeof(struct ip_set_req_macipmap_create)) {
6408 + ip_set_printk("data length wrong (want %zu, have %zu)",
6409 + sizeof(struct ip_set_req_macipmap_create),
6414 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
6415 + HIPQUAD(req->from), HIPQUAD(req->to));
6417 + if (req->from > req->to) {
6418 + DP("bad ip range");
6422 + if (req->to - req->from > MAX_RANGE) {
6423 + ip_set_printk("range too big (max %d addresses)",
6428 + map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
6430 + DP("out of memory for %d bytes",
6431 + sizeof(struct ip_set_macipmap));
6434 + map->flags = req->flags;
6435 + map->first_ip = req->from;
6436 + map->last_ip = req->to;
6437 + newbytes = members_size(map->first_ip, map->last_ip);
6438 + map->members = ip_set_malloc(newbytes);
6439 + DP("members: %u %p", newbytes, map->members);
6440 + if (!map->members) {
6441 + DP("out of memory for %d bytes", newbytes);
6445 + memset(map->members, 0, newbytes);
6451 +static void destroy(struct ip_set *set)
6453 + struct ip_set_macipmap *map =
6454 + (struct ip_set_macipmap *) set->data;
6456 + ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
6462 +static void flush(struct ip_set *set)
6464 + struct ip_set_macipmap *map =
6465 + (struct ip_set_macipmap *) set->data;
6466 + memset(map->members, 0, members_size(map->first_ip, map->last_ip));
6469 +static void list_header(const struct ip_set *set, void *data)
6471 + struct ip_set_macipmap *map =
6472 + (struct ip_set_macipmap *) set->data;
6473 + struct ip_set_req_macipmap_create *header =
6474 + (struct ip_set_req_macipmap_create *) data;
6476 + DP("list_header %x %x %u", map->first_ip, map->last_ip,
6479 + header->from = map->first_ip;
6480 + header->to = map->last_ip;
6481 + header->flags = map->flags;
6484 +static int list_members_size(const struct ip_set *set)
6486 + struct ip_set_macipmap *map =
6487 + (struct ip_set_macipmap *) set->data;
6489 + DP("%u", members_size(map->first_ip, map->last_ip));
6490 + return members_size(map->first_ip, map->last_ip);
6493 +static void list_members(const struct ip_set *set, void *data)
6495 + struct ip_set_macipmap *map =
6496 + (struct ip_set_macipmap *) set->data;
6498 + int bytes = members_size(map->first_ip, map->last_ip);
6500 + DP("members: %u %p", bytes, map->members);
6501 + memcpy(data, map->members, bytes);
6504 +static struct ip_set_type ip_set_macipmap = {
6505 + .typename = SETTYPE_NAME,
6506 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6507 + .protocol_version = IP_SET_PROTOCOL_VERSION,
6508 + .create = &create,
6509 + .destroy = &destroy,
6511 + .reqsize = sizeof(struct ip_set_req_macipmap),
6513 + .addip_kernel = &addip_kernel,
6515 + .delip_kernel = &delip_kernel,
6516 + .testip = &testip,
6517 + .testip_kernel = &testip_kernel,
6518 + .header_size = sizeof(struct ip_set_req_macipmap_create),
6519 + .list_header = &list_header,
6520 + .list_members_size = &list_members_size,
6521 + .list_members = &list_members,
6522 + .me = THIS_MODULE,
6525 +MODULE_LICENSE("GPL");
6526 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6527 +MODULE_DESCRIPTION("macipmap type of IP sets");
6529 +static int __init ip_set_macipmap_init(void)
6531 + init_max_malloc_size();
6532 + return ip_set_register_set_type(&ip_set_macipmap);
6535 +static void __exit ip_set_macipmap_fini(void)
6537 + /* FIXME: possible race with ip_set_create() */
6538 + ip_set_unregister_set_type(&ip_set_macipmap);
6541 +module_init(ip_set_macipmap_init);
6542 +module_exit(ip_set_macipmap_fini);
6543 diff --git a/net/ipv4/netfilter/ip_set_nethash.c b/net/ipv4/netfilter/ip_set_nethash.c
6544 new file mode 100644
6545 index 0000000..d94cc32
6547 +++ b/net/ipv4/netfilter/ip_set_nethash.c
6549 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6551 + * This program is free software; you can redistribute it and/or modify
6552 + * it under the terms of the GNU General Public License version 2 as
6553 + * published by the Free Software Foundation.
6556 +/* Kernel module implementing a cidr nethash set */
6558 +#include <linux/module.h>
6559 +#include <linux/ip.h>
6560 +#include <linux/skbuff.h>
6561 +#include <linux/version.h>
6562 +#include <linux/jhash.h>
6563 +#include <linux/netfilter_ipv4/ip_tables.h>
6564 +#include <linux/netfilter_ipv4/ip_set.h>
6565 +#include <linux/errno.h>
6566 +#include <asm/uaccess.h>
6567 +#include <asm/bitops.h>
6568 +#include <linux/spinlock.h>
6569 +#include <linux/vmalloc.h>
6570 +#include <linux/random.h>
6572 +#include <net/ip.h>
6574 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
6575 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
6577 +static int limit = MAX_RANGE;
6579 +static inline __u32
6580 +jhash_ip(const struct ip_set_nethash *map, uint16_t i, ip_set_ip_t ip)
6582 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
6585 +static inline __u32
6586 +hash_id_cidr(struct ip_set_nethash *map,
6588 + unsigned char cidr,
6589 + ip_set_ip_t *hash_ip)
6593 + ip_set_ip_t *elem;
6595 + *hash_ip = pack(ip, cidr);
6597 + for (i = 0; i < map->probes; i++) {
6598 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
6599 + DP("hash key: %u", id);
6600 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6601 + if (*elem == *hash_ip)
6607 +static inline __u32
6608 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6610 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6611 + __u32 id = UINT_MAX;
6614 + for (i = 0; i < 30 && map->cidr[i]; i++) {
6615 + id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
6616 + if (id != UINT_MAX)
6623 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
6624 + ip_set_ip_t *hash_ip)
6626 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6628 + return (ip && hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
6632 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6634 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
6638 +testip(struct ip_set *set, const void *data, size_t size,
6639 + ip_set_ip_t *hash_ip)
6641 + struct ip_set_req_nethash *req =
6642 + (struct ip_set_req_nethash *) data;
6644 + if (size != sizeof(struct ip_set_req_nethash)) {
6645 + ip_set_printk("data length wrong (want %zu, have %zu)",
6646 + sizeof(struct ip_set_req_nethash),
6650 + return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
6651 + : __testip_cidr(set, req->ip, req->cidr, hash_ip));
6655 +testip_kernel(struct ip_set *set,
6656 + const struct sk_buff *skb,
6657 + ip_set_ip_t *hash_ip,
6658 + const u_int32_t *flags,
6659 + unsigned char index)
6661 + return __testip(set,
6662 + ntohl(flags[index] & IPSET_SRC
6663 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6664 + ? ip_hdr(skb)->saddr
6665 + : ip_hdr(skb)->daddr),
6667 + ? skb->nh.iph->saddr
6668 + : skb->nh.iph->daddr),
6674 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
6678 + ip_set_ip_t *elem;
6680 + for (i = 0; i < map->probes; i++) {
6681 + probe = jhash_ip(map, i, ip) % map->hashsize;
6682 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
6691 + /* Trigger rehashing */
6696 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6697 + ip_set_ip_t *hash_ip)
6699 + if (!ip || map->elements >= limit)
6702 + *hash_ip = pack(ip, cidr);
6703 + DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
6705 + return __addip_base(map, *hash_ip);
6709 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
6711 + unsigned char next;
6714 + for (i = 0; i < 30 && map->cidr[i]; i++) {
6715 + if (map->cidr[i] == cidr) {
6717 + } else if (map->cidr[i] < cidr) {
6718 + next = map->cidr[i];
6719 + map->cidr[i] = cidr;
6724 + map->cidr[i] = cidr;
6728 +addip(struct ip_set *set, const void *data, size_t size,
6729 + ip_set_ip_t *hash_ip)
6731 + struct ip_set_req_nethash *req =
6732 + (struct ip_set_req_nethash *) data;
6735 + if (size != sizeof(struct ip_set_req_nethash)) {
6736 + ip_set_printk("data length wrong (want %zu, have %zu)",
6737 + sizeof(struct ip_set_req_nethash),
6741 + ret = __addip((struct ip_set_nethash *) set->data,
6742 + req->ip, req->cidr, hash_ip);
6745 + update_cidr_sizes((struct ip_set_nethash *) set->data,
6752 +addip_kernel(struct ip_set *set,
6753 + const struct sk_buff *skb,
6754 + ip_set_ip_t *hash_ip,
6755 + const u_int32_t *flags,
6756 + unsigned char index)
6758 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6759 + int ret = -ERANGE;
6760 + ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC
6761 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6762 + ? ip_hdr(skb)->saddr
6763 + : ip_hdr(skb)->daddr);
6765 + ? skb->nh.iph->saddr
6766 + : skb->nh.iph->daddr);
6770 + ret = __addip(map, ip, map->cidr[0], hash_ip);
6775 +static int retry(struct ip_set *set)
6777 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6778 + ip_set_ip_t *elem;
6780 + u_int32_t i, hashsize = map->hashsize;
6782 + struct ip_set_nethash *tmp;
6784 + if (map->resize == 0)
6790 + /* Calculate new parameters */
6791 + hashsize += (hashsize * map->resize)/100;
6792 + if (hashsize == map->hashsize)
6795 + ip_set_printk("rehashing of set %s triggered: "
6796 + "hashsize grows from %u to %u",
6797 + set->name, map->hashsize, hashsize);
6799 + tmp = kmalloc(sizeof(struct ip_set_nethash)
6800 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
6802 + DP("out of memory for %d bytes",
6803 + sizeof(struct ip_set_nethash)
6804 + + map->probes * sizeof(uint32_t));
6807 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
6808 + if (!tmp->members) {
6809 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
6813 + tmp->hashsize = hashsize;
6814 + tmp->elements = 0;
6815 + tmp->probes = map->probes;
6816 + tmp->resize = map->resize;
6817 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
6818 + memcpy(tmp->cidr, map->cidr, 30 * sizeof(unsigned char));
6820 + write_lock_bh(&set->lock);
6821 + map = (struct ip_set_nethash *) set->data; /* Play safe */
6822 + for (i = 0; i < map->hashsize && res == 0; i++) {
6823 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
6825 + res = __addip_base(tmp, *elem);
6828 + /* Failure, try again */
6829 + write_unlock_bh(&set->lock);
6830 + harray_free(tmp->members);
6835 + /* Success at resizing! */
6836 + members = map->members;
6838 + map->hashsize = tmp->hashsize;
6839 + map->members = tmp->members;
6840 + write_unlock_bh(&set->lock);
6842 + harray_free(members);
6849 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6850 + ip_set_ip_t *hash_ip)
6852 + ip_set_ip_t id, *elem;
6857 + id = hash_id_cidr(map, ip, cidr, hash_ip);
6858 + if (id == UINT_MAX)
6861 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6868 +delip(struct ip_set *set, const void *data, size_t size,
6869 + ip_set_ip_t *hash_ip)
6871 + struct ip_set_req_nethash *req =
6872 + (struct ip_set_req_nethash *) data;
6874 + if (size != sizeof(struct ip_set_req_nethash)) {
6875 + ip_set_printk("data length wrong (want %zu, have %zu)",
6876 + sizeof(struct ip_set_req_nethash),
6880 + /* TODO: no garbage collection in map->cidr */
6881 + return __delip((struct ip_set_nethash *) set->data,
6882 + req->ip, req->cidr, hash_ip);
6886 +delip_kernel(struct ip_set *set,
6887 + const struct sk_buff *skb,
6888 + ip_set_ip_t *hash_ip,
6889 + const u_int32_t *flags,
6890 + unsigned char index)
6892 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6893 + int ret = -ERANGE;
6894 + ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC
6895 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6896 + ? ip_hdr(skb)->saddr
6897 + : ip_hdr(skb)->daddr);
6899 + ? skb->nh.iph->saddr
6900 + : skb->nh.iph->daddr);
6904 + ret = __delip(map, ip, map->cidr[0], hash_ip);
6909 +static int create(struct ip_set *set, const void *data, size_t size)
6911 + struct ip_set_req_nethash_create *req =
6912 + (struct ip_set_req_nethash_create *) data;
6913 + struct ip_set_nethash *map;
6916 + if (size != sizeof(struct ip_set_req_nethash_create)) {
6917 + ip_set_printk("data length wrong (want %zu, have %zu)",
6918 + sizeof(struct ip_set_req_nethash_create),
6923 + if (req->hashsize < 1) {
6924 + ip_set_printk("hashsize too small");
6927 + if (req->probes < 1) {
6928 + ip_set_printk("probes too small");
6932 + map = kmalloc(sizeof(struct ip_set_nethash)
6933 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
6935 + DP("out of memory for %d bytes",
6936 + sizeof(struct ip_set_nethash)
6937 + + req->probes * sizeof(uint32_t));
6940 + for (i = 0; i < req->probes; i++)
6941 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
6942 + map->elements = 0;
6943 + map->hashsize = req->hashsize;
6944 + map->probes = req->probes;
6945 + map->resize = req->resize;
6946 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
6947 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
6948 + if (!map->members) {
6949 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
6958 +static void destroy(struct ip_set *set)
6960 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6962 + harray_free(map->members);
6968 +static void flush(struct ip_set *set)
6970 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6971 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
6972 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
6973 + map->elements = 0;
6976 +static void list_header(const struct ip_set *set, void *data)
6978 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6979 + struct ip_set_req_nethash_create *header =
6980 + (struct ip_set_req_nethash_create *) data;
6982 + header->hashsize = map->hashsize;
6983 + header->probes = map->probes;
6984 + header->resize = map->resize;
6987 +static int list_members_size(const struct ip_set *set)
6989 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6991 + return (map->hashsize * sizeof(ip_set_ip_t));
6994 +static void list_members(const struct ip_set *set, void *data)
6996 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6997 + ip_set_ip_t i, *elem;
6999 + for (i = 0; i < map->hashsize; i++) {
7000 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
7001 + ((ip_set_ip_t *)data)[i] = *elem;
7005 +static struct ip_set_type ip_set_nethash = {
7006 + .typename = SETTYPE_NAME,
7007 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
7008 + .protocol_version = IP_SET_PROTOCOL_VERSION,
7009 + .create = &create,
7010 + .destroy = &destroy,
7012 + .reqsize = sizeof(struct ip_set_req_nethash),
7014 + .addip_kernel = &addip_kernel,
7017 + .delip_kernel = &delip_kernel,
7018 + .testip = &testip,
7019 + .testip_kernel = &testip_kernel,
7020 + .header_size = sizeof(struct ip_set_req_nethash_create),
7021 + .list_header = &list_header,
7022 + .list_members_size = &list_members_size,
7023 + .list_members = &list_members,
7024 + .me = THIS_MODULE,
7027 +MODULE_LICENSE("GPL");
7028 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7029 +MODULE_DESCRIPTION("nethash type of IP sets");
7030 +module_param(limit, int, 0600);
7031 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
7033 +static int __init ip_set_nethash_init(void)
7035 + return ip_set_register_set_type(&ip_set_nethash);
7038 +static void __exit ip_set_nethash_fini(void)
7040 + /* FIXME: possible race with ip_set_create() */
7041 + ip_set_unregister_set_type(&ip_set_nethash);
7044 +module_init(ip_set_nethash_init);
7045 +module_exit(ip_set_nethash_fini);
7046 diff --git a/net/ipv4/netfilter/ip_set_portmap.c b/net/ipv4/netfilter/ip_set_portmap.c
7047 new file mode 100644
7048 index 0000000..9dba9cf
7050 +++ b/net/ipv4/netfilter/ip_set_portmap.c
7052 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7054 + * This program is free software; you can redistribute it and/or modify
7055 + * it under the terms of the GNU General Public License version 2 as
7056 + * published by the Free Software Foundation.
7059 +/* Kernel module implementing a port set type as a bitmap */
7061 +#include <linux/module.h>
7062 +#include <linux/ip.h>
7063 +#include <linux/tcp.h>
7064 +#include <linux/udp.h>
7065 +#include <linux/skbuff.h>
7066 +#include <linux/version.h>
7067 +#include <linux/netfilter_ipv4/ip_tables.h>
7068 +#include <linux/netfilter_ipv4/ip_set.h>
7069 +#include <linux/errno.h>
7070 +#include <asm/uaccess.h>
7071 +#include <asm/bitops.h>
7072 +#include <linux/spinlock.h>
7074 +#include <net/ip.h>
7076 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
7078 +/* We must handle non-linear skbs */
7079 +static inline ip_set_ip_t
7080 +get_port(const struct sk_buff *skb, u_int32_t flags)
7082 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
7083 + struct iphdr *iph = ip_hdr(skb);
7085 + struct iphdr *iph = skb->nh.iph;
7087 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
7088 + switch (iph->protocol) {
7089 + case IPPROTO_TCP: {
7090 + struct tcphdr tcph;
7092 + /* See comments at tcp_match in ip_tables.c */
7094 + return INVALID_PORT;
7096 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
7097 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
7099 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
7101 + /* No choice either */
7102 + return INVALID_PORT;
7104 + return ntohs(flags & IPSET_SRC ?
7105 + tcph.source : tcph.dest);
7107 + case IPPROTO_UDP: {
7108 + struct udphdr udph;
7111 + return INVALID_PORT;
7113 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
7114 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
7116 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
7118 + /* No choice either */
7119 + return INVALID_PORT;
7121 + return ntohs(flags & IPSET_SRC ?
7122 + udph.source : udph.dest);
7125 + return INVALID_PORT;
7130 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
7132 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7134 + if (port < map->first_port || port > map->last_port)
7137 + *hash_port = port;
7138 + DP("set: %s, port:%u, %u", set->name, port, *hash_port);
7139 + return !!test_bit(port - map->first_port, map->members);
7143 +testport(struct ip_set *set, const void *data, size_t size,
7144 + ip_set_ip_t *hash_port)
7146 + struct ip_set_req_portmap *req =
7147 + (struct ip_set_req_portmap *) data;
7149 + if (size != sizeof(struct ip_set_req_portmap)) {
7150 + ip_set_printk("data length wrong (want %zu, have %zu)",
7151 + sizeof(struct ip_set_req_portmap),
7155 + return __testport(set, req->port, hash_port);
7159 +testport_kernel(struct ip_set *set,
7160 + const struct sk_buff *skb,
7161 + ip_set_ip_t *hash_port,
7162 + const u_int32_t *flags,
7163 + unsigned char index)
7166 + ip_set_ip_t port = get_port(skb, flags[index]);
7168 + DP("flag %s port %u", flags[index] & IPSET_SRC ? "SRC" : "DST", port);
7169 + if (port == INVALID_PORT)
7172 + res = __testport(set, port, hash_port);
7174 + return (res < 0 ? 0 : res);
7178 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
7180 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7182 + if (port < map->first_port || port > map->last_port)
7184 + if (test_and_set_bit(port - map->first_port, map->members))
7187 + *hash_port = port;
7188 + DP("port %u", port);
7193 +addport(struct ip_set *set, const void *data, size_t size,
7194 + ip_set_ip_t *hash_port)
7196 + struct ip_set_req_portmap *req =
7197 + (struct ip_set_req_portmap *) data;
7199 + if (size != sizeof(struct ip_set_req_portmap)) {
7200 + ip_set_printk("data length wrong (want %zu, have %zu)",
7201 + sizeof(struct ip_set_req_portmap),
7205 + return __addport(set, req->port, hash_port);
7209 +addport_kernel(struct ip_set *set,
7210 + const struct sk_buff *skb,
7211 + ip_set_ip_t *hash_port,
7212 + const u_int32_t *flags,
7213 + unsigned char index)
7215 + ip_set_ip_t port = get_port(skb, flags[index]);
7217 + if (port == INVALID_PORT)
7220 + return __addport(set, port, hash_port);
7224 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
7226 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7228 + if (port < map->first_port || port > map->last_port)
7230 + if (!test_and_clear_bit(port - map->first_port, map->members))
7233 + *hash_port = port;
7234 + DP("port %u", port);
7239 +delport(struct ip_set *set, const void *data, size_t size,
7240 + ip_set_ip_t *hash_port)
7242 + struct ip_set_req_portmap *req =
7243 + (struct ip_set_req_portmap *) data;
7245 + if (size != sizeof(struct ip_set_req_portmap)) {
7246 + ip_set_printk("data length wrong (want %zu, have %zu)",
7247 + sizeof(struct ip_set_req_portmap),
7251 + return __delport(set, req->port, hash_port);
7255 +delport_kernel(struct ip_set *set,
7256 + const struct sk_buff *skb,
7257 + ip_set_ip_t *hash_port,
7258 + const u_int32_t *flags,
7259 + unsigned char index)
7261 + ip_set_ip_t port = get_port(skb, flags[index]);
7263 + if (port == INVALID_PORT)
7266 + return __delport(set, port, hash_port);
7269 +static int create(struct ip_set *set, const void *data, size_t size)
7272 + struct ip_set_req_portmap_create *req =
7273 + (struct ip_set_req_portmap_create *) data;
7274 + struct ip_set_portmap *map;
7276 + if (size != sizeof(struct ip_set_req_portmap_create)) {
7277 + ip_set_printk("data length wrong (want %zu, have %zu)",
7278 + sizeof(struct ip_set_req_portmap_create),
7283 + DP("from %u to %u", req->from, req->to);
7285 + if (req->from > req->to) {
7286 + DP("bad port range");
7290 + if (req->to - req->from > MAX_RANGE) {
7291 + ip_set_printk("range too big (max %d ports)",
7296 + map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
7298 + DP("out of memory for %d bytes",
7299 + sizeof(struct ip_set_portmap));
7302 + map->first_port = req->from;
7303 + map->last_port = req->to;
7304 + newbytes = bitmap_bytes(req->from, req->to);
7305 + map->members = kmalloc(newbytes, GFP_KERNEL);
7306 + if (!map->members) {
7307 + DP("out of memory for %d bytes", newbytes);
7311 + memset(map->members, 0, newbytes);
7317 +static void destroy(struct ip_set *set)
7319 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7321 + kfree(map->members);
7327 +static void flush(struct ip_set *set)
7329 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7330 + memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
7333 +static void list_header(const struct ip_set *set, void *data)
7335 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7336 + struct ip_set_req_portmap_create *header =
7337 + (struct ip_set_req_portmap_create *) data;
7339 + DP("list_header %u %u", map->first_port, map->last_port);
7341 + header->from = map->first_port;
7342 + header->to = map->last_port;
7345 +static int list_members_size(const struct ip_set *set)
7347 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7349 + return bitmap_bytes(map->first_port, map->last_port);
7352 +static void list_members(const struct ip_set *set, void *data)
7354 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7355 + int bytes = bitmap_bytes(map->first_port, map->last_port);
7357 + memcpy(data, map->members, bytes);
7360 +static struct ip_set_type ip_set_portmap = {
7361 + .typename = SETTYPE_NAME,
7362 + .features = IPSET_TYPE_PORT | IPSET_DATA_SINGLE,
7363 + .protocol_version = IP_SET_PROTOCOL_VERSION,
7364 + .create = &create,
7365 + .destroy = &destroy,
7367 + .reqsize = sizeof(struct ip_set_req_portmap),
7368 + .addip = &addport,
7369 + .addip_kernel = &addport_kernel,
7370 + .delip = &delport,
7371 + .delip_kernel = &delport_kernel,
7372 + .testip = &testport,
7373 + .testip_kernel = &testport_kernel,
7374 + .header_size = sizeof(struct ip_set_req_portmap_create),
7375 + .list_header = &list_header,
7376 + .list_members_size = &list_members_size,
7377 + .list_members = &list_members,
7378 + .me = THIS_MODULE,
7381 +MODULE_LICENSE("GPL");
7382 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7383 +MODULE_DESCRIPTION("portmap type of IP sets");
7385 +static int __init ip_set_portmap_init(void)
7387 + return ip_set_register_set_type(&ip_set_portmap);
7390 +static void __exit ip_set_portmap_fini(void)
7392 + /* FIXME: possible race with ip_set_create() */
7393 + ip_set_unregister_set_type(&ip_set_portmap);
7396 +module_init(ip_set_portmap_init);
7397 +module_exit(ip_set_portmap_fini);
7398 diff --git a/net/ipv4/netfilter/ipt_SET.c b/net/ipv4/netfilter/ipt_SET.c
7399 new file mode 100644
7400 index 0000000..2052c0c
7402 +++ b/net/ipv4/netfilter/ipt_SET.c
7404 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7405 + * Patrick Schaaf <bof@bof.de>
7406 + * Martin Josefsson <gandalf@wlug.westbo.se>
7407 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7409 + * This program is free software; you can redistribute it and/or modify
7410 + * it under the terms of the GNU General Public License version 2 as
7411 + * published by the Free Software Foundation.
7414 +/* ipt_SET.c - netfilter target to manipulate IP sets */
7416 +#include <linux/types.h>
7417 +#include <linux/ip.h>
7418 +#include <linux/timer.h>
7419 +#include <linux/module.h>
7420 +#include <linux/netfilter.h>
7421 +#include <linux/netdevice.h>
7422 +#include <linux/if.h>
7423 +#include <linux/inetdevice.h>
7424 +#include <linux/version.h>
7425 +#include <net/protocol.h>
7426 +#include <net/checksum.h>
7427 +#include <linux/netfilter_ipv4.h>
7428 +#include <linux/netfilter_ipv4/ip_tables.h>
7429 +#include <linux/netfilter_ipv4/ipt_set.h>
7431 +static unsigned int target(struct sk_buff *skb,
7432 + const struct net_device *in,
7433 + const struct net_device *out,
7434 + unsigned int hooknum,
7435 + const struct xt_target *target,
7436 + const void *targinfo)
7438 + const struct ipt_set_info_target *info = targinfo;
7440 + if (info->add_set.index != IP_SET_INVALID_ID)
7441 + ip_set_addip_kernel(info->add_set.index, skb,
7442 + info->add_set.flags);
7443 + if (info->del_set.index != IP_SET_INVALID_ID)
7444 + ip_set_delip_kernel(info->del_set.index, skb,
7445 + info->del_set.flags);
7447 + return IPT_CONTINUE;
7450 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7455 +checkentry(const char *tablename,
7456 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7459 + const struct ipt_entry *e,
7461 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7462 + const struct xt_target *target,
7465 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7466 + unsigned int targinfosize,
7468 + unsigned int hook_mask)
7470 + struct ipt_set_info_target *info =
7471 + (struct ipt_set_info_target *) targinfo;
7472 + ip_set_id_t index;
7474 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7475 + if (targinfosize != IPT_ALIGN(sizeof(*info))) {
7476 + DP("bad target info size %u", targinfosize);
7481 + if (info->add_set.index != IP_SET_INVALID_ID) {
7482 + index = ip_set_get_byindex(info->add_set.index);
7483 + if (index == IP_SET_INVALID_ID) {
7484 + ip_set_printk("cannot find add_set index %u as target",
7485 + info->add_set.index);
7486 + return 0; /* error */
7490 + if (info->del_set.index != IP_SET_INVALID_ID) {
7491 + index = ip_set_get_byindex(info->del_set.index);
7492 + if (index == IP_SET_INVALID_ID) {
7493 + ip_set_printk("cannot find del_set index %u as target",
7494 + info->del_set.index);
7495 + return 0; /* error */
7498 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
7499 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7500 + ip_set_printk("That's nasty!");
7501 + return 0; /* error */
7507 +static void destroy(
7508 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7509 + const struct xt_target *target,
7511 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7512 + void *targetinfo, unsigned int targetsize)
7517 + struct ipt_set_info_target *info = targetinfo;
7519 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7520 + if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
7521 + ip_set_printk("invalid targetsize %d", targetsize);
7525 + if (info->add_set.index != IP_SET_INVALID_ID)
7526 + ip_set_put(info->add_set.index);
7527 + if (info->del_set.index != IP_SET_INVALID_ID)
7528 + ip_set_put(info->del_set.index);
7531 +static struct ipt_target SET_target = {
7533 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7534 + .family = AF_INET,
7537 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7538 + .targetsize = sizeof(struct ipt_set_info_target),
7540 + .checkentry = checkentry,
7541 + .destroy = destroy,
7545 +MODULE_LICENSE("GPL");
7546 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7547 +MODULE_DESCRIPTION("iptables IP set target module");
7549 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7550 +#define ipt_register_target xt_register_target
7551 +#define ipt_unregister_target xt_unregister_target
7554 +static int __init ipt_SET_init(void)
7556 + return ipt_register_target(&SET_target);
7559 +static void __exit ipt_SET_fini(void)
7561 + ipt_unregister_target(&SET_target);
7564 +module_init(ipt_SET_init);
7565 +module_exit(ipt_SET_fini);
7566 diff --git a/net/ipv4/netfilter/ipt_set.c b/net/ipv4/netfilter/ipt_set.c
7567 new file mode 100644
7568 index 0000000..f192013
7570 +++ b/net/ipv4/netfilter/ipt_set.c
7572 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7573 + * Patrick Schaaf <bof@bof.de>
7574 + * Martin Josefsson <gandalf@wlug.westbo.se>
7575 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7577 + * This program is free software; you can redistribute it and/or modify
7578 + * it under the terms of the GNU General Public License version 2 as
7579 + * published by the Free Software Foundation.
7582 +/* Kernel module to match an IP set. */
7584 +#include <linux/module.h>
7585 +#include <linux/ip.h>
7586 +#include <linux/skbuff.h>
7587 +#include <linux/version.h>
7589 +#include <linux/netfilter_ipv4/ip_tables.h>
7590 +#include <linux/netfilter_ipv4/ip_set.h>
7591 +#include <linux/netfilter_ipv4/ipt_set.h>
7594 +match_set(const struct ipt_set_info *info,
7595 + const struct sk_buff *skb,
7598 + if (ip_set_testip_kernel(info->index, skb, info->flags))
7603 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7608 +match(const struct sk_buff *skb,
7609 + const struct net_device *in,
7610 + const struct net_device *out,
7611 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7612 + const struct xt_match *match,
7614 + const void *matchinfo,
7615 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7616 + int offset, unsigned int protoff, bool *hotdrop)
7617 +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7618 + int offset, unsigned int protoff, int *hotdrop)
7620 + int offset, int *hotdrop)
7623 + const struct ipt_set_info_match *info = matchinfo;
7625 + return match_set(&info->match_set,
7627 + info->match_set.flags[0] & IPSET_MATCH_INV);
7630 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7635 +checkentry(const char *tablename,
7636 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7639 + const struct ipt_ip *ip,
7641 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7642 + const struct xt_match *match,
7645 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7646 + unsigned int matchsize,
7648 + unsigned int hook_mask)
7650 + struct ipt_set_info_match *info =
7651 + (struct ipt_set_info_match *) matchinfo;
7652 + ip_set_id_t index;
7654 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7655 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7656 + ip_set_printk("invalid matchsize %d", matchsize);
7661 + index = ip_set_get_byindex(info->match_set.index);
7663 + if (index == IP_SET_INVALID_ID) {
7664 + ip_set_printk("Cannot find set indentified by id %u to match",
7665 + info->match_set.index);
7666 + return 0; /* error */
7668 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7669 + ip_set_printk("That's nasty!");
7670 + return 0; /* error */
7676 +static void destroy(
7677 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7678 + const struct xt_match *match,
7680 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7681 + void *matchinfo, unsigned int matchsize)
7686 + struct ipt_set_info_match *info = matchinfo;
7688 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7689 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7690 + ip_set_printk("invalid matchsize %d", matchsize);
7694 + ip_set_put(info->match_set.index);
7697 +static struct ipt_match set_match = {
7699 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7700 + .family = AF_INET,
7703 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7704 + .matchsize = sizeof(struct ipt_set_info_match),
7706 + .checkentry = &checkentry,
7707 + .destroy = &destroy,
7711 +MODULE_LICENSE("GPL");
7712 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7713 +MODULE_DESCRIPTION("iptables IP set match module");
7715 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7716 +#define ipt_register_match xt_register_match
7717 +#define ipt_unregister_match xt_unregister_match
7720 +static int __init ipt_ipset_init(void)
7722 + return ipt_register_match(&set_match);
7725 +static void __exit ipt_ipset_fini(void)
7727 + ipt_unregister_match(&set_match);
7730 +module_init(ipt_ipset_init);
7731 +module_exit(ipt_ipset_fini);