feat(wip): amnezia-wg 3.1 support

This commit is contained in:
FUFSoB committed 2026-08-30 18:07:43 +05:00
1 parent 539f6a13ee
commit a456534801
29 files changed
+4092 -12

No files matched your search

@@ -0,0 +1,68 @@
diff --git a/device/receive.go b/device/receive.go
index d1901f8..8d709bf 100644
--- a/device/receive.go
+++ b/device/receive.go
@@ -124,8 +124,18 @@ func (device *Device) RoutineReceiveIncoming(
cip.XORKeyStream(typeHash, typeHash)
}
- // get message padding and type based on information from S1-S4 and H1-H4
- msgSize, msgType, padding := device.DeterminePacketTypeAndPadding(packet, typeHash)
+ // A transport packet can also match a ranged H1-H3 value when random
+ // handshake trailers are enabled. Prefer an H4 candidate only when its
+ // decrypted receiver index belongs to this device; otherwise retain the
+ // normal handshake classification. This avoids silently sending valid
+ // transport packets to the handshake queue where MAC1 rejects them.
+ msgSize, msgType, padding := 0, uint32(MessageUnknownType), uint32(0)
+ if transportPadding, ok := device.DetermineTransportCandidate(packet); ok {
+ msgSize, msgType, padding = MessageTransportSize, MessageTransportType, transportPadding
+ } else {
+ // get message padding and type based on information from S1-S4 and H1-H4
+ msgSize, msgType, padding = device.DeterminePacketTypeAndPadding(packet, typeHash)
+ }
packet = packet[padding:]
if msgType != MessageTransportType {
@@ -590,6 +600,42 @@ func applyHash(dst, src, hash []byte) {
}
}
+// DetermineTransportCandidate recognizes transport packets before the ranged
+// handshake classifier. A range match by itself is ambiguous, so also require
+// the decrypted receiver index to identify a keypair owned by this device.
+func (device *Device) DetermineTransportCandidate(packet []byte) (uint32, bool) {
+ padding := device.paddings.transport.Load()
+ if len(packet) < int(padding)+MessageTransportSize {
+ return 0, false
+ }
+
+ header := make([]byte, MessageTransportHeaderSize)
+ copy(header, packet[padding:padding+MessageTransportHeaderSize])
+
+ cip, err := device.HeaderProtectionCipher(packet[:HeaderCipherNonceSize])
+ if err != nil {
+ return 0, false
+ }
+ if cip != nil {
+ cip.XORKeyStream(header, header)
+ }
+
+ transportHeader := device.headers.transport.Load()
+ if !transportHeader.Contains(binary.LittleEndian.Uint32(header[:4])) {
+ return 0, false
+ }
+
+ receiver := binary.LittleEndian.Uint32(
+ header[MessageTransportOffsetReceiver:MessageTransportOffsetCounter],
+ )
+ value := device.indexTable.Lookup(receiver)
+ if value.keypair == nil || value.peer == nil {
+ return 0, false
+ }
+
+ return padding, true
+}
+
func (device *Device) DeterminePacketTypeAndPadding(packet []byte, typeHash []byte) (int, uint32, uint32) {
var headerBytes [4]byte
var padding uint32
@@ -0,0 +1,17 @@
diff --git a/wg-quick/linux.bash b/wg-quick/linux.bash
index 20c820d..af0aef0 100755
--- a/wg-quick/linux.bash
+++ b/wg-quick/linux.bash
@@ -87,7 +87,11 @@ auto_su() {
add_if() {
local ret
- if ! cmd ip link add "$INTERFACE" type amneziawg; then
+ if [[ ${WG_QUICK_FORCE_USERSPACE_IMPLEMENTATION:-0} == 1 ]]; then
+ command -v "${WG_QUICK_USERSPACE_IMPLEMENTATION:-amneziawg-go}" >/dev/null || exit 1
+ echo "[!] Using the userspace AmneziaWG implementation for this configuration." >&2
+ cmd "${WG_QUICK_USERSPACE_IMPLEMENTATION:-amneziawg-go}" "$INTERFACE"
+ elif ! cmd ip link add "$INTERFACE" type amneziawg; then
ret=$?
[[ -e /sys/module/amneziawg ]] || ! command -v "${WG_QUICK_USERSPACE_IMPLEMENTATION:-amneziawg-go}" >/dev/null && exit $ret
echo "[!] Missing WireGuard (Amnezia VPN) kernel module. Falling back to slow userspace implementation." >&2