LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 06-18-2012, 07:49 AM   #1
CaptainJack
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Rep: Reputation: Disabled
Forwarding packets without changing source IP


Hi,


I have the following rule to forward packets coming externally from server A to external server B.

I have the following rule to do this for me.

iptables -t nat -A PREROUTING -p tcp --dport <server_a_port> -j DNAT --to-destination <server_b_ip>:<server_b_port>
iptables -t nat -A POSTROUTING -j MASQUERADE

It works fine with one exception: the source IP coming into server B is changed to that of server A. Is there a way to do this without changing the external source IP - i.e. I want server B to see the same source IP that comes into server A.

I tried doing:

iptables -t nat -A POSTROUTING -j ACCEPT

...but it didn't work.

Comments/ideas?

Thanks in advance.
 
Old 06-18-2012, 07:55 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
just remove the MASQUERADE entry completely. The NAT table doesn't accept or deny packets, that's the FILTER table. Just don't NAT it at all.
 
Old 06-18-2012, 08:01 AM   #3
CaptainJack
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
If I remove it completely then the forwarding doesn't work. That is I cannot connect to server B through server A. I have to add the MASQUERADE entry for it to work.

I have tried this without having any other entries in the table.
 
Old 06-18-2012, 08:04 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
No, you don't. The POSTROUTING table is after the forwarding has been done. It's already been routed by that stage.

if you tcpdump on the outbound interface, does the packet not show up?
 
Old 06-18-2012, 08:29 AM   #5
CaptainJack
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Actually it does, but on server B it seems like it's trying to return the reply packet directly to the source IP. And since it connected to server A, the client is expecting a reply from server A, not server B.

I guess that's why you need a MASQUERADE (or SNAT) rule.

Am I correct in thinking about this? Can I add some additional rules on server B to return packets via server A back to the client?
 
Old 06-18-2012, 08:56 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Well yes, that's what IP routing is. That's the exact specific precise reason NAT exists!

This presumably IS a routed network with DIFFERENT address ranges? What do you mean by "direct" here? That suggests it's on the local subnet... the return route is usually defined by the stateless routing table on the destination host, so you would usually identify the sub-range of addresses that need to go back via this intermediate devices and put that in the routing table.
 
Old 06-18-2012, 09:19 AM   #7
CaptainJack
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yes, server A and server B are on different networks - both external.

Right, so how do I do that? Let's say server A is 1.1.1.1, server B is 2.2.2.2. What is happening with the packet at the moment is:

Incoming packet:
1.2.3.4 (source IP) >>> 1.1.1.1 (server A) >>> 1.2.3.4 source is changed to 1.1.1.1 >>> 2.2.2.2 (server B)

Reply back:
2.2.2.2 (server B) >>> 1.1.1.1 (server A) >>> 1.2.3.4

Without SNAT - incoming packet
1.2.3.4 (source IP) >>> 1.1.1.1 (server A) >>> 1.2.3.4 source IP is not changed >>> 2.2.2.2 (server B)

Reply back:
2.2.2.2 (server B) >>> 1.2.3.4 ...bypassing server A. Therefore the packet never reaches the initiating client.

And I want it like so - incoming packet:
1.2.3.4 (source IP) >>> 1.1.1.1 (server A) >>> 1.2.3.4 source IP is not changed >>> 2.2.2.2 (server B)

Reply back:
2.2.2.2 (server B) >>> 1.1.1.1 (server A) >>> 1.2.3.4

If I was to set up a route, then that means ALL packets going out of server B would go via server A. I don't want that - I only want packets going out via server A if they came in via server A. Is there a way to do that using IP tables?
 
Old 06-18-2012, 09:25 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
You should stick with MASQUERADE. There are ways to do it, using iptables to force packets to use alternative routing tables, but those solutions just end up hacky and obscure. This might be of interest to you though... http://parkersamp.com/2010/02/howto-...vlan-in-linux/ Again, I would try to avoid this kind of meddling though.

Last edited by acid_kewpie; 06-18-2012 at 09:29 AM.
 
Old 06-18-2012, 09:30 AM   #9
CaptainJack
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Fair enough. I thought it was getting a little complicated but if there's a simpler way of doing this, I'd love to hear.

Thanks for your help so far, Chris.

Alex
 
Old 06-18-2012, 09:44 AM   #10
CaptainJack
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Just been reading about it. Looks like I can mark forwarded packets and using the marks route them back using different routing tables (like you said). Seems a little complicated for my little brain though.
 
Old 06-18-2012, 09:50 AM   #11
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
See, now I spend all day working with F5 BigIP load balancers and they just have a lovely tick box in the config called "auto last hop" which ensures that all traffic coming in to a given service goes back on the network with the MAC of the device it came from, totally ignoring the routing tables, which is clearly exactly what you want. Not sure what's actually going on under the hood for that. I think it's probably bespoke code, not iptables or anything not generically Linux. So if you want to spend $100,000 on a pair of BigIP's it's a doddle!
 
Old 06-18-2012, 09:54 AM   #12
CaptainJack
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hah, I am not that desperate to see the source IP - as nice as it would have been. For $100,000 I will learn ip routes and iptables - but again, it's not essential.

But yes, it looks like exactly the thing I would need.
 
  


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
Forwarding UDP Packets tzahi Linux - Networking 1 03-12-2006 02:02 PM
Forwarding packets between two NIC madhavann Linux - Networking 6 02-17-2006 10:23 PM
Forwarding packets with Iptables DrunkenDisciple Linux - Software 2 07-24-2005 11:00 PM
Not forwarding packets meadensi Linux - Networking 0 02-08-2005 07:02 PM
Forwarding ACK Packets snufferz Linux - Newbie 0 05-12-2004 02:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 10:01 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