如何使用 Windows WSL 配置路由?这可能吗?

ini*_*_js 6 networking windows-firewall virtual-machine windows-subsystem-for-linux

根据 sysctl,我注意到 WSL(Windows 10pro 1709)中似乎启用了 IP 转发。我希望我可以用它在 WSL 上制作一个软件路由器——在同一台物理主机上处理 Hyper-V VM 流量。VM 和主机可以通过共享的 /24 ipv4 子网进行通信。

理想情况下,我想使用 WSL 来配置类似 iptables 的规则,或者使用某种可以重写流量的 RAW 套接字的软件路由器程序。

# on the WSL bash
$ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
Run Code Online (Sandbox Code Playgroud)

我的 Windows 主机和 VM 都在 192.168.4.X 中。192.168.4.215 是主机。192.168.4.228 是虚拟机。

我已向 Windows 防火墙添加了一条规则,以允许该子网上的每个传入 TCP 端口,并通过传输由 WSL 主机提供服务并由 VM 下载的网页来测试连接性。(例如python -m SimpleHTTPServer在 wsl 中运行并 curl http://192.168.2.215在 vm 上使用)

我已将 Windows 主机添加为 VM 内的默认网关:

# route add default gw 192.168.4.215
# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.4.215   0.0.0.0         UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.4.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
Run Code Online (Sandbox Code Playgroud)

但是我在 VM 上启动的 TCP 连接没有被 Windows 路由(例如curl http://<publicip>。)。无法连接

我还需要在 Windows 中取消阻止什么?

编辑

我已经有点放弃了。因为我想要对 VM 流量做的就是通过主机的 VPN 隧道对其进行 NAT,所以我最终使用 Powershell 创建了一个特殊的 NAT 接口:

https://serverfault.com/questions/911578/windows-10-pro-as-a-nat-between-two-interfaces