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 static void nft_immediate_eval(const struct nft_expr
*expr
,
21 struct nft_regs
*regs
,
22 const struct nft_pktinfo
*pkt
)
24 const struct nft_immediate_expr
*priv
= nft_expr_priv(expr
);
26 nft_data_copy(®s
->data
[priv
->dreg
], &priv
->data
, priv
->dlen
);
29 static const struct nla_policy nft_immediate_policy
[NFTA_IMMEDIATE_MAX
+ 1] = {
30 [NFTA_IMMEDIATE_DREG
] = { .type
= NLA_U32
},
31 [NFTA_IMMEDIATE_DATA
] = { .type
= NLA_NESTED
},
34 static int nft_immediate_init(const struct nft_ctx
*ctx
,
35 const struct nft_expr
*expr
,
36 const struct nlattr
* const tb
[])
38 struct nft_immediate_expr
*priv
= nft_expr_priv(expr
);
39 struct nft_data_desc desc
;
42 if (tb
[NFTA_IMMEDIATE_DREG
] == NULL
||
43 tb
[NFTA_IMMEDIATE_DATA
] == NULL
)
46 err
= nft_data_init(ctx
, &priv
->data
, sizeof(priv
->data
), &desc
,
47 tb
[NFTA_IMMEDIATE_DATA
]);
51 priv
->dlen
= desc
.len
;
53 priv
->dreg
= nft_parse_register(tb
[NFTA_IMMEDIATE_DREG
]);
54 err
= nft_validate_register_store(ctx
, priv
->dreg
, &priv
->data
,
62 nft_data_release(&priv
->data
, desc
.type
);
66 static void nft_immediate_activate(const struct nft_ctx
*ctx
,
67 const struct nft_expr
*expr
)
69 const struct nft_immediate_expr
*priv
= nft_expr_priv(expr
);
71 return nft_data_hold(&priv
->data
, nft_dreg_to_type(priv
->dreg
));
74 static void nft_immediate_deactivate(const struct nft_ctx
*ctx
,
75 const struct nft_expr
*expr
)
77 const struct nft_immediate_expr
*priv
= nft_expr_priv(expr
);
79 return nft_data_release(&priv
->data
, nft_dreg_to_type(priv
->dreg
));
82 static int nft_immediate_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
84 const struct nft_immediate_expr
*priv
= nft_expr_priv(expr
);
86 if (nft_dump_register(skb
, NFTA_IMMEDIATE_DREG
, priv
->dreg
))
89 return nft_data_dump(skb
, NFTA_IMMEDIATE_DATA
, &priv
->data
,
90 nft_dreg_to_type(priv
->dreg
), priv
->dlen
);
96 static int nft_immediate_validate(const struct nft_ctx
*ctx
,
97 const struct nft_expr
*expr
,
98 const struct nft_data
**d
)
100 const struct nft_immediate_expr
*priv
= nft_expr_priv(expr
);
101 struct nft_ctx
*pctx
= (struct nft_ctx
*)ctx
;
102 const struct nft_data
*data
;
105 if (priv
->dreg
!= NFT_REG_VERDICT
)
110 switch (data
->verdict
.code
) {
114 err
= nft_chain_validate(ctx
, data
->verdict
.chain
);
126 static const struct nft_expr_ops nft_imm_ops
= {
127 .type
= &nft_imm_type
,
128 .size
= NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr
)),
129 .eval
= nft_immediate_eval
,
130 .init
= nft_immediate_init
,
131 .activate
= nft_immediate_activate
,
132 .deactivate
= nft_immediate_deactivate
,
133 .dump
= nft_immediate_dump
,
134 .validate
= nft_immediate_validate
,
137 struct nft_expr_type nft_imm_type __read_mostly
= {
140 .policy
= nft_immediate_policy
,
141 .maxattr
= NFTA_IMMEDIATE_MAX
,
142 .owner
= THIS_MODULE
,