tproxy.rst 3.8 KB

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