diff --git a/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg b/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg index f56dcb0..2f5ff39 100644 --- a/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg +++ b/doc/examples/osmo-bsc-nat/osmo-bsc-nat.cfg @@ -13,10 +13,6 @@ cs7 instance 1 asp asp-clnt-OsmoBSCNAT-RAN 2905 0 m3ua remote-ip 127.0.0.2 local-ip 127.0.0.4 - sccp-address bsc - routing-indicator PC - point-code 0.23.3 - subsystem-number 254 bsc-nat cs7-instance-cn 0 diff --git a/include/osmocom/bsc_nat/Makefile.am b/include/osmocom/bsc_nat/Makefile.am index 1c675f7..81dacad 100644 --- a/include/osmocom/bsc_nat/Makefile.am +++ b/include/osmocom/bsc_nat/Makefile.am @@ -5,5 +5,6 @@ noinst_HEADERS = \ bssap.h \ logging.h \ msc.h \ + subscr_conn.h \ vty.h \ $(NULL) diff --git a/include/osmocom/bsc_nat/bsc.h b/include/osmocom/bsc_nat/bsc.h index 34590b9..7c11a82 100644 --- a/include/osmocom/bsc_nat/bsc.h +++ b/include/osmocom/bsc_nat/bsc.h @@ -29,3 +29,4 @@ struct bsc { struct bsc *bsc_alloc(struct osmo_sccp_addr *addr); struct bsc *bsc_get_by_pc(uint32_t pointcode); void bsc_free(struct bsc *bsc); +void bsc_free_subscr_conn_all(struct bsc *bsc); diff --git a/include/osmocom/bsc_nat/bsc_nat.h b/include/osmocom/bsc_nat/bsc_nat.h index 9e58a79..93717cc 100644 --- a/include/osmocom/bsc_nat/bsc_nat.h +++ b/include/osmocom/bsc_nat/bsc_nat.h @@ -37,14 +37,17 @@ struct bsc_nat_sccp_inst { struct bsc_nat { struct osmo_fsm_inst *fi; + struct llist_head subscr_conns; /* list of struct subscr_conn */ struct { struct bsc_nat_sccp_inst *sccp_inst; + uint32_t subscr_conn_id_next; struct llist_head mscs; /* list of struct msc */ } cn; struct { struct bsc_nat_sccp_inst *sccp_inst; + uint32_t subscr_conn_id_next; struct llist_head bscs; /* list of struct bsc */ } ran; }; diff --git a/include/osmocom/bsc_nat/msc.h b/include/osmocom/bsc_nat/msc.h index db42526..77fc001 100644 --- a/include/osmocom/bsc_nat/msc.h +++ b/include/osmocom/bsc_nat/msc.h @@ -39,3 +39,4 @@ void msc_rx_reset_ack(struct msc *msc); bool msc_is_connected(struct msc *msc); void msc_free(struct msc *msc); +void msc_free_subscr_conn_all(struct msc *msc); diff --git a/include/osmocom/bsc_nat/subscr_conn.h b/include/osmocom/bsc_nat/subscr_conn.h new file mode 100644 index 0000000..696e0c3 --- /dev/null +++ b/include/osmocom/bsc_nat/subscr_conn.h @@ -0,0 +1,43 @@ +/* (C) 2021 by sysmocom - s.f.m.c. GmbH + * Author: Oliver Smith + * All Rights Reserved + * + * 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 + * (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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +#pragma once + +/* connection for one subscriber */ +struct subscr_conn { + struct llist_head list; + + struct { + uint32_t id; + struct msc *msc; + } cn; + + struct { + uint32_t id; + struct bsc *bsc; + } ran; +}; + +int subscr_conn_get_next_id(enum bsc_nat_net net); + +struct subscr_conn *subscr_conn_alloc(struct msc *msc, struct bsc *bsc, uint32_t id_cn, uint32_t id_ran); + +struct subscr_conn *subscr_conn_get_by_id(uint32_t id, enum bsc_nat_net net); + +void subscr_conn_free(struct subscr_conn *subscr_conn); diff --git a/src/osmo-bsc-nat/Makefile.am b/src/osmo-bsc-nat/Makefile.am index 8fd5667..e2b11ca 100644 --- a/src/osmo-bsc-nat/Makefile.am +++ b/src/osmo-bsc-nat/Makefile.am @@ -33,6 +33,7 @@ osmo_bsc_nat_SOURCES = \ main.c \ msc.c \ msc_fsm.c \ + subscr_conn.c \ vty.c \ $(NULL) diff --git a/src/osmo-bsc-nat/bsc.c b/src/osmo-bsc-nat/bsc.c index 2ad6e0e..0461909 100644 --- a/src/osmo-bsc-nat/bsc.c +++ b/src/osmo-bsc-nat/bsc.c @@ -21,6 +21,7 @@ #include #include #include +#include struct bsc *bsc_alloc(struct osmo_sccp_addr *addr) { @@ -51,9 +52,20 @@ struct bsc *bsc_get_by_pc(uint32_t pointcode) return NULL; } +void bsc_free_subscr_conn_all(struct bsc *bsc) +{ + struct subscr_conn *subscr_conn; + + llist_for_each_entry(subscr_conn, &g_bsc_nat->subscr_conns, list) { + if (subscr_conn->ran.bsc == bsc) + subscr_conn_free(subscr_conn); + } +} + void bsc_free(struct bsc *bsc) { LOGP(DMAIN, LOGL_DEBUG, "Del %s\n", talloc_get_name(bsc)); + bsc_free_subscr_conn_all(bsc); llist_del(&bsc->list); talloc_free(bsc); } diff --git a/src/osmo-bsc-nat/bsc_nat.c b/src/osmo-bsc-nat/bsc_nat.c index 23d7498..7ba24e6 100644 --- a/src/osmo-bsc-nat/bsc_nat.c +++ b/src/osmo-bsc-nat/bsc_nat.c @@ -18,6 +18,7 @@ */ #include "config.h" +#include #include #include #include @@ -25,6 +26,7 @@ #include #include #include +#include struct bsc_nat *bsc_nat_alloc(void *tall_ctx) { @@ -41,6 +43,7 @@ struct bsc_nat *bsc_nat_alloc(void *tall_ctx) OSMO_ASSERT(bsc_nat->ran.sccp_inst); talloc_set_name_const(bsc_nat->ran.sccp_inst, "struct bsc_nat_sccp_inst (RAN)"); + INIT_LLIST_HEAD(&bsc_nat->subscr_conns); INIT_LLIST_HEAD(&bsc_nat->cn.mscs); INIT_LLIST_HEAD(&bsc_nat->ran.bscs); @@ -51,6 +54,7 @@ struct bsc_nat *bsc_nat_alloc(void *tall_ctx) void bsc_nat_free(struct bsc_nat *bsc_nat) { + struct subscr_conn *subscr_conn, *s; struct msc *msc, *m; struct bsc *bsc, *b; @@ -59,6 +63,10 @@ void bsc_nat_free(struct bsc_nat *bsc_nat) bsc_nat->fi = NULL; } + llist_for_each_entry_safe(subscr_conn, s, &bsc_nat->subscr_conns, list) { + subscr_conn_free(subscr_conn); + } + llist_for_each_entry_safe(msc, m, &bsc_nat->cn.mscs, list) { msc_free(msc); } diff --git a/src/osmo-bsc-nat/bsc_nat_fsm.c b/src/osmo-bsc-nat/bsc_nat_fsm.c index ad10ea2..090c38f 100644 --- a/src/osmo-bsc-nat/bsc_nat_fsm.c +++ b/src/osmo-bsc-nat/bsc_nat_fsm.c @@ -19,17 +19,20 @@ #include "config.h" #include +#include #include #include #include #include #include #include +#include #include #include #include #include #include +#include #define DEFAULT_PC_RAN "0.23.1" /* same as default for OsmoMSC */ #define DEFAULT_PC_CN "0.23.3" /* same as default for OsmoBSC */ @@ -47,13 +50,6 @@ enum bsc_nat_fsm_events { BSC_NAT_FSM_EV_STOP, }; -static struct bsc_nat_sccp_inst *sccp_inst_dest(struct bsc_nat_sccp_inst *src) -{ - if (src == g_bsc_nat->cn.sccp_inst) - return g_bsc_nat->ran.sccp_inst; - return g_bsc_nat->cn.sccp_inst; -} - /* For connection-oriented messages, figure out which side is not the BSCNAT, * either the called_addr or calling_addr. */ static int sccp_sap_get_peer_addr_in(struct bsc_nat_sccp_inst *src, struct osmo_sccp_addr **peer_addr_in, @@ -79,37 +75,13 @@ static int sccp_sap_get_peer_addr_in(struct bsc_nat_sccp_inst *src, struct osmo_ return -1; } -/* Figure out who will receive the message. - * For now this is simplified by assuming there is only one MSC, one BSC. */ -static int sccp_sap_get_peer_addr_out(struct bsc_nat_sccp_inst *src, struct osmo_sccp_addr *peer_addr_in, - struct osmo_sccp_addr *peer_addr_out) -{ - struct bsc_nat_sccp_inst *dest = sccp_inst_dest(src); - - if (src == g_bsc_nat->ran.sccp_inst) { - if (osmo_sccp_addr_by_name_local(peer_addr_out, "msc", dest->ss7_inst) < 0) { - LOGP(DMAIN, LOGL_ERROR, "Could not find MSC in address book\n"); - return -1; - } - } else { - if (osmo_sccp_addr_by_name_local(peer_addr_out, "bsc", dest->ss7_inst) < 0) { - LOGP(DMAIN, LOGL_ERROR, "Could not find BSC in address book\n"); - return -2; - } - } - - return 0; -} - -/* Handle incoming messages from CN (MSC). For now this is simplified by - * assuming there is only one MSC, one BSC (not yet translating connection ids - * etc.). */ +/* Handle incoming messages from CN (MSC) */ static int sccp_sap_up_cn(struct osmo_prim_hdr *oph, void *scu) { struct bsc_nat_sccp_inst *sccp_inst = osmo_sccp_user_get_priv(scu); struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph; struct osmo_sccp_addr *addr; /* MSC's address */ - struct osmo_sccp_addr peer_addr_out; + struct subscr_conn *subscr_conn; int rc = -1; LOGP(DMAIN, LOGL_DEBUG, "Rx %s from CN\n", osmo_scu_prim_name(oph)); @@ -117,23 +89,9 @@ static int sccp_sap_up_cn(struct osmo_prim_hdr *oph, void *scu) switch (OSMO_PRIM_HDR(oph)) { case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION): /* indication of new inbound connection request */ - if (sccp_sap_get_peer_addr_in(sccp_inst, &addr, &prim->u.connect.called_addr, - &prim->u.connect.calling_addr) < 0) - goto error; - - if (sccp_sap_get_peer_addr_out(sccp_inst, addr, &peer_addr_out) < 0) - goto error; - - LOGP(DMAIN, LOGL_DEBUG, "Fwd to %s\n", bsc_nat_print_addr_ran(&peer_addr_out)); - - msgb_pull_to_l2(oph->msg); - osmo_sccp_tx_conn_req(g_bsc_nat->ran.sccp_inst->scu, - prim->u.connect.conn_id, - &g_bsc_nat->ran.sccp_inst->addr, - &peer_addr_out, - oph->msg->data, - msgb_length(oph->msg)); - rc = 0; + /* FIXME: MSC has sent a message without conn_id, figure out to + * which BSC to forward this. */ + LOGP(DMAIN, LOGL_ERROR, "%s(%s) is not implemented!\n", __func__, osmo_scu_prim_name(oph)); break; case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM): @@ -142,15 +100,19 @@ static int sccp_sap_up_cn(struct osmo_prim_hdr *oph, void *scu) &prim->u.connect.calling_addr) < 0) goto error; - if (sccp_sap_get_peer_addr_out(sccp_inst, addr, &peer_addr_out) < 0) + subscr_conn = subscr_conn_get_by_id(prim->u.connect.conn_id, BSC_NAT_NET_CN); + if (!subscr_conn) { + LOGP(DMAIN, LOGL_ERROR, "Unknown conn_id=%" PRIu32 " from %s\n", prim->u.connect.conn_id, + bsc_nat_print_addr_cn(addr)); goto error; + } - LOGP(DMAIN, LOGL_DEBUG, "Fwd to %s\n", bsc_nat_print_addr_ran(&peer_addr_out)); + LOGP(DMAIN, LOGL_DEBUG, "Fwd via %s\n", talloc_get_name(subscr_conn)); msgb_pull_to_l2(oph->msg); osmo_sccp_tx_conn_resp(g_bsc_nat->ran.sccp_inst->scu, - prim->u.connect.conn_id, - &peer_addr_out, + subscr_conn->ran.id, + &subscr_conn->ran.bsc->addr, oph->msg->data, msgb_length(oph->msg)); rc = 0; @@ -158,14 +120,18 @@ static int sccp_sap_up_cn(struct osmo_prim_hdr *oph, void *scu) case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION): /* connection-oriented data received */ - if (sccp_sap_get_peer_addr_out(sccp_inst, NULL, &peer_addr_out) < 0) + subscr_conn = subscr_conn_get_by_id(prim->u.data.conn_id, BSC_NAT_NET_CN); + if (!subscr_conn) { + LOGP(DMAIN, LOGL_ERROR, "Unknown conn_id=%" PRIu32 " from %s\n", prim->u.data.conn_id, + bsc_nat_print_addr_cn(addr)); goto error; + } - LOGP(DMAIN, LOGL_DEBUG, "Fwd to %s\n", bsc_nat_print_addr_ran(&peer_addr_out)); + LOGP(DMAIN, LOGL_DEBUG, "Fwd via %s\n", talloc_get_name(subscr_conn)); msgb_pull_to_l2(oph->msg); osmo_sccp_tx_data(g_bsc_nat->ran.sccp_inst->scu, - prim->u.data.conn_id, + subscr_conn->ran.id, oph->msg->data, msgb_length(oph->msg)); rc = 0; @@ -173,15 +139,22 @@ static int sccp_sap_up_cn(struct osmo_prim_hdr *oph, void *scu) case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION): /* indication of disconnect */ - if (sccp_sap_get_peer_addr_out(sccp_inst, NULL, &peer_addr_out) < 0) + subscr_conn = subscr_conn_get_by_id(prim->u.disconnect.conn_id, BSC_NAT_NET_CN); + if (!subscr_conn) { + LOGP(DMAIN, LOGL_ERROR, "Unknown conn_id=%" PRIu32 " from %s\n", prim->u.disconnect.conn_id, + bsc_nat_print_addr_cn(addr)); goto error; + } - LOGP(DMAIN, LOGL_DEBUG, "Fwd to %s\n", bsc_nat_print_addr_ran(&peer_addr_out)); + LOGP(DMAIN, LOGL_DEBUG, "Fwd via %s\n", talloc_get_name(subscr_conn)); osmo_sccp_tx_disconn(g_bsc_nat->ran.sccp_inst->scu, - prim->u.disconnect.conn_id, - &prim->u.disconnect.responding_addr, + subscr_conn->ran.id, + &g_bsc_nat->ran.sccp_inst->addr, prim->u.disconnect.cause); + + subscr_conn_free(subscr_conn); + rc = 0; break; @@ -200,16 +173,15 @@ error: return rc; } -/* Handle incoming messages from RAN (BSC). For now this is simplified by - * assuming there is only one MSC, one BSC (not yet translating connection ids - * etc.). */ +/* Handle incoming messages from RAN (BSC) */ static int sccp_sap_up_ran(struct osmo_prim_hdr *oph, void *scu) { struct bsc_nat_sccp_inst *sccp_inst = osmo_sccp_user_get_priv(scu); struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph; struct osmo_sccp_addr *addr; /* BSC's address */ - struct osmo_sccp_addr peer_addr_out; + struct subscr_conn *subscr_conn; struct msc *msc; + struct bsc *bsc; int rc = -1; LOGP(DMAIN, LOGL_DEBUG, "Rx %s from RAN\n", osmo_scu_prim_name(oph)); @@ -227,16 +199,25 @@ static int sccp_sap_up_ran(struct osmo_prim_hdr *oph, void *scu) &prim->u.connect.calling_addr) < 0) goto error; - if (sccp_sap_get_peer_addr_out(sccp_inst, addr, &peer_addr_out) < 0) - goto error; + bsc = bsc_get_by_pc(addr->pc); + if (!bsc) + bsc = bsc_alloc(addr); - LOGP(DMAIN, LOGL_DEBUG, "Fwd to %s\n", bsc_nat_print_addr_cn(&peer_addr_out)); + if (subscr_conn_get_by_id(prim->u.connect.conn_id, BSC_NAT_NET_RAN)) { + LOGP(DMAIN, LOGL_ERROR, "%s attempted to create new connection with already used" + " conn_id=%" PRIu32 ", ignoring\n", talloc_get_name(bsc), prim->u.connect.conn_id); + goto error; + } + + subscr_conn = subscr_conn_alloc(msc, bsc, subscr_conn_get_next_id(BSC_NAT_NET_CN), prim->u.connect.conn_id); + + LOGP(DMAIN, LOGL_DEBUG, "Fwd via %s\n", talloc_get_name(subscr_conn)); msgb_pull_to_l2(oph->msg); osmo_sccp_tx_conn_req(g_bsc_nat->cn.sccp_inst->scu, - prim->u.connect.conn_id, + subscr_conn->cn.id, &g_bsc_nat->cn.sccp_inst->addr, - &peer_addr_out, + &subscr_conn->cn.msc->addr, oph->msg->data, msgb_length(oph->msg)); rc = 0; @@ -248,15 +229,19 @@ static int sccp_sap_up_ran(struct osmo_prim_hdr *oph, void *scu) &prim->u.connect.calling_addr) < 0) goto error; - if (sccp_sap_get_peer_addr_out(sccp_inst, addr, &peer_addr_out) < 0) + subscr_conn = subscr_conn_get_by_id(prim->u.connect.conn_id, BSC_NAT_NET_RAN); + if (!subscr_conn) { + LOGP(DMAIN, LOGL_ERROR, "Unknown conn_id=%" PRIu32 " from %s\n", prim->u.connect.conn_id, + bsc_nat_print_addr_ran(addr)); goto error; + } - LOGP(DMAIN, LOGL_DEBUG, "Fwd to %s\n", bsc_nat_print_addr_cn(&peer_addr_out)); + LOGP(DMAIN, LOGL_DEBUG, "Fwd via %s\n", talloc_get_name(subscr_conn)); msgb_pull_to_l2(oph->msg); osmo_sccp_tx_conn_resp(g_bsc_nat->cn.sccp_inst->scu, - prim->u.connect.conn_id, - &peer_addr_out, + subscr_conn->cn.id, + &subscr_conn->cn.msc->addr, oph->msg->data, msgb_length(oph->msg)); rc = 0; @@ -264,14 +249,18 @@ static int sccp_sap_up_ran(struct osmo_prim_hdr *oph, void *scu) case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION): /* connection-oriented data received */ - if (sccp_sap_get_peer_addr_out(sccp_inst, NULL, &peer_addr_out) < 0) + subscr_conn = subscr_conn_get_by_id(prim->u.data.conn_id, BSC_NAT_NET_RAN); + if (!subscr_conn) { + LOGP(DMAIN, LOGL_ERROR, "Unknown conn_id=%" PRIu32 " from %s\n", prim->u.data.conn_id, + bsc_nat_print_addr_ran(addr)); goto error; + } - LOGP(DMAIN, LOGL_DEBUG, "Fwd to %s\n", bsc_nat_print_addr_cn(&peer_addr_out)); + LOGP(DMAIN, LOGL_DEBUG, "Fwd via %s\n", talloc_get_name(subscr_conn)); msgb_pull_to_l2(oph->msg); osmo_sccp_tx_data(g_bsc_nat->cn.sccp_inst->scu, - prim->u.data.conn_id, + subscr_conn->cn.id, oph->msg->data, msgb_length(oph->msg)); rc = 0; @@ -279,15 +268,22 @@ static int sccp_sap_up_ran(struct osmo_prim_hdr *oph, void *scu) case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION): /* indication of disconnect */ - if (sccp_sap_get_peer_addr_out(sccp_inst, NULL, &peer_addr_out) < 0) + subscr_conn = subscr_conn_get_by_id(prim->u.disconnect.conn_id, BSC_NAT_NET_RAN); + if (!subscr_conn) { + LOGP(DMAIN, LOGL_ERROR, "Unknown conn_id=%" PRIu32 " from %s\n", prim->u.disconnect.conn_id, + bsc_nat_print_addr_ran(addr)); goto error; + } - LOGP(DMAIN, LOGL_DEBUG, "Fwd to %s\n", bsc_nat_print_addr_cn(&peer_addr_out)); + LOGP(DMAIN, LOGL_DEBUG, "Fwd via %s\n", talloc_get_name(subscr_conn)); osmo_sccp_tx_disconn(g_bsc_nat->cn.sccp_inst->scu, - prim->u.disconnect.conn_id, - &prim->u.disconnect.responding_addr, + subscr_conn->cn.id, + &g_bsc_nat->cn.sccp_inst->addr, prim->u.disconnect.cause); + + subscr_conn_free(subscr_conn); + rc = 0; break; diff --git a/src/osmo-bsc-nat/msc.c b/src/osmo-bsc-nat/msc.c index e78a8e6..4204bac 100644 --- a/src/osmo-bsc-nat/msc.c +++ b/src/osmo-bsc-nat/msc.c @@ -22,6 +22,7 @@ #include #include #include +#include extern struct osmo_fsm msc_fsm; @@ -70,9 +71,20 @@ struct msc *msc_get(void) return llist_first_entry(&g_bsc_nat->cn.mscs, struct msc, list); } +void msc_free_subscr_conn_all(struct msc *msc) +{ + struct subscr_conn *subscr_conn; + + llist_for_each_entry(subscr_conn, &g_bsc_nat->subscr_conns, list) { + if (subscr_conn->cn.msc == msc) + subscr_conn_free(subscr_conn); + } +} + void msc_free(struct msc *msc) { LOGP(DMAIN, LOGL_DEBUG, "Del %s\n", talloc_get_name(msc)); + msc_free_subscr_conn_all(msc); llist_del(&msc->list); osmo_fsm_inst_free(msc->fi); talloc_free(msc); diff --git a/src/osmo-bsc-nat/subscr_conn.c b/src/osmo-bsc-nat/subscr_conn.c new file mode 100644 index 0000000..02afb7d --- /dev/null +++ b/src/osmo-bsc-nat/subscr_conn.c @@ -0,0 +1,99 @@ +/* (C) 2022 by sysmocom - s.f.m.c. GmbH + * Author: Oliver Smith + * All Rights Reserved + * + * 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 + * (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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +#include "config.h" +#include +#include +#include +#include +#include +#include +#include + +/* Get the next available id in either CN or RAN. */ +int subscr_conn_get_next_id(enum bsc_nat_net net) +{ + uint32_t *id; + + if (net == BSC_NAT_NET_RAN) + id = &g_bsc_nat->ran.subscr_conn_id_next; + else + id = &g_bsc_nat->cn.subscr_conn_id_next; + + for (int i = 0; i < 0xFFFFFF; i++) { + struct subscr_conn *subscr_conn; + bool already_used = false; + + *id = (*id + 1) & 0xffffff; + + llist_for_each_entry(subscr_conn, &g_bsc_nat->subscr_conns, list) { + if ((net == BSC_NAT_NET_RAN && subscr_conn->ran.id == *id) + || (net == BSC_NAT_NET_CN && subscr_conn->cn.id == *id)) { + already_used = true; + break; + } + } + + if (!already_used) + return *id; + } + return -1; +} + +struct subscr_conn *subscr_conn_alloc(struct msc *msc, struct bsc *bsc, uint32_t id_cn, uint32_t id_ran) +{ + struct subscr_conn *subscr_conn = talloc_zero(g_bsc_nat, struct subscr_conn); + + OSMO_ASSERT(subscr_conn); + talloc_set_name(subscr_conn, "SUBSCR-CONN %s:%" PRIu32 " <=> %s:%" PRIu32, + talloc_get_name(msc), id_cn, + talloc_get_name(bsc), id_ran); + + LOGP(DMAIN, LOGL_DEBUG, "Add %s\n", talloc_get_name(subscr_conn)); + + subscr_conn->cn.id = id_cn; + subscr_conn->cn.msc = msc; + subscr_conn->ran.id = id_ran; + subscr_conn->ran.bsc = bsc; + + INIT_LLIST_HEAD(&subscr_conn->list); + llist_add(&subscr_conn->list, &g_bsc_nat->subscr_conns); + + return subscr_conn; +} + +struct subscr_conn *subscr_conn_get_by_id(uint32_t id, enum bsc_nat_net net) +{ + struct subscr_conn *subscr_conn; + + llist_for_each_entry(subscr_conn, &g_bsc_nat->subscr_conns, list) { + if ((net == BSC_NAT_NET_RAN && subscr_conn->ran.id == id) + || (net == BSC_NAT_NET_CN && subscr_conn->cn.id == id)) + return subscr_conn; + } + + return NULL; +} + +void subscr_conn_free(struct subscr_conn *subscr_conn) +{ + LOGP(DMAIN, LOGL_DEBUG, "Del %s\n", talloc_get_name(subscr_conn)); + llist_del(&subscr_conn->list); + talloc_free(subscr_conn); +}