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
);
23 enum nft_registers dreg
:8;
29 static u32
nft_ng_inc_gen(struct nft_ng_inc
*priv
)
34 oval
= atomic_read(&priv
->counter
);
35 nval
= (oval
+ 1 < priv
->modulus
) ? oval
+ 1 : 0;
36 } while (atomic_cmpxchg(&priv
->counter
, oval
, nval
) != oval
);
38 return nval
+ priv
->offset
;
41 static void nft_ng_inc_eval(const struct nft_expr
*expr
,
42 struct nft_regs
*regs
,
43 const struct nft_pktinfo
*pkt
)
45 struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
47 regs
->data
[priv
->dreg
] = nft_ng_inc_gen(priv
);
50 static const struct nla_policy nft_ng_policy
[NFTA_NG_MAX
+ 1] = {
51 [NFTA_NG_DREG
] = { .type
= NLA_U32
},
52 [NFTA_NG_MODULUS
] = { .type
= NLA_U32
},
53 [NFTA_NG_TYPE
] = { .type
= NLA_U32
},
54 [NFTA_NG_OFFSET
] = { .type
= NLA_U32
},
57 static int nft_ng_inc_init(const struct nft_ctx
*ctx
,
58 const struct nft_expr
*expr
,
59 const struct nlattr
* const tb
[])
61 struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
63 if (tb
[NFTA_NG_OFFSET
])
64 priv
->offset
= ntohl(nla_get_be32(tb
[NFTA_NG_OFFSET
]));
66 priv
->modulus
= ntohl(nla_get_be32(tb
[NFTA_NG_MODULUS
]));
67 if (priv
->modulus
== 0)
70 if (priv
->offset
+ priv
->modulus
- 1 < priv
->offset
)
73 priv
->dreg
= nft_parse_register(tb
[NFTA_NG_DREG
]);
74 atomic_set(&priv
->counter
, priv
->modulus
- 1);
76 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
77 NFT_DATA_VALUE
, sizeof(u32
));
80 static int nft_ng_dump(struct sk_buff
*skb
, enum nft_registers dreg
,
81 u32 modulus
, enum nft_ng_types type
, u32 offset
)
83 if (nft_dump_register(skb
, NFTA_NG_DREG
, dreg
))
85 if (nla_put_be32(skb
, NFTA_NG_MODULUS
, htonl(modulus
)))
87 if (nla_put_be32(skb
, NFTA_NG_TYPE
, htonl(type
)))
89 if (nla_put_be32(skb
, NFTA_NG_OFFSET
, htonl(offset
)))
98 static int nft_ng_inc_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
100 const struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
102 return nft_ng_dump(skb
, priv
->dreg
, priv
->modulus
, NFT_NG_INCREMENTAL
,
106 struct nft_ng_random
{
107 enum nft_registers dreg
:8;
112 static u32
nft_ng_random_gen(struct nft_ng_random
*priv
)
114 struct rnd_state
*state
= this_cpu_ptr(&nft_numgen_prandom_state
);
116 return reciprocal_scale(prandom_u32_state(state
), priv
->modulus
) +
120 static void nft_ng_random_eval(const struct nft_expr
*expr
,
121 struct nft_regs
*regs
,
122 const struct nft_pktinfo
*pkt
)
124 struct nft_ng_random
*priv
= nft_expr_priv(expr
);
126 regs
->data
[priv
->dreg
] = nft_ng_random_gen(priv
);
129 static int nft_ng_random_init(const struct nft_ctx
*ctx
,
130 const struct nft_expr
*expr
,
131 const struct nlattr
* const tb
[])
133 struct nft_ng_random
*priv
= nft_expr_priv(expr
);
135 if (tb
[NFTA_NG_OFFSET
])
136 priv
->offset
= ntohl(nla_get_be32(tb
[NFTA_NG_OFFSET
]));
138 priv
->modulus
= ntohl(nla_get_be32(tb
[NFTA_NG_MODULUS
]));
139 if (priv
->modulus
== 0)
142 if (priv
->offset
+ priv
->modulus
- 1 < priv
->offset
)
145 prandom_init_once(&nft_numgen_prandom_state
);
147 priv
->dreg
= nft_parse_register(tb
[NFTA_NG_DREG
]);
149 return nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
150 NFT_DATA_VALUE
, sizeof(u32
));
153 static int nft_ng_random_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
155 const struct nft_ng_random
*priv
= nft_expr_priv(expr
);
157 return nft_ng_dump(skb
, priv
->dreg
, priv
->modulus
, NFT_NG_RANDOM
,
161 static struct nft_expr_type nft_ng_type
;
162 static const struct nft_expr_ops nft_ng_inc_ops
= {
163 .type
= &nft_ng_type
,
164 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ng_inc
)),
165 .eval
= nft_ng_inc_eval
,
166 .init
= nft_ng_inc_init
,
167 .dump
= nft_ng_inc_dump
,
170 static const struct nft_expr_ops nft_ng_random_ops
= {
171 .type
= &nft_ng_type
,
172 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ng_random
)),
173 .eval
= nft_ng_random_eval
,
174 .init
= nft_ng_random_init
,
175 .dump
= nft_ng_random_dump
,
178 static const struct nft_expr_ops
*
179 nft_ng_select_ops(const struct nft_ctx
*ctx
, const struct nlattr
* const tb
[])
183 if (!tb
[NFTA_NG_DREG
] ||
184 !tb
[NFTA_NG_MODULUS
] ||
186 return ERR_PTR(-EINVAL
);
188 type
= ntohl(nla_get_be32(tb
[NFTA_NG_TYPE
]));
191 case NFT_NG_INCREMENTAL
:
192 return &nft_ng_inc_ops
;
194 return &nft_ng_random_ops
;
197 return ERR_PTR(-EINVAL
);
200 static struct nft_expr_type nft_ng_type __read_mostly
= {
202 .select_ops
= nft_ng_select_ops
,
203 .policy
= nft_ng_policy
,
204 .maxattr
= NFTA_NG_MAX
,
205 .owner
= THIS_MODULE
,
208 static int __init
nft_ng_module_init(void)
210 return nft_register_expr(&nft_ng_type
);
213 static void __exit
nft_ng_module_exit(void)
215 nft_unregister_expr(&nft_ng_type
);
218 module_init(nft_ng_module_init
);
219 module_exit(nft_ng_module_exit
);
221 MODULE_LICENSE("GPL");
222 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
223 MODULE_ALIAS_NFT_EXPR("numgen");