Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'd like to set up a TCP DNAT from 127.0.0.1, port 4242 to 11.22.33.44, port 5353 on Linux 3.x (currently 3.2.52, but I can upgrade if needed).

It looks like the simple DNAT rule setup doesn't work, telnet 127.0.0.1 4242 hangs for a minute in Trying 127.0.0.1..., and then it times out. Maybe it's because the kernel is discarding the returning packets (e.g. SYN+ACK), because it considers them Martian. I don't need an explanation why the simple solution doesn't work, I need a solution, even if it's complicated (e.g. it involves creating may rules).

I could set up a usual DNAT from another local IP address, outside the 127.0.0.0/8 network, but now I need 127.0.0.1 as the destination address. I know that I can set up a user-level port forwarding process, but now I need a solution which can be set up using iptables and doesn't need helper processes.

I was googling for this for an hour. It was asked multiple times, but I couldn't find any working solutions.

Also there are many questions about DNAT to 127.0.0.1, but I don't need that, I need the opposite.

Please note that a single iptables -j REDIRECT rule wouldn't work, because I need to change the IP address of the outgoing packet from 127.0.0.1 to 11.22.33.44, and -j REDIRECT can't change the IP address.

share|improve this question

1 Answer 1

Is it something like this you need ? iptables port redirect not working for localhost

iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 443 -j REDIRECT --to-ports 8080

Adrien

share|improve this answer
    
Thank you for writing your answer. Unfortunately it doesn't solve my problem, because -j REDIRECT can't change the IP address, and I need to change it from 127.0.0.1 to 11.22.33.44. If I didn't need to change the IP address, it would work. –  pts Nov 8 '13 at 7:21
    
iptables won't change the IP address. What are you trying to achieve ? Why do you want to change the IP address ? –  Lawrence Nov 8 '13 at 7:48
    
Sorry, missed some lines. So, it's not a REDIRECT that you need, but rather something like : iptables -t nat -A PREROUTING -d 127.0.0.1 -j DNAT --to 11.22.33.44 But according to what you're trying to do, if you only change the 127.0.0.1 packet, you'll also need to change the return packet (11.22.33.44 => 127.0.0.1). Have a look on this Schema to understand placement of tables : –  Adrien M. Nov 8 '13 at 8:18
    
@Adrien M.: Yes, this (with -j DNAT) is the standard solution, but it doesn't work for telnet 127.0.0.1 4242 (it works with all other local IP address), because Linux is dropping the response packets (see the details in the question). If you think you have a solution which solves the problem, please post it. –  pts Nov 8 '13 at 13:19
    
@Lawrence: I'm trying to achieve that the connection initiated by telnet 127.0.0.1 4242 gets forwarded to 11.22.33.44:5353 by the kernel, without using a user-level port forwarder process. Sorry if this was not clear from the question. –  pts Nov 8 '13 at 13:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.