1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
5 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/netlink.h>
12 #include <linux/netfilter.h>
13 #include <linux/netfilter/nfnetlink.h>
14 #include <linux/netfilter/nf_tables.h>
15 #include <linux/netfilter/nf_tables_compat.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter_ipv4/ip_tables.h>
18 #include <linux/netfilter_ipv6/ip6_tables.h>
19 #include <linux/netfilter_bridge/ebtables.h>
20 #include <linux/netfilter_arp/arp_tables.h>
21 #include <net/netfilter/nf_tables.h>
23 /* Used for matches where *info is larger than X byte */
24 #define NFT_MATCH_LARGE_THRESH 192
26 struct nft_xt_match_priv {
30 static int nft_compat_chain_validate_dependency(const struct nft_ctx *ctx,
31 const char *tablename)
33 enum nft_chain_types type = NFT_CHAIN_T_DEFAULT;
34 const struct nft_chain *chain = ctx->chain;
35 const struct nft_base_chain *basechain;
38 !nft_is_base_chain(chain))
41 basechain = nft_base_chain(chain);
42 if (strcmp(tablename, "nat") == 0) {
43 if (ctx->family != NFPROTO_BRIDGE)
44 type = NFT_CHAIN_T_NAT;
45 if (basechain->type->type != type)
56 struct arpt_entry arp;
60 nft_compat_set_par(struct xt_action_param *par,
61 const struct nft_pktinfo *pkt,
62 const void *xt, const void *xt_info)
64 par->state = pkt->state;
65 par->thoff = nft_thoff(pkt);
66 par->fragoff = pkt->fragoff;
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;
79 struct xt_action_param xt;
82 nft_compat_set_par(&xt, pkt, target, info);
84 ret = target->target(skb, &xt);
91 regs->verdict.code = NFT_CONTINUE;
94 regs->verdict.code = ret;
99 static void nft_target_eval_bridge(const struct nft_expr *expr,
100 struct nft_regs *regs,
101 const struct nft_pktinfo *pkt)
103 void *info = nft_expr_priv(expr);
104 struct xt_target *target = expr->ops->data;
105 struct sk_buff *skb = pkt->skb;
106 struct xt_action_param xt;
109 nft_compat_set_par(&xt, pkt, target, info);
111 ret = target->target(skb, &xt);
118 regs->verdict.code = NF_ACCEPT;
121 regs->verdict.code = NF_DROP;
124 regs->verdict.code = NFT_CONTINUE;
127 regs->verdict.code = NFT_RETURN;
130 regs->verdict.code = ret;
135 static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
136 [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING },
137 [NFTA_TARGET_REV] = { .type = NLA_U32 },
138 [NFTA_TARGET_INFO] = { .type = NLA_BINARY },
142 nft_target_set_tgchk_param(struct xt_tgchk_param *par,
143 const struct nft_ctx *ctx,
144 struct xt_target *target, void *info,
145 union nft_entry *entry, u16 proto, bool inv)
148 par->table = ctx->table->name;
149 switch (ctx->family) {
151 entry->e4.ip.proto = proto;
152 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
156 entry->e6.ipv6.flags |= IP6T_F_PROTO;
158 entry->e6.ipv6.proto = proto;
159 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
162 entry->ebt.ethproto = (__force __be16)proto;
163 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
168 par->entryinfo = entry;
169 par->target = target;
170 par->targinfo = info;
171 if (nft_is_base_chain(ctx->chain)) {
172 const struct nft_base_chain *basechain =
173 nft_base_chain(ctx->chain);
174 const struct nf_hook_ops *ops = &basechain->ops;
176 par->hook_mask = 1 << ops->hooknum;
180 par->family = ctx->family;
181 par->nft_compat = true;
184 static void target_compat_from_user(struct xt_target *t, void *in, void *out)
188 memcpy(out, in, t->targetsize);
189 pad = XT_ALIGN(t->targetsize) - t->targetsize;
191 memset(out + t->targetsize, 0, pad);
194 static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
195 [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 },
196 [NFTA_RULE_COMPAT_FLAGS] = { .type = NLA_U32 },
199 static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv)
201 struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
205 err = nla_parse_nested_deprecated(tb, NFTA_RULE_COMPAT_MAX, attr,
206 nft_rule_compat_policy, NULL);
210 if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
213 flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
214 if (flags & ~NFT_RULE_COMPAT_F_MASK)
216 if (flags & NFT_RULE_COMPAT_F_INV)
219 *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
223 static void nft_compat_wait_for_destructors(void)
225 /* xtables matches or targets can have side effects, e.g.
226 * creation/destruction of /proc files.
227 * The xt ->destroy functions are run asynchronously from
228 * work queue. If we have pending invocations we thus
229 * need to wait for those to finish.
231 nf_tables_trans_destroy_flush_work();
235 nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
236 const struct nlattr * const tb[])
238 void *info = nft_expr_priv(expr);
239 struct xt_target *target = expr->ops->data;
240 struct xt_tgchk_param par;
241 size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
244 union nft_entry e = {};
247 target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
249 if (ctx->nla[NFTA_RULE_COMPAT]) {
250 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
255 nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
257 nft_compat_wait_for_destructors();
259 ret = xt_check_target(&par, size, proto, inv);
263 /* The standard target cannot be used */
270 static void __nft_mt_tg_destroy(struct module *me, const struct nft_expr *expr)
277 nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
279 struct xt_target *target = expr->ops->data;
280 void *info = nft_expr_priv(expr);
281 struct module *me = target->me;
282 struct xt_tgdtor_param par;
287 par.family = ctx->family;
288 if (par.target->destroy != NULL)
289 par.target->destroy(&par);
291 __nft_mt_tg_destroy(me, expr);
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;
342 hook_mask = 1 << ops->hooknum;
343 if (target->hooks && !(hook_mask & target->hooks))
346 ret = nft_compat_chain_validate_dependency(ctx, target->table);
353 static void __nft_match_eval(const struct nft_expr *expr,
354 struct nft_regs *regs,
355 const struct nft_pktinfo *pkt,
358 struct xt_match *match = expr->ops->data;
359 struct sk_buff *skb = pkt->skb;
360 struct xt_action_param xt;
363 nft_compat_set_par(&xt, pkt, match, info);
365 ret = match->match(skb, &xt);
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->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;
439 par->hook_mask = 1 << ops->hooknum;
443 par->family = ctx->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]));
467 union nft_entry e = {};
470 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
472 if (ctx->nla[NFTA_RULE_COMPAT]) {
473 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
478 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
480 nft_compat_wait_for_destructors();
482 return xt_check_match(&par, size, proto, inv);
486 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
487 const struct nlattr * const tb[])
489 return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
493 nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
494 const struct nlattr * const tb[])
496 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
497 struct xt_match *m = expr->ops->data;
500 priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL);
504 ret = __nft_match_init(ctx, expr, tb, priv->info);
511 __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
514 struct xt_match *match = expr->ops->data;
515 struct module *me = match->me;
516 struct xt_mtdtor_param par;
520 par.matchinfo = info;
521 par.family = ctx->family;
522 if (par.match->destroy != NULL)
523 par.match->destroy(&par);
525 __nft_mt_tg_destroy(me, expr);
529 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
531 __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
535 nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
537 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
539 __nft_match_destroy(ctx, expr, priv->info);
543 static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
546 struct xt_match *match = expr->ops->data;
548 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
549 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
550 nft_extension_dump_info(skb, NFTA_MATCH_INFO, info,
551 match->matchsize, match->usersize))
552 goto nla_put_failure;
560 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
562 return __nft_match_dump(skb, expr, nft_expr_priv(expr));
565 static int nft_match_large_dump(struct sk_buff *skb, const struct nft_expr *e)
567 struct nft_xt_match_priv *priv = nft_expr_priv(e);
569 return __nft_match_dump(skb, e, priv->info);
572 static int nft_match_validate(const struct nft_ctx *ctx,
573 const struct nft_expr *expr,
574 const struct nft_data **data)
576 struct xt_match *match = expr->ops->data;
577 unsigned int hook_mask = 0;
580 if (nft_is_base_chain(ctx->chain)) {
581 const struct nft_base_chain *basechain =
582 nft_base_chain(ctx->chain);
583 const struct nf_hook_ops *ops = &basechain->ops;
585 hook_mask = 1 << ops->hooknum;
586 if (match->hooks && !(hook_mask & match->hooks))
589 ret = nft_compat_chain_validate_dependency(ctx, match->table);
597 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
598 int event, u16 family, const char *name,
601 struct nlmsghdr *nlh;
602 unsigned int flags = portid ? NLM_F_MULTI : 0;
604 event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
605 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
610 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
611 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
612 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
613 goto nla_put_failure;
620 nlmsg_cancel(skb, nlh);
624 static int nfnl_compat_get_rcu(struct sk_buff *skb,
625 const struct nfnl_info *info,
626 const struct nlattr * const tb[])
628 u8 family = info->nfmsg->nfgen_family;
629 const char *name, *fmt;
630 struct sk_buff *skb2;
634 if (tb[NFTA_COMPAT_NAME] == NULL ||
635 tb[NFTA_COMPAT_REV] == NULL ||
636 tb[NFTA_COMPAT_TYPE] == NULL)
639 name = nla_data(tb[NFTA_COMPAT_NAME]);
640 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
641 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
657 pr_err("nft_compat: unsupported protocol %d\n", family);
661 if (!try_module_get(THIS_MODULE))
665 try_then_request_module(xt_find_revision(family, name, rev, target, &ret),
670 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
676 /* include the best revision for this extension in the message */
677 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
678 info->nlh->nlmsg_seq,
679 NFNL_MSG_TYPE(info->nlh->nlmsg_type),
681 family, name, ret, target) <= 0) {
686 ret = nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
689 module_put(THIS_MODULE);
694 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
695 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
696 .len = NFT_COMPAT_NAME_MAX-1 },
697 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
698 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
701 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
702 [NFNL_MSG_COMPAT_GET] = {
703 .call = nfnl_compat_get_rcu,
705 .attr_count = NFTA_COMPAT_MAX,
706 .policy = nfnl_compat_policy_get
710 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
711 .name = "nft-compat",
712 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
713 .cb_count = NFNL_MSG_COMPAT_MAX,
714 .cb = nfnl_nft_compat_cb,
717 static struct nft_expr_type nft_match_type;
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_expr_ops *ops;
724 struct xt_match *match;
725 unsigned int matchsize;
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->family;
739 match = xt_request_find_match(family, mt_name, rev);
741 return ERR_PTR(-ENOENT);
743 if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
748 ops = kzalloc(sizeof(struct nft_expr_ops), GFP_KERNEL);
754 ops->type = &nft_match_type;
755 ops->eval = nft_match_eval;
756 ops->init = nft_match_init;
757 ops->destroy = nft_match_destroy;
758 ops->dump = nft_match_dump;
759 ops->validate = nft_match_validate;
762 matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
763 if (matchsize > NFT_MATCH_LARGE_THRESH) {
764 matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
766 ops->eval = nft_match_large_eval;
767 ops->init = nft_match_large_init;
768 ops->destroy = nft_match_large_destroy;
769 ops->dump = nft_match_large_dump;
772 ops->size = matchsize;
776 module_put(match->me);
780 static void nft_match_release_ops(const struct nft_expr_ops *ops)
782 struct xt_match *match = ops->data;
784 module_put(match->me);
788 static struct nft_expr_type nft_match_type __read_mostly = {
790 .select_ops = nft_match_select_ops,
791 .release_ops = nft_match_release_ops,
792 .policy = nft_match_policy,
793 .maxattr = NFTA_MATCH_MAX,
794 .owner = THIS_MODULE,
797 static struct nft_expr_type nft_target_type;
799 static const struct nft_expr_ops *
800 nft_target_select_ops(const struct nft_ctx *ctx,
801 const struct nlattr * const tb[])
803 struct nft_expr_ops *ops;
804 struct xt_target *target;
809 if (tb[NFTA_TARGET_NAME] == NULL ||
810 tb[NFTA_TARGET_REV] == NULL ||
811 tb[NFTA_TARGET_INFO] == NULL)
812 return ERR_PTR(-EINVAL);
814 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
815 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
816 family = ctx->family;
818 if (strcmp(tg_name, XT_ERROR_TARGET) == 0 ||
819 strcmp(tg_name, XT_STANDARD_TARGET) == 0 ||
820 strcmp(tg_name, "standard") == 0)
821 return ERR_PTR(-EINVAL);
823 target = xt_request_find_target(family, tg_name, rev);
825 return ERR_PTR(-ENOENT);
827 if (!target->target) {
832 if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
837 ops = kzalloc(sizeof(struct nft_expr_ops), GFP_KERNEL);
843 ops->type = &nft_target_type;
844 ops->size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
845 ops->init = nft_target_init;
846 ops->destroy = nft_target_destroy;
847 ops->dump = nft_target_dump;
848 ops->validate = nft_target_validate;
851 if (family == NFPROTO_BRIDGE)
852 ops->eval = nft_target_eval_bridge;
854 ops->eval = nft_target_eval_xt;
858 module_put(target->me);
862 static void nft_target_release_ops(const struct nft_expr_ops *ops)
864 struct xt_target *target = ops->data;
866 module_put(target->me);
870 static struct nft_expr_type nft_target_type __read_mostly = {
872 .select_ops = nft_target_select_ops,
873 .release_ops = nft_target_release_ops,
874 .policy = nft_target_policy,
875 .maxattr = NFTA_TARGET_MAX,
876 .owner = THIS_MODULE,
879 static int __init nft_compat_module_init(void)
883 ret = nft_register_expr(&nft_match_type);
887 ret = nft_register_expr(&nft_target_type);
891 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
893 pr_err("nft_compat: cannot register with nfnetlink.\n");
899 nft_unregister_expr(&nft_target_type);
901 nft_unregister_expr(&nft_match_type);
905 static void __exit nft_compat_module_exit(void)
907 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
908 nft_unregister_expr(&nft_target_type);
909 nft_unregister_expr(&nft_match_type);
912 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
914 module_init(nft_compat_module_init);
915 module_exit(nft_compat_module_exit);
917 MODULE_LICENSE("GPL");
918 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
919 MODULE_ALIAS_NFT_EXPR("match");
920 MODULE_ALIAS_NFT_EXPR("target");
921 MODULE_DESCRIPTION("x_tables over nftables support");