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