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