Skip to content

Commit d734a28

Browse files
Laura Garcia Liebanaummakynes
authored andcommitted
netfilter: nft_numgen: add map lookups for numgen statements
This patch includes a new attribute in the numgen structure to allow the lookup of an element based on the number generator as a key. For this purpose, different ops have been included to extend the current numgen inc functions. Currently, only supported for numgen incremental operations, but it will be supported for random in a follow-up patch. Signed-off-by: Laura Garcia Liebana <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 8b2ebb6 commit d734a28

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,13 +1450,17 @@ enum nft_trace_types {
14501450
* @NFTA_NG_MODULUS: maximum counter value (NLA_U32)
14511451
* @NFTA_NG_TYPE: operation type (NLA_U32)
14521452
* @NFTA_NG_OFFSET: offset to be added to the counter (NLA_U32)
1453+
* @NFTA_NG_SET_NAME: name of the map to lookup (NLA_STRING)
1454+
* @NFTA_NG_SET_ID: id of the map (NLA_U32)
14531455
*/
14541456
enum nft_ng_attributes {
14551457
NFTA_NG_UNSPEC,
14561458
NFTA_NG_DREG,
14571459
NFTA_NG_MODULUS,
14581460
NFTA_NG_TYPE,
14591461
NFTA_NG_OFFSET,
1462+
NFTA_NG_SET_NAME,
1463+
NFTA_NG_SET_ID,
14601464
__NFTA_NG_MAX
14611465
};
14621466
#define NFTA_NG_MAX (__NFTA_NG_MAX - 1)

net/netfilter/nft_numgen.c

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,58 @@ struct nft_ng_inc {
2424
u32 modulus;
2525
atomic_t counter;
2626
u32 offset;
27+
struct nft_set *map;
2728
};
2829

29-
static void nft_ng_inc_eval(const struct nft_expr *expr,
30-
struct nft_regs *regs,
31-
const struct nft_pktinfo *pkt)
30+
static u32 nft_ng_inc_gen(struct nft_ng_inc *priv)
3231
{
33-
struct nft_ng_inc *priv = nft_expr_priv(expr);
3432
u32 nval, oval;
3533

3634
do {
3735
oval = atomic_read(&priv->counter);
3836
nval = (oval + 1 < priv->modulus) ? oval + 1 : 0;
3937
} while (atomic_cmpxchg(&priv->counter, oval, nval) != oval);
4038

41-
regs->data[priv->dreg] = nval + priv->offset;
39+
return nval + priv->offset;
40+
}
41+
42+
static void nft_ng_inc_eval(const struct nft_expr *expr,
43+
struct nft_regs *regs,
44+
const struct nft_pktinfo *pkt)
45+
{
46+
struct nft_ng_inc *priv = nft_expr_priv(expr);
47+
48+
regs->data[priv->dreg] = nft_ng_inc_gen(priv);
49+
}
50+
51+
static void nft_ng_inc_map_eval(const struct nft_expr *expr,
52+
struct nft_regs *regs,
53+
const struct nft_pktinfo *pkt)
54+
{
55+
struct nft_ng_inc *priv = nft_expr_priv(expr);
56+
const struct nft_set *map = priv->map;
57+
const struct nft_set_ext *ext;
58+
u32 result;
59+
bool found;
60+
61+
result = nft_ng_inc_gen(priv);
62+
found = map->ops->lookup(nft_net(pkt), map, &result, &ext);
63+
64+
if (!found)
65+
return;
66+
67+
nft_data_copy(&regs->data[priv->dreg],
68+
nft_set_ext_data(ext), map->dlen);
4269
}
4370

4471
static const struct nla_policy nft_ng_policy[NFTA_NG_MAX + 1] = {
4572
[NFTA_NG_DREG] = { .type = NLA_U32 },
4673
[NFTA_NG_MODULUS] = { .type = NLA_U32 },
4774
[NFTA_NG_TYPE] = { .type = NLA_U32 },
4875
[NFTA_NG_OFFSET] = { .type = NLA_U32 },
76+
[NFTA_NG_SET_NAME] = { .type = NLA_STRING,
77+
.len = NFT_SET_MAXNAMELEN - 1 },
78+
[NFTA_NG_SET_ID] = { .type = NLA_U32 },
4979
};
5080

5181
static int nft_ng_inc_init(const struct nft_ctx *ctx,
@@ -71,6 +101,25 @@ static int nft_ng_inc_init(const struct nft_ctx *ctx,
71101
NFT_DATA_VALUE, sizeof(u32));
72102
}
73103

104+
static int nft_ng_inc_map_init(const struct nft_ctx *ctx,
105+
const struct nft_expr *expr,
106+
const struct nlattr * const tb[])
107+
{
108+
struct nft_ng_inc *priv = nft_expr_priv(expr);
109+
u8 genmask = nft_genmask_next(ctx->net);
110+
111+
nft_ng_inc_init(ctx, expr, tb);
112+
113+
priv->map = nft_set_lookup_global(ctx->net, ctx->table,
114+
tb[NFTA_NG_SET_NAME],
115+
tb[NFTA_NG_SET_ID], genmask);
116+
117+
if (IS_ERR(priv->map))
118+
return PTR_ERR(priv->map);
119+
120+
return 0;
121+
}
122+
74123
static int nft_ng_dump(struct sk_buff *skb, enum nft_registers dreg,
75124
u32 modulus, enum nft_ng_types type, u32 offset)
76125
{
@@ -97,6 +146,22 @@ static int nft_ng_inc_dump(struct sk_buff *skb, const struct nft_expr *expr)
97146
priv->offset);
98147
}
99148

149+
static int nft_ng_inc_map_dump(struct sk_buff *skb,
150+
const struct nft_expr *expr)
151+
{
152+
const struct nft_ng_inc *priv = nft_expr_priv(expr);
153+
154+
if (nft_ng_dump(skb, priv->dreg, priv->modulus,
155+
NFT_NG_INCREMENTAL, priv->offset) ||
156+
nla_put_string(skb, NFTA_NG_SET_NAME, priv->map->name))
157+
goto nla_put_failure;
158+
159+
return 0;
160+
161+
nla_put_failure:
162+
return -1;
163+
}
164+
100165
struct nft_ng_random {
101166
enum nft_registers dreg:8;
102167
u32 modulus;
@@ -156,6 +221,14 @@ static const struct nft_expr_ops nft_ng_inc_ops = {
156221
.dump = nft_ng_inc_dump,
157222
};
158223

224+
static const struct nft_expr_ops nft_ng_inc_map_ops = {
225+
.type = &nft_ng_type,
226+
.size = NFT_EXPR_SIZE(sizeof(struct nft_ng_inc)),
227+
.eval = nft_ng_inc_map_eval,
228+
.init = nft_ng_inc_map_init,
229+
.dump = nft_ng_inc_map_dump,
230+
};
231+
159232
static const struct nft_expr_ops nft_ng_random_ops = {
160233
.type = &nft_ng_type,
161234
.size = NFT_EXPR_SIZE(sizeof(struct nft_ng_random)),
@@ -178,6 +251,8 @@ nft_ng_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
178251

179252
switch (type) {
180253
case NFT_NG_INCREMENTAL:
254+
if (tb[NFTA_NG_SET_NAME])
255+
return &nft_ng_inc_map_ops;
181256
return &nft_ng_inc_ops;
182257
case NFT_NG_RANDOM:
183258
return &nft_ng_random_ops;

0 commit comments

Comments
 (0)