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 struct nft_bitmap_elem { 19 struct list_head head; 20 struct nft_set_ext ext; 21 }; 22 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 */ 48 struct nft_bitmap { 49 struct list_head list; 50 u16 bitmap_size; 51 u8 bitmap[]; 52 }; 53 54 static 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 /* Fetch the two bits that represent the element and check if it is active based 71 * on the generation mask. 72 */ 73 static inline bool 74 nft_bitmap_active(const u8 *bitmap, u32 idx, u32 off, u8 genmask) 75 { 76 return (bitmap[idx] & (0x3 << off)) & (genmask << off); 77 } 78 79 static 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 88 return nft_bitmap_active(priv->bitmap, idx, off, genmask); 89 } 90 91 static struct nft_bitmap_elem * 92 nft_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 109 static void *nft_bitmap_get(const struct net *net, const struct nft_set *set, 110 const struct nft_set_elem *elem, unsigned int flags) 111 { 112 const struct nft_bitmap *priv = nft_set_priv(set); 113 u8 genmask = nft_genmask_cur(net); 114 struct nft_bitmap_elem *be; 115 116 list_for_each_entry_rcu(be, &priv->list, head) { 117 if (memcmp(nft_set_ext_key(&be->ext), elem->key.val.data, set->klen) || 118 !nft_set_elem_active(&be->ext, genmask)) 119 continue; 120 121 return be; 122 } 123 return ERR_PTR(-ENOENT); 124 } 125 126 static int nft_bitmap_insert(const struct net *net, const struct nft_set *set, 127 const struct nft_set_elem *elem, 128 struct nft_set_ext **ext) 129 { 130 struct nft_bitmap *priv = nft_set_priv(set); 131 struct nft_bitmap_elem *new = elem->priv, *be; 132 u8 genmask = nft_genmask_next(net); 133 u32 idx, off; 134 135 be = nft_bitmap_elem_find(set, new, genmask); 136 if (be) { 137 *ext = &be->ext; 138 return -EEXIST; 139 } 140 141 nft_bitmap_location(set, nft_set_ext_key(&new->ext), &idx, &off); 142 /* Enter 01 state. */ 143 priv->bitmap[idx] |= (genmask << off); 144 list_add_tail_rcu(&new->head, &priv->list); 145 146 return 0; 147 } 148 149 static void nft_bitmap_remove(const struct net *net, 150 const struct nft_set *set, 151 const struct nft_set_elem *elem) 152 { 153 struct nft_bitmap *priv = nft_set_priv(set); 154 struct nft_bitmap_elem *be = elem->priv; 155 u8 genmask = nft_genmask_next(net); 156 u32 idx, off; 157 158 nft_bitmap_location(set, nft_set_ext_key(&be->ext), &idx, &off); 159 /* Enter 00 state. */ 160 priv->bitmap[idx] &= ~(genmask << off); 161 list_del_rcu(&be->head); 162 } 163 164 static void nft_bitmap_activate(const struct net *net, 165 const struct nft_set *set, 166 const struct nft_set_elem *elem) 167 { 168 struct nft_bitmap *priv = nft_set_priv(set); 169 struct nft_bitmap_elem *be = elem->priv; 170 u8 genmask = nft_genmask_next(net); 171 u32 idx, off; 172 173 nft_bitmap_location(set, nft_set_ext_key(&be->ext), &idx, &off); 174 /* Enter 11 state. */ 175 priv->bitmap[idx] |= (genmask << off); 176 nft_set_elem_change_active(net, set, &be->ext); 177 } 178 179 static bool nft_bitmap_flush(const struct net *net, 180 const struct nft_set *set, void *_be) 181 { 182 struct nft_bitmap *priv = nft_set_priv(set); 183 u8 genmask = nft_genmask_next(net); 184 struct nft_bitmap_elem *be = _be; 185 u32 idx, off; 186 187 nft_bitmap_location(set, nft_set_ext_key(&be->ext), &idx, &off); 188 /* Enter 10 state, similar to deactivation. */ 189 priv->bitmap[idx] &= ~(genmask << off); 190 nft_set_elem_change_active(net, set, &be->ext); 191 192 return true; 193 } 194 195 static void *nft_bitmap_deactivate(const struct net *net, 196 const struct nft_set *set, 197 const struct nft_set_elem *elem) 198 { 199 struct nft_bitmap *priv = nft_set_priv(set); 200 struct nft_bitmap_elem *this = elem->priv, *be; 201 u8 genmask = nft_genmask_next(net); 202 u32 idx, off; 203 204 nft_bitmap_location(set, elem->key.val.data, &idx, &off); 205 206 be = nft_bitmap_elem_find(set, this, genmask); 207 if (!be) 208 return NULL; 209 210 /* Enter 10 state. */ 211 priv->bitmap[idx] &= ~(genmask << off); 212 nft_set_elem_change_active(net, set, &be->ext); 213 214 return be; 215 } 216 217 static void nft_bitmap_walk(const struct nft_ctx *ctx, 218 struct nft_set *set, 219 struct nft_set_iter *iter) 220 { 221 const struct nft_bitmap *priv = nft_set_priv(set); 222 struct nft_bitmap_elem *be; 223 struct nft_set_elem elem; 224 225 list_for_each_entry_rcu(be, &priv->list, head) { 226 if (iter->count < iter->skip) 227 goto cont; 228 if (!nft_set_elem_active(&be->ext, iter->genmask)) 229 goto cont; 230 231 elem.priv = be; 232 233 iter->err = iter->fn(ctx, set, iter, &elem); 234 235 if (iter->err < 0) 236 return; 237 cont: 238 iter->count++; 239 } 240 } 241 242 /* The bitmap size is pow(2, key length in bits) / bits per byte. This is 243 * multiplied by two since each element takes two bits. For 8 bit keys, the 244 * bitmap consumes 66 bytes. For 16 bit keys, 16388 bytes. 245 */ 246 static inline u32 nft_bitmap_size(u32 klen) 247 { 248 return ((2 << ((klen * BITS_PER_BYTE) - 1)) / BITS_PER_BYTE) << 1; 249 } 250 251 static inline u64 nft_bitmap_total_size(u32 klen) 252 { 253 return sizeof(struct nft_bitmap) + nft_bitmap_size(klen); 254 } 255 256 static u64 nft_bitmap_privsize(const struct nlattr * const nla[], 257 const struct nft_set_desc *desc) 258 { 259 u32 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); 260 261 return nft_bitmap_total_size(klen); 262 } 263 264 static int nft_bitmap_init(const struct nft_set *set, 265 const struct nft_set_desc *desc, 266 const struct nlattr * const nla[]) 267 { 268 struct nft_bitmap *priv = nft_set_priv(set); 269 270 INIT_LIST_HEAD(&priv->list); 271 priv->bitmap_size = nft_bitmap_size(set->klen); 272 273 return 0; 274 } 275 276 static void nft_bitmap_destroy(const struct nft_set *set) 277 { 278 struct nft_bitmap *priv = nft_set_priv(set); 279 struct nft_bitmap_elem *be, *n; 280 281 list_for_each_entry_safe(be, n, &priv->list, head) 282 nft_set_elem_destroy(set, be, true); 283 } 284 285 static bool nft_bitmap_estimate(const struct nft_set_desc *desc, u32 features, 286 struct nft_set_estimate *est) 287 { 288 /* Make sure bitmaps we don't get bitmaps larger than 16 Kbytes. */ 289 if (desc->klen > 2) 290 return false; 291 292 est->size = nft_bitmap_total_size(desc->klen); 293 est->lookup = NFT_SET_CLASS_O_1; 294 est->space = NFT_SET_CLASS_O_1; 295 296 return true; 297 } 298 299 struct nft_set_type nft_set_bitmap_type __read_mostly = { 300 .owner = THIS_MODULE, 301 .ops = { 302 .privsize = nft_bitmap_privsize, 303 .elemsize = offsetof(struct nft_bitmap_elem, ext), 304 .estimate = nft_bitmap_estimate, 305 .init = nft_bitmap_init, 306 .destroy = nft_bitmap_destroy, 307 .insert = nft_bitmap_insert, 308 .remove = nft_bitmap_remove, 309 .deactivate = nft_bitmap_deactivate, 310 .flush = nft_bitmap_flush, 311 .activate = nft_bitmap_activate, 312 .lookup = nft_bitmap_lookup, 313 .walk = nft_bitmap_walk, 314 .get = nft_bitmap_get, 315 }, 316 }; 317