2010-06-15 10:44:42 +00:00
|
|
|
/* BSC Multiplexer/NAT */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
|
2010-06-15 10:46:56 +00:00
|
|
|
* (C) 2010 by On-Waves
|
2010-01-12 20:34:54 +00:00
|
|
|
* (C) 2009 by Harald Welte <laforge@gnumonks.org>
|
2010-06-15 10:44:42 +00:00
|
|
|
* 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 <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2010-01-12 20:34:54 +00:00
|
|
|
#include <errno.h>
|
2010-01-12 20:15:08 +00:00
|
|
|
#include <signal.h>
|
2010-06-15 10:44:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2010-01-12 20:36:08 +00:00
|
|
|
#include <time.h>
|
2010-06-15 10:44:42 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <openbsc/debug.h>
|
2010-01-12 20:15:08 +00:00
|
|
|
#include <openbsc/bsc_msc.h>
|
2010-06-15 10:45:26 +00:00
|
|
|
#include <openbsc/bsc_nat.h>
|
2010-01-30 11:45:10 +00:00
|
|
|
#include <openbsc/bssap.h>
|
2010-01-12 20:15:08 +00:00
|
|
|
#include <openbsc/ipaccess.h>
|
|
|
|
#include <openbsc/abis_nm.h>
|
2010-06-15 10:46:11 +00:00
|
|
|
#include <openbsc/telnet_interface.h>
|
|
|
|
|
2010-06-15 11:06:18 +00:00
|
|
|
#include <osmocore/talloc.h>
|
|
|
|
|
2010-06-15 10:46:11 +00:00
|
|
|
#include <vty/vty.h>
|
2010-06-15 10:44:42 +00:00
|
|
|
|
2010-06-15 10:45:26 +00:00
|
|
|
#include <sccp/sccp.h>
|
|
|
|
|
2010-06-15 11:14:12 +00:00
|
|
|
struct log_target *stderr_target;
|
2010-06-15 10:46:11 +00:00
|
|
|
static const char *config_file = "bsc-nat.cfg";
|
2010-06-15 10:44:42 +00:00
|
|
|
static char *msc_address = "127.0.0.1";
|
|
|
|
static struct in_addr local_addr;
|
2010-06-15 10:47:29 +00:00
|
|
|
static struct bsc_msc_connection *msc_con;
|
2010-03-26 05:51:04 +00:00
|
|
|
static struct bsc_fd bsc_listen;
|
2010-01-12 20:15:08 +00:00
|
|
|
|
2010-01-13 08:28:12 +00:00
|
|
|
|
2010-06-15 10:46:11 +00:00
|
|
|
static struct bsc_nat *nat;
|
2010-04-08 08:24:57 +00:00
|
|
|
static void bsc_send_data(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length, int);
|
2010-03-29 06:04:09 +00:00
|
|
|
static void remove_bsc_connection(struct bsc_connection *connection);
|
2010-04-07 09:20:36 +00:00
|
|
|
static void msc_send_reset(struct bsc_msc_connection *con);
|
2010-06-15 10:46:11 +00:00
|
|
|
|
|
|
|
struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
|
|
|
|
{
|
|
|
|
struct bsc_config *conf;
|
|
|
|
|
|
|
|
llist_for_each_entry(conf, &nat->bsc_configs, entry)
|
|
|
|
if (conf->nr == num)
|
|
|
|
return conf;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-13 08:28:12 +00:00
|
|
|
|
2010-01-12 20:15:08 +00:00
|
|
|
/*
|
|
|
|
* below are stubs we need to link
|
|
|
|
*/
|
|
|
|
int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
|
|
|
|
struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
|
|
|
|
{}
|
|
|
|
|
|
|
|
int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-26 08:18:02 +00:00
|
|
|
static void send_reset_ack(struct bsc_connection *bsc)
|
2010-01-30 11:45:10 +00:00
|
|
|
{
|
|
|
|
static const u_int8_t gsm_reset_ack[] = {
|
|
|
|
0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
|
|
|
|
0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
|
|
|
|
0x00, 0x01, 0x31,
|
|
|
|
};
|
|
|
|
|
2010-04-08 08:24:57 +00:00
|
|
|
bsc_send_data(bsc, gsm_reset_ack, sizeof(gsm_reset_ack), IPAC_PROTO_SCCP);
|
2010-01-30 11:45:10 +00:00
|
|
|
}
|
|
|
|
|
2010-03-26 08:18:02 +00:00
|
|
|
static void send_id_ack(struct bsc_connection *bsc)
|
2010-03-26 06:41:54 +00:00
|
|
|
{
|
|
|
|
static const u_int8_t id_ack[] = {
|
2010-04-08 08:24:57 +00:00
|
|
|
IPAC_MSGT_ID_ACK
|
2010-03-26 06:41:54 +00:00
|
|
|
};
|
|
|
|
|
2010-04-08 08:24:57 +00:00
|
|
|
bsc_send_data(bsc, id_ack, sizeof(id_ack), IPAC_PROTO_IPACCESS);
|
2010-03-26 06:41:54 +00:00
|
|
|
}
|
|
|
|
|
2010-03-26 08:18:02 +00:00
|
|
|
static void send_id_req(struct bsc_connection *bsc)
|
2010-03-26 06:41:54 +00:00
|
|
|
{
|
|
|
|
static const u_int8_t id_req[] = {
|
2010-04-08 08:24:57 +00:00
|
|
|
IPAC_MSGT_ID_GET,
|
2010-03-26 06:41:54 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2010-04-08 08:24:57 +00:00
|
|
|
bsc_send_data(bsc, id_req, sizeof(id_req), IPAC_PROTO_IPACCESS);
|
2010-03-26 06:41:54 +00:00
|
|
|
}
|
|
|
|
|
2010-04-06 15:32:58 +00:00
|
|
|
static void nat_send_rlsd(struct sccp_connections *conn)
|
|
|
|
{
|
|
|
|
struct sccp_connection_released *rel;
|
|
|
|
struct msgb *msg;
|
|
|
|
|
|
|
|
msg = msgb_alloc_headroom(4096, 128, "rlsd");
|
|
|
|
if (!msg) {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->l2h = msgb_put(msg, sizeof(*rel));
|
|
|
|
rel = (struct sccp_connection_released *) msg->l2h;
|
|
|
|
rel->type = SCCP_MSG_TYPE_RLSD;
|
|
|
|
rel->release_cause = SCCP_RELEASE_CAUSE_SCCP_FAILURE;
|
|
|
|
rel->destination_local_reference = conn->remote_ref;
|
|
|
|
rel->source_local_reference = conn->patched_ref;
|
|
|
|
|
|
|
|
ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
|
|
|
|
|
|
|
|
if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
|
|
|
|
LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
|
|
|
|
msgb_free(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-08 09:28:12 +00:00
|
|
|
static void nat_send_rlc(struct sccp_source_reference *src,
|
|
|
|
struct sccp_source_reference *dst)
|
|
|
|
{
|
|
|
|
struct sccp_connection_release_complete *rlc;
|
|
|
|
struct msgb *msg;
|
|
|
|
|
|
|
|
msg = msgb_alloc_headroom(4096, 128, "rlc");
|
|
|
|
if (!msg) {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Failed to allocate clear command.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->l2h = msgb_put(msg, sizeof(*rlc));
|
|
|
|
rlc = (struct sccp_connection_release_complete *) msg->l2h;
|
|
|
|
rlc->type = SCCP_MSG_TYPE_RLC;
|
|
|
|
rlc->destination_local_reference = *dst;
|
|
|
|
rlc->source_local_reference = *src;
|
|
|
|
|
|
|
|
ipaccess_prepend_header(msg, IPAC_PROTO_SCCP);
|
|
|
|
|
|
|
|
if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
|
|
|
|
LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
|
|
|
|
msgb_free(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-31 05:30:58 +00:00
|
|
|
static void send_mgcp_reset(struct bsc_connection *bsc)
|
|
|
|
{
|
|
|
|
static const u_int8_t mgcp_reset[] = {
|
|
|
|
"RSIP 1 13@mgw MGCP 1.0\r\n"
|
|
|
|
};
|
|
|
|
|
|
|
|
bsc_write_mgcp(bsc, mgcp_reset, sizeof mgcp_reset - 1);
|
|
|
|
}
|
|
|
|
|
2010-01-12 20:15:08 +00:00
|
|
|
/*
|
|
|
|
* Below is the handling of messages coming
|
|
|
|
* from the MSC and need to be forwarded to
|
|
|
|
* a real BSC.
|
|
|
|
*/
|
|
|
|
static void initialize_msc_if_needed()
|
|
|
|
{
|
2010-04-07 09:20:36 +00:00
|
|
|
if (nat->first_contact)
|
|
|
|
return;
|
2010-01-12 20:15:08 +00:00
|
|
|
|
2010-04-07 09:20:36 +00:00
|
|
|
nat->first_contact = 1;
|
|
|
|
msc_send_reset(msc_con);
|
2010-01-12 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
2010-03-26 08:18:02 +00:00
|
|
|
/*
|
|
|
|
* Currently we are lacking refcounting so we need to copy each message.
|
|
|
|
*/
|
2010-04-08 08:24:57 +00:00
|
|
|
static void bsc_send_data(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length, int proto)
|
2010-03-26 06:20:59 +00:00
|
|
|
{
|
2010-03-26 08:18:02 +00:00
|
|
|
struct msgb *msg;
|
|
|
|
|
2010-04-08 08:24:57 +00:00
|
|
|
if (length > 4096 - 128) {
|
2010-03-26 08:18:02 +00:00
|
|
|
LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-08 08:24:57 +00:00
|
|
|
msg = msgb_alloc_headroom(4096, 128, "to-bsc");
|
2010-03-26 08:18:02 +00:00
|
|
|
if (!msg) {
|
|
|
|
LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-08 08:24:57 +00:00
|
|
|
msg->l2h = msgb_put(msg, length);
|
2010-03-26 08:18:02 +00:00
|
|
|
memcpy(msg->data, data, length);
|
2010-04-08 08:24:57 +00:00
|
|
|
|
|
|
|
bsc_write(bsc, msg, proto);
|
2010-03-26 06:20:59 +00:00
|
|
|
}
|
|
|
|
|
2010-01-31 08:46:21 +00:00
|
|
|
static int forward_sccp_to_bts(struct msgb *msg)
|
2010-01-12 20:15:08 +00:00
|
|
|
{
|
2010-06-15 10:48:55 +00:00
|
|
|
struct sccp_connections *con;
|
|
|
|
struct bsc_connection *bsc;
|
2010-06-15 10:45:38 +00:00
|
|
|
struct bsc_nat_parsed *parsed;
|
2010-04-08 08:24:57 +00:00
|
|
|
int proto;
|
2010-01-13 08:28:12 +00:00
|
|
|
|
2010-01-12 20:15:08 +00:00
|
|
|
/* filter, drop, patch the message? */
|
2010-06-15 10:45:38 +00:00
|
|
|
parsed = bsc_nat_parse(msg);
|
|
|
|
if (!parsed) {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
|
2010-01-31 08:46:21 +00:00
|
|
|
return -1;
|
2010-06-15 10:45:38 +00:00
|
|
|
}
|
|
|
|
|
2010-01-30 10:53:30 +00:00
|
|
|
if (bsc_nat_filter_ipa(DIR_BSC, msg, parsed))
|
2010-06-15 10:45:38 +00:00
|
|
|
goto exit;
|
2010-06-15 10:45:26 +00:00
|
|
|
|
2010-04-08 08:24:57 +00:00
|
|
|
proto = parsed->ipa_proto;
|
|
|
|
|
2010-01-31 08:46:21 +00:00
|
|
|
/* Route and modify the SCCP packet */
|
2010-04-08 08:24:57 +00:00
|
|
|
if (proto == IPAC_PROTO_SCCP) {
|
2010-01-31 08:46:21 +00:00
|
|
|
switch (parsed->sccp_type) {
|
|
|
|
case SCCP_MSG_TYPE_UDT:
|
|
|
|
/* forward UDT messages to every BSC */
|
|
|
|
goto send_to_all;
|
|
|
|
break;
|
|
|
|
case SCCP_MSG_TYPE_RLSD:
|
|
|
|
case SCCP_MSG_TYPE_CREF:
|
|
|
|
case SCCP_MSG_TYPE_DT1:
|
2010-04-06 08:22:34 +00:00
|
|
|
case SCCP_MSG_TYPE_IT:
|
2010-06-15 10:48:55 +00:00
|
|
|
con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
|
2010-06-15 10:49:53 +00:00
|
|
|
if (parsed->gsm_type == BSS_MAP_MSG_ASSIGMENT_RQST) {
|
2010-04-12 10:17:09 +00:00
|
|
|
counter_inc(nat->stats.sccp.calls);
|
|
|
|
|
2010-06-15 10:49:53 +00:00
|
|
|
if (con) {
|
2010-04-12 10:17:09 +00:00
|
|
|
counter_inc(con->bsc->cfg->stats.sccp.conn);
|
2010-06-15 10:49:53 +00:00
|
|
|
if (bsc_mgcp_assign(con, msg) != 0)
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
|
|
|
|
} else
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");
|
|
|
|
}
|
2010-03-29 15:18:42 +00:00
|
|
|
break;
|
2010-01-31 08:46:21 +00:00
|
|
|
case SCCP_MSG_TYPE_CC:
|
2010-06-15 10:48:55 +00:00
|
|
|
con = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
|
|
|
|
if (!con || update_sccp_src_ref(con, parsed) != 0)
|
2010-03-29 15:18:42 +00:00
|
|
|
goto exit;
|
2010-01-31 08:46:21 +00:00
|
|
|
break;
|
|
|
|
case SCCP_MSG_TYPE_RLC:
|
2010-06-15 10:47:49 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "Unexpected release complete from MSC.\n");
|
|
|
|
goto exit;
|
|
|
|
break;
|
|
|
|
case SCCP_MSG_TYPE_CR:
|
2010-01-31 08:46:21 +00:00
|
|
|
/* MSC never opens a SCCP connection, fall through */
|
|
|
|
default:
|
|
|
|
goto exit;
|
|
|
|
}
|
2010-04-06 14:07:44 +00:00
|
|
|
|
2010-04-08 09:28:12 +00:00
|
|
|
if (!con && parsed->sccp_type == SCCP_MSG_TYPE_RLSD) {
|
|
|
|
LOGP(DNAT, LOGL_NOTICE, "Sending fake RLC on RLSD message to network.\n");
|
|
|
|
/* Exchange src/dest for the reply */
|
|
|
|
nat_send_rlc(parsed->dest_local_ref, parsed->src_local_ref);
|
|
|
|
} else if (!con)
|
2010-04-06 14:07:44 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "Unknown connection for msg type: 0x%x.\n", parsed->sccp_type);
|
2010-01-31 08:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
talloc_free(parsed);
|
2010-06-15 10:48:55 +00:00
|
|
|
if (!con)
|
2010-01-31 08:46:21 +00:00
|
|
|
return -1;
|
2010-06-15 10:48:55 +00:00
|
|
|
if (!con->bsc->authenticated) {
|
2010-06-15 11:06:18 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "Selected BSC not authenticated.\n");
|
2010-02-08 22:24:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-08 08:24:57 +00:00
|
|
|
bsc_send_data(con->bsc, msg->l2h, msgb_l2len(msg), proto);
|
2010-03-26 08:18:02 +00:00
|
|
|
return 0;
|
2010-01-31 08:46:21 +00:00
|
|
|
|
|
|
|
send_to_all:
|
2010-06-15 10:46:36 +00:00
|
|
|
/*
|
|
|
|
* Filter Paging from the network. We do not want to send a PAGING
|
|
|
|
* Command to every BSC in our network. We will analys the PAGING
|
|
|
|
* message and then send it to the authenticated messages...
|
|
|
|
*/
|
|
|
|
if (parsed->ipa_proto == IPAC_PROTO_SCCP && parsed->gsm_type == BSS_MAP_MSG_PAGING) {
|
2010-03-30 03:57:42 +00:00
|
|
|
bsc = bsc_nat_find_bsc(nat, msg);
|
|
|
|
if (bsc)
|
2010-04-08 08:24:57 +00:00
|
|
|
bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
|
2010-03-30 03:57:42 +00:00
|
|
|
else
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Could not determine BSC for paging.\n");
|
2010-06-15 10:46:36 +00:00
|
|
|
|
|
|
|
goto exit;
|
|
|
|
}
|
2010-01-13 08:28:12 +00:00
|
|
|
/* currently send this to every BSC connected */
|
2010-06-15 10:46:11 +00:00
|
|
|
llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
|
2010-02-08 22:24:32 +00:00
|
|
|
if (!bsc->authenticated)
|
|
|
|
continue;
|
|
|
|
|
2010-04-08 08:24:57 +00:00
|
|
|
bsc_send_data(bsc, msg->l2h, msgb_l2len(msg), parsed->ipa_proto);
|
2010-01-13 08:28:12 +00:00
|
|
|
}
|
2010-06-15 10:45:38 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
talloc_free(parsed);
|
2010-01-31 08:46:21 +00:00
|
|
|
return 0;
|
2010-01-12 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
2010-06-15 10:47:29 +00:00
|
|
|
static void msc_connection_was_lost(struct bsc_msc_connection *con)
|
|
|
|
{
|
2010-03-29 06:04:09 +00:00
|
|
|
struct bsc_connection *bsc, *tmp;
|
|
|
|
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Closing all connections downstream.\n");
|
|
|
|
llist_for_each_entry_safe(bsc, tmp, &nat->bsc_connections, list_entry)
|
|
|
|
remove_bsc_connection(bsc);
|
|
|
|
|
2010-04-07 09:20:36 +00:00
|
|
|
nat->first_contact = 0;
|
2010-03-31 07:16:56 +00:00
|
|
|
bsc_mgcp_free_endpoints(nat);
|
2010-03-29 06:04:09 +00:00
|
|
|
bsc_msc_schedule_connect(con);
|
2010-06-15 10:47:29 +00:00
|
|
|
}
|
|
|
|
|
2010-04-08 08:31:07 +00:00
|
|
|
static void msc_send_reset(struct bsc_msc_connection *msc_con)
|
2010-04-07 08:46:30 +00:00
|
|
|
{
|
|
|
|
static const u_int8_t reset[] = {
|
2010-04-07 09:11:11 +00:00
|
|
|
0x00, 0x12, 0xfd,
|
2010-04-07 08:46:30 +00:00
|
|
|
0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
|
|
|
|
0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
|
|
|
|
0x01, 0x20
|
|
|
|
};
|
|
|
|
|
|
|
|
struct msgb *msg;
|
|
|
|
|
|
|
|
msg = msgb_alloc_headroom(4096, 128, "08.08 reset");
|
|
|
|
if (!msg) {
|
|
|
|
LOGP(DMSC, LOGL_ERROR, "Failed to allocate reset msg.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->l2h = msgb_put(msg, sizeof(reset));
|
|
|
|
memcpy(msg->l2h, reset, msgb_l2len(msg));
|
|
|
|
|
2010-04-08 08:31:07 +00:00
|
|
|
if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
|
2010-04-07 08:46:30 +00:00
|
|
|
LOGP(DMSC, LOGL_ERROR, "Failed to enqueue reset msg.\n");
|
|
|
|
msgb_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGP(DMSC, LOGL_NOTICE, "Scheduled GSM0808 reset msg for the MSC.\n");
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:47:02 +00:00
|
|
|
static int ipaccess_msc_read_cb(struct bsc_fd *bfd)
|
2010-01-12 20:15:08 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct msgb *msg = ipaccess_read_msg(bfd, &error);
|
|
|
|
struct ipaccess_head *hh;
|
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
if (error == 0) {
|
2010-01-29 04:58:43 +00:00
|
|
|
LOGP(DNAT, LOGL_FATAL, "The connection the MSC was lost, exiting\n");
|
2010-06-15 10:47:29 +00:00
|
|
|
bsc_msc_lost(msc_con);
|
|
|
|
return -1;
|
2010-01-12 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
2010-01-29 04:58:43 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
|
2010-01-12 20:15:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-29 04:58:43 +00:00
|
|
|
LOGP(DNAT, LOGL_DEBUG, "MSG from MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
|
2010-01-12 20:15:08 +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 && msg->l2h[0] == IPAC_MSGT_ID_ACK)
|
|
|
|
initialize_msc_if_needed();
|
|
|
|
else if (hh->proto == IPAC_PROTO_SCCP)
|
|
|
|
forward_sccp_to_bts(msg);
|
|
|
|
|
2010-06-15 10:46:48 +00:00
|
|
|
msgb_free(msg);
|
2010-01-12 20:15:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-06-15 10:44:42 +00:00
|
|
|
|
2010-06-15 10:47:02 +00:00
|
|
|
static int ipaccess_msc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
rc = write(bfd->fd, msg->data, msg->len);
|
|
|
|
|
|
|
|
if (rc != msg->len) {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Failed to write MSG to MSC.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2010-01-12 20:34:54 +00:00
|
|
|
/*
|
|
|
|
* Below is the handling of messages coming
|
|
|
|
* from the BSC and need to be forwarded to
|
|
|
|
* a real BSC.
|
|
|
|
*/
|
2010-01-13 08:28:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the connection from the connections list,
|
|
|
|
* remove it from the patching of SCCP header lists
|
|
|
|
* as well. Maybe in the future even close connection..
|
|
|
|
*/
|
|
|
|
static void remove_bsc_connection(struct bsc_connection *connection)
|
|
|
|
{
|
2010-01-31 08:46:21 +00:00
|
|
|
struct sccp_connections *sccp_patch, *tmp;
|
2010-06-15 10:47:10 +00:00
|
|
|
bsc_unregister_fd(&connection->write_queue.bfd);
|
|
|
|
close(connection->write_queue.bfd.fd);
|
2010-03-26 08:27:08 +00:00
|
|
|
write_queue_clear(&connection->write_queue);
|
2010-01-13 08:28:12 +00:00
|
|
|
llist_del(&connection->list_entry);
|
2010-01-31 08:46:21 +00:00
|
|
|
|
2010-06-15 10:46:19 +00:00
|
|
|
/* stop the timeout timer */
|
|
|
|
bsc_del_timer(&connection->id_timeout);
|
|
|
|
|
2010-01-31 08:46:21 +00:00
|
|
|
/* remove all SCCP connections */
|
2010-06-15 10:46:11 +00:00
|
|
|
llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
|
2010-01-31 08:46:21 +00:00
|
|
|
if (sccp_patch->bsc != connection)
|
|
|
|
continue;
|
|
|
|
|
2010-04-06 15:32:58 +00:00
|
|
|
nat_send_rlsd(sccp_patch);
|
2010-03-30 08:45:48 +00:00
|
|
|
sccp_connection_destroy(sccp_patch);
|
2010-01-31 08:46:21 +00:00
|
|
|
}
|
|
|
|
|
2010-04-05 21:09:27 +00:00
|
|
|
/* close endpoints allocated by this BSC */
|
|
|
|
bsc_mgcp_clear_endpoints_for(connection);
|
|
|
|
|
2010-01-13 08:28:12 +00:00
|
|
|
talloc_free(connection);
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:46:19 +00:00
|
|
|
static void ipaccess_close_bsc(void *data)
|
|
|
|
{
|
|
|
|
struct bsc_connection *conn = data;
|
|
|
|
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "BSC didn't respond to identity request. Closing.\n");
|
|
|
|
remove_bsc_connection(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ipaccess_auth_bsc(struct tlv_parsed *tvp, struct bsc_connection *bsc)
|
|
|
|
{
|
|
|
|
struct bsc_config *conf;
|
|
|
|
const char* token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
|
|
|
|
|
|
|
|
llist_for_each_entry(conf, &bsc->nat->bsc_configs, entry) {
|
|
|
|
if (strcmp(conf->token, token) == 0) {
|
2010-04-12 10:17:09 +00:00
|
|
|
counter_inc(conf->stats.net.reconn);
|
2010-06-15 10:46:19 +00:00
|
|
|
bsc->authenticated = 1;
|
2010-04-06 13:11:34 +00:00
|
|
|
bsc->cfg = conf;
|
2010-06-15 10:46:19 +00:00
|
|
|
bsc_del_timer(&bsc->id_timeout);
|
2010-04-06 13:11:34 +00:00
|
|
|
LOGP(DNAT, LOGL_NOTICE, "Authenticated bsc nr: %d lac: %d\n", conf->nr, conf->lac);
|
2010-06-15 10:46:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-26 06:24:34 +00:00
|
|
|
static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg)
|
2010-01-13 08:28:12 +00:00
|
|
|
{
|
2010-06-15 10:48:55 +00:00
|
|
|
struct sccp_connections *con;
|
2010-06-15 10:45:38 +00:00
|
|
|
struct bsc_nat_parsed *parsed;
|
2010-01-13 08:28:12 +00:00
|
|
|
|
2010-06-15 10:45:38 +00:00
|
|
|
/* Parse and filter messages */
|
|
|
|
parsed = bsc_nat_parse(msg);
|
|
|
|
if (!parsed) {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Can not parse msg from BSC.\n");
|
2010-03-30 05:35:46 +00:00
|
|
|
msgb_free(msg);
|
2010-06-15 10:45:38 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-30 10:53:30 +00:00
|
|
|
if (bsc_nat_filter_ipa(DIR_MSC, msg, parsed))
|
2010-06-15 10:45:38 +00:00
|
|
|
goto exit;
|
2010-06-15 10:45:26 +00:00
|
|
|
|
2010-04-02 01:42:44 +00:00
|
|
|
/*
|
|
|
|
* check authentication after filtering to not reject auth
|
|
|
|
* responses coming from the BSC. We have to make sure that
|
|
|
|
* nothing from the exit path will forward things to the MSC
|
|
|
|
*/
|
|
|
|
if (!bsc->authenticated) {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "BSC is not authenticated.\n");
|
|
|
|
msgb_free(msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-31 08:46:21 +00:00
|
|
|
/* modify the SCCP entries */
|
|
|
|
if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
|
|
|
|
switch (parsed->sccp_type) {
|
|
|
|
case SCCP_MSG_TYPE_CR:
|
|
|
|
if (create_sccp_src_ref(bsc, msg, parsed) != 0)
|
|
|
|
goto exit2;
|
2010-06-15 10:48:55 +00:00
|
|
|
con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
|
2010-01-31 08:46:21 +00:00
|
|
|
break;
|
|
|
|
case SCCP_MSG_TYPE_RLSD:
|
|
|
|
case SCCP_MSG_TYPE_CREF:
|
|
|
|
case SCCP_MSG_TYPE_DT1:
|
|
|
|
case SCCP_MSG_TYPE_CC:
|
2010-04-06 08:22:34 +00:00
|
|
|
case SCCP_MSG_TYPE_IT:
|
2010-06-15 10:48:55 +00:00
|
|
|
con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
|
2010-01-31 08:46:21 +00:00
|
|
|
break;
|
|
|
|
case SCCP_MSG_TYPE_RLC:
|
2010-06-15 10:48:55 +00:00
|
|
|
con = patch_sccp_src_ref_to_msc(msg, parsed, nat);
|
2010-03-31 07:15:05 +00:00
|
|
|
remove_sccp_src_ref(bsc, msg, parsed);
|
2010-01-31 08:46:21 +00:00
|
|
|
break;
|
|
|
|
case SCCP_MSG_TYPE_UDT:
|
|
|
|
/* simply forward everything */
|
2010-06-15 10:48:55 +00:00
|
|
|
con = NULL;
|
2010-01-31 08:46:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-06-15 10:47:49 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "Not forwarding to msc sccp type: 0x%x\n", parsed->sccp_type);
|
2010-06-15 10:48:55 +00:00
|
|
|
con = NULL;
|
2010-01-31 08:46:21 +00:00
|
|
|
goto exit2;
|
|
|
|
break;
|
|
|
|
}
|
2010-04-01 08:16:28 +00:00
|
|
|
} else if (parsed->ipa_proto == NAT_IPAC_PROTO_MGCP) {
|
|
|
|
bsc_mgcp_forward(bsc, msg);
|
|
|
|
goto exit2;
|
2010-06-15 10:48:55 +00:00
|
|
|
} else {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Not forwarding unknown stream id: 0x%x\n", parsed->ipa_proto);
|
|
|
|
goto exit2;
|
2010-01-31 08:46:21 +00:00
|
|
|
}
|
|
|
|
|
2010-06-15 10:48:55 +00:00
|
|
|
if (con && con->bsc != bsc) {
|
2010-01-31 08:46:21 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "Found the wrong entry.\n");
|
|
|
|
goto exit2;
|
|
|
|
}
|
|
|
|
|
2010-01-13 08:28:12 +00:00
|
|
|
/* send the non-filtered but maybe modified msg */
|
2010-06-15 10:47:29 +00:00
|
|
|
if (write_queue_enqueue(&msc_con->write_queue, msg) != 0) {
|
2010-06-15 10:47:02 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "Can not queue message for the MSC.\n");
|
|
|
|
msgb_free(msg);
|
|
|
|
}
|
2010-01-30 11:45:10 +00:00
|
|
|
talloc_free(parsed);
|
2010-06-15 10:47:02 +00:00
|
|
|
return 0;
|
2010-06-15 10:45:38 +00:00
|
|
|
|
|
|
|
exit:
|
2010-01-30 11:45:10 +00:00
|
|
|
/* if we filter out the reset send an ack to the BSC */
|
|
|
|
if (parsed->bssap == 0 && parsed->gsm_type == BSS_MAP_MSG_RESET) {
|
2010-03-26 06:24:34 +00:00
|
|
|
send_reset_ack(bsc);
|
|
|
|
send_reset_ack(bsc);
|
2010-06-15 10:46:19 +00:00
|
|
|
} else if (parsed->ipa_proto == IPAC_PROTO_IPACCESS) {
|
|
|
|
/* do we know who is handling this? */
|
|
|
|
if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
|
|
|
|
struct tlv_parsed tvp;
|
|
|
|
ipaccess_idtag_parse(&tvp,
|
|
|
|
(unsigned char *) msg->l2h + 2,
|
|
|
|
msgb_l2len(msg) - 2);
|
|
|
|
if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
|
|
|
|
ipaccess_auth_bsc(&tvp, bsc);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto exit2;
|
2010-01-30 11:45:10 +00:00
|
|
|
}
|
|
|
|
|
2010-01-31 08:46:21 +00:00
|
|
|
exit2:
|
2010-06-15 10:45:38 +00:00
|
|
|
talloc_free(parsed);
|
2010-06-15 10:47:02 +00:00
|
|
|
msgb_free(msg);
|
|
|
|
return -1;
|
2010-01-13 08:28:12 +00:00
|
|
|
}
|
|
|
|
|
2010-06-15 10:47:10 +00:00
|
|
|
static int ipaccess_bsc_read_cb(struct bsc_fd *bfd)
|
2010-01-13 08:28:12 +00:00
|
|
|
{
|
|
|
|
int error;
|
2010-03-26 06:24:34 +00:00
|
|
|
struct bsc_connection *bsc = bfd->data;
|
2010-01-13 08:28:12 +00:00
|
|
|
struct msgb *msg = ipaccess_read_msg(bfd, &error);
|
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
if (error == 0) {
|
2010-01-29 04:58:43 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "The connection to the BSC was lost. Cleaning it\n");
|
2010-03-26 06:24:34 +00:00
|
|
|
remove_bsc_connection(bsc);
|
2010-01-29 04:58:43 +00:00
|
|
|
} else {
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
|
2010-01-13 08:28:12 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-29 04:58:43 +00:00
|
|
|
LOGP(DNAT, LOGL_DEBUG, "MSG from BSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
|
2010-01-13 08:28:12 +00:00
|
|
|
|
|
|
|
/* Handle messages from the BSC */
|
|
|
|
/* FIXME: Currently no PONG is sent to the BSC */
|
|
|
|
/* FIXME: Currently no ID ACK is sent to the BSC */
|
2010-03-26 06:24:34 +00:00
|
|
|
forward_sccp_to_msc(bsc, msg);
|
2010-01-13 08:28:12 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-26 08:18:02 +00:00
|
|
|
static int ipaccess_bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = write(bfd->fd, msg->data, msg->len);
|
|
|
|
if (rc != msg->len)
|
|
|
|
LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n");
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2010-01-12 20:34:54 +00:00
|
|
|
static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
|
|
|
|
{
|
2010-01-13 08:28:12 +00:00
|
|
|
struct bsc_connection *bsc;
|
2010-01-12 20:34:54 +00:00
|
|
|
int ret;
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
socklen_t sa_len = sizeof(sa);
|
|
|
|
|
|
|
|
if (!(what & BSC_FD_READ))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("accept");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-04-12 10:17:09 +00:00
|
|
|
/* count the reconnect */
|
|
|
|
counter_inc(nat->stats.bsc.reconn);
|
|
|
|
|
2010-03-29 06:04:09 +00:00
|
|
|
/*
|
|
|
|
* if we are not connected to a msc... just close the socket
|
|
|
|
*/
|
|
|
|
if (!msc_con->is_connected) {
|
|
|
|
LOGP(DNAT, LOGL_NOTICE, "Disconnecting BSC due lack of MSC connection.\n");
|
2010-04-06 10:42:35 +00:00
|
|
|
close(ret);
|
2010-03-29 06:04:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-12 20:34:54 +00:00
|
|
|
/* todo... do something with the connection */
|
2010-01-12 20:35:32 +00:00
|
|
|
/* todo... use GNUtls to see if we want to trust this as a BTS */
|
2010-01-12 20:34:54 +00:00
|
|
|
|
2010-01-13 08:28:12 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
*/
|
2010-06-15 10:48:01 +00:00
|
|
|
bsc = bsc_connection_alloc(nat);
|
2010-01-13 08:28:12 +00:00
|
|
|
if (!bsc) {
|
2010-01-29 04:58:43 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
|
2010-01-13 08:28:12 +00:00
|
|
|
close(ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:47:10 +00:00
|
|
|
write_queue_init(&bsc->write_queue, 100);
|
|
|
|
bsc->write_queue.bfd.data = bsc;
|
|
|
|
bsc->write_queue.bfd.fd = ret;
|
|
|
|
bsc->write_queue.read_cb = ipaccess_bsc_read_cb;
|
2010-03-26 08:18:02 +00:00
|
|
|
bsc->write_queue.write_cb = ipaccess_bsc_write_cb;
|
2010-06-15 10:47:10 +00:00
|
|
|
bsc->write_queue.bfd.when = BSC_FD_READ;
|
|
|
|
if (bsc_register_fd(&bsc->write_queue.bfd) < 0) {
|
2010-01-29 04:58:43 +00:00
|
|
|
LOGP(DNAT, LOGL_ERROR, "Failed to register BSC fd.\n");
|
2010-01-13 08:28:12 +00:00
|
|
|
close(ret);
|
|
|
|
talloc_free(bsc);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2010-04-05 15:58:52 +00:00
|
|
|
LOGP(DNAT, LOGL_NOTICE, "Registered new BSC\n");
|
2010-06-15 10:46:11 +00:00
|
|
|
llist_add(&bsc->list_entry, &nat->bsc_connections);
|
2010-03-26 06:41:54 +00:00
|
|
|
send_id_ack(bsc);
|
|
|
|
send_id_req(bsc);
|
2010-03-31 05:30:58 +00:00
|
|
|
send_mgcp_reset(bsc);
|
2010-06-15 10:46:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* start the hangup timer
|
|
|
|
*/
|
|
|
|
bsc->id_timeout.data = bsc;
|
|
|
|
bsc->id_timeout.cb = ipaccess_close_bsc;
|
|
|
|
bsc_schedule_timer(&bsc->id_timeout, 2, 0);
|
2010-01-12 20:34:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int listen_for_bsc(struct bsc_fd *bfd, struct in_addr *in_addr, int port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int ret, on = 1;
|
|
|
|
|
|
|
|
bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
bfd->cb = ipaccess_listen_bsc_cb;
|
|
|
|
bfd->when = BSC_FD_READ;
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
addr.sin_addr.s_addr = in_addr->s_addr;
|
|
|
|
|
|
|
|
setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
|
|
|
|
|
|
|
ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Could not bind the BSC socket %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = listen(bfd->fd, 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("listen");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = bsc_register_fd(bfd);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("register_listen_fd");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:44:42 +00:00
|
|
|
static void print_usage()
|
|
|
|
{
|
|
|
|
printf("Usage: bsc_nat\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_help()
|
|
|
|
{
|
|
|
|
printf(" Some useful help...\n");
|
|
|
|
printf(" -h --help this text\n");
|
|
|
|
printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
|
|
|
|
printf(" -s --disable-color\n");
|
|
|
|
printf(" -c --config-file filename The config file to use.\n");
|
|
|
|
printf(" -m --msc=IP. The address of the MSC.\n");
|
2010-01-12 20:34:54 +00:00
|
|
|
printf(" -l --local=IP. The local address of this BSC.\n");
|
2010-06-15 10:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_options(int argc, char** argv)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0, c;
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{"debug", 1, 0, 'd'},
|
|
|
|
{"config-file", 1, 0, 'c'},
|
|
|
|
{"disable-color", 0, 0, 's'},
|
|
|
|
{"timestamp", 0, 0, 'T'},
|
|
|
|
{"msc", 1, 0, 'm'},
|
|
|
|
{"local", 1, 0, 'l'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "hd:sTPc:m:l:",
|
|
|
|
long_options, &option_index);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
print_usage();
|
|
|
|
print_help();
|
|
|
|
exit(0);
|
|
|
|
case 's':
|
2010-06-15 11:14:12 +00:00
|
|
|
log_set_use_color(stderr_target, 0);
|
2010-06-15 10:44:42 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2010-06-15 11:14:12 +00:00
|
|
|
log_parse_category_mask(stderr_target, optarg);
|
2010-06-15 10:44:42 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
config_file = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'T':
|
2010-06-15 11:14:12 +00:00
|
|
|
log_set_print_timestamp(stderr_target, 1);
|
2010-06-15 10:44:42 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
msc_address = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
inet_aton(optarg, &local_addr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* ignore */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-12 20:15:08 +00:00
|
|
|
static void signal_handler(int signal)
|
|
|
|
{
|
|
|
|
switch (signal) {
|
|
|
|
case SIGABRT:
|
|
|
|
/* in case of abort, we want to obtain a talloc report
|
|
|
|
* and then return to the caller, who will abort the process */
|
|
|
|
case SIGUSR1:
|
|
|
|
talloc_report_full(tall_bsc_ctx, stderr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:44:42 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2010-06-15 11:14:12 +00:00
|
|
|
log_init(&log_info);
|
|
|
|
stderr_target = log_target_create_stderr();
|
|
|
|
log_add_target(stderr_target);
|
|
|
|
log_set_all_filter(stderr_target, 1);
|
2010-06-15 11:06:18 +00:00
|
|
|
|
2010-06-15 10:44:42 +00:00
|
|
|
/* parse options */
|
2010-01-12 20:34:54 +00:00
|
|
|
local_addr.s_addr = INADDR_ANY;
|
2010-06-15 10:44:42 +00:00
|
|
|
handle_options(argc, argv);
|
|
|
|
|
2010-06-15 10:46:11 +00:00
|
|
|
nat = bsc_nat_alloc();
|
|
|
|
if (!nat) {
|
|
|
|
fprintf(stderr, "Failed to allocate the BSC nat.\n");
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:50:26 +00:00
|
|
|
nat->mgcp_cfg = talloc_zero(nat, struct mgcp_config);
|
|
|
|
|
2010-06-15 10:46:11 +00:00
|
|
|
/* init vty and parse */
|
|
|
|
bsc_nat_vty_init(nat);
|
|
|
|
telnet_init(NULL, 4244);
|
2010-06-15 10:50:26 +00:00
|
|
|
if (mgcp_parse_config(config_file, nat->mgcp_cfg) < 0) {
|
2010-06-15 10:46:11 +00:00
|
|
|
fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:44:42 +00:00
|
|
|
/* seed the PRNG */
|
|
|
|
srand(time(NULL));
|
|
|
|
|
2010-03-31 11:02:22 +00:00
|
|
|
/*
|
|
|
|
* Setup the MGCP code..
|
|
|
|
*/
|
|
|
|
if (bsc_mgcp_init(nat) != 0)
|
|
|
|
return -4;
|
|
|
|
|
2010-01-12 20:34:54 +00:00
|
|
|
/* connect to the MSC */
|
2010-06-15 10:47:29 +00:00
|
|
|
msc_con = bsc_msc_create(msc_address, 5000);
|
|
|
|
if (!msc_con) {
|
|
|
|
fprintf(stderr, "Creating a bsc_msc_connection failed.\n");
|
2010-01-12 20:15:08 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:47:29 +00:00
|
|
|
msc_con->connection_loss = msc_connection_was_lost;
|
|
|
|
msc_con->write_queue.read_cb = ipaccess_msc_read_cb;
|
|
|
|
msc_con->write_queue.write_cb = ipaccess_msc_write_cb;;
|
|
|
|
bsc_msc_connect(msc_con);
|
|
|
|
|
2010-01-12 20:34:54 +00:00
|
|
|
/* wait for the BSC */
|
2010-03-26 05:51:04 +00:00
|
|
|
if (listen_for_bsc(&bsc_listen, &local_addr, 5000) < 0) {
|
2010-01-12 20:34:54 +00:00
|
|
|
fprintf(stderr, "Failed to listen for BSC.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-01-12 20:15:08 +00:00
|
|
|
signal(SIGABRT, &signal_handler);
|
|
|
|
signal(SIGUSR1, &signal_handler);
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
bsc_select_main(0);
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:44:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|