Blame SOURCES/0013-arptables-Add-MARK-target.patch

0890d3
From 31eaf6577094b474932846d817c25fdd734e1aa8 Mon Sep 17 00:00:00 2001
0890d3
From: Zhang Chunyu <zhangcy@cn.fujitsu.com>
0890d3
Date: Sun, 29 Mar 2015 22:35:48 -0400
0890d3
Subject: [PATCH] arptables: Add MARK target
0890d3
0890d3
We can use MARK target to set make value for arp packet.
0890d3
0890d3
Signed-off-by: Gao feng <gaofeng@cn.fujitsu.com>
0890d3
Signed-off-by: Zhang Chunyu <zhangcy@cn.fujitsu.com>
0890d3
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
0890d3
(cherry picked from commit 4bb2f8340783fd3a3f70aa6f8807428a280f8474)
0890d3
Signed-off-by: Phil Sutter <psutter@redhat.com>
0890d3
---
0890d3
 arptables.8            |  17 +++++
0890d3
 extensions/Makefile    |   2 +-
0890d3
 extensions/arpt_MARK.c | 150 +++++++++++++++++++++++++++++++++++++++++
0890d3
 3 files changed, 168 insertions(+), 1 deletion(-)
0890d3
 create mode 100644 extensions/arpt_MARK.c
0890d3
0890d3
diff --git a/arptables.8 b/arptables.8
0890d3
index 78b2c60978c40..676b8840c60f2 100644
0890d3
--- a/arptables.8
0890d3
+++ b/arptables.8
0890d3
@@ -315,6 +315,23 @@ sify the packet into a specific CBQ class).
0890d3
 Set the major and minor  class  value.  The  values  are  always
0890d3
 interpreted as hexadecimal even if no 0x prefix is given.
0890d3
 
0890d3
+.SS MARK
0890d3
+This  module  allows you to set the skb->mark value (and thus classify
0890d3
+the packet by the mark in u32)
0890d3
+
0890d3
+.TP
0890d3
+.BR "--set-mark mark"
0890d3
+Set the mark value. The  values  are  always
0890d3
+interpreted as hexadecimal even if no 0x prefix is given
0890d3
+
0890d3
+.TP
0890d3
+.BR "--and-mark mark"
0890d3
+Binary AND the mark with bits.
0890d3
+
0890d3
+.TP
0890d3
+.BR "--or-mark mark"
0890d3
+Binary OR the mark with bits.
0890d3
+
0890d3
 .SH MAILINGLISTS
0890d3
 .BR "" "See " http://netfilter.org/mailinglists.html
0890d3
 .SH SEE ALSO
0890d3
diff --git a/extensions/Makefile b/extensions/Makefile
0890d3
index 09b244ead15ac..0189cc9066674 100644
0890d3
--- a/extensions/Makefile
0890d3
+++ b/extensions/Makefile
0890d3
@@ -1,6 +1,6 @@
0890d3
 #! /usr/bin/make
0890d3
 
0890d3
-EXT_FUNC+=standard mangle CLASSIFY
0890d3
+EXT_FUNC+=standard mangle CLASSIFY MARK
0890d3
 EXT_OBJS+=$(foreach T,$(EXT_FUNC), extensions/arpt_$(T).o)
0890d3
 
0890d3
 extensions/ebt_%.o: extensions/arpt_%.c include/arptables.h include/arptables_common.h
0890d3
diff --git a/extensions/arpt_MARK.c b/extensions/arpt_MARK.c
0890d3
new file mode 100644
0890d3
index 0000000000000..3e5a9f96db8a7
0890d3
--- /dev/null
0890d3
+++ b/extensions/arpt_MARK.c
0890d3
@@ -0,0 +1,150 @@
0890d3
+/*
0890d3
+ * (C) 2014 by Gao Feng <gaofeng@cn.fujitsu.com>
0890d3
+ *
0890d3
+ * arpt_MARK.c -- arptables extension to set mark for arp packet
0890d3
+ *
0890d3
+ *	This program is free software; you can redistribute it and/or modify
0890d3
+ *	it under the terms of the GNU General Public License as published by
0890d3
+ *	the Free Software Foundation; either version 2 of the License, or
0890d3
+ *	(at your option) any later version.
0890d3
+ *
0890d3
+ *	This program is distributed in the hope that it will be useful,
0890d3
+ *	but WITHOUT ANY WARRANTY; without even the implied warranty of
0890d3
+ *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0890d3
+ *	GNU General Public License for more details.
0890d3
+ *
0890d3
+ *	You should have received a copy of the GNU General Public License
0890d3
+ *	along with this program; if not, write to the Free Software
0890d3
+ *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
0890d3
+ */
0890d3
+
0890d3
+#include <stdio.h>
0890d3
+#include <getopt.h>
0890d3
+#include <arptables.h>
0890d3
+#include <linux/netfilter/xt_mark.h>
0890d3
+#include <linux/netfilter/xt_MARK.h>
0890d3
+#include <linux/netfilter/x_tables.h>
0890d3
+
0890d3
+static void help(void)
0890d3
+{
0890d3
+	printf(
0890d3
+"MARK target v%s options:\n"
0890d3
+"--set-mark mark : set the mark value\n"
0890d3
+"--and-mark value : binary AND the mark with value\n"
0890d3
+"--or-mark value : binary OR the mark with value\n",
0890d3
+	ARPTABLES_VERSION);
0890d3
+}
0890d3
+
0890d3
+#define MARK_OPT 1
0890d3
+#define AND_MARK_OPT 2
0890d3
+#define OR_MARK_OPT 3
0890d3
+
0890d3
+static struct option opts[] = {
0890d3
+	{ .name = "set-mark", .has_arg = required_argument, .flag = 0, .val = MARK_OPT },
0890d3
+	{ .name = "and-mark", .has_arg = required_argument, .flag = 0, .val = AND_MARK_OPT },
0890d3
+	{ .name = "or-mark", .has_arg = required_argument, .flag = 0, .val =  OR_MARK_OPT },
0890d3
+	{ .name = NULL}
0890d3
+};
0890d3
+
0890d3
+static void init(struct arpt_entry_target *t)
0890d3
+{
0890d3
+	struct xt_mark_tginfo2 *info = (struct xt_mark_tginfo2 *) t->data;
0890d3
+
0890d3
+	info->mark = 0;
0890d3
+}
0890d3
+
0890d3
+static int parse(int c, char **argv, int invert, unsigned int *flags,
0890d3
+		 const struct arpt_entry *e, struct arpt_entry_target **t)
0890d3
+{
0890d3
+	struct xt_mark_tginfo2 *info = (struct xt_mark_tginfo2 *)(*t)->data;
0890d3
+	int i;
0890d3
+
0890d3
+	switch (c) {
0890d3
+	case MARK_OPT:
0890d3
+		if (sscanf(argv[optind-1], "%x", &i) != 1) {
0890d3
+			exit_error(PARAMETER_PROBLEM,
0890d3
+				"Bad mark value `%s'", optarg);
0890d3
+			return 0;
0890d3
+		}
0890d3
+		info->mark = i;
0890d3
+		if (*flags)
0890d3
+			exit_error(PARAMETER_PROBLEM,
0890d3
+				"MARK: Can't specify --set-mark twice");
0890d3
+		*flags = 1;
0890d3
+		break;
0890d3
+	case AND_MARK_OPT:
0890d3
+		if (sscanf(argv[optind-1], "%x", &i) != 1) {
0890d3
+			exit_error(PARAMETER_PROBLEM,
0890d3
+				"Bad mark value `%s'", optarg);
0890d3
+			return 0;
0890d3
+		}
0890d3
+		info->mark = 0;
0890d3
+		info->mask = ~i;
0890d3
+		if (*flags)
0890d3
+			exit_error(PARAMETER_PROBLEM,
0890d3
+				"MARK: Can't specify --and-mark twice");
0890d3
+		*flags = 1;
0890d3
+		break;
0890d3
+	case OR_MARK_OPT:
0890d3
+		if (sscanf(argv[optind-1], "%x", &i) != 1) {
0890d3
+			exit_error(PARAMETER_PROBLEM,
0890d3
+				"Bad mark value `%s'", optarg);
0890d3
+			return 0;
0890d3
+		}
0890d3
+		info->mark = info->mask = i;
0890d3
+		if (*flags)
0890d3
+			exit_error(PARAMETER_PROBLEM,
0890d3
+				"MARK: Can't specify --or-mark twice");
0890d3
+		*flags = 1;
0890d3
+		break;
0890d3
+	default:
0890d3
+		return 0;
0890d3
+	}
0890d3
+	return 1;
0890d3
+}
0890d3
+
0890d3
+static void final_check(unsigned int flags)
0890d3
+{
0890d3
+	if (!flags)
0890d3
+		exit_error(PARAMETER_PROBLEM, "MARK: Parameter --set-mark/--and-mark/--or-mark is required");
0890d3
+}
0890d3
+
0890d3
+static void print(const struct arpt_arp *ip,
0890d3
+		  const struct arpt_entry_target *target, int numeric)
0890d3
+{
0890d3
+	struct xt_mark_tginfo2 *info = (struct xt_mark_tginfo2 *)(target->data);
0890d3
+
0890d3
+	if (info->mark == 0)
0890d3
+		printf("--and-mark %x", (unsigned int)(uint32_t)~info->mask);
0890d3
+	else if (info->mark == info->mask)
0890d3
+		printf("--or-mark %x", info->mark);
0890d3
+	else
0890d3
+		printf("--set-mark %x", info->mark);
0890d3
+}
0890d3
+
0890d3
+static void save(const struct arpt_arp *ip,
0890d3
+		 const struct arpt_entry_target *target)
0890d3
+{
0890d3
+}
0890d3
+
0890d3
+static struct arptables_target mark = {
0890d3
+	.next          = NULL,
0890d3
+	.name          = "MARK",
0890d3
+	.version       = ARPTABLES_VERSION,
0890d3
+	.size          = ARPT_ALIGN(sizeof(struct xt_mark_tginfo2)),
0890d3
+	.userspacesize = ARPT_ALIGN(sizeof(struct xt_mark_tginfo2)),
0890d3
+	.revision      = 2,
0890d3
+	.help          = help,
0890d3
+	.init          = init,
0890d3
+	.parse         = parse,
0890d3
+	.final_check   = final_check,
0890d3
+	.print         = print,
0890d3
+	.save          = save,
0890d3
+	.extra_opts    = opts
0890d3
+};
0890d3
+
0890d3
+static void _init(void) __attribute__ ((constructor));
0890d3
+static void _init(void)
0890d3
+{
0890d3
+	register_target(&mark);
0890d3
+}
0890d3
-- 
0890d3
2.21.0
0890d3