Linux Netfilter discussions
 help / color / mirror / Atom feed
From: "Nirgal Vourgère" <contact_vgernf@nirgal.com>
To: Florian Westphal <fw@strlen.de>
Cc: netfilter@vger.kernel.org
Subject: Re: Issue migrating "iptables -m socket --transparent" into nftables
Date: Tue, 18 Aug 2020 01:25:45 +0200	[thread overview]
Message-ID: <3052032.WrxeKnI8BP@deimos> (raw)
In-Reply-To: <20200817193406.GE15804@breakpoint.cc>

[-- Attachment #1: Type: text/plain, Size: 2765 bytes --]

On Monday, 17 August 2020 21:34:06 CEST Florian Westphal wrote:
> Nirgal Vourgère <contact_vgernf@nirgal.com> wrote:
> > 
> >     ip rule add fwmark 1 lookup haproxy
> >     ip route add local default dev lo table haproxy
> > 
> > My firewall rules have
> > 
> >     iptables -t mangle -A PREROUTING -m socket --transparent -j MARK --set-mark 1
> 
> [..]
> 
> > I tried this nft rule:
> > 
> >     table inet haproxy {
> >         chain prerouting {
> >             type filter hook prerouting priority -150; policy accept;
> >             socket transparent 1 mark set 0x00000001
> >         }
> >     }
> > 
> > It does work, but all traffic is routed to the haproxy socket, including outbound masqueraded connection… I mean when a box on the lan side connects to a foreign https server, the connection is grabbed by haproxy, which is not what I want.
> 
> I don't understand how the iptables rule would not do exactly the same
> thing, there is nothing that checks interface names or addresses.
> 
> Are you sure there is nothing in the iptables rule set that
> makes the socket rule only handle those packets that should be redirected?

I am sure. Yes.

Here's the output of my iptables generated "nft list ruleset", only the fragment regarding the mangle tables generated by iptables and ip6tables:

table ip mangle {
	chain PREROUTING {
		type filter hook prerouting priority -150; policy accept;
		# socket --transparent counter packets 83537 bytes 53363874 meta mark set 0x1 
	}

	chain INPUT {
		type filter hook input priority -150; policy accept;
	}

	chain FORWARD {
		type filter hook forward priority -150; policy accept;
	}

	chain OUTPUT {
		type route hook output priority -150; policy accept;
	}

	chain POSTROUTING {
		type filter hook postrouting priority -150; policy accept;
	}
}
table ip6 mangle {
	chain PREROUTING {
		type filter hook prerouting priority -150; policy accept;
		# socket --transparent counter packets 3 bytes 180 meta mark set 0x1 
	}

	chain INPUT {
		type filter hook input priority -150; policy accept;
	}

	chain FORWARD {
		type filter hook forward priority -150; policy accept;
	}

	chain OUTPUT {
		type route hook output priority -150; policy accept;
	}

	chain POSTROUTING {
		type filter hook postrouting priority -150; policy accept;
	}
}

This works.

No protocol check, no ip check, no port, just a simple brutal "iptables -t mangle -A PREROUTING -m socket --transparent -j MARK --set-mark 1"
Attached are the whole firewall mangle fragments, one works. The other does not.

Maybe there's some magic in the old transparent module, that silently add some conditions?

I've been using that set up on a whole bunch of servers.

[-- Attachment #2: fw_ok.sh --]
[-- Type: text/plain, Size: 1621 bytes --]

#!/bin/bash
ENABLE_HAPROXY=1
HA_RT_TABLE="haproxy"
RT_TABLES=/etc/iproute2/rt_tables
HAPROXY_IPMARK=1 # Id for packets to go to haproxy

ip46tables() {
    # simple function that run rule on both IPv4 and IPv6
    iptables "$@"
    ip6tables "$@"
}

###############################################################################
# Marking packets for haproxy
###############################################################################

ip46tables -t mangle --flush PREROUTING
if [ -n "$ENABLE_HAPROXY" ]
then
        sysctl -q -w net.ipv4.ip_nonlocal_bind=1
        ip46tables -t mangle -A PREROUTING -m socket --transparent -j MARK --set-mark $HAPROXY_IPMARK

        if grep -q $HA_RT_TABLE $RT_TABLES
        then
                for ipversion in 4 6
                do
                        # all packets marked by HAPROXY_IPMARK should be routed using $HA_RT_TABLE
                        ip -$ipversion rule del fwmark $HAPROXY_IPMARK
                        ip -$ipversion rule add fwmark $HAPROXY_IPMARK lookup $HA_RT_TABLE

                        # default for routing table $HA_RT_TABLE is to try local bind
                        # Note that net.ipv4.ip_nonlocal_bind=1
                        ip -$ipversion route flush table $HA_RT_TABLE
                        ip -$ipversion route add local default dev lo table $HA_RT_TABLE
                done
        else
                $LOGGER -p user.crit -- "$RT_TABLES does not have $HA_RT_TABLE entry. Consider running \"echo 100 $HA_RT_TABLE >> $RT_TABLES\". haproxy rules disabled."
        fi
else
        sysctl -q -w net.ipv4.ip_nonlocal_bind=0
fi


[-- Attachment #3: fw_nok.sh --]
[-- Type: text/plain, Size: 1600 bytes --]

#!/bin/bash
ENABLE_HAPROXY=1
HA_RT_TABLE="haproxy"
RT_TABLES=/etc/iproute2/rt_tables
HAPROXY_IPMARK=1 # Id for packets to go to haproxy

###############################################################################
# Marking packets for haproxy
###############################################################################

if [ -n "$ENABLE_HAPROXY" ]
then
        sysctl -q -w net.ipv4.ip_nonlocal_bind=1

        nft create table inet haproxy
        nft -- add chain inet haproxy prerouting \{ type filter hook prerouting priority -150\; \}
        nft add rule inet haproxy prerouting socket transparent 1 meta mark set $HAPROXY_IPMARK
        if grep -q $HA_RT_TABLE $RT_TABLES
        then
                for ipversion in 4 6
                do
                        # all packets marked by HAPROXY_IPMARK should be routed using $HA_RT_TABLE
                        ip -$ipversion rule del fwmark $HAPROXY_IPMARK
                        ip -$ipversion rule add fwmark $HAPROXY_IPMARK lookup $HA_RT_TABLE

                        # default for routing table $HA_RT_TABLE is to try local bind
                        # Note that net.ipv4.ip_nonlocal_bind=1
                        ip -$ipversion route flush table $HA_RT_TABLE
                        ip -$ipversion route add local default dev lo table $HA_RT_TABLE
                done
        else
                $LOGGER -p user.crit -- "$RT_TABLES does not have $HA_RT_TABLE entry. Consider running \"echo 100 $HA_RT_TABLE >> $RT_TABLES\". haproxy rules disabled."
        fi
else
        sysctl -q -w net.ipv4.ip_nonlocal_bind=0
fi



  reply	other threads:[~2020-08-17 23:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-17 14:54 Issue migrating "iptables -m socket --transparent" into nftables Nirgal Vourgère
2020-08-17 19:34 ` Florian Westphal
2020-08-17 23:25   ` Nirgal Vourgère [this message]
2020-08-18 10:17     ` Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3052032.WrxeKnI8BP@deimos \
    --to=contact_vgernf@nirgal.com \
    --cc=fw@strlen.de \
    --cc=netfilter@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox