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;
32 static void nft_xt_put(struct nft_xt *xt)
34 if (--xt->refcnt == 0) {
40 static int nft_compat_chain_validate_dependency(const char *tablename,
41 const struct nft_chain *chain)
43 const struct nft_base_chain *basechain;
46 !nft_is_base_chain(chain))
49 basechain = nft_base_chain(chain);
50 if (strcmp(tablename, "nat") == 0 &&
51 basechain->type->type != NFT_CHAIN_T_NAT)
61 struct arpt_entry arp;
65 nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
68 par->targinfo = xt_info;
72 static void nft_target_eval_xt(const struct nft_expr *expr,
73 struct nft_regs *regs,
74 const struct nft_pktinfo *pkt)
76 void *info = nft_expr_priv(expr);
77 struct xt_target *target = expr->ops->data;
78 struct sk_buff *skb = pkt->skb;
81 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
83 ret = target->target(skb, &pkt->xt);
90 regs->verdict.code = NFT_CONTINUE;
93 regs->verdict.code = ret;
98 static void nft_target_eval_bridge(const struct nft_expr *expr,
99 struct nft_regs *regs,
100 const struct nft_pktinfo *pkt)
102 void *info = nft_expr_priv(expr);
103 struct xt_target *target = expr->ops->data;
104 struct sk_buff *skb = pkt->skb;
107 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
109 ret = target->target(skb, &pkt->xt);
116 regs->verdict.code = NF_ACCEPT;
119 regs->verdict.code = NF_DROP;
122 regs->verdict.code = NFT_CONTINUE;
125 regs->verdict.code = NFT_RETURN;
128 regs->verdict.code = ret;
133 static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
134 [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING },
135 [NFTA_TARGET_REV] = { .type = NLA_U32 },
136 [NFTA_TARGET_INFO] = { .type = NLA_BINARY },
140 nft_target_set_tgchk_param(struct xt_tgchk_param *par,
141 const struct nft_ctx *ctx,
142 struct xt_target *target, void *info,
143 union nft_entry *entry, u16 proto, bool inv)
146 par->table = ctx->table->name;
147 switch (ctx->afi->family) {
149 entry->e4.ip.proto = proto;
150 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
154 entry->e6.ipv6.flags |= IP6T_F_PROTO;
156 entry->e6.ipv6.proto = proto;
157 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
160 entry->ebt.ethproto = (__force __be16)proto;
161 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
166 par->entryinfo = entry;
167 par->target = target;
168 par->targinfo = info;
169 if (nft_is_base_chain(ctx->chain)) {
170 const struct nft_base_chain *basechain =
171 nft_base_chain(ctx->chain);
172 const struct nf_hook_ops *ops = &basechain->ops[0];
174 par->hook_mask = 1 << ops->hooknum;
178 par->family = ctx->afi->family;
179 par->nft_compat = true;
182 static void target_compat_from_user(struct xt_target *t, void *in, void *out)
186 memcpy(out, in, t->targetsize);
187 pad = XT_ALIGN(t->targetsize) - t->targetsize;
189 memset(out + t->targetsize, 0, pad);
192 static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
193 [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 },
194 [NFTA_RULE_COMPAT_FLAGS] = { .type = NLA_U32 },
197 static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv)
199 struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
203 err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr,
204 nft_rule_compat_policy, NULL);
208 if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
211 flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
212 if (flags & ~NFT_RULE_COMPAT_F_MASK)
214 if (flags & NFT_RULE_COMPAT_F_INV)
217 *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
222 nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
223 const struct nlattr * const tb[])
225 void *info = nft_expr_priv(expr);
226 struct xt_target *target = expr->ops->data;
227 struct xt_tgchk_param par;
228 size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
231 union nft_entry e = {};
234 target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
236 if (ctx->nla[NFTA_RULE_COMPAT]) {
237 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
242 nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
244 ret = xt_check_target(&par, size, proto, inv);
248 /* The standard target cannot be used */
249 if (target->target == NULL) {
256 module_put(target->me);
261 nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
263 struct xt_target *target = expr->ops->data;
264 void *info = nft_expr_priv(expr);
265 struct xt_tgdtor_param par;
270 par.family = ctx->afi->family;
271 if (par.target->destroy != NULL)
272 par.target->destroy(&par);
274 nft_xt_put(container_of(expr->ops, struct nft_xt, ops));
275 module_put(target->me);
278 static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
280 const struct xt_target *target = expr->ops->data;
281 void *info = nft_expr_priv(expr);
283 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
284 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
285 nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(target->targetsize), info))
286 goto nla_put_failure;
294 static int nft_target_validate(const struct nft_ctx *ctx,
295 const struct nft_expr *expr,
296 const struct nft_data **data)
298 struct xt_target *target = expr->ops->data;
299 unsigned int hook_mask = 0;
302 if (nft_is_base_chain(ctx->chain)) {
303 const struct nft_base_chain *basechain =
304 nft_base_chain(ctx->chain);
305 const struct nf_hook_ops *ops = &basechain->ops[0];
307 hook_mask = 1 << ops->hooknum;
308 if (!(hook_mask & target->hooks))
311 ret = nft_compat_chain_validate_dependency(target->table,
319 static void nft_match_eval(const struct nft_expr *expr,
320 struct nft_regs *regs,
321 const struct nft_pktinfo *pkt)
323 void *info = nft_expr_priv(expr);
324 struct xt_match *match = expr->ops->data;
325 struct sk_buff *skb = pkt->skb;
328 nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
330 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
332 if (pkt->xt.hotdrop) {
333 regs->verdict.code = NF_DROP;
337 switch (ret ? 1 : 0) {
339 regs->verdict.code = NFT_CONTINUE;
342 regs->verdict.code = NFT_BREAK;
347 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
348 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
349 [NFTA_MATCH_REV] = { .type = NLA_U32 },
350 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
353 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
355 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
356 struct xt_match *match, void *info,
357 union nft_entry *entry, u16 proto, bool inv)
360 par->table = ctx->table->name;
361 switch (ctx->afi->family) {
363 entry->e4.ip.proto = proto;
364 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
368 entry->e6.ipv6.flags |= IP6T_F_PROTO;
370 entry->e6.ipv6.proto = proto;
371 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
374 entry->ebt.ethproto = (__force __be16)proto;
375 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
380 par->entryinfo = entry;
382 par->matchinfo = info;
383 if (nft_is_base_chain(ctx->chain)) {
384 const struct nft_base_chain *basechain =
385 nft_base_chain(ctx->chain);
386 const struct nf_hook_ops *ops = &basechain->ops[0];
388 par->hook_mask = 1 << ops->hooknum;
392 par->family = ctx->afi->family;
393 par->nft_compat = true;
396 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
400 memcpy(out, in, m->matchsize);
401 pad = XT_ALIGN(m->matchsize) - m->matchsize;
403 memset(out + m->matchsize, 0, pad);
407 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
408 const struct nlattr * const tb[])
410 void *info = nft_expr_priv(expr);
411 struct xt_match *match = expr->ops->data;
412 struct xt_mtchk_param par;
413 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
416 union nft_entry e = {};
419 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
421 if (ctx->nla[NFTA_RULE_COMPAT]) {
422 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
427 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
429 ret = xt_check_match(&par, size, proto, inv);
435 module_put(match->me);
440 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
442 struct xt_match *match = expr->ops->data;
443 void *info = nft_expr_priv(expr);
444 struct xt_mtdtor_param par;
448 par.matchinfo = info;
449 par.family = ctx->afi->family;
450 if (par.match->destroy != NULL)
451 par.match->destroy(&par);
453 nft_xt_put(container_of(expr->ops, struct nft_xt, ops));
454 module_put(match->me);
457 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
459 void *info = nft_expr_priv(expr);
460 struct xt_match *match = expr->ops->data;
462 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
463 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
464 nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(match->matchsize), info))
465 goto nla_put_failure;
473 static int nft_match_validate(const struct nft_ctx *ctx,
474 const struct nft_expr *expr,
475 const struct nft_data **data)
477 struct xt_match *match = expr->ops->data;
478 unsigned int hook_mask = 0;
481 if (nft_is_base_chain(ctx->chain)) {
482 const struct nft_base_chain *basechain =
483 nft_base_chain(ctx->chain);
484 const struct nf_hook_ops *ops = &basechain->ops[0];
486 hook_mask = 1 << ops->hooknum;
487 if (!(hook_mask & match->hooks))
490 ret = nft_compat_chain_validate_dependency(match->table,
499 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
500 int event, u16 family, const char *name,
503 struct nlmsghdr *nlh;
504 struct nfgenmsg *nfmsg;
505 unsigned int flags = portid ? NLM_F_MULTI : 0;
507 event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
508 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
512 nfmsg = nlmsg_data(nlh);
513 nfmsg->nfgen_family = family;
514 nfmsg->version = NFNETLINK_V0;
517 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
518 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
519 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
520 goto nla_put_failure;
527 nlmsg_cancel(skb, nlh);
531 static int nfnl_compat_get(struct net *net, struct sock *nfnl,
532 struct sk_buff *skb, const struct nlmsghdr *nlh,
533 const struct nlattr * const tb[])
536 struct nfgenmsg *nfmsg;
540 struct sk_buff *skb2;
542 if (tb[NFTA_COMPAT_NAME] == NULL ||
543 tb[NFTA_COMPAT_REV] == NULL ||
544 tb[NFTA_COMPAT_TYPE] == NULL)
547 name = nla_data(tb[NFTA_COMPAT_NAME]);
548 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
549 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
551 nfmsg = nlmsg_data(nlh);
553 switch(nfmsg->nfgen_family) {
567 pr_err("nft_compat: unsupported protocol %d\n",
568 nfmsg->nfgen_family);
572 try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
579 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
583 /* include the best revision for this extension in the message */
584 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
586 NFNL_MSG_TYPE(nlh->nlmsg_type),
589 name, ret, target) <= 0) {
594 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
599 return ret == -EAGAIN ? -ENOBUFS : ret;
602 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
603 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
604 .len = NFT_COMPAT_NAME_MAX-1 },
605 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
606 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
609 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
610 [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get,
611 .attr_count = NFTA_COMPAT_MAX,
612 .policy = nfnl_compat_policy_get },
615 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
616 .name = "nft-compat",
617 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
618 .cb_count = NFNL_MSG_COMPAT_MAX,
619 .cb = nfnl_nft_compat_cb,
622 static LIST_HEAD(nft_match_list);
624 static struct nft_expr_type nft_match_type;
626 static bool nft_match_cmp(const struct xt_match *match,
627 const char *name, u32 rev, u32 family)
629 return strcmp(match->name, name) == 0 && match->revision == rev &&
630 (match->family == NFPROTO_UNSPEC || match->family == family);
633 static const struct nft_expr_ops *
634 nft_match_select_ops(const struct nft_ctx *ctx,
635 const struct nlattr * const tb[])
637 struct nft_xt *nft_match;
638 struct xt_match *match;
643 if (tb[NFTA_MATCH_NAME] == NULL ||
644 tb[NFTA_MATCH_REV] == NULL ||
645 tb[NFTA_MATCH_INFO] == NULL)
646 return ERR_PTR(-EINVAL);
648 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
649 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
650 family = ctx->afi->family;
652 /* Re-use the existing match if it's already loaded. */
653 list_for_each_entry(nft_match, &nft_match_list, head) {
654 struct xt_match *match = nft_match->ops.data;
656 if (nft_match_cmp(match, mt_name, rev, family)) {
657 if (!try_module_get(match->me))
658 return ERR_PTR(-ENOENT);
661 return &nft_match->ops;
665 match = xt_request_find_match(family, mt_name, rev);
667 return ERR_PTR(-ENOENT);
669 if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
674 /* This is the first time we use this match, allocate operations */
675 nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
676 if (nft_match == NULL) {
681 nft_match->refcnt = 1;
682 nft_match->ops.type = &nft_match_type;
683 nft_match->ops.size = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
684 nft_match->ops.eval = nft_match_eval;
685 nft_match->ops.init = nft_match_init;
686 nft_match->ops.destroy = nft_match_destroy;
687 nft_match->ops.dump = nft_match_dump;
688 nft_match->ops.validate = nft_match_validate;
689 nft_match->ops.data = match;
691 list_add(&nft_match->head, &nft_match_list);
693 return &nft_match->ops;
695 module_put(match->me);
699 static struct nft_expr_type nft_match_type __read_mostly = {
701 .select_ops = nft_match_select_ops,
702 .policy = nft_match_policy,
703 .maxattr = NFTA_MATCH_MAX,
704 .owner = THIS_MODULE,
707 static LIST_HEAD(nft_target_list);
709 static struct nft_expr_type nft_target_type;
711 static bool nft_target_cmp(const struct xt_target *tg,
712 const char *name, u32 rev, u32 family)
714 return strcmp(tg->name, name) == 0 && tg->revision == rev &&
715 (tg->family == NFPROTO_UNSPEC || tg->family == family);
718 static const struct nft_expr_ops *
719 nft_target_select_ops(const struct nft_ctx *ctx,
720 const struct nlattr * const tb[])
722 struct nft_xt *nft_target;
723 struct xt_target *target;
728 if (tb[NFTA_TARGET_NAME] == NULL ||
729 tb[NFTA_TARGET_REV] == NULL ||
730 tb[NFTA_TARGET_INFO] == NULL)
731 return ERR_PTR(-EINVAL);
733 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
734 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
735 family = ctx->afi->family;
737 /* Re-use the existing target if it's already loaded. */
738 list_for_each_entry(nft_target, &nft_target_list, head) {
739 struct xt_target *target = nft_target->ops.data;
741 if (nft_target_cmp(target, tg_name, rev, family)) {
742 if (!try_module_get(target->me))
743 return ERR_PTR(-ENOENT);
745 nft_target->refcnt++;
746 return &nft_target->ops;
750 target = xt_request_find_target(family, tg_name, rev);
752 return ERR_PTR(-ENOENT);
754 if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
759 /* This is the first time we use this target, allocate operations */
760 nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
761 if (nft_target == NULL) {
766 nft_target->refcnt = 1;
767 nft_target->ops.type = &nft_target_type;
768 nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
769 nft_target->ops.init = nft_target_init;
770 nft_target->ops.destroy = nft_target_destroy;
771 nft_target->ops.dump = nft_target_dump;
772 nft_target->ops.validate = nft_target_validate;
773 nft_target->ops.data = target;
775 if (family == NFPROTO_BRIDGE)
776 nft_target->ops.eval = nft_target_eval_bridge;
778 nft_target->ops.eval = nft_target_eval_xt;
780 list_add(&nft_target->head, &nft_target_list);
782 return &nft_target->ops;
784 module_put(target->me);
788 static struct nft_expr_type nft_target_type __read_mostly = {
790 .select_ops = nft_target_select_ops,
791 .policy = nft_target_policy,
792 .maxattr = NFTA_TARGET_MAX,
793 .owner = THIS_MODULE,
796 static int __init nft_compat_module_init(void)
800 ret = nft_register_expr(&nft_match_type);
804 ret = nft_register_expr(&nft_target_type);
808 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
810 pr_err("nft_compat: cannot register with nfnetlink.\n");
814 pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
819 nft_unregister_expr(&nft_target_type);
821 nft_unregister_expr(&nft_match_type);
825 static void __exit nft_compat_module_exit(void)
827 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
828 nft_unregister_expr(&nft_target_type);
829 nft_unregister_expr(&nft_match_type);
832 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
834 module_init(nft_compat_module_init);
835 module_exit(nft_compat_module_exit);
837 MODULE_LICENSE("GPL");
838 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
839 MODULE_ALIAS_NFT_EXPR("match");
840 MODULE_ALIAS_NFT_EXPR("target");