博威---云架构决胜云计算

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1796|回复: 0

netfilter/iptables模块编译及应用

[复制链接]
发表于 2007-7-21 15:39:13 | 显示全部楼层 |阅读模式
netfilter/iptables模块编译及应用


相信很多人都会用iptables,我也一直用,并且天天用.特别是看完 platinum的<<如何给iptables添加新的模块>>介绍后,觉得有必要深入了解一下它的拓展功能.于是立刻下载,先查看一下它的说明, 其功能很是令人感觉很兴奋,例如:comment (备注匹配) ,string(字符串匹配,可以用做内容过滤),iprang(ip范围匹配),time(时间匹配),ipp2p(点对点匹配),connlimit (同时连接个数匹配),Nth(第n个包匹配),geoip(根据国家地区匹配). ipp2p(点对点匹配), quota(配额匹配),还有很多......之后编译,几经测试,在rh7.3 kernel2.4.18-3和rh9.0 kernel2.4.20-8下均成功实现添加扩展功能.以下是介绍其部分功能,及编译方法.环境rh9.0 kernel2.4.20-8. root身份.

一,准备原码.

1. 内核原码:为了减少复杂性,不编译所有内核和模块,建议找一个跟当前版本一样的内核原码,推荐安装时光盘的

a. [root@kindgeorge] uname -r (查看当前版本)

2.4.20-8

可以cd /usr/src 查看是否有这个目录2.4.20-8

b. 或者[root@kindgeorge]rpm -qa|grep kernel

kernel-source-2.4.20-8 如果有这个说明已安装了.

如果没有安装,可以在RH第二张光盘中拷贝过来或安装 rpm -ivh kernel-source-2.4.18-3.i386.rpm. 安装后会在/usr/src/出现linux-2.4连接和linux-2.4.20-8目录.

c.在http://www.kernel.orgwww.redhat.com下载一个和当前版本的内核原码.

2. 先获取最新的信息,当然要到http://www.netfilter.orghttp://www.iptables.org (这两个网址是一样的).

网站左方download栏已经说明了最新版本的iptables-1.3.1

[root@kindgeorge src] cd /usr/src/

a. 获取最新iptables : wget http://www.netfilter.org/files/iptables-1.3.1.tar.bz2

解压:tar xjvf iptables-1.3.1.tar.bz2

b. 获取最新patch-o-matic-ng的地址:ftp://ftp.netfilter.org/pub/patch-o-matic-ng/snapshot/

[root@kindgeorge src] wget ftp://ftp.netfilter.org/pub/patch-o-matic-ng/snapshot/patch-o-matic-ng-20050331.tar.bz2

解压: tar xjvf patch-o-matic-ng-20050331.tar.bz2

二.安装说明

(一).处理内核源码.

1. [root@kindgeorge src]# cd /usr/src/linux-2.4

2. [root@kindgeorge linux-2.4]# vi Makefile,

VERSION = 2

PATCHLEVEL = 4

SUBLEVEL = 20

EXTRAVERSION = -8custom

将“EXTRAVERSION = -8custom”改为“EXTRAVERSION = -8”

即:

VERSION = 2

PATCHLEVEL = 4

SUBLEVEL = 20

EXTRAVERSION = -8

为何要修改呢?因为不同版本的内容是不可以使用的,当不修改时,则变成这个版本(2.4.20-8custom)了,不同时是这样出错的:

/lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o: kernel-module version mismatch (版本错误)

/lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o was compiled for kernel version 2.4.20-8custom

while this kernel is version 2.4.20-8.

/lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o: insmod /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o failed

/lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipt_iprange.o: insmod ipt_iprange failed

3. [root@kindgeorge linux-2.4]# make mrproper

4. [root@kindgeorge linux-2.4]# make oldconfig

'make oldconfig' - 采用以前的 .config 文件 (编译时十分有用)

技巧:在make menuconfig时,我们面对众多的选项常常不知道该如何选择,此时可以把安装时的配置文件copy到/usr/src/linux-2.4中:cp /boot/config-2.4.* /usr/src/linux-2.4/.config,再用make menuconfig编译,它会读取.config中原来的配置信息.

(二).给netfilter打补丁

解开tar xjvf patch-o-matic-ng-20050331.tar.bz2 包后,进入该目录,就会发现有很多目录,其实每个目录对应一个模块.

我们可以这样来选择,根据不同贮仓库submitted|pending|base|extra,例如:

KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme base .

或:KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme extra

执行后,会测试是否已经应用和提示你是否应用该模块,但这样会遍历所有模块,有很多是用不着的,并且可能和系统版本有冲突,如果不管三七二十一全部选择的话,一般都会在编译和使用时出错.所以推荐用cat /模块目录名/info 和cat /模块目录名/help 看过后,认为适合自己,才选择.

我是针对在上面看过后,有目的的一个一个的应用的,这样做:

KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme string

执行后,会测试是否已经应用和提示你是否应用该模块,按"y"应用.然后继续下一个

KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme comment

KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme connlimit

KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme time

KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme iprange

KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme geoip

KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme nth

KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme ipp2p

KERNEL_DIR=/usr/src/linux-2.4 IPTABLES_DIR=/usr/src/iptables-1.3.1 ./runme quota

上面全部完成后,

cd /usr/src/linux-2.4

make menuconfig,确认
  • Prompt for development and/or incomplete code/drivers要选中

    然后进入Networking options

    再进入IP:Netfilter Configuration,会看到增加很多模块,每个新增的后面都会出现"NEW",把其想要的选中为模块"M"

    保存、退出,至此,给netfilter打补丁工作完成

    (三).编译netfilter模块

    1.这里只需要编译netfilter,不需要编译整个内核和模块.这里我只需要ipv4的,ipv6我还没用到,所以不管了

    cd /usr/src/linux-2.4

    make dep

    make modules SUBDIRS=net/ipv4/netfilter

    2.建立一个新目录备份原来模块,以防万一:

    mkdir /usr/src/netfilter

    cp /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/*.o /usr/src/netfilter/

    3.应用新的模块

    cp -f /usr/src/linux-2.4/net/ipv4/netfilter/*.o /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/

    4.更新你的modules.dep

    depmod -a

    当出现这个时,可以不用理会,因为ipchains, ipfwadm模块都没用,也可以把出错的删除.

    depmod: *** Unresolved symbols in /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipchains_core.o

    depmod: *** Unresolved symbols in /lib/modules/2.4.20-8/kernel/net/ipv4/netfilter/ipfwadm_core.o

  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    小黑屋|手机版|Archiver|boway Inc. ( 冀ICP备10011147号 )

    GMT+8, 2024-5-4 15:27 , Processed in 0.087995 second(s), 16 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表