Skip to contentSkip to conversation

NETFILTER: add support for invert condition (!) in ipt_limit

PATCH In review Marco Innocenti 8 participants 15 messages Oct 17, 2005 – Nov 21, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 NETFILTER: add support for invert condition (!) in ipt_limit

Add support for invert condition (!) in ipt_limit.

---
Hi, this is my first patch and I hope I followed the guideline. The patch change the kernel and iptables. Should I have sent two mail?

include/linux/netfilter_ipv4/ipt_limit.h +1 -0
diff -uprN linux-2.6.13.4/include/linux/netfilter_ipv4/ipt_limit.h linux-2.6.13.4.mio/include/linux/netfilter_ipv4/ipt_limit.h
@@ -9,6 +9,7 @@
 struct ipt_rateinfo {
 	u_int32_t avg;    /* Average secs between packets * scale */
 	u_int32_t burst;  /* Period multiplier for upper limit. */
+	u_int8_t invert;
 
 	/* Used internally by the kernel */
 	unsigned long prev;
net/ipv4/netfilter/ipt_limit.c +2 -2
diff -uprN linux-2.6.13.4/net/ipv4/netfilter/ipt_limit.c linux-2.6.13.4.mio/net/ipv4/netfilter/ipt_limit.c
@@ -82,11 +82,11 @@ ipt_limit_match(const struct sk_buff *sk
 		/* We're not limited. */
 		r->credit -= r->cost;
 		spin_unlock_bh(&limit_lock);
-		return 1;
+		return 1 ^ r->invert;
 	}
 
        	spin_unlock_bh(&limit_lock);
-	return 0;
+	return 0 ^ r->invert;
 }
 
 /* Precision saver. */

+diff -upNr iptables-1.3.3/extensions/libipt_limit.c iptables-1.3.3.mio/extensions/libipt_limit.c

extensions/libipt_limit.c +4 -3
@@ -23,7 +23,7 @@ help(void)
 {
 	printf(
 "limit v%s options:\n"
-"--limit avg			max average match rate: default "IPT_LIMIT_AVG"\n"
+"[!] --limit avg			max average match rate: default "IPT_LIMIT_AVG"\n"
 "                                [Packets per second unless followed by \n"
 "                                /sec /minute /hour /day postfixes]\n"
 "--limit-burst number		number to match in a burst, default %u\n"
@@ -121,8 +121,7 @@ parse(int c, char **argv, int invert, un
 	}
 
 	if (invert)
-		exit_error(PARAMETER_PROBLEM,
-			   "limit does not support invert");
+		r->invert = 1;
 
 	return 1;
 }
@@ -161,6 +160,8 @@ print(const struct ipt_ip *ip,
       int numeric)
 {
 	struct ipt_rateinfo *r = (struct ipt_rateinfo *)match->data;
+	if (r->invert)
+		printf("! ");
 	printf("limit: avg "); print_rate(r->avg);
 	printf("burst %u ", r->burst);
 }
include/linux/netfilter_ipv4/ipt_limit.h +1 -0
diff -upNr iptables-1.3.3/include/linux/netfilter_ipv4/ipt_limit.h iptables-1.3.3.mio/include/linux/netfilter_ipv4/ipt_limit.h
@@ -9,6 +9,7 @@
 struct ipt_rateinfo {
 	u_int32_t avg;    /* Average secs between packets * scale */
 	u_int32_t burst;  /* Period multiplier for upper limit. */
+	u_int8_t invert;
 
 #ifdef KERNEL_64_USERSPACE_32
 	u_int64_t prev;
Signature · 2 lines
Ciao
     Marco Innocenti
#1 lore raw

Hello.

In article <[email protected]> (at Mon, 17 Oct 2005 11:44:17 +0200), Marco Innocenti <[email protected]> says:

net/ipv4/netfilter/ipt_limit.c ipt_limit_match(const struct sk_buff *sk · line 86

Quoted 9 lines
net/ipv4/netfilter/ipt_limit.c +1 -1
@@ -82,11 +82,11 @@ ipt_limit_match(const struct sk_buff *sk
 		/* We're not limited. */
 		r->credit -= r->cost;
 		spin_unlock_bh(&limit_lock);
-		return 1;
+		return 1 ^ r->invert;
 	}

!r->invert

net/ipv4/netfilter/ipt_limit.c ipt_limit_match(const struct sk_buff *sk · line 89

Quoted 4 lines
net/ipv4/netfilter/ipt_limit.c +1 -1
        	spin_unlock_bh(&limit_lock);
-	return 0;
+	return 0 ^ r->invert;

!!r->invert

--yoshfuji

#2 lore raw
Quoted 4 lines · On Mon, Oct 17, 2005 at 06:45:47PM +0900, YOSHIFUJI Hideaki / ?$B5HF#1QL@ wrote:

In article <[email protected]> (at Mon, 17 Oct 2005 11:44:17 +0200), Marco Innocenti <[email protected]> says:

Review

Quoted 2 lines
(quoted diff) +1 -1
-		return 1;
+		return 1 ^ r->invert;

!r->invert

I've seen it in ipt_multiport.c and I copied it.
Should ipt_multiport.c be corrected?

---

include/linux/netfilter_ipv4/ipt_limit.h +1 -0
diff -uprN linux-2.6.13.4/include/linux/netfilter_ipv4/ipt_limit.h linux-2.6.13.4.mio/include/linux/netfilter_ipv4/ipt_limit.h
@@ -9,6 +9,7 @@
 struct ipt_rateinfo {
 	u_int32_t avg;    /* Average secs between packets * scale */
 	u_int32_t burst;  /* Period multiplier for upper limit. */
+	u_int8_t invert;
 
 	/* Used internally by the kernel */
 	unsigned long prev;
net/ipv4/netfilter/ipt_limit.c +2 -2
diff -uprN linux-2.6.13.4/net/ipv4/netfilter/ipt_limit.c linux-2.6.13.4.mio/net/ipv4/netfilter/ipt_limit.c
@@ -82,11 +82,11 @@ ipt_limit_match(const struct sk_buff *sk
 		/* We're not limited. */
 		r->credit -= r->cost;
 		spin_unlock_bh(&limit_lock);
-		return 1;
+		return !r->invert;
 	}
 
        	spin_unlock_bh(&limit_lock);
-	return 0;
+	return r->invert;
 }
 
 /* Precision saver. */
extensions/libipt_limit.c +4 -3
diff -upNr iptables-1.3.3/extensions/libipt_limit.c iptables-1.3.3.mio/extensions/libipt_limit.c
@@ -23,7 +23,7 @@ help(void)
 {
 	printf(
 "limit v%s options:\n"
-"--limit avg			max average match rate: default "IPT_LIMIT_AVG"\n"
+"[!] --limit avg			max average match rate: default "IPT_LIMIT_AVG"\n"
 "                                [Packets per second unless followed by \n"
 "                                /sec /minute /hour /day postfixes]\n"
 "--limit-burst number		number to match in a burst, default %u\n"
@@ -121,8 +121,7 @@ parse(int c, char **argv, int invert, un
 	}
 
 	if (invert)
-		exit_error(PARAMETER_PROBLEM,
-			   "limit does not support invert");
+		r->invert = 1;
 
 	return 1;
 }
@@ -161,6 +160,8 @@ print(const struct ipt_ip *ip,
       int numeric)
 {
 	struct ipt_rateinfo *r = (struct ipt_rateinfo *)match->data;
+	if (r->invert)
+		printf("! ");
 	printf("limit: avg "); print_rate(r->avg);
 	printf("burst %u ", r->burst);
 }
include/linux/netfilter_ipv4/ipt_limit.h +1 -0
diff -upNr iptables-1.3.3/include/linux/netfilter_ipv4/ipt_limit.h iptables-1.3.3.mio/include/linux/netfilter_ipv4/ipt_limit.h
@@ -9,6 +9,7 @@
 struct ipt_rateinfo {
 	u_int32_t avg;    /* Average secs between packets * scale */
 	u_int32_t burst;  /* Period multiplier for upper limit. */
+	u_int8_t invert;
 
 #ifdef KERNEL_64_USERSPACE_32
 	u_int64_t prev;
Signature · 2 lines
Ciao
     Marco Innocenti
#3 lore raw
In reply to YOSHIFUJI Hideaki / 吉藤英明
Quoted 3 lines · On Mon, Oct 17, 2005 at 06:45:47PM +0900, YOSHIFUJI Hideaki / ?$B5HF#1QL@ wrote:

Review

Quoted 2 lines
(quoted diff) +1 -1
-		return 1;
+		return 1 ^ r->invert;

!r->invert

I've seen it in ipt_multiport.c and I copied it.
Should ipt_multiport.c be corrected?

---

include/linux/netfilter_ipv4/ipt_limit.h +1 -0
diff -uprN linux-2.6.13.4/include/linux/netfilter_ipv4/ipt_limit.h linux-2.6.13.4.mio/include/linux/netfilter_ipv4/ipt_limit.h
@@ -9,6 +9,7 @@
 struct ipt_rateinfo {
 	u_int32_t avg;    /* Average secs between packets * scale */
 	u_int32_t burst;  /* Period multiplier for upper limit. */
+	u_int8_t invert;
 
 	/* Used internally by the kernel */
 	unsigned long prev;
net/ipv4/netfilter/ipt_limit.c +2 -2
diff -uprN linux-2.6.13.4/net/ipv4/netfilter/ipt_limit.c linux-2.6.13.4.mio/net/ipv4/netfilter/ipt_limit.c
@@ -82,11 +82,11 @@ ipt_limit_match(const struct sk_buff *sk
 		/* We're not limited. */
 		r->credit -= r->cost;
 		spin_unlock_bh(&limit_lock);
-		return 1;
+		return !r->invert;
 	}
 
        	spin_unlock_bh(&limit_lock);
-	return 0;
+	return r->invert;
 }
 
 /* Precision saver. */
extensions/libipt_limit.c +4 -3
diff -upNr iptables-1.3.3/extensions/libipt_limit.c iptables-1.3.3.mio/extensions/libipt_limit.c
@@ -23,7 +23,7 @@ help(void)
 {
 	printf(
 "limit v%s options:\n"
-"--limit avg			max average match rate: default "IPT_LIMIT_AVG"\n"
+"[!] --limit avg			max average match rate: default "IPT_LIMIT_AVG"\n"
 "                                [Packets per second unless followed by \n"
 "                                /sec /minute /hour /day postfixes]\n"
 "--limit-burst number		number to match in a burst, default %u\n"
@@ -121,8 +121,7 @@ parse(int c, char **argv, int invert, un
 	}
 
 	if (invert)
-		exit_error(PARAMETER_PROBLEM,
-			   "limit does not support invert");
+		r->invert = 1;
 
 	return 1;
 }
@@ -161,6 +160,8 @@ print(const struct ipt_ip *ip,
       int numeric)
 {
 	struct ipt_rateinfo *r = (struct ipt_rateinfo *)match->data;
+	if (r->invert)
+		printf("! ");
 	printf("limit: avg "); print_rate(r->avg);
 	printf("burst %u ", r->burst);
 }
include/linux/netfilter_ipv4/ipt_limit.h +1 -0
diff -upNr iptables-1.3.3/include/linux/netfilter_ipv4/ipt_limit.h iptables-1.3.3.mio/include/linux/netfilter_ipv4/ipt_limit.h
@@ -9,6 +9,7 @@
 struct ipt_rateinfo {
 	u_int32_t avg;    /* Average secs between packets * scale */
 	u_int32_t burst;  /* Period multiplier for upper limit. */
+	u_int8_t invert;
 
 #ifdef KERNEL_64_USERSPACE_32
 	u_int64_t prev;
Signature · 2 lines
Ciao
     Marco Innocenti
#4 lore raw
Quoted 10 lines · Marco Innocenti
Quoted 4 lines · On Mon, Oct 17, 2005 at 06:45:47PM +0900, YOSHIFUJI Hideaki / ?$B5HF#1QL@ wrote:

Review

Quoted 2 lines
(quoted diff) +1 -1
-		return 1;
+		return 1 ^ r->invert;

!r->invert

I've seen it in ipt_multiport.c and I copied it.
Should ipt_multiport.c be corrected?

Probably, I've never liked these strange looking expressions.

include/linux/netfilter_ipv4/ipt_limit.h line 15

Quoted 11 lines

diff -uprN linux-2.6.13.4/include/linux/netfilter_ipv4/ipt_limit.h linux-2.6.13.4.mio/include/linux/netfilter_ipv4/ipt_limit.h

include/linux/netfilter_ipv4/ipt_limit.h +1 -0
@@ -9,6 +9,7 @@
 struct ipt_rateinfo {
 	u_int32_t avg;    /* Average secs between packets * scale */
 	u_int32_t burst;  /* Period multiplier for upper limit. */
+	u_int8_t invert;
 
 	/* Used internally by the kernel */
 	unsigned long prev;

This breaks userspace compatibility, you need to use a new revision if you want to change this structure. Look at ipt_MARK for an example.

#5 lore raw
Quoted 4 lines · Patrick McHardy
Quoted 2 lines

I've seen it in ipt_multiport.c and I copied it.
Should ipt_multiport.c be corrected?

Probably, I've never liked these strange looking expressions.

Multiport invert works fine (I added/tested it). No sense in changing it just based on looks.

Phil

#6 lore raw
Quoted 6 lines · Phil Oester
Quoted 1 line · Patrick McHardy

Probably, I've never liked these strange looking expressions.

Multiport invert works fine (I added/tested it). No sense in changing it just based on looks.

Yes, this wouldn't be a fix but a cleanup patch. But actually it looks fine, I think it was some ip6_tables matches that had these weird looking expressions - although they also seem to be cleaned up already.

#7 lore raw
In reply to Patrick McHardy

Hi Patrick,

On Mon, Oct 17, 2005 at 05:15:07PM +0200, Patrick McHardy told us:

Quoted 1 line

Probably, I've never liked these strange looking expressions.

...

Quoted 2 lines

This breaks userspace compatibility, you need to use a new revision if you want to change this structure. Look at ipt_MARK for an example.

I know it's been a while since netfilter/iptables has been developed, but why wasn't isn't the inversion built into the netfilter/iptables core?? Inversion can be useful for any kind of match, so why duplicate it in every single match?? Why not instead do a general inversion?? If I haven't thought of something obvious please cat this post to /dev/null :-) (but pleas enlighten me before doing so *g*)

have a nice day :-)

Sven

Signature · 2 lines
Linux zion.homelinux.com 2.6.14-rc4-mm1_10 #10 Mon Oct 17 10:47:30 CEST 2005 i686 athlon i386 GNU/Linux
 23:32:03 up  3:03,  2 users,  load average: 0.04, 0.05, 0.00
#8 lore raw
Quoted 3 lines · Sven Schuster

I know it's been a while since netfilter/iptables has been developed, but why wasn't isn't the inversion built into the netfilter/iptables core??

Mainly because the way iptables is designed makes it extremely hard to extend the core with new attributes without breaking compatibility with userspace (which is not allowed), while doing it in the matches is very simple even if it leads to duplicated code.

Regards
Henrik

#9 lore raw

Hi Henrik,

On Tue, Oct 18, 2005 at 10:33:18AM +0200, Henrik Nordstrom told us:

Quoted 4 lines

Mainly because the way iptables is designed makes it extremely hard to extend the core with new attributes without breaking compatibility with userspace (which is not allowed), while doing it in the matches is very simple even if it leads to duplicated code.

yes that's what I guessed that it can't be done now due to userspace compatibility issues. But apart from that, it would have been a better solution if it had been implemented in the core from "day 1" on, wouldn't it??

Thanks,

Sven

Quoted 3 lines

Regards
Henrik

Signature · 2 lines
Linux zion.homelinux.com 2.6.14-rc4-mm1_10 #10 Mon Oct 17 10:47:30 CEST 2005 i686 athlon i386 GNU/Linux
 11:16:33 up 14:47,  1 user,  load average: 4.79, 4.26, 2.56
#10 lore raw
Quoted 1 line · Sven Schuster

yes that's what I guessed that it can't be done now due to userspace compatibility issues. But apart from that, it would have been a better solution if it had been implemented in the core from "day 1" on, wouldn't it??

I think so yes.

And likely to be addressed for hipac in the upcoming hipac function matches.

Regards
Henrik

#11 lore raw
In reply to Sven Schuster
Quoted 6 lines · Sven Schuster

I know it's been a while since netfilter/iptables has been developed, but why wasn't isn't the inversion built into the netfilter/iptables core?? Inversion can be useful for any kind of match, so why duplicate it in every single match?? Why not instead do a general inversion?? If I haven't thought of something obvious please cat this post to /dev/null :-) (but pleas enlighten me before doing so *g*)

Yes, that would have been a better idea. Unfortunately we can't change this now because of userspace compatiblity.

#12 lore raw
In reply to Patrick McHardy
Quoted 2 lines · Patrick McHardy

This breaks userspace compatibility, you need to use a new revision if you want to change this structure. Look at ipt_MARK for an example.

---

include/linux/netfilter_ipv4/ipt_limit.h +17 -1
diff -urpN linux-source-2.6.13/include/linux/netfilter_ipv4/ipt_limit.h linux-source-2.6.13.lavoro/include/linux/netfilter_ipv4/ipt_limit.h
@@ -3,9 +3,10 @@
 
 /* timings are in milliseconds. */
 #define IPT_LIMIT_SCALE 10000
-
 /* 1/10,000 sec period => max of 10,000/sec.  Min rate is then 429490
    seconds, or one every 59 hours. */
+
+/* Version 0 */
 struct ipt_rateinfo {
 	u_int32_t avg;    /* Average secs between packets * scale */
 	u_int32_t burst;  /* Period multiplier for upper limit. */
@@ -18,4 +19,19 @@ struct ipt_rateinfo {
 	/* Ugly, ugly fucker. */
 	struct ipt_rateinfo *master;
 };
+
+/* Version 1 */
+struct ipt_rateinfo_v1 {
+	u_int32_t avg;    /* Average secs between packets * scale */
+	u_int32_t burst;  /* Period multiplier for upper limit. */
+	u_int8_t invert;
+
+	/* Used internally by the kernel */
+	unsigned long prev;
+	u_int32_t credit;
+	u_int32_t credit_cap, cost;
+
+	/* Ugly, ugly fucker. */
+	struct ipt_rateinfo_v1 *master;
+};
 #endif /*_IPT_RATE_H*/
net/ipv4/netfilter/ipt_limit.c +88 -9

131 lines not on this page · raw message

extensions/libipt_limit.c +120 -13

201 lines not on this page · raw message

include/linux/netfilter_ipv4/ipt_limit.h +23 -1
diff -upr iptables-1.3.3.orig/include/linux/netfilter_ipv4/ipt_limit.h iptables-1.3.3/include/linux/netfilter_ipv4/ipt_limit.h
@@ -3,9 +3,10 @@
 
 /* timings are in milliseconds. */
 #define IPT_LIMIT_SCALE 10000
-
 /* 1/10,000 sec period => max of 10,000/sec.  Min rate is then 429490
    seconds, or one every 59 hours. */
+
+/* Version 0 */
 struct ipt_rateinfo {
 	u_int32_t avg;    /* Average secs between packets * scale */
 	u_int32_t burst;  /* Period multiplier for upper limit. */
@@ -23,4 +24,25 @@ struct ipt_rateinfo {
 	u_int32_t credit;
 	u_int32_t credit_cap, cost;
 };
+
+/* Version 1 */
+struct ipt_rateinfo_v1 {
+	u_int32_t avg;    /* Average secs between packets * scale */
+	u_int32_t burst;  /* Period multiplier for upper limit. */
+        u_int8_t invert;
+
+#ifdef KERNEL_64_USERSPACE_32
+	u_int64_t prev;
+	u_int64_t placeholder;
+#else
+	/* Used internally by the kernel */
+	unsigned long prev;
+	/* Ugly, ugly fucker. */
+	struct ipt_rateinfo_v1 *master;
+#endif
+
+	u_int32_t credit;
+	u_int32_t credit_cap, cost;
+};
+
 #endif /*_IPT_RATE_H*/
Signature · 2 lines
Ciao
     Marco Innocenti
#13 lore raw

There is something else I have to do to make the patch to add support for invert in ipt_limit being applied?

Signature · 6 lines
**********************************************************************
Marco Innocenti              Gruppo Infrastruttura e Sicurezza
CINECA                       phone:+39 0516171553 / fax:+39 0516132198
Via Magnanelli 6/3           e-mail: [email protected]
40033 Casalecchio di Reno    Bologna (Italia)
**********************************************************************
#14 lore raw
Quoted 2 lines · [email protected] wrote:

There is something else I have to do to make the patch to add support for invert in ipt_limit being applied?

Wait until someone realises that it's there. Otherwise you could resend it. About your patch, I must confess that I don't like too much the idea of adding new versions of targets/matches for such small modifications, but we have no other choice to implement that new feature.

Signature · 1 line
Pablo