1 diff -urN linux-2.6.21-rc7/include/linux/netfilter/nf_conntrack_rtsp.h linux-2.6.21-rc7.rtsp/include/linux/netfilter/nf_conntrack_rtsp.h
2 --- linux-2.6.21-rc7/include/linux/netfilter/nf_conntrack_rtsp.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.21-rc7.rtsp/include/linux/netfilter/nf_conntrack_rtsp.h 2007-04-16 14:07:06.000000000 +0200
6 + * RTSP extension for IP connection tracking.
7 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
8 + * based on ip_conntrack_irc.h
10 + * This program is free software; you can redistribute it and/or
11 + * modify it under the terms of the GNU General Public License
12 + * as published by the Free Software Foundation; either version
13 + * 2 of the License, or (at your option) any later version.
15 +#ifndef _IP_CONNTRACK_RTSP_H
16 +#define _IP_CONNTRACK_RTSP_H
18 +//#define IP_NF_RTSP_DEBUG 1
19 +#define IP_NF_RTSP_VERSION "0.6.21"
22 +/* port block types */
24 + pb_single, /* client_port=x */
25 + pb_range, /* client_port=x-y */
26 + pb_discon /* client_port=x/y (rtspbis) */
29 +/* We record seq number and length of rtsp headers here, all in host order. */
32 + * This structure is per expected connection. It is a member of struct
33 + * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
34 + * there and we are expected to only store the length of the data which
35 + * needs replaced. If a packet contains multiple RTSP messages, we create
36 + * one expected connection per message.
38 + * We use these variables to mark the entire header block. This may seem
39 + * like overkill, but the nature of RTSP requires it. A header may appear
40 + * multiple times in a message. We must treat two Transport headers the
41 + * same as one Transport header with two entries.
43 +struct ip_ct_rtsp_expect
45 + u_int32_t len; /* length of header block */
46 + portblock_t pbtype; /* Type of port block that was requested */
47 + u_int16_t loport; /* Port that was requested, low or first */
48 + u_int16_t hiport; /* Port that was requested, high or second */
50 + uint method; /* RTSP method */
51 + uint cseq; /* CSeq from request */
55 +extern unsigned int (*nf_nat_rtsp_hook)(struct sk_buff **pskb,
56 + enum ip_conntrack_info ctinfo,
57 + unsigned int matchoff, unsigned int matchlen,
58 + struct ip_ct_rtsp_expect *prtspexp,
59 + struct nf_conntrack_expect *exp);
61 +extern void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
63 +#define RTSP_PORT 554
65 +#endif /* __KERNEL__ */
67 +#endif /* _IP_CONNTRACK_RTSP_H */
68 diff -urN linux-2.6.21-rc7/include/linux/netfilter_helpers.h linux-2.6.21-rc7.rtsp/include/linux/netfilter_helpers.h
69 --- linux-2.6.21-rc7/include/linux/netfilter_helpers.h 1970-01-01 01:00:00.000000000 +0100
70 +++ linux-2.6.21-rc7.rtsp/include/linux/netfilter_helpers.h 2007-04-15 00:34:57.000000000 +0200
73 + * Helpers for netfiler modules. This file provides implementations for basic
74 + * functions such as strncasecmp(), etc.
76 + * gcc will warn for defined but unused functions, so we only include the
77 + * functions requested. The following macros are used:
78 + * NF_NEED_STRNCASECMP nf_strncasecmp()
79 + * NF_NEED_STRTOU16 nf_strtou16()
80 + * NF_NEED_STRTOU32 nf_strtou32()
82 +#ifndef _NETFILTER_HELPERS_H
83 +#define _NETFILTER_HELPERS_H
85 +/* Only include these functions for kernel code. */
88 +#include <linux/ctype.h>
89 +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
92 + * The standard strncasecmp()
94 +#ifdef NF_NEED_STRNCASECMP
96 +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
98 + if (s1 == NULL || s2 == NULL)
100 + if (s1 == NULL && s2 == NULL)
104 + return (s1 == NULL) ? -1 : 1;
106 + while (len > 0 && tolower(*s1) == tolower(*s2))
112 + return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
114 +#endif /* NF_NEED_STRNCASECMP */
117 + * Parse a string containing a 16-bit unsigned integer.
118 + * Returns the number of chars used, or zero if no number is found.
120 +#ifdef NF_NEED_STRTOU16
122 +nf_strtou16(const char* pbuf, u_int16_t* pval)
127 + while (isdigit(pbuf[n]))
129 + *pval = (*pval * 10) + (pbuf[n] - '0');
135 +#endif /* NF_NEED_STRTOU16 */
138 + * Parse a string containing a 32-bit unsigned integer.
139 + * Returns the number of chars used, or zero if no number is found.
141 +#ifdef NF_NEED_STRTOU32
143 +nf_strtou32(const char* pbuf, u_int32_t* pval)
148 + while (pbuf[n] >= '0' && pbuf[n] <= '9')
150 + *pval = (*pval * 10) + (pbuf[n] - '0');
156 +#endif /* NF_NEED_STRTOU32 */
159 + * Given a buffer and length, advance to the next line and mark the current
162 +#ifdef NF_NEED_NEXTLINE
164 +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
174 + while (p[off] != '\n')
185 + /* if we saw a crlf, physlen needs adjusted */
186 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
191 + /* advance past the newline */
195 + *plinelen = physlen;
200 +#endif /* NF_NEED_NEXTLINE */
202 +#endif /* __KERNEL__ */
204 +#endif /* _NETFILTER_HELPERS_H */
205 diff -urN linux-2.6.21-rc7/include/linux/netfilter_mime.h linux-2.6.21-rc7.rtsp/include/linux/netfilter_mime.h
206 --- linux-2.6.21-rc7/include/linux/netfilter_mime.h 1970-01-01 01:00:00.000000000 +0100
207 +++ linux-2.6.21-rc7.rtsp/include/linux/netfilter_mime.h 2007-04-15 00:34:57.000000000 +0200
210 + * MIME functions for netfilter modules. This file provides implementations
211 + * for basic MIME parsing. MIME headers are used in many protocols, such as
212 + * HTTP, RTSP, SIP, etc.
214 + * gcc will warn for defined but unused functions, so we only include the
215 + * functions requested. The following macros are used:
216 + * NF_NEED_MIME_NEXTLINE nf_mime_nextline()
218 +#ifndef _NETFILTER_MIME_H
219 +#define _NETFILTER_MIME_H
221 +/* Only include these functions for kernel code. */
224 +#include <linux/ctype.h>
227 + * Given a buffer and length, advance to the next line and mark the current
228 + * line. If the current line is empty, *plinelen will be set to zero. If
229 + * not, it will be set to the actual line length (including CRLF).
231 + * 'line' in this context means logical line (includes LWS continuations).
232 + * Returns 1 on success, 0 on failure.
234 +#ifdef NF_NEED_MIME_NEXTLINE
236 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
240 + int is_first_line = 1;
249 + while (p[off] != '\n')
260 + /* if we saw a crlf, physlen needs adjusted */
261 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
266 + /* advance past the newline */
269 + /* check for an empty line */
275 + /* check for colon on the first physical line */
279 + if (memchr(p+(*poff), ':', physlen) == NULL)
285 + while (p[off] == ' ' || p[off] == '\t');
288 + *plinelen = (physlen == 0) ? 0 : (off - *poff);
293 +#endif /* NF_NEED_MIME_NEXTLINE */
295 +#endif /* __KERNEL__ */
297 +#endif /* _NETFILTER_MIME_H */
298 diff -urN linux-2.6.21-rc7/net/ipv4/netfilter/Makefile linux-2.6.21-rc7.rtsp/net/ipv4/netfilter/Makefile
299 --- linux-2.6.21-rc7/net/ipv4/netfilter/Makefile 2007-04-18 23:26:10.000000000 +0200
300 +++ linux-2.6.21-rc7.rtsp/net/ipv4/netfilter/Makefile 2007-04-18 21:17:33.000000000 +0200
302 obj-$(CONFIG_NF_NAT_FTP) += nf_nat_ftp.o
303 obj-$(CONFIG_NF_NAT_H323) += nf_nat_h323.o
304 obj-$(CONFIG_NF_NAT_IRC) += nf_nat_irc.o
305 +obj-$(CONFIG_NF_NAT_RTSP) += nf_nat_rtsp.o
306 obj-$(CONFIG_NF_NAT_PPTP) += nf_nat_pptp.o
307 obj-$(CONFIG_NF_NAT_SIP) += nf_nat_sip.o
308 obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o
309 diff -urN linux-2.6.21-rc7/net/netfilter/Kconfig linux-2.6.21-rc7.rtsp/net/netfilter/Kconfig
310 --- linux-2.6.21-rc7/net/netfilter/Kconfig 2007-04-18 23:26:10.000000000 +0200
311 +++ linux-2.6.21-rc7.rtsp/net/netfilter/Kconfig 2007-04-18 22:25:13.000000000 +0200
314 To compile it as a module, choose M here. If unsure, say N.
316 +config NF_CONNTRACK_RTSP
317 + tristate "RTSP protocol support"
318 + depends on NF_CONNTRACK
320 + Support the RTSP protocol. This allows UDP transports to be setup
321 + properly, including RTP and RDT.
323 + If you want to compile it as a module, say 'M' here and read
324 + Documentation/modules.txt. If unsure, say 'Y'.
327 tristate 'Connection tracking netlink interface (EXPERIMENTAL)'
328 depends on EXPERIMENTAL && NF_CONNTRACK && NETFILTER_NETLINK
329 diff -urN linux-2.6.21-rc7/net/netfilter/Makefile linux-2.6.21-rc7.rtsp/net/netfilter/Makefile
330 --- linux-2.6.21-rc7/net/netfilter/Makefile 2007-04-18 23:26:10.000000000 +0200
331 +++ linux-2.6.21-rc7.rtsp/net/netfilter/Makefile 2007-04-18 21:17:33.000000000 +0200
333 obj-$(CONFIG_NF_CONNTRACK_SANE) += nf_conntrack_sane.o
334 obj-$(CONFIG_NF_CONNTRACK_SIP) += nf_conntrack_sip.o
335 obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o
336 +obj-$(CONFIG_NF_CONNTRACK_RTSP) += nf_conntrack_rtsp.o
339 obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o
340 --- linux-2.6.22.1/net/ipv4/netfilter/Kconfig.orig 2007-07-29 23:57:51.000000000 +0200
341 +++ linux-2.6.22.1/net/ipv4/netfilter/Kconfig 2007-07-30 00:00:19.000000000 +0200
343 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
344 default NF_NAT && NF_CONNTRACK_IRC
348 + depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
349 + default NF_NAT && NF_CONNTRACK_RTSP
353 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
354 --- linux-2.6.22.1/net/netfilter/nf_conntrack_rtsp.c.orig 2007-07-30 17:37:14.000000000 +0200
355 +++ linux-2.6.22.1/net/netfilter/nf_conntrack_rtsp.c 2007-07-30 00:03:07.000000000 +0200
358 + * RTSP extension for IP connection tracking
359 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
360 + * based on ip_conntrack_irc.c
362 + * This program is free software; you can redistribute it and/or
363 + * modify it under the terms of the GNU General Public License
364 + * as published by the Free Software Foundation; either version
365 + * 2 of the License, or (at your option) any later version.
367 + * Module load syntax:
368 + * insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
369 + * max_outstanding=n setup_timeout=secs
371 + * If no ports are specified, the default will be port 554.
373 + * With max_outstanding you can define the maximum number of not yet
374 + * answered SETUP requests per RTSP session (default 8).
375 + * With setup_timeout you can specify how long the system waits for
376 + * an expected data channel (default 300 seconds).
378 + * 2005-02-13: Harald Welte <laforge at netfilter.org>
380 + * - update to recent post-2.6.11 api changes
381 + * 2006-09-14: Steven Van Acker <deepstar at singularity.be>
382 + * - removed calls to NAT code from conntrack helper: NAT no longer needed to use rtsp-conntrack
383 + * 2007-04-18: Michael Guntsche <mike at it-loops.com>
384 + * - Port to new NF API
387 +#include <linux/module.h>
388 +#include <linux/netfilter.h>
389 +#include <linux/ip.h>
390 +#include <linux/inet.h>
391 +#include <net/tcp.h>
393 +#include <net/netfilter/nf_conntrack.h>
394 +#include <net/netfilter/nf_conntrack_expect.h>
395 +#include <net/netfilter/nf_conntrack_helper.h>
396 +#include <linux/netfilter/nf_conntrack_rtsp.h>
398 +#define NF_NEED_STRNCASECMP
399 +#define NF_NEED_STRTOU16
400 +#define NF_NEED_STRTOU32
401 +#define NF_NEED_NEXTLINE
402 +#include <linux/netfilter_helpers.h>
403 +#define NF_NEED_MIME_NEXTLINE
404 +#include <linux/netfilter_mime.h>
406 +#include <linux/ctype.h>
407 +#define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
408 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
410 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
412 +#define DEBUGP(fmt, args...)
416 +static int ports[MAX_PORTS];
417 +static int num_ports = 0;
418 +static int max_outstanding = 8;
419 +static unsigned int setup_timeout = 300;
421 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
422 +MODULE_DESCRIPTION("RTSP connection tracking module");
423 +MODULE_LICENSE("GPL");
424 +module_param_array(ports, int, &num_ports, 0400);
425 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
426 +module_param(max_outstanding, int, 0400);
427 +MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
428 +module_param(setup_timeout, int, 0400);
429 +MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
431 +static char *rtsp_buffer;
432 +static DEFINE_SPINLOCK(rtsp_buffer_lock);
434 +unsigned int (*nf_nat_rtsp_hook)(struct sk_buff **pskb,
435 + enum ip_conntrack_info ctinfo,
436 + unsigned int matchoff, unsigned int matchlen,struct ip_ct_rtsp_expect* prtspexp,
437 + struct nf_conntrack_expect *exp);
438 +void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
440 +EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook);
443 + * Max mappings we will allow for one RTSP connection (for RTP, the number
444 + * of allocated ports is twice this value). Note that SMIL burns a lot of
445 + * ports so keep this reasonably high. If this is too low, you will see a
446 + * lot of "no free client map entries" messages.
448 +#define MAX_PORT_MAPS 16
450 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
452 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
455 + * Parse an RTSP packet.
457 + * Returns zero if parsing failed.
460 + * IN ptcp tcp data pointer
461 + * IN tcplen tcp data len
462 + * IN/OUT ptcpoff points to current tcp offset
463 + * OUT phdrsoff set to offset of rtsp headers
464 + * OUT phdrslen set to length of rtsp headers
465 + * OUT pcseqoff set to offset of CSeq header
466 + * OUT pcseqlen set to length of CSeq header
469 +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
470 + uint* phdrsoff, uint* phdrslen,
471 + uint* pcseqoff, uint* pcseqlen,
472 + uint* transoff, uint* translen)
474 + uint entitylen = 0;
478 + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
481 + *phdrsoff = *ptcpoff;
482 + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
483 + if (linelen == 0) {
485 + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
488 + if (lineoff+linelen > tcplen) {
489 + INFOP("!! overrun !!\n");
493 + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
494 + *pcseqoff = lineoff;
495 + *pcseqlen = linelen;
498 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
499 + *transoff = lineoff;
500 + *translen = linelen;
503 + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0) {
504 + uint off = lineoff+15;
505 + SKIP_WSPACE(ptcp+lineoff, linelen, off);
506 + nf_strtou32(ptcp+off, &entitylen);
509 + *phdrslen = (*ptcpoff) - (*phdrsoff);
515 + * Find lo/hi client ports (if any) in transport header
517 + * ptcp, tcplen = packet
518 + * tranoff, tranlen = buffer to search
521 + * pport_lo, pport_hi = lo/hi ports (host endian)
523 + * Returns nonzero if any client ports found
525 + * Note: it is valid (and expected) for the client to request multiple
526 + * transports, so we need to parse the entire line.
529 +rtsp_parse_transport(char* ptran, uint tranlen,
530 + struct ip_ct_rtsp_expect* prtspexp)
535 + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
536 + nf_strncasecmp(ptran, "Transport:", 10) != 0) {
537 + INFOP("sanity check failed\n");
541 + DEBUGP("tran='%.*s'\n", (int)tranlen, ptran);
543 + SKIP_WSPACE(ptran, tranlen, off);
545 + /* Transport: tran;field;field=val,tran;field;field=val,... */
546 + while (off < tranlen) {
547 + const char* pparamend;
550 + pparamend = memchr(ptran+off, ',', tranlen-off);
551 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
552 + nextparamoff = pparamend-ptran;
554 + while (off < nextparamoff) {
555 + const char* pfieldend;
558 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
559 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
561 + if (strncmp(ptran+off, "client_port=", 12) == 0) {
566 + numlen = nf_strtou16(ptran+off, &port);
568 + if (prtspexp->loport != 0 && prtspexp->loport != port)
569 + DEBUGP("multiple ports found, port %hu ignored\n", port);
571 + DEBUGP("lo port found : %hu\n", port);
572 + prtspexp->loport = prtspexp->hiport = port;
573 + if (ptran[off] == '-') {
575 + numlen = nf_strtou16(ptran+off, &port);
577 + prtspexp->pbtype = pb_range;
578 + prtspexp->hiport = port;
580 + // If we have a range, assume rtp:
581 + // loport must be even, hiport must be loport+1
582 + if ((prtspexp->loport & 0x0001) != 0 ||
583 + prtspexp->hiport != prtspexp->loport+1) {
584 + DEBUGP("incorrect range: %hu-%hu, correcting\n",
585 + prtspexp->loport, prtspexp->hiport);
586 + prtspexp->loport &= 0xfffe;
587 + prtspexp->hiport = prtspexp->loport+1;
589 + } else if (ptran[off] == '/') {
591 + numlen = nf_strtou16(ptran+off, &port);
593 + prtspexp->pbtype = pb_discon;
594 + prtspexp->hiport = port;
601 + * Note we don't look for the destination parameter here.
602 + * If we are using NAT, the NAT module will handle it. If not,
603 + * and the client is sending packets elsewhere, the expectation
604 + * will quietly time out.
607 + off = nextfieldoff;
610 + off = nextparamoff;
616 +void expected(struct nf_conn *ct, struct nf_conntrack_expect *exp)
618 + if(nf_nat_rtsp_hook_expectfn) {
619 + nf_nat_rtsp_hook_expectfn(ct,exp);
623 +/*** conntrack functions ***/
625 +/* outbound packet: client->server */
628 +help_out(struct sk_buff **pskb, unsigned char *rb_ptr, unsigned int datalen,
629 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
631 + struct ip_ct_rtsp_expect expinfo;
633 + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
634 + //struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
635 + //uint tcplen = pktlen - iph->ihl * 4;
636 + char* pdata = rb_ptr;
637 + //uint datalen = tcplen - tcph->doff * 4;
639 + int ret = NF_ACCEPT;
641 + struct nf_conntrack_expect *exp;
645 + memset(&expinfo, 0, sizeof(expinfo));
647 + while (dataoff < datalen) {
648 + uint cmdoff = dataoff;
657 + if (!rtsp_parse_message(pdata, datalen, &dataoff,
658 + &hdrsoff, &hdrslen,
659 + &cseqoff, &cseqlen,
660 + &transoff, &translen))
661 + break; /* not a valid message */
663 + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
664 + continue; /* not a SETUP message */
665 + DEBUGP("found a setup message\n");
669 + rtsp_parse_transport(pdata+transoff, translen, &expinfo);
672 + if (expinfo.loport == 0) {
673 + DEBUGP("no udp transports found\n");
674 + continue; /* no udp transports found */
677 + DEBUGP("udp transport found, ports=(%d,%hu,%hu)\n",
678 + (int)expinfo.pbtype, expinfo.loport, expinfo.hiport);
680 + exp = nf_conntrack_expect_alloc(ct);
686 + be_loport = htons(expinfo.loport);
688 + nf_conntrack_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
689 + &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3,
690 + IPPROTO_UDP, NULL, &be_loport);
694 + exp->expectfn = expected;
697 + if (expinfo.pbtype == pb_range) {
698 + DEBUGP("Changing expectation mask to handle multiple ports\n");
699 + exp->mask.dst.u.udp.port = 0xfffe;
702 + DEBUGP("expect_related %u.%u.%u.%u:%u-%u.%u.%u.%u:%u\n",
703 + NIPQUAD(exp->tuple.src.u3.ip),
704 + ntohs(exp->tuple.src.u.udp.port),
705 + NIPQUAD(exp->tuple.dst.u3.ip),
706 + ntohs(exp->tuple.dst.u.udp.port));
708 + if (nf_nat_rtsp_hook)
709 + /* pass the request off to the nat helper */
710 + ret = nf_nat_rtsp_hook(pskb, ctinfo, hdrsoff, hdrslen, &expinfo, exp);
711 + else if (nf_conntrack_expect_related(exp) != 0) {
712 + INFOP("nf_conntrack_expect_related failed\n");
715 + nf_conntrack_expect_put(exp);
725 +help_in(struct sk_buff **pskb, size_t pktlen,
726 + struct nf_conn* ct, enum ip_conntrack_info ctinfo)
731 +static int help(struct sk_buff **pskb, unsigned int protoff,
732 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
734 + struct tcphdr _tcph, *th;
735 + unsigned int dataoff, datalen;
739 + /* Until there's been traffic both ways, don't look in packets. */
740 + if (ctinfo != IP_CT_ESTABLISHED &&
741 + ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
742 + DEBUGP("conntrackinfo = %u\n", ctinfo);
746 + /* Not whole TCP header? */
747 + th = skb_header_pointer(*pskb,protoff, sizeof(_tcph), &_tcph);
753 + dataoff = protoff + th->doff*4;
754 + datalen = (*pskb)->len - dataoff;
755 + if (dataoff >= (*pskb)->len)
758 + spin_lock_bh(&rtsp_buffer_lock);
759 + rb_ptr = skb_header_pointer(*pskb, dataoff,
760 + (*pskb)->len - dataoff, rtsp_buffer);
761 + BUG_ON(rb_ptr == NULL);
764 + /* Checksum invalid? Ignore. */
765 + /* FIXME: Source route IP option packets --RR */
766 + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
767 + csum_partial((char*)tcph, tcplen, 0)))
769 + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
770 + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
775 + switch (CTINFO2DIR(ctinfo)) {
776 + case IP_CT_DIR_ORIGINAL:
777 + ret = help_out(pskb, rb_ptr, datalen, ct, ctinfo);
779 + case IP_CT_DIR_REPLY:
780 + DEBUGP("IP_CT_DIR_REPLY\n");
781 + /* inbound packet: server->client */
786 + spin_unlock_bh(&rtsp_buffer_lock);
791 +static struct nf_conntrack_helper rtsp_helpers[MAX_PORTS];
792 +static char rtsp_names[MAX_PORTS][10];
794 +/* This function is intentionally _NOT_ defined as __exit */
799 + for (i = 0; i < num_ports; i++) {
800 + DEBUGP("unregistering port %d\n", ports[i]);
801 + nf_conntrack_helper_unregister(&rtsp_helpers[i]);
803 + kfree(rtsp_buffer);
810 + struct nf_conntrack_helper *hlpr;
813 + printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
815 + if (max_outstanding < 1) {
816 + printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
819 + if (setup_timeout < 0) {
820 + printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
824 + rtsp_buffer = kmalloc(65536, GFP_KERNEL);
828 + /* If no port given, default to standard rtsp port */
829 + if (ports[0] == 0) {
830 + ports[0] = RTSP_PORT;
833 + for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
834 + hlpr = &rtsp_helpers[i];
835 + memset(hlpr, 0, sizeof(struct nf_conntrack_helper));
836 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
837 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
838 + hlpr->mask.src.u.tcp.port = 0xFFFF;
839 + hlpr->mask.dst.protonum = 0xFF;
840 + hlpr->max_expected = max_outstanding;
841 + hlpr->timeout = setup_timeout;
842 + hlpr->me = THIS_MODULE;
845 + tmpname = &rtsp_names[i][0];
846 + if (ports[i] == RTSP_PORT) {
847 + sprintf(tmpname, "rtsp");
849 + sprintf(tmpname, "rtsp-%d", i);
851 + hlpr->name = tmpname;
853 + DEBUGP("port #%d: %d\n", i, ports[i]);
855 + ret = nf_conntrack_helper_register(hlpr);
858 + printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
870 +EXPORT_SYMBOL(nf_nat_rtsp_hook_expectfn);
872 --- linux-2.6.22.1/net/ipv4/netfilter/nf_nat_rtsp.c.orig 2007-07-30 17:35:02.000000000 +0200
873 +++ linux-2.6.22.1/net/ipv4/netfilter/nf_nat_rtsp.c 2007-07-30 00:05:28.000000000 +0200
876 + * RTSP extension for TCP NAT alteration
877 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
878 + * based on ip_nat_irc.c
880 + * This program is free software; you can redistribute it and/or
881 + * modify it under the terms of the GNU General Public License
882 + * as published by the Free Software Foundation; either version
883 + * 2 of the License, or (at your option) any later version.
885 + * Module load syntax:
886 + * insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
887 + * stunaddr=<address>
888 + * destaction=[auto|strip|none]
890 + * If no ports are specified, the default will be port 554 only.
892 + * stunaddr specifies the address used to detect that a client is using STUN.
893 + * If this address is seen in the destination parameter, it is assumed that
894 + * the client has already punched a UDP hole in the firewall, so we don't
895 + * mangle the client_port. If none is specified, it is autodetected. It
896 + * only needs to be set if you have multiple levels of NAT. It should be
897 + * set to the external address that the STUN clients detect. Note that in
898 + * this case, it will not be possible for clients to use UDP with servers
899 + * between the NATs.
901 + * If no destaction is specified, auto is used.
902 + * destaction=auto: strip destination parameter if it is not stunaddr.
903 + * destaction=strip: always strip destination parameter (not recommended).
904 + * destaction=none: do not touch destination parameter (not recommended).
907 +#include <linux/module.h>
908 +#include <net/tcp.h>
909 +#include <net/netfilter/nf_nat_helper.h>
910 +#include <net/netfilter/nf_nat_rule.h>
911 +#include <linux/netfilter/nf_conntrack_rtsp.h>
912 +#include <net/netfilter/nf_conntrack_expect.h>
914 +#include <linux/inet.h>
915 +#include <linux/ctype.h>
916 +#define NF_NEED_STRNCASECMP
917 +#define NF_NEED_STRTOU16
918 +#include <linux/netfilter_helpers.h>
919 +#define NF_NEED_MIME_NEXTLINE
920 +#include <linux/netfilter_mime.h>
922 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
924 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
926 +#define DEBUGP(fmt, args...)
930 +#define DSTACT_AUTO 0
931 +#define DSTACT_STRIP 1
932 +#define DSTACT_NONE 2
934 +static char* stunaddr = NULL;
935 +static char* destaction = NULL;
937 +static u_int32_t extip = 0;
938 +static int dstact = 0;
940 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
941 +MODULE_DESCRIPTION("RTSP network address translation module");
942 +MODULE_LICENSE("GPL");
943 +module_param(stunaddr, charp, 0644);
944 +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
945 +module_param(destaction, charp, 0644);
946 +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
948 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
950 +/*** helper functions ***/
953 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
955 + struct iphdr* iph = ip_hdr(skb);
956 + struct tcphdr* tcph = (void *)iph + ip_hdrlen(skb);
958 + *pptcpdata = (char*)tcph + tcph->doff*4;
959 + *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
962 +/*** nat functions ***/
965 + * Mangle the "Transport:" header:
966 + * - Replace all occurences of "client_port=<spec>"
967 + * - Handle destination parameter
970 + * ct, ctinfo = conntrack context
972 + * tranoff = Transport header offset from TCP data
973 + * tranlen = Transport header length (incl. CRLF)
974 + * rport_lo = replacement low port (host endian)
975 + * rport_hi = replacement high port (host endian)
977 + * Returns packet size difference.
979 + * Assumes that a complete transport header is present, ending with CR or LF
982 +rtsp_mangle_tran(enum ip_conntrack_info ctinfo,
983 + struct nf_conntrack_expect* exp,
984 + struct ip_ct_rtsp_expect* prtspexp,
985 + struct sk_buff** pskb, uint tranoff, uint tranlen)
990 + char rbuf1[16]; /* Replacement buffer (one port) */
991 + uint rbuf1len; /* Replacement len (one port) */
992 + char rbufa[16]; /* Replacement buffer (all ports) */
993 + uint rbufalen; /* Replacement len (all ports) */
995 + u_int16_t loport, hiport;
997 + uint diff; /* Number of bytes we removed */
999 + struct nf_conn *ct = exp->master;
1000 + struct nf_conntrack_tuple *t;
1002 + char szextaddr[15+1];
1006 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1007 + ptran = ptcp+tranoff;
1009 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
1010 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
1011 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
1013 + INFOP("sanity check failed\n");
1017 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
1019 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
1021 + t->dst.u3.ip = newip;
1023 + extaddrlen = extip ? sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(extip))
1024 + : sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(newip));
1025 + DEBUGP("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
1027 + rbuf1len = rbufalen = 0;
1028 + switch (prtspexp->pbtype)
1031 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1033 + t->dst.u.udp.port = htons(loport);
1034 + if (nf_conntrack_expect_related(exp) == 0)
1036 + DEBUGP("using port %hu\n", loport);
1042 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1043 + rbufalen = sprintf(rbufa, "%hu", loport);
1047 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
1049 + t->dst.u.udp.port = htons(loport);
1050 + if (nf_conntrack_expect_related(exp) == 0)
1052 + hiport = loport + ~exp->mask.dst.u.udp.port;
1053 + DEBUGP("using ports %hu-%hu\n", loport, hiport);
1059 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1060 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
1064 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1066 + t->dst.u.udp.port = htons(loport);
1067 + if (nf_conntrack_expect_related(exp) == 0)
1069 + DEBUGP("using port %hu (1 of 2)\n", loport);
1073 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
1075 + t->dst.u.udp.port = htons(hiport);
1076 + if (nf_conntrack_expect_related(exp) == 0)
1078 + DEBUGP("using port %hu (2 of 2)\n", hiport);
1082 + if (loport != 0 && hiport != 0)
1084 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1085 + if (hiport == loport+1)
1087 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
1091 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
1097 + if (rbuf1len == 0)
1099 + return 0; /* cannot get replacement port(s) */
1102 + /* Transport: tran;field;field=val,tran;field;field=val,... */
1103 + while (off < tranlen)
1106 + const char* pparamend;
1107 + uint nextparamoff;
1109 + pparamend = memchr(ptran+off, ',', tranlen-off);
1110 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
1111 + nextparamoff = pparamend-ptcp;
1114 + * We pass over each param twice. On the first pass, we look for a
1115 + * destination= field. It is handled by the security policy. If it
1116 + * is present, allowed, and equal to our external address, we assume
1117 + * that STUN is being used and we leave the client_port= field alone.
1121 + while (off < nextparamoff)
1123 + const char* pfieldend;
1124 + uint nextfieldoff;
1126 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1127 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1129 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
1131 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
1135 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
1137 + diff = nextfieldoff-off;
1138 + if (!nf_nat_mangle_tcp_packet(pskb, ct, ctinfo,
1139 + off, diff, NULL, 0))
1141 + /* mangle failed, all we can do is bail */
1142 + nf_conntrack_unexpect_related(exp);
1145 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1146 + ptran = ptcp+tranoff;
1148 + nextparamoff -= diff;
1149 + nextfieldoff -= diff;
1153 + off = nextfieldoff;
1160 + while (off < nextparamoff)
1162 + const char* pfieldend;
1163 + uint nextfieldoff;
1165 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1166 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1168 + if (strncmp(ptran+off, "client_port=", 12) == 0)
1174 + char* rbuf = rbuf1;
1175 + uint rbuflen = rbuf1len;
1178 + origoff = (ptran-ptcp)+off;
1180 + numlen = nf_strtou16(ptran+off, &port);
1182 + origlen += numlen;
1183 + if (port != prtspexp->loport)
1185 + DEBUGP("multiple ports found, port %hu ignored\n", port);
1189 + if (ptran[off] == '-' || ptran[off] == '/')
1193 + numlen = nf_strtou16(ptran+off, &port);
1195 + origlen += numlen;
1197 + rbuflen = rbufalen;
1201 + * note we cannot just memcpy() if the sizes are the same.
1202 + * the mangle function does skb resizing, checks for a
1203 + * cloned skb, and updates the checksums.
1205 + * parameter 4 below is offset from start of tcp data.
1207 + diff = origlen-rbuflen;
1208 + if (!nf_nat_mangle_tcp_packet(pskb, ct, ctinfo,
1209 + origoff, origlen, rbuf, rbuflen))
1211 + /* mangle failed, all we can do is bail */
1212 + nf_conntrack_unexpect_related(exp);
1215 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1216 + ptran = ptcp+tranoff;
1218 + nextparamoff -= diff;
1219 + nextfieldoff -= diff;
1223 + off = nextfieldoff;
1226 + off = nextparamoff;
1233 +help_out(struct sk_buff **pskb, enum ip_conntrack_info ctinfo,
1234 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
1235 + struct nf_conntrack_expect* exp)
1245 + //struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
1246 + //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
1248 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1249 + hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
1250 + hdrslen = matchlen;
1252 + DEBUGP("NAT rtsp help_out\n");
1254 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
1260 + if (off > hdrsoff+hdrslen)
1262 + INFOP("!! overrun !!");
1265 + DEBUGP("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1267 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
1269 + uint oldtcplen = tcplen;
1270 + DEBUGP("hdr: Transport\n");
1271 + if (!rtsp_mangle_tran(ctinfo, exp, prtspexp, pskb, lineoff, linelen))
1273 + DEBUGP("hdr: Transport mangle failed");
1276 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1277 + hdrslen -= (oldtcplen-tcplen);
1278 + off -= (oldtcplen-tcplen);
1279 + lineoff -= (oldtcplen-tcplen);
1280 + linelen -= (oldtcplen-tcplen);
1281 + DEBUGP("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1288 +static unsigned int
1289 +help(struct sk_buff **pskb, enum ip_conntrack_info ctinfo,
1290 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
1291 + struct nf_conntrack_expect* exp)
1293 + int dir = CTINFO2DIR(ctinfo);
1294 + int rc = NF_ACCEPT;
1298 + case IP_CT_DIR_ORIGINAL:
1299 + rc = help_out(pskb, ctinfo, matchoff, matchlen, prtspexp, exp);
1301 + case IP_CT_DIR_REPLY:
1302 + DEBUGP("unmangle ! %u\n", ctinfo);
1303 + /* XXX: unmangle */
1307 + //UNLOCK_BH(&ip_rtsp_lock);
1312 +static void expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
1314 + struct nf_nat_multi_range_compat mr;
1315 + u_int32_t newdstip, newsrcip, newip;
1317 + struct nf_conn *master = ct->master;
1319 + newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
1320 + newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
1321 + //FIXME (how to port that ?)
1322 + //code from 2.4 : newip = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) ? newsrcip : newdstip;
1325 + DEBUGP("newsrcip=%u.%u.%u.%u, newdstip=%u.%u.%u.%u, newip=%u.%u.%u.%u\n",
1326 + NIPQUAD(newsrcip), NIPQUAD(newdstip), NIPQUAD(newip));
1329 + // We don't want to manip the per-protocol, just the IPs.
1330 + mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
1331 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
1333 + nf_nat_setup_info(ct, &mr.range[0], NF_IP_PRE_ROUTING);
1337 +static void __exit fini(void)
1339 + nf_nat_rtsp_hook = NULL;
1340 + nf_nat_rtsp_hook_expectfn = NULL;
1341 + synchronize_net();
1344 +static int __init init(void)
1346 + printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1348 + BUG_ON(nf_nat_rtsp_hook);
1349 + nf_nat_rtsp_hook = help;
1350 + nf_nat_rtsp_hook_expectfn = &expected;
1352 + if (stunaddr != NULL)
1353 + extip = in_aton(stunaddr);
1355 + if (destaction != NULL) {
1356 + if (strcmp(destaction, "auto") == 0)
1357 + dstact = DSTACT_AUTO;
1359 + if (strcmp(destaction, "strip") == 0)
1360 + dstact = DSTACT_STRIP;
1362 + if (strcmp(destaction, "none") == 0)
1363 + dstact = DSTACT_NONE;