Merge tag 'linux-kselftest-5.0-rc5' of git://git.kernel.org/pub/scm/linux/kernel...
[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 #include <net/netns/generic.h>
26
27 struct nft_xt {
28         struct list_head        head;
29         struct nft_expr_ops     ops;
30         refcount_t              refcnt;
31
32         /* used only when transaction mutex is locked */
33         unsigned int            listcnt;
34
35         /* Unlike other expressions, ops doesn't have static storage duration.
36          * nft core assumes they do.  We use kfree_rcu so that nft core can
37          * can check expr->ops->size even after nft_compat->destroy() frees
38          * the nft_xt struct that holds the ops structure.
39          */
40         struct rcu_head         rcu_head;
41 };
42
43 /* Used for matches where *info is larger than X byte */
44 #define NFT_MATCH_LARGE_THRESH  192
45
46 struct nft_xt_match_priv {
47         void *info;
48 };
49
50 struct nft_compat_net {
51         struct list_head nft_target_list;
52         struct list_head nft_match_list;
53 };
54
55 static unsigned int nft_compat_net_id __read_mostly;
56 static struct nft_expr_type nft_match_type;
57 static struct nft_expr_type nft_target_type;
58
59 static struct nft_compat_net *nft_compat_pernet(struct net *net)
60 {
61         return net_generic(net, nft_compat_net_id);
62 }
63
64 static bool nft_xt_put(struct nft_xt *xt)
65 {
66         if (refcount_dec_and_test(&xt->refcnt)) {
67                 WARN_ON_ONCE(!list_empty(&xt->head));
68                 kfree_rcu(xt, rcu_head);
69                 return true;
70         }
71
72         return false;
73 }
74
75 static int nft_compat_chain_validate_dependency(const struct nft_ctx *ctx,
76                                                 const char *tablename)
77 {
78         enum nft_chain_types type = NFT_CHAIN_T_DEFAULT;
79         const struct nft_chain *chain = ctx->chain;
80         const struct nft_base_chain *basechain;
81
82         if (!tablename ||
83             !nft_is_base_chain(chain))
84                 return 0;
85
86         basechain = nft_base_chain(chain);
87         if (strcmp(tablename, "nat") == 0) {
88                 if (ctx->family != NFPROTO_BRIDGE)
89                         type = NFT_CHAIN_T_NAT;
90                 if (basechain->type->type != type)
91                         return -EINVAL;
92         }
93
94         return 0;
95 }
96
97 union nft_entry {
98         struct ipt_entry e4;
99         struct ip6t_entry e6;
100         struct ebt_entry ebt;
101         struct arpt_entry arp;
102 };
103
104 static inline void
105 nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
106 {
107         par->target     = xt;
108         par->targinfo   = xt_info;
109         par->hotdrop    = false;
110 }
111
112 static void nft_target_eval_xt(const struct nft_expr *expr,
113                                struct nft_regs *regs,
114                                const struct nft_pktinfo *pkt)
115 {
116         void *info = nft_expr_priv(expr);
117         struct xt_target *target = expr->ops->data;
118         struct sk_buff *skb = pkt->skb;
119         int ret;
120
121         nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
122
123         ret = target->target(skb, &pkt->xt);
124
125         if (pkt->xt.hotdrop)
126                 ret = NF_DROP;
127
128         switch (ret) {
129         case XT_CONTINUE:
130                 regs->verdict.code = NFT_CONTINUE;
131                 break;
132         default:
133                 regs->verdict.code = ret;
134                 break;
135         }
136 }
137
138 static void nft_target_eval_bridge(const struct nft_expr *expr,
139                                    struct nft_regs *regs,
140                                    const struct nft_pktinfo *pkt)
141 {
142         void *info = nft_expr_priv(expr);
143         struct xt_target *target = expr->ops->data;
144         struct sk_buff *skb = pkt->skb;
145         int ret;
146
147         nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
148
149         ret = target->target(skb, &pkt->xt);
150
151         if (pkt->xt.hotdrop)
152                 ret = NF_DROP;
153
154         switch (ret) {
155         case EBT_ACCEPT:
156                 regs->verdict.code = NF_ACCEPT;
157                 break;
158         case EBT_DROP:
159                 regs->verdict.code = NF_DROP;
160                 break;
161         case EBT_CONTINUE:
162                 regs->verdict.code = NFT_CONTINUE;
163                 break;
164         case EBT_RETURN:
165                 regs->verdict.code = NFT_RETURN;
166                 break;
167         default:
168                 regs->verdict.code = ret;
169                 break;
170         }
171 }
172
173 static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
174         [NFTA_TARGET_NAME]      = { .type = NLA_NUL_STRING },
175         [NFTA_TARGET_REV]       = { .type = NLA_U32 },
176         [NFTA_TARGET_INFO]      = { .type = NLA_BINARY },
177 };
178
179 static void
180 nft_target_set_tgchk_param(struct xt_tgchk_param *par,
181                            const struct nft_ctx *ctx,
182                            struct xt_target *target, void *info,
183                            union nft_entry *entry, u16 proto, bool inv)
184 {
185         par->net        = ctx->net;
186         par->table      = ctx->table->name;
187         switch (ctx->family) {
188         case AF_INET:
189                 entry->e4.ip.proto = proto;
190                 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
191                 break;
192         case AF_INET6:
193                 if (proto)
194                         entry->e6.ipv6.flags |= IP6T_F_PROTO;
195
196                 entry->e6.ipv6.proto = proto;
197                 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
198                 break;
199         case NFPROTO_BRIDGE:
200                 entry->ebt.ethproto = (__force __be16)proto;
201                 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
202                 break;
203         case NFPROTO_ARP:
204                 break;
205         }
206         par->entryinfo  = entry;
207         par->target     = target;
208         par->targinfo   = info;
209         if (nft_is_base_chain(ctx->chain)) {
210                 const struct nft_base_chain *basechain =
211                                                 nft_base_chain(ctx->chain);
212                 const struct nf_hook_ops *ops = &basechain->ops;
213
214                 par->hook_mask = 1 << ops->hooknum;
215         } else {
216                 par->hook_mask = 0;
217         }
218         par->family     = ctx->family;
219         par->nft_compat = true;
220 }
221
222 static void target_compat_from_user(struct xt_target *t, void *in, void *out)
223 {
224         int pad;
225
226         memcpy(out, in, t->targetsize);
227         pad = XT_ALIGN(t->targetsize) - t->targetsize;
228         if (pad > 0)
229                 memset(out + t->targetsize, 0, pad);
230 }
231
232 static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
233         [NFTA_RULE_COMPAT_PROTO]        = { .type = NLA_U32 },
234         [NFTA_RULE_COMPAT_FLAGS]        = { .type = NLA_U32 },
235 };
236
237 static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv)
238 {
239         struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
240         u32 flags;
241         int err;
242
243         err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr,
244                                nft_rule_compat_policy, NULL);
245         if (err < 0)
246                 return err;
247
248         if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
249                 return -EINVAL;
250
251         flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
252         if (flags & ~NFT_RULE_COMPAT_F_MASK)
253                 return -EINVAL;
254         if (flags & NFT_RULE_COMPAT_F_INV)
255                 *inv = true;
256
257         *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
258         return 0;
259 }
260
261 static int
262 nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
263                 const struct nlattr * const tb[])
264 {
265         void *info = nft_expr_priv(expr);
266         struct xt_target *target = expr->ops->data;
267         struct xt_tgchk_param par;
268         size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
269         struct nft_xt *nft_xt;
270         u16 proto = 0;
271         bool inv = false;
272         union nft_entry e = {};
273         int ret;
274
275         target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
276
277         if (ctx->nla[NFTA_RULE_COMPAT]) {
278                 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
279                 if (ret < 0)
280                         return ret;
281         }
282
283         nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
284
285         ret = xt_check_target(&par, size, proto, inv);
286         if (ret < 0)
287                 return ret;
288
289         /* The standard target cannot be used */
290         if (!target->target)
291                 return -EINVAL;
292
293         nft_xt = container_of(expr->ops, struct nft_xt, ops);
294         refcount_inc(&nft_xt->refcnt);
295         return 0;
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 xt_tgdtor_param par;
304
305         par.net = ctx->net;
306         par.target = target;
307         par.targinfo = info;
308         par.family = ctx->family;
309         if (par.target->destroy != NULL)
310                 par.target->destroy(&par);
311
312         if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
313                 module_put(target->me);
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 (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         bool ret;
383
384         nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
385
386         ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
387
388         if (pkt->xt.hotdrop) {
389                 regs->verdict.code = NF_DROP;
390                 return;
391         }
392
393         switch (ret ? 1 : 0) {
394         case 1:
395                 regs->verdict.code = NFT_CONTINUE;
396                 break;
397         case 0:
398                 regs->verdict.code = NFT_BREAK;
399                 break;
400         }
401 }
402
403 static void nft_match_large_eval(const struct nft_expr *expr,
404                                  struct nft_regs *regs,
405                                  const struct nft_pktinfo *pkt)
406 {
407         struct nft_xt_match_priv *priv = nft_expr_priv(expr);
408
409         __nft_match_eval(expr, regs, pkt, priv->info);
410 }
411
412 static void nft_match_eval(const struct nft_expr *expr,
413                            struct nft_regs *regs,
414                            const struct nft_pktinfo *pkt)
415 {
416         __nft_match_eval(expr, regs, pkt, nft_expr_priv(expr));
417 }
418
419 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
420         [NFTA_MATCH_NAME]       = { .type = NLA_NUL_STRING },
421         [NFTA_MATCH_REV]        = { .type = NLA_U32 },
422         [NFTA_MATCH_INFO]       = { .type = NLA_BINARY },
423 };
424
425 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
426 static void
427 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
428                           struct xt_match *match, void *info,
429                           union nft_entry *entry, u16 proto, bool inv)
430 {
431         par->net        = ctx->net;
432         par->table      = ctx->table->name;
433         switch (ctx->family) {
434         case AF_INET:
435                 entry->e4.ip.proto = proto;
436                 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
437                 break;
438         case AF_INET6:
439                 if (proto)
440                         entry->e6.ipv6.flags |= IP6T_F_PROTO;
441
442                 entry->e6.ipv6.proto = proto;
443                 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
444                 break;
445         case NFPROTO_BRIDGE:
446                 entry->ebt.ethproto = (__force __be16)proto;
447                 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
448                 break;
449         case NFPROTO_ARP:
450                 break;
451         }
452         par->entryinfo  = entry;
453         par->match      = match;
454         par->matchinfo  = info;
455         if (nft_is_base_chain(ctx->chain)) {
456                 const struct nft_base_chain *basechain =
457                                                 nft_base_chain(ctx->chain);
458                 const struct nf_hook_ops *ops = &basechain->ops;
459
460                 par->hook_mask = 1 << ops->hooknum;
461         } else {
462                 par->hook_mask = 0;
463         }
464         par->family     = ctx->family;
465         par->nft_compat = true;
466 }
467
468 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
469 {
470         int pad;
471
472         memcpy(out, in, m->matchsize);
473         pad = XT_ALIGN(m->matchsize) - m->matchsize;
474         if (pad > 0)
475                 memset(out + m->matchsize, 0, pad);
476 }
477
478 static int
479 __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
480                  const struct nlattr * const tb[],
481                  void *info)
482 {
483         struct xt_match *match = expr->ops->data;
484         struct xt_mtchk_param par;
485         size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
486         struct nft_xt *nft_xt;
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         ret = xt_check_match(&par, size, proto, inv);
503         if (ret < 0)
504                 return ret;
505
506         nft_xt = container_of(expr->ops, struct nft_xt, ops);
507         refcount_inc(&nft_xt->refcnt);
508         return 0;
509 }
510
511 static int
512 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
513                const struct nlattr * const tb[])
514 {
515         return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
516 }
517
518 static int
519 nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
520                      const struct nlattr * const tb[])
521 {
522         struct nft_xt_match_priv *priv = nft_expr_priv(expr);
523         struct xt_match *m = expr->ops->data;
524         int ret;
525
526         priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL);
527         if (!priv->info)
528                 return -ENOMEM;
529
530         ret = __nft_match_init(ctx, expr, tb, priv->info);
531         if (ret)
532                 kfree(priv->info);
533         return ret;
534 }
535
536 static void
537 __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
538                     void *info)
539 {
540         struct xt_match *match = expr->ops->data;
541         struct module *me = match->me;
542         struct xt_mtdtor_param par;
543
544         par.net = ctx->net;
545         par.match = match;
546         par.matchinfo = info;
547         par.family = ctx->family;
548         if (par.match->destroy != NULL)
549                 par.match->destroy(&par);
550
551         if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
552                 module_put(me);
553 }
554
555 static void
556 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
557 {
558         __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
559 }
560
561 static void nft_compat_activate(const struct nft_ctx *ctx,
562                                 const struct nft_expr *expr,
563                                 struct list_head *h)
564 {
565         struct nft_xt *xt = container_of(expr->ops, struct nft_xt, ops);
566
567         if (xt->listcnt == 0)
568                 list_add(&xt->head, h);
569
570         xt->listcnt++;
571 }
572
573 static void nft_compat_activate_mt(const struct nft_ctx *ctx,
574                                    const struct nft_expr *expr)
575 {
576         struct nft_compat_net *cn = nft_compat_pernet(ctx->net);
577
578         nft_compat_activate(ctx, expr, &cn->nft_match_list);
579 }
580
581 static void nft_compat_activate_tg(const struct nft_ctx *ctx,
582                                    const struct nft_expr *expr)
583 {
584         struct nft_compat_net *cn = nft_compat_pernet(ctx->net);
585
586         nft_compat_activate(ctx, expr, &cn->nft_target_list);
587 }
588
589 static void nft_compat_deactivate(const struct nft_ctx *ctx,
590                                   const struct nft_expr *expr)
591 {
592         struct nft_xt *xt = container_of(expr->ops, struct nft_xt, ops);
593
594         if (--xt->listcnt == 0)
595                 list_del_init(&xt->head);
596 }
597
598 static void
599 nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
600 {
601         struct nft_xt_match_priv *priv = nft_expr_priv(expr);
602
603         __nft_match_destroy(ctx, expr, priv->info);
604         kfree(priv->info);
605 }
606
607 static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
608                             void *info)
609 {
610         struct xt_match *match = expr->ops->data;
611
612         if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
613             nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
614             nft_extension_dump_info(skb, NFTA_MATCH_INFO, info,
615                                     match->matchsize, match->usersize))
616                 goto nla_put_failure;
617
618         return 0;
619
620 nla_put_failure:
621         return -1;
622 }
623
624 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
625 {
626         return __nft_match_dump(skb, expr, nft_expr_priv(expr));
627 }
628
629 static int nft_match_large_dump(struct sk_buff *skb, const struct nft_expr *e)
630 {
631         struct nft_xt_match_priv *priv = nft_expr_priv(e);
632
633         return __nft_match_dump(skb, e, priv->info);
634 }
635
636 static int nft_match_validate(const struct nft_ctx *ctx,
637                               const struct nft_expr *expr,
638                               const struct nft_data **data)
639 {
640         struct xt_match *match = expr->ops->data;
641         unsigned int hook_mask = 0;
642         int ret;
643
644         if (nft_is_base_chain(ctx->chain)) {
645                 const struct nft_base_chain *basechain =
646                                                 nft_base_chain(ctx->chain);
647                 const struct nf_hook_ops *ops = &basechain->ops;
648
649                 hook_mask = 1 << ops->hooknum;
650                 if (match->hooks && !(hook_mask & match->hooks))
651                         return -EINVAL;
652
653                 ret = nft_compat_chain_validate_dependency(ctx, match->table);
654                 if (ret < 0)
655                         return ret;
656         }
657         return 0;
658 }
659
660 static int
661 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
662                       int event, u16 family, const char *name,
663                       int rev, int target)
664 {
665         struct nlmsghdr *nlh;
666         struct nfgenmsg *nfmsg;
667         unsigned int flags = portid ? NLM_F_MULTI : 0;
668
669         event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
670         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
671         if (nlh == NULL)
672                 goto nlmsg_failure;
673
674         nfmsg = nlmsg_data(nlh);
675         nfmsg->nfgen_family = family;
676         nfmsg->version = NFNETLINK_V0;
677         nfmsg->res_id = 0;
678
679         if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
680             nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
681             nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
682                 goto nla_put_failure;
683
684         nlmsg_end(skb, nlh);
685         return skb->len;
686
687 nlmsg_failure:
688 nla_put_failure:
689         nlmsg_cancel(skb, nlh);
690         return -1;
691 }
692
693 static int nfnl_compat_get_rcu(struct net *net, struct sock *nfnl,
694                                struct sk_buff *skb, const struct nlmsghdr *nlh,
695                                const struct nlattr * const tb[],
696                                struct netlink_ext_ack *extack)
697 {
698         int ret = 0, target;
699         struct nfgenmsg *nfmsg;
700         const char *fmt;
701         const char *name;
702         u32 rev;
703         struct sk_buff *skb2;
704
705         if (tb[NFTA_COMPAT_NAME] == NULL ||
706             tb[NFTA_COMPAT_REV] == NULL ||
707             tb[NFTA_COMPAT_TYPE] == NULL)
708                 return -EINVAL;
709
710         name = nla_data(tb[NFTA_COMPAT_NAME]);
711         rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
712         target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
713
714         nfmsg = nlmsg_data(nlh);
715
716         switch(nfmsg->nfgen_family) {
717         case AF_INET:
718                 fmt = "ipt_%s";
719                 break;
720         case AF_INET6:
721                 fmt = "ip6t_%s";
722                 break;
723         case NFPROTO_BRIDGE:
724                 fmt = "ebt_%s";
725                 break;
726         case NFPROTO_ARP:
727                 fmt = "arpt_%s";
728                 break;
729         default:
730                 pr_err("nft_compat: unsupported protocol %d\n",
731                         nfmsg->nfgen_family);
732                 return -EINVAL;
733         }
734
735         if (!try_module_get(THIS_MODULE))
736                 return -EINVAL;
737
738         rcu_read_unlock();
739         try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
740                                                  rev, target, &ret),
741                                                  fmt, name);
742         if (ret < 0)
743                 goto out_put;
744
745         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
746         if (skb2 == NULL) {
747                 ret = -ENOMEM;
748                 goto out_put;
749         }
750
751         /* include the best revision for this extension in the message */
752         if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
753                                   nlh->nlmsg_seq,
754                                   NFNL_MSG_TYPE(nlh->nlmsg_type),
755                                   NFNL_MSG_COMPAT_GET,
756                                   nfmsg->nfgen_family,
757                                   name, ret, target) <= 0) {
758                 kfree_skb(skb2);
759                 goto out_put;
760         }
761
762         ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
763                                 MSG_DONTWAIT);
764         if (ret > 0)
765                 ret = 0;
766 out_put:
767         rcu_read_lock();
768         module_put(THIS_MODULE);
769         return ret == -EAGAIN ? -ENOBUFS : ret;
770 }
771
772 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
773         [NFTA_COMPAT_NAME]      = { .type = NLA_NUL_STRING,
774                                     .len = NFT_COMPAT_NAME_MAX-1 },
775         [NFTA_COMPAT_REV]       = { .type = NLA_U32 },
776         [NFTA_COMPAT_TYPE]      = { .type = NLA_U32 },
777 };
778
779 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
780         [NFNL_MSG_COMPAT_GET]           = { .call_rcu = nfnl_compat_get_rcu,
781                                             .attr_count = NFTA_COMPAT_MAX,
782                                             .policy = nfnl_compat_policy_get },
783 };
784
785 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
786         .name           = "nft-compat",
787         .subsys_id      = NFNL_SUBSYS_NFT_COMPAT,
788         .cb_count       = NFNL_MSG_COMPAT_MAX,
789         .cb             = nfnl_nft_compat_cb,
790 };
791
792 static bool nft_match_cmp(const struct xt_match *match,
793                           const char *name, u32 rev, u32 family)
794 {
795         return strcmp(match->name, name) == 0 && match->revision == rev &&
796                (match->family == NFPROTO_UNSPEC || match->family == family);
797 }
798
799 static const struct nft_expr_ops *
800 nft_match_select_ops(const struct nft_ctx *ctx,
801                      const struct nlattr * const tb[])
802 {
803         struct nft_compat_net *cn;
804         struct nft_xt *nft_match;
805         struct xt_match *match;
806         unsigned int matchsize;
807         char *mt_name;
808         u32 rev, family;
809         int err;
810
811         if (tb[NFTA_MATCH_NAME] == NULL ||
812             tb[NFTA_MATCH_REV] == NULL ||
813             tb[NFTA_MATCH_INFO] == NULL)
814                 return ERR_PTR(-EINVAL);
815
816         mt_name = nla_data(tb[NFTA_MATCH_NAME]);
817         rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
818         family = ctx->family;
819
820         cn = nft_compat_pernet(ctx->net);
821
822         /* Re-use the existing match if it's already loaded. */
823         list_for_each_entry(nft_match, &cn->nft_match_list, head) {
824                 struct xt_match *match = nft_match->ops.data;
825
826                 if (nft_match_cmp(match, mt_name, rev, family))
827                         return &nft_match->ops;
828         }
829
830         match = xt_request_find_match(family, mt_name, rev);
831         if (IS_ERR(match))
832                 return ERR_PTR(-ENOENT);
833
834         if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
835                 err = -EINVAL;
836                 goto err;
837         }
838
839         /* This is the first time we use this match, allocate operations */
840         nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
841         if (nft_match == NULL) {
842                 err = -ENOMEM;
843                 goto err;
844         }
845
846         refcount_set(&nft_match->refcnt, 0);
847         nft_match->ops.type = &nft_match_type;
848         nft_match->ops.eval = nft_match_eval;
849         nft_match->ops.init = nft_match_init;
850         nft_match->ops.destroy = nft_match_destroy;
851         nft_match->ops.activate = nft_compat_activate_mt;
852         nft_match->ops.deactivate = nft_compat_deactivate;
853         nft_match->ops.dump = nft_match_dump;
854         nft_match->ops.validate = nft_match_validate;
855         nft_match->ops.data = match;
856
857         matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
858         if (matchsize > NFT_MATCH_LARGE_THRESH) {
859                 matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
860
861                 nft_match->ops.eval = nft_match_large_eval;
862                 nft_match->ops.init = nft_match_large_init;
863                 nft_match->ops.destroy = nft_match_large_destroy;
864                 nft_match->ops.dump = nft_match_large_dump;
865         }
866
867         nft_match->ops.size = matchsize;
868
869         nft_match->listcnt = 1;
870         list_add(&nft_match->head, &cn->nft_match_list);
871
872         return &nft_match->ops;
873 err:
874         module_put(match->me);
875         return ERR_PTR(err);
876 }
877
878 static struct nft_expr_type nft_match_type __read_mostly = {
879         .name           = "match",
880         .select_ops     = nft_match_select_ops,
881         .policy         = nft_match_policy,
882         .maxattr        = NFTA_MATCH_MAX,
883         .owner          = THIS_MODULE,
884 };
885
886 static bool nft_target_cmp(const struct xt_target *tg,
887                            const char *name, u32 rev, u32 family)
888 {
889         return strcmp(tg->name, name) == 0 && tg->revision == rev &&
890                (tg->family == NFPROTO_UNSPEC || tg->family == family);
891 }
892
893 static const struct nft_expr_ops *
894 nft_target_select_ops(const struct nft_ctx *ctx,
895                       const struct nlattr * const tb[])
896 {
897         struct nft_compat_net *cn;
898         struct nft_xt *nft_target;
899         struct xt_target *target;
900         char *tg_name;
901         u32 rev, family;
902         int err;
903
904         if (tb[NFTA_TARGET_NAME] == NULL ||
905             tb[NFTA_TARGET_REV] == NULL ||
906             tb[NFTA_TARGET_INFO] == NULL)
907                 return ERR_PTR(-EINVAL);
908
909         tg_name = nla_data(tb[NFTA_TARGET_NAME]);
910         rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
911         family = ctx->family;
912
913         if (strcmp(tg_name, XT_ERROR_TARGET) == 0 ||
914             strcmp(tg_name, XT_STANDARD_TARGET) == 0 ||
915             strcmp(tg_name, "standard") == 0)
916                 return ERR_PTR(-EINVAL);
917
918         cn = nft_compat_pernet(ctx->net);
919         /* Re-use the existing target if it's already loaded. */
920         list_for_each_entry(nft_target, &cn->nft_target_list, head) {
921                 struct xt_target *target = nft_target->ops.data;
922
923                 if (!target->target)
924                         continue;
925
926                 if (nft_target_cmp(target, tg_name, rev, family))
927                         return &nft_target->ops;
928         }
929
930         target = xt_request_find_target(family, tg_name, rev);
931         if (IS_ERR(target))
932                 return ERR_PTR(-ENOENT);
933
934         if (!target->target) {
935                 err = -EINVAL;
936                 goto err;
937         }
938
939         if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
940                 err = -EINVAL;
941                 goto err;
942         }
943
944         /* This is the first time we use this target, allocate operations */
945         nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
946         if (nft_target == NULL) {
947                 err = -ENOMEM;
948                 goto err;
949         }
950
951         refcount_set(&nft_target->refcnt, 0);
952         nft_target->ops.type = &nft_target_type;
953         nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
954         nft_target->ops.init = nft_target_init;
955         nft_target->ops.destroy = nft_target_destroy;
956         nft_target->ops.activate = nft_compat_activate_tg;
957         nft_target->ops.deactivate = nft_compat_deactivate;
958         nft_target->ops.dump = nft_target_dump;
959         nft_target->ops.validate = nft_target_validate;
960         nft_target->ops.data = target;
961
962         if (family == NFPROTO_BRIDGE)
963                 nft_target->ops.eval = nft_target_eval_bridge;
964         else
965                 nft_target->ops.eval = nft_target_eval_xt;
966
967         nft_target->listcnt = 1;
968         list_add(&nft_target->head, &cn->nft_target_list);
969
970         return &nft_target->ops;
971 err:
972         module_put(target->me);
973         return ERR_PTR(err);
974 }
975
976 static struct nft_expr_type nft_target_type __read_mostly = {
977         .name           = "target",
978         .select_ops     = nft_target_select_ops,
979         .policy         = nft_target_policy,
980         .maxattr        = NFTA_TARGET_MAX,
981         .owner          = THIS_MODULE,
982 };
983
984 static int __net_init nft_compat_init_net(struct net *net)
985 {
986         struct nft_compat_net *cn = nft_compat_pernet(net);
987
988         INIT_LIST_HEAD(&cn->nft_target_list);
989         INIT_LIST_HEAD(&cn->nft_match_list);
990
991         return 0;
992 }
993
994 static void __net_exit nft_compat_exit_net(struct net *net)
995 {
996         struct nft_compat_net *cn = nft_compat_pernet(net);
997         struct nft_xt *xt, *next;
998
999         if (list_empty(&cn->nft_match_list) &&
1000             list_empty(&cn->nft_target_list))
1001                 return;
1002
1003         /* If there was an error that caused nft_xt expr to not be initialized
1004          * fully and noone else requested the same expression later, the lists
1005          * contain 0-refcount entries that still hold module reference.
1006          *
1007          * Clean them here.
1008          */
1009         mutex_lock(&net->nft.commit_mutex);
1010         list_for_each_entry_safe(xt, next, &cn->nft_target_list, head) {
1011                 struct xt_target *target = xt->ops.data;
1012
1013                 list_del_init(&xt->head);
1014
1015                 if (refcount_read(&xt->refcnt))
1016                         continue;
1017                 module_put(target->me);
1018                 kfree(xt);
1019         }
1020
1021         list_for_each_entry_safe(xt, next, &cn->nft_match_list, head) {
1022                 struct xt_match *match = xt->ops.data;
1023
1024                 list_del_init(&xt->head);
1025
1026                 if (refcount_read(&xt->refcnt))
1027                         continue;
1028                 module_put(match->me);
1029                 kfree(xt);
1030         }
1031         mutex_unlock(&net->nft.commit_mutex);
1032 }
1033
1034 static struct pernet_operations nft_compat_net_ops = {
1035         .init   = nft_compat_init_net,
1036         .exit   = nft_compat_exit_net,
1037         .id     = &nft_compat_net_id,
1038         .size   = sizeof(struct nft_compat_net),
1039 };
1040
1041 static int __init nft_compat_module_init(void)
1042 {
1043         int ret;
1044
1045         ret = register_pernet_subsys(&nft_compat_net_ops);
1046         if (ret < 0)
1047                 goto err_target;
1048
1049         ret = nft_register_expr(&nft_match_type);
1050         if (ret < 0)
1051                 goto err_pernet;
1052
1053         ret = nft_register_expr(&nft_target_type);
1054         if (ret < 0)
1055                 goto err_match;
1056
1057         ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
1058         if (ret < 0) {
1059                 pr_err("nft_compat: cannot register with nfnetlink.\n");
1060                 goto err_target;
1061         }
1062
1063         return ret;
1064 err_target:
1065         nft_unregister_expr(&nft_target_type);
1066 err_match:
1067         nft_unregister_expr(&nft_match_type);
1068 err_pernet:
1069         unregister_pernet_subsys(&nft_compat_net_ops);
1070         return ret;
1071 }
1072
1073 static void __exit nft_compat_module_exit(void)
1074 {
1075         nfnetlink_subsys_unregister(&nfnl_compat_subsys);
1076         nft_unregister_expr(&nft_target_type);
1077         nft_unregister_expr(&nft_match_type);
1078         unregister_pernet_subsys(&nft_compat_net_ops);
1079 }
1080
1081 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
1082
1083 module_init(nft_compat_module_init);
1084 module_exit(nft_compat_module_exit);
1085
1086 MODULE_LICENSE("GPL");
1087 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
1088 MODULE_ALIAS_NFT_EXPR("match");
1089 MODULE_ALIAS_NFT_EXPR("target");