首页 文章

从内部网络(eth1)到openvpn(tun0)路由流量[关闭]

提问于
浏览
0

我有两个虚拟机,它们都在同一个内部网络(eth1)中,但只有一个(网关)实际连接到Internet(通过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

在VM网关内部,我可以正确使用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工作站(在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

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

1 回答

  • 1

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

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

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

    Edit: 如果您只想通过vpn网关路由流量,代理确实是正确的方法 . 别忘了启用转发功能 . 这应该做的伎俩:

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

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

相关问题