2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/netlink.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/nf_tables.h>
17 #include <net/netfilter/nf_tables_core.h>
18 #include <net/netfilter/nf_tables.h>
20 struct nft_immediate_expr {
26 static void nft_immediate_eval(const struct nft_expr *expr,
27 struct nft_regs *regs,
28 const struct nft_pktinfo *pkt)
30 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
32 nft_data_copy(®s->data[priv->dreg], &priv->data, priv->dlen);
35 static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = {
36 [NFTA_IMMEDIATE_DREG] = { .type = NLA_U32 },
37 [NFTA_IMMEDIATE_DATA] = { .type = NLA_NESTED },
40 static int nft_immediate_init(const struct nft_ctx *ctx,
41 const struct nft_expr *expr,
42 const struct nlattr * const tb[])
44 struct nft_immediate_expr *priv = nft_expr_priv(expr);
45 struct nft_data_desc desc;
48 if (tb[NFTA_IMMEDIATE_DREG] == NULL ||
49 tb[NFTA_IMMEDIATE_DATA] == NULL)
52 err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc,
53 tb[NFTA_IMMEDIATE_DATA]);
57 priv->dlen = desc.len;
59 err = nft_parse_register_store(ctx, tb[NFTA_IMMEDIATE_DREG],
60 &priv->dreg, &priv->data, desc.type,
68 nft_data_release(&priv->data, desc.type);
72 static void nft_immediate_activate(const struct nft_ctx *ctx,
73 const struct nft_expr *expr)
75 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
77 return nft_data_hold(&priv->data, nft_dreg_to_type(priv->dreg));
80 static void nft_immediate_deactivate(const struct nft_ctx *ctx,
81 const struct nft_expr *expr,
82 enum nft_trans_phase phase)
84 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
86 if (phase == NFT_TRANS_COMMIT)
89 return nft_data_release(&priv->data, nft_dreg_to_type(priv->dreg));
92 static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
94 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
96 if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg))
99 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
100 nft_dreg_to_type(priv->dreg), priv->dlen);
106 static int nft_immediate_validate(const struct nft_ctx *ctx,
107 const struct nft_expr *expr,
108 const struct nft_data **data)
110 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
112 if (priv->dreg == NFT_REG_VERDICT)
118 static const struct nft_expr_ops nft_imm_ops = {
119 .type = &nft_imm_type,
120 .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
121 .eval = nft_immediate_eval,
122 .init = nft_immediate_init,
123 .activate = nft_immediate_activate,
124 .deactivate = nft_immediate_deactivate,
125 .dump = nft_immediate_dump,
126 .validate = nft_immediate_validate,
129 struct nft_expr_type nft_imm_type __read_mostly = {
132 .policy = nft_immediate_policy,
133 .maxattr = NFTA_IMMEDIATE_MAX,
134 .owner = THIS_MODULE,