1 Index: linux-2.6.23-rc9/include/linux/netfilter/nf_conntrack_rtsp.h
2 ===================================================================
3 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4 +++ linux-2.6.23-rc9/include/linux/netfilter/nf_conntrack_rtsp.h 2007-10-05 21:26:39.000000000 +0800
7 + * RTSP extension for IP connection tracking.
8 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
9 + * based on ip_conntrack_irc.h
11 + * This program is free software; you can redistribute it and/or
12 + * modify it under the terms of the GNU General Public License
13 + * as published by the Free Software Foundation; either version
14 + * 2 of the License, or (at your option) any later version.
16 +#ifndef _IP_CONNTRACK_RTSP_H
17 +#define _IP_CONNTRACK_RTSP_H
19 +//#define IP_NF_RTSP_DEBUG 1
20 +#define IP_NF_RTSP_VERSION "0.6.21"
23 +/* port block types */
25 + pb_single, /* client_port=x */
26 + pb_range, /* client_port=x-y */
27 + pb_discon /* client_port=x/y (rtspbis) */
30 +/* We record seq number and length of rtsp headers here, all in host order. */
33 + * This structure is per expected connection. It is a member of struct
34 + * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
35 + * there and we are expected to only store the length of the data which
36 + * needs replaced. If a packet contains multiple RTSP messages, we create
37 + * one expected connection per message.
39 + * We use these variables to mark the entire header block. This may seem
40 + * like overkill, but the nature of RTSP requires it. A header may appear
41 + * multiple times in a message. We must treat two Transport headers the
42 + * same as one Transport header with two entries.
44 +struct ip_ct_rtsp_expect
46 + u_int32_t len; /* length of header block */
47 + portblock_t pbtype; /* Type of port block that was requested */
48 + u_int16_t loport; /* Port that was requested, low or first */
49 + u_int16_t hiport; /* Port that was requested, high or second */
51 + uint method; /* RTSP method */
52 + uint cseq; /* CSeq from request */
56 +extern unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
57 + enum ip_conntrack_info ctinfo,
58 + unsigned int matchoff, unsigned int matchlen,
59 + struct ip_ct_rtsp_expect *prtspexp,
60 + struct nf_conntrack_expect *exp);
62 +extern void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
64 +#define RTSP_PORT 554
66 +#endif /* __KERNEL__ */
68 +#endif /* _IP_CONNTRACK_RTSP_H */
69 Index: linux-2.6.23-rc9/include/linux/netfilter_helpers.h
70 ===================================================================
71 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
72 +++ linux-2.6.23-rc9/include/linux/netfilter_helpers.h 2007-10-05 21:26:39.000000000 +0800
75 + * Helpers for netfiler modules. This file provides implementations for basic
76 + * functions such as strncasecmp(), etc.
78 + * gcc will warn for defined but unused functions, so we only include the
79 + * functions requested. The following macros are used:
80 + * NF_NEED_STRNCASECMP nf_strncasecmp()
81 + * NF_NEED_STRTOU16 nf_strtou16()
82 + * NF_NEED_STRTOU32 nf_strtou32()
84 +#ifndef _NETFILTER_HELPERS_H
85 +#define _NETFILTER_HELPERS_H
87 +/* Only include these functions for kernel code. */
90 +#include <linux/ctype.h>
91 +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
94 + * The standard strncasecmp()
96 +#ifdef NF_NEED_STRNCASECMP
98 +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
100 + if (s1 == NULL || s2 == NULL)
102 + if (s1 == NULL && s2 == NULL)
106 + return (s1 == NULL) ? -1 : 1;
108 + while (len > 0 && tolower(*s1) == tolower(*s2))
114 + return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
116 +#endif /* NF_NEED_STRNCASECMP */
119 + * Parse a string containing a 16-bit unsigned integer.
120 + * Returns the number of chars used, or zero if no number is found.
122 +#ifdef NF_NEED_STRTOU16
124 +nf_strtou16(const char* pbuf, u_int16_t* pval)
129 + while (isdigit(pbuf[n]))
131 + *pval = (*pval * 10) + (pbuf[n] - '0');
137 +#endif /* NF_NEED_STRTOU16 */
140 + * Parse a string containing a 32-bit unsigned integer.
141 + * Returns the number of chars used, or zero if no number is found.
143 +#ifdef NF_NEED_STRTOU32
145 +nf_strtou32(const char* pbuf, u_int32_t* pval)
150 + while (pbuf[n] >= '0' && pbuf[n] <= '9')
152 + *pval = (*pval * 10) + (pbuf[n] - '0');
158 +#endif /* NF_NEED_STRTOU32 */
161 + * Given a buffer and length, advance to the next line and mark the current
164 +#ifdef NF_NEED_NEXTLINE
166 +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
176 + while (p[off] != '\n')
187 + /* if we saw a crlf, physlen needs adjusted */
188 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
193 + /* advance past the newline */
197 + *plinelen = physlen;
202 +#endif /* NF_NEED_NEXTLINE */
204 +#endif /* __KERNEL__ */
206 +#endif /* _NETFILTER_HELPERS_H */
207 Index: linux-2.6.23-rc9/include/linux/netfilter_mime.h
208 ===================================================================
209 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
210 +++ linux-2.6.23-rc9/include/linux/netfilter_mime.h 2007-10-05 21:26:39.000000000 +0800
213 + * MIME functions for netfilter modules. This file provides implementations
214 + * for basic MIME parsing. MIME headers are used in many protocols, such as
215 + * HTTP, RTSP, SIP, etc.
217 + * gcc will warn for defined but unused functions, so we only include the
218 + * functions requested. The following macros are used:
219 + * NF_NEED_MIME_NEXTLINE nf_mime_nextline()
221 +#ifndef _NETFILTER_MIME_H
222 +#define _NETFILTER_MIME_H
224 +/* Only include these functions for kernel code. */
227 +#include <linux/ctype.h>
230 + * Given a buffer and length, advance to the next line and mark the current
231 + * line. If the current line is empty, *plinelen will be set to zero. If
232 + * not, it will be set to the actual line length (including CRLF).
234 + * 'line' in this context means logical line (includes LWS continuations).
235 + * Returns 1 on success, 0 on failure.
237 +#ifdef NF_NEED_MIME_NEXTLINE
239 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
243 + int is_first_line = 1;
252 + while (p[off] != '\n')
263 + /* if we saw a crlf, physlen needs adjusted */
264 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
269 + /* advance past the newline */
272 + /* check for an empty line */
278 + /* check for colon on the first physical line */
282 + if (memchr(p+(*poff), ':', physlen) == NULL)
288 + while (p[off] == ' ' || p[off] == '\t');
291 + *plinelen = (physlen == 0) ? 0 : (off - *poff);
296 +#endif /* NF_NEED_MIME_NEXTLINE */
298 +#endif /* __KERNEL__ */
300 +#endif /* _NETFILTER_MIME_H */
301 Index: linux-2.6.23-rc9/net/ipv4/netfilter/Makefile
302 ===================================================================
303 --- linux-2.6.23-rc9.orig/net/ipv4/netfilter/Makefile 2007-10-05 21:26:39.000000000 +0800
304 +++ linux-2.6.23-rc9/net/ipv4/netfilter/Makefile 2007-10-05 21:26:39.000000000 +0800
306 obj-$(CONFIG_NF_NAT_FTP) += nf_nat_ftp.o
307 obj-$(CONFIG_NF_NAT_H323) += nf_nat_h323.o
308 obj-$(CONFIG_NF_NAT_IRC) += nf_nat_irc.o
309 +obj-$(CONFIG_NF_NAT_RTSP) += nf_nat_rtsp.o
310 obj-$(CONFIG_NF_NAT_PPTP) += nf_nat_pptp.o
311 obj-$(CONFIG_NF_NAT_SIP) += nf_nat_sip.o
312 obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o
313 Index: linux-2.6.23-rc9/net/netfilter/Kconfig
314 ===================================================================
315 --- linux-2.6.23-rc9.orig/net/netfilter/Kconfig 2007-10-05 21:26:39.000000000 +0800
316 +++ linux-2.6.23-rc9/net/netfilter/Kconfig 2007-10-05 21:26:39.000000000 +0800
319 To compile it as a module, choose M here. If unsure, say N.
321 +config NF_CONNTRACK_RTSP
322 + tristate "RTSP protocol support"
323 + depends on NF_CONNTRACK
325 + Support the RTSP protocol. This allows UDP transports to be setup
326 + properly, including RTP and RDT.
328 + If you want to compile it as a module, say 'M' here and read
329 + Documentation/modules.txt. If unsure, say 'Y'.
332 tristate 'Connection tracking netlink interface (EXPERIMENTAL)'
333 depends on EXPERIMENTAL && NF_CONNTRACK && NETFILTER_NETLINK
334 Index: linux-2.6.23-rc9/net/netfilter/Makefile
335 ===================================================================
336 --- linux-2.6.23-rc9.orig/net/netfilter/Makefile 2007-10-05 21:26:39.000000000 +0800
337 +++ linux-2.6.23-rc9/net/netfilter/Makefile 2007-10-05 21:26:39.000000000 +0800
339 obj-$(CONFIG_NF_CONNTRACK_SANE) += nf_conntrack_sane.o
340 obj-$(CONFIG_NF_CONNTRACK_SIP) += nf_conntrack_sip.o
341 obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o
342 +obj-$(CONFIG_NF_CONNTRACK_RTSP) += nf_conntrack_rtsp.o
345 obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o
346 Index: linux-2.6.23-rc9/net/ipv4/netfilter/Kconfig
347 ===================================================================
348 --- linux-2.6.23-rc9.orig/net/ipv4/netfilter/Kconfig 2007-10-05 21:26:39.000000000 +0800
349 +++ linux-2.6.23-rc9/net/ipv4/netfilter/Kconfig 2007-10-05 21:26:39.000000000 +0800
351 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
352 default NF_NAT && NF_CONNTRACK_IRC
356 + depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
357 + default NF_NAT && NF_CONNTRACK_RTSP
361 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
362 Index: linux-2.6.23-rc9/net/netfilter/nf_conntrack_rtsp.c
363 ===================================================================
364 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
365 +++ linux-2.6.23-rc9/net/netfilter/nf_conntrack_rtsp.c 2007-10-05 21:46:43.000000000 +0800
368 + * RTSP extension for IP connection tracking
369 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
370 + * based on ip_conntrack_irc.c
372 + * This program is free software; you can redistribute it and/or
373 + * modify it under the terms of the GNU General Public License
374 + * as published by the Free Software Foundation; either version
375 + * 2 of the License, or (at your option) any later version.
377 + * Module load syntax:
378 + * insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
379 + * max_outstanding=n setup_timeout=secs
381 + * If no ports are specified, the default will be port 554.
383 + * With max_outstanding you can define the maximum number of not yet
384 + * answered SETUP requests per RTSP session (default 8).
385 + * With setup_timeout you can specify how long the system waits for
386 + * an expected data channel (default 300 seconds).
388 + * 2005-02-13: Harald Welte <laforge at netfilter.org>
390 + * - update to recent post-2.6.11 api changes
391 + * 2006-09-14: Steven Van Acker <deepstar at singularity.be>
392 + * - removed calls to NAT code from conntrack helper: NAT no longer needed to use rtsp-conntrack
393 + * 2007-04-18: Michael Guntsche <mike at it-loops.com>
394 + * - Port to new NF API
397 +#include <linux/module.h>
398 +#include <linux/netfilter.h>
399 +#include <linux/ip.h>
400 +#include <linux/inet.h>
401 +#include <net/tcp.h>
403 +#include <net/netfilter/nf_conntrack.h>
404 +#include <net/netfilter/nf_conntrack_expect.h>
405 +#include <net/netfilter/nf_conntrack_helper.h>
406 +#include <linux/netfilter/nf_conntrack_rtsp.h>
408 +#define NF_NEED_STRNCASECMP
409 +#define NF_NEED_STRTOU16
410 +#define NF_NEED_STRTOU32
411 +#define NF_NEED_NEXTLINE
412 +#include <linux/netfilter_helpers.h>
413 +#define NF_NEED_MIME_NEXTLINE
414 +#include <linux/netfilter_mime.h>
416 +#include <linux/ctype.h>
417 +#define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
418 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
420 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
422 +#define DEBUGP(fmt, args...)
426 +static int ports[MAX_PORTS];
427 +static int num_ports = 0;
428 +static int max_outstanding = 8;
429 +static unsigned int setup_timeout = 300;
431 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
432 +MODULE_DESCRIPTION("RTSP connection tracking module");
433 +MODULE_LICENSE("GPL");
434 +module_param_array(ports, int, &num_ports, 0400);
435 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
436 +module_param(max_outstanding, int, 0400);
437 +MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
438 +module_param(setup_timeout, int, 0400);
439 +MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
441 +static char *rtsp_buffer;
442 +static DEFINE_SPINLOCK(rtsp_buffer_lock);
444 +unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
445 + enum ip_conntrack_info ctinfo,
446 + unsigned int matchoff, unsigned int matchlen,struct ip_ct_rtsp_expect* prtspexp,
447 + struct nf_conntrack_expect *exp);
448 +void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
450 +EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook);
453 + * Max mappings we will allow for one RTSP connection (for RTP, the number
454 + * of allocated ports is twice this value). Note that SMIL burns a lot of
455 + * ports so keep this reasonably high. If this is too low, you will see a
456 + * lot of "no free client map entries" messages.
458 +#define MAX_PORT_MAPS 16
460 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
462 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
465 + * Parse an RTSP packet.
467 + * Returns zero if parsing failed.
470 + * IN ptcp tcp data pointer
471 + * IN tcplen tcp data len
472 + * IN/OUT ptcpoff points to current tcp offset
473 + * OUT phdrsoff set to offset of rtsp headers
474 + * OUT phdrslen set to length of rtsp headers
475 + * OUT pcseqoff set to offset of CSeq header
476 + * OUT pcseqlen set to length of CSeq header
479 +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
480 + uint* phdrsoff, uint* phdrslen,
481 + uint* pcseqoff, uint* pcseqlen,
482 + uint* transoff, uint* translen)
484 + uint entitylen = 0;
488 + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
491 + *phdrsoff = *ptcpoff;
492 + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
493 + if (linelen == 0) {
495 + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
498 + if (lineoff+linelen > tcplen) {
499 + INFOP("!! overrun !!\n");
503 + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
504 + *pcseqoff = lineoff;
505 + *pcseqlen = linelen;
508 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
509 + *transoff = lineoff;
510 + *translen = linelen;
513 + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0) {
514 + uint off = lineoff+15;
515 + SKIP_WSPACE(ptcp+lineoff, linelen, off);
516 + nf_strtou32(ptcp+off, &entitylen);
519 + *phdrslen = (*ptcpoff) - (*phdrsoff);
525 + * Find lo/hi client ports (if any) in transport header
527 + * ptcp, tcplen = packet
528 + * tranoff, tranlen = buffer to search
531 + * pport_lo, pport_hi = lo/hi ports (host endian)
533 + * Returns nonzero if any client ports found
535 + * Note: it is valid (and expected) for the client to request multiple
536 + * transports, so we need to parse the entire line.
539 +rtsp_parse_transport(char* ptran, uint tranlen,
540 + struct ip_ct_rtsp_expect* prtspexp)
545 + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
546 + nf_strncasecmp(ptran, "Transport:", 10) != 0) {
547 + INFOP("sanity check failed\n");
551 + DEBUGP("tran='%.*s'\n", (int)tranlen, ptran);
553 + SKIP_WSPACE(ptran, tranlen, off);
555 + /* Transport: tran;field;field=val,tran;field;field=val,... */
556 + while (off < tranlen) {
557 + const char* pparamend;
560 + pparamend = memchr(ptran+off, ',', tranlen-off);
561 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
562 + nextparamoff = pparamend-ptran;
564 + while (off < nextparamoff) {
565 + const char* pfieldend;
568 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
569 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
571 + if (strncmp(ptran+off, "client_port=", 12) == 0) {
576 + numlen = nf_strtou16(ptran+off, &port);
578 + if (prtspexp->loport != 0 && prtspexp->loport != port)
579 + DEBUGP("multiple ports found, port %hu ignored\n", port);
581 + DEBUGP("lo port found : %hu\n", port);
582 + prtspexp->loport = prtspexp->hiport = port;
583 + if (ptran[off] == '-') {
585 + numlen = nf_strtou16(ptran+off, &port);
587 + prtspexp->pbtype = pb_range;
588 + prtspexp->hiport = port;
590 + // If we have a range, assume rtp:
591 + // loport must be even, hiport must be loport+1
592 + if ((prtspexp->loport & 0x0001) != 0 ||
593 + prtspexp->hiport != prtspexp->loport+1) {
594 + DEBUGP("incorrect range: %hu-%hu, correcting\n",
595 + prtspexp->loport, prtspexp->hiport);
596 + prtspexp->loport &= 0xfffe;
597 + prtspexp->hiport = prtspexp->loport+1;
599 + } else if (ptran[off] == '/') {
601 + numlen = nf_strtou16(ptran+off, &port);
603 + prtspexp->pbtype = pb_discon;
604 + prtspexp->hiport = port;
611 + * Note we don't look for the destination parameter here.
612 + * If we are using NAT, the NAT module will handle it. If not,
613 + * and the client is sending packets elsewhere, the expectation
614 + * will quietly time out.
617 + off = nextfieldoff;
620 + off = nextparamoff;
626 +void expected(struct nf_conn *ct, struct nf_conntrack_expect *exp)
628 + if(nf_nat_rtsp_hook_expectfn) {
629 + nf_nat_rtsp_hook_expectfn(ct,exp);
633 +/*** conntrack functions ***/
635 +/* outbound packet: client->server */
638 +help_out(struct sk_buff *skb, unsigned char *rb_ptr, unsigned int datalen,
639 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
641 + struct ip_ct_rtsp_expect expinfo;
643 + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
644 + //struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
645 + //uint tcplen = pktlen - iph->ihl * 4;
646 + char* pdata = rb_ptr;
647 + //uint datalen = tcplen - tcph->doff * 4;
649 + int ret = NF_ACCEPT;
651 + struct nf_conntrack_expect *exp;
655 + memset(&expinfo, 0, sizeof(expinfo));
657 + while (dataoff < datalen) {
658 + uint cmdoff = dataoff;
667 + if (!rtsp_parse_message(pdata, datalen, &dataoff,
668 + &hdrsoff, &hdrslen,
669 + &cseqoff, &cseqlen,
670 + &transoff, &translen))
671 + break; /* not a valid message */
673 + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
674 + continue; /* not a SETUP message */
675 + DEBUGP("found a setup message\n");
679 + rtsp_parse_transport(pdata+transoff, translen, &expinfo);
682 + if (expinfo.loport == 0) {
683 + DEBUGP("no udp transports found\n");
684 + continue; /* no udp transports found */
687 + DEBUGP("udp transport found, ports=(%d,%hu,%hu)\n",
688 + (int)expinfo.pbtype, expinfo.loport, expinfo.hiport);
690 + exp = nf_ct_expect_alloc(ct);
696 + be_loport = htons(expinfo.loport);
698 + nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
699 + &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3,
700 + IPPROTO_UDP, NULL, &be_loport);
704 + exp->expectfn = expected;
707 + if (expinfo.pbtype == pb_range) {
708 + DEBUGP("Changing expectation mask to handle multiple ports\n");
709 + exp->mask.src.u.udp.port = 0xfffe;
712 + DEBUGP("expect_related %u.%u.%u.%u:%u-%u.%u.%u.%u:%u\n",
713 + NIPQUAD(exp->tuple.src.u3.ip),
714 + ntohs(exp->tuple.src.u.udp.port),
715 + NIPQUAD(exp->tuple.dst.u3.ip),
716 + ntohs(exp->tuple.dst.u.udp.port));
718 + if (nf_nat_rtsp_hook)
719 + /* pass the request off to the nat helper */
720 + ret = nf_nat_rtsp_hook(skb, ctinfo, hdrsoff, hdrslen, &expinfo, exp);
721 + else if (nf_ct_expect_related(exp) != 0) {
722 + INFOP("nf_ct_expect_related failed\n");
725 + nf_ct_expect_put(exp);
735 +help_in(struct sk_buff *skb, size_t pktlen,
736 + struct nf_conn* ct, enum ip_conntrack_info ctinfo)
741 +static int help(struct sk_buff *skb, unsigned int protoff,
742 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
744 + struct tcphdr _tcph, *th;
745 + unsigned int dataoff, datalen;
749 + /* Until there's been traffic both ways, don't look in packets. */
750 + if (ctinfo != IP_CT_ESTABLISHED &&
751 + ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
752 + DEBUGP("conntrackinfo = %u\n", ctinfo);
756 + /* Not whole TCP header? */
757 + th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
763 + dataoff = protoff + th->doff*4;
764 + datalen = skb->len - dataoff;
765 + if (dataoff >= skb->len)
768 + spin_lock_bh(&rtsp_buffer_lock);
769 + rb_ptr = skb_header_pointer(skb, dataoff,
770 + skb->len - dataoff, rtsp_buffer);
771 + BUG_ON(rb_ptr == NULL);
774 + /* Checksum invalid? Ignore. */
775 + /* FIXME: Source route IP option packets --RR */
776 + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
777 + csum_partial((char*)tcph, tcplen, 0)))
779 + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
780 + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
785 + switch (CTINFO2DIR(ctinfo)) {
786 + case IP_CT_DIR_ORIGINAL:
787 + ret = help_out(skb, rb_ptr, datalen, ct, ctinfo);
789 + case IP_CT_DIR_REPLY:
790 + DEBUGP("IP_CT_DIR_REPLY\n");
791 + /* inbound packet: server->client */
796 + spin_unlock_bh(&rtsp_buffer_lock);
801 +static struct nf_conntrack_helper rtsp_helpers[MAX_PORTS];
802 +static char rtsp_names[MAX_PORTS][10];
804 +/* This function is intentionally _NOT_ defined as __exit */
809 + for (i = 0; i < num_ports; i++) {
810 + DEBUGP("unregistering port %d\n", ports[i]);
811 + nf_conntrack_helper_unregister(&rtsp_helpers[i]);
813 + kfree(rtsp_buffer);
820 + struct nf_conntrack_helper *hlpr;
823 + printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
825 + if (max_outstanding < 1) {
826 + printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
829 + if (setup_timeout < 0) {
830 + printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
834 + rtsp_buffer = kmalloc(65536, GFP_KERNEL);
838 + /* If no port given, default to standard rtsp port */
839 + if (ports[0] == 0) {
840 + ports[0] = RTSP_PORT;
843 + for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
844 + hlpr = &rtsp_helpers[i];
845 + memset(hlpr, 0, sizeof(struct nf_conntrack_helper));
846 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
847 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
848 + hlpr->max_expected = max_outstanding;
849 + hlpr->timeout = setup_timeout;
850 + hlpr->me = THIS_MODULE;
853 + tmpname = &rtsp_names[i][0];
854 + if (ports[i] == RTSP_PORT) {
855 + sprintf(tmpname, "rtsp");
857 + sprintf(tmpname, "rtsp-%d", i);
859 + hlpr->name = tmpname;
861 + DEBUGP("port #%d: %d\n", i, ports[i]);
863 + ret = nf_conntrack_helper_register(hlpr);
866 + printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
878 +EXPORT_SYMBOL(nf_nat_rtsp_hook_expectfn);
880 Index: linux-2.6.23-rc9/net/ipv4/netfilter/nf_nat_rtsp.c
881 ===================================================================
882 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
883 +++ linux-2.6.23-rc9/net/ipv4/netfilter/nf_nat_rtsp.c 2007-10-05 21:41:16.000000000 +0800
886 + * RTSP extension for TCP NAT alteration
887 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
888 + * based on ip_nat_irc.c
890 + * This program is free software; you can redistribute it and/or
891 + * modify it under the terms of the GNU General Public License
892 + * as published by the Free Software Foundation; either version
893 + * 2 of the License, or (at your option) any later version.
895 + * Module load syntax:
896 + * insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
897 + * stunaddr=<address>
898 + * destaction=[auto|strip|none]
900 + * If no ports are specified, the default will be port 554 only.
902 + * stunaddr specifies the address used to detect that a client is using STUN.
903 + * If this address is seen in the destination parameter, it is assumed that
904 + * the client has already punched a UDP hole in the firewall, so we don't
905 + * mangle the client_port. If none is specified, it is autodetected. It
906 + * only needs to be set if you have multiple levels of NAT. It should be
907 + * set to the external address that the STUN clients detect. Note that in
908 + * this case, it will not be possible for clients to use UDP with servers
909 + * between the NATs.
911 + * If no destaction is specified, auto is used.
912 + * destaction=auto: strip destination parameter if it is not stunaddr.
913 + * destaction=strip: always strip destination parameter (not recommended).
914 + * destaction=none: do not touch destination parameter (not recommended).
917 +#include <linux/module.h>
918 +#include <net/tcp.h>
919 +#include <net/netfilter/nf_nat_helper.h>
920 +#include <net/netfilter/nf_nat_rule.h>
921 +#include <linux/netfilter/nf_conntrack_rtsp.h>
922 +#include <net/netfilter/nf_conntrack_expect.h>
924 +#include <linux/inet.h>
925 +#include <linux/ctype.h>
926 +#define NF_NEED_STRNCASECMP
927 +#define NF_NEED_STRTOU16
928 +#include <linux/netfilter_helpers.h>
929 +#define NF_NEED_MIME_NEXTLINE
930 +#include <linux/netfilter_mime.h>
932 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
934 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
936 +#define DEBUGP(fmt, args...)
940 +#define DSTACT_AUTO 0
941 +#define DSTACT_STRIP 1
942 +#define DSTACT_NONE 2
944 +static char* stunaddr = NULL;
945 +static char* destaction = NULL;
947 +static u_int32_t extip = 0;
948 +static int dstact = 0;
950 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
951 +MODULE_DESCRIPTION("RTSP network address translation module");
952 +MODULE_LICENSE("GPL");
953 +module_param(stunaddr, charp, 0644);
954 +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
955 +module_param(destaction, charp, 0644);
956 +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
958 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
960 +/*** helper functions ***/
963 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
965 + struct iphdr* iph = ip_hdr(skb);
966 + struct tcphdr* tcph = (void *)iph + ip_hdrlen(skb);
968 + *pptcpdata = (char*)tcph + tcph->doff*4;
969 + *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
972 +/*** nat functions ***/
975 + * Mangle the "Transport:" header:
976 + * - Replace all occurences of "client_port=<spec>"
977 + * - Handle destination parameter
980 + * ct, ctinfo = conntrack context
982 + * tranoff = Transport header offset from TCP data
983 + * tranlen = Transport header length (incl. CRLF)
984 + * rport_lo = replacement low port (host endian)
985 + * rport_hi = replacement high port (host endian)
987 + * Returns packet size difference.
989 + * Assumes that a complete transport header is present, ending with CR or LF
992 +rtsp_mangle_tran(enum ip_conntrack_info ctinfo,
993 + struct nf_conntrack_expect* exp,
994 + struct ip_ct_rtsp_expect* prtspexp,
995 + struct sk_buff* skb, uint tranoff, uint tranlen)
1000 + char rbuf1[16]; /* Replacement buffer (one port) */
1001 + uint rbuf1len; /* Replacement len (one port) */
1002 + char rbufa[16]; /* Replacement buffer (all ports) */
1003 + uint rbufalen; /* Replacement len (all ports) */
1005 + u_int16_t loport, hiport;
1007 + uint diff; /* Number of bytes we removed */
1009 + struct nf_conn *ct = exp->master;
1010 + struct nf_conntrack_tuple *t;
1012 + char szextaddr[15+1];
1016 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1017 + ptran = ptcp+tranoff;
1019 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
1020 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
1021 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
1023 + INFOP("sanity check failed\n");
1027 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
1029 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
1031 + t->dst.u3.ip = newip;
1033 + extaddrlen = extip ? sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(extip))
1034 + : sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(newip));
1035 + DEBUGP("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
1037 + rbuf1len = rbufalen = 0;
1038 + switch (prtspexp->pbtype)
1041 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1043 + t->dst.u.udp.port = htons(loport);
1044 + if (nf_ct_expect_related(exp) == 0)
1046 + DEBUGP("using port %hu\n", loport);
1052 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1053 + rbufalen = sprintf(rbufa, "%hu", loport);
1057 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
1059 + t->dst.u.udp.port = htons(loport);
1060 + if (nf_ct_expect_related(exp) == 0)
1062 + hiport = loport + ~exp->mask.src.u.udp.port;
1063 + DEBUGP("using ports %hu-%hu\n", loport, hiport);
1069 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1070 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
1074 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1076 + t->dst.u.udp.port = htons(loport);
1077 + if (nf_ct_expect_related(exp) == 0)
1079 + DEBUGP("using port %hu (1 of 2)\n", loport);
1083 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
1085 + t->dst.u.udp.port = htons(hiport);
1086 + if (nf_ct_expect_related(exp) == 0)
1088 + DEBUGP("using port %hu (2 of 2)\n", hiport);
1092 + if (loport != 0 && hiport != 0)
1094 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1095 + if (hiport == loport+1)
1097 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
1101 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
1107 + if (rbuf1len == 0)
1109 + return 0; /* cannot get replacement port(s) */
1112 + /* Transport: tran;field;field=val,tran;field;field=val,... */
1113 + while (off < tranlen)
1116 + const char* pparamend;
1117 + uint nextparamoff;
1119 + pparamend = memchr(ptran+off, ',', tranlen-off);
1120 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
1121 + nextparamoff = pparamend-ptcp;
1124 + * We pass over each param twice. On the first pass, we look for a
1125 + * destination= field. It is handled by the security policy. If it
1126 + * is present, allowed, and equal to our external address, we assume
1127 + * that STUN is being used and we leave the client_port= field alone.
1131 + while (off < nextparamoff)
1133 + const char* pfieldend;
1134 + uint nextfieldoff;
1136 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1137 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1139 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
1141 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
1145 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
1147 + diff = nextfieldoff-off;
1148 + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
1149 + off, diff, NULL, 0))
1151 + /* mangle failed, all we can do is bail */
1152 + nf_ct_unexpect_related(exp);
1155 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1156 + ptran = ptcp+tranoff;
1158 + nextparamoff -= diff;
1159 + nextfieldoff -= diff;
1163 + off = nextfieldoff;
1170 + while (off < nextparamoff)
1172 + const char* pfieldend;
1173 + uint nextfieldoff;
1175 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1176 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1178 + if (strncmp(ptran+off, "client_port=", 12) == 0)
1184 + char* rbuf = rbuf1;
1185 + uint rbuflen = rbuf1len;
1188 + origoff = (ptran-ptcp)+off;
1190 + numlen = nf_strtou16(ptran+off, &port);
1192 + origlen += numlen;
1193 + if (port != prtspexp->loport)
1195 + DEBUGP("multiple ports found, port %hu ignored\n", port);
1199 + if (ptran[off] == '-' || ptran[off] == '/')
1203 + numlen = nf_strtou16(ptran+off, &port);
1205 + origlen += numlen;
1207 + rbuflen = rbufalen;
1211 + * note we cannot just memcpy() if the sizes are the same.
1212 + * the mangle function does skb resizing, checks for a
1213 + * cloned skb, and updates the checksums.
1215 + * parameter 4 below is offset from start of tcp data.
1217 + diff = origlen-rbuflen;
1218 + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
1219 + origoff, origlen, rbuf, rbuflen))
1221 + /* mangle failed, all we can do is bail */
1222 + nf_ct_unexpect_related(exp);
1225 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1226 + ptran = ptcp+tranoff;
1228 + nextparamoff -= diff;
1229 + nextfieldoff -= diff;
1233 + off = nextfieldoff;
1236 + off = nextparamoff;
1243 +help_out(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
1244 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
1245 + struct nf_conntrack_expect* exp)
1255 + //struct iphdr* iph = (struct iphdr*)skb->nh.iph;
1256 + //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
1258 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1259 + hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
1260 + hdrslen = matchlen;
1262 + DEBUGP("NAT rtsp help_out\n");
1264 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
1270 + if (off > hdrsoff+hdrslen)
1272 + INFOP("!! overrun !!");
1275 + DEBUGP("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1277 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
1279 + uint oldtcplen = tcplen;
1280 + DEBUGP("hdr: Transport\n");
1281 + if (!rtsp_mangle_tran(ctinfo, exp, prtspexp, skb, lineoff, linelen))
1283 + DEBUGP("hdr: Transport mangle failed");
1286 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1287 + hdrslen -= (oldtcplen-tcplen);
1288 + off -= (oldtcplen-tcplen);
1289 + lineoff -= (oldtcplen-tcplen);
1290 + linelen -= (oldtcplen-tcplen);
1291 + DEBUGP("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1298 +static unsigned int
1299 +help(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
1300 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
1301 + struct nf_conntrack_expect* exp)
1303 + int dir = CTINFO2DIR(ctinfo);
1304 + int rc = NF_ACCEPT;
1308 + case IP_CT_DIR_ORIGINAL:
1309 + rc = help_out(skb, ctinfo, matchoff, matchlen, prtspexp, exp);
1311 + case IP_CT_DIR_REPLY:
1312 + DEBUGP("unmangle ! %u\n", ctinfo);
1313 + /* XXX: unmangle */
1317 + //UNLOCK_BH(&ip_rtsp_lock);
1322 +static void expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
1324 + struct nf_nat_multi_range_compat mr;
1325 + u_int32_t newdstip, newsrcip, newip;
1327 + struct nf_conn *master = ct->master;
1329 + newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
1330 + newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
1331 + //FIXME (how to port that ?)
1332 + //code from 2.4 : newip = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) ? newsrcip : newdstip;
1335 + DEBUGP("newsrcip=%u.%u.%u.%u, newdstip=%u.%u.%u.%u, newip=%u.%u.%u.%u\n",
1336 + NIPQUAD(newsrcip), NIPQUAD(newdstip), NIPQUAD(newip));
1339 + // We don't want to manip the per-protocol, just the IPs.
1340 + mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
1341 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
1343 + nf_nat_setup_info(ct, &mr.range[0], NF_IP_PRE_ROUTING);
1347 +static void __exit fini(void)
1349 + nf_nat_rtsp_hook = NULL;
1350 + nf_nat_rtsp_hook_expectfn = NULL;
1351 + synchronize_net();
1354 +static int __init init(void)
1356 + printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1358 + BUG_ON(nf_nat_rtsp_hook);
1359 + nf_nat_rtsp_hook = help;
1360 + nf_nat_rtsp_hook_expectfn = &expected;
1362 + if (stunaddr != NULL)
1363 + extip = in_aton(stunaddr);
1365 + if (destaction != NULL) {
1366 + if (strcmp(destaction, "auto") == 0)
1367 + dstact = DSTACT_AUTO;
1369 + if (strcmp(destaction, "strip") == 0)
1370 + dstact = DSTACT_STRIP;
1372 + if (strcmp(destaction, "none") == 0)
1373 + dstact = DSTACT_NONE;