iptables/Netfilter 学习
开始学iptables,因为它是和路由器技术紧密结合在一起的。
iptables的命令看起来眼花缭乱,随便找两个:
iptables -A FORWARD -p tcp -s 192.168.1.0/ -d 192.168.1.234 --dport -j ACCEPT
iptables -A FORWARD -f -p tcp -s 192.168.1.0/ -d 192.168.1.234 --dport -j ACCEPT
看了一些博客,还是云里雾里的,所以开始看内核里面的实现。看了内核的实现之后,再回过来
看别人的博客,整个框架就清晰多了。
实际上iptables这个工具在新版的kernel已经换成nftable了,但是通过看kernel里面的实现,可以
掌握linux数据包控制的大体实现。
整体分析
这里以3.10.79 kernel为例,这里采用的工具仍然还是iptables。
首先要搞清楚iptables的整体架构:
(上图转自http://segmentfault.com/a/1190000002540601)
所以说iptables对应内核的Netfilter,搞懂Netfilter是怎么工作的话,那么iptables也就容易理解了。
样例分析
下面分析一个样例,从ip_rcv开始:
/*
* Main IP Receive routine.
*/
int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
...
return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, dev, NULL, ip_rcv_finish); }
可以看到最后return的时候使用了NF_HOOK。接下来继续调用NF_HOOK_THRESH,nf_hook_thresh。
/**
* nf_hook_thresh - call a netfilter hook
*
* Returns 1 if the hook has allowed the packet to pass. The function
* okfn must be invoked by the caller in this case. Any other return
* value indicates the packet has been consumed by the hook.
*/
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
struct sk_buff *skb,
struct net_device *indev,
struct net_device *outdev,
int (*okfn)(struct sk_buff *), int thresh)
{
if (nf_hooks_active(pf, hook))
return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
return ;
}
这里先判断,再决定。
static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
{
return !list_empty(&nf_hooks[pf][hook]);
}
Normal
0
7.8 pt
0
2
false
false
false
EN-US
ZH-CN
X-NONE
MicrosoftInternetExplorer4
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:宋体;
mso-ascii-font-family:Calibri;
mso-hansi-font-family:Calibri;}
这里很简单,检查nf_hooks表中对应的项是否为0。这里若发现这个表为空的话会立马返回,也就是不进行检查。
Normal
0
7.8 pt
0
2
false
false
false
EN-US
ZH-CN
X-NONE
MicrosoftInternetExplorer4
下面整理了一下这个表里面可能的值,这个表有两部分,左边的部分是表的第一维,右边部分是表的第二维。
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:宋体;
mso-ascii-font-family:Calibri;
mso-hansi-font-family:Calibri;}
table.MsoTableMediumGrid2Accent6
{mso-style-name:"Medium Grid 2 - Accent 6";
mso-tstyle-rowband-size:1;
mso-tstyle-colband-size:1;
mso-style-priority:68;
mso-style-unhide:no;
border:solid #F79646 1.0pt;
mso-border-themecolor:accent6;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:1.0pt solid #F79646;
mso-border-insideh-themecolor:accent6;
mso-border-insidev:1.0pt solid #F79646;
mso-border-insidev-themecolor:accent6;
mso-tstyle-shading:#FDE4D0;
mso-tstyle-shading-themecolor:accent6;
mso-tstyle-shading-themetint:63;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:宋体;
mso-ascii-font-family:Cambria;
mso-ascii-theme-font:major-latin;
mso-fareast-font-family:宋体;
mso-fareast-theme-font:major-fareast;
mso-hansi-font-family:Cambria;
mso-hansi-theme-font:major-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:major-bidi;
color:black;
mso-themecolor:text1;}
table.MsoTableMediumGrid2Accent6FirstRow
{mso-style-name:"Medium Grid 2 - Accent 6";
mso-table-condition:first-row;
mso-style-priority:68;
mso-style-unhide:no;
mso-tstyle-shading:#FEF4EC;
mso-tstyle-shading-themecolor:accent6;
mso-tstyle-shading-themetint:25;
color:black;
mso-themecolor:text1;
mso-ansi-font-weight:bold;
mso-bidi-font-weight:bold;}
table.MsoTableMediumGrid2Accent6LastRow
{mso-style-name:"Medium Grid 2 - Accent 6";
mso-table-condition:last-row;
mso-style-priority:68;
mso-style-unhide:no;
mso-tstyle-shading:white;
mso-tstyle-shading-themecolor:background1;
mso-tstyle-border-top:1.5pt solid black;
mso-tstyle-border-top-themecolor:text1;
mso-tstyle-border-left:cell-none;
mso-tstyle-border-bottom:cell-none;
mso-tstyle-border-right:cell-none;
mso-tstyle-border-insideh:cell-none;
mso-tstyle-border-insidev:cell-none;
color:black;
mso-themecolor:text1;
mso-ansi-font-weight:bold;
mso-bidi-font-weight:bold;}
table.MsoTableMediumGrid2Accent6FirstCol
{mso-style-name:"Medium Grid 2 - Accent 6";
mso-table-condition:first-column;
mso-style-priority:68;
mso-style-unhide:no;
mso-tstyle-shading:white;
mso-tstyle-shading-themecolor:background1;
mso-tstyle-border-top:cell-none;
mso-tstyle-border-left:cell-none;
mso-tstyle-border-bottom:cell-none;
mso-tstyle-border-right:cell-none;
mso-tstyle-border-insideh:cell-none;
mso-tstyle-border-insidev:cell-none;
color:black;
mso-themecolor:text1;
mso-ansi-font-weight:bold;
mso-bidi-font-weight:bold;}
table.MsoTableMediumGrid2Accent6LastCol
{mso-style-name:"Medium Grid 2 - Accent 6";
mso-table-condition:last-column;
mso-style-priority:68;
mso-style-unhide:no;
mso-tstyle-shading:#FDE9D9;
mso-tstyle-shading-themecolor:accent6;
mso-tstyle-shading-themetint:51;
mso-tstyle-border-top:cell-none;
mso-tstyle-border-left:cell-none;
mso-tstyle-border-bottom:cell-none;
mso-tstyle-border-right:cell-none;
mso-tstyle-border-insideh:cell-none;
mso-tstyle-border-insidev:cell-none;
color:black;
mso-themecolor:text1;
mso-ansi-font-weight:normal;
mso-bidi-font-weight:normal;}
table.MsoTableMediumGrid2Accent6OddColumn
{mso-style-name:"Medium Grid 2 - Accent 6";
mso-table-condition:odd-column;
mso-style-priority:68;
mso-style-unhide:no;
mso-tstyle-shading:#FBCAA2;
mso-tstyle-shading-themecolor:accent6;
mso-tstyle-shading-themetint:127;}
table.MsoTableMediumGrid2Accent6OddRow
{mso-style-name:"Medium Grid 2 - Accent 6";
mso-table-condition:odd-row;
mso-style-priority:68;
mso-style-unhide:no;
mso-tstyle-shading:#FBCAA2;
mso-tstyle-shading-themecolor:accent6;
mso-tstyle-shading-themetint:127;
mso-tstyle-border-insideh:.75pt solid #F79646;
mso-tstyle-border-insideh-themecolor:accent6;
mso-tstyle-border-insidev:.75pt solid #F79646;
mso-tstyle-border-insidev-themecolor:accent6;}
table.MsoTableMediumGrid2Accent6NWCell
{mso-style-name:"Medium Grid 2 - Accent 6";
mso-table-condition:nw-cell;
mso-style-priority:68;
mso-style-unhide:no;
mso-tstyle-shading:white;
mso-tstyle-shading-themecolor:background1;}
pf |
hook |
hook example function(ipv4) |
NFPROTO_UNSPEC |
NF_INET_PRE_ROUTING |
ip_rcv |
NFPROTO_IPV4 |
NF_INET_LOCAL_IN |
ip_local_deliver |
NFPROTO_ARP |
NF_INET_FORWARD |
ip_forward |
NFPROTO_BRIDGE |
NF_INET_LOCAL_OUT |
__ip_local_out |
NFPROTO_IPV6 |
NF_INET_POST_ROUTING |
ip_output |
NFPROTO_DECNET |
这里第二列举了一个例子,是关于NFPROTO_IPV4 的。
这个表是Netfilter里面最关键的一个部分,这个数组的每一个单元都是一个链表。
struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
这里存储了对于各种协议、各个钩子的规则信息。iptables用户层将用户定义的规则设置到对应链表里面。
接下来是一个关键的函数,nf_hook_slow。这个函数里面进行主要的检查过程。
next_hook:
verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
outdev, &elem, okfn, hook_thresh);
if (verdict == NF_ACCEPT || verdict == NF_STOP) {
ret = ;
} else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
kfree_skb(skb);
ret = NF_DROP_GETERR(verdict);
if (ret == )
ret = -EPERM;
} else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
verdict >> NF_VERDICT_QBITS);
if (err < ) {
if (err == -ECANCELED)
goto next_hook;
if (err == -ESRCH &&
(verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
goto next_hook;
kfree_skb(skb);
}
}
这里检查的返回结果有多个,总结表格如下:
Normal
0
7.8 pt
0
2
false
false
false
EN-US
ZH-CN
X-NONE
MicrosoftInternetExplorer4
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:宋体;
mso-ascii-font-family:Calibri;
mso-hansi-font-family:Calibri;}
Filter result |
meaning |
NF_DROP |
丢弃报文 |
NF_ACCEPT/ NF_STOP |
继续正常的报文处理 |
NF_STOLEN |
由钩子函数处理了该报文 |
NF_QUEUE |
将报文入队,交由用户程序 |
NF_REPEAT |
再次调用该钩子函数 |
总的来说,在数据表处理的某个阶段,Netfilter会将这个数据包遍历nf_hooks对应单元的链表的规则,然后根据规则作出相应的处理结果。
数据流分析
Netfilter在报文流经的一些地方做了拦截处理,可以从下图中得知:
(图片转自http://www.ibm.com/developerworks/cn/linux/l-ntflt/)
前面分析的是ip_rcv,也就是NF_INET_PRE_ROUTING,这个参数和上图的名字不太一样,可能是kernel版本的问题。
我将上面的部分和代码对应起来:
另外又仔细分析了一下代码的详细流程:
到这里为止,iptables内核部分已经可以大致了解到是怎么回事了。
关于用户层的iptables,一般是这样划分的:
表 -> 链 -> 规则
但是这样看的话一开始也不容易理解,而且不同表和不同链容易混在一起理解。
当理解了内核的Netfilter的时候,这个规则就可以这样划分了:
链 -> (表 + 规则)
因为根本上以链为主体,数据是在链之间流动的。
其他
关于iptables网上的资料太多了,所以随便找几篇看看就可以开始上手,关于这个东西
用多了就会慢慢熟悉,另外要注意到自己设置的规则的先后顺序,如果顺序不对的话会事与愿违。
参考资料:
1.http://www.ibm.com/developerworks/cn/linux/l-ntflt/
2.http://www.360doc.com/content/08/1225/18/36491_2197786.shtml
iptables/Netfilter 学习的更多相关文章
- Linux数据包路由原理、Iptables/netfilter入门学习
相关学习资料 https://www.frozentux.net/iptables-tutorial/cn/iptables-tutorial-cn-1.1.19.html http://zh.wik ...
- IPtables/NetFilter
前提知识 任何主机若要与非同网络中的主机通信,则必须将报文发送到默认网关: 对Linux而言,IP地址是属于主机(内核中)的,不属于网卡,只要属于当前主机的IP地址间,都可直接响应,不称为转发 私有地 ...
- Linux对外提供服务 网络操作 端口操作 1.开启服务监听端口 2.设置防火墙,放行访问端口的包 iptables&netfilter 四表五链和通堵策略
主题: Linux服务器上软件提供服务 1.网络操作 2.端口操作 1.网络操作 本机必须能够ping通目标主机(本地虚拟机或者远程主机) 2.端口操作 1.开启服务监听端口 2.设置防火墙,放行访问 ...
- Linux内核下包过滤框架——iptables&netfilter
iptables & netfilter 1.简介 netfilter/iptables(下文中简称为iptables)组成Linux内核下的包过滤防火墙,完成封包过滤.封包重定向和网络地址转 ...
- iptables/netfilter命令、实现及利用(转)
原文链接:http://blog.csdn.net/sealyao/article/details/5934268 一.Netfilter和Iptables概述 netfilter/iptables ...
- iptables关键学习总结
iptables技术推荐参考这位仁兄的博客:http://www.zsythink.net/archives/category/%E8%BF%90%E7%BB%B4%E7%9B%B8%E5%85%B3 ...
- netfilter 学习摘要
netfilter 子系入口在L3,完成后把数据包发往L4 netfilter 主要功能: 数据包选择(iptables) 数据包过滤 网络地址转换(NAT) 数据包操纵(在路由选择之前或之后修改数据 ...
- centos下防火墙iptables日志学习笔记
一直找不到日志方面怎么弄,问了同事,同事给了个网址: http://www.thegeekstuff.com/2012/08/iptables-log-packets/ 下面就是我根据这个网址里面的设 ...
- Linux netfilter 学习笔记
https://blog.csdn.net/lickylin/article/details/33321905
随机推荐
- 14、SEO工程师要阅读的书籍 - IT软件人员书籍系列文章
SEO工程师是Web项目中比较重要的一个角色.他主要负责网站的针对搜索引擎的优化方案的编写和实施.因为现在网站数量庞大,在全世界的这么多网站当中,想要让用户访问你的网站,就需要一些技巧性的内容.很多用 ...
- php截取字符串函数
public function sub_string($str, $len, $charset="utf-8") { if( !is_numeric($len) or $len & ...
- java 生成和解析二维码
public class QRCode { /** * 解析二维码(QRCode) * @param imgPath * @return */ public static String decoder ...
- SQL Server 分隔字符串函数实现
在SQL Server中有时候也会遇到字符串进行分隔的需求.平时工作中常常遇到这样的需求,例如:人员数据表和人员爱好数据表,一条人员记录可以多多人员爱好记录,而往往人员和人员爱好在界面展示层要一并提交 ...
- iOS Build Active Architecture Only 属性的理解(及 not found for architecture i386 的解决方案)
最近做项目过程遇到一个问题: 涉及到这个属性:Build Active Architecture Only Yes .No的区别: 设置为yes,是只编译当前的architecture版本,是为了编译 ...
- iOS基于MBProgressHUD的二次封装,一行搞定,使用超简单
MBProgressHUD的使用,临时总结了几款最常用的使用场景: 1.提示消息 用法: [YJProgressHUD showMessage:@"显示文字,1s隐藏" inVie ...
- asp.net服务器控件onclick带参数
服务器控件处理参数随笔 正确: <asp:CheckBox ID="cbComplareProduct" runat="server" Text=&quo ...
- Linux系统管理命令之权限管理
对于一个目录来说,x权限:可以cd进去 对于目录: 读:看 执行:进去 写:写操作 rw权限没有意义 umask 022 特殊权限: suid sgid 2种情况:对于文件:类似于suid对于 ...
- 初学git,出现错误:fatal: Not a git repository (or any of the parent directories): .git
提示说没有.git这样一个目录,解决办法: 输入 git init 就可以啦.
- android 动态设置Framelayout,view,imageView,Layout高度
直接:测试可以 Framelayout.getLayoutParams().width=600;Framelayout.getLayoutParams().height=400; 如:view,ima ...