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