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