2010-02-21 10:11:04 +00:00
|
|
|
/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
|
|
|
|
/* The protocol implementation */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
|
|
|
|
* (C) 2009-2010 by On-Waves
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2010-05-13 18:14:09 +00:00
|
|
|
#include <stdlib.h>
|
2010-02-21 10:11:04 +00:00
|
|
|
#include <unistd.h>
|
2010-02-26 12:42:58 +00:00
|
|
|
#include <endian.h>
|
2010-04-07 10:55:40 +00:00
|
|
|
#include <errno.h>
|
2010-02-21 10:11:04 +00:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2010-02-26 19:16:37 +00:00
|
|
|
#include <osmocore/msgb.h>
|
|
|
|
#include <osmocore/select.h>
|
|
|
|
|
2010-02-21 10:11:04 +00:00
|
|
|
#include <openbsc/debug.h>
|
|
|
|
#include <openbsc/mgcp.h>
|
|
|
|
#include <openbsc/mgcp_internal.h>
|
|
|
|
|
|
|
|
#warning "Make use of the rtp proxy code"
|
|
|
|
|
2010-02-26 12:42:58 +00:00
|
|
|
/* according to rtp_proxy.c RFC 3550 */
|
|
|
|
struct rtp_hdr {
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2010-07-23 10:56:26 +00:00
|
|
|
uint8_t csrc_count:4,
|
2010-02-26 12:42:58 +00:00
|
|
|
extension:1,
|
|
|
|
padding:1,
|
|
|
|
version:2;
|
2010-07-23 10:56:26 +00:00
|
|
|
uint8_t payload_type:7,
|
2010-02-26 12:42:58 +00:00
|
|
|
marker:1;
|
|
|
|
#elif __BYTE_ORDER == __BIG_ENDIAN
|
2010-07-23 10:56:26 +00:00
|
|
|
uint8_t version:2,
|
2010-02-26 12:42:58 +00:00
|
|
|
padding:1,
|
|
|
|
extension:1,
|
|
|
|
csrc_count:4;
|
2010-07-23 10:56:26 +00:00
|
|
|
uint8_t marker:1,
|
2010-02-26 12:42:58 +00:00
|
|
|
payload_type:7;
|
|
|
|
#endif
|
2010-07-23 10:56:01 +00:00
|
|
|
uint16_t sequence;
|
2010-07-23 10:55:38 +00:00
|
|
|
uint32_t timestamp;
|
|
|
|
uint32_t ssrc;
|
2010-02-26 12:42:58 +00:00
|
|
|
} __attribute__((packed));
|
|
|
|
|
2010-02-21 10:11:04 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
DEST_NETWORK = 0,
|
|
|
|
DEST_BTS = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROTO_RTP,
|
|
|
|
PROTO_RTCP,
|
|
|
|
};
|
|
|
|
|
2010-04-21 13:25:13 +00:00
|
|
|
#define DUMMY_LOAD 0x23
|
|
|
|
|
2010-02-21 10:11:04 +00:00
|
|
|
|
|
|
|
static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
|
|
|
|
{
|
|
|
|
struct sockaddr_in out;
|
|
|
|
out.sin_family = AF_INET;
|
|
|
|
out.sin_port = port;
|
|
|
|
memcpy(&out.sin_addr, addr, sizeof(*addr));
|
|
|
|
|
|
|
|
return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
|
|
|
|
}
|
|
|
|
|
2010-04-21 13:25:13 +00:00
|
|
|
int mgcp_send_dummy(struct mgcp_endpoint *endp)
|
|
|
|
{
|
|
|
|
static char buf[] = { DUMMY_LOAD };
|
|
|
|
|
2010-08-04 19:37:22 +00:00
|
|
|
return udp_send(endp->net_end.rtp.fd, &endp->net_end.addr,
|
2010-08-04 17:34:51 +00:00
|
|
|
endp->net_end.rtp_port, buf, 1);
|
2010-04-21 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2010-08-04 14:20:34 +00:00
|
|
|
static void patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
|
|
|
|
int payload, struct sockaddr_in *addr, char *data, int len)
|
2010-02-26 12:42:58 +00:00
|
|
|
{
|
2010-07-28 18:38:39 +00:00
|
|
|
uint16_t seq;
|
2010-08-03 14:35:50 +00:00
|
|
|
uint32_t timestamp;
|
2010-02-26 12:42:58 +00:00
|
|
|
struct rtp_hdr *rtp_hdr;
|
|
|
|
|
|
|
|
if (len < sizeof(*rtp_hdr))
|
|
|
|
return;
|
|
|
|
|
2010-07-28 18:38:39 +00:00
|
|
|
rtp_hdr = (struct rtp_hdr *) data;
|
|
|
|
seq = ntohs(rtp_hdr->sequence);
|
2010-08-03 14:35:50 +00:00
|
|
|
timestamp = ntohl(rtp_hdr->timestamp);
|
2010-07-28 18:38:39 +00:00
|
|
|
|
2010-08-03 11:59:04 +00:00
|
|
|
if (!state->initialized) {
|
2010-08-03 14:35:50 +00:00
|
|
|
state->seq_no = seq - 1;
|
|
|
|
state->ssrc = state->orig_ssrc = rtp_hdr->ssrc;
|
2010-08-03 11:59:04 +00:00
|
|
|
state->initialized = 1;
|
2010-08-03 14:35:50 +00:00
|
|
|
state->last_timestamp = timestamp;
|
|
|
|
} else if (state->ssrc != rtp_hdr->ssrc) {
|
|
|
|
state->ssrc = rtp_hdr->ssrc;
|
|
|
|
state->seq_offset = (state->seq_no + 1) - seq;
|
|
|
|
state->timestamp_offset = state->last_timestamp - timestamp;
|
2010-08-04 13:49:19 +00:00
|
|
|
//state->patch = 1;
|
2010-08-04 13:45:39 +00:00
|
|
|
LOGP(DMGCP, LOGL_NOTICE,
|
2010-08-04 14:14:46 +00:00
|
|
|
"The SSRC changed on 0x%x SSRC: %u offset: %d from %s:%d in %d\n",
|
2010-08-04 14:20:34 +00:00
|
|
|
ENDPOINT_NUMBER(endp), state->ssrc, state->seq_offset,
|
|
|
|
inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), endp->conn_mode);
|
2010-08-03 14:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* apply the offset and store it back to the packet */
|
2010-08-03 15:36:57 +00:00
|
|
|
if (state->patch) {
|
|
|
|
seq += state->seq_offset;
|
|
|
|
rtp_hdr->sequence = htons(seq);
|
|
|
|
rtp_hdr->ssrc = state->orig_ssrc;
|
2010-08-03 14:35:50 +00:00
|
|
|
|
2010-08-03 15:36:57 +00:00
|
|
|
timestamp += state->timestamp_offset;
|
|
|
|
rtp_hdr->timestamp = htonl(timestamp);
|
|
|
|
}
|
2010-08-03 14:35:50 +00:00
|
|
|
|
|
|
|
/* seq changed, now compare if we have lost something */
|
|
|
|
if (state->seq_no + 1u != seq)
|
2010-08-03 11:59:04 +00:00
|
|
|
state->lost_no = abs(seq - (state->seq_no + 1));
|
|
|
|
state->seq_no = seq;
|
2010-07-28 18:38:39 +00:00
|
|
|
|
2010-08-03 14:35:50 +00:00
|
|
|
state->last_timestamp = timestamp;
|
|
|
|
|
2010-04-06 09:15:50 +00:00
|
|
|
if (payload < 0)
|
|
|
|
return;
|
|
|
|
|
2010-02-26 12:42:58 +00:00
|
|
|
rtp_hdr->payload_type = payload;
|
|
|
|
}
|
|
|
|
|
2010-02-21 10:11:04 +00:00
|
|
|
/*
|
|
|
|
* There is data coming. We will have to figure out if it
|
|
|
|
* came from the BTS or the MediaGateway of the MSC. On top
|
|
|
|
* of that we need to figure out if it was RTP or RTCP.
|
|
|
|
*
|
|
|
|
* Currently we do not communicate with the BSC so we have
|
|
|
|
* no idea where the BTS is listening for RTP and need to
|
|
|
|
* do the classic routing trick. Wait for the first packet
|
|
|
|
* from the BTS and then go ahead.
|
|
|
|
*/
|
|
|
|
static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
|
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t slen = sizeof(addr);
|
|
|
|
struct mgcp_endpoint *endp;
|
|
|
|
struct mgcp_config *cfg;
|
|
|
|
int rc, dest, proto;
|
|
|
|
|
|
|
|
endp = (struct mgcp_endpoint *) fd->data;
|
|
|
|
cfg = endp->cfg;
|
|
|
|
|
|
|
|
rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
|
|
|
|
(struct sockaddr *) &addr, &slen);
|
|
|
|
if (rc < 0) {
|
2010-04-07 10:55:40 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
|
|
|
|
ENDPOINT_NUMBER(endp), errno, strerror(errno));
|
2010-02-21 10:11:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do not forward aynthing... maybe there is a packet from the bts */
|
2010-03-01 17:53:05 +00:00
|
|
|
if (endp->ci == CI_UNUSED)
|
2010-02-21 10:11:04 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out where to forward it to. This code assumes that we
|
|
|
|
* have received the Connection Modify and know who is a legitimate
|
|
|
|
* partner. According to the spec we could attempt to forward even
|
|
|
|
* after the Create Connection but we will not as we are not really
|
|
|
|
* able to tell if this is legitimate.
|
|
|
|
*/
|
|
|
|
#warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
|
2010-08-04 17:34:51 +00:00
|
|
|
dest = memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) == 0 &&
|
|
|
|
(endp->net_end.rtp_port == addr.sin_port || endp->net_end.rtcp_port == addr.sin_port)
|
2010-02-21 10:11:04 +00:00
|
|
|
? DEST_BTS : DEST_NETWORK;
|
2010-08-04 19:37:22 +00:00
|
|
|
proto = (fd == &endp->net_end.rtp || fd == &endp->bts_end.rtp) ? PROTO_RTP : PROTO_RTCP;
|
2010-02-21 10:11:04 +00:00
|
|
|
|
|
|
|
/* We have no idea who called us, maybe it is the BTS. */
|
2010-08-04 17:34:51 +00:00
|
|
|
if (dest == DEST_NETWORK && endp->bts_end.rtp_port == 0) {
|
2010-02-21 10:11:04 +00:00
|
|
|
/* it was the BTS... */
|
2010-04-01 05:56:04 +00:00
|
|
|
if (!cfg->bts_ip
|
|
|
|
|| memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0
|
2010-08-04 17:34:51 +00:00
|
|
|
|| memcmp(&addr.sin_addr, &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) {
|
2010-08-04 19:37:22 +00:00
|
|
|
if (proto == PROTO_RTP) {
|
2010-08-04 17:34:51 +00:00
|
|
|
endp->bts_end.rtp_port = addr.sin_port;
|
2010-02-21 10:11:04 +00:00
|
|
|
} else {
|
2010-08-04 17:34:51 +00:00
|
|
|
endp->bts_end.rtcp_port = addr.sin_port;
|
2010-02-21 10:11:04 +00:00
|
|
|
}
|
|
|
|
|
2010-08-04 17:34:51 +00:00
|
|
|
endp->bts_end.addr = addr.sin_addr;
|
2010-04-09 16:53:01 +00:00
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
|
2010-08-04 17:34:51 +00:00
|
|
|
ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port), ntohs(endp->bts_end.rtcp_port),
|
2010-04-09 16:53:01 +00:00
|
|
|
inet_ntoa(addr.sin_addr));
|
2010-04-21 13:25:13 +00:00
|
|
|
|
2010-02-21 10:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-13 18:18:59 +00:00
|
|
|
/* throw away the dummy message */
|
2010-04-22 04:14:51 +00:00
|
|
|
if (rc == 1 && buf[0] == DUMMY_LOAD) {
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy on 0x%x\n",
|
|
|
|
ENDPOINT_NUMBER(endp));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-07 07:37:17 +00:00
|
|
|
/* do this before the loop handling */
|
|
|
|
if (dest == DEST_NETWORK)
|
2010-08-04 17:34:51 +00:00
|
|
|
++endp->bts_end.packets;
|
2010-04-07 07:37:17 +00:00
|
|
|
else
|
2010-08-04 17:34:51 +00:00
|
|
|
++endp->net_end.packets;
|
2010-04-07 07:37:17 +00:00
|
|
|
|
2010-05-13 18:18:59 +00:00
|
|
|
/* For loop toggle the destination and then dispatch. */
|
2010-02-21 10:11:04 +00:00
|
|
|
if (cfg->audio_loop)
|
|
|
|
dest = !dest;
|
|
|
|
|
2010-08-02 18:27:21 +00:00
|
|
|
/* Loop based on the conn_mode, maybe undoing the above */
|
|
|
|
if (endp->conn_mode == MGCP_CONN_LOOPBACK)
|
|
|
|
dest = !dest;
|
|
|
|
|
2010-02-21 10:11:04 +00:00
|
|
|
if (dest == DEST_NETWORK) {
|
2010-05-22 14:06:13 +00:00
|
|
|
if (proto == PROTO_RTP)
|
2010-08-04 14:20:34 +00:00
|
|
|
patch_and_count(endp, &endp->bts_state,
|
2010-08-04 17:34:51 +00:00
|
|
|
endp->net_end.payload_type,
|
2010-08-04 14:20:34 +00:00
|
|
|
&addr, buf, rc);
|
2010-08-04 17:34:51 +00:00
|
|
|
return udp_send(fd->fd, &endp->net_end.addr,
|
|
|
|
proto == PROTO_RTP ? endp->net_end.rtp_port : endp->net_end.rtcp_port,
|
2010-02-21 10:11:04 +00:00
|
|
|
buf, rc);
|
|
|
|
} else {
|
2010-05-22 14:06:13 +00:00
|
|
|
if (proto == PROTO_RTP)
|
2010-08-04 14:20:34 +00:00
|
|
|
patch_and_count(endp, &endp->net_state,
|
2010-08-04 17:34:51 +00:00
|
|
|
endp->bts_end.payload_type,
|
2010-08-04 14:20:34 +00:00
|
|
|
&addr, buf, rc);
|
2010-08-04 17:34:51 +00:00
|
|
|
return udp_send(fd->fd, &endp->bts_end.addr,
|
|
|
|
proto == PROTO_RTP ? endp->bts_end.rtp_port : endp->bts_end.rtcp_port,
|
2010-02-21 10:11:04 +00:00
|
|
|
buf, rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int create_bind(const char *source_addr, struct bsc_fd *fd, int port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int on = 1;
|
|
|
|
|
|
|
|
fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd->fd < 0) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
inet_aton(source_addr, &addr.sin_addr);
|
|
|
|
|
|
|
|
if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-31 02:22:00 +00:00
|
|
|
static int set_ip_tos(int fd, int tos)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
|
|
|
|
&tos, sizeof(tos));
|
|
|
|
return ret != 0;
|
|
|
|
}
|
|
|
|
|
2010-02-21 10:11:04 +00:00
|
|
|
static int bind_rtp(struct mgcp_endpoint *endp)
|
|
|
|
{
|
|
|
|
struct mgcp_config *cfg = endp->cfg;
|
|
|
|
|
2010-08-04 19:37:22 +00:00
|
|
|
if (create_bind(cfg->source_addr, &endp->bts_end.rtp, endp->bts_end.local_port) != 0) {
|
2010-02-21 10:11:04 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
|
2010-08-04 19:08:35 +00:00
|
|
|
cfg->source_addr, endp->bts_end.local_port, ENDPOINT_NUMBER(endp));
|
2010-02-21 10:11:04 +00:00
|
|
|
goto cleanup0;
|
|
|
|
}
|
|
|
|
|
2010-08-04 19:37:22 +00:00
|
|
|
if (create_bind(cfg->source_addr, &endp->bts_end.rtcp, endp->bts_end.local_port + 1) != 0) {
|
2010-02-21 10:11:04 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
|
2010-08-04 19:08:35 +00:00
|
|
|
cfg->source_addr, endp->bts_end.local_port + 1, ENDPOINT_NUMBER(endp));
|
2010-02-21 10:11:04 +00:00
|
|
|
goto cleanup1;
|
|
|
|
}
|
|
|
|
|
2010-08-04 19:37:22 +00:00
|
|
|
set_ip_tos(endp->bts_end.rtp.fd, cfg->endp_dscp);
|
|
|
|
set_ip_tos(endp->bts_end.rtcp.fd, cfg->endp_dscp);
|
2010-05-31 02:22:00 +00:00
|
|
|
|
2010-08-04 19:37:22 +00:00
|
|
|
endp->bts_end.rtp.cb = rtp_data_cb;
|
|
|
|
endp->bts_end.rtp.data = endp;
|
|
|
|
endp->bts_end.rtp.when = BSC_FD_READ;
|
|
|
|
if (bsc_register_fd(&endp->bts_end.rtp) != 0) {
|
2010-02-21 10:11:04 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
|
2010-08-04 19:08:35 +00:00
|
|
|
endp->bts_end.local_port, ENDPOINT_NUMBER(endp));
|
2010-02-21 10:11:04 +00:00
|
|
|
goto cleanup2;
|
|
|
|
}
|
|
|
|
|
2010-08-04 19:37:22 +00:00
|
|
|
endp->bts_end.rtcp.cb = rtp_data_cb;
|
|
|
|
endp->bts_end.rtcp.data = endp;
|
|
|
|
endp->bts_end.rtcp.when = BSC_FD_READ;
|
|
|
|
if (bsc_register_fd(&endp->bts_end.rtcp) != 0) {
|
2010-02-21 10:11:04 +00:00
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
|
2010-08-04 19:08:35 +00:00
|
|
|
endp->bts_end.local_port + 1, ENDPOINT_NUMBER(endp));
|
2010-02-21 10:11:04 +00:00
|
|
|
goto cleanup3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
cleanup3:
|
2010-08-04 19:37:22 +00:00
|
|
|
bsc_unregister_fd(&endp->bts_end.rtp);
|
2010-02-21 10:11:04 +00:00
|
|
|
cleanup2:
|
2010-08-04 19:37:22 +00:00
|
|
|
close(endp->bts_end.rtcp.fd);
|
|
|
|
endp->bts_end.rtcp.fd = -1;
|
2010-02-21 10:11:04 +00:00
|
|
|
cleanup1:
|
2010-08-04 19:37:22 +00:00
|
|
|
close(endp->bts_end.rtp.fd);
|
|
|
|
endp->bts_end.rtp.fd = -1;
|
2010-02-21 10:11:04 +00:00
|
|
|
cleanup0:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-04 19:27:41 +00:00
|
|
|
int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
|
2010-02-21 10:11:04 +00:00
|
|
|
{
|
2010-08-04 19:08:35 +00:00
|
|
|
endp->bts_end.local_port = rtp_port;
|
2010-02-21 10:11:04 +00:00
|
|
|
return bind_rtp(endp);
|
|
|
|
}
|