WSL: Unable to use WSL2 whilst connected to VPN (full 172.16.0.0/12 address space used on VPN)

Environment

Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.19041.0 Microsoft Windows NT 10.0.19041.0

lsb_release -r
Release:        20.04
cat /proc/version
Linux version 4.19.104-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Wed Feb 19 06:37:35 UTC 2020
WSL2

Steps to reproduce

  1. Boot machine to clear all pre-existing Hyper-V Virtual Ethernet Adaptors
  2. Start wsl from windows (in powershell type wsl)
  3. Notice that a new Hyper-V Virtual Ethernet Adaptor called vEthernet (WSL) has been created, with an IP address in the 172.16.0.0/12 CIDR range.
  4. In the WSL2 shell prompt (i.e. in linux) confirm that networking is available by executing nc -vz www.google.com 80 - you should see Connection to www.google.com 80 port [tcp/http] succeeded!
  5. Back in windows start VPN service (Cisco AnyConnect in this case) and login to VPN
  6. Now try the same network connectivity check in linux again - this time rather than connecting to www.google.com on port 80 it simply hangs.

WSL logs:

Expected behavior

The expected behaviour is that WSL networking should work correctly whilst the machine is VPN’d into an external VPN network and all traffic from the WSL instance should be capable of being routed to the correct place, even in the presence of an external network that assumes it owns the VPN space of 172.16.0.0/12

Actual behavior

Whilst the VPN adaptor is active the WSL instances cannot connect to any network at all - neither the host machine, nor anything else that the host machine can connect to.

The reason this hangs is that the network that this VPN service connects to really has machines all across the 172.16.0.0/12 CIDR range. Thus to deal with this the VPN client (correctly) inserts routes in the windows routing table to ensure that traffic to this CIDR range is correctly routed. This looks like

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
      172.19.80.0    255.255.240.0         On-link       172.19.80.1   5256
      172.19.80.0    255.255.240.0     172.16.239.1    172.16.239.36      2
      172.19.80.1  255.255.255.255         On-link       172.19.80.1   5256
    172.19.95.255  255.255.255.255         On-link       172.19.80.1   5256

Notice the additional route with metric 2 that has been added to ensure that all traffic on this virtual adaptor should be routed to 172.16.239.36 (the VPN adaptor) since there are machines with those IP addresses on the VPN network. This route accidentally ensures that all traffic from the WSL instance doesn’t go anywhere since the VPN doesn’t know how to deal with it (and it probably doesn’t even get that far since its probably lost somewhere in the kernel).

The problem really stems from the user (me) having no control over the address and range that the Hyper-V Virtual Ethernet Adaptor chooses when WSL starts up and helping it avoid an IP range clash with the VPN it is going to be connected to. For example, I know that I am never going to interact with the 10.0.0.0/8 range so would use that as the private IP range for networking.

To be completely transparent I have a very kludgey workaround that I’m currently using, as follows:

  1. Before starting WSL on windows, start a powershell as an administrator
  2. Find the InterfaceIndex of the “Windows Sandbox” Virtual Network Adaptor using Get-NetAdaptor - this too is in the 172.16.0.0/12 range
  3. Set the IP address on this adaptor to be the same is its current address, but change the PrefixLength to 12 using Set-NetIPAddress -InterfaceIndex NN 172.19.80.1 -PrefixLength 12
  4. Now start WSL so that it acquires an IP address in the 192.168.0.0\16 block
  5. Reset the PrefixLength on the Virtual Adaptor Set-NetIPAddress -InterfaceIndex NN 172.19.80.1 -PrefixLength 20

I need to remember to undertake this procedure every time I boot my machine, and it is a very poor-mans way of simply controlling the IP address that the WSL Hyper-V Virtual Ethernet Adaptor acquires.

REQUEST: Surely there is some internal registry entry or some such configuration that allows people in this situation to at least set the CIDR range for the auto-created Hyper-V Virtual Ethernet Adaptor?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 28
  • Comments: 25 (1 by maintainers)

Most upvoted comments

Have the same issue, where WSL2 subnet is messing with VPN network. The following steps have helped to set it in a working state.

  1. Reboot the machine
  2. Connect to the VPN
  3. Start WSL

This way, WSL chooses a subnet outside the VPN network.

+1

PARTIAL WORKAROUND FOR SOME PEOPLE

Based very heavily on the suggestions on @ec-max and with the following caveats

  • this ONLY controls the IP address range of the WSL 2 virtual network adaptor (i.e. no other Hyper-V adaptors get messed with)
  • the resulting CIDR range of the WSL virtual adaptor will be somewhere in the 192.168.0.0/24 range

Do the following:

  1. Use DeviceManager to add some legacy hardware to your machine - what you want to add is a Microsoft KM-TEST Loopback Adapter
  2. Navigate to your Network Connections page (I use right click on current network connection in bottom right are of desktop, which takes me to the “Settings - Network Status” page, followed by click on “Change Adaptor Options”)
  3. Right click on your New Adaptor and rename it to Hyper-V Fix
  4. Right click to select Properties and set its IP address to 172.16.1.1 and its subnet mask to 255.240.0.0
  5. Right click to Disable this adaptor
  6. Using a text editor that is running as Administrator so that you can save to C:\Windows\System32 (it is probably better to save this file in a common admin area) create a file called startWSLAfterBoot.ps1 with the following lines of code
netsh interface set interface "Hyper-V Fix" enable
wsl -e pwd
netsh interface set interface "Hyper-V Fix" disable
  1. In TaskScheduler create a new task called WSL Get NON-VPN IP Address, tick the “Run with highest priviledges”, set the trigger to be at logon (of yourself if only for you, or for all users if for all), and with an action to run C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe with arguments -ExecutionPolicy Bypass -File "C:\Windows\System32\startWSLAfterBoot.ps1"
  2. Assuming you have not yet started WSL after a reboot you can execute ipconfig /all to see all your enabled network adaptors, which should not contain your WSL Hyper-V adaptor.
  3. Right-click on the task you just created and select “Run” - this will test that your new task works - hopefully it will
  4. After running you can either start WSL and see what your internal IP address is (using ifconfig) or on the host you can run ipconfig /all and see that the WSL adaptor has acquired a 192.168.0.0/24 address
  5. If you are particularly paranoid (which you probably should be!) then you can now attempt a reboot and logon … during logon you should see a powershell pop-up and go away, and if you now start WSL you should get the correct IP addresses

I have been struggling with this since WSL2 was released. Even before that, really, since the same problems crop up when running Linux VMs on Windows 10 Hyper-V.

The workaround suggested by @ec-max is the first one that I have tried that actually works consistently. What would make it better? Automation!

I just developed a script to automate the outlined process. I did this for some of my teammates who would rather not bother with loopback adapters and the Windows task scheduler, but I think the script should work for pretty much anyone who is capable of running a PowerShell script:

https://github.com/jgregmac/hyperv-fix-for-devs

I’ve expanded on the very kludgey workaround to use a separate placebo adapter to control the automagic behavior. If you go to device manager and manually add a network adapter you can select Microsoft KM-TEST Loopback Adapter which is a nice built in network driver that sends outgoing packets to /dev/null and doesn’t tie up resources for incoming packets. I’ve set this adapter to 172.16.0.1 with the netmask 255.240.0.0 and have to make sure it is enabled before I reboot the computer and disabled after a user logs into the machine (disabling it at startup is too quick for hyper-v). I named this network interface “Hyper-V Fix” and used Task Scheduler to run netsh interface set interface "Hyper-V Fix" enable at shutdown or to run netsh interface set interface "Hyper-V Fix" disable at user login. Shutdown isn’t built into Task Scheduler so I used the trigger on Event : System : User32 : 1074 and task scheduler has to be set to run the task with the highest privileges. I also let this task run even when not on AC power since this was setup on my laptop.

I was almost happy (happy?) with this workaround and it works great for Hyper-V… But… the WSL adapter doesn’t get created until WSL starts; not at system boot or even at user login. So I still have to make sure it is enabled before launching something in WSL for the first time (each boot). Because of this; I’ve actually disabled the task scheduler tasks and for now make sure I enable the adapter before rebooting and disable it before connecting to the AnyConnect VPN client. So close; yet so far.

I hope this helps someone else struggling with this until it can get a real fix.

Edit: I updated the scheduled tasks to launch WSL2 automatically. Added a task to run at system startup which executes C:\Windows\System32\bash.exe -c "pwd" which was about the most low impact command I could think of to make it run the shell and terminate. With these three tasks I now have the “Hyper-V Fix” adapter enable at system shutdown; the Hyper-V Default and WSL virtual switches get created at system startup, and the “Hyper-V Fix” adapter disable itself at log on of any user. This still isn’t great for traveling to the office; where the wireless network may fall into the 172.16.0.0/12 range; but it is a viable workaround for machines that are remote. One last note; tasks scheduled for startup/shutdown were set to run whether user is logged on or not; and set to not store password.

Ref #5275 ➡️ #4601 ➡️ #4210.

I’ve been chasing this issue with WSL 2 and Hyper-V on Windows 10. It’s pretty clear MS uses 10.0.0.0/8 as their RFC1918 private space but need to give options for organizations that used something else. The ideal solution would be to present an “advanced” option on the virtual switch properties with checkboxes for 10/8 prefix, 172.16/12 prefix and 192.168/16 prefix, and maybe a fourth option for custom range; and just let users uncheck ranges that are incompatible with their VPN connectivity requirements. This is a major issue as people install something like docker desktop which enables WSL and suddenly things stop working.

Please; do something about this asap! 😃

@ec-max That is an ingenious solution - I had struggled with how to create some network adaptor that I could bring up with a specific IP address and CIDR range. I shall now try something like your proposed solution. In my mind I’m going to create a powershell script called startWSLForTheFirstTime.ps which will enable this dummy adaptor and take the whole 172.16.0.0/12 CIDR range, call a minimal wsl command to create the virtual network adaptor, and then take the dummy adaptor down again. Thanks!

Glad I could be of some help. Interested to see what you come up with!

The solution presented by @roshantm only works if the VPN adaptor ONLY routes traffic to the specific CIDR range allocated to that adaptor. In my case there are CIDR ranges outside the VPN adaptors specific subnet that it also routes and unfortunately the Hyper-V Virtual Ethernet Adaptor is taking an address in that range.

@ec-max That is an ingenious solution - I had struggled with how to create some network adaptor that I could bring up with a specific IP address and CIDR range. I shall now try something like your proposed solution. In my mind I’m going to create a powershell script called startWSLForTheFirstTime.ps which will enable this dummy adaptor and take the whole 172.16.0.0/12 CIDR range, call a minimal wsl command to create the virtual network adaptor, and then take the dummy adaptor down again. Thanks!

I am also having this issue. @roshantm’s workaround works for me but we need a real solution.