All of lore.kernel.org
 help / color / mirror / Atom feed
* MARK target extension
@ 2004-05-26 22:09 Grzegorz Nosek
  2004-05-27  9:53 ` Patrick McHardy
  0 siblings, 1 reply; 10+ messages in thread
From: Grzegorz Nosek @ 2004-05-26 22:09 UTC (permalink / raw)
  To: netfilter-devel

[-- Attachment #1: Type: text/plain, Size: 990 bytes --]

Hello all!

I've extended the MARK target (ipv4 only currently) to allow partial
modification of nfmark values instead of replacing. As in:

... -j MARK --set-mark 0x80
... -j MARK --set-mark 0x8000 --mask 0xff00

sets the resulting mark value to 0x8080. Note that it isn't a logical OR:

... -j MARK --set-mark 0xffff
... -j MARK --set-mark 0 --mask 0xff

would change the mark to 0xff00.

This way you can use the nfmark as a bit field (testing individual
bits can be done already). Patches included are based on
iptables-1.2.9 (from debian sarge source) and kernel 2.4.26, somewhat
patched (nothing related to nfmark, though, so it should apply cleanly
to a vanilla kernel too). What do you think? IMNSHO it would be useful
to include in standard kernel/iptables especially as it breaks no
existing applications (default mask is 0xffffffff), is no code bloat
and might actually be useful to somebody besides me. Note that it's my
first public kernel patch so be gentle.

Regards,
 Greg


[-- Attachment #2: iptables-mark-1.2.9.diff --]
[-- Type: text/plain, Size: 2503 bytes --]

diff -Naur iptables-1.2.9/extensions/libipt_MARK.c iptables-1.2.9-mark/extensions/libipt_MARK.c
--- iptables-1.2.9/extensions/libipt_MARK.c	2003-03-02 17:16:45.000000000 +0100
+++ iptables-1.2.9-mark/extensions/libipt_MARK.c	2004-05-26 17:37:17.000000000 +0200
@@ -20,12 +20,14 @@
 	printf(
 "MARK target v%s options:\n"
 "  --set-mark value                   Set nfmark value\n"
+"  --mask value                       Set modification mask\n"
 "\n",
 IPTABLES_VERSION);
 }
 
 static struct option opts[] = {
 	{ "set-mark", 1, 0, '1' },
+	{ "mask", 1, 0, '2' },
 	{ 0 }
 };
 
@@ -33,6 +35,9 @@
 static void
 init(struct ipt_entry_target *t, unsigned int *nfcache)
 {
+	struct ipt_mark_target_info *markinfo =
+		(struct ipt_mark_target_info *)t->data;
+	markinfo->mask = 0xffffffff;
 }
 
 /* Function which parses command options; returns true if it
@@ -50,10 +55,20 @@
 		if (string_to_number(optarg, 0, 0xffffffff, 
 				     (unsigned int *)&markinfo->mark))
 			exit_error(PARAMETER_PROBLEM, "Bad MARK value `%s'", optarg);
-		if (*flags)
+		if (*flags & 1)
 			exit_error(PARAMETER_PROBLEM,
 			           "MARK target: Can't specify --set-mark twice");
-		*flags = 1;
+		*flags |= 1;
+		break;
+
+	case '2':
+		if (string_to_number(optarg, 0, 0xffffffff, 
+				     (unsigned int *)&markinfo->mask))
+			exit_error(PARAMETER_PROBLEM, "Bad mask value `%s'", optarg);
+		if (*flags & 2)
+			exit_error(PARAMETER_PROBLEM,
+			           "MARK target: Can't specify --mask twice");
+		*flags |= 2;
 		break;
 
 	default:
@@ -66,15 +81,17 @@
 static void
 final_check(unsigned int flags)
 {
-	if (!flags)
+	if ((flags & 1) == 0)
 		exit_error(PARAMETER_PROBLEM,
 		           "MARK target: Parameter --set-mark is required");
 }
 
 static void
-print_mark(unsigned long mark, int numeric)
+print_mark(unsigned long mark, unsigned long mask, int numeric)
 {
 	printf("0x%lx ", mark);
+	if (mask != 0xffffffff)
+		printf(" mask 0x%lx ", mask);
 }
 
 /* Prints out the targinfo. */
@@ -86,7 +103,7 @@
 	const struct ipt_mark_target_info *markinfo =
 		(const struct ipt_mark_target_info *)target->data;
 	printf("MARK set ");
-	print_mark(markinfo->mark, numeric);
+	print_mark(markinfo->mark, markinfo->mask, numeric);
 }
 
 /* Saves the union ipt_targinfo in parsable form to stdout. */
@@ -97,6 +114,8 @@
 		(const struct ipt_mark_target_info *)target->data;
 
 	printf("--set-mark 0x%lx ", markinfo->mark);
+	if (markinfo->mask != 0xffffffff)
+		printf("--mask 0x%lx ", markinfo->mask);
 }
 
 static

[-- Attachment #3: kernel-mark-2.4.26.diff --]
[-- Type: text/plain, Size: 1202 bytes --]

diff -Naur linux-2.4.26-ow1+route/include/linux/netfilter_ipv4/ipt_MARK.h linux-2.4.26-ow1+route+mark/include/linux/netfilter_ipv4/ipt_MARK.h
--- linux-2.4.26-ow1+route/include/linux/netfilter_ipv4/ipt_MARK.h	2000-03-17 19:56:20.000000000 +0100
+++ linux-2.4.26-ow1+route+mark/include/linux/netfilter_ipv4/ipt_MARK.h	2004-05-26 17:10:26.000000000 +0200
@@ -3,6 +3,7 @@
 
 struct ipt_mark_target_info {
 	unsigned long mark;
+	unsigned long mask;
 };
 
 #endif /*_IPT_MARK_H_target*/
diff -Naur linux-2.4.26-ow1+route/net/ipv4/netfilter/ipt_MARK.c linux-2.4.26-ow1+route+mark/net/ipv4/netfilter/ipt_MARK.c
--- linux-2.4.26-ow1+route/net/ipv4/netfilter/ipt_MARK.c	2001-09-30 21:26:08.000000000 +0200
+++ linux-2.4.26-ow1+route+mark/net/ipv4/netfilter/ipt_MARK.c	2004-05-26 17:18:49.000000000 +0200
@@ -16,9 +16,10 @@
        void *userinfo)
 {
 	const struct ipt_mark_target_info *markinfo = targinfo;
+	long new_mark = ((*pskb)->nfmark & ~markinfo->mask) | (markinfo->mark & markinfo->mask);
 
-	if((*pskb)->nfmark != markinfo->mark) {
-		(*pskb)->nfmark = markinfo->mark;
+	if((*pskb)->nfmark != new_mark) {
+		(*pskb)->nfmark = new_mark;
 		(*pskb)->nfcache |= NFC_ALTERED;
 	}
 	return IPT_CONTINUE;

^ permalink raw reply	[flat|nested] 10+ messages in thread
[parent not found: <20040609222646.M64@metal.art.pl>]

end of thread, other threads:[~2004-06-14 20:59 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-05-26 22:09 MARK target extension Grzegorz Nosek
2004-05-27  9:53 ` Patrick McHardy
2004-06-05 22:06   ` Grzegorz Nosek
2004-06-06 14:25     ` Patrick McHardy
     [not found]       ` <20040608073821.M38174@metal.art.pl>
2004-06-09  8:51         ` Patrick McHardy
2004-06-09 13:43           ` Henrik Nordstrom
2004-06-13 20:23             ` Patrick McHardy
2004-06-14 17:26               ` Grzegorz Nosek
2004-06-14 20:59                 ` Henrik Nordstrom
     [not found] <20040609222646.M64@metal.art.pl>
2004-06-10  9:59 ` Henrik Nordstrom

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.