netfilter 是什么?

netfilter.org is home to the software of the packet filtering framework inside the Linux 2.4.x and later kernel series. 
Software commonly associated with netfilter.org is iptables. Software inside this framework enables packet filtering, network address [and port] translation (NA[P]T) and other packet mangling.
It is the re-designed and heavily improved successor of the previous Linux 2.2.x ipchains and Linux 2.0.x ipfwadm systems. netfilter is a set of hooks inside the Linux kernel that allows kernel modules to register callback functions with the network stack.
A registered callback function is then called back for every packet that traverses the respective hook within the network stack. iptables is a generic table structure for the definition of rulesets. Each rule within an IP table consists of a number of classifiers (
iptables matches) and one connected action (iptables target). netfilter, ip_tables, connection tracking (ip_conntrack, nf_conntrack) and the NAT subsystem together build the major parts of the framework.

https://www.netfilter.org/

HOOK HOWTO:

https://www.netfilter.org/documentation/HOWTO//netfilter-hacking-HOWTO.html

  阅读之前: Packet Filtering HOWTO: https://www.netfilter.org/documentation/HOWTO//packet-filtering-HOWTO.html

    摘要:

    简单的原理,【重要】 https://www.netfilter.org/documentation/HOWTO//packet-filtering-HOWTO-6.html

Each rule specifies a set of conditions the packet must meet, and what to do if it meets them (a `target')

    iptables的使用【tutorial】: https://www.netfilter.org/documentation/HOWTO//packet-filtering-HOWTO-7.html

For these you will be able to specify the new tests on the command line after the `-p' option, which will load the extension. For explicit new tests, use the `-m' option to load the extension, 
after which the extended options will be available.
The TCP extensions are automatically loaded if `-p tcp' is specified.

  再读 NAT HOWTO: https://www.netfilter.org/documentation/HOWTO//NAT-HOWTO.html

  怎么理解SNAT和DNAT的定义?

 I call this SNAT, because you change the source address of the first packet.

  

I divide NAT into two different types: Source NAT (SNAT) and Destination NAT (DNAT).

Source NAT is when you alter the source address of the first packet: i.e. you are changing where the connection is coming from. Source NAT is always done post-routing, 
just before the packet goes out onto the wire. Masquerading is a specialized form of SNAT. Destination NAT is when you alter the destination address of the first packet: i.e. you are changing where the connection is going to.
Destination NAT is always done before routing, when the packet first comes off the wire. Port forwarding, load sharing, and transparent proxying are all forms of DNAT.

  SNAT DNAT的定义是基于连接概念的。在有了连接概念的前提下。SNAT是指修改连接第一个包的源IP地址。DNAT是之修改连接第一个包的目的IP地址。而换一个角度,连接的第一个包都是从client发向server的。SNAT动作在包离开client局域网进入网线之前的那一刻触发(POST routing)。 DNAT在包到达目标网络进入server局域网之后的第一时间触发(PER routing)。 也就是说routing过程是NAT逻辑无关的。routing看见的所有地址都是本地地址。

  见这一段,用来印证以上解释。https://www.netfilter.org/documentation/HOWTO//packet-filtering-HOWTO-9.html

  另外,透明代理和DNAT神马关系?  透明代理要做DNAT。

  三个NAT的应用场景:

1. Modern Connections To The Internet
2. Multiple Servers
3. Transparent Proxying

  Masquerading & Redirection

Masquerading
There is a specialized case of Source NAT called masquerading: it should only be used for dynamically-assigned IP addresses, such as standard dialups
(for static IP addresses, use SNAT above). Redirection
There is a specialized case of Destination NAT called redirection: it is a simple convenience which is exactly equivalent to doing DNAT to the address
of the incoming interface. ## Send incoming port- web traffic to our squid (transparent) proxy
# iptables -t nat -A PREROUTING -i eth1 -p tcp --dport \
-j REDIRECT --to-port

  终于可以进入正题了。

  重点来了: Netfilter Architecture:  https://www.netfilter.org/documentation/HOWTO//netfilter-hacking-HOWTO-3.html

五个HOOK点

NF_IP_PRE_ROUTING
NF_IP_FORWARD
NF_IP_POST_ROUTING
NF_IP_LOCAL_IN
NF_IP_LOCAL_OUT

HOOK的返回值:

NF_ACCEPT: continue traversal as normal.
NF_DROP: drop the packet; don't continue traversal.
NF_STOLEN: I've taken over the packet; don't continue traversal.
NF_QUEUE: queue the packet (usually for userspace handling).
NF_REPEAT: call this hook again.

  tables 就是对挂在hook上面的函数的分类,分为 filter,nat,mangle等(更详细的table功能定义可以见 man tables 命令)

  见:https://www.netfilter.org/documentation/HOWTO//netfilter-hacking-HOWTO-3.html#ss3.2

  见图:

   --->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

  netfitler的kernel入口:https://www.netfilter.org/documentation/HOWTO//netfilter-hacking-HOWTO-4.html

  结合代码:linux.git/net/netfilter/ipvs/ip_vs_core.c

  其他:

  

┬─[tong@T7:~/Src/thirdparty/linux.git]─[:: PM]
╰─>$ vim /etc/protocols
┬─[tong@T7:~/Src/thirdparty/linux.git]─[:: PM]
╰─>$ man protocols

  如何写一个 netfilter的module: https://www.netfilter.org/documentation/HOWTO//netfilter-hacking-HOWTO-4.html#ss4.6

  内容有点旧了,和最新的kernal代码对应不起来。

  有助于理解forward:https://www.netfilter.org/documentation/HOWTO//netfilter-hacking-HOWTO-6.html

完。

自然会想到 firewalld: https://firewalld.org/

[network] netfilter的更多相关文章

  1. [network] IPVS / Load balancer / Linux Virtual Server

    Load Balancer IPVS: http://kb.linuxvirtualserver.org/wiki/IPVS NAT: http://kb.linuxvirtualserver.org ...

  2. Neutron 理解 (6): Neutron 是怎么实现虚拟三层网络的 [How Neutron implements virtual L3 network]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  3. Linux Network Related Drive

    catalog . 通过套接字通信 . 网络实现的分层模型 . 网络命名空间 . 套接字缓冲区 . 网络访问层 . 网络层 . 传输层 . 应用层 . 内核内部的网络通信 1. 通过套接字通信 Lin ...

  4. Network Address Translation(转载)

    Network Address Translation  来源:http://alexanderlaw.blog.hexun.com/9791596_d.html       地址转换用来改变源/目的 ...

  5. (转)Netfilter分析

    看到一篇讲Netfilter框架的,如果有一点基础了的话对于捋清整个框架很好帮助,转下来细细阅读. 转自http://aichundi.blog.163.com/blog/static/7013846 ...

  6. Linux数据包路由原理、Iptables/netfilter入门学习

    相关学习资料 https://www.frozentux.net/iptables-tutorial/cn/iptables-tutorial-cn-1.1.19.html http://zh.wik ...

  7. 深入Linux网络核心堆栈(对于netfilter的用法和讲解)

    http://blog.csdn.net/wswifth/article/details/5115475 注册一个hook函数是围绕nf_hook_ops数据结构的一个非常简单的操作,nf_hook_ ...

  8. iptables or netfilter

    netfilter 内部有三个表:filter .nat .mangle 每个表又有不同的操作链: 1.在filter这个防火墙功能的表中有三个chain:INPUT.FORWARD.OUTPUT. ...

  9. RH253读书笔记(5)-Lab 5 Network File Sharing Services

    Lab 5 Network File Sharing Services Goal: Share file or printer resources with FTP, NFS and Samba Se ...

随机推荐

  1. pandas DataFrame(1)

    之前介绍了numpy的二维数组,但是numpy二维数组有一些局限性,比如,它数组里所有的值的类型必须相同,不能某一列是数值型,某一列是字符串型,这样会导致无法使用 mean() , std() 等方法 ...

  2. 【Spark 深入学习 04】再说Spark底层运行机制

    本节内容 · spark底层执行机制 · 细说RDD构建过程 · Job Stage的划分算法 · Task最佳计算位置算法 一.spark底层执行机制 对于Spark底层的运行原理,找到了一副很好的 ...

  3. (转载)完成端口(Completion Port, I/OCP)详解

    http://www.cnblogs.com/lancidie/archive/2011/12/19/2293773.html 手把手叫你玩转网络编程系列之三    完成端口(Completion P ...

  4. 【Git】简单使用

    [Git & Github] 首先不能混淆两者的概念.git是一个类似于svn的版本管理工具.其可以在本地建立起针对一个项目的众多维度的版本管理体系,提升了开发的效率. 相对的,我们如果想要和 ...

  5. Linux下搭建LAMP环境(YUM)

    安装Apache 1.安装Apache yum -y install httpd 2. Apache配置httpd.conf 通过命令 find / -name httpd.conf 找到Apache ...

  6. (笔记)Linux内核学习(二)之进程

    一 进程与线程 进程就是处于执行期的程序,包含了独立地址空间,多个执行线程等资源. 线程是进程中活动的对象,每个线程都拥有独立的程序计数器.进程栈和一组进程寄存器. 内核调度的对象是线程而不是进程.对 ...

  7. akka cluster sharding

    cluster sharding 的目的在于提供一个框架,方便实现 DDD,虽然我至今也没搞明白 DDD 到底适用于是什么场合,但是 cluster sharding 却是我目前在做的一个 proje ...

  8. 设置Linux打开文件句柄/proc/sys/fs/file-max和ulimit -n的区别

    max-file 表示系统级别的能够打开的文件句柄的数量.是对整个系统的限制,并不是针对用户的. ulimit -n 控制进程级别能够打开的文件句柄的数量.提供对shell及其启动的进程的可用文件句柄 ...

  9. exec vs sp_executesql

    1.exec  vs  sp_executesql 1.1 说到exec了解SQLServer的朋友第一反应应该是它用来执行存储过程,对的这是其一,另一个作用是执行一个动态批处理.总结下:a.执行一个 ...

  10. 为什么HTML使用<!DOCTYPE HTML>

    不管是刚接触前端,还是你已经“精通”web前端开发的内容,你应该知道在你写html的时候需要定义文档类型:你知道如果没有它,浏览器在渲染页面的时候会使用怪异模式:你知道各个浏览器在怪异模式下对各个元素 ...