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