Hi All
First off, this thread is a terrific source of information for selective routing. Thank you to everyone that has taken the time to contribute their knowledge and experience.
Based on many of the posts in this thread, I have
almost gotten my routing setup working.
My ideal situation is to have two active OpenVPN client configurations: client 1 (tun11) is configured to use a US VPN; client 2 (tun12) is configured to use a UK VPN. I want to route traffic to either the WAN, tun11 or tun12 based on the IP addresses of machines on my LAN.
Based on this thread - and some broader reading - my current openvpn-event script is as follows:
Code:
#!/bin/sh
# This code based on the contributions from these threads:
# http://www.linksysinfo.org/index.php?threads/route-only-specific-ports-through-vpn-openvpn.37240/
# http://www.snbforums.com/threads/selective-routing-with-asuswrt-merlin.9311/
# And from material in these articles:
# http://linux-ip.net/html/adv-multi-internet.html
# http://fedorasolved.org/Members/kanarip/iptables-howto
#
# This script configures selective VPN routing for Asuswrt-Merlin firmware and two OpenVPN
# client connections. These changes to iptables allow some outbound traffic to use OpenVPN
# client 1, some traffic to use OpenVPN client 2, and some traffic to bypass the VPN
# client connections and use the regular internet.
# The following two commands are listed for reference.
# 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 10, 20 and 30 and flush any existing rules if they exist.
# Table 10 will be used for the WAN/internet.
# Table 20 will be used for OpenVPN client 1, and table 30 for OpenVPN client 2.
ip route flush table 10
ip route del default table 10
ip rule del fwmark 10 table 10
ip route flush table 20
ip route del default table 20
ip rule del fwmark 20 table 20
ip route flush table 30
ip route del default table 30
ip rule del fwmark 30 table 30
ip route flush cache
iptables -t mangle -F PREROUTING
# Define "tun11" (OpenVPN client 1) and "tun12" (OpenVPN client 2).
# Associate table 10 with the WAN/internet, table 20 with "tun11" (OpenVPN client 1)
# and table 30 with "tun12" (OpenVPN client 2).
tun_if="tun11"
tun_ifs="tun12"
tun_ip=$(ifconfig $tun_if | grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}')
tun_ips=$(ifconfig $tun_ifs | grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}')
ip route add default via $(nvram get wan_gateway) dev eth0 table 10
ip rule add fwmark 10 table 10
ip route add default via $tun_ip dev $tun_if table 20
ip rule add fwmark 20 table 20
ip route add default via $tun_ips dev $tun_ifs table 30
ip rule add fwmark 30 table 30
# Define the routing policies for the traffic. The rules will be applied in the order that
# they are listed. Packets with MARK set to "10" will pass through the WAN/internet. If
# MARK is set to "20" it will pass through OpenVPN client 1. If MARK is set to "30" it
# will pass through OpenVPN client 2.
#
# 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 10
# 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 10
# All traffic from a particular computer on the LAN will use the OpenVPN client 1
# iptables -t mangle -A PREROUTING -i br0 -m iprange --src-range 192.168.1.2 -j MARK --set-mark 20
# All traffic to a specific Internet IP address will use the OpenVPN client 2
# iptables -t mangle -A PREROUTING -i br0 -m iprange --dst-range 216.146.38.70 -j MARK --set-mark 30
# All UDP and ICMP traffic will bypass the VPN
# iptables -t mangle -A PREROUTING -i br0 -p udp -j MARK --set-mark 10
# iptables -t mangle -A PREROUTING -i br0 -p icmp -j MARK --set-mark 10
# By default all traffic goes through the WAN/internet
iptables -t mangle -A PREROUTING -i br0 -j MARK --set-mark 10
# Set specific IP range traffic to go through OpenVPN client 1 (tun11)
iptables -t mangle -A PREROUTING -i br0 -m iprange --src-range 192.168.1.20-192.168.1.40 -j MARK --set-mark 20
#Set specific IP range traffic to go through OpenVPN client 2 (tun12)
iptables -t mangle -A PREROUTING -i br0 -m iprange --src-range 192.168.1.120-192.168.1.140 -j MARK --set-mark 30
exit
The routing appears to be working. If a machine on the LAN has an IP that is
outside the 192.168.1.20-192.168.1.40 and 192.168.1.120-192.168.1.140 ranges, the traffic routes over the WAN. If the machine has an IP within the 192.168.1.20-192.168.1.40 range, then it routes through tun11 (the US VPN). Similarly, if the machine has an IP within the 192.168.1.120-192.168.1.140 range, then it routes through tun12 (the UK VPN).
So far, so good.
But there is one remaining issue: DNS leaks. At the moment - regardless of whether traffic routes through the WAN, tun11 or tun 12 - all connections end up utilising the DNS server(s) specified in the 'WAN' --> 'WAN DNS Setting' section of the router's settings. This is not ideal. When traffic routes through the WAN, I want queries to resolve using my ISP's DNS. When traffic is routed through tun11 or tun12, however, I want queries to resolve using the respective DNS server(s) for that VPN (and
not my ISP's DNS).
Is there a simple way to achieve this outcome? Any help would be most appreciated!