如果你沒有生活在上個世紀,並且是雲計算或相關領域的一名搬磚者,那你應該聽說最近 CentOS 8 官方正式版已經發布了,CentOS
完全遵守 Red Hat
的再發行政策,並且致力與上游產品在功能上完全兼容。CentOS 8 主要改動和 RedHat Enterprise Linux 8 是一致的,基於 Fedora 28 和內核版本 4.18
,其中網絡方面的主要改動是用 nftables
框架替代 iptables
框架作為默認的網絡包過濾工具。如果你還沒有聽說過 nftables,現在是時候學習一下了。
nftables 是一個 netfilter
項目,旨在替換現有的 {ip,ip6,arp,eb}tables 框架,為 {ip,ip6}tables 提供一個新的包過濾框架、一個新的用戶空間實用程序(nft)和一個兼容層。它使用現有的鈎子、鏈接跟蹤系統、用戶空間排隊組件和 netfilter
日志子系統。
nftables 主要由三個組件組成:內核實現、libnl netlink 通信和 nftables 用戶空間。 其中內核提供了一個 netlink
配置接口以及運行時規則集評估,libnl
包含了與內核通信的基本函數,用戶空間可以通過 nft
和用戶進行交互。
本文主要介紹用戶空間命令行工具 nft
的用法。
1. nftables VS iptables
nftables 和 iptables 一樣,由表(table)、鏈(chain)和規則(rule)組成,其中表包含鏈,鏈包含規則,規則是真正的 action。與 iptables 相比,nftables 主要有以下幾個變化:
iptables
規則的布局是基於連續的大塊內存的,即數組式布局;而nftables
的規則采用鏈式布局。其實就是數組和鏈表的區別,好像 Kubernetes 用戶對此應該很興奮?iptables
大部分工作在內核態完成,如果要添加新功能,只能重新編譯內核;而nftables
的大部分工作是在用戶態完成的,添加新功能很 easy,不需要改內核。iptables
有內置的鏈,即使你只需要一條鏈,其他的鏈也會跟着注冊;而nftables
不存在內置的鏈,你可以按需注冊。由於iptables
內置了一個數據包計數器,所以即使這些內置的鏈是空的,也會帶來性能損耗。- 簡化了
IPv4/IPv6
雙棧管理 - 原生支持集合、字典和映射
回到 nftables,先來看一下默認的規則集是啥:
$ nft list ruleset
啥也沒有,果然是沒有內置的鏈啊(如果你關閉了 firewalld
服務)。
2. 創建表
nftables 的每個表只有一個地址簇,並且只適用於該簇的數據包。表可以指定五個簇中的一個:
nftables簇 | iptables命令行工具 |
---|---|
ip | iptables |
ip6 | ip6tables |
inet | iptables和ip6tables |
arp | arptables |
bridge | ebtables |
inet
同時適用於 IPv4 和 IPv6 的數據包,即統一了 ip
和 ip6
簇,可以更容易地定義規則,下文的示例都將采用 inet 簇。
先創建一個新的表:
$ nft add table inet my_table
列出所有的規則:
$ nft list ruleset
table inet my_table {
}
現在表中還沒有任何規則,需要創建一個鏈來保存規則。
3. 創建鏈
鏈是用來保存規則的,和表一樣,鏈也需要被顯示創建,因為 nftables 沒有內置的鏈。鏈有以下兩種類型:
- 常規鏈 : 不需要指定鈎子類型和優先級,可以用來做跳轉,從邏輯上對規則進行分類。
- 基本鏈 : 數據包的入口點,需要指定鈎子類型和優先級。
創建常規鏈:
$ nft add chain inet my_table my_utility_chain
創建基本鏈:
$ nft add chain inet my_table my_filter_chain { type filter hook input priority 0 \; }
- 反斜線(
\
)用來轉義,這樣 shell 就不會將分號解釋為命令的結尾。 priority
采用整數值,可以是負數,值較小的鏈優先處理。
列出鏈中的所有規則:
$ nft list chain inet my_table my_utility_chain
table inet my_table {
chain my_utility_chain {
}
}
$ nft list chain inet my_table my_filter_chain
table inet my_table {
chain my_filter_chain {
type filter hook input priority 0; policy accept;
}
}
4. 創建規則
有了表和鏈之后,就可以創建規則了,規則由語句或表達式構成,包含在鏈中。下面添加一條規則允許 SSH 登錄:
$ nft add rule inet my_table my_filter_chain tcp dport ssh accept
add
表示將規則添加到鏈的末尾,如果想將規則添加到鏈的開頭,可以使用 insert
。
$ nft insert rule inet my_table my_filter_chain tcp dport http accept
列出所有規則:
$ nft list ruleset
table inet my_table {
chain my_filter_chain {
type filter hook input priority 0; policy accept;
tcp dport http accept
tcp dport ssh accept
}
}
注意 http 規則排在 ssh 規則的前面,因為之前使用了 insert
。
也可以將規則插入到鏈的指定位置,有兩種方法:
1、 使用 index
來指定規則的索引。add
表示新規則添加在索引位置的規則后面,inser
表示新規則添加在索引位置的規則前面。index 的值從 0 開始增加。
$ nft insert rule inet my_table my_filter_chain index 1 tcp dport nfs accept
$ nft list ruleset
table inet my_table {
chain my_filter_chain {
type filter hook input priority 0; policy accept;
tcp dport http accept
tcp dport nfs accept
tcp dport ssh accept
}
}
$ nft add rule inet my_table my_filter_chain index 0 tcp dport 1234 accept
$ nft list ruleset
table inet my_table {
chain my_filter_chain {
type filter hook input priority 0; policy accept;
tcp dport http accept
tcp dport 1234 accept
tcp dport nfs accept
tcp dport ssh accept
}
}
index
類似於 iptables 的 -I
選項,但有兩點需要注意:一是 index 的值是從 0 開始的;二是 index 必須指向一個存在的規則,比如 nft insert rule … index 0
就是非法的。
2、 使用 handle
來指定規則的句柄。add
表示新規則添加在索引位置的規則后面,inser
表示新規則添加在索引位置的規則前面。handle
的值可以通過參數 --handle
獲取。
$ nft --handle list ruleset
table inet my_table { # handle 10
chain my_filter_chain { # handle 2
type filter hook input priority 0; policy accept;
tcp dport http accept # handle 4
tcp dport 1234 accept # handle 6
tcp dport nfs accept # handle 5
tcp dport ssh accept # handle 3
}
}
$ nft add rule inet my_table my_filter_chain handle 4 tcp dport 1234 accept
$ nft insert rule inet my_table my_filter_chain handle 5 tcp dport nfs accept
$ nft --handle list ruleset
table inet my_table { # handle 10
chain my_filter_chain { # handle 2
type filter hook input priority 0; policy accept;
tcp dport http accept # handle 4
tcp dport 2345 accept # handle 8
tcp dport 1234 accept # handle 6
tcp dport 3456 accept # handle 9
tcp dport nfs accept # handle 5
tcp dport ssh accept # handle 3
}
}
在 nftables 中,句柄值是固定不變的,除非規則被刪除,這就為規則提供了穩定的索引。而 index
的值是可變的,只要有新規則插入,就有可能發生變化。一般建議使用 handle
來插入新規則。
也可以在創建規則時就獲取到規則的句柄值,只需要在創建規則時同時加上參數 --echo
和 --handle
。
$ nft --echo --handle add rule inet my_table my_filter_chain udp dport 3333 accept
add rule inet my_table my_filter_chain udp dport 3333 accept # handle 10
5. 刪除規則
單個規則只能通過其句柄刪除,首先需要找到你想刪除的規則句柄:
$ nft --handle list ruleset
table inet my_table { # handle 10
chain my_filter_chain { # handle 2
type filter hook input priority 0; policy accept;
tcp dport http accept # handle 4
tcp dport 2345 accept # handle 8
tcp dport 1234 accept # handle 6
tcp dport 3456 accept # handle 9
tcp dport nfs accept # handle 5
tcp dport ssh accept # handle 3
udp dport 3333 accept # handle 10
}
}
然后使用句柄值來刪除該規則:
$ nft delete rule inet my_table my_filter_chain handle 8
$ nft --handle list ruleset
table inet my_table { # handle 10
chain my_filter_chain { # handle 2
type filter hook input priority 0; policy accept;
tcp dport http accept # handle 4
tcp dport 1234 accept # handle 6
tcp dport 3456 accept # handle 9
tcp dport nfs accept # handle 5
tcp dport ssh accept # handle 3
udp dport 3333 accept # handle 10
}
}
6. 列出規則
前面的示例都是列出了所有規則,我們還可以根據自己的需求列出規則的一部分。例如:
列出某個表中的所有規則:
$ nft list table inet my_table
table inet my_table {
chain my_filter_chain {
type filter hook input priority 0; policy accept;
tcp dport http accept
tcp dport 1234 accept
tcp dport 3456 accept
tcp dport nfs accept
tcp dport ssh accept
udp dport 3333 accept
}
}
列出某條鏈中的所有規則:
$ nft list chain inet my_table my_other_chain
table inet my_table {
chain my_other_chain {
udp dport 12345 log prefix "UDP-12345"
}
}
7. 集合
nftables
的語法原生支持集合,可以用來匹配多個 IP 地址、端口號、網卡或其他任何條件。
匿名集合
集合分為匿名集合與命名集合,匿名集合比較適合用於將來不需要更改的規則。
例如,下面的規則允許來自源 IP 處於 10.10.10.123 ~ 10.10.10.231
這個區間內的主機的流量。
$ nft add rule inet my_table my_filter_chain ip saddr { 10.10.10.123, 10.10.10.231 } accept
$ nft list ruleset
table inet my_table {
chain my_filter_chain {
type filter hook input priority 0; policy accept;
tcp dport http accept
tcp dport nfs accept
tcp dport ssh accept
ip saddr { 10.10.10.123, 10.10.10.231 } accept
}
}
匿名集合的缺點是,如果需要修改集合,就得替換規則。如果后面需要頻繁修改集合,推薦使用命名集合。
之前的示例中添加的規則也可以通過集合來簡化:
$ nft add rule inet my_table my_filter_chain tcp dport { http, nfs, ssh } accept
iptables 可以借助
ipset
來使用集合,而 nftables 原生支持集合,所以不需要借助ipset
。
命名集合
nftables 也支持命名集合,命名集合是可以修改的。創建集合需要指定其元素的類型,當前支持的數據類型有:
ipv4_addr
: IPv4 地址ipv6_addr
: IPv6 地址ether_addr
: 以太網(Ethernet)地址inet_proto
: 網絡協議inet_service
: 網絡服務mark
: 標記類型
先創建一個空的命名集合:
$ nft add set inet my_table my_set { type ipv4_addr \; }
$ nft list sets
table inet my_table {
set my_set {
type ipv4_addr
}
}
要想在添加規則時引用集合,可以使用 @
符號跟上集合的名字。下面的規則表示將集合 my_set
中的 IP 地址添加到黑名單中。
$ nft insert rule inet my_table my_filter_chain ip saddr @my_set drop
$ nft list chain inet my_table my_filter_chain
table inet my_table {
chain my_filter_chain {
type filter hook input priority 0; policy accept;
ip saddr @my_set drop
tcp dport http accept
tcp dport nfs accept
tcp dport ssh accept
ip saddr { 10.10.10.123, 10.10.10.231 } accept
}
}
向集合中添加元素:
$ nft add element inet my_table my_set { 10.10.10.22, 10.10.10.33 }
$ nft list set inet my_table my_set
table inet my_table {
set my_set {
type ipv4_addr
elements = { 10.10.10.22, 10.10.10.33 }
}
}
如果你向集合中添加一個區間就會報錯:
$ nft add element inet my_table my_set { 10.20.20.0-10.20.20.255 }
Error: Set member cannot be range, missing interval flag on declaration
add element inet my_table my_set { 10.20.20.0-10.20.20.255 }
^^^^^^^^^^^^^^^^^^^^^^^
要想在集合中使用區間,需要加上一個 flag interval
,因為內核必須提前確認該集合存儲的數據類型,以便采用適當的數據結構。
支持區間
創建一個支持區間的命名集合:
$ nft add set inet my_table my_range_set { type ipv4_addr \; flags interval
$ nft add element inet my_table my_range_set { 10.20.20.0/24 }
$ nft list set inet my_table my_range_set
table inet my_table {
set my_range_set {
type ipv4_addr
flags interval
elements = { 10.20.20.0/24 }
}
}
子網掩碼表示法會被隱式轉換為 IP 地址的區間,你也可以直接使用區間
10.20.20.0-10.20.20.255
來獲得相同的效果。
級聯不同類型
命名集合也支持對不同類型的元素進行級聯,通過級聯操作符 .
來分隔。例如,下面的規則可以一次性匹配 IP 地址、協議和端口號。
$ nft add set inet my_table my_concat_set { type ipv4_addr . inet_proto . inet_service \; }
$ nft list set inet my_table my_concat_set
table inet my_table {
set my_concat_set {
type ipv4_addr . inet_proto . inet_service
}
}
向集合中添加元素:
$ nft add element inet my_table my_concat_set { 10.30.30.30 . tcp . telnet }
在規則中引用級聯類型的集合和之前一樣,但需要標明集合中每個元素對應到規則中的哪個位置。
$ nft add rule inet my_table my_filter_chain ip saddr . meta l4proto . tcp dport @my_concat_set accept
這就表示如果數據包的源 IP、協議類型、目標端口匹配 10.30.30.30、tcp、telnet
時,nftables 就會允許該數據包通過。
匿名集合也可以使用級聯元素,例如:
$ nft add rule inet my_table my_filter_chain ip saddr . meta l4proto . udp dport { 10.30.30.30 . udp . bootps } accept
現在你應該能體會到 nftables 集合的強大之處了吧。
nftables 級聯類型的集合類似於 ipset 的聚合類型,例如
hash:ip,port
。
8. 字典
字典是 nftables 的一個高級特性,它可以使用不同類型的數據並將匹配條件映射到某一個規則上面,並且由於是哈希映射的方式,可以完美的避免鏈式規則跳轉的性能開銷。
例如,為了從邏輯上將對 TCP 和 UDP 數據包的處理規則拆分開來,可以使用字典來實現,這樣就可以通過一條規則實現上述需求。
$ nft add chain inet my_table my_tcp_chain
$ nft add chain inet my_table my_udp_chain
$ nft add rule inet my_table my_filter_chain meta l4proto vmap { tcp : jump my_tcp_chain, udp : jump my_udp_chain }
$ nft list chain inet my_table my_filter_chain
table inet my_table {
chain my_filter_chain {
...
meta nfproto ipv4 ip saddr . meta l4proto . udp dport { 10.30.30.30 . udp . bootps } accept
meta l4proto vmap { tcp : jump my_tcp_chain, udp : jump my_udp_chain }
}
}
和集合一樣,除了匿名字典之外,還可以創建命名字典:
$ nft add map inet my_table my_vmap { type inet_proto : verdict \; }
向字典中添加元素:
$ nft add element inet my_table my_vmap { 192.168.0.10 : drop, 192.168.0.11 : accept }
后面就可以在規則中引用字典中的元素了:
$ nft add rule inet my_table my_filter_chain ip saddr vmap @my_vmap
9. 表與命名空間
在 nftables 中,每個表都是一個獨立的命名空間,這就意味着不同的表中的鏈、集合、字典等都可以有相同的名字。例如:
$ nft add table inet table_one
$ nft add chain inet table_one my_chain
$ nft add table inet table_two
$ nft add chain inet table_two my_chain
$ nft list ruleset
...
table inet table_one {
chain my_chain {
}
}
table inet table_two {
chain my_chain {
}
}
有了這個特性,不同的應用就可以在相互不影響的情況下管理自己的表中的規則,而使用 iptables
就無法做到這一點。
當然,這個特性也有缺陷,由於每個表都被視為獨立的防火牆,那么某個數據包必須被所有表中的規則放行,才算真正的放行,即使 table_one
允許該數據包通過,該數據包仍然有可能被 table_two
拒絕。為了解決這個問題,nftables 引入了優先級,priority
值越高的鏈優先級越低,所以 priority
值低的鏈比 priority
值高的鏈先執行。如果兩條鏈的優先級相同,就會進入競爭狀態。
10. 備份與恢復
以上所有示例中的規則都是臨時的,要想永久生效,我們可以將規則備份,重啟后自動加載恢復,其實 nftables 的 systemd
服務就是這么工作的。
備份規則:
$ nft list ruleset > /root/nftables.conf
加載恢復:
$ nft -f /root/nftables.conf
在 CentOS 8 中,nftables.service
的規則被存儲在 /etc/nftables.conf
中,其中 include 一些其他的示例規則,一般位於 /etc/sysconfig/nftables.conf
文件中,但默認會被注釋掉。
11. 總結
希望通過本文的講解,你能對 nftables 的功能和用法有所了解,當然本文只涉及了一些淺顯的用法,更高級的用法可以查看 nftables 的官方 wiki
,或者坐等我接下來的文章。相信有了本文的知識儲備,你應該可以愉快地使用 nftables 實現 Linux 的智能分流了,具體參考這篇文章:Linux全局智能分流方案。
微信公眾號
掃一掃下面的二維碼關注微信公眾號,在公眾號中回復◉加群◉即可加入我們的雲原生交流群,和孫宏亮、張館長、陽明等大佬一起探討雲原生技術