ixp4xx: Update config-default to 2.6.24
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.22 / 190-netfilter_rtsp.patch
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
4 @@ -0,0 +1,63 @@
5 +/*
6 + * RTSP extension for IP connection tracking.
7 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
8 + * based on ip_conntrack_irc.h
9 + *
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.
14 + */
15 +#ifndef _IP_CONNTRACK_RTSP_H
16 +#define _IP_CONNTRACK_RTSP_H
17 +
18 +//#define IP_NF_RTSP_DEBUG 1
19 +#define IP_NF_RTSP_VERSION "0.6.21"
20 +
21 +#ifdef __KERNEL__
22 +/* port block types */
23 +typedef enum {
24 +    pb_single,  /* client_port=x */
25 +    pb_range,   /* client_port=x-y */
26 +    pb_discon   /* client_port=x/y (rtspbis) */
27 +} portblock_t;
28 +
29 +/* We record seq number and length of rtsp headers here, all in host order. */
30 +
31 +/*
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.
37 + *
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.
42 + */
43 +struct ip_ct_rtsp_expect
44 +{
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 */
49 +#if 0
50 +    uint        method;     /* RTSP method */
51 +    uint        cseq;       /* CSeq from request */
52 +#endif
53 +};
54 +
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);
60 +
61 +extern void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
62 +
63 +#define RTSP_PORT   554
64 +
65 +#endif /* __KERNEL__ */
66 +
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
71 @@ -0,0 +1,133 @@
72 +/*
73 + * Helpers for netfiler modules.  This file provides implementations for basic
74 + * functions such as strncasecmp(), etc.
75 + *
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()
81 + */
82 +#ifndef _NETFILTER_HELPERS_H
83 +#define _NETFILTER_HELPERS_H
84 +
85 +/* Only include these functions for kernel code. */
86 +#ifdef __KERNEL__
87 +
88 +#include <linux/ctype.h>
89 +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
90 +
91 +/*
92 + * The standard strncasecmp()
93 + */
94 +#ifdef NF_NEED_STRNCASECMP
95 +static int
96 +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
97 +{
98 +    if (s1 == NULL || s2 == NULL)
99 +    {
100 +        if (s1 == NULL && s2 == NULL)
101 +        {
102 +            return 0;
103 +        }
104 +        return (s1 == NULL) ? -1 : 1;
105 +    }
106 +    while (len > 0 && tolower(*s1) == tolower(*s2))
107 +    {
108 +        len--;
109 +        s1++;
110 +        s2++;
111 +    }
112 +    return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
113 +}
114 +#endif /* NF_NEED_STRNCASECMP */
115 +
116 +/*
117 + * Parse a string containing a 16-bit unsigned integer.
118 + * Returns the number of chars used, or zero if no number is found.
119 + */
120 +#ifdef NF_NEED_STRTOU16
121 +static int
122 +nf_strtou16(const char* pbuf, u_int16_t* pval)
123 +{
124 +    int n = 0;
125 +
126 +    *pval = 0;
127 +    while (isdigit(pbuf[n]))
128 +    {
129 +        *pval = (*pval * 10) + (pbuf[n] - '0');
130 +        n++;
131 +    }
132 +
133 +    return n;
134 +}
135 +#endif /* NF_NEED_STRTOU16 */
136 +
137 +/*
138 + * Parse a string containing a 32-bit unsigned integer.
139 + * Returns the number of chars used, or zero if no number is found.
140 + */
141 +#ifdef NF_NEED_STRTOU32
142 +static int
143 +nf_strtou32(const char* pbuf, u_int32_t* pval)
144 +{
145 +    int n = 0;
146 +
147 +    *pval = 0;
148 +    while (pbuf[n] >= '0' && pbuf[n] <= '9')
149 +    {
150 +        *pval = (*pval * 10) + (pbuf[n] - '0');
151 +        n++;
152 +    }
153 +
154 +    return n;
155 +}
156 +#endif /* NF_NEED_STRTOU32 */
157 +
158 +/*
159 + * Given a buffer and length, advance to the next line and mark the current
160 + * line.
161 + */
162 +#ifdef NF_NEED_NEXTLINE
163 +static int
164 +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
165 +{
166 +    uint    off = *poff;
167 +    uint    physlen = 0;
168 +
169 +    if (off >= len)
170 +    {
171 +        return 0;
172 +    }
173 +
174 +    while (p[off] != '\n')
175 +    {
176 +        if (len-off <= 1)
177 +        {
178 +            return 0;
179 +        }
180 +
181 +        physlen++;
182 +        off++;
183 +    }
184 +
185 +    /* if we saw a crlf, physlen needs adjusted */
186 +    if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
187 +    {
188 +        physlen--;
189 +    }
190 +
191 +    /* advance past the newline */
192 +    off++;
193 +
194 +    *plineoff = *poff;
195 +    *plinelen = physlen;
196 +    *poff = off;
197 +
198 +    return 1;
199 +}
200 +#endif /* NF_NEED_NEXTLINE */
201 +
202 +#endif /* __KERNEL__ */
203 +
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
208 @@ -0,0 +1,89 @@
209 +/*
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.
213 + *
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()
217 + */
218 +#ifndef _NETFILTER_MIME_H
219 +#define _NETFILTER_MIME_H
220 +
221 +/* Only include these functions for kernel code. */
222 +#ifdef __KERNEL__
223 +
224 +#include <linux/ctype.h>
225 +
226 +/*
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).
230 + *
231 + * 'line' in this context means logical line (includes LWS continuations).
232 + * Returns 1 on success, 0 on failure.
233 + */
234 +#ifdef NF_NEED_MIME_NEXTLINE
235 +static int
236 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
237 +{
238 +    uint    off = *poff;
239 +    uint    physlen = 0;
240 +    int     is_first_line = 1;
241 +
242 +    if (off >= len)
243 +    {
244 +        return 0;
245 +    }
246 +
247 +    do
248 +    {
249 +        while (p[off] != '\n')
250 +        {
251 +            if (len-off <= 1)
252 +            {
253 +                return 0;
254 +            }
255 +
256 +            physlen++;
257 +            off++;
258 +        }
259 +
260 +        /* if we saw a crlf, physlen needs adjusted */
261 +        if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
262 +        {
263 +            physlen--;
264 +        }
265 +
266 +        /* advance past the newline */
267 +        off++;
268 +
269 +        /* check for an empty line */
270 +        if (physlen == 0)
271 +        {
272 +            break;
273 +        }
274 +
275 +        /* check for colon on the first physical line */
276 +        if (is_first_line)
277 +        {
278 +            is_first_line = 0;
279 +            if (memchr(p+(*poff), ':', physlen) == NULL)
280 +            {
281 +                return 0;
282 +            }
283 +        }
284 +    }
285 +    while (p[off] == ' ' || p[off] == '\t');
286 +
287 +    *plineoff = *poff;
288 +    *plinelen = (physlen == 0) ? 0 : (off - *poff);
289 +    *poff = off;
290 +
291 +    return 1;
292 +}
293 +#endif /* NF_NEED_MIME_NEXTLINE */
294 +
295 +#endif /* __KERNEL__ */
296 +
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
301 @@ -64,6 +66,7 @@
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
312 @@ -271,6 +272,16 @@
313  
314           To compile it as a module, choose M here.  If unsure, say N.
315  
316 +config NF_CONNTRACK_RTSP
317 +       tristate "RTSP protocol support"
318 +       depends on NF_CONNTRACK
319 +       help
320 +               Support the RTSP protocol.  This allows UDP transports to be setup
321 +               properly, including RTP and RDT.
322 +
323 +               If you want to compile it as a module, say 'M' here and read
324 +               Documentation/modules.txt.  If unsure, say 'Y'.
325 +
326  config NF_CT_NETLINK
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
332 @@ -32,6 +32,7 @@
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
337  
338  # generic X tables 
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
342 @@ -274,6 +274,11 @@
343         depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
344         default NF_NAT && NF_CONNTRACK_IRC
345  
346 +config NF_NAT_RTSP
347 +       tristate
348 +       depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
349 +       default NF_NAT && NF_CONNTRACK_RTSP
350 +
351  config NF_NAT_TFTP
352         tristate
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
356 @@ -0,0 +1,515 @@
357 +/*
358 + * RTSP extension for IP connection tracking
359 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
360 + * based on ip_conntrack_irc.c
361 + *
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.
366 + *
367 + * Module load syntax:
368 + *   insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
369 + *                              max_outstanding=n setup_timeout=secs
370 + *
371 + * If no ports are specified, the default will be port 554.
372 + *
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).
377 + *
378 + * 2005-02-13: Harald Welte <laforge at netfilter.org>
379 + *     - port to 2.6
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
385 + */
386 +
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>
392 +
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>
397 +
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>
405 +
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)
409 +#if 0
410 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
411 +#else
412 +#define DEBUGP(fmt, args...)
413 +#endif
414 +
415 +#define MAX_PORTS 8
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;
420 +
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");
430 +
431 +static char *rtsp_buffer;
432 +static DEFINE_SPINLOCK(rtsp_buffer_lock);
433 +
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);
439 +
440 +EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook);
441 +
442 +/*
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.
447 + */
448 +#define MAX_PORT_MAPS 16
449 +
450 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
451 +
452 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
453 +
454 +/*
455 + * Parse an RTSP packet.
456 + *
457 + * Returns zero if parsing failed.
458 + *
459 + * Parameters:
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
467 + */
468 +static int
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)
473 +{
474 +       uint    entitylen = 0;
475 +       uint    lineoff;
476 +       uint    linelen;
477 +       
478 +       if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
479 +               return 0;
480 +       
481 +       *phdrsoff = *ptcpoff;
482 +       while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
483 +               if (linelen == 0) {
484 +                       if (entitylen > 0)
485 +                               *ptcpoff += min(entitylen, tcplen - *ptcpoff);
486 +                       break;
487 +               }
488 +               if (lineoff+linelen > tcplen) {
489 +                       INFOP("!! overrun !!\n");
490 +                       break;
491 +               }
492 +               
493 +               if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
494 +                       *pcseqoff = lineoff;
495 +                       *pcseqlen = linelen;
496 +               } 
497 +
498 +               if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
499 +                       *transoff = lineoff;
500 +                       *translen = linelen;
501 +               }
502 +               
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);
507 +               }
508 +       }
509 +       *phdrslen = (*ptcpoff) - (*phdrsoff);
510 +       
511 +       return 1;
512 +}
513 +
514 +/*
515 + * Find lo/hi client ports (if any) in transport header
516 + * In:
517 + *   ptcp, tcplen = packet
518 + *   tranoff, tranlen = buffer to search
519 + *
520 + * Out:
521 + *   pport_lo, pport_hi = lo/hi ports (host endian)
522 + *
523 + * Returns nonzero if any client ports found
524 + *
525 + * Note: it is valid (and expected) for the client to request multiple
526 + * transports, so we need to parse the entire line.
527 + */
528 +static int
529 +rtsp_parse_transport(char* ptran, uint tranlen,
530 +                     struct ip_ct_rtsp_expect* prtspexp)
531 +{
532 +       int     rc = 0;
533 +       uint    off = 0;
534 +       
535 +       if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
536 +           nf_strncasecmp(ptran, "Transport:", 10) != 0) {
537 +               INFOP("sanity check failed\n");
538 +               return 0;
539 +       }
540 +       
541 +       DEBUGP("tran='%.*s'\n", (int)tranlen, ptran);
542 +       off += 10;
543 +       SKIP_WSPACE(ptran, tranlen, off);
544 +       
545 +       /* Transport: tran;field;field=val,tran;field;field=val,... */
546 +       while (off < tranlen) {
547 +               const char* pparamend;
548 +               uint        nextparamoff;
549 +               
550 +               pparamend = memchr(ptran+off, ',', tranlen-off);
551 +               pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
552 +               nextparamoff = pparamend-ptran;
553 +               
554 +               while (off < nextparamoff) {
555 +                       const char* pfieldend;
556 +                       uint        nextfieldoff;
557 +                       
558 +                       pfieldend = memchr(ptran+off, ';', nextparamoff-off);
559 +                       nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
560 +                  
561 +                       if (strncmp(ptran+off, "client_port=", 12) == 0) {
562 +                               u_int16_t   port;
563 +                               uint        numlen;
564 +                   
565 +                               off += 12;
566 +                               numlen = nf_strtou16(ptran+off, &port);
567 +                               off += numlen;
568 +                               if (prtspexp->loport != 0 && prtspexp->loport != port)
569 +                                       DEBUGP("multiple ports found, port %hu ignored\n", port);
570 +                               else {
571 +                                       DEBUGP("lo port found : %hu\n", port);
572 +                                       prtspexp->loport = prtspexp->hiport = port;
573 +                                       if (ptran[off] == '-') {
574 +                                               off++;
575 +                                               numlen = nf_strtou16(ptran+off, &port);
576 +                                               off += numlen;
577 +                                               prtspexp->pbtype = pb_range;
578 +                                               prtspexp->hiport = port;
579 +                                               
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;
588 +                                               }
589 +                                       } else if (ptran[off] == '/') {
590 +                                               off++;
591 +                                               numlen = nf_strtou16(ptran+off, &port);
592 +                                               off += numlen;
593 +                                               prtspexp->pbtype = pb_discon;
594 +                                               prtspexp->hiport = port;
595 +                                       }
596 +                                       rc = 1;
597 +                               }
598 +                       }
599 +                       
600 +                       /*
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.
605 +                        */
606 +                       
607 +                       off = nextfieldoff;
608 +               }
609 +               
610 +               off = nextparamoff;
611 +       }
612 +       
613 +       return rc;
614 +}
615 +
616 +void expected(struct nf_conn *ct, struct nf_conntrack_expect *exp)
617 +{
618 +    if(nf_nat_rtsp_hook_expectfn) {
619 +        nf_nat_rtsp_hook_expectfn(ct,exp);
620 +    }
621 +}
622 +
623 +/*** conntrack functions ***/
624 +
625 +/* outbound packet: client->server */
626 +
627 +static inline int
628 +help_out(struct sk_buff **pskb, unsigned char *rb_ptr, unsigned int datalen,
629 +                struct nf_conn *ct, enum ip_conntrack_info ctinfo)
630 +{
631 +       struct ip_ct_rtsp_expect expinfo;
632 +       
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;
638 +       uint    dataoff = 0;
639 +       int ret = NF_ACCEPT;
640 +       
641 +       struct nf_conntrack_expect *exp;
642 +       
643 +       __be16 be_loport;
644 +       
645 +       memset(&expinfo, 0, sizeof(expinfo));
646 +       
647 +       while (dataoff < datalen) {
648 +               uint    cmdoff = dataoff;
649 +               uint    hdrsoff = 0;
650 +               uint    hdrslen = 0;
651 +               uint    cseqoff = 0;
652 +               uint    cseqlen = 0;
653 +               uint    transoff = 0;
654 +               uint    translen = 0;
655 +               uint    off;
656 +               
657 +               if (!rtsp_parse_message(pdata, datalen, &dataoff,
658 +                                       &hdrsoff, &hdrslen,
659 +                                       &cseqoff, &cseqlen,
660 +                                       &transoff, &translen))
661 +                       break;      /* not a valid message */
662 +               
663 +               if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
664 +                       continue;   /* not a SETUP message */
665 +               DEBUGP("found a setup message\n");
666 +
667 +               off = 0;
668 +               if(translen) {
669 +                       rtsp_parse_transport(pdata+transoff, translen, &expinfo);
670 +               }
671 +
672 +               if (expinfo.loport == 0) {
673 +                       DEBUGP("no udp transports found\n");
674 +                       continue;   /* no udp transports found */
675 +               }
676 +
677 +               DEBUGP("udp transport found, ports=(%d,%hu,%hu)\n",
678 +                      (int)expinfo.pbtype, expinfo.loport, expinfo.hiport);
679 +
680 +               exp = nf_conntrack_expect_alloc(ct);
681 +               if (!exp) {
682 +                       ret = NF_DROP;
683 +                       goto out;
684 +               }
685 +
686 +               be_loport = htons(expinfo.loport);
687 +
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); 
691 +
692 +               exp->master = ct;
693 +
694 +               exp->expectfn = expected;
695 +               exp->flags = 0;
696 +
697 +               if (expinfo.pbtype == pb_range) {
698 +                       DEBUGP("Changing expectation mask to handle multiple ports\n");
699 +                       exp->mask.dst.u.udp.port  = 0xfffe;
700 +               }
701 +
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));
707 +
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");
713 +                       ret  = NF_DROP;
714 +               }
715 +               nf_conntrack_expect_put(exp);
716 +               goto out;
717 +       }
718 +out:
719 +
720 +       return ret;
721 +}
722 +
723 +
724 +static inline int
725 +help_in(struct sk_buff **pskb, size_t pktlen,
726 + struct nf_conn* ct, enum ip_conntrack_info ctinfo)
727 +{
728 + return NF_ACCEPT;
729 +}
730 +
731 +static int help(struct sk_buff **pskb, unsigned int protoff,
732 +               struct nf_conn *ct, enum ip_conntrack_info ctinfo) 
733 +{
734 +       struct tcphdr _tcph, *th;
735 +       unsigned int dataoff, datalen;
736 +       char *rb_ptr;
737 +       int ret = NF_DROP;
738 +
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);
743 +               return NF_ACCEPT;
744 +       }
745 +
746 +       /* Not whole TCP header? */
747 +       th = skb_header_pointer(*pskb,protoff, sizeof(_tcph), &_tcph);
748 +
749 +       if (!th)
750 +               return NF_ACCEPT;
751 +   
752 +       /* No data ? */
753 +       dataoff = protoff + th->doff*4;
754 +       datalen = (*pskb)->len - dataoff;
755 +       if (dataoff >= (*pskb)->len)
756 +               return NF_ACCEPT;
757 +
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);
762 +
763 +#if 0
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)))
768 +       {
769 +               DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
770 +                      tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
771 +               return NF_ACCEPT;
772 +       }
773 +#endif
774 +
775 +       switch (CTINFO2DIR(ctinfo)) {
776 +       case IP_CT_DIR_ORIGINAL:
777 +               ret = help_out(pskb, rb_ptr, datalen, ct, ctinfo);
778 +               break;
779 +       case IP_CT_DIR_REPLY:
780 +               DEBUGP("IP_CT_DIR_REPLY\n");
781 +               /* inbound packet: server->client */
782 +               ret = NF_ACCEPT;
783 +               break;
784 +       }
785 +
786 +       spin_unlock_bh(&rtsp_buffer_lock);
787 +
788 +       return ret;
789 +}
790 +
791 +static struct nf_conntrack_helper rtsp_helpers[MAX_PORTS];
792 +static char rtsp_names[MAX_PORTS][10];
793 +
794 +/* This function is intentionally _NOT_ defined as __exit */
795 +static void
796 +fini(void)
797 +{
798 +       int i;
799 +       for (i = 0; i < num_ports; i++) {
800 +               DEBUGP("unregistering port %d\n", ports[i]);
801 +               nf_conntrack_helper_unregister(&rtsp_helpers[i]);
802 +       }
803 +       kfree(rtsp_buffer);
804 +}
805 +
806 +static int __init
807 +init(void)
808 +{
809 +       int i, ret;
810 +       struct nf_conntrack_helper *hlpr;
811 +       char *tmpname;
812 +
813 +       printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
814 +
815 +       if (max_outstanding < 1) {
816 +               printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
817 +               return -EBUSY;
818 +       }
819 +       if (setup_timeout < 0) {
820 +               printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
821 +               return -EBUSY;
822 +       }
823 +
824 +       rtsp_buffer = kmalloc(65536, GFP_KERNEL);
825 +       if (!rtsp_buffer) 
826 +               return -ENOMEM;
827 +
828 +       /* If no port given, default to standard rtsp port */
829 +       if (ports[0] == 0) {
830 +               ports[0] = RTSP_PORT;
831 +       }
832 +
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;
843 +               hlpr->help = help;
844 +
845 +               tmpname = &rtsp_names[i][0];
846 +               if (ports[i] == RTSP_PORT) {
847 +                       sprintf(tmpname, "rtsp");
848 +               } else {
849 +                       sprintf(tmpname, "rtsp-%d", i);
850 +               }
851 +               hlpr->name = tmpname;
852 +
853 +               DEBUGP("port #%d: %d\n", i, ports[i]);
854 +
855 +               ret = nf_conntrack_helper_register(hlpr);
856 +
857 +               if (ret) {
858 +                       printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
859 +                       fini();
860 +                       return -EBUSY;
861 +               }
862 +               num_ports++;
863 +       }
864 +       return 0;
865 +}
866 +
867 +module_init(init);
868 +module_exit(fini);
869 +
870 +EXPORT_SYMBOL(nf_nat_rtsp_hook_expectfn);
871 +
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
874 @@ -0,0 +1,496 @@
875 +/*
876 + * RTSP extension for TCP NAT alteration
877 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
878 + * based on ip_nat_irc.c
879 + *
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.
884 + *
885 + * Module load syntax:
886 + *      insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
887 + *                           stunaddr=<address>
888 + *                           destaction=[auto|strip|none]
889 + *
890 + * If no ports are specified, the default will be port 554 only.
891 + *
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.
900 + *
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).
905 + */
906 +
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>
913 +
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>
921 +
922 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
923 +#if 0 
924 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
925 +#else
926 +#define DEBUGP(fmt, args...)
927 +#endif
928 +
929 +#define MAX_PORTS       8
930 +#define DSTACT_AUTO     0
931 +#define DSTACT_STRIP    1
932 +#define DSTACT_NONE     2
933 +
934 +static char*    stunaddr = NULL;
935 +static char*    destaction = NULL;
936 +
937 +static u_int32_t extip = 0;
938 +static int       dstact = 0;
939 +
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)");
947 +
948 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
949 +
950 +/*** helper functions ***/
951 +
952 +static void
953 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
954 +{
955 +    struct iphdr*   iph  = ip_hdr(skb);
956 +    struct tcphdr*  tcph = (void *)iph + ip_hdrlen(skb);
957 +
958 +    *pptcpdata = (char*)tcph +  tcph->doff*4;
959 +    *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
960 +}
961 +
962 +/*** nat functions ***/
963 +
964 +/*
965 + * Mangle the "Transport:" header:
966 + *   - Replace all occurences of "client_port=<spec>"
967 + *   - Handle destination parameter
968 + *
969 + * In:
970 + *   ct, ctinfo = conntrack context
971 + *   pskb       = packet
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)
976 + *
977 + * Returns packet size difference.
978 + *
979 + * Assumes that a complete transport header is present, ending with CR or LF
980 + */
981 +static int
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)
986 +{
987 +    char*       ptcp;
988 +    uint        tcplen;
989 +    char*       ptran;
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) */
994 +    u_int32_t   newip;
995 +    u_int16_t   loport, hiport;
996 +    uint        off = 0;
997 +    uint        diff;           /* Number of bytes we removed */
998 +
999 +    struct nf_conn *ct = exp->master;
1000 +    struct nf_conntrack_tuple *t;
1001 +
1002 +    char    szextaddr[15+1];
1003 +    uint    extaddrlen;
1004 +    int     is_stun;
1005 +
1006 +    get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1007 +    ptran = ptcp+tranoff;
1008 +
1009 +    if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
1010 +        tranlen < 10 || !iseol(ptran[tranlen-1]) ||
1011 +        nf_strncasecmp(ptran, "Transport:", 10) != 0)
1012 +    {
1013 +        INFOP("sanity check failed\n");
1014 +        return 0;
1015 +    }
1016 +    off += 10;
1017 +    SKIP_WSPACE(ptcp+tranoff, tranlen, off);
1018 +
1019 +    newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
1020 +    t = &exp->tuple;
1021 +    t->dst.u3.ip = newip;
1022 +
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"));
1026 +
1027 +    rbuf1len = rbufalen = 0;
1028 +    switch (prtspexp->pbtype)
1029 +    {
1030 +    case pb_single:
1031 +        for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1032 +        {
1033 +            t->dst.u.udp.port = htons(loport);
1034 +            if (nf_conntrack_expect_related(exp) == 0)
1035 +            {
1036 +                DEBUGP("using port %hu\n", loport);
1037 +                break;
1038 +            }
1039 +        }
1040 +        if (loport != 0)
1041 +        {
1042 +            rbuf1len = sprintf(rbuf1, "%hu", loport);
1043 +            rbufalen = sprintf(rbufa, "%hu", loport);
1044 +        }
1045 +        break;
1046 +    case pb_range:
1047 +        for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
1048 +        {
1049 +            t->dst.u.udp.port = htons(loport);
1050 +            if (nf_conntrack_expect_related(exp) == 0)
1051 +            {
1052 +                hiport = loport + ~exp->mask.dst.u.udp.port;
1053 +                DEBUGP("using ports %hu-%hu\n", loport, hiport);
1054 +                break;
1055 +            }
1056 +        }
1057 +        if (loport != 0)
1058 +        {
1059 +            rbuf1len = sprintf(rbuf1, "%hu", loport);
1060 +            rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
1061 +        }
1062 +        break;
1063 +    case pb_discon:
1064 +        for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1065 +        {
1066 +            t->dst.u.udp.port = htons(loport);
1067 +            if (nf_conntrack_expect_related(exp) == 0)
1068 +            {
1069 +                DEBUGP("using port %hu (1 of 2)\n", loport);
1070 +                break;
1071 +            }
1072 +        }
1073 +        for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
1074 +        {
1075 +            t->dst.u.udp.port = htons(hiport);
1076 +            if (nf_conntrack_expect_related(exp) == 0)
1077 +            {
1078 +                DEBUGP("using port %hu (2 of 2)\n", hiport);
1079 +                break;
1080 +            }
1081 +        }
1082 +        if (loport != 0 && hiport != 0)
1083 +        {
1084 +            rbuf1len = sprintf(rbuf1, "%hu", loport);
1085 +            if (hiport == loport+1)
1086 +            {
1087 +                rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
1088 +            }
1089 +            else
1090 +            {
1091 +                rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
1092 +            }
1093 +        }
1094 +        break;
1095 +    }
1096 +
1097 +    if (rbuf1len == 0)
1098 +    {
1099 +        return 0;   /* cannot get replacement port(s) */
1100 +    }
1101 +
1102 +    /* Transport: tran;field;field=val,tran;field;field=val,... */
1103 +    while (off < tranlen)
1104 +    {
1105 +        uint        saveoff;
1106 +        const char* pparamend;
1107 +        uint        nextparamoff;
1108 +
1109 +        pparamend = memchr(ptran+off, ',', tranlen-off);
1110 +        pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
1111 +        nextparamoff = pparamend-ptcp;
1112 +
1113 +        /*
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.
1118 +         */
1119 +        is_stun = 0;
1120 +        saveoff = off;
1121 +        while (off < nextparamoff)
1122 +        {
1123 +            const char* pfieldend;
1124 +            uint        nextfieldoff;
1125 +
1126 +            pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1127 +            nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1128 +
1129 +            if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
1130 +            {
1131 +                if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
1132 +                {
1133 +                    is_stun = 1;
1134 +                }
1135 +                if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
1136 +                {
1137 +                    diff = nextfieldoff-off;
1138 +                    if (!nf_nat_mangle_tcp_packet(pskb, ct, ctinfo,
1139 +                                                         off, diff, NULL, 0))
1140 +                    {
1141 +                        /* mangle failed, all we can do is bail */
1142 +                       nf_conntrack_unexpect_related(exp);
1143 +                        return 0;
1144 +                    }
1145 +                    get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1146 +                    ptran = ptcp+tranoff;
1147 +                    tranlen -= diff;
1148 +                    nextparamoff -= diff;
1149 +                    nextfieldoff -= diff;
1150 +                }
1151 +            }
1152 +
1153 +            off = nextfieldoff;
1154 +        }
1155 +        if (is_stun)
1156 +        {
1157 +            continue;
1158 +        }
1159 +        off = saveoff;
1160 +        while (off < nextparamoff)
1161 +        {
1162 +            const char* pfieldend;
1163 +            uint        nextfieldoff;
1164 +
1165 +            pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1166 +            nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1167 +
1168 +            if (strncmp(ptran+off, "client_port=", 12) == 0)
1169 +            {
1170 +                u_int16_t   port;
1171 +                uint        numlen;
1172 +                uint        origoff;
1173 +                uint        origlen;
1174 +                char*       rbuf    = rbuf1;
1175 +                uint        rbuflen = rbuf1len;
1176 +
1177 +                off += 12;
1178 +                origoff = (ptran-ptcp)+off;
1179 +                origlen = 0;
1180 +                numlen = nf_strtou16(ptran+off, &port);
1181 +                off += numlen;
1182 +                origlen += numlen;
1183 +                if (port != prtspexp->loport)
1184 +                {
1185 +                    DEBUGP("multiple ports found, port %hu ignored\n", port);
1186 +                }
1187 +                else
1188 +                {
1189 +                    if (ptran[off] == '-' || ptran[off] == '/')
1190 +                    {
1191 +                        off++;
1192 +                        origlen++;
1193 +                        numlen = nf_strtou16(ptran+off, &port);
1194 +                        off += numlen;
1195 +                        origlen += numlen;
1196 +                        rbuf = rbufa;
1197 +                        rbuflen = rbufalen;
1198 +                    }
1199 +
1200 +                    /*
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.
1204 +                     *
1205 +                     * parameter 4 below is offset from start of tcp data.
1206 +                     */
1207 +                    diff = origlen-rbuflen;
1208 +                    if (!nf_nat_mangle_tcp_packet(pskb, ct, ctinfo,
1209 +                                              origoff, origlen, rbuf, rbuflen))
1210 +                    {
1211 +                        /* mangle failed, all we can do is bail */
1212 +                       nf_conntrack_unexpect_related(exp);
1213 +                        return 0;
1214 +                    }
1215 +                    get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1216 +                    ptran = ptcp+tranoff;
1217 +                    tranlen -= diff;
1218 +                    nextparamoff -= diff;
1219 +                    nextfieldoff -= diff;
1220 +                }
1221 +            }
1222 +
1223 +            off = nextfieldoff;
1224 +        }
1225 +
1226 +        off = nextparamoff;
1227 +    }
1228 +
1229 +    return 1;
1230 +}
1231 +
1232 +static uint
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)
1236 +{
1237 +    char*   ptcp;
1238 +    uint    tcplen;
1239 +    uint    hdrsoff;
1240 +    uint    hdrslen;
1241 +    uint    lineoff;
1242 +    uint    linelen;
1243 +    uint    off;
1244 +
1245 +    //struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
1246 +    //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
1247 +
1248 +    get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1249 +    hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
1250 +    hdrslen = matchlen;
1251 +    off = hdrsoff;
1252 +    DEBUGP("NAT rtsp help_out\n");
1253 +
1254 +    while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
1255 +    {
1256 +        if (linelen == 0)
1257 +        {
1258 +            break;
1259 +        }
1260 +        if (off > hdrsoff+hdrslen)
1261 +        {
1262 +            INFOP("!! overrun !!");
1263 +            break;
1264 +        }
1265 +        DEBUGP("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1266 +
1267 +        if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
1268 +        {
1269 +            uint oldtcplen = tcplen;
1270 +           DEBUGP("hdr: Transport\n");
1271 +            if (!rtsp_mangle_tran(ctinfo, exp, prtspexp, pskb, lineoff, linelen))
1272 +            {
1273 +               DEBUGP("hdr: Transport mangle failed");
1274 +                break;
1275 +            }
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);
1282 +        }
1283 +    }
1284 +
1285 +    return NF_ACCEPT;
1286 +}
1287 +
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)
1292 +{
1293 +    int dir = CTINFO2DIR(ctinfo);
1294 +    int rc = NF_ACCEPT;
1295 +
1296 +    switch (dir)
1297 +    {
1298 +    case IP_CT_DIR_ORIGINAL:
1299 +        rc = help_out(pskb, ctinfo, matchoff, matchlen, prtspexp, exp);
1300 +        break;
1301 +    case IP_CT_DIR_REPLY:
1302 +       DEBUGP("unmangle ! %u\n", ctinfo);
1303 +       /* XXX: unmangle */
1304 +       rc = NF_ACCEPT;
1305 +        break;
1306 +    }
1307 +    //UNLOCK_BH(&ip_rtsp_lock);
1308 +
1309 +    return rc;
1310 +}
1311 +
1312 +static void expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
1313 +{
1314 +    struct nf_nat_multi_range_compat mr;
1315 +    u_int32_t newdstip, newsrcip, newip;
1316 +
1317 +    struct nf_conn *master = ct->master;
1318 +
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;
1323 +    newip = newdstip;
1324 +
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));
1327 +
1328 +    mr.rangesize = 1;
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;
1332 +
1333 +    nf_nat_setup_info(ct, &mr.range[0], NF_IP_PRE_ROUTING);
1334 +}
1335 +
1336 +
1337 +static void __exit fini(void)
1338 +{
1339 +       nf_nat_rtsp_hook = NULL;
1340 +        nf_nat_rtsp_hook_expectfn = NULL;
1341 +       synchronize_net();
1342 +}
1343 +
1344 +static int __init init(void)
1345 +{
1346 +       printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1347 +
1348 +       BUG_ON(nf_nat_rtsp_hook);
1349 +       nf_nat_rtsp_hook = help;
1350 +        nf_nat_rtsp_hook_expectfn = &expected;
1351 +
1352 +       if (stunaddr != NULL)
1353 +               extip = in_aton(stunaddr);
1354 +
1355 +       if (destaction != NULL) {
1356 +               if (strcmp(destaction, "auto") == 0)
1357 +                       dstact = DSTACT_AUTO;
1358 +
1359 +               if (strcmp(destaction, "strip") == 0)
1360 +                       dstact = DSTACT_STRIP;
1361 +
1362 +               if (strcmp(destaction, "none") == 0)
1363 +                       dstact = DSTACT_NONE;
1364 +       }
1365 +
1366 +       return 0;
1367 +}
1368 +
1369 +module_init(init);
1370 +module_exit(fini);