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