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