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 {
22 enum nft_registers dreg:8;
26 static void nft_immediate_eval(const struct nft_expr *expr,
27 struct nft_data data[NFT_REG_MAX + 1],
28 const struct nft_pktinfo *pkt)
30 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
32 nft_data_copy(&data[priv->dreg], &priv->data);
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 priv->dreg = ntohl(nla_get_be32(tb[NFTA_IMMEDIATE_DREG]));
53 err = nft_validate_output_register(priv->dreg);
57 err = nft_data_init(ctx, &priv->data, &desc, tb[NFTA_IMMEDIATE_DATA]);
60 priv->dlen = desc.len;
62 err = nft_validate_data_load(ctx, priv->dreg, &priv->data, desc.type);
69 nft_data_uninit(&priv->data, desc.type);
73 static void nft_immediate_destroy(const struct nft_expr *expr)
75 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
76 return nft_data_uninit(&priv->data, nft_dreg_to_type(priv->dreg));
79 static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
81 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
83 if (nla_put_be32(skb, NFTA_IMMEDIATE_DREG, htonl(priv->dreg)))
86 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
87 nft_dreg_to_type(priv->dreg), priv->dlen);
93 static int nft_immediate_validate(const struct nft_ctx *ctx,
94 const struct nft_expr *expr,
95 const struct nft_data **data)
97 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
99 if (priv->dreg == NFT_REG_VERDICT)
105 static struct nft_expr_type nft_imm_type;
106 static const struct nft_expr_ops nft_imm_ops = {
107 .type = &nft_imm_type,
108 .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
109 .eval = nft_immediate_eval,
110 .init = nft_immediate_init,
111 .destroy = nft_immediate_destroy,
112 .dump = nft_immediate_dump,
113 .validate = nft_immediate_validate,
116 static struct nft_expr_type nft_imm_type __read_mostly = {
119 .policy = nft_immediate_policy,
120 .maxattr = NFTA_IMMEDIATE_MAX,
121 .owner = THIS_MODULE,
124 int __init nft_immediate_module_init(void)
126 return nft_register_expr(&nft_imm_type);
129 void nft_immediate_module_exit(void)
131 nft_unregister_expr(&nft_imm_type);