Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Nftables Knowledge Patch?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/nevaberry-nftables-knowledge-patch-nevaberry-plugins)
---
name: nftables-knowledge-patch
description: nftables
version: "1.1.6"
license: MIT
metadata:
author: Nevaberry
---
# nftables knowledge patch
Load this skill when writing or updating nftables rulesets, working with sets,
maps, hooks, NAT, or tunnels, using JSON and monitoring, or building nftables.
Use the topic references for complete examples and version-specific behavior.
## Reference index
| Reference | Topics |
| --- | --- |
| [Rules and expressions](references/rules-and-expressions.md) | Protocol dependencies, payload access, bitwise operations, header options, metadata, optimization, validation |
| [Sets, maps, and state](references/sets-maps-and-state.md) | Types, interval merging, lookups, packet-path updates, timeouts, meters, conntrack objects |
| [Hooks, routing, NAT, and tunnels](references/hooks-routing-nat-and-tunnels.md) | Hook ordering, priorities, devices, FIB checks, address pools, forwarding, IPsec, overlays |
| [JSON, monitoring, and automation](references/json-monitoring-and-automation.md) | JSON input and output, rule placement, tracing, listings, diagnostics, netlink, check mode |
| [Operations, builds, and releases](references/operations-build-and-releases.md) | Removed support, includes, variables, ownership, deletion, resets, dependencies, installation, downloads |
## Compatibility changes and removals
- Remove DECnet rules from rulesets loaded with 1.0.6.1.
- Libnftables removed `nft_bison_have_extended_errors` in 1.1.7.
- Python bindings use setuptools with a PEP 517 build and are installed
separately from Autotools: run `python3 -m pip install ./py` from the source tree.
- Libnftables no longer re-adds the default include directory. An application
that clears the search path must explicitly add that directory if it needs it.
- Entries shown as `xt match`, `xt target`, or `xt watcher` require xtables
compatibility tools for management and restoration. Treat them as placeholders
when preparing a native ruleset restore.
For 1.0.6.1, use a meter declaration that creates its own state: that release
reverts reuse of an existing metered set. Reuse is supported in 1.1.2.
JSON flag shape depends on version: 1.1.4 emits a single flag as a string for
sets, FIB expressions, and SYNPROXY, and multiple flags as an array. In 1.1.5,
array-valued fields retain their array shape even with one item.
`auto-merge` requires a non-concatenated interval set. In 1.1.2 it skips elements
with a timeout or expiration; in 1.1.3, intervals with different timeouts can
merge again when `auto-merge` is enabled.
## Load ruleset files and variables
`nft -f` accepts regular files and named pipes; `/dev/stdin` bypasses its file-type
check. The ruleset `include` directive requires regular files.
When loading a named file with `nft -f`, its directory comes first on the include
search path, before directories added with `-I`. A path starting with `./`
resolves from the working directory and bypasses the include search path.
Wildcard includes use C-locale collation order, skip dotfiles, and succeed with
zero matches. `include "/etc/nftables.d/*.nft"` loads `10-base.nft` before `20-site.nft`.
`-D name=value` works only with `-f`. For a ruleset referencing `$uplink`:
```sh
nft -c -D uplink=eth0 -f ruleset.nft
```
A definition is visible in its block and nested blocks. After
`define uplink = "eth0"`, use `redefine uplink = "wlan0"` to change it or
`undefine uplink` to remove it.
## Declare sets, maps, and packet-path state
`typeof` requires selectors that can be serialized. Redeclarations under one
name must agree on container kind and declared types; merged containers must
also agree on `flags interval`. Element keys and map values are checked against
their declared types. Only stateful statements are accepted in set and map definitions.
Declare a `size` for set updates from rules using `add` or `update`. The `dynamic`
and `interval` flags are mutually exclusive. Expired elements can occupy capacity
until garbage collection reclaims them.
```nft
set recent {
type ipv4_addr
flags dynamic,timeout
size 65535
timeout 1m
gc-interval 10s
}
```
`update @recent { ip saddr }` requires timeout support, enabled by a declaration
such as `timeout 1m`. Include `flags dynamic` if the update expression carries
state, such as a per-element rate limit. Maps also accept `gc-interval`.
`create element` requires every supplied key to be new. When restoring a set or
map with timeout support, `expires` is the remaining lifetime and `timeout` is
the configured lifetime:
```sh
nft 'add element inet filter blocked { 198.51.100.7 timeout 1h expires 12m }'
```
`get element` returns the interval containing the lookup key, including a map's
value. `reset element` also accepts a key inside an interval to reset element state.
Maps can return named `ct helper`, `ct timeout`, and `ct expectation` objects.
Object maps accept `typeof` key selectors, such as `typeof tcp dport : ct helper`.
Conntrack timeout policies accept units, for example `policy = { established : 5m }`.
Place `ct state new` before `ct count` to evaluate the limit only on packets in
the new conntrack state. Connection limits can be named stateful objects.
## Match and update packet expressions
A named set matched against `meta l4proto` can satisfy a statement's transport
protocol dependency, including for `tproxy`.
Compound bitwise assignments combining multiple packet expressions require
Linux 6.13 or newer. The right-hand operand of `^` must be constant.
```nft
ct mark set ct mark & 0xffff0000 | meta mark & 0xffff
meta mark set meta mark ^ 0x1
ip dscp set ip dscp | 0x1
```
A single expression can chain at most 16 binary operations. A shift count may
exceed the bit width of its left operand.
`@ih,offset,length` reads bits after the transport header. Supply an explicit
transport protocol guard for raw payload matches and for `th dport`:
```nft
meta l4proto udp @ih,0,8 0x80 counter
meta l4proto { tcp, udp } th dport { 53, 80 } counter
```
`expression value / mask` tests whether the masked expression equals the value.
For example, `tcp flags syn,ack / syn,ack,fin,rst counter` matches both SYN and
ACK set with both FIN and RST clear.
## Route, translate, and inspect tunnels
FIB `check` returns `exists` or `missing` and works in matches, map keys, and
expressions assigned by a `set` statement:
```nft
fib daddr . iif check missing drop
fib daddr check vmap { missing : drop, exists : accept }
```
FIB `oif` and `oifname` results are available in prerouting, input, and forward.
For a `type` result, `. iif` limits the lookup to those hooks; `. oif` limits it
to forward, output, and postrouting.
An `inet` NAT rule can infer its address family from the mapping value type.
Interval-valued mappings can supply a NAT range. A NAT statement using a map
rejects a separately specified NAT protocol operand.
For a named DNAT map returning address and port ranges, declare `interval` on
the value side of `typeof`; each range includes both endpoints. Use
`snat ip prefix to` to translate prefixes while preserving host bits.
Tunnel templates support VXLAN, ERSPAN, and Geneve. Tunnel source and destination
addresses must be symbolic expressions; the source may be omitted. Specify each
of `ip saddr` and `ip daddr` at most once and use one tunnel section per definition.
Match outer UDP before Geneve or VXLAN expressions, typically on destination
ports 6081 and 4789, respectively:
```nft
udp dport 6081 geneve vni 42 counter
udp dport 4789 vxlan tcp dport 80 counter
```
With an XFRM interface, put outbound policy matches such as
`ipsec out reqid 42 counter` in postrouting: IPsec metadata arrives after output.
## Manage hooks, tables, rules, and state
Creating a flowtable requires both a hook and a priority. Trailing `*` device
prefix wildcards work in netdev hooks and flowtable device lists. Device bindings
of an existing `inet` ingress base chain can be updated in place.
`flags owner;` makes a table writable only by its creating process and removes
it on process exit. `flags owner,persist;` keeps it after exit so a restarting
daemon can take ownership. `dormant` unregisters base chains; reissue `add table`
to activate the table again.
`destroy` allows an absent target, as in `nft destroy rule inet filter input handle 5`.
An existing chain must still be empty and unreferenced for `destroy chain`.
`add rule` places a rule after the specified location; `insert rule` places it
before. Zero-based indexes resolve to handles before submission, so concurrent
edits can change the target's position or cause failure if it is deleted.
JSON rule commands can use `handle` for the existing rule serving as insertion point.
Use object names for individual stateful object resets in native syntax, as in
`nft reset counter inet filter hits`. `nft reset rules inet filter input` resets
rule-local state in that scope. `reset set` and `reset map` reset counters and
quotas attached to all elements of the named container.
## Inspect, validate, and build
`nft -c -o -f ruleset.nft` previews optimizer merges. Duplicate keys prevent a
set or map merge. When merging NAT rules, the optimizer skips statements that
already use a map or reference a ruleset variable.
`nft monitor trace` can emit a `conntrack:` line with direction, state, ID, and
reported status flags; use the ID to correlate the connection across hooks.
`nft monitor` reports flowtable additions, deletions, and updates.
A JSON `concat` expression requires at least two expressions. JSON set elements
can carry multiple stateful statements. See the JSON reference for input schemas,
listing shapes, diagnostics, and netlink processing behavior.
Libnftables discards its cached ruleset in check mode (`-c` or `--check`) so a dry
run does not leave cached changes in a reused context. In 1.1.7, reset commands
issued with either option are also dry runs.
Building 1.1.4 requires libnftnl 1.3.0 or newer. Python binding installation is
separate. Static-ruleset systemd unit installation is disabled by default; enable
it with `--with-unitdir=/usr/lib/systemd/system`, using your distribution's unit
directory. Ancillary systemd files are installed only with the service file.
Use `make check` for the test suite. `make distcheck` works, and build commands
honor `CFLAGS` overrides supplied to `make`. Set `SOURCE_DATE_EPOCH` when running
`./configure` for the build timestamp; use `./configure --enable-profiling` for profiling.