70208660058f659a9e32eb3d26ecfee15e5eec70
[openwrt/svn-archive/archive.git] / target / linux / generic-2.4 / patches / 608-netfilter_ipset.patch
1 --- a/Documentation/Configure.help
2 +++ b/Documentation/Configure.help
3 @@ -3231,6 +3231,75 @@ CONFIG_IP_NF_TARGET_CLASSIFY
4 If you want to compile it as a module, say M here and read
5 Documentation/modules.txt. If unsure, say `N'.
6
7 +IP set support
8 +CONFIG_IP_NF_SET
9 + This option adds IP set support to the kernel.
10 +
11 + In order to define and use sets, you need userlevel utilities: an
12 + iptables binary which knows about IP sets and the program ipset(8),
13 + by which you can define and setup the sets themselves.
14 +
15 + If you want to compile it as a module, say M here and read
16 + <file:Documentation/modules.txt>. If unsure, say `N'.
17 +
18 +set match support
19 +CONFIG_IP_NF_MATCH_SET
20 + This option adds IP set match support.
21 + You need the ipset utility to create and set up the sets.
22 +
23 + If you want to compile it as a module, say M here and read
24 + <file:Documentation/modules.txt>. If unsure, say `N'.
25 +
26 +SET target support
27 +CONFIG_IP_NF_TARGET_SET
28 + This option adds IP set target support.
29 + You need the ipset utility to create and set up the sets.
30 +
31 + If you want to compile it as a module, say M here and read
32 + <file:Documentation/modules.txt>. If unsure, say `N'.
33 +
34 +ipmap set type support
35 +CONFIG_IP_NF_SET_IPMAP
36 + This option adds the ipmap set type support.
37 +
38 + If you want to compile it as a module, say M here and read
39 + <file:Documentation/modules.txt>. If unsure, say `N'.
40 +
41 +macipmap set type support
42 +CONFIG_IP_NF_SET_MACIPMAP
43 + This option adds the macipmap set type support.
44 +
45 + If you want to compile it as a module, say M here and read
46 + <file:Documentation/modules.txt>. If unsure, say `N'.
47 +
48 +portmap set type support
49 +CONFIG_IP_NF_SET_PORTMAP
50 + This option adds the portmap set type support.
51 +
52 + If you want to compile it as a module, say M here and read
53 + <file:Documentation/modules.txt>. If unsure, say `N'.
54 +
55 +iphash set type support
56 +CONFIG_IP_NF_SET_IPHASH
57 + This option adds the iphash set type support.
58 +
59 + If you want to compile it as a module, say M here and read
60 + <file:Documentation/modules.txt>. If unsure, say `N'.
61 +
62 +nethash set type support
63 +CONFIG_IP_NF_SET_NETHASH
64 + This option adds the nethash set type support.
65 +
66 + If you want to compile it as a module, say M here and read
67 + <file:Documentation/modules.txt>. If unsure, say `N'.
68 +
69 +iptree set type support
70 +CONFIG_IP_NF_SET_IPTREE
71 + This option adds the iptree set type support.
72 +
73 + If you want to compile it as a module, say M here and read
74 + <file:Documentation/modules.txt>. If unsure, say `N'.
75 +
76 TTL target support
77 CONFIG_IP_NF_TARGET_TTL
78 This option adds a `TTL' target, which enables the user to set
79 --- /dev/null
80 +++ b/include/linux/netfilter_ipv4/ip_set.h
81 @@ -0,0 +1,489 @@
82 +#ifndef _IP_SET_H
83 +#define _IP_SET_H
84 +
85 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
86 + * Patrick Schaaf <bof@bof.de>
87 + * Martin Josefsson <gandalf@wlug.westbo.se>
88 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
89 + *
90 + * This program is free software; you can redistribute it and/or modify
91 + * it under the terms of the GNU General Public License version 2 as
92 + * published by the Free Software Foundation.
93 + */
94 +
95 +/*
96 + * A sockopt of such quality has hardly ever been seen before on the open
97 + * market! This little beauty, hardly ever used: above 64, so it's
98 + * traditionally used for firewalling, not touched (even once!) by the
99 + * 2.0, 2.2 and 2.4 kernels!
100 + *
101 + * Comes with its own certificate of authenticity, valid anywhere in the
102 + * Free world!
103 + *
104 + * Rusty, 19.4.2000
105 + */
106 +#define SO_IP_SET 83
107 +
108 +/*
109 + * Heavily modify by Joakim Axelsson 08.03.2002
110 + * - Made it more modulebased
111 + *
112 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
113 + * - bindings added
114 + * - in order to "deal with" backward compatibility, renamed to ipset
115 + */
116 +
117 +/*
118 + * Used so that the kernel module and ipset-binary can match their versions
119 + */
120 +#define IP_SET_PROTOCOL_VERSION 2
121 +
122 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
123 +
124 +/* Lets work with our own typedef for representing an IP address.
125 + * We hope to make the code more portable, possibly to IPv6...
126 + *
127 + * The representation works in HOST byte order, because most set types
128 + * will perform arithmetic operations and compare operations.
129 + *
130 + * For now the type is an uint32_t.
131 + *
132 + * Make sure to ONLY use the functions when translating and parsing
133 + * in order to keep the host byte order and make it more portable:
134 + * parse_ip()
135 + * parse_mask()
136 + * parse_ipandmask()
137 + * ip_tostring()
138 + * (Joakim: where are they???)
139 + */
140 +
141 +typedef uint32_t ip_set_ip_t;
142 +
143 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
144 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
145 + */
146 +typedef uint16_t ip_set_id_t;
147 +
148 +#define IP_SET_INVALID_ID 65535
149 +
150 +/* How deep we follow bindings */
151 +#define IP_SET_MAX_BINDINGS 6
152 +
153 +/*
154 + * Option flags for kernel operations (ipt_set_info)
155 + */
156 +#define IPSET_SRC 0x01 /* Source match/add */
157 +#define IPSET_DST 0x02 /* Destination match/add */
158 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
159 +
160 +/*
161 + * Set types (flavours)
162 + */
163 +#define IPSET_TYPE_IP 0 /* IP address type of set */
164 +#define IPSET_TYPE_PORT 1 /* Port type of set */
165 +
166 +/* Reserved keywords */
167 +#define IPSET_TOKEN_DEFAULT ":default:"
168 +#define IPSET_TOKEN_ALL ":all:"
169 +
170 +/* SO_IP_SET operation constants, and their request struct types.
171 + *
172 + * Operation ids:
173 + * 0-99: commands with version checking
174 + * 100-199: add/del/test/bind/unbind
175 + * 200-299: list, save, restore
176 + */
177 +
178 +/* Single shot operations:
179 + * version, create, destroy, flush, rename and swap
180 + *
181 + * Sets are identified by name.
182 + */
183 +
184 +#define IP_SET_REQ_STD \
185 + unsigned op; \
186 + unsigned version; \
187 + char name[IP_SET_MAXNAMELEN]
188 +
189 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
190 +struct ip_set_req_create {
191 + IP_SET_REQ_STD;
192 + char typename[IP_SET_MAXNAMELEN];
193 +};
194 +
195 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
196 +struct ip_set_req_std {
197 + IP_SET_REQ_STD;
198 +};
199 +
200 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
201 +/* Uses ip_set_req_std */
202 +
203 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
204 +/* Uses ip_set_req_create */
205 +
206 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
207 +/* Uses ip_set_req_create */
208 +
209 +union ip_set_name_index {
210 + char name[IP_SET_MAXNAMELEN];
211 + ip_set_id_t index;
212 +};
213 +
214 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
215 +struct ip_set_req_get_set {
216 + unsigned op;
217 + unsigned version;
218 + union ip_set_name_index set;
219 +};
220 +
221 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
222 +/* Uses ip_set_req_get_set */
223 +
224 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
225 +struct ip_set_req_version {
226 + unsigned op;
227 + unsigned version;
228 +};
229 +
230 +/* Double shots operations:
231 + * add, del, test, bind and unbind.
232 + *
233 + * First we query the kernel to get the index and type of the target set,
234 + * then issue the command. Validity of IP is checked in kernel in order
235 + * to minimalize sockopt operations.
236 + */
237 +
238 +/* Get minimal set data for add/del/test/bind/unbind IP */
239 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
240 +struct ip_set_req_adt_get {
241 + unsigned op;
242 + unsigned version;
243 + union ip_set_name_index set;
244 + char typename[IP_SET_MAXNAMELEN];
245 +};
246 +
247 +#define IP_SET_REQ_BYINDEX \
248 + unsigned op; \
249 + ip_set_id_t index;
250 +
251 +struct ip_set_req_adt {
252 + IP_SET_REQ_BYINDEX;
253 +};
254 +
255 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
256 +/* Uses ip_set_req_adt, with type specific addage */
257 +
258 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
259 +/* Uses ip_set_req_adt, with type specific addage */
260 +
261 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
262 +/* Uses ip_set_req_adt, with type specific addage */
263 +
264 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
265 +/* Uses ip_set_req_bind, with type specific addage */
266 +struct ip_set_req_bind {
267 + IP_SET_REQ_BYINDEX;
268 + char binding[IP_SET_MAXNAMELEN];
269 +};
270 +
271 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
272 +/* Uses ip_set_req_bind, with type speficic addage
273 + * index = 0 means unbinding for all sets */
274 +
275 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
276 +/* Uses ip_set_req_bind, with type specific addage */
277 +
278 +/* Multiple shots operations: list, save, restore.
279 + *
280 + * - check kernel version and query the max number of sets
281 + * - get the basic information on all sets
282 + * and size required for the next step
283 + * - get actual set data: header, data, bindings
284 + */
285 +
286 +/* Get max_sets and the index of a queried set
287 + */
288 +#define IP_SET_OP_MAX_SETS 0x00000020
289 +struct ip_set_req_max_sets {
290 + unsigned op;
291 + unsigned version;
292 + ip_set_id_t max_sets; /* max_sets */
293 + ip_set_id_t sets; /* real number of sets */
294 + union ip_set_name_index set; /* index of set if name used */
295 +};
296 +
297 +/* Get the id and name of the sets plus size for next step */
298 +#define IP_SET_OP_LIST_SIZE 0x00000201
299 +#define IP_SET_OP_SAVE_SIZE 0x00000202
300 +struct ip_set_req_setnames {
301 + unsigned op;
302 + ip_set_id_t index; /* set to list/save */
303 + size_t size; /* size to get setdata/bindings */
304 + /* followed by sets number of struct ip_set_name_list */
305 +};
306 +
307 +struct ip_set_name_list {
308 + char name[IP_SET_MAXNAMELEN];
309 + char typename[IP_SET_MAXNAMELEN];
310 + ip_set_id_t index;
311 + ip_set_id_t id;
312 +};
313 +
314 +/* The actual list operation */
315 +#define IP_SET_OP_LIST 0x00000203
316 +struct ip_set_req_list {
317 + IP_SET_REQ_BYINDEX;
318 + /* sets number of struct ip_set_list in reply */
319 +};
320 +
321 +struct ip_set_list {
322 + ip_set_id_t index;
323 + ip_set_id_t binding;
324 + u_int32_t ref;
325 + size_t header_size; /* Set header data of header_size */
326 + size_t members_size; /* Set members data of members_size */
327 + size_t bindings_size; /* Set bindings data of bindings_size */
328 +};
329 +
330 +struct ip_set_hash_list {
331 + ip_set_ip_t ip;
332 + ip_set_id_t binding;
333 +};
334 +
335 +/* The save operation */
336 +#define IP_SET_OP_SAVE 0x00000204
337 +/* Uses ip_set_req_list, in the reply replaced by
338 + * sets number of struct ip_set_save plus a marker
339 + * ip_set_save followed by ip_set_hash_save structures.
340 + */
341 +struct ip_set_save {
342 + ip_set_id_t index;
343 + ip_set_id_t binding;
344 + size_t header_size; /* Set header data of header_size */
345 + size_t members_size; /* Set members data of members_size */
346 +};
347 +
348 +/* At restoring, ip == 0 means default binding for the given set: */
349 +struct ip_set_hash_save {
350 + ip_set_ip_t ip;
351 + ip_set_id_t id;
352 + ip_set_id_t binding;
353 +};
354 +
355 +/* The restore operation */
356 +#define IP_SET_OP_RESTORE 0x00000205
357 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
358 + * plus a marker ip_set_restore, followed by ip_set_hash_save
359 + * structures.
360 + */
361 +struct ip_set_restore {
362 + char name[IP_SET_MAXNAMELEN];
363 + char typename[IP_SET_MAXNAMELEN];
364 + ip_set_id_t index;
365 + size_t header_size; /* Create data of header_size */
366 + size_t members_size; /* Set members data of members_size */
367 +};
368 +
369 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
370 +{
371 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
372 +}
373 +
374 +#ifdef __KERNEL__
375 +
376 +#define ip_set_printk(format, args...) \
377 + do { \
378 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
379 + printk(format "\n" , ## args); \
380 + } while (0)
381 +
382 +#if defined(IP_SET_DEBUG)
383 +#define DP(format, args...) \
384 + do { \
385 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
386 + printk(format "\n" , ## args); \
387 + } while (0)
388 +#define IP_SET_ASSERT(x) \
389 + do { \
390 + if (!(x)) \
391 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
392 + __FILE__, __LINE__, __FUNCTION__); \
393 + } while (0)
394 +#else
395 +#define DP(format, args...)
396 +#define IP_SET_ASSERT(x)
397 +#endif
398 +
399 +struct ip_set;
400 +
401 +/*
402 + * The ip_set_type definition - one per set type, e.g. "ipmap".
403 + *
404 + * Each individual set has a pointer, set->type, going to one
405 + * of these structures. Function pointers inside the structure implement
406 + * the real behaviour of the sets.
407 + *
408 + * If not mentioned differently, the implementation behind the function
409 + * pointers of a set_type, is expected to return 0 if ok, and a negative
410 + * errno (e.g. -EINVAL) on error.
411 + */
412 +struct ip_set_type {
413 + struct list_head list; /* next in list of set types */
414 +
415 + /* test for IP in set (kernel: iptables -m set src|dst)
416 + * return 0 if not in set, 1 if in set.
417 + */
418 + int (*testip_kernel) (struct ip_set *set,
419 + const struct sk_buff * skb,
420 + u_int32_t flags,
421 + ip_set_ip_t *ip);
422 +
423 + /* test for IP in set (userspace: ipset -T set IP)
424 + * return 0 if not in set, 1 if in set.
425 + */
426 + int (*testip) (struct ip_set *set,
427 + const void *data, size_t size,
428 + ip_set_ip_t *ip);
429 +
430 + /*
431 + * Size of the data structure passed by when
432 + * adding/deletin/testing an entry.
433 + */
434 + size_t reqsize;
435 +
436 + /* Add IP into set (userspace: ipset -A set IP)
437 + * Return -EEXIST if the address is already in the set,
438 + * and -ERANGE if the address lies outside the set bounds.
439 + * If the address was not already in the set, 0 is returned.
440 + */
441 + int (*addip) (struct ip_set *set,
442 + const void *data, size_t size,
443 + ip_set_ip_t *ip);
444 +
445 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
446 + * Return -EEXIST if the address is already in the set,
447 + * and -ERANGE if the address lies outside the set bounds.
448 + * If the address was not already in the set, 0 is returned.
449 + */
450 + int (*addip_kernel) (struct ip_set *set,
451 + const struct sk_buff * skb,
452 + u_int32_t flags,
453 + ip_set_ip_t *ip);
454 +
455 + /* remove IP from set (userspace: ipset -D set --entry x)
456 + * Return -EEXIST if the address is NOT in the set,
457 + * and -ERANGE if the address lies outside the set bounds.
458 + * If the address really was in the set, 0 is returned.
459 + */
460 + int (*delip) (struct ip_set *set,
461 + const void *data, size_t size,
462 + ip_set_ip_t *ip);
463 +
464 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
465 + * Return -EEXIST if the address is NOT in the set,
466 + * and -ERANGE if the address lies outside the set bounds.
467 + * If the address really was in the set, 0 is returned.
468 + */
469 + int (*delip_kernel) (struct ip_set *set,
470 + const struct sk_buff * skb,
471 + u_int32_t flags,
472 + ip_set_ip_t *ip);
473 +
474 + /* new set creation - allocated type specific items
475 + */
476 + int (*create) (struct ip_set *set,
477 + const void *data, size_t size);
478 +
479 + /* retry the operation after successfully tweaking the set
480 + */
481 + int (*retry) (struct ip_set *set);
482 +
483 + /* set destruction - free type specific items
484 + * There is no return value.
485 + * Can be called only when child sets are destroyed.
486 + */
487 + void (*destroy) (struct ip_set *set);
488 +
489 + /* set flushing - reset all bits in the set, or something similar.
490 + * There is no return value.
491 + */
492 + void (*flush) (struct ip_set *set);
493 +
494 + /* Listing: size needed for header
495 + */
496 + size_t header_size;
497 +
498 + /* Listing: Get the header
499 + *
500 + * Fill in the information in "data".
501 + * This function is always run after list_header_size() under a
502 + * writelock on the set. Therefor is the length of "data" always
503 + * correct.
504 + */
505 + void (*list_header) (const struct ip_set *set,
506 + void *data);
507 +
508 + /* Listing: Get the size for the set members
509 + */
510 + int (*list_members_size) (const struct ip_set *set);
511 +
512 + /* Listing: Get the set members
513 + *
514 + * Fill in the information in "data".
515 + * This function is always run after list_member_size() under a
516 + * writelock on the set. Therefor is the length of "data" always
517 + * correct.
518 + */
519 + void (*list_members) (const struct ip_set *set,
520 + void *data);
521 +
522 + char typename[IP_SET_MAXNAMELEN];
523 + char typecode;
524 + int protocol_version;
525 +
526 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
527 + struct module *me;
528 +};
529 +
530 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
531 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
532 +
533 +/* A generic ipset */
534 +struct ip_set {
535 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
536 + rwlock_t lock; /* lock for concurrency control */
537 + ip_set_id_t id; /* set id for swapping */
538 + ip_set_id_t binding; /* default binding for the set */
539 + atomic_t ref; /* in kernel and in hash references */
540 + struct ip_set_type *type; /* the set types */
541 + void *data; /* pooltype specific data */
542 +};
543 +
544 +/* Structure to bind set elements to sets */
545 +struct ip_set_hash {
546 + struct list_head list; /* list of clashing entries in hash */
547 + ip_set_ip_t ip; /* ip from set */
548 + ip_set_id_t id; /* set id */
549 + ip_set_id_t binding; /* set we bind the element to */
550 +};
551 +
552 +/* register and unregister set references */
553 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
554 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
555 +extern void ip_set_put(ip_set_id_t id);
556 +
557 +/* API for iptables set match, and SET target */
558 +extern void ip_set_addip_kernel(ip_set_id_t id,
559 + const struct sk_buff *skb,
560 + const u_int32_t *flags);
561 +extern void ip_set_delip_kernel(ip_set_id_t id,
562 + const struct sk_buff *skb,
563 + const u_int32_t *flags);
564 +extern int ip_set_testip_kernel(ip_set_id_t id,
565 + const struct sk_buff *skb,
566 + const u_int32_t *flags);
567 +
568 +#endif /* __KERNEL__ */
569 +
570 +#endif /*_IP_SET_H*/
571 --- /dev/null
572 +++ b/include/linux/netfilter_ipv4/ip_set_iphash.h
573 @@ -0,0 +1,30 @@
574 +#ifndef __IP_SET_IPHASH_H
575 +#define __IP_SET_IPHASH_H
576 +
577 +#include <linux/netfilter_ipv4/ip_set.h>
578 +
579 +#define SETTYPE_NAME "iphash"
580 +#define MAX_RANGE 0x0000FFFF
581 +
582 +struct ip_set_iphash {
583 + ip_set_ip_t *members; /* the iphash proper */
584 + uint32_t initval; /* initval for jhash_1word */
585 + uint32_t prime; /* prime for double hashing */
586 + uint32_t hashsize; /* hash size */
587 + uint16_t probes; /* max number of probes */
588 + uint16_t resize; /* resize factor in percent */
589 + ip_set_ip_t netmask; /* netmask */
590 +};
591 +
592 +struct ip_set_req_iphash_create {
593 + uint32_t hashsize;
594 + uint16_t probes;
595 + uint16_t resize;
596 + ip_set_ip_t netmask;
597 +};
598 +
599 +struct ip_set_req_iphash {
600 + ip_set_ip_t ip;
601 +};
602 +
603 +#endif /* __IP_SET_IPHASH_H */
604 --- /dev/null
605 +++ b/include/linux/netfilter_ipv4/ip_set_ipmap.h
606 @@ -0,0 +1,56 @@
607 +#ifndef __IP_SET_IPMAP_H
608 +#define __IP_SET_IPMAP_H
609 +
610 +#include <linux/netfilter_ipv4/ip_set.h>
611 +
612 +#define SETTYPE_NAME "ipmap"
613 +#define MAX_RANGE 0x0000FFFF
614 +
615 +struct ip_set_ipmap {
616 + void *members; /* the ipmap proper */
617 + ip_set_ip_t first_ip; /* host byte order, included in range */
618 + ip_set_ip_t last_ip; /* host byte order, included in range */
619 + ip_set_ip_t netmask; /* subnet netmask */
620 + ip_set_ip_t sizeid; /* size of set in IPs */
621 + u_int16_t hosts; /* number of hosts in a subnet */
622 +};
623 +
624 +struct ip_set_req_ipmap_create {
625 + ip_set_ip_t from;
626 + ip_set_ip_t to;
627 + ip_set_ip_t netmask;
628 +};
629 +
630 +struct ip_set_req_ipmap {
631 + ip_set_ip_t ip;
632 +};
633 +
634 +unsigned int
635 +mask_to_bits(ip_set_ip_t mask)
636 +{
637 + unsigned int bits = 32;
638 + ip_set_ip_t maskaddr;
639 +
640 + if (mask == 0xFFFFFFFF)
641 + return bits;
642 +
643 + maskaddr = 0xFFFFFFFE;
644 + while (--bits >= 0 && maskaddr != mask)
645 + maskaddr <<= 1;
646 +
647 + return bits;
648 +}
649 +
650 +ip_set_ip_t
651 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
652 +{
653 + ip_set_ip_t mask = 0xFFFFFFFE;
654 +
655 + *bits = 32;
656 + while (--(*bits) >= 0 && mask && (to & mask) != from)
657 + mask <<= 1;
658 +
659 + return mask;
660 +}
661 +
662 +#endif /* __IP_SET_IPMAP_H */
663 --- /dev/null
664 +++ b/include/linux/netfilter_ipv4/ip_set_iptree.h
665 @@ -0,0 +1,39 @@
666 +#ifndef __IP_SET_IPTREE_H
667 +#define __IP_SET_IPTREE_H
668 +
669 +#include <linux/netfilter_ipv4/ip_set.h>
670 +
671 +#define SETTYPE_NAME "iptree"
672 +#define MAX_RANGE 0x0000FFFF
673 +
674 +struct ip_set_iptreed {
675 + unsigned long expires[255]; /* x.x.x.ADDR */
676 +};
677 +
678 +struct ip_set_iptreec {
679 + struct ip_set_iptreed *tree[255]; /* x.x.ADDR.* */
680 +};
681 +
682 +struct ip_set_iptreeb {
683 + struct ip_set_iptreec *tree[255]; /* x.ADDR.*.* */
684 +};
685 +
686 +struct ip_set_iptree {
687 + unsigned int timeout;
688 + unsigned int gc_interval;
689 +#ifdef __KERNEL__
690 + struct timer_list gc;
691 + struct ip_set_iptreeb *tree[255]; /* ADDR.*.*.* */
692 +#endif
693 +};
694 +
695 +struct ip_set_req_iptree_create {
696 + unsigned int timeout;
697 +};
698 +
699 +struct ip_set_req_iptree {
700 + ip_set_ip_t ip;
701 + unsigned int timeout;
702 +};
703 +
704 +#endif /* __IP_SET_IPTREE_H */
705 --- /dev/null
706 +++ b/include/linux/netfilter_ipv4/ip_set_jhash.h
707 @@ -0,0 +1,148 @@
708 +#ifndef _LINUX_IPSET_JHASH_H
709 +#define _LINUX_IPSET_JHASH_H
710 +
711 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
712 + * to __u32/__u8 so that the header file can be included into
713 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
714 + */
715 +
716 +/* jhash.h: Jenkins hash support.
717 + *
718 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
719 + *
720 + * http://burtleburtle.net/bob/hash/
721 + *
722 + * These are the credits from Bob's sources:
723 + *
724 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
725 + * hash(), hash2(), hash3, and mix() are externally useful functions.
726 + * Routines to test the hash are included if SELF_TEST is defined.
727 + * You can use this free for any purpose. It has no warranty.
728 + *
729 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
730 + *
731 + * I've modified Bob's hash to be useful in the Linux kernel, and
732 + * any bugs present are surely my fault. -DaveM
733 + */
734 +
735 +/* NOTE: Arguments are modified. */
736 +#define __jhash_mix(a, b, c) \
737 +{ \
738 + a -= b; a -= c; a ^= (c>>13); \
739 + b -= c; b -= a; b ^= (a<<8); \
740 + c -= a; c -= b; c ^= (b>>13); \
741 + a -= b; a -= c; a ^= (c>>12); \
742 + b -= c; b -= a; b ^= (a<<16); \
743 + c -= a; c -= b; c ^= (b>>5); \
744 + a -= b; a -= c; a ^= (c>>3); \
745 + b -= c; b -= a; b ^= (a<<10); \
746 + c -= a; c -= b; c ^= (b>>15); \
747 +}
748 +
749 +/* The golden ration: an arbitrary value */
750 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
751 +
752 +/* The most generic version, hashes an arbitrary sequence
753 + * of bytes. No alignment or length assumptions are made about
754 + * the input key.
755 + */
756 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
757 +{
758 + __u32 a, b, c, len;
759 + __u8 *k = key;
760 +
761 + len = length;
762 + a = b = JHASH_GOLDEN_RATIO;
763 + c = initval;
764 +
765 + while (len >= 12) {
766 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
767 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
768 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
769 +
770 + __jhash_mix(a,b,c);
771 +
772 + k += 12;
773 + len -= 12;
774 + }
775 +
776 + c += length;
777 + switch (len) {
778 + case 11: c += ((__u32)k[10]<<24);
779 + case 10: c += ((__u32)k[9]<<16);
780 + case 9 : c += ((__u32)k[8]<<8);
781 + case 8 : b += ((__u32)k[7]<<24);
782 + case 7 : b += ((__u32)k[6]<<16);
783 + case 6 : b += ((__u32)k[5]<<8);
784 + case 5 : b += k[4];
785 + case 4 : a += ((__u32)k[3]<<24);
786 + case 3 : a += ((__u32)k[2]<<16);
787 + case 2 : a += ((__u32)k[1]<<8);
788 + case 1 : a += k[0];
789 + };
790 +
791 + __jhash_mix(a,b,c);
792 +
793 + return c;
794 +}
795 +
796 +/* A special optimized version that handles 1 or more of __u32s.
797 + * The length parameter here is the number of __u32s in the key.
798 + */
799 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
800 +{
801 + __u32 a, b, c, len;
802 +
803 + a = b = JHASH_GOLDEN_RATIO;
804 + c = initval;
805 + len = length;
806 +
807 + while (len >= 3) {
808 + a += k[0];
809 + b += k[1];
810 + c += k[2];
811 + __jhash_mix(a, b, c);
812 + k += 3; len -= 3;
813 + }
814 +
815 + c += length * 4;
816 +
817 + switch (len) {
818 + case 2 : b += k[1];
819 + case 1 : a += k[0];
820 + };
821 +
822 + __jhash_mix(a,b,c);
823 +
824 + return c;
825 +}
826 +
827 +
828 +/* A special ultra-optimized versions that knows they are hashing exactly
829 + * 3, 2 or 1 word(s).
830 + *
831 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
832 + * done at the end is not done here.
833 + */
834 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
835 +{
836 + a += JHASH_GOLDEN_RATIO;
837 + b += JHASH_GOLDEN_RATIO;
838 + c += initval;
839 +
840 + __jhash_mix(a, b, c);
841 +
842 + return c;
843 +}
844 +
845 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
846 +{
847 + return jhash_3words(a, b, 0, initval);
848 +}
849 +
850 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
851 +{
852 + return jhash_3words(a, 0, 0, initval);
853 +}
854 +
855 +#endif /* _LINUX_IPSET_JHASH_H */
856 --- /dev/null
857 +++ b/include/linux/netfilter_ipv4/ip_set_macipmap.h
858 @@ -0,0 +1,38 @@
859 +#ifndef __IP_SET_MACIPMAP_H
860 +#define __IP_SET_MACIPMAP_H
861 +
862 +#include <linux/netfilter_ipv4/ip_set.h>
863 +
864 +#define SETTYPE_NAME "macipmap"
865 +#define MAX_RANGE 0x0000FFFF
866 +
867 +/* general flags */
868 +#define IPSET_MACIP_MATCHUNSET 1
869 +
870 +/* per ip flags */
871 +#define IPSET_MACIP_ISSET 1
872 +
873 +struct ip_set_macipmap {
874 + void *members; /* the macipmap proper */
875 + ip_set_ip_t first_ip; /* host byte order, included in range */
876 + ip_set_ip_t last_ip; /* host byte order, included in range */
877 + u_int32_t flags;
878 +};
879 +
880 +struct ip_set_req_macipmap_create {
881 + ip_set_ip_t from;
882 + ip_set_ip_t to;
883 + u_int32_t flags;
884 +};
885 +
886 +struct ip_set_req_macipmap {
887 + ip_set_ip_t ip;
888 + unsigned char ethernet[ETH_ALEN];
889 +};
890 +
891 +struct ip_set_macip {
892 + unsigned short flags;
893 + unsigned char ethernet[ETH_ALEN];
894 +};
895 +
896 +#endif /* __IP_SET_MACIPMAP_H */
897 --- /dev/null
898 +++ b/include/linux/netfilter_ipv4/ip_set_malloc.h
899 @@ -0,0 +1,27 @@
900 +#ifndef _IP_SET_MALLOC_H
901 +#define _IP_SET_MALLOC_H
902 +
903 +#ifdef __KERNEL__
904 +
905 +/* Memory allocation and deallocation */
906 +static size_t max_malloc_size = 131072; /* Guaranteed: slab.c */
907 +
908 +static inline void * ip_set_malloc(size_t bytes)
909 +{
910 + if (bytes > max_malloc_size)
911 + return vmalloc(bytes);
912 + else
913 + return kmalloc(bytes, GFP_KERNEL);
914 +}
915 +
916 +static inline void ip_set_free(void * data, size_t bytes)
917 +{
918 + if (bytes > max_malloc_size)
919 + vfree(data);
920 + else
921 + kfree(data);
922 +}
923 +
924 +#endif /* __KERNEL__ */
925 +
926 +#endif /*_IP_SET_MALLOC_H*/
927 --- /dev/null
928 +++ b/include/linux/netfilter_ipv4/ip_set_nethash.h
929 @@ -0,0 +1,55 @@
930 +#ifndef __IP_SET_NETHASH_H
931 +#define __IP_SET_NETHASH_H
932 +
933 +#include <linux/netfilter_ipv4/ip_set.h>
934 +
935 +#define SETTYPE_NAME "nethash"
936 +#define MAX_RANGE 0x0000FFFF
937 +
938 +struct ip_set_nethash {
939 + ip_set_ip_t *members; /* the nethash proper */
940 + uint32_t initval; /* initval for jhash_1word */
941 + uint32_t prime; /* prime for double hashing */
942 + uint32_t hashsize; /* hash size */
943 + uint16_t probes; /* max number of probes */
944 + uint16_t resize; /* resize factor in percent */
945 + unsigned char cidr[30]; /* CIDR sizes */
946 +};
947 +
948 +struct ip_set_req_nethash_create {
949 + uint32_t hashsize;
950 + uint16_t probes;
951 + uint16_t resize;
952 +};
953 +
954 +struct ip_set_req_nethash {
955 + ip_set_ip_t ip;
956 + unsigned char cidr;
957 +};
958 +
959 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
960 +
961 +static inline ip_set_ip_t
962 +pack(ip_set_ip_t ip, unsigned char cidr)
963 +{
964 + ip_set_ip_t addr, *paddr = &addr;
965 + unsigned char n, t, *a;
966 +
967 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
968 +#ifdef __KERNEL__
969 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
970 +#endif
971 + n = cidr / 8;
972 + t = cidr % 8;
973 + a = &((unsigned char *)paddr)[n];
974 + *a = *a /(1 << (8 - t)) + shifts[t];
975 +#ifdef __KERNEL__
976 + DP("n: %u, t: %u, a: %u", n, t, *a);
977 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
978 + HIPQUAD(ip), cidr, NIPQUAD(addr));
979 +#endif
980 +
981 + return ntohl(addr);
982 +}
983 +
984 +#endif /* __IP_SET_NETHASH_H */
985 --- /dev/null
986 +++ b/include/linux/netfilter_ipv4/ip_set_portmap.h
987 @@ -0,0 +1,25 @@
988 +#ifndef __IP_SET_PORTMAP_H
989 +#define __IP_SET_PORTMAP_H
990 +
991 +#include <linux/netfilter_ipv4/ip_set.h>
992 +
993 +#define SETTYPE_NAME "portmap"
994 +#define MAX_RANGE 0x0000FFFF
995 +#define INVALID_PORT (MAX_RANGE + 1)
996 +
997 +struct ip_set_portmap {
998 + void *members; /* the portmap proper */
999 + ip_set_ip_t first_port; /* host byte order, included in range */
1000 + ip_set_ip_t last_port; /* host byte order, included in range */
1001 +};
1002 +
1003 +struct ip_set_req_portmap_create {
1004 + ip_set_ip_t from;
1005 + ip_set_ip_t to;
1006 +};
1007 +
1008 +struct ip_set_req_portmap {
1009 + ip_set_ip_t port;
1010 +};
1011 +
1012 +#endif /* __IP_SET_PORTMAP_H */
1013 --- /dev/null
1014 +++ b/include/linux/netfilter_ipv4/ip_set_prime.h
1015 @@ -0,0 +1,34 @@
1016 +#ifndef __IP_SET_PRIME_H
1017 +#define __IP_SET_PRIME_H
1018 +
1019 +static inline unsigned make_prime_bound(unsigned nr)
1020 +{
1021 + unsigned long long nr64 = nr;
1022 + unsigned long long x = 1;
1023 + nr = 1;
1024 + while (x <= nr64) { x <<= 2; nr <<= 1; }
1025 + return nr;
1026 +}
1027 +
1028 +static inline int make_prime_check(unsigned nr)
1029 +{
1030 + unsigned x = 3;
1031 + unsigned b = make_prime_bound(nr);
1032 + while (x <= b) {
1033 + if (0 == (nr % x)) return 0;
1034 + x += 2;
1035 + }
1036 + return 1;
1037 +}
1038 +
1039 +static unsigned make_prime(unsigned nr)
1040 +{
1041 + if (0 == (nr & 1)) nr--;
1042 + while (nr > 1) {
1043 + if (make_prime_check(nr)) return nr;
1044 + nr -= 2;
1045 + }
1046 + return 2;
1047 +}
1048 +
1049 +#endif /* __IP_SET_PRIME_H */
1050 --- /dev/null
1051 +++ b/include/linux/netfilter_ipv4/ipt_set.h
1052 @@ -0,0 +1,21 @@
1053 +#ifndef _IPT_SET_H
1054 +#define _IPT_SET_H
1055 +
1056 +#include <linux/netfilter_ipv4/ip_set.h>
1057 +
1058 +struct ipt_set_info {
1059 + ip_set_id_t index;
1060 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1061 +};
1062 +
1063 +/* match info */
1064 +struct ipt_set_info_match {
1065 + struct ipt_set_info match_set;
1066 +};
1067 +
1068 +struct ipt_set_info_target {
1069 + struct ipt_set_info add_set;
1070 + struct ipt_set_info del_set;
1071 +};
1072 +
1073 +#endif /*_IPT_SET_H*/
1074 --- a/net/ipv4/netfilter/Config.in
1075 +++ b/net/ipv4/netfilter/Config.in
1076 @@ -20,6 +20,20 @@ tristate 'IP tables support (required fo
1077 if [ "$CONFIG_IP_NF_IPTABLES" != "n" ]; then
1078 # The simple matches.
1079 dep_tristate ' limit match support' CONFIG_IP_NF_MATCH_LIMIT $CONFIG_IP_NF_IPTABLES
1080 +
1081 + dep_tristate ' IP set support' CONFIG_IP_NF_SET $CONFIG_IP_NF_IPTABLES
1082 + if [ "$CONFIG_IP_NF_SET" != "n" ]; then
1083 + int ' Maximum number of sets' CONFIG_IP_NF_SET_MAX 256
1084 + int ' Hash size for bindings of IP sets' CONFIG_IP_NF_SET_HASHSIZE 1024
1085 + dep_tristate ' set match support' CONFIG_IP_NF_MATCH_SET $CONFIG_IP_NF_SET
1086 + dep_tristate ' SET target support' CONFIG_IP_NF_TARGET_SET $CONFIG_IP_NF_SET
1087 + dep_tristate ' ipmap set type support' CONFIG_IP_NF_SET_IPMAP $CONFIG_IP_NF_SET
1088 + dep_tristate ' portmap set type support' CONFIG_IP_NF_SET_PORTMAP $CONFIG_IP_NF_SET
1089 + dep_tristate ' macipmap set type support' CONFIG_IP_NF_SET_MACIPMAP $CONFIG_IP_NF_SET
1090 + dep_tristate ' iphash set type support' CONFIG_IP_NF_SET_IPHASH $CONFIG_IP_NF_SET
1091 + dep_tristate ' nethash set type support' CONFIG_IP_NF_SET_NETHASH $CONFIG_IP_NF_SET
1092 + dep_tristate ' iptree set type support' CONFIG_IP_NF_SET_IPTREE $CONFIG_IP_NF_SET
1093 + fi
1094 dep_tristate ' MAC address match support' CONFIG_IP_NF_MATCH_MAC $CONFIG_IP_NF_IPTABLES
1095 dep_tristate ' Packet type match support' CONFIG_IP_NF_MATCH_PKTTYPE $CONFIG_IP_NF_IPTABLES
1096 dep_tristate ' netfilter MARK match support' CONFIG_IP_NF_MATCH_MARK $CONFIG_IP_NF_IPTABLES
1097 --- /dev/null
1098 +++ b/net/ipv4/netfilter/ip_set.c
1099 @@ -0,0 +1,2002 @@
1100 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1101 + * Patrick Schaaf <bof@bof.de>
1102 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1103 + *
1104 + * This program is free software; you can redistribute it and/or modify
1105 + * it under the terms of the GNU General Public License version 2 as
1106 + * published by the Free Software Foundation.
1107 + */
1108 +
1109 +/* Kernel module for IP set management */
1110 +
1111 +#include <linux/config.h>
1112 +#include <linux/module.h>
1113 +#include <linux/kmod.h>
1114 +#include <linux/ip.h>
1115 +#include <linux/skbuff.h>
1116 +#include <linux/random.h>
1117 +#include <linux/jhash.h>
1118 +#include <linux/netfilter_ipv4/ip_tables.h>
1119 +#include <linux/errno.h>
1120 +#include <asm/uaccess.h>
1121 +#include <asm/bitops.h>
1122 +#include <asm/softirq.h>
1123 +#include <asm/semaphore.h>
1124 +#include <linux/spinlock.h>
1125 +#include <linux/vmalloc.h>
1126 +
1127 +#define ASSERT_READ_LOCK(x) /* dont use that */
1128 +#define ASSERT_WRITE_LOCK(x)
1129 +#include <linux/netfilter_ipv4/listhelp.h>
1130 +#include <linux/netfilter_ipv4/ip_set.h>
1131 +
1132 +static struct list_head set_type_list; /* all registered sets */
1133 +static struct ip_set **ip_set_list; /* all individual sets */
1134 +static DECLARE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1135 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1136 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1137 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1138 +static struct list_head *ip_set_hash; /* hash of bindings */
1139 +static unsigned int ip_set_hash_random; /* random seed */
1140 +
1141 +/* Arrgh */
1142 +#ifdef MODULE
1143 +#define __MOD_INC(foo) __MOD_INC_USE_COUNT(foo)
1144 +#define __MOD_DEC(foo) __MOD_DEC_USE_COUNT(foo)
1145 +#define __MOD_INC_SELF MOD_INC_USE_COUNT
1146 +#define __MOD_DEC_SELF MOD_DEC_USE_COUNT
1147 +#else
1148 +#define __MOD_INC(foo)
1149 +#define __MOD_DEC(foo)
1150 +#define __MOD_INC_SELF
1151 +#define __MOD_DEC_SELF
1152 +#endif
1153 +
1154 +/*
1155 + * Sets are identified either by the index in ip_set_list or by id.
1156 + * The id never changes and is used to find a key in the hash.
1157 + * The index may change by swapping and used at all other places
1158 + * (set/SET netfilter modules, binding value, etc.)
1159 + *
1160 + * Userspace requests are serialized by ip_set_mutex and sets can
1161 + * be deleted only from userspace. Therefore ip_set_list locking
1162 + * must obey the following rules:
1163 + *
1164 + * - kernel requests: read and write locking mandatory
1165 + * - user requests: read locking optional, write locking mandatory
1166 + */
1167 +
1168 +static inline void
1169 +__ip_set_get(ip_set_id_t index)
1170 +{
1171 + atomic_inc(&ip_set_list[index]->ref);
1172 +}
1173 +
1174 +static inline void
1175 +__ip_set_put(ip_set_id_t index)
1176 +{
1177 + atomic_dec(&ip_set_list[index]->ref);
1178 +}
1179 +
1180 +/*
1181 + * Binding routines
1182 + */
1183 +
1184 +static inline int
1185 +ip_hash_cmp(const struct ip_set_hash *set_hash,
1186 + ip_set_id_t id, ip_set_ip_t ip)
1187 +{
1188 + return set_hash->id == id && set_hash->ip == ip;
1189 +}
1190 +
1191 +static ip_set_id_t
1192 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1193 +{
1194 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1195 + % ip_set_bindings_hash_size;
1196 + struct ip_set_hash *set_hash;
1197 +
1198 + MUST_BE_READ_LOCKED(&ip_set_lock);
1199 + IP_SET_ASSERT(ip_set_list[id]);
1200 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1201 +
1202 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1203 + struct ip_set_hash *, id, ip);
1204 +
1205 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1206 + HIPQUAD(ip),
1207 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1208 +
1209 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1210 +}
1211 +
1212 +static inline void
1213 +__set_hash_del(struct ip_set_hash *set_hash)
1214 +{
1215 + MUST_BE_WRITE_LOCKED(&ip_set_lock);
1216 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1217 +
1218 + __ip_set_put(set_hash->binding);
1219 + list_del(&set_hash->list);
1220 + kfree(set_hash);
1221 +}
1222 +
1223 +static int
1224 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1225 +{
1226 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1227 + % ip_set_bindings_hash_size;
1228 + struct ip_set_hash *set_hash;
1229 +
1230 + IP_SET_ASSERT(ip_set_list[id]);
1231 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1232 + WRITE_LOCK(&ip_set_lock);
1233 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1234 + struct ip_set_hash *, id, ip);
1235 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1236 + HIPQUAD(ip),
1237 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1238 +
1239 + if (set_hash != NULL)
1240 + __set_hash_del(set_hash);
1241 + WRITE_UNLOCK(&ip_set_lock);
1242 + return 0;
1243 +}
1244 +
1245 +static int
1246 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1247 +{
1248 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1249 + % ip_set_bindings_hash_size;
1250 + struct ip_set_hash *set_hash;
1251 + int ret = 0;
1252 +
1253 + IP_SET_ASSERT(ip_set_list[id]);
1254 + IP_SET_ASSERT(ip_set_list[binding]);
1255 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1256 + HIPQUAD(ip), ip_set_list[binding]->name);
1257 + WRITE_LOCK(&ip_set_lock);
1258 + set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
1259 + struct ip_set_hash *, id, ip);
1260 + if (!set_hash) {
1261 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_KERNEL);
1262 + if (!set_hash) {
1263 + ret = -ENOMEM;
1264 + goto unlock;
1265 + }
1266 + INIT_LIST_HEAD(&set_hash->list);
1267 + set_hash->id = id;
1268 + set_hash->ip = ip;
1269 + list_add(&ip_set_hash[key], &set_hash->list);
1270 + } else {
1271 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1272 + DP("overwrite binding: %s",
1273 + ip_set_list[set_hash->binding]->name);
1274 + __ip_set_put(set_hash->binding);
1275 + }
1276 + set_hash->binding = binding;
1277 + __ip_set_get(set_hash->binding);
1278 + unlock:
1279 + WRITE_UNLOCK(&ip_set_lock);
1280 + return ret;
1281 +}
1282 +
1283 +#define FOREACH_HASH_DO(fn, args...) \
1284 +({ \
1285 + ip_set_id_t __key; \
1286 + struct ip_set_hash *__set_hash; \
1287 + \
1288 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1289 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1290 + fn(__set_hash , ## args); \
1291 + } \
1292 +})
1293 +
1294 +#define FOREACH_HASH_RW_DO(fn, args...) \
1295 +({ \
1296 + ip_set_id_t __key; \
1297 + struct ip_set_hash *__set_hash, *__n; \
1298 + \
1299 + MUST_BE_WRITE_LOCKED(&ip_set_lock); \
1300 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1301 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1302 + fn(__set_hash , ## args); \
1303 + } \
1304 +})
1305 +
1306 +/* Add, del and test set entries from kernel */
1307 +
1308 +#define follow_bindings(index, set, ip) \
1309 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1310 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1311 +
1312 +int
1313 +ip_set_testip_kernel(ip_set_id_t index,
1314 + const struct sk_buff *skb,
1315 + const u_int32_t *flags)
1316 +{
1317 + struct ip_set *set;
1318 + ip_set_ip_t ip;
1319 + int res, i = 0;
1320 +
1321 + IP_SET_ASSERT(flags[i]);
1322 + READ_LOCK(&ip_set_lock);
1323 + do {
1324 + set = ip_set_list[index];
1325 + IP_SET_ASSERT(set);
1326 + DP("set %s, index %u", set->name, index);
1327 + read_lock_bh(&set->lock);
1328 + res = set->type->testip_kernel(set, skb, flags[i], &ip);
1329 + read_unlock_bh(&set->lock);
1330 + } while (res > 0
1331 + && flags[++i]
1332 + && follow_bindings(index, set, ip));
1333 + READ_UNLOCK(&ip_set_lock);
1334 +
1335 + return res;
1336 +}
1337 +
1338 +void
1339 +ip_set_addip_kernel(ip_set_id_t index,
1340 + const struct sk_buff *skb,
1341 + const u_int32_t *flags)
1342 +{
1343 + struct ip_set *set;
1344 + ip_set_ip_t ip;
1345 + int res, i= 0;
1346 +
1347 + IP_SET_ASSERT(flags[i]);
1348 + retry:
1349 + READ_LOCK(&ip_set_lock);
1350 + do {
1351 + set = ip_set_list[index];
1352 + IP_SET_ASSERT(set);
1353 + DP("set %s, index %u", set->name, index);
1354 + write_lock_bh(&set->lock);
1355 + res = set->type->addip_kernel(set, skb, flags[i], &ip);
1356 + write_unlock_bh(&set->lock);
1357 + } while ((res == 0 || res == -EEXIST)
1358 + && flags[++i]
1359 + && follow_bindings(index, set, ip));
1360 + READ_UNLOCK(&ip_set_lock);
1361 +
1362 + if (res == -EAGAIN
1363 + && set->type->retry
1364 + && (res = set->type->retry(set)) == 0)
1365 + goto retry;
1366 +}
1367 +
1368 +void
1369 +ip_set_delip_kernel(ip_set_id_t index,
1370 + const struct sk_buff *skb,
1371 + const u_int32_t *flags)
1372 +{
1373 + struct ip_set *set;
1374 + ip_set_ip_t ip;
1375 + int res, i = 0;
1376 +
1377 + IP_SET_ASSERT(flags[i]);
1378 + READ_LOCK(&ip_set_lock);
1379 + do {
1380 + set = ip_set_list[index];
1381 + IP_SET_ASSERT(set);
1382 + DP("set %s, index %u", set->name, index);
1383 + write_lock_bh(&set->lock);
1384 + res = set->type->delip_kernel(set, skb, flags[i], &ip);
1385 + write_unlock_bh(&set->lock);
1386 + } while ((res == 0 || res == -EEXIST)
1387 + && flags[++i]
1388 + && follow_bindings(index, set, ip));
1389 + READ_UNLOCK(&ip_set_lock);
1390 +}
1391 +
1392 +/* Register and deregister settype */
1393 +
1394 +static inline int
1395 +set_type_equal(const struct ip_set_type *set_type, const char *str2)
1396 +{
1397 + return !strncmp(set_type->typename, str2, IP_SET_MAXNAMELEN - 1);
1398 +}
1399 +
1400 +static inline struct ip_set_type *
1401 +find_set_type(const char *name)
1402 +{
1403 + return LIST_FIND(&set_type_list,
1404 + set_type_equal,
1405 + struct ip_set_type *,
1406 + name);
1407 +}
1408 +
1409 +int
1410 +ip_set_register_set_type(struct ip_set_type *set_type)
1411 +{
1412 + int ret = 0;
1413 +
1414 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1415 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1416 + set_type->typename,
1417 + set_type->protocol_version,
1418 + IP_SET_PROTOCOL_VERSION);
1419 + return -EINVAL;
1420 + }
1421 +
1422 + WRITE_LOCK(&ip_set_lock);
1423 + if (find_set_type(set_type->typename)) {
1424 + /* Duplicate! */
1425 + ip_set_printk("'%s' already registered!",
1426 + set_type->typename);
1427 + ret = -EINVAL;
1428 + goto unlock;
1429 + }
1430 + __MOD_INC_SELF;
1431 + list_append(&set_type_list, set_type);
1432 + DP("'%s' registered.", set_type->typename);
1433 + unlock:
1434 + WRITE_UNLOCK(&ip_set_lock);
1435 + return ret;
1436 +}
1437 +
1438 +void
1439 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1440 +{
1441 + WRITE_LOCK(&ip_set_lock);
1442 + if (!find_set_type(set_type->typename)) {
1443 + ip_set_printk("'%s' not registered?",
1444 + set_type->typename);
1445 + goto unlock;
1446 + }
1447 + LIST_DELETE(&set_type_list, set_type);
1448 + __MOD_DEC_SELF;
1449 + DP("'%s' unregistered.", set_type->typename);
1450 + unlock:
1451 + WRITE_UNLOCK(&ip_set_lock);
1452 +
1453 +}
1454 +
1455 +/*
1456 + * Userspace routines
1457 + */
1458 +
1459 +/*
1460 + * Find set by name, reference it once. The reference makes sure the
1461 + * thing pointed to, does not go away under our feet. Drop the reference
1462 + * later, using ip_set_put().
1463 + */
1464 +ip_set_id_t
1465 +ip_set_get_byname(const char *name)
1466 +{
1467 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1468 +
1469 + down(&ip_set_app_mutex);
1470 + for (i = 0; i < ip_set_max; i++) {
1471 + if (ip_set_list[i] != NULL
1472 + && strcmp(ip_set_list[i]->name, name) == 0) {
1473 + __ip_set_get(i);
1474 + index = i;
1475 + break;
1476 + }
1477 + }
1478 + up(&ip_set_app_mutex);
1479 + return index;
1480 +}
1481 +
1482 +/*
1483 + * Find set by index, reference it once. The reference makes sure the
1484 + * thing pointed to, does not go away under our feet. Drop the reference
1485 + * later, using ip_set_put().
1486 + */
1487 +ip_set_id_t
1488 +ip_set_get_byindex(ip_set_id_t index)
1489 +{
1490 + down(&ip_set_app_mutex);
1491 +
1492 + if (index >= ip_set_max)
1493 + return IP_SET_INVALID_ID;
1494 +
1495 + if (ip_set_list[index])
1496 + __ip_set_get(index);
1497 + else
1498 + index = IP_SET_INVALID_ID;
1499 +
1500 + up(&ip_set_app_mutex);
1501 + return index;
1502 +}
1503 +
1504 +/*
1505 + * If the given set pointer points to a valid set, decrement
1506 + * reference count by 1. The caller shall not assume the index
1507 + * to be valid, after calling this function.
1508 + */
1509 +void ip_set_put(ip_set_id_t index)
1510 +{
1511 + down(&ip_set_app_mutex);
1512 + if (ip_set_list[index])
1513 + __ip_set_put(index);
1514 + up(&ip_set_app_mutex);
1515 +}
1516 +
1517 +/* Find a set by name or index */
1518 +static ip_set_id_t
1519 +ip_set_find_byname(const char *name)
1520 +{
1521 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1522 +
1523 + for (i = 0; i < ip_set_max; i++) {
1524 + if (ip_set_list[i] != NULL
1525 + && strcmp(ip_set_list[i]->name, name) == 0) {
1526 + index = i;
1527 + break;
1528 + }
1529 + }
1530 + return index;
1531 +}
1532 +
1533 +static ip_set_id_t
1534 +ip_set_find_byindex(ip_set_id_t index)
1535 +{
1536 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1537 + index = IP_SET_INVALID_ID;
1538 +
1539 + return index;
1540 +}
1541 +
1542 +/*
1543 + * Add, del, test, bind and unbind
1544 + */
1545 +
1546 +static inline int
1547 +__ip_set_testip(struct ip_set *set,
1548 + const void *data,
1549 + size_t size,
1550 + ip_set_ip_t *ip)
1551 +{
1552 + int res;
1553 +
1554 + read_lock_bh(&set->lock);
1555 + res = set->type->testip(set, data, size, ip);
1556 + read_unlock_bh(&set->lock);
1557 +
1558 + return res;
1559 +}
1560 +
1561 +static int
1562 +__ip_set_addip(ip_set_id_t index,
1563 + const void *data,
1564 + size_t size)
1565 +{
1566 + struct ip_set *set = ip_set_list[index];
1567 + ip_set_ip_t ip;
1568 + int res;
1569 +
1570 + IP_SET_ASSERT(set);
1571 + do {
1572 + write_lock_bh(&set->lock);
1573 + res = set->type->addip(set, data, size, &ip);
1574 + write_unlock_bh(&set->lock);
1575 + } while (res == -EAGAIN
1576 + && set->type->retry
1577 + && (res = set->type->retry(set)) == 0);
1578 +
1579 + return res;
1580 +}
1581 +
1582 +static int
1583 +ip_set_addip(ip_set_id_t index,
1584 + const void *data,
1585 + size_t size)
1586 +{
1587 +
1588 + return __ip_set_addip(index,
1589 + data + sizeof(struct ip_set_req_adt),
1590 + size - sizeof(struct ip_set_req_adt));
1591 +}
1592 +
1593 +static int
1594 +ip_set_delip(ip_set_id_t index,
1595 + const void *data,
1596 + size_t size)
1597 +{
1598 + struct ip_set *set = ip_set_list[index];
1599 + ip_set_ip_t ip;
1600 + int res;
1601 +
1602 + IP_SET_ASSERT(set);
1603 + write_lock_bh(&set->lock);
1604 + res = set->type->delip(set,
1605 + data + sizeof(struct ip_set_req_adt),
1606 + size - sizeof(struct ip_set_req_adt),
1607 + &ip);
1608 + write_unlock_bh(&set->lock);
1609 +
1610 + return res;
1611 +}
1612 +
1613 +static int
1614 +ip_set_testip(ip_set_id_t index,
1615 + const void *data,
1616 + size_t size)
1617 +{
1618 + struct ip_set *set = ip_set_list[index];
1619 + ip_set_ip_t ip;
1620 + int res;
1621 +
1622 + IP_SET_ASSERT(set);
1623 + res = __ip_set_testip(set,
1624 + data + sizeof(struct ip_set_req_adt),
1625 + size - sizeof(struct ip_set_req_adt),
1626 + &ip);
1627 +
1628 + return (res > 0 ? -EEXIST : res);
1629 +}
1630 +
1631 +static int
1632 +ip_set_bindip(ip_set_id_t index,
1633 + const void *data,
1634 + size_t size)
1635 +{
1636 + struct ip_set *set = ip_set_list[index];
1637 + struct ip_set_req_bind *req_bind;
1638 + ip_set_id_t binding;
1639 + ip_set_ip_t ip;
1640 + int res;
1641 +
1642 + IP_SET_ASSERT(set);
1643 + if (size < sizeof(struct ip_set_req_bind))
1644 + return -EINVAL;
1645 +
1646 + req_bind = (struct ip_set_req_bind *) data;
1647 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1648 +
1649 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1650 + /* Default binding of a set */
1651 + char *binding_name;
1652 +
1653 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1654 + return -EINVAL;
1655 +
1656 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1657 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1658 +
1659 + binding = ip_set_find_byname(binding_name);
1660 + if (binding == IP_SET_INVALID_ID)
1661 + return -ENOENT;
1662 +
1663 + WRITE_LOCK(&ip_set_lock);
1664 + /* Sets as binding values are referenced */
1665 + if (set->binding != IP_SET_INVALID_ID)
1666 + __ip_set_put(set->binding);
1667 + set->binding = binding;
1668 + __ip_set_get(set->binding);
1669 + WRITE_UNLOCK(&ip_set_lock);
1670 +
1671 + return 0;
1672 + }
1673 + binding = ip_set_find_byname(req_bind->binding);
1674 + if (binding == IP_SET_INVALID_ID)
1675 + return -ENOENT;
1676 +
1677 + res = __ip_set_testip(set,
1678 + data + sizeof(struct ip_set_req_bind),
1679 + size - sizeof(struct ip_set_req_bind),
1680 + &ip);
1681 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1682 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1683 +
1684 + if (res >= 0)
1685 + res = ip_set_hash_add(set->id, ip, binding);
1686 +
1687 + return res;
1688 +}
1689 +
1690 +#define FOREACH_SET_DO(fn, args...) \
1691 +({ \
1692 + ip_set_id_t __i; \
1693 + struct ip_set *__set; \
1694 + \
1695 + for (__i = 0; __i < ip_set_max; __i++) { \
1696 + __set = ip_set_list[__i]; \
1697 + if (__set != NULL) \
1698 + fn(__set , ##args); \
1699 + } \
1700 +})
1701 +
1702 +static inline void
1703 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1704 +{
1705 + if (set_hash->id == id)
1706 + __set_hash_del(set_hash);
1707 +}
1708 +
1709 +static inline void
1710 +__unbind_default(struct ip_set *set)
1711 +{
1712 + if (set->binding != IP_SET_INVALID_ID) {
1713 + /* Sets as binding values are referenced */
1714 + __ip_set_put(set->binding);
1715 + set->binding = IP_SET_INVALID_ID;
1716 + }
1717 +}
1718 +
1719 +static int
1720 +ip_set_unbindip(ip_set_id_t index,
1721 + const void *data,
1722 + size_t size)
1723 +{
1724 + struct ip_set *set;
1725 + struct ip_set_req_bind *req_bind;
1726 + ip_set_ip_t ip;
1727 + int res;
1728 +
1729 + DP("");
1730 + if (size < sizeof(struct ip_set_req_bind))
1731 + return -EINVAL;
1732 +
1733 + req_bind = (struct ip_set_req_bind *) data;
1734 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1735 +
1736 + DP("%u %s", index, req_bind->binding);
1737 + if (index == IP_SET_INVALID_ID) {
1738 + /* unbind :all: */
1739 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1740 + /* Default binding of sets */
1741 + WRITE_LOCK(&ip_set_lock);
1742 + FOREACH_SET_DO(__unbind_default);
1743 + WRITE_UNLOCK(&ip_set_lock);
1744 + return 0;
1745 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1746 + /* Flush all bindings of all sets*/
1747 + WRITE_LOCK(&ip_set_lock);
1748 + FOREACH_HASH_RW_DO(__set_hash_del);
1749 + WRITE_UNLOCK(&ip_set_lock);
1750 + return 0;
1751 + }
1752 + DP("unreachable reached!");
1753 + return -EINVAL;
1754 + }
1755 +
1756 + set = ip_set_list[index];
1757 + IP_SET_ASSERT(set);
1758 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1759 + /* Default binding of set */
1760 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1761 +
1762 + if (binding == IP_SET_INVALID_ID)
1763 + return -ENOENT;
1764 +
1765 + WRITE_LOCK(&ip_set_lock);
1766 + /* Sets in hash values are referenced */
1767 + __ip_set_put(set->binding);
1768 + set->binding = IP_SET_INVALID_ID;
1769 + WRITE_UNLOCK(&ip_set_lock);
1770 +
1771 + return 0;
1772 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1773 + /* Flush all bindings */
1774 +
1775 + WRITE_LOCK(&ip_set_lock);
1776 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1777 + WRITE_UNLOCK(&ip_set_lock);
1778 + return 0;
1779 + }
1780 +
1781 + res = __ip_set_testip(set,
1782 + data + sizeof(struct ip_set_req_bind),
1783 + size - sizeof(struct ip_set_req_bind),
1784 + &ip);
1785 +
1786 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1787 + if (res >= 0)
1788 + res = ip_set_hash_del(set->id, ip);
1789 +
1790 + return res;
1791 +}
1792 +
1793 +static int
1794 +ip_set_testbind(ip_set_id_t index,
1795 + const void *data,
1796 + size_t size)
1797 +{
1798 + struct ip_set *set = ip_set_list[index];
1799 + struct ip_set_req_bind *req_bind;
1800 + ip_set_id_t binding;
1801 + ip_set_ip_t ip;
1802 + int res;
1803 +
1804 + IP_SET_ASSERT(set);
1805 + if (size < sizeof(struct ip_set_req_bind))
1806 + return -EINVAL;
1807 +
1808 + req_bind = (struct ip_set_req_bind *) data;
1809 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1810 +
1811 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1812 + /* Default binding of set */
1813 + char *binding_name;
1814 +
1815 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1816 + return -EINVAL;
1817 +
1818 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1819 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1820 +
1821 + binding = ip_set_find_byname(binding_name);
1822 + if (binding == IP_SET_INVALID_ID)
1823 + return -ENOENT;
1824 +
1825 + res = (set->binding == binding) ? -EEXIST : 0;
1826 +
1827 + return res;
1828 + }
1829 + binding = ip_set_find_byname(req_bind->binding);
1830 + if (binding == IP_SET_INVALID_ID)
1831 + return -ENOENT;
1832 +
1833 +
1834 + res = __ip_set_testip(set,
1835 + data + sizeof(struct ip_set_req_bind),
1836 + size - sizeof(struct ip_set_req_bind),
1837 + &ip);
1838 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1839 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1840 +
1841 + if (res >= 0)
1842 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1843 + ? -EEXIST : 0;
1844 +
1845 + return res;
1846 +}
1847 +
1848 +static struct ip_set_type *
1849 +find_set_type_rlock(const char *typename)
1850 +{
1851 + struct ip_set_type *type;
1852 +
1853 + READ_LOCK(&ip_set_lock);
1854 + type = find_set_type(typename);
1855 + if (type == NULL)
1856 + READ_UNLOCK(&ip_set_lock);
1857 +
1858 + return type;
1859 +}
1860 +
1861 +static int
1862 +find_free_id(const char *name,
1863 + ip_set_id_t *index,
1864 + ip_set_id_t *id)
1865 +{
1866 + ip_set_id_t i;
1867 +
1868 + *id = IP_SET_INVALID_ID;
1869 + for (i = 0; i < ip_set_max; i++) {
1870 + if (ip_set_list[i] == NULL) {
1871 + if (*id == IP_SET_INVALID_ID)
1872 + *id = *index = i;
1873 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1874 + /* Name clash */
1875 + return -EEXIST;
1876 + }
1877 + if (*id == IP_SET_INVALID_ID)
1878 + /* No free slot remained */
1879 + return -ERANGE;
1880 + /* Check that index is usable as id (swapping) */
1881 + check:
1882 + for (i = 0; i < ip_set_max; i++) {
1883 + if (ip_set_list[i] != NULL
1884 + && ip_set_list[i]->id == *id) {
1885 + *id = i;
1886 + goto check;
1887 + }
1888 + }
1889 + return 0;
1890 +}
1891 +
1892 +/*
1893 + * Create a set
1894 + */
1895 +static int
1896 +ip_set_create(const char *name,
1897 + const char *typename,
1898 + ip_set_id_t restore,
1899 + const void *data,
1900 + size_t size)
1901 +{
1902 + struct ip_set *set;
1903 + ip_set_id_t index, id;
1904 + int res = 0;
1905 +
1906 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1907 + /*
1908 + * First, and without any locks, allocate and initialize
1909 + * a normal base set structure.
1910 + */
1911 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1912 + if (!set)
1913 + return -ENOMEM;
1914 + set->lock = RW_LOCK_UNLOCKED;
1915 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1916 + set->binding = IP_SET_INVALID_ID;
1917 + atomic_set(&set->ref, 0);
1918 +
1919 + /*
1920 + * Next, take the &ip_set_lock, check that we know the type,
1921 + * and take a reference on the type, to make sure it
1922 + * stays available while constructing our new set.
1923 + *
1924 + * After referencing the type, we drop the &ip_set_lock,
1925 + * and let the new set construction run without locks.
1926 + */
1927 + set->type = find_set_type_rlock(typename);
1928 + if (set->type == NULL) {
1929 + /* Try loading the module */
1930 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1931 + strcpy(modulename, "ip_set_");
1932 + strcat(modulename, typename);
1933 + DP("try to load %s", modulename);
1934 + request_module(modulename);
1935 + set->type = find_set_type_rlock(typename);
1936 + }
1937 + if (set->type == NULL) {
1938 + ip_set_printk("no set type '%s', set '%s' not created",
1939 + typename, name);
1940 + kfree(set);
1941 + return -ENOENT;
1942 + }
1943 + __MOD_INC(set->type->me);
1944 + READ_UNLOCK(&ip_set_lock);
1945 +
1946 + /*
1947 + * Without holding any locks, create private part.
1948 + */
1949 + res = set->type->create(set, data, size);
1950 + if (res != 0) {
1951 + __MOD_DEC(set->type->me);
1952 + kfree(set);
1953 + return res;
1954 + }
1955 +
1956 + /* BTW, res==0 here. */
1957 +
1958 + /*
1959 + * Here, we have a valid, constructed set. &ip_set_lock again,
1960 + * find free id/index and check that it is not already in
1961 + * ip_set_list.
1962 + */
1963 + WRITE_LOCK(&ip_set_lock);
1964 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
1965 + DP("no free id!");
1966 + goto cleanup;
1967 + }
1968 +
1969 + /* Make sure restore gets the same index */
1970 + if (restore != IP_SET_INVALID_ID && index != restore) {
1971 + DP("Can't restore, sets are screwed up");
1972 + res = -ERANGE;
1973 + goto cleanup;
1974 + }
1975 +
1976 + /*
1977 + * Finally! Add our shiny new set to the list, and be done.
1978 + */
1979 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
1980 + set->id = id;
1981 + ip_set_list[index] = set;
1982 + WRITE_UNLOCK(&ip_set_lock);
1983 + return res;
1984 +
1985 + cleanup:
1986 + WRITE_UNLOCK(&ip_set_lock);
1987 + set->type->destroy(set);
1988 + __MOD_DEC(set->type->me);
1989 + kfree(set);
1990 + return res;
1991 +}
1992 +
1993 +/*
1994 + * Destroy a given existing set
1995 + */
1996 +static void
1997 +ip_set_destroy_set(ip_set_id_t index)
1998 +{
1999 + struct ip_set *set = ip_set_list[index];
2000 +
2001 + IP_SET_ASSERT(set);
2002 + DP("set: %s", set->name);
2003 + WRITE_LOCK(&ip_set_lock);
2004 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2005 + if (set->binding != IP_SET_INVALID_ID)
2006 + __ip_set_put(set->binding);
2007 + ip_set_list[index] = NULL;
2008 + WRITE_UNLOCK(&ip_set_lock);
2009 +
2010 + /* Must call it without holding any lock */
2011 + set->type->destroy(set);
2012 + __MOD_DEC(set->type->me);
2013 + kfree(set);
2014 +}
2015 +
2016 +/*
2017 + * Destroy a set - or all sets
2018 + * Sets must not be referenced/used.
2019 + */
2020 +static int
2021 +ip_set_destroy(ip_set_id_t index)
2022 +{
2023 + ip_set_id_t i;
2024 +
2025 + /* ref modification always protected by the mutex */
2026 + if (index != IP_SET_INVALID_ID) {
2027 + if (atomic_read(&ip_set_list[index]->ref))
2028 + return -EBUSY;
2029 + ip_set_destroy_set(index);
2030 + } else {
2031 + for (i = 0; i < ip_set_max; i++) {
2032 + if (ip_set_list[i] != NULL
2033 + && (atomic_read(&ip_set_list[i]->ref)))
2034 + return -EBUSY;
2035 + }
2036 +
2037 + for (i = 0; i < ip_set_max; i++) {
2038 + if (ip_set_list[i] != NULL)
2039 + ip_set_destroy_set(i);
2040 + }
2041 + }
2042 + return 0;
2043 +}
2044 +
2045 +static void
2046 +ip_set_flush_set(struct ip_set *set)
2047 +{
2048 + DP("set: %s %u", set->name, set->id);
2049 +
2050 + write_lock_bh(&set->lock);
2051 + set->type->flush(set);
2052 + write_unlock_bh(&set->lock);
2053 +}
2054 +
2055 +/*
2056 + * Flush data in a set - or in all sets
2057 + */
2058 +static int
2059 +ip_set_flush(ip_set_id_t index)
2060 +{
2061 + if (index != IP_SET_INVALID_ID) {
2062 + IP_SET_ASSERT(ip_set_list[index]);
2063 + ip_set_flush_set(ip_set_list[index]);
2064 + } else
2065 + FOREACH_SET_DO(ip_set_flush_set);
2066 +
2067 + return 0;
2068 +}
2069 +
2070 +/* Rename a set */
2071 +static int
2072 +ip_set_rename(ip_set_id_t index, const char *name)
2073 +{
2074 + struct ip_set *set = ip_set_list[index];
2075 + ip_set_id_t i;
2076 + int res = 0;
2077 +
2078 + DP("set: %s to %s", set->name, name);
2079 + WRITE_LOCK(&ip_set_lock);
2080 + for (i = 0; i < ip_set_max; i++) {
2081 + if (ip_set_list[i] != NULL
2082 + && strncmp(ip_set_list[i]->name,
2083 + name,
2084 + IP_SET_MAXNAMELEN - 1) == 0) {
2085 + res = -EEXIST;
2086 + goto unlock;
2087 + }
2088 + }
2089 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2090 + unlock:
2091 + WRITE_UNLOCK(&ip_set_lock);
2092 + return res;
2093 +}
2094 +
2095 +/*
2096 + * Swap two sets so that name/index points to the other.
2097 + * References are also swapped.
2098 + */
2099 +static int
2100 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2101 +{
2102 + struct ip_set *from = ip_set_list[from_index];
2103 + struct ip_set *to = ip_set_list[to_index];
2104 + char from_name[IP_SET_MAXNAMELEN];
2105 + u_int32_t from_ref;
2106 +
2107 + DP("set: %s to %s", from->name, to->name);
2108 + /* Type can't be changed. Artifical restriction. */
2109 + if (from->type->typecode != to->type->typecode)
2110 + return -ENOEXEC;
2111 +
2112 + /* No magic here: ref munging protected by the mutex */
2113 + WRITE_LOCK(&ip_set_lock);
2114 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2115 + from_ref = atomic_read(&from->ref);
2116 +
2117 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2118 + atomic_set(&from->ref, atomic_read(&to->ref));
2119 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2120 + atomic_set(&to->ref, from_ref);
2121 +
2122 + ip_set_list[from_index] = to;
2123 + ip_set_list[to_index] = from;
2124 +
2125 + WRITE_UNLOCK(&ip_set_lock);
2126 + return 0;
2127 +}
2128 +
2129 +/*
2130 + * List set data
2131 + */
2132 +
2133 +static inline void
2134 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2135 + ip_set_id_t id, size_t *size)
2136 +{
2137 + if (set_hash->id == id)
2138 + *size += sizeof(struct ip_set_hash_list);
2139 +}
2140 +
2141 +static inline void
2142 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2143 + ip_set_id_t id, size_t *size)
2144 +{
2145 + if (set_hash->id == id)
2146 + *size += sizeof(struct ip_set_hash_save);
2147 +}
2148 +
2149 +static inline void
2150 +__set_hash_bindings(struct ip_set_hash *set_hash,
2151 + ip_set_id_t id, void *data, int *used)
2152 +{
2153 + if (set_hash->id == id) {
2154 + struct ip_set_hash_list *hash_list =
2155 + (struct ip_set_hash_list *)(data + *used);
2156 +
2157 + hash_list->ip = set_hash->ip;
2158 + hash_list->binding = set_hash->binding;
2159 + *used += sizeof(struct ip_set_hash_list);
2160 + }
2161 +}
2162 +
2163 +static int ip_set_list_set(ip_set_id_t index,
2164 + void *data,
2165 + int *used,
2166 + int len)
2167 +{
2168 + struct ip_set *set = ip_set_list[index];
2169 + struct ip_set_list *set_list;
2170 +
2171 + /* Pointer to our header */
2172 + set_list = (struct ip_set_list *) (data + *used);
2173 +
2174 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2175 +
2176 + /* Get and ensure header size */
2177 + if (*used + sizeof(struct ip_set_list) > len)
2178 + goto not_enough_mem;
2179 + *used += sizeof(struct ip_set_list);
2180 +
2181 + read_lock_bh(&set->lock);
2182 + /* Get and ensure set specific header size */
2183 + set_list->header_size = set->type->header_size;
2184 + if (*used + set_list->header_size > len)
2185 + goto unlock_set;
2186 +
2187 + /* Fill in the header */
2188 + set_list->index = index;
2189 + set_list->binding = set->binding;
2190 + set_list->ref = atomic_read(&set->ref);
2191 +
2192 + /* Fill in set spefific header data */
2193 + DP("call list_header");
2194 + set->type->list_header(set, data + *used);
2195 + DP("call list_header, done");
2196 + *used += set_list->header_size;
2197 +
2198 + /* Get and ensure set specific members size */
2199 + DP("call list_members_size");
2200 + set_list->members_size = set->type->list_members_size(set);
2201 + DP("call list_members_size, done");
2202 + if (*used + set_list->members_size > len)
2203 + goto unlock_set;
2204 +
2205 + /* Fill in set spefific members data */
2206 + DP("call list_members");
2207 + set->type->list_members(set, data + *used);
2208 + DP("call list_members, done");
2209 + *used += set_list->members_size;
2210 + read_unlock_bh(&set->lock);
2211 +
2212 + /* Bindings */
2213 +
2214 + /* Get and ensure set specific bindings size */
2215 + set_list->bindings_size = 0;
2216 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2217 + set->id, &set_list->bindings_size);
2218 + if (*used + set_list->bindings_size > len)
2219 + goto not_enough_mem;
2220 +
2221 + /* Fill in set spefific bindings data */
2222 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2223 +
2224 + return 0;
2225 +
2226 + unlock_set:
2227 + read_unlock_bh(&set->lock);
2228 + not_enough_mem:
2229 + DP("not enough mem, try again");
2230 + return -EAGAIN;
2231 +}
2232 +
2233 +/*
2234 + * Save sets
2235 + */
2236 +static int ip_set_save_set(ip_set_id_t index,
2237 + void *data,
2238 + int *used,
2239 + int len)
2240 +{
2241 + struct ip_set *set;
2242 + struct ip_set_save *set_save;
2243 +
2244 + /* Pointer to our header */
2245 + set_save = (struct ip_set_save *) (data + *used);
2246 +
2247 + /* Get and ensure header size */
2248 + if (*used + sizeof(struct ip_set_save) > len)
2249 + goto not_enough_mem;
2250 + *used += sizeof(struct ip_set_save);
2251 +
2252 + set = ip_set_list[index];
2253 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2254 + data, data + *used);
2255 +
2256 + read_lock_bh(&set->lock);
2257 + /* Get and ensure set specific header size */
2258 + set_save->header_size = set->type->header_size;
2259 + if (*used + set_save->header_size > len)
2260 + goto unlock_set;
2261 +
2262 + /* Fill in the header */
2263 + set_save->index = index;
2264 + set_save->binding = set->binding;
2265 +
2266 + /* Fill in set spefific header data */
2267 + set->type->list_header(set, data + *used);
2268 + *used += set_save->header_size;
2269 +
2270 + DP("set header filled: %s, used: %u %p %p", set->name, *used,
2271 + data, data + *used);
2272 + /* Get and ensure set specific members size */
2273 + set_save->members_size = set->type->list_members_size(set);
2274 + if (*used + set_save->members_size > len)
2275 + goto unlock_set;
2276 +
2277 + /* Fill in set spefific members data */
2278 + set->type->list_members(set, data + *used);
2279 + *used += set_save->members_size;
2280 + read_unlock_bh(&set->lock);
2281 + DP("set members filled: %s, used: %u %p %p", set->name, *used,
2282 + data, data + *used);
2283 + return 0;
2284 +
2285 + unlock_set:
2286 + read_unlock_bh(&set->lock);
2287 + not_enough_mem:
2288 + DP("not enough mem, try again");
2289 + return -EAGAIN;
2290 +}
2291 +
2292 +static inline void
2293 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2294 + ip_set_id_t id,
2295 + void *data,
2296 + int *used,
2297 + int len,
2298 + int *res)
2299 +{
2300 + if (*res == 0
2301 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2302 + struct ip_set_hash_save *hash_save =
2303 + (struct ip_set_hash_save *)(data + *used);
2304 + /* Ensure bindings size */
2305 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2306 + *res = -ENOMEM;
2307 + return;
2308 + }
2309 + hash_save->id = set_hash->id;
2310 + hash_save->ip = set_hash->ip;
2311 + hash_save->binding = set_hash->binding;
2312 + *used += sizeof(struct ip_set_hash_save);
2313 + }
2314 +}
2315 +
2316 +static int ip_set_save_bindings(ip_set_id_t index,
2317 + void *data,
2318 + int *used,
2319 + int len)
2320 +{
2321 + int res = 0;
2322 + struct ip_set_save *set_save;
2323 +
2324 + DP("used %u, len %u", *used, len);
2325 + /* Get and ensure header size */
2326 + if (*used + sizeof(struct ip_set_save) > len)
2327 + return -ENOMEM;
2328 +
2329 + /* Marker */
2330 + set_save = (struct ip_set_save *) (data + *used);
2331 + set_save->index = IP_SET_INVALID_ID;
2332 + *used += sizeof(struct ip_set_save);
2333 +
2334 + DP("marker added used %u, len %u", *used, len);
2335 + /* Fill in bindings data */
2336 + if (index != IP_SET_INVALID_ID)
2337 + /* Sets are identified by id in hash */
2338 + index = ip_set_list[index]->id;
2339 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2340 +
2341 + return res;
2342 +}
2343 +
2344 +/*
2345 + * Restore sets
2346 + */
2347 +static int ip_set_restore(void *data,
2348 + int len)
2349 +{
2350 + int res = 0;
2351 + int line = 0, used = 0, members_size;
2352 + struct ip_set *set;
2353 + struct ip_set_hash_save *hash_save;
2354 + struct ip_set_restore *set_restore;
2355 + ip_set_id_t index;
2356 +
2357 + /* Loop to restore sets */
2358 + while (1) {
2359 + line++;
2360 +
2361 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2362 + /* Get and ensure header size */
2363 + if (used + sizeof(struct ip_set_restore) > len)
2364 + return line;
2365 + set_restore = (struct ip_set_restore *) (data + used);
2366 + used += sizeof(struct ip_set_restore);
2367 +
2368 + /* Ensure data size */
2369 + if (used
2370 + + set_restore->header_size
2371 + + set_restore->members_size > len)
2372 + return line;
2373 +
2374 + /* Check marker */
2375 + if (set_restore->index == IP_SET_INVALID_ID) {
2376 + line--;
2377 + goto bindings;
2378 + }
2379 +
2380 + /* Try to create the set */
2381 + DP("restore %s %s", set_restore->name, set_restore->typename);
2382 + res = ip_set_create(set_restore->name,
2383 + set_restore->typename,
2384 + set_restore->index,
2385 + data + used,
2386 + set_restore->header_size);
2387 +
2388 + if (res != 0)
2389 + return line;
2390 + used += set_restore->header_size;
2391 +
2392 + index = ip_set_find_byindex(set_restore->index);
2393 + DP("index %u, restore_index %u", index, set_restore->index);
2394 + if (index != set_restore->index)
2395 + return line;
2396 + /* Try to restore members data */
2397 + set = ip_set_list[index];
2398 + members_size = 0;
2399 + DP("members_size %u reqsize %u",
2400 + set_restore->members_size, set->type->reqsize);
2401 + while (members_size + set->type->reqsize <=
2402 + set_restore->members_size) {
2403 + line++;
2404 + DP("members: %u, line %u", members_size, line);
2405 + res = __ip_set_addip(index,
2406 + data + used + members_size,
2407 + set->type->reqsize);
2408 + if (!(res == 0 || res == -EEXIST))
2409 + return line;
2410 + members_size += set->type->reqsize;
2411 + }
2412 +
2413 + DP("members_size %u %u",
2414 + set_restore->members_size, members_size);
2415 + if (members_size != set_restore->members_size)
2416 + return line++;
2417 + used += set_restore->members_size;
2418 + }
2419 +
2420 + bindings:
2421 + /* Loop to restore bindings */
2422 + while (used < len) {
2423 + line++;
2424 +
2425 + DP("restore binding, line %u", line);
2426 + /* Get and ensure size */
2427 + if (used + sizeof(struct ip_set_hash_save) > len)
2428 + return line;
2429 + hash_save = (struct ip_set_hash_save *) (data + used);
2430 + used += sizeof(struct ip_set_hash_save);
2431 +
2432 + /* hash_save->id is used to store the index */
2433 + index = ip_set_find_byindex(hash_save->id);
2434 + DP("restore binding index %u, id %u, %u -> %u",
2435 + index, hash_save->id, hash_save->ip, hash_save->binding);
2436 + if (index != hash_save->id)
2437 + return line;
2438 +
2439 + set = ip_set_list[hash_save->id];
2440 + /* Null valued IP means default binding */
2441 + if (hash_save->ip)
2442 + res = ip_set_hash_add(set->id,
2443 + hash_save->ip,
2444 + hash_save->binding);
2445 + else {
2446 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2447 + WRITE_LOCK(&ip_set_lock);
2448 + set->binding = hash_save->binding;
2449 + __ip_set_get(set->binding);
2450 + WRITE_UNLOCK(&ip_set_lock);
2451 + DP("default binding: %u", set->binding);
2452 + }
2453 + if (res != 0)
2454 + return line;
2455 + }
2456 + if (used != len)
2457 + return line;
2458 +
2459 + return 0;
2460 +}
2461 +
2462 +static int
2463 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2464 +{
2465 + void *data;
2466 + int res = 0; /* Assume OK */
2467 + unsigned *op;
2468 + struct ip_set_req_adt *req_adt;
2469 + ip_set_id_t index = IP_SET_INVALID_ID;
2470 + int (*adtfn)(ip_set_id_t index,
2471 + const void *data, size_t size);
2472 + struct fn_table {
2473 + int (*fn)(ip_set_id_t index,
2474 + const void *data, size_t size);
2475 + } adtfn_table[] =
2476 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2477 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2478 + };
2479 +
2480 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2481 + if (!capable(CAP_NET_ADMIN))
2482 + return -EPERM;
2483 + if (optval != SO_IP_SET)
2484 + return -EBADF;
2485 + if (len <= sizeof(unsigned)) {
2486 + ip_set_printk("short userdata (want >%zu, got %u)",
2487 + sizeof(unsigned), len);
2488 + return -EINVAL;
2489 + }
2490 + data = vmalloc(len);
2491 + if (!data) {
2492 + DP("out of mem for %u bytes", len);
2493 + return -ENOMEM;
2494 + }
2495 + if (copy_from_user(data, user, len) != 0) {
2496 + res = -EFAULT;
2497 + goto done;
2498 + }
2499 + if (down_interruptible(&ip_set_app_mutex)) {
2500 + res = -EINTR;
2501 + goto done;
2502 + }
2503 +
2504 + op = (unsigned *)data;
2505 + DP("op=%x", *op);
2506 +
2507 + if (*op < IP_SET_OP_VERSION) {
2508 + /* Check the version at the beginning of operations */
2509 + struct ip_set_req_version *req_version =
2510 + (struct ip_set_req_version *) data;
2511 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2512 + res = -EPROTO;
2513 + goto done;
2514 + }
2515 + }
2516 +
2517 + switch (*op) {
2518 + case IP_SET_OP_CREATE:{
2519 + struct ip_set_req_create *req_create
2520 + = (struct ip_set_req_create *) data;
2521 +
2522 + if (len <= sizeof(struct ip_set_req_create)) {
2523 + ip_set_printk("short CREATE data (want >%zu, got %u)",
2524 + sizeof(struct ip_set_req_create), len);
2525 + res = -EINVAL;
2526 + goto done;
2527 + }
2528 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2529 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2530 + res = ip_set_create(req_create->name,
2531 + req_create->typename,
2532 + IP_SET_INVALID_ID,
2533 + data + sizeof(struct ip_set_req_create),
2534 + len - sizeof(struct ip_set_req_create));
2535 + goto done;
2536 + }
2537 + case IP_SET_OP_DESTROY:{
2538 + struct ip_set_req_std *req_destroy
2539 + = (struct ip_set_req_std *) data;
2540 +
2541 + if (len != sizeof(struct ip_set_req_std)) {
2542 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2543 + sizeof(struct ip_set_req_std), len);
2544 + res = -EINVAL;
2545 + goto done;
2546 + }
2547 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2548 + /* Destroy all sets */
2549 + index = IP_SET_INVALID_ID;
2550 + } else {
2551 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2552 + index = ip_set_find_byname(req_destroy->name);
2553 +
2554 + if (index == IP_SET_INVALID_ID) {
2555 + res = -ENOENT;
2556 + goto done;
2557 + }
2558 + }
2559 +
2560 + res = ip_set_destroy(index);
2561 + goto done;
2562 + }
2563 + case IP_SET_OP_FLUSH:{
2564 + struct ip_set_req_std *req_flush =
2565 + (struct ip_set_req_std *) data;
2566 +
2567 + if (len != sizeof(struct ip_set_req_std)) {
2568 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2569 + sizeof(struct ip_set_req_std), len);
2570 + res = -EINVAL;
2571 + goto done;
2572 + }
2573 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2574 + /* Flush all sets */
2575 + index = IP_SET_INVALID_ID;
2576 + } else {
2577 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2578 + index = ip_set_find_byname(req_flush->name);
2579 +
2580 + if (index == IP_SET_INVALID_ID) {
2581 + res = -ENOENT;
2582 + goto done;
2583 + }
2584 + }
2585 + res = ip_set_flush(index);
2586 + goto done;
2587 + }
2588 + case IP_SET_OP_RENAME:{
2589 + struct ip_set_req_create *req_rename
2590 + = (struct ip_set_req_create *) data;
2591 +
2592 + if (len != sizeof(struct ip_set_req_create)) {
2593 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2594 + sizeof(struct ip_set_req_create), len);
2595 + res = -EINVAL;
2596 + goto done;
2597 + }
2598 +
2599 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2600 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2601 +
2602 + index = ip_set_find_byname(req_rename->name);
2603 + if (index == IP_SET_INVALID_ID) {
2604 + res = -ENOENT;
2605 + goto done;
2606 + }
2607 + res = ip_set_rename(index, req_rename->typename);
2608 + goto done;
2609 + }
2610 + case IP_SET_OP_SWAP:{
2611 + struct ip_set_req_create *req_swap
2612 + = (struct ip_set_req_create *) data;
2613 + ip_set_id_t to_index;
2614 +
2615 + if (len != sizeof(struct ip_set_req_create)) {
2616 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2617 + sizeof(struct ip_set_req_create), len);
2618 + res = -EINVAL;
2619 + goto done;
2620 + }
2621 +
2622 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2623 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2624 +
2625 + index = ip_set_find_byname(req_swap->name);
2626 + if (index == IP_SET_INVALID_ID) {
2627 + res = -ENOENT;
2628 + goto done;
2629 + }
2630 + to_index = ip_set_find_byname(req_swap->typename);
2631 + if (to_index == IP_SET_INVALID_ID) {
2632 + res = -ENOENT;
2633 + goto done;
2634 + }
2635 + res = ip_set_swap(index, to_index);
2636 + goto done;
2637 + }
2638 + default:
2639 + break; /* Set identified by id */
2640 + }
2641 +
2642 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2643 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2644 + res = -EBADMSG;
2645 + goto done;
2646 + }
2647 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2648 +
2649 + if (len < sizeof(struct ip_set_req_adt)) {
2650 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2651 + sizeof(struct ip_set_req_adt), len);
2652 + res = -EINVAL;
2653 + goto done;
2654 + }
2655 + req_adt = (struct ip_set_req_adt *) data;
2656 +
2657 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2658 + if (!(*op == IP_SET_OP_UNBIND_SET
2659 + && req_adt->index == IP_SET_INVALID_ID)) {
2660 + index = ip_set_find_byindex(req_adt->index);
2661 + if (index == IP_SET_INVALID_ID) {
2662 + res = -ENOENT;
2663 + goto done;
2664 + }
2665 + }
2666 + res = adtfn(index, data, len);
2667 +
2668 + done:
2669 + up(&ip_set_app_mutex);
2670 + vfree(data);
2671 + if (res > 0)
2672 + res = 0;
2673 + DP("final result %d", res);
2674 + return res;
2675 +}
2676 +
2677 +static int
2678 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2679 +{
2680 + int res = 0;
2681 + unsigned *op;
2682 + ip_set_id_t index = IP_SET_INVALID_ID;
2683 + void *data;
2684 + int copylen = *len;
2685 +
2686 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2687 + if (!capable(CAP_NET_ADMIN))
2688 + return -EPERM;
2689 + if (optval != SO_IP_SET)
2690 + return -EBADF;
2691 + if (*len < sizeof(unsigned)) {
2692 + ip_set_printk("short userdata (want >=%zu, got %d)",
2693 + sizeof(unsigned), *len);
2694 + return -EINVAL;
2695 + }
2696 + data = vmalloc(*len);
2697 + if (!data) {
2698 + DP("out of mem for %d bytes", *len);
2699 + return -ENOMEM;
2700 + }
2701 + if (copy_from_user(data, user, *len) != 0) {
2702 + res = -EFAULT;
2703 + goto done;
2704 + }
2705 + if (down_interruptible(&ip_set_app_mutex)) {
2706 + res = -EINTR;
2707 + goto done;
2708 + }
2709 +
2710 + op = (unsigned *) data;
2711 + DP("op=%x", *op);
2712 +
2713 + if (*op < IP_SET_OP_VERSION) {
2714 + /* Check the version at the beginning of operations */
2715 + struct ip_set_req_version *req_version =
2716 + (struct ip_set_req_version *) data;
2717 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2718 + res = -EPROTO;
2719 + goto done;
2720 + }
2721 + }
2722 +
2723 + switch (*op) {
2724 + case IP_SET_OP_VERSION: {
2725 + struct ip_set_req_version *req_version =
2726 + (struct ip_set_req_version *) data;
2727 +
2728 + if (*len != sizeof(struct ip_set_req_version)) {
2729 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2730 + sizeof(struct ip_set_req_version),
2731 + *len);
2732 + res = -EINVAL;
2733 + goto done;
2734 + }
2735 +
2736 + req_version->version = IP_SET_PROTOCOL_VERSION;
2737 + res = copy_to_user(user, req_version,
2738 + sizeof(struct ip_set_req_version));
2739 + goto done;
2740 + }
2741 + case IP_SET_OP_GET_BYNAME: {
2742 + struct ip_set_req_get_set *req_get
2743 + = (struct ip_set_req_get_set *) data;
2744 +
2745 + if (*len != sizeof(struct ip_set_req_get_set)) {
2746 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2747 + sizeof(struct ip_set_req_get_set), *len);
2748 + res = -EINVAL;
2749 + goto done;
2750 + }
2751 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2752 + index = ip_set_find_byname(req_get->set.name);
2753 + req_get->set.index = index;
2754 + goto copy;
2755 + }
2756 + case IP_SET_OP_GET_BYINDEX: {
2757 + struct ip_set_req_get_set *req_get
2758 + = (struct ip_set_req_get_set *) data;
2759 +
2760 + if (*len != sizeof(struct ip_set_req_get_set)) {
2761 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2762 + sizeof(struct ip_set_req_get_set), *len);
2763 + res = -EINVAL;
2764 + goto done;
2765 + }
2766 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2767 + index = ip_set_find_byindex(req_get->set.index);
2768 + strncpy(req_get->set.name,
2769 + index == IP_SET_INVALID_ID ? ""
2770 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2771 + goto copy;
2772 + }
2773 + case IP_SET_OP_ADT_GET: {
2774 + struct ip_set_req_adt_get *req_get
2775 + = (struct ip_set_req_adt_get *) data;
2776 +
2777 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2778 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2779 + sizeof(struct ip_set_req_adt_get), *len);
2780 + res = -EINVAL;
2781 + goto done;
2782 + }
2783 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2784 + index = ip_set_find_byname(req_get->set.name);
2785 + if (index != IP_SET_INVALID_ID) {
2786 + req_get->set.index = index;
2787 + strncpy(req_get->typename,
2788 + ip_set_list[index]->type->typename,
2789 + IP_SET_MAXNAMELEN - 1);
2790 + } else {
2791 + res = -ENOENT;
2792 + goto done;
2793 + }
2794 + goto copy;
2795 + }
2796 + case IP_SET_OP_MAX_SETS: {
2797 + struct ip_set_req_max_sets *req_max_sets
2798 + = (struct ip_set_req_max_sets *) data;
2799 + ip_set_id_t i;
2800 +
2801 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2802 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2803 + sizeof(struct ip_set_req_max_sets), *len);
2804 + res = -EINVAL;
2805 + goto done;
2806 + }
2807 +
2808 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2809 + req_max_sets->set.index = IP_SET_INVALID_ID;
2810 + } else {
2811 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2812 + req_max_sets->set.index =
2813 + ip_set_find_byname(req_max_sets->set.name);
2814 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2815 + res = -ENOENT;
2816 + goto done;
2817 + }
2818 + }
2819 + req_max_sets->max_sets = ip_set_max;
2820 + req_max_sets->sets = 0;
2821 + for (i = 0; i < ip_set_max; i++) {
2822 + if (ip_set_list[i] != NULL)
2823 + req_max_sets->sets++;
2824 + }
2825 + goto copy;
2826 + }
2827 + case IP_SET_OP_LIST_SIZE:
2828 + case IP_SET_OP_SAVE_SIZE: {
2829 + struct ip_set_req_setnames *req_setnames
2830 + = (struct ip_set_req_setnames *) data;
2831 + struct ip_set_name_list *name_list;
2832 + struct ip_set *set;
2833 + ip_set_id_t i;
2834 + int used;
2835 +
2836 + if (*len < sizeof(struct ip_set_req_setnames)) {
2837 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2838 + sizeof(struct ip_set_req_setnames), *len);
2839 + res = -EINVAL;
2840 + goto done;
2841 + }
2842 +
2843 + req_setnames->size = 0;
2844 + used = sizeof(struct ip_set_req_setnames);
2845 + for (i = 0; i < ip_set_max; i++) {
2846 + if (ip_set_list[i] == NULL)
2847 + continue;
2848 + name_list = (struct ip_set_name_list *)
2849 + (data + used);
2850 + used += sizeof(struct ip_set_name_list);
2851 + if (used > copylen) {
2852 + res = -EAGAIN;
2853 + goto done;
2854 + }
2855 + set = ip_set_list[i];
2856 + /* Fill in index, name, etc. */
2857 + name_list->index = i;
2858 + name_list->id = set->id;
2859 + strncpy(name_list->name,
2860 + set->name,
2861 + IP_SET_MAXNAMELEN - 1);
2862 + strncpy(name_list->typename,
2863 + set->type->typename,
2864 + IP_SET_MAXNAMELEN - 1);
2865 + DP("filled %s of type %s, index %u\n",
2866 + name_list->name, name_list->typename,
2867 + name_list->index);
2868 + if (!(req_setnames->index == IP_SET_INVALID_ID
2869 + || req_setnames->index == i))
2870 + continue;
2871 + /* Update size */
2872 + switch (*op) {
2873 + case IP_SET_OP_LIST_SIZE: {
2874 + req_setnames->size += sizeof(struct ip_set_list)
2875 + + set->type->header_size
2876 + + set->type->list_members_size(set);
2877 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2878 + i, &req_setnames->size);
2879 + break;
2880 + }
2881 + case IP_SET_OP_SAVE_SIZE: {
2882 + req_setnames->size += sizeof(struct ip_set_save)
2883 + + set->type->header_size
2884 + + set->type->list_members_size(set);
2885 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2886 + i, &req_setnames->size);
2887 + break;
2888 + }
2889 + default:
2890 + break;
2891 + }
2892 + }
2893 + if (copylen != used) {
2894 + res = -EAGAIN;
2895 + goto done;
2896 + }
2897 + goto copy;
2898 + }
2899 + case IP_SET_OP_LIST: {
2900 + struct ip_set_req_list *req_list
2901 + = (struct ip_set_req_list *) data;
2902 + ip_set_id_t i;
2903 + int used;
2904 +
2905 + if (*len < sizeof(struct ip_set_req_list)) {
2906 + ip_set_printk("short LIST (want >=%zu, got %d)",
2907 + sizeof(struct ip_set_req_list), *len);
2908 + res = -EINVAL;
2909 + goto done;
2910 + }
2911 + index = req_list->index;
2912 + if (index != IP_SET_INVALID_ID
2913 + && ip_set_find_byindex(index) != index) {
2914 + res = -ENOENT;
2915 + goto done;
2916 + }
2917 + used = 0;
2918 + if (index == IP_SET_INVALID_ID) {
2919 + /* List all sets */
2920 + for (i = 0; i < ip_set_max && res == 0; i++) {
2921 + if (ip_set_list[i] != NULL)
2922 + res = ip_set_list_set(i, data, &used, *len);
2923 + }
2924 + } else {
2925 + /* List an individual set */
2926 + res = ip_set_list_set(index, data, &used, *len);
2927 + }
2928 + if (res != 0)
2929 + goto done;
2930 + else if (copylen != used) {
2931 + res = -EAGAIN;
2932 + goto done;
2933 + }
2934 + goto copy;
2935 + }
2936 + case IP_SET_OP_SAVE: {
2937 + struct ip_set_req_list *req_save
2938 + = (struct ip_set_req_list *) data;
2939 + ip_set_id_t i;
2940 + int used;
2941 +
2942 + if (*len < sizeof(struct ip_set_req_list)) {
2943 + ip_set_printk("short SAVE (want >=%zu, got %d)",
2944 + sizeof(struct ip_set_req_list), *len);
2945 + res = -EINVAL;
2946 + goto done;
2947 + }
2948 + index = req_save->index;
2949 + if (index != IP_SET_INVALID_ID
2950 + && ip_set_find_byindex(index) != index) {
2951 + res = -ENOENT;
2952 + goto done;
2953 + }
2954 + used = 0;
2955 + if (index == IP_SET_INVALID_ID) {
2956 + /* Save all sets */
2957 + for (i = 0; i < ip_set_max && res == 0; i++) {
2958 + if (ip_set_list[i] != NULL)
2959 + res = ip_set_save_set(i, data, &used, *len);
2960 + }
2961 + } else {
2962 + /* Save an individual set */
2963 + res = ip_set_save_set(index, data, &used, *len);
2964 + }
2965 + if (res == 0)
2966 + res = ip_set_save_bindings(index, data, &used, *len);
2967 +
2968 + if (res != 0)
2969 + goto done;
2970 + else if (copylen != used) {
2971 + res = -EAGAIN;
2972 + goto done;
2973 + }
2974 + goto copy;
2975 + }
2976 + case IP_SET_OP_RESTORE: {
2977 + struct ip_set_req_setnames *req_restore
2978 + = (struct ip_set_req_setnames *) data;
2979 + int line;
2980 +
2981 + if (*len < sizeof(struct ip_set_req_setnames)
2982 + || *len != req_restore->size) {
2983 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
2984 + req_restore->size, *len);
2985 + res = -EINVAL;
2986 + goto done;
2987 + }
2988 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
2989 + req_restore->size - sizeof(struct ip_set_req_setnames));
2990 + DP("ip_set_restore: %u", line);
2991 + if (line != 0) {
2992 + res = -EAGAIN;
2993 + req_restore->size = line;
2994 + copylen = sizeof(struct ip_set_req_setnames);
2995 + goto copy;
2996 + }
2997 + goto done;
2998 + }
2999 + default:
3000 + res = -EBADMSG;
3001 + goto done;
3002 + } /* end of switch(op) */
3003 +
3004 + copy:
3005 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3006 + && ip_set_list[index]
3007 + ? ip_set_list[index]->name
3008 + : ":all:", copylen);
3009 + if (res == 0)
3010 + res = copy_to_user(user, data, copylen);
3011 + else
3012 + copy_to_user(user, data, copylen);
3013 +
3014 + done:
3015 + up(&ip_set_app_mutex);
3016 + vfree(data);
3017 + if (res > 0)
3018 + res = 0;
3019 + DP("final result %d", res);
3020 + return res;
3021 +}
3022 +
3023 +static struct nf_sockopt_ops so_set = {
3024 + .pf = PF_INET,
3025 + .set_optmin = SO_IP_SET,
3026 + .set_optmax = SO_IP_SET + 1,
3027 + .set = &ip_set_sockfn_set,
3028 + .get_optmin = SO_IP_SET,
3029 + .get_optmax = SO_IP_SET + 1,
3030 + .get = &ip_set_sockfn_get,
3031 + .use = 0
3032 +};
3033 +
3034 +static int max_sets, hash_size;
3035 +MODULE_PARM(max_sets, "i");
3036 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3037 +MODULE_PARM(hash_size, "i");
3038 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3039 +MODULE_LICENSE("GPL");
3040 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3041 +MODULE_DESCRIPTION("module implementing core IP set support");
3042 +
3043 +static int __init init(void)
3044 +{
3045 + int res;
3046 + ip_set_id_t i;
3047 +
3048 + get_random_bytes(&ip_set_hash_random, 4);
3049 + if (max_sets)
3050 + ip_set_max = max_sets;
3051 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3052 + if (!ip_set_list) {
3053 + printk(KERN_ERR "Unable to create ip_set_list\n");
3054 + return -ENOMEM;
3055 + }
3056 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3057 + if (hash_size)
3058 + ip_set_bindings_hash_size = hash_size;
3059 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3060 + if (!ip_set_hash) {
3061 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3062 + vfree(ip_set_list);
3063 + return -ENOMEM;
3064 + }
3065 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3066 + INIT_LIST_HEAD(&ip_set_hash[i]);
3067 +
3068 + INIT_LIST_HEAD(&set_type_list);
3069 +
3070 + res = nf_register_sockopt(&so_set);
3071 + if (res != 0) {
3072 + ip_set_printk("SO_SET registry failed: %d", res);
3073 + vfree(ip_set_list);
3074 + vfree(ip_set_hash);
3075 + return res;
3076 + }
3077 + return 0;
3078 +}
3079 +
3080 +static void __exit fini(void)
3081 +{
3082 + /* There can't be any existing set or binding. Racy. */
3083 + nf_unregister_sockopt(&so_set);
3084 + vfree(ip_set_list);
3085 + vfree(ip_set_hash);
3086 + DP("these are the famous last words");
3087 +}
3088 +
3089 +EXPORT_SYMBOL(ip_set_register_set_type);
3090 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3091 +
3092 +EXPORT_SYMBOL(ip_set_get_byname);
3093 +EXPORT_SYMBOL(ip_set_get_byindex);
3094 +EXPORT_SYMBOL(ip_set_put);
3095 +
3096 +EXPORT_SYMBOL(ip_set_addip_kernel);
3097 +EXPORT_SYMBOL(ip_set_delip_kernel);
3098 +EXPORT_SYMBOL(ip_set_testip_kernel);
3099 +
3100 +module_init(init);
3101 +module_exit(fini);
3102 --- /dev/null
3103 +++ b/net/ipv4/netfilter/ip_set_iphash.c
3104 @@ -0,0 +1,379 @@
3105 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3106 + *
3107 + * This program is free software; you can redistribute it and/or modify
3108 + * it under the terms of the GNU General Public License version 2 as
3109 + * published by the Free Software Foundation.
3110 + */
3111 +
3112 +/* Kernel module implementing an ip hash set */
3113 +
3114 +#include <linux/module.h>
3115 +#include <linux/ip.h>
3116 +#include <linux/skbuff.h>
3117 +#include <linux/netfilter_ipv4/ip_tables.h>
3118 +#include <linux/netfilter_ipv4/ip_set.h>
3119 +#include <linux/errno.h>
3120 +#include <asm/uaccess.h>
3121 +#include <asm/bitops.h>
3122 +#include <asm/softirq.h>
3123 +#include <linux/spinlock.h>
3124 +#include <linux/vmalloc.h>
3125 +#include <linux/random.h>
3126 +
3127 +#include <net/ip.h>
3128 +
3129 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3130 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3131 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
3132 +#include <linux/netfilter_ipv4/ip_set_prime.h>
3133 +
3134 +static inline __u32
3135 +jhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3136 +{
3137 + return jhash_1word(ip, map->initval);
3138 +}
3139 +
3140 +static inline __u32
3141 +randhash_ip(const struct ip_set_iphash *map, ip_set_ip_t ip)
3142 +{
3143 + return (1 + ip % map->prime);
3144 +}
3145 +
3146 +static inline __u32
3147 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3148 +{
3149 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3150 + __u32 jhash, randhash, id;
3151 + u_int16_t i;
3152 +
3153 + *hash_ip = ip & map->netmask;
3154 + jhash = jhash_ip(map, *hash_ip);
3155 + randhash = randhash_ip(map, *hash_ip);
3156 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3157 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3158 +
3159 + for (i = 0; i < map->probes; i++) {
3160 + id = (jhash + i * randhash) % map->hashsize;
3161 + DP("hash key: %u", id);
3162 + if (map->members[id] == *hash_ip)
3163 + return id;
3164 + /* No shortcut at testing - there can be deleted
3165 + * entries. */
3166 + }
3167 + return UINT_MAX;
3168 +}
3169 +
3170 +static inline int
3171 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3172 +{
3173 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
3174 +}
3175 +
3176 +static int
3177 +testip(struct ip_set *set, const void *data, size_t size,
3178 + ip_set_ip_t *hash_ip)
3179 +{
3180 + struct ip_set_req_iphash *req =
3181 + (struct ip_set_req_iphash *) data;
3182 +
3183 + if (size != sizeof(struct ip_set_req_iphash)) {
3184 + ip_set_printk("data length wrong (want %zu, have %zu)",
3185 + sizeof(struct ip_set_req_iphash),
3186 + size);
3187 + return -EINVAL;
3188 + }
3189 + return __testip(set, req->ip, hash_ip);
3190 +}
3191 +
3192 +static int
3193 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
3194 + u_int32_t flags, ip_set_ip_t *hash_ip)
3195 +{
3196 + return __testip(set,
3197 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3198 + : skb->nh.iph->daddr),
3199 + hash_ip);
3200 +}
3201 +
3202 +static inline int
3203 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3204 +{
3205 + __u32 jhash, randhash, probe;
3206 + u_int16_t i;
3207 +
3208 + *hash_ip = ip & map->netmask;
3209 + jhash = jhash_ip(map, *hash_ip);
3210 + randhash = randhash_ip(map, *hash_ip);
3211 +
3212 + for (i = 0; i < map->probes; i++) {
3213 + probe = (jhash + i * randhash) % map->hashsize;
3214 + if (map->members[probe] == *hash_ip)
3215 + return -EEXIST;
3216 + if (!map->members[probe]) {
3217 + map->members[probe] = *hash_ip;
3218 + return 0;
3219 + }
3220 + }
3221 + /* Trigger rehashing */
3222 + return -EAGAIN;
3223 +}
3224 +
3225 +static int
3226 +addip(struct ip_set *set, const void *data, size_t size,
3227 + ip_set_ip_t *hash_ip)
3228 +{
3229 + struct ip_set_req_iphash *req =
3230 + (struct ip_set_req_iphash *) data;
3231 +
3232 + if (size != sizeof(struct ip_set_req_iphash)) {
3233 + ip_set_printk("data length wrong (want %zu, have %zu)",
3234 + sizeof(struct ip_set_req_iphash),
3235 + size);
3236 + return -EINVAL;
3237 + }
3238 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3239 +}
3240 +
3241 +static int
3242 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3243 + u_int32_t flags, ip_set_ip_t *hash_ip)
3244 +{
3245 + return __addip((struct ip_set_iphash *) set->data,
3246 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3247 + : skb->nh.iph->daddr),
3248 + hash_ip);
3249 +}
3250 +
3251 +static int retry(struct ip_set *set)
3252 +{
3253 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3254 + ip_set_ip_t hash_ip, *members;
3255 + u_int32_t i, hashsize;
3256 + unsigned newbytes;
3257 + int res;
3258 + struct ip_set_iphash tmp = {
3259 + .hashsize = map->hashsize,
3260 + .probes = map->probes,
3261 + .resize = map->resize,
3262 + .netmask = map->netmask,
3263 + };
3264 +
3265 + if (map->resize == 0)
3266 + return -ERANGE;
3267 +
3268 + again:
3269 + res = 0;
3270 +
3271 + /* Calculate new parameters */
3272 + get_random_bytes(&tmp.initval, 4);
3273 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
3274 + if (hashsize == tmp.hashsize)
3275 + hashsize++;
3276 + tmp.prime = make_prime(hashsize);
3277 +
3278 + ip_set_printk("rehashing of set %s triggered: "
3279 + "hashsize grows from %u to %u",
3280 + set->name, tmp.hashsize, hashsize);
3281 + tmp.hashsize = hashsize;
3282 +
3283 + newbytes = hashsize * sizeof(ip_set_ip_t);
3284 + tmp.members = ip_set_malloc(newbytes);
3285 + if (!tmp.members) {
3286 + DP("out of memory for %d bytes", newbytes);
3287 + return -ENOMEM;
3288 + }
3289 + memset(tmp.members, 0, newbytes);
3290 +
3291 + write_lock_bh(&set->lock);
3292 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3293 + for (i = 0; i < map->hashsize && res == 0; i++) {
3294 + if (map->members[i])
3295 + res = __addip(&tmp, map->members[i], &hash_ip);
3296 + }
3297 + if (res) {
3298 + /* Failure, try again */
3299 + write_unlock_bh(&set->lock);
3300 + ip_set_free(tmp.members, newbytes);
3301 + goto again;
3302 + }
3303 +
3304 + /* Success at resizing! */
3305 + members = map->members;
3306 + hashsize = map->hashsize;
3307 +
3308 + map->initval = tmp.initval;
3309 + map->prime = tmp.prime;
3310 + map->hashsize = tmp.hashsize;
3311 + map->members = tmp.members;
3312 + write_unlock_bh(&set->lock);
3313 +
3314 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
3315 +
3316 + return 0;
3317 +}
3318 +
3319 +static inline int
3320 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3321 +{
3322 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3323 + ip_set_ip_t id = hash_id(set, ip, hash_ip);
3324 +
3325 + if (id == UINT_MAX)
3326 + return -EEXIST;
3327 +
3328 + map->members[id] = 0;
3329 + return 0;
3330 +}
3331 +
3332 +static int
3333 +delip(struct ip_set *set, const void *data, size_t size,
3334 + ip_set_ip_t *hash_ip)
3335 +{
3336 + struct ip_set_req_iphash *req =
3337 + (struct ip_set_req_iphash *) data;
3338 +
3339 + if (size != sizeof(struct ip_set_req_iphash)) {
3340 + ip_set_printk("data length wrong (want %zu, have %zu)",
3341 + sizeof(struct ip_set_req_iphash),
3342 + size);
3343 + return -EINVAL;
3344 + }
3345 + return __delip(set, req->ip, hash_ip);
3346 +}
3347 +
3348 +static int
3349 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3350 + u_int32_t flags, ip_set_ip_t *hash_ip)
3351 +{
3352 + return __delip(set,
3353 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3354 + : skb->nh.iph->daddr),
3355 + hash_ip);
3356 +}
3357 +
3358 +static int create(struct ip_set *set, const void *data, size_t size)
3359 +{
3360 + unsigned newbytes;
3361 + struct ip_set_req_iphash_create *req =
3362 + (struct ip_set_req_iphash_create *) data;
3363 + struct ip_set_iphash *map;
3364 +
3365 + if (size != sizeof(struct ip_set_req_iphash_create)) {
3366 + ip_set_printk("data length wrong (want %zu, have %zu)",
3367 + sizeof(struct ip_set_req_iphash_create),
3368 + size);
3369 + return -EINVAL;
3370 + }
3371 +
3372 + if (req->hashsize < 1) {
3373 + ip_set_printk("hashsize too small");
3374 + return -ENOEXEC;
3375 + }
3376 +
3377 + map = kmalloc(sizeof(struct ip_set_iphash), GFP_KERNEL);
3378 + if (!map) {
3379 + DP("out of memory for %d bytes",
3380 + sizeof(struct ip_set_iphash));
3381 + return -ENOMEM;
3382 + }
3383 + get_random_bytes(&map->initval, 4);
3384 + map->prime = make_prime(req->hashsize);
3385 + map->hashsize = req->hashsize;
3386 + map->probes = req->probes;
3387 + map->resize = req->resize;
3388 + map->netmask = req->netmask;
3389 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
3390 + map->members = ip_set_malloc(newbytes);
3391 + if (!map->members) {
3392 + DP("out of memory for %d bytes", newbytes);
3393 + kfree(map);
3394 + return -ENOMEM;
3395 + }
3396 + memset(map->members, 0, newbytes);
3397 +
3398 + set->data = map;
3399 + return 0;
3400 +}
3401 +
3402 +static void destroy(struct ip_set *set)
3403 +{
3404 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3405 +
3406 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
3407 + kfree(map);
3408 +
3409 + set->data = NULL;
3410 +}
3411 +
3412 +static void flush(struct ip_set *set)
3413 +{
3414 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3415 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
3416 +}
3417 +
3418 +static void list_header(const struct ip_set *set, void *data)
3419 +{
3420 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3421 + struct ip_set_req_iphash_create *header =
3422 + (struct ip_set_req_iphash_create *) data;
3423 +
3424 + header->hashsize = map->hashsize;
3425 + header->probes = map->probes;
3426 + header->resize = map->resize;
3427 + header->netmask = map->netmask;
3428 +}
3429 +
3430 +static int list_members_size(const struct ip_set *set)
3431 +{
3432 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3433 +
3434 + return (map->hashsize * sizeof(ip_set_ip_t));
3435 +}
3436 +
3437 +static void list_members(const struct ip_set *set, void *data)
3438 +{
3439 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3440 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
3441 +
3442 + memcpy(data, map->members, bytes);
3443 +}
3444 +
3445 +static struct ip_set_type ip_set_iphash = {
3446 + .typename = SETTYPE_NAME,
3447 + .typecode = IPSET_TYPE_IP,
3448 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3449 + .create = &create,
3450 + .destroy = &destroy,
3451 + .flush = &flush,
3452 + .reqsize = sizeof(struct ip_set_req_iphash),
3453 + .addip = &addip,
3454 + .addip_kernel = &addip_kernel,
3455 + .retry = &retry,
3456 + .delip = &delip,
3457 + .delip_kernel = &delip_kernel,
3458 + .testip = &testip,
3459 + .testip_kernel = &testip_kernel,
3460 + .header_size = sizeof(struct ip_set_req_iphash_create),
3461 + .list_header = &list_header,
3462 + .list_members_size = &list_members_size,
3463 + .list_members = &list_members,
3464 + .me = THIS_MODULE,
3465 +};
3466 +
3467 +MODULE_LICENSE("GPL");
3468 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3469 +MODULE_DESCRIPTION("iphash type of IP sets");
3470 +
3471 +static int __init init(void)
3472 +{
3473 + return ip_set_register_set_type(&ip_set_iphash);
3474 +}
3475 +
3476 +static void __exit fini(void)
3477 +{
3478 + /* FIXME: possible race with ip_set_create() */
3479 + ip_set_unregister_set_type(&ip_set_iphash);
3480 +}
3481 +
3482 +module_init(init);
3483 +module_exit(fini);
3484 --- /dev/null
3485 +++ b/net/ipv4/netfilter/ip_set_ipmap.c
3486 @@ -0,0 +1,314 @@
3487 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3488 + * Patrick Schaaf <bof@bof.de>
3489 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3490 + *
3491 + * This program is free software; you can redistribute it and/or modify
3492 + * it under the terms of the GNU General Public License version 2 as
3493 + * published by the Free Software Foundation.
3494 + */
3495 +
3496 +/* Kernel module implementing an IP set type: the single bitmap type */
3497 +
3498 +#include <linux/module.h>
3499 +#include <linux/ip.h>
3500 +#include <linux/skbuff.h>
3501 +#include <linux/netfilter_ipv4/ip_tables.h>
3502 +#include <linux/netfilter_ipv4/ip_set.h>
3503 +#include <linux/errno.h>
3504 +#include <asm/uaccess.h>
3505 +#include <asm/bitops.h>
3506 +#include <asm/softirq.h>
3507 +#include <linux/spinlock.h>
3508 +
3509 +#include <linux/netfilter_ipv4/ip_set_ipmap.h>
3510 +
3511 +static inline ip_set_ip_t
3512 +ip_to_id(const struct ip_set_ipmap *map, ip_set_ip_t ip)
3513 +{
3514 + return (ip - map->first_ip)/map->hosts;
3515 +}
3516 +
3517 +static inline int
3518 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3519 +{
3520 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3521 +
3522 + if (ip < map->first_ip || ip > map->last_ip)
3523 + return -ERANGE;
3524 +
3525 + *hash_ip = ip & map->netmask;
3526 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
3527 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
3528 + return !!test_bit(ip_to_id(map, *hash_ip), map->members);
3529 +}
3530 +
3531 +static int
3532 +testip(struct ip_set *set, const void *data, size_t size,
3533 + ip_set_ip_t *hash_ip)
3534 +{
3535 + struct ip_set_req_ipmap *req =
3536 + (struct ip_set_req_ipmap *) data;
3537 +
3538 + if (size != sizeof(struct ip_set_req_ipmap)) {
3539 + ip_set_printk("data length wrong (want %zu, have %zu)",
3540 + sizeof(struct ip_set_req_ipmap),
3541 + size);
3542 + return -EINVAL;
3543 + }
3544 + return __testip(set, req->ip, hash_ip);
3545 +}
3546 +
3547 +static int
3548 +testip_kernel(struct ip_set *set,
3549 + const struct sk_buff *skb,
3550 + u_int32_t flags,
3551 + ip_set_ip_t *hash_ip)
3552 +{
3553 + int res;
3554 +
3555 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3556 + flags & IPSET_SRC ? "SRC" : "DST",
3557 + NIPQUAD(skb->nh.iph->saddr),
3558 + NIPQUAD(skb->nh.iph->daddr));
3559 +
3560 + res = __testip(set,
3561 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3562 + : skb->nh.iph->daddr),
3563 + hash_ip);
3564 + return (res < 0 ? 0 : res);
3565 +}
3566 +
3567 +static inline int
3568 +__addip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3569 +{
3570 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3571 +
3572 + if (ip < map->first_ip || ip > map->last_ip)
3573 + return -ERANGE;
3574 +
3575 + *hash_ip = ip & map->netmask;
3576 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3577 + if (test_and_set_bit(ip_to_id(map, *hash_ip), map->members))
3578 + return -EEXIST;
3579 +
3580 + return 0;
3581 +}
3582 +
3583 +static int
3584 +addip(struct ip_set *set, const void *data, size_t size,
3585 + ip_set_ip_t *hash_ip)
3586 +{
3587 + struct ip_set_req_ipmap *req =
3588 + (struct ip_set_req_ipmap *) data;
3589 +
3590 + if (size != sizeof(struct ip_set_req_ipmap)) {
3591 + ip_set_printk("data length wrong (want %zu, have %zu)",
3592 + sizeof(struct ip_set_req_ipmap),
3593 + size);
3594 + return -EINVAL;
3595 + }
3596 + DP("%u.%u.%u.%u", HIPQUAD(req->ip));
3597 + return __addip(set, req->ip, hash_ip);
3598 +}
3599 +
3600 +static int
3601 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3602 + u_int32_t flags, ip_set_ip_t *hash_ip)
3603 +{
3604 + return __addip(set,
3605 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3606 + : skb->nh.iph->daddr),
3607 + hash_ip);
3608 +}
3609 +
3610 +static inline int
3611 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3612 +{
3613 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3614 +
3615 + if (ip < map->first_ip || ip > map->last_ip)
3616 + return -ERANGE;
3617 +
3618 + *hash_ip = ip & map->netmask;
3619 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
3620 + if (!test_and_clear_bit(ip_to_id(map, *hash_ip), map->members))
3621 + return -EEXIST;
3622 +
3623 + return 0;
3624 +}
3625 +
3626 +static int
3627 +delip(struct ip_set *set, const void *data, size_t size,
3628 + ip_set_ip_t *hash_ip)
3629 +{
3630 + struct ip_set_req_ipmap *req =
3631 + (struct ip_set_req_ipmap *) data;
3632 +
3633 + if (size != sizeof(struct ip_set_req_ipmap)) {
3634 + ip_set_printk("data length wrong (want %zu, have %zu)",
3635 + sizeof(struct ip_set_req_ipmap),
3636 + size);
3637 + return -EINVAL;
3638 + }
3639 + return __delip(set, req->ip, hash_ip);
3640 +}
3641 +
3642 +static int
3643 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
3644 + u_int32_t flags, ip_set_ip_t *hash_ip)
3645 +{
3646 + return __delip(set,
3647 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3648 + : skb->nh.iph->daddr),
3649 + hash_ip);
3650 +}
3651 +
3652 +static int create(struct ip_set *set, const void *data, size_t size)
3653 +{
3654 + int newbytes;
3655 + struct ip_set_req_ipmap_create *req =
3656 + (struct ip_set_req_ipmap_create *) data;
3657 + struct ip_set_ipmap *map;
3658 +
3659 + if (size != sizeof(struct ip_set_req_ipmap_create)) {
3660 + ip_set_printk("data length wrong (want %zu, have %zu)",
3661 + sizeof(struct ip_set_req_ipmap_create),
3662 + size);
3663 + return -EINVAL;
3664 + }
3665 +
3666 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
3667 + HIPQUAD(req->from), HIPQUAD(req->to));
3668 +
3669 + if (req->from > req->to) {
3670 + DP("bad ip range");
3671 + return -ENOEXEC;
3672 + }
3673 +
3674 + if (req->to - req->from > MAX_RANGE) {
3675 + ip_set_printk("range too big (max %d addresses)",
3676 + MAX_RANGE);
3677 + return -ENOEXEC;
3678 + }
3679 +
3680 + map = kmalloc(sizeof(struct ip_set_ipmap), GFP_KERNEL);
3681 + if (!map) {
3682 + DP("out of memory for %d bytes",
3683 + sizeof(struct ip_set_ipmap));
3684 + return -ENOMEM;
3685 + }
3686 + map->first_ip = req->from;
3687 + map->last_ip = req->to;
3688 + map->netmask = req->netmask;
3689 +
3690 + if (req->netmask == 0xFFFFFFFF) {
3691 + map->hosts = 1;
3692 + map->sizeid = map->last_ip - map->first_ip + 1;
3693 + } else {
3694 + unsigned int mask_bits, netmask_bits;
3695 + ip_set_ip_t mask;
3696 +
3697 + map->first_ip &= map->netmask; /* Should we better bark? */
3698 +
3699 + mask = range_to_mask(map->first_ip, map->last_ip, &mask_bits);
3700 + netmask_bits = mask_to_bits(map->netmask);
3701 +
3702 + if (!mask || netmask_bits <= mask_bits)
3703 + return -ENOEXEC;
3704 +
3705 + map->hosts = 2 << (32 - netmask_bits - 1);
3706 + map->sizeid = 2 << (netmask_bits - mask_bits - 1);
3707 + }
3708 + newbytes = bitmap_bytes(0, map->sizeid - 1);
3709 + map->members = kmalloc(newbytes, GFP_KERNEL);
3710 + if (!map->members) {
3711 + DP("out of memory for %d bytes", newbytes);
3712 + kfree(map);
3713 + return -ENOMEM;
3714 + }
3715 + memset(map->members, 0, newbytes);
3716 +
3717 + set->data = map;
3718 + return 0;
3719 +}
3720 +
3721 +static void destroy(struct ip_set *set)
3722 +{
3723 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3724 +
3725 + kfree(map->members);
3726 + kfree(map);
3727 +
3728 + set->data = NULL;
3729 +}
3730 +
3731 +static void flush(struct ip_set *set)
3732 +{
3733 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3734 + memset(map->members, 0, bitmap_bytes(0, map->sizeid - 1));
3735 +}
3736 +
3737 +static void list_header(const struct ip_set *set, void *data)
3738 +{
3739 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3740 + struct ip_set_req_ipmap_create *header =
3741 + (struct ip_set_req_ipmap_create *) data;
3742 +
3743 + header->from = map->first_ip;
3744 + header->to = map->last_ip;
3745 + header->netmask = map->netmask;
3746 +}
3747 +
3748 +static int list_members_size(const struct ip_set *set)
3749 +{
3750 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3751 +
3752 + return bitmap_bytes(0, map->sizeid - 1);
3753 +}
3754 +
3755 +static void list_members(const struct ip_set *set, void *data)
3756 +{
3757 + struct ip_set_ipmap *map = (struct ip_set_ipmap *) set->data;
3758 + int bytes = bitmap_bytes(0, map->sizeid - 1);
3759 +
3760 + memcpy(data, map->members, bytes);
3761 +}
3762 +
3763 +static struct ip_set_type ip_set_ipmap = {
3764 + .typename = SETTYPE_NAME,
3765 + .typecode = IPSET_TYPE_IP,
3766 + .protocol_version = IP_SET_PROTOCOL_VERSION,
3767 + .create = &create,
3768 + .destroy = &destroy,
3769 + .flush = &flush,
3770 + .reqsize = sizeof(struct ip_set_req_ipmap),
3771 + .addip = &addip,
3772 + .addip_kernel = &addip_kernel,
3773 + .delip = &delip,
3774 + .delip_kernel = &delip_kernel,
3775 + .testip = &testip,
3776 + .testip_kernel = &testip_kernel,
3777 + .header_size = sizeof(struct ip_set_req_ipmap_create),
3778 + .list_header = &list_header,
3779 + .list_members_size = &list_members_size,
3780 + .list_members = &list_members,
3781 + .me = THIS_MODULE,
3782 +};
3783 +
3784 +MODULE_LICENSE("GPL");
3785 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3786 +MODULE_DESCRIPTION("ipmap type of IP sets");
3787 +
3788 +static int __init init(void)
3789 +{
3790 + return ip_set_register_set_type(&ip_set_ipmap);
3791 +}
3792 +
3793 +static void __exit fini(void)
3794 +{
3795 + /* FIXME: possible race with ip_set_create() */
3796 + ip_set_unregister_set_type(&ip_set_ipmap);
3797 +}
3798 +
3799 +module_init(init);
3800 +module_exit(fini);
3801 --- /dev/null
3802 +++ b/net/ipv4/netfilter/ip_set_iptree.c
3803 @@ -0,0 +1,511 @@
3804 +/* Copyright (C) 2005 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3805 + *
3806 + * This program is free software; you can redistribute it and/or modify
3807 + * it under the terms of the GNU General Public License version 2 as
3808 + * published by the Free Software Foundation.
3809 + */
3810 +
3811 +/* Kernel module implementing an IP set type: the iptree type */
3812 +
3813 +#include <linux/module.h>
3814 +#include <linux/ip.h>
3815 +#include <linux/skbuff.h>
3816 +#include <linux/slab.h>
3817 +#include <linux/delay.h>
3818 +#include <linux/netfilter_ipv4/ip_tables.h>
3819 +#include <linux/netfilter_ipv4/ip_set.h>
3820 +#include <linux/errno.h>
3821 +#include <asm/uaccess.h>
3822 +#include <asm/bitops.h>
3823 +#include <linux/interrupt.h>
3824 +#include <linux/spinlock.h>
3825 +
3826 +#include <linux/netfilter_ipv4/ip_set_iptree.h>
3827 +
3828 +/* Garbage collection interval in seconds: */
3829 +#define IPTREE_GC_TIME 5*60
3830 +/* Sleep so many milliseconds before trying again
3831 + * to delete the gc timer at destroying a set */
3832 +#define IPTREE_DESTROY_SLEEP 100
3833 +
3834 +static kmem_cache_t *branch_cachep;
3835 +static kmem_cache_t *leaf_cachep;
3836 +
3837 +#define ABCD(a,b,c,d,addrp) do { \
3838 + a = ((unsigned char *)addrp)[3]; \
3839 + b = ((unsigned char *)addrp)[2]; \
3840 + c = ((unsigned char *)addrp)[1]; \
3841 + d = ((unsigned char *)addrp)[0]; \
3842 +} while (0)
3843 +
3844 +#define TESTIP_WALK(map, elem, branch) do { \
3845 + if ((map)->tree[elem]) { \
3846 + branch = (map)->tree[elem]; \
3847 + } else \
3848 + return 0; \
3849 +} while (0)
3850 +
3851 +static inline int
3852 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3853 +{
3854 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3855 + struct ip_set_iptreeb *btree;
3856 + struct ip_set_iptreec *ctree;
3857 + struct ip_set_iptreed *dtree;
3858 + unsigned char a,b,c,d;
3859 +
3860 + *hash_ip = ip;
3861 + ABCD(a, b, c, d, hash_ip);
3862 + DP("%u %u %u %u timeout %u", a, b, c, d, map->timeout);
3863 + TESTIP_WALK(map, a, btree);
3864 + TESTIP_WALK(btree, b, ctree);
3865 + TESTIP_WALK(ctree, c, dtree);
3866 + DP("%lu %lu", dtree->expires[d], jiffies);
3867 + return !!(map->timeout ? (time_after(dtree->expires[d], jiffies))
3868 + : dtree->expires[d]);
3869 +}
3870 +
3871 +static int
3872 +testip(struct ip_set *set, const void *data, size_t size,
3873 + ip_set_ip_t *hash_ip)
3874 +{
3875 + struct ip_set_req_iptree *req =
3876 + (struct ip_set_req_iptree *) data;
3877 +
3878 + if (size != sizeof(struct ip_set_req_iptree)) {
3879 + ip_set_printk("data length wrong (want %zu, have %zu)",
3880 + sizeof(struct ip_set_req_iptree),
3881 + size);
3882 + return -EINVAL;
3883 + }
3884 + return __testip(set, req->ip, hash_ip);
3885 +}
3886 +
3887 +static int
3888 +testip_kernel(struct ip_set *set,
3889 + const struct sk_buff *skb,
3890 + u_int32_t flags,
3891 + ip_set_ip_t *hash_ip)
3892 +{
3893 + int res;
3894 +
3895 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
3896 + flags & IPSET_SRC ? "SRC" : "DST",
3897 + NIPQUAD(skb->nh.iph->saddr),
3898 + NIPQUAD(skb->nh.iph->daddr));
3899 +
3900 + res = __testip(set,
3901 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3902 + : skb->nh.iph->daddr),
3903 + hash_ip);
3904 + return (res < 0 ? 0 : res);
3905 +}
3906 +
3907 +#define ADDIP_WALK(map, elem, branch, type, cachep) do { \
3908 + if ((map)->tree[elem]) { \
3909 + DP("found %u", elem); \
3910 + branch = (map)->tree[elem]; \
3911 + } else { \
3912 + branch = (type *) \
3913 + kmem_cache_alloc(cachep, GFP_KERNEL); \
3914 + if (branch == NULL) \
3915 + return -ENOMEM; \
3916 + memset(branch, 0, sizeof(*branch)); \
3917 + (map)->tree[elem] = branch; \
3918 + DP("alloc %u", elem); \
3919 + } \
3920 +} while (0)
3921 +
3922 +static inline int
3923 +__addip(struct ip_set *set, ip_set_ip_t ip, unsigned int timeout,
3924 + ip_set_ip_t *hash_ip)
3925 +{
3926 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3927 + struct ip_set_iptreeb *btree;
3928 + struct ip_set_iptreec *ctree;
3929 + struct ip_set_iptreed *dtree;
3930 + unsigned char a,b,c,d;
3931 + int ret = 0;
3932 +
3933 + *hash_ip = ip;
3934 + ABCD(a, b, c, d, hash_ip);
3935 + DP("%u %u %u %u timeout %u", a, b, c, d, timeout);
3936 + ADDIP_WALK(map, a, btree, struct ip_set_iptreeb, branch_cachep);
3937 + ADDIP_WALK(btree, b, ctree, struct ip_set_iptreec, branch_cachep);
3938 + ADDIP_WALK(ctree, c, dtree, struct ip_set_iptreed, leaf_cachep);
3939 + if (dtree->expires[d]
3940 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
3941 + ret = -EEXIST;
3942 + dtree->expires[d] = map->timeout ? (timeout * HZ + jiffies) : 1;
3943 + DP("%u %lu", d, dtree->expires[d]);
3944 + return ret;
3945 +}
3946 +
3947 +static int
3948 +addip(struct ip_set *set, const void *data, size_t size,
3949 + ip_set_ip_t *hash_ip)
3950 +{
3951 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3952 + struct ip_set_req_iptree *req =
3953 + (struct ip_set_req_iptree *) data;
3954 +
3955 + if (size != sizeof(struct ip_set_req_iptree)) {
3956 + ip_set_printk("data length wrong (want %zu, have %zu)",
3957 + sizeof(struct ip_set_req_iptree),
3958 + size);
3959 + return -EINVAL;
3960 + }
3961 + DP("%u.%u.%u.%u %u", HIPQUAD(req->ip), req->timeout);
3962 + return __addip(set, req->ip,
3963 + req->timeout ? req->timeout : map->timeout,
3964 + hash_ip);
3965 +}
3966 +
3967 +static int
3968 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
3969 + u_int32_t flags, ip_set_ip_t *hash_ip)
3970 +{
3971 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3972 +
3973 + return __addip(set,
3974 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
3975 + : skb->nh.iph->daddr),
3976 + map->timeout,
3977 + hash_ip);
3978 +}
3979 +
3980 +#define DELIP_WALK(map, elem, branch) do { \
3981 + if ((map)->tree[elem]) { \
3982 + branch = (map)->tree[elem]; \
3983 + } else \
3984 + return -EEXIST; \
3985 +} while (0)
3986 +
3987 +static inline int
3988 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3989 +{
3990 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
3991 + struct ip_set_iptreeb *btree;
3992 + struct ip_set_iptreec *ctree;
3993 + struct ip_set_iptreed *dtree;
3994 + unsigned char a,b,c,d;
3995 +
3996 + *hash_ip = ip;
3997 + ABCD(a, b, c, d, hash_ip);
3998 + DELIP_WALK(map, a, btree);
3999 + DELIP_WALK(btree, b, ctree);
4000 + DELIP_WALK(ctree, c, dtree);
4001 +
4002 + if (dtree->expires[d]) {
4003 + dtree->expires[d] = 0;
4004 + return 0;
4005 + }
4006 + return -EEXIST;
4007 +}
4008 +
4009 +static int
4010 +delip(struct ip_set *set, const void *data, size_t size,
4011 + ip_set_ip_t *hash_ip)
4012 +{
4013 + struct ip_set_req_iptree *req =
4014 + (struct ip_set_req_iptree *) data;
4015 +
4016 + if (size != sizeof(struct ip_set_req_iptree)) {
4017 + ip_set_printk("data length wrong (want %zu, have %zu)",
4018 + sizeof(struct ip_set_req_iptree),
4019 + size);
4020 + return -EINVAL;
4021 + }
4022 + return __delip(set, req->ip, hash_ip);
4023 +}
4024 +
4025 +static int
4026 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4027 + u_int32_t flags, ip_set_ip_t *hash_ip)
4028 +{
4029 + return __delip(set,
4030 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4031 + : skb->nh.iph->daddr),
4032 + hash_ip);
4033 +}
4034 +
4035 +#define LOOP_WALK_BEGIN(map, i, branch) \
4036 + for (i = 0; i < 255; i++) { \
4037 + if (!(map)->tree[i]) \
4038 + continue; \
4039 + branch = (map)->tree[i]
4040 +
4041 +#define LOOP_WALK_END }
4042 +
4043 +static void ip_tree_gc(unsigned long ul_set)
4044 +{
4045 + struct ip_set *set = (void *) ul_set;
4046 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4047 + struct ip_set_iptreeb *btree;
4048 + struct ip_set_iptreec *ctree;
4049 + struct ip_set_iptreed *dtree;
4050 + unsigned char a,b,c,d;
4051 + unsigned char i,j,k;
4052 +
4053 + i = j = k = 0;
4054 + DP("gc: %s", set->name);
4055 + write_lock_bh(&set->lock);
4056 + LOOP_WALK_BEGIN(map, a, btree);
4057 + LOOP_WALK_BEGIN(btree, b, ctree);
4058 + LOOP_WALK_BEGIN(ctree, c, dtree);
4059 + for (d = 0; d < 255; d++) {
4060 + if (dtree->expires[d]) {
4061 + DP("gc: %u %u %u %u: expires %lu jiffies %lu",
4062 + a, b, c, d,
4063 + dtree->expires[d], jiffies);
4064 + if (map->timeout
4065 + && time_before(dtree->expires[d], jiffies))
4066 + dtree->expires[d] = 0;
4067 + else
4068 + k = 1;
4069 + }
4070 + }
4071 + if (k == 0) {
4072 + DP("gc: %s: leaf %u %u %u empty",
4073 + set->name, a, b, c);
4074 + kmem_cache_free(leaf_cachep, dtree);
4075 + ctree->tree[c] = NULL;
4076 + } else {
4077 + DP("gc: %s: leaf %u %u %u not empty",
4078 + set->name, a, b, c);
4079 + j = 1;
4080 + k = 0;
4081 + }
4082 + LOOP_WALK_END;
4083 + if (j == 0) {
4084 + DP("gc: %s: branch %u %u empty",
4085 + set->name, a, b);
4086 + kmem_cache_free(branch_cachep, ctree);
4087 + btree->tree[b] = NULL;
4088 + } else {
4089 + DP("gc: %s: branch %u %u not empty",
4090 + set->name, a, b);
4091 + i = 1;
4092 + j = k = 0;
4093 + }
4094 + LOOP_WALK_END;
4095 + if (i == 0) {
4096 + DP("gc: %s: branch %u empty",
4097 + set->name, a);
4098 + kmem_cache_free(branch_cachep, btree);
4099 + map->tree[a] = NULL;
4100 + } else {
4101 + DP("gc: %s: branch %u not empty",
4102 + set->name, a);
4103 + i = j = k = 0;
4104 + }
4105 + LOOP_WALK_END;
4106 + write_unlock_bh(&set->lock);
4107 +
4108 + map->gc.expires = jiffies + map->gc_interval * HZ;
4109 + add_timer(&map->gc);
4110 +}
4111 +
4112 +static int create(struct ip_set *set, const void *data, size_t size)
4113 +{
4114 + struct ip_set_req_iptree_create *req =
4115 + (struct ip_set_req_iptree_create *) data;
4116 + struct ip_set_iptree *map;
4117 +
4118 + if (size != sizeof(struct ip_set_req_iptree_create)) {
4119 + ip_set_printk("data length wrong (want %zu, have %zu)",
4120 + sizeof(struct ip_set_req_iptree_create),
4121 + size);
4122 + return -EINVAL;
4123 + }
4124 +
4125 + map = kmalloc(sizeof(struct ip_set_iptree), GFP_KERNEL);
4126 + if (!map) {
4127 + DP("out of memory for %d bytes",
4128 + sizeof(struct ip_set_iptree));
4129 + return -ENOMEM;
4130 + }
4131 + memset(map, 0, sizeof(*map));
4132 + map->timeout = req->timeout;
4133 + set->data = map;
4134 +
4135 + /* If there is no timeout for the entries,
4136 + * we still have to call gc because delete
4137 + * do not clean up empty branches */
4138 + map->gc_interval = IPTREE_GC_TIME;
4139 + init_timer(&map->gc);
4140 + map->gc.data = (unsigned long) set;
4141 + map->gc.function = ip_tree_gc;
4142 + map->gc.expires = jiffies + map->gc_interval * HZ;
4143 + add_timer(&map->gc);
4144 +
4145 + return 0;
4146 +}
4147 +
4148 +static void __flush(struct ip_set_iptree *map)
4149 +{
4150 + struct ip_set_iptreeb *btree;
4151 + struct ip_set_iptreec *ctree;
4152 + struct ip_set_iptreed *dtree;
4153 + unsigned int a,b,c;
4154 +
4155 + LOOP_WALK_BEGIN(map, a, btree);
4156 + LOOP_WALK_BEGIN(btree, b, ctree);
4157 + LOOP_WALK_BEGIN(ctree, c, dtree);
4158 + kmem_cache_free(leaf_cachep, dtree);
4159 + LOOP_WALK_END;
4160 + kmem_cache_free(branch_cachep, ctree);
4161 + LOOP_WALK_END;
4162 + kmem_cache_free(branch_cachep, btree);
4163 + LOOP_WALK_END;
4164 +}
4165 +
4166 +static void destroy(struct ip_set *set)
4167 +{
4168 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4169 +
4170 + while (!del_timer(&map->gc))
4171 + msleep(IPTREE_DESTROY_SLEEP);
4172 + __flush(map);
4173 + kfree(map);
4174 + set->data = NULL;
4175 +}
4176 +
4177 +static void flush(struct ip_set *set)
4178 +{
4179 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4180 + unsigned int timeout = map->timeout;
4181 +
4182 + __flush(map);
4183 + memset(map, 0, sizeof(*map));
4184 + map->timeout = timeout;
4185 +}
4186 +
4187 +static void list_header(const struct ip_set *set, void *data)
4188 +{
4189 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4190 + struct ip_set_req_iptree_create *header =
4191 + (struct ip_set_req_iptree_create *) data;
4192 +
4193 + header->timeout = map->timeout;
4194 +}
4195 +
4196 +static int list_members_size(const struct ip_set *set)
4197 +{
4198 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4199 + struct ip_set_iptreeb *btree;
4200 + struct ip_set_iptreec *ctree;
4201 + struct ip_set_iptreed *dtree;
4202 + unsigned char a,b,c,d;
4203 + unsigned int count = 0;
4204 +
4205 + LOOP_WALK_BEGIN(map, a, btree);
4206 + LOOP_WALK_BEGIN(btree, b, ctree);
4207 + LOOP_WALK_BEGIN(ctree, c, dtree);
4208 + for (d = 0; d < 255; d++) {
4209 + if (dtree->expires[d]
4210 + && (!map->timeout || time_after(dtree->expires[d], jiffies)))
4211 + count++;
4212 + }
4213 + LOOP_WALK_END;
4214 + LOOP_WALK_END;
4215 + LOOP_WALK_END;
4216 +
4217 + DP("members %u", count);
4218 + return (count * sizeof(struct ip_set_req_iptree));
4219 +}
4220 +
4221 +static void list_members(const struct ip_set *set, void *data)
4222 +{
4223 + struct ip_set_iptree *map = (struct ip_set_iptree *) set->data;
4224 + struct ip_set_iptreeb *btree;
4225 + struct ip_set_iptreec *ctree;
4226 + struct ip_set_iptreed *dtree;
4227 + unsigned char a,b,c,d;
4228 + size_t offset = 0;
4229 + struct ip_set_req_iptree *entry;
4230 +
4231 + LOOP_WALK_BEGIN(map, a, btree);
4232 + LOOP_WALK_BEGIN(btree, b, ctree);
4233 + LOOP_WALK_BEGIN(ctree, c, dtree);
4234 + for (d = 0; d < 255; d++) {
4235 + if (dtree->expires[d]
4236 + && (!map->timeout || time_after(dtree->expires[d], jiffies))) {
4237 + entry = (struct ip_set_req_iptree *)(data + offset);
4238 + entry->ip = ((a << 24) | (b << 16) | (c << 8) | d);
4239 + entry->timeout = !map->timeout ? 0
4240 + : (dtree->expires[d] - jiffies)/HZ;
4241 + offset += sizeof(struct ip_set_req_iptree);
4242 + }
4243 + }
4244 + LOOP_WALK_END;
4245 + LOOP_WALK_END;
4246 + LOOP_WALK_END;
4247 +}
4248 +
4249 +static struct ip_set_type ip_set_iptree = {
4250 + .typename = SETTYPE_NAME,
4251 + .typecode = IPSET_TYPE_IP,
4252 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4253 + .create = &create,
4254 + .destroy = &destroy,
4255 + .flush = &flush,
4256 + .reqsize = sizeof(struct ip_set_req_iptree),
4257 + .addip = &addip,
4258 + .addip_kernel = &addip_kernel,
4259 + .delip = &delip,
4260 + .delip_kernel = &delip_kernel,
4261 + .testip = &testip,
4262 + .testip_kernel = &testip_kernel,
4263 + .header_size = sizeof(struct ip_set_req_iptree_create),
4264 + .list_header = &list_header,
4265 + .list_members_size = &list_members_size,
4266 + .list_members = &list_members,
4267 + .me = THIS_MODULE,
4268 +};
4269 +
4270 +MODULE_LICENSE("GPL");
4271 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4272 +MODULE_DESCRIPTION("iptree type of IP sets");
4273 +
4274 +static int __init init(void)
4275 +{
4276 + int ret;
4277 +
4278 + branch_cachep = kmem_cache_create("ip_set_iptreeb",
4279 + sizeof(struct ip_set_iptreeb),
4280 + 0, 0, NULL, NULL);
4281 + if (!branch_cachep) {
4282 + printk(KERN_ERR "Unable to create ip_set_iptreeb slab cache\n");
4283 + ret = -ENOMEM;
4284 + goto out;
4285 + }
4286 + leaf_cachep = kmem_cache_create("ip_set_iptreed",
4287 + sizeof(struct ip_set_iptreed),
4288 + 0, 0, NULL, NULL);
4289 + if (!leaf_cachep) {
4290 + printk(KERN_ERR "Unable to create ip_set_iptreed slab cache\n");
4291 + ret = -ENOMEM;
4292 + goto free_branch;
4293 + }
4294 + ret = ip_set_register_set_type(&ip_set_iptree);
4295 + if (ret == 0)
4296 + goto out;
4297 +
4298 + kmem_cache_destroy(leaf_cachep);
4299 + free_branch:
4300 + kmem_cache_destroy(branch_cachep);
4301 + out:
4302 + return ret;
4303 +}
4304 +
4305 +static void __exit fini(void)
4306 +{
4307 + /* FIXME: possible race with ip_set_create() */
4308 + ip_set_unregister_set_type(&ip_set_iptree);
4309 + kmem_cache_destroy(leaf_cachep);
4310 + kmem_cache_destroy(branch_cachep);
4311 +}
4312 +
4313 +module_init(init);
4314 +module_exit(fini);
4315 --- /dev/null
4316 +++ b/net/ipv4/netfilter/ip_set_macipmap.c
4317 @@ -0,0 +1,340 @@
4318 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
4319 + * Patrick Schaaf <bof@bof.de>
4320 + * Martin Josefsson <gandalf@wlug.westbo.se>
4321 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4322 + *
4323 + * This program is free software; you can redistribute it and/or modify
4324 + * it under the terms of the GNU General Public License version 2 as
4325 + * published by the Free Software Foundation.
4326 + */
4327 +
4328 +/* Kernel module implementing an IP set type: the macipmap type */
4329 +
4330 +#include <linux/module.h>
4331 +#include <linux/ip.h>
4332 +#include <linux/skbuff.h>
4333 +#include <linux/netfilter_ipv4/ip_tables.h>
4334 +#include <linux/netfilter_ipv4/ip_set.h>
4335 +#include <linux/errno.h>
4336 +#include <asm/uaccess.h>
4337 +#include <asm/bitops.h>
4338 +#include <asm/softirq.h>
4339 +#include <linux/spinlock.h>
4340 +#include <linux/if_ether.h>
4341 +#include <linux/vmalloc.h>
4342 +
4343 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4344 +#include <linux/netfilter_ipv4/ip_set_macipmap.h>
4345 +
4346 +static int
4347 +testip(struct ip_set *set, const void *data, size_t size, ip_set_ip_t *hash_ip)
4348 +{
4349 + struct ip_set_macipmap *map = (struct ip_set_macipmap *) set->data;
4350 + struct ip_set_macip *table = (struct ip_set_macip *) map->members;
4351 + struct ip_set_req_macipmap *req = (struct ip_set_req_macipmap *) data;
4352 +
4353 + if (size != sizeof(struct ip_set_req_macipmap)) {
4354 + ip_set_printk("data length wrong (want %zu, have %zu)",
4355 + sizeof(struct ip_set_req_macipmap),
4356 + size);
4357 + return -EINVAL;
4358 + }
4359 +
4360 + if (req->ip < map->first_ip || req->ip > map->last_ip)
4361 + return -ERANGE;
4362 +
4363 + *hash_ip = req->ip;
4364 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4365 + set->name, HIPQUAD(req->ip), HIPQUAD(*hash_ip));
4366 + if (test_bit(IPSET_MACIP_ISSET,
4367 + &table[req->ip - map->first_ip].flags)) {
4368 + return (memcmp(req->ethernet,
4369 + &table[req->ip - map->first_ip].ethernet,
4370 + ETH_ALEN) == 0);
4371 + } else {
4372 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4373 + }
4374 +}
4375 +
4376 +static int
4377 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4378 + u_int32_t flags, ip_set_ip_t *hash_ip)
4379 +{
4380 + struct ip_set_macipmap *map =
4381 + (struct ip_set_macipmap *) set->data;
4382 + struct ip_set_macip *table =
4383 + (struct ip_set_macip *) map->members;
4384 + ip_set_ip_t ip;
4385 +
4386 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4387 + : skb->nh.iph->daddr);
4388 + DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
4389 + flags & IPSET_SRC ? "SRC" : "DST",
4390 + NIPQUAD(skb->nh.iph->saddr),
4391 + NIPQUAD(skb->nh.iph->daddr));
4392 +
4393 + if (ip < map->first_ip || ip > map->last_ip)
4394 + return 0;
4395 +
4396 + *hash_ip = ip;
4397 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u",
4398 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip));
4399 + if (test_bit(IPSET_MACIP_ISSET, &table[ip - map->first_ip].flags)) {
4400 + /* Is mac pointer valid?
4401 + * If so, compare... */
4402 + return (skb->mac.raw >= skb->head
4403 + && (skb->mac.raw + ETH_HLEN) <= skb->data
4404 + && (memcmp(skb->mac.ethernet->h_source,
4405 + &table[ip - map->first_ip].ethernet,
4406 + ETH_ALEN) == 0));
4407 + } else {
4408 + return (map->flags & IPSET_MACIP_MATCHUNSET ? 1 : 0);
4409 + }
4410 +}
4411 +
4412 +/* returns 0 on success */
4413 +static inline int
4414 +__addip(struct ip_set *set,
4415 + ip_set_ip_t ip, unsigned char *ethernet, ip_set_ip_t *hash_ip)
4416 +{
4417 + struct ip_set_macipmap *map =
4418 + (struct ip_set_macipmap *) set->data;
4419 + struct ip_set_macip *table =
4420 + (struct ip_set_macip *) map->members;
4421 +
4422 + if (ip < map->first_ip || ip > map->last_ip)
4423 + return -ERANGE;
4424 + if (test_and_set_bit(IPSET_MACIP_ISSET,
4425 + &table[ip - map->first_ip].flags))
4426 + return -EEXIST;
4427 +
4428 + *hash_ip = ip;
4429 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4430 + memcpy(&table[ip - map->first_ip].ethernet, ethernet, ETH_ALEN);
4431 + return 0;
4432 +}
4433 +
4434 +static int
4435 +addip(struct ip_set *set, const void *data, size_t size,
4436 + ip_set_ip_t *hash_ip)
4437 +{
4438 + struct ip_set_req_macipmap *req =
4439 + (struct ip_set_req_macipmap *) data;
4440 +
4441 + if (size != sizeof(struct ip_set_req_macipmap)) {
4442 + ip_set_printk("data length wrong (want %zu, have %zu)",
4443 + sizeof(struct ip_set_req_macipmap),
4444 + size);
4445 + return -EINVAL;
4446 + }
4447 + return __addip(set, req->ip, req->ethernet, hash_ip);
4448 +}
4449 +
4450 +static int
4451 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4452 + u_int32_t flags, ip_set_ip_t *hash_ip)
4453 +{
4454 + ip_set_ip_t ip;
4455 +
4456 + ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4457 + : skb->nh.iph->daddr);
4458 +
4459 + if (!(skb->mac.raw >= skb->head
4460 + && (skb->mac.raw + ETH_HLEN) <= skb->data))
4461 + return -EINVAL;
4462 +
4463 + return __addip(set, ip, skb->mac.ethernet->h_source, hash_ip);
4464 +}
4465 +
4466 +static inline int
4467 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4468 +{
4469 + struct ip_set_macipmap *map =
4470 + (struct ip_set_macipmap *) set->data;
4471 + struct ip_set_macip *table =
4472 + (struct ip_set_macip *) map->members;
4473 +
4474 + if (ip < map->first_ip || ip > map->last_ip)
4475 + return -ERANGE;
4476 + if (!test_and_clear_bit(IPSET_MACIP_ISSET,
4477 + &table[ip - map->first_ip].flags))
4478 + return -EEXIST;
4479 +
4480 + *hash_ip = ip;
4481 + DP("%u.%u.%u.%u, %u.%u.%u.%u", HIPQUAD(ip), HIPQUAD(*hash_ip));
4482 + return 0;
4483 +}
4484 +
4485 +static int
4486 +delip(struct ip_set *set, const void *data, size_t size,
4487 + ip_set_ip_t *hash_ip)
4488 +{
4489 + struct ip_set_req_macipmap *req =
4490 + (struct ip_set_req_macipmap *) data;
4491 +
4492 + if (size != sizeof(struct ip_set_req_macipmap)) {
4493 + ip_set_printk("data length wrong (want %zu, have %zu)",
4494 + sizeof(struct ip_set_req_macipmap),
4495 + size);
4496 + return -EINVAL;
4497 + }
4498 + return __delip(set, req->ip, hash_ip);
4499 +}
4500 +
4501 +static int
4502 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4503 + u_int32_t flags, ip_set_ip_t *hash_ip)
4504 +{
4505 + return __delip(set,
4506 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4507 + : skb->nh.iph->daddr),
4508 + hash_ip);
4509 +}
4510 +
4511 +static inline size_t members_size(ip_set_id_t from, ip_set_id_t to)
4512 +{
4513 + return (size_t)((to - from + 1) * sizeof(struct ip_set_macip));
4514 +}
4515 +
4516 +static int create(struct ip_set *set, const void *data, size_t size)
4517 +{
4518 + int newbytes;
4519 + struct ip_set_req_macipmap_create *req =
4520 + (struct ip_set_req_macipmap_create *) data;
4521 + struct ip_set_macipmap *map;
4522 +
4523 + if (size != sizeof(struct ip_set_req_macipmap_create)) {
4524 + ip_set_printk("data length wrong (want %zu, have %zu)",
4525 + sizeof(struct ip_set_req_macipmap_create),
4526 + size);
4527 + return -EINVAL;
4528 + }
4529 +
4530 + DP("from %u.%u.%u.%u to %u.%u.%u.%u",
4531 + HIPQUAD(req->from), HIPQUAD(req->to));
4532 +
4533 + if (req->from > req->to) {
4534 + DP("bad ip range");
4535 + return -ENOEXEC;
4536 + }
4537 +
4538 + if (req->to - req->from > MAX_RANGE) {
4539 + ip_set_printk("range too big (max %d addresses)",
4540 + MAX_RANGE);
4541 + return -ENOEXEC;
4542 + }
4543 +
4544 + map = kmalloc(sizeof(struct ip_set_macipmap), GFP_KERNEL);
4545 + if (!map) {
4546 + DP("out of memory for %d bytes",
4547 + sizeof(struct ip_set_macipmap));
4548 + return -ENOMEM;
4549 + }
4550 + map->flags = req->flags;
4551 + map->first_ip = req->from;
4552 + map->last_ip = req->to;
4553 + newbytes = members_size(map->first_ip, map->last_ip);
4554 + map->members = ip_set_malloc(newbytes);
4555 + DP("members: %u %p", newbytes, map->members);
4556 + if (!map->members) {
4557 + DP("out of memory for %d bytes", newbytes);
4558 + kfree(map);
4559 + return -ENOMEM;
4560 + }
4561 + memset(map->members, 0, newbytes);
4562 +
4563 + set->data = map;
4564 + return 0;
4565 +}
4566 +
4567 +static void destroy(struct ip_set *set)
4568 +{
4569 + struct ip_set_macipmap *map =
4570 + (struct ip_set_macipmap *) set->data;
4571 +
4572 + ip_set_free(map->members, members_size(map->first_ip, map->last_ip));
4573 + kfree(map);
4574 +
4575 + set->data = NULL;
4576 +}
4577 +
4578 +static void flush(struct ip_set *set)
4579 +{
4580 + struct ip_set_macipmap *map =
4581 + (struct ip_set_macipmap *) set->data;
4582 + memset(map->members, 0, members_size(map->first_ip, map->last_ip));
4583 +}
4584 +
4585 +static void list_header(const struct ip_set *set, void *data)
4586 +{
4587 + struct ip_set_macipmap *map =
4588 + (struct ip_set_macipmap *) set->data;
4589 + struct ip_set_req_macipmap_create *header =
4590 + (struct ip_set_req_macipmap_create *) data;
4591 +
4592 + DP("list_header %x %x %u", map->first_ip, map->last_ip,
4593 + map->flags);
4594 +
4595 + header->from = map->first_ip;
4596 + header->to = map->last_ip;
4597 + header->flags = map->flags;
4598 +}
4599 +
4600 +static int list_members_size(const struct ip_set *set)
4601 +{
4602 + struct ip_set_macipmap *map =
4603 + (struct ip_set_macipmap *) set->data;
4604 +
4605 + DP("%u", members_size(map->first_ip, map->last_ip));
4606 + return members_size(map->first_ip, map->last_ip);
4607 +}
4608 +
4609 +static void list_members(const struct ip_set *set, void *data)
4610 +{
4611 + struct ip_set_macipmap *map =
4612 + (struct ip_set_macipmap *) set->data;
4613 +
4614 + int bytes = members_size(map->first_ip, map->last_ip);
4615 +
4616 + DP("members: %u %p", bytes, map->members);
4617 + memcpy(data, map->members, bytes);
4618 +}
4619 +
4620 +static struct ip_set_type ip_set_macipmap = {
4621 + .typename = SETTYPE_NAME,
4622 + .typecode = IPSET_TYPE_IP,
4623 + .protocol_version = IP_SET_PROTOCOL_VERSION,
4624 + .create = &create,
4625 + .destroy = &destroy,
4626 + .flush = &flush,
4627 + .reqsize = sizeof(struct ip_set_req_macipmap),
4628 + .addip = &addip,
4629 + .addip_kernel = &addip_kernel,
4630 + .delip = &delip,
4631 + .delip_kernel = &delip_kernel,
4632 + .testip = &testip,
4633 + .testip_kernel = &testip_kernel,
4634 + .header_size = sizeof(struct ip_set_req_macipmap_create),
4635 + .list_header = &list_header,
4636 + .list_members_size = &list_members_size,
4637 + .list_members = &list_members,
4638 + .me = THIS_MODULE,
4639 +};
4640 +
4641 +MODULE_LICENSE("GPL");
4642 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
4643 +MODULE_DESCRIPTION("macipmap type of IP sets");
4644 +
4645 +static int __init init(void)
4646 +{
4647 + return ip_set_register_set_type(&ip_set_macipmap);
4648 +}
4649 +
4650 +static void __exit fini(void)
4651 +{
4652 + /* FIXME: possible race with ip_set_create() */
4653 + ip_set_unregister_set_type(&ip_set_macipmap);
4654 +}
4655 +
4656 +module_init(init);
4657 +module_exit(fini);
4658 --- /dev/null
4659 +++ b/net/ipv4/netfilter/ip_set_nethash.c
4660 @@ -0,0 +1,450 @@
4661 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
4662 + *
4663 + * This program is free software; you can redistribute it and/or modify
4664 + * it under the terms of the GNU General Public License version 2 as
4665 + * published by the Free Software Foundation.
4666 + */
4667 +
4668 +/* Kernel module implementing a cidr nethash set */
4669 +
4670 +#include <linux/module.h>
4671 +#include <linux/ip.h>
4672 +#include <linux/skbuff.h>
4673 +#include <linux/netfilter_ipv4/ip_tables.h>
4674 +#include <linux/netfilter_ipv4/ip_set.h>
4675 +#include <linux/errno.h>
4676 +#include <asm/uaccess.h>
4677 +#include <asm/bitops.h>
4678 +#include <asm/softirq.h>
4679 +#include <linux/spinlock.h>
4680 +#include <linux/vmalloc.h>
4681 +#include <linux/random.h>
4682 +
4683 +#include <net/ip.h>
4684 +
4685 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
4686 +#include <linux/netfilter_ipv4/ip_set_nethash.h>
4687 +#include <linux/netfilter_ipv4/ip_set_jhash.h>
4688 +#include <linux/netfilter_ipv4/ip_set_prime.h>
4689 +
4690 +static inline __u32
4691 +jhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4692 +{
4693 + return jhash_1word(ip, map->initval);
4694 +}
4695 +
4696 +static inline __u32
4697 +randhash_ip(const struct ip_set_nethash *map, ip_set_ip_t ip)
4698 +{
4699 + return (1 + ip % map->prime);
4700 +}
4701 +
4702 +static inline __u32
4703 +hash_id_cidr(struct ip_set_nethash *map,
4704 + ip_set_ip_t ip,
4705 + unsigned char cidr,
4706 + ip_set_ip_t *hash_ip)
4707 +{
4708 + __u32 jhash, randhash, id;
4709 + u_int16_t i;
4710 +
4711 + *hash_ip = pack(ip, cidr);
4712 + jhash = jhash_ip(map, *hash_ip);
4713 + randhash = randhash_ip(map, *hash_ip);
4714 +
4715 + for (i = 0; i < map->probes; i++) {
4716 + id = (jhash + i * randhash) % map->hashsize;
4717 + DP("hash key: %u", id);
4718 + if (map->members[id] == *hash_ip)
4719 + return id;
4720 + }
4721 + return UINT_MAX;
4722 +}
4723 +
4724 +static inline __u32
4725 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4726 +{
4727 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4728 + __u32 id = UINT_MAX;
4729 + int i;
4730 +
4731 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4732 + id = hash_id_cidr(map, ip, map->cidr[i], hash_ip);
4733 + if (id != UINT_MAX)
4734 + break;
4735 + }
4736 + return id;
4737 +}
4738 +
4739 +static inline int
4740 +__testip_cidr(struct ip_set *set, ip_set_ip_t ip, unsigned char cidr,
4741 + ip_set_ip_t *hash_ip)
4742 +{
4743 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4744 +
4745 + return (hash_id_cidr(map, ip, cidr, hash_ip) != UINT_MAX);
4746 +}
4747 +
4748 +static inline int
4749 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
4750 +{
4751 + return (hash_id(set, ip, hash_ip) != UINT_MAX);
4752 +}
4753 +
4754 +static int
4755 +testip(struct ip_set *set, const void *data, size_t size,
4756 + ip_set_ip_t *hash_ip)
4757 +{
4758 + struct ip_set_req_nethash *req =
4759 + (struct ip_set_req_nethash *) data;
4760 +
4761 + if (size != sizeof(struct ip_set_req_nethash)) {
4762 + ip_set_printk("data length wrong (want %zu, have %zu)",
4763 + sizeof(struct ip_set_req_nethash),
4764 + size);
4765 + return -EINVAL;
4766 + }
4767 + return (req->cidr == 32 ? __testip(set, req->ip, hash_ip)
4768 + : __testip_cidr(set, req->ip, req->cidr, hash_ip));
4769 +}
4770 +
4771 +static int
4772 +testip_kernel(struct ip_set *set, const struct sk_buff *skb,
4773 + u_int32_t flags, ip_set_ip_t *hash_ip)
4774 +{
4775 + return __testip(set,
4776 + ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4777 + : skb->nh.iph->daddr),
4778 + hash_ip);
4779 +}
4780 +
4781 +static inline int
4782 +__addip_base(struct ip_set_nethash *map, ip_set_ip_t ip)
4783 +{
4784 + __u32 jhash, randhash, probe;
4785 + u_int16_t i;
4786 +
4787 + jhash = jhash_ip(map, ip);
4788 + randhash = randhash_ip(map, ip);
4789 +
4790 + for (i = 0; i < map->probes; i++) {
4791 + probe = (jhash + i * randhash) % map->hashsize;
4792 + if (map->members[probe] == ip)
4793 + return -EEXIST;
4794 + if (!map->members[probe]) {
4795 + map->members[probe] = ip;
4796 + return 0;
4797 + }
4798 + }
4799 + /* Trigger rehashing */
4800 + return -EAGAIN;
4801 +}
4802 +
4803 +static inline int
4804 +__addip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4805 + ip_set_ip_t *hash_ip)
4806 +{
4807 + *hash_ip = pack(ip, cidr);
4808 + DP("%u.%u.%u.%u/%u, %u.%u.%u.%u", HIPQUAD(ip), cidr, HIPQUAD(*hash_ip));
4809 +
4810 + return __addip_base(map, *hash_ip);
4811 +}
4812 +
4813 +static void
4814 +update_cidr_sizes(struct ip_set_nethash *map, unsigned char cidr)
4815 +{
4816 + unsigned char next;
4817 + int i;
4818 +
4819 + for (i = 0; i < 30 && map->cidr[i]; i++) {
4820 + if (map->cidr[i] == cidr) {
4821 + return;
4822 + } else if (map->cidr[i] < cidr) {
4823 + next = map->cidr[i];
4824 + map->cidr[i] = cidr;
4825 + cidr = next;
4826 + }
4827 + }
4828 + if (i < 30)
4829 + map->cidr[i] = cidr;
4830 +}
4831 +
4832 +static int
4833 +addip(struct ip_set *set, const void *data, size_t size,
4834 + ip_set_ip_t *hash_ip)
4835 +{
4836 + struct ip_set_req_nethash *req =
4837 + (struct ip_set_req_nethash *) data;
4838 + int ret;
4839 +
4840 + if (size != sizeof(struct ip_set_req_nethash)) {
4841 + ip_set_printk("data length wrong (want %zu, have %zu)",
4842 + sizeof(struct ip_set_req_nethash),
4843 + size);
4844 + return -EINVAL;
4845 + }
4846 + ret = __addip((struct ip_set_nethash *) set->data,
4847 + req->ip, req->cidr, hash_ip);
4848 +
4849 + if (ret == 0)
4850 + update_cidr_sizes((struct ip_set_nethash *) set->data,
4851 + req->cidr);
4852 +
4853 + return ret;
4854 +}
4855 +
4856 +static int
4857 +addip_kernel(struct ip_set *set, const struct sk_buff *skb,
4858 + u_int32_t flags, ip_set_ip_t *hash_ip)
4859 +{
4860 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4861 + int ret = -ERANGE;
4862 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4863 + : skb->nh.iph->daddr);
4864 +
4865 + if (map->cidr[0])
4866 + ret = __addip(map, ip, map->cidr[0], hash_ip);
4867 +
4868 + return ret;
4869 +}
4870 +
4871 +static int retry(struct ip_set *set)
4872 +{
4873 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4874 + ip_set_ip_t *members;
4875 + u_int32_t i, hashsize;
4876 + unsigned newbytes;
4877 + int res;
4878 + struct ip_set_nethash tmp = {
4879 + .hashsize = map->hashsize,
4880 + .probes = map->probes,
4881 + .resize = map->resize
4882 + };
4883 +
4884 + if (map->resize == 0)
4885 + return -ERANGE;
4886 +
4887 + memcpy(tmp.cidr, map->cidr, 30 * sizeof(unsigned char));
4888 + again:
4889 + res = 0;
4890 +
4891 + /* Calculate new parameters */
4892 + get_random_bytes(&tmp.initval, 4);
4893 + hashsize = tmp.hashsize + (tmp.hashsize * map->resize)/100;
4894 + if (hashsize == tmp.hashsize)
4895 + hashsize++;
4896 + tmp.prime = make_prime(hashsize);
4897 +
4898 + ip_set_printk("rehashing of set %s triggered: "
4899 + "hashsize grows from %u to %u",
4900 + set->name, tmp.hashsize, hashsize);
4901 + tmp.hashsize = hashsize;
4902 +
4903 + newbytes = hashsize * sizeof(ip_set_ip_t);
4904 + tmp.members = ip_set_malloc(newbytes);
4905 + if (!tmp.members) {
4906 + DP("out of memory for %d bytes", newbytes);
4907 + return -ENOMEM;
4908 + }
4909 + memset(tmp.members, 0, newbytes);
4910 +
4911 + write_lock_bh(&set->lock);
4912 + map = (struct ip_set_nethash *) set->data; /* Play safe */
4913 + for (i = 0; i < map->hashsize && res == 0; i++) {
4914 + if (map->members[i])
4915 + res = __addip_base(&tmp, map->members[i]);
4916 + }
4917 + if (res) {
4918 + /* Failure, try again */
4919 + write_unlock_bh(&set->lock);
4920 + ip_set_free(tmp.members, newbytes);
4921 + goto again;
4922 + }
4923 +
4924 + /* Success at resizing! */
4925 + members = map->members;
4926 + hashsize = map->hashsize;
4927 +
4928 + map->initval = tmp.initval;
4929 + map->prime = tmp.prime;
4930 + map->hashsize = tmp.hashsize;
4931 + map->members = tmp.members;
4932 + write_unlock_bh(&set->lock);
4933 +
4934 + ip_set_free(members, hashsize * sizeof(ip_set_ip_t));
4935 +
4936 + return 0;
4937 +}
4938 +
4939 +static inline int
4940 +__delip(struct ip_set_nethash *map, ip_set_ip_t ip, unsigned char cidr,
4941 + ip_set_ip_t *hash_ip)
4942 +{
4943 + ip_set_ip_t id = hash_id_cidr(map, ip, cidr, hash_ip);
4944 +
4945 + if (id == UINT_MAX)
4946 + return -EEXIST;
4947 +
4948 + map->members[id] = 0;
4949 + return 0;
4950 +}
4951 +
4952 +static int
4953 +delip(struct ip_set *set, const void *data, size_t size,
4954 + ip_set_ip_t *hash_ip)
4955 +{
4956 + struct ip_set_req_nethash *req =
4957 + (struct ip_set_req_nethash *) data;
4958 +
4959 + if (size != sizeof(struct ip_set_req_nethash)) {
4960 + ip_set_printk("data length wrong (want %zu, have %zu)",
4961 + sizeof(struct ip_set_req_nethash),
4962 + size);
4963 + return -EINVAL;
4964 + }
4965 + /* TODO: no garbage collection in map->cidr */
4966 + return __delip((struct ip_set_nethash *) set->data,
4967 + req->ip, req->cidr, hash_ip);
4968 +}
4969 +
4970 +static int
4971 +delip_kernel(struct ip_set *set, const struct sk_buff *skb,
4972 + u_int32_t flags, ip_set_ip_t *hash_ip)
4973 +{
4974 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
4975 + int ret = -ERANGE;
4976 + ip_set_ip_t ip = ntohl(flags & IPSET_SRC ? skb->nh.iph->saddr
4977 + : skb->nh.iph->daddr);
4978 +
4979 + if (map->cidr[0])
4980 + ret = __delip(map, ip, map->cidr[0], hash_ip);
4981 +
4982 + return ret;
4983 +}
4984 +
4985 +static int create(struct ip_set *set, const void *data, size_t size)
4986 +{
4987 + unsigned newbytes;
4988 + struct ip_set_req_nethash_create *req =
4989 + (struct ip_set_req_nethash_create *) data;
4990 + struct ip_set_nethash *map;
4991 +
4992 + if (size != sizeof(struct ip_set_req_nethash_create)) {
4993 + ip_set_printk("data length wrong (want %zu, have %zu)",
4994 + sizeof(struct ip_set_req_nethash_create),
4995 + size);
4996 + return -EINVAL;
4997 + }
4998 +
4999 + if (req->hashsize < 1) {
5000 + ip_set_printk("hashsize too small");
5001 + return -ENOEXEC;
5002 + }
5003 +
5004 + map = kmalloc(sizeof(struct ip_set_nethash), GFP_KERNEL);
5005 + if (!map) {
5006 + DP("out of memory for %d bytes",
5007 + sizeof(struct ip_set_nethash));
5008 + return -ENOMEM;
5009 + }
5010 + get_random_bytes(&map->initval, 4);
5011 + map->prime = make_prime(req->hashsize);
5012 + map->hashsize = req->hashsize;
5013 + map->probes = req->probes;
5014 + map->resize = req->resize;
5015 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
5016 + newbytes = map->hashsize * sizeof(ip_set_ip_t);
5017 + map->members = ip_set_malloc(newbytes);
5018 + if (!map->members) {
5019 + DP("out of memory for %d bytes", newbytes);
5020 + kfree(map);
5021 + return -ENOMEM;
5022 + }
5023 + memset(map->members, 0, newbytes);
5024 +
5025 + set->data = map;
5026 + return 0;
5027 +}
5028 +
5029 +static void destroy(struct ip_set *set)
5030 +{
5031 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5032 +
5033 + ip_set_free(map->members, map->hashsize * sizeof(ip_set_ip_t));
5034 + kfree(map);
5035 +
5036 + set->data = NULL;
5037 +}
5038 +
5039 +static void flush(struct ip_set *set)
5040 +{
5041 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5042 + memset(map->members, 0, map->hashsize * sizeof(ip_set_ip_t));
5043 + memset(map->cidr, 0, 30 * sizeof(unsigned char));
5044 +}
5045 +
5046 +static void list_header(const struct ip_set *set, void *data)
5047 +{
5048 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5049 + struct ip_set_req_nethash_create *header =
5050 + (struct ip_set_req_nethash_create *) data;
5051 +
5052 + header->hashsize = map->hashsize;
5053 + header->probes = map->probes;
5054 + header->resize = map->resize;
5055 +}
5056 +
5057 +static int list_members_size(const struct ip_set *set)
5058 +{
5059 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5060 +
5061 + return (map->hashsize * sizeof(ip_set_ip_t));
5062 +}
5063 +
5064 +static void list_members(const struct ip_set *set, void *data)
5065 +{
5066 + struct ip_set_nethash *map = (struct ip_set_nethash *) set->data;
5067 + int bytes = map->hashsize * sizeof(ip_set_ip_t);
5068 +
5069 + memcpy(data, map->members, bytes);
5070 +}
5071 +
5072 +static struct ip_set_type ip_set_nethash = {
5073 + .typename = SETTYPE_NAME,
5074 + .typecode = IPSET_TYPE_IP,
5075 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5076 + .create = &create,
5077 + .destroy = &destroy,
5078 + .flush = &flush,
5079 + .reqsize = sizeof(struct ip_set_req_nethash),
5080 + .addip = &addip,
5081 + .addip_kernel = &addip_kernel,
5082 + .retry = &retry,
5083 + .delip = &delip,
5084 + .delip_kernel = &delip_kernel,
5085 + .testip = &testip,
5086 + .testip_kernel = &testip_kernel,
5087 + .header_size = sizeof(struct ip_set_req_nethash_create),
5088 + .list_header = &list_header,
5089 + .list_members_size = &list_members_size,
5090 + .list_members = &list_members,
5091 + .me = THIS_MODULE,
5092 +};
5093 +
5094 +MODULE_LICENSE("GPL");
5095 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5096 +MODULE_DESCRIPTION("nethash type of IP sets");
5097 +
5098 +static int __init init(void)
5099 +{
5100 + return ip_set_register_set_type(&ip_set_nethash);
5101 +}
5102 +
5103 +static void __exit fini(void)
5104 +{
5105 + /* FIXME: possible race with ip_set_create() */
5106 + ip_set_unregister_set_type(&ip_set_nethash);
5107 +}
5108 +
5109 +module_init(init);
5110 +module_exit(fini);
5111 --- /dev/null
5112 +++ b/net/ipv4/netfilter/ip_set_portmap.c
5113 @@ -0,0 +1,321 @@
5114 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5115 + *
5116 + * This program is free software; you can redistribute it and/or modify
5117 + * it under the terms of the GNU General Public License version 2 as
5118 + * published by the Free Software Foundation.
5119 + */
5120 +
5121 +/* Kernel module implementing a port set type as a bitmap */
5122 +
5123 +#include <linux/module.h>
5124 +#include <linux/ip.h>
5125 +#include <linux/tcp.h>
5126 +#include <linux/udp.h>
5127 +#include <linux/skbuff.h>
5128 +#include <linux/netfilter_ipv4/ip_tables.h>
5129 +#include <linux/netfilter_ipv4/ip_set.h>
5130 +#include <linux/errno.h>
5131 +#include <asm/uaccess.h>
5132 +#include <asm/bitops.h>
5133 +#include <asm/softirq.h>
5134 +#include <linux/spinlock.h>
5135 +
5136 +#include <net/ip.h>
5137 +
5138 +#include <linux/netfilter_ipv4/ip_set_portmap.h>
5139 +
5140 +static inline ip_set_ip_t
5141 +get_port(const struct sk_buff *skb, u_int32_t flags)
5142 +{
5143 + struct iphdr *iph = skb->nh.iph;
5144 + u_int16_t offset = ntohs(iph->frag_off) & IP_OFFSET;
5145 +
5146 + switch (iph->protocol) {
5147 + case IPPROTO_TCP: {
5148 + struct tcphdr *tcph = (struct tcphdr *)((u_int32_t *)iph + iph->ihl);
5149 +
5150 + /* See comments at tcp_match in ip_tables.c */
5151 + if (offset != 0
5152 + || (offset == 0
5153 + && (skb->len - iph->ihl * 4) < sizeof(struct tcphdr)))
5154 + return INVALID_PORT;
5155 +
5156 + return ntohs(flags & IPSET_SRC ?
5157 + tcph->source : tcph->dest);
5158 + }
5159 + case IPPROTO_UDP: {
5160 + struct udphdr *udph = (struct udphdr *)((u_int32_t *)iph + iph->ihl);
5161 +
5162 + if (offset != 0
5163 + || (offset == 0
5164 + && (skb->len - iph->ihl * 4) < sizeof(struct udphdr)))
5165 + return INVALID_PORT;
5166 +
5167 + return ntohs(flags & IPSET_SRC ?
5168 + udph->source : udph->dest);
5169 + }
5170 + default:
5171 + return INVALID_PORT;
5172 + }
5173 +}
5174 +
5175 +static inline int
5176 +__testport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5177 +{
5178 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5179 +
5180 + if (port < map->first_port || port > map->last_port)
5181 + return -ERANGE;
5182 +
5183 + *hash_port = port;
5184 + DP("set: %s, port:%u, %u", set->name, port, *hash_port);
5185 + return !!test_bit(port - map->first_port, map->members);
5186 +}
5187 +
5188 +static int
5189 +testport(struct ip_set *set, const void *data, size_t size,
5190 + ip_set_ip_t *hash_port)
5191 +{
5192 + struct ip_set_req_portmap *req =
5193 + (struct ip_set_req_portmap *) data;
5194 +
5195 + if (size != sizeof(struct ip_set_req_portmap)) {
5196 + ip_set_printk("data length wrong (want %zu, have %zu)",
5197 + sizeof(struct ip_set_req_portmap),
5198 + size);
5199 + return -EINVAL;
5200 + }
5201 + return __testport(set, req->port, hash_port);
5202 +}
5203 +
5204 +static int
5205 +testport_kernel(struct ip_set *set, const struct sk_buff *skb,
5206 + u_int32_t flags, ip_set_ip_t *hash_port)
5207 +{
5208 + int res;
5209 + ip_set_ip_t port = get_port(skb, flags);
5210 +
5211 + DP("flag %s port %u", flags & IPSET_SRC ? "SRC" : "DST", port);
5212 + if (port == INVALID_PORT)
5213 + return 0;
5214 +
5215 + res = __testport(set, port, hash_port);
5216 +
5217 + return (res < 0 ? 0 : res);
5218 +}
5219 +
5220 +static inline int
5221 +__addport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5222 +{
5223 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5224 +
5225 + if (port < map->first_port || port > map->last_port)
5226 + return -ERANGE;
5227 + if (test_and_set_bit(port - map->first_port, map->members))
5228 + return -EEXIST;
5229 +
5230 + *hash_port = port;
5231 + DP("port %u", port);
5232 + return 0;
5233 +}
5234 +
5235 +static int
5236 +addport(struct ip_set *set, const void *data, size_t size,
5237 + ip_set_ip_t *hash_port)
5238 +{
5239 + struct ip_set_req_portmap *req =
5240 + (struct ip_set_req_portmap *) data;
5241 +
5242 + if (size != sizeof(struct ip_set_req_portmap)) {
5243 + ip_set_printk("data length wrong (want %zu, have %zu)",
5244 + sizeof(struct ip_set_req_portmap),
5245 + size);
5246 + return -EINVAL;
5247 + }
5248 + return __addport(set, req->port, hash_port);
5249 +}
5250 +
5251 +static int
5252 +addport_kernel(struct ip_set *set, const struct sk_buff *skb,
5253 + u_int32_t flags, ip_set_ip_t *hash_port)
5254 +{
5255 + ip_set_ip_t port = get_port(skb, flags);
5256 +
5257 + if (port == INVALID_PORT)
5258 + return -EINVAL;
5259 +
5260 + return __addport(set, port, hash_port);
5261 +}
5262 +
5263 +static inline int
5264 +__delport(struct ip_set *set, ip_set_ip_t port, ip_set_ip_t *hash_port)
5265 +{
5266 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5267 +
5268 + if (port < map->first_port || port > map->last_port)
5269 + return -ERANGE;
5270 + if (!test_and_clear_bit(port - map->first_port, map->members))
5271 + return -EEXIST;
5272 +
5273 + *hash_port = port;
5274 + DP("port %u", port);
5275 + return 0;
5276 +}
5277 +
5278 +static int
5279 +delport(struct ip_set *set, const void *data, size_t size,
5280 + ip_set_ip_t *hash_port)
5281 +{
5282 + struct ip_set_req_portmap *req =
5283 + (struct ip_set_req_portmap *) data;
5284 +
5285 + if (size != sizeof(struct ip_set_req_portmap)) {
5286 + ip_set_printk("data length wrong (want %zu, have %zu)",
5287 + sizeof(struct ip_set_req_portmap),
5288 + size);
5289 + return -EINVAL;
5290 + }
5291 + return __delport(set, req->port, hash_port);
5292 +}
5293 +
5294 +static int
5295 +delport_kernel(struct ip_set *set, const struct sk_buff *skb,
5296 + u_int32_t flags, ip_set_ip_t *hash_port)
5297 +{
5298 + ip_set_ip_t port = get_port(skb, flags);
5299 +
5300 + if (port == INVALID_PORT)
5301 + return -EINVAL;
5302 +
5303 + return __delport(set, port, hash_port);
5304 +}
5305 +
5306 +static int create(struct ip_set *set, const void *data, size_t size)
5307 +{
5308 + int newbytes;
5309 + struct ip_set_req_portmap_create *req =
5310 + (struct ip_set_req_portmap_create *) data;
5311 + struct ip_set_portmap *map;
5312 +
5313 + if (size != sizeof(struct ip_set_req_portmap_create)) {
5314 + ip_set_printk("data length wrong (want %zu, have %zu)",
5315 + sizeof(struct ip_set_req_portmap_create),
5316 + size);
5317 + return -EINVAL;
5318 + }
5319 +
5320 + DP("from %u to %u", req->from, req->to);
5321 +
5322 + if (req->from > req->to) {
5323 + DP("bad port range");
5324 + return -ENOEXEC;
5325 + }
5326 +
5327 + if (req->to - req->from > MAX_RANGE) {
5328 + ip_set_printk("range too big (max %d ports)",
5329 + MAX_RANGE);
5330 + return -ENOEXEC;
5331 + }
5332 +
5333 + map = kmalloc(sizeof(struct ip_set_portmap), GFP_KERNEL);
5334 + if (!map) {
5335 + DP("out of memory for %d bytes",
5336 + sizeof(struct ip_set_portmap));
5337 + return -ENOMEM;
5338 + }
5339 + map->first_port = req->from;
5340 + map->last_port = req->to;
5341 + newbytes = bitmap_bytes(req->from, req->to);
5342 + map->members = kmalloc(newbytes, GFP_KERNEL);
5343 + if (!map->members) {
5344 + DP("out of memory for %d bytes", newbytes);
5345 + kfree(map);
5346 + return -ENOMEM;
5347 + }
5348 + memset(map->members, 0, newbytes);
5349 +
5350 + set->data = map;
5351 + return 0;
5352 +}
5353 +
5354 +static void destroy(struct ip_set *set)
5355 +{
5356 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5357 +
5358 + kfree(map->members);
5359 + kfree(map);
5360 +
5361 + set->data = NULL;
5362 +}
5363 +
5364 +static void flush(struct ip_set *set)
5365 +{
5366 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5367 + memset(map->members, 0, bitmap_bytes(map->first_port, map->last_port));
5368 +}
5369 +
5370 +static void list_header(const struct ip_set *set, void *data)
5371 +{
5372 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5373 + struct ip_set_req_portmap_create *header =
5374 + (struct ip_set_req_portmap_create *) data;
5375 +
5376 + DP("list_header %u %u", map->first_port, map->last_port);
5377 +
5378 + header->from = map->first_port;
5379 + header->to = map->last_port;
5380 +}
5381 +
5382 +static int list_members_size(const struct ip_set *set)
5383 +{
5384 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5385 +
5386 + return bitmap_bytes(map->first_port, map->last_port);
5387 +}
5388 +
5389 +static void list_members(const struct ip_set *set, void *data)
5390 +{
5391 + struct ip_set_portmap *map = (struct ip_set_portmap *) set->data;
5392 + int bytes = bitmap_bytes(map->first_port, map->last_port);
5393 +
5394 + memcpy(data, map->members, bytes);
5395 +}
5396 +
5397 +static struct ip_set_type ip_set_portmap = {
5398 + .typename = SETTYPE_NAME,
5399 + .typecode = IPSET_TYPE_PORT,
5400 + .protocol_version = IP_SET_PROTOCOL_VERSION,
5401 + .create = &create,
5402 + .destroy = &destroy,
5403 + .flush = &flush,
5404 + .reqsize = sizeof(struct ip_set_req_portmap),
5405 + .addip = &addport,
5406 + .addip_kernel = &addport_kernel,
5407 + .delip = &delport,
5408 + .delip_kernel = &delport_kernel,
5409 + .testip = &testport,
5410 + .testip_kernel = &testport_kernel,
5411 + .header_size = sizeof(struct ip_set_req_portmap_create),
5412 + .list_header = &list_header,
5413 + .list_members_size = &list_members_size,
5414 + .list_members = &list_members,
5415 + .me = THIS_MODULE,
5416 +};
5417 +
5418 +MODULE_LICENSE("GPL");
5419 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5420 +MODULE_DESCRIPTION("portmap type of IP sets");
5421 +
5422 +static int __init init(void)
5423 +{
5424 + return ip_set_register_set_type(&ip_set_portmap);
5425 +}
5426 +
5427 +static void __exit fini(void)
5428 +{
5429 + /* FIXME: possible race with ip_set_create() */
5430 + ip_set_unregister_set_type(&ip_set_portmap);
5431 +}
5432 +
5433 +module_init(init);
5434 +module_exit(fini);
5435 --- /dev/null
5436 +++ b/net/ipv4/netfilter/ipt_set.c
5437 @@ -0,0 +1,114 @@
5438 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5439 + * Patrick Schaaf <bof@bof.de>
5440 + * Martin Josefsson <gandalf@wlug.westbo.se>
5441 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5442 + *
5443 + * This program is free software; you can redistribute it and/or modify
5444 + * it under the terms of the GNU General Public License version 2 as
5445 + * published by the Free Software Foundation.
5446 + */
5447 +
5448 +/* Kernel module to match an IP set. */
5449 +
5450 +#include <linux/module.h>
5451 +#include <linux/ip.h>
5452 +#include <linux/skbuff.h>
5453 +
5454 +#include <linux/netfilter_ipv4/ip_tables.h>
5455 +#include <linux/netfilter_ipv4/ip_set.h>
5456 +#include <linux/netfilter_ipv4/ipt_set.h>
5457 +
5458 +static inline int
5459 +match_set(const struct ipt_set_info *info,
5460 + const struct sk_buff *skb,
5461 + int inv)
5462 +{
5463 + if (ip_set_testip_kernel(info->index, skb, info->flags))
5464 + inv = !inv;
5465 + return inv;
5466 +}
5467 +
5468 +static int
5469 +match(const struct sk_buff *skb,
5470 + const struct net_device *in,
5471 + const struct net_device *out,
5472 + const void *matchinfo,
5473 + int offset,
5474 + const void *hdr,
5475 + u_int16_t datalen,
5476 + int *hotdrop)
5477 +{
5478 + const struct ipt_set_info_match *info = matchinfo;
5479 +
5480 + return match_set(&info->match_set,
5481 + skb,
5482 + info->match_set.flags[0] & IPSET_MATCH_INV);
5483 +}
5484 +
5485 +static int
5486 +checkentry(const char *tablename,
5487 + const struct ipt_ip *ip,
5488 + void *matchinfo,
5489 + unsigned int matchsize,
5490 + unsigned int hook_mask)
5491 +{
5492 + struct ipt_set_info_match *info =
5493 + (struct ipt_set_info_match *) matchinfo;
5494 + ip_set_id_t index;
5495 +
5496 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5497 + ip_set_printk("invalid matchsize %d", matchsize);
5498 + return 0;
5499 + }
5500 +
5501 + index = ip_set_get_byindex(info->match_set.index);
5502 +
5503 + if (index == IP_SET_INVALID_ID) {
5504 + ip_set_printk("Cannot find set indentified by id %u to match",
5505 + info->match_set.index);
5506 + return 0; /* error */
5507 + }
5508 + if (info->match_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5509 + ip_set_printk("That's nasty!");
5510 + return 0; /* error */
5511 + }
5512 +
5513 + return 1;
5514 +}
5515 +
5516 +static void destroy(void *matchinfo, unsigned int matchsize)
5517 +{
5518 + struct ipt_set_info_match *info = matchinfo;
5519 +
5520 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_set_info_match))) {
5521 + ip_set_printk("invalid matchsize %d", matchsize);
5522 + return;
5523 + }
5524 +
5525 + ip_set_put(info->match_set.index);
5526 +}
5527 +
5528 +static struct ipt_match set_match = {
5529 + .name = "set",
5530 + .match = &match,
5531 + .checkentry = &checkentry,
5532 + .destroy = &destroy,
5533 + .me = THIS_MODULE
5534 +};
5535 +
5536 +MODULE_LICENSE("GPL");
5537 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5538 +MODULE_DESCRIPTION("iptables IP set match module");
5539 +
5540 +static int __init init(void)
5541 +{
5542 + return ipt_register_match(&set_match);
5543 +}
5544 +
5545 +static void __exit fini(void)
5546 +{
5547 + ipt_unregister_match(&set_match);
5548 +}
5549 +
5550 +module_init(init);
5551 +module_exit(fini);
5552 --- /dev/null
5553 +++ b/net/ipv4/netfilter/ipt_SET.c
5554 @@ -0,0 +1,127 @@
5555 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5556 + * Patrick Schaaf <bof@bof.de>
5557 + * Martin Josefsson <gandalf@wlug.westbo.se>
5558 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5559 + *
5560 + * This program is free software; you can redistribute it and/or modify
5561 + * it under the terms of the GNU General Public License version 2 as
5562 + * published by the Free Software Foundation.
5563 + */
5564 +
5565 +/* ipt_SET.c - netfilter target to manipulate IP sets */
5566 +
5567 +#include <linux/types.h>
5568 +#include <linux/ip.h>
5569 +#include <linux/timer.h>
5570 +#include <linux/module.h>
5571 +#include <linux/netfilter.h>
5572 +#include <linux/netdevice.h>
5573 +#include <linux/if.h>
5574 +#include <linux/inetdevice.h>
5575 +#include <net/protocol.h>
5576 +#include <net/checksum.h>
5577 +#include <linux/netfilter_ipv4.h>
5578 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
5579 +#include <linux/netfilter_ipv4/ipt_set.h>
5580 +
5581 +static unsigned int
5582 +target(struct sk_buff **pskb,
5583 + unsigned int hooknum,
5584 + const struct net_device *in,
5585 + const struct net_device *out,
5586 + const void *targinfo,
5587 + void *userinfo)
5588 +{
5589 + const struct ipt_set_info_target *info = targinfo;
5590 +
5591 + if (info->add_set.index != IP_SET_INVALID_ID)
5592 + ip_set_addip_kernel(info->add_set.index,
5593 + *pskb,
5594 + info->add_set.flags);
5595 + if (info->del_set.index != IP_SET_INVALID_ID)
5596 + ip_set_delip_kernel(info->del_set.index,
5597 + *pskb,
5598 + info->del_set.flags);
5599 +
5600 + return IPT_CONTINUE;
5601 +}
5602 +
5603 +static int
5604 +checkentry(const char *tablename,
5605 + const struct ipt_entry *e,
5606 + void *targinfo,
5607 + unsigned int targinfosize, unsigned int hook_mask)
5608 +{
5609 + struct ipt_set_info_target *info =
5610 + (struct ipt_set_info_target *) targinfo;
5611 + ip_set_id_t index;
5612 +
5613 + if (targinfosize != IPT_ALIGN(sizeof(*info))) {
5614 + DP("bad target info size %u", targinfosize);
5615 + return 0;
5616 + }
5617 +
5618 + if (info->add_set.index != IP_SET_INVALID_ID) {
5619 + index = ip_set_get_byindex(info->add_set.index);
5620 + if (index == IP_SET_INVALID_ID) {
5621 + ip_set_printk("cannot find add_set index %u as target",
5622 + info->add_set.index);
5623 + return 0; /* error */
5624 + }
5625 + }
5626 +
5627 + if (info->del_set.index != IP_SET_INVALID_ID) {
5628 + index = ip_set_get_byindex(info->del_set.index);
5629 + if (index == IP_SET_INVALID_ID) {
5630 + ip_set_printk("cannot find del_set index %u as target",
5631 + info->del_set.index);
5632 + return 0; /* error */
5633 + }
5634 + }
5635 + if (info->add_set.flags[IP_SET_MAX_BINDINGS] != 0
5636 + || info->del_set.flags[IP_SET_MAX_BINDINGS] != 0) {
5637 + ip_set_printk("That's nasty!");
5638 + return 0; /* error */
5639 + }
5640 + return 1;
5641 +}
5642 +
5643 +static void destroy(void *targetinfo, unsigned int targetsize)
5644 +{
5645 + struct ipt_set_info_target *info = targetinfo;
5646 +
5647 + if (targetsize != IPT_ALIGN(sizeof(struct ipt_set_info_target))) {
5648 + ip_set_printk("invalid targetsize %d", targetsize);
5649 + return;
5650 + }
5651 +
5652 + if (info->add_set.index != IP_SET_INVALID_ID)
5653 + ip_set_put(info->add_set.index);
5654 + if (info->del_set.index != IP_SET_INVALID_ID)
5655 + ip_set_put(info->del_set.index);
5656 +}
5657 +
5658 +static struct ipt_target SET_target = {
5659 + .name = "SET",
5660 + .target = target,
5661 + .checkentry = checkentry,
5662 + .destroy = destroy,
5663 + .me = THIS_MODULE
5664 +};
5665 +
5666 +MODULE_LICENSE("GPL");
5667 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
5668 +MODULE_DESCRIPTION("iptables IP set target module");
5669 +
5670 +static int __init init(void)
5671 +{
5672 + return ipt_register_target(&SET_target);
5673 +}
5674 +
5675 +static void __exit fini(void)
5676 +{
5677 + ipt_unregister_target(&SET_target);
5678 +}
5679 +
5680 +module_init(init);
5681 +module_exit(fini);
5682 --- a/net/ipv4/netfilter/Makefile
5683 +++ b/net/ipv4/netfilter/Makefile
5684 @@ -66,6 +66,18 @@ obj-$(CONFIG_IP_NF_NAT) += iptable_nat.o
5685 obj-$(CONFIG_IP_NF_MATCH_HELPER) += ipt_helper.o
5686 obj-$(CONFIG_IP_NF_MATCH_LIMIT) += ipt_limit.o
5687 obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
5688 +obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
5689 +obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
5690 +ifdef CONFIG_IP_NF_SET
5691 + obj-$(CONFIG_IP_NF_SET) += ip_set.o
5692 + export-objs += ip_set.o
5693 +endif
5694 +obj-$(CONFIG_IP_NF_SET_IPMAP) += ip_set_ipmap.o
5695 +obj-$(CONFIG_IP_NF_SET_PORTMAP) += ip_set_portmap.o
5696 +obj-$(CONFIG_IP_NF_SET_MACIPMAP) += ip_set_macipmap.o
5697 +obj-$(CONFIG_IP_NF_SET_IPHASH) += ip_set_iphash.o
5698 +obj-$(CONFIG_IP_NF_SET_NETHASH) += ip_set_nethash.o
5699 +obj-$(CONFIG_IP_NF_SET_IPTREE) += ip_set_iptree.o
5700 obj-$(CONFIG_IP_NF_MATCH_MAC) += ipt_mac.o
5701 obj-$(CONFIG_IP_NF_MATCH_IPP2P) += ipt_ipp2p.o
5702