WireGuard and MTU/MSS: Prevent Packet Loss on Your Linux Router

Optimize your WireGuard VPN performance by understanding and configuring MTU (Maximum Transmission Unit) and MSS (Maximum Segment Size) on your Linux router. This guide explains how to prevent packet loss when routing traffic between physical and virtual interfaces.

WireGuard is a fast and efficient VPN solution, but it introduces complexities when routing traffic between different network interfaces. This is because WireGuard encapsulates packets, increasing their size. If the encapsulated packet exceeds the MTU of any network link along its path, it gets fragmented, leading to performance issues and potential packet loss. This guide provides a clear explanation of MTU and MSS, and how to configure them correctly for optimal WireGuard performance.

What are MTU and MSS?

Why are MTU and MSS Important for WireGuard?

WireGuard's encapsulation adds overhead to packets. If the resulting packet size exceeds the MTU of any link (including the tunnel itself), fragmentation occurs. This can cause:

Calculating MTU and MSS

Physical Interface:

Virtual Interface (WireGuard):

MSS Clamping: Preventing Packet Loss

MSS clamping is crucial when routing between interfaces with different MTUs. It prevents your router from sending packets that are too large for the receiving interface.

Example:

Without clamping, packets between 1360 and 1460 bytes would be accepted by the router but dropped by the receiving device.

How to Implement MSS Clamping:

Using iptables:

iptables -A FORWARD -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360
ip6tables -A FORWARD -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360

iptables -A OUTPUT -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360
ip6tables -A OUTPUT -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360

Using WireGuard Configuration:

[Interface]
...
PostUp = ip6tables -A FORWARD -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360 && ip6tables -A OUTPUT -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360 && iptables -A FORWARD -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360 && iptables -A OUTPUT -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360
PostDown = ip6tables -D FORWARD -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360 && ip6tables -D OUTPUT -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360 && iptables -D FORWARD -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360 && iptables -D OUTPUT -o <device_name> -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360

Remember to replace with your WireGuard interface name.

By understanding and correctly configuring MTU and MSS, you can ensure your WireGuard VPN performs optimally and avoids packet loss, providing a smooth and reliable network experience.