Skip to contentSkip to conversation

netfilter: iptables-restore does not work as expected with xt_hashlimit

PATCH Superseded Vishwanath Pai 1 participant 1 message Jun 2, 2016

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 netfilter: iptables-restore does not work as expected with xt_hashlimit

netfilter: iptables-restore does not work as expected with xt_hashlimit

Add the following iptables rule.

$ iptables -A INPUT -m hashlimit --hashlimit-above 200/sec \
  --hashlimit-burst 5 --hashlimit-mode srcip --hashlimit-name hashlimit1 \
  --hashlimit-htable-expire 30000 -j DROP
$ iptables-save > save.txt

Edit save.txt and change the value of --hashlimit-above to 300:

-A INPUT -m hashlimit --hashlimit-above 300/sec --hashlimit-burst 5 \
--hashlimit-mode srcip --hashlimit-name hashlimit2 \
--hashlimit-htable-expire 30000 -j DROP

Now restore save.txt

$ iptables-restore < save.txt

Now userspace thinks that the value of --hashlimit-above is 300 but it is actually 200 in the kernel. This happens because when we add multiple hash-limit rules with the same name they will share the same hashtable internally. The kernel module tries to re-use the old hashtable without updating the values.

There are multiple problems here:
1) We can add two iptables rules with the same name, but kernel does not handle this well, one procfs file cannot work with two rules
2) If the second rule has no effect because the hashtable has values from rule 1
3) hashtable-restore does not work (as described above)

To fix this I have made the following design change:
1) If a second rule is added with the same name as an existing rule, append a number when we create the procfs, for example hashlimit_1, hashlimit_2 etc
2) Two rules will not share the same hashtable unless they are similar in every possible way
3) This behavior has to be forced with a new userspace flag:
--hashlimit-ehanced-procfs, if this flag is not passed we default to the old behavior. This is to make sure we do not break existing scripts that rely on the existing behavior.

include/uapi/linux/netfilter/xt_hashlimit.h +7 -0
index be5d2e1..74444c7 100644
@@ -18,6 +18,10 @@
 struct xt_hashlimit_htable;
 
 enum {
+	XT_HASHLIMIT_FLAG_PROCFS = 1
+};
+
+enum {
 	XT_HASHLIMIT_HASH_DIP = 1 << 0,
 	XT_HASHLIMIT_HASH_DPT = 1 << 1,
 	XT_HASHLIMIT_HASH_SIP = 1 << 2,
@@ -76,6 +80,9 @@ struct hashlimit_cfg2 {
 	__u32 expire;	/* when do entries expire? */
 
 	__u8 srcmask, dstmask;
+
+	__u8 procfs_suffix;
+	__u32 flags;
 };
 
 struct xt_hashlimit_mtinfo1 {
net/netfilter/xt_hashlimit.c +126 -10

203 lines not on this page · raw message