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