1 --- a/include/linux/netfilter_ipv4/Kbuild
2 +++ b/include/linux/netfilter_ipv4/Kbuild
3 @@ -45,3 +45,14 @@ header-y += ipt_ttl.h
5 unifdef-y += ip_queue.h
6 unifdef-y += ip_tables.h
9 +header-y += ip_set_iphash.h
10 +header-y += ip_set_ipmap.h
11 +header-y += ip_set_ipporthash.h
12 +unifdef-y += ip_set_iptree.h
13 +unifdef-y += ip_set_iptreemap.h
14 +header-y += ip_set_jhash.h
15 +header-y += ip_set_macipmap.h
16 +unifdef-y += ip_set_nethash.h
17 +header-y += ip_set_portmap.h
19 +++ b/include/linux/netfilter_ipv4/ip_set.h
24 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
25 + * Patrick Schaaf <bof@bof.de>
26 + * Martin Josefsson <gandalf@wlug.westbo.se>
27 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
29 + * This program is free software; you can redistribute it and/or modify
30 + * it under the terms of the GNU General Public License version 2 as
31 + * published by the Free Software Foundation.
39 + * A sockopt of such quality has hardly ever been seen before on the open
40 + * market! This little beauty, hardly ever used: above 64, so it's
41 + * traditionally used for firewalling, not touched (even once!) by the
42 + * 2.0, 2.2 and 2.4 kernels!
44 + * Comes with its own certificate of authenticity, valid anywhere in the
52 + * Heavily modify by Joakim Axelsson 08.03.2002
53 + * - Made it more modulebased
55 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
57 + * - in order to "deal with" backward compatibility, renamed to ipset
61 + * Used so that the kernel module and ipset-binary can match their versions
63 +#define IP_SET_PROTOCOL_VERSION 2
65 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
67 +/* Lets work with our own typedef for representing an IP address.
68 + * We hope to make the code more portable, possibly to IPv6...
70 + * The representation works in HOST byte order, because most set types
71 + * will perform arithmetic operations and compare operations.
73 + * For now the type is an uint32_t.
75 + * Make sure to ONLY use the functions when translating and parsing
76 + * in order to keep the host byte order and make it more portable:
81 + * (Joakim: where are they???)
84 +typedef uint32_t ip_set_ip_t;
86 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
87 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
89 +typedef uint16_t ip_set_id_t;
91 +#define IP_SET_INVALID_ID 65535
93 +/* How deep we follow bindings */
94 +#define IP_SET_MAX_BINDINGS 6
97 + * Option flags for kernel operations (ipt_set_info)
99 +#define IPSET_SRC 0x01 /* Source match/add */
100 +#define IPSET_DST 0x02 /* Destination match/add */
101 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
106 +#define IPSET_TYPE_IP 0x01 /* IP address type of set */
107 +#define IPSET_TYPE_PORT 0x02 /* Port type of set */
108 +#define IPSET_DATA_SINGLE 0x04 /* Single data storage */
109 +#define IPSET_DATA_DOUBLE 0x08 /* Double data storage */
111 +/* Reserved keywords */
112 +#define IPSET_TOKEN_DEFAULT ":default:"
113 +#define IPSET_TOKEN_ALL ":all:"
115 +/* SO_IP_SET operation constants, and their request struct types.
118 + * 0-99: commands with version checking
119 + * 100-199: add/del/test/bind/unbind
120 + * 200-299: list, save, restore
123 +/* Single shot operations:
124 + * version, create, destroy, flush, rename and swap
126 + * Sets are identified by name.
129 +#define IP_SET_REQ_STD \
131 + unsigned version; \
132 + char name[IP_SET_MAXNAMELEN]
134 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
135 +struct ip_set_req_create {
137 + char typename[IP_SET_MAXNAMELEN];
140 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
141 +struct ip_set_req_std {
145 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
146 +/* Uses ip_set_req_std */
148 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
149 +/* Uses ip_set_req_create */
151 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
152 +/* Uses ip_set_req_create */
154 +union ip_set_name_index {
155 + char name[IP_SET_MAXNAMELEN];
159 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
160 +struct ip_set_req_get_set {
163 + union ip_set_name_index set;
166 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
167 +/* Uses ip_set_req_get_set */
169 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
170 +struct ip_set_req_version {
175 +/* Double shots operations:
176 + * add, del, test, bind and unbind.
178 + * First we query the kernel to get the index and type of the target set,
179 + * then issue the command. Validity of IP is checked in kernel in order
180 + * to minimalize sockopt operations.
183 +/* Get minimal set data for add/del/test/bind/unbind IP */
184 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
185 +struct ip_set_req_adt_get {
188 + union ip_set_name_index set;
189 + char typename[IP_SET_MAXNAMELEN];
192 +#define IP_SET_REQ_BYINDEX \
196 +struct ip_set_req_adt {
197 + IP_SET_REQ_BYINDEX;
200 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
201 +/* Uses ip_set_req_adt, with type specific addage */
203 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
204 +/* Uses ip_set_req_adt, with type specific addage */
206 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
207 +/* Uses ip_set_req_adt, with type specific addage */
209 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
210 +/* Uses ip_set_req_bind, with type specific addage */
211 +struct ip_set_req_bind {
212 + IP_SET_REQ_BYINDEX;
213 + char binding[IP_SET_MAXNAMELEN];
216 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
217 +/* Uses ip_set_req_bind, with type speficic addage
218 + * index = 0 means unbinding for all sets */
220 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
221 +/* Uses ip_set_req_bind, with type specific addage */
223 +/* Multiple shots operations: list, save, restore.
225 + * - check kernel version and query the max number of sets
226 + * - get the basic information on all sets
227 + * and size required for the next step
228 + * - get actual set data: header, data, bindings
231 +/* Get max_sets and the index of a queried set
233 +#define IP_SET_OP_MAX_SETS 0x00000020
234 +struct ip_set_req_max_sets {
237 + ip_set_id_t max_sets; /* max_sets */
238 + ip_set_id_t sets; /* real number of sets */
239 + union ip_set_name_index set; /* index of set if name used */
242 +/* Get the id and name of the sets plus size for next step */
243 +#define IP_SET_OP_LIST_SIZE 0x00000201
244 +#define IP_SET_OP_SAVE_SIZE 0x00000202
245 +struct ip_set_req_setnames {
247 + ip_set_id_t index; /* set to list/save */
248 + size_t size; /* size to get setdata/bindings */
249 + /* followed by sets number of struct ip_set_name_list */
252 +struct ip_set_name_list {
253 + char name[IP_SET_MAXNAMELEN];
254 + char typename[IP_SET_MAXNAMELEN];
259 +/* The actual list operation */
260 +#define IP_SET_OP_LIST 0x00000203
261 +struct ip_set_req_list {
262 + IP_SET_REQ_BYINDEX;
263 + /* sets number of struct ip_set_list in reply */
266 +struct ip_set_list {
268 + ip_set_id_t binding;
270 + size_t header_size; /* Set header data of header_size */
271 + size_t members_size; /* Set members data of members_size */
272 + size_t bindings_size; /* Set bindings data of bindings_size */
275 +struct ip_set_hash_list {
277 + ip_set_id_t binding;
280 +/* The save operation */
281 +#define IP_SET_OP_SAVE 0x00000204
282 +/* Uses ip_set_req_list, in the reply replaced by
283 + * sets number of struct ip_set_save plus a marker
284 + * ip_set_save followed by ip_set_hash_save structures.
286 +struct ip_set_save {
288 + ip_set_id_t binding;
289 + size_t header_size; /* Set header data of header_size */
290 + size_t members_size; /* Set members data of members_size */
293 +/* At restoring, ip == 0 means default binding for the given set: */
294 +struct ip_set_hash_save {
297 + ip_set_id_t binding;
300 +/* The restore operation */
301 +#define IP_SET_OP_RESTORE 0x00000205
302 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
303 + * plus a marker ip_set_restore, followed by ip_set_hash_save
306 +struct ip_set_restore {
307 + char name[IP_SET_MAXNAMELEN];
308 + char typename[IP_SET_MAXNAMELEN];
310 + size_t header_size; /* Create data of header_size */
311 + size_t members_size; /* Set members data of members_size */
314 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
316 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
321 +#define ip_set_printk(format, args...) \
323 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
324 + printk(format "\n" , ## args); \
327 +#if defined(IP_SET_DEBUG)
328 +#define DP(format, args...) \
330 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
331 + printk(format "\n" , ## args); \
333 +#define IP_SET_ASSERT(x) \
336 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
337 + __FILE__, __LINE__, __FUNCTION__); \
340 +#define DP(format, args...)
341 +#define IP_SET_ASSERT(x)
347 + * The ip_set_type definition - one per set type, e.g. "ipmap".
349 + * Each individual set has a pointer, set->type, going to one
350 + * of these structures. Function pointers inside the structure implement
351 + * the real behaviour of the sets.
353 + * If not mentioned differently, the implementation behind the function
354 + * pointers of a set_type, is expected to return 0 if ok, and a negative
355 + * errno (e.g. -EINVAL) on error.
357 +struct ip_set_type {
358 + struct list_head list; /* next in list of set types */
360 + /* test for IP in set (kernel: iptables -m set src|dst)
361 + * return 0 if not in set, 1 if in set.
363 + int (*testip_kernel) (struct ip_set *set,
364 + const struct sk_buff * skb,
366 + const u_int32_t *flags,
367 + unsigned char index);
369 + /* test for IP in set (userspace: ipset -T set IP)
370 + * return 0 if not in set, 1 if in set.
372 + int (*testip) (struct ip_set *set,
373 + const void *data, size_t size,
377 + * Size of the data structure passed by when
378 + * adding/deletin/testing an entry.
382 + /* Add IP into set (userspace: ipset -A set IP)
383 + * Return -EEXIST if the address is already in the set,
384 + * and -ERANGE if the address lies outside the set bounds.
385 + * If the address was not already in the set, 0 is returned.
387 + int (*addip) (struct ip_set *set,
388 + const void *data, size_t size,
391 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
392 + * Return -EEXIST if the address is already in the set,
393 + * and -ERANGE if the address lies outside the set bounds.
394 + * If the address was not already in the set, 0 is returned.
396 + int (*addip_kernel) (struct ip_set *set,
397 + const struct sk_buff * skb,
399 + const u_int32_t *flags,
400 + unsigned char index);
402 + /* remove IP from set (userspace: ipset -D set --entry x)
403 + * Return -EEXIST if the address is NOT in the set,
404 + * and -ERANGE if the address lies outside the set bounds.
405 + * If the address really was in the set, 0 is returned.
407 + int (*delip) (struct ip_set *set,
408 + const void *data, size_t size,
411 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
412 + * Return -EEXIST if the address is NOT in the set,
413 + * and -ERANGE if the address lies outside the set bounds.
414 + * If the address really was in the set, 0 is returned.
416 + int (*delip_kernel) (struct ip_set *set,
417 + const struct sk_buff * skb,
419 + const u_int32_t *flags,
420 + unsigned char index);
422 + /* new set creation - allocated type specific items
424 + int (*create) (struct ip_set *set,
425 + const void *data, size_t size);
427 + /* retry the operation after successfully tweaking the set
429 + int (*retry) (struct ip_set *set);
431 + /* set destruction - free type specific items
432 + * There is no return value.
433 + * Can be called only when child sets are destroyed.
435 + void (*destroy) (struct ip_set *set);
437 + /* set flushing - reset all bits in the set, or something similar.
438 + * There is no return value.
440 + void (*flush) (struct ip_set *set);
442 + /* Listing: size needed for header
444 + size_t header_size;
446 + /* Listing: Get the header
448 + * Fill in the information in "data".
449 + * This function is always run after list_header_size() under a
450 + * writelock on the set. Therefor is the length of "data" always
453 + void (*list_header) (const struct ip_set *set,
456 + /* Listing: Get the size for the set members
458 + int (*list_members_size) (const struct ip_set *set);
460 + /* Listing: Get the set members
462 + * Fill in the information in "data".
463 + * This function is always run after list_member_size() under a
464 + * writelock on the set. Therefor is the length of "data" always
467 + void (*list_members) (const struct ip_set *set,
470 + char typename[IP_SET_MAXNAMELEN];
471 + unsigned char features;
472 + int protocol_version;
474 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
478 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
479 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
481 +/* A generic ipset */
483 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
484 + rwlock_t lock; /* lock for concurrency control */
485 + ip_set_id_t id; /* set id for swapping */
486 + ip_set_id_t binding; /* default binding for the set */
487 + atomic_t ref; /* in kernel and in hash references */
488 + struct ip_set_type *type; /* the set types */
489 + void *data; /* pooltype specific data */
492 +/* Structure to bind set elements to sets */
493 +struct ip_set_hash {
494 + struct list_head list; /* list of clashing entries in hash */
495 + ip_set_ip_t ip; /* ip from set */
496 + ip_set_id_t id; /* set id */
497 + ip_set_id_t binding; /* set we bind the element to */
500 +/* register and unregister set references */
501 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
502 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
503 +extern void ip_set_put(ip_set_id_t id);
505 +/* API for iptables set match, and SET target */
506 +extern void ip_set_addip_kernel(ip_set_id_t id,
507 + const struct sk_buff *skb,
508 + const u_int32_t *flags);
509 +extern void ip_set_delip_kernel(ip_set_id_t id,
510 + const struct sk_buff *skb,
511 + const u_int32_t *flags);
512 +extern int ip_set_testip_kernel(ip_set_id_t id,
513 + const struct sk_buff *skb,
514 + const u_int32_t *flags);
516 +#endif /* __KERNEL__ */
518 +#endif /*_IP_SET_H*/
520 +++ b/include/linux/netfilter_ipv4/ip_set_iphash.h
522 +#ifndef __IP_SET_IPHASH_H
523 +#define __IP_SET_IPHASH_H
525 +#include <linux/netfilter_ipv4/ip_set.h>
527 +#define SETTYPE_NAME "iphash"
528 +#define MAX_RANGE 0x0000FFFF
530 +struct ip_set_iphash {
531 + ip_set_ip_t *members; /* the iphash proper */
532 + uint32_t elements; /* number of elements */
533 + uint32_t hashsize; /* hash size */
534 + uint16_t probes; /* max number of probes */
535 + uint16_t resize; /* resize factor in percent */
536 + ip_set_ip_t netmask; /* netmask */
537 + void *initval[0]; /* initvals for jhash_1word */
540 +struct ip_set_req_iphash_create {
544 + ip_set_ip_t netmask;
547 +struct ip_set_req_iphash {
551 +#endif /* __IP_SET_IPHASH_H */
553 +++ b/include/linux/netfilter_ipv4/ip_set_ipmap.h
555 +#ifndef __IP_SET_IPMAP_H
556 +#define __IP_SET_IPMAP_H
558 +#include <linux/netfilter_ipv4/ip_set.h>
560 +#define SETTYPE_NAME "ipmap"
561 +#define MAX_RANGE 0x0000FFFF
563 +struct ip_set_ipmap {
564 + void *members; /* the ipmap proper */
565 + ip_set_ip_t first_ip; /* host byte order, included in range */
566 + ip_set_ip_t last_ip; /* host byte order, included in range */
567 + ip_set_ip_t netmask; /* subnet netmask */
568 + ip_set_ip_t sizeid; /* size of set in IPs */
569 + ip_set_ip_t hosts; /* number of hosts in a subnet */
572 +struct ip_set_req_ipmap_create {
575 + ip_set_ip_t netmask;
578 +struct ip_set_req_ipmap {
583 +mask_to_bits(ip_set_ip_t mask)
585 + unsigned int bits = 32;
586 + ip_set_ip_t maskaddr;
588 + if (mask == 0xFFFFFFFF)
591 + maskaddr = 0xFFFFFFFE;
592 + while (--bits >= 0 && maskaddr != mask)
599 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
601 + ip_set_ip_t mask = 0xFFFFFFFE;
604 + while (--(*bits) >= 0 && mask && (to & mask) != from)
610 +#endif /* __IP_SET_IPMAP_H */
612 +++ b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
614 +#ifndef __IP_SET_IPPORTHASH_H
615 +#define __IP_SET_IPPORTHASH_H
617 +#include <linux/netfilter_ipv4/ip_set.h>
619 +#define SETTYPE_NAME "ipporthash"
620 +#define MAX_RANGE 0x0000FFFF
621 +#define INVALID_PORT (MAX_RANGE + 1)
623 +struct ip_set_ipporthash {
624 + ip_set_ip_t *members; /* the ipporthash proper */
625 + uint32_t elements; /* number of elements */
626 + uint32_t hashsize; /* hash size */
627 + uint16_t probes; /* max number of probes */
628 + uint16_t resize; /* resize factor in percent */
629 + ip_set_ip_t first_ip; /* host byte order, included in range */
630 + ip_set_ip_t last_ip; /* host byte order, included in range */
631 + void *initval[0]; /* initvals for jhash_1word */
634 +struct ip_set_req_ipporthash_create {
642 +struct ip_set_req_ipporthash {
647 +#endif /* __IP_SET_IPPORTHASH_H */
649 +++ b/include/linux/netfilter_ipv4/ip_set_iptree.h
651 +#ifndef __IP_SET_IPTREE_H
652 +#define __IP_SET_IPTREE_H
654 +#include <linux/netfilter_ipv4/ip_set.h>
656 +#define SETTYPE_NAME "iptree"
657 +#define MAX_RANGE 0x0000FFFF
659 +struct ip_set_iptreed {
660 + unsigned long expires[256]; /* x.x.x.ADDR */
663 +struct ip_set_iptreec {
664 + struct ip_set_iptreed *tree[256]; /* x.x.ADDR.* */
667 +struct ip_set_iptreeb {
668 + struct ip_set_iptreec *tree[256]; /* x.ADDR.*.* */
671 +struct ip_set_iptree {
672 + unsigned int timeout;
673 + unsigned int gc_interval;
675 + uint32_t elements; /* number of elements */
676 + struct timer_list gc;
677 + struct ip_set_iptreeb *tree[256]; /* ADDR.*.*.* */
681 +struct ip_set_req_iptree_create {
682 + unsigned int timeout;
685 +struct ip_set_req_iptree {
687 + unsigned int timeout;
690 +#endif /* __IP_SET_IPTREE_H */
692 +++ b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
694 +#ifndef __IP_SET_IPTREEMAP_H
695 +#define __IP_SET_IPTREEMAP_H
697 +#include <linux/netfilter_ipv4/ip_set.h>
699 +#define SETTYPE_NAME "iptreemap"
702 +struct ip_set_iptreemap_d {
703 + unsigned char bitmap[32]; /* x.x.x.y */
706 +struct ip_set_iptreemap_c {
707 + struct ip_set_iptreemap_d *tree[256]; /* x.x.y.x */
710 +struct ip_set_iptreemap_b {
711 + struct ip_set_iptreemap_c *tree[256]; /* x.y.x.x */
712 + unsigned char dirty[32];
716 +struct ip_set_iptreemap {
717 + unsigned int gc_interval;
719 + struct timer_list gc;
720 + struct ip_set_iptreemap_b *tree[256]; /* y.x.x.x */
724 +struct ip_set_req_iptreemap_create {
725 + unsigned int gc_interval;
728 +struct ip_set_req_iptreemap {
733 +#endif /* __IP_SET_IPTREEMAP_H */
735 +++ b/include/linux/netfilter_ipv4/ip_set_jhash.h
737 +#ifndef _LINUX_IPSET_JHASH_H
738 +#define _LINUX_IPSET_JHASH_H
740 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
741 + * to __u32/__u8 so that the header file can be included into
742 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
745 +/* jhash.h: Jenkins hash support.
747 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
749 + * http://burtleburtle.net/bob/hash/
751 + * These are the credits from Bob's sources:
753 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
754 + * hash(), hash2(), hash3, and mix() are externally useful functions.
755 + * Routines to test the hash are included if SELF_TEST is defined.
756 + * You can use this free for any purpose. It has no warranty.
758 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
760 + * I've modified Bob's hash to be useful in the Linux kernel, and
761 + * any bugs present are surely my fault. -DaveM
764 +/* NOTE: Arguments are modified. */
765 +#define __jhash_mix(a, b, c) \
767 + a -= b; a -= c; a ^= (c>>13); \
768 + b -= c; b -= a; b ^= (a<<8); \
769 + c -= a; c -= b; c ^= (b>>13); \
770 + a -= b; a -= c; a ^= (c>>12); \
771 + b -= c; b -= a; b ^= (a<<16); \
772 + c -= a; c -= b; c ^= (b>>5); \
773 + a -= b; a -= c; a ^= (c>>3); \
774 + b -= c; b -= a; b ^= (a<<10); \
775 + c -= a; c -= b; c ^= (b>>15); \
778 +/* The golden ration: an arbitrary value */
779 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
781 +/* The most generic version, hashes an arbitrary sequence
782 + * of bytes. No alignment or length assumptions are made about
785 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
787 + __u32 a, b, c, len;
791 + a = b = JHASH_GOLDEN_RATIO;
794 + while (len >= 12) {
795 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
796 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
797 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
799 + __jhash_mix(a,b,c);
807 + case 11: c += ((__u32)k[10]<<24);
808 + case 10: c += ((__u32)k[9]<<16);
809 + case 9 : c += ((__u32)k[8]<<8);
810 + case 8 : b += ((__u32)k[7]<<24);
811 + case 7 : b += ((__u32)k[6]<<16);
812 + case 6 : b += ((__u32)k[5]<<8);
813 + case 5 : b += k[4];
814 + case 4 : a += ((__u32)k[3]<<24);
815 + case 3 : a += ((__u32)k[2]<<16);
816 + case 2 : a += ((__u32)k[1]<<8);
817 + case 1 : a += k[0];
820 + __jhash_mix(a,b,c);
825 +/* A special optimized version that handles 1 or more of __u32s.
826 + * The length parameter here is the number of __u32s in the key.
828 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
830 + __u32 a, b, c, len;
832 + a = b = JHASH_GOLDEN_RATIO;
840 + __jhash_mix(a, b, c);
847 + case 2 : b += k[1];
848 + case 1 : a += k[0];
851 + __jhash_mix(a,b,c);
857 +/* A special ultra-optimized versions that knows they are hashing exactly
858 + * 3, 2 or 1 word(s).
860 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
861 + * done at the end is not done here.
863 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
865 + a += JHASH_GOLDEN_RATIO;
866 + b += JHASH_GOLDEN_RATIO;
869 + __jhash_mix(a, b, c);
874 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
876 + return jhash_3words(a, b, 0, initval);
879 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
881 + return jhash_3words(a, 0, 0, initval);
884 +#endif /* _LINUX_IPSET_JHASH_H */
886 +++ b/include/linux/netfilter_ipv4/ip_set_macipmap.h
888 +#ifndef __IP_SET_MACIPMAP_H
889 +#define __IP_SET_MACIPMAP_H
891 +#include <linux/netfilter_ipv4/ip_set.h>
893 +#define SETTYPE_NAME "macipmap"
894 +#define MAX_RANGE 0x0000FFFF
897 +#define IPSET_MACIP_MATCHUNSET 1
900 +#define IPSET_MACIP_ISSET 1
902 +struct ip_set_macipmap {
903 + void *members; /* the macipmap proper */
904 + ip_set_ip_t first_ip; /* host byte order, included in range */
905 + ip_set_ip_t last_ip; /* host byte order, included in range */
909 +struct ip_set_req_macipmap_create {
915 +struct ip_set_req_macipmap {
917 + unsigned char ethernet[ETH_ALEN];
920 +struct ip_set_macip {
921 + unsigned short flags;
922 + unsigned char ethernet[ETH_ALEN];
925 +#endif /* __IP_SET_MACIPMAP_H */
927 +++ b/include/linux/netfilter_ipv4/ip_set_malloc.h
929 +#ifndef _IP_SET_MALLOC_H
930 +#define _IP_SET_MALLOC_H
934 +/* Memory allocation and deallocation */
935 +static size_t max_malloc_size = 0;
937 +static inline void init_max_malloc_size(void)
939 +#define CACHE(x) max_malloc_size = x;
940 +#include <linux/kmalloc_sizes.h>
944 +static inline void * ip_set_malloc(size_t bytes)
946 + if (bytes > max_malloc_size)
947 + return vmalloc(bytes);
949 + return kmalloc(bytes, GFP_KERNEL);
952 +static inline void ip_set_free(void * data, size_t bytes)
954 + if (bytes > max_malloc_size)
961 + size_t max_elements;
965 +static inline void *
966 +harray_malloc(size_t hashsize, size_t typesize, int flags)
968 + struct harray *harray;
969 + size_t max_elements, size, i, j;
971 + if (!max_malloc_size)
972 + init_max_malloc_size();
974 + if (typesize > max_malloc_size)
977 + max_elements = max_malloc_size/typesize;
978 + size = hashsize/max_elements;
979 + if (hashsize % max_elements)
982 + /* Last pointer signals end of arrays */
983 + harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
989 + for (i = 0; i < size - 1; i++) {
990 + harray->arrays[i] = kmalloc(max_elements * typesize, flags);
991 + if (!harray->arrays[i])
993 + memset(harray->arrays[i], 0, max_elements * typesize);
995 + harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize,
997 + if (!harray->arrays[i])
999 + memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
1001 + harray->max_elements = max_elements;
1002 + harray->arrays[size] = NULL;
1004 + return (void *)harray;
1007 + for (j = 0; j < i; j++) {
1008 + kfree(harray->arrays[j]);
1014 +static inline void harray_free(void *h)
1016 + struct harray *harray = (struct harray *) h;
1019 + for (i = 0; harray->arrays[i] != NULL; i++)
1020 + kfree(harray->arrays[i]);
1024 +static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
1026 + struct harray *harray = (struct harray *) h;
1029 + for (i = 0; harray->arrays[i+1] != NULL; i++)
1030 + memset(harray->arrays[i], 0, harray->max_elements * typesize);
1031 + memset(harray->arrays[i], 0,
1032 + (hashsize - i * harray->max_elements) * typesize);
1035 +#define HARRAY_ELEM(h, type, which) \
1037 + struct harray *__h = (struct harray *)(h); \
1038 + ((type)((__h)->arrays[(which)/(__h)->max_elements]) \
1039 + + (which)%(__h)->max_elements); \
1042 +#endif /* __KERNEL__ */
1044 +#endif /*_IP_SET_MALLOC_H*/
1046 +++ b/include/linux/netfilter_ipv4/ip_set_nethash.h
1048 +#ifndef __IP_SET_NETHASH_H
1049 +#define __IP_SET_NETHASH_H
1051 +#include <linux/netfilter_ipv4/ip_set.h>
1053 +#define SETTYPE_NAME "nethash"
1054 +#define MAX_RANGE 0x0000FFFF
1056 +struct ip_set_nethash {
1057 + ip_set_ip_t *members; /* the nethash proper */
1058 + uint32_t elements; /* number of elements */
1059 + uint32_t hashsize; /* hash size */
1060 + uint16_t probes; /* max number of probes */
1061 + uint16_t resize; /* resize factor in percent */
1062 + unsigned char cidr[30]; /* CIDR sizes */
1063 + void *initval[0]; /* initvals for jhash_1word */
1066 +struct ip_set_req_nethash_create {
1067 + uint32_t hashsize;
1072 +struct ip_set_req_nethash {
1074 + unsigned char cidr;
1077 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
1079 +static inline ip_set_ip_t
1080 +pack(ip_set_ip_t ip, unsigned char cidr)
1082 + ip_set_ip_t addr, *paddr = &addr;
1083 + unsigned char n, t, *a;
1085 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
1087 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
1091 + a = &((unsigned char *)paddr)[n];
1092 + *a = *a /(1 << (8 - t)) + shifts[t];
1094 + DP("n: %u, t: %u, a: %u", n, t, *a);
1095 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
1096 + HIPQUAD(ip), cidr, NIPQUAD(addr));
1099 + return ntohl(addr);
1102 +#endif /* __IP_SET_NETHASH_H */
1104 +++ b/include/linux/netfilter_ipv4/ip_set_portmap.h
1106 +#ifndef __IP_SET_PORTMAP_H
1107 +#define __IP_SET_PORTMAP_H
1109 +#include <linux/netfilter_ipv4/ip_set.h>
1111 +#define SETTYPE_NAME "portmap"
1112 +#define MAX_RANGE 0x0000FFFF
1113 +#define INVALID_PORT (MAX_RANGE + 1)
1115 +struct ip_set_portmap {
1116 + void *members; /* the portmap proper */
1117 + ip_set_ip_t first_port; /* host byte order, included in range */
1118 + ip_set_ip_t last_port; /* host byte order, included in range */
1121 +struct ip_set_req_portmap_create {
1126 +struct ip_set_req_portmap {
1130 +#endif /* __IP_SET_PORTMAP_H */
1132 +++ b/include/linux/netfilter_ipv4/ipt_set.h
1137 +#include <linux/netfilter_ipv4/ip_set.h>
1139 +struct ipt_set_info {
1140 + ip_set_id_t index;
1141 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1145 +struct ipt_set_info_match {
1146 + struct ipt_set_info match_set;
1149 +struct ipt_set_info_target {
1150 + struct ipt_set_info add_set;
1151 + struct ipt_set_info del_set;
1154 +#endif /*_IPT_SET_H*/
1156 +++ b/net/ipv4/netfilter/ip_set.c
1158 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1159 + * Patrick Schaaf <bof@bof.de>
1160 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1162 + * This program is free software; you can redistribute it and/or modify
1163 + * it under the terms of the GNU General Public License version 2 as
1164 + * published by the Free Software Foundation.
1167 +/* Kernel module for IP set management */
1169 +#include <linux/version.h>
1170 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
1171 +#include <linux/config.h>
1173 +#include <linux/module.h>
1174 +#include <linux/moduleparam.h>
1175 +#include <linux/kmod.h>
1176 +#include <linux/ip.h>
1177 +#include <linux/skbuff.h>
1178 +#include <linux/random.h>
1179 +#include <linux/jhash.h>
1180 +#include <linux/netfilter_ipv4/ip_tables.h>
1181 +#include <linux/errno.h>
1182 +#include <asm/uaccess.h>
1183 +#include <asm/bitops.h>
1184 +#include <asm/semaphore.h>
1185 +#include <linux/spinlock.h>
1186 +#include <linux/vmalloc.h>
1188 +#define ASSERT_READ_LOCK(x)
1189 +#define ASSERT_WRITE_LOCK(x)
1190 +#include <linux/netfilter_ipv4/ip_set.h>
1192 +static struct list_head set_type_list; /* all registered sets */
1193 +static struct ip_set **ip_set_list; /* all individual sets */
1194 +static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1195 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1196 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1197 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1198 +static struct list_head *ip_set_hash; /* hash of bindings */
1199 +static unsigned int ip_set_hash_random; /* random seed */
1202 + * Sets are identified either by the index in ip_set_list or by id.
1203 + * The id never changes and is used to find a key in the hash.
1204 + * The index may change by swapping and used at all other places
1205 + * (set/SET netfilter modules, binding value, etc.)
1207 + * Userspace requests are serialized by ip_set_mutex and sets can
1208 + * be deleted only from userspace. Therefore ip_set_list locking
1209 + * must obey the following rules:
1211 + * - kernel requests: read and write locking mandatory
1212 + * - user requests: read locking optional, write locking mandatory
1216 +__ip_set_get(ip_set_id_t index)
1218 + atomic_inc(&ip_set_list[index]->ref);
1222 +__ip_set_put(ip_set_id_t index)
1224 + atomic_dec(&ip_set_list[index]->ref);
1228 + * Binding routines
1231 +static inline struct ip_set_hash *
1232 +__ip_set_find(u_int32_t key, ip_set_id_t id, ip_set_ip_t ip)
1234 + struct ip_set_hash *set_hash;
1236 + list_for_each_entry(set_hash, &ip_set_hash[key], list)
1237 + if (set_hash->id == id && set_hash->ip == ip)
1244 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1246 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1247 + % ip_set_bindings_hash_size;
1248 + struct ip_set_hash *set_hash;
1250 + ASSERT_READ_LOCK(&ip_set_lock);
1251 + IP_SET_ASSERT(ip_set_list[id]);
1252 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1254 + set_hash = __ip_set_find(key, id, ip);
1256 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1258 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1260 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1264 +__set_hash_del(struct ip_set_hash *set_hash)
1266 + ASSERT_WRITE_LOCK(&ip_set_lock);
1267 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1269 + __ip_set_put(set_hash->binding);
1270 + list_del(&set_hash->list);
1275 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1277 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1278 + % ip_set_bindings_hash_size;
1279 + struct ip_set_hash *set_hash;
1281 + IP_SET_ASSERT(ip_set_list[id]);
1282 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1283 + write_lock_bh(&ip_set_lock);
1284 + set_hash = __ip_set_find(key, id, ip);
1285 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1287 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1289 + if (set_hash != NULL)
1290 + __set_hash_del(set_hash);
1291 + write_unlock_bh(&ip_set_lock);
1296 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1298 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1299 + % ip_set_bindings_hash_size;
1300 + struct ip_set_hash *set_hash;
1303 + IP_SET_ASSERT(ip_set_list[id]);
1304 + IP_SET_ASSERT(ip_set_list[binding]);
1305 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1306 + HIPQUAD(ip), ip_set_list[binding]->name);
1307 + write_lock_bh(&ip_set_lock);
1308 + set_hash = __ip_set_find(key, id, ip);
1310 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_ATOMIC);
1315 + INIT_LIST_HEAD(&set_hash->list);
1316 + set_hash->id = id;
1317 + set_hash->ip = ip;
1318 + list_add(&set_hash->list, &ip_set_hash[key]);
1320 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1321 + DP("overwrite binding: %s",
1322 + ip_set_list[set_hash->binding]->name);
1323 + __ip_set_put(set_hash->binding);
1325 + set_hash->binding = binding;
1326 + __ip_set_get(set_hash->binding);
1327 + DP("stored: key %u, id %u (%s), ip %u.%u.%u.%u, binding %u (%s)",
1328 + key, id, ip_set_list[id]->name,
1329 + HIPQUAD(ip), binding, ip_set_list[binding]->name);
1331 + write_unlock_bh(&ip_set_lock);
1335 +#define FOREACH_HASH_DO(fn, args...) \
1337 + ip_set_id_t __key; \
1338 + struct ip_set_hash *__set_hash; \
1340 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1341 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1342 + fn(__set_hash , ## args); \
1346 +#define FOREACH_HASH_RW_DO(fn, args...) \
1348 + ip_set_id_t __key; \
1349 + struct ip_set_hash *__set_hash, *__n; \
1351 + ASSERT_WRITE_LOCK(&ip_set_lock); \
1352 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1353 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1354 + fn(__set_hash , ## args); \
1358 +/* Add, del and test set entries from kernel */
1360 +#define follow_bindings(index, set, ip) \
1361 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1362 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1365 +ip_set_testip_kernel(ip_set_id_t index,
1366 + const struct sk_buff *skb,
1367 + const u_int32_t *flags)
1369 + struct ip_set *set;
1372 + unsigned char i = 0;
1374 + IP_SET_ASSERT(flags[i]);
1375 + read_lock_bh(&ip_set_lock);
1377 + set = ip_set_list[index];
1378 + IP_SET_ASSERT(set);
1379 + DP("set %s, index %u", set->name, index);
1380 + read_lock_bh(&set->lock);
1381 + res = set->type->testip_kernel(set, skb, &ip, flags, i++);
1382 + read_unlock_bh(&set->lock);
1383 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1386 + && follow_bindings(index, set, ip));
1387 + read_unlock_bh(&ip_set_lock);
1393 +ip_set_addip_kernel(ip_set_id_t index,
1394 + const struct sk_buff *skb,
1395 + const u_int32_t *flags)
1397 + struct ip_set *set;
1400 + unsigned char i = 0;
1402 + IP_SET_ASSERT(flags[i]);
1404 + read_lock_bh(&ip_set_lock);
1406 + set = ip_set_list[index];
1407 + IP_SET_ASSERT(set);
1408 + DP("set %s, index %u", set->name, index);
1409 + write_lock_bh(&set->lock);
1410 + res = set->type->addip_kernel(set, skb, &ip, flags, i++);
1411 + write_unlock_bh(&set->lock);
1412 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1413 + } while ((res == 0 || res == -EEXIST)
1415 + && follow_bindings(index, set, ip));
1416 + read_unlock_bh(&ip_set_lock);
1418 + if (res == -EAGAIN
1419 + && set->type->retry
1420 + && (res = set->type->retry(set)) == 0)
1425 +ip_set_delip_kernel(ip_set_id_t index,
1426 + const struct sk_buff *skb,
1427 + const u_int32_t *flags)
1429 + struct ip_set *set;
1432 + unsigned char i = 0;
1434 + IP_SET_ASSERT(flags[i]);
1435 + read_lock_bh(&ip_set_lock);
1437 + set = ip_set_list[index];
1438 + IP_SET_ASSERT(set);
1439 + DP("set %s, index %u", set->name, index);
1440 + write_lock_bh(&set->lock);
1441 + res = set->type->delip_kernel(set, skb, &ip, flags, i++);
1442 + write_unlock_bh(&set->lock);
1443 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1444 + } while ((res == 0 || res == -EEXIST)
1446 + && follow_bindings(index, set, ip));
1447 + read_unlock_bh(&ip_set_lock);
1450 +/* Register and deregister settype */
1452 +static inline struct ip_set_type *
1453 +find_set_type(const char *name)
1455 + struct ip_set_type *set_type;
1457 + list_for_each_entry(set_type, &set_type_list, list)
1458 + if (!strncmp(set_type->typename, name, IP_SET_MAXNAMELEN - 1))
1464 +ip_set_register_set_type(struct ip_set_type *set_type)
1468 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1469 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1470 + set_type->typename,
1471 + set_type->protocol_version,
1472 + IP_SET_PROTOCOL_VERSION);
1476 + write_lock_bh(&ip_set_lock);
1477 + if (find_set_type(set_type->typename)) {
1479 + ip_set_printk("'%s' already registered!",
1480 + set_type->typename);
1484 + if (!try_module_get(THIS_MODULE)) {
1488 + list_add(&set_type->list, &set_type_list);
1489 + DP("'%s' registered.", set_type->typename);
1491 + write_unlock_bh(&ip_set_lock);
1496 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1498 + write_lock_bh(&ip_set_lock);
1499 + if (!find_set_type(set_type->typename)) {
1500 + ip_set_printk("'%s' not registered?",
1501 + set_type->typename);
1504 + list_del(&set_type->list);
1505 + module_put(THIS_MODULE);
1506 + DP("'%s' unregistered.", set_type->typename);
1508 + write_unlock_bh(&ip_set_lock);
1513 + * Userspace routines
1517 + * Find set by name, reference it once. The reference makes sure the
1518 + * thing pointed to, does not go away under our feet. Drop the reference
1519 + * later, using ip_set_put().
1522 +ip_set_get_byname(const char *name)
1524 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1526 + down(&ip_set_app_mutex);
1527 + for (i = 0; i < ip_set_max; i++) {
1528 + if (ip_set_list[i] != NULL
1529 + && strcmp(ip_set_list[i]->name, name) == 0) {
1535 + up(&ip_set_app_mutex);
1540 + * Find set by index, reference it once. The reference makes sure the
1541 + * thing pointed to, does not go away under our feet. Drop the reference
1542 + * later, using ip_set_put().
1545 +ip_set_get_byindex(ip_set_id_t index)
1547 + down(&ip_set_app_mutex);
1549 + if (index >= ip_set_max)
1550 + return IP_SET_INVALID_ID;
1552 + if (ip_set_list[index])
1553 + __ip_set_get(index);
1555 + index = IP_SET_INVALID_ID;
1557 + up(&ip_set_app_mutex);
1562 + * If the given set pointer points to a valid set, decrement
1563 + * reference count by 1. The caller shall not assume the index
1564 + * to be valid, after calling this function.
1566 +void ip_set_put(ip_set_id_t index)
1568 + down(&ip_set_app_mutex);
1569 + if (ip_set_list[index])
1570 + __ip_set_put(index);
1571 + up(&ip_set_app_mutex);
1574 +/* Find a set by name or index */
1576 +ip_set_find_byname(const char *name)
1578 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1580 + for (i = 0; i < ip_set_max; i++) {
1581 + if (ip_set_list[i] != NULL
1582 + && strcmp(ip_set_list[i]->name, name) == 0) {
1591 +ip_set_find_byindex(ip_set_id_t index)
1593 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1594 + index = IP_SET_INVALID_ID;
1600 + * Add, del, test, bind and unbind
1604 +__ip_set_testip(struct ip_set *set,
1611 + read_lock_bh(&set->lock);
1612 + res = set->type->testip(set, data, size, ip);
1613 + read_unlock_bh(&set->lock);
1619 +__ip_set_addip(ip_set_id_t index,
1623 + struct ip_set *set = ip_set_list[index];
1627 + IP_SET_ASSERT(set);
1629 + write_lock_bh(&set->lock);
1630 + res = set->type->addip(set, data, size, &ip);
1631 + write_unlock_bh(&set->lock);
1632 + } while (res == -EAGAIN
1633 + && set->type->retry
1634 + && (res = set->type->retry(set)) == 0);
1640 +ip_set_addip(ip_set_id_t index,
1645 + return __ip_set_addip(index,
1646 + data + sizeof(struct ip_set_req_adt),
1647 + size - sizeof(struct ip_set_req_adt));
1651 +ip_set_delip(ip_set_id_t index,
1655 + struct ip_set *set = ip_set_list[index];
1659 + IP_SET_ASSERT(set);
1660 + write_lock_bh(&set->lock);
1661 + res = set->type->delip(set,
1662 + data + sizeof(struct ip_set_req_adt),
1663 + size - sizeof(struct ip_set_req_adt),
1665 + write_unlock_bh(&set->lock);
1671 +ip_set_testip(ip_set_id_t index,
1675 + struct ip_set *set = ip_set_list[index];
1679 + IP_SET_ASSERT(set);
1680 + res = __ip_set_testip(set,
1681 + data + sizeof(struct ip_set_req_adt),
1682 + size - sizeof(struct ip_set_req_adt),
1685 + return (res > 0 ? -EEXIST : res);
1689 +ip_set_bindip(ip_set_id_t index,
1693 + struct ip_set *set = ip_set_list[index];
1694 + struct ip_set_req_bind *req_bind;
1695 + ip_set_id_t binding;
1699 + IP_SET_ASSERT(set);
1700 + if (size < sizeof(struct ip_set_req_bind))
1703 + req_bind = (struct ip_set_req_bind *) data;
1704 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1706 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1707 + /* Default binding of a set */
1708 + char *binding_name;
1710 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1713 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1714 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1716 + binding = ip_set_find_byname(binding_name);
1717 + if (binding == IP_SET_INVALID_ID)
1720 + write_lock_bh(&ip_set_lock);
1721 + /* Sets as binding values are referenced */
1722 + if (set->binding != IP_SET_INVALID_ID)
1723 + __ip_set_put(set->binding);
1724 + set->binding = binding;
1725 + __ip_set_get(set->binding);
1726 + write_unlock_bh(&ip_set_lock);
1730 + binding = ip_set_find_byname(req_bind->binding);
1731 + if (binding == IP_SET_INVALID_ID)
1734 + res = __ip_set_testip(set,
1735 + data + sizeof(struct ip_set_req_bind),
1736 + size - sizeof(struct ip_set_req_bind),
1738 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1739 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1742 + res = ip_set_hash_add(set->id, ip, binding);
1747 +#define FOREACH_SET_DO(fn, args...) \
1749 + ip_set_id_t __i; \
1750 + struct ip_set *__set; \
1752 + for (__i = 0; __i < ip_set_max; __i++) { \
1753 + __set = ip_set_list[__i]; \
1754 + if (__set != NULL) \
1755 + fn(__set , ##args); \
1760 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1762 + if (set_hash->id == id)
1763 + __set_hash_del(set_hash);
1767 +__unbind_default(struct ip_set *set)
1769 + if (set->binding != IP_SET_INVALID_ID) {
1770 + /* Sets as binding values are referenced */
1771 + __ip_set_put(set->binding);
1772 + set->binding = IP_SET_INVALID_ID;
1777 +ip_set_unbindip(ip_set_id_t index,
1781 + struct ip_set *set;
1782 + struct ip_set_req_bind *req_bind;
1787 + if (size < sizeof(struct ip_set_req_bind))
1790 + req_bind = (struct ip_set_req_bind *) data;
1791 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1793 + DP("%u %s", index, req_bind->binding);
1794 + if (index == IP_SET_INVALID_ID) {
1795 + /* unbind :all: */
1796 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1797 + /* Default binding of sets */
1798 + write_lock_bh(&ip_set_lock);
1799 + FOREACH_SET_DO(__unbind_default);
1800 + write_unlock_bh(&ip_set_lock);
1802 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1803 + /* Flush all bindings of all sets*/
1804 + write_lock_bh(&ip_set_lock);
1805 + FOREACH_HASH_RW_DO(__set_hash_del);
1806 + write_unlock_bh(&ip_set_lock);
1809 + DP("unreachable reached!");
1813 + set = ip_set_list[index];
1814 + IP_SET_ASSERT(set);
1815 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1816 + /* Default binding of set */
1817 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1819 + if (binding == IP_SET_INVALID_ID)
1822 + write_lock_bh(&ip_set_lock);
1823 + /* Sets in hash values are referenced */
1824 + __ip_set_put(set->binding);
1825 + set->binding = IP_SET_INVALID_ID;
1826 + write_unlock_bh(&ip_set_lock);
1829 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1830 + /* Flush all bindings */
1832 + write_lock_bh(&ip_set_lock);
1833 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1834 + write_unlock_bh(&ip_set_lock);
1838 + res = __ip_set_testip(set,
1839 + data + sizeof(struct ip_set_req_bind),
1840 + size - sizeof(struct ip_set_req_bind),
1843 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1845 + res = ip_set_hash_del(set->id, ip);
1851 +ip_set_testbind(ip_set_id_t index,
1855 + struct ip_set *set = ip_set_list[index];
1856 + struct ip_set_req_bind *req_bind;
1857 + ip_set_id_t binding;
1861 + IP_SET_ASSERT(set);
1862 + if (size < sizeof(struct ip_set_req_bind))
1865 + req_bind = (struct ip_set_req_bind *) data;
1866 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1868 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1869 + /* Default binding of set */
1870 + char *binding_name;
1872 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1875 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1876 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1878 + binding = ip_set_find_byname(binding_name);
1879 + if (binding == IP_SET_INVALID_ID)
1882 + res = (set->binding == binding) ? -EEXIST : 0;
1886 + binding = ip_set_find_byname(req_bind->binding);
1887 + if (binding == IP_SET_INVALID_ID)
1891 + res = __ip_set_testip(set,
1892 + data + sizeof(struct ip_set_req_bind),
1893 + size - sizeof(struct ip_set_req_bind),
1895 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1896 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1899 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1905 +static struct ip_set_type *
1906 +find_set_type_rlock(const char *typename)
1908 + struct ip_set_type *type;
1910 + read_lock_bh(&ip_set_lock);
1911 + type = find_set_type(typename);
1913 + read_unlock_bh(&ip_set_lock);
1919 +find_free_id(const char *name,
1920 + ip_set_id_t *index,
1925 + *id = IP_SET_INVALID_ID;
1926 + for (i = 0; i < ip_set_max; i++) {
1927 + if (ip_set_list[i] == NULL) {
1928 + if (*id == IP_SET_INVALID_ID)
1930 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1934 + if (*id == IP_SET_INVALID_ID)
1935 + /* No free slot remained */
1937 + /* Check that index is usable as id (swapping) */
1939 + for (i = 0; i < ip_set_max; i++) {
1940 + if (ip_set_list[i] != NULL
1941 + && ip_set_list[i]->id == *id) {
1953 +ip_set_create(const char *name,
1954 + const char *typename,
1955 + ip_set_id_t restore,
1959 + struct ip_set *set;
1960 + ip_set_id_t index = 0, id;
1963 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1965 + * First, and without any locks, allocate and initialize
1966 + * a normal base set structure.
1968 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1971 + set->lock = RW_LOCK_UNLOCKED;
1972 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1973 + set->binding = IP_SET_INVALID_ID;
1974 + atomic_set(&set->ref, 0);
1977 + * Next, take the &ip_set_lock, check that we know the type,
1978 + * and take a reference on the type, to make sure it
1979 + * stays available while constructing our new set.
1981 + * After referencing the type, we drop the &ip_set_lock,
1982 + * and let the new set construction run without locks.
1984 + set->type = find_set_type_rlock(typename);
1985 + if (set->type == NULL) {
1986 + /* Try loading the module */
1987 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1988 + strcpy(modulename, "ip_set_");
1989 + strcat(modulename, typename);
1990 + DP("try to load %s", modulename);
1991 + request_module(modulename);
1992 + set->type = find_set_type_rlock(typename);
1994 + if (set->type == NULL) {
1995 + ip_set_printk("no set type '%s', set '%s' not created",
2000 + if (!try_module_get(set->type->me)) {
2001 + read_unlock_bh(&ip_set_lock);
2005 + read_unlock_bh(&ip_set_lock);
2008 + * Without holding any locks, create private part.
2010 + res = set->type->create(set, data, size);
2014 + /* BTW, res==0 here. */
2017 + * Here, we have a valid, constructed set. &ip_set_lock again,
2018 + * find free id/index and check that it is not already in
2021 + write_lock_bh(&ip_set_lock);
2022 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
2023 + DP("no free id!");
2027 + /* Make sure restore gets the same index */
2028 + if (restore != IP_SET_INVALID_ID && index != restore) {
2029 + DP("Can't restore, sets are screwed up");
2035 + * Finally! Add our shiny new set to the list, and be done.
2037 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2039 + ip_set_list[index] = set;
2040 + write_unlock_bh(&ip_set_lock);
2044 + write_unlock_bh(&ip_set_lock);
2045 + set->type->destroy(set);
2047 + module_put(set->type->me);
2054 + * Destroy a given existing set
2057 +ip_set_destroy_set(ip_set_id_t index)
2059 + struct ip_set *set = ip_set_list[index];
2061 + IP_SET_ASSERT(set);
2062 + DP("set: %s", set->name);
2063 + write_lock_bh(&ip_set_lock);
2064 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2065 + if (set->binding != IP_SET_INVALID_ID)
2066 + __ip_set_put(set->binding);
2067 + ip_set_list[index] = NULL;
2068 + write_unlock_bh(&ip_set_lock);
2070 + /* Must call it without holding any lock */
2071 + set->type->destroy(set);
2072 + module_put(set->type->me);
2077 + * Destroy a set - or all sets
2078 + * Sets must not be referenced/used.
2081 +ip_set_destroy(ip_set_id_t index)
2085 + /* ref modification always protected by the mutex */
2086 + if (index != IP_SET_INVALID_ID) {
2087 + if (atomic_read(&ip_set_list[index]->ref))
2089 + ip_set_destroy_set(index);
2091 + for (i = 0; i < ip_set_max; i++) {
2092 + if (ip_set_list[i] != NULL
2093 + && (atomic_read(&ip_set_list[i]->ref)))
2097 + for (i = 0; i < ip_set_max; i++) {
2098 + if (ip_set_list[i] != NULL)
2099 + ip_set_destroy_set(i);
2106 +ip_set_flush_set(struct ip_set *set)
2108 + DP("set: %s %u", set->name, set->id);
2110 + write_lock_bh(&set->lock);
2111 + set->type->flush(set);
2112 + write_unlock_bh(&set->lock);
2116 + * Flush data in a set - or in all sets
2119 +ip_set_flush(ip_set_id_t index)
2121 + if (index != IP_SET_INVALID_ID) {
2122 + IP_SET_ASSERT(ip_set_list[index]);
2123 + ip_set_flush_set(ip_set_list[index]);
2125 + FOREACH_SET_DO(ip_set_flush_set);
2132 +ip_set_rename(ip_set_id_t index, const char *name)
2134 + struct ip_set *set = ip_set_list[index];
2138 + DP("set: %s to %s", set->name, name);
2139 + write_lock_bh(&ip_set_lock);
2140 + for (i = 0; i < ip_set_max; i++) {
2141 + if (ip_set_list[i] != NULL
2142 + && strncmp(ip_set_list[i]->name,
2144 + IP_SET_MAXNAMELEN - 1) == 0) {
2149 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2151 + write_unlock_bh(&ip_set_lock);
2156 + * Swap two sets so that name/index points to the other.
2157 + * References are also swapped.
2160 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2162 + struct ip_set *from = ip_set_list[from_index];
2163 + struct ip_set *to = ip_set_list[to_index];
2164 + char from_name[IP_SET_MAXNAMELEN];
2165 + u_int32_t from_ref;
2167 + DP("set: %s to %s", from->name, to->name);
2168 + /* Features must not change. Artifical restriction. */
2169 + if (from->type->features != to->type->features)
2172 + /* No magic here: ref munging protected by the mutex */
2173 + write_lock_bh(&ip_set_lock);
2174 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2175 + from_ref = atomic_read(&from->ref);
2177 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2178 + atomic_set(&from->ref, atomic_read(&to->ref));
2179 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2180 + atomic_set(&to->ref, from_ref);
2182 + ip_set_list[from_index] = to;
2183 + ip_set_list[to_index] = from;
2185 + write_unlock_bh(&ip_set_lock);
2194 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2195 + ip_set_id_t id, size_t *size)
2197 + if (set_hash->id == id)
2198 + *size += sizeof(struct ip_set_hash_list);
2202 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2203 + ip_set_id_t id, size_t *size)
2205 + if (set_hash->id == id)
2206 + *size += sizeof(struct ip_set_hash_save);
2210 +__set_hash_bindings(struct ip_set_hash *set_hash,
2211 + ip_set_id_t id, void *data, int *used)
2213 + if (set_hash->id == id) {
2214 + struct ip_set_hash_list *hash_list =
2215 + (struct ip_set_hash_list *)(data + *used);
2217 + hash_list->ip = set_hash->ip;
2218 + hash_list->binding = set_hash->binding;
2219 + *used += sizeof(struct ip_set_hash_list);
2223 +static int ip_set_list_set(ip_set_id_t index,
2228 + struct ip_set *set = ip_set_list[index];
2229 + struct ip_set_list *set_list;
2231 + /* Pointer to our header */
2232 + set_list = (struct ip_set_list *) (data + *used);
2234 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2236 + /* Get and ensure header size */
2237 + if (*used + sizeof(struct ip_set_list) > len)
2238 + goto not_enough_mem;
2239 + *used += sizeof(struct ip_set_list);
2241 + read_lock_bh(&set->lock);
2242 + /* Get and ensure set specific header size */
2243 + set_list->header_size = set->type->header_size;
2244 + if (*used + set_list->header_size > len)
2247 + /* Fill in the header */
2248 + set_list->index = index;
2249 + set_list->binding = set->binding;
2250 + set_list->ref = atomic_read(&set->ref);
2252 + /* Fill in set spefific header data */
2253 + set->type->list_header(set, data + *used);
2254 + *used += set_list->header_size;
2256 + /* Get and ensure set specific members size */
2257 + set_list->members_size = set->type->list_members_size(set);
2258 + if (*used + set_list->members_size > len)
2261 + /* Fill in set spefific members data */
2262 + set->type->list_members(set, data + *used);
2263 + *used += set_list->members_size;
2264 + read_unlock_bh(&set->lock);
2268 + /* Get and ensure set specific bindings size */
2269 + set_list->bindings_size = 0;
2270 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2271 + set->id, &set_list->bindings_size);
2272 + if (*used + set_list->bindings_size > len)
2273 + goto not_enough_mem;
2275 + /* Fill in set spefific bindings data */
2276 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2281 + read_unlock_bh(&set->lock);
2283 + DP("not enough mem, try again");
2290 +static int ip_set_save_set(ip_set_id_t index,
2295 + struct ip_set *set;
2296 + struct ip_set_save *set_save;
2298 + /* Pointer to our header */
2299 + set_save = (struct ip_set_save *) (data + *used);
2301 + /* Get and ensure header size */
2302 + if (*used + sizeof(struct ip_set_save) > len)
2303 + goto not_enough_mem;
2304 + *used += sizeof(struct ip_set_save);
2306 + set = ip_set_list[index];
2307 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2308 + data, data + *used);
2310 + read_lock_bh(&set->lock);
2311 + /* Get and ensure set specific header size */
2312 + set_save->header_size = set->type->header_size;
2313 + if (*used + set_save->header_size > len)
2316 + /* Fill in the header */
2317 + set_save->index = index;
2318 + set_save->binding = set->binding;
2320 + /* Fill in set spefific header data */
2321 + set->type->list_header(set, data + *used);
2322 + *used += set_save->header_size;
2324 + DP("set header filled: %s, used: %u(%u) %p %p", set->name, *used,
2325 + set_save->header_size, data, data + *used);
2326 + /* Get and ensure set specific members size */
2327 + set_save->members_size = set->type->list_members_size(set);
2328 + if (*used + set_save->members_size > len)
2331 + /* Fill in set spefific members data */
2332 + set->type->list_members(set, data + *used);
2333 + *used += set_save->members_size;
2334 + read_unlock_bh(&set->lock);
2335 + DP("set members filled: %s, used: %u(%u) %p %p", set->name, *used,
2336 + set_save->members_size, data, data + *used);
2340 + read_unlock_bh(&set->lock);
2342 + DP("not enough mem, try again");
2347 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2355 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2356 + struct ip_set_hash_save *hash_save =
2357 + (struct ip_set_hash_save *)(data + *used);
2358 + /* Ensure bindings size */
2359 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2363 + hash_save->id = set_hash->id;
2364 + hash_save->ip = set_hash->ip;
2365 + hash_save->binding = set_hash->binding;
2366 + *used += sizeof(struct ip_set_hash_save);
2370 +static int ip_set_save_bindings(ip_set_id_t index,
2376 + struct ip_set_save *set_save;
2378 + DP("used %u, len %u", *used, len);
2379 + /* Get and ensure header size */
2380 + if (*used + sizeof(struct ip_set_save) > len)
2384 + set_save = (struct ip_set_save *) (data + *used);
2385 + set_save->index = IP_SET_INVALID_ID;
2386 + set_save->header_size = 0;
2387 + set_save->members_size = 0;
2388 + *used += sizeof(struct ip_set_save);
2390 + DP("marker added used %u, len %u", *used, len);
2391 + /* Fill in bindings data */
2392 + if (index != IP_SET_INVALID_ID)
2393 + /* Sets are identified by id in hash */
2394 + index = ip_set_list[index]->id;
2395 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2403 +static int ip_set_restore(void *data,
2407 + int line = 0, used = 0, members_size;
2408 + struct ip_set *set;
2409 + struct ip_set_hash_save *hash_save;
2410 + struct ip_set_restore *set_restore;
2411 + ip_set_id_t index;
2413 + /* Loop to restore sets */
2417 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2418 + /* Get and ensure header size */
2419 + if (used + sizeof(struct ip_set_restore) > len)
2421 + set_restore = (struct ip_set_restore *) (data + used);
2422 + used += sizeof(struct ip_set_restore);
2424 + /* Ensure data size */
2426 + + set_restore->header_size
2427 + + set_restore->members_size > len)
2430 + /* Check marker */
2431 + if (set_restore->index == IP_SET_INVALID_ID) {
2436 + /* Try to create the set */
2437 + DP("restore %s %s", set_restore->name, set_restore->typename);
2438 + res = ip_set_create(set_restore->name,
2439 + set_restore->typename,
2440 + set_restore->index,
2442 + set_restore->header_size);
2446 + used += set_restore->header_size;
2448 + index = ip_set_find_byindex(set_restore->index);
2449 + DP("index %u, restore_index %u", index, set_restore->index);
2450 + if (index != set_restore->index)
2452 + /* Try to restore members data */
2453 + set = ip_set_list[index];
2455 + DP("members_size %u reqsize %u",
2456 + set_restore->members_size, set->type->reqsize);
2457 + while (members_size + set->type->reqsize <=
2458 + set_restore->members_size) {
2460 + DP("members: %u, line %u", members_size, line);
2461 + res = __ip_set_addip(index,
2462 + data + used + members_size,
2463 + set->type->reqsize);
2464 + if (!(res == 0 || res == -EEXIST))
2466 + members_size += set->type->reqsize;
2469 + DP("members_size %u %u",
2470 + set_restore->members_size, members_size);
2471 + if (members_size != set_restore->members_size)
2473 + used += set_restore->members_size;
2477 + /* Loop to restore bindings */
2478 + while (used < len) {
2481 + DP("restore binding, line %u", line);
2482 + /* Get and ensure size */
2483 + if (used + sizeof(struct ip_set_hash_save) > len)
2485 + hash_save = (struct ip_set_hash_save *) (data + used);
2486 + used += sizeof(struct ip_set_hash_save);
2488 + /* hash_save->id is used to store the index */
2489 + index = ip_set_find_byindex(hash_save->id);
2490 + DP("restore binding index %u, id %u, %u -> %u",
2491 + index, hash_save->id, hash_save->ip, hash_save->binding);
2492 + if (index != hash_save->id)
2494 + if (ip_set_find_byindex(hash_save->binding) == IP_SET_INVALID_ID) {
2495 + DP("corrupt binding set index %u", hash_save->binding);
2498 + set = ip_set_list[hash_save->id];
2499 + /* Null valued IP means default binding */
2500 + if (hash_save->ip)
2501 + res = ip_set_hash_add(set->id,
2503 + hash_save->binding);
2505 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2506 + write_lock_bh(&ip_set_lock);
2507 + set->binding = hash_save->binding;
2508 + __ip_set_get(set->binding);
2509 + write_unlock_bh(&ip_set_lock);
2510 + DP("default binding: %u", set->binding);
2522 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2525 + int res = 0; /* Assume OK */
2527 + struct ip_set_req_adt *req_adt;
2528 + ip_set_id_t index = IP_SET_INVALID_ID;
2529 + int (*adtfn)(ip_set_id_t index,
2530 + const void *data, size_t size);
2532 + int (*fn)(ip_set_id_t index,
2533 + const void *data, size_t size);
2535 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2536 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2539 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2540 + if (!capable(CAP_NET_ADMIN))
2542 + if (optval != SO_IP_SET)
2544 + if (len <= sizeof(unsigned)) {
2545 + ip_set_printk("short userdata (want >%zu, got %u)",
2546 + sizeof(unsigned), len);
2549 + data = vmalloc(len);
2551 + DP("out of mem for %u bytes", len);
2554 + if (copy_from_user(data, user, len) != 0) {
2558 + if (down_interruptible(&ip_set_app_mutex)) {
2563 + op = (unsigned *)data;
2566 + if (*op < IP_SET_OP_VERSION) {
2567 + /* Check the version at the beginning of operations */
2568 + struct ip_set_req_version *req_version =
2569 + (struct ip_set_req_version *) data;
2570 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2577 + case IP_SET_OP_CREATE:{
2578 + struct ip_set_req_create *req_create
2579 + = (struct ip_set_req_create *) data;
2581 + if (len < sizeof(struct ip_set_req_create)) {
2582 + ip_set_printk("short CREATE data (want >=%zu, got %u)",
2583 + sizeof(struct ip_set_req_create), len);
2587 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2588 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2589 + res = ip_set_create(req_create->name,
2590 + req_create->typename,
2591 + IP_SET_INVALID_ID,
2592 + data + sizeof(struct ip_set_req_create),
2593 + len - sizeof(struct ip_set_req_create));
2596 + case IP_SET_OP_DESTROY:{
2597 + struct ip_set_req_std *req_destroy
2598 + = (struct ip_set_req_std *) data;
2600 + if (len != sizeof(struct ip_set_req_std)) {
2601 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2602 + sizeof(struct ip_set_req_std), len);
2606 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2607 + /* Destroy all sets */
2608 + index = IP_SET_INVALID_ID;
2610 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2611 + index = ip_set_find_byname(req_destroy->name);
2613 + if (index == IP_SET_INVALID_ID) {
2619 + res = ip_set_destroy(index);
2622 + case IP_SET_OP_FLUSH:{
2623 + struct ip_set_req_std *req_flush =
2624 + (struct ip_set_req_std *) data;
2626 + if (len != sizeof(struct ip_set_req_std)) {
2627 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2628 + sizeof(struct ip_set_req_std), len);
2632 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2633 + /* Flush all sets */
2634 + index = IP_SET_INVALID_ID;
2636 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2637 + index = ip_set_find_byname(req_flush->name);
2639 + if (index == IP_SET_INVALID_ID) {
2644 + res = ip_set_flush(index);
2647 + case IP_SET_OP_RENAME:{
2648 + struct ip_set_req_create *req_rename
2649 + = (struct ip_set_req_create *) data;
2651 + if (len != sizeof(struct ip_set_req_create)) {
2652 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2653 + sizeof(struct ip_set_req_create), len);
2658 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2659 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2661 + index = ip_set_find_byname(req_rename->name);
2662 + if (index == IP_SET_INVALID_ID) {
2666 + res = ip_set_rename(index, req_rename->typename);
2669 + case IP_SET_OP_SWAP:{
2670 + struct ip_set_req_create *req_swap
2671 + = (struct ip_set_req_create *) data;
2672 + ip_set_id_t to_index;
2674 + if (len != sizeof(struct ip_set_req_create)) {
2675 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2676 + sizeof(struct ip_set_req_create), len);
2681 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2682 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2684 + index = ip_set_find_byname(req_swap->name);
2685 + if (index == IP_SET_INVALID_ID) {
2689 + to_index = ip_set_find_byname(req_swap->typename);
2690 + if (to_index == IP_SET_INVALID_ID) {
2694 + res = ip_set_swap(index, to_index);
2698 + break; /* Set identified by id */
2701 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2702 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2706 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2708 + if (len < sizeof(struct ip_set_req_adt)) {
2709 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2710 + sizeof(struct ip_set_req_adt), len);
2714 + req_adt = (struct ip_set_req_adt *) data;
2716 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2717 + if (!(*op == IP_SET_OP_UNBIND_SET
2718 + && req_adt->index == IP_SET_INVALID_ID)) {
2719 + index = ip_set_find_byindex(req_adt->index);
2720 + if (index == IP_SET_INVALID_ID) {
2725 + res = adtfn(index, data, len);
2728 + up(&ip_set_app_mutex);
2732 + DP("final result %d", res);
2737 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2741 + ip_set_id_t index = IP_SET_INVALID_ID;
2743 + int copylen = *len;
2745 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2746 + if (!capable(CAP_NET_ADMIN))
2748 + if (optval != SO_IP_SET)
2750 + if (*len < sizeof(unsigned)) {
2751 + ip_set_printk("short userdata (want >=%zu, got %d)",
2752 + sizeof(unsigned), *len);
2755 + data = vmalloc(*len);
2757 + DP("out of mem for %d bytes", *len);
2760 + if (copy_from_user(data, user, *len) != 0) {
2764 + if (down_interruptible(&ip_set_app_mutex)) {
2769 + op = (unsigned *) data;
2772 + if (*op < IP_SET_OP_VERSION) {
2773 + /* Check the version at the beginning of operations */
2774 + struct ip_set_req_version *req_version =
2775 + (struct ip_set_req_version *) data;
2776 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2783 + case IP_SET_OP_VERSION: {
2784 + struct ip_set_req_version *req_version =
2785 + (struct ip_set_req_version *) data;
2787 + if (*len != sizeof(struct ip_set_req_version)) {
2788 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2789 + sizeof(struct ip_set_req_version),
2795 + req_version->version = IP_SET_PROTOCOL_VERSION;
2796 + res = copy_to_user(user, req_version,
2797 + sizeof(struct ip_set_req_version));
2800 + case IP_SET_OP_GET_BYNAME: {
2801 + struct ip_set_req_get_set *req_get
2802 + = (struct ip_set_req_get_set *) data;
2804 + if (*len != sizeof(struct ip_set_req_get_set)) {
2805 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2806 + sizeof(struct ip_set_req_get_set), *len);
2810 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2811 + index = ip_set_find_byname(req_get->set.name);
2812 + req_get->set.index = index;
2815 + case IP_SET_OP_GET_BYINDEX: {
2816 + struct ip_set_req_get_set *req_get
2817 + = (struct ip_set_req_get_set *) data;
2819 + if (*len != sizeof(struct ip_set_req_get_set)) {
2820 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2821 + sizeof(struct ip_set_req_get_set), *len);
2825 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2826 + index = ip_set_find_byindex(req_get->set.index);
2827 + strncpy(req_get->set.name,
2828 + index == IP_SET_INVALID_ID ? ""
2829 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2832 + case IP_SET_OP_ADT_GET: {
2833 + struct ip_set_req_adt_get *req_get
2834 + = (struct ip_set_req_adt_get *) data;
2836 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2837 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2838 + sizeof(struct ip_set_req_adt_get), *len);
2842 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2843 + index = ip_set_find_byname(req_get->set.name);
2844 + if (index != IP_SET_INVALID_ID) {
2845 + req_get->set.index = index;
2846 + strncpy(req_get->typename,
2847 + ip_set_list[index]->type->typename,
2848 + IP_SET_MAXNAMELEN - 1);
2855 + case IP_SET_OP_MAX_SETS: {
2856 + struct ip_set_req_max_sets *req_max_sets
2857 + = (struct ip_set_req_max_sets *) data;
2860 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2861 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2862 + sizeof(struct ip_set_req_max_sets), *len);
2867 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2868 + req_max_sets->set.index = IP_SET_INVALID_ID;
2870 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2871 + req_max_sets->set.index =
2872 + ip_set_find_byname(req_max_sets->set.name);
2873 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2878 + req_max_sets->max_sets = ip_set_max;
2879 + req_max_sets->sets = 0;
2880 + for (i = 0; i < ip_set_max; i++) {
2881 + if (ip_set_list[i] != NULL)
2882 + req_max_sets->sets++;
2886 + case IP_SET_OP_LIST_SIZE:
2887 + case IP_SET_OP_SAVE_SIZE: {
2888 + struct ip_set_req_setnames *req_setnames
2889 + = (struct ip_set_req_setnames *) data;
2890 + struct ip_set_name_list *name_list;
2891 + struct ip_set *set;
2895 + if (*len < sizeof(struct ip_set_req_setnames)) {
2896 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2897 + sizeof(struct ip_set_req_setnames), *len);
2902 + req_setnames->size = 0;
2903 + used = sizeof(struct ip_set_req_setnames);
2904 + for (i = 0; i < ip_set_max; i++) {
2905 + if (ip_set_list[i] == NULL)
2907 + name_list = (struct ip_set_name_list *)
2909 + used += sizeof(struct ip_set_name_list);
2910 + if (used > copylen) {
2914 + set = ip_set_list[i];
2915 + /* Fill in index, name, etc. */
2916 + name_list->index = i;
2917 + name_list->id = set->id;
2918 + strncpy(name_list->name,
2920 + IP_SET_MAXNAMELEN - 1);
2921 + strncpy(name_list->typename,
2922 + set->type->typename,
2923 + IP_SET_MAXNAMELEN - 1);
2924 + DP("filled %s of type %s, index %u\n",
2925 + name_list->name, name_list->typename,
2926 + name_list->index);
2927 + if (!(req_setnames->index == IP_SET_INVALID_ID
2928 + || req_setnames->index == i))
2932 + case IP_SET_OP_LIST_SIZE: {
2933 + req_setnames->size += sizeof(struct ip_set_list)
2934 + + set->type->header_size
2935 + + set->type->list_members_size(set);
2936 + /* Sets are identified by id in the hash */
2937 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2938 + set->id, &req_setnames->size);
2941 + case IP_SET_OP_SAVE_SIZE: {
2942 + req_setnames->size += sizeof(struct ip_set_save)
2943 + + set->type->header_size
2944 + + set->type->list_members_size(set);
2945 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2946 + set->id, &req_setnames->size);
2953 + if (copylen != used) {
2959 + case IP_SET_OP_LIST: {
2960 + struct ip_set_req_list *req_list
2961 + = (struct ip_set_req_list *) data;
2965 + if (*len < sizeof(struct ip_set_req_list)) {
2966 + ip_set_printk("short LIST (want >=%zu, got %d)",
2967 + sizeof(struct ip_set_req_list), *len);
2971 + index = req_list->index;
2972 + if (index != IP_SET_INVALID_ID
2973 + && ip_set_find_byindex(index) != index) {
2978 + if (index == IP_SET_INVALID_ID) {
2979 + /* List all sets */
2980 + for (i = 0; i < ip_set_max && res == 0; i++) {
2981 + if (ip_set_list[i] != NULL)
2982 + res = ip_set_list_set(i, data, &used, *len);
2985 + /* List an individual set */
2986 + res = ip_set_list_set(index, data, &used, *len);
2990 + else if (copylen != used) {
2996 + case IP_SET_OP_SAVE: {
2997 + struct ip_set_req_list *req_save
2998 + = (struct ip_set_req_list *) data;
3002 + if (*len < sizeof(struct ip_set_req_list)) {
3003 + ip_set_printk("short SAVE (want >=%zu, got %d)",
3004 + sizeof(struct ip_set_req_list), *len);
3008 + index = req_save->index;
3009 + if (index != IP_SET_INVALID_ID
3010 + && ip_set_find_byindex(index) != index) {
3015 + if (index == IP_SET_INVALID_ID) {
3016 + /* Save all sets */
3017 + for (i = 0; i < ip_set_max && res == 0; i++) {
3018 + if (ip_set_list[i] != NULL)
3019 + res = ip_set_save_set(i, data, &used, *len);
3022 + /* Save an individual set */
3023 + res = ip_set_save_set(index, data, &used, *len);
3026 + res = ip_set_save_bindings(index, data, &used, *len);
3030 + else if (copylen != used) {
3036 + case IP_SET_OP_RESTORE: {
3037 + struct ip_set_req_setnames *req_restore
3038 + = (struct ip_set_req_setnames *) data;
3041 + if (*len < sizeof(struct ip_set_req_setnames)
3042 + || *len != req_restore->size) {
3043 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3044 + req_restore->size, *len);
3048 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3049 + req_restore->size - sizeof(struct ip_set_req_setnames));
3050 + DP("ip_set_restore: %u", line);
3053 + req_restore->size = line;
3054 + copylen = sizeof(struct ip_set_req_setnames);
3062 + } /* end of switch(op) */
3065 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3066 + && ip_set_list[index]
3067 + ? ip_set_list[index]->name
3068 + : ":all:", copylen);
3069 + res = copy_to_user(user, data, copylen);
3072 + up(&ip_set_app_mutex);
3076 + DP("final result %d", res);
3080 +static struct nf_sockopt_ops so_set = {
3082 + .set_optmin = SO_IP_SET,
3083 + .set_optmax = SO_IP_SET + 1,
3084 + .set = &ip_set_sockfn_set,
3085 + .get_optmin = SO_IP_SET,
3086 + .get_optmax = SO_IP_SET + 1,
3087 + .get = &ip_set_sockfn_get,
3088 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
3089 + .owner = THIS_MODULE,
3093 +static int max_sets, hash_size;
3094 +module_param(max_sets, int, 0600);
3095 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3096 +module_param(hash_size, int, 0600);
3097 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3098 +MODULE_LICENSE("GPL");
3099 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3100 +MODULE_DESCRIPTION("module implementing core IP set support");
3102 +static int __init ip_set_init(void)
3107 + get_random_bytes(&ip_set_hash_random, 4);
3109 + ip_set_max = max_sets;
3110 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3111 + if (!ip_set_list) {
3112 + printk(KERN_ERR "Unable to create ip_set_list\n");
3115 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3117 + ip_set_bindings_hash_size = hash_size;
3118 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3119 + if (!ip_set_hash) {
3120 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3121 + vfree(ip_set_list);
3124 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3125 + INIT_LIST_HEAD(&ip_set_hash[i]);
3127 + INIT_LIST_HEAD(&set_type_list);
3129 + res = nf_register_sockopt(&so_set);
3131 + ip_set_printk("SO_SET registry failed: %d", res);
3132 + vfree(ip_set_list);
3133 + vfree(ip_set_hash);
3139 +static void __exit ip_set_fini(void)
3141 + /* There can't be any existing set or binding */
3142 + nf_unregister_sockopt(&so_set);
3143 + vfree(ip_set_list);
3144 + vfree(ip_set_hash);
3145 + DP("these are the famous last words");
3148 +EXPORT_SYMBOL(ip_set_register_set_type);
3149 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3151 +EXPORT_SYMBOL(ip_set_get_byname);
3152 +EXPORT_SYMBOL(ip_set_get_byindex);
3153 +EXPORT_SYMBOL(ip_set_put);
3155 +EXPORT_SYMBOL(ip_set_addip_kernel);
3156 +EXPORT_SYMBOL(ip_set_delip_kernel);
3157 +EXPORT_SYMBOL(ip_set_testip_kernel);
3159 +module_init(ip_set_init);
3160 +module_exit(ip_set_fini);
3162 +++ b/net/ipv4/netfilter/ip_set_iphash.c
3164 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3166 + * This program is free software; you can redistribute it and/or modify
3167 + * it under the terms of the GNU General Public License version 2 as
3168 + * published by the Free Software Foundation.
3171 +/* Kernel module implementing an ip hash set */
3173 +#include <linux/module.h>
3174 +#include <linux/ip.h>
3175 +#include <linux/skbuff.h>
3176 +#include <linux/version.h>
3177 +#include <linux/jhash.h>
3178 +#include <linux/netfilter_ipv4/ip_tables.h>
3179 +#include <linux/netfilter_ipv4/ip_set.h>
3180 +#include <linux/errno.h>
3181 +#include <asm/uaccess.h>
3182 +#include <asm/bitops.h>
3183 +#include <linux/spinlock.h>
3184 +#include <linux/vmalloc.h>
3185 +#include <linux/random.h>
3187 +#include <net/ip.h>
3189 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3190 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3192 +static int limit = MAX_RANGE;
3194 +static inline __u32
3195 +jhash_ip(const struct ip_set_iphash *map, uint16_t i, ip_set_ip_t ip)
3197 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
3200 +static inline __u32
3201 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3203 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3206 + ip_set_ip_t *elem;
3208 + *hash_ip = ip & map->netmask;
3209 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3210 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3212 + for (i = 0; i < map->probes; i++) {
3213 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
3214 + DP("hash key: %u", id);
3215 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3216 + if (*elem == *hash_ip)
3218 + /* No shortcut at testing - there can be deleted
3225 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3227 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
3231 +testip(struct ip_set *set, const void *data, size_t size,
3232 + ip_set_ip_t *hash_ip)
3234 + struct ip_set_req_iphash *req =
3235 + (struct ip_set_req_iphash *) data;
3237 + if (size != sizeof(struct ip_set_req_iphash)) {
3238 + ip_set_printk("data length wrong (want %zu, have %zu)",
3239 + sizeof(struct ip_set_req_iphash),
3243 + return __testip(set, req->ip, hash_ip);
3247 +testip_kernel(struct ip_set *set,
3248 + const struct sk_buff *skb,
3249 + ip_set_ip_t *hash_ip,
3250 + const u_int32_t *flags,
3251 + unsigned char index)
3253 + return __testip(set,
3254 + ntohl(flags[index] & IPSET_SRC
3255 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3256 + ? ip_hdr(skb)->saddr
3257 + : ip_hdr(skb)->daddr),
3259 + ? skb->nh.iph->saddr
3260 + : skb->nh.iph->daddr),
3266 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3270 + ip_set_ip_t *elem;
3272 + if (!ip || map->elements >= limit)
3275 + *hash_ip = ip & map->netmask;
3277 + for (i = 0; i < map->probes; i++) {
3278 + probe = jhash_ip(map, i, *hash_ip) % map->hashsize;
3279 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
3280 + if (*elem == *hash_ip)
3288 + /* Trigger rehashing */
3293 +addip(struct ip_set *set, const void *data, size_t size,
3294 + ip_set_ip_t *hash_ip)
3296 + struct ip_set_req_iphash *req =
3297 + (struct ip_set_req_iphash *) data;
3299 + if (size != sizeof(struct ip_set_req_iphash)) {
3300 + ip_set_printk("data length wrong (want %zu, have %zu)",
3301 + sizeof(struct ip_set_req_iphash),
3305 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3309 +addip_kernel(struct ip_set *set,
3310 + const struct sk_buff *skb,
3311 + ip_set_ip_t *hash_ip,
3312 + const u_int32_t *flags,
3313 + unsigned char index)
3315 + return __addip((struct ip_set_iphash *) set->data,
3316 + ntohl(flags[index] & IPSET_SRC
3317 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3318 + ? ip_hdr(skb)->saddr
3319 + : ip_hdr(skb)->daddr),
3321 + ? skb->nh.iph->saddr
3322 + : skb->nh.iph->daddr),
3327 +static int retry(struct ip_set *set)
3329 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3330 + ip_set_ip_t hash_ip, *elem;
3332 + u_int32_t i, hashsize = map->hashsize;
3334 + struct ip_set_iphash *tmp;
3336 + if (map->resize == 0)
3342 + /* Calculate new hash size */
3343 + hashsize += (hashsize * map->resize)/100;
3344 + if (hashsize == map->hashsize)
3347 + ip_set_printk("rehashing of set %s triggered: "
3348 + "hashsize grows from %u to %u",
3349 + set->name, map->hashsize, hashsize);
3351 + tmp = kmalloc(sizeof(struct ip_set_iphash)
3352 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
3354 + DP("out of memory for %d bytes",
3355 + sizeof(struct ip_set_iphash)
3356 + + map->probes * sizeof(uint32_t));
3359 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
3360 + if (!tmp->members) {
3361 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
3365 + tmp->hashsize = hashsize;
3366 + tmp->elements = 0;
3367 + tmp->probes = map->probes;
3368 + tmp->resize = map->resize;
3369 + tmp->netmask = map->netmask;
3370 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
3372 + write_lock_bh(&set->lock);
3373 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3374 + for (i = 0; i < map->hashsize && res == 0; i++) {
3375 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3377 + res = __addip(tmp, *elem, &hash_ip);
3380 + /* Failure, try again */
3381 + write_unlock_bh(&set->lock);
3382 + harray_free(tmp->members);
3387 + /* Success at resizing! */
3388 + members = map->members;
3390 + map->hashsize = tmp->hashsize;
3391 + map->members = tmp->members;
3392 + write_unlock_bh(&set->lock);
3394 + harray_free(members);
3401 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3403 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3404 + ip_set_ip_t id, *elem;
3409 + id = hash_id(set, ip, hash_ip);
3410 + if (id == UINT_MAX)
3413 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);