All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] extensions: libxt_connmark: Add translation to nft
@ 2015-12-22 21:06 Shivani Bhardwaj
  0 siblings, 0 replies; 5+ messages in thread
From: Shivani Bhardwaj @ 2015-12-22 21:06 UTC (permalink / raw)
  To: netfilter-devel

Add translation for connmark to nftables.

Examples:

$ sudo iptables-translate -A INPUT -m connmark --mark 2 -j ACCEPT
nft add rule ip filter INPUT ct mark 0x2 counter accept

$ sudo iptables-translate -A INPUT -m connmark ! --mark 2 -j ACCEPT
nft add rule ip filter INPUT ct mark != 0x2 counter accept

$ sudo iptables-translate -A INPUT -m connmark --mark 10/10 -j ACCEPT
nft add rule ip filter INPUT ct mark and 0xa == 0xa counter accept

$ sudo iptables-translate -A INPUT -m connmark ! --mark 10/10 -j ACCEPT
nft add rule ip filter INPUT ct mark and 0xa != 0xa counter accept

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

Signed-off-by : Shivani Bhardwaj <shivanib134@gmail.com>
---
Changes in v3:
	Fix the code for inversion of mark only

 extensions/libxt_connmark.c | 47 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/extensions/libxt_connmark.c b/extensions/libxt_connmark.c
index 95477de..6f82b65 100644
--- a/extensions/libxt_connmark.c
+++ b/extensions/libxt_connmark.c
@@ -22,6 +22,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <string.h>
 #include <xtables.h>
 #include <linux/netfilter/xt_connmark.h>
 
@@ -89,7 +90,8 @@ connmark_print(const void *ip, const struct xt_entry_match *match, int numeric)
 }
 
 static void
-connmark_mt_print(const void *ip, const struct xt_entry_match *match, int numeric)
+connmark_mt_print(const void *ip, const struct xt_entry_match *match,
+		  int numeric)
 {
 	const struct xt_connmark_mtinfo1 *info = (const void *)match->data;
 
@@ -122,6 +124,47 @@ connmark_mt_save(const void *ip, const struct xt_entry_match *match)
 	print_mark(info->mark, info->mask);
 }
 
+static void print_mark_xlate(unsigned int mark, unsigned int mask,
+			     struct xt_buf *buf, const char *str)
+{
+	if (mask != 0xffffffffU)
+		xt_buf_add(buf, " and 0x%x %s 0x%x ", mark, str, mask);
+	else
+		xt_buf_add(buf, " %s0x%x ",
+			   !strcmp(str, "==") ? "" : "!= ", mark);
+}
+
+static int connmark_xlate(const struct xt_entry_match *match,
+			  struct xt_buf *buf, int numeric)
+{
+	const struct xt_connmark_info *info = (const void *)match->data;
+	const char *str = "==";
+
+	if (info->invert)
+		str = "!=";
+
+	xt_buf_add(buf, "ct mark");
+	print_mark_xlate(info->mark, info->mask, buf, str);
+
+	return 1;
+}
+
+static int
+connmark_mt_xlate(const struct xt_entry_match *match,
+		 struct xt_buf *buf, int numeric)
+{
+	const struct xt_connmark_mtinfo1 *info = (const void *)match->data;
+	const char *str = "==";
+
+	if (info->invert)
+		str = "!=";
+
+	xt_buf_add(buf, "ct mark");
+	print_mark_xlate(info->mark, info->mask, buf, str);
+
+	return 1;
+}
+
 static struct xtables_match connmark_mt_reg[] = {
 	{
 		.family        = NFPROTO_UNSPEC,
@@ -135,6 +178,7 @@ static struct xtables_match connmark_mt_reg[] = {
 		.save          = connmark_save,
 		.x6_parse      = connmark_parse,
 		.x6_options    = connmark_mt_opts,
+		.xlate	       = connmark_xlate,
 	},
 	{
 		.version       = XTABLES_VERSION,
@@ -148,6 +192,7 @@ static struct xtables_match connmark_mt_reg[] = {
 		.save          = connmark_mt_save,
 		.x6_parse      = connmark_mt_parse,
 		.x6_options    = connmark_mt_opts,
+		.xlate	       = connmark_mt_xlate,
 	},
 };
 
-- 
1.9.1


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

* Re: [PATCH v3] extensions: libxt_connmark: Add translation to nft
  2015-12-25 14:39   ` Shivani Bhardwaj
@ 2015-12-28 12:00     ` Pablo Neira Ayuso
  0 siblings, 0 replies; 5+ messages in thread
From: Pablo Neira Ayuso @ 2015-12-28 12:00 UTC (permalink / raw)
  To: Shivani Bhardwaj; +Cc: netfilter-devel

On Fri, Dec 25, 2015 at 08:09:40PM +0530, Shivani Bhardwaj wrote:
> On Fri, Dec 25, 2015 at 5:43 PM, Pablo Neira Ayuso <pablo@netfilter.org> wrote:
> > The size of unsigned long depends on the architecture, so this code
> > will not work correctly.
> >
> > You have to add a connmark_mt_xlate_v0() and connmark_mt_xlate_v1(),
> > in each of these functions you have to cast data to the right
> > structure layout.
> >
> > Thanks.
> 
> Thanks for explaining. I understand it now but why is this patch
> wrong? I have used connmark_xlate() and connmark_mt_xlate() in their
> respective places. Do I still get it wrong?
> 
> I'm correcting this mistake for MARK.

This patch correctly handles what I indicated above, but MARK is still
wrong.

So I'm applying this patch, thanks.

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

* Re: [PATCH v3] extensions: libxt_connmark: Add translation to nft
  2015-12-25 12:13 ` Pablo Neira Ayuso
@ 2015-12-25 14:39   ` Shivani Bhardwaj
  2015-12-28 12:00     ` Pablo Neira Ayuso
  0 siblings, 1 reply; 5+ messages in thread
From: Shivani Bhardwaj @ 2015-12-25 14:39 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

On Fri, Dec 25, 2015 at 5:43 PM, Pablo Neira Ayuso <pablo@netfilter.org> wrote:
> On Wed, Dec 23, 2015 at 08:03:33PM +0530, Shivani Bhardwaj wrote:
>>  static struct xtables_match connmark_mt_reg[] = {
>>       {
>>               .family        = NFPROTO_UNSPEC,
>> @@ -135,6 +178,7 @@ static struct xtables_match connmark_mt_reg[] = {
>>               .save          = connmark_save,
>>               .x6_parse      = connmark_parse,
>>               .x6_options    = connmark_mt_opts,
>> +             .xlate         = connmark_xlate,
>>       },
>>       {
>>               .version       = XTABLES_VERSION,
>> @@ -148,6 +192,7 @@ static struct xtables_match connmark_mt_reg[] = {
>>               .save          = connmark_mt_save,
>>               .x6_parse      = connmark_mt_parse,
>>               .x6_options    = connmark_mt_opts,
>> +             .xlate         = connmark_mt_xlate,
>
> Careful.
>
> The different revisions use different structure definitions, ie. the
> structure binary layout is different, therefore you cannot reuse the
> same connmark_mt_xlate() for the two different revision.
>
> Let me make sure I clarify this, this is revision 0:
>
> struct xt_connmark_info {
>         unsigned long mark, mask;
>         uint8_t invert;
> };
>
> This is revision 1:
>
> struct xt_connmark_mtinfo1 {
>         __u32 mark, mask;
>         __u8 invert;
> };
>
> The size of unsigned long depends on the architecture, so this code
> will not work correctly.
>
> You have to add a connmark_mt_xlate_v0() and connmark_mt_xlate_v1(),
> in each of these functions you have to cast data to the right
> structure layout.
>
> Thanks.

Thanks for explaining. I understand it now but why is this patch
wrong? I have used connmark_xlate() and connmark_mt_xlate() in their
respective places. Do I still get it wrong?

I'm correcting this mistake for MARK.
Thank you

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

* Re: [PATCH v3] extensions: libxt_connmark: Add translation to nft
  2015-12-23 14:33 Shivani Bhardwaj
@ 2015-12-25 12:13 ` Pablo Neira Ayuso
  2015-12-25 14:39   ` Shivani Bhardwaj
  0 siblings, 1 reply; 5+ messages in thread
From: Pablo Neira Ayuso @ 2015-12-25 12:13 UTC (permalink / raw)
  To: Shivani Bhardwaj; +Cc: netfilter-devel

On Wed, Dec 23, 2015 at 08:03:33PM +0530, Shivani Bhardwaj wrote:
>  static struct xtables_match connmark_mt_reg[] = {
>  	{
>  		.family        = NFPROTO_UNSPEC,
> @@ -135,6 +178,7 @@ static struct xtables_match connmark_mt_reg[] = {
>  		.save          = connmark_save,
>  		.x6_parse      = connmark_parse,
>  		.x6_options    = connmark_mt_opts,
> +		.xlate	       = connmark_xlate,
>  	},
>  	{
>  		.version       = XTABLES_VERSION,
> @@ -148,6 +192,7 @@ static struct xtables_match connmark_mt_reg[] = {
>  		.save          = connmark_mt_save,
>  		.x6_parse      = connmark_mt_parse,
>  		.x6_options    = connmark_mt_opts,
> +		.xlate	       = connmark_mt_xlate,

Careful.

The different revisions use different structure definitions, ie. the
structure binary layout is different, therefore you cannot reuse the
same connmark_mt_xlate() for the two different revision.

Let me make sure I clarify this, this is revision 0:

struct xt_connmark_info {
        unsigned long mark, mask;
        uint8_t invert;
};

This is revision 1:

struct xt_connmark_mtinfo1 {
        __u32 mark, mask;
        __u8 invert;
};

The size of unsigned long depends on the architecture, so this code
will not work correctly.

You have to add a connmark_mt_xlate_v0() and connmark_mt_xlate_v1(),
in each of these functions you have to cast data to the right
structure layout.

Thanks.

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

* [PATCH v3] extensions: libxt_connmark: Add translation to nft
@ 2015-12-23 14:33 Shivani Bhardwaj
  2015-12-25 12:13 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 5+ messages in thread
From: Shivani Bhardwaj @ 2015-12-23 14:33 UTC (permalink / raw)
  To: netfilter-devel

Add translation for connmark to nftables.

Examples:

$ sudo iptables-translate -A INPUT -m connmark --mark 2 -j ACCEPT
nft add rule ip filter INPUT ct mark 0x2 counter accept

$ sudo iptables-translate -A INPUT -m connmark ! --mark 2 -j ACCEPT
nft add rule ip filter INPUT ct mark != 0x2 counter accept

$ sudo iptables-translate -A INPUT -m connmark --mark 10/10 -j ACCEPT
nft add rule ip filter INPUT ct mark and 0xa == 0xa counter accept

$ sudo iptables-translate -A INPUT -m connmark ! --mark 10/10 -j ACCEPT
nft add rule ip filter INPUT ct mark and 0xa != 0xa counter accept

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

Signed-off-by : Shivani Bhardwaj <shivanib134@gmail.com>
---
Changes in v3:
	Replace the string added in v2 with enum to make code look
	better

 extensions/libxt_connmark.c | 47 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/extensions/libxt_connmark.c b/extensions/libxt_connmark.c
index 95477de..087d4bf 100644
--- a/extensions/libxt_connmark.c
+++ b/extensions/libxt_connmark.c
@@ -89,7 +89,8 @@ connmark_print(const void *ip, const struct xt_entry_match *match, int numeric)
 }
 
 static void
-connmark_mt_print(const void *ip, const struct xt_entry_match *match, int numeric)
+connmark_mt_print(const void *ip, const struct xt_entry_match *match,
+		  int numeric)
 {
 	const struct xt_connmark_mtinfo1 *info = (const void *)match->data;
 
@@ -122,6 +123,48 @@ connmark_mt_save(const void *ip, const struct xt_entry_match *match)
 	print_mark(info->mark, info->mask);
 }
 
+static void print_mark_xlate(unsigned int mark, unsigned int mask,
+			     struct xt_buf *buf, uint32_t op)
+{
+	if (mask != 0xffffffffU)
+		xt_buf_add(buf, " and 0x%x %s 0x%x ", mark,
+			   op == XT_OP_EQ ? "==" : "!=", mask);
+	else
+		xt_buf_add(buf, " %s0x%x ",
+			   op == XT_OP_EQ ? "" : "!= ", mark);
+}
+
+static int connmark_xlate(const struct xt_entry_match *match,
+			  struct xt_buf *buf, int numeric)
+{
+	const struct xt_connmark_info *info = (const void *)match->data;
+	enum xt_op op = XT_OP_EQ;
+
+	if (info->invert)
+		op = XT_OP_NEQ;
+
+	xt_buf_add(buf, "ct mark");
+	print_mark_xlate(info->mark, info->mask, buf, op);
+
+	return 1;
+}
+
+static int
+connmark_mt_xlate(const struct xt_entry_match *match,
+		 struct xt_buf *buf, int numeric)
+{
+	const struct xt_connmark_mtinfo1 *info = (const void *)match->data;
+	enum xt_op op = XT_OP_EQ;
+
+	if (info->invert)
+		op = XT_OP_NEQ;
+
+	xt_buf_add(buf, "ct mark");
+	print_mark_xlate(info->mark, info->mask, buf, op);
+
+	return 1;
+}
+
 static struct xtables_match connmark_mt_reg[] = {
 	{
 		.family        = NFPROTO_UNSPEC,
@@ -135,6 +178,7 @@ static struct xtables_match connmark_mt_reg[] = {
 		.save          = connmark_save,
 		.x6_parse      = connmark_parse,
 		.x6_options    = connmark_mt_opts,
+		.xlate	       = connmark_xlate,
 	},
 	{
 		.version       = XTABLES_VERSION,
@@ -148,6 +192,7 @@ static struct xtables_match connmark_mt_reg[] = {
 		.save          = connmark_mt_save,
 		.x6_parse      = connmark_mt_parse,
 		.x6_options    = connmark_mt_opts,
+		.xlate	       = connmark_mt_xlate,
 	},
 };
 
-- 
1.9.1


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

end of thread, other threads:[~2015-12-28 12:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-22 21:06 [PATCH v3] extensions: libxt_connmark: Add translation to nft Shivani Bhardwaj
2015-12-23 14:33 Shivani Bhardwaj
2015-12-25 12:13 ` Pablo Neira Ayuso
2015-12-25 14:39   ` Shivani Bhardwaj
2015-12-28 12:00     ` Pablo Neira Ayuso

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.