From b1616422d5b80467b42decc6640aa180b68901de Mon Sep 17 00:00:00 2001 From: Daniel Willmann Date: Tue, 19 Sep 2017 16:39:38 +0200 Subject: osmo-bsc_nat: Modify nat init to work with multiple mgcp_configs Change-Id: I53ac858952ee15f897160b3a32483c5c91b862f6 --- openbsc/include/openbsc/bsc_nat.h | 11 +-- openbsc/src/osmo-bsc_nat/bsc_mgcp_utils.c | 115 +++++++++++++++++------------- openbsc/src/osmo-bsc_nat/bsc_nat.c | 10 +-- openbsc/src/osmo-bsc_nat/bsc_nat_utils.c | 1 + 4 files changed, 75 insertions(+), 62 deletions(-) diff --git a/openbsc/include/openbsc/bsc_nat.h b/openbsc/include/openbsc/bsc_nat.h index 08a2a931e..abafd6f51 100644 --- a/openbsc/include/openbsc/bsc_nat.h +++ b/openbsc/include/openbsc/bsc_nat.h @@ -265,7 +265,7 @@ struct bsc_nat { int bsc_ip_dscp; /* MGCP config */ - struct mgcp_config *mgcp_cfg; + struct llist_head mgcp_cfgs; uint8_t mgcp_msg[4096]; int mgcp_length; int mgcp_ipa; @@ -281,8 +281,6 @@ struct bsc_nat { int ping_timeout; int pong_timeout; - struct bsc_endpoint *bsc_endpoints; - /* path to file with BSC config */ char *include_file; char *include_base; @@ -343,6 +341,11 @@ struct bsc_nat_ussd_con { struct osmo_timer_list auth_timeout; }; +struct mgcp_nat_config { + struct bsc_nat *nat; + struct bsc_endpoint *bsc_endpoints; +}; + /* create and init the structures */ struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int number); @@ -401,7 +404,7 @@ int bsc_mgcp_assign_patch(struct nat_sccp_connection *, struct msgb *msg); void bsc_mgcp_init(struct nat_sccp_connection *); void bsc_mgcp_dlcx(struct nat_sccp_connection *); void bsc_mgcp_free_endpoints(struct bsc_nat *nat); -int bsc_mgcp_nat_init(struct bsc_nat *nat); +int bsc_mgcp_nat_init(struct bsc_nat *nat, struct llist_head* cfgs); struct nat_sccp_connection *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number); struct msgb *bsc_mgcp_rewrite(char *input, int length, int endp, const char *ip, diff --git a/openbsc/src/osmo-bsc_nat/bsc_mgcp_utils.c b/openbsc/src/osmo-bsc_nat/bsc_mgcp_utils.c index 1130c63d5..e360fbb16 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_mgcp_utils.c +++ b/openbsc/src/osmo-bsc_nat/bsc_mgcp_utils.c @@ -556,10 +556,10 @@ static int bsc_mgcp_policy_cb(struct mgcp_trunk_config *tcfg, int endpoint, int /* Allocate a Osmux circuit ID */ if (state == MGCP_ENDP_CRCX) { - if (nat->mgcp_cfg->osmux && sccp->bsc->cfg->osmux) { + if (tcfg->cfg->osmux && sccp->bsc->cfg->osmux) { osmux_allocate_cid(mgcp_endp); if (mgcp_endp->osmux.allocated_cid < 0 && - nat_osmux_only(nat->mgcp_cfg, sccp->bsc->cfg)) { + nat_osmux_only(tcfg->cfg, sccp->bsc->cfg)) { LOGP(DMGCP, LOGL_ERROR, "Rejecting usage of endpoint\n"); return MGCP_POLICY_REJECT; @@ -1081,77 +1081,92 @@ static int init_mgcp_socket(struct bsc_nat *nat, struct mgcp_config *cfg) return 0; } -int bsc_mgcp_nat_init(struct bsc_nat *nat) +int bsc_mgcp_nat_init(struct bsc_nat *nat, struct llist_head *cfgs) { - struct mgcp_config *cfg = nat->mgcp_cfg; + struct mgcp_config *cfg; + struct mgcp_nat_config *mgcp_nat; - if (!cfg->call_agent_addr) { - LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n"); - return -1; - } + llist_for_each_entry(cfg, cfgs, entry) { + if (!cfg->call_agent_addr) { + LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n"); + return -1; + } - if (cfg->bts_ip) { - LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n"); - return -1; - } + if (cfg->bts_ip) { + LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n"); + return -1; + } - /* initialize the MGCP socket */ - if (!nat->mgcp_ipa) { - int rc = init_mgcp_socket(nat, cfg); - if (rc != 0) - return rc; - } + /* initialize the MGCP socket */ + if (!nat->mgcp_ipa) { + int rc = init_mgcp_socket(nat, cfg); + if (rc != 0) + return rc; + } - /* some more MGCP config handling */ - cfg->data = nat; - cfg->policy_cb = bsc_mgcp_policy_cb; - cfg->trunk.force_realloc = 1; + /* Create a user structure to keep bsc_endpoints per mgcp + * config */ + mgcp_nat = talloc_zero(cfg, struct mgcp_nat_config); + if (!mgcp_nat) + return -1; - if (cfg->bts_ip) - talloc_free(cfg->bts_ip); - cfg->bts_ip = ""; + mgcp_nat->nat = nat; + cfg->data = mgcp_nat; - nat->bsc_endpoints = talloc_zero_array(nat, - struct bsc_endpoint, - cfg->trunk.number_endpoints + 1); - if (!nat->bsc_endpoints) { - LOGP(DMGCP, LOGL_ERROR, "Failed to allocate nat endpoints\n"); - close(cfg->gw_fd.bfd.fd); - cfg->gw_fd.bfd.fd = -1; - return -1; - } + /* some more MGCP config handling */ + cfg->policy_cb = bsc_mgcp_policy_cb; + cfg->trunk.force_realloc = 1; - if (mgcp_reset_transcoder(cfg) < 0) { - LOGP(DMGCP, LOGL_ERROR, "Failed to send packet to the transcoder.\n"); - talloc_free(nat->bsc_endpoints); - nat->bsc_endpoints = NULL; - close(cfg->gw_fd.bfd.fd); - cfg->gw_fd.bfd.fd = -1; - return -1; - } + if (cfg->bts_ip) + talloc_free(cfg->bts_ip); + cfg->bts_ip = ""; + + mgcp_nat->bsc_endpoints = talloc_zero_array(nat, + struct bsc_endpoint, + cfg->trunk.number_endpoints + 1); + if (!mgcp_nat->bsc_endpoints) { + LOGP(DMGCP, LOGL_ERROR, "Failed to allocate nat endpoints\n"); + close(cfg->gw_fd.bfd.fd); + cfg->gw_fd.bfd.fd = -1; + return -1; + } + if (mgcp_reset_transcoder(cfg) < 0) { + LOGP(DMGCP, LOGL_ERROR, "Failed to send packet to the transcoder.\n"); + talloc_free(mgcp_nat->bsc_endpoints); + mgcp_nat->bsc_endpoints = NULL; + close(cfg->gw_fd.bfd.fd); + cfg->gw_fd.bfd.fd = -1; + return -1; + } + } return 0; } void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc) { + struct mgcp_config *mgcp_cfg; + struct mgcp_nat_config *mgcp_nat; struct rate_ctr *ctr = NULL; int i; if (bsc->cfg) ctr = &bsc->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_CALLS]; - for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) { - struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i]; + llist_for_each_entry(mgcp_cfg, &bsc->nat->mgcp_cfgs, entry) { + mgcp_nat = mgcp_cfg->data; + for (i = 1; i < mgcp_cfg->trunk.number_endpoints; ++i) { + struct bsc_endpoint *bsc_endp = &mgcp_nat->bsc_endpoints[i]; - if (bsc_endp->bsc != bsc) - continue; + if (bsc_endp->bsc != bsc) + continue; - if (ctr) - rate_ctr_inc(ctr); + if (ctr) + rate_ctr_inc(ctr); - bsc_mgcp_free_endpoint(bsc->nat, i); - mgcp_release_endp(&bsc->nat->mgcp_cfg->trunk.endpoints[i]); + bsc_mgcp_free_endpoint(mgcp_cfg, i); + mgcp_release_endp(&mgcp_cfg->trunk.endpoints[i]); + } } } diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat.c b/openbsc/src/osmo-bsc_nat/bsc_nat.c index 200492cec..1edc3b710 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat.c @@ -1613,12 +1613,6 @@ int main(int argc, char **argv) return -4; } - nat->mgcp_cfg = mgcp_config_alloc(); - if (!nat->mgcp_cfg) { - fprintf(stderr, "Failed to allocate MGCP cfg.\n"); - return -5; - } - /* We need to add mode-set for amr codecs */ nat->sdp_ensure_amr_mode_set = 1; @@ -1640,7 +1634,7 @@ int main(int argc, char **argv) osmo_stats_init(tall_bsc_ctx); /* init vty and parse */ - if (mgcp_parse_config(config_file, nat->mgcp_cfg, MGCP_BSC_NAT) < 0) { + if (mgcp_parse_config(config_file, &nat->mgcp_cfgs, MGCP_BSC_NAT) < 0) { fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); return -3; } @@ -1660,7 +1654,7 @@ int main(int argc, char **argv) /* * Setup the MGCP code.. */ - if (bsc_mgcp_nat_init(nat) != 0) + if (bsc_mgcp_nat_init(nat, &nat->mgcp_cfgs) != 0) return -4; /* start control interface after reading config for diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c index 5bed6cda1..7a43777aa 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c @@ -87,6 +87,7 @@ struct bsc_nat *bsc_nat_alloc(void) INIT_LLIST_HEAD(&nat->tpdest_match); INIT_LLIST_HEAD(&nat->sms_clear_tp_srr); INIT_LLIST_HEAD(&nat->sms_num_rewr); + INIT_LLIST_HEAD(&nat->mgcp_cfgs); nat->stats.sccp.conn = osmo_counter_alloc("nat.sccp.conn"); nat->stats.sccp.calls = osmo_counter_alloc("nat.sccp.calls"); -- cgit v1.2.3