Linux Audio

Check our new training course

In-person Linux kernel drivers training

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