Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c557c4b78a | |||
| 9a7d1ab35b | |||
| 445d14c61e | |||
| 63c46fec81 | |||
| 5238886c6e | |||
| 532bd80a8a | |||
| 036021d726 | |||
| ff5a52b9e2 | |||
| 44e1ba852e | |||
| 34cf6dc9ab | |||
| b5be665902 | |||
| 78afe7c242 | |||
| 1ad025a4b7 | |||
| aa0d3e10c2 | |||
| 3687baabe5 | |||
| abbf434af5 | |||
| 937556abeb | |||
| a7be035456 | |||
| e6244d6bf0 | |||
| 0b92f1c2f3 | |||
| 6ac03e1012 | |||
| 7f9c010e1a | |||
| df7ebb8efe | |||
| 457056d58a | |||
| 8efed72c96 | |||
| ed3209681d | |||
| 852d6bf2ca | |||
| f522589a83 | |||
| e16d95fb63 | |||
| 9c30f1fa54 | |||
| ad2dd9e3e7 | |||
| cc12c4a43a | |||
| 211bacd507 | |||
| 30758855ff | |||
| 9976bc9190 | |||
| 410109515e | |||
| 27504777f8 |
+13
-1
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,9 +4,21 @@ when:
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang:1.23
|
||||
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
|
||||
@@ -19,10 +31,22 @@ steps:
|
||||
cpu: 2
|
||||
|
||||
- name: build
|
||||
image: git.unkin.net/unkin/almalinux9-gobuilder:20260606
|
||||
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
|
||||
|
||||
+13
-1
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,19 +3,18 @@ module git.unkin.net/unkin/tomswall
|
||||
go 1.23
|
||||
|
||||
require (
|
||||
github.com/google/nftables v0.2.0
|
||||
github.com/google/nftables v0.3.0
|
||||
github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42
|
||||
github.com/spf13/cobra v1.8.1
|
||||
golang.org/x/sys v0.18.0
|
||||
golang.org/x/sys v0.28.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
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
|
||||
golang.org/x/net v0.33.0 // indirect
|
||||
golang.org/x/sync v0.6.0 // indirect
|
||||
)
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/nftables v0.2.0 h1:PbJwaBmbVLzpeldoeUKGkE2RjstrjPKMl6oLrfEJ6/8=
|
||||
github.com/google/nftables v0.2.0/go.mod h1:Beg6V6zZ3oEn0JuiUQ4wqwuyqqzasOltcoXPtgLbFp4=
|
||||
github.com/google/nftables v0.3.0 h1:bkyZ0cbpVeMHXOrtlFc8ISmfVqq5gPJukoYieyVmITg=
|
||||
github.com/google/nftables v0.3.0/go.mod h1:BCp9FsrbF1Fn/Yu6CLUc9GGZFw/+hsxfluNXXmxBfRM=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA=
|
||||
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
|
||||
github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g=
|
||||
github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw=
|
||||
github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42 h1:A1Cq6Ysb0GM0tpKMbdCXCIfBclan4oHk1Jb+Hrejirg=
|
||||
github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42/go.mod h1:BB4YCPDOzfy7FniQ/lxuYQ3dgmM2cZumHbK8RpTjN2o=
|
||||
github.com/mdlayher/socket v0.5.1 h1:VZaqt6RkGkt2OE9l3GcC6nZkqD3xKeQLyfleW/uBcos=
|
||||
github.com/mdlayher/socket v0.5.1/go.mod h1:TjPLHI1UgwEv5J1B5q0zTZq12A/6H7nKmtTanQE37IQ=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
@@ -16,14 +14,14 @@ github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
|
||||
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc h1:R83G5ikgLMxrBvLh22JhdfI8K6YXEPHx5P03Uu3DRs4=
|
||||
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI=
|
||||
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
|
||||
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
|
||||
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
||||
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
@@ -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
|
||||
@@ -103,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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+478
-240
@@ -3,7 +3,10 @@ package nftables
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -14,7 +17,8 @@ import (
|
||||
)
|
||||
|
||||
type Compiler struct {
|
||||
cfg *config.Config
|
||||
cfg *config.Config
|
||||
warned map[string]bool
|
||||
}
|
||||
|
||||
func NewCompiler(cfg *config.Config) *Compiler {
|
||||
@@ -25,6 +29,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,16 +206,81 @@ 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)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// helperProtos is each kernel helper's default transport, used when an entry gives no proto.
|
||||
var helperProtos = map[string]string{
|
||||
"amanda": "udp", "ftp": "tcp", "irc": "tcp", "netbios-ns": "udp", "pptp": "tcp",
|
||||
"Q.931": "tcp", "RAS": "udp", "sane": "tcp", "sip": "udp", "snmp": "udp", "tftp": "udp",
|
||||
}
|
||||
|
||||
// compileHelper declares a ct helper object per (helper, proto) and assigns it
|
||||
// after conntrack (-200) has created the entry; a raw-priority assignment is a no-op.
|
||||
func (c *Compiler) compileHelper(state *FirewallState, tag string, ct config.ConntrackRule) error {
|
||||
if ct.Source != "" || ct.Dest != "" {
|
||||
slog.Warn("conntrack helper source/dest not supported, assigning globally", "rule", tag, "helper", ct.Helper)
|
||||
}
|
||||
chains := []string{"helper_prerouting", "helper_output"}
|
||||
switch ct.Chain {
|
||||
case config.ConntrackPrerouting:
|
||||
chains = chains[:1]
|
||||
case config.ConntrackOutput:
|
||||
chains = chains[1:]
|
||||
}
|
||||
|
||||
proto := ct.Proto
|
||||
if proto == "" {
|
||||
if proto = helperProtos[ct.Helper]; proto == "" {
|
||||
return fmt.Errorf("proto required for helper %q", ct.Helper)
|
||||
}
|
||||
}
|
||||
for _, p := range strings.Split(proto, ",") {
|
||||
p = strings.TrimSpace(p)
|
||||
n, err := protoNumber(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := ct.Helper
|
||||
if p != helperProtos[ct.Helper] {
|
||||
name += "-" + p
|
||||
}
|
||||
if !slices.ContainsFunc(state.Helpers, func(h Helper) bool { return h.Name == name }) {
|
||||
state.Helpers = append(state.Helpers, Helper{Name: name,
|
||||
Helper: expr.CtHelper{Name: ct.Helper, L3Proto: unix.NFPROTO_INET, L4Proto: n}})
|
||||
}
|
||||
|
||||
matches, err := l4Matches(p, ct.DPort, ct.SPort)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, chain := range chains {
|
||||
for _, m := range matches {
|
||||
state.Rules[chain] = append(state.Rules[chain], ManagedRule{
|
||||
Chain: chain,
|
||||
Exprs: append(append([]expr.Any{}, m.exprs...),
|
||||
&expr.Objref{Type: unix.NFT_OBJECT_CT_HELPER, Name: name}),
|
||||
Tag: tag + ":" + chain,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Compiler) compileConntrack(state *FirewallState) error {
|
||||
for i, ct := range c.cfg.Conntrack {
|
||||
tag := fmt.Sprintf("conntrack:%d", i)
|
||||
if ct.Action == config.ConntrackHelper {
|
||||
if err := c.compileHelper(state, tag, ct); err != nil {
|
||||
return fmt.Errorf("conntrack[%d]: %w", i, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
chains := []string{"prerouting"}
|
||||
switch ct.Chain {
|
||||
@@ -220,34 +290,28 @@ 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.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 +335,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 +363,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 +399,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 +424,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 +554,112 @@ 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
|
||||
natExpr.Specified = true
|
||||
}
|
||||
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
|
||||
natExpr.Specified = true
|
||||
}
|
||||
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 +680,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 +690,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 +739,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 +790,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 +930,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 +939,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 +1023,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 +1067,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 +1099,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 +1212,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 +1344,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 +1421,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 +1441,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 +1468,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 +1857,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 +1882,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 +1902,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}}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+117
-48
@@ -2,6 +2,8 @@ package nftables
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/google/nftables/expr"
|
||||
@@ -12,19 +14,31 @@ 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 {
|
||||
Rules map[string][]ManagedRule
|
||||
// Helpers are the ct helper objects in kernel (insertion) order.
|
||||
Helpers []Helper
|
||||
}
|
||||
|
||||
// Helper is a named ct helper object.
|
||||
type Helper struct {
|
||||
Name string `json:"name"`
|
||||
Helper expr.CtHelper `json:"helper"`
|
||||
}
|
||||
|
||||
type ChangeSet struct {
|
||||
Add []ManagedRule
|
||||
Remove []ManagedRule
|
||||
Add []ManagedRule
|
||||
Remove []ManagedRule
|
||||
AddHelpers []Helper
|
||||
RemoveHelpers []string
|
||||
}
|
||||
|
||||
func (cs *ChangeSet) Empty() bool {
|
||||
return len(cs.Add) == 0 && len(cs.Remove) == 0
|
||||
return len(cs.Add) == 0 && len(cs.Remove) == 0 && len(cs.AddHelpers) == 0 && len(cs.RemoveHelpers) == 0
|
||||
}
|
||||
|
||||
func (cs *ChangeSet) Summary() string {
|
||||
@@ -32,7 +46,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 {
|
||||
@@ -41,69 +59,120 @@ func (cs *ChangeSet) Summary() string {
|
||||
fmt.Fprintf(&b, " - [%s] %s (handle %d)\n", r.Chain, r.Tag, r.Handle)
|
||||
}
|
||||
}
|
||||
for _, h := range cs.AddHelpers {
|
||||
fmt.Fprintf(&b, " + ct helper %q\n", h.Name)
|
||||
}
|
||||
for _, n := range cs.RemoveHelpers {
|
||||
fmt.Fprintf(&b, " - ct helper %q\n", n)
|
||||
}
|
||||
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{}
|
||||
cs := diffHelpers(current, desired)
|
||||
|
||||
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{AddHelpers: snap.Helpers}
|
||||
for _, h := range current.Helpers {
|
||||
cs.RemoveHelpers = append(cs.RemoveHelpers, h.Name)
|
||||
}
|
||||
as := fmt.Sprintf("%v", a)
|
||||
bs := fmt.Sprintf("%v", b)
|
||||
return as == bs
|
||||
for _, rules := range current.Rules {
|
||||
for _, r := range rules {
|
||||
if r.Tag != "" {
|
||||
cs.Remove = append(cs.Remove, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// diffHelpers replaces any ct helper object that is missing or differs.
|
||||
// L3Proto is ignored: the kernel narrows inet to ip/ip6 for single-family helpers such as pptp.
|
||||
func diffHelpers(current, desired *FirewallState) *ChangeSet {
|
||||
cs := &ChangeSet{}
|
||||
same := func(a, b expr.CtHelper) bool { return a.Name == b.Name && a.L4Proto == b.L4Proto }
|
||||
find := func(hs []Helper, name string) (expr.CtHelper, bool) {
|
||||
for _, h := range hs {
|
||||
if h.Name == name {
|
||||
return h.Helper, true
|
||||
}
|
||||
}
|
||||
return expr.CtHelper{}, false
|
||||
}
|
||||
for _, h := range current.Helpers {
|
||||
if want, ok := find(desired.Helpers, h.Name); !ok || !same(want, h.Helper) {
|
||||
cs.RemoveHelpers = append(cs.RemoveHelpers, h.Name)
|
||||
}
|
||||
}
|
||||
for _, h := range desired.Helpers {
|
||||
if have, ok := find(current.Helpers, h.Name); !ok || !same(have, h.Helper) {
|
||||
cs.AddHelpers = append(cs.AddHelpers, h)
|
||||
}
|
||||
}
|
||||
return cs
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package nftables
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/nftables/expr"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiffHelpers(t *testing.T) {
|
||||
h := func(name, typ string, l3 uint16, l4 uint8) Helper {
|
||||
return Helper{Name: name, Helper: expr.CtHelper{Name: typ, L3Proto: l3, L4Proto: l4}}
|
||||
}
|
||||
ftp := h("ftp", "ftp", unix.NFPROTO_INET, unix.IPPROTO_TCP)
|
||||
tftp := h("tftp", "tftp", unix.NFPROTO_INET, unix.IPPROTO_UDP)
|
||||
sipUDP := h("sip", "sip", unix.NFPROTO_INET, unix.IPPROTO_UDP)
|
||||
sipTCP := h("sip", "sip", unix.NFPROTO_INET, unix.IPPROTO_TCP)
|
||||
|
||||
current := &FirewallState{Helpers: []Helper{tftp, ftp, sipUDP}}
|
||||
desired := &FirewallState{Helpers: []Helper{ftp, sipTCP}}
|
||||
|
||||
cs := computeDiff(current, desired)
|
||||
if !reflect.DeepEqual(cs.RemoveHelpers, []string{"tftp", "sip"}) {
|
||||
t.Errorf("remove = %v", cs.RemoveHelpers)
|
||||
}
|
||||
if !reflect.DeepEqual(cs.AddHelpers, []Helper{sipTCP}) {
|
||||
t.Errorf("add = %v", cs.AddHelpers)
|
||||
}
|
||||
|
||||
// Restore recreates every helper so kernel listing order matches the snapshot.
|
||||
cs = restoreChangeSet(desired, current)
|
||||
if !reflect.DeepEqual(cs.RemoveHelpers, []string{"ftp", "sip"}) || !reflect.DeepEqual(cs.AddHelpers, current.Helpers) {
|
||||
t.Errorf("restore = -%v +%v", cs.RemoveHelpers, cs.AddHelpers)
|
||||
}
|
||||
|
||||
live := &FirewallState{Helpers: []Helper{h("pptp", "pptp", unix.NFPROTO_IPV4, unix.IPPROTO_TCP)}}
|
||||
want := &FirewallState{Helpers: []Helper{h("pptp", "pptp", unix.NFPROTO_INET, unix.IPPROTO_TCP)}}
|
||||
if cs := computeDiff(live, want); !cs.Empty() {
|
||||
t.Errorf("kernel-narrowed l3proto should not diff: %+v", cs)
|
||||
}
|
||||
if cs := computeDiff(desired, desired); !cs.Empty() {
|
||||
t.Errorf("identical helpers should be empty: %+v", cs)
|
||||
}
|
||||
if cs := computeDiff(&FirewallState{}, desired); cs.Empty() {
|
||||
t.Error("missing helpers should not be empty")
|
||||
}
|
||||
}
|
||||
+140
-18
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/nftables"
|
||||
"github.com/google/nftables/expr"
|
||||
|
||||
"git.unkin.net/unkin/tomswall/internal/config"
|
||||
)
|
||||
@@ -28,7 +29,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",
|
||||
@@ -68,9 +70,26 @@ func (e *Engine) ensureChains(table *nftables.Table) map[string]*nftables.Chain
|
||||
Hooknum: nftables.ChainHookPrerouting,
|
||||
Priority: nftables.ChainPriorityNATDest,
|
||||
},
|
||||
"helper_prerouting": {
|
||||
Name: "helper_prerouting",
|
||||
Table: table,
|
||||
Type: nftables.ChainTypeFilter,
|
||||
Hooknum: nftables.ChainHookPrerouting,
|
||||
Priority: nftables.ChainPriorityMangle,
|
||||
},
|
||||
"helper_output": {
|
||||
Name: "helper_output",
|
||||
Table: table,
|
||||
Type: nftables.ChainTypeFilter,
|
||||
Hooknum: nftables.ChainHookOutput,
|
||||
Priority: nftables.ChainPriorityMangle,
|
||||
},
|
||||
}
|
||||
|
||||
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 +112,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{
|
||||
@@ -104,17 +127,30 @@ func (e *Engine) Apply(changes *ChangeSet) error {
|
||||
})
|
||||
}
|
||||
|
||||
for _, n := range changes.RemoveHelpers {
|
||||
e.conn.DeleteObject(helperObj(table, n, expr.CtHelper{}))
|
||||
}
|
||||
for _, h := range changes.AddHelpers {
|
||||
e.conn.AddObj(helperObj(table, h.Name, h.Helper))
|
||||
}
|
||||
|
||||
for _, r := range changes.Add {
|
||||
chain, ok := chains[r.Chain]
|
||||
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 +171,44 @@ 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()
|
||||
ourTable, err := e.findTable()
|
||||
if err != nil || ourTable == nil {
|
||||
return state, err
|
||||
}
|
||||
|
||||
objs, err := e.conn.GetNamedObjects(ourTable)
|
||||
if err != nil {
|
||||
return state, nil
|
||||
return nil, fmt.Errorf("listing objects: %w", err)
|
||||
}
|
||||
|
||||
var ourTable *nftables.Table
|
||||
for _, t := range tables {
|
||||
if t.Name == e.cfg.Settings.TableName && t.Family == nftables.TableFamilyINet {
|
||||
ourTable = t
|
||||
break
|
||||
for _, o := range objs {
|
||||
if no, ok := o.(*nftables.NamedObj); ok && no.Type == nftables.ObjTypeCtHelper {
|
||||
if h, ok := no.Obj.(*expr.CtHelper); ok {
|
||||
state.Helpers = append(state.Helpers, Helper{Name: no.Name, Helper: *h})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ourTable == nil {
|
||||
return state, nil
|
||||
}
|
||||
|
||||
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 +217,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 +232,79 @@ 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"`
|
||||
Helpers []Helper `json:"helpers,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
|
||||
}
|
||||
snap.Helpers = state.Helpers
|
||||
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
|
||||
}
|
||||
want.Helpers = s.Helpers
|
||||
current, err := e.readCurrentState()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return e.apply(restoreChangeSet(current, want), s.Policies)
|
||||
}
|
||||
|
||||
func helperObj(table *nftables.Table, name string, h expr.CtHelper) *nftables.NamedObj {
|
||||
return &nftables.NamedObj{Table: table, Name: name, Type: nftables.ObjTypeCtHelper, Obj: &h}
|
||||
}
|
||||
|
||||
func policyPtr(p nftables.ChainPolicy) *nftables.ChainPolicy {
|
||||
return &p
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user