Configure HAProxy with TPROXY kernel for full transparent proxy


by Malcolm Turnbull

Standard Kernel builds don’t support TPROXY ( 2.6.28 does now!).
For example if you use HaProxy as the load balancer then all of the backend servers see the traffic coming from the IP address of the load balancer. TPROXY allows you to make sure the backend servers see the true client IP address in the logs.

Ps. An easier alternative is inserting the clients ip in the x-forwarded-for header (option forwardfor).

For TPROXY to work you need three things:

1) TPROXY compiled into the linux kernel
2) TPROXY / Socket compiled into netfilter / iptables (due in v1.4.3?)
3) HaProxy compiled with the USE_LINUX_TPROXY option

The TPROXY patch for Linux Kernel 2.6.25.11 is here:
http://www.balabit.com/downloads/files/tproxy/tproxy-kernel-2.6.25-20080519-165031-1211208631.tar.bz2

The following is a guide how to install on Centos 5.1:

Heavily borrowed from: http://howtoforge.com/kernel_compilation_centos

Download The Kernel Sources

First we download our desired kernel to /usr/src. Go to www.kernel.org and select the kernel you want to install, e.g. linux-2.6.25.11.tar.bz2 (you can find all 2.6 kernels here: http://www.kernel.org/pub/linux/kernel/v2.6/). Then you can download it to /usr/src like this:

cd /usr/src
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.25.11.tar.bz2

Then we unpack the kernel sources and create a symlink linux to the kernel sources directory:

tar xjf linux-2.6.25.11.tar.bz2
ln -s linux-2.6.25.11 linux
cd /usr/src/

wget http://www.balabit.com/downloads/files/tproxy/tproxy-kernel-2.6.25-20080519-165031-1211208631.tar.bz2

tar -xjf tproxy-kernel-2.6.25-20080519-165031-1211208631.tar.bz2

cd linux

cat ../tproxy-kernel-2.6.25-20080519-165031-1211208631/00*.patch | patch -p1 --dry-run
cat ../tproxy-kernel-2.6.25-20080519-165031-1211208631/00*.patch | patch -p1

Configure The Kernel

It’s a good idea to use the configuration of your current working kernel as a basis for your new kernel. Therefore we copy the existing configuration to /usr/src/linux:

make clean && make mrproper
cp /boot/config-`uname -r` ./.config

I needed to do a:
yum install ncurses-devel gcc gcc-c++ make rpm-build

Then we run

make menuconfig

which brings up the kernel configuration menu. Go to Load an Alternate Configuration File and choose .config (which contains the configuration of your current working kernel) as the configuration file:

Then browse through the kernel configuration menu and make your choices.

Make sure you enable tproxy support, `socket' and `TPROXY' modules (with optional conntrack support if you need SNAT)

Make sure you specify a kernel version identification string under General Setup —> () Local version - append to kernel release. I use CS3 so our kernel rpm package will be named kernel-2.6.25.11CS3.x86_64.rpm. You can leave the string empty or specify a different one which helps you identify the kernel (e.g. -custom or whatever you like).

Please note: After you have installed kernel-2.6.25.11CS3.x86_64.rpm and decide to compile another 2.6.25 kernel rpm package, it is important to use a different version string, e.g. -default1, -default2, etc., because otherwise you can’t install your new kernel because rpm complains that kernel-2.6.25.11CS3.x86_64.rpm is already installed!

Once you are happy with the kernel configuration, save & exit menuconfig then simply:

make rpm

This may take quite a long time….
Once it has finished:

Source RPM is here:
ls -l /usr/src/redhat/SRPMS/
Binary RPM is here:
ls -l /usr/src/redhat/RPMS/x86_64/

now install the new kernel:
cd /usr/src/redhat/RPMS/x86_64/
rpm -ivh --nodeps kernel-2.6.25CS-1.x86_64.rpm

Now you can either run the following command:
/sbin/new-kernel-pkg –package kernel –mkinitrd –depmod –install 2.6.25CS

Or you can do the usual manual steps i.e.

Make sure you create a new initrd file:
mkinitrd /boot/initrd-2.6.25.11CS3.img 2.6.25.11CS3

Now configure the boot loader:
vi /boot/grub/menu.lst

default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.25.11CS3)
root (hd0,0)
kernel /boot/vmlinuz-2.6.25.11CS3 ro root=LABEL=/
initrd /boot/initrd-2.6.25.11CS3.img

That’s it, so reboot and do a:
uname -a

To check that we are using the new kernel:
Linux lbmaster 2.6.25.11CS3 #6 SMP Mon Jul 28 13:10:43 GMT 2008 x86_64 x86_64 x86_64 GNU/Linux

Compiling iptables with TPROXY support:

First download the current iptables source code:

cd /usr/src/
wget http://www.netfilter.org/projects/iptables/files/iptables-1.4.0.tar.bz2
tar -xjf iptables-1.4.0.tar.bz2

Then download the tproxy patch:

wget http://www.balabit.com/downloads/files/tproxy/tproxy-iptables-1.4.0-20080521-113954-1211362794.patch

cd /usr/src/iptables-1.4.0/
cat ../tproxy-iptables*.patch | patch -p1
make
make install

Compiling HAProxy with TPROXY support:

Download the latest version of the HAProxy source code:

wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.15.7.tar.gz
tar -xvf haproxy-1.3.15.7.tar.gz
cd haproxy-1.3.15.7/

Then compile making sure to enable TPROXY
make TARGET=linux26 CPU=x86_64 USE_STATIC_PCRE=1 USE_LINUX_TPROXY=1
make install target=linux26

If you have got this far then great, thats the hard part done!

Now before Haproxy can utilise TPROXY we need to set up some firewall marks:
You can put this script in a start up file such as rc.local etc.

#!/bin/bash
iptables -t mangle -N DIVERT
iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT
iptables -t mangle -A DIVERT -j MARK --set-mark 111
iptables -t mangle -A DIVERT -j ACCEPT
ip rule add fwmark 111 lookup 100
ip route add local 0.0.0.0/0 dev lo table 100

We also need to ensure that we have the correct architecture for the TPROXY trick to work. Using the normal HAProxy you can have real servers anywhere on the internet because the source address always points back at the HAProxy units IP address. However if the clients source IP address is going to be used then the HAProxy server MUST BE IN THE PATH of the return traffic.
The easiest way to do this is to put the backend servers in a different subnet to the front end clients and make sure that the default gateway points back at the HAProxy load balancer.

NB. With clever routing this should be possible on the same subnet but I haven’t tried that yet!

So here is an example configuration that I used for HAProxy:

# HAProxy configuration file
global
#	uid 99
#	gid 99
	daemon
	stats socket /var/run/haproxy.stat mode 600
	log 127.0.0.1 local4
	maxconn 40000
	ulimit-n 80013
	pidfile /var/run/haproxy.pid
defaults
	log global
	mode	http
	contimeout	4000
	clitimeout	42000
	srvtimeout	43000
	balance	roundrobin
listen	VIP_Name 192.168.2.87:80
	mode	http
	option	forwardfor
	source 0.0.0.0 usesrc clientip
	cookie	SERVERID insert nocache indirect
	server server1 10.0.0.60:80 weight 1 cookie server1 check
	server server2 10.0.0.61:80 weight 1 cookie server2 check
	server	backup 127.0.0.1:80 backup
	option redispatch

The most important line is this one:

	source 0.0.0.0 usesrc clientip

If your test setup doesn’t work then remove this line to check if a standard configuration does work.

Check your backend server logs to ensure that the client source IP address is correctly showing.

NB. One gotcha (of the many) is that you can no long use any local (i.e. 127.0.0.1) backup servers due to routing issues.
To resolve this change the backup server definition as follows:
server    backup 127.0.0.1:80 backup source 0.0.0.0


Ps. Many thanks to
John Lauro for his help with the firewall marks stuff

Oh and forgot to say change your sysctrls to allow redirects.. i.e.

echo 1 > /proc/sys/net/ipv4/conf/all/forwarding
echo 1 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 1 > /proc/sys/net/ipv4/conf/eth0/send_redirects

25 Responses to “Configure HAProxy with TPROXY kernel for full transparent proxy”

  1. Carlo Says:

    Hello, when I try to compile iptables, after “make”, I run “make install”: I have an error when it build dependencies:

    Unable to resolve dependency on asm/swab.h. Try ‘make clean’

    Have you got ideas?

  2. Malcolm Turnbull Says:

    Strange, are you sure you left your kernel headers in place before compiling iptables?
    make mrproper will destroy them, make clean should be safe enough.

  3. Carlo Says:

    I’ve solved my problem by compiling iptables 1.4.3 that have tproxy patch inside (problem was introduced by balabit patch).

    But now I’ve another problem: line “source 0.0.0.0 usesrc clientip” doesn’t work: I obtain “503 Service Unavailable”.
    Please note that haproxy bind on a public IP and servers (two) are in a private network (haproxy’s server have two eth with public and private ip).

    Whitout “source 0.0.0.0 usesrc clientip” it work.

    Have you got ideas?

  4. Malcolm Turnbull Says:

    Carlo,

    Yes, patches only work against the exact version….
    Are you sure you have set the default gateway on the backend servers to point at the internal interface on the haproxy instance. Traffic must pass through the proxy both ways.
    Does your setup work without that line?

  5. Carlo Says:

    Firstly I’ve followeb your howto but iptables patch fails. So I have compiled iptables 1.4.3

    Haproxy configuration is the same that you have posted here.
    Can you provide me an example please?

    Without “source” line my setup work.

    Thanks.

  6. Carlo Says:

    Solved. :)
    There was a problem in haproxy.cfg iptables:

    for haproxy use “source HAPROXY_IP usesrc clientip” (HAPROXY_IP can be public o private ip)

    for iptables:

    /usr/local/sbin/iptables -t mangle -N DIVERT
    /usr/local/sbin/iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT
    /usr/local/sbin/iptables -t mangle -A DIVERT -j MARK –set-mark 1
    /usr/local/sbin/iptables -t mangle -A DIVERT -j ACCEPT

    ip rule add fwmark 1 lookup 100
    ip route add local 0.0.0.0/0 dev lo table 100

    Thanks!

  7. Joseph Says:

    Hello Malcolm,

    Can you please provide details on what specific module to enable for the kernel? I am having a problem with my iptables, please see below;

    [root@SE-Caching ~]# service iptables status
    Table: mangle
    Chain PREROUTING (policy ACCEPT)
    num target prot opt source destination
    1 DIVERT tcp — 0.0.0.0/0 0.0.0.0/0 UNKNOWN match `socket’
    2 TPROXY tcp — 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 [16 bytes of unknown target data]

    What causes this one (UNKNOWN match `socket’ )?

  8. Malcolm Turnbull Says:

    Hi Joseph,

    The modules load on demand so…

    Yes you’ve probably missed one of the required options while doing make menuconfig….
    Its really annoying that make menuconfig only shows the nice names of modules and not the precise name as in the .config file. As I forgot to take an exact copy of the text from each option at the time I can’t help much on the nice description side….

    Looking at my .config file I think the important ones are:
    CONFIG_NETFILTER_XT_TARGET_TPROXY=m
    CONFIG_NETFILTER_XT_MATCH_SOCKET=m
    CONFIG_NETFILTER_XT_MATCH_MARK=m
    CONFIG_NETFILTER_XT_TARGET_MARK=m
    CONFIG_NETFILTER_TPROXY=m
    CONFIG_NETFILTER_XTABLES=m
    CONFIG_NF_CONNTRACK=m
    CONFIG_NF_CT_ACCT=y
    CONFIG_NF_CONNTRACK_MARK=y

    IF in doubt select them ALL!

    Contact support@loadbalancer.org by email if you want the entire .config which works and may well help.
    I can even give you a full Centos5 kernel rpm if you want.

  9. Jim Says:

    Hi I have the same issue as Joseph:

    Table: mangle
    Chain PREROUTING (policy ACCEPT)
    num target prot opt source destination
    1 DIVERT tcp — 0.0.0.0/0 0.0.0.0/0 UNKNOWN match `socket’

    I recompiled the kernel a couple of times making sure I had at least all the modules you mention.

    Any pointers as to why I’m getting this error would be greatly appreciated.
    Thanks.

  10. Malcolm Turnbull Says:

    Jim,
    I think it means iptables can’t find or load the socket module, did your patch and re-compile of iptables go OK? Does the lsmod command show the relevant modules?

  11. Jim Says:

    Thanks for you response Jim, it looks like the relevant modules are there, below is the lsmod output.
    The weird thing is it’s working fine but that UNKNOWN match `socket’ still shows when I do /etc/init.d/iptables status.

    Module Size Used by
    xt_MARK 1892 1
    xt_socket 2212 1
    nf_conntrack 59436 1 xt_socket
    nf_defrag_ipv4 1764 1 xt_socket
    nf_tproxy_core 2404 1 xt_socket,[permanent]
    iptable_mangle 2372 1
    ip_tables 10452 1 iptable_mangle
    x_tables 13864 3 xt_MARK,xt_socket,ip_tables
    ipv6 214388 12
    autofs4 20648 2
    hidp 12196 2
    rfcomm 28692 0
    l2cap 18020 10 hidp,rfcomm
    bluetooth 48900 5 hidp,rfcomm,l2cap
    sunrpc 160704 1
    dm_multipath 12880 0
    sbs 10956 0
    sbshc 5124 1 sbs
    battery 10088 0
    lp 8804 0
    sg 25208 0
    floppy 46980 0
    ide_cd_mod 25996 0
    cdrom 30176 1 ide_cd_mod
    serio_raw 4712 0
    parport_pc 23044 1
    ac 4072 0
    button 6004 0
    parport 30796 2 lp,parport_pc
    pcnet32 29188 0
    mii 4740 1 pcnet32
    i2c_piix4 8564 0
    i2c_core 20824 1 i2c_piix4
    pcspkr 2276 0
    dm_snapshot 16040 0
    dm_zero 1348 0
    dm_mirror 11912 0
    dm_region_hash 9828 1 dm_mirror
    dm_log 8360 2 dm_mirror,dm_region_hash
    dm_mod 47720 11 dm_multipath,dm_snapshot,dm_zero,dm_mirror,dm_l og
    ata_piix 21384 0
    libata 147852 1 ata_piix
    mptspi 14988 2
    mptscsih 28996 1 mptspi
    mptbase 71364 2 mptspi,mptscsih
    scsi_transport_spi 18980 1 mptspi
    sd_mod 23456 3
    scsi_mod 134356 6 sg,libata,mptspi,mptscsih,scsi_transport_spi,sd_mod
    ext3 107308 2
    jbd 40280 1 ext3
    uhci_hcd 18516 0
    ohci_hcd 19444 0
    ehci_hcd 29264 0

  12. Michael Dodwell Says:

    Joseph,

    If you’ve installed iptables from source make sure your calling it, and not the package installed version.

    ie

    /usr/local/sbin/iptables and not /sbin/iptables

  13. Loadbalancer.org Blog » Blog Archive » Transparent proxy of SSL traffic using Pound to HAProxy backend patch and howto Says:

    [...] so I’ve previously blogged about how to get TPROXY and HAProxy working nicely together. But what if you want to terminate SSL traffic on the load balancer in order to use HaProxy to [...]

  14. Vadim Says:

    Hi friends! I used haproxy in my project. But I have one problem. What I can switch between two backends servers (me need used rule url_sub) if I used haproxy as frontend

    global
    uid 1004
    gid 1002
    daemon
    #stats socket /var/run/haproxy.stat mode 600
    log 127.0.0.1 local4
    maxconn 40000
    ulimit-n 80013
    pidfile /var/run/haproxy.pid
    defaults
    log global
    mode http
    contimeout 4000
    clitimeout 42000
    srvtimeout 43000
    balance roundrobin
    listen XXX.COM x.x.x.x:80
    mode http
    option forwardfor
    stats enable
    stats auth bvv2001:la-la-la
    cookie XXX.COM insert
    source 0.0.0.0 usesrc clientip
    server server1 x.x.x.x:10000 cookie XXX.COM check
    server server2 x.x.x.x:10001 cookie XXX.COM check
    dst_port 10000 if url_sub !sms
    dst_port 10001 if url_sub sms

    this config NOT WORKING
    Help me please!

  15. Twitter Trackbacks for Loadbalancer.org Blog » Blog Archive » Configure HAProxy with TPROXY kernel for full transparent proxy [loadbalancer.org] on Topsy.com Says:

    [...] Loadbalancer.org Blog » Blog Archive » Configure HAProxy with TPROXY kernel for full transparent p… blog.loadbalancer.org/configure-haproxy-with-tproxy-kernel-for-full-transparent-proxy – view page – cached loadbalancer logo phone number 0870 4438779 space image email sales@loadbalancer.org — From the page [...]

  16. Gael Says:

    Hi Malcolm and thanks for your brilliant article!

    I have wasted a couple of days trying to debug the kernel which was throwing SOFT LOCKUP bugs. After investigation it was down to having 2 vCPU in the VM (from VMWare) I was using. Moving back to only one vCPU did the trick. Hope this can help someone else!

    In your post, you have commented the following line:
    # uid 99
    # gid 99

    Therefore, haproxy is running as root.

    I know it produces the following error if haproxy is run with another user then root:
    “Starting haproxy: [ALERT] 259/184020 (3497) : [/usr/sbin/haproxy.main()] Some configuration options require full privileges, so global.uid cannot be changed.”

    So it seems compulsory to run haproxy as root.
    I am now wondering if this can cause any problem related to using root to run haproxy.

    Thanks in advance for your answer!

    Gael

  17. Malcolm Turnbull Says:

    Willy suggests starting HAProxy as root because it can then jail itself in a chroot and drop all of its privileges before starting the instances. This is not possible if it is not started as root because only root can execute chroot()

  18. Malcolm Turnbull Says:

    You need something more like:

    frontend blah
    acl notsms url_sub !sms use_backend backend1
    acl sms url_sub sms use_backend backend2

    backend backend1
    source 0.0.0.0 usesrc clientip
    server server1 x.x.x.x:10000 cookie XXX.COM check

    NB. I haven’t tested this just off the top of my head…..
    Check out the HAProxy manual and mailing list for more specific help on ACLs…

  19. hanlin Says:

    Hello,I have got a probelm like this:
    haproxy bind on a public IP,the apache server is on the same machine with haproxy’server (haproxy’s server have two eth with public and private ip)
    Whitout “source 0.0.0.0 usesrc clientip” it work.
    could they at the same machine?

    (please note: the other apache server is not on the same machine wich haproxy’server,and I have set the default gateway on the backend server to point at the internal interface on the haproxy instance,it can work)

    Have you got ideas?
    thanks

  20. Malcolm Turnbull Says:

    Hanlin,

    Er… no idea.. It probably won’t work with Apache on the same host as HAProxy… But If Apache is on the same host why do you need HAProxy anyway?

  21. Anand Phulwani Says:

    Dear Malcolm,

    I know its very stupid to ask such newbie questions but that is all what i have right now.

    1) How is Haproxy different from squid.
    2) Transparent squid does not works with gtalk, is that a problem with Haproxy too, if it runs with tproxy.
    3) Does Haproxy support acl (based on time,based on url regex).
    4) Can we also use acl on HTTPS or in other words how does Haproxy handles HTTPS.

    Your reply would indeed be a lot of help to me.
    Expecting your positive reply.
    Thanks,
    Anand Phulwani

  22. Malcolm Turnbull Says:

    Anand,

    1) I guess squid is an outbound proxy (standard) and HAProxy is an inbound proxy (reverse), like Pound.
    2) Why would you be running Google talk servers? (this is a reverse proxy not outbound)
    3) Yes, but I’m not sure exactly what you are after…
    4) Not directly, but you just terminate the HTTPS with stunnel or Pound first…so yes.

  23. Anand Phulwani Says:

    Dear Malcolm,

    After Reading The Documentation I Had An Hint That This Is An Inbound Proxy And Works For Basically For Web Server Managed In A Kind Of Cluster.
    So Actually This Is Just Different For What I Am Trying To Do With Squid.

    Thanks,
    Anand

  24. Bob Smith Says:

    Here is my question. I have a simple setup with HAProxy and 2 backend servers. Works like a charm except that on these backend servers I look at the HTTP_HOST with RewriteCond to do various things. When I turn logging on it looks like the “Host” that I am getting is either the IP address or nothing. I have looked at the documentation, but any idea how I can get the backend server to “see” what the URL is?

  25. Daniel Says:

    Hello Malcolm,
    I try to implement haproxy in transparent mode like in your how-to.
    I add rules in iptables, but when I try to access to the service, the result is “503 Service Unavailable”.
    Without the line “source 0.0.0.0 usesrc clientip” all works correctly, obviously without transparency.
    Have you got any ideas? Where I’m wrong?
    Thanks! Daniel

Leave a Reply

CAPTCHA Image Audio Version
Reload Image