1 From 810b98f82c5999118c2f74d5a22c7f0836771c7b Mon Sep 17 00:00:00 2001
2 From: S.Çağlar Onur <caglar@cs.princeton.edu>
3 Date: Tue, 7 Dec 2010 11:05:31 -0500
4 Subject: [PATCH] linux-2.6-250-ipsets.patch
7 include/linux/netfilter_ipv4/ip_set.h | 498 ++++++
8 include/linux/netfilter_ipv4/ip_set_iphash.h | 30 +
9 include/linux/netfilter_ipv4/ip_set_ipmap.h | 56 +
10 include/linux/netfilter_ipv4/ip_set_ipporthash.h | 34 +
11 include/linux/netfilter_ipv4/ip_set_iptree.h | 40 +
12 include/linux/netfilter_ipv4/ip_set_iptreemap.h | 40 +
13 include/linux/netfilter_ipv4/ip_set_jhash.h | 148 ++
14 include/linux/netfilter_ipv4/ip_set_macipmap.h | 38 +
15 include/linux/netfilter_ipv4/ip_set_malloc.h | 116 ++
16 include/linux/netfilter_ipv4/ip_set_nethash.h | 55 +
17 include/linux/netfilter_ipv4/ip_set_portmap.h | 25 +
18 include/linux/netfilter_ipv4/ipt_set.h | 21 +
19 net/ipv4/netfilter/Kconfig | 117 ++
20 net/ipv4/netfilter/Makefile | 13 +
21 net/ipv4/netfilter/ip_set.c | 2005 ++++++++++++++++++++++
22 net/ipv4/netfilter/ip_set_iphash.c | 429 +++++
23 net/ipv4/netfilter/ip_set_ipmap.c | 336 ++++
24 net/ipv4/netfilter/ip_set_ipporthash.c | 581 +++++++
25 net/ipv4/netfilter/ip_set_iptree.c | 612 +++++++
26 net/ipv4/netfilter/ip_set_iptreemap.c | 829 +++++++++
27 net/ipv4/netfilter/ip_set_macipmap.c | 375 ++++
28 net/ipv4/netfilter/ip_set_nethash.c | 497 ++++++
29 net/ipv4/netfilter/ip_set_portmap.c | 346 ++++
30 net/ipv4/netfilter/ipt_SET.c | 162 ++
31 net/ipv4/netfilter/ipt_set.c | 160 ++
32 25 files changed, 7563 insertions(+), 0 deletions(-)
33 create mode 100644 include/linux/netfilter_ipv4/ip_set.h
34 create mode 100644 include/linux/netfilter_ipv4/ip_set_iphash.h
35 create mode 100644 include/linux/netfilter_ipv4/ip_set_ipmap.h
36 create mode 100644 include/linux/netfilter_ipv4/ip_set_ipporthash.h
37 create mode 100644 include/linux/netfilter_ipv4/ip_set_iptree.h
38 create mode 100644 include/linux/netfilter_ipv4/ip_set_iptreemap.h
39 create mode 100644 include/linux/netfilter_ipv4/ip_set_jhash.h
40 create mode 100644 include/linux/netfilter_ipv4/ip_set_macipmap.h
41 create mode 100644 include/linux/netfilter_ipv4/ip_set_malloc.h
42 create mode 100644 include/linux/netfilter_ipv4/ip_set_nethash.h
43 create mode 100644 include/linux/netfilter_ipv4/ip_set_portmap.h
44 create mode 100644 include/linux/netfilter_ipv4/ipt_set.h
45 create mode 100644 net/ipv4/netfilter/ip_set.c
46 create mode 100644 net/ipv4/netfilter/ip_set_iphash.c
47 create mode 100644 net/ipv4/netfilter/ip_set_ipmap.c
48 create mode 100644 net/ipv4/netfilter/ip_set_ipporthash.c
49 create mode 100644 net/ipv4/netfilter/ip_set_iptree.c
50 create mode 100644 net/ipv4/netfilter/ip_set_iptreemap.c
51 create mode 100644 net/ipv4/netfilter/ip_set_macipmap.c
52 create mode 100644 net/ipv4/netfilter/ip_set_nethash.c
53 create mode 100644 net/ipv4/netfilter/ip_set_portmap.c
54 create mode 100644 net/ipv4/netfilter/ipt_SET.c
55 create mode 100644 net/ipv4/netfilter/ipt_set.c
57 diff --git a/include/linux/netfilter_ipv4/ip_set.h b/include/linux/netfilter_ipv4/ip_set.h
59 index 0000000..92a746e
61 +++ b/include/linux/netfilter_ipv4/ip_set.h
66 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
67 + * Patrick Schaaf <bof@bof.de>
68 + * Martin Josefsson <gandalf@wlug.westbo.se>
69 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
71 + * This program is free software; you can redistribute it and/or modify
72 + * it under the terms of the GNU General Public License version 2 as
73 + * published by the Free Software Foundation.
81 + * A sockopt of such quality has hardly ever been seen before on the open
82 + * market! This little beauty, hardly ever used: above 64, so it's
83 + * traditionally used for firewalling, not touched (even once!) by the
84 + * 2.0, 2.2 and 2.4 kernels!
86 + * Comes with its own certificate of authenticity, valid anywhere in the
94 + * Heavily modify by Joakim Axelsson 08.03.2002
95 + * - Made it more modulebased
97 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
99 + * - in order to "deal with" backward compatibility, renamed to ipset
103 + * Used so that the kernel module and ipset-binary can match their versions
105 +#define IP_SET_PROTOCOL_VERSION 2
107 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
109 +/* Lets work with our own typedef for representing an IP address.
110 + * We hope to make the code more portable, possibly to IPv6...
112 + * The representation works in HOST byte order, because most set types
113 + * will perform arithmetic operations and compare operations.
115 + * For now the type is an uint32_t.
117 + * Make sure to ONLY use the functions when translating and parsing
118 + * in order to keep the host byte order and make it more portable:
121 + * parse_ipandmask()
123 + * (Joakim: where are they???)
126 +typedef uint32_t ip_set_ip_t;
128 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
129 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
131 +typedef uint16_t ip_set_id_t;
133 +#define IP_SET_INVALID_ID 65535
135 +/* How deep we follow bindings */
136 +#define IP_SET_MAX_BINDINGS 6
139 + * Option flags for kernel operations (ipt_set_info)
141 +#define IPSET_SRC 0x01 /* Source match/add */
142 +#define IPSET_DST 0x02 /* Destination match/add */
143 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
148 +#define IPSET_TYPE_IP 0x01 /* IP address type of set */
149 +#define IPSET_TYPE_PORT 0x02 /* Port type of set */
150 +#define IPSET_DATA_SINGLE 0x04 /* Single data storage */
151 +#define IPSET_DATA_DOUBLE 0x08 /* Double data storage */
153 +/* Reserved keywords */
154 +#define IPSET_TOKEN_DEFAULT ":default:"
155 +#define IPSET_TOKEN_ALL ":all:"
157 +/* SO_IP_SET operation constants, and their request struct types.
160 + * 0-99: commands with version checking
161 + * 100-199: add/del/test/bind/unbind
162 + * 200-299: list, save, restore
165 +/* Single shot operations:
166 + * version, create, destroy, flush, rename and swap
168 + * Sets are identified by name.
171 +#define IP_SET_REQ_STD \
173 + unsigned version; \
174 + char name[IP_SET_MAXNAMELEN]
176 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
177 +struct ip_set_req_create {
179 + char typename[IP_SET_MAXNAMELEN];
182 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
183 +struct ip_set_req_std {
187 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
188 +/* Uses ip_set_req_std */
190 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
191 +/* Uses ip_set_req_create */
193 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
194 +/* Uses ip_set_req_create */
196 +union ip_set_name_index {
197 + char name[IP_SET_MAXNAMELEN];
201 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
202 +struct ip_set_req_get_set {
205 + union ip_set_name_index set;
208 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
209 +/* Uses ip_set_req_get_set */
211 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
212 +struct ip_set_req_version {
217 +/* Double shots operations:
218 + * add, del, test, bind and unbind.
220 + * First we query the kernel to get the index and type of the target set,
221 + * then issue the command. Validity of IP is checked in kernel in order
222 + * to minimalize sockopt operations.
225 +/* Get minimal set data for add/del/test/bind/unbind IP */
226 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
227 +struct ip_set_req_adt_get {
230 + union ip_set_name_index set;
231 + char typename[IP_SET_MAXNAMELEN];
234 +#define IP_SET_REQ_BYINDEX \
238 +struct ip_set_req_adt {
239 + IP_SET_REQ_BYINDEX;
242 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
243 +/* Uses ip_set_req_adt, with type specific addage */
245 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
246 +/* Uses ip_set_req_adt, with type specific addage */
248 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
249 +/* Uses ip_set_req_adt, with type specific addage */
251 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
252 +/* Uses ip_set_req_bind, with type specific addage */
253 +struct ip_set_req_bind {
254 + IP_SET_REQ_BYINDEX;
255 + char binding[IP_SET_MAXNAMELEN];
258 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
259 +/* Uses ip_set_req_bind, with type speficic addage
260 + * index = 0 means unbinding for all sets */
262 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
263 +/* Uses ip_set_req_bind, with type specific addage */
265 +/* Multiple shots operations: list, save, restore.
267 + * - check kernel version and query the max number of sets
268 + * - get the basic information on all sets
269 + * and size required for the next step
270 + * - get actual set data: header, data, bindings
273 +/* Get max_sets and the index of a queried set
275 +#define IP_SET_OP_MAX_SETS 0x00000020
276 +struct ip_set_req_max_sets {
279 + ip_set_id_t max_sets; /* max_sets */
280 + ip_set_id_t sets; /* real number of sets */
281 + union ip_set_name_index set; /* index of set if name used */
284 +/* Get the id and name of the sets plus size for next step */
285 +#define IP_SET_OP_LIST_SIZE 0x00000201
286 +#define IP_SET_OP_SAVE_SIZE 0x00000202
287 +struct ip_set_req_setnames {
289 + ip_set_id_t index; /* set to list/save */
290 + size_t size; /* size to get setdata/bindings */
291 + /* followed by sets number of struct ip_set_name_list */
294 +struct ip_set_name_list {
295 + char name[IP_SET_MAXNAMELEN];
296 + char typename[IP_SET_MAXNAMELEN];
301 +/* The actual list operation */
302 +#define IP_SET_OP_LIST 0x00000203
303 +struct ip_set_req_list {
304 + IP_SET_REQ_BYINDEX;
305 + /* sets number of struct ip_set_list in reply */
308 +struct ip_set_list {
310 + ip_set_id_t binding;
312 + size_t header_size; /* Set header data of header_size */
313 + size_t members_size; /* Set members data of members_size */
314 + size_t bindings_size; /* Set bindings data of bindings_size */
317 +struct ip_set_hash_list {
319 + ip_set_id_t binding;
322 +/* The save operation */
323 +#define IP_SET_OP_SAVE 0x00000204
324 +/* Uses ip_set_req_list, in the reply replaced by
325 + * sets number of struct ip_set_save plus a marker
326 + * ip_set_save followed by ip_set_hash_save structures.
328 +struct ip_set_save {
330 + ip_set_id_t binding;
331 + size_t header_size; /* Set header data of header_size */
332 + size_t members_size; /* Set members data of members_size */
335 +/* At restoring, ip == 0 means default binding for the given set: */
336 +struct ip_set_hash_save {
339 + ip_set_id_t binding;
342 +/* The restore operation */
343 +#define IP_SET_OP_RESTORE 0x00000205
344 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
345 + * plus a marker ip_set_restore, followed by ip_set_hash_save
348 +struct ip_set_restore {
349 + char name[IP_SET_MAXNAMELEN];
350 + char typename[IP_SET_MAXNAMELEN];
352 + size_t header_size; /* Create data of header_size */
353 + size_t members_size; /* Set members data of members_size */
356 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
358 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
363 +#define ip_set_printk(format, args...) \
365 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
366 + printk(format "\n" , ## args); \
369 +#if defined(IP_SET_DEBUG)
370 +#define DP(format, args...) \
372 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
373 + printk(format "\n" , ## args); \
375 +#define IP_SET_ASSERT(x) \
378 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
379 + __FILE__, __LINE__, __FUNCTION__); \
382 +#define DP(format, args...)
383 +#define IP_SET_ASSERT(x)
389 + * The ip_set_type definition - one per set type, e.g. "ipmap".
391 + * Each individual set has a pointer, set->type, going to one
392 + * of these structures. Function pointers inside the structure implement
393 + * the real behaviour of the sets.
395 + * If not mentioned differently, the implementation behind the function
396 + * pointers of a set_type, is expected to return 0 if ok, and a negative
397 + * errno (e.g. -EINVAL) on error.
399 +struct ip_set_type {
400 + struct list_head list; /* next in list of set types */
402 + /* test for IP in set (kernel: iptables -m set src|dst)
403 + * return 0 if not in set, 1 if in set.
405 + int (*testip_kernel) (struct ip_set *set,
406 + const struct sk_buff * skb,
408 + const u_int32_t *flags,
409 + unsigned char index);
411 + /* test for IP in set (userspace: ipset -T set IP)
412 + * return 0 if not in set, 1 if in set.
414 + int (*testip) (struct ip_set *set,
415 + const void *data, size_t size,
419 + * Size of the data structure passed by when
420 + * adding/deletin/testing an entry.
424 + /* Add IP into set (userspace: ipset -A set IP)
425 + * Return -EEXIST if the address is already in the set,
426 + * and -ERANGE if the address lies outside the set bounds.
427 + * If the address was not already in the set, 0 is returned.
429 + int (*addip) (struct ip_set *set,
430 + const void *data, size_t size,
433 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
434 + * Return -EEXIST if the address is already in the set,
435 + * and -ERANGE if the address lies outside the set bounds.
436 + * If the address was not already in the set, 0 is returned.
438 + int (*addip_kernel) (struct ip_set *set,
439 + const struct sk_buff * skb,
441 + const u_int32_t *flags,
442 + unsigned char index);
444 + /* remove IP from set (userspace: ipset -D set --entry x)
445 + * Return -EEXIST if the address is NOT in the set,
446 + * and -ERANGE if the address lies outside the set bounds.
447 + * If the address really was in the set, 0 is returned.
449 + int (*delip) (struct ip_set *set,
450 + const void *data, size_t size,
453 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
454 + * Return -EEXIST if the address is NOT in the set,
455 + * and -ERANGE if the address lies outside the set bounds.
456 + * If the address really was in the set, 0 is returned.
458 + int (*delip_kernel) (struct ip_set *set,
459 + const struct sk_buff * skb,
461 + const u_int32_t *flags,
462 + unsigned char index);
464 + /* new set creation - allocated type specific items
466 + int (*create) (struct ip_set *set,
467 + const void *data, size_t size);
469 + /* retry the operation after successfully tweaking the set
471 + int (*retry) (struct ip_set *set);
473 + /* set destruction - free type specific items
474 + * There is no return value.
475 + * Can be called only when child sets are destroyed.
477 + void (*destroy) (struct ip_set *set);
479 + /* set flushing - reset all bits in the set, or something similar.
480 + * There is no return value.
482 + void (*flush) (struct ip_set *set);
484 + /* Listing: size needed for header
486 + size_t header_size;
488 + /* Listing: Get the header
490 + * Fill in the information in "data".
491 + * This function is always run after list_header_size() under a
492 + * writelock on the set. Therefor is the length of "data" always
495 + void (*list_header) (const struct ip_set *set,
498 + /* Listing: Get the size for the set members
500 + int (*list_members_size) (const struct ip_set *set);
502 + /* Listing: Get the set members
504 + * Fill in the information in "data".
505 + * This function is always run after list_member_size() under a
506 + * writelock on the set. Therefor is the length of "data" always
509 + void (*list_members) (const struct ip_set *set,
512 + char typename[IP_SET_MAXNAMELEN];
513 + unsigned char features;
514 + int protocol_version;
516 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
520 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
521 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
523 +/* A generic ipset */
525 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
526 + rwlock_t lock; /* lock for concurrency control */
527 + ip_set_id_t id; /* set id for swapping */
528 + ip_set_id_t binding; /* default binding for the set */
529 + atomic_t ref; /* in kernel and in hash references */
530 + struct ip_set_type *type; /* the set types */
531 + void *data; /* pooltype specific data */
534 +/* Structure to bind set elements to sets */
535 +struct ip_set_hash {
536 + struct list_head list; /* list of clashing entries in hash */
537 + ip_set_ip_t ip; /* ip from set */
538 + ip_set_id_t id; /* set id */
539 + ip_set_id_t binding; /* set we bind the element to */
542 +/* register and unregister set references */
543 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
544 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
545 +extern void ip_set_put(ip_set_id_t id);
547 +/* API for iptables set match, and SET target */
548 +extern void ip_set_addip_kernel(ip_set_id_t id,
549 + const struct sk_buff *skb,
550 + const u_int32_t *flags);
551 +extern void ip_set_delip_kernel(ip_set_id_t id,
552 + const struct sk_buff *skb,
553 + const u_int32_t *flags);
554 +extern int ip_set_testip_kernel(ip_set_id_t id,
555 + const struct sk_buff *skb,
556 + const u_int32_t *flags);
558 +#endif /* __KERNEL__ */
560 +#endif /*_IP_SET_H*/
561 diff --git a/include/linux/netfilter_ipv4/ip_set_iphash.h b/include/linux/netfilter_ipv4/ip_set_iphash.h
563 index 0000000..7de854b
565 +++ b/include/linux/netfilter_ipv4/ip_set_iphash.h
567 +#ifndef __IP_SET_IPHASH_H
568 +#define __IP_SET_IPHASH_H
570 +#include <linux/netfilter_ipv4/ip_set.h>
572 +#define SETTYPE_NAME "iphash"
573 +#define MAX_RANGE 0x0000FFFF
575 +struct ip_set_iphash {
576 + ip_set_ip_t *members; /* the iphash proper */
577 + uint32_t elements; /* number of elements */
578 + uint32_t hashsize; /* hash size */
579 + uint16_t probes; /* max number of probes */
580 + uint16_t resize; /* resize factor in percent */
581 + ip_set_ip_t netmask; /* netmask */
582 + void *initval[0]; /* initvals for jhash_1word */
585 +struct ip_set_req_iphash_create {
589 + ip_set_ip_t netmask;
592 +struct ip_set_req_iphash {
596 +#endif /* __IP_SET_IPHASH_H */
597 diff --git a/include/linux/netfilter_ipv4/ip_set_ipmap.h b/include/linux/netfilter_ipv4/ip_set_ipmap.h
599 index 0000000..2435102
601 +++ b/include/linux/netfilter_ipv4/ip_set_ipmap.h
603 +#ifndef __IP_SET_IPMAP_H
604 +#define __IP_SET_IPMAP_H
606 +#include <linux/netfilter_ipv4/ip_set.h>
608 +#define SETTYPE_NAME "ipmap"
609 +#define MAX_RANGE 0x0000FFFF
611 +struct ip_set_ipmap {
612 + void *members; /* the ipmap proper */
613 + ip_set_ip_t first_ip; /* host byte order, included in range */
614 + ip_set_ip_t last_ip; /* host byte order, included in range */
615 + ip_set_ip_t netmask; /* subnet netmask */
616 + ip_set_ip_t sizeid; /* size of set in IPs */
617 + ip_set_ip_t hosts; /* number of hosts in a subnet */
620 +struct ip_set_req_ipmap_create {
623 + ip_set_ip_t netmask;
626 +struct ip_set_req_ipmap {
631 +mask_to_bits(ip_set_ip_t mask)
633 + unsigned int bits = 32;
634 + ip_set_ip_t maskaddr;
636 + if (mask == 0xFFFFFFFF)
639 + maskaddr = 0xFFFFFFFE;
640 + while (--bits >= 0 && maskaddr != mask)
647 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
649 + ip_set_ip_t mask = 0xFFFFFFFE;
652 + while (--(*bits) >= 0 && mask && (to & mask) != from)
658 +#endif /* __IP_SET_IPMAP_H */
659 diff --git a/include/linux/netfilter_ipv4/ip_set_ipporthash.h b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
661 index 0000000..b715c56
663 +++ b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
665 +#ifndef __IP_SET_IPPORTHASH_H
666 +#define __IP_SET_IPPORTHASH_H
668 +#include <linux/netfilter_ipv4/ip_set.h>
670 +#define SETTYPE_NAME "ipporthash"
671 +#define MAX_RANGE 0x0000FFFF
672 +#define INVALID_PORT (MAX_RANGE + 1)
674 +struct ip_set_ipporthash {
675 + ip_set_ip_t *members; /* the ipporthash proper */
676 + uint32_t elements; /* number of elements */
677 + uint32_t hashsize; /* hash size */
678 + uint16_t probes; /* max number of probes */
679 + uint16_t resize; /* resize factor in percent */
680 + ip_set_ip_t first_ip; /* host byte order, included in range */
681 + ip_set_ip_t last_ip; /* host byte order, included in range */
682 + void *initval[0]; /* initvals for jhash_1word */
685 +struct ip_set_req_ipporthash_create {
693 +struct ip_set_req_ipporthash {
698 +#endif /* __IP_SET_IPPORTHASH_H */
699 diff --git a/include/linux/netfilter_ipv4/ip_set_iptree.h b/include/linux/netfilter_ipv4/ip_set_iptree.h
701 index 0000000..64e716b
703 +++ b/include/linux/netfilter_ipv4/ip_set_iptree.h
705 +#ifndef __IP_SET_IPTREE_H
706 +#define __IP_SET_IPTREE_H
708 +#include <linux/netfilter_ipv4/ip_set.h>
710 +#define SETTYPE_NAME "iptree"
711 +#define MAX_RANGE 0x0000FFFF
713 +struct ip_set_iptreed {
714 + unsigned long expires[256]; /* x.x.x.ADDR */
717 +struct ip_set_iptreec {
718 + struct ip_set_iptreed *tree[256]; /* x.x.ADDR.* */
721 +struct ip_set_iptreeb {
722 + struct ip_set_iptreec *tree[256]; /* x.ADDR.*.* */
725 +struct ip_set_iptree {
726 + unsigned int timeout;
727 + unsigned int gc_interval;
729 + uint32_t elements; /* number of elements */
730 + struct timer_list gc;
731 + struct ip_set_iptreeb *tree[256]; /* ADDR.*.*.* */
735 +struct ip_set_req_iptree_create {
736 + unsigned int timeout;
739 +struct ip_set_req_iptree {
741 + unsigned int timeout;
744 +#endif /* __IP_SET_IPTREE_H */
745 diff --git a/include/linux/netfilter_ipv4/ip_set_iptreemap.h b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
747 index 0000000..bef576a
749 +++ b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
751 +#ifndef __IP_SET_IPTREEMAP_H
752 +#define __IP_SET_IPTREEMAP_H
754 +#include <linux/netfilter_ipv4/ip_set.h>
756 +#define SETTYPE_NAME "iptreemap"
759 +struct ip_set_iptreemap_d {
760 + unsigned char bitmap[32]; /* x.x.x.y */
763 +struct ip_set_iptreemap_c {
764 + struct ip_set_iptreemap_d *tree[256]; /* x.x.y.x */
767 +struct ip_set_iptreemap_b {
768 + struct ip_set_iptreemap_c *tree[256]; /* x.y.x.x */
769 + unsigned char dirty[32];
773 +struct ip_set_iptreemap {
774 + unsigned int gc_interval;
776 + struct timer_list gc;
777 + struct ip_set_iptreemap_b *tree[256]; /* y.x.x.x */
781 +struct ip_set_req_iptreemap_create {
782 + unsigned int gc_interval;
785 +struct ip_set_req_iptreemap {
790 +#endif /* __IP_SET_IPTREEMAP_H */
791 diff --git a/include/linux/netfilter_ipv4/ip_set_jhash.h b/include/linux/netfilter_ipv4/ip_set_jhash.h
793 index 0000000..25c6b97
795 +++ b/include/linux/netfilter_ipv4/ip_set_jhash.h
797 +#ifndef _LINUX_IPSET_JHASH_H
798 +#define _LINUX_IPSET_JHASH_H
800 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
801 + * to __u32/__u8 so that the header file can be included into
802 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
805 +/* jhash.h: Jenkins hash support.
807 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
809 + * http://burtleburtle.net/bob/hash/
811 + * These are the credits from Bob's sources:
813 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
814 + * hash(), hash2(), hash3, and mix() are externally useful functions.
815 + * Routines to test the hash are included if SELF_TEST is defined.
816 + * You can use this free for any purpose. It has no warranty.
818 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
820 + * I've modified Bob's hash to be useful in the Linux kernel, and
821 + * any bugs present are surely my fault. -DaveM
824 +/* NOTE: Arguments are modified. */
825 +#define __jhash_mix(a, b, c) \
827 + a -= b; a -= c; a ^= (c>>13); \
828 + b -= c; b -= a; b ^= (a<<8); \
829 + c -= a; c -= b; c ^= (b>>13); \
830 + a -= b; a -= c; a ^= (c>>12); \
831 + b -= c; b -= a; b ^= (a<<16); \
832 + c -= a; c -= b; c ^= (b>>5); \
833 + a -= b; a -= c; a ^= (c>>3); \
834 + b -= c; b -= a; b ^= (a<<10); \
835 + c -= a; c -= b; c ^= (b>>15); \
838 +/* The golden ration: an arbitrary value */
839 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
841 +/* The most generic version, hashes an arbitrary sequence
842 + * of bytes. No alignment or length assumptions are made about
845 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
847 + __u32 a, b, c, len;
851 + a = b = JHASH_GOLDEN_RATIO;
854 + while (len >= 12) {
855 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
856 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
857 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
859 + __jhash_mix(a,b,c);
867 + case 11: c += ((__u32)k[10]<<24);
868 + case 10: c += ((__u32)k[9]<<16);
869 + case 9 : c += ((__u32)k[8]<<8);
870 + case 8 : b += ((__u32)k[7]<<24);
871 + case 7 : b += ((__u32)k[6]<<16);
872 + case 6 : b += ((__u32)k[5]<<8);
873 + case 5 : b += k[4];
874 + case 4 : a += ((__u32)k[3]<<24);
875 + case 3 : a += ((__u32)k[2]<<16);
876 + case 2 : a += ((__u32)k[1]<<8);
877 + case 1 : a += k[0];
880 + __jhash_mix(a,b,c);
885 +/* A special optimized version that handles 1 or more of __u32s.
886 + * The length parameter here is the number of __u32s in the key.
888 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
890 + __u32 a, b, c, len;
892 + a = b = JHASH_GOLDEN_RATIO;
900 + __jhash_mix(a, b, c);
907 + case 2 : b += k[1];
908 + case 1 : a += k[0];
911 + __jhash_mix(a,b,c);
917 +/* A special ultra-optimized versions that knows they are hashing exactly
918 + * 3, 2 or 1 word(s).
920 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
921 + * done at the end is not done here.
923 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
925 + a += JHASH_GOLDEN_RATIO;
926 + b += JHASH_GOLDEN_RATIO;
929 + __jhash_mix(a, b, c);
934 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
936 + return jhash_3words(a, b, 0, initval);
939 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
941 + return jhash_3words(a, 0, 0, initval);
944 +#endif /* _LINUX_IPSET_JHASH_H */
945 diff --git a/include/linux/netfilter_ipv4/ip_set_macipmap.h b/include/linux/netfilter_ipv4/ip_set_macipmap.h
947 index 0000000..ee34c9b
949 +++ b/include/linux/netfilter_ipv4/ip_set_macipmap.h
951 +#ifndef __IP_SET_MACIPMAP_H
952 +#define __IP_SET_MACIPMAP_H
954 +#include <linux/netfilter_ipv4/ip_set.h>
956 +#define SETTYPE_NAME "macipmap"
957 +#define MAX_RANGE 0x0000FFFF
960 +#define IPSET_MACIP_MATCHUNSET 1
963 +#define IPSET_MACIP_ISSET 1
965 +struct ip_set_macipmap {
966 + void *members; /* the macipmap proper */
967 + ip_set_ip_t first_ip; /* host byte order, included in range */
968 + ip_set_ip_t last_ip; /* host byte order, included in range */
972 +struct ip_set_req_macipmap_create {
978 +struct ip_set_req_macipmap {
980 + unsigned char ethernet[ETH_ALEN];
983 +struct ip_set_macip {
984 + unsigned short flags;
985 + unsigned char ethernet[ETH_ALEN];
988 +#endif /* __IP_SET_MACIPMAP_H */
989 diff --git a/include/linux/netfilter_ipv4/ip_set_malloc.h b/include/linux/netfilter_ipv4/ip_set_malloc.h
991 index 0000000..ab97e14
993 +++ b/include/linux/netfilter_ipv4/ip_set_malloc.h
995 +#ifndef _IP_SET_MALLOC_H
996 +#define _IP_SET_MALLOC_H
1000 +/* Memory allocation and deallocation */
1001 +static size_t max_malloc_size = 0;
1003 +static inline void init_max_malloc_size(void)
1005 +#define CACHE(x) max_malloc_size = x;
1006 +#include <linux/kmalloc_sizes.h>
1010 +static inline void * ip_set_malloc(size_t bytes)
1012 + if (bytes > max_malloc_size)
1013 + return vmalloc(bytes);
1015 + return kmalloc(bytes, GFP_KERNEL);
1018 +static inline void ip_set_free(void * data, size_t bytes)
1020 + if (bytes > max_malloc_size)
1027 + size_t max_elements;
1031 +static inline void *
1032 +harray_malloc(size_t hashsize, size_t typesize, int flags)
1034 + struct harray *harray;
1035 + size_t max_elements, size, i, j;
1037 + if (!max_malloc_size)
1038 + init_max_malloc_size();
1040 + if (typesize > max_malloc_size)
1043 + max_elements = max_malloc_size/typesize;
1044 + size = hashsize/max_elements;
1045 + if (hashsize % max_elements)
1048 + /* Last pointer signals end of arrays */
1049 + harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
1055 + for (i = 0; i < size - 1; i++) {
1056 + harray->arrays[i] = kmalloc(max_elements * typesize, flags);
1057 + if (!harray->arrays[i])
1059 + memset(harray->arrays[i], 0, max_elements * typesize);
1061 + harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize,
1063 + if (!harray->arrays[i])
1065 + memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
1067 + harray->max_elements = max_elements;
1068 + harray->arrays[size] = NULL;
1070 + return (void *)harray;
1073 + for (j = 0; j < i; j++) {
1074 + kfree(harray->arrays[j]);
1080 +static inline void harray_free(void *h)
1082 + struct harray *harray = (struct harray *) h;
1085 + for (i = 0; harray->arrays[i] != NULL; i++)
1086 + kfree(harray->arrays[i]);
1090 +static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
1092 + struct harray *harray = (struct harray *) h;
1095 + for (i = 0; harray->arrays[i+1] != NULL; i++)
1096 + memset(harray->arrays[i], 0, harray->max_elements * typesize);
1097 + memset(harray->arrays[i], 0,
1098 + (hashsize - i * harray->max_elements) * typesize);
1101 +#define HARRAY_ELEM(h, type, which) \
1103 + struct harray *__h = (struct harray *)(h); \
1104 + ((type)((__h)->arrays[(which)/(__h)->max_elements]) \
1105 + + (which)%(__h)->max_elements); \
1108 +#endif /* __KERNEL__ */
1110 +#endif /*_IP_SET_MALLOC_H*/
1111 diff --git a/include/linux/netfilter_ipv4/ip_set_nethash.h b/include/linux/netfilter_ipv4/ip_set_nethash.h
1112 new file mode 100644
1113 index 0000000..172ef02
1115 +++ b/include/linux/netfilter_ipv4/ip_set_nethash.h
1117 +#ifndef __IP_SET_NETHASH_H
1118 +#define __IP_SET_NETHASH_H
1120 +#include <linux/netfilter_ipv4/ip_set.h>
1122 +#define SETTYPE_NAME "nethash"
1123 +#define MAX_RANGE 0x0000FFFF
1125 +struct ip_set_nethash {
1126 + ip_set_ip_t *members; /* the nethash proper */
1127 + uint32_t elements; /* number of elements */
1128 + uint32_t hashsize; /* hash size */
1129 + uint16_t probes; /* max number of probes */
1130 + uint16_t resize; /* resize factor in percent */
1131 + unsigned char cidr[30]; /* CIDR sizes */
1132 + void *initval[0]; /* initvals for jhash_1word */
1135 +struct ip_set_req_nethash_create {
1136 + uint32_t hashsize;
1141 +struct ip_set_req_nethash {
1143 + unsigned char cidr;
1146 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
1148 +static inline ip_set_ip_t
1149 +pack(ip_set_ip_t ip, unsigned char cidr)
1151 + ip_set_ip_t addr, *paddr = &addr;
1152 + unsigned char n, t, *a;
1154 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
1156 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
1160 + a = &((unsigned char *)paddr)[n];
1161 + *a = *a /(1 << (8 - t)) + shifts[t];
1163 + DP("n: %u, t: %u, a: %u", n, t, *a);
1164 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
1165 + HIPQUAD(ip), cidr, NIPQUAD(addr));
1168 + return ntohl(addr);
1171 +#endif /* __IP_SET_NETHASH_H */
1172 diff --git a/include/linux/netfilter_ipv4/ip_set_portmap.h b/include/linux/netfilter_ipv4/ip_set_portmap.h
1173 new file mode 100644
1174 index 0000000..c17165c
1176 +++ b/include/linux/netfilter_ipv4/ip_set_portmap.h
1178 +#ifndef __IP_SET_PORTMAP_H
1179 +#define __IP_SET_PORTMAP_H
1181 +#include <linux/netfilter_ipv4/ip_set.h>
1183 +#define SETTYPE_NAME "portmap"
1184 +#define MAX_RANGE 0x0000FFFF
1185 +#define INVALID_PORT (MAX_RANGE + 1)
1187 +struct ip_set_portmap {
1188 + void *members; /* the portmap proper */
1189 + ip_set_ip_t first_port; /* host byte order, included in range */
1190 + ip_set_ip_t last_port; /* host byte order, included in range */
1193 +struct ip_set_req_portmap_create {
1198 +struct ip_set_req_portmap {
1202 +#endif /* __IP_SET_PORTMAP_H */
1203 diff --git a/include/linux/netfilter_ipv4/ipt_set.h b/include/linux/netfilter_ipv4/ipt_set.h
1204 new file mode 100644
1205 index 0000000..2a18b93
1207 +++ b/include/linux/netfilter_ipv4/ipt_set.h
1212 +#include <linux/netfilter_ipv4/ip_set.h>
1214 +struct ipt_set_info {
1215 + ip_set_id_t index;
1216 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1220 +struct ipt_set_info_match {
1221 + struct ipt_set_info match_set;
1224 +struct ipt_set_info_target {
1225 + struct ipt_set_info add_set;
1226 + struct ipt_set_info del_set;
1229 +#endif /*_IPT_SET_H*/
1230 diff --git a/net/ipv4/netfilter/Kconfig b/net/ipv4/netfilter/Kconfig
1231 index 1833bdb..4c315cf 100644
1232 --- a/net/ipv4/netfilter/Kconfig
1233 +++ b/net/ipv4/netfilter/Kconfig
1234 @@ -388,5 +388,122 @@ config IP_NF_ARP_MANGLE
1236 endif # IP_NF_ARPTABLES
1239 + tristate "IP set support"
1240 + depends on INET && NETFILTER
1242 + This option adds IP set support to the kernel.
1243 + In order to define and use sets, you need the userspace utility
1246 + To compile it as a module, choose M here. If unsure, say N.
1248 +config IP_NF_SET_MAX
1249 + int "Maximum number of IP sets"
1252 + depends on IP_NF_SET
1254 + You can define here default value of the maximum number
1255 + of IP sets for the kernel.
1257 + The value can be overriden by the 'max_sets' module
1258 + parameter of the 'ip_set' module.
1260 +config IP_NF_SET_HASHSIZE
1261 + int "Hash size for bindings of IP sets"
1263 + depends on IP_NF_SET
1265 + You can define here default value of the hash size for
1266 + bindings of IP sets.
1268 + The value can be overriden by the 'hash_size' module
1269 + parameter of the 'ip_set' module.
1271 +config IP_NF_SET_IPMAP
1272 + tristate "ipmap set support"
1273 + depends on IP_NF_SET
1275 + This option adds the ipmap set type support.
1277 + To compile it as a module, choose M here. If unsure, say N.
1279 +config IP_NF_SET_MACIPMAP
1280 + tristate "macipmap set support"
1281 + depends on IP_NF_SET
1283 + This option adds the macipmap set type support.
1285 + To compile it as a module, choose M here. If unsure, say N.
1287 +config IP_NF_SET_PORTMAP
1288 + tristate "portmap set support"
1289 + depends on IP_NF_SET
1291 + This option adds the portmap set type support.
1293 + To compile it as a module, choose M here. If unsure, say N.
1295 +config IP_NF_SET_IPHASH
1296 + tristate "iphash set support"
1297 + depends on IP_NF_SET
1299 + This option adds the iphash set type support.
1301 + To compile it as a module, choose M here. If unsure, say N.
1303 +config IP_NF_SET_NETHASH
1304 + tristate "nethash set support"
1305 + depends on IP_NF_SET
1307 + This option adds the nethash set type support.
1309 + To compile it as a module, choose M here. If unsure, say N.
1311 +config IP_NF_SET_IPPORTHASH
1312 + tristate "ipporthash set support"
1313 + depends on IP_NF_SET
1315 + This option adds the ipporthash set type support.
1317 + To compile it as a module, choose M here. If unsure, say N.
1319 +config IP_NF_SET_IPTREE
1320 + tristate "iptree set support"
1321 + depends on IP_NF_SET
1323 + This option adds the iptree set type support.
1325 + To compile it as a module, choose M here. If unsure, say N.
1327 +config IP_NF_SET_IPTREEMAP
1328 + tristate "iptreemap set support"
1329 + depends on IP_NF_SET
1331 + This option adds the iptreemap set type support.
1333 + To compile it as a module, choose M here. If unsure, say N.
1335 +config IP_NF_MATCH_SET
1336 + tristate "set match support"
1337 + depends on IP_NF_SET
1339 + Set matching matches against given IP sets.
1340 + You need the ipset utility to create and set up the sets.
1342 + To compile it as a module, choose M here. If unsure, say N.
1344 +config IP_NF_TARGET_SET
1345 + tristate "SET target support"
1346 + depends on IP_NF_SET
1348 + The SET target makes possible to add/delete entries
1350 + You need the ipset utility to create and set up the sets.
1352 + To compile it as a module, choose M here. If unsure, say N.
1357 diff --git a/net/ipv4/netfilter/Makefile b/net/ipv4/netfilter/Makefile
1358 index 4811159..fd4913a 100644
1359 --- a/net/ipv4/netfilter/Makefile
1360 +++ b/net/ipv4/netfilter/Makefile
1361 @@ -51,6 +51,7 @@ obj-$(CONFIG_IP_NF_SECURITY) += iptable_security.o
1362 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
1363 obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o
1364 obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
1365 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
1368 obj-$(CONFIG_IP_NF_TARGET_CLUSTERIP) += ipt_CLUSTERIP.o
1369 @@ -60,8 +61,20 @@ obj-$(CONFIG_IP_NF_TARGET_MASQUERADE) += ipt_MASQUERADE.o
1370 obj-$(CONFIG_IP_NF_TARGET_NETMAP) += ipt_NETMAP.o
1371 obj-$(CONFIG_IP_NF_TARGET_REDIRECT) += ipt_REDIRECT.o
1372 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
1373 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
1374 obj-$(CONFIG_IP_NF_TARGET_ULOG) += ipt_ULOG.o
1377 +obj-$(CONFIG_IP_NF_SET) += ip_set.o
1378 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
1379 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
1380 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
1381 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
1382 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
1383 +obj-$(CONFIG_IP_NF_SET_IPPORTHASH) += ip_set_ipporthash.o
1384 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
1385 +obj-$(CONFIG_IP_NF_SET_IPTREEMAP) += ip_set_iptreemap.o
1387 # generic ARP tables
1388 obj-$(CONFIG_IP_NF_ARPTABLES) += arp_tables.o
1389 obj-$(CONFIG_IP_NF_ARP_MANGLE) += arpt_mangle.o
1390 diff --git a/net/ipv4/netfilter/ip_set.c b/net/ipv4/netfilter/ip_set.c
1391 new file mode 100644
1392 index 0000000..7d00a14
1394 +++ b/net/ipv4/netfilter/ip_set.c
1396 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1397 + * Patrick Schaaf <bof@bof.de>
1398 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1400 + * This program is free software; you can redistribute it and/or modify
1401 + * it under the terms of the GNU General Public License version 2 as
1402 + * published by the Free Software Foundation.
1405 +/* Kernel module for IP set management */
1407 +#include <linux/version.h>
1408 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
1409 +#include <linux/config.h>
1411 +#include <linux/module.h>
1412 +#include <linux/moduleparam.h>
1413 +#include <linux/kmod.h>
1414 +#include <linux/ip.h>
1415 +#include <linux/skbuff.h>
1416 +#include <linux/random.h>
1417 +#include <linux/jhash.h>
1418 +#include <linux/netfilter_ipv4/ip_tables.h>
1419 +#include <linux/errno.h>
1420 +#include <asm/uaccess.h>
1421 +#include <asm/bitops.h>
1422 +// #include <asm/semaphore.h>
1423 +#include <linux/spinlock.h>
1424 +#include <linux/vmalloc.h>
1426 +#define ASSERT_READ_LOCK(x)
1427 +#define ASSERT_WRITE_LOCK(x)
1428 +#include <linux/netfilter_ipv4/ip_set.h>
1430 +static struct list_head set_type_list; /* all registered sets */
1431 +static struct ip_set **ip_set_list; /* all individual sets */
1432 +static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1433 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1434 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1435 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1436 +static struct list_head *ip_set_hash; /* hash of bindings */
1437 +static unsigned int ip_set_hash_random; /* random seed */
1440 + * Sets are identified either by the index in ip_set_list or by id.
1441 + * The id never changes and is used to find a key in the hash.
1442 + * The index may change by swapping and used at all other places
1443 + * (set/SET netfilter modules, binding value, etc.)
1445 + * Userspace requests are serialized by ip_set_mutex and sets can
1446 + * be deleted only from userspace. Therefore ip_set_list locking
1447 + * must obey the following rules:
1449 + * - kernel requests: read and write locking mandatory
1450 + * - user requests: read locking optional, write locking mandatory
1454 +__ip_set_get(ip_set_id_t index)
1456 + atomic_inc(&ip_set_list[index]->ref);
1460 +__ip_set_put(ip_set_id_t index)
1462 + atomic_dec(&ip_set_list[index]->ref);
1466 + * Binding routines
1469 +static inline struct ip_set_hash *
1470 +__ip_set_find(u_int32_t key, ip_set_id_t id, ip_set_ip_t ip)
1472 + struct ip_set_hash *set_hash;
1474 + list_for_each_entry(set_hash, &ip_set_hash[key], list)
1475 + if (set_hash->id == id && set_hash->ip == ip)
1482 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1484 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1485 + % ip_set_bindings_hash_size;
1486 + struct ip_set_hash *set_hash;
1488 + ASSERT_READ_LOCK(&ip_set_lock);
1489 + IP_SET_ASSERT(ip_set_list[id]);
1490 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1492 + set_hash = __ip_set_find(key, id, ip);
1494 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1496 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1498 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1502 +__set_hash_del(struct ip_set_hash *set_hash)
1504 + ASSERT_WRITE_LOCK(&ip_set_lock);
1505 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1507 + __ip_set_put(set_hash->binding);
1508 + list_del(&set_hash->list);
1513 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1515 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1516 + % ip_set_bindings_hash_size;
1517 + struct ip_set_hash *set_hash;
1519 + IP_SET_ASSERT(ip_set_list[id]);
1520 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1521 + write_lock_bh(&ip_set_lock);
1522 + set_hash = __ip_set_find(key, id, ip);
1523 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1525 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1527 + if (set_hash != NULL)
1528 + __set_hash_del(set_hash);
1529 + write_unlock_bh(&ip_set_lock);
1534 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1536 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1537 + % ip_set_bindings_hash_size;
1538 + struct ip_set_hash *set_hash;
1541 + IP_SET_ASSERT(ip_set_list[id]);
1542 + IP_SET_ASSERT(ip_set_list[binding]);
1543 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1544 + HIPQUAD(ip), ip_set_list[binding]->name);
1545 + write_lock_bh(&ip_set_lock);
1546 + set_hash = __ip_set_find(key, id, ip);
1548 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_ATOMIC);
1553 + INIT_LIST_HEAD(&set_hash->list);
1554 + set_hash->id = id;
1555 + set_hash->ip = ip;
1556 + list_add(&set_hash->list, &ip_set_hash[key]);
1558 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1559 + DP("overwrite binding: %s",
1560 + ip_set_list[set_hash->binding]->name);
1561 + __ip_set_put(set_hash->binding);
1563 + set_hash->binding = binding;
1564 + __ip_set_get(set_hash->binding);
1565 + DP("stored: key %u, id %u (%s), ip %u.%u.%u.%u, binding %u (%s)",
1566 + key, id, ip_set_list[id]->name,
1567 + HIPQUAD(ip), binding, ip_set_list[binding]->name);
1569 + write_unlock_bh(&ip_set_lock);
1573 +#define FOREACH_HASH_DO(fn, args...) \
1575 + ip_set_id_t __key; \
1576 + struct ip_set_hash *__set_hash; \
1578 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1579 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1580 + fn(__set_hash , ## args); \
1584 +#define FOREACH_HASH_RW_DO(fn, args...) \
1586 + ip_set_id_t __key; \
1587 + struct ip_set_hash *__set_hash, *__n; \
1589 + ASSERT_WRITE_LOCK(&ip_set_lock); \
1590 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1591 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1592 + fn(__set_hash , ## args); \
1596 +/* Add, del and test set entries from kernel */
1598 +#define follow_bindings(index, set, ip) \
1599 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1600 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1603 +ip_set_testip_kernel(ip_set_id_t index,
1604 + const struct sk_buff *skb,
1605 + const u_int32_t *flags)
1607 + struct ip_set *set;
1610 + unsigned char i = 0;
1612 + IP_SET_ASSERT(flags[i]);
1613 + read_lock_bh(&ip_set_lock);
1615 + set = ip_set_list[index];
1616 + IP_SET_ASSERT(set);
1617 + DP("set %s, index %u", set->name, index);
1618 + read_lock_bh(&set->lock);
1619 + res = set->type->testip_kernel(set, skb, &ip, flags, i++);
1620 + read_unlock_bh(&set->lock);
1621 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1624 + && follow_bindings(index, set, ip));
1625 + read_unlock_bh(&ip_set_lock);
1631 +ip_set_addip_kernel(ip_set_id_t index,
1632 + const struct sk_buff *skb,
1633 + const u_int32_t *flags)
1635 + struct ip_set *set;
1638 + unsigned char i = 0;
1640 + IP_SET_ASSERT(flags[i]);
1642 + read_lock_bh(&ip_set_lock);
1644 + set = ip_set_list[index];
1645 + IP_SET_ASSERT(set);
1646 + DP("set %s, index %u", set->name, index);
1647 + write_lock_bh(&set->lock);
1648 + res = set->type->addip_kernel(set, skb, &ip, flags, i++);
1649 + write_unlock_bh(&set->lock);
1650 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1651 + } while ((res == 0 || res == -EEXIST)
1653 + && follow_bindings(index, set, ip));
1654 + read_unlock_bh(&ip_set_lock);
1656 + if (res == -EAGAIN
1657 + && set->type->retry
1658 + && (res = set->type->retry(set)) == 0)
1663 +ip_set_delip_kernel(ip_set_id_t index,
1664 + const struct sk_buff *skb,
1665 + const u_int32_t *flags)
1667 + struct ip_set *set;
1670 + unsigned char i = 0;
1672 + IP_SET_ASSERT(flags[i]);
1673 + read_lock_bh(&ip_set_lock);
1675 + set = ip_set_list[index];
1676 + IP_SET_ASSERT(set);
1677 + DP("set %s, index %u", set->name, index);
1678 + write_lock_bh(&set->lock);
1679 + res = set->type->delip_kernel(set, skb, &ip, flags, i++);
1680 + write_unlock_bh(&set->lock);
1681 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1682 + } while ((res == 0 || res == -EEXIST)
1684 + && follow_bindings(index, set, ip));
1685 + read_unlock_bh(&ip_set_lock);
1688 +/* Register and deregister settype */
1690 +static inline struct ip_set_type *
1691 +find_set_type(const char *name)
1693 + struct ip_set_type *set_type;
1695 + list_for_each_entry(set_type, &set_type_list, list)
1696 + if (!strncmp(set_type->typename, name, IP_SET_MAXNAMELEN - 1))
1702 +ip_set_register_set_type(struct ip_set_type *set_type)
1706 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1707 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1708 + set_type->typename,
1709 + set_type->protocol_version,
1710 + IP_SET_PROTOCOL_VERSION);
1714 + write_lock_bh(&ip_set_lock);
1715 + if (find_set_type(set_type->typename)) {
1717 + ip_set_printk("'%s' already registered!",
1718 + set_type->typename);
1722 + if (!try_module_get(THIS_MODULE)) {
1726 + list_add(&set_type->list, &set_type_list);
1727 + DP("'%s' registered.", set_type->typename);
1729 + write_unlock_bh(&ip_set_lock);
1734 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1736 + write_lock_bh(&ip_set_lock);
1737 + if (!find_set_type(set_type->typename)) {
1738 + ip_set_printk("'%s' not registered?",
1739 + set_type->typename);
1742 + list_del(&set_type->list);
1743 + module_put(THIS_MODULE);
1744 + DP("'%s' unregistered.", set_type->typename);
1746 + write_unlock_bh(&ip_set_lock);
1751 + * Userspace routines
1755 + * Find set by name, reference it once. The reference makes sure the
1756 + * thing pointed to, does not go away under our feet. Drop the reference
1757 + * later, using ip_set_put().
1760 +ip_set_get_byname(const char *name)
1762 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1764 + down(&ip_set_app_mutex);
1765 + for (i = 0; i < ip_set_max; i++) {
1766 + if (ip_set_list[i] != NULL
1767 + && strcmp(ip_set_list[i]->name, name) == 0) {
1773 + up(&ip_set_app_mutex);
1778 + * Find set by index, reference it once. The reference makes sure the
1779 + * thing pointed to, does not go away under our feet. Drop the reference
1780 + * later, using ip_set_put().
1783 +ip_set_get_byindex(ip_set_id_t index)
1785 + down(&ip_set_app_mutex);
1787 + if (index >= ip_set_max)
1788 + return IP_SET_INVALID_ID;
1790 + if (ip_set_list[index])
1791 + __ip_set_get(index);
1793 + index = IP_SET_INVALID_ID;
1795 + up(&ip_set_app_mutex);
1800 + * If the given set pointer points to a valid set, decrement
1801 + * reference count by 1. The caller shall not assume the index
1802 + * to be valid, after calling this function.
1804 +void ip_set_put(ip_set_id_t index)
1806 + down(&ip_set_app_mutex);
1807 + if (ip_set_list[index])
1808 + __ip_set_put(index);
1809 + up(&ip_set_app_mutex);
1812 +/* Find a set by name or index */
1814 +ip_set_find_byname(const char *name)
1816 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1818 + for (i = 0; i < ip_set_max; i++) {
1819 + if (ip_set_list[i] != NULL
1820 + && strcmp(ip_set_list[i]->name, name) == 0) {
1829 +ip_set_find_byindex(ip_set_id_t index)
1831 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1832 + index = IP_SET_INVALID_ID;
1838 + * Add, del, test, bind and unbind
1842 +__ip_set_testip(struct ip_set *set,
1849 + read_lock_bh(&set->lock);
1850 + res = set->type->testip(set, data, size, ip);
1851 + read_unlock_bh(&set->lock);
1857 +__ip_set_addip(ip_set_id_t index,
1861 + struct ip_set *set = ip_set_list[index];
1865 + IP_SET_ASSERT(set);
1867 + write_lock_bh(&set->lock);
1868 + res = set->type->addip(set, data, size, &ip);
1869 + write_unlock_bh(&set->lock);
1870 + } while (res == -EAGAIN
1871 + && set->type->retry
1872 + && (res = set->type->retry(set)) == 0);
1878 +ip_set_addip(ip_set_id_t index,
1883 + return __ip_set_addip(index,
1884 + data + sizeof(struct ip_set_req_adt),
1885 + size - sizeof(struct ip_set_req_adt));
1889 +ip_set_delip(ip_set_id_t index,
1893 + struct ip_set *set = ip_set_list[index];
1897 + IP_SET_ASSERT(set);
1898 + write_lock_bh(&set->lock);
1899 + res = set->type->delip(set,
1900 + data + sizeof(struct ip_set_req_adt),
1901 + size - sizeof(struct ip_set_req_adt),
1903 + write_unlock_bh(&set->lock);
1909 +ip_set_testip(ip_set_id_t index,
1913 + struct ip_set *set = ip_set_list[index];
1917 + IP_SET_ASSERT(set);
1918 + res = __ip_set_testip(set,
1919 + data + sizeof(struct ip_set_req_adt),
1920 + size - sizeof(struct ip_set_req_adt),
1923 + return (res > 0 ? -EEXIST : res);
1927 +ip_set_bindip(ip_set_id_t index,
1931 + struct ip_set *set = ip_set_list[index];
1932 + struct ip_set_req_bind *req_bind;
1933 + ip_set_id_t binding;
1937 + IP_SET_ASSERT(set);
1938 + if (size < sizeof(struct ip_set_req_bind))
1941 + req_bind = (struct ip_set_req_bind *) data;
1942 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1944 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1945 + /* Default binding of a set */
1946 + char *binding_name;
1948 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1951 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1952 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1954 + binding = ip_set_find_byname(binding_name);
1955 + if (binding == IP_SET_INVALID_ID)
1958 + write_lock_bh(&ip_set_lock);
1959 + /* Sets as binding values are referenced */
1960 + if (set->binding != IP_SET_INVALID_ID)
1961 + __ip_set_put(set->binding);
1962 + set->binding = binding;
1963 + __ip_set_get(set->binding);
1964 + write_unlock_bh(&ip_set_lock);
1968 + binding = ip_set_find_byname(req_bind->binding);
1969 + if (binding == IP_SET_INVALID_ID)
1972 + res = __ip_set_testip(set,
1973 + data + sizeof(struct ip_set_req_bind),
1974 + size - sizeof(struct ip_set_req_bind),
1976 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1977 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1980 + res = ip_set_hash_add(set->id, ip, binding);
1985 +#define FOREACH_SET_DO(fn, args...) \
1987 + ip_set_id_t __i; \
1988 + struct ip_set *__set; \
1990 + for (__i = 0; __i < ip_set_max; __i++) { \
1991 + __set = ip_set_list[__i]; \
1992 + if (__set != NULL) \
1993 + fn(__set , ##args); \
1998 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
2000 + if (set_hash->id == id)
2001 + __set_hash_del(set_hash);
2005 +__unbind_default(struct ip_set *set)
2007 + if (set->binding != IP_SET_INVALID_ID) {
2008 + /* Sets as binding values are referenced */
2009 + __ip_set_put(set->binding);
2010 + set->binding = IP_SET_INVALID_ID;
2015 +ip_set_unbindip(ip_set_id_t index,
2019 + struct ip_set *set;
2020 + struct ip_set_req_bind *req_bind;
2025 + if (size < sizeof(struct ip_set_req_bind))
2028 + req_bind = (struct ip_set_req_bind *) data;
2029 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
2031 + DP("%u %s", index, req_bind->binding);
2032 + if (index == IP_SET_INVALID_ID) {
2033 + /* unbind :all: */
2034 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
2035 + /* Default binding of sets */
2036 + write_lock_bh(&ip_set_lock);
2037 + FOREACH_SET_DO(__unbind_default);
2038 + write_unlock_bh(&ip_set_lock);
2040 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
2041 + /* Flush all bindings of all sets*/
2042 + write_lock_bh(&ip_set_lock);
2043 + FOREACH_HASH_RW_DO(__set_hash_del);
2044 + write_unlock_bh(&ip_set_lock);
2047 + DP("unreachable reached!");
2051 + set = ip_set_list[index];
2052 + IP_SET_ASSERT(set);
2053 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
2054 + /* Default binding of set */
2055 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
2057 + if (binding == IP_SET_INVALID_ID)
2060 + write_lock_bh(&ip_set_lock);
2061 + /* Sets in hash values are referenced */
2062 + __ip_set_put(set->binding);
2063 + set->binding = IP_SET_INVALID_ID;
2064 + write_unlock_bh(&ip_set_lock);
2067 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
2068 + /* Flush all bindings */
2070 + write_lock_bh(&ip_set_lock);
2071 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2072 + write_unlock_bh(&ip_set_lock);
2076 + res = __ip_set_testip(set,
2077 + data + sizeof(struct ip_set_req_bind),
2078 + size - sizeof(struct ip_set_req_bind),
2081 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
2083 + res = ip_set_hash_del(set->id, ip);
2089 +ip_set_testbind(ip_set_id_t index,
2093 + struct ip_set *set = ip_set_list[index];
2094 + struct ip_set_req_bind *req_bind;
2095 + ip_set_id_t binding;
2099 + IP_SET_ASSERT(set);
2100 + if (size < sizeof(struct ip_set_req_bind))
2103 + req_bind = (struct ip_set_req_bind *) data;
2104 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
2106 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
2107 + /* Default binding of set */
2108 + char *binding_name;
2110 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
2113 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
2114 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
2116 + binding = ip_set_find_byname(binding_name);
2117 + if (binding == IP_SET_INVALID_ID)
2120 + res = (set->binding == binding) ? -EEXIST : 0;
2124 + binding = ip_set_find_byname(req_bind->binding);
2125 + if (binding == IP_SET_INVALID_ID)
2129 + res = __ip_set_testip(set,
2130 + data + sizeof(struct ip_set_req_bind),
2131 + size - sizeof(struct ip_set_req_bind),
2133 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
2134 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
2137 + res = (ip_set_find_in_hash(set->id, ip) == binding)
2143 +static struct ip_set_type *
2144 +find_set_type_rlock(const char *typename)
2146 + struct ip_set_type *type;
2148 + read_lock_bh(&ip_set_lock);
2149 + type = find_set_type(typename);
2151 + read_unlock_bh(&ip_set_lock);
2157 +find_free_id(const char *name,
2158 + ip_set_id_t *index,
2163 + *id = IP_SET_INVALID_ID;
2164 + for (i = 0; i < ip_set_max; i++) {
2165 + if (ip_set_list[i] == NULL) {
2166 + if (*id == IP_SET_INVALID_ID)
2168 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
2172 + if (*id == IP_SET_INVALID_ID)
2173 + /* No free slot remained */
2175 + /* Check that index is usable as id (swapping) */
2177 + for (i = 0; i < ip_set_max; i++) {
2178 + if (ip_set_list[i] != NULL
2179 + && ip_set_list[i]->id == *id) {
2191 +ip_set_create(const char *name,
2192 + const char *typename,
2193 + ip_set_id_t restore,
2197 + struct ip_set *set;
2198 + ip_set_id_t index = 0, id;
2201 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
2203 + * First, and without any locks, allocate and initialize
2204 + * a normal base set structure.
2206 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
2209 + set->lock = RW_LOCK_UNLOCKED;
2210 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2211 + set->binding = IP_SET_INVALID_ID;
2212 + atomic_set(&set->ref, 0);
2215 + * Next, take the &ip_set_lock, check that we know the type,
2216 + * and take a reference on the type, to make sure it
2217 + * stays available while constructing our new set.
2219 + * After referencing the type, we drop the &ip_set_lock,
2220 + * and let the new set construction run without locks.
2222 + set->type = find_set_type_rlock(typename);
2223 + if (set->type == NULL) {
2224 + /* Try loading the module */
2225 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
2226 + strcpy(modulename, "ip_set_");
2227 + strcat(modulename, typename);
2228 + DP("try to load %s", modulename);
2229 + request_module(modulename);
2230 + set->type = find_set_type_rlock(typename);
2232 + if (set->type == NULL) {
2233 + ip_set_printk("no set type '%s', set '%s' not created",
2238 + if (!try_module_get(set->type->me)) {
2239 + read_unlock_bh(&ip_set_lock);
2243 + read_unlock_bh(&ip_set_lock);
2246 + * Without holding any locks, create private part.
2248 + res = set->type->create(set, data, size);
2252 + /* BTW, res==0 here. */
2255 + * Here, we have a valid, constructed set. &ip_set_lock again,
2256 + * find free id/index and check that it is not already in
2259 + write_lock_bh(&ip_set_lock);
2260 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
2261 + DP("no free id!");
2265 + /* Make sure restore gets the same index */
2266 + if (restore != IP_SET_INVALID_ID && index != restore) {
2267 + DP("Can't restore, sets are screwed up");
2273 + * Finally! Add our shiny new set to the list, and be done.
2275 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2277 + ip_set_list[index] = set;
2278 + write_unlock_bh(&ip_set_lock);
2282 + write_unlock_bh(&ip_set_lock);
2283 + set->type->destroy(set);
2285 + module_put(set->type->me);
2292 + * Destroy a given existing set
2295 +ip_set_destroy_set(ip_set_id_t index)
2297 + struct ip_set *set = ip_set_list[index];
2299 + IP_SET_ASSERT(set);
2300 + DP("set: %s", set->name);
2301 + write_lock_bh(&ip_set_lock);
2302 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2303 + if (set->binding != IP_SET_INVALID_ID)
2304 + __ip_set_put(set->binding);
2305 + ip_set_list[index] = NULL;
2306 + write_unlock_bh(&ip_set_lock);
2308 + /* Must call it without holding any lock */
2309 + set->type->destroy(set);
2310 + module_put(set->type->me);
2315 + * Destroy a set - or all sets
2316 + * Sets must not be referenced/used.
2319 +ip_set_destroy(ip_set_id_t index)
2323 + /* ref modification always protected by the mutex */
2324 + if (index != IP_SET_INVALID_ID) {
2325 + if (atomic_read(&ip_set_list[index]->ref))
2327 + ip_set_destroy_set(index);
2329 + for (i = 0; i < ip_set_max; i++) {
2330 + if (ip_set_list[i] != NULL
2331 + && (atomic_read(&ip_set_list[i]->ref)))
2335 + for (i = 0; i < ip_set_max; i++) {
2336 + if (ip_set_list[i] != NULL)
2337 + ip_set_destroy_set(i);
2344 +ip_set_flush_set(struct ip_set *set)
2346 + DP("set: %s %u", set->name, set->id);
2348 + write_lock_bh(&set->lock);
2349 + set->type->flush(set);
2350 + write_unlock_bh(&set->lock);
2354 + * Flush data in a set - or in all sets
2357 +ip_set_flush(ip_set_id_t index)
2359 + if (index != IP_SET_INVALID_ID) {
2360 + IP_SET_ASSERT(ip_set_list[index]);
2361 + ip_set_flush_set(ip_set_list[index]);
2363 + FOREACH_SET_DO(ip_set_flush_set);
2370 +ip_set_rename(ip_set_id_t index, const char *name)
2372 + struct ip_set *set = ip_set_list[index];
2376 + DP("set: %s to %s", set->name, name);
2377 + write_lock_bh(&ip_set_lock);
2378 + for (i = 0; i < ip_set_max; i++) {
2379 + if (ip_set_list[i] != NULL
2380 + && strncmp(ip_set_list[i]->name,
2382 + IP_SET_MAXNAMELEN - 1) == 0) {
2387 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2389 + write_unlock_bh(&ip_set_lock);
2394 + * Swap two sets so that name/index points to the other.
2395 + * References are also swapped.
2398 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2400 + struct ip_set *from = ip_set_list[from_index];
2401 + struct ip_set *to = ip_set_list[to_index];
2402 + char from_name[IP_SET_MAXNAMELEN];
2403 + u_int32_t from_ref;
2405 + DP("set: %s to %s", from->name, to->name);
2406 + /* Features must not change. Artifical restriction. */
2407 + if (from->type->features != to->type->features)
2410 + /* No magic here: ref munging protected by the mutex */
2411 + write_lock_bh(&ip_set_lock);
2412 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2413 + from_ref = atomic_read(&from->ref);
2415 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2416 + atomic_set(&from->ref, atomic_read(&to->ref));
2417 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2418 + atomic_set(&to->ref, from_ref);
2420 + ip_set_list[from_index] = to;
2421 + ip_set_list[to_index] = from;
2423 + write_unlock_bh(&ip_set_lock);
2432 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2433 + ip_set_id_t id, size_t *size)
2435 + if (set_hash->id == id)
2436 + *size += sizeof(struct ip_set_hash_list);
2440 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2441 + ip_set_id_t id, size_t *size)
2443 + if (set_hash->id == id)
2444 + *size += sizeof(struct ip_set_hash_save);
2448 +__set_hash_bindings(struct ip_set_hash *set_hash,
2449 + ip_set_id_t id, void *data, int *used)
2451 + if (set_hash->id == id) {
2452 + struct ip_set_hash_list *hash_list =
2453 + (struct ip_set_hash_list *)(data + *used);
2455 + hash_list->ip = set_hash->ip;
2456 + hash_list->binding = set_hash->binding;
2457 + *used += sizeof(struct ip_set_hash_list);
2461 +static int ip_set_list_set(ip_set_id_t index,
2466 + struct ip_set *set = ip_set_list[index];
2467 + struct ip_set_list *set_list;
2469 + /* Pointer to our header */
2470 + set_list = (struct ip_set_list *) (data + *used);
2472 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2474 + /* Get and ensure header size */
2475 + if (*used + sizeof(struct ip_set_list) > len)
2476 + goto not_enough_mem;
2477 + *used += sizeof(struct ip_set_list);
2479 + read_lock_bh(&set->lock);
2480 + /* Get and ensure set specific header size */
2481 + set_list->header_size = set->type->header_size;
2482 + if (*used + set_list->header_size > len)
2485 + /* Fill in the header */
2486 + set_list->index = index;
2487 + set_list->binding = set->binding;
2488 + set_list->ref = atomic_read(&set->ref);
2490 + /* Fill in set spefific header data */
2491 + set->type->list_header(set, data + *used);
2492 + *used += set_list->header_size;
2494 + /* Get and ensure set specific members size */
2495 + set_list->members_size = set->type->list_members_size(set);
2496 + if (*used + set_list->members_size > len)
2499 + /* Fill in set spefific members data */
2500 + set->type->list_members(set, data + *used);
2501 + *used += set_list->members_size;
2502 + read_unlock_bh(&set->lock);
2506 + /* Get and ensure set specific bindings size */
2507 + set_list->bindings_size = 0;
2508 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2509 + set->id, &set_list->bindings_size);
2510 + if (*used + set_list->bindings_size > len)
2511 + goto not_enough_mem;
2513 + /* Fill in set spefific bindings data */
2514 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2519 + read_unlock_bh(&set->lock);
2521 + DP("not enough mem, try again");
2528 +static int ip_set_save_set(ip_set_id_t index,
2533 + struct ip_set *set;
2534 + struct ip_set_save *set_save;
2536 + /* Pointer to our header */
2537 + set_save = (struct ip_set_save *) (data + *used);
2539 + /* Get and ensure header size */
2540 + if (*used + sizeof(struct ip_set_save) > len)
2541 + goto not_enough_mem;
2542 + *used += sizeof(struct ip_set_save);
2544 + set = ip_set_list[index];
2545 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2546 + data, data + *used);
2548 + read_lock_bh(&set->lock);
2549 + /* Get and ensure set specific header size */
2550 + set_save->header_size = set->type->header_size;
2551 + if (*used + set_save->header_size > len)
2554 + /* Fill in the header */
2555 + set_save->index = index;
2556 + set_save->binding = set->binding;
2558 + /* Fill in set spefific header data */
2559 + set->type->list_header(set, data + *used);
2560 + *used += set_save->header_size;
2562 + DP("set header filled: %s, used: %u(%u) %p %p", set->name, *used,
2563 + set_save->header_size, data, data + *used);
2564 + /* Get and ensure set specific members size */
2565 + set_save->members_size = set->type->list_members_size(set);
2566 + if (*used + set_save->members_size > len)
2569 + /* Fill in set spefific members data */
2570 + set->type->list_members(set, data + *used);
2571 + *used += set_save->members_size;
2572 + read_unlock_bh(&set->lock);
2573 + DP("set members filled: %s, used: %u(%u) %p %p", set->name, *used,
2574 + set_save->members_size, data, data + *used);
2578 + read_unlock_bh(&set->lock);
2580 + DP("not enough mem, try again");
2585 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2593 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2594 + struct ip_set_hash_save *hash_save =
2595 + (struct ip_set_hash_save *)(data + *used);
2596 + /* Ensure bindings size */
2597 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2601 + hash_save->id = set_hash->id;
2602 + hash_save->ip = set_hash->ip;
2603 + hash_save->binding = set_hash->binding;
2604 + *used += sizeof(struct ip_set_hash_save);
2608 +static int ip_set_save_bindings(ip_set_id_t index,
2614 + struct ip_set_save *set_save;
2616 + DP("used %u, len %u", *used, len);
2617 + /* Get and ensure header size */
2618 + if (*used + sizeof(struct ip_set_save) > len)
2622 + set_save = (struct ip_set_save *) (data + *used);
2623 + set_save->index = IP_SET_INVALID_ID;
2624 + set_save->header_size = 0;
2625 + set_save->members_size = 0;
2626 + *used += sizeof(struct ip_set_save);
2628 + DP("marker added used %u, len %u", *used, len);
2629 + /* Fill in bindings data */
2630 + if (index != IP_SET_INVALID_ID)
2631 + /* Sets are identified by id in hash */
2632 + index = ip_set_list[index]->id;
2633 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2641 +static int ip_set_restore(void *data,
2645 + int line = 0, used = 0, members_size;
2646 + struct ip_set *set;
2647 + struct ip_set_hash_save *hash_save;
2648 + struct ip_set_restore *set_restore;
2649 + ip_set_id_t index;
2651 + /* Loop to restore sets */
2655 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2656 + /* Get and ensure header size */
2657 + if (used + sizeof(struct ip_set_restore) > len)
2659 + set_restore = (struct ip_set_restore *) (data + used);
2660 + used += sizeof(struct ip_set_restore);
2662 + /* Ensure data size */
2664 + + set_restore->header_size
2665 + + set_restore->members_size > len)
2668 + /* Check marker */
2669 + if (set_restore->index == IP_SET_INVALID_ID) {
2674 + /* Try to create the set */
2675 + DP("restore %s %s", set_restore->name, set_restore->typename);
2676 + res = ip_set_create(set_restore->name,
2677 + set_restore->typename,
2678 + set_restore->index,
2680 + set_restore->header_size);
2684 + used += set_restore->header_size;
2686 + index = ip_set_find_byindex(set_restore->index);
2687 + DP("index %u, restore_index %u", index, set_restore->index);
2688 + if (index != set_restore->index)
2690 + /* Try to restore members data */
2691 + set = ip_set_list[index];
2693 + DP("members_size %u reqsize %u",
2694 + set_restore->members_size, set->type->reqsize);
2695 + while (members_size + set->type->reqsize <=
2696 + set_restore->members_size) {
2698 + DP("members: %u, line %u", members_size, line);
2699 + res = __ip_set_addip(index,
2700 + data + used + members_size,
2701 + set->type->reqsize);
2702 + if (!(res == 0 || res == -EEXIST))
2704 + members_size += set->type->reqsize;
2707 + DP("members_size %u %u",
2708 + set_restore->members_size, members_size);
2709 + if (members_size != set_restore->members_size)
2711 + used += set_restore->members_size;
2715 + /* Loop to restore bindings */
2716 + while (used < len) {
2719 + DP("restore binding, line %u", line);
2720 + /* Get and ensure size */
2721 + if (used + sizeof(struct ip_set_hash_save) > len)
2723 + hash_save = (struct ip_set_hash_save *) (data + used);
2724 + used += sizeof(struct ip_set_hash_save);
2726 + /* hash_save->id is used to store the index */
2727 + index = ip_set_find_byindex(hash_save->id);
2728 + DP("restore binding index %u, id %u, %u -> %u",
2729 + index, hash_save->id, hash_save->ip, hash_save->binding);
2730 + if (index != hash_save->id)
2732 + if (ip_set_find_byindex(hash_save->binding) == IP_SET_INVALID_ID) {
2733 + DP("corrupt binding set index %u", hash_save->binding);
2736 + set = ip_set_list[hash_save->id];
2737 + /* Null valued IP means default binding */
2738 + if (hash_save->ip)
2739 + res = ip_set_hash_add(set->id,
2741 + hash_save->binding);
2743 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2744 + write_lock_bh(&ip_set_lock);
2745 + set->binding = hash_save->binding;
2746 + __ip_set_get(set->binding);
2747 + write_unlock_bh(&ip_set_lock);
2748 + DP("default binding: %u", set->binding);
2760 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2763 + int res = 0; /* Assume OK */
2765 + struct ip_set_req_adt *req_adt;
2766 + ip_set_id_t index = IP_SET_INVALID_ID;
2767 + int (*adtfn)(ip_set_id_t index,
2768 + const void *data, size_t size);
2770 + int (*fn)(ip_set_id_t index,
2771 + const void *data, size_t size);
2773 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2774 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2777 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2778 + if (!capable(CAP_NET_ADMIN))
2780 + if (optval != SO_IP_SET)
2782 + if (len <= sizeof(unsigned)) {
2783 + ip_set_printk("short userdata (want >%zu, got %u)",
2784 + sizeof(unsigned), len);
2787 + data = vmalloc(len);
2789 + DP("out of mem for %u bytes", len);
2792 + if (copy_from_user(data, user, len) != 0) {
2796 + if (down_interruptible(&ip_set_app_mutex)) {
2801 + op = (unsigned *)data;
2804 + if (*op < IP_SET_OP_VERSION) {
2805 + /* Check the version at the beginning of operations */
2806 + struct ip_set_req_version *req_version =
2807 + (struct ip_set_req_version *) data;
2808 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2815 + case IP_SET_OP_CREATE:{
2816 + struct ip_set_req_create *req_create
2817 + = (struct ip_set_req_create *) data;
2819 + if (len < sizeof(struct ip_set_req_create)) {
2820 + ip_set_printk("short CREATE data (want >=%zu, got %u)",
2821 + sizeof(struct ip_set_req_create), len);
2825 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2826 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2827 + res = ip_set_create(req_create->name,
2828 + req_create->typename,
2829 + IP_SET_INVALID_ID,
2830 + data + sizeof(struct ip_set_req_create),
2831 + len - sizeof(struct ip_set_req_create));
2834 + case IP_SET_OP_DESTROY:{
2835 + struct ip_set_req_std *req_destroy
2836 + = (struct ip_set_req_std *) data;
2838 + if (len != sizeof(struct ip_set_req_std)) {
2839 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2840 + sizeof(struct ip_set_req_std), len);
2844 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2845 + /* Destroy all sets */
2846 + index = IP_SET_INVALID_ID;
2848 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2849 + index = ip_set_find_byname(req_destroy->name);
2851 + if (index == IP_SET_INVALID_ID) {
2857 + res = ip_set_destroy(index);
2860 + case IP_SET_OP_FLUSH:{
2861 + struct ip_set_req_std *req_flush =
2862 + (struct ip_set_req_std *) data;
2864 + if (len != sizeof(struct ip_set_req_std)) {
2865 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2866 + sizeof(struct ip_set_req_std), len);
2870 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2871 + /* Flush all sets */
2872 + index = IP_SET_INVALID_ID;
2874 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2875 + index = ip_set_find_byname(req_flush->name);
2877 + if (index == IP_SET_INVALID_ID) {
2882 + res = ip_set_flush(index);
2885 + case IP_SET_OP_RENAME:{
2886 + struct ip_set_req_create *req_rename
2887 + = (struct ip_set_req_create *) data;
2889 + if (len != sizeof(struct ip_set_req_create)) {
2890 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2891 + sizeof(struct ip_set_req_create), len);
2896 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2897 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2899 + index = ip_set_find_byname(req_rename->name);
2900 + if (index == IP_SET_INVALID_ID) {
2904 + res = ip_set_rename(index, req_rename->typename);
2907 + case IP_SET_OP_SWAP:{
2908 + struct ip_set_req_create *req_swap
2909 + = (struct ip_set_req_create *) data;
2910 + ip_set_id_t to_index;
2912 + if (len != sizeof(struct ip_set_req_create)) {
2913 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2914 + sizeof(struct ip_set_req_create), len);
2919 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2920 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2922 + index = ip_set_find_byname(req_swap->name);
2923 + if (index == IP_SET_INVALID_ID) {
2927 + to_index = ip_set_find_byname(req_swap->typename);
2928 + if (to_index == IP_SET_INVALID_ID) {
2932 + res = ip_set_swap(index, to_index);
2936 + break; /* Set identified by id */
2939 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2940 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2944 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2946 + if (len < sizeof(struct ip_set_req_adt)) {
2947 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2948 + sizeof(struct ip_set_req_adt), len);
2952 + req_adt = (struct ip_set_req_adt *) data;
2954 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2955 + if (!(*op == IP_SET_OP_UNBIND_SET
2956 + && req_adt->index == IP_SET_INVALID_ID)) {
2957 + index = ip_set_find_byindex(req_adt->index);
2958 + if (index == IP_SET_INVALID_ID) {
2963 + res = adtfn(index, data, len);
2966 + up(&ip_set_app_mutex);
2970 + DP("final result %d", res);
2975 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2979 + ip_set_id_t index = IP_SET_INVALID_ID;
2981 + int copylen = *len;
2983 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2984 + if (!capable(CAP_NET_ADMIN))
2986 + if (optval != SO_IP_SET)
2988 + if (*len < sizeof(unsigned)) {
2989 + ip_set_printk("short userdata (want >=%zu, got %d)",
2990 + sizeof(unsigned), *len);
2993 + data = vmalloc(*len);
2995 + DP("out of mem for %d bytes", *len);
2998 + if (copy_from_user(data, user, *len) != 0) {
3002 + if (down_interruptible(&ip_set_app_mutex)) {
3007 + op = (unsigned *) data;
3010 + if (*op < IP_SET_OP_VERSION) {
3011 + /* Check the version at the beginning of operations */
3012 + struct ip_set_req_version *req_version =
3013 + (struct ip_set_req_version *) data;
3014 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
3021 + case IP_SET_OP_VERSION: {
3022 + struct ip_set_req_version *req_version =
3023 + (struct ip_set_req_version *) data;
3025 + if (*len != sizeof(struct ip_set_req_version)) {
3026 + ip_set_printk("invalid VERSION (want %zu, got %d)",
3027 + sizeof(struct ip_set_req_version),
3033 + req_version->version = IP_SET_PROTOCOL_VERSION;
3034 + res = copy_to_user(user, req_version,
3035 + sizeof(struct ip_set_req_version));
3038 + case IP_SET_OP_GET_BYNAME: {
3039 + struct ip_set_req_get_set *req_get
3040 + = (struct ip_set_req_get_set *) data;
3042 + if (*len != sizeof(struct ip_set_req_get_set)) {
3043 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
3044 + sizeof(struct ip_set_req_get_set), *len);
3048 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3049 + index = ip_set_find_byname(req_get->set.name);
3050 + req_get->set.index = index;
3053 + case IP_SET_OP_GET_BYINDEX: {
3054 + struct ip_set_req_get_set *req_get
3055 + = (struct ip_set_req_get_set *) data;
3057 + if (*len != sizeof(struct ip_set_req_get_set)) {
3058 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
3059 + sizeof(struct ip_set_req_get_set), *len);
3063 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3064 + index = ip_set_find_byindex(req_get->set.index);
3065 + strncpy(req_get->set.name,
3066 + index == IP_SET_INVALID_ID ? ""
3067 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
3070 + case IP_SET_OP_ADT_GET: {
3071 + struct ip_set_req_adt_get *req_get
3072 + = (struct ip_set_req_adt_get *) data;
3074 + if (*len != sizeof(struct ip_set_req_adt_get)) {
3075 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
3076 + sizeof(struct ip_set_req_adt_get), *len);
3080 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3081 + index = ip_set_find_byname(req_get->set.name);
3082 + if (index != IP_SET_INVALID_ID) {
3083 + req_get->set.index = index;
3084 + strncpy(req_get->typename,
3085 + ip_set_list[index]->type->typename,
3086 + IP_SET_MAXNAMELEN - 1);
3093 + case IP_SET_OP_MAX_SETS: {
3094 + struct ip_set_req_max_sets *req_max_sets
3095 + = (struct ip_set_req_max_sets *) data;
3098 + if (*len != sizeof(struct ip_set_req_max_sets)) {
3099 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
3100 + sizeof(struct ip_set_req_max_sets), *len);
3105 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
3106 + req_max_sets->set.index = IP_SET_INVALID_ID;
3108 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3109 + req_max_sets->set.index =
3110 + ip_set_find_byname(req_max_sets->set.name);
3111 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
3116 + req_max_sets->max_sets = ip_set_max;
3117 + req_max_sets->sets = 0;
3118 + for (i = 0; i < ip_set_max; i++) {
3119 + if (ip_set_list[i] != NULL)
3120 + req_max_sets->sets++;
3124 + case IP_SET_OP_LIST_SIZE:
3125 + case IP_SET_OP_SAVE_SIZE: {
3126 + struct ip_set_req_setnames *req_setnames
3127 + = (struct ip_set_req_setnames *) data;
3128 + struct ip_set_name_list *name_list;
3129 + struct ip_set *set;
3133 + if (*len < sizeof(struct ip_set_req_setnames)) {
3134 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
3135 + sizeof(struct ip_set_req_setnames), *len);
3140 + req_setnames->size = 0;
3141 + used = sizeof(struct ip_set_req_setnames);
3142 + for (i = 0; i < ip_set_max; i++) {
3143 + if (ip_set_list[i] == NULL)
3145 + name_list = (struct ip_set_name_list *)
3147 + used += sizeof(struct ip_set_name_list);
3148 + if (used > copylen) {
3152 + set = ip_set_list[i];
3153 + /* Fill in index, name, etc. */
3154 + name_list->index = i;
3155 + name_list->id = set->id;
3156 + strncpy(name_list->name,
3158 + IP_SET_MAXNAMELEN - 1);
3159 + strncpy(name_list->typename,
3160 + set->type->typename,
3161 + IP_SET_MAXNAMELEN - 1);
3162 + DP("filled %s of type %s, index %u\n",
3163 + name_list->name, name_list->typename,
3164 + name_list->index);
3165 + if (!(req_setnames->index == IP_SET_INVALID_ID
3166 + || req_setnames->index == i))
3170 + case IP_SET_OP_LIST_SIZE: {
3171 + req_setnames->size += sizeof(struct ip_set_list)
3172 + + set->type->header_size
3173 + + set->type->list_members_size(set);
3174 + /* Sets are identified by id in the hash */
3175 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
3176 + set->id, &req_setnames->size);
3179 + case IP_SET_OP_SAVE_SIZE: {
3180 + req_setnames->size += sizeof(struct ip_set_save)
3181 + + set->type->header_size
3182 + + set->type->list_members_size(set);
3183 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
3184 + set->id, &req_setnames->size);
3191 + if (copylen != used) {
3197 + case IP_SET_OP_LIST: {
3198 + struct ip_set_req_list *req_list
3199 + = (struct ip_set_req_list *) data;
3203 + if (*len < sizeof(struct ip_set_req_list)) {
3204 + ip_set_printk("short LIST (want >=%zu, got %d)",
3205 + sizeof(struct ip_set_req_list), *len);
3209 + index = req_list->index;
3210 + if (index != IP_SET_INVALID_ID
3211 + && ip_set_find_byindex(index) != index) {
3216 + if (index == IP_SET_INVALID_ID) {
3217 + /* List all sets */
3218 + for (i = 0; i < ip_set_max && res == 0; i++) {
3219 + if (ip_set_list[i] != NULL)
3220 + res = ip_set_list_set(i, data, &used, *len);
3223 + /* List an individual set */
3224 + res = ip_set_list_set(index, data, &used, *len);
3228 + else if (copylen != used) {
3234 + case IP_SET_OP_SAVE: {
3235 + struct ip_set_req_list *req_save
3236 + = (struct ip_set_req_list *) data;
3240 + if (*len < sizeof(struct ip_set_req_list)) {
3241 + ip_set_printk("short SAVE (want >=%zu, got %d)",
3242 + sizeof(struct ip_set_req_list), *len);
3246 + index = req_save->index;
3247 + if (index != IP_SET_INVALID_ID
3248 + && ip_set_find_byindex(index) != index) {
3253 + if (index == IP_SET_INVALID_ID) {
3254 + /* Save all sets */
3255 + for (i = 0; i < ip_set_max && res == 0; i++) {
3256 + if (ip_set_list[i] != NULL)
3257 + res = ip_set_save_set(i, data, &used, *len);
3260 + /* Save an individual set */
3261 + res = ip_set_save_set(index, data, &used, *len);
3264 + res = ip_set_save_bindings(index, data, &used, *len);
3268 + else if (copylen != used) {
3274 + case IP_SET_OP_RESTORE: {
3275 + struct ip_set_req_setnames *req_restore
3276 + = (struct ip_set_req_setnames *) data;
3279 + if (*len < sizeof(struct ip_set_req_setnames)
3280 + || *len != req_restore->size) {
3281 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3282 + req_restore->size, *len);
3286 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3287 + req_restore->size - sizeof(struct ip_set_req_setnames));
3288 + DP("ip_set_restore: %u", line);
3291 + req_restore->size = line;
3292 + copylen = sizeof(struct ip_set_req_setnames);
3300 + } /* end of switch(op) */
3303 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3304 + && ip_set_list[index]
3305 + ? ip_set_list[index]->name
3306 + : ":all:", copylen);
3307 + res = copy_to_user(user, data, copylen);
3310 + up(&ip_set_app_mutex);
3314 + DP("final result %d", res);
3318 +static struct nf_sockopt_ops so_set = {
3320 + .set_optmin = SO_IP_SET,
3321 + .set_optmax = SO_IP_SET + 1,
3322 + .set = &ip_set_sockfn_set,
3323 + .get_optmin = SO_IP_SET,
3324 + .get_optmax = SO_IP_SET + 1,
3325 + .get = &ip_set_sockfn_get,
3326 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3329 + .owner = THIS_MODULE,
3333 +static int max_sets, hash_size;
3334 +module_param(max_sets, int, 0600);
3335 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3336 +module_param(hash_size, int, 0600);
3337 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3338 +MODULE_LICENSE("GPL");
3339 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3340 +MODULE_DESCRIPTION("module implementing core IP set support");
3342 +static int __init ip_set_init(void)
3347 + get_random_bytes(&ip_set_hash_random, 4);
3349 + ip_set_max = max_sets;
3350 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3351 + if (!ip_set_list) {
3352 + printk(KERN_ERR "Unable to create ip_set_list\n");
3355 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3357 + ip_set_bindings_hash_size = hash_size;
3358 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3359 + if (!ip_set_hash) {
3360 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3361 + vfree(ip_set_list);
3364 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3365 + INIT_LIST_HEAD(&ip_set_hash[i]);
3367 + INIT_LIST_HEAD(&set_type_list);
3369 + res = nf_register_sockopt(&so_set);
3371 + ip_set_printk("SO_SET registry failed: %d", res);
3372 + vfree(ip_set_list);
3373 + vfree(ip_set_hash);
3379 +static void __exit ip_set_fini(void)
3381 + /* There can't be any existing set or binding */
3382 + nf_unregister_sockopt(&so_set);
3383 + vfree(ip_set_list);
3384 + vfree(ip_set_hash);
3385 + DP("these are the famous last words");
3388 +EXPORT_SYMBOL(ip_set_register_set_type);
3389 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3391 +EXPORT_SYMBOL(ip_set_get_byname);
3392 +EXPORT_SYMBOL(ip_set_get_byindex);
3393 +EXPORT_SYMBOL(ip_set_put);
3395 +EXPORT_SYMBOL(ip_set_addip_kernel);
3396 +EXPORT_SYMBOL(ip_set_delip_kernel);
3397 +EXPORT_SYMBOL(ip_set_testip_kernel);
3399 +module_init(ip_set_init);
3400 +module_exit(ip_set_fini);
3401 diff --git a/net/ipv4/netfilter/ip_set_iphash.c b/net/ipv4/netfilter/ip_set_iphash.c
3402 new file mode 100644
3403 index 0000000..63f7ac9
3405 +++ b/net/ipv4/netfilter/ip_set_iphash.c
3407 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3409 + * This program is free software; you can redistribute it and/or modify
3410 + * it under the terms of the GNU General Public License version 2 as
3411 + * published by the Free Software Foundation.
3414 +/* Kernel module implementing an ip hash set */
3416 +#include <linux/module.h>
3417 +#include <linux/ip.h>
3418 +#include <linux/skbuff.h>
3419 +#include <linux/version.h>
3420 +#include <linux/jhash.h>
3421 +#include <linux/netfilter_ipv4/ip_tables.h>
3422 +#include <linux/netfilter_ipv4/ip_set.h>
3423 +#include <linux/errno.h>
3424 +#include <asm/uaccess.h>
3425 +#include <asm/bitops.h>
3426 +#include <linux/spinlock.h>
3427 +#include <linux/vmalloc.h>
3428 +#include <linux/random.h>
3430 +#include <net/ip.h>
3432 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3433 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3435 +static int limit = MAX_RANGE;
3437 +static inline __u32
3438 +jhash_ip(const struct ip_set_iphash *map, uint16_t i, ip_set_ip_t ip)
3440 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
3443 +static inline __u32
3444 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3446 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3449 + ip_set_ip_t *elem;
3451 + *hash_ip = ip & map->netmask;
3452 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3453 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3455 + for (i = 0; i < map->probes; i++) {
3456 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
3457 + DP("hash key: %u", id);
3458 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3459 + if (*elem == *hash_ip)
3461 + /* No shortcut at testing - there can be deleted
3468 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3470 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
3474 +testip(struct ip_set *set, const void *data, size_t size,
3475 + ip_set_ip_t *hash_ip)
3477 + struct ip_set_req_iphash *req =
3478 + (struct ip_set_req_iphash *) data;
3480 + if (size != sizeof(struct ip_set_req_iphash)) {
3481 + ip_set_printk("data length wrong (want %zu, have %zu)",
3482 + sizeof(struct ip_set_req_iphash),
3486 + return __testip(set, req->ip, hash_ip);
3490 +testip_kernel(struct ip_set *set,
3491 + const struct sk_buff *skb,
3492 + ip_set_ip_t *hash_ip,
3493 + const u_int32_t *flags,
3494 + unsigned char index)
3496 + return __testip(set,
3497 + ntohl(flags[index] & IPSET_SRC
3498 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3499 + ? ip_hdr(skb)->saddr
3500 + : ip_hdr(skb)->daddr),
3502 + ? skb->nh.iph->saddr
3503 + : skb->nh.iph->daddr),
3509 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3513 + ip_set_ip_t *elem;
3515 + if (!ip || map->elements >= limit)
3518 + *hash_ip = ip & map->netmask;
3520 + for (i = 0; i < map->probes; i++) {
3521 + probe = jhash_ip(map, i, *hash_ip) % map->hashsize;
3522 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
3523 + if (*elem == *hash_ip)
3531 + /* Trigger rehashing */
3536 +addip(struct ip_set *set, const void *data, size_t size,
3537 + ip_set_ip_t *hash_ip)
3539 + struct ip_set_req_iphash *req =
3540 + (struct ip_set_req_iphash *) data;
3542 + if (size != sizeof(struct ip_set_req_iphash)) {
3543 + ip_set_printk("data length wrong (want %zu, have %zu)",
3544 + sizeof(struct ip_set_req_iphash),
3548 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3552 +addip_kernel(struct ip_set *set,
3553 + const struct sk_buff *skb,
3554 + ip_set_ip_t *hash_ip,
3555 + const u_int32_t *flags,
3556 + unsigned char index)
3558 + return __addip((struct ip_set_iphash *) set->data,
3559 + ntohl(flags[index] & IPSET_SRC
3560 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3561 + ? ip_hdr(skb)->saddr
3562 + : ip_hdr(skb)->daddr),
3564 + ? skb->nh.iph->saddr
3565 + : skb->nh.iph->daddr),
3570 +static int retry(struct ip_set *set)
3572 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3573 + ip_set_ip_t hash_ip, *elem;
3575 + u_int32_t i, hashsize = map->hashsize;
3577 + struct ip_set_iphash *tmp;
3579 + if (map->resize == 0)
3585 + /* Calculate new hash size */
3586 + hashsize += (hashsize * map->resize)/100;
3587 + if (hashsize == map->hashsize)
3590 + ip_set_printk("rehashing of set %s triggered: "
3591 + "hashsize grows from %u to %u",
3592 + set->name, map->hashsize, hashsize);
3594 + tmp = kmalloc(sizeof(struct ip_set_iphash)
3595 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
3597 + DP("out of memory for %d bytes",
3598 + sizeof(struct ip_set_iphash)
3599 + + map->probes * sizeof(uint32_t));
3602 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
3603 + if (!tmp->members) {
3604 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
3608 + tmp->hashsize = hashsize;
3609 + tmp->elements = 0;
3610 + tmp->probes = map->probes;
3611 + tmp->resize = map->resize;
3612 + tmp->netmask = map->netmask;
3613 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
3615 + write_lock_bh(&set->lock);
3616 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3617 + for (i = 0; i < map->hashsize && res == 0; i++) {
3618 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3620 + res = __addip(tmp, *elem, &hash_ip);
3623 + /* Failure, try again */
3624 + write_unlock_bh(&set->lock);
3625 + harray_free(tmp->members);
3630 + /* Success at resizing! */
3631 + members = map->members;
3633 + map->hashsize = tmp->hashsize;
3634 + map->members = tmp->members;
3635 + write_unlock_bh(&set->lock);
3637 + harray_free(members);
3644 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3646 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3647 + ip_set_ip_t id, *elem;
3652 + id = hash_id(set, ip, hash_ip);
3653 + if (id == UINT_MAX)
3656 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3664 +delip(struct ip_set *set, const void *data, size_t size,
3665 + ip_set_ip_t *hash_ip)
3667 + struct ip_set_req_iphash *req =
3668 + (struct ip_set_req_iphash *) data;
3670 + if (size != sizeof(struct ip_set_req_iphash)) {
3671 + ip_set_printk("data length wrong (want %zu, have %zu)",
3672 + sizeof(struct ip_set_req_iphash),
3676 + return __delip(set, req->ip, hash_ip);
3680 +delip_kernel(struct ip_set *set,
3681 + const struct sk_buff *skb,
3682 + ip_set_ip_t *hash_ip,
3683 + const u_int32_t *flags,
3684 + unsigned char index)
3686 + return __delip(set,
3687 + ntohl(flags[index] & IPSET_SRC
3688 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3689 + ? ip_hdr(skb)->saddr
3690 + : ip_hdr(skb)->daddr),
3692 + ? skb->nh.iph->saddr
3693 + : skb->nh.iph->daddr),
3698 +static int create(struct ip_set *set, const void *data, size_t size)
3700 + struct ip_set_req_iphash_create *req =
3701 + (struct ip_set_req_iphash_create *) data;
3702 + struct ip_set_iphash *map;
3705 + if (size != sizeof(struct ip_set_req_iphash_create)) {
3706 + ip_set_printk("data length wrong (want %zu, have %zu)",
3707 + sizeof(struct ip_set_req_iphash_create),
3712 + if (req->hashsize < 1) {
3713 + ip_set_printk("hashsize too small");
3717 + if (req->probes < 1) {
3718 + ip_set_printk("probes too small");
3722 + map = kmalloc(sizeof(struct ip_set_iphash)
3723 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
3725 + DP("out of memory for %d bytes",
3726 + sizeof(struct ip_set_iphash)
3727 + + req->probes * sizeof(uint32_t));
3730 + for (i = 0; i < req->probes; i++)
3731 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
3732 + map->elements = 0;
3733 + map->hashsize = req->hashsize;
3734 + map->probes = req->probes;
3735 + map->resize = req->resize;
3736 + map->netmask = req->netmask;
3737 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
3738 + if (!map->members) {
3739 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
3748 +static void destroy(struct ip_set *set)
3750 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3752 + harray_free(map->members);
3758 +static void flush(struct ip_set *set)
3760 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3761 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
3762 + map->elements = 0;
3765 +static void list_header(const struct ip_set *set, void *data)
3767 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3768 + struct ip_set_req_iphash_create *header =
3769 + (struct ip_set_req_iphash_create *) data;
3771 + header->hashsize = map->hashsize;
3772 + header->probes = map->probes;
3773 + header->resize = map->resize;
3774 + header->netmask = map->netmask;
3777 +static int list_members_size(const struct ip_set *set)
3779 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3781 + return (map->hashsize * sizeof(ip_set_ip_t));
3784 +static void list_members(const struct ip_set *set, void *data)
3786 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3787 + ip_set_ip_t i, *elem;
3789 + for (i = 0; i < map->hashsize; i++) {
3790 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3791 + ((ip_set_ip_t *)data)[i] = *elem;
3795 +static struct ip_set_type ip_set_iphash = {
3796 + .typename = SETTYPE_NAME,
3797 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
3798 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3799 + .create = &create,
3800 + .destroy = &destroy,
3802 + .reqsize = sizeof(struct ip_set_req_iphash),
3804 + .addip_kernel = &addip_kernel,
3807 + .delip_kernel = &delip_kernel,
3808 + .testip = &testip,
3809 + .testip_kernel = &testip_kernel,
3810 + .header_size = sizeof(struct ip_set_req_iphash_create),
3811 + .list_header = &list_header,
3812 + .list_members_size = &list_members_size,
3813 + .list_members = &list_members,
3814 + .me = THIS_MODULE,
3817 +MODULE_LICENSE("GPL");
3818 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3819 +MODULE_DESCRIPTION("iphash type of IP sets");
3820 +module_param(limit, int, 0600);
3821 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
3823 +static int __init ip_set_iphash_init(void)
3825 + return ip_set_register_set_type(&ip_set_iphash);
3828 +static void __exit ip_set_iphash_fini(void)
3830 + /* FIXME: possible race with ip_set_create() */
3831 + ip_set_unregister_set_type(&ip_set_iphash);
3834 +module_init(ip_set_iphash_init);
3835 +module_exit(ip_set_iphash_fini);
3836 diff --git a/net/ipv4/netfilter/ip_set_ipmap.c b/net/ipv4/netfilter/ip_set_ipmap.c
3837 new file mode 100644
3838 index 0000000..0fe8d3c
3840 +++ b/net/ipv4/netfilter/ip_set_ipmap.c
3842 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3843 + * Patrick Schaaf <bof@bof.de>
3844 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3846 + * This program is free software; you can redistribute it and/or modify
3847 + * it under the terms of the GNU General Public License version 2 as
3848 + * published by the Free Software Foundation.
3851 +/* Kernel module implementing an IP set type: the single bitmap type */
3853 +#include <linux/module.h>
3854 +#include <linux/ip.h>
3855 +#include <linux/skbuff.h>
3856 +#include <linux/version.h>
3857 +#include <linux/netfilter_ipv4/ip_tables.h>
3858 +#include <linux/netfilter_ipv4/ip_set.h>
3859 +#include <linux/errno.h>
3860 +#include <asm/uaccess.h>
3861 +#include <asm/bitops.h>
3862 +#include <linux/spinlock.h>
3864 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3866 +static inline ip_set_ip_t
3867 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3869 + return (ip - map->first_ip)/map->hosts;
3873 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3875 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3877 + if (ip < map->first_ip || ip > map->last_ip)
3880 + *hash_ip = ip & map->netmask;
3881 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3882 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3883 + return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3887 +testip(struct ip_set *set, const void *data, size_t size,
3888 + ip_set_ip_t *hash_ip)
3890 + struct ip_set_req_ipmap *req =
3891 + (struct ip_set_req_ipmap *) data;
3893 + if (size != sizeof(struct ip_set_req_ipmap)) {
3894 + ip_set_printk("data length wrong (want %zu, have %zu)",
3895 + sizeof(struct ip_set_req_ipmap),
3899 + return __testip(set, req->ip, hash_ip);
3903 +testip_kernel(struct ip_set *set,
3904 + const struct sk_buff *skb,
3905 + ip_set_ip_t *hash_ip,
3906 + const u_int32_t *flags,
3907 + unsigned char index)
3909 + int res = __testip(set,
3910 + ntohl(flags[index] & IPSET_SRC
3911 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3912 + ? ip_hdr(skb)->saddr
3913 + : ip_hdr(skb)->daddr),
3915 + ? skb->nh.iph->saddr
3916 + : skb->nh.iph->daddr),
3919 + return (res < 0 ? 0 : res);
3923 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3925 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3927 + if (ip < map->first_ip || ip > map->last_ip)
3930 + *hash_ip = ip & map->netmask;
3931 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3932 + if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3939 +addip(struct ip_set *set, const void *data, size_t size,
3940 + ip_set_ip_t *hash_ip)
3942 + struct ip_set_req_ipmap *req =
3943 + (struct ip_set_req_ipmap *) data;
3945 + if (size != sizeof(struct ip_set_req_ipmap)) {
3946 + ip_set_printk("data length wrong (want %zu, have %zu)",
3947 + sizeof(struct ip_set_req_ipmap),
3951 + DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3952 + return __addip(set, req->ip, hash_ip);
3956 +addip_kernel(struct ip_set *set,
3957 + const struct sk_buff *skb,
3958 + ip_set_ip_t *hash_ip,
3959 + const u_int32_t *flags,
3960 + unsigned char index)
3962 + return __addip(set,
3963 + ntohl(flags[index] & IPSET_SRC
3964 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3965 + ? ip_hdr(skb)->saddr
3966 + : ip_hdr(skb)->daddr),
3968 + ? skb->nh.iph->saddr
3969 + : skb->nh.iph->daddr),
3975 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3977 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3979 + if (ip < map->first_ip || ip > map->last_ip)
3982 + *hash_ip = ip & map->netmask;
3983 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3984 + if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3991 +delip(struct ip_set *set, const void *data, size_t size,
3992 + ip_set_ip_t *hash_ip)
3994 + struct ip_set_req_ipmap *req =
3995 + (struct ip_set_req_ipmap *) data;
3997 + if (size != sizeof(struct ip_set_req_ipmap)) {
3998 + ip_set_printk("data length wrong (want %zu, have %zu)",
3999 + sizeof(struct ip_set_req_ipmap),
4003 + return __delip(set, req->ip, hash_ip);
4007 +delip_kernel(struct ip_set *set,
4008 + const struct sk_buff *skb,
4009 + ip_set_ip_t *hash_ip,
4010 + const u_int32_t *flags,
4011 + unsigned char index)
4013 + return __delip(set,
4014 + ntohl(flags[index] & IPSET_SRC
4015 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4016 + ? ip_hdr(skb)->saddr
4017 + : ip_hdr(skb)->daddr),
4019 + ? skb->nh.iph->saddr
4020 + : skb->nh.iph->daddr),
4025 +static int create(struct ip_set *set, const void *data, size_t size)
4028 + struct ip_set_req_ipmap_create *req =
4029 + (struct ip_set_req_ipmap_create *) data;
4030 + struct ip_set_ipmap *map;
4032 + if (size != sizeof(struct ip_set_req_ipmap_create)) {
4033 + ip_set_printk("data length wrong (want %zu, have %zu)",
4034 + sizeof(struct ip_set_req_ipmap_create),
4039 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
4040 + HIPQUAD(req->from), HIPQUAD(req->to));
4042 + if (req->from > req->to) {
4043 + DP("bad ip range");
4047 + map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
4049 + DP("out of memory for %d bytes",
4050 + sizeof(struct ip_set_ipmap));
4053 + map->first_ip = req->from;
4054 + map->last_ip = req->to;
4055 + map->netmask = req->netmask;
4057 + if (req->netmask == 0xFFFFFFFF) {
4059 + map->sizeid = map->last_ip - map->first_ip + 1;
4061 + unsigned int mask_bits, netmask_bits;
4064 + map->first_ip &= map->netmask; /* Should we better bark? */
4066 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
4067 + netmask_bits = mask_to_bits(map->netmask);
4069 + if ((!mask && (map->first_ip || map->last_ip != 0xFFFFFFFF))
4070 + || netmask_bits <= mask_bits)
4073 + DP("mask_bits %u, netmask_bits %u",
4074 + mask_bits, netmask_bits);
4075 + map->hosts = 2 << (32 - netmask_bits - 1);
4076 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
4078 + if (map->sizeid > MAX_RANGE + 1) {
4079 + ip_set_printk("range too big (max %d addresses)",
4084 + DP("hosts %u, sizeid %u", map->hosts, map->sizeid);
4085 + newbytes = bitmap_bytes(0, map->sizeid - 1);
4086 + map->members = kmalloc(newbytes, GFP_KERNEL);
4087 + if (!map->members) {
4088 + DP("out of memory for %d bytes", newbytes);
4092 + memset(map->members, 0, newbytes);
4098 +static void destroy(struct ip_set *set)
4100 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
4102 + kfree(map->members);
4108 +static void flush(struct ip_set *set)
4110 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
4111 + memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
4114 +static void list_header(const struct ip_set *set, void *data)
4116 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
4117 + struct ip_set_req_ipmap_create *header =
4118 + (struct ip_set_req_ipmap_create *) data;
4120 + header->from = map->first_ip;
4121 + header->to = map->last_ip;
4122 + header->netmask = map->netmask;
4125 +static int list_members_size(const struct ip_set *set)
4127 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
4129 + return bitmap_bytes(0, map->sizeid - 1);
4132 +static void list_members(const struct ip_set *set, void *data)
4134 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
4135 + int bytes = bitmap_bytes(0, map->sizeid - 1);
4137 + memcpy(data, map->members, bytes);
4140 +static struct ip_set_type ip_set_ipmap = {
4141 + .typename = SETTYPE_NAME,
4142 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
4143 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4144 + .create = &create,
4145 + .destroy = &destroy,
4147 + .reqsize = sizeof(struct ip_set_req_ipmap),
4149 + .addip_kernel = &addip_kernel,
4151 + .delip_kernel = &delip_kernel,
4152 + .testip = &testip,
4153 + .testip_kernel = &testip_kernel,
4154 + .header_size = sizeof(struct ip_set_req_ipmap_create),
4155 + .list_header = &list_header,
4156 + .list_members_size = &list_members_size,
4157 + .list_members = &list_members,
4158 + .me = THIS_MODULE,
4161 +MODULE_LICENSE("GPL");
4162 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4163 +MODULE_DESCRIPTION("ipmap type of IP sets");
4165 +static int __init ip_set_ipmap_init(void)
4167 + return ip_set_register_set_type(&ip_set_ipmap);
4170 +static void __exit ip_set_ipmap_fini(void)
4172 + /* FIXME: possible race with ip_set_create() */
4173 + ip_set_unregister_set_type(&ip_set_ipmap);
4176 +module_init(ip_set_ipmap_init);
4177 +module_exit(ip_set_ipmap_fini);
4178 diff --git a/net/ipv4/netfilter/ip_set_ipporthash.c b/net/ipv4/netfilter/ip_set_ipporthash.c
4179 new file mode 100644
4180 index 0000000..0d562b8
4182 +++ b/net/ipv4/netfilter/ip_set_ipporthash.c
4184 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4186 + * This program is free software; you can redistribute it and/or modify
4187 + * it under the terms of the GNU General Public License version 2 as
4188 + * published by the Free Software Foundation.
4191 +/* Kernel module implementing an ip+port hash set */
4193 +#include <linux/module.h>
4194 +#include <linux/ip.h>
4195 +#include <linux/tcp.h>
4196 +#include <linux/udp.h>
4197 +#include <linux/skbuff.h>
4198 +#include <linux/version.h>
4199 +#include <linux/jhash.h>
4200 +#include <linux/netfilter_ipv4/ip_tables.h>
4201 +#include <linux/netfilter_ipv4/ip_set.h>
4202 +#include <linux/errno.h>
4203 +#include <asm/uaccess.h>
4204 +#include <asm/bitops.h>
4205 +#include <linux/spinlock.h>
4206 +#include <linux/vmalloc.h>
4207 +#include <linux/random.h>
4209 +#include <net/ip.h>
4211 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4212 +#include <linux/netfilter_ipv4/ip_set_ipporthash.h>
4214 +static int limit = MAX_RANGE;
4216 +/* We must handle non-linear skbs */
4217 +static inline ip_set_ip_t
4218 +get_port(const struct sk_buff *skb, u_int32_t flags)
4220 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4221 + struct iphdr *iph = ip_hdr(skb);
4223 + struct iphdr *iph = skb->nh.iph;
4225 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
4227 + switch (iph->protocol) {
4228 + case IPPROTO_TCP: {
4229 + struct tcphdr tcph;
4231 + /* See comments at tcp_match in ip_tables.c */
4233 + return INVALID_PORT;
4235 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4236 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
4238 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
4240 + /* No choice either */
4241 + return INVALID_PORT;
4243 + return ntohs(flags & IPSET_SRC ?
4244 + tcph.source : tcph.dest);
4246 + case IPPROTO_UDP: {
4247 + struct udphdr udph;
4250 + return INVALID_PORT;
4252 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4253 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
4255 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
4257 + /* No choice either */
4258 + return INVALID_PORT;
4260 + return ntohs(flags & IPSET_SRC ?
4261 + udph.source : udph.dest);
4264 + return INVALID_PORT;
4268 +static inline __u32
4269 +jhash_ip(const struct ip_set_ipporthash *map, uint16_t i, ip_set_ip_t ip)
4271 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
4274 +#define HASH_IP(map, ip, port) (port + ((ip - ((map)->first_ip)) << 16))
4276 +static inline __u32
4277 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4278 + ip_set_ip_t *hash_ip)
4280 + struct ip_set_ipporthash *map =
4281 + (struct ip_set_ipporthash *) set->data;
4284 + ip_set_ip_t *elem;
4286 + *hash_ip = HASH_IP(map, ip, port);
4287 + DP("set: %s, ipport:%u.%u.%u.%u:%u, %u.%u.%u.%u",
4288 + set->name, HIPQUAD(ip), port, HIPQUAD(*hash_ip));
4290 + for (i = 0; i < map->probes; i++) {
4291 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
4292 + DP("hash key: %u", id);
4293 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4294 + if (*elem == *hash_ip)
4296 + /* No shortcut at testing - there can be deleted
4303 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4304 + ip_set_ip_t *hash_ip)
4306 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4308 + if (ip < map->first_ip || ip > map->last_ip)
4311 + return (hash_id(set, ip, port, hash_ip) != UINT_MAX);
4315 +testip(struct ip_set *set, const void *data, size_t size,
4316 + ip_set_ip_t *hash_ip)
4318 + struct ip_set_req_ipporthash *req =
4319 + (struct ip_set_req_ipporthash *) data;
4321 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4322 + ip_set_printk("data length wrong (want %zu, have %zu)",
4323 + sizeof(struct ip_set_req_ipporthash),
4327 + return __testip(set, req->ip, req->port, hash_ip);
4331 +testip_kernel(struct ip_set *set,
4332 + const struct sk_buff *skb,
4333 + ip_set_ip_t *hash_ip,
4334 + const u_int32_t *flags,
4335 + unsigned char index)
4340 + if (flags[index+1] == 0)
4343 + port = get_port(skb, flags[index+1]);
4345 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4346 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4347 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4348 + NIPQUAD(ip_hdr(skb)->saddr),
4349 + NIPQUAD(ip_hdr(skb)->daddr));
4351 + NIPQUAD(skb->nh.iph->saddr),
4352 + NIPQUAD(skb->nh.iph->daddr));
4354 + DP("flag %s port %u",
4355 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4357 + if (port == INVALID_PORT)
4360 + res = __testip(set,
4361 + ntohl(flags[index] & IPSET_SRC
4362 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4363 + ? ip_hdr(skb)->saddr
4364 + : ip_hdr(skb)->daddr),
4366 + ? skb->nh.iph->saddr
4367 + : skb->nh.iph->daddr),
4371 + return (res < 0 ? 0 : res);
4376 +__add_haship(struct ip_set_ipporthash *map, ip_set_ip_t hash_ip)
4380 + ip_set_ip_t *elem;
4382 + for (i = 0; i < map->probes; i++) {
4383 + probe = jhash_ip(map, i, hash_ip) % map->hashsize;
4384 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
4385 + if (*elem == hash_ip)
4393 + /* Trigger rehashing */
4398 +__addip(struct ip_set_ipporthash *map, ip_set_ip_t ip, ip_set_ip_t port,
4399 + ip_set_ip_t *hash_ip)
4401 + if (map->elements > limit)
4403 + if (ip < map->first_ip || ip > map->last_ip)
4406 + *hash_ip = HASH_IP(map, ip, port);
4408 + return __add_haship(map, *hash_ip);
4412 +addip(struct ip_set *set, const void *data, size_t size,
4413 + ip_set_ip_t *hash_ip)
4415 + struct ip_set_req_ipporthash *req =
4416 + (struct ip_set_req_ipporthash *) data;
4418 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4419 + ip_set_printk("data length wrong (want %zu, have %zu)",
4420 + sizeof(struct ip_set_req_ipporthash),
4424 + return __addip((struct ip_set_ipporthash *) set->data,
4425 + req->ip, req->port, hash_ip);
4429 +addip_kernel(struct ip_set *set,
4430 + const struct sk_buff *skb,
4431 + ip_set_ip_t *hash_ip,
4432 + const u_int32_t *flags,
4433 + unsigned char index)
4437 + if (flags[index+1] == 0)
4440 + port = get_port(skb, flags[index+1]);
4442 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4443 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4444 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4445 + NIPQUAD(ip_hdr(skb)->saddr),
4446 + NIPQUAD(ip_hdr(skb)->daddr));
4448 + NIPQUAD(skb->nh.iph->saddr),
4449 + NIPQUAD(skb->nh.iph->daddr));
4451 + DP("flag %s port %u",
4452 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4454 + if (port == INVALID_PORT)
4457 + return __addip((struct ip_set_ipporthash *) set->data,
4458 + ntohl(flags[index] & IPSET_SRC
4459 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4460 + ? ip_hdr(skb)->saddr
4461 + : ip_hdr(skb)->daddr),
4463 + ? skb->nh.iph->saddr
4464 + : skb->nh.iph->daddr),
4470 +static int retry(struct ip_set *set)
4472 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4473 + ip_set_ip_t *elem;
4475 + u_int32_t i, hashsize = map->hashsize;
4477 + struct ip_set_ipporthash *tmp;
4479 + if (map->resize == 0)
4485 + /* Calculate new hash size */
4486 + hashsize += (hashsize * map->resize)/100;
4487 + if (hashsize == map->hashsize)
4490 + ip_set_printk("rehashing of set %s triggered: "
4491 + "hashsize grows from %u to %u",
4492 + set->name, map->hashsize, hashsize);
4494 + tmp = kmalloc(sizeof(struct ip_set_ipporthash)
4495 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
4497 + DP("out of memory for %d bytes",
4498 + sizeof(struct ip_set_ipporthash)
4499 + + map->probes * sizeof(uint32_t));
4502 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
4503 + if (!tmp->members) {
4504 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
4508 + tmp->hashsize = hashsize;
4509 + tmp->elements = 0;
4510 + tmp->probes = map->probes;
4511 + tmp->resize = map->resize;
4512 + tmp->first_ip = map->first_ip;
4513 + tmp->last_ip = map->last_ip;
4514 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
4516 + write_lock_bh(&set->lock);
4517 + map = (struct ip_set_ipporthash *) set->data; /* Play safe */
4518 + for (i = 0; i < map->hashsize && res == 0; i++) {
4519 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
4521 + res = __add_haship(tmp, *elem);
4524 + /* Failure, try again */
4525 + write_unlock_bh(&set->lock);
4526 + harray_free(tmp->members);
4531 + /* Success at resizing! */
4532 + members = map->members;
4534 + map->hashsize = tmp->hashsize;
4535 + map->members = tmp->members;
4536 + write_unlock_bh(&set->lock);
4538 + harray_free(members);
4545 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port,
4546 + ip_set_ip_t *hash_ip)
4548 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4550 + ip_set_ip_t *elem;
4552 + if (ip < map->first_ip || ip > map->last_ip)
4555 + id = hash_id(set, ip, port, hash_ip);
4557 + if (id == UINT_MAX)
4560 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4568 +delip(struct ip_set *set, const void *data, size_t size,
4569 + ip_set_ip_t *hash_ip)
4571 + struct ip_set_req_ipporthash *req =
4572 + (struct ip_set_req_ipporthash *) data;
4574 + if (size != sizeof(struct ip_set_req_ipporthash)) {
4575 + ip_set_printk("data length wrong (want %zu, have %zu)",
4576 + sizeof(struct ip_set_req_ipporthash),
4580 + return __delip(set, req->ip, req->port, hash_ip);
4584 +delip_kernel(struct ip_set *set,
4585 + const struct sk_buff *skb,
4586 + ip_set_ip_t *hash_ip,
4587 + const u_int32_t *flags,
4588 + unsigned char index)
4592 + if (flags[index+1] == 0)
4595 + port = get_port(skb, flags[index+1]);
4597 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4598 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4599 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4600 + NIPQUAD(ip_hdr(skb)->saddr),
4601 + NIPQUAD(ip_hdr(skb)->daddr));
4603 + NIPQUAD(skb->nh.iph->saddr),
4604 + NIPQUAD(skb->nh.iph->daddr));
4606 + DP("flag %s port %u",
4607 + flags[index+1] & IPSET_SRC ? "SRC" : "DST",
4609 + if (port == INVALID_PORT)
4612 + return __delip(set,
4613 + ntohl(flags[index] & IPSET_SRC
4614 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4615 + ? ip_hdr(skb)->saddr
4616 + : ip_hdr(skb)->daddr),
4618 + ? skb->nh.iph->saddr
4619 + : skb->nh.iph->daddr),
4625 +static int create(struct ip_set *set, const void *data, size_t size)
4627 + struct ip_set_req_ipporthash_create *req =
4628 + (struct ip_set_req_ipporthash_create *) data;
4629 + struct ip_set_ipporthash *map;
4632 + if (size != sizeof(struct ip_set_req_ipporthash_create)) {
4633 + ip_set_printk("data length wrong (want %zu, have %zu)",
4634 + sizeof(struct ip_set_req_ipporthash_create),
4639 + if (req->hashsize < 1) {
4640 + ip_set_printk("hashsize too small");
4644 + if (req->probes < 1) {
4645 + ip_set_printk("probes too small");
4649 + map = kmalloc(sizeof(struct ip_set_ipporthash)
4650 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
4652 + DP("out of memory for %d bytes",
4653 + sizeof(struct ip_set_ipporthash)
4654 + + req->probes * sizeof(uint32_t));
4657 + for (i = 0; i < req->probes; i++)
4658 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
4659 + map->elements = 0;
4660 + map->hashsize = req->hashsize;
4661 + map->probes = req->probes;
4662 + map->resize = req->resize;
4663 + map->first_ip = req->from;
4664 + map->last_ip = req->to;
4665 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
4666 + if (!map->members) {
4667 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
4676 +static void destroy(struct ip_set *set)
4678 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4680 + harray_free(map->members);
4686 +static void flush(struct ip_set *set)
4688 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4689 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
4690 + map->elements = 0;
4693 +static void list_header(const struct ip_set *set, void *data)
4695 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4696 + struct ip_set_req_ipporthash_create *header =
4697 + (struct ip_set_req_ipporthash_create *) data;
4699 + header->hashsize = map->hashsize;
4700 + header->probes = map->probes;
4701 + header->resize = map->resize;
4702 + header->from = map->first_ip;
4703 + header->to = map->last_ip;
4706 +static int list_members_size(const struct ip_set *set)
4708 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4710 + return (map->hashsize * sizeof(ip_set_ip_t));
4713 +static void list_members(const struct ip_set *set, void *data)
4715 + struct ip_set_ipporthash *map = (struct ip_set_ipporthash *) set->data;
4716 + ip_set_ip_t i, *elem;
4718 + for (i = 0; i < map->hashsize; i++) {
4719 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
4720 + ((ip_set_ip_t *)data)[i] = *elem;
4724 +static struct ip_set_type ip_set_ipporthash = {
4725 + .typename = SETTYPE_NAME,
4726 + .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_DATA_DOUBLE,
4727 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4728 + .create = &create,
4729 + .destroy = &destroy,
4731 + .reqsize = sizeof(struct ip_set_req_ipporthash),
4733 + .addip_kernel = &addip_kernel,
4736 + .delip_kernel = &delip_kernel,
4737 + .testip = &testip,
4738 + .testip_kernel = &testip_kernel,
4739 + .header_size = sizeof(struct ip_set_req_ipporthash_create),
4740 + .list_header = &list_header,
4741 + .list_members_size = &list_members_size,
4742 + .list_members = &list_members,
4743 + .me = THIS_MODULE,
4746 +MODULE_LICENSE("GPL");
4747 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4748 +MODULE_DESCRIPTION("ipporthash type of IP sets");
4749 +module_param(limit, int, 0600);
4750 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
4752 +static int __init ip_set_ipporthash_init(void)
4754 + return ip_set_register_set_type(&ip_set_ipporthash);
4757 +static void __exit ip_set_ipporthash_fini(void)
4759 + /* FIXME: possible race with ip_set_create() */
4760 + ip_set_unregister_set_type(&ip_set_ipporthash);
4763 +module_init(ip_set_ipporthash_init);
4764 +module_exit(ip_set_ipporthash_fini);
4765 diff --git a/net/ipv4/netfilter/ip_set_iptree.c b/net/ipv4/netfilter/ip_set_iptree.c
4766 new file mode 100644
4767 index 0000000..0180534
4769 +++ b/net/ipv4/netfilter/ip_set_iptree.c
4771 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4773 + * This program is free software; you can redistribute it and/or modify
4774 + * it under the terms of the GNU General Public License version 2 as
4775 + * published by the Free Software Foundation.
4778 +/* Kernel module implementing an IP set type: the iptree type */
4780 +#include <linux/version.h>
4781 +#include <linux/module.h>
4782 +#include <linux/ip.h>
4783 +#include <linux/skbuff.h>
4784 +#include <linux/slab.h>
4785 +#include <linux/delay.h>
4786 +#include <linux/netfilter_ipv4/ip_tables.h>
4787 +#include <linux/netfilter_ipv4/ip_set.h>
4788 +#include <linux/errno.h>
4789 +#include <asm/uaccess.h>
4790 +#include <asm/bitops.h>
4791 +#include <linux/spinlock.h>
4793 +/* Backward compatibility */
4798 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
4800 +static int limit = MAX_RANGE;
4802 +/* Garbage collection interval in seconds: */
4803 +#define IPTREE_GC_TIME 5*60
4804 +/* Sleep so many milliseconds before trying again
4805 + * to delete the gc timer at destroying/flushing a set */
4806 +#define IPTREE_DESTROY_SLEEP 100
4808 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
4809 +static struct kmem_cache *branch_cachep;
4810 +static struct kmem_cache *leaf_cachep;
4812 +static kmem_cache_t *branch_cachep;
4813 +static kmem_cache_t *leaf_cachep;
4816 +#if defined(__LITTLE_ENDIAN)
4817 +#define ABCD(a,b,c,d,addrp) do { \
4818 + a = ((unsigned char *)addrp)[3]; \
4819 + b = ((unsigned char *)addrp)[2]; \
4820 + c = ((unsigned char *)addrp)[1]; \
4821 + d = ((unsigned char *)addrp)[0]; \
4823 +#elif defined(__BIG_ENDIAN)
4824 +#define ABCD(a,b,c,d,addrp) do { \
4825 + a = ((unsigned char *)addrp)[0]; \
4826 + b = ((unsigned char *)addrp)[1]; \
4827 + c = ((unsigned char *)addrp)[2]; \
4828 + d = ((unsigned char *)addrp)[3]; \
4831 +#error "Please fix asm/byteorder.h"
4832 +#endif /* __LITTLE_ENDIAN */
4834 +#define TESTIP_WALK(map, elem, branch) do { \
4835 + if ((map)->tree[elem]) { \
4836 + branch = (map)->tree[elem]; \
4842 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4844 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4845 + struct ip_set_iptreeb *btree;
4846 + struct ip_set_iptreec *ctree;
4847 + struct ip_set_iptreed *dtree;
4848 + unsigned char a,b,c,d;
4854 + ABCD(a, b, c, d, hash_ip);
4855 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
4856 + TESTIP_WALK(map, a, btree);
4857 + TESTIP_WALK(btree, b, ctree);
4858 + TESTIP_WALK(ctree, c, dtree);
4859 + DP("%lu %lu", dtree->expires[d], jiffies);
4860 + return dtree->expires[d]
4862 + || time_after(dtree->expires[d], jiffies));
4866 +testip(struct ip_set *set, const void *data, size_t size,
4867 + ip_set_ip_t *hash_ip)
4869 + struct ip_set_req_iptree *req =
4870 + (struct ip_set_req_iptree *) data;
4872 + if (size != sizeof(struct ip_set_req_iptree)) {
4873 + ip_set_printk("data length wrong (want %zu, have %zu)",
4874 + sizeof(struct ip_set_req_iptree),
4878 + return __testip(set, req->ip, hash_ip);
4882 +testip_kernel(struct ip_set *set,
4883 + const struct sk_buff *skb,
4884 + ip_set_ip_t *hash_ip,
4885 + const u_int32_t *flags,
4886 + unsigned char index)
4890 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4891 + flags[index] & IPSET_SRC ? "SRC" : "DST",
4892 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4893 + NIPQUAD(ip_hdr(skb)->saddr),
4894 + NIPQUAD(ip_hdr(skb)->daddr));
4896 + NIPQUAD(skb->nh.iph->saddr),
4897 + NIPQUAD(skb->nh.iph->daddr));
4900 + res = __testip(set,
4901 + ntohl(flags[index] & IPSET_SRC
4902 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4903 + ? ip_hdr(skb)->saddr
4904 + : ip_hdr(skb)->daddr),
4906 + ? skb->nh.iph->saddr
4907 + : skb->nh.iph->daddr),
4910 + return (res < 0 ? 0 : res);
4913 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
4914 + if ((map)->tree[elem]) { \
4915 + DP("found %u", elem); \
4916 + branch = (map)->tree[elem]; \
4918 + branch = (type *) \
4919 + kmem_cache_alloc(cachep, GFP_ATOMIC); \
4920 + if (branch == NULL) \
4922 + memset(branch, 0, sizeof(*branch)); \
4923 + (map)->tree[elem] = branch; \
4924 + DP("alloc %u", elem); \
4929 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
4930 + ip_set_ip_t *hash_ip)
4932 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4933 + struct ip_set_iptreeb *btree;
4934 + struct ip_set_iptreec *ctree;
4935 + struct ip_set_iptreed *dtree;
4936 + unsigned char a,b,c,d;
4939 + if (!ip || map->elements >= limit)
4940 + /* We could call the garbage collector
4941 + * but it's probably overkill */
4945 + ABCD(a, b, c, d, hash_ip);
4946 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
4947 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
4948 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
4949 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
4950 + if (dtree->expires[d]
4951 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4953 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
4954 + /* Lottery: I won! */
4955 + if (dtree->expires[d] == 0)
4956 + dtree->expires[d] = 1;
4957 + DP("%u %lu", d, dtree->expires[d]);
4964 +addip(struct ip_set *set, const void *data, size_t size,
4965 + ip_set_ip_t *hash_ip)
4967 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4968 + struct ip_set_req_iptree *req =
4969 + (struct ip_set_req_iptree *) data;
4971 + if (size != sizeof(struct ip_set_req_iptree)) {
4972 + ip_set_printk("data length wrong (want %zu, have %zu)",
4973 + sizeof(struct ip_set_req_iptree),
4977 + DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
4978 + return __addip(set, req->ip,
4979 + req->timeout ? req->timeout : map->timeout,
4984 +addip_kernel(struct ip_set *set,
4985 + const struct sk_buff *skb,
4986 + ip_set_ip_t *hash_ip,
4987 + const u_int32_t *flags,
4988 + unsigned char index)
4990 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4992 + return __addip(set,
4993 + ntohl(flags[index] & IPSET_SRC
4994 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
4995 + ? ip_hdr(skb)->saddr
4996 + : ip_hdr(skb)->daddr),
4998 + ? skb->nh.iph->saddr
4999 + : skb->nh.iph->daddr),
5005 +#define DELIP_WALK(map, elem, branch) do { \
5006 + if ((map)->tree[elem]) { \
5007 + branch = (map)->tree[elem]; \
5013 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5015 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5016 + struct ip_set_iptreeb *btree;
5017 + struct ip_set_iptreec *ctree;
5018 + struct ip_set_iptreed *dtree;
5019 + unsigned char a,b,c,d;
5025 + ABCD(a, b, c, d, hash_ip);
5026 + DELIP_WALK(map, a, btree);
5027 + DELIP_WALK(btree, b, ctree);
5028 + DELIP_WALK(ctree, c, dtree);
5030 + if (dtree->expires[d]) {
5031 + dtree->expires[d] = 0;
5039 +delip(struct ip_set *set, const void *data, size_t size,
5040 + ip_set_ip_t *hash_ip)
5042 + struct ip_set_req_iptree *req =
5043 + (struct ip_set_req_iptree *) data;
5045 + if (size != sizeof(struct ip_set_req_iptree)) {
5046 + ip_set_printk("data length wrong (want %zu, have %zu)",
5047 + sizeof(struct ip_set_req_iptree),
5051 + return __delip(set, req->ip, hash_ip);
5055 +delip_kernel(struct ip_set *set,
5056 + const struct sk_buff *skb,
5057 + ip_set_ip_t *hash_ip,
5058 + const u_int32_t *flags,
5059 + unsigned char index)
5061 + return __delip(set,
5062 + ntohl(flags[index] & IPSET_SRC
5063 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5064 + ? ip_hdr(skb)->saddr
5065 + : ip_hdr(skb)->daddr),
5067 + ? skb->nh.iph->saddr
5068 + : skb->nh.iph->daddr),
5073 +#define LOOP_WALK_BEGIN(map, i, branch) \
5074 + for (i = 0; i < 256; i++) { \
5075 + if (!(map)->tree[i]) \
5077 + branch = (map)->tree[i]
5079 +#define LOOP_WALK_END }
5081 +static void ip_tree_gc(unsigned long ul_set)
5083 + struct ip_set *set = (void *) ul_set;
5084 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5085 + struct ip_set_iptreeb *btree;
5086 + struct ip_set_iptreec *ctree;
5087 + struct ip_set_iptreed *dtree;
5088 + unsigned int a,b,c,d;
5089 + unsigned char i,j,k;
5092 + DP("gc: %s", set->name);
5093 + write_lock_bh(&set->lock);
5094 + LOOP_WALK_BEGIN(map, a, btree);
5095 + LOOP_WALK_BEGIN(btree, b, ctree);
5096 + LOOP_WALK_BEGIN(ctree, c, dtree);
5097 + for (d = 0; d < 256; d++) {
5098 + if (dtree->expires[d]) {
5099 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
5101 + dtree->expires[d], jiffies);
5103 + && time_before(dtree->expires[d], jiffies)) {
5104 + dtree->expires[d] = 0;
5111 + DP("gc: %s: leaf %u %u %u empty",
5112 + set->name, a, b, c);
5113 + kmem_cache_free(leaf_cachep, dtree);
5114 + ctree->tree[c] = NULL;
5116 + DP("gc: %s: leaf %u %u %u not empty",
5117 + set->name, a, b, c);
5123 + DP("gc: %s: branch %u %u empty",
5125 + kmem_cache_free(branch_cachep, ctree);
5126 + btree->tree[b] = NULL;
5128 + DP("gc: %s: branch %u %u not empty",
5135 + DP("gc: %s: branch %u empty",
5137 + kmem_cache_free(branch_cachep, btree);
5138 + map->tree[a] = NULL;
5140 + DP("gc: %s: branch %u not empty",
5145 + write_unlock_bh(&set->lock);
5147 + map->gc.expires = jiffies + map->gc_interval * HZ;
5148 + add_timer(&map->gc);
5151 +static inline void init_gc_timer(struct ip_set *set)
5153 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5155 + /* Even if there is no timeout for the entries,
5156 + * we still have to call gc because delete
5157 + * do not clean up empty branches */
5158 + map->gc_interval = IPTREE_GC_TIME;
5159 + init_timer(&map->gc);
5160 + map->gc.data = (unsigned long) set;
5161 + map->gc.function = ip_tree_gc;
5162 + map->gc.expires = jiffies + map->gc_interval * HZ;
5163 + add_timer(&map->gc);
5166 +static int create(struct ip_set *set, const void *data, size_t size)
5168 + struct ip_set_req_iptree_create *req =
5169 + (struct ip_set_req_iptree_create *) data;
5170 + struct ip_set_iptree *map;
5172 + if (size != sizeof(struct ip_set_req_iptree_create)) {
5173 + ip_set_printk("data length wrong (want %zu, have %zu)",
5174 + sizeof(struct ip_set_req_iptree_create),
5179 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
5181 + DP("out of memory for %d bytes",
5182 + sizeof(struct ip_set_iptree));
5185 + memset(map, 0, sizeof(*map));
5186 + map->timeout = req->timeout;
5187 + map->elements = 0;
5190 + init_gc_timer(set);
5195 +static void __flush(struct ip_set_iptree *map)
5197 + struct ip_set_iptreeb *btree;
5198 + struct ip_set_iptreec *ctree;
5199 + struct ip_set_iptreed *dtree;
5200 + unsigned int a,b,c;
5202 + LOOP_WALK_BEGIN(map, a, btree);
5203 + LOOP_WALK_BEGIN(btree, b, ctree);
5204 + LOOP_WALK_BEGIN(ctree, c, dtree);
5205 + kmem_cache_free(leaf_cachep, dtree);
5207 + kmem_cache_free(branch_cachep, ctree);
5209 + kmem_cache_free(branch_cachep, btree);
5211 + map->elements = 0;
5214 +static void destroy(struct ip_set *set)
5216 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5218 + /* gc might be running */
5219 + while (!del_timer(&map->gc))
5220 + msleep(IPTREE_DESTROY_SLEEP);
5226 +static void flush(struct ip_set *set)
5228 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5229 + unsigned int timeout = map->timeout;
5231 + /* gc might be running */
5232 + while (!del_timer(&map->gc))
5233 + msleep(IPTREE_DESTROY_SLEEP);
5235 + memset(map, 0, sizeof(*map));
5236 + map->timeout = timeout;
5238 + init_gc_timer(set);
5241 +static void list_header(const struct ip_set *set, void *data)
5243 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5244 + struct ip_set_req_iptree_create *header =
5245 + (struct ip_set_req_iptree_create *) data;
5247 + header->timeout = map->timeout;
5250 +static int list_members_size(const struct ip_set *set)
5252 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5253 + struct ip_set_iptreeb *btree;
5254 + struct ip_set_iptreec *ctree;
5255 + struct ip_set_iptreed *dtree;
5256 + unsigned int a,b,c,d;
5257 + unsigned int count = 0;
5259 + LOOP_WALK_BEGIN(map, a, btree);
5260 + LOOP_WALK_BEGIN(btree, b, ctree);
5261 + LOOP_WALK_BEGIN(ctree, c, dtree);
5262 + for (d = 0; d < 256; d++) {
5263 + if (dtree->expires[d]
5264 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
5271 + DP("members %u", count);
5272 + return (count * sizeof(struct ip_set_req_iptree));
5275 +static void list_members(const struct ip_set *set, void *data)
5277 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
5278 + struct ip_set_iptreeb *btree;
5279 + struct ip_set_iptreec *ctree;
5280 + struct ip_set_iptreed *dtree;
5281 + unsigned int a,b,c,d;
5282 + size_t offset = 0;
5283 + struct ip_set_req_iptree *entry;
5285 + LOOP_WALK_BEGIN(map, a, btree);
5286 + LOOP_WALK_BEGIN(btree, b, ctree);
5287 + LOOP_WALK_BEGIN(ctree, c, dtree);
5288 + for (d = 0; d < 256; d++) {
5289 + if (dtree->expires[d]
5290 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
5291 + entry = (struct ip_set_req_iptree *)(data + offset);
5292 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
5293 + entry->timeout = !map->timeout ? 0
5294 + : (dtree->expires[d] - jiffies)/HZ;
5295 + offset += sizeof(struct ip_set_req_iptree);
5303 +static struct ip_set_type ip_set_iptree = {
5304 + .typename = SETTYPE_NAME,
5305 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
5306 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5307 + .create = &create,
5308 + .destroy = &destroy,
5310 + .reqsize = sizeof(struct ip_set_req_iptree),
5312 + .addip_kernel = &addip_kernel,
5314 + .delip_kernel = &delip_kernel,
5315 + .testip = &testip,
5316 + .testip_kernel = &testip_kernel,
5317 + .header_size = sizeof(struct ip_set_req_iptree_create),
5318 + .list_header = &list_header,
5319 + .list_members_size = &list_members_size,
5320 + .list_members = &list_members,
5321 + .me = THIS_MODULE,
5324 +MODULE_LICENSE("GPL");
5325 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5326 +MODULE_DESCRIPTION("iptree type of IP sets");
5327 +module_param(limit, int, 0600);
5328 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
5330 +static int __init ip_set_iptree_init(void)
5334 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5335 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
5336 + sizeof(struct ip_set_iptreeb),
5339 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
5340 + sizeof(struct ip_set_iptreeb),
5341 + 0, 0, NULL, NULL);
5343 + if (!branch_cachep) {
5344 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
5348 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
5349 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
5350 + sizeof(struct ip_set_iptreed),
5353 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
5354 + sizeof(struct ip_set_iptreed),
5355 + 0, 0, NULL, NULL);
5357 + if (!leaf_cachep) {
5358 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
5362 + ret = ip_set_register_set_type(&ip_set_iptree);
5366 + kmem_cache_destroy(leaf_cachep);
5368 + kmem_cache_destroy(branch_cachep);
5373 +static void __exit ip_set_iptree_fini(void)
5375 + /* FIXME: possible race with ip_set_create() */
5376 + ip_set_unregister_set_type(&ip_set_iptree);
5377 + kmem_cache_destroy(leaf_cachep);
5378 + kmem_cache_destroy(branch_cachep);
5381 +module_init(ip_set_iptree_init);
5382 +module_exit(ip_set_iptree_fini);
5383 diff --git a/net/ipv4/netfilter/ip_set_iptreemap.c b/net/ipv4/netfilter/ip_set_iptreemap.c
5384 new file mode 100644
5385 index 0000000..3825055
5387 +++ b/net/ipv4/netfilter/ip_set_iptreemap.c
5389 +/* Copyright (C) 2007 Sven Wegener <sven.wegener@stealer.net>
5391 + * This program is free software; you can redistribute it and/or modify it
5392 + * under the terms of the GNU General Public License version 2 as published by
5393 + * the Free Software Foundation.
5396 +/* This modules implements the iptreemap ipset type. It uses bitmaps to
5397 + * represent every single IPv4 address as a single bit. The bitmaps are managed
5398 + * in a tree structure, where the first three octets of an addresses are used
5399 + * as an index to find the bitmap and the last octet is used as the bit number.
5402 +#include <linux/version.h>
5403 +#include <linux/module.h>
5404 +#include <linux/ip.h>
5405 +#include <linux/skbuff.h>
5406 +#include <linux/slab.h>
5407 +#include <linux/delay.h>
5408 +#include <linux/netfilter_ipv4/ip_tables.h>
5409 +#include <linux/netfilter_ipv4/ip_set.h>
5410 +#include <linux/errno.h>
5411 +#include <asm/uaccess.h>
5412 +#include <asm/bitops.h>
5413 +#include <linux/spinlock.h>
5415 +#include <linux/netfilter_ipv4/ip_set_iptreemap.h>
5417 +#define IPTREEMAP_DEFAULT_GC_TIME (5 * 60)
5418 +#define IPTREEMAP_DESTROY_SLEEP (100)
5420 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
5421 +static struct kmem_cache *cachep_b;
5422 +static struct kmem_cache *cachep_c;
5423 +static struct kmem_cache *cachep_d;
5425 +static kmem_cache_t *cachep_b;
5426 +static kmem_cache_t *cachep_c;
5427 +static kmem_cache_t *cachep_d;
5430 +static struct ip_set_iptreemap_d *fullbitmap_d;
5431 +static struct ip_set_iptreemap_c *fullbitmap_c;
5432 +static struct ip_set_iptreemap_b *fullbitmap_b;
5434 +#if defined(__LITTLE_ENDIAN)
5435 +#define ABCD(a, b, c, d, addr) \
5437 + a = ((unsigned char *)addr)[3]; \
5438 + b = ((unsigned char *)addr)[2]; \
5439 + c = ((unsigned char *)addr)[1]; \
5440 + d = ((unsigned char *)addr)[0]; \
5442 +#elif defined(__BIG_ENDIAN)
5443 +#define ABCD(a,b,c,d,addrp) do { \
5444 + a = ((unsigned char *)addrp)[0]; \
5445 + b = ((unsigned char *)addrp)[1]; \
5446 + c = ((unsigned char *)addrp)[2]; \
5447 + d = ((unsigned char *)addrp)[3]; \
5450 +#error "Please fix asm/byteorder.h"
5451 +#endif /* __LITTLE_ENDIAN */
5453 +#define TESTIP_WALK(map, elem, branch, full) \
5455 + branch = (map)->tree[elem]; \
5458 + else if (branch == full) \
5462 +#define ADDIP_WALK(map, elem, branch, type, cachep, full) \
5464 + branch = (map)->tree[elem]; \
5466 + branch = (type *) kmem_cache_alloc(cachep, GFP_ATOMIC); \
5469 + memset(branch, 0, sizeof(*branch)); \
5470 + (map)->tree[elem] = branch; \
5471 + } else if (branch == full) { \
5476 +#define ADDIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free) \
5477 + for (a = a1; a <= a2; a++) { \
5478 + branch = (map)->tree[a]; \
5479 + if (branch != full) { \
5480 + if ((a > a1 && a < a2) || (hint)) { \
5483 + (map)->tree[a] = full; \
5485 + } else if (!branch) { \
5486 + branch = kmem_cache_alloc(cachep, GFP_ATOMIC); \
5489 + memset(branch, 0, sizeof(*branch)); \
5490 + (map)->tree[a] = branch; \
5493 +#define ADDIP_RANGE_LOOP_END() \
5497 +#define DELIP_WALK(map, elem, branch, cachep, full, flags) \
5499 + branch = (map)->tree[elem]; \
5502 + } else if (branch == full) { \
5503 + branch = kmem_cache_alloc(cachep, flags); \
5506 + memcpy(branch, full, sizeof(*full)); \
5507 + (map)->tree[elem] = branch; \
5511 +#define DELIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free, flags) \
5512 + for (a = a1; a <= a2; a++) { \
5513 + branch = (map)->tree[a]; \
5515 + if ((a > a1 && a < a2) || (hint)) { \
5516 + if (branch != full) \
5518 + (map)->tree[a] = NULL; \
5520 + } else if (branch == full) { \
5521 + branch = kmem_cache_alloc(cachep, flags); \
5524 + memcpy(branch, full, sizeof(*branch)); \
5525 + (map)->tree[a] = branch; \
5528 +#define DELIP_RANGE_LOOP_END() \
5532 +#define LOOP_WALK_BEGIN(map, i, branch) \
5533 + for (i = 0; i < 256; i++) { \
5534 + branch = (map)->tree[i]; \
5535 + if (likely(!branch)) \
5538 +#define LOOP_WALK_END() \
5541 +#define LOOP_WALK_BEGIN_GC(map, i, branch, full, cachep, count) \
5543 + for (i = 0; i < 256; i++) { \
5544 + branch = (map)->tree[i]; \
5545 + if (likely(!branch)) \
5548 + if (branch == full) { \
5553 +#define LOOP_WALK_END_GC(map, i, branch, full, cachep, count) \
5554 + if (-256 == count) { \
5555 + kmem_cache_free(cachep, branch); \
5556 + (map)->tree[i] = NULL; \
5557 + } else if (256 == count) { \
5558 + kmem_cache_free(cachep, branch); \
5559 + (map)->tree[i] = full; \
5563 +#define LOOP_WALK_BEGIN_COUNT(map, i, branch, inrange, count) \
5564 + for (i = 0; i < 256; i++) { \
5565 + if (!(map)->tree[i]) { \
5572 + branch = (map)->tree[i];
5574 +#define LOOP_WALK_END_COUNT() \
5577 +#define MIN(a, b) (a < b ? a : b)
5578 +#define MAX(a, b) (a > b ? a : b)
5580 +#define GETVALUE1(a, a1, b1, r) \
5581 + (a == a1 ? b1 : r)
5583 +#define GETVALUE2(a, b, a1, b1, c1, r) \
5584 + (a == a1 && b == b1 ? c1 : r)
5586 +#define GETVALUE3(a, b, c, a1, b1, c1, d1, r) \
5587 + (a == a1 && b == b1 && c == c1 ? d1 : r)
5589 +#define CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2) \
5591 + GETVALUE1(a, a1, b1, 0) == 0 \
5592 + && GETVALUE1(a, a2, b2, 255) == 255 \
5599 +#define CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2) \
5601 + GETVALUE2(a, b, a1, b1, c1, 0) == 0 \
5602 + && GETVALUE2(a, b, a2, b2, c2, 255) == 255 \
5607 +#define CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2) \
5609 + GETVALUE3(a, b, c, a1, b1, c1, d1, 0) == 0 \
5610 + && GETVALUE3(a, b, c, a2, b2, c2, d2, 255) == 255 \
5615 +free_d(struct ip_set_iptreemap_d *map)
5617 + kmem_cache_free(cachep_d, map);
5621 +free_c(struct ip_set_iptreemap_c *map)
5623 + struct ip_set_iptreemap_d *dtree;
5626 + LOOP_WALK_BEGIN(map, i, dtree) {
5627 + if (dtree != fullbitmap_d)
5629 + } LOOP_WALK_END();
5631 + kmem_cache_free(cachep_c, map);
5635 +free_b(struct ip_set_iptreemap_b *map)
5637 + struct ip_set_iptreemap_c *ctree;
5640 + LOOP_WALK_BEGIN(map, i, ctree) {
5641 + if (ctree != fullbitmap_c)
5643 + } LOOP_WALK_END();
5645 + kmem_cache_free(cachep_b, map);
5649 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5651 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5652 + struct ip_set_iptreemap_b *btree;
5653 + struct ip_set_iptreemap_c *ctree;
5654 + struct ip_set_iptreemap_d *dtree;
5655 + unsigned char a, b, c, d;
5659 + ABCD(a, b, c, d, hash_ip);
5661 + TESTIP_WALK(map, a, btree, fullbitmap_b);
5662 + TESTIP_WALK(btree, b, ctree, fullbitmap_c);
5663 + TESTIP_WALK(ctree, c, dtree, fullbitmap_d);
5665 + return !!test_bit(d, (void *) dtree->bitmap);
5669 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5671 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5673 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5674 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5678 + return __testip(set, req->start, hash_ip);
5682 +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)
5686 + res = __testip(set,
5687 + ntohl(flags[index] & IPSET_SRC
5688 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5689 + ? ip_hdr(skb)->saddr
5690 + : ip_hdr(skb)->daddr),
5692 + ? skb->nh.iph->saddr
5693 + : skb->nh.iph->daddr),
5697 + return (res < 0 ? 0 : res);
5701 +__addip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
5703 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5704 + struct ip_set_iptreemap_b *btree;
5705 + struct ip_set_iptreemap_c *ctree;
5706 + struct ip_set_iptreemap_d *dtree;
5707 + unsigned char a, b, c, d;
5711 + ABCD(a, b, c, d, hash_ip);
5713 + ADDIP_WALK(map, a, btree, struct ip_set_iptreemap_b, cachep_b, fullbitmap_b);
5714 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreemap_c, cachep_c, fullbitmap_c);
5715 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreemap_d, cachep_d, fullbitmap_d);
5717 + if (test_and_set_bit(d, (void *) dtree->bitmap))
5720 + set_bit(b, (void *) btree->dirty);
5726 +__addip_range(struct ip_set *set, ip_set_ip_t start, ip_set_ip_t end, ip_set_ip_t *hash_ip)
5728 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5729 + struct ip_set_iptreemap_b *btree;
5730 + struct ip_set_iptreemap_c *ctree;
5731 + struct ip_set_iptreemap_d *dtree;
5732 + unsigned int a, b, c, d;
5733 + unsigned char a1, b1, c1, d1;
5734 + unsigned char a2, b2, c2, d2;
5737 + return __addip_single(set, start, hash_ip);
5741 + ABCD(a1, b1, c1, d1, &start);
5742 + ABCD(a2, b2, c2, d2, &end);
5744 + /* This is sooo ugly... */
5745 + ADDIP_RANGE_LOOP(map, a, a1, a2, CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2), btree, fullbitmap_b, cachep_b, free_b) {
5746 + 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) {
5747 + 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) {
5748 + for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5749 + set_bit(d, (void *) dtree->bitmap);
5750 + set_bit(b, (void *) btree->dirty);
5751 + } ADDIP_RANGE_LOOP_END();
5752 + } ADDIP_RANGE_LOOP_END();
5753 + } ADDIP_RANGE_LOOP_END();
5759 +addip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5761 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5763 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5764 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5768 + return __addip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip);
5772 +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)
5775 + return __addip_single(set,
5776 + ntohl(flags[index] & IPSET_SRC
5777 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5778 + ? ip_hdr(skb)->saddr
5779 + : ip_hdr(skb)->daddr),
5781 + ? skb->nh.iph->saddr
5782 + : skb->nh.iph->daddr),
5788 +__delip_single(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip, unsigned int __nocast flags)
5790 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5791 + struct ip_set_iptreemap_b *btree;
5792 + struct ip_set_iptreemap_c *ctree;
5793 + struct ip_set_iptreemap_d *dtree;
5794 + unsigned char a,b,c,d;
5798 + ABCD(a, b, c, d, hash_ip);
5800 + DELIP_WALK(map, a, btree, cachep_b, fullbitmap_b, flags);
5801 + DELIP_WALK(btree, b, ctree, cachep_c, fullbitmap_c, flags);
5802 + DELIP_WALK(ctree, c, dtree, cachep_d, fullbitmap_d, flags);
5804 + if (!test_and_clear_bit(d, (void *) dtree->bitmap))
5807 + set_bit(b, (void *) btree->dirty);
5813 +__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)
5815 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5816 + struct ip_set_iptreemap_b *btree;
5817 + struct ip_set_iptreemap_c *ctree;
5818 + struct ip_set_iptreemap_d *dtree;
5819 + unsigned int a, b, c, d;
5820 + unsigned char a1, b1, c1, d1;
5821 + unsigned char a2, b2, c2, d2;
5824 + return __delip_single(set, start, hash_ip, flags);
5828 + ABCD(a1, b1, c1, d1, &start);
5829 + ABCD(a2, b2, c2, d2, &end);
5831 + /* This is sooo ugly... */
5832 + 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) {
5833 + 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) {
5834 + 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) {
5835 + for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5836 + clear_bit(d, (void *) dtree->bitmap);
5837 + set_bit(b, (void *) btree->dirty);
5838 + } DELIP_RANGE_LOOP_END();
5839 + } DELIP_RANGE_LOOP_END();
5840 + } DELIP_RANGE_LOOP_END();
5846 +delip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
5848 + struct ip_set_req_iptreemap *req = (struct ip_set_req_iptreemap *) data;
5850 + if (size != sizeof(struct ip_set_req_iptreemap)) {
5851 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap), size);
5855 + return __delip_range(set, MIN(req->start, req->end), MAX(req->start, req->end), hash_ip, GFP_KERNEL);
5859 +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)
5861 + return __delip_single(set,
5862 + ntohl(flags[index] & IPSET_SRC
5863 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
5864 + ? ip_hdr(skb)->saddr
5865 + : ip_hdr(skb)->daddr),
5867 + ? skb->nh.iph->saddr
5868 + : skb->nh.iph->daddr),
5874 +/* Check the status of the bitmap
5875 + * -1 == all bits cleared
5876 + * 1 == all bits set
5877 + * 0 == anything else
5880 +bitmap_status(struct ip_set_iptreemap_d *dtree)
5882 + unsigned char first = dtree->bitmap[0];
5885 + for (a = 1; a < 32; a++)
5886 + if (dtree->bitmap[a] != first)
5889 + return (first == 0 ? -1 : (first == 255 ? 1 : 0));
5893 +gc(unsigned long addr)
5895 + struct ip_set *set = (struct ip_set *) addr;
5896 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5897 + struct ip_set_iptreemap_b *btree;
5898 + struct ip_set_iptreemap_c *ctree;
5899 + struct ip_set_iptreemap_d *dtree;
5900 + unsigned int a, b, c;
5903 + write_lock_bh(&set->lock);
5905 + LOOP_WALK_BEGIN_GC(map, a, btree, fullbitmap_b, cachep_b, i) {
5906 + LOOP_WALK_BEGIN_GC(btree, b, ctree, fullbitmap_c, cachep_c, j) {
5907 + if (!test_and_clear_bit(b, (void *) btree->dirty))
5909 + LOOP_WALK_BEGIN_GC(ctree, c, dtree, fullbitmap_d, cachep_d, k) {
5910 + switch (bitmap_status(dtree)) {
5912 + kmem_cache_free(cachep_d, dtree);
5913 + ctree->tree[c] = NULL;
5917 + kmem_cache_free(cachep_d, dtree);
5918 + ctree->tree[c] = fullbitmap_d;
5922 + } LOOP_WALK_END();
5923 + } LOOP_WALK_END_GC(btree, b, ctree, fullbitmap_c, cachep_c, k);
5924 + } LOOP_WALK_END_GC(map, a, btree, fullbitmap_b, cachep_b, j);
5926 + write_unlock_bh(&set->lock);
5928 + map->gc.expires = jiffies + map->gc_interval * HZ;
5929 + add_timer(&map->gc);
5933 +init_gc_timer(struct ip_set *set)
5935 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5937 + init_timer(&map->gc);
5938 + map->gc.data = (unsigned long) set;
5939 + map->gc.function = gc;
5940 + map->gc.expires = jiffies + map->gc_interval * HZ;
5941 + add_timer(&map->gc);
5944 +static int create(struct ip_set *set, const void *data, size_t size)
5946 + struct ip_set_req_iptreemap_create *req = (struct ip_set_req_iptreemap_create *) data;
5947 + struct ip_set_iptreemap *map;
5949 + if (size != sizeof(struct ip_set_req_iptreemap_create)) {
5950 + ip_set_printk("data length wrong (want %zu, have %zu)", sizeof(struct ip_set_req_iptreemap_create), size);
5954 + map = kzalloc(sizeof(*map), GFP_KERNEL);
5958 + map->gc_interval = req->gc_interval ? req->gc_interval : IPTREEMAP_DEFAULT_GC_TIME;
5961 + init_gc_timer(set);
5966 +static inline void __flush(struct ip_set_iptreemap *map)
5968 + struct ip_set_iptreemap_b *btree;
5971 + LOOP_WALK_BEGIN(map, a, btree);
5972 + if (btree != fullbitmap_b)
5977 +static void destroy(struct ip_set *set)
5979 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5981 + while (!del_timer(&map->gc))
5982 + msleep(IPTREEMAP_DESTROY_SLEEP);
5990 +static void flush(struct ip_set *set)
5992 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5994 + while (!del_timer(&map->gc))
5995 + msleep(IPTREEMAP_DESTROY_SLEEP);
5999 + memset(map, 0, sizeof(*map));
6001 + init_gc_timer(set);
6004 +static void list_header(const struct ip_set *set, void *data)
6006 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
6007 + struct ip_set_req_iptreemap_create *header = (struct ip_set_req_iptreemap_create *) data;
6009 + header->gc_interval = map->gc_interval;
6012 +static int list_members_size(const struct ip_set *set)
6014 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
6015 + struct ip_set_iptreemap_b *btree;
6016 + struct ip_set_iptreemap_c *ctree;
6017 + struct ip_set_iptreemap_d *dtree;
6018 + unsigned int a, b, c, d, inrange = 0, count = 0;
6020 + LOOP_WALK_BEGIN_COUNT(map, a, btree, inrange, count) {
6021 + LOOP_WALK_BEGIN_COUNT(btree, b, ctree, inrange, count) {
6022 + LOOP_WALK_BEGIN_COUNT(ctree, c, dtree, inrange, count) {
6023 + for (d = 0; d < 256; d++) {
6024 + if (test_bit(d, (void *) dtree->bitmap)) {
6026 + } else if (inrange) {
6031 + } LOOP_WALK_END_COUNT();
6032 + } LOOP_WALK_END_COUNT();
6033 + } LOOP_WALK_END_COUNT();
6038 + return (count * sizeof(struct ip_set_req_iptreemap));
6041 +static inline size_t add_member(void *data, size_t offset, ip_set_ip_t start, ip_set_ip_t end)
6043 + struct ip_set_req_iptreemap *entry = (struct ip_set_req_iptreemap *) (data + offset);
6045 + entry->start = start;
6048 + return sizeof(*entry);
6051 +static void list_members(const struct ip_set *set, void *data)
6053 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
6054 + struct ip_set_iptreemap_b *btree;
6055 + struct ip_set_iptreemap_c *ctree;
6056 + struct ip_set_iptreemap_d *dtree;
6057 + unsigned int a, b, c, d, inrange = 0;
6058 + size_t offset = 0;
6059 + ip_set_ip_t start = 0, end = 0, ip;
6061 + LOOP_WALK_BEGIN(map, a, btree) {
6062 + LOOP_WALK_BEGIN(btree, b, ctree) {
6063 + LOOP_WALK_BEGIN(ctree, c, dtree) {
6064 + for (d = 0; d < 256; d++) {
6065 + if (test_bit(d, (void *) dtree->bitmap)) {
6066 + ip = ((a << 24) | (b << 16) | (c << 8) | d);
6070 + } else if (end < ip - 1) {
6071 + offset += add_member(data, offset, start, end);
6075 + } else if (inrange) {
6076 + offset += add_member(data, offset, start, end);
6080 + } LOOP_WALK_END();
6081 + } LOOP_WALK_END();
6082 + } LOOP_WALK_END();
6085 + add_member(data, offset, start, end);
6088 +static struct ip_set_type ip_set_iptreemap = {
6089 + .typename = SETTYPE_NAME,
6090 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6091 + .protocol_version = IP_SET_PROTOCOL_VERSION,
6093 + .destroy = destroy,
6095 + .reqsize = sizeof(struct ip_set_req_iptreemap),
6097 + .addip_kernel = addip_kernel,
6099 + .delip_kernel = delip_kernel,
6101 + .testip_kernel = testip_kernel,
6102 + .header_size = sizeof(struct ip_set_req_iptreemap_create),
6103 + .list_header = list_header,
6104 + .list_members_size = list_members_size,
6105 + .list_members = list_members,
6106 + .me = THIS_MODULE,
6109 +MODULE_LICENSE("GPL");
6110 +MODULE_AUTHOR("Sven Wegener <sven.wegener@stealer.net>");
6111 +MODULE_DESCRIPTION("iptreemap type of IP sets");
6113 +static int __init ip_set_iptreemap_init(void)
6115 + int ret = -ENOMEM;
6118 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
6119 + cachep_b = kmem_cache_create("ip_set_iptreemap_b",
6120 + sizeof(struct ip_set_iptreemap_b),
6123 + cachep_b = kmem_cache_create("ip_set_iptreemap_b",
6124 + sizeof(struct ip_set_iptreemap_b),
6125 + 0, 0, NULL, NULL);
6128 + ip_set_printk("Unable to create ip_set_iptreemap_b slab cache");
6132 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
6133 + cachep_c = kmem_cache_create("ip_set_iptreemap_c",
6134 + sizeof(struct ip_set_iptreemap_c),
6137 + cachep_c = kmem_cache_create("ip_set_iptreemap_c",
6138 + sizeof(struct ip_set_iptreemap_c),
6139 + 0, 0, NULL, NULL);
6142 + ip_set_printk("Unable to create ip_set_iptreemap_c slab cache");
6146 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
6147 + cachep_d = kmem_cache_create("ip_set_iptreemap_d",
6148 + sizeof(struct ip_set_iptreemap_d),
6151 + cachep_d = kmem_cache_create("ip_set_iptreemap_d",
6152 + sizeof(struct ip_set_iptreemap_d),
6153 + 0, 0, NULL, NULL);
6156 + ip_set_printk("Unable to create ip_set_iptreemap_d slab cache");
6160 + fullbitmap_d = kmem_cache_alloc(cachep_d, GFP_KERNEL);
6161 + if (!fullbitmap_d)
6164 + fullbitmap_c = kmem_cache_alloc(cachep_c, GFP_KERNEL);
6165 + if (!fullbitmap_c)
6168 + fullbitmap_b = kmem_cache_alloc(cachep_b, GFP_KERNEL);
6169 + if (!fullbitmap_b)
6172 + ret = ip_set_register_set_type(&ip_set_iptreemap);
6176 + /* Now init our global bitmaps */
6177 + memset(fullbitmap_d->bitmap, 0xff, sizeof(fullbitmap_d->bitmap));
6179 + for (a = 0; a < 256; a++)
6180 + fullbitmap_c->tree[a] = fullbitmap_d;
6182 + for (a = 0; a < 256; a++)
6183 + fullbitmap_b->tree[a] = fullbitmap_c;
6184 + memset(fullbitmap_b->dirty, 0, sizeof(fullbitmap_b->dirty));
6189 + kmem_cache_free(cachep_b, fullbitmap_b);
6191 + kmem_cache_free(cachep_c, fullbitmap_c);
6193 + kmem_cache_free(cachep_d, fullbitmap_d);
6195 + kmem_cache_destroy(cachep_d);
6197 + kmem_cache_destroy(cachep_c);
6199 + kmem_cache_destroy(cachep_b);
6205 +static void __exit ip_set_iptreemap_fini(void)
6207 + ip_set_unregister_set_type(&ip_set_iptreemap);
6208 + kmem_cache_free(cachep_d, fullbitmap_d);
6209 + kmem_cache_free(cachep_c, fullbitmap_c);
6210 + kmem_cache_free(cachep_b, fullbitmap_b);
6211 + kmem_cache_destroy(cachep_d);
6212 + kmem_cache_destroy(cachep_c);
6213 + kmem_cache_destroy(cachep_b);
6216 +module_init(ip_set_iptreemap_init);
6217 +module_exit(ip_set_iptreemap_fini);
6218 diff --git a/net/ipv4/netfilter/ip_set_macipmap.c b/net/ipv4/netfilter/ip_set_macipmap.c
6219 new file mode 100644
6220 index 0000000..8ca2159
6222 +++ b/net/ipv4/netfilter/ip_set_macipmap.c
6224 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
6225 + * Patrick Schaaf <bof@bof.de>
6226 + * Martin Josefsson <gandalf@wlug.westbo.se>
6227 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6229 + * This program is free software; you can redistribute it and/or modify
6230 + * it under the terms of the GNU General Public License version 2 as
6231 + * published by the Free Software Foundation.
6234 +/* Kernel module implementing an IP set type: the macipmap type */
6236 +#include <linux/module.h>
6237 +#include <linux/ip.h>
6238 +#include <linux/skbuff.h>
6239 +#include <linux/version.h>
6240 +#include <linux/netfilter_ipv4/ip_tables.h>
6241 +#include <linux/netfilter_ipv4/ip_set.h>
6242 +#include <linux/errno.h>
6243 +#include <asm/uaccess.h>
6244 +#include <asm/bitops.h>
6245 +#include <linux/spinlock.h>
6246 +#include <linux/if_ether.h>
6247 +#include <linux/vmalloc.h>
6249 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
6250 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
6253 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
6255 + struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
6256 + struct ip_set_macip *table = (struct ip_set_macip *) map->members;
6257 + struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
6259 + if (size != sizeof(struct ip_set_req_macipmap)) {
6260 + ip_set_printk("data length wrong (want %zu, have %zu)",
6261 + sizeof(struct ip_set_req_macipmap),
6266 + if (req->ip < map->first_ip || req->ip > map->last_ip)
6269 + *hash_ip = req->ip;
6270 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
6271 + set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
6272 + if (test_bit(IPSET_MACIP_ISSET,
6273 + (void *) &table[req->ip - map->first_ip].flags)) {
6274 + return (memcmp(req->ethernet,
6275 + &table[req->ip - map->first_ip].ethernet,
6278 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6283 +testip_kernel(struct ip_set *set,
6284 + const struct sk_buff *skb,
6285 + ip_set_ip_t *hash_ip,
6286 + const u_int32_t *flags,
6287 + unsigned char index)
6289 + struct ip_set_macipmap *map =
6290 + (struct ip_set_macipmap *) set->data;
6291 + struct ip_set_macip *table =
6292 + (struct ip_set_macip *) map->members;
6295 + ip = ntohl(flags[index] & IPSET_SRC
6296 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6297 + ? ip_hdr(skb)->saddr
6298 + : ip_hdr(skb)->daddr);
6300 + ? skb->nh.iph->saddr
6301 + : skb->nh.iph->daddr);
6304 + if (ip < map->first_ip || ip > map->last_ip)
6308 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
6309 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
6310 + if (test_bit(IPSET_MACIP_ISSET,
6311 + (void *) &table[ip - map->first_ip].flags)) {
6312 + /* Is mac pointer valid?
6313 + * If so, compare... */
6314 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6315 + return (skb_mac_header(skb) >= skb->head
6316 + && (skb_mac_header(skb) + ETH_HLEN) <= skb->data
6318 + return (skb->mac.raw >= skb->head
6319 + && (skb->mac.raw + ETH_HLEN) <= skb->data
6321 + && (memcmp(eth_hdr(skb)->h_source,
6322 + &table[ip - map->first_ip].ethernet,
6325 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6329 +/* returns 0 on success */
6331 +__addip(struct ip_set *set,
6332 + ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
6334 + struct ip_set_macipmap *map =
6335 + (struct ip_set_macipmap *) set->data;
6336 + struct ip_set_macip *table =
6337 + (struct ip_set_macip *) map->members;
6339 + if (ip < map->first_ip || ip > map->last_ip)
6341 + if (test_and_set_bit(IPSET_MACIP_ISSET,
6342 + (void *) &table[ip - map->first_ip].flags))
6346 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6347 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
6352 +addip(struct ip_set *set, const void *data, size_t size,
6353 + ip_set_ip_t *hash_ip)
6355 + struct ip_set_req_macipmap *req =
6356 + (struct ip_set_req_macipmap *) data;
6358 + if (size != sizeof(struct ip_set_req_macipmap)) {
6359 + ip_set_printk("data length wrong (want %zu, have %zu)",
6360 + sizeof(struct ip_set_req_macipmap),
6364 + return __addip(set, req->ip, req->ethernet, hash_ip);
6368 +addip_kernel(struct ip_set *set,
6369 + const struct sk_buff *skb,
6370 + ip_set_ip_t *hash_ip,
6371 + const u_int32_t *flags,
6372 + unsigned char index)
6376 + ip = ntohl(flags[index] & IPSET_SRC
6377 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6378 + ? ip_hdr(skb)->saddr
6379 + : ip_hdr(skb)->daddr);
6381 + ? skb->nh.iph->saddr
6382 + : skb->nh.iph->daddr);
6385 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6386 + if (!(skb_mac_header(skb) >= skb->head
6387 + && (skb_mac_header(skb) + ETH_HLEN) <= skb->data))
6389 + if (!(skb->mac.raw >= skb->head
6390 + && (skb->mac.raw + ETH_HLEN) <= skb->data))
6394 + return __addip(set, ip, eth_hdr(skb)->h_source, hash_ip);
6398 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6400 + struct ip_set_macipmap *map =
6401 + (struct ip_set_macipmap *) set->data;
6402 + struct ip_set_macip *table =
6403 + (struct ip_set_macip *) map->members;
6405 + if (ip < map->first_ip || ip > map->last_ip)
6407 + if (!test_and_clear_bit(IPSET_MACIP_ISSET,
6408 + (void *)&table[ip - map->first_ip].flags))
6412 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
6417 +delip(struct ip_set *set, const void *data, size_t size,
6418 + ip_set_ip_t *hash_ip)
6420 + struct ip_set_req_macipmap *req =
6421 + (struct ip_set_req_macipmap *) data;
6423 + if (size != sizeof(struct ip_set_req_macipmap)) {
6424 + ip_set_printk("data length wrong (want %zu, have %zu)",
6425 + sizeof(struct ip_set_req_macipmap),
6429 + return __delip(set, req->ip, hash_ip);
6433 +delip_kernel(struct ip_set *set,
6434 + const struct sk_buff *skb,
6435 + ip_set_ip_t *hash_ip,
6436 + const u_int32_t *flags,
6437 + unsigned char index)
6439 + return __delip(set,
6440 + ntohl(flags[index] & IPSET_SRC
6441 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6442 + ? ip_hdr(skb)->saddr
6443 + : ip_hdr(skb)->daddr),
6445 + ? skb->nh.iph->saddr
6446 + : skb->nh.iph->daddr),
6451 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
6453 + return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
6456 +static int create(struct ip_set *set, const void *data, size_t size)
6459 + struct ip_set_req_macipmap_create *req =
6460 + (struct ip_set_req_macipmap_create *) data;
6461 + struct ip_set_macipmap *map;
6463 + if (size != sizeof(struct ip_set_req_macipmap_create)) {
6464 + ip_set_printk("data length wrong (want %zu, have %zu)",
6465 + sizeof(struct ip_set_req_macipmap_create),
6470 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
6471 + HIPQUAD(req->from), HIPQUAD(req->to));
6473 + if (req->from > req->to) {
6474 + DP("bad ip range");
6478 + if (req->to - req->from > MAX_RANGE) {
6479 + ip_set_printk("range too big (max %d addresses)",
6484 + map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
6486 + DP("out of memory for %d bytes",
6487 + sizeof(struct ip_set_macipmap));
6490 + map->flags = req->flags;
6491 + map->first_ip = req->from;
6492 + map->last_ip = req->to;
6493 + newbytes = members_size(map->first_ip, map->last_ip);
6494 + map->members = ip_set_malloc(newbytes);
6495 + DP("members: %u %p", newbytes, map->members);
6496 + if (!map->members) {
6497 + DP("out of memory for %d bytes", newbytes);
6501 + memset(map->members, 0, newbytes);
6507 +static void destroy(struct ip_set *set)
6509 + struct ip_set_macipmap *map =
6510 + (struct ip_set_macipmap *) set->data;
6512 + ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
6518 +static void flush(struct ip_set *set)
6520 + struct ip_set_macipmap *map =
6521 + (struct ip_set_macipmap *) set->data;
6522 + memset(map->members, 0, members_size(map->first_ip, map->last_ip));
6525 +static void list_header(const struct ip_set *set, void *data)
6527 + struct ip_set_macipmap *map =
6528 + (struct ip_set_macipmap *) set->data;
6529 + struct ip_set_req_macipmap_create *header =
6530 + (struct ip_set_req_macipmap_create *) data;
6532 + DP("list_header %x %x %u", map->first_ip, map->last_ip,
6535 + header->from = map->first_ip;
6536 + header->to = map->last_ip;
6537 + header->flags = map->flags;
6540 +static int list_members_size(const struct ip_set *set)
6542 + struct ip_set_macipmap *map =
6543 + (struct ip_set_macipmap *) set->data;
6545 + DP("%u", members_size(map->first_ip, map->last_ip));
6546 + return members_size(map->first_ip, map->last_ip);
6549 +static void list_members(const struct ip_set *set, void *data)
6551 + struct ip_set_macipmap *map =
6552 + (struct ip_set_macipmap *) set->data;
6554 + int bytes = members_size(map->first_ip, map->last_ip);
6556 + DP("members: %u %p", bytes, map->members);
6557 + memcpy(data, map->members, bytes);
6560 +static struct ip_set_type ip_set_macipmap = {
6561 + .typename = SETTYPE_NAME,
6562 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
6563 + .protocol_version = IP_SET_PROTOCOL_VERSION,
6564 + .create = &create,
6565 + .destroy = &destroy,
6567 + .reqsize = sizeof(struct ip_set_req_macipmap),
6569 + .addip_kernel = &addip_kernel,
6571 + .delip_kernel = &delip_kernel,
6572 + .testip = &testip,
6573 + .testip_kernel = &testip_kernel,
6574 + .header_size = sizeof(struct ip_set_req_macipmap_create),
6575 + .list_header = &list_header,
6576 + .list_members_size = &list_members_size,
6577 + .list_members = &list_members,
6578 + .me = THIS_MODULE,
6581 +MODULE_LICENSE("GPL");
6582 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6583 +MODULE_DESCRIPTION("macipmap type of IP sets");
6585 +static int __init ip_set_macipmap_init(void)
6587 + init_max_malloc_size();
6588 + return ip_set_register_set_type(&ip_set_macipmap);
6591 +static void __exit ip_set_macipmap_fini(void)
6593 + /* FIXME: possible race with ip_set_create() */
6594 + ip_set_unregister_set_type(&ip_set_macipmap);
6597 +module_init(ip_set_macipmap_init);
6598 +module_exit(ip_set_macipmap_fini);
6599 diff --git a/net/ipv4/netfilter/ip_set_nethash.c b/net/ipv4/netfilter/ip_set_nethash.c
6600 new file mode 100644
6601 index 0000000..d94cc32
6603 +++ b/net/ipv4/netfilter/ip_set_nethash.c
6605 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6607 + * This program is free software; you can redistribute it and/or modify
6608 + * it under the terms of the GNU General Public License version 2 as
6609 + * published by the Free Software Foundation.
6612 +/* Kernel module implementing a cidr nethash set */
6614 +#include <linux/module.h>
6615 +#include <linux/ip.h>
6616 +#include <linux/skbuff.h>
6617 +#include <linux/version.h>
6618 +#include <linux/jhash.h>
6619 +#include <linux/netfilter_ipv4/ip_tables.h>
6620 +#include <linux/netfilter_ipv4/ip_set.h>
6621 +#include <linux/errno.h>
6622 +#include <asm/uaccess.h>
6623 +#include <asm/bitops.h>
6624 +#include <linux/spinlock.h>
6625 +#include <linux/vmalloc.h>
6626 +#include <linux/random.h>
6628 +#include <net/ip.h>
6630 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
6631 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
6633 +static int limit = MAX_RANGE;
6635 +static inline __u32
6636 +jhash_ip(const struct ip_set_nethash *map, uint16_t i, ip_set_ip_t ip)
6638 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
6641 +static inline __u32
6642 +hash_id_cidr(struct ip_set_nethash *map,
6644 + unsigned char cidr,
6645 + ip_set_ip_t *hash_ip)
6649 + ip_set_ip_t *elem;
6651 + *hash_ip = pack(ip, cidr);
6653 + for (i = 0; i < map->probes; i++) {
6654 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
6655 + DP("hash key: %u", id);
6656 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6657 + if (*elem == *hash_ip)
6663 +static inline __u32
6664 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6666 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6667 + __u32 id = UINT_MAX;
6670 + for (i = 0; i < 30 && map->cidr[i]; i++) {
6671 + id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
6672 + if (id != UINT_MAX)
6679 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
6680 + ip_set_ip_t *hash_ip)
6682 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6684 + return (ip && hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
6688 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
6690 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
6694 +testip(struct ip_set *set, const void *data, size_t size,
6695 + ip_set_ip_t *hash_ip)
6697 + struct ip_set_req_nethash *req =
6698 + (struct ip_set_req_nethash *) data;
6700 + if (size != sizeof(struct ip_set_req_nethash)) {
6701 + ip_set_printk("data length wrong (want %zu, have %zu)",
6702 + sizeof(struct ip_set_req_nethash),
6706 + return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
6707 + : __testip_cidr(set, req->ip, req->cidr, hash_ip));
6711 +testip_kernel(struct ip_set *set,
6712 + const struct sk_buff *skb,
6713 + ip_set_ip_t *hash_ip,
6714 + const u_int32_t *flags,
6715 + unsigned char index)
6717 + return __testip(set,
6718 + ntohl(flags[index] & IPSET_SRC
6719 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6720 + ? ip_hdr(skb)->saddr
6721 + : ip_hdr(skb)->daddr),
6723 + ? skb->nh.iph->saddr
6724 + : skb->nh.iph->daddr),
6730 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
6734 + ip_set_ip_t *elem;
6736 + for (i = 0; i < map->probes; i++) {
6737 + probe = jhash_ip(map, i, ip) % map->hashsize;
6738 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
6747 + /* Trigger rehashing */
6752 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6753 + ip_set_ip_t *hash_ip)
6755 + if (!ip || map->elements >= limit)
6758 + *hash_ip = pack(ip, cidr);
6759 + DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
6761 + return __addip_base(map, *hash_ip);
6765 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
6767 + unsigned char next;
6770 + for (i = 0; i < 30 && map->cidr[i]; i++) {
6771 + if (map->cidr[i] == cidr) {
6773 + } else if (map->cidr[i] < cidr) {
6774 + next = map->cidr[i];
6775 + map->cidr[i] = cidr;
6780 + map->cidr[i] = cidr;
6784 +addip(struct ip_set *set, const void *data, size_t size,
6785 + ip_set_ip_t *hash_ip)
6787 + struct ip_set_req_nethash *req =
6788 + (struct ip_set_req_nethash *) data;
6791 + if (size != sizeof(struct ip_set_req_nethash)) {
6792 + ip_set_printk("data length wrong (want %zu, have %zu)",
6793 + sizeof(struct ip_set_req_nethash),
6797 + ret = __addip((struct ip_set_nethash *) set->data,
6798 + req->ip, req->cidr, hash_ip);
6801 + update_cidr_sizes((struct ip_set_nethash *) set->data,
6808 +addip_kernel(struct ip_set *set,
6809 + const struct sk_buff *skb,
6810 + ip_set_ip_t *hash_ip,
6811 + const u_int32_t *flags,
6812 + unsigned char index)
6814 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6815 + int ret = -ERANGE;
6816 + ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC
6817 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6818 + ? ip_hdr(skb)->saddr
6819 + : ip_hdr(skb)->daddr);
6821 + ? skb->nh.iph->saddr
6822 + : skb->nh.iph->daddr);
6826 + ret = __addip(map, ip, map->cidr[0], hash_ip);
6831 +static int retry(struct ip_set *set)
6833 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6834 + ip_set_ip_t *elem;
6836 + u_int32_t i, hashsize = map->hashsize;
6838 + struct ip_set_nethash *tmp;
6840 + if (map->resize == 0)
6846 + /* Calculate new parameters */
6847 + hashsize += (hashsize * map->resize)/100;
6848 + if (hashsize == map->hashsize)
6851 + ip_set_printk("rehashing of set %s triggered: "
6852 + "hashsize grows from %u to %u",
6853 + set->name, map->hashsize, hashsize);
6855 + tmp = kmalloc(sizeof(struct ip_set_nethash)
6856 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
6858 + DP("out of memory for %d bytes",
6859 + sizeof(struct ip_set_nethash)
6860 + + map->probes * sizeof(uint32_t));
6863 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
6864 + if (!tmp->members) {
6865 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
6869 + tmp->hashsize = hashsize;
6870 + tmp->elements = 0;
6871 + tmp->probes = map->probes;
6872 + tmp->resize = map->resize;
6873 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
6874 + memcpy(tmp->cidr, map->cidr, 30 * sizeof(unsigned char));
6876 + write_lock_bh(&set->lock);
6877 + map = (struct ip_set_nethash *) set->data; /* Play safe */
6878 + for (i = 0; i < map->hashsize && res == 0; i++) {
6879 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
6881 + res = __addip_base(tmp, *elem);
6884 + /* Failure, try again */
6885 + write_unlock_bh(&set->lock);
6886 + harray_free(tmp->members);
6891 + /* Success at resizing! */
6892 + members = map->members;
6894 + map->hashsize = tmp->hashsize;
6895 + map->members = tmp->members;
6896 + write_unlock_bh(&set->lock);
6898 + harray_free(members);
6905 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
6906 + ip_set_ip_t *hash_ip)
6908 + ip_set_ip_t id, *elem;
6913 + id = hash_id_cidr(map, ip, cidr, hash_ip);
6914 + if (id == UINT_MAX)
6917 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6924 +delip(struct ip_set *set, const void *data, size_t size,
6925 + ip_set_ip_t *hash_ip)
6927 + struct ip_set_req_nethash *req =
6928 + (struct ip_set_req_nethash *) data;
6930 + if (size != sizeof(struct ip_set_req_nethash)) {
6931 + ip_set_printk("data length wrong (want %zu, have %zu)",
6932 + sizeof(struct ip_set_req_nethash),
6936 + /* TODO: no garbage collection in map->cidr */
6937 + return __delip((struct ip_set_nethash *) set->data,
6938 + req->ip, req->cidr, hash_ip);
6942 +delip_kernel(struct ip_set *set,
6943 + const struct sk_buff *skb,
6944 + ip_set_ip_t *hash_ip,
6945 + const u_int32_t *flags,
6946 + unsigned char index)
6948 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
6949 + int ret = -ERANGE;
6950 + ip_set_ip_t ip = ntohl(flags[index] & IPSET_SRC
6951 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
6952 + ? ip_hdr(skb)->saddr
6953 + : ip_hdr(skb)->daddr);
6955 + ? skb->nh.iph->saddr
6956 + : skb->nh.iph->daddr);
6960 + ret = __delip(map, ip, map->cidr[0], hash_ip);
6965 +static int create(struct ip_set *set, const void *data, size_t size)
6967 + struct ip_set_req_nethash_create *req =
6968 + (struct ip_set_req_nethash_create *) data;
6969 + struct ip_set_nethash *map;
6972 + if (size != sizeof(struct ip_set_req_nethash_create)) {
6973 + ip_set_printk("data length wrong (want %zu, have %zu)",
6974 + sizeof(struct ip_set_req_nethash_create),
6979 + if (req->hashsize < 1) {
6980 + ip_set_printk("hashsize too small");
6983 + if (req->probes < 1) {
6984 + ip_set_printk("probes too small");
6988 + map = kmalloc(sizeof(struct ip_set_nethash)
6989 + + req->probes * sizeof(uint32_t), GFP_KERNEL);
6991 + DP("out of memory for %d bytes",
6992 + sizeof(struct ip_set_nethash)
6993 + + req->probes * sizeof(uint32_t));
6996 + for (i = 0; i < req->probes; i++)
6997 + get_random_bytes(((uint32_t *) map->initval)+i, 4);
6998 + map->elements = 0;
6999 + map->hashsize = req->hashsize;
7000 + map->probes = req->probes;
7001 + map->resize = req->resize;
7002 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
7003 + map->members = harray_malloc(map->hashsize, sizeof(ip_set_ip_t), GFP_KERNEL);
7004 + if (!map->members) {
7005 + DP("out of memory for %d bytes", map->hashsize * sizeof(ip_set_ip_t));
7014 +static void destroy(struct ip_set *set)
7016 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
7018 + harray_free(map->members);
7024 +static void flush(struct ip_set *set)
7026 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
7027 + harray_flush(map->members, map->hashsize, sizeof(ip_set_ip_t));
7028 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
7029 + map->elements = 0;
7032 +static void list_header(const struct ip_set *set, void *data)
7034 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
7035 + struct ip_set_req_nethash_create *header =
7036 + (struct ip_set_req_nethash_create *) data;
7038 + header->hashsize = map->hashsize;
7039 + header->probes = map->probes;
7040 + header->resize = map->resize;
7043 +static int list_members_size(const struct ip_set *set)
7045 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
7047 + return (map->hashsize * sizeof(ip_set_ip_t));
7050 +static void list_members(const struct ip_set *set, void *data)
7052 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
7053 + ip_set_ip_t i, *elem;
7055 + for (i = 0; i < map->hashsize; i++) {
7056 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
7057 + ((ip_set_ip_t *)data)[i] = *elem;
7061 +static struct ip_set_type ip_set_nethash = {
7062 + .typename = SETTYPE_NAME,
7063 + .features = IPSET_TYPE_IP | IPSET_DATA_SINGLE,
7064 + .protocol_version = IP_SET_PROTOCOL_VERSION,
7065 + .create = &create,
7066 + .destroy = &destroy,
7068 + .reqsize = sizeof(struct ip_set_req_nethash),
7070 + .addip_kernel = &addip_kernel,
7073 + .delip_kernel = &delip_kernel,
7074 + .testip = &testip,
7075 + .testip_kernel = &testip_kernel,
7076 + .header_size = sizeof(struct ip_set_req_nethash_create),
7077 + .list_header = &list_header,
7078 + .list_members_size = &list_members_size,
7079 + .list_members = &list_members,
7080 + .me = THIS_MODULE,
7083 +MODULE_LICENSE("GPL");
7084 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7085 +MODULE_DESCRIPTION("nethash type of IP sets");
7086 +module_param(limit, int, 0600);
7087 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
7089 +static int __init ip_set_nethash_init(void)
7091 + return ip_set_register_set_type(&ip_set_nethash);
7094 +static void __exit ip_set_nethash_fini(void)
7096 + /* FIXME: possible race with ip_set_create() */
7097 + ip_set_unregister_set_type(&ip_set_nethash);
7100 +module_init(ip_set_nethash_init);
7101 +module_exit(ip_set_nethash_fini);
7102 diff --git a/net/ipv4/netfilter/ip_set_portmap.c b/net/ipv4/netfilter/ip_set_portmap.c
7103 new file mode 100644
7104 index 0000000..9dba9cf
7106 +++ b/net/ipv4/netfilter/ip_set_portmap.c
7108 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7110 + * This program is free software; you can redistribute it and/or modify
7111 + * it under the terms of the GNU General Public License version 2 as
7112 + * published by the Free Software Foundation.
7115 +/* Kernel module implementing a port set type as a bitmap */
7117 +#include <linux/module.h>
7118 +#include <linux/ip.h>
7119 +#include <linux/tcp.h>
7120 +#include <linux/udp.h>
7121 +#include <linux/skbuff.h>
7122 +#include <linux/version.h>
7123 +#include <linux/netfilter_ipv4/ip_tables.h>
7124 +#include <linux/netfilter_ipv4/ip_set.h>
7125 +#include <linux/errno.h>
7126 +#include <asm/uaccess.h>
7127 +#include <asm/bitops.h>
7128 +#include <linux/spinlock.h>
7130 +#include <net/ip.h>
7132 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
7134 +/* We must handle non-linear skbs */
7135 +static inline ip_set_ip_t
7136 +get_port(const struct sk_buff *skb, u_int32_t flags)
7138 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
7139 + struct iphdr *iph = ip_hdr(skb);
7141 + struct iphdr *iph = skb->nh.iph;
7143 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
7144 + switch (iph->protocol) {
7145 + case IPPROTO_TCP: {
7146 + struct tcphdr tcph;
7148 + /* See comments at tcp_match in ip_tables.c */
7150 + return INVALID_PORT;
7152 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
7153 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
7155 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) < 0)
7157 + /* No choice either */
7158 + return INVALID_PORT;
7160 + return ntohs(flags & IPSET_SRC ?
7161 + tcph.source : tcph.dest);
7163 + case IPPROTO_UDP: {
7164 + struct udphdr udph;
7167 + return INVALID_PORT;
7169 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
7170 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
7172 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &udph, sizeof(udph)) < 0)
7174 + /* No choice either */
7175 + return INVALID_PORT;
7177 + return ntohs(flags & IPSET_SRC ?
7178 + udph.source : udph.dest);
7181 + return INVALID_PORT;
7186 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
7188 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7190 + if (port < map->first_port || port > map->last_port)
7193 + *hash_port = port;
7194 + DP("set: %s, port:%u, %u", set->name, port, *hash_port);
7195 + return !!test_bit(port - map->first_port, map->members);
7199 +testport(struct ip_set *set, const void *data, size_t size,
7200 + ip_set_ip_t *hash_port)
7202 + struct ip_set_req_portmap *req =
7203 + (struct ip_set_req_portmap *) data;
7205 + if (size != sizeof(struct ip_set_req_portmap)) {
7206 + ip_set_printk("data length wrong (want %zu, have %zu)",
7207 + sizeof(struct ip_set_req_portmap),
7211 + return __testport(set, req->port, hash_port);
7215 +testport_kernel(struct ip_set *set,
7216 + const struct sk_buff *skb,
7217 + ip_set_ip_t *hash_port,
7218 + const u_int32_t *flags,
7219 + unsigned char index)
7222 + ip_set_ip_t port = get_port(skb, flags[index]);
7224 + DP("flag %s port %u", flags[index] & IPSET_SRC ? "SRC" : "DST", port);
7225 + if (port == INVALID_PORT)
7228 + res = __testport(set, port, hash_port);
7230 + return (res < 0 ? 0 : res);
7234 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
7236 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7238 + if (port < map->first_port || port > map->last_port)
7240 + if (test_and_set_bit(port - map->first_port, map->members))
7243 + *hash_port = port;
7244 + DP("port %u", port);
7249 +addport(struct ip_set *set, const void *data, size_t size,
7250 + ip_set_ip_t *hash_port)
7252 + struct ip_set_req_portmap *req =
7253 + (struct ip_set_req_portmap *) data;
7255 + if (size != sizeof(struct ip_set_req_portmap)) {
7256 + ip_set_printk("data length wrong (want %zu, have %zu)",
7257 + sizeof(struct ip_set_req_portmap),
7261 + return __addport(set, req->port, hash_port);
7265 +addport_kernel(struct ip_set *set,
7266 + const struct sk_buff *skb,
7267 + ip_set_ip_t *hash_port,
7268 + const u_int32_t *flags,
7269 + unsigned char index)
7271 + ip_set_ip_t port = get_port(skb, flags[index]);
7273 + if (port == INVALID_PORT)
7276 + return __addport(set, port, hash_port);
7280 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
7282 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7284 + if (port < map->first_port || port > map->last_port)
7286 + if (!test_and_clear_bit(port - map->first_port, map->members))
7289 + *hash_port = port;
7290 + DP("port %u", port);
7295 +delport(struct ip_set *set, const void *data, size_t size,
7296 + ip_set_ip_t *hash_port)
7298 + struct ip_set_req_portmap *req =
7299 + (struct ip_set_req_portmap *) data;
7301 + if (size != sizeof(struct ip_set_req_portmap)) {
7302 + ip_set_printk("data length wrong (want %zu, have %zu)",
7303 + sizeof(struct ip_set_req_portmap),
7307 + return __delport(set, req->port, hash_port);
7311 +delport_kernel(struct ip_set *set,
7312 + const struct sk_buff *skb,
7313 + ip_set_ip_t *hash_port,
7314 + const u_int32_t *flags,
7315 + unsigned char index)
7317 + ip_set_ip_t port = get_port(skb, flags[index]);
7319 + if (port == INVALID_PORT)
7322 + return __delport(set, port, hash_port);
7325 +static int create(struct ip_set *set, const void *data, size_t size)
7328 + struct ip_set_req_portmap_create *req =
7329 + (struct ip_set_req_portmap_create *) data;
7330 + struct ip_set_portmap *map;
7332 + if (size != sizeof(struct ip_set_req_portmap_create)) {
7333 + ip_set_printk("data length wrong (want %zu, have %zu)",
7334 + sizeof(struct ip_set_req_portmap_create),
7339 + DP("from %u to %u", req->from, req->to);
7341 + if (req->from > req->to) {
7342 + DP("bad port range");
7346 + if (req->to - req->from > MAX_RANGE) {
7347 + ip_set_printk("range too big (max %d ports)",
7352 + map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
7354 + DP("out of memory for %d bytes",
7355 + sizeof(struct ip_set_portmap));
7358 + map->first_port = req->from;
7359 + map->last_port = req->to;
7360 + newbytes = bitmap_bytes(req->from, req->to);
7361 + map->members = kmalloc(newbytes, GFP_KERNEL);
7362 + if (!map->members) {
7363 + DP("out of memory for %d bytes", newbytes);
7367 + memset(map->members, 0, newbytes);
7373 +static void destroy(struct ip_set *set)
7375 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7377 + kfree(map->members);
7383 +static void flush(struct ip_set *set)
7385 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7386 + memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
7389 +static void list_header(const struct ip_set *set, void *data)
7391 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7392 + struct ip_set_req_portmap_create *header =
7393 + (struct ip_set_req_portmap_create *) data;
7395 + DP("list_header %u %u", map->first_port, map->last_port);
7397 + header->from = map->first_port;
7398 + header->to = map->last_port;
7401 +static int list_members_size(const struct ip_set *set)
7403 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7405 + return bitmap_bytes(map->first_port, map->last_port);
7408 +static void list_members(const struct ip_set *set, void *data)
7410 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
7411 + int bytes = bitmap_bytes(map->first_port, map->last_port);
7413 + memcpy(data, map->members, bytes);
7416 +static struct ip_set_type ip_set_portmap = {
7417 + .typename = SETTYPE_NAME,
7418 + .features = IPSET_TYPE_PORT | IPSET_DATA_SINGLE,
7419 + .protocol_version = IP_SET_PROTOCOL_VERSION,
7420 + .create = &create,
7421 + .destroy = &destroy,
7423 + .reqsize = sizeof(struct ip_set_req_portmap),
7424 + .addip = &addport,
7425 + .addip_kernel = &addport_kernel,
7426 + .delip = &delport,
7427 + .delip_kernel = &delport_kernel,
7428 + .testip = &testport,
7429 + .testip_kernel = &testport_kernel,
7430 + .header_size = sizeof(struct ip_set_req_portmap_create),
7431 + .list_header = &list_header,
7432 + .list_members_size = &list_members_size,
7433 + .list_members = &list_members,
7434 + .me = THIS_MODULE,
7437 +MODULE_LICENSE("GPL");
7438 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7439 +MODULE_DESCRIPTION("portmap type of IP sets");
7441 +static int __init ip_set_portmap_init(void)
7443 + return ip_set_register_set_type(&ip_set_portmap);
7446 +static void __exit ip_set_portmap_fini(void)
7448 + /* FIXME: possible race with ip_set_create() */
7449 + ip_set_unregister_set_type(&ip_set_portmap);
7452 +module_init(ip_set_portmap_init);
7453 +module_exit(ip_set_portmap_fini);
7454 diff --git a/net/ipv4/netfilter/ipt_SET.c b/net/ipv4/netfilter/ipt_SET.c
7455 new file mode 100644
7456 index 0000000..2052c0c
7458 +++ b/net/ipv4/netfilter/ipt_SET.c
7460 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7461 + * Patrick Schaaf <bof@bof.de>
7462 + * Martin Josefsson <gandalf@wlug.westbo.se>
7463 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7465 + * This program is free software; you can redistribute it and/or modify
7466 + * it under the terms of the GNU General Public License version 2 as
7467 + * published by the Free Software Foundation.
7470 +/* ipt_SET.c - netfilter target to manipulate IP sets */
7472 +#include <linux/types.h>
7473 +#include <linux/ip.h>
7474 +#include <linux/timer.h>
7475 +#include <linux/module.h>
7476 +#include <linux/netfilter.h>
7477 +#include <linux/netdevice.h>
7478 +#include <linux/if.h>
7479 +#include <linux/inetdevice.h>
7480 +#include <linux/version.h>
7481 +#include <net/protocol.h>
7482 +#include <net/checksum.h>
7483 +#include <linux/netfilter_ipv4.h>
7484 +#include <linux/netfilter_ipv4/ip_tables.h>
7485 +#include <linux/netfilter_ipv4/ipt_set.h>
7487 +static unsigned int target(struct sk_buff *skb,
7488 + const struct net_device *in,
7489 + const struct net_device *out,
7490 + unsigned int hooknum,
7491 + const struct xt_target *target,
7492 + const void *targinfo)
7494 + const struct ipt_set_info_target *info = targinfo;
7496 + if (info->add_set.index != IP_SET_INVALID_ID)
7497 + ip_set_addip_kernel(info->add_set.index, skb,
7498 + info->add_set.flags);
7499 + if (info->del_set.index != IP_SET_INVALID_ID)
7500 + ip_set_delip_kernel(info->del_set.index, skb,
7501 + info->del_set.flags);
7503 + return IPT_CONTINUE;
7506 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7511 +checkentry(const char *tablename,
7512 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7515 + const struct ipt_entry *e,
7517 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7518 + const struct xt_target *target,
7521 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7522 + unsigned int targinfosize,
7524 + unsigned int hook_mask)
7526 + struct ipt_set_info_target *info =
7527 + (struct ipt_set_info_target *) targinfo;
7528 + ip_set_id_t index;
7530 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7531 + if (targinfosize != IPT_ALIGN(sizeof(*info))) {
7532 + DP("bad target info size %u", targinfosize);
7537 + if (info->add_set.index != IP_SET_INVALID_ID) {
7538 + index = ip_set_get_byindex(info->add_set.index);
7539 + if (index == IP_SET_INVALID_ID) {
7540 + ip_set_printk("cannot find add_set index %u as target",
7541 + info->add_set.index);
7542 + return 0; /* error */
7546 + if (info->del_set.index != IP_SET_INVALID_ID) {
7547 + index = ip_set_get_byindex(info->del_set.index);
7548 + if (index == IP_SET_INVALID_ID) {
7549 + ip_set_printk("cannot find del_set index %u as target",
7550 + info->del_set.index);
7551 + return 0; /* error */
7554 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
7555 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7556 + ip_set_printk("That's nasty!");
7557 + return 0; /* error */
7563 +static void destroy(
7564 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7565 + const struct xt_target *target,
7567 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7568 + void *targetinfo, unsigned int targetsize)
7573 + struct ipt_set_info_target *info = targetinfo;
7575 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7576 + if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
7577 + ip_set_printk("invalid targetsize %d", targetsize);
7581 + if (info->add_set.index != IP_SET_INVALID_ID)
7582 + ip_set_put(info->add_set.index);
7583 + if (info->del_set.index != IP_SET_INVALID_ID)
7584 + ip_set_put(info->del_set.index);
7587 +static struct ipt_target SET_target = {
7589 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7590 + .family = AF_INET,
7593 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7594 + .targetsize = sizeof(struct ipt_set_info_target),
7596 + .checkentry = checkentry,
7597 + .destroy = destroy,
7601 +MODULE_LICENSE("GPL");
7602 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7603 +MODULE_DESCRIPTION("iptables IP set target module");
7605 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7606 +#define ipt_register_target xt_register_target
7607 +#define ipt_unregister_target xt_unregister_target
7610 +static int __init ipt_SET_init(void)
7612 + return ipt_register_target(&SET_target);
7615 +static void __exit ipt_SET_fini(void)
7617 + ipt_unregister_target(&SET_target);
7620 +module_init(ipt_SET_init);
7621 +module_exit(ipt_SET_fini);
7622 diff --git a/net/ipv4/netfilter/ipt_set.c b/net/ipv4/netfilter/ipt_set.c
7623 new file mode 100644
7624 index 0000000..f192013
7626 +++ b/net/ipv4/netfilter/ipt_set.c
7628 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7629 + * Patrick Schaaf <bof@bof.de>
7630 + * Martin Josefsson <gandalf@wlug.westbo.se>
7631 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7633 + * This program is free software; you can redistribute it and/or modify
7634 + * it under the terms of the GNU General Public License version 2 as
7635 + * published by the Free Software Foundation.
7638 +/* Kernel module to match an IP set. */
7640 +#include <linux/module.h>
7641 +#include <linux/ip.h>
7642 +#include <linux/skbuff.h>
7643 +#include <linux/version.h>
7645 +#include <linux/netfilter_ipv4/ip_tables.h>
7646 +#include <linux/netfilter_ipv4/ip_set.h>
7647 +#include <linux/netfilter_ipv4/ipt_set.h>
7650 +match_set(const struct ipt_set_info *info,
7651 + const struct sk_buff *skb,
7654 + if (ip_set_testip_kernel(info->index, skb, info->flags))
7659 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7664 +match(const struct sk_buff *skb,
7665 + const struct net_device *in,
7666 + const struct net_device *out,
7667 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7668 + const struct xt_match *match,
7670 + const void *matchinfo,
7671 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7672 + int offset, unsigned int protoff, bool *hotdrop)
7673 +#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7674 + int offset, unsigned int protoff, int *hotdrop)
7676 + int offset, int *hotdrop)
7679 + const struct ipt_set_info_match *info = matchinfo;
7681 + return match_set(&info->match_set,
7683 + info->match_set.flags[0] & IPSET_MATCH_INV);
7686 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
7691 +checkentry(const char *tablename,
7692 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
7695 + const struct ipt_ip *ip,
7697 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7698 + const struct xt_match *match,
7701 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7702 + unsigned int matchsize,
7704 + unsigned int hook_mask)
7706 + struct ipt_set_info_match *info =
7707 + (struct ipt_set_info_match *) matchinfo;
7708 + ip_set_id_t index;
7710 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7711 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7712 + ip_set_printk("invalid matchsize %d", matchsize);
7717 + index = ip_set_get_byindex(info->match_set.index);
7719 + if (index == IP_SET_INVALID_ID) {
7720 + ip_set_printk("Cannot find set indentified by id %u to match",
7721 + info->match_set.index);
7722 + return 0; /* error */
7724 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7725 + ip_set_printk("That's nasty!");
7726 + return 0; /* error */
7732 +static void destroy(
7733 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7734 + const struct xt_match *match,
7736 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7737 + void *matchinfo, unsigned int matchsize)
7742 + struct ipt_set_info_match *info = matchinfo;
7744 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7745 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7746 + ip_set_printk("invalid matchsize %d", matchsize);
7750 + ip_set_put(info->match_set.index);
7753 +static struct ipt_match set_match = {
7755 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7756 + .family = AF_INET,
7759 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
7760 + .matchsize = sizeof(struct ipt_set_info_match),
7762 + .checkentry = &checkentry,
7763 + .destroy = &destroy,
7767 +MODULE_LICENSE("GPL");
7768 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7769 +MODULE_DESCRIPTION("iptables IP set match module");
7771 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
7772 +#define ipt_register_match xt_register_match
7773 +#define ipt_unregister_match xt_unregister_match
7776 +static int __init ipt_ipset_init(void)
7778 + return ipt_register_match(&set_match);
7781 +static void __exit ipt_ipset_fini(void)
7783 + ipt_unregister_match(&set_match);
7786 +module_init(ipt_ipset_init);
7787 +module_exit(ipt_ipset_fini);