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 size_t */
108 hsize
= jhash_size(hbits
);
109 if ((((size_t)-1) - sizeof(struct htable
)) / sizeof(struct hbucket
*)
113 return hsize
* sizeof(struct hbucket
*) + sizeof(struct htable
);
116 /* Compute htable_bits from the user input parameter hashsize */
118 htable_bits(u32 hashsize
)
120 /* Assume that hashsize == 2^htable_bits */
121 u8 bits
= fls(hashsize
- 1);
123 if (jhash_size(bits
) != hashsize
)
124 /* Round up to the first 2^n value */
125 bits
= fls(hashsize
);
130 #ifdef IP_SET_HASH_WITH_NETS
131 #if IPSET_NET_COUNT > 1
132 #define __CIDR(cidr, i) (cidr[i])
134 #define __CIDR(cidr, i) (cidr)
137 /* cidr + 1 is stored in net_prefixes to support /0 */
138 #define NCIDR_PUT(cidr) ((cidr) + 1)
139 #define NCIDR_GET(cidr) ((cidr) - 1)
141 #ifdef IP_SET_HASH_WITH_NETS_PACKED
142 /* When cidr is packed with nomatch, cidr - 1 is stored in the data entry */
143 #define DCIDR_PUT(cidr) ((cidr) - 1)
144 #define DCIDR_GET(cidr, i) (__CIDR(cidr, i) + 1)
146 #define DCIDR_PUT(cidr) (cidr)
147 #define DCIDR_GET(cidr, i) __CIDR(cidr, i)
150 #define INIT_CIDR(cidr, host_mask) \
151 DCIDR_PUT(((cidr) ? NCIDR_GET(cidr) : host_mask))
153 #define SET_HOST_MASK(family) (family == AF_INET ? 32 : 128)
155 #ifdef IP_SET_HASH_WITH_NET0
156 /* cidr from 0 to SET_HOST_MASK() value and c = cidr + 1 */
157 #define NLEN(family) (SET_HOST_MASK(family) + 1)
158 #define CIDR_POS(c) ((c) - 1)
160 /* cidr from 1 to SET_HOST_MASK() value and c = cidr + 1 */
161 #define NLEN(family) SET_HOST_MASK(family)
162 #define CIDR_POS(c) ((c) - 2)
166 #define NLEN(family) 0
167 #endif /* IP_SET_HASH_WITH_NETS */
169 #endif /* _IP_SET_HASH_GEN_H */
171 /* Family dependent templates */
174 #undef mtype_data_equal
175 #undef mtype_do_data_match
176 #undef mtype_data_set_flags
177 #undef mtype_data_reset_elem
178 #undef mtype_data_reset_flags
179 #undef mtype_data_netmask
180 #undef mtype_data_list
181 #undef mtype_data_next
184 #undef mtype_ahash_destroy
185 #undef mtype_ext_cleanup
186 #undef mtype_add_cidr
187 #undef mtype_del_cidr
188 #undef mtype_ahash_memsize
191 #undef mtype_same_set
198 #undef mtype_test_cidrs
208 #undef mtype_data_match
212 #define mtype_data_equal IPSET_TOKEN(MTYPE, _data_equal)
213 #ifdef IP_SET_HASH_WITH_NETS
214 #define mtype_do_data_match IPSET_TOKEN(MTYPE, _do_data_match)
216 #define mtype_do_data_match(d) 1
218 #define mtype_data_set_flags IPSET_TOKEN(MTYPE, _data_set_flags)
219 #define mtype_data_reset_elem IPSET_TOKEN(MTYPE, _data_reset_elem)
220 #define mtype_data_reset_flags IPSET_TOKEN(MTYPE, _data_reset_flags)
221 #define mtype_data_netmask IPSET_TOKEN(MTYPE, _data_netmask)
222 #define mtype_data_list IPSET_TOKEN(MTYPE, _data_list)
223 #define mtype_data_next IPSET_TOKEN(MTYPE, _data_next)
224 #define mtype_elem IPSET_TOKEN(MTYPE, _elem)
226 #define mtype_ahash_destroy IPSET_TOKEN(MTYPE, _ahash_destroy)
227 #define mtype_ext_cleanup IPSET_TOKEN(MTYPE, _ext_cleanup)
228 #define mtype_add_cidr IPSET_TOKEN(MTYPE, _add_cidr)
229 #define mtype_del_cidr IPSET_TOKEN(MTYPE, _del_cidr)
230 #define mtype_ahash_memsize IPSET_TOKEN(MTYPE, _ahash_memsize)
231 #define mtype_flush IPSET_TOKEN(MTYPE, _flush)
232 #define mtype_destroy IPSET_TOKEN(MTYPE, _destroy)
233 #define mtype_same_set IPSET_TOKEN(MTYPE, _same_set)
234 #define mtype_kadt IPSET_TOKEN(MTYPE, _kadt)
235 #define mtype_uadt IPSET_TOKEN(MTYPE, _uadt)
238 #define mtype_add IPSET_TOKEN(MTYPE, _add)
239 #define mtype_del IPSET_TOKEN(MTYPE, _del)
240 #define mtype_test_cidrs IPSET_TOKEN(MTYPE, _test_cidrs)
241 #define mtype_test IPSET_TOKEN(MTYPE, _test)
242 #define mtype_uref IPSET_TOKEN(MTYPE, _uref)
243 #define mtype_expire IPSET_TOKEN(MTYPE, _expire)
244 #define mtype_resize IPSET_TOKEN(MTYPE, _resize)
245 #define mtype_head IPSET_TOKEN(MTYPE, _head)
246 #define mtype_list IPSET_TOKEN(MTYPE, _list)
247 #define mtype_gc IPSET_TOKEN(MTYPE, _gc)
248 #define mtype_gc_init IPSET_TOKEN(MTYPE, _gc_init)
249 #define mtype_variant IPSET_TOKEN(MTYPE, _variant)
250 #define mtype_data_match IPSET_TOKEN(MTYPE, _data_match)
253 #error "MTYPE is not defined!"
257 #error "HOST_MASK is not defined!"
261 #define HKEY_DATALEN sizeof(struct mtype_elem)
264 #define HKEY(data, initval, htable_bits) \
265 (jhash2((u32 *)(data), HKEY_DATALEN / sizeof(u32), initval) \
266 & jhash_mask(htable_bits))
270 #error "HTYPE is not defined!"
274 /* The generic hash structure */
276 struct htable __rcu
*table
; /* the hash table */
277 u32 maxelem
; /* max elements in the hash */
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
)
478 struct hbucket
*n
, *tmp
;
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
) {
514 rcu_assign_pointer(hbucket(t
, i
), NULL
);
518 tmp
= kzalloc(sizeof(*tmp
) +
519 (n
->size
- AHASH_INIT_SIZE
) * dsize
,
522 /* Still try to delete expired elements */
524 tmp
->size
= n
->size
- AHASH_INIT_SIZE
;
525 for (j
= 0, d
= 0; j
< n
->pos
; j
++) {
526 if (!test_bit(j
, n
->used
))
528 data
= ahash_data(n
, j
, dsize
);
529 memcpy(tmp
->value
+ d
* dsize
, data
, dsize
);
530 set_bit(d
, tmp
->used
);
534 rcu_assign_pointer(hbucket(t
, i
), tmp
);
541 mtype_gc(unsigned long ul_set
)
543 struct ip_set
*set
= (struct ip_set
*)ul_set
;
544 struct htype
*h
= set
->data
;
546 pr_debug("called\n");
547 spin_lock_bh(&set
->lock
);
548 mtype_expire(set
, h
, NLEN(set
->family
), set
->dsize
);
549 spin_unlock_bh(&set
->lock
);
551 h
->gc
.expires
= jiffies
+ IPSET_GC_PERIOD(set
->timeout
) * HZ
;
555 /* Resize a hash: create a new hash table with doubling the hashsize
556 * and inserting the elements to it. Repeat until we succeed or
557 * 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
;
565 size_t dsize
= set
->dsize
;
566 #ifdef IP_SET_HASH_WITH_NETS
568 struct mtype_elem
*tmp
;
570 struct mtype_elem
*data
;
571 struct mtype_elem
*d
;
572 struct hbucket
*n
, *m
;
576 #ifdef IP_SET_HASH_WITH_NETS
577 tmp
= kmalloc(dsize
, GFP_KERNEL
);
582 orig
= rcu_dereference_bh_nfnl(h
->table
);
583 htable_bits
= orig
->htable_bits
;
584 rcu_read_unlock_bh();
590 /* In case we have plenty of memory :-) */
591 pr_warn("Cannot increase the hashsize of set %s further\n",
593 ret
= -IPSET_ERR_HASH_FULL
;
596 t
= ip_set_alloc(htable_size(htable_bits
));
601 t
->htable_bits
= htable_bits
;
603 spin_lock_bh(&set
->lock
);
604 orig
= __ipset_dereference_protected(h
->table
, 1);
605 /* There can't be another parallel resizing, but dumping is possible */
606 atomic_set(&orig
->ref
, 1);
607 atomic_inc(&orig
->uref
);
608 pr_debug("attempt to resize set %s from %u to %u, t %p\n",
609 set
->name
, orig
->htable_bits
, htable_bits
, orig
);
610 for (i
= 0; i
< jhash_size(orig
->htable_bits
); i
++) {
611 n
= __ipset_dereference_protected(hbucket(orig
, i
), 1);
614 for (j
= 0; j
< n
->pos
; j
++) {
615 if (!test_bit(j
, n
->used
))
617 data
= ahash_data(n
, j
, dsize
);
618 #ifdef IP_SET_HASH_WITH_NETS
619 /* We have readers running parallel with us,
620 * so the live data cannot be modified.
623 memcpy(tmp
, data
, dsize
);
625 mtype_data_reset_flags(data
, &flags
);
627 key
= HKEY(data
, h
->initval
, htable_bits
);
628 m
= __ipset_dereference_protected(hbucket(t
, key
), 1);
630 m
= kzalloc(sizeof(*m
) +
631 AHASH_INIT_SIZE
* dsize
,
637 m
->size
= AHASH_INIT_SIZE
;
638 RCU_INIT_POINTER(hbucket(t
, key
), m
);
639 } else if (m
->pos
>= m
->size
) {
642 if (m
->size
>= AHASH_MAX(h
)) {
645 ht
= kzalloc(sizeof(*ht
) +
646 (m
->size
+ AHASH_INIT_SIZE
)
654 memcpy(ht
, m
, sizeof(struct hbucket
) +
656 ht
->size
= m
->size
+ AHASH_INIT_SIZE
;
659 RCU_INIT_POINTER(hbucket(t
, key
), ht
);
661 d
= ahash_data(m
, m
->pos
, dsize
);
662 memcpy(d
, data
, dsize
);
663 set_bit(m
->pos
++, m
->used
);
664 #ifdef IP_SET_HASH_WITH_NETS
665 mtype_data_reset_flags(d
, &flags
);
669 rcu_assign_pointer(h
->table
, t
);
671 spin_unlock_bh(&set
->lock
);
673 /* Give time to other readers of the set */
674 synchronize_rcu_bh();
676 pr_debug("set %s resized from %u (%p) to %u (%p)\n", set
->name
,
677 orig
->htable_bits
, orig
, t
->htable_bits
, t
);
678 /* If there's nobody else dumping the table, destroy it */
679 if (atomic_dec_and_test(&orig
->uref
)) {
680 pr_debug("Table destroy by resize %p\n", orig
);
681 mtype_ahash_destroy(set
, orig
, false);
685 #ifdef IP_SET_HASH_WITH_NETS
691 atomic_set(&orig
->ref
, 0);
692 atomic_dec(&orig
->uref
);
693 spin_unlock_bh(&set
->lock
);
694 mtype_ahash_destroy(set
, t
, false);
700 /* Add an element to a hash and update the internal counters when succeeded,
701 * otherwise report the proper error code.
704 mtype_add(struct ip_set
*set
, void *value
, const struct ip_set_ext
*ext
,
705 struct ip_set_ext
*mext
, u32 flags
)
707 struct htype
*h
= set
->data
;
709 const struct mtype_elem
*d
= value
;
710 struct mtype_elem
*data
;
711 struct hbucket
*n
, *old
= ERR_PTR(-ENOENT
);
713 bool flag_exist
= flags
& IPSET_FLAG_EXIST
;
714 bool deleted
= false, forceadd
= false, reuse
= false;
717 if (set
->elements
>= h
->maxelem
) {
718 if (SET_WITH_TIMEOUT(set
))
719 /* FIXME: when set is full, we slow down here */
720 mtype_expire(set
, h
, NLEN(set
->family
), set
->dsize
);
721 if (set
->elements
>= h
->maxelem
&& SET_WITH_FORCEADD(set
))
725 t
= ipset_dereference_protected(h
->table
, set
);
726 key
= HKEY(value
, h
->initval
, t
->htable_bits
);
727 n
= __ipset_dereference_protected(hbucket(t
, key
), 1);
731 pr_warn("Set %s is full, maxelem %u reached\n",
732 set
->name
, h
->maxelem
);
733 return -IPSET_ERR_HASH_FULL
;
734 } else if (set
->elements
>= h
->maxelem
) {
738 n
= kzalloc(sizeof(*n
) + AHASH_INIT_SIZE
* set
->dsize
,
742 n
->size
= AHASH_INIT_SIZE
;
745 for (i
= 0; i
< n
->pos
; i
++) {
746 if (!test_bit(i
, n
->used
)) {
747 /* Reuse first deleted entry */
749 deleted
= reuse
= true;
754 data
= ahash_data(n
, i
, set
->dsize
);
755 if (mtype_data_equal(data
, d
, &multi
)) {
757 (SET_WITH_TIMEOUT(set
) &&
758 ip_set_timeout_expired(ext_timeout(data
, set
)))) {
759 /* Just the extensions could be overwritten */
761 goto overwrite_extensions
;
763 return -IPSET_ERR_EXIST
;
765 /* Reuse first timed out entry */
766 if (SET_WITH_TIMEOUT(set
) &&
767 ip_set_timeout_expired(ext_timeout(data
, set
)) &&
773 if (reuse
|| forceadd
) {
774 data
= ahash_data(n
, j
, set
->dsize
);
776 #ifdef IP_SET_HASH_WITH_NETS
777 for (i
= 0; i
< IPSET_NET_COUNT
; i
++)
779 NCIDR_PUT(DCIDR_GET(data
->cidr
, i
)),
780 NLEN(set
->family
), i
);
782 ip_set_ext_destroy(set
, data
);
787 if (set
->elements
>= h
->maxelem
)
789 /* Create a new slot */
790 if (n
->pos
>= n
->size
) {
791 TUNE_AHASH_MAX(h
, multi
);
792 if (n
->size
>= AHASH_MAX(h
)) {
793 /* Trigger rehashing */
794 mtype_data_next(&h
->next
, d
);
798 n
= kzalloc(sizeof(*n
) +
799 (old
->size
+ AHASH_INIT_SIZE
) * set
->dsize
,
803 memcpy(n
, old
, sizeof(struct hbucket
) +
804 old
->size
* set
->dsize
);
805 n
->size
= old
->size
+ AHASH_INIT_SIZE
;
810 data
= ahash_data(n
, j
, set
->dsize
);
813 #ifdef IP_SET_HASH_WITH_NETS
814 for (i
= 0; i
< IPSET_NET_COUNT
; i
++)
815 mtype_add_cidr(h
, NCIDR_PUT(DCIDR_GET(d
->cidr
, i
)),
816 NLEN(set
->family
), i
);
818 memcpy(data
, d
, sizeof(struct mtype_elem
));
819 overwrite_extensions
:
820 #ifdef IP_SET_HASH_WITH_NETS
821 mtype_data_set_flags(data
, flags
);
823 if (SET_WITH_COUNTER(set
))
824 ip_set_init_counter(ext_counter(data
, set
), ext
);
825 if (SET_WITH_COMMENT(set
))
826 ip_set_init_comment(ext_comment(data
, set
), ext
);
827 if (SET_WITH_SKBINFO(set
))
828 ip_set_init_skbinfo(ext_skbinfo(data
, set
), ext
);
829 /* Must come last for the case when timed out entry is reused */
830 if (SET_WITH_TIMEOUT(set
))
831 ip_set_timeout_set(ext_timeout(data
, set
), ext
->timeout
);
832 smp_mb__before_atomic();
834 if (old
!= ERR_PTR(-ENOENT
)) {
835 rcu_assign_pointer(hbucket(t
, key
), n
);
843 pr_warn("Set %s is full, maxelem %u reached\n",
844 set
->name
, h
->maxelem
);
845 return -IPSET_ERR_HASH_FULL
;
848 /* Delete an element from the hash and free up space if possible.
851 mtype_del(struct ip_set
*set
, void *value
, const struct ip_set_ext
*ext
,
852 struct ip_set_ext
*mext
, u32 flags
)
854 struct htype
*h
= set
->data
;
856 const struct mtype_elem
*d
= value
;
857 struct mtype_elem
*data
;
859 int i
, j
, k
, ret
= -IPSET_ERR_EXIST
;
861 size_t dsize
= set
->dsize
;
863 t
= ipset_dereference_protected(h
->table
, set
);
864 key
= HKEY(value
, h
->initval
, t
->htable_bits
);
865 n
= __ipset_dereference_protected(hbucket(t
, key
), 1);
868 for (i
= 0, k
= 0; i
< n
->pos
; i
++) {
869 if (!test_bit(i
, n
->used
)) {
873 data
= ahash_data(n
, i
, dsize
);
874 if (!mtype_data_equal(data
, d
, &multi
))
876 if (SET_WITH_TIMEOUT(set
) &&
877 ip_set_timeout_expired(ext_timeout(data
, set
)))
881 clear_bit(i
, n
->used
);
882 smp_mb__after_atomic();
886 #ifdef IP_SET_HASH_WITH_NETS
887 for (j
= 0; j
< IPSET_NET_COUNT
; j
++)
888 mtype_del_cidr(h
, NCIDR_PUT(DCIDR_GET(d
->cidr
, j
)),
889 NLEN(set
->family
), j
);
891 ip_set_ext_destroy(set
, data
);
893 for (; i
< n
->pos
; i
++) {
894 if (!test_bit(i
, n
->used
))
897 if (n
->pos
== 0 && k
== 0) {
898 rcu_assign_pointer(hbucket(t
, key
), NULL
);
900 } else if (k
>= AHASH_INIT_SIZE
) {
901 struct hbucket
*tmp
= kzalloc(sizeof(*tmp
) +
902 (n
->size
- AHASH_INIT_SIZE
) * dsize
,
906 tmp
->size
= n
->size
- AHASH_INIT_SIZE
;
907 for (j
= 0, k
= 0; j
< n
->pos
; j
++) {
908 if (!test_bit(j
, n
->used
))
910 data
= ahash_data(n
, j
, dsize
);
911 memcpy(tmp
->value
+ k
* dsize
, data
, dsize
);
912 set_bit(j
, tmp
->used
);
916 rcu_assign_pointer(hbucket(t
, key
), tmp
);
927 mtype_data_match(struct mtype_elem
*data
, const struct ip_set_ext
*ext
,
928 struct ip_set_ext
*mext
, struct ip_set
*set
, u32 flags
)
930 if (SET_WITH_COUNTER(set
))
931 ip_set_update_counter(ext_counter(data
, set
),
933 if (SET_WITH_SKBINFO(set
))
934 ip_set_get_skbinfo(ext_skbinfo(data
, set
),
936 return mtype_do_data_match(data
);
939 #ifdef IP_SET_HASH_WITH_NETS
940 /* Special test function which takes into account the different network
941 * sizes added to the set
944 mtype_test_cidrs(struct ip_set
*set
, struct mtype_elem
*d
,
945 const struct ip_set_ext
*ext
,
946 struct ip_set_ext
*mext
, u32 flags
)
948 struct htype
*h
= set
->data
;
949 struct htable
*t
= rcu_dereference_bh(h
->table
);
951 struct mtype_elem
*data
;
952 #if IPSET_NET_COUNT == 2
953 struct mtype_elem orig
= *d
;
959 u8 nets_length
= NLEN(set
->family
);
961 pr_debug("test by nets\n");
962 for (; j
< nets_length
&& h
->nets
[j
].cidr
[0] && !multi
; j
++) {
963 #if IPSET_NET_COUNT == 2
964 mtype_data_reset_elem(d
, &orig
);
965 mtype_data_netmask(d
, NCIDR_GET(h
->nets
[j
].cidr
[0]), false);
966 for (k
= 0; k
< nets_length
&& h
->nets
[k
].cidr
[1] && !multi
;
968 mtype_data_netmask(d
, NCIDR_GET(h
->nets
[k
].cidr
[1]),
971 mtype_data_netmask(d
, NCIDR_GET(h
->nets
[j
].cidr
[0]));
973 key
= HKEY(d
, h
->initval
, t
->htable_bits
);
974 n
= rcu_dereference_bh(hbucket(t
, key
));
977 for (i
= 0; i
< n
->pos
; i
++) {
978 if (!test_bit(i
, n
->used
))
980 data
= ahash_data(n
, i
, set
->dsize
);
981 if (!mtype_data_equal(data
, d
, &multi
))
983 if (SET_WITH_TIMEOUT(set
)) {
984 if (!ip_set_timeout_expired(
985 ext_timeout(data
, set
)))
986 return mtype_data_match(data
, ext
,
989 #ifdef IP_SET_HASH_WITH_MULTI
993 return mtype_data_match(data
, ext
,
996 #if IPSET_NET_COUNT == 2
1004 /* Test whether the element is added to the set */
1006 mtype_test(struct ip_set
*set
, void *value
, const struct ip_set_ext
*ext
,
1007 struct ip_set_ext
*mext
, u32 flags
)
1009 struct htype
*h
= set
->data
;
1011 struct mtype_elem
*d
= value
;
1013 struct mtype_elem
*data
;
1017 t
= rcu_dereference_bh(h
->table
);
1018 #ifdef IP_SET_HASH_WITH_NETS
1019 /* If we test an IP address and not a network address,
1020 * try all possible network sizes
1022 for (i
= 0; i
< IPSET_NET_COUNT
; i
++)
1023 if (DCIDR_GET(d
->cidr
, i
) != SET_HOST_MASK(set
->family
))
1025 if (i
== IPSET_NET_COUNT
) {
1026 ret
= mtype_test_cidrs(set
, d
, ext
, mext
, flags
);
1031 key
= HKEY(d
, h
->initval
, t
->htable_bits
);
1032 n
= rcu_dereference_bh(hbucket(t
, key
));
1037 for (i
= 0; i
< n
->pos
; i
++) {
1038 if (!test_bit(i
, n
->used
))
1040 data
= ahash_data(n
, i
, set
->dsize
);
1041 if (mtype_data_equal(data
, d
, &multi
) &&
1042 !(SET_WITH_TIMEOUT(set
) &&
1043 ip_set_timeout_expired(ext_timeout(data
, set
)))) {
1044 ret
= mtype_data_match(data
, ext
, mext
, set
, flags
);
1052 /* Reply a HEADER request: fill out the header part of the set */
1054 mtype_head(struct ip_set
*set
, struct sk_buff
*skb
)
1056 const struct htype
*h
= set
->data
;
1057 const struct htable
*t
;
1058 struct nlattr
*nested
;
1063 t
= rcu_dereference_bh_nfnl(h
->table
);
1064 memsize
= mtype_ahash_memsize(h
, t
, NLEN(set
->family
), set
->dsize
);
1065 htable_bits
= t
->htable_bits
;
1066 rcu_read_unlock_bh();
1068 nested
= ipset_nest_start(skb
, IPSET_ATTR_DATA
);
1070 goto nla_put_failure
;
1071 if (nla_put_net32(skb
, IPSET_ATTR_HASHSIZE
,
1072 htonl(jhash_size(htable_bits
))) ||
1073 nla_put_net32(skb
, IPSET_ATTR_MAXELEM
, htonl(h
->maxelem
)))
1074 goto nla_put_failure
;
1075 #ifdef IP_SET_HASH_WITH_NETMASK
1076 if (h
->netmask
!= HOST_MASK
&&
1077 nla_put_u8(skb
, IPSET_ATTR_NETMASK
, h
->netmask
))
1078 goto nla_put_failure
;
1080 #ifdef IP_SET_HASH_WITH_MARKMASK
1081 if (nla_put_u32(skb
, IPSET_ATTR_MARKMASK
, h
->markmask
))
1082 goto nla_put_failure
;
1084 if (nla_put_net32(skb
, IPSET_ATTR_REFERENCES
, htonl(set
->ref
)) ||
1085 nla_put_net32(skb
, IPSET_ATTR_MEMSIZE
, htonl(memsize
)) ||
1086 nla_put_net32(skb
, IPSET_ATTR_ELEMENTS
, htonl(set
->elements
)))
1087 goto nla_put_failure
;
1088 if (unlikely(ip_set_put_flags(skb
, set
)))
1089 goto nla_put_failure
;
1090 ipset_nest_end(skb
, nested
);
1097 /* Make possible to run dumping parallel with resizing */
1099 mtype_uref(struct ip_set
*set
, struct netlink_callback
*cb
, bool start
)
1101 struct htype
*h
= set
->data
;
1106 t
= rcu_dereference_bh_nfnl(h
->table
);
1107 atomic_inc(&t
->uref
);
1108 cb
->args
[IPSET_CB_PRIVATE
] = (unsigned long)t
;
1109 rcu_read_unlock_bh();
1110 } else if (cb
->args
[IPSET_CB_PRIVATE
]) {
1111 t
= (struct htable
*)cb
->args
[IPSET_CB_PRIVATE
];
1112 if (atomic_dec_and_test(&t
->uref
) && atomic_read(&t
->ref
)) {
1113 /* Resizing didn't destroy the hash table */
1114 pr_debug("Table destroy by dump: %p\n", t
);
1115 mtype_ahash_destroy(set
, t
, false);
1117 cb
->args
[IPSET_CB_PRIVATE
] = 0;
1121 /* Reply a LIST/SAVE request: dump the elements of the specified set */
1123 mtype_list(const struct ip_set
*set
,
1124 struct sk_buff
*skb
, struct netlink_callback
*cb
)
1126 const struct htable
*t
;
1127 struct nlattr
*atd
, *nested
;
1128 const struct hbucket
*n
;
1129 const struct mtype_elem
*e
;
1130 u32 first
= cb
->args
[IPSET_CB_ARG0
];
1131 /* We assume that one hash bucket fills into one page */
1135 atd
= ipset_nest_start(skb
, IPSET_ATTR_ADT
);
1139 pr_debug("list hash set %s\n", set
->name
);
1140 t
= (const struct htable
*)cb
->args
[IPSET_CB_PRIVATE
];
1141 /* Expire may replace a hbucket with another one */
1143 for (; cb
->args
[IPSET_CB_ARG0
] < jhash_size(t
->htable_bits
);
1144 cb
->args
[IPSET_CB_ARG0
]++) {
1145 incomplete
= skb_tail_pointer(skb
);
1146 n
= rcu_dereference(hbucket(t
, cb
->args
[IPSET_CB_ARG0
]));
1147 pr_debug("cb->arg bucket: %lu, t %p n %p\n",
1148 cb
->args
[IPSET_CB_ARG0
], t
, n
);
1151 for (i
= 0; i
< n
->pos
; i
++) {
1152 if (!test_bit(i
, n
->used
))
1154 e
= ahash_data(n
, i
, set
->dsize
);
1155 if (SET_WITH_TIMEOUT(set
) &&
1156 ip_set_timeout_expired(ext_timeout(e
, set
)))
1158 pr_debug("list hash %lu hbucket %p i %u, data %p\n",
1159 cb
->args
[IPSET_CB_ARG0
], n
, i
, e
);
1160 nested
= ipset_nest_start(skb
, IPSET_ATTR_DATA
);
1162 if (cb
->args
[IPSET_CB_ARG0
] == first
) {
1163 nla_nest_cancel(skb
, atd
);
1167 goto nla_put_failure
;
1169 if (mtype_data_list(skb
, e
))
1170 goto nla_put_failure
;
1171 if (ip_set_put_extensions(skb
, set
, e
, true))
1172 goto nla_put_failure
;
1173 ipset_nest_end(skb
, nested
);
1176 ipset_nest_end(skb
, atd
);
1177 /* Set listing finished */
1178 cb
->args
[IPSET_CB_ARG0
] = 0;
1183 nlmsg_trim(skb
, incomplete
);
1184 if (unlikely(first
== cb
->args
[IPSET_CB_ARG0
])) {
1185 pr_warn("Can't list set %s: one bucket does not fit into a message. Please report it!\n",
1187 cb
->args
[IPSET_CB_ARG0
] = 0;
1190 ipset_nest_end(skb
, atd
);
1198 IPSET_TOKEN(MTYPE
, _kadt
)(struct ip_set
*set
, const struct sk_buff
*skb
,
1199 const struct xt_action_param
*par
,
1200 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
);
1203 IPSET_TOKEN(MTYPE
, _uadt
)(struct ip_set
*set
, struct nlattr
*tb
[],
1204 enum ipset_adt adt
, u32
*lineno
, u32 flags
,
1207 static const struct ip_set_type_variant mtype_variant
= {
1211 [IPSET_ADD
] = mtype_add
,
1212 [IPSET_DEL
] = mtype_del
,
1213 [IPSET_TEST
] = mtype_test
,
1215 .destroy
= mtype_destroy
,
1216 .flush
= mtype_flush
,
1220 .resize
= mtype_resize
,
1221 .same_set
= mtype_same_set
,
1224 #ifdef IP_SET_EMIT_CREATE
1226 IPSET_TOKEN(HTYPE
, _create
)(struct net
*net
, struct ip_set
*set
,
1227 struct nlattr
*tb
[], u32 flags
)
1229 u32 hashsize
= IPSET_DEFAULT_HASHSIZE
, maxelem
= IPSET_DEFAULT_MAXELEM
;
1230 #ifdef IP_SET_HASH_WITH_MARKMASK
1234 #ifdef IP_SET_HASH_WITH_NETMASK
1241 #ifndef IP_SET_PROTO_UNDEF
1242 if (!(set
->family
== NFPROTO_IPV4
|| set
->family
== NFPROTO_IPV6
))
1243 return -IPSET_ERR_INVALID_FAMILY
;
1246 #ifdef IP_SET_HASH_WITH_MARKMASK
1247 markmask
= 0xffffffff;
1249 #ifdef IP_SET_HASH_WITH_NETMASK
1250 netmask
= set
->family
== NFPROTO_IPV4
? 32 : 128;
1251 pr_debug("Create set %s with family %s\n",
1252 set
->name
, set
->family
== NFPROTO_IPV4
? "inet" : "inet6");
1255 if (unlikely(!ip_set_optattr_netorder(tb
, IPSET_ATTR_HASHSIZE
) ||
1256 !ip_set_optattr_netorder(tb
, IPSET_ATTR_MAXELEM
) ||
1257 !ip_set_optattr_netorder(tb
, IPSET_ATTR_TIMEOUT
) ||
1258 !ip_set_optattr_netorder(tb
, IPSET_ATTR_CADT_FLAGS
)))
1259 return -IPSET_ERR_PROTOCOL
;
1260 #ifdef IP_SET_HASH_WITH_MARKMASK
1261 /* Separated condition in order to avoid directive in argument list */
1262 if (unlikely(!ip_set_optattr_netorder(tb
, IPSET_ATTR_MARKMASK
)))
1263 return -IPSET_ERR_PROTOCOL
;
1266 if (tb
[IPSET_ATTR_HASHSIZE
]) {
1267 hashsize
= ip_set_get_h32(tb
[IPSET_ATTR_HASHSIZE
]);
1268 if (hashsize
< IPSET_MIMINAL_HASHSIZE
)
1269 hashsize
= IPSET_MIMINAL_HASHSIZE
;
1272 if (tb
[IPSET_ATTR_MAXELEM
])
1273 maxelem
= ip_set_get_h32(tb
[IPSET_ATTR_MAXELEM
]);
1275 #ifdef IP_SET_HASH_WITH_NETMASK
1276 if (tb
[IPSET_ATTR_NETMASK
]) {
1277 netmask
= nla_get_u8(tb
[IPSET_ATTR_NETMASK
]);
1279 if ((set
->family
== NFPROTO_IPV4
&& netmask
> 32) ||
1280 (set
->family
== NFPROTO_IPV6
&& netmask
> 128) ||
1282 return -IPSET_ERR_INVALID_NETMASK
;
1285 #ifdef IP_SET_HASH_WITH_MARKMASK
1286 if (tb
[IPSET_ATTR_MARKMASK
]) {
1287 markmask
= ntohl(nla_get_be32(tb
[IPSET_ATTR_MARKMASK
]));
1290 return -IPSET_ERR_INVALID_MARKMASK
;
1295 #ifdef IP_SET_HASH_WITH_NETS
1296 hsize
+= sizeof(struct net_prefixes
) * NLEN(set
->family
);
1298 h
= kzalloc(hsize
, GFP_KERNEL
);
1302 h
->maxelem
= maxelem
;
1303 #ifdef IP_SET_HASH_WITH_NETMASK
1304 h
->netmask
= netmask
;
1306 #ifdef IP_SET_HASH_WITH_MARKMASK
1307 h
->markmask
= markmask
;
1309 get_random_bytes(&h
->initval
, sizeof(h
->initval
));
1310 set
->timeout
= IPSET_NO_TIMEOUT
;
1312 hbits
= htable_bits(hashsize
);
1313 hsize
= htable_size(hbits
);
1318 t
= ip_set_alloc(hsize
);
1323 t
->htable_bits
= hbits
;
1324 rcu_assign_pointer(h
->table
, t
);
1327 #ifndef IP_SET_PROTO_UNDEF
1328 if (set
->family
== NFPROTO_IPV4
) {
1330 set
->variant
= &IPSET_TOKEN(HTYPE
, 4_variant
);
1331 set
->dsize
= ip_set_elem_len(set
, tb
,
1332 sizeof(struct IPSET_TOKEN(HTYPE
, 4_elem
)),
1333 __alignof__(struct IPSET_TOKEN(HTYPE
, 4_elem
)));
1334 #ifndef IP_SET_PROTO_UNDEF
1336 set
->variant
= &IPSET_TOKEN(HTYPE
, 6_variant
);
1337 set
->dsize
= ip_set_elem_len(set
, tb
,
1338 sizeof(struct IPSET_TOKEN(HTYPE
, 6_elem
)),
1339 __alignof__(struct IPSET_TOKEN(HTYPE
, 6_elem
)));
1342 if (tb
[IPSET_ATTR_TIMEOUT
]) {
1343 set
->timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
1344 #ifndef IP_SET_PROTO_UNDEF
1345 if (set
->family
== NFPROTO_IPV4
)
1347 IPSET_TOKEN(HTYPE
, 4_gc_init
)(set
,
1348 IPSET_TOKEN(HTYPE
, 4_gc
));
1349 #ifndef IP_SET_PROTO_UNDEF
1351 IPSET_TOKEN(HTYPE
, 6_gc_init
)(set
,
1352 IPSET_TOKEN(HTYPE
, 6_gc
));
1355 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
1356 set
->name
, jhash_size(t
->htable_bits
),
1357 t
->htable_bits
, h
->maxelem
, set
->data
, t
);
1361 #endif /* IP_SET_EMIT_CREATE */