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 */
52 #define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
54 #define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
56 #define DEBUGP(fmt, args...)
60 static unsigned short ports
[MAX_PORTS
];
61 static int num_ports
= 0;
62 static unsigned int max_outstanding __read_mostly
= 8;
63 static unsigned int setup_timeout __read_mostly
= 300;
65 MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
66 MODULE_DESCRIPTION("RTSP connection tracking module");
67 MODULE_LICENSE("GPL");
68 module_param_array(ports
, ushort
, &num_ports
, 0400);
69 MODULE_PARM_DESC(ports
, "port numbers of RTSP servers");
70 module_param(max_outstanding
, uint
, 0400);
71 MODULE_PARM_DESC(max_outstanding
, "max number of outstanding SETUP requests per RTSP session");
72 module_param(setup_timeout
, uint
, 0400);
73 MODULE_PARM_DESC(setup_timeout
, "timeout on for unestablished data channels");
75 static char *rtsp_buffer
;
76 static DEFINE_SPINLOCK(rtsp_buffer_lock
);
78 static struct nf_conntrack_expect_policy rtsp_exp_policy
;
80 unsigned int (*nf_nat_rtsp_hook
)(struct sk_buff
*skb
,
81 enum ip_conntrack_info ctinfo
,
82 unsigned int matchoff
, unsigned int matchlen
,struct ip_ct_rtsp_expect
* prtspexp
,
83 struct nf_conntrack_expect
*exp
) __read_mostly
;
84 EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook
);
86 void (*nf_nat_rtsp_hook_expectfn
)(struct nf_conn
*ct
, struct nf_conntrack_expect
*exp
) __read_mostly
;
87 EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook_expectfn
);
90 * Max mappings we will allow for one RTSP connection (for RTP, the number
91 * of allocated ports is twice this value). Note that SMIL burns a lot of
92 * ports so keep this reasonably high. If this is too low, you will see a
93 * lot of "no free client map entries" messages.
95 #define MAX_PORT_MAPS 16
97 /*** default port list was here in the masq code: 554, 3030, 4040 ***/
99 #define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
102 * Parse an RTSP packet.
104 * Returns zero if parsing failed.
107 * IN ptcp tcp data pointer
108 * IN tcplen tcp data len
109 * IN/OUT ptcpoff points to current tcp offset
110 * OUT phdrsoff set to offset of rtsp headers
111 * OUT phdrslen set to length of rtsp headers
112 * OUT pcseqoff set to offset of CSeq header
113 * OUT pcseqlen set to length of CSeq header
116 rtsp_parse_message(char* ptcp
, uint tcplen
, uint
* ptcpoff
,
117 uint
* phdrsoff
, uint
* phdrslen
,
118 uint
* pcseqoff
, uint
* pcseqlen
,
119 uint
* transoff
, uint
* translen
)
125 if (!nf_nextline(ptcp
, tcplen
, ptcpoff
, &lineoff
, &linelen
))
128 *phdrsoff
= *ptcpoff
;
129 while (nf_mime_nextline(ptcp
, tcplen
, ptcpoff
, &lineoff
, &linelen
)) {
132 *ptcpoff
+= min(entitylen
, tcplen
- *ptcpoff
);
135 if (lineoff
+linelen
> tcplen
) {
136 DEBUGP("!! overrun !!\n");
140 if (nf_strncasecmp(ptcp
+lineoff
, "CSeq:", 5) == 0) {
145 if (nf_strncasecmp(ptcp
+lineoff
, "Transport:", 10) == 0) {
150 if (nf_strncasecmp(ptcp
+lineoff
, "Content-Length:", 15) == 0) {
151 uint off
= lineoff
+15;
152 SKIP_WSPACE(ptcp
+lineoff
, linelen
, off
);
153 nf_strtou32(ptcp
+off
, &entitylen
);
156 *phdrslen
= (*ptcpoff
) - (*phdrsoff
);
162 * Find lo/hi client ports (if any) in transport header
164 * ptcp, tcplen = packet
165 * tranoff, tranlen = buffer to search
168 * pport_lo, pport_hi = lo/hi ports (host endian)
170 * Returns nonzero if any client ports found
172 * Note: it is valid (and expected) for the client to request multiple
173 * transports, so we need to parse the entire line.
176 rtsp_parse_transport(char* ptran
, uint tranlen
,
177 struct ip_ct_rtsp_expect
* prtspexp
)
182 if (tranlen
< 10 || !iseol(ptran
[tranlen
-1]) ||
183 nf_strncasecmp(ptran
, "Transport:", 10) != 0) {
184 DEBUGP("sanity check failed\n");
188 DEBUGP("tran='%.*s'\n", (int)tranlen
, ptran
);
190 SKIP_WSPACE(ptran
, tranlen
, off
);
192 /* Transport: tran;field;field=val,tran;field;field=val,... */
193 while (off
< tranlen
) {
194 const char* pparamend
;
197 pparamend
= memchr(ptran
+off
, ',', tranlen
-off
);
198 pparamend
= (pparamend
== NULL
) ? ptran
+tranlen
: pparamend
+1;
199 nextparamoff
= pparamend
-ptran
;
201 while (off
< nextparamoff
) {
202 const char* pfieldend
;
205 pfieldend
= memchr(ptran
+off
, ';', nextparamoff
-off
);
206 nextfieldoff
= (pfieldend
== NULL
) ? nextparamoff
: pfieldend
-ptran
+1;
208 if (strncmp(ptran
+off
, "client_port=", 12) == 0) {
213 numlen
= nf_strtou16(ptran
+off
, &port
);
215 if (prtspexp
->loport
!= 0 && prtspexp
->loport
!= port
)
216 DEBUGP("multiple ports found, port %hu ignored\n", port
);
218 DEBUGP("lo port found : %hu\n", port
);
219 prtspexp
->loport
= prtspexp
->hiport
= port
;
220 if (ptran
[off
] == '-') {
222 numlen
= nf_strtou16(ptran
+off
, &port
);
224 prtspexp
->pbtype
= pb_range
;
225 prtspexp
->hiport
= port
;
227 // If we have a range, assume rtp:
228 // loport must be even, hiport must be loport+1
229 if ((prtspexp
->loport
& 0x0001) != 0 ||
230 prtspexp
->hiport
!= prtspexp
->loport
+1) {
231 DEBUGP("incorrect range: %hu-%hu, correcting\n",
232 prtspexp
->loport
, prtspexp
->hiport
);
233 prtspexp
->loport
&= 0xfffe;
234 prtspexp
->hiport
= prtspexp
->loport
+1;
236 } else if (ptran
[off
] == '/') {
238 numlen
= nf_strtou16(ptran
+off
, &port
);
240 prtspexp
->pbtype
= pb_discon
;
241 prtspexp
->hiport
= port
;
248 * Note we don't look for the destination parameter here.
249 * If we are using NAT, the NAT module will handle it. If not,
250 * and the client is sending packets elsewhere, the expectation
251 * will quietly time out.
263 void expected(struct nf_conn
*ct
, struct nf_conntrack_expect
*exp
)
265 typeof(nf_nat_rtsp_hook_expectfn
) nf_nat_rtsp_expectfn
;
266 nf_nat_rtsp_expectfn
= rcu_dereference(nf_nat_rtsp_hook_expectfn
);
268 if (nf_nat_rtsp_expectfn
&& ct
->master
->status
& IPS_NAT_MASK
) {
269 nf_nat_rtsp_expectfn(ct
,exp
);
273 /*** conntrack functions ***/
275 /* outbound packet: client->server */
278 help_out(struct sk_buff
*skb
, unsigned char *rb_ptr
, unsigned int datalen
,
279 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
281 struct ip_ct_rtsp_expect expinfo
;
283 int dir
= CTINFO2DIR(ctinfo
); /* = IP_CT_DIR_ORIGINAL */
284 //struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
285 //uint tcplen = pktlen - iph->ihl * 4;
286 char* pdata
= rb_ptr
;
287 //uint datalen = tcplen - tcph->doff * 4;
291 struct nf_conntrack_expect
*exp
;
292 typeof(nf_nat_rtsp_hook
) nf_nat_rtsp
;
296 memset(&expinfo
, 0, sizeof(expinfo
));
298 while (dataoff
< datalen
) {
299 uint cmdoff
= dataoff
;
308 if (!rtsp_parse_message(pdata
, datalen
, &dataoff
,
311 &transoff
, &translen
))
312 break; /* not a valid message */
314 if (strncmp(pdata
+cmdoff
, "SETUP ", 6) != 0)
315 continue; /* not a SETUP message */
316 DEBUGP("found a setup message\n");
320 rtsp_parse_transport(pdata
+transoff
, translen
, &expinfo
);
323 if (expinfo
.loport
== 0) {
324 DEBUGP("no udp transports found\n");
325 continue; /* no udp transports found */
328 DEBUGP("udp transport found, ports=(%d,%hu,%hu)\n",
329 (int)expinfo
.pbtype
, expinfo
.loport
, expinfo
.hiport
);
331 exp
= nf_conntrack_expect_alloc(ct
);
337 be_loport
= htons(expinfo
.loport
);
339 nf_conntrack_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, ct
->tuplehash
[!dir
].tuple
.src
.l3num
,
340 /* media stream source can be different from the RTSP server address */
341 // &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3,
342 NULL
, &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
343 IPPROTO_UDP
, NULL
, &be_loport
);
347 exp
->expectfn
= expected
;
350 if (expinfo
.pbtype
== pb_range
) {
351 DEBUGP("Changing expectation mask to handle multiple ports\n");
352 exp
->mask
.dst
.u
.udp
.port
= 0xfffe;
353 //exp->mask.src.u.udp.port = 0xfffe;
356 DEBUGP("expect_related %u.%u.%u.%u:%u-%u.%u.%u.%u:%u\n",
357 NIPQUAD(exp
->tuple
.src
.u3
.ip
),
358 ntohs(exp
->tuple
.src
.u
.udp
.port
),
359 NIPQUAD(exp
->tuple
.dst
.u3
.ip
),
360 ntohs(exp
->tuple
.dst
.u
.udp
.port
));
362 nf_nat_rtsp
= rcu_dereference(nf_nat_rtsp_hook
);
363 if (nf_nat_rtsp
&& ct
->status
& IPS_NAT_MASK
)
364 /* pass the request off to the nat helper */
365 ret
= nf_nat_rtsp(skb
, ctinfo
, hdrsoff
, hdrslen
, &expinfo
, exp
);
366 else if (nf_conntrack_expect_related(exp
) != 0) {
367 DEBUGP("nf_conntrack_expect_related failed\n");
370 nf_conntrack_expect_put(exp
);
380 help_in(struct sk_buff
*skb
, size_t pktlen
,
381 struct nf_conn
* ct
, enum ip_conntrack_info ctinfo
)
386 static int help(struct sk_buff
*skb
, unsigned int protoff
,
387 struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
)
389 struct tcphdr _tcph
, *th
;
390 unsigned int dataoff
, datalen
;
394 /* Until there's been traffic both ways, don't look in packets. */
395 if (ctinfo
!= IP_CT_ESTABLISHED
&&
396 ctinfo
!= IP_CT_ESTABLISHED
+ IP_CT_IS_REPLY
) {
397 DEBUGP("conntrackinfo = %u\n", ctinfo
);
401 /* Not whole TCP header? */
402 th
= skb_header_pointer(skb
, protoff
, sizeof(_tcph
), &_tcph
);
408 dataoff
= protoff
+ th
->doff
*4;
409 datalen
= skb
->len
- dataoff
;
410 if (dataoff
>= skb
->len
)
413 spin_lock_bh(&rtsp_buffer_lock
);
414 rb_ptr
= skb_header_pointer(skb
, dataoff
,
415 skb
->len
- dataoff
, rtsp_buffer
);
416 BUG_ON(rb_ptr
== NULL
);
419 /* Checksum invalid? Ignore. */
420 /* FIXME: Source route IP option packets --RR */
421 if (tcp_v4_check(tcph
, tcplen
, iph
->saddr
, iph
->daddr
,
422 csum_partial((char*)tcph
, tcplen
, 0)))
424 DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
425 tcph
, tcplen
, NIPQUAD(iph
->saddr
), NIPQUAD(iph
->daddr
));
430 switch (CTINFO2DIR(ctinfo
)) {
431 case IP_CT_DIR_ORIGINAL
:
432 ret
= help_out(skb
, rb_ptr
, datalen
, ct
, ctinfo
);
434 case IP_CT_DIR_REPLY
:
435 DEBUGP("IP_CT_DIR_REPLY\n");
436 /* inbound packet: server->client */
441 spin_unlock_bh(&rtsp_buffer_lock
);
446 static struct nf_conntrack_helper rtsp_helpers
[MAX_PORTS
] __read_mostly
;
447 static char rtsp_names
[MAX_PORTS
][sizeof("rtsp-65535")] __read_mostly
;
449 /* This function is intentionally _NOT_ defined as __exit */
454 for (i
= 0; i
< num_ports
; i
++) {
455 if (rtsp_helpers
[i
].me
== NULL
)
457 DEBUGP("unregistering port %d\n", ports
[i
]);
458 nf_conntrack_helper_unregister(&rtsp_helpers
[i
]);
467 struct nf_conntrack_helper
*hlpr
;
470 printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION
" loading\n");
472 if (max_outstanding
< 1) {
473 printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
477 rtsp_exp_policy
.max_expected
= max_outstanding
;
478 rtsp_exp_policy
.timeout
= setup_timeout
;
480 rtsp_buffer
= kmalloc(65536, GFP_KERNEL
);
484 /* If no port given, default to standard rtsp port */
486 ports
[0] = RTSP_PORT
;
489 for (i
= 0; (i
< MAX_PORTS
) && ports
[i
]; i
++) {
490 hlpr
= &rtsp_helpers
[i
];
491 memset(hlpr
, 0, sizeof(struct nf_conntrack_helper
));
492 hlpr
->tuple
.src
.l3num
= AF_INET
;
493 hlpr
->tuple
.src
.u
.tcp
.port
= htons(ports
[i
]);
494 hlpr
->tuple
.dst
.protonum
= IPPROTO_TCP
;
495 hlpr
->mask
.src
.l3num
= 0xFFFF;
496 hlpr
->mask
.src
.u
.tcp
.port
= htons(0xFFFF);
497 hlpr
->mask
.dst
.protonum
= 0xFF;
498 hlpr
->expect_policy
= &rtsp_exp_policy
;
499 hlpr
->me
= THIS_MODULE
;
502 tmpname
= &rtsp_names
[i
][0];
503 if (ports
[i
] == RTSP_PORT
) {
504 sprintf(tmpname
, "rtsp");
506 sprintf(tmpname
, "rtsp-%d", i
);
508 hlpr
->name
= tmpname
;
510 DEBUGP("port #%d: %d\n", i
, ports
[i
]);
512 ret
= nf_conntrack_helper_register(hlpr
);
515 printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports
[i
]);