K2.6 patches and update.
[tomato.git] / release / src-rt / linux / linux-2.6 / net / netfilter / nf_conntrack_rtsp.c
bloba7b518ce90d65633f3207839cf5d698167ab2759
1 /*
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.
11 * Module load syntax:
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>
23 * - port to 2.6
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>
33 #include <linux/ip.h>
34 #include <linux/inet.h>
35 #include <net/tcp.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)
53 #if 0
54 #define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
55 #else
56 #define DEBUGP(fmt, args...)
57 #endif
59 #define MAX_PORTS 8
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.
106 * Parameters:
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
115 static int
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)
121 uint entitylen = 0;
122 uint lineoff;
123 uint linelen;
125 if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
126 return 0;
128 *phdrsoff = *ptcpoff;
129 while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
130 if (linelen == 0) {
131 if (entitylen > 0)
132 *ptcpoff += min(entitylen, tcplen - *ptcpoff);
133 break;
135 if (lineoff+linelen > tcplen) {
136 DEBUGP("!! overrun !!\n");
137 break;
140 if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
141 *pcseqoff = lineoff;
142 *pcseqlen = linelen;
145 if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
146 *transoff = lineoff;
147 *translen = linelen;
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);
158 return 1;
162 * Find lo/hi client ports (if any) in transport header
163 * In:
164 * ptcp, tcplen = packet
165 * tranoff, tranlen = buffer to search
167 * Out:
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.
175 static int
176 rtsp_parse_transport(char* ptran, uint tranlen,
177 struct ip_ct_rtsp_expect* prtspexp)
179 int rc = 0;
180 uint off = 0;
182 if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
183 nf_strncasecmp(ptran, "Transport:", 10) != 0) {
184 DEBUGP("sanity check failed\n");
185 return 0;
188 DEBUGP("tran='%.*s'\n", (int)tranlen, ptran);
189 off += 10;
190 SKIP_WSPACE(ptran, tranlen, off);
192 /* Transport: tran;field;field=val,tran;field;field=val,... */
193 while (off < tranlen) {
194 const char* pparamend;
195 uint nextparamoff;
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;
203 uint nextfieldoff;
205 pfieldend = memchr(ptran+off, ';', nextparamoff-off);
206 nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
208 if (strncmp(ptran+off, "client_port=", 12) == 0) {
209 u_int16_t port;
210 uint numlen;
212 off += 12;
213 numlen = nf_strtou16(ptran+off, &port);
214 off += numlen;
215 if (prtspexp->loport != 0 && prtspexp->loport != port)
216 DEBUGP("multiple ports found, port %hu ignored\n", port);
217 else {
218 DEBUGP("lo port found : %hu\n", port);
219 prtspexp->loport = prtspexp->hiport = port;
220 if (ptran[off] == '-') {
221 off++;
222 numlen = nf_strtou16(ptran+off, &port);
223 off += numlen;
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] == '/') {
237 off++;
238 numlen = nf_strtou16(ptran+off, &port);
239 off += numlen;
240 prtspexp->pbtype = pb_discon;
241 prtspexp->hiport = port;
243 rc = 1;
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.
254 off = nextfieldoff;
257 off = nextparamoff;
260 return rc;
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 */
277 static inline int
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;
288 uint dataoff = 0;
289 int ret = NF_ACCEPT;
291 struct nf_conntrack_expect *exp;
292 typeof(nf_nat_rtsp_hook) nf_nat_rtsp;
294 __be16 be_loport;
296 memset(&expinfo, 0, sizeof(expinfo));
298 while (dataoff < datalen) {
299 uint cmdoff = dataoff;
300 uint hdrsoff = 0;
301 uint hdrslen = 0;
302 uint cseqoff = 0;
303 uint cseqlen = 0;
304 uint transoff = 0;
305 uint translen = 0;
306 uint off;
308 if (!rtsp_parse_message(pdata, datalen, &dataoff,
309 &hdrsoff, &hdrslen,
310 &cseqoff, &cseqlen,
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");
318 off = 0;
319 if (translen) {
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);
332 if (!exp) {
333 ret = NF_DROP;
334 goto out;
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);
345 exp->master = ct;
347 exp->expectfn = expected;
348 exp->flags = 0;
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");
368 ret = NF_DROP;
370 nf_conntrack_expect_put(exp);
371 goto out;
373 out:
375 return ret;
379 static inline int
380 help_in(struct sk_buff *skb, size_t pktlen,
381 struct nf_conn* ct, enum ip_conntrack_info ctinfo)
383 return NF_ACCEPT;
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;
391 char *rb_ptr;
392 int ret = NF_DROP;
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);
398 return NF_ACCEPT;
401 /* Not whole TCP header? */
402 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
404 if (!th)
405 return NF_ACCEPT;
407 /* No data ? */
408 dataoff = protoff + th->doff*4;
409 datalen = skb->len - dataoff;
410 if (dataoff >= skb->len)
411 return NF_ACCEPT;
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);
418 #if 0
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));
426 return NF_ACCEPT;
428 #endif
430 switch (CTINFO2DIR(ctinfo)) {
431 case IP_CT_DIR_ORIGINAL:
432 ret = help_out(skb, rb_ptr, datalen, ct, ctinfo);
433 break;
434 case IP_CT_DIR_REPLY:
435 DEBUGP("IP_CT_DIR_REPLY\n");
436 /* inbound packet: server->client */
437 ret = NF_ACCEPT;
438 break;
441 spin_unlock_bh(&rtsp_buffer_lock);
443 return ret;
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 */
450 static void
451 fini(void)
453 int i;
454 for (i = 0; i < num_ports; i++) {
455 if (rtsp_helpers[i].me == NULL)
456 continue;
457 DEBUGP("unregistering port %d\n", ports[i]);
458 nf_conntrack_helper_unregister(&rtsp_helpers[i]);
460 kfree(rtsp_buffer);
463 static int __init
464 init(void)
466 int i, ret;
467 struct nf_conntrack_helper *hlpr;
468 char *tmpname;
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");
474 return -EBUSY;
477 rtsp_exp_policy.max_expected = max_outstanding;
478 rtsp_exp_policy.timeout = setup_timeout;
480 rtsp_buffer = kmalloc(65536, GFP_KERNEL);
481 if (!rtsp_buffer)
482 return -ENOMEM;
484 /* If no port given, default to standard rtsp port */
485 if (ports[0] == 0) {
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;
500 hlpr->help = help;
502 tmpname = &rtsp_names[i][0];
503 if (ports[i] == RTSP_PORT) {
504 sprintf(tmpname, "rtsp");
505 } else {
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);
514 if (ret) {
515 printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
516 fini();
517 return -EBUSY;
519 num_ports++;
521 return 0;
524 module_init(init);
525 module_exit(fini);