Commit | Line | Data |
---|---|---|
96518518 | 1 | /* |
ef1f7df9 | 2 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
96518518 PM |
3 | * |
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. | |
7 | * | |
8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
9 | */ | |
10 | ||
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> | |
19 | ||
10870dd8 FW |
20 | void nft_immediate_eval(const struct nft_expr *expr, |
21 | struct nft_regs *regs, | |
22 | const struct nft_pktinfo *pkt) | |
96518518 PM |
23 | { |
24 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
25 | ||
49499c3e | 26 | nft_data_copy(®s->data[priv->dreg], &priv->data, priv->dlen); |
96518518 PM |
27 | } |
28 | ||
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 }, | |
32 | }; | |
33 | ||
34 | static int nft_immediate_init(const struct nft_ctx *ctx, | |
35 | const struct nft_expr *expr, | |
36 | const struct nlattr * const tb[]) | |
37 | { | |
38 | struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
39 | struct nft_data_desc desc; | |
40 | int err; | |
41 | ||
42 | if (tb[NFTA_IMMEDIATE_DREG] == NULL || | |
43 | tb[NFTA_IMMEDIATE_DATA] == NULL) | |
44 | return -EINVAL; | |
45 | ||
d0a11fc3 PM |
46 | err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc, |
47 | tb[NFTA_IMMEDIATE_DATA]); | |
96518518 PM |
48 | if (err < 0) |
49 | return err; | |
36b701fa | 50 | |
96518518 PM |
51 | priv->dlen = desc.len; |
52 | ||
b1c96ed3 | 53 | priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]); |
1ec10212 PM |
54 | err = nft_validate_register_store(ctx, priv->dreg, &priv->data, |
55 | desc.type, desc.len); | |
96518518 PM |
56 | if (err < 0) |
57 | goto err1; | |
58 | ||
59 | return 0; | |
60 | ||
61 | err1: | |
59105446 | 62 | nft_data_release(&priv->data, desc.type); |
96518518 PM |
63 | return err; |
64 | } | |
65 | ||
bb7b40ae PNA |
66 | static void nft_immediate_activate(const struct nft_ctx *ctx, |
67 | const struct nft_expr *expr) | |
68 | { | |
69 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
70 | ||
71 | return nft_data_hold(&priv->data, nft_dreg_to_type(priv->dreg)); | |
72 | } | |
73 | ||
74 | static void nft_immediate_deactivate(const struct nft_ctx *ctx, | |
f6ac8585 PNA |
75 | const struct nft_expr *expr, |
76 | enum nft_trans_phase phase) | |
96518518 PM |
77 | { |
78 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
59105446 | 79 | |
f6ac8585 PNA |
80 | if (phase == NFT_TRANS_COMMIT) |
81 | return; | |
82 | ||
59105446 | 83 | return nft_data_release(&priv->data, nft_dreg_to_type(priv->dreg)); |
96518518 PM |
84 | } |
85 | ||
86 | static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
87 | { | |
88 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
89 | ||
b1c96ed3 | 90 | if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg)) |
96518518 PM |
91 | goto nla_put_failure; |
92 | ||
93 | return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data, | |
94 | nft_dreg_to_type(priv->dreg), priv->dlen); | |
95 | ||
96 | nla_put_failure: | |
97 | return -1; | |
98 | } | |
99 | ||
0ca743a5 PNA |
100 | static int nft_immediate_validate(const struct nft_ctx *ctx, |
101 | const struct nft_expr *expr, | |
a654de8f | 102 | const struct nft_data **d) |
20a69341 PM |
103 | { |
104 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
26b2f552 | 105 | struct nft_ctx *pctx = (struct nft_ctx *)ctx; |
a654de8f PNA |
106 | const struct nft_data *data; |
107 | int err; | |
20a69341 | 108 | |
a654de8f PNA |
109 | if (priv->dreg != NFT_REG_VERDICT) |
110 | return 0; | |
111 | ||
112 | data = &priv->data; | |
113 | ||
114 | switch (data->verdict.code) { | |
115 | case NFT_JUMP: | |
116 | case NFT_GOTO: | |
26b2f552 | 117 | pctx->level++; |
a654de8f PNA |
118 | err = nft_chain_validate(ctx, data->verdict.chain); |
119 | if (err < 0) | |
120 | return err; | |
26b2f552 | 121 | pctx->level--; |
a654de8f PNA |
122 | break; |
123 | default: | |
124 | break; | |
125 | } | |
0ca743a5 PNA |
126 | |
127 | return 0; | |
20a69341 PM |
128 | } |
129 | ||
ef1f7df9 PM |
130 | static const struct nft_expr_ops nft_imm_ops = { |
131 | .type = &nft_imm_type, | |
96518518 | 132 | .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)), |
96518518 PM |
133 | .eval = nft_immediate_eval, |
134 | .init = nft_immediate_init, | |
bb7b40ae PNA |
135 | .activate = nft_immediate_activate, |
136 | .deactivate = nft_immediate_deactivate, | |
96518518 | 137 | .dump = nft_immediate_dump, |
0ca743a5 | 138 | .validate = nft_immediate_validate, |
ef1f7df9 PM |
139 | }; |
140 | ||
4e24877e | 141 | struct nft_expr_type nft_imm_type __read_mostly = { |
ef1f7df9 PM |
142 | .name = "immediate", |
143 | .ops = &nft_imm_ops, | |
96518518 PM |
144 | .policy = nft_immediate_policy, |
145 | .maxattr = NFTA_IMMEDIATE_MAX, | |
ef1f7df9 | 146 | .owner = THIS_MODULE, |
96518518 | 147 | }; |