Jun 23

[原]给iptables增加connlimit模块 晴

linuxing , 16:32 , 网络服务 » 安全相关 , 评论(1) , 引用(0) , 阅读(41368) , Via 本站原创 | |
   使用p2p软件下载的客户端会建立了很多的tcp连接,导致网关负载太重,上网速度很慢。而iptables的connlimit模块,可以限制用户的tcp连接数,以下是给iptables加载模块的步骤和过程。

一、系统平台
引用
操作系统:红旗 DC Server 5.0 for x86_64 sp2
iptables版本:1.3.5

系统自带的iptalbes版本为1.2.1,版本太低,加载connlimit补丁很麻烦。为保持较好的兼容性,我决定使用Asianux 3.0上的1.3.5版本。

二、核心模块编译过程
要为iptables增加connlimit模块,需要给内核及iptables都打补丁。
1、获取补丁
我是参考网上的资料,说只有patch-o-matic-ng-20060725.tar.bz2才拥有connlimit补丁的,所以就直接使用该包了。

# cd /root/iptables_update
# wget http://ftp.netfilter.org/pub/patch-o-matic-ng/snapshot/patch-o-matic-ng-20060725.tar.bz2

以下是Asianux 3.0使用的1.3.5版本的iptables源码:

2、准备工作
把补丁和iptables源码都解压出来:

# rpm -ivh iptables-1.3.5-1.2.1qk.src.rpm
# cd /usr/src/asianux/SOURCES
# tar xjvf iptables-1.3.5.tar.bz2
# cd /root/iptables_update
# tar xjvf patch-o-matic-ng-20060725.tar.bz2


3、打补丁

# cd /root/iptable_update/patch-o-matic-ng-20060725
# KERNEL_DIR=/usr/src/linux-2.6.9-42.7AX/ IPTABLES_DIR=/usr/src/asianux/SOURCES/iptables-1.3.5/ ./runme connlimit

提示如下:
点击在新窗口中浏览此图片
按“y”确认后,补丁完成。

4、引用旧配置文件
根据您使用的核心版本,修改Makefile文件:

# uname -r
2.6.9-42.7AXsmp
# cd /usr/src/linux-2.6.9-42.7AX/
# vi Makefile

把EXTRAVERSION = -42.7AXcustom修改为EXTRAVERSION = -42.7AXsmp
使用当前系统的配置文件:

# cp /boot/config-2.6.9-42.7AXsmp ./.config


5、选择核心模块
运行make menuconfig,选择下面的参数编译为模块<M>:
引用
Device Drivers-->Networking support-->Networking options --->Network packet filtering(replaces ipchains)--->IP: Netfilter Configuration --->Connections/IP limit match support

如图:
点击在新窗口中浏览此图片
退出并保存配置文件。

6、修改源码
前面提到connlimit补丁是针对2.6.11以后的内核版本涉及的,若直接编译会失败,所以我们必须先修改一下ipt_connlimit.c的部分代码。
补丁下载:本地下载
打补丁:

# cd /usr/src/linux-2.6.9-42.7AX/net/ipv4/netfilter
# bzip2 -dc /root/iptable_update/ipt_connlimit_by_qk.patch.bz2 |patch -p1
patching file ipt_connlimit.c


7、编译核心
# make M='net/ipv4/netfilter/'

查看信息:
引用
# modinfo net/ipv4/netfilter/ipt_connlimit.ko
filename:       net/ipv4/netfilter/ipt_connlimit.ko
license:        GPL
depends:        ip_tables,ip_conntrack
vermagic:       2.6.9-42.7AXsmp SMP gcc-3.4

拷贝模块:

# cp net/ipv4/netfilter/ipt_connlimit.ko /lib/modules/2.6.9-42.7AXsmp/kernel/net/ipv4/netfilter/
# chmod u+x /lib/modules/2.6.9-42.7AXsmp/kernel/net/ipv4/netfilter/ipt_connlimit.ko

模块下载:(只适用于红旗DC Server 5.0 sp2)


三、编译iptables过程
使用1.3.5版本直接编译即可:

# cd /usr/src/asianux/SPECS/
# rpmbuild -bb iptables.spec

最后生成的rpm包如下:

源码见标题二。

四、测试
安装上述iptables-1.3.5-1.2.1qk版本后,运行下面的命令进行测试:

# depmod -a
# iptables -m connlimit -help
......(上面信息省略,最后是使用说明)
connlimit v1.3.5 options:
[!] --connlimit-above n         match if the number of existing tcp connections is (not) above n
--connlimit-mask n             group hosts using mask
# iptables -I INPUT -p tcp -m connlimit --connlimit-above 4 -j REJECT

查看:
引用
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
REJECT     tcp  --  anywhere             anywhere            #conn/32 > 4 reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

可见connlimit模块已经加载成功,至此完成。

五、参考资料
http://www.cublog.cn/u/21917/showart.php?id=163921
http://www.sudu.cn/info/html/edu/linux/20080425/302134.html
http://hi.baidu.com/haigang/blog/item/fcf3a40f2c1414e9aa645734.html
一个connlimit模块的RPM包:
ftp://ftp.pslib.cz/pub/users/Milan.Kerslager/RHEL-4/stable/SRPMS/kernel-modules-connlimit-2.6.9_67.0.4.EL-0.20051006.ker.rhel4.src.rpm
使用上述rpm,安装后,可直接开始编译核心,并生成connlimit模块。
Tags: , ,
eyouy
2009/02/20 15:43
我用你的connlimit补丁更新时,提示[root@www netfilter]# patch -p1 < /home/lizhentao/iptables/ipt_connlimit_by_qk.patch
patching file ipt_connlimit.c
Hunk #1 FAILED at 65.
Hunk #2 FAILED at 91.
2 out of 2 hunks FAILED -- saving rejects to file ipt_connlimit.c.rej



请问这个什么意思?
linuxing 回复于 2009/02/21 10:56
你使用的核心版本与我的类同吗?也就是2.6.9.xx版本的。若不是的话,不同版本源码会有差异,那当然打不进patch啦。该patch也只是对2.6.11以下的核心也打,若用的是红旗Asianux 3.x或红帽RHEL 5.x等的核心是2.6.18.xx的,那就不用打这个补丁了。
还有一种方法,就是patch文件,参考里面的内容手动修改对应的源码文件。patch中会告诉你要修改的源码文件名称,- 是要删除的行,+ 是在对应位置新增加的行。
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]