1 Index: linux-2.4.35.4/net/ipv4/netfilter/Config.in
2 ===================================================================
3 --- linux-2.4.35.4.orig/net/ipv4/netfilter/Config.in
4 +++ linux-2.4.35.4/net/ipv4/netfilter/Config.in
5 @@ -16,6 +16,7 @@ if [ "$CONFIG_IP_NF_CONNTRACK" != "n" ];
6 dep_tristate ' GRE protocol support' CONFIG_IP_NF_CT_PROTO_GRE $CONFIG_IP_NF_CONNTRACK
7 dep_tristate ' PPTP protocol support' CONFIG_IP_NF_PPTP $CONFIG_IP_NF_CT_PROTO_GRE
8 dep_tristate ' H.323 (netmeeting) support' CONFIG_IP_NF_H323 $CONFIG_IP_NF_CONNTRACK
9 + dep_tristate ' RTSP protocol support' CONFIG_IP_NF_RTSP $CONFIG_IP_NF_CONNTRACK
12 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
13 @@ -118,6 +119,13 @@ if [ "$CONFIG_IP_NF_IPTABLES" != "n" ];
14 define_tristate CONFIG_IP_NF_NAT_H323 $CONFIG_IP_NF_NAT
17 + if [ "$CONFIG_IP_NF_RTSP" = "m" ]; then
18 + define_tristate CONFIG_IP_NF_NAT_RTSP m
20 + if [ "$CONFIG_IP_NF_RTSP" = "y" ]; then
21 + define_tristate CONFIG_IP_NF_NAT_RTSP $CONFIG_IP_NF_NAT
24 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
25 dep_tristate ' Basic SNMP-ALG support (EXPERIMENTAL)' CONFIG_IP_NF_NAT_SNMP_BASIC $CONFIG_IP_NF_NAT
27 Index: linux-2.4.35.4/net/ipv4/netfilter/Makefile
28 ===================================================================
29 --- linux-2.4.35.4.orig/net/ipv4/netfilter/Makefile
30 +++ linux-2.4.35.4/net/ipv4/netfilter/Makefile
31 @@ -57,6 +57,11 @@ obj-$(CONFIG_IP_NF_H323) += ip_conntrack
32 ifdef CONFIG_IP_NF_NAT_H323
33 export-objs += ip_conntrack_h323.o
35 +obj-$(CONFIG_IP_NF_RTSP) += ip_conntrack_rtsp.o
36 +ifdef CONFIG_IP_NF_NAT_RTSP
37 + export-objs += ip_conntrack_rtsp.o
43 @@ -67,6 +72,7 @@ obj-$(CONFIG_IP_NF_NAT_IRC) += ip_nat_ir
44 obj-$(CONFIG_IP_NF_NAT_PROTO_GRE) += ip_nat_proto_gre.o
45 obj-$(CONFIG_IP_NF_NAT_PPTP) += ip_nat_pptp.o
46 obj-$(CONFIG_IP_NF_NAT_H323) += ip_nat_h323.o
47 +obj-$(CONFIG_IP_NF_NAT_RTSP) += ip_nat_rtsp.o
50 obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o
51 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_conntrack_rtsp.c
52 ===================================================================
54 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_conntrack_rtsp.c
57 + * RTSP extension for IP connection tracking
58 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
59 + * based on ip_conntrack_irc.c
61 + * This program is free software; you can redistribute it and/or
62 + * modify it under the terms of the GNU General Public License
63 + * as published by the Free Software Foundation; either version
64 + * 2 of the License, or (at your option) any later version.
66 + * Module load syntax:
67 + * insmod ip_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
68 + * max_outstanding=n setup_timeout=secs
70 + * If no ports are specified, the default will be port 554.
72 + * With max_outstanding you can define the maximum number of not yet
73 + * answered SETUP requests per RTSP session (default 8).
74 + * With setup_timeout you can specify how long the system waits for
75 + * an expected data channel (default 300 seconds).
78 +#include <linux/config.h>
79 +#include <linux/module.h>
80 +#include <linux/netfilter.h>
81 +#include <linux/ip.h>
82 +#include <net/checksum.h>
85 +#include <linux/netfilter_ipv4/lockhelp.h>
86 +#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
87 +#include <linux/netfilter_ipv4/ip_conntrack_rtsp.h>
89 +#include <linux/ctype.h>
90 +#define NF_NEED_STRNCASECMP
91 +#define NF_NEED_STRTOU16
92 +#define NF_NEED_STRTOU32
93 +#define NF_NEED_NEXTLINE
94 +#include <linux/netfilter_helpers.h>
95 +#define NF_NEED_MIME_NEXTLINE
96 +#include <linux/netfilter_mime.h>
98 +#define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
100 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
101 +#ifdef IP_NF_RTSP_DEBUG
102 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
104 +#define DEBUGP(fmt, args...)
108 +static int ports[MAX_PORTS];
109 +static int num_ports = 0;
110 +static int max_outstanding = 8;
111 +static unsigned int setup_timeout = 300;
113 +MODULE_AUTHOR("Tom Marshall <tmarshall@real.com>");
114 +MODULE_DESCRIPTION("RTSP connection tracking module");
115 +MODULE_LICENSE("GPL");
117 +MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
118 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
119 +MODULE_PARM(max_outstanding, "i");
120 +MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
121 +MODULE_PARM(setup_timeout, "i");
122 +MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
125 +DECLARE_LOCK(ip_rtsp_lock);
126 +struct module* ip_conntrack_rtsp = THIS_MODULE;
129 + * Max mappings we will allow for one RTSP connection (for RTP, the number
130 + * of allocated ports is twice this value). Note that SMIL burns a lot of
131 + * ports so keep this reasonably high. If this is too low, you will see a
132 + * lot of "no free client map entries" messages.
134 +#define MAX_PORT_MAPS 16
136 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
138 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
141 + * Parse an RTSP packet.
143 + * Returns zero if parsing failed.
146 + * IN ptcp tcp data pointer
147 + * IN tcplen tcp data len
148 + * IN/OUT ptcpoff points to current tcp offset
149 + * OUT phdrsoff set to offset of rtsp headers
150 + * OUT phdrslen set to length of rtsp headers
151 + * OUT pcseqoff set to offset of CSeq header
152 + * OUT pcseqlen set to length of CSeq header
155 +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
156 + uint* phdrsoff, uint* phdrslen,
157 + uint* pcseqoff, uint* pcseqlen)
159 + uint entitylen = 0;
163 + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
168 + *phdrsoff = *ptcpoff;
169 + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
175 + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
179 + if (lineoff+linelen > tcplen)
181 + INFOP("!! overrun !!\n");
185 + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0)
187 + *pcseqoff = lineoff;
188 + *pcseqlen = linelen;
190 + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0)
192 + uint off = lineoff+15;
193 + SKIP_WSPACE(ptcp+lineoff, linelen, off);
194 + nf_strtou32(ptcp+off, &entitylen);
197 + *phdrslen = (*ptcpoff) - (*phdrsoff);
203 + * Find lo/hi client ports (if any) in transport header
205 + * ptcp, tcplen = packet
206 + * tranoff, tranlen = buffer to search
209 + * pport_lo, pport_hi = lo/hi ports (host endian)
211 + * Returns nonzero if any client ports found
213 + * Note: it is valid (and expected) for the client to request multiple
214 + * transports, so we need to parse the entire line.
217 +rtsp_parse_transport(char* ptran, uint tranlen,
218 + struct ip_ct_rtsp_expect* prtspexp)
223 + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
224 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
226 + INFOP("sanity check failed\n");
229 + DEBUGP("tran='%.*s'\n", (int)tranlen, ptran);
231 + SKIP_WSPACE(ptran, tranlen, off);
233 + /* Transport: tran;field;field=val,tran;field;field=val,... */
234 + while (off < tranlen)
236 + const char* pparamend;
239 + pparamend = memchr(ptran+off, ',', tranlen-off);
240 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
241 + nextparamoff = pparamend-ptran;
243 + while (off < nextparamoff)
245 + const char* pfieldend;
248 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
249 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
251 + if (strncmp(ptran+off, "client_port=", 12) == 0)
257 + numlen = nf_strtou16(ptran+off, &port);
259 + if (prtspexp->loport != 0 && prtspexp->loport != port)
261 + DEBUGP("multiple ports found, port %hu ignored\n", port);
265 + prtspexp->loport = prtspexp->hiport = port;
266 + if (ptran[off] == '-')
269 + numlen = nf_strtou16(ptran+off, &port);
271 + prtspexp->pbtype = pb_range;
272 + prtspexp->hiport = port;
274 + // If we have a range, assume rtp:
275 + // loport must be even, hiport must be loport+1
276 + if ((prtspexp->loport & 0x0001) != 0 ||
277 + prtspexp->hiport != prtspexp->loport+1)
279 + DEBUGP("incorrect range: %hu-%hu, correcting\n",
280 + prtspexp->loport, prtspexp->hiport);
281 + prtspexp->loport &= 0xfffe;
282 + prtspexp->hiport = prtspexp->loport+1;
285 + else if (ptran[off] == '/')
288 + numlen = nf_strtou16(ptran+off, &port);
290 + prtspexp->pbtype = pb_discon;
291 + prtspexp->hiport = port;
298 + * Note we don't look for the destination parameter here.
299 + * If we are using NAT, the NAT module will handle it. If not,
300 + * and the client is sending packets elsewhere, the expectation
301 + * will quietly time out.
304 + off = nextfieldoff;
307 + off = nextparamoff;
313 +/*** conntrack functions ***/
315 +/* outbound packet: client->server */
317 +help_out(const struct iphdr* iph, size_t pktlen,
318 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
320 + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
321 + struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
322 + uint tcplen = pktlen - iph->ihl * 4;
323 + char* pdata = (char*)tcph + tcph->doff * 4;
324 + uint datalen = tcplen - tcph->doff * 4;
327 + struct ip_conntrack_expect exp;
329 + while (dataoff < datalen)
331 + uint cmdoff = dataoff;
341 + if (!rtsp_parse_message(pdata, datalen, &dataoff,
342 + &hdrsoff, &hdrslen,
343 + &cseqoff, &cseqlen))
345 + break; /* not a valid message */
348 + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
350 + continue; /* not a SETUP message */
352 + DEBUGP("found a setup message\n");
354 + memset(&exp, 0, sizeof(exp));
357 + while (nf_mime_nextline(pdata+hdrsoff, hdrslen, &off,
358 + &lineoff, &linelen))
364 + if (off > hdrsoff+hdrslen)
366 + INFOP("!! overrun !!");
370 + if (nf_strncasecmp(pdata+hdrsoff+lineoff, "Transport:", 10) == 0)
372 + rtsp_parse_transport(pdata+hdrsoff+lineoff, linelen,
373 + &exp.help.exp_rtsp_info);
377 + if (exp.help.exp_rtsp_info.loport == 0)
379 + DEBUGP("no udp transports found\n");
380 + continue; /* no udp transports found */
383 + DEBUGP("udp transport found, ports=(%d,%hu,%hu)\n",
384 + (int)exp.help.exp_rtsp_info.pbtype,
385 + exp.help.exp_rtsp_info.loport,
386 + exp.help.exp_rtsp_info.hiport);
388 + LOCK_BH(&ip_rtsp_lock);
389 + exp.seq = ntohl(tcph->seq) + hdrsoff; /* mark all the headers */
390 + exp.help.exp_rtsp_info.len = hdrslen;
392 + exp.tuple.src.ip = ct->tuplehash[!dir].tuple.src.ip;
393 + exp.mask.src.ip = 0xffffffff;
394 + exp.tuple.dst.ip = ct->tuplehash[dir].tuple.src.ip;
395 + exp.mask.dst.ip = 0xffffffff;
396 + exp.tuple.dst.u.udp.port = exp.help.exp_rtsp_info.loport;
397 + exp.mask.dst.u.udp.port = (exp.help.exp_rtsp_info.pbtype == pb_range) ? 0xfffe : 0xffff;
398 + exp.tuple.dst.protonum = IPPROTO_UDP;
399 + exp.mask.dst.protonum = 0xffff;
401 + DEBUGP("expect_related %u.%u.%u.%u:%u-%u.%u.%u.%u:%u\n",
402 + NIPQUAD(exp.tuple.src.ip),
403 + ntohs(exp.tuple.src.u.tcp.port),
404 + NIPQUAD(exp.tuple.dst.ip),
405 + ntohs(exp.tuple.dst.u.tcp.port));
407 + /* pass the request off to the nat helper */
408 + rc = ip_conntrack_expect_related(ct, &exp);
409 + UNLOCK_BH(&ip_rtsp_lock);
412 + DEBUGP("ip_conntrack_expect_related succeeded\n");
416 + INFOP("ip_conntrack_expect_related failed (%d)\n", rc);
423 +/* inbound packet: server->client */
425 +help_in(const struct iphdr* iph, size_t pktlen,
426 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
432 +help(const struct iphdr* iph, size_t pktlen,
433 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
435 + /* tcplen not negative guarenteed by ip_conntrack_tcp.c */
436 + struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
437 + u_int32_t tcplen = pktlen - iph->ihl * 4;
439 + /* Until there's been traffic both ways, don't look in packets. */
440 + if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
442 + DEBUGP("conntrackinfo = %u\n", ctinfo);
446 + /* Not whole TCP header? */
447 + if (tcplen < sizeof(struct tcphdr) || tcplen < tcph->doff * 4)
449 + DEBUGP("tcplen = %u\n", (unsigned)tcplen);
453 + /* Checksum invalid? Ignore. */
454 + /* FIXME: Source route IP option packets --RR */
455 + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
456 + csum_partial((char*)tcph, tcplen, 0)))
458 + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
459 + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
463 + switch (CTINFO2DIR(ctinfo))
465 + case IP_CT_DIR_ORIGINAL:
466 + help_out(iph, pktlen, ct, ctinfo);
468 + case IP_CT_DIR_REPLY:
469 + help_in(iph, pktlen, ct, ctinfo);
476 +static struct ip_conntrack_helper rtsp_helpers[MAX_PORTS];
477 +static char rtsp_names[MAX_PORTS][10];
479 +/* This function is intentionally _NOT_ defined as __exit */
484 + for (i = 0; i < num_ports; i++)
486 + DEBUGP("unregistering port %d\n", ports[i]);
487 + ip_conntrack_helper_unregister(&rtsp_helpers[i]);
495 + struct ip_conntrack_helper *hlpr;
498 + printk("ip_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
500 + if (max_outstanding < 1)
502 + printk("ip_conntrack_rtsp: max_outstanding must be a positive integer\n");
505 + if (setup_timeout < 0)
507 + printk("ip_conntrack_rtsp: setup_timeout must be a positive integer\n");
511 + /* If no port given, default to standard rtsp port */
514 + ports[0] = RTSP_PORT;
517 + for (i = 0; (i < MAX_PORTS) && ports[i]; i++)
519 + hlpr = &rtsp_helpers[i];
520 + memset(hlpr, 0, sizeof(struct ip_conntrack_helper));
521 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
522 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
523 + hlpr->mask.src.u.tcp.port = 0xFFFF;
524 + hlpr->mask.dst.protonum = 0xFFFF;
525 + hlpr->max_expected = max_outstanding;
526 + hlpr->timeout = setup_timeout;
527 + hlpr->flags = IP_CT_HELPER_F_REUSE_EXPECT;
528 + hlpr->me = ip_conntrack_rtsp;
531 + tmpname = &rtsp_names[i][0];
532 + if (ports[i] == RTSP_PORT)
534 + sprintf(tmpname, "rtsp");
538 + sprintf(tmpname, "rtsp-%d", i);
540 + hlpr->name = tmpname;
542 + DEBUGP("port #%d: %d\n", i, ports[i]);
544 + ret = ip_conntrack_helper_register(hlpr);
548 + printk("ip_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
557 +#ifdef CONFIG_IP_NF_NAT_NEEDED
558 +EXPORT_SYMBOL(ip_rtsp_lock);
563 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_nat_rtsp.c
564 ===================================================================
566 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_nat_rtsp.c
569 + * RTSP extension for TCP NAT alteration
570 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
571 + * based on ip_nat_irc.c
573 + * This program is free software; you can redistribute it and/or
574 + * modify it under the terms of the GNU General Public License
575 + * as published by the Free Software Foundation; either version
576 + * 2 of the License, or (at your option) any later version.
578 + * Module load syntax:
579 + * insmod ip_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
580 + * stunaddr=<address>
581 + * destaction=[auto|strip|none]
583 + * If no ports are specified, the default will be port 554 only.
585 + * stunaddr specifies the address used to detect that a client is using STUN.
586 + * If this address is seen in the destination parameter, it is assumed that
587 + * the client has already punched a UDP hole in the firewall, so we don't
588 + * mangle the client_port. If none is specified, it is autodetected. It
589 + * only needs to be set if you have multiple levels of NAT. It should be
590 + * set to the external address that the STUN clients detect. Note that in
591 + * this case, it will not be possible for clients to use UDP with servers
592 + * between the NATs.
594 + * If no destaction is specified, auto is used.
595 + * destaction=auto: strip destination parameter if it is not stunaddr.
596 + * destaction=strip: always strip destination parameter (not recommended).
597 + * destaction=none: do not touch destination parameter (not recommended).
600 +#include <linux/module.h>
601 +#include <linux/netfilter_ipv4.h>
602 +#include <linux/ip.h>
603 +#include <linux/tcp.h>
604 +#include <linux/kernel.h>
605 +#include <net/tcp.h>
606 +#include <linux/netfilter_ipv4/ip_nat.h>
607 +#include <linux/netfilter_ipv4/ip_nat_helper.h>
608 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
609 +#include <linux/netfilter_ipv4/ip_conntrack_rtsp.h>
610 +#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
612 +#include <linux/inet.h>
613 +#include <linux/ctype.h>
614 +#define NF_NEED_STRNCASECMP
615 +#define NF_NEED_STRTOU16
616 +#include <linux/netfilter_helpers.h>
617 +#define NF_NEED_MIME_NEXTLINE
618 +#include <linux/netfilter_mime.h>
620 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
621 +#ifdef IP_NF_RTSP_DEBUG
622 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
624 +#define DEBUGP(fmt, args...)
628 +#define DSTACT_AUTO 0
629 +#define DSTACT_STRIP 1
630 +#define DSTACT_NONE 2
632 +static int ports[MAX_PORTS];
633 +static char* stunaddr = NULL;
634 +static char* destaction = NULL;
636 +static int num_ports = 0;
637 +static u_int32_t extip = 0;
638 +static int dstact = 0;
640 +MODULE_AUTHOR("Tom Marshall <tmarshall@real.com>");
641 +MODULE_DESCRIPTION("RTSP network address translation module");
642 +MODULE_LICENSE("GPL");
644 +MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
645 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
646 +MODULE_PARM(stunaddr, "s");
647 +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
648 +MODULE_PARM(destaction, "s");
649 +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
652 +/* protects rtsp part of conntracks */
653 +DECLARE_LOCK_EXTERN(ip_rtsp_lock);
655 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
657 +/*** helper functions ***/
660 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
662 + struct iphdr* iph = (struct iphdr*)skb->nh.iph;
663 + struct tcphdr* tcph = (struct tcphdr*)((char*)iph + iph->ihl*4);
665 + *pptcpdata = (char*)tcph + tcph->doff*4;
666 + *ptcpdatalen = ((char*)skb->h.raw + skb->len) - *pptcpdata;
669 +/*** nat functions ***/
672 + * Mangle the "Transport:" header:
673 + * - Replace all occurences of "client_port=<spec>"
674 + * - Handle destination parameter
677 + * ct, ctinfo = conntrack context
679 + * tranoff = Transport header offset from TCP data
680 + * tranlen = Transport header length (incl. CRLF)
681 + * rport_lo = replacement low port (host endian)
682 + * rport_hi = replacement high port (host endian)
684 + * Returns packet size difference.
686 + * Assumes that a complete transport header is present, ending with CR or LF
689 +rtsp_mangle_tran(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
690 + struct ip_conntrack_expect* exp,
691 + struct sk_buff** pskb, uint tranoff, uint tranlen)
696 + char rbuf1[16]; /* Replacement buffer (one port) */
697 + uint rbuf1len; /* Replacement len (one port) */
698 + char rbufa[16]; /* Replacement buffer (all ports) */
699 + uint rbufalen; /* Replacement len (all ports) */
701 + u_int16_t loport, hiport;
703 + uint diff; /* Number of bytes we removed */
705 + struct ip_ct_rtsp_expect* prtspexp = &exp->help.exp_rtsp_info;
706 + struct ip_conntrack_tuple t;
708 + char szextaddr[15+1];
712 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
713 + ptran = ptcp+tranoff;
715 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
716 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
717 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
719 + INFOP("sanity check failed\n");
723 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
725 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
729 + extaddrlen = extip ? sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(extip))
730 + : sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(newip));
731 + DEBUGP("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
733 + rbuf1len = rbufalen = 0;
734 + switch (prtspexp->pbtype)
737 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
739 + t.dst.u.udp.port = htons(loport);
740 + if (ip_conntrack_change_expect(exp, &t) == 0)
742 + DEBUGP("using port %hu\n", loport);
748 + rbuf1len = sprintf(rbuf1, "%hu", loport);
749 + rbufalen = sprintf(rbufa, "%hu", loport);
753 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
755 + t.dst.u.udp.port = htons(loport);
756 + if (ip_conntrack_change_expect(exp, &t) == 0)
758 + hiport = loport + ~exp->mask.dst.u.udp.port;
759 + DEBUGP("using ports %hu-%hu\n", loport, hiport);
765 + rbuf1len = sprintf(rbuf1, "%hu", loport);
766 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
770 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
772 + t.dst.u.udp.port = htons(loport);
773 + if (ip_conntrack_change_expect(exp, &t) == 0)
775 + DEBUGP("using port %hu (1 of 2)\n", loport);
779 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
781 + t.dst.u.udp.port = htons(hiport);
782 + if (ip_conntrack_change_expect(exp, &t) == 0)
784 + DEBUGP("using port %hu (2 of 2)\n", hiport);
788 + if (loport != 0 && hiport != 0)
790 + rbuf1len = sprintf(rbuf1, "%hu", loport);
791 + if (hiport == loport+1)
793 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
797 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
805 + return 0; /* cannot get replacement port(s) */
808 + /* Transport: tran;field;field=val,tran;field;field=val,... */
809 + while (off < tranlen)
812 + const char* pparamend;
815 + pparamend = memchr(ptran+off, ',', tranlen-off);
816 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
817 + nextparamoff = pparamend-ptcp;
820 + * We pass over each param twice. On the first pass, we look for a
821 + * destination= field. It is handled by the security policy. If it
822 + * is present, allowed, and equal to our external address, we assume
823 + * that STUN is being used and we leave the client_port= field alone.
827 + while (off < nextparamoff)
829 + const char* pfieldend;
832 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
833 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
835 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
837 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
841 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
843 + diff = nextfieldoff-off;
844 + if (!ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
845 + off, diff, NULL, 0))
847 + /* mangle failed, all we can do is bail */
850 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
851 + ptran = ptcp+tranoff;
853 + nextparamoff -= diff;
854 + nextfieldoff -= diff;
858 + off = nextfieldoff;
865 + while (off < nextparamoff)
867 + const char* pfieldend;
870 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
871 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
873 + if (strncmp(ptran+off, "client_port=", 12) == 0)
879 + char* rbuf = rbuf1;
880 + uint rbuflen = rbuf1len;
883 + origoff = (ptran-ptcp)+off;
885 + numlen = nf_strtou16(ptran+off, &port);
888 + if (port != prtspexp->loport)
890 + DEBUGP("multiple ports found, port %hu ignored\n", port);
894 + if (ptran[off] == '-' || ptran[off] == '/')
898 + numlen = nf_strtou16(ptran+off, &port);
902 + rbuflen = rbufalen;
906 + * note we cannot just memcpy() if the sizes are the same.
907 + * the mangle function does skb resizing, checks for a
908 + * cloned skb, and updates the checksums.
910 + * parameter 4 below is offset from start of tcp data.
912 + diff = origlen-rbuflen;
913 + if (!ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
914 + origoff, origlen, rbuf, rbuflen))
916 + /* mangle failed, all we can do is bail */
919 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
920 + ptran = ptcp+tranoff;
922 + nextparamoff -= diff;
923 + nextfieldoff -= diff;
927 + off = nextfieldoff;
930 + off = nextparamoff;
937 +expected(struct sk_buff **pskb, uint hooknum, struct ip_conntrack* ct, struct ip_nat_info* info)
939 + struct ip_nat_multi_range mr;
940 + u_int32_t newdstip, newsrcip, newip;
942 + struct ip_conntrack *master = master_ct(ct);
944 + IP_NF_ASSERT(info);
945 + IP_NF_ASSERT(master);
947 + IP_NF_ASSERT(!(info->initialized & (1 << HOOK2MANIP(hooknum))));
949 + newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
950 + newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
951 + newip = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) ? newsrcip : newdstip;
953 + DEBUGP("newsrcip=%u.%u.%u.%u, newdstip=%u.%u.%u.%u, newip=%u.%u.%u.%u\n",
954 + NIPQUAD(newsrcip), NIPQUAD(newdstip), NIPQUAD(newip));
957 + /* We don't want to manip the per-protocol, just the IPs. */
958 + mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
959 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
961 + return ip_nat_setup_info(ct, &mr, hooknum);
965 +help_out(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
966 + struct ip_conntrack_expect* exp, struct sk_buff** pskb)
976 + struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
977 + struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
979 + struct ip_ct_rtsp_expect* prtspexp = &exp->help.exp_rtsp_info;
981 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
983 + hdrsoff = exp->seq - ntohl(tcph->seq);
984 + hdrslen = prtspexp->len;
987 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
993 + if (off > hdrsoff+hdrslen)
995 + INFOP("!! overrun !!");
998 + DEBUGP("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1000 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
1002 + uint oldtcplen = tcplen;
1003 + if (!rtsp_mangle_tran(ct, ctinfo, exp, pskb, lineoff, linelen))
1007 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1008 + hdrslen -= (oldtcplen-tcplen);
1009 + off -= (oldtcplen-tcplen);
1010 + lineoff -= (oldtcplen-tcplen);
1011 + linelen -= (oldtcplen-tcplen);
1012 + DEBUGP("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1020 +help_in(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
1021 + struct ip_conntrack_expect* exp, struct sk_buff** pskb)
1023 + /* XXX: unmangle */
1028 +help(struct ip_conntrack* ct,
1029 + struct ip_conntrack_expect* exp,
1030 + struct ip_nat_info* info,
1031 + enum ip_conntrack_info ctinfo,
1032 + unsigned int hooknum,
1033 + struct sk_buff** pskb)
1035 + struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
1036 + struct tcphdr* tcph = (struct tcphdr*)((char*)iph + iph->ihl * 4);
1039 + struct ip_ct_rtsp_expect* ct_rtsp_info;
1040 + int rc = NF_ACCEPT;
1042 + if (ct == NULL || exp == NULL || info == NULL || pskb == NULL)
1044 + DEBUGP("!! null ptr (%p,%p,%p,%p) !!\n", ct, exp, info, pskb);
1048 + ct_rtsp_info = &exp->help.exp_rtsp_info;
1051 + * Only mangle things once: original direction in POST_ROUTING
1052 + * and reply direction on PRE_ROUTING.
1054 + dir = CTINFO2DIR(ctinfo);
1055 + if (!((hooknum == NF_IP_POST_ROUTING && dir == IP_CT_DIR_ORIGINAL)
1056 + || (hooknum == NF_IP_PRE_ROUTING && dir == IP_CT_DIR_REPLY)))
1058 + DEBUGP("Not touching dir %s at hook %s\n",
1059 + dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY",
1060 + hooknum == NF_IP_POST_ROUTING ? "POSTROUTING"
1061 + : hooknum == NF_IP_PRE_ROUTING ? "PREROUTING"
1062 + : hooknum == NF_IP_LOCAL_OUT ? "OUTPUT" : "???");
1065 + DEBUGP("got beyond not touching\n");
1067 + datalen = (*pskb)->len - iph->ihl * 4 - tcph->doff * 4;
1069 + LOCK_BH(&ip_rtsp_lock);
1070 + /* Ensure the packet contains all of the marked data */
1071 + if (!between(exp->seq + ct_rtsp_info->len,
1072 + ntohl(tcph->seq), ntohl(tcph->seq) + datalen))
1074 + /* Partial retransmission? Probably a hacker. */
1075 + if (net_ratelimit())
1077 + INFOP("partial packet %u/%u in %u/%u\n",
1078 + exp->seq, ct_rtsp_info->len, ntohl(tcph->seq), ntohl(tcph->seq) + datalen);
1080 + UNLOCK_BH(&ip_rtsp_lock);
1086 + case IP_CT_DIR_ORIGINAL:
1087 + rc = help_out(ct, ctinfo, exp, pskb);
1089 + case IP_CT_DIR_REPLY:
1090 + rc = help_in(ct, ctinfo, exp, pskb);
1093 + UNLOCK_BH(&ip_rtsp_lock);
1098 +static struct ip_nat_helper ip_nat_rtsp_helpers[MAX_PORTS];
1099 +static char rtsp_names[MAX_PORTS][10];
1101 +/* This function is intentionally _NOT_ defined as __exit */
1107 + for (i = 0; i < num_ports; i++)
1109 + DEBUGP("unregistering helper for port %d\n", ports[i]);
1110 + ip_nat_helper_unregister(&ip_nat_rtsp_helpers[i]);
1119 + struct ip_nat_helper* hlpr;
1122 + printk("ip_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1124 + if (ports[0] == 0)
1126 + ports[0] = RTSP_PORT;
1129 + for (i = 0; (i < MAX_PORTS) && ports[i] != 0; i++)
1131 + hlpr = &ip_nat_rtsp_helpers[i];
1132 + memset(hlpr, 0, sizeof(struct ip_nat_helper));
1134 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
1135 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
1136 + hlpr->mask.src.u.tcp.port = 0xFFFF;
1137 + hlpr->mask.dst.protonum = 0xFFFF;
1138 + hlpr->help = help;
1140 + hlpr->me = THIS_MODULE;
1141 + hlpr->expect = expected;
1143 + tmpname = &rtsp_names[i][0];
1144 + if (ports[i] == RTSP_PORT)
1146 + sprintf(tmpname, "rtsp");
1150 + sprintf(tmpname, "rtsp-%d", i);
1152 + hlpr->name = tmpname;
1154 + DEBUGP("registering helper for port %d: name %s\n", ports[i], hlpr->name);
1155 + ret = ip_nat_helper_register(hlpr);
1159 + printk("ip_nat_rtsp: error registering helper for port %d\n", ports[i]);
1165 + if (stunaddr != NULL)
1167 + extip = in_aton(stunaddr);
1169 + if (destaction != NULL)
1171 + if (strcmp(destaction, "auto") == 0)
1173 + dstact = DSTACT_AUTO;
1175 + if (strcmp(destaction, "strip") == 0)
1177 + dstact = DSTACT_STRIP;
1179 + if (strcmp(destaction, "none") == 0)
1181 + dstact = DSTACT_NONE;
1189 Index: linux-2.4.35.4/arch/mips/kernel/mips_ksyms.c
1190 ===================================================================
1191 --- linux-2.4.35.4.orig/arch/mips/kernel/mips_ksyms.c
1192 +++ linux-2.4.35.4/arch/mips/kernel/mips_ksyms.c
1193 @@ -52,6 +52,7 @@ EXPORT_SYMBOL(EISA_bus);
1197 +EXPORT_SYMBOL_NOVERS(memchr);
1198 EXPORT_SYMBOL_NOVERS(memcmp);
1199 EXPORT_SYMBOL_NOVERS(memset);
1200 EXPORT_SYMBOL_NOVERS(memcpy);
1201 Index: linux-2.4.35.4/include/linux/netfilter_helpers.h
1202 ===================================================================
1204 +++ linux-2.4.35.4/include/linux/netfilter_helpers.h
1207 + * Helpers for netfiler modules. This file provides implementations for basic
1208 + * functions such as strncasecmp(), etc.
1210 + * gcc will warn for defined but unused functions, so we only include the
1211 + * functions requested. The following macros are used:
1212 + * NF_NEED_STRNCASECMP nf_strncasecmp()
1213 + * NF_NEED_STRTOU16 nf_strtou16()
1214 + * NF_NEED_STRTOU32 nf_strtou32()
1216 +#ifndef _NETFILTER_HELPERS_H
1217 +#define _NETFILTER_HELPERS_H
1219 +/* Only include these functions for kernel code. */
1222 +#include <linux/ctype.h>
1223 +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
1226 + * The standard strncasecmp()
1228 +#ifdef NF_NEED_STRNCASECMP
1230 +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
1232 + if (s1 == NULL || s2 == NULL)
1234 + if (s1 == NULL && s2 == NULL)
1238 + return (s1 == NULL) ? -1 : 1;
1240 + while (len > 0 && tolower(*s1) == tolower(*s2))
1246 + return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
1248 +#endif /* NF_NEED_STRNCASECMP */
1251 + * Parse a string containing a 16-bit unsigned integer.
1252 + * Returns the number of chars used, or zero if no number is found.
1254 +#ifdef NF_NEED_STRTOU16
1256 +nf_strtou16(const char* pbuf, u_int16_t* pval)
1261 + while (isdigit(pbuf[n]))
1263 + *pval = (*pval * 10) + (pbuf[n] - '0');
1269 +#endif /* NF_NEED_STRTOU16 */
1272 + * Parse a string containing a 32-bit unsigned integer.
1273 + * Returns the number of chars used, or zero if no number is found.
1275 +#ifdef NF_NEED_STRTOU32
1277 +nf_strtou32(const char* pbuf, u_int32_t* pval)
1282 + while (pbuf[n] >= '0' && pbuf[n] <= '9')
1284 + *pval = (*pval * 10) + (pbuf[n] - '0');
1290 +#endif /* NF_NEED_STRTOU32 */
1293 + * Given a buffer and length, advance to the next line and mark the current
1296 +#ifdef NF_NEED_NEXTLINE
1298 +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
1308 + while (p[off] != '\n')
1319 + /* if we saw a crlf, physlen needs adjusted */
1320 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
1325 + /* advance past the newline */
1328 + *plineoff = *poff;
1329 + *plinelen = physlen;
1334 +#endif /* NF_NEED_NEXTLINE */
1336 +#endif /* __KERNEL__ */
1338 +#endif /* _NETFILTER_HELPERS_H */
1339 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_conntrack_rtsp.h
1340 ===================================================================
1342 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_conntrack_rtsp.h
1345 + * RTSP extension for IP connection tracking.
1346 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
1347 + * based on ip_conntrack_irc.h
1349 + * This program is free software; you can redistribute it and/or
1350 + * modify it under the terms of the GNU General Public License
1351 + * as published by the Free Software Foundation; either version
1352 + * 2 of the License, or (at your option) any later version.
1354 +#ifndef _IP_CONNTRACK_RTSP_H
1355 +#define _IP_CONNTRACK_RTSP_H
1357 +/* #define IP_NF_RTSP_DEBUG */
1358 +#define IP_NF_RTSP_VERSION "0.01"
1360 +/* port block types */
1362 + pb_single, /* client_port=x */
1363 + pb_range, /* client_port=x-y */
1364 + pb_discon /* client_port=x/y (rtspbis) */
1367 +/* We record seq number and length of rtsp headers here, all in host order. */
1370 + * This structure is per expected connection. It is a member of struct
1371 + * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
1372 + * there and we are expected to only store the length of the data which
1373 + * needs replaced. If a packet contains multiple RTSP messages, we create
1374 + * one expected connection per message.
1376 + * We use these variables to mark the entire header block. This may seem
1377 + * like overkill, but the nature of RTSP requires it. A header may appear
1378 + * multiple times in a message. We must treat two Transport headers the
1379 + * same as one Transport header with two entries.
1381 +struct ip_ct_rtsp_expect
1383 + u_int32_t len; /* length of header block */
1384 + portblock_t pbtype; /* Type of port block that was requested */
1385 + u_int16_t loport; /* Port that was requested, low or first */
1386 + u_int16_t hiport; /* Port that was requested, high or second */
1388 + uint method; /* RTSP method */
1389 + uint cseq; /* CSeq from request */
1393 +/* This structure exists only once per master */
1394 +struct ip_ct_rtsp_master
1402 +#include <linux/netfilter_ipv4/lockhelp.h>
1404 +#define RTSP_PORT 554
1406 +/* Protects rtsp part of conntracks */
1407 +DECLARE_LOCK_EXTERN(ip_rtsp_lock);
1409 +#endif /* __KERNEL__ */
1411 +#endif /* _IP_CONNTRACK_RTSP_H */
1412 Index: linux-2.4.35.4/include/linux/netfilter_mime.h
1413 ===================================================================
1415 +++ linux-2.4.35.4/include/linux/netfilter_mime.h
1418 + * MIME functions for netfilter modules. This file provides implementations
1419 + * for basic MIME parsing. MIME headers are used in many protocols, such as
1420 + * HTTP, RTSP, SIP, etc.
1422 + * gcc will warn for defined but unused functions, so we only include the
1423 + * functions requested. The following macros are used:
1424 + * NF_NEED_MIME_NEXTLINE nf_mime_nextline()
1426 +#ifndef _NETFILTER_MIME_H
1427 +#define _NETFILTER_MIME_H
1429 +/* Only include these functions for kernel code. */
1432 +#include <linux/kernel.h>
1433 +#include <linux/ctype.h>
1436 + * Given a buffer and length, advance to the next line and mark the current
1437 + * line. If the current line is empty, *plinelen will be set to zero. If
1438 + * not, it will be set to the actual line length (including CRLF).
1440 + * 'line' in this context means logical line (includes LWS continuations).
1441 + * Returns 1 on success, 0 on failure.
1443 +#ifdef NF_NEED_MIME_NEXTLINE
1445 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
1449 + int is_first_line = 1;
1458 + while (p[off] != '\n')
1469 + /* if we saw a crlf, physlen needs adjusted */
1470 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
1475 + /* advance past the newline */
1478 + /* check for an empty line */
1484 + /* check for colon on the first physical line */
1485 + if (is_first_line)
1487 + is_first_line = 0;
1488 + if (memchr(p+(*poff), ':', physlen) == NULL)
1494 + while (p[off] == ' ' || p[off] == '\t');
1496 + *plineoff = *poff;
1497 + *plinelen = (physlen == 0) ? 0 : (off - *poff);
1502 +#endif /* NF_NEED_MIME_NEXTLINE */
1504 +#endif /* __KERNEL__ */
1506 +#endif /* _NETFILTER_MIME_H */
1507 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_conntrack.h
1508 ===================================================================
1509 --- linux-2.4.35.4.orig/include/linux/netfilter_ipv4/ip_conntrack.h
1510 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_conntrack.h
1511 @@ -72,6 +72,7 @@ union ip_conntrack_expect_proto {
1512 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
1513 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
1514 #include <linux/netfilter_ipv4/ip_conntrack_h323.h>
1515 +#include <linux/netfilter_ipv4/ip_conntrack_rtsp.h>
1517 /* per expectation: application helper private data */
1518 union ip_conntrack_expect_help {
1519 @@ -81,6 +82,7 @@ union ip_conntrack_expect_help {
1520 struct ip_ct_irc_expect exp_irc_info;
1521 struct ip_ct_pptp_expect exp_pptp_info;
1522 struct ip_ct_h225_expect exp_h225_info;
1523 + struct ip_ct_rtsp_expect exp_rtsp_info;
1525 #ifdef CONFIG_IP_NF_NAT_NEEDED
1527 @@ -96,6 +98,7 @@ union ip_conntrack_help {
1528 struct ip_ct_irc_master ct_irc_info;
1529 struct ip_ct_pptp_master ct_pptp_info;
1530 struct ip_ct_h225_master ct_h225_info;
1531 + struct ip_ct_rtsp_master ct_rtsp_info;
1534 #ifdef CONFIG_IP_NF_NAT_NEEDED