LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   basic port forward help (https://www.linuxquestions.org/questions/linux-networking-3/basic-port-forward-help-915259/)

dbudbu 11-24-2011 12:44 AM

basic port forward help
 
As an exercise, trying to make a trivial port forward work. Eg. forward requests to 12345 to standard ssh. Starting as small a possible...

# iptables -t nat -A PREROUTING -p tcp --dport 12345 -j DNAT --to 22
# telnet localhost 12345
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

# telnet localhost 22
Trying 127.0.0.1...
Connected to localhost.
^]

Forwarding on, all policies wide open, accept. Can someone throw me a bone?

fukawi1 11-24-2011 01:09 AM

Code:

-j DNAT --to-port 22

dbudbu 11-24-2011 01:17 AM

Thanks, but I had typo; I tried

--to 127.0.0.1:22
--to-port 22
--to-destination 127.0.0.1:22

Same result.

fukawi1 11-24-2011 01:18 AM

Hows about?

Code:

-j REDIRECT --to-port 22

dbudbu 11-24-2011 01:25 AM

So.. you would have me say:

Code:

# iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to-destination 127.0.0.1:22
.. so I'd be forwarding _from_ *:22 -> localhost:22. But I want to forward from 12345->22.

fukawi1 11-24-2011 01:28 AM

Yeah, i brain farted, I just edited that post...
Dont mind me, long day :p

dbudbu 11-24-2011 08:46 AM

only from outside.
 
I cannot claim to understand it, not even entirely believe it, but the failure seems to be limited to attempts to navigate the iptables DNAT/SNAT rules on a host from within that host.

So if I have an interface with address 192.168.1.100 and make a rule to send hiport traffic to the ssh port:

Code:

# iptables -t nat -A PREROUTING -p tcp -d 192.168.1.100 --dport 12345 -j DNAT --to 192.168.1.100:22
That rule works great if you come in from the outside world with the command
Code:

$ telnet 192.168.1.100 12345
  ..connected...

but that indentical incantation from within the host with DNAT running on it fails. So there is something about locally sourced socket traffic that causes iptables to not do the expected. I guess we live with it..

TimothyEBaldwin 11-28-2011 02:37 AM

Quote:

Originally Posted by dbudbu (Post 4532485)
# iptables -t nat -A PREROUTING -p tcp --dport 12345 -j DNAT --to 22
# telnet localhost 12345
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

To apply to locally source traffic you need to use the OUTPUT chain, not PREROUTING.


All times are GMT -5. The time now is 12:40 PM.