← Back to blog

Reading packets from a TUN device

The first few weeks went into the two building blocks of the VPN, a TUN device and a UDP transport, and into finding a sane way to test them.

The TUN device

I used the water library to create the device and wrapped it in a small tun package. Creating it needs root or CAP_NET_ADMIN, and after that it behaves like a file: every Read returns one IP packet the kernel routed into the interface, and every Write injects one.

The first version of govpn did nothing but print what it read:

buf := make([]byte, 1500)

for {
	n, err := dev.Read(buf)
	if err != nil {
		log.Fatalf("failed to read from tun device: %v", err)
	}
	fmt.Printf("Read %d bytes from tun\n", n)
	fmt.Printf("Content read: \"% x\"\n", buf[:n])
}

Give the interface an address, ping something in its subnet, and the output starts with 45: IP version 4, header length 5 words. That was the first real confirmation that I was getting raw IP packets.

Answering pings by hand

Before there was a second peer, I wanted to see a packet go in and a reply come out. So for a while govpn answered pings itself. It read an ICMP Echo Request from the TUN device, swapped the source and destination addresses, changed the type to Echo Reply, recomputed both checksums, and wrote the packet back.

It was throwaway code, but it taught me more about IPv4 headers than any reading had. The checksum is a ones’ complement sum of 16-bit words, and it’s easy to get wrong:

func checksum(data []byte) uint16 {
	var sum uint32
	for i := 0; i+1 < len(data); i += 2 {
		sum += uint32(data[i])<<8 | uint32(data[i+1])
	}
	if len(data)%2 == 1 {
		sum += uint32(data[len(data)-1]) << 8
	}
	for sum>>16 != 0 {
		sum = (sum & 0xFFFF) + (sum >> 16)
	}
	return ^uint16(sum)
}

The UDP transport

The transport package is a thin layer over net.UDPConn: it listens on a local port and sends to and receives from a single peer. To test it on its own I wrote udptest, a tiny tool that sends Ping to the peer every three seconds and prints whatever arrives.

Two machines in one laptop

Testing a VPN on one machine is misleading, because the kernel short-circuits traffic between local addresses. I needed two separate network stacks, and Linux network namespaces give you exactly that.

scripts/lab-up.sh creates two namespaces, A and B, and connects them with a veth pair, which works like a virtual cable:

ip netns add A
ip netns add B
ip link add vethA type veth peer name vethB
ip link set vethA netns A
ip link set vethB netns B
ip -n A addr add 192.168.50.1/24 dev vethA
ip -n B addr add 192.168.50.2/24 dev vethB

Each namespace has its own interfaces, routes, and sockets, so running a program with ip netns exec A is close enough to running it on another machine. With the lab in place I deleted the ping responder: from now on, a real peer answers.

What’s next

I have a TUN device that produces packets and a transport that moves bytes between two hosts. Next is connecting them, so packets read from one side come out on the other.