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