1Transparent proxy support 2========================= 3 4This feature adds Linux 2.2-like transparent proxy support to current kernels. 5To use it, enable the socket match and the TPROXY target in your kernel config. 6You will need policy routing too, so be sure to enable that as well. 7 8 91. Making non-local sockets work 10================================ 11 12The idea is that you identify packets with destination address matching a local 13socket on your box, set the packet mark to a certain value, and then match on that 14value using policy routing to have those packets delivered locally: 15 16# iptables -t mangle -N DIVERT 17# iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT 18# iptables -t mangle -A DIVERT -j MARK --set-mark 1 19# iptables -t mangle -A DIVERT -j ACCEPT 20 21# ip rule add fwmark 1 lookup 100 22# ip route add local 0.0.0.0/0 dev lo table 100 23 24Because of certain restrictions in the IPv4 routing output code you'll have to 25modify your application to allow it to send datagrams _from_ non-local IP 26addresses. All you have to do is enable the (SOL_IP, IP_TRANSPARENT) socket 27option before calling bind: 28 29fd = socket(AF_INET, SOCK_STREAM, 0); 30/* - 8< -*/ 31int value = 1; 32setsockopt(fd, SOL_IP, IP_TRANSPARENT, &value, sizeof(value)); 33/* - 8< -*/ 34name.sin_family = AF_INET; 35name.sin_port = htons(0xCAFE); 36name.sin_addr.s_addr = htonl(0xDEADBEEF); 37bind(fd, &name, sizeof(name)); 38 39A trivial patch for netcat is available here: 40http://people.netfilter.org/hidden/tproxy/netcat-ip_transparent-support.patch 41 42 432. Redirecting traffic 44====================== 45 46Transparent proxying often involves "intercepting" traffic on a router. This is 47usually done with the iptables REDIRECT target; however, there are serious 48limitations of that method. One of the major issues is that it actually 49modifies the packets to change the destination address -- which might not be 50acceptable in certain situations. (Think of proxying UDP for example: you won't 51be able to find out the original destination address. Even in case of TCP 52getting the original destination address is racy.) 53 54The 'TPROXY' target provides similar functionality without relying on NAT. Simply 55add rules like this to the iptables ruleset above: 56 57# iptables -t mangle -A PREROUTING -p tcp --dport 80 -j TPROXY \ 58 --tproxy-mark 0x1/0x1 --on-port 50080 59 60Note that for this to work you'll have to modify the proxy to enable (SOL_IP, 61IP_TRANSPARENT) for the listening socket. 62 63 643. Iptables extensions 65====================== 66 67To use tproxy you'll need to have the 'socket' and 'TPROXY' modules 68compiled for iptables. A patched version of iptables is available 69here: http://git.balabit.hu/?p=bazsi/iptables-tproxy.git 70 71 724. Application support 73====================== 74 754.1. Squid 76---------- 77 78Squid 3.HEAD has support built-in. To use it, pass 79'--enable-linux-netfilter' to configure and set the 'tproxy' option on 80the HTTP listener you redirect traffic to with the TPROXY iptables 81target. 82 83For more information please consult the following page on the Squid 84wiki: http://wiki.squid-cache.org/Features/Tproxy4 85