Skip to the text
KDOS
Menu
kdos.dev KDOS from Scratch KDOS 0.2 · 4dd4a10 · 2026-11-30 Contents Index

410. Firewall

The KDOS firewall is one nftables ruleset, loaded at boot before the network starts. It drops what arrives unasked, apart from the few things a workstation has to receive: ICMP, mDNS, DHCPv6, NetBIOS name replies, WS-Discovery replies from the local network, and a hotspot’s DHCP and DNS requests. It forwards nothing but the traffic of a hotspot, rootful containers and libvirt guests, and lets everything out. This page covers the files that make up the ruleset, opening a named service from the desktop or the command line, adding rules of your own, checking what is loaded, and what to do when the ruleset does not load.

Installation

nftables provides nft, and is installed by 41_system on every image. The ruleset and the script that loads it are part of the base filesystem: fs/etc/nftables.conf, fs/etc/nftables.d/ and fs/etc/init.d/25_nftables.sh.

The service is nftables. It is enabled on the live medium and on an installed system alike, and the installer offers no choice about it. It is not supervised: nft hands the ruleset to the kernel and exits. It is also the one service /etc/init.d/rcK does not stop at shutdown.

Opening a named service goes through kdos-powerd, which owns the list of names, and its two clients: kdos-power firewall on the command line and the Firewall page of System Settings, from kcm-kdos-firewall, which also raises the notification that offers to let a blocked program in. iptables is installed too, built over the nftables kernel interface; the firewall does not use it.

Configuration

/etc/nftables.conf

The policy, from fs/etc/nftables.conf. It deletes and rebuilds its own table, inet filter, and nothing else, then includes every drop-in last:

#
# KDOS default firewall, loaded by /etc/init.d/25_nftables.sh.
#
# The policy is the one a workstation wants and is deliberately dull:
#   - anything this machine started, comes back
#   - loopback is trusted
#   - ICMP and ICMPv6 are ANSWERED, not dropped (see below)
#   - everything else arriving unasked is dropped
#   - everything going out is allowed
#
# It is a starting point a user is expected to edit, not a security product.
# Any service that should be reachable from the network needs a rule in a
# file of its own under /etc/nftables.d (its README shows the shape); the
# shipped chains exist so that is an addition rather than an invention.
#
# THIS FILE REPLACES ITS OWN TABLE, NOT THE RULESET. netavark keeps rootful
# podman's masquerade and published-port DNAT in `inet netavark`, and
# NetworkManager keeps a shared hotspot's in `nm-shared-*`; both are written
# only when a container or the hotspot starts. `flush ruleset` here would
# strip them on every reload — every firewall toggle is one — and cut
# running containers and hotspot clients off until they restart. The first
# line makes the table exist so the delete cannot fail on a first load; the
# whole file is still one transaction. Every file under /etc/nftables.d must
# therefore add to `table inet filter` and nothing else, or its table
# survives a reload with stale rules.

table inet filter
delete table inet filter

table inet filter {
	chain input {
		type filter hook input priority filter; policy drop;

		# The connection this machine opened, coming back. First rule
		# because it matches the overwhelming majority of packets.
		ct state established,related accept
		ct state invalid drop

		iif lo accept

		# ICMPv6 IS NOT OPTIONAL. Dropping it does not harden IPv6, it
		# breaks it: neighbour discovery, router advertisement and above
		# all Packet Too Big are how IPv6 works at all, and a host that
		# drops them gets a network that appears to connect and then
		# stalls on the first large transfer. This is the single most
		# common way a hand-written IPv6 firewall is wrong.
		ip6 nexthdr icmpv6 icmpv6 type {
			destination-unreachable,
			packet-too-big,
			time-exceeded,
			parameter-problem,
			echo-request,
			nd-router-advert,
			nd-neighbor-solicit,
			nd-neighbor-advert
		} accept

		# IPv4 echo and the unreachables path MTU discovery depends on.
		ip protocol icmp icmp type {
			destination-unreachable,
			time-exceeded,
			parameter-problem,
			echo-request
		} accept

		# mDNS, because avahi is installed and started by
		# fs/etc/init.d/45_avahi.sh — a machine that runs a responder
		# and firewalls its own port is advertising into a wall.
		udp dport 5353 accept

		# A NETBIOS NAME QUERY IS A BROADCAST AND ITS ANSWERS ARE NOT
		# THE CONNECTION COMING BACK. Dolphin's network browsing, and the
		# workgroup-name resolution behind a share mount, send to the
		# subnet's broadcast address; every machine answers from its
		# OWN, so conntrack holds no entry for any of them and the
		# first rule in this chain does not match. Without this a
		# browse finds nothing on a network full of file servers.
		#
		# SCOPED TO THE PORT PAIR A REPLY CARRIES, 137 to 137, rather
		# than opening the NetBIOS service: nothing on this image
		# listens there, and for the second or so `nmblookup` does, it
		# is the thing that asked.
		udp sport 137 udp dport 137 accept

		# A WS-DISCOVERY PROBE IS MULTICAST, SO ITS ANSWERS ARE NOT THE
		# CONNECTION COMING BACK EITHER. Dolphin's smb:/ lists Windows
		# machines, which no longer answer NetBIOS, by sending a probe
		# to 239.255.255.250 or ff02::c, port 3702; each host answers
		# from its own address and port 3702 to the probe's source
		# port, so conntrack holds no entry for the answer. Without this
		# the network view lists only the hosts NetBIOS finds.
		#
		# SCOPED TO AN ANSWER FROM THE LOCAL NETWORK INTO THE KERNEL'S
		# EPHEMERAL RANGE (net.ipv4.ip_local_port_range): a private or
		# link-local source, source port 3702, and the kind of port a
		# probe's socket is given. A service listening below 32768 is
		# not reachable through it.
		ip saddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 169.254.0.0/16 } udp sport 3702 udp dport 32768-60999 accept
		ip6 saddr { fc00::/7, fe80::/10 } udp sport 3702 udp dport 32768-60999 accept

		# DHCPv6 client.
		udp dport 546 accept

		# A HOTSPOT'S CLIENTS NEED A LEASE AND A RESOLVER, and this is
		# the only place they can be let in. Plasma's Wi-Fi hotspot is
		# NetworkManager's `ipv4.method = shared`, which starts dnsmasq
		# on the access point's own interface; NetworkManager's shared
		# table deliberately carries NO input chain, so with the policy
		# above a client associates, ARPs, answers a ping and never
		# gets an address.
		#
		# SCOPED TO THE SUBNET NetworkManager HANDS OUT, not to an
		# interface name this file cannot know: 10.42.0.0/16 is the
		# pool it allocates a /24 out of for every shared connection.
		# Off that subnet these two ports stay closed.
		ip saddr 10.42.0.0/16 udp dport { 53, 67 } accept
		ip saddr 10.42.0.0/16 tcp dport 53 accept

		# --- Add your own below. Examples, deliberately commented: ---
		# tcp dport 22   accept      # sshd (70_sshd.sh)
		# tcp dport 8080 accept      # kiwix-serve
		# tcp dport 631  accept      # cups, if this machine SHARES a printer
	}

	chain forward {
		# Nothing is routed through a workstation by default. The
		# exceptions are the hotspot below and rootful podman's bridges,
		# in /etc/nftables.d/40-podman.nft.
		type filter hook forward priority filter; policy drop;

		# EXCEPT A HOTSPOT, AND THE POLICY ABOVE IS WHY THIS HAS TO BE
		# HERE. NetworkManager registers its own forward chain on this
		# same hook with policy accept when a shared connection comes
		# up — but in netfilter an `accept` verdict ends only the chain
		# that issued it: the packet still meets every other base chain
		# on the hook, and a drop anywhere is final. So NetworkManager's
		# chain cannot rescue a packet this one drops, in either
		# registration order, and a hotspot would associate, lease an
		# address and route nothing.
		#
		# The subnet is NetworkManager's own shared pool, so this opens
		# forwarding for a hotspot and for nothing else.
		ct state established,related accept
		ip saddr 10.42.0.0/16 accept
		ip daddr 10.42.0.0/16 accept
	}

	chain output {
		type filter hook output priority filter; policy accept;
	}
}

# EVERYTHING UNDER /etc/nftables.d, LAST.
#
# A file there re-opens a chain above and appends to it, which is
# why this include is at the bottom: nft evaluates rules in the order they
# were added, and the chain POLICY is what applies when none matched — so an
# accept appended here is still reached.
#
# kdos-powerd owns 50-kdos-services.nft and rewrites it whole; a closed
# service's line there counts its packets and accepts none, so they reach
# this chain's drop. Anything hand-written belongs in another file beside
# it. 40-podman.nft is shipped
# there too: it lets rootful podman's bridges through the forward chain.
#
# A glob matching nothing is not an error, but the directory ships with files
# in it so the common case is never the untested one.
include "/etc/nftables.d/*.nft"

fs/etc/nftables.conf

What each rule is for is in The firewall and The firewall.

/etc/nftables.d/

Every *.nft file here is included at the end of nftables.conf, and must add to table inet filter without declaring a table or a chain hook of its own. The directory’s README, from fs/etc/nftables.d/README:

Included at the bottom of /etc/nftables.conf, as `*.nft`.

A file here adds rules to the ruleset the main file builds. To add to a chain
that already exists, name the table and chain WITHOUT a `type`/`hook` line —
that re-opens the chain; repeating the hook declares a second one and nft
refuses the load.

    table inet filter {
        chain input {
            tcp dport 8080 accept
        }
    }

Add to `inet filter` and declare no table of your own. The main file deletes
and rebuilds only that table, so that netavark's and NetworkManager's NAT
tables survive a reload; a table declared here would survive it too, keeping
its old rules alongside the new ones.

50-kdos-services.nft is kdos-powerd's, for System Settings' Firewall page and
`kdos-power firewall`, and is rewritten whole on every toggle: an open service
is its accept rule, and a closed one counts its packets from another machine
in the named counter kdos_knock_<name> and leaves every packet to the chain's
drop; this machine's own broadcasts, looped back, are not counted. Anything you write by
hand belongs in a file of your own, which the daemon never touches; a port
such a file opens is still counted in 50-kdos-services.nft, whose rules come
first.

40-podman.nft ships here and re-opens the forward chain as well as input: it
lets rootful podman's bridge networks (podman0, podman1, ...) route and reach
aardvark-dns.

40-libvirt.nft is installed by the libvirt package and does the same for
libvirt's NAT networks (virbr0, ...): it forwards the guests' traffic and lets
them reach the network's dnsmasq for DHCP and DNS. It goes when libvirt does.

The load is checked before it is applied — see /etc/init.d/25_nftables.sh —
so a file with a syntax error refuses the whole ruleset rather than leaving
half of it applied. The previous ruleset stays in the kernel until a good one
replaces it.

fs/etc/nftables.d/README

File Written by Opens
40-podman.nft The base filesystem, fs/etc/nftables.d/ Forwarding and DNS for rootful podman’s podman* bridges
40-libvirt.nft The libvirt package Forwarding, DHCP and DNS for libvirt’s virbr* bridges
50-kdos-services.nft The base filesystem, as the daemon writes it with every service closed; rewritten whole by kdos-powerd The named services that are on; each closed one counts the packets from another machine that knock on it

The shipped 50-kdos-services.nft:

# Written by kdos-powerd, for the Firewall settings page and
# `kdos-power firewall`. Rewritten WHOLE on every change, so a
# rule it does not recognise is dropped — anything hand-made
# belongs in a file beside this one, which this never touches.
#
# An open name is its rule. A closed name's packets from another
# machine are counted in kdos_knock_<name>; every closed name's
# packets then meet the chain's policy.
#
# No `type`/`hook` line: that is what re-opens the existing
# input chain rather than declaring a second one.

table inet filter {
	counter kdos_knock_ssh {
	}
	counter kdos_knock_http {
	}
	counter kdos_knock_https {
	}
	counter kdos_knock_ipp {
	}
	counter kdos_knock_smb {
	}
	counter kdos_knock_kiwix {
	}
	counter kdos_knock_mqtt {
	}
	counter kdos_knock_xmpp {
	}
	counter kdos_knock_nfs {
	}
	counter kdos_knock_babel {
	}
	counter kdos_knock_nut {
	}
	counter kdos_knock_snmp {
	}
	counter kdos_knock_mumble {
	}
	counter kdos_knock_caldav {
	}
	counter kdos_knock_mail {
	}
	counter kdos_knock_irc {
	}
	counter kdos_knock_dlna {
	}
	counter kdos_knock_tryton {
	}
	counter kdos_knock_caddy {
	}
	counter kdos_knock_mosh {
	}
	counter kdos_knock_syncthing {
	}
	counter kdos_knock_kdeconnect {
	}
	counter kdos_knock_vnc {
	}
	counter kdos_knock_rdp {
	}
	counter kdos_knock_xonotic {
	}
	chain input {
		fib saddr type != local tcp dport 22 counter name "kdos_knock_ssh"
		fib saddr type != local tcp dport 80 counter name "kdos_knock_http"
		fib saddr type != local tcp dport 443 counter name "kdos_knock_https"
		fib saddr type != local tcp dport 631 counter name "kdos_knock_ipp"
		fib saddr type != local tcp dport 445 counter name "kdos_knock_smb"
		fib saddr type != local tcp dport 8080 counter name "kdos_knock_kiwix"
		fib saddr type != local tcp dport { 1883, 8883 } counter name "kdos_knock_mqtt"
		fib saddr type != local tcp dport 5222 counter name "kdos_knock_xmpp"
		fib saddr type != local tcp dport 2049 counter name "kdos_knock_nfs"
		fib saddr type != local udp dport 6696 counter name "kdos_knock_babel"
		fib saddr type != local tcp dport 3493 counter name "kdos_knock_nut"
		fib saddr type != local udp dport 161 counter name "kdos_knock_snmp"
		fib saddr type != local meta l4proto . th dport { tcp . 64738, udp . 64738 } counter name "kdos_knock_mumble"
		fib saddr type != local tcp dport 5232 counter name "kdos_knock_caldav"
		fib saddr type != local tcp dport { 25, 143, 465, 587, 993 } counter name "kdos_knock_mail"
		fib saddr type != local tcp dport { 6667, 6697 } counter name "kdos_knock_irc"
		fib saddr type != local meta l4proto . th dport { tcp . 8200, udp . 1900 } counter name "kdos_knock_dlna"
		fib saddr type != local tcp dport 8000 counter name "kdos_knock_tryton"
		fib saddr type != local tcp dport 8443 counter name "kdos_knock_caddy"
		fib saddr type != local udp dport 60000-61000 counter name "kdos_knock_mosh"
		fib saddr type != local meta l4proto . th dport { tcp . 22000, udp . 22000, udp . 21027 } counter name "kdos_knock_syncthing"
		fib saddr type != local meta l4proto { tcp, udp } th dport 1714-1764 counter name "kdos_knock_kdeconnect"
		fib saddr type != local tcp dport 5900 counter name "kdos_knock_vnc"
		fib saddr type != local tcp dport 3389 counter name "kdos_knock_rdp"
		fib saddr type != local udp dport 26000 counter name "kdos_knock_xonotic"
	}
}

fs/etc/nftables.d/50-kdos-services.nft

/etc/init.d/25_nftables.sh

The start branch checks the whole ruleset with nft -c and loads it only when the check passes:

case "$1" in
    start)
        if [ ! -x "$NFT" ]; then
            echo "[SKIP] $NAME: $NFT not found"
            exit 0
        fi
        if [ ! -f "$CONF" ]; then
            echo "[SKIP] $NAME: no $CONF — no ruleset to load"
            exit 0
        fi

        # CHECK BEFORE LOADING, AND REFUSE RATHER THAN HALF-APPLY.
        #
        # The file deletes and rebuilds `inet filter`, so an error partway
        # through would leave the rules read so far and none of the rest — typically
        # the input chain's drop policy with its accept rules missing, which is
        # a policy nobody wrote. `nft -c` runs the whole file against the
        # kernel and commits nothing, so an unloadable ruleset leaves a
        # loaded one standing, and load_fallback covers a boot with none.
        if ! "$NFT" -c -f "$CONF" 2>/tmp/nft-check.$$; then
            load_fallback
            sed 's/^/       /' /tmp/nft-check.$$
            rm -f /tmp/nft-check.$$
            exit 1
        fi
        rm -f /tmp/nft-check.$$

        echo "[KDOS] Loading $NAME ruleset..."
        "$NFT" -f "$CONF"
        ;;

fs/etc/init.d/25_nftables.sh, lines 65–94

When the check fails and no inet filter table is loaded yet, as at boot, load_fallback loads nftables.conf without its include line, and when that fails too, a minimal drop policy written to /run/nftables-fallback.nft. Either way the script reports [FAIL]. stop deletes inet filter and nothing else.

Usage

Seeing what is loaded

  1. Ask the service:

    sudo service status nftables

    It answers nftables: ruleset loaded or nftables: no ruleset loaded. Run it with sudo: the kernel hands the ruleset only to root, so as an ordinary user it answers nftables: needs root to read the ruleset, and service list shows that line in the nftables row whether a ruleset is loaded or not.

  2. List the rules the kernel holds:

    sudo nft list table inet filter    # this firewall
    sudo nft list ruleset              # with the tables other programs add
  3. List the named services and whether each is open (any account):

    kdos-power firewall list

    nft list ruleset on a fresh boot shows table inet filter and, with libvirt installed, libvirt’s own ip libvirt_network and ip6 libvirt_network; podman and a NetworkManager hotspot add theirs while they run.

Opening a named service

The names, and the ports each one opens, are compiled into kdos-powerd; the table is in firewall. Who may turn one on or off depends on the route:

  1. With the desktop: System Settings → Firewall. Turn the service’s switch on or off and Apply, and type an administrator’s password when asked.

  2. From a shell, as a member of wheel:

    kdos-power firewall https on
    kdos-power firewall https off

    A successful change prints ok https on. The daemon rewrites 50-kdos-services.nft, checks the whole ruleset and reloads it. The change is in force at once and at every later boot: the rewritten file holds the rule, tcp dport 443 accept, and so does sudo nft list chain inet filter input.

  3. Start the daemon that answers on the port, if it is not running: opening ipp, for example, does not start CUPS. ssh is the exception: turning it on also starts sshd and enables it at boot; see SSH server.

Adding a rule of your own

For a port no name covers.

  1. Create a file of your own in /etc/nftables.d, for example /etc/nftables.d/60-local.nft, which re-opens the input chain without a type … hook line:

    table inet filter {
        chain input {
            tcp dport 3000 accept
        }
    }

    To open a port to one network only, match the source as well: ip saddr 192.168.1.0/24 tcp dport 3000 accept.

  2. Check the whole ruleset without applying it:

    sudo nft -c -f /etc/nftables.conf

    No output means it would load. An error names the file and the line, as in /etc/nftables.d/60-local.nft:3:29-29: Error: syntax error, unexpected newline.

  3. Apply it:

    sudo service start nftables

    start reloads: the file deletes and rebuilds inet filter in one transaction, so a running container’s address translation and a hotspot’s tables survive.

  4. Do not write into 50-kdos-services.nft. kdos-powerd rewrites it whole at the next toggle, and a rule it does not recognise is dropped.

Turning the firewall off

  1. For this boot only:

    sudo service stop nftables

    It prints [KDOS] Removing nftables ruleset... and deletes inet filter alone; libvirt’s and podman’s tables stay. The machine then accepts every connection until the next start or the next boot.

  2. At every boot, with sudo service disable nftables, which writes /etc/service.disabled/nftables and prints Service 'nftables' disabled. sudo service enable nftables removes it; the marker is described in Choosing what starts at boot.

On the live medium the disable marker is lost at power-off unless a persistence store exists.

Tips

Troubleshooting

The boot shows [FAIL] nftables: /etc/nftables.conf does not load. Read the script’s boot log, which holds nft’s own error lines:

cat /run/kdos-init.nftables.log

The rest of the line says what was loaded instead: loaded it without /etc/nftables.d (a drop-in is wrong) or loaded the minimal drop policy (the main file is wrong). Fix the file the error names, check with sudo nft -c -f /etc/nftables.conf, and run sudo service start nftables. no ruleset could be loaded — input is NOT filtered means even the fallback failed; check that the kernel has nftables with sudo nft list tables.

Run by hand on a machine whose inet filter table is already loaded, the same failure loads nothing and ends keeping the current ruleset.

nft -c says Chain "input" already exists in table inet 'filter' with different declaration. The drop-in repeats the chain’s type filter hook … line with another priority. Remove that line; naming the table and the chain alone re-opens the existing one. A type … hook line that matches the main file’s exactly is accepted and adds to the same chain, so the check does not catch it, and a policy on that line replaces the chain’s own; leave both out of a drop-in.

kdos-power firewall <name> on prints err the ruleset would not load; nothing changed. A hand-written drop-in is broken, so the whole ruleset fails its check. The kernel keeps the rules it had, and the daemon puts the previous 50-kdos-services.nft back, so firewall list and the next boot keep the previous state. Run sudo nft -c -f /etc/nftables.conf, fix the file it names, and repeat the toggle. err the ruleset did not apply is the same refusal from the kernel after the check passed, and leaves the same previous state.

kdos-power firewall <name> on prints err cannot read the current rules. 50-kdos-services.nft exists but the daemon cannot read it, so it could not put the file back if the new ruleset were refused, and it changes nothing. Check the file with ls -l /etc/nftables.d/50-kdos-services.nft; it belongs to root and is readable by its owner. If it is damaged, delete it and repeat the toggle: the daemon writes the file whole, and with no file every service starts from off, so turn the others back on afterwards.

kdos-power firewall prints err no service called that. The name is not in the daemon’s table. Run kdos-power firewall list for the names, or write a drop-in.

kdos-power firewall prints kdos-power: no kdos-powerd — `service start powerd`, or err not permitted. Start the daemon with sudo service start powerd, or run the command as a member of wheel.

A hotspot’s clients join and get no address. The input rules for 10.42.0.0/16 are missing or a drop-in drops that traffic first. Check sudo nft list chain inet filter input for the ip saddr 10.42.0.0/16 lines.

A rootful container reaches nothing, or a libvirt guest gets no address. Check that 40-podman.nft, or 40-libvirt.nft, is in /etc/nftables.d, and that sudo nft list chain inet filter forward shows its rules.

A rule added by hand vanished. It was in 50-kdos-services.nft, which kdos-powerd rewrites whole. Move it to a file of your own.

Known gaps

See also