Configuring the tunnel and getting the MTU right
Two pull requests this week, both about the TUN device. The first lets govpn configure the device itself. The second sets its MTU, which turned out to be the more interesting problem.
Configuring the device
Until now, govpn created the TUN device and left the rest to someone else: the Docker entrypoint or a few ip commands typed by hand. With this change, a new required -address flag takes the tunnel address with its prefix length, like 10.0.0.1/24, and govpn assigns it and brings the device up before forwarding anything.
Configure shells out to ip. A netlink library would avoid the external command, but ip is on every Linux machine I care about, and calling it keeps the code short and easy to check by hand. When a command fails, the error includes its output, so a failure says why instead of just “exit status 2”.
The flag is validated before any device is created:
if p.Bits() == 32 {
return fmt.Errorf("/32 leaves no room for the peer")
}
if p.Bits() <= 30 && p.Masked() == p {
return fmt.Errorf("%s is a network address, use a host address such as %s",
p, netip.PrefixFrom(p.Addr().Next(), p.Bits()))
}
Using 10.0.0.0/24 by mistake now fails with a message suggesting 10.0.0.1/24. The <= 30 check skips /31, where both addresses are valid hosts. These rules got their own table-driven tests.
The Docker entrypoint got simpler too. It no longer assigns the address. It waits for tun0 to be up and only adds the route to the other peer’s subnet.
Why the MTU matters
Every packet that enters the tunnel gets wrapped in more headers before it goes out on the real network. If the TUN device accepts 1500-byte packets, the wrapped packet ends up bigger than 1500 bytes and no longer fits on a standard Ethernet link. It either gets fragmented or dropped.
The fix is to set the TUN MTU low enough that the outer packet always fits:
const (
ethernetMTU = 1500
ipv6Size = 40
udpOverhead = 8
cryptoOverhead = 32 // 16 bytes header + 16 bytes tag
tunnelMTU = ethernetMTU - ipv6Size - udpOverhead - cryptoOverhead
udpPayloadSize = ethernetMTU - ipv6Size - udpOverhead
)
Some choices worth explaining:
- IPv6, not IPv4. The outer header could be either, and IPv6 is 20 bytes larger. Using the worst case means the MTU stays valid if the peers ever talk over IPv6.
- 32 bytes for encryption, before encryption exists. I already know the plan is a 16-byte header plus a 16-byte authentication tag. Reserving the space now means the MTU won’t change later, and anything that depends on it won’t either.
- Buffers sized from the same constants. The receive buffer is
udpPayloadSize, the largest datagram a peer should ever send.
The result is 1500 - 40 - 8 - 32 = 1420, the same value WireGuard uses by default.
Checking it
ping can set the “don’t fragment” bit and choose the payload size. 1392 bytes of ICMP payload, plus 8 bytes of ICMP header and 20 of IPv4 header, is exactly 1420:
sudo ip netns exec A ping -c1 -M do -s 1392 10.0.0.2
sudo ip netns exec A ping -c1 -M do -s 1393 10.0.0.2
The first ping gets a reply. The second is rejected locally with message too long, which is what should happen: the kernel refuses to put a packet bigger than the MTU into the tunnel.
What’s next
The tunnel is configured and sized, and the 32 bytes are waiting to be used. Next is encryption.