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
接口配置如下:
- # 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)
加上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
- ...
这很好,因为我的目标是仅通过隧道从特定应用程序发送流量(特别是传输,使用其-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),60 byte packets
- 1 * * *
- 2 * * *
- 3 * * *
- ...
我这意味着使用隧道的本地地址作为源的连接是不可能的.
什么是可能的(虽然只作为root)是指定源接口:
- # traceroute 8.8.8.8 -n -i tun1
- traceroute to 8.8.8.8 (8.8.8.8),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
- ...
显然,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
并且脚本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
使用此配置,traceroute -s命令提供与traceroute -i相同的输出,显示通过VPN到目标地址的路由.
我在原帖中没有解释的东西:有问题的VPN不提供对受保护的本地网络的访问,而是提供对Internet的访问,但是来自不同的位置/ IP地址.否则,首先不需要添加默认路由.
解决方法
您正在寻找的是“基于源的路由”,旧的路由程序无法管理它.您需要使用iproute2并根据源IP地址设置备用路由表.使用指南
here,你会有类似的东西:
- # 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
我不确定你是否需要在执行此操作之前从常规路由表中删除路由,但是保留它应该没问题(实际上为了让内核选择正确的源IP可能是必要的)与不绑定到IP的应用程序联系10.30.92.5.