Linux内核防火墙netfilter入门.doc_第1页
Linux内核防火墙netfilter入门.doc_第2页
Linux内核防火墙netfilter入门.doc_第3页
Linux内核防火墙netfilter入门.doc_第4页
Linux内核防火墙netfilter入门.doc_第5页
免费预览已结束,剩余5页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Linux内核防火墙netfilter入门通俗的说,netfilter的架构就是在整个网络流程的若干位置放置了一些检测点(HOOK ),而在每个检测点上上登记了一些处理函数进行处理(如包过滤,NAT等,甚至可以是 用户自定义的功能)。1. netfilter的5个钩子在Linux 2.6内核中,netfilter中有5个钩子,分别是PREROUTING,POSTROUTING, FORWORD,INPUT,OUTPUT。Netfilter在设计的时候,考虑到了应用中的各种情况。在IPV4的协议中,netfilter在IP数据包的路线上仔细选取了5个挂接点(HOOK),这5个点中,在合适的位置对NF_HOOK()宏函数进行调用如图:Netfilter的5个钩子IP层的五个HOOK点的位置如下图所示:1:NF_IP_PRE_ROUTING:刚刚进入网络层的数据包通过此点(刚刚进行完版本号,校验 和等检测), 源地址转换在此点进行; 2:NF_IP_LOCAL_IN:经路由查找后,送往本机的通过此检查点,INPUT包过滤在此点进行; 3:NF_IP_FORWARD:要转发的包通过此检测点,FORWORD包过滤在此点进行; 4:NF_IP_POST_ROUTING:所有马上便要通过网络设备出去的包通过此检测点,内置的目 的地址转换功能(包括地址伪装)在此点进行; 5:NF_IP_LOCAL_OUT:本机进程发出的包通过此检测点,OUTPUT包过滤在此点进行。 2. NF_HOOK宏Netfilter的框架是在协议栈处理过程中调用函数宏NF_HOOK(),插入处理过程来实现的.NF_HOOK()函数宏定义在 /usr/src/linux-2.6.x.x/include/netfilter.h中。3. 注册和注销钩子前面介绍的netfilter的5个钩子,可以在5个钩子处注册钩子函数,对网络数据插入自己的处理。在此介绍netfilter的注册和注销钩子函数的接口,主要有nf_register_hook,nf_unregister_hook,nf_register_sockopt,nf_unregister_hook。结构nf_hook_opsStruct nf_hook_ops Struct list_head list;/钩子链表Nf_hookfn *hook;/钩子处理函数Struct module *owner;/模块所有者Int pf;/钩子协议族Int hooknum;/钩子的位置值Int priority;/钩子的优先级;注册钩子为了方便其他的内核模块操作网络数据,netfilter提供了注册钩子函数,其原形在netfilter.h中声明:intnf_register_hook(struct nf_hook_ops *reg);这个函数在nf_hook_ops链表中插入一个用户自定义的nf_hook_ops结构,在合适的时机调用用户注册的函数。当注册成功时,返回值为0;失败为小于0的错误值。注册回调函数时,首先要书写回调函数,然后将其挂接到nf_hook_ops链上。我们试着写一个简单的回调函数,此函数只是打印一句 “hello world”static unsigned int nf_hook_hello(unsigned int hooknum,struct sk_buff *skb,const struct net_device *in,const struct net_device *out,int (*okfn)(struct sk_buff*) printk(KERN_ALERT “hello worldn”); return NF_ACCEPT;注册钩子函数:static int _init init(void) return nf_register_hook(&nf_hook_hello);Module_init(init);注销钩子只要将nf_register_hook()注册的钩子函数注销即可,其原形:void nf_unregister_hook(struct nf_hook_ops *reg);采用如下方式注销:static void _exit exit(void) return nf_unregister_hook(&nf_hook_hello);Module_exit(exit);4.钩子的简单处理例子通过此例子可以进一步了解netfilter钩子编程框架:本例子实现屏蔽ping的回显,静止向某个IP发送数据,关闭端口不进行响应功能。代码如下:/netfilter_test1.c/#include #include #include #include /ip头部结构#include #include #include #include #include #include #include nf_sockopt.h/版权声明MODULE_LICENSE(Dual BSD/GPL);/NF初始状态宏#define NF_SUCCESS 0#define NF_FAILURE 1 /初始化绑定状态band_status b_status;#define IS_BANDPORT_TCP(status) (status.band_port.port!=0&status.band_tocol=IPPROTO_TCP)#define IS_BANDPORT_UDP(status) (status.band_port.port!=0&status.band_tocol=IPPROTO_UDP)#define IS_BANDPING(status) (status.band_ping)#define IS_BANDIP(status) (status.band_ip)/设置sock选项函数static int nf_sockopt_set(struct sock *sock, int cmd, void _user *user, unsigned int len)int ret=0;band_status status;if(!capable(CAP_NET_ADMIN)ret=-EPERM; /goto ERROR;ret=copy_from_user(&status,user,len);if(ret!=0)ret=-EINVAL;goto ERROR;switch(cmd)case SOE_BANDIP: if(IS_BANDIP(status) b_status.band_ip=status.band_ip; else b_status.band_ip=0; break;case SOE_BANDPORT: if(IS_BANDPORT_TCP(status) b_status.band_tocol=IPPROTO_TCP; b_status.band_port.port=status.band_port.port;else if(IS_BANDPORT_UDP(status) b_status.band_tocol=IPPROTO_UDP; b_status.band_port.port=status.band_port.port;else b_status.band_tocol=0; b_status.band_port.port=0;break;case SOE_BANDPING:if(IS_BANDPING(status)b_status.band_ping=1;elseb_status.band_ping=0;break;default:ret=-EINVAL;break;ERROR:return ret;/获取sock选项函数static int nf_sockopt_get(struct sock *sock, int cmd, void _user *user, unsigned int len) int ret; if(!capable(CAP_NET_ADMIN) ret=-EPERM; goto ERROR;switch(cmd)case SOE_BANDIP:case SOE_BANDPORT:case SOE_BANDPING:ret=copy_to_user(user,&b_status,len);if(ret!=0) ret=-EINVAL;goto ERROR;break;default:ret=-EINVAL;break;ERROR:return ret;/在LOCAL_OUT上挂接钩子static unsigned int nf_hook_out(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=ip_hdr(sb);/if(IS_BANDIP(b_status)if(b_status.band_ip=iph-saddr) return NF_DROP;return NF_ACCEPT;/在LOCAL_IN上挂接的钩子static unsigned int nf_hook_in(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=ip_hdr(sb);/unsigned int src_ip =iph-saddr;struct tcphdr *tcph=NULL;struct udphdr *udph=NULL;switch(iph-protocol)case IPPROTO_TCP:if(IS_BANDPORT_TCP(b_status) tcph=tcp_hdr(sb);if(tcph-dest=b_status.band_port.port)return NF_DROP;break;case IPPROTO_UDP:if(IS_BANDPORT_UDP(b_status) udph=udp_hdr(sb);if(udph-dest=b_status.band_port.port)return NF_DROP;break;case IPPROTO_ICMP:if(IS_BANDPING(b_status) printk(KERN_ALERT DROP ICMP packet form x.x,x,xn);return NF_DROP;break;default:break; return NF_ACCEPT;/初始化nfin钩子,在钩子上static struct nf_hook_ops nfin=.hook=nf_hook_in,.hooknum=NF_IP_LOCAL_IN,.pf=PF_INET,.priority=NF_IP_PRI_FIRST,;/初始化nfin钩子,在钩子OUT上static struct nf_hook_ops nfout=.hook=nf_hook_out,.hooknum=NF_IP_LOCAL_OUT,.pf=PF_INET,.priority=NF_IP_PRI_FIRST,;/初始化nf套接字选项static struct nf_sockopt_ops nfsockopt=.pf=PF_INET,.set_optmin=SOE_BANDIP,.set_optmax=SOE_BANDIP+2,.set=nf_sockopt_set,.get_optmin=SOE_BANDIP,.get_optmax=SOE_BANDIP+2,.get=nf_sockopt_get,;/初始化模块static _init int init()nf_register_hook(&nfin);nf_register_hook(&nfout);nf_register_sockopt(&nfsockopt);printk(KERN_ALERT netfilter test init successfullyn);return NF_SUCCESS;/模块清除static void _exit int exit()nf_unregister_hook(&nfin);nf_unregister_hook(&nfout);nf_unregister_sockopt(&nfsockopt);printk(KERN_ALERT netfilter test clean successfullyn);module_init(init);module_exit(exit);/作者,描述,版本,别名MODULE_AUTHOR(LINJIANYING);MODULE_DESCRIPTION(HELLO WORLD DEMO);MODULE_VERSION(0.0.1);MODULE_ALIAS(NETFILTER TEST);/ nf_sockopt.h /*file: nf_sockopt.h *author: *sockopt extern header file */#ifndef _NF_SOCKOPT_#define _NF_SOCKOPT_#define SOE_BANDIP 0x

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论