本文不描述nftables的具体使用,具体命令看man,wiki等文档。主要是简单描述nftables的一些概念,方便我后面看文档快速理解。
family
nftables有 ip,ipv6,inet,arp,bridge,netdev。这些 family 就是和OSI的网络分层相关联的。ip,ipv6基于网络层,可以对ipv4或ipv6处理,inet则是可以同时处理 ipv4和ipv6的family。arp 则是专门处理 arp数据包的。bridge 位于的数据链路层上。netdev则是基于物理层上,对于单个single network interface进行设置。
PS:关于inet, 原文有 “Within a table of inet family, both IPv4 and IPv6 packets traverse the same rules. Rules for IPv4 packets don’t affect IPv6 packets and vice-versa. Rules for both layer 3 protocols affect both. Use meta l4proto to match on the layer 4 protocol regardless the packet is either IPv4 or IPv6.” 前半截是说对于ipv4和ipv6分别处理,但是后面又说可以基于第四层协议,去同时处理两者。但是后面又有 “New in nftables 0.9.7 and Linux kernel 4.10 is the inet family ingress hook, which filters at the same location as the netdev ingress hook."。所以我没太明白inet究竟对应哪一层。
这些family 实际就是和 在nftables之前的各个工具的统一。对应关系:ip-> iptables, ipv6->ip6tables,arp -> arptables,bridge -> ebtables。
这些的family的处理顺序,请参考计算机网络。
table
每一个family 下可以有多个table。每个table 包含多个chain。table 可以对chain进行分类,管理。
chain
这个nftables和iptables基本类似。就是一系列 rule 的集合,按顺序匹配规则和执行操作。
chain 根据是否有hook 指定,分为 base chain 和 regular chain。
base chain 是有会自动的在对应的hook点,进入匹配执行。
regular chain 则是不会自动执行,只有在其他规则中执行jump或者goto的时候才有机会被执行。
chain 可以有不同的类型,如下:
The possible chain types are:
- filter, which is used to filter packets. This is supported by the arp, bridge, ip, ip6 and inet table families.
- route, which is used to reroute packets if any relevant IP header field or the packet mark is modified. If you are familiar with iptables, this chain type provides equivalent semantics to the mangle table but only for the output hook (for other hooks use type filter instead). This is supported by the ip, ip6 and inet table families.
- nat, which is used to perform Networking Address Translation (NAT). Only the first packet of a given flow hits this chain; subsequent packets bypass it. Therefore, never use this chain for filtering. The nat chain type is supported by the ip, ip6 and inet table families.
rule
就是匹配规则,按从上到下的顺序,进行表达式匹配,如果匹配则执行操作语句。某些verdict statement性质的语句,例如drop
,vmap
等,不会再执行该chain后面的规则。
hook
如上图, hook就是用来设置chain会在哪个位置执行的设置。
实际可使用的 hook,会根据 chain type 和 family 有所不同。