tproxy.txt 3.0 KB

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