Route some traffic through VPN, some through isp

Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking
Goto page 1, 2  Next
Author Message
Jeankri
DD-WRT Novice


Joined: 29 Dec 2014
Posts: 5

PostPosted: Fri Jan 02, 2015 9:26    Post subject: Route some traffic through VPN, some through isp Reply with quote
Hello,

I have set up a pptp VPN as main connection of my Dd wrt router which is behind my ISP modem. I want to use this connection only for one computer and one port. The others should directly go to internet through my ISP.

Vlan1 is my LAN, vlan2 is the WAN, ppp0 is the tunnel.

How can I do that ? Should I build a Vlan3 between my router and my modem ? Should I use iptables commands to route the traffic properly ?

Please help,

Jean-Christophe
Sponsor
Jeankri
DD-WRT Novice


Joined: 29 Dec 2014
Posts: 5

PostPosted: Fri Jan 02, 2015 16:52    Post subject: Reply with quote
Thx.

Is it possible to make policy based routing with vlan2 (wan) and ppp0 (main connection of the router, not simply pptp client), or should I use the pptp client config ?
Jeankri
DD-WRT Novice


Joined: 29 Dec 2014
Posts: 5

PostPosted: Fri Jan 02, 2015 20:03    Post subject: Reply with quote
To configuré a VPN, you can do two things : either configure the wan connection to use pptp or use the pptp VPN client. I used the first one. Does that mean that all the traffic is routed trough VPN ? Should I use the pptp VPN client, if I want to do policy based routing ?
Jeankri
DD-WRT Novice


Joined: 29 Dec 2014
Posts: 5

PostPosted: Fri Jan 02, 2015 21:13    Post subject: Reply with quote
Isn't there a way through iptables commands, because there --dports options ?
psufan5
DD-WRT Novice


Joined: 19 Dec 2014
Posts: 19

PostPosted: Thu Jan 15, 2015 1:15    Post subject: Reply with quote
I do this on my router. This is my firewall setup to do so (I am assuming you have your VPN setup correctly and it connects).

What its doing: Basically I have it setup so my router gives out DHCP addresses from 192.168.1.100 to 192.168.1.105. Any address obtained by DHCP will bypass the VPN. Address 192.168.1.113 is a static server I have bypassing it as well. Any other IP address (I always static address my machines) will go through my VPN which is PrivateInternetAccess.

This is also a VPN killswitch - meaning if your VPN drops out, it will not allow traffic from any of the static IPs not bypassing it.

#----------------------------------------------------

WAN_GTWY="$(nvram get wan_gateway)"
WAN_IF="$(nvram get wan_iface)"

ip route add default via $WAN_GTWY dev $WAN_IF table 10

ip rule add from 192.168.1.100 table 10
ip rule add from 192.168.1.101 table 10
ip rule add from 192.168.1.102 table 10
ip rule add from 192.168.1.103 table 10
ip rule add from 192.168.1.104 table 10
ip rule add from 192.168.1.105 table 10
ip rule add from 192.168.1.113 table 10
#----------------------------------------------------

iptables -I FORWARD -i br0 -o tun0 -j ACCEPT
iptables -I FORWARD -i tun0 -o br0 -j ACCEPT
iptables -I FORWARD -i br0 -o vlan2 -j DROP
iptables -I FORWARD -i br0 -s 192.168.1.100 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.101 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.102 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.103 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.104 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.105 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.113 -o vlan2 -j ACCEPT
iptables -I INPUT -i tun0 -j REJECT
iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE




I set it up like this because I have 2 chomecasts which you can't static IP, a slingbox, and a TiVo. They take up my DHCP bypass addresses (all but 2) and the rest is static going through the VPN tunnel.
kabadisha
DD-WRT Novice


Joined: 13 Mar 2010
Posts: 34

PostPosted: Sat May 14, 2016 12:50    Post subject: My solution Reply with quote
Hi Guys,

Thanks for sharing all you solutions - I thought I would do the same for anyone else who finds this thread.

My config:
    - Routes all traffic from a single LAN IP over the VPN.
    - Prevents traffic from the same LAN IP from reaching the internet when the VPN is down
      - This is often referred to as a kill switch
    - Routes all traffic destined for a specific port over the VPN
    - Prevents requests to the same port when the VPN is down

Under Administration > Commands, save the following as a custom script:

Code:
#!/bin/sh
# Some MASQUERADE line that I don't really understand.
iptables -I POSTROUTING -t nat -o tun1 -j MASQUERADE

# Set the default route for table 200 as over the VPN
ip route add default dev tun1 table 200

# Assign all outgoing connections from 192.168.11 to table 200 (so they go over the VPN)
ip rule add from 192.168.1.11 table 200

# Assign all packets marked with 11 to table 200 (so they go over the VPN)
ip rule add fwmark 11 table 200

# Flush the cache
ip route flush cache

# Mark all tcp packets whos destination port is 563 with 11 (so that it will be routed over the VPN)
iptables -t mangle -I PREROUTING -p tcp --dport 563 -j MARK --set-mark 11

Under Administration > Commands, save the following to the firewall script:

Code:
# Prevent 192.168.1.11 from reaching the internet directly (so no connection if VPN down)
iptables -I FORWARD -i br0 -s 192.168.1.11 -o vlan2 -j DROP

# Prevent 192.168.1.11 from connecting to port 563 (NZB servers) directly (so no connection if VPN down)
iptables -I FORWARD -i br0 -s 192.168.1.11 -p tcp --dport 563 -o vlan2 -j DROP

Under additional config for OpenVPN client:

Code:
# Write to a log file for easy viewing
log /tmp/tigervpn.log

# Mute messages that repeat a bunch of times
mute 50

# Do not accept the routes provided by the VPN server
# (will manage those myself)
route-nopull

# Keep the connection alive and attempt to reestablish it if it dies
keepalive 10 60

# Additional settings specified by VPN provider
tls-client
remote-cert-tls server

# Dont use auth-nocache as it prevents reconnection due to a bug
# auth-nocache

# Script to run when the link is established
# This sets up my custom routes and iptables rules
up /tmp/custom.sh

Generated OpenVPN config (viewable under '/tmp/openvpncl/openvpn.conf' on the router) looks like this:

Code:
ca /tmp/openvpncl/ca.crt
management 127.0.0.1 16
management-log-cache 100
verb 3
mute 3
syslog
writepid /var/run/openvpncl.pid
client
resolv-retry infinite
nobind
persist-key
persist-tun
script-security 2
dev tun1
proto udp
cipher aes-256-cbc
auth sha1
auth-user-pass /tmp/openvpncl/credentials
remote zur.tigervpn.com 1194
comp-lzo adaptive
tun-mtu 1500
mtu-disc yes
fast-io
tun-ipv6
# Write to a log file for easy viewing
log /tmp/tigervpn.log

# Mute messages that repeat a bunch of times
mute 50

# Do not accept the routes provided by the VPN server
# (will manage those myself)
route-nopull

# Keep the connection alive and attempt to reestablish it if it dies
keepalive 10 60

# Additional settings specified by VPN provider
tls-client
remote-cert-tls server

# Script to run when the link is established
# This sets up my custom routes and iptables rules
up /tmp/custom.sh


Hope this helps someone Smile
kabadisha
DD-WRT Novice


Joined: 13 Mar 2010
Posts: 34

PostPosted: Sat May 14, 2016 22:04    Post subject: Created a guide Reply with quote
Hey Guys,

Just a note to say that I have created a guide on how to do this with a bit more detail. Available here:

https://charleswilkinson.co.uk/2016/05/14/selective-routing-using-ddwrt-and-openvpn/

Cheers
Nilugeator
DD-WRT Novice


Joined: 31 Jul 2016
Posts: 32

PostPosted: Wed Aug 03, 2016 22:13    Post subject: need help to open portsin https (to bypass VPN) Reply with quote
Hi all,I used those wonderfull script to allow 3 of my peripherals to bypass my VPN

this works great

But now I also want to let some ports (8080 and 9091) to bypass the VPN too, I tried the following, but it didnt work

This is certainly because they also exist in the "normal " table (table 100?)





Any help will be appreciated! thanks in advance guys Smile

WAN_GTWY="$(nvram get wan_gateway)"
WAN_IF="$(nvram get wan_iface)"

ip route add default via $WAN_GTWY dev $WAN_IF table 10

#autorise le MSI
ip rule add from 192.168.1.125 table 10
#autorise la tablette
ip rule add from 192.168.1.111 table 10
#autorise le shield
ip rule add from 192.168.1.141 table 10
# Assign all packets marked with 11 to table 10 (so they go over the WAN)
ip rule add fwmark 11 table 10

# Flush the cache
ip route flush cache

# Mark all tcp packets whos destination port is 9091 with 11 (so that it will be routed over the WAN)

iptables -t mangle -A OUTPUT -p tcp -m multiport --sport 8080 -j MARK --set-mark 11
iptables -t mangle -A OUTPUT -p tcp -m multiport --sport 9091 -j MARK --set-mark 11




#----------------------------------------------------

iptables -I FORWARD -i br0 -o tun1 -j ACCEPT
iptables -I FORWARD -i tun1 -o br0 -j ACCEPT
iptables -I FORWARD -i br0 -o vlan2 -j DROP
iptables -I FORWARD -i br0 -s 192.168.1.125 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.111 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.141 -o vlan2 -j ACCEPT
iptab
iptables -I INPUT -i tun1 -j REJECT
iptables -t nat -A POSTROUTING -o tun1 -j MASQUERADE
dave4763
DD-WRT Novice


Joined: 08 Dec 2016
Posts: 1

PostPosted: Thu Dec 08, 2016 3:25    Post subject: Reply with quote
Great topic. I'm curious. Will each device that is routed through the VPN still be able to file share with each other? Will The non VPN IP's be able to file share with the VPN devices?

If there is no isolation between VPN devices and non-VPN devices, how would you recommend creating that isolation? Setting up different virtual LANs with different gateways perhaps?

Thanks for contributing!
nashchelsy
DD-WRT Novice


Joined: 26 Jul 2017
Posts: 5

PostPosted: Fri Aug 18, 2017 21:05    Post subject: Reply with quote
I am unable to get this to work. I tried your walk through

https://charleswilkinson.co.uk/2016/05/14/selective-routing-using-ddwrt-and-

But still it will not work

I have a R8000 and am using PIA
Also, I have the Router set to DHCP Forward to my dhcp server on my network. Could that be what is causing it? If so, is there a work around?

I am able to get the VPN to connect, and the firewall script will block the specified IP(s) from getting out of the network. But the custom script it will not route the ip to the VPN. I've verified everything it typed correctly.

I have tried to disable the "route-nopull" and all devices on my network get the VPN connection. But the specified IPs still won't get VPN access. And the firewall still blocks the specified IP(s) from getting out of the network due to this. Just access to the local network.

It looks to be something with the custom script:

# Some MASQUERADE line that I don't really understand.
iptables -I POSTROUTING -t nat -o tun1 -j MASQUERADE

# Set the default route for table 200 as over the VPN
ip route add default dev tun1 table 200

# Assign all outgoing connections from 10.0.0.101 to table 200 (so they go over the VPN)
ip rule add from 10.0.0.101 table 200

# Flush the cache
ip route flush cache

As I said in the beginning, I have the Router set to DHCP Forward to my dhcp server on my network. Could that be what is causing it? If so, is there a work around?
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12915
Location: Netherlands

PostPosted: Sat Aug 19, 2017 8:25    Post subject: Reply with quote
see: http://www.dd-wrt.com/phpBB2/viewtopic.php?t=307192&highlight=pbr+destination
for @Eibgrad's solution and script

_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
nashchelsy
DD-WRT Novice


Joined: 26 Jul 2017
Posts: 5

PostPosted: Sat Aug 19, 2017 13:26    Post subject: Reply with quote
Thanks for the quick reply. Took a look at the script. It looks perfect. I'll use that to see if it fixed the issue. If you don't hear back, everything went perfect!
wadeshuler
DD-WRT Novice


Joined: 20 Aug 2017
Posts: 10

PostPosted: Tue Aug 22, 2017 7:27    Post subject: Reply with quote
It would be awesome to detect the difference between the Amazon Firestick and Kodi (side loaded on the Firestick) Smile

Amazon Firestick = ISP traffic
Kodi = VPN

_________________
Router: Linksys 1900ACS v2
Firmware: DD-WRT v3.0-r33555 std (10/20/17)
yorchz
DD-WRT Novice


Joined: 14 May 2017
Posts: 14

PostPosted: Sun Sep 03, 2017 16:27    Post subject: Reply with quote
psufan5 wrote:
I do this on my router. This is my firewall setup to do so (I am assuming you have your VPN setup correctly and it connects).

What its doing: Basically I have it setup so my router gives out DHCP addresses from 192.168.1.100 to 192.168.1.105. Any address obtained by DHCP will bypass the VPN. Address 192.168.1.113 is a static server I have bypassing it as well. Any other IP address (I always static address my machines) will go through my VPN which is PrivateInternetAccess.

This is also a VPN killswitch - meaning if your VPN drops out, it will not allow traffic from any of the static IPs not bypassing it.

#----------------------------------------------------

WAN_GTWY="$(nvram get wan_gateway)"
WAN_IF="$(nvram get wan_iface)"

ip route add default via $WAN_GTWY dev $WAN_IF table 10

ip rule add from 192.168.1.100 table 10
ip rule add from 192.168.1.101 table 10
ip rule add from 192.168.1.102 table 10
ip rule add from 192.168.1.103 table 10
ip rule add from 192.168.1.104 table 10
ip rule add from 192.168.1.105 table 10
ip rule add from 192.168.1.113 table 10
#----------------------------------------------------

iptables -I FORWARD -i br0 -o tun0 -j ACCEPT
iptables -I FORWARD -i tun0 -o br0 -j ACCEPT
iptables -I FORWARD -i br0 -o vlan2 -j DROP
iptables -I FORWARD -i br0 -s 192.168.1.100 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.101 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.102 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.103 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.104 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.105 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.113 -o vlan2 -j ACCEPT
iptables -I INPUT -i tun0 -j REJECT
iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE




I set it up like this because I have 2 chomecasts which you can't static IP, a slingbox, and a TiVo. They take up my DHCP bypass addresses (all but 2) and the rest is static going through the VPN tunnel.



PSUFAN5 Could you assist on this? Or someone that help me to figure it out this issue?

So this is the script I've been using for months but for some reason in the latest DD-WRT versions of my router stopped working. I have a WRT3200 and this script works on any version below Jul 7 2017, unfortunately this version have an old WIFI driver that drops the 5Ghz connection so that is the reason I updated it to the latest version but the script do not work anymore.


Basically this IPs used to bypass the VPN connection but not anymore the error now is that all the ips listed here do not have internet anymore any ideas??

#----------------------------------------------------

WAN_GTWY="$(nvram get wan_gateway)"
WAN_IF="$(nvram get wan_iface)"

ip route add default via $WAN_GTWY dev $WAN_IF table 10

ip rule add from 192.168.1.100 table 10
ip rule add from 192.168.1.101 table 10
ip rule add from 192.168.1.102 table 10
ip rule add from 192.168.1.103 table 10
ip rule add from 192.168.1.104 table 10
ip rule add from 192.168.1.105 table 10
ip rule add from 192.168.1.106 table 10
ip rule add from 192.168.1.107 table 10
ip rule add from 192.168.1.108 table 10
ip rule add from 192.168.1.109 table 10
ip rule add from 192.168.1.110 table 10
ip rule add from 192.168.1.111 table 10
ip rule add from 192.168.1.112 table 10
ip rule add from 192.168.1.113 table 10
ip rule add from 192.168.1.114 table 10
ip rule add from 192.168.1.115 table 10
ip rule add from 192.168.1.116 table 10
ip rule add from 192.168.1.117 table 10
ip rule add from 192.168.1.118 table 10
ip rule add from 192.168.1.119 table 10
ip rule add from 192.168.1.120 table 10
ip rule add from 192.168.1.121 table 10
ip rule add from 192.168.1.122 table 10
ip rule add from 192.168.1.123 table 10
ip rule add from 192.168.1.124 table 10
ip rule add from 192.168.1.125 table 10
#----------------------------------------------------

iptables -I FORWARD -i br0 -o tun0 -j ACCEPT
iptables -I FORWARD -i tun0 -o br0 -j ACCEPT
iptables -I FORWARD -i br0 -o vlan2 -j DROP
iptables -I FORWARD -i br0 -s 192.168.1.100 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.101 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.102 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.103 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.104 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.105 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.106 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.107 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.108 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.109 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.110 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.111 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.112 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.113 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.114 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.115 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.116 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.117 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.118 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.119 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.120 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.121 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.122 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.123 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.124 -o vlan2 -j ACCEPT
iptables -I FORWARD -i br0 -s 192.168.1.125 -o vlan2 -j ACCEPT
iptables -I INPUT -i tun0 -j REJECT
iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
egc
DD-WRT Guru


Joined: 18 Mar 2014
Posts: 12915
Location: Netherlands

PostPosted: Sun Sep 03, 2017 16:41    Post subject: Reply with quote
Try disabling SFE
_________________
Routers:Netgear R7000, R6400v1, R6400v2, EA6900 (XvortexCFE), E2000, E1200v1, WRT54GS v1.
Install guide R6400v2, R6700v3,XR300:https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=316399
Install guide R7800/XR500: https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=320614
Forum Guide Lines (important read):https://forum.dd-wrt.com/phpBB2/viewtopic.php?t=324087
Goto page 1, 2  Next Display posts from previous:    Page 1 of 2
Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum