1 // SPDX-License-Identifier: GPL-2.0-only
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/netlink.h>
12 #include <linux/netfilter.h>
13 #include <linux/netfilter/nf_tables.h>
14 #include <net/netfilter/nf_tables_core.h>
15 #include <net/netfilter/nf_tables.h>
17 void nft_immediate_eval(const struct nft_expr *expr,
18 struct nft_regs *regs,
19 const struct nft_pktinfo *pkt)
21 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
23 nft_data_copy(®s->data[priv->dreg], &priv->data, priv->dlen);
26 static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = {
27 [NFTA_IMMEDIATE_DREG] = { .type = NLA_U32 },
28 [NFTA_IMMEDIATE_DATA] = { .type = NLA_NESTED },
31 static int nft_immediate_init(const struct nft_ctx *ctx,
32 const struct nft_expr *expr,
33 const struct nlattr * const tb[])
35 struct nft_immediate_expr *priv = nft_expr_priv(expr);
36 struct nft_data_desc desc;
39 if (tb[NFTA_IMMEDIATE_DREG] == NULL ||
40 tb[NFTA_IMMEDIATE_DATA] == NULL)
43 err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc,
44 tb[NFTA_IMMEDIATE_DATA]);
48 priv->dlen = desc.len;
50 priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]);
51 err = nft_validate_register_store(ctx, priv->dreg, &priv->data,
59 nft_data_release(&priv->data, desc.type);
63 static void nft_immediate_activate(const struct nft_ctx *ctx,
64 const struct nft_expr *expr)
66 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
68 return nft_data_hold(&priv->data, nft_dreg_to_type(priv->dreg));
71 static void nft_immediate_deactivate(const struct nft_ctx *ctx,
72 const struct nft_expr *expr,
73 enum nft_trans_phase phase)
75 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
77 if (phase == NFT_TRANS_COMMIT)
80 return nft_data_release(&priv->data, nft_dreg_to_type(priv->dreg));
83 static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
85 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
87 if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg))
90 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
91 nft_dreg_to_type(priv->dreg), priv->dlen);
97 static int nft_immediate_validate(const struct nft_ctx *ctx,
98 const struct nft_expr *expr,
99 const struct nft_data **d)
101 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
102 struct nft_ctx *pctx = (struct nft_ctx *)ctx;
103 const struct nft_data *data;
106 if (priv->dreg != NFT_REG_VERDICT)
111 switch (data->verdict.code) {
115 err = nft_chain_validate(ctx, data->verdict.chain);
127 static const struct nft_expr_ops nft_imm_ops = {
128 .type = &nft_imm_type,
129 .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
130 .eval = nft_immediate_eval,
131 .init = nft_immediate_init,
132 .activate = nft_immediate_activate,
133 .deactivate = nft_immediate_deactivate,
134 .dump = nft_immediate_dump,
135 .validate = nft_immediate_validate,
138 struct nft_expr_type nft_imm_type __read_mostly = {
141 .policy = nft_immediate_policy,
142 .maxattr = NFTA_IMMEDIATE_MAX,
143 .owner = THIS_MODULE,