Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 1 | /* (C) 2017 by sysmocom s.f.m.c. GmbH |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 2 | * All Rights Reserved |
| 3 | * |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 4 | * Author: Philipp Maier |
| 5 | * |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 21 | #include <osmocom/core/utils.h> |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 22 | #include <osmocom/core/msgb.h> |
| 23 | #include <osmocom/core/logging.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 24 | #include <osmocom/sigtran/sccp_helpers.h> |
| 25 | #include <osmocom/sigtran/sccp_sap.h> |
| 26 | #include <osmocom/sigtran/osmo_ss7.h> |
| 27 | #include <osmocom/sigtran/protocol/m3ua.h> |
| 28 | #include <osmocom/gsm/gsm0808.h> |
| 29 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 30 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 31 | #include <osmocom/gsm/gsm0808_utils.h> |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 32 | #include <openbsc/debug.h> |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 33 | #include <openbsc/msc_ifaces.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 34 | #include <openbsc/a_iface.h> |
| 35 | #include <openbsc/a_iface_bssap.h> |
| 36 | #include <openbsc/transaction.h> |
Neels Hofmeyr | 979cd26 | 2017-07-19 16:48:42 +0200 | [diff] [blame] | 37 | #include <osmocom/legacy_mgcp/mgcpgw_client.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 38 | #include <osmocom/core/byteswap.h> |
| 39 | #include <osmocom/sccp/sccp_types.h> |
| 40 | #include <openbsc/a_reset.h> |
| 41 | #include <openbsc/osmo_msc.h> |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 42 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 43 | /* A pointer to the GSM network we work with. By the current paradigm, |
| 44 | * there can only be one gsm_network per MSC. The pointer is set once |
| 45 | * when calling a_init() */ |
| 46 | static struct gsm_network *gsm_network = NULL; |
| 47 | |
| 48 | /* A struct to track currently active connections. We need that information |
| 49 | * to handle failure sitautions. In case of a problem, we must know which |
| 50 | * connections are currently open and which BSC is responsible. We also need |
| 51 | * the data to perform our connection checks (a_reset). All other logic will |
| 52 | * look at the connection ids and addresses that are supplied by the |
| 53 | * primitives */ |
| 54 | struct bsc_conn { |
| 55 | struct llist_head list; |
| 56 | uint32_t conn_id; /* Connection identifier */ |
| 57 | }; |
| 58 | |
| 59 | /* Internal list with connections we currently maintain. This |
| 60 | * list is of type struct bsc_conn (see above) */ |
| 61 | static LLIST_HEAD(active_connections); |
| 62 | |
| 63 | /* Record info of a new active connection in the active connection list */ |
| 64 | static void record_bsc_con(const void *ctx, uint32_t conn_id) |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 65 | { |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 66 | struct bsc_conn *conn; |
| 67 | |
| 68 | conn = talloc_zero(ctx, struct bsc_conn); |
| 69 | OSMO_ASSERT(conn); |
| 70 | |
| 71 | conn->conn_id = conn_id; |
| 72 | |
| 73 | llist_add_tail(&conn->list, &active_connections); |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 76 | /* Delete info of a closed connection from the active connection list */ |
| 77 | void a_delete_bsc_con(uint32_t conn_id) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 78 | { |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 79 | struct bsc_conn *conn; |
| 80 | struct bsc_conn *conn_temp; |
| 81 | |
| 82 | LOGP(DMSC, LOGL_DEBUG, |
| 83 | "Removing connection from active sccp-connection list (conn_id=%i)\n", |
| 84 | conn_id); |
| 85 | |
| 86 | llist_for_each_entry_safe(conn, conn_temp, &active_connections, list) { |
| 87 | if (conn->conn_id == conn_id) { |
| 88 | llist_del(&conn->list); |
| 89 | talloc_free(conn); |
| 90 | } |
| 91 | } |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 94 | /* Check if a specified connection id has an active SCCP connection */ |
| 95 | static bool check_connection_active(uint32_t conn_id) |
| 96 | { |
| 97 | struct bsc_conn *conn; |
| 98 | |
| 99 | /* Find the address for the current connection id */ |
| 100 | llist_for_each_entry(conn, &active_connections, list) { |
| 101 | if (conn->conn_id == conn_id) { |
| 102 | return true; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | /* Get the reset context for a specifiec calling (BSC) address */ |
| 110 | static struct a_reset_ctx *get_reset_ctx_by_sccp_addr(const struct osmo_sccp_addr *addr) |
| 111 | { |
| 112 | struct bsc_context *bsc_ctx; |
| 113 | struct osmo_ss7_instance *ss7; |
| 114 | |
| 115 | if (!addr) |
| 116 | return NULL; |
| 117 | |
| 118 | llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) { |
| 119 | if (memcmp(&bsc_ctx->bsc_addr, addr, sizeof(*addr)) == 0) |
| 120 | return bsc_ctx->reset; |
| 121 | } |
| 122 | |
| 123 | ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance); |
| 124 | OSMO_ASSERT(ss7); |
| 125 | LOGP(DMSC, LOGL_ERROR, "The calling BSC (%s) is unknown to this MSC ...\n", |
| 126 | osmo_sccp_addr_name(ss7, addr)); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | /* Send DTAP message via A-interface */ |
| 131 | int a_iface_tx_dtap(struct msgb *msg) |
| 132 | { |
| 133 | struct gsm_subscriber_connection *conn; |
| 134 | struct msgb *msg_resp; |
| 135 | |
| 136 | /* FIXME: Set this to some meaninful value! */ |
| 137 | uint8_t link_id = 0x00; |
| 138 | OSMO_ASSERT(msg); |
| 139 | conn = (struct gsm_subscriber_connection *)msg->dst; |
| 140 | OSMO_ASSERT(conn); |
| 141 | OSMO_ASSERT(conn->a.scu); |
| 142 | |
| 143 | LOGP(DMSC, LOGL_DEBUG, "Passing DTAP message from MSC to BSC (conn_id=%i)\n", conn->a.conn_id); |
| 144 | |
| 145 | msg->l3h = msg->data; |
| 146 | msg_resp = gsm0808_create_dtap(msg, link_id); |
| 147 | if (!msg_resp) { |
| 148 | LOGP(DMSC, LOGL_ERROR, "Unable to generate BSSMAP DTAP message!\n"); |
| 149 | return -EINVAL; |
| 150 | } else |
| 151 | LOGP(DMSC, LOGL_DEBUG, "Massage will be sent as BSSMAP DTAP message!\n"); |
| 152 | |
| 153 | LOGP(DMSC, LOGL_DEBUG, "N-DATA.req(%u, %s)\n", conn->a.conn_id, osmo_hexdump(msg_resp->data, msg_resp->len)); |
| 154 | return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp); |
| 155 | } |
| 156 | |
| 157 | /* Send Cipher mode command via A-interface */ |
| 158 | int a_iface_tx_cipher_mode(const struct gsm_subscriber_connection *conn, |
| 159 | int cipher, const const uint8_t *key, int len, int include_imeisv) |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 160 | { |
| 161 | /* 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] | 162 | struct msgb *msg_resp; |
| 163 | struct gsm0808_encrypt_info ei; |
| 164 | |
| 165 | OSMO_ASSERT(conn); |
| 166 | |
| 167 | LOGP(DMSC, LOGL_DEBUG, "Passing Cipher mode command message from MSC to BSC (conn_id=%i)\n", conn->a.conn_id); |
| 168 | uint8_t crm = 0x01; |
| 169 | uint8_t *crm_ptr = NULL; |
| 170 | |
| 171 | /* Setup encryption information */ |
| 172 | if (len > ENCRY_INFO_KEY_MAXLEN || !key) { |
| 173 | LOGP(DMSC, LOGL_ERROR, |
| 174 | "Cipher mode command message could not be generated due to invalid key! (conn_id=%i)\n", |
| 175 | conn->a.conn_id); |
| 176 | return -EINVAL; |
| 177 | } else { |
| 178 | memcpy(&ei.key, key, len); |
| 179 | ei.key_len = len; |
| 180 | } |
| 181 | |
| 182 | if (include_imeisv) |
| 183 | crm_ptr = &crm; |
| 184 | |
| 185 | ei.perm_algo[0] = (uint8_t) (1 << cipher); |
| 186 | ei.perm_algo_len = 1; |
| 187 | |
| 188 | msg_resp = gsm0808_create_cipher(&ei, crm_ptr); |
| 189 | LOGP(DMSC, LOGL_DEBUG, "N-DATA.req(%u, %s)\n", conn->a.conn_id, osmo_hexdump(msg_resp->data, msg_resp->len)); |
| 190 | |
| 191 | return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg_resp); |
| 192 | } |
| 193 | |
| 194 | /* Page a subscriber via A-interface */ |
| 195 | int a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac) |
| 196 | { |
| 197 | struct bsc_context *bsc_ctx; |
| 198 | struct gsm0808_cell_id_list cil; |
| 199 | struct msgb *msg; |
| 200 | int page_count = 0; |
| 201 | struct osmo_ss7_instance *ss7; |
| 202 | |
| 203 | OSMO_ASSERT(imsi); |
| 204 | |
| 205 | cil.id_discr = CELL_IDENT_LAC; |
| 206 | cil.id_list_lac[0] = lac; |
| 207 | cil.id_list_len = 1; |
| 208 | |
| 209 | ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance); |
| 210 | OSMO_ASSERT(ss7); |
| 211 | |
| 212 | /* Deliver paging request to all known BSCs */ |
| 213 | llist_for_each_entry(bsc_ctx, &gsm_network->a.bscs, list) { |
| 214 | if (a_reset_conn_ready(bsc_ctx->reset)) { |
| 215 | LOGP(DMSC, LOGL_DEBUG, |
| 216 | "Passing paging message from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n", |
| 217 | osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr), |
| 218 | osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac); |
| 219 | msg = gsm0808_create_paging(imsi, &tmsi, &cil, NULL); |
| 220 | osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user, |
| 221 | &bsc_ctx->msc_addr, &bsc_ctx->bsc_addr, msg); |
| 222 | page_count++; |
| 223 | } else { |
| 224 | LOGP(DMSC, LOGL_DEBUG, |
| 225 | "Connection down, dropping paging from MSC %s to BSC %s (imsi=%s, tmsi=0x%08x, lac=%u)\n", |
| 226 | osmo_sccp_addr_name(ss7, &bsc_ctx->msc_addr), |
| 227 | osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr), imsi, tmsi, lac); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if (page_count <= 0) |
| 232 | LOGP(DMSC, LOGL_ERROR, "Could not deliver paging because none of the associated BSCs is available!\n"); |
| 233 | |
| 234 | return page_count; |
| 235 | } |
| 236 | |
| 237 | /* Convert speech version field */ |
| 238 | static uint8_t convert_Abis_sv_to_A_sv(int speech_ver) |
| 239 | { |
| 240 | /* The speech versions that are transmitted in the Bearer capability |
| 241 | * information element, that is transmitted on the Abis interfece |
| 242 | * use a different encoding than the permitted speech version |
| 243 | * identifier, that is signalled in the channel type element on the A |
| 244 | * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008, |
| 245 | * 10.5.103 */ |
| 246 | |
| 247 | switch (speech_ver) { |
| 248 | case GSM48_BCAP_SV_FR: |
| 249 | return GSM0808_PERM_FR1; |
| 250 | break; |
| 251 | case GSM48_BCAP_SV_HR: |
| 252 | return GSM0808_PERM_HR1; |
| 253 | break; |
| 254 | case GSM48_BCAP_SV_EFR: |
| 255 | return GSM0808_PERM_FR2; |
| 256 | break; |
| 257 | case GSM48_BCAP_SV_AMR_F: |
| 258 | return GSM0808_PERM_FR3; |
| 259 | break; |
| 260 | case GSM48_BCAP_SV_AMR_H: |
| 261 | return GSM0808_PERM_HR3; |
| 262 | break; |
| 263 | case GSM48_BCAP_SV_AMR_OFW: |
| 264 | return GSM0808_PERM_FR4; |
| 265 | break; |
| 266 | case GSM48_BCAP_SV_AMR_OHW: |
| 267 | return GSM0808_PERM_HR4; |
| 268 | break; |
| 269 | case GSM48_BCAP_SV_AMR_FW: |
| 270 | return GSM0808_PERM_FR5; |
| 271 | break; |
| 272 | case GSM48_BCAP_SV_AMR_OH: |
| 273 | return GSM0808_PERM_HR6; |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | /* If nothing matches, tag the result as invalid */ |
| 278 | LOGP(DMSC, LOGL_ERROR, "Invalid permitted speech version / rate detected, discarding.\n"); |
| 279 | return 0xFF; |
| 280 | } |
| 281 | |
| 282 | /* Convert speech preference field */ |
| 283 | static uint8_t convert_Abis_prev_to_A_pref(int radio) |
| 284 | { |
| 285 | /* The Radio channel requirement field that is transmitted in the |
| 286 | * Bearer capability information element, that is transmitted on the |
| 287 | * Abis interfece uses a different encoding than the Channel rate and |
| 288 | * type field that is signalled in the channel type element on the A |
| 289 | * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008, |
| 290 | * 10.5.102 */ |
| 291 | |
| 292 | switch (radio) { |
| 293 | case GSM48_BCAP_RRQ_FR_ONLY: |
| 294 | return GSM0808_SPEECH_FULL_BM; |
| 295 | case GSM48_BCAP_RRQ_DUAL_FR: |
| 296 | return GSM0808_SPEECH_FULL_PREF; |
| 297 | case GSM48_BCAP_RRQ_DUAL_HR: |
| 298 | return GSM0808_SPEECH_HALF_PREF; |
| 299 | } |
| 300 | |
| 301 | LOGP(DMSC, LOGL_ERROR, "Invalid speech version / rate combination preference, defaulting to full rate.\n"); |
| 302 | return GSM0808_SPEECH_FULL_BM; |
| 303 | } |
| 304 | |
| 305 | /* Assemble the channel type field */ |
| 306 | static int enc_channel_type(struct gsm0808_channel_type *ct, const struct gsm_mncc_bearer_cap *bc) |
| 307 | { |
| 308 | unsigned int i; |
| 309 | uint8_t sv; |
| 310 | unsigned int count = 0; |
| 311 | bool only_gsm_hr = true; |
| 312 | |
| 313 | OSMO_ASSERT(ct); |
| 314 | OSMO_ASSERT(bc); |
| 315 | |
| 316 | ct->ch_indctr = GSM0808_CHAN_SPEECH; |
| 317 | |
| 318 | for (i = 0; i < ARRAY_SIZE(bc->speech_ver); i++) { |
| 319 | if (bc->speech_ver[i] == -1) |
| 320 | break; |
| 321 | sv = convert_Abis_sv_to_A_sv(bc->speech_ver[i]); |
| 322 | if (sv != 0xFF) { |
| 323 | /* Detect if something else than |
| 324 | * GSM HR V1 is supported */ |
| 325 | if (sv == GSM0808_PERM_HR2 || |
| 326 | sv == GSM0808_PERM_HR3 || sv == GSM0808_PERM_HR4 || sv == GSM0808_PERM_HR6) |
| 327 | only_gsm_hr = false; |
| 328 | |
| 329 | ct->perm_spch[count] = sv; |
| 330 | count++; |
| 331 | } |
| 332 | } |
| 333 | ct->perm_spch_len = count; |
| 334 | |
| 335 | if (only_gsm_hr) |
| 336 | /* Note: We must avoid the usage of GSM HR1 as this |
| 337 | * codec only offers very poor audio quality. If the |
| 338 | * MS only supports GSM HR1 (and full rate), and has |
| 339 | * a preference for half rate. Then we will ignore the |
| 340 | * preference and assume a preference for full rate. */ |
| 341 | ct->ch_rate_type = GSM0808_SPEECH_FULL_BM; |
| 342 | else |
| 343 | ct->ch_rate_type = convert_Abis_prev_to_A_pref(bc->radio); |
| 344 | |
| 345 | if (count) |
| 346 | return 0; |
| 347 | else |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | |
| 351 | /* Assemble the speech codec field */ |
| 352 | static int enc_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct) |
| 353 | { |
| 354 | unsigned int i; |
| 355 | int rc; |
| 356 | |
| 357 | memset(scl, 0, sizeof(*scl)); |
| 358 | for (i = 0; i < ct->perm_spch_len; i++) { |
| 359 | rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]); |
| 360 | if (rc != 0) |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | scl->len = i; |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /* Send assignment request via A-interface */ |
| 369 | int a_iface_tx_assignment(const struct gsm_trans *trans) |
| 370 | { |
| 371 | struct gsm_subscriber_connection *conn; |
| 372 | struct gsm0808_channel_type ct; |
| 373 | struct gsm0808_speech_codec_list scl; |
| 374 | uint32_t *ci_ptr = NULL; |
| 375 | struct msgb *msg; |
| 376 | struct sockaddr_storage rtp_addr; |
| 377 | struct sockaddr_in rtp_addr_in; |
| 378 | int rc; |
| 379 | |
| 380 | OSMO_ASSERT(trans); |
| 381 | conn = trans->conn; |
| 382 | OSMO_ASSERT(conn); |
| 383 | |
| 384 | LOGP(DMSC, LOGL_ERROR, "Sending assignment command to BSC (conn_id %u)\n", conn->a.conn_id); |
| 385 | |
| 386 | /* Channel type */ |
| 387 | rc = enc_channel_type(&ct, &trans->bearer_cap); |
| 388 | if (rc < 0) { |
| 389 | LOGP(DMSC, LOGL_ERROR, "Faild to generate channel type -- assignment not sent!\n"); |
| 390 | return -EINVAL; |
| 391 | } |
| 392 | |
| 393 | /* Speech codec list */ |
| 394 | rc = enc_speech_codec_list(&scl, &ct); |
| 395 | if (rc < 0) { |
| 396 | LOGP(DMSC, LOGL_ERROR, "Faild to generate Speech codec list -- assignment not sent!\n"); |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | /* Package RTP-Address data */ |
| 401 | memset(&rtp_addr_in, 0, sizeof(rtp_addr_in)); |
| 402 | rtp_addr_in.sin_family = AF_INET; |
| 403 | rtp_addr_in.sin_port = osmo_htons(conn->rtp.port_subscr); |
| 404 | rtp_addr_in.sin_addr.s_addr = osmo_htonl(mgcpgw_client_remote_addr_n(gsm_network->mgcpgw.client)); |
| 405 | |
| 406 | memset(&rtp_addr, 0, sizeof(rtp_addr)); |
| 407 | memcpy(&rtp_addr, &rtp_addr_in, sizeof(rtp_addr_in)); |
| 408 | |
| 409 | msg = gsm0808_create_ass(&ct, NULL, &rtp_addr, &scl, ci_ptr); |
| 410 | |
| 411 | LOGP(DMSC, LOGL_DEBUG, "N-DATA.req(%u, %s)\n", conn->a.conn_id, osmo_hexdump(msg->data, msg->len)); |
| 412 | return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg); |
| 413 | } |
| 414 | |
| 415 | /* Send clear command via A-interface */ |
| 416 | int a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn) |
| 417 | { |
| 418 | struct msgb *msg; |
| 419 | |
| 420 | LOGP(DMSC, LOGL_NOTICE, "Sending clear command to BSC (conn_id=%u)\n", conn->a.conn_id); |
| 421 | |
| 422 | msg = gsm0808_create_clear_command(GSM0808_CAUSE_CALL_CONTROL); |
| 423 | return osmo_sccp_tx_data_msg(conn->a.scu, conn->a.conn_id, msg); |
| 424 | } |
| 425 | |
| 426 | /* Callback function: Close all open connections */ |
| 427 | static void a_reset_cb(const void *priv) |
| 428 | { |
| 429 | struct msgb *msg; |
| 430 | struct bsc_context *bsc_ctx = (struct bsc_context*) priv; |
| 431 | struct osmo_ss7_instance *ss7; |
| 432 | |
| 433 | /* Skip if the A interface is not properly initalized yet */ |
| 434 | if (!gsm_network) |
| 435 | return; |
| 436 | |
| 437 | /* Clear all now orphaned subscriber connections */ |
| 438 | a_clear_all(bsc_ctx->sccp_user, &bsc_ctx->bsc_addr); |
| 439 | |
| 440 | /* Send reset to the remote BSC */ |
| 441 | ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance); |
| 442 | OSMO_ASSERT(ss7); |
| 443 | LOGP(DMSC, LOGL_NOTICE, "Sending RESET to BSC %s\n", osmo_sccp_addr_name(ss7, &bsc_ctx->bsc_addr)); |
| 444 | msg = gsm0808_create_reset(); |
| 445 | osmo_sccp_tx_unitdata_msg(bsc_ctx->sccp_user, &bsc_ctx->msc_addr, |
| 446 | &bsc_ctx->bsc_addr, msg); |
| 447 | } |
| 448 | |
| 449 | /* Add a new BSC connection to our internal list with known BSCs */ |
| 450 | static void add_bsc(const struct osmo_sccp_addr *msc_addr, const struct osmo_sccp_addr *bsc_addr, |
| 451 | struct osmo_sccp_user *scu) |
| 452 | { |
| 453 | struct bsc_context *bsc_ctx; |
| 454 | struct osmo_ss7_instance *ss7; |
| 455 | |
| 456 | OSMO_ASSERT(bsc_addr); |
| 457 | OSMO_ASSERT(msc_addr); |
| 458 | OSMO_ASSERT(scu); |
| 459 | |
| 460 | /* Check if we already know this BSC, if yes, skip adding it. */ |
| 461 | if (get_reset_ctx_by_sccp_addr(bsc_addr)) |
| 462 | return; |
| 463 | |
| 464 | ss7 = osmo_ss7_instance_find(gsm_network->a.cs7_instance); |
| 465 | OSMO_ASSERT(ss7); |
| 466 | LOGP(DMSC, LOGL_NOTICE, "Adding new BSC connection for BSC %s...\n", osmo_sccp_addr_name(ss7, bsc_addr)); |
| 467 | |
| 468 | /* Generate and fill up a new bsc context */ |
| 469 | bsc_ctx = talloc_zero(gsm_network, struct bsc_context); |
| 470 | OSMO_ASSERT(bsc_ctx); |
| 471 | memcpy(&bsc_ctx->bsc_addr, bsc_addr, sizeof(*bsc_addr)); |
| 472 | memcpy(&bsc_ctx->msc_addr, msc_addr, sizeof(*msc_addr)); |
| 473 | bsc_ctx->sccp_user = scu; |
| 474 | llist_add_tail(&bsc_ctx->list, &gsm_network->a.bscs); |
| 475 | |
| 476 | /* Start reset procedure to make the new connection active */ |
| 477 | bsc_ctx->reset = a_reset_alloc(bsc_ctx, osmo_sccp_addr_name(ss7, bsc_addr), a_reset_cb, bsc_ctx); |
| 478 | } |
| 479 | |
| 480 | /* Callback function, called by the SSCP stack when data arrives */ |
| 481 | static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu) |
| 482 | { |
| 483 | struct osmo_sccp_user *scu = _scu; |
| 484 | struct osmo_scu_prim *scu_prim = (struct osmo_scu_prim *)oph; |
| 485 | int rc = 0; |
| 486 | struct a_conn_info a_conn_info; |
| 487 | memset(&a_conn_info, 0, sizeof(a_conn_info)); |
| 488 | a_conn_info.network = gsm_network; |
| 489 | a_conn_info.reset = NULL; |
| 490 | |
| 491 | switch (OSMO_PRIM_HDR(&scu_prim->oph)) { |
| 492 | case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION): |
| 493 | /* Handle inbound connection indication */ |
| 494 | add_bsc(&scu_prim->u.connect.called_addr, &scu_prim->u.connect.calling_addr, scu); |
| 495 | a_conn_info.conn_id = scu_prim->u.connect.conn_id; |
| 496 | a_conn_info.msc_addr = &scu_prim->u.connect.called_addr; |
| 497 | a_conn_info.bsc_addr = &scu_prim->u.connect.calling_addr; |
| 498 | a_conn_info.reset = get_reset_ctx_by_sccp_addr(&scu_prim->u.unitdata.calling_addr); |
| 499 | |
| 500 | if (a_reset_conn_ready(a_conn_info.reset) == false) { |
| 501 | rc = osmo_sccp_tx_disconn(scu, a_conn_info.conn_id, a_conn_info.msc_addr, |
| 502 | SCCP_RETURN_CAUSE_UNQUALIFIED); |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | osmo_sccp_tx_conn_resp(scu, scu_prim->u.connect.conn_id, &scu_prim->u.connect.called_addr, NULL, 0); |
| 507 | if (msgb_l2len(oph->msg) > 0) { |
| 508 | LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u, %s)\n", |
| 509 | scu_prim->u.connect.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); |
| 510 | rc = sccp_rx_dt(scu, &a_conn_info, oph->msg); |
| 511 | } else |
| 512 | LOGP(DMSC, LOGL_DEBUG, "N-CONNECT.ind(%u)\n", scu_prim->u.connect.conn_id); |
| 513 | |
| 514 | record_bsc_con(scu, scu_prim->u.connect.conn_id); |
| 515 | break; |
| 516 | |
| 517 | case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION): |
| 518 | /* Handle incoming connection oriented data */ |
| 519 | a_conn_info.conn_id = scu_prim->u.data.conn_id; |
| 520 | LOGP(DMSC, LOGL_DEBUG, "N-DATA.ind(%u, %s)\n", |
| 521 | scu_prim->u.data.conn_id, osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); |
| 522 | sccp_rx_dt(scu, &a_conn_info, oph->msg); |
| 523 | break; |
| 524 | |
| 525 | case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION): |
| 526 | /* Handle inbound UNITDATA */ |
| 527 | add_bsc(&scu_prim->u.unitdata.called_addr, &scu_prim->u.unitdata.calling_addr, scu); |
| 528 | a_conn_info.msc_addr = &scu_prim->u.unitdata.called_addr; |
| 529 | a_conn_info.bsc_addr = &scu_prim->u.unitdata.calling_addr; |
| 530 | a_conn_info.reset = get_reset_ctx_by_sccp_addr(&scu_prim->u.unitdata.calling_addr); |
| 531 | DEBUGP(DMSC, "N-UNITDATA.ind(%s)\n", osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg))); |
| 532 | sccp_rx_udt(scu, &a_conn_info, oph->msg); |
| 533 | break; |
| 534 | |
| 535 | default: |
| 536 | LOGP(DMSC, LOGL_ERROR, "Unhandled SIGTRAN primitive: %u:%u\n", oph->primitive, oph->operation); |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | return rc; |
| 541 | } |
| 542 | |
| 543 | /* Clear all subscriber connections on a specified BSC */ |
| 544 | void a_clear_all(struct osmo_sccp_user *scu, const struct osmo_sccp_addr *bsc_addr) |
| 545 | { |
| 546 | struct gsm_subscriber_connection *conn; |
| 547 | struct gsm_subscriber_connection *conn_temp; |
| 548 | struct gsm_network *network = gsm_network; |
| 549 | |
| 550 | OSMO_ASSERT(scu); |
| 551 | OSMO_ASSERT(bsc_addr); |
| 552 | |
| 553 | llist_for_each_entry_safe(conn, conn_temp, &network->subscr_conns, entry) { |
| 554 | /* Clear only A connections and connections that actually |
| 555 | * belong to the specified BSC */ |
| 556 | if (conn->via_ran == RAN_GERAN_A && memcmp(bsc_addr, &conn->a.bsc_addr, sizeof(conn->a.bsc_addr)) == 0) { |
| 557 | LOGP(DMSC, LOGL_NOTICE, "Dropping orphaned subscriber connection (conn_id %i)\n", |
| 558 | conn->a.conn_id); |
| 559 | msc_clear_request(conn, GSM48_CC_CAUSE_SWITCH_CONG); |
| 560 | |
| 561 | /* If there is still an SCCP connection active, remove it now */ |
| 562 | if (check_connection_active(conn->a.conn_id)) { |
| 563 | osmo_sccp_tx_disconn(scu, conn->a.conn_id, bsc_addr, |
| 564 | SCCP_RELEASE_CAUSE_END_USER_ORIGINATED); |
| 565 | a_delete_bsc_con(conn->a.conn_id); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* Initalize A interface connection between to MSC and BSC */ |
| 572 | int a_init(struct osmo_sccp_instance *sccp, struct gsm_network *network) |
| 573 | { |
| 574 | OSMO_ASSERT(sccp); |
| 575 | OSMO_ASSERT(network); |
| 576 | |
| 577 | /* FIXME: Remove hardcoded parameters, use parameters in parameter list */ |
| 578 | LOGP(DMSC, LOGL_NOTICE, "Initalizing SCCP connection to stp...\n"); |
| 579 | |
| 580 | /* Set GSM network variable, there can only be |
| 581 | * one network by design */ |
| 582 | if (gsm_network != NULL) { |
| 583 | OSMO_ASSERT(gsm_network == network); |
| 584 | } else |
| 585 | gsm_network = network; |
| 586 | |
| 587 | /* SCCP Protocol stack */ |
| 588 | osmo_sccp_user_bind(sccp, "OsmoMSC-A", sccp_sap_up, SCCP_SSN_BSSAP); |
| 589 | |
| 590 | return 0; |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 591 | } |