1 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Copyright (C) 2003-2004 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 /* Kernel module for IP set management */
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/kmod.h>
17 #include <linux/skbuff.h>
18 #include <linux/random.h>
19 #include <linux/jhash.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/errno.h>
22 #include <asm/uaccess.h>
23 #include <asm/bitops.h>
24 #include <asm/semaphore.h>
25 #include <linux/spinlock.h>
26 #include <linux/vmalloc.h>
28 #define ASSERT_READ_LOCK(x) /* dont use that */
29 #define ASSERT_WRITE_LOCK(x)
30 #include <linux/netfilter_ipv4/listhelp.h>
31 #include <linux/netfilter_ipv4/ip_set.h>
33 static struct list_head set_type_list; /* all registered sets */
34 static struct ip_set **ip_set_list; /* all individual sets */
35 static DEFINE_RWLOCK(ip_set_lock); /* protects the lists and the hash */
36 static DECLARE_MUTEX(ip_set_app_mutex); /* serializes user access */
37 static ip_set_id_t ip_set_max = CONFIG_IP_NF_SET_MAX;
38 static ip_set_id_t ip_set_bindings_hash_size = CONFIG_IP_NF_SET_HASHSIZE;
39 static struct list_head *ip_set_hash; /* hash of bindings */
40 static unsigned int ip_set_hash_random; /* random seed */
43 * Sets are identified either by the index in ip_set_list or by id.
44 * The id never changes and is used to find a key in the hash.
45 * The index may change by swapping and used at all other places
46 * (set/SET netfilter modules, binding value, etc.)
48 * Userspace requests are serialized by ip_set_mutex and sets can
49 * be deleted only from userspace. Therefore ip_set_list locking
50 * must obey the following rules:
52 * - kernel requests: read and write locking mandatory
53 * - user requests: read locking optional, write locking mandatory
57 __ip_set_get(ip_set_id_t index)
59 atomic_inc(&ip_set_list[index]->ref);
63 __ip_set_put(ip_set_id_t index)
65 atomic_dec(&ip_set_list[index]->ref);
73 ip_hash_cmp(const struct ip_set_hash *set_hash,
74 ip_set_id_t id, ip_set_ip_t ip)
76 return set_hash->id == id && set_hash->ip == ip;
80 ip_set_find_in_hash(ip_set_id_t id, ip_set_ip_t ip)
82 u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
83 % ip_set_bindings_hash_size;
84 struct ip_set_hash *set_hash;
86 ASSERT_READ_LOCK(&ip_set_lock);
87 IP_SET_ASSERT(ip_set_list[id]);
88 DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
90 set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
91 struct ip_set_hash *, id, ip);
93 DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
95 set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
97 return (set_hash != NULL ? set_hash->binding : IP_SET_INVALID_ID);
101 __set_hash_del(struct ip_set_hash *set_hash)
103 ASSERT_WRITE_LOCK(&ip_set_lock);
104 IP_SET_ASSERT(ip_set_list[set_hash->binding]);
106 __ip_set_put(set_hash->binding);
107 list_del(&set_hash->list);
112 ip_set_hash_del(ip_set_id_t id, ip_set_ip_t ip)
114 u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
115 % ip_set_bindings_hash_size;
116 struct ip_set_hash *set_hash;
118 IP_SET_ASSERT(ip_set_list[id]);
119 DP("set: %s, ip: %u.%u.%u.%u", ip_set_list[id]->name, HIPQUAD(ip));
120 write_lock_bh(&ip_set_lock);
121 set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
122 struct ip_set_hash *, id, ip);
123 DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
125 set_hash != NULL ? ip_set_list[set_hash->binding]->name : "");
127 if (set_hash != NULL)
128 __set_hash_del(set_hash);
129 write_unlock_bh(&ip_set_lock);
134 ip_set_hash_add(ip_set_id_t id, ip_set_ip_t ip, ip_set_id_t binding)
136 u_int32_t key = jhash_2words(id, ip, ip_set_hash_random)
137 % ip_set_bindings_hash_size;
138 struct ip_set_hash *set_hash;
141 IP_SET_ASSERT(ip_set_list[id]);
142 IP_SET_ASSERT(ip_set_list[binding]);
143 DP("set: %s, ip: %u.%u.%u.%u, binding: %s", ip_set_list[id]->name,
144 HIPQUAD(ip), ip_set_list[binding]->name);
145 write_lock_bh(&ip_set_lock);
146 set_hash = LIST_FIND(&ip_set_hash[key], ip_hash_cmp,
147 struct ip_set_hash *, id, ip);
149 set_hash = kmalloc(sizeof(struct ip_set_hash), GFP_KERNEL);
154 INIT_LIST_HEAD(&set_hash->list);
157 list_add(&ip_set_hash[key], &set_hash->list);
159 IP_SET_ASSERT(ip_set_list[set_hash->binding]);
160 DP("overwrite binding: %s",
161 ip_set_list[set_hash->binding]->name);
162 __ip_set_put(set_hash->binding);
164 set_hash->binding = binding;
165 __ip_set_get(set_hash->binding);
167 write_unlock_bh(&ip_set_lock);
171 #define FOREACH_HASH_DO(fn, args...) \
174 struct ip_set_hash *__set_hash; \
176 for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
177 list_for_each_entry(__set_hash, &ip_set_hash[__key], list) \
178 fn(__set_hash , ## args); \
182 #define FOREACH_HASH_RW_DO(fn, args...) \
185 struct ip_set_hash *__set_hash, *__n; \
187 ASSERT_WRITE_LOCK(&ip_set_lock); \
188 for (__key = 0; __key < ip_set_bindings_hash_size; __key++) { \
189 list_for_each_entry_safe(__set_hash, __n, &ip_set_hash[__key], list)\
190 fn(__set_hash , ## args); \
194 /* Add, del and test set entries from kernel */
196 #define follow_bindings(index, set, ip) \
197 ((index = ip_set_find_in_hash((set)->id, ip)) != IP_SET_INVALID_ID \
198 || (index = (set)->binding) != IP_SET_INVALID_ID)
201 ip_set_testip_kernel(ip_set_id_t index,
202 const struct sk_buff *skb,
203 const u_int32_t *flags)
210 IP_SET_ASSERT(flags[i]);
211 read_lock_bh(&ip_set_lock);
213 set = ip_set_list[index];
215 DP("set %s, index %u", set->name, index);
216 read_lock_bh(&set->lock);
217 res = set->type->testip_kernel(set, skb, &ip, flags, i++);
218 read_unlock_bh(&set->lock);
219 i += !!(set->type->features & IPSET_DATA_DOUBLE);
222 && follow_bindings(index, set, ip));
223 read_unlock_bh(&ip_set_lock);
229 ip_set_addip_kernel(ip_set_id_t index,
230 const struct sk_buff *skb,
231 const u_int32_t *flags)
238 IP_SET_ASSERT(flags[i]);
240 read_lock_bh(&ip_set_lock);
242 set = ip_set_list[index];
244 DP("set %s, index %u", set->name, index);
245 write_lock_bh(&set->lock);
246 res = set->type->addip_kernel(set, skb, &ip, flags, i++);
247 write_unlock_bh(&set->lock);
248 i += !!(set->type->features & IPSET_DATA_DOUBLE);
249 } while ((res == 0 || res == -EEXIST)
251 && follow_bindings(index, set, ip));
252 read_unlock_bh(&ip_set_lock);
256 && (res = set->type->retry(set)) == 0)
261 ip_set_delip_kernel(ip_set_id_t index,
262 const struct sk_buff *skb,
263 const u_int32_t *flags)
270 IP_SET_ASSERT(flags[i]);
271 read_lock_bh(&ip_set_lock);
273 set = ip_set_list[index];
275 DP("set %s, index %u", set->name, index);
276 write_lock_bh(&set->lock);
277 res = set->type->delip_kernel(set, skb, &ip, flags, i++);
278 write_unlock_bh(&set->lock);
279 i += !!(set->type->features & IPSET_DATA_DOUBLE);
280 } while ((res == 0 || res == -EEXIST)
282 && follow_bindings(index, set, ip));
283 read_unlock_bh(&ip_set_lock);
286 /* Register and deregister settype */
289 set_type_equal(const struct ip_set_type *set_type, const char *str2)
291 return !strncmp(set_type->typename, str2, IP_SET_MAXNAMELEN - 1);
294 static inline struct ip_set_type *
295 find_set_type(const char *name)
297 return LIST_FIND(&set_type_list,
299 struct ip_set_type *,
304 ip_set_register_set_type(struct ip_set_type *set_type)
308 if (set_type->protocol_version != IP_SET_PROTOCOL_VERSION) {
309 ip_set_printk("'%s' uses wrong protocol version %u (want %u)",
311 set_type->protocol_version,
312 IP_SET_PROTOCOL_VERSION);
316 write_lock_bh(&ip_set_lock);
317 if (find_set_type(set_type->typename)) {
319 ip_set_printk("'%s' already registered!",
324 if (!try_module_get(THIS_MODULE)) {
328 list_append(&set_type_list, set_type);
329 DP("'%s' registered.", set_type->typename);
331 write_unlock_bh(&ip_set_lock);
336 ip_set_unregister_set_type(struct ip_set_type *set_type)
338 write_lock_bh(&ip_set_lock);
339 if (!find_set_type(set_type->typename)) {
340 ip_set_printk("'%s' not registered?",
344 LIST_DELETE(&set_type_list, set_type);
345 module_put(THIS_MODULE);
346 DP("'%s' unregistered.", set_type->typename);
348 write_unlock_bh(&ip_set_lock);
357 * Find set by name, reference it once. The reference makes sure the
358 * thing pointed to, does not go away under our feet. Drop the reference
359 * later, using ip_set_put().
362 ip_set_get_byname(const char *name)
364 ip_set_id_t i, index = IP_SET_INVALID_ID;
366 down(&ip_set_app_mutex);
367 for (i = 0; i < ip_set_max; i++) {
368 if (ip_set_list[i] != NULL
369 && strcmp(ip_set_list[i]->name, name) == 0) {
375 up(&ip_set_app_mutex);
380 * Find set by index, reference it once. The reference makes sure the
381 * thing pointed to, does not go away under our feet. Drop the reference
382 * later, using ip_set_put().
385 ip_set_get_byindex(ip_set_id_t index)
387 down(&ip_set_app_mutex);
389 if (index >= ip_set_max)
390 return IP_SET_INVALID_ID;
392 if (ip_set_list[index])
395 index = IP_SET_INVALID_ID;
397 up(&ip_set_app_mutex);
402 * If the given set pointer points to a valid set, decrement
403 * reference count by 1. The caller shall not assume the index
404 * to be valid, after calling this function.
406 void ip_set_put(ip_set_id_t index)
408 down(&ip_set_app_mutex);
409 if (ip_set_list[index])
411 up(&ip_set_app_mutex);
414 /* Find a set by name or index */
416 ip_set_find_byname(const char *name)
418 ip_set_id_t i, index = IP_SET_INVALID_ID;
420 for (i = 0; i < ip_set_max; i++) {
421 if (ip_set_list[i] != NULL
422 && strcmp(ip_set_list[i]->name, name) == 0) {
431 ip_set_find_byindex(ip_set_id_t index)
433 if (index >= ip_set_max || ip_set_list[index] == NULL)
434 index = IP_SET_INVALID_ID;
440 * Add, del, test, bind and unbind
444 __ip_set_testip(struct ip_set *set,
451 read_lock_bh(&set->lock);
452 res = set->type->testip(set, data, size, ip);
453 read_unlock_bh(&set->lock);
459 __ip_set_addip(ip_set_id_t index,
463 struct ip_set *set = ip_set_list[index];
469 write_lock_bh(&set->lock);
470 res = set->type->addip(set, data, size, &ip);
471 write_unlock_bh(&set->lock);
472 } while (res == -EAGAIN
474 && (res = set->type->retry(set)) == 0);
480 ip_set_addip(ip_set_id_t index,
485 return __ip_set_addip(index,
486 data + sizeof(struct ip_set_req_adt),
487 size - sizeof(struct ip_set_req_adt));
491 ip_set_delip(ip_set_id_t index,
495 struct ip_set *set = ip_set_list[index];
500 write_lock_bh(&set->lock);
501 res = set->type->delip(set,
502 data + sizeof(struct ip_set_req_adt),
503 size - sizeof(struct ip_set_req_adt),
505 write_unlock_bh(&set->lock);
511 ip_set_testip(ip_set_id_t index,
515 struct ip_set *set = ip_set_list[index];
520 res = __ip_set_testip(set,
521 data + sizeof(struct ip_set_req_adt),
522 size - sizeof(struct ip_set_req_adt),
525 return (res > 0 ? -EEXIST : res);
529 ip_set_bindip(ip_set_id_t index,
533 struct ip_set *set = ip_set_list[index];
534 struct ip_set_req_bind *req_bind;
540 if (size < sizeof(struct ip_set_req_bind))
543 req_bind = (struct ip_set_req_bind *) data;
544 req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
546 if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
547 /* Default binding of a set */
550 if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
553 binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
554 binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
556 binding = ip_set_find_byname(binding_name);
557 if (binding == IP_SET_INVALID_ID)
560 write_lock_bh(&ip_set_lock);
561 /* Sets as binding values are referenced */
562 if (set->binding != IP_SET_INVALID_ID)
563 __ip_set_put(set->binding);
564 set->binding = binding;
565 __ip_set_get(set->binding);
566 write_unlock_bh(&ip_set_lock);
570 binding = ip_set_find_byname(req_bind->binding);
571 if (binding == IP_SET_INVALID_ID)
574 res = __ip_set_testip(set,
575 data + sizeof(struct ip_set_req_bind),
576 size - sizeof(struct ip_set_req_bind),
578 DP("set %s, ip: %u.%u.%u.%u, binding %s",
579 set->name, HIPQUAD(ip), ip_set_list[binding]->name);
582 res = ip_set_hash_add(set->id, ip, binding);
587 #define FOREACH_SET_DO(fn, args...) \
590 struct ip_set *__set; \
592 for (__i = 0; __i < ip_set_max; __i++) { \
593 __set = ip_set_list[__i]; \
595 fn(__set , ##args); \
600 __set_hash_del_byid(struct ip_set_hash *set_hash, ip_set_id_t id)
602 if (set_hash->id == id)
603 __set_hash_del(set_hash);
607 __unbind_default(struct ip_set *set)
609 if (set->binding != IP_SET_INVALID_ID) {
610 /* Sets as binding values are referenced */
611 __ip_set_put(set->binding);
612 set->binding = IP_SET_INVALID_ID;
617 ip_set_unbindip(ip_set_id_t index,
622 struct ip_set_req_bind *req_bind;
627 if (size < sizeof(struct ip_set_req_bind))
630 req_bind = (struct ip_set_req_bind *) data;
631 req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
633 DP("%u %s", index, req_bind->binding);
634 if (index == IP_SET_INVALID_ID) {
636 if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
637 /* Default binding of sets */
638 write_lock_bh(&ip_set_lock);
639 FOREACH_SET_DO(__unbind_default);
640 write_unlock_bh(&ip_set_lock);
642 } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
643 /* Flush all bindings of all sets*/
644 write_lock_bh(&ip_set_lock);
645 FOREACH_HASH_RW_DO(__set_hash_del);
646 write_unlock_bh(&ip_set_lock);
649 DP("unreachable reached!");
653 set = ip_set_list[index];
655 if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
656 /* Default binding of set */
657 ip_set_id_t binding = ip_set_find_byindex(set->binding);
659 if (binding == IP_SET_INVALID_ID)
662 write_lock_bh(&ip_set_lock);
663 /* Sets in hash values are referenced */
664 __ip_set_put(set->binding);
665 set->binding = IP_SET_INVALID_ID;
666 write_unlock_bh(&ip_set_lock);
669 } else if (strcmp(req_bind->binding, IPSET_TOKEN_ALL) == 0) {
670 /* Flush all bindings */
672 write_lock_bh(&ip_set_lock);
673 FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
674 write_unlock_bh(&ip_set_lock);
678 res = __ip_set_testip(set,
679 data + sizeof(struct ip_set_req_bind),
680 size - sizeof(struct ip_set_req_bind),
683 DP("set %s, ip: %u.%u.%u.%u", set->name, HIPQUAD(ip));
685 res = ip_set_hash_del(set->id, ip);
691 ip_set_testbind(ip_set_id_t index,
695 struct ip_set *set = ip_set_list[index];
696 struct ip_set_req_bind *req_bind;
702 if (size < sizeof(struct ip_set_req_bind))
705 req_bind = (struct ip_set_req_bind *) data;
706 req_bind->binding[IP_SET_MAXNAMELEN - 1] = '\0';
708 if (strcmp(req_bind->binding, IPSET_TOKEN_DEFAULT) == 0) {
709 /* Default binding of set */
712 if (size != sizeof(struct ip_set_req_bind) + IP_SET_MAXNAMELEN)
715 binding_name = (char *)(data + sizeof(struct ip_set_req_bind));
716 binding_name[IP_SET_MAXNAMELEN - 1] = '\0';
718 binding = ip_set_find_byname(binding_name);
719 if (binding == IP_SET_INVALID_ID)
722 res = (set->binding == binding) ? -EEXIST : 0;
726 binding = ip_set_find_byname(req_bind->binding);
727 if (binding == IP_SET_INVALID_ID)
731 res = __ip_set_testip(set,
732 data + sizeof(struct ip_set_req_bind),
733 size - sizeof(struct ip_set_req_bind),
735 DP("set %s, ip: %u.%u.%u.%u, binding %s",
736 set->name, HIPQUAD(ip), ip_set_list[binding]->name);
739 res = (ip_set_find_in_hash(set->id, ip) == binding)
745 static struct ip_set_type *
746 find_set_type_rlock(const char *typename)
748 struct ip_set_type *type;
750 read_lock_bh(&ip_set_lock);
751 type = find_set_type(typename);
753 read_unlock_bh(&ip_set_lock);
759 find_free_id(const char *name,
765 *id = IP_SET_INVALID_ID;
766 for (i = 0; i < ip_set_max; i++) {
767 if (ip_set_list[i] == NULL) {
768 if (*id == IP_SET_INVALID_ID)
770 } else if (strcmp(name, ip_set_list[i]->name) == 0)
774 if (*id == IP_SET_INVALID_ID)
775 /* No free slot remained */
777 /* Check that index is usable as id (swapping) */
779 for (i = 0; i < ip_set_max; i++) {
780 if (ip_set_list[i] != NULL
781 && ip_set_list[i]->id == *id) {
793 ip_set_create(const char *name,
794 const char *typename,
800 ip_set_id_t index, id;
803 DP("setname: %s, typename: %s, id: %u", name, typename, restore);
805 * First, and without any locks, allocate and initialize
806 * a normal base set structure.
808 set = kmalloc(sizeof(struct ip_set), GFP_KERNEL);
811 set->lock = RW_LOCK_UNLOCKED;
812 strncpy(set->name, name, IP_SET_MAXNAMELEN);
813 set->binding = IP_SET_INVALID_ID;
814 atomic_set(&set->ref, 0);
817 * Next, take the &ip_set_lock, check that we know the type,
818 * and take a reference on the type, to make sure it
819 * stays available while constructing our new set.
821 * After referencing the type, we drop the &ip_set_lock,
822 * and let the new set construction run without locks.
824 set->type = find_set_type_rlock(typename);
825 if (set->type == NULL) {
826 /* Try loading the module */
827 char modulename[IP_SET_MAXNAMELEN + strlen("ip_set_") + 1];
828 strcpy(modulename, "ip_set_");
829 strcat(modulename, typename);
830 DP("try to load %s", modulename);
831 request_module(modulename);
832 set->type = find_set_type_rlock(typename);
834 if (set->type == NULL) {
835 ip_set_printk("no set type '%s', set '%s' not created",
840 if (!try_module_get(set->type->me)) {
841 read_unlock_bh(&ip_set_lock);
845 read_unlock_bh(&ip_set_lock);
848 * Without holding any locks, create private part.
850 res = set->type->create(set, data, size);
854 /* BTW, res==0 here. */
857 * Here, we have a valid, constructed set. &ip_set_lock again,
858 * find free id/index and check that it is not already in
861 write_lock_bh(&ip_set_lock);
862 if ((res = find_free_id(set->name, &index, &id)) != 0) {
867 /* Make sure restore gets the same index */
868 if (restore != IP_SET_INVALID_ID && index != restore) {
869 DP("Can't restore, sets are screwed up");
875 * Finally! Add our shiny new set to the list, and be done.
877 DP("create: '%s' created with index %u, id %u!", set->name, index, id);
879 ip_set_list[index] = set;
880 write_unlock_bh(&ip_set_lock);
884 write_unlock_bh(&ip_set_lock);
885 set->type->destroy(set);
887 module_put(set->type->me);
894 * Destroy a given existing set
897 ip_set_destroy_set(ip_set_id_t index)
899 struct ip_set *set = ip_set_list[index];
902 DP("set: %s", set->name);
903 write_lock_bh(&ip_set_lock);
904 FOREACH_HASH_RW_DO(__set_hash_del_byid, set->id);
905 if (set->binding != IP_SET_INVALID_ID)
906 __ip_set_put(set->binding);
907 ip_set_list[index] = NULL;
908 write_unlock_bh(&ip_set_lock);
910 /* Must call it without holding any lock */
911 set->type->destroy(set);
912 module_put(set->type->me);
917 * Destroy a set - or all sets
918 * Sets must not be referenced/used.
921 ip_set_destroy(ip_set_id_t index)
925 /* ref modification always protected by the mutex */
926 if (index != IP_SET_INVALID_ID) {
927 if (atomic_read(&ip_set_list[index]->ref))
929 ip_set_destroy_set(index);
931 for (i = 0; i < ip_set_max; i++) {
932 if (ip_set_list[i] != NULL
933 && (atomic_read(&ip_set_list[i]->ref)))
937 for (i = 0; i < ip_set_max; i++) {
938 if (ip_set_list[i] != NULL)
939 ip_set_destroy_set(i);
946 ip_set_flush_set(struct ip_set *set)
948 DP("set: %s %u", set->name, set->id);
950 write_lock_bh(&set->lock);
951 set->type->flush(set);
952 write_unlock_bh(&set->lock);
956 * Flush data in a set - or in all sets
959 ip_set_flush(ip_set_id_t index)
961 if (index != IP_SET_INVALID_ID) {
962 IP_SET_ASSERT(ip_set_list[index]);
963 ip_set_flush_set(ip_set_list[index]);
965 FOREACH_SET_DO(ip_set_flush_set);
972 ip_set_rename(ip_set_id_t index, const char *name)
974 struct ip_set *set = ip_set_list[index];
978 DP("set: %s to %s", set->name, name);
979 write_lock_bh(&ip_set_lock);
980 for (i = 0; i < ip_set_max; i++) {
981 if (ip_set_list[i] != NULL
982 && strncmp(ip_set_list[i]->name,
984 IP_SET_MAXNAMELEN - 1) == 0) {
989 strncpy(set->name, name, IP_SET_MAXNAMELEN);
991 write_unlock_bh(&ip_set_lock);
996 * Swap two sets so that name/index points to the other.
997 * References are also swapped.
1000 ip_set_swap(ip_set_id_t from_index, ip_set_id_t to_index)
1002 struct ip_set *from = ip_set_list[from_index];
1003 struct ip_set *to = ip_set_list[to_index];
1004 char from_name[IP_SET_MAXNAMELEN];
1007 DP("set: %s to %s", from->name, to->name);
1008 /* Features must not change. Artifical restriction. */
1009 if (from->type->features != to->type->features)
1012 /* No magic here: ref munging protected by the mutex */
1013 write_lock_bh(&ip_set_lock);
1014 strncpy(from_name, from->name, IP_SET_MAXNAMELEN);
1015 from_ref = atomic_read(&from->ref);
1017 strncpy(from->name, to->name, IP_SET_MAXNAMELEN);
1018 atomic_set(&from->ref, atomic_read(&to->ref));
1019 strncpy(to->name, from_name, IP_SET_MAXNAMELEN);
1020 atomic_set(&to->ref, from_ref);
1022 ip_set_list[from_index] = to;
1023 ip_set_list[to_index] = from;
1025 write_unlock_bh(&ip_set_lock);
1034 __set_hash_bindings_size_list(struct ip_set_hash *set_hash,
1035 ip_set_id_t id, size_t *size)
1037 if (set_hash->id == id)
1038 *size += sizeof(struct ip_set_hash_list);
1042 __set_hash_bindings_size_save(struct ip_set_hash *set_hash,
1043 ip_set_id_t id, size_t *size)
1045 if (set_hash->id == id)
1046 *size += sizeof(struct ip_set_hash_save);
1050 __set_hash_bindings(struct ip_set_hash *set_hash,
1051 ip_set_id_t id, void *data, int *used)
1053 if (set_hash->id == id) {
1054 struct ip_set_hash_list *hash_list =
1055 (struct ip_set_hash_list *)(data + *used);
1057 hash_list->ip = set_hash->ip;
1058 hash_list->binding = set_hash->binding;
1059 *used += sizeof(struct ip_set_hash_list);
1063 static int ip_set_list_set(ip_set_id_t index,
1068 struct ip_set *set = ip_set_list[index];
1069 struct ip_set_list *set_list;
1071 /* Pointer to our header */
1072 set_list = (struct ip_set_list *) (data + *used);
1074 DP("set: %s, used: %d %p %p", set->name, *used, data, data + *used);
1076 /* Get and ensure header size */
1077 if (*used + sizeof(struct ip_set_list) > len)
1078 goto not_enough_mem;
1079 *used += sizeof(struct ip_set_list);
1081 read_lock_bh(&set->lock);
1082 /* Get and ensure set specific header size */
1083 set_list->header_size = set->type->header_size;
1084 if (*used + set_list->header_size > len)
1087 /* Fill in the header */
1088 set_list->index = index;
1089 set_list->binding = set->binding;
1090 set_list->ref = atomic_read(&set->ref);
1092 /* Fill in set spefific header data */
1093 set->type->list_header(set, data + *used);
1094 *used += set_list->header_size;
1096 /* Get and ensure set specific members size */
1097 set_list->members_size = set->type->list_members_size(set);
1098 if (*used + set_list->members_size > len)
1101 /* Fill in set spefific members data */
1102 set->type->list_members(set, data + *used);
1103 *used += set_list->members_size;
1104 read_unlock_bh(&set->lock);
1108 /* Get and ensure set specific bindings size */
1109 set_list->bindings_size = 0;
1110 FOREACH_HASH_DO(__set_hash_bindings_size_list,
1111 set->id, &set_list->bindings_size);
1112 if (*used + set_list->bindings_size > len)
1113 goto not_enough_mem;
1115 /* Fill in set spefific bindings data */
1116 FOREACH_HASH_DO(__set_hash_bindings, set->id, data, used);
1121 read_unlock_bh(&set->lock);
1123 DP("not enough mem, try again");
1130 static int ip_set_save_set(ip_set_id_t index,
1136 struct ip_set_save *set_save;
1138 /* Pointer to our header */
1139 set_save = (struct ip_set_save *) (data + *used);
1141 /* Get and ensure header size */
1142 if (*used + sizeof(struct ip_set_save) > len)
1143 goto not_enough_mem;
1144 *used += sizeof(struct ip_set_save);
1146 set = ip_set_list[index];
1147 DP("set: %s, used: %u(%u) %p %p", set->name, *used, len,
1148 data, data + *used);
1150 read_lock_bh(&set->lock);
1151 /* Get and ensure set specific header size */
1152 set_save->header_size = set->type->header_size;
1153 if (*used + set_save->header_size > len)
1156 /* Fill in the header */
1157 set_save->index = index;
1158 set_save->binding = set->binding;
1160 /* Fill in set spefific header data */
1161 set->type->list_header(set, data + *used);
1162 *used += set_save->header_size;
1164 DP("set header filled: %s, used: %u %p %p", set->name, *used,
1165 data, data + *used);
1166 /* Get and ensure set specific members size */
1167 set_save->members_size = set->type->list_members_size(set);
1168 if (*used + set_save->members_size > len)
1171 /* Fill in set spefific members data */
1172 set->type->list_members(set, data + *used);
1173 *used += set_save->members_size;
1174 read_unlock_bh(&set->lock);
1175 DP("set members filled: %s, used: %u %p %p", set->name, *used,
1176 data, data + *used);
1180 read_unlock_bh(&set->lock);
1182 DP("not enough mem, try again");
1187 __set_hash_save_bindings(struct ip_set_hash *set_hash,
1195 && (id == IP_SET_INVALID_ID || set_hash->id == id)) {
1196 struct ip_set_hash_save *hash_save =
1197 (struct ip_set_hash_save *)(data + *used);
1198 /* Ensure bindings size */
1199 if (*used + sizeof(struct ip_set_hash_save) > len) {
1203 hash_save->id = set_hash->id;
1204 hash_save->ip = set_hash->ip;
1205 hash_save->binding = set_hash->binding;
1206 *used += sizeof(struct ip_set_hash_save);
1210 static int ip_set_save_bindings(ip_set_id_t index,
1216 struct ip_set_save *set_save;
1218 DP("used %u, len %u", *used, len);
1219 /* Get and ensure header size */
1220 if (*used + sizeof(struct ip_set_save) > len)
1224 set_save = (struct ip_set_save *) (data + *used);
1225 set_save->index = IP_SET_INVALID_ID;
1226 *used += sizeof(struct ip_set_save);
1228 DP("marker added used %u, len %u", *used, len);
1229 /* Fill in bindings data */
1230 if (index != IP_SET_INVALID_ID)
1231 /* Sets are identified by id in hash */
1232 index = ip_set_list[index]->id;
1233 FOREACH_HASH_DO(__set_hash_save_bindings, index, data, used, len, &res);
1241 static int ip_set_restore(void *data,
1245 int line = 0, used = 0, members_size;
1247 struct ip_set_hash_save *hash_save;
1248 struct ip_set_restore *set_restore;
1251 /* Loop to restore sets */
1255 DP("%u %u %u", used, sizeof(struct ip_set_restore), len);
1256 /* Get and ensure header size */
1257 if (used + sizeof(struct ip_set_restore) > len)
1259 set_restore = (struct ip_set_restore *) (data + used);
1260 used += sizeof(struct ip_set_restore);
1262 /* Ensure data size */
1264 + set_restore->header_size
1265 + set_restore->members_size > len)
1269 if (set_restore->index == IP_SET_INVALID_ID) {
1274 /* Try to create the set */
1275 DP("restore %s %s", set_restore->name, set_restore->typename);
1276 res = ip_set_create(set_restore->name,
1277 set_restore->typename,
1280 set_restore->header_size);
1284 used += set_restore->header_size;
1286 index = ip_set_find_byindex(set_restore->index);
1287 DP("index %u, restore_index %u", index, set_restore->index);
1288 if (index != set_restore->index)
1290 /* Try to restore members data */
1291 set = ip_set_list[index];
1293 DP("members_size %u reqsize %u",
1294 set_restore->members_size, set->type->reqsize);
1295 while (members_size + set->type->reqsize <=
1296 set_restore->members_size) {
1298 DP("members: %u, line %u", members_size, line);
1299 res = __ip_set_addip(index,
1300 data + used + members_size,
1301 set->type->reqsize);
1302 if (!(res == 0 || res == -EEXIST))
1304 members_size += set->type->reqsize;
1307 DP("members_size %u %u",
1308 set_restore->members_size, members_size);
1309 if (members_size != set_restore->members_size)
1311 used += set_restore->members_size;
1315 /* Loop to restore bindings */
1316 while (used < len) {
1319 DP("restore binding, line %u", line);
1320 /* Get and ensure size */
1321 if (used + sizeof(struct ip_set_hash_save) > len)
1323 hash_save = (struct ip_set_hash_save *) (data + used);
1324 used += sizeof(struct ip_set_hash_save);
1326 /* hash_save->id is used to store the index */
1327 index = ip_set_find_byindex(hash_save->id);
1328 DP("restore binding index %u, id %u, %u -> %u",
1329 index, hash_save->id, hash_save->ip, hash_save->binding);
1330 if (index != hash_save->id)
1333 set = ip_set_list[hash_save->id];
1334 /* Null valued IP means default binding */
1336 res = ip_set_hash_add(set->id,
1338 hash_save->binding);
1340 IP_SET_ASSERT(set->binding == IP_SET_INVALID_ID);
1341 write_lock_bh(&ip_set_lock);
1342 set->binding = hash_save->binding;
1343 __ip_set_get(set->binding);
1344 write_unlock_bh(&ip_set_lock);
1345 DP("default binding: %u", set->binding);
1357 ip_set_sockfn_set(struct sock *sk, int optval, void *user, unsigned int len)
1360 int res = 0; /* Assume OK */
1362 struct ip_set_req_adt *req_adt;
1363 ip_set_id_t index = IP_SET_INVALID_ID;
1364 int (*adtfn)(ip_set_id_t index,
1365 const void *data, size_t size);
1367 int (*fn)(ip_set_id_t index,
1368 const void *data, size_t size);
1370 { { ip_set_addip }, { ip_set_delip }, { ip_set_testip},
1371 { ip_set_bindip}, { ip_set_unbindip }, { ip_set_testbind },
1374 DP("optval=%d, user=%p, len=%d", optval, user, len);
1375 if (!capable(CAP_NET_ADMIN))
1377 if (optval != SO_IP_SET)
1379 if (len <= sizeof(unsigned)) {
1380 ip_set_printk("short userdata (want >%zu, got %u)",
1381 sizeof(unsigned), len);
1384 data = vmalloc(len);
1386 DP("out of mem for %u bytes", len);
1389 if (copy_from_user(data, user, len) != 0) {
1393 if (down_interruptible(&ip_set_app_mutex)) {
1398 op = (unsigned *)data;
1401 if (*op < IP_SET_OP_VERSION) {
1402 /* Check the version at the beginning of operations */
1403 struct ip_set_req_version *req_version =
1404 (struct ip_set_req_version *) data;
1405 if (req_version->version != IP_SET_PROTOCOL_VERSION) {
1412 case IP_SET_OP_CREATE:{
1413 struct ip_set_req_create *req_create
1414 = (struct ip_set_req_create *) data;
1416 if (len <= sizeof(struct ip_set_req_create)) {
1417 ip_set_printk("short CREATE data (want >%zu, got %u)",
1418 sizeof(struct ip_set_req_create), len);
1422 req_create->name[IP_SET_MAXNAMELEN - 1] = '\0';
1423 req_create->typename[IP_SET_MAXNAMELEN - 1] = '\0';
1424 res = ip_set_create(req_create->name,
1425 req_create->typename,
1427 data + sizeof(struct ip_set_req_create),
1428 len - sizeof(struct ip_set_req_create));
1431 case IP_SET_OP_DESTROY:{
1432 struct ip_set_req_std *req_destroy
1433 = (struct ip_set_req_std *) data;
1435 if (len != sizeof(struct ip_set_req_std)) {
1436 ip_set_printk("invalid DESTROY data (want %zu, got %u)",
1437 sizeof(struct ip_set_req_std), len);
1441 if (strcmp(req_destroy->name, IPSET_TOKEN_ALL) == 0) {
1442 /* Destroy all sets */
1443 index = IP_SET_INVALID_ID;
1445 req_destroy->name[IP_SET_MAXNAMELEN - 1] = '\0';
1446 index = ip_set_find_byname(req_destroy->name);
1448 if (index == IP_SET_INVALID_ID) {
1454 res = ip_set_destroy(index);
1457 case IP_SET_OP_FLUSH:{
1458 struct ip_set_req_std *req_flush =
1459 (struct ip_set_req_std *) data;
1461 if (len != sizeof(struct ip_set_req_std)) {
1462 ip_set_printk("invalid FLUSH data (want %zu, got %u)",
1463 sizeof(struct ip_set_req_std), len);
1467 if (strcmp(req_flush->name, IPSET_TOKEN_ALL) == 0) {
1468 /* Flush all sets */
1469 index = IP_SET_INVALID_ID;
1471 req_flush->name[IP_SET_MAXNAMELEN - 1] = '\0';
1472 index = ip_set_find_byname(req_flush->name);
1474 if (index == IP_SET_INVALID_ID) {
1479 res = ip_set_flush(index);
1482 case IP_SET_OP_RENAME:{
1483 struct ip_set_req_create *req_rename
1484 = (struct ip_set_req_create *) data;
1486 if (len != sizeof(struct ip_set_req_create)) {
1487 ip_set_printk("invalid RENAME data (want %zu, got %u)",
1488 sizeof(struct ip_set_req_create), len);
1493 req_rename->name[IP_SET_MAXNAMELEN - 1] = '\0';
1494 req_rename->typename[IP_SET_MAXNAMELEN - 1] = '\0';
1496 index = ip_set_find_byname(req_rename->name);
1497 if (index == IP_SET_INVALID_ID) {
1501 res = ip_set_rename(index, req_rename->typename);
1504 case IP_SET_OP_SWAP:{
1505 struct ip_set_req_create *req_swap
1506 = (struct ip_set_req_create *) data;
1507 ip_set_id_t to_index;
1509 if (len != sizeof(struct ip_set_req_create)) {
1510 ip_set_printk("invalid SWAP data (want %zu, got %u)",
1511 sizeof(struct ip_set_req_create), len);
1516 req_swap->name[IP_SET_MAXNAMELEN - 1] = '\0';
1517 req_swap->typename[IP_SET_MAXNAMELEN - 1] = '\0';
1519 index = ip_set_find_byname(req_swap->name);
1520 if (index == IP_SET_INVALID_ID) {
1524 to_index = ip_set_find_byname(req_swap->typename);
1525 if (to_index == IP_SET_INVALID_ID) {
1529 res = ip_set_swap(index, to_index);
1533 break; /* Set identified by id */
1536 /* There we may have add/del/test/bind/unbind/test_bind operations */
1537 if (*op < IP_SET_OP_ADD_IP || *op > IP_SET_OP_TEST_BIND_SET) {
1541 adtfn = adtfn_table[*op - IP_SET_OP_ADD_IP].fn;
1543 if (len < sizeof(struct ip_set_req_adt)) {
1544 ip_set_printk("short data in adt request (want >=%zu, got %u)",
1545 sizeof(struct ip_set_req_adt), len);
1549 req_adt = (struct ip_set_req_adt *) data;
1551 /* -U :all: :all:|:default: uses IP_SET_INVALID_ID */
1552 if (!(*op == IP_SET_OP_UNBIND_SET
1553 && req_adt->index == IP_SET_INVALID_ID)) {
1554 index = ip_set_find_byindex(req_adt->index);
1555 if (index == IP_SET_INVALID_ID) {
1560 res = adtfn(index, data, len);
1563 up(&ip_set_app_mutex);
1567 DP("final result %d", res);
1572 ip_set_sockfn_get(struct sock *sk, int optval, void *user, int *len)
1576 ip_set_id_t index = IP_SET_INVALID_ID;
1580 DP("optval=%d, user=%p, len=%d", optval, user, *len);
1581 if (!capable(CAP_NET_ADMIN))
1583 if (optval != SO_IP_SET)
1585 if (*len < sizeof(unsigned)) {
1586 ip_set_printk("short userdata (want >=%zu, got %d)",
1587 sizeof(unsigned), *len);
1590 data = vmalloc(*len);
1592 DP("out of mem for %d bytes", *len);
1595 if (copy_from_user(data, user, *len) != 0) {
1599 if (down_interruptible(&ip_set_app_mutex)) {
1604 op = (unsigned *) data;
1607 if (*op < IP_SET_OP_VERSION) {
1608 /* Check the version at the beginning of operations */
1609 struct ip_set_req_version *req_version =
1610 (struct ip_set_req_version *) data;
1611 if (req_version->version != IP_SET_PROTOCOL_VERSION) {
1618 case IP_SET_OP_VERSION: {
1619 struct ip_set_req_version *req_version =
1620 (struct ip_set_req_version *) data;
1622 if (*len != sizeof(struct ip_set_req_version)) {
1623 ip_set_printk("invalid VERSION (want %zu, got %d)",
1624 sizeof(struct ip_set_req_version),
1630 req_version->version = IP_SET_PROTOCOL_VERSION;
1631 res = copy_to_user(user, req_version,
1632 sizeof(struct ip_set_req_version));
1635 case IP_SET_OP_GET_BYNAME: {
1636 struct ip_set_req_get_set *req_get
1637 = (struct ip_set_req_get_set *) data;
1639 if (*len != sizeof(struct ip_set_req_get_set)) {
1640 ip_set_printk("invalid GET_BYNAME (want %zu, got %d)",
1641 sizeof(struct ip_set_req_get_set), *len);
1645 req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
1646 index = ip_set_find_byname(req_get->set.name);
1647 req_get->set.index = index;
1650 case IP_SET_OP_GET_BYINDEX: {
1651 struct ip_set_req_get_set *req_get
1652 = (struct ip_set_req_get_set *) data;
1654 if (*len != sizeof(struct ip_set_req_get_set)) {
1655 ip_set_printk("invalid GET_BYINDEX (want %zu, got %d)",
1656 sizeof(struct ip_set_req_get_set), *len);
1660 req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
1661 index = ip_set_find_byindex(req_get->set.index);
1662 strncpy(req_get->set.name,
1663 index == IP_SET_INVALID_ID ? ""
1664 : ip_set_list[index]->name, IP_SET_MAXNAMELEN);
1667 case IP_SET_OP_ADT_GET: {
1668 struct ip_set_req_adt_get *req_get
1669 = (struct ip_set_req_adt_get *) data;
1671 if (*len != sizeof(struct ip_set_req_adt_get)) {
1672 ip_set_printk("invalid ADT_GET (want %zu, got %d)",
1673 sizeof(struct ip_set_req_adt_get), *len);
1677 req_get->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
1678 index = ip_set_find_byname(req_get->set.name);
1679 if (index != IP_SET_INVALID_ID) {
1680 req_get->set.index = index;
1681 strncpy(req_get->typename,
1682 ip_set_list[index]->type->typename,
1683 IP_SET_MAXNAMELEN - 1);
1690 case IP_SET_OP_MAX_SETS: {
1691 struct ip_set_req_max_sets *req_max_sets
1692 = (struct ip_set_req_max_sets *) data;
1695 if (*len != sizeof(struct ip_set_req_max_sets)) {
1696 ip_set_printk("invalid MAX_SETS (want %zu, got %d)",
1697 sizeof(struct ip_set_req_max_sets), *len);
1702 if (strcmp(req_max_sets->set.name, IPSET_TOKEN_ALL) == 0) {
1703 req_max_sets->set.index = IP_SET_INVALID_ID;
1705 req_max_sets->set.name[IP_SET_MAXNAMELEN - 1] = '\0';
1706 req_max_sets->set.index =
1707 ip_set_find_byname(req_max_sets->set.name);
1708 if (req_max_sets->set.index == IP_SET_INVALID_ID) {
1713 req_max_sets->max_sets = ip_set_max;
1714 req_max_sets->sets = 0;
1715 for (i = 0; i < ip_set_max; i++) {
1716 if (ip_set_list[i] != NULL)
1717 req_max_sets->sets++;
1721 case IP_SET_OP_LIST_SIZE:
1722 case IP_SET_OP_SAVE_SIZE: {
1723 struct ip_set_req_setnames *req_setnames
1724 = (struct ip_set_req_setnames *) data;
1725 struct ip_set_name_list *name_list;
1730 if (*len < sizeof(struct ip_set_req_setnames)) {
1731 ip_set_printk("short LIST_SIZE (want >=%zu, got %d)",
1732 sizeof(struct ip_set_req_setnames), *len);
1737 req_setnames->size = 0;
1738 used = sizeof(struct ip_set_req_setnames);
1739 for (i = 0; i < ip_set_max; i++) {
1740 if (ip_set_list[i] == NULL)
1742 name_list = (struct ip_set_name_list *)
1744 used += sizeof(struct ip_set_name_list);
1745 if (used > copylen) {
1749 set = ip_set_list[i];
1750 /* Fill in index, name, etc. */
1751 name_list->index = i;
1752 name_list->id = set->id;
1753 strncpy(name_list->name,
1755 IP_SET_MAXNAMELEN - 1);
1756 strncpy(name_list->typename,
1757 set->type->typename,
1758 IP_SET_MAXNAMELEN - 1);
1759 DP("filled %s of type %s, index %u\n",
1760 name_list->name, name_list->typename,
1762 if (!(req_setnames->index == IP_SET_INVALID_ID
1763 || req_setnames->index == i))
1767 case IP_SET_OP_LIST_SIZE: {
1768 req_setnames->size += sizeof(struct ip_set_list)
1769 + set->type->header_size
1770 + set->type->list_members_size(set);
1771 FOREACH_HASH_DO(__set_hash_bindings_size_list,
1772 i, &req_setnames->size);
1775 case IP_SET_OP_SAVE_SIZE: {
1776 req_setnames->size += sizeof(struct ip_set_save)
1777 + set->type->header_size
1778 + set->type->list_members_size(set);
1779 FOREACH_HASH_DO(__set_hash_bindings_size_save,
1780 i, &req_setnames->size);
1787 if (copylen != used) {
1793 case IP_SET_OP_LIST: {
1794 struct ip_set_req_list *req_list
1795 = (struct ip_set_req_list *) data;
1799 if (*len < sizeof(struct ip_set_req_list)) {
1800 ip_set_printk("short LIST (want >=%zu, got %d)",
1801 sizeof(struct ip_set_req_list), *len);
1805 index = req_list->index;
1806 if (index != IP_SET_INVALID_ID
1807 && ip_set_find_byindex(index) != index) {
1812 if (index == IP_SET_INVALID_ID) {
1814 for (i = 0; i < ip_set_max && res == 0; i++) {
1815 if (ip_set_list[i] != NULL)
1816 res = ip_set_list_set(i, data, &used, *len);
1819 /* List an individual set */
1820 res = ip_set_list_set(index, data, &used, *len);
1824 else if (copylen != used) {
1830 case IP_SET_OP_SAVE: {
1831 struct ip_set_req_list *req_save
1832 = (struct ip_set_req_list *) data;
1836 if (*len < sizeof(struct ip_set_req_list)) {
1837 ip_set_printk("short SAVE (want >=%zu, got %d)",
1838 sizeof(struct ip_set_req_list), *len);
1842 index = req_save->index;
1843 if (index != IP_SET_INVALID_ID
1844 && ip_set_find_byindex(index) != index) {
1849 if (index == IP_SET_INVALID_ID) {
1851 for (i = 0; i < ip_set_max && res == 0; i++) {
1852 if (ip_set_list[i] != NULL)
1853 res = ip_set_save_set(i, data, &used, *len);
1856 /* Save an individual set */
1857 res = ip_set_save_set(index, data, &used, *len);
1860 res = ip_set_save_bindings(index, data, &used, *len);
1864 else if (copylen != used) {
1870 case IP_SET_OP_RESTORE: {
1871 struct ip_set_req_setnames *req_restore
1872 = (struct ip_set_req_setnames *) data;
1875 if (*len < sizeof(struct ip_set_req_setnames)
1876 || *len != req_restore->size) {
1877 ip_set_printk("invalid RESTORE (want =%zu, got %d)",
1878 req_restore->size, *len);
1882 line = ip_set_restore(data + sizeof(struct ip_set_req_setnames),
1883 req_restore->size - sizeof(struct ip_set_req_setnames));
1884 DP("ip_set_restore: %u", line);
1887 req_restore->size = line;
1888 copylen = sizeof(struct ip_set_req_setnames);
1896 } /* end of switch(op) */
1899 DP("set %s, copylen %u", index != IP_SET_INVALID_ID
1900 && ip_set_list[index]
1901 ? ip_set_list[index]->name
1902 : ":all:", copylen);
1903 res = copy_to_user(user, data, copylen);
1906 up(&ip_set_app_mutex);
1910 DP("final result %d", res);
1914 static struct nf_sockopt_ops so_set = {
1916 .set_optmin = SO_IP_SET,
1917 .set_optmax = SO_IP_SET + 1,
1918 .set = &ip_set_sockfn_set,
1919 .get_optmin = SO_IP_SET,
1920 .get_optmax = SO_IP_SET + 1,
1921 .get = &ip_set_sockfn_get,
1925 static int max_sets, hash_size;
1926 module_param(max_sets, int, 0600);
1927 MODULE_PARM_DESC(max_sets, "maximal number of sets");
1928 module_param(hash_size, int, 0600);
1929 MODULE_PARM_DESC(hash_size, "hash size for bindings");
1930 MODULE_LICENSE("GPL");
1931 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
1932 MODULE_DESCRIPTION("module implementing core IP set support");
1934 static int __init init(void)
1939 get_random_bytes(&ip_set_hash_random, 4);
1941 ip_set_max = max_sets;
1942 ip_set_list = vmalloc(sizeof(struct ip_set *) * ip_set_max);
1944 printk(KERN_ERR "Unable to create ip_set_list\n");
1947 memset(ip_set_list, 0, sizeof(struct ip_set *) * ip_set_max);
1949 ip_set_bindings_hash_size = hash_size;
1950 ip_set_hash = vmalloc(sizeof(struct list_head) * ip_set_bindings_hash_size);
1952 printk(KERN_ERR "Unable to create ip_set_hash\n");
1956 for (i = 0; i < ip_set_bindings_hash_size; i++)
1957 INIT_LIST_HEAD(&ip_set_hash[i]);
1959 INIT_LIST_HEAD(&set_type_list);
1961 res = nf_register_sockopt(&so_set);
1963 ip_set_printk("SO_SET registry failed: %d", res);
1971 static void __exit fini(void)
1973 /* There can't be any existing set or binding */
1974 nf_unregister_sockopt(&so_set);
1977 DP("these are the famous last words");
1980 EXPORT_SYMBOL(ip_set_register_set_type);
1981 EXPORT_SYMBOL(ip_set_unregister_set_type);
1983 EXPORT_SYMBOL(ip_set_get_byname);
1984 EXPORT_SYMBOL(ip_set_get_byindex);
1985 EXPORT_SYMBOL(ip_set_put);
1987 EXPORT_SYMBOL(ip_set_addip_kernel);
1988 EXPORT_SYMBOL(ip_set_delip_kernel);
1989 EXPORT_SYMBOL(ip_set_testip_kernel);