用户登录

iptables的妙用——条件限速

某些路由器具有所谓的流量控制中具有“惩罚性限速”的功能:(例如)192.168.1.100在120秒内平均速率超过100KB/S,那么把该IP列入惩罚队列,惩罚队列速率是40KB/S。其实,利用iptables的也可以实现类似的功能。脚本如下:

代码如下:
  1. iptables -t mangle -N LMT
  2.  
  3. iptables -t mangle -N LMT2
  4.  
  5. iptables -t mangle -I FORWARD -d 192.168.1.100 -m length --length 128: -j LMT
  6.  
  7. iptables -t mangle -A LMT -m recent --rdest --name badguy --rcheck --seconds 60 -j LMT2
  8.  
  9. iptables -t mangle -A LMT -m limit --limit 100/sec --limit-burst 5000 -j RETURN
  10.  
  11. iptables -t mangle -A LMT -m recent --rdest --name badguy --set -j RETURN
  12.  
  13. iptables -t mangle -A LMT2 -m limit --limit 50/sec --limit-burst 5000 -j RETURN
  14.  
  15. iptables -t mangle -A LMT2 -j DROP

上面代码是用limit限制计算速率,为了更准确,使用了数据包长度筛选: “-m length --length 128:”,这样更准确一点。一般限制为100/s,按照数据包平均大小1000Bytes来算,大概就是100KB/S。如果超出,限制变为50/sec,大约50KB/S。

当然,这只是一个示范性的例子了,其中limit模块也可以改用更为强大的hashlimit,hashsped等模块。还可以用connlimit使用连接数作为限制条件。。。。。。。。。。如果再把这些模块结合MARK和TC流量控制,就实现了某些路由器的所谓“条件限速”“P2P惩罚”等功能。

http://linux.chinaunix.net/bbs/thread-1156170-1-1.html

uddtm 2010-02-02

游客评论

发表评论