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

前端之家收集整理的这篇文章主要介绍了linux – 设置源地址使用tun设备不起作用(Debian Squeeze)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
StackExchange上也有类似的问题,但没有一个答案对我有帮助,所以我会尝试自己的问题.

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

  1. 198.144.156.141 192.168.2.1 255.255.255.255 UGH 0 0 0 eth0
  2. 10.30.92.5 0.0.0.0 255.255.255.255 UH 0 0 0 tun1
  3. 10.30.92.1 10.30.92.5 255.255.255.255 UGH 0 0 0 tun1
  4. 192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
  5. 0.0.0.0 10.30.92.5 0.0.0.0 UG 0 0 0 tun1
  6. 0.0.0.0 192.168.2.1 0.0.0.0 UG 0 0 0 eth0

接口配置如下:

  1. # ifconfig
  2. eth0 Link encap:Ethernet HWaddr XX-XX-
  3. inet addr:192.168.2.100 Bcast:192.168.2.255 Mask:255.255.255.0
  4. inet6 addr: fe80::211:9ff:fe8d:acbd/64 Scope:Link
  5. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  6. RX packets:394869 errors:0 dropped:0 overruns:0 frame:0
  7. TX packets:293489 errors:0 dropped:0 overruns:0 carrier:0
  8. collisions:0 txqueuelen:1000
  9. RX bytes:388519578 (370.5 MiB) TX bytes:148817487 (141.9 MiB)
  10. Interrupt:20 Base address:0x6f00
  11.  
  12. tun1 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
  13. inet addr:10.30.92.6 P-t-P:10.30.92.5 Mask:255.255.255.255
  14. UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
  15. RX packets:64 errors:0 dropped:0 overruns:0 frame:0
  16. TX packets:67 errors:0 dropped:0 overruns:0 carrier:0
  17. collisions:0 txqueuelen:100
  18. RX bytes:9885 (9.6 KiB) TX bytes:4380 (4.2 KiB)

加上lo设备.

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

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

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

  1. # traceroute 8.8.8.8 -n -s 10.30.92.6
  2. traceroute to 8.8.8.8 (8.8.8.8),60 byte packets
  3. 1 * * *
  4. 2 * * *
  5. 3 * * *
  6. ...

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

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

  1. # traceroute 8.8.8.8 -n -i tun1
  2. traceroute to 8.8.8.8 (8.8.8.8),60 byte packets
  3. 1 10.30.92.1 129.337 ms 297.758 ms 297.725 ms
  4. 2 * * *
  5. 3 198.144.152.17 297.653 ms 297.652 ms 297.650 ms
  6. ...

显然,tun1接口正在工作,可以通过它发送数据包.

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

我究竟做错了什么?

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

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

  1. # disable automatic route configuration
  2. route-noexec
  3. # enable executing scripts
  4. script-security 2
  5. # script to configure routes
  6. up updown.sh
  7. # script to deconfigure routes
  8. down updown.sh

并且脚本updown.sh包含:

  1. #!/bin/bash
  2. case $script_type in
  3.  
  4. up) echo routes up
  5. # reproduce OpenVPN's standard routes
  6. ip route add $trusted_ip via $route_net_gateway
  7. ip route add $route_network_1 via $route_vpn_gateway
  8. # but put the default route in an extra table
  9. ip route add default via $route_vpn_gateway table 200
  10. # and make sure it is activated by selection of the interface's address
  11. ip rule add from $ifconfig_local/32 table 200
  12. # flush routing cache (but everything seems to work without this,too)
  13. ip route flush cache
  14. ;;
  15.  
  16. down) echo routes down
  17. ip route del $trusted_ip
  18. ;;
  19.  
  20. esac

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

我在原帖中没有解释的东西:有问题的VPN不提供对受保护的本地网络的访问,而是提供对Internet的访问,但是来自不同的位置/ IP地址.否则,首先不需要添加默认路由.

解决方法

您正在寻找的是“基于源的路由”,旧的路由程序无法管理它.您需要使用iproute2并根据源IP地址设置备用路由表.使用指南 here,你会有类似的东西:
  1. # Designate "table 200" as "vpntunnel"
  2. # - optional,but otherwise we'd have to use 200 everywhere. Also,200 is an arbitrary number,0 253 254 255 are reserved.
  3. echo 200 vpntunnel >> /etc/iproute2/rt_tables
  4. # Packets from your tunnel address use this table
  5. ip rule add from 10.30.92.6/32 table vpntunnel
  6. # The default route for packets using the vpntunnel is...
  7. ip route add default via 10.30.92.5 dev tun1 table vpntunnel
  8. # Flush routing cache
  9. ip route flush cache

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

猜你在找的Linux相关文章