f52d0ea730277038dffc5316739df802ed7074f5
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches-2.6.22 / 130-netfilter-ipset.patch
1 diff -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set.h
2 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set.h 1969-12-31 18:00:00.000000000 -0600
3 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set.h 2007-06-08 16:29:31.825808000 -0500
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 -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_iphash.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_iphash.h
504 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_iphash.h 1969-12-31 18:00:00.000000000 -0600
505 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_iphash.h 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_ipmap.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_ipmap.h
538 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_ipmap.h 1969-12-31 18:00:00.000000000 -0600
539 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_ipmap.h 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_ipporthash.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_ipporthash.h
598 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_ipporthash.h 1969-12-31 18:00:00.000000000 -0600
599 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_ipporthash.h 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_iptree.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_iptree.h
636 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_iptree.h 1969-12-31 18:00:00.000000000 -0600
637 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_iptree.h 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_jhash.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_jhash.h
680 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_jhash.h 1969-12-31 18:00:00.000000000 -0600
681 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_jhash.h 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_macipmap.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_macipmap.h
832 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_macipmap.h 1969-12-31 18:00:00.000000000 -0600
833 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_macipmap.h 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_malloc.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_malloc.h
874 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_malloc.h 1969-12-31 18:00:00.000000000 -0600
875 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_malloc.h 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_nethash.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_nethash.h
994 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_nethash.h 1969-12-31 18:00:00.000000000 -0600
995 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_nethash.h 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_portmap.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_portmap.h
1053 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ip_set_portmap.h 1969-12-31 18:00:00.000000000 -0600
1054 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ip_set_portmap.h 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/include/linux/netfilter_ipv4/ipt_set.h linux-2.6.21.1.new/include/linux/netfilter_ipv4/ipt_set.h
1082 --- linux-2.6.21.1/include/linux/netfilter_ipv4/ipt_set.h 1969-12-31 18:00:00.000000000 -0600
1083 +++ linux-2.6.21.1.new/include/linux/netfilter_ipv4/ipt_set.h 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/net/ipv4/netfilter/ip_set.c linux-2.6.21.1.new/net/ipv4/netfilter/ip_set.c
1107 --- linux-2.6.21.1/net/ipv4/netfilter/ip_set.c 1969-12-31 18:00:00.000000000 -0600
1108 +++ linux-2.6.21.1.new/net/ipv4/netfilter/ip_set.c 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/net/ipv4/netfilter/ip_set_iphash.c linux-2.6.21.1.new/net/ipv4/netfilter/ip_set_iphash.c
3112 --- linux-2.6.21.1/net/ipv4/netfilter/ip_set_iphash.c 1969-12-31 18:00:00.000000000 -0600
3113 +++ linux-2.6.21.1.new/net/ipv4/netfilter/ip_set_iphash.c 2007-06-08 16:29:31.829808250 -0500
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 -ruN linux-2.6.21.1/net/ipv4/netfilter/ip_set_ipmap.c linux-2.6.21.1.new/net/ipv4/netfilter/ip_set_ipmap.c
3529 --- linux-2.6.21.1/net/ipv4/netfilter/ip_set_ipmap.c 1969-12-31 18:00:00.000000000 -0600
3530 +++ linux-2.6.21.1.new/net/ipv4/netfilter/ip_set_ipmap.c 2007-06-08 16:29:31.833808500 -0500
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 -ruN linux-2.6.21.1/net/ipv4/netfilter/ip_set_ipporthash.c linux-2.6.21.1.new/net/ipv4/netfilter/ip_set_ipporthash.c
3860 --- linux-2.6.21.1/net/ipv4/netfilter/ip_set_ipporthash.c 1969-12-31 18:00:00.000000000 -0600
3861 +++ linux-2.6.21.1.new/net/ipv4/netfilter/ip_set_ipporthash.c 2007-06-08 16:29:31.833808500 -0500
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 + str