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
- Boot machine to clear all pre-existing Hyper-V Virtual Ethernet Adaptors
- Start wsl from windows (in powershell type
wsl) - Notice that a new Hyper-V Virtual Ethernet Adaptor called
vEthernet (WSL)has been created, with an IP address in the172.16.0.0/12CIDR range. - In the WSL2 shell prompt (i.e. in linux) confirm that networking is available by executing
nc -vz www.google.com 80- you should seeConnection to www.google.com 80 port [tcp/http] succeeded! - Back in windows start VPN service (Cisco AnyConnect in this case) and login to VPN
- 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:
- Before starting WSL on windows, start a powershell as an administrator
- Find the
InterfaceIndexof the “Windows Sandbox” Virtual Network Adaptor usingGet-NetAdaptor- this too is in the172.16.0.0/12range - Set the IP address on this adaptor to be the same is its current address, but change the
PrefixLengthto 12 usingSet-NetIPAddress -InterfaceIndex NN 172.19.80.1 -PrefixLength 12 - Now start WSL so that it acquires an IP address in the
192.168.0.0\16block - Reset the
PrefixLengthon the Virtual AdaptorSet-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)
Have the same issue, where WSL2 subnet is messing with VPN network. The following steps have helped to set it in a working state.
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
Do the following:
DeviceManagerto add some legacy hardware to your machine - what you want to add is aMicrosoft KM-TEST Loopback AdapterHyper-V Fix172.16.1.1and its subnet mask to255.240.0.0C:\Windows\System32(it is probably better to save this file in a common admin area) create a file calledstartWSLAfterBoot.ps1with the following lines of codeWSL 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 runC:\Windows\System32\WindowsPowerShell\v1.0\powershell.exewith arguments-ExecutionPolicy Bypass -File "C:\Windows\System32\startWSLAfterBoot.ps1"ipconfig /allto see all your enabled network adaptors, which should not contain your WSL Hyper-V adaptor.ifconfig) or on the host you can runipconfig /alland see that the WSL adaptor has acquired a192.168.0.0/24addressI 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 Adapterwhich 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 to172.16.0.1with the netmask255.240.0.0and 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 runnetsh interface set interface "Hyper-V Fix" enableat shutdown or to runnetsh interface set interface "Hyper-V Fix" disableat user login. Shutdown isn’t built into Task Scheduler so I used the triggeron Event : System : User32 : 1074and 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! 😃
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.pswhich will enable this dummy adaptor and take the whole172.16.0.0/12CIDR 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.