LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2009, 02:25 PM   #1
casolorz
LQ Newbie
 
Registered: Feb 2009
Posts: 11

Rep: Reputation: 0
Iptables forwarding from gateway back to the inside network


We have a bunch of machines on a private subnet and we can port forward to them really easily however they can not access the port forwards by going to the gateway ip.
So for example:

iptables -A FORWARD -i eth0 -d ${PRIVATE_SUBNET}.3 -p tcp --dport 3341 -j ACCEPT
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 3341 -j DNAT --to-destination ${PRIVATE_SUBNET}.3

from the outside world if I go to the IP of eth0 and port 3341 it works just fine. But if I go from the inside lan to port 3341 on the eth0 IP it all fails.

How can I allow the gateway to forward ports back into the LAN when those ports are requested from the LAN?

Thanks,

--Carlos
 
Old 02-02-2009, 04:34 PM   #2
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
Think about packets which are sent back as a reply.

The host A (local network) is sending packet to host B (IP:3341) and it expects the reply will have src = IP.
But you have a rule
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 3341 -j DNAT --to-destination ${PRIVATE_SUBNET}.3
which makes that returned packet will have scr = ${PRIVATE_SUBNET}.3 since this is the ip of the replying host.

As a result the reply which is received by A has "incorrect" src = ${PRIVATE_SUBNET}.3 against expected IP.
 
Old 02-02-2009, 04:47 PM   #3
casolorz
LQ Newbie
 
Registered: Feb 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dorian33 View Post
Think about packets which are sent back as a reply.

The host A (local network) is sending packet to host B (IP:3341) and it expects the reply will have src = IP.
But you have a rule
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 3341 -j DNAT --to-destination ${PRIVATE_SUBNET}.3
which makes that returned packet will have scr = ${PRIVATE_SUBNET}.3 since this is the ip of the replying host.

As a result the reply which is received by A has "incorrect" src = ${PRIVATE_SUBNET}.3 against expected IP.
So how can I make it so PublicIP:3341 goes to ${PRIVATE_SUBNET}.3 from the outside world and from the LAN?
I know my IPCop does that just fine but I can't figure out how its doing it.

Thanks,

--Carlos
 
Old 02-02-2009, 06:24 PM   #4
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
The whole trick is to force both the query and reply are sent via router.

Sending the query from LAN host (A) the packet is sent to the router (since its dst address is public) and next is routed to the LAN host S (server) with ip ${PRIVATE_SUBNET}.3.
When the host S receives the query packet it see the packet as sent directly from A (the src address in the query packet belongs LAN subnet). So the reply is sent to the A directly arther than to the router.

Therefore you have to make the host S to sent the reply to the router by changing the src address in the query packet received by S.

Looks like this (or similar) will help:
iptables -t nat -A POSTROUTING -i eth1 -p tcp -d ${PUBLIC_IP} -dport 3341 -j SNAT --to-source ${PUBLIC_IP}
(eth1 is LAN interface)

This way host S will send the reply to host B (router) rather than directly to host A.
The router will made de-SNAT and de-DNAT next and sends the packet to the host A.

I hope I didn't made mistake above...
And remember you need to allow forward of the packets from S to A also.
 
Old 02-03-2009, 01:26 PM   #5
casolorz
LQ Newbie
 
Registered: Feb 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks for the reply. I think I understand the problem. I am still trying to get the iptables commands correctly. I'll post back once I get it working.
 
Old 02-03-2009, 03:18 PM   #6
casolorz
LQ Newbie
 
Registered: Feb 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by casolorz View Post
Thanks for the reply. I think I understand the problem. I am still trying to get the iptables commands correctly. I'll post back once I get it working.
Ok I have fixed it but I don't like my fix. My problem was that I do my prerouting with the interface and not the destination ip. So I use -i eth0 instead of -d ${PUBLICIP}
Code:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination ${PRIVATE_SUBNET}.3
As soon as I switch it to -d it starts working.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Port Forwarding trouble with iptables on ubuntu gateway elgranjefe Linux - Networking 2 11-17-2008 07:44 PM
Simple Network forwarding with iptables javiersp Linux - Networking 1 02-26-2008 08:13 PM
Testing Port Forwarding from inside network? humbletech99 Linux - Networking 2 07-08-2006 02:37 AM
IPTABLES NAT Gateway, No Access from the inside? nweaver916 Linux - Networking 2 08-27-2004 03:46 PM
IPTABLES port forwarding to internal network ivanros Linux - Networking 2 12-28-2002 10:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 07:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration