netfilter 架构分析.doc_第1页
netfilter 架构分析.doc_第2页
netfilter 架构分析.doc_第3页
netfilter 架构分析.doc_第4页
netfilter 架构分析.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

Netfilter 架构分析-基于Linux 3.2.0一、 全局图在文件net/netfilter/core.c中定义了一全局变量nf_hooks,用于记录钩子点。nf_hooks第一维代表协议数,第二维代表钩子数。struct list_head nf_hooksNFPROTO_NUMPROTONF_MAX_HOOKS _read_mostly;net/netfilter/f_hooks中每个元素都是一个链表list_headEXPORT_SYMBOL(nf_hooks);nf_hookslistlistlistlistlistlistlistlistlistinclude/linux/netfilter.h 钩子点结构体,其中的hook是钩子函数nf_register_hook() nf_hook_opsinclude/linux/netfilter.h xt_table结构体,用于管理过滤规则listnet/netfilter/core.c 调用nf_register_hook()将钩子点挂载到全局变量nf_hooks的某个list上hookownerpfhooknumprioritylistvalid_hooksinclude/linux/netfilter.h xt_table_info结构体,记录规则入口等信息privatemenet/netfilter/x_tables.c调用xt_hook_link(table, fn)将xt_table与nf_hook_ops关联起来,并挂载钩子点(xt_hook_link()内部调用了nf_register_hooks())。afprioritynamesizenumberinitial_entrieshook_entryunderflownet/ipv4/netfilter/ip_tables.c在ipt_do_table()中,通过hook_entry与entries获得ipt_entry:get_entry(entries, hook_entry);stacksizestackptrjumpstackentries ipt_entryinclude/linux/netfilter_ipv4/ip_tables.hinclude/linux/netfilter/x_tables.h调用ipt_get_target(ipt_entry)获得xt_entry_target调用xt_ematch_foreach()查找匹配信息ipnfcachetarget_offsetnext_offsetcomefromcounterselems0xt_entry_match xt_entry_targetunion uunion usermatch_sizenamerevision;union kernelmatch_sizematchmatch_sizeunion uunion usertarget_sizenamerevision;union kerneltarget_sizetargettarget_sizeunsigned char data0unsigned char data0 xt_match xt_targetlistlistnamenamerevisionrevisioninclude/linux/netfilter/x_tables.hbool (*match)()uint (*target)()int (*checkentry)()int (*checkentry)()void (*destroy)()void (*destroy)()memetabletablematchsizetargetsizehookshooksprotoprotofamilyfamily二、 钩子函数(hook)与过滤规则表(xt_table)前面已经提到,钩子函数与过滤规则的管理是通过全局变量nf_hooks来实现的,那么,什么时候会调用钩子函数呢?钩子函数又是如何利用已经注册好的过滤规则的呢?在Linux内核中定义了网络数据包的流动方向,数据包被网卡捕获后,它在内核网络子系统里的传输路径是:pre-routingroute(in or forward)(out)post-routing。在netfilter上注册的钩子函数如下所示(这些钩子函数按它们被调用的顺序排列): -PRE-ROUTE-FWD-POST- Conntrack | Mangle Mangle Mangle | Filter | NAT (Src) NAT (Dst) | | Conntrack (QDisc) | ROUTE v | IN Filter OUT Conntrack | Conntrack Mangle | Mangle | NAT (Dst) v | Filter共有五个位置设置了钩子点,PRE、IN、FWD、OUT、POST。钩子函数被注册到相应位置之后,它们就会那里等待数据包的到来,在接收数据包的地方,钩子函数被调用,数据包先由钩子函数捕获,进行处理,然后再转发或者丢弃。钩子函数的声明:include/linux/netfilter.htypedef 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 *);net/ipv4/netfilter/iptable_filter.c调用xt_hook_link()使xt_table与nf_hook_ops关联起来,并挂载钩子点。下面围绕iptable_filter为中心进行分析:static int _init iptable_filter_init(void)。/* Register hooks */filter_ops = xt_hook_link(&packet_filter, iptable_filter_hook);。net/ipv4/netfilter/iptable_filter.c定义一个钩子函数iptable_filter_hook(),调用ipt_do_table来使用规则表里的规则。static unsigned int iptable_filter_hook()。net = dev_net(in != NULL) ? in : out);return ipt_do_table(skb, hook, in, out, net-ipv4.iptable_filter);获得规则入口ipt_entryunsigned int ipt_do_table()。table_base = private-entriescpu;e = get_entry(table_base, private-hook_entryhook);do 。xt_ematch_foreach(ematch, e) acpar.match = ematch-u.kernel.match;acpar.matchinfo = ematch-data;if (!acpar.match-match(skb, &acpar)goto no_match;。t = ipt_get_target(e);。verdict = t-u.kernel.target-target(skb, &acpar);。 while (!acpar.hotdrop);。遍历匹配表,匹配则执行下面动作1、 钩子函数获得规则目标xt_entry_target调用目标方法xt_target.target()2、 安全规则的应用从上面已经知道了过滤规则的使用是由钩子函数来实现的,那么,什么时候、在什么地方调用钩子函数呢?net/ipv4/ ip_input.c通过NF_HOOK()来遍历钩子链表并调用钩子函数下面以ip数据包的接收为例进行分析:int ip_rcv()/ 数据包前期处理。return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, dev, NULL, ip_rcv_finish);。include/linux/netfilter.hNF_HOOK() return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);net/netfilter/core.c调用nf_hook_slow()处理钩子点 经过N次解封装,最后调用nf_hook_slow()int nf_hook_slow()。verdict = nf_iterate(&nf_hookspfhook, skb, hook, indev, outdev, &elem, okfn, hook_thresh);。net/netfilter/core.c调用nf_iterate()遍历钩子链表unsigned int nf_iterate()。verdict = elem-hook(hook, skb, indev, outdev, okfn);。钩子函数hook()被调用三、 规则的管理一条规则由三个结构体管理:ipt_entry、ipt_entry_match(其中的xt_match)、ipt_entry_target(其中的xt_target)。在文件net/netfilter/x_tables.c中定义了一个全局变量xt:struct xt_af struct mutex mutex;struct list_head match;struct list_head target;。;static struct xt_af *xt;其中的match管理xt_match,target管理xt_target,在2.6.25以后的Linux版本里少了对xt_table的管理,那么,xt_table哪里去了呢?根据xt_table的注册函数xt_register_table()发现,xt_table已交由struct net管理了。在文件include/net/net_namespace.h中定义了net结构体:struct net 。#ifdef CONFIG_NETFILTERstruct netns_xtxt;。#endif。;在文件include/net/netns/x_tables.h中定义了netns_xt结构体:struct netns_xt struct list_head tablesNFPROTO_NUMPROTO;。;就是通过上面这个tables变量来管理xt_table的。下图展现了这种规则管理的层次关系:匹配xt_match与动作xt_target的管理:定义一维全局变量static struct xt_af *xt;xtafxt1xt0xtfamilystruct list_head targetstruct list_head matchnet/netfilter/x_tables.c调用xt_register_match ()注册xt_match到全局变量xt中 xt_match xt_targetlistlistnamenamerevisionrevisionbool (*match)()uint (*target)()int (*checkentry)()int (*checkentry)()void (*destroy)()void (*destroy)()net/netfilter/x_tables.c调用xt_register_target()注册xt_target到全局变量xt中memetabletablematchsizetargetsizehookshooksprotoprotofamilyfamily规则表xt_table的管理:定义一个net结构体struct netstruct netns_xtstruct list_head tablesproto xt_tablenet/netfilter/x_tables.c调用xt_register_table()注册xt_table到net结构体中listvalid_hooksprivatemeafpriorityname四、 ip table内建的filter表定义在文件net/ipv4/netfilter/iptable_filter.c中xt_table表:static const struct xt_table packet_filter = .name= filter,/规则表默认的名字.valid_hooks= FILTER_VALID_HOOKS,/定义了三个钩子点IN/FORWARD/OUT.me= THIS_MODULE,.af= NFPROTO_IPV4,/协议.priority= NF_IP_PRI_FILTER,/优先级;per network 操作:static struct pernet_operations iptable_filter_net_ops = .init = iptable_filter_net_init,.exit = iptable_filter_net_exit,;初始化filter表:static int _init iptable_filter_init(void)。/* 注册pernet_operations */ret = register_pernet_subsys(&iptable_filter_net_ops);if (ret 0)return ret;/* 注册 hooks */filter_ops = xt_hook_link(&packet_filter, iptable_filter_hook);。五、 内核空间与用户空间的通信ip table是一个基于netfilter框架的网络数据包过滤工具,用户态下发的过滤规则通过套接字操作结构体struct nf_sockopt_ops里的相关系统调用使该规则能被内核识别,并更新过滤规则表。net/ipv4/netfilter/ip_tables.csocket系统调用结构体定义在文件net/ipv4/netfilter/ip_tables.c中static struct nf_sockopt_ops ipt_sockopts = .pf= PF_INET,.set_optmin= IPT_BASE_CTL,.set_optmax= IPT_SO_SET_MAX+1,.set= do_ipt_set_ctl,#ifdef CONFIG_COMPAT.compat_set= compat_do_ipt_set_ctl,#endif.get_optmin= IPT_BASE_CTL,.get_optmax= IPT_SO_GET_MAX+1,.get= do_ipt_get_ctl,#ifdef CONFIG_COMPAT.compat_get= compat_do_ipt_get_ctl,#endif.owner= THIS_MODULE,;net/tipc/socket.csocket系统调用函数,在用户态下调用static int getsockopt(struct socket *sock, int lvl, int opt, ch

温馨提示

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

最新文档

评论

0/150

提交评论