All of lore.kernel.org
 help / color / mirror / Atom feed
* iptables dnat to loopback
@ 2004-08-06 11:38 Damian Gatabria
  2004-08-06 11:54 ` Klemen Kecman
  0 siblings, 1 reply; 10+ messages in thread
From: Damian Gatabria @ 2004-08-06 11:38 UTC (permalink / raw)
  To: Lista Netfilter

Hello listers.

I know this is probably not the proper place to ask 
a support question, but after a long googling
session and asking in my regular lists i was unable to 
get an answer for what i think is a rather tough situation..

So, flame me if you must, but here it is =oP

I have a rather busy server box hosting several web
sites in separate independent installation directories,
which means several apache, mysql and tomcat installations
running simultaneously, with each mysql instance binding
a port in the loopback interface.

Now, for a very specific reason, i need to provide someone
with remote access to one of these mysql instances. 

It would be a real hassle to change mysql's config
(and the webapps' working with it) so i just decided
to use DNAT to redirect incoming tcp connections 
to the loopback interface.... but for some reason you
hopefully already know about, it doesn't seem to work. 

So, while this iptables rule does work for port forwarding
to another host:

iptables -t nat -A PREROUTING -p tcp -i eth0 -m multiport \
        --dports mysql -j DNAT --to some.other.host

changing "some.other.host" to 127.0.0.1 doesn't work
as expected, and packets seem to be dropped altogether.
Using tcpdump i can see packets reaching eth0, but
never reaching loopback.

Is there any way to make iptables do what i'm looking
for? Any help will be greatly appreciated.

Thanks much.

-- 
Damian Gatabria <damian_g@speedy.com.ar>



^ permalink raw reply	[flat|nested] 10+ messages in thread
* RE: iptables dnat to loopback
@ 2004-08-06 12:48 Jason Opperisano
  0 siblings, 0 replies; 10+ messages in thread
From: Jason Opperisano @ 2004-08-06 12:48 UTC (permalink / raw)
  To: netfilter

> I have a rather busy server box hosting several web
> sites in separate independent installation directories,
> which means several apache, mysql and tomcat installations
> running simultaneously, with each mysql instance binding
> a port in the loopback interface.
>
> So, while this iptables rule does work for port forwarding
> to another host:
>
> iptables -t nat -A PREROUTING -p tcp -i eth0 -m multiport \
>         --dports mysql -j DNAT --to some.other.host
>
> changing "some.other.host" to 127.0.0.1 doesn't work
> as expected, and packets seem to be dropped altogether.
> Using tcpdump i can see packets reaching eth0, but
> never reaching loopback.

the target used for this is REDIRECT.  from "man iptables"

   REDIRECT
       This target is only valid in the nat table, in the PREROUTING and  OUT-
       PUT  chains,  and  user-defined chains which are only called from those
       chains.  It alters the destination IP address to send the packet to the
       machine  itself  (locally-generated packets are mapped to the 127.0.0.1
       address).  It takes one option:

       --to-ports port[-port]



HTH...

-j


^ permalink raw reply	[flat|nested] 10+ messages in thread
* RE: iptables dnat to loopback
@ 2004-08-06 17:13 Jason Opperisano
  2004-08-07 10:15 ` David Cannings
  2004-08-08  6:17 ` Damian Gatabria
  0 siblings, 2 replies; 10+ messages in thread
From: Jason Opperisano @ 2004-08-06 17:13 UTC (permalink / raw)
  To: netfilter; +Cc: damian_g

> Unfortunately, it does not seem to work either =(
> On the server itself i can `telnet 127.0.0.1 3306`
> and connect to the listening mysql process. But
> after adding this rule:
>
> iptables -A PREROUTING -t nat -p tcp -s (client address) --dport \
> 3306 -j REDIRECT --to-ports 3306
>
> I sit in the client box and `telnet (server ip address) 3306`
> and just get a "connection refused".
>
> Am i missing something? Is the rule ok? I have also tried
> removing the "--to-ports" option to leave the port unchanged,
> but the result is the same.

yes--my post was misleading.  REDIRECT does not precisely do what you want.  it rewrites the destination IP address of the packet to be the primary IP of the interface the packet is received on.  i just verfied this by poking through "/usr/src/linux/net/ipv4/netfilter/ipt_REDIRECT.c"--somewhere around line 85, you'll see: 

                /* Grab first address on interface. */
                newdst = indev->ifa_list->ifa_local;

which will not get the packet to 127.0.0.1.

however, after some further testing--your original DNAT *should* work--the problem is probably somewhere in your filter rules.  i just tested this with a machine that has sendmail bound only to 127.0.0.1:

# netstat -lnt | grep 25
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN

#iptables -t nat -I PREROUTING -i eth0 -p tcp -d 172.30.30.2 --dport 25 -j DNAT --to 127.0.0.1:25

the log entry associated with the incoming, DNAT-ed packet may not look exactly as you suspect; however.  this is what popped into my logs upon a successful "telnet 172.30.30.2 25" (the packet is received in the INPUT chain, btw):

Aug  6 13:06:33 fw kernel: IN=lo OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:08:00 SRC=127.0.0.1 DST=127.0.0.1 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=21041 DF PROTO=TCP SPT=35801 DPT=25 WINDOW=32767 RES=0x00 SYN URGP=0

note the inbound interface is "lo" and both the src and dst IP's are 127.0.0.1.  if you need to filter this kind of connection--make sure you specify a "-s x.x.x.x" in your DNAT rule.

sorry about my earlier post... hope *this* one helps...

-j

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2004-08-10  1:29 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-08-06 11:38 iptables dnat to loopback Damian Gatabria
2004-08-06 11:54 ` Klemen Kecman
  -- strict thread matches above, loose matches on Subject: below --
2004-08-06 12:48 Jason Opperisano
2004-08-06 17:13 Jason Opperisano
2004-08-07 10:15 ` David Cannings
2004-08-08  6:17 ` Damian Gatabria
2004-08-08  8:41   ` David Cannings
2004-08-08 17:50     ` Alistair Tonner
2004-08-09 14:40       ` Damian Gatabria
2004-08-10  1:29         ` Damian Gatabria

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.