OpenBSD 通过具有相同 IP 但不同接口的不同网关进行路由

OpenBSD 通过具有相同 IP 但不同接口的不同网关进行路由

我有多个使用相同网关 IP 的 VPN 连接(我无法更改它,因为它不受我控制)。这些 VPN 都提供对不同网络的访问,并且这些网络至少在上游有一到两个跃点,因此在所有情况下都需要网关 IP。使用 Linux,要路由到网络,我只需执行以下操作:

ip route add $destination_1 via $gateway_ip dev $interface_1
ip route add $destination_2 via $gateway_ip dev $interface_2
ip route add $destination_3 via $gateway_ip dev $interface_3

ETC。

然后,Linux 会将每个目标网络的流量放到正确的接口上,并流向正确的网关,因此每个接口的网关 IP 是否相同并不重要。

我的问题是,如何在 OpenBSD 中实现这一点?我试过但失败了。我的发现是,对于特定目的地,我可以:

  • 指定一个接口(如果目的地可以通过该链接直接到达 - 但在我的例子中不是这样)
  • 指定网关 IP,因为目标不直接在链接上

但我不知道如何指定这两者。

答案1

使用-ifp修饰语路线。 来自手册页

In a change or add command where the destination and gateway are not
sufficient to specify the route, the -ifp or -ifa modifiers may be 
used to determine the interface name or interface address.

因此这样的方法可行:

# for  arg in tun0 tun1 tun2; do ifconfig $arg  192.168.11.1/24; done
# route add 10/8 -iface 192.168.11.1 -ifp tun0
add net 10/8: gateway 192.168.11.1
# route add 172.16/12 -iface 192.168.11.1 -ifp tun1
add net 172.16/12: gateway 192.168.11.1
# route add 192.168.254/24 -iface 192.168.11.1 -ifp tun2
add net 192.168.254/24: gateway 192.168.11.1
# route show -inet
Routing tables

Internet:
Destination        Gateway            Flags   Refs      Use   Mtu  Prio 
Iface
10/8               192.168.11.1       GS         0        0     -     8 tun0
localhost          localhost          UHl        0       22 32768     1 lo0
172.16/12          192.168.11.1       S          0        0     -     8 tun1
192.168.11.1       192.168.11.1       UHhl       1        4     -     1 tun0
[...my real routes omitted...]
192.168.254/24     192.168.11.1       S          0        0     -     8 tun2

如果您的目的地路线重叠,那么您可以使用普法并路由标签进行匹配,或者路由域

相关内容