Merge branch 'on-waves/nat-alloc-bsc-endpoints'

This commit is contained in:
Holger Hans Peter Freyther 2010-09-01 19:10:17 +08:00
commit afdb522ced
8 changed files with 178 additions and 73 deletions

View File

@ -75,6 +75,10 @@ struct bsc_connection {
struct timer_list ping_timeout;
struct timer_list pong_timeout;
/* mgcp related code */
int endpoint_status[32];
int last_endpoint;
/* a back pointer */
struct bsc_nat *nat;
};
@ -253,14 +257,14 @@ struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *, struct bsc_nat
* MGCP/Audio handling
*/
int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length);
int bsc_mgcp_assign(struct sccp_connections *, struct msgb *msg);
int bsc_mgcp_assign_patch(struct sccp_connections *, struct msgb *msg);
void bsc_mgcp_init(struct sccp_connections *);
void bsc_mgcp_dlcx(struct sccp_connections *);
void bsc_mgcp_free_endpoints(struct bsc_nat *nat);
int bsc_mgcp_nat_init(struct bsc_nat *nat);
struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number);
struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port);
struct msgb *bsc_mgcp_rewrite(char *input, int length, int endp, const char *ip, int port);
void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg);
void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc);

View File

@ -79,10 +79,12 @@ struct sccp_connections {
int con_type;
int con_local;
/* GSM audio handling. That is 32 * multiplex + ts */
int crcx;
int msc_timeslot;
int bsc_timeslot;
/*
* audio handling. Remember if we have ever send a CRCX,
* remember the endpoint used by the MSC and BSC.
*/
int msc_endp;
int bsc_endp;
/* timeout handling */
struct timespec creation_time;

View File

@ -147,7 +147,16 @@ static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot)
{
if (timeslot == 0)
timeslot = 1;
return timeslot + (31 * multiplex);
return timeslot + (32 * multiplex);
}
static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot)
{
*multiplex = endpoint / 32;
*timeslot = endpoint % 32;
if (*timeslot == 1)
*timeslot = 0;
}

View File

@ -38,14 +38,43 @@
#include <errno.h>
#include <unistd.h>
int bsc_mgcp_assign(struct sccp_connections *con, struct msgb *msg)
static int bsc_assign_endpoint(struct bsc_connection *bsc, struct sccp_connections *con)
{
const int number_endpoints = ARRAY_SIZE(bsc->endpoint_status);
int i;
for (i = 1; i < number_endpoints; ++i) {
int endpoint = (bsc->last_endpoint + i) % number_endpoints;
if (endpoint == 0)
endpoint = 1;
if (bsc->endpoint_status[endpoint] == 0) {
bsc->endpoint_status[endpoint] = 1;
con->bsc_endp = endpoint;
bsc->last_endpoint = endpoint;
return 0;
}
}
return -1;
}
static uint16_t create_cic(int endpoint)
{
int timeslot, multiplex;
mgcp_endpoint_to_timeslot(endpoint, &multiplex, &timeslot);
return (multiplex << 5) | (timeslot & 0x1f);
}
int bsc_mgcp_assign_patch(struct sccp_connections *con, struct msgb *msg)
{
struct sccp_connections *mcon;
struct tlv_parsed tp;
uint16_t cic;
uint8_t timeslot;
uint8_t multiplex;
int combined;
int endp;
if (!msg->l3h) {
LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n");
@ -68,22 +97,32 @@ int bsc_mgcp_assign(struct sccp_connections *con, struct msgb *msg)
multiplex = (cic & ~0x1f) >> 5;
combined = (32 * multiplex) + timeslot;
endp = mgcp_timeslot_to_endpoint(multiplex, timeslot);
/* find stale connections using that endpoint */
llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) {
if (mcon->msc_timeslot == combined) {
if (mcon->msc_endp == endp) {
LOGP(DNAT, LOGL_ERROR,
"Timeslot %d was assigned to 0x%x and now 0x%x\n",
combined,
"Endpoint %d was assigned to 0x%x and now 0x%x\n",
endp,
sccp_src_ref_to_int(&mcon->patched_ref),
sccp_src_ref_to_int(&con->patched_ref));
bsc_mgcp_dlcx(mcon);
}
}
con->msc_timeslot = combined;
con->bsc_timeslot = con->msc_timeslot;
con->msc_endp = endp;
if (bsc_assign_endpoint(con->bsc, con) != 0)
return -1;
/*
* now patch the message for the new CIC...
* still assumed to be one multiplex only
*/
cic = htons(create_cic(con->bsc_endp));
memcpy((uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE),
&cic, sizeof(cic));
return 0;
}
@ -109,7 +148,7 @@ void bsc_mgcp_free_endpoints(struct bsc_nat *nat)
}
/* send a MDCX where we do not want a response */
static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, struct mgcp_endpoint *endp)
static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, int port, struct mgcp_endpoint *endp)
{
char buf[2096];
int len;
@ -120,13 +159,15 @@ static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, struct mgcp_endpoint
"\r\n"
"c=IN IP4 %s\r\n"
"m=audio %d RTP/AVP 255\r\n",
ENDPOINT_NUMBER(endp),
port,
bsc->nat->mgcp_cfg->source_addr,
endp->bts_end.local_port);
if (len < 0) {
LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
return;
}
#warning "The MDCX is not send to the BSC. It should"
}
static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
@ -135,7 +176,7 @@ static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
int len;
len = snprintf(buf, sizeof(buf),
"DLCX 23 %x@mgw MGCP 1.0\r\n"
"DLCX 26 %x@mgw MGCP 1.0\r\n"
"Z: noanswer\r\n", endpoint);
if (len < 0) {
LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
@ -147,18 +188,19 @@ static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
void bsc_mgcp_init(struct sccp_connections *con)
{
con->msc_timeslot = -1;
con->bsc_timeslot = -1;
con->crcx = 0;
con->msc_endp = -1;
con->bsc_endp = -1;
}
void bsc_mgcp_dlcx(struct sccp_connections *con)
{
/* send a DLCX down the stream */
if (con->bsc_timeslot != -1 && con->crcx) {
int endp = mgcp_timeslot_to_endpoint(0, con->msc_timeslot);
bsc_mgcp_send_dlcx(con->bsc, endp);
bsc_mgcp_free_endpoint(con->bsc->nat, endp);
if (con->bsc_endp != -1) {
if (con->bsc->endpoint_status[con->bsc_endp] != 1)
LOGP(DNAT, LOGL_ERROR, "Endpoint 0x%x was not in use\n", con->bsc_endp);
con->bsc->endpoint_status[con->bsc_endp] = 0;
bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp);
bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp);
}
bsc_mgcp_init(con);
@ -171,9 +213,9 @@ struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
struct sccp_connections *sccp;
llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
if (sccp->msc_timeslot == -1)
if (sccp->msc_endp == -1)
continue;
if (mgcp_timeslot_to_endpoint(0, sccp->msc_timeslot) != endpoint)
if (sccp->msc_endp != endpoint)
continue;
con = sccp;
@ -230,7 +272,7 @@ int bsc_mgcp_policy_cb(struct mgcp_config *cfg, int endpoint, int state, const c
}
/* we need to generate a new and patched message */
bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length,
bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length, sccp->bsc_endp,
nat->mgcp_cfg->source_addr, mgcp_endp->bts_end.local_port);
if (!bsc_msg) {
LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
@ -254,9 +296,8 @@ int bsc_mgcp_policy_cb(struct mgcp_config *cfg, int endpoint, int state, const c
}
/* send the message and a fake MDCX to force sending of a dummy packet */
sccp->crcx = 1;
bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
bsc_mgcp_send_mdcx(sccp->bsc, mgcp_endp);
bsc_mgcp_send_mdcx(sccp->bsc, sccp->bsc_endp, mgcp_endp);
return MGCP_POLICY_DEFER;
} else if (state == MGCP_ENDP_DLCX) {
/* we will free the endpoint now and send a DLCX to the BSC */
@ -275,30 +316,29 @@ int bsc_mgcp_policy_cb(struct mgcp_config *cfg, int endpoint, int state, const c
static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp,
struct bsc_connection *bsc)
{
LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
/* if a CRCX failed... send a DLCX down the stream */
if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
struct sccp_connections *con;
con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
if (!con) {
LOGP(DMGCP, LOGL_ERROR,
"No SCCP connection for endp 0x%x\n",
ENDPOINT_NUMBER(endp));
/* if a CRCX failed... send a DLCX down the stream */
if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
struct sccp_connections *con;
con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
if (!con) {
LOGP(DMGCP, LOGL_ERROR,
"No SCCP connection for endp 0x%x\n",
ENDPOINT_NUMBER(endp));
} else {
if (con->bsc == bsc) {
bsc_mgcp_send_dlcx(bsc, con->bsc_endp);
} else {
if (con->bsc == bsc) {
bsc_mgcp_send_dlcx(bsc, ENDPOINT_NUMBER(endp));
con->crcx = 0;
} else {
LOGP(DMGCP, LOGL_ERROR,
"Endpoint belongs to a different BSC\n");
}
LOGP(DMGCP, LOGL_ERROR,
"Endpoint belongs to a different BSC\n");
}
}
}
bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
mgcp_free_endp(endp);
bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
mgcp_free_endp(endp);
}
/*
@ -364,7 +404,7 @@ void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
* there should be nothing for us to rewrite so putting endp->rtp_port
* with the value of 0 should be no problem.
*/
output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg),
output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg), -1,
bsc->nat->mgcp_cfg->source_addr, endp->net_end.local_port);
if (!output) {
@ -401,9 +441,28 @@ uint32_t bsc_mgcp_extract_ci(const char *str)
return ci;
}
/* we need to replace some strings... */
struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
static void patch_mgcp(struct msgb *output, const char *op, const char *tok,
int endp, int len, int cr)
{
int slen;
int ret;
char buf[40];
buf[0] = buf[39] = '\0';
ret = sscanf(tok, "%*s %s", buf);
slen = sprintf((char *) output->l3h, "%s %s %x@mgw MGCP 1.0%s",
op, buf, endp, cr ? "\r\n" : "\n");
output->l3h = msgb_put(output, slen);
}
/* we need to replace some strings... */
struct msgb *bsc_mgcp_rewrite(char *input, int length, int endpoint, const char *ip, int port)
{
static const char *crcx_str = "CRCX ";
static const char *dlcx_str = "DLCX ";
static const char *mdcx_str = "MDCX ";
static const char *ip_str = "c=IN IP4 ";
static const char *aud_str = "m=audio ";
@ -424,11 +483,18 @@ struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
running = input;
output->l2h = output->data;
output->l3h = output->l2h;
for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
int len = strlen(token);
int cr = len > 0 && token[len - 1] == '\r';
if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
if (strncmp(crcx_str, token, (sizeof crcx_str) - 1) == 0) {
patch_mgcp(output, "CRCX", token, endpoint, len, cr);
} else if (strncmp(dlcx_str, token, (sizeof dlcx_str) - 1) == 0) {
patch_mgcp(output, "DLCX", token, endpoint, len, cr);
} else if (strncmp(mdcx_str, token, (sizeof mdcx_str) - 1) == 0) {
patch_mgcp(output, "MDCX", token, endpoint, len, cr);
} else if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
output->l3h = msgb_put(output, strlen(ip_str));
memcpy(output->l3h, ip_str, strlen(ip_str));
output->l3h = msgb_put(output, strlen(ip));

View File

@ -422,7 +422,7 @@ static int forward_sccp_to_bts(struct bsc_msc_connection *msc_con, struct msgb *
struct rate_ctr_group *ctrg;
ctrg = con->bsc->cfg->stats.ctrg;
rate_ctr_inc(&ctrg->ctr[BCFG_CTR_SCCP_CALLS]);
if (bsc_mgcp_assign(con, msg) != 0)
if (bsc_mgcp_assign_patch(con, msg) != 0)
LOGP(DNAT, LOGL_ERROR, "Failed to assign...\n");
} else
LOGP(DNAT, LOGL_ERROR, "Assignment command but no BSC.\n");

View File

@ -29,6 +29,7 @@
#include <osmocore/talloc.h>
#include <osmocore/rate_ctr.h>
#include <osmocore/utils.h>
#include <osmocom/sccp/sccp.h>
@ -121,7 +122,7 @@ DEFUN(show_sccp, show_sccp_cmd, "show sccp connections",
sccp_src_ref_to_int(&con->patched_ref),
con->has_remote_ref,
sccp_src_ref_to_int(&con->remote_ref),
con->msc_timeslot, con->bsc_timeslot,
con->msc_endp, con->bsc_endp,
bsc_con_type_to_string(con->con_type),
VTY_NEWLINE);
}
@ -148,6 +149,30 @@ DEFUN(show_bsc, show_bsc_cmd, "show bsc connections",
return CMD_SUCCESS;
}
DEFUN(show_bsc_mgcp, show_bsc_mgcp_cmd, "show bsc mgcp NR",
SHOW_STR "Display the MGCP status for a given BSC")
{
struct bsc_connection *con;
int nr = atoi(argv[0]);
int i;
llist_for_each_entry(con, &_nat->bsc_connections, list_entry) {
if (!con->cfg)
continue;
if (con->cfg->nr != nr)
continue;
vty_out(vty, "MGCP Status for %d%s", con->cfg->nr, VTY_NEWLINE);
for (i = 1; i < ARRAY_SIZE(con->endpoint_status); ++i)
vty_out(vty, " Endpoint 0x%x %s%s", i,
con->endpoint_status[i] == 0 ? "free" : "allocated",
VTY_NEWLINE);
break;
}
return CMD_SUCCESS;
}
DEFUN(show_bsc_cfg, show_bsc_cfg_cmd, "show bsc config",
SHOW_STR "Display information about known BSC configs")
{
@ -564,6 +589,7 @@ int bsc_nat_vty_init(struct bsc_nat *nat)
install_element_ve(&close_bsc_cmd);
install_element_ve(&show_msc_cmd);
install_element_ve(&test_regex_cmd);
install_element_ve(&show_bsc_mgcp_cmd);
/* nat group */
install_element(CONFIG_NODE, &cfg_nat_cmd);

View File

@ -94,7 +94,7 @@ static const uint8_t ass_cmd[] = {
/* nothing to patch */
static const char crcx[] = "CRCX 23265295 8@mgw MGCP 1.0\r\nC: 394b0439fb\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n";
static const char crcx_patched[] = "CRCX 23265295 8@mgw MGCP 1.0\r\nC: 394b0439fb\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n";
static const char crcx_patched[] = "CRCX 23265295 1e@mgw MGCP 1.0\r\nC: 394b0439fb\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n";
/* patch the ip and port */
@ -102,8 +102,8 @@ static const char crcx_resp[] = "200 23265295\r\nI: 1\r\n\r\nv=0\r\nc=IN IP4 172
static const char crcx_resp_patched[] = "200 23265295\r\nI: 1\r\n\r\nv=0\r\nc=IN IP4 10.0.0.1\r\nm=audio 999 RTP/AVP 98\r\na=rtpmap:98 AMR/8000\r\n";
/* patch the ip and port */
static const char mdcx[] = " MDCX 23330829 8@mgw MGCP 1.0\r\nC: 394b0439fb\r\nI: 1\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n\r\nv=0\r\no=- 1049380491 0 IN IP4 172.16.18.2\r\ns=-\r\nc=IN IP4 172.16.18.2\r\nt=0 0\r\nm=audio 4410 RTP/AVP 126\r\na=rtpmap:126 AMR/8000/1\r\na=fmtp:126 mode-set=2;start-mode=0\r\na=ptime:20\r\na=recvonly\r\nm=image 4412 udptl t38\r\na=T38FaxVersion:0\r\na=T38MaxBitRate:14400\r\n";
static const char mdcx_patched[] = " MDCX 23330829 8@mgw MGCP 1.0\r\nC: 394b0439fb\r\nI: 1\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n\r\nv=0\r\no=- 1049380491 0 IN IP4 172.16.18.2\r\ns=-\r\nc=IN IP4 10.0.0.23\r\nt=0 0\r\nm=audio 6666 RTP/AVP 126\r\na=rtpmap:126 AMR/8000/1\r\na=fmtp:126 mode-set=2;start-mode=0\r\na=ptime:20\r\na=recvonly\r\nm=image 4412 udptl t38\r\na=T38FaxVersion:0\r\na=T38MaxBitRate:14400\r\n";
static const char mdcx[] = "MDCX 23330829 8@mgw MGCP 1.0\r\nC: 394b0439fb\r\nI: 1\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n\r\nv=0\r\no=- 1049380491 0 IN IP4 172.16.18.2\r\ns=-\r\nc=IN IP4 172.16.18.2\r\nt=0 0\r\nm=audio 4410 RTP/AVP 126\r\na=rtpmap:126 AMR/8000/1\r\na=fmtp:126 mode-set=2;start-mode=0\r\na=ptime:20\r\na=recvonly\r\nm=image 4412 udptl t38\r\na=T38FaxVersion:0\r\na=T38MaxBitRate:14400\r\n";
static const char mdcx_patched[] = "MDCX 23330829 1e@mgw MGCP 1.0\r\nC: 394b0439fb\r\nI: 1\r\nL: p:20, a:AMR, nt:IN\r\nM: recvonly\r\n\r\nv=0\r\no=- 1049380491 0 IN IP4 172.16.18.2\r\ns=-\r\nc=IN IP4 10.0.0.23\r\nt=0 0\r\nm=audio 6666 RTP/AVP 126\r\na=rtpmap:126 AMR/8000/1\r\na=fmtp:126 mode-set=2;start-mode=0\r\na=ptime:20\r\na=recvonly\r\nm=image 4412 udptl t38\r\na=T38FaxVersion:0\r\na=T38MaxBitRate:14400\r\n";
static const char mdcx_resp[] = "200 23330829\r\n\r\nv=0\r\nc=IN IP4 172.16.18.2\r\nm=audio 4002 RTP/AVP 98\r\na=rtpmap:98 AMR/8000\r\n";

View File

@ -444,24 +444,29 @@ static void test_mgcp_ass_tracking(void)
msg = msgb_alloc(4096, "foo");
copy_to_msg(msg, ass_cmd, sizeof(ass_cmd));
parsed = bsc_nat_parse(msg);
if (bsc_mgcp_assign(&con, msg) != 0) {
if (bsc_mgcp_assign_patch(&con, msg) != 0) {
fprintf(stderr, "Failed to handle assignment.\n");
abort();
}
if (con.msc_timeslot != 21) {
if (con.msc_endp != 21) {
fprintf(stderr, "Timeslot should be 21.\n");
abort();
}
if (con.bsc_timeslot != 21) {
fprintf(stderr, "Assigned timeslot should have been 21.\n");
if (con.bsc_endp != 1) {
fprintf(stderr, "Assigned timeslot should have been 1.\n");
abort();
}
if (con.bsc->endpoint_status[1] != 1) {
fprintf(stderr, "The status on the BSC is wrong.\n");
abort();
}
talloc_free(parsed);
bsc_mgcp_dlcx(&con);
if (con.bsc_timeslot != -1 || con.msc_timeslot != -1) {
if (con.bsc_endp != -1 || con.msc_endp != -1 || con.bsc->endpoint_status[1] != 0) {
fprintf(stderr, "Clearing should remove the mapping.\n");
abort();
}
@ -483,8 +488,8 @@ static void test_mgcp_find(void)
llist_add(&con->list_entry, &nat->bsc_connections);
sccp_con = talloc_zero(con, struct sccp_connections);
sccp_con->msc_timeslot = 12;
sccp_con->bsc_timeslot = 12;
sccp_con->msc_endp = 12;
sccp_con->bsc_endp = 12;
sccp_con->bsc = con;
llist_add(&sccp_con->list_entry, &nat->sccp_connections);
@ -498,13 +503,6 @@ static void test_mgcp_find(void)
abort();
}
sccp_con->msc_timeslot = 0;
sccp_con->bsc_timeslot = 0;
if (bsc_mgcp_find_con(nat, 1) != sccp_con) {
fprintf(stderr, "Didn't find the connection\n");
abort();
}
/* free everything */
talloc_free(nat);
}
@ -523,7 +521,7 @@ static void test_mgcp_rewrite(void)
char *input = strdup(orig);
output = bsc_mgcp_rewrite(input, strlen(input), ip, port);
output = bsc_mgcp_rewrite(input, strlen(input), 0x1e, ip, port);
if (msgb_l2len(output) != strlen(patc)) {
fprintf(stderr, "Wrong sizes for test: %d %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig));
fprintf(stderr, "String '%s' vs '%s'\n", (const char *) output->l2h, patc);