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