Skip to contentSkip to conversation

limit and negation

DISCUSSION Eric Leblond 3 participants 4 messages Nov 9, 2004 1 message not in the archive

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

Hi,

On a discussion yesterday on #netfilter, someone point out that the limit seems to have problem with negation.
Man page announce it is supported, but when doing this test :
iptables rules set :
iptables -A OUTPUT -m limit --limit 1/s -j LOG --log-prefix "under" iptables -A OUTPUT -m limit ! --limit 1/s -j LOG --log-prefix "over"

It seems that the rules match the same packets (same ID on tcp flows).

I never had to use limit with a negation, but there's at least a problem in the man page.

BR,

Signature · 2 lines
Eric Leblond <[email protected]>
INL
#1 lore raw
Quoted 9 lines · Eric Leblond

Hi,

On a discussion yesterday on #netfilter, someone point out that the limit seems to have problem with negation.
Man page announce it is supported, but when doing this test :
iptables rules set :
iptables -A OUTPUT -m limit --limit 1/s -j LOG --log-prefix "under" iptables -A OUTPUT -m limit ! --limit 1/s -j LOG --log-prefix "over"

Quoted 4 lines

It seems that the rules match the same packets (same ID on tcp flows).

I never had to use limit with a negation, but there's at least a problem in the man page.

Yes, the manual page is wrong. It's not implemented. I'm wondering, that you did not get an error-message...

I attached a patch for it (but it breaks compatibilty, so I think nobody will correct it...)

Regards
Sven

Signature · 9 lines
 Sven Anders <[email protected]>

 ANDURAS service solutions AG
 Innstraße 71 - 94036 Passau - Germany
 Web: www.anduras.de - Tel: +49 (0)851-4 90 50-0 - Fax: +49 (0)851-4 90 50-55

Rechtsform: Aktiengesellschaft - Sitz: Passau - Amtsgericht Passau HRB 6032
Mitglieder des Vorstands: Sven Anders, Marcus Junker, Michael Schön
Vorsitzender des Aufsichtsrats: Dipl. Kfm. Karlheinz Antesberger

# Limit rule can now be inversed.
# So it's possible to match, if the limit is exceeded and drop the packet. #
# Sven Anders <[email protected]>

include/linux/netfilter_ipv4/ipt_limit.h renamed from include/linux/netfilter_ipv4/ipt_limit.h.orig +5 -0
@@ -4,9 +4,14 @@
 /* timings are in milliseconds. */
 #define IPT_LIMIT_SCALE 10000
 
+/* invert match? */
+#define IPT_LIMIT_MATCH_BELOW  0x00
+#define IPT_LIMIT_MATCH_ABOVE  0x01
+
 /* 1/10,000 sec period => max of 10,000/sec.  Min rate is then 429490
    seconds, or one every 59 hours. */
 struct ipt_rateinfo {
+        u_int8_t flags;   /* Match if, below or above limit? */
 	u_int32_t avg;    /* Average secs between packets * scale */
 	u_int32_t burst;  /* Period multiplier for upper limit. */
 
net/ipv4/netfilter/ipt_limit.c renamed from net/ipv4/netfilter/ipt_limit.c.orig +6 -3
@@ -6,7 +6,9 @@
  * 2 September 1999: Changed from the target RATE to the match
  *                   `limit', removed logging.  Did I mention that
  *                   Alexey is a fucking genius?
- *                   Rusty Russell ([email protected]).  */
+ *                   Rusty Russell ([email protected]). 
+ * 8 September 2004: Added inversion, Sven Anders <[email protected]>
+ */
 #include <linux/module.h>
 #include <linux/skbuff.h>
 #include <linux/spinlock.h>
@@ -52,6 +54,7 @@
 		int *hotdrop)
 {
 	struct ipt_rateinfo *r = ((struct ipt_rateinfo *)matchinfo)->master;
+	struct ipt_rateinfo *info = (struct ipt_rateinfo *)matchinfo;
 	unsigned long now = jiffies;
 
 	spin_lock_bh(&limit_lock);
@@ -63,11 +66,11 @@
 		/* We're not limited. */
 		r->credit -= r->cost;
 		spin_unlock_bh(&limit_lock);
-		return 1;
+		return !(info->flags & IPT_LIMIT_MATCH_ABOVE);
 	}
 
        	spin_unlock_bh(&limit_lock);
-	return 0;
+	return (info->flags & IPT_LIMIT_MATCH_ABOVE);
 }
 
 /* Precision saver. */
#3 lore raw
In reply to a message not in the archive
Quoted 5 lines · Nicolas Bouliane

bash-2.05b# iptables -A OUTPUT -m limit --limit ! 1/sec -j LOG --log-prefix "over"
iptables v1.2.11: Unexpected `!' after --limit
Try `iptables -h' or 'iptables --help' for more information. bash-2.05b#

I try this one too, but it should not accept this command as it does now :

Quoted 1 line
Quoted 1 line

iptables -A OUTPUT -m limit ! --limit 1/s -j LOG --log-prefix "over"

BR,

Signature · 2 lines
Eric Leblond <[email protected]>
INL
#4 lore raw
Quoted 3 lines · Eric Leblond

I try this one too, but it should not accept this command as it does now :

Quoted 1 line
Quoted 1 line

iptables -A OUTPUT -m limit ! --limit 1/s -j LOG --log-prefix "over"

Last time I tried, rule was accepted by iptables, bu was acting ignoring inversion statement. So just like you was stating before.

By the way, you can invert limit (as well as any match you may need) using a user chain :

	iptables -N MYLIMIT
	iptables -A MYLIMIT -m limit --limit 1/s -j RETURN
	iptables -A MYLIMIT -j LOG --log-prefix "over"
	iptables -A OUTPUT -j MYLIMIT
Signature · 2 lines
http://www.netexit.com/~sid/
PGP KeyID: 157E98EE FingerPrint: FA62226DA9E72FA8AECAA240008B480E157E98EE
Quoted 2 lines
Quoted 2 lines

Hi! I'm your friendly neighbourhood signature virus. Copy me to your signature file and help me spread!