添加新规则时,iptables超过配额

我有iptables在Centos 7上工作,使用v1.4.21版本,但也testing了v1.6.0(请注意,我没有重build内核,因为它说我不再需要扩展)。

我build立了一个配额,并被使用:

# iptables -nvx -L 192.168.2.5 Chain 192.168.2.5 (2 references) pkts bytes target prot opt in out source destination 3639 3999378 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 quota: 4000000 bytes 142 175468 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 # 

然后,当我添加任何其他规则到这个链,现有的规则“重置”字节的使用,并再次用尽配额:

 # iptables -I 192.168.2.5 -m quota --quota 1000 -j ACCEPT # iptables -nvx -L 192.168.2.5 Chain 192.168.2.5 (2 references) pkts bytes target prot opt in out source destination 2 168 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 quota: 1000 bytes 7239 7998334 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 quota: 4000000 bytes 890 387931 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 

即使没有超过,这种行为总是会增加规则的配额量,即使我正在影响一个不同的规则:

 # iptables -nvx -L 192.168.2.5 Chain 192.168.2.5 (2 references) pkts bytes target prot opt in out source destination 379 67755 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 quota: 4000000 bytes 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 # iptables -I 192.168.2.5 -m quota --quota 1000 -j ACCEPT # iptables -nvx -L 192.168.2.5 Chain 192.168.2.5 (2 references) pkts bytes target prot opt in out source destination 2 168 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 quota: 1000 bytes 379 67755 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 quota: 4000000 bytes 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 # iptables -nvx -L 192.168.2.5 Chain 192.168.2.5 (2 references) pkts bytes target prot opt in out source destination 11 924 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 quota: 1000 bytes 4159 4066453 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 quota: 4000000 bytes 315 190056 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 

这似乎是一个错误,也许和这个有关。

有任何想法吗? 我的一个解决方法是自己捕获字节并将其添加到新规则的配额中。 当它已经被超过时,这个效果很好,但是如果没有的话,由于阅读,计算,删除和添加之间的差距,我可能会错过字节。

阅读你链接和testing的另一个问题,我只能得出结论:配额模块不是非常有用:只要有变化就重置。

这当然是为什么还有一个叫做quota2的模块! 这不是iptables的一部分,而是xtables-addons的一部分。 在Debian中,它可以通过xtables-addons-dkms进行编译。 我想你将不得不在CentOS7中编译它。

手册页的三个摘录(可以在这里find: xtables-addons.8 )

计数器的值可以通过procfs读取和重置,从而使这个匹配极简主义的会计工具。

– 名字的名字
为计数器指定一个特定的名称。 这个选项必须存在,

配额出现在/ proc / net / xt_quota / name中,并且是可读/写的

– 配额iq
指定此计数器的初始配额。 如果计数器已经存在,则不会重置。

这意味着必须使用iptables本身之外的一些逻辑(例如,如果必须重新启动服务器,请保存剩余的配额并在启动时恢复),但是这肯定能解决您的问题。