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