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