Chinaunix

标题: [原创]在2.6内核下不编译内核给iptables添加模块 [打印本页]

作者: jiecho    时间: 2005-08-11 14:31
标题: [原创]在2.6内核下不编译内核给iptables添加模块
看了platinum兄弟写的在2.4内核下给iptables添加模块,想在2.6的内核下也试试,经过一天半的努力,并参考platinum兄弟的文章,
终于成功,写下来跟CU的兄弟们分享,我的系统为fedora2+linux-2.6.5-1.358

1,安装kernel源码
[root@jiecho]# yum install kernel-source

2,下载iptables-1.3.3.tar.bz2和patch-o-matic-ng-20050810.tar.bz2并解包,为了方便,我们都放在/usr/src下,
并把kernel和iptables做符号链接.
[root@jiecho]# cd /usr/src
[root@jiecho]# wget ftp://ftp.be.netfilter.org/pub/netfilter/iptables/iptables-1.3.3.tar.bz2
[root@jiecho]# wget ftp://ftp.be.netfilter.org/pub/netfilter/patch-o-matic-ng/snapshot/patch-o-matic-ng-20050810.tar.bz2
[root@jiecho]# tar -jxvf iptables-1.3.3.tar.bz2
[root@jiecho]# tar -jxvf patch-o-matic-ng-20050810.tar.bz2
[root@jiecho]# ln -s linux-2.6.5-1.358 linux
[root@jiecho]# ln -s iptables-1.3.3 iptables

3,给netfilter打补丁,选择你要的模块,为了测试我就选择了ipp2p和time
[root@jiecho]# cd /usr/src/linux
[root@jiecho]# make mrproper
[root@jiecho]# make menuconfig
什么不做,保存退出,然后把Makefile改一下
[root@jiecho]# vi Makefile
把EXTRAVERSION = -1.358custom更改为EXTRAVERSION = -1.358
保持跟uname -a 的版本一致(可详细参考platinum兄弟的文章)
[root@jiecho]# cd /usr/src/patch-o-matic-ng-20050810
[root@jiecho]# ./runme ipp2p
[root@jiecho]# ./runme time
因为事先做好了符号链接,等问你kernel和iptables的位置的时候直接回车就行,
提示你是否添加ipp2p和time模块是就y确定,至此,netfilter的补丁打完了,如果你需要别的模块可以根据需要加上.

4,编译kernel的modules
[root@jiecho]# cd /usr/src/linux
[root@jiecho]# make menuconfig
在Device Drivers->;Networking support->;Networking options->;Network packet filtering (replaces ipchains)
->;IP: Netfilter Configuration中把下面的两项M选中.
<M>; TIME match support                                                               
<M>; IPP2P match support
然后保存退出
4,编译安装模块
下面就是最重要的步骤了,因为我们的原则是节省时间,不重新编译内核,而只编译其中的模块,
这点2.4的内核跟2.6的内核有所不同,郁闷了我一整天,2.4内核的模块是以*.o形式的,而2.6内核是以*.ko形式的,
这有个改变的方法,参考:http://blog.chinaunix.net/article.php?articleId=40742&blogId=2662
[root@jiecho]# make modules
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/basic/split-include
  HOSTCC  scripts/basic/docproc
  HOSTCC  scripts/conmakehash
  HOSTCC  scripts/kallsyms
  CC      scripts/empty.o
  HOSTCC  scripts/mk_elfconfig
  MKELF   scripts/elfconfig.h
  HOSTCC  scripts/file2alias.o
  HOSTCC  scripts/modpost.o
  HOSTCC  scripts/sumversion.o
  HOSTLD  scripts/modpost
  HOSTCC  scripts/pnmtologo
  HOSTCC  scripts/bin2c
  到这里就可以ctrl+c中止了,因为我们不是要编译所有的模块,这样太浪费时间,而仅仅是netfilter的模块,
  但是如果你直接执行make modules SUBDIRS=net/ipv4/netfilter就会出错,这就是2.4和2.6的区别,
  我们先生成了scripts目录下的一系列需要的文件后就可以make modules SUBDIRS=net/ipv4/netfilter,
  并用modpost等等把*.o文件生成为*.ko文件.
[root@jiecho]# make modules SUBDIRS=net/ipv4/netfilter
编译完成netfilter的模块后拷贝编译完成的模块
[root@jiecho]# cp /usr/src/linux/net/ipv4/netfilter/ipt_ipp2p.ko /lib/modules/2.6.5-1.358/kernel/net/ipv4/netfilter/
[root@jiecho]# cp /usr/src/linux/net/ipv4/netfilter/ipt_time.ko /lib/modules/2.6.5-1.358/kernel/net/ipv4/netfilter/
[root@jiecho]# chmod +x /lib/modules/2.6.5-1.358/kernel/net/ipv4/netfilter/ipt_ipp2p.ko
[root@jiecho]# chmod +x /lib/modules/2.6.5-1.358/kernel/net/ipv4/netfilter/ipt_time.ko
[root@jiecho]# depmod -a
5,编译iptables
[root@jiecho]# cd /usr/src/iptables
参照INSTALL文件我们进行编译安装
[root@jiecho]# make KERNEL_DIR=/usr/src/linux
会有错误编译不过
/usr/src/linux/include/linux/config.h:6:2: [root@jiecho]#error including kernel header in userspace; use the glibc headers instead!
我google了半天也没有找到是什么原因,就自己动手解决
[root@jiecho]# vi /usr/src/linux/include/linux/config.h把下面的三行注释掉
//#ifndef __KERNEL__
//#error including kernel header in userspace; use the glibc headers instead!
//#endif

然后重新编译安装
make KERNEL_DIR=/usr/src/linux
make install KERNEL_DIR=/usr/src/linux
完成安装以后测试一下
[root@jiecho]# iptables -V
[root@jiecho]# modprobe ipt_time
[root@jiecho]# modprobe ipt_ipp2p
[root@jiecho]# lsmod
ipt_time                2432  0
ipt_ipp2p               7552  0
ipt_REJECT              4736  1
ipt_state               1536  1
ip_conntrack           24968  1 ipt_state
iptable_filter          2048  1
ip_tables              13440  5 ipt_time,ipt_ipp2p,ipt_REJECT,ipt_state,iptable_filter
battery                 6924  0
ipv6                  184288  10
3c59x                  30376  0
binfmt_misc             7176  1
nls_utf8                1536  4
ntfs                   81516  4
dm_mod                 33184  0
uhci_hcd               23708  0
button                  4504  0
asus_acpi               8472  0
ac                      3340  0
ext3                  102376  4
jbd                    40216  1 ext3
测试iptables
[root@jiecho]# iptables -A FORWARD -m ipp2p --edk --kazaa --bit -j DROP
[root@jiecho]# iptables -A INPUT -m time --timestart 8:00 --timestop 18:00 --days Mon,Tue,Wed,Thu,Fri
[root@jiecho]# iptalbes -L
作者: platinum    时间: 2005-08-11 15:01
标题: [原创]在2.6内核下不编译内核给iptables添加模块
但是如果你直接执行make modules SUBDIRS=net/ipv4/netfilter就会出错,这就是2.4和2.6的区别,
我们先生成了scripts目录下的一系列需要的文件后就可以make modules SUBDIRS=net/ipv4/netfilter

这里是关键
作者: 蓝白725    时间: 2005-08-11 15:57
标题: [原创]在2.6内核下不编译内核给iptables添加模块
我好像碰到了一个和楼主类似的问题。
我也在网上下载了一个软件包,该软件包也会向内核中添加一个模块。
按照其Readme中的说法,只需进行下面两步就OK了:

make KINCLUDE=<path-to-linux-2.6-src>;/include
insmod net/tipc/tipc.ko

可是我在做第一步的时候就挂了,make的时候出错,也没生成tipc.ko文件。。。

我系统的内核是2.6.5,而在另外一台2.4内核的机子上make和insmod都没有问题。

请教一下楼主这是怎么回事啊?
谢谢!!!
作者: jiecho    时间: 2005-08-11 17:02
标题: [原创]在2.6内核下不编译内核给iptables添加模块
make modules 生成tipc.ko,跟编译iptables没有关系
作者: colddawn    时间: 2005-08-24 12:38
标题: [原创]在2.6内核下不编译内核给iptables添加模块
才看到这个帖子,2.6下kernel的makefile做了不少改动,所以按照以前2.4的编译方式有些地方就不太一样了,尤其是模块编译

楼主前面的步骤都没问题,只是在make modules这部其实不用按ctrl+c,
将这部替换成
make modules_prepare
然后SUBDIRS其实也可以不用了,2.6用更简洁的方式代替,下一步可以这样写:
make M=net/ipv4/netfilter

然后其它步骤照旧即可。


这些都是通过看Makefile对比得出来的,我已经用过很多次了,都没有问题。
作者: cool-bugs    时间: 2005-10-03 14:59
标题: [原创]在2.6内核下不编译内核给iptables添加模块
到了4,编译安装模块就出现了以下错误:

[root@liunxas 2.6.9-11.EL-i686]# make modules
  CHK     include/linux/version.h
  SPLIT   include/linux/autoconf.h ->; include/config/*
  CHK     include/asm-i386/asm_offsets.h
/bin/sh: arch/i386/kernel/asm-offsets.s: 没有那个文件或目录
  UPD     include/asm-i386/asm_offsets.h
mv: stat‘include/asm-i386/asm_offsets.h.tmp’失败: 没有那个文件或目录
make: *** [include/asm-i386/asm_offsets.h] 错误 1
作者: cool-bugs    时间: 2005-10-03 15:06
标题: [原创]在2.6内核下不编译内核给iptables添加模块
我的是Red hat linux AS 4.1 想大netfilter的补丁,但打不上.救命啊
作者: bingosek    时间: 2005-10-03 19:26
标题: [原创]在2.6内核下不编译内核给iptables添加模块
原帖由 "colddawn" 发表:
才看到这个帖子,2.6下kernel的makefile做了不少改动,所以按照以前2.4的编译方式有些地方就不太一样了,尤其是模块编译

楼主前面的步骤都没问题,只是在make modules这部其实不用按ctrl+c,
将这部替换成
make modules_prepare
然后SUBDIRS其实也可以不用了,2.6用更简洁的方式代替,下一步可以这样写:
make M=net/ipv4/netfilter

然后其它步骤照旧即可。


这些都是通过看Makefile对比得出来的,我已经用过很多次了,都没有问题。

你这个应该是最好的解决方法,我以前也是用ctrl+c的方法来解决的
不知道兄弟有没有介绍的资料?我找了好久也找不到编译时的参数资料,又懒得看Makefile
作者: desert969    时间: 2005-10-17 15:15
标题: [原创]在2.6内核下不编译内核给iptables添加模块
我在debian2.4内核上,make menuconfig,在Networking options里面怎么也找不到IP: Netfilter Configuration,不知道是怎么回事?
作者: zsgd    时间: 2006-04-04 23:21
在2.6的kernel下怎么直接生产*.ko的文件,Makefile看不懂,谁指点一下,谢谢!!
作者: egret    时间: 2006-04-05 13:54
这么多步骤,不如直接编内核好了。
作者: webyuhang    时间: 2006-08-02 15:58
以前的步骤都正常,但是在挂在模块的时候出问题了
[root@new-host-9 netfilter]# modprobe ipt_time
FATAL: Error inserting ipt_time (/lib/modules/2.6.9-22.EL/kernel/net/ipv4/netfilter/ipt_time.ko): Unknown symbol in module, or unknown parameter (see dmesg)

日志:
ipt_time: disagrees about version of symbol ipt_unregister_match
ipt_time: Unknown symbol ipt_unregister_match
ipt_time: disagrees about version of symbol ipt_register_match
ipt_time: Unknown symbol ipt_register_match
ipt_time: disagrees about version of symbol ipt_unregister_match
ipt_time: Unknown symbol ipt_unregister_match
作者: lovegqin    时间: 2006-11-06 13:34
提示: 作者被禁止或删除 内容自动屏蔽
作者: lovegqin    时间: 2006-11-07 22:39
提示: 作者被禁止或删除 内容自动屏蔽
作者: duanjigang    时间: 2006-11-08 09:18
俺曾经按照白金的帖子和另外一篇CU的帖子整了3天,最后惨败,功力不够....
作者: juanshuchun    时间: 2006-11-09 10:36
thanks
作者: zhy0414    时间: 2006-11-28 15:46
[root@localhost linux-2.6.15.4]# make M=net/ipv4/netfilter
scripts/Makefile.build:51: kbuild: net/ipv4/netfilter/Makefile - Usage of export-objs is obsolete in 2.6. Please fix!
  CC [M]  net/ipv4/netfilter/ip_conntrack_standalone.o
In file included from include/linux/netfilter_ipv4/ip_conntrack.h:32,
                 from net/ipv4/netfilter/ip_conntrack_standalone.c:34:
include/linux/netfilter_ipv4/ip_conntrack_talk.h:7:43: linux/netfilter_ipv4/lockhelp.h: 没有那个文件或目录
In file included from include/linux/netfilter_ipv4/ip_conntrack.h:32,
                 from net/ipv4/netfilter/ip_conntrack_standalone.c:34:
include/linux/netfilter_ipv4/ip_conntrack_talk.h:10: warning: type defaults to `int' in declaration of `DECLARE_LOCK_EXTERN'
include/linux/netfilter_ipv4/ip_conntrack_talk.h:10: warning: parameter names (without types) in function declaration
include/linux/netfilter_ipv4/ip_conntrack_talk.h:10: warning: data definition has no type or storage class
make[1]: *** [net/ipv4/netfilter/ip_conntrack_standalone.o] 错误 1
make: *** [_module_net/ipv4/netfilter] 错误 2



为什么会这样阿?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2