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>
34 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
41 #include <arpa/inet.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47 #include <netinet/tcp.h>
48 #include <netinet/udp.h>
51 #include <netinet/libalias/alias.h>
52 #include <netinet/libalias/alias_local.h>
54 #include "alias_local.h"
58 * alias_skinny.c handles the translation for the Cisco Skinny Station
59 * protocol. Skinny typically uses TCP port 2000 to set up calls between
60 * a Cisco Call Manager and a Cisco IP phone. When a phone comes on line,
61 * it first needs to register with the Call Manager. To do this it sends
62 * a registration message. This message contains the IP address of the
63 * IP phone. This message must then be translated to reflect our global
64 * IP address. Along with the registration message (and usually in the
65 * same packet), the phone sends an IP port message. This message indicates
66 * the TCP port over which it will communicate.
68 * When a call is placed from the phone, the Call Manager will send an
69 * Open Receive Channel message to the phone to let the caller know someone
70 * has answered. The phone then sends back an Open Receive Channel
71 * Acknowledgement. In this packet, the phone sends its IP address again,
72 * and the UDP port over which the voice traffic should flow. These values
73 * need translation. Right after the Open Receive Channel Acknowledgement,
74 * the Call Manager sends a Start Media Transmission message indicating the
75 * call is connected. This message contains the IP address and UDP port
76 * number of the remote (called) party. Once this message is translated, the
77 * call can commence. The called part sends the first UDP packet to the
78 * calling phone at the pre-arranged UDP port in the Open Receive Channel
81 * Skinny is a Cisco-proprietary protocol and is a trademark of Cisco Systems,
82 * Inc. All rights reserved.
85 /* #define LIBALIAS_DEBUG 1 */
87 /* Message types that need translating */
88 #define REG_MSG 0x00000001
89 #define IP_PORT_MSG 0x00000002
90 #define OPNRCVCH_ACK 0x00000022
91 #define START_MEDIATX 0x0000008a
93 struct skinny_header {
99 struct RegisterMessage {
106 u_int32_t maxStreams;
109 struct IpPortMessage {
111 u_int32_t stationIpPort; /* Note: Skinny uses 32-bit port
115 struct OpenReceiveChannelAck {
120 u_int32_t passThruPartyID;
123 struct StartMediaTransmission {
125 u_int32_t conferenceID;
126 u_int32_t passThruPartyID;
127 u_int32_t remoteIpAddr;
128 u_int32_t remotePort;
130 u_int32_t payloadCap;
131 u_int32_t precedence;
132 u_int32_t silenceSuppression;
133 u_short maxFramesPerPacket;
134 u_int32_t G723BitRate;
144 alias_skinny_reg_msg(struct RegisterMessage *reg_msg, struct ip *pip,
145 struct tcphdr *tc, struct alias_link *lnk,
146 ConvDirection direction)
150 reg_msg->ipAddr = (u_int32_t) GetAliasAddress(lnk).s_addr;
153 tc->th_sum = TcpChecksum(pip);
159 alias_skinny_startmedia(struct StartMediaTransmission *start_media,
160 struct ip *pip, struct tcphdr *tc,
161 struct alias_link *lnk, u_int32_t localIpAddr,
162 ConvDirection direction)
164 struct in_addr dst, src;
171 dst.s_addr = start_media->remoteIpAddr;
172 src.s_addr = localIpAddr;
175 * XXX I should probably handle in bound global translations as
183 alias_skinny_port_msg(struct IpPortMessage *port_msg, struct ip *pip,
184 struct tcphdr *tc, struct alias_link *lnk,
185 ConvDirection direction)
189 port_msg->stationIpPort = (u_int32_t) ntohs(GetAliasPort(lnk));
192 tc->th_sum = TcpChecksum(pip);
198 alias_skinny_opnrcvch_ack(struct libalias *la, struct OpenReceiveChannelAck *opnrcvch_ack,
199 struct ip *pip, struct tcphdr *tc,
200 struct alias_link *lnk, u_int32_t * localIpAddr,
201 ConvDirection direction)
203 struct in_addr null_addr;
204 struct alias_link *opnrcv_lnk;
210 *localIpAddr = (u_int32_t) opnrcvch_ack->ipAddr;
211 localPort = opnrcvch_ack->port;
213 null_addr.s_addr = INADDR_ANY;
214 opnrcv_lnk = FindUdpTcpOut(la, pip->ip_src, null_addr,
215 htons((u_short) opnrcvch_ack->port), 0,
217 opnrcvch_ack->ipAddr = (u_int32_t) GetAliasAddress(opnrcv_lnk).s_addr;
218 opnrcvch_ack->port = (u_int32_t) ntohs(GetAliasPort(opnrcv_lnk));
221 tc->th_sum = TcpChecksum(pip);
227 AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *lnk)
229 size_t hlen, tlen, dlen;
231 u_int32_t msgId, t, len, lip;
232 struct skinny_header *sd;
233 size_t orig_len, skinny_hdr_len = sizeof(struct skinny_header);
234 ConvDirection direction;
236 tc = (struct tcphdr *)ip_next(pip);
237 hlen = (pip->ip_hl + tc->th_off) << 2;
238 tlen = ntohs(pip->ip_len);
241 sd = (struct skinny_header *)tcp_next(tc);
244 * XXX This direction is reserved for future use. I still need to
245 * handle the scenario where the call manager is on the inside, and
246 * the calling phone is on the global outside.
248 if (ntohs(tc->th_dport) == la->skinnyPort) {
249 direction = ClientToServer;
250 } else if (ntohs(tc->th_sport) == la->skinnyPort) {
251 direction = ServerToClient;
253 #ifdef LIBALIAS_DEBUG
255 "PacketAlias/Skinny: Invalid port number, not a Skinny packet\n");
262 * Skinny packets can contain many messages. We need to loop
263 * through the packet using len to determine message boundaries.
264 * This comes into play big time with port messages being in the
265 * same packet as register messages. Also, open receive channel
266 * acks are usually buried in a pakcet some 400 bytes long.
268 while (dlen >= skinny_hdr_len) {
273 if (t > orig_len || t > dlen) {
274 #ifdef LIBALIAS_DEBUG
276 "PacketAlias/Skinny: Not a skinny packet, invalid length \n");
282 struct RegisterMessage *reg_mesg;
284 if (len < (int)sizeof(struct RegisterMessage)) {
285 #ifdef LIBALIAS_DEBUG
287 "PacketAlias/Skinny: Not a skinny packet, bad registration message\n");
291 reg_mesg = (struct RegisterMessage *)&sd->msgId;
292 #ifdef LIBALIAS_DEBUG
294 "PacketAlias/Skinny: Received a register message");
296 alias_skinny_reg_msg(reg_mesg, pip, tc, lnk, direction);
300 struct IpPortMessage *port_mesg;
302 if (len < (int)sizeof(struct IpPortMessage)) {
303 #ifdef LIBALIAS_DEBUG
305 "PacketAlias/Skinny: Not a skinny packet, port message\n");
309 #ifdef LIBALIAS_DEBUG
311 "PacketAlias/Skinny: Received ipport message\n");
313 port_mesg = (struct IpPortMessage *)&sd->msgId;
314 alias_skinny_port_msg(port_mesg, pip, tc, lnk, direction);
318 struct OpenReceiveChannelAck *opnrcvchn_ack;
320 if (len < (int)sizeof(struct OpenReceiveChannelAck)) {
321 #ifdef LIBALIAS_DEBUG
323 "PacketAlias/Skinny: Not a skinny packet, packet,OpnRcvChnAckMsg\n");
327 #ifdef LIBALIAS_DEBUG
329 "PacketAlias/Skinny: Received open rcv channel msg\n");
331 opnrcvchn_ack = (struct OpenReceiveChannelAck *)&sd->msgId;
332 alias_skinny_opnrcvch_ack(la, opnrcvchn_ack, pip, tc, lnk, &lip, direction);
335 case START_MEDIATX: {
336 struct StartMediaTransmission *startmedia_tx;
338 if (len < (int)sizeof(struct StartMediaTransmission)) {
339 #ifdef LIBALIAS_DEBUG
341 "PacketAlias/Skinny: Not a skinny packet,StartMediaTx Message\n");
345 #ifdef LIBALIAS_DEBUG
347 "PacketAlias/Skinny: Received start media trans msg\n");
349 startmedia_tx = (struct StartMediaTransmission *)&sd->msgId;
350 alias_skinny_startmedia(startmedia_tx, pip, tc, lnk, lip, direction);
356 /* Place the pointer at the next message in the packet. */
357 dlen -= len + (skinny_hdr_len - sizeof(msgId));
358 sd = (struct skinny_header *)(((char *)&sd->msgId) + len);