···11+Transparent proxy support22+=========================33+44+This feature adds Linux 2.2-like transparent proxy support to current kernels.55+To use it, enable NETFILTER_TPROXY, the socket match and the TPROXY target in66+your kernel config. You will need policy routing too, so be sure to enable that77+as well.88+99+1010+1. Making non-local sockets work1111+================================1212+1313+The idea is that you identify packets with destination address matching a local1414+socket on your box, set the packet mark to a certain value, and then match on that1515+value using policy routing to have those packets delivered locally:1616+1717+# iptables -t mangle -N DIVERT1818+# iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT1919+# iptables -t mangle -A DIVERT -j MARK --set-mark 12020+# iptables -t mangle -A DIVERT -j ACCEPT2121+2222+# ip rule add fwmark 1 lookup 1002323+# ip route add local 0.0.0.0/0 dev lo table 1002424+2525+Because of certain restrictions in the IPv4 routing output code you'll have to2626+modify your application to allow it to send datagrams _from_ non-local IP2727+addresses. All you have to do is enable the (SOL_IP, IP_TRANSPARENT) socket2828+option before calling bind:2929+3030+fd = socket(AF_INET, SOCK_STREAM, 0);3131+/* - 8< -*/3232+int value = 1;3333+setsockopt(fd, SOL_IP, IP_TRANSPARENT, &value, sizeof(value));3434+/* - 8< -*/3535+name.sin_family = AF_INET;3636+name.sin_port = htons(0xCAFE);3737+name.sin_addr.s_addr = htonl(0xDEADBEEF);3838+bind(fd, &name, sizeof(name));3939+4040+A trivial patch for netcat is available here:4141+http://people.netfilter.org/hidden/tproxy/netcat-ip_transparent-support.patch4242+4343+4444+2. Redirecting traffic4545+======================4646+4747+Transparent proxying often involves "intercepting" traffic on a router. This is4848+usually done with the iptables REDIRECT target; however, there are serious4949+limitations of that method. One of the major issues is that it actually5050+modifies the packets to change the destination address -- which might not be5151+acceptable in certain situations. (Think of proxying UDP for example: you won't5252+be able to find out the original destination address. Even in case of TCP5353+getting the original destination address is racy.)5454+5555+The 'TPROXY' target provides similar functionality without relying on NAT. Simply5656+add rules like this to the iptables ruleset above:5757+5858+# iptables -t mangle -A PREROUTING -p tcp --dport 80 -j TPROXY \5959+ --tproxy-mark 0x1/0x1 --on-port 500806060+6161+Note that for this to work you'll have to modify the proxy to enable (SOL_IP,6262+IP_TRANSPARENT) for the listening socket.6363+6464+6565+3. Iptables extensions6666+======================6767+6868+To use tproxy you'll need to have the 'socket' and 'TPROXY' modules6969+compiled for iptables. A patched version of iptables is available7070+here: http://git.balabit.hu/?p=bazsi/iptables-tproxy.git7171+7272+7373+4. Application support7474+======================7575+7676+4.1. Squid7777+----------7878+7979+Squid 3.HEAD has support built-in. To use it, pass8080+'--enable-linux-netfilter' to configure and set the 'tproxy' option on8181+the HTTP listener you redirect traffic to with the TPROXY iptables8282+target.8383+8484+For more information please consult the following page on the Squid8585+wiki: http://wiki.squid-cache.org/Features/Tproxy4