80253b491e28eb39a6bac78b5e2c57527c20fb78
[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-03 23:41:29.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-03 23:41:29.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-03 23:41:29.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-02 15:04:56.000000000 +0800
304 +++ linux-2.6.23-rc9/net/ipv4/netfilter/Makefile 2007-10-03 23:41:29.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-02 15:04:56.000000000 +0800
316 +++ linux-2.6.23-rc9/net/netfilter/Kconfig 2007-10-03 23:41:29.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-02 15:04:56.000000000 +0800
337 +++ linux-2.6.23-rc9/net/netfilter/Makefile 2007-10-03 23:41:29.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-02 15:04:56.000000000 +0800
349 +++ linux-2.6.23-rc9/net/ipv4/netfilter/Kconfig 2007-10-03 23:41:29.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-03 23:41:29.000000000 +0800
366 @@ -0,0 +1,515 @@
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_conntrack_expect_alloc(ct);
691 + if (!exp) {
692 + ret = NF_DROP;
693 + goto out;
694 + }
695 +
696 + be_loport = htons(expinfo.loport);
697 +
698 + nf_conntrack_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.dst.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_conntrack_expect_related(exp) != 0) {
722 + INFOP("nf_conntrack_expect_related failed\n");
723 + ret = NF_DROP;
724 + }
725 + nf_conntrack_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->mask.src.u.tcp.port = 0xFFFF;
849 + hlpr->mask.dst.protonum = 0xFF;
850 + hlpr->max_expected = max_outstanding;
851 + hlpr->timeout = setup_timeout;
852 + hlpr->me = THIS_MODULE;
853 + hlpr->help = help;
854 +
855 + tmpname = &rtsp_names[i][0];
856 + if (ports[i] == RTSP_PORT) {
857 + sprintf(tmpname, "rtsp");
858 + } else {
859 + sprintf(tmpname, "rtsp-%d", i);
860 + }
861 + hlpr->name = tmpname;
862 +
863 + DEBUGP("port #%d: %d\n", i, ports[i]);
864 +
865 + ret = nf_conntrack_helper_register(hlpr);
866 +
867 + if (ret) {
868 + printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
869 + fini();
870 + return -EBUSY;
871 + }
872 + num_ports++;
873 + }
874 + return 0;
875 +}
876 +
877 +module_init(init);
878 +module_exit(fini);
879 +
880 +EXPORT_SYMBOL(nf_nat_rtsp_hook_expectfn);
881 +
882 Index: linux-2.6.23-rc9/net/ipv4/netfilter/nf_nat_rtsp.c
883 ===================================================================
884 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
885 +++ linux-2.6.23-rc9/net/ipv4/netfilter/nf_nat_rtsp.c 2007-10-03 23:41:29.000000000 +0800
886 @@ -0,0 +1,496 @@
887 +/*
888 + * RTSP extension for TCP NAT alteration
889 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
890 + * based on ip_nat_irc.c
891 + *
892 + * This program is free software; you can redistribute it and/or
893 + * modify it under the terms of the GNU General Public License
894 + * as published by the Free Software Foundation; either version
895 + * 2 of the License, or (at your option) any later version.
896 + *
897 + * Module load syntax:
898 + * insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
899 + * stunaddr=<address>
900 + * destaction=[auto|strip|none]
901 + *
902 + * If no ports are specified, the default will be port 554 only.
903 + *
904 + * stunaddr specifies the address used to detect that a client is using STUN.
905 + * If this address is seen in the destination parameter, it is assumed that
906 + * the client has already punched a UDP hole in the firewall, so we don't
907 + * mangle the client_port. If none is specified, it is autodetected. It
908 + * only needs to be set if you have multiple levels of NAT. It should be
909 + * set to the external address that the STUN clients detect. Note that in
910 + * this case, it will not be possible for clients to use UDP with servers
911 + * between the NATs.
912 + *
913 + * If no destaction is specified, auto is used.
914 + * destaction=auto: strip destination parameter if it is not stunaddr.
915 + * destaction=strip: always strip destination parameter (not recommended).
916 + * destaction=none: do not touch destination parameter (not recommended).
917 + */
918 +
919 +#include <linux/module.h>
920 +#include <net/tcp.h>
921 +#include <net/netfilter/nf_nat_helper.h>
922 +#include <net/netfilter/nf_nat_rule.h>
923 +#include <linux/netfilter/nf_conntrack_rtsp.h>
924 +#include <net/netfilter/nf_conntrack_expect.h>
925 +
926 +#include <linux/inet.h>
927 +#include <linux/ctype.h>
928 +#define NF_NEED_STRNCASECMP
929 +#define NF_NEED_STRTOU16
930 +#include <linux/netfilter_helpers.h>
931 +#define NF_NEED_MIME_NEXTLINE
932 +#include <linux/netfilter_mime.h>
933 +
934 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
935 +#if 0
936 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
937 +#else
938 +#define DEBUGP(fmt, args...)
939 +#endif
940 +
941 +#define MAX_PORTS 8
942 +#define DSTACT_AUTO 0
943 +#define DSTACT_STRIP 1
944 +#define DSTACT_NONE 2
945 +
946 +static char* stunaddr = NULL;
947 +static char* destaction = NULL;
948 +
949 +static u_int32_t extip = 0;
950 +static int dstact = 0;
951 +
952 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
953 +MODULE_DESCRIPTION("RTSP network address translation module");
954 +MODULE_LICENSE("GPL");
955 +module_param(stunaddr, charp, 0644);
956 +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
957 +module_param(destaction, charp, 0644);
958 +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
959 +
960 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
961 +
962 +/*** helper functions ***/
963 +
964 +static void
965 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
966 +{
967 + struct iphdr* iph = ip_hdr(skb);
968 + struct tcphdr* tcph = (void *)iph + ip_hdrlen(skb);
969 +
970 + *pptcpdata = (char*)tcph + tcph->doff*4;
971 + *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
972 +}
973 +
974 +/*** nat functions ***/
975 +
976 +/*
977 + * Mangle the "Transport:" header:
978 + * - Replace all occurences of "client_port=<spec>"
979 + * - Handle destination parameter
980 + *
981 + * In:
982 + * ct, ctinfo = conntrack context
983 + * pskb = packet
984 + * tranoff = Transport header offset from TCP data
985 + * tranlen = Transport header length (incl. CRLF)
986 + * rport_lo = replacement low port (host endian)
987 + * rport_hi = replacement high port (host endian)
988 + *
989 + * Returns packet size difference.
990 + *
991 + * Assumes that a complete transport header is present, ending with CR or LF
992 + */
993 +static int
994 +rtsp_mangle_tran(enum ip_conntrack_info ctinfo,
995 + struct nf_conntrack_expect* exp,
996 + struct ip_ct_rtsp_expect* prtspexp,
997 + struct sk_buff** pskb, uint tranoff, uint tranlen)
998 +{
999 + char* ptcp;
1000 + uint tcplen;
1001 + char* ptran;
1002 + char rbuf1[16]; /* Replacement buffer (one port) */
1003 + uint rbuf1len; /* Replacement len (one port) */
1004 + char rbufa[16]; /* Replacement buffer (all ports) */
1005 + uint rbufalen; /* Replacement len (all ports) */
1006 + u_int32_t newip;
1007 + u_int16_t loport, hiport;
1008 + uint off = 0;
1009 + uint diff; /* Number of bytes we removed */
1010 +
1011 + struct nf_conn *ct = exp->master;
1012 + struct nf_conntrack_tuple *t;
1013 +
1014 + char szextaddr[15+1];
1015 + uint extaddrlen;
1016 + int is_stun;
1017 +
1018 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1019 + ptran = ptcp+tranoff;
1020 +
1021 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
1022 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
1023 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
1024 + {
1025 + INFOP("sanity check failed\n");
1026 + return 0;
1027 + }
1028 + off += 10;
1029 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
1030 +
1031 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
1032 + t = &exp->tuple;
1033 + t->dst.u3.ip = newip;
1034 +
1035 + extaddrlen = extip ? sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(extip))
1036 + : sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(newip));
1037 + DEBUGP("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
1038 +
1039 + rbuf1len = rbufalen = 0;
1040 + switch (prtspexp->pbtype)
1041 + {
1042 + case pb_single:
1043 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1044 + {
1045 + t->dst.u.udp.port = htons(loport);
1046 + if (nf_conntrack_expect_related(exp) == 0)
1047 + {
1048 + DEBUGP("using port %hu\n", loport);
1049 + break;
1050 + }
1051 + }
1052 + if (loport != 0)
1053 + {
1054 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1055 + rbufalen = sprintf(rbufa, "%hu", loport);
1056 + }
1057 + break;
1058 + case pb_range:
1059 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
1060 + {
1061 + t->dst.u.udp.port = htons(loport);
1062 + if (nf_conntrack_expect_related(exp) == 0)
1063 + {
1064 + hiport = loport + ~exp->mask.dst.u.udp.port;
1065 + DEBUGP("using ports %hu-%hu\n", loport, hiport);
1066 + break;
1067 + }
1068 + }
1069 + if (loport != 0)
1070 + {
1071 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1072 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
1073 + }
1074 + break;
1075 + case pb_discon:
1076 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1077 + {
1078 + t->dst.u.udp.port = htons(loport);
1079 + if (nf_conntrack_expect_related(exp) == 0)
1080 + {
1081 + DEBUGP("using port %hu (1 of 2)\n", loport);
1082 + break;
1083 + }
1084 + }
1085 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
1086 + {
1087 + t->dst.u.udp.port = htons(hiport);
1088 + if (nf_conntrack_expect_related(exp) == 0)
1089 + {
1090 + DEBUGP("using port %hu (2 of 2)\n", hiport);
1091 + break;
1092 + }
1093 + }
1094 + if (loport != 0 && hiport != 0)
1095 + {
1096 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1097 + if (hiport == loport+1)
1098 + {
1099 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
1100 + }
1101 + else
1102 + {
1103 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
1104 + }
1105 + }
1106 + break;
1107 + }
1108 +
1109 + if (rbuf1len == 0)
1110 + {
1111 + return 0; /* cannot get replacement port(s) */
1112 + }
1113 +
1114 + /* Transport: tran;field;field=val,tran;field;field=val,... */
1115 + while (off < tranlen)
1116 + {
1117 + uint saveoff;
1118 + const char* pparamend;
1119 + uint nextparamoff;
1120 +
1121 + pparamend = memchr(ptran+off, ',', tranlen-off);
1122 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
1123 + nextparamoff = pparamend-ptcp;
1124 +
1125 + /*
1126 + * We pass over each param twice. On the first pass, we look for a
1127 + * destination= field. It is handled by the security policy. If it
1128 + * is present, allowed, and equal to our external address, we assume
1129 + * that STUN is being used and we leave the client_port= field alone.
1130 + */
1131 + is_stun = 0;
1132 + saveoff = off;
1133 + while (off < nextparamoff)
1134 + {
1135 + const char* pfieldend;
1136 + uint nextfieldoff;
1137 +
1138 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1139 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1140 +
1141 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
1142 + {
1143 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
1144 + {
1145 + is_stun = 1;
1146 + }
1147 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
1148 + {
1149 + diff = nextfieldoff-off;
1150 + if (!nf_nat_mangle_tcp_packet(pskb, ct, ctinfo,
1151 + off, diff, NULL, 0))
1152 + {
1153 + /* mangle failed, all we can do is bail */
1154 + nf_conntrack_unexpect_related(exp);
1155 + return 0;
1156 + }
1157 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1158 + ptran = ptcp+tranoff;
1159 + tranlen -= diff;
1160 + nextparamoff -= diff;
1161 + nextfieldoff -= diff;
1162 + }
1163 + }
1164 +
1165 + off = nextfieldoff;
1166 + }
1167 + if (is_stun)
1168 + {
1169 + continue;
1170 + }
1171 + off = saveoff;
1172 + while (off < nextparamoff)
1173 + {
1174 + const char* pfieldend;
1175 + uint nextfieldoff;
1176 +
1177 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1178 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1179 +
1180 + if (strncmp(ptran+off, "client_port=", 12) == 0)
1181 + {
1182 + u_int16_t port;
1183 + uint numlen;
1184 + uint origoff;
1185 + uint origlen;
1186 + char* rbuf = rbuf1;
1187 + uint rbuflen = rbuf1len;
1188 +
1189 + off += 12;
1190 + origoff = (ptran-ptcp)+off;
1191 + origlen = 0;
1192 + numlen = nf_strtou16(ptran+off, &port);
1193 + off += numlen;
1194 + origlen += numlen;
1195 + if (port != prtspexp->loport)
1196 + {
1197 + DEBUGP("multiple ports found, port %hu ignored\n", port);
1198 + }
1199 + else
1200 + {
1201 + if (ptran[off] == '-' || ptran[off] == '/')
1202 + {
1203 + off++;
1204 + origlen++;
1205 + numlen = nf_strtou16(ptran+off, &port);
1206 + off += numlen;
1207 + origlen += numlen;
1208 + rbuf = rbufa;
1209 + rbuflen = rbufalen;
1210 + }
1211 +
1212 + /*
1213 + * note we cannot just memcpy() if the sizes are the same.
1214 + * the mangle function does skb resizing, checks for a
1215 + * cloned skb, and updates the checksums.
1216 + *
1217 + * parameter 4 below is offset from start of tcp data.
1218 + */
1219 + diff = origlen-rbuflen;
1220 + if (!nf_nat_mangle_tcp_packet(pskb, ct, ctinfo,
1221 + origoff, origlen, rbuf, rbuflen))
1222 + {
1223 + /* mangle failed, all we can do is bail */
1224 + nf_conntrack_unexpect_related(exp);
1225 + return 0;
1226 + }
1227 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1228 + ptran = ptcp+tranoff;
1229 + tranlen -= diff;
1230 + nextparamoff -= diff;
1231 + nextfieldoff -= diff;
1232 + }
1233 + }
1234 +
1235 + off = nextfieldoff;
1236 + }
1237 +
1238 + off = nextparamoff;
1239 + }
1240 +
1241 + return 1;
1242 +}
1243 +
1244 +static uint
1245 +help_out(struct sk_buff **pskb, enum ip_conntrack_info ctinfo,
1246 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
1247 + struct nf_conntrack_expect* exp)
1248 +{
1249 + char* ptcp;
1250 + uint tcplen;
1251 + uint hdrsoff;
1252 + uint hdrslen;
1253 + uint lineoff;
1254 + uint linelen;
1255 + uint off;
1256 +
1257 + //struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
1258 + //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
1259 +
1260 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1261 + hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
1262 + hdrslen = matchlen;
1263 + off = hdrsoff;
1264 + DEBUGP("NAT rtsp help_out\n");
1265 +
1266 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
1267 + {
1268 + if (linelen == 0)
1269 + {
1270 + break;
1271 + }
1272 + if (off > hdrsoff+hdrslen)
1273 + {
1274 + INFOP("!! overrun !!");
1275 + break;
1276 + }
1277 + DEBUGP("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1278 +
1279 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
1280 + {
1281 + uint oldtcplen = tcplen;
1282 + DEBUGP("hdr: Transport\n");
1283 + if (!rtsp_mangle_tran(ctinfo, exp, prtspexp, pskb, lineoff, linelen))
1284 + {
1285 + DEBUGP("hdr: Transport mangle failed");
1286 + break;
1287 + }
1288 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1289 + hdrslen -= (oldtcplen-tcplen);
1290 + off -= (oldtcplen-tcplen);
1291 + lineoff -= (oldtcplen-tcplen);
1292 + linelen -= (oldtcplen-tcplen);
1293 + DEBUGP("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1294 + }
1295 + }
1296 +
1297 + return NF_ACCEPT;
1298 +}
1299 +
1300 +static unsigned int
1301 +help(struct sk_buff **pskb, enum ip_conntrack_info ctinfo,
1302 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
1303 + struct nf_conntrack_expect* exp)
1304 +{
1305 + int dir = CTINFO2DIR(ctinfo);
1306 + int rc = NF_ACCEPT;
1307 +
1308 + switch (dir)
1309 + {
1310 + case IP_CT_DIR_ORIGINAL:
1311 + rc = help_out(pskb, ctinfo, matchoff, matchlen, prtspexp, exp);
1312 + break;
1313 + case IP_CT_DIR_REPLY:
1314 + DEBUGP("unmangle ! %u\n", ctinfo);
1315 + /* XXX: unmangle */
1316 + rc = NF_ACCEPT;
1317 + break;
1318 + }
1319 + //UNLOCK_BH(&ip_rtsp_lock);
1320 +
1321 + return rc;
1322 +}
1323 +
1324 +static void expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
1325 +{
1326 + struct nf_nat_multi_range_compat mr;
1327 + u_int32_t newdstip, newsrcip, newip;
1328 +
1329 + struct nf_conn *master = ct->master;
1330 +
1331 + newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
1332 + newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
1333 + //FIXME (how to port that ?)
1334 + //code from 2.4 : newip = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) ? newsrcip : newdstip;
1335 + newip = newdstip;
1336 +
1337 + DEBUGP("newsrcip=%u.%u.%u.%u, newdstip=%u.%u.%u.%u, newip=%u.%u.%u.%u\n",
1338 + NIPQUAD(newsrcip), NIPQUAD(newdstip), NIPQUAD(newip));
1339 +
1340 + mr.rangesize = 1;
1341 + // We don't want to manip the per-protocol, just the IPs.
1342 + mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
1343 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
1344 +
1345 + nf_nat_setup_info(ct, &mr.range[0], NF_IP_PRE_ROUTING);
1346 +}
1347 +
1348 +
1349 +static void __exit fini(void)
1350 +{
1351 + nf_nat_rtsp_hook = NULL;
1352 + nf_nat_rtsp_hook_expectfn = NULL;
1353 + synchronize_net();
1354 +}
1355 +
1356 +static int __init init(void)
1357 +{
1358 + printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1359 +
1360 + BUG_ON(nf_nat_rtsp_hook);
1361 + nf_nat_rtsp_hook = help;
1362 + nf_nat_rtsp_hook_expectfn = &expected;
1363 +
1364 + if (stunaddr != NULL)
1365 + extip = in_aton(stunaddr);
1366 +
1367 + if (destaction != NULL) {
1368 + if (strcmp(destaction, "auto") == 0)
1369 + dstact = DSTACT_AUTO;
1370 +
1371 + if (strcmp(destaction, "strip") == 0)
1372 + dstact = DSTACT_STRIP;
1373 +
1374 + if (strcmp(destaction, "none") == 0)
1375 + dstact = DSTACT_NONE;
1376 + }
1377 +
1378 + return 0;
1379 +}
1380 +
1381 +module_init(init);
1382 +module_exit(fini);