V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
skinny
V2EX  ›  Linux

请问怎么用 nftables 限制速率?为什么我添加的规则总是不符合我的预期?

  •  
  •   skinny · 2020-04-15 11:14:12 +08:00 · 1779 次点击
    这是一个创建于 1497 天前的主题,其中的信息可能已经有所发展或是发生改变。
    操作系统:debian buster (stable)
    运行环境:virtualbox 虚拟机
    网络设置:bridge
    nftables 版本:0.9.0
    

    ping 命令:psping64 -t -i 0 192.168.1.177

    比如,我想限制每秒只处理一个 icmp echo-request,规则配置文件如下:

    #!/usr/sbin/nft -f
    flush ruleset
    
    define ICMP_TYPES = {
        destination-unreachable,
        time-exceeded,
        parameter-problem,
        router-solicitation,
        router-advertisement
    }
    
    define ICMPV6_TYPES = {
        destination-unreachable,
        packet-too-big,
        time-exceeded,
        parameter-problem,
        mld-listener-query,
        mld-listener-report,
        mld-listener-done,
        mld-listener-reduction,
        nd-router-solicit,
        nd-router-advert,
        nd-neighbor-solicit,
        nd-neighbor-advert,
        ind-neighbor-solicit,
        ind-neighbor-advert,
        mld2-listener-report
    }
    
    
    table inet filter {
        chain input {
            type filter hook input priority 0; policy drop;
    
            iif lo accept
            ct state invalid drop
            ct state { established, related } accept
    
            icmp type echo-request limit rate 1/second accept
    
            icmpv6 icmpv6 type $ICMPV6_TYPES accept
            icmp icmp type $ICMP_TYPES accept
    
            ip protocol igmp accept
    
            tcp dport { ssh } accept
        }
    
        chain forward {
            type filter hook forward priority 0; policy drop;
        }
    
        chain output {
            type filter hook output priority 0; policy accept;
        }
    }
    
    

    使用 fast ping 工具测试时,平均间隔 0.68ms 左右,且 0% loss 。 我尝试过添加下面的规则试图抛弃超过限制的,结果没有变化。

    icmp type echo-request limit rate over 1/second drop
    

    还尝试过 burst <num> packets 语句,也没有用。

    第 1 条附言  ·  2020-04-15 14:59:54 +08:00
    测试了 debian testing 的最新 nftables,使用了局域网真机进行 Flood Ping,嗯,防火墙依然不起作用。不过,iptables 的规则起了作用。
    第 2 条附言  ·  2020-04-15 21:18:00 +08:00
    找到原因了……只要我在相同或更高优先级的 chain 上添加放行 ESTABLISHED,RELATED 状态的连接的规则,icmp 速率限制规则就必定失效,且两条规则的前后顺序并不能改变这个结果。iptables 的话,icmp 速率限制的规则要在放行 ESTABLISHED,RELATED 状态的连接的规则前面才可以。
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2710 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:33 · PVG 21:33 · LAX 06:33 · JFK 09:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.