Nftables 更新 0.9.4

Jive at Five

Netfilter 项目组在 04/01 更新了 Nftables 0.9.4 版本, 版本定义为 Jive at Five (命名来自Jo Jones Trio’s “Jive at Five”). 目前Debian源已经有 0.9.4的Deb包, 可以按需下载进行测试. 下面根据官方的 Announce 梳理下具体更新的新功能.

升级需重点注意

  • 新版本的 参数选项 必须要放在子命令之前, 例如

    $ nft list ruleset -a
    Error: syntax error, options must be specified before commands
    nft list ruleset -a
    ^
    
    $nft -a list ruleset
    

新功能

1. Set/Map 关系连接结构支持 范围定义 (Kernel 5.6)

支持IP属性的范围定义

table ip foo {
        set whitelist {
                type ipv4_addr . ipv4_addr . inet_service
                flags interval
                elements = { 192.168.10.35-192.168.10.40 . 192.68.11.123-192.168.11.125 . 80 }
        }

        chain bar {
                type filter hook prerouting priority filter; policy drop;
                ip saddr . ip daddr . tcp dport @whitelist accept
        }
}

注意, 非 5.6 内核 element会出现 [invalid type] 报错.

2. Set/Map 类型指定 (可以不在rule里面定义)

table ip foo {
    set whitelist {
            typeof ip saddr
            elements = { 192.168.10.35, 192.168.10.101, 192.168.10.135 }
    }

    chain bar {
            type filter hook prerouting priority filter; policy drop;
            ip daddr @whitelist accept
    }
}

这里的 ip daddr @whitelist accept 改成 ip saddr @whitelist accept 更合理

set的类型可以通过 type ipv4_addr 或者 type inet_service, 但这个patch 可以支持自动读取类型来设置, 可以参考commit 记录

Add a typeof keyword to automatically use the correct type in set and map declarations.

3. 支持nat mappings规则的连接结构

一种nat端口映射map对应的新格式. 之前是通过2个map来实现.

table ip foo {
    map destinations {
            type ipv4_addr . inet_service : ipv4_addr . inet_service
            elements = {
                192.168.1.240 . 2021 : 123.58.180.8 . 80
            }
    }

    chain pre {
            type nat hook prerouting priority filter;

            # 192.168.1.240:80 => 123.58.180.8:80  [TCP/UDP]
            dnat ip addr . port to ip daddr map { 192.168.1.240 : 123.58.180.8 . 80 }
            
            # 192.168.1.240:2020 => 123.58.180.8:80 [TCP]
            dnat ip addr . port to ip daddr . tcp dport map {  192.168.1.240 . 2020 : 123.58.180.8 . 80 }

            # tcp dnat with a map => Local_ip. Local_port : nat_ip . nat_port
            dnat ip addr . port to ip daddr . tcp dport map @destinations
    }
}

4. 支持硬件卸载 (hw-tc-offload)

  • 通过ethool启用 ethtool -K eth0 hw-tc-offload on
  • 在nftable 规则里 打开 offload 标记 flags offload

在 Kernel 5.6 下, 支持的特性还有

  • 匹配规则支持: 包头部/网卡名称
  • 操作支持: 数据包 接受,丢弃,复制,转发

5. 错误标识增强

通过 ^ 来标识存在问题的规则

6. 通过 meta sdif/sdifname 适配子接口

7. 支持数据包 meta mark的 左右移位

meta mark set meta mark lshift 1 or 0x1

8. -V 选项提供更详细的扩展信息以及编译信息

# nft -V
nftables v0.9.4 (Jive at Five)
    cli:          readline
    json:         yes
    minigmp:      no
    libxtables:   yes