{"id":980,"date":"2016-06-01T16:43:25","date_gmt":"2016-06-01T14:43:25","guid":{"rendered":"http:\/\/www.hauweele.net\/~gawen\/blog\/?p=980"},"modified":"2016-06-01T16:45:05","modified_gmt":"2016-06-01T14:45:05","slug":"redirect-traffic-to-loopback","status":"publish","type":"post","link":"https:\/\/hauweele.net\/~gawen\/blog\/?p=980","title":{"rendered":"Redirect traffic to loopback"},"content":{"rendered":"<p>Today I wanted to transparantly redirect the DNS requests coming at the output of a tunnel to a local caching DNS resolver. The caching DNS was listening only on the loopback as port 53 was already bound to other interfaces. That would be fairly simple on Linux:<\/p>\n<pre>\r\necho 1 > \/proc\/sys\/net\/ipv4\/ip_forward\r\n\r\niptables -t nat -A PREROUTING -i tun0 -p udp --dport 53 -j DNAT --to-destination 127.0.0.1\r\niptables -A FORWARD -i tun0 -o lo -p udp --dport 53 -j ACCEPT\r\n<\/pre>\n<p>But&#8230; The kernel will refuse to route packets with the loopback as source or destination because this qualify as a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Martian_packet\">martian packet<\/a>. The solution was to enable the <code>route_localnet<\/code> flag. As stated in the <a href=\"https:\/\/www.kernel.org\/doc\/Documentation\/networking\/ip-sysctl.txt\">kernel documentation<\/a>:<\/p>\n<blockquote><p>\nroute_localnet &#8211; BOOLEAN: Do not consider loopback addresses as martian source or destination while routing. This enables the use of 127\/8 for local routing purposes (default FALSE).\n<\/p><\/blockquote>\n<p>This is per interface. So I just had to enable this on the tunnel interface:<\/p>\n<pre>\r\necho 1 > \/proc\/sys\/net\/ipv4\/conf\/tun0\/route_localnet\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today I wanted to transparantly redirect the DNS requests coming at the output of a tunnel to a local caching DNS resolver. The caching DNS was listening only on the loopback as port 53 was already bound to other interfaces. &hellip; <a href=\"https:\/\/hauweele.net\/~gawen\/blog\/?p=980\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[645,6,686,624,687,688,689,404],"class_list":["post-980","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-interface","tag-linux","tag-loopback","tag-martian","tag-packet","tag-redirect","tag-route_localnet","tag-routing"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=\/wp\/v2\/posts\/980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=980"}],"version-history":[{"count":0,"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=\/wp\/v2\/posts\/980\/revisions"}],"wp:attachment":[{"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}