如何通过隧道上的openvpn客户端在番茄隧道特定的主机

我在networking世界相对新手,虽然我编写了很长一段时间的系统pipe理员背景。 在这里,我离目的地只有一步之遥。

整个情况是:在家里我使用一个LinkSys E3000作为网关(不知道这是否是它的名字),无线AP和其他路由/交换设备。 它提供1台个人电脑和1台带局域网的Mac,1台Mac Mini + 1台iPad + 2台带WIFI的智能手机。

我的目标是在E3000上使用openvpn客户端(使用番茄固件),并使我的iPad和智能手机的所有WiFistream量都通过它,其他设备路由保持相同的非openvpn路由。

到目前为止,我可以将E3000上的openvpn客户端连接到openvpn服务器,通过openvpn连接传输所有设备的所有stream量。 剩下的就是如何通过源IP(至less在我的猜测中)select性地路由到隧道,而不要打扰别人。

过去几天我学到了一些“iptables”和“路由”,但没有太多的运气,所以这里就是我的问题。 这里有一些信息可以帮助你获得结构。

ifconfig -a输出一些无用的线条,并且在web界面C0:C1:C0:1A:E0:28是WAN,C0:C1:C0:1A:E0:27是LAN,C0:C1:C0:1A :E0:29是2.4G wifi AP,C0:C1:C0:1A:E0:2A是5G wifi AP。

root @ router:/ tmp / home / root#ifconfig -a

br0 Link encap:以太网HWaddr C0:C1:C0:1A:E0:27
inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST运行组播MTU:1500公制:1

eth0 Link encap:以太网HWaddr C0:C1:C0:1A:E0:27
广播运行多播MTU:1500公制:1

eth1 Link encap:以太网HWaddr C0:C1:C0:1A:E0:29
广播运行ALLMULTI MULTICAST MTU:1500公制:1

eth2 Link encap:以太网HWaddr C0:C1:C0:1A:E0:2A
广播运行ALLMULTI MULTICAST MTU:1500公制:1

链路封装:本地环回
inet addr:127.0.0.1掩码:255.0.0.0 inet6 addr::: 1/128作用域:主机

ppp0链接封装:点对点协议
inet addr:172.200.1.43 PtP:172.200.0.1掩码:255.255.255.255 UP POINTOPOINT运行多播MTU:1480度量标准:1

vlan1 Link encap:以太网HWaddr C0:C1:C0:1A:E0:27
广播运行ALLMULTI MULTICAST MTU:1500公制:1

vlan2 Link encap:以太网HWaddr C0:C1:C0:1A:E0:28
广播运行多播MTU:1500公制:1

wl0.1 Link encap:以太网HWaddr C0:C1:C0:1A:E0:29
BROADCAST MULTICAST MTU:1500公制:1

brctl显示输出

root@router:/tmp/home/root# brctl show bridge name bridge id STP enabled interfaces br0 8000.c0c1c01ae027 no vlan1 eth1 eth2 

之前openvpn路由的脚本

 root@router:/tmp/home/root# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.200.0.1 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 br0 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo 0.0.0.0 172.200.0.1 0.0.0.0 UG 0 0 0 ppp0 

openvpn服务器推送

 PUSH: Received control message: 'PUSH_REPLY,redirect-gateway,dhcp-option DNS 8.8.8.8,route 172.20.0.1,topology net30,ping 10,ping-restart 120,ifconfig 172.20.0.6 172.20.0.5' 

openvpn的股票路由脚本

 Apr 24 14:52:06 router daemon.notice openvpn[1768]: /sbin/ifconfig tun11 172.20.0.6 pointopoint 172.20.0.5 mtu 1500 Apr 24 14:52:08 router daemon.notice openvpn[1768]: /sbin/route add -net 72.14.177.29 netmask 255.255.255.255 gw 172.200.0.1 Apr 24 14:52:08 router daemon.notice openvpn[1768]: /sbin/route add -net 0.0.0.0 netmask 128.0.0.0 gw 172.20.0.5 Apr 24 14:52:08 router daemon.notice openvpn[1768]: /sbin/route add -net 128.0.0.0 netmask 128.0.0.0 gw 172.20.0.5 Apr 24 14:52:08 router daemon.notice openvpn[1768]: /sbin/route add -net 172.20.0.1 netmask 255.255.255.255 gw 172.20.0.5 

openvpn之后的路线

 root@router:/tmp/home/root# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.20.0.5 0.0.0.0 255.255.255.255 UH 0 0 0 tun11 72.14.177.29 172.200.0.1 255.255.255.255 UGH 0 0 0 ppp0 172.200.0.1 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0 172.20.0.1 172.20.0.5 255.255.255.255 UGH 0 0 0 tun11 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 br0 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo 0.0.0.0 172.20.0.5 128.0.0.0 UG 0 0 0 tun11 128.0.0.0 172.20.0.5 128.0.0.0 UG 0 0 0 tun11 0.0.0.0 172.200.0.1 0.0.0.0 UG 0 0 0 ppp0 

我已经注意到和尝试的东西:*在openvpn客户端的Web界面有一个选项“创build隧道上的NAT”,如果我检查这个,有下面的脚本(可能在openvpn连接build立后执行)

 root@router:/tmp/home/root# cat /tmp/etc/openvpn/fw/client1-fw.sh #!/bin/sh iptables -I INPUT -i tun11 -j ACCEPT iptables -I FORWARD -i tun11 -j ACCEPT iptables -t nat -I POSTROUTING -s 192.168.1.0/255.255.255.0 -o tun11 -j MASQUERADE 

如果我取消这个选项,最后一行不会出现。 那么我猜想我的问题可能会通过iptables和NAT相关的命令来解决,我只是没有足够的知识来解决这个问题。 我试过跑步

 iptables -t nat -I POSTROUTING -s 192.168.1.6 -o tun11 -j MASQUERADE 

手动后openvpn连接(192.168.1.6是我的iPad的IP地址),然后我的iPad上网openvpn隧道,但所有其他设备无法访问互联网。

如果需要的话,这里是关于NAT的iptables

 root@router:/tmp/home/root# iptables -t nat -L -n Chain PREROUTING (policy ACCEPT) target prot opt source destination DROP all -- 0.0.0.0/0 192.168.1.0/24 WANPREROUTING all -- 0.0.0.0/0 172.200.1.43 upnp all -- 0.0.0.0/0 172.200.1.43 Chain POSTROUTING (policy ACCEPT) target prot opt source destination MASQUERADE all -- 0.0.0.0/0 0.0.0.0/0 SNAT all -- 192.168.1.0/24 192.168.1.0/24 to:192.168.1.1 Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain WANPREROUTING (1 references) target prot opt source destination DNAT icmp -- 0.0.0.0/0 0.0.0.0/0 to:192.168.1.1 Chain upnp (1 references) target prot opt source destination DNAT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:5353 to:192.168.1.3:5353 

在此先感谢您的帮助和阅读这么多,我希望我把你需要的每一个信息给予帮助:)

这个固件是否支持iproute2,可以支持多个路由表,基于src ip。

 echo "200 ovpn" >> /etc/iproute2/rt_tables ip rule add from YOUR.WIFI.IP.S table ovpn 

并为该表添加路由规则

 ip route add default via VPN.ROUTE.IP.ADDR dev vpn_dev table ovpn ip route flush cache 

你可以参考:Linux高级路由和stream量控制HOWTO: http : //lartc.org/howto/index.html

或者,因为你是中国人,你可以阅读我的博客关于这个主题: http : //wangxu.me/blog/p/675

希望这可以帮助

我终于做到了:-)

我正在使用v1.28.9054 MIPSR2-beta K26 USB vpn3.6 ,但应该在别人上工作,因为它是networking。

OpenVPN想要设置所有的路由,你必须停止…

在VPN隧道/客户端/基本

在VPN隧道/客户端/高级中取消选中“在隧道上创buildNAT”

取消选中“redirect互联网stream量”

自定义configuration,添加行:route-nopull

在pipe理/脚本/防火墙中,确保你有:

 iptables -I FORWARD -i br0 -o tun11 -j ACCEPT iptables -I FORWARD -i tun11 -o br0 -j ACCEPT iptables -I INPUT -i tun11 -j REJECT iptables -t nat -A POSTROUTING -o tun11 -j MASQUERADE 

重新启动,中提琴,你什么都没有…

然后,在VPN up脚本(最佳select)中,或者如果您的VPN以WAN启动,请将您的Administration / Scripts / WANUP

 sleep 30 ip route flush table 200 ip route flush cache ip rule add from 192.168.1.11 lookup 200 ip rule add from 192.168.1.13 lookup 200 VPN_GW=`ifconfig tun11 | awk '/inet addr/ {split ($2,A,":"); print A[2]}'` ip route add table 200 default via $VPN_GW dev tun11 

这个概率并不是“最好”或者“正确”的方式,但是它是有效的。 现在只有192.168.1.11192.168.2.13会通过OpenVPN隧道。 所有其他设备,他们的stream量将本地。

这对我来说非常重要,因为我不希望我的传输洪streamstream量通过VPN。 像SgtPepperKSU这样的人正在看这个,可能笑了…无论如何,从一个路由noob到另一个…

我做了一些不同的事情,但仍然完成了相同的事情。 以为我会在这里分享。

我将Tomato路由器设置为OpenVPN客户端,以便通过VPN路由所有互联网stream量。 我为此使用了StrongVPN.com的服务,他们提供了一步一步的指导,所以很容易让这部分工作顺利进行。 谷歌“番茄OpenVPN的设置教程StrongVPN”,你会发现它

但“select性路由只有某些主机”的事情是非常棘手的。

我结束了实施它作为一个“WAN UP”脚本(在番茄GUI:pipe理 – >脚本 – > WAN UP)。 下面的代码是我把它放进那个盒子里的。 它有很多意见来解释发生了什么事情。 它很大程度上取决于我在这里find的代码:

http://linksysinfo.org/index.php?threads/route-only-specific-ports-through-vpn-openvpn.37240/

阅读这篇文章是非常有帮助的:

http://linux-ip.net/html/adv-multi-internet.html

代码如下:

 # This code goes in the WAN UP section of the Tomato GUI. # # This script configures "selective" VPN routing. Normally Tomato will route ALL traffic out # the OpenVPN tunnel. These changes to iptables allow some outbound traffic to use the VPN, and some # traffic to bypass the VPN and use the regular Internet instead. # # To list the current rules on the router, issue the command: # iptables -t mangle -L PREROUTING # # Flush/reset all the rules to default by issuing the command: # iptables -t mangle -F PREROUTING # # # First it is necessary to disable Reverse Path Filtering on all # current and future network interfaces: # for i in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 0 > $i done # # Delete and table 100 and flush any existing rules if they exist. # ip route flush table 100 ip route del default table 100 ip rule del fwmark 1 table 100 ip route flush cache iptables -t mangle -F PREROUTING # # Copy all non-default and non-VPN related routes from the main table into table 100. # Then configure table 100 to route all traffic out the WAN gateway and assign it mark "1" # # NOTE: Here I assume the OpenVPN tunnel is named "tun11". # # ip route show table main | grep -Ev ^default | grep -Ev tun11 \ | while read ROUTE ; do ip route add table 100 $ROUTE done ip route add default table 100 via $(nvram get wan_gateway) ip rule add fwmark 1 table 100 ip route flush cache # # Define the routing policies for the traffic. The rules will be applied in the order that they # are listed. In the end, packets with MARK set to "0" will pass through the VPN. If MARK is set # to "1" it will bypass the VPN. # # EXAMPLES: # # All LAN traffic will bypass the VPN (Useful to put this rule first, so all traffic bypasses the VPN and you can configure exceptions afterwards) # iptables -t mangle -A PREROUTING -i br0 -j MARK --set-mark 1 # Ports 80 and 443 will bypass the VPN # iptables -t mangle -A PREROUTING -i br0 -p tcp -m multiport --dport 80,443 -j MARK --set-mark 1 # All traffic from a particular computer on the LAN will use the VPN # iptables -t mangle -A PREROUTING -i br0 -m iprange --src-range 192.168.1.2 -j MARK --set-mark 0 # All traffic to a specific Internet IP address will use the VPN # iptables -t mangle -A PREROUTING -i br0 -m iprange --dst-range 216.146.38.70 -j MARK --set-mark 0 # All UDP and ICMP traffic will bypass the VPN # iptables -t mangle -A PREROUTING -i br0 -p udp -j MARK --set-mark 1 # iptables -t mangle -A PREROUTING -i br0 -p icmp -j MARK --set-mark 1 # By default all traffic bypasses the VPN iptables -t mangle -A PREROUTING -i br0 -j MARK --set-mark 1 # Spotify explicitly uses the VPN iptables -t mangle -A PREROUTING -i br0 -m iprange --dst-range 78.31.8.1-78.31.15.254 -j MARK --set-mark 0 iptables -t mangle -A PREROUTING -i br0 -m iprange --dst-range 193.182.8.1-193.182.15.254 -j MARK --set-mark 0