Persistent routes for DD-WRT PPTP VPN client

dd-wrtpptproutingvpn

My home network in the USA is behind a Buffalo router (G300NH) running their version of DD-WRT. I use the built-in PPTP VPN client to connect to a VPN provider in the UK. I route certain traffic over the VPN (so it has a UK source address, for various entirely legal reasons) which I achieved by following the instructions in the DD-WRT docs and my VPN provider's own instructions. I placed two commands like this in the firewall script:

route add -net xxx.xxx.0.0 netmask 255.255.0.0 dev ppp0
route add -net yyy.yyy.0.0 netmask 255.255.0.0 dev ppp0

I didn't put any of the iptables rules in since it my setup doesn't seem to need them. It works like a charm. Traffic to the xxx subnets goes over the VPN, everything else goes out over my ISP's own pipes.

The problem comes when the VPN drops, which it does occasionally. DD-WRT does a fine job of reconnecting it automatically, but the routes are trashed every time that happens.

How do I automate the process of re-establishing my routes? I thought about static routes, but the IP address of the VPN connection is dynamically assigned (which is why I'm using dev ppp0).

Best Answer

The sed command works fine except that dd-wrt does not create the ip-up file until after the pptpd daemon is started. So I added a delay to the startup script to give time and also execute the ip-up command after adding the routes. This should solve it for you.

Here is the startup command for your router:

sleep 40
sed -i '' -e 's|exit 0|route add -net xxx.xxx.0.0 netmask 255.255.0.0 dev ppp0\nroute add -net yyy.yyy.0.0 netmask 255.255.0.0 dev ppp0\n&\n|' /tmp/pptpd_client/ip-up
/tmp/pptpd_client/ip-up

You could alternatively do this:

sleep 40
sed -i '' -e 's|exit 0|route add -net xxx.xxx.0.0 netmask 255.255.0.0 dev ppp0\n&\n|' /tmp/pptpd_client/ip-up
sed -i '' -e 's|exit 0|route add -net xxx.xxx.0.0 netmask 255.255.0.0 dev ppp0\n&\n|' /tmp/pptpd_client/ip-up
sed -i '' -e 's|exit 0|route add -net xxx.xxx.0.0 netmask 255.255.0.0 dev ppp0\n&\n|' /tmp/pptpd_client/ip-up
/tmp/pptpd_client/ip-up

adding as many routes as needed. The & replaces the exit 0.

Related Question