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