2 * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
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.
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/netlink.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <linux/static_key.h>
17 #include <net/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_tables_core.h>
20 static DEFINE_PER_CPU(struct rnd_state, nft_numgen_prandom_state);
29 static void nft_ng_inc_eval(const struct nft_expr *expr,
30 struct nft_regs *regs,
31 const struct nft_pktinfo *pkt)
33 struct nft_ng_inc *priv = nft_expr_priv(expr);
37 oval = atomic_read(&priv->counter);
38 nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
39 } while (atomic_cmpxchg(&priv->counter, oval, nval) != oval);
41 regs->data[priv->dreg] = nval + priv->offset;
44 static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = {
45 [NFTA_NG_DREG] = { .type = NLA_U32 },
46 [NFTA_NG_MODULUS] = { .type = NLA_U32 },
47 [NFTA_NG_TYPE] = { .type = NLA_U32 },
48 [NFTA_NG_OFFSET] = { .type = NLA_U32 },
51 static int nft_ng_inc_init(const struct nft_ctx *ctx,
52 const struct nft_expr *expr,
53 const struct nlattr * const tb[])
55 struct nft_ng_inc *priv = nft_expr_priv(expr);
57 if (tb[NFTA_NG_OFFSET])
58 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
60 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
61 if (priv->modulus == 0)
64 if (priv->offset + priv->modulus - 1 < priv->offset)
67 atomic_set(&priv->counter, priv->modulus - 1);
69 return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
70 NULL, NFT_DATA_VALUE, sizeof(u32));
73 static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
74 u32 modulus, enum nft_ng_types type, u32 offset)
76 if (nft_dump_register(skb, NFTA_NG_DREG, dreg))
78 if (nla_put_be32(skb, NFTA_NG_MODULUS, htonl(modulus)))
80 if (nla_put_be32(skb, NFTA_NG_TYPE, htonl(type)))
82 if (nla_put_be32(skb, NFTA_NG_OFFSET, htonl(offset)))
91 static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
93 const struct nft_ng_inc *priv = nft_expr_priv(expr);
95 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_INCREMENTAL,
99 struct nft_ng_random {
105 static void nft_ng_random_eval(const struct nft_expr *expr,
106 struct nft_regs *regs,
107 const struct nft_pktinfo *pkt)
109 struct nft_ng_random *priv = nft_expr_priv(expr);
110 struct rnd_state *state = this_cpu_ptr(&nft_numgen_prandom_state);
113 val = reciprocal_scale(prandom_u32_state(state), priv->modulus);
114 regs->data[priv->dreg] = val + priv->offset;
117 static int nft_ng_random_init(const struct nft_ctx *ctx,
118 const struct nft_expr *expr,
119 const struct nlattr * const tb[])
121 struct nft_ng_random *priv = nft_expr_priv(expr);
123 if (tb[NFTA_NG_OFFSET])
124 priv->offset = ntohl(nla_get_be32(tb[NFTA_NG_OFFSET]));
126 priv->modulus = ntohl(nla_get_be32(tb[NFTA_NG_MODULUS]));
127 if (priv->modulus == 0)
130 if (priv->offset + priv->modulus - 1 < priv->offset)
133 prandom_init_once(&nft_numgen_prandom_state);
135 return nft_parse_register_store(ctx, tb[NFTA_NG_DREG], &priv->dreg,
136 NULL, NFT_DATA_VALUE, sizeof(u32));
139 static int nft_ng_random_dump(struct sk_buff *skb, const struct nft_expr *expr)
141 const struct nft_ng_random *priv = nft_expr_priv(expr);
143 return nft_ng_dump(skb, priv->dreg, priv->modulus, NFT_NG_RANDOM,
147 static struct nft_expr_type nft_ng_type;
148 static const struct nft_expr_ops nft_ng_inc_ops = {
149 .type = &nft_ng_type,
150 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
151 .eval = nft_ng_inc_eval,
152 .init = nft_ng_inc_init,
153 .dump = nft_ng_inc_dump,
156 static const struct nft_expr_ops nft_ng_random_ops = {
157 .type = &nft_ng_type,
158 .size = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
159 .eval = nft_ng_random_eval,
160 .init = nft_ng_random_init,
161 .dump = nft_ng_random_dump,
164 static const struct nft_expr_ops *
165 nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
169 if (!tb[NFTA_NG_DREG] ||
170 !tb[NFTA_NG_MODULUS] ||
172 return ERR_PTR(-EINVAL);
174 type = ntohl(nla_get_be32(tb[NFTA_NG_TYPE]));
177 case NFT_NG_INCREMENTAL:
178 return &nft_ng_inc_ops;
180 return &nft_ng_random_ops;
183 return ERR_PTR(-EINVAL);
186 static struct nft_expr_type nft_ng_type __read_mostly = {
188 .select_ops = nft_ng_select_ops,
189 .policy = nft_ng_policy,
190 .maxattr = NFTA_NG_MAX,
191 .owner = THIS_MODULE,
194 static int __init nft_ng_module_init(void)
196 return nft_register_expr(&nft_ng_type);
199 static void __exit nft_ng_module_exit(void)
201 nft_unregister_expr(&nft_ng_type);
204 module_init(nft_ng_module_init);
205 module_exit(nft_ng_module_exit);
207 MODULE_LICENSE("GPL");
208 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
209 MODULE_ALIAS_NFT_EXPR("numgen");