Fedora Linux Support Community & Resources Center
  #1  
Old 18th February 2010, 05:07 AM
rurikc Offline
Registered User
 
Join Date: Jan 2010
Posts: 62
linuxfedorafirefox
simple port redirect

Hi,

I want to do a simple port redirect, i.e. whatever comes trough whatever interface on port AAAA will get redirected to port BBBB

I thought that

iptables -t nat -I PREROUTING --source 0/0 --destination 0/0 -p tcp --dport AAAA -j REDIRECT --to-ports BBBB

however it doesn't work, e.g.
nc -v -w2 -z localhost AAAA
gives:
nc: connect to localhost port AAAA (tcp) failed: Connection refused
while
nc -v -w2 -z localhost BBBB
gives:
Connection to localhost BBBB port [....] succeeded!

I suspect that I am missing something obvious but what ?

(there is no firewall and net.ipv4.ip_forward=1)

Cheers,
Reply With Quote
  #2  
Old 18th February 2010, 08:41 PM
madhavdiwan Offline
Registered User
 
Join Date: Jun 2009
Posts: 472
windows_xp_2003firefox
you are using the wrong target

what you want to do , with the same syntax , is called port forwarding

and the target is DNAT

Quote:
/sbin/iptables -t nat -A PREROUTING -p tcp --source 0/0 -d 0/0
--dport 8888 -j DNAT --to 192.168.0.2:80
by the way do not forget you need a rule for FORWARD , because , iptables IS a firewall

Last edited by madhavdiwan; 18th February 2010 at 09:12 PM. Reason: removed incorrect REDIRECT def
Reply With Quote
  #3  
Old 18th February 2010, 08:49 PM
William Haller Offline
Registered User
 
Join Date: Jul 2005
Age: 52
Posts: 1,013
linuxsafari
/sbin/iptables -t nat -A PREROUTING -s 192.168.0.0/16 -p tcp --dport 80 -j REDIRECT --to-port 8081

works here to reroute http to dansguardian on our gateway. That may not be what you are trying to do, but at least it's a working example.

(Also have /sbin/iptables -A In-LAN -i + -p tcp -m tcp --sport 1024:65535 --dport 8081 -m state --state NEW -j ACCEPT as part of the main firewall configuration).

Last edited by William Haller; 18th February 2010 at 08:52 PM. Reason: Additional info
Reply With Quote
  #4  
Old 18th February 2010, 10:39 PM
rurikc Offline
Registered User
 
Join Date: Jan 2010
Posts: 62
linuxfedorafirefox
Quote:
Originally Posted by madhavdiwan View Post
you are using the wrong target

what you want to do , with the same syntax , is called port forwarding

and the target is DNAT
madhavdiwan,

My understanding is that DNAT is for forwarding to another IP

What I want is on the same IP (machine if you will)

I.e. I have server responding on port AAAA, I want also to respond if a request comes to port BBBB. My understanding was that a simple REDIRECT will do just that: move the packets from BBBB to AAAA

Quote:
Originally Posted by madhavdiwan View Post
by the way do not forget you need a rule for FORWARD , because , iptables IS a firewall
Even if there are no other rules ? (everything set to ACCEPT ?) can you give an example ?

Thanks.

Cheers,
Reply With Quote
  #5  
Old 18th February 2010, 10:55 PM
madhavdiwan Offline
Registered User
 
Join Date: Jun 2009
Posts: 472
windows_xp_2003firefox
the use of FORWARD rules depends on what your default Iptables FORWARD policy is set to.

most defualt policies that come with a distro, are set to accept everything , and rely on rules to deny

You are correct DNAT is usually for another IP, but does not have to be, If you want to keep the packet on the same machine , you could follow William Haller's example from his post

Last edited by madhavdiwan; 18th February 2010 at 11:17 PM.
Reply With Quote
  #6  
Old 18th February 2010, 11:01 PM
Gödel's Avatar
Gödel Offline
Registered User
 
Join Date: Jul 2009
Location: London,England
Posts: 1,095
linuxfedorafirefox
Actually, port forwarding can be local. And you can do it via the Firewall gui under the port forwarding section.

Once you have used the gui to do it, you can then type 'iptables-save' (as root) to see what commands were used:

eg if I forward port 9090 to 9091 locally on interface eth0 I get:

*nat
...
-A PREROUTING -i eth0 -p tcp -m tcp --dport 9090 -j DNAT --to-destination :9091
...
*filter
...
-A INPUT -i eth0 -p tcp -m state --state NEW -m tcp --dport 9091 -j ACCEPT
...
Reply With Quote
  #7  
Old 18th February 2010, 11:52 PM
rurikc Offline
Registered User
 
Join Date: Jan 2010
Posts: 62
linuxfedorafirefox
Quote:
Originally Posted by Gödel View Post
Actually, port forwarding can be local. And you can do it via the Firewall gui under the port forwarding section.

...
This is bizarre:

So I did used the firewall gui (system-config-firewall),

it did put in the rules as I expected and it still doesn't work

???

(this is a stock fedora kernel 2.6.31.12-174.2.3.fc12.x86_64 with nvidia on top)
Reply With Quote
  #8  
Old 19th February 2010, 04:16 PM
Gödel's Avatar
Gödel Offline
Registered User
 
Join Date: Jul 2009
Location: London,England
Posts: 1,095
linuxfedorafirefox
It would work if you specify the lan network interface in the iptables rule, and run the nc command from another machine on the lan.

But by default iptables doesn't filter loopback traffic, and I'm not sure how you would set it up (breaking loopback traffic will break lots of local services). If you need to enable this locally for testing puprposes then an easier solution would perhaps be to use something like socat:

Code:
yum install socat
socat TCP-LISTEN:AAAA,fork TCP:localhost:BBBB &
then run your tests, if it works locally and you finish testing then you can kill the socat process, and the iptables rules explained above will route the packets for incoming network connections.

To do it via iptables would require a filter table rule on the OUTPUT chain I assume, but I've not done this before so can't advise on that (The Firewall gui doesn't even have the loopback device lo in the interface list, so I assume it's not trivial or common)

---------- Post added at 04:16 PM CST ---------- Previous post was at 01:59 PM CST ----------

experimented this afternoon, and it's pretty straightforward actually, applying a rule to the OUTPUT chain in the nat table (not filter as I incorrectly suggested above)

Code:
iptables -t nat -A OUTPUT -p tcp --dport AAAA -j REDIRECT --to-ports BBBB
now (assuming something is listening on tcp port BBBB)

Code:
nc -v -w2 -z localhost AAAA
nc: connect to localhost port AAAA (tcp) failed: Connection refused
Connection to localhost AAAA port [tcp/*] succeeded!
the first Connection refused appears because the iptablesl only does ipv4 forwarding, and there are two localhost addresses defined in /etc/hosts, ::1 for ipv6 and 127.0.0.1 for ipv4 you can check with telnet:

Code:
$ telnet localhost AAAA
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
you won't get the refused message if you explicitly use 127.0.0.1

Code:
$ nc -v -w2 -z 127.0.0.1 AAAA
Connection to 127.0.0.1 AAAA port [tcp/*] succeeded!

(you need ip6tables for ipv6 rules)
Reply With Quote
  #9  
Old 19th February 2010, 04:17 PM
madhavdiwan Offline
Registered User
 
Join Date: Jun 2009
Posts: 472
windows_xp_2003firefox
.. OR

use the DNAT target
Reply With Quote
  #10  
Old 19th February 2010, 04:21 PM
Gödel's Avatar
Gödel Offline
Registered User
 
Join Date: Jul 2009
Location: London,England
Posts: 1,095
linuxfedorafirefox
Quote:
Originally Posted by madhavdiwan View Post
.. OR

use the DNAT target
I think you missed my update to the previous post, you can do it with a simple rule on the OUTPUT chain of the nat table

Code:
iptables -t nat -A OUTPUT -p tcp --dport AAAA -j REDIRECT --to-ports BBBB
Reply With Quote
  #11  
Old 19th February 2010, 04:26 PM
madhavdiwan Offline
Registered User
 
Join Date: Jun 2009
Posts: 472
windows_xp_2003firefox
not at all , i appreciate the fact that you found an answer, and did the research to prove it.
really , i was just pointing out that there is more than one way to do things.
Reply With Quote
Reply

Tags
port, redirect, simple

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
HowTo redirect Port ? faischue Servers & Networking 6 25th June 2008 07:31 AM
Redirect traffic to a port to an interface/ip pyutor Security and Privacy 1 7th August 2006 03:15 PM
redirect parallel port to usb fedoracorebeast Using Fedora 0 9th August 2005 11:57 PM
redirect parallel port to usb fedoracorebeast Using Fedora 0 9th August 2005 11:56 PM


Current GMT-time: 00:55 (Monday, 20-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat