2 +++ b/include/linux/netfilter/nf_conntrack_rtsp.h
5 + * RTSP extension for IP connection tracking.
6 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
7 + * based on ip_conntrack_irc.h
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.
14 +#ifndef _IP_CONNTRACK_RTSP_H
15 +#define _IP_CONNTRACK_RTSP_H
17 +//#define IP_NF_RTSP_DEBUG 1
18 +#define IP_NF_RTSP_VERSION "0.6.21"
21 +/* port block types */
23 + pb_single, /* client_port=x */
24 + pb_range, /* client_port=x-y */
25 + pb_discon /* client_port=x/y (rtspbis) */
28 +/* We record seq number and length of rtsp headers here, all in host order. */
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.
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.
42 +struct ip_ct_rtsp_expect
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 */
49 + uint method; /* RTSP method */
50 + uint cseq; /* CSeq from request */
54 +extern unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
55 + enum ip_conntrack_info ctinfo,
56 + unsigned int matchoff, unsigned int matchlen,
57 + struct ip_ct_rtsp_expect *prtspexp,
58 + struct nf_conntrack_expect *exp);
60 +extern void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
62 +#define RTSP_PORT 554
64 +#endif /* __KERNEL__ */
66 +#endif /* _IP_CONNTRACK_RTSP_H */
68 +++ b/include/linux/netfilter_helpers.h
71 + * Helpers for netfiler modules. This file provides implementations for basic
72 + * functions such as strncasecmp(), etc.
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()
80 +#ifndef _NETFILTER_HELPERS_H
81 +#define _NETFILTER_HELPERS_H
83 +/* Only include these functions for kernel code. */
86 +#include <linux/ctype.h>
87 +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
90 + * The standard strncasecmp()
92 +#ifdef NF_NEED_STRNCASECMP
94 +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
96 + if (s1 == NULL || s2 == NULL)
98 + if (s1 == NULL && s2 == NULL)
102 + return (s1 == NULL) ? -1 : 1;
104 + while (len > 0 && tolower(*s1) == tolower(*s2))
110 + return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
112 +#endif /* NF_NEED_STRNCASECMP */
115 + * Parse a string containing a 16-bit unsigned integer.
116 + * Returns the number of chars used, or zero if no number is found.
118 +#ifdef NF_NEED_STRTOU16
120 +nf_strtou16(const char* pbuf, u_int16_t* pval)
125 + while (isdigit(pbuf[n]))
127 + *pval = (*pval * 10) + (pbuf[n] - '0');
133 +#endif /* NF_NEED_STRTOU16 */
136 + * Parse a string containing a 32-bit unsigned integer.
137 + * Returns the number of chars used, or zero if no number is found.
139 +#ifdef NF_NEED_STRTOU32
141 +nf_strtou32(const char* pbuf, u_int32_t* pval)
146 + while (pbuf[n] >= '0' && pbuf[n] <= '9')
148 + *pval = (*pval * 10) + (pbuf[n] - '0');
154 +#endif /* NF_NEED_STRTOU32 */
157 + * Given a buffer and length, advance to the next line and mark the current
160 +#ifdef NF_NEED_NEXTLINE
162 +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
172 + while (p[off] != '\n')
183 + /* if we saw a crlf, physlen needs adjusted */
184 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
189 + /* advance past the newline */
193 + *plinelen = physlen;
198 +#endif /* NF_NEED_NEXTLINE */
200 +#endif /* __KERNEL__ */
202 +#endif /* _NETFILTER_HELPERS_H */
204 +++ b/include/linux/netfilter_mime.h
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.
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()
215 +#ifndef _NETFILTER_MIME_H
216 +#define _NETFILTER_MIME_H
218 +/* Only include these functions for kernel code. */
221 +#include <linux/ctype.h>
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).
228 + * 'line' in this context means logical line (includes LWS continuations).
229 + * Returns 1 on success, 0 on failure.
231 +#ifdef NF_NEED_MIME_NEXTLINE
233 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
237 + int is_first_line = 1;
246 + while (p[off] != '\n')
257 + /* if we saw a crlf, physlen needs adjusted */
258 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
263 + /* advance past the newline */
266 + /* check for an empty line */
272 + /* check for colon on the first physical line */
276 + if (memchr(p+(*poff), ':', physlen) == NULL)
282 + while (p[off] == ' ' || p[off] == '\t');
285 + *plinelen = (physlen == 0) ? 0 : (off - *poff);
290 +#endif /* NF_NEED_MIME_NEXTLINE */
292 +#endif /* __KERNEL__ */
294 +#endif /* _NETFILTER_MIME_H */
295 --- a/net/ipv4/netfilter/Makefile
296 +++ b/net/ipv4/netfilter/Makefile
297 @@ -23,6 +23,7 @@ obj-$(CONFIG_NF_NAT_AMANDA) += nf_nat_am
298 obj-$(CONFIG_NF_NAT_FTP) += nf_nat_ftp.o
299 obj-$(CONFIG_NF_NAT_H323) += nf_nat_h323.o
300 obj-$(CONFIG_NF_NAT_IRC) += nf_nat_irc.o
301 +obj-$(CONFIG_NF_NAT_RTSP) += nf_nat_rtsp.o
302 obj-$(CONFIG_NF_NAT_PPTP) += nf_nat_pptp.o
303 obj-$(CONFIG_NF_NAT_SIP) += nf_nat_sip.o
304 obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o
305 --- a/net/netfilter/Kconfig
306 +++ b/net/netfilter/Kconfig
307 @@ -249,6 +249,16 @@ config NF_CONNTRACK_TFTP
309 To compile it as a module, choose M here. If unsure, say N.
311 +config NF_CONNTRACK_RTSP
312 + tristate "RTSP protocol support"
313 + depends on NF_CONNTRACK
315 + Support the RTSP protocol. This allows UDP transports to be setup
316 + properly, including RTP and RDT.
318 + If you want to compile it as a module, say 'M' here and read
319 + Documentation/modules.txt. If unsure, say 'Y'.
322 tristate 'Connection tracking netlink interface (EXPERIMENTAL)'
323 depends on EXPERIMENTAL && NF_CONNTRACK && NETFILTER_NETLINK
324 --- a/net/netfilter/Makefile
325 +++ b/net/netfilter/Makefile
326 @@ -33,6 +33,7 @@ obj-$(CONFIG_NF_CONNTRACK_PPTP) += nf_co
327 obj-$(CONFIG_NF_CONNTRACK_SANE) += nf_conntrack_sane.o
328 obj-$(CONFIG_NF_CONNTRACK_SIP) += nf_conntrack_sip.o
329 obj-$(CONFIG_NF_CONNTRACK_TFTP) += nf_conntrack_tftp.o
330 +obj-$(CONFIG_NF_CONNTRACK_RTSP) += nf_conntrack_rtsp.o
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 @@ config NF_NAT_IRC
337 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
338 default NF_NAT && NF_CONNTRACK_IRC
342 + depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
343 + default NF_NAT && NF_CONNTRACK_RTSP
347 depends on IP_NF_IPTABLES && NF_CONNTRACK && NF_NAT
349 +++ b/net/netfilter/nf_conntrack_rtsp.c
352 + * RTSP extension for IP connection tracking
353 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
354 + * based on ip_conntrack_irc.c
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.
361 + * Module load syntax:
362 + * insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
363 + * max_outstanding=n setup_timeout=secs
365 + * If no ports are specified, the default will be port 554.
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).
372 + * 2005-02-13: Harald Welte <laforge at netfilter.org>
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
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>
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>
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>
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)
404 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
406 +#define DEBUGP(fmt, args...)
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;
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");
425 +static char *rtsp_buffer;
426 +static DEFINE_SPINLOCK(rtsp_buffer_lock);
428 +unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
429 + enum ip_conntrack_info ctinfo,
430 + unsigned int matchoff, unsigned int matchlen,struct ip_ct_rtsp_expect* prtspexp,
431 + struct nf_conntrack_expect *exp);
432 +void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
434 +EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook);
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.
442 +#define MAX_PORT_MAPS 16
444 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
446 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
449 + * Parse an RTSP packet.
451 + * Returns zero if parsing failed.
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
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)
468 + uint entitylen = 0;
472 + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
475 + *phdrsoff = *ptcpoff;
476 + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
477 + if (linelen == 0) {
479 + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
482 + if (lineoff+linelen > tcplen) {
483 + INFOP("!! overrun !!\n");
487 + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
488 + *pcseqoff = lineoff;
489 + *pcseqlen = linelen;
492 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
493 + *transoff = lineoff;
494 + *translen = linelen;
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);
503 + *phdrslen = (*ptcpoff) - (*phdrsoff);
509 + * Find lo/hi client ports (if any) in transport header
511 + * ptcp, tcplen = packet
512 + * tranoff, tranlen = buffer to search
515 + * pport_lo, pport_hi = lo/hi ports (host endian)
517 + * Returns nonzero if any client ports found
519 + * Note: it is valid (and expected) for the client to request multiple
520 + * transports, so we need to parse the entire line.
523 +rtsp_parse_transport(char* ptran, uint tranlen,
524 + struct ip_ct_rtsp_expect* prtspexp)
529 + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
530 + nf_strncasecmp(ptran, "Transport:", 10) != 0) {
531 + INFOP("sanity check failed\n");
535 + DEBUGP("tran='%.*s'\n", (int)tranlen, ptran);
537 + SKIP_WSPACE(ptran, tranlen, off);
539 + /* Transport: tran;field;field=val,tran;field;field=val,... */
540 + while (off < tranlen) {
541 + const char* pparamend;
544 + pparamend = memchr(ptran+off, ',', tranlen-off);
545 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
546 + nextparamoff = pparamend-ptran;
548 + while (off < nextparamoff) {
549 + const char* pfieldend;
552 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
553 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
555 + if (strncmp(ptran+off, "client_port=", 12) == 0) {
560 + numlen = nf_strtou16(ptran+off, &port);
562 + if (prtspexp->loport != 0 && prtspexp->loport != port)
563 + DEBUGP("multiple ports found, port %hu ignored\n", port);
565 + DEBUGP("lo port found : %hu\n", port);
566 + prtspexp->loport = prtspexp->hiport = port;
567 + if (ptran[off] == '-') {
569 + numlen = nf_strtou16(ptran+off, &port);
571 + prtspexp->pbtype = pb_range;
572 + prtspexp->hiport = port;
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;
583 + } else if (ptran[off] == '/') {
585 + numlen = nf_strtou16(ptran+off, &port);
587 + prtspexp->pbtype = pb_discon;
588 + prtspexp->hiport = port;
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.
601 + off = nextfieldoff;
604 + off = nextparamoff;
610 +void expected(struct nf_conn *ct, struct nf_conntrack_expect *exp)
612 + if(nf_nat_rtsp_hook_expectfn) {
613 + nf_nat_rtsp_hook_expectfn(ct,exp);
617 +/*** conntrack functions ***/
619 +/* outbound packet: client->server */
622 +help_out(struct sk_buff *skb, unsigned char *rb_ptr, unsigned int datalen,
623 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
625 + struct ip_ct_rtsp_expect expinfo;
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;
633 + int ret = NF_ACCEPT;
635 + struct nf_conntrack_expect *exp;
639 + memset(&expinfo, 0, sizeof(expinfo));
641 + while (dataoff < datalen) {
642 + uint cmdoff = dataoff;
651 + if (!rtsp_parse_message(pdata, datalen, &dataoff,
652 + &hdrsoff, &hdrslen,
653 + &cseqoff, &cseqlen,
654 + &transoff, &translen))
655 + break; /* not a valid message */
657 + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
658 + continue; /* not a SETUP message */
659 + DEBUGP("found a setup message\n");
663 + rtsp_parse_transport(pdata+transoff, translen, &expinfo);
666 + if (expinfo.loport == 0) {
667 + DEBUGP("no udp transports found\n");
668 + continue; /* no udp transports found */
671 + DEBUGP("udp transport found, ports=(%d,%hu,%hu)\n",
672 + (int)expinfo.pbtype, expinfo.loport, expinfo.hiport);
674 + exp = nf_ct_expect_alloc(ct);
680 + be_loport = htons(expinfo.loport);
682 + nf_ct_expect_init(exp, ct->tuplehash[!dir].tuple.src.l3num,
683 + &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3,
684 + IPPROTO_UDP, NULL, &be_loport);
688 + exp->expectfn = expected;
691 + if (expinfo.pbtype == pb_range) {
692 + DEBUGP("Changing expectation mask to handle multiple ports\n");
693 + exp->mask.src.u.udp.port = 0xfffe;
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));
702 + if (nf_nat_rtsp_hook)
703 + /* pass the request off to the nat helper */
704 + ret = nf_nat_rtsp_hook(skb, ctinfo, hdrsoff, hdrslen, &expinfo, exp);
705 + else if (nf_ct_expect_related(exp) != 0) {
706 + INFOP("nf_ct_expect_related failed\n");
709 + nf_ct_expect_put(exp);
719 +help_in(struct sk_buff *skb, size_t pktlen,
720 + struct nf_conn* ct, enum ip_conntrack_info ctinfo)
725 +static int help(struct sk_buff *skb, unsigned int protoff,
726 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
728 + struct tcphdr _tcph, *th;
729 + unsigned int dataoff, datalen;
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);
740 + /* Not whole TCP header? */
741 + th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
747 + dataoff = protoff + th->doff*4;
748 + datalen = skb->len - dataoff;
749 + if (dataoff >= skb->len)
752 + spin_lock_bh(&rtsp_buffer_lock);
753 + rb_ptr = skb_header_pointer(skb, dataoff,
754 + skb->len - dataoff, rtsp_buffer);
755 + BUG_ON(rb_ptr == NULL);
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)))
763 + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
764 + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
769 + switch (CTINFO2DIR(ctinfo)) {
770 + case IP_CT_DIR_ORIGINAL:
771 + ret = help_out(skb, rb_ptr, datalen, ct, ctinfo);
773 + case IP_CT_DIR_REPLY:
774 + DEBUGP("IP_CT_DIR_REPLY\n");
775 + /* inbound packet: server->client */
780 + spin_unlock_bh(&rtsp_buffer_lock);
785 +static struct nf_conntrack_helper rtsp_helpers[MAX_PORTS];
786 +static char rtsp_names[MAX_PORTS][10];
788 +/* This function is intentionally _NOT_ defined as __exit */
793 + for (i = 0; i < num_ports; i++) {
794 + DEBUGP("unregistering port %d\n", ports[i]);
795 + nf_conntrack_helper_unregister(&rtsp_helpers[i]);
797 + kfree(rtsp_buffer);
804 + struct nf_conntrack_helper *hlpr;
807 + printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
809 + if (max_outstanding < 1) {
810 + printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
813 + if (setup_timeout < 0) {
814 + printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
818 + rtsp_buffer = kmalloc(65536, GFP_KERNEL);
822 + /* If no port given, default to standard rtsp port */
823 + if (ports[0] == 0) {
824 + ports[0] = RTSP_PORT;
827 + for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
828 + hlpr = &rtsp_helpers[i];
829 + memset(hlpr, 0, sizeof(struct nf_conntrack_helper));
830 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
831 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
832 + hlpr->max_expected = max_outstanding;
833 + hlpr->timeout = setup_timeout;
834 + hlpr->me = THIS_MODULE;
837 + tmpname = &rtsp_names[i][0];
838 + if (ports[i] == RTSP_PORT) {
839 + sprintf(tmpname, "rtsp");
841 + sprintf(tmpname, "rtsp-%d", i);
843 + hlpr->name = tmpname;
845 + DEBUGP("port #%d: %d\n", i, ports[i]);
847 + ret = nf_conntrack_helper_register(hlpr);
850 + printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
862 +EXPORT_SYMBOL(nf_nat_rtsp_hook_expectfn);
865 +++ b/net/ipv4/netfilter/nf_nat_rtsp.c
868 + * RTSP extension for TCP NAT alteration
869 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
870 + * based on ip_nat_irc.c
872 + * This program is free software; you can redistribute it and/or
873 + * modify it under the terms of the GNU General Public License
874 + * as published by the Free Software Foundation; either version
875 + * 2 of the License, or (at your option) any later version.
877 + * Module load syntax:
878 + * insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
879 + * stunaddr=<address>
880 + * destaction=[auto|strip|none]
882 + * If no ports are specified, the default will be port 554 only.
884 + * stunaddr specifies the address used to detect that a client is using STUN.
885 + * If this address is seen in the destination parameter, it is assumed that
886 + * the client has already punched a UDP hole in the firewall, so we don't
887 + * mangle the client_port. If none is specified, it is autodetected. It
888 + * only needs to be set if you have multiple levels of NAT. It should be
889 + * set to the external address that the STUN clients detect. Note that in
890 + * this case, it will not be possible for clients to use UDP with servers
891 + * between the NATs.
893 + * If no destaction is specified, auto is used.
894 + * destaction=auto: strip destination parameter if it is not stunaddr.
895 + * destaction=strip: always strip destination parameter (not recommended).
896 + * destaction=none: do not touch destination parameter (not recommended).
899 +#include <linux/module.h>
900 +#include <net/tcp.h>
901 +#include <net/netfilter/nf_nat_helper.h>
902 +#include <net/netfilter/nf_nat_rule.h>
903 +#include <linux/netfilter/nf_conntrack_rtsp.h>
904 +#include <net/netfilter/nf_conntrack_expect.h>
906 +#include <linux/inet.h>
907 +#include <linux/ctype.h>
908 +#define NF_NEED_STRNCASECMP
909 +#define NF_NEED_STRTOU16
910 +#include <linux/netfilter_helpers.h>
911 +#define NF_NEED_MIME_NEXTLINE
912 +#include <linux/netfilter_mime.h>
914 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
916 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
918 +#define DEBUGP(fmt, args...)
922 +#define DSTACT_AUTO 0
923 +#define DSTACT_STRIP 1
924 +#define DSTACT_NONE 2
926 +static char* stunaddr = NULL;
927 +static char* destaction = NULL;
929 +static u_int32_t extip = 0;
930 +static int dstact = 0;
932 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
933 +MODULE_DESCRIPTION("RTSP network address translation module");
934 +MODULE_LICENSE("GPL");
935 +module_param(stunaddr, charp, 0644);
936 +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
937 +module_param(destaction, charp, 0644);
938 +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
940 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
942 +/*** helper functions ***/
945 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
947 + struct iphdr* iph = ip_hdr(skb);
948 + struct tcphdr* tcph = (void *)iph + ip_hdrlen(skb);
950 + *pptcpdata = (char*)tcph + tcph->doff*4;
951 + *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
954 +/*** nat functions ***/
957 + * Mangle the "Transport:" header:
958 + * - Replace all occurences of "client_port=<spec>"
959 + * - Handle destination parameter
962 + * ct, ctinfo = conntrack context
964 + * tranoff = Transport header offset from TCP data
965 + * tranlen = Transport header length (incl. CRLF)
966 + * rport_lo = replacement low port (host endian)
967 + * rport_hi = replacement high port (host endian)
969 + * Returns packet size difference.
971 + * Assumes that a complete transport header is present, ending with CR or LF
974 +rtsp_mangle_tran(enum ip_conntrack_info ctinfo,
975 + struct nf_conntrack_expect* exp,
976 + struct ip_ct_rtsp_expect* prtspexp,
977 + struct sk_buff* skb, uint tranoff, uint tranlen)
982 + char rbuf1[16]; /* Replacement buffer (one port) */
983 + uint rbuf1len; /* Replacement len (one port) */
984 + char rbufa[16]; /* Replacement buffer (all ports) */
985 + uint rbufalen; /* Replacement len (all ports) */
987 + u_int16_t loport, hiport;
989 + uint diff; /* Number of bytes we removed */
991 + struct nf_conn *ct = exp->master;
992 + struct nf_conntrack_tuple *t;
994 + char szextaddr[15+1];
998 + get_skb_tcpdata(skb, &ptcp, &tcplen);
999 + ptran = ptcp+tranoff;
1001 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
1002 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
1003 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
1005 + INFOP("sanity check failed\n");
1009 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
1011 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
1013 + t->dst.u3.ip = newip;
1015 + extaddrlen = extip ? sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(extip))
1016 + : sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(newip));
1017 + DEBUGP("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
1019 + rbuf1len = rbufalen = 0;
1020 + switch (prtspexp->pbtype)
1023 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1025 + t->dst.u.udp.port = htons(loport);
1026 + if (nf_ct_expect_related(exp) == 0)
1028 + DEBUGP("using port %hu\n", loport);
1034 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1035 + rbufalen = sprintf(rbufa, "%hu", loport);
1039 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
1041 + t->dst.u.udp.port = htons(loport);
1042 + if (nf_ct_expect_related(exp) == 0)
1044 + hiport = loport + ~exp->mask.src.u.udp.port;
1045 + DEBUGP("using ports %hu-%hu\n", loport, hiport);
1051 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1052 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
1056 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1058 + t->dst.u.udp.port = htons(loport);
1059 + if (nf_ct_expect_related(exp) == 0)
1061 + DEBUGP("using port %hu (1 of 2)\n", loport);
1065 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
1067 + t->dst.u.udp.port = htons(hiport);
1068 + if (nf_ct_expect_related(exp) == 0)
1070 + DEBUGP("using port %hu (2 of 2)\n", hiport);
1074 + if (loport != 0 && hiport != 0)
1076 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1077 + if (hiport == loport+1)
1079 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
1083 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
1089 + if (rbuf1len == 0)
1091 + return 0; /* cannot get replacement port(s) */
1094 + /* Transport: tran;field;field=val,tran;field;field=val,... */
1095 + while (off < tranlen)
1098 + const char* pparamend;
1099 + uint nextparamoff;
1101 + pparamend = memchr(ptran+off, ',', tranlen-off);
1102 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
1103 + nextparamoff = pparamend-ptcp;
1106 + * We pass over each param twice. On the first pass, we look for a
1107 + * destination= field. It is handled by the security policy. If it
1108 + * is present, allowed, and equal to our external address, we assume
1109 + * that STUN is being used and we leave the client_port= field alone.
1113 + while (off < nextparamoff)
1115 + const char* pfieldend;
1116 + uint nextfieldoff;
1118 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1119 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1121 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
1123 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
1127 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
1129 + diff = nextfieldoff-off;
1130 + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
1131 + off, diff, NULL, 0))
1133 + /* mangle failed, all we can do is bail */
1134 + nf_ct_unexpect_related(exp);
1137 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1138 + ptran = ptcp+tranoff;
1140 + nextparamoff -= diff;
1141 + nextfieldoff -= diff;
1145 + off = nextfieldoff;
1152 + while (off < nextparamoff)
1154 + const char* pfieldend;
1155 + uint nextfieldoff;
1157 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1158 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1160 + if (strncmp(ptran+off, "client_port=", 12) == 0)
1166 + char* rbuf = rbuf1;
1167 + uint rbuflen = rbuf1len;
1170 + origoff = (ptran-ptcp)+off;
1172 + numlen = nf_strtou16(ptran+off, &port);
1174 + origlen += numlen;
1175 + if (port != prtspexp->loport)
1177 + DEBUGP("multiple ports found, port %hu ignored\n", port);
1181 + if (ptran[off] == '-' || ptran[off] == '/')
1185 + numlen = nf_strtou16(ptran+off, &port);
1187 + origlen += numlen;
1189 + rbuflen = rbufalen;
1193 + * note we cannot just memcpy() if the sizes are the same.
1194 + * the mangle function does skb resizing, checks for a
1195 + * cloned skb, and updates the checksums.
1197 + * parameter 4 below is offset from start of tcp data.
1199 + diff = origlen-rbuflen;
1200 + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
1201 + origoff, origlen, rbuf, rbuflen))
1203 + /* mangle failed, all we can do is bail */
1204 + nf_ct_unexpect_related(exp);
1207 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1208 + ptran = ptcp+tranoff;
1210 + nextparamoff -= diff;
1211 + nextfieldoff -= diff;
1215 + off = nextfieldoff;
1218 + off = nextparamoff;
1225 +help_out(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
1226 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
1227 + struct nf_conntrack_expect* exp)
1237 + //struct iphdr* iph = (struct iphdr*)skb->nh.iph;
1238 + //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
1240 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1241 + hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
1242 + hdrslen = matchlen;
1244 + DEBUGP("NAT rtsp help_out\n");
1246 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
1252 + if (off > hdrsoff+hdrslen)
1254 + INFOP("!! overrun !!");
1257 + DEBUGP("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1259 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
1261 + uint oldtcplen = tcplen;
1262 + DEBUGP("hdr: Transport\n");
1263 + if (!rtsp_mangle_tran(ctinfo, exp, prtspexp, skb, lineoff, linelen))
1265 + DEBUGP("hdr: Transport mangle failed");
1268 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1269 + hdrslen -= (oldtcplen-tcplen);
1270 + off -= (oldtcplen-tcplen);
1271 + lineoff -= (oldtcplen-tcplen);
1272 + linelen -= (oldtcplen-tcplen);
1273 + DEBUGP("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1280 +static unsigned int
1281 +help(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
1282 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
1283 + struct nf_conntrack_expect* exp)
1285 + int dir = CTINFO2DIR(ctinfo);
1286 + int rc = NF_ACCEPT;
1290 + case IP_CT_DIR_ORIGINAL:
1291 + rc = help_out(skb, ctinfo, matchoff, matchlen, prtspexp, exp);
1293 + case IP_CT_DIR_REPLY:
1294 + DEBUGP("unmangle ! %u\n", ctinfo);
1295 + /* XXX: unmangle */
1299 + //UNLOCK_BH(&ip_rtsp_lock);
1304 +static void expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
1306 + struct nf_nat_multi_range_compat mr;
1307 + u_int32_t newdstip, newsrcip, newip;
1309 + struct nf_conn *master = ct->master;
1311 + newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
1312 + newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
1313 + //FIXME (how to port that ?)
1314 + //code from 2.4 : newip = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) ? newsrcip : newdstip;
1317 + DEBUGP("newsrcip=%u.%u.%u.%u, newdstip=%u.%u.%u.%u, newip=%u.%u.%u.%u\n",
1318 + NIPQUAD(newsrcip), NIPQUAD(newdstip), NIPQUAD(newip));
1321 + // We don't want to manip the per-protocol, just the IPs.
1322 + mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
1323 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
1325 + nf_nat_setup_info(ct, &mr.range[0], NF_IP_PRE_ROUTING);
1329 +static void __exit fini(void)
1331 + nf_nat_rtsp_hook = NULL;
1332 + nf_nat_rtsp_hook_expectfn = NULL;
1333 + synchronize_net();
1336 +static int __init init(void)
1338 + printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1340 + BUG_ON(nf_nat_rtsp_hook);
1341 + nf_nat_rtsp_hook = help;
1342 + nf_nat_rtsp_hook_expectfn = &expected;
1344 + if (stunaddr != NULL)
1345 + extip = in_aton(stunaddr);
1347 + if (destaction != NULL) {
1348 + if (strcmp(destaction, "auto") == 0)
1349 + dstact = DSTACT_AUTO;
1351 + if (strcmp(destaction, "strip") == 0)
1352 + dstact = DSTACT_STRIP;
1354 + if (strcmp(destaction, "none") == 0)
1355 + dstact = DSTACT_NONE;