2 * RTSP extension for IP connection tracking
3 * (C) 2003 by Tom Marshall <tmarshall at real.com>
4 * based on ip_conntrack_irc.c
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 * insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
13 * max_outstanding=n setup_timeout=secs
15 * If no ports are specified, the default will be port 554.
17 * With max_outstanding you can define the maximum number of not yet
18 * answered SETUP requests per RTSP session (default 8).
19 * With setup_timeout you can specify how long the system waits for
20 * an expected data channel (default 300 seconds).
22 * 2005-02-13: Harald Welte <laforge at netfilter.org>
24 * - update to recent post-2.6.11 api changes
25 * 2006-09-14: Steven Van Acker <deepstar at singularity.be>
26 * - removed calls to NAT code from conntrack helper: NAT no longer needed to use rtsp-conntrack
27 * 2007-04-18: Michael Guntsche <mike at it-loops.com>
28 * - Port to new NF API
31 #include <linux/module.h>
32 #include <linux/netfilter.h>
34 #include <linux/inet.h>
37 #include <net/netfilter/nf_conntrack.h>
38 #include <net/netfilter/nf_conntrack_expect.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <linux/netfilter/nf_conntrack_rtsp.h>
42 #define NF_NEED_STRNCASECMP
43 #define NF_NEED_STRTOU16
44 #define NF_NEED_STRTOU32
45 #define NF_NEED_NEXTLINE
46 #include <linux/netfilter_helpers.h>
47 #define NF_NEED_MIME_NEXTLINE
48 #include <linux/netfilter_mime.h>
50 #include <linux/ctype.h>
51 #define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
54 static int ports
[MAX_PORTS
];
55 static int num_ports
= 0;
56 static int max_outstanding
= 8;
57 static unsigned int setup_timeout
= 300;
59 MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
60 MODULE_DESCRIPTION("RTSP connection tracking module");
61 MODULE_LICENSE("GPL");
62 module_param_array(ports
, int, &num_ports
, 0400);
63 MODULE_PARM_DESC(ports
, "port numbers of RTSP servers");
64 module_param(max_outstanding
, int, 0400);
65 MODULE_PARM_DESC(max_outstanding
, "max number of outstanding SETUP requests per RTSP session");
66 module_param(setup_timeout
, int, 0400);
67 MODULE_PARM_DESC(setup_timeout
, "timeout on for unestablished data channels");
69 static char *rtsp_buffer
;
70 static DEFINE_SPINLOCK(rtsp_buffer_lock
);
72 static struct nf_conntrack_expect_policy rtsp_exp_policy
;
74 unsigned int (*nf_nat_rtsp_hook
)(struct sk_buff
*skb
,
75 enum ip_conntrack_info ctinfo
,
76 unsigned int matchoff
, unsigned int matchlen
,struct ip_ct_rtsp_expect
* prtspexp
,
77 struct nf_conntrack_expect
*exp
);
78 void (*nf_nat_rtsp_hook_expectfn
)(struct nf_conn
*ct
, struct nf_conntrack_expect
*exp
);
80 EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook
);
83 * Max mappings we will allow for one RTSP connection (for RTP, the number
84 * of allocated ports is twice this value). Note that SMIL burns a lot of
85 * ports so keep this reasonably high. If this is too low, you will see a
86 * lot of "no free client map entries" messages.
88 #define MAX_PORT_MAPS 16
90 /*** default port list was here in the masq code: 554, 3030, 4040 ***/
92 #define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
95 * Parse an RTSP packet.
97 * Returns zero if parsing failed.
100 * IN ptcp tcp data pointer
101 * IN tcplen tcp data len
102 * IN/OUT ptcpoff points to current tcp offset
103 * OUT phdrsoff set to offset of rtsp headers
104 * OUT phdrslen set to length of rtsp headers
105 * OUT pcseqoff set to offset of CSeq header
106 * OUT pcseqlen set to length of CSeq header
109 rtsp_parse_message(char* ptcp
, uint tcplen
, uint
* ptcpoff
,
110 uint
* phdrsoff
, uint
* phdrslen
,
111 uint
* pcseqoff
, uint
* pcseqlen
,
112 uint
* transoff
, uint
* translen
)
118 if (!nf_nextline(ptcp
, tcplen
, ptcpoff
, &lineoff
, &linelen
))
121 *phdrsoff
= *ptcpoff
;
122 while (nf_mime_nextline(ptcp
, tcplen
, ptcpoff
, &lineoff
, &linelen
)) {
125 *ptcpoff
+= min(entitylen
, tcplen
- *ptcpoff
);
128 if (lineoff
+linelen
> tcplen
) {
129 pr_info("!! overrun !!\n");
133 if (nf_strncasecmp(ptcp
+lineoff
, "CSeq:", 5) == 0) {
138 if (nf_strncasecmp(ptcp
+lineoff
, "Transport:", 10) == 0) {
143 if (nf_strncasecmp(ptcp
+lineoff
, "Content-Length:", 15) == 0) {
144 uint off
= lineoff
+15;
145 SKIP_WSPACE(ptcp
+lineoff
, linelen
, off
);
146 nf_strtou32(ptcp
+off
, &entitylen
);
149 *phdrslen
= (*ptcpoff
) - (*phdrsoff
);
155 * Find lo/hi client ports (if any) in transport header
157 * ptcp, tcplen = packet
158 * tranoff, tranlen = buffer to search
161 * pport_lo, pport_hi = lo/hi ports (host endian)
163 * Returns nonzero if any client ports found
165 * Note: it is valid (and expected) for the client to request multiple
166 * transports, so we need to parse the entire line.
169 rtsp_parse_transport(char* ptran
, uint tranlen
,
170 struct ip_ct_rtsp_expect
* prtspexp
)
175 if (tranlen
< 10 || !iseol(ptran
[tranlen
-1]) ||
176 nf_strncasecmp(ptran
, "Transport:", 10) != 0) {
177 pr_info("sanity check failed\n");
181 pr_debug("tran='%.*s'\n", (int)tranlen
, ptran
);
183 SKIP_WSPACE(ptran
, tranlen
, off
);
185 /* Transport: tran;field;field=val,tran;field;field=val,... */
186 while (off
< tranlen
) {
187 const char* pparamend
;
190 pparamend
= memchr(ptran
+off
, ',', tranlen
-off
);
191 pparamend
= (pparamend
== NULL
) ? ptran
+tranlen
: pparamend
+1;
192 nextparamoff
= pparamend
-ptran
;
194 while (off
< nextparamoff
) {
195 const char* pfieldend
;
198 pfieldend
= memchr(ptran
+off
, ';', nextparamoff
-off
);
199 nextfieldoff
= (pfieldend
== NULL
) ? nextparamoff
: pfieldend
-ptran
+1;
201 if (strncmp(ptran
+off
, "client_port=", 12) == 0) {
206 numlen
= nf_strtou16(ptran
+off
, &port
);
208 if (prtspexp
->loport
!= 0 && prtspexp
->loport
!= port
)
209 pr_debug("multiple ports found, port %hu ignored\n", port
);
211 pr_debug("lo port found : %hu\n", port
);
212 prtspexp
->loport
= prtspexp
->hiport
= port
;
213 if (ptran
[off
] == '-') {
215 numlen
= nf_strtou16(ptran
+off
, &port
);
217 prtspexp
->pbtype
= pb_range
;
218 prtspexp
->hiport
= port
;
220 // If we have a range, assume rtp:
221 // loport must be even, hiport must be loport+1
222 if ((prtspexp
->loport
& 0x0001) != 0 ||
223 prtspexp
->hiport
!= prtspexp
->loport
+1) {
224 pr_debug("incorrect range: %hu-%hu, correcting\n",
225 prtspexp
->loport
, prtspexp
->hiport
);
226 prtspexp
->loport
&= 0xfffe;
227 prtspexp
->hiport
= prtspexp
->loport
+1;
229 } else if (ptran
[off
] == '/') {
231 numlen
= nf_strtou16(ptran
+off
, &port
);
233 prtspexp
->pbtype
= pb_discon
;
234 prtspexp
->hiport
= port
;
241 * Note we don't look for the destination parameter here.
242 * If we are using NAT, the NAT module will handle it. If not,
243 * and the client is sending packets elsewhere, the expectation
244 * will quietly time out.
256 void expected(struct nf_conn
*ct
, struct nf_conntrack_expect
*exp
)
258 typeof(nf_nat_rtsp_hook_expectfn
) nf_nat_rtsp_expectfn
;
259 nf_nat_rtsp_expectfn
= rcu_dereference(nf_nat_rtsp_hook_expectfn
);
260 if(nf_nat_rtsp_expectfn
&& ct
->master
->status
& IPS_NAT_MASK
) {
261 nf_nat_rtsp_expectfn(ct
,exp
);
265 /*** conntrack functions ***/
267 /* outbound packet: client->server */
270 help_out(struct sk_buff
*skb
, unsigned char *rb_ptr
, unsigned int datalen
,
271 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
273 struct ip_ct_rtsp_expect expinfo
;
275 int dir
= CTINFO2DIR(ctinfo
); /* = IP_CT_DIR_ORIGINAL */
276 //struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
277 //uint tcplen = pktlen - iph->ihl * 4;
278 char* pdata
= rb_ptr
;
279 //uint datalen = tcplen - tcph->doff * 4;
283 struct nf_conntrack_expect
*exp
;
287 typeof(nf_nat_rtsp_hook
) nf_nat_rtsp
;
289 memset(&expinfo
, 0, sizeof(expinfo
));
291 while (dataoff
< datalen
) {
292 uint cmdoff
= dataoff
;
301 if (!rtsp_parse_message(pdata
, datalen
, &dataoff
,
304 &transoff
, &translen
))
305 break; /* not a valid message */
307 if (strncmp(pdata
+cmdoff
, "SETUP ", 6) != 0)
308 continue; /* not a SETUP message */
309 pr_debug("found a setup message\n");
313 rtsp_parse_transport(pdata
+transoff
, translen
, &expinfo
);
316 if (expinfo
.loport
== 0) {
317 pr_debug("no udp transports found\n");
318 continue; /* no udp transports found */
321 pr_debug("udp transport found, ports=(%d,%hu,%hu)\n",
322 (int)expinfo
.pbtype
, expinfo
.loport
, expinfo
.hiport
);
324 exp
= nf_ct_expect_alloc(ct
);
330 be_loport
= htons(expinfo
.loport
);
332 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, nf_ct_l3num(ct
),
333 /* media stream source can be different from the RTSP server address */
334 // &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3,
335 NULL
, &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
336 IPPROTO_UDP
, NULL
, &be_loport
);
340 exp
->expectfn
= expected
;
343 if (expinfo
.pbtype
== pb_range
) {
344 pr_debug("Changing expectation mask to handle multiple ports\n");
345 //exp->mask.dst.u.udp.port = 0xfffe;
348 pr_debug("expect_related %pI4:%u-%pI4:%u\n",
349 &exp
->tuple
.src
.u3
.ip
,
350 ntohs(exp
->tuple
.src
.u
.udp
.port
),
351 &exp
->tuple
.dst
.u3
.ip
,
352 ntohs(exp
->tuple
.dst
.u
.udp
.port
));
354 nf_nat_rtsp
= rcu_dereference(nf_nat_rtsp_hook
);
355 if (nf_nat_rtsp
&& ct
->status
& IPS_NAT_MASK
)
356 /* pass the request off to the nat helper */
357 ret
= nf_nat_rtsp(skb
, ctinfo
, hdrsoff
, hdrslen
, &expinfo
, exp
);
358 else if (nf_ct_expect_related(exp
) != 0) {
359 pr_info("nf_conntrack_expect_related failed\n");
362 nf_ct_expect_put(exp
);
372 help_in(struct sk_buff
*skb
, size_t pktlen
,
373 struct nf_conn
* ct
, enum ip_conntrack_info ctinfo
)
378 static int help(struct sk_buff
*skb
, unsigned int protoff
,
379 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
381 struct tcphdr _tcph
, *th
;
382 unsigned int dataoff
, datalen
;
386 /* Until there's been traffic both ways, don't look in packets. */
387 if (ctinfo
!= IP_CT_ESTABLISHED
&&
388 ctinfo
!= IP_CT_ESTABLISHED
+ IP_CT_IS_REPLY
) {
389 pr_debug("conntrackinfo = %u\n", ctinfo
);
393 /* Not whole TCP header? */
394 th
= skb_header_pointer(skb
,protoff
, sizeof(_tcph
), &_tcph
);
400 dataoff
= protoff
+ th
->doff
*4;
401 datalen
= skb
->len
- dataoff
;
402 if (dataoff
>= skb
->len
)
405 spin_lock_bh(&rtsp_buffer_lock
);
406 rb_ptr
= skb_header_pointer(skb
, dataoff
,
407 skb
->len
- dataoff
, rtsp_buffer
);
408 BUG_ON(rb_ptr
== NULL
);
411 /* Checksum invalid? Ignore. */
412 /* FIXME: Source route IP option packets --RR */
413 if (tcp_v4_check(tcph
, tcplen
, iph
->saddr
, iph
->daddr
,
414 csum_partial((char*)tcph
, tcplen
, 0)))
416 DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
417 tcph
, tcplen
, NIPQUAD(iph
->saddr
), NIPQUAD(iph
->daddr
));
422 switch (CTINFO2DIR(ctinfo
)) {
423 case IP_CT_DIR_ORIGINAL
:
424 ret
= help_out(skb
, rb_ptr
, datalen
, ct
, ctinfo
);
426 case IP_CT_DIR_REPLY
:
427 pr_debug("IP_CT_DIR_REPLY\n");
428 /* inbound packet: server->client */
433 spin_unlock_bh(&rtsp_buffer_lock
);
438 static struct nf_conntrack_helper rtsp_helpers
[MAX_PORTS
];
439 static char rtsp_names
[MAX_PORTS
][10];
441 /* This function is intentionally _NOT_ defined as __exit */
446 for (i
= 0; i
< num_ports
; i
++) {
447 if (rtsp_helpers
[i
].me
== NULL
)
449 pr_debug("unregistering port %d\n", ports
[i
]);
450 nf_conntrack_helper_unregister(&rtsp_helpers
[i
]);
459 struct nf_conntrack_helper
*hlpr
;
462 printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION
" loading\n");
464 if (max_outstanding
< 1) {
465 printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
468 if (setup_timeout
< 0) {
469 printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
473 rtsp_exp_policy
.max_expected
= max_outstanding
;
474 rtsp_exp_policy
.timeout
= setup_timeout
;
476 rtsp_buffer
= kmalloc(65536, GFP_KERNEL
);
480 /* If no port given, default to standard rtsp port */
482 ports
[0] = RTSP_PORT
;
485 for (i
= 0; (i
< MAX_PORTS
) && ports
[i
]; i
++) {
486 hlpr
= &rtsp_helpers
[i
];
487 memset(hlpr
, 0, sizeof(struct nf_conntrack_helper
));
488 hlpr
->tuple
.src
.l3num
= AF_INET
;
489 hlpr
->tuple
.src
.u
.tcp
.port
= htons(ports
[i
]);
490 hlpr
->tuple
.dst
.protonum
= IPPROTO_TCP
;
491 //hlpr->mask.src.u.tcp.port = 0xFFFF;
492 //hlpr->mask.dst.protonum = 0xFF;
493 hlpr
->expect_policy
= &rtsp_exp_policy
;
494 hlpr
->me
= THIS_MODULE
;
497 tmpname
= &rtsp_names
[i
][0];
498 if (ports
[i
] == RTSP_PORT
) {
499 sprintf(tmpname
, "rtsp");
501 sprintf(tmpname
, "rtsp-%d", i
);
503 hlpr
->name
= tmpname
;
505 pr_debug("port #%d: %d\n", i
, ports
[i
]);
507 ret
= nf_conntrack_helper_register(hlpr
);
510 printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports
[i
]);
522 EXPORT_SYMBOL(nf_nat_rtsp_hook_expectfn
);