1 --- a/net/ipv4/netfilter/Config.in
2 +++ b/net/ipv4/netfilter/Config.in
3 @@ -14,6 +14,7 @@ if [ "$CONFIG_IP_NF_CONNTRACK" != "n" ];
4 dep_tristate ' Connection tracking flow accounting' CONFIG_IP_NF_CT_ACCT $CONFIG_IP_NF_CONNTRACK
5 dep_tristate ' Connection byte counter support' CONFIG_IP_NF_MATCH_CONNBYTES $CONFIG_IP_NF_CT_ACCT $CONFIG_IP_NF_CONNTRACK $CONFIG_IP_NF_IPTABLES
6 dep_tristate ' H.323 (netmeeting) support' CONFIG_IP_NF_H323 $CONFIG_IP_NF_CONNTRACK
7 + dep_tristate ' RTSP protocol support' CONFIG_IP_NF_RTSP $CONFIG_IP_NF_CONNTRACK
10 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
11 @@ -102,6 +103,13 @@ if [ "$CONFIG_IP_NF_IPTABLES" != "n" ];
12 define_tristate CONFIG_IP_NF_NAT_H323 $CONFIG_IP_NF_NAT
15 + if [ "$CONFIG_IP_NF_RTSP" = "m" ]; then
16 + define_tristate CONFIG_IP_NF_NAT_RTSP m
18 + if [ "$CONFIG_IP_NF_RTSP" = "y" ]; then
19 + define_tristate CONFIG_IP_NF_NAT_RTSP $CONFIG_IP_NF_NAT
22 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
23 dep_tristate ' Basic SNMP-ALG support (EXPERIMENTAL)' CONFIG_IP_NF_NAT_SNMP_BASIC $CONFIG_IP_NF_NAT
25 --- a/net/ipv4/netfilter/Makefile
26 +++ b/net/ipv4/netfilter/Makefile
27 @@ -51,6 +51,11 @@ obj-$(CONFIG_IP_NF_H323) += ip_conntrack
28 ifdef CONFIG_IP_NF_NAT_H323
29 export-objs += ip_conntrack_h323.o
31 +obj-$(CONFIG_IP_NF_RTSP) += ip_conntrack_rtsp.o
32 +ifdef CONFIG_IP_NF_NAT_RTSP
33 + export-objs += ip_conntrack_rtsp.o
38 obj-$(CONFIG_IP_NF_NAT_AMANDA) += ip_nat_amanda.o
39 @@ -58,6 +63,7 @@ obj-$(CONFIG_IP_NF_NAT_TFTP) += ip_nat_t
40 obj-$(CONFIG_IP_NF_NAT_FTP) += ip_nat_ftp.o
41 obj-$(CONFIG_IP_NF_NAT_IRC) += ip_nat_irc.o
42 obj-$(CONFIG_IP_NF_NAT_H323) += ip_nat_h323.o
43 +obj-$(CONFIG_IP_NF_NAT_RTSP) += ip_nat_rtsp.o
46 obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o
48 +++ b/net/ipv4/netfilter/ip_conntrack_rtsp.c
51 + * RTSP extension for IP connection tracking
52 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
53 + * based on ip_conntrack_irc.c
55 + * This program is free software; you can redistribute it and/or
56 + * modify it under the terms of the GNU General Public License
57 + * as published by the Free Software Foundation; either version
58 + * 2 of the License, or (at your option) any later version.
60 + * Module load syntax:
61 + * insmod ip_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
62 + * max_outstanding=n setup_timeout=secs
64 + * If no ports are specified, the default will be port 554.
66 + * With max_outstanding you can define the maximum number of not yet
67 + * answered SETUP requests per RTSP session (default 8).
68 + * With setup_timeout you can specify how long the system waits for
69 + * an expected data channel (default 300 seconds).
72 +#include <linux/config.h>
73 +#include <linux/module.h>
74 +#include <linux/netfilter.h>
75 +#include <linux/ip.h>
76 +#include <net/checksum.h>
79 +#include <linux/netfilter_ipv4/lockhelp.h>
80 +#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
81 +#include <linux/netfilter_ipv4/ip_conntrack_rtsp.h>
83 +#include <linux/ctype.h>
84 +#define NF_NEED_STRNCASECMP
85 +#define NF_NEED_STRTOU16
86 +#define NF_NEED_STRTOU32
87 +#define NF_NEED_NEXTLINE
88 +#include <linux/netfilter_helpers.h>
89 +#define NF_NEED_MIME_NEXTLINE
90 +#include <linux/netfilter_mime.h>
92 +#define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
94 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
95 +#ifdef IP_NF_RTSP_DEBUG
96 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
98 +#define DEBUGP(fmt, args...)
102 +static int ports[MAX_PORTS];
103 +static int num_ports = 0;
104 +static int max_outstanding = 8;
105 +static unsigned int setup_timeout = 300;
107 +MODULE_AUTHOR("Tom Marshall <tmarshall@real.com>");
108 +MODULE_DESCRIPTION("RTSP connection tracking module");
109 +MODULE_LICENSE("GPL");
111 +MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
112 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
113 +MODULE_PARM(max_outstanding, "i");
114 +MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
115 +MODULE_PARM(setup_timeout, "i");
116 +MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
119 +DECLARE_LOCK(ip_rtsp_lock);
120 +struct module* ip_conntrack_rtsp = THIS_MODULE;
123 + * Max mappings we will allow for one RTSP connection (for RTP, the number
124 + * of allocated ports is twice this value). Note that SMIL burns a lot of
125 + * ports so keep this reasonably high. If this is too low, you will see a
126 + * lot of "no free client map entries" messages.
128 +#define MAX_PORT_MAPS 16
130 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
132 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
135 + * Parse an RTSP packet.
137 + * Returns zero if parsing failed.
140 + * IN ptcp tcp data pointer
141 + * IN tcplen tcp data len
142 + * IN/OUT ptcpoff points to current tcp offset
143 + * OUT phdrsoff set to offset of rtsp headers
144 + * OUT phdrslen set to length of rtsp headers
145 + * OUT pcseqoff set to offset of CSeq header
146 + * OUT pcseqlen set to length of CSeq header
149 +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
150 + uint* phdrsoff, uint* phdrslen,
151 + uint* pcseqoff, uint* pcseqlen)
153 + uint entitylen = 0;
157 + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
162 + *phdrsoff = *ptcpoff;
163 + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
169 + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
173 + if (lineoff+linelen > tcplen)
175 + INFOP("!! overrun !!\n");
179 + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0)
181 + *pcseqoff = lineoff;
182 + *pcseqlen = linelen;
184 + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0)
186 + uint off = lineoff+15;
187 + SKIP_WSPACE(ptcp+lineoff, linelen, off);
188 + nf_strtou32(ptcp+off, &entitylen);
191 + *phdrslen = (*ptcpoff) - (*phdrsoff);
197 + * Find lo/hi client ports (if any) in transport header
199 + * ptcp, tcplen = packet
200 + * tranoff, tranlen = buffer to search
203 + * pport_lo, pport_hi = lo/hi ports (host endian)
205 + * Returns nonzero if any client ports found
207 + * Note: it is valid (and expected) for the client to request multiple
208 + * transports, so we need to parse the entire line.
211 +rtsp_parse_transport(char* ptran, uint tranlen,
212 + struct ip_ct_rtsp_expect* prtspexp)
217 + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
218 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
220 + INFOP("sanity check failed\n");
223 + DEBUGP("tran='%.*s'\n", (int)tranlen, ptran);
225 + SKIP_WSPACE(ptran, tranlen, off);
227 + /* Transport: tran;field;field=val,tran;field;field=val,... */
228 + while (off < tranlen)
230 + const char* pparamend;
233 + pparamend = memchr(ptran+off, ',', tranlen-off);
234 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
235 + nextparamoff = pparamend-ptran;
237 + while (off < nextparamoff)
239 + const char* pfieldend;
242 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
243 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
245 + if (strncmp(ptran+off, "client_port=", 12) == 0)
251 + numlen = nf_strtou16(ptran+off, &port);
253 + if (prtspexp->loport != 0 && prtspexp->loport != port)
255 + DEBUGP("multiple ports found, port %hu ignored\n", port);
259 + prtspexp->loport = prtspexp->hiport = port;
260 + if (ptran[off] == '-')
263 + numlen = nf_strtou16(ptran+off, &port);
265 + prtspexp->pbtype = pb_range;
266 + prtspexp->hiport = port;
268 + // If we have a range, assume rtp:
269 + // loport must be even, hiport must be loport+1
270 + if ((prtspexp->loport & 0x0001) != 0 ||
271 + prtspexp->hiport != prtspexp->loport+1)
273 + DEBUGP("incorrect range: %hu-%hu, correcting\n",
274 + prtspexp->loport, prtspexp->hiport);
275 + prtspexp->loport &= 0xfffe;
276 + prtspexp->hiport = prtspexp->loport+1;
279 + else if (ptran[off] == '/')
282 + numlen = nf_strtou16(ptran+off, &port);
284 + prtspexp->pbtype = pb_discon;
285 + prtspexp->hiport = port;
292 + * Note we don't look for the destination parameter here.
293 + * If we are using NAT, the NAT module will handle it. If not,
294 + * and the client is sending packets elsewhere, the expectation
295 + * will quietly time out.
298 + off = nextfieldoff;
301 + off = nextparamoff;
307 +/*** conntrack functions ***/
309 +/* outbound packet: client->server */
311 +help_out(const struct iphdr* iph, size_t pktlen,
312 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
314 + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
315 + struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
316 + uint tcplen = pktlen - iph->ihl * 4;
317 + char* pdata = (char*)tcph + tcph->doff * 4;
318 + uint datalen = tcplen - tcph->doff * 4;
321 + struct ip_conntrack_expect exp;
323 + while (dataoff < datalen)
325 + uint cmdoff = dataoff;
335 + if (!rtsp_parse_message(pdata, datalen, &dataoff,
336 + &hdrsoff, &hdrslen,
337 + &cseqoff, &cseqlen))
339 + break; /* not a valid message */
342 + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
344 + continue; /* not a SETUP message */
346 + DEBUGP("found a setup message\n");
348 + memset(&exp, 0, sizeof(exp));
351 + while (nf_mime_nextline(pdata+hdrsoff, hdrslen, &off,
352 + &lineoff, &linelen))
358 + if (off > hdrsoff+hdrslen)
360 + INFOP("!! overrun !!");
364 + if (nf_strncasecmp(pdata+hdrsoff+lineoff, "Transport:", 10) == 0)
366 + rtsp_parse_transport(pdata+hdrsoff+lineoff, linelen,
367 + &exp.help.exp_rtsp_info);
371 + if (exp.help.exp_rtsp_info.loport == 0)
373 + DEBUGP("no udp transports found\n");
374 + continue; /* no udp transports found */
377 + DEBUGP("udp transport found, ports=(%d,%hu,%hu)\n",
378 + (int)exp.help.exp_rtsp_info.pbtype,
379 + exp.help.exp_rtsp_info.loport,
380 + exp.help.exp_rtsp_info.hiport);
382 + LOCK_BH(&ip_rtsp_lock);
383 + exp.seq = ntohl(tcph->seq) + hdrsoff; /* mark all the headers */
384 + exp.help.exp_rtsp_info.len = hdrslen;
386 + exp.tuple.src.ip = ct->tuplehash[!dir].tuple.src.ip;
387 + exp.mask.src.ip = 0xffffffff;
388 + exp.tuple.dst.ip = ct->tuplehash[dir].tuple.src.ip;
389 + exp.mask.dst.ip = 0xffffffff;
390 + exp.tuple.dst.u.udp.port = exp.help.exp_rtsp_info.loport;
391 + exp.mask.dst.u.udp.port = (exp.help.exp_rtsp_info.pbtype == pb_range) ? 0xfffe : 0xffff;
392 + exp.tuple.dst.protonum = IPPROTO_UDP;
393 + exp.mask.dst.protonum = 0xffff;
395 + DEBUGP("expect_related %u.%u.%u.%u:%u-%u.%u.%u.%u:%u\n",
396 + NIPQUAD(exp.tuple.src.ip),
397 + ntohs(exp.tuple.src.u.tcp.port),
398 + NIPQUAD(exp.tuple.dst.ip),
399 + ntohs(exp.tuple.dst.u.tcp.port));
401 + /* pass the request off to the nat helper */
402 + rc = ip_conntrack_expect_related(ct, &exp);
403 + UNLOCK_BH(&ip_rtsp_lock);
406 + DEBUGP("ip_conntrack_expect_related succeeded\n");
410 + INFOP("ip_conntrack_expect_related failed (%d)\n", rc);
417 +/* inbound packet: server->client */
419 +help_in(const struct iphdr* iph, size_t pktlen,
420 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
426 +help(const struct iphdr* iph, size_t pktlen,
427 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
429 + /* tcplen not negative guarenteed by ip_conntrack_tcp.c */
430 + struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
431 + u_int32_t tcplen = pktlen - iph->ihl * 4;
433 + /* Until there's been traffic both ways, don't look in packets. */
434 + if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
436 + DEBUGP("conntrackinfo = %u\n", ctinfo);
440 + /* Not whole TCP header? */
441 + if (tcplen < sizeof(struct tcphdr) || tcplen < tcph->doff * 4)
443 + DEBUGP("tcplen = %u\n", (unsigned)tcplen);
447 + /* Checksum invalid? Ignore. */
448 + /* FIXME: Source route IP option packets --RR */
449 + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
450 + csum_partial((char*)tcph, tcplen, 0)))
452 + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
453 + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
457 + switch (CTINFO2DIR(ctinfo))
459 + case IP_CT_DIR_ORIGINAL:
460 + help_out(iph, pktlen, ct, ctinfo);
462 + case IP_CT_DIR_REPLY:
463 + help_in(iph, pktlen, ct, ctinfo);
470 +static struct ip_conntrack_helper rtsp_helpers[MAX_PORTS];
471 +static char rtsp_names[MAX_PORTS][10];
473 +/* This function is intentionally _NOT_ defined as __exit */
478 + for (i = 0; i < num_ports; i++)
480 + DEBUGP("unregistering port %d\n", ports[i]);
481 + ip_conntrack_helper_unregister(&rtsp_helpers[i]);
489 + struct ip_conntrack_helper *hlpr;
492 + printk("ip_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
494 + if (max_outstanding < 1)
496 + printk("ip_conntrack_rtsp: max_outstanding must be a positive integer\n");
499 + if (setup_timeout < 0)
501 + printk("ip_conntrack_rtsp: setup_timeout must be a positive integer\n");
505 + /* If no port given, default to standard rtsp port */
508 + ports[0] = RTSP_PORT;
511 + for (i = 0; (i < MAX_PORTS) && ports[i]; i++)
513 + hlpr = &rtsp_helpers[i];
514 + memset(hlpr, 0, sizeof(struct ip_conntrack_helper));
515 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
516 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
517 + hlpr->mask.src.u.tcp.port = 0xFFFF;
518 + hlpr->mask.dst.protonum = 0xFFFF;
519 + hlpr->max_expected = max_outstanding;
520 + hlpr->timeout = setup_timeout;
521 + hlpr->flags = IP_CT_HELPER_F_REUSE_EXPECT;
522 + hlpr->me = ip_conntrack_rtsp;
525 + tmpname = &rtsp_names[i][0];
526 + if (ports[i] == RTSP_PORT)
528 + sprintf(tmpname, "rtsp");
532 + sprintf(tmpname, "rtsp-%d", i);
534 + hlpr->name = tmpname;
536 + DEBUGP("port #%d: %d\n", i, ports[i]);
538 + ret = ip_conntrack_helper_register(hlpr);
542 + printk("ip_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
551 +#ifdef CONFIG_IP_NF_NAT_NEEDED
552 +EXPORT_SYMBOL(ip_rtsp_lock);
558 +++ b/net/ipv4/netfilter/ip_nat_rtsp.c
561 + * RTSP extension for TCP NAT alteration
562 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
563 + * based on ip_nat_irc.c
565 + * This program is free software; you can redistribute it and/or
566 + * modify it under the terms of the GNU General Public License
567 + * as published by the Free Software Foundation; either version
568 + * 2 of the License, or (at your option) any later version.
570 + * Module load syntax:
571 + * insmod ip_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
572 + * stunaddr=<address>
573 + * destaction=[auto|strip|none]
575 + * If no ports are specified, the default will be port 554 only.
577 + * stunaddr specifies the address used to detect that a client is using STUN.
578 + * If this address is seen in the destination parameter, it is assumed that
579 + * the client has already punched a UDP hole in the firewall, so we don't
580 + * mangle the client_port. If none is specified, it is autodetected. It
581 + * only needs to be set if you have multiple levels of NAT. It should be
582 + * set to the external address that the STUN clients detect. Note that in
583 + * this case, it will not be possible for clients to use UDP with servers
584 + * between the NATs.
586 + * If no destaction is specified, auto is used.
587 + * destaction=auto: strip destination parameter if it is not stunaddr.
588 + * destaction=strip: always strip destination parameter (not recommended).
589 + * destaction=none: do not touch destination parameter (not recommended).
592 +#include <linux/module.h>
593 +#include <linux/netfilter_ipv4.h>
594 +#include <linux/ip.h>
595 +#include <linux/tcp.h>
596 +#include <linux/kernel.h>
597 +#include <net/tcp.h>
598 +#include <linux/netfilter_ipv4/ip_nat.h>
599 +#include <linux/netfilter_ipv4/ip_nat_helper.h>
600 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
601 +#include <linux/netfilter_ipv4/ip_conntrack_rtsp.h>
602 +#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
604 +#include <linux/inet.h>
605 +#include <linux/ctype.h>
606 +#define NF_NEED_STRNCASECMP
607 +#define NF_NEED_STRTOU16
608 +#include <linux/netfilter_helpers.h>
609 +#define NF_NEED_MIME_NEXTLINE
610 +#include <linux/netfilter_mime.h>
612 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
613 +#ifdef IP_NF_RTSP_DEBUG
614 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
616 +#define DEBUGP(fmt, args...)
620 +#define DSTACT_AUTO 0
621 +#define DSTACT_STRIP 1
622 +#define DSTACT_NONE 2
624 +static int ports[MAX_PORTS];
625 +static char* stunaddr = NULL;
626 +static char* destaction = NULL;
628 +static int num_ports = 0;
629 +static u_int32_t extip = 0;
630 +static int dstact = 0;
632 +MODULE_AUTHOR("Tom Marshall <tmarshall@real.com>");
633 +MODULE_DESCRIPTION("RTSP network address translation module");
634 +MODULE_LICENSE("GPL");
636 +MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
637 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
638 +MODULE_PARM(stunaddr, "s");
639 +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
640 +MODULE_PARM(destaction, "s");
641 +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
644 +/* protects rtsp part of conntracks */
645 +DECLARE_LOCK_EXTERN(ip_rtsp_lock);
647 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
649 +/*** helper functions ***/
652 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
654 + struct iphdr* iph = (struct iphdr*)skb->nh.iph;
655 + struct tcphdr* tcph = (struct tcphdr*)((char*)iph + iph->ihl*4);
657 + *pptcpdata = (char*)tcph + tcph->doff*4;
658 + *ptcpdatalen = ((char*)skb->h.raw + skb->len) - *pptcpdata;
661 +/*** nat functions ***/
664 + * Mangle the "Transport:" header:
665 + * - Replace all occurences of "client_port=<spec>"
666 + * - Handle destination parameter
669 + * ct, ctinfo = conntrack context
671 + * tranoff = Transport header offset from TCP data
672 + * tranlen = Transport header length (incl. CRLF)
673 + * rport_lo = replacement low port (host endian)
674 + * rport_hi = replacement high port (host endian)
676 + * Returns packet size difference.
678 + * Assumes that a complete transport header is present, ending with CR or LF
681 +rtsp_mangle_tran(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
682 + struct ip_conntrack_expect* exp,
683 + struct sk_buff** pskb, uint tranoff, uint tranlen)
688 + char rbuf1[16]; /* Replacement buffer (one port) */
689 + uint rbuf1len; /* Replacement len (one port) */
690 + char rbufa[16]; /* Replacement buffer (all ports) */
691 + uint rbufalen; /* Replacement len (all ports) */
693 + u_int16_t loport, hiport;
695 + uint diff; /* Number of bytes we removed */
697 + struct ip_ct_rtsp_expect* prtspexp = &exp->help.exp_rtsp_info;
698 + struct ip_conntrack_tuple t;
700 + char szextaddr[15+1];
704 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
705 + ptran = ptcp+tranoff;
707 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
708 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
709 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
711 + INFOP("sanity check failed\n");
715 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
717 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
721 + extaddrlen = extip ? sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(extip))
722 + : sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(newip));
723 + DEBUGP("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
725 + rbuf1len = rbufalen = 0;
726 + switch (prtspexp->pbtype)
729 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
731 + t.dst.u.udp.port = htons(loport);
732 + if (ip_conntrack_change_expect(exp, &t) == 0)
734 + DEBUGP("using port %hu\n", loport);
740 + rbuf1len = sprintf(rbuf1, "%hu", loport);
741 + rbufalen = sprintf(rbufa, "%hu", loport);
745 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
747 + t.dst.u.udp.port = htons(loport);
748 + if (ip_conntrack_change_expect(exp, &t) == 0)
750 + hiport = loport + ~exp->mask.dst.u.udp.port;
751 + DEBUGP("using ports %hu-%hu\n", loport, hiport);
757 + rbuf1len = sprintf(rbuf1, "%hu", loport);
758 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
762 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
764 + t.dst.u.udp.port = htons(loport);
765 + if (ip_conntrack_change_expect(exp, &t) == 0)
767 + DEBUGP("using port %hu (1 of 2)\n", loport);
771 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
773 + t.dst.u.udp.port = htons(hiport);
774 + if (ip_conntrack_change_expect(exp, &t) == 0)
776 + DEBUGP("using port %hu (2 of 2)\n", hiport);
780 + if (loport != 0 && hiport != 0)
782 + rbuf1len = sprintf(rbuf1, "%hu", loport);
783 + if (hiport == loport+1)
785 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
789 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
797 + return 0; /* cannot get replacement port(s) */
800 + /* Transport: tran;field;field=val,tran;field;field=val,... */
801 + while (off < tranlen)
804 + const char* pparamend;
807 + pparamend = memchr(ptran+off, ',', tranlen-off);
808 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
809 + nextparamoff = pparamend-ptcp;
812 + * We pass over each param twice. On the first pass, we look for a
813 + * destination= field. It is handled by the security policy. If it
814 + * is present, allowed, and equal to our external address, we assume
815 + * that STUN is being used and we leave the client_port= field alone.
819 + while (off < nextparamoff)
821 + const char* pfieldend;
824 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
825 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
827 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
829 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
833 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
835 + diff = nextfieldoff-off;
836 + if (!ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
837 + off, diff, NULL, 0))
839 + /* mangle failed, all we can do is bail */
842 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
843 + ptran = ptcp+tranoff;
845 + nextparamoff -= diff;
846 + nextfieldoff -= diff;
850 + off = nextfieldoff;
857 + while (off < nextparamoff)
859 + const char* pfieldend;
862 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
863 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
865 + if (strncmp(ptran+off, "client_port=", 12) == 0)
871 + char* rbuf = rbuf1;
872 + uint rbuflen = rbuf1len;
875 + origoff = (ptran-ptcp)+off;
877 + numlen = nf_strtou16(ptran+off, &port);
880 + if (port != prtspexp->loport)
882 + DEBUGP("multiple ports found, port %hu ignored\n", port);
886 + if (ptran[off] == '-' || ptran[off] == '/')
890 + numlen = nf_strtou16(ptran+off, &port);
894 + rbuflen = rbufalen;
898 + * note we cannot just memcpy() if the sizes are the same.
899 + * the mangle function does skb resizing, checks for a
900 + * cloned skb, and updates the checksums.
902 + * parameter 4 below is offset from start of tcp data.
904 + diff = origlen-rbuflen;
905 + if (!ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
906 + origoff, origlen, rbuf, rbuflen))
908 + /* mangle failed, all we can do is bail */
911 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
912 + ptran = ptcp+tranoff;
914 + nextparamoff -= diff;
915 + nextfieldoff -= diff;
919 + off = nextfieldoff;
922 + off = nextparamoff;
929 +expected(struct sk_buff **pskb, uint hooknum, struct ip_conntrack* ct, struct ip_nat_info* info)
931 + struct ip_nat_multi_range mr;
932 + u_int32_t newdstip, newsrcip, newip;
934 + struct ip_conntrack *master = master_ct(ct);
936 + IP_NF_ASSERT(info);
937 + IP_NF_ASSERT(master);
939 + IP_NF_ASSERT(!(info->initialized & (1 << HOOK2MANIP(hooknum))));
941 + newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
942 + newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
943 + newip = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) ? newsrcip : newdstip;
945 + DEBUGP("newsrcip=%u.%u.%u.%u, newdstip=%u.%u.%u.%u, newip=%u.%u.%u.%u\n",
946 + NIPQUAD(newsrcip), NIPQUAD(newdstip), NIPQUAD(newip));
949 + /* We don't want to manip the per-protocol, just the IPs. */
950 + mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
951 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
953 + return ip_nat_setup_info(ct, &mr, hooknum);
957 +help_out(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
958 + struct ip_conntrack_expect* exp, struct sk_buff** pskb)
968 + struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
969 + struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
971 + struct ip_ct_rtsp_expect* prtspexp = &exp->help.exp_rtsp_info;
973 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
975 + hdrsoff = exp->seq - ntohl(tcph->seq);
976 + hdrslen = prtspexp->len;
979 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
985 + if (off > hdrsoff+hdrslen)
987 + INFOP("!! overrun !!");
990 + DEBUGP("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
992 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
994 + uint oldtcplen = tcplen;
995 + if (!rtsp_mangle_tran(ct, ctinfo, exp, pskb, lineoff, linelen))
999 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1000 + hdrslen -= (oldtcplen-tcplen);
1001 + off -= (oldtcplen-tcplen);
1002 + lineoff -= (oldtcplen-tcplen);
1003 + linelen -= (oldtcplen-tcplen);
1004 + DEBUGP("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1012 +help_in(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
1013 + struct ip_conntrack_expect* exp, struct sk_buff** pskb)
1015 + /* XXX: unmangle */
1020 +help(struct ip_conntrack* ct,
1021 + struct ip_conntrack_expect* exp,
1022 + struct ip_nat_info* info,
1023 + enum ip_conntrack_info ctinfo,
1024 + unsigned int hooknum,
1025 + struct sk_buff** pskb)
1027 + struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
1028 + struct tcphdr* tcph = (struct tcphdr*)((char*)iph + iph->ihl * 4);
1031 + struct ip_ct_rtsp_expect* ct_rtsp_info;
1032 + int rc = NF_ACCEPT;
1034 + if (ct == NULL || exp == NULL || info == NULL || pskb == NULL)
1036 + DEBUGP("!! null ptr (%p,%p,%p,%p) !!\n", ct, exp, info, pskb);
1040 + ct_rtsp_info = &exp->help.exp_rtsp_info;
1043 + * Only mangle things once: original direction in POST_ROUTING
1044 + * and reply direction on PRE_ROUTING.
1046 + dir = CTINFO2DIR(ctinfo);
1047 + if (!((hooknum == NF_IP_POST_ROUTING && dir == IP_CT_DIR_ORIGINAL)
1048 + || (hooknum == NF_IP_PRE_ROUTING && dir == IP_CT_DIR_REPLY)))
1050 + DEBUGP("Not touching dir %s at hook %s\n",
1051 + dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY",
1052 + hooknum == NF_IP_POST_ROUTING ? "POSTROUTING"
1053 + : hooknum == NF_IP_PRE_ROUTING ? "PREROUTING"
1054 + : hooknum == NF_IP_LOCAL_OUT ? "OUTPUT" : "???");
1057 + DEBUGP("got beyond not touching\n");
1059 + datalen = (*pskb)->len - iph->ihl * 4 - tcph->doff * 4;
1061 + LOCK_BH(&ip_rtsp_lock);
1062 + /* Ensure the packet contains all of the marked data */
1063 + if (!between(exp->seq + ct_rtsp_info->len,
1064 + ntohl(tcph->seq), ntohl(tcph->seq) + datalen))
1066 + /* Partial retransmission? Probably a hacker. */
1067 + if (net_ratelimit())
1069 + INFOP("partial packet %u/%u in %u/%u\n",
1070 + exp->seq, ct_rtsp_info->len, ntohl(tcph->seq), ntohl(tcph->seq) + datalen);
1072 + UNLOCK_BH(&ip_rtsp_lock);
1078 + case IP_CT_DIR_ORIGINAL:
1079 + rc = help_out(ct, ctinfo, exp, pskb);
1081 + case IP_CT_DIR_REPLY:
1082 + rc = help_in(ct, ctinfo, exp, pskb);
1085 + UNLOCK_BH(&ip_rtsp_lock);
1090 +static struct ip_nat_helper ip_nat_rtsp_helpers[MAX_PORTS];
1091 +static char rtsp_names[MAX_PORTS][10];
1093 +/* This function is intentionally _NOT_ defined as __exit */
1099 + for (i = 0; i < num_ports; i++)
1101 + DEBUGP("unregistering helper for port %d\n", ports[i]);
1102 + ip_nat_helper_unregister(&ip_nat_rtsp_helpers[i]);
1111 + struct ip_nat_helper* hlpr;
1114 + printk("ip_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1116 + if (ports[0] == 0)
1118 + ports[0] = RTSP_PORT;
1121 + for (i = 0; (i < MAX_PORTS) && ports[i] != 0; i++)
1123 + hlpr = &ip_nat_rtsp_helpers[i];
1124 + memset(hlpr, 0, sizeof(struct ip_nat_helper));
1126 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
1127 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
1128 + hlpr->mask.src.u.tcp.port = 0xFFFF;
1129 + hlpr->mask.dst.protonum = 0xFFFF;
1130 + hlpr->help = help;
1132 + hlpr->me = THIS_MODULE;
1133 + hlpr->expect = expected;
1135 + tmpname = &rtsp_names[i][0];
1136 + if (ports[i] == RTSP_PORT)
1138 + sprintf(tmpname, "rtsp");
1142 + sprintf(tmpname, "rtsp-%d", i);
1144 + hlpr->name = tmpname;
1146 + DEBUGP("registering helper for port %d: name %s\n", ports[i], hlpr->name);
1147 + ret = ip_nat_helper_register(hlpr);
1151 + printk("ip_nat_rtsp: error registering helper for port %d\n", ports[i]);
1157 + if (stunaddr != NULL)
1159 + extip = in_aton(stunaddr);
1161 + if (destaction != NULL)
1163 + if (strcmp(destaction, "auto") == 0)
1165 + dstact = DSTACT_AUTO;
1167 + if (strcmp(destaction, "strip") == 0)
1169 + dstact = DSTACT_STRIP;
1171 + if (strcmp(destaction, "none") == 0)
1173 + dstact = DSTACT_NONE;
1181 --- a/arch/mips/kernel/mips_ksyms.c
1182 +++ b/arch/mips/kernel/mips_ksyms.c
1183 @@ -52,6 +52,7 @@ EXPORT_SYMBOL(EISA_bus);
1187 +EXPORT_SYMBOL_NOVERS(memchr);
1188 EXPORT_SYMBOL_NOVERS(memcmp);
1189 EXPORT_SYMBOL_NOVERS(memset);
1190 EXPORT_SYMBOL_NOVERS(memcpy);
1192 +++ b/include/linux/netfilter_helpers.h
1195 + * Helpers for netfiler modules. This file provides implementations for basic
1196 + * functions such as strncasecmp(), etc.
1198 + * gcc will warn for defined but unused functions, so we only include the
1199 + * functions requested. The following macros are used:
1200 + * NF_NEED_STRNCASECMP nf_strncasecmp()
1201 + * NF_NEED_STRTOU16 nf_strtou16()
1202 + * NF_NEED_STRTOU32 nf_strtou32()
1204 +#ifndef _NETFILTER_HELPERS_H
1205 +#define _NETFILTER_HELPERS_H
1207 +/* Only include these functions for kernel code. */
1210 +#include <linux/ctype.h>
1211 +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
1214 + * The standard strncasecmp()
1216 +#ifdef NF_NEED_STRNCASECMP
1218 +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
1220 + if (s1 == NULL || s2 == NULL)
1222 + if (s1 == NULL && s2 == NULL)
1226 + return (s1 == NULL) ? -1 : 1;
1228 + while (len > 0 && tolower(*s1) == tolower(*s2))
1234 + return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
1236 +#endif /* NF_NEED_STRNCASECMP */
1239 + * Parse a string containing a 16-bit unsigned integer.
1240 + * Returns the number of chars used, or zero if no number is found.
1242 +#ifdef NF_NEED_STRTOU16
1244 +nf_strtou16(const char* pbuf, u_int16_t* pval)
1249 + while (isdigit(pbuf[n]))
1251 + *pval = (*pval * 10) + (pbuf[n] - '0');
1257 +#endif /* NF_NEED_STRTOU16 */
1260 + * Parse a string containing a 32-bit unsigned integer.
1261 + * Returns the number of chars used, or zero if no number is found.
1263 +#ifdef NF_NEED_STRTOU32
1265 +nf_strtou32(const char* pbuf, u_int32_t* pval)
1270 + while (pbuf[n] >= '0' && pbuf[n] <= '9')
1272 + *pval = (*pval * 10) + (pbuf[n] - '0');
1278 +#endif /* NF_NEED_STRTOU32 */
1281 + * Given a buffer and length, advance to the next line and mark the current
1284 +#ifdef NF_NEED_NEXTLINE
1286 +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
1296 + while (p[off] != '\n')
1307 + /* if we saw a crlf, physlen needs adjusted */
1308 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
1313 + /* advance past the newline */
1316 + *plineoff = *poff;
1317 + *plinelen = physlen;
1322 +#endif /* NF_NEED_NEXTLINE */
1324 +#endif /* __KERNEL__ */
1326 +#endif /* _NETFILTER_HELPERS_H */
1328 +++ b/include/linux/netfilter_ipv4/ip_conntrack_rtsp.h
1331 + * RTSP extension for IP connection tracking.
1332 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
1333 + * based on ip_conntrack_irc.h
1335 + * This program is free software; you can redistribute it and/or
1336 + * modify it under the terms of the GNU General Public License
1337 + * as published by the Free Software Foundation; either version
1338 + * 2 of the License, or (at your option) any later version.
1340 +#ifndef _IP_CONNTRACK_RTSP_H
1341 +#define _IP_CONNTRACK_RTSP_H
1343 +/* #define IP_NF_RTSP_DEBUG */
1344 +#define IP_NF_RTSP_VERSION "0.01"
1346 +/* port block types */
1348 + pb_single, /* client_port=x */
1349 + pb_range, /* client_port=x-y */
1350 + pb_discon /* client_port=x/y (rtspbis) */
1353 +/* We record seq number and length of rtsp headers here, all in host order. */
1356 + * This structure is per expected connection. It is a member of struct
1357 + * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
1358 + * there and we are expected to only store the length of the data which
1359 + * needs replaced. If a packet contains multiple RTSP messages, we create
1360 + * one expected connection per message.
1362 + * We use these variables to mark the entire header block. This may seem
1363 + * like overkill, but the nature of RTSP requires it. A header may appear
1364 + * multiple times in a message. We must treat two Transport headers the
1365 + * same as one Transport header with two entries.
1367 +struct ip_ct_rtsp_expect
1369 + u_int32_t len; /* length of header block */
1370 + portblock_t pbtype; /* Type of port block that was requested */
1371 + u_int16_t loport; /* Port that was requested, low or first */
1372 + u_int16_t hiport; /* Port that was requested, high or second */
1374 + uint method; /* RTSP method */
1375 + uint cseq; /* CSeq from request */
1379 +/* This structure exists only once per master */
1380 +struct ip_ct_rtsp_master
1388 +#include <linux/netfilter_ipv4/lockhelp.h>
1390 +#define RTSP_PORT 554
1392 +/* Protects rtsp part of conntracks */
1393 +DECLARE_LOCK_EXTERN(ip_rtsp_lock);
1395 +#endif /* __KERNEL__ */
1397 +#endif /* _IP_CONNTRACK_RTSP_H */
1399 +++ b/include/linux/netfilter_mime.h
1402 + * MIME functions for netfilter modules. This file provides implementations
1403 + * for basic MIME parsing. MIME headers are used in many protocols, such as
1404 + * HTTP, RTSP, SIP, etc.
1406 + * gcc will warn for defined but unused functions, so we only include the
1407 + * functions requested. The following macros are used:
1408 + * NF_NEED_MIME_NEXTLINE nf_mime_nextline()
1410 +#ifndef _NETFILTER_MIME_H
1411 +#define _NETFILTER_MIME_H
1413 +/* Only include these functions for kernel code. */
1416 +#include <linux/kernel.h>
1417 +#include <linux/ctype.h>
1420 + * Given a buffer and length, advance to the next line and mark the current
1421 + * line. If the current line is empty, *plinelen will be set to zero. If
1422 + * not, it will be set to the actual line length (including CRLF).
1424 + * 'line' in this context means logical line (includes LWS continuations).
1425 + * Returns 1 on success, 0 on failure.
1427 +#ifdef NF_NEED_MIME_NEXTLINE
1429 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
1433 + int is_first_line = 1;
1442 + while (p[off] != '\n')
1453 + /* if we saw a crlf, physlen needs adjusted */
1454 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
1459 + /* advance past the newline */
1462 + /* check for an empty line */
1468 + /* check for colon on the first physical line */
1469 + if (is_first_line)
1471 + is_first_line = 0;
1472 + if (memchr(p+(*poff), ':', physlen) == NULL)
1478 + while (p[off] == ' ' || p[off] == '\t');
1480 + *plineoff = *poff;
1481 + *plinelen = (physlen == 0) ? 0 : (off - *poff);
1486 +#endif /* NF_NEED_MIME_NEXTLINE */
1488 +#endif /* __KERNEL__ */
1490 +#endif /* _NETFILTER_MIME_H */
1491 --- a/include/linux/netfilter_ipv4/ip_conntrack.h
1492 +++ b/include/linux/netfilter_ipv4/ip_conntrack.h
1493 @@ -68,6 +68,7 @@ union ip_conntrack_expect_proto {
1494 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
1495 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
1496 #include <linux/netfilter_ipv4/ip_conntrack_h323.h>
1497 +#include <linux/netfilter_ipv4/ip_conntrack_rtsp.h>
1499 /* per expectation: application helper private data */
1500 union ip_conntrack_expect_help {
1501 @@ -76,6 +77,7 @@ union ip_conntrack_expect_help {
1502 struct ip_ct_ftp_expect exp_ftp_info;
1503 struct ip_ct_irc_expect exp_irc_info;
1504 struct ip_ct_h225_expect exp_h225_info;
1505 + struct ip_ct_rtsp_expect exp_rtsp_info;
1507 #ifdef CONFIG_IP_NF_NAT_NEEDED
1509 @@ -90,6 +92,7 @@ union ip_conntrack_help {
1510 struct ip_ct_ftp_master ct_ftp_info;
1511 struct ip_ct_irc_master ct_irc_info;
1512 struct ip_ct_h225_master ct_h225_info;
1513 + struct ip_ct_rtsp_master ct_rtsp_info;
1516 #ifdef CONFIG_IP_NF_NAT_NEEDED