strongSwan 使用了一种叫做策略路由的网络功能。这与 SPD 和 IPsec 使用的安全策略不同。正常的 IP 路由是基于 IP 头中的目标 IP 地址字段。启用 IP 转发的主机将为所有前往特定目的地的数据包选择相同的路径。这种情况并不总是可取的。策略路由的一个早期案例是支持不同客户类别的不同 QoS 级别。金牌客户为获得高吞吐量和低延迟服务而付费,而青铜客户则为更便宜的尽力服务而解决。为这些客户选择路由时,除其他外,还需要将源 IP 地址和目的地址作为 IP 路由过程的一部分。
Linux 策略路由的实现需要多个路由表和一个路由策略数据库(RPDB)。你可以通过输入以下命令列出 RPDB 中的策略:
1
ip rule show
输出应该显示这样的内容:
1 2 3 4
0: from all lookup local 220: from all lookup 220 32766: from all lookup main 32767: from all lookup default
最左边的一列显示了分配给同一行的策略路由规则的优先级。可用的规则从最高优先级(0)到最低优先级(32767)进行扫描,第一个匹配的规则会选择路由过程将使用的路由表来查找路由。一旦 RPDB 策略匹配,就会使用指定的表来查找下一跳。如果路由过程无法从规则所指示的路由表计算出数据包的下一跳,则继续处理到 RPDB 中的下一条规则。from 关键字表示对源 IP 地址的选择符,但由于也使用了 all ,所以表示所有数据包都会匹配。默认情况下,strongSwan 会插入一个优先级为 220 的策略规则。当 Ubuntu Linux 启动时,它会设置三个表:本地(id 255)、主(254)和默认(253)。本地路由表包含本地和广播地址的高优先级控制路由。这个表是供内核内部使用的,不应该从用户空间修改。主路由表是在没有指定表时使用的普通表。最后,默认表通常是空的,当没有其他表匹配时使用。
服务器 A 和客户机 A 各有一个只连接到 192.168.60.0/24 网络的主机接口。服务器 A 上的接口被分配的 IP 地址为 192.168.60.100 ,客户端 A 上的接口被分配的 IP 地址为 192.168.60.111 。为了使这个例子类似于现实中的例子,我们说服务器 A 和客户端 A 是站点 A 的一部分,站点 A “拥有” 网络192.168.60.0/24 。服务器 A 通过其 NAT 接口为网站 A 提供互联网访问。
相似的,服务器 B 上的网络接口被分配了 IP 地址 192.168.80.100 ,客户端 B 上的接口被分配了 IP 地址 192.168.80.111 。服务器 B 通过其 NAT 接口为 B 站点提供 Internet 访问。
服务器 A 和服务器 B 都有一个只连接到网络的二级主机接口 192.168.70.0/24 。服务器 A 的接口被分配的 IP 地址是 192.168.70.5 ,而服务器 B 的 IP 地址是 192.168.70.6 。您将配置服务器 A 和服务器 B 在 192.168.70.0/24 网络上建立 VPN 服务。
# server A # This file holds shared secrets or RSA private keys for authentication. # RSA private key for this host, authenticating it to any other host # which knows the public part.
# server B # This file holds shared secrets or RSA private keys for authentication. # RSA private key for this host, authenticating it to any other host # which knows the public part.
# server A # This file holds shared secrets or RSA private keys for authentication. # RSA private key for this host, authenticating it to any other host # which knows the public part.
# server B # This file holds shared secrets or RSA private keys for authentication. # RSA private key for this host, authenticating it to any other host # which knows the public part.
# server A # This file holds shared secrets or RSA private keys for authentication. # RSA private key for this host, authenticating it to any other host # which knows the public part.
# server B # This file holds shared secrets or RSA private keys for authentication. # RSA private key for this host, authenticating it to any other host # which knows the public part.
: RSA 192.168.70.6.key.pem
带 IP 转发的 Site-to-Site Tunnel Mode 配置
在 Client A 上编辑 /etc/network/interfaces 并添加 gateway 192.168.60.100 。编辑 /etc/resolv.conf 并验证 nameserver 10.0.98.3 是 DNS 服务器。
# server A # This file holds shared secrets or RSA private keys for authentication. # RSA private key for this host, authenticating it to any other host # which knows the public part.