1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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 err = nf_tables_bind_chain(ctx, chain);
91 nft_data_release(&priv->data, desc.type);
95 static void nft_immediate_activate(const struct nft_ctx *ctx,
96 const struct nft_expr *expr)
98 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
99 const struct nft_data *data = &priv->data;
100 struct nft_ctx chain_ctx;
101 struct nft_chain *chain;
102 struct nft_rule *rule;
104 if (priv->dreg == NFT_REG_VERDICT) {
105 switch (data->verdict.code) {
108 chain = data->verdict.chain;
109 if (!nft_chain_binding(chain))
113 chain_ctx.chain = chain;
115 list_for_each_entry(rule, &chain->rules, list)
116 nft_rule_expr_activate(&chain_ctx, rule);
118 nft_clear(ctx->net, chain);
125 return nft_data_hold(&priv->data, nft_dreg_to_type(priv->dreg));
128 static void nft_immediate_deactivate(const struct nft_ctx *ctx,
129 const struct nft_expr *expr,
130 enum nft_trans_phase phase)
132 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
133 const struct nft_data *data = &priv->data;
134 struct nft_ctx chain_ctx;
135 struct nft_chain *chain;
136 struct nft_rule *rule;
138 if (priv->dreg == NFT_REG_VERDICT) {
139 switch (data->verdict.code) {
142 chain = data->verdict.chain;
143 if (!nft_chain_binding(chain))
147 chain_ctx.chain = chain;
149 list_for_each_entry(rule, &chain->rules, list)
150 nft_rule_expr_deactivate(&chain_ctx, rule, phase);
153 case NFT_TRANS_PREPARE_ERROR:
154 nf_tables_unbind_chain(ctx, chain);
156 case NFT_TRANS_PREPARE:
157 nft_deactivate_next(ctx->net, chain);
160 nft_chain_del(chain);
161 chain->bound = false;
171 if (phase == NFT_TRANS_COMMIT)
174 return nft_data_release(&priv->data, nft_dreg_to_type(priv->dreg));
177 static void nft_immediate_destroy(const struct nft_ctx *ctx,
178 const struct nft_expr *expr)
180 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
181 const struct nft_data *data = &priv->data;
182 struct nft_rule *rule, *n;
183 struct nft_ctx chain_ctx;
184 struct nft_chain *chain;
186 if (priv->dreg != NFT_REG_VERDICT)
189 switch (data->verdict.code) {
192 chain = data->verdict.chain;
194 if (!nft_chain_binding(chain))
197 /* Rule construction failed, but chain is already bound:
198 * let the transaction records release this chain and its rules.
205 /* Rule has been deleted, release chain and its rules. */
207 chain_ctx.chain = chain;
210 list_for_each_entry_safe(rule, n, &chain->rules, list) {
212 list_del(&rule->list);
213 nf_tables_rule_destroy(&chain_ctx, rule);
215 nf_tables_chain_destroy(&chain_ctx);
222 static int nft_immediate_dump(struct sk_buff *skb,
223 const struct nft_expr *expr, bool reset)
225 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
227 if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg))
228 goto nla_put_failure;
230 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
231 nft_dreg_to_type(priv->dreg), priv->dlen);
237 static int nft_immediate_validate(const struct nft_ctx *ctx,
238 const struct nft_expr *expr,
239 const struct nft_data **d)
241 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
242 struct nft_ctx *pctx = (struct nft_ctx *)ctx;
243 const struct nft_data *data;
246 if (priv->dreg != NFT_REG_VERDICT)
251 switch (data->verdict.code) {
255 err = nft_chain_validate(ctx, data->verdict.chain);
267 static int nft_immediate_offload_verdict(struct nft_offload_ctx *ctx,
268 struct nft_flow_rule *flow,
269 const struct nft_immediate_expr *priv)
271 struct flow_action_entry *entry;
272 const struct nft_data *data;
274 entry = &flow->rule->action.entries[ctx->num_actions++];
277 switch (data->verdict.code) {
279 entry->id = FLOW_ACTION_ACCEPT;
282 entry->id = FLOW_ACTION_DROP;
291 static int nft_immediate_offload(struct nft_offload_ctx *ctx,
292 struct nft_flow_rule *flow,
293 const struct nft_expr *expr)
295 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
297 if (priv->dreg == NFT_REG_VERDICT)
298 return nft_immediate_offload_verdict(ctx, flow, priv);
300 memcpy(&ctx->regs[priv->dreg].data, &priv->data, sizeof(priv->data));
305 static bool nft_immediate_offload_action(const struct nft_expr *expr)
307 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
309 if (priv->dreg == NFT_REG_VERDICT)
315 static bool nft_immediate_reduce(struct nft_regs_track *track,
316 const struct nft_expr *expr)
318 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
320 if (priv->dreg != NFT_REG_VERDICT)
321 nft_reg_track_cancel(track, priv->dreg, priv->dlen);
326 static const struct nft_expr_ops nft_imm_ops = {
327 .type = &nft_imm_type,
328 .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
329 .eval = nft_immediate_eval,
330 .init = nft_immediate_init,
331 .activate = nft_immediate_activate,
332 .deactivate = nft_immediate_deactivate,
333 .destroy = nft_immediate_destroy,
334 .dump = nft_immediate_dump,
335 .validate = nft_immediate_validate,
336 .reduce = nft_immediate_reduce,
337 .offload = nft_immediate_offload,
338 .offload_action = nft_immediate_offload_action,
341 struct nft_expr_type nft_imm_type __read_mostly = {
344 .policy = nft_immediate_policy,
345 .maxattr = NFTA_IMMEDIATE_MAX,
346 .owner = THIS_MODULE,