netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] extensions: libxt_mark: Add translation to nft
@ 2015-12-20  3:14 Shivani Bhardwaj
  2015-12-22 16:19 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 3+ messages in thread
From: Shivani Bhardwaj @ 2015-12-20  3:14 UTC (permalink / raw)
  To: netfilter-devel

Add translation for metainformation mark to nftables.

Examples:

$ sudo iptables-translate -I INPUT -m mark --mark 12
nft insert rule ip filter INPUT mark  0xc counter

$ sudo iptables-translate -A FORWARD -m mark --mark 22 -j ACCEPT
nft add rule ip filter FORWARD mark  0x16 counter accept

$ sudo iptables-translate -t mangle -A PREROUTING -p tcp --dport 4600 -m mark --mark 0x40
nft add rule ip mangle PREROUTING tcp dport 4600 mark  0x40 counter

$ sudo iptables-translate -A FORWARD -m mark --mark 0x400/0x400 -j ACCEPT
nft add rule ip filter FORWARD mark  and 0x400 == 0x400 counter accept

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
---
Changes in v4:
	Fix indentation errors shown by checkpatch
	Add tested and verified examples in commit message

 extensions/libxt_mark.c | 42 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)

diff --git a/extensions/libxt_mark.c b/extensions/libxt_mark.c
index 7f8c995..4eb5ef8 100644
--- a/extensions/libxt_mark.c
+++ b/extensions/libxt_mark.c
@@ -75,7 +75,7 @@ mark_print(const void *ip, const struct xt_entry_match *match, int numeric)
 
 	if (info->invert)
 		printf(" !");
-	
+
 	print_mark(info->mark, info->mask);
 }
 
@@ -97,11 +97,47 @@ mark_save(const void *ip, const struct xt_entry_match *match)
 
 	if (info->invert)
 		printf(" !");
-	
+
 	printf(" --mark");
 	print_mark(info->mark, info->mask);
 }
 
+static void
+print_mark_xlate(struct xt_buf *buf,
+		 unsigned int mark, unsigned int mask)
+{
+	if (mask != 0xffffffffU)
+		xt_buf_add(buf, " and 0x%x == 0x%x", mark, mask);
+	else
+		xt_buf_add(buf, " 0x%x", mark);
+}
+
+static int
+mark_mt_xlate(const struct xt_entry_match *match,
+	      struct xt_buf *buf, int numeric)
+{
+	const struct xt_mark_mtinfo1 *info = (const void *)match->data;
+
+	xt_buf_add(buf, "mark %s", info->invert ? " !=" : "");
+	print_mark_xlate(buf, info->mark, info->mask);
+	xt_buf_add(buf, " ");
+
+	return 1;
+}
+
+static int
+mark_xlate(const struct xt_entry_match *match,
+	   struct xt_buf *buf, int numeric)
+{
+	const struct xt_mark_info *info = (const void *)match->data;
+
+	xt_buf_add(buf, "mark %s", info->invert ? " !=" : "");
+	print_mark_xlate(buf, info->mark, info->mask);
+	xt_buf_add(buf, " ");
+
+	return 1;
+}
+
 static struct xtables_match mark_mt_reg[] = {
 	{
 		.family        = NFPROTO_UNSPEC,
@@ -115,6 +151,7 @@ static struct xtables_match mark_mt_reg[] = {
 		.save          = mark_save,
 		.x6_parse      = mark_parse,
 		.x6_options    = mark_mt_opts,
+		.xlate	       = mark_xlate,
 	},
 	{
 		.version       = XTABLES_VERSION,
@@ -128,6 +165,7 @@ static struct xtables_match mark_mt_reg[] = {
 		.save          = mark_mt_save,
 		.x6_parse      = mark_mt_parse,
 		.x6_options    = mark_mt_opts,
+		.xlate	       = mark_mt_xlate,
 	},
 };
 
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] extensions: libxt_mark: Add translation to nft
  2015-12-20  3:14 [PATCH v4] extensions: libxt_mark: Add translation to nft Shivani Bhardwaj
@ 2015-12-22 16:19 ` Pablo Neira Ayuso
  2015-12-22 16:22   ` Pablo Neira Ayuso
  0 siblings, 1 reply; 3+ messages in thread
From: Pablo Neira Ayuso @ 2015-12-22 16:19 UTC (permalink / raw)
  To: Shivani Bhardwaj; +Cc: netfilter-devel

On Sun, Dec 20, 2015 at 08:44:12AM +0530, Shivani Bhardwaj wrote:
> Add translation for metainformation mark to nftables.
>
> Examples:
> 
> $ sudo iptables-translate -I INPUT -m mark --mark 12
> nft insert rule ip filter INPUT mark  0xc counter
> 
> $ sudo iptables-translate -A FORWARD -m mark --mark 22 -j ACCEPT
> nft add rule ip filter FORWARD mark  0x16 counter accept
> 
> $ sudo iptables-translate -t mangle -A PREROUTING -p tcp --dport 4600 -m mark --mark 0x40
> nft add rule ip mangle PREROUTING tcp dport 4600 mark  0x40 counter
> 
> $ sudo iptables-translate -A FORWARD -m mark --mark 0x400/0x400 -j ACCEPT
> nft add rule ip filter FORWARD mark  and 0x400 == 0x400 counter accept

Applied with minor glitch I have fixed here.

> +static void
> +print_mark_xlate(struct xt_buf *buf,
> +		 unsigned int mark, unsigned int mask)
> +{
> +	if (mask != 0xffffffffU)
> +		xt_buf_add(buf, " and 0x%x == 0x%x", mark, mask);
> +	else
> +		xt_buf_add(buf, " 0x%x", mark);
> +}
> +
> +static int
> +mark_mt_xlate(const struct xt_entry_match *match,
> +	      struct xt_buf *buf, int numeric)
> +{
> +	const struct xt_mark_mtinfo1 *info = (const void *)match->data;
> +
> +	xt_buf_add(buf, "mark %s", info->invert ? " !=" : "");

I have edited the line above to become:

	xt_buf_add(buf, "mark%s", info->invert ? " !=" : "");

So we get rid of the extra space:

> nft add rule ip filter FORWARD mark  and 0x400 == 0x400 counter accept
                                     ^
                                   here

> +	print_mark_xlate(buf, info->mark, info->mask);
> +	xt_buf_add(buf, " ");

Removed this xt_buf_add(buf, " ") and added the space to
print_mark_xlate().

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] extensions: libxt_mark: Add translation to nft
  2015-12-22 16:19 ` Pablo Neira Ayuso
@ 2015-12-22 16:22   ` Pablo Neira Ayuso
  0 siblings, 0 replies; 3+ messages in thread
From: Pablo Neira Ayuso @ 2015-12-22 16:22 UTC (permalink / raw)
  To: Shivani Bhardwaj; +Cc: netfilter-devel

On Tue, Dec 22, 2015 at 05:19:48PM +0100, Pablo Neira Ayuso wrote:
> On Sun, Dec 20, 2015 at 08:44:12AM +0530, Shivani Bhardwaj wrote:
> > Add translation for metainformation mark to nftables.
> >
> > Examples:
> > 
> > $ sudo iptables-translate -I INPUT -m mark --mark 12
> > nft insert rule ip filter INPUT mark  0xc counter
> > 
> > $ sudo iptables-translate -A FORWARD -m mark --mark 22 -j ACCEPT
> > nft add rule ip filter FORWARD mark  0x16 counter accept
> > 
> > $ sudo iptables-translate -t mangle -A PREROUTING -p tcp --dport 4600 -m mark --mark 0x40
> > nft add rule ip mangle PREROUTING tcp dport 4600 mark  0x40 counter
> > 
> > $ sudo iptables-translate -A FORWARD -m mark --mark 0x400/0x400 -j ACCEPT
> > nft add rule ip filter FORWARD mark  and 0x400 == 0x400 counter accept
> 
> Applied with minor glitch I have fixed here.

I noticed another problem.

This translation doesn't look OK.

 # iptables-translate -I INPUT -p tcp -m mark ! --mark 0xa/0xa
 nft insert rule ip filter INPUT ip protocol tcp mark != and 0xa == 0xa counter 

No problem, please just send me a follow up patch to fix this and make
sure you test all possible code branches next time.

Thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-12-22 16:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-12-20  3:14 [PATCH v4] extensions: libxt_mark: Add translation to nft Shivani Bhardwaj
2015-12-22 16:19 ` Pablo Neira Ayuso
2015-12-22 16:22   ` Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).