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>
27 struct list_head head;
28 struct nft_expr_ops ops;
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 {
46 static bool nft_xt_put(struct nft_xt *xt)
48 if (--xt->refcnt == 0) {
50 kfree_rcu(xt, rcu_head);
57 static int nft_compat_chain_validate_dependency(const char *tablename,
58 const struct nft_chain *chain)
60 const struct nft_base_chain *basechain;
63 !nft_is_base_chain(chain))
66 basechain = nft_base_chain(chain);
67 if (strcmp(tablename, "nat") == 0 &&
68 basechain->type->type != NFT_CHAIN_T_NAT)
78 struct arpt_entry arp;
82 nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
85 par->targinfo = xt_info;
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;
98 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
100 ret = target->target(skb, &pkt->xt);
107 regs->verdict.code = NFT_CONTINUE;
110 regs->verdict.code = ret;
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;
124 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
126 ret = target->target(skb, &pkt->xt);
133 regs->verdict.code = NF_ACCEPT;
136 regs->verdict.code = NF_DROP;
139 regs->verdict.code = NFT_CONTINUE;
142 regs->verdict.code = NFT_RETURN;
145 regs->verdict.code = ret;
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 },
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)
163 par->table = ctx->table->name;
164 switch (ctx->afi->family) {
166 entry->e4.ip.proto = proto;
167 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
171 entry->e6.ipv6.flags |= IP6T_F_PROTO;
173 entry->e6.ipv6.proto = proto;
174 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
177 entry->ebt.ethproto = (__force __be16)proto;
178 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
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;
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)
203 memcpy(out, in, t->targetsize);
204 pad = XT_ALIGN(t->targetsize) - t->targetsize;
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];
220 err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr,
221 nft_rule_compat_policy, NULL);
225 if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
228 flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
229 if (flags & ~NFT_RULE_COMPAT_F_MASK)
231 if (flags & NFT_RULE_COMPAT_F_INV)
234 *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
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;
249 union nft_entry e = {};
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);
260 nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
262 ret = xt_check_target(&par, size, proto, inv);
266 /* The standard target cannot be used */
270 nft_xt = container_of(expr->ops, struct nft_xt, ops);
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;
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)))
294 static int nft_extension_dump_info(struct sk_buff *skb, int attr,
296 unsigned int size, unsigned int user_size)
298 unsigned int info_size, aligned_size = XT_ALIGN(size);
301 nla = nla_reserve(skb, attr, aligned_size);
305 info_size = user_size ? : size;
306 memcpy(nla_data(nla), info, info_size);
307 memset(nla_data(nla) + info_size, 0, aligned_size - info_size);
312 static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
314 const struct xt_target *target = expr->ops->data;
315 void *info = nft_expr_priv(expr);
317 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
318 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
319 nft_extension_dump_info(skb, NFTA_TARGET_INFO, info,
320 target->targetsize, target->usersize))
321 goto nla_put_failure;
329 static int nft_target_validate(const struct nft_ctx *ctx,
330 const struct nft_expr *expr,
331 const struct nft_data **data)
333 struct xt_target *target = expr->ops->data;
334 unsigned int hook_mask = 0;
337 if (nft_is_base_chain(ctx->chain)) {
338 const struct nft_base_chain *basechain =
339 nft_base_chain(ctx->chain);
340 const struct nf_hook_ops *ops = &basechain->ops[0];
342 hook_mask = 1 << ops->hooknum;
343 if (target->hooks && !(hook_mask & target->hooks))
346 ret = nft_compat_chain_validate_dependency(target->table,
354 static void __nft_match_eval(const struct nft_expr *expr,
355 struct nft_regs *regs,
356 const struct nft_pktinfo *pkt,
359 struct xt_match *match = expr->ops->data;
360 struct sk_buff *skb = pkt->skb;
363 nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
365 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
367 if (pkt->xt.hotdrop) {
368 regs->verdict.code = NF_DROP;
372 switch (ret ? 1 : 0) {
374 regs->verdict.code = NFT_CONTINUE;
377 regs->verdict.code = NFT_BREAK;
382 static void nft_match_large_eval(const struct nft_expr *expr,
383 struct nft_regs *regs,
384 const struct nft_pktinfo *pkt)
386 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
388 __nft_match_eval(expr, regs, pkt, priv->info);
391 static void nft_match_eval(const struct nft_expr *expr,
392 struct nft_regs *regs,
393 const struct nft_pktinfo *pkt)
395 __nft_match_eval(expr, regs, pkt, nft_expr_priv(expr));
398 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
399 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
400 [NFTA_MATCH_REV] = { .type = NLA_U32 },
401 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
404 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
406 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
407 struct xt_match *match, void *info,
408 union nft_entry *entry, u16 proto, bool inv)
411 par->table = ctx->table->name;
412 switch (ctx->afi->family) {
414 entry->e4.ip.proto = proto;
415 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
419 entry->e6.ipv6.flags |= IP6T_F_PROTO;
421 entry->e6.ipv6.proto = proto;
422 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
425 entry->ebt.ethproto = (__force __be16)proto;
426 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
431 par->entryinfo = entry;
433 par->matchinfo = info;
434 if (nft_is_base_chain(ctx->chain)) {
435 const struct nft_base_chain *basechain =
436 nft_base_chain(ctx->chain);
437 const struct nf_hook_ops *ops = &basechain->ops[0];
439 par->hook_mask = 1 << ops->hooknum;
443 par->family = ctx->afi->family;
444 par->nft_compat = true;
447 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
451 memcpy(out, in, m->matchsize);
452 pad = XT_ALIGN(m->matchsize) - m->matchsize;
454 memset(out + m->matchsize, 0, pad);
458 __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
459 const struct nlattr * const tb[],
462 struct xt_match *match = expr->ops->data;
463 struct xt_mtchk_param par;
464 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
465 struct nft_xt *nft_xt;
468 union nft_entry e = {};
471 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
473 if (ctx->nla[NFTA_RULE_COMPAT]) {
474 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
479 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
481 ret = xt_check_match(&par, size, proto, inv);
485 nft_xt = container_of(expr->ops, struct nft_xt, ops);
491 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
492 const struct nlattr * const tb[])
494 return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
498 nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
499 const struct nlattr * const tb[])
501 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
502 struct xt_match *m = expr->ops->data;
505 priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL);
509 ret = __nft_match_init(ctx, expr, tb, priv->info);
516 __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
519 struct xt_match *match = expr->ops->data;
520 struct module *me = match->me;
521 struct xt_mtdtor_param par;
525 par.matchinfo = info;
526 par.family = ctx->afi->family;
527 if (par.match->destroy != NULL)
528 par.match->destroy(&par);
530 if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
535 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
537 __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
541 nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
543 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
545 __nft_match_destroy(ctx, expr, priv->info);
549 static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
552 struct xt_match *match = expr->ops->data;
554 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
555 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
556 nft_extension_dump_info(skb, NFTA_MATCH_INFO, info,
557 match->matchsize, match->usersize))
558 goto nla_put_failure;
566 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
568 return __nft_match_dump(skb, expr, nft_expr_priv(expr));
571 static int nft_match_large_dump(struct sk_buff *skb, const struct nft_expr *e)
573 struct nft_xt_match_priv *priv = nft_expr_priv(e);
575 return __nft_match_dump(skb, e, priv->info);
578 static int nft_match_validate(const struct nft_ctx *ctx,
579 const struct nft_expr *expr,
580 const struct nft_data **data)
582 struct xt_match *match = expr->ops->data;
583 unsigned int hook_mask = 0;
586 if (nft_is_base_chain(ctx->chain)) {
587 const struct nft_base_chain *basechain =
588 nft_base_chain(ctx->chain);
589 const struct nf_hook_ops *ops = &basechain->ops[0];
591 hook_mask = 1 << ops->hooknum;
592 if (match->hooks && !(hook_mask & match->hooks))
595 ret = nft_compat_chain_validate_dependency(match->table,
604 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
605 int event, u16 family, const char *name,
608 struct nlmsghdr *nlh;
609 struct nfgenmsg *nfmsg;
610 unsigned int flags = portid ? NLM_F_MULTI : 0;
612 event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
613 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
617 nfmsg = nlmsg_data(nlh);
618 nfmsg->nfgen_family = family;
619 nfmsg->version = NFNETLINK_V0;
622 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
623 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
624 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
625 goto nla_put_failure;
632 nlmsg_cancel(skb, nlh);
636 static int nfnl_compat_get(struct net *net, struct sock *nfnl,
637 struct sk_buff *skb, const struct nlmsghdr *nlh,
638 const struct nlattr * const tb[],
639 struct netlink_ext_ack *extack)
642 struct nfgenmsg *nfmsg;
646 struct sk_buff *skb2;
648 if (tb[NFTA_COMPAT_NAME] == NULL ||
649 tb[NFTA_COMPAT_REV] == NULL ||
650 tb[NFTA_COMPAT_TYPE] == NULL)
653 name = nla_data(tb[NFTA_COMPAT_NAME]);
654 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
655 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
657 nfmsg = nlmsg_data(nlh);
659 switch(nfmsg->nfgen_family) {
673 pr_err("nft_compat: unsupported protocol %d\n",
674 nfmsg->nfgen_family);
678 try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
685 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
689 /* include the best revision for this extension in the message */
690 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
692 NFNL_MSG_TYPE(nlh->nlmsg_type),
695 name, ret, target) <= 0) {
700 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
705 return ret == -EAGAIN ? -ENOBUFS : ret;
708 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
709 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
710 .len = NFT_COMPAT_NAME_MAX-1 },
711 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
712 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
715 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
716 [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get,
717 .attr_count = NFTA_COMPAT_MAX,
718 .policy = nfnl_compat_policy_get },
721 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
722 .name = "nft-compat",
723 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
724 .cb_count = NFNL_MSG_COMPAT_MAX,
725 .cb = nfnl_nft_compat_cb,
728 static LIST_HEAD(nft_match_list);
730 static struct nft_expr_type nft_match_type;
732 static bool nft_match_cmp(const struct xt_match *match,
733 const char *name, u32 rev, u32 family)
735 return strcmp(match->name, name) == 0 && match->revision == rev &&
736 (match->family == NFPROTO_UNSPEC || match->family == family);
739 static const struct nft_expr_ops *
740 nft_match_select_ops(const struct nft_ctx *ctx,
741 const struct nlattr * const tb[])
743 struct nft_xt *nft_match;
744 struct xt_match *match;
745 unsigned int matchsize;
750 if (tb[NFTA_MATCH_NAME] == NULL ||
751 tb[NFTA_MATCH_REV] == NULL ||
752 tb[NFTA_MATCH_INFO] == NULL)
753 return ERR_PTR(-EINVAL);
755 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
756 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
757 family = ctx->afi->family;
759 /* Re-use the existing match if it's already loaded. */
760 list_for_each_entry(nft_match, &nft_match_list, head) {
761 struct xt_match *match = nft_match->ops.data;
763 if (nft_match_cmp(match, mt_name, rev, family))
764 return &nft_match->ops;
767 match = xt_request_find_match(family, mt_name, rev);
769 return ERR_PTR(-ENOENT);
771 if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
776 /* This is the first time we use this match, allocate operations */
777 nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
778 if (nft_match == NULL) {
783 nft_match->refcnt = 0;
784 nft_match->ops.type = &nft_match_type;
785 nft_match->ops.eval = nft_match_eval;
786 nft_match->ops.init = nft_match_init;
787 nft_match->ops.destroy = nft_match_destroy;
788 nft_match->ops.dump = nft_match_dump;
789 nft_match->ops.validate = nft_match_validate;
790 nft_match->ops.data = match;
792 matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
793 if (matchsize > NFT_MATCH_LARGE_THRESH) {
794 matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
796 nft_match->ops.eval = nft_match_large_eval;
797 nft_match->ops.init = nft_match_large_init;
798 nft_match->ops.destroy = nft_match_large_destroy;
799 nft_match->ops.dump = nft_match_large_dump;
802 nft_match->ops.size = matchsize;
804 list_add(&nft_match->head, &nft_match_list);
806 return &nft_match->ops;
808 module_put(match->me);
812 static struct nft_expr_type nft_match_type __read_mostly = {
814 .select_ops = nft_match_select_ops,
815 .policy = nft_match_policy,
816 .maxattr = NFTA_MATCH_MAX,
817 .owner = THIS_MODULE,
820 static LIST_HEAD(nft_target_list);
822 static struct nft_expr_type nft_target_type;
824 static bool nft_target_cmp(const struct xt_target *tg,
825 const char *name, u32 rev, u32 family)
827 return strcmp(tg->name, name) == 0 && tg->revision == rev &&
828 (tg->family == NFPROTO_UNSPEC || tg->family == family);
831 static const struct nft_expr_ops *
832 nft_target_select_ops(const struct nft_ctx *ctx,
833 const struct nlattr * const tb[])
835 struct nft_xt *nft_target;
836 struct xt_target *target;
841 if (tb[NFTA_TARGET_NAME] == NULL ||
842 tb[NFTA_TARGET_REV] == NULL ||
843 tb[NFTA_TARGET_INFO] == NULL)
844 return ERR_PTR(-EINVAL);
846 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
847 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
848 family = ctx->afi->family;
850 if (strcmp(tg_name, XT_ERROR_TARGET) == 0 ||
851 strcmp(tg_name, XT_STANDARD_TARGET) == 0 ||
852 strcmp(tg_name, "standard") == 0)
853 return ERR_PTR(-EINVAL);
855 /* Re-use the existing target if it's already loaded. */
856 list_for_each_entry(nft_target, &nft_target_list, head) {
857 struct xt_target *target = nft_target->ops.data;
862 if (nft_target_cmp(target, tg_name, rev, family))
863 return &nft_target->ops;
866 target = xt_request_find_target(family, tg_name, rev);
868 return ERR_PTR(-ENOENT);
870 if (!target->target) {
875 if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
880 /* This is the first time we use this target, allocate operations */
881 nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
882 if (nft_target == NULL) {
887 nft_target->refcnt = 0;
888 nft_target->ops.type = &nft_target_type;
889 nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
890 nft_target->ops.init = nft_target_init;
891 nft_target->ops.destroy = nft_target_destroy;
892 nft_target->ops.dump = nft_target_dump;
893 nft_target->ops.validate = nft_target_validate;
894 nft_target->ops.data = target;
896 if (family == NFPROTO_BRIDGE)
897 nft_target->ops.eval = nft_target_eval_bridge;
899 nft_target->ops.eval = nft_target_eval_xt;
901 list_add(&nft_target->head, &nft_target_list);
903 return &nft_target->ops;
905 module_put(target->me);
909 static struct nft_expr_type nft_target_type __read_mostly = {
911 .select_ops = nft_target_select_ops,
912 .policy = nft_target_policy,
913 .maxattr = NFTA_TARGET_MAX,
914 .owner = THIS_MODULE,
917 static int __init nft_compat_module_init(void)
921 ret = nft_register_expr(&nft_match_type);
925 ret = nft_register_expr(&nft_target_type);
929 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
931 pr_err("nft_compat: cannot register with nfnetlink.\n");
935 pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
940 nft_unregister_expr(&nft_target_type);
942 nft_unregister_expr(&nft_match_type);
946 static void __exit nft_compat_module_exit(void)
948 struct nft_xt *xt, *next;
950 /* list should be empty here, it can be non-empty only in case there
951 * was an error that caused nft_xt expr to not be initialized fully
952 * and noone else requested the same expression later.
954 * In this case, the lists contain 0-refcount entries that still
955 * hold module reference.
957 list_for_each_entry_safe(xt, next, &nft_target_list, head) {
958 struct xt_target *target = xt->ops.data;
960 if (WARN_ON_ONCE(xt->refcnt))
962 module_put(target->me);
966 list_for_each_entry_safe(xt, next, &nft_match_list, head) {
967 struct xt_match *match = xt->ops.data;
969 if (WARN_ON_ONCE(xt->refcnt))
971 module_put(match->me);
974 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
975 nft_unregister_expr(&nft_target_type);
976 nft_unregister_expr(&nft_match_type);
979 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
981 module_init(nft_compat_module_init);
982 module_exit(nft_compat_module_exit);
984 MODULE_LICENSE("GPL");
985 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
986 MODULE_ALIAS_NFT_EXPR("match");
987 MODULE_ALIAS_NFT_EXPR("target");