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

What is the INPUT_direct chain in firewalld?

· 52CE 我爱测

In many firewalld config examples I see mention of the normal INPUT iptables chain, as well as one named INPUT_direct . e.g. $ sudo firewall-cmd --direct --add-rule ipv4 filter INPUT_direct 0 \ # etc... $ sudo firewall-cmd --dire…

问题原文

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

In many firewalld config examples I see mention of the normal INPUT iptables chain, as well as one named INPUT_direct.

e.g.

$ sudo firewall-cmd --direct --add-rule ipv4 filter INPUT_direct 0 \
  # etc...

$ sudo firewall-cmd --direct --add-rule ipv4 filter INPUT_direct 1 \
  # etc...

What is the significance of INPUT_direct? Must I use it instead of INPUT?

And what is the difference between INPUT_direct 0, INPUT_direct 1, etc.?

已采纳解答

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

TL;DR:

  • if direct rules are really needed, just always use INPUT rather than INPUT_direct: it will always work.

  • if you don't know what priority value to use, just increase the priority value by one for each new direct rule to add.


This chain is (sometimes) used by the direct rules interface of firewalld. This interface/feature allows to store custom iptables rules (and always iptables even when firewalld's backend is nftables) using firewalld and ensure that these rules are the first seen by any packet (here in the filter table and INPUT chain). Normally you should use it only if something can't be handled by firewalld including by firewalld's rich language.

And this is a deprecated feature:

deprecated

The direct interface has been deprecated. It will be removed in a future release. It is superseded by policies, see firewalld.policies(5).

I'm not really sure it's superseded by policies for all of its uses, anyway...

Just as deprecated is the iptables backend. A typical installation on a recent system also includes in /etc/firewalld/firewalld.conf:

# FirewallBackend
# Selects the firewall backend implementation.
# Choices are:
# - nftables (default)
# - iptables (iptables, ip6tables, ebtables and ipset)
# Note: The iptables backend is deprecated. It will be removed in a future
# release.
FirewallBackend=nftables

firewalld can use either iptables or nftables as backend, but the direct rules are always using iptables. There is no concept of nftables direct rule even when the backend is nftables: that's probably one of the reasons that made direct rules deprecated.

  • when the backend is iptables

    • firewalld creates the INPUT_direct chain and adds as first rule in INPUT a jump to INPUT_direct so it's always the first chain to see packets (in the INPUT path).

    • any reference to INPUT in a direct rule is switched to a reference to INPUT_direct

  • when the backend is nftables

    • firewalld doesn't create by itself any iptables chain or rule, but ensures its own nftables rules are hooked to Netfilter at the priority iptables would have used +10.

    So for INPUT it hooks at priority 10 instead of 0. This ensures again that anything iptables does will do it first

    • direct rules referencing builtin chains are not changed. This time INPUT is used.

This peculiarity can be seen in the python code fw_direct.py:

        # if nftables is in use, just put the direct rules in the chain
        # specified by the user. i.e. don't append _direct.
        if not self._fw.nftables_enabled \
           and backend.is_chain_builtin(ipv, table, chain):
            _chain = "%s_direct" % (chain)

In both cases anything created using firewall-cmd --direct --add-rule ipv4 filter INPUT 0 (followed by the same syntax as what would be provided to an iptables command without including the table and chain) will be seen by a packet before firewalld itself sees it.

For the prority value: this affects the order of the rules you want to use in case their order matters. If, by the way rules were chosen by the administrator, a rule doesn't depend on previous rules, then its priority doesn't matter and can even stay the same. To ensure a rule is added after other rules, increase its priority:

If you want to make sure that a rule will be added after another one, use a low priority for the first and a higher for the following.

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

资料来源与适用范围

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

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

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