Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 1 | /* (C) 2017 by sysmocom s.f.m.c. GmbH |
Harald Welte | c9e7859 | 2018-02-09 01:42:50 +0100 | [diff] [blame] | 2 | * (C) 2018 by Harald Welte <laforge@gnumonks.org> |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 3 | * All Rights Reserved |
| 4 | * |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 5 | * Author: Philipp Maier |
| 6 | * |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 22 | #include <osmocom/core/utils.h> |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 23 | #include <osmocom/core/msgb.h> |
| 24 | #include <osmocom/core/logging.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 25 | #include <osmocom/sigtran/sccp_helpers.h> |
| 26 | #include <osmocom/sigtran/sccp_sap.h> |
| 27 | #include <osmocom/sigtran/osmo_ss7.h> |
| 28 | #include <osmocom/sigtran/protocol/m3ua.h> |
| 29 | #include <osmocom/gsm/gsm0808.h> |
| 30 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 31 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 32 | #include <osmocom/gsm/gsm0808_utils.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 33 | #include <osmocom/msc/debug.h> |
| 34 | #include <osmocom/msc/msc_ifaces.h> |
| 35 | #include <osmocom/msc/a_iface.h> |
| 36 | #include <osmocom/msc/a_iface_bssap.h> |
| 37 | #include <osmocom/msc/transaction.h> |
Neels Hofmeyr | 6c8afe1 | 2017-09-04 01:03:58 +0200 | [diff] [blame] | 38 | #include <osmocom/mgcp_client/mgcp_client.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 39 | #include <osmocom/core/byteswap.h> |
| 40 | #include <osmocom/sccp/sccp_types.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 41 | #include <osmocom/msc/a_reset.h> |
Neels Hofmeyr | f879fc9 | 2017-12-14 03:52:18 +0100 | [diff] [blame] | 42 | #include <osmocom/msc/vlr.h> |
Neels Hofmeyr | 46c06e2 | 2019-01-04 17:42:05 +0100 | [diff] [blame] | 43 | #include <osmocom/msc/ran_conn.h> |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 44 | |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 45 | #include <errno.h> |
| 46 | |
Neels Hofmeyr | 46c06e2 | 2019-01-04 17:42:05 +0100 | [diff] [blame] | 47 | #define LOGPCONN LOG_RAN_CONN |
| 48 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 49 | /* A pointer to the GSM network we work with. By the current paradigm, |
| 50 | * there can only be one gsm_network per MSC. The pointer is set once |
| 51 | * when calling a_init() */ |
| 52 | static struct gsm_network *gsm_network = NULL; |
| 53 | |
| 54 | /* A struct to track currently active connections. We need that information |
| 55 | * to handle failure sitautions. In case of a problem, we must know which |
| 56 | * connections are currently open and which BSC is responsible. We also need |
| 57 | * the data to perform our connection checks (a_reset). All other logic will |
| 58 | * look at the connection ids and addresses that are supplied by the |
| 59 | * primitives */ |
| 60 | struct bsc_conn { |
| 61 | struct llist_head list; |
| 62 | uint32_t conn_id; /* Connection identifier */ |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 63 | struct bsc_context *bsc; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /* Internal list with connections we currently maintain. This |
| 67 | * list is of type struct bsc_conn (see above) */ |
| 68 | static LLIST_HEAD(active_connections); |
| 69 | |
| 70 | /* Record info of a new active connection in the active connection list */ |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 71 | static void record_bsc_con(const void *ctx, struct bsc_context *bsc, uint32_t conn_id) |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 72 | { |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 73 | struct bsc_conn *conn; |
| 74 | |
| 75 | conn = talloc_zero(ctx, struct bsc_conn); |
| 76 | OSMO_ASSERT(conn); |
| 77 | |
| 78 | conn->conn_id = conn_id; |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 79 | conn->bsc = bsc; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 80 | |
| 81 | llist_add_tail(&conn->list, &active_connections); |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 84 | /* Delete info of a closed connection from the active connection list */ |
| 85 | void a_delete_bsc_con(uint32_t conn_id) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 86 | { |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 87 | struct bsc_conn *conn; |
| 88 | struct bsc_conn *conn_temp; |
| 89 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 90 | llist_for_each_entry_safe(conn, conn_temp, &active_connections, list) { |
| 91 | if (conn->conn_id == conn_id) { |
Neels Hofmeyr | 46c06e2 | 2019-01-04 17:42:05 +0100 | [diff] [blame] | 92 | LOGP(DBSSAP, LOGL_DEBUG, "(conn%u) Removing A-interface conn\n", conn->conn_id); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 93 | llist_del(&conn->list); |
| 94 | talloc_free(conn); |
| 95 | } |
| 96 | } |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 99 | /* Find a specified connection id */ |
| 100 | static struct bsc_conn *find_bsc_con(uint32_t conn_id) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 101 | { |
| 102 | struct bsc_conn *conn; |
| 103 | |
| 104 | /* Find the address for the current connection id */ |
| 105 | llist_for_each_entry(conn, &active_connections, list) { |
| 106 | if (conn->conn_id == conn_id) { |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 107 | return conn; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 111 | return NULL; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 114 | /* Check if a specified connection id has an active SCCP connection */ |
| 115 | static bool check_connection_active(uint32_t conn_id) |
| 116 | { |
| 117 | if (find_bsc_con(conn_id)) |
| 118 | return true; |
| 119 | else |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | /* Get the context for a specific calling (BSC) address */ |
| 124 | static struct bsc_context *get_bsc_context_by_sccp_addr(const struct osmo_sccp_addr *addr) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 125 | { |
| 126 | struct bsc_context *bsc_ctx; |
| 127 | struct osmo_ss7_instance *ss7; |
| 128 | |
| 129 | if (!addr) |
| 130 | return NULL; |
| 131 | |
| 132 | llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) { |
| 133 | if (memcmp(&bsc_ctx->bsc_addr, addr, sizeof(*addr)) == 0) |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 134 | return bsc_ctx; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance); |
| 138 | OSMO_ASSERT(ss7); |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 139 | LOGP(DBSSAP, LOGL_NOTICE, "The calling BSC (%s) is unknown to this MSC ...\n", |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 140 | osmo_sccp_addr_name(ss7, addr)); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
Harald Welte | 977b548 | 2019-02-18 12:23:43 +0100 | [diff] [blame] | 144 | /* wrapper around osmo_sccp_tx_data_msg(): Transmit a fully encoded BSSAP (DTAP or BSSMAP) message */ |
| 145 | static int a_iface_tx_bssap(const struct ran_conn *conn, struct msgb *msg) |
| 146 | { |
| 147 | OSMO_ASSERT(conn); |
| 148 | OSMO_ASSERT(conn->a.scu); |
| 149 | |
Harald Welte | 390d140 | 2019-02-18 12:55:31 +0100 | [diff] [blame] | 150 | LOGPCONN(conn, LOGL_DEBUG, "N-DATA.req(%s)\n", msgb_hexdump_l3(msg)); |
Harald Welte | 31f4c1f | 2019-02-18 13:02:00 +0100 | [diff] [blame] | 151 | |
| 152 | /* some consistency checks to ensure we don't send invalid length */ |
| 153 | switch (msg->l3h[0]) { |
| 154 | case BSSAP_MSG_DTAP: |
| 155 | OSMO_ASSERT(msgb_l3len(msg) == msg->l3h[2] + 3); |
| 156 | break; |
| 157 | case BSSAP_MSG_BSS_MANAGEMENT: |
| 158 | OSMO_ASSERT(msgb_l3len(msg) == msg->l3h[1] + 2); |
| 159 | break; |
| 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | |
Harald Welte | 977b548 | 2019-02-18 12:23:43 +0100 | [diff] [blame] | 164 | return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg); |
| 165 | } |
| 166 | |
Philipp Maier | 4502f5f | 2017-09-07 11:39:58 +0200 | [diff] [blame] | 167 | /* Send DTAP message via A-interface, take ownership of msg */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 168 | int a_iface_tx_dtap(struct msgb *msg) |
| 169 | { |
Harald Welte | fd96d45 | 2019-02-18 12:28:09 +0100 | [diff] [blame] | 170 | const struct ran_conn *conn; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 171 | struct msgb *msg_resp; |
| 172 | |
Harald Welte | 0e2fa5d | 2018-04-09 16:35:01 +0200 | [diff] [blame] | 173 | uint8_t link_id = OMSC_LINKID_CB(msg); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 174 | OSMO_ASSERT(msg); |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 175 | conn = (struct ran_conn *)msg->dst; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 176 | OSMO_ASSERT(conn); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 177 | |
Harald Welte | 0e2fa5d | 2018-04-09 16:35:01 +0200 | [diff] [blame] | 178 | LOGPCONN(conn, LOGL_DEBUG, "Passing DTAP message (DLCI=0x%02x) from MSC to BSC\n", link_id); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 179 | |
| 180 | msg->l3h = msg->data; |
| 181 | msg_resp = gsm0808_create_dtap(msg, link_id); |
Philipp Maier | 4502f5f | 2017-09-07 11:39:58 +0200 | [diff] [blame] | 182 | |
| 183 | /* gsm0808_create_dtap() has copied the data to msg_resp, |
| 184 | * so msg has served its purpose now */ |
| 185 | msgb_free(msg); |
| 186 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 187 | if (!msg_resp) { |
Neels Hofmeyr | 04960b1 | 2017-12-18 05:17:25 +0100 | [diff] [blame] | 188 | LOGPCONN(conn, LOGL_ERROR, "Unable to generate BSSMAP DTAP message!\n"); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 189 | return -EINVAL; |
Neels Hofmeyr | 04960b1 | 2017-12-18 05:17:25 +0100 | [diff] [blame] | 190 | } |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 191 | |
Philipp Maier | 4502f5f | 2017-09-07 11:39:58 +0200 | [diff] [blame] | 192 | /* osmo_sccp_tx_data_msg() takes ownership of msg_resp */ |
Harald Welte | 977b548 | 2019-02-18 12:23:43 +0100 | [diff] [blame] | 193 | return a_iface_tx_bssap(conn, msg_resp); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* Send Cipher mode command via A-interface */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 197 | int a_iface_tx_cipher_mode(const struct ran_conn *conn, |
Neels Hofmeyr | 703638e | 2017-12-14 05:30:16 +0100 | [diff] [blame] | 198 | struct gsm0808_encrypt_info *ei, int include_imeisv) |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 199 | { |
| 200 | /* TODO generalize for A- and Iu interfaces, don't name after 08.08 */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 201 | struct msgb *msg_resp; |
Neels Hofmeyr | 703638e | 2017-12-14 05:30:16 +0100 | [diff] [blame] | 202 | uint8_t crm = 0x01; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 203 | |
| 204 | OSMO_ASSERT(conn); |
Harald Welte | fb7ba91 | 2018-02-09 01:05:27 +0100 | [diff] [blame] | 205 | LOGPCONN(conn, LOGL_DEBUG, "Tx BSSMAP CIPHER MODE COMMAND to BSC, %u ciphers (%s)", |
Neels Hofmeyr | 703638e | 2017-12-14 05:30:16 +0100 | [diff] [blame] | 206 | ei->perm_algo_len, osmo_hexdump_nospc(ei->perm_algo, ei->perm_algo_len)); |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 207 | LOGPC(DBSSAP, LOGL_DEBUG, " key %s\n", osmo_hexdump_nospc(ei->key, ei->key_len)); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 208 | |
Neels Hofmeyr | 703638e | 2017-12-14 05:30:16 +0100 | [diff] [blame] | 209 | msg_resp = gsm0808_create_cipher(ei, include_imeisv ? &crm : NULL); |
Harald Welte | 977b548 | 2019-02-18 12:23:43 +0100 | [diff] [blame] | 210 | return a_iface_tx_bssap(conn, msg_resp); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* Page a subscriber via A-interface */ |
| 214 | int a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac) |
| 215 | { |
| 216 | struct bsc_context *bsc_ctx; |
Stefan Sperling | 621c729 | 2018-02-16 13:40:42 +0100 | [diff] [blame] | 217 | struct gsm0808_cell_id_list2 cil; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 218 | struct msgb *msg; |
| 219 | int page_count = 0; |
| 220 | struct osmo_ss7_instance *ss7; |
| 221 | |
| 222 | OSMO_ASSERT(imsi); |
| 223 | |
| 224 | cil.id_discr = CELL_IDENT_LAC; |
Stefan Sperling | 621c729 | 2018-02-16 13:40:42 +0100 | [diff] [blame] | 225 | cil.id_list[0].lac = lac; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 226 | cil.id_list_len = 1; |
| 227 | |
| 228 | ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance); |
| 229 | OSMO_ASSERT(ss7); |
| 230 | |
| 231 | /* Deliver paging request to all known BSCs */ |
| 232 | llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) { |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 233 | if (a_reset_conn_ready(bsc_ctx->reset_fsm)) { |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 234 | LOGP(DBSSAP, LOGL_DEBUG, |
Harald Welte | fb7ba91 | 2018-02-09 01:05:27 +0100 | [diff] [blame] | 235 | "Tx BSSMAP paging message from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n", |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 236 | osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr), |
| 237 | osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac); |
Stefan Sperling | 621c729 | 2018-02-16 13:40:42 +0100 | [diff] [blame] | 238 | msg = gsm0808_create_paging2(imsi, &tmsi, &cil, NULL); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 239 | osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user, |
| 240 | &bsc_ctx->msc_addr, &bsc_ctx->bsc_addr, msg); |
| 241 | page_count++; |
| 242 | } else { |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 243 | LOGP(DBSSAP, LOGL_DEBUG, |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 244 | "Connection down, dropping paging from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n", |
| 245 | osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr), |
| 246 | osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if (page_count <= 0) |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 251 | LOGP(DBSSAP, LOGL_ERROR, "Could not deliver paging because none of the associated BSCs is available!\n"); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 252 | |
| 253 | return page_count; |
| 254 | } |
| 255 | |
| 256 | /* Convert speech version field */ |
Harald Welte | 474e5a7 | 2018-02-12 10:12:35 +0100 | [diff] [blame] | 257 | static uint8_t convert_speech_version_l3_to_A(int speech_ver) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 258 | { |
| 259 | /* The speech versions that are transmitted in the Bearer capability |
Harald Welte | f417b8c | 2018-02-12 10:10:05 +0100 | [diff] [blame] | 260 | * information element, that is transmitted on the Layer 3 (CC) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 261 | * use a different encoding than the permitted speech version |
| 262 | * identifier, that is signalled in the channel type element on the A |
| 263 | * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008, |
| 264 | * 10.5.103 */ |
| 265 | |
| 266 | switch (speech_ver) { |
| 267 | case GSM48_BCAP_SV_FR: |
| 268 | return GSM0808_PERM_FR1; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 269 | case GSM48_BCAP_SV_HR: |
| 270 | return GSM0808_PERM_HR1; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 271 | case GSM48_BCAP_SV_EFR: |
| 272 | return GSM0808_PERM_FR2; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 273 | case GSM48_BCAP_SV_AMR_F: |
| 274 | return GSM0808_PERM_FR3; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 275 | case GSM48_BCAP_SV_AMR_H: |
| 276 | return GSM0808_PERM_HR3; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 277 | case GSM48_BCAP_SV_AMR_OFW: |
| 278 | return GSM0808_PERM_FR4; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 279 | case GSM48_BCAP_SV_AMR_OHW: |
| 280 | return GSM0808_PERM_HR4; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 281 | case GSM48_BCAP_SV_AMR_FW: |
| 282 | return GSM0808_PERM_FR5; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 283 | case GSM48_BCAP_SV_AMR_OH: |
| 284 | return GSM0808_PERM_HR6; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* If nothing matches, tag the result as invalid */ |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 288 | LOGP(DBSSAP, LOGL_ERROR, "Invalid permitted speech version: %d\n", speech_ver); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 289 | return 0xFF; |
| 290 | } |
| 291 | |
| 292 | /* Convert speech preference field */ |
Harald Welte | 474e5a7 | 2018-02-12 10:12:35 +0100 | [diff] [blame] | 293 | static uint8_t convert_speech_pref_l3_to_A(int radio) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 294 | { |
| 295 | /* The Radio channel requirement field that is transmitted in the |
| 296 | * Bearer capability information element, that is transmitted on the |
Harald Welte | f417b8c | 2018-02-12 10:10:05 +0100 | [diff] [blame] | 297 | * Layer 3 (CC) uses a different encoding than the Channel rate and |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 298 | * type field that is signalled in the channel type element on the A |
| 299 | * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008, |
| 300 | * 10.5.102 */ |
| 301 | |
| 302 | switch (radio) { |
| 303 | case GSM48_BCAP_RRQ_FR_ONLY: |
| 304 | return GSM0808_SPEECH_FULL_BM; |
| 305 | case GSM48_BCAP_RRQ_DUAL_FR: |
| 306 | return GSM0808_SPEECH_FULL_PREF; |
| 307 | case GSM48_BCAP_RRQ_DUAL_HR: |
| 308 | return GSM0808_SPEECH_HALF_PREF; |
| 309 | } |
| 310 | |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 311 | LOGP(DBSSAP, LOGL_ERROR, "Invalid radio channel preference: %d; defaulting to full rate.\n", |
Neels Hofmeyr | 04960b1 | 2017-12-18 05:17:25 +0100 | [diff] [blame] | 312 | radio); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 313 | return GSM0808_SPEECH_FULL_BM; |
| 314 | } |
| 315 | |
| 316 | /* Assemble the channel type field */ |
| 317 | static int enc_channel_type(struct gsm0808_channel_type *ct, const struct gsm_mncc_bearer_cap *bc) |
| 318 | { |
| 319 | unsigned int i; |
| 320 | uint8_t sv; |
| 321 | unsigned int count = 0; |
| 322 | bool only_gsm_hr = true; |
| 323 | |
| 324 | OSMO_ASSERT(ct); |
| 325 | OSMO_ASSERT(bc); |
| 326 | |
| 327 | ct->ch_indctr = GSM0808_CHAN_SPEECH; |
| 328 | |
| 329 | for (i = 0; i < ARRAY_SIZE(bc->speech_ver); i++) { |
| 330 | if (bc->speech_ver[i] == -1) |
| 331 | break; |
Harald Welte | 474e5a7 | 2018-02-12 10:12:35 +0100 | [diff] [blame] | 332 | sv = convert_speech_version_l3_to_A(bc->speech_ver[i]); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 333 | if (sv != 0xFF) { |
| 334 | /* Detect if something else than |
| 335 | * GSM HR V1 is supported */ |
| 336 | if (sv == GSM0808_PERM_HR2 || |
| 337 | sv == GSM0808_PERM_HR3 || sv == GSM0808_PERM_HR4 || sv == GSM0808_PERM_HR6) |
| 338 | only_gsm_hr = false; |
| 339 | |
| 340 | ct->perm_spch[count] = sv; |
| 341 | count++; |
| 342 | } |
| 343 | } |
| 344 | ct->perm_spch_len = count; |
| 345 | |
| 346 | if (only_gsm_hr) |
| 347 | /* Note: We must avoid the usage of GSM HR1 as this |
| 348 | * codec only offers very poor audio quality. If the |
| 349 | * MS only supports GSM HR1 (and full rate), and has |
| 350 | * a preference for half rate. Then we will ignore the |
| 351 | * preference and assume a preference for full rate. */ |
| 352 | ct->ch_rate_type = GSM0808_SPEECH_FULL_BM; |
| 353 | else |
Harald Welte | 474e5a7 | 2018-02-12 10:12:35 +0100 | [diff] [blame] | 354 | ct->ch_rate_type = convert_speech_pref_l3_to_A(bc->radio); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 355 | |
| 356 | if (count) |
| 357 | return 0; |
| 358 | else |
| 359 | return -EINVAL; |
| 360 | } |
| 361 | |
| 362 | /* Assemble the speech codec field */ |
| 363 | static int enc_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct) |
| 364 | { |
| 365 | unsigned int i; |
| 366 | int rc; |
| 367 | |
| 368 | memset(scl, 0, sizeof(*scl)); |
| 369 | for (i = 0; i < ct->perm_spch_len; i++) { |
| 370 | rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]); |
| 371 | if (rc != 0) |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | scl->len = i; |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* Send assignment request via A-interface */ |
| 380 | int a_iface_tx_assignment(const struct gsm_trans *trans) |
| 381 | { |
Harald Welte | fd96d45 | 2019-02-18 12:28:09 +0100 | [diff] [blame] | 382 | const struct ran_conn *conn; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 383 | struct gsm0808_channel_type ct; |
| 384 | struct gsm0808_speech_codec_list scl; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 385 | struct msgb *msg; |
| 386 | struct sockaddr_storage rtp_addr; |
| 387 | struct sockaddr_in rtp_addr_in; |
| 388 | int rc; |
| 389 | |
| 390 | OSMO_ASSERT(trans); |
| 391 | conn = trans->conn; |
| 392 | OSMO_ASSERT(conn); |
| 393 | |
Harald Welte | fb7ba91 | 2018-02-09 01:05:27 +0100 | [diff] [blame] | 394 | LOGPCONN(conn, LOGL_DEBUG, "Tx BSSMAP ASSIGNMENT COMMAND to BSC\n"); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 395 | |
| 396 | /* Channel type */ |
| 397 | rc = enc_channel_type(&ct, &trans->bearer_cap); |
| 398 | if (rc < 0) { |
Neels Hofmeyr | 04960b1 | 2017-12-18 05:17:25 +0100 | [diff] [blame] | 399 | LOGPCONN(conn, LOGL_ERROR, "Not sending Assignment to BSC: failed to generate channel type\n"); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 400 | return -EINVAL; |
| 401 | } |
| 402 | |
| 403 | /* Speech codec list */ |
| 404 | rc = enc_speech_codec_list(&scl, &ct); |
| 405 | if (rc < 0) { |
Neels Hofmeyr | 04960b1 | 2017-12-18 05:17:25 +0100 | [diff] [blame] | 406 | LOGPCONN(conn, LOGL_ERROR, "Not sending Assignment to BSC: failed to generate speech codec list\n"); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 407 | return -EINVAL; |
| 408 | } |
| 409 | |
| 410 | /* Package RTP-Address data */ |
| 411 | memset(&rtp_addr_in, 0, sizeof(rtp_addr_in)); |
| 412 | rtp_addr_in.sin_family = AF_INET; |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 413 | rtp_addr_in.sin_port = osmo_htons(conn->rtp.local_port_ran); |
| 414 | rtp_addr_in.sin_addr.s_addr = inet_addr(conn->rtp.local_addr_ran); |
| 415 | |
| 416 | if (rtp_addr_in.sin_addr.s_addr == INADDR_NONE) { |
| 417 | LOGPCONN(conn, LOGL_ERROR, "Invalid RTP-Address -- assignment not sent!\n"); |
| 418 | return -EINVAL; |
| 419 | } |
| 420 | if (rtp_addr_in.sin_port == 0) { |
| 421 | LOGPCONN(conn, LOGL_ERROR, "Invalid RTP-Port -- assignment not sent!\n"); |
| 422 | return -EINVAL; |
| 423 | } |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 424 | |
| 425 | memset(&rtp_addr, 0, sizeof(rtp_addr)); |
| 426 | memcpy(&rtp_addr, &rtp_addr_in, sizeof(rtp_addr_in)); |
| 427 | |
Max | 020ec7a | 2018-12-17 16:03:55 +0100 | [diff] [blame] | 428 | msg = gsm0808_create_ass(&ct, NULL, &rtp_addr, &scl, NULL); |
Harald Welte | 977b548 | 2019-02-18 12:23:43 +0100 | [diff] [blame] | 429 | return a_iface_tx_bssap(conn, msg); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* Send clear command via A-interface */ |
Harald Welte | fd96d45 | 2019-02-18 12:28:09 +0100 | [diff] [blame] | 433 | int a_iface_tx_clear_cmd(const struct ran_conn *conn) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 434 | { |
| 435 | struct msgb *msg; |
Philipp Maier | 896950a | 2019-02-04 16:46:55 +0100 | [diff] [blame] | 436 | struct vlr_subscr *vsub = conn->vsub; |
| 437 | bool csfb_ind = false; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 438 | |
Harald Welte | fb7ba91 | 2018-02-09 01:05:27 +0100 | [diff] [blame] | 439 | LOGPCONN(conn, LOGL_INFO, "Tx BSSMAP CLEAR COMMAND to BSC\n"); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 440 | |
Philipp Maier | 896950a | 2019-02-04 16:46:55 +0100 | [diff] [blame] | 441 | if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED) |
| 442 | csfb_ind = true; |
| 443 | |
| 444 | msg = gsm0808_create_clear_command2(GSM0808_CAUSE_CALL_CONTROL, csfb_ind); |
Harald Welte | 977b548 | 2019-02-18 12:23:43 +0100 | [diff] [blame] | 445 | return a_iface_tx_bssap(conn, msg); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 448 | int a_iface_tx_classmark_request(const struct ran_conn *conn) |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 449 | { |
| 450 | struct msgb *msg; |
| 451 | |
| 452 | LOGPCONN(conn, LOGL_INFO, "Tx BSSMAP CLASSMARK REQUEST to BSC\n"); |
| 453 | |
| 454 | msg = gsm0808_create_classmark_request(); |
Harald Welte | 977b548 | 2019-02-18 12:23:43 +0100 | [diff] [blame] | 455 | return a_iface_tx_bssap(conn, msg); |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 458 | /* Callback function: Close all open connections */ |
| 459 | static void a_reset_cb(const void *priv) |
| 460 | { |
| 461 | struct msgb *msg; |
| 462 | struct bsc_context *bsc_ctx = (struct bsc_context*) priv; |
| 463 | struct osmo_ss7_instance *ss7; |
| 464 | |
| 465 | /* Skip if the A interface is not properly initalized yet */ |
| 466 | if (!gsm_network) |
| 467 | return; |
| 468 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 469 | /* Clear all now orphaned RAN connections */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 470 | a_clear_all(bsc_ctx->sccp_user, &bsc_ctx->bsc_addr); |
| 471 | |
| 472 | /* Send reset to the remote BSC */ |
| 473 | ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance); |
| 474 | OSMO_ASSERT(ss7); |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 475 | LOGP(DBSSAP, LOGL_NOTICE, "Tx BSSMAP RESET to BSC %s\n", osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr)); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 476 | msg = gsm0808_create_reset(); |
| 477 | osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user, &bsc_ctx->msc_addr, |
| 478 | &bsc_ctx->bsc_addr, msg); |
| 479 | } |
| 480 | |
| 481 | /* Add a new BSC connection to our internal list with known BSCs */ |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 482 | static struct bsc_context *add_bsc(const struct osmo_sccp_addr *msc_addr, |
| 483 | const struct osmo_sccp_addr *bsc_addr, struct osmo_sccp_user *scu) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 484 | { |
| 485 | struct bsc_context *bsc_ctx; |
| 486 | struct osmo_ss7_instance *ss7; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 487 | |
| 488 | ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance); |
| 489 | OSMO_ASSERT(ss7); |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 490 | LOGP(DBSSAP, LOGL_NOTICE, "Adding new BSC connection for BSC %s...\n", osmo_sccp_addr_name(ss7, bsc_addr)); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 491 | |
| 492 | /* Generate and fill up a new bsc context */ |
| 493 | bsc_ctx = talloc_zero(gsm_network, struct bsc_context); |
| 494 | OSMO_ASSERT(bsc_ctx); |
| 495 | memcpy(&bsc_ctx->bsc_addr, bsc_addr, sizeof(*bsc_addr)); |
| 496 | memcpy(&bsc_ctx->msc_addr, msc_addr, sizeof(*msc_addr)); |
| 497 | bsc_ctx->sccp_user = scu; |
| 498 | llist_add_tail(&bsc_ctx->list, &gsm_network->a.bscs); |
| 499 | |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 500 | return bsc_ctx; |
| 501 | } |
| 502 | |
| 503 | /* start the BSSMAP RESET fsm */ |
| 504 | void a_start_reset(struct bsc_context *bsc_ctx, bool already_connected) |
| 505 | { |
| 506 | char bsc_name[32]; |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 507 | OSMO_ASSERT(bsc_ctx->reset_fsm == NULL); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 508 | /* Start reset procedure to make the new connection active */ |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 509 | snprintf(bsc_name, sizeof(bsc_name), "bsc-%i", bsc_ctx->bsc_addr.pc); |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 510 | bsc_ctx->reset_fsm = a_reset_alloc(bsc_ctx, bsc_name, a_reset_cb, bsc_ctx, already_connected); |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Philipp Maier | ce1298b | 2018-02-22 16:44:41 +0100 | [diff] [blame] | 513 | /* determine if given msg is BSSMAP RESET related (true) or not (false) */ |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 514 | static bool bssmap_is_reset(struct msgb *msg) |
| 515 | { |
| 516 | struct bssmap_header *bs = (struct bssmap_header *)msgb_l2(msg); |
| 517 | |
| 518 | if (msgb_l2len(msg) < sizeof(*bs)) |
| 519 | return false; |
| 520 | |
| 521 | if (bs->type != BSSAP_MSG_BSS_MANAGEMENT) |
| 522 | return false; |
| 523 | |
| 524 | if (msg->l2h[sizeof(*bs)] == BSS_MAP_MSG_RESET) |
| 525 | return true; |
| 526 | |
Philipp Maier | ce1298b | 2018-02-22 16:44:41 +0100 | [diff] [blame] | 527 | if (msg->l2h[sizeof(*bs)] == BSS_MAP_MSG_RESET_ACKNOWLEDGE) |
| 528 | return true; |
| 529 | |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 530 | return false; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* Callback function, called by the SSCP stack when data arrives */ |
| 534 | static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu) |
| 535 | { |
| 536 | struct osmo_sccp_user *scu = _scu; |
| 537 | struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *)oph; |
| 538 | int rc = 0; |
| 539 | struct a_conn_info a_conn_info; |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 540 | struct bsc_conn *bsc_con; |
| 541 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 542 | memset(&a_conn_info, 0, sizeof(a_conn_info)); |
| 543 | a_conn_info.network = gsm_network; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 544 | |
| 545 | switch (OSMO_PRIM_HDR(&scu_prim->oph)) { |
| 546 | case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION): |
| 547 | /* Handle inbound connection indication */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 548 | a_conn_info.conn_id = scu_prim->u.connect.conn_id; |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 549 | a_conn_info.bsc = get_bsc_context_by_sccp_addr(&scu_prim->u.unitdata.calling_addr); |
| 550 | if (!a_conn_info.bsc) { |
| 551 | /* We haven't heard from this BSC before, allocate it */ |
| 552 | a_conn_info.bsc = add_bsc(&scu_prim->u.connect.called_addr, |
| 553 | &scu_prim->u.connect.calling_addr, scu); |
| 554 | a_start_reset(a_conn_info.bsc, false); |
| 555 | } else { |
| 556 | /* This BSC is already known to us, check if we have been through reset yet */ |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 557 | if (a_reset_conn_ready(a_conn_info.bsc->reset_fsm) == false) { |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 558 | LOGP(DBSSAP, LOGL_NOTICE, "Refusing N-CONNECT.ind(%u, %s), BSC not reset yet\n", |
Harald Welte | a41b630 | 2018-02-09 00:27:56 +0100 | [diff] [blame] | 559 | scu_prim->u.connect.conn_id, msgb_hexdump_l2(oph->msg)); |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 560 | rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id, &a_conn_info.bsc->msc_addr, |
| 561 | SCCP_RETURN_CAUSE_UNQUALIFIED); |
| 562 | break; |
| 563 | } |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 564 | |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 565 | osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, NULL, 0); |
| 566 | if (msgb_l2len(oph->msg) > 0) { |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 567 | LOGP(DBSSAP, LOGL_DEBUG, "N-CONNECT.ind(%u, %s)\n", |
Harald Welte | a41b630 | 2018-02-09 00:27:56 +0100 | [diff] [blame] | 568 | scu_prim->u.connect.conn_id, msgb_hexdump_l2(oph->msg)); |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 569 | rc = a_sccp_rx_dt(scu, &a_conn_info, oph->msg); |
Harald Welte | 8a991ed | 2018-03-18 21:56:04 +0100 | [diff] [blame] | 570 | } else { |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 571 | LOGP(DBSSAP, LOGL_DEBUG, "N-CONNECT.ind(%u)\n", scu_prim->u.connect.conn_id); |
Harald Welte | 8a991ed | 2018-03-18 21:56:04 +0100 | [diff] [blame] | 572 | rc = -ENODATA; |
| 573 | } |
| 574 | |
| 575 | if (rc < 0) { |
| 576 | /* initial message (COMPL L3) caused some error, we didn't allocate |
| 577 | * a subscriber_conn and must close the connection again */ |
| 578 | rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id, |
| 579 | &a_conn_info.bsc->msc_addr, |
| 580 | SCCP_RETURN_CAUSE_UNQUALIFIED); |
| 581 | } else |
| 582 | record_bsc_con(scu, a_conn_info.bsc, scu_prim->u.connect.conn_id); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 583 | } |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 584 | break; |
| 585 | |
| 586 | case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION): |
| 587 | /* Handle incoming connection oriented data */ |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 588 | bsc_con = find_bsc_con(scu_prim->u.data.conn_id); |
| 589 | if (!bsc_con) { |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 590 | LOGP(DBSSAP, LOGL_ERROR, "N-DATA.ind(%u, %s) for unknown conn_id\n", |
Harald Welte | a41b630 | 2018-02-09 00:27:56 +0100 | [diff] [blame] | 591 | scu_prim->u.data.conn_id, msgb_hexdump_l2(oph->msg)); |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 592 | break; |
| 593 | } |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 594 | a_conn_info.conn_id = scu_prim->u.data.conn_id; |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 595 | a_conn_info.bsc = bsc_con->bsc; |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 596 | LOGP(DBSSAP, LOGL_DEBUG, "N-DATA.ind(%u, %s)\n", |
Harald Welte | a41b630 | 2018-02-09 00:27:56 +0100 | [diff] [blame] | 597 | scu_prim->u.data.conn_id, msgb_hexdump_l2(oph->msg)); |
Neels Hofmeyr | c1d6925 | 2017-12-18 04:06:04 +0100 | [diff] [blame] | 598 | a_sccp_rx_dt(scu, &a_conn_info, oph->msg); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 599 | break; |
| 600 | |
| 601 | case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION): |
| 602 | /* Handle inbound UNITDATA */ |
Philipp Maier | ce1298b | 2018-02-22 16:44:41 +0100 | [diff] [blame] | 603 | |
| 604 | /* Get BSC context, create a new one if necessary */ |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 605 | a_conn_info.bsc = get_bsc_context_by_sccp_addr(&scu_prim->u.unitdata.calling_addr); |
| 606 | if (!a_conn_info.bsc) { |
| 607 | /* We haven't heard from this BSC before, allocate it */ |
| 608 | a_conn_info.bsc = add_bsc(&scu_prim->u.unitdata.called_addr, |
| 609 | &scu_prim->u.unitdata.calling_addr, scu); |
Philipp Maier | ce1298b | 2018-02-22 16:44:41 +0100 | [diff] [blame] | 610 | /* Make sure that reset procedure is started */ |
| 611 | a_start_reset(a_conn_info.bsc, false); |
Harald Welte | 54a10ef | 2018-02-09 00:09:16 +0100 | [diff] [blame] | 612 | } |
Philipp Maier | ce1298b | 2018-02-22 16:44:41 +0100 | [diff] [blame] | 613 | |
| 614 | /* As long as we are in the reset phase, only reset related BSSMAP messages may pass |
| 615 | * beond here. */ |
Philipp Maier | f913e5f | 2018-05-07 10:15:49 +0200 | [diff] [blame] | 616 | if (!bssmap_is_reset(oph->msg) && a_reset_conn_ready(a_conn_info.bsc->reset_fsm) == false) { |
Philipp Maier | ce1298b | 2018-02-22 16:44:41 +0100 | [diff] [blame] | 617 | LOGP(DBSSAP, LOGL_NOTICE, "Ignoring N-UNITDATA.ind(%s), BSC not reset yet\n", |
| 618 | msgb_hexdump_l2(oph->msg)); |
| 619 | break; |
| 620 | } |
| 621 | |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 622 | DEBUGP(DBSSAP, "N-UNITDATA.ind(%s)\n", msgb_hexdump_l2(oph->msg)); |
Neels Hofmeyr | c1d6925 | 2017-12-18 04:06:04 +0100 | [diff] [blame] | 623 | a_sccp_rx_udt(scu, &a_conn_info, oph->msg); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 624 | break; |
| 625 | |
| 626 | default: |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 627 | LOGP(DBSSAP, LOGL_ERROR, "Unhandled SIGTRAN operation %s on primitive %u\n", |
Max | c309fe3 | 2018-01-24 14:02:38 +0100 | [diff] [blame] | 628 | get_value_string(osmo_prim_op_names, oph->operation), oph->primitive); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 629 | break; |
| 630 | } |
| 631 | |
Harald Welte | a172e9e | 2018-02-09 21:33:24 +0100 | [diff] [blame] | 632 | /* We didn't transfer msgb ownership to any downstream functions so we rely on |
| 633 | * this single/central location to free() the msgb wrapping the primitive */ |
| 634 | msgb_free(oph->msg); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 635 | return rc; |
| 636 | } |
| 637 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 638 | /* Clear all RAN connections on a specified BSC */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 639 | void a_clear_all(struct osmo_sccp_user *scu, const struct osmo_sccp_addr *bsc_addr) |
| 640 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 641 | struct ran_conn *conn; |
| 642 | struct ran_conn *conn_temp; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 643 | struct gsm_network *network = gsm_network; |
| 644 | |
| 645 | OSMO_ASSERT(scu); |
| 646 | OSMO_ASSERT(bsc_addr); |
| 647 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 648 | llist_for_each_entry_safe(conn, conn_temp, &network->ran_conns, entry) { |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 649 | /* Clear only A connections and connections that actually |
| 650 | * belong to the specified BSC */ |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 651 | if (conn->via_ran == OSMO_RAT_GERAN_A && memcmp(bsc_addr, &conn->a.bsc_addr, sizeof(conn->a.bsc_addr)) == 0) { |
Harald Welte | 80620d2 | 2018-02-10 10:24:15 +0100 | [diff] [blame] | 652 | uint32_t conn_id = conn->a.conn_id; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 653 | LOGPCONN(conn, LOGL_NOTICE, "Dropping orphaned RAN connection\n"); |
Harald Welte | 80620d2 | 2018-02-10 10:24:15 +0100 | [diff] [blame] | 654 | /* This call will/may talloc_free(conn), so we must save conn_id above */ |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 655 | ran_conn_clear_request(conn, GSM48_CC_CAUSE_SWITCH_CONG); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 656 | |
| 657 | /* If there is still an SCCP connection active, remove it now */ |
Harald Welte | 80620d2 | 2018-02-10 10:24:15 +0100 | [diff] [blame] | 658 | if (check_connection_active(conn_id)) { |
| 659 | osmo_sccp_tx_disconn(scu, conn_id, bsc_addr, |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 660 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
Harald Welte | 80620d2 | 2018-02-10 10:24:15 +0100 | [diff] [blame] | 661 | a_delete_bsc_con(conn_id); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /* Initalize A interface connection between to MSC and BSC */ |
| 668 | int a_init(struct osmo_sccp_instance *sccp, struct gsm_network *network) |
| 669 | { |
| 670 | OSMO_ASSERT(sccp); |
| 671 | OSMO_ASSERT(network); |
| 672 | |
| 673 | /* FIXME: Remove hardcoded parameters, use parameters in parameter list */ |
Harald Welte | 1f47744 | 2018-02-09 01:49:01 +0100 | [diff] [blame] | 674 | LOGP(DBSSAP, LOGL_NOTICE, "Initalizing SCCP connection to stp...\n"); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 675 | |
| 676 | /* Set GSM network variable, there can only be |
| 677 | * one network by design */ |
| 678 | if (gsm_network != NULL) { |
| 679 | OSMO_ASSERT(gsm_network == network); |
| 680 | } else |
| 681 | gsm_network = network; |
| 682 | |
| 683 | /* SCCP Protocol stack */ |
| 684 | osmo_sccp_user_bind(sccp, "OsmoMSC-A", sccp_sap_up, SCCP_SSN_BSSAP); |
| 685 | |
| 686 | return 0; |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 687 | } |