Skip to content

Instantly share code, notes, and snippets.

@ddrown
Created May 26, 2023 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddrown/dac2359afd2121dd11357e59607b70fc to your computer and use it in GitHub Desktop.
Save ddrown/dac2359afd2121dd11357e59607b70fc to your computer and use it in GitHub Desktop.
IPv6 for WSL2
powershell -ExecutionPolicy Unrestricted .\wsl2-ipv6.ps1
{
"prefix": "2001:db8:200:300::"
}
# modified from https://github.com/shigenobuokamoto/wsl2ipv6 which uses DHCPv6-PD
# launch in privileged mode
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "-executionpolicy unrestricted & '" +$myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
# config file expected to be in your home directory + \bin\
$configfile = $env:USERPROFILE + "\bin\wsl2-ipv6.json"
if (-NOT (Test-Path $configfile)) {
echo "config file $configfile not found"
pause
Break
}
$up = Get-NetRoute -DestinationPrefix ::/0
$wsl = Get-NetIPInterface -AddressFamily IPv6 | where {$_.InterfaceAlias -match '(WSL)'}
$config = Get-Content $configfile | ConvertFrom-Json
# prefix in the form "fe80::"
$prefix = $config.prefix
# windows stores this address in the registry, and it takes extra steps to change it
$local = ":1"
$remote = [system.String]::Join(":", (1..4 | % {(Get-Random -Maximum 65535).toString("X")}))
$subnet = $prefix -replace "::$", ":"
echo "local (windows) ${subnet}${local}"
echo "remote (wsl2) ${subnet}${remote}"
$routed = Get-NetRoute -DestinationPrefix ${prefix}/64 -Erroraction silentlycontinue
# if we don't have this subnet routed
if($routed.ifIndex -ne $wsl.ifIndex) {
Set-NetIPInterface -Forwarding Enabled -InterfaceIndex $up.ifIndex
Set-NetIPInterface -Dhcp Disabled -RouterDiscovery Disabled -InterfaceIndex $wsl.ifIndex
New-NetIPAddress -IPAddress ${subnet}${local} -PrefixLength 64 -InterfaceIndex $wsl.ifIndex -SkipAsSource $True -PolicyStore ActiveStore
Set-NetIPInterface -InterfaceIndex $wsl.ifIndex -Forwarding Enabled
}
# configure wsl network
wsl sudo ip -6 route del default dev eth0
wsl sudo ip -6 addr flush dev eth0 scope global
wsl sudo ip addr add ${subnet}${remote}/64 dev eth0
wsl sudo ip -6 route add default via ${subnet}${local} dev eth0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment