Skip to contentSkip to conversation

extensions: libxt_MARK: Add translation to nft

PATCH In review Shivani Bhardwaj 4 participants 6 messages Dec 23, 2015 – Dec 25, 2015 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 extensions: libxt_MARK: Add translation to nft

Add translation for target MARK to nftables.

Examples:

$ sudo iptables-translate -t mangle -A PREROUTING -j MARK --set-mark 12
nft add rule ip mangle PREROUTING counter mark set 0xc
$ sudo iptables-translate -t mangle -A PREROUTING -p tcp --dport 22 -j MARK --set-mark 2
nft add rule ip mangle PREROUTING tcp dport 22 counter mark set 0x2
---
 extensions/libxt_MARK.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
extensions/libxt_MARK.c +21 -1
index 556dbde..c5de763 100644
@@ -242,7 +242,24 @@ static void mark_tg_save(const void *ip, const struct xt_entry_target *target)
 {
 	const struct xt_mark_tginfo2 *info = (const void *)target->data;
 
-	printf(" --set-xmark 0x%x/0x%x", info->mark, info->mask);
+	printf(" --set mark 0x%x/0x%x", info->mark, info->mask);
+}
+
+static void
+print_mark_xlate(unsigned long mark, struct xt_buf *buf)
+{
+	xt_buf_add(buf, " 0x%lx", mark);
+}
+
+static int mark_tg_xlate(const struct xt_entry_target *target,
+			  struct xt_buf *buf, int numeric)
+{
+	const struct xt_mark_tginfo2 *info = (const void *)target->data;
+
+	xt_buf_add(buf, "mark set");
+	print_mark_xlate(info->mark, buf);
+
+	return 1;
 }
 
 static struct xtables_target mark_tg_reg[] = {
@@ -259,6 +276,7 @@ static struct xtables_target mark_tg_reg[] = {
 		.x6_parse      = MARK_parse_v0,
 		.x6_fcheck     = MARK_check,
 		.x6_options    = MARK_opts,
+		.xlate	       = mark_tg_xlate,
 	},
 	{
 		.family        = NFPROTO_IPV4,
@@ -273,6 +291,7 @@ static struct xtables_target mark_tg_reg[] = {
 		.x6_parse      = MARK_parse_v1,
 		.x6_fcheck     = MARK_check,
 		.x6_options    = MARK_opts,
+		.xlate	       = mark_tg_xlate,
 	},
 	{
 		.version       = XTABLES_VERSION,
@@ -287,6 +306,7 @@ static struct xtables_target mark_tg_reg[] = {
 		.x6_parse      = mark_tg_parse,
 		.x6_fcheck     = mark_tg_check,
 		.x6_options    = mark_tg_opts,
+		.xlate	       = mark_tg_xlate,
 	},
 };
 
Signature · 1 line
1.9.1
#1 lore raw
Quoted 70 lines · Shivani Bhardwaj

Add translation for target MARK to nftables.

Examples:

$ sudo iptables-translate -t mangle -A PREROUTING -j MARK --set-mark 12
nft add rule ip mangle PREROUTING counter mark set 0xc
$ sudo iptables-translate -t mangle -A PREROUTING -p tcp --dport 22 -j MARK --set-mark 2
nft add rule ip mangle PREROUTING tcp dport 22 counter mark set 0x2
---
 extensions/libxt_MARK.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
extensions/libxt_MARK.c +21 -2
index 556dbde..c5de763 100644
@@ -242,7 +242,24 @@ static void mark_tg_save(const void *ip, const struct xt_entry_target *target)
 {
        const struct xt_mark_tginfo2 *info = (const void *)target->data;
 
-       printf(" --set-xmark 0x%x/0x%x", info->mark, info->mask);
+       printf(" --set mark 0x%x/0x%x", info->mark, info->mask);
+}
+
+static void
+print_mark_xlate(unsigned long mark, struct xt_buf *buf)
+{
+       xt_buf_add(buf, " 0x%lx", mark);
+}
+
+static int mark_tg_xlate(const struct xt_entry_target *target,
+                         struct xt_buf *buf, int numeric)
+{
+       const struct xt_mark_tginfo2 *info = (const void *)target->data;
+
+       xt_buf_add(buf, "mark set");
+       print_mark_xlate(info->mark, buf);
+
+       return 1;
 }
 
 static struct xtables_target mark_tg_reg[] = {
@@ -259,6 +276,7 @@ static struct xtables_target mark_tg_reg[] = {
                .x6_parse      = MARK_parse_v0,
                .x6_fcheck     = MARK_check,
                .x6_options    = MARK_opts,
+               .xlate         = mark_tg_xlate,
        },
        {
                .family        = NFPROTO_IPV4,
@@ -273,6 +291,7 @@ static struct xtables_target mark_tg_reg[] = {
                .x6_parse      = MARK_parse_v1,
                .x6_fcheck     = MARK_check,
                .x6_options    = MARK_opts,
+               .xlate         = mark_tg_xlate,
        },
        {
                .version       = XTABLES_VERSION,
@@ -287,6 +306,7 @@ static struct xtables_target mark_tg_reg[] = {
                .x6_parse      = mark_tg_parse,
                .x6_fcheck     = mark_tg_check,
                .x6_options    = mark_tg_opts,
+               .xlate         = mark_tg_xlate,
        },
 };
 
--
Quoted 2 lines

1.9.1

Hi,

I'm unsure if the options or-set, and-set are supported in nftables. I cannot find much about them. Please let me know if there are translations corresponding to these options as well, I'll send another patch then.

Thank you

#3 lore raw
In reply to Shivani Bhardwaj

extensions/libxt_MARK.c static void mark_tg_save(const void *ip, const struct xt_entry_target *target) · line 246

Quoted 8 lines · Shivani Bhardwaj

+++ b/extensions/libxt_MARK.c

extensions/libxt_MARK.c +2 -1
@@ -242,7 +242,24 @@ static void mark_tg_save(const void *ip, const struct xt_entry_target *target)
 {
 	const struct xt_mark_tginfo2 *info = (const void *)target->data;
 
-	printf(" --set-xmark 0x%x/0x%x", info->mark, info->mask);
+	printf(" --set mark 0x%x/0x%x", info->mark, info->mask);
+}

This looks like it will break regular iptables.

#4 lore raw
Quoted 7 lines · Jan Engelhardt
Quoted 5 lines · Shivani Bhardwaj

+++ b/extensions/libxt_MARK.c

extensions/libxt_MARK.c +0 -0
@@ -242,7 +242,24 @@ static void mark_tg_save(const void *ip, const struct xt_entry_target *target)
 {
      const struct xt_mark_tginfo2 *info = (const void *)target->data;
 
Quoted 5 lines

extensions/libxt_MARK.c static void mark_tg_save(const void *ip, const struct xt_entry_target *target) · line 246

Quoted 3 lines
extensions/libxt_MARK.c +2 -1
-      printf(" --set-xmark 0x%x/0x%x", info->mark, info->mask);
+      printf(" --set mark 0x%x/0x%x", info->mark, info->mask);
+}

This looks like it will break regular iptables.

This is my mistake. I'm correcting this. Could you please tell about the options or-, and-, mark? Does nftables have an option corresponding to these?

Thank you

maintainer
#2 lore raw
In reply to Shivani Bhardwaj
Quoted 4 lines · Shivani Bhardwaj

I'm unsure if the options or-set, and-set are supported in nftables. I cannot find much about them. Please let me know if there are translations corresponding to these options as well, I'll send another patch then.

You have to combine another mark + binop, i.e.:

filter input mark set mark or 1
[ meta load mark => reg 1 ]
[ bitwise reg 1 = (reg=1 & 0xfffffffe ) ^ 0x00000001 ] [ meta set mark with reg 1 ]

i.e., load mark into register, or with 1 (or whatever), then set new mark based on result.

Replace or with and to get --and-mark.

#5 lore raw
In reply to Shivani Bhardwaj
Quoted 18 lines · Shivani Bhardwaj

static struct xtables_target mark_tg_reg[] = {

extensions/libxt_MARK.c +2 -0
@@ -259,6 +276,7 @@ static struct xtables_target mark_tg_reg[] = {
 		.x6_parse      = MARK_parse_v0,
 		.x6_fcheck     = MARK_check,
 		.x6_options    = MARK_opts,
+		.xlate	       = mark_tg_xlate,
 	},
 	{
 		.family        = NFPROTO_IPV4,
@@ -273,6 +291,7 @@ static struct xtables_target mark_tg_reg[] = {
 		.x6_parse      = MARK_parse_v1,
 		.x6_fcheck     = MARK_check,
 		.x6_options    = MARK_opts,
+		.xlate	       = mark_tg_xlate,
 	},
 	{
 		.version       = XTABLES_VERSION,

extensions/libxt_MARK.c static struct xtables_target mark_tg_reg[] = { · line 310

Quoted 5 lines
extensions/libxt_MARK.c +1 -0
@@ -287,6 +306,7 @@ static struct xtables_target mark_tg_reg[] = {
 		.x6_parse      = mark_tg_parse,
 		.x6_fcheck     = mark_tg_check,
 		.x6_options    = mark_tg_opts,
+		.xlate	       = mark_tg_xlate,
 	},

Same comment as for connmark. Please send v2.