Skip to contentSkip to conversation

Redirecting packets based on source+destination ip's

DISCUSSION [email protected] 2 participants 2 messages Apr 9, 2006 – Apr 10, 2006

lore
Share & tools
lore
#0 lore raw

I have been trying to find a way with iptables to redirect a packet created on a server to be sent to 1.1.1.1 instead of 2.2.2.2 but only if the packet is coming from 3.3.3.3.

With the help of linuxquestions.org I have gotten to the point of using DNAT where the packet redirects, but the determining factor is destination address. Not source. Since this is a shared server(each user has a different ip) it would be nice to only redirect certain ip's, but leave the others alone.

In case I didn't make it understandable what I want to do here is what I am trying to accomplish, I rent a server for running a battlefield 2 server. This is of course shared, so there are other battlefield instances running next to mine albeit on different ip's. I want to run a stats program that requires redirecting bf2web.gamespy.com to 212.77.171.103 so that when my server sends out stats they go to ABR instead of EA. The usual way of doing this is with a hosts file, but that effects all ip's on the server, and causes some pretty big problems with the other servers on the machine.

Here is what the guy at linuxquestions.org gave me to work with iptables -t nat -A PREROUTING -t nat -p tcp -d 1.1.1.1 --dport 80 -j DNAT --to 2.2.2.2

To make it work I had to change PREROUTING to OUTPUT. So is there a way for that to only effect certain source ip's?

Thank you so much, Fourthbean

#1 lore raw
Quoted 2 lines · [email protected] wrote:

To make it work I had to change PREROUTING to OUTPUT. So is there a way for that to only effect certain source ip's?

If the packet originated from localhost, then OUTPUT is correct. -s is source and -d is destination ip.

iptables -t nat -A OUTPUT -s 3.3.3.3 -d 1.1.1.1 -p tcp -m tcp --dport 80 -j DNAT --to 2.2.2.2

Should work for you. Remove your previously inserted rule first though.

Please read the manual page of iptables and/or http://iptables-tutorial.frozentux.net/iptables-tutorial.html

HTH,
Alex