Nftables 更新 1.0.9

Old Doc Yak #3

Netfilter 项目组在 10/19 更新了 Nftables 1.0.9 版本, 版本定义为 Old Doc Yak #3 (命名来自会说话的山羊为中心的漫画) 目前Debian源已经有 1.0.9 的Deb包, 可以按需下载进行测试. (注意本次版本限制 libnftnl > 1.2.6 )

下面根据官方的 Announce 梳理下具体更新的新功能.

新功能

conntrack超时时间的自定义设定

tcp连接的状态有 (conntrack -E -o ktimestam可以观测)

  • SYN_SENT
  • SYN_RECV
  • ESTABLISHED
  • FIN_WAIT
  • CLOSE_WAIT
  • LAST_ACK
  • TIME_WAIT
    table inet x {
        ct timeout customtimeout {
                protocol tcp
                l3proto ip
                policy = { established: 2m, close_wait: 10s, close: 20s }
                # 可根据连接的不同状态设定超时时间
        }

        chain y {
                type filter hook prerouting priority filter; policy accept;
                tcp dport 8888 ct timeout set "customtimeout"
        }
    }

ct timeout set 这个功能在v0.9.1 开始引入,但默认参数为 秒(s)

dnat支持numgen与offset

在之前的写法一般为

tcp dport 443 dnat to numgen inc mod 5 map { 0 : 192.168.0.1, 1 : 192.168.0.2, 2 : 192.168.0.3, 3 : 192.168.0.4, 4 : 192.168.0.5 }:443

现在可以调整为

# 低版本该指令的dnat ip会出现异常,注意对齐版本
tcp dport 443 dnat to numgen inc mod 5 offset 0xC0A80001

允许使用常量作为动态set的key

table inet x {
        chain y {
                type filter hook input priority 0; policy drop;
                udp dport 45378 add @dynmark { 10.2.3.4 timeout 3s : 0x00000002 }
        }
    }

在多连接情况下检查元素是否存在(网段情况下)

    table ip filter {
            set test {
                    type ipv4_addr . ether_addr . mark
                    flags interval
                    elements = { 198.51.100.0/25 . 00:0b:0c:ca:cc:10-c1:a0:c1:cc:10:00 . 0x0000006f, }
            }
    }

检测元素是否存在

nft get element ip filter test { 198.51.100.1 . 00:0b:0c:ca:cc:10 . 0x6f }

版本升级

Debian12 - DEB 更新

## libgmp10 > 6.3.0
wget http://mirrors.ustc.edu.cn/debian/pool/main/g/gmp/libgmp10_6.3.0+dfsg-2_amd64.deb
dpkg -i libgmp10_6.3.0+dfsg-2_amd64.deb

## libnftnl11 > 2.6.2
wget http://mirrors.ustc.edu.cn/debian/pool/main/libn/libnftnl/libnftnl11_1.2.6-2_amd64.deb
dpkg -i libnftnl11_1.2.6-2_amd64.deb

## libnftables1 > 1.0.9
wget http://mirrors.ustc.edu.cn/debian/pool/main/n/nftables/libnftables1_1.0.9-1%2Bb1_amd64.deb
dpkg -i libnftables1_1.0.9-1+b1_amd64.deb

## nftables > 1.0.9
wget 'http://mirrors.ustc.edu.cn/debian/pool/main/n/nftables/nftables_1.0.9-1%2Bb1_amd64.deb'
dpkg -i nftables_1.0.9-1+b1_amd64.deb