]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/netfilter/nft_compat.c
ipvs: initialize tbl->entries in ip_vs_lblc_init_svc()
[mirror_ubuntu-bionic-kernel.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 xt_tgdtor_param par;
281
282 par.net = ctx->net;
283 par.target = target;
284 par.targinfo = info;
285 par.family = ctx->afi->family;
286 if (par.target->destroy != NULL)
287 par.target->destroy(&par);
288
289 if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
290 module_put(target->me);
291 }
292
293 static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
294 {
295 const struct xt_target *target = expr->ops->data;
296 void *info = nft_expr_priv(expr);
297
298 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
299 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
300 nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(target->targetsize), info))
301 goto nla_put_failure;
302
303 return 0;
304
305 nla_put_failure:
306 return -1;
307 }
308
309 static int nft_target_validate(const struct nft_ctx *ctx,
310 const struct nft_expr *expr,
311 const struct nft_data **data)
312 {
313 struct xt_target *target = expr->ops->data;
314 unsigned int hook_mask = 0;
315 int ret;
316
317 if (nft_is_base_chain(ctx->chain)) {
318 const struct nft_base_chain *basechain =
319 nft_base_chain(ctx->chain);
320 const struct nf_hook_ops *ops = &basechain->ops[0];
321
322 hook_mask = 1 << ops->hooknum;
323 if (target->hooks && !(hook_mask & target->hooks))
324 return -EINVAL;
325
326 ret = nft_compat_chain_validate_dependency(target->table,
327 ctx->chain);
328 if (ret < 0)
329 return ret;
330 }
331 return 0;
332 }
333
334 static void __nft_match_eval(const struct nft_expr *expr,
335 struct nft_regs *regs,
336 const struct nft_pktinfo *pkt,
337 void *info)
338 {
339 struct xt_match *match = expr->ops->data;
340 struct sk_buff *skb = pkt->skb;
341 bool ret;
342
343 nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
344
345 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
346
347 if (pkt->xt.hotdrop) {
348 regs->verdict.code = NF_DROP;
349 return;
350 }
351
352 switch (ret ? 1 : 0) {
353 case 1:
354 regs->verdict.code = NFT_CONTINUE;
355 break;
356 case 0:
357 regs->verdict.code = NFT_BREAK;
358 break;
359 }
360 }
361
362 static void nft_match_large_eval(const struct nft_expr *expr,
363 struct nft_regs *regs,
364 const struct nft_pktinfo *pkt)
365 {
366 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
367
368 __nft_match_eval(expr, regs, pkt, priv->info);
369 }
370
371 static void nft_match_eval(const struct nft_expr *expr,
372 struct nft_regs *regs,
373 const struct nft_pktinfo *pkt)
374 {
375 __nft_match_eval(expr, regs, pkt, nft_expr_priv(expr));
376 }
377
378 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
379 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
380 [NFTA_MATCH_REV] = { .type = NLA_U32 },
381 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
382 };
383
384 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
385 static void
386 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
387 struct xt_match *match, void *info,
388 union nft_entry *entry, u16 proto, bool inv)
389 {
390 par->net = ctx->net;
391 par->table = ctx->table->name;
392 switch (ctx->afi->family) {
393 case AF_INET:
394 entry->e4.ip.proto = proto;
395 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
396 break;
397 case AF_INET6:
398 if (proto)
399 entry->e6.ipv6.flags |= IP6T_F_PROTO;
400
401 entry->e6.ipv6.proto = proto;
402 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
403 break;
404 case NFPROTO_BRIDGE:
405 entry->ebt.ethproto = (__force __be16)proto;
406 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
407 break;
408 case NFPROTO_ARP:
409 break;
410 }
411 par->entryinfo = entry;
412 par->match = match;
413 par->matchinfo = info;
414 if (nft_is_base_chain(ctx->chain)) {
415 const struct nft_base_chain *basechain =
416 nft_base_chain(ctx->chain);
417 const struct nf_hook_ops *ops = &basechain->ops[0];
418
419 par->hook_mask = 1 << ops->hooknum;
420 } else {
421 par->hook_mask = 0;
422 }
423 par->family = ctx->afi->family;
424 par->nft_compat = true;
425 }
426
427 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
428 {
429 int pad;
430
431 memcpy(out, in, m->matchsize);
432 pad = XT_ALIGN(m->matchsize) - m->matchsize;
433 if (pad > 0)
434 memset(out + m->matchsize, 0, pad);
435 }
436
437 static int
438 __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
439 const struct nlattr * const tb[],
440 void *info)
441 {
442 struct xt_match *match = expr->ops->data;
443 struct xt_mtchk_param par;
444 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
445 struct nft_xt *nft_xt;
446 u16 proto = 0;
447 bool inv = false;
448 union nft_entry e = {};
449 int ret;
450
451 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
452
453 if (ctx->nla[NFTA_RULE_COMPAT]) {
454 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
455 if (ret < 0)
456 return ret;
457 }
458
459 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
460
461 ret = xt_check_match(&par, size, proto, inv);
462 if (ret < 0)
463 return ret;
464
465 nft_xt = container_of(expr->ops, struct nft_xt, ops);
466 nft_xt->refcnt++;
467 return 0;
468 }
469
470 static int
471 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
472 const struct nlattr * const tb[])
473 {
474 return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
475 }
476
477 static int
478 nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
479 const struct nlattr * const tb[])
480 {
481 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
482 struct xt_match *m = expr->ops->data;
483 int ret;
484
485 priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL);
486 if (!priv->info)
487 return -ENOMEM;
488
489 ret = __nft_match_init(ctx, expr, tb, priv->info);
490 if (ret)
491 kfree(priv->info);
492 return ret;
493 }
494
495 static void
496 __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
497 void *info)
498 {
499 struct xt_match *match = expr->ops->data;
500 struct xt_mtdtor_param par;
501
502 par.net = ctx->net;
503 par.match = match;
504 par.matchinfo = info;
505 par.family = ctx->afi->family;
506 if (par.match->destroy != NULL)
507 par.match->destroy(&par);
508
509 if (nft_xt_put(container_of(expr->ops, struct nft_xt, ops)))
510 module_put(match->me);
511 }
512
513 static void
514 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
515 {
516 __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
517 }
518
519 static void
520 nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
521 {
522 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
523
524 __nft_match_destroy(ctx, expr, priv->info);
525 kfree(priv->info);
526 }
527
528 static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
529 void *info)
530 {
531 struct xt_match *match = expr->ops->data;
532
533 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
534 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
535 nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(match->matchsize), info))
536 goto nla_put_failure;
537
538 return 0;
539
540 nla_put_failure:
541 return -1;
542 }
543
544 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
545 {
546 return __nft_match_dump(skb, expr, nft_expr_priv(expr));
547 }
548
549 static int nft_match_large_dump(struct sk_buff *skb, const struct nft_expr *e)
550 {
551 struct nft_xt_match_priv *priv = nft_expr_priv(e);
552
553 return __nft_match_dump(skb, e, priv->info);
554 }
555
556 static int nft_match_validate(const struct nft_ctx *ctx,
557 const struct nft_expr *expr,
558 const struct nft_data **data)
559 {
560 struct xt_match *match = expr->ops->data;
561 unsigned int hook_mask = 0;
562 int ret;
563
564 if (nft_is_base_chain(ctx->chain)) {
565 const struct nft_base_chain *basechain =
566 nft_base_chain(ctx->chain);
567 const struct nf_hook_ops *ops = &basechain->ops[0];
568
569 hook_mask = 1 << ops->hooknum;
570 if (match->hooks && !(hook_mask & match->hooks))
571 return -EINVAL;
572
573 ret = nft_compat_chain_validate_dependency(match->table,
574 ctx->chain);
575 if (ret < 0)
576 return ret;
577 }
578 return 0;
579 }
580
581 static int
582 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
583 int event, u16 family, const char *name,
584 int rev, int target)
585 {
586 struct nlmsghdr *nlh;
587 struct nfgenmsg *nfmsg;
588 unsigned int flags = portid ? NLM_F_MULTI : 0;
589
590 event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
591 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
592 if (nlh == NULL)
593 goto nlmsg_failure;
594
595 nfmsg = nlmsg_data(nlh);
596 nfmsg->nfgen_family = family;
597 nfmsg->version = NFNETLINK_V0;
598 nfmsg->res_id = 0;
599
600 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
601 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
602 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
603 goto nla_put_failure;
604
605 nlmsg_end(skb, nlh);
606 return skb->len;
607
608 nlmsg_failure:
609 nla_put_failure:
610 nlmsg_cancel(skb, nlh);
611 return -1;
612 }
613
614 static int nfnl_compat_get(struct net *net, struct sock *nfnl,
615 struct sk_buff *skb, const struct nlmsghdr *nlh,
616 const struct nlattr * const tb[],
617 struct netlink_ext_ack *extack)
618 {
619 int ret = 0, target;
620 struct nfgenmsg *nfmsg;
621 const char *fmt;
622 const char *name;
623 u32 rev;
624 struct sk_buff *skb2;
625
626 if (tb[NFTA_COMPAT_NAME] == NULL ||
627 tb[NFTA_COMPAT_REV] == NULL ||
628 tb[NFTA_COMPAT_TYPE] == NULL)
629 return -EINVAL;
630
631 name = nla_data(tb[NFTA_COMPAT_NAME]);
632 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
633 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
634
635 nfmsg = nlmsg_data(nlh);
636
637 switch(nfmsg->nfgen_family) {
638 case AF_INET:
639 fmt = "ipt_%s";
640 break;
641 case AF_INET6:
642 fmt = "ip6t_%s";
643 break;
644 case NFPROTO_BRIDGE:
645 fmt = "ebt_%s";
646 break;
647 case NFPROTO_ARP:
648 fmt = "arpt_%s";
649 break;
650 default:
651 pr_err("nft_compat: unsupported protocol %d\n",
652 nfmsg->nfgen_family);
653 return -EINVAL;
654 }
655
656 try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
657 rev, target, &ret),
658 fmt, name);
659
660 if (ret < 0)
661 return ret;
662
663 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
664 if (skb2 == NULL)
665 return -ENOMEM;
666
667 /* include the best revision for this extension in the message */
668 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
669 nlh->nlmsg_seq,
670 NFNL_MSG_TYPE(nlh->nlmsg_type),
671 NFNL_MSG_COMPAT_GET,
672 nfmsg->nfgen_family,
673 name, ret, target) <= 0) {
674 kfree_skb(skb2);
675 return -ENOSPC;
676 }
677
678 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
679 MSG_DONTWAIT);
680 if (ret > 0)
681 ret = 0;
682
683 return ret == -EAGAIN ? -ENOBUFS : ret;
684 }
685
686 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
687 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
688 .len = NFT_COMPAT_NAME_MAX-1 },
689 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
690 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
691 };
692
693 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
694 [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get,
695 .attr_count = NFTA_COMPAT_MAX,
696 .policy = nfnl_compat_policy_get },
697 };
698
699 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
700 .name = "nft-compat",
701 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
702 .cb_count = NFNL_MSG_COMPAT_MAX,
703 .cb = nfnl_nft_compat_cb,
704 };
705
706 static LIST_HEAD(nft_match_list);
707
708 static struct nft_expr_type nft_match_type;
709
710 static bool nft_match_cmp(const struct xt_match *match,
711 const char *name, u32 rev, u32 family)
712 {
713 return strcmp(match->name, name) == 0 && match->revision == rev &&
714 (match->family == NFPROTO_UNSPEC || match->family == family);
715 }
716
717 static const struct nft_expr_ops *
718 nft_match_select_ops(const struct nft_ctx *ctx,
719 const struct nlattr * const tb[])
720 {
721 struct nft_xt *nft_match;
722 struct xt_match *match;
723 unsigned int matchsize;
724 char *mt_name;
725 u32 rev, family;
726 int err;
727
728 if (tb[NFTA_MATCH_NAME] == NULL ||
729 tb[NFTA_MATCH_REV] == NULL ||
730 tb[NFTA_MATCH_INFO] == NULL)
731 return ERR_PTR(-EINVAL);
732
733 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
734 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
735 family = ctx->afi->family;
736
737 /* Re-use the existing match if it's already loaded. */
738 list_for_each_entry(nft_match, &nft_match_list, head) {
739 struct xt_match *match = nft_match->ops.data;
740
741 if (nft_match_cmp(match, mt_name, rev, family))
742 return &nft_match->ops;
743 }
744
745 match = xt_request_find_match(family, mt_name, rev);
746 if (IS_ERR(match))
747 return ERR_PTR(-ENOENT);
748
749 if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
750 err = -EINVAL;
751 goto err;
752 }
753
754 /* This is the first time we use this match, allocate operations */
755 nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
756 if (nft_match == NULL) {
757 err = -ENOMEM;
758 goto err;
759 }
760
761 nft_match->refcnt = 0;
762 nft_match->ops.type = &nft_match_type;
763 nft_match->ops.eval = nft_match_eval;
764 nft_match->ops.init = nft_match_init;
765 nft_match->ops.destroy = nft_match_destroy;
766 nft_match->ops.dump = nft_match_dump;
767 nft_match->ops.validate = nft_match_validate;
768 nft_match->ops.data = match;
769
770 matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
771 if (matchsize > NFT_MATCH_LARGE_THRESH) {
772 matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
773
774 nft_match->ops.eval = nft_match_large_eval;
775 nft_match->ops.init = nft_match_large_init;
776 nft_match->ops.destroy = nft_match_large_destroy;
777 nft_match->ops.dump = nft_match_large_dump;
778 }
779
780 nft_match->ops.size = matchsize;
781
782 list_add(&nft_match->head, &nft_match_list);
783
784 return &nft_match->ops;
785 err:
786 module_put(match->me);
787 return ERR_PTR(err);
788 }
789
790 static struct nft_expr_type nft_match_type __read_mostly = {
791 .name = "match",
792 .select_ops = nft_match_select_ops,
793 .policy = nft_match_policy,
794 .maxattr = NFTA_MATCH_MAX,
795 .owner = THIS_MODULE,
796 };
797
798 static LIST_HEAD(nft_target_list);
799
800 static struct nft_expr_type nft_target_type;
801
802 static bool nft_target_cmp(const struct xt_target *tg,
803 const char *name, u32 rev, u32 family)
804 {
805 return strcmp(tg->name, name) == 0 && tg->revision == rev &&
806 (tg->family == NFPROTO_UNSPEC || tg->family == family);
807 }
808
809 static const struct nft_expr_ops *
810 nft_target_select_ops(const struct nft_ctx *ctx,
811 const struct nlattr * const tb[])
812 {
813 struct nft_xt *nft_target;
814 struct xt_target *target;
815 char *tg_name;
816 u32 rev, family;
817 int err;
818
819 if (tb[NFTA_TARGET_NAME] == NULL ||
820 tb[NFTA_TARGET_REV] == NULL ||
821 tb[NFTA_TARGET_INFO] == NULL)
822 return ERR_PTR(-EINVAL);
823
824 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
825 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
826 family = ctx->afi->family;
827
828 /* Re-use the existing target if it's already loaded. */
829 list_for_each_entry(nft_target, &nft_target_list, head) {
830 struct xt_target *target = nft_target->ops.data;
831
832 if (nft_target_cmp(target, tg_name, rev, family))
833 return &nft_target->ops;
834 }
835
836 target = xt_request_find_target(family, tg_name, rev);
837 if (IS_ERR(target))
838 return ERR_PTR(-ENOENT);
839
840 if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
841 err = -EINVAL;
842 goto err;
843 }
844
845 /* This is the first time we use this target, allocate operations */
846 nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
847 if (nft_target == NULL) {
848 err = -ENOMEM;
849 goto err;
850 }
851
852 nft_target->refcnt = 0;
853 nft_target->ops.type = &nft_target_type;
854 nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
855 nft_target->ops.init = nft_target_init;
856 nft_target->ops.destroy = nft_target_destroy;
857 nft_target->ops.dump = nft_target_dump;
858 nft_target->ops.validate = nft_target_validate;
859 nft_target->ops.data = target;
860
861 if (family == NFPROTO_BRIDGE)
862 nft_target->ops.eval = nft_target_eval_bridge;
863 else
864 nft_target->ops.eval = nft_target_eval_xt;
865
866 list_add(&nft_target->head, &nft_target_list);
867
868 return &nft_target->ops;
869 err:
870 module_put(target->me);
871 return ERR_PTR(err);
872 }
873
874 static struct nft_expr_type nft_target_type __read_mostly = {
875 .name = "target",
876 .select_ops = nft_target_select_ops,
877 .policy = nft_target_policy,
878 .maxattr = NFTA_TARGET_MAX,
879 .owner = THIS_MODULE,
880 };
881
882 static int __init nft_compat_module_init(void)
883 {
884 int ret;
885
886 ret = nft_register_expr(&nft_match_type);
887 if (ret < 0)
888 return ret;
889
890 ret = nft_register_expr(&nft_target_type);
891 if (ret < 0)
892 goto err_match;
893
894 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
895 if (ret < 0) {
896 pr_err("nft_compat: cannot register with nfnetlink.\n");
897 goto err_target;
898 }
899
900 pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
901
902 return ret;
903
904 err_target:
905 nft_unregister_expr(&nft_target_type);
906 err_match:
907 nft_unregister_expr(&nft_match_type);
908 return ret;
909 }
910
911 static void __exit nft_compat_module_exit(void)
912 {
913 struct nft_xt *xt, *next;
914
915 /* list should be empty here, it can be non-empty only in case there
916 * was an error that caused nft_xt expr to not be initialized fully
917 * and noone else requested the same expression later.
918 *
919 * In this case, the lists contain 0-refcount entries that still
920 * hold module reference.
921 */
922 list_for_each_entry_safe(xt, next, &nft_target_list, head) {
923 struct xt_target *target = xt->ops.data;
924
925 if (WARN_ON_ONCE(xt->refcnt))
926 continue;
927 module_put(target->me);
928 kfree(xt);
929 }
930
931 list_for_each_entry_safe(xt, next, &nft_match_list, head) {
932 struct xt_match *match = xt->ops.data;
933
934 if (WARN_ON_ONCE(xt->refcnt))
935 continue;
936 module_put(match->me);
937 kfree(xt);
938 }
939 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
940 nft_unregister_expr(&nft_target_type);
941 nft_unregister_expr(&nft_match_type);
942 }
943
944 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
945
946 module_init(nft_compat_module_init);
947 module_exit(nft_compat_module_exit);
948
949 MODULE_LICENSE("GPL");
950 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
951 MODULE_ALIAS_NFT_EXPR("match");
952 MODULE_ALIAS_NFT_EXPR("target");