1 /* Copyright (C) 2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
8 #ifndef _IP_SET_HASH_GEN_H
9 #define _IP_SET_HASH_GEN_H
11 #include <linux/rcupdate.h>
12 #include <linux/jhash.h>
13 #include <linux/netfilter/ipset/ip_set_timeout.h>
14 #ifndef rcu_dereference_bh
15 #define rcu_dereference_bh(p) rcu_dereference(p)
18 #define rcu_dereference_bh_nfnl(p) rcu_dereference_bh_check(p, 1)
20 /* Hashing which uses arrays to resolve clashing. The hash table is resized
21 * (doubled) when searching becomes too long.
22 * Internally jhash is used with the assumption that the size of the
23 * stored data is a multiple of sizeof(u32). If storage supports timeout,
24 * the timeout field must be the last one in the data structure - that field
25 * is ignored when computing the hash key.
27 * Readers and resizing
29 * Resizing can be triggered by userspace command only, and those
30 * are serialized by the nfnl mutex. During resizing the set is
31 * read-locked, so the only possible concurrent operations are
32 * the kernel side readers. Those must be protected by proper RCU locking.
35 /* Number of elements to store in an initial array block */
36 #define AHASH_INIT_SIZE 4
37 /* Max number of elements to store in an array block */
38 #define AHASH_MAX_SIZE (3*AHASH_INIT_SIZE)
40 /* Max number of elements can be tuned */
41 #ifdef IP_SET_HASH_WITH_MULTI
42 #define AHASH_MAX(h) ((h)->ahash_max)
45 tune_ahash_max(u8 curr, u32 multi)
52 n = curr + AHASH_INIT_SIZE;
53 /* Currently, at listing one hash bucket must fit into a message.
54 * Therefore we have a hard limit here.
56 return n > curr && n <= 64 ? n : curr;
58 #define TUNE_AHASH_MAX(h, multi) \
59 ((h)->ahash_max = tune_ahash_max((h)->ahash_max, multi))
61 #define AHASH_MAX(h) AHASH_MAX_SIZE
62 #define TUNE_AHASH_MAX(h, multi)
67 void *value; /* the array of the values */
68 u8 size; /* size of the array */
69 u8 pos; /* position of the first free entry */
72 /* The hash table: the table size stored here in order to make resizing easy */
74 atomic_t ref; /* References for resizing */
75 atomic_t uref; /* References for dumping */
76 u8 htable_bits; /* size of hash table == 2^htable_bits */
77 struct hbucket bucket[0]; /* hashtable buckets */
80 #define hbucket(h, i) (&((h)->bucket[i]))
82 #ifndef IPSET_NET_COUNT
83 #define IPSET_NET_COUNT 1
86 /* Book-keeping of the prefixes added to the set */
88 u32 nets[IPSET_NET_COUNT]; /* number of elements per cidr */
89 u8 cidr[IPSET_NET_COUNT]; /* the different cidr values in the set */
92 /* Compute the hash table size */
98 /* We must fit both into u32 in jhash and size_t */
101 hsize = jhash_size(hbits);
102 if ((((size_t)-1) - sizeof(struct htable))/sizeof(struct hbucket)
106 return hsize * sizeof(struct hbucket) + sizeof(struct htable);
109 /* Compute htable_bits from the user input parameter hashsize */
111 htable_bits(u32 hashsize)
113 /* Assume that hashsize == 2^htable_bits */
114 u8 bits = fls(hashsize - 1);
115 if (jhash_size(bits) != hashsize)
116 /* Round up to the first 2^n value */
117 bits = fls(hashsize);
123 hbucket_elem_add(struct hbucket *n, u8 ahash_max, size_t dsize)
125 if (n->pos >= n->size) {
128 if (n->size >= ahash_max)
129 /* Trigger rehashing */
132 tmp = kzalloc((n->size + AHASH_INIT_SIZE) * dsize,
137 memcpy(tmp, n->value, n->size * dsize);
141 n->size += AHASH_INIT_SIZE;
146 #ifdef IP_SET_HASH_WITH_NETS
147 #if IPSET_NET_COUNT > 1
148 #define __CIDR(cidr, i) (cidr[i])
150 #define __CIDR(cidr, i) (cidr)
153 /* cidr + 1 is stored in net_prefixes to support /0 */
154 #define NCIDR_PUT(cidr) ((cidr) + 1)
155 #define NCIDR_GET(cidr) ((cidr) - 1)
157 #ifdef IP_SET_HASH_WITH_NETS_PACKED
158 /* When cidr is packed with nomatch, cidr - 1 is stored in the data entry */
159 #define DCIDR_PUT(cidr) ((cidr) - 1)
160 #define DCIDR_GET(cidr, i) (__CIDR(cidr, i) + 1)
162 #define DCIDR_PUT(cidr) (cidr)
163 #define DCIDR_GET(cidr, i) __CIDR(cidr, i)
166 #define INIT_CIDR(cidr, host_mask) \
167 DCIDR_PUT(((cidr) ? NCIDR_GET(cidr) : host_mask))
169 #define SET_HOST_MASK(family) (family == AF_INET ? 32 : 128)
171 #ifdef IP_SET_HASH_WITH_NET0
172 #define NLEN(family) (SET_HOST_MASK(family) + 1)
174 #define NLEN(family) SET_HOST_MASK(family)
178 #define NLEN(family) 0
179 #endif /* IP_SET_HASH_WITH_NETS */
181 #endif /* _IP_SET_HASH_GEN_H */
183 /* Family dependent templates */
186 #undef mtype_data_equal
187 #undef mtype_do_data_match
188 #undef mtype_data_set_flags
189 #undef mtype_data_reset_elem
190 #undef mtype_data_reset_flags
191 #undef mtype_data_netmask
192 #undef mtype_data_list
193 #undef mtype_data_next
196 #undef mtype_ahash_destroy
197 #undef mtype_ext_cleanup
198 #undef mtype_add_cidr
199 #undef mtype_del_cidr
200 #undef mtype_ahash_memsize
203 #undef mtype_same_set
210 #undef mtype_test_cidrs
220 #undef mtype_data_match
224 #define mtype_data_equal IPSET_TOKEN(MTYPE, _data_equal)
225 #ifdef IP_SET_HASH_WITH_NETS
226 #define mtype_do_data_match IPSET_TOKEN(MTYPE, _do_data_match)
228 #define mtype_do_data_match(d) 1
230 #define mtype_data_set_flags IPSET_TOKEN(MTYPE, _data_set_flags)
231 #define mtype_data_reset_elem IPSET_TOKEN(MTYPE, _data_reset_elem)
232 #define mtype_data_reset_flags IPSET_TOKEN(MTYPE, _data_reset_flags)
233 #define mtype_data_netmask IPSET_TOKEN(MTYPE, _data_netmask)
234 #define mtype_data_list IPSET_TOKEN(MTYPE, _data_list)
235 #define mtype_data_next IPSET_TOKEN(MTYPE, _data_next)
236 #define mtype_elem IPSET_TOKEN(MTYPE, _elem)
238 #define mtype_ahash_destroy IPSET_TOKEN(MTYPE, _ahash_destroy)
239 #define mtype_ext_cleanup IPSET_TOKEN(MTYPE, _ext_cleanup)
240 #define mtype_add_cidr IPSET_TOKEN(MTYPE, _add_cidr)
241 #define mtype_del_cidr IPSET_TOKEN(MTYPE, _del_cidr)
242 #define mtype_ahash_memsize IPSET_TOKEN(MTYPE, _ahash_memsize)
243 #define mtype_flush IPSET_TOKEN(MTYPE, _flush)
244 #define mtype_destroy IPSET_TOKEN(MTYPE, _destroy)
245 #define mtype_same_set IPSET_TOKEN(MTYPE, _same_set)
246 #define mtype_kadt IPSET_TOKEN(MTYPE, _kadt)
247 #define mtype_uadt IPSET_TOKEN(MTYPE, _uadt)
250 #define mtype_add IPSET_TOKEN(MTYPE, _add)
251 #define mtype_del IPSET_TOKEN(MTYPE, _del)
252 #define mtype_test_cidrs IPSET_TOKEN(MTYPE, _test_cidrs)
253 #define mtype_test IPSET_TOKEN(MTYPE, _test)
254 #define mtype_uref IPSET_TOKEN(MTYPE, _uref)
255 #define mtype_expire IPSET_TOKEN(MTYPE, _expire)
256 #define mtype_resize IPSET_TOKEN(MTYPE, _resize)
257 #define mtype_head IPSET_TOKEN(MTYPE, _head)
258 #define mtype_list IPSET_TOKEN(MTYPE, _list)
259 #define mtype_gc IPSET_TOKEN(MTYPE, _gc)
260 #define mtype_gc_init IPSET_TOKEN(MTYPE, _gc_init)
261 #define mtype_variant IPSET_TOKEN(MTYPE, _variant)
262 #define mtype_data_match IPSET_TOKEN(MTYPE, _data_match)
265 #error "MTYPE is not defined!"
269 #error "HOST_MASK is not defined!"
273 #define HKEY_DATALEN sizeof(struct mtype_elem)
276 #define HKEY(data, initval, htable_bits) \
277 (jhash2((u32 *)(data), HKEY_DATALEN/sizeof(u32), initval) \
278 & jhash_mask(htable_bits))
282 #error "HTYPE is not defined!"
286 /* The generic hash structure */
288 struct htable __rcu *table; /* the hash table */
289 u32 maxelem; /* max elements in the hash */
290 u32 elements; /* current element (vs timeout) */
291 u32 initval; /* random jhash init value */
292 #ifdef IP_SET_HASH_WITH_MARKMASK
293 u32 markmask; /* markmask value for mark mask to store */
295 struct timer_list gc; /* garbage collection when timeout enabled */
296 struct mtype_elem next; /* temporary storage for uadd */
297 #ifdef IP_SET_HASH_WITH_MULTI
298 u8 ahash_max; /* max elements in an array block */
300 #ifdef IP_SET_HASH_WITH_NETMASK
301 u8 netmask; /* netmask value for subnets to store */
303 #ifdef IP_SET_HASH_WITH_RBTREE
304 struct rb_root rbtree;
306 #ifdef IP_SET_HASH_WITH_NETS
307 struct net_prefixes nets[0]; /* book-keeping of prefixes */
312 #ifdef IP_SET_HASH_WITH_NETS
313 /* Network cidr size book keeping when the hash stores different
314 * sized networks. cidr == real cidr + 1 to support /0.
317 mtype_add_cidr(struct htype *h, u8 cidr, u8 nets_length, u8 n)
321 /* Add in increasing prefix order, so larger cidr first */
322 for (i = 0, j = -1; i < nets_length && h->nets[i].cidr[n]; i++) {
325 else if (h->nets[i].cidr[n] < cidr)
327 else if (h->nets[i].cidr[n] == cidr) {
328 h->nets[cidr - 1].nets[n]++;
334 h->nets[i].cidr[n] = h->nets[i - 1].cidr[n];
336 h->nets[i].cidr[n] = cidr;
337 h->nets[cidr - 1].nets[n] = 1;
341 mtype_del_cidr(struct htype *h, u8 cidr, u8 nets_length, u8 n)
343 u8 i, j, net_end = nets_length - 1;
345 for (i = 0; i < nets_length; i++) {
346 if (h->nets[i].cidr[n] != cidr)
348 h->nets[cidr -1].nets[n]--;
349 if (h->nets[cidr -1].nets[n] > 0)
351 for (j = i; j < net_end && h->nets[j].cidr[n]; j++)
352 h->nets[j].cidr[n] = h->nets[j + 1].cidr[n];
353 h->nets[j].cidr[n] = 0;
359 /* Calculate the actual memory size of the set data */
361 mtype_ahash_memsize(const struct htype *h, const struct htable *t,
362 u8 nets_length, size_t dsize)
365 size_t memsize = sizeof(*h)
367 #ifdef IP_SET_HASH_WITH_NETS
368 + sizeof(struct net_prefixes) * nets_length
370 + jhash_size(t->htable_bits) * sizeof(struct hbucket);
372 for (i = 0; i < jhash_size(t->htable_bits); i++)
373 memsize += t->bucket[i].size * dsize;
378 /* Get the ith element from the array block n */
379 #define ahash_data(n, i, dsize) \
380 ((struct mtype_elem *)((n)->value + ((i) * (dsize))))
383 mtype_ext_cleanup(struct ip_set *set, struct hbucket *n)
387 for (i = 0; i < n->pos; i++)
388 ip_set_ext_destroy(set, ahash_data(n, i, set->dsize));
391 /* Flush a hash type of set: destroy all elements */
393 mtype_flush(struct ip_set *set)
395 struct htype *h = set->data;
400 t = rcu_dereference_bh_nfnl(h->table);
401 for (i = 0; i < jhash_size(t->htable_bits); i++) {
404 if (set->extensions & IPSET_EXT_DESTROY)
405 mtype_ext_cleanup(set, n);
406 n->size = n->pos = 0;
407 /* FIXME: use slab cache */
411 #ifdef IP_SET_HASH_WITH_NETS
412 memset(h->nets, 0, sizeof(struct net_prefixes) * NLEN(set->family));
417 /* Destroy the hashtable part of the set */
419 mtype_ahash_destroy(struct ip_set *set, struct htable *t, bool ext_destroy)
424 for (i = 0; i < jhash_size(t->htable_bits); i++) {
427 if (set->extensions & IPSET_EXT_DESTROY && ext_destroy)
428 mtype_ext_cleanup(set, n);
429 /* FIXME: use slab cache */
437 /* Destroy a hash type of set */
439 mtype_destroy(struct ip_set *set)
441 struct htype *h = set->data;
443 if (SET_WITH_TIMEOUT(set))
444 del_timer_sync(&h->gc);
446 mtype_ahash_destroy(set, rcu_dereference_bh_nfnl(h->table), true);
447 #ifdef IP_SET_HASH_WITH_RBTREE
448 rbtree_destroy(&h->rbtree);
456 mtype_gc_init(struct ip_set *set, void (*gc)(unsigned long ul_set))
458 struct htype *h = set->data;
461 h->gc.data = (unsigned long) set;
463 h->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
465 pr_debug("gc initialized, run in every %u\n",
466 IPSET_GC_PERIOD(set->timeout));
470 mtype_same_set(const struct ip_set *a, const struct ip_set *b)
472 const struct htype *x = a->data;
473 const struct htype *y = b->data;
475 /* Resizing changes htable_bits, so we ignore it */
476 return x->maxelem == y->maxelem &&
477 a->timeout == b->timeout &&
478 #ifdef IP_SET_HASH_WITH_NETMASK
479 x->netmask == y->netmask &&
481 #ifdef IP_SET_HASH_WITH_MARKMASK
482 x->markmask == y->markmask &&
484 a->extensions == b->extensions;
487 /* Delete expired elements from the hashtable */
489 mtype_expire(struct ip_set *set, struct htype *h, u8 nets_length, size_t dsize)
493 struct mtype_elem *data;
496 #ifdef IP_SET_HASH_WITH_NETS
501 t = rcu_dereference_bh(h->table);
502 for (i = 0; i < jhash_size(t->htable_bits); i++) {
504 for (j = 0; j < n->pos; j++) {
505 data = ahash_data(n, j, dsize);
506 if (ip_set_timeout_expired(ext_timeout(data, set))) {
507 pr_debug("expired %u/%u\n", i, j);
508 #ifdef IP_SET_HASH_WITH_NETS
509 for (k = 0; k < IPSET_NET_COUNT; k++)
511 NCIDR_PUT(DCIDR_GET(data->cidr,
515 ip_set_ext_destroy(set, data);
519 ahash_data(n, n->pos - 1, dsize),
525 if (n->pos + AHASH_INIT_SIZE < n->size) {
526 void *tmp = kzalloc((n->size - AHASH_INIT_SIZE)
530 /* Still try to delete expired elements */
532 n->size -= AHASH_INIT_SIZE;
533 memcpy(tmp, n->value, n->size * dsize);
538 rcu_read_unlock_bh();
542 mtype_gc(unsigned long ul_set)
544 struct ip_set *set = (struct ip_set *) ul_set;
545 struct htype *h = set->data;
547 pr_debug("called\n");
548 write_lock_bh(&set->lock);
549 mtype_expire(set, h, NLEN(set->family), set->dsize);
550 write_unlock_bh(&set->lock);
552 h->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
556 /* Resize a hash: create a new hash table with doubling the hashsize
557 * and inserting the elements to it. Repeat until we succeed or
558 * fail due to memory pressures. */
560 mtype_resize(struct ip_set *set, bool retried)
562 struct htype *h = set->data;
563 struct htable *t, *orig = rcu_dereference_bh_nfnl(h->table);
564 u8 htable_bits = orig->htable_bits;
565 #ifdef IP_SET_HASH_WITH_NETS
568 struct mtype_elem *data;
569 struct mtype_elem *d;
570 struct hbucket *n, *m;
574 /* Try to cleanup once */
575 if (SET_WITH_TIMEOUT(set) && !retried) {
577 write_lock_bh(&set->lock);
578 mtype_expire(set, set->data, NLEN(set->family), set->dsize);
579 write_unlock_bh(&set->lock);
587 pr_debug("attempt to resize set %s from %u to %u, t %p\n",
588 set->name, orig->htable_bits, htable_bits, orig);
590 /* In case we have plenty of memory :-) */
591 pr_warn("Cannot increase the hashsize of set %s further\n",
593 return -IPSET_ERR_HASH_FULL;
595 t = ip_set_alloc(sizeof(*t)
596 + jhash_size(htable_bits) * sizeof(struct hbucket));
599 t->htable_bits = htable_bits;
601 read_lock_bh(&set->lock);
602 /* There can't be another parallel resizing, but dumping is possible */
603 atomic_set(&orig->ref, 1);
604 atomic_inc(&orig->uref);
605 for (i = 0; i < jhash_size(orig->htable_bits); i++) {
606 n = hbucket(orig, i);
607 for (j = 0; j < n->pos; j++) {
608 data = ahash_data(n, j, set->dsize);
609 #ifdef IP_SET_HASH_WITH_NETS
611 mtype_data_reset_flags(data, &flags);
613 m = hbucket(t, HKEY(data, h->initval, htable_bits));
614 ret = hbucket_elem_add(m, AHASH_MAX(h), set->dsize);
616 #ifdef IP_SET_HASH_WITH_NETS
617 mtype_data_reset_flags(data, &flags);
619 atomic_set(&orig->ref, 0);
620 atomic_dec(&orig->uref);
621 read_unlock_bh(&set->lock);
622 mtype_ahash_destroy(set, t, false);
627 d = ahash_data(m, m->pos++, set->dsize);
628 memcpy(d, data, set->dsize);
629 #ifdef IP_SET_HASH_WITH_NETS
630 mtype_data_reset_flags(d, &flags);
635 rcu_assign_pointer(h->table, t);
636 read_unlock_bh(&set->lock);
638 /* Give time to other readers of the set */
639 synchronize_rcu_bh();
641 pr_debug("set %s resized from %u (%p) to %u (%p)\n", set->name,
642 orig->htable_bits, orig, t->htable_bits, t);
643 /* If there's nobody else dumping the table, destroy it */
644 if (atomic_dec_and_test(&orig->uref)) {
645 pr_debug("Table destroy by resize %p\n", orig);
646 mtype_ahash_destroy(set, orig, false);
652 /* Add an element to a hash and update the internal counters when succeeded,
653 * otherwise report the proper error code. */
655 mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
656 struct ip_set_ext *mext, u32 flags)
658 struct htype *h = set->data;
660 const struct mtype_elem *d = value;
661 struct mtype_elem *data;
664 int j = AHASH_MAX(h) + 1;
665 bool flag_exist = flags & IPSET_FLAG_EXIST;
669 t = rcu_dereference_bh(h->table);
670 key = HKEY(value, h->initval, t->htable_bits);
672 for (i = 0; i < n->pos; i++) {
673 data = ahash_data(n, i, set->dsize);
674 if (mtype_data_equal(data, d, &multi)) {
676 (SET_WITH_TIMEOUT(set) &&
677 ip_set_timeout_expired(ext_timeout(data, set)))) {
678 /* Just the extensions could be overwritten */
682 ret = -IPSET_ERR_EXIST;
686 /* Reuse first timed out entry */
687 if (SET_WITH_TIMEOUT(set) &&
688 ip_set_timeout_expired(ext_timeout(data, set)) &&
689 j != AHASH_MAX(h) + 1)
692 if (h->elements >= h->maxelem && SET_WITH_FORCEADD(set) && n->pos) {
693 /* Choosing the first entry in the array to replace */
697 if (SET_WITH_TIMEOUT(set) && h->elements >= h->maxelem)
698 /* FIXME: when set is full, we slow down here */
699 mtype_expire(set, h, NLEN(set->family), set->dsize);
701 if (h->elements >= h->maxelem) {
703 pr_warn("Set %s is full, maxelem %u reached\n",
704 set->name, h->maxelem);
705 ret = -IPSET_ERR_HASH_FULL;
710 if (j != AHASH_MAX(h) + 1) {
711 /* Fill out reused slot */
712 data = ahash_data(n, j, set->dsize);
713 #ifdef IP_SET_HASH_WITH_NETS
714 for (i = 0; i < IPSET_NET_COUNT; i++) {
715 mtype_del_cidr(h, NCIDR_PUT(DCIDR_GET(data->cidr, i)),
716 NLEN(set->family), i);
717 mtype_add_cidr(h, NCIDR_PUT(DCIDR_GET(d->cidr, i)),
718 NLEN(set->family), i);
721 ip_set_ext_destroy(set, data);
723 /* Use/create a new slot */
724 TUNE_AHASH_MAX(h, multi);
725 ret = hbucket_elem_add(n, AHASH_MAX(h), set->dsize);
728 mtype_data_next(&h->next, d);
731 data = ahash_data(n, n->pos++, set->dsize);
732 #ifdef IP_SET_HASH_WITH_NETS
733 for (i = 0; i < IPSET_NET_COUNT; i++)
734 mtype_add_cidr(h, NCIDR_PUT(DCIDR_GET(d->cidr, i)),
735 NLEN(set->family), i);
739 memcpy(data, d, sizeof(struct mtype_elem));
740 #ifdef IP_SET_HASH_WITH_NETS
741 mtype_data_set_flags(data, flags);
743 if (SET_WITH_TIMEOUT(set))
744 ip_set_timeout_set(ext_timeout(data, set), ext->timeout);
745 if (SET_WITH_COUNTER(set))
746 ip_set_init_counter(ext_counter(data, set), ext);
747 if (SET_WITH_COMMENT(set))
748 ip_set_init_comment(ext_comment(data, set), ext);
749 if (SET_WITH_SKBINFO(set))
750 ip_set_init_skbinfo(ext_skbinfo(data, set), ext);
753 rcu_read_unlock_bh();
757 /* Delete an element from the hash: swap it with the last element
758 * and free up space if possible.
761 mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
762 struct ip_set_ext *mext, u32 flags)
764 struct htype *h = set->data;
766 const struct mtype_elem *d = value;
767 struct mtype_elem *data;
769 int i, ret = -IPSET_ERR_EXIST;
770 #ifdef IP_SET_HASH_WITH_NETS
776 t = rcu_dereference_bh(h->table);
777 key = HKEY(value, h->initval, t->htable_bits);
779 for (i = 0; i < n->pos; i++) {
780 data = ahash_data(n, i, set->dsize);
781 if (!mtype_data_equal(data, d, &multi))
783 if (SET_WITH_TIMEOUT(set) &&
784 ip_set_timeout_expired(ext_timeout(data, set)))
788 memcpy(data, ahash_data(n, n->pos - 1, set->dsize),
793 #ifdef IP_SET_HASH_WITH_NETS
794 for (j = 0; j < IPSET_NET_COUNT; j++)
795 mtype_del_cidr(h, NCIDR_PUT(DCIDR_GET(d->cidr, j)),
796 NLEN(set->family), j);
798 ip_set_ext_destroy(set, data);
799 if (n->pos + AHASH_INIT_SIZE < n->size) {
800 void *tmp = kzalloc((n->size - AHASH_INIT_SIZE)
807 n->size -= AHASH_INIT_SIZE;
808 memcpy(tmp, n->value, n->size * set->dsize);
817 rcu_read_unlock_bh();
822 mtype_data_match(struct mtype_elem *data, const struct ip_set_ext *ext,
823 struct ip_set_ext *mext, struct ip_set *set, u32 flags)
825 if (SET_WITH_COUNTER(set))
826 ip_set_update_counter(ext_counter(data, set),
828 if (SET_WITH_SKBINFO(set))
829 ip_set_get_skbinfo(ext_skbinfo(data, set),
831 return mtype_do_data_match(data);
834 #ifdef IP_SET_HASH_WITH_NETS
835 /* Special test function which takes into account the different network
836 * sizes added to the set */
838 mtype_test_cidrs(struct ip_set *set, struct mtype_elem *d,
839 const struct ip_set_ext *ext,
840 struct ip_set_ext *mext, u32 flags)
842 struct htype *h = set->data;
843 struct htable *t = rcu_dereference_bh(h->table);
845 struct mtype_elem *data;
846 #if IPSET_NET_COUNT == 2
847 struct mtype_elem orig = *d;
853 u8 nets_length = NLEN(set->family);
855 pr_debug("test by nets\n");
856 for (; j < nets_length && h->nets[j].cidr[0] && !multi; j++) {
857 #if IPSET_NET_COUNT == 2
858 mtype_data_reset_elem(d, &orig);
859 mtype_data_netmask(d, NCIDR_GET(h->nets[j].cidr[0]), false);
860 for (k = 0; k < nets_length && h->nets[k].cidr[1] && !multi;
862 mtype_data_netmask(d, NCIDR_GET(h->nets[k].cidr[1]),
865 mtype_data_netmask(d, NCIDR_GET(h->nets[j].cidr[0]));
867 key = HKEY(d, h->initval, t->htable_bits);
869 for (i = 0; i < n->pos; i++) {
870 data = ahash_data(n, i, set->dsize);
871 if (!mtype_data_equal(data, d, &multi))
873 if (SET_WITH_TIMEOUT(set)) {
874 if (!ip_set_timeout_expired(
875 ext_timeout(data, set)))
876 return mtype_data_match(data, ext,
879 #ifdef IP_SET_HASH_WITH_MULTI
883 return mtype_data_match(data, ext,
886 #if IPSET_NET_COUNT == 2
894 /* Test whether the element is added to the set */
896 mtype_test(struct ip_set *set, void *value, const struct ip_set_ext *ext,
897 struct ip_set_ext *mext, u32 flags)
899 struct htype *h = set->data;
901 struct mtype_elem *d = value;
903 struct mtype_elem *data;
908 t = rcu_dereference_bh(h->table);
909 #ifdef IP_SET_HASH_WITH_NETS
910 /* If we test an IP address and not a network address,
911 * try all possible network sizes */
912 for (i = 0; i < IPSET_NET_COUNT; i++)
913 if (DCIDR_GET(d->cidr, i) != SET_HOST_MASK(set->family))
915 if (i == IPSET_NET_COUNT) {
916 ret = mtype_test_cidrs(set, d, ext, mext, flags);
921 key = HKEY(d, h->initval, t->htable_bits);
923 for (i = 0; i < n->pos; i++) {
924 data = ahash_data(n, i, set->dsize);
925 if (mtype_data_equal(data, d, &multi) &&
926 !(SET_WITH_TIMEOUT(set) &&
927 ip_set_timeout_expired(ext_timeout(data, set)))) {
928 ret = mtype_data_match(data, ext, mext, set, flags);
933 rcu_read_unlock_bh();
937 /* Reply a HEADER request: fill out the header part of the set */
939 mtype_head(struct ip_set *set, struct sk_buff *skb)
941 const struct htype *h = set->data;
942 const struct htable *t;
943 struct nlattr *nested;
946 t = rcu_dereference_bh_nfnl(h->table);
947 memsize = mtype_ahash_memsize(h, t, NLEN(set->family), set->dsize);
949 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
951 goto nla_put_failure;
952 if (nla_put_net32(skb, IPSET_ATTR_HASHSIZE,
953 htonl(jhash_size(t->htable_bits))) ||
954 nla_put_net32(skb, IPSET_ATTR_MAXELEM, htonl(h->maxelem)))
955 goto nla_put_failure;
956 #ifdef IP_SET_HASH_WITH_NETMASK
957 if (h->netmask != HOST_MASK &&
958 nla_put_u8(skb, IPSET_ATTR_NETMASK, h->netmask))
959 goto nla_put_failure;
961 #ifdef IP_SET_HASH_WITH_MARKMASK
962 if (nla_put_u32(skb, IPSET_ATTR_MARKMASK, h->markmask))
963 goto nla_put_failure;
965 if (nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
966 nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)))
967 goto nla_put_failure;
968 if (unlikely(ip_set_put_flags(skb, set)))
969 goto nla_put_failure;
970 ipset_nest_end(skb, nested);
977 /* Make possible to run dumping parallel with resizing */
979 mtype_uref(struct ip_set *set, struct netlink_callback *cb, bool start)
981 struct htype *h = set->data;
986 t = rcu_dereference_bh_nfnl(h->table);
987 atomic_inc(&t->uref);
988 cb->args[IPSET_CB_PRIVATE] = (unsigned long)t;
989 rcu_read_unlock_bh();
990 } else if (cb->args[IPSET_CB_PRIVATE]) {
991 t = (struct htable *)cb->args[IPSET_CB_PRIVATE];
992 if (atomic_dec_and_test(&t->uref) && atomic_read(&t->ref)) {
993 /* Resizing didn't destroy the hash table */
994 pr_debug("Table destroy by dump: %p\n", t);
995 mtype_ahash_destroy(set, t, false);
997 cb->args[IPSET_CB_PRIVATE] = 0;
1001 /* Reply a LIST/SAVE request: dump the elements of the specified set */
1003 mtype_list(const struct ip_set *set,
1004 struct sk_buff *skb, struct netlink_callback *cb)
1006 const struct htable *t;
1007 struct nlattr *atd, *nested;
1008 const struct hbucket *n;
1009 const struct mtype_elem *e;
1010 u32 first = cb->args[IPSET_CB_ARG0];
1011 /* We assume that one hash bucket fills into one page */
1015 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
1018 pr_debug("list hash set %s\n", set->name);
1019 t = (const struct htable *)cb->args[IPSET_CB_PRIVATE];
1020 for (; cb->args[IPSET_CB_ARG0] < jhash_size(t->htable_bits);
1021 cb->args[IPSET_CB_ARG0]++) {
1022 incomplete = skb_tail_pointer(skb);
1023 n = hbucket(t, cb->args[IPSET_CB_ARG0]);
1024 pr_debug("cb->arg bucket: %lu, t %p n %p\n",
1025 cb->args[IPSET_CB_ARG0], t, n);
1026 for (i = 0; i < n->pos; i++) {
1027 e = ahash_data(n, i, set->dsize);
1028 if (SET_WITH_TIMEOUT(set) &&
1029 ip_set_timeout_expired(ext_timeout(e, set)))
1031 pr_debug("list hash %lu hbucket %p i %u, data %p\n",
1032 cb->args[IPSET_CB_ARG0], n, i, e);
1033 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
1035 if (cb->args[IPSET_CB_ARG0] == first) {
1036 nla_nest_cancel(skb, atd);
1039 goto nla_put_failure;
1041 if (mtype_data_list(skb, e))
1042 goto nla_put_failure;
1043 if (ip_set_put_extensions(skb, set, e, true))
1044 goto nla_put_failure;
1045 ipset_nest_end(skb, nested);
1048 ipset_nest_end(skb, atd);
1049 /* Set listing finished */
1050 cb->args[IPSET_CB_ARG0] = 0;
1055 nlmsg_trim(skb, incomplete);
1056 if (unlikely(first == cb->args[IPSET_CB_ARG0])) {
1057 pr_warn("Can't list set %s: one bucket does not fit into a message. Please report it!\n",
1059 cb->args[IPSET_CB_ARG0] = 0;
1062 ipset_nest_end(skb, atd);
1067 IPSET_TOKEN(MTYPE, _kadt)(struct ip_set *set, const struct sk_buff *skb,
1068 const struct xt_action_param *par,
1069 enum ipset_adt adt, struct ip_set_adt_opt *opt);
1072 IPSET_TOKEN(MTYPE, _uadt)(struct ip_set *set, struct nlattr *tb[],
1073 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
1075 static const struct ip_set_type_variant mtype_variant = {
1079 [IPSET_ADD] = mtype_add,
1080 [IPSET_DEL] = mtype_del,
1081 [IPSET_TEST] = mtype_test,
1083 .destroy = mtype_destroy,
1084 .flush = mtype_flush,
1088 .resize = mtype_resize,
1089 .same_set = mtype_same_set,
1092 #ifdef IP_SET_EMIT_CREATE
1094 IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
1095 struct nlattr *tb[], u32 flags)
1097 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
1098 #ifdef IP_SET_HASH_WITH_MARKMASK
1102 #ifdef IP_SET_HASH_WITH_NETMASK
1109 #ifndef IP_SET_PROTO_UNDEF
1110 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
1111 return -IPSET_ERR_INVALID_FAMILY;
1114 #ifdef IP_SET_HASH_WITH_MARKMASK
1115 markmask = 0xffffffff;
1117 #ifdef IP_SET_HASH_WITH_NETMASK
1118 netmask = set->family == NFPROTO_IPV4 ? 32 : 128;
1119 pr_debug("Create set %s with family %s\n",
1120 set->name, set->family == NFPROTO_IPV4 ? "inet" : "inet6");
1123 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
1124 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
1125 #ifdef IP_SET_HASH_WITH_MARKMASK
1126 !ip_set_optattr_netorder(tb, IPSET_ATTR_MARKMASK) ||
1128 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
1129 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
1130 return -IPSET_ERR_PROTOCOL;
1132 if (tb[IPSET_ATTR_HASHSIZE]) {
1133 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
1134 if (hashsize < IPSET_MIMINAL_HASHSIZE)
1135 hashsize = IPSET_MIMINAL_HASHSIZE;
1138 if (tb[IPSET_ATTR_MAXELEM])
1139 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
1141 #ifdef IP_SET_HASH_WITH_NETMASK
1142 if (tb[IPSET_ATTR_NETMASK]) {
1143 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
1145 if ((set->family == NFPROTO_IPV4 && netmask > 32) ||
1146 (set->family == NFPROTO_IPV6 && netmask > 128) ||
1148 return -IPSET_ERR_INVALID_NETMASK;
1151 #ifdef IP_SET_HASH_WITH_MARKMASK
1152 if (tb[IPSET_ATTR_MARKMASK]) {
1153 markmask = ntohl(nla_get_u32(tb[IPSET_ATTR_MARKMASK]));
1156 return -IPSET_ERR_INVALID_MARKMASK;
1161 #ifdef IP_SET_HASH_WITH_NETS
1162 hsize += sizeof(struct net_prefixes) * NLEN(set->family);
1164 h = kzalloc(hsize, GFP_KERNEL);
1168 h->maxelem = maxelem;
1169 #ifdef IP_SET_HASH_WITH_NETMASK
1170 h->netmask = netmask;
1172 #ifdef IP_SET_HASH_WITH_MARKMASK
1173 h->markmask = markmask;
1175 get_random_bytes(&h->initval, sizeof(h->initval));
1176 set->timeout = IPSET_NO_TIMEOUT;
1178 hbits = htable_bits(hashsize);
1179 hsize = htable_size(hbits);
1184 t = ip_set_alloc(hsize);
1189 t->htable_bits = hbits;
1190 rcu_assign_pointer(h->table, t);
1193 #ifndef IP_SET_PROTO_UNDEF
1194 if (set->family == NFPROTO_IPV4) {
1196 set->variant = &IPSET_TOKEN(HTYPE, 4_variant);
1197 set->dsize = ip_set_elem_len(set, tb,
1198 sizeof(struct IPSET_TOKEN(HTYPE, 4_elem)));
1199 #ifndef IP_SET_PROTO_UNDEF
1201 set->variant = &IPSET_TOKEN(HTYPE, 6_variant);
1202 set->dsize = ip_set_elem_len(set, tb,
1203 sizeof(struct IPSET_TOKEN(HTYPE, 6_elem)));
1206 if (tb[IPSET_ATTR_TIMEOUT]) {
1207 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
1208 #ifndef IP_SET_PROTO_UNDEF
1209 if (set->family == NFPROTO_IPV4)
1211 IPSET_TOKEN(HTYPE, 4_gc_init)(set,
1212 IPSET_TOKEN(HTYPE, 4_gc));
1213 #ifndef IP_SET_PROTO_UNDEF
1215 IPSET_TOKEN(HTYPE, 6_gc_init)(set,
1216 IPSET_TOKEN(HTYPE, 6_gc));
1219 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
1220 set->name, jhash_size(t->htable_bits),
1221 t->htable_bits, h->maxelem, set->data, t);
1225 #endif /* IP_SET_EMIT_CREATE */