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