Skip to content

Instantly share code, notes, and snippets.

@j67678
Forked from systembell/wan-up
Created April 5, 2016 10:10
Show Gist options
  • Save j67678/7ff454b9cc05a727b8dd4cffc92ee758 to your computer and use it in GitHub Desktop.
Save j67678/7ff454b9cc05a727b8dd4cffc92ee758 to your computer and use it in GitHub Desktop.
Selective VPN routing in Tomato
# This code goes in the WAN UP section of the Tomato GUI.
#
# 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"
#
# Assumption: 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.
#
# By default all traffic bypasses the VPN
iptables -t mangle -A PREROUTING -i br0 -j MARK --set-mark 1
# p2p / usenet specifically uses the VPN
iptables -t mangle -A PREROUTING -i br0 -m multiport --dport 119,563 -j MARK --set-mark 0
iptables -t mangle -A PREROUTING -i br0 -m multiport --dport 6681-6889 -j MARK --set-mark 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment