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