WSL networking - fordsfords/fordsfords.github.io GitHub Wiki

Some experiments networking between the Windows and Linux instances (WSL2).

The Windows side has an interface onto an internal 172 network. And, of course, it has an interface onto the active laptop's NIC. With just this, a process on Win can talk to a process on Linux. However, this won't let Linux access hosts off of the laptop. And you can't do normal routing between those two networks because 172 isn't routed. So Win runs a NAT between the interfaces. This NAT is typically not used when a Win process is communicating with a Linux process since both processes have an IP network in common.

win_nat1

The entirety of the above diagram is in the laptop. The Windows Process and the NAT are running under the Windows OS, and therefore share the two interfaces supplied by Windows. The WSL2 Linux host is a VM connected to the Windows OS via the 172 internal network.

So processes running on Windows can communicate directly with Linux processes simply because both processes have direct access to the 172 network. Even multicast works.

But if a Linux process wants to access something off of the laptop, like the public Internet, the NAT is used to access the external network.

Interfaces

Win:

C:> cd ...\GitHub\mtools\Win64
C:> ipconfig
...
Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . : xxx
   Link-local IPv6 Address . . . . . : xxx
   IPv4 Address. . . . . . . . . . . : 192.168.1.211
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1
...
Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::c3f:a502:abb0:594b%48
   IPv4 Address. . . . . . . . . . . : 172.31.0.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :

Linux:

$ cd .../GitHub/mtools/Linux64
$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.31.8.31  netmask 255.255.240.0  broadcast 172.31.15.255
        inet6 fe80::215:5dff:fe4e:6bf1  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:4e:6b:f1  txqueuelen 1000  (Ethernet)

TCP, Win Listener, Linux Initiator

Win:

C:> cd .../GitHub/mtools/Win64
C:> mdump -t 0.0.0.0 12000 172.31.0.1
Equiv cmd line: mdump -p0 -Q0 -r4194304 -t 0.0.0.0 12000 172.31.0.1
13:50:29.899000 172.31.8.31.58856 80 bytes:
65 63 68 6f 20 73 65 6e 64 65 72 20 65 71 75 69         echo sender equi
76 20 63 6d 64 3a 20 6d 73 65 6e 64 20 2d 62 31         v cmd: msend -b1
20 2d 6d 30 20 2d 6e 30 20 2d 70 31 30 30 30 20          -m0 -n0 -p1000
2d 73 30 20 2d 53 36 35 35 33 36 20 2d 74 20 31         -s0 -S65536 -t 1
37 32 2e 33 31 2e 30 2e 31 20 31 32 30 30 30 00         72.31.0.1 12000.

echo sender equiv cmd: msend -b1 -m0 -n0 -p1000 -s0 -S65536 -t 172.31.0.1 12000
13:50:30.892000 172.31.8.31.58856 9 bytes:
4d 65 73 73 61 67 65 20 30                              Message 0
13:50:31.893000 172.31.8.31.58856 9 bytes:
4d 65 73 73 61 67 65 20 31                              Message 1
13:50:32.893000 172.31.8.31.58856 9 bytes:
4d 65 73 73 61 67 65 20 32                              Message 2
EOF

Linux:

$ ./msend -t 172.31.0.1 12000
Equiv cmd line: msend -b1 -m0 -n0 -p1000 -s0 -S65536 -t 172.31.0.1 12000
Sending 9 bytes
Sending 9 bytes
Sending 9 bytes
^C

Killed with control-C.

TCP, Linux Listener, Win Initiator

Linux:

$ ./mdump -t 0.0.0.0 12000 172.31.8.31
Equiv cmd line: mdump -p0 -Q0 -r4194304 -t 0.0.0.0 12000 172.31.8.31
WARNING: tried to set SO_RCVBUF to 4194304, only got 425984
14:03:03.776492 172.31.0.1.63878 81 bytes:
65 63 68 6f 20 73 65 6e 64 65 72 20 65 71 75 69         echo sender equi
76 20 63 6d 64 3a 20 6d 73 65 6e 64 20 2d 62 31         v cmd: msend -b1
20 2d 6d 30 20 2d 6e 30 20 2d 70 31 30 30 30 20          -m0 -n0 -p1000
2d 73 30 20 2d 53 36 35 35 33 36 20 2d 74 20 31         -s0 -S65536 -t 1
37 32 2e 33 31 2e 38 2e 33 31 20 31 32 30 30 30         72.31.8.31 12000
00                                                      .
echo sender equiv cmd: msend -b1 -m0 -n0 -p1000 -s0 -S65536 -t 172.31.8.31 12000
14:03:04.792035 172.31.0.1.63878 9 bytes:
4d 65 73 73 61 67 65 20 30                              Message 0
14:03:05.804978 172.31.0.1.63878 9 bytes:
4d 65 73 73 61 67 65 20 31                              Message 1
14:03:06.819955 172.31.0.1.63878 9 bytes:
4d 65 73 73 61 67 65 20 32                              Message 2
ERROR: recv: Connection reset by peer

Win:

C:> msend -t 172.31.8.31 12000
Equiv cmd line: msend -b1 -m0 -n0 -p1000 -s0 -S65536 -t 172.31.8.31 12000
Sending 9 bytes
Sending 9 bytes
Sending 9 bytes
^C

Killed with control-C.

UDP, Win Receiver, Linux Sender

Win:

C:> mdump 0.0.0.0 12000 172.31.0.1
Equiv cmd line: mdump -p0 -Q0 -r4194304 0.0.0.0 12000 172.31.0.1
15:00:20.547000 172.31.8.31.33448 80 bytes:
65 63 68 6f 20 73 65 6e 64 65 72 20 65 71 75 69         echo sender equi
76 20 63 6d 64 3a 20 6d 73 65 6e 64 20 2d 62 31         v cmd: msend -b1
20 2d 6d 30 20 2d 6e 30 20 2d 70 31 30 30 30 20          -m0 -n0 -p1000
2d 73 30 20 2d 53 36 35 35 33 36 20 2d 75 20 31         -s0 -S65536 -u 1
37 32 2e 33 31 2e 30 2e 31 20 31 32 30 30 30 00         72.31.0.1 12000.

echo sender equiv cmd: msend -b1 -m0 -n0 -p1000 -s0 -S65536 -u 172.31.0.1 12000
15:00:21.545000 172.31.8.31.33448 9 bytes:
4d 65 73 73 61 67 65 20 30                              Message 0
15:00:22.545000 172.31.8.31.33448 9 bytes:
4d 65 73 73 61 67 65 20 31                              Message 1
15:00:23.545000 172.31.8.31.33448 9 bytes:
4d 65 73 73 61 67 65 20 32                              Message 2
^C

Linux:

$ ./msend -u 172.31.0.1 12000
Equiv cmd line: msend -b1 -m0 -n0 -p1000 -s0 -S65536 -u 172.31.0.1 12000
Sending 9 bytes
Sending 9 bytes
Sending 9 bytes
^C

UDP, Linux Receiver, Win Sender

Linux:

$ ./mdump 0.0.0.0 12000 172.31.8.31
Equiv cmd line: mdump -p0 -Q0 -r4194304 0.0.0.0 12000 172.31.8.31
WARNING: tried to set SO_RCVBUF to 4194304, only got 425984
15:03:14.408952 172.31.0.1.49982 81 bytes:
65 63 68 6f 20 73 65 6e 64 65 72 20 65 71 75 69         echo sender equi
76 20 63 6d 64 3a 20 6d 73 65 6e 64 20 2d 62 31         v cmd: msend -b1
20 2d 6d 30 20 2d 6e 30 20 2d 70 31 30 30 30 20          -m0 -n0 -p1000
2d 73 30 20 2d 53 36 35 35 33 36 20 2d 75 20 31         -s0 -S65536 -u 1
37 32 2e 33 31 2e 38 2e 33 31 20 31 32 30 30 30         72.31.8.31 12000
00                                                      .
echo sender equiv cmd: msend -b1 -m0 -n0 -p1000 -s0 -S65536 -u 172.31.8.31 12000
15:03:15.420070 172.31.0.1.49982 9 bytes:
4d 65 73 73 61 67 65 20 30                              Message 0
15:03:16.430355 172.31.0.1.49982 9 bytes:
4d 65 73 73 61 67 65 20 31                              Message 1
15:03:17.444740 172.31.0.1.49982 9 bytes:
4d 65 73 73 61 67 65 20 32                              Message 2
^C

The Warning indicates that Linux needs a larger maximum socket buffer. See this.

Win:

C:> msend -u 172.31.8.31 12000
Equiv cmd line: msend -b1 -m0 -n0 -p1000 -s0 -S65536 -u 172.31.8.31 12000
Sending 9 bytes
Sending 9 bytes
Sending 9 bytes
^C

Multicast, Win Receiver, Linux Sender

Win:

C:> mdump 239.101.3.10 12000 172.31.0.1
Equiv cmd line: mdump -p0 -Q0 -r4194304 239.101.3.10 12000 172.31.0.1
15:06:54.587000 172.31.8.31.39807 94 bytes:
65 63 68 6f 20 73 65 6e 64 65 72 20 65 71 75 69         echo sender equi
76 20 63 6d 64 3a 20 6d 73 65 6e 64 20 2d 62 31         v cmd: msend -b1
20 2d 6d 30 20 2d 6e 30 20 2d 70 31 30 30 30 20          -m0 -n0 -p1000
2d 73 30 20 2d 53 36 35 35 33 36 20 32 33 39 2e         -s0 -S65536 239.
31 30 31 2e 33 2e 31 30 20 31 32 30 30 30 20 31         101.3.10 12000 1
35 20 31 37 32 2e 33 31 2e 38 2e 33 31 00               5 172.31.8.31.
echo sender equiv cmd: msend -b1 -m0 -n0 -p1000 -s0 -S65536 239.101.3.10 12000 15 172.31.8.31
15:06:55.586000 172.31.8.31.39807 9 bytes:
4d 65 73 73 61 67 65 20 30                              Message 0
15:06:56.586000 172.31.8.31.39807 9 bytes:
4d 65 73 73 61 67 65 20 31                              Message 1
15:06:57.587000 172.31.8.31.39807 9 bytes:
4d 65 73 73 61 67 65 20 32                              Message 2
^C

Linux:

$ ./msend 239.101.3.10 12000 15 172.31.8.31
Equiv cmd line: msend -b1 -m0 -n0 -p1000 -s0 -S65536 239.101.3.10 12000 15 172.31.8.31
Sending 9 bytes
Sending 9 bytes
Sending 9 bytes
^C

Multicast, Linux Receiver, Win Sender

$ ./mdump 239.101.3.10 12000 172.31.8.31
Equiv cmd line: mdump -p0 -Q0 -r4194304 239.101.3.10 12000 172.31.8.31
WARNING: tried to set SO_RCVBUF to 4194304, only got 425984
15:09:05.887424 172.31.0.1.53773 93 bytes:
65 63 68 6f 20 73 65 6e 64 65 72 20 65 71 75 69         echo sender equi
76 20 63 6d 64 3a 20 6d 73 65 6e 64 20 2d 62 31         v cmd: msend -b1
20 2d 6d 30 20 2d 6e 30 20 2d 70 31 30 30 30 20          -m0 -n0 -p1000
2d 73 30 20 2d 53 36 35 35 33 36 20 32 33 39 2e         -s0 -S65536 239.
31 30 31 2e 33 2e 31 30 20 31 32 30 30 30 20 31         101.3.10 12000 1
35 20 31 37 32 2e 33 31 2e 30 2e 31 00                  5 172.31.0.1.
echo sender equiv cmd: msend -b1 -m0 -n0 -p1000 -s0 -S65536 239.101.3.10 12000 15 172.31.0.1
15:09:06.894083 172.31.0.1.53773 9 bytes:
4d 65 73 73 61 67 65 20 30                              Message 0
15:09:07.910453 172.31.0.1.53773 9 bytes:
4d 65 73 73 61 67 65 20 31                              Message 1
15:09:08.921685 172.31.0.1.53773 9 bytes:
4d 65 73 73 61 67 65 20 32                              Message 2
^C

The Warning indicates that Linux needs a larger maximum socket buffer. See this.

Win:

C:> msend 239.101.3.10 12000 15 172.31.0.1
Equiv cmd line: msend -b1 -m0 -n0 -p1000 -s0 -S65536 239.101.3.10 12000 15 172.31.0.1
Sending 9 bytes
Sending 9 bytes
Sending 9 bytes
^C

NAT

The above allows a process on the Win side to talk to a process on the Linux side without trouble. But to get off-box you have to leverage the NAT on the Windows side that routes between the two interfaces.

⚠️ **GitHub.com Fallback** ⚠️