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 __aligned(__alignof__(u64));
79 /* The hash table: the table size stored here in order to make resizing easy */
81 atomic_t ref; /* References for resizing */
82 atomic_t uref; /* References for dumping */
83 u8 htable_bits; /* size of hash table == 2^htable_bits */
84 struct hbucket __rcu *bucket[0]; /* hashtable buckets */
87 #define hbucket(h, i) ((h)->bucket[i])
89 #ifndef IPSET_NET_COUNT
90 #define IPSET_NET_COUNT 1
93 /* Book-keeping of the prefixes added to the set */
95 u32 nets[IPSET_NET_COUNT]; /* number of elements for this cidr */
96 u8 cidr[IPSET_NET_COUNT]; /* the cidr value */
99 /* Compute the hash table size */
101 htable_size(u8 hbits)
105 /* We must fit both into u32 in jhash and INT_MAX in kvmalloc_node() */
108 hsize = jhash_size(hbits);
109 if ((INT_MAX - sizeof(struct htable)) / sizeof(struct hbucket *)
113 return hsize * sizeof(struct hbucket *) + sizeof(struct htable);
116 #ifdef IP_SET_HASH_WITH_NETS
117 #if IPSET_NET_COUNT > 1
118 #define __CIDR(cidr, i) (cidr[i])
120 #define __CIDR(cidr, i) (cidr)
123 /* cidr + 1 is stored in net_prefixes to support /0 */
124 #define NCIDR_PUT(cidr) ((cidr) + 1)
125 #define NCIDR_GET(cidr) ((cidr) - 1)
127 #ifdef IP_SET_HASH_WITH_NETS_PACKED
128 /* When cidr is packed with nomatch, cidr - 1 is stored in the data entry */
129 #define DCIDR_PUT(cidr) ((cidr) - 1)
130 #define DCIDR_GET(cidr, i) (__CIDR(cidr, i) + 1)
132 #define DCIDR_PUT(cidr) (cidr)
133 #define DCIDR_GET(cidr, i) __CIDR(cidr, i)
136 #define INIT_CIDR(cidr, host_mask) \
137 DCIDR_PUT(((cidr) ? NCIDR_GET(cidr) : host_mask))
139 #define SET_HOST_MASK(family) (family == AF_INET ? 32 : 128)
141 #ifdef IP_SET_HASH_WITH_NET0
142 /* cidr from 0 to SET_HOST_MASK() value and c = cidr + 1 */
143 #define NLEN(family) (SET_HOST_MASK(family) + 1)
144 #define CIDR_POS(c) ((c) - 1)
146 /* cidr from 1 to SET_HOST_MASK() value and c = cidr + 1 */
147 #define NLEN(family) SET_HOST_MASK(family)
148 #define CIDR_POS(c) ((c) - 2)
152 #define NLEN(family) 0
153 #endif /* IP_SET_HASH_WITH_NETS */
155 #endif /* _IP_SET_HASH_GEN_H */
157 /* Family dependent templates */
160 #undef mtype_data_equal
161 #undef mtype_do_data_match
162 #undef mtype_data_set_flags
163 #undef mtype_data_reset_elem
164 #undef mtype_data_reset_flags
165 #undef mtype_data_netmask
166 #undef mtype_data_list
167 #undef mtype_data_next
170 #undef mtype_ahash_destroy
171 #undef mtype_ext_cleanup
172 #undef mtype_add_cidr
173 #undef mtype_del_cidr
174 #undef mtype_ahash_memsize
177 #undef mtype_same_set
184 #undef mtype_test_cidrs
194 #undef mtype_data_match
198 #define mtype_data_equal IPSET_TOKEN(MTYPE, _data_equal)
199 #ifdef IP_SET_HASH_WITH_NETS
200 #define mtype_do_data_match IPSET_TOKEN(MTYPE, _do_data_match)
202 #define mtype_do_data_match(d) 1
204 #define mtype_data_set_flags IPSET_TOKEN(MTYPE, _data_set_flags)
205 #define mtype_data_reset_elem IPSET_TOKEN(MTYPE, _data_reset_elem)
206 #define mtype_data_reset_flags IPSET_TOKEN(MTYPE, _data_reset_flags)
207 #define mtype_data_netmask IPSET_TOKEN(MTYPE, _data_netmask)
208 #define mtype_data_list IPSET_TOKEN(MTYPE, _data_list)
209 #define mtype_data_next IPSET_TOKEN(MTYPE, _data_next)
210 #define mtype_elem IPSET_TOKEN(MTYPE, _elem)
212 #define mtype_ahash_destroy IPSET_TOKEN(MTYPE, _ahash_destroy)
213 #define mtype_ext_cleanup IPSET_TOKEN(MTYPE, _ext_cleanup)
214 #define mtype_add_cidr IPSET_TOKEN(MTYPE, _add_cidr)
215 #define mtype_del_cidr IPSET_TOKEN(MTYPE, _del_cidr)
216 #define mtype_ahash_memsize IPSET_TOKEN(MTYPE, _ahash_memsize)
217 #define mtype_flush IPSET_TOKEN(MTYPE, _flush)
218 #define mtype_destroy IPSET_TOKEN(MTYPE, _destroy)
219 #define mtype_same_set IPSET_TOKEN(MTYPE, _same_set)
220 #define mtype_kadt IPSET_TOKEN(MTYPE, _kadt)
221 #define mtype_uadt IPSET_TOKEN(MTYPE, _uadt)
224 #define mtype_add IPSET_TOKEN(MTYPE, _add)
225 #define mtype_del IPSET_TOKEN(MTYPE, _del)
226 #define mtype_test_cidrs IPSET_TOKEN(MTYPE, _test_cidrs)
227 #define mtype_test IPSET_TOKEN(MTYPE, _test)
228 #define mtype_uref IPSET_TOKEN(MTYPE, _uref)
229 #define mtype_expire IPSET_TOKEN(MTYPE, _expire)
230 #define mtype_resize IPSET_TOKEN(MTYPE, _resize)
231 #define mtype_head IPSET_TOKEN(MTYPE, _head)
232 #define mtype_list IPSET_TOKEN(MTYPE, _list)
233 #define mtype_gc IPSET_TOKEN(MTYPE, _gc)
234 #define mtype_gc_init IPSET_TOKEN(MTYPE, _gc_init)
235 #define mtype_variant IPSET_TOKEN(MTYPE, _variant)
236 #define mtype_data_match IPSET_TOKEN(MTYPE, _data_match)
239 #error "MTYPE is not defined!"
243 #error "HOST_MASK is not defined!"
247 #define HKEY_DATALEN sizeof(struct mtype_elem)
250 #define HKEY(data, initval, htable_bits) \
251 (jhash2((u32 *)(data), HKEY_DATALEN / sizeof(u32), initval) \
252 & jhash_mask(htable_bits))
256 #error "HTYPE is not defined!"
260 /* The generic hash structure */
262 struct htable __rcu *table; /* the hash table */
263 u32 maxelem; /* max elements in the hash */
264 u32 elements; /* current element (vs timeout) */
265 u32 initval; /* random jhash init value */
266 #ifdef IP_SET_HASH_WITH_MARKMASK
267 u32 markmask; /* markmask value for mark mask to store */
269 struct timer_list gc; /* garbage collection when timeout enabled */
270 struct mtype_elem next; /* temporary storage for uadd */
271 #ifdef IP_SET_HASH_WITH_MULTI
272 u8 ahash_max; /* max elements in an array block */
274 #ifdef IP_SET_HASH_WITH_NETMASK
275 u8 netmask; /* netmask value for subnets to store */
277 #ifdef IP_SET_HASH_WITH_NETS
278 struct net_prefixes nets[0]; /* book-keeping of prefixes */
283 #ifdef IP_SET_HASH_WITH_NETS
284 /* Network cidr size book keeping when the hash stores different
285 * sized networks. cidr == real cidr + 1 to support /0.
288 mtype_add_cidr(struct htype *h, u8 cidr, u8 nets_length, u8 n)
292 /* Add in increasing prefix order, so larger cidr first */
293 for (i = 0, j = -1; i < nets_length && h->nets[i].cidr[n]; i++) {
296 } else if (h->nets[i].cidr[n] < cidr) {
298 } else if (h->nets[i].cidr[n] == cidr) {
299 h->nets[CIDR_POS(cidr)].nets[n]++;
305 h->nets[i].cidr[n] = h->nets[i - 1].cidr[n];
307 h->nets[i].cidr[n] = cidr;
308 h->nets[CIDR_POS(cidr)].nets[n] = 1;
312 mtype_del_cidr(struct htype *h, u8 cidr, u8 nets_length, u8 n)
314 u8 i, j, net_end = nets_length - 1;
316 for (i = 0; i < nets_length; i++) {
317 if (h->nets[i].cidr[n] != cidr)
319 h->nets[CIDR_POS(cidr)].nets[n]--;
320 if (h->nets[CIDR_POS(cidr)].nets[n] > 0)
322 for (j = i; j < net_end && h->nets[j].cidr[n]; j++)
323 h->nets[j].cidr[n] = h->nets[j + 1].cidr[n];
324 h->nets[j].cidr[n] = 0;
330 /* Calculate the actual memory size of the set data */
332 mtype_ahash_memsize(const struct htype *h, const struct htable *t,
333 u8 nets_length, size_t dsize)
337 size_t memsize = sizeof(*h) + sizeof(*t);
339 #ifdef IP_SET_HASH_WITH_NETS
340 memsize += sizeof(struct net_prefixes) * nets_length;
342 for (i = 0; i < jhash_size(t->htable_bits); i++) {
343 n = rcu_dereference_bh(hbucket(t, i));
346 memsize += sizeof(struct hbucket) + n->size * dsize;
352 /* Get the ith element from the array block n */
353 #define ahash_data(n, i, dsize) \
354 ((struct mtype_elem *)((n)->value + ((i) * (dsize))))
357 mtype_ext_cleanup(struct ip_set *set, struct hbucket *n)
361 for (i = 0; i < n->pos; i++)
362 if (test_bit(i, n->used))
363 ip_set_ext_destroy(set, ahash_data(n, i, set->dsize));
366 /* Flush a hash type of set: destroy all elements */
368 mtype_flush(struct ip_set *set)
370 struct htype *h = set->data;
375 t = ipset_dereference_protected(h->table, set);
376 for (i = 0; i < jhash_size(t->htable_bits); i++) {
377 n = __ipset_dereference_protected(hbucket(t, i), 1);
380 if (set->extensions & IPSET_EXT_DESTROY)
381 mtype_ext_cleanup(set, n);
382 /* FIXME: use slab cache */
383 rcu_assign_pointer(hbucket(t, i), NULL);
386 #ifdef IP_SET_HASH_WITH_NETS
387 memset(h->nets, 0, sizeof(struct net_prefixes) * NLEN(set->family));
392 /* Destroy the hashtable part of the set */
394 mtype_ahash_destroy(struct ip_set *set, struct htable *t, bool ext_destroy)
399 for (i = 0; i < jhash_size(t->htable_bits); i++) {
400 n = __ipset_dereference_protected(hbucket(t, i), 1);
403 if (set->extensions & IPSET_EXT_DESTROY && ext_destroy)
404 mtype_ext_cleanup(set, n);
405 /* FIXME: use slab cache */
412 /* Destroy a hash type of set */
414 mtype_destroy(struct ip_set *set)
416 struct htype *h = set->data;
418 if (SET_WITH_TIMEOUT(set))
419 del_timer_sync(&h->gc);
421 mtype_ahash_destroy(set,
422 __ipset_dereference_protected(h->table, 1), true);
429 mtype_gc_init(struct ip_set *set, void (*gc)(unsigned long ul_set))
431 struct htype *h = set->data;
434 h->gc.data = (unsigned long)set;
436 h->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
438 pr_debug("gc initialized, run in every %u\n",
439 IPSET_GC_PERIOD(set->timeout));
443 mtype_same_set(const struct ip_set *a, const struct ip_set *b)
445 const struct htype *x = a->data;
446 const struct htype *y = b->data;
448 /* Resizing changes htable_bits, so we ignore it */
449 return x->maxelem == y->maxelem &&
450 a->timeout == b->timeout &&
451 #ifdef IP_SET_HASH_WITH_NETMASK
452 x->netmask == y->netmask &&
454 #ifdef IP_SET_HASH_WITH_MARKMASK
455 x->markmask == y->markmask &&
457 a->extensions == b->extensions;
460 /* Delete expired elements from the hashtable */
462 mtype_expire(struct ip_set *set, struct htype *h, u8 nets_length, size_t dsize)
465 struct hbucket *n, *tmp;
466 struct mtype_elem *data;
468 #ifdef IP_SET_HASH_WITH_NETS
472 t = ipset_dereference_protected(h->table, set);
473 for (i = 0; i < jhash_size(t->htable_bits); i++) {
474 n = __ipset_dereference_protected(hbucket(t, i), 1);
477 for (j = 0, d = 0; j < n->pos; j++) {
478 if (!test_bit(j, n->used)) {
482 data = ahash_data(n, j, dsize);
483 if (ip_set_timeout_expired(ext_timeout(data, set))) {
484 pr_debug("expired %u/%u\n", i, j);
485 clear_bit(j, n->used);
486 smp_mb__after_atomic();
487 #ifdef IP_SET_HASH_WITH_NETS
488 for (k = 0; k < IPSET_NET_COUNT; k++)
490 NCIDR_PUT(DCIDR_GET(data->cidr,
494 ip_set_ext_destroy(set, data);
499 if (d >= AHASH_INIT_SIZE) {
501 rcu_assign_pointer(hbucket(t, i), NULL);
505 tmp = kzalloc(sizeof(*tmp) +
506 (n->size - AHASH_INIT_SIZE) * dsize,
509 /* Still try to delete expired elements */
511 tmp->size = n->size - AHASH_INIT_SIZE;
512 for (j = 0, d = 0; j < n->pos; j++) {
513 if (!test_bit(j, n->used))
515 data = ahash_data(n, j, dsize);
516 memcpy(tmp->value + d * dsize, data, dsize);
517 set_bit(d, tmp->used);
521 rcu_assign_pointer(hbucket(t, i), tmp);
528 mtype_gc(unsigned long ul_set)
530 struct ip_set *set = (struct ip_set *)ul_set;
531 struct htype *h = set->data;
533 pr_debug("called\n");
534 spin_lock_bh(&set->lock);
535 mtype_expire(set, h, NLEN(set->family), set->dsize);
536 spin_unlock_bh(&set->lock);
538 h->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
542 /* Resize a hash: create a new hash table with doubling the hashsize
543 * and inserting the elements to it. Repeat until we succeed or
544 * fail due to memory pressures.
547 mtype_resize(struct ip_set *set, bool retried)
549 struct htype *h = set->data;
550 struct htable *t, *orig;
552 size_t dsize = set->dsize;
553 #ifdef IP_SET_HASH_WITH_NETS
555 struct mtype_elem *tmp;
557 struct mtype_elem *data;
558 struct mtype_elem *d;
559 struct hbucket *n, *m;
563 #ifdef IP_SET_HASH_WITH_NETS
564 tmp = kmalloc(dsize, GFP_KERNEL);
569 orig = rcu_dereference_bh_nfnl(h->table);
570 htable_bits = orig->htable_bits;
571 rcu_read_unlock_bh();
577 /* In case we have plenty of memory :-) */
578 pr_warn("Cannot increase the hashsize of set %s further\n",
580 ret = -IPSET_ERR_HASH_FULL;
583 t = ip_set_alloc(htable_size(htable_bits));
588 t->htable_bits = htable_bits;
590 spin_lock_bh(&set->lock);
591 orig = __ipset_dereference_protected(h->table, 1);
592 /* There can't be another parallel resizing, but dumping is possible */
593 atomic_set(&orig->ref, 1);
594 atomic_inc(&orig->uref);
595 pr_debug("attempt to resize set %s from %u to %u, t %p\n",
596 set->name, orig->htable_bits, htable_bits, orig);
597 for (i = 0; i < jhash_size(orig->htable_bits); i++) {
598 n = __ipset_dereference_protected(hbucket(orig, i), 1);
601 for (j = 0; j < n->pos; j++) {
602 if (!test_bit(j, n->used))
604 data = ahash_data(n, j, dsize);
605 #ifdef IP_SET_HASH_WITH_NETS
606 /* We have readers running parallel with us,
607 * so the live data cannot be modified.
610 memcpy(tmp, data, dsize);
612 mtype_data_reset_flags(data, &flags);
614 key = HKEY(data, h->initval, htable_bits);
615 m = __ipset_dereference_protected(hbucket(t, key), 1);
617 m = kzalloc(sizeof(*m) +
618 AHASH_INIT_SIZE * dsize,
624 m->size = AHASH_INIT_SIZE;
625 RCU_INIT_POINTER(hbucket(t, key), m);
626 } else if (m->pos >= m->size) {
629 if (m->size >= AHASH_MAX(h)) {
632 ht = kzalloc(sizeof(*ht) +
633 (m->size + AHASH_INIT_SIZE)
641 memcpy(ht, m, sizeof(struct hbucket) +
643 ht->size = m->size + AHASH_INIT_SIZE;
646 RCU_INIT_POINTER(hbucket(t, key), ht);
648 d = ahash_data(m, m->pos, dsize);
649 memcpy(d, data, dsize);
650 set_bit(m->pos++, m->used);
651 #ifdef IP_SET_HASH_WITH_NETS
652 mtype_data_reset_flags(d, &flags);
656 rcu_assign_pointer(h->table, t);
658 spin_unlock_bh(&set->lock);
660 /* Give time to other readers of the set */
661 synchronize_rcu_bh();
663 pr_debug("set %s resized from %u (%p) to %u (%p)\n", set->name,
664 orig->htable_bits, orig, t->htable_bits, t);
665 /* If there's nobody else dumping the table, destroy it */
666 if (atomic_dec_and_test(&orig->uref)) {
667 pr_debug("Table destroy by resize %p\n", orig);
668 mtype_ahash_destroy(set, orig, false);
672 #ifdef IP_SET_HASH_WITH_NETS
678 atomic_set(&orig->ref, 0);
679 atomic_dec(&orig->uref);
680 spin_unlock_bh(&set->lock);
681 mtype_ahash_destroy(set, t, false);
687 /* Add an element to a hash and update the internal counters when succeeded,
688 * otherwise report the proper error code.
691 mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
692 struct ip_set_ext *mext, u32 flags)
694 struct htype *h = set->data;
696 const struct mtype_elem *d = value;
697 struct mtype_elem *data;
698 struct hbucket *n, *old = ERR_PTR(-ENOENT);
700 bool flag_exist = flags & IPSET_FLAG_EXIST;
701 bool deleted = false, forceadd = false, reuse = false;
704 if (h->elements >= h->maxelem) {
705 if (SET_WITH_TIMEOUT(set))
706 /* FIXME: when set is full, we slow down here */
707 mtype_expire(set, h, NLEN(set->family), set->dsize);
708 if (h->elements >= h->maxelem && SET_WITH_FORCEADD(set))
712 t = ipset_dereference_protected(h->table, set);
713 key = HKEY(value, h->initval, t->htable_bits);
714 n = __ipset_dereference_protected(hbucket(t, key), 1);
718 pr_warn("Set %s is full, maxelem %u reached\n",
719 set->name, h->maxelem);
720 return -IPSET_ERR_HASH_FULL;
721 } else if (h->elements >= h->maxelem) {
725 n = kzalloc(sizeof(*n) + AHASH_INIT_SIZE * set->dsize,
729 n->size = AHASH_INIT_SIZE;
732 for (i = 0; i < n->pos; i++) {
733 if (!test_bit(i, n->used)) {
734 /* Reuse first deleted entry */
736 deleted = reuse = true;
741 data = ahash_data(n, i, set->dsize);
742 if (mtype_data_equal(data, d, &multi)) {
744 (SET_WITH_TIMEOUT(set) &&
745 ip_set_timeout_expired(ext_timeout(data, set)))) {
746 /* Just the extensions could be overwritten */
748 goto overwrite_extensions;
750 return -IPSET_ERR_EXIST;
752 /* Reuse first timed out entry */
753 if (SET_WITH_TIMEOUT(set) &&
754 ip_set_timeout_expired(ext_timeout(data, set)) &&
760 if (reuse || forceadd) {
761 data = ahash_data(n, j, set->dsize);
763 #ifdef IP_SET_HASH_WITH_NETS
764 for (i = 0; i < IPSET_NET_COUNT; i++)
766 NCIDR_PUT(DCIDR_GET(data->cidr, i)),
767 NLEN(set->family), i);
769 ip_set_ext_destroy(set, data);
774 if (h->elements >= h->maxelem)
776 /* Create a new slot */
777 if (n->pos >= n->size) {
778 TUNE_AHASH_MAX(h, multi);
779 if (n->size >= AHASH_MAX(h)) {
780 /* Trigger rehashing */
781 mtype_data_next(&h->next, d);
785 n = kzalloc(sizeof(*n) +
786 (old->size + AHASH_INIT_SIZE) * set->dsize,
790 memcpy(n, old, sizeof(struct hbucket) +
791 old->size * set->dsize);
792 n->size = old->size + AHASH_INIT_SIZE;
797 data = ahash_data(n, j, set->dsize);
800 #ifdef IP_SET_HASH_WITH_NETS
801 for (i = 0; i < IPSET_NET_COUNT; i++)
802 mtype_add_cidr(h, NCIDR_PUT(DCIDR_GET(d->cidr, i)),
803 NLEN(set->family), i);
805 memcpy(data, d, sizeof(struct mtype_elem));
806 overwrite_extensions:
807 #ifdef IP_SET_HASH_WITH_NETS
808 mtype_data_set_flags(data, flags);
810 if (SET_WITH_COUNTER(set))
811 ip_set_init_counter(ext_counter(data, set), ext);
812 if (SET_WITH_COMMENT(set))
813 ip_set_init_comment(ext_comment(data, set), ext);
814 if (SET_WITH_SKBINFO(set))
815 ip_set_init_skbinfo(ext_skbinfo(data, set), ext);
816 /* Must come last for the case when timed out entry is reused */
817 if (SET_WITH_TIMEOUT(set))
818 ip_set_timeout_set(ext_timeout(data, set), ext->timeout);
819 smp_mb__before_atomic();
821 if (old != ERR_PTR(-ENOENT)) {
822 rcu_assign_pointer(hbucket(t, key), n);
830 pr_warn("Set %s is full, maxelem %u reached\n",
831 set->name, h->maxelem);
832 return -IPSET_ERR_HASH_FULL;
835 /* Delete an element from the hash and free up space if possible.
838 mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
839 struct ip_set_ext *mext, u32 flags)
841 struct htype *h = set->data;
843 const struct mtype_elem *d = value;
844 struct mtype_elem *data;
846 int i, j, k, ret = -IPSET_ERR_EXIST;
848 size_t dsize = set->dsize;
850 t = ipset_dereference_protected(h->table, set);
851 key = HKEY(value, h->initval, t->htable_bits);
852 n = __ipset_dereference_protected(hbucket(t, key), 1);
855 for (i = 0, k = 0; i < n->pos; i++) {
856 if (!test_bit(i, n->used)) {
860 data = ahash_data(n, i, dsize);
861 if (!mtype_data_equal(data, d, &multi))
863 if (SET_WITH_TIMEOUT(set) &&
864 ip_set_timeout_expired(ext_timeout(data, set)))
868 clear_bit(i, n->used);
869 smp_mb__after_atomic();
873 #ifdef IP_SET_HASH_WITH_NETS
874 for (j = 0; j < IPSET_NET_COUNT; j++)
875 mtype_del_cidr(h, NCIDR_PUT(DCIDR_GET(d->cidr, j)),
876 NLEN(set->family), j);
878 ip_set_ext_destroy(set, data);
880 for (; i < n->pos; i++) {
881 if (!test_bit(i, n->used))
884 if (n->pos == 0 && k == 0) {
885 rcu_assign_pointer(hbucket(t, key), NULL);
887 } else if (k >= AHASH_INIT_SIZE) {
888 struct hbucket *tmp = kzalloc(sizeof(*tmp) +
889 (n->size - AHASH_INIT_SIZE) * dsize,
893 tmp->size = n->size - AHASH_INIT_SIZE;
894 for (j = 0, k = 0; j < n->pos; j++) {
895 if (!test_bit(j, n->used))
897 data = ahash_data(n, j, dsize);
898 memcpy(tmp->value + k * dsize, data, dsize);
899 set_bit(j, tmp->used);
903 rcu_assign_pointer(hbucket(t, key), tmp);
914 mtype_data_match(struct mtype_elem *data, const struct ip_set_ext *ext,
915 struct ip_set_ext *mext, struct ip_set *set, u32 flags)
917 if (SET_WITH_COUNTER(set))
918 ip_set_update_counter(ext_counter(data, set),
920 if (SET_WITH_SKBINFO(set))
921 ip_set_get_skbinfo(ext_skbinfo(data, set),
923 return mtype_do_data_match(data);
926 #ifdef IP_SET_HASH_WITH_NETS
927 /* Special test function which takes into account the different network
928 * sizes added to the set
931 mtype_test_cidrs(struct ip_set *set, struct mtype_elem *d,
932 const struct ip_set_ext *ext,
933 struct ip_set_ext *mext, u32 flags)
935 struct htype *h = set->data;
936 struct htable *t = rcu_dereference_bh(h->table);
938 struct mtype_elem *data;
939 #if IPSET_NET_COUNT == 2
940 struct mtype_elem orig = *d;
946 u8 nets_length = NLEN(set->family);
948 pr_debug("test by nets\n");
949 for (; j < nets_length && h->nets[j].cidr[0] && !multi; j++) {
950 #if IPSET_NET_COUNT == 2
951 mtype_data_reset_elem(d, &orig);
952 mtype_data_netmask(d, NCIDR_GET(h->nets[j].cidr[0]), false);
953 for (k = 0; k < nets_length && h->nets[k].cidr[1] && !multi;
955 mtype_data_netmask(d, NCIDR_GET(h->nets[k].cidr[1]),
958 mtype_data_netmask(d, NCIDR_GET(h->nets[j].cidr[0]));
960 key = HKEY(d, h->initval, t->htable_bits);
961 n = rcu_dereference_bh(hbucket(t, key));
964 for (i = 0; i < n->pos; i++) {
965 if (!test_bit(i, n->used))
967 data = ahash_data(n, i, set->dsize);
968 if (!mtype_data_equal(data, d, &multi))
970 if (SET_WITH_TIMEOUT(set)) {
971 if (!ip_set_timeout_expired(
972 ext_timeout(data, set)))
973 return mtype_data_match(data, ext,
976 #ifdef IP_SET_HASH_WITH_MULTI
980 return mtype_data_match(data, ext,
983 #if IPSET_NET_COUNT == 2
991 /* Test whether the element is added to the set */
993 mtype_test(struct ip_set *set, void *value, const struct ip_set_ext *ext,
994 struct ip_set_ext *mext, u32 flags)
996 struct htype *h = set->data;
998 struct mtype_elem *d = value;
1000 struct mtype_elem *data;
1004 t = rcu_dereference_bh(h->table);
1005 #ifdef IP_SET_HASH_WITH_NETS
1006 /* If we test an IP address and not a network address,
1007 * try all possible network sizes
1009 for (i = 0; i < IPSET_NET_COUNT; i++)
1010 if (DCIDR_GET(d->cidr, i) != SET_HOST_MASK(set->family))
1012 if (i == IPSET_NET_COUNT) {
1013 ret = mtype_test_cidrs(set, d, ext, mext, flags);
1018 key = HKEY(d, h->initval, t->htable_bits);
1019 n = rcu_dereference_bh(hbucket(t, key));
1024 for (i = 0; i < n->pos; i++) {
1025 if (!test_bit(i, n->used))
1027 data = ahash_data(n, i, set->dsize);
1028 if (mtype_data_equal(data, d, &multi) &&
1029 !(SET_WITH_TIMEOUT(set) &&
1030 ip_set_timeout_expired(ext_timeout(data, set)))) {
1031 ret = mtype_data_match(data, ext, mext, set, flags);
1039 /* Reply a HEADER request: fill out the header part of the set */
1041 mtype_head(struct ip_set *set, struct sk_buff *skb)
1043 const struct htype *h = set->data;
1044 const struct htable *t;
1045 struct nlattr *nested;
1050 t = rcu_dereference_bh_nfnl(h->table);
1051 memsize = mtype_ahash_memsize(h, t, NLEN(set->family), set->dsize);
1052 htable_bits = t->htable_bits;
1053 rcu_read_unlock_bh();
1055 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
1057 goto nla_put_failure;
1058 if (nla_put_net32(skb, IPSET_ATTR_HASHSIZE,
1059 htonl(jhash_size(htable_bits))) ||
1060 nla_put_net32(skb, IPSET_ATTR_MAXELEM, htonl(h->maxelem)))
1061 goto nla_put_failure;
1062 #ifdef IP_SET_HASH_WITH_NETMASK
1063 if (h->netmask != HOST_MASK &&
1064 nla_put_u8(skb, IPSET_ATTR_NETMASK, h->netmask))
1065 goto nla_put_failure;
1067 #ifdef IP_SET_HASH_WITH_MARKMASK
1068 if (nla_put_u32(skb, IPSET_ATTR_MARKMASK, h->markmask))
1069 goto nla_put_failure;
1071 if (nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref)) ||
1072 nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)))
1073 goto nla_put_failure;
1074 if (unlikely(ip_set_put_flags(skb, set)))
1075 goto nla_put_failure;
1076 ipset_nest_end(skb, nested);
1083 /* Make possible to run dumping parallel with resizing */
1085 mtype_uref(struct ip_set *set, struct netlink_callback *cb, bool start)
1087 struct htype *h = set->data;
1092 t = rcu_dereference_bh_nfnl(h->table);
1093 atomic_inc(&t->uref);
1094 cb->args[IPSET_CB_PRIVATE] = (unsigned long)t;
1095 rcu_read_unlock_bh();
1096 } else if (cb->args[IPSET_CB_PRIVATE]) {
1097 t = (struct htable *)cb->args[IPSET_CB_PRIVATE];
1098 if (atomic_dec_and_test(&t->uref) && atomic_read(&t->ref)) {
1099 /* Resizing didn't destroy the hash table */
1100 pr_debug("Table destroy by dump: %p\n", t);
1101 mtype_ahash_destroy(set, t, false);
1103 cb->args[IPSET_CB_PRIVATE] = 0;
1107 /* Reply a LIST/SAVE request: dump the elements of the specified set */
1109 mtype_list(const struct ip_set *set,
1110 struct sk_buff *skb, struct netlink_callback *cb)
1112 const struct htable *t;
1113 struct nlattr *atd, *nested;
1114 const struct hbucket *n;
1115 const struct mtype_elem *e;
1116 u32 first = cb->args[IPSET_CB_ARG0];
1117 /* We assume that one hash bucket fills into one page */
1121 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
1125 pr_debug("list hash set %s\n", set->name);
1126 t = (const struct htable *)cb->args[IPSET_CB_PRIVATE];
1127 /* Expire may replace a hbucket with another one */
1129 for (; cb->args[IPSET_CB_ARG0] < jhash_size(t->htable_bits);
1130 cb->args[IPSET_CB_ARG0]++) {
1131 incomplete = skb_tail_pointer(skb);
1132 n = rcu_dereference(hbucket(t, cb->args[IPSET_CB_ARG0]));
1133 pr_debug("cb->arg bucket: %lu, t %p n %p\n",
1134 cb->args[IPSET_CB_ARG0], t, n);
1137 for (i = 0; i < n->pos; i++) {
1138 if (!test_bit(i, n->used))
1140 e = ahash_data(n, i, set->dsize);
1141 if (SET_WITH_TIMEOUT(set) &&
1142 ip_set_timeout_expired(ext_timeout(e, set)))
1144 pr_debug("list hash %lu hbucket %p i %u, data %p\n",
1145 cb->args[IPSET_CB_ARG0], n, i, e);
1146 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
1148 if (cb->args[IPSET_CB_ARG0] == first) {
1149 nla_nest_cancel(skb, atd);
1153 goto nla_put_failure;
1155 if (mtype_data_list(skb, e))
1156 goto nla_put_failure;
1157 if (ip_set_put_extensions(skb, set, e, true))
1158 goto nla_put_failure;
1159 ipset_nest_end(skb, nested);
1162 ipset_nest_end(skb, atd);
1163 /* Set listing finished */
1164 cb->args[IPSET_CB_ARG0] = 0;
1169 nlmsg_trim(skb, incomplete);
1170 if (unlikely(first == cb->args[IPSET_CB_ARG0])) {
1171 pr_warn("Can't list set %s: one bucket does not fit into a message. Please report it!\n",
1173 cb->args[IPSET_CB_ARG0] = 0;
1176 ipset_nest_end(skb, atd);
1184 IPSET_TOKEN(MTYPE, _kadt)(struct ip_set *set, const struct sk_buff *skb,
1185 const struct xt_action_param *par,
1186 enum ipset_adt adt, struct ip_set_adt_opt *opt);
1189 IPSET_TOKEN(MTYPE, _uadt)(struct ip_set *set, struct nlattr *tb[],
1190 enum ipset_adt adt, u32 *lineno, u32 flags,
1193 static const struct ip_set_type_variant mtype_variant = {
1197 [IPSET_ADD] = mtype_add,
1198 [IPSET_DEL] = mtype_del,
1199 [IPSET_TEST] = mtype_test,
1201 .destroy = mtype_destroy,
1202 .flush = mtype_flush,
1206 .resize = mtype_resize,
1207 .same_set = mtype_same_set,
1210 #ifdef IP_SET_EMIT_CREATE
1212 IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
1213 struct nlattr *tb[], u32 flags)
1215 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
1216 #ifdef IP_SET_HASH_WITH_MARKMASK
1220 #ifdef IP_SET_HASH_WITH_NETMASK
1227 #ifndef IP_SET_PROTO_UNDEF
1228 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
1229 return -IPSET_ERR_INVALID_FAMILY;
1232 #ifdef IP_SET_HASH_WITH_MARKMASK
1233 markmask = 0xffffffff;
1235 #ifdef IP_SET_HASH_WITH_NETMASK
1236 netmask = set->family == NFPROTO_IPV4 ? 32 : 128;
1237 pr_debug("Create set %s with family %s\n",
1238 set->name, set->family == NFPROTO_IPV4 ? "inet" : "inet6");
1241 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
1242 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
1243 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
1244 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
1245 return -IPSET_ERR_PROTOCOL;
1246 #ifdef IP_SET_HASH_WITH_MARKMASK
1247 /* Separated condition in order to avoid directive in argument list */
1248 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_MARKMASK)))
1249 return -IPSET_ERR_PROTOCOL;
1252 if (tb[IPSET_ATTR_HASHSIZE]) {
1253 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
1254 if (hashsize < IPSET_MIMINAL_HASHSIZE)
1255 hashsize = IPSET_MIMINAL_HASHSIZE;
1258 if (tb[IPSET_ATTR_MAXELEM])
1259 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
1261 #ifdef IP_SET_HASH_WITH_NETMASK
1262 if (tb[IPSET_ATTR_NETMASK]) {
1263 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
1265 if ((set->family == NFPROTO_IPV4 && netmask > 32) ||
1266 (set->family == NFPROTO_IPV6 && netmask > 128) ||
1268 return -IPSET_ERR_INVALID_NETMASK;
1271 #ifdef IP_SET_HASH_WITH_MARKMASK
1272 if (tb[IPSET_ATTR_MARKMASK]) {
1273 markmask = ntohl(nla_get_be32(tb[IPSET_ATTR_MARKMASK]));
1276 return -IPSET_ERR_INVALID_MARKMASK;
1281 #ifdef IP_SET_HASH_WITH_NETS
1282 hsize += sizeof(struct net_prefixes) * NLEN(set->family);
1284 h = kzalloc(hsize, GFP_KERNEL);
1288 h->maxelem = maxelem;
1289 #ifdef IP_SET_HASH_WITH_NETMASK
1290 h->netmask = netmask;
1292 #ifdef IP_SET_HASH_WITH_MARKMASK
1293 h->markmask = markmask;
1295 get_random_bytes(&h->initval, sizeof(h->initval));
1296 set->timeout = IPSET_NO_TIMEOUT;
1298 /* Compute htable_bits from the user input parameter hashsize.
1299 * Assume that hashsize == 2^htable_bits,
1300 * otherwise round up to the first 2^n value.
1302 hbits = fls(hashsize - 1);
1303 hsize = htable_size(hbits);
1308 t = ip_set_alloc(hsize);
1313 t->htable_bits = hbits;
1314 rcu_assign_pointer(h->table, t);
1317 #ifndef IP_SET_PROTO_UNDEF
1318 if (set->family == NFPROTO_IPV4) {
1320 set->variant = &IPSET_TOKEN(HTYPE, 4_variant);
1321 set->dsize = ip_set_elem_len(set, tb,
1322 sizeof(struct IPSET_TOKEN(HTYPE, 4_elem)),
1323 __alignof__(struct IPSET_TOKEN(HTYPE, 4_elem)));
1324 #ifndef IP_SET_PROTO_UNDEF
1326 set->variant = &IPSET_TOKEN(HTYPE, 6_variant);
1327 set->dsize = ip_set_elem_len(set, tb,
1328 sizeof(struct IPSET_TOKEN(HTYPE, 6_elem)),
1329 __alignof__(struct IPSET_TOKEN(HTYPE, 6_elem)));
1332 if (tb[IPSET_ATTR_TIMEOUT]) {
1333 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
1334 #ifndef IP_SET_PROTO_UNDEF
1335 if (set->family == NFPROTO_IPV4)
1337 IPSET_TOKEN(HTYPE, 4_gc_init)(set,
1338 IPSET_TOKEN(HTYPE, 4_gc));
1339 #ifndef IP_SET_PROTO_UNDEF
1341 IPSET_TOKEN(HTYPE, 6_gc_init)(set,
1342 IPSET_TOKEN(HTYPE, 6_gc));
1345 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
1346 set->name, jhash_size(t->htable_bits),
1347 t->htable_bits, h->maxelem, set->data, t);
1351 #endif /* IP_SET_EMIT_CREATE */