I have a server bound to localhost:7060. It is using ipv6 socket instead of ipv4. Below is netstat outout.

# netstat -an
Proto Recv-Q Send-Q Local Address          Foreign Address        State
 tcp       0      0*              LISTEN
 tcp       0      0 *              LISTEN
 tcp       0      0 *              LISTEN
 tcp       0      0        ESTABLISHED
 tcp       0      0     ESTABLISHED
tcp6       0      0 :::5060                :::*                   LISTEN
tcp6       0      0 ::ffff:  :::*                   LISTEN
tcp6       0      0 :::23                  :::*                   LISTEN
tcp6       0      0 ::ffff: ::ffff: ESTABLISHED
tcp6       0      0 ::ffff: ::ffff:  ESTABLISHED
tcp6       0      0 ::ffff: ::ffff: ESTABLISHED
udp6       0      0 :::5060                :::*                   CLOSE

I want to setup a port forwarding rule that accepts connections on port 24 (on all interfaces loopback as well as eth0) and forward the data to localhost:7060.

This is how I am setting up the iptables rule:

iptables -t nat -A PREROUTING -p tcp --dport 24 -j DNAT --to**

It is not working. When I telnet from different box, I see the following

$telnet 24

If I change the server to bind to *:7060 and set the following rule, it seems to work fine.

iptables -t nat -A PREROUTING -p tcp --dport 24 -j REDIRECT --to-port 7060

But that will make my server available on WAN interface which I don't like.

I feel it had something to do with ipv6 socket (tcp6 line in netstat output). This whole thing is done on an Android device with custom built Android platform image.

How do I get this working?

  • 1
    If your socket is IPv6, you need to do IPv6 filtering. Further, I am of the understanding that ip6tables does not yet support NAT for things like masquerading or port forwarding (your case). Try making the socket an IPv4 one instead and see if your rule works.
    – Garrett
    Nov 30, 2011 at 17:05

2 Answers 2


When the response packets coming back from port 7060 and being sent to the router, these packets also need a sender mask operation for it, to mask these packets source address to router's address( and port as 24.So you need to add a SNAT iptables rule to make it work.

iptables -t nat -A POSTROUTING -p tcp --sport 7060 -j MASQUERADE --to-ports 24

Even though the packets is generated by localhost, it will also go into the POSTROUTER chain. REDIRECT operation automatically do these two things for you, but if your service is on another server in your local net, you have to use SNAT and DNAT.


I think you must use --sport 24 instead of --dport 24, because de traffic is incoming, not outgoing. Although, as Garret said, probably you must use ip6tables...

  • 1
    wrong, the incoming connection has the destination port 24 thus --dport should be used. Usually the source port(from the connecting client) is assigned randomly for every.
    – hultqvist
    Apr 12, 2013 at 8:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .