1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (C) 2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
4 #ifndef _IP_SET_HASH_GEN_H
5 #define _IP_SET_HASH_GEN_H
7 #include <linux/rcupdate.h>
8 #include <linux/jhash.h>
9 #include <linux/types.h>
10 #include <linux/netfilter/ipset/ip_set.h>
12 #define __ipset_dereference_protected(p, c) rcu_dereference_protected(p, c)
13 #define ipset_dereference_protected(p, set) \
14 __ipset_dereference_protected(p, lockdep_is_held(&(set)->lock))
16 #define rcu_dereference_bh_nfnl(p) rcu_dereference_bh_check(p, 1)
18 /* Hashing which uses arrays to resolve clashing. The hash table is resized
19 * (doubled) when searching becomes too long.
20 * Internally jhash is used with the assumption that the size of the
21 * stored data is a multiple of sizeof(u32).
23 * Readers and resizing
25 * Resizing can be triggered by userspace command only, and those
26 * are serialized by the nfnl mutex. During resizing the set is
27 * read-locked, so the only possible concurrent operations are
28 * the kernel side readers. Those must be protected by proper RCU locking.
31 /* Number of elements to store in an initial array block */
32 #define AHASH_INIT_SIZE 4
33 /* Max number of elements to store in an array block */
34 #define AHASH_MAX_SIZE (3 * AHASH_INIT_SIZE)
35 /* Max muber of elements in the array block when tuned */
36 #define AHASH_MAX_TUNED 64
38 /* Max number of elements can be tuned */
39 #ifdef IP_SET_HASH_WITH_MULTI
40 #define AHASH_MAX(h) ((h)->ahash_max)
43 tune_ahash_max(u8 curr
, u32 multi
)
50 n
= curr
+ AHASH_INIT_SIZE
;
51 /* Currently, at listing one hash bucket must fit into a message.
52 * Therefore we have a hard limit here.
54 return n
> curr
&& n
<= AHASH_MAX_TUNED
? n
: curr
;
57 #define TUNE_AHASH_MAX(h, multi) \
58 ((h)->ahash_max = tune_ahash_max((h)->ahash_max, multi))
60 #define AHASH_MAX(h) AHASH_MAX_SIZE
61 #define TUNE_AHASH_MAX(h, multi)
66 struct rcu_head rcu
; /* for call_rcu */
67 /* Which positions are used in the array */
68 DECLARE_BITMAP(used
, AHASH_MAX_TUNED
);
69 u8 size
; /* size of the array */
70 u8 pos
; /* position of the first free entry */
71 unsigned char value
[0] /* the array of the values */
72 __aligned(__alignof__(u64
));
75 /* The hash table: the table size stored here in order to make resizing easy */
77 atomic_t ref
; /* References for resizing */
78 atomic_t uref
; /* References for dumping */
79 u8 htable_bits
; /* size of hash table == 2^htable_bits */
80 struct hbucket __rcu
*bucket
[0]; /* hashtable buckets */
83 #define hbucket(h, i) ((h)->bucket[i])
84 #define ext_size(n, dsize) \
85 (sizeof(struct hbucket) + (n) * (dsize))
87 #ifndef IPSET_NET_COUNT
88 #define IPSET_NET_COUNT 1
91 /* Book-keeping of the prefixes added to the set */
93 u32 nets
[IPSET_NET_COUNT
]; /* number of elements for this cidr */
94 u8 cidr
[IPSET_NET_COUNT
]; /* the cidr value */
97 /* Compute the hash table size */
103 /* We must fit both into u32 in jhash and size_t */
106 hsize
= jhash_size(hbits
);
107 if ((((size_t)-1) - sizeof(struct htable
)) / sizeof(struct hbucket
*)
111 return hsize
* sizeof(struct hbucket
*) + sizeof(struct htable
);
114 /* Compute htable_bits from the user input parameter hashsize */
116 htable_bits(u32 hashsize
)
118 /* Assume that hashsize == 2^htable_bits */
119 u8 bits
= fls(hashsize
- 1);
121 if (jhash_size(bits
) != hashsize
)
122 /* Round up to the first 2^n value */
123 bits
= fls(hashsize
);
128 #ifdef IP_SET_HASH_WITH_NETS
129 #if IPSET_NET_COUNT > 1
130 #define __CIDR(cidr, i) (cidr[i])
132 #define __CIDR(cidr, i) (cidr)
135 /* cidr + 1 is stored in net_prefixes to support /0 */
136 #define NCIDR_PUT(cidr) ((cidr) + 1)
137 #define NCIDR_GET(cidr) ((cidr) - 1)
139 #ifdef IP_SET_HASH_WITH_NETS_PACKED
140 /* When cidr is packed with nomatch, cidr - 1 is stored in the data entry */
141 #define DCIDR_PUT(cidr) ((cidr) - 1)
142 #define DCIDR_GET(cidr, i) (__CIDR(cidr, i) + 1)
144 #define DCIDR_PUT(cidr) (cidr)
145 #define DCIDR_GET(cidr, i) __CIDR(cidr, i)
148 #define INIT_CIDR(cidr, host_mask) \
149 DCIDR_PUT(((cidr) ? NCIDR_GET(cidr) : host_mask))
151 #ifdef IP_SET_HASH_WITH_NET0
152 /* cidr from 0 to HOST_MASK value and c = cidr + 1 */
153 #define NLEN (HOST_MASK + 1)
154 #define CIDR_POS(c) ((c) - 1)
156 /* cidr from 1 to HOST_MASK value and c = cidr + 1 */
157 #define NLEN HOST_MASK
158 #define CIDR_POS(c) ((c) - 2)
163 #endif /* IP_SET_HASH_WITH_NETS */
165 #endif /* _IP_SET_HASH_GEN_H */
168 #error "MTYPE is not defined!"
172 #error "HTYPE is not defined!"
176 #error "HOST_MASK is not defined!"
179 /* Family dependent templates */
182 #undef mtype_data_equal
183 #undef mtype_do_data_match
184 #undef mtype_data_set_flags
185 #undef mtype_data_reset_elem
186 #undef mtype_data_reset_flags
187 #undef mtype_data_netmask
188 #undef mtype_data_list
189 #undef mtype_data_next
192 #undef mtype_ahash_destroy
193 #undef mtype_ext_cleanup
194 #undef mtype_add_cidr
195 #undef mtype_del_cidr
196 #undef mtype_ahash_memsize
199 #undef mtype_same_set
205 #undef mtype_test_cidrs
215 #undef mtype_data_match
220 #define mtype_data_equal IPSET_TOKEN(MTYPE, _data_equal)
221 #ifdef IP_SET_HASH_WITH_NETS
222 #define mtype_do_data_match IPSET_TOKEN(MTYPE, _do_data_match)
224 #define mtype_do_data_match(d) 1
226 #define mtype_data_set_flags IPSET_TOKEN(MTYPE, _data_set_flags)
227 #define mtype_data_reset_elem IPSET_TOKEN(MTYPE, _data_reset_elem)
228 #define mtype_data_reset_flags IPSET_TOKEN(MTYPE, _data_reset_flags)
229 #define mtype_data_netmask IPSET_TOKEN(MTYPE, _data_netmask)
230 #define mtype_data_list IPSET_TOKEN(MTYPE, _data_list)
231 #define mtype_data_next IPSET_TOKEN(MTYPE, _data_next)
232 #define mtype_elem IPSET_TOKEN(MTYPE, _elem)
234 #define mtype_ahash_destroy IPSET_TOKEN(MTYPE, _ahash_destroy)
235 #define mtype_ext_cleanup IPSET_TOKEN(MTYPE, _ext_cleanup)
236 #define mtype_add_cidr IPSET_TOKEN(MTYPE, _add_cidr)
237 #define mtype_del_cidr IPSET_TOKEN(MTYPE, _del_cidr)
238 #define mtype_ahash_memsize IPSET_TOKEN(MTYPE, _ahash_memsize)
239 #define mtype_flush IPSET_TOKEN(MTYPE, _flush)
240 #define mtype_destroy IPSET_TOKEN(MTYPE, _destroy)
241 #define mtype_same_set IPSET_TOKEN(MTYPE, _same_set)
242 #define mtype_kadt IPSET_TOKEN(MTYPE, _kadt)
243 #define mtype_uadt IPSET_TOKEN(MTYPE, _uadt)
245 #define mtype_add IPSET_TOKEN(MTYPE, _add)
246 #define mtype_del IPSET_TOKEN(MTYPE, _del)
247 #define mtype_test_cidrs IPSET_TOKEN(MTYPE, _test_cidrs)
248 #define mtype_test IPSET_TOKEN(MTYPE, _test)
249 #define mtype_uref IPSET_TOKEN(MTYPE, _uref)
250 #define mtype_expire IPSET_TOKEN(MTYPE, _expire)
251 #define mtype_resize IPSET_TOKEN(MTYPE, _resize)
252 #define mtype_head IPSET_TOKEN(MTYPE, _head)
253 #define mtype_list IPSET_TOKEN(MTYPE, _list)
254 #define mtype_gc IPSET_TOKEN(MTYPE, _gc)
255 #define mtype_gc_init IPSET_TOKEN(MTYPE, _gc_init)
256 #define mtype_variant IPSET_TOKEN(MTYPE, _variant)
257 #define mtype_data_match IPSET_TOKEN(MTYPE, _data_match)
260 #define HKEY_DATALEN sizeof(struct mtype_elem)
265 #define HKEY(data, initval, htable_bits) \
267 const u32 *__k = (const u32 *)data; \
268 u32 __l = HKEY_DATALEN / sizeof(u32); \
270 BUILD_BUG_ON(HKEY_DATALEN % sizeof(u32) != 0); \
272 jhash2(__k, __l, initval) & jhash_mask(htable_bits); \
275 /* The generic hash structure */
277 struct htable __rcu
*table
; /* the hash table */
278 struct timer_list gc
; /* garbage collection when timeout enabled */
279 struct ip_set
*set
; /* attached to this ip_set */
280 u32 maxelem
; /* max elements in the hash */
281 u32 initval
; /* random jhash init value */
282 #ifdef IP_SET_HASH_WITH_MARKMASK
283 u32 markmask
; /* markmask value for mark mask to store */
285 #ifdef IP_SET_HASH_WITH_MULTI
286 u8 ahash_max
; /* max elements in an array block */
288 #ifdef IP_SET_HASH_WITH_NETMASK
289 u8 netmask
; /* netmask value for subnets to store */
291 struct mtype_elem next
; /* temporary storage for uadd */
292 #ifdef IP_SET_HASH_WITH_NETS
293 struct net_prefixes nets
[NLEN
]; /* book-keeping of prefixes */
297 #ifdef IP_SET_HASH_WITH_NETS
298 /* Network cidr size book keeping when the hash stores different
299 * sized networks. cidr == real cidr + 1 to support /0.
302 mtype_add_cidr(struct htype
*h
, u8 cidr
, u8 n
)
306 /* Add in increasing prefix order, so larger cidr first */
307 for (i
= 0, j
= -1; i
< NLEN
&& h
->nets
[i
].cidr
[n
]; i
++) {
310 } else if (h
->nets
[i
].cidr
[n
] < cidr
) {
312 } else if (h
->nets
[i
].cidr
[n
] == cidr
) {
313 h
->nets
[CIDR_POS(cidr
)].nets
[n
]++;
319 h
->nets
[i
].cidr
[n
] = h
->nets
[i
- 1].cidr
[n
];
321 h
->nets
[i
].cidr
[n
] = cidr
;
322 h
->nets
[CIDR_POS(cidr
)].nets
[n
] = 1;
326 mtype_del_cidr(struct htype
*h
, u8 cidr
, u8 n
)
328 u8 i
, j
, net_end
= NLEN
- 1;
330 for (i
= 0; i
< NLEN
; i
++) {
331 if (h
->nets
[i
].cidr
[n
] != cidr
)
333 h
->nets
[CIDR_POS(cidr
)].nets
[n
]--;
334 if (h
->nets
[CIDR_POS(cidr
)].nets
[n
] > 0)
336 for (j
= i
; j
< net_end
&& h
->nets
[j
].cidr
[n
]; j
++)
337 h
->nets
[j
].cidr
[n
] = h
->nets
[j
+ 1].cidr
[n
];
338 h
->nets
[j
].cidr
[n
] = 0;
344 /* Calculate the actual memory size of the set data */
346 mtype_ahash_memsize(const struct htype
*h
, const struct htable
*t
)
348 return sizeof(*h
) + sizeof(*t
);
351 /* Get the ith element from the array block n */
352 #define ahash_data(n, i, dsize) \
353 ((struct mtype_elem *)((n)->value + ((i) * (dsize))))
356 mtype_ext_cleanup(struct ip_set
*set
, struct hbucket
*n
)
360 for (i
= 0; i
< n
->pos
; i
++)
361 if (test_bit(i
, n
->used
))
362 ip_set_ext_destroy(set
, ahash_data(n
, i
, set
->dsize
));
365 /* Flush a hash type of set: destroy all elements */
367 mtype_flush(struct ip_set
*set
)
369 struct htype
*h
= set
->data
;
374 t
= ipset_dereference_protected(h
->table
, set
);
375 for (i
= 0; i
< jhash_size(t
->htable_bits
); i
++) {
376 n
= __ipset_dereference_protected(hbucket(t
, i
), 1);
379 if (set
->extensions
& IPSET_EXT_DESTROY
)
380 mtype_ext_cleanup(set
, n
);
381 /* FIXME: use slab cache */
382 rcu_assign_pointer(hbucket(t
, i
), NULL
);
385 #ifdef IP_SET_HASH_WITH_NETS
386 memset(h
->nets
, 0, sizeof(h
->nets
));
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
)(struct timer_list
*t
))
431 struct htype
*h
= set
->data
;
433 timer_setup(&h
->gc
, gc
, 0);
434 mod_timer(&h
->gc
, jiffies
+ IPSET_GC_PERIOD(set
->timeout
) * HZ
);
435 pr_debug("gc initialized, run in every %u\n",
436 IPSET_GC_PERIOD(set
->timeout
));
440 mtype_same_set(const struct ip_set
*a
, const struct ip_set
*b
)
442 const struct htype
*x
= a
->data
;
443 const struct htype
*y
= b
->data
;
445 /* Resizing changes htable_bits, so we ignore it */
446 return x
->maxelem
== y
->maxelem
&&
447 a
->timeout
== b
->timeout
&&
448 #ifdef IP_SET_HASH_WITH_NETMASK
449 x
->netmask
== y
->netmask
&&
451 #ifdef IP_SET_HASH_WITH_MARKMASK
452 x
->markmask
== y
->markmask
&&
454 a
->extensions
== b
->extensions
;
457 /* Delete expired elements from the hashtable */
459 mtype_expire(struct ip_set
*set
, struct htype
*h
)
462 struct hbucket
*n
, *tmp
;
463 struct mtype_elem
*data
;
465 size_t dsize
= set
->dsize
;
466 #ifdef IP_SET_HASH_WITH_NETS
470 t
= ipset_dereference_protected(h
->table
, set
);
471 for (i
= 0; i
< jhash_size(t
->htable_bits
); i
++) {
472 n
= __ipset_dereference_protected(hbucket(t
, i
), 1);
475 for (j
= 0, d
= 0; j
< n
->pos
; j
++) {
476 if (!test_bit(j
, n
->used
)) {
480 data
= ahash_data(n
, j
, dsize
);
481 if (!ip_set_timeout_expired(ext_timeout(data
, set
)))
483 pr_debug("expired %u/%u\n", i
, j
);
484 clear_bit(j
, n
->used
);
485 smp_mb__after_atomic();
486 #ifdef IP_SET_HASH_WITH_NETS
487 for (k
= 0; k
< IPSET_NET_COUNT
; k
++)
489 NCIDR_PUT(DCIDR_GET(data
->cidr
, k
)),
492 ip_set_ext_destroy(set
, data
);
496 if (d
>= AHASH_INIT_SIZE
) {
498 rcu_assign_pointer(hbucket(t
, i
), NULL
);
502 tmp
= kzalloc(sizeof(*tmp
) +
503 (n
->size
- AHASH_INIT_SIZE
) * dsize
,
506 /* Still try to delete expired elements */
508 tmp
->size
= n
->size
- AHASH_INIT_SIZE
;
509 for (j
= 0, d
= 0; j
< n
->pos
; j
++) {
510 if (!test_bit(j
, n
->used
))
512 data
= ahash_data(n
, j
, dsize
);
513 memcpy(tmp
->value
+ d
* dsize
, data
, dsize
);
514 set_bit(d
, tmp
->used
);
518 set
->ext_size
-= ext_size(AHASH_INIT_SIZE
, dsize
);
519 rcu_assign_pointer(hbucket(t
, i
), tmp
);
526 mtype_gc(struct timer_list
*t
)
528 struct htype
*h
= from_timer(h
, t
, gc
);
529 struct ip_set
*set
= h
->set
;
531 pr_debug("called\n");
532 spin_lock_bh(&set
->lock
);
533 mtype_expire(set
, h
);
534 spin_unlock_bh(&set
->lock
);
536 h
->gc
.expires
= jiffies
+ IPSET_GC_PERIOD(set
->timeout
) * HZ
;
540 /* Resize a hash: create a new hash table with doubling the hashsize
541 * and inserting the elements to it. Repeat until we succeed or
542 * fail due to memory pressures.
545 mtype_resize(struct ip_set
*set
, bool retried
)
547 struct htype
*h
= set
->data
;
548 struct htable
*t
, *orig
;
550 size_t extsize
, dsize
= set
->dsize
;
551 #ifdef IP_SET_HASH_WITH_NETS
553 struct mtype_elem
*tmp
;
555 struct mtype_elem
*data
;
556 struct mtype_elem
*d
;
557 struct hbucket
*n
, *m
;
561 #ifdef IP_SET_HASH_WITH_NETS
562 tmp
= kmalloc(dsize
, GFP_KERNEL
);
567 orig
= rcu_dereference_bh_nfnl(h
->table
);
568 htable_bits
= orig
->htable_bits
;
569 rcu_read_unlock_bh();
575 /* In case we have plenty of memory :-) */
576 pr_warn("Cannot increase the hashsize of set %s further\n",
578 ret
= -IPSET_ERR_HASH_FULL
;
581 t
= ip_set_alloc(htable_size(htable_bits
));
586 t
->htable_bits
= htable_bits
;
588 spin_lock_bh(&set
->lock
);
589 orig
= __ipset_dereference_protected(h
->table
, 1);
590 /* There can't be another parallel resizing, but dumping is possible */
591 atomic_set(&orig
->ref
, 1);
592 atomic_inc(&orig
->uref
);
594 pr_debug("attempt to resize set %s from %u to %u, t %p\n",
595 set
->name
, orig
->htable_bits
, htable_bits
, orig
);
596 for (i
= 0; i
< jhash_size(orig
->htable_bits
); i
++) {
597 n
= __ipset_dereference_protected(hbucket(orig
, i
), 1);
600 for (j
= 0; j
< n
->pos
; j
++) {
601 if (!test_bit(j
, n
->used
))
603 data
= ahash_data(n
, j
, dsize
);
604 #ifdef IP_SET_HASH_WITH_NETS
605 /* We have readers running parallel with us,
606 * so the live data cannot be modified.
609 memcpy(tmp
, data
, dsize
);
611 mtype_data_reset_flags(data
, &flags
);
613 key
= HKEY(data
, h
->initval
, htable_bits
);
614 m
= __ipset_dereference_protected(hbucket(t
, key
), 1);
616 m
= kzalloc(sizeof(*m
) +
617 AHASH_INIT_SIZE
* dsize
,
623 m
->size
= AHASH_INIT_SIZE
;
624 extsize
+= ext_size(AHASH_INIT_SIZE
, dsize
);
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
;
644 extsize
+= ext_size(AHASH_INIT_SIZE
, dsize
);
647 RCU_INIT_POINTER(hbucket(t
, key
), ht
);
649 d
= ahash_data(m
, m
->pos
, dsize
);
650 memcpy(d
, data
, dsize
);
651 set_bit(m
->pos
++, m
->used
);
652 #ifdef IP_SET_HASH_WITH_NETS
653 mtype_data_reset_flags(d
, &flags
);
657 rcu_assign_pointer(h
->table
, t
);
658 set
->ext_size
= extsize
;
660 spin_unlock_bh(&set
->lock
);
662 /* Give time to other readers of the set */
665 pr_debug("set %s resized from %u (%p) to %u (%p)\n", set
->name
,
666 orig
->htable_bits
, orig
, t
->htable_bits
, t
);
667 /* If there's nobody else dumping the table, destroy it */
668 if (atomic_dec_and_test(&orig
->uref
)) {
669 pr_debug("Table destroy by resize %p\n", orig
);
670 mtype_ahash_destroy(set
, orig
, false);
674 #ifdef IP_SET_HASH_WITH_NETS
680 atomic_set(&orig
->ref
, 0);
681 atomic_dec(&orig
->uref
);
682 spin_unlock_bh(&set
->lock
);
683 mtype_ahash_destroy(set
, t
, false);
689 /* Add an element to a hash and update the internal counters when succeeded,
690 * otherwise report the proper error code.
693 mtype_add(struct ip_set
*set
, void *value
, const struct ip_set_ext
*ext
,
694 struct ip_set_ext
*mext
, u32 flags
)
696 struct htype
*h
= set
->data
;
698 const struct mtype_elem
*d
= value
;
699 struct mtype_elem
*data
;
700 struct hbucket
*n
, *old
= ERR_PTR(-ENOENT
);
702 bool flag_exist
= flags
& IPSET_FLAG_EXIST
;
703 bool deleted
= false, forceadd
= false, reuse
= false;
706 if (set
->elements
>= h
->maxelem
) {
707 if (SET_WITH_TIMEOUT(set
))
708 /* FIXME: when set is full, we slow down here */
709 mtype_expire(set
, h
);
710 if (set
->elements
>= h
->maxelem
&& SET_WITH_FORCEADD(set
))
714 t
= ipset_dereference_protected(h
->table
, set
);
715 key
= HKEY(value
, h
->initval
, t
->htable_bits
);
716 n
= __ipset_dereference_protected(hbucket(t
, key
), 1);
718 if (forceadd
|| set
->elements
>= h
->maxelem
)
721 n
= kzalloc(sizeof(*n
) + AHASH_INIT_SIZE
* set
->dsize
,
725 n
->size
= AHASH_INIT_SIZE
;
726 set
->ext_size
+= ext_size(AHASH_INIT_SIZE
, set
->dsize
);
729 for (i
= 0; i
< n
->pos
; i
++) {
730 if (!test_bit(i
, n
->used
)) {
731 /* Reuse first deleted entry */
733 deleted
= reuse
= true;
738 data
= ahash_data(n
, i
, set
->dsize
);
739 if (mtype_data_equal(data
, d
, &multi
)) {
741 (SET_WITH_TIMEOUT(set
) &&
742 ip_set_timeout_expired(ext_timeout(data
, set
)))) {
743 /* Just the extensions could be overwritten */
745 goto overwrite_extensions
;
747 return -IPSET_ERR_EXIST
;
749 /* Reuse first timed out entry */
750 if (SET_WITH_TIMEOUT(set
) &&
751 ip_set_timeout_expired(ext_timeout(data
, set
)) &&
757 if (reuse
|| forceadd
) {
758 data
= ahash_data(n
, j
, set
->dsize
);
760 #ifdef IP_SET_HASH_WITH_NETS
761 for (i
= 0; i
< IPSET_NET_COUNT
; i
++)
763 NCIDR_PUT(DCIDR_GET(data
->cidr
, i
)),
766 ip_set_ext_destroy(set
, data
);
771 if (set
->elements
>= h
->maxelem
)
773 /* Create a new slot */
774 if (n
->pos
>= n
->size
) {
775 TUNE_AHASH_MAX(h
, multi
);
776 if (n
->size
>= AHASH_MAX(h
)) {
777 /* Trigger rehashing */
778 mtype_data_next(&h
->next
, d
);
782 n
= kzalloc(sizeof(*n
) +
783 (old
->size
+ AHASH_INIT_SIZE
) * set
->dsize
,
787 memcpy(n
, old
, sizeof(struct hbucket
) +
788 old
->size
* set
->dsize
);
789 n
->size
= old
->size
+ AHASH_INIT_SIZE
;
790 set
->ext_size
+= ext_size(AHASH_INIT_SIZE
, set
->dsize
);
795 data
= ahash_data(n
, j
, set
->dsize
);
798 #ifdef IP_SET_HASH_WITH_NETS
799 for (i
= 0; i
< IPSET_NET_COUNT
; i
++)
800 mtype_add_cidr(h
, NCIDR_PUT(DCIDR_GET(d
->cidr
, i
)), i
);
802 memcpy(data
, d
, sizeof(struct mtype_elem
));
803 overwrite_extensions
:
804 #ifdef IP_SET_HASH_WITH_NETS
805 mtype_data_set_flags(data
, flags
);
807 if (SET_WITH_COUNTER(set
))
808 ip_set_init_counter(ext_counter(data
, set
), ext
);
809 if (SET_WITH_COMMENT(set
))
810 ip_set_init_comment(set
, ext_comment(data
, set
), ext
);
811 if (SET_WITH_SKBINFO(set
))
812 ip_set_init_skbinfo(ext_skbinfo(data
, set
), ext
);
813 /* Must come last for the case when timed out entry is reused */
814 if (SET_WITH_TIMEOUT(set
))
815 ip_set_timeout_set(ext_timeout(data
, set
), ext
->timeout
);
816 smp_mb__before_atomic();
818 if (old
!= ERR_PTR(-ENOENT
)) {
819 rcu_assign_pointer(hbucket(t
, key
), n
);
827 pr_warn("Set %s is full, maxelem %u reached\n",
828 set
->name
, h
->maxelem
);
829 return -IPSET_ERR_HASH_FULL
;
832 /* Delete an element from the hash and free up space if possible.
835 mtype_del(struct ip_set
*set
, void *value
, const struct ip_set_ext
*ext
,
836 struct ip_set_ext
*mext
, u32 flags
)
838 struct htype
*h
= set
->data
;
840 const struct mtype_elem
*d
= value
;
841 struct mtype_elem
*data
;
843 int i
, j
, k
, ret
= -IPSET_ERR_EXIST
;
845 size_t dsize
= set
->dsize
;
847 t
= ipset_dereference_protected(h
->table
, set
);
848 key
= HKEY(value
, h
->initval
, t
->htable_bits
);
849 n
= __ipset_dereference_protected(hbucket(t
, key
), 1);
852 for (i
= 0, k
= 0; i
< n
->pos
; i
++) {
853 if (!test_bit(i
, n
->used
)) {
857 data
= ahash_data(n
, i
, dsize
);
858 if (!mtype_data_equal(data
, d
, &multi
))
860 if (SET_WITH_TIMEOUT(set
) &&
861 ip_set_timeout_expired(ext_timeout(data
, set
)))
865 clear_bit(i
, n
->used
);
866 smp_mb__after_atomic();
870 #ifdef IP_SET_HASH_WITH_NETS
871 for (j
= 0; j
< IPSET_NET_COUNT
; j
++)
872 mtype_del_cidr(h
, NCIDR_PUT(DCIDR_GET(d
->cidr
, j
)),
875 ip_set_ext_destroy(set
, data
);
877 for (; i
< n
->pos
; i
++) {
878 if (!test_bit(i
, n
->used
))
881 if (n
->pos
== 0 && k
== 0) {
882 set
->ext_size
-= ext_size(n
->size
, dsize
);
883 rcu_assign_pointer(hbucket(t
, key
), NULL
);
885 } else if (k
>= AHASH_INIT_SIZE
) {
886 struct hbucket
*tmp
= kzalloc(sizeof(*tmp
) +
887 (n
->size
- AHASH_INIT_SIZE
) * dsize
,
891 tmp
->size
= n
->size
- AHASH_INIT_SIZE
;
892 for (j
= 0, k
= 0; j
< n
->pos
; j
++) {
893 if (!test_bit(j
, n
->used
))
895 data
= ahash_data(n
, j
, dsize
);
896 memcpy(tmp
->value
+ k
* dsize
, data
, dsize
);
897 set_bit(k
, tmp
->used
);
901 set
->ext_size
-= ext_size(AHASH_INIT_SIZE
, dsize
);
902 rcu_assign_pointer(hbucket(t
, key
), tmp
);
913 mtype_data_match(struct mtype_elem
*data
, const struct ip_set_ext
*ext
,
914 struct ip_set_ext
*mext
, struct ip_set
*set
, u32 flags
)
916 if (!ip_set_match_extensions(set
, ext
, mext
, flags
, data
))
918 /* nomatch entries return -ENOTEMPTY */
919 return mtype_do_data_match(data
);
922 #ifdef IP_SET_HASH_WITH_NETS
923 /* Special test function which takes into account the different network
924 * sizes added to the set
927 mtype_test_cidrs(struct ip_set
*set
, struct mtype_elem
*d
,
928 const struct ip_set_ext
*ext
,
929 struct ip_set_ext
*mext
, u32 flags
)
931 struct htype
*h
= set
->data
;
932 struct htable
*t
= rcu_dereference_bh(h
->table
);
934 struct mtype_elem
*data
;
935 #if IPSET_NET_COUNT == 2
936 struct mtype_elem orig
= *d
;
937 int ret
, i
, j
= 0, k
;
943 pr_debug("test by nets\n");
944 for (; j
< NLEN
&& h
->nets
[j
].cidr
[0] && !multi
; j
++) {
945 #if IPSET_NET_COUNT == 2
946 mtype_data_reset_elem(d
, &orig
);
947 mtype_data_netmask(d
, NCIDR_GET(h
->nets
[j
].cidr
[0]), false);
948 for (k
= 0; k
< NLEN
&& h
->nets
[k
].cidr
[1] && !multi
;
950 mtype_data_netmask(d
, NCIDR_GET(h
->nets
[k
].cidr
[1]),
953 mtype_data_netmask(d
, NCIDR_GET(h
->nets
[j
].cidr
[0]));
955 key
= HKEY(d
, h
->initval
, t
->htable_bits
);
956 n
= rcu_dereference_bh(hbucket(t
, key
));
959 for (i
= 0; i
< n
->pos
; i
++) {
960 if (!test_bit(i
, n
->used
))
962 data
= ahash_data(n
, i
, set
->dsize
);
963 if (!mtype_data_equal(data
, d
, &multi
))
965 ret
= mtype_data_match(data
, ext
, mext
, set
, flags
);
968 #ifdef IP_SET_HASH_WITH_MULTI
969 /* No match, reset multiple match flag */
973 #if IPSET_NET_COUNT == 2
981 /* Test whether the element is added to the set */
983 mtype_test(struct ip_set
*set
, void *value
, const struct ip_set_ext
*ext
,
984 struct ip_set_ext
*mext
, u32 flags
)
986 struct htype
*h
= set
->data
;
988 struct mtype_elem
*d
= value
;
990 struct mtype_elem
*data
;
994 t
= rcu_dereference_bh(h
->table
);
995 #ifdef IP_SET_HASH_WITH_NETS
996 /* If we test an IP address and not a network address,
997 * try all possible network sizes
999 for (i
= 0; i
< IPSET_NET_COUNT
; i
++)
1000 if (DCIDR_GET(d
->cidr
, i
) != HOST_MASK
)
1002 if (i
== IPSET_NET_COUNT
) {
1003 ret
= mtype_test_cidrs(set
, d
, ext
, mext
, flags
);
1008 key
= HKEY(d
, h
->initval
, t
->htable_bits
);
1009 n
= rcu_dereference_bh(hbucket(t
, key
));
1014 for (i
= 0; i
< n
->pos
; i
++) {
1015 if (!test_bit(i
, n
->used
))
1017 data
= ahash_data(n
, i
, set
->dsize
);
1018 if (!mtype_data_equal(data
, d
, &multi
))
1020 ret
= mtype_data_match(data
, ext
, mext
, set
, flags
);
1028 /* Reply a HEADER request: fill out the header part of the set */
1030 mtype_head(struct ip_set
*set
, struct sk_buff
*skb
)
1032 struct htype
*h
= set
->data
;
1033 const struct htable
*t
;
1034 struct nlattr
*nested
;
1038 /* If any members have expired, set->elements will be wrong
1039 * mytype_expire function will update it with the right count.
1040 * we do not hold set->lock here, so grab it first.
1041 * set->elements can still be incorrect in the case of a huge set,
1042 * because elements might time out during the listing.
1044 if (SET_WITH_TIMEOUT(set
)) {
1045 spin_lock_bh(&set
->lock
);
1046 mtype_expire(set
, h
);
1047 spin_unlock_bh(&set
->lock
);
1051 t
= rcu_dereference_bh_nfnl(h
->table
);
1052 memsize
= mtype_ahash_memsize(h
, t
) + set
->ext_size
;
1053 htable_bits
= t
->htable_bits
;
1054 rcu_read_unlock_bh();
1056 nested
= nla_nest_start(skb
, IPSET_ATTR_DATA
);
1058 goto nla_put_failure
;
1059 if (nla_put_net32(skb
, IPSET_ATTR_HASHSIZE
,
1060 htonl(jhash_size(htable_bits
))) ||
1061 nla_put_net32(skb
, IPSET_ATTR_MAXELEM
, htonl(h
->maxelem
)))
1062 goto nla_put_failure
;
1063 #ifdef IP_SET_HASH_WITH_NETMASK
1064 if (h
->netmask
!= HOST_MASK
&&
1065 nla_put_u8(skb
, IPSET_ATTR_NETMASK
, h
->netmask
))
1066 goto nla_put_failure
;
1068 #ifdef IP_SET_HASH_WITH_MARKMASK
1069 if (nla_put_u32(skb
, IPSET_ATTR_MARKMASK
, h
->markmask
))
1070 goto nla_put_failure
;
1072 if (nla_put_net32(skb
, IPSET_ATTR_REFERENCES
, htonl(set
->ref
)) ||
1073 nla_put_net32(skb
, IPSET_ATTR_MEMSIZE
, htonl(memsize
)) ||
1074 nla_put_net32(skb
, IPSET_ATTR_ELEMENTS
, htonl(set
->elements
)))
1075 goto nla_put_failure
;
1076 if (unlikely(ip_set_put_flags(skb
, set
)))
1077 goto nla_put_failure
;
1078 nla_nest_end(skb
, nested
);
1085 /* Make possible to run dumping parallel with resizing */
1087 mtype_uref(struct ip_set
*set
, struct netlink_callback
*cb
, bool start
)
1089 struct htype
*h
= set
->data
;
1094 t
= rcu_dereference_bh_nfnl(h
->table
);
1095 atomic_inc(&t
->uref
);
1096 cb
->args
[IPSET_CB_PRIVATE
] = (unsigned long)t
;
1097 rcu_read_unlock_bh();
1098 } else if (cb
->args
[IPSET_CB_PRIVATE
]) {
1099 t
= (struct htable
*)cb
->args
[IPSET_CB_PRIVATE
];
1100 if (atomic_dec_and_test(&t
->uref
) && atomic_read(&t
->ref
)) {
1101 /* Resizing didn't destroy the hash table */
1102 pr_debug("Table destroy by dump: %p\n", t
);
1103 mtype_ahash_destroy(set
, t
, false);
1105 cb
->args
[IPSET_CB_PRIVATE
] = 0;
1109 /* Reply a LIST/SAVE request: dump the elements of the specified set */
1111 mtype_list(const struct ip_set
*set
,
1112 struct sk_buff
*skb
, struct netlink_callback
*cb
)
1114 const struct htable
*t
;
1115 struct nlattr
*atd
, *nested
;
1116 const struct hbucket
*n
;
1117 const struct mtype_elem
*e
;
1118 u32 first
= cb
->args
[IPSET_CB_ARG0
];
1119 /* We assume that one hash bucket fills into one page */
1123 atd
= nla_nest_start(skb
, IPSET_ATTR_ADT
);
1127 pr_debug("list hash set %s\n", set
->name
);
1128 t
= (const struct htable
*)cb
->args
[IPSET_CB_PRIVATE
];
1129 /* Expire may replace a hbucket with another one */
1131 for (; cb
->args
[IPSET_CB_ARG0
] < jhash_size(t
->htable_bits
);
1132 cb
->args
[IPSET_CB_ARG0
]++) {
1134 incomplete
= skb_tail_pointer(skb
);
1135 n
= rcu_dereference(hbucket(t
, cb
->args
[IPSET_CB_ARG0
]));
1136 pr_debug("cb->arg bucket: %lu, t %p n %p\n",
1137 cb
->args
[IPSET_CB_ARG0
], t
, n
);
1140 for (i
= 0; i
< n
->pos
; i
++) {
1141 if (!test_bit(i
, n
->used
))
1143 e
= ahash_data(n
, i
, set
->dsize
);
1144 if (SET_WITH_TIMEOUT(set
) &&
1145 ip_set_timeout_expired(ext_timeout(e
, set
)))
1147 pr_debug("list hash %lu hbucket %p i %u, data %p\n",
1148 cb
->args
[IPSET_CB_ARG0
], n
, i
, e
);
1149 nested
= nla_nest_start(skb
, IPSET_ATTR_DATA
);
1151 if (cb
->args
[IPSET_CB_ARG0
] == first
) {
1152 nla_nest_cancel(skb
, atd
);
1156 goto nla_put_failure
;
1158 if (mtype_data_list(skb
, e
))
1159 goto nla_put_failure
;
1160 if (ip_set_put_extensions(skb
, set
, e
, true))
1161 goto nla_put_failure
;
1162 nla_nest_end(skb
, nested
);
1165 nla_nest_end(skb
, atd
);
1166 /* Set listing finished */
1167 cb
->args
[IPSET_CB_ARG0
] = 0;
1172 nlmsg_trim(skb
, incomplete
);
1173 if (unlikely(first
== cb
->args
[IPSET_CB_ARG0
])) {
1174 pr_warn("Can't list set %s: one bucket does not fit into a message. Please report it!\n",
1176 cb
->args
[IPSET_CB_ARG0
] = 0;
1179 nla_nest_end(skb
, atd
);
1187 IPSET_TOKEN(MTYPE
, _kadt
)(struct ip_set
*set
, const struct sk_buff
*skb
,
1188 const struct xt_action_param
*par
,
1189 enum ipset_adt adt
, struct ip_set_adt_opt
*opt
);
1192 IPSET_TOKEN(MTYPE
, _uadt
)(struct ip_set
*set
, struct nlattr
*tb
[],
1193 enum ipset_adt adt
, u32
*lineno
, u32 flags
,
1196 static const struct ip_set_type_variant mtype_variant
= {
1200 [IPSET_ADD
] = mtype_add
,
1201 [IPSET_DEL
] = mtype_del
,
1202 [IPSET_TEST
] = mtype_test
,
1204 .destroy
= mtype_destroy
,
1205 .flush
= mtype_flush
,
1209 .resize
= mtype_resize
,
1210 .same_set
= mtype_same_set
,
1213 #ifdef IP_SET_EMIT_CREATE
1215 IPSET_TOKEN(HTYPE
, _create
)(struct net
*net
, struct ip_set
*set
,
1216 struct nlattr
*tb
[], u32 flags
)
1218 u32 hashsize
= IPSET_DEFAULT_HASHSIZE
, maxelem
= IPSET_DEFAULT_MAXELEM
;
1219 #ifdef IP_SET_HASH_WITH_MARKMASK
1223 #ifdef IP_SET_HASH_WITH_NETMASK
1230 pr_debug("Create set %s with family %s\n",
1231 set
->name
, set
->family
== NFPROTO_IPV4
? "inet" : "inet6");
1233 #ifdef IP_SET_PROTO_UNDEF
1234 if (set
->family
!= NFPROTO_UNSPEC
)
1235 return -IPSET_ERR_INVALID_FAMILY
;
1237 if (!(set
->family
== NFPROTO_IPV4
|| set
->family
== NFPROTO_IPV6
))
1238 return -IPSET_ERR_INVALID_FAMILY
;
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
;
1247 #ifdef IP_SET_HASH_WITH_MARKMASK
1248 /* Separated condition in order to avoid directive in argument list */
1249 if (unlikely(!ip_set_optattr_netorder(tb
, IPSET_ATTR_MARKMASK
)))
1250 return -IPSET_ERR_PROTOCOL
;
1252 markmask
= 0xffffffff;
1253 if (tb
[IPSET_ATTR_MARKMASK
]) {
1254 markmask
= ntohl(nla_get_be32(tb
[IPSET_ATTR_MARKMASK
]));
1256 return -IPSET_ERR_INVALID_MARKMASK
;
1260 #ifdef IP_SET_HASH_WITH_NETMASK
1261 netmask
= set
->family
== NFPROTO_IPV4
? 32 : 128;
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
;
1272 if (tb
[IPSET_ATTR_HASHSIZE
]) {
1273 hashsize
= ip_set_get_h32(tb
[IPSET_ATTR_HASHSIZE
]);
1274 if (hashsize
< IPSET_MIMINAL_HASHSIZE
)
1275 hashsize
= IPSET_MIMINAL_HASHSIZE
;
1278 if (tb
[IPSET_ATTR_MAXELEM
])
1279 maxelem
= ip_set_get_h32(tb
[IPSET_ATTR_MAXELEM
]);
1282 h
= kzalloc(hsize
, GFP_KERNEL
);
1286 hbits
= htable_bits(hashsize
);
1287 hsize
= htable_size(hbits
);
1292 t
= ip_set_alloc(hsize
);
1297 h
->maxelem
= maxelem
;
1298 #ifdef IP_SET_HASH_WITH_NETMASK
1299 h
->netmask
= netmask
;
1301 #ifdef IP_SET_HASH_WITH_MARKMASK
1302 h
->markmask
= markmask
;
1304 get_random_bytes(&h
->initval
, sizeof(h
->initval
));
1306 t
->htable_bits
= hbits
;
1307 RCU_INIT_POINTER(h
->table
, t
);
1311 #ifndef IP_SET_PROTO_UNDEF
1312 if (set
->family
== NFPROTO_IPV4
) {
1314 set
->variant
= &IPSET_TOKEN(HTYPE
, 4_variant
);
1315 set
->dsize
= ip_set_elem_len(set
, tb
,
1316 sizeof(struct IPSET_TOKEN(HTYPE
, 4_elem
)),
1317 __alignof__(struct IPSET_TOKEN(HTYPE
, 4_elem
)));
1318 #ifndef IP_SET_PROTO_UNDEF
1320 set
->variant
= &IPSET_TOKEN(HTYPE
, 6_variant
);
1321 set
->dsize
= ip_set_elem_len(set
, tb
,
1322 sizeof(struct IPSET_TOKEN(HTYPE
, 6_elem
)),
1323 __alignof__(struct IPSET_TOKEN(HTYPE
, 6_elem
)));
1326 set
->timeout
= IPSET_NO_TIMEOUT
;
1327 if (tb
[IPSET_ATTR_TIMEOUT
]) {
1328 set
->timeout
= ip_set_timeout_uget(tb
[IPSET_ATTR_TIMEOUT
]);
1329 #ifndef IP_SET_PROTO_UNDEF
1330 if (set
->family
== NFPROTO_IPV4
)
1332 IPSET_TOKEN(HTYPE
, 4_gc_init
)(set
,
1333 IPSET_TOKEN(HTYPE
, 4_gc
));
1334 #ifndef IP_SET_PROTO_UNDEF
1336 IPSET_TOKEN(HTYPE
, 6_gc_init
)(set
,
1337 IPSET_TOKEN(HTYPE
, 6_gc
));
1340 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
1341 set
->name
, jhash_size(t
->htable_bits
),
1342 t
->htable_bits
, h
->maxelem
, set
->data
, t
);
1346 #endif /* IP_SET_EMIT_CREATE */