本人大学生,最近在研究arp防火墙,想实现主机拦截从本机出去发送的所有数据包,获取其源mac地址,以防止主机欺骗.在网上大量调研后采用了netfilter钩子来实现截获数据包,但是在加载内核模块时出现了问题.因为之前没有接触过网络和内核,只能在几天的时间里自学了一下.所以问题比较多.现在贴出我的程序和makefile文件请大神帮忙解答!!小弟感谢不尽,这次开发对我真的很重要哈,但毕竟本人能力有限,先谢谢大家了!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//static char *in_dev="eth0";
//MOUDLE_PARM(in_dev,"s");
static struct nf_hook_ops nfho;
//module_init(pack_init);
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 *nskb;//mac地址通过网上给的方法无法获取,所以直接执行DROP
// struct ethhdr *eth;
// nskb=*skb;
// eth=(struct ethhdr*)(nskb->mac.raw);
// printk("src mac:");
// printk("%02x",eth_hdr->h_source);
// printk("\n");
// switch(mac)
// {
// case 111111111111 :
return NF_DROP;
// default:
// return NF_DROP;
// }
}
int init_hook(void)
{
nfho.hook=hook_func;
nfho.hooknum=NF_INET_POST_ROUTING;
nfho.pf=PF_INET;
nfho.priority=NF_IP_PRI_FIRST;
nf_register_hook(&nfho);
return 0;
}
void cleanup_module()
{
nf_unregister_hook(&nfho);
}
makefile:
obj-m :=hook.o
KERNELDIR :=/lib/modules/uname -r
/build
PWD :=$(shell pwd)
default:
make -C ${KERNELDIR} M=${PWD} modules
clean:
rm -f .o *.ko *.mod.c *.mod.o modules. Module.*
现在有三个问题:
1、模块加载成功后没有实现DROP的效果,主机还是可以上网。
2、模块无法卸载。执行一次rmmod显示杀死成功,但是还是可以grep到。
3、网上的方法已经无法实现获取数据包源mac地址,因为后来内核中取消了ethhdr结构体。求解决办法。