{"id":3181,"date":"2013-07-16T13:35:01","date_gmt":"2013-07-16T05:35:01","guid":{"rendered":"http:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/?p=3181"},"modified":"2013-07-16T15:29:25","modified_gmt":"2013-07-16T07:29:25","slug":"load-balancing-nat-ing-multiple-uplinks-on-linux","status":"publish","type":"post","link":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/?p=3181","title":{"rendered":"Load balancing &#038; NAT-ing multiple uplinks on Linux"},"content":{"rendered":"<p>LAN: eth0: 192.168.0.1\/24<\/p>\n<p>IP1: eth1: 192.168.1.1\/24, gateway: 192.168.1.2\/24<\/p>\n<p>IP2: eth2: 192.168.2.1\/24, gateway: 192.168.2.2\/24<\/p>\n<p>So here is how I would do by using iptables method:<\/p>\n<h2>Route tables<\/h2>\n<p>First edit the \/etc\/iproute2\/rt_tables to add a map between route table numbers and ISP names<\/p>\n<pre><code>...\u00a0<\/code><\/pre>\n<pre><code>10 IP1\u00a0<\/code><\/pre>\n<pre><code>20 IP2\u00a0<\/code><\/pre>\n<pre><code>... <\/code><\/pre>\n<p>So table 10 and 20 is for ISP1 and ISP2, respectively. I need to populate these tables with routes from main table with this code snippet (which I have taken from hxxp:\/\/linux-ip.net\/html\/adv-multi-internet.html)<\/p>\n<pre><strong><code>#!\/bin\/bash ip route show table main | grep -Ev '^default' | while read ROUTE ; do \r\nip route add table IP1 $ROUTE \r\ndone <\/code><\/strong><\/pre>\n<p>And add default gateway to ISP1 through that ISP1&#8217;s gateway:<\/p>\n<pre><code>ip route add default via 192.168.1.2 table IP1 <\/code><\/pre>\n<p>Do the same for IP2<\/p>\n<p>So now I have 2 route tables, 1 for each IP.<\/p>\n<h2>Iptables<\/h2>\n<p>OK now I use iptables to evenly distribute packets to each route tables. More info on how this work can be found here (<a href=\"http:\/\/www.diegolima.org\/wordpress\/?p=36\">http:\/\/www.diegolima.org\/wordpress\/?p=36<\/a>) and here (<a href=\"http:\/\/home.regit.org\/?page%5Fid=7\">http:\/\/home.regit.org\/?page%5Fid=7<\/a>)<\/p>\n<pre><code># iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark\u00a0<\/code><\/pre>\n<pre><code># iptables -t mangle -A PREROUTING -m mark ! --mark 0 -j ACCEPT\u00a0<\/code><\/pre>\n<pre><code># iptables -t mangle -A PREROUTING -j MARK --set-mark 10\u00a0<\/code><\/pre>\n<pre><code># iptables -t mangle -A PREROUTING -m statistic --mode random --probability 0.5 -j MARK --set-mark 20\u00a0<\/code><\/pre>\n<pre><code># iptables -t mangle -A PREROUTING -j CONNMARK --save-mark <\/code><\/pre>\n<h2>NAT<\/h2>\n<p>Well NAT is easy:<\/p>\n<pre><code># iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE\u00a0<\/code><\/pre>\n<pre><code># iptables -t nat -A POSTROUTING -o eth2 -j MASQUERADE<\/code><\/pre>\n<pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>LAN: eth0: 192.168.0.1\/24<\/p>\n<p>IP1: eth1: 192.168.1.1\/24, gateway: 192.168.1.2\/24<\/p>\n<p>IP2: eth2: 192.168.2.1\/24, gateway: 192.168.2.2\/24<\/p>\n<p>So here is how I would do by using iptables method:<\/p>\n<p> Route tables <\/p>\n<p>First edit the \/etc\/iproute2\/rt_tables to add a map between route table numbers and ISP names<\/p>\n<p> &#8230; 10 IP1 20 IP2 &#8230; <\/p>\n<p>So table 10 and 20 is for ISP1 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[3],"tags":[57],"class_list":["post-3181","post","type-post","status-publish","format-standard","hentry","category-computer","tag-linux","odd"],"_links":{"self":[{"href":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3181"}],"version-history":[{"count":8,"href":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3181\/revisions"}],"predecessor-version":[{"id":3190,"href":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3181\/revisions\/3190"}],"wp:attachment":[{"href":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staff.ie.cuhk.edu.hk\/~sfluk\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}