Skip to contentSkip to conversation

2.4 kernels and max # of rules with iptables

DISCUSSION Jason Wever 4 participants 11 messages Jun 20, 2005 – Aug 22, 2005

lore
Share & tools
lore
#0 lore raw

Hi All,

I don't know if anyone else has run into this or not, but over in Gentoo we've had a user report[1] of a "hard limit" as to the number of rules you can have with iptables on the 2.4 kernels before it starts erroring out. Currently, it seems that the limit is 857 rules, and the error given is "iptables: Memory allocation problem".

I've been able to confirm this behavior on 2.4.31 (using iptables 1.2.11 and 1.3.1) and was able to load more than 10,000 rules in 2.6.12-rc3 before I gave up.

Current Gentoo userland is using either kernel headers from 2.4.23 (for stable keywords) and 2.4.26 (for testing keywords).

If anyone has any suggestions or pointers, I'd be glad to hear them.

[1] - https://bugs.gentoo.org/show_bug.cgi?id=75668

Thanks,

Signature · 2 lines
Jason Wever
Gentoo/Sparc Team Co-Lead
#1 lore raw

From: Jason Wever <[email protected]>
Date: Sun, 19 Jun 2005 20:07:12 -0600

Quoted 3 lines

I've been able to confirm this behavior on 2.4.31 (using iptables 1.2.11 and 1.3.1) and was able to load more than 10,000 rules in 2.6.12-rc3 before I gave up.

64-bit or 32-bit userland binaries?

He could be hitting the kmalloc() limit via the netfilter 32-bit userland compat code in:

arch/sparc64/kernel/sys_sparc32.c:do_netfilter_replace()

You have to make sure you replicate the netfilter commands his system is running, in order to determine where the limit is actually coming from.

#2 lore raw
In reply to Jason Wever
Quoted 1 line · On Sun, 19 Jun 2005 19:20:34 -0700 (PDT) "David S. Miller"…

64-bit or 32-bit userland binaries?

32 bit userland binaries. Currently we don't have a working 64 bit environment that is suggested for general use.

Quoted 8 lines

He could be hitting the kmalloc() limit via the netfilter 32-bit userland compat code in:

arch/sparc64/kernel/sys_sparc32.c:do_netfilter_replace()

You have to make sure you replicate the netfilter commands his system is running, in order to determine where the limit is actually coming from.

I used a C program[1] written by Gustavo Zacarias to replicate here and I'll post a follow-up once I get a response from the user.

[1] - http://dev.gentoo.org/~gustavoz/source/iptables_test.c

Thanks,

Signature · 2 lines
Jason Wever
Gentoo/Sparc Team Co-Lead
#4 lore raw
In reply to Jason Wever

Good Morning,

I threw a little printk patch at Jason and we determined that to be correct. In do_netfilter_replace(), on about line 2924, the call

        krepl = (struct ipt_replace *)kmalloc(kreplsize, GFP_KERNEL);

results in krepl being NULL after a pile of rules have been added.

I compiled a statically linked iptables binary and Jason was able to add over 7k rules before he stopped it, the 32 bit iptables bombs before 900 rules.

As far as changing this behaviour, I plead ignorance.

Thanks,

Josh

Quoted 16 lines · Jason Wever
Quoted 1 line · On Sun, 19 Jun 2005 19:20:34 -0700 (PDT) "David S. Miller"…

64-bit or 32-bit userland binaries?

32 bit userland binaries. Currently we don't have a working 64 bit environment that is suggested for general use.

Quoted 5 lines

He could be hitting the kmalloc() limit via the netfilter 32-bit userland compat code in:

arch/sparc64/kernel/sys_sparc32.c:do_netfilter_replace()

#5 lore raw
In reply to Jason Wever

From: Josh Grebe <[email protected]>
Date: Wed, 17 Aug 2005 08:06:06 -0500

Quoted 10 lines

I threw a little printk patch at Jason and we determined that to be correct. In do_netfilter_replace(), on about line 2924, the call

        krepl = (struct ipt_replace *)kmalloc(kreplsize, GFP_KERNEL);

results in krepl being NULL after a pile of rules have been added.

I compiled a statically linked iptables binary and Jason was able to add over 7k rules before he stopped it, the 32 bit iptables bombs before 900 rules.

As far as changing this behaviour, I plead ignorance.

So how large was kreplsize in the failing call?

#6 lore raw
In reply to Jason Wever

I guess that would help...

kmalloc failed, size of kreplsize is 939608

Quoted 24 lines · David S. Miller

From: Josh Grebe <[email protected]>
Date: Wed, 17 Aug 2005 08:06:06 -0500

Quoted 10 lines

I threw a little printk patch at Jason and we determined that to be correct. In do_netfilter_replace(), on about line 2924, the call

       krepl = (struct ipt_replace *)kmalloc(kreplsize, GFP_KERNEL);

results in krepl being NULL after a pile of rules have been added.

I compiled a statically linked iptables binary and Jason was able to add over 7k rules before he stopped it, the 32 bit iptables bombs before 900 rules.

As far as changing this behaviour, I plead ignorance.

So how large was kreplsize in the failing call?
-
To unsubscribe from this list: send the line "unsubscribe sparclinux" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

!DSPAM:43038246130492096615112!

#7 lore raw
In reply to Jason Wever

From: Josh Grebe <[email protected]>
Subject: Re: 2.4 kernels and max # of rules with iptables
Date: Wed, 17 Aug 2005 13:53:38 -0500

Quoted 3 lines

I guess that would help...

kmalloc failed, size of kreplsize is 939608

Yes, when running 32-bit programs on a 64-bit kernel, there will be a hard limit of how many ipt_replace entries can be loaded because we have to copy the user's rule set into a kernel copy and therefore we have to allocate kmalloc() memory to hold that.

You _could_ experiment with making the code there use vmalloc()/vfree() instead, and if you get it working cleanly I'd be happy to add such a patch to fix this.

#8 lore raw
In reply to Jason Wever
Quoted 3 lines · David S. Miller

You _could_ experiment with making the code there use vmalloc()/vfree() instead, and if you get it working cleanly I'd be happy to add such a patch to fix this.

Tested with 5000+ rules.
Does it look clean enough?

Signature · 2 lines
Gustavo Zacarias
Gentoo/SPARC monkey
arch/sparc64/kernel/sys_sparc32.c +6 -5
diff -Nura linux-2.4.31/arch/sparc64/kernel/sys_sparc32.c linux-2.4.31.netfilter/arch/sparc64/kernel/sys_sparc32.c
@@ -50,6 +50,7 @@
 #include <linux/in.h>
 #include <linux/icmpv6.h>
 #include <linux/sysctl.h>
+#include <linux/vmalloc.h>
 #include <linux/dnotify.h>
 #include <linux/netfilter_ipv4/ip_tables.h>
 
@@ -2919,12 +2920,12 @@
 	if (optlen != kreplsize)
 		return -ENOPROTOOPT;
 
-	krepl = (struct ipt_replace *)kmalloc(kreplsize, GFP_KERNEL);
+	krepl = (struct ipt_replace *)vmalloc(kreplsize);
 	if (krepl == NULL)
 		return -ENOMEM;
 
 	if (copy_from_user(krepl, optval, kreplsize)) {
-		kfree(krepl);
+		vfree(krepl);
 		return -EFAULT;
 	}
 
@@ -2935,7 +2936,7 @@
 	krepl->counters = (struct ipt_counters *)kmalloc(
 					kcountersize, GFP_KERNEL);
 	if (krepl->counters == NULL) {
-		kfree(krepl);
+		vfree(krepl);
 		return -ENOMEM;
 	}
 
@@ -2949,8 +2950,8 @@
 		copy_to_user(counters32, krepl->counters, kcountersize))
 			ret = -EFAULT;
 
-	kfree(krepl->counters);
-	kfree(krepl);
+	vfree(krepl->counters);
+	vfree(krepl);
 
 	return ret;
 }
#9 lore raw
In reply to Jason Wever

From: Gustavo Zacarias <[email protected]>
Date: Mon, 22 Aug 2005 14:10:28 -0300

Quoted 8 lines
Quoted 3 lines · David S. Miller

You _could_ experiment with making the code there use vmalloc()/vfree() instead, and if you get it working cleanly I'd be happy to add such a patch to fix this.

Tested with 5000+ rules.
Does it look clean enough?

Looks good to me, I'll pass this along to the netfilter maintainers.

#10 lore raw
In reply to Jason Wever
Quoted 1 line · David S. Miller

Looks good to me, I'll pass this along to the netfilter maintainers.

Actually i'm still doing a kmalloc for replsize->counter, that may work, but since we're cleaning up it should be a vmalloc too (specially since i'm doing a vfree).
Second time is the charm :)

Signature · 2 lines
Gustavo Zacarias
Gentoo/SPARC monkey
arch/sparc64/kernel/sys_sparc32.c +7 -7
diff -Nura linux-2.4.31/arch/sparc64/kernel/sys_sparc32.c linux-2.4.31.netfilter/arch/sparc64/kernel/sys_sparc32.c
@@ -50,6 +50,7 @@
 #include <linux/in.h>
 #include <linux/icmpv6.h>
 #include <linux/sysctl.h>
+#include <linux/vmalloc.h>
 #include <linux/dnotify.h>
 #include <linux/netfilter_ipv4/ip_tables.h>
 
@@ -2919,12 +2920,12 @@
 	if (optlen != kreplsize)
 		return -ENOPROTOOPT;
 
-	krepl = (struct ipt_replace *)kmalloc(kreplsize, GFP_KERNEL);
+	krepl = (struct ipt_replace *)vmalloc(kreplsize);
 	if (krepl == NULL)
 		return -ENOMEM;
 
 	if (copy_from_user(krepl, optval, kreplsize)) {
-		kfree(krepl);
+		vfree(krepl);
 		return -EFAULT;
 	}
 
@@ -2932,10 +2933,9 @@
 		((struct ipt_replace32 *)krepl)->counters);
 
 	kcountersize = krepl->num_counters * sizeof(struct ipt_counters);
-	krepl->counters = (struct ipt_counters *)kmalloc(
-					kcountersize, GFP_KERNEL);
+	krepl->counters = (struct ipt_counters *)vmalloc(kcountersize);
 	if (krepl->counters == NULL) {
-		kfree(krepl);
+		vfree(krepl);
 		return -ENOMEM;
 	}
 
@@ -2949,8 +2949,8 @@
 		copy_to_user(counters32, krepl->counters, kcountersize))
 			ret = -EFAULT;
 
-	kfree(krepl->counters);
-	kfree(krepl);
+	vfree(krepl->counters);
+	vfree(krepl);
 
 	return ret;
 }