]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-nf_rtsp.patch
- renamed to kernel-wrr.patch
[packages/kernel.git] / kernel-nf_rtsp.patch
CommitLineData
1c44405b 1diff -purN linux-2.6.23-rc2.orig/include/linux/netfilter_helpers.h linux-2.6.23-rc2/include/linux/netfilter_helpers.h
2--- linux-2.6.23-rc2.orig/include/linux/netfilter_helpers.h 1970-01-01 01:00:00.000000000 +0100
3+++ linux-2.6.23-rc2/include/linux/netfilter_helpers.h 2007-08-05 10:53:55.000000000 +0200
4@@ -0,0 +1,133 @@
5+/*
6+ * Helpers for netfiler modules. This file provides implementations for basic
7+ * functions such as strncasecmp(), etc.
8+ *
9+ * gcc will warn for defined but unused functions, so we only include the
10+ * functions requested. The following macros are used:
11+ * NF_NEED_STRNCASECMP nf_strncasecmp()
12+ * NF_NEED_STRTOU16 nf_strtou16()
13+ * NF_NEED_STRTOU32 nf_strtou32()
14+ */
15+#ifndef _NETFILTER_HELPERS_H
16+#define _NETFILTER_HELPERS_H
17+
18+/* Only include these functions for kernel code. */
19+#ifdef __KERNEL__
20+
21+#include <linux/ctype.h>
22+#define iseol(c) ( (c) == '\r' || (c) == '\n' )
23+
24+/*
25+ * The standard strncasecmp()
26+ */
27+#ifdef NF_NEED_STRNCASECMP
28+static int
29+nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
30+{
31+ if (s1 == NULL || s2 == NULL)
32+ {
33+ if (s1 == NULL && s2 == NULL)
34+ {
35+ return 0;
36+ }
37+ return (s1 == NULL) ? -1 : 1;
38+ }
39+ while (len > 0 && tolower(*s1) == tolower(*s2))
40+ {
41+ len--;
42+ s1++;
43+ s2++;
44+ }
45+ return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
46+}
47+#endif /* NF_NEED_STRNCASECMP */
48+
49+/*
50+ * Parse a string containing a 16-bit unsigned integer.
51+ * Returns the number of chars used, or zero if no number is found.
52+ */
53+#ifdef NF_NEED_STRTOU16
54+static int
55+nf_strtou16(const char* pbuf, u_int16_t* pval)
56+{
57+ int n = 0;
58+
59+ *pval = 0;
60+ while (isdigit(pbuf[n]))
61+ {
62+ *pval = (*pval * 10) + (pbuf[n] - '0');
63+ n++;
64+ }
65+
66+ return n;
67+}
68+#endif /* NF_NEED_STRTOU16 */
69+
70+/*
71+ * Parse a string containing a 32-bit unsigned integer.
72+ * Returns the number of chars used, or zero if no number is found.
73+ */
74+#ifdef NF_NEED_STRTOU32
75+static int
76+nf_strtou32(const char* pbuf, u_int32_t* pval)
77+{
78+ int n = 0;
79+
80+ *pval = 0;
81+ while (pbuf[n] >= '0' && pbuf[n] <= '9')
82+ {
83+ *pval = (*pval * 10) + (pbuf[n] - '0');
84+ n++;
85+ }
86+
87+ return n;
88+}
89+#endif /* NF_NEED_STRTOU32 */
90+
91+/*
92+ * Given a buffer and length, advance to the next line and mark the current
93+ * line.
94+ */
95+#ifdef NF_NEED_NEXTLINE
96+static int
97+nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
98+{
99+ uint off = *poff;
100+ uint physlen = 0;
101+
102+ if (off >= len)
103+ {
104+ return 0;
105+ }
106+
107+ while (p[off] != '\n')
108+ {
109+ if (len-off <= 1)
110+ {
111+ return 0;
112+ }
113+
114+ physlen++;
115+ off++;
116+ }
117+
118+ /* if we saw a crlf, physlen needs adjusted */
119+ if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
120+ {
121+ physlen--;
122+ }
123+
124+ /* advance past the newline */
125+ off++;
126+
127+ *plineoff = *poff;
128+ *plinelen = physlen;
129+ *poff = off;
130+
131+ return 1;
132+}
133+#endif /* NF_NEED_NEXTLINE */
134+
135+#endif /* __KERNEL__ */
136+
137+#endif /* _NETFILTER_HELPERS_H */
138diff -purN linux-2.6.23-rc2.orig/include/linux/netfilter_mime.h linux-2.6.23-rc2/include/linux/netfilter_mime.h
139--- linux-2.6.23-rc2.orig/include/linux/netfilter_mime.h 1970-01-01 01:00:00.000000000 +0100
140+++ linux-2.6.23-rc2/include/linux/netfilter_mime.h 2007-08-05 10:53:55.000000000 +0200
141@@ -0,0 +1,89 @@
142+/*
143+ * MIME functions for netfilter modules. This file provides implementations
144+ * for basic MIME parsing. MIME headers are used in many protocols, such as
145+ * HTTP, RTSP, SIP, etc.
146+ *
147+ * gcc will warn for defined but unused functions, so we only include the
148+ * functions requested. The following macros are used:
149+ * NF_NEED_MIME_NEXTLINE nf_mime_nextline()
150+ */
151+#ifndef _NETFILTER_MIME_H
152+#define _NETFILTER_MIME_H
153+
154+/* Only include these functions for kernel code. */
155+#ifdef __KERNEL__
156+
157+#include <linux/ctype.h>
158+
159+/*
160+ * Given a buffer and length, advance to the next line and mark the current
161+ * line. If the current line is empty, *plinelen will be set to zero. If
162+ * not, it will be set to the actual line length (including CRLF).
163+ *
164+ * 'line' in this context means logical line (includes LWS continuations).
165+ * Returns 1 on success, 0 on failure.
166+ */
167+#ifdef NF_NEED_MIME_NEXTLINE
168+static int
169+nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
170+{
171+ uint off = *poff;
172+ uint physlen = 0;
173+ int is_first_line = 1;
174+
175+ if (off >= len)
176+ {
177+ return 0;
178+ }
179+
180+ do
181+ {
182+ while (p[off] != '\n')
183+ {
184+ if (len-off <= 1)
185+ {
186+ return 0;
187+ }
188+
189+ physlen++;
190+ off++;
191+ }
192+
193+ /* if we saw a crlf, physlen needs adjusted */
194+ if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
195+ {
196+ physlen--;
197+ }
198+
199+ /* advance past the newline */
200+ off++;
201+
202+ /* check for an empty line */
203+ if (physlen == 0)
204+ {
205+ break;
206+ }
207+
208+ /* check for colon on the first physical line */
209+ if (is_first_line)
210+ {
211+ is_first_line = 0;
212+ if (memchr(p+(*poff), ':', physlen) == NULL)
213+ {
214+ return 0;
215+ }
216+ }
217+ }
218+ while (p[off] == ' ' || p[off] == '\t');
219+
220+ *plineoff = *poff;
221+ *plinelen = (physlen == 0) ? 0 : (off - *poff);
222+ *poff = off;
223+
224+ return 1;
225+}
226+#endif /* NF_NEED_MIME_NEXTLINE */
227+
228+#endif /* __KERNEL__ */
229+
230+#endif /* _NETFILTER_MIME_H */
231diff -purN linux-2.6.23-rc2.orig/net/ipv4/netfilter/Kconfig linux-2.6.23-rc2/net/ipv4/netfilter/Kconfig
232--- linux-2.6.23-rc2.orig/net/ipv4/netfilter/Kconfig 2007-08-05 21:17:01.000000000 +0200
233+++ linux-2.6.23-rc2/net/ipv4/netfilter/Kconfig 2007-08-05 11:04:59.000000000 +0200
234@@ -274,6 +274,11 @@ config NF_NAT_IRC
235 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
236 default NF_NAT && NF_CONNTRACK_IRC
237
238+config NF_NAT_RTSP
239+ tristate
240+ depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
241+ default NF_NAT && NF_CONNTRACK_RTSP
242+
243 config NF_NAT_TFTP
244 tristate
245 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
246diff -purN linux-2.6.23-rc2.orig/net/ipv4/netfilter/Makefile linux-2.6.23-rc2/net/ipv4/netfilter/Makefile
247--- linux-2.6.23-rc2.orig/net/ipv4/netfilter/Makefile 2007-08-05 21:14:19.000000000 +0200
248+++ linux-2.6.23-rc2/net/ipv4/netfilter/Makefile 2007-08-05 10:53:55.000000000 +0200
249@@ -0,0 +0,1 @@ obj-$(CONFIG_NF_NAT_AMANDA) += nf_nat_am
250+obj-$(CONFIG_NF_NAT_RTSP) += nf_nat_rtsp.o
251diff -purN linux-2.6.23-rc2.orig/net/netfilter/Kconfig linux-2.6.23-rc2/net/netfilter/Kconfig
252--- linux-2.6.23-rc2.orig/net/netfilter/Kconfig 2007-08-05 21:17:02.000000000 +0200
253+++ linux-2.6.23-rc2/net/netfilter/Kconfig 2007-08-05 11:04:59.000000000 +0200
254@@ -249,6 +249,16 @
255 If you want to compile it as a module, say M here and read
256 <file:Documentation/modules.txt>. If unsure, say `Y'.
257
258+config NF_CONNTRACK_RTSP
259+ tristate "RTSP protocol support"
260+ depends on NF_CONNTRACK
261+ help
262+ Support the RTSP protocol. This allows UDP transports to be setup
263+ properly, including RTP and RDT.
264+
265+ If you want to compile it as a module, say 'M' here and read
266+ Documentation/modules.txt. If unsure, say 'Y'.
267+
268 config NF_CT_NETLINK
269 tristate 'Connection tracking netlink interface (EXPERIMENTAL)'
270 depends on EXPERIMENTAL && NF_CONNTRACK && NETFILTER_NETLINK
271diff -purN linux-2.6.23-rc2.orig/net/netfilter/Makefile linux-2.6.23-rc2/net/netfilter/Makefile
272--- linux-2.6.23-rc2.orig/net/netfilter/Makefile 2007-08-05 21:17:02.000000000 +0200
273+++ linux-2.6.23-rc2/net/netfilter/Makefile 2007-08-05 11:04:59.000000000 +0200
274@@ -0,0 +0,1 @@
275+obj-$(CONFIG_NF_CONNTRACK_RTSP) += nf_conntrack_rtsp.o
276diff -purN linux-2.6.24-rc1.orig/include/linux/netfilter/nf_conntrack_rtsp.h linux-2.6.24-rc1/include/linux/netfilter/nf_conntrack_rtsp.h
277--- linux-2.6.24-rc1.orig/include/linux/netfilter/nf_conntrack_rtsp.h 1970-01-01 01:00:00.000000000 +0100
278+++ linux-2.6.24-rc1/include/linux/netfilter/nf_conntrack_rtsp.h 2007-11-10 17:16:36.000000000 +0100
279@@ -0,0 +1,63 @@
280+/*
281+ * RTSP extension for IP connection tracking.
282+ * (C) 2003 by Tom Marshall <tmarshall at real.com>
283+ * based on ip_conntrack_irc.h
284+ *
285+ * This program is free software; you can redistribute it and/or
286+ * modify it under the terms of the GNU General Public License
287+ * as published by the Free Software Foundation; either version
288+ * 2 of the License, or (at your option) any later version.
289+ */
290+#ifndef _IP_CONNTRACK_RTSP_H
291+#define _IP_CONNTRACK_RTSP_H
292+
293+//#define IP_NF_RTSP_DEBUG 1
294+#define IP_NF_RTSP_VERSION "0.6.21"
295+
296+#ifdef __KERNEL__
297+/* port block types */
298+typedef enum {
299+ pb_single, /* client_port=x */
300+ pb_range, /* client_port=x-y */
301+ pb_discon /* client_port=x/y (rtspbis) */
302+} portblock_t;
303+
304+/* We record seq number and length of rtsp headers here, all in host order. */
305+
306+/*
307+ * This structure is per expected connection. It is a member of struct
308+ * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
309+ * there and we are expected to only store the length of the data which
310+ * needs replaced. If a packet contains multiple RTSP messages, we create
311+ * one expected connection per message.
312+ *
313+ * We use these variables to mark the entire header block. This may seem
314+ * like overkill, but the nature of RTSP requires it. A header may appear
315+ * multiple times in a message. We must treat two Transport headers the
316+ * same as one Transport header with two entries.
317+ */
318+struct ip_ct_rtsp_expect
319+{
320+ u_int32_t len; /* length of header block */
321+ portblock_t pbtype; /* Type of port block that was requested */
322+ u_int16_t loport; /* Port that was requested, low or first */
323+ u_int16_t hiport; /* Port that was requested, high or second */
324+#if 0
325+ uint method; /* RTSP method */
326+ uint cseq; /* CSeq from request */
327+#endif
328+};
329+
330+extern unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
331+ enum ip_conntrack_info ctinfo,
332+ unsigned int matchoff, unsigned int matchlen,
333+ struct ip_ct_rtsp_expect *prtspexp,
334+ struct nf_conntrack_expect *exp);
335+
336+extern void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
337+
338+#define RTSP_PORT 554
339+
340+#endif /* __KERNEL__ */
341+
342+#endif /* _IP_CONNTRACK_RTSP_H */
343diff -purN linux-2.6.24-rc1.orig/net/ipv4/netfilter/nf_nat_rtsp.c linux-2.6.24-rc1/net/ipv4/netfilter/nf_nat_rtsp.c
344--- linux-2.6.24-rc1.orig/net/ipv4/netfilter/nf_nat_rtsp.c 1970-01-01 01:00:00.000000000 +0100
345+++ linux-2.6.24-rc1/net/ipv4/netfilter/nf_nat_rtsp.c 2007-11-10 17:17:21.000000000 +0100
346@@ -0,0 +1,489 @@
347+/*
348+ * RTSP extension for TCP NAT alteration
349+ * (C) 2003 by Tom Marshall <tmarshall at real.com>
350+ * based on ip_nat_irc.c
351+ *
352+ * This program is free software; you can redistribute it and/or
353+ * modify it under the terms of the GNU General Public License
354+ * as published by the Free Software Foundation; either version
355+ * 2 of the License, or (at your option) any later version.
356+ *
357+ * Module load syntax:
358+ * insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
359+ * stunaddr=<address>
360+ * destaction=[auto|strip|none]
361+ *
362+ * If no ports are specified, the default will be port 554 only.
363+ *
364+ * stunaddr specifies the address used to detect that a client is using STUN.
365+ * If this address is seen in the destination parameter, it is assumed that
366+ * the client has already punched a UDP hole in the firewall, so we don't
367+ * mangle the client_port. If none is specified, it is autodetected. It
368+ * only needs to be set if you have multiple levels of NAT. It should be
369+ * set to the external address that the STUN clients detect. Note that in
370+ * this case, it will not be possible for clients to use UDP with servers
371+ * between the NATs.
372+ *
373+ * If no destaction is specified, auto is used.
374+ * destaction=auto: strip destination parameter if it is not stunaddr.
375+ * destaction=strip: always strip destination parameter (not recommended).
376+ * destaction=none: do not touch destination parameter (not recommended).
377+ */
378+
379+#include <linux/module.h>
380+#include <net/tcp.h>
381+#include <net/netfilter/nf_nat_helper.h>
382+#include <net/netfilter/nf_nat_rule.h>
383+#include <linux/netfilter/nf_conntrack_rtsp.h>
384+#include <net/netfilter/nf_conntrack_expect.h>
385+
386+#include <linux/inet.h>
387+#include <linux/ctype.h>
388+#define NF_NEED_STRNCASECMP
389+#define NF_NEED_STRTOU16
390+#include <linux/netfilter_helpers.h>
391+#define NF_NEED_MIME_NEXTLINE
392+#include <linux/netfilter_mime.h>
393+
394+#define MAX_PORTS 8
395+#define DSTACT_AUTO 0
396+#define DSTACT_STRIP 1
397+#define DSTACT_NONE 2
398+
399+static char* stunaddr = NULL;
400+static char* destaction = NULL;
401+
402+static u_int32_t extip = 0;
403+static int dstact = 0;
404+
405+MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
406+MODULE_DESCRIPTION("RTSP network address translation module");
407+MODULE_LICENSE("GPL");
408+module_param(stunaddr, charp, 0644);
409+MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
410+module_param(destaction, charp, 0644);
411+MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
412+
413+#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
414+
415+/*** helper functions ***/
416+
417+static void
418+get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
419+{
420+ struct iphdr* iph = ip_hdr(skb);
421+ struct tcphdr* tcph = (void *)iph + ip_hdrlen(skb);
422+
423+ *pptcpdata = (char*)tcph + tcph->doff*4;
424+ *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
425+}
426+
427+/*** nat functions ***/
428+
429+/*
430+ * Mangle the "Transport:" header:
431+ * - Replace all occurences of "client_port=<spec>"
432+ * - Handle destination parameter
433+ *
434+ * In:
435+ * ct, ctinfo = conntrack context
436+ * skb = packet
437+ * tranoff = Transport header offset from TCP data
438+ * tranlen = Transport header length (incl. CRLF)
439+ * rport_lo = replacement low port (host endian)
440+ * rport_hi = replacement high port (host endian)
441+ *
442+ * Returns packet size difference.
443+ *
444+ * Assumes that a complete transport header is present, ending with CR or LF
445+ */
446+static int
447+rtsp_mangle_tran(enum ip_conntrack_info ctinfo,
448+ struct nf_conntrack_expect* exp,
449+ struct ip_ct_rtsp_expect* prtspexp,
450+ struct sk_buff* skb, uint tranoff, uint tranlen)
451+{
452+ char* ptcp;
453+ uint tcplen;
454+ char* ptran;
455+ char rbuf1[16]; /* Replacement buffer (one port) */
456+ uint rbuf1len; /* Replacement len (one port) */
457+ char rbufa[16]; /* Replacement buffer (all ports) */
458+ uint rbufalen; /* Replacement len (all ports) */
459+ u_int32_t newip;
460+ u_int16_t loport, hiport;
461+ uint off = 0;
462+ uint diff; /* Number of bytes we removed */
463+
464+ struct nf_conn *ct = exp->master;
465+ struct nf_conntrack_tuple *t;
466+
467+ char szextaddr[15+1];
468+ uint extaddrlen;
469+ int is_stun;
470+
471+ get_skb_tcpdata(skb, &ptcp, &tcplen);
472+ ptran = ptcp+tranoff;
473+
474+ if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
475+ tranlen < 10 || !iseol(ptran[tranlen-1]) ||
476+ nf_strncasecmp(ptran, "Transport:", 10) != 0)
477+ {
478+ pr_info("sanity check failed\n");
479+ return 0;
480+ }
481+ off += 10;
482+ SKIP_WSPACE(ptcp+tranoff, tranlen, off);
483+
484+ newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
485+ t = &exp->tuple;
486+ t->dst.u3.ip = newip;
487+
488+ extaddrlen = extip ? sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(extip))
489+ : sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(newip));
490+ pr_debug("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
491+
492+ rbuf1len = rbufalen = 0;
493+ switch (prtspexp->pbtype)
494+ {
495+ case pb_single:
496+ for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
497+ {
498+ t->dst.u.udp.port = htons(loport);
499+ if (nf_ct_expect_related(exp) == 0)
500+ {
501+ pr_debug("using port %hu\n", loport);
502+ break;
503+ }
504+ }
505+ if (loport != 0)
506+ {
507+ rbuf1len = sprintf(rbuf1, "%hu", loport);
508+ rbufalen = sprintf(rbufa, "%hu", loport);
509+ }
510+ break;
511+ case pb_range:
512+ for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
513+ {
514+ t->dst.u.udp.port = htons(loport);
515+ if (nf_ct_expect_related(exp) == 0)
516+ {
517+ hiport = loport + 1; //~exp->mask.dst.u.udp.port;
518+ pr_debug("using ports %hu-%hu\n", loport, hiport);
519+ break;
520+ }
521+ }
522+ if (loport != 0)
523+ {
524+ rbuf1len = sprintf(rbuf1, "%hu", loport);
525+ rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
526+ }
527+ break;
528+ case pb_discon:
529+ for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
530+ {
531+ t->dst.u.udp.port = htons(loport);
532+ if (nf_ct_expect_related(exp) == 0)
533+ {
534+ pr_debug("using port %hu (1 of 2)\n", loport);
535+ break;
536+ }
537+ }
538+ for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
539+ {
540+ t->dst.u.udp.port = htons(hiport);
541+ if (nf_ct_expect_related(exp) == 0)
542+ {
543+ pr_debug("using port %hu (2 of 2)\n", hiport);
544+ break;
545+ }
546+ }
547+ if (loport != 0 && hiport != 0)
548+ {
549+ rbuf1len = sprintf(rbuf1, "%hu", loport);
550+ if (hiport == loport+1)
551+ {
552+ rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
553+ }
554+ else
555+ {
556+ rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
557+ }
558+ }
559+ break;
560+ }
561+
562+ if (rbuf1len == 0)
563+ {
564+ return 0; /* cannot get replacement port(s) */
565+ }
566+
567+ /* Transport: tran;field;field=val,tran;field;field=val,... */
568+ while (off < tranlen)
569+ {
570+ uint saveoff;
571+ const char* pparamend;
572+ uint nextparamoff;
573+
574+ pparamend = memchr(ptran+off, ',', tranlen-off);
575+ pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
576+ nextparamoff = pparamend-ptcp;
577+
578+ /*
579+ * We pass over each param twice. On the first pass, we look for a
580+ * destination= field. It is handled by the security policy. If it
581+ * is present, allowed, and equal to our external address, we assume
582+ * that STUN is being used and we leave the client_port= field alone.
583+ */
584+ is_stun = 0;
585+ saveoff = off;
586+ while (off < nextparamoff)
587+ {
588+ const char* pfieldend;
589+ uint nextfieldoff;
590+
591+ pfieldend = memchr(ptran+off, ';', nextparamoff-off);
592+ nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
593+
594+ if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
595+ {
596+ if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
597+ {
598+ is_stun = 1;
599+ }
600+ if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
601+ {
602+ diff = nextfieldoff-off;
603+ if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
604+ off, diff, NULL, 0))
605+ {
606+ /* mangle failed, all we can do is bail */
607+ nf_ct_unexpect_related(exp);
608+ return 0;
609+ }
610+ get_skb_tcpdata(skb, &ptcp, &tcplen);
611+ ptran = ptcp+tranoff;
612+ tranlen -= diff;
613+ nextparamoff -= diff;
614+ nextfieldoff -= diff;
615+ }
616+ }
617+
618+ off = nextfieldoff;
619+ }
620+ if (is_stun)
621+ {
622+ continue;
623+ }
624+ off = saveoff;
625+ while (off < nextparamoff)
626+ {
627+ const char* pfieldend;
628+ uint nextfieldoff;
629+
630+ pfieldend = memchr(ptran+off, ';', nextparamoff-off);
631+ nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
632+
633+ if (strncmp(ptran+off, "client_port=", 12) == 0)
634+ {
635+ u_int16_t port;
636+ uint numlen;
637+ uint origoff;
638+ uint origlen;
639+ char* rbuf = rbuf1;
640+ uint rbuflen = rbuf1len;
641+
642+ off += 12;
643+ origoff = (ptran-ptcp)+off;
644+ origlen = 0;
645+ numlen = nf_strtou16(ptran+off, &port);
646+ off += numlen;
647+ origlen += numlen;
648+ if (port != prtspexp->loport)
649+ {
650+ pr_debug("multiple ports found, port %hu ignored\n", port);
651+ }
652+ else
653+ {
654+ if (ptran[off] == '-' || ptran[off] == '/')
655+ {
656+ off++;
657+ origlen++;
658+ numlen = nf_strtou16(ptran+off, &port);
659+ off += numlen;
660+ origlen += numlen;
661+ rbuf = rbufa;
662+ rbuflen = rbufalen;
663+ }
664+
665+ /*
666+ * note we cannot just memcpy() if the sizes are the same.
667+ * the mangle function does skb resizing, checks for a
668+ * cloned skb, and updates the checksums.
669+ *
670+ * parameter 4 below is offset from start of tcp data.
671+ */
672+ diff = origlen-rbuflen;
673+ if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
674+ origoff, origlen, rbuf, rbuflen))
675+ {
676+ /* mangle failed, all we can do is bail */
677+ nf_ct_unexpect_related(exp);
678+ return 0;
679+ }
680+ get_skb_tcpdata(skb, &ptcp, &tcplen);
681+ ptran = ptcp+tranoff;
682+ tranlen -= diff;
683+ nextparamoff -= diff;
684+ nextfieldoff -= diff;
685+ }
686+ }
687+
688+ off = nextfieldoff;
689+ }
690+
691+ off = nextparamoff;
692+ }
693+
694+ return 1;
695+}
696+
697+static uint
698+help_out(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
699+ unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
700+ struct nf_conntrack_expect* exp)
701+{
702+ char* ptcp;
703+ uint tcplen;
704+ uint hdrsoff;
705+ uint hdrslen;
706+ uint lineoff;
707+ uint linelen;
708+ uint off;
709+
710+ //struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
711+ //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
712+
713+ get_skb_tcpdata(skb, &ptcp, &tcplen);
714+ hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
715+ hdrslen = matchlen;
716+ off = hdrsoff;
717+ pr_debug("NAT rtsp help_out\n");
718+
719+ while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
720+ {
721+ if (linelen == 0)
722+ {
723+ break;
724+ }
725+ if (off > hdrsoff+hdrslen)
726+ {
727+ pr_info("!! overrun !!");
728+ break;
729+ }
730+ pr_debug("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
731+
732+ if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
733+ {
734+ uint oldtcplen = tcplen;
735+ pr_debug("hdr: Transport\n");
736+ if (!rtsp_mangle_tran(ctinfo, exp, prtspexp, skb, lineoff, linelen))
737+ {
738+ pr_debug("hdr: Transport mangle failed");
739+ break;
740+ }
741+ get_skb_tcpdata(skb, &ptcp, &tcplen);
742+ hdrslen -= (oldtcplen-tcplen);
743+ off -= (oldtcplen-tcplen);
744+ lineoff -= (oldtcplen-tcplen);
745+ linelen -= (oldtcplen-tcplen);
746+ pr_debug("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
747+ }
748+ }
749+
750+ return NF_ACCEPT;
751+}
752+
753+static unsigned int
754+help(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
755+ unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
756+ struct nf_conntrack_expect* exp)
757+{
758+ int dir = CTINFO2DIR(ctinfo);
759+ int rc = NF_ACCEPT;
760+
761+ switch (dir)
762+ {
763+ case IP_CT_DIR_ORIGINAL:
764+ rc = help_out(skb, ctinfo, matchoff, matchlen, prtspexp, exp);
765+ break;
766+ case IP_CT_DIR_REPLY:
767+ pr_debug("unmangle ! %u\n", ctinfo);
768+ /* XXX: unmangle */
769+ rc = NF_ACCEPT;
770+ break;
771+ }
772+ //UNLOCK_BH(&ip_rtsp_lock);
773+
774+ return rc;
775+}
776+
777+static void expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
778+{
779+ struct nf_nat_multi_range_compat mr;
780+ u_int32_t newdstip, newsrcip, newip;
781+
782+ struct nf_conn *master = ct->master;
783+
784+ newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
785+ newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
786+ //FIXME (how to port that ?)
787+ //code from 2.4 : newip = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) ? newsrcip : newdstip;
788+ newip = newdstip;
789+
790+ pr_debug("newsrcip=%u.%u.%u.%u, newdstip=%u.%u.%u.%u, newip=%u.%u.%u.%u\n",
791+ NIPQUAD(newsrcip), NIPQUAD(newdstip), NIPQUAD(newip));
792+
793+ mr.rangesize = 1;
794+ // We don't want to manip the per-protocol, just the IPs.
795+ mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
796+ mr.range[0].min_ip = mr.range[0].max_ip = newip;
797+
ee7951b0 798+ nf_nat_setup_info(ct, &mr.range[0], NF_INET_PRE_ROUTING);
1c44405b 799+}
800+
801+
802+static void __exit fini(void)
803+{
804+ nf_nat_rtsp_hook = NULL;
805+ nf_nat_rtsp_hook_expectfn = NULL;
806+ synchronize_net();
807+}
808+
809+static int __init init(void)
810+{
811+ printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
812+
813+ BUG_ON(nf_nat_rtsp_hook);
814+ nf_nat_rtsp_hook = help;
815+ nf_nat_rtsp_hook_expectfn = &expected;
816+
817+ if (stunaddr != NULL)
818+ extip = in_aton(stunaddr);
819+
820+ if (destaction != NULL) {
821+ if (strcmp(destaction, "auto") == 0)
822+ dstact = DSTACT_AUTO;
823+
824+ if (strcmp(destaction, "strip") == 0)
825+ dstact = DSTACT_STRIP;
826+
827+ if (strcmp(destaction, "none") == 0)
828+ dstact = DSTACT_NONE;
829+ }
830+
831+ return 0;
832+}
833+
834+module_init(init);
835+module_exit(fini);
836diff -purN linux-2.6.24-rc1.orig/net/netfilter/nf_conntrack_rtsp.c linux-2.6.24-rc1/net/netfilter/nf_conntrack_rtsp.c
837--- linux-2.6.24-rc1.orig/net/netfilter/nf_conntrack_rtsp.c 1970-01-01 01:00:00.000000000 +0100
838+++ linux-2.6.24-rc1/net/netfilter/nf_conntrack_rtsp.c 2007-11-10 17:17:43.000000000 +0100
839@@ -0,0 +1,515 @@
840+/*
841+ * RTSP extension for IP connection tracking
842+ * (C) 2003 by Tom Marshall <tmarshall at real.com>
843+ * based on ip_conntrack_irc.c
844+ *
845+ * This program is free software; you can redistribute it and/or
846+ * modify it under the terms of the GNU General Public License
847+ * as published by the Free Software Foundation; either version
848+ * 2 of the License, or (at your option) any later version.
849+ *
850+ * Module load syntax:
851+ * insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
852+ * max_outstanding=n setup_timeout=secs
853+ *
854+ * If no ports are specified, the default will be port 554.
855+ *
856+ * With max_outstanding you can define the maximum number of not yet
857+ * answered SETUP requests per RTSP session (default 8).
858+ * With setup_timeout you can specify how long the system waits for
859+ * an expected data channel (default 300 seconds).
860+ *
861+ * 2005-02-13: Harald Welte <laforge at netfilter.org>
862+ * - port to 2.6
863+ * - update to recent post-2.6.11 api changes
864+ * 2006-09-14: Steven Van Acker <deepstar at singularity.be>
865+ * - removed calls to NAT code from conntrack helper: NAT no longer needed to use rtsp-conntrack
866+ * 2007-04-18: Michael Guntsche <mike at it-loops.com>
867+ * - Port to new NF API
868+ */
869+
870+#include <linux/module.h>
871+#include <linux/netfilter.h>
872+#include <linux/ip.h>
873+#include <linux/inet.h>
874+#include <net/tcp.h>
875+
876+#include <net/netfilter/nf_conntrack.h>
877+#include <net/netfilter/nf_conntrack_expect.h>
878+#include <net/netfilter/nf_conntrack_helper.h>
879+#include <linux/netfilter/nf_conntrack_rtsp.h>
880+
881+#define NF_NEED_STRNCASECMP
882+#define NF_NEED_STRTOU16
883+#define NF_NEED_STRTOU32
884+#define NF_NEED_NEXTLINE
885+#include <linux/netfilter_helpers.h>
886+#define NF_NEED_MIME_NEXTLINE
887+#include <linux/netfilter_mime.h>
888+
889+#include <linux/ctype.h>
890+#define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
891+
892+#define MAX_PORTS 8
893+static int ports[MAX_PORTS];
894+static int num_ports = 0;
895+static int max_outstanding = 8;
896+static unsigned int setup_timeout = 300;
897+
898+MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
899+MODULE_DESCRIPTION("RTSP connection tracking module");
900+MODULE_LICENSE("GPL");
901+module_param_array(ports, int, &num_ports, 0400);
902+MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
903+module_param(max_outstanding, int, 0400);
904+MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
905+module_param(setup_timeout, int, 0400);
906+MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
907+
908+static char *rtsp_buffer;
909+static DEFINE_SPINLOCK(rtsp_buffer_lock);
910+
911+unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
912+ enum ip_conntrack_info ctinfo,
913+ unsigned int matchoff, unsigned int matchlen,struct ip_ct_rtsp_expect* prtspexp,
914+ struct nf_conntrack_expect *exp);
915+void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
916+
917+EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook);
918+
919+/*
920+ * Max mappings we will allow for one RTSP connection (for RTP, the number
921+ * of allocated ports is twice this value). Note that SMIL burns a lot of
922+ * ports so keep this reasonably high. If this is too low, you will see a
923+ * lot of "no free client map entries" messages.
924+ */
925+#define MAX_PORT_MAPS 16
926+
927+/*** default port list was here in the masq code: 554, 3030, 4040 ***/
928+
929+#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
930+
931+/*
932+ * Parse an RTSP packet.
933+ *
934+ * Returns zero if parsing failed.
935+ *
936+ * Parameters:
937+ * IN ptcp tcp data pointer
938+ * IN tcplen tcp data len
939+ * IN/OUT ptcpoff points to current tcp offset
940+ * OUT phdrsoff set to offset of rtsp headers
941+ * OUT phdrslen set to length of rtsp headers
942+ * OUT pcseqoff set to offset of CSeq header
943+ * OUT pcseqlen set to length of CSeq header
944+ */
945+static int
946+rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
947+ uint* phdrsoff, uint* phdrslen,
948+ uint* pcseqoff, uint* pcseqlen,
949+ uint* transoff, uint* translen)
950+{
951+ uint entitylen = 0;
952+ uint lineoff;
953+ uint linelen;
954+
955+ if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
956+ return 0;
957+
958+ *phdrsoff = *ptcpoff;
959+ while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
960+ if (linelen == 0) {
961+ if (entitylen > 0)
962+ *ptcpoff += min(entitylen, tcplen - *ptcpoff);
963+ break;
964+ }
965+ if (lineoff+linelen > tcplen) {
966+ pr_info("!! overrun !!\n");
967+ break;
968+ }
969+
970+ if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
971+ *pcseqoff = lineoff;
972+ *pcseqlen = linelen;
973+ }
974+
975+ if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
976+ *transoff = lineoff;
977+ *translen = linelen;
978+ }
979+
980+ if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0) {
981+ uint off = lineoff+15;
982+ SKIP_WSPACE(ptcp+lineoff, linelen, off);
983+ nf_strtou32(ptcp+off, &entitylen);
984+ }
985+ }
986+ *phdrslen = (*ptcpoff) - (*phdrsoff);
987+
988+ return 1;
989+}
990+
991+/*
992+ * Find lo/hi client ports (if any) in transport header
993+ * In:
994+ * ptcp, tcplen = packet
995+ * tranoff, tranlen = buffer to search
996+ *
997+ * Out:
998+ * pport_lo, pport_hi = lo/hi ports (host endian)
999+ *
1000+ * Returns nonzero if any client ports found
1001+ *
1002+ * Note: it is valid (and expected) for the client to request multiple
1003+ * transports, so we need to parse the entire line.
1004+ */
1005+static int
1006+rtsp_parse_transport(char* ptran, uint tranlen,
1007+ struct ip_ct_rtsp_expect* prtspexp)
1008+{
1009+ int rc = 0;
1010+ uint off = 0;
1011+
1012+ if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
1013+ nf_strncasecmp(ptran, "Transport:", 10) != 0) {
1014+ pr_info("sanity check failed\n");
1015+ return 0;
1016+ }
1017+
1018+ pr_debug("tran='%.*s'\n", (int)tranlen, ptran);
1019+ off += 10;
1020+ SKIP_WSPACE(ptran, tranlen, off);
1021+
1022+ /* Transport: tran;field;field=val,tran;field;field=val,... */
1023+ while (off < tranlen) {
1024+ const char* pparamend;
1025+ uint nextparamoff;
1026+
1027+ pparamend = memchr(ptran+off, ',', tranlen-off);
1028+ pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
1029+ nextparamoff = pparamend-ptran;
1030+
1031+ while (off < nextparamoff) {
1032+ const char* pfieldend;
1033+ uint nextfieldoff;
1034+
1035+ pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1036+ nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1037+
1038+ if (strncmp(ptran+off, "client_port=", 12) == 0) {
1039+ u_int16_t port;
1040+ uint numlen;
1041+
1042+ off += 12;
1043+ numlen = nf_strtou16(ptran+off, &port);
1044+ off += numlen;
1045+ if (prtspexp->loport != 0 && prtspexp->loport != port)
1046+ pr_debug("multiple ports found, port %hu ignored\n", port);
1047+ else {
1048+ pr_debug("lo port found : %hu\n", port);
1049+ prtspexp->loport = prtspexp->hiport = port;
1050+ if (ptran[off] == '-') {
1051+ off++;
1052+ numlen = nf_strtou16(ptran+off, &port);
1053+ off += numlen;
1054+ prtspexp->pbtype = pb_range;
1055+ prtspexp->hiport = port;
1056+
1057+ // If we have a range, assume rtp:
1058+ // loport must be even, hiport must be loport+1
1059+ if ((prtspexp->loport & 0x0001) != 0 ||
1060+ prtspexp->hiport != prtspexp->loport+1) {
1061+ pr_debug("incorrect range: %hu-%hu, correcting\n",
1062+ prtspexp->loport, prtspexp->hiport);
1063+ prtspexp->loport &= 0xfffe;
1064+ prtspexp->hiport = prtspexp->loport+1;
1065+ }
1066+ } else if (ptran[off] == '/') {
1067+ off++;
1068+ numlen = nf_strtou16(ptran+off, &port);
1069+ off += numlen;
1070+ prtspexp->pbtype = pb_discon;
1071+ prtspexp->hiport = port;
1072+ }
1073+ rc = 1;
1074+ }
1075+ }
1076+
1077+ /*
1078+ * Note we don't look for the destination parameter here.
1079+ * If we are using NAT, the NAT module will handle it. If not,
1080+ * and the client is sending packets elsewhere, the expectation
1081+ * will quietly time out.
1082+ */
1083+
1084+ off = nextfieldoff;
1085+ }
1086+
1087+ off = nextparamoff;
1088+ }
1089+
1090+ return rc;
1091+}
1092+
1093+void expected(struct nf_conn *ct, struct nf_conntrack_expect *exp)
1094+{
1095+ typeof(nf_nat_rtsp_hook_expectfn) nf_nat_rtsp_expectfn;
1096+ nf_nat_rtsp_expectfn = rcu_dereference(nf_nat_rtsp_hook_expectfn);
1097+ if(nf_nat_rtsp_expectfn && ct->master->status & IPS_NAT_MASK) {
1098+ nf_nat_rtsp_expectfn(ct,exp);
1099+ }
1100+}
1101+
1102+/*** conntrack functions ***/
1103+
1104+/* outbound packet: client->server */
1105+
1106+static inline int
1107+help_out(struct sk_buff *skb, unsigned char *rb_ptr, unsigned int datalen,
1108+ struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1109+{
1110+ struct ip_ct_rtsp_expect expinfo;
1111+
1112+ int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
1113+ //struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
1114+ //uint tcplen = pktlen - iph->ihl * 4;
1115+ char* pdata = rb_ptr;
1116+ //uint datalen = tcplen - tcph->doff * 4;
1117+ uint dataoff = 0;
1118+ int ret = NF_ACCEPT;
1119+
1120+ struct nf_conntrack_expect *exp;
1121+
1122+ __be16 be_loport;
1123+
1124+ typeof(nf_nat_rtsp_hook) nf_nat_rtsp;
1125+
1126+ memset(&expinfo, 0, sizeof(expinfo));
1127+
1128+ while (dataoff < datalen) {
1129+ uint cmdoff = dataoff;
1130+ uint hdrsoff = 0;
1131+ uint hdrslen = 0;
1132+ uint cseqoff = 0;
1133+ uint cseqlen = 0;
1134+ uint transoff = 0;
1135+ uint translen = 0;
1136+ uint off;
1137+
1138+ if (!rtsp_parse_message(pdata, datalen, &dataoff,
1139+ &hdrsoff, &hdrslen,
1140+ &cseqoff, &cseqlen,
1141+ &transoff, &translen))
1142+ break; /* not a valid message */
1143+
1144+ if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
1145+ continue; /* not a SETUP message */
1146+ pr_debug("found a setup message\n");
1147+
1148+ off = 0;
1149+ if(translen) {
1150+ rtsp_parse_transport(pdata+transoff, translen, &expinfo);
1151+ }
1152+
1153+ if (expinfo.loport == 0) {
1154+ pr_debug("no udp transports found\n");
1155+ continue; /* no udp transports found */
1156+ }
1157+
1158+ pr_debug("udp transport found, ports=(%d,%hu,%hu)\n",
1159+ (int)expinfo.pbtype, expinfo.loport, expinfo.hiport);
1160+
1161+ exp = nf_ct_expect_alloc(ct);
1162+ if (!exp) {
1163+ ret = NF_DROP;
1164+ goto out;
1165+ }
1166+
1167+ be_loport = htons(expinfo.loport);
1168+
1169+ nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
1170+ &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3,
1171+ IPPROTO_UDP, NULL, &be_loport);
1172+
1173+ exp->master = ct;
1174+
1175+ exp->expectfn = expected;
1176+ exp->flags = 0;
1177+
1178+ if (expinfo.pbtype == pb_range) {
1179+ pr_debug("Changing expectation mask to handle multiple ports\n");
1180+ //exp->mask.dst.u.udp.port = 0xfffe;
1181+ }
1182+
1183+ pr_debug("expect_related %u.%u.%u.%u:%u-%u.%u.%u.%u:%u\n",
1184+ NIPQUAD(exp->tuple.src.u3.ip),
1185+ ntohs(exp->tuple.src.u.udp.port),
1186+ NIPQUAD(exp->tuple.dst.u3.ip),
1187+ ntohs(exp->tuple.dst.u.udp.port));
1188+
1189+ nf_nat_rtsp = rcu_dereference(nf_nat_rtsp_hook);
1190+ if (nf_nat_rtsp && ct->status & IPS_NAT_MASK)
1191+ /* pass the request off to the nat helper */
1192+ ret = nf_nat_rtsp(skb, ctinfo, hdrsoff, hdrslen, &expinfo, exp);
1193+ else if (nf_ct_expect_related(exp) != 0) {
1194+ pr_info("nf_conntrack_expect_related failed\n");
1195+ ret = NF_DROP;
1196+ }
1197+ nf_ct_expect_put(exp);
1198+ goto out;
1199+ }
1200+out:
1201+
1202+ return ret;
1203+}
1204+
1205+
1206+static inline int
1207+help_in(struct sk_buff *skb, size_t pktlen,
1208+ struct nf_conn* ct, enum ip_conntrack_info ctinfo)
1209+{
1210+ return NF_ACCEPT;
1211+}
1212+
1213+static int help(struct sk_buff *skb, unsigned int protoff,
1214+ struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1215+{
1216+ struct tcphdr _tcph, *th;
1217+ unsigned int dataoff, datalen;
1218+ char *rb_ptr;
1219+ int ret = NF_DROP;
1220+
1221+ /* Until there's been traffic both ways, don't look in packets. */
1222+ if (ctinfo != IP_CT_ESTABLISHED &&
1223+ ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
1224+ pr_debug("conntrackinfo = %u\n", ctinfo);
1225+ return NF_ACCEPT;
1226+ }
1227+
1228+ /* Not whole TCP header? */
1229+ th = skb_header_pointer(skb,protoff, sizeof(_tcph), &_tcph);
1230+
1231+ if (!th)
1232+ return NF_ACCEPT;
1233+
1234+ /* No data ? */
1235+ dataoff = protoff + th->doff*4;
1236+ datalen = skb->len - dataoff;
1237+ if (dataoff >= skb->len)
1238+ return NF_ACCEPT;
1239+
1240+ spin_lock_bh(&rtsp_buffer_lock);
1241+ rb_ptr = skb_header_pointer(skb, dataoff,
1242+ skb->len - dataoff, rtsp_buffer);
1243+ BUG_ON(rb_ptr == NULL);
1244+
1245+#if 0
1246+ /* Checksum invalid? Ignore. */
1247+ /* FIXME: Source route IP option packets --RR */
1248+ if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
1249+ csum_partial((char*)tcph, tcplen, 0)))
1250+ {
1251+ DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
1252+ tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1253+ return NF_ACCEPT;
1254+ }
1255+#endif
1256+
1257+ switch (CTINFO2DIR(ctinfo)) {
1258+ case IP_CT_DIR_ORIGINAL:
1259+ ret = help_out(skb, rb_ptr, datalen, ct, ctinfo);
1260+ break;
1261+ case IP_CT_DIR_REPLY:
1262+ pr_debug("IP_CT_DIR_REPLY\n");
1263+ /* inbound packet: server->client */
1264+ ret = NF_ACCEPT;
1265+ break;
1266+ }
1267+
1268+ spin_unlock_bh(&rtsp_buffer_lock);
1269+
1270+ return ret;
1271+}
1272+
1273+static struct nf_conntrack_helper rtsp_helpers[MAX_PORTS];
1274+static char rtsp_names[MAX_PORTS][10];
1275+
1276+/* This function is intentionally _NOT_ defined as __exit */
1277+static void
1278+fini(void)
1279+{
1280+ int i;
1281+ for (i = 0; i < num_ports; i++) {
1282+ pr_debug("unregistering port %d\n", ports[i]);
1283+ nf_conntrack_helper_unregister(&rtsp_helpers[i]);
1284+ }
1285+ kfree(rtsp_buffer);
1286+}
1287+
1288+static int __init
1289+init(void)
1290+{
1291+ int i, ret;
1292+ struct nf_conntrack_helper *hlpr;
1293+ char *tmpname;
1294+
1295+ printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1296+
1297+ if (max_outstanding < 1) {
1298+ printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
1299+ return -EBUSY;
1300+ }
1301+ if (setup_timeout < 0) {
1302+ printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
1303+ return -EBUSY;
1304+ }
1305+
1306+ rtsp_buffer = kmalloc(65536, GFP_KERNEL);
1307+ if (!rtsp_buffer)
1308+ return -ENOMEM;
1309+
1310+ /* If no port given, default to standard rtsp port */
1311+ if (ports[0] == 0) {
1312+ ports[0] = RTSP_PORT;
1313+ }
1314+
1315+ for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
1316+ hlpr = &rtsp_helpers[i];
1317+ memset(hlpr, 0, sizeof(struct nf_conntrack_helper));
1318+ hlpr->tuple.src.l3num = AF_INET;
1319+ hlpr->tuple.src.u.tcp.port = htons(ports[i]);
1320+ hlpr->tuple.dst.protonum = IPPROTO_TCP;
1321+ //hlpr->mask.src.u.tcp.port = 0xFFFF;
1322+ //hlpr->mask.dst.protonum = 0xFF;
1323+ hlpr->max_expected = max_outstanding;
1324+ hlpr->timeout = setup_timeout;
1325+ hlpr->me = THIS_MODULE;
1326+ hlpr->help = help;
1327+
1328+ tmpname = &rtsp_names[i][0];
1329+ if (ports[i] == RTSP_PORT) {
1330+ sprintf(tmpname, "rtsp");
1331+ } else {
1332+ sprintf(tmpname, "rtsp-%d", i);
1333+ }
1334+ hlpr->name = tmpname;
1335+
1336+ pr_debug("port #%d: %d\n", i, ports[i]);
1337+
1338+ ret = nf_conntrack_helper_register(hlpr);
1339+
1340+ if (ret) {
1341+ printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
1342+ fini();
1343+ return -EBUSY;
1344+ }
1345+ num_ports++;
1346+ }
1347+ return 0;
1348+}
1349+
1350+module_init(init);
1351+module_exit(fini);
1352+
1353+EXPORT_SYMBOL(nf_nat_rtsp_hook_expectfn);
1354+
This page took 0.173389 seconds and 4 git commands to generate.