• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
nft_compat_chain_validate_dependency(const struct nft_ctx * ctx,const char * tablename)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
nft_compat_set_par(struct xt_action_param * par,void * xt,const void * xt_info)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 
nft_target_eval_xt(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)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 
nft_target_eval_bridge(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)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
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)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 
target_compat_from_user(struct xt_target * t,void * in,void * out)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 
nft_parse_compat(const struct nlattr * attr,u16 * proto,bool * inv)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 l4proto;
196 	u32 flags;
197 	int err;
198 
199 	err = nla_parse_nested_deprecated(tb, NFTA_RULE_COMPAT_MAX, attr,
200 					  nft_rule_compat_policy, NULL);
201 	if (err < 0)
202 		return err;
203 
204 	if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
205 		return -EINVAL;
206 
207 	flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
208 	if (flags & NFT_RULE_COMPAT_F_UNUSED ||
209 	    flags & ~NFT_RULE_COMPAT_F_MASK)
210 		return -EINVAL;
211 	if (flags & NFT_RULE_COMPAT_F_INV)
212 		*inv = true;
213 
214 	l4proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
215 	if (l4proto > U16_MAX)
216 		return -EINVAL;
217 
218 	*proto = l4proto;
219 
220 	return 0;
221 }
222 
nft_compat_wait_for_destructors(void)223 static void nft_compat_wait_for_destructors(void)
224 {
225 	/* xtables matches or targets can have side effects, e.g.
226 	 * creation/destruction of /proc files.
227 	 * The xt ->destroy functions are run asynchronously from
228 	 * work queue.  If we have pending invocations we thus
229 	 * need to wait for those to finish.
230 	 */
231 	nf_tables_trans_destroy_flush_work();
232 }
233 
234 static int
nft_target_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])235 nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
236 		const struct nlattr * const tb[])
237 {
238 	void *info = nft_expr_priv(expr);
239 	struct xt_target *target = expr->ops->data;
240 	struct xt_tgchk_param par;
241 	size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
242 	u16 proto = 0;
243 	bool inv = false;
244 	union nft_entry e = {};
245 	int ret;
246 
247 	target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
248 
249 	if (ctx->nla[NFTA_RULE_COMPAT]) {
250 		ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
251 		if (ret < 0)
252 			return ret;
253 	}
254 
255 	nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
256 
257 	nft_compat_wait_for_destructors();
258 
259 	ret = xt_check_target(&par, size, proto, inv);
260 	if (ret < 0)
261 		return ret;
262 
263 	/* The standard target cannot be used */
264 	if (!target->target)
265 		return -EINVAL;
266 
267 	return 0;
268 }
269 
__nft_mt_tg_destroy(struct module * me,const struct nft_expr * expr)270 static void __nft_mt_tg_destroy(struct module *me, const struct nft_expr *expr)
271 {
272 	module_put(me);
273 	kfree(expr->ops);
274 }
275 
276 static void
nft_target_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)277 nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
278 {
279 	struct xt_target *target = expr->ops->data;
280 	void *info = nft_expr_priv(expr);
281 	struct module *me = target->me;
282 	struct xt_tgdtor_param par;
283 
284 	par.net = ctx->net;
285 	par.target = target;
286 	par.targinfo = info;
287 	par.family = ctx->family;
288 	if (par.target->destroy != NULL)
289 		par.target->destroy(&par);
290 
291 	__nft_mt_tg_destroy(me, expr);
292 }
293 
nft_extension_dump_info(struct sk_buff * skb,int attr,const void * info,unsigned int size,unsigned int user_size)294 static int nft_extension_dump_info(struct sk_buff *skb, int attr,
295 				   const void *info,
296 				   unsigned int size, unsigned int user_size)
297 {
298 	unsigned int info_size, aligned_size = XT_ALIGN(size);
299 	struct nlattr *nla;
300 
301 	nla = nla_reserve(skb, attr, aligned_size);
302 	if (!nla)
303 		return -1;
304 
305 	info_size = user_size ? : size;
306 	memcpy(nla_data(nla), info, info_size);
307 	memset(nla_data(nla) + info_size, 0, aligned_size - info_size);
308 
309 	return 0;
310 }
311 
nft_target_dump(struct sk_buff * skb,const struct nft_expr * expr)312 static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
313 {
314 	const struct xt_target *target = expr->ops->data;
315 	void *info = nft_expr_priv(expr);
316 
317 	if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
318 	    nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
319 	    nft_extension_dump_info(skb, NFTA_TARGET_INFO, info,
320 				    target->targetsize, target->usersize))
321 		goto nla_put_failure;
322 
323 	return 0;
324 
325 nla_put_failure:
326 	return -1;
327 }
328 
nft_target_validate(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nft_data ** data)329 static int nft_target_validate(const struct nft_ctx *ctx,
330 			       const struct nft_expr *expr,
331 			       const struct nft_data **data)
332 {
333 	struct xt_target *target = expr->ops->data;
334 	unsigned int hook_mask = 0;
335 	int ret;
336 
337 	if (ctx->family != NFPROTO_IPV4 &&
338 	    ctx->family != NFPROTO_IPV6 &&
339 	    ctx->family != NFPROTO_INET &&
340 	    ctx->family != NFPROTO_BRIDGE &&
341 	    ctx->family != NFPROTO_ARP)
342 		return -EOPNOTSUPP;
343 
344 	ret = nft_chain_validate_hooks(ctx->chain,
345 				       (1 << NF_INET_PRE_ROUTING) |
346 				       (1 << NF_INET_LOCAL_IN) |
347 				       (1 << NF_INET_FORWARD) |
348 				       (1 << NF_INET_LOCAL_OUT) |
349 				       (1 << NF_INET_POST_ROUTING));
350 	if (ret)
351 		return ret;
352 
353 	if (nft_is_base_chain(ctx->chain)) {
354 		const struct nft_base_chain *basechain =
355 						nft_base_chain(ctx->chain);
356 		const struct nf_hook_ops *ops = &basechain->ops;
357 
358 		hook_mask = 1 << ops->hooknum;
359 		if (target->hooks && !(hook_mask & target->hooks))
360 			return -EINVAL;
361 
362 		ret = nft_compat_chain_validate_dependency(ctx, target->table);
363 		if (ret < 0)
364 			return ret;
365 	}
366 	return 0;
367 }
368 
__nft_match_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt,void * info)369 static void __nft_match_eval(const struct nft_expr *expr,
370 			     struct nft_regs *regs,
371 			     const struct nft_pktinfo *pkt,
372 			     void *info)
373 {
374 	struct xt_match *match = expr->ops->data;
375 	struct sk_buff *skb = pkt->skb;
376 	bool ret;
377 
378 	nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
379 
380 	ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
381 
382 	if (pkt->xt.hotdrop) {
383 		regs->verdict.code = NF_DROP;
384 		return;
385 	}
386 
387 	switch (ret ? 1 : 0) {
388 	case 1:
389 		regs->verdict.code = NFT_CONTINUE;
390 		break;
391 	case 0:
392 		regs->verdict.code = NFT_BREAK;
393 		break;
394 	}
395 }
396 
nft_match_large_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)397 static void nft_match_large_eval(const struct nft_expr *expr,
398 				 struct nft_regs *regs,
399 				 const struct nft_pktinfo *pkt)
400 {
401 	struct nft_xt_match_priv *priv = nft_expr_priv(expr);
402 
403 	__nft_match_eval(expr, regs, pkt, priv->info);
404 }
405 
nft_match_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)406 static void nft_match_eval(const struct nft_expr *expr,
407 			   struct nft_regs *regs,
408 			   const struct nft_pktinfo *pkt)
409 {
410 	__nft_match_eval(expr, regs, pkt, nft_expr_priv(expr));
411 }
412 
413 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
414 	[NFTA_MATCH_NAME]	= { .type = NLA_NUL_STRING },
415 	[NFTA_MATCH_REV]	= { .type = NLA_U32 },
416 	[NFTA_MATCH_INFO]	= { .type = NLA_BINARY },
417 };
418 
419 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
420 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)421 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
422 			  struct xt_match *match, void *info,
423 			  union nft_entry *entry, u16 proto, bool inv)
424 {
425 	par->net	= ctx->net;
426 	par->table	= ctx->table->name;
427 	switch (ctx->family) {
428 	case AF_INET:
429 		entry->e4.ip.proto = proto;
430 		entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
431 		break;
432 	case AF_INET6:
433 		if (proto)
434 			entry->e6.ipv6.flags |= IP6T_F_PROTO;
435 
436 		entry->e6.ipv6.proto = proto;
437 		entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
438 		break;
439 	case NFPROTO_BRIDGE:
440 		entry->ebt.ethproto = (__force __be16)proto;
441 		entry->ebt.invflags = inv ? EBT_IPROTO : 0;
442 		break;
443 	case NFPROTO_ARP:
444 		break;
445 	}
446 	par->entryinfo	= entry;
447 	par->match	= match;
448 	par->matchinfo	= info;
449 	if (nft_is_base_chain(ctx->chain)) {
450 		const struct nft_base_chain *basechain =
451 						nft_base_chain(ctx->chain);
452 		const struct nf_hook_ops *ops = &basechain->ops;
453 
454 		par->hook_mask = 1 << ops->hooknum;
455 	} else {
456 		par->hook_mask = 0;
457 	}
458 	par->family	= ctx->family;
459 	par->nft_compat = true;
460 }
461 
match_compat_from_user(struct xt_match * m,void * in,void * out)462 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
463 {
464 	int pad;
465 
466 	memcpy(out, in, m->matchsize);
467 	pad = XT_ALIGN(m->matchsize) - m->matchsize;
468 	if (pad > 0)
469 		memset(out + m->matchsize, 0, pad);
470 }
471 
472 static int
__nft_match_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[],void * info)473 __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
474 		 const struct nlattr * const tb[],
475 		 void *info)
476 {
477 	struct xt_match *match = expr->ops->data;
478 	struct xt_mtchk_param par;
479 	size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
480 	u16 proto = 0;
481 	bool inv = false;
482 	union nft_entry e = {};
483 	int ret;
484 
485 	match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
486 
487 	if (ctx->nla[NFTA_RULE_COMPAT]) {
488 		ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
489 		if (ret < 0)
490 			return ret;
491 	}
492 
493 	nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
494 
495 	nft_compat_wait_for_destructors();
496 
497 	return xt_check_match(&par, size, proto, inv);
498 }
499 
500 static int
nft_match_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])501 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
502 	       const struct nlattr * const tb[])
503 {
504 	return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
505 }
506 
507 static int
nft_match_large_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])508 nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
509 		     const struct nlattr * const tb[])
510 {
511 	struct nft_xt_match_priv *priv = nft_expr_priv(expr);
512 	struct xt_match *m = expr->ops->data;
513 	int ret;
514 
515 	priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL);
516 	if (!priv->info)
517 		return -ENOMEM;
518 
519 	ret = __nft_match_init(ctx, expr, tb, priv->info);
520 	if (ret)
521 		kfree(priv->info);
522 	return ret;
523 }
524 
525 static void
__nft_match_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr,void * info)526 __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
527 		    void *info)
528 {
529 	struct xt_match *match = expr->ops->data;
530 	struct module *me = match->me;
531 	struct xt_mtdtor_param par;
532 
533 	par.net = ctx->net;
534 	par.match = match;
535 	par.matchinfo = info;
536 	par.family = ctx->family;
537 	if (par.match->destroy != NULL)
538 		par.match->destroy(&par);
539 
540 	__nft_mt_tg_destroy(me, expr);
541 }
542 
543 static void
nft_match_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)544 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
545 {
546 	__nft_match_destroy(ctx, expr, nft_expr_priv(expr));
547 }
548 
549 static void
nft_match_large_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)550 nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
551 {
552 	struct nft_xt_match_priv *priv = nft_expr_priv(expr);
553 
554 	__nft_match_destroy(ctx, expr, priv->info);
555 	kfree(priv->info);
556 }
557 
__nft_match_dump(struct sk_buff * skb,const struct nft_expr * expr,void * info)558 static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
559 			    void *info)
560 {
561 	struct xt_match *match = expr->ops->data;
562 
563 	if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
564 	    nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
565 	    nft_extension_dump_info(skb, NFTA_MATCH_INFO, info,
566 				    match->matchsize, match->usersize))
567 		goto nla_put_failure;
568 
569 	return 0;
570 
571 nla_put_failure:
572 	return -1;
573 }
574 
nft_match_dump(struct sk_buff * skb,const struct nft_expr * expr)575 static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
576 {
577 	return __nft_match_dump(skb, expr, nft_expr_priv(expr));
578 }
579 
nft_match_large_dump(struct sk_buff * skb,const struct nft_expr * e)580 static int nft_match_large_dump(struct sk_buff *skb, const struct nft_expr *e)
581 {
582 	struct nft_xt_match_priv *priv = nft_expr_priv(e);
583 
584 	return __nft_match_dump(skb, e, priv->info);
585 }
586 
nft_match_validate(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nft_data ** data)587 static int nft_match_validate(const struct nft_ctx *ctx,
588 			      const struct nft_expr *expr,
589 			      const struct nft_data **data)
590 {
591 	struct xt_match *match = expr->ops->data;
592 	unsigned int hook_mask = 0;
593 	int ret;
594 
595 	if (ctx->family != NFPROTO_IPV4 &&
596 	    ctx->family != NFPROTO_IPV6 &&
597 	    ctx->family != NFPROTO_INET &&
598 	    ctx->family != NFPROTO_BRIDGE &&
599 	    ctx->family != NFPROTO_ARP)
600 		return -EOPNOTSUPP;
601 
602 	ret = nft_chain_validate_hooks(ctx->chain,
603 				       (1 << NF_INET_PRE_ROUTING) |
604 				       (1 << NF_INET_LOCAL_IN) |
605 				       (1 << NF_INET_FORWARD) |
606 				       (1 << NF_INET_LOCAL_OUT) |
607 				       (1 << NF_INET_POST_ROUTING));
608 	if (ret)
609 		return ret;
610 
611 	if (nft_is_base_chain(ctx->chain)) {
612 		const struct nft_base_chain *basechain =
613 						nft_base_chain(ctx->chain);
614 		const struct nf_hook_ops *ops = &basechain->ops;
615 
616 		hook_mask = 1 << ops->hooknum;
617 		if (match->hooks && !(hook_mask & match->hooks))
618 			return -EINVAL;
619 
620 		ret = nft_compat_chain_validate_dependency(ctx, match->table);
621 		if (ret < 0)
622 			return ret;
623 	}
624 	return 0;
625 }
626 
627 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)628 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
629 		      int event, u16 family, const char *name,
630 		      int rev, int target)
631 {
632 	struct nlmsghdr *nlh;
633 	unsigned int flags = portid ? NLM_F_MULTI : 0;
634 
635 	event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
636 	nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
637 			   NFNETLINK_V0, 0);
638 	if (!nlh)
639 		goto nlmsg_failure;
640 
641 	if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
642 	    nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
643 	    nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
644 		goto nla_put_failure;
645 
646 	nlmsg_end(skb, nlh);
647 	return skb->len;
648 
649 nlmsg_failure:
650 nla_put_failure:
651 	nlmsg_cancel(skb, nlh);
652 	return -1;
653 }
654 
nfnl_compat_get_rcu(struct net * net,struct sock * nfnl,struct sk_buff * skb,const struct nlmsghdr * nlh,const struct nlattr * const tb[],struct netlink_ext_ack * extack)655 static int nfnl_compat_get_rcu(struct net *net, struct sock *nfnl,
656 			       struct sk_buff *skb, const struct nlmsghdr *nlh,
657 			       const struct nlattr * const tb[],
658 			       struct netlink_ext_ack *extack)
659 {
660 	int ret = 0, target;
661 	struct nfgenmsg *nfmsg;
662 	const char *fmt;
663 	const char *name;
664 	u32 rev;
665 	struct sk_buff *skb2;
666 
667 	if (tb[NFTA_COMPAT_NAME] == NULL ||
668 	    tb[NFTA_COMPAT_REV] == NULL ||
669 	    tb[NFTA_COMPAT_TYPE] == NULL)
670 		return -EINVAL;
671 
672 	name = nla_data(tb[NFTA_COMPAT_NAME]);
673 	rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
674 	target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
675 
676 	nfmsg = nlmsg_data(nlh);
677 
678 	switch(nfmsg->nfgen_family) {
679 	case AF_INET:
680 		fmt = "ipt_%s";
681 		break;
682 	case AF_INET6:
683 		fmt = "ip6t_%s";
684 		break;
685 	case NFPROTO_BRIDGE:
686 		fmt = "ebt_%s";
687 		break;
688 	case NFPROTO_ARP:
689 		fmt = "arpt_%s";
690 		break;
691 	default:
692 		pr_err("nft_compat: unsupported protocol %d\n",
693 			nfmsg->nfgen_family);
694 		return -EINVAL;
695 	}
696 
697 	if (!try_module_get(THIS_MODULE))
698 		return -EINVAL;
699 
700 	rcu_read_unlock();
701 	try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
702 						 rev, target, &ret),
703 						 fmt, name);
704 	if (ret < 0)
705 		goto out_put;
706 
707 	skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
708 	if (skb2 == NULL) {
709 		ret = -ENOMEM;
710 		goto out_put;
711 	}
712 
713 	/* include the best revision for this extension in the message */
714 	if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
715 				  nlh->nlmsg_seq,
716 				  NFNL_MSG_TYPE(nlh->nlmsg_type),
717 				  NFNL_MSG_COMPAT_GET,
718 				  nfmsg->nfgen_family,
719 				  name, ret, target) <= 0) {
720 		kfree_skb(skb2);
721 		goto out_put;
722 	}
723 
724 	ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
725 				MSG_DONTWAIT);
726 	if (ret > 0)
727 		ret = 0;
728 out_put:
729 	rcu_read_lock();
730 	module_put(THIS_MODULE);
731 	return ret == -EAGAIN ? -ENOBUFS : ret;
732 }
733 
734 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
735 	[NFTA_COMPAT_NAME]	= { .type = NLA_NUL_STRING,
736 				    .len = NFT_COMPAT_NAME_MAX-1 },
737 	[NFTA_COMPAT_REV]	= { .type = NLA_U32 },
738 	[NFTA_COMPAT_TYPE]	= { .type = NLA_U32 },
739 };
740 
741 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
742 	[NFNL_MSG_COMPAT_GET]		= { .call_rcu = nfnl_compat_get_rcu,
743 					    .attr_count = NFTA_COMPAT_MAX,
744 					    .policy = nfnl_compat_policy_get },
745 };
746 
747 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
748 	.name		= "nft-compat",
749 	.subsys_id	= NFNL_SUBSYS_NFT_COMPAT,
750 	.cb_count	= NFNL_MSG_COMPAT_MAX,
751 	.cb		= nfnl_nft_compat_cb,
752 };
753 
754 static struct nft_expr_type nft_match_type;
755 
756 static const struct nft_expr_ops *
nft_match_select_ops(const struct nft_ctx * ctx,const struct nlattr * const tb[])757 nft_match_select_ops(const struct nft_ctx *ctx,
758 		     const struct nlattr * const tb[])
759 {
760 	struct nft_expr_ops *ops;
761 	struct xt_match *match;
762 	unsigned int matchsize;
763 	char *mt_name;
764 	u32 rev, family;
765 	int err;
766 
767 	if (tb[NFTA_MATCH_NAME] == NULL ||
768 	    tb[NFTA_MATCH_REV] == NULL ||
769 	    tb[NFTA_MATCH_INFO] == NULL)
770 		return ERR_PTR(-EINVAL);
771 
772 	mt_name = nla_data(tb[NFTA_MATCH_NAME]);
773 	rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
774 	family = ctx->family;
775 
776 	match = xt_request_find_match(family, mt_name, rev);
777 	if (IS_ERR(match))
778 		return ERR_PTR(-ENOENT);
779 
780 	if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
781 		err = -EINVAL;
782 		goto err;
783 	}
784 
785 	ops = kzalloc(sizeof(struct nft_expr_ops), GFP_KERNEL);
786 	if (!ops) {
787 		err = -ENOMEM;
788 		goto err;
789 	}
790 
791 	ops->type = &nft_match_type;
792 	ops->eval = nft_match_eval;
793 	ops->init = nft_match_init;
794 	ops->destroy = nft_match_destroy;
795 	ops->dump = nft_match_dump;
796 	ops->validate = nft_match_validate;
797 	ops->data = match;
798 
799 	matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
800 	if (matchsize > NFT_MATCH_LARGE_THRESH) {
801 		matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
802 
803 		ops->eval = nft_match_large_eval;
804 		ops->init = nft_match_large_init;
805 		ops->destroy = nft_match_large_destroy;
806 		ops->dump = nft_match_large_dump;
807 	}
808 
809 	ops->size = matchsize;
810 
811 	return ops;
812 err:
813 	module_put(match->me);
814 	return ERR_PTR(err);
815 }
816 
nft_match_release_ops(const struct nft_expr_ops * ops)817 static void nft_match_release_ops(const struct nft_expr_ops *ops)
818 {
819 	struct xt_match *match = ops->data;
820 
821 	module_put(match->me);
822 	kfree(ops);
823 }
824 
825 static struct nft_expr_type nft_match_type __read_mostly = {
826 	.name		= "match",
827 	.select_ops	= nft_match_select_ops,
828 	.release_ops	= nft_match_release_ops,
829 	.policy		= nft_match_policy,
830 	.maxattr	= NFTA_MATCH_MAX,
831 	.owner		= THIS_MODULE,
832 };
833 
834 static struct nft_expr_type nft_target_type;
835 
836 static const struct nft_expr_ops *
nft_target_select_ops(const struct nft_ctx * ctx,const struct nlattr * const tb[])837 nft_target_select_ops(const struct nft_ctx *ctx,
838 		      const struct nlattr * const tb[])
839 {
840 	struct nft_expr_ops *ops;
841 	struct xt_target *target;
842 	char *tg_name;
843 	u32 rev, family;
844 	int err;
845 
846 	if (tb[NFTA_TARGET_NAME] == NULL ||
847 	    tb[NFTA_TARGET_REV] == NULL ||
848 	    tb[NFTA_TARGET_INFO] == NULL)
849 		return ERR_PTR(-EINVAL);
850 
851 	tg_name = nla_data(tb[NFTA_TARGET_NAME]);
852 	rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
853 	family = ctx->family;
854 
855 	if (strcmp(tg_name, XT_ERROR_TARGET) == 0 ||
856 	    strcmp(tg_name, XT_STANDARD_TARGET) == 0 ||
857 	    strcmp(tg_name, "standard") == 0)
858 		return ERR_PTR(-EINVAL);
859 
860 	target = xt_request_find_target(family, tg_name, rev);
861 	if (IS_ERR(target))
862 		return ERR_PTR(-ENOENT);
863 
864 	if (!target->target) {
865 		err = -EINVAL;
866 		goto err;
867 	}
868 
869 	if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
870 		err = -EINVAL;
871 		goto err;
872 	}
873 
874 	ops = kzalloc(sizeof(struct nft_expr_ops), GFP_KERNEL);
875 	if (!ops) {
876 		err = -ENOMEM;
877 		goto err;
878 	}
879 
880 	ops->type = &nft_target_type;
881 	ops->size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
882 	ops->init = nft_target_init;
883 	ops->destroy = nft_target_destroy;
884 	ops->dump = nft_target_dump;
885 	ops->validate = nft_target_validate;
886 	ops->data = target;
887 
888 	if (family == NFPROTO_BRIDGE)
889 		ops->eval = nft_target_eval_bridge;
890 	else
891 		ops->eval = nft_target_eval_xt;
892 
893 	return ops;
894 err:
895 	module_put(target->me);
896 	return ERR_PTR(err);
897 }
898 
nft_target_release_ops(const struct nft_expr_ops * ops)899 static void nft_target_release_ops(const struct nft_expr_ops *ops)
900 {
901 	struct xt_target *target = ops->data;
902 
903 	module_put(target->me);
904 	kfree(ops);
905 }
906 
907 static struct nft_expr_type nft_target_type __read_mostly = {
908 	.name		= "target",
909 	.select_ops	= nft_target_select_ops,
910 	.release_ops	= nft_target_release_ops,
911 	.policy		= nft_target_policy,
912 	.maxattr	= NFTA_TARGET_MAX,
913 	.owner		= THIS_MODULE,
914 };
915 
nft_compat_module_init(void)916 static int __init nft_compat_module_init(void)
917 {
918 	int ret;
919 
920 	ret = nft_register_expr(&nft_match_type);
921 	if (ret < 0)
922 		return ret;
923 
924 	ret = nft_register_expr(&nft_target_type);
925 	if (ret < 0)
926 		goto err_match;
927 
928 	ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
929 	if (ret < 0) {
930 		pr_err("nft_compat: cannot register with nfnetlink.\n");
931 		goto err_target;
932 	}
933 
934 	return ret;
935 err_target:
936 	nft_unregister_expr(&nft_target_type);
937 err_match:
938 	nft_unregister_expr(&nft_match_type);
939 	return ret;
940 }
941 
nft_compat_module_exit(void)942 static void __exit nft_compat_module_exit(void)
943 {
944 	nfnetlink_subsys_unregister(&nfnl_compat_subsys);
945 	nft_unregister_expr(&nft_target_type);
946 	nft_unregister_expr(&nft_match_type);
947 }
948 
949 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
950 
951 module_init(nft_compat_module_init);
952 module_exit(nft_compat_module_exit);
953 
954 MODULE_LICENSE("GPL");
955 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
956 MODULE_ALIAS_NFT_EXPR("match");
957 MODULE_ALIAS_NFT_EXPR("target");
958