在StackExchange上也有类似的问题,但是没有一个答案对我有帮助,所以我会尝试一个我自己的问题。
我有一个通过OpenVPN的VPN连接。 默认情况下,所有的stream量都通过隧道使用OpenVPN的“两个更具体的路线”技巧redirect,但我禁用了。 我的路由表是这样的:
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
接口configuration是这样的:
# 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 PtP: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通过本地networking路由器(DSL modem)192.168.2.1,另一个通过tun1通过VPN的网关。 有了这个configuration,如果我连接到一个站点,所select的路由是直接的(因为跳跃较less?):
# 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 ...
这很好,因为我的目标是仅通过隧道发送来自特定应用程序的stream量(尤其是传输,使用-i / bind-address-ipv4选项)。 要testing这是否可以工作,我先用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 * * * ...
我认为使用隧道本地地址作为源的连接是不可能的。
什么是可能的(尽pipe只是作为根)是指定源接口:
# 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 ...
所以显然tun1接口正在工作,并有可能通过它发送数据包。
但是select源接口并没有在我的实际目标应用程序(传输)中实现,所以我想要select源地址来工作。
我究竟做错了什么?
编辑:根据DerfK的回答,这是我的OpenVPNconfiguration:
在.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
使用此configuration, traceroute -s命令会提供与traceroute -i相同的输出,以显示通过VPN到目标地址的路由。
在我原来的文章中没有解释的东西:所讨论的VPN不提供对受保护的本地networking的访问,而是访问互联网,但是来自不同的位置/ IP地址。 否则,首先不需要添加默认路由。
你正在寻找的是所谓的“基于源的路由”,旧的route程序无法pipe理。 您需要使用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
我不确定是否需要在执行此操作之前从常规route表中删除路由,但可以将其保留(实际上,为了让内核select正确的源IP与10.30.92.5讨论不绑定到IP的应用程序)。