[PR #6780] [MERGED] fix(amneziawg): respect tunnel MTU when writing UDP replies #6452

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6780
Author: @mrdarkerr
Created: 10/9/2026
Status: ✅ Merged
Merged: 10/12/2026
Merged by: @MHSanaei

Base: main ← Head: fix/awg-udp-reply-mtu


📝 Commits (6)

  • 23ea0ab fix(amneziawg): respect tunnel MTU when writing UDP replies
  • 7c403a1 Merge branch 'main' into fix/awg-udp-reply-mtu
  • c598e06 Merge branch 'main' into fix/awg-udp-reply-mtu
  • e0a012b docs(review): put the tally before Design and name who can store a trigger
  • f2b24d8 Merge branch 'main' into fix/awg-udp-reply-mtu
  • 5a76b0b Merge branch 'main' into fix/awg-udp-reply-mtu

📊 Changes

4 files changed (+360 additions, -29 deletions)

View changed files

📝 internal/amneziawgnet/udp.go (+26 -29)
➕ internal/amneziawgnet/udp_mtu_concurrent_test.go (+62 -0)
➕ internal/amneziawgnet/udp_mtu_test.go (+97 -0)
➕ internal/amneziawgnet/udp_mtu_v6_test.go (+175 -0)

📄 Description

Summary

Make the embedded AmneziaWG UDP reply path respect the tunnel MTU by sending replies through gVisor's FindRoute / Route.WritePacket instead of injecting complete IP packets with WriteRawPacket.

Only internal/amneziawgnet/udp.go and three regression-test files are changed. No transport extensions, panel features, configuration fields, installer changes or dependency changes are included.

Why

WriteRawPacket bypasses the normal network-endpoint path that applies the NIC MTU and fragments locally generated IP datagrams. A large UDP response can therefore be emitted into the AWG TUN as one IP packet larger than its configured MTU.

On upstream main 442b7fb1637d6ecf4f2c28f76092228856e766c9, the new tests reproduce this with a 1280-byte TUN MTU and a 1452-byte UDP payload:

TUN packet length 1480 exceeds MTU 1280
IPv6 TUN packet length 1500 exceeds MTU

The route-based path preserves the remote source/destination, ports and payload while letting gVisor construct and fragment the IP packets. It releases both route and packet references. UDP checksums remain valid, including encoding a zero-complement checksum as 0xffff rather than an invalid IPv6 UDP zero checksum. Address-family/zone and family-specific length checks prevent invalid datagrams and length truncation.

This is a reproduced packet-sizing defect, not a claim that it explains every AWG throughput issue.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Embedded AmneziaWG UDP reply / gVisor network path
  • Frontend (UI / panel pages)
  • Database / migrations
  • Install / upgrade script
  • Multi-node (sub-nodes)

How was this tested?

The regression tests consume actual channel-backed TUN output, check every emitted packet against the MTU, reassemble IPv4/IPv6 fragments, and verify addresses, UDP ports, payload and checksums. They also cover invalid lengths/addresses, the IPv6 zero-complement checksum and concurrent reply sources.

Before applying the fix, the following command exited 1 on the oversized IPv4/IPv6 cases and the IPv6 zero-checksum case:

go test ./internal/amneziawgnet -run 'TestWriteUDPReply(MTU|IPv6MTU|IPv6ZeroChecksum)' -count=1 -v

After applying the fix, these local checks passed:

go test ./internal/amneziawgnet -run TestWriteUDPReply -count=1 -v
go test -p 2 -shuffle=on -count=1 ./...
go build ./...
go vet ./internal/amneziawgnet
go test -race -shuffle=on -count=1 ./internal/amneziawgnet
make build build-storybook

make verify also passed codegen freshness, golangci-lint (0 issues), frontend lint, format and typecheck before stopping at msw-worker-check. The tracked frontend/public/mockServiceWorker.js is byte-identical to upstream main, but differs from the worker installed by the unchanged lockfile's MSW 3.0.2. This PR does not modify either file or the lockfile.

A separate make test-fe attempt could not complete browser coverage: Playwright Chromium could not launch because this environment lacks libatk-1.0.so.0. Therefore full make verify / frontend test success is not claimed. Browser dependencies, the shared worker mismatch, full-repository race, live PostgreSQL/node E2E and mobile-network performance are not verified by the local runs above.

Screenshots / recordings

Not applicable; no UI changes.

Breaking changes

None. No settings, schema or client configuration changes are required.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added tests that reproduce the defect before the fix.
  • go build ./... and the Go test suite pass locally.
  • Frontend lint, format, typecheck and builds pass; frontend sources are unchanged.
  • Full make verify and browser tests pass (limitations documented above).
  • My commit follows the project's existing message style.
  • I have no unrelated changes mixed into this PR.

AI disclosure: Hermes Agent assisted with preparing this fix, regression tests and verification. All command outcomes reported above were executed locally.


🔄 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/6780 **Author:** [@mrdarkerr](https://github.com/mrdarkerr) **Created:** 10/9/2026 **Status:** ✅ Merged **Merged:** 10/12/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/awg-udp-reply-mtu` --- ### 📝 Commits (6) - [`23ea0ab`](https://github.com/MHSanaei/3x-ui/commit/23ea0abbb9cf7fa5af38c19d0ba76b9808b820b5) fix(amneziawg): respect tunnel MTU when writing UDP replies - [`7c403a1`](https://github.com/MHSanaei/3x-ui/commit/7c403a1ebb6ddb103059e742230d04dce0ac7184) Merge branch 'main' into fix/awg-udp-reply-mtu - [`c598e06`](https://github.com/MHSanaei/3x-ui/commit/c598e0639492a546c6010c9958e9ec7d674116aa) Merge branch 'main' into fix/awg-udp-reply-mtu - [`e0a012b`](https://github.com/MHSanaei/3x-ui/commit/e0a012b8b1f840aa1c5f18d28cbe4ec861d889a4) docs(review): put the tally before Design and name who can store a trigger - [`f2b24d8`](https://github.com/MHSanaei/3x-ui/commit/f2b24d8fa406b487484270a843e5e453bcefda98) Merge branch 'main' into fix/awg-udp-reply-mtu - [`5a76b0b`](https://github.com/MHSanaei/3x-ui/commit/5a76b0bef03abd125eed297f2e7d7563d47933ec) Merge branch 'main' into fix/awg-udp-reply-mtu ### 📊 Changes **4 files changed** (+360 additions, -29 deletions) <details> <summary>View changed files</summary> 📝 `internal/amneziawgnet/udp.go` (+26 -29) ➕ `internal/amneziawgnet/udp_mtu_concurrent_test.go` (+62 -0) ➕ `internal/amneziawgnet/udp_mtu_test.go` (+97 -0) ➕ `internal/amneziawgnet/udp_mtu_v6_test.go` (+175 -0) </details> ### 📄 Description ## Summary Make the embedded AmneziaWG UDP reply path respect the tunnel MTU by sending replies through gVisor's `FindRoute` / `Route.WritePacket` instead of injecting complete IP packets with `WriteRawPacket`. Only `internal/amneziawgnet/udp.go` and three regression-test files are changed. No transport extensions, panel features, configuration fields, installer changes or dependency changes are included. ## Why `WriteRawPacket` bypasses the normal network-endpoint path that applies the NIC MTU and fragments locally generated IP datagrams. A large UDP response can therefore be emitted into the AWG TUN as one IP packet larger than its configured MTU. On upstream main `442b7fb1637d6ecf4f2c28f76092228856e766c9`, the new tests reproduce this with a 1280-byte TUN MTU and a 1452-byte UDP payload: ```text TUN packet length 1480 exceeds MTU 1280 IPv6 TUN packet length 1500 exceeds MTU ``` The route-based path preserves the remote source/destination, ports and payload while letting gVisor construct and fragment the IP packets. It releases both route and packet references. UDP checksums remain valid, including encoding a zero-complement checksum as `0xffff` rather than an invalid IPv6 UDP zero checksum. Address-family/zone and family-specific length checks prevent invalid datagrams and length truncation. This is a reproduced packet-sizing defect, not a claim that it explains every AWG throughput issue. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Embedded AmneziaWG UDP reply / gVisor network path - [ ] Frontend (UI / panel pages) - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Multi-node (sub-nodes) ## How was this tested? The regression tests consume actual channel-backed TUN output, check every emitted packet against the MTU, reassemble IPv4/IPv6 fragments, and verify addresses, UDP ports, payload and checksums. They also cover invalid lengths/addresses, the IPv6 zero-complement checksum and concurrent reply sources. Before applying the fix, the following command exited 1 on the oversized IPv4/IPv6 cases and the IPv6 zero-checksum case: ```sh go test ./internal/amneziawgnet -run 'TestWriteUDPReply(MTU|IPv6MTU|IPv6ZeroChecksum)' -count=1 -v ``` After applying the fix, these local checks passed: ```sh go test ./internal/amneziawgnet -run TestWriteUDPReply -count=1 -v go test -p 2 -shuffle=on -count=1 ./... go build ./... go vet ./internal/amneziawgnet go test -race -shuffle=on -count=1 ./internal/amneziawgnet make build build-storybook ``` `make verify` also passed codegen freshness, golangci-lint (0 issues), frontend lint, format and typecheck before stopping at `msw-worker-check`. The tracked `frontend/public/mockServiceWorker.js` is byte-identical to upstream main, but differs from the worker installed by the unchanged lockfile's MSW 3.0.2. This PR does not modify either file or the lockfile. A separate `make test-fe` attempt could not complete browser coverage: Playwright Chromium could not launch because this environment lacks `libatk-1.0.so.0`. Therefore full `make verify` / frontend test success is **not claimed**. Browser dependencies, the shared worker mismatch, full-repository race, live PostgreSQL/node E2E and mobile-network performance are not verified by the local runs above. ## Screenshots / recordings Not applicable; no UI changes. ## Breaking changes None. No settings, schema or client configuration changes are required. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added tests that reproduce the defect before the fix. - [x] `go build ./...` and the Go test suite pass locally. - [x] Frontend lint, format, typecheck and builds pass; frontend sources are unchanged. - [ ] Full `make verify` and browser tests pass (limitations documented above). - [x] My commit follows the project's existing message style. - [x] I have no unrelated changes mixed into this PR. **AI disclosure:** Hermes Agent assisted with preparing this fix, regression tests and verification. All command outcomes reported above were executed locally. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:52 +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#6452
No description provided.