设置源地址以使用 tun 设备不起作用 (Debian Squeeze)

A. *_*nda 5 networking linux vpn route interface

StackExchange 上也有类似的问题,但没有一个答案对我有帮助,所以我会尝试一个我自己的问题。

我通过 OpenVPN 建立了 VPN 连接。默认情况下,所有流量都使用 OpenVPN 的“两个更具体的路由”技巧通过隧道重定向,但我禁用了它。我的路由表是这样的:

198.144.156.141 192.168.2.1     255.255.255.255 UGH   0      0        0 eth0
10.30.92.5      0.0.0.0         255.255.255.255 UH    0      0        0 tun1
10.30.92.1      10.30.92.5      255.255.255.255 UGH   0      0        0 tun1
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         10.30.92.5      0.0.0.0         UG    0      0        0 tun1
0.0.0.0         192.168.2.1     0.0.0.0         UG    0      0        0 eth0
Run Code Online (Sandbox Code Playgroud)

并且接口配置是这样的:

# ifconfig
eth0      Link encap:Ethernet  HWaddr XX-XX-
          inet addr:192.168.2.100  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::211:9ff:fe8d:acbd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:394869 errors:0 dropped:0 overruns:0 frame:0
          TX packets:293489 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:388519578 (370.5 MiB)  TX bytes:148817487 (141.9 MiB)
          Interrupt:20 Base address:0x6f00

tun1      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          inet addr:10.30.92.6  P-t-P:10.30.92.5  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:64 errors:0 dropped:0 overruns:0 frame:0
          TX packets:67 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          RX bytes:9885 (9.6 KiB)  TX bytes:4380 (4.2 KiB)
Run Code Online (Sandbox Code Playgroud)

加上 lo 设备。

路由表有两个默认路由,一个通过 eth0 通过我的本地网络路由器(DSL 调制解调器)在 192.168.2.1,另一个通过 tun1 通过 VPN 的网关。使用此配置,如果我连接到站点,则选择的路由是直接路由(因为它的跳数较少?):

# traceroute 8.8.8.8 -n
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  192.168.2.1  0.427 ms  0.491 ms  0.610 ms
 2  213.191.89.13  17.981 ms  20.137 ms  22.141 ms
 3  62.109.108.48  23.681 ms  25.009 ms  26.401 ms
...
Run Code Online (Sandbox Code Playgroud)

这很好,因为我的目标是仅通过隧道发送来自特定应用程序的流量(特别是传输,使用其 -i / bind-address-ipv4 选项)。为了测试这是否可以正常工作,我首先使用 traceroute 的 -s 选项进行检查:

# traceroute 8.8.8.8 -n -s 10.30.92.6
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  * * *
 2  * * *
 3  * * *
...
Run Code Online (Sandbox Code Playgroud)

我认为这意味着使用隧道的本地地址作为源的连接是不可能的。

什么是可能的(虽然只是作为根)是指定源接口:

# traceroute 8.8.8.8 -n -i tun1
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  10.30.92.1  129.337 ms  297.758 ms  297.725 ms
 2  * * *
 3  198.144.152.17  297.653 ms  297.652 ms  297.650 ms
...
Run Code Online (Sandbox Code Playgroud)

所以显然TUN1接口工作,它可以通过它来发送数据包。

但是选择源接口并没有在我的实际目标应用程序(传输)中实现,所以我想让源地址选择工作。

我究竟做错了什么?


编辑:根据 DerfK 的回答,这是我的 OpenVPN 配置:

在 .conf- 或 .ovpn- 文件中,包含这些命令

# disable automatic route configuration
route-noexec
# enable executing scripts
script-security 2
# script to configure routes
up updown.sh
# script to deconfigure routes
down updown.sh
Run Code Online (Sandbox Code Playgroud)

该脚本updown.sh包含:

#!/bin/bash
case $script_type in

up)     echo routes up
        # reproduce OpenVPN's standard routes
        ip route add $trusted_ip via $route_net_gateway
        ip route add $route_network_1 via $route_vpn_gateway
        # but put the default route in an extra table
        ip route add default via $route_vpn_gateway table 200
        # and make sure it is activated by selection of the interface's address
        ip rule add from $ifconfig_local/32 table 200
        # flush routing cache (but everything seems to work without this, too)
        ip route flush cache
        ;;

down)   echo routes down
        ip route del $trusted_ip
        ;;

esac
Run Code Online (Sandbox Code Playgroud)

使用此配置,该traceroute -s命令提供与traceroute -i显示通过 VPN 到目标地址的路由相同的输出。

我在原帖中没有解释的事情:有问题的 VPN 不提供对受保护的本地网络的访问,而是从不同的位置/IP 地址访问 Internet。否则,一开始就不需要添加默认路由。

Der*_*rfK 2

您正在寻找的称为“基于源的路由”,旧route程序无法管理它。您需要使用iproute2并设置基于源 IP 地址的备用路由表。使用这里的指南你会得到类似的东西:

# Designate "table 200" as "vpntunnel"
# - optional, but otherwise we'd have to use 200 everywhere.  Also, 200 is an arbitrary number, 0 253 254 255 are reserved.
echo 200 vpntunnel >> /etc/iproute2/rt_tables
# Packets from your tunnel address use this table
ip rule add from 10.30.92.6/32 table vpntunnel
# The default route for packets using the vpntunnel is...
ip route add default via 10.30.92.5 dev tun1 table vpntunnel
# Flush routing cache
ip route flush cache
Run Code Online (Sandbox Code Playgroud)

我不确定route在执行此操作之前是否需要从常规表中删除路由,但保留它应该没问题(事实上,为了让内核选择正确的源 IP 可能是必要的)对于不绑定到 IP 的应用程序,请与 10.30.92.5 对话)。