hwmon: (acpi_power_meter) Fix acpi_bus_get_device() return value check
[linux-drm-fsl-dcu.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 <asm/uaccess.h> /* for set_fs */
23 #include <net/netfilter/nf_tables.h>
24
25 union nft_entry {
26         struct ipt_entry e4;
27         struct ip6t_entry e6;
28 };
29
30 static inline void
31 nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
32 {
33         par->target     = xt;
34         par->targinfo   = xt_info;
35         par->hotdrop    = false;
36 }
37
38 static void nft_target_eval(const struct nft_expr *expr,
39                             struct nft_data data[NFT_REG_MAX + 1],
40                             const struct nft_pktinfo *pkt)
41 {
42         void *info = nft_expr_priv(expr);
43         struct xt_target *target = expr->ops->data;
44         struct sk_buff *skb = pkt->skb;
45         int ret;
46
47         nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
48
49         ret = target->target(skb, &pkt->xt);
50
51         if (pkt->xt.hotdrop)
52                 ret = NF_DROP;
53
54         switch(ret) {
55         case XT_CONTINUE:
56                 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
57                 break;
58         default:
59                 data[NFT_REG_VERDICT].verdict = ret;
60                 break;
61         }
62         return;
63 }
64
65 static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
66         [NFTA_TARGET_NAME]      = { .type = NLA_NUL_STRING },
67         [NFTA_TARGET_REV]       = { .type = NLA_U32 },
68         [NFTA_TARGET_INFO]      = { .type = NLA_BINARY },
69 };
70
71 static void
72 nft_target_set_tgchk_param(struct xt_tgchk_param *par,
73                            const struct nft_ctx *ctx,
74                            struct xt_target *target, void *info,
75                            union nft_entry *entry, u8 proto, bool inv)
76 {
77         par->net        = &init_net;
78         par->table      = ctx->table->name;
79         switch (ctx->afi->family) {
80         case AF_INET:
81                 entry->e4.ip.proto = proto;
82                 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
83                 break;
84         case AF_INET6:
85                 entry->e6.ipv6.proto = proto;
86                 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
87                 break;
88         }
89         par->entryinfo  = entry;
90         par->target     = target;
91         par->targinfo   = info;
92         if (ctx->chain->flags & NFT_BASE_CHAIN) {
93                 const struct nft_base_chain *basechain =
94                                                 nft_base_chain(ctx->chain);
95                 const struct nf_hook_ops *ops = &basechain->ops;
96
97                 par->hook_mask = 1 << ops->hooknum;
98         }
99         par->family     = ctx->afi->family;
100 }
101
102 static void target_compat_from_user(struct xt_target *t, void *in, void *out)
103 {
104 #ifdef CONFIG_COMPAT
105         if (t->compat_from_user) {
106                 int pad;
107
108                 t->compat_from_user(out, in);
109                 pad = XT_ALIGN(t->targetsize) - t->targetsize;
110                 if (pad > 0)
111                         memset(out + t->targetsize, 0, pad);
112         } else
113 #endif
114                 memcpy(out, in, XT_ALIGN(t->targetsize));
115 }
116
117 static inline int nft_compat_target_offset(struct xt_target *target)
118 {
119 #ifdef CONFIG_COMPAT
120         return xt_compat_target_offset(target);
121 #else
122         return 0;
123 #endif
124 }
125
126 static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
127         [NFTA_RULE_COMPAT_PROTO]        = { .type = NLA_U32 },
128         [NFTA_RULE_COMPAT_FLAGS]        = { .type = NLA_U32 },
129 };
130
131 static u8 nft_parse_compat(const struct nlattr *attr, bool *inv)
132 {
133         struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
134         u32 flags;
135         int err;
136
137         err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr,
138                                nft_rule_compat_policy);
139         if (err < 0)
140                 return err;
141
142         if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
143                 return -EINVAL;
144
145         flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
146         if (flags & ~NFT_RULE_COMPAT_F_MASK)
147                 return -EINVAL;
148         if (flags & NFT_RULE_COMPAT_F_INV)
149                 *inv = true;
150
151         return ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
152 }
153
154 static int
155 nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
156                 const struct nlattr * const tb[])
157 {
158         void *info = nft_expr_priv(expr);
159         struct xt_target *target = expr->ops->data;
160         struct xt_tgchk_param par;
161         size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
162         u8 proto = 0;
163         bool inv = false;
164         union nft_entry e = {};
165         int ret;
166
167         target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
168
169         if (ctx->nla[NFTA_RULE_COMPAT])
170                 proto = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &inv);
171
172         nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
173
174         ret = xt_check_target(&par, size, proto, inv);
175         if (ret < 0)
176                 goto err;
177
178         /* The standard target cannot be used */
179         if (target->target == NULL) {
180                 ret = -EINVAL;
181                 goto err;
182         }
183
184         return 0;
185 err:
186         module_put(target->me);
187         return ret;
188 }
189
190 static void
191 nft_target_destroy(const struct nft_expr *expr)
192 {
193         struct xt_target *target = expr->ops->data;
194
195         module_put(target->me);
196 }
197
198 static int
199 target_dump_info(struct sk_buff *skb, const struct xt_target *t, const void *in)
200 {
201         int ret;
202
203 #ifdef CONFIG_COMPAT
204         if (t->compat_to_user) {
205                 mm_segment_t old_fs;
206                 void *out;
207
208                 out = kmalloc(XT_ALIGN(t->targetsize), GFP_ATOMIC);
209                 if (out == NULL)
210                         return -ENOMEM;
211
212                 /* We want to reuse existing compat_to_user */
213                 old_fs = get_fs();
214                 set_fs(KERNEL_DS);
215                 t->compat_to_user(out, in);
216                 set_fs(old_fs);
217                 ret = nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(t->targetsize), out);
218                 kfree(out);
219         } else
220 #endif
221                 ret = nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(t->targetsize), in);
222
223         return ret;
224 }
225
226 static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
227 {
228         const struct xt_target *target = expr->ops->data;
229         void *info = nft_expr_priv(expr);
230
231         if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
232             nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
233             target_dump_info(skb, target, info))
234                 goto nla_put_failure;
235
236         return 0;
237
238 nla_put_failure:
239         return -1;
240 }
241
242 static int nft_target_validate(const struct nft_ctx *ctx,
243                                const struct nft_expr *expr,
244                                const struct nft_data **data)
245 {
246         struct xt_target *target = expr->ops->data;
247         unsigned int hook_mask = 0;
248
249         if (ctx->chain->flags & NFT_BASE_CHAIN) {
250                 const struct nft_base_chain *basechain =
251                                                 nft_base_chain(ctx->chain);
252                 const struct nf_hook_ops *ops = &basechain->ops;
253
254                 hook_mask = 1 << ops->hooknum;
255                 if (hook_mask & target->hooks)
256                         return 0;
257
258                 /* This target is being called from an invalid chain */
259                 return -EINVAL;
260         }
261         return 0;
262 }
263
264 static void nft_match_eval(const struct nft_expr *expr,
265                            struct nft_data data[NFT_REG_MAX + 1],
266                            const struct nft_pktinfo *pkt)
267 {
268         void *info = nft_expr_priv(expr);
269         struct xt_match *match = expr->ops->data;
270         struct sk_buff *skb = pkt->skb;
271         bool ret;
272
273         nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
274
275         ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
276
277         if (pkt->xt.hotdrop) {
278                 data[NFT_REG_VERDICT].verdict = NF_DROP;
279                 return;
280         }
281
282         switch(ret) {
283         case true:
284                 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
285                 break;
286         case false:
287                 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
288                 break;
289         }
290 }
291
292 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
293         [NFTA_MATCH_NAME]       = { .type = NLA_NUL_STRING },
294         [NFTA_MATCH_REV]        = { .type = NLA_U32 },
295         [NFTA_MATCH_INFO]       = { .type = NLA_BINARY },
296 };
297
298 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
299 static void
300 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
301                           struct xt_match *match, void *info,
302                           union nft_entry *entry, u8 proto, bool inv)
303 {
304         par->net        = &init_net;
305         par->table      = ctx->table->name;
306         switch (ctx->afi->family) {
307         case AF_INET:
308                 entry->e4.ip.proto = proto;
309                 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
310                 break;
311         case AF_INET6:
312                 entry->e6.ipv6.proto = proto;
313                 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
314                 break;
315         }
316         par->entryinfo  = entry;
317         par->match      = match;
318         par->matchinfo  = info;
319         if (ctx->chain->flags & NFT_BASE_CHAIN) {
320                 const struct nft_base_chain *basechain =
321                                                 nft_base_chain(ctx->chain);
322                 const struct nf_hook_ops *ops = &basechain->ops;
323
324                 par->hook_mask = 1 << ops->hooknum;
325         }
326         par->family     = ctx->afi->family;
327 }
328
329 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
330 {
331 #ifdef CONFIG_COMPAT
332         if (m->compat_from_user) {
333                 int pad;
334
335                 m->compat_from_user(out, in);
336                 pad = XT_ALIGN(m->matchsize) - m->matchsize;
337                 if (pad > 0)
338                         memset(out + m->matchsize, 0, pad);
339         } else
340 #endif
341                 memcpy(out, in, XT_ALIGN(m->matchsize));
342 }
343
344 static int
345 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
346                 const struct nlattr * const tb[])
347 {
348         void *info = nft_expr_priv(expr);
349         struct xt_match *match = expr->ops->data;
350         struct xt_mtchk_param par;
351         size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
352         u8 proto = 0;
353         bool inv = false;
354         union nft_entry e = {};
355         int ret;
356
357         match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
358
359         if (ctx->nla[NFTA_RULE_COMPAT])
360                 proto = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &inv);
361
362         nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
363
364         ret = xt_check_match(&par, size, proto, inv);
365         if (ret < 0)
366                 goto err;
367
368         return 0;
369 err:
370         module_put(match->me);
371         return ret;
372 }
373
374 static void
375 nft_match_destroy(const struct nft_expr *expr)
376 {
377         struct xt_match *match = expr->ops->data;
378
379         module_put(match->me);
380 }
381
382 static int
383 match_dump_info(struct sk_buff *skb, const struct xt_match *m, const void *in)
384 {
385         int ret;
386
387 #ifdef CONFIG_COMPAT
388         if (m->compat_to_user) {
389                 mm_segment_t old_fs;
390                 void *out;
391
392                 out = kmalloc(XT_ALIGN(m->matchsize), GFP_ATOMIC);
393                 if (out == NULL)
394                         return -ENOMEM;
395
396                 /* We want to reuse existing compat_to_user */
397                 old_fs = get_fs();
398                 set_fs(KERNEL_DS);
399                 m->compat_to_user(out, in);
400                 set_fs(old_fs);
401                 ret = nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(m->matchsize), out);
402                 kfree(out);
403         } else
404 #endif
405                 ret = nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(m->matchsize), in);
406
407         return ret;
408 }
409
410 static inline int nft_compat_match_offset(struct xt_match *match)
411 {
412 #ifdef CONFIG_COMPAT
413         return xt_compat_match_offset(match);
414 #else
415         return 0;
416 #endif
417 }
418
419 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
420 {
421         void *info = nft_expr_priv(expr);
422         struct xt_match *match = expr->ops->data;
423
424         if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
425             nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
426             match_dump_info(skb, match, info))
427                 goto nla_put_failure;
428
429         return 0;
430
431 nla_put_failure:
432         return -1;
433 }
434
435 static int nft_match_validate(const struct nft_ctx *ctx,
436                               const struct nft_expr *expr,
437                               const struct nft_data **data)
438 {
439         struct xt_match *match = expr->ops->data;
440         unsigned int hook_mask = 0;
441
442         if (ctx->chain->flags & NFT_BASE_CHAIN) {
443                 const struct nft_base_chain *basechain =
444                                                 nft_base_chain(ctx->chain);
445                 const struct nf_hook_ops *ops = &basechain->ops;
446
447                 hook_mask = 1 << ops->hooknum;
448                 if (hook_mask & match->hooks)
449                         return 0;
450
451                 /* This match is being called from an invalid chain */
452                 return -EINVAL;
453         }
454         return 0;
455 }
456
457 static int
458 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
459                       int event, u16 family, const char *name,
460                       int rev, int target)
461 {
462         struct nlmsghdr *nlh;
463         struct nfgenmsg *nfmsg;
464         unsigned int flags = portid ? NLM_F_MULTI : 0;
465
466         event |= NFNL_SUBSYS_NFT_COMPAT << 8;
467         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
468         if (nlh == NULL)
469                 goto nlmsg_failure;
470
471         nfmsg = nlmsg_data(nlh);
472         nfmsg->nfgen_family = family;
473         nfmsg->version = NFNETLINK_V0;
474         nfmsg->res_id = 0;
475
476         if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
477             nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
478             nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
479                 goto nla_put_failure;
480
481         nlmsg_end(skb, nlh);
482         return skb->len;
483
484 nlmsg_failure:
485 nla_put_failure:
486         nlmsg_cancel(skb, nlh);
487         return -1;
488 }
489
490 static int
491 nfnl_compat_get(struct sock *nfnl, struct sk_buff *skb,
492                 const struct nlmsghdr *nlh, const struct nlattr * const tb[])
493 {
494         int ret = 0, target;
495         struct nfgenmsg *nfmsg;
496         const char *fmt;
497         const char *name;
498         u32 rev;
499         struct sk_buff *skb2;
500
501         if (tb[NFTA_COMPAT_NAME] == NULL ||
502             tb[NFTA_COMPAT_REV] == NULL ||
503             tb[NFTA_COMPAT_TYPE] == NULL)
504                 return -EINVAL;
505
506         name = nla_data(tb[NFTA_COMPAT_NAME]);
507         rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
508         target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
509
510         nfmsg = nlmsg_data(nlh);
511
512         switch(nfmsg->nfgen_family) {
513         case AF_INET:
514                 fmt = "ipt_%s";
515                 break;
516         case AF_INET6:
517                 fmt = "ip6t_%s";
518                 break;
519         default:
520                 pr_err("nft_compat: unsupported protocol %d\n",
521                         nfmsg->nfgen_family);
522                 return -EINVAL;
523         }
524
525         try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
526                                                  rev, target, &ret),
527                                                  fmt, name);
528
529         if (ret < 0)
530                 return ret;
531
532         skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
533         if (skb2 == NULL)
534                 return -ENOMEM;
535
536         /* include the best revision for this extension in the message */
537         if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
538                                   nlh->nlmsg_seq,
539                                   NFNL_MSG_TYPE(nlh->nlmsg_type),
540                                   NFNL_MSG_COMPAT_GET,
541                                   nfmsg->nfgen_family,
542                                   name, ret, target) <= 0) {
543                 kfree_skb(skb2);
544                 return -ENOSPC;
545         }
546
547         ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
548                                 MSG_DONTWAIT);
549         if (ret > 0)
550                 ret = 0;
551
552         return ret == -EAGAIN ? -ENOBUFS : ret;
553 }
554
555 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
556         [NFTA_COMPAT_NAME]      = { .type = NLA_NUL_STRING,
557                                     .len = NFT_COMPAT_NAME_MAX-1 },
558         [NFTA_COMPAT_REV]       = { .type = NLA_U32 },
559         [NFTA_COMPAT_TYPE]      = { .type = NLA_U32 },
560 };
561
562 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
563         [NFNL_MSG_COMPAT_GET]           = { .call = nfnl_compat_get,
564                                             .attr_count = NFTA_COMPAT_MAX,
565                                             .policy = nfnl_compat_policy_get },
566 };
567
568 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
569         .name           = "nft-compat",
570         .subsys_id      = NFNL_SUBSYS_NFT_COMPAT,
571         .cb_count       = NFNL_MSG_COMPAT_MAX,
572         .cb             = nfnl_nft_compat_cb,
573 };
574
575 static LIST_HEAD(nft_match_list);
576
577 struct nft_xt {
578         struct list_head        head;
579         struct nft_expr_ops     ops;
580 };
581
582 static struct nft_expr_type nft_match_type;
583
584 static const struct nft_expr_ops *
585 nft_match_select_ops(const struct nft_ctx *ctx,
586                      const struct nlattr * const tb[])
587 {
588         struct nft_xt *nft_match;
589         struct xt_match *match;
590         char *mt_name;
591         __u32 rev, family;
592
593         if (tb[NFTA_MATCH_NAME] == NULL ||
594             tb[NFTA_MATCH_REV] == NULL ||
595             tb[NFTA_MATCH_INFO] == NULL)
596                 return ERR_PTR(-EINVAL);
597
598         mt_name = nla_data(tb[NFTA_MATCH_NAME]);
599         rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
600         family = ctx->afi->family;
601
602         /* Re-use the existing match if it's already loaded. */
603         list_for_each_entry(nft_match, &nft_match_list, head) {
604                 struct xt_match *match = nft_match->ops.data;
605
606                 if (strcmp(match->name, mt_name) == 0 &&
607                     match->revision == rev && match->family == family)
608                         return &nft_match->ops;
609         }
610
611         match = xt_request_find_match(family, mt_name, rev);
612         if (IS_ERR(match))
613                 return ERR_PTR(-ENOENT);
614
615         /* This is the first time we use this match, allocate operations */
616         nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
617         if (nft_match == NULL)
618                 return ERR_PTR(-ENOMEM);
619
620         nft_match->ops.type = &nft_match_type;
621         nft_match->ops.size = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize) +
622                                             nft_compat_match_offset(match));
623         nft_match->ops.eval = nft_match_eval;
624         nft_match->ops.init = nft_match_init;
625         nft_match->ops.destroy = nft_match_destroy;
626         nft_match->ops.dump = nft_match_dump;
627         nft_match->ops.validate = nft_match_validate;
628         nft_match->ops.data = match;
629
630         list_add(&nft_match->head, &nft_match_list);
631
632         return &nft_match->ops;
633 }
634
635 static void nft_match_release(void)
636 {
637         struct nft_xt *nft_match, *tmp;
638
639         list_for_each_entry_safe(nft_match, tmp, &nft_match_list, head)
640                 kfree(nft_match);
641 }
642
643 static struct nft_expr_type nft_match_type __read_mostly = {
644         .name           = "match",
645         .select_ops     = nft_match_select_ops,
646         .policy         = nft_match_policy,
647         .maxattr        = NFTA_MATCH_MAX,
648         .owner          = THIS_MODULE,
649 };
650
651 static LIST_HEAD(nft_target_list);
652
653 static struct nft_expr_type nft_target_type;
654
655 static const struct nft_expr_ops *
656 nft_target_select_ops(const struct nft_ctx *ctx,
657                       const struct nlattr * const tb[])
658 {
659         struct nft_xt *nft_target;
660         struct xt_target *target;
661         char *tg_name;
662         __u32 rev, family;
663
664         if (tb[NFTA_TARGET_NAME] == NULL ||
665             tb[NFTA_TARGET_REV] == NULL ||
666             tb[NFTA_TARGET_INFO] == NULL)
667                 return ERR_PTR(-EINVAL);
668
669         tg_name = nla_data(tb[NFTA_TARGET_NAME]);
670         rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
671         family = ctx->afi->family;
672
673         /* Re-use the existing target if it's already loaded. */
674         list_for_each_entry(nft_target, &nft_match_list, head) {
675                 struct xt_target *target = nft_target->ops.data;
676
677                 if (strcmp(target->name, tg_name) == 0 &&
678                     target->revision == rev && target->family == family)
679                         return &nft_target->ops;
680         }
681
682         target = xt_request_find_target(family, tg_name, rev);
683         if (IS_ERR(target))
684                 return ERR_PTR(-ENOENT);
685
686         /* This is the first time we use this target, allocate operations */
687         nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
688         if (nft_target == NULL)
689                 return ERR_PTR(-ENOMEM);
690
691         nft_target->ops.type = &nft_target_type;
692         nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize) +
693                                              nft_compat_target_offset(target));
694         nft_target->ops.eval = nft_target_eval;
695         nft_target->ops.init = nft_target_init;
696         nft_target->ops.destroy = nft_target_destroy;
697         nft_target->ops.dump = nft_target_dump;
698         nft_target->ops.validate = nft_target_validate;
699         nft_target->ops.data = target;
700
701         list_add(&nft_target->head, &nft_target_list);
702
703         return &nft_target->ops;
704 }
705
706 static void nft_target_release(void)
707 {
708         struct nft_xt *nft_target, *tmp;
709
710         list_for_each_entry_safe(nft_target, tmp, &nft_target_list, head)
711                 kfree(nft_target);
712 }
713
714 static struct nft_expr_type nft_target_type __read_mostly = {
715         .name           = "target",
716         .select_ops     = nft_target_select_ops,
717         .policy         = nft_target_policy,
718         .maxattr        = NFTA_TARGET_MAX,
719         .owner          = THIS_MODULE,
720 };
721
722 static int __init nft_compat_module_init(void)
723 {
724         int ret;
725
726         ret = nft_register_expr(&nft_match_type);
727         if (ret < 0)
728                 return ret;
729
730         ret = nft_register_expr(&nft_target_type);
731         if (ret < 0)
732                 goto err_match;
733
734         ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
735         if (ret < 0) {
736                 pr_err("nft_compat: cannot register with nfnetlink.\n");
737                 goto err_target;
738         }
739
740         pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
741
742         return ret;
743
744 err_target:
745         nft_unregister_expr(&nft_target_type);
746 err_match:
747         nft_unregister_expr(&nft_match_type);
748         return ret;
749 }
750
751 static void __exit nft_compat_module_exit(void)
752 {
753         nfnetlink_subsys_unregister(&nfnl_compat_subsys);
754         nft_unregister_expr(&nft_target_type);
755         nft_unregister_expr(&nft_match_type);
756         nft_match_release();
757         nft_target_release();
758 }
759
760 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
761
762 module_init(nft_compat_module_init);
763 module_exit(nft_compat_module_exit);
764
765 MODULE_LICENSE("GPL");
766 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
767 MODULE_ALIAS_NFT_EXPR("match");
768 MODULE_ALIAS_NFT_EXPR("target");