Skip to content

Commit d2caa69

Browse files
kaberPablo Neira Ayuso
authored andcommitted
netfilter: nft_meta: split nft_meta_init() into two functions for get/set
For value spanning multiple registers, we need to validate the length of data loads. In order to add this to nft_meta, we need the length from key validation. Split the nft_meta_init() function into two functions for the get and set operations as preparation for that. Signed-off-by: Patrick McHardy <kaber@trash.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Pablo Neira Ayuso <pablo@gnumonks.org>
1 parent e88e514 commit d2caa69

1 file changed

Lines changed: 30 additions & 35 deletions

File tree

‎net/netfilter/nft_meta.c‎

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,15 @@ static const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = {
170170
[NFTA_META_SREG] = { .type = NLA_U32 },
171171
};
172172

173-
static int nft_meta_init_validate_set(uint32_t key)
173+
static int nft_meta_get_init(const struct nft_ctx *ctx,
174+
const struct nft_expr *expr,
175+
const struct nlattr * const tb[])
174176
{
175-
switch (key) {
176-
case NFT_META_MARK:
177-
case NFT_META_PRIORITY:
178-
case NFT_META_NFTRACE:
179-
return 0;
180-
default:
181-
return -EOPNOTSUPP;
182-
}
183-
}
177+
struct nft_meta *priv = nft_expr_priv(expr);
178+
int err;
184179

185-
static int nft_meta_init_validate_get(uint32_t key)
186-
{
187-
switch (key) {
180+
priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
181+
switch (priv->key) {
188182
case NFT_META_LEN:
189183
case NFT_META_PROTOCOL:
190184
case NFT_META_NFPROTO:
@@ -205,39 +199,40 @@ static int nft_meta_init_validate_get(uint32_t key)
205199
#ifdef CONFIG_NETWORK_SECMARK
206200
case NFT_META_SECMARK:
207201
#endif
208-
return 0;
202+
break;
209203
default:
210204
return -EOPNOTSUPP;
211205
}
212206

207+
priv->dreg = ntohl(nla_get_be32(tb[NFTA_META_DREG]));
208+
err = nft_validate_output_register(priv->dreg);
209+
if (err < 0)
210+
return err;
211+
212+
err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
213+
if (err < 0)
214+
return err;
215+
216+
return 0;
213217
}
214218

215-
static int nft_meta_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
216-
const struct nlattr * const tb[])
219+
static int nft_meta_set_init(const struct nft_ctx *ctx,
220+
const struct nft_expr *expr,
221+
const struct nlattr * const tb[])
217222
{
218223
struct nft_meta *priv = nft_expr_priv(expr);
219224
int err;
220225

221226
priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
222-
223-
if (tb[NFTA_META_DREG]) {
224-
err = nft_meta_init_validate_get(priv->key);
225-
if (err < 0)
226-
return err;
227-
228-
priv->dreg = ntohl(nla_get_be32(tb[NFTA_META_DREG]));
229-
err = nft_validate_output_register(priv->dreg);
230-
if (err < 0)
231-
return err;
232-
233-
return nft_validate_data_load(ctx, priv->dreg, NULL,
234-
NFT_DATA_VALUE);
227+
switch (priv->key) {
228+
case NFT_META_MARK:
229+
case NFT_META_PRIORITY:
230+
case NFT_META_NFTRACE:
231+
break;
232+
default:
233+
return -EOPNOTSUPP;
235234
}
236235

237-
err = nft_meta_init_validate_set(priv->key);
238-
if (err < 0)
239-
return err;
240-
241236
priv->sreg = ntohl(nla_get_be32(tb[NFTA_META_SREG]));
242237
err = nft_validate_input_register(priv->sreg);
243238
if (err < 0)
@@ -282,15 +277,15 @@ static const struct nft_expr_ops nft_meta_get_ops = {
282277
.type = &nft_meta_type,
283278
.size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
284279
.eval = nft_meta_get_eval,
285-
.init = nft_meta_init,
280+
.init = nft_meta_get_init,
286281
.dump = nft_meta_get_dump,
287282
};
288283

289284
static const struct nft_expr_ops nft_meta_set_ops = {
290285
.type = &nft_meta_type,
291286
.size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
292287
.eval = nft_meta_set_eval,
293-
.init = nft_meta_init,
288+
.init = nft_meta_set_init,
294289
.dump = nft_meta_set_dump,
295290
};
296291

0 commit comments

Comments
 (0)