Sending specific traffic over a VPN connection on OSX
For a while I’ve had to go back and forth between using the “Send all traffic over VPN connection” setting and not using it. Generally I want the setting off, so that all my network traffic doesn’t get slowed down as it bounces through extra servers. However, sometimes I need to access computers in the office that aren’t on the default VPN subnet.
Recently I found this link: http://blog.liip.ch/archive/2006/01/07/changing-default-routes-on-os-x-on-vpn.html which describes how to tweak the routing settings. However, it’s a bit out of date; as of that writing, any time you used the built-in VPN setup, all traffic would go over the connection.
In any case, let’s say you have a VPN where the default subnet is 192.168.2.x but some servers are on 192.168.3.x addresses. When you connect to the VPN, requests to 192.168.2.x addresses work just fine, but trying to access the others ends up trying to send the requests over the Internet, which of course won’t work.
To fix this, create a file called /etc/ppp/ip-up and put the following in it:
#!/bin/sh /sbin/route -n add -net 192.168.3 $IPREMOTE >> /tmp/ppp.log 2>&1
You’ll need to create the file as root, and then set it executable. Next time you connect to your VPN, the system will automatically run that file, having set the IPREMOTE environment variable to the IP address of the VPN gateway server. It will make a log of these changes in /tmp/ppp.log for future reference.
This tells your computer to always try routing 192.168.3.x addresses through that gateway. And you can stop sending *all* your network connections through the VPN. :)
When you disconnect from the VPN, it automatically removes the route, so you shouldn’t need to do any other cleanup.
2 years ago