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