Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
96518518 | 2 | /* |
ef1f7df9 | 3 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
96518518 | 4 | * |
96518518 PM |
5 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
6 | */ | |
7 | ||
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> | |
c9626a2c | 16 | #include <net/netfilter/nf_tables_offload.h> |
96518518 | 17 | |
10870dd8 FW |
18 | void nft_immediate_eval(const struct nft_expr *expr, |
19 | struct nft_regs *regs, | |
20 | const struct nft_pktinfo *pkt) | |
96518518 PM |
21 | { |
22 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
23 | ||
49499c3e | 24 | nft_data_copy(®s->data[priv->dreg], &priv->data, priv->dlen); |
96518518 PM |
25 | } |
26 | ||
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 }, | |
30 | }; | |
31 | ||
32 | static int nft_immediate_init(const struct nft_ctx *ctx, | |
33 | const struct nft_expr *expr, | |
34 | const struct nlattr * const tb[]) | |
35 | { | |
36 | struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
37 | struct nft_data_desc desc; | |
38 | int err; | |
39 | ||
40 | if (tb[NFTA_IMMEDIATE_DREG] == NULL || | |
41 | tb[NFTA_IMMEDIATE_DATA] == NULL) | |
42 | return -EINVAL; | |
43 | ||
d0a11fc3 PM |
44 | err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc, |
45 | tb[NFTA_IMMEDIATE_DATA]); | |
96518518 PM |
46 | if (err < 0) |
47 | return err; | |
36b701fa | 48 | |
96518518 PM |
49 | priv->dlen = desc.len; |
50 | ||
b1c96ed3 | 51 | priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]); |
1ec10212 PM |
52 | err = nft_validate_register_store(ctx, priv->dreg, &priv->data, |
53 | desc.type, desc.len); | |
96518518 PM |
54 | if (err < 0) |
55 | goto err1; | |
56 | ||
d0e2c7de PNA |
57 | if (priv->dreg == NFT_REG_VERDICT) { |
58 | struct nft_chain *chain = priv->data.verdict.chain; | |
59 | ||
60 | switch (priv->data.verdict.code) { | |
61 | case NFT_JUMP: | |
62 | case NFT_GOTO: | |
63 | if (nft_chain_is_bound(chain)) { | |
64 | err = -EBUSY; | |
65 | goto err1; | |
66 | } | |
67 | chain->bound = true; | |
68 | break; | |
69 | default: | |
70 | break; | |
71 | } | |
72 | } | |
73 | ||
96518518 PM |
74 | return 0; |
75 | ||
76 | err1: | |
59105446 | 77 | nft_data_release(&priv->data, desc.type); |
96518518 PM |
78 | return err; |
79 | } | |
80 | ||
bb7b40ae PNA |
81 | static void nft_immediate_activate(const struct nft_ctx *ctx, |
82 | const struct nft_expr *expr) | |
83 | { | |
84 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
85 | ||
86 | return nft_data_hold(&priv->data, nft_dreg_to_type(priv->dreg)); | |
87 | } | |
88 | ||
89 | static void nft_immediate_deactivate(const struct nft_ctx *ctx, | |
f6ac8585 PNA |
90 | const struct nft_expr *expr, |
91 | enum nft_trans_phase phase) | |
96518518 PM |
92 | { |
93 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
59105446 | 94 | |
f6ac8585 PNA |
95 | if (phase == NFT_TRANS_COMMIT) |
96 | return; | |
97 | ||
59105446 | 98 | return nft_data_release(&priv->data, nft_dreg_to_type(priv->dreg)); |
96518518 PM |
99 | } |
100 | ||
d0e2c7de PNA |
101 | static void nft_immediate_destroy(const struct nft_ctx *ctx, |
102 | const struct nft_expr *expr) | |
103 | { | |
104 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
105 | const struct nft_data *data = &priv->data; | |
336f531a | 106 | struct nft_rule *rule, *n; |
d0e2c7de PNA |
107 | struct nft_ctx chain_ctx; |
108 | struct nft_chain *chain; | |
d0e2c7de PNA |
109 | |
110 | if (priv->dreg != NFT_REG_VERDICT) | |
111 | return; | |
112 | ||
113 | switch (data->verdict.code) { | |
114 | case NFT_JUMP: | |
115 | case NFT_GOTO: | |
116 | chain = data->verdict.chain; | |
117 | ||
118 | if (!nft_chain_is_bound(chain)) | |
119 | break; | |
120 | ||
121 | chain_ctx = *ctx; | |
122 | chain_ctx.chain = chain; | |
123 | ||
336f531a | 124 | list_for_each_entry_safe(rule, n, &chain->rules, list) |
d0e2c7de PNA |
125 | nf_tables_rule_release(&chain_ctx, rule); |
126 | ||
127 | nf_tables_chain_destroy(&chain_ctx); | |
128 | break; | |
129 | default: | |
130 | break; | |
131 | } | |
132 | } | |
133 | ||
96518518 PM |
134 | static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr) |
135 | { | |
136 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
137 | ||
b1c96ed3 | 138 | if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg)) |
96518518 PM |
139 | goto nla_put_failure; |
140 | ||
141 | return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data, | |
142 | nft_dreg_to_type(priv->dreg), priv->dlen); | |
143 | ||
144 | nla_put_failure: | |
145 | return -1; | |
146 | } | |
147 | ||
0ca743a5 PNA |
148 | static int nft_immediate_validate(const struct nft_ctx *ctx, |
149 | const struct nft_expr *expr, | |
a654de8f | 150 | const struct nft_data **d) |
20a69341 PM |
151 | { |
152 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
26b2f552 | 153 | struct nft_ctx *pctx = (struct nft_ctx *)ctx; |
a654de8f PNA |
154 | const struct nft_data *data; |
155 | int err; | |
20a69341 | 156 | |
a654de8f PNA |
157 | if (priv->dreg != NFT_REG_VERDICT) |
158 | return 0; | |
159 | ||
160 | data = &priv->data; | |
161 | ||
162 | switch (data->verdict.code) { | |
163 | case NFT_JUMP: | |
164 | case NFT_GOTO: | |
26b2f552 | 165 | pctx->level++; |
a654de8f PNA |
166 | err = nft_chain_validate(ctx, data->verdict.chain); |
167 | if (err < 0) | |
168 | return err; | |
26b2f552 | 169 | pctx->level--; |
a654de8f PNA |
170 | break; |
171 | default: | |
172 | break; | |
173 | } | |
0ca743a5 PNA |
174 | |
175 | return 0; | |
20a69341 PM |
176 | } |
177 | ||
43dd16ef PNA |
178 | static int nft_immediate_offload_verdict(struct nft_offload_ctx *ctx, |
179 | struct nft_flow_rule *flow, | |
180 | const struct nft_immediate_expr *priv) | |
c9626a2c | 181 | { |
c9626a2c PNA |
182 | struct flow_action_entry *entry; |
183 | const struct nft_data *data; | |
184 | ||
c9626a2c PNA |
185 | entry = &flow->rule->action.entries[ctx->num_actions++]; |
186 | ||
187 | data = &priv->data; | |
188 | switch (data->verdict.code) { | |
189 | case NF_ACCEPT: | |
190 | entry->id = FLOW_ACTION_ACCEPT; | |
191 | break; | |
192 | case NF_DROP: | |
193 | entry->id = FLOW_ACTION_DROP; | |
194 | break; | |
195 | default: | |
196 | return -EOPNOTSUPP; | |
197 | } | |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
43dd16ef PNA |
202 | static int nft_immediate_offload(struct nft_offload_ctx *ctx, |
203 | struct nft_flow_rule *flow, | |
204 | const struct nft_expr *expr) | |
205 | { | |
206 | const struct nft_immediate_expr *priv = nft_expr_priv(expr); | |
207 | ||
208 | if (priv->dreg == NFT_REG_VERDICT) | |
209 | return nft_immediate_offload_verdict(ctx, flow, priv); | |
210 | ||
211 | memcpy(&ctx->regs[priv->dreg].data, &priv->data, sizeof(priv->data)); | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
ef1f7df9 PM |
216 | static const struct nft_expr_ops nft_imm_ops = { |
217 | .type = &nft_imm_type, | |
96518518 | 218 | .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)), |
96518518 PM |
219 | .eval = nft_immediate_eval, |
220 | .init = nft_immediate_init, | |
bb7b40ae PNA |
221 | .activate = nft_immediate_activate, |
222 | .deactivate = nft_immediate_deactivate, | |
d0e2c7de | 223 | .destroy = nft_immediate_destroy, |
96518518 | 224 | .dump = nft_immediate_dump, |
0ca743a5 | 225 | .validate = nft_immediate_validate, |
c9626a2c PNA |
226 | .offload = nft_immediate_offload, |
227 | .offload_flags = NFT_OFFLOAD_F_ACTION, | |
ef1f7df9 PM |
228 | }; |
229 | ||
4e24877e | 230 | struct nft_expr_type nft_imm_type __read_mostly = { |
ef1f7df9 PM |
231 | .name = "immediate", |
232 | .ops = &nft_imm_ops, | |
96518518 PM |
233 | .policy = nft_immediate_policy, |
234 | .maxattr = NFTA_IMMEDIATE_MAX, | |
ef1f7df9 | 235 | .owner = THIS_MODULE, |
96518518 | 236 | }; |