4 * Copyright (c) 2002, 2003 MarcusCom, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Author: Joe Marcus Clarke <marcus@FreeBSD.org>
30 * $FreeBSD: src/sys/netinet/libalias/alias_skinny.c,v 1.14.6.1 2008/11/25 02:59:29 kensmith Exp $
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/in.h>
45 #include <netinet/ip.h>
46 #include <netinet/tcp.h>
49 #include <netinet/libalias/alias_local.h>
50 #include <netinet/libalias/alias_mod.h>
52 #include "alias_local.h"
53 #include "alias_mod.h"
57 AliasHandleSkinny(struct libalias *, struct ip *, struct alias_link *);
60 fingerprint(struct libalias *la, struct ip *pip, struct alias_data *ah)
63 if (ah->dport == NULL || ah->sport == NULL || ah->lnk == NULL)
65 if (la->skinnyPort != 0 && (ntohs(*ah->sport) == la->skinnyPort ||
66 ntohs(*ah->dport) == la->skinnyPort))
72 protohandler(struct libalias *la, struct ip *pip, struct alias_data *ah)
75 AliasHandleSkinny(la, pip, ah->lnk);
79 struct proto_handler handlers[] = {
84 .fingerprint = &fingerprint,
85 .protohandler = &protohandler
91 mod_handler(module_t mod, int type, void *data)
98 LibAliasAttachHandlers(handlers);
102 LibAliasDetachHandlers(handlers);
113 moduledata_t alias_mod = {
114 "alias_skinny", mod_handler, NULL
118 DECLARE_MODULE(alias_skinny, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
119 MODULE_VERSION(alias_skinny, 1);
120 MODULE_DEPEND(alias_skinny, libalias, 1, 1, 1);
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.
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
147 * Skinny is a Cisco-proprietary protocol and is a trademark of Cisco Systems,
148 * Inc. All rights reserved.
151 /* #define LIBALIAS_DEBUG 1 */
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
159 struct skinny_header {
165 struct RegisterMessage {
172 u_int32_t maxStreams;
175 struct IpPortMessage {
177 u_int32_t stationIpPort; /* Note: Skinny uses 32-bit port
181 struct OpenReceiveChannelAck {
186 u_int32_t passThruPartyID;
189 struct StartMediaTransmission {
191 u_int32_t conferenceID;
192 u_int32_t passThruPartyID;
193 u_int32_t remoteIpAddr;
194 u_int32_t remotePort;
196 u_int32_t payloadCap;
197 u_int32_t precedence;
198 u_int32_t silenceSuppression;
199 u_short maxFramesPerPacket;
200 u_int32_t G723BitRate;
210 alias_skinny_reg_msg(struct RegisterMessage *reg_msg, struct ip *pip,
211 struct tcphdr *tc, struct alias_link *lnk,
212 ConvDirection direction)
216 reg_msg->ipAddr = (u_int32_t) GetAliasAddress(lnk).s_addr;
222 tc->th_sum = TcpChecksum(pip);
229 alias_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)
234 struct in_addr dst, src;
241 dst.s_addr = start_media->remoteIpAddr;
242 src.s_addr = localIpAddr;
245 * XXX I should probably handle in bound global translations as
253 alias_skinny_port_msg(struct IpPortMessage *port_msg, struct ip *pip,
254 struct tcphdr *tc, struct alias_link *lnk,
255 ConvDirection direction)
259 port_msg->stationIpPort = (u_int32_t) ntohs(GetAliasPort(lnk));
265 tc->th_sum = TcpChecksum(pip);
271 alias_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)
276 struct in_addr null_addr;
277 struct alias_link *opnrcv_lnk;
283 *localIpAddr = (u_int32_t) opnrcvch_ack->ipAddr;
284 localPort = opnrcvch_ack->port;
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,
290 opnrcvch_ack->ipAddr = (u_int32_t) GetAliasAddress(opnrcv_lnk).s_addr;
291 opnrcvch_ack->port = (u_int32_t) ntohs(GetAliasPort(opnrcv_lnk));
297 tc->th_sum = TcpChecksum(pip);
303 AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *lnk)
305 size_t hlen, tlen, dlen;
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;
313 tc = (struct tcphdr *)ip_next(pip);
314 hlen = (pip->ip_hl + tc->th_off) << 2;
315 tlen = ntohs(pip->ip_len);
318 sd = (struct skinny_header *)tcp_next(tc);
321 * XXX This direction is reserved for future use. I still need to
322 * handle the scenario where the call manager is on the inside, and
323 * the calling phone is on the global outside.
325 if (ntohs(tc->th_dport) == la->skinnyPort) {
326 direction = ClientToServer;
327 } else if (ntohs(tc->th_sport) == la->skinnyPort) {
328 direction = ServerToClient;
330 #ifdef LIBALIAS_DEBUG
332 "PacketAlias/Skinny: Invalid port number, not a Skinny packet\n");
339 * Skinny packets can contain many messages. We need to loop
340 * through the packet using len to determine message boundaries.
341 * This comes into play big time with port messages being in the
342 * same packet as register messages. Also, open receive channel
343 * acks are usually buried in a pakcet some 400 bytes long.
345 while (dlen >= skinny_hdr_len) {
350 if (t > orig_len || t > dlen) {
351 #ifdef LIBALIAS_DEBUG
353 "PacketAlias/Skinny: Not a skinny packet, invalid length \n");
359 struct RegisterMessage *reg_mesg;
361 if (len < (int)sizeof(struct RegisterMessage)) {
362 #ifdef LIBALIAS_DEBUG
364 "PacketAlias/Skinny: Not a skinny packet, bad registration message\n");
368 reg_mesg = (struct RegisterMessage *)&sd->msgId;
369 #ifdef LIBALIAS_DEBUG
371 "PacketAlias/Skinny: Received a register message");
373 alias_skinny_reg_msg(reg_mesg, pip, tc, lnk, direction);
377 struct IpPortMessage *port_mesg;
379 if (len < (int)sizeof(struct IpPortMessage)) {
380 #ifdef LIBALIAS_DEBUG
382 "PacketAlias/Skinny: Not a skinny packet, port message\n");
386 #ifdef LIBALIAS_DEBUG
388 "PacketAlias/Skinny: Received ipport message\n");
390 port_mesg = (struct IpPortMessage *)&sd->msgId;
391 alias_skinny_port_msg(port_mesg, pip, tc, lnk, direction);
395 struct OpenReceiveChannelAck *opnrcvchn_ack;
397 if (len < (int)sizeof(struct OpenReceiveChannelAck)) {
398 #ifdef LIBALIAS_DEBUG
400 "PacketAlias/Skinny: Not a skinny packet, packet,OpnRcvChnAckMsg\n");
404 #ifdef LIBALIAS_DEBUG
406 "PacketAlias/Skinny: Received open rcv channel msg\n");
408 opnrcvchn_ack = (struct OpenReceiveChannelAck *)&sd->msgId;
409 alias_skinny_opnrcvch_ack(la, opnrcvchn_ack, pip, tc, lnk, &lip, direction);
412 case START_MEDIATX: {
413 struct StartMediaTransmission *startmedia_tx;
415 if (len < (int)sizeof(struct StartMediaTransmission)) {
416 #ifdef LIBALIAS_DEBUG
418 "PacketAlias/Skinny: Not a skinny packet,StartMediaTx Message\n");
423 #ifdef LIBALIAS_DEBUG
425 "PacketAlias/Skinny: received a"
426 " packet,StartMediaTx Message before"
427 " packet,OpnRcvChnAckMsg\n"
432 #ifdef LIBALIAS_DEBUG
434 "PacketAlias/Skinny: Received start media trans msg\n");
436 startmedia_tx = (struct StartMediaTransmission *)&sd->msgId;
437 alias_skinny_startmedia(startmedia_tx, pip, tc, lnk, lip, direction);
443 /* Place the pointer at the next message in the packet. */
444 dlen -= len + (skinny_hdr_len - sizeof(msgId));
445 sd = (struct skinny_header *)(((char *)&sd->msgId) + len);