Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 235 lines 5.8 kB view raw
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Copyright (c) 2016 Laura Garcia <nevola@gmail.com> 4 */ 5 6#include <linux/kernel.h> 7#include <linux/init.h> 8#include <linux/module.h> 9#include <linux/netlink.h> 10#include <linux/netfilter.h> 11#include <linux/netfilter/nf_tables.h> 12#include <linux/random.h> 13#include <linux/static_key.h> 14#include <net/netfilter/nf_tables.h> 15#include <net/netfilter/nf_tables_core.h> 16 17struct nft_ng_inc { 18 u8 dreg; 19 u32 modulus; 20 atomic_t *counter; 21 u32 offset; 22}; 23 24static u32 nft_ng_inc_gen(struct nft_ng_inc *priv) 25{ 26 u32 nval, oval; 27 28 do { 29 oval = atomic_read(priv->counter); 30 nval = (oval + 1 < priv->modulus) ? oval + 1 : 0; 31 } while (atomic_cmpxchg(priv->counter, oval, nval) != oval); 32 33 return nval + priv->offset; 34} 35 36static void nft_ng_inc_eval(const struct nft_expr *expr, 37 struct nft_regs *regs, 38 const struct nft_pktinfo *pkt) 39{ 40 struct nft_ng_inc *priv = nft_expr_priv(expr); 41 42 regs->data[priv->dreg] = nft_ng_inc_gen(priv); 43} 44 45static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = { 46 [NFTA_NG_DREG] = NLA_POLICY_MAX(NLA_BE32, NFT_REG32_MAX), 47 [NFTA_NG_MODULUS] = { .type = NLA_U32 }, 48 [NFTA_NG_TYPE] = { .type = NLA_U32 }, 49 [NFTA_NG_OFFSET] = { .type = NLA_U32 }, 50}; 51 52static int nft_ng_inc_init(const struct nft_ctx *ctx, 53 const struct nft_expr *expr, 54 const struct nlattr * const tb[]) 55{ 56 struct nft_ng_inc *priv = nft_expr_priv(expr); 57 int err; 58 59 if (tb[NFTA_NG_OFFSET]) 60 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET])); 61 62 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS])); 63 if (priv->modulus == 0) 64 return -ERANGE; 65 66 if (priv->offset + priv->modulus - 1 < priv->offset) 67 return -EOVERFLOW; 68 69 priv->counter = kmalloc_obj(*priv->counter, GFP_KERNEL_ACCOUNT); 70 if (!priv->counter) 71 return -ENOMEM; 72 73 atomic_set(priv->counter, priv->modulus - 1); 74 75 err = nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg, 76 NULL, NFT_DATA_VALUE, sizeof(u32)); 77 if (err < 0) 78 goto err; 79 80 return 0; 81err: 82 kfree(priv->counter); 83 84 return err; 85} 86 87static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg, 88 u32 modulus, enum nft_ng_types type, u32 offset) 89{ 90 if (nft_dump_register(skb, NFTA_NG_DREG, dreg)) 91 goto nla_put_failure; 92 if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus))) 93 goto nla_put_failure; 94 if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type))) 95 goto nla_put_failure; 96 if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset))) 97 goto nla_put_failure; 98 99 return 0; 100 101nla_put_failure: 102 return -1; 103} 104 105static int nft_ng_inc_dump(struct sk_buff *skb, 106 const struct nft_expr *expr, bool reset) 107{ 108 const struct nft_ng_inc *priv = nft_expr_priv(expr); 109 110 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL, 111 priv->offset); 112} 113 114static void nft_ng_inc_destroy(const struct nft_ctx *ctx, 115 const struct nft_expr *expr) 116{ 117 const struct nft_ng_inc *priv = nft_expr_priv(expr); 118 119 kfree(priv->counter); 120} 121 122struct nft_ng_random { 123 u8 dreg; 124 u32 modulus; 125 u32 offset; 126}; 127 128static u32 nft_ng_random_gen(const struct nft_ng_random *priv) 129{ 130 return reciprocal_scale(get_random_u32(), priv->modulus) + priv->offset; 131} 132 133static void nft_ng_random_eval(const struct nft_expr *expr, 134 struct nft_regs *regs, 135 const struct nft_pktinfo *pkt) 136{ 137 struct nft_ng_random *priv = nft_expr_priv(expr); 138 139 regs->data[priv->dreg] = nft_ng_random_gen(priv); 140} 141 142static int nft_ng_random_init(const struct nft_ctx *ctx, 143 const struct nft_expr *expr, 144 const struct nlattr * const tb[]) 145{ 146 struct nft_ng_random *priv = nft_expr_priv(expr); 147 148 if (tb[NFTA_NG_OFFSET]) 149 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET])); 150 151 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS])); 152 if (priv->modulus == 0) 153 return -ERANGE; 154 155 if (priv->offset + priv->modulus - 1 < priv->offset) 156 return -EOVERFLOW; 157 158 return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg, 159 NULL, NFT_DATA_VALUE, sizeof(u32)); 160} 161 162static int nft_ng_random_dump(struct sk_buff *skb, 163 const struct nft_expr *expr, bool reset) 164{ 165 const struct nft_ng_random *priv = nft_expr_priv(expr); 166 167 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM, 168 priv->offset); 169} 170 171static struct nft_expr_type nft_ng_type; 172static const struct nft_expr_ops nft_ng_inc_ops = { 173 .type = &nft_ng_type, 174 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)), 175 .eval = nft_ng_inc_eval, 176 .init = nft_ng_inc_init, 177 .destroy = nft_ng_inc_destroy, 178 .dump = nft_ng_inc_dump, 179}; 180 181static const struct nft_expr_ops nft_ng_random_ops = { 182 .type = &nft_ng_type, 183 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)), 184 .eval = nft_ng_random_eval, 185 .init = nft_ng_random_init, 186 .dump = nft_ng_random_dump, 187}; 188 189static const struct nft_expr_ops * 190nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[]) 191{ 192 u32 type; 193 194 if (!tb[NFTA_NG_DREG] || 195 !tb[NFTA_NG_MODULUS] || 196 !tb[NFTA_NG_TYPE]) 197 return ERR_PTR(-EINVAL); 198 199 type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE])); 200 201 switch (type) { 202 case NFT_NG_INCREMENTAL: 203 return &nft_ng_inc_ops; 204 case NFT_NG_RANDOM: 205 return &nft_ng_random_ops; 206 } 207 208 return ERR_PTR(-EINVAL); 209} 210 211static struct nft_expr_type nft_ng_type __read_mostly = { 212 .name = "numgen", 213 .select_ops = nft_ng_select_ops, 214 .policy = nft_ng_policy, 215 .maxattr = NFTA_NG_MAX, 216 .owner = THIS_MODULE, 217}; 218 219static int __init nft_ng_module_init(void) 220{ 221 return nft_register_expr(&nft_ng_type); 222} 223 224static void __exit nft_ng_module_exit(void) 225{ 226 nft_unregister_expr(&nft_ng_type); 227} 228 229module_init(nft_ng_module_init); 230module_exit(nft_ng_module_exit); 231 232MODULE_LICENSE("GPL"); 233MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>"); 234MODULE_ALIAS_NFT_EXPR("numgen"); 235MODULE_DESCRIPTION("nftables number generator module");