如何将流量从专用网络路由到 openvpn 子网(并返回)

Ran*_*ing 5 openvpn ip-routing

我在 Linode 有几台服务器。我正在尝试设置它们,以便在其中一台机器上使用 VPN,然后可以使用私有 linode 网络访问所有其他机器。对私有服务(SSH 等)的公共访问将仅限于具有 VPN 访问权限的人。

注意:我还没有在这些服务器上运行防火墙

root@internal:~# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination  
Run Code Online (Sandbox Code Playgroud)

内部服务器(运行 openvpn 服务器)

eth0      Link encap:Ethernet  HWaddr f2:3c:91:db:68:b4  
          inet addr:23.239.17.12  Bcast:23.239.17.255  Mask:255.255.255.0
          inet6 addr: 2600:3c02::f03c:91ff:fedb:68b4/64 Scope:Global
          inet6 addr: fe80::f03c:91ff:fedb:68b4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:80780 errors:0 dropped:0 overruns:0 frame:0
          TX packets:102812 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:14317079 (14.3 MB)  TX bytes:17385151 (17.3 MB)

eth0:1    Link encap:Ethernet  HWaddr f2:3c:91:db:68:b4  
          inet addr:192.168.137.64  Bcast:192.168.255.255  Mask:255.255.128.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          inet addr:172.20.1.1  P-t-P:172.20.1.2  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:2318 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1484 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:174573 (174.5 KB)  TX bytes:170941 (170.9 KB)
Run Code Online (Sandbox Code Playgroud)

对以上的评论:

  • eth0 是公共接口
  • eth0:1 是私有网络的接口
  • VPN 隧道工作正常。从连接到 VPN 的客户端,我可以 ping 172.20.1.1 和 192.168.137.64。
  • 此服务器上设置了 net.ipv4.ip_forward=1

数据库服务器(nix03):

root@nix03:~# ifconfig eth0      Link encap:Ethernet  HWaddr f2:3c:91:73:d2:cc  
          inet addr:173.230.140.52  Bcast:173.230.140.255  Mask:255.255.255.0
          inet6 addr: 2600:3c02::f03c:91ff:fe73:d2cc/64 Scope:Global
          inet6 addr: fe80::f03c:91ff:fe73:d2cc/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12348 errors:0 dropped:0 overruns:0 frame:0
          TX packets:44434 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1166666 (1.1 MB)  TX bytes:5339936 (5.3 MB)

eth0:1    Link encap:Ethernet  HWaddr f2:3c:91:73:d2:cc  
          inet addr:192.168.137.63  Bcast:192.168.255.255  Mask:255.255.128.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
Run Code Online (Sandbox Code Playgroud)

对以上的评论:

  • eth0 是公共接口
  • eth0:1 是私有网络的接口
  • 我可以在私有接口 (192.168.137.64) 上 ping 内部服务器。

当前问题

我希望能够通过 VPN 访问数据库服务器。从我的 vpn 客户端(办公室的笔记本电脑),我希望能够 ping 192.168.137.63。然而,这目前失败了。

在尝试进行故障排除时,我决定从数据库服务器端处理它,看看是否可以 ping 内部服务器 (172.20.1.1) 上的 VPN 隧道。我意识到我需要在数据库服务器上设置一个路由来告诉它从哪里发送发往 172.20.1.0/24 网络的数据包,所以我这样做了:

root@nix03:~# ip route add 172.20.1.0/24 via 192.168.137.64 root@nix03:~# ip route list default via 173.230.140.1 dev eth0
172.20.1.0/24 via 192.168.137.64 dev eth0
173.230.140.0/24 dev eth0  proto kernel  scope link  src 173.230.140.52
192.168.128.0/17 dev eth0  proto kernel  scope link  src 192.168.137.63 root@nix03:~# ip route get 172.20.1.1
172.20.1.1 via 192.168.137.64 dev eth0  src 192.168.137.63
    cache     
Run Code Online (Sandbox Code Playgroud)

所以,我认为基于上述,当我 ping 172.20.1.1 时,我的服务器应该将数据包发送到 192.168.137.64(内部服务器)。由于 ip 转发,该服务器应该从 eth0:1 获取数据包并将其路由到 tun0 (172.20.1.1)。

但是,正如您可能已经猜到的那样,从 nix03(数据库服务器)ping 172.20.1.1 不起作用。

我做了一些数据包捕获以查看我的 ICMP 数据包被发送到哪个 MAC 地址:

root@nix03:~# tcpdump -i eth0 -e icmp tcpdump: 详细输出被抑制,使用 -v 或 -vv 进行完整协议解码监听 eth0,链接类型 EN10MB(以太网),捕获大小 65535 字节 16:41:39.623759 f2:3c:91:73:d2:cc (oui Unknown) > f2:3c:91:db:68:b4 (oui Unknown), ethertype IPv4 (0x0800), length 98: 192.168.137.63 > 172.20.1.1: ICMP echo request, id 3324, seq 33653, length 64 root@nix03:~# arp Address HWtype HWaddress Flags Mask Iface 192.168.137.64 ether f2:3c:91:db:68:b4 C eth0

这告诉我数据包应该到达内部服务器。至少,它们被发送到正确的 NIC。但是,当我在内部服务器的 eth0 和 eth0:1 上运行 tcpdump 时,我没有看到任何来自 db 服务器的 icmp 数据包。

我还能尝试什么?提前致谢。

更新 #1

“内部”服务器的路由表:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gw-li686.linode 0.0.0.0         UG    0      0        0 eth0
23.239.17.0     *               255.255.255.0   U     0      0        0 eth0
172.20.1.0      172.20.1.2      255.255.255.0   UG    0      0        0 tun0
172.20.1.2      *               255.255.255.255 UH    0      0        0 tun0
192.168.128.0   *               255.255.128.0   U     0      0        0 eth0
Run Code Online (Sandbox Code Playgroud)

Ran*_*ing 2

我最终不得不向内部服务器添加 NAT 规则。我不确定它是否必要,但它确实有效:

*nat
:PREROUTING ACCEPT [21:1248]
:INPUT ACCEPT [21:1248]
:OUTPUT ACCEPT [21:1529]
:POSTROUTING ACCEPT [21:1529]
# enable NAT for VPN clients so they can hit the private network
-A POSTROUTING -s 172.20.1.0/24 -o eth0 -j MASQUERADE
COMMIT
Run Code Online (Sandbox Code Playgroud)