netfilter 防火墙 包过滤 实例

下面这个模块是用来将

 

#include <linux/module.h>
#include <linux/netfilter_ipv4/ip_tables.h>

static struct nf_hook_ops user_hook_ops= {  
{ NULL, NULL },  
user_hookfn, //用户定义的钩子函数 
 PF_INET,  
NF_IP_LOCAL_IN, //挂载点 。对于所有进入本机的数据包
(包括使用网络套接字的IPC),都是在此挂载点完成的过滤工作之后再被交给上层用户进
程。
 NF_IP_PRI_FILTER-1 };  

static unsigned int user_hookfn(unsigned int hooknum, struct sk_buff **skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff *))

 return NF_QUEUE;//将数据包送入用户空间
}
int init_module{
 return nf_register_hook(&user_hook_ops );//注册
}
int cleanup_module{
 nf_unregister_hook(&user_hook_ops );//注销
}

//#gcc –O –c –Wall usermodule.c
//#insmod usermodule.o
//#rmmod usermodule.o

//unsigned char buf[BUFSIZE]; //读取数据包用的临时缓存
//struct ipq_handle *h; //访问ip_queue 用的句柄
//unsigned int verdict; //对数据包的判决
//h = ipq_create_handle(0, PF_INET); //创建句柄
//ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE); //设置数据拷贝模式
//while(1)
//{
//ipq_read(h, buf, BUFSIZE, 0); //读取消息
//switch (ipq_message_type(buf)) //判断消息类型
//{
//case NLMSG_ERROR: //读取的是出错消息(例如队列满了)
//break;
//case IPQM_PACKET: //读取到数据包,则获取其指针
//ipq_packet_msg_t *m = ipq_get_packet(buf);
//verdict=handle_packet(m); //处理数据包,得到判决值
//break;
//}
//ipq_set_verdict(h, m->packet_id, verdict, 0, NULL);
判决数据包,重新注入内核
//} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值