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