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