Skip to contentSkip to conversation

dynamic rule for bi-directional udp conversation

DISCUSSION Shen Rong 2 participants 2 messages Sep 28, 2003 – Sep 29, 2003 netfilter

lore
Share & tools
lore

Shen Rong

author
#0 lore raw

Hi,

Sorry for long mail.:)

We use dnat & snat to dynamically maintain the
RTP stream between the hosts from the internal network and external network. When the two parties' IP address and UDP port is determined, our application will create dnat & snat rules dynamically to masquerade the internal IP address. Just like the following pic.

Host A       ------   gateway  -----    Host B
192.168.0.2  192.168.0.1   1.1.1.1      1.1.1.2

Say A will use the UDP 10000 as it's receiving port, and use UDP 10001 as it's sending port(source port). B will use the UDP 20000/20001 as it's receiving/sending port. Since the RTP stream is bi-directional, we should dynamically create two rules for the conversation:

iptables -t nat -A PREROUTING -d 1.1.1.1 -p udp \
         --destination-port 50000 -j DNAT \
         --to-destination 192.168.0.2:10000
iptables -t nat -A POSTROUTING -d 1.1.1.2 -p udp \
         --destination-port 20000 -j SNAT \
         --to-source 1.1.1.1:50000

We create the above two rules only with the dest. information as match since we only know these information when creating the rules. As you see, we want to use only one port (50000) as the receiving/sending port on the gateway whatever A and B use.

The problem is if the data packets come from the outside first, they will match the DNAT rule, like that:

original pkt: dest. 1.1.1.1:50000       src 1.1.1.2:20001
DNATed pkt:   dest. 192.168.0.2:10000   src 1.1.1.2:20001

IIRC, the packets will leads to the following conntrack:

original:   dest. 1.1.1.1:50000   src 1.1.1.2:20001
reply:      dest. 1.1.1.2:20001   src 192.168.0.2:10000

But the packets from the internal network will use 10001 as source port, so they won't match this conntrack, thus the IP Conntrack will create another conntrack for them. It sounds ok, but in fact, after the internal packets match the SNAT rule, they are all dropped, since the NAT code can't find unique tuple (50000 is already used by the first conntrack) for them as the SNAT rule says. Though I could use another port (say 50001) to create the SNAT rule, but it will leads to two un-replied conntrack, which may be filtered by stateful filter rule.

So far, I don't want to write a helper/expecter module for our application since there are already ALG there. So, is there any other solution for my problem? Thanks a lot.

--
Shenrong

#1 lore raw
Quoted 7 lines · Shen Rong

Hi,

Sorry for long mail.:)

We use dnat & snat to dynamically maintain the
RTP stream between the hosts from the internal
network and external network.

What is your control protocol? RTSP, SIP, or other?

Signature · 4 lines
More Wartime News: Bush denies reports that he hired psychic Miss Cleo to
channel Alexander the Great. "I did not have psychic relations with Aunt
Jemima," said chief executive.
        -- Tatsuya Ishida, http://sinfest.net/