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 +#endif /* __KERNEL__ */
137 +#endif /* _NETFILTER_HELPERS_H */
138 diff -purN linux-2.6.23-rc2.orig/include/linux/netfilter_mime.h linux-2.6.23-rc2/include/linux/netfilter_mime.h
139 --- linux-2.6.23-rc2.orig/include/linux/netfilter_mime.h 1970-01-01 01:00:00.000000000 +0100
140 +++ linux-2.6.23-rc2/include/linux/netfilter_mime.h 2007-08-05 10:53:55.000000000 +0200
143 + * MIME functions for netfilter modules. This file provides implementations
144 + * for basic MIME parsing. MIME headers are used in many protocols, such as
145 + * HTTP, RTSP, SIP, etc.
147 + * gcc will warn for defined but unused functions, so we only include the
148 + * functions requested. The following macros are used:
149 + * NF_NEED_MIME_NEXTLINE nf_mime_nextline()
151 +#ifndef _NETFILTER_MIME_H
152 +#define _NETFILTER_MIME_H
154 +/* Only include these functions for kernel code. */
157 +#include <linux/ctype.h>
160 + * Given a buffer and length, advance to the next line and mark the current
161 + * line. If the current line is empty, *plinelen will be set to zero. If
162 + * not, it will be set to the actual line length (including CRLF).
164 + * 'line' in this context means logical line (includes LWS continuations).
165 + * Returns 1 on success, 0 on failure.
167 +#ifdef NF_NEED_MIME_NEXTLINE
169 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
173 + int is_first_line = 1;
182 + while (p[off] != '\n')
193 + /* if we saw a crlf, physlen needs adjusted */
194 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
199 + /* advance past the newline */
202 + /* check for an empty line */
208 + /* check for colon on the first physical line */
212 + if (memchr(p+(*poff), ':', physlen) == NULL)
218 + while (p[off] == ' ' || p[off] == '\t');
221 + *plinelen = (physlen == 0) ? 0 : (off - *poff);
226 +#endif /* NF_NEED_MIME_NEXTLINE */
228 +#endif /* __KERNEL__ */
230 +#endif /* _NETFILTER_MIME_H */
231 diff -purN linux-2.6.23-rc2.orig/net/ipv4/netfilter/Kconfig linux-2.6.23-rc2/net/ipv4/netfilter/Kconfig
232 --- linux-2.6.23-rc2.orig/net/ipv4/netfilter/Kconfig 2007-08-05 21:17:01.000000000 +0200
233 +++ linux-2.6.23-rc2/net/ipv4/netfilter/Kconfig 2007-08-05 11:04:59.000000000 +0200
234 @@ -274,6 +274,11 @@ config NF_NAT_IRC
235 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
236 default NF_NAT && NF_CONNTRACK_IRC
240 + depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
241 + default NF_NAT && NF_CONNTRACK_RTSP
245 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
246 diff -purN linux-2.6.23-rc2.orig/net/ipv4/netfilter/Makefile linux-2.6.23-rc2/net/ipv4/netfilter/Makefile
247 --- linux-2.6.23-rc2.orig/net/ipv4/netfilter/Makefile 2007-08-05 21:14:19.000000000 +0200
248 +++ linux-2.6.23-rc2/net/ipv4/netfilter/Makefile 2007-08-05 10:53:55.000000000 +0200
250 +obj-$(CONFIG_NF_NAT_RTSP) += nf_nat_rtsp.o
251 diff -purN linux-2.6.23-rc2.orig/net/netfilter/Kconfig linux-2.6.23-rc2/net/netfilter/Kconfig
252 --- linux-2.6.23-rc2.orig/net/netfilter/Kconfig 2007-08-05 21:17:02.000000000 +0200
253 +++ linux-2.6.23-rc2/net/netfilter/Kconfig 2007-08-05 11:04:59.000000000 +0200
254 @@ -249,6 +249,16 @@ config NF_CONNTRACK_TFTP
256 To compile it as a module, choose M here. If unsure, say N.
258 +config NF_CONNTRACK_RTSP
259 + tristate "RTSP protocol support"
260 + depends on NF_CONNTRACK
262 + Support the RTSP protocol. This allows UDP transports to be setup
263 + properly, including RTP and RDT.
265 + If you want to compile it as a module, say 'M' here and read
266 + Documentation/modules.txt. If unsure, say 'Y'.
268 config NF_CONNTRACK_MMS
269 tristate 'MMS protocol support'
270 depends on NF_CONNTRACK
271 diff -purN linux-2.6.23-rc2.orig/net/netfilter/Makefile linux-2.6.23-rc2/net/netfilter/Makefile
272 --- linux-2.6.23-rc2.orig/net/netfilter/Makefile 2007-08-05 21:17:02.000000000 +0200
273 +++ linux-2.6.23-rc2/net/netfilter/Makefile 2007-08-05 11:04:59.000000000 +0200
275 +obj-$(CONFIG_NF_CONNTRACK_RTSP) += nf_conntrack_rtsp.o
276 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
277 --- linux-2.6.24-rc1.orig/include/linux/netfilter/nf_conntrack_rtsp.h 1970-01-01 01:00:00.000000000 +0100
278 +++ linux-2.6.24-rc1/include/linux/netfilter/nf_conntrack_rtsp.h 2007-11-10 17:16:36.000000000 +0100
281 + * RTSP extension for IP connection tracking.
282 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
283 + * based on ip_conntrack_irc.h
285 + * This program is free software; you can redistribute it and/or
286 + * modify it under the terms of the GNU General Public License
287 + * as published by the Free Software Foundation; either version
288 + * 2 of the License, or (at your option) any later version.
290 +#ifndef _IP_CONNTRACK_RTSP_H
291 +#define _IP_CONNTRACK_RTSP_H
293 +//#define IP_NF_RTSP_DEBUG 1
294 +#define IP_NF_RTSP_VERSION "0.6.21"
297 +/* port block types */
299 + pb_single, /* client_port=x */
300 + pb_range, /* client_port=x-y */
301 + pb_discon /* client_port=x/y (rtspbis) */
304 +/* We record seq number and length of rtsp headers here, all in host order. */
307 + * This structure is per expected connection. It is a member of struct
308 + * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
309 + * there and we are expected to only store the length of the data which
310 + * needs replaced. If a packet contains multiple RTSP messages, we create
311 + * one expected connection per message.
313 + * We use these variables to mark the entire header block. This may seem
314 + * like overkill, but the nature of RTSP requires it. A header may appear
315 + * multiple times in a message. We must treat two Transport headers the
316 + * same as one Transport header with two entries.
318 +struct ip_ct_rtsp_expect
320 + u_int32_t len; /* length of header block */
321 + portblock_t pbtype; /* Type of port block that was requested */
322 + u_int16_t loport; /* Port that was requested, low or first */
323 + u_int16_t hiport; /* Port that was requested, high or second */
325 + uint method; /* RTSP method */
326 + uint cseq; /* CSeq from request */
330 +extern unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
331 + enum ip_conntrack_info ctinfo,
332 + unsigned int matchoff, unsigned int matchlen,
333 + struct ip_ct_rtsp_expect *prtspexp,
334 + struct nf_conntrack_expect *exp);
336 +extern void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
338 +#define RTSP_PORT 554
340 +#endif /* __KERNEL__ */
342 +#endif /* _IP_CONNTRACK_RTSP_H */
343 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
344 --- linux-2.6.24-rc1.orig/net/ipv4/netfilter/nf_nat_rtsp.c 1970-01-01 01:00:00.000000000 +0100
345 +++ linux-2.6.24-rc1/net/ipv4/netfilter/nf_nat_rtsp.c 2007-11-10 17:17:21.000000000 +0100
348 + * RTSP extension for TCP NAT alteration
349 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
350 + * based on ip_nat_irc.c
352 + * This program is free software; you can redistribute it and/or
353 + * modify it under the terms of the GNU General Public License
354 + * as published by the Free Software Foundation; either version
355 + * 2 of the License, or (at your option) any later version.
357 + * Module load syntax:
358 + * insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
359 + * stunaddr=<address>
360 + * destaction=[auto|strip|none]
362 + * If no ports are specified, the default will be port 554 only.
364 + * stunaddr specifies the address used to detect that a client is using STUN.
365 + * If this address is seen in the destination parameter, it is assumed that
366 + * the client has already punched a UDP hole in the firewall, so we don't
367 + * mangle the client_port. If none is specified, it is autodetected. It
368 + * only needs to be set if you have multiple levels of NAT. It should be
369 + * set to the external address that the STUN clients detect. Note that in
370 + * this case, it will not be possible for clients to use UDP with servers
371 + * between the NATs.
373 + * If no destaction is specified, auto is used.
374 + * destaction=auto: strip destination parameter if it is not stunaddr.
375 + * destaction=strip: always strip destination parameter (not recommended).
376 + * destaction=none: do not touch destination parameter (not recommended).
379 +#include <linux/module.h>
380 +#include <net/tcp.h>
381 +#include <net/netfilter/nf_nat_helper.h>
382 +#include <net/netfilter/nf_nat_rule.h>
383 +#include <linux/netfilter/nf_conntrack_rtsp.h>
384 +#include <net/netfilter/nf_conntrack_expect.h>
386 +#include <linux/inet.h>
387 +#include <linux/ctype.h>
388 +#define NF_NEED_STRNCASECMP
389 +#define NF_NEED_STRTOU16
390 +#include <linux/netfilter_helpers.h>
391 +#define NF_NEED_MIME_NEXTLINE
392 +#include <linux/netfilter_mime.h>
395 +#define DSTACT_AUTO 0
396 +#define DSTACT_STRIP 1
397 +#define DSTACT_NONE 2
399 +static char* stunaddr = NULL;
400 +static char* destaction = NULL;
402 +static u_int32_t extip = 0;
403 +static int dstact = 0;
405 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
406 +MODULE_DESCRIPTION("RTSP network address translation module");
407 +MODULE_LICENSE("GPL");
408 +module_param(stunaddr, charp, 0644);
409 +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
410 +module_param(destaction, charp, 0644);
411 +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
413 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
415 +/*** helper functions ***/
418 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
420 + struct iphdr* iph = ip_hdr(skb);
421 + struct tcphdr* tcph = (void *)iph + ip_hdrlen(skb);
423 + *pptcpdata = (char*)tcph + tcph->doff*4;
424 + *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
427 +/*** nat functions ***/
430 + * Mangle the "Transport:" header:
431 + * - Replace all occurences of "client_port=<spec>"
432 + * - Handle destination parameter
435 + * ct, ctinfo = conntrack context
437 + * tranoff = Transport header offset from TCP data
438 + * tranlen = Transport header length (incl. CRLF)
439 + * rport_lo = replacement low port (host endian)
440 + * rport_hi = replacement high port (host endian)
442 + * Returns packet size difference.
444 + * Assumes that a complete transport header is present, ending with CR or LF
447 +rtsp_mangle_tran(enum ip_conntrack_info ctinfo,
448 + struct nf_conntrack_expect* exp,
449 + struct ip_ct_rtsp_expect* prtspexp,
450 + struct sk_buff* skb, uint tranoff, uint tranlen)
455 + char rbuf1[16]; /* Replacement buffer (one port) */
456 + uint rbuf1len; /* Replacement len (one port) */
457 + char rbufa[16]; /* Replacement buffer (all ports) */
458 + uint rbufalen; /* Replacement len (all ports) */
460 + u_int16_t loport, hiport;
462 + uint diff; /* Number of bytes we removed */
464 + struct nf_conn *ct = exp->master;
465 + struct nf_conntrack_tuple *t;
467 + char szextaddr[15+1];
471 + get_skb_tcpdata(skb, &ptcp, &tcplen);
472 + ptran = ptcp+tranoff;
474 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
475 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
476 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
478 + pr_info("sanity check failed\n");
482 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
484 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
486 + t->dst.u3.ip = newip;
488 + extaddrlen = extip ? sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(extip))
489 + : sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(newip));
490 + pr_debug("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
492 + rbuf1len = rbufalen = 0;
493 + switch (prtspexp->pbtype)
496 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
498 + t->dst.u.udp.port = htons(loport);
499 + if (nf_ct_expect_related(exp) == 0)
501 + pr_debug("using port %hu\n", loport);
507 + rbuf1len = sprintf(rbuf1, "%hu", loport);
508 + rbufalen = sprintf(rbufa, "%hu", loport);
512 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
514 + t->dst.u.udp.port = htons(loport);
515 + if (nf_ct_expect_related(exp) == 0)
517 + hiport = loport + 1; //~exp->mask.dst.u.udp.port;
518 + pr_debug("using ports %hu-%hu\n", loport, hiport);
524 + rbuf1len = sprintf(rbuf1, "%hu", loport);
525 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
529 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
531 + t->dst.u.udp.port = htons(loport);
532 + if (nf_ct_expect_related(exp) == 0)
534 + pr_debug("using port %hu (1 of 2)\n", loport);
538 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
540 + t->dst.u.udp.port = htons(hiport);
541 + if (nf_ct_expect_related(exp) == 0)
543 + pr_debug("using port %hu (2 of 2)\n", hiport);
547 + if (loport != 0 && hiport != 0)
549 + rbuf1len = sprintf(rbuf1, "%hu", loport);
550 + if (hiport == loport+1)
552 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
556 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
564 + return 0; /* cannot get replacement port(s) */
567 + /* Transport: tran;field;field=val,tran;field;field=val,... */
568 + while (off < tranlen)
571 + const char* pparamend;
574 + pparamend = memchr(ptran+off, ',', tranlen-off);
575 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
576 + nextparamoff = pparamend-ptcp;
579 + * We pass over each param twice. On the first pass, we look for a
580 + * destination= field. It is handled by the security policy. If it
581 + * is present, allowed, and equal to our external address, we assume
582 + * that STUN is being used and we leave the client_port= field alone.
586 + while (off < nextparamoff)
588 + const char* pfieldend;
591 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
592 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
594 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
596 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
600 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
602 + diff = nextfieldoff-off;
603 + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
604 + off, diff, NULL, 0))
606 + /* mangle failed, all we can do is bail */
607 + nf_ct_unexpect_related(exp);
610 + get_skb_tcpdata(skb, &ptcp, &tcplen);
611 + ptran = ptcp+tranoff;
613 + nextparamoff -= diff;
614 + nextfieldoff -= diff;
618 + off = nextfieldoff;
625 + while (off < nextparamoff)
627 + const char* pfieldend;
630 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
631 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
633 + if (strncmp(ptran+off, "client_port=", 12) == 0)
639 + char* rbuf = rbuf1;
640 + uint rbuflen = rbuf1len;
643 + origoff = (ptran-ptcp)+off;
645 + numlen = nf_strtou16(ptran+off, &port);
648 + if (port != prtspexp->loport)
650 + pr_debug("multiple ports found, port %hu ignored\n", port);
654 + if (ptran[off] == '-' || ptran[off] == '/')
658 + numlen = nf_strtou16(ptran+off, &port);
662 + rbuflen = rbufalen;
666 + * note we cannot just memcpy() if the sizes are the same.
667 + * the mangle function does skb resizing, checks for a
668 + * cloned skb, and updates the checksums.
670 + * parameter 4 below is offset from start of tcp data.
672 + diff = origlen-rbuflen;
673 + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
674 + origoff, origlen, rbuf, rbuflen))
676 + /* mangle failed, all we can do is bail */
677 + nf_ct_unexpect_related(exp);
680 + get_skb_tcpdata(skb, &ptcp, &tcplen);
681 + ptran = ptcp+tranoff;
683 + nextparamoff -= diff;
684 + nextfieldoff -= diff;
688 + off = nextfieldoff;
691 + off = nextparamoff;
698 +help_out(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
699 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
700 + struct nf_conntrack_expect* exp)
710 + //struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
711 + //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
713 + get_skb_tcpdata(skb, &ptcp, &tcplen);
714 + hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
715 + hdrslen = matchlen;
717 + pr_debug("NAT rtsp help_out\n");
719 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
725 + if (off > hdrsoff+hdrslen)
727 + pr_info("!! overrun !!");
730 + pr_debug("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
732 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
734 + uint oldtcplen = tcplen;
735 + pr_debug("hdr: Transport\n");
736 + if (!rtsp_mangle_tran(ctinfo, exp, prtspexp, skb, lineoff, linelen))
738 + pr_debug("hdr: Transport mangle failed");
741 + get_skb_tcpdata(skb, &ptcp, &tcplen);
742 + hdrslen -= (oldtcplen-tcplen);
743 + off -= (oldtcplen-tcplen);
744 + lineoff -= (oldtcplen-tcplen);
745 + linelen -= (oldtcplen-tcplen);
746 + pr_debug("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
754 +help(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
755 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
756 + struct nf_conntrack_expect* exp)
758 + int dir = CTINFO2DIR(ctinfo);
759 + int rc = NF_ACCEPT;
763 + case IP_CT_DIR_ORIGINAL:
764 + rc = help_out(skb, ctinfo, matchoff, matchlen, prtspexp, exp);
766 + case IP_CT_DIR_REPLY:
767 + pr_debug("unmangle ! %u\n", ctinfo);
768 + /* XXX: unmangle */
772 + //UNLOCK_BH(&ip_rtsp_lock);
777 +static void expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
779 + struct nf_nat_multi_range_compat mr;
780 + u_int32_t newdstip, newsrcip, newip;
782 + struct nf_conn *master = ct->master;
784 + newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
785 + newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
786 + //FIXME (how to port that ?)
787 + //code from 2.4 : newip = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) ? newsrcip : newdstip;
790 + pr_debug("newsrcip=%u.%u.%u.%u, newdstip=%u.%u.%u.%u, newip=%u.%u.%u.%u\n",
791 + NIPQUAD(newsrcip), NIPQUAD(newdstip), NIPQUAD(newip));
794 + // We don't want to manip the per-protocol, just the IPs.
795 + mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
796 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
798 + nf_nat_setup_info(ct, &mr.range[0], IP_NAT_MANIP_DST);
802 +static void __exit fini(void)
804 + nf_nat_rtsp_hook = NULL;
805 + nf_nat_rtsp_hook_expectfn = NULL;
809 +static int __init init(void)
811 + printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
813 + BUG_ON(nf_nat_rtsp_hook);
814 + nf_nat_rtsp_hook = help;
815 + nf_nat_rtsp_hook_expectfn = &expected;
817 + if (stunaddr != NULL)
818 + extip = in_aton(stunaddr);
820 + if (destaction != NULL) {
821 + if (strcmp(destaction, "auto") == 0)
822 + dstact = DSTACT_AUTO;
824 + if (strcmp(destaction, "strip") == 0)
825 + dstact = DSTACT_STRIP;
827 + if (strcmp(destaction, "none") == 0)
828 + dstact = DSTACT_NONE;
836 diff -purN linux-2.6.26.orig/net/netfilter/nf_conntrack_rtsp.c linux-2.6.26/net/netfilter/nf_conntrack_rtsp.c
837 --- linux-2.6.26.orig/net/netfilter/nf_conntrack_rtsp.c 1970-01-01 01:00:00.000000000 +0100
838 +++ linux-2.6.26/net/netfilter/nf_conntrack_rtsp.c 2008-07-24 16:19:36.000000000 +0200
841 + * RTSP extension for IP connection tracking
842 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
843 + * based on ip_conntrack_irc.c
845 + * This program is free software; you can redistribute it and/or
846 + * modify it under the terms of the GNU General Public License
847 + * as published by the Free Software Foundation; either version
848 + * 2 of the License, or (at your option) any later version.
850 + * Module load syntax:
851 + * insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
852 + * max_outstanding=n setup_timeout=secs
854 + * If no ports are specified, the default will be port 554.
856 + * With max_outstanding you can define the maximum number of not yet
857 + * answered SETUP requests per RTSP session (default 8).
858 + * With setup_timeout you can specify how long the system waits for
859 + * an expected data channel (default 300 seconds).
861 + * 2005-02-13: Harald Welte <laforge at netfilter.org>
863 + * - update to recent post-2.6.11 api changes
864 + * 2006-09-14: Steven Van Acker <deepstar at singularity.be>
865 + * - removed calls to NAT code from conntrack helper: NAT no longer needed to use rtsp-conntrack
866 + * 2007-04-18: Michael Guntsche <mike at it-loops.com>
867 + * - Port to new NF API
870 +#include <linux/module.h>
871 +#include <linux/netfilter.h>
872 +#include <linux/ip.h>
873 +#include <linux/inet.h>
874 +#include <net/tcp.h>
876 +#include <net/netfilter/nf_conntrack.h>
877 +#include <net/netfilter/nf_conntrack_expect.h>
878 +#include <net/netfilter/nf_conntrack_helper.h>
879 +#include <linux/netfilter/nf_conntrack_rtsp.h>
881 +#define NF_NEED_STRNCASECMP
882 +#define NF_NEED_STRTOU16
883 +#define NF_NEED_STRTOU32
884 +#define NF_NEED_NEXTLINE
885 +#include <linux/netfilter_helpers.h>
886 +#define NF_NEED_MIME_NEXTLINE
887 +#include <linux/netfilter_mime.h>
889 +#include <linux/ctype.h>
890 +#define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
893 +static int ports[MAX_PORTS];
894 +static int num_ports = 0;
895 +static int max_outstanding = 8;
896 +static unsigned int setup_timeout = 300;
898 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
899 +MODULE_DESCRIPTION("RTSP connection tracking module");
900 +MODULE_LICENSE("GPL");
901 +module_param_array(ports, int, &num_ports, 0400);
902 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
903 +module_param(max_outstanding, int, 0400);
904 +MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
905 +module_param(setup_timeout, int, 0400);
906 +MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
908 +static char *rtsp_buffer;
909 +static DEFINE_SPINLOCK(rtsp_buffer_lock);
911 +static struct nf_conntrack_expect_policy rtsp_exp_policy;
913 +unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
914 + enum ip_conntrack_info ctinfo,
915 + unsigned int matchoff, unsigned int matchlen,struct ip_ct_rtsp_expect* prtspexp,
916 + struct nf_conntrack_expect *exp);
917 +void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
919 +EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook);
922 + * Max mappings we will allow for one RTSP connection (for RTP, the number
923 + * of allocated ports is twice this value). Note that SMIL burns a lot of
924 + * ports so keep this reasonably high. If this is too low, you will see a
925 + * lot of "no free client map entries" messages.
927 +#define MAX_PORT_MAPS 16
929 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
931 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
934 + * Parse an RTSP packet.
936 + * Returns zero if parsing failed.
939 + * IN ptcp tcp data pointer
940 + * IN tcplen tcp data len
941 + * IN/OUT ptcpoff points to current tcp offset
942 + * OUT phdrsoff set to offset of rtsp headers
943 + * OUT phdrslen set to length of rtsp headers
944 + * OUT pcseqoff set to offset of CSeq header
945 + * OUT pcseqlen set to length of CSeq header
948 +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
949 + uint* phdrsoff, uint* phdrslen,
950 + uint* pcseqoff, uint* pcseqlen,
951 + uint* transoff, uint* translen)
953 + uint entitylen = 0;
957 + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
960 + *phdrsoff = *ptcpoff;
961 + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
962 + if (linelen == 0) {
964 + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
967 + if (lineoff+linelen > tcplen) {
968 + pr_info("!! overrun !!\n");
972 + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
973 + *pcseqoff = lineoff;
974 + *pcseqlen = linelen;
977 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
978 + *transoff = lineoff;
979 + *translen = linelen;
982 + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0) {
983 + uint off = lineoff+15;
984 + SKIP_WSPACE(ptcp+lineoff, linelen, off);
985 + nf_strtou32(ptcp+off, &entitylen);
988 + *phdrslen = (*ptcpoff) - (*phdrsoff);
994 + * Find lo/hi client ports (if any) in transport header
996 + * ptcp, tcplen = packet
997 + * tranoff, tranlen = buffer to search
1000 + * pport_lo, pport_hi = lo/hi ports (host endian)
1002 + * Returns nonzero if any client ports found
1004 + * Note: it is valid (and expected) for the client to request multiple
1005 + * transports, so we need to parse the entire line.
1008 +rtsp_parse_transport(char* ptran, uint tranlen,
1009 + struct ip_ct_rtsp_expect* prtspexp)
1014 + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
1015 + nf_strncasecmp(ptran, "Transport:", 10) != 0) {
1016 + pr_info("sanity check failed\n");
1020 + pr_debug("tran='%.*s'\n", (int)tranlen, ptran);
1022 + SKIP_WSPACE(ptran, tranlen, off);
1024 + /* Transport: tran;field;field=val,tran;field;field=val,... */
1025 + while (off < tranlen) {
1026 + const char* pparamend;
1027 + uint nextparamoff;
1029 + pparamend = memchr(ptran+off, ',', tranlen-off);
1030 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
1031 + nextparamoff = pparamend-ptran;
1033 + while (off < nextparamoff) {
1034 + const char* pfieldend;
1035 + uint nextfieldoff;
1037 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1038 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1040 + if (strncmp(ptran+off, "client_port=", 12) == 0) {
1045 + numlen = nf_strtou16(ptran+off, &port);
1047 + if (prtspexp->loport != 0 && prtspexp->loport != port)
1048 + pr_debug("multiple ports found, port %hu ignored\n", port);
1050 + pr_debug("lo port found : %hu\n", port);
1051 + prtspexp->loport = prtspexp->hiport = port;
1052 + if (ptran[off] == '-') {
1054 + numlen = nf_strtou16(ptran+off, &port);
1056 + prtspexp->pbtype = pb_range;
1057 + prtspexp->hiport = port;
1059 + // If we have a range, assume rtp:
1060 + // loport must be even, hiport must be loport+1
1061 + if ((prtspexp->loport & 0x0001) != 0 ||
1062 + prtspexp->hiport != prtspexp->loport+1) {
1063 + pr_debug("incorrect range: %hu-%hu, correcting\n",
1064 + prtspexp->loport, prtspexp->hiport);
1065 + prtspexp->loport &= 0xfffe;
1066 + prtspexp->hiport = prtspexp->loport+1;
1068 + } else if (ptran[off] == '/') {
1070 + numlen = nf_strtou16(ptran+off, &port);
1072 + prtspexp->pbtype = pb_discon;
1073 + prtspexp->hiport = port;
1080 + * Note we don't look for the destination parameter here.
1081 + * If we are using NAT, the NAT module will handle it. If not,
1082 + * and the client is sending packets elsewhere, the expectation
1083 + * will quietly time out.
1086 + off = nextfieldoff;
1089 + off = nextparamoff;
1095 +void expected(struct nf_conn *ct, struct nf_conntrack_expect *exp)
1097 + typeof(nf_nat_rtsp_hook_expectfn) nf_nat_rtsp_expectfn;
1098 + nf_nat_rtsp_expectfn = rcu_dereference(nf_nat_rtsp_hook_expectfn);
1099 + if(nf_nat_rtsp_expectfn && ct->master->status & IPS_NAT_MASK) {
1100 + nf_nat_rtsp_expectfn(ct,exp);
1104 +/*** conntrack functions ***/
1106 +/* outbound packet: client->server */
1109 +help_out(struct sk_buff *skb, unsigned char *rb_ptr, unsigned int datalen,
1110 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1112 + struct ip_ct_rtsp_expect expinfo;
1114 + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
1115 + //struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
1116 + //uint tcplen = pktlen - iph->ihl * 4;
1117 + char* pdata = rb_ptr;
1118 + //uint datalen = tcplen - tcph->doff * 4;
1120 + int ret = NF_ACCEPT;
1122 + struct nf_conntrack_expect *exp;
1126 + typeof(nf_nat_rtsp_hook) nf_nat_rtsp;
1128 + memset(&expinfo, 0, sizeof(expinfo));
1130 + while (dataoff < datalen) {
1131 + uint cmdoff = dataoff;
1136 + uint transoff = 0;
1137 + uint translen = 0;
1140 + if (!rtsp_parse_message(pdata, datalen, &dataoff,
1141 + &hdrsoff, &hdrslen,
1142 + &cseqoff, &cseqlen,
1143 + &transoff, &translen))
1144 + break; /* not a valid message */
1146 + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
1147 + continue; /* not a SETUP message */
1148 + pr_debug("found a setup message\n");
1152 + rtsp_parse_transport(pdata+transoff, translen, &expinfo);
1155 + if (expinfo.loport == 0) {
1156 + pr_debug("no udp transports found\n");
1157 + continue; /* no udp transports found */
1160 + pr_debug("udp transport found, ports=(%d,%hu,%hu)\n",
1161 + (int)expinfo.pbtype, expinfo.loport, expinfo.hiport);
1163 + exp = nf_ct_expect_alloc(ct);
1169 + be_loport = htons(expinfo.loport);
1171 + nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
1172 + &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3,
1173 + IPPROTO_UDP, NULL, &be_loport);
1177 + exp->expectfn = expected;
1180 + if (expinfo.pbtype == pb_range) {
1181 + pr_debug("Changing expectation mask to handle multiple ports\n");
1182 + //exp->mask.dst.u.udp.port = 0xfffe;
1185 + pr_debug("expect_related %u.%u.%u.%u:%u-%u.%u.%u.%u:%u\n",
1186 + NIPQUAD(exp->tuple.src.u3.ip),
1187 + ntohs(exp->tuple.src.u.udp.port),
1188 + NIPQUAD(exp->tuple.dst.u3.ip),
1189 + ntohs(exp->tuple.dst.u.udp.port));
1191 + nf_nat_rtsp = rcu_dereference(nf_nat_rtsp_hook);
1192 + if (nf_nat_rtsp && ct->status & IPS_NAT_MASK)
1193 + /* pass the request off to the nat helper */
1194 + ret = nf_nat_rtsp(skb, ctinfo, hdrsoff, hdrslen, &expinfo, exp);
1195 + else if (nf_ct_expect_related(exp) != 0) {
1196 + pr_info("nf_conntrack_expect_related failed\n");
1199 + nf_ct_expect_put(exp);
1209 +help_in(struct sk_buff *skb, size_t pktlen,
1210 + struct nf_conn* ct, enum ip_conntrack_info ctinfo)
1215 +static int help(struct sk_buff *skb, unsigned int protoff,
1216 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
1218 + struct tcphdr _tcph, *th;
1219 + unsigned int dataoff, datalen;
1221 + int ret = NF_DROP;
1223 + /* Until there's been traffic both ways, don't look in packets. */
1224 + if (ctinfo != IP_CT_ESTABLISHED &&
1225 + ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
1226 + pr_debug("conntrackinfo = %u\n", ctinfo);
1230 + /* Not whole TCP header? */
1231 + th = skb_header_pointer(skb,protoff, sizeof(_tcph), &_tcph);
1237 + dataoff = protoff + th->doff*4;
1238 + datalen = skb->len - dataoff;
1239 + if (dataoff >= skb->len)
1242 + spin_lock_bh(&rtsp_buffer_lock);
1243 + rb_ptr = skb_header_pointer(skb, dataoff,
1244 + skb->len - dataoff, rtsp_buffer);
1245 + BUG_ON(rb_ptr == NULL);
1248 + /* Checksum invalid? Ignore. */
1249 + /* FIXME: Source route IP option packets --RR */
1250 + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
1251 + csum_partial((char*)tcph, tcplen, 0)))
1253 + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
1254 + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
1259 + switch (CTINFO2DIR(ctinfo)) {
1260 + case IP_CT_DIR_ORIGINAL:
1261 + ret = help_out(skb, rb_ptr, datalen, ct, ctinfo);
1263 + case IP_CT_DIR_REPLY:
1264 + pr_debug("IP_CT_DIR_REPLY\n");
1265 + /* inbound packet: server->client */
1270 + spin_unlock_bh(&rtsp_buffer_lock);
1275 +static struct nf_conntrack_helper rtsp_helpers[MAX_PORTS];
1276 +static char rtsp_names[MAX_PORTS][10];
1278 +/* This function is intentionally _NOT_ defined as __exit */
1283 + for (i = 0; i < num_ports; i++) {
1284 + pr_debug("unregistering port %d\n", ports[i]);
1285 + nf_conntrack_helper_unregister(&rtsp_helpers[i]);
1287 + kfree(rtsp_buffer);
1294 + struct nf_conntrack_helper *hlpr;
1297 + printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1299 + if (max_outstanding < 1) {
1300 + printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
1303 + if (setup_timeout < 0) {
1304 + printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
1308 + rtsp_exp_policy.max_expected = max_outstanding;
1309 + rtsp_exp_policy.timeout = setup_timeout;
1311 + rtsp_buffer = kmalloc(65536, GFP_KERNEL);
1315 + /* If no port given, default to standard rtsp port */
1316 + if (ports[0] == 0) {
1317 + ports[0] = RTSP_PORT;
1320 + for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
1321 + hlpr = &rtsp_helpers[i];
1322 + memset(hlpr, 0, sizeof(struct nf_conntrack_helper));
1323 + hlpr->tuple.src.l3num = AF_INET;
1324 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
1325 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
1326 + //hlpr->mask.src.u.tcp.port = 0xFFFF;
1327 + //hlpr->mask.dst.protonum = 0xFF;
1328 + hlpr->expect_policy = &rtsp_exp_policy;
1329 + hlpr->me = THIS_MODULE;
1330 + hlpr->help = help;
1332 + tmpname = &rtsp_names[i][0];
1333 + if (ports[i] == RTSP_PORT) {
1334 + sprintf(tmpname, "rtsp");
1336 + sprintf(tmpname, "rtsp-%d", i);
1338 + hlpr->name = tmpname;
1340 + pr_debug("port #%d: %d\n", i, ports[i]);
1342 + ret = nf_conntrack_helper_register(hlpr);
1345 + printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
1357 +EXPORT_SYMBOL(nf_nat_rtsp_hook_expectfn);