]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - net/netfilter/nft_hash.c
netfilter: nf_tables: add set extensions
[mirror_ubuntu-focal-kernel.git] / net / netfilter / nft_hash.c
CommitLineData
96518518 1/*
ce6eb0d7 2 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
96518518
PM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/list.h>
c50b960c 15#include <linux/log2.h>
96518518
PM
16#include <linux/jhash.h>
17#include <linux/netlink.h>
cfe4a9dd 18#include <linux/rhashtable.h>
96518518
PM
19#include <linux/netfilter.h>
20#include <linux/netfilter/nf_tables.h>
21#include <net/netfilter/nf_tables.h>
22
cfe4a9dd
TG
23/* We target a hash table size of 4, element hint is 75% of final size */
24#define NFT_HASH_ELEMENT_HINT 3
96518518 25
745f5450
PM
26struct nft_hash {
27 struct rhashtable ht;
28};
29
96518518 30struct nft_hash_elem {
cfe4a9dd 31 struct rhash_head node;
ce6eb0d7
PM
32 struct nft_data key;
33 struct nft_data data[];
96518518
PM
34};
35
bfd6e327
PM
36struct nft_hash_cmp_arg {
37 const struct nft_set *set;
38 const struct nft_data *key;
39};
40
fa377321
HX
41static const struct rhashtable_params nft_hash_params;
42
bfd6e327
PM
43static inline u32 nft_hash_key(const void *data, u32 len, u32 seed)
44{
45 const struct nft_hash_cmp_arg *arg = data;
46
47 return jhash(arg->key, len, seed);
48}
49
50static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed)
51{
52 const struct nft_hash_elem *he = data;
53
54 return jhash(&he->key, len, seed);
55}
56
57static inline int nft_hash_cmp(struct rhashtable_compare_arg *arg,
58 const void *ptr)
59{
60 const struct nft_hash_cmp_arg *x = arg->key;
61 const struct nft_hash_elem *he = ptr;
62
63 if (nft_data_cmp(&he->key, x->key, x->set->klen))
64 return 1;
65 return 0;
66}
67
20a69341
PM
68static bool nft_hash_lookup(const struct nft_set *set,
69 const struct nft_data *key,
70 struct nft_data *data)
96518518 71{
745f5450 72 struct nft_hash *priv = nft_set_priv(set);
20a69341 73 const struct nft_hash_elem *he;
bfd6e327
PM
74 struct nft_hash_cmp_arg arg = {
75 .set = set,
76 .key = key,
77 };
ce6eb0d7 78
bfd6e327 79 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
cfe4a9dd
TG
80 if (he && set->flags & NFT_SET_MAP)
81 nft_data_copy(data, he->data);
ce6eb0d7 82
cfe4a9dd 83 return !!he;
96518518
PM
84}
85
20a69341
PM
86static int nft_hash_insert(const struct nft_set *set,
87 const struct nft_set_elem *elem)
96518518 88{
745f5450 89 struct nft_hash *priv = nft_set_priv(set);
20a69341 90 struct nft_hash_elem *he;
bfd6e327
PM
91 struct nft_hash_cmp_arg arg = {
92 .set = set,
93 .key = &elem->key,
94 };
cfe4a9dd 95 unsigned int size;
fa377321 96 int err;
96518518 97
20a69341 98 if (elem->flags != 0)
96518518 99 return -EINVAL;
96518518 100
20a69341
PM
101 size = sizeof(*he);
102 if (set->flags & NFT_SET_MAP)
103 size += sizeof(he->data[0]);
104
105 he = kzalloc(size, GFP_KERNEL);
106 if (he == NULL)
96518518
PM
107 return -ENOMEM;
108
20a69341
PM
109 nft_data_copy(&he->key, &elem->key);
110 if (set->flags & NFT_SET_MAP)
111 nft_data_copy(he->data, &elem->data);
96518518 112
bfd6e327
PM
113 err = rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
114 nft_hash_params);
fa377321
HX
115 if (err)
116 kfree(he);
ce6eb0d7 117
fa377321 118 return err;
96518518
PM
119}
120
ce6eb0d7
PM
121static void nft_hash_elem_destroy(const struct nft_set *set,
122 struct nft_hash_elem *he)
123{
124 nft_data_uninit(&he->key, NFT_DATA_VALUE);
125 if (set->flags & NFT_SET_MAP)
126 nft_data_uninit(he->data, set->dtype);
127 kfree(he);
128}
129
20a69341
PM
130static void nft_hash_remove(const struct nft_set *set,
131 const struct nft_set_elem *elem)
96518518 132{
745f5450 133 struct nft_hash *priv = nft_set_priv(set);
ce6eb0d7 134
745f5450 135 rhashtable_remove_fast(&priv->ht, elem->cookie, nft_hash_params);
ce6eb0d7 136 synchronize_rcu();
897362e4 137 kfree(elem->cookie);
20a69341 138}
96518518 139
20a69341
PM
140static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)
141{
745f5450 142 struct nft_hash *priv = nft_set_priv(set);
fa377321 143 struct nft_hash_elem *he;
bfd6e327
PM
144 struct nft_hash_cmp_arg arg = {
145 .set = set,
146 .key = &elem->key,
147 };
fa377321 148
bfd6e327 149 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
fa377321
HX
150 if (!he)
151 return -ENOENT;
8d24c0b4 152
fa377321
HX
153 elem->cookie = he;
154 elem->flags = 0;
155 if (set->flags & NFT_SET_MAP)
156 nft_data_copy(&elem->data, he->data);
8d24c0b4 157
fa377321 158 return 0;
96518518
PM
159}
160
20a69341
PM
161static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
162 struct nft_set_iter *iter)
96518518 163{
745f5450 164 struct nft_hash *priv = nft_set_priv(set);
20a69341 165 const struct nft_hash_elem *he;
9a776628 166 struct rhashtable_iter hti;
20a69341 167 struct nft_set_elem elem;
9a776628 168 int err;
96518518 169
745f5450 170 err = rhashtable_walk_init(&priv->ht, &hti);
9a776628
HX
171 iter->err = err;
172 if (err)
173 return;
88d6ed15 174
9a776628
HX
175 err = rhashtable_walk_start(&hti);
176 if (err && err != -EAGAIN) {
177 iter->err = err;
178 goto out;
179 }
180
181 while ((he = rhashtable_walk_next(&hti))) {
182 if (IS_ERR(he)) {
183 err = PTR_ERR(he);
184 if (err != -EAGAIN) {
185 iter->err = err;
186 goto out;
187 }
d8bdff59
HX
188
189 continue;
9a776628
HX
190 }
191
192 if (iter->count < iter->skip)
193 goto cont;
20a69341 194
9a776628
HX
195 memcpy(&elem.key, &he->key, sizeof(elem.key));
196 if (set->flags & NFT_SET_MAP)
197 memcpy(&elem.data, he->data, sizeof(elem.data));
198 elem.flags = 0;
199
200 iter->err = iter->fn(ctx, set, iter, &elem);
201 if (iter->err < 0)
202 goto out;
20a69341 203
20a69341 204cont:
9a776628 205 iter->count++;
96518518 206 }
9a776628
HX
207
208out:
209 rhashtable_walk_stop(&hti);
210 rhashtable_walk_exit(&hti);
96518518
PM
211}
212
20a69341
PM
213static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
214{
745f5450 215 return sizeof(struct nft_hash);
cfe4a9dd
TG
216}
217
fa377321 218static const struct rhashtable_params nft_hash_params = {
45d84751 219 .head_offset = offsetof(struct nft_hash_elem, node),
bfd6e327
PM
220 .hashfn = nft_hash_key,
221 .obj_hashfn = nft_hash_obj,
222 .obj_cmpfn = nft_hash_cmp,
45d84751 223 .automatic_shrinking = true,
fa377321
HX
224};
225
20a69341 226static int nft_hash_init(const struct nft_set *set,
c50b960c 227 const struct nft_set_desc *desc,
96518518
PM
228 const struct nlattr * const tb[])
229{
745f5450 230 struct nft_hash *priv = nft_set_priv(set);
fa377321
HX
231 struct rhashtable_params params = nft_hash_params;
232
233 params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT;
45d84751 234 params.key_len = set->klen;
96518518 235
745f5450 236 return rhashtable_init(&priv->ht, &params);
96518518
PM
237}
238
6b6f302c 239static void nft_free_element(void *ptr, void *arg)
96518518 240{
6b6f302c
TG
241 nft_hash_elem_destroy((const struct nft_set *)arg, ptr);
242}
97defe1e 243
6b6f302c
TG
244static void nft_hash_destroy(const struct nft_set *set)
245{
745f5450
PM
246 struct nft_hash *priv = nft_set_priv(set);
247
248 rhashtable_free_and_destroy(&priv->ht, nft_free_element, (void *)set);
96518518
PM
249}
250
c50b960c
PM
251static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
252 struct nft_set_estimate *est)
253{
254 unsigned int esize;
255
256 esize = sizeof(struct nft_hash_elem);
257 if (features & NFT_SET_MAP)
258 esize += FIELD_SIZEOF(struct nft_hash_elem, data[0]);
259
260 if (desc->size) {
745f5450 261 est->size = sizeof(struct nft_hash) +
cfe4a9dd 262 roundup_pow_of_two(desc->size * 4 / 3) *
c50b960c
PM
263 sizeof(struct nft_hash_elem *) +
264 desc->size * esize;
265 } else {
266 /* Resizing happens when the load drops below 30% or goes
267 * above 75%. The average of 52.5% load (approximated by 50%)
268 * is used for the size estimation of the hash buckets,
269 * meaning we calculate two buckets per element.
270 */
271 est->size = esize + 2 * sizeof(struct nft_hash_elem *);
272 }
273
274 est->class = NFT_SET_CLASS_O_1;
275
276 return true;
277}
278
20a69341
PM
279static struct nft_set_ops nft_hash_ops __read_mostly = {
280 .privsize = nft_hash_privsize,
c50b960c 281 .estimate = nft_hash_estimate,
96518518
PM
282 .init = nft_hash_init,
283 .destroy = nft_hash_destroy,
20a69341
PM
284 .get = nft_hash_get,
285 .insert = nft_hash_insert,
286 .remove = nft_hash_remove,
287 .lookup = nft_hash_lookup,
288 .walk = nft_hash_walk,
289 .features = NFT_SET_MAP,
290 .owner = THIS_MODULE,
96518518
PM
291};
292
293static int __init nft_hash_module_init(void)
294{
20a69341 295 return nft_register_set(&nft_hash_ops);
96518518
PM
296}
297
298static void __exit nft_hash_module_exit(void)
299{
20a69341 300 nft_unregister_set(&nft_hash_ops);
96518518
PM
301}
302
303module_init(nft_hash_module_init);
304module_exit(nft_hash_module_exit);
305
306MODULE_LICENSE("GPL");
307MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
20a69341 308MODULE_ALIAS_NFT_SET();