2 * Copyright (c) 2017 Pablo Neira Ayuso <pablo@netfilter.org>
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.
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>
18 /* This bitmap uses two bits to represent one element. These two bits determine
19 * the element state in the current and the future generation.
21 * An element can be in three states. The generation cursor is represented using
22 * the ^ character, note that this cursor shifts on every succesful transaction.
23 * If no transaction is going on, we observe all elements are in the following
26 * 11 = this element is active in the current generation. In case of no updates,
27 * ^ it stays active in the next generation.
28 * 00 = this element is inactive in the current generation. In case of no
29 * ^ updates, it stays inactive in the next generation.
31 * On transaction handling, we observe these two temporary states:
33 * 01 = this element is inactive in the current generation and it becomes active
34 * ^ in the next one. This happens when the element is inserted but commit
35 * path has not yet been executed yet, so activation is still pending. On
36 * transaction abortion, the element is removed.
37 * 10 = this element is active in the current generation and it becomes inactive
38 * ^ in the next one. This happens when the element is deactivated but commit
39 * path has not yet been executed yet, so removal is still pending. On
40 * transation abortion, the next generation bit is reset to go back to
41 * restore its previous state.
48 static inline void nft_bitmap_location(u32 key, u32 *idx, u32 *off)
52 *idx = k / BITS_PER_BYTE;
53 *off = k % BITS_PER_BYTE;
56 /* Fetch the two bits that represent the element and check if it is active based
57 * on the generation mask.
60 nft_bitmap_active(const u8 *bitmap, u32 idx, u32 off, u8 genmask)
62 return (bitmap[idx] & (0x3 << off)) & (genmask << off);
65 static bool nft_bitmap_lookup(const struct net *net, const struct nft_set *set,
66 const u32 *key, const struct nft_set_ext **ext)
68 const struct nft_bitmap *priv = nft_set_priv(set);
69 u8 genmask = nft_genmask_cur(net);
72 nft_bitmap_location(*key, &idx, &off);
74 return nft_bitmap_active(priv->bitmap, idx, off, genmask);
77 static int nft_bitmap_insert(const struct net *net, const struct nft_set *set,
78 const struct nft_set_elem *elem,
79 struct nft_set_ext **_ext)
81 struct nft_bitmap *priv = nft_set_priv(set);
82 struct nft_set_ext *ext = elem->priv;
83 u8 genmask = nft_genmask_next(net);
86 nft_bitmap_location(nft_set_ext_key(ext)->data[0], &idx, &off);
87 if (nft_bitmap_active(priv->bitmap, idx, off, genmask))
91 priv->bitmap[idx] |= (genmask << off);
96 static void nft_bitmap_remove(const struct net *net,
97 const struct nft_set *set,
98 const struct nft_set_elem *elem)
100 struct nft_bitmap *priv = nft_set_priv(set);
101 struct nft_set_ext *ext = elem->priv;
102 u8 genmask = nft_genmask_next(net);
105 nft_bitmap_location(nft_set_ext_key(ext)->data[0], &idx, &off);
106 /* Enter 00 state. */
107 priv->bitmap[idx] &= ~(genmask << off);
110 static void nft_bitmap_activate(const struct net *net,
111 const struct nft_set *set,
112 const struct nft_set_elem *elem)
114 struct nft_bitmap *priv = nft_set_priv(set);
115 struct nft_set_ext *ext = elem->priv;
116 u8 genmask = nft_genmask_next(net);
119 nft_bitmap_location(nft_set_ext_key(ext)->data[0], &idx, &off);
120 /* Enter 11 state. */
121 priv->bitmap[idx] |= (genmask << off);
124 static bool nft_bitmap_flush(const struct net *net,
125 const struct nft_set *set, void *ext)
127 struct nft_bitmap *priv = nft_set_priv(set);
128 u8 genmask = nft_genmask_next(net);
131 nft_bitmap_location(nft_set_ext_key(ext)->data[0], &idx, &off);
132 /* Enter 10 state, similar to deactivation. */
133 priv->bitmap[idx] &= ~(genmask << off);
138 static struct nft_set_ext *nft_bitmap_ext_alloc(const struct nft_set *set,
139 const struct nft_set_elem *elem)
141 struct nft_set_ext_tmpl tmpl;
142 struct nft_set_ext *ext;
144 nft_set_ext_prepare(&tmpl);
145 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
147 ext = kzalloc(tmpl.len, GFP_KERNEL);
151 nft_set_ext_init(ext, &tmpl);
152 memcpy(nft_set_ext_key(ext), elem->key.val.data, set->klen);
157 static void *nft_bitmap_deactivate(const struct net *net,
158 const struct nft_set *set,
159 const struct nft_set_elem *elem)
161 struct nft_bitmap *priv = nft_set_priv(set);
162 u8 genmask = nft_genmask_next(net);
163 struct nft_set_ext *ext;
164 u32 idx, off, key = 0;
166 memcpy(&key, elem->key.val.data, set->klen);
167 nft_bitmap_location(key, &idx, &off);
169 if (!nft_bitmap_active(priv->bitmap, idx, off, genmask))
172 /* We have no real set extension since this is a bitmap, allocate this
173 * dummy object that is released from the commit/abort path.
175 ext = nft_bitmap_ext_alloc(set, elem);
179 /* Enter 10 state. */
180 priv->bitmap[idx] &= ~(genmask << off);
185 static void nft_bitmap_walk(const struct nft_ctx *ctx,
187 struct nft_set_iter *iter)
189 const struct nft_bitmap *priv = nft_set_priv(set);
190 struct nft_set_ext_tmpl tmpl;
191 struct nft_set_elem elem;
192 struct nft_set_ext *ext;
196 nft_set_ext_prepare(&tmpl);
197 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
199 for (idx = 0; idx < priv->bitmap_size; idx++) {
200 for (off = 0; off < BITS_PER_BYTE; off += 2) {
201 if (iter->count < iter->skip)
204 if (!nft_bitmap_active(priv->bitmap, idx, off,
208 ext = kzalloc(tmpl.len, GFP_KERNEL);
213 nft_set_ext_init(ext, &tmpl);
214 key = ((idx * BITS_PER_BYTE) + off) >> 1;
215 memcpy(nft_set_ext_key(ext), &key, set->klen);
218 iter->err = iter->fn(ctx, set, iter, &elem);
220 /* On set flush, this dummy extension object is released
221 * from the commit/abort path.
234 /* The bitmap size is pow(2, key length in bits) / bits per byte. This is
235 * multiplied by two since each element takes two bits. For 8 bit keys, the
236 * bitmap consumes 66 bytes. For 16 bit keys, 16388 bytes.
238 static inline u32 nft_bitmap_size(u32 klen)
240 return ((2 << ((klen * BITS_PER_BYTE) - 1)) / BITS_PER_BYTE) << 1;
243 static inline u32 nft_bitmap_total_size(u32 klen)
245 return sizeof(struct nft_bitmap) + nft_bitmap_size(klen);
248 static unsigned int nft_bitmap_privsize(const struct nlattr * const nla[])
250 u32 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
252 return nft_bitmap_total_size(klen);
255 static int nft_bitmap_init(const struct nft_set *set,
256 const struct nft_set_desc *desc,
257 const struct nlattr * const nla[])
259 struct nft_bitmap *priv = nft_set_priv(set);
261 priv->bitmap_size = nft_bitmap_size(set->klen);
266 static void nft_bitmap_destroy(const struct nft_set *set)
270 static bool nft_bitmap_estimate(const struct nft_set_desc *desc, u32 features,
271 struct nft_set_estimate *est)
273 /* Make sure bitmaps we don't get bitmaps larger than 16 Kbytes. */
277 est->size = nft_bitmap_total_size(desc->klen);
278 est->lookup = NFT_SET_CLASS_O_1;
279 est->space = NFT_SET_CLASS_O_1;
284 static struct nft_set_ops nft_bitmap_ops __read_mostly = {
285 .privsize = nft_bitmap_privsize,
286 .estimate = nft_bitmap_estimate,
287 .init = nft_bitmap_init,
288 .destroy = nft_bitmap_destroy,
289 .insert = nft_bitmap_insert,
290 .remove = nft_bitmap_remove,
291 .deactivate = nft_bitmap_deactivate,
292 .flush = nft_bitmap_flush,
293 .activate = nft_bitmap_activate,
294 .lookup = nft_bitmap_lookup,
295 .walk = nft_bitmap_walk,
296 .owner = THIS_MODULE,
299 static int __init nft_bitmap_module_init(void)
301 return nft_register_set(&nft_bitmap_ops);
304 static void __exit nft_bitmap_module_exit(void)
306 nft_unregister_set(&nft_bitmap_ops);
309 module_init(nft_bitmap_module_init);
310 module_exit(nft_bitmap_module_exit);
312 MODULE_LICENSE("GPL");
313 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
314 MODULE_ALIAS_NFT_SET();