2.6.30内核Netfilter的简单例子、四(filterIp)
来源:互联网 发布:剑网3捏脸数据成男 编辑:程序博客网 时间:2024/06/05 18:18
将革命进行到底,再来一个netfilter例子,这次是过滤指定IP地址的数据包。
1、源代码:filterIp.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;
/* IP address we want to drop packets from, in NB order */
static unsigned char *drop_ip = "/x7f/x00/x00/x01"; /* 127.0.0.1 */
/* 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);
if (iph->saddr == *(unsigned int *)drop_ip)
{
printk("Dropped packet from... %d.%d.%d.%d/n",
*drop_ip, *(drop_ip + 1), *(drop_ip + 2),*(drop_ip + 3));
return NF_DROP;
}
else
{
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("filterIp install into kernel!/n");
return 0;
}
/* Cleanup routine */
void cleanup_module()
{
nf_unregister_hook(&nfho);
pr_info("filterIp removed from kernel!/n");
}
2、Makefile:
obj-m +=filterIp.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 filterIp.ko
remove:
/sbin/rmmod filterIp
3、编译:
make
4、安装模块:
make install
5、测试:
ping 127.0.0.1
查看/var/log/messages,有如下字样:
Sep 11 11:15:40 vm04 kernel: Dropped packet from... 127.0.0.1
6、卸载模块:
make remove
- 2.6.30内核Netfilter的简单例子、四(filterIp)
- 2.6.30内核Netfilter的简单例子、一(DropAll)
- 2.6.30内核Netfilter的简单例子、二(DropLo)
- 2.6.30内核Netfilter的简单例子、三(whoVisit)
- 2.6.30内核Netfilter的简单例子、五(filterProtocol)
- 2.6.30内核Netfilter的简单例子、六(filterPort)
- Linux2.6内核Netfilter的简单例子、七(netMonitor)
- Linux2.6内核Netfilter的简单例子、八(simpNat)
- 2.6.30内核Netfilter的简单例子、一(DropAll) http://blog.csdn.net/sahusoft/article/details/4540886
- 两个netfilter的例子
- 内核定时器 的简单例子
- 2.6内核netfilter包截获
- 2.4和2.6内核的netfilter差异点
- protobuf的C简单的代码例子(四)
- 简单的netfilter hook函数注册以及内核链表的使用
- linux socket的IO多路复用简单例子(四)
- 看了netfilter/iptables内核原理,合理的配置实现简单firewall
- CentOS 6 安装Hadoop 2.6 (四)运行简单例子
- 获取客户端的IP地址
- 结贴的问题
- 加水印图片生成缩略图公用函数
- struts2 debug include param push标签(数据标签)
- 高精度
- 2.6.30内核Netfilter的简单例子、四(filterIp)
- 使用Js调用WebService
- 实现正则标签
- Delphi 函数大全
- [Oracle] Oracle常用命令(待续)
- 分析各家2440开发板的性价比(初学者如何选择开发板)
- [AIX] 常用命令(待续)
- VF中菜单调用表的过程语句
- [Windows] windump(抓包工具)使用方法简介