|
| 1 | +WireGuard over TCP |
| 2 | +------------------ |
| 3 | + |
| 4 | +We hate running one TCP implementation on top of another TCP implementation. |
| 5 | +There's problems with cascading retransmissions and head of line blocking, |
| 6 | +and performance is always much worse than a UDP based tunnel. |
| 7 | + |
| 8 | +However, we also recognize that several users need to run WireGuard over TCP. |
| 9 | +One reason is that UDP packets are sometimes blocked by the network in |
| 10 | +corporate scenarios or in other types of firewalls. Also, in misconfigured |
| 11 | +networks outside of the user's control, TCP may be more reliable than UDP. |
| 12 | + |
| 13 | +Additionally, we want TunSafe to be a drop-in replacement for OpenVPN, which |
| 14 | +also supports TCP based tunneling. The feature could also be used to run |
| 15 | +WireGuard tunnels over ssh tunnels, or through socks/https proxies. |
| 16 | + |
| 17 | +The TunSafe project therefore takes the pragmatic approach of supporting |
| 18 | +WireGuard over TCP, while discouraging its use. We absolutely don't want |
| 19 | +people to start using TCP by default. It's meant to be used only in the |
| 20 | +extreme cases when nothing else is working. |
| 21 | + |
| 22 | +We've added experimental support for TCP in the latest TunSafe master, |
| 23 | +which means you can try this out on Windows, OSX, Linux, and FreeBSD. |
| 24 | +On the server side, to listen on a TCP port, use ListenPortTCP=1234. (Not |
| 25 | +working on Windows yet). On the clients, use Endpoint=tcp://5.5.5.5:1234. |
| 26 | +The code is still very experimental and untested, and is not recommended |
| 27 | +for general use. Once the code is more well tested, we'll also release |
| 28 | +support for connecting to WireGuard over TCP in our Android and iOS clients. |
| 29 | + |
| 30 | +To make the impact as small as possible to our WireGuard protocol handling, |
| 31 | +and to minimize the risk of security related issues, the TCP feature has been |
| 32 | +designed to be as self-contained as possible. When a packet comes in over |
| 33 | +TCP, it's sent over to the WireGuard protocol handler and treated as if it |
| 34 | +was a UDP packet, and vice versa. This means TCP support can also be supported |
| 35 | +in existing WireGuard deployments by using a separate process that converts |
| 36 | +TCP connections into UDP packets sent to the WireGuard Linux kernel module. |
| 37 | + |
| 38 | +Each packet over TCP is prefixed by a 2-byte big endian number, which contains |
| 39 | +the length of the packet's payload. The payload is then the actual WireGuard |
| 40 | +UDP packet. |
| 41 | + |
| 42 | +TCP has larger overhead than UDP, and we want to support the usual WireGuard |
| 43 | +MTU of 1420 without introducing extra packet "fragmenting". So we implemented |
| 44 | +an optimization to skip sending the 16-byte WireGuard header for every packet. |
| 45 | +TCP is a reliable connection, we know that sequence numbers are always |
| 46 | +monotonically increasing, so we can predict the contents of this header. |
| 47 | + |
| 48 | +Here's an example: |
| 49 | +A 1420 byte big packet sent over a WireGuard link will have 2 bytes of |
| 50 | +TCP payload length, 16 bytes of WireGuard headers, 16 bytes of WireGuard MAC, |
| 51 | +20 bytes of TCP headers, and 40 bytes of IPv6 headers. |
| 52 | +This is a total of 1420 + 2 + 16 + 16 + 20 + 40 = 1514 bytes, exceeding |
| 53 | +the usual 1500 byte Ethernet MTU by 14 bytes. This means that a single full |
| 54 | +sized packet over WireGuard will result in 2 TCP packets. With our |
| 55 | +optimization, we reduce this to 1498 bytes, so it fits in one TCP packet. |
| 56 | + |
| 57 | +Protocol specification |
| 58 | +---------------------- |
| 59 | + |
| 60 | +TT LLLLLL LLLLLLLL [Payload LL bytes] |
| 61 | +| | |
| 62 | +| \-- Payload length, high byte first. |
| 63 | +\----- Packet type |
| 64 | + |
| 65 | +The packet types (TT) currently defined are: |
| 66 | +TT = 00 = Normal The payload is a normal unmodified WireGuard packet |
| 67 | + including the regular WireGuard header. |
| 68 | + 01 = Reserved |
| 69 | + 10 = Data A WireGuard data packet (type 04) without the 16 byte |
| 70 | + header. The predicted header is prefixed to the payload. |
| 71 | + 11 = Control A TCP control packet. Currently this is used only to setup |
| 72 | + the header prediction. See below. |
| 73 | + |
| 74 | +There's only one defined Control packet, type 00 (SetKeyAndCounter): |
| 75 | + |
| 76 | + 0 1 2 3 |
| 77 | + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 78 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 79 | + |1 1| Length is 13 (14 bits) | 00 (8 bits) | |
| 80 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 81 | + | Key ID (32 bits) | |
| 82 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 83 | + | Counter (64 bits) ... |
| 84 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 85 | + |
| 86 | +This sets up the Key ID and Counter used for the Data packets. Then Counter |
| 87 | +is incremented by 1 for every such packet. |
| 88 | + |
| 89 | +For every Data packet, the predicted Key ID and Counter is expanded to a |
| 90 | +regular WireGuard data (type 04) header, which is prefixed to the payload: |
| 91 | + |
| 92 | + 0 1 2 3 |
| 93 | + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 94 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 95 | + | 04 (8 bits) | Reserved (24 bits) | |
| 96 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 97 | + | Key ID (32 bits) | |
| 98 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 99 | + | Counter (64 bits) ... |
| 100 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 101 | + | Data Payload (LL * 8 bits) ... |
| 102 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 103 | + |
| 104 | +This happens independently in each of the two TCP directions. |
0 commit comments