Nftables 規則集無法在啟動時正確應用

Nftables 規則集無法在啟動時正確應用

Ubuntu 18.04.1。

完全相同的規則集檔案無論是在啟動時由系統載入(損壞)還是在啟動後手動載入(正常工作)都會產生不同的行為。
具體來說,當啟動時載入時,nft 似乎會將相關流量識別為進入其輸入鏈。但是在啟動後(重新)加載,它正確地通過前向鏈傳遞它。
感興趣的流量透過 SSH DNAT 從公共​​介面上的連接埠 4022 傳輸到連接埠 22 上的內部 IP。

我的 /etc/nftables.conf :

#!/usr/sbin/nft -f

flush ruleset

table ip nat {
        chain prerouting {
                        type nat hook prerouting priority -100; policy accept;
                        iif eth0 tcp dport 4022 dnat to 10.0.0.2:22
                }
        chain postrouting {
                        type nat hook postrouting priority 100; policy accept;
                        oifname "eth0" masquerade
                }
}

table ip filter {
        chain input {   
                        type filter hook input priority 0; policy drop;
                        tcp dport 4022 meta nftrace set 1
                        counter
                        ip protocol icmp icmp type echo-request accept
                        ct state { established, related} accept
                        iif lo accept
                        iif eth0 accept
                        iifname "eth0" jump input_eth0
                        iifname "br0" jump input_br0
                }
        chain forward { 
                        type filter hook forward priority 0; policy drop;
                        meta nftrace set 1
                        counter
                        ct state { established, related} accept
                        iifname "eth0" jump forward_eth0
                        iifname "br0" jump forward_br0
                }
        chain output {  
                        type filter hook output priority 0; policy accept;
                }

# ETH0 : 
        chain input_eth0 {
                        counter
                        tcp dport ssh accept
                        tcp dport http accept
                        tcp dport https accept
                }
        chain forward_eth0 {
                        counter
                        ip daddr 10.0.0.2 tcp dport 22 nftrace set 1 accept
                }

# LXD BRIDGE : 
        chain input_br0 {
                        counter
                        reject with icmp type host-prohibited
                }
        chain forward_br0 {
                        counter
                        ip saddr 10.0.0.2 accept
                        ip saddr 10.0.0.10 accept
                        ip saddr 10.0.0.11 accept
                        reject with icmp type host-prohibited
                }

# POSTROUTING : 
        chain pr {
                        type filter hook postrouting priority -10; policy accept;
                        oifname "eth0" jump pr_eth0
                }
        chain pr_eth0 {
                }

}

這是 nft 監視器追蹤在啟動後、重新載入檔案之前的樣子,其中 nft 似乎認為流量應該通過其輸入鏈:

trace id 68d42e8a ip filter input packet: iif "eth0" ether saddr 00:de:ad:be:ef:01 ether daddr 00:de:ad:be:ef:02 ip saddr S.S.S.S ip daddr D.D.D.D ip dscp cs0 ip ecn not-ect ip ttl 58 ip id 50033 ip length 60 tcp sport 7240 tcp dport 4022 tcp flags == syn tcp window 29200 
trace id 68d42e8a ip filter input rule tcp dport 4022 nftrace set 1 (verdict continue)
trace id 68d42e8a ip filter input rule counter packets 230 bytes 382785 (verdict continue)
trace id 68d42e8a ip filter input rule iif "eth0" accept (verdict accept)

這是發出「sudo nft -f /etc/nftables.conf」後的追蹤開始,其中 nft 使用其前向鏈,正如我所期望的那樣:

trace id 1177c4c4 ip filter forward packet: iif "eth0" oif "br0" ether saddr 00:de:ad:be:ef:01 ether daddr 00:de:ad:be:ef:02 ip saddr S.S.S.S ip daddr 10.0.0.2 ip dscp cs0 ip ecn not-ect ip ttl 57 ip id 22369 ip length 60 tcp sport 1665 tcp dport ssh tcp flags == syn tcp window 29200
trace id 1177c4c4 ip filter forward rule nftrace set 1 (verdict continue)
trace id 1177c4c4 ip filter forward rule counter packets 41 bytes 6437 (verdict continue)
trace id 1177c4c4 ip filter forward rule iifname "eth0" jump forward_eth0 (verdict jump forward_eth0)
trace id 1177c4c4 ip filter forward_eth0 rule counter packets 1 bytes 60 (verdict continue)
trace id 1177c4c4 ip filter forward_eth0 rule ip daddr 10.0.0.2 tcp dport ssh nftrace set 1 accept (verdict accept)
trace id 1177c4c4 ip filter pr verdict continue
trace id 1177c4c4 ip filter pr
trace id 1177c4c4 ip nat postrouting verdict continue
trace id 1177c4c4 ip nat postrouting
trace id b9475640 ip filter forward packet: iif "br0" oif "eth0" ether saddr 00:de:ad:be:ef:03 ether daddr 00:de:ad:be:ef:04 ip saddr 10.0.0.2 ip daddr S.S.S.S ip dscp cs0 ip ecn not-ect ip ttl 63 ip id 0 ip length 60 tcp sport ssh tcp dport 1665 tcp flags == 0x12 tcp window 28960
trace id b9475640 ip filter forward rule nftrace set 1 (verdict continue)
trace id b9475640 ip filter forward rule counter packets 41 bytes 6437 (verdict continue)
trace id b9475640 ip filter forward rule ct state {  } accept (verdict accept)
trace id b9475640 ip filter pr packet: oif "eth0" ip saddr 10.0.0.2 ip daddr S.S.S.S ip dscp cs0 ip ecn not-ect ip ttl 63 ip id 0 ip length 60 tcp sport ssh tcp dport 1665 tcp flags == 0x12 tcp window 28960
trace id b9475640 ip filter pr rule oifname "eth0" jump pr_eth0 (verdict jump pr_eth0)
trace id b9475640 ip filter pr_eth0 verdict continue
trace id b9475640 ip filter pr verdict continue
trace id b9475640 ip filter pr
[...]

我在 /var/log/syslog、/var/log/kern.log、dmesg 等中沒有看到 nft 的任何警告或錯誤。

eth0、br0、lo皆在/etc/network/interfaces中配置;不使用網路計劃。

我的第一個猜測是,這個問題在某種程度上與 nft 在介面(eth0、br0)啟動甚至可能存在之前初始化這一事實有關。除了 Ubuntu 之外,我還沒有在其他作業系統上嘗試過。我在兩個不同的伺服器上遇到這個問題。我還沒有嘗試在 iptables 中複製 nftables 規則集。

有人知道如何解決 - 甚至排除 - 這個問題嗎?

相關內容