2.6.30内核Netfilter的简单例子、三(whoVisit)

本想写一个简单的显示访问者IP的内核模块,结果调试了一天。总是栈溢出,恶心了很久。经过多次实验,发现以下问题:

以前写过一个《2.6内核netfilter包截获 》,本想直接在上面修改一下。编译时就有警告:

 /root/work/kernel/whoVisit/whoVisit.c: In function ‘hook_func’:
/root/work/kernel/whoVisit/whoVisit.c:21: warning: initialization from incompatible pointer type
/root/work/kernel/whoVisit/whoVisit.c: In function ‘init_module’:
/root/work/kernel/whoVisit/whoVisit.c:34: warning: assignment from incompatible pointer type

但是,一开始没有引起重视,以为没什么大不了的。insmod,也正常,可一有网络数据就出现栈溢出,然后宕机!

最后发现,就是警告的地方出现了致命错误。《2.6内核netfilter包截获 》好像是在2.6.22版本上实验的。

 

在2.6.22版本上 nf_hookfn 的定义如下:

typedef unsigned int nf_hookfn(unsigned int hooknum,
struct sk_buff **skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *));

但是现在的我用的内核版本是2.6.30,nf_hookfn 的定义如下:

typedef unsigned int nf_hookfn(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *));

细心就会看到:第2个参数 skb ,在2.6.22内核上是sk_buff 的指针的指针;但在2.6.30内核上就是sk_buff 结构体的指针,少了一层引用。

于是,对程序进行修改。

并对前两个例子进行修改:

2.6.30内核Netfilter的简单例子、二(DropLo)

2.6.30内核Netfilter的简单例子、一(DropAll)

以至无误!

 

本次实验的代码其实很简单(如果早点发现内核的改动 的话!)

1、源代码:whoVisit.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netdevice.h>

MODULE_LICENSE("GPL");

/* This is the structure we shall use to register our function */
    static struct nf_hook_ops nfho;

    /* This is the hook function itself */
    unsigned int hook_func(unsigned int hooknum,
                           struct sk_buff *skb,
                           const struct net_device *in,
                           const struct net_device *out,
                           int (*okfn)(struct sk_buff *))
    {
        struct sk_buff *sb = skb;
        struct iphdr *iph;

        iph = ip_hdr(sb);
        pr_warning("Packet from %d.%d.%d.%d/n",NIPQUAD(iph->saddr));

        return NF_ACCEPT;
    }

    /* Initialisation routine */
    int init_module()
    {
        /* Fill in our hook structure */
        nfho.hook     = hook_func;         /* Handler function */
        nfho.hooknum  = NF_INET_PRE_ROUTING; /* First hook for IPv4 */
        nfho.pf       = PF_INET;
        nfho.priority = NF_IP_PRI_FIRST;   /* Make our function first */

        nf_register_hook(&nfho);

        pr_info("whoVisit install into kernel!/n");
        return 0;
    }
            /* Cleanup routine */
    void cleanup_module()
    {
        nf_unregister_hook(&nfho);
        pr_info("whoVisit removed from kernel!/n");
    }

2、Makfile:

obj-m +=whoVisit.o
all:
  make -C /lib/modules/`uname -r`/build/ M=`pwd`
clean:
  make -C /lib/modules/`uname -r`/build/ M=`pwd` clean
install:
  /sbin/insmod whoVisit.ko
remove:
  /sbin/rmmod whoVisit

3、编译:

make

4、安装模块:

make install

5、测试:

ping 127.0.0.1

查看/var/log/messages,有如下字样:

Sep 11 10:52:00 vm04 kernel: Packet from 127.0.0.1
Sep 11 10:52:00 vm04 kernel: Packet from 127.0.0.1

6、卸载模块:

make remove

7、下载 http://download.csdn.net/source/1653530

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值