1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
/*
* IP_MASQ_RAUDIO - Real Audio masquerading module
*
*
* Version: @(#)$Id: ip_masq_raudio.c,v 1.11.2.1 2001/01/10 01:52:33 davem Exp $
*
* Author: Nigel Metheringham
* Real Time Streaming code by Progressive Networks
* [strongly based on ftp module by Juan Jose Ciarlante & Wouter Gadeyne]
* [Real Audio information taken from Progressive Networks firewall docs]
* [Kudos to Progressive Networks for making the protocol specs available]
*
*
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
*
* Limitations
* The IP Masquerading proxies at present do not have access to a processed
* data stream. Hence for a protocol like the Real Audio control protocol,
* which depends on knowing where you are in the data stream, you either
* to keep a *lot* of state in your proxy, or you cheat and simplify the
* problem [needless to say I did the latter].
*
* This proxy only handles data in the first packet. Everything else is
* passed transparently. This means it should work under all normal
* circumstances, but it could be fooled by new data formats or a
* malicious application!
*
* At present the "first packet" is defined as a packet starting with
* the protocol ID string - "PNA".
* When the link is up there appears to be enough control data
* crossing the control link to keep it open even if a long audio
* piece is playing.
*
* The Robust UDP support added in RealAudio 3.0 is supported, but due
* to servers/clients not making great use of this has not been greatly
* tested. RealVideo (as used in the Real client version 4.0beta1) is
* supported but again is not greatly tested (bandwidth requirements
* appear to exceed that available at the sites supporting the protocol).
*
* Multiple Port Support
* The helper can be made to handle up to MAX_MASQ_APP_PORTS (normally 12)
* with the port numbers being defined at module load time. The module
* uses the symbol "ports" to define a list of monitored ports, which can
* be specified on the insmod command line as
* ports=x1,x2,x3...
* where x[n] are integer port numbers. This option can be put into
* /etc/conf.modules (or /etc/modules.conf depending on your config)
* where modload will pick it up should you use modload to load your
* modules.
*
* Fixes:
* Juan Jose Ciarlante : Use control_add() for control chan
* 10/15/97 - Modifications to allow masquerading of RTSP connections as
* well as PNA, which can potentially exist on the same port.
* Joe Rumsey <ogre@real.com>
*
*/
#include <linux/config.h>
#include <linux/module.h>
#include <asm/system.h>
#include <linux/types.h>
#include <linux/ctype.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/init.h>
#include <net/protocol.h>
#include <net/tcp.h>
#include <net/ip_masq.h>
/*
#ifndef DEBUG_CONFIG_IP_MASQ_RAUDIO
#define DEBUG_CONFIG_IP_MASQ_RAUDIO 0
#endif
*/
#define TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? ((c) - 'A' + 'a') : (c))
#define ISDIGIT(c) (((c) >= '0') && ((c) <= '9'))
struct raudio_priv_data {
/* Associated data connection - setup but not used at present */
struct ip_masq *data_conn;
/* UDP Error correction connection - setup but not used at present */
struct ip_masq *error_conn;
/* Have we seen and performed setup */
short seen_start;
short is_rtsp;
};
int
masq_rtsp_out (struct ip_masq_app *mapp,
struct ip_masq *ms,
struct sk_buff **skb_p,
__u32 maddr);
/*
* List of ports (up to MAX_MASQ_APP_PORTS) to be handled by helper
* First port is set to the default port.
*/
int ports[MAX_MASQ_APP_PORTS] = {554, 7070, 0}; /* I rely on the trailing items being set to zero */
struct ip_masq_app *masq_incarnations[MAX_MASQ_APP_PORTS];
/*
* Debug level
*/
#ifdef CONFIG_IP_MASQ_DEBUG
static int debug=0;
MODULE_PARM(debug, "i");
#endif
MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_MASQ_APP_PORTS) "i");
static int
masq_raudio_init_1 (struct ip_masq_app *mapp, struct ip_masq *ms)
{
MOD_INC_USE_COUNT;
if ((ms->app_data = kmalloc(sizeof(struct raudio_priv_data),
GFP_ATOMIC)) == NULL)
printk(KERN_INFO "RealAudio: No memory for application data\n");
else
{
struct raudio_priv_data *priv =
(struct raudio_priv_data *)ms->app_data;
priv->seen_start = 0;
priv->data_conn = NULL;
priv->error_conn = NULL;
priv->is_rtsp = 0;
}
return 0;
}
static int
masq_raudio_done_1 (struct ip_masq_app *mapp, struct ip_masq *ms)
{
MOD_DEC_USE_COUNT;
if (ms->app_data)
kfree_s(ms->app_data, sizeof(struct raudio_priv_data));
return 0;
}
int
masq_raudio_out (struct ip_masq_app *mapp, struct ip_masq *ms, struct sk_buff **skb_p, __u32 maddr)
{
struct sk_buff *skb;
struct iphdr *iph;
struct tcphdr *th;
char *p, *data, *data_limit;
struct ip_masq *n_ms;
unsigned short version, msg_id, msg_len, udp_port;
struct raudio_priv_data *priv =
(struct raudio_priv_data *)ms->app_data;
/* Everything running correctly already */
if (priv && priv->seen_start)
return 0;
if(priv && priv->is_rtsp)
return masq_rtsp_out(mapp, ms, skb_p, maddr);
skb = *skb_p;
iph = skb->nh.iph;
th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
/* Make sure we take into account the size of the TCP packet. This is
* because there may be TCP options in the TCP packet */
data = ((char *)&th[0]) + (th->doff * 4);
data_limit = skb->h.raw + skb->len;
if(memcmp(data, "OPTIONS", 7) == 0 ||
memcmp(data, "DESCRIBE", 8) == 0)
{
IP_MASQ_DEBUG(1-debug, "RealAudio: Detected RTSP connection\n");
/* This is an RTSP client */
if(priv)
priv->is_rtsp = 1;
return masq_rtsp_out(mapp, ms, skb_p, maddr);
}
/* Check to see if this is the first packet with protocol ID */
if (memcmp(data, "PNA", 3)) {
IP_MASQ_DEBUG(1-debug, "RealAudio: not initial protocol packet - ignored\n");
return(0);
}
data += 3;
memcpy(&version, data, 2);
IP_MASQ_DEBUG(1-debug, "RealAudio: initial seen - protocol version %d\n",
ntohs(version));
if (priv)
priv->seen_start = 1;
if (ntohs(version) >= 256)
{
printk(KERN_INFO "RealAudio: version (%d) not supported\n",
ntohs(version));
return 0;
}
data += 2;
while (data+4 < data_limit) {
memcpy(&msg_id, data, 2);
data += 2;
memcpy(&msg_len, data, 2);
data += 2;
if (ntohs(msg_id) == 0) {
/* The zero tag indicates the end of options */
IP_MASQ_DEBUG(1-debug, "RealAudio: packet end tag seen\n");
return 0;
}
IP_MASQ_DEBUG(1-debug, "RealAudio: msg %d - %d byte\n",
ntohs(msg_id), ntohs(msg_len));
if (ntohs(msg_id) == 0) {
/* The zero tag indicates the end of options */
return 0;
}
p = data;
data += ntohs(msg_len);
if (data > data_limit)
{
printk(KERN_INFO "RealAudio: Packet too short for data\n");
return 0;
}
if ((ntohs(msg_id) == 1) || (ntohs(msg_id) == 7)) {
/*
* MsgId == 1
* Audio UDP data port on client
*
* MsgId == 7
* Robust UDP error correction port number on client
*
* Since these messages are treated just the same, they
* are bundled together here....
*/
memcpy(&udp_port, p, 2);
/*
* Sometimes a server sends a message 7 with a zero UDP port
* Rather than do anything with this, just ignore it!
*/
if (udp_port == 0)
continue;
n_ms = ip_masq_new(IPPROTO_UDP,
maddr, 0,
ms->saddr, udp_port,
ms->daddr, 0,
IP_MASQ_F_NO_DPORT);
if (n_ms==NULL)
return 0;
ip_masq_listen(n_ms);
ip_masq_control_add(n_ms, ms);
memcpy(p, &(n_ms->mport), 2);
IP_MASQ_DEBUG(1-debug, "RealAudio: rewrote UDP port %d -> %d in msg %d\n",
ntohs(udp_port), ntohs(n_ms->mport), ntohs(msg_id));
/* Make ref in application data to data connection */
if (priv) {
if (ntohs(msg_id) == 1)
priv->data_conn = n_ms;
else
priv->error_conn = n_ms;
}
ip_masq_put(n_ms);
}
}
return 0;
}
/*
* masq_rtsp_out
*
*
*/
int
masq_rtsp_out (struct ip_masq_app *mapp,
struct ip_masq *ms,
struct sk_buff **skb_p,
__u32 maddr)
{
struct sk_buff *skb;
struct iphdr *iph;
struct tcphdr *th;
char *data, *data_limit;
struct ip_masq *n_ms, *n_ms2;
unsigned short udp_port;
struct raudio_priv_data *priv =
(struct raudio_priv_data *)ms->app_data;
const char* srch = "transport:";
const char* srchpos = srch;
const char* srchend = srch + strlen(srch);
int state = 0;
char firstport[6];
int firstportpos = 0;
char secondport[6];
int secondportpos = 0;
char *portstart = NULL, *portend = NULL;
int diff;
/* Everything running correctly already */
if (priv && priv->seen_start)
return 0;
skb = *skb_p;
iph = skb->nh.iph;
th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
/* Make sure we take into account the size of the TCP packet. This is
* because there may be TCP options in the TCP packet */
data = ((char *)&th[0]) + (th->doff * 4);
data_limit = skb->h.raw + skb->len;
firstport[0] = 0;
secondport[0] = 0;
while(data < data_limit && state >= 0)
{
switch(state)
{
case 0:
case 1:
if(TOLOWER(*data) == *srchpos)
{
srchpos++;
if(srchpos == srchend)
{
IP_MASQ_DEBUG(1-debug, "Found string %s in message\n",
srch);
state++;
if(state == 1)
{
srch = "client_port";
srchpos = srch;
srchend = srch + strlen(srch);
}
}
}
else
{
srchpos = srch;
}
break;
case 2:
if(*data == '=')
state = 3;
break;
case 3:
if(ISDIGIT(*data))
{
portstart = data;
firstportpos = 0;
firstport[firstportpos++] = *data;
state = 4;
}
break;
case 4:
if(*data == '-')
{
state = 5;
}
else if(*data == ';')
{
portend = data - 1;
firstport[firstportpos] = 0;
state = -1;
}
else if(ISDIGIT(*data))
{
firstport[firstportpos++] = *data;
}
else if(*data != ' ' && *data != '\t')
{
/* This is a badly formed RTSP message, let's bail out */
IP_MASQ_DEBUG(1-debug, "Badly formed RTSP Message\n");
return 0;
}
break;
case 5:
if(ISDIGIT(*data))
{
secondportpos = 0;
secondport[secondportpos++] = *data;
state = 6;
}
else if(*data == ';')
{
portend = data - 1;
secondport[secondportpos] = 0;
state = -1;
}
break;
case 6:
if(*data == ';')
{
portend = data - 1;
secondport[secondportpos] = 0;
state = -1;
}
else if(ISDIGIT(*data))
{
secondport[secondportpos++] = *data;
}
else if(*data != ' ' && *data != '\t')
{
/* This is a badly formed RTSP message, let's bail out */
IP_MASQ_DEBUG(1-debug, "Badly formed RTSP Message\n");
return 0;
}
break;
}
data++;
}
if(state >= 0)
return 0;
if(firstportpos > 0)
{
char newbuf[12]; /* xxxxx-xxxxx\0 */
char* tmpptr;
udp_port = htons(simple_strtoul(firstport, &tmpptr, 10));
n_ms = ip_masq_new(IPPROTO_UDP,
maddr, 0,
ms->saddr, udp_port,
ms->daddr, 0,
IP_MASQ_F_NO_DPORT);
if (n_ms==NULL)
return 0;
ip_masq_listen(n_ms);
ip_masq_control_add(n_ms, ms);
if(secondportpos > 0)
{
udp_port = htons(simple_strtoul(secondport, &tmpptr, 10));
n_ms2 = ip_masq_new(IPPROTO_UDP,
maddr, 0,
ms->saddr, udp_port,
ms->daddr, 0,
IP_MASQ_F_NO_DPORT);
if (n_ms2==NULL) {
ip_masq_put(n_ms);
return 0;
}
ip_masq_listen(n_ms2);
ip_masq_control_add(n_ms2, ms);
sprintf(newbuf, "%d-%d", ntohs(n_ms->mport),
ntohs(n_ms2->mport));
}
else
{
sprintf(newbuf, "%d", ntohs(n_ms->mport));
n_ms2 = NULL;
}
*skb_p = ip_masq_skb_replace(skb, GFP_ATOMIC,
portstart, portend - portstart + 1,
newbuf, strlen(newbuf));
IP_MASQ_DEBUG(1-debug, "RTSP: rewrote client_port to %s\n", newbuf);
diff = strlen(newbuf) - (portend - portstart);
}
else
{
return 0;
}
if(priv)
{
priv->seen_start = 1;
if(n_ms)
priv->data_conn = n_ms;
if(n_ms2)
priv->error_conn = n_ms2;
}
/*
* Release tunnels
*/
if (n_ms)
ip_masq_put(n_ms);
if (n_ms2)
ip_masq_put(n_ms2);
return diff;
}
struct ip_masq_app ip_masq_raudio = {
NULL, /* next */
"RealAudio", /* name */
0, /* type */
0, /* n_attach */
masq_raudio_init_1, /* ip_masq_init_1 */
masq_raudio_done_1, /* ip_masq_done_1 */
masq_raudio_out, /* pkt_out */
NULL /* pkt_in */
};
/*
* ip_masq_raudio initialization
*/
__initfunc(int ip_masq_raudio_init(void))
{
int i, j;
for (i=0; (i<MAX_MASQ_APP_PORTS); i++) {
if (ports[i]) {
if ((masq_incarnations[i] = kmalloc(sizeof(struct ip_masq_app),
GFP_KERNEL)) == NULL)
return -ENOMEM;
memcpy(masq_incarnations[i], &ip_masq_raudio, sizeof(struct ip_masq_app));
if ((j = register_ip_masq_app(masq_incarnations[i],
IPPROTO_TCP,
ports[i]))) {
return j;
}
IP_MASQ_DEBUG(1-debug, "RealAudio: loaded support on port[%d] = %d\n",
i, ports[i]);
} else {
/* To be safe, force the incarnation table entry to NULL */
masq_incarnations[i] = NULL;
}
}
return 0;
}
/*
* ip_masq_raudio fin.
*/
int ip_masq_raudio_done(void)
{
int i, j, k;
k=0;
for (i=0; (i<MAX_MASQ_APP_PORTS); i++) {
if (masq_incarnations[i]) {
if ((j = unregister_ip_masq_app(masq_incarnations[i]))) {
k = j;
} else {
kfree(masq_incarnations[i]);
masq_incarnations[i] = NULL;
IP_MASQ_DEBUG(1-debug, "RealAudio: unloaded support on port[%d] = %d\n",
i, ports[i]);
}
}
}
return k;
}
#ifdef MODULE
EXPORT_NO_SYMBOLS;
int init_module(void)
{
if (ip_masq_raudio_init() != 0)
return -EIO;
return 0;
}
void cleanup_module(void)
{
if (ip_masq_raudio_done() != 0)
printk(KERN_INFO "ip_masq_raudio: can't remove module");
}
#endif /* MODULE */
|