netfilter: nft_compat: restrict match/target protocol to u16
[platform/kernel/linux-rpi.git] / net / netfilter / nft_compat.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
4  *
5  * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
6  */
7
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>
22 #include <net/netfilter/nf_log.h>
23
24 /* Used for matches where *info is larger than X byte */
25 #define NFT_MATCH_LARGE_THRESH  192
26
27 struct nft_xt_match_priv {
28         void *info;
29 };
30
31 static int nft_compat_chain_validate_dependency(const struct nft_ctx *ctx,
32                                                 const char *tablename)
33 {
34         enum nft_chain_types type = NFT_CHAIN_T_DEFAULT;
35         const struct nft_chain *chain = ctx->chain;
36         const struct nft_base_chain *basechain;
37
38         if (!tablename ||
39             !nft_is_base_chain(chain))
40                 return 0;
41
42         basechain = nft_base_chain(chain);
43         if (strcmp(tablename, "nat") == 0) {
44                 if (ctx->family != NFPROTO_BRIDGE)
45                         type = NFT_CHAIN_T_NAT;
46                 if (basechain->type->type != type)
47                         return -EINVAL;
48         }
49
50         return 0;
51 }
52
53 union nft_entry {
54         struct ipt_entry e4;
55         struct ip6t_entry e6;
56         struct ebt_entry ebt;
57         struct arpt_entry arp;
58 };
59
60 static inline void
61 nft_compat_set_par(struct xt_action_param *par,
62                    const struct nft_pktinfo *pkt,
63                    const void *xt, const void *xt_info)
64 {
65         par->state      = pkt->state;
66         par->thoff      = nft_thoff(pkt);
67         par->fragoff    = pkt->fragoff;
68         par->target     = xt;
69         par->targinfo   = xt_info;
70         par->hotdrop    = false;
71 }
72
73 static void nft_target_eval_xt(const struct nft_expr *expr,
74                                struct nft_regs *regs,
75                                const struct nft_pktinfo *pkt)
76 {
77         void *info = nft_expr_priv(expr);
78         struct xt_target *target = expr->ops->data;
79         struct sk_buff *skb = pkt->skb;
80         struct xt_action_param xt;
81         int ret;
82
83         nft_compat_set_par(&xt, pkt, target, info);
84
85         ret = target->target(skb, &xt);
86
87         if (xt.hotdrop)
88                 ret = NF_DROP;
89
90         switch (ret) {
91         case XT_CONTINUE:
92                 regs->verdict.code = NFT_CONTINUE;
93                 break;
94         default:
95                 regs->verdict.code = ret;
96                 break;
97         }
98 }
99
100 static void nft_target_eval_bridge(const struct nft_expr *expr,
101                                    struct nft_regs *regs,
102                                    const struct nft_pktinfo *pkt)
103 {
104         void *info = nft_expr_priv(expr);
105         struct xt_target *target = expr->ops->data;
106         struct sk_buff *skb = pkt->skb;
107         struct xt_action_param xt;
108         int ret;
109
110         nft_compat_set_par(&xt, pkt, target, info);
111
112         ret = target->target(skb, &xt);
113
114         if (xt.hotdrop)
115                 ret = NF_DROP;
116
117         switch (ret) {
118         case EBT_ACCEPT:
119                 regs->verdict.code = NF_ACCEPT;
120                 break;
121         case EBT_DROP:
122                 regs->verdict.code = NF_DROP;
123                 break;
124         case EBT_CONTINUE:
125                 regs->verdict.code = NFT_CONTINUE;
126                 break;
127         case EBT_RETURN:
128                 regs->verdict.code = NFT_RETURN;
129                 break;
130         default:
131                 regs->verdict.code = ret;
132                 break;
133         }
134 }
135
136 static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
137         [NFTA_TARGET_NAME]      = { .type = NLA_NUL_STRING },
138         [NFTA_TARGET_REV]       = NLA_POLICY_MAX(NLA_BE32, 255),
139         [NFTA_TARGET_INFO]      = { .type = NLA_BINARY },
140 };
141
142 static void
143 nft_target_set_tgchk_param(struct xt_tgchk_param *par,
144                            const struct nft_ctx *ctx,
145                            struct xt_target *target, void *info,
146                            union nft_entry *entry, u16 proto, bool inv)
147 {
148         par->net        = ctx->net;
149         par->table      = ctx->table->name;
150         switch (ctx->family) {
151         case AF_INET:
152                 entry->e4.ip.proto = proto;
153                 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
154                 break;
155         case AF_INET6:
156                 if (proto)
157                         entry->e6.ipv6.flags |= IP6T_F_PROTO;
158
159                 entry->e6.ipv6.proto = proto;
160                 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
161                 break;
162         case NFPROTO_BRIDGE:
163                 entry->ebt.ethproto = (__force __be16)proto;
164                 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
165                 break;
166         case NFPROTO_ARP:
167                 break;
168         }
169         par->entryinfo  = entry;
170         par->target     = target;
171         par->targinfo   = info;
172         if (nft_is_base_chain(ctx->chain)) {
173                 const struct nft_base_chain *basechain =
174                                                 nft_base_chain(ctx->chain);
175                 const struct nf_hook_ops *ops = &basechain->ops;
176
177                 par->hook_mask = 1 << ops->hooknum;
178         } else {
179                 par->hook_mask = 0;
180         }
181         par->family     = ctx->family;
182         par->nft_compat = true;
183 }
184
185 static void target_compat_from_user(struct xt_target *t, void *in, void *out)
186 {
187         int pad;
188
189         memcpy(out, in, t->targetsize);
190         pad = XT_ALIGN(t->targetsize) - t->targetsize;
191         if (pad > 0)
192                 memset(out + t->targetsize, 0, pad);
193 }
194
195 static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
196         [NFTA_RULE_COMPAT_PROTO]        = { .type = NLA_U32 },
197         [NFTA_RULE_COMPAT_FLAGS]        = { .type = NLA_U32 },
198 };
199
200 static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv)
201 {
202         struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
203         u32 l4proto;
204         u32 flags;
205         int err;
206
207         err = nla_parse_nested_deprecated(tb, NFTA_RULE_COMPAT_MAX, attr,
208                                           nft_rule_compat_policy, NULL);
209         if (err < 0)
210                 return err;
211
212         if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
213                 return -EINVAL;
214
215         flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
216         if (flags & NFT_RULE_COMPAT_F_UNUSED ||
217             flags & ~NFT_RULE_COMPAT_F_MASK)
218                 return -EINVAL;
219         if (flags & NFT_RULE_COMPAT_F_INV)
220                 *inv = true;
221
222         l4proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
223         if (l4proto > U16_MAX)
224                 return -EINVAL;
225
226         *proto = l4proto;
227
228         return 0;
229 }
230
231 static void nft_compat_wait_for_destructors(void)
232 {
233         /* xtables matches or targets can have side effects, e.g.
234          * creation/destruction of /proc files.
235          * The xt ->destroy functions are run asynchronously from
236          * work queue.  If we have pending invocations we thus
237          * need to wait for those to finish.
238          */
239         nf_tables_trans_destroy_flush_work();
240 }
241
242 static int
243 nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
244                 const struct nlattr * const tb[])
245 {
246         void *info = nft_expr_priv(expr);
247         struct xt_target *target = expr->ops->data;
248         struct xt_tgchk_param par;
249         size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
250         u16 proto = 0;
251         bool inv = false;
252         union nft_entry e = {};
253         int ret;
254
255         target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
256
257         if (ctx->nla[NFTA_RULE_COMPAT]) {
258                 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
259                 if (ret < 0)
260                         return ret;
261         }
262
263         nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
264
265         nft_compat_wait_for_destructors();
266
267         ret = xt_check_target(&par, size, proto, inv);
268         if (ret < 0) {
269                 if (ret == -ENOENT) {
270                         const char *modname = NULL;
271
272                         if (strcmp(target->name, "LOG") == 0)
273                                 modname = "nf_log_syslog";
274                         else if (strcmp(target->name, "NFLOG") == 0)
275                                 modname = "nfnetlink_log";
276
277                         if (modname &&
278                             nft_request_module(ctx->net, "%s", modname) == -EAGAIN)
279                                 return -EAGAIN;
280                 }
281
282                 return ret;
283         }
284
285         /* The standard target cannot be used */
286         if (!target->target)
287                 return -EINVAL;
288
289         return 0;
290 }
291
292 static void __nft_mt_tg_destroy(struct module *me, const struct nft_expr *expr)
293 {
294         module_put(me);
295         kfree(expr->ops);
296 }
297
298 static void
299 nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
300 {
301         struct xt_target *target = expr->ops->data;
302         void *info = nft_expr_priv(expr);
303         struct module *me = target->me;
304         struct xt_tgdtor_param par;
305
306         par.net = ctx->net;
307         par.target = target;
308         par.targinfo = info;
309         par.family = ctx->family;
310         if (par.target->destroy != NULL)
311                 par.target->destroy(&par);
312
313         __nft_mt_tg_destroy(me, expr);
314 }
315
316 static int nft_extension_dump_info(struct sk_buff *skb, int attr,
317                                    const void *info,
318                                    unsigned int size, unsigned int user_size)
319 {
320         unsigned int info_size, aligned_size = XT_ALIGN(size);
321         struct nlattr *nla;
322
323         nla = nla_reserve(skb, attr, aligned_size);
324         if (!nla)
325                 return -1;
326
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);
330
331         return 0;
332 }
333
334 static int nft_target_dump(struct sk_buff *skb,
335                            const struct nft_expr *expr, bool reset)
336 {
337         const struct xt_target *target = expr->ops->data;
338         void *info = nft_expr_priv(expr);
339
340         if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
341             nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
342             nft_extension_dump_info(skb, NFTA_TARGET_INFO, info,
343                                     target->targetsize, target->usersize))
344                 goto nla_put_failure;
345
346         return 0;
347
348 nla_put_failure:
349         return -1;
350 }
351
352 static int nft_target_validate(const struct nft_ctx *ctx,
353                                const struct nft_expr *expr,
354                                const struct nft_data **data)
355 {
356         struct xt_target *target = expr->ops->data;
357         unsigned int hook_mask = 0;
358         int ret;
359
360         if (ctx->family != NFPROTO_IPV4 &&
361             ctx->family != NFPROTO_IPV6 &&
362             ctx->family != NFPROTO_BRIDGE &&
363             ctx->family != NFPROTO_ARP)
364                 return -EOPNOTSUPP;
365
366         if (nft_is_base_chain(ctx->chain)) {
367                 const struct nft_base_chain *basechain =
368                                                 nft_base_chain(ctx->chain);
369                 const struct nf_hook_ops *ops = &basechain->ops;
370
371                 hook_mask = 1 << ops->hooknum;
372                 if (target->hooks && !(hook_mask & target->hooks))
373                         return -EINVAL;
374
375                 ret = nft_compat_chain_validate_dependency(ctx, target->table);
376                 if (ret < 0)
377                         return ret;
378         }
379         return 0;
380 }
381
382 static void __nft_match_eval(const struct nft_expr *expr,
383                              struct nft_regs *regs,
384                              const struct nft_pktinfo *pkt,
385                              void *info)
386 {
387         struct xt_match *match = expr->ops->data;
388         struct sk_buff *skb = pkt->skb;
389         struct xt_action_param xt;
390         bool ret;
391
392         nft_compat_set_par(&xt, pkt, match, info);
393
394         ret = match->match(skb, &xt);
395
396         if (xt.hotdrop) {
397                 regs->verdict.code = NF_DROP;
398                 return;
399         }
400
401         switch (ret ? 1 : 0) {
402         case 1:
403                 regs->verdict.code = NFT_CONTINUE;
404                 break;
405         case 0:
406                 regs->verdict.code = NFT_BREAK;
407                 break;
408         }
409 }
410
411 static void nft_match_large_eval(const struct nft_expr *expr,
412                                  struct nft_regs *regs,
413                                  const struct nft_pktinfo *pkt)
414 {
415         struct nft_xt_match_priv *priv = nft_expr_priv(expr);
416
417         __nft_match_eval(expr, regs, pkt, priv->info);
418 }
419
420 static void nft_match_eval(const struct nft_expr *expr,
421                            struct nft_regs *regs,
422                            const struct nft_pktinfo *pkt)
423 {
424         __nft_match_eval(expr, regs, pkt, nft_expr_priv(expr));
425 }
426
427 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
428         [NFTA_MATCH_NAME]       = { .type = NLA_NUL_STRING },
429         [NFTA_MATCH_REV]        = NLA_POLICY_MAX(NLA_BE32, 255),
430         [NFTA_MATCH_INFO]       = { .type = NLA_BINARY },
431 };
432
433 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
434 static void
435 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
436                           struct xt_match *match, void *info,
437                           union nft_entry *entry, u16 proto, bool inv)
438 {
439         par->net        = ctx->net;
440         par->table      = ctx->table->name;
441         switch (ctx->family) {
442         case AF_INET:
443                 entry->e4.ip.proto = proto;
444                 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
445                 break;
446         case AF_INET6:
447                 if (proto)
448                         entry->e6.ipv6.flags |= IP6T_F_PROTO;
449
450                 entry->e6.ipv6.proto = proto;
451                 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
452                 break;
453         case NFPROTO_BRIDGE:
454                 entry->ebt.ethproto = (__force __be16)proto;
455                 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
456                 break;
457         case NFPROTO_ARP:
458                 break;
459         }
460         par->entryinfo  = entry;
461         par->match      = match;
462         par->matchinfo  = info;
463         if (nft_is_base_chain(ctx->chain)) {
464                 const struct nft_base_chain *basechain =
465                                                 nft_base_chain(ctx->chain);
466                 const struct nf_hook_ops *ops = &basechain->ops;
467
468                 par->hook_mask = 1 << ops->hooknum;
469         } else {
470                 par->hook_mask = 0;
471         }
472         par->family     = ctx->family;
473         par->nft_compat = true;
474 }
475
476 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
477 {
478         int pad;
479
480         memcpy(out, in, m->matchsize);
481         pad = XT_ALIGN(m->matchsize) - m->matchsize;
482         if (pad > 0)
483                 memset(out + m->matchsize, 0, pad);
484 }
485
486 static int
487 __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
488                  const struct nlattr * const tb[],
489                  void *info)
490 {
491         struct xt_match *match = expr->ops->data;
492         struct xt_mtchk_param par;
493         size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
494         u16 proto = 0;
495         bool inv = false;
496         union nft_entry e = {};
497         int ret;
498
499         match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
500
501         if (ctx->nla[NFTA_RULE_COMPAT]) {
502                 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
503                 if (ret < 0)
504                         return ret;
505         }
506
507         nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
508
509         nft_compat_wait_for_destructors();
510
511         return xt_check_match(&par, size, proto, inv);
512 }
513
514 static int
515 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
516                const struct nlattr * const tb[])
517 {
518         return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
519 }
520
521 static int
522 nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
523                      const struct nlattr * const tb[])
524 {
525         struct nft_xt_match_priv *priv = nft_expr_priv(expr);
526         struct xt_match *m = expr->ops->data;
527         int ret;
528
529         priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL);
530         if (!priv->info)
531                 return -ENOMEM;
532
533         ret = __nft_match_init(ctx, expr, tb, priv->info);
534         if (ret)
535                 kfree(priv->info);
536         return ret;
537 }
538
539 static void
540 __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
541                     void *info)
542 {
543         struct xt_match *match = expr->ops->data;
544         struct module *me = match->me;
545         struct xt_mtdtor_param par;
546
547         par.net = ctx->net;
548         par.match = match;
549         par.matchinfo = info;
550         par.family = ctx->family;
551         if (par.match->destroy != NULL)
552                 par.match->destroy(&par);
553
554         __nft_mt_tg_destroy(me, expr);
555 }
556
557 static void
558 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
559 {
560         __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
561 }
562
563 static void
564 nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
565 {
566         struct nft_xt_match_priv *priv = nft_expr_priv(expr);
567
568         __nft_match_destroy(ctx, expr, priv->info);
569         kfree(priv->info);
570 }
571
572 static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
573                             void *info)
574 {
575         struct xt_match *match = expr->ops->data;
576
577         if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
578             nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
579             nft_extension_dump_info(skb, NFTA_MATCH_INFO, info,
580                                     match->matchsize, match->usersize))
581                 goto nla_put_failure;
582
583         return 0;
584
585 nla_put_failure:
586         return -1;
587 }
588
589 static int nft_match_dump(struct sk_buff *skb,
590                           const struct nft_expr *expr, bool reset)
591 {
592         return __nft_match_dump(skb, expr, nft_expr_priv(expr));
593 }
594
595 static int nft_match_large_dump(struct sk_buff *skb,
596                                 const struct nft_expr *e, bool reset)
597 {
598         struct nft_xt_match_priv *priv = nft_expr_priv(e);
599
600         return __nft_match_dump(skb, e, priv->info);
601 }
602
603 static int nft_match_validate(const struct nft_ctx *ctx,
604                               const struct nft_expr *expr,
605                               const struct nft_data **data)
606 {
607         struct xt_match *match = expr->ops->data;
608         unsigned int hook_mask = 0;
609         int ret;
610
611         if (ctx->family != NFPROTO_IPV4 &&
612             ctx->family != NFPROTO_IPV6 &&
613             ctx->family != NFPROTO_BRIDGE &&
614             ctx->family != NFPROTO_ARP)
615                 return -EOPNOTSUPP;
616
617         if (nft_is_base_chain(ctx->chain)) {
618                 const struct nft_base_chain *basechain =
619                                                 nft_base_chain(ctx->chain);
620                 const struct nf_hook_ops *ops = &basechain->ops;
621
622                 hook_mask = 1 << ops->hooknum;
623                 if (match->hooks && !(hook_mask & match->hooks))
624                         return -EINVAL;
625
626                 ret = nft_compat_chain_validate_dependency(ctx, match->table);
627                 if (ret < 0)
628                         return ret;
629         }
630         return 0;
631 }
632
633 static int
634 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
635                       int event, u16 family, const char *name,
636                       int rev, int target)
637 {
638         struct nlmsghdr *nlh;
639         unsigned int flags = portid ? NLM_F_MULTI : 0;
640
641         event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
642         nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
643                            NFNETLINK_V0, 0);
644         if (!nlh)
645                 goto nlmsg_failure;
646
647         if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
648             nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
649             nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
650                 goto nla_put_failure;
651
652         nlmsg_end(skb, nlh);
653         return skb->len;
654
655 nlmsg_failure:
656 nla_put_failure:
657         nlmsg_cancel(skb, nlh);
658         return -1;
659 }
660
661 static int nfnl_compat_get_rcu(struct sk_buff *skb,
662                                const struct nfnl_info *info,
663                                const struct nlattr * const tb[])
664 {
665         u8 family = info->nfmsg->nfgen_family;
666         const char *name, *fmt;
667         struct sk_buff *skb2;
668         int ret = 0, target;
669         u32 rev;
670
671         if (tb[NFTA_COMPAT_NAME] == NULL ||
672             tb[NFTA_COMPAT_REV] == NULL ||
673             tb[NFTA_COMPAT_TYPE] == NULL)
674                 return -EINVAL;
675
676         name = nla_data(tb[NFTA_COMPAT_NAME]);
677         rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
678         target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
679
680         switch(family) {
681         case AF_INET:
682                 fmt = "ipt_%s";
683                 break;
684         case AF_INET6:
685                 fmt = "ip6t_%s";
686                 break;
687         case NFPROTO_BRIDGE:
688                 fmt = "ebt_%s";
689                 break;
690         case NFPROTO_ARP:
691                 fmt = "arpt_%s";
692                 break;
693         default:
694                 pr_err("nft_compat: unsupported protocol %d\n", family);
695                 return -EINVAL;
696         }
697
698         if (!try_module_get(THIS_MODULE))
699                 return -EINVAL;
700
701         rcu_read_unlock();
702         try_then_request_module(xt_find_revision(family, name, rev, target, &ret),
703                                 fmt, name);
704         if (ret < 0)
705                 goto out_put;
706
707         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
708         if (skb2 == NULL) {
709                 ret = -ENOMEM;
710                 goto out_put;
711         }
712
713         /* include the best revision for this extension in the message */
714         if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
715                                   info->nlh->nlmsg_seq,
716                                   NFNL_MSG_TYPE(info->nlh->nlmsg_type),
717                                   NFNL_MSG_COMPAT_GET,
718                                   family, name, ret, target) <= 0) {
719                 kfree_skb(skb2);
720                 goto out_put;
721         }
722
723         ret = nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
724 out_put:
725         rcu_read_lock();
726         module_put(THIS_MODULE);
727
728         return ret;
729 }
730
731 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
732         [NFTA_COMPAT_NAME]      = { .type = NLA_NUL_STRING,
733                                     .len = NFT_COMPAT_NAME_MAX-1 },
734         [NFTA_COMPAT_REV]       = NLA_POLICY_MAX(NLA_BE32, 255),
735         [NFTA_COMPAT_TYPE]      = { .type = NLA_U32 },
736 };
737
738 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
739         [NFNL_MSG_COMPAT_GET]   = {
740                 .call           = nfnl_compat_get_rcu,
741                 .type           = NFNL_CB_RCU,
742                 .attr_count     = NFTA_COMPAT_MAX,
743                 .policy         = nfnl_compat_policy_get
744         },
745 };
746
747 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
748         .name           = "nft-compat",
749         .subsys_id      = NFNL_SUBSYS_NFT_COMPAT,
750         .cb_count       = NFNL_MSG_COMPAT_MAX,
751         .cb             = nfnl_nft_compat_cb,
752 };
753
754 static struct nft_expr_type nft_match_type;
755
756 static bool nft_match_reduce(struct nft_regs_track *track,
757                              const struct nft_expr *expr)
758 {
759         const struct xt_match *match = expr->ops->data;
760
761         return strcmp(match->name, "comment") == 0;
762 }
763
764 static const struct nft_expr_ops *
765 nft_match_select_ops(const struct nft_ctx *ctx,
766                      const struct nlattr * const tb[])
767 {
768         struct nft_expr_ops *ops;
769         struct xt_match *match;
770         unsigned int matchsize;
771         char *mt_name;
772         u32 rev, family;
773         int err;
774
775         if (tb[NFTA_MATCH_NAME] == NULL ||
776             tb[NFTA_MATCH_REV] == NULL ||
777             tb[NFTA_MATCH_INFO] == NULL)
778                 return ERR_PTR(-EINVAL);
779
780         mt_name = nla_data(tb[NFTA_MATCH_NAME]);
781         rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
782         family = ctx->family;
783
784         match = xt_request_find_match(family, mt_name, rev);
785         if (IS_ERR(match))
786                 return ERR_PTR(-ENOENT);
787
788         if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
789                 err = -EINVAL;
790                 goto err;
791         }
792
793         ops = kzalloc(sizeof(struct nft_expr_ops), GFP_KERNEL);
794         if (!ops) {
795                 err = -ENOMEM;
796                 goto err;
797         }
798
799         ops->type = &nft_match_type;
800         ops->eval = nft_match_eval;
801         ops->init = nft_match_init;
802         ops->destroy = nft_match_destroy;
803         ops->dump = nft_match_dump;
804         ops->validate = nft_match_validate;
805         ops->data = match;
806         ops->reduce = nft_match_reduce;
807
808         matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
809         if (matchsize > NFT_MATCH_LARGE_THRESH) {
810                 matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
811
812                 ops->eval = nft_match_large_eval;
813                 ops->init = nft_match_large_init;
814                 ops->destroy = nft_match_large_destroy;
815                 ops->dump = nft_match_large_dump;
816         }
817
818         ops->size = matchsize;
819
820         return ops;
821 err:
822         module_put(match->me);
823         return ERR_PTR(err);
824 }
825
826 static void nft_match_release_ops(const struct nft_expr_ops *ops)
827 {
828         struct xt_match *match = ops->data;
829
830         module_put(match->me);
831         kfree(ops);
832 }
833
834 static struct nft_expr_type nft_match_type __read_mostly = {
835         .name           = "match",
836         .select_ops     = nft_match_select_ops,
837         .release_ops    = nft_match_release_ops,
838         .policy         = nft_match_policy,
839         .maxattr        = NFTA_MATCH_MAX,
840         .owner          = THIS_MODULE,
841 };
842
843 static struct nft_expr_type nft_target_type;
844
845 static const struct nft_expr_ops *
846 nft_target_select_ops(const struct nft_ctx *ctx,
847                       const struct nlattr * const tb[])
848 {
849         struct nft_expr_ops *ops;
850         struct xt_target *target;
851         char *tg_name;
852         u32 rev, family;
853         int err;
854
855         if (tb[NFTA_TARGET_NAME] == NULL ||
856             tb[NFTA_TARGET_REV] == NULL ||
857             tb[NFTA_TARGET_INFO] == NULL)
858                 return ERR_PTR(-EINVAL);
859
860         tg_name = nla_data(tb[NFTA_TARGET_NAME]);
861         rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
862         family = ctx->family;
863
864         if (strcmp(tg_name, XT_ERROR_TARGET) == 0 ||
865             strcmp(tg_name, XT_STANDARD_TARGET) == 0 ||
866             strcmp(tg_name, "standard") == 0)
867                 return ERR_PTR(-EINVAL);
868
869         target = xt_request_find_target(family, tg_name, rev);
870         if (IS_ERR(target))
871                 return ERR_PTR(-ENOENT);
872
873         if (!target->target) {
874                 err = -EINVAL;
875                 goto err;
876         }
877
878         if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
879                 err = -EINVAL;
880                 goto err;
881         }
882
883         ops = kzalloc(sizeof(struct nft_expr_ops), GFP_KERNEL);
884         if (!ops) {
885                 err = -ENOMEM;
886                 goto err;
887         }
888
889         ops->type = &nft_target_type;
890         ops->size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
891         ops->init = nft_target_init;
892         ops->destroy = nft_target_destroy;
893         ops->dump = nft_target_dump;
894         ops->validate = nft_target_validate;
895         ops->data = target;
896         ops->reduce = NFT_REDUCE_READONLY;
897
898         if (family == NFPROTO_BRIDGE)
899                 ops->eval = nft_target_eval_bridge;
900         else
901                 ops->eval = nft_target_eval_xt;
902
903         return ops;
904 err:
905         module_put(target->me);
906         return ERR_PTR(err);
907 }
908
909 static void nft_target_release_ops(const struct nft_expr_ops *ops)
910 {
911         struct xt_target *target = ops->data;
912
913         module_put(target->me);
914         kfree(ops);
915 }
916
917 static struct nft_expr_type nft_target_type __read_mostly = {
918         .name           = "target",
919         .select_ops     = nft_target_select_ops,
920         .release_ops    = nft_target_release_ops,
921         .policy         = nft_target_policy,
922         .maxattr        = NFTA_TARGET_MAX,
923         .owner          = THIS_MODULE,
924 };
925
926 static int __init nft_compat_module_init(void)
927 {
928         int ret;
929
930         ret = nft_register_expr(&nft_match_type);
931         if (ret < 0)
932                 return ret;
933
934         ret = nft_register_expr(&nft_target_type);
935         if (ret < 0)
936                 goto err_match;
937
938         ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
939         if (ret < 0) {
940                 pr_err("nft_compat: cannot register with nfnetlink.\n");
941                 goto err_target;
942         }
943
944         return ret;
945 err_target:
946         nft_unregister_expr(&nft_target_type);
947 err_match:
948         nft_unregister_expr(&nft_match_type);
949         return ret;
950 }
951
952 static void __exit nft_compat_module_exit(void)
953 {
954         nfnetlink_subsys_unregister(&nfnl_compat_subsys);
955         nft_unregister_expr(&nft_target_type);
956         nft_unregister_expr(&nft_match_type);
957 }
958
959 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
960
961 module_init(nft_compat_module_init);
962 module_exit(nft_compat_module_exit);
963
964 MODULE_LICENSE("GPL");
965 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
966 MODULE_ALIAS_NFT_EXPR("match");
967 MODULE_ALIAS_NFT_EXPR("target");
968 MODULE_DESCRIPTION("x_tables over nftables support");