Linux Audio

Check our new training course

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