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