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/types.h>
14 #include <linux/netfilter/ipset/ip_set_timeout.h>
16 #define __ipset_dereference_protected(p, c) rcu_dereference_protected(p, c)
17 #define ipset_dereference_protected(p, set) \
18 __ipset_dereference_protected(p, spin_is_locked(&(set)->lock))
20 #define rcu_dereference_bh_nfnl(p) rcu_dereference_bh_check(p, 1)
22 /* Hashing which uses arrays to resolve clashing. The hash table is resized
23 * (doubled) when searching becomes too long.
24 * Internally jhash is used with the assumption that the size of the
25 * stored data is a multiple of sizeof(u32).
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)
39 /* Max muber of elements in the array block when tuned */
40 #define AHASH_MAX_TUNED 64
42 /* Max number of elements can be tuned */
43 #ifdef IP_SET_HASH_WITH_MULTI
44 #define AHASH_MAX(h) ((h)->ahash_max)
47 tune_ahash_max(u8 curr, u32 multi)
54 n = curr + AHASH_INIT_SIZE;
55 /* Currently, at listing one hash bucket must fit into a message.
56 * Therefore we have a hard limit here.
58 return n > curr && n <= AHASH_MAX_TUNED ? n : curr;
61 #define TUNE_AHASH_MAX(h, multi) \
62 ((h)->ahash_max = tune_ahash_max((h)->ahash_max, multi))
64 #define AHASH_MAX(h) AHASH_MAX_SIZE
65 #define TUNE_AHASH_MAX(h, multi)
70 struct rcu_head rcu; /* for call_rcu_bh */
71 /* Which positions are used in the array */
72 DECLARE_BITMAP(used, AHASH_MAX_TUNED);
73 u8 size; /* size of the array */
74 u8 pos; /* position of the first free entry */
75 unsigned char value[0]; /* the array of the values */
76 } __attribute__ ((aligned));
78 /* The hash table: the table size stored here in order to make resizing easy */
80 atomic_t ref; /* References for resizing */
81 atomic_t uref; /* References for dumping */
82 u8 htable_bits; /* size of hash table == 2^htable_bits */
83 struct hbucket __rcu *bucket[0]; /* hashtable buckets */
86 #define hbucket(h, i) ((h)->bucket[i])
88 #ifndef IPSET_NET_COUNT
89 #define IPSET_NET_COUNT 1
92 /* Book-keeping of the prefixes added to the set */
94 u32 nets[IPSET_NET_COUNT]; /* number of elements for this cidr */
95 u8 cidr[IPSET_NET_COUNT]; /* the cidr value */
98 /* Compute the hash table size */
100 htable_size(u8 hbits)
104 /* We must fit both into u32 in jhash and size_t */
107 hsize = jhash_size(hbits);
108 if ((((size_t)-1) - sizeof(struct htable)) / sizeof(struct hbucket *)
112 return hsize * sizeof(struct hbucket *) + sizeof(struct htable);
115 /* Compute htable_bits from the user input parameter hashsize */
117 htable_bits(u32 hashsize)
119 /* Assume that hashsize == 2^htable_bits */
120 u8 bits = fls(hashsize - 1);
122 if (jhash_size(bits) != hashsize)
123 /* Round up to the first 2^n value */
124 bits = fls(hashsize);
129 #ifdef IP_SET_HASH_WITH_NETS
130 #if IPSET_NET_COUNT > 1
131 #define __CIDR(cidr, i) (cidr[i])
133 #define __CIDR(cidr, i) (cidr)
136 /* cidr + 1 is stored in net_prefixes to support /0 */
137 #define NCIDR_PUT(cidr) ((cidr) + 1)
138 #define NCIDR_GET(cidr) ((cidr) - 1)
140 #ifdef IP_SET_HASH_WITH_NETS_PACKED
141 /* When cidr is packed with nomatch, cidr - 1 is stored in the data entry */
142 #define DCIDR_PUT(cidr) ((cidr) - 1)
143 #define DCIDR_GET(cidr, i) (__CIDR(cidr, i) + 1)
145 #define DCIDR_PUT(cidr) (cidr)
146 #define DCIDR_GET(cidr, i) __CIDR(cidr, i)
149 #define INIT_CIDR(cidr, host_mask) \
150 DCIDR_PUT(((cidr) ? NCIDR_GET(cidr) : host_mask))
152 #define SET_HOST_MASK(family) (family == AF_INET ? 32 : 128)
154 #ifdef IP_SET_HASH_WITH_NET0
155 /* cidr from 0 to SET_HOST_MASK() value and c = cidr + 1 */
156 #define NLEN(family) (SET_HOST_MASK(family) + 1)
157 #define CIDR_POS(c) ((c) - 1)
159 /* cidr from 1 to SET_HOST_MASK() value and c = cidr + 1 */
160 #define NLEN(family) SET_HOST_MASK(family)
161 #define CIDR_POS(c) ((c) - 2)
165 #define NLEN(family) 0
166 #endif /* IP_SET_HASH_WITH_NETS */
168 #endif /* _IP_SET_HASH_GEN_H */
170 /* Family dependent templates */
173 #undef mtype_data_equal
174 #undef mtype_do_data_match
175 #undef mtype_data_set_flags
176 #undef mtype_data_reset_elem
177 #undef mtype_data_reset_flags
178 #undef mtype_data_netmask
179 #undef mtype_data_list
180 #undef mtype_data_next
183 #undef mtype_ahash_destroy
184 #undef mtype_ext_cleanup
185 #undef mtype_add_cidr
186 #undef mtype_del_cidr
187 #undef mtype_ahash_memsize
190 #undef mtype_same_set
197 #undef mtype_test_cidrs
207 #undef mtype_data_match
211 #define mtype_data_equal IPSET_TOKEN(MTYPE, _data_equal)
212 #ifdef IP_SET_HASH_WITH_NETS
213 #define mtype_do_data_match IPSET_TOKEN(MTYPE, _do_data_match)
215 #define mtype_do_data_match(d) 1
217 #define mtype_data_set_flags IPSET_TOKEN(MTYPE, _data_set_flags)
218 #define mtype_data_reset_elem IPSET_TOKEN(MTYPE, _data_reset_elem)
219 #define mtype_data_reset_flags IPSET_TOKEN(MTYPE, _data_reset_flags)
220 #define mtype_data_netmask IPSET_TOKEN(MTYPE, _data_netmask)
221 #define mtype_data_list IPSET_TOKEN(MTYPE, _data_list)
222 #define mtype_data_next IPSET_TOKEN(MTYPE, _data_next)
223 #define mtype_elem IPSET_TOKEN(MTYPE, _elem)
225 #define mtype_ahash_destroy IPSET_TOKEN(MTYPE, _ahash_destroy)
226 #define mtype_ext_cleanup IPSET_TOKEN(MTYPE, _ext_cleanup)
227 #define mtype_add_cidr IPSET_TOKEN(MTYPE, _add_cidr)
228 #define mtype_del_cidr IPSET_TOKEN(MTYPE, _del_cidr)
229 #define mtype_ahash_memsize IPSET_TOKEN(MTYPE, _ahash_memsize)
230 #define mtype_flush IPSET_TOKEN(MTYPE, _flush)
231 #define mtype_destroy IPSET_TOKEN(MTYPE, _destroy)
232 #define mtype_same_set IPSET_TOKEN(MTYPE, _same_set)
233 #define mtype_kadt IPSET_TOKEN(MTYPE, _kadt)
234 #define mtype_uadt IPSET_TOKEN(MTYPE, _uadt)
237 #define mtype_add IPSET_TOKEN(MTYPE, _add)
238 #define mtype_del IPSET_TOKEN(MTYPE, _del)
239 #define mtype_test_cidrs IPSET_TOKEN(MTYPE, _test_cidrs)
240 #define mtype_test IPSET_TOKEN(MTYPE, _test)
241 #define mtype_uref IPSET_TOKEN(MTYPE, _uref)
242 #define mtype_expire IPSET_TOKEN(MTYPE, _expire)
243 #define mtype_resize IPSET_TOKEN(MTYPE, _resize)
244 #define mtype_head IPSET_TOKEN(MTYPE, _head)
245 #define mtype_list IPSET_TOKEN(MTYPE, _list)
246 #define mtype_gc IPSET_TOKEN(MTYPE, _gc)
247 #define mtype_gc_init IPSET_TOKEN(MTYPE, _gc_init)
248 #define mtype_variant IPSET_TOKEN(MTYPE, _variant)
249 #define mtype_data_match IPSET_TOKEN(MTYPE, _data_match)
252 #error "MTYPE is not defined!"
256 #error "HOST_MASK is not defined!"
260 #define HKEY_DATALEN sizeof(struct mtype_elem)
263 #define HKEY(data, initval, htable_bits) \
264 (jhash2((u32 *)(data), HKEY_DATALEN / sizeof(u32), initval) \
265 & jhash_mask(htable_bits))
269 #error "HTYPE is not defined!"
273 /* The generic hash structure */
275 struct htable __rcu *table; /* the hash table */
276 u32 maxelem; /* max elements in the hash */
277 u32 elements; /* current element (vs timeout) */
278 u32 initval; /* random jhash init value */
279 #ifdef IP_SET_HASH_WITH_MARKMASK
280 u32 markmask; /* markmask value for mark mask to store */
282 struct timer_list gc; /* garbage collection when timeout enabled */
283 struct mtype_elem next; /* temporary storage for uadd */
284 #ifdef IP_SET_HASH_WITH_MULTI
285 u8 ahash_max; /* max elements in an array block */
287 #ifdef IP_SET_HASH_WITH_NETMASK
288 u8 netmask; /* netmask value for subnets to store */
290 #ifdef IP_SET_HASH_WITH_NETS
291 struct net_prefixes nets[0]; /* book-keeping of prefixes */
296 #ifdef IP_SET_HASH_WITH_NETS
297 /* Network cidr size book keeping when the hash stores different
298 * sized networks. cidr == real cidr + 1 to support /0.
301 mtype_add_cidr(struct htype *h, u8 cidr, u8 nets_length, u8 n)
305 /* Add in increasing prefix order, so larger cidr first */
306 for (i = 0, j = -1; i < nets_length && h->nets[i].cidr[n]; i++) {
309 } else if (h->nets[i].cidr[n] < cidr) {
311 } else if (h->nets[i].cidr[n] == cidr) {
312 h->nets[CIDR_POS(cidr)].nets[n]++;
318 h->nets[i].cidr[n] = h->nets[i - 1].cidr[n];
320 h->nets[i].cidr[n] = cidr;
321 h->nets[CIDR_POS(cidr)].nets[n] = 1;
325 mtype_del_cidr(struct htype *h, u8 cidr, u8 nets_length, u8 n)
327 u8 i, j, net_end = nets_length - 1;
329 for (i = 0; i < nets_length; i++) {
330 if (h->nets[i].cidr[n] != cidr)
332 h->nets[CIDR_POS(cidr)].nets[n]--;
333 if (h->nets[CIDR_POS(cidr)].nets[n] > 0)
335 for (j = i; j < net_end && h->nets[j].cidr[n]; j++)
336 h->nets[j].cidr[n] = h->nets[j + 1].cidr[n];
337 h->nets[j].cidr[n] = 0;
343 /* Calculate the actual memory size of the set data */
345 mtype_ahash_memsize(const struct htype *h, const struct htable *t,
346 u8 nets_length, size_t dsize)
350 size_t memsize = sizeof(*h) + sizeof(*t);
352 #ifdef IP_SET_HASH_WITH_NETS
353 memsize += sizeof(struct net_prefixes) * nets_length;
355 for (i = 0; i < jhash_size(t->htable_bits); i++) {
356 n = rcu_dereference_bh(hbucket(t, i));
359 memsize += sizeof(struct hbucket) + n->size * dsize;
365 /* Get the ith element from the array block n */
366 #define ahash_data(n, i, dsize) \
367 ((struct mtype_elem *)((n)->value + ((i) * (dsize))))
370 mtype_ext_cleanup(struct ip_set *set, struct hbucket *n)
374 for (i = 0; i < n->pos; i++)
375 if (test_bit(i, n->used))
376 ip_set_ext_destroy(set, ahash_data(n, i, set->dsize));
379 /* Flush a hash type of set: destroy all elements */
381 mtype_flush(struct ip_set *set)
383 struct htype *h = set->data;
388 t = ipset_dereference_protected(h->table, set);
389 for (i = 0; i < jhash_size(t->htable_bits); i++) {
390 n = __ipset_dereference_protected(hbucket(t, i), 1);
393 if (set->extensions & IPSET_EXT_DESTROY)
394 mtype_ext_cleanup(set, n);
395 /* FIXME: use slab cache */
396 rcu_assign_pointer(hbucket(t, i), NULL);
399 #ifdef IP_SET_HASH_WITH_NETS
400 memset(h->nets, 0, sizeof(struct net_prefixes) * NLEN(set->family));
405 /* Destroy the hashtable part of the set */
407 mtype_ahash_destroy(struct ip_set *set, struct htable *t, bool ext_destroy)
412 for (i = 0; i < jhash_size(t->htable_bits); i++) {
413 n = __ipset_dereference_protected(hbucket(t, i), 1);
416 if (set->extensions & IPSET_EXT_DESTROY && ext_destroy)
417 mtype_ext_cleanup(set, n);
418 /* FIXME: use slab cache */
425 /* Destroy a hash type of set */
427 mtype_destroy(struct ip_set *set)
429 struct htype *h = set->data;
431 if (SET_WITH_TIMEOUT(set))
432 del_timer_sync(&h->gc);
434 mtype_ahash_destroy(set,
435 __ipset_dereference_protected(h->table, 1), true);
442 mtype_gc_init(struct ip_set *set, void (*gc)(unsigned long ul_set))
444 struct htype *h = set->data;
447 h->gc.data = (unsigned long)set;
449 h->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
451 pr_debug("gc initialized, run in every %u\n",
452 IPSET_GC_PERIOD(set->timeout));
456 mtype_same_set(const struct ip_set *a, const struct ip_set *b)
458 const struct htype *x = a->data;
459 const struct htype *y = b->data;
461 /* Resizing changes htable_bits, so we ignore it */
462 return x->maxelem == y->maxelem &&
463 a->timeout == b->timeout &&
464 #ifdef IP_SET_HASH_WITH_NETMASK
465 x->netmask == y->netmask &&
467 #ifdef IP_SET_HASH_WITH_MARKMASK
468 x->markmask == y->markmask &&
470 a->extensions == b->extensions;
473 /* Delete expired elements from the hashtable */
475 mtype_expire(struct ip_set *set, struct htype *h, u8 nets_length, size_t dsize)
479 struct mtype_elem *data;
481 #ifdef IP_SET_HASH_WITH_NETS
485 t = ipset_dereference_protected(h->table, set);
486 for (i = 0; i < jhash_size(t->htable_bits); i++) {
487 n = __ipset_dereference_protected(hbucket(t, i), 1);
490 for (j = 0, d = 0; j < n->pos; j++) {
491 if (!test_bit(j, n->used)) {
495 data = ahash_data(n, j, dsize);
496 if (ip_set_timeout_expired(ext_timeout(data, set))) {
497 pr_debug("expired %u/%u\n", i, j);
498 clear_bit(j, n->used);
499 smp_mb__after_atomic();
500 #ifdef IP_SET_HASH_WITH_NETS
501 for (k = 0; k < IPSET_NET_COUNT; k++)
503 NCIDR_PUT(DCIDR_GET(data->cidr,
507 ip_set_ext_destroy(set, data);
512 if (d >= AHASH_INIT_SIZE) {
513 struct hbucket *tmp = kzalloc(sizeof(*tmp) +
514 (n->size - AHASH_INIT_SIZE) * dsize,
517 /* Still try to delete expired elements */
519 tmp->size = n->size - AHASH_INIT_SIZE;
520 for (j = 0, d = 0; j < n->pos; j++) {
521 if (!test_bit(j, n->used))
523 data = ahash_data(n, j, dsize);
524 memcpy(tmp->value + d * dsize, data, dsize);
525 set_bit(j, tmp->used);
529 rcu_assign_pointer(hbucket(t, i), tmp);
536 mtype_gc(unsigned long ul_set)
538 struct ip_set *set = (struct ip_set *)ul_set;
539 struct htype *h = set->data;
541 pr_debug("called\n");
542 spin_lock_bh(&set->lock);
543 mtype_expire(set, h, NLEN(set->family), set->dsize);
544 spin_unlock_bh(&set->lock);
546 h->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
550 /* Resize a hash: create a new hash table with doubling the hashsize
551 * and inserting the elements to it. Repeat until we succeed or
552 * fail due to memory pressures.
555 mtype_resize(struct ip_set *set, bool retried)
557 struct htype *h = set->data;
558 struct htable *t, *orig;
560 size_t dsize = set->dsize;
561 #ifdef IP_SET_HASH_WITH_NETS
563 struct mtype_elem *tmp;
565 struct mtype_elem *data;
566 struct mtype_elem *d;
567 struct hbucket *n, *m;
571 #ifdef IP_SET_HASH_WITH_NETS
572 tmp = kmalloc(dsize, GFP_KERNEL);
577 orig = rcu_dereference_bh_nfnl(h->table);
578 htable_bits = orig->htable_bits;
579 rcu_read_unlock_bh();
585 /* In case we have plenty of memory :-) */
586 pr_warn("Cannot increase the hashsize of set %s further\n",
588 ret = -IPSET_ERR_HASH_FULL;
591 t = ip_set_alloc(htable_size(htable_bits));
596 t->htable_bits = htable_bits;
598 spin_lock_bh(&set->lock);
599 orig = __ipset_dereference_protected(h->table, 1);
600 /* There can't be another parallel resizing, but dumping is possible */
601 atomic_set(&orig->ref, 1);
602 atomic_inc(&orig->uref);
603 pr_debug("attempt to resize set %s from %u to %u, t %p\n",
604 set->name, orig->htable_bits, htable_bits, orig);
605 for (i = 0; i < jhash_size(orig->htable_bits); i++) {
606 n = __ipset_dereference_protected(hbucket(orig, i), 1);
609 for (j = 0; j < n->pos; j++) {
610 if (!test_bit(j, n->used))
612 data = ahash_data(n, j, dsize);
613 #ifdef IP_SET_HASH_WITH_NETS
614 /* We have readers running parallel with us,
615 * so the live data cannot be modified.
618 memcpy(tmp, data, dsize);
620 mtype_data_reset_flags(data, &flags);
622 key = HKEY(data, h->initval, htable_bits);
623 m = __ipset_dereference_protected(hbucket(t, key), 1);
625 m = kzalloc(sizeof(*m) +
626 AHASH_INIT_SIZE * dsize,
632 m->size = AHASH_INIT_SIZE;
633 RCU_INIT_POINTER(hbucket(t, key), m);
634 } else if (m->pos >= m->size) {
637 if (m->size >= AHASH_MAX(h)) {
640 ht = kzalloc(sizeof(*ht) +
641 (m->size + AHASH_INIT_SIZE)
649 memcpy(ht, m, sizeof(struct hbucket) +
651 ht->size = m->size + AHASH_INIT_SIZE;
654 RCU_INIT_POINTER(hbucket(t, key), ht);
656 d = ahash_data(m, m->pos, dsize);
657 memcpy(d, data, dsize);
658 set_bit(m->pos++, m->used);
659 #ifdef IP_SET_HASH_WITH_NETS
660 mtype_data_reset_flags(d, &flags);
664 rcu_assign_pointer(h->table, t);
666 spin_unlock_bh(&set->lock);
668 /* Give time to other readers of the set */
669 synchronize_rcu_bh();
671 pr_debug("set %s resized from %u (%p) to %u (%p)\n", set->name,
672 orig->htable_bits, orig, t->htable_bits, t);
673 /* If there's nobody else dumping the table, destroy it */
674 if (atomic_dec_and_test(&orig->uref)) {
675 pr_debug("Table destroy by resize %p\n", orig);
676 mtype_ahash_destroy(set, orig, false);
680 #ifdef IP_SET_HASH_WITH_NETS
686 atomic_set(&orig->ref, 0);
687 atomic_dec(&orig->uref);
688 spin_unlock_bh(&set->lock);
689 mtype_ahash_destroy(set, t, false);
695 /* Add an element to a hash and update the internal counters when succeeded,
696 * otherwise report the proper error code.
699 mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
700 struct ip_set_ext *mext, u32 flags)
702 struct htype *h = set->data;
704 const struct mtype_elem *d = value;
705 struct mtype_elem *data;
706 struct hbucket *n, *old = ERR_PTR(-ENOENT);
708 bool flag_exist = flags & IPSET_FLAG_EXIST;
709 bool deleted = false, forceadd = false, reuse = false;
712 if (h->elements >= h->maxelem) {
713 if (SET_WITH_TIMEOUT(set))
714 /* FIXME: when set is full, we slow down here */
715 mtype_expire(set, h, NLEN(set->family), set->dsize);
716 if (h->elements >= h->maxelem && SET_WITH_FORCEADD(set))
720 t = ipset_dereference_protected(h->table, set);
721 key = HKEY(value, h->initval, t->htable_bits);
722 n = __ipset_dereference_protected(hbucket(t, key), 1);
726 pr_warn("Set %s is full, maxelem %u reached\n",
727 set->name, h->maxelem);
728 return -IPSET_ERR_HASH_FULL;
729 } else if (h->elements >= h->maxelem) {
733 n = kzalloc(sizeof(*n) + AHASH_INIT_SIZE * set->dsize,
737 n->size = AHASH_INIT_SIZE;
740 for (i = 0; i < n->pos; i++) {
741 if (!test_bit(i, n->used)) {
742 /* Reuse first deleted entry */
744 deleted = reuse = true;
749 data = ahash_data(n, i, set->dsize);
750 if (mtype_data_equal(data, d, &multi)) {
752 (SET_WITH_TIMEOUT(set) &&
753 ip_set_timeout_expired(ext_timeout(data, set)))) {
754 /* Just the extensions could be overwritten */
756 goto overwrite_extensions;
758 return -IPSET_ERR_EXIST;
760 /* Reuse first timed out entry */
761 if (SET_WITH_TIMEOUT(set) &&
762 ip_set_timeout_expired(ext_timeout(data, set)) &&
768 if (reuse || forceadd) {
769 data = ahash_data(n, j, set->dsize);
771 #ifdef IP_SET_HASH_WITH_NETS
772 for (i = 0; i < IPSET_NET_COUNT; i++)
774 NCIDR_PUT(DCIDR_GET(data->cidr, i)),
775 NLEN(set->family), i);
777 ip_set_ext_destroy(set, data);
782 if (h->elements >= h->maxelem)
784 /* Create a new slot */
785 if (n->pos >= n->size) {
786 TUNE_AHASH_MAX(h, multi);
787 if (n->size >= AHASH_MAX(h)) {
788 /* Trigger rehashing */
789 mtype_data_next(&h->next, d);
793 n = kzalloc(sizeof(*n) +
794 (old->size + AHASH_INIT_SIZE) * set->dsize,
798 memcpy(n, old, sizeof(struct hbucket) +
799 old->size * set->dsize);
800 n->size = old->size + AHASH_INIT_SIZE;
805 data = ahash_data(n, j, set->dsize);
808 #ifdef IP_SET_HASH_WITH_NETS
809 for (i = 0; i < IPSET_NET_COUNT; i++)
810 mtype_add_cidr(h, NCIDR_PUT(DCIDR_GET(d->cidr, i)),
811 NLEN(set->family), i);
813 memcpy(data, d, sizeof(struct mtype_elem));
814 overwrite_extensions:
815 #ifdef IP_SET_HASH_WITH_NETS
816 mtype_data_set_flags(data, flags);
818 if (SET_WITH_COUNTER(set))
819 ip_set_init_counter(ext_counter(data, set), ext);
820 if (SET_WITH_COMMENT(set))
821 ip_set_init_comment(ext_comment(data, set), ext);
822 if (SET_WITH_SKBINFO(set))
823 ip_set_init_skbinfo(ext_skbinfo(data, set), ext);
824 /* Must come last for the case when timed out entry is reused */
825 if (SET_WITH_TIMEOUT(set))
826 ip_set_timeout_set(ext_timeout(data, set), ext->timeout);
827 smp_mb__before_atomic();
829 if (old != ERR_PTR(-ENOENT)) {
830 rcu_assign_pointer(hbucket(t, key), n);
838 pr_warn("Set %s is full, maxelem %u reached\n",
839 set->name, h->maxelem);
840 return -IPSET_ERR_HASH_FULL;
843 /* Delete an element from the hash and free up space if possible.
846 mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
847 struct ip_set_ext *mext, u32 flags)
849 struct htype *h = set->data;
851 const struct mtype_elem *d = value;
852 struct mtype_elem *data;
854 int i, j, k, ret = -IPSET_ERR_EXIST;
856 size_t dsize = set->dsize;
858 t = ipset_dereference_protected(h->table, set);
859 key = HKEY(value, h->initval, t->htable_bits);
860 n = __ipset_dereference_protected(hbucket(t, key), 1);
863 for (i = 0, k = 0; i < n->pos; i++) {
864 if (!test_bit(i, n->used)) {
868 data = ahash_data(n, i, dsize);
869 if (!mtype_data_equal(data, d, &multi))
871 if (SET_WITH_TIMEOUT(set) &&
872 ip_set_timeout_expired(ext_timeout(data, set)))
876 clear_bit(i, n->used);
877 smp_mb__after_atomic();
881 #ifdef IP_SET_HASH_WITH_NETS
882 for (j = 0; j < IPSET_NET_COUNT; j++)
883 mtype_del_cidr(h, NCIDR_PUT(DCIDR_GET(d->cidr, j)),
884 NLEN(set->family), j);
886 ip_set_ext_destroy(set, data);
888 for (; i < n->pos; i++) {
889 if (!test_bit(i, n->used))
892 if (n->pos == 0 && k == 0) {
893 rcu_assign_pointer(hbucket(t, key), NULL);
895 } else if (k >= AHASH_INIT_SIZE) {
896 struct hbucket *tmp = kzalloc(sizeof(*tmp) +
897 (n->size - AHASH_INIT_SIZE) * dsize,
901 tmp->size = n->size - AHASH_INIT_SIZE;
902 for (j = 0, k = 0; j < n->pos; j++) {
903 if (!test_bit(j, n->used))
905 data = ahash_data(n, j, dsize);
906 memcpy(tmp->value + k * dsize, data, dsize);
907 set_bit(j, tmp->used);
911 rcu_assign_pointer(hbucket(t, key), tmp);
922 mtype_data_match(struct mtype_elem *data, const struct ip_set_ext *ext,
923 struct ip_set_ext *mext, struct ip_set *set, u32 flags)
925 if (SET_WITH_COUNTER(set))
926 ip_set_update_counter(ext_counter(data, set),
928 if (SET_WITH_SKBINFO(set))
929 ip_set_get_skbinfo(ext_skbinfo(data, set),
931 return mtype_do_data_match(data);
934 #ifdef IP_SET_HASH_WITH_NETS
935 /* Special test function which takes into account the different network
936 * sizes added to the set
939 mtype_test_cidrs(struct ip_set *set, struct mtype_elem *d,
940 const struct ip_set_ext *ext,
941 struct ip_set_ext *mext, u32 flags)
943 struct htype *h = set->data;
944 struct htable *t = rcu_dereference_bh(h->table);
946 struct mtype_elem *data;
947 #if IPSET_NET_COUNT == 2
948 struct mtype_elem orig = *d;
954 u8 nets_length = NLEN(set->family);
956 pr_debug("test by nets\n");
957 for (; j < nets_length && h->nets[j].cidr[0] && !multi; j++) {
958 #if IPSET_NET_COUNT == 2
959 mtype_data_reset_elem(d, &orig);
960 mtype_data_netmask(d, NCIDR_GET(h->nets[j].cidr[0]), false);
961 for (k = 0; k < nets_length && h->nets[k].cidr[1] && !multi;
963 mtype_data_netmask(d, NCIDR_GET(h->nets[k].cidr[1]),
966 mtype_data_netmask(d, NCIDR_GET(h->nets[j].cidr[0]));
968 key = HKEY(d, h->initval, t->htable_bits);
969 n = rcu_dereference_bh(hbucket(t, key));
972 for (i = 0; i < n->pos; i++) {
973 if (!test_bit(i, n->used))
975 data = ahash_data(n, i, set->dsize);
976 if (!mtype_data_equal(data, d, &multi))
978 if (SET_WITH_TIMEOUT(set)) {
979 if (!ip_set_timeout_expired(
980 ext_timeout(data, set)))
981 return mtype_data_match(data, ext,
984 #ifdef IP_SET_HASH_WITH_MULTI
988 return mtype_data_match(data, ext,
991 #if IPSET_NET_COUNT == 2
999 /* Test whether the element is added to the set */
1001 mtype_test(struct ip_set *set, void *value, const struct ip_set_ext *ext,
1002 struct ip_set_ext *mext, u32 flags)
1004 struct htype *h = set->data;
1006 struct mtype_elem *d = value;
1008 struct mtype_elem *data;
1012 t = rcu_dereference_bh(h->table);
1013 #ifdef IP_SET_HASH_WITH_NETS
1014 /* If we test an IP address and not a network address,
1015 * try all possible network sizes
1017 for (i = 0; i < IPSET_NET_COUNT; i++)
1018 if (DCIDR_GET(d->cidr, i) != SET_HOST_MASK(set->family))
1020 if (i == IPSET_NET_COUNT) {
1021 ret = mtype_test_cidrs(set, d, ext, mext, flags);
1026 key = HKEY(d, h->initval, t->htable_bits);
1027 n = rcu_dereference_bh(hbucket(t, key));
1032 for (i = 0; i < n->pos; i++) {
1033 if (!test_bit(i, n->used))
1035 data = ahash_data(n, i, set->dsize);
1036 if (mtype_data_equal(data, d, &multi) &&
1037 !(SET_WITH_TIMEOUT(set) &&
1038 ip_set_timeout_expired(ext_timeout(data, set)))) {
1039 ret = mtype_data_match(data, ext, mext, set, flags);
1047 /* Reply a HEADER request: fill out the header part of the set */
1049 mtype_head(struct ip_set *set, struct sk_buff *skb)
1051 const struct htype *h = set->data;
1052 const struct htable *t;
1053 struct nlattr *nested;
1058 t = rcu_dereference_bh_nfnl(h->table);
1059 memsize = mtype_ahash_memsize(h, t, NLEN(set->family), set->dsize);
1060 htable_bits = t->htable_bits;
1061 rcu_read_unlock_bh();
1063 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
1065 goto nla_put_failure;
1066 if (nla_put_net32(skb, IPSET_ATTR_HASHSIZE,
1067 htonl(jhash_size(htable_bits))) ||
1068 nla_put_net32(skb, IPSET_ATTR_MAXELEM, htonl(h->maxelem)))
1069 goto nla_put_failure;
1070 #ifdef IP_SET_HASH_WITH_NETMASK
1071 if (h->netmask != HOST_MASK &&
1072 nla_put_u8(skb, IPSET_ATTR_NETMASK, h->netmask))
1073 goto nla_put_failure;
1075 #ifdef IP_SET_HASH_WITH_MARKMASK
1076 if (nla_put_u32(skb, IPSET_ATTR_MARKMASK, h->markmask))
1077 goto nla_put_failure;
1079 if (nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
1080 nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)))
1081 goto nla_put_failure;
1082 if (unlikely(ip_set_put_flags(skb, set)))
1083 goto nla_put_failure;
1084 ipset_nest_end(skb, nested);
1091 /* Make possible to run dumping parallel with resizing */
1093 mtype_uref(struct ip_set *set, struct netlink_callback *cb, bool start)
1095 struct htype *h = set->data;
1100 t = rcu_dereference_bh_nfnl(h->table);
1101 atomic_inc(&t->uref);
1102 cb->args[IPSET_CB_PRIVATE] = (unsigned long)t;
1103 rcu_read_unlock_bh();
1104 } else if (cb->args[IPSET_CB_PRIVATE]) {
1105 t = (struct htable *)cb->args[IPSET_CB_PRIVATE];
1106 if (atomic_dec_and_test(&t->uref) && atomic_read(&t->ref)) {
1107 /* Resizing didn't destroy the hash table */
1108 pr_debug("Table destroy by dump: %p\n", t);
1109 mtype_ahash_destroy(set, t, false);
1111 cb->args[IPSET_CB_PRIVATE] = 0;
1115 /* Reply a LIST/SAVE request: dump the elements of the specified set */
1117 mtype_list(const struct ip_set *set,
1118 struct sk_buff *skb, struct netlink_callback *cb)
1120 const struct htable *t;
1121 struct nlattr *atd, *nested;
1122 const struct hbucket *n;
1123 const struct mtype_elem *e;
1124 u32 first = cb->args[IPSET_CB_ARG0];
1125 /* We assume that one hash bucket fills into one page */
1129 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
1133 pr_debug("list hash set %s\n", set->name);
1134 t = (const struct htable *)cb->args[IPSET_CB_PRIVATE];
1135 /* Expire may replace a hbucket with another one */
1137 for (; cb->args[IPSET_CB_ARG0] < jhash_size(t->htable_bits);
1138 cb->args[IPSET_CB_ARG0]++) {
1139 incomplete = skb_tail_pointer(skb);
1140 n = rcu_dereference(hbucket(t, cb->args[IPSET_CB_ARG0]));
1141 pr_debug("cb->arg bucket: %lu, t %p n %p\n",
1142 cb->args[IPSET_CB_ARG0], t, n);
1145 for (i = 0; i < n->pos; i++) {
1146 if (!test_bit(i, n->used))
1148 e = ahash_data(n, i, set->dsize);
1149 if (SET_WITH_TIMEOUT(set) &&
1150 ip_set_timeout_expired(ext_timeout(e, set)))
1152 pr_debug("list hash %lu hbucket %p i %u, data %p\n",
1153 cb->args[IPSET_CB_ARG0], n, i, e);
1154 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
1156 if (cb->args[IPSET_CB_ARG0] == first) {
1157 nla_nest_cancel(skb, atd);
1161 goto nla_put_failure;
1163 if (mtype_data_list(skb, e))
1164 goto nla_put_failure;
1165 if (ip_set_put_extensions(skb, set, e, true))
1166 goto nla_put_failure;
1167 ipset_nest_end(skb, nested);
1170 ipset_nest_end(skb, atd);
1171 /* Set listing finished */
1172 cb->args[IPSET_CB_ARG0] = 0;
1177 nlmsg_trim(skb, incomplete);
1178 if (unlikely(first == cb->args[IPSET_CB_ARG0])) {
1179 pr_warn("Can't list set %s: one bucket does not fit into a message. Please report it!\n",
1181 cb->args[IPSET_CB_ARG0] = 0;
1184 ipset_nest_end(skb, atd);
1192 IPSET_TOKEN(MTYPE, _kadt)(struct ip_set *set, const struct sk_buff *skb,
1193 const struct xt_action_param *par,
1194 enum ipset_adt adt, struct ip_set_adt_opt *opt);
1197 IPSET_TOKEN(MTYPE, _uadt)(struct ip_set *set, struct nlattr *tb[],
1198 enum ipset_adt adt, u32 *lineno, u32 flags,
1201 static const struct ip_set_type_variant mtype_variant = {
1205 [IPSET_ADD] = mtype_add,
1206 [IPSET_DEL] = mtype_del,
1207 [IPSET_TEST] = mtype_test,
1209 .destroy = mtype_destroy,
1210 .flush = mtype_flush,
1214 .resize = mtype_resize,
1215 .same_set = mtype_same_set,
1218 #ifdef IP_SET_EMIT_CREATE
1220 IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
1221 struct nlattr *tb[], u32 flags)
1223 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
1224 #ifdef IP_SET_HASH_WITH_MARKMASK
1228 #ifdef IP_SET_HASH_WITH_NETMASK
1235 #ifndef IP_SET_PROTO_UNDEF
1236 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
1237 return -IPSET_ERR_INVALID_FAMILY;
1240 #ifdef IP_SET_HASH_WITH_MARKMASK
1241 markmask = 0xffffffff;
1243 #ifdef IP_SET_HASH_WITH_NETMASK
1244 netmask = set->family == NFPROTO_IPV4 ? 32 : 128;
1245 pr_debug("Create set %s with family %s\n",
1246 set->name, set->family == NFPROTO_IPV4 ? "inet" : "inet6");
1249 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
1250 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
1251 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
1252 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
1253 return -IPSET_ERR_PROTOCOL;
1254 #ifdef IP_SET_HASH_WITH_MARKMASK
1255 /* Separated condition in order to avoid directive in argument list */
1256 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_MARKMASK)))
1257 return -IPSET_ERR_PROTOCOL;
1260 if (tb[IPSET_ATTR_HASHSIZE]) {
1261 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
1262 if (hashsize < IPSET_MIMINAL_HASHSIZE)
1263 hashsize = IPSET_MIMINAL_HASHSIZE;
1266 if (tb[IPSET_ATTR_MAXELEM])
1267 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
1269 #ifdef IP_SET_HASH_WITH_NETMASK
1270 if (tb[IPSET_ATTR_NETMASK]) {
1271 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
1273 if ((set->family == NFPROTO_IPV4 && netmask > 32) ||
1274 (set->family == NFPROTO_IPV6 && netmask > 128) ||
1276 return -IPSET_ERR_INVALID_NETMASK;
1279 #ifdef IP_SET_HASH_WITH_MARKMASK
1280 if (tb[IPSET_ATTR_MARKMASK]) {
1281 markmask = ntohl(nla_get_be32(tb[IPSET_ATTR_MARKMASK]));
1284 return -IPSET_ERR_INVALID_MARKMASK;
1289 #ifdef IP_SET_HASH_WITH_NETS
1290 hsize += sizeof(struct net_prefixes) * NLEN(set->family);
1292 h = kzalloc(hsize, GFP_KERNEL);
1296 h->maxelem = maxelem;
1297 #ifdef IP_SET_HASH_WITH_NETMASK
1298 h->netmask = netmask;
1300 #ifdef IP_SET_HASH_WITH_MARKMASK
1301 h->markmask = markmask;
1303 get_random_bytes(&h->initval, sizeof(h->initval));
1304 set->timeout = IPSET_NO_TIMEOUT;
1306 hbits = htable_bits(hashsize);
1307 hsize = htable_size(hbits);
1312 t = ip_set_alloc(hsize);
1317 t->htable_bits = hbits;
1318 rcu_assign_pointer(h->table, t);
1321 #ifndef IP_SET_PROTO_UNDEF
1322 if (set->family == NFPROTO_IPV4) {
1324 set->variant = &IPSET_TOKEN(HTYPE, 4_variant);
1325 set->dsize = ip_set_elem_len(set, tb,
1326 sizeof(struct IPSET_TOKEN(HTYPE, 4_elem)));
1327 #ifndef IP_SET_PROTO_UNDEF
1329 set->variant = &IPSET_TOKEN(HTYPE, 6_variant);
1330 set->dsize = ip_set_elem_len(set, tb,
1331 sizeof(struct IPSET_TOKEN(HTYPE, 6_elem)));
1334 if (tb[IPSET_ATTR_TIMEOUT]) {
1335 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
1336 #ifndef IP_SET_PROTO_UNDEF
1337 if (set->family == NFPROTO_IPV4)
1339 IPSET_TOKEN(HTYPE, 4_gc_init)(set,
1340 IPSET_TOKEN(HTYPE, 4_gc));
1341 #ifndef IP_SET_PROTO_UNDEF
1343 IPSET_TOKEN(HTYPE, 6_gc_init)(set,
1344 IPSET_TOKEN(HTYPE, 6_gc));
1347 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
1348 set->name, jhash_size(t->htable_bits),
1349 t->htable_bits, h->maxelem, set->data, t);
1353 #endif /* IP_SET_EMIT_CREATE */