diff mbox series

[ovs-dev,net,2/2] act_ct: support asymmetric conntrack

Message ID 20191108210714.12426-2-aconole@redhat.com
State Awaiting Upstream
Headers show
Series [ovs-dev,net,1/2] openvswitch: support asymmetric conntrack | expand

Commit Message

Aaron Conole Nov. 8, 2019, 9:07 p.m. UTC
The act_ct TC module shares a common conntrack and NAT infrastructure
exposed via netfilter.  It's possible that a packet needs both SNAT and
DNAT manipulation, due to e.g. tuple collision.  Netfilter can support
this because it runs through the NAT table twice - once on ingress and
again after egress.  The act_ct action doesn't have such capability.

Like netfilter hook infrastructure, we should run through NAT twice to
keep the symmetry.

Fixes: b57dc7c13ea9 ("net/sched: Introduce action ct")

Signed-off-by: Aaron Conole <aconole@redhat.com>
---
 net/sched/act_ct.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

Comments

Roi Dayan Nov. 14, 2019, 2:22 p.m. UTC | #1
On 2019-11-08 11:07 PM, Aaron Conole wrote:
> The act_ct TC module shares a common conntrack and NAT infrastructure
> exposed via netfilter.  It's possible that a packet needs both SNAT and
> DNAT manipulation, due to e.g. tuple collision.  Netfilter can support
> this because it runs through the NAT table twice - once on ingress and
> again after egress.  The act_ct action doesn't have such capability.
> 
> Like netfilter hook infrastructure, we should run through NAT twice to
> keep the symmetry.
> 
> Fixes: b57dc7c13ea9 ("net/sched: Introduce action ct")
> 
> Signed-off-by: Aaron Conole <aconole@redhat.com>
> ---
>  net/sched/act_ct.c | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c
> index fcc46025e790..f3232a00970f 100644
> --- a/net/sched/act_ct.c
> +++ b/net/sched/act_ct.c
> @@ -329,6 +329,7 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>  			  bool commit)
>  {
>  #if IS_ENABLED(CONFIG_NF_NAT)
> +	int err;
>  	enum nf_nat_manip_type maniptype;
>  
>  	if (!(ct_action & TCA_CT_ACT_NAT))
> @@ -359,7 +360,17 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>  		return NF_ACCEPT;
>  	}
>  
> -	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> +	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> +	if (err == NF_ACCEPT &&
> +	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
> +		if (maniptype == NF_NAT_MANIP_SRC)
> +			maniptype = NF_NAT_MANIP_DST;
> +		else
> +			maniptype = NF_NAT_MANIP_SRC;
> +
> +		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> +	}
> +	return err;
>  #else
>  	return NF_ACCEPT;
>  #endif
> 

+paul
Paul Blakey Nov. 14, 2019, 2:24 p.m. UTC | #2
On 11/14/2019 4:22 PM, Roi Dayan wrote:
>
> On 2019-11-08 11:07 PM, Aaron Conole wrote:
>> The act_ct TC module shares a common conntrack and NAT infrastructure
>> exposed via netfilter.  It's possible that a packet needs both SNAT and
>> DNAT manipulation, due to e.g. tuple collision.  Netfilter can support
>> this because it runs through the NAT table twice - once on ingress and
>> again after egress.  The act_ct action doesn't have such capability.
>>
>> Like netfilter hook infrastructure, we should run through NAT twice to
>> keep the symmetry.
>>
>> Fixes: b57dc7c13ea9 ("net/sched: Introduce action ct")
>>
>> Signed-off-by: Aaron Conole <aconole@redhat.com>
>> ---
>>   net/sched/act_ct.c | 13 ++++++++++++-
>>   1 file changed, 12 insertions(+), 1 deletion(-)
>>
>> diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c
>> index fcc46025e790..f3232a00970f 100644
>> --- a/net/sched/act_ct.c
>> +++ b/net/sched/act_ct.c
>> @@ -329,6 +329,7 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>>   			  bool commit)
>>   {
>>   #if IS_ENABLED(CONFIG_NF_NAT)
>> +	int err;
>>   	enum nf_nat_manip_type maniptype;
>>   
>>   	if (!(ct_action & TCA_CT_ACT_NAT))
>> @@ -359,7 +360,17 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>>   		return NF_ACCEPT;
>>   	}
>>   
>> -	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>> +	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>> +	if (err == NF_ACCEPT &&
>> +	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
>> +		if (maniptype == NF_NAT_MANIP_SRC)
>> +			maniptype = NF_NAT_MANIP_DST;
>> +		else
>> +			maniptype = NF_NAT_MANIP_SRC;
>> +
>> +		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>> +	}
>> +	return err;
>>   #else
>>   	return NF_ACCEPT;
>>   #endif
>>
> +paul

Hi Aaron,

I think I understand the issue and this looks good,

Can you describe the scenario to reproduce this?


Thanks,

Paul.
Marcelo Ricardo Leitner Nov. 14, 2019, 4:29 p.m. UTC | #3
On Fri, Nov 08, 2019 at 04:07:14PM -0500, Aaron Conole wrote:
> The act_ct TC module shares a common conntrack and NAT infrastructure
> exposed via netfilter.  It's possible that a packet needs both SNAT and
> DNAT manipulation, due to e.g. tuple collision.  Netfilter can support
> this because it runs through the NAT table twice - once on ingress and
> again after egress.  The act_ct action doesn't have such capability.
> 
> Like netfilter hook infrastructure, we should run through NAT twice to
> keep the symmetry.
> 
> Fixes: b57dc7c13ea9 ("net/sched: Introduce action ct")
> 
> Signed-off-by: Aaron Conole <aconole@redhat.com>
> ---
>  net/sched/act_ct.c | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c
> index fcc46025e790..f3232a00970f 100644
> --- a/net/sched/act_ct.c
> +++ b/net/sched/act_ct.c
> @@ -329,6 +329,7 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>  			  bool commit)
>  {
>  #if IS_ENABLED(CONFIG_NF_NAT)
> +	int err;
>  	enum nf_nat_manip_type maniptype;
>  
>  	if (!(ct_action & TCA_CT_ACT_NAT))
> @@ -359,7 +360,17 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>  		return NF_ACCEPT;
>  	}
>  
> -	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> +	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> +	if (err == NF_ACCEPT &&
> +	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
> +		if (maniptype == NF_NAT_MANIP_SRC)
> +			maniptype = NF_NAT_MANIP_DST;
> +		else
> +			maniptype = NF_NAT_MANIP_SRC;
> +
> +		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> +	}

I keep thinking about this and I'm not entirely convinced that this
shouldn't be simpler. More like:

if (DNAT)
	DNAT
if (SNAT)
	SNAT

So it always does DNAT before SNAT, similarly to what iptables would
do on PRE/POSTROUTING chains.

> +	return err;
>  #else
>  	return NF_ACCEPT;
>  #endif
> -- 
> 2.21.0
>
Aaron Conole Nov. 18, 2019, 9:21 p.m. UTC | #4
Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> writes:

> On Fri, Nov 08, 2019 at 04:07:14PM -0500, Aaron Conole wrote:
>> The act_ct TC module shares a common conntrack and NAT infrastructure
>> exposed via netfilter.  It's possible that a packet needs both SNAT and
>> DNAT manipulation, due to e.g. tuple collision.  Netfilter can support
>> this because it runs through the NAT table twice - once on ingress and
>> again after egress.  The act_ct action doesn't have such capability.
>> 
>> Like netfilter hook infrastructure, we should run through NAT twice to
>> keep the symmetry.
>> 
>> Fixes: b57dc7c13ea9 ("net/sched: Introduce action ct")
>> 
>> Signed-off-by: Aaron Conole <aconole@redhat.com>
>> ---
>>  net/sched/act_ct.c | 13 ++++++++++++-
>>  1 file changed, 12 insertions(+), 1 deletion(-)
>> 
>> diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c
>> index fcc46025e790..f3232a00970f 100644
>> --- a/net/sched/act_ct.c
>> +++ b/net/sched/act_ct.c
>> @@ -329,6 +329,7 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>>  			  bool commit)
>>  {
>>  #if IS_ENABLED(CONFIG_NF_NAT)
>> +	int err;
>>  	enum nf_nat_manip_type maniptype;
>>  
>>  	if (!(ct_action & TCA_CT_ACT_NAT))
>> @@ -359,7 +360,17 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>>  		return NF_ACCEPT;
>>  	}
>>  
>> -	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>> +	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>> +	if (err == NF_ACCEPT &&
>> +	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
>> +		if (maniptype == NF_NAT_MANIP_SRC)
>> +			maniptype = NF_NAT_MANIP_DST;
>> +		else
>> +			maniptype = NF_NAT_MANIP_SRC;
>> +
>> +		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>> +	}
>
> I keep thinking about this and I'm not entirely convinced that this
> shouldn't be simpler. More like:
>
> if (DNAT)
> 	DNAT
> if (SNAT)
> 	SNAT
>
> So it always does DNAT before SNAT, similarly to what iptables would
> do on PRE/POSTROUTING chains.

I can rewrite the whole function, but I wanted to start with the smaller
fix that worked.  I also think it needs more testing then (since it's
something of a rewrite of the function).

I guess it's not too important - do you think it gives any readability
to do it this way?  If so, I can respin the patch changing it like you
describe.

>> +	return err;
>>  #else
>>  	return NF_ACCEPT;
>>  #endif
>> -- 
>> 2.21.0
>>
Aaron Conole Nov. 18, 2019, 9:24 p.m. UTC | #5
Paul Blakey <paulb@mellanox.com> writes:

> On 11/14/2019 4:22 PM, Roi Dayan wrote:
>>
>> On 2019-11-08 11:07 PM, Aaron Conole wrote:
>>> The act_ct TC module shares a common conntrack and NAT infrastructure
>>> exposed via netfilter.  It's possible that a packet needs both SNAT and
>>> DNAT manipulation, due to e.g. tuple collision.  Netfilter can support
>>> this because it runs through the NAT table twice - once on ingress and
>>> again after egress.  The act_ct action doesn't have such capability.
>>>
>>> Like netfilter hook infrastructure, we should run through NAT twice to
>>> keep the symmetry.
>>>
>>> Fixes: b57dc7c13ea9 ("net/sched: Introduce action ct")
>>>
>>> Signed-off-by: Aaron Conole <aconole@redhat.com>
>>> ---
>>>   net/sched/act_ct.c | 13 ++++++++++++-
>>>   1 file changed, 12 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c
>>> index fcc46025e790..f3232a00970f 100644
>>> --- a/net/sched/act_ct.c
>>> +++ b/net/sched/act_ct.c
>>> @@ -329,6 +329,7 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>>>   			  bool commit)
>>>   {
>>>   #if IS_ENABLED(CONFIG_NF_NAT)
>>> +	int err;
>>>   	enum nf_nat_manip_type maniptype;
>>>   
>>>   	if (!(ct_action & TCA_CT_ACT_NAT))
>>> @@ -359,7 +360,17 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>>>   		return NF_ACCEPT;
>>>   	}
>>>   
>>> -	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>>> +	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>>> +	if (err == NF_ACCEPT &&
>>> +	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
>>> +		if (maniptype == NF_NAT_MANIP_SRC)
>>> +			maniptype = NF_NAT_MANIP_DST;
>>> +		else
>>> +			maniptype = NF_NAT_MANIP_SRC;
>>> +
>>> +		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>>> +	}
>>> +	return err;
>>>   #else
>>>   	return NF_ACCEPT;
>>>   #endif
>>>
>> +paul
>
> Hi Aaron,
>
> I think I understand the issue and this looks good,
>
> Can you describe the scenario to reproduce this?

It reproduces with OpenShift 3.10, which makes forward direction packets
between namespaces pump through a tun device that applies NAT rules to
rewrite the dest.  Limit the namespace number of ephemeral sockets using
by editing net.ipv4.ip_local_port_range in the client namespace, and
connect to the server namespace.  That's the mechanism for OvS.  But for
TC I guess there wouldn't be anything convenient avaiable.

I'll try to script up something that doesn't use openshift.

>
> Thanks,
>
> Paul.
Marcelo Ricardo Leitner Nov. 18, 2019, 10:40 p.m. UTC | #6
On Mon, Nov 18, 2019 at 04:21:39PM -0500, Aaron Conole wrote:
> Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> writes:
> 
> > On Fri, Nov 08, 2019 at 04:07:14PM -0500, Aaron Conole wrote:
> >> The act_ct TC module shares a common conntrack and NAT infrastructure
> >> exposed via netfilter.  It's possible that a packet needs both SNAT and
> >> DNAT manipulation, due to e.g. tuple collision.  Netfilter can support
> >> this because it runs through the NAT table twice - once on ingress and
> >> again after egress.  The act_ct action doesn't have such capability.
> >> 
> >> Like netfilter hook infrastructure, we should run through NAT twice to
> >> keep the symmetry.
> >> 
> >> Fixes: b57dc7c13ea9 ("net/sched: Introduce action ct")
> >> 
> >> Signed-off-by: Aaron Conole <aconole@redhat.com>
> >> ---
> >>  net/sched/act_ct.c | 13 ++++++++++++-
> >>  1 file changed, 12 insertions(+), 1 deletion(-)
> >> 
> >> diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c
> >> index fcc46025e790..f3232a00970f 100644
> >> --- a/net/sched/act_ct.c
> >> +++ b/net/sched/act_ct.c
> >> @@ -329,6 +329,7 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
> >>  			  bool commit)
> >>  {
> >>  #if IS_ENABLED(CONFIG_NF_NAT)
> >> +	int err;
> >>  	enum nf_nat_manip_type maniptype;
> >>  
> >>  	if (!(ct_action & TCA_CT_ACT_NAT))
> >> @@ -359,7 +360,17 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
> >>  		return NF_ACCEPT;
> >>  	}
> >>  
> >> -	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> >> +	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> >> +	if (err == NF_ACCEPT &&
> >> +	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
> >> +		if (maniptype == NF_NAT_MANIP_SRC)
> >> +			maniptype = NF_NAT_MANIP_DST;
> >> +		else
> >> +			maniptype = NF_NAT_MANIP_SRC;
> >> +
> >> +		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> >> +	}
> >
> > I keep thinking about this and I'm not entirely convinced that this
> > shouldn't be simpler. More like:
> >
> > if (DNAT)
> > 	DNAT
> > if (SNAT)
> > 	SNAT
> >
> > So it always does DNAT before SNAT, similarly to what iptables would
> > do on PRE/POSTROUTING chains.
> 
> I can rewrite the whole function, but I wanted to start with the smaller
> fix that worked.  I also think it needs more testing then (since it's
> something of a rewrite of the function).
> 
> I guess it's not too important - do you think it gives any readability
> to do it this way?  If so, I can respin the patch changing it like you
> describe.

I didn't mean a rewrite, but just to never handle SNAT before DNAT. So
the fix here would be like:

-	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
+	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
+	if (err == NF_ACCEPT && maniptype == NF_NAT_MANIP_DST &&
+	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
+		maniptype = NF_NAT_MANIP_SRC;
+		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
+	}
+	return err;

> >> +	return err;
> >>  #else
> >>  	return NF_ACCEPT;
> >>  #endif
> >> -- 
> >> 2.21.0
> >> 
>
Aaron Conole Nov. 22, 2019, 8:39 p.m. UTC | #7
Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> writes:

> On Mon, Nov 18, 2019 at 04:21:39PM -0500, Aaron Conole wrote:
>> Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> writes:
>> 
>> > On Fri, Nov 08, 2019 at 04:07:14PM -0500, Aaron Conole wrote:
>> >> The act_ct TC module shares a common conntrack and NAT infrastructure
>> >> exposed via netfilter.  It's possible that a packet needs both SNAT and
>> >> DNAT manipulation, due to e.g. tuple collision.  Netfilter can support
>> >> this because it runs through the NAT table twice - once on ingress and
>> >> again after egress.  The act_ct action doesn't have such capability.
>> >> 
>> >> Like netfilter hook infrastructure, we should run through NAT twice to
>> >> keep the symmetry.
>> >> 
>> >> Fixes: b57dc7c13ea9 ("net/sched: Introduce action ct")
>> >> 
>> >> Signed-off-by: Aaron Conole <aconole@redhat.com>
>> >> ---
>> >>  net/sched/act_ct.c | 13 ++++++++++++-
>> >>  1 file changed, 12 insertions(+), 1 deletion(-)
>> >> 
>> >> diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c
>> >> index fcc46025e790..f3232a00970f 100644
>> >> --- a/net/sched/act_ct.c
>> >> +++ b/net/sched/act_ct.c
>> >> @@ -329,6 +329,7 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>> >>  			  bool commit)
>> >>  {
>> >>  #if IS_ENABLED(CONFIG_NF_NAT)
>> >> +	int err;
>> >>  	enum nf_nat_manip_type maniptype;
>> >>  
>> >>  	if (!(ct_action & TCA_CT_ACT_NAT))
>> >> @@ -359,7 +360,17 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
>> >>  		return NF_ACCEPT;
>> >>  	}
>> >>  
>> >> -	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>> >> +	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>> >> +	if (err == NF_ACCEPT &&
>> >> +	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
>> >> +		if (maniptype == NF_NAT_MANIP_SRC)
>> >> +			maniptype = NF_NAT_MANIP_DST;
>> >> +		else
>> >> +			maniptype = NF_NAT_MANIP_SRC;
>> >> +
>> >> +		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
>> >> +	}
>> >
>> > I keep thinking about this and I'm not entirely convinced that this
>> > shouldn't be simpler. More like:
>> >
>> > if (DNAT)
>> > 	DNAT
>> > if (SNAT)
>> > 	SNAT
>> >
>> > So it always does DNAT before SNAT, similarly to what iptables would
>> > do on PRE/POSTROUTING chains.
>> 
>> I can rewrite the whole function, but I wanted to start with the smaller
>> fix that worked.  I also think it needs more testing then (since it's
>> something of a rewrite of the function).
>> 
>> I guess it's not too important - do you think it gives any readability
>> to do it this way?  If so, I can respin the patch changing it like you
>> describe.
>
> I didn't mean a rewrite, but just to never handle SNAT before DNAT. So
> the fix here would be like:
>
> -	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> +	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> +	if (err == NF_ACCEPT && maniptype == NF_NAT_MANIP_DST &&
> +	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
> +		maniptype = NF_NAT_MANIP_SRC;
> +		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> +	}
> +	return err;

But the maniptype of the first call could be NAT_MANIP_SRC.  In fact,
that's what I see if the packet is reply direction && !related.

So, we need the block to invert the manipulation type.  Otherwise, we
miss the DNAT manipulation.

So I don't think I can use that block.

>> >> +	return err;
>> >>  #else
>> >>  	return NF_ACCEPT;
>> >>  #endif
>> >> -- 
>> >> 2.21.0
>> >> 
>>
Marcelo Ricardo Leitner Nov. 22, 2019, 8:43 p.m. UTC | #8
On Fri, Nov 22, 2019 at 03:39:16PM -0500, Aaron Conole wrote:
> Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> writes:
> 
> > On Mon, Nov 18, 2019 at 04:21:39PM -0500, Aaron Conole wrote:
> >> Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> writes:
> >> 
> >> > On Fri, Nov 08, 2019 at 04:07:14PM -0500, Aaron Conole wrote:
> >> >> The act_ct TC module shares a common conntrack and NAT infrastructure
> >> >> exposed via netfilter.  It's possible that a packet needs both SNAT and
> >> >> DNAT manipulation, due to e.g. tuple collision.  Netfilter can support
> >> >> this because it runs through the NAT table twice - once on ingress and
> >> >> again after egress.  The act_ct action doesn't have such capability.
> >> >> 
> >> >> Like netfilter hook infrastructure, we should run through NAT twice to
> >> >> keep the symmetry.
> >> >> 
> >> >> Fixes: b57dc7c13ea9 ("net/sched: Introduce action ct")
> >> >> 
> >> >> Signed-off-by: Aaron Conole <aconole@redhat.com>
> >> >> ---
> >> >>  net/sched/act_ct.c | 13 ++++++++++++-
> >> >>  1 file changed, 12 insertions(+), 1 deletion(-)
> >> >> 
> >> >> diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c
> >> >> index fcc46025e790..f3232a00970f 100644
> >> >> --- a/net/sched/act_ct.c
> >> >> +++ b/net/sched/act_ct.c
> >> >> @@ -329,6 +329,7 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
> >> >>  			  bool commit)
> >> >>  {
> >> >>  #if IS_ENABLED(CONFIG_NF_NAT)
> >> >> +	int err;
> >> >>  	enum nf_nat_manip_type maniptype;
> >> >>  
> >> >>  	if (!(ct_action & TCA_CT_ACT_NAT))
> >> >> @@ -359,7 +360,17 @@ static int tcf_ct_act_nat(struct sk_buff *skb,
> >> >>  		return NF_ACCEPT;
> >> >>  	}
> >> >>  
> >> >> -	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> >> >> +	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> >> >> +	if (err == NF_ACCEPT &&
> >> >> +	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
> >> >> +		if (maniptype == NF_NAT_MANIP_SRC)
> >> >> +			maniptype = NF_NAT_MANIP_DST;
> >> >> +		else
> >> >> +			maniptype = NF_NAT_MANIP_SRC;
> >> >> +
> >> >> +		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> >> >> +	}
> >> >
> >> > I keep thinking about this and I'm not entirely convinced that this
> >> > shouldn't be simpler. More like:
> >> >
> >> > if (DNAT)
> >> > 	DNAT
> >> > if (SNAT)
> >> > 	SNAT
> >> >
> >> > So it always does DNAT before SNAT, similarly to what iptables would
> >> > do on PRE/POSTROUTING chains.
> >> 
> >> I can rewrite the whole function, but I wanted to start with the smaller
> >> fix that worked.  I also think it needs more testing then (since it's
> >> something of a rewrite of the function).
> >> 
> >> I guess it's not too important - do you think it gives any readability
> >> to do it this way?  If so, I can respin the patch changing it like you
> >> describe.
> >
> > I didn't mean a rewrite, but just to never handle SNAT before DNAT. So
> > the fix here would be like:
> >
> > -	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> > +	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> > +	if (err == NF_ACCEPT && maniptype == NF_NAT_MANIP_DST &&
> > +	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
> > +		maniptype = NF_NAT_MANIP_SRC;
> > +		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
> > +	}
> > +	return err;
> 
> But the maniptype of the first call could be NAT_MANIP_SRC.  In fact,
> that's what I see if the packet is reply direction && !related.

Interesting, ok.

> 
> So, we need the block to invert the manipulation type.  Otherwise, we
> miss the DNAT manipulation.
> 
> So I don't think I can use that block.

Thanks for digging on it.

Acked-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>

> 
> >> >> +	return err;
> >> >>  #else
> >> >>  	return NF_ACCEPT;
> >> >>  #endif
> >> >> -- 
> >> >> 2.21.0
> >> >> 
> >> 
>
diff mbox series

Patch

diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c
index fcc46025e790..f3232a00970f 100644
--- a/net/sched/act_ct.c
+++ b/net/sched/act_ct.c
@@ -329,6 +329,7 @@  static int tcf_ct_act_nat(struct sk_buff *skb,
 			  bool commit)
 {
 #if IS_ENABLED(CONFIG_NF_NAT)
+	int err;
 	enum nf_nat_manip_type maniptype;
 
 	if (!(ct_action & TCA_CT_ACT_NAT))
@@ -359,7 +360,17 @@  static int tcf_ct_act_nat(struct sk_buff *skb,
 		return NF_ACCEPT;
 	}
 
-	return ct_nat_execute(skb, ct, ctinfo, range, maniptype);
+	err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
+	if (err == NF_ACCEPT &&
+	    ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
+		if (maniptype == NF_NAT_MANIP_SRC)
+			maniptype = NF_NAT_MANIP_DST;
+		else
+			maniptype = NF_NAT_MANIP_SRC;
+
+		err = ct_nat_execute(skb, ct, ctinfo, range, maniptype);
+	}
+	return err;
 #else
 	return NF_ACCEPT;
 #endif