Score:1

What is the INPUT_direct chain in firewalld?

cn flag

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.?

Tom Newton avatar
cn flag
Maybe, tread from developers of firewalld helps https://lists.fedorahosted.org/archives/list/[email protected]/thread/5PBK6WUBFC2VA4F76YG33YX6UXZYMIYY/
Score:1
cl flag
A.B

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.

lonix avatar
cn flag
Thank you for this detailed explanation. BTW I'm using iptables because docker doesn't yet support nftables and I don't want to use a compatibility shim.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.