laurenceman / rpms / iptables

Forked from rpms/iptables 5 years ago
Clone

Blame SOURCES/0035-arptables-nft-Fix-MARK-target-parsing-and-printing.patch

029dc7
From 0b6b93e52f2a862c23206f9ad9fc48aadc06fbd2 Mon Sep 17 00:00:00 2001
029dc7
From: Phil Sutter <phil@nwl.cc>
029dc7
Date: Thu, 31 Jan 2019 16:12:51 +0100
029dc7
Subject: [PATCH] arptables-nft: Fix MARK target parsing and printing
029dc7
029dc7
Legacy arptables parses mark values in hex no matter if prefixed with
029dc7
'0x' or not. Sadly, this is not easily achievable with guided option
029dc7
parser. Hence fall back to the old 'parse' callback. The introduced
029dc7
target definition is valid only for revision 2, but that's consistent
029dc7
with legacy arptables.
029dc7
029dc7
When printing, use --set-mark option instead of --set-xmark.
029dc7
029dc7
Signed-off-by: Phil Sutter <phil@nwl.cc>
029dc7
Signed-off-by: Florian Westphal <fw@strlen.de>
029dc7
(cherry picked from commit f7fa88020f3bc4ec646ce2a48731a1f5fa2aa0a9)
029dc7
Signed-off-by: Phil Sutter <psutter@redhat.com>
029dc7
---
029dc7
 extensions/libxt_MARK.c                       | 95 +++++++++++++++++++
029dc7
 .../arptables/0001-arptables-save-restore_0   |  2 +-
029dc7
 2 files changed, 96 insertions(+), 1 deletion(-)
029dc7
029dc7
diff --git a/extensions/libxt_MARK.c b/extensions/libxt_MARK.c
029dc7
index 43aa977924b12..b765af6c35304 100644
029dc7
--- a/extensions/libxt_MARK.c
029dc7
+++ b/extensions/libxt_MARK.c
029dc7
@@ -1,3 +1,4 @@
029dc7
+#include <getopt.h>
029dc7
 #include <stdbool.h>
029dc7
 #include <stdio.h>
029dc7
 #include <xtables.h>
029dc7
@@ -245,6 +246,87 @@ static void mark_tg_save(const void *ip, const struct xt_entry_target *target)
029dc7
 	printf(" --set-xmark 0x%x/0x%x", info->mark, info->mask);
029dc7
 }
029dc7
 
029dc7
+static void mark_tg_arp_save(const void *ip, const struct xt_entry_target *target)
029dc7
+{
029dc7
+	const struct xt_mark_tginfo2 *info = (const void *)target->data;
029dc7
+
029dc7
+	if (info->mark == 0)
029dc7
+		printf(" --and-mark %x", (unsigned int)(uint32_t)~info->mask);
029dc7
+	else if (info->mark == info->mask)
029dc7
+		printf(" --or-mark %x", info->mark);
029dc7
+	else
029dc7
+		printf(" --set-mark %x", info->mark);
029dc7
+}
029dc7
+
029dc7
+static void mark_tg_arp_print(const void *ip,
029dc7
+			      const struct xt_entry_target *target, int numeric)
029dc7
+{
029dc7
+	mark_tg_arp_save(ip, target);
029dc7
+}
029dc7
+
029dc7
+#define MARK_OPT 1
029dc7
+#define AND_MARK_OPT 2
029dc7
+#define OR_MARK_OPT 3
029dc7
+
029dc7
+static struct option mark_tg_arp_opts[] = {
029dc7
+	{ .name = "set-mark", .has_arg = required_argument, .flag = 0, .val = MARK_OPT },
029dc7
+	{ .name = "and-mark", .has_arg = required_argument, .flag = 0, .val = AND_MARK_OPT },
029dc7
+	{ .name = "or-mark", .has_arg = required_argument, .flag = 0, .val =  OR_MARK_OPT },
029dc7
+	{ .name = NULL}
029dc7
+};
029dc7
+
029dc7
+static int
029dc7
+mark_tg_arp_parse(int c, char **argv, int invert, unsigned int *flags,
029dc7
+		  const void *entry, struct xt_entry_target **target)
029dc7
+{
029dc7
+	struct xt_mark_tginfo2 *info =
029dc7
+		(struct xt_mark_tginfo2 *)(*target)->data;
029dc7
+	int i;
029dc7
+
029dc7
+	switch (c) {
029dc7
+	case MARK_OPT:
029dc7
+		if (sscanf(argv[optind-1], "%x", &i) != 1) {
029dc7
+			xtables_error(PARAMETER_PROBLEM,
029dc7
+				"Bad mark value `%s'", optarg);
029dc7
+			return 0;
029dc7
+		}
029dc7
+		info->mark = i;
029dc7
+		if (*flags)
029dc7
+			xtables_error(PARAMETER_PROBLEM,
029dc7
+				"MARK: Can't specify --set-mark twice");
029dc7
+		*flags = 1;
029dc7
+		break;
029dc7
+	case AND_MARK_OPT:
029dc7
+		if (sscanf(argv[optind-1], "%x", &i) != 1) {
029dc7
+			xtables_error(PARAMETER_PROBLEM,
029dc7
+				"Bad mark value `%s'", optarg);
029dc7
+			return 0;
029dc7
+		}
029dc7
+		info->mark = 0;
029dc7
+		info->mask = ~i;
029dc7
+		if (*flags)
029dc7
+			xtables_error(PARAMETER_PROBLEM,
029dc7
+				"MARK: Can't specify --and-mark twice");
029dc7
+		*flags = 1;
029dc7
+		break;
029dc7
+	case OR_MARK_OPT:
029dc7
+		if (sscanf(argv[optind-1], "%x", &i) != 1) {
029dc7
+			xtables_error(PARAMETER_PROBLEM,
029dc7
+				"Bad mark value `%s'", optarg);
029dc7
+			return 0;
029dc7
+		}
029dc7
+		info->mark = info->mask = i;
029dc7
+		if (*flags)
029dc7
+			xtables_error(PARAMETER_PROBLEM,
029dc7
+				"MARK: Can't specify --or-mark twice");
029dc7
+		*flags = 1;
029dc7
+		break;
029dc7
+	default:
029dc7
+		return 0;
029dc7
+	}
029dc7
+	return 1;
029dc7
+}
029dc7
+
029dc7
 static int mark_tg_xlate(struct xt_xlate *xl,
029dc7
 			 const struct xt_xlate_tg_params *params)
029dc7
 {
029dc7
@@ -335,6 +417,19 @@ static struct xtables_target mark_tg_reg[] = {
029dc7
 		.x6_options    = mark_tg_opts,
029dc7
 		.xlate	       = mark_tg_xlate,
029dc7
 	},
029dc7
+	{
029dc7
+		.version       = XTABLES_VERSION,
029dc7
+		.name          = "MARK",
029dc7
+		.revision      = 2,
029dc7
+		.family        = NFPROTO_ARP,
029dc7
+		.size          = XT_ALIGN(sizeof(struct xt_mark_tginfo2)),
029dc7
+		.userspacesize = XT_ALIGN(sizeof(struct xt_mark_tginfo2)),
029dc7
+		.help          = mark_tg_help,
029dc7
+		.print         = mark_tg_arp_print,
029dc7
+		.save          = mark_tg_arp_save,
029dc7
+		.parse         = mark_tg_arp_parse,
029dc7
+		.extra_opts    = mark_tg_arp_opts,
029dc7
+	},
029dc7
 };
029dc7
 
029dc7
 void _init(void)
029dc7
diff --git a/iptables/tests/shell/testcases/arptables/0001-arptables-save-restore_0 b/iptables/tests/shell/testcases/arptables/0001-arptables-save-restore_0
029dc7
index 73b3b0cf88e18..f8629551b0ba9 100755
029dc7
--- a/iptables/tests/shell/testcases/arptables/0001-arptables-save-restore_0
029dc7
+++ b/iptables/tests/shell/testcases/arptables/0001-arptables-save-restore_0
029dc7
@@ -47,7 +47,7 @@ DUMP='*filter
029dc7
 -A OUTPUT -o eth432 --h-length 6 --opcode 1 --h-type 1 -j CLASSIFY --set-class feed:babe
029dc7
 -A foo -i lo --h-length 6 --h-type 1 -j ACCEPT
029dc7
 -A foo --h-length 6 --h-type 1 -j ACCEPT
029dc7
--A foo --h-length 6 --h-type 1 -j MARK --set-xmark 0x3039/0xffffffff
029dc7
+-A foo --h-length 6 --h-type 1 -j MARK --set-mark 12345
029dc7
 -A foo --h-length 6 --opcode 1 --h-type 1 -j ACCEPT
029dc7
 -A foo --h-length 6 --h-type 1 --proto-type 0x800 -j ACCEPT
029dc7
 -A foo -i lo --h-length 6 --opcode 1 --h-type 1 --proto-type 0x800 -j ACCEPT
029dc7
-- 
029dc7
2.21.0
029dc7