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