1
2
3
4
5
6
7
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/list.h>
13#include <linux/netlink.h>
14#include <linux/netfilter.h>
15#include <linux/netfilter/nf_tables.h>
16#include <net/netfilter/nf_tables.h>
17
18struct nft_bitmap_elem {
19 struct list_head head;
20 struct nft_set_ext ext;
21};
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48struct nft_bitmap {
49 struct list_head list;
50 u16 bitmap_size;
51 u8 bitmap[];
52};
53
54static inline void nft_bitmap_location(const struct nft_set *set,
55 const void *key,
56 u32 *idx, u32 *off)
57{
58 u32 k;
59
60 if (set->klen == 2)
61 k = *(u16 *)key;
62 else
63 k = *(u8 *)key;
64 k <<= 1;
65
66 *idx = k / BITS_PER_BYTE;
67 *off = k % BITS_PER_BYTE;
68}
69
70
71
72
73static inline bool
74nft_bitmap_active(const u8 *bitmap, u32 idx, u32 off, u8 genmask)
75{
76 return (bitmap[idx] & (0x3 << off)) & (genmask << off);
77}
78
79static bool nft_bitmap_lookup(const struct net *net, const struct nft_set *set,
80 const u32 *key, const struct nft_set_ext **ext)
81{
82 const struct nft_bitmap *priv = nft_set_priv(set);
83 u8 genmask = nft_genmask_cur(net);
84 u32 idx, off;
85
86 nft_bitmap_location(set, key, &idx, &off);
87 *ext = NULL;
88
89 return nft_bitmap_active(priv->bitmap, idx, off, genmask);
90}
91
92static struct nft_bitmap_elem *
93nft_bitmap_elem_find(const struct nft_set *set, struct nft_bitmap_elem *this,
94 u8 genmask)
95{
96 const struct nft_bitmap *priv = nft_set_priv(set);
97 struct nft_bitmap_elem *be;
98
99 list_for_each_entry_rcu(be, &priv->list, head) {
100 if (memcmp(nft_set_ext_key(&be->ext),
101 nft_set_ext_key(&this->ext), set->klen) ||
102 !nft_set_elem_active(&be->ext, genmask))
103 continue;
104
105 return be;
106 }
107 return NULL;
108}
109
110static void *nft_bitmap_get(const struct net *net, const struct nft_set *set,
111 const struct nft_set_elem *elem, unsigned int flags)
112{
113 const struct nft_bitmap *priv = nft_set_priv(set);
114 u8 genmask = nft_genmask_cur(net);
115 struct nft_bitmap_elem *be;
116
117 list_for_each_entry_rcu(be, &priv->list, head) {
118 if (memcmp(nft_set_ext_key(&be->ext), elem->key.val.data, set->klen) ||
119 !nft_set_elem_active(&be->ext, genmask))
120 continue;
121
122 return be;
123 }
124 return ERR_PTR(-ENOENT);
125}
126
127static int nft_bitmap_insert(const struct net *net, const struct nft_set *set,
128 const struct nft_set_elem *elem,
129 struct nft_set_ext **ext)
130{
131 struct nft_bitmap *priv = nft_set_priv(set);
132 struct nft_bitmap_elem *new = elem->priv, *be;
133 u8 genmask = nft_genmask_next(net);
134 u32 idx, off;
135
136 be = nft_bitmap_elem_find(set, new, genmask);
137 if (be) {
138 *ext = &be->ext;
139 return -EEXIST;
140 }
141
142 nft_bitmap_location(set, nft_set_ext_key(&new->ext), &idx, &off);
143
144 priv->bitmap[idx] |= (genmask << off);
145 list_add_tail_rcu(&new->head, &priv->list);
146
147 return 0;
148}
149
150static void nft_bitmap_remove(const struct net *net,
151 const struct nft_set *set,
152 const struct nft_set_elem *elem)
153{
154 struct nft_bitmap *priv = nft_set_priv(set);
155 struct nft_bitmap_elem *be = elem->priv;
156 u8 genmask = nft_genmask_next(net);
157 u32 idx, off;
158
159 nft_bitmap_location(set, nft_set_ext_key(&be->ext), &idx, &off);
160
161 priv->bitmap[idx] &= ~(genmask << off);
162 list_del_rcu(&be->head);
163}
164
165static void nft_bitmap_activate(const struct net *net,
166 const struct nft_set *set,
167 const struct nft_set_elem *elem)
168{
169 struct nft_bitmap *priv = nft_set_priv(set);
170 struct nft_bitmap_elem *be = elem->priv;
171 u8 genmask = nft_genmask_next(net);
172 u32 idx, off;
173
174 nft_bitmap_location(set, nft_set_ext_key(&be->ext), &idx, &off);
175
176 priv->bitmap[idx] |= (genmask << off);
177 nft_set_elem_change_active(net, set, &be->ext);
178}
179
180static bool nft_bitmap_flush(const struct net *net,
181 const struct nft_set *set, void *_be)
182{
183 struct nft_bitmap *priv = nft_set_priv(set);
184 u8 genmask = nft_genmask_next(net);
185 struct nft_bitmap_elem *be = _be;
186 u32 idx, off;
187
188 nft_bitmap_location(set, nft_set_ext_key(&be->ext), &idx, &off);
189
190 priv->bitmap[idx] &= ~(genmask << off);
191 nft_set_elem_change_active(net, set, &be->ext);
192
193 return true;
194}
195
196static void *nft_bitmap_deactivate(const struct net *net,
197 const struct nft_set *set,
198 const struct nft_set_elem *elem)
199{
200 struct nft_bitmap *priv = nft_set_priv(set);
201 struct nft_bitmap_elem *this = elem->priv, *be;
202 u8 genmask = nft_genmask_next(net);
203 u32 idx, off;
204
205 nft_bitmap_location(set, elem->key.val.data, &idx, &off);
206
207 be = nft_bitmap_elem_find(set, this, genmask);
208 if (!be)
209 return NULL;
210
211
212 priv->bitmap[idx] &= ~(genmask << off);
213 nft_set_elem_change_active(net, set, &be->ext);
214
215 return be;
216}
217
218static void nft_bitmap_walk(const struct nft_ctx *ctx,
219 struct nft_set *set,
220 struct nft_set_iter *iter)
221{
222 const struct nft_bitmap *priv = nft_set_priv(set);
223 struct nft_bitmap_elem *be;
224 struct nft_set_elem elem;
225
226 list_for_each_entry_rcu(be, &priv->list, head) {
227 if (iter->count < iter->skip)
228 goto cont;
229 if (!nft_set_elem_active(&be->ext, iter->genmask))
230 goto cont;
231
232 elem.priv = be;
233
234 iter->err = iter->fn(ctx, set, iter, &elem);
235
236 if (iter->err < 0)
237 return;
238cont:
239 iter->count++;
240 }
241}
242
243
244
245
246
247static inline u32 nft_bitmap_size(u32 klen)
248{
249 return ((2 << ((klen * BITS_PER_BYTE) - 1)) / BITS_PER_BYTE) << 1;
250}
251
252static inline u64 nft_bitmap_total_size(u32 klen)
253{
254 return sizeof(struct nft_bitmap) + nft_bitmap_size(klen);
255}
256
257static u64 nft_bitmap_privsize(const struct nlattr * const nla[],
258 const struct nft_set_desc *desc)
259{
260 u32 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
261
262 return nft_bitmap_total_size(klen);
263}
264
265static int nft_bitmap_init(const struct nft_set *set,
266 const struct nft_set_desc *desc,
267 const struct nlattr * const nla[])
268{
269 struct nft_bitmap *priv = nft_set_priv(set);
270
271 INIT_LIST_HEAD(&priv->list);
272 priv->bitmap_size = nft_bitmap_size(set->klen);
273
274 return 0;
275}
276
277static void nft_bitmap_destroy(const struct nft_set *set)
278{
279 struct nft_bitmap *priv = nft_set_priv(set);
280 struct nft_bitmap_elem *be, *n;
281
282 list_for_each_entry_safe(be, n, &priv->list, head)
283 nft_set_elem_destroy(set, be, true);
284}
285
286static bool nft_bitmap_estimate(const struct nft_set_desc *desc, u32 features,
287 struct nft_set_estimate *est)
288{
289
290 if (desc->klen > 2)
291 return false;
292
293 est->size = nft_bitmap_total_size(desc->klen);
294 est->lookup = NFT_SET_CLASS_O_1;
295 est->space = NFT_SET_CLASS_O_1;
296
297 return true;
298}
299
300struct nft_set_type nft_set_bitmap_type __read_mostly = {
301 .owner = THIS_MODULE,
302 .ops = {
303 .privsize = nft_bitmap_privsize,
304 .elemsize = offsetof(struct nft_bitmap_elem, ext),
305 .estimate = nft_bitmap_estimate,
306 .init = nft_bitmap_init,
307 .destroy = nft_bitmap_destroy,
308 .insert = nft_bitmap_insert,
309 .remove = nft_bitmap_remove,
310 .deactivate = nft_bitmap_deactivate,
311 .flush = nft_bitmap_flush,
312 .activate = nft_bitmap_activate,
313 .lookup = nft_bitmap_lookup,
314 .walk = nft_bitmap_walk,
315 .get = nft_bitmap_get,
316 },
317};
318