{"id":49,"date":"2013-08-22T18:47:41","date_gmt":"2013-08-22T17:47:41","guid":{"rendered":"http:\/\/eggblog.invertedegg.com\/?p=49"},"modified":"2013-08-22T18:47:41","modified_gmt":"2013-08-22T17:47:41","slug":"tunneling-smtp-tcp-port-25-through-a-vpn","status":"publish","type":"post","link":"http:\/\/eggblog.invertedegg.com\/?p=49","title":{"rendered":"Tunneling SMTP (TCP port 25) through a VPN"},"content":{"rendered":"<p>I&#8217;ve recently switched providers (having moved countries), and am now reconstructing my services in a new location, using AT&amp;T UVerse. \u00c2\u00a0I continue to have an account with StrongVPN that I use (I originally acquired it to give me a US IP for use when out of the US &#8230; side note, I&#8217;m really happy with StrongVPN).<\/p>\n<p>The problem is that UVerse blocks outbound SMTP (port 25) traffic, and doesn&#8217;t provide their own relay (or, more accurately, won&#8217;t relay mail that&#8217;s neither to nor from an AT&amp;T address). \u00c2\u00a0I don&#8217;t have much mail to send (just what the kids generate, and the occasional system alert), so I don&#8217;t feel that I&#8217;m much of a threat to anyone&#8217;s traffic. \u00c2\u00a0It took me a while to figure this out (I&#8217;m not the world&#8217;s greatest IP routing guru), so I figured it might be of use to you.<\/p>\n<p>Many thanks to <a href=\"http:\/\/serverfault.com\/users\/51929\/lekensteyn\">Lekensteyn<\/a> and the other contributors to the post &#8220;<a href=\"http:\/\/serverfault.com\/questions\/345111\/iptables-target-to-route-packet-to-specific-interface\">iptables &#8211; Target to Route Packet to Specific Interface<\/a>&#8221; on serverfault.com for key pointers.<\/p>\n<p>Objective: \u00c2\u00a0Running a postfix SMTP server on Ubuntu Linux, route all outbound SMTP through a VPN tunnel. \u00c2\u00a0We&#8217;re going to wind up not using a relay server, and just directly connecting to the target hosts.<\/p>\n<p>We&#8217;ll assume that your tunnel interface is TUN0 &#8230; replace this below as you see fit.<\/p>\n<h4>1. Remove any relays from your postfix configuration<\/h4>\n<p>If you&#8217;re coming from a previous configuration, you were probably configured with a relay server. \u00c2\u00a0You need to remove it, so that you&#8217;re directly connecting to your target hosts (if you had a relay server, you probably don&#8217;t need to force the traffic anywhere! \u00c2\u00a0On the other hand, if you&#8217;re trying to route port 25 for some other reason, then skip this step.)<\/p>\n<h5 style=\"padding-left: 30px;\">a. Edit \/etc\/postfix\/main.cf<\/h5>\n<p style=\"padding-left: 30px;\">Edit this file to remove or comment out the line that established your relay host:<\/p>\n<pre style=\"padding-left: 30px;\"># relayhost = smtp.mypreviousprovider.nl<\/pre>\n<h4>2. Force SMTP Port 25 through the VPN<\/h4>\n<p>I have a very complex routing scheme, with a ton of subnets that I use for all sorts of things &#8212; for example, forcing a wifi AP out through the VPN (so that you can connect to a specific AP to auto-use the VPN), keeping the kids on a different subnet so that I can force them out a different Internet connection, and blackholing unknown devices so that the kids can&#8217;t hook anything up without me verifying the MAC address &#8230; drop me a comment if you&#8217;re interested in any of these things &#8230; so the long and short of it is that I already have a routing script that I use to set up all my routing tables.<\/p>\n<p>If you already have such a script, just add the additions below to that script. \u00c2\u00a0If not, then create a new script for these commands. \u00c2\u00a0It&#8217;s a somewhat separate exercise to hook it up so that it&#8217;s properly invoked whenever you bring an interface up or down (which I leave as a Google exercise for you, as it&#8217;s not fresh on my mind) &#8230; but in the worst case, you can just run it manually whenever your routing tables get rebuilt.<\/p>\n<p style=\"padding-left: 30px;\">Note that we&#8217;re going to use &#8220;7&#8221; as the mark for our rules. \u00c2\u00a0This is arbitrary.<\/p>\n<h5 style=\"padding-left: 30px;\">a. \u00c2\u00a0If you haven&#8217;t already, create a routing table for the VPN<\/h5>\n<p style=\"padding-left: 30px;\">I already had a routing table set up &#8230; if you do then just use that routing table, below. \u00c2\u00a0But if you don&#8217;t, here&#8217;s a quick and dirty routing table to push over your VPN (please note, I&#8217;m just typing this, and haven&#8217;t actually tested the exact lines below, as I don&#8217;t need them in my setup!!):<\/p>\n<pre style=\"padding-left: 30px;\"># For clarity, clear anything that might have accumulated there. \u00c2\u00a0Ignore any error, here.<\/pre>\n<pre style=\"padding-left: 30px;\">ip route flush table vpn_table<\/pre>\n<pre style=\"padding-left: 30px;\"># Push all traffic that goes to this table out the VPN. \u00c2\u00a0Substitute your VPN's gateway for 99.99.99.99 below.<\/pre>\n<pre style=\"padding-left: 30px;\">iproute add default via 99.99.99.99 table vpn_table<\/pre>\n<pre style=\"padding-left: 30px;\"># And be sure to flush to pick it up<\/pre>\n<pre style=\"padding-left: 30px;\">iproute flush cache<\/pre>\n<h5 style=\"padding-left: 30px;\">b. Now mark all SMTP port 25 packets with 7<\/h5>\n<pre style=\"padding-left: 30px;\">iptables -t mangle -A OUTPUT -p tcp --dport 25 -j MARK --set-mark 7<\/pre>\n<h5 style=\"padding-left: 30px;\">c. Set the source IP to our ID on the VPN (substitute for 88.88.88.88) rather than our local network ID. \u00c2\u00a0Remember to use your correct interface if not TUN0, below.<\/h5>\n<pre style=\"padding-left: 30px;\">iptables -t nat -A POSTROUTING -o tun0 -j SNAT --to-source 88.88.88.88<\/pre>\n<h5 style=\"padding-left: 30px;\">d. Send everything marked with 7 to the VPN table (to force out the VPN)<\/h5>\n<pre style=\"padding-left: 30px;\">ip rule add fwmark 25 table vpn_table<\/pre>\n<h5 style=\"padding-left: 30px;\">e. Relax the reverse path source validation<\/h5>\n<p style=\"padding-left: 30px;\">(See the post for a discussion.)<\/p>\n<pre style=\"padding-left: 30px;\">sysctl -w net.ipv4.conf.tun0.rp_filter=2<\/pre>\n<h5 style=\"padding-left: 30px;\">f. And flush for good measure<\/h5>\n<pre style=\"padding-left: 30px;\">ip route flush cache<\/pre>\n<p>That should do it! \u00c2\u00a0Run your script, and all your port 25 traffic should be running out your VPN. \u00c2\u00a0Obviously, you can adapt the concepts here for other applications.<\/p>\n<p style=\"padding-left: 30px;\">\n<p style=\"padding-left: 30px;\">\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve recently switched providers (having moved countries), and am now reconstructing my services in a new location, using AT&amp;T UVerse. \u00c2\u00a0I continue to have an account with StrongVPN that I use (I originally acquired it to give me a US IP for use when out of the US &#8230; side note, I&#8217;m really happy with &hellip; <a href=\"http:\/\/eggblog.invertedegg.com\/?p=49\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Tunneling SMTP (TCP port 25) through a VPN&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/posts\/49"}],"collection":[{"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=49"}],"version-history":[{"count":1,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":50,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions\/50"}],"wp:attachment":[{"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/eggblog.invertedegg.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}