x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / net / netfilter / nft_compat.c
blob7344ec7fff2a792bb39730442387801bc38754db
1 /*
2 * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
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 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
9 */
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>
22 #include <linux/netfilter_bridge/ebtables.h>
23 #include <linux/netfilter_arp/arp_tables.h>
24 #include <net/netfilter/nf_tables.h>
26 struct nft_xt {
27 struct list_head head;
28 struct nft_expr_ops ops;
29 unsigned int refcnt;
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.
36 struct rcu_head rcu_head;
39 /* Used for matches where *info is larger than X byte */
40 #define NFT_MATCH_LARGE_THRESH 192
42 struct nft_xt_match_priv {
43 void *info;
46 static bool nft_xt_put(struct nft_xt *xt)
48 if (--xt->refcnt == 0) {
49 list_del(&xt->head);
50 kfree_rcu(xt, rcu_head);
51 return true;
54 return false;
57 static int nft_compat_chain_validate_dependency(const char *tablename,
58 const struct nft_chain *chain)
60 const struct nft_base_chain *basechain;
62 if (!tablename ||
63 !nft_is_base_chain(chain))
64 return 0;
66 basechain = nft_base_chain(chain);
67 if (strcmp(tablename, "nat") == 0 &&
68 basechain->type->type != NFT_CHAIN_T_NAT)
69 return -EINVAL;
71 return 0;
74 union nft_entry {
75 struct ipt_entry e4;
76 struct ip6t_entry e6;
77 struct ebt_entry ebt;
78 struct arpt_entry arp;
81 static inline void
82 nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
84 par->target = xt;
85 par->targinfo = xt_info;
86 par->hotdrop = false;
89 static void nft_target_eval_xt(const struct nft_expr *expr,
90 struct nft_regs *regs,
91 const struct nft_pktinfo *pkt)
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;
98 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
100 ret = target->target(skb, &pkt->xt);
102 if (pkt->xt.hotdrop)
103 ret = NF_DROP;
105 switch (ret) {
106 case XT_CONTINUE:
107 regs->verdict.code = NFT_CONTINUE;
108 break;
109 default:
110 regs->verdict.code = ret;
111 break;
115 static void nft_target_eval_bridge(const struct nft_expr *expr,
116 struct nft_regs *regs,
117 const struct nft_pktinfo *pkt)
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;
124 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
126 ret = target->target(skb, &pkt->xt);
128 if (pkt->xt.hotdrop)
129 ret = NF_DROP;
131 switch (ret) {
132 case EBT_ACCEPT:
133 regs->verdict.code = NF_ACCEPT;
134 break;
135 case EBT_DROP:
136 regs->verdict.code = NF_DROP;
137 break;
138 case EBT_CONTINUE:
139 regs->verdict.code = NFT_CONTINUE;
140 break;
141 case EBT_RETURN:
142 regs->verdict.code = NFT_RETURN;
143 break;
144 default:
145 regs->verdict.code = ret;
146 break;
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 },
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,
160 union nft_entry *entry, u16 proto, bool inv)
162 par->net = ctx->net;
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:
170 if (proto)
171 entry->e6.ipv6.flags |= IP6T_F_PROTO;
173 entry->e6.ipv6.proto = proto;
174 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
175 break;
176 case NFPROTO_BRIDGE:
177 entry->ebt.ethproto = (__force __be16)proto;
178 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
179 break;
180 case NFPROTO_ARP:
181 break;
183 par->entryinfo = entry;
184 par->target = target;
185 par->targinfo = info;
186 if (nft_is_base_chain(ctx->chain)) {
187 const struct nft_base_chain *basechain =
188 nft_base_chain(ctx->chain);
189 const struct nf_hook_ops *ops = &basechain->ops[0];
191 par->hook_mask = 1 << ops->hooknum;
192 } else {
193 par->hook_mask = 0;
195 par->family = ctx->afi->family;
196 par->nft_compat = true;
199 static void target_compat_from_user(struct xt_target *t, void *in, void *out)
201 int pad;
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);
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 },
214 static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv)
216 struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
217 u32 flags;
218 int err;
220 err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr,
221 nft_rule_compat_policy, NULL);
222 if (err < 0)
223 return err;
225 if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
226 return -EINVAL;
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;
234 *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
235 return 0;
238 static int
239 nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
240 const struct nlattr * const tb[])
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]));
246 struct nft_xt *nft_xt;
247 u16 proto = 0;
248 bool inv = false;
249 union nft_entry e = {};
250 int ret;
252 target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
254 if (ctx->nla[NFTA_RULE_COMPAT]) {
255 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
256 if (ret < 0)
257 return ret;
260 nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
262 ret = xt_check_target(&par, size, proto, inv);
263 if (ret < 0)
264 return ret;
266 /* The standard target cannot be used */
267 if (!target->target)
268 return -EINVAL;
270 nft_xt = container_of(expr->ops, struct nft_xt, ops);
271 nft_xt->refcnt++;
272 return 0;
275 static void
276 nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
278 struct xt_target *target = expr->ops->data;
279 void *info = nft_expr_priv(expr);
280 struct module *me = target->me;
281 struct xt_tgdtor_param par;
283 par.net = ctx->net;
284 par.target = target;
285 par.targinfo = info;
286 par.family = ctx->afi->family;
287 if (par.target->destroy != NULL)
288 par.target->destroy(&par);
290 if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
291 module_put(me);
294 static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
296 const struct xt_target *target = expr->ops->data;
297 void *info = nft_expr_priv(expr);
299 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
300 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
301 nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(target->targetsize), info))
302 goto nla_put_failure;
304 return 0;
306 nla_put_failure:
307 return -1;
310 static int nft_target_validate(const struct nft_ctx *ctx,
311 const struct nft_expr *expr,
312 const struct nft_data **data)
314 struct xt_target *target = expr->ops->data;
315 unsigned int hook_mask = 0;
316 int ret;
318 if (nft_is_base_chain(ctx->chain)) {
319 const struct nft_base_chain *basechain =
320 nft_base_chain(ctx->chain);
321 const struct nf_hook_ops *ops = &basechain->ops[0];
323 hook_mask = 1 << ops->hooknum;
324 if (target->hooks && !(hook_mask & target->hooks))
325 return -EINVAL;
327 ret = nft_compat_chain_validate_dependency(target->table,
328 ctx->chain);
329 if (ret < 0)
330 return ret;
332 return 0;
335 static void __nft_match_eval(const struct nft_expr *expr,
336 struct nft_regs *regs,
337 const struct nft_pktinfo *pkt,
338 void *info)
340 struct xt_match *match = expr->ops->data;
341 struct sk_buff *skb = pkt->skb;
342 bool ret;
344 nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
346 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
348 if (pkt->xt.hotdrop) {
349 regs->verdict.code = NF_DROP;
350 return;
353 switch (ret ? 1 : 0) {
354 case 1:
355 regs->verdict.code = NFT_CONTINUE;
356 break;
357 case 0:
358 regs->verdict.code = NFT_BREAK;
359 break;
363 static void nft_match_large_eval(const struct nft_expr *expr,
364 struct nft_regs *regs,
365 const struct nft_pktinfo *pkt)
367 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
369 __nft_match_eval(expr, regs, pkt, priv->info);
372 static void nft_match_eval(const struct nft_expr *expr,
373 struct nft_regs *regs,
374 const struct nft_pktinfo *pkt)
376 __nft_match_eval(expr, regs, pkt, nft_expr_priv(expr));
379 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
380 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
381 [NFTA_MATCH_REV] = { .type = NLA_U32 },
382 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
385 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
386 static void
387 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
388 struct xt_match *match, void *info,
389 union nft_entry *entry, u16 proto, bool inv)
391 par->net = ctx->net;
392 par->table = ctx->table->name;
393 switch (ctx->afi->family) {
394 case AF_INET:
395 entry->e4.ip.proto = proto;
396 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
397 break;
398 case AF_INET6:
399 if (proto)
400 entry->e6.ipv6.flags |= IP6T_F_PROTO;
402 entry->e6.ipv6.proto = proto;
403 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
404 break;
405 case NFPROTO_BRIDGE:
406 entry->ebt.ethproto = (__force __be16)proto;
407 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
408 break;
409 case NFPROTO_ARP:
410 break;
412 par->entryinfo = entry;
413 par->match = match;
414 par->matchinfo = info;
415 if (nft_is_base_chain(ctx->chain)) {
416 const struct nft_base_chain *basechain =
417 nft_base_chain(ctx->chain);
418 const struct nf_hook_ops *ops = &basechain->ops[0];
420 par->hook_mask = 1 << ops->hooknum;
421 } else {
422 par->hook_mask = 0;
424 par->family = ctx->afi->family;
425 par->nft_compat = true;
428 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
430 int pad;
432 memcpy(out, in, m->matchsize);
433 pad = XT_ALIGN(m->matchsize) - m->matchsize;
434 if (pad > 0)
435 memset(out + m->matchsize, 0, pad);
438 static int
439 __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
440 const struct nlattr * const tb[],
441 void *info)
443 struct xt_match *match = expr->ops->data;
444 struct xt_mtchk_param par;
445 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
446 struct nft_xt *nft_xt;
447 u16 proto = 0;
448 bool inv = false;
449 union nft_entry e = {};
450 int ret;
452 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
454 if (ctx->nla[NFTA_RULE_COMPAT]) {
455 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
456 if (ret < 0)
457 return ret;
460 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
462 ret = xt_check_match(&par, size, proto, inv);
463 if (ret < 0)
464 return ret;
466 nft_xt = container_of(expr->ops, struct nft_xt, ops);
467 nft_xt->refcnt++;
468 return 0;
471 static int
472 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
473 const struct nlattr * const tb[])
475 return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
478 static int
479 nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
480 const struct nlattr * const tb[])
482 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
483 struct xt_match *m = expr->ops->data;
484 int ret;
486 priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL);
487 if (!priv->info)
488 return -ENOMEM;
490 ret = __nft_match_init(ctx, expr, tb, priv->info);
491 if (ret)
492 kfree(priv->info);
493 return ret;
496 static void
497 __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
498 void *info)
500 struct xt_match *match = expr->ops->data;
501 struct module *me = match->me;
502 struct xt_mtdtor_param par;
504 par.net = ctx->net;
505 par.match = match;
506 par.matchinfo = info;
507 par.family = ctx->afi->family;
508 if (par.match->destroy != NULL)
509 par.match->destroy(&par);
511 if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
512 module_put(me);
515 static void
516 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
518 __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
521 static void
522 nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
524 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
526 __nft_match_destroy(ctx, expr, priv->info);
527 kfree(priv->info);
530 static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
531 void *info)
533 struct xt_match *match = expr->ops->data;
535 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
536 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
537 nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(match->matchsize), info))
538 goto nla_put_failure;
540 return 0;
542 nla_put_failure:
543 return -1;
546 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
548 return __nft_match_dump(skb, expr, nft_expr_priv(expr));
551 static int nft_match_large_dump(struct sk_buff *skb, const struct nft_expr *e)
553 struct nft_xt_match_priv *priv = nft_expr_priv(e);
555 return __nft_match_dump(skb, e, priv->info);
558 static int nft_match_validate(const struct nft_ctx *ctx,
559 const struct nft_expr *expr,
560 const struct nft_data **data)
562 struct xt_match *match = expr->ops->data;
563 unsigned int hook_mask = 0;
564 int ret;
566 if (nft_is_base_chain(ctx->chain)) {
567 const struct nft_base_chain *basechain =
568 nft_base_chain(ctx->chain);
569 const struct nf_hook_ops *ops = &basechain->ops[0];
571 hook_mask = 1 << ops->hooknum;
572 if (match->hooks && !(hook_mask & match->hooks))
573 return -EINVAL;
575 ret = nft_compat_chain_validate_dependency(match->table,
576 ctx->chain);
577 if (ret < 0)
578 return ret;
580 return 0;
583 static int
584 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
585 int event, u16 family, const char *name,
586 int rev, int target)
588 struct nlmsghdr *nlh;
589 struct nfgenmsg *nfmsg;
590 unsigned int flags = portid ? NLM_F_MULTI : 0;
592 event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
593 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
594 if (nlh == NULL)
595 goto nlmsg_failure;
597 nfmsg = nlmsg_data(nlh);
598 nfmsg->nfgen_family = family;
599 nfmsg->version = NFNETLINK_V0;
600 nfmsg->res_id = 0;
602 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
603 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
604 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
605 goto nla_put_failure;
607 nlmsg_end(skb, nlh);
608 return skb->len;
610 nlmsg_failure:
611 nla_put_failure:
612 nlmsg_cancel(skb, nlh);
613 return -1;
616 static int nfnl_compat_get(struct net *net, struct sock *nfnl,
617 struct sk_buff *skb, const struct nlmsghdr *nlh,
618 const struct nlattr * const tb[],
619 struct netlink_ext_ack *extack)
621 int ret = 0, target;
622 struct nfgenmsg *nfmsg;
623 const char *fmt;
624 const char *name;
625 u32 rev;
626 struct sk_buff *skb2;
628 if (tb[NFTA_COMPAT_NAME] == NULL ||
629 tb[NFTA_COMPAT_REV] == NULL ||
630 tb[NFTA_COMPAT_TYPE] == NULL)
631 return -EINVAL;
633 name = nla_data(tb[NFTA_COMPAT_NAME]);
634 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
635 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
637 nfmsg = nlmsg_data(nlh);
639 switch(nfmsg->nfgen_family) {
640 case AF_INET:
641 fmt = "ipt_%s";
642 break;
643 case AF_INET6:
644 fmt = "ip6t_%s";
645 break;
646 case NFPROTO_BRIDGE:
647 fmt = "ebt_%s";
648 break;
649 case NFPROTO_ARP:
650 fmt = "arpt_%s";
651 break;
652 default:
653 pr_err("nft_compat: unsupported protocol %d\n",
654 nfmsg->nfgen_family);
655 return -EINVAL;
658 try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
659 rev, target, &ret),
660 fmt, name);
662 if (ret < 0)
663 return ret;
665 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
666 if (skb2 == NULL)
667 return -ENOMEM;
669 /* include the best revision for this extension in the message */
670 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
671 nlh->nlmsg_seq,
672 NFNL_MSG_TYPE(nlh->nlmsg_type),
673 NFNL_MSG_COMPAT_GET,
674 nfmsg->nfgen_family,
675 name, ret, target) <= 0) {
676 kfree_skb(skb2);
677 return -ENOSPC;
680 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
681 MSG_DONTWAIT);
682 if (ret > 0)
683 ret = 0;
685 return ret == -EAGAIN ? -ENOBUFS : ret;
688 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
689 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
690 .len = NFT_COMPAT_NAME_MAX-1 },
691 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
692 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
695 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
696 [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get,
697 .attr_count = NFTA_COMPAT_MAX,
698 .policy = nfnl_compat_policy_get },
701 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
702 .name = "nft-compat",
703 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
704 .cb_count = NFNL_MSG_COMPAT_MAX,
705 .cb = nfnl_nft_compat_cb,
708 static LIST_HEAD(nft_match_list);
710 static struct nft_expr_type nft_match_type;
712 static bool nft_match_cmp(const struct xt_match *match,
713 const char *name, u32 rev, u32 family)
715 return strcmp(match->name, name) == 0 && match->revision == rev &&
716 (match->family == NFPROTO_UNSPEC || match->family == family);
719 static const struct nft_expr_ops *
720 nft_match_select_ops(const struct nft_ctx *ctx,
721 const struct nlattr * const tb[])
723 struct nft_xt *nft_match;
724 struct xt_match *match;
725 unsigned int matchsize;
726 char *mt_name;
727 u32 rev, family;
728 int err;
730 if (tb[NFTA_MATCH_NAME] == NULL ||
731 tb[NFTA_MATCH_REV] == NULL ||
732 tb[NFTA_MATCH_INFO] == NULL)
733 return ERR_PTR(-EINVAL);
735 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
736 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
737 family = ctx->afi->family;
739 /* Re-use the existing match if it's already loaded. */
740 list_for_each_entry(nft_match, &nft_match_list, head) {
741 struct xt_match *match = nft_match->ops.data;
743 if (nft_match_cmp(match, mt_name, rev, family))
744 return &nft_match->ops;
747 match = xt_request_find_match(family, mt_name, rev);
748 if (IS_ERR(match))
749 return ERR_PTR(-ENOENT);
751 if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
752 err = -EINVAL;
753 goto err;
756 /* This is the first time we use this match, allocate operations */
757 nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
758 if (nft_match == NULL) {
759 err = -ENOMEM;
760 goto err;
763 nft_match->refcnt = 0;
764 nft_match->ops.type = &nft_match_type;
765 nft_match->ops.eval = nft_match_eval;
766 nft_match->ops.init = nft_match_init;
767 nft_match->ops.destroy = nft_match_destroy;
768 nft_match->ops.dump = nft_match_dump;
769 nft_match->ops.validate = nft_match_validate;
770 nft_match->ops.data = match;
772 matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
773 if (matchsize > NFT_MATCH_LARGE_THRESH) {
774 matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
776 nft_match->ops.eval = nft_match_large_eval;
777 nft_match->ops.init = nft_match_large_init;
778 nft_match->ops.destroy = nft_match_large_destroy;
779 nft_match->ops.dump = nft_match_large_dump;
782 nft_match->ops.size = matchsize;
784 list_add(&nft_match->head, &nft_match_list);
786 return &nft_match->ops;
787 err:
788 module_put(match->me);
789 return ERR_PTR(err);
792 static struct nft_expr_type nft_match_type __read_mostly = {
793 .name = "match",
794 .select_ops = nft_match_select_ops,
795 .policy = nft_match_policy,
796 .maxattr = NFTA_MATCH_MAX,
797 .owner = THIS_MODULE,
800 static LIST_HEAD(nft_target_list);
802 static struct nft_expr_type nft_target_type;
804 static bool nft_target_cmp(const struct xt_target *tg,
805 const char *name, u32 rev, u32 family)
807 return strcmp(tg->name, name) == 0 && tg->revision == rev &&
808 (tg->family == NFPROTO_UNSPEC || tg->family == family);
811 static const struct nft_expr_ops *
812 nft_target_select_ops(const struct nft_ctx *ctx,
813 const struct nlattr * const tb[])
815 struct nft_xt *nft_target;
816 struct xt_target *target;
817 char *tg_name;
818 u32 rev, family;
819 int err;
821 if (tb[NFTA_TARGET_NAME] == NULL ||
822 tb[NFTA_TARGET_REV] == NULL ||
823 tb[NFTA_TARGET_INFO] == NULL)
824 return ERR_PTR(-EINVAL);
826 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
827 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
828 family = ctx->afi->family;
830 if (strcmp(tg_name, XT_ERROR_TARGET) == 0 ||
831 strcmp(tg_name, XT_STANDARD_TARGET) == 0 ||
832 strcmp(tg_name, "standard") == 0)
833 return ERR_PTR(-EINVAL);
835 /* Re-use the existing target if it's already loaded. */
836 list_for_each_entry(nft_target, &nft_target_list, head) {
837 struct xt_target *target = nft_target->ops.data;
839 if (!target->target)
840 continue;
842 if (nft_target_cmp(target, tg_name, rev, family))
843 return &nft_target->ops;
846 target = xt_request_find_target(family, tg_name, rev);
847 if (IS_ERR(target))
848 return ERR_PTR(-ENOENT);
850 if (!target->target) {
851 err = -EINVAL;
852 goto err;
855 if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
856 err = -EINVAL;
857 goto err;
860 /* This is the first time we use this target, allocate operations */
861 nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
862 if (nft_target == NULL) {
863 err = -ENOMEM;
864 goto err;
867 nft_target->refcnt = 0;
868 nft_target->ops.type = &nft_target_type;
869 nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
870 nft_target->ops.init = nft_target_init;
871 nft_target->ops.destroy = nft_target_destroy;
872 nft_target->ops.dump = nft_target_dump;
873 nft_target->ops.validate = nft_target_validate;
874 nft_target->ops.data = target;
876 if (family == NFPROTO_BRIDGE)
877 nft_target->ops.eval = nft_target_eval_bridge;
878 else
879 nft_target->ops.eval = nft_target_eval_xt;
881 list_add(&nft_target->head, &nft_target_list);
883 return &nft_target->ops;
884 err:
885 module_put(target->me);
886 return ERR_PTR(err);
889 static struct nft_expr_type nft_target_type __read_mostly = {
890 .name = "target",
891 .select_ops = nft_target_select_ops,
892 .policy = nft_target_policy,
893 .maxattr = NFTA_TARGET_MAX,
894 .owner = THIS_MODULE,
897 static int __init nft_compat_module_init(void)
899 int ret;
901 ret = nft_register_expr(&nft_match_type);
902 if (ret < 0)
903 return ret;
905 ret = nft_register_expr(&nft_target_type);
906 if (ret < 0)
907 goto err_match;
909 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
910 if (ret < 0) {
911 pr_err("nft_compat: cannot register with nfnetlink.\n");
912 goto err_target;
915 pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
917 return ret;
919 err_target:
920 nft_unregister_expr(&nft_target_type);
921 err_match:
922 nft_unregister_expr(&nft_match_type);
923 return ret;
926 static void __exit nft_compat_module_exit(void)
928 struct nft_xt *xt, *next;
930 /* list should be empty here, it can be non-empty only in case there
931 * was an error that caused nft_xt expr to not be initialized fully
932 * and noone else requested the same expression later.
934 * In this case, the lists contain 0-refcount entries that still
935 * hold module reference.
937 list_for_each_entry_safe(xt, next, &nft_target_list, head) {
938 struct xt_target *target = xt->ops.data;
940 if (WARN_ON_ONCE(xt->refcnt))
941 continue;
942 module_put(target->me);
943 kfree(xt);
946 list_for_each_entry_safe(xt, next, &nft_match_list, head) {
947 struct xt_match *match = xt->ops.data;
949 if (WARN_ON_ONCE(xt->refcnt))
950 continue;
951 module_put(match->me);
952 kfree(xt);
954 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
955 nft_unregister_expr(&nft_target_type);
956 nft_unregister_expr(&nft_match_type);
959 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
961 module_init(nft_compat_module_init);
962 module_exit(nft_compat_module_exit);
964 MODULE_LICENSE("GPL");
965 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
966 MODULE_ALIAS_NFT_EXPR("match");
967 MODULE_ALIAS_NFT_EXPR("target");