alias_skinny.c revision 162674
1294452Sbrooks/*- 2294452Sbrooks * alias_skinny.c 3294452Sbrooks * 4294452Sbrooks * Copyright (c) 2002, 2003 MarcusCom, Inc. 5294452Sbrooks * All rights reserved. 6294452Sbrooks * 7294452Sbrooks * Redistribution and use in source and binary forms, with or without 8294452Sbrooks * modification, are permitted provided that the following conditions 9294452Sbrooks * are met: 10294452Sbrooks * 1. Redistributions of source code must retain the above copyright 11294452Sbrooks * notice, this list of conditions and the following disclaimer. 12294452Sbrooks * 2. Redistributions in binary form must reproduce the above copyright 13294452Sbrooks * notice, this list of conditions and the following disclaimer in the 14294452Sbrooks * documentation and/or other materials provided with the distribution. 15294452Sbrooks * 16294452Sbrooks * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17294452Sbrooks * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18294452Sbrooks * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19294452Sbrooks * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20294452Sbrooks * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21294452Sbrooks * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22294452Sbrooks * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23294452Sbrooks * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24294452Sbrooks * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25294452Sbrooks * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26294452Sbrooks * SUCH DAMAGE. 27294452Sbrooks * 28294452Sbrooks * Author: Joe Marcus Clarke <marcus@FreeBSD.org> 29294452Sbrooks * 30294452Sbrooks * $FreeBSD: head/sys/netinet/libalias/alias_skinny.c 162674 2006-09-26 23:26:53Z piso $ 31294452Sbrooks */ 32294452Sbrooks 33294452Sbrooks#ifdef _KERNEL 34294452Sbrooks#include <sys/param.h> 35294452Sbrooks#include <sys/kernel.h> 36294452Sbrooks#include <sys/module.h> 37294452Sbrooks#else 38294452Sbrooks#include <errno.h> 39294452Sbrooks#include <stdio.h> 40294452Sbrooks#include <unistd.h> 41294452Sbrooks#endif 42294452Sbrooks 43294452Sbrooks#include <netinet/in_systm.h> 44294452Sbrooks#include <netinet/in.h> 45294452Sbrooks#include <netinet/ip.h> 46294452Sbrooks#include <netinet/tcp.h> 47294452Sbrooks 48294452Sbrooks#ifdef _KERNEL 49294452Sbrooks#include <netinet/libalias/alias_local.h> 50294452Sbrooks#include <netinet/libalias/alias_mod.h> 51294452Sbrooks#else 52294452Sbrooks#include "alias_local.h" 53294452Sbrooks#include "alias_mod.h" 54294452Sbrooks#endif 55294452Sbrooks 56294452Sbrooksstatic void 57294452SbrooksAliasHandleSkinny(struct libalias *, struct ip *, struct alias_link *); 58294452Sbrooks 59294452Sbrooksstatic int 60294452Sbrooksfingerprint(struct libalias *la, struct ip *pip, struct alias_data *ah) 61294452Sbrooks{ 62294452Sbrooks 63294452Sbrooks if (ah->dport == NULL || ah->sport == NULL || ah->lnk == NULL) 64294452Sbrooks return (-1); 65294452Sbrooks if (la->skinnyPort != 0 && (ntohs(*ah->sport) == la->skinnyPort || 66294452Sbrooks ntohs(*ah->dport) == la->skinnyPort)) 67294452Sbrooks return (0); 68294452Sbrooks return (-1); 69294452Sbrooks} 70294452Sbrooks 71294452Sbrooksstatic int 72294452Sbrooksprotohandler(struct libalias *la, struct ip *pip, struct alias_data *ah) 73294452Sbrooks{ 74294452Sbrooks 75294452Sbrooks AliasHandleSkinny(la, pip, ah->lnk); 76294452Sbrooks return (0); 77294452Sbrooks} 78294452Sbrooks 79294452Sbrooksstruct proto_handler handlers[] = { 80294452Sbrooks { 81294452Sbrooks .pri = 110, 82294452Sbrooks .dir = IN|OUT, 83294452Sbrooks .proto = TCP, 84294452Sbrooks .fingerprint = &fingerprint, 85294452Sbrooks .protohandler = &protohandler 86294452Sbrooks }, 87294452Sbrooks { EOH } 88294452Sbrooks}; 89294452Sbrooks 90294452Sbrooksstatic int 91294452Sbrooksmod_handler(module_t mod, int type, void *data) 92294452Sbrooks{ 93294452Sbrooks int error; 94294452Sbrooks 95 switch (type) { 96 case MOD_LOAD: 97 error = 0; 98 LibAliasAttachHandlers(handlers); 99 break; 100 case MOD_UNLOAD: 101 error = 0; 102 LibAliasDetachHandlers(handlers); 103 break; 104 default: 105 error = EINVAL; 106 } 107 return (error); 108} 109 110#ifdef _KERNEL 111static 112#endif 113moduledata_t alias_mod = { 114 "alias_skinny", mod_handler, NULL 115}; 116 117#ifdef _KERNEL 118DECLARE_MODULE(alias_skinny, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND); 119MODULE_VERSION(alias_skinny, 1); 120MODULE_DEPEND(alias_skinny, libalias, 1, 1, 1); 121#endif 122 123/* 124 * alias_skinny.c handles the translation for the Cisco Skinny Station 125 * protocol. Skinny typically uses TCP port 2000 to set up calls between 126 * a Cisco Call Manager and a Cisco IP phone. When a phone comes on line, 127 * it first needs to register with the Call Manager. To do this it sends 128 * a registration message. This message contains the IP address of the 129 * IP phone. This message must then be translated to reflect our global 130 * IP address. Along with the registration message (and usually in the 131 * same packet), the phone sends an IP port message. This message indicates 132 * the TCP port over which it will communicate. 133 * 134 * When a call is placed from the phone, the Call Manager will send an 135 * Open Receive Channel message to the phone to let the caller know someone 136 * has answered. The phone then sends back an Open Receive Channel 137 * Acknowledgement. In this packet, the phone sends its IP address again, 138 * and the UDP port over which the voice traffic should flow. These values 139 * need translation. Right after the Open Receive Channel Acknowledgement, 140 * the Call Manager sends a Start Media Transmission message indicating the 141 * call is connected. This message contains the IP address and UDP port 142 * number of the remote (called) party. Once this message is translated, the 143 * call can commence. The called part sends the first UDP packet to the 144 * calling phone at the pre-arranged UDP port in the Open Receive Channel 145 * Acknowledgement. 146 * 147 * Skinny is a Cisco-proprietary protocol and is a trademark of Cisco Systems, 148 * Inc. All rights reserved. 149*/ 150 151/* #define LIBALIAS_DEBUG 1 */ 152 153/* Message types that need translating */ 154#define REG_MSG 0x00000001 155#define IP_PORT_MSG 0x00000002 156#define OPNRCVCH_ACK 0x00000022 157#define START_MEDIATX 0x0000008a 158 159struct skinny_header { 160 u_int32_t len; 161 u_int32_t reserved; 162 u_int32_t msgId; 163}; 164 165struct RegisterMessage { 166 u_int32_t msgId; 167 char devName [16]; 168 u_int32_t uid; 169 u_int32_t instance; 170 u_int32_t ipAddr; 171 u_char devType; 172 u_int32_t maxStreams; 173}; 174 175struct IpPortMessage { 176 u_int32_t msgId; 177 u_int32_t stationIpPort; /* Note: Skinny uses 32-bit port 178 * numbers */ 179}; 180 181struct OpenReceiveChannelAck { 182 u_int32_t msgId; 183 u_int32_t status; 184 u_int32_t ipAddr; 185 u_int32_t port; 186 u_int32_t passThruPartyID; 187}; 188 189struct StartMediaTransmission { 190 u_int32_t msgId; 191 u_int32_t conferenceID; 192 u_int32_t passThruPartyID; 193 u_int32_t remoteIpAddr; 194 u_int32_t remotePort; 195 u_int32_t MSPacket; 196 u_int32_t payloadCap; 197 u_int32_t precedence; 198 u_int32_t silenceSuppression; 199 u_short maxFramesPerPacket; 200 u_int32_t G723BitRate; 201}; 202 203typedef enum { 204 ClientToServer = 0, 205 ServerToClient = 1 206} ConvDirection; 207 208 209static int 210alias_skinny_reg_msg(struct RegisterMessage *reg_msg, struct ip *pip, 211 struct tcphdr *tc, struct alias_link *lnk, 212 ConvDirection direction) 213{ 214 (void)direction; 215 216 reg_msg->ipAddr = (u_int32_t) GetAliasAddress(lnk).s_addr; 217 218 tc->th_sum = 0; 219#ifdef _KERNEL 220 tc->th_x2 = 1; 221#else 222 tc->th_sum = TcpChecksum(pip); 223#endif 224 225 return (0); 226} 227 228static int 229alias_skinny_startmedia(struct StartMediaTransmission *start_media, 230 struct ip *pip, struct tcphdr *tc, 231 struct alias_link *lnk, u_int32_t localIpAddr, 232 ConvDirection direction) 233{ 234 struct in_addr dst, src; 235 236 (void)pip; 237 (void)tc; 238 (void)lnk; 239 (void)direction; 240 241 dst.s_addr = start_media->remoteIpAddr; 242 src.s_addr = localIpAddr; 243 244 /* 245 * XXX I should probably handle in bound global translations as 246 * well. 247 */ 248 249 return (0); 250} 251 252static int 253alias_skinny_port_msg(struct IpPortMessage *port_msg, struct ip *pip, 254 struct tcphdr *tc, struct alias_link *lnk, 255 ConvDirection direction) 256{ 257 (void)direction; 258 259 port_msg->stationIpPort = (u_int32_t) ntohs(GetAliasPort(lnk)); 260 261 tc->th_sum = 0; 262#ifdef _KERNEL 263 tc->th_x2 = 1; 264#else 265 tc->th_sum = TcpChecksum(pip); 266#endif 267 return (0); 268} 269 270static int 271alias_skinny_opnrcvch_ack(struct libalias *la, struct OpenReceiveChannelAck *opnrcvch_ack, 272 struct ip *pip, struct tcphdr *tc, 273 struct alias_link *lnk, u_int32_t * localIpAddr, 274 ConvDirection direction) 275{ 276 struct in_addr null_addr; 277 struct alias_link *opnrcv_lnk; 278 u_int32_t localPort; 279 280 (void)lnk; 281 (void)direction; 282 283 *localIpAddr = (u_int32_t) opnrcvch_ack->ipAddr; 284 localPort = opnrcvch_ack->port; 285 286 null_addr.s_addr = INADDR_ANY; 287 opnrcv_lnk = FindUdpTcpOut(la, pip->ip_src, null_addr, 288 htons((u_short) opnrcvch_ack->port), 0, 289 IPPROTO_UDP, 1); 290 opnrcvch_ack->ipAddr = (u_int32_t) GetAliasAddress(opnrcv_lnk).s_addr; 291 opnrcvch_ack->port = (u_int32_t) ntohs(GetAliasPort(opnrcv_lnk)); 292 293 tc->th_sum = 0; 294#ifdef _KERNEL 295 tc->th_x2 = 1; 296#else 297 tc->th_sum = TcpChecksum(pip); 298#endif 299 return (0); 300} 301 302static void 303AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *lnk) 304{ 305 size_t hlen, tlen, dlen; 306 struct tcphdr *tc; 307 u_int32_t msgId, t, len, lip; 308 struct skinny_header *sd; 309 size_t orig_len, skinny_hdr_len = sizeof(struct skinny_header); 310 ConvDirection direction; 311 312 tc = (struct tcphdr *)ip_next(pip); 313 hlen = (pip->ip_hl + tc->th_off) << 2; 314 tlen = ntohs(pip->ip_len); 315 dlen = tlen - hlen; 316 317 sd = (struct skinny_header *)tcp_next(tc); 318 319 /* 320 * XXX This direction is reserved for future use. I still need to 321 * handle the scenario where the call manager is on the inside, and 322 * the calling phone is on the global outside. 323 */ 324 if (ntohs(tc->th_dport) == la->skinnyPort) { 325 direction = ClientToServer; 326 } else if (ntohs(tc->th_sport) == la->skinnyPort) { 327 direction = ServerToClient; 328 } else { 329#ifdef LIBALIAS_DEBUG 330 fprintf(stderr, 331 "PacketAlias/Skinny: Invalid port number, not a Skinny packet\n"); 332#endif 333 return; 334 } 335 336 orig_len = dlen; 337 /* 338 * Skinny packets can contain many messages. We need to loop 339 * through the packet using len to determine message boundaries. 340 * This comes into play big time with port messages being in the 341 * same packet as register messages. Also, open receive channel 342 * acks are usually buried in a pakcet some 400 bytes long. 343 */ 344 while (dlen >= skinny_hdr_len) { 345 len = (sd->len); 346 msgId = (sd->msgId); 347 t = len; 348 349 if (t > orig_len || t > dlen) { 350#ifdef LIBALIAS_DEBUG 351 fprintf(stderr, 352 "PacketAlias/Skinny: Not a skinny packet, invalid length \n"); 353#endif 354 return; 355 } 356 switch (msgId) { 357 case REG_MSG: { 358 struct RegisterMessage *reg_mesg; 359 360 if (len < (int)sizeof(struct RegisterMessage)) { 361#ifdef LIBALIAS_DEBUG 362 fprintf(stderr, 363 "PacketAlias/Skinny: Not a skinny packet, bad registration message\n"); 364#endif 365 return; 366 } 367 reg_mesg = (struct RegisterMessage *)&sd->msgId; 368#ifdef LIBALIAS_DEBUG 369 fprintf(stderr, 370 "PacketAlias/Skinny: Received a register message"); 371#endif 372 alias_skinny_reg_msg(reg_mesg, pip, tc, lnk, direction); 373 break; 374 } 375 case IP_PORT_MSG: { 376 struct IpPortMessage *port_mesg; 377 378 if (len < (int)sizeof(struct IpPortMessage)) { 379#ifdef LIBALIAS_DEBUG 380 fprintf(stderr, 381 "PacketAlias/Skinny: Not a skinny packet, port message\n"); 382#endif 383 return; 384 } 385#ifdef LIBALIAS_DEBUG 386 fprintf(stderr, 387 "PacketAlias/Skinny: Received ipport message\n"); 388#endif 389 port_mesg = (struct IpPortMessage *)&sd->msgId; 390 alias_skinny_port_msg(port_mesg, pip, tc, lnk, direction); 391 break; 392 } 393 case OPNRCVCH_ACK: { 394 struct OpenReceiveChannelAck *opnrcvchn_ack; 395 396 if (len < (int)sizeof(struct OpenReceiveChannelAck)) { 397#ifdef LIBALIAS_DEBUG 398 fprintf(stderr, 399 "PacketAlias/Skinny: Not a skinny packet, packet,OpnRcvChnAckMsg\n"); 400#endif 401 return; 402 } 403#ifdef LIBALIAS_DEBUG 404 fprintf(stderr, 405 "PacketAlias/Skinny: Received open rcv channel msg\n"); 406#endif 407 opnrcvchn_ack = (struct OpenReceiveChannelAck *)&sd->msgId; 408 alias_skinny_opnrcvch_ack(la, opnrcvchn_ack, pip, tc, lnk, &lip, direction); 409 break; 410 } 411 case START_MEDIATX: { 412 struct StartMediaTransmission *startmedia_tx; 413 414 if (len < (int)sizeof(struct StartMediaTransmission)) { 415#ifdef LIBALIAS_DEBUG 416 fprintf(stderr, 417 "PacketAlias/Skinny: Not a skinny packet,StartMediaTx Message\n"); 418#endif 419 return; 420 } 421#ifdef LIBALIAS_DEBUG 422 fprintf(stderr, 423 "PacketAlias/Skinny: Received start media trans msg\n"); 424#endif 425 startmedia_tx = (struct StartMediaTransmission *)&sd->msgId; 426 alias_skinny_startmedia(startmedia_tx, pip, tc, lnk, lip, direction); 427 break; 428 } 429 default: 430 break; 431 } 432 /* Place the pointer at the next message in the packet. */ 433 dlen -= len + (skinny_hdr_len - sizeof(msgId)); 434 sd = (struct skinny_header *)(((char *)&sd->msgId) + len); 435 } 436} 437