Skip to contentSkip to conversation

iptables rule not matching after stream begins

LARTC Bob Beers 3 participants 6 messages Nov 21, 2006

lore
Share & tools
lore

Bob Beers

author
#0 lore raw

Trying again, after re-subscribing:

Quoted 29 lines · Bob Beers

Hello,

I want to dynamically create DNAT rules for
RTP streams (port-mapping for a SIP proxy).

If my proxy adds the rule before the first packet
of the RTP stream hits the port, all is well. But, if the stream begins arriving before my rule is in place, it never matches. I cannot always be sure that the info for setting up the rule arrives sufficiently ahead of the stream.

I suspect if there is a simple resolution to my
problem. Does anyone else see this behavior,
and will share with me the solution?

Apologies if there is a better place to seek an
answer to this question, please redirect me as
necessary.

I am using kernel 2.6.15.4, and iptables 1.3.3.

My rules are similar to this:
iptables -I PREROUTING -t nat -p UDP \

  -d<public_ip> --dport <public_port> \
   -j DNAT --to-destination <private_ip:port>
iptables -I FORWARD -p UDP \
   -d <private_ip> --dport <private_port>
Signature · 5 lines
-Bob
_______________________________________________
LARTC mailing list
[email protected]
http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc
#1 lore raw
Quoted 2 lines · Bob Beers
Quoted 2 lines

I want to dynamically create DNAT rules for
RTP streams (port-mapping for a SIP proxy).

Have you considered testing any of the patches from netfilter's patch-o-matic?

There are two patches that seem promising. Quoting from the netfilter website:

http://www.netfilter.org/patch-o-matic/pom-extra.html#pom-extra-rtsp-conntrack
rtsp-conntrack - RTSP connection tracking and nat helper
Author: Tom Marshall <[email protected]>
Status: Beta - needs some testing and porting to 2.6.x
This patch adds CONFIG_IP_NF_RTSP: support for the RTSP protocol. This allows UDP transports to be setup properly, including RTP and RDT.

http://www.netfilter.org/patch-o-matic/pom-extra.html#pom-extra-sip-conntrack-nat
ip-conntrack-nat - SIP connection tracking and NAT helper
Author: Christian Hentschel <[email protected]>
Status: Alpha
This adds CONFIG_IP_NF_SIP: SIP support module for netfilter connection tracking and NAT.
The SIP conntrack/NAT modules support the connection tracking/NATing of the data streams requested on the dynamic RTP/RTCP ports, as well as mangling
of SIP requests/responses.

Joe
_______________________________________________
LARTC mailing list
[email protected]
http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc

author
#2 lore raw
In reply to Bob Beers

Thank you, Joe, for your response

Quoted 2 lines · Flophouse Joe

Have you considered testing any of the patches from netfilter's patch-o-matic?

I will consider doing just that.

Quoted 5 lines

There are two patches that seem promising. Quoting from the netfilter website:

http://www.netfilter.org/patch-o-matic/pom-extra.html#pom-extra-rtsp-conntrack

...

Quoted 1 line

http://www.netfilter.org/patch-o-matic/pom-extra.html#pom-extra-sip-conntrack-nat

...

Thanks for the pointers.

But, I think my problem is really more basic. I only mentioned
that it was SIP related for background.

Let me try to restate my question:

Is it a common problem that inserting a rule after a (UDP) stream is
established does not match the rule, even though the exact same rule for the exact same stream does match, as long as it is inserted before the first packet of the stream arrives?

If so, (that it is a common, or at least known, problem), how does
one overcome this problem? Is there a way to "disconnect" the stream, once the rule is installed, so that can match? Like I said in the original post, everything works right, as long as my rule wins the race with the first packet.

Any other hints most welcome, meanwhile I will examine the two
patches mentioned. Thanks,

Quoted 3 lines

Joe

Bob
_______________________________________________
LARTC mailing list
[email protected]
http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc

#3 lore raw
In reply to Bob Beers
Quoted 6 lines · Bob Beers

Let me try to restate my question:

Is it a common problem that inserting a rule after a (UDP) stream is established does not match the rule, even though the exact same rule for the exact same stream does match, as long as it is inserted before the first packet of the stream arrives?

This is the way it is designed: PREROUTING rules in the nat table are only checked for packets that haven't already been assigned to a connection. If you want, you can use the conntrack tool to flush the connection states after you add a new rule.

Alexey
_______________________________________________
LARTC mailing list
[email protected]
http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc

author
#4 lore raw
In reply to Bob Beers
Quoted 9 lines · Alexey Toptygin
Quoted 6 lines · Bob Beers

Let me try to restate my question:

Is it a common problem that inserting a rule after a (UDP) stream is established does not match the rule, even though the exact same rule for the exact same stream does match, as long as it is inserted before the first packet of the stream arrives?

Quoted 4 lines

This is the way it is designed: PREROUTING rules in the nat table are only checked for packets that haven't already been assigned to a connection. If you want, you can use the conntrack tool to flush the connection states after you add a new rule.

Ah, yes, this sounds like what I need. Please excuse my ignorance, but
how does one "use the conntrack tool to flush the connection states after you add a new rule"? I have read through several tutorials and the iptables man pages, but did not yet find this particular gem. In my ideal solution, I would flush only the connection in question, to avoid any perturbance of other connections.

<after a little googling ...>

I guess you mean this:
<http://www.netfilter.org/projects/conntrack/index.html>
and/or this:
<http://www.netfilter.org/projects/libnetfilter_conntrack/index.html>

I will RTF documentation, now that I see it ...

But, I wonder, is there a shortcut to the behavior I want
through iptables --ctstatus and friends?

Quoted 3 lines

Alexey

Thank you all very much for the hints so far.

Bob
_______________________________________________
LARTC mailing list
[email protected]
http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc

#5 lore raw
In reply to Bob Beers
Quoted 6 lines · Bob Beers

<after a little googling ...>

I guess you mean this:
<http://www.netfilter.org/projects/conntrack/index.html>
and/or this:
<http://www.netfilter.org/projects/libnetfilter_conntrack/index.html>

Yep.

Quoted 2 lines

But, I wonder, is there a shortcut to the behavior I want through iptables --ctstatus and friends?

Not really. Even if you match the packet with --ctstate, I don't believe there is any iptables target that would delete the connection of the current packet (and presumably drop the packet?). Even if you could, you'd still have to wait for the next packet to come along and set up a new connection entry, so there's no advantage over deleting the connection with a userspace tool, and it would be a terrible hack.

Alexey
_______________________________________________
LARTC mailing list
[email protected]
http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc