【问题标题】:Route traffic from internal network (eth1) through openvpn (tun0) [closed]通过openvpn(tun0)路由来自内部网络(eth1)的流量[关闭]
【发布时间】:2016-02-11 23:37:44
【问题描述】:

我在同一个内部网络 (eth1) 中有两台虚拟机,但只有一台(网关)实际连接到互联网(通过 eth0)并运行 openvpn(在 tun0 上)。

在 VM 网关中,我想通过 tun0 路由 eth1,但我无法做到。

这是实际设置:

     10.152.152.12                                    
     VM-workstation <===eth1===> VM-gateway <===eth0==> {internet}
                                      /\
                                      ||
                                      \\
                                       \===tun0===> {openvpn tunnel}

VM-gateway 内的网络配置

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.211.1.18     128.0.0.0       UG    0      0        0 tun0
0.0.0.0         10.0.2.2        0.0.0.0         UG    0      0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.152.128.0    0.0.0.0         255.255.192.0   U     0      0        0 eth1
10.211.1.18     0.0.0.0         255.255.255.255 UH    0      0        0 tun0
128.0.0.0       10.211.1.18     128.0.0.0       UG    0      0        0 tun0
220.123.19.246  10.0.2.2        255.255.255.255 UGH   0      0        0 eth0

#220.123.19.246 is the ip address of the vpn server


$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:c8:73:5d  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:157704 errors:0 dropped:0 overruns:0 frame:0
          TX packets:85478 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:139871643 (133.3 MiB)  TX bytes:9667249 (9.2 MiB)
          Interrupt:19 Base address:0xd000

eth1      Link encap:Ethernet  HWaddr 08:00:27:99:f1:e4  
          inet addr:10.152.152.10  Bcast:10.152.191.255  Mask:255.255.192.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1649 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1306 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:139887 (136.6 KiB)  TX bytes:122465 (119.5 KiB)
          Interrupt:16 Base address:0xd040

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:43763 errors:0 dropped:0 overruns:0 frame:0
          TX packets:43763 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:114248277 (108.9 MiB)  TX bytes:114248277 (108.9 MiB)

tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          inet addr:10.211.1.17  P-t-P:10.211.1.18  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:1295 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2092 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:968529 (945.8 KiB)  TX bytes:269286 (262.9 KiB)


$ sysctl net.ipv4.conf.all.forwarding 
net.ipv4.conf.all.forwarding = 1

在虚拟机网关内我可以正确使用 tun0

$ ping -I tun0 4.2.2.2
PING 4.2.2.2 (4.2.2.2) from 10.211.1.17 tun0: 56(84) bytes of data.
64 bytes from 4.2.2.2: icmp_seq=1 ttl=55 time=423.2 ms
64 bytes from 4.2.2.2: icmp_seq=2 ttl=55 time=421.7 ms
^C
--- 4.2.2.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1801ms
rtt min/avg/max/mdev = 421.762/422.484/423.207/12.737 ms

我尝试过的事情(没有成功)

正如我之前所说,我想通过 tun0 路由来自 VM-workstation(在 eth0 上)的所有流量。 我尝试使用 iptables 来重定向流量,但它只能部分工作,而且无论如何它没有使用 tun0

iptables -t nat -A PREROUTING -i eth1 -s 10.152.128.0/18 ! -d 10.152.128.0/18 -J REDIRECT
iptables -t nat -A POSTROUTING -o eth0 -s 10.152.128.0/18 -J MASQUERADE

我也尝试过 FORWARD,但它根本不起作用

iptables -A FORWARD -i eth1 -o tun0 -J ACCEPT
iptables -A FORWARD -i tun0 -o eth1 -J ACCEPT

我尝试了 ip route,但也完全不起作用:

ip rule add from 10.152.128.0/18 table 200
ip route add default dev tun0 table 200

我觉得有一个简单的解决方案,但我找不到它

【问题讨论】:

    标签: networking routing iptables openvpn gateway


    【解决方案1】:

    您发布的第二个解决方案(FORWARD)应该可以使用。但是,您需要为您的系统启用 IP 转发,否则这将不起作用。你可以这样启用它

    echo 1 > /proc/sys/net/ipv4/ip_forward
    

    注意: OpenVPN 服务器不知道您正在证明从 eth1 到 tun0 的路由,因此他不知道如何响应来自您的 LAN (10.152.128.0/18) 的流量)。因此,您还需要告诉您的 OpenVPN 服务器该特定客户端可以访问的本地网络。 Here 解释了如何做到这一点。此外,您还需要告诉您的 VM-Workstation 在普通网络接口上通往 VPN 网络的可用路由。

    编辑:如果您只想通过您的 vpn 网关路由流量,代理确实是正确的方法。不要忘记启用转发 ofc。 这应该可以解决问题:

    iptables -t nat -A POSTROUTING -s 10.152.128.0/18 -o tun0 -j MASQUERADE
    

    使用这种方法需要您通知您的 VM-Workstation VM-Gateway 是它的默认网关。

    【讨论】:

    • 我无法控制 openvpn 服务器,因此无法编辑其配置。我需要在 VM 网关中路由流量。我正在考虑在 VM 网关中配置一个代理,该代理通过 tun0 发送流量并将来自 eth1 (10.152.128.0/18) 的所有内容路由到代理。你觉得可行吗?
    • 您需要通过隧道访问一个特定设备还是多个?这两个问题都可以解决,但方法不同。
    • 通过隧道,我需要到达整个互联网。当我在 VM-gateway 中时我可以这样做(例如:ping -I tun0 4.2.2.2 工作正常)但当我在 VM-workstation 中时我不能这样做。
    • 我更正了我的答案。我希望这个解决方案能完成你想做的事情。
    • 哦,你是我的救星!如果可以的话,我会吻你!
    猜你喜欢
    • 2021-03-22
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2014-06-15
    • 1970-01-01
    相关资源
    最近更新 更多