Skip to contentSkip to conversation

Understanding output from "nft list"

DISCUSSION Brian Sammon 5 participants 12 messages Aug 23, 2024 – Aug 27, 2024

lore
Share & tools
lore
#0 lore raw

Is there a document that is good for understanding the output of "nft list"? (more specifically the kind of output that comes from "nfs list ruleset") In particular I'm looking for a description of the syntax.

I've been browsing around https://www.netfilter.org/ and I've been having trouble finding the kind of information that I'm looking for.

The first questions I have:
Can/should I assume that each line in the output of "nft list" is a separate, uh... "thing"[1] that can be (human-)parsed independently from every other line? Is there something that signifies "this line should be considered as part of the same Thing as the last/next line"?

Should I be reading each line/"thing" from left to right in my attempt to parse/understand it?

This is complicated by the fact that the current firewall config is generated by a "front end" package, which I'm also trying to learn.
I wonder if the output from "nft list" represents some weird syntax generated by the front end package. Can I assume that the output of "nft list" is _valid_ syntax?

[1] - I'm not sure if "expression", "statement", or "rule", or something else, is an appropriate word to use here.

#1 lore raw
Quoted 6 lines · Brian Sammon

Is there a document that is good for understanding the output of "nft list"? (more specifically the kind of output that comes from "nfs list ruleset") In particular I'm looking for a description of the syntax.

I've been browsing around https://www.netfilter.org/ and I've been having trouble finding the kind of information that I'm looking for.

There is no formal definition of the grammar beyond the source code itself, and no good documentation for the grammar beyond that which can be cobbled together from reading https://netfilter.org/projects/nftables/manpage.html and the wiki. I would say that experimentation also helps.

Quoted 5 lines

The first questions I have:
Can/should I assume that each line in the output of "nft list" is a separate, uh... "thing"[1] that can be (human-)parsed independently from every other line?

No, though it is possible to compose rulesets in the iptables-esque fashion that you imply. That is, by writing consecutive commands of the form of "add table", "add chain", "add rule" and so forth. However, the nft(8) utility does not have an option to list the loaded ruleset in that way. Instead, it always lists the ruleset in the declarative style.

Quoted 2 lines

Is there something that signifies "this line
should be considered as part of the same Thing as the last/next line"?

Depending on what you mean, the fact that it is enclosed by the same pair of curly braces.

# A table (a namespace, really).
table inet t {
# A chain contained by said table.
chain c1 {
# There may be rules contained by this chain.
}
# Another chain contained by said table. chain c2 {
# There may also be rules contained by this chain.
}
}

That could also have been written as the following command sequence:

add table inet t
add chain inet t c1
add chain inet t c2

Commands and statements may be line-continued by way of backslash-escaping the newline that would otherwise terminate them.

# This is a single rule.
meta l4 proto tcp dport { 80, 443 } accept

# And so is this.

meta l4 proto tcp \
    dport { 80, 443 } accept
Quoted 3 lines

Should I be reading each line/"thing" from left to right in my attempt to parse/understand it?

To say forwards would be more accurate. In several respects, the syntax is not entirely unlike that of the shell:

- whitespace generally serves only to separate tokens unless quoted
- newlines are metacharacters unless demoted to mere whitespace by being quoted (escaped)
- semicolons have the same effect as raw newlines
- upon encountering an opening curly brace, the parser must look for its closing counterpart

Quoted 5 lines

This is complicated by the fact that the current firewall config is generated by a "front end" package, which I'm also trying to learn. I wonder if the output from "nft list" represents some weird syntax generated by the front end package. Can I assume that the output of "nft list" is _valid_ syntax?

Always, unless it happens to be a hybrid ruleset produced by iptables-nft(8). See https://www.redhat.com/en/blog/using-iptables-nft-hybrid-linux-firewall. If you instruct nft(8) to list a hybrid ruleset, it will include the following diagnostic message as part of its output.

# Warning: table ip filter is managed by iptables-nft, do not touch!

Quoted 3 lines

[1] - I'm not sure if "expression", "statement", or "rule", or something else, is an appropriate word to use here.

Such is understandable. Even as an experienced nftables user, it is difficult to explain using strictly formal terminology.

Signature · 1 line
Kerin Millar
#11 lore raw
In reply to Brian Sammon
Quoted 1 line · Brian Sammon

Is there a document that is good for understanding the output of "nft list"? (more specifically the kind of output that comes from "nfs list ruleset") In particular I'm looking for a description of the syntax.

Like Kerin says, experimentation is probably the best way.

One trick I used when learning nft syntax (and still do), is to use nft list with and without the -j (json format) option, and compare the two sets of output to get a better feel for what the "terse" form means.

$ nft list chain inet t1 c1
...
$ nft -j list chain inet t1 c1 | json_pp
...

(Having written compilers back in the '80s, the parse-tree form of the json looks very natural to me, although I suspect it isn't all that much better for many. But, it won't hurt to try it and find out.)

Eric

#2 lore raw
In reply to Kerin Millar
Quoted 10 lines · On Sat, 24 Aug 2024 01:08:53 +0100 "Kerin Millar" <[email protected]> wrote:
Quoted 4 lines

The first questions I have:
Can/should I assume that each line in the output of "nft list" is a separate, uh... "thing"[1] that can be (human-)parsed independently from every other line?

No, though it is possible to compose rulesets in the iptables-esque fashion that you imply. That is, by writing consecutive commands of the form of "add table", "add chain", "add rule" and so forth. However, the nft(8) utility does not have an option to list the loaded ruleset in that way. Instead, it always lists the ruleset in the declarative style.

I'm wondering if there's a misunderstanding here.

I'm thinking that the output of "nft list" could be like (90% of) python code, where a line like
a = b + c
can be understood (somewhat) in isolation, even though you don't have the context to know what a, b, and c are.

Or, the output of "nft list" could be more like SQL, where you can have lines like
SELECT x FROM
or
users INNER
which are each, an incomplete "thing", but can be part of a valid statement.

Is the "nft list" output more like python, or more like SQL?

#3 lore raw

Rule commands within chains are somewhat standalone, like a python or SQL statement. They completely describe which packets are matched (the expression) and what should be done with them (the statements). Rules are the only things that actually examine packets and perform actions. But rules don't control which packets are examined. The chain they are placed in controls that, similar to how a Python statement executes in method.

The 'hook' chains define where in the flow packets are examined. Where in the chain the rule is placed restricts which packets it examines (based on both the initial chain's hook and the previous rules already run). Non-hook chains gather rules to facilitate control flow through the table, similar to Python method.

A table is just a set of chains restricted to a specific protocol family (inet, ip, ip6, arp, bridge, netdev), which, in turn, defines which hooks are available.

I find this diagram to be very helpful: https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks

The rest of the configuration (sets, maps, etc.) are just ways to provide data to rule expressions. Rule actions can update structures like sets and maps.

I also find the --json option to be helpful. The structure is what controls the flow. Packets enter at a hook chain (like 'input', or 'prerouting'), and traverse rules according to the structure of the chains. The chain JSON objects are analogous to Python functions.

Note that the same packet can come in on different hooks as it flows through the system (see the diagram mentioned above). Another thing to keep in mind is that 'drop' is immediate, and the packet is not handled further and will not be available via subsequent hooks in the flow. 'Accept', on the other hand, accepts the packet in the current hook. No other rules reachable from the hook chain are executed. However, the same packet can come in via a subsequent hook, and the rules reachable from that chain may drop the packet.

Not an answer to your specific question, but hopefully some of this context helps.

-----Original Message-----
From: Brian Sammon <[email protected]>
Sent: Friday, August 23, 2024 10:39 PM
To: [email protected]
Subject: Re: Understanding output from "nft list"

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

EXTERNAL EMAIL - USE CAUTION when clicking links or attachments

Quoted 11 lines · On Sat, 24 Aug 2024 01:08:53 +0100 "Kerin Millar" <[email protected]> wrote:
Quoted 4 lines

The first questions I have:
Can/should I assume that each line in the output of "nft list" is a separate, uh... "thing"[1] that can be (human-)parsed independently from every other line?

No, though it is possible to compose rulesets in the iptables-esque fashion that you imply. That is, by writing consecutive commands of the form of "add table", "add chain", "add rule" and so forth. However, the
nft(8) utility does not have an option to list the loaded ruleset in that way. Instead, it always lists the ruleset in the declarative style.

I'm wondering if there's a misunderstanding here.

I'm thinking that the output of "nft list" could be like (90% of) python code, where a line like
a = b + c
can be understood (somewhat) in isolation, even though you don't have the context to know what a, b, and c are.

Or, the output of "nft list" could be more like SQL, where you can have lines like
SELECT x FROM
or
users INNER
which are each, an incomplete "thing", but can be part of a valid statement.

Is the "nft list" output more like python, or more like SQL?

#4 lore raw

Dňa 26. augusta 2024 16:32:23 UTC používateľ "Atkins, Brian" <[email protected]> napísal:

Quoted 1 line

'Accept', on the other hand, accepts the packet in the current hook. No other rules reachable from the hook chain are executed.

Are you sure with that? My understanding of "accept" verdict is, that it ends processing rules in current chain, not in current hook. Thus rules in other chains (with lower priority -- higher number) in current hook are applied.

regards

Signature · 2 lines
Slavko
https://www.slavino.sk/
#5 lore raw

In the nft(8) man page, I see the following. I have not tested this behavior myself.
accept
Terminate ruleset evaluation and accept the packet. The packet can still be dropped later by another hook, for instance accept in the forward hook still allows to drop the packet later in the postrouting hook, or another forward base chain that has a higher priority number and is evaluated afterwards in the processing pipeline.

I was keying off "another hook". I think another base chain with the same hook type but a different priority is considered a different hook. It's hard to describe 'the set of chains reachable from the current base chain' with a single word. They are all executing within the context of the current hook+priority, which is what is terminated on accept.

-----Original Message-----
From: Slavko <[email protected]>
Sent: Monday, August 26, 2024 10:53 AM
To: [email protected]
Subject: RE: Understanding output from "nft list"

EXTERNAL EMAIL - USE CAUTION when clicking links or attachments

Dňa 26. augusta 2024 16:32:23 UTC používateľ "Atkins, Brian" <[email protected]> napísal:

Quoted 1 line

'Accept', on the other hand, accepts the packet in the current hook. No other rules reachable from the hook chain are executed.

Are you sure with that? My understanding of "accept" verdict is, that it ends processing rules in current chain, not in current hook. Thus rules in other chains (with lower priority -- higher number) in current hook are applied.

regards

--
Slavko
https://www.slavino.sk/

#6 lore raw

Dňa 26. augusta 2024 18:12:58 UTC používateľ "Atkins, Brian" <[email protected]> napísal:

Quoted 5 lines

In the nft(8) man page, I see the following. I have not tested this behavior myself.
accept
Terminate ruleset evaluation and accept the packet. The packet can still be dropped later by another hook, for instance accept in the forward hook still allows to drop the packet later in the postrouting hook, or another forward base chain that has a higher priority number and is evaluated afterwards in the processing pipeline.

I was keying off "another hook". I think another base chain with the same hook type but a different priority is considered a different hook. It's hard to describe 'the set of chains reachable from the current base chain' with a single word. They are all executing within the context of the current hook+priority, which is what is terminated on accept.

Perhaps it is only terminology problem, or my English is not enough to see difference, but it seems that we both talk about the same...

When i start to use nftables (switch from iptables) i read that and "another hook, for instance accept in the forward hook" i understand exactly as example points, eg. accept in output hook mens go to postrouting hook. And it works exactly as this, if (and only if) one have only one chain per hook.

Then i start to experiment with multiple chains in the same hook (really it was mix of ip/ip6 and inet tables) and i was surprised, that accept eg. in "ip input" chain can be not enough as packet can be dropped in "inet input" chain (both in input hook). The same applies to multiple chains of one table in the same hook.

Then i read somewhere (perhaps in this ML, i forgot), that accept ends only current chain (with hook definition), which was more undestandable for me.

Perhaps one have to distinguish "hook" and "hook type" (or so), and perhaps my "chain" suggestion is wrong, as here are "user chains" (without hook definition)...

regards

Signature · 2 lines
Slavko
https://www.slavino.sk/
#7 lore raw

I was thinking of non-base chains (chains without a type statement). The base chain can have rules that jump to non-base chains, which can also have rules that jump to other chains, etc. The accept ends that entire chain 'stack' (for want of a better term), including the original base chain.

If, say, you had a table of type ip, and another table of type inet, and they both had input hook chains, I don't think accept in one chain would have an impact on the other table's chain, although I don't know that for sure.

-----Original Message-----
From: Slavko <[email protected]>
Sent: Monday, August 26, 2024 12:45 PM
To: [email protected]
Subject: RE: Understanding output from "nft list"

EXTERNAL EMAIL - USE CAUTION when clicking links or attachments

Dňa 26. augusta 2024 18:12:58 UTC používateľ "Atkins, Brian" <[email protected]> napísal:

Quoted 5 lines

In the nft(8) man page, I see the following. I have not tested this behavior myself.
accept
Terminate ruleset evaluation and accept the packet. The packet can still be dropped later by another hook, for instance accept in the forward hook still allows to drop the packet later in the postrouting hook, or another forward base chain that has a higher priority number and is evaluated afterwards in the processing pipeline.

I was keying off "another hook". I think another base chain with the same hook type but a different priority is considered a different hook. It's hard to describe 'the set of chains reachable from the current base chain' with a single word. They are all executing within the context of the current hook+priority, which is what is terminated on accept.

Perhaps it is only terminology problem, or my English is not enough to see difference, but it seems that we both talk about the same...

When i start to use nftables (switch from iptables) i read that and "another hook, for instance accept in the forward hook" i understand exactly as example points, eg. accept in output hook mens go to postrouting hook. And it works exactly as this, if (and only if) one have only one chain per hook.

Then i start to experiment with multiple chains in the same hook (really it was mix of ip/ip6 and inet tables) and i was surprised, that accept eg. in "ip input" chain can be not enough as packet can be dropped in "inet input" chain (both in input hook). The same applies to multiple chains of one table in the same hook.

Then i read somewhere (perhaps in this ML, i forgot), that accept ends only current chain (with hook definition), which was more undestandable for me.

Perhaps one have to distinguish "hook" and "hook type" (or so), and perhaps my "chain" suggestion is wrong, as here are "user chains" (without hook definition)...

regards

--
Slavko
https://www.slavino.sk/

#8 lore raw

Dňa 26. augusta 2024 23:55:20 UTC používateľ "Atkins, Brian" <[email protected]> napísal:

OK, thus in our topic, the chain is in mean of chain with hook defined, nested chains (jump/goto, without hook) ends whole chain.

Quoted 1 line

If, say, you had a table of type ip, and another table of type inet, and they both had input hook chains, I don't think accept in one chain would have an impact on the other table's chain, although I don't know that for sure.

I asked about that in this ML some time ago. IIRC, it doesn't matter on table, all chains with input hooks goes into input hook. The inet family table just register its input chain into both family (ip/ip6) hooks. The order of processing is based only on priority value, in case of the same priority, the order is unspecified.

In other words, it doesn't matter if input hook chains are one eg. in ip family table and second in inet table, or both input hooks chains are in the same table, they both ends in the same netfilter hook. If packet have to be accepted in that hook, all chains (in that hook) must not drop/reject it.

You can simply test that by enabling trace and then monitor it, i created ip table (for you) just with one accept in input chain (redacted)::

trace id be75f3b8 inet filter input packet: ... trace id be75f3b8 inet filter input rule ... (verdict accept) trace id be75f3b8 ip filter input packet: ...
trace id be75f3b8 ip filter input rule ... (verdict accept)

Note "verdict accept" in both, in the inet filter input and in the ip filter input chains.

Another note, both these chains has the same priority and the ip table
is defined before inet table in config, but packet goes into inet table
first.

regards

Signature · 2 lines
Slavko
https://www.slavino.sk/
#9 lore raw

Dňa 27. augusta 2024 11:19:06 UTC používateľ Slavko <[email protected]> napísal:

Quoted 6 lines

Note "verdict accept" in both, in the inet filter input and in the ip filter input chains.

Another note, both these chains has the same priority and the ip table
is defined before inet table in config, but packet goes into inet table
first.

Huh some weird chars from copy/paste in android's ssh terminal, invisible in mail app, sorry for that.

regards

Signature · 2 lines
Slavko
https://www.slavino.sk/
#10 lore raw
In reply to Slavko

Thanks Slavko, that's good information!

-----Original Message-----
From: Slavko <[email protected]>
Sent: Tuesday, August 27, 2024 5:19 AM
To: [email protected]
Subject: RE: Understanding output from "nft list"

EXTERNAL EMAIL - USE CAUTION when clicking links or attachments

Dňa 26. augusta 2024 23:55:20 UTC používateľ "Atkins, Brian" <[email protected]> napísal:

OK, thus in our topic, the chain is in mean of chain with hook defined, nested chains (jump/goto, without hook) ends whole chain.

Quoted 1 line

If, say, you had a table of type ip, and another table of type inet, and they both had input hook chains, I don't think accept in one chain would have an impact on the other table's chain, although I don't know that for sure.

I asked about that in this ML some time ago. IIRC, it doesn't matter on table, all chains with input hooks goes into input hook. The inet family table just register its input chain into both family (ip/ip6) hooks. The order of processing is based only on priority value, in case of the same priority, the order is unspecified.

In other words, it doesn't matter if input hook chains are one eg. in ip family table and second in inet table, or both input hooks chains are in the same table, they both ends in the same netfilter hook. If packet have to be accepted in that hook, all chains (in that hook) must not drop/reject it.

You can simply test that by enabling trace and then monitor it, i created ip table (for you) just with one accept in input chain (redacted)::

trace id be75f3b8 inet filter input packet: ... trace id be75f3b8 inet filter input rule ... (verdict accept) trace id be75f3b8 ip filter input packet: ...
trace id be75f3b8 ip filter input rule ... (verdict accept)

Note "verdict accept" in both, in the inet filter input and in the ip filter input chains.

Another note, both these chains has the same priority and the ip table is defined before inet table in config, but packet goes into inet table first.

regards

--
Slavko
https://www.slavino.sk/