[kernel] refresh 2.6.25 patches
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches-2.6.25 / 130-netfilter_ipset.patch
1 --- a/include/linux/netfilter_ipv4/Kbuild
2 +++ b/include/linux/netfilter_ipv4/Kbuild
3 @@ -45,3 +45,14 @@ header-y += ipt_ttl.h
4
5 unifdef-y += ip_queue.h
6 unifdef-y += ip_tables.h
7 +
8 +unifdef-y += ip_set.h
9 +header-y += ip_set_iphash.h
10 +header-y += ip_set_ipmap.h
11 +header-y += ip_set_ipporthash.h
12 +unifdef-y += ip_set_iptree.h
13 +unifdef-y += ip_set_iptreemap.h
14 +header-y += ip_set_jhash.h
15 +header-y += ip_set_macipmap.h
16 +unifdef-y += ip_set_nethash.h
17 +header-y += ip_set_portmap.h
18 --- /dev/null
19 +++ b/include/linux/netfilter_ipv4/ip_set.h
20 @@ -0,0 +1,498 @@
21 +#ifndef _IP_SET_H
22 +#define _IP_SET_H
23 +
24 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
25 + * Patrick Schaaf <bof@bof.de>
26 + * Martin Josefsson <gandalf@wlug.westbo.se>
27 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
28 + *
29 + * This program is free software; you can redistribute it and/or modify
30 + * it under the terms of the GNU General Public License version 2 as
31 + * published by the Free Software Foundation.
32 + */
33 +
34 +#if 0
35 +#define IP_SET_DEBUG
36 +#endif
37 +
38 +/*
39 + * A sockopt of such quality has hardly ever been seen before on the open
40 + * market! This little beauty, hardly ever used: above 64, so it's
41 + * traditionally used for firewalling, not touched (even once!) by the
42 + * 2.0, 2.2 and 2.4 kernels!
43 + *
44 + * Comes with its own certificate of authenticity, valid anywhere in the
45 + * Free world!
46 + *
47 + * Rusty, 19.4.2000
48 + */
49 +#define SO_IP_SET 83
50 +
51 +/*
52 + * Heavily modify by Joakim Axelsson 08.03.2002
53 + * - Made it more modulebased
54 + *
55 + * Additional heavy modifications by Jozsef Kadlecsik 22.02.2004
56 + * - bindings added
57 + * - in order to "deal with" backward compatibility, renamed to ipset
58 + */
59 +
60 +/*
61 + * Used so that the kernel module and ipset-binary can match their versions
62 + */
63 +#define IP_SET_PROTOCOL_VERSION 2
64 +
65 +#define IP_SET_MAXNAMELEN 32 /* set names and set typenames */
66 +
67 +/* Lets work with our own typedef for representing an IP address.
68 + * We hope to make the code more portable, possibly to IPv6...
69 + *
70 + * The representation works in HOST byte order, because most set types
71 + * will perform arithmetic operations and compare operations.
72 + *
73 + * For now the type is an uint32_t.
74 + *
75 + * Make sure to ONLY use the functions when translating and parsing
76 + * in order to keep the host byte order and make it more portable:
77 + * parse_ip()
78 + * parse_mask()
79 + * parse_ipandmask()
80 + * ip_tostring()
81 + * (Joakim: where are they???)
82 + */
83 +
84 +typedef uint32_t ip_set_ip_t;
85 +
86 +/* Sets are identified by an id in kernel space. Tweak with ip_set_id_t
87 + * and IP_SET_INVALID_ID if you want to increase the max number of sets.
88 + */
89 +typedef uint16_t ip_set_id_t;
90 +
91 +#define IP_SET_INVALID_ID 65535
92 +
93 +/* How deep we follow bindings */
94 +#define IP_SET_MAX_BINDINGS 6
95 +
96 +/*
97 + * Option flags for kernel operations (ipt_set_info)
98 + */
99 +#define IPSET_SRC 0x01 /* Source match/add */
100 +#define IPSET_DST 0x02 /* Destination match/add */
101 +#define IPSET_MATCH_INV 0x04 /* Inverse matching */
102 +
103 +/*
104 + * Set features
105 + */
106 +#define IPSET_TYPE_IP 0x01 /* IP address type of set */
107 +#define IPSET_TYPE_PORT 0x02 /* Port type of set */
108 +#define IPSET_DATA_SINGLE 0x04 /* Single data storage */
109 +#define IPSET_DATA_DOUBLE 0x08 /* Double data storage */
110 +
111 +/* Reserved keywords */
112 +#define IPSET_TOKEN_DEFAULT ":default:"
113 +#define IPSET_TOKEN_ALL ":all:"
114 +
115 +/* SO_IP_SET operation constants, and their request struct types.
116 + *
117 + * Operation ids:
118 + * 0-99: commands with version checking
119 + * 100-199: add/del/test/bind/unbind
120 + * 200-299: list, save, restore
121 + */
122 +
123 +/* Single shot operations:
124 + * version, create, destroy, flush, rename and swap
125 + *
126 + * Sets are identified by name.
127 + */
128 +
129 +#define IP_SET_REQ_STD \
130 + unsigned op; \
131 + unsigned version; \
132 + char name[IP_SET_MAXNAMELEN]
133 +
134 +#define IP_SET_OP_CREATE 0x00000001 /* Create a new (empty) set */
135 +struct ip_set_req_create {
136 + IP_SET_REQ_STD;
137 + char typename[IP_SET_MAXNAMELEN];
138 +};
139 +
140 +#define IP_SET_OP_DESTROY 0x00000002 /* Remove a (empty) set */
141 +struct ip_set_req_std {
142 + IP_SET_REQ_STD;
143 +};
144 +
145 +#define IP_SET_OP_FLUSH 0x00000003 /* Remove all IPs in a set */
146 +/* Uses ip_set_req_std */
147 +
148 +#define IP_SET_OP_RENAME 0x00000004 /* Rename a set */
149 +/* Uses ip_set_req_create */
150 +
151 +#define IP_SET_OP_SWAP 0x00000005 /* Swap two sets */
152 +/* Uses ip_set_req_create */
153 +
154 +union ip_set_name_index {
155 + char name[IP_SET_MAXNAMELEN];
156 + ip_set_id_t index;
157 +};
158 +
159 +#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
160 +struct ip_set_req_get_set {
161 + unsigned op;
162 + unsigned version;
163 + union ip_set_name_index set;
164 +};
165 +
166 +#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
167 +/* Uses ip_set_req_get_set */
168 +
169 +#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
170 +struct ip_set_req_version {
171 + unsigned op;
172 + unsigned version;
173 +};
174 +
175 +/* Double shots operations:
176 + * add, del, test, bind and unbind.
177 + *
178 + * First we query the kernel to get the index and type of the target set,
179 + * then issue the command. Validity of IP is checked in kernel in order
180 + * to minimalize sockopt operations.
181 + */
182 +
183 +/* Get minimal set data for add/del/test/bind/unbind IP */
184 +#define IP_SET_OP_ADT_GET 0x00000010 /* Get set and type */
185 +struct ip_set_req_adt_get {
186 + unsigned op;
187 + unsigned version;
188 + union ip_set_name_index set;
189 + char typename[IP_SET_MAXNAMELEN];
190 +};
191 +
192 +#define IP_SET_REQ_BYINDEX \
193 + unsigned op; \
194 + ip_set_id_t index;
195 +
196 +struct ip_set_req_adt {
197 + IP_SET_REQ_BYINDEX;
198 +};
199 +
200 +#define IP_SET_OP_ADD_IP 0x00000101 /* Add an IP to a set */
201 +/* Uses ip_set_req_adt, with type specific addage */
202 +
203 +#define IP_SET_OP_DEL_IP 0x00000102 /* Remove an IP from a set */
204 +/* Uses ip_set_req_adt, with type specific addage */
205 +
206 +#define IP_SET_OP_TEST_IP 0x00000103 /* Test an IP in a set */
207 +/* Uses ip_set_req_adt, with type specific addage */
208 +
209 +#define IP_SET_OP_BIND_SET 0x00000104 /* Bind an IP to a set */
210 +/* Uses ip_set_req_bind, with type specific addage */
211 +struct ip_set_req_bind {
212 + IP_SET_REQ_BYINDEX;
213 + char binding[IP_SET_MAXNAMELEN];
214 +};
215 +
216 +#define IP_SET_OP_UNBIND_SET 0x00000105 /* Unbind an IP from a set */
217 +/* Uses ip_set_req_bind, with type speficic addage
218 + * index = 0 means unbinding for all sets */
219 +
220 +#define IP_SET_OP_TEST_BIND_SET 0x00000106 /* Test binding an IP to a set */
221 +/* Uses ip_set_req_bind, with type specific addage */
222 +
223 +/* Multiple shots operations: list, save, restore.
224 + *
225 + * - check kernel version and query the max number of sets
226 + * - get the basic information on all sets
227 + * and size required for the next step
228 + * - get actual set data: header, data, bindings
229 + */
230 +
231 +/* Get max_sets and the index of a queried set
232 + */
233 +#define IP_SET_OP_MAX_SETS 0x00000020
234 +struct ip_set_req_max_sets {
235 + unsigned op;
236 + unsigned version;
237 + ip_set_id_t max_sets; /* max_sets */
238 + ip_set_id_t sets; /* real number of sets */
239 + union ip_set_name_index set; /* index of set if name used */
240 +};
241 +
242 +/* Get the id and name of the sets plus size for next step */
243 +#define IP_SET_OP_LIST_SIZE 0x00000201
244 +#define IP_SET_OP_SAVE_SIZE 0x00000202
245 +struct ip_set_req_setnames {
246 + unsigned op;
247 + ip_set_id_t index; /* set to list/save */
248 + size_t size; /* size to get setdata/bindings */
249 + /* followed by sets number of struct ip_set_name_list */
250 +};
251 +
252 +struct ip_set_name_list {
253 + char name[IP_SET_MAXNAMELEN];
254 + char typename[IP_SET_MAXNAMELEN];
255 + ip_set_id_t index;
256 + ip_set_id_t id;
257 +};
258 +
259 +/* The actual list operation */
260 +#define IP_SET_OP_LIST 0x00000203
261 +struct ip_set_req_list {
262 + IP_SET_REQ_BYINDEX;
263 + /* sets number of struct ip_set_list in reply */
264 +};
265 +
266 +struct ip_set_list {
267 + ip_set_id_t index;
268 + ip_set_id_t binding;
269 + u_int32_t ref;
270 + size_t header_size; /* Set header data of header_size */
271 + size_t members_size; /* Set members data of members_size */
272 + size_t bindings_size; /* Set bindings data of bindings_size */
273 +};
274 +
275 +struct ip_set_hash_list {
276 + ip_set_ip_t ip;
277 + ip_set_id_t binding;
278 +};
279 +
280 +/* The save operation */
281 +#define IP_SET_OP_SAVE 0x00000204
282 +/* Uses ip_set_req_list, in the reply replaced by
283 + * sets number of struct ip_set_save plus a marker
284 + * ip_set_save followed by ip_set_hash_save structures.
285 + */
286 +struct ip_set_save {
287 + ip_set_id_t index;
288 + ip_set_id_t binding;
289 + size_t header_size; /* Set header data of header_size */
290 + size_t members_size; /* Set members data of members_size */
291 +};
292 +
293 +/* At restoring, ip == 0 means default binding for the given set: */
294 +struct ip_set_hash_save {
295 + ip_set_ip_t ip;
296 + ip_set_id_t id;
297 + ip_set_id_t binding;
298 +};
299 +
300 +/* The restore operation */
301 +#define IP_SET_OP_RESTORE 0x00000205
302 +/* Uses ip_set_req_setnames followed by ip_set_restore structures
303 + * plus a marker ip_set_restore, followed by ip_set_hash_save
304 + * structures.
305 + */
306 +struct ip_set_restore {
307 + char name[IP_SET_MAXNAMELEN];
308 + char typename[IP_SET_MAXNAMELEN];
309 + ip_set_id_t index;
310 + size_t header_size; /* Create data of header_size */
311 + size_t members_size; /* Set members data of members_size */
312 +};
313 +
314 +static inline int bitmap_bytes(ip_set_ip_t a, ip_set_ip_t b)
315 +{
316 + return 4 * ((((b - a + 8) / 8) + 3) / 4);
317 +}
318 +
319 +#ifdef __KERNEL__
320 +
321 +#define ip_set_printk(format, args...) \
322 + do { \
323 + printk("%s: %s: ", __FILE__, __FUNCTION__); \
324 + printk(format "\n" , ## args); \
325 + } while (0)
326 +
327 +#if defined(IP_SET_DEBUG)
328 +#define DP(format, args...) \
329 + do { \
330 + printk("%s: %s (DBG): ", __FILE__, __FUNCTION__);\
331 + printk(format "\n" , ## args); \
332 + } while (0)
333 +#define IP_SET_ASSERT(x) \
334 + do { \
335 + if (!(x)) \
336 + printk("IP_SET_ASSERT: %s:%i(%s)\n", \
337 + __FILE__, __LINE__, __FUNCTION__); \
338 + } while (0)
339 +#else
340 +#define DP(format, args...)
341 +#define IP_SET_ASSERT(x)
342 +#endif
343 +
344 +struct ip_set;
345 +
346 +/*
347 + * The ip_set_type definition - one per set type, e.g. "ipmap".
348 + *
349 + * Each individual set has a pointer, set->type, going to one
350 + * of these structures. Function pointers inside the structure implement
351 + * the real behaviour of the sets.
352 + *
353 + * If not mentioned differently, the implementation behind the function
354 + * pointers of a set_type, is expected to return 0 if ok, and a negative
355 + * errno (e.g. -EINVAL) on error.
356 + */
357 +struct ip_set_type {
358 + struct list_head list; /* next in list of set types */
359 +
360 + /* test for IP in set (kernel: iptables -m set src|dst)
361 + * return 0 if not in set, 1 if in set.
362 + */
363 + int (*testip_kernel) (struct ip_set *set,
364 + const struct sk_buff * skb,
365 + ip_set_ip_t *ip,
366 + const u_int32_t *flags,
367 + unsigned char index);
368 +
369 + /* test for IP in set (userspace: ipset -T set IP)
370 + * return 0 if not in set, 1 if in set.
371 + */
372 + int (*testip) (struct ip_set *set,
373 + const void *data, size_t size,
374 + ip_set_ip_t *ip);
375 +
376 + /*
377 + * Size of the data structure passed by when
378 + * adding/deletin/testing an entry.
379 + */
380 + size_t reqsize;
381 +
382 + /* Add IP into set (userspace: ipset -A set IP)
383 + * Return -EEXIST if the address is already in the set,
384 + * and -ERANGE if the address lies outside the set bounds.
385 + * If the address was not already in the set, 0 is returned.
386 + */
387 + int (*addip) (struct ip_set *set,
388 + const void *data, size_t size,
389 + ip_set_ip_t *ip);
390 +
391 + /* Add IP into set (kernel: iptables ... -j SET set src|dst)
392 + * Return -EEXIST if the address is already in the set,
393 + * and -ERANGE if the address lies outside the set bounds.
394 + * If the address was not already in the set, 0 is returned.
395 + */
396 + int (*addip_kernel) (struct ip_set *set,
397 + const struct sk_buff * skb,
398 + ip_set_ip_t *ip,
399 + const u_int32_t *flags,
400 + unsigned char index);
401 +
402 + /* remove IP from set (userspace: ipset -D set --entry x)
403 + * Return -EEXIST if the address is NOT in the set,
404 + * and -ERANGE if the address lies outside the set bounds.
405 + * If the address really was in the set, 0 is returned.
406 + */
407 + int (*delip) (struct ip_set *set,
408 + const void *data, size_t size,
409 + ip_set_ip_t *ip);
410 +
411 + /* remove IP from set (kernel: iptables ... -j SET --entry x)
412 + * Return -EEXIST if the address is NOT in the set,
413 + * and -ERANGE if the address lies outside the set bounds.
414 + * If the address really was in the set, 0 is returned.
415 + */
416 + int (*delip_kernel) (struct ip_set *set,
417 + const struct sk_buff * skb,
418 + ip_set_ip_t *ip,
419 + const u_int32_t *flags,
420 + unsigned char index);
421 +
422 + /* new set creation - allocated type specific items
423 + */
424 + int (*create) (struct ip_set *set,
425 + const void *data, size_t size);
426 +
427 + /* retry the operation after successfully tweaking the set
428 + */
429 + int (*retry) (struct ip_set *set);
430 +
431 + /* set destruction - free type specific items
432 + * There is no return value.
433 + * Can be called only when child sets are destroyed.
434 + */
435 + void (*destroy) (struct ip_set *set);
436 +
437 + /* set flushing - reset all bits in the set, or something similar.
438 + * There is no return value.
439 + */
440 + void (*flush) (struct ip_set *set);
441 +
442 + /* Listing: size needed for header
443 + */
444 + size_t header_size;
445 +
446 + /* Listing: Get the header
447 + *
448 + * Fill in the information in "data".
449 + * This function is always run after list_header_size() under a
450 + * writelock on the set. Therefor is the length of "data" always
451 + * correct.
452 + */
453 + void (*list_header) (const struct ip_set *set,
454 + void *data);
455 +
456 + /* Listing: Get the size for the set members
457 + */
458 + int (*list_members_size) (const struct ip_set *set);
459 +
460 + /* Listing: Get the set members
461 + *
462 + * Fill in the information in "data".
463 + * This function is always run after list_member_size() under a
464 + * writelock on the set. Therefor is the length of "data" always
465 + * correct.
466 + */
467 + void (*list_members) (const struct ip_set *set,
468 + void *data);
469 +
470 + char typename[IP_SET_MAXNAMELEN];
471 + unsigned char features;
472 + int protocol_version;
473 +
474 + /* Set this to THIS_MODULE if you are a module, otherwise NULL */
475 + struct module *me;
476 +};
477 +
478 +extern int ip_set_register_set_type(struct ip_set_type *set_type);
479 +extern void ip_set_unregister_set_type(struct ip_set_type *set_type);
480 +
481 +/* A generic ipset */
482 +struct ip_set {
483 + char name[IP_SET_MAXNAMELEN]; /* the name of the set */
484 + rwlock_t lock; /* lock for concurrency control */
485 + ip_set_id_t id; /* set id for swapping */
486 + ip_set_id_t binding; /* default binding for the set */
487 + atomic_t ref; /* in kernel and in hash references */
488 + struct ip_set_type *type; /* the set types */
489 + void *data; /* pooltype specific data */
490 +};
491 +
492 +/* Structure to bind set elements to sets */
493 +struct ip_set_hash {
494 + struct list_head list; /* list of clashing entries in hash */
495 + ip_set_ip_t ip; /* ip from set */
496 + ip_set_id_t id; /* set id */
497 + ip_set_id_t binding; /* set we bind the element to */
498 +};
499 +
500 +/* register and unregister set references */
501 +extern ip_set_id_t ip_set_get_byname(const char name[IP_SET_MAXNAMELEN]);
502 +extern ip_set_id_t ip_set_get_byindex(ip_set_id_t id);
503 +extern void ip_set_put(ip_set_id_t id);
504 +
505 +/* API for iptables set match, and SET target */
506 +extern void ip_set_addip_kernel(ip_set_id_t id,
507 + const struct sk_buff *skb,
508 + const u_int32_t *flags);
509 +extern void ip_set_delip_kernel(ip_set_id_t id,
510 + const struct sk_buff *skb,
511 + const u_int32_t *flags);
512 +extern int ip_set_testip_kernel(ip_set_id_t id,
513 + const struct sk_buff *skb,
514 + const u_int32_t *flags);
515 +
516 +#endif /* __KERNEL__ */
517 +
518 +#endif /*_IP_SET_H*/
519 --- /dev/null
520 +++ b/include/linux/netfilter_ipv4/ip_set_iphash.h
521 @@ -0,0 +1,30 @@
522 +#ifndef __IP_SET_IPHASH_H
523 +#define __IP_SET_IPHASH_H
524 +
525 +#include <linux/netfilter_ipv4/ip_set.h>
526 +
527 +#define SETTYPE_NAME "iphash"
528 +#define MAX_RANGE 0x0000FFFF
529 +
530 +struct ip_set_iphash {
531 + ip_set_ip_t *members; /* the iphash proper */
532 + uint32_t elements; /* number of elements */
533 + uint32_t hashsize; /* hash size */
534 + uint16_t probes; /* max number of probes */
535 + uint16_t resize; /* resize factor in percent */
536 + ip_set_ip_t netmask; /* netmask */
537 + void *initval[0]; /* initvals for jhash_1word */
538 +};
539 +
540 +struct ip_set_req_iphash_create {
541 + uint32_t hashsize;
542 + uint16_t probes;
543 + uint16_t resize;
544 + ip_set_ip_t netmask;
545 +};
546 +
547 +struct ip_set_req_iphash {
548 + ip_set_ip_t ip;
549 +};
550 +
551 +#endif /* __IP_SET_IPHASH_H */
552 --- /dev/null
553 +++ b/include/linux/netfilter_ipv4/ip_set_ipmap.h
554 @@ -0,0 +1,56 @@
555 +#ifndef __IP_SET_IPMAP_H
556 +#define __IP_SET_IPMAP_H
557 +
558 +#include <linux/netfilter_ipv4/ip_set.h>
559 +
560 +#define SETTYPE_NAME "ipmap"
561 +#define MAX_RANGE 0x0000FFFF
562 +
563 +struct ip_set_ipmap {
564 + void *members; /* the ipmap proper */
565 + ip_set_ip_t first_ip; /* host byte order, included in range */
566 + ip_set_ip_t last_ip; /* host byte order, included in range */
567 + ip_set_ip_t netmask; /* subnet netmask */
568 + ip_set_ip_t sizeid; /* size of set in IPs */
569 + ip_set_ip_t hosts; /* number of hosts in a subnet */
570 +};
571 +
572 +struct ip_set_req_ipmap_create {
573 + ip_set_ip_t from;
574 + ip_set_ip_t to;
575 + ip_set_ip_t netmask;
576 +};
577 +
578 +struct ip_set_req_ipmap {
579 + ip_set_ip_t ip;
580 +};
581 +
582 +unsigned int
583 +mask_to_bits(ip_set_ip_t mask)
584 +{
585 + unsigned int bits = 32;
586 + ip_set_ip_t maskaddr;
587 +
588 + if (mask == 0xFFFFFFFF)
589 + return bits;
590 +
591 + maskaddr = 0xFFFFFFFE;
592 + while (--bits >= 0 && maskaddr != mask)
593 + maskaddr <<= 1;
594 +
595 + return bits;
596 +}
597 +
598 +ip_set_ip_t
599 +range_to_mask(ip_set_ip_t from, ip_set_ip_t to, unsigned int *bits)
600 +{
601 + ip_set_ip_t mask = 0xFFFFFFFE;
602 +
603 + *bits = 32;
604 + while (--(*bits) >= 0 && mask && (to & mask) != from)
605 + mask <<= 1;
606 +
607 + return mask;
608 +}
609 +
610 +#endif /* __IP_SET_IPMAP_H */
611 --- /dev/null
612 +++ b/include/linux/netfilter_ipv4/ip_set_ipporthash.h
613 @@ -0,0 +1,34 @@
614 +#ifndef __IP_SET_IPPORTHASH_H
615 +#define __IP_SET_IPPORTHASH_H
616 +
617 +#include <linux/netfilter_ipv4/ip_set.h>
618 +
619 +#define SETTYPE_NAME "ipporthash"
620 +#define MAX_RANGE 0x0000FFFF
621 +#define INVALID_PORT (MAX_RANGE + 1)
622 +
623 +struct ip_set_ipporthash {
624 + ip_set_ip_t *members; /* the ipporthash proper */
625 + uint32_t elements; /* number of elements */
626 + uint32_t hashsize; /* hash size */
627 + uint16_t probes; /* max number of probes */
628 + uint16_t resize; /* resize factor in percent */
629 + ip_set_ip_t first_ip; /* host byte order, included in range */
630 + ip_set_ip_t last_ip; /* host byte order, included in range */
631 + void *initval[0]; /* initvals for jhash_1word */
632 +};
633 +
634 +struct ip_set_req_ipporthash_create {
635 + uint32_t hashsize;
636 + uint16_t probes;
637 + uint16_t resize;
638 + ip_set_ip_t from;
639 + ip_set_ip_t to;
640 +};
641 +
642 +struct ip_set_req_ipporthash {
643 + ip_set_ip_t ip;
644 + ip_set_ip_t port;
645 +};
646 +
647 +#endif /* __IP_SET_IPPORTHASH_H */
648 --- /dev/null
649 +++ b/include/linux/netfilter_ipv4/ip_set_iptree.h
650 @@ -0,0 +1,40 @@
651 +#ifndef __IP_SET_IPTREE_H
652 +#define __IP_SET_IPTREE_H
653 +
654 +#include <linux/netfilter_ipv4/ip_set.h>
655 +
656 +#define SETTYPE_NAME "iptree"
657 +#define MAX_RANGE 0x0000FFFF
658 +
659 +struct ip_set_iptreed {
660 + unsigned long expires[256]; /* x.x.x.ADDR */
661 +};
662 +
663 +struct ip_set_iptreec {
664 + struct ip_set_iptreed *tree[256]; /* x.x.ADDR.* */
665 +};
666 +
667 +struct ip_set_iptreeb {
668 + struct ip_set_iptreec *tree[256]; /* x.ADDR.*.* */
669 +};
670 +
671 +struct ip_set_iptree {
672 + unsigned int timeout;
673 + unsigned int gc_interval;
674 +#ifdef __KERNEL__
675 + uint32_t elements; /* number of elements */
676 + struct timer_list gc;
677 + struct ip_set_iptreeb *tree[256]; /* ADDR.*.*.* */
678 +#endif
679 +};
680 +
681 +struct ip_set_req_iptree_create {
682 + unsigned int timeout;
683 +};
684 +
685 +struct ip_set_req_iptree {
686 + ip_set_ip_t ip;
687 + unsigned int timeout;
688 +};
689 +
690 +#endif /* __IP_SET_IPTREE_H */
691 --- /dev/null
692 +++ b/include/linux/netfilter_ipv4/ip_set_iptreemap.h
693 @@ -0,0 +1,40 @@
694 +#ifndef __IP_SET_IPTREEMAP_H
695 +#define __IP_SET_IPTREEMAP_H
696 +
697 +#include <linux/netfilter_ipv4/ip_set.h>
698 +
699 +#define SETTYPE_NAME "iptreemap"
700 +
701 +#ifdef __KERNEL__
702 +struct ip_set_iptreemap_d {
703 + unsigned char bitmap[32]; /* x.x.x.y */
704 +};
705 +
706 +struct ip_set_iptreemap_c {
707 + struct ip_set_iptreemap_d *tree[256]; /* x.x.y.x */
708 +};
709 +
710 +struct ip_set_iptreemap_b {
711 + struct ip_set_iptreemap_c *tree[256]; /* x.y.x.x */
712 + unsigned char dirty[32];
713 +};
714 +#endif
715 +
716 +struct ip_set_iptreemap {
717 + unsigned int gc_interval;
718 +#ifdef __KERNEL__
719 + struct timer_list gc;
720 + struct ip_set_iptreemap_b *tree[256]; /* y.x.x.x */
721 +#endif
722 +};
723 +
724 +struct ip_set_req_iptreemap_create {
725 + unsigned int gc_interval;
726 +};
727 +
728 +struct ip_set_req_iptreemap {
729 + ip_set_ip_t start;
730 + ip_set_ip_t end;
731 +};
732 +
733 +#endif /* __IP_SET_IPTREEMAP_H */
734 --- /dev/null
735 +++ b/include/linux/netfilter_ipv4/ip_set_jhash.h
736 @@ -0,0 +1,148 @@
737 +#ifndef _LINUX_IPSET_JHASH_H
738 +#define _LINUX_IPSET_JHASH_H
739 +
740 +/* This is a copy of linux/jhash.h but the types u32/u8 are changed
741 + * to __u32/__u8 so that the header file can be included into
742 + * userspace code as well. Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
743 + */
744 +
745 +/* jhash.h: Jenkins hash support.
746 + *
747 + * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
748 + *
749 + * http://burtleburtle.net/bob/hash/
750 + *
751 + * These are the credits from Bob's sources:
752 + *
753 + * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
754 + * hash(), hash2(), hash3, and mix() are externally useful functions.
755 + * Routines to test the hash are included if SELF_TEST is defined.
756 + * You can use this free for any purpose. It has no warranty.
757 + *
758 + * Copyright (C) 2003 David S. Miller (davem@redhat.com)
759 + *
760 + * I've modified Bob's hash to be useful in the Linux kernel, and
761 + * any bugs present are surely my fault. -DaveM
762 + */
763 +
764 +/* NOTE: Arguments are modified. */
765 +#define __jhash_mix(a, b, c) \
766 +{ \
767 + a -= b; a -= c; a ^= (c>>13); \
768 + b -= c; b -= a; b ^= (a<<8); \
769 + c -= a; c -= b; c ^= (b>>13); \
770 + a -= b; a -= c; a ^= (c>>12); \
771 + b -= c; b -= a; b ^= (a<<16); \
772 + c -= a; c -= b; c ^= (b>>5); \
773 + a -= b; a -= c; a ^= (c>>3); \
774 + b -= c; b -= a; b ^= (a<<10); \
775 + c -= a; c -= b; c ^= (b>>15); \
776 +}
777 +
778 +/* The golden ration: an arbitrary value */
779 +#define JHASH_GOLDEN_RATIO 0x9e3779b9
780 +
781 +/* The most generic version, hashes an arbitrary sequence
782 + * of bytes. No alignment or length assumptions are made about
783 + * the input key.
784 + */
785 +static inline __u32 jhash(void *key, __u32 length, __u32 initval)
786 +{
787 + __u32 a, b, c, len;
788 + __u8 *k = key;
789 +
790 + len = length;
791 + a = b = JHASH_GOLDEN_RATIO;
792 + c = initval;
793 +
794 + while (len >= 12) {
795 + a += (k[0] +((__u32)k[1]<<8) +((__u32)k[2]<<16) +((__u32)k[3]<<24));
796 + b += (k[4] +((__u32)k[5]<<8) +((__u32)k[6]<<16) +((__u32)k[7]<<24));
797 + c += (k[8] +((__u32)k[9]<<8) +((__u32)k[10]<<16)+((__u32)k[11]<<24));
798 +
799 + __jhash_mix(a,b,c);
800 +
801 + k += 12;
802 + len -= 12;
803 + }
804 +
805 + c += length;
806 + switch (len) {
807 + case 11: c += ((__u32)k[10]<<24);
808 + case 10: c += ((__u32)k[9]<<16);
809 + case 9 : c += ((__u32)k[8]<<8);
810 + case 8 : b += ((__u32)k[7]<<24);
811 + case 7 : b += ((__u32)k[6]<<16);
812 + case 6 : b += ((__u32)k[5]<<8);
813 + case 5 : b += k[4];
814 + case 4 : a += ((__u32)k[3]<<24);
815 + case 3 : a += ((__u32)k[2]<<16);
816 + case 2 : a += ((__u32)k[1]<<8);
817 + case 1 : a += k[0];
818 + };
819 +
820 + __jhash_mix(a,b,c);
821 +
822 + return c;
823 +}
824 +
825 +/* A special optimized version that handles 1 or more of __u32s.
826 + * The length parameter here is the number of __u32s in the key.
827 + */
828 +static inline __u32 jhash2(__u32 *k, __u32 length, __u32 initval)
829 +{
830 + __u32 a, b, c, len;
831 +
832 + a = b = JHASH_GOLDEN_RATIO;
833 + c = initval;
834 + len = length;
835 +
836 + while (len >= 3) {
837 + a += k[0];
838 + b += k[1];
839 + c += k[2];
840 + __jhash_mix(a, b, c);
841 + k += 3; len -= 3;
842 + }
843 +
844 + c += length * 4;
845 +
846 + switch (len) {
847 + case 2 : b += k[1];
848 + case 1 : a += k[0];
849 + };
850 +
851 + __jhash_mix(a,b,c);
852 +
853 + return c;
854 +}
855 +
856 +
857 +/* A special ultra-optimized versions that knows they are hashing exactly
858 + * 3, 2 or 1 word(s).
859 + *
860 + * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally
861 + * done at the end is not done here.
862 + */
863 +static inline __u32 jhash_3words(__u32 a, __u32 b, __u32 c, __u32 initval)
864 +{
865 + a += JHASH_GOLDEN_RATIO;
866 + b += JHASH_GOLDEN_RATIO;
867 + c += initval;
868 +
869 + __jhash_mix(a, b, c);
870 +
871 + return c;
872 +}
873 +
874 +static inline __u32 jhash_2words(__u32 a, __u32 b, __u32 initval)
875 +{
876 + return jhash_3words(a, b, 0, initval);
877 +}
878 +
879 +static inline __u32 jhash_1word(__u32 a, __u32 initval)
880 +{
881 + return jhash_3words(a, 0, 0, initval);
882 +}
883 +
884 +#endif /* _LINUX_IPSET_JHASH_H */
885 --- /dev/null
886 +++ b/include/linux/netfilter_ipv4/ip_set_macipmap.h
887 @@ -0,0 +1,38 @@
888 +#ifndef __IP_SET_MACIPMAP_H
889 +#define __IP_SET_MACIPMAP_H
890 +
891 +#include <linux/netfilter_ipv4/ip_set.h>
892 +
893 +#define SETTYPE_NAME "macipmap"
894 +#define MAX_RANGE 0x0000FFFF
895 +
896 +/* general flags */
897 +#define IPSET_MACIP_MATCHUNSET 1
898 +
899 +/* per ip flags */
900 +#define IPSET_MACIP_ISSET 1
901 +
902 +struct ip_set_macipmap {
903 + void *members; /* the macipmap proper */
904 + ip_set_ip_t first_ip; /* host byte order, included in range */
905 + ip_set_ip_t last_ip; /* host byte order, included in range */
906 + u_int32_t flags;
907 +};
908 +
909 +struct ip_set_req_macipmap_create {
910 + ip_set_ip_t from;
911 + ip_set_ip_t to;
912 + u_int32_t flags;
913 +};
914 +
915 +struct ip_set_req_macipmap {
916 + ip_set_ip_t ip;
917 + unsigned char ethernet[ETH_ALEN];
918 +};
919 +
920 +struct ip_set_macip {
921 + unsigned short flags;
922 + unsigned char ethernet[ETH_ALEN];
923 +};
924 +
925 +#endif /* __IP_SET_MACIPMAP_H */
926 --- /dev/null
927 +++ b/include/linux/netfilter_ipv4/ip_set_malloc.h
928 @@ -0,0 +1,116 @@
929 +#ifndef _IP_SET_MALLOC_H
930 +#define _IP_SET_MALLOC_H
931 +
932 +#ifdef __KERNEL__
933 +
934 +/* Memory allocation and deallocation */
935 +static size_t max_malloc_size = 0;
936 +
937 +static inline void init_max_malloc_size(void)
938 +{
939 +#define CACHE(x) max_malloc_size = x;
940 +#include <linux/kmalloc_sizes.h>
941 +#undef CACHE
942 +}
943 +
944 +static inline void * ip_set_malloc(size_t bytes)
945 +{
946 + if (bytes > max_malloc_size)
947 + return vmalloc(bytes);
948 + else
949 + return kmalloc(bytes, GFP_KERNEL);
950 +}
951 +
952 +static inline void ip_set_free(void * data, size_t bytes)
953 +{
954 + if (bytes > max_malloc_size)
955 + vfree(data);
956 + else
957 + kfree(data);
958 +}
959 +
960 +struct harray {
961 + size_t max_elements;
962 + void *arrays[0];
963 +};
964 +
965 +static inline void *
966 +harray_malloc(size_t hashsize, size_t typesize, int flags)
967 +{
968 + struct harray *harray;
969 + size_t max_elements, size, i, j;
970 +
971 + if (!max_malloc_size)
972 + init_max_malloc_size();
973 +
974 + if (typesize > max_malloc_size)
975 + return NULL;
976 +
977 + max_elements = max_malloc_size/typesize;
978 + size = hashsize/max_elements;
979 + if (hashsize % max_elements)
980 + size++;
981 +
982 + /* Last pointer signals end of arrays */
983 + harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
984 + flags);
985 +
986 + if (!harray)
987 + return NULL;
988 +
989 + for (i = 0; i < size - 1; i++) {
990 + harray->arrays[i] = kmalloc(max_elements * typesize, flags);
991 + if (!harray->arrays[i])
992 + goto undo;
993 + memset(harray->arrays[i], 0, max_elements * typesize);
994 + }
995 + harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize,
996 + flags);
997 + if (!harray->arrays[i])
998 + goto undo;
999 + memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
1000 +
1001 + harray->max_elements = max_elements;
1002 + harray->arrays[size] = NULL;
1003 +
1004 + return (void *)harray;
1005 +
1006 + undo:
1007 + for (j = 0; j < i; j++) {
1008 + kfree(harray->arrays[j]);
1009 + }
1010 + kfree(harray);
1011 + return NULL;
1012 +}
1013 +
1014 +static inline void harray_free(void *h)
1015 +{
1016 + struct harray *harray = (struct harray *) h;
1017 + size_t i;
1018 +
1019 + for (i = 0; harray->arrays[i] != NULL; i++)
1020 + kfree(harray->arrays[i]);
1021 + kfree(harray);
1022 +}
1023 +
1024 +static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
1025 +{
1026 + struct harray *harray = (struct harray *) h;
1027 + size_t i;
1028 +
1029 + for (i = 0; harray->arrays[i+1] != NULL; i++)
1030 + memset(harray->arrays[i], 0, harray->max_elements * typesize);
1031 + memset(harray->arrays[i], 0,
1032 + (hashsize - i * harray->max_elements) * typesize);
1033 +}
1034 +
1035 +#define HARRAY_ELEM(h, type, which) \
1036 +({ \
1037 + struct harray *__h = (struct harray *)(h); \
1038 + ((type)((__h)->arrays[(which)/(__h)->max_elements]) \
1039 + + (which)%(__h)->max_elements); \
1040 +})
1041 +
1042 +#endif /* __KERNEL__ */
1043 +
1044 +#endif /*_IP_SET_MALLOC_H*/
1045 --- /dev/null
1046 +++ b/include/linux/netfilter_ipv4/ip_set_nethash.h
1047 @@ -0,0 +1,55 @@
1048 +#ifndef __IP_SET_NETHASH_H
1049 +#define __IP_SET_NETHASH_H
1050 +
1051 +#include <linux/netfilter_ipv4/ip_set.h>
1052 +
1053 +#define SETTYPE_NAME "nethash"
1054 +#define MAX_RANGE 0x0000FFFF
1055 +
1056 +struct ip_set_nethash {
1057 + ip_set_ip_t *members; /* the nethash proper */
1058 + uint32_t elements; /* number of elements */
1059 + uint32_t hashsize; /* hash size */
1060 + uint16_t probes; /* max number of probes */
1061 + uint16_t resize; /* resize factor in percent */
1062 + unsigned char cidr[30]; /* CIDR sizes */
1063 + void *initval[0]; /* initvals for jhash_1word */
1064 +};
1065 +
1066 +struct ip_set_req_nethash_create {
1067 + uint32_t hashsize;
1068 + uint16_t probes;
1069 + uint16_t resize;
1070 +};
1071 +
1072 +struct ip_set_req_nethash {
1073 + ip_set_ip_t ip;
1074 + unsigned char cidr;
1075 +};
1076 +
1077 +static unsigned char shifts[] = {255, 253, 249, 241, 225, 193, 129, 1};
1078 +
1079 +static inline ip_set_ip_t
1080 +pack(ip_set_ip_t ip, unsigned char cidr)
1081 +{
1082 + ip_set_ip_t addr, *paddr = &addr;
1083 + unsigned char n, t, *a;
1084 +
1085 + addr = htonl(ip & (0xFFFFFFFF << (32 - (cidr))));
1086 +#ifdef __KERNEL__
1087 + DP("ip:%u.%u.%u.%u/%u", NIPQUAD(addr), cidr);
1088 +#endif
1089 + n = cidr / 8;
1090 + t = cidr % 8;
1091 + a = &((unsigned char *)paddr)[n];
1092 + *a = *a /(1 << (8 - t)) + shifts[t];
1093 +#ifdef __KERNEL__
1094 + DP("n: %u, t: %u, a: %u", n, t, *a);
1095 + DP("ip:%u.%u.%u.%u/%u, %u.%u.%u.%u",
1096 + HIPQUAD(ip), cidr, NIPQUAD(addr));
1097 +#endif
1098 +
1099 + return ntohl(addr);
1100 +}
1101 +
1102 +#endif /* __IP_SET_NETHASH_H */
1103 --- /dev/null
1104 +++ b/include/linux/netfilter_ipv4/ip_set_portmap.h
1105 @@ -0,0 +1,25 @@
1106 +#ifndef __IP_SET_PORTMAP_H
1107 +#define __IP_SET_PORTMAP_H
1108 +
1109 +#include <linux/netfilter_ipv4/ip_set.h>
1110 +
1111 +#define SETTYPE_NAME "portmap"
1112 +#define MAX_RANGE 0x0000FFFF
1113 +#define INVALID_PORT (MAX_RANGE + 1)
1114 +
1115 +struct ip_set_portmap {
1116 + void *members; /* the portmap proper */
1117 + ip_set_ip_t first_port; /* host byte order, included in range */
1118 + ip_set_ip_t last_port; /* host byte order, included in range */
1119 +};
1120 +
1121 +struct ip_set_req_portmap_create {
1122 + ip_set_ip_t from;
1123 + ip_set_ip_t to;
1124 +};
1125 +
1126 +struct ip_set_req_portmap {
1127 + ip_set_ip_t port;
1128 +};
1129 +
1130 +#endif /* __IP_SET_PORTMAP_H */
1131 --- /dev/null
1132 +++ b/include/linux/netfilter_ipv4/ipt_set.h
1133 @@ -0,0 +1,21 @@
1134 +#ifndef _IPT_SET_H
1135 +#define _IPT_SET_H
1136 +
1137 +#include <linux/netfilter_ipv4/ip_set.h>
1138 +
1139 +struct ipt_set_info {
1140 + ip_set_id_t index;
1141 + u_int32_t flags[IP_SET_MAX_BINDINGS + 1];
1142 +};
1143 +
1144 +/* match info */
1145 +struct ipt_set_info_match {
1146 + struct ipt_set_info match_set;
1147 +};
1148 +
1149 +struct ipt_set_info_target {
1150 + struct ipt_set_info add_set;
1151 + struct ipt_set_info del_set;
1152 +};
1153 +
1154 +#endif /*_IPT_SET_H*/
1155 --- /dev/null
1156 +++ b/net/ipv4/netfilter/ip_set.c
1157 @@ -0,0 +1,2003 @@
1158 +/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
1159 + * Patrick Schaaf <bof@bof.de>
1160 + * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1161 + *
1162 + * This program is free software; you can redistribute it and/or modify
1163 + * it under the terms of the GNU General Public License version 2 as
1164 + * published by the Free Software Foundation.
1165 + */
1166 +
1167 +/* Kernel module for IP set management */
1168 +
1169 +#include <linux/version.h>
1170 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
1171 +#include <linux/config.h>
1172 +#endif
1173 +#include <linux/module.h>
1174 +#include <linux/moduleparam.h>
1175 +#include <linux/kmod.h>
1176 +#include <linux/ip.h>
1177 +#include <linux/skbuff.h>
1178 +#include <linux/random.h>
1179 +#include <linux/jhash.h>
1180 +#include <linux/netfilter_ipv4/ip_tables.h>
1181 +#include <linux/errno.h>
1182 +#include <asm/uaccess.h>
1183 +#include <asm/bitops.h>
1184 +#include <asm/semaphore.h>
1185 +#include <linux/spinlock.h>
1186 +#include <linux/vmalloc.h>
1187 +
1188 +#define ASSERT_READ_LOCK(x)
1189 +#define ASSERT_WRITE_LOCK(x)
1190 +#include <linux/netfilter_ipv4/ip_set.h>
1191 +
1192 +static struct list_head set_type_list; /* all registered sets */
1193 +static struct ip_set **ip_set_list; /* all individual sets */
1194 +static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
1195 +static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
1196 +static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
1197 +static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
1198 +static struct list_head *ip_set_hash; /* hash of bindings */
1199 +static unsigned int ip_set_hash_random; /* random seed */
1200 +
1201 +/*
1202 + * Sets are identified either by the index in ip_set_list or by id.
1203 + * The id never changes and is used to find a key in the hash.
1204 + * The index may change by swapping and used at all other places
1205 + * (set/SET netfilter modules, binding value, etc.)
1206 + *
1207 + * Userspace requests are serialized by ip_set_mutex and sets can
1208 + * be deleted only from userspace. Therefore ip_set_list locking
1209 + * must obey the following rules:
1210 + *
1211 + * - kernel requests: read and write locking mandatory
1212 + * - user requests: read locking optional, write locking mandatory
1213 + */
1214 +
1215 +static inline void
1216 +__ip_set_get(ip_set_id_t index)
1217 +{
1218 + atomic_inc(&ip_set_list[index]->ref);
1219 +}
1220 +
1221 +static inline void
1222 +__ip_set_put(ip_set_id_t index)
1223 +{
1224 + atomic_dec(&ip_set_list[index]->ref);
1225 +}
1226 +
1227 +/*
1228 + * Binding routines
1229 + */
1230 +
1231 +static inline struct ip_set_hash *
1232 +__ip_set_find(u_int32_t key, ip_set_id_t id, ip_set_ip_t ip)
1233 +{
1234 + struct ip_set_hash *set_hash;
1235 +
1236 + list_for_each_entry(set_hash, &ip_set_hash[key], list)
1237 + if (set_hash->id == id && set_hash->ip == ip)
1238 + return set_hash;
1239 +
1240 + return NULL;
1241 +}
1242 +
1243 +static ip_set_id_t
1244 +ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
1245 +{
1246 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1247 + % ip_set_bindings_hash_size;
1248 + struct ip_set_hash *set_hash;
1249 +
1250 + ASSERT_READ_LOCK(&ip_set_lock);
1251 + IP_SET_ASSERT(ip_set_list[id]);
1252 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1253 +
1254 + set_hash = __ip_set_find(key, id, ip);
1255 +
1256 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1257 + HIPQUAD(ip),
1258 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1259 +
1260 + return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
1261 +}
1262 +
1263 +static inline void
1264 +__set_hash_del(struct ip_set_hash *set_hash)
1265 +{
1266 + ASSERT_WRITE_LOCK(&ip_set_lock);
1267 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1268 +
1269 + __ip_set_put(set_hash->binding);
1270 + list_del(&set_hash->list);
1271 + kfree(set_hash);
1272 +}
1273 +
1274 +static int
1275 +ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
1276 +{
1277 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1278 + % ip_set_bindings_hash_size;
1279 + struct ip_set_hash *set_hash;
1280 +
1281 + IP_SET_ASSERT(ip_set_list[id]);
1282 + DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
1283 + write_lock_bh(&ip_set_lock);
1284 + set_hash = __ip_set_find(key, id, ip);
1285 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1286 + HIPQUAD(ip),
1287 + set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
1288 +
1289 + if (set_hash != NULL)
1290 + __set_hash_del(set_hash);
1291 + write_unlock_bh(&ip_set_lock);
1292 + return 0;
1293 +}
1294 +
1295 +static int
1296 +ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
1297 +{
1298 + u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
1299 + % ip_set_bindings_hash_size;
1300 + struct ip_set_hash *set_hash;
1301 + int ret = 0;
1302 +
1303 + IP_SET_ASSERT(ip_set_list[id]);
1304 + IP_SET_ASSERT(ip_set_list[binding]);
1305 + DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
1306 + HIPQUAD(ip), ip_set_list[binding]->name);
1307 + write_lock_bh(&ip_set_lock);
1308 + set_hash = __ip_set_find(key, id, ip);
1309 + if (!set_hash) {
1310 + set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_ATOMIC);
1311 + if (!set_hash) {
1312 + ret = -ENOMEM;
1313 + goto unlock;
1314 + }
1315 + INIT_LIST_HEAD(&set_hash->list);
1316 + set_hash->id = id;
1317 + set_hash->ip = ip;
1318 + list_add(&set_hash->list, &ip_set_hash[key]);
1319 + } else {
1320 + IP_SET_ASSERT(ip_set_list[set_hash->binding]);
1321 + DP("overwrite binding: %s",
1322 + ip_set_list[set_hash->binding]->name);
1323 + __ip_set_put(set_hash->binding);
1324 + }
1325 + set_hash->binding = binding;
1326 + __ip_set_get(set_hash->binding);
1327 + DP("stored: key %u, id %u (%s), ip %u.%u.%u.%u, binding %u (%s)",
1328 + key, id, ip_set_list[id]->name,
1329 + HIPQUAD(ip), binding, ip_set_list[binding]->name);
1330 + unlock:
1331 + write_unlock_bh(&ip_set_lock);
1332 + return ret;
1333 +}
1334 +
1335 +#define FOREACH_HASH_DO(fn, args...) \
1336 +({ \
1337 + ip_set_id_t __key; \
1338 + struct ip_set_hash *__set_hash; \
1339 + \
1340 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1341 + list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
1342 + fn(__set_hash , ## args); \
1343 + } \
1344 +})
1345 +
1346 +#define FOREACH_HASH_RW_DO(fn, args...) \
1347 +({ \
1348 + ip_set_id_t __key; \
1349 + struct ip_set_hash *__set_hash, *__n; \
1350 + \
1351 + ASSERT_WRITE_LOCK(&ip_set_lock); \
1352 + for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
1353 + list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
1354 + fn(__set_hash , ## args); \
1355 + } \
1356 +})
1357 +
1358 +/* Add, del and test set entries from kernel */
1359 +
1360 +#define follow_bindings(index, set, ip) \
1361 +((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
1362 + || (index = (set)->binding) != IP_SET_INVALID_ID)
1363 +
1364 +int
1365 +ip_set_testip_kernel(ip_set_id_t index,
1366 + const struct sk_buff *skb,
1367 + const u_int32_t *flags)
1368 +{
1369 + struct ip_set *set;
1370 + ip_set_ip_t ip;
1371 + int res;
1372 + unsigned char i = 0;
1373 +
1374 + IP_SET_ASSERT(flags[i]);
1375 + read_lock_bh(&ip_set_lock);
1376 + do {
1377 + set = ip_set_list[index];
1378 + IP_SET_ASSERT(set);
1379 + DP("set %s, index %u", set->name, index);
1380 + read_lock_bh(&set->lock);
1381 + res = set->type->testip_kernel(set, skb, &ip, flags, i++);
1382 + read_unlock_bh(&set->lock);
1383 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1384 + } while (res > 0
1385 + && flags[i]
1386 + && follow_bindings(index, set, ip));
1387 + read_unlock_bh(&ip_set_lock);
1388 +
1389 + return res;
1390 +}
1391 +
1392 +void
1393 +ip_set_addip_kernel(ip_set_id_t index,
1394 + const struct sk_buff *skb,
1395 + const u_int32_t *flags)
1396 +{
1397 + struct ip_set *set;
1398 + ip_set_ip_t ip;
1399 + int res;
1400 + unsigned char i = 0;
1401 +
1402 + IP_SET_ASSERT(flags[i]);
1403 + retry:
1404 + read_lock_bh(&ip_set_lock);
1405 + do {
1406 + set = ip_set_list[index];
1407 + IP_SET_ASSERT(set);
1408 + DP("set %s, index %u", set->name, index);
1409 + write_lock_bh(&set->lock);
1410 + res = set->type->addip_kernel(set, skb, &ip, flags, i++);
1411 + write_unlock_bh(&set->lock);
1412 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1413 + } while ((res == 0 || res == -EEXIST)
1414 + && flags[i]
1415 + && follow_bindings(index, set, ip));
1416 + read_unlock_bh(&ip_set_lock);
1417 +
1418 + if (res == -EAGAIN
1419 + && set->type->retry
1420 + && (res = set->type->retry(set)) == 0)
1421 + goto retry;
1422 +}
1423 +
1424 +void
1425 +ip_set_delip_kernel(ip_set_id_t index,
1426 + const struct sk_buff *skb,
1427 + const u_int32_t *flags)
1428 +{
1429 + struct ip_set *set;
1430 + ip_set_ip_t ip;
1431 + int res;
1432 + unsigned char i = 0;
1433 +
1434 + IP_SET_ASSERT(flags[i]);
1435 + read_lock_bh(&ip_set_lock);
1436 + do {
1437 + set = ip_set_list[index];
1438 + IP_SET_ASSERT(set);
1439 + DP("set %s, index %u", set->name, index);
1440 + write_lock_bh(&set->lock);
1441 + res = set->type->delip_kernel(set, skb, &ip, flags, i++);
1442 + write_unlock_bh(&set->lock);
1443 + i += !!(set->type->features & IPSET_DATA_DOUBLE);
1444 + } while ((res == 0 || res == -EEXIST)
1445 + && flags[i]
1446 + && follow_bindings(index, set, ip));
1447 + read_unlock_bh(&ip_set_lock);
1448 +}
1449 +
1450 +/* Register and deregister settype */
1451 +
1452 +static inline struct ip_set_type *
1453 +find_set_type(const char *name)
1454 +{
1455 + struct ip_set_type *set_type;
1456 +
1457 + list_for_each_entry(set_type, &set_type_list, list)
1458 + if (!strncmp(set_type->typename, name, IP_SET_MAXNAMELEN - 1))
1459 + return set_type;
1460 + return NULL;
1461 +}
1462 +
1463 +int
1464 +ip_set_register_set_type(struct ip_set_type *set_type)
1465 +{
1466 + int ret = 0;
1467 +
1468 + if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
1469 + ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
1470 + set_type->typename,
1471 + set_type->protocol_version,
1472 + IP_SET_PROTOCOL_VERSION);
1473 + return -EINVAL;
1474 + }
1475 +
1476 + write_lock_bh(&ip_set_lock);
1477 + if (find_set_type(set_type->typename)) {
1478 + /* Duplicate! */
1479 + ip_set_printk("'%s' already registered!",
1480 + set_type->typename);
1481 + ret = -EINVAL;
1482 + goto unlock;
1483 + }
1484 + if (!try_module_get(THIS_MODULE)) {
1485 + ret = -EFAULT;
1486 + goto unlock;
1487 + }
1488 + list_add(&set_type->list, &set_type_list);
1489 + DP("'%s' registered.", set_type->typename);
1490 + unlock:
1491 + write_unlock_bh(&ip_set_lock);
1492 + return ret;
1493 +}
1494 +
1495 +void
1496 +ip_set_unregister_set_type(struct ip_set_type *set_type)
1497 +{
1498 + write_lock_bh(&ip_set_lock);
1499 + if (!find_set_type(set_type->typename)) {
1500 + ip_set_printk("'%s' not registered?",
1501 + set_type->typename);
1502 + goto unlock;
1503 + }
1504 + list_del(&set_type->list);
1505 + module_put(THIS_MODULE);
1506 + DP("'%s' unregistered.", set_type->typename);
1507 + unlock:
1508 + write_unlock_bh(&ip_set_lock);
1509 +
1510 +}
1511 +
1512 +/*
1513 + * Userspace routines
1514 + */
1515 +
1516 +/*
1517 + * Find set by name, reference it once. The reference makes sure the
1518 + * thing pointed to, does not go away under our feet. Drop the reference
1519 + * later, using ip_set_put().
1520 + */
1521 +ip_set_id_t
1522 +ip_set_get_byname(const char *name)
1523 +{
1524 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1525 +
1526 + down(&ip_set_app_mutex);
1527 + for (i = 0; i < ip_set_max; i++) {
1528 + if (ip_set_list[i] != NULL
1529 + && strcmp(ip_set_list[i]->name, name) == 0) {
1530 + __ip_set_get(i);
1531 + index = i;
1532 + break;
1533 + }
1534 + }
1535 + up(&ip_set_app_mutex);
1536 + return index;
1537 +}
1538 +
1539 +/*
1540 + * Find set by index, reference it once. The reference makes sure the
1541 + * thing pointed to, does not go away under our feet. Drop the reference
1542 + * later, using ip_set_put().
1543 + */
1544 +ip_set_id_t
1545 +ip_set_get_byindex(ip_set_id_t index)
1546 +{
1547 + down(&ip_set_app_mutex);
1548 +
1549 + if (index >= ip_set_max)
1550 + return IP_SET_INVALID_ID;
1551 +
1552 + if (ip_set_list[index])
1553 + __ip_set_get(index);
1554 + else
1555 + index = IP_SET_INVALID_ID;
1556 +
1557 + up(&ip_set_app_mutex);
1558 + return index;
1559 +}
1560 +
1561 +/*
1562 + * If the given set pointer points to a valid set, decrement
1563 + * reference count by 1. The caller shall not assume the index
1564 + * to be valid, after calling this function.
1565 + */
1566 +void ip_set_put(ip_set_id_t index)
1567 +{
1568 + down(&ip_set_app_mutex);
1569 + if (ip_set_list[index])
1570 + __ip_set_put(index);
1571 + up(&ip_set_app_mutex);
1572 +}
1573 +
1574 +/* Find a set by name or index */
1575 +static ip_set_id_t
1576 +ip_set_find_byname(const char *name)
1577 +{
1578 + ip_set_id_t i, index = IP_SET_INVALID_ID;
1579 +
1580 + for (i = 0; i < ip_set_max; i++) {
1581 + if (ip_set_list[i] != NULL
1582 + && strcmp(ip_set_list[i]->name, name) == 0) {
1583 + index = i;
1584 + break;
1585 + }
1586 + }
1587 + return index;
1588 +}
1589 +
1590 +static ip_set_id_t
1591 +ip_set_find_byindex(ip_set_id_t index)
1592 +{
1593 + if (index >= ip_set_max || ip_set_list[index] == NULL)
1594 + index = IP_SET_INVALID_ID;
1595 +
1596 + return index;
1597 +}
1598 +
1599 +/*
1600 + * Add, del, test, bind and unbind
1601 + */
1602 +
1603 +static inline int
1604 +__ip_set_testip(struct ip_set *set,
1605 + const void *data,
1606 + size_t size,
1607 + ip_set_ip_t *ip)
1608 +{
1609 + int res;
1610 +
1611 + read_lock_bh(&set->lock);
1612 + res = set->type->testip(set, data, size, ip);
1613 + read_unlock_bh(&set->lock);
1614 +
1615 + return res;
1616 +}
1617 +
1618 +static int
1619 +__ip_set_addip(ip_set_id_t index,
1620 + const void *data,
1621 + size_t size)
1622 +{
1623 + struct ip_set *set = ip_set_list[index];
1624 + ip_set_ip_t ip;
1625 + int res;
1626 +
1627 + IP_SET_ASSERT(set);
1628 + do {
1629 + write_lock_bh(&set->lock);
1630 + res = set->type->addip(set, data, size, &ip);
1631 + write_unlock_bh(&set->lock);
1632 + } while (res == -EAGAIN
1633 + && set->type->retry
1634 + && (res = set->type->retry(set)) == 0);
1635 +
1636 + return res;
1637 +}
1638 +
1639 +static int
1640 +ip_set_addip(ip_set_id_t index,
1641 + const void *data,
1642 + size_t size)
1643 +{
1644 +
1645 + return __ip_set_addip(index,
1646 + data + sizeof(struct ip_set_req_adt),
1647 + size - sizeof(struct ip_set_req_adt));
1648 +}
1649 +
1650 +static int
1651 +ip_set_delip(ip_set_id_t index,
1652 + const void *data,
1653 + size_t size)
1654 +{
1655 + struct ip_set *set = ip_set_list[index];
1656 + ip_set_ip_t ip;
1657 + int res;
1658 +
1659 + IP_SET_ASSERT(set);
1660 + write_lock_bh(&set->lock);
1661 + res = set->type->delip(set,
1662 + data + sizeof(struct ip_set_req_adt),
1663 + size - sizeof(struct ip_set_req_adt),
1664 + &ip);
1665 + write_unlock_bh(&set->lock);
1666 +
1667 + return res;
1668 +}
1669 +
1670 +static int
1671 +ip_set_testip(ip_set_id_t index,
1672 + const void *data,
1673 + size_t size)
1674 +{
1675 + struct ip_set *set = ip_set_list[index];
1676 + ip_set_ip_t ip;
1677 + int res;
1678 +
1679 + IP_SET_ASSERT(set);
1680 + res = __ip_set_testip(set,
1681 + data + sizeof(struct ip_set_req_adt),
1682 + size - sizeof(struct ip_set_req_adt),
1683 + &ip);
1684 +
1685 + return (res > 0 ? -EEXIST : res);
1686 +}
1687 +
1688 +static int
1689 +ip_set_bindip(ip_set_id_t index,
1690 + const void *data,
1691 + size_t size)
1692 +{
1693 + struct ip_set *set = ip_set_list[index];
1694 + struct ip_set_req_bind *req_bind;
1695 + ip_set_id_t binding;
1696 + ip_set_ip_t ip;
1697 + int res;
1698 +
1699 + IP_SET_ASSERT(set);
1700 + if (size < sizeof(struct ip_set_req_bind))
1701 + return -EINVAL;
1702 +
1703 + req_bind = (struct ip_set_req_bind *) data;
1704 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1705 +
1706 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1707 + /* Default binding of a set */
1708 + char *binding_name;
1709 +
1710 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1711 + return -EINVAL;
1712 +
1713 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1714 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1715 +
1716 + binding = ip_set_find_byname(binding_name);
1717 + if (binding == IP_SET_INVALID_ID)
1718 + return -ENOENT;
1719 +
1720 + write_lock_bh(&ip_set_lock);
1721 + /* Sets as binding values are referenced */
1722 + if (set->binding != IP_SET_INVALID_ID)
1723 + __ip_set_put(set->binding);
1724 + set->binding = binding;
1725 + __ip_set_get(set->binding);
1726 + write_unlock_bh(&ip_set_lock);
1727 +
1728 + return 0;
1729 + }
1730 + binding = ip_set_find_byname(req_bind->binding);
1731 + if (binding == IP_SET_INVALID_ID)
1732 + return -ENOENT;
1733 +
1734 + res = __ip_set_testip(set,
1735 + data + sizeof(struct ip_set_req_bind),
1736 + size - sizeof(struct ip_set_req_bind),
1737 + &ip);
1738 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1739 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1740 +
1741 + if (res >= 0)
1742 + res = ip_set_hash_add(set->id, ip, binding);
1743 +
1744 + return res;
1745 +}
1746 +
1747 +#define FOREACH_SET_DO(fn, args...) \
1748 +({ \
1749 + ip_set_id_t __i; \
1750 + struct ip_set *__set; \
1751 + \
1752 + for (__i = 0; __i < ip_set_max; __i++) { \
1753 + __set = ip_set_list[__i]; \
1754 + if (__set != NULL) \
1755 + fn(__set , ##args); \
1756 + } \
1757 +})
1758 +
1759 +static inline void
1760 +__set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
1761 +{
1762 + if (set_hash->id == id)
1763 + __set_hash_del(set_hash);
1764 +}
1765 +
1766 +static inline void
1767 +__unbind_default(struct ip_set *set)
1768 +{
1769 + if (set->binding != IP_SET_INVALID_ID) {
1770 + /* Sets as binding values are referenced */
1771 + __ip_set_put(set->binding);
1772 + set->binding = IP_SET_INVALID_ID;
1773 + }
1774 +}
1775 +
1776 +static int
1777 +ip_set_unbindip(ip_set_id_t index,
1778 + const void *data,
1779 + size_t size)
1780 +{
1781 + struct ip_set *set;
1782 + struct ip_set_req_bind *req_bind;
1783 + ip_set_ip_t ip;
1784 + int res;
1785 +
1786 + DP("");
1787 + if (size < sizeof(struct ip_set_req_bind))
1788 + return -EINVAL;
1789 +
1790 + req_bind = (struct ip_set_req_bind *) data;
1791 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1792 +
1793 + DP("%u %s", index, req_bind->binding);
1794 + if (index == IP_SET_INVALID_ID) {
1795 + /* unbind :all: */
1796 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1797 + /* Default binding of sets */
1798 + write_lock_bh(&ip_set_lock);
1799 + FOREACH_SET_DO(__unbind_default);
1800 + write_unlock_bh(&ip_set_lock);
1801 + return 0;
1802 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1803 + /* Flush all bindings of all sets*/
1804 + write_lock_bh(&ip_set_lock);
1805 + FOREACH_HASH_RW_DO(__set_hash_del);
1806 + write_unlock_bh(&ip_set_lock);
1807 + return 0;
1808 + }
1809 + DP("unreachable reached!");
1810 + return -EINVAL;
1811 + }
1812 +
1813 + set = ip_set_list[index];
1814 + IP_SET_ASSERT(set);
1815 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1816 + /* Default binding of set */
1817 + ip_set_id_t binding = ip_set_find_byindex(set->binding);
1818 +
1819 + if (binding == IP_SET_INVALID_ID)
1820 + return -ENOENT;
1821 +
1822 + write_lock_bh(&ip_set_lock);
1823 + /* Sets in hash values are referenced */
1824 + __ip_set_put(set->binding);
1825 + set->binding = IP_SET_INVALID_ID;
1826 + write_unlock_bh(&ip_set_lock);
1827 +
1828 + return 0;
1829 + } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
1830 + /* Flush all bindings */
1831 +
1832 + write_lock_bh(&ip_set_lock);
1833 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
1834 + write_unlock_bh(&ip_set_lock);
1835 + return 0;
1836 + }
1837 +
1838 + res = __ip_set_testip(set,
1839 + data + sizeof(struct ip_set_req_bind),
1840 + size - sizeof(struct ip_set_req_bind),
1841 + &ip);
1842 +
1843 + DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
1844 + if (res >= 0)
1845 + res = ip_set_hash_del(set->id, ip);
1846 +
1847 + return res;
1848 +}
1849 +
1850 +static int
1851 +ip_set_testbind(ip_set_id_t index,
1852 + const void *data,
1853 + size_t size)
1854 +{
1855 + struct ip_set *set = ip_set_list[index];
1856 + struct ip_set_req_bind *req_bind;
1857 + ip_set_id_t binding;
1858 + ip_set_ip_t ip;
1859 + int res;
1860 +
1861 + IP_SET_ASSERT(set);
1862 + if (size < sizeof(struct ip_set_req_bind))
1863 + return -EINVAL;
1864 +
1865 + req_bind = (struct ip_set_req_bind *) data;
1866 + req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
1867 +
1868 + if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
1869 + /* Default binding of set */
1870 + char *binding_name;
1871 +
1872 + if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
1873 + return -EINVAL;
1874 +
1875 + binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
1876 + binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
1877 +
1878 + binding = ip_set_find_byname(binding_name);
1879 + if (binding == IP_SET_INVALID_ID)
1880 + return -ENOENT;
1881 +
1882 + res = (set->binding == binding) ? -EEXIST : 0;
1883 +
1884 + return res;
1885 + }
1886 + binding = ip_set_find_byname(req_bind->binding);
1887 + if (binding == IP_SET_INVALID_ID)
1888 + return -ENOENT;
1889 +
1890 +
1891 + res = __ip_set_testip(set,
1892 + data + sizeof(struct ip_set_req_bind),
1893 + size - sizeof(struct ip_set_req_bind),
1894 + &ip);
1895 + DP("set %s, ip: %u.%u.%u.%u, binding %s",
1896 + set->name, HIPQUAD(ip), ip_set_list[binding]->name);
1897 +
1898 + if (res >= 0)
1899 + res = (ip_set_find_in_hash(set->id, ip) == binding)
1900 + ? -EEXIST : 0;
1901 +
1902 + return res;
1903 +}
1904 +
1905 +static struct ip_set_type *
1906 +find_set_type_rlock(const char *typename)
1907 +{
1908 + struct ip_set_type *type;
1909 +
1910 + read_lock_bh(&ip_set_lock);
1911 + type = find_set_type(typename);
1912 + if (type == NULL)
1913 + read_unlock_bh(&ip_set_lock);
1914 +
1915 + return type;
1916 +}
1917 +
1918 +static int
1919 +find_free_id(const char *name,
1920 + ip_set_id_t *index,
1921 + ip_set_id_t *id)
1922 +{
1923 + ip_set_id_t i;
1924 +
1925 + *id = IP_SET_INVALID_ID;
1926 + for (i = 0; i < ip_set_max; i++) {
1927 + if (ip_set_list[i] == NULL) {
1928 + if (*id == IP_SET_INVALID_ID)
1929 + *id = *index = i;
1930 + } else if (strcmp(name, ip_set_list[i]->name) == 0)
1931 + /* Name clash */
1932 + return -EEXIST;
1933 + }
1934 + if (*id == IP_SET_INVALID_ID)
1935 + /* No free slot remained */
1936 + return -ERANGE;
1937 + /* Check that index is usable as id (swapping) */
1938 + check:
1939 + for (i = 0; i < ip_set_max; i++) {
1940 + if (ip_set_list[i] != NULL
1941 + && ip_set_list[i]->id == *id) {
1942 + *id = i;
1943 + goto check;
1944 + }
1945 + }
1946 + return 0;
1947 +}
1948 +
1949 +/*
1950 + * Create a set
1951 + */
1952 +static int
1953 +ip_set_create(const char *name,
1954 + const char *typename,
1955 + ip_set_id_t restore,
1956 + const void *data,
1957 + size_t size)
1958 +{
1959 + struct ip_set *set;
1960 + ip_set_id_t index = 0, id;
1961 + int res = 0;
1962 +
1963 + DP("setname: %s, typename: %s, id: %u", name, typename, restore);
1964 + /*
1965 + * First, and without any locks, allocate and initialize
1966 + * a normal base set structure.
1967 + */
1968 + set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
1969 + if (!set)
1970 + return -ENOMEM;
1971 + set->lock = RW_LOCK_UNLOCKED;
1972 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
1973 + set->binding = IP_SET_INVALID_ID;
1974 + atomic_set(&set->ref, 0);
1975 +
1976 + /*
1977 + * Next, take the &ip_set_lock, check that we know the type,
1978 + * and take a reference on the type, to make sure it
1979 + * stays available while constructing our new set.
1980 + *
1981 + * After referencing the type, we drop the &ip_set_lock,
1982 + * and let the new set construction run without locks.
1983 + */
1984 + set->type = find_set_type_rlock(typename);
1985 + if (set->type == NULL) {
1986 + /* Try loading the module */
1987 + char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
1988 + strcpy(modulename, "ip_set_");
1989 + strcat(modulename, typename);
1990 + DP("try to load %s", modulename);
1991 + request_module(modulename);
1992 + set->type = find_set_type_rlock(typename);
1993 + }
1994 + if (set->type == NULL) {
1995 + ip_set_printk("no set type '%s', set '%s' not created",
1996 + typename, name);
1997 + res = -ENOENT;
1998 + goto out;
1999 + }
2000 + if (!try_module_get(set->type->me)) {
2001 + read_unlock_bh(&ip_set_lock);
2002 + res = -EFAULT;
2003 + goto out;
2004 + }
2005 + read_unlock_bh(&ip_set_lock);
2006 +
2007 + /*
2008 + * Without holding any locks, create private part.
2009 + */
2010 + res = set->type->create(set, data, size);
2011 + if (res != 0)
2012 + goto put_out;
2013 +
2014 + /* BTW, res==0 here. */
2015 +
2016 + /*
2017 + * Here, we have a valid, constructed set. &ip_set_lock again,
2018 + * find free id/index and check that it is not already in
2019 + * ip_set_list.
2020 + */
2021 + write_lock_bh(&ip_set_lock);
2022 + if ((res = find_free_id(set->name, &index, &id)) != 0) {
2023 + DP("no free id!");
2024 + goto cleanup;
2025 + }
2026 +
2027 + /* Make sure restore gets the same index */
2028 + if (restore != IP_SET_INVALID_ID && index != restore) {
2029 + DP("Can't restore, sets are screwed up");
2030 + res = -ERANGE;
2031 + goto cleanup;
2032 + }
2033 +
2034 + /*
2035 + * Finally! Add our shiny new set to the list, and be done.
2036 + */
2037 + DP("create: '%s' created with index %u, id %u!", set->name, index, id);
2038 + set->id = id;
2039 + ip_set_list[index] = set;
2040 + write_unlock_bh(&ip_set_lock);
2041 + return res;
2042 +
2043 + cleanup:
2044 + write_unlock_bh(&ip_set_lock);
2045 + set->type->destroy(set);
2046 + put_out:
2047 + module_put(set->type->me);
2048 + out:
2049 + kfree(set);
2050 + return res;
2051 +}
2052 +
2053 +/*
2054 + * Destroy a given existing set
2055 + */
2056 +static void
2057 +ip_set_destroy_set(ip_set_id_t index)
2058 +{
2059 + struct ip_set *set = ip_set_list[index];
2060 +
2061 + IP_SET_ASSERT(set);
2062 + DP("set: %s", set->name);
2063 + write_lock_bh(&ip_set_lock);
2064 + FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
2065 + if (set->binding != IP_SET_INVALID_ID)
2066 + __ip_set_put(set->binding);
2067 + ip_set_list[index] = NULL;
2068 + write_unlock_bh(&ip_set_lock);
2069 +
2070 + /* Must call it without holding any lock */
2071 + set->type->destroy(set);
2072 + module_put(set->type->me);
2073 + kfree(set);
2074 +}
2075 +
2076 +/*
2077 + * Destroy a set - or all sets
2078 + * Sets must not be referenced/used.
2079 + */
2080 +static int
2081 +ip_set_destroy(ip_set_id_t index)
2082 +{
2083 + ip_set_id_t i;
2084 +
2085 + /* ref modification always protected by the mutex */
2086 + if (index != IP_SET_INVALID_ID) {
2087 + if (atomic_read(&ip_set_list[index]->ref))
2088 + return -EBUSY;
2089 + ip_set_destroy_set(index);
2090 + } else {
2091 + for (i = 0; i < ip_set_max; i++) {
2092 + if (ip_set_list[i] != NULL
2093 + && (atomic_read(&ip_set_list[i]->ref)))
2094 + return -EBUSY;
2095 + }
2096 +
2097 + for (i = 0; i < ip_set_max; i++) {
2098 + if (ip_set_list[i] != NULL)
2099 + ip_set_destroy_set(i);
2100 + }
2101 + }
2102 + return 0;
2103 +}
2104 +
2105 +static void
2106 +ip_set_flush_set(struct ip_set *set)
2107 +{
2108 + DP("set: %s %u", set->name, set->id);
2109 +
2110 + write_lock_bh(&set->lock);
2111 + set->type->flush(set);
2112 + write_unlock_bh(&set->lock);
2113 +}
2114 +
2115 +/*
2116 + * Flush data in a set - or in all sets
2117 + */
2118 +static int
2119 +ip_set_flush(ip_set_id_t index)
2120 +{
2121 + if (index != IP_SET_INVALID_ID) {
2122 + IP_SET_ASSERT(ip_set_list[index]);
2123 + ip_set_flush_set(ip_set_list[index]);
2124 + } else
2125 + FOREACH_SET_DO(ip_set_flush_set);
2126 +
2127 + return 0;
2128 +}
2129 +
2130 +/* Rename a set */
2131 +static int
2132 +ip_set_rename(ip_set_id_t index, const char *name)
2133 +{
2134 + struct ip_set *set = ip_set_list[index];
2135 + ip_set_id_t i;
2136 + int res = 0;
2137 +
2138 + DP("set: %s to %s", set->name, name);
2139 + write_lock_bh(&ip_set_lock);
2140 + for (i = 0; i < ip_set_max; i++) {
2141 + if (ip_set_list[i] != NULL
2142 + && strncmp(ip_set_list[i]->name,
2143 + name,
2144 + IP_SET_MAXNAMELEN - 1) == 0) {
2145 + res = -EEXIST;
2146 + goto unlock;
2147 + }
2148 + }
2149 + strncpy(set->name, name, IP_SET_MAXNAMELEN);
2150 + unlock:
2151 + write_unlock_bh(&ip_set_lock);
2152 + return res;
2153 +}
2154 +
2155 +/*
2156 + * Swap two sets so that name/index points to the other.
2157 + * References are also swapped.
2158 + */
2159 +static int
2160 +ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
2161 +{
2162 + struct ip_set *from = ip_set_list[from_index];
2163 + struct ip_set *to = ip_set_list[to_index];
2164 + char from_name[IP_SET_MAXNAMELEN];
2165 + u_int32_t from_ref;
2166 +
2167 + DP("set: %s to %s", from->name, to->name);
2168 + /* Features must not change. Artifical restriction. */
2169 + if (from->type->features != to->type->features)
2170 + return -ENOEXEC;
2171 +
2172 + /* No magic here: ref munging protected by the mutex */
2173 + write_lock_bh(&ip_set_lock);
2174 + strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
2175 + from_ref = atomic_read(&from->ref);
2176 +
2177 + strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
2178 + atomic_set(&from->ref, atomic_read(&to->ref));
2179 + strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
2180 + atomic_set(&to->ref, from_ref);
2181 +
2182 + ip_set_list[from_index] = to;
2183 + ip_set_list[to_index] = from;
2184 +
2185 + write_unlock_bh(&ip_set_lock);
2186 + return 0;
2187 +}
2188 +
2189 +/*
2190 + * List set data
2191 + */
2192 +
2193 +static inline void
2194 +__set_hash_bindings_size_list(struct ip_set_hash *set_hash,
2195 + ip_set_id_t id, size_t *size)
2196 +{
2197 + if (set_hash->id == id)
2198 + *size += sizeof(struct ip_set_hash_list);
2199 +}
2200 +
2201 +static inline void
2202 +__set_hash_bindings_size_save(struct ip_set_hash *set_hash,
2203 + ip_set_id_t id, size_t *size)
2204 +{
2205 + if (set_hash->id == id)
2206 + *size += sizeof(struct ip_set_hash_save);
2207 +}
2208 +
2209 +static inline void
2210 +__set_hash_bindings(struct ip_set_hash *set_hash,
2211 + ip_set_id_t id, void *data, int *used)
2212 +{
2213 + if (set_hash->id == id) {
2214 + struct ip_set_hash_list *hash_list =
2215 + (struct ip_set_hash_list *)(data + *used);
2216 +
2217 + hash_list->ip = set_hash->ip;
2218 + hash_list->binding = set_hash->binding;
2219 + *used += sizeof(struct ip_set_hash_list);
2220 + }
2221 +}
2222 +
2223 +static int ip_set_list_set(ip_set_id_t index,
2224 + void *data,
2225 + int *used,
2226 + int len)
2227 +{
2228 + struct ip_set *set = ip_set_list[index];
2229 + struct ip_set_list *set_list;
2230 +
2231 + /* Pointer to our header */
2232 + set_list = (struct ip_set_list *) (data + *used);
2233 +
2234 + DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
2235 +
2236 + /* Get and ensure header size */
2237 + if (*used + sizeof(struct ip_set_list) > len)
2238 + goto not_enough_mem;
2239 + *used += sizeof(struct ip_set_list);
2240 +
2241 + read_lock_bh(&set->lock);
2242 + /* Get and ensure set specific header size */
2243 + set_list->header_size = set->type->header_size;
2244 + if (*used + set_list->header_size > len)
2245 + goto unlock_set;
2246 +
2247 + /* Fill in the header */
2248 + set_list->index = index;
2249 + set_list->binding = set->binding;
2250 + set_list->ref = atomic_read(&set->ref);
2251 +
2252 + /* Fill in set spefific header data */
2253 + set->type->list_header(set, data + *used);
2254 + *used += set_list->header_size;
2255 +
2256 + /* Get and ensure set specific members size */
2257 + set_list->members_size = set->type->list_members_size(set);
2258 + if (*used + set_list->members_size > len)
2259 + goto unlock_set;
2260 +
2261 + /* Fill in set spefific members data */
2262 + set->type->list_members(set, data + *used);
2263 + *used += set_list->members_size;
2264 + read_unlock_bh(&set->lock);
2265 +
2266 + /* Bindings */
2267 +
2268 + /* Get and ensure set specific bindings size */
2269 + set_list->bindings_size = 0;
2270 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2271 + set->id, &set_list->bindings_size);
2272 + if (*used + set_list->bindings_size > len)
2273 + goto not_enough_mem;
2274 +
2275 + /* Fill in set spefific bindings data */
2276 + FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
2277 +
2278 + return 0;
2279 +
2280 + unlock_set:
2281 + read_unlock_bh(&set->lock);
2282 + not_enough_mem:
2283 + DP("not enough mem, try again");
2284 + return -EAGAIN;
2285 +}
2286 +
2287 +/*
2288 + * Save sets
2289 + */
2290 +static int ip_set_save_set(ip_set_id_t index,
2291 + void *data,
2292 + int *used,
2293 + int len)
2294 +{
2295 + struct ip_set *set;
2296 + struct ip_set_save *set_save;
2297 +
2298 + /* Pointer to our header */
2299 + set_save = (struct ip_set_save *) (data + *used);
2300 +
2301 + /* Get and ensure header size */
2302 + if (*used + sizeof(struct ip_set_save) > len)
2303 + goto not_enough_mem;
2304 + *used += sizeof(struct ip_set_save);
2305 +
2306 + set = ip_set_list[index];
2307 + DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
2308 + data, data + *used);
2309 +
2310 + read_lock_bh(&set->lock);
2311 + /* Get and ensure set specific header size */
2312 + set_save->header_size = set->type->header_size;
2313 + if (*used + set_save->header_size > len)
2314 + goto unlock_set;
2315 +
2316 + /* Fill in the header */
2317 + set_save->index = index;
2318 + set_save->binding = set->binding;
2319 +
2320 + /* Fill in set spefific header data */
2321 + set->type->list_header(set, data + *used);
2322 + *used += set_save->header_size;
2323 +
2324 + DP("set header filled: %s, used: %u(%u) %p %p", set->name, *used,
2325 + set_save->header_size, data, data + *used);
2326 + /* Get and ensure set specific members size */
2327 + set_save->members_size = set->type->list_members_size(set);
2328 + if (*used + set_save->members_size > len)
2329 + goto unlock_set;
2330 +
2331 + /* Fill in set spefific members data */
2332 + set->type->list_members(set, data + *used);
2333 + *used += set_save->members_size;
2334 + read_unlock_bh(&set->lock);
2335 + DP("set members filled: %s, used: %u(%u) %p %p", set->name, *used,
2336 + set_save->members_size, data, data + *used);
2337 + return 0;
2338 +
2339 + unlock_set:
2340 + read_unlock_bh(&set->lock);
2341 + not_enough_mem:
2342 + DP("not enough mem, try again");
2343 + return -EAGAIN;
2344 +}
2345 +
2346 +static inline void
2347 +__set_hash_save_bindings(struct ip_set_hash *set_hash,
2348 + ip_set_id_t id,
2349 + void *data,
2350 + int *used,
2351 + int len,
2352 + int *res)
2353 +{
2354 + if (*res == 0
2355 + && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
2356 + struct ip_set_hash_save *hash_save =
2357 + (struct ip_set_hash_save *)(data + *used);
2358 + /* Ensure bindings size */
2359 + if (*used + sizeof(struct ip_set_hash_save) > len) {
2360 + *res = -ENOMEM;
2361 + return;
2362 + }
2363 + hash_save->id = set_hash->id;
2364 + hash_save->ip = set_hash->ip;
2365 + hash_save->binding = set_hash->binding;
2366 + *used += sizeof(struct ip_set_hash_save);
2367 + }
2368 +}
2369 +
2370 +static int ip_set_save_bindings(ip_set_id_t index,
2371 + void *data,
2372 + int *used,
2373 + int len)
2374 +{
2375 + int res = 0;
2376 + struct ip_set_save *set_save;
2377 +
2378 + DP("used %u, len %u", *used, len);
2379 + /* Get and ensure header size */
2380 + if (*used + sizeof(struct ip_set_save) > len)
2381 + return -ENOMEM;
2382 +
2383 + /* Marker */
2384 + set_save = (struct ip_set_save *) (data + *used);
2385 + set_save->index = IP_SET_INVALID_ID;
2386 + set_save->header_size = 0;
2387 + set_save->members_size = 0;
2388 + *used += sizeof(struct ip_set_save);
2389 +
2390 + DP("marker added used %u, len %u", *used, len);
2391 + /* Fill in bindings data */
2392 + if (index != IP_SET_INVALID_ID)
2393 + /* Sets are identified by id in hash */
2394 + index = ip_set_list[index]->id;
2395 + FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
2396 +
2397 + return res;
2398 +}
2399 +
2400 +/*
2401 + * Restore sets
2402 + */
2403 +static int ip_set_restore(void *data,
2404 + int len)
2405 +{
2406 + int res = 0;
2407 + int line = 0, used = 0, members_size;
2408 + struct ip_set *set;
2409 + struct ip_set_hash_save *hash_save;
2410 + struct ip_set_restore *set_restore;
2411 + ip_set_id_t index;
2412 +
2413 + /* Loop to restore sets */
2414 + while (1) {
2415 + line++;
2416 +
2417 + DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
2418 + /* Get and ensure header size */
2419 + if (used + sizeof(struct ip_set_restore) > len)
2420 + return line;
2421 + set_restore = (struct ip_set_restore *) (data + used);
2422 + used += sizeof(struct ip_set_restore);
2423 +
2424 + /* Ensure data size */
2425 + if (used
2426 + + set_restore->header_size
2427 + + set_restore->members_size > len)
2428 + return line;
2429 +
2430 + /* Check marker */
2431 + if (set_restore->index == IP_SET_INVALID_ID) {
2432 + line--;
2433 + goto bindings;
2434 + }
2435 +
2436 + /* Try to create the set */
2437 + DP("restore %s %s", set_restore->name, set_restore->typename);
2438 + res = ip_set_create(set_restore->name,
2439 + set_restore->typename,
2440 + set_restore->index,
2441 + data + used,
2442 + set_restore->header_size);
2443 +
2444 + if (res != 0)
2445 + return line;
2446 + used += set_restore->header_size;
2447 +
2448 + index = ip_set_find_byindex(set_restore->index);
2449 + DP("index %u, restore_index %u", index, set_restore->index);
2450 + if (index != set_restore->index)
2451 + return line;
2452 + /* Try to restore members data */
2453 + set = ip_set_list[index];
2454 + members_size = 0;
2455 + DP("members_size %u reqsize %u",
2456 + set_restore->members_size, set->type->reqsize);
2457 + while (members_size + set->type->reqsize <=
2458 + set_restore->members_size) {
2459 + line++;
2460 + DP("members: %u, line %u", members_size, line);
2461 + res = __ip_set_addip(index,
2462 + data + used + members_size,
2463 + set->type->reqsize);
2464 + if (!(res == 0 || res == -EEXIST))
2465 + return line;
2466 + members_size += set->type->reqsize;
2467 + }
2468 +
2469 + DP("members_size %u %u",
2470 + set_restore->members_size, members_size);
2471 + if (members_size != set_restore->members_size)
2472 + return line++;
2473 + used += set_restore->members_size;
2474 + }
2475 +
2476 + bindings:
2477 + /* Loop to restore bindings */
2478 + while (used < len) {
2479 + line++;
2480 +
2481 + DP("restore binding, line %u", line);
2482 + /* Get and ensure size */
2483 + if (used + sizeof(struct ip_set_hash_save) > len)
2484 + return line;
2485 + hash_save = (struct ip_set_hash_save *) (data + used);
2486 + used += sizeof(struct ip_set_hash_save);
2487 +
2488 + /* hash_save->id is used to store the index */
2489 + index = ip_set_find_byindex(hash_save->id);
2490 + DP("restore binding index %u, id %u, %u -> %u",
2491 + index, hash_save->id, hash_save->ip, hash_save->binding);
2492 + if (index != hash_save->id)
2493 + return line;
2494 + if (ip_set_find_byindex(hash_save->binding) == IP_SET_INVALID_ID) {
2495 + DP("corrupt binding set index %u", hash_save->binding);
2496 + return line;
2497 + }
2498 + set = ip_set_list[hash_save->id];
2499 + /* Null valued IP means default binding */
2500 + if (hash_save->ip)
2501 + res = ip_set_hash_add(set->id,
2502 + hash_save->ip,
2503 + hash_save->binding);
2504 + else {
2505 + IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
2506 + write_lock_bh(&ip_set_lock);
2507 + set->binding = hash_save->binding;
2508 + __ip_set_get(set->binding);
2509 + write_unlock_bh(&ip_set_lock);
2510 + DP("default binding: %u", set->binding);
2511 + }
2512 + if (res != 0)
2513 + return line;
2514 + }
2515 + if (used != len)
2516 + return line;
2517 +
2518 + return 0;
2519 +}
2520 +
2521 +static int
2522 +ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
2523 +{
2524 + void *data;
2525 + int res = 0; /* Assume OK */
2526 + unsigned *op;
2527 + struct ip_set_req_adt *req_adt;
2528 + ip_set_id_t index = IP_SET_INVALID_ID;
2529 + int (*adtfn)(ip_set_id_t index,
2530 + const void *data, size_t size);
2531 + struct fn_table {
2532 + int (*fn)(ip_set_id_t index,
2533 + const void *data, size_t size);
2534 + } adtfn_table[] =
2535 + { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
2536 + { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
2537 + };
2538 +
2539 + DP("optval=%d, user=%p, len=%d", optval, user, len);
2540 + if (!capable(CAP_NET_ADMIN))
2541 + return -EPERM;
2542 + if (optval != SO_IP_SET)
2543 + return -EBADF;
2544 + if (len <= sizeof(unsigned)) {
2545 + ip_set_printk("short userdata (want >%zu, got %u)",
2546 + sizeof(unsigned), len);
2547 + return -EINVAL;
2548 + }
2549 + data = vmalloc(len);
2550 + if (!data) {
2551 + DP("out of mem for %u bytes", len);
2552 + return -ENOMEM;
2553 + }
2554 + if (copy_from_user(data, user, len) != 0) {
2555 + res = -EFAULT;
2556 + goto done;
2557 + }
2558 + if (down_interruptible(&ip_set_app_mutex)) {
2559 + res = -EINTR;
2560 + goto done;
2561 + }
2562 +
2563 + op = (unsigned *)data;
2564 + DP("op=%x", *op);
2565 +
2566 + if (*op < IP_SET_OP_VERSION) {
2567 + /* Check the version at the beginning of operations */
2568 + struct ip_set_req_version *req_version =
2569 + (struct ip_set_req_version *) data;
2570 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2571 + res = -EPROTO;
2572 + goto done;
2573 + }
2574 + }
2575 +
2576 + switch (*op) {
2577 + case IP_SET_OP_CREATE:{
2578 + struct ip_set_req_create *req_create
2579 + = (struct ip_set_req_create *) data;
2580 +
2581 + if (len < sizeof(struct ip_set_req_create)) {
2582 + ip_set_printk("short CREATE data (want >=%zu, got %u)",
2583 + sizeof(struct ip_set_req_create), len);
2584 + res = -EINVAL;
2585 + goto done;
2586 + }
2587 + req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
2588 + req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2589 + res = ip_set_create(req_create->name,
2590 + req_create->typename,
2591 + IP_SET_INVALID_ID,
2592 + data + sizeof(struct ip_set_req_create),
2593 + len - sizeof(struct ip_set_req_create));
2594 + goto done;
2595 + }
2596 + case IP_SET_OP_DESTROY:{
2597 + struct ip_set_req_std *req_destroy
2598 + = (struct ip_set_req_std *) data;
2599 +
2600 + if (len != sizeof(struct ip_set_req_std)) {
2601 + ip_set_printk("invalid DESTROY data (want %zu, got %u)",
2602 + sizeof(struct ip_set_req_std), len);
2603 + res = -EINVAL;
2604 + goto done;
2605 + }
2606 + if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
2607 + /* Destroy all sets */
2608 + index = IP_SET_INVALID_ID;
2609 + } else {
2610 + req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
2611 + index = ip_set_find_byname(req_destroy->name);
2612 +
2613 + if (index == IP_SET_INVALID_ID) {
2614 + res = -ENOENT;
2615 + goto done;
2616 + }
2617 + }
2618 +
2619 + res = ip_set_destroy(index);
2620 + goto done;
2621 + }
2622 + case IP_SET_OP_FLUSH:{
2623 + struct ip_set_req_std *req_flush =
2624 + (struct ip_set_req_std *) data;
2625 +
2626 + if (len != sizeof(struct ip_set_req_std)) {
2627 + ip_set_printk("invalid FLUSH data (want %zu, got %u)",
2628 + sizeof(struct ip_set_req_std), len);
2629 + res = -EINVAL;
2630 + goto done;
2631 + }
2632 + if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
2633 + /* Flush all sets */
2634 + index = IP_SET_INVALID_ID;
2635 + } else {
2636 + req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
2637 + index = ip_set_find_byname(req_flush->name);
2638 +
2639 + if (index == IP_SET_INVALID_ID) {
2640 + res = -ENOENT;
2641 + goto done;
2642 + }
2643 + }
2644 + res = ip_set_flush(index);
2645 + goto done;
2646 + }
2647 + case IP_SET_OP_RENAME:{
2648 + struct ip_set_req_create *req_rename
2649 + = (struct ip_set_req_create *) data;
2650 +
2651 + if (len != sizeof(struct ip_set_req_create)) {
2652 + ip_set_printk("invalid RENAME data (want %zu, got %u)",
2653 + sizeof(struct ip_set_req_create), len);
2654 + res = -EINVAL;
2655 + goto done;
2656 + }
2657 +
2658 + req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
2659 + req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2660 +
2661 + index = ip_set_find_byname(req_rename->name);
2662 + if (index == IP_SET_INVALID_ID) {
2663 + res = -ENOENT;
2664 + goto done;
2665 + }
2666 + res = ip_set_rename(index, req_rename->typename);
2667 + goto done;
2668 + }
2669 + case IP_SET_OP_SWAP:{
2670 + struct ip_set_req_create *req_swap
2671 + = (struct ip_set_req_create *) data;
2672 + ip_set_id_t to_index;
2673 +
2674 + if (len != sizeof(struct ip_set_req_create)) {
2675 + ip_set_printk("invalid SWAP data (want %zu, got %u)",
2676 + sizeof(struct ip_set_req_create), len);
2677 + res = -EINVAL;
2678 + goto done;
2679 + }
2680 +
2681 + req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
2682 + req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
2683 +
2684 + index = ip_set_find_byname(req_swap->name);
2685 + if (index == IP_SET_INVALID_ID) {
2686 + res = -ENOENT;
2687 + goto done;
2688 + }
2689 + to_index = ip_set_find_byname(req_swap->typename);
2690 + if (to_index == IP_SET_INVALID_ID) {
2691 + res = -ENOENT;
2692 + goto done;
2693 + }
2694 + res = ip_set_swap(index, to_index);
2695 + goto done;
2696 + }
2697 + default:
2698 + break; /* Set identified by id */
2699 + }
2700 +
2701 + /* There we may have add/del/test/bind/unbind/test_bind operations */
2702 + if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
2703 + res = -EBADMSG;
2704 + goto done;
2705 + }
2706 + adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
2707 +
2708 + if (len < sizeof(struct ip_set_req_adt)) {
2709 + ip_set_printk("short data in adt request (want >=%zu, got %u)",
2710 + sizeof(struct ip_set_req_adt), len);
2711 + res = -EINVAL;
2712 + goto done;
2713 + }
2714 + req_adt = (struct ip_set_req_adt *) data;
2715 +
2716 + /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
2717 + if (!(*op == IP_SET_OP_UNBIND_SET
2718 + && req_adt->index == IP_SET_INVALID_ID)) {
2719 + index = ip_set_find_byindex(req_adt->index);
2720 + if (index == IP_SET_INVALID_ID) {
2721 + res = -ENOENT;
2722 + goto done;
2723 + }
2724 + }
2725 + res = adtfn(index, data, len);
2726 +
2727 + done:
2728 + up(&ip_set_app_mutex);
2729 + vfree(data);
2730 + if (res > 0)
2731 + res = 0;
2732 + DP("final result %d", res);
2733 + return res;
2734 +}
2735 +
2736 +static int
2737 +ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
2738 +{
2739 + int res = 0;
2740 + unsigned *op;
2741 + ip_set_id_t index = IP_SET_INVALID_ID;
2742 + void *data;
2743 + int copylen = *len;
2744 +
2745 + DP("optval=%d, user=%p, len=%d", optval, user, *len);
2746 + if (!capable(CAP_NET_ADMIN))
2747 + return -EPERM;
2748 + if (optval != SO_IP_SET)
2749 + return -EBADF;
2750 + if (*len < sizeof(unsigned)) {
2751 + ip_set_printk("short userdata (want >=%zu, got %d)",
2752 + sizeof(unsigned), *len);
2753 + return -EINVAL;
2754 + }
2755 + data = vmalloc(*len);
2756 + if (!data) {
2757 + DP("out of mem for %d bytes", *len);
2758 + return -ENOMEM;
2759 + }
2760 + if (copy_from_user(data, user, *len) != 0) {
2761 + res = -EFAULT;
2762 + goto done;
2763 + }
2764 + if (down_interruptible(&ip_set_app_mutex)) {
2765 + res = -EINTR;
2766 + goto done;
2767 + }
2768 +
2769 + op = (unsigned *) data;
2770 + DP("op=%x", *op);
2771 +
2772 + if (*op < IP_SET_OP_VERSION) {
2773 + /* Check the version at the beginning of operations */
2774 + struct ip_set_req_version *req_version =
2775 + (struct ip_set_req_version *) data;
2776 + if (req_version->version != IP_SET_PROTOCOL_VERSION) {
2777 + res = -EPROTO;
2778 + goto done;
2779 + }
2780 + }
2781 +
2782 + switch (*op) {
2783 + case IP_SET_OP_VERSION: {
2784 + struct ip_set_req_version *req_version =
2785 + (struct ip_set_req_version *) data;
2786 +
2787 + if (*len != sizeof(struct ip_set_req_version)) {
2788 + ip_set_printk("invalid VERSION (want %zu, got %d)",
2789 + sizeof(struct ip_set_req_version),
2790 + *len);
2791 + res = -EINVAL;
2792 + goto done;
2793 + }
2794 +
2795 + req_version->version = IP_SET_PROTOCOL_VERSION;
2796 + res = copy_to_user(user, req_version,
2797 + sizeof(struct ip_set_req_version));
2798 + goto done;
2799 + }
2800 + case IP_SET_OP_GET_BYNAME: {
2801 + struct ip_set_req_get_set *req_get
2802 + = (struct ip_set_req_get_set *) data;
2803 +
2804 + if (*len != sizeof(struct ip_set_req_get_set)) {
2805 + ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
2806 + sizeof(struct ip_set_req_get_set), *len);
2807 + res = -EINVAL;
2808 + goto done;
2809 + }
2810 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2811 + index = ip_set_find_byname(req_get->set.name);
2812 + req_get->set.index = index;
2813 + goto copy;
2814 + }
2815 + case IP_SET_OP_GET_BYINDEX: {
2816 + struct ip_set_req_get_set *req_get
2817 + = (struct ip_set_req_get_set *) data;
2818 +
2819 + if (*len != sizeof(struct ip_set_req_get_set)) {
2820 + ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
2821 + sizeof(struct ip_set_req_get_set), *len);
2822 + res = -EINVAL;
2823 + goto done;
2824 + }
2825 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2826 + index = ip_set_find_byindex(req_get->set.index);
2827 + strncpy(req_get->set.name,
2828 + index == IP_SET_INVALID_ID ? ""
2829 + : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
2830 + goto copy;
2831 + }
2832 + case IP_SET_OP_ADT_GET: {
2833 + struct ip_set_req_adt_get *req_get
2834 + = (struct ip_set_req_adt_get *) data;
2835 +
2836 + if (*len != sizeof(struct ip_set_req_adt_get)) {
2837 + ip_set_printk("invalid ADT_GET (want %zu, got %d)",
2838 + sizeof(struct ip_set_req_adt_get), *len);
2839 + res = -EINVAL;
2840 + goto done;
2841 + }
2842 + req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2843 + index = ip_set_find_byname(req_get->set.name);
2844 + if (index != IP_SET_INVALID_ID) {
2845 + req_get->set.index = index;
2846 + strncpy(req_get->typename,
2847 + ip_set_list[index]->type->typename,
2848 + IP_SET_MAXNAMELEN - 1);
2849 + } else {
2850 + res = -ENOENT;
2851 + goto done;
2852 + }
2853 + goto copy;
2854 + }
2855 + case IP_SET_OP_MAX_SETS: {
2856 + struct ip_set_req_max_sets *req_max_sets
2857 + = (struct ip_set_req_max_sets *) data;
2858 + ip_set_id_t i;
2859 +
2860 + if (*len != sizeof(struct ip_set_req_max_sets)) {
2861 + ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
2862 + sizeof(struct ip_set_req_max_sets), *len);
2863 + res = -EINVAL;
2864 + goto done;
2865 + }
2866 +
2867 + if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
2868 + req_max_sets->set.index = IP_SET_INVALID_ID;
2869 + } else {
2870 + req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
2871 + req_max_sets->set.index =
2872 + ip_set_find_byname(req_max_sets->set.name);
2873 + if (req_max_sets->set.index == IP_SET_INVALID_ID) {
2874 + res = -ENOENT;
2875 + goto done;
2876 + }
2877 + }
2878 + req_max_sets->max_sets = ip_set_max;
2879 + req_max_sets->sets = 0;
2880 + for (i = 0; i < ip_set_max; i++) {
2881 + if (ip_set_list[i] != NULL)
2882 + req_max_sets->sets++;
2883 + }
2884 + goto copy;
2885 + }
2886 + case IP_SET_OP_LIST_SIZE:
2887 + case IP_SET_OP_SAVE_SIZE: {
2888 + struct ip_set_req_setnames *req_setnames
2889 + = (struct ip_set_req_setnames *) data;
2890 + struct ip_set_name_list *name_list;
2891 + struct ip_set *set;
2892 + ip_set_id_t i;
2893 + int used;
2894 +
2895 + if (*len < sizeof(struct ip_set_req_setnames)) {
2896 + ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
2897 + sizeof(struct ip_set_req_setnames), *len);
2898 + res = -EINVAL;
2899 + goto done;
2900 + }
2901 +
2902 + req_setnames->size = 0;
2903 + used = sizeof(struct ip_set_req_setnames);
2904 + for (i = 0; i < ip_set_max; i++) {
2905 + if (ip_set_list[i] == NULL)
2906 + continue;
2907 + name_list = (struct ip_set_name_list *)
2908 + (data + used);
2909 + used += sizeof(struct ip_set_name_list);
2910 + if (used > copylen) {
2911 + res = -EAGAIN;
2912 + goto done;
2913 + }
2914 + set = ip_set_list[i];
2915 + /* Fill in index, name, etc. */
2916 + name_list->index = i;
2917 + name_list->id = set->id;
2918 + strncpy(name_list->name,
2919 + set->name,
2920 + IP_SET_MAXNAMELEN - 1);
2921 + strncpy(name_list->typename,
2922 + set->type->typename,
2923 + IP_SET_MAXNAMELEN - 1);
2924 + DP("filled %s of type %s, index %u\n",
2925 + name_list->name, name_list->typename,
2926 + name_list->index);
2927 + if (!(req_setnames->index == IP_SET_INVALID_ID
2928 + || req_setnames->index == i))
2929 + continue;
2930 + /* Update size */
2931 + switch (*op) {
2932 + case IP_SET_OP_LIST_SIZE: {
2933 + req_setnames->size += sizeof(struct ip_set_list)
2934 + + set->type->header_size
2935 + + set->type->list_members_size(set);
2936 + /* Sets are identified by id in the hash */
2937 + FOREACH_HASH_DO(__set_hash_bindings_size_list,
2938 + set->id, &req_setnames->size);
2939 + break;
2940 + }
2941 + case IP_SET_OP_SAVE_SIZE: {
2942 + req_setnames->size += sizeof(struct ip_set_save)
2943 + + set->type->header_size
2944 + + set->type->list_members_size(set);
2945 + FOREACH_HASH_DO(__set_hash_bindings_size_save,
2946 + set->id, &req_setnames->size);
2947 + break;
2948 + }
2949 + default:
2950 + break;
2951 + }
2952 + }
2953 + if (copylen != used) {
2954 + res = -EAGAIN;
2955 + goto done;
2956 + }
2957 + goto copy;
2958 + }
2959 + case IP_SET_OP_LIST: {
2960 + struct ip_set_req_list *req_list
2961 + = (struct ip_set_req_list *) data;
2962 + ip_set_id_t i;
2963 + int used;
2964 +
2965 + if (*len < sizeof(struct ip_set_req_list)) {
2966 + ip_set_printk("short LIST (want >=%zu, got %d)",
2967 + sizeof(struct ip_set_req_list), *len);
2968 + res = -EINVAL;
2969 + goto done;
2970 + }
2971 + index = req_list->index;
2972 + if (index != IP_SET_INVALID_ID
2973 + && ip_set_find_byindex(index) != index) {
2974 + res = -ENOENT;
2975 + goto done;
2976 + }
2977 + used = 0;
2978 + if (index == IP_SET_INVALID_ID) {
2979 + /* List all sets */
2980 + for (i = 0; i < ip_set_max && res == 0; i++) {
2981 + if (ip_set_list[i] != NULL)
2982 + res = ip_set_list_set(i, data, &used, *len);
2983 + }
2984 + } else {
2985 + /* List an individual set */
2986 + res = ip_set_list_set(index, data, &used, *len);
2987 + }
2988 + if (res != 0)
2989 + goto done;
2990 + else if (copylen != used) {
2991 + res = -EAGAIN;
2992 + goto done;
2993 + }
2994 + goto copy;
2995 + }
2996 + case IP_SET_OP_SAVE: {
2997 + struct ip_set_req_list *req_save
2998 + = (struct ip_set_req_list *) data;
2999 + ip_set_id_t i;
3000 + int used;
3001 +
3002 + if (*len < sizeof(struct ip_set_req_list)) {
3003 + ip_set_printk("short SAVE (want >=%zu, got %d)",
3004 + sizeof(struct ip_set_req_list), *len);
3005 + res = -EINVAL;
3006 + goto done;
3007 + }
3008 + index = req_save->index;
3009 + if (index != IP_SET_INVALID_ID
3010 + && ip_set_find_byindex(index) != index) {
3011 + res = -ENOENT;
3012 + goto done;
3013 + }
3014 + used = 0;
3015 + if (index == IP_SET_INVALID_ID) {
3016 + /* Save all sets */
3017 + for (i = 0; i < ip_set_max && res == 0; i++) {
3018 + if (ip_set_list[i] != NULL)
3019 + res = ip_set_save_set(i, data, &used, *len);
3020 + }
3021 + } else {
3022 + /* Save an individual set */
3023 + res = ip_set_save_set(index, data, &used, *len);
3024 + }
3025 + if (res == 0)
3026 + res = ip_set_save_bindings(index, data, &used, *len);
3027 +
3028 + if (res != 0)
3029 + goto done;
3030 + else if (copylen != used) {
3031 + res = -EAGAIN;
3032 + goto done;
3033 + }
3034 + goto copy;
3035 + }
3036 + case IP_SET_OP_RESTORE: {
3037 + struct ip_set_req_setnames *req_restore
3038 + = (struct ip_set_req_setnames *) data;
3039 + int line;
3040 +
3041 + if (*len < sizeof(struct ip_set_req_setnames)
3042 + || *len != req_restore->size) {
3043 + ip_set_printk("invalid RESTORE (want =%zu, got %d)",
3044 + req_restore->size, *len);
3045 + res = -EINVAL;
3046 + goto done;
3047 + }
3048 + line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
3049 + req_restore->size - sizeof(struct ip_set_req_setnames));
3050 + DP("ip_set_restore: %u", line);
3051 + if (line != 0) {
3052 + res = -EAGAIN;
3053 + req_restore->size = line;
3054 + copylen = sizeof(struct ip_set_req_setnames);
3055 + goto copy;
3056 + }
3057 + goto done;
3058 + }
3059 + default:
3060 + res = -EBADMSG;
3061 + goto done;
3062 + } /* end of switch(op) */
3063 +
3064 + copy:
3065 + DP("set %s, copylen %u", index != IP_SET_INVALID_ID
3066 + && ip_set_list[index]
3067 + ? ip_set_list[index]->name
3068 + : ":all:", copylen);
3069 + res = copy_to_user(user, data, copylen);
3070 +
3071 + done:
3072 + up(&ip_set_app_mutex);
3073 + vfree(data);
3074 + if (res > 0)
3075 + res = 0;
3076 + DP("final result %d", res);
3077 + return res;
3078 +}
3079 +
3080 +static struct nf_sockopt_ops so_set = {
3081 + .pf = PF_INET,
3082 + .set_optmin = SO_IP_SET,
3083 + .set_optmax = SO_IP_SET + 1,
3084 + .set = &ip_set_sockfn_set,
3085 + .get_optmin = SO_IP_SET,
3086 + .get_optmax = SO_IP_SET + 1,
3087 + .get = &ip_set_sockfn_get,
3088 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
3089 + .owner = THIS_MODULE,
3090 +#endif
3091 +};
3092 +
3093 +static int max_sets, hash_size;
3094 +module_param(max_sets, int, 0600);
3095 +MODULE_PARM_DESC(max_sets, "maximal number of sets");
3096 +module_param(hash_size, int, 0600);
3097 +MODULE_PARM_DESC(hash_size, "hash size for bindings");
3098 +MODULE_LICENSE("GPL");
3099 +MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
3100 +MODULE_DESCRIPTION("module implementing core IP set support");
3101 +
3102 +static int __init ip_set_init(void)
3103 +{
3104 + int res;
3105 + ip_set_id_t i;
3106 +
3107 + get_random_bytes(&ip_set_hash_random, 4);
3108 + if (max_sets)
3109 + ip_set_max = max_sets;
3110 + ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
3111 + if (!ip_set_list) {
3112 + printk(KERN_ERR "Unable to create ip_set_list\n");
3113 + return -ENOMEM;
3114 + }
3115 + memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
3116 + if (hash_size)
3117 + ip_set_bindings_hash_size = hash_size;
3118 + ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
3119 + if (!ip_set_hash) {
3120 + printk(KERN_ERR "Unable to create ip_set_hash\n");
3121 + vfree(ip_set_list);
3122 + return -ENOMEM;
3123 + }
3124 + for (i = 0; i < ip_set_bindings_hash_size; i++)
3125 + INIT_LIST_HEAD(&ip_set_hash[i]);
3126 +
3127 + INIT_LIST_HEAD(&set_type_list);
3128 +
3129 + res = nf_register_sockopt(&so_set);
3130 + if (res != 0) {
3131 + ip_set_printk("SO_SET registry failed: %d", res);
3132 + vfree(ip_set_list);
3133 + vfree(ip_set_hash);
3134 + return res;
3135 + }
3136 + return 0;
3137 +}
3138 +
3139 +static void __exit ip_set_fini(void)
3140 +{
3141 + /* There can't be any existing set or binding */
3142 + nf_unregister_sockopt(&so_set);
3143 + vfree(ip_set_list);
3144 + vfree(ip_set_hash);
3145 + DP("these are the famous last words");
3146 +}
3147 +
3148 +EXPORT_SYMBOL(ip_set_register_set_type);
3149 +EXPORT_SYMBOL(ip_set_unregister_set_type);
3150 +
3151 +EXPORT_SYMBOL(ip_set_get_byname);
3152 +EXPORT_SYMBOL(ip_set_get_byindex);
3153 +EXPORT_SYMBOL(ip_set_put);
3154 +
3155 +EXPORT_SYMBOL(ip_set_addip_kernel);
3156 +EXPORT_SYMBOL(ip_set_delip_kernel);
3157 +EXPORT_SYMBOL(ip_set_testip_kernel);
3158 +
3159 +module_init(ip_set_init);
3160 +module_exit(ip_set_fini);
3161 --- /dev/null
3162 +++ b/net/ipv4/netfilter/ip_set_iphash.c
3163 @@ -0,0 +1,429 @@
3164 +/* Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3165 + *
3166 + * This program is free software; you can redistribute it and/or modify
3167 + * it under the terms of the GNU General Public License version 2 as
3168 + * published by the Free Software Foundation.
3169 + */
3170 +
3171 +/* Kernel module implementing an ip hash set */
3172 +
3173 +#include <linux/module.h>
3174 +#include <linux/ip.h>
3175 +#include <linux/skbuff.h>
3176 +#include <linux/version.h>
3177 +#include <linux/jhash.h>
3178 +#include <linux/netfilter_ipv4/ip_tables.h>
3179 +#include <linux/netfilter_ipv4/ip_set.h>
3180 +#include <linux/errno.h>
3181 +#include <asm/uaccess.h>
3182 +#include <asm/bitops.h>
3183 +#include <linux/spinlock.h>
3184 +#include <linux/vmalloc.h>
3185 +#include <linux/random.h>
3186 +
3187 +#include <net/ip.h>
3188 +
3189 +#include <linux/netfilter_ipv4/ip_set_malloc.h>
3190 +#include <linux/netfilter_ipv4/ip_set_iphash.h>
3191 +
3192 +static int limit = MAX_RANGE;
3193 +
3194 +static inline __u32
3195 +jhash_ip(const struct ip_set_iphash *map, uint16_t i, ip_set_ip_t ip)
3196 +{
3197 + return jhash_1word(ip, *(((uint32_t *) map->initval) + i));
3198 +}
3199 +
3200 +static inline __u32
3201 +hash_id(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3202 +{
3203 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3204 + __u32 id;
3205 + u_int16_t i;
3206 + ip_set_ip_t *elem;
3207 +
3208 + *hash_ip = ip & map->netmask;
3209 + DP("set: %s, ip:%u.%u.%u.%u, %u.%u.%u.%u, %u.%u.%u.%u",
3210 + set->name, HIPQUAD(ip), HIPQUAD(*hash_ip), HIPQUAD(map->netmask));
3211 +
3212 + for (i = 0; i < map->probes; i++) {
3213 + id = jhash_ip(map, i, *hash_ip) % map->hashsize;
3214 + DP("hash key: %u", id);
3215 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3216 + if (*elem == *hash_ip)
3217 + return id;
3218 + /* No shortcut at testing - there can be deleted
3219 + * entries. */
3220 + }
3221 + return UINT_MAX;
3222 +}
3223 +
3224 +static inline int
3225 +__testip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3226 +{
3227 + return (ip && hash_id(set, ip, hash_ip) != UINT_MAX);
3228 +}
3229 +
3230 +static int
3231 +testip(struct ip_set *set, const void *data, size_t size,
3232 + ip_set_ip_t *hash_ip)
3233 +{
3234 + struct ip_set_req_iphash *req =
3235 + (struct ip_set_req_iphash *) data;
3236 +
3237 + if (size != sizeof(struct ip_set_req_iphash)) {
3238 + ip_set_printk("data length wrong (want %zu, have %zu)",
3239 + sizeof(struct ip_set_req_iphash),
3240 + size);
3241 + return -EINVAL;
3242 + }
3243 + return __testip(set, req->ip, hash_ip);
3244 +}
3245 +
3246 +static int
3247 +testip_kernel(struct ip_set *set,
3248 + const struct sk_buff *skb,
3249 + ip_set_ip_t *hash_ip,
3250 + const u_int32_t *flags,
3251 + unsigned char index)
3252 +{
3253 + return __testip(set,
3254 + ntohl(flags[index] & IPSET_SRC
3255 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3256 + ? ip_hdr(skb)->saddr
3257 + : ip_hdr(skb)->daddr),
3258 +#else
3259 + ? skb->nh.iph->saddr
3260 + : skb->nh.iph->daddr),
3261 +#endif
3262 + hash_ip);
3263 +}
3264 +
3265 +static inline int
3266 +__addip(struct ip_set_iphash *map, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3267 +{
3268 + __u32 probe;
3269 + u_int16_t i;
3270 + ip_set_ip_t *elem;
3271 +
3272 + if (!ip || map->elements >= limit)
3273 + return -ERANGE;
3274 +
3275 + *hash_ip = ip & map->netmask;
3276 +
3277 + for (i = 0; i < map->probes; i++) {
3278 + probe = jhash_ip(map, i, *hash_ip) % map->hashsize;
3279 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, probe);
3280 + if (*elem == *hash_ip)
3281 + return -EEXIST;
3282 + if (!*elem) {
3283 + *elem = *hash_ip;
3284 + map->elements++;
3285 + return 0;
3286 + }
3287 + }
3288 + /* Trigger rehashing */
3289 + return -EAGAIN;
3290 +}
3291 +
3292 +static int
3293 +addip(struct ip_set *set, const void *data, size_t size,
3294 + ip_set_ip_t *hash_ip)
3295 +{
3296 + struct ip_set_req_iphash *req =
3297 + (struct ip_set_req_iphash *) data;
3298 +
3299 + if (size != sizeof(struct ip_set_req_iphash)) {
3300 + ip_set_printk("data length wrong (want %zu, have %zu)",
3301 + sizeof(struct ip_set_req_iphash),
3302 + size);
3303 + return -EINVAL;
3304 + }
3305 + return __addip((struct ip_set_iphash *) set->data, req->ip, hash_ip);
3306 +}
3307 +
3308 +static int
3309 +addip_kernel(struct ip_set *set,
3310 + const struct sk_buff *skb,
3311 + ip_set_ip_t *hash_ip,
3312 + const u_int32_t *flags,
3313 + unsigned char index)
3314 +{
3315 + return __addip((struct ip_set_iphash *) set->data,
3316 + ntohl(flags[index] & IPSET_SRC
3317 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
3318 + ? ip_hdr(skb)->saddr
3319 + : ip_hdr(skb)->daddr),
3320 +#else
3321 + ? skb->nh.iph->saddr
3322 + : skb->nh.iph->daddr),
3323 +#endif
3324 + hash_ip);
3325 +}
3326 +
3327 +static int retry(struct ip_set *set)
3328 +{
3329 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3330 + ip_set_ip_t hash_ip, *elem;
3331 + void *members;
3332 + u_int32_t i, hashsize = map->hashsize;
3333 + int res;
3334 + struct ip_set_iphash *tmp;
3335 +
3336 + if (map->resize == 0)
3337 + return -ERANGE;
3338 +
3339 + again:
3340 + res = 0;
3341 +
3342 + /* Calculate new hash size */
3343 + hashsize += (hashsize * map->resize)/100;
3344 + if (hashsize == map->hashsize)
3345 + hashsize++;
3346 +
3347 + ip_set_printk("rehashing of set %s triggered: "
3348 + "hashsize grows from %u to %u",
3349 + set->name, map->hashsize, hashsize);
3350 +
3351 + tmp = kmalloc(sizeof(struct ip_set_iphash)
3352 + + map->probes * sizeof(uint32_t), GFP_ATOMIC);
3353 + if (!tmp) {
3354 + DP("out of memory for %d bytes",
3355 + sizeof(struct ip_set_iphash)
3356 + + map->probes * sizeof(uint32_t));
3357 + return -ENOMEM;
3358 + }
3359 + tmp->members = harray_malloc(hashsize, sizeof(ip_set_ip_t), GFP_ATOMIC);
3360 + if (!tmp->members) {
3361 + DP("out of memory for %d bytes", hashsize * sizeof(ip_set_ip_t));
3362 + kfree(tmp);
3363 + return -ENOMEM;
3364 + }
3365 + tmp->hashsize = hashsize;
3366 + tmp->elements = 0;
3367 + tmp->probes = map->probes;
3368 + tmp->resize = map->resize;
3369 + tmp->netmask = map->netmask;
3370 + memcpy(tmp->initval, map->initval, map->probes * sizeof(uint32_t));
3371 +
3372 + write_lock_bh(&set->lock);
3373 + map = (struct ip_set_iphash *) set->data; /* Play safe */
3374 + for (i = 0; i < map->hashsize && res == 0; i++) {
3375 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, i);
3376 + if (*elem)
3377 + res = __addip(tmp, *elem, &hash_ip);
3378 + }
3379 + if (res) {
3380 + /* Failure, try again */
3381 + write_unlock_bh(&set->lock);
3382 + harray_free(tmp->members);
3383 + kfree(tmp);
3384 + goto again;
3385 + }
3386 +
3387 + /* Success at resizing! */
3388 + members = map->members;
3389 +
3390 + map->hashsize = tmp->hashsize;
3391 + map->members = tmp->members;
3392 + write_unlock_bh(&set->lock);
3393 +
3394 + harray_free(members);
3395 + kfree(tmp);
3396 +
3397 + return 0;
3398 +}
3399 +
3400 +static inline int
3401 +__delip(struct ip_set *set, ip_set_ip_t ip, ip_set_ip_t *hash_ip)
3402 +{
3403 + struct ip_set_iphash *map = (struct ip_set_iphash *) set->data;
3404 + ip_set_ip_t id, *elem;
3405 +
3406 + if (!ip)
3407 + return -ERANGE;
3408 +
3409 + id = hash_id(set, ip, hash_ip);
3410 + if (id == UINT_MAX)
3411 + return -EEXIST;
3412 +
3413 + elem = HARRAY_ELEM(map->members, ip_set_ip_t *, id);
3414 + *elem = 0;
3415 + map->elements--;
3416 +
3417 + return 0;
3418 +}