2010-07-27 19:32:52 +00:00
|
|
|
/* MSC related stuff... */
|
|
|
|
/*
|
2013-03-19 21:29:41 +00:00
|
|
|
* (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org>
|
|
|
|
* (C) 2010-2013 by On-Waves
|
2010-07-27 19:32:52 +00:00
|
|
|
* All Rights Reserved
|
|
|
|
*
|
2011-01-16 16:45:14 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2010-07-27 19:32:52 +00:00
|
|
|
* (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
|
2011-01-16 16:45:14 +00:00
|
|
|
* GNU Affero General Public License for more details.
|
2010-07-27 19:32:52 +00:00
|
|
|
*
|
2011-01-16 16:45:14 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-07-27 19:32:52 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
#include <msc_connection.h>
|
2010-07-27 19:32:52 +00:00
|
|
|
#include <bsc_data.h>
|
2011-02-10 14:32:14 +00:00
|
|
|
#include <bsc_sccp.h>
|
2010-07-27 19:32:52 +00:00
|
|
|
#include <bssap_sccp.h>
|
|
|
|
#include <ipaccess.h>
|
|
|
|
#include <mtp_data.h>
|
2010-07-30 21:25:35 +00:00
|
|
|
#include <cellmgr_debug.h>
|
2011-09-14 22:20:52 +00:00
|
|
|
#include <ss7_application.h>
|
2011-09-15 17:30:20 +00:00
|
|
|
#include <mgcp_patch.h>
|
2010-07-27 19:32:52 +00:00
|
|
|
|
2013-03-19 21:29:41 +00:00
|
|
|
#include <osmocom/core/socket.h>
|
2011-05-08 08:29:23 +00:00
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/utils.h>
|
|
|
|
#include <osmocom/core/write_queue.h>
|
2013-03-19 21:29:41 +00:00
|
|
|
#include <osmocom/gsm/tlv.h>
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define RECONNECT_TIME 10, 0
|
|
|
|
#define NAT_MUX 0xfc
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
static void msc_send_id_response(struct msc_connection *bsc);
|
|
|
|
static void msc_send(struct msc_connection *bsc, struct msgb *msg, int proto);
|
|
|
|
static void msc_schedule_reconnect(struct msc_connection *bsc);
|
2013-03-19 21:29:41 +00:00
|
|
|
static int msc_conn_bind(struct msc_connection *bsc);
|
|
|
|
static void msc_handle_id_response(struct msc_connection *bsc, struct msgb *msg);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
void msc_close_connection(struct msc_connection *fw)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
2011-05-08 08:29:23 +00:00
|
|
|
struct osmo_fd *bfd = &fw->msc_connection.bfd;
|
2010-07-27 19:32:52 +00:00
|
|
|
|
2011-11-25 14:31:15 +00:00
|
|
|
if (bfd->fd >= 0) {
|
|
|
|
close(bfd->fd);
|
|
|
|
osmo_fd_unregister(bfd);
|
|
|
|
bfd->fd = -1;
|
|
|
|
}
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
fw->msc_link_down = 1;
|
|
|
|
release_bsc_resources(fw);
|
2011-05-08 08:29:23 +00:00
|
|
|
osmo_timer_del(&fw->ping_timeout);
|
|
|
|
osmo_timer_del(&fw->pong_timeout);
|
|
|
|
osmo_timer_del(&fw->msc_timeout);
|
2013-03-19 21:51:19 +00:00
|
|
|
osmo_wqueue_clear(&fw->msc_connection);
|
2015-09-21 13:47:10 +00:00
|
|
|
ss7_application_msc_down(fw->app);
|
2011-02-10 14:32:14 +00:00
|
|
|
msc_schedule_reconnect(fw);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
static void msc_connect_timeout(void *_fw_data)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
2011-02-15 19:01:47 +00:00
|
|
|
struct msc_connection *fw = _fw_data;
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Timeout on the MSC connection.\n");
|
2011-02-10 14:32:14 +00:00
|
|
|
msc_close_connection(fw);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
static void msc_pong_timeout(void *_fw_data)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
2011-02-15 19:01:47 +00:00
|
|
|
struct msc_connection *fw = _fw_data;
|
2010-07-27 19:32:52 +00:00
|
|
|
LOGP(DMSC, LOGL_ERROR, "MSC didn't respond to ping. Closing.\n");
|
2011-02-10 14:32:14 +00:00
|
|
|
msc_close_connection(fw);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
static void send_ping(struct msc_connection *fw)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
|
|
|
struct msgb *msg;
|
|
|
|
|
|
|
|
msg = msgb_alloc_headroom(4096, 128, "ping");
|
|
|
|
if (!msg) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Failed to create PING.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->l2h = msgb_put(msg, 1);
|
|
|
|
msg->l2h[0] = IPAC_MSGT_PING;
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
msc_send(fw, msg, IPAC_PROTO_IPACCESS);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
static void msc_ping_timeout(void *_fw_data)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
2011-02-15 19:01:47 +00:00
|
|
|
struct msc_connection *fw = _fw_data;
|
2010-07-27 19:32:52 +00:00
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
if (fw->ping_time < 0)
|
2010-07-27 19:32:52 +00:00
|
|
|
return;
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
send_ping(fw);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
/* send another ping in 20 seconds */
|
2011-05-08 08:29:23 +00:00
|
|
|
osmo_timer_schedule(&fw->ping_timeout, fw->ping_time, 0);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
/* also start a pong timer */
|
2011-05-08 08:29:23 +00:00
|
|
|
osmo_timer_schedule(&fw->pong_timeout, fw->pong_time, 0);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* callback with IP access data
|
|
|
|
*/
|
2011-05-08 08:29:23 +00:00
|
|
|
static int ipaccess_a_fd_cb(struct osmo_fd *bfd)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct ipaccess_head *hh;
|
2011-02-15 19:01:47 +00:00
|
|
|
struct msc_connection *fw;
|
2010-07-27 19:32:52 +00:00
|
|
|
struct msgb *msg;
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
fw = bfd->data;
|
2010-07-27 19:32:52 +00:00
|
|
|
msg = ipaccess_read_msg(bfd, &error);
|
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
if (error == 0)
|
|
|
|
fprintf(stderr, "The connection to the MSC was lost, exiting\n");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Error in the IPA stream.\n");
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
msc_close_connection(fw);
|
2010-07-27 19:32:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-05-08 08:29:23 +00:00
|
|
|
LOGP(DMSC, LOGL_DEBUG, "From MSC: %s proto: %d\n", osmo_hexdump(msg->data, msg->len), msg->l2h[0]);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
/* handle base message handling */
|
|
|
|
hh = (struct ipaccess_head *) msg->data;
|
|
|
|
ipaccess_rcvmsg_base(msg, bfd);
|
|
|
|
|
|
|
|
/* initialize the networking. This includes sending a GSM08.08 message */
|
|
|
|
if (hh->proto == IPAC_PROTO_IPACCESS) {
|
2011-02-10 14:32:14 +00:00
|
|
|
if (fw->first_contact) {
|
2010-07-27 19:32:52 +00:00
|
|
|
LOGP(DMSC, LOGL_NOTICE, "Connected to MSC. Sending reset.\n");
|
2011-05-08 08:29:23 +00:00
|
|
|
osmo_timer_del(&fw->msc_timeout);
|
2011-02-10 14:32:14 +00:00
|
|
|
fw->first_contact = 0;
|
|
|
|
fw->msc_link_down = 0;
|
2015-09-21 13:47:10 +00:00
|
|
|
ss7_application_msc_up(fw->app);
|
2011-02-10 14:32:14 +00:00
|
|
|
msc_send_reset(fw);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
2011-02-10 14:32:14 +00:00
|
|
|
if (msg->l2h[0] == IPAC_MSGT_ID_GET && fw->token) {
|
|
|
|
msc_send_id_response(fw);
|
2010-07-27 19:32:52 +00:00
|
|
|
} else if (msg->l2h[0] == IPAC_MSGT_PONG) {
|
2011-05-08 08:29:23 +00:00
|
|
|
osmo_timer_del(&fw->pong_timeout);
|
2013-03-19 21:29:41 +00:00
|
|
|
} else if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
|
|
|
|
msc_handle_id_response(fw, msg);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
2013-03-19 21:29:41 +00:00
|
|
|
|
|
|
|
msgb_free(msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fw->mode == MSC_MODE_SERVER && !fw->auth) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR,
|
|
|
|
"Ignoring non ipa message for unauth user.\n");
|
|
|
|
msgb_free(msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hh->proto == IPAC_PROTO_SCCP) {
|
2011-02-15 20:14:09 +00:00
|
|
|
msc_dispatch_sccp(fw, msg);
|
2010-07-27 19:32:52 +00:00
|
|
|
} else if (hh->proto == NAT_MUX) {
|
2011-09-15 17:30:20 +00:00
|
|
|
msg = mgcp_patch(fw->app, msg);
|
2012-12-11 15:35:01 +00:00
|
|
|
mgcp_forward(&fw->mgcp_agent, msg->l2h, msgb_l2len(msg));
|
2010-07-27 19:32:52 +00:00
|
|
|
} else {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Unknown IPA proto 0x%x\n", hh->proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
msgb_free(msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-08 08:29:23 +00:00
|
|
|
static int ipaccess_write_cb(struct osmo_fd *fd, struct msgb *msg)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2011-05-08 08:29:23 +00:00
|
|
|
LOGP(DMSC, LOGL_DEBUG, "Sending to MSC: %s\n", osmo_hexdump(msg->data, msg->len));
|
2010-07-27 19:32:52 +00:00
|
|
|
rc = write(fd->fd, msg->data, msg->len);
|
|
|
|
if (rc != msg->len)
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Could not write to MSC.\n");
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called in the case of a non blocking connect */
|
2011-05-08 08:29:23 +00:00
|
|
|
static int msc_connection_connect(struct osmo_fd *fd, unsigned int what)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int val;
|
|
|
|
socklen_t len = sizeof(val);
|
2011-02-15 19:01:47 +00:00
|
|
|
struct msc_connection *fw;
|
2010-07-27 19:32:52 +00:00
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
fw = fd->data;
|
2010-07-27 19:32:52 +00:00
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
if (fd != &fw->msc_connection.bfd) {
|
2010-07-27 19:32:52 +00:00
|
|
|
LOGP(DMSC, LOGL_ERROR, "This is only working with the MSC connection.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((what & BSC_FD_WRITE) == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* check the socket state */
|
|
|
|
rc = getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &val, &len);
|
|
|
|
if (rc != 0) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "getsockopt for the MSC socket failed.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (val != 0) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Not connected to the MSC.\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* go to full operation */
|
2011-05-08 08:29:23 +00:00
|
|
|
fd->cb = osmo_wqueue_bfd_cb;
|
2010-07-27 19:32:52 +00:00
|
|
|
fd->when = BSC_FD_READ;
|
2011-02-10 14:32:14 +00:00
|
|
|
if (!llist_empty(&fw->msc_connection.msg_queue))
|
2010-07-27 19:32:52 +00:00
|
|
|
fd->when |= BSC_FD_WRITE;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
2011-02-10 14:32:14 +00:00
|
|
|
msc_close_connection(fw);
|
2010-07-27 19:32:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-05-08 08:29:23 +00:00
|
|
|
static int setnonblocking(struct osmo_fd *fd)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = fcntl(fd->fd, F_GETFL);
|
|
|
|
if (flags < 0) {
|
|
|
|
perror("fcntl get failed");
|
|
|
|
close(fd->fd);
|
|
|
|
fd->fd = -1;
|
2010-10-01 12:37:40 +00:00
|
|
|
return -1;
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
flags = fcntl(fd->fd, F_SETFL, flags);
|
|
|
|
if (flags < 0) {
|
|
|
|
perror("fcntl get failed");
|
|
|
|
close(fd->fd);
|
|
|
|
fd->fd = -1;
|
2010-10-01 12:37:40 +00:00
|
|
|
return -1;
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
2010-10-01 12:37:40 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-05-08 08:29:23 +00:00
|
|
|
static int connect_to_msc(struct osmo_fd *fd, const char *ip, int port, int tos)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
int on = 1, ret;
|
|
|
|
|
|
|
|
LOGP(DMSC, LOGL_NOTICE, "Attempting to connect MSC at %s:%d\n", ip, port);
|
|
|
|
|
|
|
|
fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
|
|
|
|
if (fd->fd < 0) {
|
|
|
|
perror("Creating TCP socket failed");
|
|
|
|
return fd->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make it non blocking */
|
2010-10-01 12:37:40 +00:00
|
|
|
if (setnonblocking(fd) != 0)
|
|
|
|
return -1;
|
2010-07-27 19:32:52 +00:00
|
|
|
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = htons(port);
|
|
|
|
inet_aton(ip, &sin.sin_addr);
|
|
|
|
|
|
|
|
setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
|
|
|
ret = setsockopt(fd->fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
|
|
|
|
if (ret != 0)
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
|
|
|
|
ret = setsockopt(fd->fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
|
|
|
|
if (ret != 0)
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin));
|
|
|
|
|
|
|
|
if (ret == -1 && errno == EINPROGRESS) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "MSC Connection in progress\n");
|
|
|
|
fd->when = BSC_FD_WRITE;
|
|
|
|
fd->cb = msc_connection_connect;
|
|
|
|
} else if (ret < 0) {
|
|
|
|
perror("Connection failed");
|
|
|
|
close(fd->fd);
|
|
|
|
fd->fd = -1;
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
fd->when = BSC_FD_READ;
|
2011-05-08 08:29:23 +00:00
|
|
|
fd->cb = osmo_wqueue_bfd_cb;
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-05-08 08:29:23 +00:00
|
|
|
ret = osmo_fd_register(fd);
|
2010-07-27 19:32:52 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
perror("Registering the fd failed");
|
|
|
|
close(fd->fd);
|
|
|
|
fd->fd = -1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void msc_reconnect(void *_data)
|
|
|
|
{
|
|
|
|
int rc;
|
2011-02-15 19:01:47 +00:00
|
|
|
struct msc_connection *fw = _data;
|
2010-07-27 19:32:52 +00:00
|
|
|
|
2011-05-08 08:29:23 +00:00
|
|
|
osmo_timer_del(&fw->reconnect_timer);
|
2011-02-10 14:32:14 +00:00
|
|
|
fw->first_contact = 1;
|
2010-07-27 19:32:52 +00:00
|
|
|
|
2013-03-19 21:29:41 +00:00
|
|
|
rc = connect_to_msc(&fw->msc_connection.bfd, fw->ip, fw->port, fw->dscp);
|
2010-07-27 19:32:52 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "Opening the MSC connection failed. Trying again\n");
|
2011-05-08 08:29:23 +00:00
|
|
|
osmo_timer_schedule(&fw->reconnect_timer, RECONNECT_TIME);
|
2010-07-27 19:32:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
fw->msc_timeout.cb = msc_connect_timeout;
|
|
|
|
fw->msc_timeout.data = fw;
|
2011-05-08 08:29:23 +00:00
|
|
|
osmo_timer_schedule(&fw->msc_timeout, fw->msc_time, 0);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
static void msc_schedule_reconnect(struct msc_connection *fw)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
2013-03-19 21:29:41 +00:00
|
|
|
if (fw->mode == MSC_MODE_SERVER)
|
|
|
|
return;
|
2011-05-08 08:29:23 +00:00
|
|
|
osmo_timer_schedule(&fw->reconnect_timer, RECONNECT_TIME);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mgcp forwarding is below
|
|
|
|
*/
|
2011-02-16 21:35:30 +00:00
|
|
|
/* send a RSIP to the MGCP GW */
|
|
|
|
void msc_mgcp_reset(struct msc_connection *msc)
|
|
|
|
{
|
2011-09-14 22:20:52 +00:00
|
|
|
char buf[512];
|
|
|
|
char *dest = "mgw";
|
2011-02-16 21:35:30 +00:00
|
|
|
|
2011-09-14 22:20:52 +00:00
|
|
|
if (msc->app->mgcp_domain_name)
|
|
|
|
dest = msc->app->mgcp_domain_name;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf) - 1, "RSIP 1 13@%s MGCP 1.0\r\n", dest);
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
|
2012-12-11 15:35:01 +00:00
|
|
|
mgcp_forward(&msc->mgcp_agent, (const uint8_t *) buf, strlen(buf));
|
2011-02-16 21:35:30 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 15:35:01 +00:00
|
|
|
static void msc_mgcp_read_cb(struct mgcp_callagent *agent, struct msgb *msg)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
2012-12-11 15:35:01 +00:00
|
|
|
struct msc_connection *fw = container_of(agent, struct msc_connection, mgcp_agent);
|
|
|
|
msc_send(fw, msg, NAT_MUX);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
static void msc_send(struct msc_connection *fw, struct msgb *msg, int proto)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
2011-02-10 14:32:14 +00:00
|
|
|
if (fw->msc_link_down) {
|
2010-08-06 21:41:06 +00:00
|
|
|
LOGP(DMSC, LOGL_NOTICE, "Dropping data due lack of MSC connection.\n");
|
|
|
|
msgb_free(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-27 19:32:52 +00:00
|
|
|
ipaccess_prepend_header(msg, proto);
|
|
|
|
|
2011-05-08 08:29:23 +00:00
|
|
|
if (osmo_wqueue_enqueue(&fw->msc_connection, msg) != 0) {
|
2010-07-27 19:32:52 +00:00
|
|
|
LOGP(DMSC, LOGL_FATAL, "Failed to queue MSG for the MSC.\n");
|
|
|
|
msgb_free(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
void msc_send_rlc(struct msc_connection *fw,
|
2010-07-27 19:32:52 +00:00
|
|
|
struct sccp_source_reference *src, struct sccp_source_reference *dst)
|
|
|
|
{
|
|
|
|
struct msgb *msg;
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
if (fw->msc_link_down) {
|
2011-01-21 11:50:02 +00:00
|
|
|
LOGP(DMSC, LOGL_NOTICE, "Not releasing connection due lack of connection.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-27 19:32:52 +00:00
|
|
|
msg = create_sccp_rlc(src, dst);
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
msc_send(fw, msg, IPAC_PROTO_SCCP);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
void msc_send_reset(struct msc_connection *fw)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
|
|
|
struct msgb *msg;
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
if (fw->msc_link_down) {
|
2011-01-21 11:50:02 +00:00
|
|
|
LOGP(DMSC, LOGL_NOTICE, "Not sending reset due lack of connection.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-19 21:29:41 +00:00
|
|
|
/* start the ping/pong but nothing else */
|
|
|
|
if (fw->mode == MSC_MODE_SERVER) {
|
|
|
|
LOGP(DMSC, LOGL_DEBUG, "Not sending BSSMAP resets in server mode.\n");
|
|
|
|
msc_ping_timeout(fw);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 19:32:52 +00:00
|
|
|
msg = create_reset();
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
msc_send(fw, msg, IPAC_PROTO_SCCP);
|
|
|
|
msc_ping_timeout(fw);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
static void msc_send_id_response(struct msc_connection *fw)
|
2010-07-27 19:32:52 +00:00
|
|
|
{
|
|
|
|
struct msgb *msg;
|
|
|
|
|
2013-03-19 21:29:41 +00:00
|
|
|
if (fw->mode == MSC_MODE_SERVER) {
|
|
|
|
LOGP(DMSC, LOGL_DEBUG,
|
|
|
|
"Not sending our token in server mode.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-27 19:32:52 +00:00
|
|
|
msg = msgb_alloc_headroom(4096, 128, "id resp");
|
|
|
|
msg->l2h = msgb_v_put(msg, IPAC_MSGT_ID_RESP);
|
2011-02-10 14:32:14 +00:00
|
|
|
msgb_l16tv_put(msg, strlen(fw->token) + 1,
|
|
|
|
IPAC_IDTAG_UNITNAME, (uint8_t *) fw->token);
|
2010-07-27 19:32:52 +00:00
|
|
|
|
2011-02-10 14:32:14 +00:00
|
|
|
msc_send(fw, msg, IPAC_PROTO_IPACCESS);
|
2010-07-27 19:32:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
void msc_send_direct(struct msc_connection *fw, struct msgb *msg)
|
2010-11-16 10:03:19 +00:00
|
|
|
{
|
2011-02-10 14:32:14 +00:00
|
|
|
return msc_send(fw, msg, IPAC_PROTO_SCCP);
|
2010-11-16 10:03:19 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
struct msc_connection *msc_connection_create(struct bsc_data *bsc, int mgcp)
|
|
|
|
{
|
|
|
|
struct msc_connection *msc;
|
|
|
|
|
|
|
|
msc = talloc_zero(NULL, struct msc_connection);
|
|
|
|
if (!msc) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Failed to allocate the MSC Connection.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-19 21:29:41 +00:00
|
|
|
msc->mode = MSC_MODE_CLIENT;
|
|
|
|
msc->port = 5000;
|
|
|
|
|
2011-05-08 08:29:23 +00:00
|
|
|
osmo_wqueue_init(&msc->msc_connection, 100);
|
2011-02-15 19:01:47 +00:00
|
|
|
msc->reconnect_timer.cb = msc_reconnect;
|
|
|
|
msc->reconnect_timer.data = msc;
|
|
|
|
msc->msc_connection.read_cb = ipaccess_a_fd_cb;
|
|
|
|
msc->msc_connection.write_cb = ipaccess_write_cb;
|
|
|
|
msc->msc_connection.bfd.data = msc;
|
2011-11-25 14:31:15 +00:00
|
|
|
msc->msc_connection.bfd.fd = -1;
|
2011-02-15 19:01:47 +00:00
|
|
|
msc->msc_link_down = 1;
|
|
|
|
|
|
|
|
/* handle the timeout */
|
2011-09-19 21:31:45 +00:00
|
|
|
msc->ping_time = -1;
|
2011-02-15 19:01:47 +00:00
|
|
|
msc->ping_timeout.cb = msc_ping_timeout;
|
|
|
|
msc->ping_timeout.data = msc;
|
|
|
|
msc->pong_timeout.cb = msc_pong_timeout;
|
|
|
|
msc->pong_timeout.data = msc;
|
|
|
|
|
|
|
|
/* create MGCP port */
|
2012-12-11 15:35:01 +00:00
|
|
|
if (mgcp && mgcp_create_port(&msc->mgcp_agent) != 0) {
|
2011-02-15 19:01:47 +00:00
|
|
|
LOGP(DMSC, LOGL_ERROR, "Failed to bind for the MGCP port.\n");
|
|
|
|
talloc_free(msc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-22 14:45:52 +00:00
|
|
|
llist_add_tail(&msc->entry, &bsc->mscs);
|
2011-02-15 19:01:47 +00:00
|
|
|
msc->nr = bsc->num_mscs++;
|
2012-12-11 15:35:01 +00:00
|
|
|
msc->mgcp_agent.read_cb = msc_mgcp_read_cb;
|
2011-02-15 19:01:47 +00:00
|
|
|
|
|
|
|
return msc;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct msc_connection *msc_connection_num(struct bsc_data *bsc, int num)
|
|
|
|
{
|
|
|
|
struct msc_connection *msc;
|
|
|
|
|
|
|
|
llist_for_each_entry(msc, &bsc->mscs, entry)
|
|
|
|
if (msc->nr == num)
|
|
|
|
return msc;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msc_connection_start(struct msc_connection *msc)
|
|
|
|
{
|
|
|
|
if (msc->msc_connection.bfd.fd > 0) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR,
|
|
|
|
"Function should not be called with active connection.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-19 21:29:41 +00:00
|
|
|
/* bind and wait if we are a server */
|
|
|
|
if (msc->mode == MSC_MODE_SERVER)
|
|
|
|
return msc_conn_bind(msc);
|
|
|
|
|
2011-02-15 19:01:47 +00:00
|
|
|
msc_schedule_reconnect(msc);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-19 21:29:41 +00:00
|
|
|
|
|
|
|
const char *msc_mode(struct msc_connection *msc)
|
|
|
|
{
|
|
|
|
switch (msc->mode) {
|
|
|
|
case MSC_MODE_CLIENT:
|
|
|
|
return "client";
|
|
|
|
case MSC_MODE_SERVER:
|
|
|
|
return "server";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Non-clean MSC server socket abstraction.. bind and accept */
|
|
|
|
static int msc_send_auth_req(struct msc_connection *msc)
|
|
|
|
{
|
|
|
|
struct msgb *msg;
|
|
|
|
|
|
|
|
static const uint8_t id_req[] = {
|
|
|
|
IPAC_MSGT_ID_GET,
|
|
|
|
0x01, IPAC_IDTAG_UNIT,
|
|
|
|
0x01, IPAC_IDTAG_MACADDR,
|
|
|
|
0x01, IPAC_IDTAG_LOCATION1,
|
|
|
|
0x01, IPAC_IDTAG_LOCATION2,
|
|
|
|
0x01, IPAC_IDTAG_EQUIPVERS,
|
|
|
|
0x01, IPAC_IDTAG_SWVERSION,
|
|
|
|
0x01, IPAC_IDTAG_UNITNAME,
|
|
|
|
0x01, IPAC_IDTAG_SERNR,
|
|
|
|
};
|
|
|
|
|
|
|
|
msg = msgb_alloc_headroom(4096, 128, "auth");
|
|
|
|
if (!msg) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Failed to allocate auth.\n");
|
|
|
|
msc_close_connection(msc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->l2h = msgb_put(msg, ARRAY_SIZE(id_req));
|
|
|
|
memcpy(msg->l2h, id_req, ARRAY_SIZE(id_req));
|
|
|
|
|
|
|
|
msc_send(msc, msg, IPAC_PROTO_IPACCESS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void msc_handle_id_response(struct msc_connection *msc, struct msgb *msg)
|
|
|
|
{
|
|
|
|
unsigned int len;
|
|
|
|
const char *token;
|
|
|
|
|
|
|
|
/* only for the server */
|
|
|
|
if (msc->mode != MSC_MODE_SERVER) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Unexpected ID response for client.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!msc->token) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "No token defined. Giving up.\n");
|
|
|
|
goto clean;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msgb_l2len(msg) < 4) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Too short message...%u\n",
|
|
|
|
msgb_l2len(msg));
|
|
|
|
goto clean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* in lack of ipaccess_idtag_parse we have a very basic method */
|
|
|
|
if (msg->l2h[3] != IPAC_IDTAG_UNITNAME) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Expected unitname tag got %d\n",
|
|
|
|
msg->l2h[3]);
|
|
|
|
goto clean;
|
|
|
|
}
|
|
|
|
|
|
|
|
token = (const char *) &msg->l2h[4];
|
|
|
|
len = msgb_l2len(msg) - 4;
|
|
|
|
|
|
|
|
if (len != strlen(msc->token)) {
|
2016-08-15 11:24:52 +00:00
|
|
|
LOGP(DMSC, LOGL_ERROR, "Wrong length %u vs. %zu\n",
|
2013-03-19 21:29:41 +00:00
|
|
|
len, strlen(msc->token));
|
|
|
|
goto clean;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(msc->token, token, len) != 0) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Token has the wrong size.\n");
|
|
|
|
goto clean;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGP(DMSC, LOGL_NOTICE, "Authenticated the connection.\n");
|
|
|
|
msc->auth = 1;
|
2015-09-21 13:47:10 +00:00
|
|
|
ss7_application_msc_up(msc->app);
|
2013-03-19 21:29:41 +00:00
|
|
|
return;
|
|
|
|
clean:
|
|
|
|
msc_close_connection(msc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int msc_conn_accept(struct osmo_fd *bsc_fd, unsigned int what)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t len = sizeof(addr);
|
|
|
|
struct msc_connection *msc = bsc_fd->data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
LOGP(DMSC, LOGL_NOTICE, "Going to accept a connection.\n");
|
|
|
|
|
|
|
|
ret = accept(bsc_fd->fd, (struct sockaddr *) &addr, &len);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Accept failed with fd(%d) errno(%d)\n",
|
|
|
|
ret, errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close the previous/current connection.
|
|
|
|
* TODO: switch only once we know it is a valid connection
|
|
|
|
*/
|
|
|
|
msc_close_connection(msc);
|
|
|
|
|
|
|
|
/* re-set the internal state */
|
|
|
|
msc->auth = 0;
|
|
|
|
|
|
|
|
/* adopt the connection */
|
|
|
|
msc->msc_connection.bfd.fd = ret;
|
|
|
|
msc->msc_connection.bfd.when = BSC_FD_READ;
|
|
|
|
ret = osmo_fd_register(&msc->msc_connection.bfd);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Failed to register fd.\n");
|
|
|
|
close(msc->msc_connection.bfd.fd);
|
|
|
|
msc->msc_connection.bfd.fd = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* consider it up and running */
|
|
|
|
msc->msc_link_down = 0;
|
|
|
|
|
|
|
|
/* msc send auth request */
|
|
|
|
msc_send_auth_req(msc);
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Registered fd %d and waiting for data.\n",
|
|
|
|
msc->msc_connection.bfd.fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int msc_conn_bind(struct msc_connection *msc)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
LOGP(DMSC, LOGL_NOTICE, "Going to bind and wait for connections.\n");
|
|
|
|
|
|
|
|
rc = osmo_sock_init_ofd(&msc->listen_fd, AF_UNSPEC, SOCK_STREAM,
|
|
|
|
IPPROTO_TCP, "127.0.0.1", msc->port, OSMO_SOCK_F_BIND);
|
|
|
|
if (rc < 0) {
|
|
|
|
LOGP(DMSC, LOGL_NOTICE, "Failed to bind the socket.\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
msc->listen_fd.data = msc;
|
|
|
|
msc->listen_fd.cb = msc_conn_accept;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|