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>
16 #include <net/netfilter/nf_tables_offload.h>
18 void nft_immediate_eval(const struct nft_expr *expr,
19 struct nft_regs *regs,
20 const struct nft_pktinfo *pkt)
22 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
24 nft_data_copy(®s->data[priv->dreg], &priv->data, priv->dlen);
27 static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = {
28 [NFTA_IMMEDIATE_DREG] = { .type = NLA_U32 },
29 [NFTA_IMMEDIATE_DATA] = { .type = NLA_NESTED },
32 static enum nft_data_types nft_reg_to_type(const struct nlattr *nla)
34 enum nft_data_types type;
37 reg = ntohl(nla_get_be32(nla));
38 if (reg == NFT_REG_VERDICT)
39 type = NFT_DATA_VERDICT;
41 type = NFT_DATA_VALUE;
46 static int nft_immediate_init(const struct nft_ctx *ctx,
47 const struct nft_expr *expr,
48 const struct nlattr * const tb[])
50 struct nft_immediate_expr *priv = nft_expr_priv(expr);
51 struct nft_data_desc desc = {
52 .size = sizeof(priv->data),
56 if (tb[NFTA_IMMEDIATE_DREG] == NULL ||
57 tb[NFTA_IMMEDIATE_DATA] == NULL)
60 desc.type = nft_reg_to_type(tb[NFTA_IMMEDIATE_DREG]);
61 err = nft_data_init(ctx, &priv->data, &desc, tb[NFTA_IMMEDIATE_DATA]);
65 priv->dlen = desc.len;
67 err = nft_parse_register_store(ctx, tb[NFTA_IMMEDIATE_DREG],
68 &priv->dreg, &priv->data, desc.type,
73 if (priv->dreg == NFT_REG_VERDICT) {
74 struct nft_chain *chain = priv->data.verdict.chain;
76 switch (priv->data.verdict.code) {
79 if (nft_chain_is_bound(chain)) {
93 nft_data_release(&priv->data, desc.type);
97 static void nft_immediate_activate(const struct nft_ctx *ctx,
98 const struct nft_expr *expr)
100 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
102 return nft_data_hold(&priv->data, nft_dreg_to_type(priv->dreg));
105 static void nft_immediate_deactivate(const struct nft_ctx *ctx,
106 const struct nft_expr *expr,
107 enum nft_trans_phase phase)
109 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
111 if (phase == NFT_TRANS_COMMIT)
114 return nft_data_release(&priv->data, nft_dreg_to_type(priv->dreg));
117 static void nft_immediate_destroy(const struct nft_ctx *ctx,
118 const struct nft_expr *expr)
120 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
121 const struct nft_data *data = &priv->data;
122 struct nft_rule *rule, *n;
123 struct nft_ctx chain_ctx;
124 struct nft_chain *chain;
126 if (priv->dreg != NFT_REG_VERDICT)
129 switch (data->verdict.code) {
132 chain = data->verdict.chain;
134 if (!nft_chain_is_bound(chain))
138 chain_ctx.chain = chain;
140 list_for_each_entry_safe(rule, n, &chain->rules, list)
141 nf_tables_rule_release(&chain_ctx, rule);
143 nf_tables_chain_destroy(&chain_ctx);
150 static int nft_immediate_dump(struct sk_buff *skb,
151 const struct nft_expr *expr, bool reset)
153 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
155 if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg))
156 goto nla_put_failure;
158 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
159 nft_dreg_to_type(priv->dreg), priv->dlen);
165 static int nft_immediate_validate(const struct nft_ctx *ctx,
166 const struct nft_expr *expr,
167 const struct nft_data **d)
169 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
170 struct nft_ctx *pctx = (struct nft_ctx *)ctx;
171 const struct nft_data *data;
174 if (priv->dreg != NFT_REG_VERDICT)
179 switch (data->verdict.code) {
183 err = nft_chain_validate(ctx, data->verdict.chain);
195 static int nft_immediate_offload_verdict(struct nft_offload_ctx *ctx,
196 struct nft_flow_rule *flow,
197 const struct nft_immediate_expr *priv)
199 struct flow_action_entry *entry;
200 const struct nft_data *data;
202 entry = &flow->rule->action.entries[ctx->num_actions++];
205 switch (data->verdict.code) {
207 entry->id = FLOW_ACTION_ACCEPT;
210 entry->id = FLOW_ACTION_DROP;
219 static int nft_immediate_offload(struct nft_offload_ctx *ctx,
220 struct nft_flow_rule *flow,
221 const struct nft_expr *expr)
223 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
225 if (priv->dreg == NFT_REG_VERDICT)
226 return nft_immediate_offload_verdict(ctx, flow, priv);
228 memcpy(&ctx->regs[priv->dreg].data, &priv->data, sizeof(priv->data));
233 static bool nft_immediate_offload_action(const struct nft_expr *expr)
235 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
237 if (priv->dreg == NFT_REG_VERDICT)
243 static bool nft_immediate_reduce(struct nft_regs_track *track,
244 const struct nft_expr *expr)
246 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
248 if (priv->dreg != NFT_REG_VERDICT)
249 nft_reg_track_cancel(track, priv->dreg, priv->dlen);
254 static const struct nft_expr_ops nft_imm_ops = {
255 .type = &nft_imm_type,
256 .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
257 .eval = nft_immediate_eval,
258 .init = nft_immediate_init,
259 .activate = nft_immediate_activate,
260 .deactivate = nft_immediate_deactivate,
261 .destroy = nft_immediate_destroy,
262 .dump = nft_immediate_dump,
263 .validate = nft_immediate_validate,
264 .reduce = nft_immediate_reduce,
265 .offload = nft_immediate_offload,
266 .offload_action = nft_immediate_offload_action,
269 struct nft_expr_type nft_imm_type __read_mostly = {
272 .policy = nft_immediate_policy,
273 .maxattr = NFTA_IMMEDIATE_MAX,
274 .owner = THIS_MODULE,