]>
Commit | Line | Data |
---|---|---|
0ca743a5 PNA |
1 | /* |
2 | * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org> | |
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 | * This software has been sponsored by Sophos Astaro <http://www.sophos.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/nfnetlink.h> | |
17 | #include <linux/netfilter/nf_tables.h> | |
18 | #include <linux/netfilter/nf_tables_compat.h> | |
19 | #include <linux/netfilter/x_tables.h> | |
20 | #include <linux/netfilter_ipv4/ip_tables.h> | |
21 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
5191f4d8 | 22 | #include <linux/netfilter_bridge/ebtables.h> |
5f158939 | 23 | #include <linux/netfilter_arp/arp_tables.h> |
0ca743a5 PNA |
24 | #include <net/netfilter/nf_tables.h> |
25 | ||
4b512e1c LZ |
26 | struct nft_xt { |
27 | struct list_head head; | |
28 | struct nft_expr_ops ops; | |
29 | unsigned int refcnt; | |
93020a5d FW |
30 | |
31 | /* Unlike other expressions, ops doesn't have static storage duration. | |
32 | * nft core assumes they do. We use kfree_rcu so that nft core can | |
33 | * can check expr->ops->size even after nft_compat->destroy() frees | |
34 | * the nft_xt struct that holds the ops structure. | |
35 | */ | |
36 | struct rcu_head rcu_head; | |
4b512e1c LZ |
37 | }; |
38 | ||
0c93dbed FW |
39 | /* Used for matches where *info is larger than X byte */ |
40 | #define NFT_MATCH_LARGE_THRESH 192 | |
41 | ||
42 | struct nft_xt_match_priv { | |
43 | void *info; | |
44 | }; | |
45 | ||
93020a5d | 46 | static bool nft_xt_put(struct nft_xt *xt) |
4b512e1c LZ |
47 | { |
48 | if (--xt->refcnt == 0) { | |
49 | list_del(&xt->head); | |
93020a5d FW |
50 | kfree_rcu(xt, rcu_head); |
51 | return true; | |
4b512e1c | 52 | } |
93020a5d FW |
53 | |
54 | return false; | |
4b512e1c LZ |
55 | } |
56 | ||
f3f5dded PNA |
57 | static int nft_compat_chain_validate_dependency(const char *tablename, |
58 | const struct nft_chain *chain) | |
59 | { | |
f3f5dded PNA |
60 | const struct nft_base_chain *basechain; |
61 | ||
f323d954 PNA |
62 | if (!tablename || |
63 | !nft_is_base_chain(chain)) | |
f3f5dded PNA |
64 | return 0; |
65 | ||
f3f5dded | 66 | basechain = nft_base_chain(chain); |
c918687f PNA |
67 | if (strcmp(tablename, "nat") == 0 && |
68 | basechain->type->type != NFT_CHAIN_T_NAT) | |
f3f5dded PNA |
69 | return -EINVAL; |
70 | ||
71 | return 0; | |
72 | } | |
73 | ||
0ca743a5 PNA |
74 | union nft_entry { |
75 | struct ipt_entry e4; | |
76 | struct ip6t_entry e6; | |
5191f4d8 | 77 | struct ebt_entry ebt; |
5f158939 | 78 | struct arpt_entry arp; |
0ca743a5 PNA |
79 | }; |
80 | ||
81 | static inline void | |
82 | nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info) | |
83 | { | |
84 | par->target = xt; | |
85 | par->targinfo = xt_info; | |
86 | par->hotdrop = false; | |
87 | } | |
88 | ||
5191f4d8 | 89 | static void nft_target_eval_xt(const struct nft_expr *expr, |
a55e22e9 | 90 | struct nft_regs *regs, |
5191f4d8 | 91 | const struct nft_pktinfo *pkt) |
0ca743a5 PNA |
92 | { |
93 | void *info = nft_expr_priv(expr); | |
94 | struct xt_target *target = expr->ops->data; | |
95 | struct sk_buff *skb = pkt->skb; | |
96 | int ret; | |
97 | ||
98 | nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info); | |
99 | ||
100 | ret = target->target(skb, &pkt->xt); | |
101 | ||
102 | if (pkt->xt.hotdrop) | |
103 | ret = NF_DROP; | |
104 | ||
5191f4d8 | 105 | switch (ret) { |
0ca743a5 | 106 | case XT_CONTINUE: |
a55e22e9 | 107 | regs->verdict.code = NFT_CONTINUE; |
0ca743a5 PNA |
108 | break; |
109 | default: | |
a55e22e9 | 110 | regs->verdict.code = ret; |
0ca743a5 PNA |
111 | break; |
112 | } | |
5191f4d8 AB |
113 | } |
114 | ||
115 | static void nft_target_eval_bridge(const struct nft_expr *expr, | |
a55e22e9 | 116 | struct nft_regs *regs, |
5191f4d8 AB |
117 | const struct nft_pktinfo *pkt) |
118 | { | |
119 | void *info = nft_expr_priv(expr); | |
120 | struct xt_target *target = expr->ops->data; | |
121 | struct sk_buff *skb = pkt->skb; | |
122 | int ret; | |
123 | ||
124 | nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info); | |
125 | ||
126 | ret = target->target(skb, &pkt->xt); | |
127 | ||
128 | if (pkt->xt.hotdrop) | |
129 | ret = NF_DROP; | |
130 | ||
131 | switch (ret) { | |
132 | case EBT_ACCEPT: | |
a55e22e9 | 133 | regs->verdict.code = NF_ACCEPT; |
5191f4d8 AB |
134 | break; |
135 | case EBT_DROP: | |
a55e22e9 | 136 | regs->verdict.code = NF_DROP; |
5191f4d8 AB |
137 | break; |
138 | case EBT_CONTINUE: | |
a55e22e9 | 139 | regs->verdict.code = NFT_CONTINUE; |
5191f4d8 AB |
140 | break; |
141 | case EBT_RETURN: | |
a55e22e9 | 142 | regs->verdict.code = NFT_RETURN; |
5191f4d8 AB |
143 | break; |
144 | default: | |
a55e22e9 | 145 | regs->verdict.code = ret; |
5191f4d8 AB |
146 | break; |
147 | } | |
0ca743a5 PNA |
148 | } |
149 | ||
150 | static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = { | |
151 | [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING }, | |
152 | [NFTA_TARGET_REV] = { .type = NLA_U32 }, | |
153 | [NFTA_TARGET_INFO] = { .type = NLA_BINARY }, | |
154 | }; | |
155 | ||
156 | static void | |
157 | nft_target_set_tgchk_param(struct xt_tgchk_param *par, | |
158 | const struct nft_ctx *ctx, | |
159 | struct xt_target *target, void *info, | |
2156d321 | 160 | union nft_entry *entry, u16 proto, bool inv) |
0ca743a5 | 161 | { |
2daf1b4d | 162 | par->net = ctx->net; |
0ca743a5 PNA |
163 | par->table = ctx->table->name; |
164 | switch (ctx->afi->family) { | |
165 | case AF_INET: | |
166 | entry->e4.ip.proto = proto; | |
167 | entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0; | |
168 | break; | |
169 | case AF_INET6: | |
749177cc PNA |
170 | if (proto) |
171 | entry->e6.ipv6.flags |= IP6T_F_PROTO; | |
172 | ||
0ca743a5 PNA |
173 | entry->e6.ipv6.proto = proto; |
174 | entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0; | |
175 | break; | |
5191f4d8 | 176 | case NFPROTO_BRIDGE: |
2156d321 | 177 | entry->ebt.ethproto = (__force __be16)proto; |
5191f4d8 AB |
178 | entry->ebt.invflags = inv ? EBT_IPROTO : 0; |
179 | break; | |
5f158939 AB |
180 | case NFPROTO_ARP: |
181 | break; | |
0ca743a5 PNA |
182 | } |
183 | par->entryinfo = entry; | |
184 | par->target = target; | |
185 | par->targinfo = info; | |
f323d954 | 186 | if (nft_is_base_chain(ctx->chain)) { |
0ca743a5 PNA |
187 | const struct nft_base_chain *basechain = |
188 | nft_base_chain(ctx->chain); | |
115a60b1 | 189 | const struct nf_hook_ops *ops = &basechain->ops[0]; |
0ca743a5 PNA |
190 | |
191 | par->hook_mask = 1 << ops->hooknum; | |
493618a9 PNA |
192 | } else { |
193 | par->hook_mask = 0; | |
0ca743a5 PNA |
194 | } |
195 | par->family = ctx->afi->family; | |
55917a21 | 196 | par->nft_compat = true; |
0ca743a5 PNA |
197 | } |
198 | ||
199 | static void target_compat_from_user(struct xt_target *t, void *in, void *out) | |
200 | { | |
756c1b1a | 201 | int pad; |
0ca743a5 | 202 | |
756c1b1a PNA |
203 | memcpy(out, in, t->targetsize); |
204 | pad = XT_ALIGN(t->targetsize) - t->targetsize; | |
205 | if (pad > 0) | |
206 | memset(out + t->targetsize, 0, pad); | |
0ca743a5 PNA |
207 | } |
208 | ||
209 | static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = { | |
210 | [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 }, | |
211 | [NFTA_RULE_COMPAT_FLAGS] = { .type = NLA_U32 }, | |
212 | }; | |
213 | ||
2156d321 | 214 | static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv) |
0ca743a5 PNA |
215 | { |
216 | struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1]; | |
217 | u32 flags; | |
218 | int err; | |
219 | ||
220 | err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr, | |
fceb6435 | 221 | nft_rule_compat_policy, NULL); |
0ca743a5 PNA |
222 | if (err < 0) |
223 | return err; | |
224 | ||
225 | if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS]) | |
226 | return -EINVAL; | |
227 | ||
228 | flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS])); | |
229 | if (flags & ~NFT_RULE_COMPAT_F_MASK) | |
230 | return -EINVAL; | |
231 | if (flags & NFT_RULE_COMPAT_F_INV) | |
232 | *inv = true; | |
233 | ||
8691a9a3 PNA |
234 | *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO])); |
235 | return 0; | |
0ca743a5 PNA |
236 | } |
237 | ||
238 | static int | |
239 | nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr, | |
240 | const struct nlattr * const tb[]) | |
241 | { | |
242 | void *info = nft_expr_priv(expr); | |
243 | struct xt_target *target = expr->ops->data; | |
244 | struct xt_tgchk_param par; | |
245 | size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO])); | |
93020a5d | 246 | struct nft_xt *nft_xt; |
2156d321 | 247 | u16 proto = 0; |
0ca743a5 PNA |
248 | bool inv = false; |
249 | union nft_entry e = {}; | |
250 | int ret; | |
251 | ||
252 | target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info); | |
253 | ||
8691a9a3 PNA |
254 | if (ctx->nla[NFTA_RULE_COMPAT]) { |
255 | ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv); | |
256 | if (ret < 0) | |
93020a5d | 257 | return ret; |
8691a9a3 | 258 | } |
0ca743a5 PNA |
259 | |
260 | nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv); | |
261 | ||
262 | ret = xt_check_target(&par, size, proto, inv); | |
263 | if (ret < 0) | |
93020a5d | 264 | return ret; |
0ca743a5 PNA |
265 | |
266 | /* The standard target cannot be used */ | |
93020a5d FW |
267 | if (!target->target) |
268 | return -EINVAL; | |
0ca743a5 | 269 | |
93020a5d FW |
270 | nft_xt = container_of(expr->ops, struct nft_xt, ops); |
271 | nft_xt->refcnt++; | |
0ca743a5 | 272 | return 0; |
0ca743a5 PNA |
273 | } |
274 | ||
275 | static void | |
62472bce | 276 | nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) |
0ca743a5 PNA |
277 | { |
278 | struct xt_target *target = expr->ops->data; | |
3d9b1421 PNA |
279 | void *info = nft_expr_priv(expr); |
280 | struct xt_tgdtor_param par; | |
281 | ||
282 | par.net = ctx->net; | |
283 | par.target = target; | |
284 | par.targinfo = info; | |
285 | par.family = ctx->afi->family; | |
286 | if (par.target->destroy != NULL) | |
287 | par.target->destroy(&par); | |
0ca743a5 | 288 | |
93020a5d FW |
289 | if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops))) |
290 | module_put(target->me); | |
0ca743a5 PNA |
291 | } |
292 | ||
0ca743a5 PNA |
293 | static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr) |
294 | { | |
295 | const struct xt_target *target = expr->ops->data; | |
296 | void *info = nft_expr_priv(expr); | |
297 | ||
298 | if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) || | |
299 | nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) || | |
756c1b1a | 300 | nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(target->targetsize), info)) |
0ca743a5 PNA |
301 | goto nla_put_failure; |
302 | ||
303 | return 0; | |
304 | ||
305 | nla_put_failure: | |
306 | return -1; | |
307 | } | |
308 | ||
309 | static int nft_target_validate(const struct nft_ctx *ctx, | |
310 | const struct nft_expr *expr, | |
311 | const struct nft_data **data) | |
312 | { | |
313 | struct xt_target *target = expr->ops->data; | |
314 | unsigned int hook_mask = 0; | |
f3f5dded | 315 | int ret; |
0ca743a5 | 316 | |
f323d954 | 317 | if (nft_is_base_chain(ctx->chain)) { |
0ca743a5 PNA |
318 | const struct nft_base_chain *basechain = |
319 | nft_base_chain(ctx->chain); | |
115a60b1 | 320 | const struct nf_hook_ops *ops = &basechain->ops[0]; |
0ca743a5 PNA |
321 | |
322 | hook_mask = 1 << ops->hooknum; | |
f7fb77fc | 323 | if (target->hooks && !(hook_mask & target->hooks)) |
f3f5dded | 324 | return -EINVAL; |
0ca743a5 | 325 | |
f3f5dded PNA |
326 | ret = nft_compat_chain_validate_dependency(target->table, |
327 | ctx->chain); | |
328 | if (ret < 0) | |
329 | return ret; | |
0ca743a5 PNA |
330 | } |
331 | return 0; | |
332 | } | |
333 | ||
741e0633 FW |
334 | static void __nft_match_eval(const struct nft_expr *expr, |
335 | struct nft_regs *regs, | |
336 | const struct nft_pktinfo *pkt, | |
337 | void *info) | |
0ca743a5 | 338 | { |
0ca743a5 PNA |
339 | struct xt_match *match = expr->ops->data; |
340 | struct sk_buff *skb = pkt->skb; | |
341 | bool ret; | |
342 | ||
343 | nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info); | |
344 | ||
345 | ret = match->match(skb, (struct xt_action_param *)&pkt->xt); | |
346 | ||
347 | if (pkt->xt.hotdrop) { | |
a55e22e9 | 348 | regs->verdict.code = NF_DROP; |
0ca743a5 PNA |
349 | return; |
350 | } | |
351 | ||
c1f86676 DM |
352 | switch (ret ? 1 : 0) { |
353 | case 1: | |
a55e22e9 | 354 | regs->verdict.code = NFT_CONTINUE; |
0ca743a5 | 355 | break; |
c1f86676 | 356 | case 0: |
a55e22e9 | 357 | regs->verdict.code = NFT_BREAK; |
0ca743a5 PNA |
358 | break; |
359 | } | |
360 | } | |
361 | ||
0c93dbed FW |
362 | static void nft_match_large_eval(const struct nft_expr *expr, |
363 | struct nft_regs *regs, | |
364 | const struct nft_pktinfo *pkt) | |
365 | { | |
366 | struct nft_xt_match_priv *priv = nft_expr_priv(expr); | |
367 | ||
368 | __nft_match_eval(expr, regs, pkt, priv->info); | |
369 | } | |
370 | ||
741e0633 FW |
371 | static void nft_match_eval(const struct nft_expr *expr, |
372 | struct nft_regs *regs, | |
373 | const struct nft_pktinfo *pkt) | |
374 | { | |
375 | __nft_match_eval(expr, regs, pkt, nft_expr_priv(expr)); | |
376 | } | |
377 | ||
0ca743a5 PNA |
378 | static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = { |
379 | [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING }, | |
380 | [NFTA_MATCH_REV] = { .type = NLA_U32 }, | |
381 | [NFTA_MATCH_INFO] = { .type = NLA_BINARY }, | |
382 | }; | |
383 | ||
384 | /* struct xt_mtchk_param and xt_tgchk_param look very similar */ | |
385 | static void | |
386 | nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx, | |
387 | struct xt_match *match, void *info, | |
2156d321 | 388 | union nft_entry *entry, u16 proto, bool inv) |
0ca743a5 | 389 | { |
2daf1b4d | 390 | par->net = ctx->net; |
0ca743a5 PNA |
391 | par->table = ctx->table->name; |
392 | switch (ctx->afi->family) { | |
393 | case AF_INET: | |
394 | entry->e4.ip.proto = proto; | |
395 | entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0; | |
396 | break; | |
397 | case AF_INET6: | |
749177cc PNA |
398 | if (proto) |
399 | entry->e6.ipv6.flags |= IP6T_F_PROTO; | |
400 | ||
0ca743a5 PNA |
401 | entry->e6.ipv6.proto = proto; |
402 | entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0; | |
403 | break; | |
5191f4d8 | 404 | case NFPROTO_BRIDGE: |
2156d321 | 405 | entry->ebt.ethproto = (__force __be16)proto; |
5191f4d8 AB |
406 | entry->ebt.invflags = inv ? EBT_IPROTO : 0; |
407 | break; | |
5f158939 AB |
408 | case NFPROTO_ARP: |
409 | break; | |
0ca743a5 PNA |
410 | } |
411 | par->entryinfo = entry; | |
412 | par->match = match; | |
413 | par->matchinfo = info; | |
f323d954 | 414 | if (nft_is_base_chain(ctx->chain)) { |
0ca743a5 PNA |
415 | const struct nft_base_chain *basechain = |
416 | nft_base_chain(ctx->chain); | |
115a60b1 | 417 | const struct nf_hook_ops *ops = &basechain->ops[0]; |
0ca743a5 PNA |
418 | |
419 | par->hook_mask = 1 << ops->hooknum; | |
493618a9 PNA |
420 | } else { |
421 | par->hook_mask = 0; | |
0ca743a5 PNA |
422 | } |
423 | par->family = ctx->afi->family; | |
55917a21 | 424 | par->nft_compat = true; |
0ca743a5 PNA |
425 | } |
426 | ||
427 | static void match_compat_from_user(struct xt_match *m, void *in, void *out) | |
428 | { | |
756c1b1a PNA |
429 | int pad; |
430 | ||
431 | memcpy(out, in, m->matchsize); | |
432 | pad = XT_ALIGN(m->matchsize) - m->matchsize; | |
433 | if (pad > 0) | |
434 | memset(out + m->matchsize, 0, pad); | |
0ca743a5 PNA |
435 | } |
436 | ||
437 | static int | |
741e0633 FW |
438 | __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr, |
439 | const struct nlattr * const tb[], | |
440 | void *info) | |
0ca743a5 | 441 | { |
0ca743a5 PNA |
442 | struct xt_match *match = expr->ops->data; |
443 | struct xt_mtchk_param par; | |
444 | size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO])); | |
93020a5d | 445 | struct nft_xt *nft_xt; |
2156d321 | 446 | u16 proto = 0; |
0ca743a5 PNA |
447 | bool inv = false; |
448 | union nft_entry e = {}; | |
449 | int ret; | |
450 | ||
451 | match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info); | |
452 | ||
8691a9a3 PNA |
453 | if (ctx->nla[NFTA_RULE_COMPAT]) { |
454 | ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv); | |
455 | if (ret < 0) | |
93020a5d | 456 | return ret; |
8691a9a3 | 457 | } |
0ca743a5 PNA |
458 | |
459 | nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv); | |
460 | ||
461 | ret = xt_check_match(&par, size, proto, inv); | |
462 | if (ret < 0) | |
93020a5d | 463 | return ret; |
0ca743a5 | 464 | |
93020a5d FW |
465 | nft_xt = container_of(expr->ops, struct nft_xt, ops); |
466 | nft_xt->refcnt++; | |
0ca743a5 | 467 | return 0; |
0ca743a5 PNA |
468 | } |
469 | ||
741e0633 FW |
470 | static int |
471 | nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr, | |
472 | const struct nlattr * const tb[]) | |
473 | { | |
474 | return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr)); | |
475 | } | |
476 | ||
0c93dbed FW |
477 | static int |
478 | nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr, | |
479 | const struct nlattr * const tb[]) | |
480 | { | |
481 | struct nft_xt_match_priv *priv = nft_expr_priv(expr); | |
482 | struct xt_match *m = expr->ops->data; | |
483 | int ret; | |
484 | ||
485 | priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL); | |
486 | if (!priv->info) | |
487 | return -ENOMEM; | |
488 | ||
489 | ret = __nft_match_init(ctx, expr, tb, priv->info); | |
490 | if (ret) | |
491 | kfree(priv->info); | |
492 | return ret; | |
493 | } | |
494 | ||
0ca743a5 | 495 | static void |
741e0633 FW |
496 | __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr, |
497 | void *info) | |
0ca743a5 PNA |
498 | { |
499 | struct xt_match *match = expr->ops->data; | |
3d9b1421 PNA |
500 | struct xt_mtdtor_param par; |
501 | ||
502 | par.net = ctx->net; | |
503 | par.match = match; | |
504 | par.matchinfo = info; | |
505 | par.family = ctx->afi->family; | |
506 | if (par.match->destroy != NULL) | |
507 | par.match->destroy(&par); | |
0ca743a5 | 508 | |
93020a5d FW |
509 | if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops))) |
510 | module_put(match->me); | |
0ca743a5 PNA |
511 | } |
512 | ||
741e0633 FW |
513 | static void |
514 | nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) | |
515 | { | |
516 | __nft_match_destroy(ctx, expr, nft_expr_priv(expr)); | |
517 | } | |
518 | ||
0c93dbed FW |
519 | static void |
520 | nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) | |
521 | { | |
522 | struct nft_xt_match_priv *priv = nft_expr_priv(expr); | |
523 | ||
524 | __nft_match_destroy(ctx, expr, priv->info); | |
525 | kfree(priv->info); | |
526 | } | |
527 | ||
741e0633 FW |
528 | static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr, |
529 | void *info) | |
0ca743a5 | 530 | { |
0ca743a5 PNA |
531 | struct xt_match *match = expr->ops->data; |
532 | ||
533 | if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) || | |
534 | nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) || | |
756c1b1a | 535 | nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(match->matchsize), info)) |
0ca743a5 PNA |
536 | goto nla_put_failure; |
537 | ||
538 | return 0; | |
539 | ||
540 | nla_put_failure: | |
541 | return -1; | |
542 | } | |
543 | ||
741e0633 FW |
544 | static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr) |
545 | { | |
546 | return __nft_match_dump(skb, expr, nft_expr_priv(expr)); | |
547 | } | |
548 | ||
0c93dbed FW |
549 | static int nft_match_large_dump(struct sk_buff *skb, const struct nft_expr *e) |
550 | { | |
551 | struct nft_xt_match_priv *priv = nft_expr_priv(e); | |
552 | ||
553 | return __nft_match_dump(skb, e, priv->info); | |
554 | } | |
555 | ||
0ca743a5 PNA |
556 | static int nft_match_validate(const struct nft_ctx *ctx, |
557 | const struct nft_expr *expr, | |
558 | const struct nft_data **data) | |
559 | { | |
560 | struct xt_match *match = expr->ops->data; | |
561 | unsigned int hook_mask = 0; | |
f3f5dded | 562 | int ret; |
0ca743a5 | 563 | |
f323d954 | 564 | if (nft_is_base_chain(ctx->chain)) { |
0ca743a5 PNA |
565 | const struct nft_base_chain *basechain = |
566 | nft_base_chain(ctx->chain); | |
115a60b1 | 567 | const struct nf_hook_ops *ops = &basechain->ops[0]; |
0ca743a5 PNA |
568 | |
569 | hook_mask = 1 << ops->hooknum; | |
f7fb77fc | 570 | if (match->hooks && !(hook_mask & match->hooks)) |
f3f5dded | 571 | return -EINVAL; |
0ca743a5 | 572 | |
afefb6f9 | 573 | ret = nft_compat_chain_validate_dependency(match->table, |
f3f5dded PNA |
574 | ctx->chain); |
575 | if (ret < 0) | |
576 | return ret; | |
0ca743a5 PNA |
577 | } |
578 | return 0; | |
579 | } | |
580 | ||
581 | static int | |
582 | nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, | |
583 | int event, u16 family, const char *name, | |
584 | int rev, int target) | |
585 | { | |
586 | struct nlmsghdr *nlh; | |
587 | struct nfgenmsg *nfmsg; | |
588 | unsigned int flags = portid ? NLM_F_MULTI : 0; | |
589 | ||
dedb67c4 | 590 | event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event); |
0ca743a5 PNA |
591 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
592 | if (nlh == NULL) | |
593 | goto nlmsg_failure; | |
594 | ||
595 | nfmsg = nlmsg_data(nlh); | |
596 | nfmsg->nfgen_family = family; | |
597 | nfmsg->version = NFNETLINK_V0; | |
598 | nfmsg->res_id = 0; | |
599 | ||
600 | if (nla_put_string(skb, NFTA_COMPAT_NAME, name) || | |
601 | nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) || | |
602 | nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target))) | |
603 | goto nla_put_failure; | |
604 | ||
605 | nlmsg_end(skb, nlh); | |
606 | return skb->len; | |
607 | ||
608 | nlmsg_failure: | |
609 | nla_put_failure: | |
610 | nlmsg_cancel(skb, nlh); | |
611 | return -1; | |
612 | } | |
613 | ||
7b8002a1 PNA |
614 | static int nfnl_compat_get(struct net *net, struct sock *nfnl, |
615 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
616 | const struct nlattr * const tb[], |
617 | struct netlink_ext_ack *extack) | |
0ca743a5 PNA |
618 | { |
619 | int ret = 0, target; | |
620 | struct nfgenmsg *nfmsg; | |
621 | const char *fmt; | |
622 | const char *name; | |
623 | u32 rev; | |
624 | struct sk_buff *skb2; | |
625 | ||
626 | if (tb[NFTA_COMPAT_NAME] == NULL || | |
627 | tb[NFTA_COMPAT_REV] == NULL || | |
628 | tb[NFTA_COMPAT_TYPE] == NULL) | |
629 | return -EINVAL; | |
630 | ||
631 | name = nla_data(tb[NFTA_COMPAT_NAME]); | |
632 | rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV])); | |
633 | target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE])); | |
634 | ||
635 | nfmsg = nlmsg_data(nlh); | |
636 | ||
637 | switch(nfmsg->nfgen_family) { | |
638 | case AF_INET: | |
639 | fmt = "ipt_%s"; | |
640 | break; | |
641 | case AF_INET6: | |
642 | fmt = "ip6t_%s"; | |
643 | break; | |
5191f4d8 AB |
644 | case NFPROTO_BRIDGE: |
645 | fmt = "ebt_%s"; | |
646 | break; | |
5f158939 AB |
647 | case NFPROTO_ARP: |
648 | fmt = "arpt_%s"; | |
649 | break; | |
0ca743a5 PNA |
650 | default: |
651 | pr_err("nft_compat: unsupported protocol %d\n", | |
652 | nfmsg->nfgen_family); | |
653 | return -EINVAL; | |
654 | } | |
655 | ||
656 | try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name, | |
657 | rev, target, &ret), | |
658 | fmt, name); | |
659 | ||
660 | if (ret < 0) | |
661 | return ret; | |
662 | ||
663 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
664 | if (skb2 == NULL) | |
665 | return -ENOMEM; | |
666 | ||
667 | /* include the best revision for this extension in the message */ | |
668 | if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid, | |
669 | nlh->nlmsg_seq, | |
670 | NFNL_MSG_TYPE(nlh->nlmsg_type), | |
671 | NFNL_MSG_COMPAT_GET, | |
672 | nfmsg->nfgen_family, | |
673 | name, ret, target) <= 0) { | |
674 | kfree_skb(skb2); | |
675 | return -ENOSPC; | |
676 | } | |
677 | ||
678 | ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid, | |
679 | MSG_DONTWAIT); | |
680 | if (ret > 0) | |
681 | ret = 0; | |
682 | ||
683 | return ret == -EAGAIN ? -ENOBUFS : ret; | |
684 | } | |
685 | ||
686 | static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = { | |
687 | [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING, | |
688 | .len = NFT_COMPAT_NAME_MAX-1 }, | |
689 | [NFTA_COMPAT_REV] = { .type = NLA_U32 }, | |
690 | [NFTA_COMPAT_TYPE] = { .type = NLA_U32 }, | |
691 | }; | |
692 | ||
693 | static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = { | |
694 | [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get, | |
695 | .attr_count = NFTA_COMPAT_MAX, | |
696 | .policy = nfnl_compat_policy_get }, | |
697 | }; | |
698 | ||
699 | static const struct nfnetlink_subsystem nfnl_compat_subsys = { | |
700 | .name = "nft-compat", | |
701 | .subsys_id = NFNL_SUBSYS_NFT_COMPAT, | |
702 | .cb_count = NFNL_MSG_COMPAT_MAX, | |
703 | .cb = nfnl_nft_compat_cb, | |
704 | }; | |
705 | ||
706 | static LIST_HEAD(nft_match_list); | |
707 | ||
0ca743a5 PNA |
708 | static struct nft_expr_type nft_match_type; |
709 | ||
ba378ca9 PNA |
710 | static bool nft_match_cmp(const struct xt_match *match, |
711 | const char *name, u32 rev, u32 family) | |
712 | { | |
713 | return strcmp(match->name, name) == 0 && match->revision == rev && | |
714 | (match->family == NFPROTO_UNSPEC || match->family == family); | |
715 | } | |
716 | ||
0ca743a5 PNA |
717 | static const struct nft_expr_ops * |
718 | nft_match_select_ops(const struct nft_ctx *ctx, | |
719 | const struct nlattr * const tb[]) | |
720 | { | |
721 | struct nft_xt *nft_match; | |
722 | struct xt_match *match; | |
0c93dbed | 723 | unsigned int matchsize; |
0ca743a5 | 724 | char *mt_name; |
ba378ca9 | 725 | u32 rev, family; |
2bf4fade | 726 | int err; |
0ca743a5 PNA |
727 | |
728 | if (tb[NFTA_MATCH_NAME] == NULL || | |
729 | tb[NFTA_MATCH_REV] == NULL || | |
730 | tb[NFTA_MATCH_INFO] == NULL) | |
731 | return ERR_PTR(-EINVAL); | |
732 | ||
733 | mt_name = nla_data(tb[NFTA_MATCH_NAME]); | |
734 | rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV])); | |
735 | family = ctx->afi->family; | |
736 | ||
737 | /* Re-use the existing match if it's already loaded. */ | |
738 | list_for_each_entry(nft_match, &nft_match_list, head) { | |
739 | struct xt_match *match = nft_match->ops.data; | |
740 | ||
93020a5d | 741 | if (nft_match_cmp(match, mt_name, rev, family)) |
0ca743a5 PNA |
742 | return &nft_match->ops; |
743 | } | |
744 | ||
745 | match = xt_request_find_match(family, mt_name, rev); | |
746 | if (IS_ERR(match)) | |
747 | return ERR_PTR(-ENOENT); | |
748 | ||
2bf4fade LZ |
749 | if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) { |
750 | err = -EINVAL; | |
751 | goto err; | |
752 | } | |
f0716cd6 | 753 | |
0ca743a5 PNA |
754 | /* This is the first time we use this match, allocate operations */ |
755 | nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL); | |
2bf4fade LZ |
756 | if (nft_match == NULL) { |
757 | err = -ENOMEM; | |
758 | goto err; | |
759 | } | |
0ca743a5 | 760 | |
93020a5d | 761 | nft_match->refcnt = 0; |
0ca743a5 | 762 | nft_match->ops.type = &nft_match_type; |
0ca743a5 PNA |
763 | nft_match->ops.eval = nft_match_eval; |
764 | nft_match->ops.init = nft_match_init; | |
765 | nft_match->ops.destroy = nft_match_destroy; | |
766 | nft_match->ops.dump = nft_match_dump; | |
767 | nft_match->ops.validate = nft_match_validate; | |
768 | nft_match->ops.data = match; | |
769 | ||
0c93dbed FW |
770 | matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize)); |
771 | if (matchsize > NFT_MATCH_LARGE_THRESH) { | |
772 | matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv)); | |
773 | ||
774 | nft_match->ops.eval = nft_match_large_eval; | |
775 | nft_match->ops.init = nft_match_large_init; | |
776 | nft_match->ops.destroy = nft_match_large_destroy; | |
777 | nft_match->ops.dump = nft_match_large_dump; | |
778 | } | |
779 | ||
780 | nft_match->ops.size = matchsize; | |
781 | ||
0ca743a5 PNA |
782 | list_add(&nft_match->head, &nft_match_list); |
783 | ||
784 | return &nft_match->ops; | |
2bf4fade LZ |
785 | err: |
786 | module_put(match->me); | |
787 | return ERR_PTR(err); | |
0ca743a5 PNA |
788 | } |
789 | ||
0ca743a5 PNA |
790 | static struct nft_expr_type nft_match_type __read_mostly = { |
791 | .name = "match", | |
792 | .select_ops = nft_match_select_ops, | |
793 | .policy = nft_match_policy, | |
794 | .maxattr = NFTA_MATCH_MAX, | |
795 | .owner = THIS_MODULE, | |
796 | }; | |
797 | ||
798 | static LIST_HEAD(nft_target_list); | |
799 | ||
800 | static struct nft_expr_type nft_target_type; | |
801 | ||
ba378ca9 PNA |
802 | static bool nft_target_cmp(const struct xt_target *tg, |
803 | const char *name, u32 rev, u32 family) | |
804 | { | |
805 | return strcmp(tg->name, name) == 0 && tg->revision == rev && | |
806 | (tg->family == NFPROTO_UNSPEC || tg->family == family); | |
807 | } | |
808 | ||
0ca743a5 PNA |
809 | static const struct nft_expr_ops * |
810 | nft_target_select_ops(const struct nft_ctx *ctx, | |
811 | const struct nlattr * const tb[]) | |
812 | { | |
813 | struct nft_xt *nft_target; | |
814 | struct xt_target *target; | |
815 | char *tg_name; | |
ba378ca9 | 816 | u32 rev, family; |
2bf4fade | 817 | int err; |
0ca743a5 PNA |
818 | |
819 | if (tb[NFTA_TARGET_NAME] == NULL || | |
820 | tb[NFTA_TARGET_REV] == NULL || | |
821 | tb[NFTA_TARGET_INFO] == NULL) | |
822 | return ERR_PTR(-EINVAL); | |
823 | ||
824 | tg_name = nla_data(tb[NFTA_TARGET_NAME]); | |
825 | rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV])); | |
826 | family = ctx->afi->family; | |
827 | ||
828 | /* Re-use the existing target if it's already loaded. */ | |
7965ee93 | 829 | list_for_each_entry(nft_target, &nft_target_list, head) { |
0ca743a5 PNA |
830 | struct xt_target *target = nft_target->ops.data; |
831 | ||
93020a5d | 832 | if (nft_target_cmp(target, tg_name, rev, family)) |
0ca743a5 PNA |
833 | return &nft_target->ops; |
834 | } | |
835 | ||
836 | target = xt_request_find_target(family, tg_name, rev); | |
837 | if (IS_ERR(target)) | |
838 | return ERR_PTR(-ENOENT); | |
839 | ||
2bf4fade LZ |
840 | if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) { |
841 | err = -EINVAL; | |
842 | goto err; | |
843 | } | |
f0716cd6 | 844 | |
0ca743a5 PNA |
845 | /* This is the first time we use this target, allocate operations */ |
846 | nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL); | |
2bf4fade LZ |
847 | if (nft_target == NULL) { |
848 | err = -ENOMEM; | |
849 | goto err; | |
850 | } | |
0ca743a5 | 851 | |
93020a5d | 852 | nft_target->refcnt = 0; |
0ca743a5 | 853 | nft_target->ops.type = &nft_target_type; |
756c1b1a | 854 | nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize)); |
0ca743a5 PNA |
855 | nft_target->ops.init = nft_target_init; |
856 | nft_target->ops.destroy = nft_target_destroy; | |
857 | nft_target->ops.dump = nft_target_dump; | |
858 | nft_target->ops.validate = nft_target_validate; | |
859 | nft_target->ops.data = target; | |
860 | ||
5191f4d8 AB |
861 | if (family == NFPROTO_BRIDGE) |
862 | nft_target->ops.eval = nft_target_eval_bridge; | |
863 | else | |
864 | nft_target->ops.eval = nft_target_eval_xt; | |
865 | ||
0ca743a5 PNA |
866 | list_add(&nft_target->head, &nft_target_list); |
867 | ||
868 | return &nft_target->ops; | |
2bf4fade LZ |
869 | err: |
870 | module_put(target->me); | |
871 | return ERR_PTR(err); | |
0ca743a5 PNA |
872 | } |
873 | ||
0ca743a5 PNA |
874 | static struct nft_expr_type nft_target_type __read_mostly = { |
875 | .name = "target", | |
876 | .select_ops = nft_target_select_ops, | |
877 | .policy = nft_target_policy, | |
878 | .maxattr = NFTA_TARGET_MAX, | |
879 | .owner = THIS_MODULE, | |
880 | }; | |
881 | ||
882 | static int __init nft_compat_module_init(void) | |
883 | { | |
884 | int ret; | |
885 | ||
886 | ret = nft_register_expr(&nft_match_type); | |
887 | if (ret < 0) | |
888 | return ret; | |
889 | ||
890 | ret = nft_register_expr(&nft_target_type); | |
891 | if (ret < 0) | |
892 | goto err_match; | |
893 | ||
894 | ret = nfnetlink_subsys_register(&nfnl_compat_subsys); | |
895 | if (ret < 0) { | |
896 | pr_err("nft_compat: cannot register with nfnetlink.\n"); | |
897 | goto err_target; | |
898 | } | |
899 | ||
900 | pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n"); | |
901 | ||
902 | return ret; | |
903 | ||
904 | err_target: | |
905 | nft_unregister_expr(&nft_target_type); | |
906 | err_match: | |
907 | nft_unregister_expr(&nft_match_type); | |
908 | return ret; | |
909 | } | |
910 | ||
911 | static void __exit nft_compat_module_exit(void) | |
912 | { | |
93020a5d FW |
913 | struct nft_xt *xt, *next; |
914 | ||
915 | /* list should be empty here, it can be non-empty only in case there | |
916 | * was an error that caused nft_xt expr to not be initialized fully | |
917 | * and noone else requested the same expression later. | |
918 | * | |
919 | * In this case, the lists contain 0-refcount entries that still | |
920 | * hold module reference. | |
921 | */ | |
922 | list_for_each_entry_safe(xt, next, &nft_target_list, head) { | |
923 | struct xt_target *target = xt->ops.data; | |
924 | ||
925 | if (WARN_ON_ONCE(xt->refcnt)) | |
926 | continue; | |
927 | module_put(target->me); | |
928 | kfree(xt); | |
929 | } | |
930 | ||
931 | list_for_each_entry_safe(xt, next, &nft_match_list, head) { | |
932 | struct xt_match *match = xt->ops.data; | |
933 | ||
934 | if (WARN_ON_ONCE(xt->refcnt)) | |
935 | continue; | |
936 | module_put(match->me); | |
937 | kfree(xt); | |
938 | } | |
0ca743a5 PNA |
939 | nfnetlink_subsys_unregister(&nfnl_compat_subsys); |
940 | nft_unregister_expr(&nft_target_type); | |
941 | nft_unregister_expr(&nft_match_type); | |
0ca743a5 PNA |
942 | } |
943 | ||
944 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT); | |
945 | ||
946 | module_init(nft_compat_module_init); | |
947 | module_exit(nft_compat_module_exit); | |
948 | ||
949 | MODULE_LICENSE("GPL"); | |
950 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); | |
951 | MODULE_ALIAS_NFT_EXPR("match"); | |
952 | MODULE_ALIAS_NFT_EXPR("target"); |