alias_skinny.c revision 147623
1/*-
2 * alias_skinny.c
3 *
4 * Copyright (c) 2002, 2003 MarcusCom, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 *
28 * Author: Joe Marcus Clarke <marcus@FreeBSD.org>
29 *
30 * $FreeBSD: head/sys/netinet/libalias/alias_skinny.c 147623 2005-06-27 07:36:02Z glebius $
31 */
32
33#ifdef _KERNEL
34#include <sys/param.h>
35#else
36#include <sys/types.h>
37#include <sys/socket.h>
38#include <stdio.h>
39#include <string.h>
40#include <unistd.h>
41#include <arpa/inet.h>
42#endif
43
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>
49
50#ifdef _KERNEL
51#include <netinet/libalias/alias.h>
52#include <netinet/libalias/alias_local.h>
53#else
54#include "alias_local.h"
55#endif
56
57/*
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.
67 *
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
79 * Acknowledgement.
80 *
81 * Skinny is a Cisco-proprietary protocol and is a trademark of Cisco Systems,
82 * Inc.  All rights reserved.
83*/
84
85/* #define LIBALIAS_DEBUG 1 */
86
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
92
93struct skinny_header {
94	u_int32_t	len;
95	u_int32_t	reserved;
96	u_int32_t	msgId;
97};
98
99struct RegisterMessage {
100	u_int32_t	msgId;
101	char		devName   [16];
102	u_int32_t	uid;
103	u_int32_t	instance;
104	u_int32_t	ipAddr;
105	u_char		devType;
106	u_int32_t	maxStreams;
107};
108
109struct IpPortMessage {
110	u_int32_t	msgId;
111	u_int32_t	stationIpPort;	/* Note: Skinny uses 32-bit port
112					 * numbers */
113};
114
115struct OpenReceiveChannelAck {
116	u_int32_t	msgId;
117	u_int32_t	status;
118	u_int32_t	ipAddr;
119	u_int32_t	port;
120	u_int32_t	passThruPartyID;
121};
122
123struct StartMediaTransmission {
124	u_int32_t	msgId;
125	u_int32_t	conferenceID;
126	u_int32_t	passThruPartyID;
127	u_int32_t	remoteIpAddr;
128	u_int32_t	remotePort;
129	u_int32_t	MSPacket;
130	u_int32_t	payloadCap;
131	u_int32_t	precedence;
132	u_int32_t	silenceSuppression;
133	u_short		maxFramesPerPacket;
134	u_int32_t	G723BitRate;
135};
136
137typedef enum {
138	ClientToServer = 0,
139	ServerToClient = 1
140} ConvDirection;
141
142
143static int
144alias_skinny_reg_msg(struct RegisterMessage *reg_msg, struct ip *pip,
145    struct tcphdr *tc, struct alias_link *lnk,
146    ConvDirection direction)
147{
148	(void)direction;
149
150	reg_msg->ipAddr = (u_int32_t) GetAliasAddress(lnk).s_addr;
151
152	tc->th_sum = 0;
153#ifdef _KERNEL
154	tc->th_x2 = 1;
155#else
156	tc->th_sum = TcpChecksum(pip);
157#endif
158
159	return (0);
160}
161
162static int
163alias_skinny_startmedia(struct StartMediaTransmission *start_media,
164    struct ip *pip, struct tcphdr *tc,
165    struct alias_link *lnk, u_int32_t localIpAddr,
166    ConvDirection direction)
167{
168	struct in_addr dst, src;
169
170	(void)pip;
171	(void)tc;
172	(void)lnk;
173	(void)direction;
174
175	dst.s_addr = start_media->remoteIpAddr;
176	src.s_addr = localIpAddr;
177
178	/*
179	 * XXX I should probably handle in bound global translations as
180	 * well.
181	 */
182
183	return (0);
184}
185
186static int
187alias_skinny_port_msg(struct IpPortMessage *port_msg, struct ip *pip,
188    struct tcphdr *tc, struct alias_link *lnk,
189    ConvDirection direction)
190{
191	(void)direction;
192
193	port_msg->stationIpPort = (u_int32_t) ntohs(GetAliasPort(lnk));
194
195	tc->th_sum = 0;
196#ifdef _KERNEL
197	tc->th_x2 = 1;
198#else
199	tc->th_sum = TcpChecksum(pip);
200#endif
201	return (0);
202}
203
204static int
205alias_skinny_opnrcvch_ack(struct libalias *la, struct OpenReceiveChannelAck *opnrcvch_ack,
206    struct ip *pip, struct tcphdr *tc,
207    struct alias_link *lnk, u_int32_t * localIpAddr,
208    ConvDirection direction)
209{
210	struct in_addr null_addr;
211	struct alias_link *opnrcv_lnk;
212	u_int32_t localPort;
213
214	(void)lnk;
215	(void)direction;
216
217	*localIpAddr = (u_int32_t) opnrcvch_ack->ipAddr;
218	localPort = opnrcvch_ack->port;
219
220	null_addr.s_addr = INADDR_ANY;
221	opnrcv_lnk = FindUdpTcpOut(la, pip->ip_src, null_addr,
222	    htons((u_short) opnrcvch_ack->port), 0,
223	    IPPROTO_UDP, 1);
224	opnrcvch_ack->ipAddr = (u_int32_t) GetAliasAddress(opnrcv_lnk).s_addr;
225	opnrcvch_ack->port = (u_int32_t) ntohs(GetAliasPort(opnrcv_lnk));
226
227	tc->th_sum = 0;
228#ifdef _KERNEL
229	tc->th_x2 = 1;
230#else
231	tc->th_sum = TcpChecksum(pip);
232#endif
233	return (0);
234}
235
236void
237AliasHandleSkinny(struct libalias *la, struct ip *pip, struct alias_link *lnk)
238{
239	size_t hlen, tlen, dlen;
240	struct tcphdr *tc;
241	u_int32_t msgId, t, len, lip;
242	struct skinny_header *sd;
243	size_t orig_len, skinny_hdr_len = sizeof(struct skinny_header);
244	ConvDirection direction;
245
246	tc = (struct tcphdr *)ip_next(pip);
247	hlen = (pip->ip_hl + tc->th_off) << 2;
248	tlen = ntohs(pip->ip_len);
249	dlen = tlen - hlen;
250
251	sd = (struct skinny_header *)tcp_next(tc);
252
253	/*
254	 * XXX This direction is reserved for future use.  I still need to
255	 * handle the scenario where the call manager is on the inside, and
256	 * the calling phone is on the global outside.
257	 */
258	if (ntohs(tc->th_dport) == la->skinnyPort) {
259		direction = ClientToServer;
260	} else if (ntohs(tc->th_sport) == la->skinnyPort) {
261		direction = ServerToClient;
262	} else {
263#ifdef LIBALIAS_DEBUG
264		fprintf(stderr,
265		    "PacketAlias/Skinny: Invalid port number, not a Skinny packet\n");
266#endif
267		return;
268	}
269
270	orig_len = dlen;
271	/*
272	 * Skinny packets can contain many messages.  We need to loop
273	 * through the packet using len to determine message boundaries.
274	 * This comes into play big time with port messages being in the
275	 * same packet as register messages.  Also, open receive channel
276	 * acks are usually buried in a pakcet some 400 bytes long.
277	 */
278	while (dlen >= skinny_hdr_len) {
279		len = (sd->len);
280		msgId = (sd->msgId);
281		t = len;
282
283		if (t > orig_len || t > dlen) {
284#ifdef LIBALIAS_DEBUG
285			fprintf(stderr,
286			    "PacketAlias/Skinny: Not a skinny packet, invalid length \n");
287#endif
288			return;
289		}
290		switch (msgId) {
291		case REG_MSG: {
292			struct RegisterMessage *reg_mesg;
293
294			if (len < (int)sizeof(struct RegisterMessage)) {
295#ifdef LIBALIAS_DEBUG
296				fprintf(stderr,
297				    "PacketAlias/Skinny: Not a skinny packet, bad registration message\n");
298#endif
299				return;
300			}
301			reg_mesg = (struct RegisterMessage *)&sd->msgId;
302#ifdef LIBALIAS_DEBUG
303			fprintf(stderr,
304			    "PacketAlias/Skinny: Received a register message");
305#endif
306			alias_skinny_reg_msg(reg_mesg, pip, tc, lnk, direction);
307			break;
308		}
309		case IP_PORT_MSG: {
310			struct IpPortMessage *port_mesg;
311
312			if (len < (int)sizeof(struct IpPortMessage)) {
313#ifdef LIBALIAS_DEBUG
314				fprintf(stderr,
315				    "PacketAlias/Skinny: Not a skinny packet, port message\n");
316#endif
317				return;
318			}
319#ifdef LIBALIAS_DEBUG
320			fprintf(stderr,
321			    "PacketAlias/Skinny: Received ipport message\n");
322#endif
323			port_mesg = (struct IpPortMessage *)&sd->msgId;
324			alias_skinny_port_msg(port_mesg, pip, tc, lnk, direction);
325			break;
326		}
327		case OPNRCVCH_ACK: {
328			struct OpenReceiveChannelAck *opnrcvchn_ack;
329
330			if (len < (int)sizeof(struct OpenReceiveChannelAck)) {
331#ifdef LIBALIAS_DEBUG
332				fprintf(stderr,
333				    "PacketAlias/Skinny: Not a skinny packet, packet,OpnRcvChnAckMsg\n");
334#endif
335				return;
336			}
337#ifdef LIBALIAS_DEBUG
338			fprintf(stderr,
339			    "PacketAlias/Skinny: Received open rcv channel msg\n");
340#endif
341			opnrcvchn_ack = (struct OpenReceiveChannelAck *)&sd->msgId;
342			alias_skinny_opnrcvch_ack(la, opnrcvchn_ack, pip, tc, lnk, &lip, direction);
343			break;
344		}
345		case START_MEDIATX: {
346			struct StartMediaTransmission *startmedia_tx;
347
348			if (len < (int)sizeof(struct StartMediaTransmission)) {
349#ifdef LIBALIAS_DEBUG
350				fprintf(stderr,
351				    "PacketAlias/Skinny: Not a skinny packet,StartMediaTx Message\n");
352#endif
353				return;
354			}
355#ifdef LIBALIAS_DEBUG
356			fprintf(stderr,
357			    "PacketAlias/Skinny: Received start media trans msg\n");
358#endif
359			startmedia_tx = (struct StartMediaTransmission *)&sd->msgId;
360			alias_skinny_startmedia(startmedia_tx, pip, tc, lnk, lip, direction);
361			break;
362		}
363		default:
364			break;
365		}
366		/* Place the pointer at the next message in the packet. */
367		dlen -= len + (skinny_hdr_len - sizeof(msgId));
368		sd = (struct skinny_header *)(((char *)&sd->msgId) + len);
369	}
370}
371