1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2017 Pablo Neira Ayuso <pablo@netfilter.org>
4 */
5
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/list.h>
10 #include <linux/netlink.h>
11 #include <linux/netfilter.h>
12 #include <linux/netfilter/nf_tables.h>
13 #include <net/netfilter/nf_tables_core.h>
14
15 struct nft_bitmap_elem {
16 struct nft_elem_priv priv;
17 struct list_head head;
18 struct nft_set_ext ext;
19 };
20
21 /* This bitmap uses two bits to represent one element. These two bits determine
22 * the element state in the current and the future generation.
23 *
24 * An element can be in three states. The generation cursor is represented using
25 * the ^ character, note that this cursor shifts on every successful transaction.
26 * If no transaction is going on, we observe all elements are in the following
27 * state:
28 *
29 * 11 = this element is active in the current generation. In case of no updates,
30 * ^ it stays active in the next generation.
31 * 00 = this element is inactive in the current generation. In case of no
32 * ^ updates, it stays inactive in the next generation.
33 *
34 * On transaction handling, we observe these two temporary states:
35 *
36 * 01 = this element is inactive in the current generation and it becomes active
37 * ^ in the next one. This happens when the element is inserted but commit
38 * path has not yet been executed yet, so activation is still pending. On
39 * transaction abortion, the element is removed.
40 * 10 = this element is active in the current generation and it becomes inactive
41 * ^ in the next one. This happens when the element is deactivated but commit
42 * path has not yet been executed yet, so removal is still pending. On
43 * transaction abortion, the next generation bit is reset to go back to
44 * restore its previous state.
45 */
46 struct nft_bitmap {
47 struct list_head list;
48 u16 bitmap_size;
49 u8 bitmap[];
50 };
51
nft_bitmap_location(const struct nft_set * set,const void * key,u32 * idx,u32 * off)52 static inline void nft_bitmap_location(const struct nft_set *set,
53 const void *key,
54 u32 *idx, u32 *off)
55 {
56 u32 k;
57
58 if (set->klen == 2)
59 k = *(u16 *)key;
60 else
61 k = *(u8 *)key;
62 k <<= 1;
63
64 *idx = k / BITS_PER_BYTE;
65 *off = k % BITS_PER_BYTE;
66 }
67
68 /* Fetch the two bits that represent the element and check if it is active based
69 * on the generation mask.
70 */
71 static inline bool
nft_bitmap_active(const u8 * bitmap,u32 idx,u32 off,u8 genmask)72 nft_bitmap_active(const u8 *bitmap, u32 idx, u32 off, u8 genmask)
73 {
74 return (bitmap[idx] & (0x3 << off)) & (genmask << off);
75 }
76
77 INDIRECT_CALLABLE_SCOPE
78 const struct nft_set_ext *
nft_bitmap_lookup(const struct net * net,const struct nft_set * set,const u32 * key)79 nft_bitmap_lookup(const struct net *net, const struct nft_set *set,
80 const u32 *key)
81 {
82 const struct nft_bitmap *priv = nft_set_priv(set);
83 static const struct nft_set_ext found;
84 u8 genmask = nft_genmask_cur(net);
85 u32 idx, off;
86
87 nft_bitmap_location(set, key, &idx, &off);
88
89 if (nft_bitmap_active(priv->bitmap, idx, off, genmask))
90 return &found;
91
92 return NULL;
93 }
94
95 static struct nft_bitmap_elem *
nft_bitmap_elem_find(const struct nft_set * set,struct nft_bitmap_elem * this,u8 genmask)96 nft_bitmap_elem_find(const struct nft_set *set, struct nft_bitmap_elem *this,
97 u8 genmask)
98 {
99 const struct nft_bitmap *priv = nft_set_priv(set);
100 struct nft_bitmap_elem *be;
101
102 list_for_each_entry_rcu(be, &priv->list, head) {
103 if (memcmp(nft_set_ext_key(&be->ext),
104 nft_set_ext_key(&this->ext), set->klen) ||
105 !nft_set_elem_active(&be->ext, genmask))
106 continue;
107
108 return be;
109 }
110 return NULL;
111 }
112
113 static struct nft_elem_priv *
nft_bitmap_get(const struct net * net,const struct nft_set * set,const struct nft_set_elem * elem,unsigned int flags)114 nft_bitmap_get(const struct net *net, const struct nft_set *set,
115 const struct nft_set_elem *elem, unsigned int flags)
116 {
117 const struct nft_bitmap *priv = nft_set_priv(set);
118 u8 genmask = nft_genmask_cur(net);
119 struct nft_bitmap_elem *be;
120
121 list_for_each_entry_rcu(be, &priv->list, head) {
122 if (memcmp(nft_set_ext_key(&be->ext), elem->key.val.data, set->klen) ||
123 !nft_set_elem_active(&be->ext, genmask))
124 continue;
125
126 return &be->priv;
127 }
128 return ERR_PTR(-ENOENT);
129 }
130
nft_bitmap_insert(const struct net * net,const struct nft_set * set,const struct nft_set_elem * elem,struct nft_elem_priv ** elem_priv)131 static int nft_bitmap_insert(const struct net *net, const struct nft_set *set,
132 const struct nft_set_elem *elem,
133 struct nft_elem_priv **elem_priv)
134 {
135 struct nft_bitmap_elem *new = nft_elem_priv_cast(elem->priv), *be;
136 struct nft_bitmap *priv = nft_set_priv(set);
137 u8 genmask = nft_genmask_next(net);
138 u32 idx, off;
139
140 be = nft_bitmap_elem_find(set, new, genmask);
141 if (be) {
142 *elem_priv = &be->priv;
143 return -EEXIST;
144 }
145
146 nft_bitmap_location(set, nft_set_ext_key(&new->ext), &idx, &off);
147 /* Enter 01 state. */
148 priv->bitmap[idx] |= (genmask << off);
149 list_add_tail_rcu(&new->head, &priv->list);
150
151 return 0;
152 }
153
nft_bitmap_remove(const struct net * net,const struct nft_set * set,struct nft_elem_priv * elem_priv)154 static void nft_bitmap_remove(const struct net *net, const struct nft_set *set,
155 struct nft_elem_priv *elem_priv)
156 {
157 struct nft_bitmap_elem *be = nft_elem_priv_cast(elem_priv);
158 struct nft_bitmap *priv = nft_set_priv(set);
159 u8 genmask = nft_genmask_next(net);
160 u32 idx, off;
161
162 nft_bitmap_location(set, nft_set_ext_key(&be->ext), &idx, &off);
163 /* Enter 00 state. */
164 priv->bitmap[idx] &= ~(genmask << off);
165 list_del_rcu(&be->head);
166 }
167
nft_bitmap_activate(const struct net * net,const struct nft_set * set,struct nft_elem_priv * elem_priv)168 static void nft_bitmap_activate(const struct net *net,
169 const struct nft_set *set,
170 struct nft_elem_priv *elem_priv)
171 {
172 struct nft_bitmap_elem *be = nft_elem_priv_cast(elem_priv);
173 struct nft_bitmap *priv = nft_set_priv(set);
174 u8 genmask = nft_genmask_next(net);
175 u32 idx, off;
176
177 nft_bitmap_location(set, nft_set_ext_key(&be->ext), &idx, &off);
178 /* Enter 11 state. */
179 priv->bitmap[idx] |= (genmask << off);
180 nft_clear(net, &be->ext);
181 }
182
nft_bitmap_flush(const struct net * net,const struct nft_set * set,struct nft_elem_priv * elem_priv)183 static void nft_bitmap_flush(const struct net *net,
184 const struct nft_set *set,
185 struct nft_elem_priv *elem_priv)
186 {
187 struct nft_bitmap_elem *be = nft_elem_priv_cast(elem_priv);
188 struct nft_bitmap *priv = nft_set_priv(set);
189 u8 genmask = nft_genmask_next(net);
190 u32 idx, off;
191
192 nft_bitmap_location(set, nft_set_ext_key(&be->ext), &idx, &off);
193 /* Enter 10 state, similar to deactivation. */
194 priv->bitmap[idx] &= ~(genmask << off);
195 nft_set_elem_change_active(net, set, &be->ext);
196 }
197
198 static struct nft_elem_priv *
nft_bitmap_deactivate(const struct net * net,const struct nft_set * set,const struct nft_set_elem * elem)199 nft_bitmap_deactivate(const struct net *net, const struct nft_set *set,
200 const struct nft_set_elem *elem)
201 {
202 struct nft_bitmap_elem *this = nft_elem_priv_cast(elem->priv), *be;
203 struct nft_bitmap *priv = nft_set_priv(set);
204 u8 genmask = nft_genmask_next(net);
205 u32 idx, off;
206
207 nft_bitmap_location(set, elem->key.val.data, &idx, &off);
208
209 be = nft_bitmap_elem_find(set, this, genmask);
210 if (!be)
211 return NULL;
212
213 /* Enter 10 state. */
214 priv->bitmap[idx] &= ~(genmask << off);
215 nft_set_elem_change_active(net, set, &be->ext);
216
217 return &be->priv;
218 }
219
nft_bitmap_walk(const struct nft_ctx * ctx,struct nft_set * set,struct nft_set_iter * iter)220 static void nft_bitmap_walk(const struct nft_ctx *ctx,
221 struct nft_set *set,
222 struct nft_set_iter *iter)
223 {
224 const struct nft_bitmap *priv = nft_set_priv(set);
225 struct nft_bitmap_elem *be;
226
227 list_for_each_entry_rcu(be, &priv->list, head) {
228 if (iter->count < iter->skip)
229 goto cont;
230
231 iter->err = iter->fn(ctx, set, iter, &be->priv);
232
233 if (iter->err < 0)
234 return;
235 cont:
236 iter->count++;
237 }
238 }
239
240 /* The bitmap size is pow(2, key length in bits) / bits per byte. This is
241 * multiplied by two since each element takes two bits. For 8 bit keys, the
242 * bitmap consumes 66 bytes. For 16 bit keys, 16388 bytes.
243 */
nft_bitmap_size(u32 klen)244 static inline u32 nft_bitmap_size(u32 klen)
245 {
246 return ((2 << ((klen * BITS_PER_BYTE) - 1)) / BITS_PER_BYTE) << 1;
247 }
248
nft_bitmap_total_size(u32 klen)249 static inline u64 nft_bitmap_total_size(u32 klen)
250 {
251 return sizeof(struct nft_bitmap) + nft_bitmap_size(klen);
252 }
253
nft_bitmap_privsize(const struct nlattr * const nla[],const struct nft_set_desc * desc)254 static u64 nft_bitmap_privsize(const struct nlattr * const nla[],
255 const struct nft_set_desc *desc)
256 {
257 u32 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
258
259 return nft_bitmap_total_size(klen);
260 }
261
nft_bitmap_init(const struct nft_set * set,const struct nft_set_desc * desc,const struct nlattr * const nla[])262 static int nft_bitmap_init(const struct nft_set *set,
263 const struct nft_set_desc *desc,
264 const struct nlattr * const nla[])
265 {
266 struct nft_bitmap *priv = nft_set_priv(set);
267
268 BUILD_BUG_ON(offsetof(struct nft_bitmap_elem, priv) != 0);
269
270 INIT_LIST_HEAD(&priv->list);
271 priv->bitmap_size = nft_bitmap_size(set->klen);
272
273 return 0;
274 }
275
nft_bitmap_destroy(const struct nft_ctx * ctx,const struct nft_set * set)276 static void nft_bitmap_destroy(const struct nft_ctx *ctx,
277 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 nf_tables_set_elem_destroy(ctx, set, &be->priv);
284 }
285
nft_bitmap_estimate(const struct nft_set_desc * desc,u32 features,struct nft_set_estimate * est)286 static bool nft_bitmap_estimate(const struct nft_set_desc *desc, u32 features,
287 struct nft_set_estimate *est)
288 {
289 /* Make sure bitmaps we don't get bitmaps larger than 16 Kbytes. */
290 if (desc->klen > 2)
291 return false;
292 else if (desc->expr)
293 return false;
294
295 est->size = nft_bitmap_total_size(desc->klen);
296 est->lookup = NFT_SET_CLASS_O_1;
297 est->space = NFT_SET_CLASS_O_1;
298
299 return true;
300 }
301
302 const struct nft_set_type nft_set_bitmap_type = {
303 .ops = {
304 .privsize = nft_bitmap_privsize,
305 .elemsize = offsetof(struct nft_bitmap_elem, ext),
306 .estimate = nft_bitmap_estimate,
307 .init = nft_bitmap_init,
308 .destroy = nft_bitmap_destroy,
309 .insert = nft_bitmap_insert,
310 .remove = nft_bitmap_remove,
311 .deactivate = nft_bitmap_deactivate,
312 .flush = nft_bitmap_flush,
313 .activate = nft_bitmap_activate,
314 .lookup = nft_bitmap_lookup,
315 .walk = nft_bitmap_walk,
316 .get = nft_bitmap_get,
317 },
318 };
319