Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions cmd/galactic-nat/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"go.datum.net/galactic/internal/plumbing/ebpf/natmap"
"go.datum.net/galactic/internal/plumbing/ebpf/natprog"
"go.datum.net/galactic/internal/plumbing/ebpf/natsrcfiltermap"
)

const metricsNamespace = "galactic_nat"
Expand All @@ -40,6 +41,7 @@ var trafficLabels = []string{labelFamily, "direction", "action"}
// scoped to what the map layer exposes read access to, the connection table
// being entirely datapath-owned and so observability-only.
type natCollector struct {
srcFilter *natsrcfiltermap.Filter
connTable *natmap.ConnTable
connMaxEntries uint32
dropReasons natmap.DropReasonsReader
Expand Down Expand Up @@ -78,6 +80,7 @@ func newNatCollector(objs *natprog.NatObjects) *natCollector {
claims: objs.NatClaims,
neighFallbacks: objs.NeighFallbacks,
shardConfig: natmap.NewShardConfigTable(natmap.KernelTable{Map: objs.ShardConfigTable}),
srcFilter: natsrcfiltermap.NewKernelFilter(objs),
}
}

Expand Down Expand Up @@ -177,6 +180,23 @@ var (
"Packets dropped by this shard's datapath, by reason (drop_reasons map).",
[]string{"reason"}, nil,
)
srcFilterDecisionsDesc = prometheus.NewDesc(
prometheus.BuildFQName(metricsNamespace, "srv6_source_filter", "decisions_total"),
"SRv6 source filter decisions on encapsulated tenant packets, by decision. Denials are counted "+
"in audit mode too, where the packet is still forwarded.",
[]string{"decision"}, nil,
)
srcFilterStateDesc = prometheus.NewDesc(
prometheus.BuildFQName(metricsNamespace, "srv6_source_filter", "populated"),
"1 once the SRv6 source filter's allow-list has completed a full sync, else 0, labelled with "+
"the filter's mode.",
[]string{"mode"}, nil,
)
srcFilterAllowEntriesDesc = prometheus.NewDesc(
prometheus.BuildFQName(metricsNamespace, "srv6_source_filter", "allow_entries"),
"Current number of prefixes in the SRv6 source filter's allow-list.",
nil, nil,
)
)

// Describe implements prometheus.Collector.
Expand All @@ -194,6 +214,9 @@ func (c *natCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- sessionCreatesDesc
ch <- claimProbesDesc
ch <- neighFallbacksDesc
ch <- srcFilterDecisionsDesc
ch <- srcFilterStateDesc
ch <- srcFilterAllowEntriesDesc
}

// Collect implements prometheus.Collector.
Expand All @@ -204,6 +227,7 @@ func (c *natCollector) Collect(ch chan<- prometheus.Metric) {
c.collectTraffic(ch)
c.collectClaims(ch)
c.collectNeighFallbacks(ch)
c.collectSrcFilter(ch)
}

// collectNeighFallbacks reports the return packets passed to the kernel for
Expand Down Expand Up @@ -430,4 +454,41 @@ func (c *natCollector) collectClaims(ch chan<- prometheus.Metric) {
f.label, p.label)
}
}

}
func (c *natCollector) collectSrcFilter(ch chan<- prometheus.Metric) {
if c.srcFilter == nil {
return
}

if stats, err := c.srcFilter.Stats(); err != nil {
ch <- prometheus.NewInvalidMetric(srcFilterDecisionsDesc, fmt.Errorf("read source filter stats: %w", err))
} else {
for decision, value := range map[string]uint64{
natprog.SrcStatNames[natprog.SrcStatChecked]: stats.Checked,
natprog.SrcStatNames[natprog.SrcStatAllowed]: stats.Allowed,
natprog.SrcStatNames[natprog.SrcStatDenyPrefix]: stats.DenyPrefix,
natprog.SrcStatNames[natprog.SrcStatDenyInterface]: stats.DenyInterface,
natprog.SrcStatNames[natprog.SrcStatDenyStructure]: stats.DenyStructure,
natprog.SrcStatNames[natprog.SrcStatBypassUnpopulated]: stats.BypassUnpopulated,
} {
ch <- prometheus.MustNewConstMetric(srcFilterDecisionsDesc, prometheus.CounterValue, float64(value), decision)
}
}

if cfg, err := c.srcFilter.Config(); err != nil {
ch <- prometheus.NewInvalidMetric(srcFilterStateDesc, fmt.Errorf("read source filter config: %w", err))
} else {
populated := 0.0
if cfg.Populated {
populated = 1
}
ch <- prometheus.MustNewConstMetric(srcFilterStateDesc, prometheus.GaugeValue, populated, cfg.Mode.String())
}

if entries, err := c.srcFilter.ListAllow(); err != nil {
ch <- prometheus.NewInvalidMetric(srcFilterAllowEntriesDesc, fmt.Errorf("list source filter allow-list: %w", err))
} else {
ch <- prometheus.MustNewConstMetric(srcFilterAllowEntriesDesc, prometheus.GaugeValue, float64(len(entries)))
}
}
7 changes: 7 additions & 0 deletions cmd/galactic-nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.datum.net/galactic/internal/plumbing/ebpf/natattach"
"go.datum.net/galactic/internal/plumbing/ebpf/natmap"
"go.datum.net/galactic/internal/plumbing/ebpf/natprog"
"go.datum.net/galactic/internal/plumbing/ebpf/natsrcfiltermap"
"go.datum.net/galactic/internal/plumbing/ebpf/xdpattach"
"go.datum.net/galactic/internal/plumbing/ebpf/xdpdispatch"
"go.datum.net/galactic/internal/plumbing/ebpf/xdpmode"
Expand Down Expand Up @@ -322,6 +323,11 @@ func setupNatDatapath(ctx context.Context, cfg *config.NATConfig,
return nil, fmt.Errorf("load egress translation eBPF datapath: %w", err)
}

if err := setupSourceFilter(cfg.SRv6SourceFilter, uplinks, natsrcfiltermap.NewKernelFilter(objs)); err != nil {
_ = objs.Close()
return nil, fmt.Errorf("configure SRv6 source filter: %w", err)
}

// Before the attach, not after: the dispatcher tail-calls into these, and a
// packet arriving at an empty slot passes through untranslated.
if err := natattach.PopulateProgArray(objs); err != nil {
Expand Down Expand Up @@ -381,6 +387,7 @@ func setupNatDatapath(ctx context.Context, cfg *config.NATConfig,
"interfaces", uplinks,
"autoDetected", len(cfg.UplinkInterfaces) == 0,
"xdpAttach", cfg.XDPAttach,
"srv6SourceFilter", cfg.SRv6SourceFilter,
)

d.attached = true
Expand Down
64 changes: 64 additions & 0 deletions cmd/galactic-nat/srcfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"fmt"

"github.com/vishvananda/netlink"

"go.datum.net/galactic/internal/plumbing/ebpf/natprog"
"go.datum.net/galactic/internal/plumbing/ebpf/natsrcfiltermap"
)

// setupSourceFilter writes the SRv6 source filter's mode and numbers this
// shard's uplinks into slots, in configuration order, so allow-list entries
// can bind a prefix to the uplinks it is reachable through. It leaves the
// allow-list and its populated flag to their own reconciler, so until that
// has run the datapath applies only its structural source checks.
func setupSourceFilter(configuredMode string, uplinks []string, filter *natsrcfiltermap.Filter) error {
mode, err := natsrcfiltermap.ParseMode(configuredMode)
if err != nil {
return err
}
if mode == natsrcfiltermap.ModeOff {
return filter.SetMode(mode)
}
slots, err := uplinkSlots(uplinks, linkIndexByName)
if err != nil {
return err
}
if err := filter.SyncUplinkSlots(slots); err != nil {
return err
}
return filter.SetMode(mode)
}

// uplinkSlots assigns slot N to the Nth interface in ifaces, keyed by the
// ifindex ifindexOf resolves it to. It returns an error when an interface does
// not resolve or there are more interfaces than slots.
func uplinkSlots(ifaces []string, ifindexOf func(string) (int, error)) (map[uint32]uint8, error) {
if len(ifaces) > int(natprog.SrcFilterMaxSlot)+1 {
return nil, fmt.Errorf("%d uplink interfaces exceed the source filter's %d slots",
len(ifaces), int(natprog.SrcFilterMaxSlot)+1)
}
slots := make(map[uint32]uint8, len(ifaces))
for i, name := range ifaces {
ifindex, err := ifindexOf(name)
if err != nil {
return nil, fmt.Errorf("resolve uplink interface %q: %w", name, err)
}
slots[uint32(ifindex)] = uint8(i)
}
return slots, nil
}

func linkIndexByName(name string) (int, error) {
link, err := netlink.LinkByName(name)
if err != nil {
return 0, err
}
return link.Attrs().Index, nil
}
80 changes: 80 additions & 0 deletions cmd/galactic-nat/srcfilter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2026 Datum Cloud, Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package main

import (
"errors"
"fmt"
"reflect"
"testing"

"go.datum.net/galactic/internal/plumbing/ebpf/natsrcfiltermap"
)

const (
testUplinkA = "eth1"
testUplinkB = "eth0"
)

func fakeIfindex(indexes map[string]int) func(string) (int, error) {
return func(name string) (int, error) {
if i, ok := indexes[name]; ok {
return i, nil
}
return 0, errors.New("link not found")
}
}

func TestUplinkSlots(t *testing.T) {
many := make([]string, 33)
for i := range many {
many[i] = fmt.Sprintf("eth%d", i)
}
tests := []struct {
name string
ifaces []string
want map[uint32]uint8
wantError bool
}{
{"ConfigurationOrder", []string{testUplinkA, testUplinkB}, map[uint32]uint8{3: 0, 2: 1}, false},
{"Unresolvable", []string{testUplinkA, "missing"}, nil, true},
{"TooMany", many, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := uplinkSlots(tt.ifaces, fakeIfindex(map[string]int{testUplinkB: 2, testUplinkA: 3}))
if (err != nil) != tt.wantError {
t.Fatalf("uplinkSlots error = %v, wantError = %v", err, tt.wantError)
}
if !tt.wantError && !reflect.DeepEqual(got, tt.want) {
t.Errorf("uplinkSlots = %v, want %v", got, tt.want)
}
})
}
}

func TestSetupSourceFilterRejectsUnknownMode(t *testing.T) {
filter, fakes := natsrcfiltermap.NewFakeFilter()
if err := setupSourceFilter("strict", nil, filter); err == nil {
t.Fatal("setupSourceFilter succeeded with an unknown mode, want error")
}
if fakes.Config.Len() != 0 || fakes.UplinkSlots.Len() != 0 {
t.Error("setupSourceFilter wrote state before rejecting its mode")
}
}

func TestSetupSourceFilterOffSkipsUplinkResolution(t *testing.T) {
filter, _ := natsrcfiltermap.NewFakeFilter()
if err := filter.SetMode(natsrcfiltermap.ModeEnforce); err != nil {
t.Fatal(err)
}
if err := setupSourceFilter("off", []string{"missing-test-interface"}, filter); err != nil {
t.Fatal(err)
}
cfg, err := filter.Config()
if err != nil || cfg.Mode != natsrcfiltermap.ModeOff {
t.Fatalf("config = %+v, %v, want off", cfg, err)
}
}
35 changes: 32 additions & 3 deletions docs/nat/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Logging takes `--log-level` (`LOG_LEVEL`), without the `GALACTIC_NAT` prefix and
| Uplink interfaces | `GALACTIC_NAT_UPLINK_INTERFACES` | `--nat-uplink-interfaces` | auto-detected | No |
| XDP attach mode | `GALACTIC_NAT_XDP_ATTACH` | `--nat-xdp-attach` | `direct` | No |
| Datapath enabled | `GALACTIC_NAT_DATAPATH_ENABLED` | `--nat-datapath-enabled` | `true` | No |
| SRv6 source filter | `GALACTIC_NAT_SRV6_SOURCE_FILTER` | — | `off` | No |
| Echo responder | `GALACTIC_NAT_ECHO_RESPONDER` | `--nat-echo-responder` | `false` | No |
| Procfs sysctl root | `GALACTIC_NAT_PROC_SYS_PATH` | `--nat-proc-sys-path` | `/proc/sys` | No |
| Metrics port | `GALACTIC_NAT_METRICS_PORT` | `--metrics-port` | `9182` | No |
Expand Down Expand Up @@ -197,6 +198,32 @@ node where something else already turned forwarding on passes with the
default too. SELinux policy on an enforcing node can still refuse the write,
and the shard then says so at startup.

**`GALACTIC_NAT_SRV6_SOURCE_FILTER`**
The datapath's SRv6 source filter mode: `off`, `audit` or `enforce`.
Environment-only. Before a tenant's encapsulated packet reaches either
forward leg, the filter checks its outer source:

- It must be a well-formed tenant SID: Function End.DT46, a non-zero
Argument and zero padding. The reply is re-encapsulated toward exactly
that address, so nothing that fails this could have been answered.
- Once the allow-list is populated, it must fall inside an allowed fabric
prefix and arrive on an uplink that prefix is bound to. Until then these
two checks are bypassed and counted as such.

| Mode | Behaviour |
| --------- | ------------------------------------------------------------------------- |
| `off` | No check; the datapath behaves exactly as without the filter. |
| `audit` | Every decision is counted and denied sources are recorded; nothing drops. |
| `enforce` | Packets whose source fails a check are dropped. |

`galactic-nat` numbers the resolved startup uplinks into slots in order, for allow-list entries to
bind to. Nothing populates the allow-list yet, so today `audit` and
`enforce` apply only the structural check. Decisions are exported as
`galactic_nat_srv6_source_filter_decisions_total{decision}`, with
`galactic_nat_srv6_source_filter_populated{mode}` and
`galactic_nat_srv6_source_filter_allow_entries` alongside. Roll out with
`audit` first and move to `enforce` once the denials are understood.

### Capabilities and host requirements

`galactic-nat` runs `hostNetwork: true` and needs:
Expand Down Expand Up @@ -499,9 +526,11 @@ knowing before you rely on this component in production:
deployment needs its own redistribution/border design for this.
- **IPv4 is out of scope.** `nat.c` rejects non-IPv6 inner packets
outright; there is no IPv4 masquerade path.
- **No anti-spoofing / trust boundary on ingress to a shard.** The
datapath trusts fabric-internal traffic; this deserves its own security
pass before carrying untrusted traffic.
- **Source filtering is structural only until its allow-list is
populated.** `GALACTIC_NAT_SRV6_SOURCE_FILTER` checks that an
encapsulated packet's source is a well-formed tenant SID; the fabric-peer
allow-list and uplink binding it also supports are not yet populated by
anything in this repo. The filter is `off` by default.
- **A shard node's netfilter rules never see tenant egress.** Both
directions are claimed in XDP and leave from the driver, so nothing a
shard translates traverses `PREROUTING`, `FORWARD`, or connection
Expand Down
Loading
Loading