7fd2c8b3cb4d3bbb699b6c496c349fbda87d50ef
[openwrt/staging/yousong.git] / target / linux / generic-2.4 / patches / 614-netfilter_nat_rtsp.patch
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
10 fi
11
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
15 fi
16 fi
17 + if [ "$CONFIG_IP_NF_RTSP" = "m" ]; then
18 + define_tristate CONFIG_IP_NF_NAT_RTSP m
19 + else
20 + if [ "$CONFIG_IP_NF_RTSP" = "y" ]; then
21 + define_tristate CONFIG_IP_NF_NAT_RTSP $CONFIG_IP_NF_NAT
22 + fi
23 + fi
24 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
25 dep_tristate ' Basic SNMP-ALG support (EXPERIMENTAL)' CONFIG_IP_NF_NAT_SNMP_BASIC $CONFIG_IP_NF_NAT
26 fi
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
34 endif
35 +obj-$(CONFIG_IP_NF_RTSP) += ip_conntrack_rtsp.o
36 +ifdef CONFIG_IP_NF_NAT_RTSP
37 + export-objs += ip_conntrack_rtsp.o
38 +endif
39 +
40
41
42 # NAT helpers
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
48
49 # generic IP tables
50 obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o
51 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_conntrack_rtsp.c
52 ===================================================================
53 --- /dev/null
54 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_conntrack_rtsp.c
55 @@ -0,0 +1,507 @@
56 +/*
57 + * RTSP extension for IP connection tracking
58 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
59 + * based on ip_conntrack_irc.c
60 + *
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.
65 + *
66 + * Module load syntax:
67 + * insmod ip_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
68 + * max_outstanding=n setup_timeout=secs
69 + *
70 + * If no ports are specified, the default will be port 554.
71 + *
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).
76 + */
77 +
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>
83 +#include <net/tcp.h>
84 +
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>
88 +
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>
97 +
98 +#define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
99 +
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)
103 +#else
104 +#define DEBUGP(fmt, args...)
105 +#endif
106 +
107 +#define MAX_PORTS 8
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;
112 +
113 +MODULE_AUTHOR("Tom Marshall <tmarshall@real.com>");
114 +MODULE_DESCRIPTION("RTSP connection tracking module");
115 +MODULE_LICENSE("GPL");
116 +#ifdef MODULE_PARM
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");
123 +#endif
124 +
125 +DECLARE_LOCK(ip_rtsp_lock);
126 +struct module* ip_conntrack_rtsp = THIS_MODULE;
127 +
128 +/*
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.
133 + */
134 +#define MAX_PORT_MAPS 16
135 +
136 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
137 +
138 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
139 +
140 +/*
141 + * Parse an RTSP packet.
142 + *
143 + * Returns zero if parsing failed.
144 + *
145 + * Parameters:
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
153 + */
154 +static int
155 +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
156 + uint* phdrsoff, uint* phdrslen,
157 + uint* pcseqoff, uint* pcseqlen)
158 +{
159 + uint entitylen = 0;
160 + uint lineoff;
161 + uint linelen;
162 +
163 + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
164 + {
165 + return 0;
166 + }
167 +
168 + *phdrsoff = *ptcpoff;
169 + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
170 + {
171 + if (linelen == 0)
172 + {
173 + if (entitylen > 0)
174 + {
175 + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
176 + }
177 + break;
178 + }
179 + if (lineoff+linelen > tcplen)
180 + {
181 + INFOP("!! overrun !!\n");
182 + break;
183 + }
184 +
185 + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0)
186 + {
187 + *pcseqoff = lineoff;
188 + *pcseqlen = linelen;
189 + }
190 + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0)
191 + {
192 + uint off = lineoff+15;
193 + SKIP_WSPACE(ptcp+lineoff, linelen, off);
194 + nf_strtou32(ptcp+off, &entitylen);
195 + }
196 + }
197 + *phdrslen = (*ptcpoff) - (*phdrsoff);
198 +
199 + return 1;
200 +}
201 +
202 +/*
203 + * Find lo/hi client ports (if any) in transport header
204 + * In:
205 + * ptcp, tcplen = packet
206 + * tranoff, tranlen = buffer to search
207 + *
208 + * Out:
209 + * pport_lo, pport_hi = lo/hi ports (host endian)
210 + *
211 + * Returns nonzero if any client ports found
212 + *
213 + * Note: it is valid (and expected) for the client to request multiple
214 + * transports, so we need to parse the entire line.
215 + */
216 +static int
217 +rtsp_parse_transport(char* ptran, uint tranlen,
218 + struct ip_ct_rtsp_expect* prtspexp)
219 +{
220 + int rc = 0;
221 + uint off = 0;
222 +
223 + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
224 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
225 + {
226 + INFOP("sanity check failed\n");
227 + return 0;
228 + }
229 + DEBUGP("tran='%.*s'\n", (int)tranlen, ptran);
230 + off += 10;
231 + SKIP_WSPACE(ptran, tranlen, off);
232 +
233 + /* Transport: tran;field;field=val,tran;field;field=val,... */
234 + while (off < tranlen)
235 + {
236 + const char* pparamend;
237 + uint nextparamoff;
238 +
239 + pparamend = memchr(ptran+off, ',', tranlen-off);
240 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
241 + nextparamoff = pparamend-ptran;
242 +
243 + while (off < nextparamoff)
244 + {
245 + const char* pfieldend;
246 + uint nextfieldoff;
247 +
248 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
249 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
250 +
251 + if (strncmp(ptran+off, "client_port=", 12) == 0)
252 + {
253 + u_int16_t port;
254 + uint numlen;
255 +
256 + off += 12;
257 + numlen = nf_strtou16(ptran+off, &port);
258 + off += numlen;
259 + if (prtspexp->loport != 0 && prtspexp->loport != port)
260 + {
261 + DEBUGP("multiple ports found, port %hu ignored\n", port);
262 + }
263 + else
264 + {
265 + prtspexp->loport = prtspexp->hiport = port;
266 + if (ptran[off] == '-')
267 + {
268 + off++;
269 + numlen = nf_strtou16(ptran+off, &port);
270 + off += numlen;
271 + prtspexp->pbtype = pb_range;
272 + prtspexp->hiport = port;
273 +
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)
278 + {
279 + DEBUGP("incorrect range: %hu-%hu, correcting\n",
280 + prtspexp->loport, prtspexp->hiport);
281 + prtspexp->loport &= 0xfffe;
282 + prtspexp->hiport = prtspexp->loport+1;
283 + }
284 + }
285 + else if (ptran[off] == '/')
286 + {
287 + off++;
288 + numlen = nf_strtou16(ptran+off, &port);
289 + off += numlen;
290 + prtspexp->pbtype = pb_discon;
291 + prtspexp->hiport = port;
292 + }
293 + rc = 1;
294 + }
295 + }
296 +
297 + /*
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.
302 + */
303 +
304 + off = nextfieldoff;
305 + }
306 +
307 + off = nextparamoff;
308 + }
309 +
310 + return rc;
311 +}
312 +
313 +/*** conntrack functions ***/
314 +
315 +/* outbound packet: client->server */
316 +static int
317 +help_out(const struct iphdr* iph, size_t pktlen,
318 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
319 +{
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;
325 + uint dataoff = 0;
326 +
327 + struct ip_conntrack_expect exp;
328 +
329 + while (dataoff < datalen)
330 + {
331 + uint cmdoff = dataoff;
332 + uint hdrsoff = 0;
333 + uint hdrslen = 0;
334 + uint cseqoff = 0;
335 + uint cseqlen = 0;
336 + uint lineoff = 0;
337 + uint linelen = 0;
338 + uint off;
339 + int rc;
340 +
341 + if (!rtsp_parse_message(pdata, datalen, &dataoff,
342 + &hdrsoff, &hdrslen,
343 + &cseqoff, &cseqlen))
344 + {
345 + break; /* not a valid message */
346 + }
347 +
348 + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
349 + {
350 + continue; /* not a SETUP message */
351 + }
352 + DEBUGP("found a setup message\n");
353 +
354 + memset(&exp, 0, sizeof(exp));
355 +
356 + off = 0;
357 + while (nf_mime_nextline(pdata+hdrsoff, hdrslen, &off,
358 + &lineoff, &linelen))
359 + {
360 + if (linelen == 0)
361 + {
362 + break;
363 + }
364 + if (off > hdrsoff+hdrslen)
365 + {
366 + INFOP("!! overrun !!");
367 + break;
368 + }
369 +
370 + if (nf_strncasecmp(pdata+hdrsoff+lineoff, "Transport:", 10) == 0)
371 + {
372 + rtsp_parse_transport(pdata+hdrsoff+lineoff, linelen,
373 + &exp.help.exp_rtsp_info);
374 + }
375 + }
376 +
377 + if (exp.help.exp_rtsp_info.loport == 0)
378 + {
379 + DEBUGP("no udp transports found\n");
380 + continue; /* no udp transports found */
381 + }
382 +
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);
387 +
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;
391 +
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;
400 +
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));
406 +
407 + /* pass the request off to the nat helper */
408 + rc = ip_conntrack_expect_related(ct, &exp);
409 + UNLOCK_BH(&ip_rtsp_lock);
410 + if (rc == 0)
411 + {
412 + DEBUGP("ip_conntrack_expect_related succeeded\n");
413 + }
414 + else
415 + {
416 + INFOP("ip_conntrack_expect_related failed (%d)\n", rc);
417 + }
418 + }
419 +
420 + return NF_ACCEPT;
421 +}
422 +
423 +/* inbound packet: server->client */
424 +static int
425 +help_in(const struct iphdr* iph, size_t pktlen,
426 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
427 +{
428 + return NF_ACCEPT;
429 +}
430 +
431 +static int
432 +help(const struct iphdr* iph, size_t pktlen,
433 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
434 +{
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;
438 +
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)
441 + {
442 + DEBUGP("conntrackinfo = %u\n", ctinfo);
443 + return NF_ACCEPT;
444 + }
445 +
446 + /* Not whole TCP header? */
447 + if (tcplen < sizeof(struct tcphdr) || tcplen < tcph->doff * 4)
448 + {
449 + DEBUGP("tcplen = %u\n", (unsigned)tcplen);
450 + return NF_ACCEPT;
451 + }
452 +
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)))
457 + {
458 + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
459 + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
460 + return NF_ACCEPT;
461 + }
462 +
463 + switch (CTINFO2DIR(ctinfo))
464 + {
465 + case IP_CT_DIR_ORIGINAL:
466 + help_out(iph, pktlen, ct, ctinfo);
467 + break;
468 + case IP_CT_DIR_REPLY:
469 + help_in(iph, pktlen, ct, ctinfo);
470 + break;
471 + }
472 +
473 + return NF_ACCEPT;
474 +}
475 +
476 +static struct ip_conntrack_helper rtsp_helpers[MAX_PORTS];
477 +static char rtsp_names[MAX_PORTS][10];
478 +
479 +/* This function is intentionally _NOT_ defined as __exit */
480 +static void
481 +fini(void)
482 +{
483 + int i;
484 + for (i = 0; i < num_ports; i++)
485 + {
486 + DEBUGP("unregistering port %d\n", ports[i]);
487 + ip_conntrack_helper_unregister(&rtsp_helpers[i]);
488 + }
489 +}
490 +
491 +static int __init
492 +init(void)
493 +{
494 + int i, ret;
495 + struct ip_conntrack_helper *hlpr;
496 + char *tmpname;
497 +
498 + printk("ip_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
499 +
500 + if (max_outstanding < 1)
501 + {
502 + printk("ip_conntrack_rtsp: max_outstanding must be a positive integer\n");
503 + return -EBUSY;
504 + }
505 + if (setup_timeout < 0)
506 + {
507 + printk("ip_conntrack_rtsp: setup_timeout must be a positive integer\n");
508 + return -EBUSY;
509 + }
510 +
511 + /* If no port given, default to standard rtsp port */
512 + if (ports[0] == 0)
513 + {
514 + ports[0] = RTSP_PORT;
515 + }
516 +
517 + for (i = 0; (i < MAX_PORTS) && ports[i]; i++)
518 + {
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;
529 + hlpr->help = help;
530 +
531 + tmpname = &rtsp_names[i][0];
532 + if (ports[i] == RTSP_PORT)
533 + {
534 + sprintf(tmpname, "rtsp");
535 + }
536 + else
537 + {
538 + sprintf(tmpname, "rtsp-%d", i);
539 + }
540 + hlpr->name = tmpname;
541 +
542 + DEBUGP("port #%d: %d\n", i, ports[i]);
543 +
544 + ret = ip_conntrack_helper_register(hlpr);
545 +
546 + if (ret)
547 + {
548 + printk("ip_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
549 + fini();
550 + return -EBUSY;
551 + }
552 + num_ports++;
553 + }
554 + return 0;
555 +}
556 +
557 +#ifdef CONFIG_IP_NF_NAT_NEEDED
558 +EXPORT_SYMBOL(ip_rtsp_lock);
559 +#endif
560 +
561 +module_init(init);
562 +module_exit(fini);
563 Index: linux-2.4.35.4/net/ipv4/netfilter/ip_nat_rtsp.c
564 ===================================================================
565 --- /dev/null
566 +++ linux-2.4.35.4/net/ipv4/netfilter/ip_nat_rtsp.c
567 @@ -0,0 +1,621 @@
568 +/*
569 + * RTSP extension for TCP NAT alteration
570 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
571 + * based on ip_nat_irc.c
572 + *
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.
577 + *
578 + * Module load syntax:
579 + * insmod ip_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
580 + * stunaddr=<address>
581 + * destaction=[auto|strip|none]
582 + *
583 + * If no ports are specified, the default will be port 554 only.
584 + *
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.
593 + *
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).
598 + */
599 +
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>
611 +
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>
619 +
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)
623 +#else
624 +#define DEBUGP(fmt, args...)
625 +#endif
626 +
627 +#define MAX_PORTS 8
628 +#define DSTACT_AUTO 0
629 +#define DSTACT_STRIP 1
630 +#define DSTACT_NONE 2
631 +
632 +static int ports[MAX_PORTS];
633 +static char* stunaddr = NULL;
634 +static char* destaction = NULL;
635 +
636 +static int num_ports = 0;
637 +static u_int32_t extip = 0;
638 +static int dstact = 0;
639 +
640 +MODULE_AUTHOR("Tom Marshall <tmarshall@real.com>");
641 +MODULE_DESCRIPTION("RTSP network address translation module");
642 +MODULE_LICENSE("GPL");
643 +#ifdef MODULE_PARM
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)");
650 +#endif
651 +
652 +/* protects rtsp part of conntracks */
653 +DECLARE_LOCK_EXTERN(ip_rtsp_lock);
654 +
655 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
656 +
657 +/*** helper functions ***/
658 +
659 +static void
660 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
661 +{
662 + struct iphdr* iph = (struct iphdr*)skb->nh.iph;
663 + struct tcphdr* tcph = (struct tcphdr*)((char*)iph + iph->ihl*4);
664 +
665 + *pptcpdata = (char*)tcph + tcph->doff*4;
666 + *ptcpdatalen = ((char*)skb->h.raw + skb->len) - *pptcpdata;
667 +}
668 +
669 +/*** nat functions ***/
670 +
671 +/*
672 + * Mangle the "Transport:" header:
673 + * - Replace all occurences of "client_port=<spec>"
674 + * - Handle destination parameter
675 + *
676 + * In:
677 + * ct, ctinfo = conntrack context
678 + * pskb = packet
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)
683 + *
684 + * Returns packet size difference.
685 + *
686 + * Assumes that a complete transport header is present, ending with CR or LF
687 + */
688 +static int
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)
692 +{
693 + char* ptcp;
694 + uint tcplen;
695 + char* ptran;
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) */
700 + u_int32_t newip;
701 + u_int16_t loport, hiport;
702 + uint off = 0;
703 + uint diff; /* Number of bytes we removed */
704 +
705 + struct ip_ct_rtsp_expect* prtspexp = &exp->help.exp_rtsp_info;
706 + struct ip_conntrack_tuple t;
707 +
708 + char szextaddr[15+1];
709 + uint extaddrlen;
710 + int is_stun;
711 +
712 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
713 + ptran = ptcp+tranoff;
714 +
715 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
716 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
717 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
718 + {
719 + INFOP("sanity check failed\n");
720 + return 0;
721 + }
722 + off += 10;
723 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
724 +
725 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
726 + t = exp->tuple;
727 + t.dst.ip = newip;
728 +
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"));
732 +
733 + rbuf1len = rbufalen = 0;
734 + switch (prtspexp->pbtype)
735 + {
736 + case pb_single:
737 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
738 + {
739 + t.dst.u.udp.port = htons(loport);
740 + if (ip_conntrack_change_expect(exp, &t) == 0)
741 + {
742 + DEBUGP("using port %hu\n", loport);
743 + break;
744 + }
745 + }
746 + if (loport != 0)
747 + {
748 + rbuf1len = sprintf(rbuf1, "%hu", loport);
749 + rbufalen = sprintf(rbufa, "%hu", loport);
750 + }
751 + break;
752 + case pb_range:
753 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
754 + {
755 + t.dst.u.udp.port = htons(loport);
756 + if (ip_conntrack_change_expect(exp, &t) == 0)
757 + {
758 + hiport = loport + ~exp->mask.dst.u.udp.port;
759 + DEBUGP("using ports %hu-%hu\n", loport, hiport);
760 + break;
761 + }
762 + }
763 + if (loport != 0)
764 + {
765 + rbuf1len = sprintf(rbuf1, "%hu", loport);
766 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
767 + }
768 + break;
769 + case pb_discon:
770 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
771 + {
772 + t.dst.u.udp.port = htons(loport);
773 + if (ip_conntrack_change_expect(exp, &t) == 0)
774 + {
775 + DEBUGP("using port %hu (1 of 2)\n", loport);
776 + break;
777 + }
778 + }
779 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
780 + {
781 + t.dst.u.udp.port = htons(hiport);
782 + if (ip_conntrack_change_expect(exp, &t) == 0)
783 + {
784 + DEBUGP("using port %hu (2 of 2)\n", hiport);
785 + break;
786 + }
787 + }
788 + if (loport != 0 && hiport != 0)
789 + {
790 + rbuf1len = sprintf(rbuf1, "%hu", loport);
791 + if (hiport == loport+1)
792 + {
793 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
794 + }
795 + else
796 + {
797 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
798 + }
799 + }
800 + break;
801 + }
802 +
803 + if (rbuf1len == 0)
804 + {
805 + return 0; /* cannot get replacement port(s) */
806 + }
807 +
808 + /* Transport: tran;field;field=val,tran;field;field=val,... */
809 + while (off < tranlen)
810 + {
811 + uint saveoff;
812 + const char* pparamend;
813 + uint nextparamoff;
814 +
815 + pparamend = memchr(ptran+off, ',', tranlen-off);
816 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
817 + nextparamoff = pparamend-ptcp;
818 +
819 + /*
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.
824 + */
825 + is_stun = 0;
826 + saveoff = off;
827 + while (off < nextparamoff)
828 + {
829 + const char* pfieldend;
830 + uint nextfieldoff;
831 +
832 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
833 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
834 +
835 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
836 + {
837 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
838 + {
839 + is_stun = 1;
840 + }
841 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
842 + {
843 + diff = nextfieldoff-off;
844 + if (!ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
845 + off, diff, NULL, 0))
846 + {
847 + /* mangle failed, all we can do is bail */
848 + return 0;
849 + }
850 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
851 + ptran = ptcp+tranoff;
852 + tranlen -= diff;
853 + nextparamoff -= diff;
854 + nextfieldoff -= diff;
855 + }
856 + }
857 +
858 + off = nextfieldoff;
859 + }
860 + if (is_stun)
861 + {
862 + continue;
863 + }
864 + off = saveoff;
865 + while (off < nextparamoff)
866 + {
867 + const char* pfieldend;
868 + uint nextfieldoff;
869 +
870 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
871 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
872 +
873 + if (strncmp(ptran+off, "client_port=", 12) == 0)
874 + {
875 + u_int16_t port;
876 + uint numlen;
877 + uint origoff;
878 + uint origlen;
879 + char* rbuf = rbuf1;
880 + uint rbuflen = rbuf1len;
881 +
882 + off += 12;
883 + origoff = (ptran-ptcp)+off;
884 + origlen = 0;
885 + numlen = nf_strtou16(ptran+off, &port);
886 + off += numlen;
887 + origlen += numlen;
888 + if (port != prtspexp->loport)
889 + {
890 + DEBUGP("multiple ports found, port %hu ignored\n", port);
891 + }
892 + else
893 + {
894 + if (ptran[off] == '-' || ptran[off] == '/')
895 + {
896 + off++;
897 + origlen++;
898 + numlen = nf_strtou16(ptran+off, &port);
899 + off += numlen;
900 + origlen += numlen;
901 + rbuf = rbufa;
902 + rbuflen = rbufalen;
903 + }
904 +
905 + /*
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.
909 + *
910 + * parameter 4 below is offset from start of tcp data.
911 + */
912 + diff = origlen-rbuflen;
913 + if (!ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
914 + origoff, origlen, rbuf, rbuflen))
915 + {
916 + /* mangle failed, all we can do is bail */
917 + return 0;
918 + }
919 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
920 + ptran = ptcp+tranoff;
921 + tranlen -= diff;
922 + nextparamoff -= diff;
923 + nextfieldoff -= diff;
924 + }
925 + }
926 +
927 + off = nextfieldoff;
928 + }
929 +
930 + off = nextparamoff;
931 + }
932 +
933 + return 1;
934 +}
935 +
936 +static unsigned int
937 +expected(struct sk_buff **pskb, uint hooknum, struct ip_conntrack* ct, struct ip_nat_info* info)
938 +{
939 + struct ip_nat_multi_range mr;
940 + u_int32_t newdstip, newsrcip, newip;
941 +
942 + struct ip_conntrack *master = master_ct(ct);
943 +
944 + IP_NF_ASSERT(info);
945 + IP_NF_ASSERT(master);
946 +
947 + IP_NF_ASSERT(!(info->initialized & (1 << HOOK2MANIP(hooknum))));
948 +
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;
952 +
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));
955 +
956 + mr.rangesize = 1;
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;
960 +
961 + return ip_nat_setup_info(ct, &mr, hooknum);
962 +}
963 +
964 +static uint
965 +help_out(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
966 + struct ip_conntrack_expect* exp, struct sk_buff** pskb)
967 +{
968 + char* ptcp;
969 + uint tcplen;
970 + uint hdrsoff;
971 + uint hdrslen;
972 + uint lineoff;
973 + uint linelen;
974 + uint off;
975 +
976 + struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
977 + struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
978 +
979 + struct ip_ct_rtsp_expect* prtspexp = &exp->help.exp_rtsp_info;
980 +
981 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
982 +
983 + hdrsoff = exp->seq - ntohl(tcph->seq);
984 + hdrslen = prtspexp->len;
985 + off = hdrsoff;
986 +
987 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
988 + {
989 + if (linelen == 0)
990 + {
991 + break;
992 + }
993 + if (off > hdrsoff+hdrslen)
994 + {
995 + INFOP("!! overrun !!");
996 + break;
997 + }
998 + DEBUGP("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
999 +
1000 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
1001 + {
1002 + uint oldtcplen = tcplen;
1003 + if (!rtsp_mangle_tran(ct, ctinfo, exp, pskb, lineoff, linelen))
1004 + {
1005 + break;
1006 + }
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);
1013 + }
1014 + }
1015 +
1016 + return NF_ACCEPT;
1017 +}
1018 +
1019 +static uint
1020 +help_in(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
1021 + struct ip_conntrack_expect* exp, struct sk_buff** pskb)
1022 +{
1023 + /* XXX: unmangle */
1024 + return NF_ACCEPT;
1025 +}
1026 +
1027 +static uint
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)
1034 +{
1035 + struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
1036 + struct tcphdr* tcph = (struct tcphdr*)((char*)iph + iph->ihl * 4);
1037 + uint datalen;
1038 + int dir;
1039 + struct ip_ct_rtsp_expect* ct_rtsp_info;
1040 + int rc = NF_ACCEPT;
1041 +
1042 + if (ct == NULL || exp == NULL || info == NULL || pskb == NULL)
1043 + {
1044 + DEBUGP("!! null ptr (%p,%p,%p,%p) !!\n", ct, exp, info, pskb);
1045 + return NF_ACCEPT;
1046 + }
1047 +
1048 + ct_rtsp_info = &exp->help.exp_rtsp_info;
1049 +
1050 + /*
1051 + * Only mangle things once: original direction in POST_ROUTING
1052 + * and reply direction on PRE_ROUTING.
1053 + */
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)))
1057 + {
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" : "???");
1063 + return NF_ACCEPT;
1064 + }
1065 + DEBUGP("got beyond not touching\n");
1066 +
1067 + datalen = (*pskb)->len - iph->ihl * 4 - tcph->doff * 4;
1068 +
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))
1073 + {
1074 + /* Partial retransmission? Probably a hacker. */
1075 + if (net_ratelimit())
1076 + {
1077 + INFOP("partial packet %u/%u in %u/%u\n",
1078 + exp->seq, ct_rtsp_info->len, ntohl(tcph->seq), ntohl(tcph->seq) + datalen);
1079 + }
1080 + UNLOCK_BH(&ip_rtsp_lock);
1081 + return NF_DROP;
1082 + }
1083 +
1084 + switch (dir)
1085 + {
1086 + case IP_CT_DIR_ORIGINAL:
1087 + rc = help_out(ct, ctinfo, exp, pskb);
1088 + break;
1089 + case IP_CT_DIR_REPLY:
1090 + rc = help_in(ct, ctinfo, exp, pskb);
1091 + break;
1092 + }
1093 + UNLOCK_BH(&ip_rtsp_lock);
1094 +
1095 + return rc;
1096 +}
1097 +
1098 +static struct ip_nat_helper ip_nat_rtsp_helpers[MAX_PORTS];
1099 +static char rtsp_names[MAX_PORTS][10];
1100 +
1101 +/* This function is intentionally _NOT_ defined as __exit */
1102 +static void
1103 +fini(void)
1104 +{
1105 + int i;
1106 +
1107 + for (i = 0; i < num_ports; i++)
1108 + {
1109 + DEBUGP("unregistering helper for port %d\n", ports[i]);
1110 + ip_nat_helper_unregister(&ip_nat_rtsp_helpers[i]);
1111 + }
1112 +}
1113 +
1114 +static int __init
1115 +init(void)
1116 +{
1117 + int ret = 0;
1118 + int i;
1119 + struct ip_nat_helper* hlpr;
1120 + char* tmpname;
1121 +
1122 + printk("ip_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1123 +
1124 + if (ports[0] == 0)
1125 + {
1126 + ports[0] = RTSP_PORT;
1127 + }
1128 +
1129 + for (i = 0; (i < MAX_PORTS) && ports[i] != 0; i++)
1130 + {
1131 + hlpr = &ip_nat_rtsp_helpers[i];
1132 + memset(hlpr, 0, sizeof(struct ip_nat_helper));
1133 +
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;
1139 + hlpr->flags = 0;
1140 + hlpr->me = THIS_MODULE;
1141 + hlpr->expect = expected;
1142 +
1143 + tmpname = &rtsp_names[i][0];
1144 + if (ports[i] == RTSP_PORT)
1145 + {
1146 + sprintf(tmpname, "rtsp");
1147 + }
1148 + else
1149 + {
1150 + sprintf(tmpname, "rtsp-%d", i);
1151 + }
1152 + hlpr->name = tmpname;
1153 +
1154 + DEBUGP("registering helper for port %d: name %s\n", ports[i], hlpr->name);
1155 + ret = ip_nat_helper_register(hlpr);
1156 +
1157 + if (ret)
1158 + {
1159 + printk("ip_nat_rtsp: error registering helper for port %d\n", ports[i]);
1160 + fini();
1161 + return 1;
1162 + }
1163 + num_ports++;
1164 + }
1165 + if (stunaddr != NULL)
1166 + {
1167 + extip = in_aton(stunaddr);
1168 + }
1169 + if (destaction != NULL)
1170 + {
1171 + if (strcmp(destaction, "auto") == 0)
1172 + {
1173 + dstact = DSTACT_AUTO;
1174 + }
1175 + if (strcmp(destaction, "strip") == 0)
1176 + {
1177 + dstact = DSTACT_STRIP;
1178 + }
1179 + if (strcmp(destaction, "none") == 0)
1180 + {
1181 + dstact = DSTACT_NONE;
1182 + }
1183 + }
1184 + return ret;
1185 +}
1186 +
1187 +module_init(init);
1188 +module_exit(fini);
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);
1194 /*
1195 * String functions
1196 */
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 ===================================================================
1203 --- /dev/null
1204 +++ linux-2.4.35.4/include/linux/netfilter_helpers.h
1205 @@ -0,0 +1,133 @@
1206 +/*
1207 + * Helpers for netfiler modules. This file provides implementations for basic
1208 + * functions such as strncasecmp(), etc.
1209 + *
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()
1215 + */
1216 +#ifndef _NETFILTER_HELPERS_H
1217 +#define _NETFILTER_HELPERS_H
1218 +
1219 +/* Only include these functions for kernel code. */
1220 +#ifdef __KERNEL__
1221 +
1222 +#include <linux/ctype.h>
1223 +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
1224 +
1225 +/*
1226 + * The standard strncasecmp()
1227 + */
1228 +#ifdef NF_NEED_STRNCASECMP
1229 +static int
1230 +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
1231 +{
1232 + if (s1 == NULL || s2 == NULL)
1233 + {
1234 + if (s1 == NULL && s2 == NULL)
1235 + {
1236 + return 0;
1237 + }
1238 + return (s1 == NULL) ? -1 : 1;
1239 + }
1240 + while (len > 0 && tolower(*s1) == tolower(*s2))
1241 + {
1242 + len--;
1243 + s1++;
1244 + s2++;
1245 + }
1246 + return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
1247 +}
1248 +#endif /* NF_NEED_STRNCASECMP */
1249 +
1250 +/*
1251 + * Parse a string containing a 16-bit unsigned integer.
1252 + * Returns the number of chars used, or zero if no number is found.
1253 + */
1254 +#ifdef NF_NEED_STRTOU16
1255 +static int
1256 +nf_strtou16(const char* pbuf, u_int16_t* pval)
1257 +{
1258 + int n = 0;
1259 +
1260 + *pval = 0;
1261 + while (isdigit(pbuf[n]))
1262 + {
1263 + *pval = (*pval * 10) + (pbuf[n] - '0');
1264 + n++;
1265 + }
1266 +
1267 + return n;
1268 +}
1269 +#endif /* NF_NEED_STRTOU16 */
1270 +
1271 +/*
1272 + * Parse a string containing a 32-bit unsigned integer.
1273 + * Returns the number of chars used, or zero if no number is found.
1274 + */
1275 +#ifdef NF_NEED_STRTOU32
1276 +static int
1277 +nf_strtou32(const char* pbuf, u_int32_t* pval)
1278 +{
1279 + int n = 0;
1280 +
1281 + *pval = 0;
1282 + while (pbuf[n] >= '0' && pbuf[n] <= '9')
1283 + {
1284 + *pval = (*pval * 10) + (pbuf[n] - '0');
1285 + n++;
1286 + }
1287 +
1288 + return n;
1289 +}
1290 +#endif /* NF_NEED_STRTOU32 */
1291 +
1292 +/*
1293 + * Given a buffer and length, advance to the next line and mark the current
1294 + * line.
1295 + */
1296 +#ifdef NF_NEED_NEXTLINE
1297 +static int
1298 +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
1299 +{
1300 + uint off = *poff;
1301 + uint physlen = 0;
1302 +
1303 + if (off >= len)
1304 + {
1305 + return 0;
1306 + }
1307 +
1308 + while (p[off] != '\n')
1309 + {
1310 + if (len-off <= 1)
1311 + {
1312 + return 0;
1313 + }
1314 +
1315 + physlen++;
1316 + off++;
1317 + }
1318 +
1319 + /* if we saw a crlf, physlen needs adjusted */
1320 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
1321 + {
1322 + physlen--;
1323 + }
1324 +
1325 + /* advance past the newline */
1326 + off++;
1327 +
1328 + *plineoff = *poff;
1329 + *plinelen = physlen;
1330 + *poff = off;
1331 +
1332 + return 1;
1333 +}
1334 +#endif /* NF_NEED_NEXTLINE */
1335 +
1336 +#endif /* __KERNEL__ */
1337 +
1338 +#endif /* _NETFILTER_HELPERS_H */
1339 Index: linux-2.4.35.4/include/linux/netfilter_ipv4/ip_conntrack_rtsp.h
1340 ===================================================================
1341 --- /dev/null
1342 +++ linux-2.4.35.4/include/linux/netfilter_ipv4/ip_conntrack_rtsp.h
1343 @@ -0,0 +1,68 @@
1344 +/*
1345 + * RTSP extension for IP connection tracking.
1346 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
1347 + * based on ip_conntrack_irc.h
1348 + *
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.
1353 + */
1354 +#ifndef _IP_CONNTRACK_RTSP_H
1355 +#define _IP_CONNTRACK_RTSP_H
1356 +
1357 +/* #define IP_NF_RTSP_DEBUG */
1358 +#define IP_NF_RTSP_VERSION "0.01"
1359 +
1360 +/* port block types */
1361 +typedef enum {
1362 + pb_single, /* client_port=x */
1363 + pb_range, /* client_port=x-y */
1364 + pb_discon /* client_port=x/y (rtspbis) */
1365 +} portblock_t;
1366 +
1367 +/* We record seq number and length of rtsp headers here, all in host order. */
1368 +
1369 +/*
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.
1375 + *
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.
1380 + */
1381 +struct ip_ct_rtsp_expect
1382 +{
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 */
1387 +#if 0
1388 + uint method; /* RTSP method */
1389 + uint cseq; /* CSeq from request */
1390 +#endif
1391 +};
1392 +
1393 +/* This structure exists only once per master */
1394 +struct ip_ct_rtsp_master
1395 +{
1396 + /* Empty (?) */
1397 +};
1398 +
1399 +
1400 +#ifdef __KERNEL__
1401 +
1402 +#include <linux/netfilter_ipv4/lockhelp.h>
1403 +
1404 +#define RTSP_PORT 554
1405 +
1406 +/* Protects rtsp part of conntracks */
1407 +DECLARE_LOCK_EXTERN(ip_rtsp_lock);
1408 +
1409 +#endif /* __KERNEL__ */
1410 +
1411 +#endif /* _IP_CONNTRACK_RTSP_H */
1412 Index: linux-2.4.35.4/include/linux/netfilter_mime.h
1413 ===================================================================
1414 --- /dev/null
1415 +++ linux-2.4.35.4/include/linux/netfilter_mime.h
1416 @@ -0,0 +1,90 @@
1417 +/*
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.
1421 + *
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()
1425 + */
1426 +#ifndef _NETFILTER_MIME_H
1427 +#define _NETFILTER_MIME_H
1428 +
1429 +/* Only include these functions for kernel code. */
1430 +#ifdef __KERNEL__
1431 +
1432 +#include <linux/kernel.h>
1433 +#include <linux/ctype.h>
1434 +
1435 +/*
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).
1439 + *
1440 + * 'line' in this context means logical line (includes LWS continuations).
1441 + * Returns 1 on success, 0 on failure.
1442 + */
1443 +#ifdef NF_NEED_MIME_NEXTLINE
1444 +static int
1445 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
1446 +{
1447 + uint off = *poff;
1448 + uint physlen = 0;
1449 + int is_first_line = 1;
1450 +
1451 + if (off >= len)
1452 + {
1453 + return 0;
1454 + }
1455 +
1456 + do
1457 + {
1458 + while (p[off] != '\n')
1459 + {
1460 + if (len-off <= 1)
1461 + {
1462 + return 0;
1463 + }
1464 +
1465 + physlen++;
1466 + off++;
1467 + }
1468 +
1469 + /* if we saw a crlf, physlen needs adjusted */
1470 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
1471 + {
1472 + physlen--;
1473 + }
1474 +
1475 + /* advance past the newline */
1476 + off++;
1477 +
1478 + /* check for an empty line */
1479 + if (physlen == 0)
1480 + {
1481 + break;
1482 + }
1483 +
1484 + /* check for colon on the first physical line */
1485 + if (is_first_line)
1486 + {
1487 + is_first_line = 0;
1488 + if (memchr(p+(*poff), ':', physlen) == NULL)
1489 + {
1490 + return 0;
1491 + }
1492 + }
1493 + }
1494 + while (p[off] == ' ' || p[off] == '\t');
1495 +
1496 + *plineoff = *poff;
1497 + *plinelen = (physlen == 0) ? 0 : (off - *poff);
1498 + *poff = off;
1499 +
1500 + return 1;
1501 +}
1502 +#endif /* NF_NEED_MIME_NEXTLINE */
1503 +
1504 +#endif /* __KERNEL__ */
1505 +
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>
1516
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;
1524
1525 #ifdef CONFIG_IP_NF_NAT_NEEDED
1526 union {
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;
1532 };
1533
1534 #ifdef CONFIG_IP_NF_NAT_NEEDED