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