大网数据广告活动
服务器运维问答

Multiple hooks per nftables chain?

· 52CE 我爱测

Is it possible to define multiple hooks in nftables chain or is there any other elegant way how to prevent duplicationg the rules inside (without include from another file)? table inet raw { chain mangle { type filter hook { prer…

问题原文

原帖链接 · mvorisek · CC BY-SA 4.0

Is it possible to define multiple hooks in nftables chain or is there any other elegant way how to prevent duplicationg the rules inside (without include from another file)?

table inet raw {
    chain mangle {
        type filter hook { prerouting, input, output, forward } priority -190; policy accept;

        ct state invalid counter drop
    }
}

已采纳解答

原帖链接 · A.B · CC BY-SA 4.0

No, a base chain has only one hook into netfilter.

But within the same table, you can call the same user chain from each base chain. Here's an example, with extra counters showing their state after a single ping -c1 127.0.0.1:

table inet myfilter {
    chain mypreroutingchain {
        type filter hook prerouting priority filter; policy accept;
        counter packets 2 bytes 168
        jump myuserchain
    }

    chain myinput {
        type filter hook input priority filter; policy accept;
        counter packets 2 bytes 168
        jump myuserchain
    }

    chain myforward {
        type filter hook forward priority filter; policy accept;
        counter packets 0 bytes 0
        jump myuserchain
    }

    chain myoutput {
        type filter hook output priority filter; policy accept;
        counter packets 2 bytes 168
        jump myuserchain
    }

    chain mypostrouting {
        type filter hook postrouting priority filter; policy accept;
        counter packets 2 bytes 168
        jump myuserchain
    }

    chain myuserchain {
        oif "lo" counter packets 4 bytes 336
        iif "lo" counter packets 4 bytes 336
        counter packets 8 bytes 672
        ct state invalid counter packets 0 bytes 0 drop
    }
}

the same user chain myuserchain was called 8 times for the ping:

outgoing echo-request: from output+postrouting, incoming echo request from prerouting+input, then again the same with echo-reply.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-09-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。