[PR #6376] [MERGED] fix(amneziawg): account for S4 junk in the default tunnel MTU #6239

Closed
opened 2026-11-02 04:07:19 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6376
Author: @YoungReckless4
Created: 8/31/2026
Status: ✅ Merged
Merged: 9/8/2026
Merged by: @MHSanaei

Base: main ← Head: fix/amneziawg-mtu-s4


📝 Commits (3)

  • f54e6ee fix(amneziawg): account for S4 junk in the default tunnel MTU
  • 09d255b fix(amneziawg): rebuild the device when S4 changes the derived MTU
  • 0a430ab Merge remote-tracking branch 'origin/main' into pr/6376

📊 Changes

12 files changed (+263 additions, -26 deletions)

View changed files

📝 frontend/src/lib/xray/amneziawg-obfuscation.ts (+15 -0)
📝 frontend/src/lib/xray/inbound-link.ts (+2 -3)
📝 frontend/src/pages/clients/amneziawgConfig.ts (+2 -1)
📝 frontend/src/test/amneziawg-conf-parity.test.ts (+76 -0)
📝 internal/amneziawg/params.go (+13 -0)
📝 internal/amneziawg/params_test.go (+36 -0)
📝 internal/amneziawgnet/device.go (+2 -10)
📝 internal/amneziawgnet/manager.go (+9 -8)
📝 internal/amneziawgnet/manager_test.go (+63 -0)
📝 internal/sub/service.go (+3 -3)
📝 internal/sub/service_amneziawg_test.go (+40 -0)
📝 internal/web/runtime/local.go (+2 -1)

📄 Description

The bug

amneziawg prepends S4 random bytes to every transport packet
(device.NewOutboundElement sets elem.padding = device.paddings.transport) and,
unlike ContentPaddingAddition and RandomTrailers, never clamps them against the
tunnel MTU. randomPaddingAddition and calculatePaddingSize both cap at mtu, so
they contribute 0 on a full-size packet; randomTrailer returns 0 for anything above
DefaultUdpWindow. S4 is the one that always applies.

A full-size packet therefore reaches the wire at:

MTU + 20 (IPv4) + 8 (UDP) + S4 + 16 (transport header) + 16 (poly1305 tag)
= MTU + 60 + S4

With defaultMTU = 1420 that crosses 1500 as soon as S4 exceeds 20, and
GenerateObfuscation31 draws S4 = randInt(12, 27) with both bounds inclusive —
so roughly 44% of newly created inbounds (S4 in 21..27) fragment every full-size
packet they send
, silently.

Measured, not derived

Two AmneziaWG interfaces in separate network namespaces joined by a veth pair,
measuring the outer UDP datagram:

MTU S4 wire size predicted
1380 12 1452 1452 fits
1420 12 1492 1492 fits
1420 20 1500 1500 exactly at the limit
1420 21 1501 1501 fragments
1420 27 1507 1507 fragments

The formula matches byte for byte at every point, and the threshold is exactly the
predicted S4 ≥ 21.

The fix

One new exported symbol, EffectiveMTU(configuredMTU, s4) in internal/amneziawg: an explicit MTU
is returned untouched, otherwise 1420 - S4. The 1280 floor is defensive — validation caps S4 at
32, so it cannot be reached through the panel.

Client configs now 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 — so fixing only the server side would have left the client-to-server
direction fragmenting, silently and in one direction only.

All three emitters agree now. The two that matter most in practice are the
TypeScript ones — buildAmneziaWGClientConfig (download/QR) and
genAmneziaWGConfig (the vpn:// share link), since the AmneziaVPN client imports
a config rather than polling a subscription — and amneziaWGConfigText on the Go
side emits the same text for the subscription endpoint. That three-way agreement is
what amneziawg-conf-parity.test.ts exists to protect, and the new cases there
cover MTU specifically.

Tests

  • TestDefaultMTUForS4KeepsFullSizePacketsUnfragmented — sweeps S4 0..32 and asserts
    the wire size never exceeds 1500. Fails on the old default from S4 = 21 up.
  • TestEffectiveMTUPrefersTheAdminsValue — an explicit MTU is never overridden.
  • TestAmneziaWGClientConfigAlwaysCarriesTheServerMTU — the emitted client config
    always contains the server's effective MTU.

gofmt, go vet ./..., go test on internal/amneziawg, internal/amneziawgnet,
internal/sub, plus tsc --noEmit and the two affected frontend test files (61 tests)
all pass.

Compatibility

Existing inbounds with an explicit MTU are unaffected. Inbounds relying on the default
get a smaller MTU on the next reconcile; clients must re-import their config (or refetch
the subscription) for the client side to match, which is the same requirement any MTU
change has always had.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6376 **Author:** [@YoungReckless4](https://github.com/YoungReckless4) **Created:** 8/31/2026 **Status:** ✅ Merged **Merged:** 9/8/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/amneziawg-mtu-s4` --- ### 📝 Commits (3) - [`f54e6ee`](https://github.com/MHSanaei/3x-ui/commit/f54e6ee65218d9e5e3f59f81fe1523da16affd7b) fix(amneziawg): account for S4 junk in the default tunnel MTU - [`09d255b`](https://github.com/MHSanaei/3x-ui/commit/09d255b4bf844e0acd3a9e84025373c39764a99c) fix(amneziawg): rebuild the device when S4 changes the derived MTU - [`0a430ab`](https://github.com/MHSanaei/3x-ui/commit/0a430ab34525ff2f0655768cebc988ee4db53bd2) Merge remote-tracking branch 'origin/main' into pr/6376 ### 📊 Changes **12 files changed** (+263 additions, -26 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/lib/xray/amneziawg-obfuscation.ts` (+15 -0) 📝 `frontend/src/lib/xray/inbound-link.ts` (+2 -3) 📝 `frontend/src/pages/clients/amneziawgConfig.ts` (+2 -1) 📝 `frontend/src/test/amneziawg-conf-parity.test.ts` (+76 -0) 📝 `internal/amneziawg/params.go` (+13 -0) 📝 `internal/amneziawg/params_test.go` (+36 -0) 📝 `internal/amneziawgnet/device.go` (+2 -10) 📝 `internal/amneziawgnet/manager.go` (+9 -8) 📝 `internal/amneziawgnet/manager_test.go` (+63 -0) 📝 `internal/sub/service.go` (+3 -3) 📝 `internal/sub/service_amneziawg_test.go` (+40 -0) 📝 `internal/web/runtime/local.go` (+2 -1) </details> ### 📄 Description ### The bug `amneziawg` prepends S4 random bytes to every transport packet (`device.NewOutboundElement` sets `elem.padding = device.paddings.transport`) and, unlike `ContentPaddingAddition` and `RandomTrailers`, never clamps them against the tunnel MTU. `randomPaddingAddition` and `calculatePaddingSize` both cap at `mtu`, so they contribute 0 on a full-size packet; `randomTrailer` returns 0 for anything above `DefaultUdpWindow`. S4 is the one that always applies. A full-size packet therefore reaches the wire at: ``` MTU + 20 (IPv4) + 8 (UDP) + S4 + 16 (transport header) + 16 (poly1305 tag) = MTU + 60 + S4 ``` With `defaultMTU = 1420` that crosses 1500 as soon as S4 exceeds 20, and `GenerateObfuscation31` draws `S4 = randInt(12, 27)` with both bounds inclusive — so **roughly 44% of newly created inbounds (S4 in 21..27) fragment every full-size packet they send**, silently. ### Measured, not derived Two AmneziaWG interfaces in separate network namespaces joined by a veth pair, measuring the outer UDP datagram: | MTU | S4 | wire size | predicted | | |---|---|---|---|---| | 1380 | 12 | 1452 | 1452 | fits | | 1420 | 12 | 1492 | 1492 | fits | | 1420 | 20 | 1500 | 1500 | exactly at the limit | | 1420 | 21 | **1501** | 1501 | fragments | | 1420 | 27 | **1507** | 1507 | fragments | The formula matches byte for byte at every point, and the threshold is exactly the predicted S4 ≥ 21. ### The fix One new exported symbol, `EffectiveMTU(configuredMTU, s4)` in `internal/amneziawg`: an explicit MTU is returned untouched, otherwise `1420 - S4`. The 1280 floor is defensive — validation caps S4 at 32, so it cannot be reached through the panel. Client configs now 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 — so fixing only the server side would have left the **client-to-server** direction fragmenting, silently and in one direction only. All three emitters agree now. The two that matter most in practice are the TypeScript ones — `buildAmneziaWGClientConfig` (download/QR) and `genAmneziaWGConfig` (the `vpn://` share link), since the AmneziaVPN client imports a config rather than polling a subscription — and `amneziaWGConfigText` on the Go side emits the same text for the subscription endpoint. That three-way agreement is what `amneziawg-conf-parity.test.ts` exists to protect, and the new cases there cover MTU specifically. ### Tests - `TestDefaultMTUForS4KeepsFullSizePacketsUnfragmented` — sweeps S4 0..32 and asserts the wire size never exceeds 1500. Fails on the old default from S4 = 21 up. - `TestEffectiveMTUPrefersTheAdminsValue` — an explicit MTU is never overridden. - `TestAmneziaWGClientConfigAlwaysCarriesTheServerMTU` — the emitted client config always contains the server's effective MTU. `gofmt`, `go vet ./...`, `go test` on `internal/amneziawg`, `internal/amneziawgnet`, `internal/sub`, plus `tsc --noEmit` and the two affected frontend test files (61 tests) all pass. ### Compatibility Existing inbounds with an explicit MTU are unaffected. Inbounds relying on the default get a smaller MTU on the next reconcile; clients must re-import their config (or refetch the subscription) for the client side to match, which is the same requirement any MTU change has always had. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:19 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6239
No description provided.