2 +++ b/extensions/rtsp/Kbuild
6 +obj-m += nf_nat_rtsp.o
7 +obj-m += nf_conntrack_rtsp.o
9 +++ b/extensions/rtsp/netfilter_helpers.h
12 + * Helpers for netfiler modules. This file provides implementations for basic
13 + * functions such as strncasecmp(), etc.
15 + * gcc will warn for defined but unused functions, so we only include the
16 + * functions requested. The following macros are used:
17 + * NF_NEED_STRNCASECMP nf_strncasecmp()
18 + * NF_NEED_STRTOU16 nf_strtou16()
19 + * NF_NEED_STRTOU32 nf_strtou32()
21 +#ifndef _NETFILTER_HELPERS_H
22 +#define _NETFILTER_HELPERS_H
24 +/* Only include these functions for kernel code. */
27 +#include <linux/ctype.h>
28 +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
31 + * The standard strncasecmp()
33 +#ifdef NF_NEED_STRNCASECMP
35 +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
37 + if (s1 == NULL || s2 == NULL)
39 + if (s1 == NULL && s2 == NULL)
43 + return (s1 == NULL) ? -1 : 1;
45 + while (len > 0 && tolower(*s1) == tolower(*s2))
51 + return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
53 +#endif /* NF_NEED_STRNCASECMP */
56 + * Parse a string containing a 16-bit unsigned integer.
57 + * Returns the number of chars used, or zero if no number is found.
59 +#ifdef NF_NEED_STRTOU16
61 +nf_strtou16(const char* pbuf, u_int16_t* pval)
66 + while (isdigit(pbuf[n]))
68 + *pval = (*pval * 10) + (pbuf[n] - '0');
74 +#endif /* NF_NEED_STRTOU16 */
77 + * Parse a string containing a 32-bit unsigned integer.
78 + * Returns the number of chars used, or zero if no number is found.
80 +#ifdef NF_NEED_STRTOU32
82 +nf_strtou32(const char* pbuf, u_int32_t* pval)
87 + while (pbuf[n] >= '0' && pbuf[n] <= '9')
89 + *pval = (*pval * 10) + (pbuf[n] - '0');
95 +#endif /* NF_NEED_STRTOU32 */
98 + * Given a buffer and length, advance to the next line and mark the current
101 +#ifdef NF_NEED_NEXTLINE
103 +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
113 + while (p[off] != '\n')
124 + /* if we saw a crlf, physlen needs adjusted */
125 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
130 + /* advance past the newline */
134 + *plinelen = physlen;
139 +#endif /* NF_NEED_NEXTLINE */
141 +#endif /* __KERNEL__ */
143 +#endif /* _NETFILTER_HELPERS_H */
145 +++ b/extensions/rtsp/netfilter_mime.h
148 + * MIME functions for netfilter modules. This file provides implementations
149 + * for basic MIME parsing. MIME headers are used in many protocols, such as
150 + * HTTP, RTSP, SIP, etc.
152 + * gcc will warn for defined but unused functions, so we only include the
153 + * functions requested. The following macros are used:
154 + * NF_NEED_MIME_NEXTLINE nf_mime_nextline()
156 +#ifndef _NETFILTER_MIME_H
157 +#define _NETFILTER_MIME_H
159 +/* Only include these functions for kernel code. */
162 +#include <linux/ctype.h>
165 + * Given a buffer and length, advance to the next line and mark the current
166 + * line. If the current line is empty, *plinelen will be set to zero. If
167 + * not, it will be set to the actual line length (including CRLF).
169 + * 'line' in this context means logical line (includes LWS continuations).
170 + * Returns 1 on success, 0 on failure.
172 +#ifdef NF_NEED_MIME_NEXTLINE
174 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
178 + int is_first_line = 1;
187 + while (p[off] != '\n')
198 + /* if we saw a crlf, physlen needs adjusted */
199 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
204 + /* advance past the newline */
207 + /* check for an empty line */
213 + /* check for colon on the first physical line */
217 + if (memchr(p+(*poff), ':', physlen) == NULL)
223 + while (p[off] == ' ' || p[off] == '\t');
226 + *plinelen = (physlen == 0) ? 0 : (off - *poff);
231 +#endif /* NF_NEED_MIME_NEXTLINE */
233 +#endif /* __KERNEL__ */
235 +#endif /* _NETFILTER_MIME_H */
237 +++ b/extensions/rtsp/nf_conntrack_rtsp.c
240 + * RTSP extension for IP connection tracking
241 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
242 + * based on ip_conntrack_irc.c
244 + * This program is free software; you can redistribute it and/or
245 + * modify it under the terms of the GNU General Public License
246 + * as published by the Free Software Foundation; either version
247 + * 2 of the License, or (at your option) any later version.
249 + * Module load syntax:
250 + * insmod nf_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
251 + * max_outstanding=n setup_timeout=secs
253 + * If no ports are specified, the default will be port 554.
255 + * With max_outstanding you can define the maximum number of not yet
256 + * answered SETUP requests per RTSP session (default 8).
257 + * With setup_timeout you can specify how long the system waits for
258 + * an expected data channel (default 300 seconds).
260 + * 2005-02-13: Harald Welte <laforge at netfilter.org>
262 + * - update to recent post-2.6.11 api changes
263 + * 2006-09-14: Steven Van Acker <deepstar at singularity.be>
264 + * - removed calls to NAT code from conntrack helper: NAT no longer needed to use rtsp-conntrack
265 + * 2007-04-18: Michael Guntsche <mike at it-loops.com>
266 + * - Port to new NF API
269 +#include <linux/module.h>
270 +#include <linux/netfilter.h>
271 +#include <linux/ip.h>
272 +#include <linux/inet.h>
273 +#include <net/tcp.h>
275 +#include <net/netfilter/nf_conntrack.h>
276 +#include <net/netfilter/nf_conntrack_expect.h>
277 +#include <net/netfilter/nf_conntrack_helper.h>
278 +#include "nf_conntrack_rtsp.h"
280 +#define NF_NEED_STRNCASECMP
281 +#define NF_NEED_STRTOU16
282 +#define NF_NEED_STRTOU32
283 +#define NF_NEED_NEXTLINE
284 +#include "netfilter_helpers.h"
285 +#define NF_NEED_MIME_NEXTLINE
286 +#include "netfilter_mime.h"
288 +#include <linux/ctype.h>
289 +#define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
292 +static int ports[MAX_PORTS];
293 +static int num_ports = 0;
294 +static int max_outstanding = 8;
295 +static unsigned int setup_timeout = 300;
297 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
298 +MODULE_DESCRIPTION("RTSP connection tracking module");
299 +MODULE_LICENSE("GPL");
300 +module_param_array(ports, int, &num_ports, 0400);
301 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
302 +module_param(max_outstanding, int, 0400);
303 +MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
304 +module_param(setup_timeout, int, 0400);
305 +MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
307 +static char *rtsp_buffer;
308 +static DEFINE_SPINLOCK(rtsp_buffer_lock);
310 +static struct nf_conntrack_expect_policy rtsp_exp_policy;
312 +unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
313 + enum ip_conntrack_info ctinfo,
314 + unsigned int matchoff, unsigned int matchlen,struct ip_ct_rtsp_expect* prtspexp,
315 + struct nf_conntrack_expect *exp);
316 +void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
318 +EXPORT_SYMBOL_GPL(nf_nat_rtsp_hook);
321 + * Max mappings we will allow for one RTSP connection (for RTP, the number
322 + * of allocated ports is twice this value). Note that SMIL burns a lot of
323 + * ports so keep this reasonably high. If this is too low, you will see a
324 + * lot of "no free client map entries" messages.
326 +#define MAX_PORT_MAPS 16
328 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
330 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
333 + * Parse an RTSP packet.
335 + * Returns zero if parsing failed.
338 + * IN ptcp tcp data pointer
339 + * IN tcplen tcp data len
340 + * IN/OUT ptcpoff points to current tcp offset
341 + * OUT phdrsoff set to offset of rtsp headers
342 + * OUT phdrslen set to length of rtsp headers
343 + * OUT pcseqoff set to offset of CSeq header
344 + * OUT pcseqlen set to length of CSeq header
347 +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
348 + uint* phdrsoff, uint* phdrslen,
349 + uint* pcseqoff, uint* pcseqlen,
350 + uint* transoff, uint* translen)
352 + uint entitylen = 0;
356 + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
359 + *phdrsoff = *ptcpoff;
360 + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen)) {
361 + if (linelen == 0) {
363 + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
366 + if (lineoff+linelen > tcplen) {
367 + pr_info("!! overrun !!\n");
371 + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0) {
372 + *pcseqoff = lineoff;
373 + *pcseqlen = linelen;
376 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0) {
377 + *transoff = lineoff;
378 + *translen = linelen;
381 + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0) {
382 + uint off = lineoff+15;
383 + SKIP_WSPACE(ptcp+lineoff, linelen, off);
384 + nf_strtou32(ptcp+off, &entitylen);
387 + *phdrslen = (*ptcpoff) - (*phdrsoff);
393 + * Find lo/hi client ports (if any) in transport header
395 + * ptcp, tcplen = packet
396 + * tranoff, tranlen = buffer to search
399 + * pport_lo, pport_hi = lo/hi ports (host endian)
401 + * Returns nonzero if any client ports found
403 + * Note: it is valid (and expected) for the client to request multiple
404 + * transports, so we need to parse the entire line.
407 +rtsp_parse_transport(char* ptran, uint tranlen,
408 + struct ip_ct_rtsp_expect* prtspexp)
413 + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
414 + nf_strncasecmp(ptran, "Transport:", 10) != 0) {
415 + pr_info("sanity check failed\n");
419 + pr_debug("tran='%.*s'\n", (int)tranlen, ptran);
421 + SKIP_WSPACE(ptran, tranlen, off);
423 + /* Transport: tran;field;field=val,tran;field;field=val,... */
424 + while (off < tranlen) {
425 + const char* pparamend;
428 + pparamend = memchr(ptran+off, ',', tranlen-off);
429 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
430 + nextparamoff = pparamend-ptran;
432 + while (off < nextparamoff) {
433 + const char* pfieldend;
436 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
437 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
439 + if (strncmp(ptran+off, "client_port=", 12) == 0) {
444 + numlen = nf_strtou16(ptran+off, &port);
446 + if (prtspexp->loport != 0 && prtspexp->loport != port)
447 + pr_debug("multiple ports found, port %hu ignored\n", port);
449 + pr_debug("lo port found : %hu\n", port);
450 + prtspexp->loport = prtspexp->hiport = port;
451 + if (ptran[off] == '-') {
453 + numlen = nf_strtou16(ptran+off, &port);
455 + prtspexp->pbtype = pb_range;
456 + prtspexp->hiport = port;
458 + // If we have a range, assume rtp:
459 + // loport must be even, hiport must be loport+1
460 + if ((prtspexp->loport & 0x0001) != 0 ||
461 + prtspexp->hiport != prtspexp->loport+1) {
462 + pr_debug("incorrect range: %hu-%hu, correcting\n",
463 + prtspexp->loport, prtspexp->hiport);
464 + prtspexp->loport &= 0xfffe;
465 + prtspexp->hiport = prtspexp->loport+1;
467 + } else if (ptran[off] == '/') {
469 + numlen = nf_strtou16(ptran+off, &port);
471 + prtspexp->pbtype = pb_discon;
472 + prtspexp->hiport = port;
479 + * Note we don't look for the destination parameter here.
480 + * If we are using NAT, the NAT module will handle it. If not,
481 + * and the client is sending packets elsewhere, the expectation
482 + * will quietly time out.
485 + off = nextfieldoff;
488 + off = nextparamoff;
494 +void expected(struct nf_conn *ct, struct nf_conntrack_expect *exp)
496 + typeof(nf_nat_rtsp_hook_expectfn) nf_nat_rtsp_expectfn;
497 + nf_nat_rtsp_expectfn = rcu_dereference(nf_nat_rtsp_hook_expectfn);
498 + if(nf_nat_rtsp_expectfn && ct->master->status & IPS_NAT_MASK) {
499 + nf_nat_rtsp_expectfn(ct,exp);
503 +/*** conntrack functions ***/
505 +/* outbound packet: client->server */
508 +help_out(struct sk_buff *skb, unsigned char *rb_ptr, unsigned int datalen,
509 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
511 + struct ip_ct_rtsp_expect expinfo;
513 + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
514 + //struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
515 + //uint tcplen = pktlen - iph->ihl * 4;
516 + char* pdata = rb_ptr;
517 + //uint datalen = tcplen - tcph->doff * 4;
519 + int ret = NF_ACCEPT;
521 + struct nf_conntrack_expect *exp;
525 + typeof(nf_nat_rtsp_hook) nf_nat_rtsp;
527 + memset(&expinfo, 0, sizeof(expinfo));
529 + while (dataoff < datalen) {
530 + uint cmdoff = dataoff;
539 + if (!rtsp_parse_message(pdata, datalen, &dataoff,
540 + &hdrsoff, &hdrslen,
541 + &cseqoff, &cseqlen,
542 + &transoff, &translen))
543 + break; /* not a valid message */
545 + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
546 + continue; /* not a SETUP message */
547 + pr_debug("found a setup message\n");
551 + rtsp_parse_transport(pdata+transoff, translen, &expinfo);
554 + if (expinfo.loport == 0) {
555 + pr_debug("no udp transports found\n");
556 + continue; /* no udp transports found */
559 + pr_debug("udp transport found, ports=(%d,%hu,%hu)\n",
560 + (int)expinfo.pbtype, expinfo.loport, expinfo.hiport);
562 + exp = nf_ct_expect_alloc(ct);
568 + be_loport = htons(expinfo.loport);
570 + nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, nf_ct_l3num(ct),
571 + &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3,
572 + IPPROTO_UDP, NULL, &be_loport);
576 + exp->expectfn = expected;
579 + if (expinfo.pbtype == pb_range) {
580 + pr_debug("Changing expectation mask to handle multiple ports\n");
581 + //exp->mask.dst.u.udp.port = 0xfffe;
584 + pr_debug("expect_related %pI4:%u-%pI4:%u\n",
585 + &exp->tuple.src.u3.ip,
586 + ntohs(exp->tuple.src.u.udp.port),
587 + &exp->tuple.dst.u3.ip,
588 + ntohs(exp->tuple.dst.u.udp.port));
590 + nf_nat_rtsp = rcu_dereference(nf_nat_rtsp_hook);
591 + if (nf_nat_rtsp && ct->status & IPS_NAT_MASK)
592 + /* pass the request off to the nat helper */
593 + ret = nf_nat_rtsp(skb, ctinfo, hdrsoff, hdrslen, &expinfo, exp);
594 + else if (nf_ct_expect_related(exp) != 0) {
595 + pr_info("nf_conntrack_expect_related failed\n");
598 + nf_ct_expect_put(exp);
608 +help_in(struct sk_buff *skb, size_t pktlen,
609 + struct nf_conn* ct, enum ip_conntrack_info ctinfo)
614 +static int help(struct sk_buff *skb, unsigned int protoff,
615 + struct nf_conn *ct, enum ip_conntrack_info ctinfo)
617 + struct tcphdr _tcph, *th;
618 + unsigned int dataoff, datalen;
622 + /* Until there's been traffic both ways, don't look in packets. */
623 + if (ctinfo != IP_CT_ESTABLISHED &&
624 + ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
625 + pr_debug("conntrackinfo = %u\n", ctinfo);
629 + /* Not whole TCP header? */
630 + th = skb_header_pointer(skb,protoff, sizeof(_tcph), &_tcph);
636 + dataoff = protoff + th->doff*4;
637 + datalen = skb->len - dataoff;
638 + if (dataoff >= skb->len)
641 + spin_lock_bh(&rtsp_buffer_lock);
642 + rb_ptr = skb_header_pointer(skb, dataoff,
643 + skb->len - dataoff, rtsp_buffer);
644 + BUG_ON(rb_ptr == NULL);
647 + /* Checksum invalid? Ignore. */
648 + /* FIXME: Source route IP option packets --RR */
649 + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
650 + csum_partial((char*)tcph, tcplen, 0)))
652 + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
653 + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
658 + switch (CTINFO2DIR(ctinfo)) {
659 + case IP_CT_DIR_ORIGINAL:
660 + ret = help_out(skb, rb_ptr, datalen, ct, ctinfo);
662 + case IP_CT_DIR_REPLY:
663 + pr_debug("IP_CT_DIR_REPLY\n");
664 + /* inbound packet: server->client */
669 + spin_unlock_bh(&rtsp_buffer_lock);
674 +static struct nf_conntrack_helper rtsp_helpers[MAX_PORTS];
675 +static char rtsp_names[MAX_PORTS][10];
677 +/* This function is intentionally _NOT_ defined as __exit */
682 + for (i = 0; i < num_ports; i++) {
683 + pr_debug("unregistering port %d\n", ports[i]);
684 + nf_conntrack_helper_unregister(&rtsp_helpers[i]);
686 + kfree(rtsp_buffer);
693 + struct nf_conntrack_helper *hlpr;
696 + printk("nf_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
698 + if (max_outstanding < 1) {
699 + printk("nf_conntrack_rtsp: max_outstanding must be a positive integer\n");
702 + if (setup_timeout < 0) {
703 + printk("nf_conntrack_rtsp: setup_timeout must be a positive integer\n");
707 + rtsp_exp_policy.max_expected = max_outstanding;
708 + rtsp_exp_policy.timeout = setup_timeout;
710 + rtsp_buffer = kmalloc(65536, GFP_KERNEL);
714 + /* If no port given, default to standard rtsp port */
715 + if (ports[0] == 0) {
716 + ports[0] = RTSP_PORT;
719 + for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
720 + hlpr = &rtsp_helpers[i];
721 + memset(hlpr, 0, sizeof(struct nf_conntrack_helper));
722 + hlpr->tuple.src.l3num = AF_INET;
723 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
724 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
725 + //hlpr->mask.src.u.tcp.port = 0xFFFF;
726 + //hlpr->mask.dst.protonum = 0xFF;
727 + hlpr->expect_policy = &rtsp_exp_policy;
728 + hlpr->me = THIS_MODULE;
731 + tmpname = &rtsp_names[i][0];
732 + if (ports[i] == RTSP_PORT) {
733 + sprintf(tmpname, "rtsp");
735 + sprintf(tmpname, "rtsp-%d", i);
737 + hlpr->name = tmpname;
739 + pr_debug("port #%d: %d\n", i, ports[i]);
741 + ret = nf_conntrack_helper_register(hlpr);
744 + printk("nf_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
756 +EXPORT_SYMBOL(nf_nat_rtsp_hook_expectfn);
759 +++ b/extensions/rtsp/nf_conntrack_rtsp.h
762 + * RTSP extension for IP connection tracking.
763 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
764 + * based on ip_conntrack_irc.h
766 + * This program is free software; you can redistribute it and/or
767 + * modify it under the terms of the GNU General Public License
768 + * as published by the Free Software Foundation; either version
769 + * 2 of the License, or (at your option) any later version.
771 +#ifndef _IP_CONNTRACK_RTSP_H
772 +#define _IP_CONNTRACK_RTSP_H
774 +//#define IP_NF_RTSP_DEBUG 1
775 +#define IP_NF_RTSP_VERSION "0.6.21"
778 +/* port block types */
780 + pb_single, /* client_port=x */
781 + pb_range, /* client_port=x-y */
782 + pb_discon /* client_port=x/y (rtspbis) */
785 +/* We record seq number and length of rtsp headers here, all in host order. */
788 + * This structure is per expected connection. It is a member of struct
789 + * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
790 + * there and we are expected to only store the length of the data which
791 + * needs replaced. If a packet contains multiple RTSP messages, we create
792 + * one expected connection per message.
794 + * We use these variables to mark the entire header block. This may seem
795 + * like overkill, but the nature of RTSP requires it. A header may appear
796 + * multiple times in a message. We must treat two Transport headers the
797 + * same as one Transport header with two entries.
799 +struct ip_ct_rtsp_expect
801 + u_int32_t len; /* length of header block */
802 + portblock_t pbtype; /* Type of port block that was requested */
803 + u_int16_t loport; /* Port that was requested, low or first */
804 + u_int16_t hiport; /* Port that was requested, high or second */
806 + uint method; /* RTSP method */
807 + uint cseq; /* CSeq from request */
811 +extern unsigned int (*nf_nat_rtsp_hook)(struct sk_buff *skb,
812 + enum ip_conntrack_info ctinfo,
813 + unsigned int matchoff, unsigned int matchlen,
814 + struct ip_ct_rtsp_expect *prtspexp,
815 + struct nf_conntrack_expect *exp);
817 +extern void (*nf_nat_rtsp_hook_expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
819 +#define RTSP_PORT 554
821 +#endif /* __KERNEL__ */
823 +#endif /* _IP_CONNTRACK_RTSP_H */
825 +++ b/extensions/rtsp/nf_nat_rtsp.c
828 + * RTSP extension for TCP NAT alteration
829 + * (C) 2003 by Tom Marshall <tmarshall at real.com>
830 + * based on ip_nat_irc.c
832 + * This program is free software; you can redistribute it and/or
833 + * modify it under the terms of the GNU General Public License
834 + * as published by the Free Software Foundation; either version
835 + * 2 of the License, or (at your option) any later version.
837 + * Module load syntax:
838 + * insmod nf_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
839 + * stunaddr=<address>
840 + * destaction=[auto|strip|none]
842 + * If no ports are specified, the default will be port 554 only.
844 + * stunaddr specifies the address used to detect that a client is using STUN.
845 + * If this address is seen in the destination parameter, it is assumed that
846 + * the client has already punched a UDP hole in the firewall, so we don't
847 + * mangle the client_port. If none is specified, it is autodetected. It
848 + * only needs to be set if you have multiple levels of NAT. It should be
849 + * set to the external address that the STUN clients detect. Note that in
850 + * this case, it will not be possible for clients to use UDP with servers
851 + * between the NATs.
853 + * If no destaction is specified, auto is used.
854 + * destaction=auto: strip destination parameter if it is not stunaddr.
855 + * destaction=strip: always strip destination parameter (not recommended).
856 + * destaction=none: do not touch destination parameter (not recommended).
859 +#include <linux/module.h>
860 +#include <net/tcp.h>
861 +#include <net/netfilter/nf_nat_helper.h>
862 +#include <net/netfilter/nf_nat_rule.h>
863 +#include "nf_conntrack_rtsp.h"
864 +#include <net/netfilter/nf_conntrack_expect.h>
866 +#include <linux/inet.h>
867 +#include <linux/ctype.h>
868 +#define NF_NEED_STRNCASECMP
869 +#define NF_NEED_STRTOU16
870 +#include "netfilter_helpers.h"
871 +#define NF_NEED_MIME_NEXTLINE
872 +#include "netfilter_mime.h"
874 +#include "../compat_xtables.h"
877 +#define DSTACT_AUTO 0
878 +#define DSTACT_STRIP 1
879 +#define DSTACT_NONE 2
881 +static char* stunaddr = NULL;
882 +static char* destaction = NULL;
884 +static u_int32_t extip = 0;
885 +static int dstact = 0;
887 +MODULE_AUTHOR("Tom Marshall <tmarshall at real.com>");
888 +MODULE_DESCRIPTION("RTSP network address translation module");
889 +MODULE_LICENSE("GPL");
890 +module_param(stunaddr, charp, 0644);
891 +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
892 +module_param(destaction, charp, 0644);
893 +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
895 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
897 +/*** helper functions ***/
900 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
902 + struct iphdr* iph = ip_hdr(skb);
903 + struct tcphdr* tcph = (void *)iph + ip_hdrlen(skb);
905 + *pptcpdata = (char*)tcph + tcph->doff*4;
906 + *ptcpdatalen = ((char*)skb_transport_header(skb) + skb->len) - *pptcpdata;
909 +/*** nat functions ***/
912 + * Mangle the "Transport:" header:
913 + * - Replace all occurences of "client_port=<spec>"
914 + * - Handle destination parameter
917 + * ct, ctinfo = conntrack context
919 + * tranoff = Transport header offset from TCP data
920 + * tranlen = Transport header length (incl. CRLF)
921 + * rport_lo = replacement low port (host endian)
922 + * rport_hi = replacement high port (host endian)
924 + * Returns packet size difference.
926 + * Assumes that a complete transport header is present, ending with CR or LF
929 +rtsp_mangle_tran(enum ip_conntrack_info ctinfo,
930 + struct nf_conntrack_expect* exp,
931 + struct ip_ct_rtsp_expect* prtspexp,
932 + struct sk_buff* skb, uint tranoff, uint tranlen)
937 + char rbuf1[16]; /* Replacement buffer (one port) */
938 + uint rbuf1len; /* Replacement len (one port) */
939 + char rbufa[16]; /* Replacement buffer (all ports) */
940 + uint rbufalen; /* Replacement len (all ports) */
942 + u_int16_t loport, hiport;
944 + uint diff; /* Number of bytes we removed */
946 + struct nf_conn *ct = exp->master;
947 + struct nf_conntrack_tuple *t;
949 + char szextaddr[15+1];
953 + get_skb_tcpdata(skb, &ptcp, &tcplen);
954 + ptran = ptcp+tranoff;
956 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
957 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
958 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
960 + pr_info("sanity check failed\n");
964 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
966 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip;
968 + t->dst.u3.ip = newip;
970 + extaddrlen = extip ? sprintf(szextaddr, "%pI4", &extip)
971 + : sprintf(szextaddr, "%pI4", &newip);
972 + pr_debug("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
974 + rbuf1len = rbufalen = 0;
975 + switch (prtspexp->pbtype)
978 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
980 + t->dst.u.udp.port = htons(loport);
981 + if (nf_ct_expect_related(exp) == 0)
983 + pr_debug("using port %hu\n", loport);
989 + rbuf1len = sprintf(rbuf1, "%hu", loport);
990 + rbufalen = sprintf(rbufa, "%hu", loport);
994 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
996 + t->dst.u.udp.port = htons(loport);
997 + if (nf_ct_expect_related(exp) == 0)
999 + hiport = loport + 1; //~exp->mask.dst.u.udp.port;
1000 + pr_debug("using ports %hu-%hu\n", loport, hiport);
1006 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1007 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
1011 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
1013 + t->dst.u.udp.port = htons(loport);
1014 + if (nf_ct_expect_related(exp) == 0)
1016 + pr_debug("using port %hu (1 of 2)\n", loport);
1020 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
1022 + t->dst.u.udp.port = htons(hiport);
1023 + if (nf_ct_expect_related(exp) == 0)
1025 + pr_debug("using port %hu (2 of 2)\n", hiport);
1029 + if (loport != 0 && hiport != 0)
1031 + rbuf1len = sprintf(rbuf1, "%hu", loport);
1032 + if (hiport == loport+1)
1034 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
1038 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
1044 + if (rbuf1len == 0)
1046 + return 0; /* cannot get replacement port(s) */
1049 + /* Transport: tran;field;field=val,tran;field;field=val,... */
1050 + while (off < tranlen)
1053 + const char* pparamend;
1054 + uint nextparamoff;
1056 + pparamend = memchr(ptran+off, ',', tranlen-off);
1057 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
1058 + nextparamoff = pparamend-ptcp;
1061 + * We pass over each param twice. On the first pass, we look for a
1062 + * destination= field. It is handled by the security policy. If it
1063 + * is present, allowed, and equal to our external address, we assume
1064 + * that STUN is being used and we leave the client_port= field alone.
1068 + while (off < nextparamoff)
1070 + const char* pfieldend;
1071 + uint nextfieldoff;
1073 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1074 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1076 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
1078 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
1082 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
1084 + diff = nextfieldoff-off;
1085 + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
1086 + off, diff, NULL, 0))
1088 + /* mangle failed, all we can do is bail */
1089 + nf_ct_unexpect_related(exp);
1092 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1093 + ptran = ptcp+tranoff;
1095 + nextparamoff -= diff;
1096 + nextfieldoff -= diff;
1100 + off = nextfieldoff;
1107 + while (off < nextparamoff)
1109 + const char* pfieldend;
1110 + uint nextfieldoff;
1112 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
1113 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
1115 + if (strncmp(ptran+off, "client_port=", 12) == 0)
1121 + char* rbuf = rbuf1;
1122 + uint rbuflen = rbuf1len;
1125 + origoff = (ptran-ptcp)+off;
1127 + numlen = nf_strtou16(ptran+off, &port);
1129 + origlen += numlen;
1130 + if (port != prtspexp->loport)
1132 + pr_debug("multiple ports found, port %hu ignored\n", port);
1136 + if (ptran[off] == '-' || ptran[off] == '/')
1140 + numlen = nf_strtou16(ptran+off, &port);
1142 + origlen += numlen;
1144 + rbuflen = rbufalen;
1148 + * note we cannot just memcpy() if the sizes are the same.
1149 + * the mangle function does skb resizing, checks for a
1150 + * cloned skb, and updates the checksums.
1152 + * parameter 4 below is offset from start of tcp data.
1154 + diff = origlen-rbuflen;
1155 + if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
1156 + origoff, origlen, rbuf, rbuflen))
1158 + /* mangle failed, all we can do is bail */
1159 + nf_ct_unexpect_related(exp);
1162 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1163 + ptran = ptcp+tranoff;
1165 + nextparamoff -= diff;
1166 + nextfieldoff -= diff;
1170 + off = nextfieldoff;
1173 + off = nextparamoff;
1180 +help_out(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
1181 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
1182 + struct nf_conntrack_expect* exp)
1192 + //struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
1193 + //struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
1195 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1196 + hdrsoff = matchoff;//exp->seq - ntohl(tcph->seq);
1197 + hdrslen = matchlen;
1199 + pr_debug("NAT rtsp help_out\n");
1201 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
1207 + if (off > hdrsoff+hdrslen)
1209 + pr_info("!! overrun !!");
1212 + pr_debug("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1214 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
1216 + uint oldtcplen = tcplen;
1217 + pr_debug("hdr: Transport\n");
1218 + if (!rtsp_mangle_tran(ctinfo, exp, prtspexp, skb, lineoff, linelen))
1220 + pr_debug("hdr: Transport mangle failed");
1223 + get_skb_tcpdata(skb, &ptcp, &tcplen);
1224 + hdrslen -= (oldtcplen-tcplen);
1225 + off -= (oldtcplen-tcplen);
1226 + lineoff -= (oldtcplen-tcplen);
1227 + linelen -= (oldtcplen-tcplen);
1228 + pr_debug("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1235 +static unsigned int
1236 +help(struct sk_buff *skb, enum ip_conntrack_info ctinfo,
1237 + unsigned int matchoff, unsigned int matchlen, struct ip_ct_rtsp_expect* prtspexp,
1238 + struct nf_conntrack_expect* exp)
1240 + int dir = CTINFO2DIR(ctinfo);
1241 + int rc = NF_ACCEPT;
1245 + case IP_CT_DIR_ORIGINAL:
1246 + rc = help_out(skb, ctinfo, matchoff, matchlen, prtspexp, exp);
1248 + case IP_CT_DIR_REPLY:
1249 + pr_debug("unmangle ! %u\n", ctinfo);
1250 + /* XXX: unmangle */
1254 + //UNLOCK_BH(&ip_rtsp_lock);
1259 +static void expected(struct nf_conn* ct, struct nf_conntrack_expect *exp)
1261 + struct nf_nat_ipv4_multi_range_compat mr;
1262 + u_int32_t newdstip, newsrcip, newip;
1264 + struct nf_conn *master = ct->master;
1266 + newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
1267 + newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
1268 + //FIXME (how to port that ?)
1269 + //code from 2.4 : newip = (HOOK2MANIP(hooknum) == NF_NAT_MANIP_SRC) ? newsrcip : newdstip;
1272 + pr_debug("newsrcip=%pI4, newdstip=%pI4, newip=%pI4\n",
1273 + &newsrcip, &newdstip, &newip);
1276 + // We don't want to manip the per-protocol, just the IPs.
1277 + mr.range[0].flags = NF_NAT_RANGE_MAP_IPS;
1278 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
1280 + nf_nat_setup_info(ct, &mr.range[0], NF_NAT_MANIP_DST);
1284 +static void __exit fini(void)
1286 + nf_nat_rtsp_hook = NULL;
1287 + nf_nat_rtsp_hook_expectfn = NULL;
1288 + synchronize_net();
1291 +static int __init init(void)
1293 + printk("nf_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1295 + BUG_ON(nf_nat_rtsp_hook);
1296 + nf_nat_rtsp_hook = help;
1297 + nf_nat_rtsp_hook_expectfn = &expected;
1299 + if (stunaddr != NULL)
1300 + extip = in_aton(stunaddr);
1302 + if (destaction != NULL) {
1303 + if (strcmp(destaction, "auto") == 0)
1304 + dstact = DSTACT_AUTO;
1306 + if (strcmp(destaction, "strip") == 0)
1307 + dstact = DSTACT_STRIP;
1309 + if (strcmp(destaction, "none") == 0)
1310 + dstact = DSTACT_NONE;
1318 --- a/extensions/Kbuild
1319 +++ b/extensions/Kbuild
1320 @@ -28,6 +28,7 @@ obj-${build_lscan} += xt_lscan.o
1321 obj-${build_pknock} += pknock/
1322 obj-${build_psd} += xt_psd.o
1323 obj-${build_quota2} += xt_quota2.o
1324 +obj-${build_rtsp} += rtsp/
1326 -include ${M}/*.Kbuild
1327 -include ${M}/Kbuild.*
1330 @@ -22,3 +22,4 @@ build_lscan=m