Skip to contentSkip to conversation

port forwarding question

DISCUSSION Phil Sutter 4 participants 5 messages Mar 17, 2008 – Mar 18, 2008

lore
Share & tools
lore
#0 lore raw

Hi,

We have been using commands like the following to forward a single port on our Linux systems and it works fine:

iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport 5080 -j DNAT --to 192.168.1.10:80

I am now trying to find a method for port forwarding a range of ports. For instance, forwarding port 5080 - 5084 to ports 80 - 84 so that:
- port 5080 traffic ends up on port 80
- port 5081 traffic ends up on port 81
- port 5082 traffic ends up on port 82
- port 5083 traffic ends up on port 83
- port 5084 traffic ends up on port 84

I thought I could do the following but it does not work:

iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport 5080:5085 -j DNAT --to 192.168.1.10:80-85

Is there a way to do what I want to do with a single command or do I have to forward each port with an individual command?

Thanks,

Phil Sutter

#1 lore raw
Quoted 8 lines · Phil Sutter

I thought I could do the following but it does not work:

iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport 5080:5085 -j DNAT --to 192.168.1.10:80-85

Is there a way to do what I want to do with a single command or do I have to forward each port with an individual command?

To do it with a single rule requires your own target extension.

:80-85 just tells it to choose any one of it.

#2 lore raw

Em Seg, 2008-03-17 às 19:13 +0100, Jan Engelhardt escreveu:

Quoted 10 lines · Phil Sutter
Quoted 8 lines

I thought I could do the following but it does not work:

iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport 5080:5085 -j DNAT --to 192.168.1.10:80-85

Is there a way to do what I want to do with a single command or do I have to forward each port with an individual command?

Quoted 8 lines

To do it with a single rule requires your own target extension.

:80-85 just tells it to choose any one of it.
--
To unsubscribe from this list: send the line "unsubscribe netfilter" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

AFAIK, don't exist any single command to do that... What exists, is NETMAP target that DNAT/SNAT every single address in two ranges. i.e: "iptables -t mangle -A PREROUTING -s 192.168.1.0/24 -j NETMAP --to 10.5.6.0/24"

If anyone knows any command that do this, I'll be pleased to know too... If don't, this is a good feature to implement in futures versions of IPTables.

[]s

Cloves

#3 lore raw
Quoted 12 lines
Quoted 11 lines
Quoted 7 lines

I thought I could do the following but it does not work:

iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport 5080:5085 -j DNAT --to 192.168.1.10:80-85

Is there a way to do what I want to do with a single command or do I have to forward each port with an individual command?

To do it with a single rule requires your own target extension.

:80-85 just tells it to choose any one of it.

Quoted 8 lines

AFAIK, don't exist any single command to do that... What exists, is NETMAP target that DNAT/SNAT every single address in two ranges. i.e: "iptables -t mangle -A PREROUTING -s 192.168.1.0/24 -j NETMAP --to 10.5.6.0/24"

If anyone knows any command that do this, I'll be pleased to know too... If don't, this is a good feature to implement in futures versions of IPTables.

It doesn't seem like a high priority for iptables, since the same thing can easily and more flexibly be accomplished with some bash scripting:

for (( i=80 ; i<=85 ; ++i ))
do

  iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport $(( 5000+i )) \
    -j DNAT --to 192.168.1.10:$i

done

Yes, that is 6 iptables rules, but the performance difference is probably negligible, it's simple to code, and it's totally customizable to the user's needs. A specially written iptables target, OTOH, would require a whole separate kernel module just to cover this one fairly unusual transformation.

Andrew.

--
To unsubscribe from this list: send the line "unsubscribe netfilter" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 5 lines · Andrew Schulman

It doesn't seem like a high priority for iptables, since the same thing can easily and more flexibly be accomplished with some bash scripting:

for (( i=80 ; i<=85 ; ++i ))
do

Quoted 8 lines
 iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport $(( 5000+i )) \
   -j DNAT --to 192.168.1.10:$i

done

Yes, that is 6 iptables rules, but the performance difference is probably negligible, it's simple to code, and it's totally customizable to the user's needs. A specially written iptables target, OTOH, would require a whole separate kernel module just to cover this one fairly unusual transformation.

	iptables -p tcp --dport A:B -j DNAT --to xxx:C-D

And you would _also_ have to deal with cases where amount of(A..B) and amount of(C..D) are not the same. No, it would be too troublesome.