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>
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>
25 #include <net/netns/generic.h>
28 struct list_head head;
29 struct nft_expr_ops ops;
32 /* used only when transaction mutex is locked */
35 /* Unlike other expressions, ops doesn't have static storage duration.
36 * nft core assumes they do. We use kfree_rcu so that nft core can
37 * can check expr->ops->size even after nft_compat->destroy() frees
38 * the nft_xt struct that holds the ops structure.
40 struct rcu_head rcu_head;
43 /* Used for matches where *info is larger than X byte */
44 #define NFT_MATCH_LARGE_THRESH 192
46 struct nft_xt_match_priv {
50 struct nft_compat_net {
51 struct list_head nft_target_list;
52 struct list_head nft_match_list;
55 static unsigned int nft_compat_net_id __read_mostly;
56 static struct nft_expr_type nft_match_type;
57 static struct nft_expr_type nft_target_type;
59 static struct nft_compat_net *nft_compat_pernet(struct net *net)
61 return net_generic(net, nft_compat_net_id);
64 static bool nft_xt_put(struct nft_xt *xt)
66 if (refcount_dec_and_test(&xt->refcnt)) {
67 WARN_ON_ONCE(!list_empty(&xt->head));
68 kfree_rcu(xt, rcu_head);
75 static int nft_compat_chain_validate_dependency(const struct nft_ctx *ctx,
76 const char *tablename)
78 enum nft_chain_types type = NFT_CHAIN_T_DEFAULT;
79 const struct nft_chain *chain = ctx->chain;
80 const struct nft_base_chain *basechain;
83 !nft_is_base_chain(chain))
86 basechain = nft_base_chain(chain);
87 if (strcmp(tablename, "nat") == 0) {
88 if (ctx->family != NFPROTO_BRIDGE)
89 type = NFT_CHAIN_T_NAT;
90 if (basechain->type->type != type)
100 struct ebt_entry ebt;
101 struct arpt_entry arp;
105 nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
108 par->targinfo = xt_info;
109 par->hotdrop = false;
112 static void nft_target_eval_xt(const struct nft_expr *expr,
113 struct nft_regs *regs,
114 const struct nft_pktinfo *pkt)
116 void *info = nft_expr_priv(expr);
117 struct xt_target *target = expr->ops->data;
118 struct sk_buff *skb = pkt->skb;
121 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
123 ret = target->target(skb, &pkt->xt);
130 regs->verdict.code = NFT_CONTINUE;
133 regs->verdict.code = ret;
138 static void nft_target_eval_bridge(const struct nft_expr *expr,
139 struct nft_regs *regs,
140 const struct nft_pktinfo *pkt)
142 void *info = nft_expr_priv(expr);
143 struct xt_target *target = expr->ops->data;
144 struct sk_buff *skb = pkt->skb;
147 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
149 ret = target->target(skb, &pkt->xt);
156 regs->verdict.code = NF_ACCEPT;
159 regs->verdict.code = NF_DROP;
162 regs->verdict.code = NFT_CONTINUE;
165 regs->verdict.code = NFT_RETURN;
168 regs->verdict.code = ret;
173 static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
174 [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING },
175 [NFTA_TARGET_REV] = { .type = NLA_U32 },
176 [NFTA_TARGET_INFO] = { .type = NLA_BINARY },
180 nft_target_set_tgchk_param(struct xt_tgchk_param *par,
181 const struct nft_ctx *ctx,
182 struct xt_target *target, void *info,
183 union nft_entry *entry, u16 proto, bool inv)
186 par->table = ctx->table->name;
187 switch (ctx->family) {
189 entry->e4.ip.proto = proto;
190 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
194 entry->e6.ipv6.flags |= IP6T_F_PROTO;
196 entry->e6.ipv6.proto = proto;
197 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
200 entry->ebt.ethproto = (__force __be16)proto;
201 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
206 par->entryinfo = entry;
207 par->target = target;
208 par->targinfo = info;
209 if (nft_is_base_chain(ctx->chain)) {
210 const struct nft_base_chain *basechain =
211 nft_base_chain(ctx->chain);
212 const struct nf_hook_ops *ops = &basechain->ops;
214 par->hook_mask = 1 << ops->hooknum;
218 par->family = ctx->family;
219 par->nft_compat = true;
222 static void target_compat_from_user(struct xt_target *t, void *in, void *out)
226 memcpy(out, in, t->targetsize);
227 pad = XT_ALIGN(t->targetsize) - t->targetsize;
229 memset(out + t->targetsize, 0, pad);
232 static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
233 [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 },
234 [NFTA_RULE_COMPAT_FLAGS] = { .type = NLA_U32 },
237 static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv)
239 struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
243 err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr,
244 nft_rule_compat_policy, NULL);
248 if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
251 flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
252 if (flags & ~NFT_RULE_COMPAT_F_MASK)
254 if (flags & NFT_RULE_COMPAT_F_INV)
257 *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
262 nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
263 const struct nlattr * const tb[])
265 void *info = nft_expr_priv(expr);
266 struct xt_target *target = expr->ops->data;
267 struct xt_tgchk_param par;
268 size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
269 struct nft_xt *nft_xt;
272 union nft_entry e = {};
275 target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
277 if (ctx->nla[NFTA_RULE_COMPAT]) {
278 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
283 nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
285 ret = xt_check_target(&par, size, proto, inv);
289 /* The standard target cannot be used */
293 nft_xt = container_of(expr->ops, struct nft_xt, ops);
294 refcount_inc(&nft_xt->refcnt);
299 nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
301 struct xt_target *target = expr->ops->data;
302 void *info = nft_expr_priv(expr);
303 struct xt_tgdtor_param par;
308 par.family = ctx->family;
309 if (par.target->destroy != NULL)
310 par.target->destroy(&par);
312 if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
313 module_put(target->me);
316 static int nft_extension_dump_info(struct sk_buff *skb, int attr,
318 unsigned int size, unsigned int user_size)
320 unsigned int info_size, aligned_size = XT_ALIGN(size);
323 nla = nla_reserve(skb, attr, aligned_size);
327 info_size = user_size ? : size;
328 memcpy(nla_data(nla), info, info_size);
329 memset(nla_data(nla) + info_size, 0, aligned_size - info_size);
334 static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
336 const struct xt_target *target = expr->ops->data;
337 void *info = nft_expr_priv(expr);
339 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
340 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
341 nft_extension_dump_info(skb, NFTA_TARGET_INFO, info,
342 target->targetsize, target->usersize))
343 goto nla_put_failure;
351 static int nft_target_validate(const struct nft_ctx *ctx,
352 const struct nft_expr *expr,
353 const struct nft_data **data)
355 struct xt_target *target = expr->ops->data;
356 unsigned int hook_mask = 0;
359 if (nft_is_base_chain(ctx->chain)) {
360 const struct nft_base_chain *basechain =
361 nft_base_chain(ctx->chain);
362 const struct nf_hook_ops *ops = &basechain->ops;
364 hook_mask = 1 << ops->hooknum;
365 if (target->hooks && !(hook_mask & target->hooks))
368 ret = nft_compat_chain_validate_dependency(ctx, target->table);
375 static void __nft_match_eval(const struct nft_expr *expr,
376 struct nft_regs *regs,
377 const struct nft_pktinfo *pkt,
380 struct xt_match *match = expr->ops->data;
381 struct sk_buff *skb = pkt->skb;
384 nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
386 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
388 if (pkt->xt.hotdrop) {
389 regs->verdict.code = NF_DROP;
393 switch (ret ? 1 : 0) {
395 regs->verdict.code = NFT_CONTINUE;
398 regs->verdict.code = NFT_BREAK;
403 static void nft_match_large_eval(const struct nft_expr *expr,
404 struct nft_regs *regs,
405 const struct nft_pktinfo *pkt)
407 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
409 __nft_match_eval(expr, regs, pkt, priv->info);
412 static void nft_match_eval(const struct nft_expr *expr,
413 struct nft_regs *regs,
414 const struct nft_pktinfo *pkt)
416 __nft_match_eval(expr, regs, pkt, nft_expr_priv(expr));
419 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
420 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
421 [NFTA_MATCH_REV] = { .type = NLA_U32 },
422 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
425 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
427 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
428 struct xt_match *match, void *info,
429 union nft_entry *entry, u16 proto, bool inv)
432 par->table = ctx->table->name;
433 switch (ctx->family) {
435 entry->e4.ip.proto = proto;
436 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
440 entry->e6.ipv6.flags |= IP6T_F_PROTO;
442 entry->e6.ipv6.proto = proto;
443 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
446 entry->ebt.ethproto = (__force __be16)proto;
447 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
452 par->entryinfo = entry;
454 par->matchinfo = info;
455 if (nft_is_base_chain(ctx->chain)) {
456 const struct nft_base_chain *basechain =
457 nft_base_chain(ctx->chain);
458 const struct nf_hook_ops *ops = &basechain->ops;
460 par->hook_mask = 1 << ops->hooknum;
464 par->family = ctx->family;
465 par->nft_compat = true;
468 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
472 memcpy(out, in, m->matchsize);
473 pad = XT_ALIGN(m->matchsize) - m->matchsize;
475 memset(out + m->matchsize, 0, pad);
479 __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
480 const struct nlattr * const tb[],
483 struct xt_match *match = expr->ops->data;
484 struct xt_mtchk_param par;
485 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
486 struct nft_xt *nft_xt;
489 union nft_entry e = {};
492 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
494 if (ctx->nla[NFTA_RULE_COMPAT]) {
495 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
500 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
502 ret = xt_check_match(&par, size, proto, inv);
506 nft_xt = container_of(expr->ops, struct nft_xt, ops);
507 refcount_inc(&nft_xt->refcnt);
512 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
513 const struct nlattr * const tb[])
515 return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
519 nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
520 const struct nlattr * const tb[])
522 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
523 struct xt_match *m = expr->ops->data;
526 priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL);
530 ret = __nft_match_init(ctx, expr, tb, priv->info);
537 __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
540 struct xt_match *match = expr->ops->data;
541 struct module *me = match->me;
542 struct xt_mtdtor_param par;
546 par.matchinfo = info;
547 par.family = ctx->family;
548 if (par.match->destroy != NULL)
549 par.match->destroy(&par);
551 if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
556 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
558 __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
561 static void nft_compat_activate(const struct nft_ctx *ctx,
562 const struct nft_expr *expr,
565 struct nft_xt *xt = container_of(expr->ops, struct nft_xt, ops);
567 if (xt->listcnt == 0)
568 list_add(&xt->head, h);
573 static void nft_compat_activate_mt(const struct nft_ctx *ctx,
574 const struct nft_expr *expr)
576 struct nft_compat_net *cn = nft_compat_pernet(ctx->net);
578 nft_compat_activate(ctx, expr, &cn->nft_match_list);
581 static void nft_compat_activate_tg(const struct nft_ctx *ctx,
582 const struct nft_expr *expr)
584 struct nft_compat_net *cn = nft_compat_pernet(ctx->net);
586 nft_compat_activate(ctx, expr, &cn->nft_target_list);
589 static void nft_compat_deactivate(const struct nft_ctx *ctx,
590 const struct nft_expr *expr)
592 struct nft_xt *xt = container_of(expr->ops, struct nft_xt, ops);
594 if (--xt->listcnt == 0)
595 list_del_init(&xt->head);
599 nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
601 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
603 __nft_match_destroy(ctx, expr, priv->info);
607 static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
610 struct xt_match *match = expr->ops->data;
612 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
613 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
614 nft_extension_dump_info(skb, NFTA_MATCH_INFO, info,
615 match->matchsize, match->usersize))
616 goto nla_put_failure;
624 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
626 return __nft_match_dump(skb, expr, nft_expr_priv(expr));
629 static int nft_match_large_dump(struct sk_buff *skb, const struct nft_expr *e)
631 struct nft_xt_match_priv *priv = nft_expr_priv(e);
633 return __nft_match_dump(skb, e, priv->info);
636 static int nft_match_validate(const struct nft_ctx *ctx,
637 const struct nft_expr *expr,
638 const struct nft_data **data)
640 struct xt_match *match = expr->ops->data;
641 unsigned int hook_mask = 0;
644 if (nft_is_base_chain(ctx->chain)) {
645 const struct nft_base_chain *basechain =
646 nft_base_chain(ctx->chain);
647 const struct nf_hook_ops *ops = &basechain->ops;
649 hook_mask = 1 << ops->hooknum;
650 if (match->hooks && !(hook_mask & match->hooks))
653 ret = nft_compat_chain_validate_dependency(ctx, match->table);
661 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
662 int event, u16 family, const char *name,
665 struct nlmsghdr *nlh;
666 struct nfgenmsg *nfmsg;
667 unsigned int flags = portid ? NLM_F_MULTI : 0;
669 event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
670 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
674 nfmsg = nlmsg_data(nlh);
675 nfmsg->nfgen_family = family;
676 nfmsg->version = NFNETLINK_V0;
679 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
680 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
681 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
682 goto nla_put_failure;
689 nlmsg_cancel(skb, nlh);
693 static int nfnl_compat_get_rcu(struct net *net, struct sock *nfnl,
694 struct sk_buff *skb, const struct nlmsghdr *nlh,
695 const struct nlattr * const tb[],
696 struct netlink_ext_ack *extack)
699 struct nfgenmsg *nfmsg;
703 struct sk_buff *skb2;
705 if (tb[NFTA_COMPAT_NAME] == NULL ||
706 tb[NFTA_COMPAT_REV] == NULL ||
707 tb[NFTA_COMPAT_TYPE] == NULL)
710 name = nla_data(tb[NFTA_COMPAT_NAME]);
711 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
712 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
714 nfmsg = nlmsg_data(nlh);
716 switch(nfmsg->nfgen_family) {
730 pr_err("nft_compat: unsupported protocol %d\n",
731 nfmsg->nfgen_family);
735 if (!try_module_get(THIS_MODULE))
739 try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
745 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
751 /* include the best revision for this extension in the message */
752 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
754 NFNL_MSG_TYPE(nlh->nlmsg_type),
757 name, ret, target) <= 0) {
762 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
768 module_put(THIS_MODULE);
769 return ret == -EAGAIN ? -ENOBUFS : ret;
772 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
773 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
774 .len = NFT_COMPAT_NAME_MAX-1 },
775 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
776 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
779 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
780 [NFNL_MSG_COMPAT_GET] = { .call_rcu = nfnl_compat_get_rcu,
781 .attr_count = NFTA_COMPAT_MAX,
782 .policy = nfnl_compat_policy_get },
785 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
786 .name = "nft-compat",
787 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
788 .cb_count = NFNL_MSG_COMPAT_MAX,
789 .cb = nfnl_nft_compat_cb,
792 static bool nft_match_cmp(const struct xt_match *match,
793 const char *name, u32 rev, u32 family)
795 return strcmp(match->name, name) == 0 && match->revision == rev &&
796 (match->family == NFPROTO_UNSPEC || match->family == family);
799 static const struct nft_expr_ops *
800 nft_match_select_ops(const struct nft_ctx *ctx,
801 const struct nlattr * const tb[])
803 struct nft_compat_net *cn;
804 struct nft_xt *nft_match;
805 struct xt_match *match;
806 unsigned int matchsize;
811 if (tb[NFTA_MATCH_NAME] == NULL ||
812 tb[NFTA_MATCH_REV] == NULL ||
813 tb[NFTA_MATCH_INFO] == NULL)
814 return ERR_PTR(-EINVAL);
816 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
817 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
818 family = ctx->family;
820 cn = nft_compat_pernet(ctx->net);
822 /* Re-use the existing match if it's already loaded. */
823 list_for_each_entry(nft_match, &cn->nft_match_list, head) {
824 struct xt_match *match = nft_match->ops.data;
826 if (nft_match_cmp(match, mt_name, rev, family))
827 return &nft_match->ops;
830 match = xt_request_find_match(family, mt_name, rev);
832 return ERR_PTR(-ENOENT);
834 if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
839 /* This is the first time we use this match, allocate operations */
840 nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
841 if (nft_match == NULL) {
846 refcount_set(&nft_match->refcnt, 0);
847 nft_match->ops.type = &nft_match_type;
848 nft_match->ops.eval = nft_match_eval;
849 nft_match->ops.init = nft_match_init;
850 nft_match->ops.destroy = nft_match_destroy;
851 nft_match->ops.activate = nft_compat_activate_mt;
852 nft_match->ops.deactivate = nft_compat_deactivate;
853 nft_match->ops.dump = nft_match_dump;
854 nft_match->ops.validate = nft_match_validate;
855 nft_match->ops.data = match;
857 matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
858 if (matchsize > NFT_MATCH_LARGE_THRESH) {
859 matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
861 nft_match->ops.eval = nft_match_large_eval;
862 nft_match->ops.init = nft_match_large_init;
863 nft_match->ops.destroy = nft_match_large_destroy;
864 nft_match->ops.dump = nft_match_large_dump;
867 nft_match->ops.size = matchsize;
869 nft_match->listcnt = 1;
870 list_add(&nft_match->head, &cn->nft_match_list);
872 return &nft_match->ops;
874 module_put(match->me);
878 static struct nft_expr_type nft_match_type __read_mostly = {
880 .select_ops = nft_match_select_ops,
881 .policy = nft_match_policy,
882 .maxattr = NFTA_MATCH_MAX,
883 .owner = THIS_MODULE,
886 static bool nft_target_cmp(const struct xt_target *tg,
887 const char *name, u32 rev, u32 family)
889 return strcmp(tg->name, name) == 0 && tg->revision == rev &&
890 (tg->family == NFPROTO_UNSPEC || tg->family == family);
893 static const struct nft_expr_ops *
894 nft_target_select_ops(const struct nft_ctx *ctx,
895 const struct nlattr * const tb[])
897 struct nft_compat_net *cn;
898 struct nft_xt *nft_target;
899 struct xt_target *target;
904 if (tb[NFTA_TARGET_NAME] == NULL ||
905 tb[NFTA_TARGET_REV] == NULL ||
906 tb[NFTA_TARGET_INFO] == NULL)
907 return ERR_PTR(-EINVAL);
909 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
910 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
911 family = ctx->family;
913 if (strcmp(tg_name, XT_ERROR_TARGET) == 0 ||
914 strcmp(tg_name, XT_STANDARD_TARGET) == 0 ||
915 strcmp(tg_name, "standard") == 0)
916 return ERR_PTR(-EINVAL);
918 cn = nft_compat_pernet(ctx->net);
919 /* Re-use the existing target if it's already loaded. */
920 list_for_each_entry(nft_target, &cn->nft_target_list, head) {
921 struct xt_target *target = nft_target->ops.data;
926 if (nft_target_cmp(target, tg_name, rev, family))
927 return &nft_target->ops;
930 target = xt_request_find_target(family, tg_name, rev);
932 return ERR_PTR(-ENOENT);
934 if (!target->target) {
939 if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
944 /* This is the first time we use this target, allocate operations */
945 nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
946 if (nft_target == NULL) {
951 refcount_set(&nft_target->refcnt, 0);
952 nft_target->ops.type = &nft_target_type;
953 nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
954 nft_target->ops.init = nft_target_init;
955 nft_target->ops.destroy = nft_target_destroy;
956 nft_target->ops.activate = nft_compat_activate_tg;
957 nft_target->ops.deactivate = nft_compat_deactivate;
958 nft_target->ops.dump = nft_target_dump;
959 nft_target->ops.validate = nft_target_validate;
960 nft_target->ops.data = target;
962 if (family == NFPROTO_BRIDGE)
963 nft_target->ops.eval = nft_target_eval_bridge;
965 nft_target->ops.eval = nft_target_eval_xt;
967 nft_target->listcnt = 1;
968 list_add(&nft_target->head, &cn->nft_target_list);
970 return &nft_target->ops;
972 module_put(target->me);
976 static struct nft_expr_type nft_target_type __read_mostly = {
978 .select_ops = nft_target_select_ops,
979 .policy = nft_target_policy,
980 .maxattr = NFTA_TARGET_MAX,
981 .owner = THIS_MODULE,
984 static int __net_init nft_compat_init_net(struct net *net)
986 struct nft_compat_net *cn = nft_compat_pernet(net);
988 INIT_LIST_HEAD(&cn->nft_target_list);
989 INIT_LIST_HEAD(&cn->nft_match_list);
994 static void __net_exit nft_compat_exit_net(struct net *net)
996 struct nft_compat_net *cn = nft_compat_pernet(net);
997 struct nft_xt *xt, *next;
999 if (list_empty(&cn->nft_match_list) &&
1000 list_empty(&cn->nft_target_list))
1003 /* If there was an error that caused nft_xt expr to not be initialized
1004 * fully and noone else requested the same expression later, the lists
1005 * contain 0-refcount entries that still hold module reference.
1009 mutex_lock(&net->nft.commit_mutex);
1010 list_for_each_entry_safe(xt, next, &cn->nft_target_list, head) {
1011 struct xt_target *target = xt->ops.data;
1013 list_del_init(&xt->head);
1015 if (refcount_read(&xt->refcnt))
1017 module_put(target->me);
1021 list_for_each_entry_safe(xt, next, &cn->nft_match_list, head) {
1022 struct xt_match *match = xt->ops.data;
1024 list_del_init(&xt->head);
1026 if (refcount_read(&xt->refcnt))
1028 module_put(match->me);
1031 mutex_unlock(&net->nft.commit_mutex);
1034 static struct pernet_operations nft_compat_net_ops = {
1035 .init = nft_compat_init_net,
1036 .exit = nft_compat_exit_net,
1037 .id = &nft_compat_net_id,
1038 .size = sizeof(struct nft_compat_net),
1041 static int __init nft_compat_module_init(void)
1045 ret = register_pernet_subsys(&nft_compat_net_ops);
1049 ret = nft_register_expr(&nft_match_type);
1053 ret = nft_register_expr(&nft_target_type);
1057 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
1059 pr_err("nft_compat: cannot register with nfnetlink.\n");
1065 nft_unregister_expr(&nft_target_type);
1067 nft_unregister_expr(&nft_match_type);
1069 unregister_pernet_subsys(&nft_compat_net_ops);
1073 static void __exit nft_compat_module_exit(void)
1075 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
1076 nft_unregister_expr(&nft_target_type);
1077 nft_unregister_expr(&nft_match_type);
1078 unregister_pernet_subsys(&nft_compat_net_ops);
1081 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
1083 module_init(nft_compat_module_init);
1084 module_exit(nft_compat_module_exit);
1086 MODULE_LICENSE("GPL");
1087 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
1088 MODULE_ALIAS_NFT_EXPR("match");
1089 MODULE_ALIAS_NFT_EXPR("target");