finally move buildroot-ng to trunk
[openwrt/staging/chunkeey.git] / target / linux / generic-2.4 / patches / 614-netfilter_nat_rtsp.patch
1 diff -urN linux-2.4.32/net/ipv4/netfilter/Config.in linux-2.4.32.new/net/ipv4/netfilter/Config.in
2 --- linux-2.4.32/net/ipv4/netfilter/Config.in 2006-03-01 00:53:57.884792456 +0100
3 +++ linux-2.4.32.new/net/ipv4/netfilter/Config.in 2006-03-01 00:55:14.462150928 +0100
4 @@ -16,6 +16,7 @@
5 dep_tristate ' GRE protocol support' CONFIG_IP_NF_CT_PROTO_GRE $CONFIG_IP_NF_CONNTRACK
6 dep_tristate ' PPTP protocol support' CONFIG_IP_NF_PPTP $CONFIG_IP_NF_CT_PROTO_GRE
7 dep_tristate ' H.323 (netmeeting) support' CONFIG_IP_NF_H323 $CONFIG_IP_NF_CONNTRACK
8 + dep_tristate ' RTSP protocol support' CONFIG_IP_NF_RTSP $CONFIG_IP_NF_CONNTRACK
9 fi
10
11 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
12 @@ -119,6 +120,13 @@
13 define_tristate CONFIG_IP_NF_NAT_H323 $CONFIG_IP_NF_NAT
14 fi
15 fi
16 + if [ "$CONFIG_IP_NF_RTSP" = "m" ]; then
17 + define_tristate CONFIG_IP_NF_NAT_RTSP m
18 + else
19 + if [ "$CONFIG_IP_NF_RTSP" = "y" ]; then
20 + define_tristate CONFIG_IP_NF_NAT_RTSP $CONFIG_IP_NF_NAT
21 + fi
22 + fi
23 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
24 dep_tristate ' Basic SNMP-ALG support (EXPERIMENTAL)' CONFIG_IP_NF_NAT_SNMP_BASIC $CONFIG_IP_NF_NAT
25 fi
26 diff -urN linux-2.4.32/net/ipv4/netfilter/Makefile linux-2.4.32.new/net/ipv4/netfilter/Makefile
27 --- linux-2.4.32/net/ipv4/netfilter/Makefile 2006-03-01 00:53:57.886792152 +0100
28 +++ linux-2.4.32.new/net/ipv4/netfilter/Makefile 2006-03-01 00:55:14.463150776 +0100
29 @@ -57,6 +57,11 @@
30 ifdef CONFIG_IP_NF_NAT_H323
31 export-objs += ip_conntrack_h323.o
32 endif
33 +obj-$(CONFIG_IP_NF_RTSP) += ip_conntrack_rtsp.o
34 +ifdef CONFIG_IP_NF_NAT_RTSP
35 + export-objs += ip_conntrack_rtsp.o
36 +endif
37 +
38
39
40 # NAT helpers
41 @@ -67,6 +72,7 @@
42 obj-$(CONFIG_IP_NF_NAT_PROTO_GRE) += ip_nat_proto_gre.o
43 obj-$(CONFIG_IP_NF_NAT_PPTP) += ip_nat_pptp.o
44 obj-$(CONFIG_IP_NF_NAT_H323) += ip_nat_h323.o
45 +obj-$(CONFIG_IP_NF_NAT_RTSP) += ip_nat_rtsp.o
46
47 # generic IP tables
48 obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o
49 diff -urN linux-2.4.32/net/ipv4/netfilter/ip_conntrack_rtsp.c linux-2.4.32.new/net/ipv4/netfilter/ip_conntrack_rtsp.c
50 --- linux-2.4.32/net/ipv4/netfilter/ip_conntrack_rtsp.c 1970-01-01 01:00:00.000000000 +0100
51 +++ linux-2.4.32.new/net/ipv4/netfilter/ip_conntrack_rtsp.c 2006-03-01 00:55:14.465150472 +0100
52 @@ -0,0 +1,507 @@
53 +/*
54 + * RTSP extension for IP connection tracking
55 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
56 + * based on ip_conntrack_irc.c
57 + *
58 + * This program is free software; you can redistribute it and/or
59 + * modify it under the terms of the GNU General Public License
60 + * as published by the Free Software Foundation; either version
61 + * 2 of the License, or (at your option) any later version.
62 + *
63 + * Module load syntax:
64 + * insmod ip_conntrack_rtsp.o ports=port1,port2,...port<MAX_PORTS>
65 + * max_outstanding=n setup_timeout=secs
66 + *
67 + * If no ports are specified, the default will be port 554.
68 + *
69 + * With max_outstanding you can define the maximum number of not yet
70 + * answered SETUP requests per RTSP session (default 8).
71 + * With setup_timeout you can specify how long the system waits for
72 + * an expected data channel (default 300 seconds).
73 + */
74 +
75 +#include <linux/config.h>
76 +#include <linux/module.h>
77 +#include <linux/netfilter.h>
78 +#include <linux/ip.h>
79 +#include <net/checksum.h>
80 +#include <net/tcp.h>
81 +
82 +#include <linux/netfilter_ipv4/lockhelp.h>
83 +#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
84 +#include <linux/netfilter_ipv4/ip_conntrack_rtsp.h>
85 +
86 +#include <linux/ctype.h>
87 +#define NF_NEED_STRNCASECMP
88 +#define NF_NEED_STRTOU16
89 +#define NF_NEED_STRTOU32
90 +#define NF_NEED_NEXTLINE
91 +#include <linux/netfilter_helpers.h>
92 +#define NF_NEED_MIME_NEXTLINE
93 +#include <linux/netfilter_mime.h>
94 +
95 +#define MAX_SIMUL_SETUP 8 /* XXX: use max_outstanding */
96 +
97 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
98 +#ifdef IP_NF_RTSP_DEBUG
99 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
100 +#else
101 +#define DEBUGP(fmt, args...)
102 +#endif
103 +
104 +#define MAX_PORTS 8
105 +static int ports[MAX_PORTS];
106 +static int num_ports = 0;
107 +static int max_outstanding = 8;
108 +static unsigned int setup_timeout = 300;
109 +
110 +MODULE_AUTHOR("Tom Marshall <tmarshall@real.com>");
111 +MODULE_DESCRIPTION("RTSP connection tracking module");
112 +MODULE_LICENSE("GPL");
113 +#ifdef MODULE_PARM
114 +MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
115 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
116 +MODULE_PARM(max_outstanding, "i");
117 +MODULE_PARM_DESC(max_outstanding, "max number of outstanding SETUP requests per RTSP session");
118 +MODULE_PARM(setup_timeout, "i");
119 +MODULE_PARM_DESC(setup_timeout, "timeout on for unestablished data channels");
120 +#endif
121 +
122 +DECLARE_LOCK(ip_rtsp_lock);
123 +struct module* ip_conntrack_rtsp = THIS_MODULE;
124 +
125 +/*
126 + * Max mappings we will allow for one RTSP connection (for RTP, the number
127 + * of allocated ports is twice this value). Note that SMIL burns a lot of
128 + * ports so keep this reasonably high. If this is too low, you will see a
129 + * lot of "no free client map entries" messages.
130 + */
131 +#define MAX_PORT_MAPS 16
132 +
133 +/*** default port list was here in the masq code: 554, 3030, 4040 ***/
134 +
135 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
136 +
137 +/*
138 + * Parse an RTSP packet.
139 + *
140 + * Returns zero if parsing failed.
141 + *
142 + * Parameters:
143 + * IN ptcp tcp data pointer
144 + * IN tcplen tcp data len
145 + * IN/OUT ptcpoff points to current tcp offset
146 + * OUT phdrsoff set to offset of rtsp headers
147 + * OUT phdrslen set to length of rtsp headers
148 + * OUT pcseqoff set to offset of CSeq header
149 + * OUT pcseqlen set to length of CSeq header
150 + */
151 +static int
152 +rtsp_parse_message(char* ptcp, uint tcplen, uint* ptcpoff,
153 + uint* phdrsoff, uint* phdrslen,
154 + uint* pcseqoff, uint* pcseqlen)
155 +{
156 + uint entitylen = 0;
157 + uint lineoff;
158 + uint linelen;
159 +
160 + if (!nf_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
161 + {
162 + return 0;
163 + }
164 +
165 + *phdrsoff = *ptcpoff;
166 + while (nf_mime_nextline(ptcp, tcplen, ptcpoff, &lineoff, &linelen))
167 + {
168 + if (linelen == 0)
169 + {
170 + if (entitylen > 0)
171 + {
172 + *ptcpoff += min(entitylen, tcplen - *ptcpoff);
173 + }
174 + break;
175 + }
176 + if (lineoff+linelen > tcplen)
177 + {
178 + INFOP("!! overrun !!\n");
179 + break;
180 + }
181 +
182 + if (nf_strncasecmp(ptcp+lineoff, "CSeq:", 5) == 0)
183 + {
184 + *pcseqoff = lineoff;
185 + *pcseqlen = linelen;
186 + }
187 + if (nf_strncasecmp(ptcp+lineoff, "Content-Length:", 15) == 0)
188 + {
189 + uint off = lineoff+15;
190 + SKIP_WSPACE(ptcp+lineoff, linelen, off);
191 + nf_strtou32(ptcp+off, &entitylen);
192 + }
193 + }
194 + *phdrslen = (*ptcpoff) - (*phdrsoff);
195 +
196 + return 1;
197 +}
198 +
199 +/*
200 + * Find lo/hi client ports (if any) in transport header
201 + * In:
202 + * ptcp, tcplen = packet
203 + * tranoff, tranlen = buffer to search
204 + *
205 + * Out:
206 + * pport_lo, pport_hi = lo/hi ports (host endian)
207 + *
208 + * Returns nonzero if any client ports found
209 + *
210 + * Note: it is valid (and expected) for the client to request multiple
211 + * transports, so we need to parse the entire line.
212 + */
213 +static int
214 +rtsp_parse_transport(char* ptran, uint tranlen,
215 + struct ip_ct_rtsp_expect* prtspexp)
216 +{
217 + int rc = 0;
218 + uint off = 0;
219 +
220 + if (tranlen < 10 || !iseol(ptran[tranlen-1]) ||
221 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
222 + {
223 + INFOP("sanity check failed\n");
224 + return 0;
225 + }
226 + DEBUGP("tran='%.*s'\n", (int)tranlen, ptran);
227 + off += 10;
228 + SKIP_WSPACE(ptran, tranlen, off);
229 +
230 + /* Transport: tran;field;field=val,tran;field;field=val,... */
231 + while (off < tranlen)
232 + {
233 + const char* pparamend;
234 + uint nextparamoff;
235 +
236 + pparamend = memchr(ptran+off, ',', tranlen-off);
237 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
238 + nextparamoff = pparamend-ptran;
239 +
240 + while (off < nextparamoff)
241 + {
242 + const char* pfieldend;
243 + uint nextfieldoff;
244 +
245 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
246 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
247 +
248 + if (strncmp(ptran+off, "client_port=", 12) == 0)
249 + {
250 + u_int16_t port;
251 + uint numlen;
252 +
253 + off += 12;
254 + numlen = nf_strtou16(ptran+off, &port);
255 + off += numlen;
256 + if (prtspexp->loport != 0 && prtspexp->loport != port)
257 + {
258 + DEBUGP("multiple ports found, port %hu ignored\n", port);
259 + }
260 + else
261 + {
262 + prtspexp->loport = prtspexp->hiport = port;
263 + if (ptran[off] == '-')
264 + {
265 + off++;
266 + numlen = nf_strtou16(ptran+off, &port);
267 + off += numlen;
268 + prtspexp->pbtype = pb_range;
269 + prtspexp->hiport = port;
270 +
271 + // If we have a range, assume rtp:
272 + // loport must be even, hiport must be loport+1
273 + if ((prtspexp->loport & 0x0001) != 0 ||
274 + prtspexp->hiport != prtspexp->loport+1)
275 + {
276 + DEBUGP("incorrect range: %hu-%hu, correcting\n",
277 + prtspexp->loport, prtspexp->hiport);
278 + prtspexp->loport &= 0xfffe;
279 + prtspexp->hiport = prtspexp->loport+1;
280 + }
281 + }
282 + else if (ptran[off] == '/')
283 + {
284 + off++;
285 + numlen = nf_strtou16(ptran+off, &port);
286 + off += numlen;
287 + prtspexp->pbtype = pb_discon;
288 + prtspexp->hiport = port;
289 + }
290 + rc = 1;
291 + }
292 + }
293 +
294 + /*
295 + * Note we don't look for the destination parameter here.
296 + * If we are using NAT, the NAT module will handle it. If not,
297 + * and the client is sending packets elsewhere, the expectation
298 + * will quietly time out.
299 + */
300 +
301 + off = nextfieldoff;
302 + }
303 +
304 + off = nextparamoff;
305 + }
306 +
307 + return rc;
308 +}
309 +
310 +/*** conntrack functions ***/
311 +
312 +/* outbound packet: client->server */
313 +static int
314 +help_out(const struct iphdr* iph, size_t pktlen,
315 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
316 +{
317 + int dir = CTINFO2DIR(ctinfo); /* = IP_CT_DIR_ORIGINAL */
318 + struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
319 + uint tcplen = pktlen - iph->ihl * 4;
320 + char* pdata = (char*)tcph + tcph->doff * 4;
321 + uint datalen = tcplen - tcph->doff * 4;
322 + uint dataoff = 0;
323 +
324 + struct ip_conntrack_expect exp;
325 +
326 + while (dataoff < datalen)
327 + {
328 + uint cmdoff = dataoff;
329 + uint hdrsoff = 0;
330 + uint hdrslen = 0;
331 + uint cseqoff = 0;
332 + uint cseqlen = 0;
333 + uint lineoff = 0;
334 + uint linelen = 0;
335 + uint off;
336 + int rc;
337 +
338 + if (!rtsp_parse_message(pdata, datalen, &dataoff,
339 + &hdrsoff, &hdrslen,
340 + &cseqoff, &cseqlen))
341 + {
342 + break; /* not a valid message */
343 + }
344 +
345 + if (strncmp(pdata+cmdoff, "SETUP ", 6) != 0)
346 + {
347 + continue; /* not a SETUP message */
348 + }
349 + DEBUGP("found a setup message\n");
350 +
351 + memset(&exp, 0, sizeof(exp));
352 +
353 + off = 0;
354 + while (nf_mime_nextline(pdata+hdrsoff, hdrslen, &off,
355 + &lineoff, &linelen))
356 + {
357 + if (linelen == 0)
358 + {
359 + break;
360 + }
361 + if (off > hdrsoff+hdrslen)
362 + {
363 + INFOP("!! overrun !!");
364 + break;
365 + }
366 +
367 + if (nf_strncasecmp(pdata+hdrsoff+lineoff, "Transport:", 10) == 0)
368 + {
369 + rtsp_parse_transport(pdata+hdrsoff+lineoff, linelen,
370 + &exp.help.exp_rtsp_info);
371 + }
372 + }
373 +
374 + if (exp.help.exp_rtsp_info.loport == 0)
375 + {
376 + DEBUGP("no udp transports found\n");
377 + continue; /* no udp transports found */
378 + }
379 +
380 + DEBUGP("udp transport found, ports=(%d,%hu,%hu)\n",
381 + (int)exp.help.exp_rtsp_info.pbtype,
382 + exp.help.exp_rtsp_info.loport,
383 + exp.help.exp_rtsp_info.hiport);
384 +
385 + LOCK_BH(&ip_rtsp_lock);
386 + exp.seq = ntohl(tcph->seq) + hdrsoff; /* mark all the headers */
387 + exp.help.exp_rtsp_info.len = hdrslen;
388 +
389 + exp.tuple.src.ip = ct->tuplehash[!dir].tuple.src.ip;
390 + exp.mask.src.ip = 0xffffffff;
391 + exp.tuple.dst.ip = ct->tuplehash[dir].tuple.src.ip;
392 + exp.mask.dst.ip = 0xffffffff;
393 + exp.tuple.dst.u.udp.port = exp.help.exp_rtsp_info.loport;
394 + exp.mask.dst.u.udp.port = (exp.help.exp_rtsp_info.pbtype == pb_range) ? 0xfffe : 0xffff;
395 + exp.tuple.dst.protonum = IPPROTO_UDP;
396 + exp.mask.dst.protonum = 0xffff;
397 +
398 + DEBUGP("expect_related %u.%u.%u.%u:%u-%u.%u.%u.%u:%u\n",
399 + NIPQUAD(exp.tuple.src.ip),
400 + ntohs(exp.tuple.src.u.tcp.port),
401 + NIPQUAD(exp.tuple.dst.ip),
402 + ntohs(exp.tuple.dst.u.tcp.port));
403 +
404 + /* pass the request off to the nat helper */
405 + rc = ip_conntrack_expect_related(ct, &exp);
406 + UNLOCK_BH(&ip_rtsp_lock);
407 + if (rc == 0)
408 + {
409 + DEBUGP("ip_conntrack_expect_related succeeded\n");
410 + }
411 + else
412 + {
413 + INFOP("ip_conntrack_expect_related failed (%d)\n", rc);
414 + }
415 + }
416 +
417 + return NF_ACCEPT;
418 +}
419 +
420 +/* inbound packet: server->client */
421 +static int
422 +help_in(const struct iphdr* iph, size_t pktlen,
423 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
424 +{
425 + return NF_ACCEPT;
426 +}
427 +
428 +static int
429 +help(const struct iphdr* iph, size_t pktlen,
430 + struct ip_conntrack* ct, enum ip_conntrack_info ctinfo)
431 +{
432 + /* tcplen not negative guarenteed by ip_conntrack_tcp.c */
433 + struct tcphdr* tcph = (void*)iph + iph->ihl * 4;
434 + u_int32_t tcplen = pktlen - iph->ihl * 4;
435 +
436 + /* Until there's been traffic both ways, don't look in packets. */
437 + if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
438 + {
439 + DEBUGP("conntrackinfo = %u\n", ctinfo);
440 + return NF_ACCEPT;
441 + }
442 +
443 + /* Not whole TCP header? */
444 + if (tcplen < sizeof(struct tcphdr) || tcplen < tcph->doff * 4)
445 + {
446 + DEBUGP("tcplen = %u\n", (unsigned)tcplen);
447 + return NF_ACCEPT;
448 + }
449 +
450 + /* Checksum invalid? Ignore. */
451 + /* FIXME: Source route IP option packets --RR */
452 + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
453 + csum_partial((char*)tcph, tcplen, 0)))
454 + {
455 + DEBUGP("bad csum: %p %u %u.%u.%u.%u %u.%u.%u.%u\n",
456 + tcph, tcplen, NIPQUAD(iph->saddr), NIPQUAD(iph->daddr));
457 + return NF_ACCEPT;
458 + }
459 +
460 + switch (CTINFO2DIR(ctinfo))
461 + {
462 + case IP_CT_DIR_ORIGINAL:
463 + help_out(iph, pktlen, ct, ctinfo);
464 + break;
465 + case IP_CT_DIR_REPLY:
466 + help_in(iph, pktlen, ct, ctinfo);
467 + break;
468 + }
469 +
470 + return NF_ACCEPT;
471 +}
472 +
473 +static struct ip_conntrack_helper rtsp_helpers[MAX_PORTS];
474 +static char rtsp_names[MAX_PORTS][10];
475 +
476 +/* This function is intentionally _NOT_ defined as __exit */
477 +static void
478 +fini(void)
479 +{
480 + int i;
481 + for (i = 0; i < num_ports; i++)
482 + {
483 + DEBUGP("unregistering port %d\n", ports[i]);
484 + ip_conntrack_helper_unregister(&rtsp_helpers[i]);
485 + }
486 +}
487 +
488 +static int __init
489 +init(void)
490 +{
491 + int i, ret;
492 + struct ip_conntrack_helper *hlpr;
493 + char *tmpname;
494 +
495 + printk("ip_conntrack_rtsp v" IP_NF_RTSP_VERSION " loading\n");
496 +
497 + if (max_outstanding < 1)
498 + {
499 + printk("ip_conntrack_rtsp: max_outstanding must be a positive integer\n");
500 + return -EBUSY;
501 + }
502 + if (setup_timeout < 0)
503 + {
504 + printk("ip_conntrack_rtsp: setup_timeout must be a positive integer\n");
505 + return -EBUSY;
506 + }
507 +
508 + /* If no port given, default to standard rtsp port */
509 + if (ports[0] == 0)
510 + {
511 + ports[0] = RTSP_PORT;
512 + }
513 +
514 + for (i = 0; (i < MAX_PORTS) && ports[i]; i++)
515 + {
516 + hlpr = &rtsp_helpers[i];
517 + memset(hlpr, 0, sizeof(struct ip_conntrack_helper));
518 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
519 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
520 + hlpr->mask.src.u.tcp.port = 0xFFFF;
521 + hlpr->mask.dst.protonum = 0xFFFF;
522 + hlpr->max_expected = max_outstanding;
523 + hlpr->timeout = setup_timeout;
524 + hlpr->flags = IP_CT_HELPER_F_REUSE_EXPECT;
525 + hlpr->me = ip_conntrack_rtsp;
526 + hlpr->help = help;
527 +
528 + tmpname = &rtsp_names[i][0];
529 + if (ports[i] == RTSP_PORT)
530 + {
531 + sprintf(tmpname, "rtsp");
532 + }
533 + else
534 + {
535 + sprintf(tmpname, "rtsp-%d", i);
536 + }
537 + hlpr->name = tmpname;
538 +
539 + DEBUGP("port #%d: %d\n", i, ports[i]);
540 +
541 + ret = ip_conntrack_helper_register(hlpr);
542 +
543 + if (ret)
544 + {
545 + printk("ip_conntrack_rtsp: ERROR registering port %d\n", ports[i]);
546 + fini();
547 + return -EBUSY;
548 + }
549 + num_ports++;
550 + }
551 + return 0;
552 +}
553 +
554 +#ifdef CONFIG_IP_NF_NAT_NEEDED
555 +EXPORT_SYMBOL(ip_rtsp_lock);
556 +#endif
557 +
558 +module_init(init);
559 +module_exit(fini);
560 diff -urN linux-2.4.32/net/ipv4/netfilter/ip_nat_rtsp.c linux-2.4.32.new/net/ipv4/netfilter/ip_nat_rtsp.c
561 --- linux-2.4.32/net/ipv4/netfilter/ip_nat_rtsp.c 1970-01-01 01:00:00.000000000 +0100
562 +++ linux-2.4.32.new/net/ipv4/netfilter/ip_nat_rtsp.c 2006-03-01 00:55:14.467150168 +0100
563 @@ -0,0 +1,621 @@
564 +/*
565 + * RTSP extension for TCP NAT alteration
566 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
567 + * based on ip_nat_irc.c
568 + *
569 + * This program is free software; you can redistribute it and/or
570 + * modify it under the terms of the GNU General Public License
571 + * as published by the Free Software Foundation; either version
572 + * 2 of the License, or (at your option) any later version.
573 + *
574 + * Module load syntax:
575 + * insmod ip_nat_rtsp.o ports=port1,port2,...port<MAX_PORTS>
576 + * stunaddr=<address>
577 + * destaction=[auto|strip|none]
578 + *
579 + * If no ports are specified, the default will be port 554 only.
580 + *
581 + * stunaddr specifies the address used to detect that a client is using STUN.
582 + * If this address is seen in the destination parameter, it is assumed that
583 + * the client has already punched a UDP hole in the firewall, so we don't
584 + * mangle the client_port. If none is specified, it is autodetected. It
585 + * only needs to be set if you have multiple levels of NAT. It should be
586 + * set to the external address that the STUN clients detect. Note that in
587 + * this case, it will not be possible for clients to use UDP with servers
588 + * between the NATs.
589 + *
590 + * If no destaction is specified, auto is used.
591 + * destaction=auto: strip destination parameter if it is not stunaddr.
592 + * destaction=strip: always strip destination parameter (not recommended).
593 + * destaction=none: do not touch destination parameter (not recommended).
594 + */
595 +
596 +#include <linux/module.h>
597 +#include <linux/netfilter_ipv4.h>
598 +#include <linux/ip.h>
599 +#include <linux/tcp.h>
600 +#include <linux/kernel.h>
601 +#include <net/tcp.h>
602 +#include <linux/netfilter_ipv4/ip_nat.h>
603 +#include <linux/netfilter_ipv4/ip_nat_helper.h>
604 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
605 +#include <linux/netfilter_ipv4/ip_conntrack_rtsp.h>
606 +#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
607 +
608 +#include <linux/inet.h>
609 +#include <linux/ctype.h>
610 +#define NF_NEED_STRNCASECMP
611 +#define NF_NEED_STRTOU16
612 +#include <linux/netfilter_helpers.h>
613 +#define NF_NEED_MIME_NEXTLINE
614 +#include <linux/netfilter_mime.h>
615 +
616 +#define INFOP(fmt, args...) printk(KERN_INFO "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
617 +#ifdef IP_NF_RTSP_DEBUG
618 +#define DEBUGP(fmt, args...) printk(KERN_DEBUG "%s: %s: " fmt, __FILE__, __FUNCTION__ , ## args)
619 +#else
620 +#define DEBUGP(fmt, args...)
621 +#endif
622 +
623 +#define MAX_PORTS 8
624 +#define DSTACT_AUTO 0
625 +#define DSTACT_STRIP 1
626 +#define DSTACT_NONE 2
627 +
628 +static int ports[MAX_PORTS];
629 +static char* stunaddr = NULL;
630 +static char* destaction = NULL;
631 +
632 +static int num_ports = 0;
633 +static u_int32_t extip = 0;
634 +static int dstact = 0;
635 +
636 +MODULE_AUTHOR("Tom Marshall <tmarshall@real.com>");
637 +MODULE_DESCRIPTION("RTSP network address translation module");
638 +MODULE_LICENSE("GPL");
639 +#ifdef MODULE_PARM
640 +MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
641 +MODULE_PARM_DESC(ports, "port numbers of RTSP servers");
642 +MODULE_PARM(stunaddr, "s");
643 +MODULE_PARM_DESC(stunaddr, "Address for detecting STUN");
644 +MODULE_PARM(destaction, "s");
645 +MODULE_PARM_DESC(destaction, "Action for destination parameter (auto/strip/none)");
646 +#endif
647 +
648 +/* protects rtsp part of conntracks */
649 +DECLARE_LOCK_EXTERN(ip_rtsp_lock);
650 +
651 +#define SKIP_WSPACE(ptr,len,off) while(off < len && isspace(*(ptr+off))) { off++; }
652 +
653 +/*** helper functions ***/
654 +
655 +static void
656 +get_skb_tcpdata(struct sk_buff* skb, char** pptcpdata, uint* ptcpdatalen)
657 +{
658 + struct iphdr* iph = (struct iphdr*)skb->nh.iph;
659 + struct tcphdr* tcph = (struct tcphdr*)((char*)iph + iph->ihl*4);
660 +
661 + *pptcpdata = (char*)tcph + tcph->doff*4;
662 + *ptcpdatalen = ((char*)skb->h.raw + skb->len) - *pptcpdata;
663 +}
664 +
665 +/*** nat functions ***/
666 +
667 +/*
668 + * Mangle the "Transport:" header:
669 + * - Replace all occurences of "client_port=<spec>"
670 + * - Handle destination parameter
671 + *
672 + * In:
673 + * ct, ctinfo = conntrack context
674 + * pskb = packet
675 + * tranoff = Transport header offset from TCP data
676 + * tranlen = Transport header length (incl. CRLF)
677 + * rport_lo = replacement low port (host endian)
678 + * rport_hi = replacement high port (host endian)
679 + *
680 + * Returns packet size difference.
681 + *
682 + * Assumes that a complete transport header is present, ending with CR or LF
683 + */
684 +static int
685 +rtsp_mangle_tran(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
686 + struct ip_conntrack_expect* exp,
687 + struct sk_buff** pskb, uint tranoff, uint tranlen)
688 +{
689 + char* ptcp;
690 + uint tcplen;
691 + char* ptran;
692 + char rbuf1[16]; /* Replacement buffer (one port) */
693 + uint rbuf1len; /* Replacement len (one port) */
694 + char rbufa[16]; /* Replacement buffer (all ports) */
695 + uint rbufalen; /* Replacement len (all ports) */
696 + u_int32_t newip;
697 + u_int16_t loport, hiport;
698 + uint off = 0;
699 + uint diff; /* Number of bytes we removed */
700 +
701 + struct ip_ct_rtsp_expect* prtspexp = &exp->help.exp_rtsp_info;
702 + struct ip_conntrack_tuple t;
703 +
704 + char szextaddr[15+1];
705 + uint extaddrlen;
706 + int is_stun;
707 +
708 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
709 + ptran = ptcp+tranoff;
710 +
711 + if (tranoff+tranlen > tcplen || tcplen-tranoff < tranlen ||
712 + tranlen < 10 || !iseol(ptran[tranlen-1]) ||
713 + nf_strncasecmp(ptran, "Transport:", 10) != 0)
714 + {
715 + INFOP("sanity check failed\n");
716 + return 0;
717 + }
718 + off += 10;
719 + SKIP_WSPACE(ptcp+tranoff, tranlen, off);
720 +
721 + newip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
722 + t = exp->tuple;
723 + t.dst.ip = newip;
724 +
725 + extaddrlen = extip ? sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(extip))
726 + : sprintf(szextaddr, "%u.%u.%u.%u", NIPQUAD(newip));
727 + DEBUGP("stunaddr=%s (%s)\n", szextaddr, (extip?"forced":"auto"));
728 +
729 + rbuf1len = rbufalen = 0;
730 + switch (prtspexp->pbtype)
731 + {
732 + case pb_single:
733 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
734 + {
735 + t.dst.u.udp.port = htons(loport);
736 + if (ip_conntrack_change_expect(exp, &t) == 0)
737 + {
738 + DEBUGP("using port %hu\n", loport);
739 + break;
740 + }
741 + }
742 + if (loport != 0)
743 + {
744 + rbuf1len = sprintf(rbuf1, "%hu", loport);
745 + rbufalen = sprintf(rbufa, "%hu", loport);
746 + }
747 + break;
748 + case pb_range:
749 + for (loport = prtspexp->loport; loport != 0; loport += 2) /* XXX: improper wrap? */
750 + {
751 + t.dst.u.udp.port = htons(loport);
752 + if (ip_conntrack_change_expect(exp, &t) == 0)
753 + {
754 + hiport = loport + ~exp->mask.dst.u.udp.port;
755 + DEBUGP("using ports %hu-%hu\n", loport, hiport);
756 + break;
757 + }
758 + }
759 + if (loport != 0)
760 + {
761 + rbuf1len = sprintf(rbuf1, "%hu", loport);
762 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, loport+1);
763 + }
764 + break;
765 + case pb_discon:
766 + for (loport = prtspexp->loport; loport != 0; loport++) /* XXX: improper wrap? */
767 + {
768 + t.dst.u.udp.port = htons(loport);
769 + if (ip_conntrack_change_expect(exp, &t) == 0)
770 + {
771 + DEBUGP("using port %hu (1 of 2)\n", loport);
772 + break;
773 + }
774 + }
775 + for (hiport = prtspexp->hiport; hiport != 0; hiport++) /* XXX: improper wrap? */
776 + {
777 + t.dst.u.udp.port = htons(hiport);
778 + if (ip_conntrack_change_expect(exp, &t) == 0)
779 + {
780 + DEBUGP("using port %hu (2 of 2)\n", hiport);
781 + break;
782 + }
783 + }
784 + if (loport != 0 && hiport != 0)
785 + {
786 + rbuf1len = sprintf(rbuf1, "%hu", loport);
787 + if (hiport == loport+1)
788 + {
789 + rbufalen = sprintf(rbufa, "%hu-%hu", loport, hiport);
790 + }
791 + else
792 + {
793 + rbufalen = sprintf(rbufa, "%hu/%hu", loport, hiport);
794 + }
795 + }
796 + break;
797 + }
798 +
799 + if (rbuf1len == 0)
800 + {
801 + return 0; /* cannot get replacement port(s) */
802 + }
803 +
804 + /* Transport: tran;field;field=val,tran;field;field=val,... */
805 + while (off < tranlen)
806 + {
807 + uint saveoff;
808 + const char* pparamend;
809 + uint nextparamoff;
810 +
811 + pparamend = memchr(ptran+off, ',', tranlen-off);
812 + pparamend = (pparamend == NULL) ? ptran+tranlen : pparamend+1;
813 + nextparamoff = pparamend-ptcp;
814 +
815 + /*
816 + * We pass over each param twice. On the first pass, we look for a
817 + * destination= field. It is handled by the security policy. If it
818 + * is present, allowed, and equal to our external address, we assume
819 + * that STUN is being used and we leave the client_port= field alone.
820 + */
821 + is_stun = 0;
822 + saveoff = off;
823 + while (off < nextparamoff)
824 + {
825 + const char* pfieldend;
826 + uint nextfieldoff;
827 +
828 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
829 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
830 +
831 + if (dstact != DSTACT_NONE && strncmp(ptran+off, "destination=", 12) == 0)
832 + {
833 + if (strncmp(ptran+off+12, szextaddr, extaddrlen) == 0)
834 + {
835 + is_stun = 1;
836 + }
837 + if (dstact == DSTACT_STRIP || (dstact == DSTACT_AUTO && !is_stun))
838 + {
839 + diff = nextfieldoff-off;
840 + if (!ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
841 + off, diff, NULL, 0))
842 + {
843 + /* mangle failed, all we can do is bail */
844 + return 0;
845 + }
846 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
847 + ptran = ptcp+tranoff;
848 + tranlen -= diff;
849 + nextparamoff -= diff;
850 + nextfieldoff -= diff;
851 + }
852 + }
853 +
854 + off = nextfieldoff;
855 + }
856 + if (is_stun)
857 + {
858 + continue;
859 + }
860 + off = saveoff;
861 + while (off < nextparamoff)
862 + {
863 + const char* pfieldend;
864 + uint nextfieldoff;
865 +
866 + pfieldend = memchr(ptran+off, ';', nextparamoff-off);
867 + nextfieldoff = (pfieldend == NULL) ? nextparamoff : pfieldend-ptran+1;
868 +
869 + if (strncmp(ptran+off, "client_port=", 12) == 0)
870 + {
871 + u_int16_t port;
872 + uint numlen;
873 + uint origoff;
874 + uint origlen;
875 + char* rbuf = rbuf1;
876 + uint rbuflen = rbuf1len;
877 +
878 + off += 12;
879 + origoff = (ptran-ptcp)+off;
880 + origlen = 0;
881 + numlen = nf_strtou16(ptran+off, &port);
882 + off += numlen;
883 + origlen += numlen;
884 + if (port != prtspexp->loport)
885 + {
886 + DEBUGP("multiple ports found, port %hu ignored\n", port);
887 + }
888 + else
889 + {
890 + if (ptran[off] == '-' || ptran[off] == '/')
891 + {
892 + off++;
893 + origlen++;
894 + numlen = nf_strtou16(ptran+off, &port);
895 + off += numlen;
896 + origlen += numlen;
897 + rbuf = rbufa;
898 + rbuflen = rbufalen;
899 + }
900 +
901 + /*
902 + * note we cannot just memcpy() if the sizes are the same.
903 + * the mangle function does skb resizing, checks for a
904 + * cloned skb, and updates the checksums.
905 + *
906 + * parameter 4 below is offset from start of tcp data.
907 + */
908 + diff = origlen-rbuflen;
909 + if (!ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
910 + origoff, origlen, rbuf, rbuflen))
911 + {
912 + /* mangle failed, all we can do is bail */
913 + return 0;
914 + }
915 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
916 + ptran = ptcp+tranoff;
917 + tranlen -= diff;
918 + nextparamoff -= diff;
919 + nextfieldoff -= diff;
920 + }
921 + }
922 +
923 + off = nextfieldoff;
924 + }
925 +
926 + off = nextparamoff;
927 + }
928 +
929 + return 1;
930 +}
931 +
932 +static unsigned int
933 +expected(struct sk_buff **pskb, uint hooknum, struct ip_conntrack* ct, struct ip_nat_info* info)
934 +{
935 + struct ip_nat_multi_range mr;
936 + u_int32_t newdstip, newsrcip, newip;
937 +
938 + struct ip_conntrack *master = master_ct(ct);
939 +
940 + IP_NF_ASSERT(info);
941 + IP_NF_ASSERT(master);
942 +
943 + IP_NF_ASSERT(!(info->initialized & (1 << HOOK2MANIP(hooknum))));
944 +
945 + newdstip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
946 + newsrcip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
947 + newip = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC) ? newsrcip : newdstip;
948 +
949 + DEBUGP("newsrcip=%u.%u.%u.%u, newdstip=%u.%u.%u.%u, newip=%u.%u.%u.%u\n",
950 + NIPQUAD(newsrcip), NIPQUAD(newdstip), NIPQUAD(newip));
951 +
952 + mr.rangesize = 1;
953 + /* We don't want to manip the per-protocol, just the IPs. */
954 + mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
955 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
956 +
957 + return ip_nat_setup_info(ct, &mr, hooknum);
958 +}
959 +
960 +static uint
961 +help_out(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
962 + struct ip_conntrack_expect* exp, struct sk_buff** pskb)
963 +{
964 + char* ptcp;
965 + uint tcplen;
966 + uint hdrsoff;
967 + uint hdrslen;
968 + uint lineoff;
969 + uint linelen;
970 + uint off;
971 +
972 + struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
973 + struct tcphdr* tcph = (struct tcphdr*)((void*)iph + iph->ihl*4);
974 +
975 + struct ip_ct_rtsp_expect* prtspexp = &exp->help.exp_rtsp_info;
976 +
977 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
978 +
979 + hdrsoff = exp->seq - ntohl(tcph->seq);
980 + hdrslen = prtspexp->len;
981 + off = hdrsoff;
982 +
983 + while (nf_mime_nextline(ptcp, hdrsoff+hdrslen, &off, &lineoff, &linelen))
984 + {
985 + if (linelen == 0)
986 + {
987 + break;
988 + }
989 + if (off > hdrsoff+hdrslen)
990 + {
991 + INFOP("!! overrun !!");
992 + break;
993 + }
994 + DEBUGP("hdr: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
995 +
996 + if (nf_strncasecmp(ptcp+lineoff, "Transport:", 10) == 0)
997 + {
998 + uint oldtcplen = tcplen;
999 + if (!rtsp_mangle_tran(ct, ctinfo, exp, pskb, lineoff, linelen))
1000 + {
1001 + break;
1002 + }
1003 + get_skb_tcpdata(*pskb, &ptcp, &tcplen);
1004 + hdrslen -= (oldtcplen-tcplen);
1005 + off -= (oldtcplen-tcplen);
1006 + lineoff -= (oldtcplen-tcplen);
1007 + linelen -= (oldtcplen-tcplen);
1008 + DEBUGP("rep: len=%u, %.*s", linelen, (int)linelen, ptcp+lineoff);
1009 + }
1010 + }
1011 +
1012 + return NF_ACCEPT;
1013 +}
1014 +
1015 +static uint
1016 +help_in(struct ip_conntrack* ct, enum ip_conntrack_info ctinfo,
1017 + struct ip_conntrack_expect* exp, struct sk_buff** pskb)
1018 +{
1019 + /* XXX: unmangle */
1020 + return NF_ACCEPT;
1021 +}
1022 +
1023 +static uint
1024 +help(struct ip_conntrack* ct,
1025 + struct ip_conntrack_expect* exp,
1026 + struct ip_nat_info* info,
1027 + enum ip_conntrack_info ctinfo,
1028 + unsigned int hooknum,
1029 + struct sk_buff** pskb)
1030 +{
1031 + struct iphdr* iph = (struct iphdr*)(*pskb)->nh.iph;
1032 + struct tcphdr* tcph = (struct tcphdr*)((char*)iph + iph->ihl * 4);
1033 + uint datalen;
1034 + int dir;
1035 + struct ip_ct_rtsp_expect* ct_rtsp_info;
1036 + int rc = NF_ACCEPT;
1037 +
1038 + if (ct == NULL || exp == NULL || info == NULL || pskb == NULL)
1039 + {
1040 + DEBUGP("!! null ptr (%p,%p,%p,%p) !!\n", ct, exp, info, pskb);
1041 + return NF_ACCEPT;
1042 + }
1043 +
1044 + ct_rtsp_info = &exp->help.exp_rtsp_info;
1045 +
1046 + /*
1047 + * Only mangle things once: original direction in POST_ROUTING
1048 + * and reply direction on PRE_ROUTING.
1049 + */
1050 + dir = CTINFO2DIR(ctinfo);
1051 + if (!((hooknum == NF_IP_POST_ROUTING && dir == IP_CT_DIR_ORIGINAL)
1052 + || (hooknum == NF_IP_PRE_ROUTING && dir == IP_CT_DIR_REPLY)))
1053 + {
1054 + DEBUGP("Not touching dir %s at hook %s\n",
1055 + dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY",
1056 + hooknum == NF_IP_POST_ROUTING ? "POSTROUTING"
1057 + : hooknum == NF_IP_PRE_ROUTING ? "PREROUTING"
1058 + : hooknum == NF_IP_LOCAL_OUT ? "OUTPUT" : "???");
1059 + return NF_ACCEPT;
1060 + }
1061 + DEBUGP("got beyond not touching\n");
1062 +
1063 + datalen = (*pskb)->len - iph->ihl * 4 - tcph->doff * 4;
1064 +
1065 + LOCK_BH(&ip_rtsp_lock);
1066 + /* Ensure the packet contains all of the marked data */
1067 + if (!between(exp->seq + ct_rtsp_info->len,
1068 + ntohl(tcph->seq), ntohl(tcph->seq) + datalen))
1069 + {
1070 + /* Partial retransmission? Probably a hacker. */
1071 + if (net_ratelimit())
1072 + {
1073 + INFOP("partial packet %u/%u in %u/%u\n",
1074 + exp->seq, ct_rtsp_info->len, ntohl(tcph->seq), ntohl(tcph->seq) + datalen);
1075 + }
1076 + UNLOCK_BH(&ip_rtsp_lock);
1077 + return NF_DROP;
1078 + }
1079 +
1080 + switch (dir)
1081 + {
1082 + case IP_CT_DIR_ORIGINAL:
1083 + rc = help_out(ct, ctinfo, exp, pskb);
1084 + break;
1085 + case IP_CT_DIR_REPLY:
1086 + rc = help_in(ct, ctinfo, exp, pskb);
1087 + break;
1088 + }
1089 + UNLOCK_BH(&ip_rtsp_lock);
1090 +
1091 + return rc;
1092 +}
1093 +
1094 +static struct ip_nat_helper ip_nat_rtsp_helpers[MAX_PORTS];
1095 +static char rtsp_names[MAX_PORTS][10];
1096 +
1097 +/* This function is intentionally _NOT_ defined as __exit */
1098 +static void
1099 +fini(void)
1100 +{
1101 + int i;
1102 +
1103 + for (i = 0; i < num_ports; i++)
1104 + {
1105 + DEBUGP("unregistering helper for port %d\n", ports[i]);
1106 + ip_nat_helper_unregister(&ip_nat_rtsp_helpers[i]);
1107 + }
1108 +}
1109 +
1110 +static int __init
1111 +init(void)
1112 +{
1113 + int ret = 0;
1114 + int i;
1115 + struct ip_nat_helper* hlpr;
1116 + char* tmpname;
1117 +
1118 + printk("ip_nat_rtsp v" IP_NF_RTSP_VERSION " loading\n");
1119 +
1120 + if (ports[0] == 0)
1121 + {
1122 + ports[0] = RTSP_PORT;
1123 + }
1124 +
1125 + for (i = 0; (i < MAX_PORTS) && ports[i] != 0; i++)
1126 + {
1127 + hlpr = &ip_nat_rtsp_helpers[i];
1128 + memset(hlpr, 0, sizeof(struct ip_nat_helper));
1129 +
1130 + hlpr->tuple.dst.protonum = IPPROTO_TCP;
1131 + hlpr->tuple.src.u.tcp.port = htons(ports[i]);
1132 + hlpr->mask.src.u.tcp.port = 0xFFFF;
1133 + hlpr->mask.dst.protonum = 0xFFFF;
1134 + hlpr->help = help;
1135 + hlpr->flags = 0;
1136 + hlpr->me = THIS_MODULE;
1137 + hlpr->expect = expected;
1138 +
1139 + tmpname = &rtsp_names[i][0];
1140 + if (ports[i] == RTSP_PORT)
1141 + {
1142 + sprintf(tmpname, "rtsp");
1143 + }
1144 + else
1145 + {
1146 + sprintf(tmpname, "rtsp-%d", i);
1147 + }
1148 + hlpr->name = tmpname;
1149 +
1150 + DEBUGP("registering helper for port %d: name %s\n", ports[i], hlpr->name);
1151 + ret = ip_nat_helper_register(hlpr);
1152 +
1153 + if (ret)
1154 + {
1155 + printk("ip_nat_rtsp: error registering helper for port %d\n", ports[i]);
1156 + fini();
1157 + return 1;
1158 + }
1159 + num_ports++;
1160 + }
1161 + if (stunaddr != NULL)
1162 + {
1163 + extip = in_aton(stunaddr);
1164 + }
1165 + if (destaction != NULL)
1166 + {
1167 + if (strcmp(destaction, "auto") == 0)
1168 + {
1169 + dstact = DSTACT_AUTO;
1170 + }
1171 + if (strcmp(destaction, "strip") == 0)
1172 + {
1173 + dstact = DSTACT_STRIP;
1174 + }
1175 + if (strcmp(destaction, "none") == 0)
1176 + {
1177 + dstact = DSTACT_NONE;
1178 + }
1179 + }
1180 + return ret;
1181 +}
1182 +
1183 +module_init(init);
1184 +module_exit(fini);
1185 --- linux-2.4.32/arch/mips/kernel/mips_ksyms.c 2006-03-01 00:49:34.142887320 +0100
1186 +++ linux-2.4.32.new/arch/mips/kernel/mips_ksyms.c 2006-03-01 00:55:14.469149864 +0100
1187 @@ -52,6 +52,7 @@
1188 /*
1189 * String functions
1190 */
1191 +EXPORT_SYMBOL_NOVERS(memchr);
1192 EXPORT_SYMBOL_NOVERS(memcmp);
1193 EXPORT_SYMBOL_NOVERS(memset);
1194 EXPORT_SYMBOL_NOVERS(memcpy);
1195 diff -urN linux-2.4.32/include/linux/netfilter_helpers.h linux-2.4.32.new/include/linux/netfilter_helpers.h
1196 --- linux-2.4.32/include/linux/netfilter_helpers.h 1970-01-01 01:00:00.000000000 +0100
1197 +++ linux-2.4.32.new/include/linux/netfilter_helpers.h 2006-03-01 00:55:14.470149712 +0100
1198 @@ -0,0 +1,133 @@
1199 +/*
1200 + * Helpers for netfiler modules. This file provides implementations for basic
1201 + * functions such as strncasecmp(), etc.
1202 + *
1203 + * gcc will warn for defined but unused functions, so we only include the
1204 + * functions requested. The following macros are used:
1205 + * NF_NEED_STRNCASECMP nf_strncasecmp()
1206 + * NF_NEED_STRTOU16 nf_strtou16()
1207 + * NF_NEED_STRTOU32 nf_strtou32()
1208 + */
1209 +#ifndef _NETFILTER_HELPERS_H
1210 +#define _NETFILTER_HELPERS_H
1211 +
1212 +/* Only include these functions for kernel code. */
1213 +#ifdef __KERNEL__
1214 +
1215 +#include <linux/ctype.h>
1216 +#define iseol(c) ( (c) == '\r' || (c) == '\n' )
1217 +
1218 +/*
1219 + * The standard strncasecmp()
1220 + */
1221 +#ifdef NF_NEED_STRNCASECMP
1222 +static int
1223 +nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
1224 +{
1225 + if (s1 == NULL || s2 == NULL)
1226 + {
1227 + if (s1 == NULL && s2 == NULL)
1228 + {
1229 + return 0;
1230 + }
1231 + return (s1 == NULL) ? -1 : 1;
1232 + }
1233 + while (len > 0 && tolower(*s1) == tolower(*s2))
1234 + {
1235 + len--;
1236 + s1++;
1237 + s2++;
1238 + }
1239 + return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
1240 +}
1241 +#endif /* NF_NEED_STRNCASECMP */
1242 +
1243 +/*
1244 + * Parse a string containing a 16-bit unsigned integer.
1245 + * Returns the number of chars used, or zero if no number is found.
1246 + */
1247 +#ifdef NF_NEED_STRTOU16
1248 +static int
1249 +nf_strtou16(const char* pbuf, u_int16_t* pval)
1250 +{
1251 + int n = 0;
1252 +
1253 + *pval = 0;
1254 + while (isdigit(pbuf[n]))
1255 + {
1256 + *pval = (*pval * 10) + (pbuf[n] - '0');
1257 + n++;
1258 + }
1259 +
1260 + return n;
1261 +}
1262 +#endif /* NF_NEED_STRTOU16 */
1263 +
1264 +/*
1265 + * Parse a string containing a 32-bit unsigned integer.
1266 + * Returns the number of chars used, or zero if no number is found.
1267 + */
1268 +#ifdef NF_NEED_STRTOU32
1269 +static int
1270 +nf_strtou32(const char* pbuf, u_int32_t* pval)
1271 +{
1272 + int n = 0;
1273 +
1274 + *pval = 0;
1275 + while (pbuf[n] >= '0' && pbuf[n] <= '9')
1276 + {
1277 + *pval = (*pval * 10) + (pbuf[n] - '0');
1278 + n++;
1279 + }
1280 +
1281 + return n;
1282 +}
1283 +#endif /* NF_NEED_STRTOU32 */
1284 +
1285 +/*
1286 + * Given a buffer and length, advance to the next line and mark the current
1287 + * line.
1288 + */
1289 +#ifdef NF_NEED_NEXTLINE
1290 +static int
1291 +nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
1292 +{
1293 + uint off = *poff;
1294 + uint physlen = 0;
1295 +
1296 + if (off >= len)
1297 + {
1298 + return 0;
1299 + }
1300 +
1301 + while (p[off] != '\n')
1302 + {
1303 + if (len-off <= 1)
1304 + {
1305 + return 0;
1306 + }
1307 +
1308 + physlen++;
1309 + off++;
1310 + }
1311 +
1312 + /* if we saw a crlf, physlen needs adjusted */
1313 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
1314 + {
1315 + physlen--;
1316 + }
1317 +
1318 + /* advance past the newline */
1319 + off++;
1320 +
1321 + *plineoff = *poff;
1322 + *plinelen = physlen;
1323 + *poff = off;
1324 +
1325 + return 1;
1326 +}
1327 +#endif /* NF_NEED_NEXTLINE */
1328 +
1329 +#endif /* __KERNEL__ */
1330 +
1331 +#endif /* _NETFILTER_HELPERS_H */
1332 diff -urN linux-2.4.32/include/linux/netfilter_ipv4/ip_conntrack_rtsp.h linux-2.4.32.new/include/linux/netfilter_ipv4/ip_conntrack_rtsp.h
1333 --- linux-2.4.32/include/linux/netfilter_ipv4/ip_conntrack_rtsp.h 1970-01-01 01:00:00.000000000 +0100
1334 +++ linux-2.4.32.new/include/linux/netfilter_ipv4/ip_conntrack_rtsp.h 2006-03-01 00:55:14.472149408 +0100
1335 @@ -0,0 +1,68 @@
1336 +/*
1337 + * RTSP extension for IP connection tracking.
1338 + * (C) 2003 by Tom Marshall <tmarshall@real.com>
1339 + * based on ip_conntrack_irc.h
1340 + *
1341 + * This program is free software; you can redistribute it and/or
1342 + * modify it under the terms of the GNU General Public License
1343 + * as published by the Free Software Foundation; either version
1344 + * 2 of the License, or (at your option) any later version.
1345 + */
1346 +#ifndef _IP_CONNTRACK_RTSP_H
1347 +#define _IP_CONNTRACK_RTSP_H
1348 +
1349 +/* #define IP_NF_RTSP_DEBUG */
1350 +#define IP_NF_RTSP_VERSION "0.01"
1351 +
1352 +/* port block types */
1353 +typedef enum {
1354 + pb_single, /* client_port=x */
1355 + pb_range, /* client_port=x-y */
1356 + pb_discon /* client_port=x/y (rtspbis) */
1357 +} portblock_t;
1358 +
1359 +/* We record seq number and length of rtsp headers here, all in host order. */
1360 +
1361 +/*
1362 + * This structure is per expected connection. It is a member of struct
1363 + * ip_conntrack_expect. The TCP SEQ for the conntrack expect is stored
1364 + * there and we are expected to only store the length of the data which
1365 + * needs replaced. If a packet contains multiple RTSP messages, we create
1366 + * one expected connection per message.
1367 + *
1368 + * We use these variables to mark the entire header block. This may seem
1369 + * like overkill, but the nature of RTSP requires it. A header may appear
1370 + * multiple times in a message. We must treat two Transport headers the
1371 + * same as one Transport header with two entries.
1372 + */
1373 +struct ip_ct_rtsp_expect
1374 +{
1375 + u_int32_t len; /* length of header block */
1376 + portblock_t pbtype; /* Type of port block that was requested */
1377 + u_int16_t loport; /* Port that was requested, low or first */
1378 + u_int16_t hiport; /* Port that was requested, high or second */
1379 +#if 0
1380 + uint method; /* RTSP method */
1381 + uint cseq; /* CSeq from request */
1382 +#endif
1383 +};
1384 +
1385 +/* This structure exists only once per master */
1386 +struct ip_ct_rtsp_master
1387 +{
1388 + /* Empty (?) */
1389 +};
1390 +
1391 +
1392 +#ifdef __KERNEL__
1393 +
1394 +#include <linux/netfilter_ipv4/lockhelp.h>
1395 +
1396 +#define RTSP_PORT 554
1397 +
1398 +/* Protects rtsp part of conntracks */
1399 +DECLARE_LOCK_EXTERN(ip_rtsp_lock);
1400 +
1401 +#endif /* __KERNEL__ */
1402 +
1403 +#endif /* _IP_CONNTRACK_RTSP_H */
1404 diff -urN linux-2.4.32/include/linux/netfilter_mime.h linux-2.4.32.new/include/linux/netfilter_mime.h
1405 --- linux-2.4.32/include/linux/netfilter_mime.h 1970-01-01 01:00:00.000000000 +0100
1406 +++ linux-2.4.32.new/include/linux/netfilter_mime.h 2006-03-01 00:55:14.473149256 +0100
1407 @@ -0,0 +1,90 @@
1408 +/*
1409 + * MIME functions for netfilter modules. This file provides implementations
1410 + * for basic MIME parsing. MIME headers are used in many protocols, such as
1411 + * HTTP, RTSP, SIP, etc.
1412 + *
1413 + * gcc will warn for defined but unused functions, so we only include the
1414 + * functions requested. The following macros are used:
1415 + * NF_NEED_MIME_NEXTLINE nf_mime_nextline()
1416 + */
1417 +#ifndef _NETFILTER_MIME_H
1418 +#define _NETFILTER_MIME_H
1419 +
1420 +/* Only include these functions for kernel code. */
1421 +#ifdef __KERNEL__
1422 +
1423 +#include <linux/kernel.h>
1424 +#include <linux/ctype.h>
1425 +
1426 +/*
1427 + * Given a buffer and length, advance to the next line and mark the current
1428 + * line. If the current line is empty, *plinelen will be set to zero. If
1429 + * not, it will be set to the actual line length (including CRLF).
1430 + *
1431 + * 'line' in this context means logical line (includes LWS continuations).
1432 + * Returns 1 on success, 0 on failure.
1433 + */
1434 +#ifdef NF_NEED_MIME_NEXTLINE
1435 +static int
1436 +nf_mime_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
1437 +{
1438 + uint off = *poff;
1439 + uint physlen = 0;
1440 + int is_first_line = 1;
1441 +
1442 + if (off >= len)
1443 + {
1444 + return 0;
1445 + }
1446 +
1447 + do
1448 + {
1449 + while (p[off] != '\n')
1450 + {
1451 + if (len-off <= 1)
1452 + {
1453 + return 0;
1454 + }
1455 +
1456 + physlen++;
1457 + off++;
1458 + }
1459 +
1460 + /* if we saw a crlf, physlen needs adjusted */
1461 + if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
1462 + {
1463 + physlen--;
1464 + }
1465 +
1466 + /* advance past the newline */
1467 + off++;
1468 +
1469 + /* check for an empty line */
1470 + if (physlen == 0)
1471 + {
1472 + break;
1473 + }
1474 +
1475 + /* check for colon on the first physical line */
1476 + if (is_first_line)
1477 + {
1478 + is_first_line = 0;
1479 + if (memchr(p+(*poff), ':', physlen) == NULL)
1480 + {
1481 + return 0;
1482 + }
1483 + }
1484 + }
1485 + while (p[off] == ' ' || p[off] == '\t');
1486 +
1487 + *plineoff = *poff;
1488 + *plinelen = (physlen == 0) ? 0 : (off - *poff);
1489 + *poff = off;
1490 +
1491 + return 1;
1492 +}
1493 +#endif /* NF_NEED_MIME_NEXTLINE */
1494 +
1495 +#endif /* __KERNEL__ */
1496 +
1497 +#endif /* _NETFILTER_MIME_H */
1498 --- linux-2.4.32/include/linux/netfilter_ipv4/ip_conntrack.h 2006-03-01 00:53:57.889791696 +0100
1499 +++ linux-2.4.32.new/include/linux/netfilter_ipv4/ip_conntrack.h 2006-03-01 01:06:32.178122440 +0100
1500 @@ -72,6 +72,7 @@
1501 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
1502 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
1503 #include <linux/netfilter_ipv4/ip_conntrack_h323.h>
1504 +#include <linux/netfilter_ipv4/ip_conntrack_rtsp.h>
1505
1506 /* per expectation: application helper private data */
1507 union ip_conntrack_expect_help {
1508 @@ -81,6 +82,7 @@
1509 struct ip_ct_irc_expect exp_irc_info;
1510 struct ip_ct_pptp_expect exp_pptp_info;
1511 struct ip_ct_h225_expect exp_h225_info;
1512 + struct ip_ct_rtsp_expect exp_rtsp_info;
1513
1514 #ifdef CONFIG_IP_NF_NAT_NEEDED
1515 union {
1516 @@ -96,6 +98,7 @@
1517 struct ip_ct_irc_master ct_irc_info;
1518 struct ip_ct_pptp_master ct_pptp_info;
1519 struct ip_ct_h225_master ct_h225_info;
1520 + struct ip_ct_rtsp_master ct_rtsp_info;
1521 };
1522
1523 #ifdef CONFIG_IP_NF_NAT_NEEDED