48 Commits

Author SHA1 Message Date
benvin 63c46fec81 Merge pull request 'Match dest zone oif on output-chain rules and policies' (#20) from benvin/output-oif into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #20
2026-10-03 22:51:28 +10:00
unkin-agent 5238886c6e Merge remote-tracking branch 'origin/main' into benvin/output-oif
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
# Conflicts:
#	internal/nftables/compiler_test.go
2026-10-03 22:46:09 +10:00
benvin 532bd80a8a Merge pull request 'Match ORIGDEST in DNAT and filter rules' (#21) from benvin/origdest into main
Reviewed-on: #21
2026-10-03 22:44:37 +10:00
unkin-agent 036021d726 Treat only non-negated addresses as scoping interfaceless zones
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 22:20:41 +10:00
unkin-agent ff5a52b9e2 Reject ORIGDEST on forwarded rules and mixed-family lists
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 22:18:48 +10:00
unkin-agent 44e1ba852e Skip rules and policies for zones without interfaces
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 22:18:10 +10:00
unkin-agent 34cf6dc9ab Match ORIGDEST in DNAT and filter rules
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 22:16:40 +10:00
unkin-agent b5be665902 Match dest zone oif on output-chain rules and policies
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 22:15:24 +10:00
benvin 78afe7c242 Merge pull request 'fix: stop differential apply rewriting unchanged rules' (#18) from benvin/diff-expr-equality into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #18
2026-10-03 21:24:48 +10:00
unkin-agent 1ad025a4b7 Merge remote-tracking branch 'origin/main' into benvin/diff-expr-equality
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
# Conflicts:
#	internal/nftables/diff.go
2026-10-03 21:16:39 +10:00
unkin-agent aa0d3e10c2 Merge remote-tracking branch 'origin/main' into benvin/diff-expr-equality
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
# Conflicts:
#	internal/nftables/compiler.go
#	internal/nftables/compiler_test.go
2026-10-03 21:15:27 +10:00
benvin 3687baabe5 Merge pull request 'Fix MSS clamp loading the option as an IPv6 exthdr' (#19) from benvin/mss-clamp-tcpopt into main
Reviewed-on: #19
2026-10-03 21:15:19 +10:00
unkin-agent abbf434af5 Merge remote-tracking branch 'origin/main' into benvin/mss-clamp-tcpopt
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
# Conflicts:
#	internal/nftables/compiler_test.go
2026-10-03 21:14:10 +10:00
benvin 937556abeb Merge pull request 'Add try/confirm safe-apply with out-of-process revert' (#17) from benvin/safe-apply into main
Reviewed-on: #17
2026-10-03 21:13:57 +10:00
benvin a7be035456 Merge pull request 'Match any listed port or protocol in a rule' (#15) from benvin/multiport-multiproto into main
Reviewed-on: #15
2026-10-03 21:13:10 +10:00
benvin e6244d6bf0 Merge pull request 'Expand comma zone lists in rule source and dest' (#16) from benvin/comma-zones into benvin/multiport-multiproto
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Reviewed-on: #16
2026-10-03 21:12:52 +10:00
unkin-agent 0b92f1c2f3 Refuse mutating commands during a try and scope reverts to the try ID
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
apply, flush and purge take the try lock and fail with ErrPending, which now
names 'tomswall revert' as the recovery after a failed automatic revert. Each
try gets an ID passed to the timer's 'revert --id', so a stale timer cannot
revert a newer try. Adds tests for Revert success/failure/stale ID and for
restoring a present table at the engine level.
2026-10-03 20:56:52 +10:00
unkin-agent 6ac03e1012 Persist try snapshot and arm a systemd revert timer
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:52:42 +10:00
unkin-agent 7f9c010e1a Drop agent auto-revert; skip agent apply while a try is pending 2026-10-03 20:52:42 +10:00
unkin-agent df7ebb8efe fix: preserve rule order in diff and insert replacements in place
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:52:01 +10:00
unkin-agent 457056d58a Pick reject type by resolved protocol number
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:51:25 +10:00
unkin-agent 8efed72c96 Match exact all/any zone tokens, skip only interface-less ip zones, keep DNAT free of rule extras, reject '!' inside address lists
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:50:36 +10:00
unkin-agent ed3209681d Load MSS option via tcpopt exthdr op in clamp rule
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:50:13 +10:00
unkin-agent 852d6bf2ca Resolve common IANA protocol names and reject ports on portless protocols
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
2026-10-03 20:49:54 +10:00
unkin-agent f522589a83 Merge remote-tracking branch 'origin/benvin/multiport-multiproto' into benvin/comma-zones 2026-10-03 20:48:31 +10:00
unkin-agent e16d95fb63 fix: compare rule expressions by value in diff
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:48:29 +10:00
unkin-agent 9c30f1fa54 Reject unknown protocols and dports on mixed ICMP proto lists
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:47:46 +10:00
unkin-agent ad2dd9e3e7 Merge benvin/multiport-multiproto; reject limits on zone and address lists
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:46:37 +10:00
unkin-agent cc12c4a43a Add try/confirm safe-apply and agent auto-revert
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:45:57 +10:00
unkin-agent 211bacd507 Expand comma zone lists in rule source and dest
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:45:32 +10:00
unkin-agent 30758855ff Reject empty list elements, unknown ICMP types and limits on list rules
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:45:16 +10:00
unkin-agent 9976bc9190 Match any listed port or protocol instead of AND-ing them
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
2026-10-03 20:41:42 +10:00
benvin 410109515e Merge pull request 'ci: switch Go toolchain steps to gobuilder + shared S3 cache' (#14) from benvin/gocache into main
Reviewed-on: #14
2026-10-02 23:56:29 +10:00
unkin-agent 27504777f8 ci: switch Go toolchain steps to gobuilder + shared S3 cache
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
Cut CI time/network by caching go build/vet/test artifacts in S3.
- bump build/test/pre-commit/release(test,build) to
  artifactapi.../gobuilder:0.1.2-alma9
- wire GOCACHEPROG via go-cache-plugin with an absolute cache dir
- add GOCACHE_* env (ci-tomswall prefix) and AWS creds from org secrets
- leave rpm package/upload steps untouched (no Go toolchain)
2026-10-02 23:53:13 +10:00
benvin fff9967da0 Merge pull request 'Agent: translate blrules/conntrack/secmarks/vars' (#8) from benvin/agent-longtail-global2 into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #8
2026-07-26 16:56:25 +10:00
benvin 4b703f854c Merge pull request 'Agent: translate traffic-control long-tail (mangle/accounting/tc_*)' (#7) from benvin/agent-longtail-tc into main
Reviewed-on: #7
2026-07-26 16:55:32 +10:00
benvin 8ae09c0941 Agent: translate blrules/conntrack/secmarks/vars
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Map the rendered global-compiled tail into native config.Blrules/Conntrack/
Secmarks/Vars.
2026-07-26 16:07:28 +10:00
benvin 6d16035a0b Agent: translate traffic-control long-tail (mangle/accounting/tc_*)
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Map the rendered mangle/accounting/tc_* sections into native tomswall config.
2026-07-26 15:57:05 +10:00
benvin 09f39ec9fe Merge pull request 'Agent: translate per-device L2/misc long-tail' (#6) from benvin/agent-longtail-l2 into main
Reviewed-on: #6
2026-07-26 15:46:07 +10:00
benvin b94cb96510 Agent: translate per-device L2/misc long-tail
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Map the rendered tunnels/stopped_rules/proxy_arp/proxy_ndp/arp_rules/maclist
sections into native tomswall config.
2026-07-26 15:19:09 +10:00
benvin 9fd300652f Merge pull request 'Agent: translate per-device routing long-tail' (#5) from benvin/agent-longtail-routing into main
Reviewed-on: #5
2026-07-26 15:09:15 +10:00
benvin 59e8320dda Agent: translate per-device routing long-tail (hosts/providers/routes/routing_rules)
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
Map the rendered hosts/providers/routes/routing_rules sections into native
tomswall config (config.Host/Provider/StaticRoute/RoutingRule). The route's
egress interface (oif) maps to StaticRoute.Device.
2026-07-26 13:02:17 +10:00
benvin 17b2130047 Merge pull request 'Agent: translate the NAT tier into native config' (#4) from benvin/agent-nat into main
Reviewed-on: #4
2026-07-21 22:30:10 +10:00
benvin 06928bc150 Agent: translate the NAT tier into native config
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
The agent now maps the rendered NAT sections into native tomswall config:
- snat/masquerade -> config.SNAT, expanding a rendered rule's egress interface
  list and source CIDRs into one native rule per (egress, source) pair (a native
  SNAT rule takes a single dest interface); carries address/probability.
- netmap -> config.Netmap (from_net/to_net -> net1/net2 on the resolved interface).
- 1:1 nat -> config.StaticNAT.
Unit-tested end to end from RenderedConfig to config.Config.
2026-07-21 22:21:24 +10:00
benvin 6f1ac9a1ae Merge pull request 'Agent: report the FIB for reachability scoping' (#3) from benvin/agent-fib into main
Reviewed-on: #3
2026-07-20 22:40:25 +10:00
benvin a739d87597 Merge pull request 'Add release machinery: version bump, nfpm RPM, release-on-tag' (#2) from benvin/release-machinery into main
Reviewed-on: #2
2026-07-20 22:39:21 +10:00
benvin 66265764df Agent: report the FIB for reachability scoping
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
The agent now collects the device's reachable prefixes from the kernel FIB
(including FRR-installed routes) via 'ip route show' / 'ip -6 route show' and
reports them to the control plane (POST /devices/{name}/routes) alongside its
status. tomswallapi uses these to scope which routers enforce a rule. Route
parsing (default routes, ECMP nexthop lines, route-type keywords, host routes,
v4/v6) is unit-tested; collection degrades to nil without iproute2.
2026-07-20 22:37:24 +10:00
benvin a3b51018a9 Add release machinery: version bump, nfpm RPM, release-on-tag pipeline
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
- Makefile: add make patch|minor|major (tag + push), dist-build, completions,
  and rpm/rpm-package targets.
- packaging/nfpm.yaml + scripts/build-rpm.sh: package the tomswall binary with
  bash/zsh completions, the example config, and a systemd agent unit into an RPM.
- packaging/tomswall-agent.service + agent.env: run `tomswall agent` as a
  systemd service (CAP_NET_ADMIN/CAP_NET_RAW), configured via /etc/tomswall/agent.env.
- .woodpecker/release.yaml: on v* tag, test -> build -> package RPM -> PUT to the
  artifactapi rpm-internal repo. Matches node-lookup conventions.
2026-07-20 22:30:57 +10:00
33 changed files with 3680 additions and 351 deletions
+13 -1
View File
@@ -3,9 +3,21 @@ when:
steps:
- name: build
image: golang:1.23
image: artifactapi.k8s.syd1.au.unkin.net/docker-internal/gobuilder:0.1.2-alma9
commands:
- go build ./...
environment:
GOCACHE_S3_BUCKET: gocache
GOCACHE_S3_REGION: us-east-1
GOCACHE_S3_ENDPOINT_URL: https://s3.ceph.unkin.net
GOCACHE_S3_PATH_STYLE: "true"
GOCACHE_KEY_PREFIX: ci-tomswall
GOCACHE_METRICS: "true"
GOCACHEPROG: go-cache-plugin --cache-dir=/tmp/gocache
AWS_ACCESS_KEY_ID:
from_secret: GOCACHE_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY:
from_secret: GOCACHE_AWS_SECRET_ACCESS_KEY
backend_options:
kubernetes:
serviceAccountName: default
+13 -1
View File
@@ -3,9 +3,21 @@ when:
steps:
- name: pre-commit
image: git.unkin.net/unkin/almalinux9-gobuilder:20260606
image: artifactapi.k8s.syd1.au.unkin.net/docker-internal/gobuilder:0.1.2-alma9
commands:
- uvx pre-commit run --all-files
environment:
GOCACHE_S3_BUCKET: gocache
GOCACHE_S3_REGION: us-east-1
GOCACHE_S3_ENDPOINT_URL: https://s3.ceph.unkin.net
GOCACHE_S3_PATH_STYLE: "true"
GOCACHE_KEY_PREFIX: ci-tomswall
GOCACHE_METRICS: "true"
GOCACHEPROG: go-cache-plugin --cache-dir=/tmp/gocache
AWS_ACCESS_KEY_ID:
from_secret: GOCACHE_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY:
from_secret: GOCACHE_AWS_SECRET_ACCESS_KEY
backend_options:
kubernetes:
serviceAccountName: default
+106
View File
@@ -0,0 +1,106 @@
when:
- event: tag
ref: refs/tags/v*
steps:
- name: test
image: artifactapi.k8s.syd1.au.unkin.net/docker-internal/gobuilder:0.1.2-alma9
commands:
- go test ./...
environment:
GOCACHE_S3_BUCKET: gocache
GOCACHE_S3_REGION: us-east-1
GOCACHE_S3_ENDPOINT_URL: https://s3.ceph.unkin.net
GOCACHE_S3_PATH_STYLE: "true"
GOCACHE_KEY_PREFIX: ci-tomswall
GOCACHE_METRICS: "true"
GOCACHEPROG: go-cache-plugin --cache-dir=/tmp/gocache
AWS_ACCESS_KEY_ID:
from_secret: GOCACHE_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY:
from_secret: GOCACHE_AWS_SECRET_ACCESS_KEY
backend_options:
kubernetes:
serviceAccountName: default
resources:
requests:
memory: 512Mi
cpu: 1
limits:
memory: 2Gi
cpu: 2
- name: build
image: artifactapi.k8s.syd1.au.unkin.net/docker-internal/gobuilder:0.1.2-alma9
commands:
- make dist-build VERSION=${CI_COMMIT_TAG}
depends_on: [test]
environment:
GOCACHE_S3_BUCKET: gocache
GOCACHE_S3_REGION: us-east-1
GOCACHE_S3_ENDPOINT_URL: https://s3.ceph.unkin.net
GOCACHE_S3_PATH_STYLE: "true"
GOCACHE_KEY_PREFIX: ci-tomswall
GOCACHE_METRICS: "true"
GOCACHEPROG: go-cache-plugin --cache-dir=/tmp/gocache
AWS_ACCESS_KEY_ID:
from_secret: GOCACHE_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY:
from_secret: GOCACHE_AWS_SECRET_ACCESS_KEY
backend_options:
kubernetes:
serviceAccountName: default
resources:
requests:
memory: 512Mi
cpu: 1
limits:
memory: 2Gi
cpu: 2
- name: package
image: git.unkin.net/unkin/almalinux9-rpmbuilder:latest
commands:
- ./scripts/build-rpm.sh ${CI_COMMIT_TAG}
depends_on: [build]
backend_options:
kubernetes:
serviceAccountName: default
resources:
requests:
memory: 512Mi
cpu: 1
limits:
memory: 2Gi
cpu: 2
- name: upload-rpm
image: git.unkin.net/unkin/almalinux9-base:20260606
commands:
- |
HOST="https://artifactapi.k8s.syd1.au.unkin.net"
REPO="rpm-internal"
for rpm in dist/*.rpm; do
FILE=$$(basename "$$rpm")
code=$$(curl -s -o /dev/null -w '%{http_code}' "$$HOST/api/v2/remotes/$$REPO/files/Packages/$$FILE" || true)
if [ "$$code" = "200" ]; then
echo "$$FILE already exists in $$REPO (HTTP $$code); skipping upload"
continue
fi
echo "Uploading $$FILE to $$REPO (existence probe returned $$code)"
curl -f -X PUT \
"$$HOST/api/v2/remotes/$$REPO/files/$$FILE" \
-H "Content-Type: application/x-rpm" \
--data-binary @"$$rpm"
done
depends_on: [package]
backend_options:
kubernetes:
serviceAccountName: default
resources:
requests:
memory: 128Mi
cpu: 100m
limits:
memory: 512Mi
cpu: 500m
+13 -1
View File
@@ -3,10 +3,22 @@ when:
steps:
- name: test
image: golang:1.23
image: artifactapi.k8s.syd1.au.unkin.net/docker-internal/gobuilder:0.1.2-alma9
commands:
- go vet ./...
- go test ./...
environment:
GOCACHE_S3_BUCKET: gocache
GOCACHE_S3_REGION: us-east-1
GOCACHE_S3_ENDPOINT_URL: https://s3.ceph.unkin.net
GOCACHE_S3_PATH_STYLE: "true"
GOCACHE_KEY_PREFIX: ci-tomswall
GOCACHE_METRICS: "true"
GOCACHEPROG: go-cache-plugin --cache-dir=/tmp/gocache
AWS_ACCESS_KEY_ID:
from_secret: GOCACHE_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY:
from_secret: GOCACHE_AWS_SECRET_ACCESS_KEY
backend_options:
kubernetes:
serviceAccountName: default
+55 -5
View File
@@ -1,12 +1,17 @@
BINARY := tomswall
MODULE := git.unkin.net/unkin/tomswall
PREFIX := /usr/local
BINARY := tomswall
MODULE := git.unkin.net/unkin/tomswall
PREFIX := /usr/local
CONFDIR := /etc/tomswall
DIST := dist
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
GOFLAGS := -ldflags="-s -w -X main.version=$(VERSION)"
OS ?= $(shell go env GOOS)
ARCH ?= $(shell go env GOARCH)
.PHONY: build install clean check test
.PHONY: build install clean check test fmt dist-build completions rpm rpm-package patch minor major _tag
build:
go build -o $(BINARY) ./cmd/tomswall
go build $(GOFLAGS) -o $(BINARY) ./cmd/tomswall
install: build
install -Dm755 $(BINARY) $(DESTDIR)$(PREFIX)/sbin/$(BINARY)
@@ -17,9 +22,54 @@ install: build
clean:
rm -f $(BINARY)
rm -rf $(DIST)
check:
go vet ./...
test:
go test ./...
fmt:
gofmt -w .
# Build the binary into dist/ for the RPM packaging step.
dist-build:
@mkdir -p $(DIST)
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build $(GOFLAGS) -o $(DIST)/$(BINARY) ./cmd/tomswall
# Generate bash/zsh completions into dist/completions.
completions: dist-build
@mkdir -p $(DIST)/completions
$(DIST)/$(BINARY) completion bash > $(DIST)/completions/$(BINARY).bash
$(DIST)/$(BINARY) completion zsh > $(DIST)/completions/_$(BINARY)
# Build the binary then package it (with completions) into an RPM via nfpm.
rpm: dist-build rpm-package
# Package an already-built dist/ binary into an RPM (used by CI after build).
rpm-package:
./scripts/build-rpm.sh $(VERSION)
# Bump helpers — read the latest semver tag and create+push the next one, which
# triggers the release-on-tag pipeline. Starts from v0.0.0 when no tag exists.
_LATEST := $(shell git tag --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$$' | head -1)
_BASE := $(if $(_LATEST),$(_LATEST),v0.0.0)
_MAJ := $(shell echo $(_BASE) | sed 's/^v//' | cut -d. -f1)
_MIN := $(shell echo $(_BASE) | sed 's/^v//' | cut -d. -f2)
_PAT := $(shell echo $(_BASE) | sed 's/^v//' | cut -d. -f3)
patch:
@NEW=v$(_MAJ).$(_MIN).$(shell expr $(_PAT) + 1); \
git tag $$NEW && echo "Tagged $$NEW" && $(MAKE) _tag TAG=$$NEW
minor:
@NEW=v$(_MAJ).$(shell expr $(_MIN) + 1).0; \
git tag $$NEW && echo "Tagged $$NEW" && $(MAKE) _tag TAG=$$NEW
major:
@NEW=v$(shell expr $(_MAJ) + 1).0.0; \
git tag $$NEW && echo "Tagged $$NEW" && $(MAKE) _tag TAG=$$NEW
_tag:
git push origin $(TAG)
+30
View File
@@ -0,0 +1,30 @@
package main
import (
"errors"
"os"
"path/filepath"
"testing"
"github.com/spf13/cobra"
"git.unkin.net/unkin/tomswall/internal/tryapply"
)
func TestMutatingCommandsRefuseWhileTryPending(t *testing.T) {
orig := tryapply.Dir
tryapply.Dir = t.TempDir()
t.Cleanup(func() { tryapply.Dir = orig })
if err := os.WriteFile(filepath.Join(tryapply.Dir, "try-snapshot.json"), []byte("{}"), 0o600); err != nil {
t.Fatal(err)
}
configPath = "../../tomswall.example.yaml"
for _, cmd := range []*cobra.Command{applyCmd(), flushCmd(), purgeCmd()} {
t.Run(cmd.Use, func(t *testing.T) {
if err := cmd.RunE(cmd, nil); !errors.Is(err, tryapply.ErrPending) {
t.Errorf("got %v, want ErrPending", err)
}
})
}
}
+24
View File
@@ -12,6 +12,7 @@ import (
"git.unkin.net/unkin/tomswall/internal/config"
"git.unkin.net/unkin/tomswall/internal/nftables"
"git.unkin.net/unkin/tomswall/internal/shorewall"
"git.unkin.net/unkin/tomswall/internal/tryapply"
)
var configPath string
@@ -33,6 +34,9 @@ Use 'tomswall migrate' to convert a shorewall config to YAML.`,
root.AddCommand(
applyCmd(),
tryCmd(),
confirmCmd(),
revertCmd(),
planCmd(),
validateCmd(),
statusCmd(),
@@ -86,6 +90,12 @@ The firewall is never torn down — existing connections are preserved.`,
return err
}
unlock, err := tryapply.Acquire()
if err != nil {
return err
}
defer unlock()
engine, err := nftables.NewEngine(cfg)
if err != nil {
return fmt.Errorf("initializing nftables: %w", err)
@@ -227,6 +237,14 @@ func purgeCmd() *cobra.Command {
return err
}
if !dryRun {
unlock, err := tryapply.Acquire()
if err != nil {
return err
}
defer unlock()
}
engine, err := nftables.NewEngine(cfg)
if err != nil {
return fmt.Errorf("initializing nftables: %w", err)
@@ -272,6 +290,12 @@ func flushCmd() *cobra.Command {
return err
}
unlock, err := tryapply.Acquire()
if err != nil {
return err
}
defer unlock()
engine, err := nftables.NewEngine(cfg)
if err != nil {
return fmt.Errorf("initializing nftables: %w", err)
+168
View File
@@ -0,0 +1,168 @@
package main
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/spf13/cobra"
"git.unkin.net/unkin/tomswall/internal/config"
"git.unkin.net/unkin/tomswall/internal/nftables"
"git.unkin.net/unkin/tomswall/internal/tryapply"
)
// revertGrace lets the in-process revert win before the systemd fallback fires.
const revertGrace = 30 * time.Second
func tryCmd() *cobra.Command {
var timeout time.Duration
cmd := &cobra.Command{
Use: "try",
Short: "Apply configuration and revert unless confirmed within a timeout",
Long: `Try snapshots the live tomswall table to disk, applies the configuration, then
waits for 'tomswall confirm'. On timeout, interrupt or hangup the snapshot is
restored atomically. A transient systemd timer restores it too if this process
dies. Confirm from a new session to prove new connections still work.`,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := loadConfig()
if err != nil {
return err
}
// Registered before apply so a confirm or hangup cannot be missed.
signal.Ignore(syscall.SIGPIPE)
confirm := make(chan os.Signal, 1)
signal.Notify(confirm, syscall.SIGUSR1)
abort := make(chan os.Signal, 1)
signal.Notify(abort, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
id, err := tryApply(cfg, timeout+revertGrace)
if err != nil || id == "" {
return err
}
fmt.Printf("Applied. Run 'tomswall confirm' within %s or the previous ruleset is restored.\n", timeout)
msg, err := confirmOrRevert(confirm, abort, timeout, func() (bool, error) { return tryapply.Revert(id) })
if err != nil {
return err
}
fmt.Println(msg)
return nil
},
}
cmd.Flags().DurationVar(&timeout, "timeout", 60*time.Second, "time to wait for confirmation before reverting")
return cmd
}
// tryApply applies cfg under a pending try and returns its ID, or "" when
// there was nothing to change.
func tryApply(cfg *config.Config, fallback time.Duration) (string, error) {
unlock, err := tryapply.Acquire()
if err != nil {
return "", err
}
defer unlock()
engine, err := nftables.NewEngine(cfg)
if err != nil {
return "", fmt.Errorf("initializing nftables: %w", err)
}
changes, err := engine.Plan()
if err != nil {
return "", fmt.Errorf("computing changes: %w", err)
}
if changes.Empty() {
fmt.Println("No changes needed — firewall is up to date.")
return "", nil
}
fmt.Println(changes.Summary())
snap, err := engine.Snapshot()
if err != nil {
return "", fmt.Errorf("snapshotting ruleset: %w", err)
}
id, err := tryapply.Arm(snap, os.Getpid(), fallback)
if err != nil {
return "", err
}
if err := engine.Apply(changes); err != nil {
if derr := tryapply.Discard(); derr != nil {
err = fmt.Errorf("%w (discarding snapshot: %v)", err, derr)
}
return "", fmt.Errorf("applying changes: %w", err)
}
return id, nil
}
// confirmOrRevert waits for confirm; on abort or timeout it runs revert.
// A confirm already delivered wins over a simultaneous abort or timeout.
func confirmOrRevert(confirm, abort <-chan os.Signal, timeout time.Duration, revert func() (bool, error)) (string, error) {
reason := "not confirmed within " + timeout.String()
select {
case <-confirm:
return "Confirmed.", nil
case s := <-abort:
reason = "interrupted by " + s.String()
case <-time.After(timeout):
}
select {
case <-confirm:
return "Confirmed.", nil
default:
}
reverted, err := revert()
if err != nil {
return "", fmt.Errorf("%s; revert failed: %w", reason, err)
}
if !reverted {
return "Already resolved by 'tomswall confirm' or 'tomswall revert'.", nil
}
return "", fmt.Errorf("%s: previous ruleset restored", reason)
}
func confirmCmd() *cobra.Command {
return &cobra.Command{
Use: "confirm",
Short: "Keep the configuration applied by a pending 'tomswall try'",
RunE: func(cmd *cobra.Command, args []string) error {
pid, ok, err := tryapply.Confirm()
if err != nil {
return err
}
if !ok {
return fmt.Errorf("no pending 'tomswall try': it was already reverted or confirmed")
}
// A recycled PID must not be signalled.
if comm, err := os.ReadFile(fmt.Sprintf("/proc/%d/comm", pid)); err == nil && strings.TrimSpace(string(comm)) == "tomswall" {
_ = syscall.Kill(pid, syscall.SIGUSR1)
}
fmt.Println("Confirmed.")
return nil
},
}
}
func revertCmd() *cobra.Command {
var id string
cmd := &cobra.Command{
Use: "revert",
Short: "Restore the ruleset saved by a pending 'tomswall try'",
RunE: func(cmd *cobra.Command, args []string) error {
reverted, err := tryapply.Revert(id)
if err != nil {
return err
}
if !reverted {
fmt.Println("No pending 'tomswall try'.")
return nil
}
fmt.Println("Previous ruleset restored.")
return nil
},
}
cmd.Flags().StringVar(&id, "id", "", "only revert the try with this ID (used by the revert timer)")
return cmd
}
+63
View File
@@ -0,0 +1,63 @@
package main
import (
"errors"
"os"
"syscall"
"testing"
"time"
)
func TestConfirmOrRevert(t *testing.T) {
tests := []struct {
name string
confirm bool
abort bool
resolved bool
revertErr error
wantRevert bool
wantErr bool
wantResolved bool
}{
{name: "confirmed keeps ruleset", confirm: true},
{name: "confirm wins over simultaneous abort", confirm: true, abort: true},
{name: "timeout reverts", wantRevert: true, wantErr: true},
{name: "hangup reverts", abort: true, wantRevert: true, wantErr: true},
{name: "revert failure surfaces", revertErr: errors.New("boom"), wantRevert: true, wantErr: true},
{name: "resolved elsewhere", resolved: true, wantRevert: true, wantResolved: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for i := 0; i < 20; i++ { // select between ready channels is random
confirm := make(chan os.Signal, 1)
abort := make(chan os.Signal, 1)
if tt.confirm {
confirm <- syscall.SIGUSR1
}
if tt.abort {
abort <- syscall.SIGHUP
}
called := false
msg, err := confirmOrRevert(confirm, abort, 10*time.Millisecond, func() (bool, error) {
called = true
return !tt.resolved, tt.revertErr
})
if called != tt.wantRevert {
t.Fatalf("revert called = %v, want %v", called, tt.wantRevert)
}
if (err != nil) != tt.wantErr {
t.Fatalf("err = %v, wantErr %v", err, tt.wantErr)
}
if tt.revertErr != nil && !errors.Is(err, tt.revertErr) {
t.Fatalf("revert error not wrapped: %v", err)
}
if tt.wantResolved && msg == "" {
t.Fatal("expected already-resolved message")
}
if !(tt.confirm && tt.abort) {
break
}
}
})
}
}
+1 -1
View File
@@ -4,6 +4,7 @@ go 1.23
require (
github.com/google/nftables v0.2.0
github.com/mdlayher/netlink v1.7.2
github.com/spf13/cobra v1.8.1
golang.org/x/sys v0.18.0
gopkg.in/yaml.v3 v3.0.1
@@ -13,7 +14,6 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/mdlayher/netlink v1.7.2 // indirect
github.com/mdlayher/socket v0.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.23.0 // indirect
+14 -1
View File
@@ -8,6 +8,7 @@ import (
"git.unkin.net/unkin/tomswall/internal/config"
"git.unkin.net/unkin/tomswall/internal/nftables"
"git.unkin.net/unkin/tomswall/internal/tryapply"
)
// Applier applies a translated config to the firewall. Abstracted so the run
@@ -90,6 +91,12 @@ func (a *Agent) applyConfig(ctx context.Context, rc *RenderedConfig, report bool
if err := a.Client.ReportStatus(ctx, rc.Generation); err != nil {
slog.Warn("agent: reporting status failed", "err", err)
}
// Report the FIB so the control plane can scope router enforcement.
if fib := CollectFIB(ctx); len(fib) > 0 {
if err := a.Client.ReportRoutes(ctx, fib); err != nil {
slog.Warn("agent: reporting routes failed", "err", err)
}
}
}
return nil
}
@@ -97,8 +104,14 @@ func (a *Agent) applyConfig(ctx context.Context, rc *RenderedConfig, report bool
// EngineApplier applies via the real nftables differential engine.
type EngineApplier struct{}
// Apply computes and applies the differential change set for cfg.
// Apply computes and applies the differential change set for cfg. It refuses
// while a 'tomswall try' awaits confirmation.
func (EngineApplier) Apply(_ context.Context, cfg *config.Config) error {
unlock, err := tryapply.Acquire()
if err != nil {
return err
}
defer unlock()
engine, err := nftables.NewEngine(cfg)
if err != nil {
return fmt.Errorf("initializing nftables: %w", err)
+35
View File
@@ -84,6 +84,41 @@ func TestTranslateBareZone(t *testing.T) {
}
}
func TestTranslateNATTier(t *testing.T) {
prob := 0.5
rc := &RenderedConfig{
Enforcing: true,
SNAT: []RenderedSNAT{
{Action: "masquerade", Source: []string{"10.1.0.0/24"}, Egress: []string{"eth0", "eth3"}},
{Action: "snat", Source: []string{"10.2.0.0/24"}, Egress: []string{"eth0"}, Address: "203.0.113.1", Probability: &prob},
},
Netmap: []RenderedNetmap{{Type: "dnat", FromNet: "10.0.0.0/24", ToNet: "192.168.1.0/24", Interface: "eth0"}},
NAT: []RenderedNAT{{External: "203.0.113.10", Internal: "10.1.0.10", Interface: "eth0"}},
}
cfg, err := Translate(rc)
if err != nil {
t.Fatalf("Translate: %v", err)
}
// masquerade with two egress interfaces expands to two rules; snat adds one.
if len(cfg.SNAT) != 3 {
t.Fatalf("expected 3 SNAT rules, got %d: %+v", len(cfg.SNAT), cfg.SNAT)
}
if cfg.SNAT[0].Action != config.SNATMasquerade || cfg.SNAT[0].Source != "10.1.0.0/24" || cfg.SNAT[0].Dest != "eth0" {
t.Errorf("unexpected masquerade rule: %+v", cfg.SNAT[0])
}
if cfg.SNAT[2].Address != "203.0.113.1" || cfg.SNAT[2].Probability != 0.5 {
t.Errorf("snat address/probability not carried: %+v", cfg.SNAT[2])
}
if len(cfg.Netmap) != 1 || cfg.Netmap[0].Net1 != "10.0.0.0/24" || cfg.Netmap[0].Net2 != "192.168.1.0/24" || cfg.Netmap[0].Interface != "eth0" {
t.Errorf("netmap not translated: %+v", cfg.Netmap)
}
if len(cfg.StaticNAT) != 1 || cfg.StaticNAT[0].External != "203.0.113.10" || cfg.StaticNAT[0].Internal != "10.1.0.10" {
t.Errorf("static nat not translated: %+v", cfg.StaticNAT)
}
}
func TestTranslateRejectsUnknownAction(t *testing.T) {
rc := &RenderedConfig{Enforcing: true, Rules: []RenderedRule{{Action: "bogus"}}}
if _, err := Translate(rc); err == nil {
+24
View File
@@ -70,6 +70,30 @@ func ParseRendered(body []byte) (*RenderedConfig, error) {
return &cfg, nil
}
// ReportRoutes reports the device's reachable prefixes (FIB) so the control
// plane can scope which routers enforce a rule.
func (c *Client) ReportRoutes(ctx context.Context, prefixes []string) error {
url := fmt.Sprintf("%s/api/v1/devices/%s/routes", c.BaseURL, c.Device)
payload, _ := json.Marshal(map[string][]string{"prefixes": prefixes})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payload))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.Token)
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTP.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<16))
if resp.StatusCode >= 400 {
return fmt.Errorf("report routes: %d", resp.StatusCode)
}
return nil
}
// ReportStatus tells the control plane which generation this device has applied.
func (c *Client) ReportStatus(ctx context.Context, generation int64) error {
url := fmt.Sprintf("%s/api/v1/devices/%s/status", c.BaseURL, c.Device)
+76
View File
@@ -0,0 +1,76 @@
package agent
import (
"context"
"os/exec"
"strings"
)
// CollectFIB returns the device's reachable prefixes from the kernel FIB — which
// includes FRR-installed routes — by shelling out to `ip route`. The control
// plane uses these to scope which routers enforce a rule. On a host without
// iproute2 it returns nil, and the control plane falls back to over-approximation.
func CollectFIB(ctx context.Context) []string {
var prefixes []string
for _, spec := range []struct {
args []string
def string
}{
{[]string{"route", "show"}, "0.0.0.0/0"},
{[]string{"-6", "route", "show"}, "::/0"},
} {
out, err := exec.CommandContext(ctx, "ip", spec.args...).Output()
if err != nil {
continue
}
prefixes = append(prefixes, parseRoutes(string(out), spec.def)...)
}
return dedup(prefixes)
}
// routeTypeKeywords are leading tokens in `ip route` output that precede the
// actual destination (e.g. "unreachable 10.0.0.0/8").
var routeTypeKeywords = map[string]bool{
"unreachable": true, "blackhole": true, "prohibit": true, "throw": true,
"local": true, "broadcast": true, "multicast": true, "anycast": true, "nat": true,
}
// parseRoutes extracts destination prefixes from `ip route show` output.
// defaultPrefix is substituted for a "default" route (family-specific).
func parseRoutes(output, defaultPrefix string) []string {
var out []string
for _, line := range strings.Split(output, "\n") {
fields := strings.Fields(line)
if len(fields) == 0 {
continue
}
dst := fields[0]
// ECMP routes emit continuation "nexthop ..." lines with no destination.
if dst == "nexthop" {
continue
}
if routeTypeKeywords[dst] {
if len(fields) < 2 {
continue
}
dst = fields[1]
}
if dst == "default" {
out = append(out, defaultPrefix)
continue
}
out = append(out, normalizePrefix(dst))
}
return out
}
// normalizePrefix turns a bare host address into a host prefix (/32 or /128).
func normalizePrefix(dst string) string {
if strings.Contains(dst, "/") {
return dst
}
if strings.Contains(dst, ":") {
return dst + "/128"
}
return dst + "/32"
}
+55
View File
@@ -0,0 +1,55 @@
package agent
import (
"reflect"
"sort"
"testing"
)
func TestParseRoutesV4(t *testing.T) {
// Representative `ip route show` output, including a default route, an ECMP
// route with nexthop continuation lines, a connected route, and a host route.
out := `default via 10.0.0.1 dev eth0 proto dhcp
10.1.0.0/24 dev eth1 proto kernel scope link src 10.1.0.5
10.4.0.0/24 proto bgp metric 20
nexthop via 10.0.0.2 dev eth0 weight 1
nexthop via 10.0.0.3 dev eth0 weight 1
192.0.2.7 dev eth2 scope link
blackhole 172.16.0.0/12
`
got := parseRoutes(out, "0.0.0.0/0")
sort.Strings(got)
want := []string{"0.0.0.0/0", "10.1.0.0/24", "10.4.0.0/24", "172.16.0.0/12", "192.0.2.7/32"}
sort.Strings(want)
if !reflect.DeepEqual(got, want) {
t.Errorf("parseRoutes v4 = %v, want %v", got, want)
}
}
func TestParseRoutesV6(t *testing.T) {
out := `default via fe80::1 dev eth0 metric 1024
2001:db8:1::/64 dev eth1 proto kernel metric 256
2001:db8:4::5 dev eth2
`
got := parseRoutes(out, "::/0")
sort.Strings(got)
want := []string{"2001:db8:1::/64", "2001:db8:4::5/128", "::/0"}
sort.Strings(want)
if !reflect.DeepEqual(got, want) {
t.Errorf("parseRoutes v6 = %v, want %v", got, want)
}
}
func TestNormalizePrefix(t *testing.T) {
cases := map[string]string{
"10.1.0.0/24": "10.1.0.0/24",
"10.1.0.5": "10.1.0.5/32",
"2001:db8::1": "2001:db8::1/128",
"2001:db8::/32": "2001:db8::/32",
}
for in, want := range cases {
if got := normalizePrefix(in); got != want {
t.Errorf("normalizePrefix(%q) = %q, want %q", in, got, want)
}
}
}
+260 -10
View File
@@ -8,16 +8,266 @@ package agent
// GET /api/v1/devices/{name}/config. It mirrors the control plane's compiler
// output: interface-agnostic, address-matched rules plus named sets.
type RenderedConfig struct {
Generation int64 `yaml:"generation" json:"generation"`
Device string `yaml:"device" json:"device"`
Class string `yaml:"class" json:"class"`
Enforcing bool `yaml:"enforcing" json:"enforcing"`
Settings RenderedSettings `yaml:"settings" json:"settings"`
Resolver []string `yaml:"resolver,omitempty" json:"resolver,omitempty"`
Bindings map[string][]string `yaml:"bindings,omitempty" json:"bindings,omitempty"` // zone -> interfaces
Sets []RenderedSet `yaml:"sets,omitempty" json:"sets,omitempty"`
Rules []RenderedRule `yaml:"rules,omitempty" json:"rules,omitempty"`
Policies []RenderedPolicy `yaml:"policies,omitempty" json:"policies,omitempty"`
Generation int64 `yaml:"generation" json:"generation"`
Device string `yaml:"device" json:"device"`
Class string `yaml:"class" json:"class"`
Enforcing bool `yaml:"enforcing" json:"enforcing"`
Settings RenderedSettings `yaml:"settings" json:"settings"`
Resolver []string `yaml:"resolver,omitempty" json:"resolver,omitempty"`
Bindings map[string][]string `yaml:"bindings,omitempty" json:"bindings,omitempty"` // zone -> interfaces
Sets []RenderedSet `yaml:"sets,omitempty" json:"sets,omitempty"`
Rules []RenderedRule `yaml:"rules,omitempty" json:"rules,omitempty"`
Policies []RenderedPolicy `yaml:"policies,omitempty" json:"policies,omitempty"`
SNAT []RenderedSNAT `yaml:"snat,omitempty" json:"snat,omitempty"`
Netmap []RenderedNetmap `yaml:"netmap,omitempty" json:"netmap,omitempty"`
NAT []RenderedNAT `yaml:"nat,omitempty" json:"nat,omitempty"`
Hosts []RenderedHost `yaml:"hosts,omitempty" json:"hosts,omitempty"`
Providers []RenderedProvider `yaml:"providers,omitempty" json:"providers,omitempty"`
Routes []RenderedRoute `yaml:"routes,omitempty" json:"routes,omitempty"`
RoutingRules []RenderedRoutingRule `yaml:"routing_rules,omitempty" json:"routing_rules,omitempty"`
Tunnels []RenderedTunnel `yaml:"tunnels,omitempty" json:"tunnels,omitempty"`
StoppedRules []RenderedStoppedRule `yaml:"stopped_rules,omitempty" json:"stopped_rules,omitempty"`
ProxyARP []RenderedProxy `yaml:"proxy_arp,omitempty" json:"proxy_arp,omitempty"`
ProxyNDP []RenderedProxy `yaml:"proxy_ndp,omitempty" json:"proxy_ndp,omitempty"`
ArpRules []RenderedArpRule `yaml:"arp_rules,omitempty" json:"arp_rules,omitempty"`
Maclist []RenderedMaclist `yaml:"maclist,omitempty" json:"maclist,omitempty"`
Mangle []RenderedMangle `yaml:"mangle,omitempty" json:"mangle,omitempty"`
Accounting []RenderedAccounting `yaml:"accounting,omitempty" json:"accounting,omitempty"`
TCDevices []RenderedTCDevice `yaml:"tc_devices,omitempty" json:"tc_devices,omitempty"`
TCClasses []RenderedTCClass `yaml:"tc_classes,omitempty" json:"tc_classes,omitempty"`
TCFilters []RenderedTCFilter `yaml:"tc_filters,omitempty" json:"tc_filters,omitempty"`
TCInterfaces []RenderedTCInterface `yaml:"tc_interfaces,omitempty" json:"tc_interfaces,omitempty"`
TCPriorities []RenderedTCPriority `yaml:"tc_priorities,omitempty" json:"tc_priorities,omitempty"`
Blrules []RenderedBlrule `yaml:"blrules,omitempty" json:"blrules,omitempty"`
Conntrack []RenderedConntrack `yaml:"conntrack,omitempty" json:"conntrack,omitempty"`
Secmarks []RenderedSecmark `yaml:"secmarks,omitempty" json:"secmarks,omitempty"`
Vars map[string]string `yaml:"vars,omitempty" json:"vars,omitempty"`
}
type RenderedBlrule struct {
Priority int `yaml:"priority,omitempty" json:"priority,omitempty"`
Action string `yaml:"action" json:"action"`
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
Log string `yaml:"log,omitempty" json:"log,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedConntrack struct {
Action string `yaml:"action" json:"action"`
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
Chain string `yaml:"chain,omitempty" json:"chain,omitempty"`
Helper string `yaml:"helper,omitempty" json:"helper,omitempty"`
User string `yaml:"user,omitempty" json:"user,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedSecmark struct {
Secmark string `yaml:"secmark" json:"secmark"`
Chain string `yaml:"chain" json:"chain"`
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedMangle struct {
Action string `yaml:"action" json:"action"`
Chain string `yaml:"chain,omitempty" json:"chain,omitempty"`
MarkValue string `yaml:"mark_value,omitempty" json:"mark_value,omitempty"`
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
User string `yaml:"user,omitempty" json:"user,omitempty"`
Mark string `yaml:"mark,omitempty" json:"mark,omitempty"`
Length string `yaml:"length,omitempty" json:"length,omitempty"`
TOS string `yaml:"tos,omitempty" json:"tos,omitempty"`
Helper string `yaml:"helper,omitempty" json:"helper,omitempty"`
Probability *float64 `yaml:"probability,omitempty" json:"probability,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedAccounting struct {
Action string `yaml:"action" json:"action"`
Section string `yaml:"section,omitempty" json:"section,omitempty"`
Chain string `yaml:"chain,omitempty" json:"chain,omitempty"`
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
Mark string `yaml:"mark,omitempty" json:"mark,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedTCDevice struct {
Interface string `yaml:"interface" json:"interface"`
InBandwidth string `yaml:"in_bandwidth,omitempty" json:"in_bandwidth,omitempty"`
OutBandwidth string `yaml:"out_bandwidth,omitempty" json:"out_bandwidth,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedTCClass struct {
Interface string `yaml:"interface" json:"interface"`
Mark int `yaml:"mark,omitempty" json:"mark,omitempty"`
Rate string `yaml:"rate,omitempty" json:"rate,omitempty"`
Ceil string `yaml:"ceil,omitempty" json:"ceil,omitempty"`
Priority int `yaml:"priority,omitempty" json:"priority,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedTCFilter struct {
Class string `yaml:"class" json:"class"`
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
TOS string `yaml:"tos,omitempty" json:"tos,omitempty"`
Length int `yaml:"length,omitempty" json:"length,omitempty"`
Priority int `yaml:"priority,omitempty" json:"priority,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedTCInterface struct {
Interface string `yaml:"interface" json:"interface"`
Type string `yaml:"type,omitempty" json:"type,omitempty"`
InBandwidth string `yaml:"in_bandwidth,omitempty" json:"in_bandwidth,omitempty"`
OutBandwidth string `yaml:"out_bandwidth,omitempty" json:"out_bandwidth,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedTCPriority struct {
Band int `yaml:"band" json:"band"`
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
Address string `yaml:"address,omitempty" json:"address,omitempty"`
Interface string `yaml:"interface,omitempty" json:"interface,omitempty"`
Helper string `yaml:"helper,omitempty" json:"helper,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedTunnel struct {
Type string `yaml:"type" json:"type"`
Zone string `yaml:"zone" json:"zone"`
Gateways []string `yaml:"gateways,omitempty" json:"gateways,omitempty"`
GatewayZones []string `yaml:"gateway_zones,omitempty" json:"gateway_zones,omitempty"`
Port int `yaml:"port,omitempty" json:"port,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedStoppedRule struct {
Action string `yaml:"action" json:"action"`
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedProxy struct {
Address string `yaml:"address" json:"address"`
Interface string `yaml:"interface,omitempty" json:"interface,omitempty"`
External string `yaml:"external" json:"external"`
HaveRoute bool `yaml:"haveroute,omitempty" json:"haveroute,omitempty"`
Persistent bool `yaml:"persistent,omitempty" json:"persistent,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedArpRule struct {
Action string `yaml:"action" json:"action"`
ActionAddress string `yaml:"action_address,omitempty" json:"action_address,omitempty"`
ActionMAC string `yaml:"action_mac,omitempty" json:"action_mac,omitempty"`
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
Opcode int `yaml:"opcode,omitempty" json:"opcode,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedMaclist struct {
Action string `yaml:"action" json:"action"`
Interface string `yaml:"interface" json:"interface"`
MAC string `yaml:"mac,omitempty" json:"mac,omitempty"`
Addresses []string `yaml:"addresses,omitempty" json:"addresses,omitempty"`
Log string `yaml:"log,omitempty" json:"log,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedHost struct {
Zone string `yaml:"zone" json:"zone"`
Interface string `yaml:"interface" json:"interface"`
Addresses []string `yaml:"addresses,omitempty" json:"addresses,omitempty"`
Exclusions []string `yaml:"exclusions,omitempty" json:"exclusions,omitempty"`
Dynamic bool `yaml:"dynamic,omitempty" json:"dynamic,omitempty"`
}
type RenderedProvider struct {
Name string `yaml:"name" json:"name"`
Number int `yaml:"number" json:"number"`
Mark int `yaml:"mark,omitempty" json:"mark,omitempty"`
Duplicate string `yaml:"duplicate,omitempty" json:"duplicate,omitempty"`
Interface string `yaml:"interface" json:"interface"`
Gateway string `yaml:"gateway,omitempty" json:"gateway,omitempty"`
Copy []string `yaml:"copy,omitempty" json:"copy,omitempty"`
}
type RenderedRoute struct {
Provider string `yaml:"provider,omitempty" json:"provider,omitempty"`
Dest string `yaml:"dest" json:"dest"`
Gateway string `yaml:"gateway,omitempty" json:"gateway,omitempty"`
Oif string `yaml:"oif,omitempty" json:"oif,omitempty"`
Persistent bool `yaml:"persistent,omitempty" json:"persistent,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedRoutingRule struct {
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
Provider string `yaml:"provider" json:"provider"`
Priority int `yaml:"priority,omitempty" json:"priority,omitempty"`
Persistent bool `yaml:"persistent,omitempty" json:"persistent,omitempty"`
Mark string `yaml:"mark,omitempty" json:"mark,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
// RenderedSNAT is a resolved SNAT/masquerade rule (egress carries interface names).
type RenderedSNAT struct {
Action string `yaml:"action" json:"action"`
Source []string `yaml:"source,omitempty" json:"source,omitempty"`
Egress []string `yaml:"egress" json:"egress"`
Address string `yaml:"address,omitempty" json:"address,omitempty"`
Probability *float64 `yaml:"probability,omitempty" json:"probability,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
// RenderedNetmap is a resolved network-to-network mapping on one interface.
type RenderedNetmap struct {
Type string `yaml:"type" json:"type"`
FromNet string `yaml:"from_net" json:"from_net"`
ToNet string `yaml:"to_net" json:"to_net"`
Interface string `yaml:"interface,omitempty" json:"interface,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
// RenderedNAT is a resolved one-to-one static NAT on one interface.
type RenderedNAT struct {
External string `yaml:"external" json:"external"`
Internal string `yaml:"internal" json:"internal"`
Interface string `yaml:"interface,omitempty" json:"interface,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}
type RenderedSettings struct {
+183
View File
@@ -60,9 +60,192 @@ func Translate(rc *RenderedConfig) (*config.Config, error) {
})
}
for _, s := range rc.SNAT {
cfg.SNAT = append(cfg.SNAT, translateSNAT(s)...)
}
for _, n := range rc.Netmap {
cfg.Netmap = append(cfg.Netmap, config.Netmap{
Type: config.NetmapType(n.Type),
Net1: n.FromNet,
Net2: n.ToNet,
Interface: n.Interface,
Comment: n.Comment,
})
}
for _, n := range rc.NAT {
cfg.StaticNAT = append(cfg.StaticNAT, config.StaticNAT{
External: n.External,
Internal: n.Internal,
Interface: n.Interface,
Comment: n.Comment,
})
}
for _, h := range rc.Hosts {
cfg.Hosts = append(cfg.Hosts, config.Host{
Zone: h.Zone, Interface: h.Interface, Addresses: h.Addresses,
Exclusions: h.Exclusions, Dynamic: h.Dynamic,
})
}
for _, p := range rc.Providers {
cfg.Providers = append(cfg.Providers, config.Provider{
Name: p.Name, Number: p.Number, Mark: p.Mark, Duplicate: p.Duplicate,
Interface: p.Interface, Gateway: p.Gateway, Copy: p.Copy,
})
}
for _, r := range rc.Routes {
cfg.Routes = append(cfg.Routes, config.StaticRoute{
Provider: r.Provider, Dest: r.Dest, Gateway: r.Gateway,
Device: r.Oif, Persistent: r.Persistent, Comment: r.Comment,
})
}
for _, r := range rc.RoutingRules {
cfg.RoutingRules = append(cfg.RoutingRules, config.RoutingRule{
Source: r.Source, Dest: r.Dest, Provider: r.Provider, Priority: r.Priority,
Persistent: r.Persistent, Mark: r.Mark, Comment: r.Comment,
})
}
for _, t := range rc.Tunnels {
cfg.Tunnels = append(cfg.Tunnels, config.Tunnel{
Type: t.Type, Zone: t.Zone, Gateways: t.Gateways,
GatewayZones: t.GatewayZones, Port: t.Port, Comment: t.Comment,
})
}
for _, r := range rc.StoppedRules {
cfg.StoppedRules = append(cfg.StoppedRules, config.StoppedRule{
Action: config.StoppedAction(r.Action), Source: r.Source, Dest: r.Dest,
Proto: r.Proto, DPort: config.PortSpec(r.DPort), SPort: config.PortSpec(r.SPort), Comment: r.Comment,
})
}
for _, p := range rc.ProxyARP {
cfg.ProxyARP = append(cfg.ProxyARP, config.ProxyARP{
Address: p.Address, Interface: p.Interface, External: p.External,
HaveRoute: p.HaveRoute, Persistent: p.Persistent, Comment: p.Comment,
})
}
for _, p := range rc.ProxyNDP {
cfg.ProxyNDP = append(cfg.ProxyNDP, config.ProxyNDP{
Address: p.Address, Interface: p.Interface, External: p.External,
HaveRoute: p.HaveRoute, Persistent: p.Persistent, Comment: p.Comment,
})
}
for _, a := range rc.ArpRules {
cfg.ArpRules = append(cfg.ArpRules, config.ArpRule{
Action: config.ArpAction(a.Action), ActionAddress: a.ActionAddress, ActionMAC: a.ActionMAC,
Source: a.Source, Dest: a.Dest, Opcode: a.Opcode, Comment: a.Comment,
})
}
for _, m := range rc.Maclist {
cfg.Maclist = append(cfg.Maclist, config.MaclistEntry{
Action: config.MaclistAction(m.Action), Interface: m.Interface, MAC: m.MAC,
Addresses: m.Addresses, Log: m.Log, Comment: m.Comment,
})
}
for _, m := range rc.Mangle {
mr := config.MangleRule{
Action: config.MangleAction(m.Action), Chain: config.MangleChain(m.Chain), MarkValue: m.MarkValue,
Source: m.Source, Dest: m.Dest, Proto: m.Proto, DPort: config.PortSpec(m.DPort), SPort: config.PortSpec(m.SPort),
User: m.User, Mark: m.Mark, Length: m.Length, TOS: m.TOS, Helper: m.Helper, Comment: m.Comment,
}
if m.Probability != nil {
mr.Probability = *m.Probability
}
cfg.Mangle = append(cfg.Mangle, mr)
}
for _, a := range rc.Accounting {
cfg.Accounting = append(cfg.Accounting, config.AccountingRule{
Action: config.AccountingAction(a.Action), Section: config.AccountingSection(a.Section), Chain: a.Chain,
Source: a.Source, Dest: a.Dest, Proto: a.Proto, DPort: config.PortSpec(a.DPort), SPort: config.PortSpec(a.SPort),
Mark: a.Mark, Comment: a.Comment,
})
}
for _, t := range rc.TCDevices {
cfg.TCDevices = append(cfg.TCDevices, config.TCDevice{
Interface: t.Interface, InBandwidth: t.InBandwidth, OutBandwidth: t.OutBandwidth, Comment: t.Comment,
})
}
for _, t := range rc.TCClasses {
cfg.TCClasses = append(cfg.TCClasses, config.TCClass{
Interface: t.Interface, Mark: t.Mark, Rate: t.Rate, Ceil: t.Ceil, Priority: t.Priority, Comment: t.Comment,
})
}
for _, t := range rc.TCFilters {
cfg.TCFilters = append(cfg.TCFilters, config.TCFilter{
Class: t.Class, Source: t.Source, Dest: t.Dest, Proto: t.Proto,
DPort: config.PortSpec(t.DPort), SPort: config.PortSpec(t.SPort),
TOS: t.TOS, Length: t.Length, Priority: t.Priority, Comment: t.Comment,
})
}
for _, t := range rc.TCInterfaces {
cfg.TCInterfaces = append(cfg.TCInterfaces, config.TCInterface{
Interface: t.Interface, Type: t.Type, InBandwidth: t.InBandwidth, OutBandwidth: t.OutBandwidth, Comment: t.Comment,
})
}
for _, t := range rc.TCPriorities {
cfg.TCPriorities = append(cfg.TCPriorities, config.TCPriority{
Band: t.Band, Proto: t.Proto, DPort: config.PortSpec(t.DPort), SPort: config.PortSpec(t.SPort),
Address: t.Address, Interface: t.Interface, Helper: t.Helper, Comment: t.Comment,
})
}
for _, b := range rc.Blrules {
cfg.Blrules = append(cfg.Blrules, config.BlruleRule{
Action: config.BlruleAction(b.Action), Source: b.Source, Dest: b.Dest, Proto: b.Proto,
DPort: config.PortSpec(b.DPort), SPort: config.PortSpec(b.SPort), Log: b.Log, Comment: b.Comment,
})
}
for _, c := range rc.Conntrack {
cfg.Conntrack = append(cfg.Conntrack, config.ConntrackRule{
Action: config.ConntrackAction(c.Action), Source: c.Source, Dest: c.Dest, Proto: c.Proto,
DPort: config.PortSpec(c.DPort), SPort: config.PortSpec(c.SPort),
Chain: config.ConntrackChain(c.Chain), Helper: c.Helper, User: c.User, Comment: c.Comment,
})
}
for _, sm := range rc.Secmarks {
cfg.Secmarks = append(cfg.Secmarks, config.SecmarkRule{
Secmark: sm.Secmark, Chain: sm.Chain, Source: sm.Source, Dest: sm.Dest, Proto: sm.Proto,
DPort: config.PortSpec(sm.DPort), SPort: config.PortSpec(sm.SPort), Comment: sm.Comment,
})
}
if len(rc.Vars) > 0 {
cfg.Vars = make(map[string]string, len(rc.Vars))
for k, v := range rc.Vars {
cfg.Vars[k] = v
}
}
return cfg, nil
}
// translateSNAT expands a rendered SNAT (which carries a list of egress
// interfaces and source CIDRs) into native tomswall SNAT rules — one per
// (egress interface, source) pair, since a native rule takes a single Dest.
func translateSNAT(s RenderedSNAT) []config.SNATRule {
sources := s.Source
if len(sources) == 0 {
sources = []string{""}
}
var out []config.SNATRule
for _, egress := range s.Egress {
for _, src := range sources {
r := config.SNATRule{
Action: config.SNATAction(s.Action),
Source: src,
Dest: egress,
Address: s.Address,
Comment: s.Comment,
}
if s.Probability != nil {
r.Probability = *s.Probability
}
out = append(out, r)
}
}
return out
}
// indexSets maps set name -> concrete member CIDRs (invalid members skipped).
func indexSets(sets []RenderedSet) map[string][]string {
m := make(map[string][]string, len(sets))
+14 -6
View File
@@ -57,17 +57,25 @@ func (c *Config) validateBlrules() error {
if r.Source != "all" && r.Source != "any" && r.Source != "none" &&
!hasPrefix(r.Source, "all!") && !hasPrefix(r.Source, "any!") {
srcZone := zoneFromSpec(r.Source)
if _, ok := c.Zones[srcZone]; !ok {
return fmt.Errorf("blrules[%d]: source zone %q not defined", i, srcZone)
for _, zs := range SplitZoneList(r.Source) {
if _, ok := c.Zones[zs.Zone]; !ok {
return fmt.Errorf("blrules[%d]: source zone %q not defined", i, zs.Zone)
}
if !validAddrList(zs.Addr) {
return fmt.Errorf("blrules[%d]: source %q: '!' may only prefix the whole address list", i, zs.Addr)
}
}
}
if r.Dest != "all" && r.Dest != "any" && r.Dest != "none" &&
!hasPrefix(r.Dest, "all!") && !hasPrefix(r.Dest, "any!") {
dstZone := zoneFromSpec(r.Dest)
if _, ok := c.Zones[dstZone]; !ok {
return fmt.Errorf("blrules[%d]: dest zone %q not defined", i, dstZone)
for _, zs := range SplitZoneList(r.Dest) {
if _, ok := c.Zones[zs.Zone]; !ok {
return fmt.Errorf("blrules[%d]: dest zone %q not defined", i, zs.Zone)
}
if !validAddrList(zs.Addr) {
return fmt.Errorf("blrules[%d]: dest %q: '!' may only prefix the whole address list", i, zs.Addr)
}
}
}
}
+46
View File
@@ -3,6 +3,7 @@ package config
import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
@@ -854,6 +855,32 @@ func TestValidateRules(t *testing.T) {
},
wantErr: "source zone \"missing\" not defined",
},
{
name: "comma zone lists are valid",
rules: []Rule{
{Action: RuleAccept, Source: "fw,loc", Dest: "loc,net:192.0.2.1,198.51.100.1"},
},
},
{
name: "undefined zone in dest list",
rules: []Rule{
{Action: RuleAccept, Source: "loc", Dest: "net,missing"},
},
wantErr: "dest zone \"missing\" not defined",
},
{
name: "negation prefixing the whole address list is valid",
rules: []Rule{
{Action: RuleAccept, Source: "net:!192.0.2.1,198.51.100.1", Dest: "fw"},
},
},
{
name: "negation inside an address list",
rules: []Rule{
{Action: RuleAccept, Source: "net", Dest: "loc:192.0.2.1,!198.51.100.1"},
},
wantErr: "'!' may only prefix the whole address list",
},
{
name: "all keyword is valid source",
rules: []Rule{
@@ -1006,3 +1033,22 @@ func TestValidateSNAT(t *testing.T) {
})
}
}
func TestSplitZoneList(t *testing.T) {
tests := []struct {
in string
want []ZoneSpec
}{
{"net", []ZoneSpec{{Zone: "net"}}},
{"fw,lan,svr", []ZoneSpec{{Zone: "fw"}, {Zone: "lan"}, {Zone: "svr"}}},
{"svr:192.0.2.17", []ZoneSpec{{Zone: "svr", Addr: "192.0.2.17"}}},
{"net:192.0.2.1,198.51.100.1", []ZoneSpec{{Zone: "net", Addr: "192.0.2.1,198.51.100.1"}}},
{"lan,svr:192.0.2.17", []ZoneSpec{{Zone: "lan"}, {Zone: "svr", Addr: "192.0.2.17"}}},
{"net:2001:db8::1", []ZoneSpec{{Zone: "net", Addr: "2001:db8::1"}}},
}
for _, tt := range tests {
if got := SplitZoneList(tt.in); !reflect.DeepEqual(got, tt.want) {
t.Errorf("SplitZoneList(%q) = %+v, want %+v", tt.in, got, tt.want)
}
}
}
+40 -9
View File
@@ -1,6 +1,9 @@
package config
import "fmt"
import (
"fmt"
"strings"
)
type RuleAction string
@@ -172,10 +175,12 @@ func (c *Config) validateRules() error {
if r.Source != "all" && r.Source != "any" && r.Source != "none" &&
!hasPrefix(r.Source, "all+") && !hasPrefix(r.Source, "all!") && !hasPrefix(r.Source, "any!") {
for _, srcPart := range splitZones(r.Source) {
srcZone := zoneFromSpec(srcPart)
if _, ok := c.Zones[srcZone]; !ok {
return fmt.Errorf("rule[%d]: source zone %q not defined", i, srcZone)
for _, zs := range SplitZoneList(r.Source) {
if _, ok := c.Zones[zs.Zone]; !ok {
return fmt.Errorf("rule[%d]: source zone %q not defined", i, zs.Zone)
}
if !validAddrList(zs.Addr) {
return fmt.Errorf("rule[%d]: source %q: '!' may only prefix the whole address list", i, zs.Addr)
}
}
}
@@ -183,10 +188,12 @@ func (c *Config) validateRules() error {
if r.Action != RuleDNAT && r.Action != RuleRedirect && r.Action != RuleNoNAT {
if r.Dest != "all" && r.Dest != "any" && r.Dest != "none" &&
!hasPrefix(r.Dest, "all+") && !hasPrefix(r.Dest, "all!") && !hasPrefix(r.Dest, "any!") {
for _, dstPart := range splitZones(r.Dest) {
dstZone := zoneFromSpec(dstPart)
if _, ok := c.Zones[dstZone]; !ok {
return fmt.Errorf("rule[%d]: dest zone %q not defined", i, dstZone)
for _, zs := range SplitZoneList(r.Dest) {
if _, ok := c.Zones[zs.Zone]; !ok {
return fmt.Errorf("rule[%d]: dest zone %q not defined", i, zs.Zone)
}
if !validAddrList(zs.Addr) {
return fmt.Errorf("rule[%d]: dest %q: '!' may only prefix the whole address list", i, zs.Addr)
}
}
}
@@ -223,6 +230,30 @@ func (c *Config) validateRules() error {
return nil
}
// ZoneSpec is one zone of a SOURCE/DEST list; Addr is its comma-separated address list, if any.
type ZoneSpec struct{ Zone, Addr string }
// SplitZoneList parses "lan,svr:a,b": commas before the first colon separate zones,
// commas after it separate addresses of the last zone (shorewall semantics).
func SplitZoneList(spec string) []ZoneSpec {
zones, addr, _ := strings.Cut(spec, ":")
var out []ZoneSpec
for _, z := range strings.Split(zones, ",") {
if z = strings.TrimSpace(z); z != "" {
out = append(out, ZoneSpec{Zone: z})
}
}
if len(out) > 0 {
out[len(out)-1].Addr = addr
}
return out
}
// validAddrList reports whether '!' appears only at the start, negating the whole list.
func validAddrList(addr string) bool {
return !strings.Contains(strings.TrimPrefix(addr, "!"), "!")
}
// zoneFromSpec extracts the zone name from a zone spec like "net" or "net:192.168.1.0/24".
func zoneFromSpec(spec string) string {
for i, c := range spec {
+412 -240
View File
@@ -3,7 +3,9 @@ package nftables
import (
"encoding/binary"
"fmt"
"log/slog"
"net"
"sort"
"strconv"
"strings"
@@ -14,7 +16,8 @@ import (
)
type Compiler struct {
cfg *config.Config
cfg *config.Config
warned map[string]bool
}
func NewCompiler(cfg *config.Config) *Compiler {
@@ -25,6 +28,7 @@ func (c *Compiler) Compile() (*FirewallState, error) {
state := &FirewallState{
Rules: make(map[string][]ManagedRule),
}
c.warned = nil
c.compileLoopback(state)
if err := c.compileConntrackFastPath(state); err != nil {
@@ -201,7 +205,7 @@ func (c *Compiler) compileBlrules(state *FirewallState) error {
}
if err := c.compileOneRule(state, tag, rule.Source, rule.Dest,
rule.Proto, rule.DPort, rule.SPort,
action, rule.Log, "", fwZone, ""); err != nil {
action, rule.Log, "", "", fwZone, ""); err != nil {
return fmt.Errorf("blrule[%d]: %w", i, err)
}
}
@@ -220,34 +224,30 @@ func (c *Compiler) compileConntrack(state *FirewallState) error {
chains = []string{"prerouting", "output"}
}
matches, err := l4Matches(ct.Proto, ct.DPort, nil)
if err != nil {
return fmt.Errorf("conntrack[%d]: %w", i, err)
}
for _, chain := range chains {
var exprs []expr.Any
for _, m := range matches {
exprs := append([]expr.Any{}, m.exprs...)
if ct.Proto != "" {
exprs = append(exprs, matchProto(ct.Proto)...)
}
for _, p := range ct.DPort {
pe, err := parsePortOrRange(p)
if err != nil {
return fmt.Errorf("conntrack[%d]: %w", i, err)
switch ct.Action {
case config.ConntrackNoTrack:
exprs = append(exprs, &expr.Notrack{})
case config.ConntrackHelper:
continue
case config.ConntrackDrop:
exprs = append(exprs, &expr.Verdict{Kind: expr.VerdictDrop})
}
exprs = append(exprs, pe...)
}
switch ct.Action {
case config.ConntrackNoTrack:
exprs = append(exprs, &expr.Notrack{})
case config.ConntrackHelper:
continue
case config.ConntrackDrop:
exprs = append(exprs, &expr.Verdict{Kind: expr.VerdictDrop})
state.Rules[chain] = append(state.Rules[chain], ManagedRule{
Chain: chain,
Exprs: exprs,
Tag: tag + ":" + chain,
})
}
state.Rules[chain] = append(state.Rules[chain], ManagedRule{
Chain: chain,
Exprs: exprs,
Tag: tag + ":" + chain,
})
}
}
return nil
@@ -271,9 +271,19 @@ func (c *Compiler) compileRules(state *FirewallState) error {
sport = rule.SPort
}
if rule.RateLimit != "" || rule.ConnLimit != "" {
matches, err := l4Matches(proto, dports, sport)
if err != nil {
return fmt.Errorf("rule[%d]: %w", i, err)
}
if len(matches)*specCount(rule.Source, rule.Dest, rule.OrigDest, rule.Action) > 1 {
return fmt.Errorf("rule[%d]: ratelimit/connlimit cannot be combined with proto, port, zone or address lists (each expanded rule would get its own limiter)", i)
}
}
if err := c.compileOneRule(state, tag, rule.Source, rule.Dest,
proto, dports, sport,
rule.Action, rule.Log, rule.Dest, fwZone, rule.Section); err != nil {
rule.Action, rule.Log, rule.Dest, rule.OrigDest, fwZone, rule.Section); err != nil {
return fmt.Errorf("rule[%d]: %w", i, err)
}
@@ -289,11 +299,14 @@ func (c *Compiler) compileRules(state *FirewallState) error {
}
func (c *Compiler) applyRuleExtras(state *FirewallState, tag string, rule config.Rule) {
fwZone := c.cfg.FirewallZone()
srcZone, _ := splitZoneSpec(rule.Source)
dstZone, _ := splitZoneSpec(rule.Dest)
chain := c.selectChain(srcZone, dstZone, fwZone)
for chain := range state.Rules {
if chain != "prerouting" {
c.applyChainExtras(state, chain, tag, rule)
}
}
}
func (c *Compiler) applyChainExtras(state *FirewallState, chain, tag string, rule config.Rule) {
rules := state.Rules[chain]
for idx := len(rules) - 1; idx >= 0; idx-- {
if rules[idx].Tag != tag {
@@ -322,7 +335,7 @@ func (c *Compiler) applyRuleExtras(state *FirewallState, tag string, rule config
extra = append(extra, setMarkExprs(rule.SetMark)...)
}
if rule.Action == config.RuleNFQueue {
replaceVerdict = []expr.Any{&expr.Queue{Num: uint16(rule.NFQueue)}}
replaceVerdict = []expr.Any{&expr.Queue{Num: uint16(rule.NFQueue), Total: 1}}
}
if len(extra) > 0 || len(replaceVerdict) > 0 {
@@ -347,53 +360,119 @@ func (c *Compiler) applyRuleExtras(state *FirewallState, tag string, rule config
func (c *Compiler) compileOneRule(state *FirewallState, tag, srcSpec, dstSpec, proto string,
dports, sports config.PortSpec, action config.RuleAction, logLevel string,
dnatDest string, fwZone string, section config.RuleSection) error {
dnatDest, origDest string, fwZone string, section config.RuleSection) error {
srcZone, srcAddr := splitZoneSpec(srcSpec)
dstZone, dstAddr := splitZoneSpec(dstSpec)
if action == config.RuleDNAT || action == config.RuleRedirect {
return c.compileDNATRule(state, tag, srcSpec, dstSpec, proto, dports, sports, action, logLevel, fwZone)
}
srcIfaces := c.resolveZoneInterfaces(srcZone)
dstIfaces := c.resolveZoneInterfaces(dstZone)
chain := c.selectChain(srcZone, dstZone, fwZone)
for _, srcIface := range srcIfaces {
for _, dstIface := range dstIfaces {
exprs, err := c.buildMatchExprs(srcIface, dstIface, chain, proto, dports, sports, srcAddr, dstAddr)
if err != nil {
return err
for _, src := range zoneSpecs(srcSpec) {
for _, srcAddr := range splitAddrs(src.Addr) {
for _, od := range splitAddrs(origDest) {
if action == config.RuleDNAT || action == config.RuleRedirect {
if err := c.compileDNATRule(state, tag, src.Zone, srcAddr, od, dstSpec, proto, dports, action, logLevel); err != nil {
return err
}
continue
}
for _, dst := range zoneSpecs(dstSpec) {
for _, dstAddr := range splitAddrs(dst.Addr) {
if err := c.compileZonePair(state, tag, src.Zone, srcAddr, dst.Zone, dstAddr, od, proto,
dports, sports, action, logLevel, fwZone, section); err != nil {
return err
}
}
}
}
if section != "" && section != config.SectionAll {
exprs = append(exprs, matchSection(section)...)
}
if logLevel != "" {
exprs = append(exprs, buildLog(logLevel, tag)...)
}
verdict := actionVerdict(action, proto, c.cfg.Settings.AddressFamily)
if verdict != nil {
exprs = append(exprs, verdict...)
}
state.Rules[chain] = append(state.Rules[chain], ManagedRule{
Chain: chain,
Exprs: exprs,
Tag: tag,
})
}
}
return nil
}
func (c *Compiler) compileDNATRule(state *FirewallState, tag, srcSpec, dstSpec, proto string,
dports, sports config.PortSpec, action config.RuleAction, logLevel, fwZone string) error {
// specCount is how many zone/address combinations compileOneRule expands src and dst into.
func specCount(srcSpec, dstSpec, origDest string, action config.RuleAction) int {
count := func(spec string) (n int) {
for _, z := range zoneSpecs(spec) {
n += len(splitAddrs(z.Addr))
}
return n
}
n := count(srcSpec) * len(splitAddrs(origDest))
if action == config.RuleDNAT || action == config.RuleRedirect {
return n
}
return n * count(dstSpec)
}
srcZone, srcAddr := splitZoneSpec(srcSpec)
// zoneSpecs expands a comma zone list; "all"/"any" forms keep their own comma (exclusion) syntax.
func zoneSpecs(spec string) []config.ZoneSpec {
zone, addr := splitZoneSpec(spec)
if base, _, _ := strings.Cut(strings.TrimSuffix(zone, "+"), "!"); base == "all" || base == "any" {
return []config.ZoneSpec{{Zone: zone, Addr: addr}}
}
return config.SplitZoneList(spec)
}
// splitAddrs yields one alternative per listed address; a negated list stays one AND-ed match.
func splitAddrs(addr string) []string {
if addr == "" || strings.HasPrefix(addr, "!") {
return []string{addr}
}
return strings.Split(addr, ",")
}
func (c *Compiler) compileZonePair(state *FirewallState, tag, srcZone, srcAddr, dstZone, dstAddr, origDest, proto string,
dports, sports config.PortSpec, action config.RuleAction, logLevel string,
fwZone string, section config.RuleSection) error {
srcIfaces := c.resolveZoneInterfaces(srcZone, srcAddr)
dstIfaces := c.resolveZoneInterfaces(dstZone, dstAddr)
chain := c.selectChain(srcZone, dstZone, fwZone)
// ponytail: forward daddr is post-DNAT; lift with `ct original daddr` (expr.Ct Direction, google/nftables v0.3.0).
if origDest != "" && chain == "forward" {
return fmt.Errorf("origdest: ORIGDEST on forwarded rules is not supported yet")
}
for _, srcIface := range srcIfaces {
for _, dstIface := range dstIfaces {
matches, err := c.buildMatchExprs(srcIface, dstIface, chain, proto, dports, sports, srcAddr, dstAddr)
if err != nil {
return err
}
if origDest != "" {
od, err := matchOrigDest(origDest)
if err != nil {
return fmt.Errorf("origdest: %w", err)
}
for i := range matches {
matches[i].exprs = append(matches[i].exprs, od...)
}
}
for _, m := range matches {
exprs := m.exprs
if section != "" && section != config.SectionAll {
exprs = append(exprs, matchSection(section)...)
}
if logLevel != "" {
exprs = append(exprs, buildLog(logLevel, tag)...)
}
verdict := actionVerdict(action, m.proto, c.cfg.Settings.AddressFamily)
if verdict != nil {
exprs = append(exprs, verdict...)
}
state.Rules[chain] = append(state.Rules[chain], ManagedRule{
Chain: chain,
Exprs: exprs,
Tag: tag,
})
}
}
}
return nil
}
func (c *Compiler) compileDNATRule(state *FirewallState, tag, srcZone, srcAddr, origDest, dstSpec, proto string,
dports config.PortSpec, action config.RuleAction, logLevel string) error {
chain := "prerouting"
parts := strings.SplitN(dstSpec, ":", 3)
@@ -411,104 +490,110 @@ func (c *Compiler) compileDNATRule(state *FirewallState, tag, srcSpec, dstSpec,
dnatPort = uint16(p)
}
srcIfaces := c.resolveZoneInterfaces(srcZone)
srcIfaces := c.resolveZoneInterfaces(srcZone, srcAddr)
var odExprs []expr.Any
if origDest != "" {
var err error
if odExprs, err = matchOrigDest(origDest); err != nil {
return fmt.Errorf("origdest: %w", err)
}
}
matches, err := l4Matches(proto, dports, nil)
if err != nil {
return err
}
ip := net.ParseIP(dnatAddr)
if ip == nil {
return fmt.Errorf("invalid DNAT address %q", dnatAddr)
}
for _, srcIface := range srcIfaces {
var exprs []expr.Any
for _, m := range matches {
var exprs []expr.Any
if srcIface != "" {
exprs = append(exprs, matchIfaceName(true, srcIface)...)
}
if srcAddr != "" {
src, err := matchSourceCIDR(srcAddr)
if err != nil {
return err
if srcIface != "" {
exprs = append(exprs, matchIfaceName(true, srcIface)...)
}
exprs = append(exprs, src...)
}
if proto != "" {
exprs = append(exprs, matchProto(proto)...)
}
for _, portStr := range dports {
pe, err := parsePortOrRange(portStr)
if err != nil {
return err
}
exprs = append(exprs, pe...)
}
if logLevel != "" {
exprs = append(exprs, buildLog(logLevel, tag)...)
}
ip := net.ParseIP(dnatAddr)
if ip == nil {
return fmt.Errorf("invalid DNAT address %q", dnatAddr)
}
if action == config.RuleRedirect {
if dnatPort > 0 {
portBytes := make([]byte, 2)
binary.BigEndian.PutUint16(portBytes, dnatPort)
exprs = append(exprs,
&expr.Immediate{Register: 1, Data: portBytes},
&expr.Redir{RegisterProtoMin: 1},
)
} else {
exprs = append(exprs, &expr.Redir{})
}
} else {
if ip4 := ip.To4(); ip4 != nil {
exprs = append(exprs,
&expr.Immediate{Register: 1, Data: ip4},
)
natExpr := &expr.NAT{
Type: expr.NATTypeDestNAT,
Family: unix.NFPROTO_IPV4,
RegAddrMin: 1,
RegAddrMax: 1,
if srcAddr != "" {
src, err := matchSourceCIDR(srcAddr)
if err != nil {
return err
}
exprs = append(exprs, src...)
}
exprs = append(exprs, odExprs...)
exprs = append(exprs, m.exprs...)
if logLevel != "" {
exprs = append(exprs, buildLog(logLevel, tag)...)
}
if action == config.RuleRedirect {
if dnatPort > 0 {
portBytes := make([]byte, 2)
binary.BigEndian.PutUint16(portBytes, dnatPort)
exprs = append(exprs,
&expr.Immediate{Register: 2, Data: portBytes},
&expr.Immediate{Register: 1, Data: portBytes},
&expr.Redir{RegisterProtoMin: 1, RegisterProtoMax: 1, Flags: unix.NF_NAT_RANGE_PROTO_SPECIFIED},
)
natExpr.RegProtoMin = 2
natExpr.RegProtoMax = 2
} else {
exprs = append(exprs, &expr.Redir{})
}
exprs = append(exprs, natExpr)
} else {
exprs = append(exprs,
&expr.Immediate{Register: 1, Data: ip.To16()},
)
natExpr := &expr.NAT{
Type: expr.NATTypeDestNAT,
Family: unix.NFPROTO_IPV6,
RegAddrMin: 1,
RegAddrMax: 1,
}
if dnatPort > 0 {
portBytes := make([]byte, 2)
binary.BigEndian.PutUint16(portBytes, dnatPort)
if ip4 := ip.To4(); ip4 != nil {
exprs = append(exprs,
&expr.Immediate{Register: 2, Data: portBytes},
&expr.Immediate{Register: 1, Data: ip4},
)
natExpr.RegProtoMin = 2
natExpr.RegProtoMax = 2
natExpr := &expr.NAT{
Type: expr.NATTypeDestNAT,
Family: unix.NFPROTO_IPV4,
RegAddrMin: 1,
RegAddrMax: 1,
}
if dnatPort > 0 {
portBytes := make([]byte, 2)
binary.BigEndian.PutUint16(portBytes, dnatPort)
exprs = append(exprs,
&expr.Immediate{Register: 2, Data: portBytes},
)
natExpr.RegProtoMin = 2
natExpr.RegProtoMax = 2
}
exprs = append(exprs, natExpr)
} else {
exprs = append(exprs,
&expr.Immediate{Register: 1, Data: ip.To16()},
)
natExpr := &expr.NAT{
Type: expr.NATTypeDestNAT,
Family: unix.NFPROTO_IPV6,
RegAddrMin: 1,
RegAddrMax: 1,
}
if dnatPort > 0 {
portBytes := make([]byte, 2)
binary.BigEndian.PutUint16(portBytes, dnatPort)
exprs = append(exprs,
&expr.Immediate{Register: 2, Data: portBytes},
)
natExpr.RegProtoMin = 2
natExpr.RegProtoMax = 2
}
exprs = append(exprs, natExpr)
}
exprs = append(exprs, natExpr)
}
}
state.Rules[chain] = append(state.Rules[chain], ManagedRule{
Chain: chain,
Exprs: exprs,
Tag: tag,
})
state.Rules[chain] = append(state.Rules[chain], ManagedRule{
Chain: chain,
Exprs: exprs,
Tag: tag,
})
}
}
return nil
}
@@ -529,8 +614,8 @@ func (c *Compiler) compilePolicies(state *FirewallState) error {
}
chain := c.selectChain(sz, dz, fwZone)
srcIfaces := c.resolveZoneInterfaces(sz)
dstIfaces := c.resolveZoneInterfaces(dz)
srcIfaces := c.resolveZoneInterfaces(sz, "")
dstIfaces := c.resolveZoneInterfaces(dz, "")
for _, si := range srcIfaces {
for _, di := range dstIfaces {
@@ -539,7 +624,7 @@ func (c *Compiler) compilePolicies(state *FirewallState) error {
if si != "" {
exprs = append(exprs, matchIfaceName(true, si)...)
}
if di != "" && chain == "forward" {
if di != "" && chain != "input" {
exprs = append(exprs, matchIfaceName(false, di)...)
}
@@ -588,25 +673,12 @@ func (c *Compiler) compileSNAT(state *FirewallState) error {
exprs = append(exprs, srcExprs...)
}
if snat.Proto != "" {
exprs = append(exprs, matchProto(snat.Proto)...)
}
for _, portStr := range snat.DPort {
pe, err := parsePortOrRange(portStr)
if err != nil {
return fmt.Errorf("snat[%d] dport: %w", i, err)
}
exprs = append(exprs, pe...)
}
for _, portStr := range snat.SPort {
pe, err := parseSPortOrRange(portStr)
if err != nil {
return fmt.Errorf("snat[%d] sport: %w", i, err)
}
exprs = append(exprs, pe...)
matches, err := l4Matches(snat.Proto, snat.DPort, snat.SPort)
if err != nil {
return fmt.Errorf("snat[%d]: %w", i, err)
}
head := exprs
exprs = nil
if snat.Mark != "" {
exprs = append(exprs, matchMark(snat.Mark)...)
@@ -652,11 +724,13 @@ func (c *Compiler) compileSNAT(state *FirewallState) error {
}
}
state.Rules["postrouting"] = append(state.Rules["postrouting"], ManagedRule{
Chain: "postrouting",
Exprs: exprs,
Tag: tag,
})
for _, m := range matches {
state.Rules["postrouting"] = append(state.Rules["postrouting"], ManagedRule{
Chain: "postrouting",
Exprs: append(append(append([]expr.Any{}, head...), m.exprs...), exprs...),
Tag: tag,
})
}
}
return nil
@@ -790,7 +864,7 @@ func (c *Compiler) compileMSSClamp(state *FirewallState) {
Type: 2,
Offset: 2,
Len: 2,
Op: 0,
Op: expr.ExthdrOpTcpopt,
},
&expr.Cmp{Op: expr.CmpOpGt, Register: 1, Data: mssBytes},
&expr.Immediate{Register: 1, Data: mssBytes},
@@ -799,7 +873,7 @@ func (c *Compiler) compileMSSClamp(state *FirewallState) {
Type: 2,
Offset: 2,
Len: 2,
Op: 1,
Op: expr.ExthdrOpTcpopt,
},
)
state.Rules["forward"] = append(state.Rules["forward"], ManagedRule{
@@ -883,15 +957,25 @@ func (c *Compiler) selectChain(srcZone, dstZone, fwZone string) string {
return "forward"
}
func (c *Compiler) resolveZoneInterfaces(zone string) []string {
if zone == "all" || zone == "" {
// resolveZoneInterfaces returns nil (fail closed) for a zone with no interfaces unless a non-negated address match narrows the rule.
func (c *Compiler) resolveZoneInterfaces(zone, addr string) []string {
if z, ok := c.cfg.Zones[zone]; !ok || z.Type == config.ZoneFirewall {
return []string{""}
}
ifaces := c.cfg.ZoneInterfaces(zone)
if len(ifaces) == 0 {
if ifaces := c.cfg.ZoneInterfaces(zone); len(ifaces) > 0 {
return ifaces
}
if addr != "" && !strings.HasPrefix(addr, "!") {
return []string{""}
}
return ifaces
if !c.warned[zone] {
if c.warned == nil {
c.warned = map[string]bool{}
}
c.warned[zone] = true
slog.Warn("compiler: zone has no interfaces, skipping its rules", "zone", zone)
}
return nil
}
func (c *Compiler) expandZoneRef(ref string) []string {
@@ -917,18 +1001,19 @@ func (c *Compiler) expandZoneRef(ref string) []string {
}
zones = append(zones, name)
}
sort.Strings(zones)
return zones
}
return []string{base}
}
func (c *Compiler) buildMatchExprs(srcIface, dstIface, chain, proto string, dports, sports config.PortSpec, srcAddr, dstAddr string) ([]expr.Any, error) {
func (c *Compiler) buildMatchExprs(srcIface, dstIface, chain, proto string, dports, sports config.PortSpec, srcAddr, dstAddr string) ([]l4Match, error) {
var exprs []expr.Any
if srcIface != "" {
exprs = append(exprs, matchIfaceName(true, srcIface)...)
}
if dstIface != "" && chain == "forward" {
if dstIface != "" && chain != "input" {
exprs = append(exprs, matchIfaceName(false, dstIface)...)
}
@@ -948,34 +1033,92 @@ func (c *Compiler) buildMatchExprs(srcIface, dstIface, chain, proto string, dpor
exprs = append(exprs, dst...)
}
matches, err := l4Matches(proto, dports, sports)
if err != nil {
return nil, err
}
for i := range matches {
matches[i].exprs = append(append([]expr.Any{}, exprs...), matches[i].exprs...)
}
return matches, nil
}
type l4Match struct {
proto byte
exprs []expr.Any
}
// l4Matches yields one alternative per (proto, dport, sport): nft ANDs a rule's exprs, so lists need one rule each.
func l4Matches(proto string, dports, sports config.PortSpec) ([]l4Match, error) {
protos := []string{""}
if proto != "" {
exprs = append(exprs, matchProto(proto)...)
protos = strings.Split(proto, ",")
}
isICMP := strings.EqualFold(proto, "icmp") || strings.EqualFold(proto, "icmpv6") || strings.EqualFold(proto, "ipv6-icmp")
for _, portStr := range dports {
if isICMP {
pe := matchICMPType(portStr)
exprs = append(exprs, pe...)
} else {
pe, err := parsePortOrRange(portStr)
var out []l4Match
for _, p := range protos {
p = strings.TrimSpace(p)
if proto != "" && p == "" {
return nil, fmt.Errorf("empty element in proto list %q", proto)
}
var pm []expr.Any
var n byte
isICMP := false
if p != "" {
var err error
n, err = protoNumber(p)
if err != nil {
return nil, err
}
exprs = append(exprs, pe...)
isICMP = n == unix.IPPROTO_ICMP || n == unix.IPPROTO_ICMPV6
if (len(dports) > 0 && !isICMP || len(sports) > 0) && !hasPorts(n) {
return nil, fmt.Errorf("protocol %q does not support ports", p)
}
pm = matchProtoNum(n)
}
}
for _, portStr := range sports {
pe, err := parseSPortOrRange(portStr)
parseD := parsePortOrRange
if isICMP {
if len(protos) > 1 && len(dports) > 0 {
return nil, fmt.Errorf("dport %v is ambiguous with %s in proto list %q", dports, p, proto)
}
parseD = matchICMPType
}
dalts, err := portAlternatives(dports, parseD)
if err != nil {
return nil, err
}
exprs = append(exprs, pe...)
salts, err := portAlternatives(sports, parseSPortOrRange)
if err != nil {
return nil, err
}
for _, d := range dalts {
for _, sp := range salts {
e := append(append(append([]expr.Any{}, pm...), d...), sp...)
out = append(out, l4Match{proto: n, exprs: e})
}
}
}
return out, nil
}
return exprs, nil
func portAlternatives(ports config.PortSpec, parse func(string) ([]expr.Any, error)) ([][]expr.Any, error) {
var alts [][]expr.Any
for _, item := range ports {
for _, s := range strings.Split(item, ",") {
if s = strings.TrimSpace(s); s == "" {
return nil, fmt.Errorf("empty element in port list %q", item)
}
e, err := parse(s)
if err != nil {
return nil, err
}
alts = append(alts, e)
}
}
if len(alts) == 0 {
return [][]expr.Any{nil}, nil
}
return alts, nil
}
// matchIfaceName matches an interface name, supporting wildcard "+" suffix.
@@ -1003,33 +1146,27 @@ func matchIfaceName(input bool, name string) []expr.Any {
}
}
func matchProto(proto string) []expr.Any {
var protoNum byte
switch strings.ToLower(proto) {
case "tcp":
protoNum = unix.IPPROTO_TCP
case "udp":
protoNum = unix.IPPROTO_UDP
case "icmp":
protoNum = unix.IPPROTO_ICMP
case "icmpv6", "ipv6-icmp":
protoNum = unix.IPPROTO_ICMPV6
case "gre":
protoNum = 47
case "esp":
protoNum = 50
case "ah":
protoNum = 51
case "sctp":
protoNum = unix.IPPROTO_SCTP
default:
n, _ := strconv.Atoi(proto)
protoNum = byte(n)
var protoNumbers = map[string]byte{
"icmp": 1, "igmp": 2, "ipip": 4, "ipencap": 4, "tcp": 6, "udp": 17,
"gre": 47, "esp": 50, "ah": 51, "icmpv6": 58, "ipv6-icmp": 58,
"ospf": 89, "ospfigp": 89, "pim": 103, "vrrp": 112, "l2tp": 115,
"sctp": 132, "udplite": 136,
}
// protoNumber resolves a common IANA protocol name or a 0-255 number.
func protoNumber(proto string) (byte, error) {
if n, ok := protoNumbers[strings.ToLower(proto)]; ok {
return n, nil
}
return []expr.Any{
&expr.Meta{Key: expr.MetaKeyL4PROTO, Register: 1},
&expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{protoNum}},
n, err := strconv.ParseUint(proto, 10, 8)
if err != nil {
return 0, fmt.Errorf("unknown protocol %q", proto)
}
return byte(n), nil
}
func hasPorts(proto byte) bool {
return proto == unix.IPPROTO_TCP || proto == unix.IPPROTO_UDP || proto == unix.IPPROTO_SCTP || proto == unix.IPPROTO_UDPLITE
}
func matchDPort(port uint16) []expr.Any {
@@ -1141,33 +1278,33 @@ var icmpTypeNames = map[string]byte{
"address-mask-reply": 18,
}
func matchICMPType(spec string) []expr.Any {
func matchICMPType(spec string) ([]expr.Any, error) {
if strings.Contains(spec, "/") {
parts := strings.SplitN(spec, "/", 2)
typeVal, ok := resolveICMPType(parts[0])
if !ok {
return nil
return nil, fmt.Errorf("invalid icmp type %q", parts[0])
}
code, err := strconv.ParseUint(parts[1], 10, 8)
if err != nil {
return nil
return nil, fmt.Errorf("invalid icmp code %q: %w", parts[1], err)
}
return []expr.Any{
&expr.Payload{DestRegister: 1, Base: expr.PayloadBaseTransportHeader, Offset: 0, Len: 1},
&expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{typeVal}},
&expr.Payload{DestRegister: 1, Base: expr.PayloadBaseTransportHeader, Offset: 1, Len: 1},
&expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{byte(code)}},
}
}, nil
}
typeVal, ok := resolveICMPType(spec)
if !ok {
return nil
return nil, fmt.Errorf("invalid icmp type %q", spec)
}
return []expr.Any{
&expr.Payload{DestRegister: 1, Base: expr.PayloadBaseTransportHeader, Offset: 0, Len: 1},
&expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{typeVal}},
}
}, nil
}
func resolveICMPType(s string) (byte, bool) {
@@ -1218,8 +1355,8 @@ func matchConnLimit(spec string) []expr.Any {
}
func parsePortOrRange(s string) ([]expr.Any, error) {
if strings.Contains(s, "-") {
parts := strings.SplitN(s, "-", 2)
if i := strings.IndexAny(s, "-:"); i >= 0 {
parts := []string{s[:i], s[i+1:]}
low, err := strconv.ParseUint(parts[0], 10, 16)
if err != nil {
return nil, fmt.Errorf("invalid port range low %q: %w", parts[0], err)
@@ -1238,8 +1375,8 @@ func parsePortOrRange(s string) ([]expr.Any, error) {
}
func parseSPortOrRange(s string) ([]expr.Any, error) {
if strings.Contains(s, "-") {
parts := strings.SplitN(s, "-", 2)
if i := strings.IndexAny(s, "-:"); i >= 0 {
parts := []string{s[:i], s[i+1:]}
low, err := strconv.ParseUint(parts[0], 10, 16)
if err != nil {
return nil, fmt.Errorf("invalid sport range low %q: %w", parts[0], err)
@@ -1265,11 +1402,46 @@ func matchDestCIDR(cidr string) ([]expr.Any, error) {
return matchAddrCIDR(cidr, false)
}
// matchOrigDest guards the daddr match with the address's nfproto so it is family-correct in the inet table.
func matchOrigDest(addr string) ([]expr.Any, error) {
var proto byte
for i, a := range strings.Split(strings.TrimPrefix(addr, "!"), ",") {
a, _, _ = strings.Cut(a, "/")
p := byte(unix.NFPROTO_IPV6)
if ip := net.ParseIP(a); ip != nil && ip.To4() != nil {
p = unix.NFPROTO_IPV4
}
if i > 0 && p != proto {
return nil, fmt.Errorf("%q mixes IPv4 and IPv6 addresses", addr)
}
proto = p
}
dst, err := matchDestCIDR(addr)
if err != nil {
return nil, err
}
return append([]expr.Any{
&expr.Meta{Key: expr.MetaKeyNFPROTO, Register: 1},
&expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{proto}},
}, dst...), nil
}
func matchAddrCIDR(cidr string, isSrc bool) ([]expr.Any, error) {
negated := false
if strings.HasPrefix(cidr, "!") {
negated = true
cidr = cidr[1:]
if strings.Contains(cidr, ",") {
var all []expr.Any
for _, a := range strings.Split(cidr, ",") {
e, err := matchAddrCIDR("!"+a, isSrc)
if err != nil {
return nil, err
}
all = append(all, e...)
}
return all, nil
}
}
cmpOp := expr.CmpOpEq
@@ -1619,7 +1791,7 @@ func logLevelToNF(level string) expr.LogLevel {
}
}
func actionVerdict(action config.RuleAction, proto string, family config.AddressFamily) []expr.Any {
func actionVerdict(action config.RuleAction, proto byte, family config.AddressFamily) []expr.Any {
switch action {
case config.RuleAccept:
return []expr.Any{&expr.Verdict{Kind: expr.VerdictAccept}}
@@ -1644,8 +1816,8 @@ func actionVerdict(action config.RuleAction, proto string, family config.Address
}
}
func rejectExprs(proto string, family config.AddressFamily) []expr.Any {
if strings.ToLower(proto) == "tcp" {
func rejectExprs(proto byte, family config.AddressFamily) []expr.Any {
if proto == unix.IPPROTO_TCP {
return []expr.Any{&expr.Reject{
Type: unix.NFT_REJECT_TCP_RST,
Code: 0,
@@ -1664,7 +1836,7 @@ func policyVerdict(action config.PolicyAction, family config.AddressFamily) []ex
case config.PolicyDrop:
return []expr.Any{&expr.Verdict{Kind: expr.VerdictDrop}}
case config.PolicyReject:
return rejectExprs("", family)
return rejectExprs(0, family)
default:
return []expr.Any{&expr.Verdict{Kind: expr.VerdictDrop}}
}
+893 -11
View File
@@ -1,9 +1,17 @@
package nftables
import (
"bytes"
"encoding/binary"
"fmt"
"log/slog"
"net"
"reflect"
"strings"
"testing"
"github.com/google/nftables/expr"
"golang.org/x/sys/unix"
"git.unkin.net/unkin/tomswall/internal/config"
)
@@ -136,17 +144,17 @@ func TestCompiler_ResolveZoneInterfaces(t *testing.T) {
}
c := NewCompiler(cfg)
ifaces := c.resolveZoneInterfaces("net")
ifaces := c.resolveZoneInterfaces("net", "")
if len(ifaces) != 1 || ifaces[0] != "eth0" {
t.Errorf("resolveZoneInterfaces(net) = %v, want [eth0]", ifaces)
}
ifaces = c.resolveZoneInterfaces("all")
ifaces = c.resolveZoneInterfaces("all", "")
if len(ifaces) != 1 || ifaces[0] != "" {
t.Errorf("resolveZoneInterfaces(all) = %v, want [\"\"]", ifaces)
}
ifaces = c.resolveZoneInterfaces("fw")
ifaces = c.resolveZoneInterfaces("fw", "")
if len(ifaces) != 1 || ifaces[0] != "" {
t.Errorf("resolveZoneInterfaces(fw) = %v, want [\"\"]", ifaces)
}
@@ -973,7 +981,7 @@ func TestNegatedAddress(t *testing.T) {
}
func TestRejectTCPRST(t *testing.T) {
exprs := rejectExprs("tcp", config.FamilyINET)
exprs := rejectExprs(unix.IPPROTO_TCP, config.FamilyINET)
if len(exprs) != 1 {
t.Fatalf("expected 1 expr, got %d", len(exprs))
}
@@ -982,7 +990,7 @@ func TestRejectTCPRST(t *testing.T) {
t.Errorf("TCP reject should use NFT_REJECT_TCP_RST (1), got %d", rej.Type)
}
exprs = rejectExprs("udp", config.FamilyINET)
exprs = rejectExprs(unix.IPPROTO_UDP, config.FamilyINET)
rej = exprs[0].(*expr.Reject)
if rej.Type != 2 {
t.Errorf("non-TCP reject should use NFT_REJECT_ICMPX_UNREACH (2), got %d", rej.Type)
@@ -1197,14 +1205,26 @@ func TestCompile_MSSClamp(t *testing.T) {
t.Fatalf("Compile() error: %v", err)
}
found := false
for _, r := range state.Rules["forward"] {
var rule *ManagedRule
for i, r := range state.Rules["forward"] {
if r.Tag == "mss:eth1" {
found = true
rule = &state.Rules["forward"][i]
}
}
if !found {
t.Error("MSS clamp rule not found in forward chain")
if rule == nil {
t.Fatal("MSS clamp rule not found in forward chain")
}
mss := []byte{0x05, 0x78}
want := []expr.Any{
&expr.Exthdr{DestRegister: 1, Type: 2, Offset: 2, Len: 2, Op: expr.ExthdrOpTcpopt},
&expr.Cmp{Op: expr.CmpOpGt, Register: 1, Data: mss},
&expr.Immediate{Register: 1, Data: mss},
&expr.Exthdr{SourceRegister: 1, Type: 2, Offset: 2, Len: 2, Op: expr.ExthdrOpTcpopt},
}
got := rule.Exprs[len(rule.Exprs)-len(want):]
if !reflect.DeepEqual(got, want) {
t.Errorf("MSS clamp exprs = %#v, want %#v", got, want)
}
}
@@ -1265,7 +1285,10 @@ func TestMatchICMPType(t *testing.T) {
}
for _, tt := range tests {
exprs := matchICMPType(tt.input)
exprs, err := matchICMPType(tt.input)
if err != nil {
t.Fatalf("matchICMPType(%q) error: %v", tt.input, err)
}
if len(exprs) != tt.wantLen {
t.Errorf("matchICMPType(%q) returned %d exprs, want %d", tt.input, len(exprs), tt.wantLen)
}
@@ -1531,3 +1554,862 @@ func TestCompile_PolicyRateLimit(t *testing.T) {
}
t.Error("policy:0 not found in input chain")
}
func diffTestConfig(port string) *config.Config {
return &config.Config{
Settings: config.Settings{TableName: "test", AddressFamily: config.FamilyINET},
Zones: map[string]config.Zone{
"fw": {Type: config.ZoneFirewall},
"net": {Type: config.ZoneIP},
"loc": {Type: config.ZoneIP},
"dmz": {Type: config.ZoneIP},
},
Interfaces: []config.Interface{
{Zone: "net", Interface: "eth0"},
{Zone: "loc", Interface: "eth1"},
{Zone: "dmz", Interface: "eth2"},
},
Policy: []config.Policy{
{Source: "net", Dest: "all", Action: config.PolicyDrop, Log: "info"},
{Source: "all", Dest: "all", Action: config.PolicyReject},
},
Rules: []config.Rule{
{Action: config.RuleAccept, Source: "net:192.0.2.0/24", Dest: "fw", Proto: "tcp", DPort: config.PortSpec{port}},
{Action: config.RuleDNAT, Source: "net", Dest: "loc:198.51.100.10:80", Proto: "tcp", DPort: config.PortSpec{"8000"}},
{Action: config.RuleNFQueue, Source: "net", Dest: "fw", Proto: "tcp", DPort: config.PortSpec{"80"}, NFQueue: 3},
{Action: config.RuleRedirect, Source: "loc", Dest: "fw:192.0.2.1:3128", Proto: "tcp", DPort: config.PortSpec{"80"}},
{Action: config.RuleAccept, Source: "loc,dmz", Dest: "fw,net", Proto: "tcp,udp", DPort: config.PortSpec{"53", "5353"}},
},
SNAT: []config.SNATRule{{Action: config.SNATAddress, Source: "198.51.100.0/24", Dest: "eth0", Address: "203.0.113.7"}},
PortGroups: make(map[string]config.PortGroup),
}
}
func TestDiffEngine_IndependentCompilesMatch(t *testing.T) {
compile := func(port string) *FirewallState {
t.Helper()
s, err := NewCompiler(diffTestConfig(port)).Compile()
if err != nil {
t.Fatal(err)
}
return s
}
for i := 0; i < 20; i++ {
if cs := computeDiff(compile("22"), compile("22")); !cs.Empty() {
t.Fatalf("expected empty changeset, got:\n%s", cs.Summary())
}
}
cs := computeDiff(compile("22"), compile("2222"))
if len(cs.Add) != 1 || len(cs.Remove) != 1 || cs.Add[0].Tag != "rule:0" {
t.Errorf("expected rule:0 replaced, got:\n%s", cs.Summary())
}
}
// shapes observed by applying diffTestConfig in a netns and reading it back
func TestCompile_QueueRedirMatchKernelReadback(t *testing.T) {
state, err := NewCompiler(diffTestConfig("22")).Compile()
if err != nil {
t.Fatal(err)
}
want := map[string]expr.Any{
"rule:2": &expr.Queue{Num: 3, Total: 1},
"rule:3": &expr.Redir{RegisterProtoMin: 1, RegisterProtoMax: 1, Flags: unix.NF_NAT_RANGE_PROTO_SPECIFIED},
}
for _, rules := range state.Rules {
for _, r := range rules {
w, ok := want[r.Tag]
if !ok {
continue
}
if got := r.Exprs[len(r.Exprs)-1]; !reflect.DeepEqual(got, w) {
t.Errorf("%s: got %#v, want %#v", r.Tag, got, w)
}
delete(want, r.Tag)
}
}
for tag := range want {
t.Errorf("%s not compiled", tag)
}
}
func withHandles(s *FirewallState) *FirewallState {
h := uint64(100)
for _, rules := range s.Rules {
for i := range rules {
rules[i].Handle = h
h++
}
}
return s
}
func tags(rules []ManagedRule) []string {
out := make([]string, len(rules))
for i, r := range rules {
out[i] = r.Chain + "/" + r.Tag
}
return out
}
func TestCompile_PortAndProtoLists(t *testing.T) {
type want struct {
proto byte
dport string // "80" for an exact compare, "8000-8100" for a range
}
tests := []struct {
name string
rule config.Rule
chain string
want []want
}{
{
name: "multi-port",
rule: config.Rule{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "tcp", DPort: config.PortSpec{"80", "443"}},
chain: "input",
want: []want{{6, "80"}, {6, "443"}},
},
{
name: "range in list",
rule: config.Rule{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "tcp", DPort: config.PortSpec{"22", "8000:8100"}},
chain: "input",
want: []want{{6, "22"}, {6, "8000-8100"}},
},
{
name: "comma string",
rule: config.Rule{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "tcp", DPort: config.PortSpec{"80,443"}},
chain: "input",
want: []want{{6, "80"}, {6, "443"}},
},
{
name: "tcp,udp",
rule: config.Rule{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "tcp,udp", DPort: config.PortSpec{"53"}},
chain: "input",
want: []want{{6, "53"}, {17, "53"}},
},
{
name: "dnat tcp,udp",
rule: config.Rule{Action: config.RuleDNAT, Source: "net", Dest: "fw:192.0.2.10", Proto: "tcp,udp", DPort: config.PortSpec{"53"}},
chain: "prerouting",
want: []want{{6, "53"}, {17, "53"}},
},
{
name: "protocol names",
rule: config.Rule{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "ospf,OSPFIGP,igmp,gre,esp,ah,vrrp,pim,ipencap,ipv6-icmp"},
chain: "input",
want: []want{{89, ""}, {89, ""}, {2, ""}, {47, ""}, {50, ""}, {51, ""}, {112, ""}, {103, ""}, {4, ""}, {58, ""}},
},
{
name: "protocol numbers",
rule: config.Rule{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "0,89,255"},
chain: "input",
want: []want{{0, ""}, {89, ""}, {255, ""}},
},
{
name: "numeric tcp with port",
rule: config.Rule{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "6,udplite", DPort: config.PortSpec{"22"}},
chain: "input",
want: []want{{6, "22"}, {136, "22"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &config.Config{
Settings: config.Settings{TableName: "test", AddressFamily: config.FamilyINET},
Zones: map[string]config.Zone{"fw": {Type: config.ZoneFirewall}, "net": {Type: config.ZoneIP}},
Interfaces: []config.Interface{{Zone: "net", Interface: "eth0"}},
Policy: []config.Policy{{Source: "all", Dest: "all", Action: config.PolicyDrop}},
Rules: []config.Rule{tt.rule},
PortGroups: make(map[string]config.PortGroup),
}
state, err := NewCompiler(cfg).Compile()
if err != nil {
t.Fatalf("Compile() error: %v", err)
}
var got []want
for _, r := range state.Rules[tt.chain] {
if r.Tag != "rule:0" {
continue
}
var w want
var portCmps []string
for i, e := range r.Exprs {
if m, ok := e.(*expr.Meta); ok && m.Key == expr.MetaKeyL4PROTO {
w.proto = r.Exprs[i+1].(*expr.Cmp).Data[0]
}
if p, ok := e.(*expr.Payload); ok && p.Base == expr.PayloadBaseTransportHeader && p.Offset == 2 {
for _, c := range r.Exprs[i+1:] {
cmp, ok := c.(*expr.Cmp)
if !ok {
break
}
portCmps = append(portCmps, fmt.Sprint(binary.BigEndian.Uint16(cmp.Data)))
}
}
}
w.dport = strings.Join(portCmps, "-")
got = append(got, w)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("rules = %+v, want %+v", got, tt.want)
}
})
}
}
func TestCompile_OutputPolicyMatchesOif(t *testing.T) {
cfg := listCfg(func(c *config.Config) {
c.Zones["lan"] = config.Zone{Type: config.ZoneIP}
c.Interfaces = append(c.Interfaces, config.Interface{Zone: "lan", Interface: "eth1"})
c.Policy = []config.Policy{{Source: "fw", Dest: "lan", Action: config.PolicyAccept}}
})
got := taggedRules(mustCompile(t, cfg), "output", "policy:0")
if len(got) != 1 || describeRule(got[0]) != "oif=eth1" {
t.Fatalf("fw->lan policy = %v, want one rule oif=eth1", got)
}
}
func TestCompile_InterfacelessZonesFailClosed(t *testing.T) {
ipsec := func(c *config.Config) { c.Zones["ips"] = config.Zone{Type: config.ZoneIPSec} }
rule := func(dest string) func(*config.Config) {
return func(c *config.Config) {
ipsec(c)
c.Rules = []config.Rule{{Action: config.RuleAccept, Source: "fw", Dest: dest}}
}
}
tests := []struct {
name string
mod func(*config.Config)
tag string
want int
warns []string
}{
{"ipsec zone without interface", func(c *config.Config) {
ipsec(c)
c.Policy = []config.Policy{{Source: "fw", Dest: "ips", Action: config.PolicyAccept}}
}, "policy:0", 0, []string{"ips"}},
{"hosts-only zone", func(c *config.Config) {
c.Zones["hst"] = config.Zone{Type: config.ZoneIP}
c.Hosts = []config.Host{{Zone: "hst", Interface: "eth0", Addresses: []string{"192.0.2.0/24"}}}
c.Policy = []config.Policy{{Source: "fw", Dest: "hst", Action: config.PolicyAccept}}
}, "policy:0", 0, []string{"hst"}},
{"fw all expansion keeps zones with interfaces", func(c *config.Config) {
ipsec(c)
c.Zones["hst"] = config.Zone{Type: config.ZoneIP}
c.Hosts = []config.Host{{Zone: "hst", Interface: "eth0", Addresses: []string{"192.0.2.0/24"}}}
c.Policy = []config.Policy{{Source: "fw", Dest: "all", Action: config.PolicyDrop}}
}, "policy:0", 1, []string{"hst", "ips"}},
{"negated address does not scope", rule("ips:!192.0.2.1"), "rule:0", 0, []string{"ips"}},
{"address scopes", rule("ips:192.0.2.1"), "rule:0", 1, []string{"ips"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var logs bytes.Buffer
prev := slog.Default()
slog.SetDefault(slog.New(slog.NewTextHandler(&logs, nil)))
defer slog.SetDefault(prev)
state := mustCompile(t, listCfg(tt.mod))
got := 0
for chain := range state.Rules {
for _, r := range taggedRules(state, chain, tt.tag) {
got++
if describeRule(r) == "" {
t.Errorf("%s: un-scoped rule", chain)
}
}
}
if got != tt.want {
t.Errorf("got %d %s rules, want %d", got, tt.tag, tt.want)
}
if n := strings.Count(logs.String(), "zone has no interfaces"); n != len(tt.warns) {
t.Errorf("got %d warnings, want %d:\n%s", n, len(tt.warns), logs.String())
}
for _, z := range tt.warns {
if n := strings.Count(logs.String(), "zone="+z+"\n"); n != 1 {
t.Errorf("zone %s warned %d times, want 1", z, n)
}
}
})
}
}
// describeRule renders a rule's iif/oif/saddr/daddr matches, e.g. "iif=eth1 oif=eth2 daddr=192.0.2.1".
func describeRule(r ManagedRule) string {
var parts []string
for i, e := range r.Exprs {
cmp, ok := func() (*expr.Cmp, bool) {
if i+1 >= len(r.Exprs) {
return nil, false
}
c, ok := r.Exprs[i+1].(*expr.Cmp)
return c, ok
}()
if !ok {
continue
}
switch m := e.(type) {
case *expr.Meta:
switch m.Key {
case expr.MetaKeyIIFNAME:
parts = append(parts, "iif="+strings.TrimRight(string(cmp.Data), "\x00"))
case expr.MetaKeyOIFNAME:
parts = append(parts, "oif="+strings.TrimRight(string(cmp.Data), "\x00"))
}
case *expr.Payload:
if m.Base == expr.PayloadBaseNetworkHeader && (m.Len == 4 || m.Len == 16) {
name := map[uint32]string{12: "saddr", 16: "daddr", 8: "saddr", 24: "daddr"}[m.Offset]
if cmp.Op == expr.CmpOpNeq {
name = "!" + name
}
parts = append(parts, name+"="+net.IP(cmp.Data).String())
}
}
}
return strings.Join(parts, " ")
}
func TestCompile_CommaZoneLists(t *testing.T) {
tests := []struct {
name string
rule config.Rule
blrule *config.BlruleRule
want map[string][]string
}{
{
name: "fw in source list goes to output",
rule: config.Rule{Action: config.RuleAccept, Source: "fw,lan", Dest: "svr", Proto: "tcp", DPort: config.PortSpec{"22"}},
want: map[string][]string{"output": {"oif=eth2"}, "forward": {"iif=eth1 oif=eth2"}},
},
{
name: "dest list with fw splits input and forward",
rule: config.Rule{Action: config.RuleAccept, Source: "lan", Dest: "fw,svr,net"},
want: map[string][]string{"input": {"iif=eth1"}, "forward": {"iif=eth1 oif=eth2", "iif=eth1 oif=eth0"}},
},
{
name: "zone without interfaces emits nothing",
rule: config.Rule{Action: config.RuleAccept, Source: "lan", Dest: "svr,dmz"},
want: map[string][]string{"forward": {"iif=eth1 oif=eth2"}},
},
{
name: "address list after colon belongs to one zone",
rule: config.Rule{Action: config.RuleAccept, Source: "lan", Dest: "net:192.0.2.1,198.51.100.1"},
want: map[string][]string{"forward": {"iif=eth1 oif=eth0 daddr=192.0.2.1", "iif=eth1 oif=eth0 daddr=198.51.100.1"}},
},
{
name: "zone:address inside a list",
rule: config.Rule{Action: config.RuleAccept, Source: "lan,svr:203.0.113.7", Dest: "fw"},
want: map[string][]string{"input": {"iif=eth1", "iif=eth2 saddr=203.0.113.7"}},
},
{
name: "dnat source list",
rule: config.Rule{Action: config.RuleDNAT, Source: "net,lan", Dest: "svr:192.0.2.10", Proto: "tcp", DPort: config.PortSpec{"80"}},
want: map[string][]string{"prerouting": {"iif=eth0", "iif=eth1"}},
},
{
name: "dnat source address list",
rule: config.Rule{Action: config.RuleDNAT, Source: "net:192.0.2.5,198.51.100.5", Dest: "svr:192.0.2.10", Proto: "tcp", DPort: config.PortSpec{"80"}},
want: map[string][]string{"prerouting": {"iif=eth0 saddr=192.0.2.5", "iif=eth0 saddr=198.51.100.5"}},
},
{
name: "negated address list stays one AND-ed rule",
rule: config.Rule{Action: config.RuleAccept, Source: "net:!192.0.2.5,198.51.100.5", Dest: "fw"},
want: map[string][]string{"input": {"iif=eth0 !saddr=192.0.2.5 !saddr=198.51.100.5"}},
},
{
name: "zone named like all/any keyword is a plain zone",
rule: config.Rule{Action: config.RuleAccept, Source: "lan", Dest: "anycast,net"},
want: map[string][]string{"forward": {"iif=eth1 oif=eth3", "iif=eth1 oif=eth0"}},
},
{
name: "interface-less zones are skipped",
rule: config.Rule{Action: config.RuleAccept, Source: "lan", Dest: "vpn,dmz"},
want: map[string][]string{},
},
{
name: "interface-less zone kept when address narrows it",
rule: config.Rule{Action: config.RuleAccept, Source: "lan", Dest: "vpn:192.0.2.1"},
want: map[string][]string{"forward": {"iif=eth1 daddr=192.0.2.1"}},
},
{
name: "fw source matches dest zone oif",
rule: config.Rule{Action: config.RuleAccept, Source: "fw", Dest: "lan,vpn,dmz", Proto: "tcp", DPort: config.PortSpec{"22"}},
want: map[string][]string{"output": {"oif=eth1"}},
},
{
name: "fw to all has no oif",
rule: config.Rule{Action: config.RuleAccept, Source: "fw", Dest: "all:192.0.2.1"},
want: map[string][]string{"output": {"daddr=192.0.2.1"}},
},
{
name: "dnat origdest",
rule: config.Rule{Action: config.RuleDNAT, Source: "net", Dest: "svr:192.0.2.17", Proto: "tcp", DPort: config.PortSpec{"80"}, OrigDest: "203.0.113.5"},
want: map[string][]string{"prerouting": {"iif=eth0 daddr=203.0.113.5"}},
},
{
name: "dnat origdest list",
rule: config.Rule{Action: config.RuleDNAT, Source: "net", Dest: "svr:192.0.2.17", Proto: "tcp", DPort: config.PortSpec{"80"}, OrigDest: "203.0.113.5,203.0.113.6"},
want: map[string][]string{"prerouting": {"iif=eth0 daddr=203.0.113.5", "iif=eth0 daddr=203.0.113.6"}},
},
{
name: "dnat negated origdest list",
rule: config.Rule{Action: config.RuleDNAT, Source: "net", Dest: "svr:192.0.2.17", Proto: "tcp", DPort: config.PortSpec{"80"}, OrigDest: "!203.0.113.5,203.0.113.6"},
want: map[string][]string{"prerouting": {"iif=eth0 !daddr=203.0.113.5 !daddr=203.0.113.6"}},
},
{
name: "accept origdest",
rule: config.Rule{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "tcp", DPort: config.PortSpec{"22"}, OrigDest: "203.0.113.5"},
want: map[string][]string{"input": {"iif=eth0 daddr=203.0.113.5"}},
},
{
name: "origdest does not scope interface-less zone",
rule: config.Rule{Action: config.RuleAccept, Source: "vpn", Dest: "fw", Proto: "tcp", DPort: config.PortSpec{"22"}, OrigDest: "203.0.113.5"},
want: map[string][]string{},
},
{
name: "accept ipv6 origdest",
rule: config.Rule{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "tcp", DPort: config.PortSpec{"22"}, OrigDest: "2001:db8::5"},
want: map[string][]string{"input": {"iif=eth0 daddr=2001:db8::5"}},
},
{
name: "blrule zone list",
blrule: &config.BlruleRule{Action: config.BlruleDrop, Source: "net,anycast", Dest: "fw"},
want: map[string][]string{"input": {"iif=eth0", "iif=eth3"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &config.Config{
Settings: config.Settings{TableName: "test", AddressFamily: config.FamilyINET},
Zones: map[string]config.Zone{
"fw": {Type: config.ZoneFirewall}, "net": {Type: config.ZoneIP},
"lan": {Type: config.ZoneIP}, "svr": {Type: config.ZoneIP}, "dmz": {Type: config.ZoneIP},
"anycast": {Type: config.ZoneIP}, "vpn": {Type: config.ZoneIPSec},
},
Interfaces: []config.Interface{
{Zone: "net", Interface: "eth0"}, {Zone: "lan", Interface: "eth1"}, {Zone: "svr", Interface: "eth2"},
{Zone: "anycast", Interface: "eth3"},
},
Rules: []config.Rule{tt.rule},
PortGroups: make(map[string]config.PortGroup),
}
tag := "rule:0"
if tt.blrule != nil {
cfg.Rules, cfg.Blrules, tag = nil, []config.BlruleRule{*tt.blrule}, "blrule:0"
}
state, err := NewCompiler(cfg).Compile()
if err != nil {
t.Fatalf("Compile() error: %v", err)
}
got := map[string][]string{}
for chain, rules := range state.Rules {
for _, r := range rules {
if r.Tag == tag {
got[chain] = append(got[chain], describeRule(r))
}
}
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("rules = %v, want %v", got, tt.want)
}
})
}
}
func listCfg(mod func(*config.Config)) *config.Config {
cfg := &config.Config{
Settings: config.Settings{TableName: "test", AddressFamily: config.FamilyINET},
Zones: map[string]config.Zone{"fw": {Type: config.ZoneFirewall}, "net": {Type: config.ZoneIP}},
Interfaces: []config.Interface{{Zone: "net", Interface: "eth0"}},
Policy: []config.Policy{{Source: "all", Dest: "all", Action: config.PolicyDrop}},
PortGroups: make(map[string]config.PortGroup),
}
mod(cfg)
return cfg
}
func taggedRules(state *FirewallState, chain, tag string) []ManagedRule {
var out []ManagedRule
for _, r := range state.Rules[chain] {
if r.Tag == tag {
out = append(out, r)
}
}
return out
}
func TestDiffEngine_FreshApplyKeepsDesiredOrder(t *testing.T) {
desired, err := NewCompiler(diffTestConfig("22")).Compile()
if err != nil {
t.Fatal(err)
}
var want []string
for _, chain := range []string{"forward", "input", "output", "postrouting", "prerouting"} {
want = append(want, tags(desired.Rules[chain])...)
}
for i := 0; i < 20; i++ {
cs := computeDiff(&FirewallState{Rules: map[string][]ManagedRule{}}, desired)
if got := tags(cs.Add); !reflect.DeepEqual(got, want) {
t.Fatalf("add order:\n got %v\nwant %v", got, want)
}
for _, r := range cs.Add {
if r.Before != 0 {
t.Fatalf("fresh apply should append, %s has Before=%d", r.Tag, r.Before)
}
}
}
}
func TestDiffEngine_MiddleChangeInsertsBeforeNextRule(t *testing.T) {
current := withHandles(mustCompile(t, diffTestConfig("22")))
desired := mustCompile(t, diffTestConfig("2222"))
cs := computeDiff(current, desired)
if len(cs.Add) != 1 || len(cs.Remove) != 1 {
t.Fatalf("expected one replace, got:\n%s", cs.Summary())
}
input := current.Rules["input"]
idx := -1
for i, r := range input {
if r.Tag == "rule:0" {
idx = i
}
}
if idx < 1 || idx == len(input)-1 {
t.Fatalf("rule:0 at %d is not mid-chain", idx)
}
if cs.Remove[0].Handle != input[idx].Handle {
t.Errorf("removed handle %d, want %d", cs.Remove[0].Handle, input[idx].Handle)
}
if cs.Add[0].Before != input[idx+1].Handle {
t.Errorf("insert before %d, want %d (%s)", cs.Add[0].Before, input[idx+1].Handle, input[idx+1].Tag)
}
}
func TestDiffEngine_ExpandedRuleReplacedInPlace(t *testing.T) {
current := withHandles(mustCompile(t, diffTestConfig("22")))
if cs := computeDiff(current, mustCompile(t, diffTestConfig("22"))); !cs.Empty() {
t.Fatalf("expected empty changeset, got:\n%s", cs.Summary())
}
cfg := diffTestConfig("22")
cfg.Rules[4].DPort = config.PortSpec{"53", "853"}
desired := mustCompile(t, cfg)
cs := computeDiff(current, desired)
for _, r := range append(append([]ManagedRule{}, cs.Add...), cs.Remove...) {
if r.Tag != "rule:4" {
t.Errorf("unexpected change to %s/%s", r.Chain, r.Tag)
}
}
for _, chain := range []string{"input", "forward"} {
if n := len(taggedRules(desired, chain, "rule:4")); n != 8 {
t.Fatalf("%s: expected 8 expanded rule:4 rules, got %d", chain, n)
}
}
applied := applyChangeSet(current, cs)
if cs := computeDiff(applied, desired); !cs.Empty() {
t.Fatalf("second plan not empty:\n%s", cs.Summary())
}
}
// applyChangeSet mimics the engine: removals by handle, adds inserted before r.Before or appended.
func applyChangeSet(s *FirewallState, cs *ChangeSet) *FirewallState {
gone := map[uint64]bool{}
for _, r := range cs.Remove {
gone[r.Handle] = true
}
out := &FirewallState{Rules: map[string][]ManagedRule{}}
for chain, rules := range s.Rules {
for _, r := range rules {
if !gone[r.Handle] {
out.Rules[chain] = append(out.Rules[chain], r)
}
}
}
h := uint64(10000)
for _, r := range cs.Add {
r.Handle, h = h, h+1
rules := out.Rules[r.Chain]
i := len(rules)
for j, x := range rules {
if r.Before != 0 && x.Handle == r.Before {
i = j
break
}
}
r.Before = 0
out.Rules[r.Chain] = append(rules[:i], append([]ManagedRule{r}, rules[i:]...)...)
}
return out
}
func mustCompile(t *testing.T, cfg *config.Config) *FirewallState {
t.Helper()
s, err := NewCompiler(cfg).Compile()
if err != nil {
t.Fatal(err)
}
return s
}
func TestCompile_ListExpansionCounts(t *testing.T) {
tests := []struct {
name string
mod func(*config.Config)
chain string
tag string
want int
}{
{"proto x dport cross product", func(c *config.Config) {
c.Rules = []config.Rule{{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "tcp,udp", DPort: config.PortSpec{"80", "443"}}}
}, "input", "rule:0", 4},
{"sport list", func(c *config.Config) {
c.Rules = []config.Rule{{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "tcp", SPort: config.PortSpec{"1024,2048"}}}
}, "input", "rule:0", 2},
{"snat proto x dport", func(c *config.Config) {
c.SNAT = []config.SNATRule{{Action: config.SNATMasquerade, Dest: "eth0", Proto: "tcp,udp", DPort: config.PortSpec{"80,443"}}}
}, "postrouting", "snat:0", 4},
{"conntrack dport list", func(c *config.Config) {
c.Conntrack = []config.ConntrackRule{{Action: config.ConntrackNoTrack, Proto: "udp", DPort: config.PortSpec{"53", "123"}}}
}, "prerouting", "conntrack:0:prerouting", 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
state, err := NewCompiler(listCfg(tt.mod)).Compile()
if err != nil {
t.Fatalf("Compile() error: %v", err)
}
if got := len(taggedRules(state, tt.chain, tt.tag)); got != tt.want {
t.Errorf("%s rules in %s = %d, want %d", tt.tag, tt.chain, got, tt.want)
}
})
}
}
func TestCompile_DNATGetsNoRuleExtras(t *testing.T) {
compile := func(mark string) []expr.Any {
state, err := NewCompiler(listCfg(func(c *config.Config) {
c.Rules = []config.Rule{{Action: config.RuleDNAT, Source: "net", Dest: "fw:192.0.2.10", Proto: "tcp", DPort: config.PortSpec{"80"}, Mark: mark}}
})).Compile()
if err != nil {
t.Fatalf("Compile() error: %v", err)
}
return state.Rules["prerouting"][len(state.Rules["prerouting"])-1].Exprs
}
if plain, marked := compile(""), compile("0x1"); !reflect.DeepEqual(plain, marked) {
t.Errorf("DNAT prerouting rule changed by mark extra: %d exprs vs %d", len(plain), len(marked))
}
}
func TestCompile_CommaZoneListLimitErrors(t *testing.T) {
for _, r := range []config.Rule{
{Action: config.RuleAccept, Source: "net", Dest: "fw,lan", RateLimit: "10/sec:5"},
{Action: config.RuleAccept, Source: "net,lan", Dest: "fw", ConnLimit: "10"},
{Action: config.RuleAccept, Source: "net", Dest: "fw:192.0.2.1,198.51.100.1", RateLimit: "10/sec"},
} {
t.Run(r.Source+">"+r.Dest, func(t *testing.T) {
cfg := &config.Config{
Settings: config.Settings{TableName: "test", AddressFamily: config.FamilyINET},
Zones: map[string]config.Zone{"fw": {Type: config.ZoneFirewall}, "net": {Type: config.ZoneIP}, "lan": {Type: config.ZoneIP}},
Interfaces: []config.Interface{{Zone: "net", Interface: "eth0"}, {Zone: "lan", Interface: "eth1"}},
Rules: []config.Rule{r},
PortGroups: make(map[string]config.PortGroup),
}
if _, err := NewCompiler(cfg).Compile(); err == nil {
t.Fatal("Compile() succeeded, want error")
}
})
}
}
func TestCompile_RejectPerProto(t *testing.T) {
tests := []struct {
proto string
want []uint32
}{
{"tcp,udp", []uint32{unix.NFT_REJECT_TCP_RST, unix.NFT_REJECT_ICMPX_UNREACH}},
{"6", []uint32{unix.NFT_REJECT_TCP_RST}},
{"6,17", []uint32{unix.NFT_REJECT_TCP_RST, unix.NFT_REJECT_ICMPX_UNREACH}},
}
for _, tt := range tests {
t.Run(tt.proto, func(t *testing.T) {
state, err := NewCompiler(listCfg(func(c *config.Config) {
c.Rules = []config.Rule{{Action: config.RuleReject, Source: "net", Dest: "fw", Proto: tt.proto, DPort: config.PortSpec{"53"}}}
})).Compile()
if err != nil {
t.Fatalf("Compile() error: %v", err)
}
rules := taggedRules(state, "input", "rule:0")
if len(rules) != len(tt.want) {
t.Fatalf("got %d rules, want %d", len(rules), len(tt.want))
}
for i, r := range rules {
rej, ok := r.Exprs[len(r.Exprs)-1].(*expr.Reject)
if !ok {
t.Fatalf("rule %d: last expr %T, want *expr.Reject", i, r.Exprs[len(r.Exprs)-1])
}
if rej.Type != tt.want[i] {
t.Errorf("rule %d: reject type %d, want %d", i, rej.Type, tt.want[i])
}
}
})
}
}
func TestNegatedAddressList(t *testing.T) {
exprs, err := matchDestCIDR("!192.0.2.1,198.51.100.1")
if err != nil {
t.Fatalf("matchDestCIDR error: %v", err)
}
if len(exprs) != 4 {
t.Fatalf("expected 4 exprs, got %d", len(exprs))
}
for _, i := range []int{1, 3} {
if exprs[i].(*expr.Cmp).Op != expr.CmpOpNeq {
t.Errorf("expr %d should be CmpOpNeq", i)
}
}
}
func TestCompile_ListErrors(t *testing.T) {
tests := []struct {
name string
rule config.Rule
}{
{"invalid port in list", config.Rule{Proto: "tcp", DPort: config.PortSpec{"80,abc"}}},
{"trailing empty proto", config.Rule{Proto: "tcp,", DPort: config.PortSpec{"80"}}},
{"leading empty proto", config.Rule{Proto: ",udp", DPort: config.PortSpec{"80"}}},
{"empty port element", config.Rule{Proto: "tcp", DPort: config.PortSpec{"80,"}}},
{"unknown icmp type", config.Rule{Proto: "icmp", DPort: config.PortSpec{"bogus"}}},
{"unknown icmp type with code", config.Rule{Proto: "icmp", DPort: config.PortSpec{"bogus/0"}}},
{"invalid icmp code", config.Rule{Proto: "icmp", DPort: config.PortSpec{"destination-unreachable/x"}}},
{"icmp code out of range", config.Rule{Proto: "icmp", DPort: config.PortSpec{"3/256"}}},
{"unknown proto in list", config.Rule{Proto: "tcp,udpp", DPort: config.PortSpec{"53"}}},
{"proto number out of range", config.Rule{Proto: "256"}},
{"unknown proto name", config.Rule{Proto: "bogus"}},
{"dport with ospf", config.Rule{Proto: "ospf", DPort: config.PortSpec{"80"}}},
{"sport with gre", config.Rule{Proto: "gre", SPort: config.PortSpec{"80"}}},
{"dport with icmp in proto list", config.Rule{Proto: "icmp,tcp", DPort: config.PortSpec{"80"}}},
{"ratelimit with port list", config.Rule{Proto: "tcp", DPort: config.PortSpec{"80,443"}, RateLimit: "10/sec"}},
{"connlimit with proto list", config.Rule{Proto: "tcp,udp", DPort: config.PortSpec{"53"}, ConnLimit: "10"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := tt.rule
r.Action, r.Source, r.Dest = config.RuleAccept, "net", "fw"
_, err := NewCompiler(listCfg(func(c *config.Config) { c.Rules = []config.Rule{r} })).Compile()
if err == nil {
t.Fatal("Compile() succeeded, want error")
}
})
}
}
func TestCompile_ICMPList(t *testing.T) {
state, err := NewCompiler(listCfg(func(c *config.Config) {
c.Rules = []config.Rule{{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "icmp", DPort: config.PortSpec{"echo-request,echo-reply", "destination-unreachable/4"}}}
})).Compile()
if err != nil {
t.Fatalf("Compile() error: %v", err)
}
var got [][]byte
for _, r := range taggedRules(state, "input", "rule:0") {
var tc []byte
for i, e := range r.Exprs {
if p, ok := e.(*expr.Payload); ok && p.Base == expr.PayloadBaseTransportHeader && p.Len == 1 {
tc = append(tc, r.Exprs[i+1].(*expr.Cmp).Data[0])
}
}
got = append(got, tc)
}
want := [][]byte{{8}, {0}, {3, 4}}
if !reflect.DeepEqual(got, want) {
t.Errorf("icmp type/code per rule = %v, want %v", got, want)
}
}
func TestCompile_ColonRanges(t *testing.T) {
tests := []struct {
name string
mod func(*config.Config)
chain string
tag string
offset uint32
}{
{"rule sport", func(c *config.Config) {
c.Rules = []config.Rule{{Action: config.RuleAccept, Source: "net", Dest: "fw", Proto: "tcp", SPort: config.PortSpec{"1024:2048"}}}
}, "input", "rule:0", 0},
{"snat sport", func(c *config.Config) {
c.SNAT = []config.SNATRule{{Action: config.SNATMasquerade, Dest: "eth0", Proto: "udp", SPort: config.PortSpec{"1024:2048"}}}
}, "postrouting", "snat:0", 0},
{"snat dport", func(c *config.Config) {
c.SNAT = []config.SNATRule{{Action: config.SNATMasquerade, Dest: "eth0", Proto: "tcp", DPort: config.PortSpec{"1024:2048"}}}
}, "postrouting", "snat:0", 2},
{"conntrack dport", func(c *config.Config) {
c.Conntrack = []config.ConntrackRule{{Action: config.ConntrackNoTrack, Proto: "udp", DPort: config.PortSpec{"1024:2048"}}}
}, "prerouting", "conntrack:0:prerouting", 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
state, err := NewCompiler(listCfg(tt.mod)).Compile()
if err != nil {
t.Fatalf("Compile() error: %v", err)
}
rules := taggedRules(state, tt.chain, tt.tag)
if len(rules) != 1 {
t.Fatalf("got %d %s rules, want 1", len(rules), tt.tag)
}
var got []string
ex := rules[0].Exprs
for i, e := range ex {
if p, ok := e.(*expr.Payload); ok && p.Base == expr.PayloadBaseTransportHeader && p.Offset == tt.offset && p.Len == 2 && i+2 < len(ex) {
lo, hi := ex[i+1].(*expr.Cmp), ex[i+2].(*expr.Cmp)
got = append(got, fmt.Sprintf("%d>=%d,%d<=%d", lo.Op, binary.BigEndian.Uint16(lo.Data), hi.Op, binary.BigEndian.Uint16(hi.Data)))
}
}
want := []string{fmt.Sprintf("%d>=1024,%d<=2048", expr.CmpOpGte, expr.CmpOpLte)}
if !reflect.DeepEqual(got, want) {
t.Errorf("range match = %v, want %v", got, want)
}
})
}
}
func TestMatchOrigDest_FamilyGuard(t *testing.T) {
for addr, want := range map[string]byte{
"203.0.113.5": unix.NFPROTO_IPV4,
"!203.0.113.0/24,192.0.2.1": unix.NFPROTO_IPV4,
"2001:db8::5": unix.NFPROTO_IPV6,
} {
e, err := matchOrigDest(addr)
if err != nil {
t.Fatalf("%s: %v", addr, err)
}
if m, ok := e[0].(*expr.Meta); !ok || m.Key != expr.MetaKeyNFPROTO || e[1].(*expr.Cmp).Data[0] != want {
t.Errorf("%s: missing nfproto %d guard: %v", addr, want, e[:2])
}
}
if _, err := matchOrigDest("!203.0.113.5,2001:db8::5"); err == nil {
t.Error("mixed IPv4/IPv6 origdest: want error")
}
}
func TestCompile_OrigDestForwardRejected(t *testing.T) {
cfg := &config.Config{
Settings: config.Settings{TableName: "test", AddressFamily: config.FamilyINET},
Zones: map[string]config.Zone{"fw": {Type: config.ZoneFirewall}, "net": {Type: config.ZoneIP}, "svr": {Type: config.ZoneIP}},
Interfaces: []config.Interface{{Zone: "net", Interface: "eth0"}, {Zone: "svr", Interface: "eth2"}},
Rules: []config.Rule{{Action: config.RuleAccept, Source: "net", Dest: "svr", Proto: "tcp", DPort: config.PortSpec{"80"}, OrigDest: "203.0.113.5"}},
PortGroups: make(map[string]config.PortGroup),
}
_, err := NewCompiler(cfg).Compile()
if err == nil || !strings.Contains(err.Error(), "not supported yet") {
t.Fatalf("Compile() error = %v, want forwarded ORIGDEST rejection", err)
}
}
+68 -44
View File
@@ -2,6 +2,8 @@ package nftables
import (
"fmt"
"reflect"
"sort"
"strings"
"github.com/google/nftables/expr"
@@ -12,6 +14,8 @@ type ManagedRule struct {
Handle uint64
Exprs []expr.Any
Tag string
// Before is the handle of the existing rule an added rule is inserted ahead of; 0 appends.
Before uint64
}
type FirewallState struct {
@@ -32,7 +36,11 @@ func (cs *ChangeSet) Summary() string {
if len(cs.Add) > 0 {
fmt.Fprintf(&b, " + %d rule(s) to add\n", len(cs.Add))
for _, r := range cs.Add {
fmt.Fprintf(&b, " + [%s] %s\n", r.Chain, r.Tag)
if r.Before != 0 {
fmt.Fprintf(&b, " + [%s] %s (before handle %d)\n", r.Chain, r.Tag, r.Before)
} else {
fmt.Fprintf(&b, " + [%s] %s\n", r.Chain, r.Tag)
}
}
}
if len(cs.Remove) > 0 {
@@ -44,66 +52,82 @@ func (cs *ChangeSet) Summary() string {
return b.String()
}
// Rules are first-match, so order matters: keep the common prefix and suffix of
// each chain, replace the middle, and insert the new rules before the first kept
// suffix rule (or append when there is none).
func computeDiff(current, desired *FirewallState) *ChangeSet {
cs := &ChangeSet{}
currentByTag := make(map[string][]ManagedRule)
for _, rules := range current.Rules {
for _, r := range rules {
chains := make([]string, 0, len(current.Rules)+len(desired.Rules))
for c := range current.Rules {
chains = append(chains, c)
}
for c := range desired.Rules {
if _, ok := current.Rules[c]; !ok {
chains = append(chains, c)
}
}
sort.Strings(chains)
for _, chain := range chains {
var cur []ManagedRule
for _, r := range current.Rules[chain] {
if r.Tag != "" {
currentByTag[r.Tag] = append(currentByTag[r.Tag], r)
cur = append(cur, r)
}
}
}
want := desired.Rules[chain]
desiredByTag := make(map[string][]ManagedRule)
for _, rules := range desired.Rules {
for _, r := range rules {
desiredByTag[r.Tag] = append(desiredByTag[r.Tag], r)
pre := 0
for pre < len(cur) && pre < len(want) && ruleEqual(cur[pre], want[pre]) {
pre++
}
suf := 0
for suf < len(cur)-pre && suf < len(want)-pre &&
ruleEqual(cur[len(cur)-1-suf], want[len(want)-1-suf]) {
suf++
}
}
for tag, desiredRules := range desiredByTag {
currentRules, exists := currentByTag[tag]
if !exists {
cs.Add = append(cs.Add, desiredRules...)
continue
var before uint64
if suf > 0 {
before = cur[len(cur)-suf].Handle
}
if !rulesMatch(currentRules, desiredRules) {
cs.Remove = append(cs.Remove, currentRules...)
cs.Add = append(cs.Add, desiredRules...)
}
}
for tag, currentRules := range currentByTag {
if _, exists := desiredByTag[tag]; !exists {
cs.Remove = append(cs.Remove, currentRules...)
cs.Remove = append(cs.Remove, cur[pre:len(cur)-suf]...)
for _, r := range want[pre : len(want)-suf] {
r.Before = before
cs.Add = append(cs.Add, r)
}
}
return cs
}
func rulesMatch(a, b []ManagedRule) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i].Chain != b[i].Chain {
return false
}
if !exprsEqual(a[i].Exprs, b[i].Exprs) {
return false
}
}
return true
func ruleEqual(a, b ManagedRule) bool {
return a.Chain == b.Chain && a.Tag == b.Tag && reflect.DeepEqual(a.Exprs, b.Exprs)
}
func exprsEqual(a, b []expr.Any) bool {
if len(a) != len(b) {
return false
// restoreChangeSet replaces every managed rule in current with the snapshot's,
// in snapshot order, so a restore cannot reorder rules.
func restoreChangeSet(current, snap *FirewallState) *ChangeSet {
cs := &ChangeSet{}
for _, rules := range current.Rules {
for _, r := range rules {
if r.Tag != "" {
cs.Remove = append(cs.Remove, r)
}
}
}
as := fmt.Sprintf("%v", a)
bs := fmt.Sprintf("%v", b)
return as == bs
chains := make([]string, 0, len(snap.Rules))
for c := range snap.Rules {
chains = append(chains, c)
}
sort.Strings(chains)
for _, c := range chains {
for _, r := range snap.Rules[c] {
if r.Tag != "" {
cs.Add = append(cs.Add, r)
}
}
}
return cs
}
+66
View File
@@ -0,0 +1,66 @@
package nftables
import (
"reflect"
"testing"
"github.com/google/nftables/expr"
)
func TestRestoreChangeSet(t *testing.T) {
accept := []expr.Any{&expr.Verdict{Kind: expr.VerdictAccept}}
drop := []expr.Any{&expr.Verdict{Kind: expr.VerdictDrop}}
snap := &FirewallState{Rules: map[string][]ManagedRule{
"input": {
{Chain: "input", Tag: "ssh", Exprs: accept, Handle: 4},
{Chain: "input", Tag: "web", Exprs: accept, Handle: 5},
{Chain: "input", Tag: "", Exprs: drop, Handle: 6},
},
"forward": {{Chain: "forward", Tag: "fwd", Exprs: accept, Handle: 7}},
}}
current := &FirewallState{Rules: map[string][]ManagedRule{
"input": {
{Chain: "input", Tag: "web", Exprs: accept, Handle: 10},
{Chain: "input", Tag: "ssh", Exprs: drop, Handle: 11},
{Chain: "input", Tag: "", Exprs: drop, Handle: 12},
},
}}
cs := restoreChangeSet(current, snap)
var removed []uint64
for _, r := range cs.Remove {
removed = append(removed, r.Handle)
}
if len(removed) != 2 || removed[0] != 10 || removed[1] != 11 {
t.Errorf("expected managed handles [10 11] removed, untagged kept; got %v", removed)
}
var added []string
for _, r := range cs.Add {
added = append(added, r.Tag)
}
want := []string{"fwd", "ssh", "web"}
if len(added) != len(want) {
t.Fatalf("added %v, want %v", added, want)
}
for i := range want {
if added[i] != want[i] {
t.Fatalf("added %v, want %v (snapshot order per chain)", added, want)
}
}
if !reflect.DeepEqual(cs.Add[1].Exprs, accept) {
t.Error("ssh not restored to its snapshot exprs")
}
}
func TestRestoreChangeSetEmptySnapshotRemovesAll(t *testing.T) {
current := &FirewallState{Rules: map[string][]ManagedRule{
"input": {{Chain: "input", Tag: "x", Handle: 1}},
}}
cs := restoreChangeSet(current, &FirewallState{Rules: map[string][]ManagedRule{}})
if len(cs.Remove) != 1 || len(cs.Add) != 0 {
t.Errorf("expected 1 remove 0 add, got %d/%d", len(cs.Remove), len(cs.Add))
}
}
+102 -21
View File
@@ -28,7 +28,8 @@ func (e *Engine) ensureTable() *nftables.Table {
})
}
func (e *Engine) ensureChains(table *nftables.Table) map[string]*nftables.Chain {
// ensureChains declares the base chains; policies overrides their default policy.
func (e *Engine) ensureChains(table *nftables.Table, policies map[string]nftables.ChainPolicy) map[string]*nftables.Chain {
chains := map[string]*nftables.Chain{
"input": {
Name: "input",
@@ -71,6 +72,9 @@ func (e *Engine) ensureChains(table *nftables.Table) map[string]*nftables.Chain
}
for name, chain := range chains {
if p, ok := policies[name]; ok {
chain.Policy = policyPtr(p)
}
chains[name] = e.conn.AddChain(chain)
}
return chains
@@ -93,8 +97,12 @@ func (e *Engine) Plan() (*ChangeSet, error) {
}
func (e *Engine) Apply(changes *ChangeSet) error {
return e.apply(changes, nil)
}
func (e *Engine) apply(changes *ChangeSet, policies map[string]nftables.ChainPolicy) error {
table := e.ensureTable()
chains := e.ensureChains(table)
chains := e.ensureChains(table, policies)
for _, r := range changes.Remove {
e.conn.DelRule(&nftables.Rule{
@@ -109,12 +117,18 @@ func (e *Engine) Apply(changes *ChangeSet) error {
if !ok {
return fmt.Errorf("unknown chain %q", r.Chain)
}
e.conn.AddRule(&nftables.Rule{
rule := &nftables.Rule{
Table: table,
Chain: chain,
Exprs: r.Exprs,
UserData: []byte(r.Tag),
})
}
if r.Before != 0 {
rule.Position = r.Before
e.conn.InsertRule(rule)
} else {
e.conn.AddRule(rule)
}
}
return e.conn.Flush()
@@ -135,31 +149,32 @@ func (e *Engine) Flush() error {
return nil
}
func (e *Engine) findTable() (*nftables.Table, error) {
tables, err := e.conn.ListTables()
if err != nil {
return nil, fmt.Errorf("listing tables: %w", err)
}
for _, t := range tables {
if t.Name == e.cfg.Settings.TableName && t.Family == nftables.TableFamilyINet {
return t, nil
}
}
return nil, nil
}
func (e *Engine) readCurrentState() (*FirewallState, error) {
state := &FirewallState{
Rules: make(map[string][]ManagedRule),
}
tables, err := e.conn.ListTables()
if err != nil {
return state, nil
}
var ourTable *nftables.Table
for _, t := range tables {
if t.Name == e.cfg.Settings.TableName && t.Family == nftables.TableFamilyINet {
ourTable = t
break
}
}
if ourTable == nil {
return state, nil
ourTable, err := e.findTable()
if err != nil || ourTable == nil {
return state, err
}
chains, err := e.conn.ListChainsOfTableFamily(nftables.TableFamilyINet)
if err != nil {
return state, nil
return nil, fmt.Errorf("listing chains: %w", err)
}
for _, chain := range chains {
@@ -168,7 +183,7 @@ func (e *Engine) readCurrentState() (*FirewallState, error) {
}
rules, err := e.conn.GetRules(ourTable, chain)
if err != nil {
continue
return nil, fmt.Errorf("listing rules of %s: %w", chain.Name, err)
}
for _, rule := range rules {
state.Rules[chain.Name] = append(state.Rules[chain.Name], ManagedRule{
@@ -183,6 +198,72 @@ func (e *Engine) readCurrentState() (*FirewallState, error) {
return state, nil
}
// Snapshot is the tomswall table as captured live, serialisable so a revert
// survives the process that took it.
type Snapshot struct {
Table string `json:"table"`
Present bool `json:"present"`
Policies map[string]nftables.ChainPolicy `json:"policies,omitempty"`
Rules map[string][]SnapshotRule `json:"rules,omitempty"`
}
// SnapshotRule is a managed rule with its expressions in netlink wire format.
type SnapshotRule struct {
Tag string `json:"tag"`
Exprs [][]byte `json:"exprs"`
}
// Snapshot captures the live tomswall table so Restore can roll back to it.
func (e *Engine) Snapshot() (*Snapshot, error) {
snap := &Snapshot{Table: e.cfg.Settings.TableName}
t, err := e.findTable()
if err != nil || t == nil {
return snap, err
}
snap.Present = true
chains, err := e.conn.ListChainsOfTableFamily(nftables.TableFamilyINet)
if err != nil {
return nil, fmt.Errorf("listing chains: %w", err)
}
snap.Policies = make(map[string]nftables.ChainPolicy)
for _, c := range chains {
if c.Table.Name == snap.Table && c.Policy != nil {
snap.Policies[c.Name] = *c.Policy
}
}
state, err := e.readCurrentState()
if err != nil {
return nil, err
}
snap.Rules, err = encodeState(state)
if err != nil {
return nil, err
}
return snap, nil
}
// Restore atomically returns the tomswall table to the snapshot: rule order
// and chain policies included, or removed if it was absent.
func (e *Engine) Restore(s *Snapshot) error {
if s.Table != e.cfg.Settings.TableName {
return fmt.Errorf("snapshot is of table %q, engine manages %q", s.Table, e.cfg.Settings.TableName)
}
if !s.Present {
return e.Flush()
}
want, err := decodeState(s.Rules)
if err != nil {
return err
}
current, err := e.readCurrentState()
if err != nil {
return err
}
return e.apply(restoreChangeSet(current, want), s.Policies)
}
func policyPtr(p nftables.ChainPolicy) *nftables.ChainPolicy {
return &p
}
+119
View File
@@ -0,0 +1,119 @@
package nftables
import (
"encoding/binary"
"fmt"
"github.com/google/nftables"
"github.com/google/nftables/expr"
"github.com/mdlayher/netlink"
"golang.org/x/sys/unix"
)
const inet = byte(nftables.TableFamilyINet)
// exprByName mirrors the expression types google/nftables can parse back from the kernel.
var exprByName = map[string]func() expr.Any{
"ct": func() expr.Any { return &expr.Ct{} },
"range": func() expr.Any { return &expr.Range{} },
"meta": func() expr.Any { return &expr.Meta{} },
"cmp": func() expr.Any { return &expr.Cmp{} },
"counter": func() expr.Any { return &expr.Counter{} },
"objref": func() expr.Any { return &expr.Objref{} },
"payload": func() expr.Any { return &expr.Payload{} },
"lookup": func() expr.Any { return &expr.Lookup{} },
"immediate": func() expr.Any { return &expr.Immediate{} },
"bitwise": func() expr.Any { return &expr.Bitwise{} },
"redir": func() expr.Any { return &expr.Redir{} },
"nat": func() expr.Any { return &expr.NAT{} },
"limit": func() expr.Any { return &expr.Limit{} },
"quota": func() expr.Any { return &expr.Quota{} },
"dynset": func() expr.Any { return &expr.Dynset{} },
"log": func() expr.Any { return &expr.Log{} },
"exthdr": func() expr.Any { return &expr.Exthdr{} },
"connlimit": func() expr.Any { return &expr.Connlimit{} },
"queue": func() expr.Any { return &expr.Queue{} },
"flow_offload": func() expr.Any { return &expr.FlowOffload{} },
"reject": func() expr.Any { return &expr.Reject{} },
"masq": func() expr.Any { return &expr.Masq{} },
"hash": func() expr.Any { return &expr.Hash{} },
"notrack": func() expr.Any { return &expr.Notrack{} },
}
func encodeState(state *FirewallState) (map[string][]SnapshotRule, error) {
out := make(map[string][]SnapshotRule, len(state.Rules))
for chain, rules := range state.Rules {
for _, r := range rules {
sr := SnapshotRule{Tag: r.Tag}
for _, e := range r.Exprs {
b, err := expr.Marshal(inet, e)
if err != nil {
return nil, fmt.Errorf("encoding %s rule %q: %w", chain, r.Tag, err)
}
sr.Exprs = append(sr.Exprs, b)
}
out[chain] = append(out[chain], sr)
}
}
return out, nil
}
func decodeState(rules map[string][]SnapshotRule) (*FirewallState, error) {
state := &FirewallState{Rules: make(map[string][]ManagedRule, len(rules))}
for chain, rs := range rules {
for _, sr := range rs {
r := ManagedRule{Chain: chain, Tag: sr.Tag}
for _, b := range sr.Exprs {
e, err := decodeExpr(b)
if err != nil {
return nil, fmt.Errorf("decoding %s rule %q: %w", chain, sr.Tag, err)
}
r.Exprs = append(r.Exprs, e)
}
state.Rules[chain] = append(state.Rules[chain], r)
}
}
return state, nil
}
// decodeExpr reverses expr.Marshal, as google/nftables does when reading rules.
func decodeExpr(b []byte) (expr.Any, error) {
ad, err := netlink.NewAttributeDecoder(b)
if err != nil {
return nil, err
}
ad.ByteOrder = binary.BigEndian
var name string
var data []byte
for ad.Next() {
switch ad.Type() {
case unix.NFTA_EXPR_NAME:
name = ad.String()
case unix.NFTA_EXPR_DATA:
data = ad.Bytes()
}
}
if err := ad.Err(); err != nil {
return nil, err
}
newExpr, ok := exprByName[name]
if !ok {
return nil, fmt.Errorf("unsupported expression %q", name)
}
e := newExpr()
if name == "notrack" {
return e, nil
}
if err := expr.Unmarshal(inet, data, e); err != nil {
return nil, err
}
// A verdict is an immediate into the verdict register with no data.
if imm, ok := e.(*expr.Immediate); ok && imm.Register == unix.NFT_REG_VERDICT && len(imm.Data) == 0 {
v := &expr.Verdict{}
if err := expr.Unmarshal(inet, data, v); err != nil {
return nil, err
}
return v, nil
}
return e, nil
}
+245
View File
@@ -0,0 +1,245 @@
package nftables
import (
"bytes"
"encoding/binary"
"encoding/json"
"reflect"
"testing"
"github.com/google/nftables"
"github.com/google/nftables/expr"
"github.com/mdlayher/netlink"
"golang.org/x/sys/unix"
"git.unkin.net/unkin/tomswall/internal/config"
)
func TestSnapshotRulesRoundTrip(t *testing.T) {
exprs := []expr.Any{
&expr.Meta{Key: expr.MetaKeyL4PROTO, Register: 1},
&expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{unix.IPPROTO_TCP}},
&expr.Payload{DestRegister: 1, Base: expr.PayloadBaseTransportHeader, Offset: 2, Len: 2},
&expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{0, 22}},
&expr.Ct{Register: 1, Key: expr.CtKeySTATE},
&expr.Notrack{},
&expr.Verdict{Kind: expr.VerdictAccept},
}
state := &FirewallState{Rules: map[string][]ManagedRule{
"input": {{Chain: "input", Tag: "ssh", Exprs: exprs}, {Chain: "input", Tag: "drop", Exprs: []expr.Any{&expr.Verdict{Kind: expr.VerdictDrop}}}},
}}
rules, err := encodeState(state)
if err != nil {
t.Fatal(err)
}
b, err := json.Marshal(&Snapshot{Table: "tomswall", Present: true, Rules: rules,
Policies: map[string]nftables.ChainPolicy{"input": nftables.ChainPolicyAccept}})
if err != nil {
t.Fatal(err)
}
var snap Snapshot
if err := json.Unmarshal(b, &snap); err != nil {
t.Fatal(err)
}
if snap.Policies["input"] != nftables.ChainPolicyAccept {
t.Errorf("policy lost: %v", snap.Policies)
}
got, err := decodeState(snap.Rules)
if err != nil {
t.Fatal(err)
}
in := got.Rules["input"]
if len(in) != 2 || in[0].Tag != "ssh" || in[1].Tag != "drop" {
t.Fatalf("rules/order lost: %+v", in)
}
if !reflect.DeepEqual(in[0].Exprs, exprs) {
t.Errorf("exprs changed:\n got %#v\nwant %#v", in[0].Exprs, exprs)
}
if _, ok := in[1].Exprs[0].(*expr.Verdict); !ok {
t.Errorf("verdict decoded as %T", in[1].Exprs[0])
}
}
func TestEnsureChainsPolicyOverride(t *testing.T) {
e := testEngine(t, nil)
chains := e.ensureChains(e.ensureTable(), map[string]nftables.ChainPolicy{"input": nftables.ChainPolicyAccept})
if *chains["input"].Policy != nftables.ChainPolicyAccept {
t.Error("input policy not overridden")
}
if *chains["forward"].Policy != nftables.ChainPolicyDrop {
t.Error("forward policy should keep its default")
}
}
func TestSnapshotAndRestoreAbsentTable(t *testing.T) {
tablePresent := false
var sent []netlink.HeaderType
e := testEngine(t, func(req []netlink.Message) ([]netlink.Message, error) {
for _, m := range req {
sent = append(sent, m.Header.Type)
if m.Header.Type == nftType(unix.NFT_MSG_GETTABLE) && tablePresent {
data := []byte{inet, 0, 0, 0}
attrs, _ := netlink.MarshalAttributes([]netlink.Attribute{{Type: unix.NFTA_TABLE_NAME, Data: []byte("tomswall\x00")}})
return []netlink.Message{{Header: netlink.Header{Type: nftType(unix.NFT_MSG_NEWTABLE), Sequence: m.Header.Sequence}, Data: append(data, attrs...)}}, nil
}
}
return nil, nil
})
snap, err := e.Snapshot()
if err != nil {
t.Fatal(err)
}
if snap.Present || snap.Table != "tomswall" {
t.Fatalf("want absent tomswall snapshot, got %+v", snap)
}
// The try created the table; restoring the absent snapshot deletes it.
tablePresent = true
sent = nil
if err := e.Restore(snap); err != nil {
t.Fatal(err)
}
deleted := false
for _, ht := range sent {
deleted = deleted || ht == nftType(unix.NFT_MSG_DELTABLE)
}
if !deleted {
t.Errorf("table not deleted; sent %v", sent)
}
}
func TestRestorePresentTable(t *testing.T) {
want := []SnapshotRule{}
for _, r := range []ManagedRule{
{Tag: "ssh", Exprs: []expr.Any{&expr.Ct{Register: 1, Key: expr.CtKeySTATE}, &expr.Verdict{Kind: expr.VerdictAccept}}},
{Tag: "drop", Exprs: []expr.Any{&expr.Verdict{Kind: expr.VerdictDrop}}},
} {
enc, err := encodeState(&FirewallState{Rules: map[string][]ManagedRule{"input": {r}}})
if err != nil {
t.Fatal(err)
}
want = append(want, enc["input"]...)
}
snap := &Snapshot{Table: "tomswall", Present: true,
Policies: map[string]nftables.ChainPolicy{"input": nftables.ChainPolicyAccept},
Rules: map[string][]SnapshotRule{"input": want}}
// Live state: the tried ruleset left one managed rule (handle 7) in input.
attrs := func(a ...netlink.Attribute) []byte {
b, err := netlink.MarshalAttributes(a)
if err != nil {
t.Fatal(err)
}
return append([]byte{inet, 0, 0, 0}, b...)
}
handle := make([]byte, 8)
binary.BigEndian.PutUint64(handle, 7)
var batch []netlink.Message
e := testEngine(t, func(req []netlink.Message) ([]netlink.Message, error) {
if len(req) == 0 {
return nil, nil
}
reply := func(msg int, data []byte) ([]netlink.Message, error) {
return []netlink.Message{{Header: netlink.Header{Type: nftType(msg), Sequence: req[0].Header.Sequence}, Data: data}}, nil
}
switch req[0].Header.Type {
case nftType(unix.NFT_MSG_GETTABLE):
return reply(unix.NFT_MSG_NEWTABLE, attrs(netlink.Attribute{Type: unix.NFTA_TABLE_NAME, Data: []byte("tomswall\x00")}))
case nftType(unix.NFT_MSG_GETCHAIN):
return reply(unix.NFT_MSG_NEWCHAIN, attrs(
netlink.Attribute{Type: unix.NFTA_CHAIN_TABLE, Data: []byte("tomswall\x00")},
netlink.Attribute{Type: unix.NFTA_CHAIN_NAME, Data: []byte("input\x00")}))
case nftType(unix.NFT_MSG_GETRULE):
return reply(unix.NFT_MSG_NEWRULE, attrs(
netlink.Attribute{Type: unix.NFTA_RULE_TABLE, Data: []byte("tomswall\x00")},
netlink.Attribute{Type: unix.NFTA_RULE_CHAIN, Data: []byte("input\x00")},
netlink.Attribute{Type: unix.NFTA_RULE_HANDLE, Data: handle},
netlink.Attribute{Type: unix.NFTA_RULE_USERDATA, Data: []byte("tried")}))
}
batch = append(batch, req...)
return nil, nil
})
if err := e.Restore(snap); err != nil {
t.Fatal(err)
}
var deleted []uint64
var added []SnapshotRule
policy := map[string]uint32{}
for _, m := range batch {
ad, err := netlink.NewAttributeDecoder(m.Data[4:])
if err != nil {
t.Fatal(err)
}
ad.ByteOrder = binary.BigEndian
var name string
var r SnapshotRule
var h uint64
var pol *uint32
for ad.Next() {
switch {
case m.Header.Type == nftType(unix.NFT_MSG_NEWCHAIN) && ad.Type() == unix.NFTA_CHAIN_NAME:
name = ad.String()
case m.Header.Type == nftType(unix.NFT_MSG_NEWCHAIN) && ad.Type() == unix.NFTA_CHAIN_POLICY:
v := ad.Uint32()
pol = &v
case m.Header.Type == nftType(unix.NFT_MSG_DELRULE) && ad.Type() == unix.NFTA_RULE_HANDLE:
h = ad.Uint64()
case m.Header.Type == nftType(unix.NFT_MSG_NEWRULE) && ad.Type() == unix.NFTA_RULE_USERDATA:
r.Tag = string(ad.Bytes())
case m.Header.Type == nftType(unix.NFT_MSG_NEWRULE) && ad.Type() == unix.NFTA_RULE_EXPRESSIONS:
ad.Nested(func(nad *netlink.AttributeDecoder) error {
for nad.Next() {
r.Exprs = append(r.Exprs, bytes.Clone(nad.Bytes()))
}
return nil
})
}
}
switch m.Header.Type {
case nftType(unix.NFT_MSG_NEWCHAIN):
if pol != nil {
policy[name] = *pol
}
case nftType(unix.NFT_MSG_DELRULE):
deleted = append(deleted, h)
case nftType(unix.NFT_MSG_NEWRULE):
added = append(added, r)
}
}
if !reflect.DeepEqual(deleted, []uint64{7}) {
t.Errorf("deleted handles %v, want [7]", deleted)
}
if !reflect.DeepEqual(added, want) {
t.Errorf("restored rules differ from snapshot:\n got %+v\nwant %+v", added, want)
}
if policy["input"] != uint32(nftables.ChainPolicyAccept) || policy["forward"] != uint32(nftables.ChainPolicyDrop) {
t.Errorf("chain policies %v: input must be restored to accept, forward keep drop", policy)
}
}
func TestRestoreRejectsOtherTable(t *testing.T) {
e := testEngine(t, nil)
if err := e.Restore(&Snapshot{Table: "other"}); err == nil {
t.Error("expected table mismatch error")
}
}
func nftType(msg int) netlink.HeaderType {
return netlink.HeaderType(unix.NFNL_SUBSYS_NFTABLES<<8 | msg)
}
func testEngine(t *testing.T, dial func([]netlink.Message) ([]netlink.Message, error)) *Engine {
if dial == nil {
dial = func([]netlink.Message) ([]netlink.Message, error) { return nil, nil }
}
conn, err := nftables.New(nftables.WithTestDial(dial))
if err != nil {
t.Fatal(err)
}
return &Engine{cfg: &config.Config{Settings: config.Settings{TableName: "tomswall"}}, conn: conn}
}
+200
View File
@@ -0,0 +1,200 @@
// Package tryapply keeps the state of a pending 'tomswall try' on disk so the
// revert survives the try process, backed by a transient systemd timer.
package tryapply
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"git.unkin.net/unkin/tomswall/internal/config"
"git.unkin.net/unkin/tomswall/internal/nftables"
)
// Unit is the transient systemd unit that reverts an unconfirmed try.
const Unit = "tomswall-try-revert"
var (
// Dir holds the lock and the pending snapshot.
Dir = "/var/lib/tomswall"
// run executes a systemd command; replaced in tests.
run = func(name string, args ...string) error {
if out, err := exec.Command(name, args...).CombinedOutput(); err != nil {
return fmt.Errorf("%s %s: %w: %s", name, strings.Join(args, " "), err, strings.TrimSpace(string(out)))
}
return nil
}
// restore rolls the live table back to a snapshot; replaced in tests.
restore = func(s *nftables.Snapshot) error {
engine, err := nftables.NewEngine(&config.Config{Settings: config.Settings{TableName: s.Table}})
if err != nil {
return err
}
return engine.Restore(s)
}
)
// ErrPending means a try awaits confirmation; nothing else may apply meanwhile.
var ErrPending = errors.New("a 'tomswall try' is pending; run 'tomswall confirm' to keep it or 'tomswall revert' to restore the previous ruleset (also the recovery if an automatic revert failed)")
type pending struct {
ID string `json:"id"`
PID int `json:"pid"`
Snapshot *nftables.Snapshot `json:"snapshot"`
}
func snapshotPath() string { return filepath.Join(Dir, "try-snapshot.json") }
// Acquire takes the exclusive try lock, failing with ErrPending while a try is unconfirmed.
func Acquire() (unlock func(), err error) {
unlock, err = lock()
if err != nil {
return nil, err
}
if _, err := os.Stat(snapshotPath()); err == nil {
unlock()
return nil, ErrPending
}
return unlock, nil
}
func lock() (func(), error) {
if err := os.MkdirAll(Dir, 0o755); err != nil {
return nil, err
}
f, err := os.OpenFile(filepath.Join(Dir, "try.lock"), os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return nil, err
}
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
f.Close()
return nil, fmt.Errorf("locking %s: %w", f.Name(), err)
}
return func() { f.Close() }, nil
}
// Arm persists snap and schedules an out-of-process revert after delay,
// returning the try ID that scopes later reverts to this try.
// The caller must hold the lock from Acquire.
func Arm(snap *nftables.Snapshot, pid int, delay time.Duration) (string, error) {
raw := make([]byte, 8)
if _, err := rand.Read(raw); err != nil {
return "", err
}
id := hex.EncodeToString(raw)
b, err := json.Marshal(pending{ID: id, PID: pid, Snapshot: snap})
if err != nil {
return "", err
}
f, err := os.CreateTemp(Dir, ".try-snapshot-*")
if err != nil {
return "", err
}
defer os.Remove(f.Name())
if _, err := f.Write(b); err != nil {
f.Close()
return "", err
}
if err := f.Sync(); err != nil {
f.Close()
return "", err
}
if err := f.Close(); err != nil {
return "", err
}
if err := os.Rename(f.Name(), snapshotPath()); err != nil {
return "", err
}
exe, err := os.Executable()
if err != nil {
return "", discardWith(err)
}
_ = disarm() // a leftover timer from an earlier try would block the unit name
if err := run("systemd-run", "--quiet", "--collect", "--unit", Unit,
fmt.Sprintf("--on-active=%ds", int(delay.Round(time.Second).Seconds())), exe, "revert", "--id", id); err != nil {
return "", discardWith(fmt.Errorf("arming revert timer: %w", err))
}
return id, nil
}
// Discard drops the pending snapshot and timer without restoring. The caller must hold the lock.
func Discard() error {
_ = disarm()
if err := os.Remove(snapshotPath()); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
func discardWith(err error) error {
if derr := Discard(); derr != nil {
return fmt.Errorf("%w (discarding snapshot: %v)", err, derr)
}
return err
}
func disarm() error {
return run("systemctl", "stop", Unit+".timer")
}
// Confirm keeps the tried ruleset. ok is false when no try was pending, i.e.
// it was already reverted; pid is the waiting try process, if any.
func Confirm() (pid int, ok bool, err error) {
unlock, err := lock()
if err != nil {
return 0, false, err
}
defer unlock()
p, err := load()
if err != nil || p == nil {
return 0, false, err
}
return p.PID, true, Discard()
}
// Revert restores the pending snapshot. A non-empty id only reverts that try,
// so a stale timer cannot undo a newer one. reverted is false when nothing
// matching was pending (already confirmed or reverted). A failed restore keeps
// the snapshot so 'tomswall revert' can retry.
func Revert(id string) (reverted bool, err error) {
unlock, err := lock()
if err != nil {
return false, err
}
defer unlock()
p, err := load()
if err != nil || p == nil || (id != "" && p.ID != id) {
return false, err
}
if err := restore(p.Snapshot); err != nil {
return false, fmt.Errorf("restoring snapshot: %w", err)
}
return true, Discard()
}
func load() (*pending, error) {
b, err := os.ReadFile(snapshotPath())
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
var p pending
if err := json.Unmarshal(b, &p); err != nil {
return nil, fmt.Errorf("parsing %s: %w", snapshotPath(), err)
}
if p.Snapshot == nil {
return nil, fmt.Errorf("%s has no snapshot", snapshotPath())
}
return &p, nil
}
+211
View File
@@ -0,0 +1,211 @@
package tryapply
import (
"errors"
"os"
"reflect"
"strings"
"testing"
"time"
"git.unkin.net/unkin/tomswall/internal/nftables"
)
func setup(t *testing.T) *[]string {
t.Helper()
Dir = t.TempDir()
var cmds []string
orig := run
run = func(name string, args ...string) error {
cmds = append(cmds, name+" "+strings.Join(args, " "))
return nil
}
t.Cleanup(func() { run = orig })
return &cmds
}
func arm(t *testing.T, snap *nftables.Snapshot) string {
t.Helper()
unlock, err := Acquire()
if err != nil {
t.Fatal(err)
}
defer unlock()
id, err := Arm(snap, 4242, 90*time.Second)
if err != nil {
t.Fatal(err)
}
return id
}
func TestArmPersistsSnapshotAndTimer(t *testing.T) {
cmds := setup(t)
snap := &nftables.Snapshot{Table: "tomswall", Present: true,
Rules: map[string][]nftables.SnapshotRule{"input": {{Tag: "ssh", Exprs: [][]byte{{1, 2, 3}}}}}}
id := arm(t, snap)
info, err := os.Stat(snapshotPath())
if err != nil {
t.Fatal(err)
}
if info.Mode().Perm() != 0o600 {
t.Errorf("snapshot mode %v, want 0600", info.Mode().Perm())
}
p, err := load()
if err != nil {
t.Fatal(err)
}
if p.ID != id || id == "" || p.PID != 4242 || !reflect.DeepEqual(p.Snapshot, snap) {
t.Errorf("round trip mismatch: %+v", p)
}
last := (*cmds)[len(*cmds)-1]
if !strings.HasPrefix(last, "systemd-run ") || !strings.Contains(last, "--unit "+Unit) ||
!strings.Contains(last, "--on-active=90s") || !strings.HasSuffix(last, " revert --id "+id) {
t.Errorf("unexpected arm command %q", last)
}
}
func TestAbsentTableSnapshotRoundTrip(t *testing.T) {
setup(t)
arm(t, &nftables.Snapshot{Table: "tomswall"})
p, err := load()
if err != nil {
t.Fatal(err)
}
if p.Snapshot.Present || p.Snapshot.Table != "tomswall" {
t.Errorf("absent table not preserved: %+v", p.Snapshot)
}
}
func TestAcquireRefusesWhilePending(t *testing.T) {
setup(t)
arm(t, &nftables.Snapshot{Table: "tomswall"})
_, err := Acquire()
if !errors.Is(err, ErrPending) {
t.Fatalf("second try: got %v, want ErrPending", err)
}
if !strings.Contains(err.Error(), "tomswall revert") {
t.Errorf("ErrPending does not name the recovery: %v", err)
}
}
func TestArmFailureDiscardsSnapshot(t *testing.T) {
setup(t)
run = func(name string, args ...string) error {
if name == "systemd-run" {
return errors.New("no systemd")
}
return nil
}
unlock, err := Acquire()
if err != nil {
t.Fatal(err)
}
defer unlock()
if _, err := Arm(&nftables.Snapshot{Table: "tomswall"}, 1, time.Minute); err == nil {
t.Fatal("expected arm error")
}
if _, err := os.Stat(snapshotPath()); !os.IsNotExist(err) {
t.Error("snapshot left behind without a revert timer")
}
}
func TestConfirmPendingDisarms(t *testing.T) {
cmds := setup(t)
arm(t, &nftables.Snapshot{Table: "tomswall"})
pid, ok, err := Confirm()
if err != nil || !ok || pid != 4242 {
t.Fatalf("Confirm = %d, %v, %v", pid, ok, err)
}
if _, err := os.Stat(snapshotPath()); !os.IsNotExist(err) {
t.Error("snapshot not removed")
}
if last := (*cmds)[len(*cmds)-1]; last != "systemctl stop "+Unit+".timer" {
t.Errorf("timer not stopped, last command %q", last)
}
unlock, err := Acquire()
if err != nil {
t.Fatalf("new try refused after confirm: %v", err)
}
unlock()
}
func TestConfirmAfterRevertFails(t *testing.T) {
setup(t)
_, ok, err := Confirm()
if err != nil || ok {
t.Fatalf("Confirm with nothing pending = %v, %v; want not ok", ok, err)
}
reverted, err := Revert("")
if err != nil || reverted {
t.Fatalf("Revert with nothing pending = %v, %v", reverted, err)
}
}
func stubRestore(t *testing.T, err error) *[]*nftables.Snapshot {
t.Helper()
var got []*nftables.Snapshot
orig := restore
restore = func(s *nftables.Snapshot) error {
got = append(got, s)
return err
}
t.Cleanup(func() { restore = orig })
return &got
}
func TestRevertPendingRestoresAndDisarms(t *testing.T) {
cmds := setup(t)
restored := stubRestore(t, nil)
snap := &nftables.Snapshot{Table: "tomswall", Present: true,
Rules: map[string][]nftables.SnapshotRule{"input": {{Tag: "ssh", Exprs: [][]byte{{1}}}}}}
id := arm(t, snap)
reverted, err := Revert(id)
if err != nil || !reverted {
t.Fatalf("Revert = %v, %v", reverted, err)
}
if len(*restored) != 1 || !reflect.DeepEqual((*restored)[0], snap) {
t.Errorf("restored %+v, want the armed snapshot", *restored)
}
if _, err := os.Stat(snapshotPath()); !os.IsNotExist(err) {
t.Error("snapshot not removed")
}
if last := (*cmds)[len(*cmds)-1]; last != "systemctl stop "+Unit+".timer" {
t.Errorf("timer not stopped, last command %q", last)
}
}
func TestRevertFailureKeepsSnapshot(t *testing.T) {
setup(t)
boom := errors.New("netlink down")
stubRestore(t, boom)
id := arm(t, &nftables.Snapshot{Table: "tomswall"})
if _, err := Revert(id); !errors.Is(err, boom) {
t.Fatalf("Revert error = %v, want %v", err, boom)
}
if _, err := os.Stat(snapshotPath()); err != nil {
t.Fatalf("snapshot gone after failed revert: %v", err)
}
if _, err := Acquire(); !errors.Is(err, ErrPending) {
t.Errorf("failed revert must keep the try pending, got %v", err)
}
}
func TestRevertStaleIDIgnored(t *testing.T) {
setup(t)
restored := stubRestore(t, nil)
arm(t, &nftables.Snapshot{Table: "tomswall"})
reverted, err := Revert("stale-try")
if err != nil || reverted {
t.Fatalf("stale Revert = %v, %v; want no-op", reverted, err)
}
if len(*restored) != 0 {
t.Error("stale timer restored a newer try's snapshot")
}
if _, err := os.Stat(snapshotPath()); err != nil {
t.Errorf("newer try's snapshot removed: %v", err)
}
}
+58
View File
@@ -0,0 +1,58 @@
---
# nfpm config for building the tomswall RPM.
# Rendered through envsubst (see scripts/build-rpm.sh) then fed to `nfpm pkg`.
name: ${PACKAGE_NAME}
version: ${PACKAGE_VERSION}
release: ${PACKAGE_RELEASE}
arch: ${PACKAGE_ARCH}
platform: ${PACKAGE_PLATFORM}
section: default
priority: extra
description: "${PACKAGE_DESCRIPTION}"
maintainer: ${PACKAGE_MAINTAINER}
homepage: ${PACKAGE_HOMEPAGE}
license: ${PACKAGE_LICENSE}
disable_globbing: false
replaces:
- tomswall
provides:
- tomswall
contents:
- src: dist/tomswall
dst: /usr/sbin/tomswall
file_info:
mode: 0755
owner: root
group: root
# Example configuration (never overwrites an existing tomswall.yaml).
- src: tomswall.example.yaml
dst: /etc/tomswall/tomswall.example.yaml
file_info:
mode: 0644
# systemd unit + environment file for the control-plane agent.
- src: packaging/tomswall-agent.service
dst: /usr/lib/systemd/system/tomswall-agent.service
file_info:
mode: 0644
- src: packaging/tomswall-agent.env
dst: /etc/tomswall/agent.env
type: config|noreplace
file_info:
mode: 0640
# Shell completions (generated by scripts/build-rpm.sh before packaging).
- src: dist/completions/tomswall.bash
dst: /usr/share/bash-completion/completions/tomswall
file_info:
mode: 0644
- src: dist/completions/_tomswall
dst: /usr/share/zsh/site-functions/_tomswall
file_info:
mode: 0644
+12
View File
@@ -0,0 +1,12 @@
# Environment for the tomswall control-plane agent (tomswall-agent.service).
# The agent reads these; flags may also be passed via ExecStart.
# Base URL of the tomswallapi control plane.
TOMSWALL_API_URL=https://tomswallapi.k8s.syd1.au.unkin.net
# Agent bearer token (issued by the control plane / Vault). Keep this file 0640.
TOMSWALL_AGENT_TOKEN=
# The device name defaults to the system hostname. To override it, add
# `--device <name>` to ExecStart in the unit (drop-in), e.g.:
# ExecStart=/usr/sbin/tomswall agent --device fw-a
+18
View File
@@ -0,0 +1,18 @@
[Unit]
Description=tomswall control-plane agent (pull and apply firewall config)
Documentation=https://git.unkin.net/unkin/tomswall
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
EnvironmentFile=/etc/tomswall/agent.env
ExecStart=/usr/sbin/tomswall agent
Restart=on-failure
RestartSec=10
# The agent programs nftables and needs the requisite capabilities.
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW
[Install]
WantedBy=multi-user.target
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
#
# Package the (already built) tomswall binary into an RPM with nfpm, bundling
# generated bash/zsh shell completions and the systemd agent unit.
# Usage: scripts/build-rpm.sh [version] (version defaults to $CI_COMMIT_TAG)
#
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "${ROOT_DIR}"
VERSION="${1:-${CI_COMMIT_TAG:-0.0.0-dev}}"
VERSION="${VERSION#v}" # strip a leading v
BINARY="tomswall"
DIST="dist"
if [ ! -f "${DIST}/${BINARY}" ]; then
echo "ERROR: ${DIST}/${BINARY} not found; run 'make dist-build' first" >&2
exit 1
fi
# Generate shell completions from the freshly built binary so they always match
# the shipped flags/subcommands.
COMP_DIR="${DIST}/completions"
mkdir -p "${COMP_DIR}"
"./${DIST}/${BINARY}" completion bash >"${COMP_DIR}/${BINARY}.bash"
"./${DIST}/${BINARY}" completion zsh >"${COMP_DIR}/_${BINARY}"
export PACKAGE_NAME="${BINARY}"
export PACKAGE_VERSION="${VERSION}"
export PACKAGE_RELEASE="1"
export PACKAGE_ARCH="amd64"
export PACKAGE_PLATFORM="linux"
export PACKAGE_DESCRIPTION="Spiritual successor to shorewall — nftables firewall manager, with a control-plane agent that pulls compiled config from tomswallapi"
export PACKAGE_MAINTAINER="Ben Vincent <ben@unkin.net>"
export PACKAGE_HOMEPAGE="https://git.unkin.net/unkin/tomswall"
export PACKAGE_LICENSE="MIT"
envsubst <packaging/nfpm.yaml >"${DIST}/nfpm.yaml"
nfpm pkg --config "${DIST}/nfpm.yaml" --target "${DIST}" --packager rpm
echo "Built:"
ls -1 "${DIST}"/*.rpm