Skip to contentSkip to conversation

Expressive limitation: (daddr,dport) <--> (daddr',dport')

DISCUSSION Rick van Rein 2 participants 5 messages Jun 7, 2020 – Jun 8, 2020 netfilter

lore
Share & tools
lore
#0 lore raw

Hello,

I seem to be running into an expressive limitation of nft while trying to do stateless translation. I prefer statelessness because it it is clearer for bidirectionality / peering, and saves lookup times.

After nat64, I have a small set of IPv6 addresses and I would like to map their (daddr,dport) or better even (daddr,proto,dport) tuples to outgoing (daddr',dport'). Effectively, port forwarding for IPv6.

Individual rules work, like this one side of a bidir portmap:

nft add rule ip6 raw prerouting \
ip6 daddr $PREFIX::64:75 \
tcp dport 8080 \
ip6 daddr set $PREFIX::100:20 \
tcp dport set 80 \
notrack

I have problems doing this with the map construct, presumably because it does not atomically replace (daddr,dport) by (daddr',dport') but instead does two assignments with intermediate alterede state. This is bound to work in many cases, but it can give undesired crossover behaviours [namely between incoming IPs if they map to the same daddr' while coming from the same dport]:

nft add rule ip6 raw prerouting \

   ip6 daddr set \
      ip6 daddr . tcp dport \
         map { $PREFIX::64:75 . 8080 : $PREFIX::100:20 } \
   tcp dport set \
      ip6 daddr . tcp dport \
         map { $PREFIX::100:20 . 8080 : 80 } \

notrack

So now I am wondering,

0. Is there a way to use maps as atomic setter for (daddr,dport)?
1. Can I reach back to the original value of a just-modified value?
2. Is there a variable, or stack, to prepare with the old value?

Without this, I need to work around an expressive limitation,

* Fan out from a few IPv6 to many first to minimise rule clashes
* Make separate maps and rules and maps for each of the IPv6 addresses

Both sound to me like a lack of expressiveness, or that I missed how.

Thanks!
-Rick

#1 lore raw

Hi Rick,

Quoted 19 lines · Rick van Rein

Hello,

I seem to be running into an expressive limitation of nft while trying to do stateless translation. I prefer statelessness because it it is clearer for bidirectionality / peering, and saves lookup times.

After nat64, I have a small set of IPv6 addresses and I would like to map their (daddr,dport) or better even (daddr,proto,dport) tuples to outgoing (daddr',dport'). Effectively, port forwarding for IPv6.

Individual rules work, like this one side of a bidir portmap:

nft add rule ip6 raw prerouting \

   ip6 daddr $PREFIX::64:75 \
   tcp dport 8080 \
   ip6 daddr set $PREFIX::100:20 \
   tcp dport set 80 \
   notrack
Quoted 3 lines

I have problems doing this with the map construct, presumably because it does not atomically replace (daddr,dport) by (daddr',dport') but instead does two assignments with intermediate alterede state.

Right.

Quoted 11 lines

This is bound to work in many cases, but it can give undesired crossover behaviours [namely between incoming IPs if they map to the same daddr' while coming from the same dport]:

nft add rule ip6 raw prerouting \

   ip6 daddr set \
      ip6 daddr . tcp dport \
         map { $PREFIX::64:75 . 8080 : $PREFIX::100:20 } \
   tcp dport set \
      ip6 daddr . tcp dport \
         map { $PREFIX::100:20 . 8080 : 80 } \

So, you would consolidate this in one single rule? So there is one single lookup to obtain the IP address and the destination port for the stateless packet mangling.

Quoted 5 lines

notrack

So now I am wondering,

0. Is there a way to use maps as atomic setter for (daddr,dport)?

Not yet.

Quoted 1 line

1. Can I reach back to the original value of a just-modified value?

You mean, the original header field that was just mangled? Like matching on the former IP address before the mangling?

Quoted 1 line

2. Is there a variable, or stack, to prepare with the old value?

But this is to achieve the atomic mangling that you describe above or you have something else in mind? You would like to store the former IP daddr in some scratchpad area that can be accessed later on, right?

Thanks.

#2 lore raw

Hi Pablo / NFT-dev,

Quoted 12 lines
Quoted 11 lines

This is bound to work in many cases, but it can give undesired crossover behaviours [namely between incoming IPs if they map to the same daddr' while coming from the same dport]:

nft add rule ip6 raw prerouting \

   ip6 daddr set \
      ip6 daddr . tcp dport \
         map { $PREFIX::64:75 . 8080 : $PREFIX::100:20 } \
   tcp dport set \
      ip6 daddr . tcp dport \
         map { $PREFIX::100:20 . 8080 : 80 } \
Quoted 3 lines

So, you would consolidate this in one single rule? So there is one single lookup to obtain the IP address and the destination port for the stateless packet mangling.

It already is a single rule, but a single mapping, or one that appears like one. In reality, I use dynamic map @refs, of course.

A single lookup would avoid the problem that the key has changed in the second lookup.

I played around, trying if I could "ip6 daddr . tcp dport set" and perhaps have a map with elements like "{ $PREFIX::64:75 . 8080 :
$PREFIX::100:20 . 80 }" but did not find a syntax. [I've been missing a formal syntax, it's all examples so I wasn't sure if this was possible at all.] It'd look like

new_nft add rule ip6 raw prerouting \

   ip6 daddr . tcp dport set \
      ip6 daddr . tcp dport \
         map { $PREFIX::64:75 . 8080 : $PREFIX::100:20 . 80 }
Quoted 3 lines
Quoted 1 line

0. Is there a way to use maps as atomic setter for (daddr,dport)?

Not yet.

Ah, you spotted the problem too. No surprise ;-)

Quoted 4 lines
Quoted 1 line

1. Can I reach back to the original value of a just-modified value?

You mean, the original header field that was just mangled? Like matching on the former IP address before the mangling?

Yes, exactly. That way, I can use two maps but find the right combination of addr/port without intermediate key changes.

Quoted 5 lines
Quoted 1 line

2. Is there a variable, or stack, to prepare with the old value?

But this is to achieve the atomic mangling that you describe above or you have something else in mind? You would like to store the former IP daddr in some scratchpad area that can be accessed later on, right?

It is another possible way to get to the old value so I can make the same mapping.

I could imagine storing the old daddr in daddr2 then mapping daddr and using daddr2 in the second map looking to find the matching port. That might look like

new_nft add rule ip6 raw prerouting \
rulevar set ip6 daddr \

   ip6 daddr set \
      ip6 daddr . tcp dport \
         map { $PREFIX::64:75 . 8080 : $PREFIX::100:20 } \
   tcp dport set \
      rulevar . tcp dport \
         map { $PREFIX::100:20 . 8080 : 80 } \

If the language internally uses a stack, I could imagine pushing the old value(s) to prepare for the second map, then perform the first map and continue with the second. That might look like

new_nft add rule ip6 raw prerouting \
ip6 daddr push \

   ip6 daddr set \
      ip6 daddr . tcp dport \
         map { $PREFIX::64:75 . 8080 : $PREFIX::100:20 } \
   tcp dport set \
      pop . tcp dport \
         map { $PREFIX::100:20 . 8080 : 80 } \

The examples are just three syntaxes I can think of.

Thanks,
-Rick

#3 lore raw

Hi Rick,

Quoted 14 lines · Rick van Rein

Hi Pablo / NFT-dev,

Quoted 12 lines
Quoted 11 lines

This is bound to work in many cases, but it can give undesired crossover behaviours [namely between incoming IPs if they map to the same daddr' while coming from the same dport]:

nft add rule ip6 raw prerouting \

   ip6 daddr set \
      ip6 daddr . tcp dport \
         map { $PREFIX::64:75 . 8080 : $PREFIX::100:20 } \
   tcp dport set \
      ip6 daddr . tcp dport \
         map { $PREFIX::100:20 . 8080 : 80 } \
Quoted 6 lines
Quoted 3 lines

So, you would consolidate this in one single rule? So there is one single lookup to obtain the IP address and the destination port for the stateless packet mangling.

It already is a single rule, but a single mapping, or one that appears like one. In reality, I use dynamic map @refs, of course.

Right, one single mapping.

Quoted 9 lines

A single lookup would avoid the problem that the key has changed in the second lookup.

I played around, trying if I could "ip6 daddr . tcp dport set" and perhaps have a map with elements like "{ $PREFIX::64:75 . 8080 :
$PREFIX::100:20 . 80 }" but did not find a syntax. [I've been missing a formal syntax, it's all examples so I wasn't sure if this was possible at all.] It'd look like

Quoted 4 lines
new_nft add rule ip6 raw prerouting \
   ip6 daddr . tcp dport set \
      ip6 daddr . tcp dport \
         map { $PREFIX::64:75 . 8080 : $PREFIX::100:20 . 80 }

I see.

Quoted 50 lines
Quoted 3 lines
Quoted 1 line

0. Is there a way to use maps as atomic setter for (daddr,dport)?

Not yet.

Ah, you spotted the problem too. No surprise ;-)

Quoted 4 lines
Quoted 1 line

1. Can I reach back to the original value of a just-modified value?

You mean, the original header field that was just mangled? Like matching on the former IP address before the mangling?

Yes, exactly. That way, I can use two maps but find the right combination of addr/port without intermediate key changes.

Quoted 5 lines
Quoted 1 line

2. Is there a variable, or stack, to prepare with the old value?

But this is to achieve the atomic mangling that you describe above or you have something else in mind? You would like to store the former IP daddr in some scratchpad area that can be accessed later on, right?

It is another possible way to get to the old value so I can make the same mapping.

I could imagine storing the old daddr in daddr2 then mapping daddr and using daddr2 in the second map looking to find the matching port. That might look like

new_nft add rule ip6 raw prerouting \
   rulevar set ip6 daddr \
   ip6 daddr set \
      ip6 daddr . tcp dport \
         map { $PREFIX::64:75 . 8080 : $PREFIX::100:20 } \
   tcp dport set \
      rulevar . tcp dport \
         map { $PREFIX::100:20 . 8080 : 80 } \

If the language internally uses a stack, I could imagine pushing the old value(s) to prepare for the second map, then perform the first map and continue with the second. That might look like

new_nft add rule ip6 raw prerouting \
   ip6 daddr push \
   ip6 daddr set \
      ip6 daddr . tcp dport \
         map { $PREFIX::64:75 . 8080 : $PREFIX::100:20 } \
   tcp dport set \
      pop . tcp dport \
         map { $PREFIX::100:20 . 8080 : 80 } \
Quoted 1 line

The examples are just three syntaxes I can think of.

OK, but you only need this "stack" idea is alternative proposal to get the "one single mapping" idea working, correct?

Thanks.

#4 lore raw

Hi Pablo,

Thanks for following up on this :)

Quoted 4 lines
Quoted 1 line

The examples are just three syntaxes I can think of.

OK, but you only need this "stack" idea is alternative proposal to get the "one single mapping" idea working, correct?

Yes, indeed.

-Rick