4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2002, 2003 MarcusCom, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Author: Joe Marcus Clarke <marcus@FreeBSD.org>
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/in.h>
47 #include <netinet/ip.h>
48 #include <netinet/tcp.h>
51 #include <netinet/libalias/alias_local.h>
52 #include <netinet/libalias/alias_mod.h>
54 #include "alias_local.h"
55 #include "alias_mod.h"
59 AliasHandleSkinny(struct libalias *, struct ip *, struct alias_link *);
62 fingerprint(struct libalias *la, struct alias_data *ah)
65 if (ah->dport == NULL || ah->sport == NULL || ah->lnk == NULL)
67 if (la->skinnyPort != 0 && (ntohs(*ah->sport) == la->skinnyPort ||
68 ntohs(*ah->dport) == la->skinnyPort))
74 protohandler(struct libalias *la, struct ip *pip, struct alias_data *ah)
77 AliasHandleSkinny(la, pip, ah->lnk);
81 struct proto_handler handlers[] = {
86 .fingerprint = &fingerprint,
87 .protohandler = &protohandler
93 mod_handler(module_t mod, int type, void *data)
100 LibAliasAttachHandlers(handlers);
104 LibAliasDetachHandlers(handlers);
115 moduledata_t alias_mod = {
116 "alias_skinny", mod_handler, NULL
120 DECLARE_MODULE(alias_skinny, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
121 MODULE_VERSION(alias_skinny, 1);
122 MODULE_DEPEND(alias_skinny, libalias, 1, 1, 1);
126 * alias_skinny.c handles the translation for the Cisco Skinny Station
127 * protocol. Skinny typically uses TCP port 2000 to set up calls between
128 * a Cisco Call Manager and a Cisco IP phone. When a phone comes on line,
129 * it first needs to register with the Call Manager. To do this it sends
130 * a registration message. This message contains the IP address of the
131 * IP phone. This message must then be translated to reflect our global
132 * IP address. Along with the registration message (and usually in the
133 * same packet), the phone sends an IP port message. This message indicates
134 * the TCP port over which it will communicate.
136 * When a call is placed from the phone, the Call Manager will send an
137 * Open Receive Channel message to the phone to let the caller know someone
138 * has answered. The phone then sends back an Open Receive Channel
139 * Acknowledgement. In this packet, the phone sends its IP address again,
140 * and the UDP port over which the voice traffic should flow. These values
141 * need translation. Right after the Open Receive Channel Acknowledgement,
142 * the Call Manager sends a Start Media Transmission message indicating the
143 * call is connected. This message contains the IP address and UDP port
144 * number of the remote (called) party. Once this message is translated, the
145 * call can commence. The called part sends the first UDP packet to the
146 * calling phone at the pre-arranged UDP port in the Open Receive Channel
149 * Skinny is a Cisco-proprietary protocol and is a trademark of Cisco Systems,
150 * Inc. All rights reserved.
153 /* #define LIBALIAS_DEBUG 1 */
155 /* Message types that need translating */
156 #define REG_MSG 0x00000001
157 #define IP_PORT_MSG 0x00000002
158 #define OPNRCVCH_ACK 0x00000022
159 #define START_MEDIATX 0x0000008a
161 struct skinny_header {
167 struct RegisterMessage {
174 u_int32_t maxStreams;
177 struct IpPortMessage {
179 u_int32_t stationIpPort; /* Note: Skinny uses 32-bit port
183 struct OpenReceiveChannelAck {
188 u_int32_t passThruPartyID;
191 struct StartMediaTransmission {
193 u_int32_t conferenceID;
194 u_int32_t passThruPartyID;
195 u_int32_t remoteIpAddr;
196 u_int32_t remotePort;
198 u_int32_t payloadCap;
199 u_int32_t precedence;
200 u_int32_t silenceSuppression;
201 u_short maxFramesPerPacket;
202 u_int32_t G723BitRate;
212 alias_skinny_reg_msg(struct RegisterMessage *reg_msg, struct ip *pip,
213 struct tcphdr *tc, struct alias_link *lnk,
214 ConvDirection direction)
218 reg_msg->ipAddr = (u_int32_t) GetAliasAddress(lnk).s_addr;
224 tc->th_sum = TcpChecksum(pip);
231 alias_skinny_startmedia(struct StartMediaTransmission *start_media,
232 struct ip *pip, struct tcphdr *tc,
233 struct alias_link *lnk, u_int32_t localIpAddr,
234 ConvDirection direction)
236 struct in_addr dst, src;
243 dst.s_addr = start_media->remoteIpAddr;
244 src.s_addr = localIpAddr;
247 * XXX I should probably handle in bound global translations as
255 alias_skinny_port_msg(struct IpPortMessage *port_msg, struct ip *pip,
256 struct tcphdr *tc, struct alias_link *lnk,
257 ConvDirection direction)
261 port_msg->stationIpPort = (u_int32_t) ntohs(GetAliasPort(lnk));
267 tc->th_sum = TcpChecksum(pip);
273 alias_skinny_opnrcvch_ack(struct libalias *la, struct OpenReceiveChannelAck *opnrcvch_ack,
274 struct ip *pip, struct tcphdr *tc,
275 struct alias_link *lnk, u_int32_t * localIpAddr,
276 ConvDirection direction)
278 struct in_addr null_addr;
279 struct alias_link *opnrcv_lnk;
285 *localIpAddr = (u_int32_t) opnrcvch_ack->ipAddr;
286 localPort = opnrcvch_ack->port;
288 null_addr.s_addr = INADDR_ANY;
289 opnrcv_lnk = FindUdpTcpOut(la, pip->ip_src, null_addr,
290 htons((u_short) opnrcvch_ack->port), 0,
292 opnrcvch_ack->ipAddr = (u_int32_t) GetAliasAddress(opnrcv_lnk).s_addr;
293 opnrcvch_ack->port = (u_int32_t) ntohs(GetAliasPort(opnrcv_lnk));
299 tc->th_sum = TcpChecksum(pip);
305 AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *lnk)
307 size_t hlen, tlen, dlen;
309 u_int32_t msgId, t, len, lip;
310 struct skinny_header *sd;
311 size_t orig_len, skinny_hdr_len = sizeof(struct skinny_header);
312 ConvDirection direction;
315 tc = (struct tcphdr *)ip_next(pip);
316 hlen = (pip->ip_hl + tc->th_off) << 2;
317 tlen = ntohs(pip->ip_len);
320 sd = (struct skinny_header *)tcp_next(tc);
323 * XXX This direction is reserved for future use. I still need to
324 * handle the scenario where the call manager is on the inside, and
325 * the calling phone is on the global outside.
327 if (ntohs(tc->th_dport) == la->skinnyPort) {
328 direction = ClientToServer;
329 } else if (ntohs(tc->th_sport) == la->skinnyPort) {
330 direction = ServerToClient;
332 #ifdef LIBALIAS_DEBUG
334 "PacketAlias/Skinny: Invalid port number, not a Skinny packet\n");
341 * Skinny packets can contain many messages. We need to loop
342 * through the packet using len to determine message boundaries.
343 * This comes into play big time with port messages being in the
344 * same packet as register messages. Also, open receive channel
345 * acks are usually buried in a packet some 400 bytes long.
347 while (dlen >= skinny_hdr_len) {
352 if (t > orig_len || t > dlen) {
353 #ifdef LIBALIAS_DEBUG
355 "PacketAlias/Skinny: Not a skinny packet, invalid length \n");
361 struct RegisterMessage *reg_mesg;
363 if (len < (int)sizeof(struct RegisterMessage)) {
364 #ifdef LIBALIAS_DEBUG
366 "PacketAlias/Skinny: Not a skinny packet, bad registration message\n");
370 reg_mesg = (struct RegisterMessage *)&sd->msgId;
371 #ifdef LIBALIAS_DEBUG
373 "PacketAlias/Skinny: Received a register message");
375 alias_skinny_reg_msg(reg_mesg, pip, tc, lnk, direction);
379 struct IpPortMessage *port_mesg;
381 if (len < (int)sizeof(struct IpPortMessage)) {
382 #ifdef LIBALIAS_DEBUG
384 "PacketAlias/Skinny: Not a skinny packet, port message\n");
388 #ifdef LIBALIAS_DEBUG
390 "PacketAlias/Skinny: Received ipport message\n");
392 port_mesg = (struct IpPortMessage *)&sd->msgId;
393 alias_skinny_port_msg(port_mesg, pip, tc, lnk, direction);
397 struct OpenReceiveChannelAck *opnrcvchn_ack;
399 if (len < (int)sizeof(struct OpenReceiveChannelAck)) {
400 #ifdef LIBALIAS_DEBUG
402 "PacketAlias/Skinny: Not a skinny packet, packet,OpnRcvChnAckMsg\n");
406 #ifdef LIBALIAS_DEBUG
408 "PacketAlias/Skinny: Received open rcv channel msg\n");
410 opnrcvchn_ack = (struct OpenReceiveChannelAck *)&sd->msgId;
411 alias_skinny_opnrcvch_ack(la, opnrcvchn_ack, pip, tc, lnk, &lip, direction);
414 case START_MEDIATX: {
415 struct StartMediaTransmission *startmedia_tx;
417 if (len < (int)sizeof(struct StartMediaTransmission)) {
418 #ifdef LIBALIAS_DEBUG
420 "PacketAlias/Skinny: Not a skinny packet,StartMediaTx Message\n");
425 #ifdef LIBALIAS_DEBUG
427 "PacketAlias/Skinny: received a"
428 " packet,StartMediaTx Message before"
429 " packet,OpnRcvChnAckMsg\n"
434 #ifdef LIBALIAS_DEBUG
436 "PacketAlias/Skinny: Received start media trans msg\n");
438 startmedia_tx = (struct StartMediaTransmission *)&sd->msgId;
439 alias_skinny_startmedia(startmedia_tx, pip, tc, lnk, lip, direction);
445 /* Place the pointer at the next message in the packet. */
446 dlen -= len + (skinny_hdr_len - sizeof(msgId));
447 sd = (struct skinny_header *)(((char *)&sd->msgId) + len);