4.1. 简单的源策略路由

让我们再次举一个真实的例子,我有 2 个(实际上是 3 个,是时候归还它们了)有线调制解调器,连接到一个 Linux NAT(“伪装”)路由器。住在这里的人付钱给我使用互联网。假设我的一个室友只访问 hotmail 并且想少付钱。这对我来说没问题,但他们最终会使用低端有线调制解调器。

“快速”有线调制解调器被称为 212.64.94.251,并且是到 212.64.94.1 的 PPP 链接。“慢速”有线调制解调器以各种 IP 地址而闻名,在本例中为 212.64.78.148,并且是到 195.96.98.253 的链接。

本地表

[ahu@home ahu]$ ip route list table local
broadcast 127.255.255.255 dev lo  proto kernel  scope link  src 127.0.0.1 
local 10.0.0.1 dev eth0  proto kernel  scope host  src 10.0.0.1 
broadcast 10.0.0.0 dev eth0  proto kernel  scope link  src 10.0.0.1 
local 212.64.94.251 dev ppp0  proto kernel  scope host  src 212.64.94.251 
broadcast 10.255.255.255 dev eth0  proto kernel  scope link  src 10.0.0.1 
broadcast 127.0.0.0 dev lo  proto kernel  scope link  src 127.0.0.1 
local 212.64.78.148 dev ppp2  proto kernel  scope host  src 212.64.78.148 
local 127.0.0.1 dev lo  proto kernel  scope host  src 127.0.0.1 
local 127.0.0.0/8 dev lo  proto kernel  scope host  src 127.0.0.1 

很多显而易见的事情,但有些事情需要在某个地方指定。好吧,它们就在这里。默认表是空的。

让我们查看“main”表

[ahu@home ahu]$ ip route list table main 
195.96.98.253 dev ppp2  proto kernel  scope link  src 212.64.78.148 
212.64.94.1 dev ppp0  proto kernel  scope link  src 212.64.94.251 
10.0.0.0/8 dev eth0  proto kernel  scope link  src 10.0.0.1 
127.0.0.0/8 dev lo  scope link 
default via 212.64.94.1 dev ppp0 

现在,我们为我们假设的室友生成一个名为“John”的新规则。虽然我们可以使用纯数字,但如果我们将表添加到 /etc/iproute2/rt_tables 中,则会容易得多。

# echo 200 John >> /etc/iproute2/rt_tables
# ip rule add from 10.0.0.10 table John
# ip rule ls
0:	from all lookup local 
32765:	from 10.0.0.10 lookup John
32766:	from all lookup main 
32767:	from all lookup default

现在剩下要做的就是生成 John 的表,并刷新路由缓存

# ip route add default via 195.96.98.253 dev ppp2 table John
# ip route flush cache

我们完成了。剩下的就作为读者的练习,在 ip-up 中实现它。