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