fix(amneziawg): account for S4 junk in the default tunnel MTU (#6376)

* fix(amneziawg): account for S4 junk in the default tunnel MTU

amneziawg prepends S4 random bytes to every transport packet
(device.NewOutboundElement) and, unlike content padding and random trailers,
never clamps them against the tunnel MTU. A full-size packet therefore lands on
the wire at MTU + 60 + S4 bytes: 20 IPv4 + 8 UDP + S4 + 16 transport header +
16 poly1305 tag.

With the 1420 default that overflows a 1500-byte link once S4 exceeds 20, and
GenerateObfuscation31 draws S4 from 12..27 inclusive -- so roughly 44% of newly
created inbounds fragment every full-size packet they send.

Measured on a live pair of interfaces, predicted against observed:

    MTU 1380  S4 12  ->  1452 on the wire   (fits)
    MTU 1420  S4 12  ->  1492               (fits)
    MTU 1420  S4 20  ->  1500               (exactly at the limit)
    MTU 1420  S4 21  ->  1501               (fragments)
    MTU 1420  S4 27  ->  1507               (fragments)

EffectiveMTU now subtracts S4 from the default; an explicit MTU is untouched.

Client configs carry the same number. They previously omitted the MTU line
whenever the server had no explicit value, which left the client on its own
1420 default and fragmented the client-to-server direction even after the
server side was fixed -- silently, and only in one direction. All three
emitters (the Go subscription text and the two TypeScript ones) now agree,
which is what the existing parity test exists to protect.

* fix(amneziawg): rebuild the device when S4 changes the derived MTU

Addresses review feedback on the previous commit.

Deriving the default MTU from S4 made a construction-time-only property depend
on a hot-reloadable input, but addressFingerprint -- ensureLocked's only rebuild
trigger -- still hashed the raw inst.MTU. S4 is a UAPI field, so an S4-only edit
took the in-place IpcSet branch and the gVisor netstack kept the MTU derived
from the old S4 while all three client emitters already advertised the new one.

Every panel-created inbound leaves mtu unset, so that was the normal case, not
an edge one: with S4 raised far enough the fragmentation this fix exists to
remove came straight back, and stayed until a panel restart or an unrelated
address edit.

Folding EffectiveMTU into the fingerprint fixes it. An explicit MTU still takes
the in-place branch on an S4 edit, since it does not move the interface MTU.

Also trims four comment blocks to the 2-line cap in CLAUDE.md, and points
NewDevice's doc comment at EffectiveMTU instead of the deleted defaultMTU.
This commit is contained in:
YoungReckless4
2026-09-08 17:55:32 +03:00
committed by GitHub
parent 5a63d5d468
commit 3cd3836d77
12 changed files with 263 additions and 26 deletions
+13
View File
@@ -33,6 +33,19 @@ func randInt(min, max int) int {
return min + int(n.Int64())
}
// DefaultMTU is WireGuard/AmneziaWG's usual tunnel MTU on a 1500-byte host
// link, before AmneziaWG's own S4 transport junk is prepended.
const DefaultMTU = 1420
// EffectiveMTU is the admin's value when set, else DefaultMTU minus S4: s4 junk
// is prepended to every transport packet and never clamped against the MTU.
func EffectiveMTU(configuredMTU, s4 int) int {
if configuredMTU > 0 {
return configuredMTU
}
return max(DefaultMTU-max(s4, 0), 1280)
}
// GenerateObfuscation31 produces a randomized AmneziaWG 3.1 parameter set: a
// static value gets profiled by DPI, defeating the point.
func GenerateObfuscation31() Obfuscation31 {
+36
View File
@@ -378,6 +378,42 @@ func TestValidateConfigValueRejectsControlCharacters(t *testing.T) {
}
}
// The plain 1420 default left no headroom for s4: it put full-size packets at
// 1480+S4 on the wire and fragmented every one of them once S4 passed 20.
func TestEffectiveMTUKeepsFullSizePacketsUnfragmented(t *testing.T) {
t.Parallel()
// 20 IPv4 + 8 UDP + 16 transport header + 16 poly1305 tag.
const encapOverhead = 60
const hostLinkMTU = 1500
for s4 := 0; s4 <= 32; s4++ {
mtu := EffectiveMTU(0, s4)
if wire := mtu + encapOverhead + s4; wire > hostLinkMTU {
t.Errorf("s4=%d: MTU %d puts a full-size transport packet at %d bytes on the wire, over the %d-byte host link", s4, mtu, wire, hostLinkMTU)
}
}
}
// TestEffectiveMTUPrefersTheAdminsValue: the S4-aware default is a fallback,
// not an override -- an explicit MTU must survive untouched.
func TestEffectiveMTUPrefersTheAdminsValue(t *testing.T) {
t.Parallel()
if got := EffectiveMTU(1380, 27); got != 1380 {
t.Errorf("EffectiveMTU(1380, 27) = %d, want the configured 1380", got)
}
if got := EffectiveMTU(0, 27); got != DefaultMTU-27 {
t.Errorf("EffectiveMTU(0, 27) = %d, want %d", got, DefaultMTU-27)
}
if got := EffectiveMTU(0, 0); got != DefaultMTU {
t.Errorf("EffectiveMTU(0, 0) = %d, want %d", got, DefaultMTU)
}
if got := EffectiveMTU(-5, 12); got != DefaultMTU-12 {
t.Errorf("a nonsense configured MTU must fall back, got %d", got)
}
}
// TestValidateObfuscationRejectsOutOfRangeJunkAndPadding pins the widths
// amneziawg-go's UAPI actually parses: uint32 for jc/jmin/jmax, uint16 for s1-s4.
func TestValidateObfuscationRejectsOutOfRangeJunkAndPadding(t *testing.T) {