1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (C) 2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
3
4 #ifndef _IP_SET_HASH_GEN_H
5 #define _IP_SET_HASH_GEN_H
6
7 #include <linux/rcupdate.h>
8 #include <linux/rcupdate_wait.h>
9 #include <linux/jhash.h>
10 #include <linux/types.h>
11 #include <linux/netfilter/nfnetlink.h>
12 #include <linux/netfilter/ipset/ip_set.h>
13
14 #define __ipset_dereference(p) \
15 rcu_dereference_protected(p, 1)
16 #define ipset_dereference_nfnl(p) \
17 rcu_dereference_protected(p, \
18 lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET))
19 #define ipset_dereference_set(p, set) \
20 rcu_dereference_protected(p, \
21 lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET) || \
22 lockdep_is_held(&(set)->lock))
23 #define ipset_dereference_bh_nfnl(p) \
24 rcu_dereference_bh_check(p, \
25 lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET))
26
27 /* Hashing which uses arrays to resolve clashing. The hash table is resized
28 * (doubled) when searching becomes too long.
29 * Internally jhash is used with the assumption that the size of the
30 * stored data is a multiple of sizeof(u32).
31 *
32 * Readers and resizing
33 *
34 * Resizing can be triggered by userspace command only, and those
35 * are serialized by the nfnl mutex. During resizing the set is
36 * read-locked, so the only possible concurrent operations are
37 * the kernel side readers. Those must be protected by proper RCU locking.
38 */
39
40 /* Number of elements to store in an initial array block */
41 #define AHASH_INIT_SIZE 2
42 /* Max number of elements to store in an array block */
43 #define AHASH_MAX_SIZE (6 * AHASH_INIT_SIZE)
44 /* Max muber of elements in the array block when tuned */
45 #define AHASH_MAX_TUNED 64
46 #define AHASH_MAX(h) ((h)->bucketsize)
47
48 /* A hash bucket */
49 struct hbucket {
50 struct rcu_head rcu; /* for call_rcu */
51 /* Which positions are used in the array */
52 DECLARE_BITMAP(used, AHASH_MAX_TUNED);
53 u8 size; /* size of the array */
54 u8 pos; /* position of the first free entry */
55 unsigned char value[] /* the array of the values */
56 __aligned(__alignof__(u64));
57 };
58
59 /* Region size for locking == 2^HTABLE_REGION_BITS */
60 #define HTABLE_REGION_BITS 10
61 #define ahash_numof_locks(htable_bits) \
62 ((htable_bits) < HTABLE_REGION_BITS ? 1 \
63 : jhash_size((htable_bits) - HTABLE_REGION_BITS))
64 #define ahash_sizeof_regions(htable_bits) \
65 (ahash_numof_locks(htable_bits) * sizeof(struct ip_set_region))
66 #define ahash_region(n) \
67 ((n) / jhash_size(HTABLE_REGION_BITS))
68 #define ahash_bucket_start(h, htable_bits) \
69 ((htable_bits) < HTABLE_REGION_BITS ? 0 \
70 : (h) * jhash_size(HTABLE_REGION_BITS))
71 #define ahash_bucket_end(h, htable_bits) \
72 ((htable_bits) < HTABLE_REGION_BITS ? jhash_size(htable_bits) \
73 : ((h) + 1) * jhash_size(HTABLE_REGION_BITS))
74
75 struct htable_gc {
76 struct delayed_work dwork;
77 struct ip_set *set; /* Set the gc belongs to */
78 spinlock_t lock; /* Lock to exclude gc and resize */
79 u32 region; /* Last gc run position */
80 };
81
82 /* The hash table: the table size stored here in order to make resizing easy */
83 struct htable {
84 bool resizing; /* Mark ongoing resize */
85 atomic_t uref; /* References for dumping and gc */
86 u8 htable_bits; /* size of hash table == 2^htable_bits */
87 u32 maxelem; /* Maxelem per region */
88 struct list_head ad; /* Resize add|del backlist */
89 struct ip_set_region *hregion; /* Region locks and ext sizes */
90 struct hbucket __rcu *bucket[]; /* hashtable buckets */
91 };
92
93 #define hbucket(h, i) ((h)->bucket[i])
94 #define ext_size(n, dsize) \
95 (sizeof(struct hbucket) + (n) * (dsize))
96
97 #ifndef IPSET_NET_COUNT
98 #define IPSET_NET_COUNT 1
99 #endif
100
101 /* Book-keeping of the prefixes added to the set */
102 struct net_prefix {
103 u8 cidr; /* the cidr value */
104 u32 count; /* number of elements of this cidr */
105 };
106
107 struct net_prefixes {
108 struct rcu_head rcu;
109 u8 len;
110 struct net_prefix nets[] __counted_by(len);
111 };
112
113 /* Compute the hash table size */
114 static size_t
htable_size(u8 hbits)115 htable_size(u8 hbits)
116 {
117 size_t hsize;
118
119 /* We must fit both into u32 in jhash and INT_MAX in kvmalloc_node() */
120 if (hbits > 31)
121 return 0;
122 hsize = jhash_size(hbits);
123 if ((INT_MAX - sizeof(struct htable)) / sizeof(struct hbucket *)
124 < hsize)
125 return 0;
126
127 return hsize * sizeof(struct hbucket *) + sizeof(struct htable);
128 }
129
130 #ifdef IP_SET_HASH_WITH_NETS
131 #if IPSET_NET_COUNT > 1
132 #define __CIDR(cidr, i) (cidr[i])
133 #else
134 #define __CIDR(cidr, i) (cidr)
135 #endif
136 #ifdef IP_SET_HASH_WITH_NETS_PACKED
137 /* When cidr is packed with nomatch, cidr - 1 is stored in the data entry */
138 #define DCIDR_PUT(cidr) ((cidr) - 1)
139 #define DCIDR_GET(cidr, i) (__CIDR(cidr, i) + 1)
140 #else
141 #define DCIDR_PUT(cidr) (cidr)
142 #define DCIDR_GET(cidr, i) __CIDR(cidr, i)
143 #endif
144
145 #define INIT_CIDR(n, host_mask) ({ \
146 const struct net_prefixes *__n = rcu_dereference(n); \
147 DCIDR_PUT((__n)->len ? (__n)->nets[0].cidr : host_mask);\
148 })
149
150 #endif /* IP_SET_HASH_WITH_NETS */
151
152 #define SET_ELEM_EXPIRED(set, d) \
153 (SET_WITH_TIMEOUT(set) && \
154 ip_set_timeout_expired(ext_timeout(d, set)))
155
156 #if defined(IP_SET_HASH_WITH_NETMASK) || defined(IP_SET_HASH_WITH_BITMASK)
157 static const union nf_inet_addr onesmask = {
158 .all[0] = 0xffffffff,
159 .all[1] = 0xffffffff,
160 .all[2] = 0xffffffff,
161 .all[3] = 0xffffffff
162 };
163
164 static const union nf_inet_addr zeromask = {};
165 #endif
166
167 #endif /* _IP_SET_HASH_GEN_H */
168
169 #ifndef MTYPE
170 #error "MTYPE is not defined!"
171 #endif
172
173 #ifndef HTYPE
174 #error "HTYPE is not defined!"
175 #endif
176
177 #ifndef HOST_MASK
178 #error "HOST_MASK is not defined!"
179 #endif
180
181 /* Family dependent templates */
182
183 #undef ahash_data
184 #undef mtype_data_equal
185 #undef mtype_do_data_match
186 #undef mtype_data_set_flags
187 #undef mtype_data_reset_elem
188 #undef mtype_data_reset_flags
189 #undef mtype_data_netmask
190 #undef mtype_data_list
191 #undef mtype_data_next
192 #undef mtype_elem
193
194 #undef mtype_ahash_destroy
195 #undef mtype_ext_cleanup
196 #undef mtype_add_cidr
197 #undef mtype_del_cidr
198 #undef mtype_del_cidr_all
199 #undef mtype_ahash_memsize
200 #undef mtype_flush
201 #undef mtype_destroy
202 #undef mtype_same_set
203 #undef mtype_kadt
204 #undef mtype_uadt
205 #undef mtype_bucket_size
206 #undef mtype_hash_size
207
208 #undef mtype_add
209 #undef mtype_del
210 #undef mtype_test_cidrs
211 #undef mtype_test
212 #undef mtype_uref
213 #undef mtype_resize
214 #undef mtype_ext_size
215 #undef mtype_resize_ad
216 #undef mtype_head
217 #undef mtype_list
218 #undef mtype_gc_do
219 #undef mtype_gc
220 #undef mtype_gc_init
221 #undef mtype_cancel_gc
222 #undef mtype_variant
223 #undef mtype_data_match
224
225 #undef htype
226 #undef HKEY
227
228 #define mtype_data_equal IPSET_TOKEN(MTYPE, _data_equal)
229 #ifdef IP_SET_HASH_WITH_NETS
230 #define mtype_do_data_match IPSET_TOKEN(MTYPE, _do_data_match)
231 #else
232 #define mtype_do_data_match(d) 1
233 #endif
234 #define mtype_data_set_flags IPSET_TOKEN(MTYPE, _data_set_flags)
235 #define mtype_data_reset_elem IPSET_TOKEN(MTYPE, _data_reset_elem)
236 #define mtype_data_reset_flags IPSET_TOKEN(MTYPE, _data_reset_flags)
237 #define mtype_data_netmask IPSET_TOKEN(MTYPE, _data_netmask)
238 #define mtype_data_list IPSET_TOKEN(MTYPE, _data_list)
239 #define mtype_data_next IPSET_TOKEN(MTYPE, _data_next)
240 #define mtype_elem IPSET_TOKEN(MTYPE, _elem)
241
242 #define mtype_ahash_destroy IPSET_TOKEN(MTYPE, _ahash_destroy)
243 #define mtype_ext_cleanup IPSET_TOKEN(MTYPE, _ext_cleanup)
244 #define mtype_add_cidr IPSET_TOKEN(MTYPE, _add_cidr)
245 #define mtype_del_cidr IPSET_TOKEN(MTYPE, _del_cidr)
246 #define mtype_del_cidr_all IPSET_TOKEN(MTYPE, _del_cidr_all)
247 #define mtype_ahash_memsize IPSET_TOKEN(MTYPE, _ahash_memsize)
248 #define mtype_flush IPSET_TOKEN(MTYPE, _flush)
249 #define mtype_destroy IPSET_TOKEN(MTYPE, _destroy)
250 #define mtype_same_set IPSET_TOKEN(MTYPE, _same_set)
251 #define mtype_kadt IPSET_TOKEN(MTYPE, _kadt)
252 #define mtype_uadt IPSET_TOKEN(MTYPE, _uadt)
253 #define mtype_bucket_size IPSET_TOKEN(MTYPE, _bucket_size)
254 #define mtype_hash_size IPSET_TOKEN(MTYPE, _hash_size)
255
256 #define mtype_add IPSET_TOKEN(MTYPE, _add)
257 #define mtype_del IPSET_TOKEN(MTYPE, _del)
258 #define mtype_test_cidrs IPSET_TOKEN(MTYPE, _test_cidrs)
259 #define mtype_test IPSET_TOKEN(MTYPE, _test)
260 #define mtype_uref IPSET_TOKEN(MTYPE, _uref)
261 #define mtype_resize IPSET_TOKEN(MTYPE, _resize)
262 #define mtype_ext_size IPSET_TOKEN(MTYPE, _ext_size)
263 #define mtype_resize_ad IPSET_TOKEN(MTYPE, _resize_ad)
264 #define mtype_head IPSET_TOKEN(MTYPE, _head)
265 #define mtype_list IPSET_TOKEN(MTYPE, _list)
266 #define mtype_gc_do IPSET_TOKEN(MTYPE, _gc_do)
267 #define mtype_gc IPSET_TOKEN(MTYPE, _gc)
268 #define mtype_gc_init IPSET_TOKEN(MTYPE, _gc_init)
269 #define mtype_cancel_gc IPSET_TOKEN(MTYPE, _cancel_gc)
270 #define mtype_variant IPSET_TOKEN(MTYPE, _variant)
271 #define mtype_data_match IPSET_TOKEN(MTYPE, _data_match)
272
273 #ifndef HKEY_DATALEN
274 #define HKEY_DATALEN sizeof(struct mtype_elem)
275 #endif
276
277 #define htype MTYPE
278
279 #define HKEY(data, initval, htable_bits) \
280 ({ \
281 const u32 *__k = (const u32 *)data; \
282 u32 __l = HKEY_DATALEN / sizeof(u32); \
283 \
284 BUILD_BUG_ON(HKEY_DATALEN % sizeof(u32) != 0); \
285 \
286 jhash2(__k, __l, initval) & jhash_mask(htable_bits); \
287 })
288
289 /* The generic hash structure */
290 struct htype {
291 struct htable __rcu *table; /* the hash table */
292 struct net_prefixes __rcu *rnets[IPSET_NET_COUNT]; /* cidr prefixes */
293 struct htable_gc gc; /* gc workqueue */
294 u32 maxelem; /* max elements in the hash */
295 u32 initval; /* random jhash init value */
296 #ifdef IP_SET_HASH_WITH_MARKMASK
297 u32 markmask; /* markmask value for mark mask to store */
298 #endif
299 u8 bucketsize; /* max elements in an array block */
300 #if defined(IP_SET_HASH_WITH_NETMASK) || defined(IP_SET_HASH_WITH_BITMASK)
301 u8 netmask; /* netmask value for subnets to store */
302 union nf_inet_addr bitmask; /* stores bitmask */
303 #endif
304 /* Because 'next' is IPv4/IPv6 dependent, no elements of this
305 * structure and referred in create() may come after 'next'.
306 */
307 struct mtype_elem next; /* temporary storage for uadd */
308 };
309
310 /* ADD|DEL entries saved during resize */
311 struct mtype_resize_ad {
312 struct list_head list;
313 enum ipset_adt ad; /* ADD|DEL element */
314 struct mtype_elem d; /* Element value */
315 struct ip_set_ext ext; /* Extensions for ADD */
316 struct ip_set_ext mext; /* Target extensions for ADD */
317 u32 flags; /* Flags for ADD */
318 };
319
320 #ifdef IP_SET_HASH_WITH_NETS
321 /* Network cidr size book keeping when the hash stores different
322 * sized networks. cidr == real cidr + 1 to support /0.
323 */
324 static int
mtype_add_cidr(struct ip_set * set,struct htype * h,u8 cidr,u8 n)325 mtype_add_cidr(struct ip_set *set, struct htype *h, u8 cidr, u8 n)
326 {
327 struct net_prefixes *nets, *tmp;
328 int i, j, found, len = 0, ret = 0;
329
330 spin_lock_bh(&set->lock);
331 nets = __ipset_dereference(h->rnets[n]);
332 /* Add in increasing prefix order, so larger cidr first */
333 for (i = 0, found = -1; i < nets->len; i++) {
334 if (nets->nets[i].count)
335 len++;
336 if (found != -1) {
337 continue;
338 } else if (nets->nets[i].cidr < cidr) {
339 found = i;
340 } else if (nets->nets[i].cidr == cidr) {
341 nets->nets[i].count++;
342 goto unlock;
343 }
344 }
345 len++;
346 tmp = kzalloc_flex(*tmp, nets, len, GFP_ATOMIC);
347 if (!tmp) {
348 ret = -ENOMEM;
349 goto unlock;
350 }
351
352 tmp->len = len;
353 for (i = 0, j = 0; i < nets->len; i++) {
354 if (i == found) {
355 tmp->nets[j].cidr = cidr;
356 tmp->nets[j++].count = 1;
357 }
358 if (!nets->nets[i].count)
359 continue;
360 tmp->nets[j].cidr = nets->nets[i].cidr;
361 tmp->nets[j++].count = nets->nets[i].count;
362 }
363 if (found == -1) {
364 tmp->nets[j].cidr = cidr;
365 tmp->nets[j].count = 1;
366 }
367 rcu_assign_pointer(h->rnets[n], tmp);
368 kfree_rcu(nets, rcu);
369 unlock:
370 spin_unlock_bh(&set->lock);
371 return ret;
372 }
373
374 static void
mtype_del_cidr(struct ip_set * set,struct htype * h,u8 cidr,u8 n)375 mtype_del_cidr(struct ip_set *set, struct htype *h, u8 cidr, u8 n)
376 {
377 struct net_prefixes *nets, *tmp;
378 u8 i, j, len = 0;
379 int found;
380
381 spin_lock_bh(&set->lock);
382 nets = __ipset_dereference(h->rnets[n]);
383 for (i = 0, found = -1; i < nets->len; i++) {
384 if (nets->nets[i].count)
385 len++;
386 if (nets->nets[i].cidr == cidr)
387 found = i;
388 }
389 if (unlikely(found == -1))
390 goto unlock;
391
392 nets->nets[found].count--;
393 if (nets->nets[found].count)
394 goto unlock;
395 len--;
396 tmp = kzalloc_flex(*tmp, nets, len, GFP_ATOMIC);
397 if (!tmp)
398 /* Leave a hole */
399 goto unlock;
400
401 tmp->len = len;
402 for (i = 0, j = 0; i < nets->len; i++) {
403 if (!nets->nets[i].count || i == found)
404 continue;
405 tmp->nets[j].cidr = nets->nets[i].cidr;
406 tmp->nets[j++].count = nets->nets[i].count;
407 }
408 rcu_assign_pointer(h->rnets[n], tmp);
409 kfree_rcu(nets, rcu);
410 unlock:
411 spin_unlock_bh(&set->lock);
412 }
413 #endif
414
415 static void
mtype_del_cidr_all(struct ip_set * set,struct htype * h,const struct mtype_elem * data)416 mtype_del_cidr_all(struct ip_set *set, struct htype *h, const struct mtype_elem *data)
417 {
418 #ifdef IP_SET_HASH_WITH_NETS
419 int k;
420
421 for (k = 0; k < IPSET_NET_COUNT; k++)
422 mtype_del_cidr(set, h, DCIDR_GET(data->cidr, k), k);
423 #endif
424 }
425
426 /* Calculate the actual memory size of the set data */
427 static size_t
mtype_ahash_memsize(const struct htype * h,const struct htable * t)428 mtype_ahash_memsize(const struct htype *h, const struct htable *t)
429 {
430 return sizeof(*h) + sizeof(*t) + ahash_sizeof_regions(t->htable_bits);
431 }
432
433 /* Get the ith element from the array block n */
434 #define ahash_data(n, i, dsize) \
435 ((struct mtype_elem *)((n)->value + ((i) * (dsize))))
436
437 static void
mtype_ext_cleanup(struct ip_set * set,struct hbucket * n)438 mtype_ext_cleanup(struct ip_set *set, struct hbucket *n)
439 {
440 int i;
441 u8 pos = smp_load_acquire(&n->pos);
442
443 for (i = 0; i < pos; i++)
444 if (test_bit(i, n->used))
445 ip_set_ext_destroy(set, ahash_data(n, i, set->dsize));
446 }
447
448 /* Flush a hash type of set: destroy all elements */
449 static void
mtype_flush(struct ip_set * set)450 mtype_flush(struct ip_set *set)
451 {
452 struct htype *h = set->data;
453 #ifdef IP_SET_HASH_WITH_NETS
454 struct net_prefixes *nets, *tmp;
455 #endif
456 struct htable *t;
457 struct hbucket *n;
458 u32 r, i;
459
460 t = ipset_dereference_nfnl(h->table);
461 for (r = 0; r < ahash_numof_locks(t->htable_bits); r++) {
462 spin_lock_bh(&t->hregion[r].lock);
463 for (i = ahash_bucket_start(r, t->htable_bits);
464 i < ahash_bucket_end(r, t->htable_bits); i++) {
465 n = __ipset_dereference(hbucket(t, i));
466 if (!n)
467 continue;
468 if (set->extensions & IPSET_EXT_DESTROY)
469 mtype_ext_cleanup(set, n);
470 /* FIXME: use slab cache */
471 rcu_assign_pointer(hbucket(t, i), NULL);
472 kfree_rcu(n, rcu);
473 }
474 t->hregion[r].ext_size = 0;
475 t->hregion[r].elements = 0;
476 spin_unlock_bh(&t->hregion[r].lock);
477 }
478 #ifdef IP_SET_HASH_WITH_NETS
479 for (i = 0; i < IPSET_NET_COUNT; i++) {
480 nets = ipset_dereference_nfnl(h->rnets[i]);
481 tmp = kzalloc_obj(*tmp, GFP_ATOMIC);
482 if (!tmp) {
483 u8 j;
484
485 for (j = 0; j < nets->len; j++)
486 nets->nets[j].count = 0;
487 } else {
488 rcu_assign_pointer(h->rnets[i], tmp);
489 kfree_rcu(nets, rcu);
490 }
491 }
492 #endif
493 }
494
495 /* Destroy the hashtable part of the set */
496 static void
mtype_ahash_destroy(struct ip_set * set,struct htable * t,bool ext_destroy)497 mtype_ahash_destroy(struct ip_set *set, struct htable *t, bool ext_destroy)
498 {
499 #ifdef IP_SET_HASH_WITH_NETS
500 struct htype *h = set->data;
501 #endif
502 struct hbucket *n;
503 u32 i;
504
505 for (i = 0; i < jhash_size(t->htable_bits); i++) {
506 n = (__force struct hbucket *)hbucket(t, i);
507 if (!n)
508 continue;
509 if (set->extensions & IPSET_EXT_DESTROY && ext_destroy)
510 mtype_ext_cleanup(set, n);
511 /* FIXME: use slab cache */
512 kfree(n);
513 }
514
515 #ifdef IP_SET_HASH_WITH_NETS
516 if (ext_destroy)
517 for (i = 0; i < IPSET_NET_COUNT; i++)
518 kfree(rcu_dereference_raw(h->rnets[i]));
519 #endif
520 ip_set_free(t->hregion);
521 ip_set_free(t);
522 }
523
524 /* Destroy a hash type of set */
525 static void
mtype_destroy(struct ip_set * set)526 mtype_destroy(struct ip_set *set)
527 {
528 struct htype *h = set->data;
529 struct htable *t = (__force struct htable *)h->table;
530 struct list_head *l, *lt;
531
532 list_for_each_safe(l, lt, &t->ad) {
533 list_del(l);
534 kfree(l);
535 }
536 mtype_ahash_destroy(set, t, true);
537 kfree(h);
538
539 set->data = NULL;
540 }
541
542 static bool
mtype_same_set(const struct ip_set * a,const struct ip_set * b)543 mtype_same_set(const struct ip_set *a, const struct ip_set *b)
544 {
545 const struct htype *x = a->data;
546 const struct htype *y = b->data;
547
548 /* Resizing changes htable_bits, so we ignore it */
549 return x->maxelem == y->maxelem &&
550 a->timeout == b->timeout &&
551 #if defined(IP_SET_HASH_WITH_NETMASK) || defined(IP_SET_HASH_WITH_BITMASK)
552 nf_inet_addr_cmp(&x->bitmask, &y->bitmask) &&
553 #endif
554 #ifdef IP_SET_HASH_WITH_MARKMASK
555 x->markmask == y->markmask &&
556 #endif
557 a->extensions == b->extensions;
558 }
559
560 static void
mtype_gc_do(struct ip_set * set,struct htype * h,struct htable * t,u32 r)561 mtype_gc_do(struct ip_set *set, struct htype *h, struct htable *t, u32 r)
562 {
563 struct hbucket *n, *tmp;
564 struct mtype_elem *data;
565 u32 i, j, d;
566 size_t dsize = set->dsize;
567 u8 pos, htable_bits = t->htable_bits;
568
569 spin_lock_bh(&t->hregion[r].lock);
570 for (i = ahash_bucket_start(r, htable_bits);
571 i < ahash_bucket_end(r, htable_bits); i++) {
572 n = __ipset_dereference(hbucket(t, i));
573 if (!n)
574 continue;
575 pos = smp_load_acquire(&n->pos);
576 for (j = 0, d = 0; j < pos; j++) {
577 if (!test_bit(j, n->used)) {
578 d++;
579 continue;
580 }
581 data = ahash_data(n, j, dsize);
582 if (!ip_set_timeout_expired(ext_timeout(data, set)))
583 continue;
584 pr_debug("expired %u/%u\n", i, j);
585 clear_bit(j, n->used);
586 smp_mb__after_atomic();
587 mtype_del_cidr_all(set, h, data);
588 t->hregion[r].elements--;
589 ip_set_ext_destroy(set, data);
590 d++;
591 }
592 if (d >= AHASH_INIT_SIZE) {
593 if (d >= n->size) {
594 t->hregion[r].ext_size -=
595 ext_size(n->size, dsize);
596 rcu_assign_pointer(hbucket(t, i), NULL);
597 kfree_rcu(n, rcu);
598 continue;
599 }
600 tmp = kzalloc(sizeof(*tmp) +
601 (n->size - AHASH_INIT_SIZE) * dsize,
602 GFP_ATOMIC);
603 if (!tmp)
604 /* Still try to delete expired elements. */
605 continue;
606 tmp->size = n->size - AHASH_INIT_SIZE;
607 for (j = 0, d = 0; j < pos; j++) {
608 if (!test_bit(j, n->used))
609 continue;
610 data = ahash_data(n, j, dsize);
611 memcpy(tmp->value + d * dsize,
612 data, dsize);
613 set_bit(d, tmp->used);
614 d++;
615 }
616 tmp->pos = d;
617 t->hregion[r].ext_size -=
618 ext_size(AHASH_INIT_SIZE, dsize);
619 rcu_assign_pointer(hbucket(t, i), tmp);
620 kfree_rcu(n, rcu);
621 }
622 }
623 spin_unlock_bh(&t->hregion[r].lock);
624 }
625
626 static void
mtype_gc(struct work_struct * work)627 mtype_gc(struct work_struct *work)
628 {
629 struct htable_gc *gc;
630 struct ip_set *set;
631 struct htype *h;
632 struct htable *t;
633 u32 r, numof_locks;
634 unsigned int next_run;
635
636 gc = container_of(work, struct htable_gc, dwork.work);
637 set = gc->set;
638 h = set->data;
639
640 rcu_read_lock_bh();
641 t = rcu_dereference_bh(h->table);
642 atomic_inc(&t->uref);
643 rcu_read_unlock_bh();
644 numof_locks = ahash_numof_locks(t->htable_bits);
645 r = gc->region++;
646 if (r >= numof_locks) {
647 r = gc->region = 0;
648 }
649 next_run = (IPSET_GC_PERIOD(set->timeout) * HZ) / numof_locks;
650 if (next_run < HZ/10)
651 next_run = HZ/10;
652
653 spin_lock_bh(&gc->lock);
654 if (!t->resizing)
655 mtype_gc_do(set, h, t, r);
656 spin_unlock_bh(&gc->lock);
657
658 if (atomic_dec_and_test(&t->uref) && t->resizing) {
659 pr_debug("Table destroy after resize by expire: %p\n", t);
660 mtype_ahash_destroy(set, t, false);
661 }
662
663 queue_delayed_work(system_power_efficient_wq, &gc->dwork, next_run);
664
665 }
666
667 static void
mtype_gc_init(struct htable_gc * gc)668 mtype_gc_init(struct htable_gc *gc)
669 {
670 INIT_DEFERRABLE_WORK(&gc->dwork, mtype_gc);
671 queue_delayed_work(system_power_efficient_wq, &gc->dwork, HZ);
672 }
673
674 static void
mtype_cancel_gc(struct ip_set * set)675 mtype_cancel_gc(struct ip_set *set)
676 {
677 struct htype *h = set->data;
678
679 if (SET_WITH_TIMEOUT(set))
680 disable_delayed_work_sync(&h->gc.dwork);
681 }
682
683 static int
684 mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
685 struct ip_set_ext *mext, u32 flags);
686 static int
687 mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
688 struct ip_set_ext *mext, u32 flags);
689
690 /* Resize a hash: create a new hash table with doubling the hashsize
691 * and inserting the elements to it. Repeat until we succeed or
692 * fail due to memory pressures.
693 */
694 static int
mtype_resize(struct ip_set * set,bool retried)695 mtype_resize(struct ip_set *set, bool retried)
696 {
697 struct htype *h = set->data;
698 struct htable *t, *orig;
699 u8 pos, htable_bits;
700 size_t hsize, dsize = set->dsize;
701 #ifdef IP_SET_HASH_WITH_NETS
702 u8 flags;
703 struct mtype_elem *tmp;
704 #endif
705 struct mtype_elem *data;
706 struct mtype_elem *d;
707 struct hbucket *n, *m;
708 struct list_head *l, *lt;
709 struct mtype_resize_ad *x;
710 u32 i, j, r, nr, key;
711 int ret;
712
713 #ifdef IP_SET_HASH_WITH_NETS
714 tmp = kmalloc(dsize, GFP_KERNEL);
715 if (!tmp)
716 return -ENOMEM;
717 #endif
718 orig = ipset_dereference_bh_nfnl(h->table);
719 htable_bits = orig->htable_bits;
720
721 retry:
722 ret = 0;
723 htable_bits++;
724 if (!htable_bits)
725 goto hbwarn;
726 hsize = htable_size(htable_bits);
727 if (!hsize)
728 goto hbwarn;
729 t = ip_set_alloc(hsize);
730 if (!t) {
731 ret = -ENOMEM;
732 goto out;
733 }
734 t->hregion = ip_set_alloc(ahash_sizeof_regions(htable_bits));
735 if (!t->hregion) {
736 ip_set_free(t);
737 ret = -ENOMEM;
738 goto out;
739 }
740 t->htable_bits = htable_bits;
741 t->maxelem = h->maxelem / ahash_numof_locks(htable_bits);
742 INIT_LIST_HEAD(&t->ad);
743 for (i = 0; i < ahash_numof_locks(htable_bits); i++)
744 spin_lock_init(&t->hregion[i].lock);
745
746 /* There can't be another parallel resizing,
747 * but dumping and kernel side add/del are possible
748 */
749 orig = ipset_dereference_bh_nfnl(h->table);
750 atomic_inc(&orig->uref);
751 spin_lock_bh(&h->gc.lock);
752 orig->resizing = true;
753 spin_unlock_bh(&h->gc.lock);
754 pr_debug("attempt to resize set %s from %u to %u, t %p\n",
755 set->name, orig->htable_bits, htable_bits, orig);
756 for (r = 0; r < ahash_numof_locks(orig->htable_bits); r++) {
757 /* Expire may replace a hbucket with another one */
758 rcu_read_lock_bh();
759 for (i = ahash_bucket_start(r, orig->htable_bits);
760 i < ahash_bucket_end(r, orig->htable_bits); i++) {
761 n = __ipset_dereference(hbucket(orig, i));
762 if (!n)
763 continue;
764 pos = smp_load_acquire(&n->pos);
765 for (j = 0; j < pos; j++) {
766 if (!test_bit_acquire(j, n->used))
767 continue;
768 data = ahash_data(n, j, dsize);
769 if (SET_ELEM_EXPIRED(set, data))
770 continue;
771 #ifdef IP_SET_HASH_WITH_NETS
772 /* We have readers running parallel with us,
773 * so the live data cannot be modified.
774 */
775 flags = 0;
776 memcpy(tmp, data, dsize);
777 data = tmp;
778 mtype_data_reset_flags(data, &flags);
779 #endif
780 key = HKEY(data, h->initval, htable_bits);
781 m = __ipset_dereference(hbucket(t, key));
782 nr = ahash_region(key);
783 if (!m) {
784 m = kzalloc(sizeof(*m) +
785 AHASH_INIT_SIZE * dsize,
786 GFP_ATOMIC);
787 if (!m) {
788 ret = -ENOMEM;
789 goto cleanup;
790 }
791 m->size = AHASH_INIT_SIZE;
792 t->hregion[nr].ext_size +=
793 ext_size(AHASH_INIT_SIZE,
794 dsize);
795 RCU_INIT_POINTER(hbucket(t, key), m);
796 } else if (m->pos >= m->size) {
797 struct hbucket *ht;
798
799 if (m->size >= AHASH_MAX(h)) {
800 ret = -EAGAIN;
801 } else {
802 ht = kzalloc(sizeof(*ht) +
803 (m->size + AHASH_INIT_SIZE)
804 * dsize,
805 GFP_ATOMIC);
806 if (!ht)
807 ret = -ENOMEM;
808 }
809 if (ret < 0)
810 goto cleanup;
811 memcpy(ht, m, sizeof(struct hbucket) +
812 m->size * dsize);
813 ht->size = m->size + AHASH_INIT_SIZE;
814 t->hregion[nr].ext_size +=
815 ext_size(AHASH_INIT_SIZE,
816 dsize);
817 kfree(m);
818 m = ht;
819 RCU_INIT_POINTER(hbucket(t, key), ht);
820 }
821 d = ahash_data(m, m->pos, dsize);
822 memcpy(d, data, dsize);
823 set_bit(m->pos++, m->used);
824 t->hregion[nr].elements++;
825 #ifdef IP_SET_HASH_WITH_NETS
826 mtype_data_reset_flags(d, &flags);
827 #endif
828 }
829 }
830 rcu_read_unlock_bh();
831 }
832
833 /* There can't be any other writer. */
834 rcu_assign_pointer(h->table, t);
835
836 /* Give time to other readers of the set */
837 synchronize_rcu();
838
839 pr_debug("set %s resized from %u (%p) to %u (%p)\n", set->name,
840 orig->htable_bits, orig, t->htable_bits, t);
841 /* Add/delete elements processed by the SET target during resize.
842 * Kernel-side add cannot trigger a resize and userspace actions
843 * are serialized by the mutex.
844 */
845 list_for_each_safe(l, lt, &orig->ad) {
846 x = list_entry(l, struct mtype_resize_ad, list);
847 if (x->ad == IPSET_ADD) {
848 mtype_add(set, &x->d, &x->ext, &x->mext, x->flags);
849 } else {
850 mtype_del(set, &x->d, NULL, NULL, 0);
851 }
852 list_del(l);
853 kfree(l);
854 }
855 /* If there's nobody else using the table, destroy it */
856 if (atomic_dec_and_test(&orig->uref)) {
857 pr_debug("Table destroy by resize %p\n", orig);
858 mtype_ahash_destroy(set, orig, false);
859 }
860
861 out:
862 #ifdef IP_SET_HASH_WITH_NETS
863 kfree(tmp);
864 #endif
865 return ret;
866
867 cleanup:
868 rcu_read_unlock_bh();
869 spin_lock_bh(&h->gc.lock);
870 orig->resizing = false;
871 spin_unlock_bh(&h->gc.lock);
872 /* Make sure parallel readers see that orig->resizing is false
873 * before we decrement uref */
874 synchronize_rcu();
875 atomic_dec(&orig->uref);
876 mtype_ahash_destroy(set, t, false);
877 if (ret == -EAGAIN)
878 goto retry;
879
880 /* Cleanup the backlog of ADD/DEL elements */
881 spin_lock_bh(&set->lock);
882 list_for_each_safe(l, lt, &orig->ad) {
883 list_del(l);
884 kfree(l);
885 }
886 spin_unlock_bh(&set->lock);
887 goto out;
888
889 hbwarn:
890 /* In case we have plenty of memory :-) */
891 pr_warn("Cannot increase the hashsize of set %s further\n", set->name);
892 ret = -IPSET_ERR_HASH_FULL;
893 goto out;
894 }
895
896 /* Get the current number of elements and ext_size in the set */
897 static void
mtype_ext_size(struct ip_set * set,u32 * elements,size_t * ext_size)898 mtype_ext_size(struct ip_set *set, u32 *elements, size_t *ext_size)
899 {
900 struct htype *h = set->data;
901 const struct htable *t;
902 struct hbucket *n;
903 struct mtype_elem *data;
904 u32 i, j, r;
905 u8 pos;
906
907 t = rcu_dereference_bh(h->table);
908 for (r = 0; r < ahash_numof_locks(t->htable_bits); r++) {
909 for (i = ahash_bucket_start(r, t->htable_bits);
910 i < ahash_bucket_end(r, t->htable_bits); i++) {
911 n = rcu_dereference_bh(hbucket(t, i));
912 if (!n)
913 continue;
914 pos = smp_load_acquire(&n->pos);
915 for (j = 0; j < pos; j++) {
916 if (!test_bit_acquire(j, n->used))
917 continue;
918 data = ahash_data(n, j, set->dsize);
919 if (!SET_ELEM_EXPIRED(set, data))
920 (*elements)++;
921 }
922 }
923 *ext_size += t->hregion[r].ext_size;
924 }
925 }
926
927 /* Add an element to a hash and update the internal counters when succeeded,
928 * otherwise report the proper error code.
929 */
930 static int
mtype_add(struct ip_set * set,void * value,const struct ip_set_ext * ext,struct ip_set_ext * mext,u32 flags)931 mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
932 struct ip_set_ext *mext, u32 flags)
933 {
934 struct htype *h = set->data;
935 struct htable *t;
936 const struct mtype_elem *d = value;
937 struct mtype_elem *data;
938 struct hbucket *n, *old = ERR_PTR(-ENOENT);
939 int i, j = -1, ret;
940 bool flag_exist = flags & IPSET_FLAG_EXIST;
941 bool deleted = false, forceadd = false, reuse = false;
942 u32 r, key, multi = 0, elements, maxelem;
943 u8 npos = 0;
944
945 rcu_read_lock_bh();
946 t = rcu_dereference_bh(h->table);
947 key = HKEY(value, h->initval, t->htable_bits);
948 r = ahash_region(key);
949 atomic_inc(&t->uref);
950 rcu_read_unlock_bh();
951 elements = t->hregion[r].elements;
952 maxelem = t->maxelem;
953 if (elements >= maxelem) {
954 u32 e;
955 if (SET_WITH_TIMEOUT(set))
956 mtype_gc_do(set, h, t, r);
957 maxelem = h->maxelem;
958 elements = 0;
959 for (e = 0; e < ahash_numof_locks(t->htable_bits); e++)
960 elements += t->hregion[e].elements;
961 if (elements >= maxelem && SET_WITH_FORCEADD(set))
962 forceadd = true;
963 }
964
965 spin_lock_bh(&t->hregion[r].lock);
966 n = rcu_dereference_bh(hbucket(t, key));
967 if (!n) {
968 if (forceadd || elements >= maxelem)
969 goto set_full;
970 old = NULL;
971 n = kzalloc(sizeof(*n) + AHASH_INIT_SIZE * set->dsize,
972 GFP_ATOMIC);
973 if (!n) {
974 ret = -ENOMEM;
975 goto unlock;
976 }
977 n->size = AHASH_INIT_SIZE;
978 t->hregion[r].ext_size +=
979 ext_size(AHASH_INIT_SIZE, set->dsize);
980 goto copy_elem;
981 }
982 npos = smp_load_acquire(&n->pos);
983 for (i = 0; i < npos; i++) {
984 if (!test_bit(i, n->used)) {
985 /* Reuse first deleted entry */
986 if (j == -1) {
987 deleted = reuse = true;
988 j = i;
989 }
990 continue;
991 }
992 data = ahash_data(n, i, set->dsize);
993 if (mtype_data_equal(data, d, &multi)) {
994 if (flag_exist || SET_ELEM_EXPIRED(set, data)) {
995 /* Just the extensions could be overwritten */
996 j = i;
997 goto overwrite_extensions;
998 }
999 ret = -IPSET_ERR_EXIST;
1000 goto unlock;
1001 }
1002 /* Reuse first timed out entry */
1003 if (SET_ELEM_EXPIRED(set, data) && j == -1) {
1004 j = i;
1005 reuse = true;
1006 }
1007 }
1008 if (reuse || forceadd) {
1009 if (j == -1)
1010 j = 0;
1011 data = ahash_data(n, j, set->dsize);
1012 if (!deleted) {
1013 mtype_del_cidr_all(set, h, data);
1014 ip_set_ext_destroy(set, data);
1015 t->hregion[r].elements--;
1016 }
1017 goto copy_data;
1018 }
1019 if (elements >= maxelem)
1020 goto set_full;
1021 /* Create a new slot */
1022 if (npos >= n->size) {
1023 #ifdef IP_SET_HASH_WITH_MULTI
1024 if (h->bucketsize >= AHASH_MAX_TUNED)
1025 goto set_full;
1026 else if (h->bucketsize <= multi)
1027 h->bucketsize += AHASH_INIT_SIZE;
1028 #endif
1029 if (n->size >= AHASH_MAX(h)) {
1030 /* Trigger rehashing */
1031 mtype_data_next(&h->next, d);
1032 ret = -EAGAIN;
1033 goto resize;
1034 }
1035 old = n;
1036 n = kzalloc(sizeof(*n) +
1037 (old->size + AHASH_INIT_SIZE) * set->dsize,
1038 GFP_ATOMIC);
1039 if (!n) {
1040 ret = -ENOMEM;
1041 goto unlock;
1042 }
1043 memcpy(n, old, sizeof(struct hbucket) +
1044 old->size * set->dsize);
1045 n->size = old->size + AHASH_INIT_SIZE;
1046 t->hregion[r].ext_size +=
1047 ext_size(AHASH_INIT_SIZE, set->dsize);
1048 }
1049
1050 copy_elem:
1051 j = npos++;
1052 data = ahash_data(n, j, set->dsize);
1053 copy_data:
1054 t->hregion[r].elements++;
1055 #ifdef IP_SET_HASH_WITH_NETS
1056 for (i = 0; i < IPSET_NET_COUNT; i++)
1057 mtype_add_cidr(set, h, DCIDR_GET(d->cidr, i), i);
1058 #endif
1059 memcpy(data, d, sizeof(struct mtype_elem));
1060 overwrite_extensions:
1061 #ifdef IP_SET_HASH_WITH_NETS
1062 mtype_data_set_flags(data, flags);
1063 #endif
1064 if (SET_WITH_COUNTER(set))
1065 ip_set_init_counter(ext_counter(data, set), ext);
1066 if (SET_WITH_COMMENT(set) && !ext->target)
1067 ip_set_init_comment(set, ext_comment(data, set), ext);
1068 if (SET_WITH_SKBINFO(set))
1069 ip_set_init_skbinfo(ext_skbinfo(data, set), ext);
1070 /* Must come last for the case when timed out entry is reused */
1071 if (SET_WITH_TIMEOUT(set))
1072 ip_set_timeout_set(ext_timeout(data, set), ext->timeout);
1073 smp_mb__before_atomic();
1074 /* Ensure all data writes are visible before updating position */
1075 smp_store_release(&n->pos, npos);
1076 set_bit(j, n->used);
1077 if (old != ERR_PTR(-ENOENT)) {
1078 rcu_assign_pointer(hbucket(t, key), n);
1079 if (old)
1080 kfree_rcu(old, rcu);
1081 }
1082 ret = 0;
1083 resize:
1084 spin_unlock_bh(&t->hregion[r].lock);
1085 if (t->resizing && ext && ext->target) {
1086 /* Resize is in process and kernel side add, save values */
1087 struct mtype_resize_ad *x;
1088
1089 x = kzalloc_obj(struct mtype_resize_ad, GFP_ATOMIC);
1090 if (!x)
1091 /* Don't bother */
1092 goto out;
1093 x->ad = IPSET_ADD;
1094 memcpy(&x->d, value, sizeof(struct mtype_elem));
1095 memcpy(&x->ext, ext, sizeof(struct ip_set_ext));
1096 memcpy(&x->mext, mext, sizeof(struct ip_set_ext));
1097 x->flags = flags;
1098 spin_lock_bh(&set->lock);
1099 list_add_tail(&x->list, &t->ad);
1100 spin_unlock_bh(&set->lock);
1101 }
1102 goto out;
1103
1104 set_full:
1105 if (net_ratelimit())
1106 pr_warn("Set %s is full, maxelem %u reached\n",
1107 set->name, maxelem);
1108 ret = -IPSET_ERR_HASH_FULL;
1109 unlock:
1110 spin_unlock_bh(&t->hregion[r].lock);
1111 out:
1112 if (atomic_dec_and_test(&t->uref) && t->resizing) {
1113 pr_debug("Table destroy after resize by add: %p\n", t);
1114 mtype_ahash_destroy(set, t, false);
1115 }
1116 return ret;
1117 }
1118
1119 /* Delete an element from the hash and free up space if possible.
1120 */
1121 static int
mtype_del(struct ip_set * set,void * value,const struct ip_set_ext * ext,struct ip_set_ext * mext,u32 flags)1122 mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
1123 struct ip_set_ext *mext, u32 flags)
1124 {
1125 struct htype *h = set->data;
1126 struct htable *t;
1127 const struct mtype_elem *d = value;
1128 struct mtype_elem *data;
1129 struct hbucket *n;
1130 struct mtype_resize_ad *x = NULL;
1131 int i, j, k, r, ret = -IPSET_ERR_EXIST;
1132 u32 key, multi = 0;
1133 size_t dsize = set->dsize;
1134 u8 pos;
1135
1136 /* Userspace add and resize is excluded by the mutex.
1137 * Kernespace add does not trigger resize.
1138 */
1139 rcu_read_lock_bh();
1140 t = rcu_dereference_bh(h->table);
1141 key = HKEY(value, h->initval, t->htable_bits);
1142 r = ahash_region(key);
1143 atomic_inc(&t->uref);
1144 rcu_read_unlock_bh();
1145
1146 spin_lock_bh(&t->hregion[r].lock);
1147 n = rcu_dereference_bh(hbucket(t, key));
1148 if (!n)
1149 goto out;
1150 pos = smp_load_acquire(&n->pos);
1151 for (i = 0, k = 0; i < pos; i++) {
1152 if (!test_bit(i, n->used)) {
1153 k++;
1154 continue;
1155 }
1156 data = ahash_data(n, i, dsize);
1157 if (!mtype_data_equal(data, d, &multi))
1158 continue;
1159 if (SET_ELEM_EXPIRED(set, data))
1160 goto out;
1161
1162 ret = 0;
1163 clear_bit(i, n->used);
1164 smp_mb__after_atomic();
1165 if (i + 1 == pos)
1166 smp_store_release(&n->pos, --pos);
1167 t->hregion[r].elements--;
1168 mtype_del_cidr_all(set, h, d);
1169 ip_set_ext_destroy(set, data);
1170
1171 if (t->resizing && ext && ext->target) {
1172 /* Resize is in process and kernel side del,
1173 * save values
1174 */
1175 x = kzalloc_obj(struct mtype_resize_ad, GFP_ATOMIC);
1176 if (x) {
1177 x->ad = IPSET_DEL;
1178 memcpy(&x->d, value,
1179 sizeof(struct mtype_elem));
1180 x->flags = flags;
1181 }
1182 }
1183 for (; i < pos; i++) {
1184 if (!test_bit(i, n->used))
1185 k++;
1186 }
1187 if (k == pos) {
1188 t->hregion[r].ext_size -= ext_size(n->size, dsize);
1189 rcu_assign_pointer(hbucket(t, key), NULL);
1190 kfree_rcu(n, rcu);
1191 } else if (k >= AHASH_INIT_SIZE) {
1192 struct hbucket *tmp = kzalloc(sizeof(*tmp) +
1193 (n->size - AHASH_INIT_SIZE) * dsize,
1194 GFP_ATOMIC);
1195 if (!tmp)
1196 goto out;
1197 tmp->size = n->size - AHASH_INIT_SIZE;
1198 for (j = 0, k = 0; j < pos; j++) {
1199 if (!test_bit(j, n->used))
1200 continue;
1201 data = ahash_data(n, j, dsize);
1202 memcpy(tmp->value + k * dsize, data, dsize);
1203 set_bit(k, tmp->used);
1204 k++;
1205 }
1206 tmp->pos = k;
1207 t->hregion[r].ext_size -=
1208 ext_size(AHASH_INIT_SIZE, dsize);
1209 rcu_assign_pointer(hbucket(t, key), tmp);
1210 kfree_rcu(n, rcu);
1211 }
1212 goto out;
1213 }
1214
1215 out:
1216 spin_unlock_bh(&t->hregion[r].lock);
1217 if (x) {
1218 spin_lock_bh(&set->lock);
1219 list_add(&x->list, &t->ad);
1220 spin_unlock_bh(&set->lock);
1221 }
1222 if (atomic_dec_and_test(&t->uref) && t->resizing) {
1223 pr_debug("Table destroy after resize by del: %p\n", t);
1224 mtype_ahash_destroy(set, t, false);
1225 }
1226 return ret;
1227 }
1228
1229 static int
mtype_data_match(struct mtype_elem * data,const struct ip_set_ext * ext,struct ip_set_ext * mext,struct ip_set * set,u32 flags)1230 mtype_data_match(struct mtype_elem *data, const struct ip_set_ext *ext,
1231 struct ip_set_ext *mext, struct ip_set *set, u32 flags)
1232 {
1233 if (!ip_set_match_extensions(set, ext, mext, flags, data))
1234 return 0;
1235 /* nomatch entries return -ENOTEMPTY */
1236 return mtype_do_data_match(data);
1237 }
1238
1239 #ifdef IP_SET_HASH_WITH_NETS
1240 /* Special test function which takes into account the different network
1241 * sizes added to the set
1242 */
1243 static int
mtype_test_cidrs(struct ip_set * set,struct mtype_elem * d,const struct ip_set_ext * ext,struct ip_set_ext * mext,u32 flags)1244 mtype_test_cidrs(struct ip_set *set, struct mtype_elem *d,
1245 const struct ip_set_ext *ext,
1246 struct ip_set_ext *mext, u32 flags)
1247 {
1248 struct htype *h = set->data;
1249 struct htable *t = rcu_dereference_bh(h->table);
1250 struct net_prefixes *nets0;
1251 struct hbucket *n;
1252 struct mtype_elem *data;
1253 #if IPSET_NET_COUNT == 2
1254 struct net_prefixes *nets1;
1255 struct mtype_elem orig = *d;
1256 int ret, i, j, k;
1257 #else
1258 int ret, i, j;
1259 #endif
1260 u32 key, multi = 0;
1261 u8 pos;
1262
1263 pr_debug("test by nets\n");
1264 rcu_read_lock_bh();
1265 nets0 = rcu_dereference_bh(h->rnets[0]);
1266 #if IPSET_NET_COUNT == 2
1267 nets1 = rcu_dereference_bh(h->rnets[1]);
1268 #endif
1269 for (j = 0; j < nets0->len && !multi; j++) {
1270 if (!nets0->nets[j].count)
1271 continue;
1272 #if IPSET_NET_COUNT == 2
1273 mtype_data_reset_elem(d, &orig);
1274 mtype_data_netmask(d, nets0->nets[j].cidr, false);
1275 for (k = 0; k < nets1->len && !multi; k++) {
1276 if (!nets1->nets[k].count)
1277 continue;
1278 mtype_data_netmask(d, nets1->nets[k].cidr, true);
1279 #else
1280 mtype_data_netmask(d, nets0->nets[j].cidr);
1281 #endif
1282 key = HKEY(d, h->initval, t->htable_bits);
1283 n = rcu_dereference_bh(hbucket(t, key));
1284 if (!n)
1285 continue;
1286 pos = smp_load_acquire(&n->pos);
1287 for (i = 0; i < pos; i++) {
1288 if (!test_bit_acquire(i, n->used))
1289 continue;
1290 data = ahash_data(n, i, set->dsize);
1291 if (!mtype_data_equal(data, d, &multi))
1292 continue;
1293 ret = mtype_data_match(data, ext, mext, set, flags);
1294 if (ret != 0)
1295 goto unlock;
1296 #ifdef IP_SET_HASH_WITH_MULTI
1297 /* No match, reset multiple match flag */
1298 multi = 0;
1299 #endif
1300 }
1301 #if IPSET_NET_COUNT == 2
1302 }
1303 #endif
1304 }
1305 ret = 0;
1306 unlock:
1307 rcu_read_unlock_bh();
1308 return ret;
1309 }
1310 #endif
1311
1312 /* Test whether the element is added to the set */
1313 static int
mtype_test(struct ip_set * set,void * value,const struct ip_set_ext * ext,struct ip_set_ext * mext,u32 flags)1314 mtype_test(struct ip_set *set, void *value, const struct ip_set_ext *ext,
1315 struct ip_set_ext *mext, u32 flags)
1316 {
1317 struct htype *h = set->data;
1318 struct htable *t;
1319 struct mtype_elem *d = value;
1320 struct hbucket *n;
1321 struct mtype_elem *data;
1322 int i, ret = 0;
1323 u32 key, multi = 0;
1324 u8 pos;
1325
1326 rcu_read_lock_bh();
1327 t = rcu_dereference_bh(h->table);
1328 #ifdef IP_SET_HASH_WITH_NETS
1329 /* If we test an IP address and not a network address,
1330 * try all possible network sizes
1331 */
1332 for (i = 0; i < IPSET_NET_COUNT; i++)
1333 if (DCIDR_GET(d->cidr, i) != HOST_MASK)
1334 break;
1335 if (i == IPSET_NET_COUNT) {
1336 ret = mtype_test_cidrs(set, d, ext, mext, flags);
1337 goto out;
1338 }
1339 #endif
1340
1341 key = HKEY(d, h->initval, t->htable_bits);
1342 n = rcu_dereference_bh(hbucket(t, key));
1343 if (!n) {
1344 ret = 0;
1345 goto out;
1346 }
1347 pos = smp_load_acquire(&n->pos);
1348 for (i = 0; i < pos; i++) {
1349 if (!test_bit_acquire(i, n->used))
1350 continue;
1351 data = ahash_data(n, i, set->dsize);
1352 if (!mtype_data_equal(data, d, &multi))
1353 continue;
1354 ret = mtype_data_match(data, ext, mext, set, flags);
1355 if (ret != 0)
1356 goto out;
1357 }
1358 out:
1359 rcu_read_unlock_bh();
1360 return ret;
1361 }
1362
mtype_hash_size(const struct htype * h)1363 static u32 mtype_hash_size(const struct htype *h)
1364 {
1365 const struct htable *t;
1366 u8 htable_bits;
1367
1368 rcu_read_lock();
1369 t = rcu_dereference(h->table);
1370 htable_bits = t->htable_bits;
1371 rcu_read_unlock();
1372
1373 return jhash_size(htable_bits);
1374 }
1375
mtype_bucket_size(const struct htype * h)1376 static u32 mtype_bucket_size(const struct htype *h)
1377 {
1378 return h->bucketsize;
1379 }
1380
1381 /* Reply a HEADER request: fill out the header part of the set */
1382 static int
mtype_head(struct ip_set * set,struct sk_buff * skb)1383 mtype_head(struct ip_set *set, struct sk_buff *skb)
1384 {
1385 struct htype *h = set->data;
1386 const struct htable *t;
1387 struct nlattr *nested;
1388 size_t memsize;
1389 u32 elements = 0;
1390 size_t ext_size = 0;
1391
1392 rcu_read_lock_bh();
1393 t = rcu_dereference_bh(h->table);
1394 mtype_ext_size(set, &elements, &ext_size);
1395 memsize = mtype_ahash_memsize(h, t) + ext_size + atomic64_read(&set->ext_size);
1396 rcu_read_unlock_bh();
1397
1398 nested = nla_nest_start(skb, IPSET_ATTR_DATA);
1399 if (!nested)
1400 goto nla_put_failure;
1401
1402 if (nla_put_net32(skb, IPSET_ATTR_HASHSIZE, htonl(mtype_hash_size(h))))
1403 goto nla_put_failure;
1404 if (nla_put_net32(skb, IPSET_ATTR_MAXELEM, htonl(h->maxelem)))
1405 goto nla_put_failure;
1406 #ifdef IP_SET_HASH_WITH_BITMASK
1407 /* if netmask is set to anything other than HOST_MASK we know that the user supplied netmask
1408 * and not bitmask. These two are mutually exclusive. */
1409 if (h->netmask == HOST_MASK && !nf_inet_addr_cmp(&onesmask, &h->bitmask)) {
1410 if (set->family == NFPROTO_IPV4) {
1411 if (nla_put_ipaddr4(skb, IPSET_ATTR_BITMASK, h->bitmask.ip))
1412 goto nla_put_failure;
1413 } else if (set->family == NFPROTO_IPV6) {
1414 if (nla_put_ipaddr6(skb, IPSET_ATTR_BITMASK, &h->bitmask.in6))
1415 goto nla_put_failure;
1416 }
1417 }
1418 #endif
1419 #ifdef IP_SET_HASH_WITH_NETMASK
1420 if (h->netmask != HOST_MASK && nla_put_u8(skb, IPSET_ATTR_NETMASK, h->netmask))
1421 goto nla_put_failure;
1422 #endif
1423 #ifdef IP_SET_HASH_WITH_MARKMASK
1424 if (nla_put_u32(skb, IPSET_ATTR_MARKMASK, h->markmask))
1425 goto nla_put_failure;
1426 #endif
1427 if (set->flags & IPSET_CREATE_FLAG_BUCKETSIZE) {
1428 if (nla_put_u8(skb, IPSET_ATTR_BUCKETSIZE, mtype_bucket_size(h)))
1429 goto nla_put_failure;
1430 if (nla_put_net32(skb, IPSET_ATTR_INITVAL, htonl(h->initval)))
1431 goto nla_put_failure;
1432 }
1433 if (nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref)) ||
1434 nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)) ||
1435 nla_put_net32(skb, IPSET_ATTR_ELEMENTS, htonl(elements)))
1436 goto nla_put_failure;
1437 if (unlikely(ip_set_put_flags(skb, set)))
1438 goto nla_put_failure;
1439 nla_nest_end(skb, nested);
1440
1441 return 0;
1442 nla_put_failure:
1443 return -EMSGSIZE;
1444 }
1445
1446 /* Make possible to run dumping parallel with resizing */
1447 static void
mtype_uref(struct ip_set * set,struct netlink_callback * cb,bool start)1448 mtype_uref(struct ip_set *set, struct netlink_callback *cb, bool start)
1449 {
1450 struct htype *h = set->data;
1451 struct htable *t;
1452
1453 if (start) {
1454 rcu_read_lock_bh();
1455 t = ipset_dereference_bh_nfnl(h->table);
1456 atomic_inc(&t->uref);
1457 cb->args[IPSET_CB_PRIVATE] = (unsigned long)t;
1458 rcu_read_unlock_bh();
1459 } else if (cb->args[IPSET_CB_PRIVATE]) {
1460 t = (struct htable *)cb->args[IPSET_CB_PRIVATE];
1461 if (atomic_dec_and_test(&t->uref) && t->resizing) {
1462 pr_debug("Table destroy after resize "
1463 " by dump: %p\n", t);
1464 mtype_ahash_destroy(set, t, false);
1465 }
1466 cb->args[IPSET_CB_PRIVATE] = 0;
1467 }
1468 }
1469
1470 /* Reply a LIST/SAVE request: dump the elements of the specified set */
1471 static int
mtype_list(const struct ip_set * set,struct sk_buff * skb,struct netlink_callback * cb)1472 mtype_list(const struct ip_set *set,
1473 struct sk_buff *skb, struct netlink_callback *cb)
1474 {
1475 const struct htable *t;
1476 struct nlattr *atd, *nested;
1477 const struct hbucket *n;
1478 const struct mtype_elem *e;
1479 u32 first = cb->args[IPSET_CB_ARG0];
1480 /* We assume that one hash bucket fills into one page */
1481 void *incomplete;
1482 int i, ret = 0;
1483 u8 pos;
1484
1485 atd = nla_nest_start(skb, IPSET_ATTR_ADT);
1486 if (!atd)
1487 return -EMSGSIZE;
1488
1489 pr_debug("list hash set %s\n", set->name);
1490 t = (const struct htable *)cb->args[IPSET_CB_PRIVATE];
1491 /* Expire may replace a hbucket with another one */
1492 rcu_read_lock();
1493 for (; cb->args[IPSET_CB_ARG0] < jhash_size(t->htable_bits);
1494 cb->args[IPSET_CB_ARG0]++) {
1495 cond_resched_rcu();
1496 incomplete = skb_tail_pointer(skb);
1497 n = rcu_dereference(hbucket(t, cb->args[IPSET_CB_ARG0]));
1498 pr_debug("cb->arg bucket: %lu, t %p n %p\n",
1499 cb->args[IPSET_CB_ARG0], t, n);
1500 if (!n)
1501 continue;
1502 pos = smp_load_acquire(&n->pos);
1503 for (i = 0; i < pos; i++) {
1504 if (!test_bit_acquire(i, n->used))
1505 continue;
1506 e = ahash_data(n, i, set->dsize);
1507 if (SET_ELEM_EXPIRED(set, e))
1508 continue;
1509 pr_debug("list hash %lu hbucket %p i %u, data %p\n",
1510 cb->args[IPSET_CB_ARG0], n, i, e);
1511 nested = nla_nest_start(skb, IPSET_ATTR_DATA);
1512 if (!nested) {
1513 if (cb->args[IPSET_CB_ARG0] == first) {
1514 nla_nest_cancel(skb, atd);
1515 ret = -EMSGSIZE;
1516 goto out;
1517 }
1518 goto nla_put_failure;
1519 }
1520 if (mtype_data_list(skb, e))
1521 goto nla_put_failure;
1522 if (ip_set_put_extensions(skb, set, e, true))
1523 goto nla_put_failure;
1524 nla_nest_end(skb, nested);
1525 }
1526 }
1527 nla_nest_end(skb, atd);
1528 /* Set listing finished */
1529 cb->args[IPSET_CB_ARG0] = 0;
1530
1531 goto out;
1532
1533 nla_put_failure:
1534 nlmsg_trim(skb, incomplete);
1535 if (unlikely(first == cb->args[IPSET_CB_ARG0])) {
1536 pr_warn("Can't list set %s: one bucket does not fit into a message. Please report it!\n",
1537 set->name);
1538 cb->args[IPSET_CB_ARG0] = 0;
1539 ret = -EMSGSIZE;
1540 } else {
1541 nla_nest_end(skb, atd);
1542 }
1543 out:
1544 rcu_read_unlock();
1545 return ret;
1546 }
1547
1548 static int
1549 IPSET_TOKEN(MTYPE, _kadt)(struct ip_set *set, const struct sk_buff *skb,
1550 const struct xt_action_param *par,
1551 enum ipset_adt adt, struct ip_set_adt_opt *opt);
1552
1553 static int
1554 IPSET_TOKEN(MTYPE, _uadt)(struct ip_set *set, struct nlattr *tb[],
1555 enum ipset_adt adt, u32 *lineno, u32 flags,
1556 bool retried);
1557
1558 static const struct ip_set_type_variant mtype_variant = {
1559 .kadt = mtype_kadt,
1560 .uadt = mtype_uadt,
1561 .adt = {
1562 [IPSET_ADD] = mtype_add,
1563 [IPSET_DEL] = mtype_del,
1564 [IPSET_TEST] = mtype_test,
1565 },
1566 .destroy = mtype_destroy,
1567 .flush = mtype_flush,
1568 .head = mtype_head,
1569 .list = mtype_list,
1570 .uref = mtype_uref,
1571 .resize = mtype_resize,
1572 .same_set = mtype_same_set,
1573 .cancel_gc = mtype_cancel_gc,
1574 .region_lock = true,
1575 };
1576
1577 #ifdef IP_SET_EMIT_CREATE
1578 static int
IPSET_TOKEN(HTYPE,_create)1579 IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
1580 struct nlattr *tb[], u32 flags)
1581 {
1582 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
1583 #ifdef IP_SET_HASH_WITH_MARKMASK
1584 u32 markmask;
1585 #endif
1586 u8 hbits;
1587 #if defined(IP_SET_HASH_WITH_NETMASK) || defined(IP_SET_HASH_WITH_BITMASK)
1588 int ret __attribute__((unused)) = 0;
1589 u8 netmask = set->family == NFPROTO_IPV4 ? 32 : 128;
1590 union nf_inet_addr bitmask = onesmask;
1591 #endif
1592 #ifdef IP_SET_HASH_WITH_NETS
1593 struct net_prefixes *nets;
1594 #endif
1595 size_t hsize;
1596 struct htype *h;
1597 struct htable *t;
1598 u32 i;
1599
1600 pr_debug("Create set %s with family %s\n",
1601 set->name, set->family == NFPROTO_IPV4 ? "inet" : "inet6");
1602
1603 #ifdef IP_SET_PROTO_UNDEF
1604 if (set->family != NFPROTO_UNSPEC)
1605 return -IPSET_ERR_INVALID_FAMILY;
1606 #else
1607 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
1608 return -IPSET_ERR_INVALID_FAMILY;
1609 #endif
1610
1611 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
1612 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
1613 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
1614 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
1615 return -IPSET_ERR_PROTOCOL;
1616
1617 #ifdef IP_SET_HASH_WITH_MARKMASK
1618 /* Separated condition in order to avoid directive in argument list */
1619 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_MARKMASK)))
1620 return -IPSET_ERR_PROTOCOL;
1621
1622 markmask = 0xffffffff;
1623 if (tb[IPSET_ATTR_MARKMASK]) {
1624 markmask = ntohl(nla_get_be32(tb[IPSET_ATTR_MARKMASK]));
1625 if (markmask == 0)
1626 return -IPSET_ERR_INVALID_MARKMASK;
1627 }
1628 #endif
1629
1630 #ifdef IP_SET_HASH_WITH_NETMASK
1631 if (tb[IPSET_ATTR_NETMASK]) {
1632 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
1633
1634 if ((set->family == NFPROTO_IPV4 && netmask > 32) ||
1635 (set->family == NFPROTO_IPV6 && netmask > 128) ||
1636 netmask == 0)
1637 return -IPSET_ERR_INVALID_NETMASK;
1638
1639 /* we convert netmask to bitmask and store it */
1640 if (set->family == NFPROTO_IPV4)
1641 bitmask.ip = ip_set_netmask(netmask);
1642 else
1643 ip6_netmask(&bitmask, netmask);
1644 }
1645 #endif
1646
1647 #ifdef IP_SET_HASH_WITH_BITMASK
1648 if (tb[IPSET_ATTR_BITMASK]) {
1649 /* bitmask and netmask do the same thing, allow only one of these options */
1650 if (tb[IPSET_ATTR_NETMASK])
1651 return -IPSET_ERR_BITMASK_NETMASK_EXCL;
1652
1653 if (set->family == NFPROTO_IPV4) {
1654 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_BITMASK], &bitmask.ip);
1655 if (ret || !bitmask.ip)
1656 return -IPSET_ERR_INVALID_NETMASK;
1657 } else if (set->family == NFPROTO_IPV6) {
1658 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_BITMASK], &bitmask);
1659 if (ret || ipv6_addr_any(&bitmask.in6))
1660 return -IPSET_ERR_INVALID_NETMASK;
1661 }
1662
1663 if (nf_inet_addr_cmp(&bitmask, &zeromask))
1664 return -IPSET_ERR_INVALID_NETMASK;
1665 }
1666 #endif
1667
1668 if (tb[IPSET_ATTR_HASHSIZE]) {
1669 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
1670 if (hashsize < IPSET_MIMINAL_HASHSIZE)
1671 hashsize = IPSET_MIMINAL_HASHSIZE;
1672 }
1673
1674 if (tb[IPSET_ATTR_MAXELEM])
1675 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
1676
1677 #ifdef IP_SET_PROTO_UNDEF
1678 hsize = sizeof(struct htype);
1679 #else
1680 hsize = set->family == NFPROTO_IPV6 ?
1681 sizeof(struct IPSET_TOKEN(HTYPE, 6)) :
1682 sizeof(struct IPSET_TOKEN(HTYPE, 4));
1683 #endif
1684 h = kzalloc(hsize, GFP_KERNEL);
1685 if (!h)
1686 return -ENOMEM;
1687
1688 /* Compute htable_bits from the user input parameter hashsize.
1689 * Assume that hashsize == 2^htable_bits,
1690 * otherwise round up to the first 2^n value.
1691 */
1692 hbits = fls(hashsize - 1);
1693 hsize = htable_size(hbits);
1694 if (hsize == 0)
1695 goto free_h;
1696 t = ip_set_alloc(hsize);
1697 if (!t)
1698 goto free_h;
1699 t->hregion = ip_set_alloc(ahash_sizeof_regions(hbits));
1700 if (!t->hregion)
1701 goto free_t;
1702 #ifdef IP_SET_HASH_WITH_NETS
1703 for (i = 0; i < IPSET_NET_COUNT; i++) {
1704 nets = kzalloc_obj(*nets);
1705 if (!nets) {
1706 while (i > 0)
1707 kfree(rcu_dereference_raw(h->rnets[--i]));
1708 goto free_hregion;
1709 }
1710 RCU_INIT_POINTER(h->rnets[i], nets);
1711 }
1712 #endif
1713 h->gc.set = set;
1714 spin_lock_init(&h->gc.lock);
1715 for (i = 0; i < ahash_numof_locks(hbits); i++)
1716 spin_lock_init(&t->hregion[i].lock);
1717 h->maxelem = maxelem;
1718 #if defined(IP_SET_HASH_WITH_NETMASK) || defined(IP_SET_HASH_WITH_BITMASK)
1719 h->bitmask = bitmask;
1720 h->netmask = netmask;
1721 #endif
1722 #ifdef IP_SET_HASH_WITH_MARKMASK
1723 h->markmask = markmask;
1724 #endif
1725 if (tb[IPSET_ATTR_INITVAL])
1726 h->initval = ntohl(nla_get_be32(tb[IPSET_ATTR_INITVAL]));
1727 else
1728 get_random_bytes(&h->initval, sizeof(h->initval));
1729 h->bucketsize = AHASH_MAX_SIZE;
1730 if (tb[IPSET_ATTR_BUCKETSIZE]) {
1731 h->bucketsize = nla_get_u8(tb[IPSET_ATTR_BUCKETSIZE]);
1732 if (h->bucketsize < AHASH_INIT_SIZE)
1733 h->bucketsize = AHASH_INIT_SIZE;
1734 else if (h->bucketsize > AHASH_MAX_SIZE)
1735 h->bucketsize = AHASH_MAX_SIZE;
1736 else if (h->bucketsize % 2)
1737 h->bucketsize += 1;
1738 }
1739 t->htable_bits = hbits;
1740 t->maxelem = h->maxelem / ahash_numof_locks(hbits);
1741 INIT_LIST_HEAD(&t->ad);
1742 RCU_INIT_POINTER(h->table, t);
1743 set->data = h;
1744
1745 #ifndef IP_SET_PROTO_UNDEF
1746 if (set->family == NFPROTO_IPV4) {
1747 #endif
1748 set->variant = &IPSET_TOKEN(HTYPE, 4_variant);
1749 set->dsize = ip_set_elem_len(set, tb,
1750 sizeof(struct IPSET_TOKEN(HTYPE, 4_elem)),
1751 __alignof__(struct IPSET_TOKEN(HTYPE, 4_elem)));
1752 #ifndef IP_SET_PROTO_UNDEF
1753 } else {
1754 set->variant = &IPSET_TOKEN(HTYPE, 6_variant);
1755 set->dsize = ip_set_elem_len(set, tb,
1756 sizeof(struct IPSET_TOKEN(HTYPE, 6_elem)),
1757 __alignof__(struct IPSET_TOKEN(HTYPE, 6_elem)));
1758 }
1759 #endif
1760 set->timeout = IPSET_NO_TIMEOUT;
1761 if (tb[IPSET_ATTR_TIMEOUT]) {
1762 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
1763 #ifndef IP_SET_PROTO_UNDEF
1764 if (set->family == NFPROTO_IPV4)
1765 #endif
1766 IPSET_TOKEN(HTYPE, 4_gc_init)(&h->gc);
1767 #ifndef IP_SET_PROTO_UNDEF
1768 else
1769 IPSET_TOKEN(HTYPE, 6_gc_init)(&h->gc);
1770 #endif
1771 }
1772 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
1773 set->name, mtype_hash_size(h),
1774 t->htable_bits, h->maxelem, set->data, t);
1775
1776 return 0;
1777
1778 #ifdef IP_SET_HASH_WITH_NETS
1779 free_hregion:
1780 ip_set_free(t->hregion);
1781 #endif
1782 free_t:
1783 ip_set_free(t);
1784 free_h:
1785 kfree(h);
1786 return -ENOMEM;
1787 }
1788 #endif /* IP_SET_EMIT_CREATE */
1789
1790 #undef HKEY_DATALEN
1791