]>
Commit | Line | Data |
---|---|---|
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 | ||
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. | |
20 | * | |
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 | |
24 | * state: | |
25 | * | |
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. | |
30 | * | |
31 | * On transaction handling, we observe these two temporary states: | |
32 | * | |
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. | |
42 | */ | |
43 | struct nft_bitmap { | |
44 | u16 bitmap_size; | |
45 | u8 bitmap[]; | |
46 | }; | |
47 | ||
fd89b23a LZ |
48 | static inline void nft_bitmap_location(const struct nft_set *set, |
49 | const void *key, | |
50 | u32 *idx, u32 *off) | |
665153ff | 51 | { |
fd89b23a LZ |
52 | u32 k; |
53 | ||
54 | if (set->klen == 2) | |
55 | k = *(u16 *)key; | |
56 | else | |
57 | k = *(u8 *)key; | |
58 | k <<= 1; | |
665153ff PNA |
59 | |
60 | *idx = k / BITS_PER_BYTE; | |
61 | *off = k % BITS_PER_BYTE; | |
62 | } | |
63 | ||
64 | /* Fetch the two bits that represent the element and check if it is active based | |
65 | * on the generation mask. | |
66 | */ | |
67 | static inline bool | |
68 | nft_bitmap_active(const u8 *bitmap, u32 idx, u32 off, u8 genmask) | |
69 | { | |
70 | return (bitmap[idx] & (0x3 << off)) & (genmask << off); | |
71 | } | |
72 | ||
73 | static bool nft_bitmap_lookup(const struct net *net, const struct nft_set *set, | |
74 | const u32 *key, const struct nft_set_ext **ext) | |
75 | { | |
76 | const struct nft_bitmap *priv = nft_set_priv(set); | |
77 | u8 genmask = nft_genmask_cur(net); | |
78 | u32 idx, off; | |
79 | ||
fd89b23a | 80 | nft_bitmap_location(set, key, &idx, &off); |
665153ff PNA |
81 | |
82 | return nft_bitmap_active(priv->bitmap, idx, off, genmask); | |
83 | } | |
84 | ||
85 | static int nft_bitmap_insert(const struct net *net, const struct nft_set *set, | |
86 | const struct nft_set_elem *elem, | |
87 | struct nft_set_ext **_ext) | |
88 | { | |
89 | struct nft_bitmap *priv = nft_set_priv(set); | |
90 | struct nft_set_ext *ext = elem->priv; | |
91 | u8 genmask = nft_genmask_next(net); | |
92 | u32 idx, off; | |
93 | ||
fd89b23a | 94 | nft_bitmap_location(set, nft_set_ext_key(ext), &idx, &off); |
665153ff PNA |
95 | if (nft_bitmap_active(priv->bitmap, idx, off, genmask)) |
96 | return -EEXIST; | |
97 | ||
98 | /* Enter 01 state. */ | |
99 | priv->bitmap[idx] |= (genmask << off); | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | static void nft_bitmap_remove(const struct net *net, | |
105 | const struct nft_set *set, | |
106 | const struct nft_set_elem *elem) | |
107 | { | |
108 | struct nft_bitmap *priv = nft_set_priv(set); | |
109 | struct nft_set_ext *ext = elem->priv; | |
110 | u8 genmask = nft_genmask_next(net); | |
111 | u32 idx, off; | |
112 | ||
fd89b23a | 113 | nft_bitmap_location(set, nft_set_ext_key(ext), &idx, &off); |
665153ff PNA |
114 | /* Enter 00 state. */ |
115 | priv->bitmap[idx] &= ~(genmask << off); | |
116 | } | |
117 | ||
118 | static void nft_bitmap_activate(const struct net *net, | |
119 | const struct nft_set *set, | |
120 | const struct nft_set_elem *elem) | |
121 | { | |
122 | struct nft_bitmap *priv = nft_set_priv(set); | |
123 | struct nft_set_ext *ext = elem->priv; | |
124 | u8 genmask = nft_genmask_next(net); | |
125 | u32 idx, off; | |
126 | ||
fd89b23a | 127 | nft_bitmap_location(set, nft_set_ext_key(ext), &idx, &off); |
665153ff PNA |
128 | /* Enter 11 state. */ |
129 | priv->bitmap[idx] |= (genmask << off); | |
130 | } | |
131 | ||
132 | static bool nft_bitmap_flush(const struct net *net, | |
133 | const struct nft_set *set, void *ext) | |
134 | { | |
135 | struct nft_bitmap *priv = nft_set_priv(set); | |
136 | u8 genmask = nft_genmask_next(net); | |
137 | u32 idx, off; | |
138 | ||
fd89b23a | 139 | nft_bitmap_location(set, nft_set_ext_key(ext), &idx, &off); |
665153ff PNA |
140 | /* Enter 10 state, similar to deactivation. */ |
141 | priv->bitmap[idx] &= ~(genmask << off); | |
142 | ||
143 | return true; | |
144 | } | |
145 | ||
146 | static struct nft_set_ext *nft_bitmap_ext_alloc(const struct nft_set *set, | |
147 | const struct nft_set_elem *elem) | |
148 | { | |
149 | struct nft_set_ext_tmpl tmpl; | |
150 | struct nft_set_ext *ext; | |
151 | ||
152 | nft_set_ext_prepare(&tmpl); | |
153 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); | |
154 | ||
155 | ext = kzalloc(tmpl.len, GFP_KERNEL); | |
156 | if (!ext) | |
157 | return NULL; | |
158 | ||
159 | nft_set_ext_init(ext, &tmpl); | |
160 | memcpy(nft_set_ext_key(ext), elem->key.val.data, set->klen); | |
161 | ||
162 | return ext; | |
163 | } | |
164 | ||
165 | static void *nft_bitmap_deactivate(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 | u8 genmask = nft_genmask_next(net); | |
171 | struct nft_set_ext *ext; | |
fd89b23a | 172 | u32 idx, off; |
665153ff | 173 | |
fd89b23a | 174 | nft_bitmap_location(set, elem->key.val.data, &idx, &off); |
665153ff PNA |
175 | |
176 | if (!nft_bitmap_active(priv->bitmap, idx, off, genmask)) | |
177 | return NULL; | |
178 | ||
179 | /* We have no real set extension since this is a bitmap, allocate this | |
180 | * dummy object that is released from the commit/abort path. | |
181 | */ | |
182 | ext = nft_bitmap_ext_alloc(set, elem); | |
183 | if (!ext) | |
184 | return NULL; | |
185 | ||
186 | /* Enter 10 state. */ | |
187 | priv->bitmap[idx] &= ~(genmask << off); | |
188 | ||
189 | return ext; | |
190 | } | |
191 | ||
192 | static void nft_bitmap_walk(const struct nft_ctx *ctx, | |
193 | struct nft_set *set, | |
194 | struct nft_set_iter *iter) | |
195 | { | |
196 | const struct nft_bitmap *priv = nft_set_priv(set); | |
197 | struct nft_set_ext_tmpl tmpl; | |
198 | struct nft_set_elem elem; | |
199 | struct nft_set_ext *ext; | |
200 | int idx, off; | |
201 | u16 key; | |
202 | ||
203 | nft_set_ext_prepare(&tmpl); | |
204 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); | |
205 | ||
206 | for (idx = 0; idx < priv->bitmap_size; idx++) { | |
207 | for (off = 0; off < BITS_PER_BYTE; off += 2) { | |
208 | if (iter->count < iter->skip) | |
209 | goto cont; | |
210 | ||
211 | if (!nft_bitmap_active(priv->bitmap, idx, off, | |
212 | iter->genmask)) | |
213 | goto cont; | |
214 | ||
215 | ext = kzalloc(tmpl.len, GFP_KERNEL); | |
216 | if (!ext) { | |
217 | iter->err = -ENOMEM; | |
218 | return; | |
219 | } | |
220 | nft_set_ext_init(ext, &tmpl); | |
221 | key = ((idx * BITS_PER_BYTE) + off) >> 1; | |
222 | memcpy(nft_set_ext_key(ext), &key, set->klen); | |
223 | ||
224 | elem.priv = ext; | |
225 | iter->err = iter->fn(ctx, set, iter, &elem); | |
226 | ||
227 | /* On set flush, this dummy extension object is released | |
228 | * from the commit/abort path. | |
229 | */ | |
230 | if (!iter->flush) | |
231 | kfree(ext); | |
232 | ||
233 | if (iter->err < 0) | |
234 | return; | |
235 | cont: | |
236 | iter->count++; | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
241 | /* The bitmap size is pow(2, key length in bits) / bits per byte. This is | |
242 | * multiplied by two since each element takes two bits. For 8 bit keys, the | |
243 | * bitmap consumes 66 bytes. For 16 bit keys, 16388 bytes. | |
244 | */ | |
245 | static inline u32 nft_bitmap_size(u32 klen) | |
246 | { | |
247 | return ((2 << ((klen * BITS_PER_BYTE) - 1)) / BITS_PER_BYTE) << 1; | |
248 | } | |
249 | ||
250 | static inline u32 nft_bitmap_total_size(u32 klen) | |
251 | { | |
252 | return sizeof(struct nft_bitmap) + nft_bitmap_size(klen); | |
253 | } | |
254 | ||
255 | static unsigned int nft_bitmap_privsize(const struct nlattr * const nla[]) | |
256 | { | |
257 | u32 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); | |
258 | ||
259 | return nft_bitmap_total_size(klen); | |
260 | } | |
261 | ||
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 | ||
13aa5a8f | 268 | priv->bitmap_size = nft_bitmap_size(set->klen); |
665153ff PNA |
269 | |
270 | return 0; | |
271 | } | |
272 | ||
273 | static void nft_bitmap_destroy(const struct nft_set *set) | |
274 | { | |
275 | } | |
276 | ||
277 | static bool nft_bitmap_estimate(const struct nft_set_desc *desc, u32 features, | |
278 | struct nft_set_estimate *est) | |
279 | { | |
280 | /* Make sure bitmaps we don't get bitmaps larger than 16 Kbytes. */ | |
281 | if (desc->klen > 2) | |
282 | return false; | |
283 | ||
284 | est->size = nft_bitmap_total_size(desc->klen); | |
285 | est->lookup = NFT_SET_CLASS_O_1; | |
286 | est->space = NFT_SET_CLASS_O_1; | |
287 | ||
288 | return true; | |
289 | } | |
290 | ||
291 | static struct nft_set_ops nft_bitmap_ops __read_mostly = { | |
292 | .privsize = nft_bitmap_privsize, | |
293 | .estimate = nft_bitmap_estimate, | |
294 | .init = nft_bitmap_init, | |
295 | .destroy = nft_bitmap_destroy, | |
296 | .insert = nft_bitmap_insert, | |
297 | .remove = nft_bitmap_remove, | |
298 | .deactivate = nft_bitmap_deactivate, | |
299 | .flush = nft_bitmap_flush, | |
300 | .activate = nft_bitmap_activate, | |
301 | .lookup = nft_bitmap_lookup, | |
302 | .walk = nft_bitmap_walk, | |
303 | .owner = THIS_MODULE, | |
304 | }; | |
305 | ||
306 | static int __init nft_bitmap_module_init(void) | |
307 | { | |
308 | return nft_register_set(&nft_bitmap_ops); | |
309 | } | |
310 | ||
311 | static void __exit nft_bitmap_module_exit(void) | |
312 | { | |
313 | nft_unregister_set(&nft_bitmap_ops); | |
314 | } | |
315 | ||
316 | module_init(nft_bitmap_module_init); | |
317 | module_exit(nft_bitmap_module_exit); | |
318 | ||
319 | MODULE_LICENSE("GPL"); | |
320 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); | |
321 | MODULE_ALIAS_NFT_SET(); |