1 diff -purN linux-2.6.23-rc2.orig/include/linux/netfilter_helpers.h linux-2.6.23-rc2/include/linux/netfilter_helpers.h
2 --- linux-2.6.23-rc2.orig/include/linux/netfilter_helpers.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.23-rc2/include/linux/netfilter_helpers.h 2007-08-05 10:53:55.000000000 +0200
6 + * Helpers for netfiler modules. This file provides implementations for basic
7 + * functions such as strncasecmp(), etc.
9 + * gcc will warn for defined but unused functions, so we only include the
10 + * functions requested. The following macros are used:
11 + * NF_NEED_STRNCASECMP nf_strncasecmp()
12 + * NF_NEED_STRTOU16 nf_strtou16()
13 + * NF_NEED_STRTOU32 nf_strtou32()
15 +#ifndef _NETFILTER_HELPERS_H
16 +#define _NETFILTER_HELPERS_H
18 +/* Only include these functions for kernel code. */
21 +#include <linux/ctype.h>
22 +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
25 + * The standard strncasecmp()
27 +#ifdef NF_NEED_STRNCASECMP
29 +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
31 + if (s1 == NULL || s2 == NULL)
33 + if (s1 == NULL && s2 == NULL)
37 + return (s1 == NULL) ? -1 : 1;
39 + while (len > 0 && tolower(*s1) == tolower(*s2))
45 + return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
47 +#endif /* NF_NEED_STRNCASECMP */
50 + * Parse a string containing a 16-bit unsigned integer.
51 + * Returns the number of chars used, or zero if no number is found.
53 +#ifdef NF_NEED_STRTOU16
55 +nf_strtou16(const char* pbuf, u_int16_t* pval)
60 + while (isdigit(pbuf[n]))
62 + *pval = (*pval * 10) + (pbuf[n] - '0');
68 +#endif /* NF_NEED_STRTOU16 */
71 + * Parse a string containing a 32-bit unsigned integer.
72 + * Returns the number of chars used, or zero if no number is found.
74 +#ifdef NF_NEED_STRTOU32
76 +nf_strtou32(const char* pbuf, u_int32_t* pval)
81 + while (pbuf[n] >= '0' && pbuf[n] <= '9')
83 + *pval = (*pval * 10) + (pbuf[n] - '0');
89 +#endif /* NF_NEED_STRTOU32 */
92 + * Given a buffer and length, advance to the next line and mark the current
95 +#ifdef NF_NEED_NEXTLINE
97 +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
107 + while (p[off] != '\n')
118 + /* if we saw a crlf, physlen needs adjusted */
119 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
124 + /* advance past the newline */
128 + *plinelen = physlen;
133 +#endif /* NF_NEED_NEXTLINE */
135 +#define NIPQUAD(addr) \
136 + ((unsigned char *)&addr)[0], \
137 + ((unsigned char *)&addr)[1], \
138 + ((unsigned char *)&addr)[2], \
139 + ((unsigned char *)&addr)[3]
141 +#endif /* __KERNEL__ */
143 +#endif /* _NETFILTER_HELPERS_H */
144 diff -purN linux-2.6.23-rc2.orig/include/linux/netfilter_mime.h linux-2.6.23-rc2/include/linux/netfilter_mime.h
145 --- linux-2.6.23-rc2.orig/include/linux/netfilter_mime.h 1970-01-01 01:00:00.000000000 +0100
146 +++ linux-2.6.23-rc2/include/linux/netfilter_mime.h 2007-08-05 10:53:55.000000000 +0200
149 + * MIME functions for netfilter modules. This file provides implementations
150 + * for basic MIME parsing. MIME headers are used in many protocols, such as
151 + * HTTP, RTSP, SIP, etc.
153 + * gcc will warn for defined but unused functions, so we only include the
154 + * functions requested. The following macros are used:
155 + * NF_NEED_MIME_NEXTLINE nf_mime_nextline()
157 +#ifndef _NETFILTER_MIME_H
158 +#define _NETFILTER_MIME_H
160 +/* Only include these functions for kernel code. */
163 +#include <linux/ctype.h>
166 + * Given a buffer and length, advance to the next line and mark the current
167 + * line. If the current line is empty, *plinelen will be set to zero. If
168 + * not, it will be set to the actual line length (including CRLF).
170 + * 'line' in this context means logical line (includes LWS continuations).
171 + * Returns 1 on success, 0 on failure.
173 +#ifdef NF_NEED_MIME_NEXTLINE
175 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
179 + int is_first_line = 1;
188 + while (p[off] != '\n')
199 + /* if we saw a crlf, physlen needs adjusted */
200 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
205 + /* advance past the newline */
208 + /* check for an empty line */
214 + /* check for colon on the first physical line */
218 + if (memchr(p+(*poff), ':', physlen) == NULL)
224 + while (p[off] == ' ' || p[off] == '\t');
227 + *plinelen = (physlen == 0) ? 0 : (off - *poff);
232 +#endif /* NF_NEED_MIME_NEXTLINE */
234 +#endif /* __KERNEL__ */
236 +#endif /* _NETFILTER_MIME_H */
237 diff -purN linux-2.6.23-rc2.orig/net/ipv4/netfilter/Kconfig linux-2.6.23-rc2/net/ipv4/netfilter/Kconfig
238 --- linux-2.6.23-rc2.orig/net/ipv4/netfilter/Kconfig 2007-08-05 21:17:01.000000000 +0200
239 +++ linux-2.6.23-rc2/net/ipv4/netfilter/Kconfig 2007-08-05 11:04:59.000000000 +0200
240 @@ -274,6 +274,11 @@ config NF_NAT_IRC
241 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
242 default NF_NAT && NF_CONNTRACK_IRC
246 + depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
247 + default NF_NAT && NF_CONNTRACK_RTSP
251 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
252 diff -purN linux-2.6.23-rc2.orig/net/ipv4/netfilter/Makefile linux-2.6.23-rc2/net/ipv4/netfilter/Makefile
253 --- linux-2.6.23-rc2.orig/net/ipv4/netfilter/Makefile 2007-08-05 21:14:19.000000000 +0200
254 +++ linux-2.6.23-rc2/net/ipv4/netfilter/Makefile 2007-08-05 10:53:55.000000000 +0200
256 +obj-$(CONFIG_NF_NAT_RTSP) += nf_nat_rtsp.o
257 diff -purN linux-2.6.23-rc2.orig/net/netfilter/Kconfig linux-2.6.23-rc2/net/netfilter/Kconfig
258 --- linux-2.6.23-rc2.orig/net/netfilter/Kconfig 2007-08-05 21:17:02.000000000 +0200
259 +++ linux-2.6.23-rc2/net/netfilter/Kconfig 2007-08-05 11:04:59.000000000 +0200
260 @@ -249,6 +249,16 @@ config NF_CONNTRACK_TFTP
262 To compile it as a module, choose M here. If unsure, say N.
264 +config NF_CONNTRACK_RTSP
265 + tristate "RTSP protocol support"
266 + depends on NF_CONNTRACK
268 + Support the RTSP protocol. This allows UDP transports to be setup
269 + properly, including RTP and RDT.
271 + If you want to compile it as a module, say 'M' here and read
272 + Documentation/modules.txt. If unsure, say 'Y'.
274 config NF_CONNTRACK_MMS
275 tristate 'MMS protocol support'
276 depends on NF_CONNTRACK
277 diff -purN linux-2.6.23-rc2.orig/net/netfilter/Makefile linux-2.6.23-rc2/net/netfilter/Makefile
278 --- linux-2.6.23-rc2.orig/net/netfilter/Makefile 2007-08-05 21:17:02.000000000 +0200
279 +++ linux-2.6.23-rc2/net/netfilter/Makefile 2007-08-05 11:04:59.000000000 +0200
281 +obj-$(CONFIG_NF_CONNTRACK_RTSP) += nf_conntrack_rtsp.o
282 diff -purN linux-2.6.24-rc1.orig/include/linux/netfilter/nf_conntrack_rtsp.h linux-2.6.24-rc1/include/linux/netfilter/nf_conntrack_rtsp.h
283 --- linux-2.6.24-rc1.orig/include/linux/netfilter/nf_conntrack_rtsp.h 1970-01-01 01:00:00.000000000 +0100
284 +++ linux-2.6.24-rc1/include/linux/netfilter/nf_conntrack_rtsp.h 2007-11-10 17:16:36.000000000 +0100
287 + * RTSP extension for IP connection tracking.
288 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
289 + * based on ip_conntrack_irc.h
291 + * This program is free software; you can redistribute it and/or
292 + * modify it under the terms of the GNU General Public License
293 + * as published by the Free Software Foundation; either version
294 + * 2 of the License, or (at your option) any later version.
296 +#ifndef _IP_CONNTRACK_RTSP_H
297 +#define _IP_CONNTRACK_RTSP_H
299 +//#define IP_NF_RTSP_DEBUG 1
300 +#define IP_NF_RTSP_VERSION "0.6.21"
303 +/* port block types */
305 + pb_single, /* client_port=x */
306 + pb_range, /* client_port=x-y */
307 + pb_discon /* client_port=x/y (rtspbis) */
310 +/* We record seq number and length of rtsp headers here, all in host order. */
313 + * This structure is per expected connection. It is a member of struct
314 + * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
315 + * there and we are expected to only store the length of the data which
316 + * needs replaced. If a packet contains multiple RTSP messages, we create
317 + * one expected connection per message.
319 + * We use these variables to mark the entire header block. This may seem
320 + * like overkill, but the nature of RTSP requires it. A header may appear
321 + * multiple times in a message. We must treat two Transport headers the
322 + * same as one Transport header with two entries.
324 +struct ip_ct_rtsp_expect
326 + u_int32_t len; /* length of header block */
327 + portblock_t pbtype; /* Type of port block that was requested */
328 + u_int16_t loport; /* Port that was requested, low or first */
329 + u_int16_t hiport; /* Port that was requested, high or second */
331 + uint method; /* RTSP method */
332 + uint cseq; /* CSeq from request */
336 +extern unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
337 + enum ip_conntrack_info ctinfo,
338 + unsigned int matchoff, unsigned int matchlen,
339 + struct ip_ct_rtsp_expect *prtspexp,
340 + struct nf_conntrack_expect *exp);
342 +extern void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
344 +#define RTSP_PORT 554
346 +#endif /* __KERNEL__ */
348 +#endif /* _IP_CONNTRACK_RTSP_H */
349 diff -purN linux-2.6.24-rc1.orig/net/ipv4/netfilter/nf_nat_rtsp.c linux-2.6.24-rc1/net/ipv4/netfilter/nf_nat_rtsp.c
350 --- linux-2.6.24-rc1.orig/net/ipv4/netfilter/nf_nat_rtsp.c 1970-01-01 01:00:00.000000000 +0100
351 +++ linux-2.6.24-rc1/net/ipv4/netfilter/nf_nat_rtsp.c 2007-11-10 17:17:21.000000000 +0100
354 + * RTSP extension for TCP NAT alteration
355 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
356 + * based on ip_nat_irc.c
358 + * This program is free software; you can redistribute it and/or
359 + * modify it under the terms of the GNU General Public License
360 + * as published by the Free Software Foundation; either version
361 + * 2 of the License, or (at your option) any later version.
363 + * Module load syntax:
364 + * insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
365 + * stunaddr=<address>
366 + * destaction=[auto|strip|none]
368 + * If no ports are specified, the default will be port 554 only.
370 + * stunaddr specifies the address used to detect that a client is using STUN.
371 + * If this address is seen in the destination parameter, it is assumed that
372 + * the client has already punched a UDP hole in the firewall, so we don't
373 + * mangle the client_port. If none is specified, it is autodetected. It
374 + * only needs to be set if you have multiple levels of NAT. It should be
375 + * set to the external address that the STUN clients detect. Note that in
376 + * this case, it will not be possible for clients to use UDP with servers
377 + * between the NATs.
379 + * If no destaction is specified, auto is used.
380 + * destaction=auto: strip destination parameter if it is not stunaddr.
381 + * destaction=strip: always strip destination parameter (not recommended).
382 + * destaction=none: do not touch destination parameter (not recommended).
385 +#include <linux/module.h>
386 +#include <net/tcp.h>
387 +#include <net/netfilter/nf_nat_helper.h>
388 +#include <net/netfilter/nf_nat_rule.h>
389 +#include <linux/netfilter/nf_conntrack_rtsp.h>
390 +#include <net/netfilter/nf_conntrack_expect.h>
392 +#include <linux/inet.h>
393 +#include <linux/ctype.h>
394 +#define NF_NEED_STRNCASECMP
395 +#define NF_NEED_STRTOU16
396 +#include <linux/netfilter_helpers.h>
397 +#define NF_NEED_MIME_NEXTLINE
398 +#include <linux/netfilter_mime.h>
401 +#define DSTACT_AUTO 0
402 +#define DSTACT_STRIP 1
403 +#define DSTACT_NONE 2
405 +static char* stunaddr = NULL;
406 +static char* destaction = NULL;
408 +static u_int32_t extip = 0;
409 +static int dstact = 0;
411 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
412 +MODULE_DESCRIPTION("RTSP network address translation module");
413 +MODULE_LICENSE("GPL");
414 +module_param(stunaddr, charp, 0644);
415 +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
416 +module_param(destaction, charp, 0644);
417 +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
419 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
421 +/*** helper functions ***/
424 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
426 + struct iphdr* iph = ip_hdr(skb);
427 + struct tcphdr* tcph = (void *)iph + ip_hdrlen(skb);
429 + *pptcpdata = (char*)tcph + tcph->doff*4;
430 + *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
433 +/*** nat functions ***/
436 + * Mangle the "Transport:" header:
437 + * - Replace all occurences of "client_port=<spec>"
438 + * - Handle destination parameter
441 + * ct, ctinfo = conntrack context
443 + * tranoff = Transport header offset from TCP data
444 + * tranlen = Transport header length (incl. CRLF)
445 + * rport_lo = replacement low port (host endian)
446 + * rport_hi = replacement high port (host endian)
448 + * Returns packet size difference.
450 + * Assumes that a complete transport header is present, ending with CR or LF
453 +rtsp_mangle_tran(enum ip_conntrack_info ctinfo,
454 + struct nf_conntrack_expect* exp,
455 + struct ip_ct_rtsp_expect* prtspexp,
456 + struct sk_buff* skb, uint tranoff, uint tranlen)
461 + char rbuf1[16]; /* Replacement buffer (one port) */
462 + uint rbuf1len; /* Replacement len (one port) */
463 + char rbufa[16]; /* Replacement buffer (all ports) */
464 + uint rbufalen; /* Replacement len (all ports) */
466 + u_int16_t loport, hiport;
468 + uint diff; /* Number of bytes we removed */
470 + struct nf_conn *ct = exp->master;
471 + struct nf_conntrack_tuple *t;
473 + char szextaddr[15+1];
477 + get_skb_tcpdata(skb, &ptcp, &tcplen);
478 + ptran = ptcp+tranoff;
480 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
481 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
482 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
484 + pr_info("sanity check failed\n");
488 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
490 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
492 + t->dst.u3.ip = newip;
494 + extaddrlen = extip ? sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(extip))
495 + : sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(newip));
496 + pr_debug("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
498 + rbuf1len = rbufalen = 0;
499 + switch (prtspexp->pbtype)
502 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
504 + t->dst.u.udp.port = htons(loport);
505 + if (nf_ct_expect_related(exp) == 0)
507 + pr_debug("using port %hu\n", loport);
513 + rbuf1len = sprintf(rbuf1, "%hu", loport);
514 + rbufalen = sprintf(rbufa, "%hu", loport);
518 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
520 + t->dst.u.udp.port = htons(loport);
521 + if (nf_ct_expect_related(exp) == 0)
523 + hiport = loport + 1; //~exp->mask.dst.u.udp.port;
524 + pr_debug("using ports %hu-%hu\n", loport, hiport);
530 + rbuf1len = sprintf(rbuf1, "%hu", loport);
531 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
535 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
537 + t->dst.u.udp.port = htons(loport);
538 + if (nf_ct_expect_related(exp) == 0)
540 + pr_debug("using port %hu (1 of 2)\n", loport);
544 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
546 + t->dst.u.udp.port = htons(hiport);
547 + if (nf_ct_expect_related(exp) == 0)
549 + pr_debug("using port %hu (2 of 2)\n", hiport);
553 + if (loport != 0 && hiport != 0)
555 + rbuf1len = sprintf(rbuf1, "%hu", loport);
556 + if (hiport == loport+1)
558 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
562 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
570 + return 0; /* cannot get replacement port(s) */
573 + /* Transport: tran;field;field=val,tran;field;field=val,... */
574 + while (off < tranlen)
577 + const char* pparamend;
580 + pparamend = memchr(ptran+off, ',', tranlen-off);
581 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
582 + nextparamoff = pparamend-ptcp;
585 + * We pass over each param twice. On the first pass, we look for a
586 + * destination= field. It is handled by the security policy. If it
587 + * is present, allowed, and equal to our external address, we assume
588 + * that STUN is being used and we leave the client_port= field alone.
592 + while (off < nextparamoff)
594 + const char* pfieldend;
597 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
598 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
600 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
602 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
606 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
608 + diff = nextfieldoff-off;
609 + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
610 + off, diff, NULL, 0))
612 + /* mangle failed, all we can do is bail */
613 + nf_ct_unexpect_related(exp);
616 + get_skb_tcpdata(skb, &ptcp, &tcplen);
617 + ptran = ptcp+tranoff;
619 + nextparamoff -= diff;
620 + nextfieldoff -= diff;
624 + off = nextfieldoff;
631 + while (off < nextparamoff)
633 + const char* pfieldend;
636 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
637 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
639 + if (strncmp(ptran+off, "client_port=", 12) == 0)
645 + char* rbuf = rbuf1;
646 + uint rbuflen = rbuf1len;
649 + origoff = (ptran-ptcp)+off;
651 + numlen = nf_strtou16(ptran+off, &port);
654 + if (port != prtspexp->loport)
656 + pr_debug("multiple ports found, port %hu ignored\n", port);
660 + if (ptran[off] == '-' || ptran[off] == '/')
664 + numlen = nf_strtou16(ptran+off, &port);
668 + rbuflen = rbufalen;
672 + * note we cannot just memcpy() if the sizes are the same.
673 + * the mangle function does skb resizing, checks for a
674 + * cloned skb, and updates the checksums.
676 + * parameter 4 below is offset from start of tcp data.
678 + diff = origlen-rbuflen;
679 + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
680 + origoff, origlen, rbuf, rbuflen))
682 + /* mangle failed, all we can do is bail */
683 + nf_ct_unexpect_related(exp);
686 + get_skb_tcpdata(skb, &ptcp, &tcplen);
687 + ptran = ptcp+tranoff;
689 + nextparamoff -= diff;
690 + nextfieldoff -= diff;
694 + off = nextfieldoff;
697 + off = nextparamoff;
704 +help_out(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
705 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
706 + struct nf_conntrack_expect* exp)
716 + //struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
717 + //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
719 + get_skb_tcpdata(skb, &ptcp, &tcplen);
720 + hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
721 + hdrslen = matchlen;
723 + pr_debug("NAT rtsp help_out\n");
725 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
731 + if (off > hdrsoff+hdrslen)
733 + pr_info("!! overrun !!");
736 + pr_debug("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
738 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
740 + uint oldtcplen = tcplen;
741 + pr_debug("hdr: Transport\n");
742 + if (!rtsp_mangle_tran(ctinfo, exp, prtspexp, skb, lineoff, linelen))
744 + pr_debug("hdr: Transport mangle failed");
747 + get_skb_tcpdata(skb, &ptcp, &tcplen);
748 + hdrslen -= (oldtcplen-tcplen);
749 + off -= (oldtcplen-tcplen);
750 + lineoff -= (oldtcplen-tcplen);
751 + linelen -= (oldtcplen-tcplen);
752 + pr_debug("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
760 +help(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
761 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
762 + struct nf_conntrack_expect* exp)
764 + int dir = CTINFO2DIR(ctinfo);
765 + int rc = NF_ACCEPT;
769 + case IP_CT_DIR_ORIGINAL:
770 + rc = help_out(skb, ctinfo, matchoff, matchlen, prtspexp, exp);
772 + case IP_CT_DIR_REPLY:
773 + pr_debug("unmangle ! %u\n", ctinfo);
774 + /* XXX: unmangle */
778 + //UNLOCK_BH(&ip_rtsp_lock);
783 +static void expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
785 + struct nf_nat_multi_range_compat mr;
786 + u_int32_t newdstip, newsrcip, newip;
788 + struct nf_conn *master = ct->master;
790 + newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
791 + newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
792 + //FIXME (how to port that ?)
793 + //code from 2.4 : newip = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) ? newsrcip : newdstip;
796 + pr_debug("newsrcip=%u.%u.%u.%u, newdstip=%u.%u.%u.%u, newip=%u.%u.%u.%u\n",
797 + NIPQUAD(newsrcip), NIPQUAD(newdstip), NIPQUAD(newip));
800 + // We don't want to manip the per-protocol, just the IPs.
801 + mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
802 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
804 + nf_nat_setup_info(ct, &mr.range[0], IP_NAT_MANIP_DST);
808 +static void __exit fini(void)
810 + nf_nat_rtsp_hook = NULL;
811 + nf_nat_rtsp_hook_expectfn = NULL;
815 +static int __init init(void)
817 + printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
819 + BUG_ON(nf_nat_rtsp_hook);
820 + nf_nat_rtsp_hook = help;
821 + nf_nat_rtsp_hook_expectfn = &expected;
823 + if (stunaddr != NULL)
824 + extip = in_aton(stunaddr);
826 + if (destaction != NULL) {
827 + if (strcmp(destaction, "auto") == 0)
828 + dstact = DSTACT_AUTO;
830 + if (strcmp(destaction, "strip") == 0)
831 + dstact = DSTACT_STRIP;
833 + if (strcmp(destaction, "none") == 0)
834 + dstact = DSTACT_NONE;
842 diff -purN linux-2.6.26.orig/net/netfilter/nf_conntrack_rtsp.c linux-2.6.26/net/netfilter/nf_conntrack_rtsp.c
843 --- linux-2.6.26.orig/net/netfilter/nf_conntrack_rtsp.c 1970-01-01 01:00:00.000000000 +0100
844 +++ linux-2.6.26/net/netfilter/nf_conntrack_rtsp.c 2008-07-24 16:19:36.000000000 +0200
847 + * RTSP extension for IP connection tracking
848 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
849 + * based on ip_conntrack_irc.c
851 + * This program is free software; you can redistribute it and/or
852 + * modify it under the terms of the GNU General Public License
853 + * as published by the Free Software Foundation; either version
854 + * 2 of the License, or (at your option) any later version.
856 + * Module load syntax:
857 + * insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
858 + * max_outstanding=n setup_timeout=secs
860 + * If no ports are specified, the default will be port 554.
862 + * With max_outstanding you can define the maximum number of not yet
863 + * answered SETUP requests per RTSP session (default 8).
864 + * With setup_timeout you can specify how long the system waits for
865 + * an expected data channel (default 300 seconds).
867 + * 2005-02-13: Harald Welte <laforge at netfilter.org>
869 + * - update to recent post-2.6.11 api changes
870 + * 2006-09-14: Steven Van Acker <deepstar at singularity.be>
871 + * - removed calls to NAT code from conntrack helper: NAT no longer needed to use rtsp-conntrack
872 + * 2007-04-18: Michael Guntsche <mike at it-loops.com>
873 + * - Port to new NF API
876 +#include <linux/module.h>
877 +#include <linux/netfilter.h>
878 +#include <linux/ip.h>
879 +#include <linux/inet.h>
880 +#include <net/tcp.h>
882 +#include <net/netfilter/nf_conntrack.h>
883 +#include <net/netfilter/nf_conntrack_expect.h>
884 +#include <net/netfilter/nf_conntrack_helper.h>
885 +#include <linux/netfilter/nf_conntrack_rtsp.h>
887 +#define NF_NEED_STRNCASECMP
888 +#define NF_NEED_STRTOU16
889 +#define NF_NEED_STRTOU32
890 +#define NF_NEED_NEXTLINE
891 +#include <linux/netfilter_helpers.h>
892 +#define NF_NEED_MIME_NEXTLINE
893 +#include <linux/netfilter_mime.h>
895 +#include <linux/ctype.h>
896 +#define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
899 +static int ports[MAX_PORTS];
900 +static int num_ports = 0;
901 +static int max_outstanding = 8;
902 +static unsigned int setup_timeout = 300;
904 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
905 +MODULE_DESCRIPTION("RTSP connection tracking module");
906 +MODULE_LICENSE("GPL");
907 +module_param_array(ports, int, &num_ports, 0400);
908 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
909 +module_param(max_outstanding, int, 0400);
910 +MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
911 +module_param(setup_timeout, int, 0400);
912 +MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
914 +static char *rtsp_buffer;
915 +static DEFINE_SPINLOCK(rtsp_buffer_lock);
917 +static struct nf_conntrack_expect_policy rtsp_exp_policy;
919 +unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
920 + enum ip_conntrack_info ctinfo,
921 + unsigned int matchoff, unsigned int matchlen,struct ip_ct_rtsp_expect* prtspexp,
922 + struct nf_conntrack_expect *exp);
923 +void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
925 +EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook);
928 + * Max mappings we will allow for one RTSP connection (for RTP, the number
929 + * of allocated ports is twice this value). Note that SMIL burns a lot of
930 + * ports so keep this reasonably high. If this is too low, you will see a
931 + * lot of "no free client map entries" messages.
933 +#define MAX_PORT_MAPS 16
935 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
937 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
940 + * Parse an RTSP packet.
942 + * Returns zero if parsing failed.
945 + * IN ptcp tcp data pointer
946 + * IN tcplen tcp data len
947 + * IN/OUT ptcpoff points to current tcp offset
948 + * OUT phdrsoff set to offset of rtsp headers
949 + * OUT phdrslen set to length of rtsp headers
950 + * OUT pcseqoff set to offset of CSeq header
951 + * OUT pcseqlen set to length of CSeq header
954 +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
955 + uint* phdrsoff, uint* phdrslen,
956 + uint* pcseqoff, uint* pcseqlen,
957 + uint* transoff, uint* translen)
959 + uint entitylen = 0;
963 + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
966 + *phdrsoff = *ptcpoff;
967 + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
968 + if (linelen == 0) {
970 + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
973 + if (lineoff+linelen > tcplen) {
974 + pr_info("!! overrun !!\n");
978 + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
979 + *pcseqoff = lineoff;
980 + *pcseqlen = linelen;
983 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
984 + *transoff = lineoff;
985 + *translen = linelen;
988 + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0) {
989 + uint off = lineoff+15;
990 + SKIP_WSPACE(ptcp+lineoff, linelen, off);
991 + nf_strtou32(ptcp+off, &entitylen);
994 + *phdrslen = (*ptcpoff) - (*phdrsoff);
1000 + * Find lo/hi client ports (if any) in transport header
1002 + * ptcp, tcplen = packet
1003 + * tranoff, tranlen = buffer to search
1006 + * pport_lo, pport_hi = lo/hi ports (host endian)
1008 + * Returns nonzero if any client ports found
1010 + * Note: it is valid (and expected) for the client to request multiple
1011 + * transports, so we need to parse the entire line.
1014 +rtsp_parse_transport(char* ptran, uint tranlen,
1015 + struct ip_ct_rtsp_expect* prtspexp)
1020 + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
1021 + nf_strncasecmp(ptran, "Transport:", 10) != 0) {
1022 + pr_info("sanity check failed\n");
1026 + pr_debug("tran='%.*s'\n", (int)tranlen, ptran);
1028 + SKIP_WSPACE(ptran, tranlen, off);
1030 + /* Transport: tran;field;field=val,tran;field;field=val,... */
1031 + while (off < tranlen) {
1032 + const char* pparamend;
1033 + uint nextparamoff;
1035 + pparamend = memchr(ptran+off, ',', tranlen-off);
1036 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
1037 + nextparamoff = pparamend-ptran;
1039 + while (off < nextparamoff) {
1040 + const char* pfieldend;
1041 + uint nextfieldoff;
1043 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1044 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1046 + if (strncmp(ptran+off, "client_port=", 12) == 0) {
1051 + numlen = nf_strtou16(ptran+off, &port);
1053 + if (prtspexp->loport != 0 && prtspexp->loport != port)
1054 + pr_debug("multiple ports found, port %hu ignored\n", port);
1056 + pr_debug("lo port found : %hu\n", port);
1057 + prtspexp->loport = prtspexp->hiport = port;
1058 + if (ptran[off] == '-') {
1060 + numlen = nf_strtou16(ptran+off, &port);
1062 + prtspexp->pbtype = pb_range;
1063 + prtspexp->hiport = port;
1065 + // If we have a range, assume rtp:
1066 + // loport must be even, hiport must be loport+1
1067 + if ((prtspexp->loport & 0x0001) != 0 ||
1068 + prtspexp->hiport != prtspexp->loport+1) {
1069 + pr_debug("incorrect range: %hu-%hu, correcting\n",
1070 + prtspexp->loport, prtspexp->hiport);
1071 + prtspexp->loport &= 0xfffe;
1072 + prtspexp->hiport = prtspexp->loport+1;
1074 + } else if (ptran[off] == '/') {
1076 + numlen = nf_strtou16(ptran+off, &port);
1078 + prtspexp->pbtype = pb_discon;
1079 + prtspexp->hiport = port;
1086 + * Note we don't look for the destination parameter here.
1087 + * If we are using NAT, the NAT module will handle it. If not,
1088 + * and the client is sending packets elsewhere, the expectation
1089 + * will quietly time out.
1092 + off = nextfieldoff;
1095 + off = nextparamoff;
1101 +void expected(struct nf_conn *ct, struct nf_conntrack_expect *exp)
1103 + typeof(nf_nat_rtsp_hook_expectfn) nf_nat_rtsp_expectfn;
1104 + nf_nat_rtsp_expectfn = rcu_dereference(nf_nat_rtsp_hook_expectfn);
1105 + if(nf_nat_rtsp_expectfn && ct->master->status & IPS_NAT_MASK) {
1106 + nf_nat_rtsp_expectfn(ct,exp);
1110 +/*** conntrack functions ***/
1112 +/* outbound packet: client->server */
1115 +help_out(struct sk_buff *skb, unsigned char *rb_ptr, unsigned int datalen,
1116 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1118 + struct ip_ct_rtsp_expect expinfo;
1120 + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
1121 + //struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
1122 + //uint tcplen = pktlen - iph->ihl * 4;
1123 + char* pdata = rb_ptr;
1124 + //uint datalen = tcplen - tcph->doff * 4;
1126 + int ret = NF_ACCEPT;
1128 + struct nf_conntrack_expect *exp;
1132 + typeof(nf_nat_rtsp_hook) nf_nat_rtsp;
1134 + memset(&expinfo, 0, sizeof(expinfo));
1136 + while (dataoff < datalen) {
1137 + uint cmdoff = dataoff;
1142 + uint transoff = 0;
1143 + uint translen = 0;
1146 + if (!rtsp_parse_message(pdata, datalen, &dataoff,
1147 + &hdrsoff, &hdrslen,
1148 + &cseqoff, &cseqlen,
1149 + &transoff, &translen))
1150 + break; /* not a valid message */
1152 + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
1153 + continue; /* not a SETUP message */
1154 + pr_debug("found a setup message\n");
1158 + rtsp_parse_transport(pdata+transoff, translen, &expinfo);
1161 + if (expinfo.loport == 0) {
1162 + pr_debug("no udp transports found\n");
1163 + continue; /* no udp transports found */
1166 + pr_debug("udp transport found, ports=(%d,%hu,%hu)\n",
1167 + (int)expinfo.pbtype, expinfo.loport, expinfo.hiport);
1169 + exp = nf_ct_expect_alloc(ct);
1175 + be_loport = htons(expinfo.loport);
1177 + nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1178 + &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3,
1179 + IPPROTO_UDP, NULL, &be_loport);
1183 + exp->expectfn = expected;
1186 + if (expinfo.pbtype == pb_range) {
1187 + pr_debug("Changing expectation mask to handle multiple ports\n");
1188 + //exp->mask.dst.u.udp.port = 0xfffe;
1191 + pr_debug("expect_related %u.%u.%u.%u:%u-%u.%u.%u.%u:%u\n",
1192 + NIPQUAD(exp->tuple.src.u3.ip),
1193 + ntohs(exp->tuple.src.u.udp.port),
1194 + NIPQUAD(exp->tuple.dst.u3.ip),
1195 + ntohs(exp->tuple.dst.u.udp.port));
1197 + nf_nat_rtsp = rcu_dereference(nf_nat_rtsp_hook);
1198 + if (nf_nat_rtsp && ct->status & IPS_NAT_MASK)
1199 + /* pass the request off to the nat helper */
1200 + ret = nf_nat_rtsp(skb, ctinfo, hdrsoff, hdrslen, &expinfo, exp);
1201 + else if (nf_ct_expect_related(exp) != 0) {
1202 + pr_info("nf_conntrack_expect_related failed\n");
1205 + nf_ct_expect_put(exp);
1215 +help_in(struct sk_buff *skb, size_t pktlen,
1216 + struct nf_conn* ct, enum ip_conntrack_info ctinfo)
1221 +static int help(struct sk_buff *skb, unsigned int protoff,
1222 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1224 + struct tcphdr _tcph, *th;
1225 + unsigned int dataoff, datalen;
1227 + int ret = NF_DROP;
1229 + /* Until there's been traffic both ways, don't look in packets. */
1230 + if (ctinfo != IP_CT_ESTABLISHED &&
1231 + ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
1232 + pr_debug("conntrackinfo = %u\n", ctinfo);
1236 + /* Not whole TCP header? */
1237 + th = skb_header_pointer(skb,protoff, sizeof(_tcph), &_tcph);
1243 + dataoff = protoff + th->doff*4;
1244 + datalen = skb->len - dataoff;
1245 + if (dataoff >= skb->len)
1248 + spin_lock_bh(&rtsp_buffer_lock);
1249 + rb_ptr = skb_header_pointer(skb, dataoff,
1250 + skb->len - dataoff, rtsp_buffer);
1251 + BUG_ON(rb_ptr == NULL);
1254 + /* Checksum invalid? Ignore. */
1255 + /* FIXME: Source route IP option packets --RR */
1256 + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
1257 + csum_partial((char*)tcph, tcplen, 0)))
1259 + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
1260 + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1265 + switch (CTINFO2DIR(ctinfo)) {
1266 + case IP_CT_DIR_ORIGINAL:
1267 + ret = help_out(skb, rb_ptr, datalen, ct, ctinfo);
1269 + case IP_CT_DIR_REPLY:
1270 + pr_debug("IP_CT_DIR_REPLY\n");
1271 + /* inbound packet: server->client */
1276 + spin_unlock_bh(&rtsp_buffer_lock);
1281 +static struct nf_conntrack_helper rtsp_helpers[MAX_PORTS];
1282 +static char rtsp_names[MAX_PORTS][10];
1284 +/* This function is intentionally _NOT_ defined as __exit */
1289 + for (i = 0; i < num_ports; i++) {
1290 + pr_debug("unregistering port %d\n", ports[i]);
1291 + nf_conntrack_helper_unregister(&rtsp_helpers[i]);
1293 + kfree(rtsp_buffer);
1300 + struct nf_conntrack_helper *hlpr;
1303 + printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1305 + if (max_outstanding < 1) {
1306 + printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
1309 + if (setup_timeout < 0) {
1310 + printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
1314 + rtsp_exp_policy.max_expected = max_outstanding;
1315 + rtsp_exp_policy.timeout = setup_timeout;
1317 + rtsp_buffer = kmalloc(65536, GFP_KERNEL);
1321 + /* If no port given, default to standard rtsp port */
1322 + if (ports[0] == 0) {
1323 + ports[0] = RTSP_PORT;
1326 + for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
1327 + hlpr = &rtsp_helpers[i];
1328 + memset(hlpr, 0, sizeof(struct nf_conntrack_helper));
1329 + hlpr->tuple.src.l3num = AF_INET;
1330 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
1331 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
1332 + //hlpr->mask.src.u.tcp.port = 0xFFFF;
1333 + //hlpr->mask.dst.protonum = 0xFF;
1334 + hlpr->expect_policy = &rtsp_exp_policy;
1335 + hlpr->me = THIS_MODULE;
1336 + hlpr->help = help;
1338 + tmpname = &rtsp_names[i][0];
1339 + if (ports[i] == RTSP_PORT) {
1340 + sprintf(tmpname, "rtsp");
1342 + sprintf(tmpname, "rtsp-%d", i);
1344 + hlpr->name = tmpname;
1346 + pr_debug("port #%d: %d\n", i, ports[i]);
1348 + ret = nf_conntrack_helper_register(hlpr);
1351 + printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
1363 +EXPORT_SYMBOL(nf_nat_rtsp_hook_expectfn);