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 | 561 +++++++
8 include/linux/netfilter_ipv4/ip_set_bitmaps.h | 120 ++
9 include/linux/netfilter_ipv4/ip_set_compat.h | 92 ++
10 include/linux/netfilter_ipv4/ip_set_getport.h | 48 +
11 include/linux/netfilter_ipv4/ip_set_hashes.h | 314 ++++
12 include/linux/netfilter_ipv4/ip_set_iphash.h | 30 +
13 include/linux/netfilter_ipv4/ip_set_ipmap.h | 57 +
14 include/linux/netfilter_ipv4/ip_set_ipporthash.h | 33 +
15 include/linux/netfilter_ipv4/ip_set_ipportiphash.h | 39 +
16 .../linux/netfilter_ipv4/ip_set_ipportnethash.h | 42 +
17 include/linux/netfilter_ipv4/ip_set_iptree.h | 39 +
18 include/linux/netfilter_ipv4/ip_set_iptreemap.h | 40 +
19 include/linux/netfilter_ipv4/ip_set_jhash.h | 157 ++
20 include/linux/netfilter_ipv4/ip_set_macipmap.h | 39 +
21 include/linux/netfilter_ipv4/ip_set_malloc.h | 153 ++
22 include/linux/netfilter_ipv4/ip_set_nethash.h | 31 +
23 include/linux/netfilter_ipv4/ip_set_portmap.h | 25 +
24 include/linux/netfilter_ipv4/ip_set_setlist.h | 26 +
25 include/linux/netfilter_ipv4/ipt_set.h | 21 +
26 net/ipv4/netfilter/Kconfig | 117 ++
27 net/ipv4/netfilter/Makefile | 13 +
28 net/ipv4/netfilter/ip_set.c | 1531 ++++++++++++++++++++
29 net/ipv4/netfilter/ip_set_iphash.c | 164 +++
30 net/ipv4/netfilter/ip_set_ipmap.c | 158 ++
31 net/ipv4/netfilter/ip_set_ipporthash.c | 197 +++
32 net/ipv4/netfilter/ip_set_ipportiphash.c | 215 +++
33 net/ipv4/netfilter/ip_set_ipportnethash.c | 298 ++++
34 net/ipv4/netfilter/ip_set_iptree.c | 464 ++++++
35 net/ipv4/netfilter/ip_set_iptreemap.c | 699 +++++++++
36 net/ipv4/netfilter/ip_set_macipmap.c | 179 +++
37 net/ipv4/netfilter/ip_set_nethash.c | 218 +++
38 net/ipv4/netfilter/ip_set_portmap.c | 130 ++
39 net/ipv4/netfilter/ip_set_setlist.c | 324 +++++
40 net/ipv4/netfilter/ipt_SET.c | 256 ++++
41 net/ipv4/netfilter/ipt_set.c | 253 ++++
42 35 files changed, 7083 insertions(+), 0 deletions(-)
44 diff --git a/include/linux/netfilter_ipv4/ip_set.h b/include/linux/netfilter_ipv4/ip_set.h
46 index 0000000..da17319
48 +++ b/include/linux/netfilter_ipv4/ip_set.h
53 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
54 + * Patrick Schaaf <bof@bof.de>
55 + * Martin Josefsson <gandalf@wlug.westbo.se>
56 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
58 + * This program is free software; you can redistribute it and/or modify
59 + * it under the terms of the GNU General Public License version 2 as
60 + * published by the Free Software Foundation.
68 + * A sockopt of such quality has hardly ever been seen before on the open
69 + * market! This little beauty, hardly ever used: above 64, so it's
70 + * traditionally used for firewalling, not touched (even once!) by the
71 + * 2.0, 2.2 and 2.4 kernels!
73 + * Comes with its own certificate of authenticity, valid anywhere in the
81 + * Heavily modify by Joakim Axelsson 08.03.2002
82 + * - Made it more modulebased
84 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
86 + * - in order to "deal with" backward compatibility, renamed to ipset
90 + * Used so that the kernel module and ipset-binary can match their versions
92 +#define IP_SET_PROTOCOL_UNALIGNED 3
93 +#define IP_SET_PROTOCOL_VERSION 4
95 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
97 +/* Lets work with our own typedef for representing an IP address.
98 + * We hope to make the code more portable, possibly to IPv6...
100 + * The representation works in HOST byte order, because most set types
101 + * will perform arithmetic operations and compare operations.
103 + * For now the type is an uint32_t.
105 + * Make sure to ONLY use the functions when translating and parsing
106 + * in order to keep the host byte order and make it more portable:
109 + * parse_ipandmask()
111 + * (Joakim: where are they???)
114 +typedef uint32_t ip_set_ip_t;
116 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
117 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
119 +typedef uint16_t ip_set_id_t;
121 +#define IP_SET_INVALID_ID 65535
123 +/* How deep we follow bindings */
124 +#define IP_SET_MAX_BINDINGS 6
127 + * Option flags for kernel operations (ipt_set_info)
129 +#define IPSET_SRC 0x01 /* Source match/add */
130 +#define IPSET_DST 0x02 /* Destination match/add */
131 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
136 +#define IPSET_TYPE_IP 0x01 /* IP address type of set */
137 +#define IPSET_TYPE_PORT 0x02 /* Port type of set */
138 +#define IPSET_DATA_SINGLE 0x04 /* Single data storage */
139 +#define IPSET_DATA_DOUBLE 0x08 /* Double data storage */
140 +#define IPSET_DATA_TRIPLE 0x10 /* Triple data storage */
141 +#define IPSET_TYPE_IP1 0x20 /* IP address type of set */
142 +#define IPSET_TYPE_SETNAME 0x40 /* setname type of set */
144 +/* Reserved keywords */
145 +#define IPSET_TOKEN_DEFAULT ":default:"
146 +#define IPSET_TOKEN_ALL ":all:"
148 +/* SO_IP_SET operation constants, and their request struct types.
151 + * 0-99: commands with version checking
152 + * 100-199: add/del/test/bind/unbind
153 + * 200-299: list, save, restore
156 +/* Single shot operations:
157 + * version, create, destroy, flush, rename and swap
159 + * Sets are identified by name.
162 +#define IP_SET_REQ_STD \
164 + unsigned version; \
165 + char name[IP_SET_MAXNAMELEN]
167 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
168 +struct ip_set_req_create {
170 + char typename[IP_SET_MAXNAMELEN];
173 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
174 +struct ip_set_req_std {
178 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
179 +/* Uses ip_set_req_std */
181 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
182 +/* Uses ip_set_req_create */
184 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
185 +/* Uses ip_set_req_create */
187 +union ip_set_name_index {
188 + char name[IP_SET_MAXNAMELEN];
192 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
193 +struct ip_set_req_get_set {
196 + union ip_set_name_index set;
199 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
200 +/* Uses ip_set_req_get_set */
202 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
203 +struct ip_set_req_version {
208 +/* Double shots operations:
209 + * add, del, test, bind and unbind.
211 + * First we query the kernel to get the index and type of the target set,
212 + * then issue the command. Validity of IP is checked in kernel in order
213 + * to minimalize sockopt operations.
216 +/* Get minimal set data for add/del/test/bind/unbind IP */
217 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
218 +struct ip_set_req_adt_get {
221 + union ip_set_name_index set;
222 + char typename[IP_SET_MAXNAMELEN];
225 +#define IP_SET_REQ_BYINDEX \
229 +struct ip_set_req_adt {
230 + IP_SET_REQ_BYINDEX;
233 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
234 +/* Uses ip_set_req_adt, with type specific addage */
236 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
237 +/* Uses ip_set_req_adt, with type specific addage */
239 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
240 +/* Uses ip_set_req_adt, with type specific addage */
242 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
243 +/* Uses ip_set_req_bind, with type specific addage */
244 +struct ip_set_req_bind {
245 + IP_SET_REQ_BYINDEX;
246 + char binding[IP_SET_MAXNAMELEN];
249 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
250 +/* Uses ip_set_req_bind, with type speficic addage
251 + * index = 0 means unbinding for all sets */
253 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
254 +/* Uses ip_set_req_bind, with type specific addage */
256 +/* Multiple shots operations: list, save, restore.
258 + * - check kernel version and query the max number of sets
259 + * - get the basic information on all sets
260 + * and size required for the next step
261 + * - get actual set data: header, data, bindings
264 +/* Get max_sets and the index of a queried set
266 +#define IP_SET_OP_MAX_SETS 0x00000020
267 +struct ip_set_req_max_sets {
270 + ip_set_id_t max_sets; /* max_sets */
271 + ip_set_id_t sets; /* real number of sets */
272 + union ip_set_name_index set; /* index of set if name used */
275 +/* Get the id and name of the sets plus size for next step */
276 +#define IP_SET_OP_LIST_SIZE 0x00000201
277 +#define IP_SET_OP_SAVE_SIZE 0x00000202
278 +struct ip_set_req_setnames {
280 + ip_set_id_t index; /* set to list/save */
281 + u_int32_t size; /* size to get setdata */
282 + /* followed by sets number of struct ip_set_name_list */
285 +struct ip_set_name_list {
286 + char name[IP_SET_MAXNAMELEN];
287 + char typename[IP_SET_MAXNAMELEN];
292 +/* The actual list operation */
293 +#define IP_SET_OP_LIST 0x00000203
294 +struct ip_set_req_list {
295 + IP_SET_REQ_BYINDEX;
296 + /* sets number of struct ip_set_list in reply */
299 +struct ip_set_list {
301 + ip_set_id_t binding;
303 + u_int32_t header_size; /* Set header data of header_size */
304 + u_int32_t members_size; /* Set members data of members_size */
305 + u_int32_t bindings_size;/* Set bindings data of bindings_size */
308 +struct ip_set_hash_list {
310 + ip_set_id_t binding;
313 +/* The save operation */
314 +#define IP_SET_OP_SAVE 0x00000204
315 +/* Uses ip_set_req_list, in the reply replaced by
316 + * sets number of struct ip_set_save plus a marker
317 + * ip_set_save followed by ip_set_hash_save structures.
319 +struct ip_set_save {
321 + ip_set_id_t binding;
322 + u_int32_t header_size; /* Set header data of header_size */
323 + u_int32_t members_size; /* Set members data of members_size */
326 +/* At restoring, ip == 0 means default binding for the given set: */
327 +struct ip_set_hash_save {
330 + ip_set_id_t binding;
333 +/* The restore operation */
334 +#define IP_SET_OP_RESTORE 0x00000205
335 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
336 + * plus a marker ip_set_restore, followed by ip_set_hash_save
339 +struct ip_set_restore {
340 + char name[IP_SET_MAXNAMELEN];
341 + char typename[IP_SET_MAXNAMELEN];
343 + u_int32_t header_size; /* Create data of header_size */
344 + u_int32_t members_size; /* Set members data of members_size */
347 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
349 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
352 +/* General limit for the elements in a set */
353 +#define MAX_RANGE 0x0000FFFF
355 +/* Alignment: 'unsigned long' unsupported */
356 +#define IPSET_ALIGNTO 4
357 +#define IPSET_ALIGN(len) (((len) + IPSET_ALIGNTO - 1) & ~(IPSET_ALIGNTO - 1))
358 +#define IPSET_VALIGN(len, old) ((old) ? (len) : IPSET_ALIGN(len))
361 +#include <linux/netfilter_ipv4/ip_set_compat.h>
362 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
364 +#define ip_set_printk(format, args...) \
366 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
367 + printk(format "\n" , ## args); \
370 +#if defined(IP_SET_DEBUG)
371 +#define DP(format, args...) \
373 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
374 + printk(format "\n" , ## args); \
376 +#define IP_SET_ASSERT(x) \
379 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
380 + __FILE__, __LINE__, __FUNCTION__); \
383 +#define DP(format, args...)
384 +#define IP_SET_ASSERT(x)
390 + * The ip_set_type definition - one per set type, e.g. "ipmap".
392 + * Each individual set has a pointer, set->type, going to one
393 + * of these structures. Function pointers inside the structure implement
394 + * the real behaviour of the sets.
396 + * If not mentioned differently, the implementation behind the function
397 + * pointers of a set_type, is expected to return 0 if ok, and a negative
398 + * errno (e.g. -EINVAL) on error.
400 +struct ip_set_type {
401 + struct list_head list; /* next in list of set types */
403 + /* test for IP in set (kernel: iptables -m set src|dst)
404 + * return 0 if not in set, 1 if in set.
406 + int (*testip_kernel) (struct ip_set *set,
407 + const struct sk_buff * skb,
408 + const u_int32_t *flags);
410 + /* test for IP in set (userspace: ipset -T set IP)
411 + * return 0 if not in set, 1 if in set.
413 + int (*testip) (struct ip_set *set,
414 + const void *data, u_int32_t size);
417 + * Size of the data structure passed by when
418 + * adding/deletin/testing an entry.
422 + /* Add IP into set (userspace: ipset -A set IP)
423 + * Return -EEXIST if the address is already in the set,
424 + * and -ERANGE if the address lies outside the set bounds.
425 + * If the address was not already in the set, 0 is returned.
427 + int (*addip) (struct ip_set *set,
428 + const void *data, u_int32_t size);
430 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
431 + * Return -EEXIST if the address is already in the set,
432 + * and -ERANGE if the address lies outside the set bounds.
433 + * If the address was not already in the set, 0 is returned.
435 + int (*addip_kernel) (struct ip_set *set,
436 + const struct sk_buff * skb,
437 + const u_int32_t *flags);
439 + /* remove IP from set (userspace: ipset -D set --entry x)
440 + * Return -EEXIST if the address is NOT in the set,
441 + * and -ERANGE if the address lies outside the set bounds.
442 + * If the address really was in the set, 0 is returned.
444 + int (*delip) (struct ip_set *set,
445 + const void *data, u_int32_t size);
447 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
448 + * Return -EEXIST if the address is NOT in the set,
449 + * and -ERANGE if the address lies outside the set bounds.
450 + * If the address really was in the set, 0 is returned.
452 + int (*delip_kernel) (struct ip_set *set,
453 + const struct sk_buff * skb,
454 + const u_int32_t *flags);
456 + /* new set creation - allocated type specific items
458 + int (*create) (struct ip_set *set,
459 + const void *data, u_int32_t size);
461 + /* retry the operation after successfully tweaking the set
463 + int (*retry) (struct ip_set *set);
465 + /* set destruction - free type specific items
466 + * There is no return value.
467 + * Can be called only when child sets are destroyed.
469 + void (*destroy) (struct ip_set *set);
471 + /* set flushing - reset all bits in the set, or something similar.
472 + * There is no return value.
474 + void (*flush) (struct ip_set *set);
476 + /* Listing: size needed for header
478 + u_int32_t header_size;
480 + /* Listing: Get the header
482 + * Fill in the information in "data".
483 + * This function is always run after list_header_size() under a
484 + * writelock on the set. Therefor is the length of "data" always
487 + void (*list_header) (const struct ip_set *set,
490 + /* Listing: Get the size for the set members
492 + int (*list_members_size) (const struct ip_set *set, char dont_align);
494 + /* Listing: Get the set members
496 + * Fill in the information in "data".
497 + * This function is always run after list_member_size() under a
498 + * writelock on the set. Therefor is the length of "data" always
501 + void (*list_members) (const struct ip_set *set,
502 + void *data, char dont_align);
504 + char typename[IP_SET_MAXNAMELEN];
505 + unsigned char features;
506 + int protocol_version;
508 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
512 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
513 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
515 +/* A generic ipset */
517 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
518 + rwlock_t lock; /* lock for concurrency control */
519 + ip_set_id_t id; /* set id for swapping */
520 + atomic_t ref; /* in kernel and in hash references */
521 + struct ip_set_type *type; /* the set types */
522 + void *data; /* pooltype specific data */
525 +/* register and unregister set references */
526 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
527 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t index);
528 +extern void ip_set_put_byindex(ip_set_id_t index);
529 +extern ip_set_id_t ip_set_id(ip_set_id_t index);
530 +extern ip_set_id_t __ip_set_get_byname(const char name[IP_SET_MAXNAMELEN],
531 + struct ip_set **set);
532 +extern void __ip_set_put_byindex(ip_set_id_t index);
534 +/* API for iptables set match, and SET target */
535 +extern int ip_set_addip_kernel(ip_set_id_t id,
536 + const struct sk_buff *skb,
537 + const u_int32_t *flags);
538 +extern int ip_set_delip_kernel(ip_set_id_t id,
539 + const struct sk_buff *skb,
540 + const u_int32_t *flags);
541 +extern int ip_set_testip_kernel(ip_set_id_t id,
542 + const struct sk_buff *skb,
543 + const u_int32_t *flags);
545 +/* Macros to generate functions */
547 +#define STRUCT(pre, type) CONCAT2(pre, type)
548 +#define CONCAT2(pre, type) struct pre##type
550 +#define FNAME(pre, mid, post) CONCAT3(pre, mid, post)
551 +#define CONCAT3(pre, mid, post) pre##mid##post
553 +#define UADT0(type, adt, args...) \
555 +FNAME(type,_u,adt)(struct ip_set *set, const void *data, u_int32_t size)\
557 + const STRUCT(ip_set_req_,type) *req = data; \
559 + return FNAME(type,_,adt)(set , ## args); \
562 +#define UADT(type, adt, args...) \
563 + UADT0(type, adt, req->ip , ## args)
565 +#define KADT(type, adt, getfn, args...) \
567 +FNAME(type,_k,adt)(struct ip_set *set, \
568 + const struct sk_buff *skb, \
569 + const u_int32_t *flags) \
571 + ip_set_ip_t ip = getfn(skb, flags); \
574 + return FNAME(type,_,adt)(set, ip , ##args); \
577 +#define REGISTER_MODULE(type) \
578 +static int __init ip_set_##type##_init(void) \
580 + init_max_page_size(); \
581 + return ip_set_register_set_type(&ip_set_##type); \
584 +static void __exit ip_set_##type##_fini(void) \
586 + /* FIXME: possible race with ip_set_create() */ \
587 + ip_set_unregister_set_type(&ip_set_##type); \
590 +module_init(ip_set_##type##_init); \
591 +module_exit(ip_set_##type##_fini);
593 +/* Common functions */
595 +static inline ip_set_ip_t
596 +ipaddr(const struct sk_buff *skb, const u_int32_t *flags)
598 + return ntohl(flags[0] & IPSET_SRC ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr);
601 +#define jhash_ip(map, i, ip) jhash_1word(ip, *(map->initval + i))
603 +#define pack_ip_port(map, ip, port) \
604 + (port + ((ip - ((map)->first_ip)) << 16))
606 +#endif /* __KERNEL__ */
608 +#define UNUSED __attribute__ ((unused))
610 +#endif /*_IP_SET_H*/
611 diff --git a/include/linux/netfilter_ipv4/ip_set_bitmaps.h b/include/linux/netfilter_ipv4/ip_set_bitmaps.h
613 index 0000000..da3493f
615 +++ b/include/linux/netfilter_ipv4/ip_set_bitmaps.h
617 +#ifndef __IP_SET_BITMAPS_H
618 +#define __IP_SET_BITMAPS_H
620 +/* Macros to generate functions */
623 +#define BITMAP_CREATE(type) \
625 +type##_create(struct ip_set *set, const void *data, u_int32_t size) \
628 + const struct ip_set_req_##type##_create *req = data; \
629 + struct ip_set_##type *map; \
631 + if (req->from > req->to) { \
636 + map = kmalloc(sizeof(struct ip_set_##type), GFP_KERNEL); \
638 + DP("out of memory for %zu bytes", \
639 + sizeof(struct ip_set_##type)); \
642 + map->first_ip = req->from; \
643 + map->last_ip = req->to; \
645 + newbytes = __##type##_create(req, map); \
646 + if (newbytes < 0) { \
651 + map->size = newbytes; \
652 + map->members = ip_set_malloc(newbytes); \
653 + if (!map->members) { \
654 + DP("out of memory for %i bytes", newbytes); \
658 + memset(map->members, 0, newbytes); \
664 +#define BITMAP_DESTROY(type) \
666 +type##_destroy(struct ip_set *set) \
668 + struct ip_set_##type *map = set->data; \
670 + ip_set_free(map->members, map->size); \
673 + set->data = NULL; \
676 +#define BITMAP_FLUSH(type) \
678 +type##_flush(struct ip_set *set) \
680 + struct ip_set_##type *map = set->data; \
681 + memset(map->members, 0, map->size); \
684 +#define BITMAP_LIST_HEADER(type) \
686 +type##_list_header(const struct ip_set *set, void *data) \
688 + const struct ip_set_##type *map = set->data; \
689 + struct ip_set_req_##type##_create *header = data; \
691 + header->from = map->first_ip; \
692 + header->to = map->last_ip; \
693 + __##type##_list_header(map, header); \
696 +#define BITMAP_LIST_MEMBERS_SIZE(type, dtype, sizeid, testfn) \
698 +type##_list_members_size(const struct ip_set *set, char dont_align) \
700 + const struct ip_set_##type *map = set->data; \
701 + ip_set_ip_t i, elements = 0; \
704 + return map->size; \
706 + for (i = 0; i < sizeid; i++) \
710 + return elements * IPSET_ALIGN(sizeof(dtype)); \
713 +#define IP_SET_TYPE(type, __features) \
714 +struct ip_set_type ip_set_##type = { \
715 + .typename = #type, \
716 + .features = __features, \
717 + .protocol_version = IP_SET_PROTOCOL_VERSION, \
718 + .create = &type##_create, \
719 + .destroy = &type##_destroy, \
720 + .flush = &type##_flush, \
721 + .reqsize = sizeof(struct ip_set_req_##type), \
722 + .addip = &type##_uadd, \
723 + .addip_kernel = &type##_kadd, \
724 + .delip = &type##_udel, \
725 + .delip_kernel = &type##_kdel, \
726 + .testip = &type##_utest, \
727 + .testip_kernel = &type##_ktest, \
728 + .header_size = sizeof(struct ip_set_req_##type##_create),\
729 + .list_header = &type##_list_header, \
730 + .list_members_size = &type##_list_members_size, \
731 + .list_members = &type##_list_members, \
732 + .me = THIS_MODULE, \
734 +#endif /* __KERNEL */
736 +#endif /* __IP_SET_BITMAPS_H */
737 diff --git a/include/linux/netfilter_ipv4/ip_set_compat.h b/include/linux/netfilter_ipv4/ip_set_compat.h
739 index 0000000..9f17397
741 +++ b/include/linux/netfilter_ipv4/ip_set_compat.h
743 +#ifndef _IP_SET_COMPAT_H
744 +#define _IP_SET_COMPAT_H
747 +#include <linux/version.h>
751 +#define __MOD_INC(foo) __MOD_INC_USE_COUNT(foo)
752 +#define __MOD_DEC(foo) __MOD_DEC_USE_COUNT(foo)
754 +#define __MOD_INC(foo) 1
755 +#define __MOD_DEC(foo)
758 +/* Backward compatibility */
766 +/* Compatibility glue code */
767 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
768 +#include <linux/interrupt.h>
769 +#define DEFINE_RWLOCK(x) rwlock_t x = RW_LOCK_UNLOCKED
770 +#define try_module_get(x) __MOD_INC(x)
771 +#define module_put(x) __MOD_DEC(x)
772 +#define __clear_bit(nr, addr) clear_bit(nr, addr)
773 +#define __set_bit(nr, addr) set_bit(nr, addr)
774 +#define __test_and_set_bit(nr, addr) test_and_set_bit(nr, addr)
775 +#define __test_and_clear_bit(nr, addr) test_and_clear_bit(nr, addr)
777 +typedef unsigned __bitwise__ gfp_t;
779 +static inline void *kzalloc(size_t size, gfp_t flags)
781 + void *data = kmalloc(size, flags);
784 + memset(data, 0, size);
790 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
791 +#define __KMEM_CACHE_T__ kmem_cache_t
793 +#define __KMEM_CACHE_T__ struct kmem_cache
796 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
797 +#define ip_hdr(skb) ((skb)->nh.iph)
798 +#define skb_mac_header(skb) ((skb)->mac.raw)
799 +#define eth_hdr(skb) ((struct ethhdr *)skb_mac_header(skb))
802 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
803 +#include <linux/netfilter.h>
804 +#define KMEM_CACHE_CREATE(name, size) \
805 + kmem_cache_create(name, size, 0, 0, NULL, NULL)
807 +#define KMEM_CACHE_CREATE(name, size) \
808 + kmem_cache_create(name, size, 0, 0, NULL)
812 +#define NIPQUAD(addr) \
813 + ((unsigned char *)&addr)[0], \
814 + ((unsigned char *)&addr)[1], \
815 + ((unsigned char *)&addr)[2], \
816 + ((unsigned char *)&addr)[3]
820 +#if defined(__LITTLE_ENDIAN)
821 +#define HIPQUAD(addr) \
822 + ((unsigned char *)&addr)[3], \
823 + ((unsigned char *)&addr)[2], \
824 + ((unsigned char *)&addr)[1], \
825 + ((unsigned char *)&addr)[0]
826 +#elif defined(__BIG_ENDIAN)
827 +#define HIPQUAD NIPQUAD
829 +#error "Please fix asm/byteorder.h"
830 +#endif /* __LITTLE_ENDIAN */
833 +#endif /* __KERNEL__ */
834 +#endif /* _IP_SET_COMPAT_H */
835 diff --git a/include/linux/netfilter_ipv4/ip_set_getport.h b/include/linux/netfilter_ipv4/ip_set_getport.h
837 index 0000000..18ed729
839 +++ b/include/linux/netfilter_ipv4/ip_set_getport.h
841 +#ifndef _IP_SET_GETPORT_H
842 +#define _IP_SET_GETPORT_H
846 +#define INVALID_PORT (MAX_RANGE + 1)
848 +/* We must handle non-linear skbs */
849 +static inline ip_set_ip_t
850 +get_port(const struct sk_buff *skb, const u_int32_t *flags)
852 + struct iphdr *iph = ip_hdr(skb);
853 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
854 + switch (iph->protocol) {
855 + case IPPROTO_TCP: {
856 + struct tcphdr tcph;
858 + /* See comments at tcp_match in ip_tables.c */
860 + return INVALID_PORT;
862 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &tcph, sizeof(tcph)) < 0)
863 + /* No choice either */
864 + return INVALID_PORT;
866 + return ntohs(flags[0] & IPSET_SRC ?
867 + tcph.source : tcph.dest);
869 + case IPPROTO_UDP: {
870 + struct udphdr udph;
873 + return INVALID_PORT;
875 + if (skb_copy_bits(skb, ip_hdr(skb)->ihl*4, &udph, sizeof(udph)) < 0)
876 + /* No choice either */
877 + return INVALID_PORT;
879 + return ntohs(flags[0] & IPSET_SRC ?
880 + udph.source : udph.dest);
883 + return INVALID_PORT;
886 +#endif /* __KERNEL__ */
888 +#endif /*_IP_SET_GETPORT_H*/
889 diff --git a/include/linux/netfilter_ipv4/ip_set_hashes.h b/include/linux/netfilter_ipv4/ip_set_hashes.h
891 index 0000000..8eeced3
893 +++ b/include/linux/netfilter_ipv4/ip_set_hashes.h
895 +#ifndef __IP_SET_HASHES_H
896 +#define __IP_SET_HASHES_H
898 +#define initval_t uint32_t
900 +/* Macros to generate functions */
903 +#define HASH_RETRY0(type, dtype, cond) \
905 +type##_retry(struct ip_set *set) \
907 + struct ip_set_##type *map = set->data, *tmp; \
910 + u_int32_t i, hashsize = map->hashsize; \
913 + if (map->resize == 0) \
919 + /* Calculate new hash size */ \
920 + hashsize += (hashsize * map->resize)/100; \
921 + if (hashsize == map->hashsize) \
924 + ip_set_printk("rehashing of set %s triggered: " \
925 + "hashsize grows from %lu to %lu", \
927 + (long unsigned)map->hashsize, \
928 + (long unsigned)hashsize); \
930 + tmp = kmalloc(sizeof(struct ip_set_##type) \
931 + + map->probes * sizeof(initval_t), GFP_ATOMIC); \
933 + DP("out of memory for %zu bytes", \
934 + sizeof(struct ip_set_##type) \
935 + + map->probes * sizeof(initval_t)); \
938 + tmp->members = harray_malloc(hashsize, sizeof(dtype), GFP_ATOMIC);\
939 + if (!tmp->members) { \
940 + DP("out of memory for %zu bytes", hashsize * sizeof(dtype));\
944 + tmp->hashsize = hashsize; \
945 + tmp->elements = 0; \
946 + tmp->probes = map->probes; \
947 + tmp->resize = map->resize; \
948 + memcpy(tmp->initval, map->initval, map->probes * sizeof(initval_t));\
949 + __##type##_retry(tmp, map); \
951 + write_lock_bh(&set->lock); \
952 + map = set->data; /* Play safe */ \
953 + for (i = 0; i < map->hashsize && res == 0; i++) { \
954 + elem = HARRAY_ELEM(map->members, dtype *, i); \
956 + res = __##type##_add(tmp, elem); \
959 + /* Failure, try again */ \
960 + write_unlock_bh(&set->lock); \
961 + harray_free(tmp->members); \
966 + /* Success at resizing! */ \
967 + members = map->members; \
969 + map->hashsize = tmp->hashsize; \
970 + map->members = tmp->members; \
971 + write_unlock_bh(&set->lock); \
973 + harray_free(members); \
979 +#define HASH_RETRY(type, dtype) \
980 + HASH_RETRY0(type, dtype, *elem)
982 +#define HASH_RETRY2(type, dtype) \
983 + HASH_RETRY0(type, dtype, elem->ip || elem->ip1)
985 +#define HASH_CREATE(type, dtype) \
987 +type##_create(struct ip_set *set, const void *data, u_int32_t size) \
989 + const struct ip_set_req_##type##_create *req = data; \
990 + struct ip_set_##type *map; \
993 + if (req->hashsize < 1) { \
994 + ip_set_printk("hashsize too small"); \
998 + if (req->probes < 1) { \
999 + ip_set_printk("probes too small"); \
1000 + return -ENOEXEC; \
1003 + map = kmalloc(sizeof(struct ip_set_##type) \
1004 + + req->probes * sizeof(initval_t), GFP_KERNEL); \
1006 + DP("out of memory for %zu bytes", \
1007 + sizeof(struct ip_set_##type) \
1008 + + req->probes * sizeof(initval_t)); \
1011 + for (i = 0; i < req->probes; i++) \
1012 + get_random_bytes(((initval_t *) map->initval)+i, 4); \
1013 + map->elements = 0; \
1014 + map->hashsize = req->hashsize; \
1015 + map->probes = req->probes; \
1016 + map->resize = req->resize; \
1017 + if (__##type##_create(req, map)) { \
1019 + return -ENOEXEC; \
1021 + map->members = harray_malloc(map->hashsize, sizeof(dtype), GFP_KERNEL);\
1022 + if (!map->members) { \
1023 + DP("out of memory for %zu bytes", map->hashsize * sizeof(dtype));\
1028 + set->data = map; \
1032 +#define HASH_DESTROY(type) \
1034 +type##_destroy(struct ip_set *set) \
1036 + struct ip_set_##type *map = set->data; \
1038 + harray_free(map->members); \
1041 + set->data = NULL; \
1044 +#define HASH_FLUSH(type, dtype) \
1046 +type##_flush(struct ip_set *set) \
1048 + struct ip_set_##type *map = set->data; \
1049 + harray_flush(map->members, map->hashsize, sizeof(dtype)); \
1050 + map->elements = 0; \
1053 +#define HASH_FLUSH_CIDR(type, dtype) \
1055 +type##_flush(struct ip_set *set) \
1057 + struct ip_set_##type *map = set->data; \
1058 + harray_flush(map->members, map->hashsize, sizeof(dtype)); \
1059 + memset(map->cidr, 0, sizeof(map->cidr)); \
1060 + memset(map->nets, 0, sizeof(map->nets)); \
1061 + map->elements = 0; \
1064 +#define HASH_LIST_HEADER(type) \
1066 +type##_list_header(const struct ip_set *set, void *data) \
1068 + const struct ip_set_##type *map = set->data; \
1069 + struct ip_set_req_##type##_create *header = data; \
1071 + header->hashsize = map->hashsize; \
1072 + header->probes = map->probes; \
1073 + header->resize = map->resize; \
1074 + __##type##_list_header(map, header); \
1077 +#define HASH_LIST_MEMBERS_SIZE(type, dtype) \
1079 +type##_list_members_size(const struct ip_set *set, char dont_align) \
1081 + const struct ip_set_##type *map = set->data; \
1083 + return (map->elements * IPSET_VALIGN(sizeof(dtype), dont_align));\
1086 +#define HASH_LIST_MEMBERS(type, dtype) \
1088 +type##_list_members(const struct ip_set *set, void *data, char dont_align)\
1090 + const struct ip_set_##type *map = set->data; \
1091 + dtype *elem, *d; \
1092 + uint32_t i, n = 0; \
1094 + for (i = 0; i < map->hashsize; i++) { \
1095 + elem = HARRAY_ELEM(map->members, dtype *, i); \
1097 + d = data + n * IPSET_VALIGN(sizeof(dtype), dont_align);\
1104 +#define HASH_LIST_MEMBERS_MEMCPY(type, dtype, nonzero) \
1106 +type##_list_members(const struct ip_set *set, void *data, char dont_align)\
1108 + const struct ip_set_##type *map = set->data; \
1110 + uint32_t i, n = 0; \
1112 + for (i = 0; i < map->hashsize; i++) { \
1113 + elem = HARRAY_ELEM(map->members, dtype *, i); \
1115 + memcpy(data + n * IPSET_VALIGN(sizeof(dtype), dont_align),\
1116 + elem, sizeof(dtype)); \
1122 +#define IP_SET_RTYPE(type, __features) \
1123 +struct ip_set_type ip_set_##type = { \
1124 + .typename = #type, \
1125 + .features = __features, \
1126 + .protocol_version = IP_SET_PROTOCOL_VERSION, \
1127 + .create = &type##_create, \
1128 + .retry = &type##_retry, \
1129 + .destroy = &type##_destroy, \
1130 + .flush = &type##_flush, \
1131 + .reqsize = sizeof(struct ip_set_req_##type), \
1132 + .addip = &type##_uadd, \
1133 + .addip_kernel = &type##_kadd, \
1134 + .delip = &type##_udel, \
1135 + .delip_kernel = &type##_kdel, \
1136 + .testip = &type##_utest, \
1137 + .testip_kernel = &type##_ktest, \
1138 + .header_size = sizeof(struct ip_set_req_##type##_create),\
1139 + .list_header = &type##_list_header, \
1140 + .list_members_size = &type##_list_members_size, \
1141 + .list_members = &type##_list_members, \
1142 + .me = THIS_MODULE, \
1145 +/* Helper functions */
1147 +add_cidr_size(uint8_t *cidr, uint8_t size)
1152 + for (i = 0; i < 30 && cidr[i]; i++) {
1153 + if (cidr[i] < size) {
1164 +del_cidr_size(uint8_t *cidr, uint8_t size)
1168 + for (i = 0; i < 29 && cidr[i]; i++) {
1169 + if (cidr[i] == size)
1170 + cidr[i] = size = cidr[i+1];
1175 +#include <arpa/inet.h>
1176 +#endif /* __KERNEL */
1179 +#define UINT16_MAX 65535
1182 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
1184 +static inline ip_set_ip_t
1185 +pack_ip_cidr(ip_set_ip_t ip, unsigned char cidr)
1187 + ip_set_ip_t addr, *paddr = &addr;
1188 + unsigned char n, t, *a;
1190 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
1192 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
1196 + a = &((unsigned char *)paddr)[n];
1197 + *a = *a /(1 << (8 - t)) + shifts[t];
1199 + DP("n: %u, t: %u, a: %u", n, t, *a);
1200 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
1201 + HIPQUAD(ip), cidr, NIPQUAD(addr));
1204 + return ntohl(addr);
1208 +#endif /* __IP_SET_HASHES_H */
1209 diff --git a/include/linux/netfilter_ipv4/ip_set_iphash.h b/include/linux/netfilter_ipv4/ip_set_iphash.h
1210 new file mode 100644
1211 index 0000000..0a0c7e8
1213 +++ b/include/linux/netfilter_ipv4/ip_set_iphash.h
1215 +#ifndef __IP_SET_IPHASH_H
1216 +#define __IP_SET_IPHASH_H
1218 +#include <linux/netfilter_ipv4/ip_set.h>
1219 +#include <linux/netfilter_ipv4/ip_set_hashes.h>
1221 +#define SETTYPE_NAME "iphash"
1223 +struct ip_set_iphash {
1224 + ip_set_ip_t *members; /* the iphash proper */
1225 + uint32_t elements; /* number of elements */
1226 + uint32_t hashsize; /* hash size */
1227 + uint16_t probes; /* max number of probes */
1228 + uint16_t resize; /* resize factor in percent */
1229 + ip_set_ip_t netmask; /* netmask */
1230 + initval_t initval[0]; /* initvals for jhash_1word */
1233 +struct ip_set_req_iphash_create {
1234 + uint32_t hashsize;
1237 + ip_set_ip_t netmask;
1240 +struct ip_set_req_iphash {
1244 +#endif /* __IP_SET_IPHASH_H */
1245 diff --git a/include/linux/netfilter_ipv4/ip_set_ipmap.h b/include/linux/netfilter_ipv4/ip_set_ipmap.h
1246 new file mode 100644
1247 index 0000000..d16c0ae
1249 +++ b/include/linux/netfilter_ipv4/ip_set_ipmap.h
1251 +#ifndef __IP_SET_IPMAP_H
1252 +#define __IP_SET_IPMAP_H
1254 +#include <linux/netfilter_ipv4/ip_set.h>
1255 +#include <linux/netfilter_ipv4/ip_set_bitmaps.h>
1257 +#define SETTYPE_NAME "ipmap"
1259 +struct ip_set_ipmap {
1260 + void *members; /* the ipmap proper */
1261 + ip_set_ip_t first_ip; /* host byte order, included in range */
1262 + ip_set_ip_t last_ip; /* host byte order, included in range */
1263 + ip_set_ip_t netmask; /* subnet netmask */
1264 + ip_set_ip_t sizeid; /* size of set in IPs */
1265 + ip_set_ip_t hosts; /* number of hosts in a subnet */
1266 + u_int32_t size; /* size of the ipmap proper */
1269 +struct ip_set_req_ipmap_create {
1272 + ip_set_ip_t netmask;
1275 +struct ip_set_req_ipmap {
1279 +static inline unsigned int
1280 +mask_to_bits(ip_set_ip_t mask)
1282 + unsigned int bits = 32;
1283 + ip_set_ip_t maskaddr;
1285 + if (mask == 0xFFFFFFFF)
1288 + maskaddr = 0xFFFFFFFE;
1289 + while (--bits > 0 && maskaddr != mask)
1295 +static inline ip_set_ip_t
1296 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
1298 + ip_set_ip_t mask = 0xFFFFFFFE;
1301 + while (--(*bits) > 0 && mask && (to & mask) != from)
1307 +#endif /* __IP_SET_IPMAP_H */
1308 diff --git a/include/linux/netfilter_ipv4/ip_set_ipporthash.h b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
1309 new file mode 100644
1310 index 0000000..a3b781a
1312 +++ b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
1314 +#ifndef __IP_SET_IPPORTHASH_H
1315 +#define __IP_SET_IPPORTHASH_H
1317 +#include <linux/netfilter_ipv4/ip_set.h>
1318 +#include <linux/netfilter_ipv4/ip_set_hashes.h>
1320 +#define SETTYPE_NAME "ipporthash"
1322 +struct ip_set_ipporthash {
1323 + ip_set_ip_t *members; /* the ipporthash proper */
1324 + uint32_t elements; /* number of elements */
1325 + uint32_t hashsize; /* hash size */
1326 + uint16_t probes; /* max number of probes */
1327 + uint16_t resize; /* resize factor in percent */
1328 + ip_set_ip_t first_ip; /* host byte order, included in range */
1329 + ip_set_ip_t last_ip; /* host byte order, included in range */
1330 + initval_t initval[0]; /* initvals for jhash_1word */
1333 +struct ip_set_req_ipporthash_create {
1334 + uint32_t hashsize;
1341 +struct ip_set_req_ipporthash {
1346 +#endif /* __IP_SET_IPPORTHASH_H */
1347 diff --git a/include/linux/netfilter_ipv4/ip_set_ipportiphash.h b/include/linux/netfilter_ipv4/ip_set_ipportiphash.h
1348 new file mode 100644
1349 index 0000000..2202c51
1351 +++ b/include/linux/netfilter_ipv4/ip_set_ipportiphash.h
1353 +#ifndef __IP_SET_IPPORTIPHASH_H
1354 +#define __IP_SET_IPPORTIPHASH_H
1356 +#include <linux/netfilter_ipv4/ip_set.h>
1357 +#include <linux/netfilter_ipv4/ip_set_hashes.h>
1359 +#define SETTYPE_NAME "ipportiphash"
1366 +struct ip_set_ipportiphash {
1367 + struct ipportip *members; /* the ipportip proper */
1368 + uint32_t elements; /* number of elements */
1369 + uint32_t hashsize; /* hash size */
1370 + uint16_t probes; /* max number of probes */
1371 + uint16_t resize; /* resize factor in percent */
1372 + ip_set_ip_t first_ip; /* host byte order, included in range */
1373 + ip_set_ip_t last_ip; /* host byte order, included in range */
1374 + initval_t initval[0]; /* initvals for jhash_1word */
1377 +struct ip_set_req_ipportiphash_create {
1378 + uint32_t hashsize;
1385 +struct ip_set_req_ipportiphash {
1391 +#endif /* __IP_SET_IPPORTIPHASH_H */
1392 diff --git a/include/linux/netfilter_ipv4/ip_set_ipportnethash.h b/include/linux/netfilter_ipv4/ip_set_ipportnethash.h
1393 new file mode 100644
1394 index 0000000..73b2430
1396 +++ b/include/linux/netfilter_ipv4/ip_set_ipportnethash.h
1398 +#ifndef __IP_SET_IPPORTNETHASH_H
1399 +#define __IP_SET_IPPORTNETHASH_H
1401 +#include <linux/netfilter_ipv4/ip_set.h>
1402 +#include <linux/netfilter_ipv4/ip_set_hashes.h>
1404 +#define SETTYPE_NAME "ipportnethash"
1411 +struct ip_set_ipportnethash {
1412 + struct ipportip *members; /* the ipportip proper */
1413 + uint32_t elements; /* number of elements */
1414 + uint32_t hashsize; /* hash size */
1415 + uint16_t probes; /* max number of probes */
1416 + uint16_t resize; /* resize factor in percent */
1417 + ip_set_ip_t first_ip; /* host byte order, included in range */
1418 + ip_set_ip_t last_ip; /* host byte order, included in range */
1419 + uint8_t cidr[30]; /* CIDR sizes */
1420 + uint16_t nets[30]; /* nr of nets by CIDR sizes */
1421 + initval_t initval[0]; /* initvals for jhash_1word */
1424 +struct ip_set_req_ipportnethash_create {
1425 + uint32_t hashsize;
1432 +struct ip_set_req_ipportnethash {
1439 +#endif /* __IP_SET_IPPORTNETHASH_H */
1440 diff --git a/include/linux/netfilter_ipv4/ip_set_iptree.h b/include/linux/netfilter_ipv4/ip_set_iptree.h
1441 new file mode 100644
1442 index 0000000..36bf5ac
1444 +++ b/include/linux/netfilter_ipv4/ip_set_iptree.h
1446 +#ifndef __IP_SET_IPTREE_H
1447 +#define __IP_SET_IPTREE_H
1449 +#include <linux/netfilter_ipv4/ip_set.h>
1451 +#define SETTYPE_NAME "iptree"
1453 +struct ip_set_iptreed {
1454 + unsigned long expires[256]; /* x.x.x.ADDR */
1457 +struct ip_set_iptreec {
1458 + struct ip_set_iptreed *tree[256]; /* x.x.ADDR.* */
1461 +struct ip_set_iptreeb {
1462 + struct ip_set_iptreec *tree[256]; /* x.ADDR.*.* */
1465 +struct ip_set_iptree {
1466 + unsigned int timeout;
1467 + unsigned int gc_interval;
1469 + uint32_t elements; /* number of elements */
1470 + struct timer_list gc;
1471 + struct ip_set_iptreeb *tree[256]; /* ADDR.*.*.* */
1475 +struct ip_set_req_iptree_create {
1476 + unsigned int timeout;
1479 +struct ip_set_req_iptree {
1481 + unsigned int timeout;
1484 +#endif /* __IP_SET_IPTREE_H */
1485 diff --git a/include/linux/netfilter_ipv4/ip_set_iptreemap.h b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
1486 new file mode 100644
1487 index 0000000..6ea771a
1489 +++ b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
1491 +#ifndef __IP_SET_IPTREEMAP_H
1492 +#define __IP_SET_IPTREEMAP_H
1494 +#include <linux/netfilter_ipv4/ip_set.h>
1496 +#define SETTYPE_NAME "iptreemap"
1499 +struct ip_set_iptreemap_d {
1500 + unsigned char bitmap[32]; /* x.x.x.y */
1503 +struct ip_set_iptreemap_c {
1504 + struct ip_set_iptreemap_d *tree[256]; /* x.x.y.x */
1507 +struct ip_set_iptreemap_b {
1508 + struct ip_set_iptreemap_c *tree[256]; /* x.y.x.x */
1509 + unsigned char dirty[32];
1513 +struct ip_set_iptreemap {
1514 + unsigned int gc_interval;
1516 + struct timer_list gc;
1517 + struct ip_set_iptreemap_b *tree[256]; /* y.x.x.x */
1521 +struct ip_set_req_iptreemap_create {
1522 + unsigned int gc_interval;
1525 +struct ip_set_req_iptreemap {
1530 +#endif /* __IP_SET_IPTREEMAP_H */
1531 diff --git a/include/linux/netfilter_ipv4/ip_set_jhash.h b/include/linux/netfilter_ipv4/ip_set_jhash.h
1532 new file mode 100644
1533 index 0000000..2000b9f
1535 +++ b/include/linux/netfilter_ipv4/ip_set_jhash.h
1537 +#ifndef _LINUX_JHASH_H
1538 +#define _LINUX_JHASH_H
1540 +/* jhash.h: Jenkins hash support.
1542 + * Copyright (C) 2006. Bob Jenkins (bob_jenkins@burtleburtle.net)
1544 + * http://burtleburtle.net/bob/hash/
1546 + * These are the credits from Bob's sources:
1548 + * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
1550 + * These are functions for producing 32-bit hashes for hash table lookup.
1551 + * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
1552 + * are externally useful functions. Routines to test the hash are included
1553 + * if SELF_TEST is defined. You can use this free for any purpose. It's in
1554 + * the public domain. It has no warranty.
1556 + * Copyright (C) 2009 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
1558 + * I've modified Bob's hash to be useful in the Linux kernel, and
1559 + * any bugs present are my fault. Jozsef
1562 +#define __rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
1564 +/* __jhash_mix - mix 3 32-bit values reversibly. */
1565 +#define __jhash_mix(a,b,c) \
1567 + a -= c; a ^= __rot(c, 4); c += b; \
1568 + b -= a; b ^= __rot(a, 6); a += c; \
1569 + c -= b; c ^= __rot(b, 8); b += a; \
1570 + a -= c; a ^= __rot(c,16); c += b; \
1571 + b -= a; b ^= __rot(a,19); a += c; \
1572 + c -= b; c ^= __rot(b, 4); b += a; \
1575 +/* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
1576 +#define __jhash_final(a,b,c) \
1578 + c ^= b; c -= __rot(b,14); \
1579 + a ^= c; a -= __rot(c,11); \
1580 + b ^= a; b -= __rot(a,25); \
1581 + c ^= b; c -= __rot(b,16); \
1582 + a ^= c; a -= __rot(c,4); \
1583 + b ^= a; b -= __rot(a,14); \
1584 + c ^= b; c -= __rot(b,24); \
1587 +/* The golden ration: an arbitrary value */
1588 +#define JHASH_GOLDEN_RATIO 0xdeadbeef
1590 +/* The most generic version, hashes an arbitrary sequence
1591 + * of bytes. No alignment or length assumptions are made about
1592 + * the input key. The result depends on endianness.
1594 +static inline u32 jhash(const void *key, u32 length, u32 initval)
1597 + const u8 *k = key;
1599 + /* Set up the internal state */
1600 + a = b = c = JHASH_GOLDEN_RATIO + length + initval;
1602 + /* all but the last block: affect some 32 bits of (a,b,c) */
1603 + while (length > 12) {
1604 + a += (k[0] + ((u32)k[1]<<8) + ((u32)k[2]<<16) + ((u32)k[3]<<24));
1605 + b += (k[4] + ((u32)k[5]<<8) + ((u32)k[6]<<16) + ((u32)k[7]<<24));
1606 + c += (k[8] + ((u32)k[9]<<8) + ((u32)k[10]<<16) + ((u32)k[11]<<24));
1607 + __jhash_mix(a, b, c);
1612 + /* last block: affect all 32 bits of (c) */
1613 + /* all the case statements fall through */
1615 + case 12: c += (u32)k[11]<<24;
1616 + case 11: c += (u32)k[10]<<16;
1617 + case 10: c += (u32)k[9]<<8;
1618 + case 9 : c += k[8];
1619 + case 8 : b += (u32)k[7]<<24;
1620 + case 7 : b += (u32)k[6]<<16;
1621 + case 6 : b += (u32)k[5]<<8;
1622 + case 5 : b += k[4];
1623 + case 4 : a += (u32)k[3]<<24;
1624 + case 3 : a += (u32)k[2]<<16;
1625 + case 2 : a += (u32)k[1]<<8;
1626 + case 1 : a += k[0];
1627 + __jhash_final(a, b, c);
1635 +/* A special optimized version that handles 1 or more of u32s.
1636 + * The length parameter here is the number of u32s in the key.
1638 +static inline u32 jhash2(const u32 *k, u32 length, u32 initval)
1642 + /* Set up the internal state */
1643 + a = b = c = JHASH_GOLDEN_RATIO + (length<<2) + initval;
1645 + /* handle most of the key */
1646 + while (length > 3) {
1650 + __jhash_mix(a, b, c);
1655 + /* handle the last 3 u32's */
1656 + /* all the case statements fall through */
1658 + case 3: c += k[2];
1659 + case 2: b += k[1];
1660 + case 1: a += k[0];
1661 + __jhash_final(a, b, c);
1662 + case 0: /* case 0: nothing left to add */
1669 +/* A special ultra-optimized versions that knows they are hashing exactly
1670 + * 3, 2 or 1 word(s).
1672 +static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
1674 + a += JHASH_GOLDEN_RATIO + initval;
1675 + b += JHASH_GOLDEN_RATIO + initval;
1676 + c += JHASH_GOLDEN_RATIO + initval;
1678 + __jhash_final(a, b, c);
1683 +static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
1685 + return jhash_3words(0, a, b, initval);
1688 +static inline u32 jhash_1word(u32 a, u32 initval)
1690 + return jhash_3words(0, 0, a, initval);
1693 +#endif /* _LINUX_JHASH_H */
1694 diff --git a/include/linux/netfilter_ipv4/ip_set_macipmap.h b/include/linux/netfilter_ipv4/ip_set_macipmap.h
1695 new file mode 100644
1696 index 0000000..0615e9f
1698 +++ b/include/linux/netfilter_ipv4/ip_set_macipmap.h
1700 +#ifndef __IP_SET_MACIPMAP_H
1701 +#define __IP_SET_MACIPMAP_H
1703 +#include <linux/netfilter_ipv4/ip_set.h>
1704 +#include <linux/netfilter_ipv4/ip_set_bitmaps.h>
1706 +#define SETTYPE_NAME "macipmap"
1708 +/* general flags */
1709 +#define IPSET_MACIP_MATCHUNSET 1
1712 +#define IPSET_MACIP_ISSET 1
1714 +struct ip_set_macipmap {
1715 + void *members; /* the macipmap proper */
1716 + ip_set_ip_t first_ip; /* host byte order, included in range */
1717 + ip_set_ip_t last_ip; /* host byte order, included in range */
1719 + u_int32_t size; /* size of the ipmap proper */
1722 +struct ip_set_req_macipmap_create {
1728 +struct ip_set_req_macipmap {
1730 + unsigned char ethernet[ETH_ALEN];
1733 +struct ip_set_macip {
1734 + unsigned short match;
1735 + unsigned char ethernet[ETH_ALEN];
1738 +#endif /* __IP_SET_MACIPMAP_H */
1739 diff --git a/include/linux/netfilter_ipv4/ip_set_malloc.h b/include/linux/netfilter_ipv4/ip_set_malloc.h
1740 new file mode 100644
1741 index 0000000..2a80443
1743 +++ b/include/linux/netfilter_ipv4/ip_set_malloc.h
1745 +#ifndef _IP_SET_MALLOC_H
1746 +#define _IP_SET_MALLOC_H
1749 +#include <linux/vmalloc.h>
1751 +static size_t max_malloc_size = 0, max_page_size = 0;
1752 +static size_t default_max_malloc_size = 131072; /* Guaranteed: slab.c */
1754 +static inline int init_max_page_size(void)
1756 +/* Compatibility glues to support 2.4.36 */
1757 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1758 +#define __GFP_NOWARN 0
1760 + /* Guaranteed: slab.c */
1761 + max_malloc_size = max_page_size = default_max_malloc_size;
1763 + size_t page_size = 0;
1765 +#define CACHE(x) if (max_page_size == 0 || x < max_page_size) \
1767 +#include <linux/kmalloc_sizes.h>
1770 + if (max_malloc_size == 0)
1771 + max_malloc_size = page_size;
1773 + max_page_size = page_size;
1782 + size_t max_elements;
1786 +static inline void *
1787 +__harray_malloc(size_t hashsize, size_t typesize, gfp_t flags)
1789 + struct harray *harray;
1790 + size_t max_elements, size, i, j;
1792 + BUG_ON(max_page_size == 0);
1794 + if (typesize > max_page_size)
1797 + max_elements = max_page_size/typesize;
1798 + size = hashsize/max_elements;
1799 + if (hashsize % max_elements)
1802 + /* Last pointer signals end of arrays */
1803 + harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
1809 + for (i = 0; i < size - 1; i++) {
1810 + harray->arrays[i] = kmalloc(max_elements * typesize, flags);
1811 + if (!harray->arrays[i])
1813 + memset(harray->arrays[i], 0, max_elements * typesize);
1815 + harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize,
1817 + if (!harray->arrays[i])
1819 + memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
1821 + harray->max_elements = max_elements;
1822 + harray->arrays[size] = NULL;
1824 + return (void *)harray;
1827 + for (j = 0; j < i; j++) {
1828 + kfree(harray->arrays[j]);
1834 +static inline void *
1835 +harray_malloc(size_t hashsize, size_t typesize, gfp_t flags)
1840 + harray = __harray_malloc(hashsize, typesize, flags|__GFP_NOWARN);
1841 + } while (harray == NULL && init_max_page_size());
1846 +static inline void harray_free(void *h)
1848 + struct harray *harray = (struct harray *) h;
1851 + for (i = 0; harray->arrays[i] != NULL; i++)
1852 + kfree(harray->arrays[i]);
1856 +static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
1858 + struct harray *harray = (struct harray *) h;
1861 + for (i = 0; harray->arrays[i+1] != NULL; i++)
1862 + memset(harray->arrays[i], 0, harray->max_elements * typesize);
1863 + memset(harray->arrays[i], 0,
1864 + (hashsize - i * harray->max_elements) * typesize);
1867 +#define HARRAY_ELEM(h, type, which) \
1869 + struct harray *__h = (struct harray *)(h); \
1870 + ((type)((__h)->arrays[(which)/(__h)->max_elements]) \
1871 + + (which)%(__h)->max_elements); \
1874 +/* General memory allocation and deallocation */
1875 +static inline void * ip_set_malloc(size_t bytes)
1877 + BUG_ON(max_malloc_size == 0);
1879 + if (bytes > default_max_malloc_size)
1880 + return vmalloc(bytes);
1882 + return kmalloc(bytes, GFP_KERNEL | __GFP_NOWARN);
1885 +static inline void ip_set_free(void * data, size_t bytes)
1887 + BUG_ON(max_malloc_size == 0);
1889 + if (bytes > default_max_malloc_size)
1895 +#endif /* __KERNEL__ */
1897 +#endif /*_IP_SET_MALLOC_H*/
1898 diff --git a/include/linux/netfilter_ipv4/ip_set_nethash.h b/include/linux/netfilter_ipv4/ip_set_nethash.h
1899 new file mode 100644
1900 index 0000000..cf0b794
1902 +++ b/include/linux/netfilter_ipv4/ip_set_nethash.h
1904 +#ifndef __IP_SET_NETHASH_H
1905 +#define __IP_SET_NETHASH_H
1907 +#include <linux/netfilter_ipv4/ip_set.h>
1908 +#include <linux/netfilter_ipv4/ip_set_hashes.h>
1910 +#define SETTYPE_NAME "nethash"
1912 +struct ip_set_nethash {
1913 + ip_set_ip_t *members; /* the nethash proper */
1914 + uint32_t elements; /* number of elements */
1915 + uint32_t hashsize; /* hash size */
1916 + uint16_t probes; /* max number of probes */
1917 + uint16_t resize; /* resize factor in percent */
1918 + uint8_t cidr[30]; /* CIDR sizes */
1919 + uint16_t nets[30]; /* nr of nets by CIDR sizes */
1920 + initval_t initval[0]; /* initvals for jhash_1word */
1923 +struct ip_set_req_nethash_create {
1924 + uint32_t hashsize;
1929 +struct ip_set_req_nethash {
1934 +#endif /* __IP_SET_NETHASH_H */
1935 diff --git a/include/linux/netfilter_ipv4/ip_set_portmap.h b/include/linux/netfilter_ipv4/ip_set_portmap.h
1936 new file mode 100644
1937 index 0000000..37f411e
1939 +++ b/include/linux/netfilter_ipv4/ip_set_portmap.h
1941 +#ifndef __IP_SET_PORTMAP_H
1942 +#define __IP_SET_PORTMAP_H
1944 +#include <linux/netfilter_ipv4/ip_set.h>
1945 +#include <linux/netfilter_ipv4/ip_set_bitmaps.h>
1947 +#define SETTYPE_NAME "portmap"
1949 +struct ip_set_portmap {
1950 + void *members; /* the portmap proper */
1951 + ip_set_ip_t first_ip; /* host byte order, included in range */
1952 + ip_set_ip_t last_ip; /* host byte order, included in range */
1953 + u_int32_t size; /* size of the ipmap proper */
1956 +struct ip_set_req_portmap_create {
1961 +struct ip_set_req_portmap {
1965 +#endif /* __IP_SET_PORTMAP_H */
1966 diff --git a/include/linux/netfilter_ipv4/ip_set_setlist.h b/include/linux/netfilter_ipv4/ip_set_setlist.h
1967 new file mode 100644
1968 index 0000000..7cc6ed0
1970 +++ b/include/linux/netfilter_ipv4/ip_set_setlist.h
1972 +#ifndef __IP_SET_SETLIST_H
1973 +#define __IP_SET_SETLIST_H
1975 +#include <linux/netfilter_ipv4/ip_set.h>
1977 +#define SETTYPE_NAME "setlist"
1979 +#define IP_SET_SETLIST_ADD_AFTER 0
1980 +#define IP_SET_SETLIST_ADD_BEFORE 1
1982 +struct ip_set_setlist {
1984 + ip_set_id_t index[0];
1987 +struct ip_set_req_setlist_create {
1991 +struct ip_set_req_setlist {
1992 + char name[IP_SET_MAXNAMELEN];
1993 + char ref[IP_SET_MAXNAMELEN];
1997 +#endif /* __IP_SET_SETLIST_H */
1998 diff --git a/include/linux/netfilter_ipv4/ipt_set.h b/include/linux/netfilter_ipv4/ipt_set.h
1999 new file mode 100644
2000 index 0000000..2a18b93
2002 +++ b/include/linux/netfilter_ipv4/ipt_set.h
2007 +#include <linux/netfilter_ipv4/ip_set.h>
2009 +struct ipt_set_info {
2010 + ip_set_id_t index;
2011 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
2015 +struct ipt_set_info_match {
2016 + struct ipt_set_info match_set;
2019 +struct ipt_set_info_target {
2020 + struct ipt_set_info add_set;
2021 + struct ipt_set_info del_set;
2024 +#endif /*_IPT_SET_H*/
2025 diff --git a/net/ipv4/netfilter/Kconfig b/net/ipv4/netfilter/Kconfig
2026 index 1833bdb..4c315cf 100644
2027 --- a/net/ipv4/netfilter/Kconfig
2028 +++ b/net/ipv4/netfilter/Kconfig
2029 @@ -388,5 +388,122 @@ config IP_NF_ARP_MANGLE
2031 endif # IP_NF_ARPTABLES
2034 + tristate "IP set support"
2035 + depends on INET && NETFILTER
2037 + This option adds IP set support to the kernel.
2038 + In order to define and use sets, you need the userspace utility
2041 + To compile it as a module, choose M here. If unsure, say N.
2043 +config IP_NF_SET_MAX
2044 + int "Maximum number of IP sets"
2047 + depends on IP_NF_SET
2049 + You can define here default value of the maximum number
2050 + of IP sets for the kernel.
2052 + The value can be overriden by the 'max_sets' module
2053 + parameter of the 'ip_set' module.
2055 +config IP_NF_SET_HASHSIZE
2056 + int "Hash size for bindings of IP sets"
2058 + depends on IP_NF_SET
2060 + You can define here default value of the hash size for
2061 + bindings of IP sets.
2063 + The value can be overriden by the 'hash_size' module
2064 + parameter of the 'ip_set' module.
2066 +config IP_NF_SET_IPMAP
2067 + tristate "ipmap set support"
2068 + depends on IP_NF_SET
2070 + This option adds the ipmap set type support.
2072 + To compile it as a module, choose M here. If unsure, say N.
2074 +config IP_NF_SET_MACIPMAP
2075 + tristate "macipmap set support"
2076 + depends on IP_NF_SET
2078 + This option adds the macipmap set type support.
2080 + To compile it as a module, choose M here. If unsure, say N.
2082 +config IP_NF_SET_PORTMAP
2083 + tristate "portmap set support"
2084 + depends on IP_NF_SET
2086 + This option adds the portmap set type support.
2088 + To compile it as a module, choose M here. If unsure, say N.
2090 +config IP_NF_SET_IPHASH
2091 + tristate "iphash set support"
2092 + depends on IP_NF_SET
2094 + This option adds the iphash set type support.
2096 + To compile it as a module, choose M here. If unsure, say N.
2098 +config IP_NF_SET_NETHASH
2099 + tristate "nethash set support"
2100 + depends on IP_NF_SET
2102 + This option adds the nethash set type support.
2104 + To compile it as a module, choose M here. If unsure, say N.
2106 +config IP_NF_SET_IPPORTHASH
2107 + tristate "ipporthash set support"
2108 + depends on IP_NF_SET
2110 + This option adds the ipporthash set type support.
2112 + To compile it as a module, choose M here. If unsure, say N.
2114 +config IP_NF_SET_IPTREE
2115 + tristate "iptree set support"
2116 + depends on IP_NF_SET
2118 + This option adds the iptree set type support.
2120 + To compile it as a module, choose M here. If unsure, say N.
2122 +config IP_NF_SET_IPTREEMAP
2123 + tristate "iptreemap set support"
2124 + depends on IP_NF_SET
2126 + This option adds the iptreemap set type support.
2128 + To compile it as a module, choose M here. If unsure, say N.
2130 +config IP_NF_MATCH_SET
2131 + tristate "set match support"
2132 + depends on IP_NF_SET
2134 + Set matching matches against given IP sets.
2135 + You need the ipset utility to create and set up the sets.
2137 + To compile it as a module, choose M here. If unsure, say N.
2139 +config IP_NF_TARGET_SET
2140 + tristate "SET target support"
2141 + depends on IP_NF_SET
2143 + The SET target makes possible to add/delete entries
2145 + You need the ipset utility to create and set up the sets.
2147 + To compile it as a module, choose M here. If unsure, say N.
2152 diff --git a/net/ipv4/netfilter/Makefile b/net/ipv4/netfilter/Makefile
2153 index 4811159..fd4913a 100644
2154 --- a/net/ipv4/netfilter/Makefile
2155 +++ b/net/ipv4/netfilter/Makefile
2156 @@ -51,6 +51,7 @@ obj-$(CONFIG_IP_NF_SECURITY) += iptable_security.o
2157 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
2158 obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o
2159 obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
2160 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
2163 obj-$(CONFIG_IP_NF_TARGET_CLUSTERIP) += ipt_CLUSTERIP.o
2164 @@ -60,8 +61,20 @@ obj-$(CONFIG_IP_NF_TARGET_MASQUERADE) += ipt_MASQUERADE.o
2165 obj-$(CONFIG_IP_NF_TARGET_NETMAP) += ipt_NETMAP.o
2166 obj-$(CONFIG_IP_NF_TARGET_REDIRECT) += ipt_REDIRECT.o
2167 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
2168 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
2169 obj-$(CONFIG_IP_NF_TARGET_ULOG) += ipt_ULOG.o
2172 +obj-$(CONFIG_IP_NF_SET) += ip_set.o
2173 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
2174 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
2175 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
2176 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
2177 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
2178 +obj-$(CONFIG_IP_NF_SET_IPPORTHASH) += ip_set_ipporthash.o
2179 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
2180 +obj-$(CONFIG_IP_NF_SET_IPTREEMAP) += ip_set_iptreemap.o
2182 # generic ARP tables
2183 obj-$(CONFIG_IP_NF_ARPTABLES) += arp_tables.o
2184 obj-$(CONFIG_IP_NF_ARP_MANGLE) += arpt_mangle.o
2185 diff --git a/net/ipv4/netfilter/ip_set.c b/net/ipv4/netfilter/ip_set.c
2186 new file mode 100644
2187 index 0000000..7ecdc83
2189 +++ b/net/ipv4/netfilter/ip_set.c
2191 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2192 + * Patrick Schaaf <bof@bof.de>
2193 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2195 + * This program is free software; you can redistribute it and/or modify
2196 + * it under the terms of the GNU General Public License version 2 as
2197 + * published by the Free Software Foundation.
2200 +/* Kernel module for IP set management */
2202 +#include <linux/version.h>
2203 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
2204 +#include <linux/config.h>
2206 +#include <linux/module.h>
2207 +#include <linux/moduleparam.h>
2208 +#include <linux/kmod.h>
2209 +#include <linux/ip.h>
2210 +#include <linux/skbuff.h>
2211 +#include <linux/random.h>
2212 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
2213 +#include <linux/errno.h>
2214 +#include <linux/capability.h>
2215 +#include <asm/uaccess.h>
2216 +#include <asm/bitops.h>
2217 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
2218 +#include <asm/semaphore.h>
2220 +#include <linux/semaphore.h>
2222 +#include <linux/spinlock.h>
2224 +#define ASSERT_READ_LOCK(x)
2225 +#define ASSERT_WRITE_LOCK(x)
2226 +#include <linux/netfilter.h>
2227 +#include <linux/netfilter_ipv4/ip_set.h>
2229 +static struct list_head set_type_list; /* all registered sets */
2230 +static struct ip_set **ip_set_list; /* all individual sets */
2231 +static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
2232 +static struct semaphore ip_set_app_mutex; /* serializes user access */
2233 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
2234 +static int protocol_version = IP_SET_PROTOCOL_VERSION;
2236 +#define STREQ(a,b) (strncmp(a,b,IP_SET_MAXNAMELEN) == 0)
2237 +#define DONT_ALIGN (protocol_version == IP_SET_PROTOCOL_UNALIGNED)
2238 +#define ALIGNED(len) IPSET_VALIGN(len, DONT_ALIGN)
2241 + * Sets are identified either by the index in ip_set_list or by id.
2242 + * The id never changes. The index may change by swapping and used
2243 + * by external references (set/SET netfilter modules, etc.)
2245 + * Userspace requests are serialized by ip_set_mutex and sets can
2246 + * be deleted only from userspace. Therefore ip_set_list locking
2247 + * must obey the following rules:
2249 + * - kernel requests: read and write locking mandatory
2250 + * - user requests: read locking optional, write locking mandatory
2254 +__ip_set_get(ip_set_id_t index)
2256 + atomic_inc(&ip_set_list[index]->ref);
2260 +__ip_set_put(ip_set_id_t index)
2262 + atomic_dec(&ip_set_list[index]->ref);
2265 +/* Add, del and test set entries from kernel */
2268 +ip_set_testip_kernel(ip_set_id_t index,
2269 + const struct sk_buff *skb,
2270 + const u_int32_t *flags)
2272 + struct ip_set *set;
2275 + read_lock_bh(&ip_set_lock);
2276 + set = ip_set_list[index];
2277 + IP_SET_ASSERT(set);
2278 + DP("set %s, index %u", set->name, index);
2280 + read_lock_bh(&set->lock);
2281 + res = set->type->testip_kernel(set, skb, flags);
2282 + read_unlock_bh(&set->lock);
2284 + read_unlock_bh(&ip_set_lock);
2286 + return (res < 0 ? 0 : res);
2290 +ip_set_addip_kernel(ip_set_id_t index,
2291 + const struct sk_buff *skb,
2292 + const u_int32_t *flags)
2294 + struct ip_set *set;
2298 + read_lock_bh(&ip_set_lock);
2299 + set = ip_set_list[index];
2300 + IP_SET_ASSERT(set);
2301 + DP("set %s, index %u", set->name, index);
2303 + write_lock_bh(&set->lock);
2304 + res = set->type->addip_kernel(set, skb, flags);
2305 + write_unlock_bh(&set->lock);
2307 + read_unlock_bh(&ip_set_lock);
2308 + /* Retry function called without holding any lock */
2309 + if (res == -EAGAIN
2310 + && set->type->retry
2311 + && (res = set->type->retry(set)) == 0)
2318 +ip_set_delip_kernel(ip_set_id_t index,
2319 + const struct sk_buff *skb,
2320 + const u_int32_t *flags)
2322 + struct ip_set *set;
2325 + read_lock_bh(&ip_set_lock);
2326 + set = ip_set_list[index];
2327 + IP_SET_ASSERT(set);
2328 + DP("set %s, index %u", set->name, index);
2330 + write_lock_bh(&set->lock);
2331 + res = set->type->delip_kernel(set, skb, flags);
2332 + write_unlock_bh(&set->lock);
2334 + read_unlock_bh(&ip_set_lock);
2339 +/* Register and deregister settype */
2341 +static inline struct ip_set_type *
2342 +find_set_type(const char *name)
2344 + struct ip_set_type *set_type;
2346 + list_for_each_entry(set_type, &set_type_list, list)
2347 + if (STREQ(set_type->typename, name))
2353 +ip_set_register_set_type(struct ip_set_type *set_type)
2357 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
2358 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
2359 + set_type->typename,
2360 + set_type->protocol_version,
2361 + IP_SET_PROTOCOL_VERSION);
2365 + write_lock_bh(&ip_set_lock);
2366 + if (find_set_type(set_type->typename)) {
2368 + ip_set_printk("'%s' already registered!",
2369 + set_type->typename);
2373 + if (!try_module_get(THIS_MODULE)) {
2377 + list_add(&set_type->list, &set_type_list);
2378 + DP("'%s' registered.", set_type->typename);
2380 + write_unlock_bh(&ip_set_lock);
2385 +ip_set_unregister_set_type(struct ip_set_type *set_type)
2387 + write_lock_bh(&ip_set_lock);
2388 + if (!find_set_type(set_type->typename)) {
2389 + ip_set_printk("'%s' not registered?",
2390 + set_type->typename);
2393 + list_del(&set_type->list);
2394 + module_put(THIS_MODULE);
2395 + DP("'%s' unregistered.", set_type->typename);
2397 + write_unlock_bh(&ip_set_lock);
2402 +__ip_set_get_byname(const char *name, struct ip_set **set)
2404 + ip_set_id_t i, index = IP_SET_INVALID_ID;
2406 + for (i = 0; i < ip_set_max; i++) {
2407 + if (ip_set_list[i] != NULL
2408 + && STREQ(ip_set_list[i]->name, name)) {
2411 + *set = ip_set_list[i];
2419 +__ip_set_put_byindex(ip_set_id_t index)
2421 + if (ip_set_list[index])
2422 + __ip_set_put(index);
2426 + * Userspace routines
2430 + * Find set by name, reference it once. The reference makes sure the
2431 + * thing pointed to, does not go away under our feet. Drop the reference
2432 + * later, using ip_set_put().
2435 +ip_set_get_byname(const char *name)
2437 + ip_set_id_t i, index = IP_SET_INVALID_ID;
2439 + down(&ip_set_app_mutex);
2440 + for (i = 0; i < ip_set_max; i++) {
2441 + if (ip_set_list[i] != NULL
2442 + && STREQ(ip_set_list[i]->name, name)) {
2448 + up(&ip_set_app_mutex);
2453 + * Find set by index, reference it once. The reference makes sure the
2454 + * thing pointed to, does not go away under our feet. Drop the reference
2455 + * later, using ip_set_put().
2458 +ip_set_get_byindex(ip_set_id_t index)
2460 + down(&ip_set_app_mutex);
2462 + if (index >= ip_set_max)
2463 + return IP_SET_INVALID_ID;
2465 + if (ip_set_list[index])
2466 + __ip_set_get(index);
2468 + index = IP_SET_INVALID_ID;
2470 + up(&ip_set_app_mutex);
2475 + * Find the set id belonging to the index.
2476 + * We are protected by the mutex, so we do not need to use
2477 + * ip_set_lock. There is no need to reference the sets either.
2480 +ip_set_id(ip_set_id_t index)
2482 + if (index >= ip_set_max || !ip_set_list[index])
2483 + return IP_SET_INVALID_ID;
2485 + return ip_set_list[index]->id;
2489 + * If the given set pointer points to a valid set, decrement
2490 + * reference count by 1. The caller shall not assume the index
2491 + * to be valid, after calling this function.
2494 +ip_set_put_byindex(ip_set_id_t index)
2496 + down(&ip_set_app_mutex);
2497 + if (ip_set_list[index])
2498 + __ip_set_put(index);
2499 + up(&ip_set_app_mutex);
2502 +/* Find a set by name or index */
2504 +ip_set_find_byname(const char *name)
2506 + ip_set_id_t i, index = IP_SET_INVALID_ID;
2508 + for (i = 0; i < ip_set_max; i++) {
2509 + if (ip_set_list[i] != NULL
2510 + && STREQ(ip_set_list[i]->name, name)) {
2519 +ip_set_find_byindex(ip_set_id_t index)
2521 + if (index >= ip_set_max || ip_set_list[index] == NULL)
2522 + index = IP_SET_INVALID_ID;
2528 + * Add, del and test
2532 +ip_set_addip(struct ip_set *set, const void *data, u_int32_t size)
2536 + IP_SET_ASSERT(set);
2538 + write_lock_bh(&set->lock);
2539 + res = set->type->addip(set, data, size);
2540 + write_unlock_bh(&set->lock);
2541 + } while (res == -EAGAIN
2542 + && set->type->retry
2543 + && (res = set->type->retry(set)) == 0);
2549 +ip_set_delip(struct ip_set *set, const void *data, u_int32_t size)
2553 + IP_SET_ASSERT(set);
2555 + write_lock_bh(&set->lock);
2556 + res = set->type->delip(set, data, size);
2557 + write_unlock_bh(&set->lock);
2563 +ip_set_testip(struct ip_set *set, const void *data, u_int32_t size)
2567 + IP_SET_ASSERT(set);
2569 + read_lock_bh(&set->lock);
2570 + res = set->type->testip(set, data, size);
2571 + read_unlock_bh(&set->lock);
2573 + return (res > 0 ? -EEXIST : res);
2576 +static struct ip_set_type *
2577 +find_set_type_rlock(const char *typename)
2579 + struct ip_set_type *type;
2581 + read_lock_bh(&ip_set_lock);
2582 + type = find_set_type(typename);
2584 + read_unlock_bh(&ip_set_lock);
2590 +find_free_id(const char *name,
2591 + ip_set_id_t *index,
2596 + *id = IP_SET_INVALID_ID;
2597 + for (i = 0; i < ip_set_max; i++) {
2598 + if (ip_set_list[i] == NULL) {
2599 + if (*id == IP_SET_INVALID_ID)
2601 + } else if (STREQ(name, ip_set_list[i]->name))
2605 + if (*id == IP_SET_INVALID_ID)
2606 + /* No free slot remained */
2608 + /* Check that index is usable as id (swapping) */
2610 + for (i = 0; i < ip_set_max; i++) {
2611 + if (ip_set_list[i] != NULL
2612 + && ip_set_list[i]->id == *id) {
2624 +ip_set_create(const char *name,
2625 + const char *typename,
2626 + ip_set_id_t restore,
2630 + struct ip_set *set;
2631 + ip_set_id_t index = 0, id;
2634 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
2637 + * First, and without any locks, allocate and initialize
2638 + * a normal base set structure.
2640 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
2643 + rwlock_init(&set->lock);
2644 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2645 + atomic_set(&set->ref, 0);
2648 + * Next, take the &ip_set_lock, check that we know the type,
2649 + * and take a reference on the type, to make sure it
2650 + * stays available while constructing our new set.
2652 + * After referencing the type, we drop the &ip_set_lock,
2653 + * and let the new set construction run without locks.
2655 + set->type = find_set_type_rlock(typename);
2656 + if (set->type == NULL) {
2657 + /* Try loading the module */
2658 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
2659 + strcpy(modulename, "ip_set_");
2660 + strcat(modulename, typename);
2661 + DP("try to load %s", modulename);
2662 + request_module(modulename);
2663 + set->type = find_set_type_rlock(typename);
2665 + if (set->type == NULL) {
2666 + ip_set_printk("no set type '%s', set '%s' not created",
2671 + if (!try_module_get(set->type->me)) {
2672 + read_unlock_bh(&ip_set_lock);
2676 + read_unlock_bh(&ip_set_lock);
2678 + /* Check request size */
2679 + if (size != set->type->header_size) {
2680 + ip_set_printk("data length wrong (want %lu, have %lu)",
2681 + (long unsigned)set->type->header_size,
2682 + (long unsigned)size);
2687 + * Without holding any locks, create private part.
2689 + res = set->type->create(set, data, size);
2693 + /* BTW, res==0 here. */
2696 + * Here, we have a valid, constructed set. &ip_set_lock again,
2697 + * find free id/index and check that it is not already in
2700 + write_lock_bh(&ip_set_lock);
2701 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
2702 + DP("no free id!");
2706 + /* Make sure restore gets the same index */
2707 + if (restore != IP_SET_INVALID_ID && index != restore) {
2708 + DP("Can't restore, sets are screwed up");
2714 + * Finally! Add our shiny new set to the list, and be done.
2716 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2718 + ip_set_list[index] = set;
2719 + write_unlock_bh(&ip_set_lock);
2723 + write_unlock_bh(&ip_set_lock);
2724 + set->type->destroy(set);
2726 + module_put(set->type->me);
2733 + * Destroy a given existing set
2736 +ip_set_destroy_set(ip_set_id_t index)
2738 + struct ip_set *set = ip_set_list[index];
2740 + IP_SET_ASSERT(set);
2741 + DP("set: %s", set->name);
2742 + write_lock_bh(&ip_set_lock);
2743 + ip_set_list[index] = NULL;
2744 + write_unlock_bh(&ip_set_lock);
2746 + /* Must call it without holding any lock */
2747 + set->type->destroy(set);
2748 + module_put(set->type->me);
2753 + * Destroy a set - or all sets
2754 + * Sets must not be referenced/used.
2757 +ip_set_destroy(ip_set_id_t index)
2761 + /* ref modification always protected by the mutex */
2762 + if (index != IP_SET_INVALID_ID) {
2763 + if (atomic_read(&ip_set_list[index]->ref))
2765 + ip_set_destroy_set(index);
2767 + for (i = 0; i < ip_set_max; i++) {
2768 + if (ip_set_list[i] != NULL
2769 + && (atomic_read(&ip_set_list[i]->ref)))
2773 + for (i = 0; i < ip_set_max; i++) {
2774 + if (ip_set_list[i] != NULL)
2775 + ip_set_destroy_set(i);
2782 +ip_set_flush_set(struct ip_set *set)
2784 + DP("set: %s %u", set->name, set->id);
2786 + write_lock_bh(&set->lock);
2787 + set->type->flush(set);
2788 + write_unlock_bh(&set->lock);
2792 + * Flush data in a set - or in all sets
2795 +ip_set_flush(ip_set_id_t index)
2797 + if (index != IP_SET_INVALID_ID) {
2798 + IP_SET_ASSERT(ip_set_list[index]);
2799 + ip_set_flush_set(ip_set_list[index]);
2803 + for (i = 0; i < ip_set_max; i++)
2804 + if (ip_set_list[i] != NULL)
2805 + ip_set_flush_set(ip_set_list[i]);
2813 +ip_set_rename(ip_set_id_t index, const char *name)
2815 + struct ip_set *set = ip_set_list[index];
2819 + DP("set: %s to %s", set->name, name);
2820 + write_lock_bh(&ip_set_lock);
2821 + for (i = 0; i < ip_set_max; i++) {
2822 + if (ip_set_list[i] != NULL
2823 + && STREQ(ip_set_list[i]->name, name)) {
2828 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2830 + write_unlock_bh(&ip_set_lock);
2835 + * Swap two sets so that name/index points to the other.
2836 + * References are also swapped.
2839 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2841 + struct ip_set *from = ip_set_list[from_index];
2842 + struct ip_set *to = ip_set_list[to_index];
2843 + char from_name[IP_SET_MAXNAMELEN];
2844 + u_int32_t from_ref;
2846 + DP("set: %s to %s", from->name, to->name);
2847 + /* Features must not change.
2848 + * Not an artifical restriction anymore, as we must prevent
2849 + * possible loops created by swapping in setlist type of sets. */
2850 + if (from->type->features != to->type->features)
2853 + /* No magic here: ref munging protected by the mutex */
2854 + write_lock_bh(&ip_set_lock);
2855 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2856 + from_ref = atomic_read(&from->ref);
2858 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2859 + atomic_set(&from->ref, atomic_read(&to->ref));
2860 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2861 + atomic_set(&to->ref, from_ref);
2863 + ip_set_list[from_index] = to;
2864 + ip_set_list[to_index] = from;
2866 + write_unlock_bh(&ip_set_lock);
2875 +ip_set_list_set(ip_set_id_t index, void *data, int *used, int len)
2877 + struct ip_set *set = ip_set_list[index];
2878 + struct ip_set_list *set_list;
2880 + /* Pointer to our header */
2881 + set_list = data + *used;
2883 + DP("set: %s, used: %d len %u %p %p", set->name, *used, len, data, data + *used);
2885 + /* Get and ensure header size */
2886 + if (*used + ALIGNED(sizeof(struct ip_set_list)) > len)
2887 + goto not_enough_mem;
2888 + *used += ALIGNED(sizeof(struct ip_set_list));
2890 + read_lock_bh(&set->lock);
2891 + /* Get and ensure set specific header size */
2892 + set_list->header_size = ALIGNED(set->type->header_size);
2893 + if (*used + set_list->header_size > len)
2896 + /* Fill in the header */
2897 + set_list->index = index;
2898 + set_list->binding = IP_SET_INVALID_ID;
2899 + set_list->ref = atomic_read(&set->ref);
2901 + /* Fill in set spefific header data */
2902 + set->type->list_header(set, data + *used);
2903 + *used += set_list->header_size;
2905 + /* Get and ensure set specific members size */
2906 + set_list->members_size = set->type->list_members_size(set, DONT_ALIGN);
2907 + if (*used + set_list->members_size > len)
2910 + /* Fill in set spefific members data */
2911 + set->type->list_members(set, data + *used, DONT_ALIGN);
2912 + *used += set_list->members_size;
2913 + read_unlock_bh(&set->lock);
2916 + set_list->bindings_size = 0;
2921 + read_unlock_bh(&set->lock);
2923 + DP("not enough mem, try again");
2931 +ip_set_save_marker(void *data, int *used, int len)
2933 + struct ip_set_save *set_save;
2935 + DP("used %u, len %u", *used, len);
2936 + /* Get and ensure header size */
2937 + if (*used + ALIGNED(sizeof(struct ip_set_save)) > len)
2940 + /* Marker: just for backward compatibility */
2941 + set_save = data + *used;
2942 + set_save->index = IP_SET_INVALID_ID;
2943 + set_save->header_size = 0;
2944 + set_save->members_size = 0;
2945 + *used += ALIGNED(sizeof(struct ip_set_save));
2951 +ip_set_save_set(ip_set_id_t index, void *data, int *used, int len)
2953 + struct ip_set *set;
2954 + struct ip_set_save *set_save;
2956 + /* Pointer to our header */
2957 + set_save = data + *used;
2959 + /* Get and ensure header size */
2960 + if (*used + ALIGNED(sizeof(struct ip_set_save)) > len)
2961 + goto not_enough_mem;
2962 + *used += ALIGNED(sizeof(struct ip_set_save));
2964 + set = ip_set_list[index];
2965 + DP("set: %s, used: %d(%d) %p %p", set->name, *used, len,
2966 + data, data + *used);
2968 + read_lock_bh(&set->lock);
2969 + /* Get and ensure set specific header size */
2970 + set_save->header_size = ALIGNED(set->type->header_size);
2971 + if (*used + set_save->header_size > len)
2974 + /* Fill in the header */
2975 + set_save->index = index;
2976 + set_save->binding = IP_SET_INVALID_ID;
2978 + /* Fill in set spefific header data */
2979 + set->type->list_header(set, data + *used);
2980 + *used += set_save->header_size;
2982 + DP("set header filled: %s, used: %d(%lu) %p %p", set->name, *used,
2983 + (unsigned long)set_save->header_size, data, data + *used);
2984 + /* Get and ensure set specific members size */
2985 + set_save->members_size = set->type->list_members_size(set, DONT_ALIGN);
2986 + if (*used + set_save->members_size > len)
2989 + /* Fill in set spefific members data */
2990 + set->type->list_members(set, data + *used, DONT_ALIGN);
2991 + *used += set_save->members_size;
2992 + read_unlock_bh(&set->lock);
2993 + DP("set members filled: %s, used: %d(%lu) %p %p", set->name, *used,
2994 + (unsigned long)set_save->members_size, data, data + *used);
2998 + read_unlock_bh(&set->lock);
3000 + DP("not enough mem, try again");
3008 +ip_set_restore(void *data, int len)
3011 + int line = 0, used = 0, members_size;
3012 + struct ip_set *set;
3013 + struct ip_set_restore *set_restore;
3014 + ip_set_id_t index;
3016 + /* Loop to restore sets */
3020 + DP("%d %zu %d", used, ALIGNED(sizeof(struct ip_set_restore)), len);
3021 + /* Get and ensure header size */
3022 + if (used + ALIGNED(sizeof(struct ip_set_restore)) > len)
3024 + set_restore = data + used;
3025 + used += ALIGNED(sizeof(struct ip_set_restore));
3027 + /* Ensure data size */
3029 + + set_restore->header_size
3030 + + set_restore->members_size > len)
3033 + /* Check marker */
3034 + if (set_restore->index == IP_SET_INVALID_ID) {
3039 + /* Try to create the set */
3040 + DP("restore %s %s", set_restore->name, set_restore->typename);
3041 + res = ip_set_create(set_restore->name,
3042 + set_restore->typename,
3043 + set_restore->index,
3045 + set_restore->header_size);
3049 + used += ALIGNED(set_restore->header_size);
3051 + index = ip_set_find_byindex(set_restore->index);
3052 + DP("index %u, restore_index %u", index, set_restore->index);
3053 + if (index != set_restore->index)
3055 + /* Try to restore members data */
3056 + set = ip_set_list[index];
3058 + DP("members_size %lu reqsize %lu",
3059 + (unsigned long)set_restore->members_size,
3060 + (unsigned long)set->type->reqsize);
3061 + while (members_size + ALIGNED(set->type->reqsize) <=
3062 + set_restore->members_size) {
3064 + DP("members: %d, line %d", members_size, line);
3065 + res = ip_set_addip(set,
3066 + data + used + members_size,
3067 + set->type->reqsize);
3068 + if (!(res == 0 || res == -EEXIST))
3070 + members_size += ALIGNED(set->type->reqsize);
3073 + DP("members_size %lu %d",
3074 + (unsigned long)set_restore->members_size, members_size);
3075 + if (members_size != set_restore->members_size)
3077 + used += set_restore->members_size;
3088 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
3091 + int res = 0; /* Assume OK */
3094 + struct ip_set_req_adt *req_adt;
3095 + ip_set_id_t index = IP_SET_INVALID_ID;
3096 + int (*adtfn)(struct ip_set *set,
3097 + const void *data, u_int32_t size);
3099 + int (*fn)(struct ip_set *set,
3100 + const void *data, u_int32_t size);
3102 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
3105 + DP("optval=%d, user=%p, len=%d", optval, user, len);
3106 + if (!capable(CAP_NET_ADMIN))
3108 + if (optval != SO_IP_SET)
3110 + if (len <= sizeof(unsigned)) {
3111 + ip_set_printk("short userdata (want >%zu, got %u)",
3112 + sizeof(unsigned), len);
3115 + data = vmalloc(len);
3117 + DP("out of mem for %u bytes", len);
3120 + if (copy_from_user(data, user, len) != 0) {
3124 + if (down_interruptible(&ip_set_app_mutex)) {
3129 + op = (unsigned *)data;
3132 + if (*op < IP_SET_OP_VERSION) {
3133 + /* Check the version at the beginning of operations */
3134 + struct ip_set_req_version *req_version = data;
3135 + if (!(req_version->version == IP_SET_PROTOCOL_UNALIGNED
3136 + || req_version->version == IP_SET_PROTOCOL_VERSION)) {
3140 + protocol_version = req_version->version;
3144 + case IP_SET_OP_CREATE:{
3145 + struct ip_set_req_create *req_create = data;
3146 + offset = ALIGNED(sizeof(struct ip_set_req_create));
3148 + if (len < offset) {
3149 + ip_set_printk("short CREATE data (want >=%zu, got %u)",
3154 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
3155 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
3156 + res = ip_set_create(req_create->name,
3157 + req_create->typename,
3158 + IP_SET_INVALID_ID,
3163 + case IP_SET_OP_DESTROY:{
3164 + struct ip_set_req_std *req_destroy = data;
3166 + if (len != sizeof(struct ip_set_req_std)) {
3167 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
3168 + sizeof(struct ip_set_req_std), len);
3172 + if (STREQ(req_destroy->name, IPSET_TOKEN_ALL)) {
3173 + /* Destroy all sets */
3174 + index = IP_SET_INVALID_ID;
3176 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
3177 + index = ip_set_find_byname(req_destroy->name);
3179 + if (index == IP_SET_INVALID_ID) {
3185 + res = ip_set_destroy(index);
3188 + case IP_SET_OP_FLUSH:{
3189 + struct ip_set_req_std *req_flush = data;
3191 + if (len != sizeof(struct ip_set_req_std)) {
3192 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
3193 + sizeof(struct ip_set_req_std), len);
3197 + if (STREQ(req_flush->name, IPSET_TOKEN_ALL)) {
3198 + /* Flush all sets */
3199 + index = IP_SET_INVALID_ID;
3201 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
3202 + index = ip_set_find_byname(req_flush->name);
3204 + if (index == IP_SET_INVALID_ID) {
3209 + res = ip_set_flush(index);
3212 + case IP_SET_OP_RENAME:{
3213 + struct ip_set_req_create *req_rename = data;
3215 + if (len != sizeof(struct ip_set_req_create)) {
3216 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
3217 + sizeof(struct ip_set_req_create), len);
3222 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
3223 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
3225 + index = ip_set_find_byname(req_rename->name);
3226 + if (index == IP_SET_INVALID_ID) {
3230 + res = ip_set_rename(index, req_rename->typename);
3233 + case IP_SET_OP_SWAP:{
3234 + struct ip_set_req_create *req_swap = data;
3235 + ip_set_id_t to_index;
3237 + if (len != sizeof(struct ip_set_req_create)) {
3238 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
3239 + sizeof(struct ip_set_req_create), len);
3244 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
3245 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
3247 + index = ip_set_find_byname(req_swap->name);
3248 + if (index == IP_SET_INVALID_ID) {
3252 + to_index = ip_set_find_byname(req_swap->typename);
3253 + if (to_index == IP_SET_INVALID_ID) {
3257 + res = ip_set_swap(index, to_index);
3261 + break; /* Set identified by id */
3264 + /* There we may have add/del/test/bind/unbind/test_bind operations */
3265 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_IP) {
3269 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
3271 + if (len < ALIGNED(sizeof(struct ip_set_req_adt))) {
3272 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
3273 + ALIGNED(sizeof(struct ip_set_req_adt)), len);
3279 + index = ip_set_find_byindex(req_adt->index);
3280 + if (index == IP_SET_INVALID_ID) {
3285 + struct ip_set *set = ip_set_list[index];
3286 + size_t offset = ALIGNED(sizeof(struct ip_set_req_adt));
3288 + IP_SET_ASSERT(set);
3290 + if (len - offset != set->type->reqsize) {
3291 + ip_set_printk("data length wrong (want %lu, have %zu)",
3292 + (long unsigned)set->type->reqsize,
3297 + res = adtfn(set, data + offset, len - offset);
3301 + up(&ip_set_app_mutex);
3306 + DP("final result %d", res);
3311 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
3315 + ip_set_id_t index = IP_SET_INVALID_ID;
3317 + int copylen = *len;
3319 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
3320 + if (!capable(CAP_NET_ADMIN))
3322 + if (optval != SO_IP_SET)
3324 + if (*len < sizeof(unsigned)) {
3325 + ip_set_printk("short userdata (want >=%zu, got %d)",
3326 + sizeof(unsigned), *len);
3329 + data = vmalloc(*len);
3331 + DP("out of mem for %d bytes", *len);
3334 + if (copy_from_user(data, user, *len) != 0) {
3338 + if (down_interruptible(&ip_set_app_mutex)) {
3343 + op = (unsigned *) data;
3346 + if (*op < IP_SET_OP_VERSION) {
3347 + /* Check the version at the beginning of operations */
3348 + struct ip_set_req_version *req_version = data;
3349 + if (!(req_version->version == IP_SET_PROTOCOL_UNALIGNED
3350 + || req_version->version == IP_SET_PROTOCOL_VERSION)) {
3354 + protocol_version = req_version->version;
3358 + case IP_SET_OP_VERSION: {
3359 + struct ip_set_req_version *req_version = data;
3361 + if (*len != sizeof(struct ip_set_req_version)) {
3362 + ip_set_printk("invalid VERSION (want %zu, got %d)",
3363 + sizeof(struct ip_set_req_version),
3369 + req_version->version = IP_SET_PROTOCOL_VERSION;
3370 + res = copy_to_user(user, req_version,
3371 + sizeof(struct ip_set_req_version));
3374 + case IP_SET_OP_GET_BYNAME: {
3375 + struct ip_set_req_get_set *req_get = data;
3377 + if (*len != sizeof(struct ip_set_req_get_set)) {
3378 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
3379 + sizeof(struct ip_set_req_get_set), *len);
3383 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3384 + index = ip_set_find_byname(req_get->set.name);
3385 + req_get->set.index = index;
3388 + case IP_SET_OP_GET_BYINDEX: {
3389 + struct ip_set_req_get_set *req_get = data;
3391 + if (*len != sizeof(struct ip_set_req_get_set)) {
3392 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
3393 + sizeof(struct ip_set_req_get_set), *len);
3397 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3398 + index = ip_set_find_byindex(req_get->set.index);
3399 + strncpy(req_get->set.name,
3400 + index == IP_SET_INVALID_ID ? ""
3401 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
3404 + case IP_SET_OP_ADT_GET: {
3405 + struct ip_set_req_adt_get *req_get = data;
3407 + if (*len != sizeof(struct ip_set_req_adt_get)) {
3408 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
3409 + sizeof(struct ip_set_req_adt_get), *len);
3413 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3414 + index = ip_set_find_byname(req_get->set.name);
3415 + if (index != IP_SET_INVALID_ID) {
3416 + req_get->set.index = index;
3417 + strncpy(req_get->typename,
3418 + ip_set_list[index]->type->typename,
3419 + IP_SET_MAXNAMELEN - 1);
3426 + case IP_SET_OP_MAX_SETS: {
3427 + struct ip_set_req_max_sets *req_max_sets = data;
3430 + if (*len != sizeof(struct ip_set_req_max_sets)) {
3431 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
3432 + sizeof(struct ip_set_req_max_sets), *len);
3437 + if (STREQ(req_max_sets->set.name, IPSET_TOKEN_ALL)) {
3438 + req_max_sets->set.index = IP_SET_INVALID_ID;
3440 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
3441 + req_max_sets->set.index =
3442 + ip_set_find_byname(req_max_sets->set.name);
3443 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
3448 + req_max_sets->max_sets = ip_set_max;
3449 + req_max_sets->sets = 0;
3450 + for (i = 0; i < ip_set_max; i++) {
3451 + if (ip_set_list[i] != NULL)
3452 + req_max_sets->sets++;
3456 + case IP_SET_OP_LIST_SIZE:
3457 + case IP_SET_OP_SAVE_SIZE: {
3458 + struct ip_set_req_setnames *req_setnames = data;
3459 + struct ip_set_name_list *name_list;
3460 + struct ip_set *set;
3464 + if (*len < ALIGNED(sizeof(struct ip_set_req_setnames))) {
3465 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
3466 + ALIGNED(sizeof(struct ip_set_req_setnames)),
3472 + req_setnames->size = 0;
3473 + used = ALIGNED(sizeof(struct ip_set_req_setnames));
3474 + for (i = 0; i < ip_set_max; i++) {
3475 + if (ip_set_list[i] == NULL)
3477 + name_list = data + used;
3478 + used += ALIGNED(sizeof(struct ip_set_name_list));
3479 + if (used > copylen) {
3483 + set = ip_set_list[i];
3484 + /* Fill in index, name, etc. */
3485 + name_list->index = i;
3486 + name_list->id = set->id;
3487 + strncpy(name_list->name,
3489 + IP_SET_MAXNAMELEN - 1);
3490 + strncpy(name_list->typename,
3491 + set->type->typename,
3492 + IP_SET_MAXNAMELEN - 1);
3493 + DP("filled %s of type %s, index %u\n",
3494 + name_list->name, name_list->typename,
3495 + name_list->index);
3496 + if (!(req_setnames->index == IP_SET_INVALID_ID
3497 + || req_setnames->index == i))
3500 + req_setnames->size +=
3501 + (*op == IP_SET_OP_LIST_SIZE ?
3502 + ALIGNED(sizeof(struct ip_set_list)) :
3503 + ALIGNED(sizeof(struct ip_set_save)))
3504 + + ALIGNED(set->type->header_size)
3505 + + set->type->list_members_size(set, DONT_ALIGN);
3507 + if (copylen != used) {
3513 + case IP_SET_OP_LIST: {
3514 + struct ip_set_req_list *req_list = data;
3518 + if (*len < sizeof(struct ip_set_req_list)) {
3519 + ip_set_printk("short LIST (want >=%zu, got %d)",
3520 + sizeof(struct ip_set_req_list), *len);
3524 + index = req_list->index;
3525 + if (index != IP_SET_INVALID_ID
3526 + && ip_set_find_byindex(index) != index) {
3531 + if (index == IP_SET_INVALID_ID) {
3532 + /* List all sets */
3533 + for (i = 0; i < ip_set_max && res == 0; i++) {
3534 + if (ip_set_list[i] != NULL)
3535 + res = ip_set_list_set(i, data, &used, *len);
3538 + /* List an individual set */
3539 + res = ip_set_list_set(index, data, &used, *len);
3543 + else if (copylen != used) {
3549 + case IP_SET_OP_SAVE: {
3550 + struct ip_set_req_list *req_save = data;
3554 + if (*len < sizeof(struct ip_set_req_list)) {
3555 + ip_set_printk("short SAVE (want >=%zu, got %d)",
3556 + sizeof(struct ip_set_req_list), *len);
3560 + index = req_save->index;
3561 + if (index != IP_SET_INVALID_ID
3562 + && ip_set_find_byindex(index) != index) {
3567 +#define SETLIST(set) (strcmp(set->type->typename, "setlist") == 0)
3570 + if (index == IP_SET_INVALID_ID) {
3571 + /* Save all sets: ugly setlist type dependency */
3574 + for (i = 0; i < ip_set_max && res == 0; i++) {
3575 + if (ip_set_list[i] != NULL
3576 + && !(setlist ^ SETLIST(ip_set_list[i])))
3577 + res = ip_set_save_set(i, data, &used, *len);
3584 + /* Save an individual set */
3585 + res = ip_set_save_set(index, data, &used, *len);
3588 + res = ip_set_save_marker(data, &used, *len);
3592 + else if (copylen != used) {
3598 + case IP_SET_OP_RESTORE: {
3599 + struct ip_set_req_setnames *req_restore = data;
3600 + size_t offset = ALIGNED(sizeof(struct ip_set_req_setnames));
3603 + if (*len < offset || *len != req_restore->size) {
3604 + ip_set_printk("invalid RESTORE (want =%lu, got %d)",
3605 + (long unsigned)req_restore->size, *len);
3609 + line = ip_set_restore(data + offset, req_restore->size - offset);
3610 + DP("ip_set_restore: %d", line);
3613 + req_restore->size = line;
3614 + copylen = sizeof(struct ip_set_req_setnames);
3622 + } /* end of switch(op) */
3625 + DP("set %s, copylen %d", index != IP_SET_INVALID_ID
3626 + && ip_set_list[index]
3627 + ? ip_set_list[index]->name
3628 + : ":all:", copylen);
3629 + res = copy_to_user(user, data, copylen);
3632 + up(&ip_set_app_mutex);
3637 + DP("final result %d", res);
3641 +static struct nf_sockopt_ops so_set = {
3643 + .set_optmin = SO_IP_SET,
3644 + .set_optmax = SO_IP_SET + 1,
3645 + .set = &ip_set_sockfn_set,
3646 + .get_optmin = SO_IP_SET,
3647 + .get_optmax = SO_IP_SET + 1,
3648 + .get = &ip_set_sockfn_get,
3649 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3652 + .owner = THIS_MODULE,
3656 +static int max_sets;
3658 +module_param(max_sets, int, 0600);
3659 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3660 +MODULE_LICENSE("GPL");
3661 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3662 +MODULE_DESCRIPTION("module implementing core IP set support");
3669 + /* For the -rt branch, DECLARE_MUTEX/init_MUTEX avoided */
3670 + sema_init(&ip_set_app_mutex, 1);
3673 + ip_set_max = max_sets;
3674 + if (ip_set_max >= IP_SET_INVALID_ID)
3675 + ip_set_max = IP_SET_INVALID_ID - 1;
3677 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3678 + if (!ip_set_list) {
3679 + printk(KERN_ERR "Unable to create ip_set_list\n");
3682 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3684 + INIT_LIST_HEAD(&set_type_list);
3686 + res = nf_register_sockopt(&so_set);
3688 + ip_set_printk("SO_SET registry failed: %d", res);
3689 + vfree(ip_set_list);
3693 + printk("ip_set version %u loaded\n", IP_SET_PROTOCOL_VERSION);
3700 + /* There can't be any existing set or binding */
3701 + nf_unregister_sockopt(&so_set);
3702 + vfree(ip_set_list);
3703 + DP("these are the famous last words");
3706 +EXPORT_SYMBOL(ip_set_register_set_type);
3707 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3709 +EXPORT_SYMBOL(ip_set_get_byname);
3710 +EXPORT_SYMBOL(ip_set_get_byindex);
3711 +EXPORT_SYMBOL(ip_set_put_byindex);
3712 +EXPORT_SYMBOL(ip_set_id);
3713 +EXPORT_SYMBOL(__ip_set_get_byname);
3714 +EXPORT_SYMBOL(__ip_set_put_byindex);
3716 +EXPORT_SYMBOL(ip_set_addip_kernel);
3717 +EXPORT_SYMBOL(ip_set_delip_kernel);
3718 +EXPORT_SYMBOL(ip_set_testip_kernel);
3720 +module_init(ip_set_init);
3721 +module_exit(ip_set_fini);
3722 diff --git a/net/ipv4/netfilter/ip_set_iphash.c b/net/ipv4/netfilter/ip_set_iphash.c
3723 new file mode 100644
3724 index 0000000..1accbe3
3726 +++ b/net/ipv4/netfilter/ip_set_iphash.c
3728 +/* Copyright (C) 2003-2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3730 + * This program is free software; you can redistribute it and/or modify
3731 + * it under the terms of the GNU General Public License version 2 as
3732 + * published by the Free Software Foundation.
3735 +/* Kernel module implementing an ip hash set */
3737 +#include <linux/module.h>
3738 +#include <linux/moduleparam.h>
3739 +#include <linux/ip.h>
3740 +#include <linux/skbuff.h>
3741 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
3742 +#include <linux/errno.h>
3743 +#include <asm/uaccess.h>
3744 +#include <asm/bitops.h>
3745 +#include <linux/spinlock.h>
3746 +#include <linux/random.h>
3748 +#include <net/ip.h>
3750 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3752 +static int limit = MAX_RANGE;
3754 +static inline __u32
3755 +iphash_id(struct ip_set *set, ip_set_ip_t ip)
3757 + struct ip_set_iphash *map = set->data;
3760 + ip_set_ip_t *elem;
3763 + ip &= map->netmask;
3764 + DP("set: %s, ip:%u.%u.%u.%u", set->name, HIPQUAD(ip));
3765 + for (i = 0; i < map->probes; i++) {
3766 + id = jhash_ip(map, i, ip) % map->hashsize;
3767 + DP("hash key: %u", id);
3768 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3771 + /* No shortcut - there can be deleted entries. */
3777 +iphash_test(struct ip_set *set, ip_set_ip_t ip)
3779 + return (ip && iphash_id(set, ip) != UINT_MAX);
3782 +#define KADT_CONDITION
3785 +KADT(iphash, test, ipaddr)
3788 +__iphash_add(struct ip_set_iphash *map, ip_set_ip_t *ip)
3792 + ip_set_ip_t *elem, *slot = NULL;
3794 + for (i = 0; i < map->probes; i++) {
3795 + probe = jhash_ip(map, i, *ip) % map->hashsize;
3796 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
3799 + if (!(slot || *elem))
3801 + /* There can be deleted entries, must check all slots */
3808 + /* Trigger rehashing */
3813 +iphash_add(struct ip_set *set, ip_set_ip_t ip)
3815 + struct ip_set_iphash *map = set->data;
3817 + if (!ip || map->elements >= limit)
3820 + ip &= map->netmask;
3821 + return __iphash_add(map, &ip);
3825 +KADT(iphash, add, ipaddr)
3828 +__iphash_retry(struct ip_set_iphash *tmp, struct ip_set_iphash *map)
3830 + tmp->netmask = map->netmask;
3833 +HASH_RETRY(iphash, ip_set_ip_t)
3836 +iphash_del(struct ip_set *set, ip_set_ip_t ip)
3838 + struct ip_set_iphash *map = set->data;
3839 + ip_set_ip_t id, *elem;
3844 + id = iphash_id(set, ip);
3845 + if (id == UINT_MAX)
3848 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3856 +KADT(iphash, del, ipaddr)
3859 +__iphash_create(const struct ip_set_req_iphash_create *req,
3860 + struct ip_set_iphash *map)
3862 + map->netmask = req->netmask;
3867 +HASH_CREATE(iphash, ip_set_ip_t)
3868 +HASH_DESTROY(iphash)
3870 +HASH_FLUSH(iphash, ip_set_ip_t)
3873 +__iphash_list_header(const struct ip_set_iphash *map,
3874 + struct ip_set_req_iphash_create *header)
3876 + header->netmask = map->netmask;
3879 +HASH_LIST_HEADER(iphash)
3880 +HASH_LIST_MEMBERS_SIZE(iphash, ip_set_ip_t)
3881 +HASH_LIST_MEMBERS(iphash, ip_set_ip_t)
3883 +IP_SET_RTYPE(iphash, IPSET_TYPE_IP | IPSET_DATA_SINGLE)
3885 +MODULE_LICENSE("GPL");
3886 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3887 +MODULE_DESCRIPTION("iphash type of IP sets");
3888 +module_param(limit, int, 0600);
3889 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
3891 +REGISTER_MODULE(iphash)
3892 diff --git a/net/ipv4/netfilter/ip_set_ipmap.c b/net/ipv4/netfilter/ip_set_ipmap.c
3893 new file mode 100644
3894 index 0000000..be3c538
3896 +++ b/net/ipv4/netfilter/ip_set_ipmap.c
3898 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3899 + * Patrick Schaaf <bof@bof.de>
3900 + * Copyright (C) 2003-2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3902 + * This program is free software; you can redistribute it and/or modify
3903 + * it under the terms of the GNU General Public License version 2 as
3904 + * published by the Free Software Foundation.
3907 +/* Kernel module implementing an IP set type: the single bitmap type */
3909 +#include <linux/module.h>
3910 +#include <linux/ip.h>
3911 +#include <linux/skbuff.h>
3912 +#include <linux/errno.h>
3913 +#include <asm/uaccess.h>
3914 +#include <asm/bitops.h>
3915 +#include <linux/spinlock.h>
3917 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3919 +static inline ip_set_ip_t
3920 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3922 + return ((ip & map->netmask) - map->first_ip)/map->hosts;
3926 +ipmap_test(const struct ip_set *set, ip_set_ip_t ip)
3928 + const struct ip_set_ipmap *map = set->data;
3930 + if (ip < map->first_ip || ip > map->last_ip)
3933 + DP("set: %s, ip:%u.%u.%u.%u", set->name, HIPQUAD(ip));
3934 + return !!test_bit(ip_to_id(map, ip), map->members);
3937 +#define KADT_CONDITION
3940 +KADT(ipmap, test, ipaddr)
3943 +ipmap_add(struct ip_set *set, ip_set_ip_t ip)
3945 + struct ip_set_ipmap *map = set->data;
3947 + if (ip < map->first_ip || ip > map->last_ip)
3950 + DP("set: %s, ip:%u.%u.%u.%u", set->name, HIPQUAD(ip));
3951 + if (test_and_set_bit(ip_to_id(map, ip), map->members))
3958 +KADT(ipmap, add, ipaddr)
3961 +ipmap_del(struct ip_set *set, ip_set_ip_t ip)
3963 + struct ip_set_ipmap *map = set->data;
3965 + if (ip < map->first_ip || ip > map->last_ip)
3968 + DP("set: %s, ip:%u.%u.%u.%u", set->name, HIPQUAD(ip));
3969 + if (!test_and_clear_bit(ip_to_id(map, ip), map->members))
3976 +KADT(ipmap, del, ipaddr)
3979 +__ipmap_create(const struct ip_set_req_ipmap_create *req,
3980 + struct ip_set_ipmap *map)
3982 + map->netmask = req->netmask;
3984 + if (req->netmask == 0xFFFFFFFF) {
3986 + map->sizeid = map->last_ip - map->first_ip + 1;
3988 + unsigned int mask_bits, netmask_bits;
3991 + map->first_ip &= map->netmask; /* Should we better bark? */
3993 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3994 + netmask_bits = mask_to_bits(map->netmask);
3996 + if ((!mask && (map->first_ip || map->last_ip != 0xFFFFFFFF))
3997 + || netmask_bits <= mask_bits)
4000 + DP("mask_bits %u, netmask_bits %u",
4001 + mask_bits, netmask_bits);
4002 + map->hosts = 2 << (32 - netmask_bits - 1);
4003 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
4005 + if (map->sizeid > MAX_RANGE + 1) {
4006 + ip_set_printk("range too big, %d elements (max %d)",
4007 + map->sizeid, MAX_RANGE+1);
4010 + DP("hosts %u, sizeid %u", map->hosts, map->sizeid);
4011 + return bitmap_bytes(0, map->sizeid - 1);
4014 +BITMAP_CREATE(ipmap)
4015 +BITMAP_DESTROY(ipmap)
4016 +BITMAP_FLUSH(ipmap)
4019 +__ipmap_list_header(const struct ip_set_ipmap *map,
4020 + struct ip_set_req_ipmap_create *header)
4022 + header->netmask = map->netmask;
4025 +BITMAP_LIST_HEADER(ipmap)
4026 +BITMAP_LIST_MEMBERS_SIZE(ipmap, ip_set_ip_t, map->sizeid,
4027 + test_bit(i, map->members))
4030 +ipmap_list_members(const struct ip_set *set, void *data, char dont_align)
4032 + const struct ip_set_ipmap *map = set->data;
4033 + uint32_t i, n = 0;
4037 + memcpy(data, map->members, map->size);
4041 + for (i = 0; i < map->sizeid; i++)
4042 + if (test_bit(i, map->members)) {
4043 + d = data + n * IPSET_ALIGN(sizeof(ip_set_ip_t));
4044 + *d = map->first_ip + i * map->hosts;
4049 +IP_SET_TYPE(ipmap, IPSET_TYPE_IP | IPSET_DATA_SINGLE)
4051 +MODULE_LICENSE("GPL");
4052 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4053 +MODULE_DESCRIPTION("ipmap type of IP sets");
4055 +REGISTER_MODULE(ipmap)
4056 diff --git a/net/ipv4/netfilter/ip_set_ipporthash.c b/net/ipv4/netfilter/ip_set_ipporthash.c
4057 new file mode 100644
4058 index 0000000..4a79c8b
4060 +++ b/net/ipv4/netfilter/ip_set_ipporthash.c
4062 +/* Copyright (C) 2003-2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4064 + * This program is free software; you can redistribute it and/or modify
4065 + * it under the terms of the GNU General Public License version 2 as
4066 + * published by the Free Software Foundation.
4069 +/* Kernel module implementing an ip+port hash set */
4071 +#include <linux/module.h>
4072 +#include <linux/moduleparam.h>
4073 +#include <linux/ip.h>
4074 +#include <linux/tcp.h>
4075 +#include <linux/udp.h>
4076 +#include <linux/skbuff.h>
4077 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
4078 +#include <linux/errno.h>
4079 +#include <asm/uaccess.h>
4080 +#include <asm/bitops.h>
4081 +#include <linux/spinlock.h>
4082 +#include <linux/random.h>
4084 +#include <net/ip.h>
4086 +#include <linux/netfilter_ipv4/ip_set_ipporthash.h>
4087 +#include <linux/netfilter_ipv4/ip_set_getport.h>
4089 +static int limit = MAX_RANGE;
4091 +static inline __u32
4092 +ipporthash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port)
4094 + struct ip_set_ipporthash *map = set->data;
4097 + ip_set_ip_t *elem;
4099 + ip = pack_ip_port(map, ip, port);
4104 + for (i = 0; i < map->probes; i++) {
4105 + id = jhash_ip(map, i, ip) % map->hashsize;
4106 + DP("hash key: %u", id);
4107 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4110 + /* No shortcut - there can be deleted entries. */
4116 +ipporthash_test(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port)
4118 + struct ip_set_ipporthash *map = set->data;
4120 + if (ip < map->first_ip || ip > map->last_ip)
4123 + return (ipporthash_id(set, ip, port) != UINT_MAX);
4126 +#define KADT_CONDITION \
4127 + ip_set_ip_t port; \
4129 + if (flags[1] == 0) \
4132 + port = get_port(skb, ++flags); \
4134 + if (port == INVALID_PORT) \
4137 +UADT(ipporthash, test, req->port)
4138 +KADT(ipporthash, test, ipaddr, port)
4141 +__ipporthash_add(struct ip_set_ipporthash *map, ip_set_ip_t *ip)
4145 + ip_set_ip_t *elem, *slot = NULL;
4147 + for (i = 0; i < map->probes; i++) {
4148 + probe = jhash_ip(map, i, *ip) % map->hashsize;
4149 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
4152 + if (!(slot || *elem))
4154 + /* There can be deleted entries, must check all slots */
4161 + /* Trigger rehashing */
4166 +ipporthash_add(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port)
4168 + struct ip_set_ipporthash *map = set->data;
4169 + if (map->elements > limit)
4171 + if (ip < map->first_ip || ip > map->last_ip)
4174 + ip = pack_ip_port(map, ip, port);
4179 + return __ipporthash_add(map, &ip);
4182 +UADT(ipporthash, add, req->port)
4183 +KADT(ipporthash, add, ipaddr, port)
4186 +__ipporthash_retry(struct ip_set_ipporthash *tmp,
4187 + struct ip_set_ipporthash *map)
4189 + tmp->first_ip = map->first_ip;
4190 + tmp->last_ip = map->last_ip;
4193 +HASH_RETRY(ipporthash, ip_set_ip_t)
4196 +ipporthash_del(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t port)
4198 + struct ip_set_ipporthash *map = set->data;
4200 + ip_set_ip_t *elem;
4202 + if (ip < map->first_ip || ip > map->last_ip)
4205 + id = ipporthash_id(set, ip, port);
4207 + if (id == UINT_MAX)
4210 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
4217 +UADT(ipporthash, del, req->port)
4218 +KADT(ipporthash, del, ipaddr, port)
4221 +__ipporthash_create(const struct ip_set_req_ipporthash_create *req,
4222 + struct ip_set_ipporthash *map)
4224 + if (req->to - req->from > MAX_RANGE) {
4225 + ip_set_printk("range too big, %d elements (max %d)",
4226 + req->to - req->from + 1, MAX_RANGE+1);
4229 + map->first_ip = req->from;
4230 + map->last_ip = req->to;
4234 +HASH_CREATE(ipporthash, ip_set_ip_t)
4235 +HASH_DESTROY(ipporthash)
4236 +HASH_FLUSH(ipporthash, ip_set_ip_t)
4239 +__ipporthash_list_header(const struct ip_set_ipporthash *map,
4240 + struct ip_set_req_ipporthash_create *header)
4242 + header->from = map->first_ip;
4243 + header->to = map->last_ip;
4246 +HASH_LIST_HEADER(ipporthash)
4247 +HASH_LIST_MEMBERS_SIZE(ipporthash, ip_set_ip_t)
4248 +HASH_LIST_MEMBERS(ipporthash, ip_set_ip_t)
4250 +IP_SET_RTYPE(ipporthash, IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_DATA_DOUBLE)
4252 +MODULE_LICENSE("GPL");
4253 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4254 +MODULE_DESCRIPTION("ipporthash type of IP sets");
4255 +module_param(limit, int, 0600);
4256 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
4258 +REGISTER_MODULE(ipporthash)
4259 diff --git a/net/ipv4/netfilter/ip_set_ipportiphash.c b/net/ipv4/netfilter/ip_set_ipportiphash.c
4260 new file mode 100644
4261 index 0000000..c80087f
4263 +++ b/net/ipv4/netfilter/ip_set_ipportiphash.c
4265 +/* Copyright (C) 2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4267 + * This program is free software; you can redistribute it and/or modify
4268 + * it under the terms of the GNU General Public License version 2 as
4269 + * published by the Free Software Foundation.
4272 +/* Kernel module implementing an ip+port+ip hash set */
4274 +#include <linux/module.h>
4275 +#include <linux/moduleparam.h>
4276 +#include <linux/ip.h>
4277 +#include <linux/tcp.h>
4278 +#include <linux/udp.h>
4279 +#include <linux/skbuff.h>
4280 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
4281 +#include <linux/errno.h>
4282 +#include <asm/uaccess.h>
4283 +#include <asm/bitops.h>
4284 +#include <linux/spinlock.h>
4285 +#include <linux/random.h>
4287 +#include <net/ip.h>
4289 +#include <linux/netfilter_ipv4/ip_set_ipportiphash.h>
4290 +#include <linux/netfilter_ipv4/ip_set_getport.h>
4292 +static int limit = MAX_RANGE;
4294 +#define jhash_ip2(map, i, ipport, ip1) \
4295 + jhash_2words(ipport, ip1, *(map->initval + i))
4297 +static inline __u32
4298 +ipportiphash_id(struct ip_set *set,
4299 + ip_set_ip_t ip, ip_set_ip_t port, ip_set_ip_t ip1)
4301 + struct ip_set_ipportiphash *map = set->data;
4304 + struct ipportip *elem;
4306 + ip = pack_ip_port(map, ip, port);
4310 + for (i = 0; i < map->probes; i++) {
4311 + id = jhash_ip2(map, i, ip, ip1) % map->hashsize;
4312 + DP("hash key: %u", id);
4313 + elem = HARRAY_ELEM(map->members, struct ipportip *, id);
4314 + if (elem->ip == ip && elem->ip1 == ip1)
4316 + /* No shortcut - there can be deleted entries. */
4322 +ipportiphash_test(struct ip_set *set,
4323 + ip_set_ip_t ip, ip_set_ip_t port, ip_set_ip_t ip1)
4325 + struct ip_set_ipportiphash *map = set->data;
4327 + if (ip < map->first_ip || ip > map->last_ip)
4330 + return (ipportiphash_id(set, ip, port, ip1) != UINT_MAX);
4333 +#define KADT_CONDITION \
4334 + ip_set_ip_t port, ip1; \
4336 + if (flags[2] == 0) \
4339 + port = get_port(skb, ++flags); \
4340 + ip1 = ipaddr(skb, ++flags); \
4342 + if (port == INVALID_PORT) \
4345 +UADT(ipportiphash, test, req->port, req->ip1)
4346 +KADT(ipportiphash, test, ipaddr, port, ip1)
4349 +__ipportip_add(struct ip_set_ipportiphash *map,
4350 + ip_set_ip_t ip, ip_set_ip_t ip1)
4354 + struct ipportip *elem, *slot = NULL;
4356 + for (i = 0; i < map->probes; i++) {
4357 + probe = jhash_ip2(map, i, ip, ip1) % map->hashsize;
4358 + elem = HARRAY_ELEM(map->members, struct ipportip *, probe);
4359 + if (elem->ip == ip && elem->ip1 == ip1)
4361 + if (!(slot || elem->ip || elem->ip1))
4363 + /* There can be deleted entries, must check all slots */
4371 + /* Trigger rehashing */
4376 +__ipportiphash_add(struct ip_set_ipportiphash *map,
4377 + struct ipportip *elem)
4379 + return __ipportip_add(map, elem->ip, elem->ip1);
4383 +ipportiphash_add(struct ip_set *set,
4384 + ip_set_ip_t ip, ip_set_ip_t port, ip_set_ip_t ip1)
4386 + struct ip_set_ipportiphash *map = set->data;
4388 + if (map->elements > limit)
4390 + if (ip < map->first_ip || ip > map->last_ip)
4393 + ip = pack_ip_port(map, ip, port);
4397 + return __ipportip_add(map, ip, ip1);
4400 +UADT(ipportiphash, add, req->port, req->ip1)
4401 +KADT(ipportiphash, add, ipaddr, port, ip1)
4404 +__ipportiphash_retry(struct ip_set_ipportiphash *tmp,
4405 + struct ip_set_ipportiphash *map)
4407 + tmp->first_ip = map->first_ip;
4408 + tmp->last_ip = map->last_ip;
4411 +HASH_RETRY2(ipportiphash, struct ipportip)
4414 +ipportiphash_del(struct ip_set *set,
4415 + ip_set_ip_t ip, ip_set_ip_t port, ip_set_ip_t ip1)
4417 + struct ip_set_ipportiphash *map = set->data;
4419 + struct ipportip *elem;
4421 + if (ip < map->first_ip || ip > map->last_ip)
4424 + id = ipportiphash_id(set, ip, port, ip1);
4426 + if (id == UINT_MAX)
4429 + elem = HARRAY_ELEM(map->members, struct ipportip *, id);
4430 + elem->ip = elem->ip1 = 0;
4436 +UADT(ipportiphash, del, req->port, req->ip1)
4437 +KADT(ipportiphash, del, ipaddr, port, ip1)
4440 +__ipportiphash_create(const struct ip_set_req_ipportiphash_create *req,
4441 + struct ip_set_ipportiphash *map)
4443 + if (req->to - req->from > MAX_RANGE) {
4444 + ip_set_printk("range too big, %d elements (max %d)",
4445 + req->to - req->from + 1, MAX_RANGE+1);
4448 + map->first_ip = req->from;
4449 + map->last_ip = req->to;
4453 +HASH_CREATE(ipportiphash, struct ipportip)
4454 +HASH_DESTROY(ipportiphash)
4455 +HASH_FLUSH(ipportiphash, struct ipportip)
4458 +__ipportiphash_list_header(const struct ip_set_ipportiphash *map,
4459 + struct ip_set_req_ipportiphash_create *header)
4461 + header->from = map->first_ip;
4462 + header->to = map->last_ip;
4465 +HASH_LIST_HEADER(ipportiphash)
4466 +HASH_LIST_MEMBERS_SIZE(ipportiphash, struct ipportip)
4467 +HASH_LIST_MEMBERS_MEMCPY(ipportiphash, struct ipportip,
4468 + (elem->ip || elem->ip1))
4470 +IP_SET_RTYPE(ipportiphash, IPSET_TYPE_IP | IPSET_TYPE_PORT
4471 + | IPSET_TYPE_IP1 | IPSET_DATA_TRIPLE)
4473 +MODULE_LICENSE("GPL");
4474 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4475 +MODULE_DESCRIPTION("ipportiphash type of IP sets");
4476 +module_param(limit, int, 0600);
4477 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
4479 +REGISTER_MODULE(ipportiphash)
4480 diff --git a/net/ipv4/netfilter/ip_set_ipportnethash.c b/net/ipv4/netfilter/ip_set_ipportnethash.c
4481 new file mode 100644
4482 index 0000000..2680cd9
4484 +++ b/net/ipv4/netfilter/ip_set_ipportnethash.c
4486 +/* Copyright (C) 2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4488 + * This program is free software; you can redistribute it and/or modify
4489 + * it under the terms of the GNU General Public License version 2 as
4490 + * published by the Free Software Foundation.
4493 +/* Kernel module implementing an ip+port+net hash set */
4495 +#include <linux/module.h>
4496 +#include <linux/moduleparam.h>
4497 +#include <linux/ip.h>
4498 +#include <linux/tcp.h>
4499 +#include <linux/udp.h>
4500 +#include <linux/skbuff.h>
4501 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
4502 +#include <linux/errno.h>
4503 +#include <asm/uaccess.h>
4504 +#include <asm/bitops.h>
4505 +#include <linux/spinlock.h>
4506 +#include <linux/random.h>
4508 +#include <net/ip.h>
4510 +#include <linux/netfilter_ipv4/ip_set_ipportnethash.h>
4511 +#include <linux/netfilter_ipv4/ip_set_getport.h>
4513 +static int limit = MAX_RANGE;
4515 +#define jhash_ip2(map, i, ipport, ip1) \
4516 + jhash_2words(ipport, ip1, *(map->initval + i))
4518 +static inline __u32
4519 +ipportnethash_id_cidr(struct ip_set *set,
4520 + ip_set_ip_t ip, ip_set_ip_t port,
4521 + ip_set_ip_t ip1, uint8_t cidr)
4523 + struct ip_set_ipportnethash *map = set->data;
4526 + struct ipportip *elem;
4528 + ip = pack_ip_port(map, ip, port);
4529 + ip1 = pack_ip_cidr(ip1, cidr);
4533 + for (i = 0; i < map->probes; i++) {
4534 + id = jhash_ip2(map, i, ip, ip1) % map->hashsize;
4535 + DP("hash key: %u", id);
4536 + elem = HARRAY_ELEM(map->members, struct ipportip *, id);
4537 + if (elem->ip == ip && elem->ip1 == ip1)
4539 + /* No shortcut - there can be deleted entries. */
4544 +static inline __u32
4545 +ipportnethash_id(struct ip_set *set,
4546 + ip_set_ip_t ip, ip_set_ip_t port, ip_set_ip_t ip1)
4548 + struct ip_set_ipportnethash *map = set->data;
4549 + __u32 id = UINT_MAX;
4552 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4553 + id = ipportnethash_id_cidr(set, ip, port, ip1, map->cidr[i]);
4554 + if (id != UINT_MAX)
4561 +ipportnethash_test_cidr(struct ip_set *set,
4562 + ip_set_ip_t ip, ip_set_ip_t port,
4563 + ip_set_ip_t ip1, uint8_t cidr)
4565 + struct ip_set_ipportnethash *map = set->data;
4567 + if (ip < map->first_ip || ip > map->last_ip)
4570 + return (ipportnethash_id_cidr(set, ip, port, ip1, cidr) != UINT_MAX);
4574 +ipportnethash_test(struct ip_set *set,
4575 + ip_set_ip_t ip, ip_set_ip_t port, ip_set_ip_t ip1)
4577 + struct ip_set_ipportnethash *map = set->data;
4579 + if (ip < map->first_ip || ip > map->last_ip)
4582 + return (ipportnethash_id(set, ip, port, ip1) != UINT_MAX);
4586 +ipportnethash_utest(struct ip_set *set, const void *data, u_int32_t size)
4588 + const struct ip_set_req_ipportnethash *req = data;
4590 + if (req->cidr <= 0 || req->cidr > 32)
4592 + return (req->cidr == 32
4593 + ? ipportnethash_test(set, req->ip, req->port, req->ip1)
4594 + : ipportnethash_test_cidr(set, req->ip, req->port,
4595 + req->ip1, req->cidr));
4598 +#define KADT_CONDITION \
4599 + ip_set_ip_t port, ip1; \
4601 + if (flags[2] == 0) \
4604 + port = get_port(skb, ++flags); \
4605 + ip1 = ipaddr(skb, ++flags); \
4607 + if (port == INVALID_PORT) \
4610 +KADT(ipportnethash, test, ipaddr, port, ip1)
4613 +__ipportnet_add(struct ip_set_ipportnethash *map,
4614 + ip_set_ip_t ip, ip_set_ip_t ip1)
4618 + struct ipportip *elem, *slot = NULL;
4620 + for (i = 0; i < map->probes; i++) {
4621 + probe = jhash_ip2(map, i, ip, ip1) % map->hashsize;
4622 + elem = HARRAY_ELEM(map->members, struct ipportip *, probe);
4623 + if (elem->ip == ip && elem->ip1 == ip1)
4625 + if (!(slot || elem->ip || elem->ip1))
4627 + /* There can be deleted entries, must check all slots */
4635 + /* Trigger rehashing */
4640 +__ipportnethash_add(struct ip_set_ipportnethash *map,
4641 + struct ipportip *elem)
4643 + return __ipportnet_add(map, elem->ip, elem->ip1);
4647 +ipportnethash_add(struct ip_set *set,
4648 + ip_set_ip_t ip, ip_set_ip_t port,
4649 + ip_set_ip_t ip1, uint8_t cidr)
4651 + struct ip_set_ipportnethash *map = set->data;
4655 + if (map->elements > limit)
4657 + if (ip < map->first_ip || ip > map->last_ip)
4659 + if (cidr <= 0 || cidr >= 32)
4661 + if (map->nets[cidr-1] == UINT16_MAX)
4664 + ip = pack_ip_port(map, ip, port);
4665 + ip1 = pack_ip_cidr(ip1, cidr);
4669 + ret =__ipportnet_add(map, ip, ip1);
4671 + if (!map->nets[cidr-1]++)
4672 + add_cidr_size(map->cidr, cidr);
4677 +#undef KADT_CONDITION
4678 +#define KADT_CONDITION \
4679 + struct ip_set_ipportnethash *map = set->data; \
4680 + uint8_t cidr = map->cidr[0] ? map->cidr[0] : 31; \
4681 + ip_set_ip_t port, ip1; \
4683 + if (flags[2] == 0) \
4686 + port = get_port(skb, flags++); \
4687 + ip1 = ipaddr(skb, flags++); \
4689 + if (port == INVALID_PORT) \
4692 +UADT(ipportnethash, add, req->port, req->ip1, req->cidr)
4693 +KADT(ipportnethash, add, ipaddr, port, ip1, cidr)
4696 +__ipportnethash_retry(struct ip_set_ipportnethash *tmp,
4697 + struct ip_set_ipportnethash *map)
4699 + tmp->first_ip = map->first_ip;
4700 + tmp->last_ip = map->last_ip;
4701 + memcpy(tmp->cidr, map->cidr, sizeof(tmp->cidr));
4702 + memcpy(tmp->nets, map->nets, sizeof(tmp->nets));
4705 +HASH_RETRY2(ipportnethash, struct ipportip)
4708 +ipportnethash_del(struct ip_set *set,
4709 + ip_set_ip_t ip, ip_set_ip_t port,
4710 + ip_set_ip_t ip1, uint8_t cidr)
4712 + struct ip_set_ipportnethash *map = set->data;
4714 + struct ipportip *elem;
4716 + if (ip < map->first_ip || ip > map->last_ip)
4720 + if (cidr <= 0 || cidr >= 32)
4723 + id = ipportnethash_id_cidr(set, ip, port, ip1, cidr);
4725 + if (id == UINT_MAX)
4728 + elem = HARRAY_ELEM(map->members, struct ipportip *, id);
4729 + elem->ip = elem->ip1 = 0;
4731 + if (!map->nets[cidr-1]--)
4732 + del_cidr_size(map->cidr, cidr);
4737 +UADT(ipportnethash, del, req->port, req->ip1, req->cidr)
4738 +KADT(ipportnethash, del, ipaddr, port, ip1, cidr)
4741 +__ipportnethash_create(const struct ip_set_req_ipportnethash_create *req,
4742 + struct ip_set_ipportnethash *map)
4744 + if (req->to - req->from > MAX_RANGE) {
4745 + ip_set_printk("range too big, %d elements (max %d)",
4746 + req->to - req->from + 1, MAX_RANGE+1);
4749 + map->first_ip = req->from;
4750 + map->last_ip = req->to;
4751 + memset(map->cidr, 0, sizeof(map->cidr));
4752 + memset(map->nets, 0, sizeof(map->nets));
4756 +HASH_CREATE(ipportnethash, struct ipportip)
4757 +HASH_DESTROY(ipportnethash)
4758 +HASH_FLUSH_CIDR(ipportnethash, struct ipportip);
4761 +__ipportnethash_list_header(const struct ip_set_ipportnethash *map,
4762 + struct ip_set_req_ipportnethash_create *header)
4764 + header->from = map->first_ip;
4765 + header->to = map->last_ip;
4768 +HASH_LIST_HEADER(ipportnethash)
4770 +HASH_LIST_MEMBERS_SIZE(ipportnethash, struct ipportip)
4771 +HASH_LIST_MEMBERS_MEMCPY(ipportnethash, struct ipportip,
4772 + (elem->ip || elem->ip1))
4774 +IP_SET_RTYPE(ipportnethash, IPSET_TYPE_IP | IPSET_TYPE_PORT
4775 + | IPSET_TYPE_IP1 | IPSET_DATA_TRIPLE)
4777 +MODULE_LICENSE("GPL");
4778 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4779 +MODULE_DESCRIPTION("ipportnethash type of IP sets");
4780 +module_param(limit, int, 0600);
4781 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
4783 +REGISTER_MODULE(ipportnethash)
4784 diff --git a/net/ipv4/netfilter/ip_set_iptree.c b/net/ipv4/netfilter/ip_set_iptree.c
4785 new file mode 100644
4786 index 0000000..77eb180
4788 +++ b/net/ipv4/netfilter/ip_set_iptree.c
4790 +/* Copyright (C) 2005-2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4792 + * This program is free software; you can redistribute it and/or modify
4793 + * it under the terms of the GNU General Public License version 2 as
4794 + * published by the Free Software Foundation.
4797 +/* Kernel module implementing an IP set type: the iptree type */
4799 +#include <linux/module.h>
4800 +#include <linux/moduleparam.h>
4801 +#include <linux/ip.h>
4802 +#include <linux/jiffies.h>
4803 +#include <linux/skbuff.h>
4804 +#include <linux/slab.h>
4805 +#include <linux/delay.h>
4806 +#include <linux/errno.h>
4807 +#include <asm/uaccess.h>
4808 +#include <asm/bitops.h>
4809 +#include <linux/spinlock.h>
4810 +#include <linux/timer.h>
4812 +#include <linux/netfilter_ipv4/ip_set.h>
4813 +#include <linux/netfilter_ipv4/ip_set_bitmaps.h>
4814 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
4816 +static int limit = MAX_RANGE;
4818 +/* Garbage collection interval in seconds: */
4819 +#define IPTREE_GC_TIME 5*60
4820 +/* Sleep so many milliseconds before trying again
4821 + * to delete the gc timer at destroying/flushing a set */
4822 +#define IPTREE_DESTROY_SLEEP 100
4824 +static __KMEM_CACHE_T__ *branch_cachep;
4825 +static __KMEM_CACHE_T__ *leaf_cachep;
4828 +#if defined(__LITTLE_ENDIAN)
4829 +#define ABCD(a,b,c,d,addrp) do { \
4830 + a = ((unsigned char *)addrp)[3]; \
4831 + b = ((unsigned char *)addrp)[2]; \
4832 + c = ((unsigned char *)addrp)[1]; \
4833 + d = ((unsigned char *)addrp)[0]; \
4835 +#elif defined(__BIG_ENDIAN)
4836 +#define ABCD(a,b,c,d,addrp) do { \
4837 + a = ((unsigned char *)addrp)[0]; \
4838 + b = ((unsigned char *)addrp)[1]; \
4839 + c = ((unsigned char *)addrp)[2]; \
4840 + d = ((unsigned char *)addrp)[3]; \
4843 +#error "Please fix asm/byteorder.h"
4844 +#endif /* __LITTLE_ENDIAN */
4846 +#define TESTIP_WALK(map, elem, branch) do { \
4847 + if ((map)->tree[elem]) { \
4848 + branch = (map)->tree[elem]; \
4854 +iptree_test(struct ip_set *set, ip_set_ip_t ip)
4856 + struct ip_set_iptree *map = set->data;
4857 + struct ip_set_iptreeb *btree;
4858 + struct ip_set_iptreec *ctree;
4859 + struct ip_set_iptreed *dtree;
4860 + unsigned char a,b,c,d;
4865 + ABCD(a, b, c, d, &ip);
4866 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
4867 + TESTIP_WALK(map, a, btree);
4868 + TESTIP_WALK(btree, b, ctree);
4869 + TESTIP_WALK(ctree, c, dtree);
4870 + DP("%lu %lu", dtree->expires[d], jiffies);
4871 + return dtree->expires[d]
4873 + || time_after(dtree->expires[d], jiffies));
4876 +#define KADT_CONDITION
4879 +KADT(iptree, test, ipaddr)
4881 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
4882 + if ((map)->tree[elem]) { \
4883 + DP("found %u", elem); \
4884 + branch = (map)->tree[elem]; \
4886 + branch = (type *) \
4887 + kmem_cache_alloc(cachep, GFP_ATOMIC); \
4888 + if (branch == NULL) \
4890 + memset(branch, 0, sizeof(*branch)); \
4891 + (map)->tree[elem] = branch; \
4892 + DP("alloc %u", elem); \
4897 +iptree_add(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout)
4899 + struct ip_set_iptree *map = set->data;
4900 + struct ip_set_iptreeb *btree;
4901 + struct ip_set_iptreec *ctree;
4902 + struct ip_set_iptreed *dtree;
4903 + unsigned char a,b,c,d;
4906 + if (!ip || map->elements >= limit)
4907 + /* We could call the garbage collector
4908 + * but it's probably overkill */
4911 + ABCD(a, b, c, d, &ip);
4912 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
4913 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
4914 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
4915 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
4916 + if (dtree->expires[d]
4917 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4919 + if (map->timeout && timeout == 0)
4920 + timeout = map->timeout;
4921 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
4922 + /* Lottery: I won! */
4923 + if (dtree->expires[d] == 0)
4924 + dtree->expires[d] = 1;
4925 + DP("%u %lu", d, dtree->expires[d]);
4931 +UADT(iptree, add, req->timeout)
4932 +KADT(iptree, add, ipaddr, 0)
4934 +#define DELIP_WALK(map, elem, branch) do { \
4935 + if ((map)->tree[elem]) { \
4936 + branch = (map)->tree[elem]; \
4942 +iptree_del(struct ip_set *set, ip_set_ip_t ip)
4944 + struct ip_set_iptree *map = set->data;
4945 + struct ip_set_iptreeb *btree;
4946 + struct ip_set_iptreec *ctree;
4947 + struct ip_set_iptreed *dtree;
4948 + unsigned char a,b,c,d;
4953 + ABCD(a, b, c, d, &ip);
4954 + DELIP_WALK(map, a, btree);
4955 + DELIP_WALK(btree, b, ctree);
4956 + DELIP_WALK(ctree, c, dtree);
4958 + if (dtree->expires[d]) {
4959 + dtree->expires[d] = 0;
4967 +KADT(iptree, del, ipaddr)
4969 +#define LOOP_WALK_BEGIN(map, i, branch) \
4970 + for (i = 0; i < 256; i++) { \
4971 + if (!(map)->tree[i]) \
4973 + branch = (map)->tree[i]
4975 +#define LOOP_WALK_END }
4978 +ip_tree_gc(unsigned long ul_set)
4980 + struct ip_set *set = (struct ip_set *) ul_set;
4981 + struct ip_set_iptree *map = set->data;
4982 + struct ip_set_iptreeb *btree;
4983 + struct ip_set_iptreec *ctree;
4984 + struct ip_set_iptreed *dtree;
4985 + unsigned int a,b,c,d;
4986 + unsigned char i,j,k;
4989 + DP("gc: %s", set->name);
4990 + write_lock_bh(&set->lock);
4991 + LOOP_WALK_BEGIN(map, a, btree);
4992 + LOOP_WALK_BEGIN(btree, b, ctree);
4993 + LOOP_WALK_BEGIN(ctree, c, dtree);
4994 + for (d = 0; d < 256; d++) {
4995 + if (dtree->expires[d]) {
4996 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4998 + dtree->expires[d], jiffies);
5000 + && time_before(dtree->expires[d], jiffies)) {
5001 + dtree->expires[d] = 0;
5008 + DP("gc: %s: leaf %u %u %u empty",
5009 + set->name, a, b, c);
5010 + kmem_cache_free(leaf_cachep, dtree);
5011 + ctree->tree[c] = NULL;
5013 + DP("gc: %s: leaf %u %u %u not empty",
5014 + set->name, a, b, c);
5020 + DP("gc: %s: branch %u %u empty",
5022 + kmem_cache_free(branch_cachep, ctree);
5023 + btree->tree[b] = NULL;
5025 + DP("gc: %s: branch %u %u not empty",
5032 + DP("gc: %s: branch %u empty",
5034 + kmem_cache_free(branch_cachep, btree);
5035 + map->tree[a] = NULL;
5037 + DP("gc: %s: branch %u not empty",
5042 + write_unlock_bh(&set->lock);
5044 + map->gc.expires = jiffies + map->gc_interval * HZ;
5045 + add_timer(&map->gc);
5049 +init_gc_timer(struct ip_set *set)
5051 + struct ip_set_iptree *map = set->data;
5053 + /* Even if there is no timeout for the entries,
5054 + * we still have to call gc because delete
5055 + * do not clean up empty branches */
5056 + map->gc_interval = IPTREE_GC_TIME;
5057 + init_timer(&map->gc);
5058 + map->gc.data = (unsigned long) set;
5059 + map->gc.function = ip_tree_gc;
5060 + map->gc.expires = jiffies + map->gc_interval * HZ;
5061 + add_timer(&map->gc);
5065 +iptree_create(struct ip_set *set, const void *data, u_int32_t size)
5067 + const struct ip_set_req_iptree_create *req = data;
5068 + struct ip_set_iptree *map;
5070 + if (size != sizeof(struct ip_set_req_iptree_create)) {
5071 + ip_set_printk("data length wrong (want %zu, have %lu)",
5072 + sizeof(struct ip_set_req_iptree_create),
5073 + (unsigned long)size);
5077 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
5079 + DP("out of memory for %zu bytes",
5080 + sizeof(struct ip_set_iptree));
5083 + memset(map, 0, sizeof(*map));
5084 + map->timeout = req->timeout;
5085 + map->elements = 0;
5088 + init_gc_timer(set);
5094 +__flush(struct ip_set_iptree *map)
5096 + struct ip_set_iptreeb *btree;
5097 + struct ip_set_iptreec *ctree;
5098 + struct ip_set_iptreed *dtree;
5099 + unsigned int a,b,c;
5101 + LOOP_WALK_BEGIN(map, a, btree);
5102 + LOOP_WALK_BEGIN(btree, b, ctree);
5103 + LOOP_WALK_BEGIN(ctree, c, dtree);
5104 + kmem_cache_free(leaf_cachep, dtree);
5106 + kmem_cache_free(branch_cachep, ctree);
5108 + kmem_cache_free(branch_cachep, btree);
5110 + map->elements = 0;
5114 +iptree_destroy(struct ip_set *set)
5116 + struct ip_set_iptree *map = set->data;
5118 + /* gc might be running */
5119 + while (!del_timer(&map->gc))
5120 + msleep(IPTREE_DESTROY_SLEEP);
5127 +iptree_flush(struct ip_set *set)
5129 + struct ip_set_iptree *map = set->data;
5130 + unsigned int timeout = map->timeout;
5132 + /* gc might be running */
5133 + while (!del_timer(&map->gc))
5134 + msleep(IPTREE_DESTROY_SLEEP);
5136 + memset(map, 0, sizeof(*map));
5137 + map->timeout = timeout;
5139 + init_gc_timer(set);
5143 +iptree_list_header(const struct ip_set *set, void *data)
5145 + const struct ip_set_iptree *map = set->data;
5146 + struct ip_set_req_iptree_create *header = data;
5148 + header->timeout = map->timeout;
5152 +iptree_list_members_size(const struct ip_set *set, char dont_align)
5154 + const struct ip_set_iptree *map = set->data;
5155 + struct ip_set_iptreeb *btree;
5156 + struct ip_set_iptreec *ctree;
5157 + struct ip_set_iptreed *dtree;
5158 + unsigned int a,b,c,d;
5159 + unsigned int count = 0;
5161 + LOOP_WALK_BEGIN(map, a, btree);
5162 + LOOP_WALK_BEGIN(btree, b, ctree);
5163 + LOOP_WALK_BEGIN(ctree, c, dtree);
5164 + for (d = 0; d < 256; d++) {
5165 + if (dtree->expires[d]
5166 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
5173 + DP("members %u", count);
5174 + return (count * IPSET_VALIGN(sizeof(struct ip_set_req_iptree), dont_align));
5178 +iptree_list_members(const struct ip_set *set, void *data, char dont_align)
5180 + const struct ip_set_iptree *map = set->data;
5181 + struct ip_set_iptreeb *btree;
5182 + struct ip_set_iptreec *ctree;
5183 + struct ip_set_iptreed *dtree;
5184 + unsigned int a,b,c,d;
5185 + size_t offset = 0, datasize;
5186 + struct ip_set_req_iptree *entry;
5188 + datasize = IPSET_VALIGN(sizeof(struct ip_set_req_iptree), dont_align);
5189 + LOOP_WALK_BEGIN(map, a, btree);
5190 + LOOP_WALK_BEGIN(btree, b, ctree);
5191 + LOOP_WALK_BEGIN(ctree, c, dtree);
5192 + for (d = 0; d < 256; d++) {
5193 + if (dtree->expires[d]
5194 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
5195 + entry = data + offset;
5196 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
5197 + entry->timeout = !map->timeout ? 0
5198 + : (dtree->expires[d] - jiffies)/HZ;
5199 + offset += datasize;
5207 +IP_SET_TYPE(iptree, IPSET_TYPE_IP | IPSET_DATA_SINGLE)
5209 +MODULE_LICENSE("GPL");
5210 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5211 +MODULE_DESCRIPTION("iptree type of IP sets");
5212 +module_param(limit, int, 0600);
5213 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
5215 +static int __init ip_set_iptree_init(void)
5219 + branch_cachep = KMEM_CACHE_CREATE("ip_set_iptreeb",
5220 + sizeof(struct ip_set_iptreeb));
5221 + if (!branch_cachep) {
5222 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
5226 + leaf_cachep = KMEM_CACHE_CREATE("ip_set_iptreed",
5227 + sizeof(struct ip_set_iptreed));
5228 + if (!leaf_cachep) {
5229 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
5233 + ret = ip_set_register_set_type(&ip_set_iptree);
5237 + kmem_cache_destroy(leaf_cachep);
5239 + kmem_cache_destroy(branch_cachep);
5244 +static void __exit ip_set_iptree_fini(void)
5246 + /* FIXME: possible race with ip_set_create() */
5247 + ip_set_unregister_set_type(&ip_set_iptree);
5248 + kmem_cache_destroy(leaf_cachep);
5249 + kmem_cache_destroy(branch_cachep);
5252 +module_init(ip_set_iptree_init);
5253 +module_exit(ip_set_iptree_fini);
5254 diff --git a/net/ipv4/netfilter/ip_set_iptreemap.c b/net/ipv4/netfilter/ip_set_iptreemap.c
5255 new file mode 100644
5256 index 0000000..76d0142
5258 +++ b/net/ipv4/netfilter/ip_set_iptreemap.c
5260 +/* Copyright (C) 2007 Sven Wegener <sven.wegener@stealer.net>
5262 + * This program is free software; you can redistribute it and/or modify it
5263 + * under the terms of the GNU General Public License version 2 as published by
5264 + * the Free Software Foundation.
5267 +/* This modules implements the iptreemap ipset type. It uses bitmaps to
5268 + * represent every single IPv4 address as a bit. The bitmaps are managed in a
5269 + * tree structure, where the first three octets of an address are used as an
5270 + * index to find the bitmap and the last octet is used as the bit number.
5273 +#include <linux/kernel.h>
5274 +#include <linux/module.h>
5275 +#include <linux/ip.h>
5276 +#include <linux/jiffies.h>
5277 +#include <linux/skbuff.h>
5278 +#include <linux/slab.h>
5279 +#include <linux/delay.h>
5280 +#include <linux/errno.h>
5281 +#include <asm/uaccess.h>
5282 +#include <asm/bitops.h>
5283 +#include <linux/spinlock.h>
5284 +#include <linux/timer.h>
5286 +#include <linux/netfilter_ipv4/ip_set.h>
5287 +#include <linux/netfilter_ipv4/ip_set_bitmaps.h>
5288 +#include <linux/netfilter_ipv4/ip_set_iptreemap.h>
5290 +#define IPTREEMAP_DEFAULT_GC_TIME (5 * 60)
5291 +#define IPTREEMAP_DESTROY_SLEEP (100)
5293 +static __KMEM_CACHE_T__ *cachep_b;
5294 +static __KMEM_CACHE_T__ *cachep_c;
5295 +static __KMEM_CACHE_T__ *cachep_d;
5297 +static struct ip_set_iptreemap_d *fullbitmap_d;
5298 +static struct ip_set_iptreemap_c *fullbitmap_c;
5299 +static struct ip_set_iptreemap_b *fullbitmap_b;
5301 +#if defined(__LITTLE_ENDIAN)
5302 +#define ABCD(a, b, c, d, addr) \
5304 + a = ((unsigned char *)addr)[3]; \
5305 + b = ((unsigned char *)addr)[2]; \
5306 + c = ((unsigned char *)addr)[1]; \
5307 + d = ((unsigned char *)addr)[0]; \
5309 +#elif defined(__BIG_ENDIAN)
5310 +#define ABCD(a,b,c,d,addrp) do { \
5311 + a = ((unsigned char *)addrp)[0]; \
5312 + b = ((unsigned char *)addrp)[1]; \
5313 + c = ((unsigned char *)addrp)[2]; \
5314 + d = ((unsigned char *)addrp)[3]; \
5317 +#error "Please fix asm/byteorder.h"
5318 +#endif /* __LITTLE_ENDIAN */
5320 +#define TESTIP_WALK(map, elem, branch, full) \
5322 + branch = (map)->tree[elem]; \
5325 + else if (branch == full) \
5329 +#define ADDIP_WALK(map, elem, branch, type, cachep, full) \
5331 + branch = (map)->tree[elem]; \
5333 + branch = (type *) kmem_cache_alloc(cachep, GFP_ATOMIC); \
5336 + memset(branch, 0, sizeof(*branch)); \
5337 + (map)->tree[elem] = branch; \
5338 + } else if (branch == full) { \
5343 +#define ADDIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free) \
5344 + for (a = a1; a <= a2; a++) { \
5345 + branch = (map)->tree[a]; \
5346 + if (branch != full) { \
5347 + if ((a > a1 && a < a2) || (hint)) { \
5350 + (map)->tree[a] = full; \
5352 + } else if (!branch) { \
5353 + branch = kmem_cache_alloc(cachep, GFP_ATOMIC); \
5356 + memset(branch, 0, sizeof(*branch)); \
5357 + (map)->tree[a] = branch; \
5360 +#define ADDIP_RANGE_LOOP_END() \
5364 +#define DELIP_WALK(map, elem, branch, cachep, full) \
5366 + branch = (map)->tree[elem]; \
5369 + } else if (branch == full) { \
5370 + branch = kmem_cache_alloc(cachep, GFP_ATOMIC); \
5373 + memcpy(branch, full, sizeof(*full)); \
5374 + (map)->tree[elem] = branch; \
5378 +#define DELIP_RANGE_LOOP(map, a, a1, a2, hint, branch, full, cachep, free) \
5379 + for (a = a1; a <= a2; a++) { \
5380 + branch = (map)->tree[a]; \
5382 + if ((a > a1 && a < a2) || (hint)) { \
5383 + if (branch != full) \
5385 + (map)->tree[a] = NULL; \
5387 + } else if (branch == full) { \
5388 + branch = kmem_cache_alloc(cachep, GFP_ATOMIC); \
5391 + memcpy(branch, full, sizeof(*branch)); \
5392 + (map)->tree[a] = branch; \
5395 +#define DELIP_RANGE_LOOP_END() \
5399 +#define LOOP_WALK_BEGIN(map, i, branch) \
5400 + for (i = 0; i < 256; i++) { \
5401 + branch = (map)->tree[i]; \
5402 + if (likely(!branch)) \
5405 +#define LOOP_WALK_END() \
5408 +#define LOOP_WALK_BEGIN_GC(map, i, branch, full, cachep, count) \
5410 + for (i = 0; i < 256; i++) { \
5411 + branch = (map)->tree[i]; \
5412 + if (likely(!branch)) \
5415 + if (branch == full) { \
5420 +#define LOOP_WALK_END_GC(map, i, branch, full, cachep, count) \
5421 + if (-256 == count) { \
5422 + kmem_cache_free(cachep, branch); \
5423 + (map)->tree[i] = NULL; \
5424 + } else if (256 == count) { \
5425 + kmem_cache_free(cachep, branch); \
5426 + (map)->tree[i] = full; \
5430 +#define LOOP_WALK_BEGIN_COUNT(map, i, branch, inrange, count) \
5431 + for (i = 0; i < 256; i++) { \
5432 + if (!(map)->tree[i]) { \
5439 + branch = (map)->tree[i];
5441 +#define LOOP_WALK_END_COUNT() \
5444 +#define GETVALUE1(a, a1, b1, r) \
5445 + (a == a1 ? b1 : r)
5447 +#define GETVALUE2(a, b, a1, b1, c1, r) \
5448 + (a == a1 && b == b1 ? c1 : r)
5450 +#define GETVALUE3(a, b, c, a1, b1, c1, d1, r) \
5451 + (a == a1 && b == b1 && c == c1 ? d1 : r)
5453 +#define CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2) \
5455 + GETVALUE1(a, a1, b1, 0) == 0 \
5456 + && GETVALUE1(a, a2, b2, 255) == 255 \
5463 +#define CHECK2(a, b, a1, a2, b1, b2, c1, c2, d1, d2) \
5465 + GETVALUE2(a, b, a1, b1, c1, 0) == 0 \
5466 + && GETVALUE2(a, b, a2, b2, c2, 255) == 255 \
5471 +#define CHECK3(a, b, c, a1, a2, b1, b2, c1, c2, d1, d2) \
5473 + GETVALUE3(a, b, c, a1, b1, c1, d1, 0) == 0 \
5474 + && GETVALUE3(a, b, c, a2, b2, c2, d2, 255) == 255 \
5479 +free_d(struct ip_set_iptreemap_d *map)
5481 + kmem_cache_free(cachep_d, map);
5485 +free_c(struct ip_set_iptreemap_c *map)
5487 + struct ip_set_iptreemap_d *dtree;
5490 + LOOP_WALK_BEGIN(map, i, dtree) {
5491 + if (dtree != fullbitmap_d)
5493 + } LOOP_WALK_END();
5495 + kmem_cache_free(cachep_c, map);
5499 +free_b(struct ip_set_iptreemap_b *map)
5501 + struct ip_set_iptreemap_c *ctree;
5504 + LOOP_WALK_BEGIN(map, i, ctree) {
5505 + if (ctree != fullbitmap_c)
5507 + } LOOP_WALK_END();
5509 + kmem_cache_free(cachep_b, map);
5513 +iptreemap_test(struct ip_set *set, ip_set_ip_t ip)
5515 + struct ip_set_iptreemap *map = set->data;
5516 + struct ip_set_iptreemap_b *btree;
5517 + struct ip_set_iptreemap_c *ctree;
5518 + struct ip_set_iptreemap_d *dtree;
5519 + unsigned char a, b, c, d;
5521 + ABCD(a, b, c, d, &ip);
5523 + TESTIP_WALK(map, a, btree, fullbitmap_b);
5524 + TESTIP_WALK(btree, b, ctree, fullbitmap_c);
5525 + TESTIP_WALK(ctree, c, dtree, fullbitmap_d);
5527 + return !!test_bit(d, (void *) dtree->bitmap);
5530 +#define KADT_CONDITION
5532 +UADT(iptreemap, test)
5533 +KADT(iptreemap, test, ipaddr)
5536 +__addip_single(struct ip_set *set, ip_set_ip_t ip)
5538 + struct ip_set_iptreemap *map = (struct ip_set_iptreemap *) set->data;
5539 + struct ip_set_iptreemap_b *btree;
5540 + struct ip_set_iptreemap_c *ctree;
5541 + struct ip_set_iptreemap_d *dtree;
5542 + unsigned char a, b, c, d;
5544 + ABCD(a, b, c, d, &ip);
5546 + ADDIP_WALK(map, a, btree, struct ip_set_iptreemap_b, cachep_b, fullbitmap_b);
5547 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreemap_c, cachep_c, fullbitmap_c);
5548 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreemap_d, cachep_d, fullbitmap_d);
5550 + if (__test_and_set_bit(d, (void *) dtree->bitmap))
5553 + __set_bit(b, (void *) btree->dirty);
5559 +iptreemap_add(struct ip_set *set, ip_set_ip_t start, ip_set_ip_t end)
5561 + struct ip_set_iptreemap *map = set->data;
5562 + struct ip_set_iptreemap_b *btree;
5563 + struct ip_set_iptreemap_c *ctree;
5564 + struct ip_set_iptreemap_d *dtree;
5565 + unsigned int a, b, c, d;
5566 + unsigned char a1, b1, c1, d1;
5567 + unsigned char a2, b2, c2, d2;
5570 + return __addip_single(set, start);
5572 + ABCD(a1, b1, c1, d1, &start);
5573 + ABCD(a2, b2, c2, d2, &end);
5575 + /* This is sooo ugly... */
5576 + ADDIP_RANGE_LOOP(map, a, a1, a2, CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2), btree, fullbitmap_b, cachep_b, free_b) {
5577 + 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) {
5578 + 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) {
5579 + for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5580 + __set_bit(d, (void *) dtree->bitmap);
5581 + __set_bit(b, (void *) btree->dirty);
5582 + } ADDIP_RANGE_LOOP_END();
5583 + } ADDIP_RANGE_LOOP_END();
5584 + } ADDIP_RANGE_LOOP_END();
5589 +UADT0(iptreemap, add, min(req->ip, req->end), max(req->ip, req->end))
5590 +KADT(iptreemap, add, ipaddr, ip)
5593 +__delip_single(struct ip_set *set, ip_set_ip_t ip)
5595 + struct ip_set_iptreemap *map = set->data;
5596 + struct ip_set_iptreemap_b *btree;
5597 + struct ip_set_iptreemap_c *ctree;
5598 + struct ip_set_iptreemap_d *dtree;
5599 + unsigned char a,b,c,d;
5601 + ABCD(a, b, c, d, &ip);
5603 + DELIP_WALK(map, a, btree, cachep_b, fullbitmap_b);
5604 + DELIP_WALK(btree, b, ctree, cachep_c, fullbitmap_c);
5605 + DELIP_WALK(ctree, c, dtree, cachep_d, fullbitmap_d);
5607 + if (!__test_and_clear_bit(d, (void *) dtree->bitmap))
5610 + __set_bit(b, (void *) btree->dirty);
5616 +iptreemap_del(struct ip_set *set, ip_set_ip_t start, ip_set_ip_t end)
5618 + struct ip_set_iptreemap *map = set->data;
5619 + struct ip_set_iptreemap_b *btree;
5620 + struct ip_set_iptreemap_c *ctree;
5621 + struct ip_set_iptreemap_d *dtree;
5622 + unsigned int a, b, c, d;
5623 + unsigned char a1, b1, c1, d1;
5624 + unsigned char a2, b2, c2, d2;
5627 + return __delip_single(set, start);
5629 + ABCD(a1, b1, c1, d1, &start);
5630 + ABCD(a2, b2, c2, d2, &end);
5632 + /* This is sooo ugly... */
5633 + DELIP_RANGE_LOOP(map, a, a1, a2, CHECK1(a, a1, a2, b1, b2, c1, c2, d1, d2), btree, fullbitmap_b, cachep_b, free_b) {
5634 + 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) {
5635 + 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) {
5636 + for (d = GETVALUE3(a, b, c, a1, b1, c1, d1, 0); d <= GETVALUE3(a, b, c, a2, b2, c2, d2, 255); d++)
5637 + __clear_bit(d, (void *) dtree->bitmap);
5638 + __set_bit(b, (void *) btree->dirty);
5639 + } DELIP_RANGE_LOOP_END();
5640 + } DELIP_RANGE_LOOP_END();
5641 + } DELIP_RANGE_LOOP_END();
5646 +UADT0(iptreemap, del, min(req->ip, req->end), max(req->ip, req->end))
5647 +KADT(iptreemap, del, ipaddr, ip)
5649 +/* Check the status of the bitmap
5650 + * -1 == all bits cleared
5651 + * 1 == all bits set
5652 + * 0 == anything else
5655 +bitmap_status(struct ip_set_iptreemap_d *dtree)
5657 + unsigned char first = dtree->bitmap[0];
5660 + for (a = 1; a < 32; a++)
5661 + if (dtree->bitmap[a] != first)
5664 + return (first == 0 ? -1 : (first == 255 ? 1 : 0));
5668 +gc(unsigned long addr)
5670 + struct ip_set *set = (struct ip_set *) addr;
5671 + struct ip_set_iptreemap *map = set->data;
5672 + struct ip_set_iptreemap_b *btree;
5673 + struct ip_set_iptreemap_c *ctree;
5674 + struct ip_set_iptreemap_d *dtree;
5675 + unsigned int a, b, c;
5678 + write_lock_bh(&set->lock);
5680 + LOOP_WALK_BEGIN_GC(map, a, btree, fullbitmap_b, cachep_b, i) {
5681 + LOOP_WALK_BEGIN_GC(btree, b, ctree, fullbitmap_c, cachep_c, j) {
5682 + if (!__test_and_clear_bit(b, (void *) btree->dirty))
5684 + LOOP_WALK_BEGIN_GC(ctree, c, dtree, fullbitmap_d, cachep_d, k) {
5685 + switch (bitmap_status(dtree)) {
5687 + kmem_cache_free(cachep_d, dtree);
5688 + ctree->tree[c] = NULL;
5692 + kmem_cache_free(cachep_d, dtree);
5693 + ctree->tree[c] = fullbitmap_d;
5697 + } LOOP_WALK_END();
5698 + } LOOP_WALK_END_GC(btree, b, ctree, fullbitmap_c, cachep_c, k);
5699 + } LOOP_WALK_END_GC(map, a, btree, fullbitmap_b, cachep_b, j);
5701 + write_unlock_bh(&set->lock);
5703 + map->gc.expires = jiffies + map->gc_interval * HZ;
5704 + add_timer(&map->gc);
5708 +init_gc_timer(struct ip_set *set)
5710 + struct ip_set_iptreemap *map = set->data;
5712 + init_timer(&map->gc);
5713 + map->gc.data = (unsigned long) set;
5714 + map->gc.function = gc;
5715 + map->gc.expires = jiffies + map->gc_interval * HZ;
5716 + add_timer(&map->gc);
5720 +iptreemap_create(struct ip_set *set, const void *data, u_int32_t size)
5722 + const struct ip_set_req_iptreemap_create *req = data;
5723 + struct ip_set_iptreemap *map;
5725 + map = kzalloc(sizeof(*map), GFP_KERNEL);
5729 + map->gc_interval = req->gc_interval ? req->gc_interval : IPTREEMAP_DEFAULT_GC_TIME;
5732 + init_gc_timer(set);
5738 +__flush(struct ip_set_iptreemap *map)
5740 + struct ip_set_iptreemap_b *btree;
5743 + LOOP_WALK_BEGIN(map, a, btree);
5744 + if (btree != fullbitmap_b)
5750 +iptreemap_destroy(struct ip_set *set)
5752 + struct ip_set_iptreemap *map = set->data;
5754 + while (!del_timer(&map->gc))
5755 + msleep(IPTREEMAP_DESTROY_SLEEP);
5764 +iptreemap_flush(struct ip_set *set)
5766 + struct ip_set_iptreemap *map = set->data;
5767 + unsigned int gc_interval = map->gc_interval;
5769 + while (!del_timer(&map->gc))
5770 + msleep(IPTREEMAP_DESTROY_SLEEP);
5774 + memset(map, 0, sizeof(*map));
5775 + map->gc_interval = gc_interval;
5777 + init_gc_timer(set);
5781 +iptreemap_list_header(const struct ip_set *set, void *data)
5783 + struct ip_set_iptreemap *map = set->data;
5784 + struct ip_set_req_iptreemap_create *header = data;
5786 + header->gc_interval = map->gc_interval;
5790 +iptreemap_list_members_size(const struct ip_set *set, char dont_align)
5792 + struct ip_set_iptreemap *map = set->data;
5793 + struct ip_set_iptreemap_b *btree;
5794 + struct ip_set_iptreemap_c *ctree;
5795 + struct ip_set_iptreemap_d *dtree;
5796 + unsigned int a, b, c, d, inrange = 0, count = 0;
5798 + LOOP_WALK_BEGIN_COUNT(map, a, btree, inrange, count) {
5799 + LOOP_WALK_BEGIN_COUNT(btree, b, ctree, inrange, count) {
5800 + LOOP_WALK_BEGIN_COUNT(ctree, c, dtree, inrange, count) {
5801 + for (d = 0; d < 256; d++) {
5802 + if (test_bit(d, (void *) dtree->bitmap)) {
5804 + } else if (inrange) {
5809 + } LOOP_WALK_END_COUNT();
5810 + } LOOP_WALK_END_COUNT();
5811 + } LOOP_WALK_END_COUNT();
5816 + return (count * IPSET_VALIGN(sizeof(struct ip_set_req_iptreemap), dont_align));
5820 +add_member(void *data, size_t offset, ip_set_ip_t start, ip_set_ip_t end)
5822 + struct ip_set_req_iptreemap *entry = data + offset;
5824 + entry->ip = start;
5829 +iptreemap_list_members(const struct ip_set *set, void *data, char dont_align)
5831 + struct ip_set_iptreemap *map = set->data;
5832 + struct ip_set_iptreemap_b *btree;
5833 + struct ip_set_iptreemap_c *ctree;
5834 + struct ip_set_iptreemap_d *dtree;
5835 + unsigned int a, b, c, d, inrange = 0;
5836 + size_t offset = 0, datasize;
5837 + ip_set_ip_t start = 0, end = 0, ip;
5839 + datasize = IPSET_VALIGN(sizeof(struct ip_set_req_iptreemap), dont_align);
5840 + LOOP_WALK_BEGIN(map, a, btree) {
5841 + LOOP_WALK_BEGIN(btree, b, ctree) {
5842 + LOOP_WALK_BEGIN(ctree, c, dtree) {
5843 + for (d = 0; d < 256; d++) {
5844 + if (test_bit(d, (void *) dtree->bitmap)) {
5845 + ip = ((a << 24) | (b << 16) | (c << 8) | d);
5849 + } else if (end < ip - 1) {
5850 + add_member(data, offset, start, end);
5851 + offset += datasize;
5855 + } else if (inrange) {
5856 + add_member(data, offset, start, end);
5857 + offset += datasize;
5861 + } LOOP_WALK_END();
5862 + } LOOP_WALK_END();
5863 + } LOOP_WALK_END();
5866 + add_member(data, offset, start, end);
5869 +IP_SET_TYPE(iptreemap, IPSET_TYPE_IP | IPSET_DATA_SINGLE)
5871 +MODULE_LICENSE("GPL");
5872 +MODULE_AUTHOR("Sven Wegener <sven.wegener@stealer.net>");
5873 +MODULE_DESCRIPTION("iptreemap type of IP sets");
5875 +static int __init ip_set_iptreemap_init(void)
5877 + int ret = -ENOMEM;
5880 + cachep_b = KMEM_CACHE_CREATE("ip_set_iptreemap_b",
5881 + sizeof(struct ip_set_iptreemap_b));
5883 + ip_set_printk("Unable to create ip_set_iptreemap_b slab cache");
5887 + cachep_c = KMEM_CACHE_CREATE("ip_set_iptreemap_c",
5888 + sizeof(struct ip_set_iptreemap_c));
5890 + ip_set_printk("Unable to create ip_set_iptreemap_c slab cache");
5894 + cachep_d = KMEM_CACHE_CREATE("ip_set_iptreemap_d",
5895 + sizeof(struct ip_set_iptreemap_d));
5897 + ip_set_printk("Unable to create ip_set_iptreemap_d slab cache");
5901 + fullbitmap_d = kmem_cache_alloc(cachep_d, GFP_KERNEL);
5902 + if (!fullbitmap_d)
5905 + fullbitmap_c = kmem_cache_alloc(cachep_c, GFP_KERNEL);
5906 + if (!fullbitmap_c)
5909 + fullbitmap_b = kmem_cache_alloc(cachep_b, GFP_KERNEL);
5910 + if (!fullbitmap_b)
5913 + ret = ip_set_register_set_type(&ip_set_iptreemap);
5917 + /* Now init our global bitmaps */
5918 + memset(fullbitmap_d->bitmap, 0xff, sizeof(fullbitmap_d->bitmap));
5920 + for (a = 0; a < 256; a++)
5921 + fullbitmap_c->tree[a] = fullbitmap_d;
5923 + for (a = 0; a < 256; a++)
5924 + fullbitmap_b->tree[a] = fullbitmap_c;
5925 + memset(fullbitmap_b->dirty, 0, sizeof(fullbitmap_b->dirty));
5930 + kmem_cache_free(cachep_b, fullbitmap_b);
5932 + kmem_cache_free(cachep_c, fullbitmap_c);
5934 + kmem_cache_free(cachep_d, fullbitmap_d);
5936 + kmem_cache_destroy(cachep_d);
5938 + kmem_cache_destroy(cachep_c);
5940 + kmem_cache_destroy(cachep_b);
5946 +static void __exit ip_set_iptreemap_fini(void)
5948 + ip_set_unregister_set_type(&ip_set_iptreemap);
5949 + kmem_cache_free(cachep_d, fullbitmap_d);
5950 + kmem_cache_free(cachep_c, fullbitmap_c);
5951 + kmem_cache_free(cachep_b, fullbitmap_b);
5952 + kmem_cache_destroy(cachep_d);
5953 + kmem_cache_destroy(cachep_c);
5954 + kmem_cache_destroy(cachep_b);
5957 +module_init(ip_set_iptreemap_init);
5958 +module_exit(ip_set_iptreemap_fini);
5959 diff --git a/net/ipv4/netfilter/ip_set_macipmap.c b/net/ipv4/netfilter/ip_set_macipmap.c
5960 new file mode 100644
5961 index 0000000..89e907b
5963 +++ b/net/ipv4/netfilter/ip_set_macipmap.c
5965 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5966 + * Patrick Schaaf <bof@bof.de>
5967 + * Martin Josefsson <gandalf@wlug.westbo.se>
5968 + * Copyright (C) 2003-2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5970 + * This program is free software; you can redistribute it and/or modify
5971 + * it under the terms of the GNU General Public License version 2 as
5972 + * published by the Free Software Foundation.
5975 +/* Kernel module implementing an IP set type: the macipmap type */
5977 +#include <linux/module.h>
5978 +#include <linux/ip.h>
5979 +#include <linux/skbuff.h>
5980 +#include <linux/errno.h>
5981 +#include <asm/uaccess.h>
5982 +#include <asm/bitops.h>
5983 +#include <linux/spinlock.h>
5984 +#include <linux/if_ether.h>
5986 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
5989 +macipmap_utest(struct ip_set *set, const void *data, u_int32_t size)
5991 + const struct ip_set_macipmap *map = set->data;
5992 + const struct ip_set_macip *table = map->members;
5993 + const struct ip_set_req_macipmap *req = data;
5995 + if (req->ip < map->first_ip || req->ip > map->last_ip)
5998 + DP("set: %s, ip:%u.%u.%u.%u", set->name, HIPQUAD(req->ip));
5999 + if (table[req->ip - map->first_ip].match) {
6000 + return (memcmp(req->ethernet,
6001 + &table[req->ip - map->first_ip].ethernet,
6004 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6009 +macipmap_ktest(struct ip_set *set,
6010 + const struct sk_buff *skb,
6011 + const u_int32_t *flags)
6013 + const struct ip_set_macipmap *map = set->data;
6014 + const struct ip_set_macip *table = map->members;
6017 + ip = ipaddr(skb, flags);
6019 + if (ip < map->first_ip || ip > map->last_ip)
6022 + DP("set: %s, ip:%u.%u.%u.%u", set->name, HIPQUAD(ip));
6023 + if (table[ip - map->first_ip].match) {
6024 + /* Is mac pointer valid?
6025 + * If so, compare... */
6026 + return (skb_mac_header(skb) >= skb->head
6027 + && (skb_mac_header(skb) + ETH_HLEN) <= skb->data
6028 + && (memcmp(eth_hdr(skb)->h_source,
6029 + &table[ip - map->first_ip].ethernet,
6032 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
6036 +/* returns 0 on success */
6038 +macipmap_add(struct ip_set *set,
6039 + ip_set_ip_t ip, const unsigned char *ethernet)
6041 + struct ip_set_macipmap *map = set->data;
6042 + struct ip_set_macip *table = map->members;
6044 + if (ip < map->first_ip || ip > map->last_ip)
6046 + if (table[ip - map->first_ip].match)
6049 + DP("set: %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
6050 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
6051 + table[ip - map->first_ip].match = IPSET_MACIP_ISSET;
6055 +#define KADT_CONDITION \
6056 + if (!(skb_mac_header(skb) >= skb->head \
6057 + && (skb_mac_header(skb) + ETH_HLEN) <= skb->data))\
6060 +UADT(macipmap, add, req->ethernet)
6061 +KADT(macipmap, add, ipaddr, eth_hdr(skb)->h_source)
6064 +macipmap_del(struct ip_set *set, ip_set_ip_t ip)
6066 + struct ip_set_macipmap *map = set->data;
6067 + struct ip_set_macip *table = map->members;
6069 + if (ip < map->first_ip || ip > map->last_ip)
6071 + if (!table[ip - map->first_ip].match)
6074 + table[ip - map->first_ip].match = 0;
6075 + DP("set: %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
6079 +#undef KADT_CONDITION
6080 +#define KADT_CONDITION
6082 +UADT(macipmap, del)
6083 +KADT(macipmap, del, ipaddr)
6086 +__macipmap_create(const struct ip_set_req_macipmap_create *req,
6087 + struct ip_set_macipmap *map)
6089 + if (req->to - req->from > MAX_RANGE) {
6090 + ip_set_printk("range too big, %d elements (max %d)",
6091 + req->to - req->from + 1, MAX_RANGE+1);
6094 + map->flags = req->flags;
6095 + return (req->to - req->from + 1) * sizeof(struct ip_set_macip);
6098 +BITMAP_CREATE(macipmap)
6099 +BITMAP_DESTROY(macipmap)
6100 +BITMAP_FLUSH(macipmap)
6103 +__macipmap_list_header(const struct ip_set_macipmap *map,
6104 + struct ip_set_req_macipmap_create *header)
6106 + header->flags = map->flags;
6109 +BITMAP_LIST_HEADER(macipmap)
6110 +BITMAP_LIST_MEMBERS_SIZE(macipmap, struct ip_set_req_macipmap,
6111 + (map->last_ip - map->first_ip + 1),
6112 + ((const struct ip_set_macip *)map->members)[i].match)
6116 +macipmap_list_members(const struct ip_set *set, void *data, char dont_align)
6118 + const struct ip_set_macipmap *map = set->data;
6119 + const struct ip_set_macip *table = map->members;
6120 + uint32_t i, n = 0;
6121 + struct ip_set_req_macipmap *d;
6124 + memcpy(data, map->members, map->size);
6128 + for (i = 0; i < map->last_ip - map->first_ip + 1; i++)
6129 + if (table[i].match) {
6130 + d = data + n * IPSET_ALIGN(sizeof(struct ip_set_req_macipmap));
6131 + d->ip = map->first_ip + i;
6132 + memcpy(d->ethernet, &table[i].ethernet, ETH_ALEN);
6137 +IP_SET_TYPE(macipmap, IPSET_TYPE_IP | IPSET_DATA_SINGLE)
6139 +MODULE_LICENSE("GPL");
6140 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6141 +MODULE_DESCRIPTION("macipmap type of IP sets");
6143 +REGISTER_MODULE(macipmap)
6144 diff --git a/net/ipv4/netfilter/ip_set_nethash.c b/net/ipv4/netfilter/ip_set_nethash.c
6145 new file mode 100644
6146 index 0000000..e3b09e0
6148 +++ b/net/ipv4/netfilter/ip_set_nethash.c
6150 +/* Copyright (C) 2003-2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6152 + * This program is free software; you can redistribute it and/or modify
6153 + * it under the terms of the GNU General Public License version 2 as
6154 + * published by the Free Software Foundation.
6157 +/* Kernel module implementing a cidr nethash set */
6159 +#include <linux/module.h>
6160 +#include <linux/moduleparam.h>
6161 +#include <linux/ip.h>
6162 +#include <linux/skbuff.h>
6163 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
6164 +#include <linux/errno.h>
6165 +#include <asm/uaccess.h>
6166 +#include <asm/bitops.h>
6167 +#include <linux/spinlock.h>
6168 +#include <linux/random.h>
6170 +#include <net/ip.h>
6172 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
6174 +static int limit = MAX_RANGE;
6176 +static inline __u32
6177 +nethash_id_cidr(const struct ip_set_nethash *map,
6183 + ip_set_ip_t *elem;
6185 + ip = pack_ip_cidr(ip, cidr);
6189 + for (i = 0; i < map->probes; i++) {
6190 + id = jhash_ip(map, i, ip) % map->hashsize;
6191 + DP("hash key: %u", id);
6192 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6195 + /* No shortcut - there can be deleted entries. */
6200 +static inline __u32
6201 +nethash_id(struct ip_set *set, ip_set_ip_t ip)
6203 + const struct ip_set_nethash *map = set->data;
6204 + __u32 id = UINT_MAX;
6207 + for (i = 0; i < 30 && map->cidr[i]; i++) {
6208 + id = nethash_id_cidr(map, ip, map->cidr[i]);
6209 + if (id != UINT_MAX)
6216 +nethash_test_cidr(struct ip_set *set, ip_set_ip_t ip, uint8_t cidr)
6218 + const struct ip_set_nethash *map = set->data;
6220 + return (nethash_id_cidr(map, ip, cidr) != UINT_MAX);
6224 +nethash_test(struct ip_set *set, ip_set_ip_t ip)
6226 + return (nethash_id(set, ip) != UINT_MAX);
6230 +nethash_utest(struct ip_set *set, const void *data, u_int32_t size)
6232 + const struct ip_set_req_nethash *req = data;
6234 + if (req->cidr <= 0 || req->cidr > 32)
6236 + return (req->cidr == 32 ? nethash_test(set, req->ip)
6237 + : nethash_test_cidr(set, req->ip, req->cidr));
6240 +#define KADT_CONDITION
6242 +KADT(nethash, test, ipaddr)
6245 +__nethash_add(struct ip_set_nethash *map, ip_set_ip_t *ip)
6249 + ip_set_ip_t *elem, *slot = NULL;
6251 + for (i = 0; i < map->probes; i++) {
6252 + probe = jhash_ip(map, i, *ip) % map->hashsize;
6253 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
6256 + if (!(slot || *elem))
6258 + /* There can be deleted entries, must check all slots */
6265 + /* Trigger rehashing */
6270 +nethash_add(struct ip_set *set, ip_set_ip_t ip, uint8_t cidr)
6272 + struct ip_set_nethash *map = set->data;
6275 + if (map->elements >= limit || map->nets[cidr-1] == UINT16_MAX)
6277 + if (cidr <= 0 || cidr >= 32)
6280 + ip = pack_ip_cidr(ip, cidr);
6284 + ret = __nethash_add(map, &ip);
6286 + if (!map->nets[cidr-1]++)
6287 + add_cidr_size(map->cidr, cidr);
6293 +#undef KADT_CONDITION
6294 +#define KADT_CONDITION \
6295 + struct ip_set_nethash *map = set->data; \
6296 + uint8_t cidr = map->cidr[0] ? map->cidr[0] : 31;
6298 +UADT(nethash, add, req->cidr)
6299 +KADT(nethash, add, ipaddr, cidr)
6302 +__nethash_retry(struct ip_set_nethash *tmp, struct ip_set_nethash *map)
6304 + memcpy(tmp->cidr, map->cidr, sizeof(tmp->cidr));
6305 + memcpy(tmp->nets, map->nets, sizeof(tmp->nets));
6308 +HASH_RETRY(nethash, ip_set_ip_t)
6311 +nethash_del(struct ip_set *set, ip_set_ip_t ip, uint8_t cidr)
6313 + struct ip_set_nethash *map = set->data;
6314 + ip_set_ip_t id, *elem;
6316 + if (cidr <= 0 || cidr >= 32)
6319 + id = nethash_id_cidr(map, ip, cidr);
6320 + if (id == UINT_MAX)
6323 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
6326 + if (!map->nets[cidr-1]--)
6327 + del_cidr_size(map->cidr, cidr);
6331 +UADT(nethash, del, req->cidr)
6332 +KADT(nethash, del, ipaddr, cidr)
6335 +__nethash_create(const struct ip_set_req_nethash_create *req,
6336 + struct ip_set_nethash *map)
6338 + memset(map->cidr, 0, sizeof(map->cidr));
6339 + memset(map->nets, 0, sizeof(map->nets));
6344 +HASH_CREATE(nethash, ip_set_ip_t)
6345 +HASH_DESTROY(nethash)
6347 +HASH_FLUSH_CIDR(nethash, ip_set_ip_t)
6350 +__nethash_list_header(const struct ip_set_nethash *map,
6351 + struct ip_set_req_nethash_create *header)
6355 +HASH_LIST_HEADER(nethash)
6356 +HASH_LIST_MEMBERS_SIZE(nethash, ip_set_ip_t)
6357 +HASH_LIST_MEMBERS(nethash, ip_set_ip_t)
6359 +IP_SET_RTYPE(nethash, IPSET_TYPE_IP | IPSET_DATA_SINGLE)
6361 +MODULE_LICENSE("GPL");
6362 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6363 +MODULE_DESCRIPTION("nethash type of IP sets");
6364 +module_param(limit, int, 0600);
6365 +MODULE_PARM_DESC(limit, "maximal number of elements stored in the sets");
6367 +REGISTER_MODULE(nethash)
6368 diff --git a/net/ipv4/netfilter/ip_set_portmap.c b/net/ipv4/netfilter/ip_set_portmap.c
6369 new file mode 100644
6370 index 0000000..8bb6e76
6372 +++ b/net/ipv4/netfilter/ip_set_portmap.c
6374 +/* Copyright (C) 2003-2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6376 + * This program is free software; you can redistribute it and/or modify
6377 + * it under the terms of the GNU General Public License version 2 as
6378 + * published by the Free Software Foundation.
6381 +/* Kernel module implementing a port set type as a bitmap */
6383 +#include <linux/module.h>
6384 +#include <linux/ip.h>
6385 +#include <linux/tcp.h>
6386 +#include <linux/udp.h>
6387 +#include <linux/skbuff.h>
6388 +#include <linux/errno.h>
6389 +#include <asm/uaccess.h>
6390 +#include <asm/bitops.h>
6391 +#include <linux/spinlock.h>
6393 +#include <net/ip.h>
6395 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
6396 +#include <linux/netfilter_ipv4/ip_set_getport.h>
6399 +portmap_test(const struct ip_set *set, ip_set_ip_t port)
6401 + const struct ip_set_portmap *map = set->data;
6403 + if (port < map->first_ip || port > map->last_ip)
6406 + DP("set: %s, port: %u", set->name, port);
6407 + return !!test_bit(port - map->first_ip, map->members);
6410 +#define KADT_CONDITION \
6411 + if (ip == INVALID_PORT) \
6414 +UADT(portmap, test)
6415 +KADT(portmap, test, get_port)
6418 +portmap_add(struct ip_set *set, ip_set_ip_t port)
6420 + struct ip_set_portmap *map = set->data;
6422 + if (port < map->first_ip || port > map->last_ip)
6424 + if (test_and_set_bit(port - map->first_ip, map->members))
6427 + DP("set: %s, port %u", set->name, port);
6432 +KADT(portmap, add, get_port)
6435 +portmap_del(struct ip_set *set, ip_set_ip_t port)
6437 + struct ip_set_portmap *map = set->data;
6439 + if (port < map->first_ip || port > map->last_ip)
6441 + if (!test_and_clear_bit(port - map->first_ip, map->members))
6444 + DP("set: %s, port %u", set->name, port);
6449 +KADT(portmap, del, get_port)
6452 +__portmap_create(const struct ip_set_req_portmap_create *req,
6453 + struct ip_set_portmap *map)
6455 + if (req->to - req->from > MAX_RANGE) {
6456 + ip_set_printk("range too big, %d elements (max %d)",
6457 + req->to - req->from + 1, MAX_RANGE+1);
6460 + return bitmap_bytes(req->from, req->to);
6463 +BITMAP_CREATE(portmap)
6464 +BITMAP_DESTROY(portmap)
6465 +BITMAP_FLUSH(portmap)
6468 +__portmap_list_header(const struct ip_set_portmap *map,
6469 + struct ip_set_req_portmap_create *header)
6473 +BITMAP_LIST_HEADER(portmap)
6474 +BITMAP_LIST_MEMBERS_SIZE(portmap, ip_set_ip_t, (map->last_ip - map->first_ip + 1),
6475 + test_bit(i, map->members))
6478 +portmap_list_members(const struct ip_set *set, void *data, char dont_align)
6480 + const struct ip_set_portmap *map = set->data;
6481 + uint32_t i, n = 0;
6485 + memcpy(data, map->members, map->size);
6489 + for (i = 0; i < map->last_ip - map->first_ip + 1; i++)
6490 + if (test_bit(i, map->members)) {
6491 + d = data + n * IPSET_ALIGN(sizeof(ip_set_ip_t));
6492 + *d = map->first_ip + i;
6497 +IP_SET_TYPE(portmap, IPSET_TYPE_PORT | IPSET_DATA_SINGLE)
6499 +MODULE_LICENSE("GPL");
6500 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6501 +MODULE_DESCRIPTION("portmap type of IP sets");
6503 +REGISTER_MODULE(portmap)
6504 diff --git a/net/ipv4/netfilter/ip_set_setlist.c b/net/ipv4/netfilter/ip_set_setlist.c
6505 new file mode 100644
6506 index 0000000..3cfdae8
6508 +++ b/net/ipv4/netfilter/ip_set_setlist.c
6510 +/* Copyright (C) 2008 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6512 + * This program is free software; you can redistribute it and/or modify
6513 + * it under the terms of the GNU General Public License version 2 as
6514 + * published by the Free Software Foundation.
6517 +/* Kernel module implementing an IP set type: the setlist type */
6519 +#include <linux/module.h>
6520 +#include <linux/ip.h>
6521 +#include <linux/skbuff.h>
6522 +#include <linux/errno.h>
6524 +#include <linux/netfilter_ipv4/ip_set.h>
6525 +#include <linux/netfilter_ipv4/ip_set_bitmaps.h>
6526 +#include <linux/netfilter_ipv4/ip_set_setlist.h>
6529 + * before ==> index, ref
6530 + * after ==> ref, index
6534 +next_index_eq(const struct ip_set_setlist *map, int i, ip_set_id_t index)
6536 + return i < map->size && map->index[i] == index;
6540 +setlist_utest(struct ip_set *set, const void *data, u_int32_t size)
6542 + const struct ip_set_setlist *map = set->data;
6543 + const struct ip_set_req_setlist *req = data;
6544 + ip_set_id_t index, ref = IP_SET_INVALID_ID;
6548 + if (req->before && req->ref[0] == '\0')
6551 + index = __ip_set_get_byname(req->name, &s);
6552 + if (index == IP_SET_INVALID_ID)
6554 + if (req->ref[0] != '\0') {
6555 + ref = __ip_set_get_byname(req->ref, &s);
6556 + if (ref == IP_SET_INVALID_ID)
6559 + for (i = 0; i < map->size
6560 + && map->index[i] != IP_SET_INVALID_ID; i++) {
6561 + if (req->before && map->index[i] == index) {
6562 + res = next_index_eq(map, i + 1, ref);
6564 + } else if (!req->before) {
6565 + if ((ref == IP_SET_INVALID_ID
6566 + && map->index[i] == index)
6567 + || (map->index[i] == ref
6568 + && next_index_eq(map, i + 1, index))) {
6574 + if (ref != IP_SET_INVALID_ID)
6575 + __ip_set_put_byindex(ref);
6577 + __ip_set_put_byindex(index);
6582 +setlist_ktest(struct ip_set *set,
6583 + const struct sk_buff *skb,
6584 + const u_int32_t *flags)
6586 + struct ip_set_setlist *map = set->data;
6589 + for (i = 0; i < map->size
6590 + && map->index[i] != IP_SET_INVALID_ID
6592 + res = ip_set_testip_kernel(map->index[i], skb, flags);
6597 +insert_setlist(struct ip_set_setlist *map, int i, ip_set_id_t index)
6602 + DP("i: %u, last %u\n", i, map->index[map->size - 1]);
6603 + if (i >= map->size || map->index[map->size - 1] != IP_SET_INVALID_ID)
6606 + for (j = i; j < map->size
6607 + && index != IP_SET_INVALID_ID; j++) {
6608 + tmp = map->index[j];
6609 + map->index[j] = index;
6616 +setlist_uadd(struct ip_set *set, const void *data, u_int32_t size)
6618 + struct ip_set_setlist *map = set->data;
6619 + const struct ip_set_req_setlist *req = data;
6620 + ip_set_id_t index, ref = IP_SET_INVALID_ID;
6621 + int i, res = -ERANGE;
6624 + if (req->before && req->ref[0] == '\0')
6627 + index = __ip_set_get_byname(req->name, &s);
6628 + if (index == IP_SET_INVALID_ID)
6630 + /* "Loop detection" */
6631 + if (strcmp(s->type->typename, "setlist") == 0)
6634 + if (req->ref[0] != '\0') {
6635 + ref = __ip_set_get_byname(req->ref, &s);
6636 + if (ref == IP_SET_INVALID_ID) {
6641 + for (i = 0; i < map->size; i++) {
6642 + if (map->index[i] != ref)
6645 + res = insert_setlist(map, i, index);
6647 + res = insert_setlist(map,
6648 + ref == IP_SET_INVALID_ID ? i : i + 1,
6652 + if (ref != IP_SET_INVALID_ID)
6653 + __ip_set_put_byindex(ref);
6654 + /* In case of success, we keep the reference to the set */
6657 + __ip_set_put_byindex(index);
6662 +setlist_kadd(struct ip_set *set,
6663 + const struct sk_buff *skb,
6664 + const u_int32_t *flags)
6666 + struct ip_set_setlist *map = set->data;
6667 + int i, res = -EINVAL;
6669 + for (i = 0; i < map->size
6670 + && map->index[i] != IP_SET_INVALID_ID
6672 + res = ip_set_addip_kernel(map->index[i], skb, flags);
6677 +unshift_setlist(struct ip_set_setlist *map, int i)
6681 + for (j = i; j < map->size - 1; j++)
6682 + map->index[j] = map->index[j+1];
6683 + map->index[map->size-1] = IP_SET_INVALID_ID;
6688 +setlist_udel(struct ip_set *set, const void *data, u_int32_t size)
6690 + struct ip_set_setlist *map = set->data;
6691 + const struct ip_set_req_setlist *req = data;
6692 + ip_set_id_t index, ref = IP_SET_INVALID_ID;
6693 + int i, res = -EEXIST;
6696 + if (req->before && req->ref[0] == '\0')
6699 + index = __ip_set_get_byname(req->name, &s);
6700 + if (index == IP_SET_INVALID_ID)
6702 + if (req->ref[0] != '\0') {
6703 + ref = __ip_set_get_byname(req->ref, &s);
6704 + if (ref == IP_SET_INVALID_ID)
6707 + for (i = 0; i < map->size
6708 + && map->index[i] != IP_SET_INVALID_ID; i++) {
6709 + if (req->before) {
6710 + if (map->index[i] == index
6711 + && next_index_eq(map, i + 1, ref)) {
6712 + res = unshift_setlist(map, i);
6715 + } else if (ref == IP_SET_INVALID_ID) {
6716 + if (map->index[i] == index) {
6717 + res = unshift_setlist(map, i);
6720 + } else if (map->index[i] == ref
6721 + && next_index_eq(map, i + 1, index)) {
6722 + res = unshift_setlist(map, i + 1);
6726 + if (ref != IP_SET_INVALID_ID)
6727 + __ip_set_put_byindex(ref);
6729 + __ip_set_put_byindex(index);
6730 + /* In case of success, release the reference to the set */
6732 + __ip_set_put_byindex(index);
6737 +setlist_kdel(struct ip_set *set,
6738 + const struct sk_buff *skb,
6739 + const u_int32_t *flags)
6741 + struct ip_set_setlist *map = set->data;
6742 + int i, res = -EINVAL;
6744 + for (i = 0; i < map->size
6745 + && map->index[i] != IP_SET_INVALID_ID
6747 + res = ip_set_delip_kernel(map->index[i], skb, flags);
6752 +setlist_create(struct ip_set *set, const void *data, u_int32_t size)
6754 + struct ip_set_setlist *map;
6755 + const struct ip_set_req_setlist_create *req = data;
6758 + map = kmalloc(sizeof(struct ip_set_setlist) +
6759 + req->size * sizeof(ip_set_id_t), GFP_KERNEL);
6762 + map->size = req->size;
6763 + for (i = 0; i < map->size; i++)
6764 + map->index[i] = IP_SET_INVALID_ID;
6771 +setlist_destroy(struct ip_set *set)
6773 + struct ip_set_setlist *map = set->data;
6776 + for (i = 0; i < map->size
6777 + && map->index[i] != IP_SET_INVALID_ID; i++)
6778 + __ip_set_put_byindex(map->index[i]);
6785 +setlist_flush(struct ip_set *set)
6787 + struct ip_set_setlist *map = set->data;
6790 + for (i = 0; i < map->size
6791 + && map->index[i] != IP_SET_INVALID_ID; i++) {
6792 + __ip_set_put_byindex(map->index[i]);
6793 + map->index[i] = IP_SET_INVALID_ID;
6798 +setlist_list_header(const struct ip_set *set, void *data)
6800 + const struct ip_set_setlist *map = set->data;
6801 + struct ip_set_req_setlist_create *header = data;
6803 + header->size = map->size;
6807 +setlist_list_members_size(const struct ip_set *set, char dont_align)
6809 + const struct ip_set_setlist *map = set->data;
6811 + return map->size * IPSET_VALIGN(sizeof(ip_set_id_t), dont_align);
6815 +setlist_list_members(const struct ip_set *set, void *data, char dont_align)
6817 + struct ip_set_setlist *map = set->data;
6821 + for (i = 0; i < map->size; i++) {
6822 + d = data + i * IPSET_VALIGN(sizeof(ip_set_id_t), dont_align);
6823 + *d = ip_set_id(map->index[i]);
6827 +IP_SET_TYPE(setlist, IPSET_TYPE_SETNAME | IPSET_DATA_SINGLE)
6829 +MODULE_LICENSE("GPL");
6830 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
6831 +MODULE_DESCRIPTION("setlist type of IP sets");
6833 +REGISTER_MODULE(setlist)
6834 diff --git a/net/ipv4/netfilter/ipt_SET.c b/net/ipv4/netfilter/ipt_SET.c
6835 new file mode 100644
6836 index 0000000..952b5d9
6838 +++ b/net/ipv4/netfilter/ipt_SET.c
6840 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
6841 + * Patrick Schaaf <bof@bof.de>
6842 + * Martin Josefsson <gandalf@wlug.westbo.se>
6843 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6845 + * This program is free software; you can redistribute it and/or modify
6846 + * it under the terms of the GNU General Public License version 2 as
6847 + * published by the Free Software Foundation.
6850 +/* ipt_SET.c - netfilter target to manipulate IP sets */
6852 +#include <linux/module.h>
6853 +#include <linux/ip.h>
6854 +#include <linux/skbuff.h>
6855 +#include <linux/version.h>
6857 +#include <linux/netfilter_ipv4.h>
6858 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
6859 +#include <linux/netfilter_ipv4/ip_tables.h>
6860 +#define xt_register_target ipt_register_target
6861 +#define xt_unregister_target ipt_unregister_target
6862 +#define xt_target ipt_target
6863 +#define XT_CONTINUE IPT_CONTINUE
6865 +#include <linux/netfilter/x_tables.h>
6867 +#include <linux/netfilter_ipv4/ipt_set.h>
6869 +static unsigned int
6870 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
6871 +target(struct sk_buff **pskb,
6872 + unsigned int hooknum,
6873 + const struct net_device *in,
6874 + const struct net_device *out,
6875 + const void *targinfo,
6877 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
6878 +target(struct sk_buff **pskb,
6879 + const struct net_device *in,
6880 + const struct net_device *out,
6881 + unsigned int hooknum,
6882 + const void *targinfo,
6884 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
6885 +target(struct sk_buff **pskb,
6886 + const struct net_device *in,
6887 + const struct net_device *out,
6888 + unsigned int hooknum,
6889 + const struct xt_target *target,
6890 + const void *targinfo,
6892 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
6893 +target(struct sk_buff **pskb,
6894 + const struct net_device *in,
6895 + const struct net_device *out,
6896 + unsigned int hooknum,
6897 + const struct xt_target *target,
6898 + const void *targinfo)
6899 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
6900 +target(struct sk_buff *skb,
6901 + const struct net_device *in,
6902 + const struct net_device *out,
6903 + unsigned int hooknum,
6904 + const struct xt_target *target,
6905 + const void *targinfo)
6906 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
6907 +target(struct sk_buff *skb,
6908 + const struct xt_target_param *par)
6909 +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35) */
6910 +target(struct sk_buff *skb,
6911 + const struct xt_action_param *par)
6914 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
6915 + const struct ipt_set_info_target *info = targinfo;
6917 + const struct ipt_set_info_target *info = par->targinfo;
6919 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
6920 + struct sk_buff *skb = *pskb;
6924 + if (info->add_set.index != IP_SET_INVALID_ID)
6925 + ip_set_addip_kernel(info->add_set.index,
6927 + info->add_set.flags);
6928 + if (info->del_set.index != IP_SET_INVALID_ID)
6929 + ip_set_delip_kernel(info->del_set.index,
6931 + info->del_set.flags);
6933 + return XT_CONTINUE;
6936 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
6938 +#define CHECK_FAIL 0
6939 +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35) */
6941 +#define CHECK_FAIL -EINVAL
6944 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
6946 +checkentry(const char *tablename,
6947 + const struct ipt_entry *e,
6949 + unsigned int targinfosize,
6950 + unsigned int hook_mask)
6951 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
6953 +checkentry(const char *tablename,
6956 + unsigned int targinfosize,
6957 + unsigned int hook_mask)
6958 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
6960 +checkentry(const char *tablename,
6962 + const struct xt_target *target,
6964 + unsigned int targinfosize,
6965 + unsigned int hook_mask)
6966 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
6968 +checkentry(const char *tablename,
6970 + const struct xt_target *target,
6972 + unsigned int hook_mask)
6973 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
6975 +checkentry(const char *tablename,
6977 + const struct xt_target *target,
6979 + unsigned int hook_mask)
6980 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
6982 +checkentry(const struct xt_tgchk_param *par)
6983 +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35) */
6985 +checkentry(const struct xt_tgchk_param *par)
6988 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
6989 + const struct ipt_set_info_target *info = targinfo;
6991 + const struct ipt_set_info_target *info = par->targinfo;
6993 + ip_set_id_t index;
6995 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
6996 + if (targinfosize != IPT_ALIGN(sizeof(*info))) {
6997 + DP("bad target info size %u", targinfosize);
6998 + return CHECK_FAIL;
7002 + if (info->add_set.index != IP_SET_INVALID_ID) {
7003 + index = ip_set_get_byindex(info->add_set.index);
7004 + if (index == IP_SET_INVALID_ID) {
7005 + ip_set_printk("cannot find add_set index %u as target",
7006 + info->add_set.index);
7007 + return CHECK_FAIL; /* error */
7011 + if (info->del_set.index != IP_SET_INVALID_ID) {
7012 + index = ip_set_get_byindex(info->del_set.index);
7013 + if (index == IP_SET_INVALID_ID) {
7014 + ip_set_printk("cannot find del_set index %u as target",
7015 + info->del_set.index);
7016 + return CHECK_FAIL; /* error */
7019 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
7020 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7021 + ip_set_printk("That's nasty!");
7022 + return CHECK_FAIL; /* error */
7028 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
7029 +static void destroy(void *targetinfo,
7030 + unsigned int targetsize)
7031 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7032 +static void destroy(const struct xt_target *target,
7034 + unsigned int targetsize)
7035 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
7036 +static void destroy(const struct xt_target *target,
7038 +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28) */
7039 +static void destroy(const struct xt_tgdtor_param *par)
7042 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
7043 + const struct ipt_set_info_target *info = targetinfo;
7045 + const struct ipt_set_info_target *info = par->targinfo;
7048 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
7049 + if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
7050 + ip_set_printk("invalid targetsize %d", targetsize);
7054 + if (info->add_set.index != IP_SET_INVALID_ID)
7055 + ip_set_put_byindex(info->add_set.index);
7056 + if (info->del_set.index != IP_SET_INVALID_ID)
7057 + ip_set_put_byindex(info->del_set.index);
7060 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
7061 +static struct xt_target SET_target = {
7064 + .checkentry = checkentry,
7065 + .destroy = destroy,
7068 +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17) */
7069 +static struct xt_target SET_target = {
7071 + .family = AF_INET,
7073 + .targetsize = sizeof(struct ipt_set_info_target),
7074 + .checkentry = checkentry,
7075 + .destroy = destroy,
7080 +MODULE_LICENSE("GPL");
7081 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7082 +MODULE_DESCRIPTION("iptables IP set target module");
7084 +static int __init ipt_SET_init(void)
7086 + return xt_register_target(&SET_target);
7089 +static void __exit ipt_SET_fini(void)
7091 + xt_unregister_target(&SET_target);
7094 +module_init(ipt_SET_init);
7095 +module_exit(ipt_SET_fini);
7096 diff --git a/net/ipv4/netfilter/ipt_set.c b/net/ipv4/netfilter/ipt_set.c
7097 new file mode 100644
7098 index 0000000..3c661be
7100 +++ b/net/ipv4/netfilter/ipt_set.c
7102 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
7103 + * Patrick Schaaf <bof@bof.de>
7104 + * Martin Josefsson <gandalf@wlug.westbo.se>
7105 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
7107 + * This program is free software; you can redistribute it and/or modify
7108 + * it under the terms of the GNU General Public License version 2 as
7109 + * published by the Free Software Foundation.
7112 +/* Kernel module to match an IP set. */
7114 +#include <linux/module.h>
7115 +#include <linux/ip.h>
7116 +#include <linux/skbuff.h>
7117 +#include <linux/version.h>
7119 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
7120 +#include <linux/netfilter_ipv4/ip_tables.h>
7121 +#define xt_register_match ipt_register_match
7122 +#define xt_unregister_match ipt_unregister_match
7123 +#define xt_match ipt_match
7125 +#include <linux/netfilter/x_tables.h>
7127 +#include <linux/netfilter_ipv4/ip_set.h>
7128 +#include <linux/netfilter_ipv4/ipt_set.h>
7131 +match_set(const struct ipt_set_info *info,
7132 + const struct sk_buff *skb,
7135 + if (ip_set_testip_kernel(info->index, skb, info->flags))
7140 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
7142 +match(const struct sk_buff *skb,
7143 + const struct net_device *in,
7144 + const struct net_device *out,
7145 + const void *matchinfo,
7148 + u_int16_t datalen,
7150 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
7152 +match(const struct sk_buff *skb,
7153 + const struct net_device *in,
7154 + const struct net_device *out,
7155 + const void *matchinfo,
7158 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
7160 +match(const struct sk_buff *skb,
7161 + const struct net_device *in,
7162 + const struct net_device *out,
7163 + const void *matchinfo,
7165 + unsigned int protoff,
7167 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
7169 +match(const struct sk_buff *skb,
7170 + const struct net_device *in,
7171 + const struct net_device *out,
7172 + const struct xt_match *match,
7173 + const void *matchinfo,
7175 + unsigned int protoff,
7177 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
7179 +match(const struct sk_buff *skb,
7180 + const struct net_device *in,
7181 + const struct net_device *out,
7182 + const struct xt_match *match,
7183 + const void *matchinfo,
7185 + unsigned int protoff,
7187 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
7189 +match(const struct sk_buff *skb,
7190 + const struct xt_match_param *par)
7191 +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35) */
7193 +match(const struct sk_buff *skb,
7194 + struct xt_action_param *par)
7197 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
7198 + const struct ipt_set_info_match *info = matchinfo;
7200 + const struct ipt_set_info_match *info = par->matchinfo;
7203 + return match_set(&info->match_set,
7205 + info->match_set.flags[0] & IPSET_MATCH_INV);
7208 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
7210 +#define CHECK_FAIL 0
7211 +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35) */
7213 +#define CHECK_FAIL -EINVAL
7216 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
7218 +checkentry(const char *tablename,
7219 + const struct ipt_ip *ip,
7221 + unsigned int matchsize,
7222 + unsigned int hook_mask)
7223 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
7225 +checkentry(const char *tablename,
7228 + unsigned int matchsize,
7229 + unsigned int hook_mask)
7230 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7232 +checkentry(const char *tablename,
7234 + const struct xt_match *match,
7236 + unsigned int matchsize,
7237 + unsigned int hook_mask)
7238 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
7240 +checkentry(const char *tablename,
7242 + const struct xt_match *match,
7244 + unsigned int hook_mask)
7245 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
7247 +checkentry(const char *tablename,
7249 + const struct xt_match *match,
7251 + unsigned int hook_mask)
7252 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
7254 +checkentry(const struct xt_mtchk_param *par)
7255 +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35) */
7257 +checkentry(const struct xt_mtchk_param *par)
7260 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
7261 + struct ipt_set_info_match *info = matchinfo;
7263 + struct ipt_set_info_match *info = par->matchinfo;
7265 + ip_set_id_t index;
7267 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
7268 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7269 + ip_set_printk("invalid matchsize %d", matchsize);
7270 + return CHECK_FAIL;
7274 + index = ip_set_get_byindex(info->match_set.index);
7276 + if (index == IP_SET_INVALID_ID) {
7277 + ip_set_printk("Cannot find set indentified by id %u to match",
7278 + info->match_set.index);
7279 + return CHECK_FAIL; /* error */
7281 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
7282 + ip_set_printk("That's nasty!");
7283 + return CHECK_FAIL; /* error */
7289 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
7290 +static void destroy(void *matchinfo,
7291 + unsigned int matchsize)
7292 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
7293 +static void destroy(const struct xt_match *match,
7295 + unsigned int matchsize)
7296 +#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
7297 +static void destroy(const struct xt_match *match,
7299 +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28) */
7300 +static void destroy(const struct xt_mtdtor_param *par)
7303 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
7304 + struct ipt_set_info_match *info = matchinfo;
7306 + struct ipt_set_info_match *info = par->matchinfo;
7310 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
7311 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
7312 + ip_set_printk("invalid matchsize %d", matchsize);
7316 + ip_set_put_byindex(info->match_set.index);
7319 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
7320 +static struct xt_match set_match = {
7323 + .checkentry = &checkentry,
7324 + .destroy = &destroy,
7327 +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17) */
7328 +static struct xt_match set_match = {
7330 + .family = AF_INET,
7332 + .matchsize = sizeof(struct ipt_set_info_match),
7333 + .checkentry = &checkentry,
7334 + .destroy = &destroy,
7339 +MODULE_LICENSE("GPL");
7340 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
7341 +MODULE_DESCRIPTION("iptables IP set match module");
7343 +static int __init ipt_ipset_init(void)
7345 + return xt_register_match(&set_match);
7348 +static void __exit ipt_ipset_fini(void)
7350 + xt_unregister_match(&set_match);
7353 +module_init(ipt_ipset_init);
7354 +module_exit(ipt_ipset_fini);