1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 Laura Garcia <nevola@gmail.com>
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>
24 static u32
nft_ng_inc_gen(struct nft_ng_inc
*priv
)
29 oval
= atomic_read(priv
->counter
);
30 nval
= (oval
+ 1 < priv
->modulus
) ? oval
+ 1 : 0;
31 } while (atomic_cmpxchg(priv
->counter
, oval
, nval
) != oval
);
33 return nval
+ priv
->offset
;
36 static void nft_ng_inc_eval(const struct nft_expr
*expr
,
37 struct nft_regs
*regs
,
38 const struct nft_pktinfo
*pkt
)
40 struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
42 regs
->data
[priv
->dreg
] = nft_ng_inc_gen(priv
);
45 static const struct nla_policy nft_ng_policy
[NFTA_NG_MAX
+ 1] = {
46 [NFTA_NG_DREG
] = { .type
= NLA_U32
},
47 [NFTA_NG_MODULUS
] = { .type
= NLA_U32
},
48 [NFTA_NG_TYPE
] = { .type
= NLA_U32
},
49 [NFTA_NG_OFFSET
] = { .type
= NLA_U32
},
52 static int nft_ng_inc_init(const struct nft_ctx
*ctx
,
53 const struct nft_expr
*expr
,
54 const struct nlattr
* const tb
[])
56 struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
59 if (tb
[NFTA_NG_OFFSET
])
60 priv
->offset
= ntohl(nla_get_be32(tb
[NFTA_NG_OFFSET
]));
62 priv
->modulus
= ntohl(nla_get_be32(tb
[NFTA_NG_MODULUS
]));
63 if (priv
->modulus
== 0)
66 if (priv
->offset
+ priv
->modulus
- 1 < priv
->offset
)
69 priv
->counter
= kmalloc(sizeof(*priv
->counter
), GFP_KERNEL_ACCOUNT
);
73 atomic_set(priv
->counter
, priv
->modulus
- 1);
75 err
= nft_parse_register_store(ctx
, tb
[NFTA_NG_DREG
], &priv
->dreg
,
76 NULL
, NFT_DATA_VALUE
, sizeof(u32
));
87 static bool nft_ng_inc_reduce(struct nft_regs_track
*track
,
88 const struct nft_expr
*expr
)
90 const struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
92 nft_reg_track_cancel(track
, priv
->dreg
, NFT_REG32_SIZE
);
97 static int nft_ng_dump(struct sk_buff
*skb
, enum nft_registers dreg
,
98 u32 modulus
, enum nft_ng_types type
, u32 offset
)
100 if (nft_dump_register(skb
, NFTA_NG_DREG
, dreg
))
101 goto nla_put_failure
;
102 if (nla_put_be32(skb
, NFTA_NG_MODULUS
, htonl(modulus
)))
103 goto nla_put_failure
;
104 if (nla_put_be32(skb
, NFTA_NG_TYPE
, htonl(type
)))
105 goto nla_put_failure
;
106 if (nla_put_be32(skb
, NFTA_NG_OFFSET
, htonl(offset
)))
107 goto nla_put_failure
;
115 static int nft_ng_inc_dump(struct sk_buff
*skb
,
116 const struct nft_expr
*expr
, bool reset
)
118 const struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
120 return nft_ng_dump(skb
, priv
->dreg
, priv
->modulus
, NFT_NG_INCREMENTAL
,
124 static void nft_ng_inc_destroy(const struct nft_ctx
*ctx
,
125 const struct nft_expr
*expr
)
127 const struct nft_ng_inc
*priv
= nft_expr_priv(expr
);
129 kfree(priv
->counter
);
132 struct nft_ng_random
{
138 static u32
nft_ng_random_gen(const struct nft_ng_random
*priv
)
140 return reciprocal_scale(get_random_u32(), priv
->modulus
) + priv
->offset
;
143 static void nft_ng_random_eval(const struct nft_expr
*expr
,
144 struct nft_regs
*regs
,
145 const struct nft_pktinfo
*pkt
)
147 struct nft_ng_random
*priv
= nft_expr_priv(expr
);
149 regs
->data
[priv
->dreg
] = nft_ng_random_gen(priv
);
152 static int nft_ng_random_init(const struct nft_ctx
*ctx
,
153 const struct nft_expr
*expr
,
154 const struct nlattr
* const tb
[])
156 struct nft_ng_random
*priv
= nft_expr_priv(expr
);
158 if (tb
[NFTA_NG_OFFSET
])
159 priv
->offset
= ntohl(nla_get_be32(tb
[NFTA_NG_OFFSET
]));
161 priv
->modulus
= ntohl(nla_get_be32(tb
[NFTA_NG_MODULUS
]));
162 if (priv
->modulus
== 0)
165 if (priv
->offset
+ priv
->modulus
- 1 < priv
->offset
)
168 return nft_parse_register_store(ctx
, tb
[NFTA_NG_DREG
], &priv
->dreg
,
169 NULL
, NFT_DATA_VALUE
, sizeof(u32
));
172 static int nft_ng_random_dump(struct sk_buff
*skb
,
173 const struct nft_expr
*expr
, bool reset
)
175 const struct nft_ng_random
*priv
= nft_expr_priv(expr
);
177 return nft_ng_dump(skb
, priv
->dreg
, priv
->modulus
, NFT_NG_RANDOM
,
181 static bool nft_ng_random_reduce(struct nft_regs_track
*track
,
182 const struct nft_expr
*expr
)
184 const struct nft_ng_random
*priv
= nft_expr_priv(expr
);
186 nft_reg_track_cancel(track
, priv
->dreg
, NFT_REG32_SIZE
);
191 static struct nft_expr_type nft_ng_type
;
192 static const struct nft_expr_ops nft_ng_inc_ops
= {
193 .type
= &nft_ng_type
,
194 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ng_inc
)),
195 .eval
= nft_ng_inc_eval
,
196 .init
= nft_ng_inc_init
,
197 .destroy
= nft_ng_inc_destroy
,
198 .dump
= nft_ng_inc_dump
,
199 .reduce
= nft_ng_inc_reduce
,
202 static const struct nft_expr_ops nft_ng_random_ops
= {
203 .type
= &nft_ng_type
,
204 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ng_random
)),
205 .eval
= nft_ng_random_eval
,
206 .init
= nft_ng_random_init
,
207 .dump
= nft_ng_random_dump
,
208 .reduce
= nft_ng_random_reduce
,
211 static const struct nft_expr_ops
*
212 nft_ng_select_ops(const struct nft_ctx
*ctx
, const struct nlattr
* const tb
[])
216 if (!tb
[NFTA_NG_DREG
] ||
217 !tb
[NFTA_NG_MODULUS
] ||
219 return ERR_PTR(-EINVAL
);
221 type
= ntohl(nla_get_be32(tb
[NFTA_NG_TYPE
]));
224 case NFT_NG_INCREMENTAL
:
225 return &nft_ng_inc_ops
;
227 return &nft_ng_random_ops
;
230 return ERR_PTR(-EINVAL
);
233 static struct nft_expr_type nft_ng_type __read_mostly
= {
235 .select_ops
= nft_ng_select_ops
,
236 .policy
= nft_ng_policy
,
237 .maxattr
= NFTA_NG_MAX
,
238 .owner
= THIS_MODULE
,
241 static int __init
nft_ng_module_init(void)
243 return nft_register_expr(&nft_ng_type
);
246 static void __exit
nft_ng_module_exit(void)
248 nft_unregister_expr(&nft_ng_type
);
251 module_init(nft_ng_module_init
);
252 module_exit(nft_ng_module_exit
);
254 MODULE_LICENSE("GPL");
255 MODULE_AUTHOR("Laura Garcia <nevola@gmail.com>");
256 MODULE_ALIAS_NFT_EXPR("numgen");
257 MODULE_DESCRIPTION("nftables number generator module");