perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / net / netfilter / nft_compat.c
blob768292eac2a46afe84df3b8a949a70bf77baf478
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->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;
191 par->hook_mask = 1 << ops->hooknum;
192 } else {
193 par->hook_mask = 0;
195 par->family = ctx->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 xt_tgdtor_param par;
282 par.net = ctx->net;
283 par.target = target;
284 par.targinfo = info;
285 par.family = ctx->family;
286 if (par.target->destroy != NULL)
287 par.target->destroy(&par);
289 if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
290 module_put(target->me);
293 static int nft_extension_dump_info(struct sk_buff *skb, int attr,
294 const void *info,
295 unsigned int size, unsigned int user_size)
297 unsigned int info_size, aligned_size = XT_ALIGN(size);
298 struct nlattr *nla;
300 nla = nla_reserve(skb, attr, aligned_size);
301 if (!nla)
302 return -1;
304 info_size = user_size ? : size;
305 memcpy(nla_data(nla), info, info_size);
306 memset(nla_data(nla) + info_size, 0, aligned_size - info_size);
308 return 0;
311 static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
313 const struct xt_target *target = expr->ops->data;
314 void *info = nft_expr_priv(expr);
316 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
317 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
318 nft_extension_dump_info(skb, NFTA_TARGET_INFO, info,
319 target->targetsize, target->usersize))
320 goto nla_put_failure;
322 return 0;
324 nla_put_failure:
325 return -1;
328 static int nft_target_validate(const struct nft_ctx *ctx,
329 const struct nft_expr *expr,
330 const struct nft_data **data)
332 struct xt_target *target = expr->ops->data;
333 unsigned int hook_mask = 0;
334 int ret;
336 if (nft_is_base_chain(ctx->chain)) {
337 const struct nft_base_chain *basechain =
338 nft_base_chain(ctx->chain);
339 const struct nf_hook_ops *ops = &basechain->ops;
341 hook_mask = 1 << ops->hooknum;
342 if (target->hooks && !(hook_mask & target->hooks))
343 return -EINVAL;
345 ret = nft_compat_chain_validate_dependency(target->table,
346 ctx->chain);
347 if (ret < 0)
348 return ret;
350 return 0;
353 static void __nft_match_eval(const struct nft_expr *expr,
354 struct nft_regs *regs,
355 const struct nft_pktinfo *pkt,
356 void *info)
358 struct xt_match *match = expr->ops->data;
359 struct sk_buff *skb = pkt->skb;
360 bool ret;
362 nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
364 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
366 if (pkt->xt.hotdrop) {
367 regs->verdict.code = NF_DROP;
368 return;
371 switch (ret ? 1 : 0) {
372 case 1:
373 regs->verdict.code = NFT_CONTINUE;
374 break;
375 case 0:
376 regs->verdict.code = NFT_BREAK;
377 break;
381 static void nft_match_large_eval(const struct nft_expr *expr,
382 struct nft_regs *regs,
383 const struct nft_pktinfo *pkt)
385 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
387 __nft_match_eval(expr, regs, pkt, priv->info);
390 static void nft_match_eval(const struct nft_expr *expr,
391 struct nft_regs *regs,
392 const struct nft_pktinfo *pkt)
394 __nft_match_eval(expr, regs, pkt, nft_expr_priv(expr));
397 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
398 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
399 [NFTA_MATCH_REV] = { .type = NLA_U32 },
400 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
403 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
404 static void
405 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
406 struct xt_match *match, void *info,
407 union nft_entry *entry, u16 proto, bool inv)
409 par->net = ctx->net;
410 par->table = ctx->table->name;
411 switch (ctx->family) {
412 case AF_INET:
413 entry->e4.ip.proto = proto;
414 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
415 break;
416 case AF_INET6:
417 if (proto)
418 entry->e6.ipv6.flags |= IP6T_F_PROTO;
420 entry->e6.ipv6.proto = proto;
421 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
422 break;
423 case NFPROTO_BRIDGE:
424 entry->ebt.ethproto = (__force __be16)proto;
425 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
426 break;
427 case NFPROTO_ARP:
428 break;
430 par->entryinfo = entry;
431 par->match = match;
432 par->matchinfo = info;
433 if (nft_is_base_chain(ctx->chain)) {
434 const struct nft_base_chain *basechain =
435 nft_base_chain(ctx->chain);
436 const struct nf_hook_ops *ops = &basechain->ops;
438 par->hook_mask = 1 << ops->hooknum;
439 } else {
440 par->hook_mask = 0;
442 par->family = ctx->family;
443 par->nft_compat = true;
446 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
448 int pad;
450 memcpy(out, in, m->matchsize);
451 pad = XT_ALIGN(m->matchsize) - m->matchsize;
452 if (pad > 0)
453 memset(out + m->matchsize, 0, pad);
456 static int
457 __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
458 const struct nlattr * const tb[],
459 void *info)
461 struct xt_match *match = expr->ops->data;
462 struct xt_mtchk_param par;
463 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
464 struct nft_xt *nft_xt;
465 u16 proto = 0;
466 bool inv = false;
467 union nft_entry e = {};
468 int ret;
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);
474 if (ret < 0)
475 return ret;
478 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
480 ret = xt_check_match(&par, size, proto, inv);
481 if (ret < 0)
482 return ret;
484 nft_xt = container_of(expr->ops, struct nft_xt, ops);
485 nft_xt->refcnt++;
486 return 0;
489 static int
490 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
491 const struct nlattr * const tb[])
493 return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
496 static int
497 nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
498 const struct nlattr * const tb[])
500 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
501 struct xt_match *m = expr->ops->data;
502 int ret;
504 priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL);
505 if (!priv->info)
506 return -ENOMEM;
508 ret = __nft_match_init(ctx, expr, tb, priv->info);
509 if (ret)
510 kfree(priv->info);
511 return ret;
514 static void
515 __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
516 void *info)
518 struct xt_match *match = expr->ops->data;
519 struct xt_mtdtor_param par;
521 par.net = ctx->net;
522 par.match = match;
523 par.matchinfo = info;
524 par.family = ctx->family;
525 if (par.match->destroy != NULL)
526 par.match->destroy(&par);
528 if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
529 module_put(match->me);
532 static void
533 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
535 __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
538 static void
539 nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
541 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
543 __nft_match_destroy(ctx, expr, priv->info);
544 kfree(priv->info);
547 static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
548 void *info)
550 struct xt_match *match = expr->ops->data;
552 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
553 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
554 nft_extension_dump_info(skb, NFTA_MATCH_INFO, info,
555 match->matchsize, match->usersize))
556 goto nla_put_failure;
558 return 0;
560 nla_put_failure:
561 return -1;
564 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
566 return __nft_match_dump(skb, expr, nft_expr_priv(expr));
569 static int nft_match_large_dump(struct sk_buff *skb, const struct nft_expr *e)
571 struct nft_xt_match_priv *priv = nft_expr_priv(e);
573 return __nft_match_dump(skb, e, priv->info);
576 static int nft_match_validate(const struct nft_ctx *ctx,
577 const struct nft_expr *expr,
578 const struct nft_data **data)
580 struct xt_match *match = expr->ops->data;
581 unsigned int hook_mask = 0;
582 int ret;
584 if (nft_is_base_chain(ctx->chain)) {
585 const struct nft_base_chain *basechain =
586 nft_base_chain(ctx->chain);
587 const struct nf_hook_ops *ops = &basechain->ops;
589 hook_mask = 1 << ops->hooknum;
590 if (match->hooks && !(hook_mask & match->hooks))
591 return -EINVAL;
593 ret = nft_compat_chain_validate_dependency(match->table,
594 ctx->chain);
595 if (ret < 0)
596 return ret;
598 return 0;
601 static int
602 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
603 int event, u16 family, const char *name,
604 int rev, int target)
606 struct nlmsghdr *nlh;
607 struct nfgenmsg *nfmsg;
608 unsigned int flags = portid ? NLM_F_MULTI : 0;
610 event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
611 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
612 if (nlh == NULL)
613 goto nlmsg_failure;
615 nfmsg = nlmsg_data(nlh);
616 nfmsg->nfgen_family = family;
617 nfmsg->version = NFNETLINK_V0;
618 nfmsg->res_id = 0;
620 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
621 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
622 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
623 goto nla_put_failure;
625 nlmsg_end(skb, nlh);
626 return skb->len;
628 nlmsg_failure:
629 nla_put_failure:
630 nlmsg_cancel(skb, nlh);
631 return -1;
634 static int nfnl_compat_get_rcu(struct net *net, struct sock *nfnl,
635 struct sk_buff *skb, const struct nlmsghdr *nlh,
636 const struct nlattr * const tb[],
637 struct netlink_ext_ack *extack)
639 int ret = 0, target;
640 struct nfgenmsg *nfmsg;
641 const char *fmt;
642 const char *name;
643 u32 rev;
644 struct sk_buff *skb2;
646 if (tb[NFTA_COMPAT_NAME] == NULL ||
647 tb[NFTA_COMPAT_REV] == NULL ||
648 tb[NFTA_COMPAT_TYPE] == NULL)
649 return -EINVAL;
651 name = nla_data(tb[NFTA_COMPAT_NAME]);
652 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
653 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
655 nfmsg = nlmsg_data(nlh);
657 switch(nfmsg->nfgen_family) {
658 case AF_INET:
659 fmt = "ipt_%s";
660 break;
661 case AF_INET6:
662 fmt = "ip6t_%s";
663 break;
664 case NFPROTO_BRIDGE:
665 fmt = "ebt_%s";
666 break;
667 case NFPROTO_ARP:
668 fmt = "arpt_%s";
669 break;
670 default:
671 pr_err("nft_compat: unsupported protocol %d\n",
672 nfmsg->nfgen_family);
673 return -EINVAL;
676 if (!try_module_get(THIS_MODULE))
677 return -EINVAL;
679 rcu_read_unlock();
680 try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
681 rev, target, &ret),
682 fmt, name);
683 if (ret < 0)
684 goto out_put;
686 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
687 if (skb2 == NULL) {
688 ret = -ENOMEM;
689 goto out_put;
692 /* include the best revision for this extension in the message */
693 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
694 nlh->nlmsg_seq,
695 NFNL_MSG_TYPE(nlh->nlmsg_type),
696 NFNL_MSG_COMPAT_GET,
697 nfmsg->nfgen_family,
698 name, ret, target) <= 0) {
699 kfree_skb(skb2);
700 goto out_put;
703 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
704 MSG_DONTWAIT);
705 if (ret > 0)
706 ret = 0;
707 out_put:
708 rcu_read_lock();
709 module_put(THIS_MODULE);
710 return ret == -EAGAIN ? -ENOBUFS : ret;
713 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
714 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
715 .len = NFT_COMPAT_NAME_MAX-1 },
716 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
717 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
720 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
721 [NFNL_MSG_COMPAT_GET] = { .call_rcu = nfnl_compat_get_rcu,
722 .attr_count = NFTA_COMPAT_MAX,
723 .policy = nfnl_compat_policy_get },
726 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
727 .name = "nft-compat",
728 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
729 .cb_count = NFNL_MSG_COMPAT_MAX,
730 .cb = nfnl_nft_compat_cb,
733 static LIST_HEAD(nft_match_list);
735 static struct nft_expr_type nft_match_type;
737 static bool nft_match_cmp(const struct xt_match *match,
738 const char *name, u32 rev, u32 family)
740 return strcmp(match->name, name) == 0 && match->revision == rev &&
741 (match->family == NFPROTO_UNSPEC || match->family == family);
744 static const struct nft_expr_ops *
745 nft_match_select_ops(const struct nft_ctx *ctx,
746 const struct nlattr * const tb[])
748 struct nft_xt *nft_match;
749 struct xt_match *match;
750 unsigned int matchsize;
751 char *mt_name;
752 u32 rev, family;
753 int err;
755 if (tb[NFTA_MATCH_NAME] == NULL ||
756 tb[NFTA_MATCH_REV] == NULL ||
757 tb[NFTA_MATCH_INFO] == NULL)
758 return ERR_PTR(-EINVAL);
760 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
761 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
762 family = ctx->family;
764 /* Re-use the existing match if it's already loaded. */
765 list_for_each_entry(nft_match, &nft_match_list, head) {
766 struct xt_match *match = nft_match->ops.data;
768 if (nft_match_cmp(match, mt_name, rev, family))
769 return &nft_match->ops;
772 match = xt_request_find_match(family, mt_name, rev);
773 if (IS_ERR(match))
774 return ERR_PTR(-ENOENT);
776 if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
777 err = -EINVAL;
778 goto err;
781 /* This is the first time we use this match, allocate operations */
782 nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
783 if (nft_match == NULL) {
784 err = -ENOMEM;
785 goto err;
788 nft_match->refcnt = 0;
789 nft_match->ops.type = &nft_match_type;
790 nft_match->ops.eval = nft_match_eval;
791 nft_match->ops.init = nft_match_init;
792 nft_match->ops.destroy = nft_match_destroy;
793 nft_match->ops.dump = nft_match_dump;
794 nft_match->ops.validate = nft_match_validate;
795 nft_match->ops.data = match;
797 matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
798 if (matchsize > NFT_MATCH_LARGE_THRESH) {
799 matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
801 nft_match->ops.eval = nft_match_large_eval;
802 nft_match->ops.init = nft_match_large_init;
803 nft_match->ops.destroy = nft_match_large_destroy;
804 nft_match->ops.dump = nft_match_large_dump;
807 nft_match->ops.size = matchsize;
809 list_add(&nft_match->head, &nft_match_list);
811 return &nft_match->ops;
812 err:
813 module_put(match->me);
814 return ERR_PTR(err);
817 static struct nft_expr_type nft_match_type __read_mostly = {
818 .name = "match",
819 .select_ops = nft_match_select_ops,
820 .policy = nft_match_policy,
821 .maxattr = NFTA_MATCH_MAX,
822 .owner = THIS_MODULE,
825 static LIST_HEAD(nft_target_list);
827 static struct nft_expr_type nft_target_type;
829 static bool nft_target_cmp(const struct xt_target *tg,
830 const char *name, u32 rev, u32 family)
832 return strcmp(tg->name, name) == 0 && tg->revision == rev &&
833 (tg->family == NFPROTO_UNSPEC || tg->family == family);
836 static const struct nft_expr_ops *
837 nft_target_select_ops(const struct nft_ctx *ctx,
838 const struct nlattr * const tb[])
840 struct nft_xt *nft_target;
841 struct xt_target *target;
842 char *tg_name;
843 u32 rev, family;
844 int err;
846 if (tb[NFTA_TARGET_NAME] == NULL ||
847 tb[NFTA_TARGET_REV] == NULL ||
848 tb[NFTA_TARGET_INFO] == NULL)
849 return ERR_PTR(-EINVAL);
851 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
852 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
853 family = ctx->family;
855 if (strcmp(tg_name, XT_ERROR_TARGET) == 0 ||
856 strcmp(tg_name, XT_STANDARD_TARGET) == 0 ||
857 strcmp(tg_name, "standard") == 0)
858 return ERR_PTR(-EINVAL);
860 /* Re-use the existing target if it's already loaded. */
861 list_for_each_entry(nft_target, &nft_target_list, head) {
862 struct xt_target *target = nft_target->ops.data;
864 if (!target->target)
865 continue;
867 if (nft_target_cmp(target, tg_name, rev, family))
868 return &nft_target->ops;
871 target = xt_request_find_target(family, tg_name, rev);
872 if (IS_ERR(target))
873 return ERR_PTR(-ENOENT);
875 if (!target->target) {
876 err = -EINVAL;
877 goto err;
880 if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
881 err = -EINVAL;
882 goto err;
885 /* This is the first time we use this target, allocate operations */
886 nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
887 if (nft_target == NULL) {
888 err = -ENOMEM;
889 goto err;
892 nft_target->refcnt = 0;
893 nft_target->ops.type = &nft_target_type;
894 nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
895 nft_target->ops.init = nft_target_init;
896 nft_target->ops.destroy = nft_target_destroy;
897 nft_target->ops.dump = nft_target_dump;
898 nft_target->ops.validate = nft_target_validate;
899 nft_target->ops.data = target;
901 if (family == NFPROTO_BRIDGE)
902 nft_target->ops.eval = nft_target_eval_bridge;
903 else
904 nft_target->ops.eval = nft_target_eval_xt;
906 list_add(&nft_target->head, &nft_target_list);
908 return &nft_target->ops;
909 err:
910 module_put(target->me);
911 return ERR_PTR(err);
914 static struct nft_expr_type nft_target_type __read_mostly = {
915 .name = "target",
916 .select_ops = nft_target_select_ops,
917 .policy = nft_target_policy,
918 .maxattr = NFTA_TARGET_MAX,
919 .owner = THIS_MODULE,
922 static int __init nft_compat_module_init(void)
924 int ret;
926 ret = nft_register_expr(&nft_match_type);
927 if (ret < 0)
928 return ret;
930 ret = nft_register_expr(&nft_target_type);
931 if (ret < 0)
932 goto err_match;
934 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
935 if (ret < 0) {
936 pr_err("nft_compat: cannot register with nfnetlink.\n");
937 goto err_target;
940 return ret;
942 err_target:
943 nft_unregister_expr(&nft_target_type);
944 err_match:
945 nft_unregister_expr(&nft_match_type);
946 return ret;
949 static void __exit nft_compat_module_exit(void)
951 struct nft_xt *xt, *next;
953 /* list should be empty here, it can be non-empty only in case there
954 * was an error that caused nft_xt expr to not be initialized fully
955 * and noone else requested the same expression later.
957 * In this case, the lists contain 0-refcount entries that still
958 * hold module reference.
960 list_for_each_entry_safe(xt, next, &nft_target_list, head) {
961 struct xt_target *target = xt->ops.data;
963 if (WARN_ON_ONCE(xt->refcnt))
964 continue;
965 module_put(target->me);
966 kfree(xt);
969 list_for_each_entry_safe(xt, next, &nft_match_list, head) {
970 struct xt_match *match = xt->ops.data;
972 if (WARN_ON_ONCE(xt->refcnt))
973 continue;
974 module_put(match->me);
975 kfree(xt);
977 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
978 nft_unregister_expr(&nft_target_type);
979 nft_unregister_expr(&nft_match_type);
982 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
984 module_init(nft_compat_module_init);
985 module_exit(nft_compat_module_exit);
987 MODULE_LICENSE("GPL");
988 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
989 MODULE_ALIAS_NFT_EXPR("match");
990 MODULE_ALIAS_NFT_EXPR("target");