使用 OpenVPN 通过 VPN 网关路由子网

使用 OpenVPN 通过 VPN 网关路由子网

我所在的一家小公司即将关闭办公室,因此我得将目前本地托管的 VPN(仅 Zyxel Zywall 110 设备)迁移到基于云的 VM。我在网络方面经验不多(后端开发转为运营),因此我想验证以下方法是否可行。


我有一个专用的虚拟机,在其中设置了 OpenVPN 访问服务器,并且基本运行良好,人们可以连接,一切都很好。

不过有一个问题,当前的 VPN 通过“隧道”将某个 IP 范围转发到合作伙伴公司的内部网络。它看起来像这样:

if dest_addr in '172.30.239.0/25':
    route through gw 194.xxx.xxx.xxx
else:
    route through gw 0.0.0.0

举个例子,当连接到 VPN 时,我的流量将被按如下方式路由:

dest 1.2.3.4       => me -> VPN server -> its internet gateway -*-> 1.2.3.4
dest 172.30.239.10 => me -> VPN server -> partner network gateway (194.xxx.xxx.xxx) -(internal network routing)-> 172.30.239.10

其中,从我们的路由器到合作伙伴公司的 VPN GW 的连接是通过具有预共享密钥的 IKEv1 完成的(从路由器的 Web UI 判断,因为这就是我拥有的所有“文档”)。

以下是一些描述设置的 ASCII 图像。我正在Router用 VM 替换。

            +-----------------+           [     Partner infra, this has to stay the same     ]
            | Router          |           194.xxx.xxx.xxx            e.g. 172.30.239.75
            | --------------- |   IKEv1   +-------------+       +-------------------------+
User -----> | 172.30.239.0/25-| --------> | VPN gateway |-----> | Internal network server |
            |     default     |           +-------------+       +-------------------------+
            |        |        |
            +--------+--------+
                     |
                     |
                 internet

OpenVPN 访问服务器本身不支持任何类似的东西(或者我无法找到该配置),所以我认为我可以在 VM 级别上执行此操作。如果我使用 Strongswan 之类的东西将操作系统连接到 VPN 网关并在 中配置适当的路由iptables,这可以工作吗?连接到 OpenVPN 服务器的用户的流量是否172.30.239.0/25会路由到 Strongswan 的连接,或者这种方法从根本上是错误的?我有什么选择?

谢谢!

答案1

现在事情稍微清楚了一点。当然,OpenVPN 有一个选项可以解决您的问题:如果您希望 VPN 客户端通过您的 VPN 隧道路由网络 1.2.3.0/25 的流量,请使用选项push(来源):

--push option
    Push a config file option back to the client for remote execution. Note that
 option must be enclosed in double quotes (""). The client must specify --pull in
 its config file. The set of options which can be pushed is limited by both
 feasibility and security. Some options such as those which would execute scripts
 are banned, since they would effectively allow a compromised server to execute
 arbitrary code on the client. Other options such as TLS or MTU parameters cannot
 be pushed because the client needs to know them before the connection to the
 server can be initiated. 

因此,你必须在 openvpn 服务器配置中添加

push "route 1.2.3.0 255.255.255.128"

这告诉 openvpn 客户端,它应该通过 vpn 隧道路由所有到该子网的流量。当然,在服务器本身上,也必须有正确的路由 - 但我猜这个路由可能已经到位了。

[更新]
我的回答缺少一些关于路由的基本信息。任何网络设备都可以充当网关 - 这就是家用路由器的作用。路由不是用iptables- 这是一个修改 Linux 内置内核防火墙的工具。路由决策是在路由表(为了简单起见,我在这里只讨论 IPv4)。对于每个要发送的 IP 数据包,都会考虑路由表,从主机专用路由(/32 子网)开始,采用它能找到的最“精确”的路由。因此,只有在未找到具有更“精确”子网的匹配路由时,才会采用默认路由(/0 子网)。

通过 VPN 隧道路由所有流量很容易。OpenVPN 使用redirect-gateway指令来实现这一点。因此,要么将该指令放入所有客户端配置中,要么将

push "redirect-gateway def1"

进入您的服务器配置。使用选项 def1,将使用两个具有 128.0.0.0 子网的路由,而不是一个 0.0.0.0 子网路由 - 这样做是为了避免新的默认路由被 DHCP 客户端覆盖。

谈到您服务器上的路由。由于服务器是 IPSec 隧道的端点(通往您的合作伙伴),因此软件(strongswan?)已在该服务器上安装了必要的路由,以便远程网络的流量通过 IPSec 隧道路由。

为了便于理解,我画了一张包含示例网络和路由的图。请考虑以下网络:

A ---------- B -------- C --------- D
  • A 是一个 OpenVPN 客户端,ip 为 10.8.0.2
  • B 是 OpenVPN 服务器,IP 为 10.8.0.1 和 172.16.0.1
  • C 是合作伙伴网络内的远程 IPSec 端点,IP 为 172.16.0.2 和 192.168.0.1
  • D 是合作伙伴网络内的网络设备,IP 为 192.168.0.10

在此示例设置中,您有三个网络:

  • OpenVPN 网络
  • IPSec 网络
  • 合作伙伴网络

设备 A 具有以下路由:0.0.0.0/0 gw 10.8.0.1
服务器 B 有两条路由:一个通往 ISP 的默认网关,并且:192.168.0.0/24 gw 172.16.0.2
服务器 C 有两条路由:一个通往合作伙伴 ISP 的默认网关,并且:对于设备 D,只需要10.8.0.0/24 gw 172.16.0.1
默认路由。0.0.0.0/0 gw 192.168.0.1

答案2

以下是我们仅通过 OpenVPN 隧道路由 OpenVPN 子网的方法:

pull-filter ignore redirect-gateway
route-nopull
route 10.8.0.0 255.255.255.0

相关内容