1.簇(family)
nftables統(tong)一了(le)(le)之前的xtables,提出了(le)(le)簇的概念
| ip | ipv4 |
| ip6 | ipv6 |
| inet | internet(IPv4/IPv6) |
| arp | arp ,處理arp數據包 |
| bridge | 網橋,處理通過網橋設備的數據包 |
| netdev | netdev,處理來自入口的數據包 |
2.表(tables)
容器,存放chain鏈的
默認是不存在表的(de)(de),需要人為的(de)(de)自定義表
1.列出表: nft list tables
2.列出表中(zhong)的(de)鏈和規則:nft list table family table
3.創建新表(biao):nft add table family table
4.刪除表
只能刪除不包含鏈的表 (能刪除包含鏈的)
刪除表: nft delete table family table
5.清空表
清空一個表中(zhong)的所有規則:nft flush table family table
3.鏈(chain)
1.基本鏈
1). 類型(type)
分(fen)為 filter / route / nat
- filter:過濾數據包
- route:對數據包進行重路由
- nat:對(dui) 數據流 進(jin)行 網絡(luo)地址轉換(數據流僅第一個包(bao)會因(yin)為(wei)鉤子(zi)進(jin)入本鏈,之后的會繞過(guo))
| 類型 | 簇 | hook | 描述 |
| filter | all | all | 標準鏈類型 |
| nat | ipv4,ipv6,inet |
prerouting,input, output,postrouting |
根據conntrack條目執行本地地址轉。 只有連接的第一個數據包實際遍歷此鏈 - 它的規則通常定義創建的 conntrack 條目的詳細信息 |
| route | ipv4,ipv6 | output | 如果數據包已經通過該類型的鏈并將被接受,且 IP 標頭的相關部分已更改,則會執行新的路由查找。 |
2)鉤子(hook)
hook 有六種主要鉤子,通過鉤子進而抓取指定區域的流量分別為 INGRESS / PREROUTING / INPUT / FORWARD / OUTPUT / POSTROUTING(INGRESS/EGRESS 從 Kernel 4.2 后加入 NetDev 地址簇,Kernel 5.10 后加入 inet 地址簇)
IP / IP6 / INET / Bridge 表 family 地址簇 使用 PREROUTING / INPUT / FORWARD / OUTPUT / POSTROUTING
ARP 地址簇表 可以使用 Input 和 Output
NetDev 地址簇表 可以使用 INGRESS 鉤子類型
3)優先級(priority)
Nftables 基礎鏈 優先級 priority 通過 順序排列 進而 按順序執行不同hook
4)策略(lve)(policy)
決(jue)定經過所有(you)規則(ze)后的數據包流向,分(fen)別為 Accept(默認) 和 Drop
選擇 Accept 則通過匹配并繼續執行 Nftables 所配置的 其他鏈
選擇 Drop 則放棄(qi)并終止繼續(xu)執行
2.常規鏈
和規則跳轉有關
3.列出所有鏈
顯示所有表(biao)中包含的chains:nft list chains
4.列出某條鏈中的所有規則
列出一個鏈中的所有規則:
# nft list chain family table chain
5.創建鏈
# 常規(gui)鏈 nft add chain family table chain
# 基本鏈 nft add chain family table chain { type type hook hook priority priority \; }
6. 編輯鏈
要(yao)編輯(ji)一個鏈(lian),只需按名(ming)稱(cheng)調用并(bing)定義要(yao)更改的規則。
nft chain family table chain { [ type type hook hook device device priority priority \; policy <policy> \; ] }
例如,將默認表中的input鏈策略從accept更改為drop:
nft chain inet my_table my_filter_chain {policy drop \;} # 高危操作,連接會(hui)中斷
7.刪除鏈
nft delete chain family table chain (刪除 my_filter_chain (不(bu)帶規則))
8.清空鏈中的規則
nft flush chain family table chain
清空GLOBAL_INGRESS_ALLOWED 鏈 :nft flush chain bridge sddc-microseg GLOBAL_INGRESS_ALLOWED
4.規則(rule)
只針對 chain 鏈 捕獲的流量處理
重點(dian)是聲明 statements的使用
1.介紹
1).expression 表達式(shi)
meta:(元屬性,例如接口)
oif <output interface INDEX>
iif <input interface INDEX>
oifname <output interface NAME>
iifname <input interface NAME>
(oif 和 iif 接受字符串參數并轉換為接口索引)
(oifname 和 iifname 更(geng)具動態(tai)性,但因字符(fu)串(chuan)匹配速度更(geng)慢)
icmp:(ICMP協議)
type <icmp type>
icmpv6:(ICMPv6協議)
type <icmpv6 type>
ip:(IP協議)
protocol <protocol>
daddr <destination address>
saddr <source address>
ip6:(IPv6協議)
daddr <destination address>
saddr <source address>
tcp:(TCP協議)
dport <destination port>
sport <source port>
udp:(UDP協議)
dport <destination port>
sport <source port>
sctp:(SCTP協議)
dport <destination port>
sport <source port>
ct:(鏈接跟蹤)
state <new | established | related | invalid>
2).action:(與(yu)iptables有點類(lei)似) -- 用于判斷
accept:接受數據包并停止其余規則。
drop:丟棄數據包并(bing)停止(zhi)其余規則。
queue:將數據包(bao)排隊到用(yong)戶空間并(bing)停止其余規則。
continue:使用下一個規則并繼續規則集判斷。
return:從當前鏈返回并繼續上一個鏈的下一條規則。在基鏈中它相當于接受jump <chain> :跳轉到新鏈,開始執行新鏈的第一條規則。發出 return 語句后,它將繼續執行原鏈下一個規則goto <chain>:類似于跳轉,但執行完畢新鏈之后,將直接進行新鏈最終判定而不是包含 goto 語句的鏈上繼續
Nftables 規則的配置需要參考可選項 [handle] 代表指定規則位置,參考 list 時的 handle 值決定可選項 [comment] 未知使用場景,
add 將(jiang)(jiang) 規(gui)則 插(cha)在 原規(gui)則列表 后面,insert 將(jiang)(jiang) 規(gui)則 插(cha)在 原規(gui)則列表 前面
2.列出規則
1)列出所有規則:nft list ruleset
2)列出表中所有規則:nft list table family table
3)列出一個鏈中的所有規則:nft list chain family table chain
3.創建規則
add 是后加;規則添加到handle處。默認是規則添加到鏈的末尾。
nft add rule family table chain handle handle statement
insert是指定或者往前加;默認是規則插入到鏈的開頭。
nft insert rule family table chain handle handle statement
添加一條規則允許 SSH 登錄
nft add rule inet my_table my_filter_chain tcp dport ssh accept
nft insert rule inet my_table my_filter_chain tcp dport http accept
4.插入規則到指定位置
1)index
add 表示新規則添加在索引位置的規則后面,
inser 表示新規則添加在索引位置的規則前面。index 的值從 0 開始增加。ps:
index 類似于 iptables 的
-I 選項,但有兩點需要注意:
- index 的值是從 0 開始的;
- index 必須指向一個存在的規則,比如
nft insert rule … index 0 就是非法的。
[root@11 microseg]# nft insert rule inet my_table my_filter_chain index 1 tcp dport nfs accept
[root@11 microseg]# nft list chain inet my_table my_filter_chain table inet my_table {
chain my_filter_chain {
type filter hook input priority filter; policy accept;
tcp dport 80 accept
tcp dport 2049 accept
tcp dport 22 accept
}
}
[root@11 microseg]# nft add rule inet my_table my_filter_chain index 0 tcp dport 1234 accept
[root@11 microseg]# nft list chain inet my_table my_filter_chain table inet my_table {
chain my_filter_chain {
type filter hook input priority filter; policy accept;
tcp dport 80 accept
tcp dport 1234 accept
tcp dport 2049 accept
tcp dport 22 accept
}
}
2)handle
add 表示新規則添加在索引位置的規則后面,
inser 表示新規則添加在索引位置的規則前面。
handle 的值可以通過參數
--handle 獲取在 nftables 中,句柄值是固定不變的,除非規則被刪除,這就為規則提供了穩定的索引。而
index 的值是可變的,只要有新規則插入,就有可能發生變化。一般建議使用
handle 來插入新規則。也可以在創建規則時就獲取到規則的句柄值,只需要在創建規則時同時加上參數
--echo 和(he) --handle
[root@11 microseg]# nft --handle list chain inet my_table my_filter_chain
table inet my_table {
chain my_filter_chain { # handle 3
type filter hook input priority filter; policy accept;
tcp dport 80 accept # handle 5
tcp dport 1234 accept # handle 7
tcp dport 2049 accept # handle 6
tcp dport 22 accept # handle 4
}
}
[root@11 microseg]# nft add rule inet my_table my_filter_chain handle 7 tcp dport 1234 accept
[root@11 microseg]# nft --handle list chain inet my_table my_filter_chain
table inet my_table {
chain my_filter_chain { # handle 3
type filter hook input priority filter; policy accept;
tcp dport 80 accept # handle 5
tcp dport 1234 accept # handle 7
tcp dport 1234 accept # handle 8 # 新增
tcp dport 2049 accept # handle 6
tcp dport 22 accept # handle 4
}
}
# echo 和 handle
[root@11 microseg]# 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 9 # 表示為第9個
# new generation 187 by process 1637000 (nft)
[root@11 microseg]# nft --handle list chain inet my_table my_filter_chain
table inet my_table {
chain my_filter_chain { # handle 3
type filter hook input priority filter; policy accept;
tcp dport 80 accept # handle 5
tcp dport 1234 accept # handle 7
tcp dport 1234 accept # handle 8
tcp dport 2049 accept # handle 6
tcp dport 22 accept # handle 4
udp dport 3333 accept # handle 9
}
}
5. 刪除規則
單個規則只(zhi)能通(tong)過其句柄刪除,首先需要找到刪除的規則句柄:
1. 確定句柄
[root@11 microseg]# nft --handle list chain inet my_table my_filter_chain
table inet my_table {
chain my_filter_chain { # handle 3
type filter hook input priority filter; policy accept;
tcp dport 80 accept # handle 5
tcp dport 1234 accept # handle 7
tcp dport 1234 accept # handle 8
tcp dport 2049 accept # handle 6
tcp dport 22 accept # handle 4
udp dport 3333 accept # handle 9
}
}
然后使用句柄值來刪除該規則:
[root@11 microseg]# nft delete rule inet my_table my_filter_chain handle 8
[root@11 microseg]# nft --handle list chain inet my_table my_filter_chain
table inet my_table {
chain my_filter_chain { # handle 3
type filter hook input priority filter; policy accept;
tcp dport 80 accept # handle 5
tcp dport 1234 accept # handle 7
tcp dport 2049 accept # handle 6
tcp dport 22 accept # handle 4
udp dport 3333 accept # handle 9
}
}