Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1 | /* Code to manage a subscriber's MSC-A role */ |
| 2 | /* |
Vadim Yanitskiy | 999a593 | 2023-05-18 17:22:26 +0700 | [diff] [blame] | 3 | * (C) 2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * SPDX-License-Identifier: AGPL-3.0+ |
| 7 | * |
| 8 | * Author: Neels Hofmeyr |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Affero General Public License as published by |
| 12 | * the Free Software Foundation; either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Affero General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Affero General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #include <osmocom/core/utils.h> |
| 25 | #include <osmocom/core/tdef.h> |
| 26 | #include <osmocom/core/rate_ctr.h> |
| 27 | #include <osmocom/core/signal.h> |
| 28 | |
| 29 | #include <osmocom/msc/gsm_data.h> |
| 30 | #include <osmocom/msc/msc_roles.h> |
| 31 | #include <osmocom/msc/msub.h> |
| 32 | #include <osmocom/msc/msc_a.h> |
| 33 | #include <osmocom/msc/msc_t.h> |
| 34 | #include <osmocom/msc/msc_i.h> |
| 35 | #include <osmocom/msc/paging.h> |
| 36 | #include <osmocom/msc/signal.h> |
Alexander Couzens | eff28ab | 2024-09-12 00:55:25 +0200 | [diff] [blame^] | 37 | #include <osmocom/vlr/vlr.h> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 38 | #include <osmocom/msc/transaction.h> |
Oliver Smith | ceca8e6 | 2023-05-24 11:15:52 +0200 | [diff] [blame] | 39 | #include <osmocom/msc/transaction_cc.h> |
Alexander Couzens | 7900a05 | 2024-08-27 16:17:39 +0200 | [diff] [blame] | 40 | #include <osmocom/msc/ran_conn.h> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 41 | #include <osmocom/msc/ran_peer.h> |
| 42 | #include <osmocom/msc/ran_msg_a.h> |
| 43 | #include <osmocom/msc/ran_msg_iu.h> |
| 44 | #include <osmocom/msc/sgs_iface.h> |
| 45 | #include <osmocom/msc/gsm_04_08.h> |
| 46 | #include <osmocom/msc/gsm_09_11.h> |
| 47 | #include <osmocom/msc/gsm_04_14.h> |
| 48 | #include <osmocom/msc/call_leg.h> |
| 49 | #include <osmocom/msc/rtp_stream.h> |
| 50 | #include <osmocom/msc/msc_ho.h> |
Neels Hofmeyr | e276ae9 | 2022-01-13 21:38:35 +0100 | [diff] [blame] | 51 | #include <osmocom/msc/codec_mapping.h> |
Andreas Eversberg | f7c6f14 | 2023-04-23 12:10:42 +0200 | [diff] [blame] | 52 | #include <osmocom/msc/msc_vgcs.h> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 53 | |
| 54 | #define MSC_A_USE_WAIT_CLEAR_COMPLETE "wait-Clear-Complete" |
| 55 | |
| 56 | static struct osmo_fsm msc_a_fsm; |
| 57 | |
| 58 | static const struct osmo_tdef_state_timeout msc_a_fsm_timeouts[32] = { |
| 59 | [MSC_A_ST_VALIDATE_L3] = { .T = -1 }, |
| 60 | [MSC_A_ST_AUTH_CIPH] = { .keep_timer = true }, |
| 61 | [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = { .keep_timer = true }, |
| 62 | [MSC_A_ST_AUTHENTICATED] = { .keep_timer = true }, |
| 63 | [MSC_A_ST_RELEASING] = { .T = -2 }, |
| 64 | [MSC_A_ST_RELEASED] = { .T = -2 }, |
| 65 | }; |
| 66 | |
| 67 | /* Transition to a state, using the T timer defined in msc_a_fsm_timeouts. |
| 68 | * The actual timeout value is in turn obtained from network->T_defs. |
| 69 | * Assumes local variable fi exists. */ |
Neels Hofmeyr | 0165325 | 2019-09-03 02:06:22 +0200 | [diff] [blame] | 70 | #define msc_a_state_chg_always(msc_a, state) \ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 71 | osmo_tdef_fsm_inst_state_chg((msc_a)->c.fi, state, msc_a_fsm_timeouts, (msc_a)->c.ran->tdefs, 5) |
| 72 | |
Neels Hofmeyr | 0165325 | 2019-09-03 02:06:22 +0200 | [diff] [blame] | 73 | /* Same as msc_a_state_chg_always() but ignore if the msc_a already is in the target state. */ |
| 74 | #define msc_a_state_chg(msc_a, STATE) do { \ |
| 75 | if ((msc_a)->c.fi->state != STATE) \ |
| 76 | msc_a_state_chg_always(msc_a, STATE); \ |
| 77 | } while(0) |
| 78 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 79 | struct gsm_network *msc_a_net(const struct msc_a *msc_a) |
| 80 | { |
| 81 | return msub_net(msc_a->c.msub); |
| 82 | } |
| 83 | |
| 84 | struct vlr_subscr *msc_a_vsub(const struct msc_a *msc_a) |
| 85 | { |
Neels Hofmeyr | 911e597 | 2019-05-09 13:28:26 +0200 | [diff] [blame] | 86 | if (!msc_a) |
| 87 | return NULL; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 88 | return msub_vsub(msc_a->c.msub); |
| 89 | } |
| 90 | |
| 91 | struct msc_i *msc_a_msc_i(const struct msc_a *msc_a) |
| 92 | { |
Neels Hofmeyr | 911e597 | 2019-05-09 13:28:26 +0200 | [diff] [blame] | 93 | if (!msc_a) |
| 94 | return NULL; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 95 | return msub_msc_i(msc_a->c.msub); |
| 96 | } |
| 97 | |
| 98 | struct msc_t *msc_a_msc_t(const struct msc_a *msc_a) |
| 99 | { |
Neels Hofmeyr | 911e597 | 2019-05-09 13:28:26 +0200 | [diff] [blame] | 100 | if (!msc_a) |
| 101 | return NULL; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 102 | return msub_msc_t(msc_a->c.msub); |
| 103 | } |
| 104 | |
| 105 | struct msc_a *msc_a_fi_priv(struct osmo_fsm_inst *fi) |
| 106 | { |
| 107 | OSMO_ASSERT(fi); |
| 108 | OSMO_ASSERT(fi->fsm == &msc_a_fsm); |
| 109 | OSMO_ASSERT(fi->priv); |
| 110 | return fi->priv; |
| 111 | } |
| 112 | |
Neels Hofmeyr | d99a607 | 2022-10-10 23:34:48 +0200 | [diff] [blame] | 113 | bool msc_a_is_ciphering_to_be_attempted(const struct msc_a *msc_a) |
Alexander Couzens | 2c5e461 | 2021-11-05 02:00:17 +0100 | [diff] [blame] | 114 | { |
| 115 | struct gsm_network *net = msc_a_net(msc_a); |
| 116 | bool is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU); |
| 117 | if (is_utran) |
| 118 | return net->uea_encryption_mask > (1 << OSMO_UTRAN_UEA0); |
| 119 | else |
| 120 | return net->a5_encryption_mask > 0x1; |
| 121 | } |
| 122 | |
Neels Hofmeyr | 2ea7264 | 2022-10-10 23:35:47 +0200 | [diff] [blame] | 123 | bool msc_a_is_ciphering_required(const struct msc_a *msc_a) |
| 124 | { |
| 125 | struct gsm_network *net = msc_a_net(msc_a); |
| 126 | bool is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU); |
| 127 | if (is_utran) |
| 128 | return net->uea_encryption_mask |
| 129 | && ((net->uea_encryption_mask & (1 << OSMO_UTRAN_UEA0)) == 0); |
| 130 | else |
| 131 | return net->a5_encryption_mask |
| 132 | && ((net->a5_encryption_mask & 0x1) == 0); |
| 133 | } |
| 134 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 135 | static void update_counters(struct osmo_fsm_inst *fi, bool conn_accepted) |
| 136 | { |
| 137 | struct msc_a *msc_a = fi->priv; |
| 138 | struct gsm_network *net = msc_a_net(msc_a); |
| 139 | switch (msc_a->complete_layer3_type) { |
| 140 | case COMPLETE_LAYER3_LU: |
Pau Espin Pedrol | 2e21a68 | 2021-06-04 16:45:44 +0200 | [diff] [blame] | 141 | rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_LOC_UPDATE_COMPLETED : MSC_CTR_LOC_UPDATE_FAILED)); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 142 | break; |
| 143 | case COMPLETE_LAYER3_CM_SERVICE_REQ: |
Pau Espin Pedrol | 2e21a68 | 2021-06-04 16:45:44 +0200 | [diff] [blame] | 144 | rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_CM_SERVICE_REQUEST_ACCEPTED : MSC_CTR_CM_SERVICE_REQUEST_REJECTED)); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 145 | break; |
| 146 | case COMPLETE_LAYER3_PAGING_RESP: |
Pau Espin Pedrol | 2e21a68 | 2021-06-04 16:45:44 +0200 | [diff] [blame] | 147 | rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_PAGING_RESP_ACCEPTED : MSC_CTR_PAGING_RESP_REJECTED)); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 148 | break; |
Neels Hofmeyr | ae98b97 | 2021-07-27 03:46:49 +0200 | [diff] [blame] | 149 | case COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ: |
| 150 | rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, |
| 151 | conn_accepted ? MSC_CTR_CM_RE_ESTABLISH_REQ_ACCEPTED |
| 152 | : MSC_CTR_CM_RE_ESTABLISH_REQ_REJECTED)); |
| 153 | break; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 154 | default: |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
Vadim Yanitskiy | 3e66ec5 | 2024-05-08 00:17:43 +0200 | [diff] [blame] | 159 | static void lu_delay_timer_cb(void *data) |
| 160 | { |
| 161 | struct msc_a *msc_a = (struct msc_a *)data; |
| 162 | msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING); |
| 163 | } |
| 164 | |
| 165 | static void msc_a_put_lu_deferred(struct msc_a *msc_a) |
| 166 | { |
| 167 | unsigned long Tval; |
| 168 | |
| 169 | /* The idea behind timer X36 is to allow re-using the radio channel that was used for |
| 170 | * Location Updating to deliver MT SMS over GSUP. This is achieved by delaying |
| 171 | * release of a BSSAP/RANAP connection and thus delaying the release of the radio |
| 172 | * channel. The delay can be configured separately for GERAN and UTRAN. */ |
| 173 | switch (msc_a->c.ran->type) { |
| 174 | case OSMO_RAT_GERAN_A: |
| 175 | Tval = osmo_tdef_get(msc_tdefs_geran, -36, OSMO_TDEF_MS, 0); |
| 176 | break; |
| 177 | case OSMO_RAT_UTRAN_IU: |
| 178 | Tval = osmo_tdef_get(msc_tdefs_utran, -36, OSMO_TDEF_MS, 0); |
| 179 | break; |
| 180 | default: |
| 181 | Tval = 0; |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | if (Tval == 0) { |
| 186 | /* no delay, put LU token immediately */ |
| 187 | msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | LOG_MSC_A(msc_a, LOGL_INFO, "Keeping LU token for +%lu ms\n", Tval); |
| 192 | osmo_timer_schedule(&msc_a->lu_delay_timer, |
| 193 | Tval / 1000, /* seconds */ |
| 194 | Tval % 1000 * 1000); /* microseconds */ |
| 195 | } |
| 196 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 197 | static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted) |
| 198 | { |
| 199 | struct msc_a *msc_a = fi->priv; |
| 200 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
| 201 | |
| 202 | update_counters(fi, conn_accepted); |
| 203 | |
Neels Hofmeyr | 83cf10c | 2020-06-24 14:23:26 +0200 | [diff] [blame] | 204 | if (conn_accepted) { |
| 205 | /* Record the Cell ID seen in Complete Layer 3 Information in the VLR, so that it also shows in vty |
| 206 | * 'show' output. */ |
| 207 | vsub->cgi = msc_a->via_cell; |
| 208 | } |
| 209 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 210 | /* Trigger transactions that we paged for */ |
| 211 | if (msc_a->complete_layer3_type == COMPLETE_LAYER3_PAGING_RESP) { |
| 212 | if (conn_accepted) |
| 213 | paging_response(msc_a); |
| 214 | else |
| 215 | paging_expired(vsub); |
| 216 | } |
| 217 | |
| 218 | if (conn_accepted) |
| 219 | osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, msc_a_vsub(msc_a)); |
| 220 | |
| 221 | if (msc_a->complete_layer3_type == COMPLETE_LAYER3_LU) |
Vadim Yanitskiy | 3e66ec5 | 2024-05-08 00:17:43 +0200 | [diff] [blame] | 222 | msc_a_put_lu_deferred(msc_a); |
Neels Hofmeyr | ae98b97 | 2021-07-27 03:46:49 +0200 | [diff] [blame] | 223 | |
Neels Hofmeyr | c19f8fe | 2024-04-03 20:50:36 +0200 | [diff] [blame] | 224 | if (conn_accepted && msc_a->complete_layer3_type == COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ) { |
Neels Hofmeyr | ae98b97 | 2021-07-27 03:46:49 +0200 | [diff] [blame] | 225 | /* Trigger new Assignment to recommence the voice call. A little dance here because normally we verify |
| 226 | * that no CC trans is already active. */ |
| 227 | struct gsm_trans *cc_trans = msc_a->cc.active_trans; |
| 228 | msc_a->cc.active_trans = NULL; |
| 229 | osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, cc_trans); |
| 230 | msc_a_try_call_assignment(cc_trans); |
| 231 | } |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | bool msc_a_is_accepted(const struct msc_a *msc_a) |
| 235 | { |
| 236 | if (!msc_a || !msc_a->c.fi) |
| 237 | return false; |
| 238 | return msc_a->c.fi->state == MSC_A_ST_AUTHENTICATED |
| 239 | || msc_a->c.fi->state == MSC_A_ST_COMMUNICATING; |
| 240 | } |
| 241 | |
| 242 | bool msc_a_in_release(struct msc_a *msc_a) |
| 243 | { |
| 244 | if (!msc_a) |
| 245 | return true; |
| 246 | if (msc_a->c.fi->state == MSC_A_ST_RELEASING) |
| 247 | return true; |
| 248 | if (msc_a->c.fi->state == MSC_A_ST_RELEASED) |
| 249 | return true; |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | static int msc_a_ran_dec(struct msc_a *msc_a, const struct an_apdu *an_apdu, enum msc_role from_role) |
| 254 | { |
| 255 | int rc; |
| 256 | struct msc_a_ran_dec_data d = { |
| 257 | .from_role = from_role, |
| 258 | .an_apdu = an_apdu, |
| 259 | }; |
| 260 | msc_a_get(msc_a, __func__); |
| 261 | rc = msc_role_ran_decode(msc_a->c.fi, an_apdu, msc_a_ran_decode_cb, &d); |
| 262 | msc_a_put(msc_a, __func__); |
| 263 | return rc; |
| 264 | }; |
| 265 | |
| 266 | static void msc_a_fsm_validate_l3(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 267 | { |
| 268 | struct msc_a *msc_a = fi->priv; |
| 269 | const struct an_apdu *an_apdu; |
| 270 | |
| 271 | switch (event) { |
| 272 | case MSC_A_EV_FROM_I_COMPLETE_LAYER_3: |
| 273 | case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST: |
| 274 | case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST: |
| 275 | an_apdu = data; |
| 276 | msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I); |
| 277 | return; |
| 278 | |
| 279 | case MSC_A_EV_COMPLETE_LAYER_3_OK: |
| 280 | msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH); |
| 281 | return; |
| 282 | |
| 283 | case MSC_A_EV_MO_CLOSE: |
| 284 | case MSC_A_EV_CN_CLOSE: |
| 285 | evaluate_acceptance_outcome(fi, false); |
| 286 | /* fall through */ |
| 287 | case MSC_A_EV_UNUSED: |
| 288 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASING); |
| 289 | return; |
| 290 | |
| 291 | default: |
| 292 | OSMO_ASSERT(false); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* Figure out whether to first send a Classmark Request to the MS to figure out algorithm support. */ |
| 297 | static bool msc_a_need_classmark_for_ciphering(struct msc_a *msc_a) |
| 298 | { |
| 299 | struct gsm_network *net = msc_a_net(msc_a); |
| 300 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
| 301 | int i = 0; |
| 302 | bool request_classmark = false; |
| 303 | |
| 304 | /* Only on GERAN-A do we ever need Classmark Information for Ciphering. */ |
| 305 | if (msc_a->c.ran->type != OSMO_RAT_GERAN_A) |
| 306 | return false; |
| 307 | |
| 308 | for (i = 0; i < 8; i++) { |
| 309 | int supported; |
| 310 | |
| 311 | /* A5/n permitted by osmo-msc.cfg? */ |
| 312 | if (!(net->a5_encryption_mask & (1 << i))) |
| 313 | continue; |
| 314 | |
| 315 | /* A5/n supported by MS? */ |
| 316 | supported = osmo_gsm48_classmark_supports_a5(&vsub->classmark, i); |
| 317 | if (supported < 0) { |
| 318 | LOG_MSC_A(msc_a, LOGL_DEBUG, "For A5/%d, we still need Classmark %d\n", i, -supported); |
| 319 | request_classmark = true; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return request_classmark; |
| 324 | } |
| 325 | |
| 326 | static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv); |
| 327 | |
| 328 | /* VLR callback for ops.set_ciph_mode() */ |
| 329 | int msc_a_vlr_set_cipher_mode(void *_msc_a, bool umts_aka, bool retrieve_imeisv) |
| 330 | { |
| 331 | struct msc_a *msc_a = _msc_a; |
Vadim Yanitskiy | 4dd477f | 2019-05-11 03:00:30 +0700 | [diff] [blame] | 332 | struct vlr_subscr *vsub; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 333 | |
Vadim Yanitskiy | 4dd477f | 2019-05-11 03:00:30 +0700 | [diff] [blame] | 334 | if (!msc_a) { |
| 335 | LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: " |
| 336 | "MSC-A role is NULL?!?\n"); |
| 337 | return -EINVAL; |
| 338 | } |
| 339 | |
| 340 | vsub = msc_a_vsub(msc_a); |
| 341 | if (!vsub || !vsub->last_tuple) { |
| 342 | LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: " |
| 343 | "vlr_subscr is NULL?!?\n"); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 344 | return -EINVAL; |
| 345 | } |
| 346 | |
| 347 | if (msc_a_need_classmark_for_ciphering(msc_a)) { |
| 348 | int rc; |
| 349 | struct ran_msg msg = { |
| 350 | .msg_type = RAN_MSG_CLASSMARK_REQUEST, |
| 351 | }; |
| 352 | rc = msc_a_ran_down(msc_a, MSC_ROLE_I, &msg); |
| 353 | if (rc) { |
| 354 | LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Classmark Request\n"); |
| 355 | return -EIO; |
| 356 | } |
| 357 | |
| 358 | msc_a->state_before_classmark_update = msc_a->c.fi->state; |
| 359 | msc_a->action_on_classmark_update = (struct msc_a_action_on_classmark_update){ |
| 360 | .type = MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING, |
| 361 | .ciphering = { |
| 362 | .umts_aka = umts_aka, |
| 363 | .retrieve_imeisv = retrieve_imeisv, |
| 364 | }, |
| 365 | }; |
| 366 | msc_a_state_chg(msc_a, MSC_A_ST_WAIT_CLASSMARK_UPDATE); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | return msc_a_ran_enc_ciphering(msc_a, umts_aka, retrieve_imeisv); |
| 371 | } |
| 372 | |
Neels Hofmeyr | 6ce2edc | 2021-06-09 22:26:11 +0200 | [diff] [blame] | 373 | static uint8_t filter_a5(uint8_t a5_mask, bool umts_aka) |
| 374 | { |
| 375 | /* With GSM AKA: allow A5/0, 1, 3 = 0b00001011 = 0xb. |
| 376 | * UMTS aka: allow A5/0, 1, 3, 4 = 0b00011011 = 0x1b. |
| 377 | */ |
| 378 | return a5_mask & (umts_aka ? 0x1b : 0x0b); |
| 379 | } |
| 380 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 381 | static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv) |
| 382 | { |
Vadim Yanitskiy | 4dd477f | 2019-05-11 03:00:30 +0700 | [diff] [blame] | 383 | struct gsm_network *net; |
| 384 | struct vlr_subscr *vsub; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 385 | struct ran_msg msg; |
| 386 | |
Vadim Yanitskiy | 4dd477f | 2019-05-11 03:00:30 +0700 | [diff] [blame] | 387 | if (!msc_a) { |
| 388 | LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: " |
| 389 | "MSC-A role is NULL?!?\n"); |
| 390 | return -EINVAL; |
| 391 | } |
| 392 | |
| 393 | net = msc_a_net(msc_a); |
| 394 | vsub = msc_a_vsub(msc_a); |
| 395 | |
| 396 | if (!net || !vsub || !vsub->last_tuple) { |
| 397 | LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: " |
| 398 | "gsm_network and/or vlr_subscr is NULL?!?\n"); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 399 | return -EINVAL; |
| 400 | } |
| 401 | |
| 402 | msg = (struct ran_msg){ |
| 403 | .msg_type = RAN_MSG_CIPHER_MODE_COMMAND, |
| 404 | .cipher_mode_command = { |
| 405 | .vec = vsub->last_tuple ? &vsub->last_tuple->vec : NULL, |
| 406 | .classmark = &vsub->classmark, |
| 407 | .geran = { |
| 408 | .umts_aka = umts_aka, |
| 409 | .retrieve_imeisv = retrieve_imeisv, |
Neels Hofmeyr | 6ce2edc | 2021-06-09 22:26:11 +0200 | [diff] [blame] | 410 | .a5_encryption_mask = filter_a5(net->a5_encryption_mask, umts_aka), |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 411 | |
| 412 | /* for ran_a.c to store the GERAN key that is actually used */ |
| 413 | .chosen_key = &msc_a->geran_encr, |
| 414 | }, |
Harald Welte | 274b70f | 2021-02-06 16:47:39 +0100 | [diff] [blame] | 415 | .utran = { |
Harald Welte | 505a94a | 2021-02-06 17:12:20 +0100 | [diff] [blame] | 416 | .uea_encryption_mask = net->uea_encryption_mask, |
| 417 | }, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 418 | }, |
| 419 | }; |
| 420 | |
| 421 | if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) { |
| 422 | LOG_MSC_A(msc_a, LOGL_ERROR, "Sending Cipher Mode Command failed\n"); |
| 423 | /* Returning error to the VLR ops.set_ciph_mode() will cancel the attach. Other callers need to take |
| 424 | * care of the return value. */ |
| 425 | return -EINVAL; |
| 426 | } |
| 427 | |
| 428 | if (msc_a->geran_encr.key_len) |
Neels Hofmeyr | 73d093a | 2021-06-23 23:54:43 +0200 | [diff] [blame] | 429 | LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN encoding chose ciphering: A5/%d kc %s kc128 %s\n", |
| 430 | msc_a->geran_encr.alg_id - 1, |
| 431 | osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.key, msc_a->geran_encr.key_len), |
| 432 | msc_a->geran_encr.kc128_present ? |
| 433 | osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.kc128, sizeof(msc_a->geran_encr.kc128)) |
| 434 | : "-"); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static void msc_a_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 439 | { |
| 440 | struct msc_a *msc_a = fi->priv; |
| 441 | |
| 442 | /* If accepted, transition the state, all other cases mean failure. */ |
| 443 | switch (event) { |
| 444 | case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST: |
| 445 | case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST: |
| 446 | msc_a_ran_dec(msc_a, data, MSC_ROLE_I); |
| 447 | return; |
| 448 | |
| 449 | case MSC_A_EV_AUTHENTICATED: |
| 450 | msc_a_state_chg(msc_a, MSC_A_ST_AUTHENTICATED); |
| 451 | return; |
| 452 | |
| 453 | case MSC_A_EV_UNUSED: |
| 454 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASING); |
| 455 | return; |
| 456 | |
| 457 | case MSC_A_EV_MO_CLOSE: |
| 458 | case MSC_A_EV_CN_CLOSE: |
| 459 | evaluate_acceptance_outcome(fi, false); |
| 460 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASING); |
| 461 | return; |
| 462 | |
| 463 | |
| 464 | default: |
| 465 | OSMO_ASSERT(false); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | static void msc_a_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 470 | { |
| 471 | struct msc_a *msc_a = fi->priv; |
| 472 | |
| 473 | switch (event) { |
| 474 | case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST: |
| 475 | case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST: |
| 476 | msc_a_ran_dec(msc_a, data, MSC_ROLE_I); |
| 477 | return; |
| 478 | |
| 479 | case MSC_A_EV_CLASSMARK_UPDATE: |
| 480 | switch (msc_a->action_on_classmark_update.type) { |
| 481 | case MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING: |
| 482 | msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH); |
| 483 | if (msc_a_ran_enc_ciphering(msc_a, |
| 484 | msc_a->action_on_classmark_update.ciphering.umts_aka, |
| 485 | msc_a->action_on_classmark_update.ciphering.retrieve_imeisv)) { |
| 486 | LOG_MSC_A(msc_a, LOGL_ERROR, |
| 487 | "After Classmark Update, still failed to send Cipher Mode Command\n"); |
| 488 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASING); |
| 489 | } |
| 490 | return; |
| 491 | |
| 492 | default: |
| 493 | LOG_MSC_A(msc_a, LOGL_ERROR, "Internal error: After Classmark Update, don't know what to do\n"); |
| 494 | msc_a_state_chg(msc_a, msc_a->state_before_classmark_update); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | case MSC_A_EV_UNUSED: |
| 499 | /* Seems something detached / aborted in the middle of auth+ciph. */ |
| 500 | evaluate_acceptance_outcome(fi, false); |
| 501 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASING); |
| 502 | return; |
| 503 | |
| 504 | case MSC_A_EV_MO_CLOSE: |
| 505 | case MSC_A_EV_CN_CLOSE: |
| 506 | evaluate_acceptance_outcome(fi, false); |
| 507 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASING); |
| 508 | return; |
| 509 | |
| 510 | default: |
| 511 | OSMO_ASSERT(false); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | static bool msc_a_fsm_has_active_transactions(struct osmo_fsm_inst *fi) |
| 516 | { |
| 517 | struct msc_a *msc_a = fi->priv; |
| 518 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
| 519 | struct gsm_trans *trans; |
| 520 | |
| 521 | if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_SILENT_CALL)) { |
| 522 | LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: silent call still active\n", __func__); |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC)) { |
| 527 | LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO CC request after a CM Service Request\n", |
| 528 | __func__); |
| 529 | return true; |
| 530 | } |
Andreas Eversberg | 456c6f7 | 2023-04-23 11:54:16 +0200 | [diff] [blame] | 531 | if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_GCC)) { |
| 532 | LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO GCC request after a CM Service Request\n", |
| 533 | __func__); |
| 534 | return true; |
| 535 | } |
| 536 | if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_BCC)) { |
| 537 | LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO BCC request after a CM Service Request\n", |
| 538 | __func__); |
| 539 | return true; |
| 540 | } |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 541 | if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SMS)) { |
| 542 | LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SMS after a CM Service Request\n", |
| 543 | __func__); |
| 544 | return true; |
| 545 | } |
| 546 | if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SS)) { |
| 547 | LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SS after a CM Service Request\n", |
| 548 | __func__); |
| 549 | return true; |
| 550 | } |
| 551 | |
| 552 | if (vsub && !llist_empty(&vsub->cs.requests)) { |
| 553 | struct paging_request *pr; |
| 554 | llist_for_each_entry(pr, &vsub->cs.requests, entry) { |
| 555 | LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still active: %s\n", __func__, pr->label); |
| 556 | } |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | if ((trans = trans_has_conn(msc_a))) { |
| 561 | LOG_MSC_A(msc_a, LOGL_DEBUG, "connection still has active transaction: %s\n", |
| 562 | trans_type_name(trans->type)); |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | static void msc_a_fsm_authenticated_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 570 | { |
| 571 | struct msc_a *msc_a = fi->priv; |
| 572 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
| 573 | |
| 574 | /* Stop Location Update expiry for this subscriber. While the subscriber |
| 575 | * has an open connection the LU expiry timer must remain disabled. |
| 576 | * Otherwise we would kick the subscriber off the network when the timer |
| 577 | * expires e.g. during a long phone call. |
| 578 | * The LU expiry timer will restart once the connection is closed. */ |
| 579 | if (vsub) |
| 580 | vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION; |
| 581 | |
| 582 | evaluate_acceptance_outcome(fi, true); |
| 583 | } |
| 584 | |
| 585 | static void msc_a_fsm_authenticated(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 586 | { |
| 587 | struct msc_a *msc_a = fi->priv; |
| 588 | |
| 589 | switch (event) { |
| 590 | case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST: |
| 591 | case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST: |
| 592 | case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST: |
| 593 | msc_a_ran_dec(msc_a, data, MSC_ROLE_I); |
| 594 | return; |
| 595 | |
| 596 | case MSC_A_EV_COMPLETE_LAYER_3_OK: |
| 597 | /* When Authentication is off, we may already be in the Accepted state when the code |
| 598 | * evaluates the Compl L3. Simply ignore. This just cosmetically mutes the error log |
| 599 | * about the useless event. */ |
| 600 | return; |
| 601 | |
| 602 | case MSC_A_EV_TRANSACTION_ACCEPTED: |
| 603 | msc_a_state_chg(msc_a, MSC_A_ST_COMMUNICATING); |
| 604 | return; |
| 605 | |
| 606 | case MSC_A_EV_MO_CLOSE: |
| 607 | case MSC_A_EV_CN_CLOSE: |
| 608 | case MSC_A_EV_UNUSED: |
| 609 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASING); |
| 610 | return; |
| 611 | |
| 612 | default: |
| 613 | OSMO_ASSERT(false); |
| 614 | } |
| 615 | } |
| 616 | |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 617 | static struct call_leg *msc_a_ensure_call_leg(struct msc_a *msc_a, struct gsm_trans *for_cc_trans) |
| 618 | { |
| 619 | struct call_leg *cl = msc_a->cc.call_leg; |
| 620 | struct gsm_network *net = msc_a_net(msc_a); |
| 621 | |
| 622 | /* Ensure that events about RTP endpoints coming from the msc_a->cc.call_leg know which gsm_trans to abort on |
| 623 | * error */ |
| 624 | if (!msc_a->cc.active_trans) |
| 625 | msc_a->cc.active_trans = for_cc_trans; |
| 626 | if (msc_a->cc.active_trans != for_cc_trans) { |
| 627 | LOG_TRANS(for_cc_trans, LOGL_ERROR, |
| 628 | "Cannot create call leg, another trans is already active for this conn\n"); |
| 629 | return NULL; |
| 630 | } |
| 631 | |
| 632 | if (!cl) { |
| 633 | cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi, |
| 634 | MSC_EV_CALL_LEG_TERM, |
| 635 | MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, |
| 636 | MSC_EV_CALL_LEG_RTP_COMPLETE); |
| 637 | OSMO_ASSERT(cl); |
| 638 | |
| 639 | if (net->use_osmux != OSMUX_USAGE_OFF) { |
| 640 | struct msc_i *msc_i = msc_a_msc_i(msc_a); |
| 641 | if (msc_i->c.remote_to) { |
| 642 | /* TODO: investigate what to do in this case */ |
| 643 | LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC"); |
| 644 | } else { |
| 645 | cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | } |
| 650 | return cl; |
| 651 | } |
| 652 | |
| 653 | int msc_a_ensure_cn_local_rtp(struct msc_a *msc_a, struct gsm_trans *cc_trans) |
| 654 | { |
| 655 | struct call_leg *cl; |
| 656 | struct rtp_stream *rtp_to_ran; |
| 657 | |
| 658 | cl = msc_a_ensure_call_leg(msc_a, cc_trans); |
| 659 | if (!cl) |
| 660 | return -EINVAL; |
| 661 | rtp_to_ran = cl->rtp[RTP_TO_RAN]; |
| 662 | |
| 663 | if (call_leg_local_ip(cl, RTP_TO_CN)) { |
| 664 | /* Already has an RTP address and port towards the CN, continue right away. */ |
| 665 | return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]); |
| 666 | } |
| 667 | |
| 668 | /* No CN RTP address available yet, ask the MGW to create one. |
| 669 | * Set a codec to be used: if Assignment on the RAN side is already done, take the same codec as the RTP_TO_RAN. |
| 670 | * If no RAN side RTP is established, try to guess a preliminary codec from SDP -- before Assignment, picking a |
| 671 | * codec from the SDP is more politeness/avoiding confusion than necessity. The actual codec to be used would be |
| 672 | * determined later. If no codec could be determined, pass none for the time being. */ |
Andreas Eversberg | 712b28e | 2023-06-21 11:17:26 +0200 | [diff] [blame] | 673 | return call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans, |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 674 | rtp_to_ran->codecs_known ? &rtp_to_ran->codecs : NULL, NULL); |
| 675 | } |
| 676 | |
Neels Hofmeyr | 22d4f35 | 2024-06-21 19:08:34 +0200 | [diff] [blame] | 677 | static void assignment_request_timeout_cb(void *data); |
| 678 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 679 | /* The MGW has given us a local IP address for the RAN side. Ready to start the Assignment of a voice channel. */ |
Neels Hofmeyr | d767c73 | 2023-11-17 04:12:29 +0100 | [diff] [blame] | 680 | void msc_a_tx_assignment_cmd(struct msc_a *msc_a) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 681 | { |
| 682 | struct ran_msg msg; |
| 683 | struct gsm_trans *cc_trans = msc_a->cc.active_trans; |
| 684 | struct gsm0808_channel_type channel_type; |
| 685 | |
Neels Hofmeyr | 22d4f35 | 2024-06-21 19:08:34 +0200 | [diff] [blame] | 686 | /* Do not dispatch another Assignment Command before an earlier assignment is completed. This is a sanity |
| 687 | * safeguard, ideally callers should not even invoke this function when an Assignment is already ongoing. |
| 688 | * (There is no osmo_fsm for Assignment / the CC trans code; when we refactor that one day, this timer should be |
| 689 | * an FSM state.) */ |
| 690 | if (osmo_timer_pending(&msc_a->cc.assignment_request_pending)) { |
| 691 | LOG_MSC_A(msc_a, LOGL_ERROR, |
| 692 | "Not transmitting Assignment, still waiting for the response to an earlier Assignment\n"); |
| 693 | return; |
| 694 | } |
| 695 | osmo_timer_setup(&msc_a->cc.assignment_request_pending, assignment_request_timeout_cb, msc_a); |
| 696 | osmo_timer_schedule(&msc_a->cc.assignment_request_pending, |
| 697 | osmo_tdef_get(msc_a->c.ran->tdefs, -37, OSMO_TDEF_S, 10), 0); |
| 698 | |
Neels Hofmeyr | 00a476b | 2019-11-28 02:46:05 +0100 | [diff] [blame] | 699 | if (!cc_trans) { |
| 700 | LOG_MSC_A(msc_a, LOGL_ERROR, "No CC transaction active\n"); |
| 701 | call_leg_release(msc_a->cc.call_leg); |
| 702 | return; |
| 703 | } |
| 704 | |
Oliver Smith | ceca8e6 | 2023-05-24 11:15:52 +0200 | [diff] [blame] | 705 | trans_cc_filter_run(cc_trans); |
| 706 | LOG_TRANS(cc_trans, LOGL_DEBUG, "Sending Assignment Command\n"); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 707 | |
Oliver Smith | 1063213 | 2023-05-12 12:14:22 +0200 | [diff] [blame] | 708 | switch (cc_trans->bearer_cap.transfer) { |
| 709 | case GSM48_BCAP_ITCAP_SPEECH: |
| 710 | if (!cc_trans->cc.local.audio_codecs.count) { |
| 711 | LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible, no matching codec: %s\n", |
| 712 | codec_filter_to_str(&cc_trans->cc.codecs, &cc_trans->cc.local, &cc_trans->cc.remote)); |
| 713 | call_leg_release(msc_a->cc.call_leg); |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | /* Compose 48.008 Channel Type from the current set of codecs |
| 718 | * determined from both local and remote codec capabilities. */ |
| 719 | if (sdp_audio_codecs_to_gsm0808_channel_type(&channel_type, &cc_trans->cc.local.audio_codecs)) { |
| 720 | LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose Channel Type (Permitted Speech) from codecs: %s\n", |
| 721 | codec_filter_to_str(&cc_trans->cc.codecs, &cc_trans->cc.local, &cc_trans->cc.remote)); |
| 722 | trans_free(cc_trans); |
| 723 | return; |
| 724 | } |
| 725 | break; |
Manawyrm | 1ed12ea | 2023-10-14 17:23:04 +0200 | [diff] [blame] | 726 | case GSM48_BCAP_ITCAP_3k1_AUDIO: |
| 727 | case GSM48_BCAP_ITCAP_FAX_G3: |
Oliver Smith | 1063213 | 2023-05-12 12:14:22 +0200 | [diff] [blame] | 728 | case GSM48_BCAP_ITCAP_UNR_DIG_INF: |
| 729 | if (!cc_trans->cc.local.bearer_services.count) { |
| 730 | LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible, no matching bearer service: %s\n", |
| 731 | csd_filter_to_str(&cc_trans->cc.csd, &cc_trans->cc.local, &cc_trans->cc.remote)); |
| 732 | call_leg_release(msc_a->cc.call_leg); |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | /* Compose 48.008 Channel Type from the current set of bearer |
| 737 | * services determined from local and remote capabilities. */ |
| 738 | if (csd_bs_list_to_gsm0808_channel_type(&channel_type, &cc_trans->cc.local.bearer_services)) { |
| 739 | LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose channel type from: %s\n", |
| 740 | csd_filter_to_str(&cc_trans->cc.csd, &cc_trans->cc.local, &cc_trans->cc.remote)); |
| 741 | return; |
| 742 | } |
| 743 | break; |
| 744 | default: |
| 745 | LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible for information transfer capability %d\n", |
| 746 | cc_trans->bearer_cap.transfer); |
Neels Hofmeyr | 11a746a | 2023-01-26 15:00:06 +0100 | [diff] [blame] | 747 | call_leg_release(msc_a->cc.call_leg); |
| 748 | return; |
| 749 | } |
| 750 | |
Oliver Smith | 1063213 | 2023-05-12 12:14:22 +0200 | [diff] [blame] | 751 | /* The RAN side RTP address is known, so the voice/CSD Assignment can commence. */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 752 | msg = (struct ran_msg){ |
| 753 | .msg_type = RAN_MSG_ASSIGNMENT_COMMAND, |
| 754 | .assignment_command = { |
| 755 | .cn_rtp = &msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local, |
| 756 | .channel_type = &channel_type, |
Pau Espin Pedrol | a3cdab4 | 2019-05-09 17:54:08 +0200 | [diff] [blame] | 757 | .osmux_present = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->use_osmux, |
| 758 | .osmux_cid = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local_osmux_cid, |
Philipp Maier | f34d945 | 2020-06-05 15:49:35 +0200 | [diff] [blame] | 759 | .call_id_present = true, |
Andreas Eversberg | 712b28e | 2023-06-21 11:17:26 +0200 | [diff] [blame] | 760 | .call_id = cc_trans->call_id, |
Keith Whyte | a1a70be | 2021-05-16 02:59:52 +0200 | [diff] [blame] | 761 | .lcls = cc_trans->cc.lcls, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 762 | }, |
| 763 | }; |
| 764 | if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) { |
| 765 | LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Assignment\n"); |
Neels Hofmeyr | f439ff1 | 2019-10-05 04:19:36 +0200 | [diff] [blame] | 766 | trans_free(cc_trans); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | } |
| 770 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 771 | static struct gsm_trans *find_waiting_call(struct msc_a *msc_a) |
| 772 | { |
| 773 | struct gsm_trans *trans; |
| 774 | struct gsm_network *net = msc_a_net(msc_a); |
| 775 | |
| 776 | llist_for_each_entry(trans, &net->trans_list, entry) { |
| 777 | if (trans->msc_a != msc_a) |
| 778 | continue; |
| 779 | if (trans->type != TRANS_CC) |
| 780 | continue; |
| 781 | if (trans->msc_a->cc.active_trans == trans) |
| 782 | continue; |
| 783 | return trans; |
| 784 | } |
| 785 | return NULL; |
| 786 | } |
| 787 | |
| 788 | static void msc_a_cleanup_rtp_streams(struct msc_a *msc_a, uint32_t event, void *data) |
| 789 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 790 | switch (event) { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 791 | |
| 792 | case MSC_EV_CALL_LEG_TERM: |
| 793 | msc_a->cc.call_leg = NULL; |
| 794 | if (msc_a->cc.mncc_forwarding_to_remote_ran) |
| 795 | msc_a->cc.mncc_forwarding_to_remote_ran->rtps = NULL; |
| 796 | |
Neels Hofmeyr | 265a4c7 | 2019-05-09 16:20:51 +0200 | [diff] [blame] | 797 | if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 798 | msc_a->ho.new_cell.mncc_forwarding_to_remote_ran->rtps = NULL; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 799 | return; |
| 800 | |
| 801 | case MSC_MNCC_EV_CALL_ENDED: |
| 802 | msc_a->cc.mncc_forwarding_to_remote_ran = NULL; |
| 803 | return; |
| 804 | |
| 805 | default: |
| 806 | return; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | static void msc_a_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 811 | { |
| 812 | struct msc_a *msc_a = fi->priv; |
| 813 | struct rtp_stream *rtps; |
| 814 | struct gsm_trans *waiting_trans; |
| 815 | struct an_apdu *an_apdu; |
| 816 | |
| 817 | msc_a_cleanup_rtp_streams(msc_a, event, data); |
| 818 | |
| 819 | switch (event) { |
| 820 | case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST: |
| 821 | case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST: |
| 822 | case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST: |
| 823 | an_apdu = data; |
| 824 | msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I); |
| 825 | return; |
| 826 | |
| 827 | case MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE: |
| 828 | case MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE: |
| 829 | case MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST: |
| 830 | case MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST: |
| 831 | an_apdu = data; |
| 832 | msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_T); |
| 833 | return; |
| 834 | |
| 835 | case MSC_A_EV_TRANSACTION_ACCEPTED: |
| 836 | /* no-op */ |
| 837 | return; |
| 838 | |
| 839 | case MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE: |
| 840 | rtps = data; |
| 841 | if (!rtps) { |
| 842 | LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event)); |
| 843 | return; |
| 844 | } |
Neels Hofmeyr | 00a476b | 2019-11-28 02:46:05 +0100 | [diff] [blame] | 845 | if (!msc_a->cc.call_leg) { |
| 846 | LOG_MSC_A(msc_a, LOGL_ERROR, "No call leg active\n"); |
| 847 | return; |
| 848 | } |
Neels Hofmeyr | cc918cb | 2019-11-28 02:16:34 +0100 | [diff] [blame] | 849 | if (!osmo_sockaddr_str_is_nonzero(&rtps->local)) { |
| 850 | LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n", |
| 851 | OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local)); |
| 852 | call_leg_release(msc_a->cc.call_leg); |
| 853 | return; |
| 854 | } |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 855 | LOG_MSC_A(msc_a, LOGL_DEBUG, |
Pau Espin Pedrol | a3cdab4 | 2019-05-09 17:54:08 +0200 | [diff] [blame] | 856 | "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n", |
| 857 | rtp_direction_name(rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local), |
| 858 | rtps->use_osmux ? "yes" : "no", rtps->local_osmux_cid); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 859 | switch (rtps->dir) { |
| 860 | case RTP_TO_RAN: |
Neels Hofmeyr | d767c73 | 2023-11-17 04:12:29 +0100 | [diff] [blame] | 861 | msc_a_tx_assignment_cmd(msc_a); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 862 | return; |
| 863 | case RTP_TO_CN: |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 864 | cc_on_cn_local_rtp_port_known(rtps->for_trans); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 865 | return; |
| 866 | default: |
| 867 | LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event)); |
| 868 | return; |
| 869 | } |
| 870 | |
| 871 | case MSC_EV_CALL_LEG_RTP_COMPLETE: |
| 872 | /* Nothing to do. */ |
| 873 | return; |
| 874 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 875 | case MSC_MNCC_EV_CALL_ENDED: |
| 876 | /* Cleaned up above */ |
| 877 | return; |
| 878 | |
| 879 | case MSC_EV_CALL_LEG_TERM: |
| 880 | /* RTP streams cleaned up above */ |
| 881 | |
| 882 | msc_a_get(msc_a, __func__); |
| 883 | if (msc_a->cc.active_trans) |
| 884 | trans_free(msc_a->cc.active_trans); |
| 885 | |
| 886 | /* If there is another call still waiting to be activated, this is the time when the mgcp_ctx is |
| 887 | * available again and the other call can start assigning. */ |
| 888 | waiting_trans = find_waiting_call(msc_a); |
| 889 | if (waiting_trans) { |
| 890 | LOG_MSC_A(msc_a, LOGL_DEBUG, "(ti %02x) Call waiting: starting Assignment\n", |
| 891 | waiting_trans->transaction_id); |
| 892 | msc_a_try_call_assignment(waiting_trans); |
| 893 | } |
| 894 | msc_a_put(msc_a, __func__); |
| 895 | return; |
| 896 | |
| 897 | case MSC_A_EV_HANDOVER_REQUIRED: |
| 898 | msc_ho_start(msc_a, (struct ran_handover_required*)data); |
| 899 | return; |
| 900 | |
Neels Hofmeyr | 0a437be | 2019-05-10 15:55:52 +0200 | [diff] [blame] | 901 | case MSC_A_EV_HANDOVER_END: |
| 902 | /* Termination event of the msc_ho_fsm. No action needed, it's all done in the msc_ho_fsm cleanup. This |
| 903 | * event only exists because osmo_fsm_inst_alloc_child() requires a parent term event; and maybe |
| 904 | * interesting for logging. */ |
| 905 | return; |
| 906 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 907 | case MSC_A_EV_MO_CLOSE: |
| 908 | case MSC_A_EV_CN_CLOSE: |
| 909 | case MSC_A_EV_UNUSED: |
| 910 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASING); |
| 911 | return; |
| 912 | |
| 913 | default: |
| 914 | OSMO_ASSERT(false); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | static int msc_a_fsm_timer_cb(struct osmo_fsm_inst *fi) |
| 919 | { |
| 920 | struct msc_a *msc_a = fi->priv; |
| 921 | if (msc_a_in_release(msc_a)) { |
| 922 | LOG_MSC_A(msc_a, LOGL_ERROR, "Timeout while releasing, discarding right now\n"); |
| 923 | msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE); |
| 924 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASED); |
| 925 | } else { |
| 926 | enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION; |
| 927 | osmo_fsm_inst_dispatch(fi, MSC_A_EV_CN_CLOSE, &cause); |
| 928 | } |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | static void msc_a_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 933 | { |
| 934 | struct msc_a *msc_a = fi->priv; |
| 935 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
| 936 | int i; |
| 937 | char buf[128]; |
| 938 | const char * const use_counts_to_cancel[] = { |
| 939 | MSC_A_USE_LOCATION_UPDATING, |
| 940 | MSC_A_USE_CM_SERVICE_CC, |
| 941 | MSC_A_USE_CM_SERVICE_SMS, |
| 942 | MSC_A_USE_CM_SERVICE_SS, |
Andreas Eversberg | 456c6f7 | 2023-04-23 11:54:16 +0200 | [diff] [blame] | 943 | MSC_A_USE_CM_SERVICE_GCC, |
| 944 | MSC_A_USE_CM_SERVICE_BCC, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 945 | MSC_A_USE_PAGING_RESPONSE, |
| 946 | }; |
| 947 | |
| 948 | LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: msc_a use is %s\n", |
| 949 | osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count)); |
| 950 | |
| 951 | if (vsub) { |
| 952 | vlr_subscr_get(vsub, __func__); |
| 953 | |
| 954 | /* Cancel all VLR FSMs, if any */ |
| 955 | vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION); |
| 956 | |
| 957 | /* The subscriber has no active connection anymore. |
| 958 | * Restart the periodic Location Update expiry timer for this subscriber. */ |
| 959 | vlr_subscr_enable_expire_lu(vsub); |
| 960 | } |
| 961 | |
Neels Hofmeyr | 22d4f35 | 2024-06-21 19:08:34 +0200 | [diff] [blame] | 962 | /* We no longer care about assignment responses. */ |
| 963 | osmo_timer_del(&msc_a->cc.assignment_request_pending); |
| 964 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 965 | /* If we're closing in a middle of a trans, we need to clean up */ |
| 966 | trans_conn_closed(msc_a); |
| 967 | |
| 968 | call_leg_release(msc_a->cc.call_leg); |
| 969 | |
| 970 | /* Cancel use counts for pending CM Service / Paging */ |
| 971 | for (i = 0; i < ARRAY_SIZE(use_counts_to_cancel); i++) { |
| 972 | const char *use = use_counts_to_cancel[i]; |
| 973 | int32_t count = osmo_use_count_by(&msc_a->use_count, use); |
| 974 | if (!count) |
| 975 | continue; |
| 976 | LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: canceling still pending use: %s (%d)\n", use, count); |
| 977 | osmo_use_count_get_put(&msc_a->use_count, use, -count); |
| 978 | } |
| 979 | |
| 980 | if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) { |
| 981 | sgs_iface_tx_release(vsub); |
| 982 | /* In SGsAP there is no confirmation of a release. */ |
| 983 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASED); |
| 984 | } else { |
| 985 | struct ran_msg msg = { |
| 986 | .msg_type = RAN_MSG_CLEAR_COMMAND, |
| 987 | .clear_command = { |
Neels Hofmeyr | d9fe711 | 2020-07-11 00:20:20 +0200 | [diff] [blame] | 988 | /* "Call Control" is the only cause code listed in 3GPP TS 48.008 3.2.1.21 CLEAR COMMAND |
| 989 | * that qualifies for a normal release situation. (OS#4664) */ |
| 990 | .gsm0808_cause = GSM0808_CAUSE_CALL_CONTROL, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 991 | .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED), |
| 992 | }, |
| 993 | }; |
| 994 | msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE); |
| 995 | msc_a_ran_down(msc_a, MSC_ROLE_I, &msg); |
Philipp Maier | 47cf84d | 2019-08-15 14:56:54 +0200 | [diff] [blame] | 996 | |
| 997 | /* The connection is cleared. The MS will now go back to 4G, |
| 998 | Switch the RAN type back to SGS. */ |
| 999 | if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED) |
| 1000 | vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | if (vsub) |
| 1004 | vlr_subscr_put(vsub, __func__); |
| 1005 | } |
| 1006 | |
| 1007 | static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1008 | { |
| 1009 | struct msc_a *msc_a = fi->priv; |
| 1010 | |
| 1011 | msc_a_cleanup_rtp_streams(msc_a, event, data); |
| 1012 | |
| 1013 | switch (event) { |
| 1014 | case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST: |
| 1015 | case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST: |
| 1016 | msc_a_ran_dec(msc_a, data, MSC_ROLE_I); |
| 1017 | return; |
| 1018 | |
| 1019 | case MSC_A_EV_MO_CLOSE: |
| 1020 | case MSC_A_EV_CN_CLOSE: |
| 1021 | case MSC_A_EV_UNUSED: |
| 1022 | /* Already releasing */ |
| 1023 | return; |
| 1024 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1025 | case MSC_EV_CALL_LEG_TERM: |
| 1026 | case MSC_MNCC_EV_CALL_ENDED: |
| 1027 | /* RTP streams cleaned up above */ |
| 1028 | return; |
| 1029 | |
Neels Hofmeyr | 0a437be | 2019-05-10 15:55:52 +0200 | [diff] [blame] | 1030 | case MSC_A_EV_HANDOVER_END: |
| 1031 | /* msc_ho_fsm does cleanup. */ |
| 1032 | return; |
| 1033 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1034 | default: |
| 1035 | OSMO_ASSERT(false); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 1041 | { |
| 1042 | struct msc_a *msc_a = msc_a_fi_priv(fi); |
| 1043 | char buf[128]; |
| 1044 | LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n", |
| 1045 | osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count)); |
| 1046 | if (osmo_use_count_total(&msc_a->use_count) == 0) |
| 1047 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi); |
| 1048 | } |
| 1049 | |
| 1050 | static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1051 | { |
| 1052 | if (event == MSC_A_EV_UNUSED) |
| 1053 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi); |
| 1054 | } |
| 1055 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1056 | void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) |
| 1057 | { |
| 1058 | struct msc_a *msc_a = msc_a_fi_priv(fi); |
Neels Hofmeyr | 72ef7d8 | 2024-03-26 01:43:53 +0100 | [diff] [blame] | 1059 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1060 | |
| 1061 | trans_conn_closed(msc_a); |
| 1062 | |
| 1063 | if (msc_a_fsm_has_active_transactions(fi)) |
| 1064 | LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n"); |
| 1065 | |
| 1066 | LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count); |
Neels Hofmeyr | 72ef7d8 | 2024-03-26 01:43:53 +0100 | [diff] [blame] | 1067 | |
| 1068 | /* Invalidate the active conn in VLR subscriber state, if any. */ |
| 1069 | if (vsub && vsub->msc_conn_ref == msc_a) |
| 1070 | vsub->msc_conn_ref = NULL; |
Vadim Yanitskiy | 3e66ec5 | 2024-05-08 00:17:43 +0200 | [diff] [blame] | 1071 | |
| 1072 | osmo_timer_del(&msc_a->lu_delay_timer); |
Neels Hofmeyr | 22d4f35 | 2024-06-21 19:08:34 +0200 | [diff] [blame] | 1073 | osmo_timer_del(&msc_a->cc.assignment_request_pending); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | const struct value_string msc_a_fsm_event_names[] = { |
| 1077 | OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP), |
| 1078 | OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE), |
| 1079 | OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE), |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1080 | OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM), |
| 1081 | OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP), |
| 1082 | OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING), |
| 1083 | OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE), |
| 1084 | OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED), |
| 1085 | OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3), |
| 1086 | OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST), |
| 1087 | OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST), |
| 1088 | OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST), |
| 1089 | OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST), |
| 1090 | OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE), |
| 1091 | OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE), |
| 1092 | OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST), |
| 1093 | OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK), |
| 1094 | OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE), |
| 1095 | OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED), |
| 1096 | OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED), |
| 1097 | OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE), |
| 1098 | OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE), |
| 1099 | OSMO_VALUE_STRING(MSC_A_EV_UNUSED), |
| 1100 | OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED), |
| 1101 | OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END), |
| 1102 | {} |
| 1103 | }; |
| 1104 | |
| 1105 | #define S(x) (1 << (x)) |
| 1106 | |
| 1107 | static const struct osmo_fsm_state msc_a_fsm_states[] = { |
| 1108 | [MSC_A_ST_VALIDATE_L3] = { |
| 1109 | .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3), |
| 1110 | .in_event_mask = 0 |
| 1111 | | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3) |
| 1112 | | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST) |
| 1113 | | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST) |
| 1114 | | S(MSC_A_EV_COMPLETE_LAYER_3_OK) |
| 1115 | | S(MSC_A_EV_MO_CLOSE) |
| 1116 | | S(MSC_A_EV_CN_CLOSE) |
| 1117 | | S(MSC_A_EV_UNUSED) |
| 1118 | , |
| 1119 | .out_state_mask = 0 |
| 1120 | | S(MSC_A_ST_VALIDATE_L3) |
| 1121 | | S(MSC_A_ST_AUTH_CIPH) |
| 1122 | | S(MSC_A_ST_RELEASING) |
| 1123 | , |
| 1124 | .action = msc_a_fsm_validate_l3, |
| 1125 | }, |
| 1126 | [MSC_A_ST_AUTH_CIPH] = { |
| 1127 | .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH), |
| 1128 | .in_event_mask = 0 |
| 1129 | | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST) |
| 1130 | | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST) |
| 1131 | | S(MSC_A_EV_AUTHENTICATED) |
| 1132 | | S(MSC_A_EV_MO_CLOSE) |
| 1133 | | S(MSC_A_EV_CN_CLOSE) |
| 1134 | | S(MSC_A_EV_UNUSED) |
| 1135 | , |
| 1136 | .out_state_mask = 0 |
| 1137 | | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE) |
| 1138 | | S(MSC_A_ST_AUTHENTICATED) |
| 1139 | | S(MSC_A_ST_RELEASING) |
| 1140 | , |
| 1141 | .action = msc_a_fsm_auth_ciph, |
| 1142 | }, |
| 1143 | [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = { |
| 1144 | .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE), |
| 1145 | .in_event_mask = 0 |
| 1146 | | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST) |
| 1147 | | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST) |
| 1148 | | S(MSC_A_EV_CLASSMARK_UPDATE) |
| 1149 | | S(MSC_A_EV_MO_CLOSE) |
| 1150 | | S(MSC_A_EV_CN_CLOSE) |
| 1151 | , |
| 1152 | .out_state_mask = 0 |
| 1153 | | S(MSC_A_ST_AUTH_CIPH) |
| 1154 | | S(MSC_A_ST_RELEASING) |
| 1155 | , |
| 1156 | .action = msc_a_fsm_wait_classmark_update, |
| 1157 | }, |
| 1158 | [MSC_A_ST_AUTHENTICATED] = { |
| 1159 | .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED), |
| 1160 | /* allow everything to release for any odd behavior */ |
| 1161 | .in_event_mask = 0 |
| 1162 | | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST) |
| 1163 | | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST) |
| 1164 | | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST) |
| 1165 | | S(MSC_A_EV_TRANSACTION_ACCEPTED) |
| 1166 | | S(MSC_A_EV_MO_CLOSE) |
| 1167 | | S(MSC_A_EV_CN_CLOSE) |
| 1168 | | S(MSC_A_EV_UNUSED) |
| 1169 | , |
| 1170 | .out_state_mask = 0 |
| 1171 | | S(MSC_A_ST_RELEASING) |
| 1172 | | S(MSC_A_ST_COMMUNICATING) |
| 1173 | , |
| 1174 | .onenter = msc_a_fsm_authenticated_enter, |
| 1175 | .action = msc_a_fsm_authenticated, |
| 1176 | }, |
| 1177 | [MSC_A_ST_COMMUNICATING] = { |
| 1178 | .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING), |
| 1179 | /* allow everything to release for any odd behavior */ |
| 1180 | .in_event_mask = 0 |
| 1181 | | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST) |
| 1182 | | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST) |
| 1183 | | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST) |
| 1184 | | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE) |
| 1185 | | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE) |
| 1186 | | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST) |
| 1187 | | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST) |
| 1188 | | S(MSC_A_EV_TRANSACTION_ACCEPTED) |
| 1189 | | S(MSC_A_EV_MO_CLOSE) |
| 1190 | | S(MSC_A_EV_CN_CLOSE) |
| 1191 | | S(MSC_A_EV_UNUSED) |
| 1192 | | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE) |
| 1193 | | S(MSC_EV_CALL_LEG_RTP_COMPLETE) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1194 | | S(MSC_EV_CALL_LEG_TERM) |
| 1195 | | S(MSC_MNCC_EV_CALL_ENDED) |
| 1196 | | S(MSC_A_EV_HANDOVER_REQUIRED) |
Neels Hofmeyr | 0a437be | 2019-05-10 15:55:52 +0200 | [diff] [blame] | 1197 | | S(MSC_A_EV_HANDOVER_END) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1198 | , |
| 1199 | .out_state_mask = 0 |
| 1200 | | S(MSC_A_ST_RELEASING) |
| 1201 | , |
| 1202 | .action = msc_a_fsm_communicating, |
| 1203 | }, |
| 1204 | [MSC_A_ST_RELEASING] = { |
| 1205 | .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING), |
| 1206 | .in_event_mask = 0 |
| 1207 | | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST) |
| 1208 | | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST) |
| 1209 | | S(MSC_A_EV_UNUSED) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1210 | | S(MSC_EV_CALL_LEG_TERM) |
| 1211 | | S(MSC_MNCC_EV_CALL_ENDED) |
Neels Hofmeyr | 0a437be | 2019-05-10 15:55:52 +0200 | [diff] [blame] | 1212 | | S(MSC_A_EV_HANDOVER_END) |
Pau Espin Pedrol | e53ecde | 2021-07-12 13:37:24 +0200 | [diff] [blame] | 1213 | | S(MSC_A_EV_CN_CLOSE) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1214 | , |
| 1215 | .out_state_mask = 0 |
| 1216 | | S(MSC_A_ST_RELEASED) |
| 1217 | , |
| 1218 | .onenter = msc_a_fsm_releasing_onenter, |
| 1219 | .action = msc_a_fsm_releasing, |
| 1220 | }, |
| 1221 | [MSC_A_ST_RELEASED] = { |
| 1222 | .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED), |
| 1223 | .in_event_mask = 0 |
| 1224 | | S(MSC_A_EV_UNUSED) |
| 1225 | , |
| 1226 | .onenter = msc_a_fsm_released_onenter, |
| 1227 | .action = msc_a_fsm_released, |
| 1228 | }, |
| 1229 | }; |
| 1230 | |
| 1231 | static struct osmo_fsm msc_a_fsm = { |
| 1232 | .name = "msc_a", |
| 1233 | .states = msc_a_fsm_states, |
| 1234 | .num_states = ARRAY_SIZE(msc_a_fsm_states), |
| 1235 | .log_subsys = DMSC, |
| 1236 | .event_names = msc_a_fsm_event_names, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1237 | .timer_cb = msc_a_fsm_timer_cb, |
| 1238 | .cleanup = msc_a_fsm_cleanup, |
| 1239 | }; |
| 1240 | |
| 1241 | static __attribute__((constructor)) void msc_a_fsm_init() |
| 1242 | { |
| 1243 | OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0); |
| 1244 | } |
| 1245 | |
| 1246 | static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line) |
| 1247 | { |
| 1248 | struct msc_a *msc_a = e->use_count->talloc_object; |
| 1249 | char buf[128]; |
| 1250 | int32_t total; |
| 1251 | int level; |
| 1252 | |
| 1253 | if (!e->use) |
| 1254 | return -EINVAL; |
| 1255 | |
| 1256 | total = osmo_use_count_total(&msc_a->use_count); |
| 1257 | |
| 1258 | if (total == 0 |
| 1259 | || (total == 1 && old_use_count == 0 && e->count == 1)) |
| 1260 | level = LOGL_INFO; |
| 1261 | else |
| 1262 | level = LOGL_DEBUG; |
| 1263 | |
| 1264 | LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n", |
| 1265 | (e->count - old_use_count) > 0? "+" : "-", e->use, |
| 1266 | osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count)); |
| 1267 | |
| 1268 | if (e->count < 0) |
| 1269 | return -ERANGE; |
| 1270 | |
| 1271 | msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total); |
| 1272 | |
| 1273 | if (total == 0) |
| 1274 | osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL); |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran) |
| 1279 | { |
| 1280 | struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran); |
| 1281 | msc_a->use_count = (struct osmo_use_count){ |
| 1282 | .talloc_object = msc_a, |
| 1283 | .use_cb = msc_a_use_cb, |
| 1284 | }; |
| 1285 | osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf)); |
| 1286 | /* Start timeout for first state */ |
Neels Hofmeyr | 0165325 | 2019-09-03 02:06:22 +0200 | [diff] [blame] | 1287 | msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3); |
Vadim Yanitskiy | 3e66ec5 | 2024-05-08 00:17:43 +0200 | [diff] [blame] | 1288 | osmo_timer_setup(&msc_a->lu_delay_timer, &lu_delay_timer_cb, msc_a); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1289 | return msc_a; |
| 1290 | } |
| 1291 | |
| 1292 | bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a) |
| 1293 | { |
| 1294 | if (!msc_a || !msc_a->c.fi) |
| 1295 | return false; |
| 1296 | return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH; |
| 1297 | } |
| 1298 | |
| 1299 | const struct value_string complete_layer3_type_names[] = { |
| 1300 | { COMPLETE_LAYER3_NONE, "NONE" }, |
| 1301 | { COMPLETE_LAYER3_LU, "LU" }, |
| 1302 | { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" }, |
| 1303 | { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" }, |
Neels Hofmeyr | ae98b97 | 2021-07-27 03:46:49 +0200 | [diff] [blame] | 1304 | { COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ, "CM_RE_ESTABLISH_REQ" }, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1305 | { 0, NULL } |
| 1306 | }; |
| 1307 | |
| 1308 | #define _msc_a_update_id(MSC_A, FMT, ARGS ...) \ |
| 1309 | do { \ |
| 1310 | if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \ |
| 1311 | ## ARGS, \ |
| 1312 | msub_ran_conn_name(msc_a->c.msub), \ |
| 1313 | complete_layer3_type_name(msc_a->complete_layer3_type)) \ |
| 1314 | == 0) { \ |
| 1315 | struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \ |
| 1316 | if (_vsub) { \ |
| 1317 | if (_vsub->lu_fsm) \ |
| 1318 | osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \ |
| 1319 | if (_vsub->auth_fsm) \ |
| 1320 | osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \ |
| 1321 | if (_vsub->proc_arq_fsm) \ |
| 1322 | osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \ |
| 1323 | } \ |
| 1324 | LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \ |
| 1325 | } \ |
| 1326 | /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \ |
| 1327 | } while (0) |
| 1328 | |
| 1329 | |
| 1330 | /* Compose an ID almost like gsm48_mi_to_string(), but print the MI type along, and print a TMSI as hex. */ |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1331 | void msc_a_update_id_from_mi(struct msc_a *msc_a, const struct osmo_mobile_identity *mi) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1332 | { |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1333 | _msc_a_update_id(msc_a, "%s", osmo_mobile_identity_to_str_c(OTC_SELECT, mi)); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */ |
| 1337 | void msc_a_update_id(struct msc_a *msc_a) |
| 1338 | { |
| 1339 | _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a))); |
| 1340 | } |
| 1341 | |
| 1342 | /* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM |
| 1343 | * instances. */ |
| 1344 | void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub) |
| 1345 | { |
| 1346 | struct msub *msub; |
| 1347 | llist_for_each_entry(msub, &msub_list, entry) { |
| 1348 | struct vlr_subscr *vsub = msub_vsub(msub); |
| 1349 | if (vsub != for_vsub) |
| 1350 | continue; |
| 1351 | msc_a_update_id(msub_msc_a(msub)); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr) |
| 1356 | { |
| 1357 | uint8_t pdisc = gsm48_hdr_pdisc(hdr); |
| 1358 | uint8_t msg_type = gsm48_hdr_msg_type(hdr); |
| 1359 | |
| 1360 | switch (pdisc) { |
| 1361 | case GSM48_PDISC_MM: |
| 1362 | switch (msg_type) { |
| 1363 | case GSM48_MT_MM_LOC_UPD_REQUEST: |
| 1364 | case GSM48_MT_MM_CM_SERV_REQ: |
| 1365 | case GSM48_MT_MM_CM_REEST_REQ: |
| 1366 | case GSM48_MT_MM_AUTH_RESP: |
| 1367 | case GSM48_MT_MM_AUTH_FAIL: |
| 1368 | case GSM48_MT_MM_ID_RESP: |
| 1369 | case GSM48_MT_MM_TMSI_REALL_COMPL: |
| 1370 | case GSM48_MT_MM_IMSI_DETACH_IND: |
| 1371 | return true; |
| 1372 | default: |
| 1373 | break; |
| 1374 | } |
| 1375 | break; |
| 1376 | case GSM48_PDISC_RR: |
| 1377 | switch (msg_type) { |
| 1378 | /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the |
| 1379 | * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */ |
| 1380 | case GSM48_MT_RR_PAG_RESP: |
| 1381 | case GSM48_MT_RR_CIPH_M_COMPL: |
| 1382 | return true; |
| 1383 | default: |
| 1384 | break; |
| 1385 | } |
| 1386 | break; |
| 1387 | default: |
| 1388 | break; |
| 1389 | } |
| 1390 | |
| 1391 | return false; |
| 1392 | } |
| 1393 | |
| 1394 | /* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */ |
| 1395 | int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg) |
| 1396 | { |
| 1397 | struct gsm48_hdr *gh; |
| 1398 | uint8_t pdisc; |
| 1399 | int rc; |
| 1400 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
| 1401 | int is_r99; |
| 1402 | |
| 1403 | OSMO_ASSERT(msg->l3h); |
| 1404 | OSMO_ASSERT(msg); |
| 1405 | |
| 1406 | gh = msgb_l3(msg); |
| 1407 | pdisc = gsm48_hdr_pdisc(gh); |
| 1408 | |
| 1409 | LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n", |
| 1410 | gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh))); |
| 1411 | |
| 1412 | /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99 |
| 1413 | * capable. If the subscriber is already actively connected, the Classmark information is stored with the |
| 1414 | * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */ |
| 1415 | if (vsub) |
| 1416 | is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0; |
| 1417 | else |
| 1418 | is_r99 = compl_l3_msg_is_r99(msg); |
| 1419 | |
| 1420 | if (is_r99 < 0) { |
| 1421 | LOG_MSC_A(msc_a, LOGL_ERROR, |
| 1422 | "No Classmark Information, dropping non-Complete-Layer3 message: %s\n", |
| 1423 | gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh))); |
| 1424 | return -EACCES; |
| 1425 | } |
| 1426 | |
| 1427 | if (is_r99 >= 0 |
| 1428 | && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) { |
| 1429 | LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message" |
| 1430 | " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n"); |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | if (!msc_a_is_accepted(msc_a) |
| 1435 | && !msg_is_initially_permitted(gh)) { |
| 1436 | LOG_MSC_A(msc_a, LOGL_ERROR, |
| 1437 | "Message not permitted for initial conn: %s\n", |
| 1438 | gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh))); |
| 1439 | return -EACCES; |
| 1440 | } |
| 1441 | |
| 1442 | if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) { |
| 1443 | LOG_MSC_A(msc_a, LOGL_ERROR, |
| 1444 | "Illegal situation: RAN type mismatch:" |
| 1445 | " attached via %s, received message via %s\n", |
| 1446 | osmo_rat_type_name(vsub->cs.attached_via_ran), |
| 1447 | osmo_rat_type_name(msc_a->c.ran->type)); |
| 1448 | return -EACCES; |
| 1449 | } |
| 1450 | |
| 1451 | #if 0 |
| 1452 | if (silent_call_reroute(conn, msg)) |
| 1453 | return silent_call_rx(conn, msg); |
| 1454 | #endif |
| 1455 | |
| 1456 | switch (pdisc) { |
Andreas Eversberg | 23b53e5 | 2023-06-21 13:30:58 +0200 | [diff] [blame] | 1457 | case GSM48_PDISC_GROUP_CC: |
| 1458 | case GSM48_PDISC_BCAST_CC: |
| 1459 | rc = gsm44068_rcv_bcc_gcc(msc_a, NULL, msg); |
| 1460 | break; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1461 | case GSM48_PDISC_CC: |
| 1462 | rc = gsm0408_rcv_cc(msc_a, msg); |
| 1463 | break; |
| 1464 | case GSM48_PDISC_MM: |
| 1465 | rc = gsm0408_rcv_mm(msc_a, msg); |
| 1466 | break; |
| 1467 | case GSM48_PDISC_RR: |
| 1468 | rc = gsm0408_rcv_rr(msc_a, msg); |
| 1469 | break; |
| 1470 | case GSM48_PDISC_SMS: |
| 1471 | rc = gsm0411_rcv_sms(msc_a, msg); |
| 1472 | break; |
| 1473 | case GSM48_PDISC_MM_GPRS: |
| 1474 | case GSM48_PDISC_SM_GPRS: |
| 1475 | LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented " |
| 1476 | "GSM 04.08 discriminator 0x%02x\n", pdisc); |
| 1477 | rc = -ENOTSUP; |
| 1478 | break; |
| 1479 | case GSM48_PDISC_NC_SS: |
| 1480 | rc = gsm0911_rcv_nc_ss(msc_a, msg); |
| 1481 | break; |
| 1482 | case GSM48_PDISC_TEST: |
| 1483 | rc = gsm0414_rcv_test(msc_a, msg); |
| 1484 | break; |
| 1485 | default: |
| 1486 | LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown " |
| 1487 | "GSM 04.08 discriminator 0x%02x\n", pdisc); |
| 1488 | rc = -EINVAL; |
| 1489 | break; |
| 1490 | } |
| 1491 | |
| 1492 | return rc; |
| 1493 | } |
| 1494 | |
| 1495 | static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac) |
| 1496 | { |
Andreas Eversberg | 23b53e5 | 2023-06-21 13:30:58 +0200 | [diff] [blame] | 1497 | struct gsm_trans *cc_trans = msc_a->cc.active_trans, *gcc_trans; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1498 | struct rtp_stream *rtps_to_ran = msc_a->cc.call_leg ? msc_a->cc.call_leg->rtp[RTP_TO_RAN] : NULL; |
Neels Hofmeyr | cec51b3 | 2023-03-01 03:47:45 +0100 | [diff] [blame] | 1499 | const struct gsm0808_speech_codec *codec_if_known = ac->assignment_complete.codec_present ? |
| 1500 | &ac->assignment_complete.codec : NULL; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1501 | |
Neels Hofmeyr | 22d4f35 | 2024-06-21 19:08:34 +0200 | [diff] [blame] | 1502 | /* Pending assignment has worked out. We're no longer waiting for a response now. */ |
| 1503 | osmo_timer_del(&msc_a->cc.assignment_request_pending); |
| 1504 | |
Andreas Eversberg | 23b53e5 | 2023-06-21 13:30:58 +0200 | [diff] [blame] | 1505 | /* For a voice group call, handling is performed by VGCS FSM */ |
| 1506 | gcc_trans = trans_find_by_type(msc_a, TRANS_GCC); |
| 1507 | if (gcc_trans) { |
| 1508 | vgcs_vbs_caller_assign_cpl(gcc_trans); |
| 1509 | return; |
| 1510 | } |
| 1511 | gcc_trans = trans_find_by_type(msc_a, TRANS_BCC); |
| 1512 | if (gcc_trans) { |
| 1513 | vgcs_vbs_caller_assign_cpl(gcc_trans); |
| 1514 | return; |
| 1515 | } |
| 1516 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1517 | if (!rtps_to_ran) { |
| 1518 | LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n"); |
| 1519 | return; |
| 1520 | } |
| 1521 | if (!cc_trans) { |
Neels Hofmeyr | 550506a | 2022-01-13 23:31:57 +0100 | [diff] [blame] | 1522 | LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no CC transaction is active\n"); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1523 | return; |
| 1524 | } |
| 1525 | |
Pau Espin Pedrol | a3cdab4 | 2019-05-09 17:54:08 +0200 | [diff] [blame] | 1526 | if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) { |
| 1527 | LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n", |
| 1528 | rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present); |
| 1529 | call_leg_release(msc_a->cc.call_leg); |
| 1530 | return; |
| 1531 | } |
| 1532 | |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 1533 | if (codec_if_known) { |
Neels Hofmeyr | 1a07bcd | 2024-01-19 06:37:57 +0100 | [diff] [blame] | 1534 | const struct codec_mapping *codec_assigned; |
| 1535 | |
Neels Hofmeyr | bd8ac51 | 2024-01-19 06:38:11 +0100 | [diff] [blame] | 1536 | /* Check for unexpected codec with CSD */ |
Vadim Yanitskiy | b030011 | 2024-02-06 16:58:28 +0700 | [diff] [blame] | 1537 | switch (cc_trans->bearer_cap.transfer) { |
| 1538 | case GSM48_BCAP_ITCAP_FAX_G3: |
| 1539 | case GSM48_BCAP_ITCAP_3k1_AUDIO: |
| 1540 | case GSM48_BCAP_ITCAP_UNR_DIG_INF: |
| 1541 | if (codec_if_known->type == GSM0808_SCT_CSD) |
| 1542 | break; /* we're good */ |
Neels Hofmeyr | bd8ac51 | 2024-01-19 06:38:11 +0100 | [diff] [blame] | 1543 | LOG_TRANS(cc_trans, LOGL_ERROR, "Unexpected codec in Assignment Complete for CSD: %s\n", |
| 1544 | gsm0808_speech_codec_type_name(codec_if_known->type)); |
| 1545 | call_leg_release(msc_a->cc.call_leg); |
| 1546 | return; |
Vadim Yanitskiy | b030011 | 2024-02-06 16:58:28 +0700 | [diff] [blame] | 1547 | default: |
| 1548 | break; |
Neels Hofmeyr | bd8ac51 | 2024-01-19 06:38:11 +0100 | [diff] [blame] | 1549 | } |
| 1550 | |
Neels Hofmeyr | 388d7c9 | 2023-03-22 19:03:15 +0100 | [diff] [blame] | 1551 | /* For 2G: |
| 1552 | * - The Assignment Complete has returned a specific codec (e.g. FR3 for AMR FR). |
| 1553 | * - Set this codec at the MGW endpoint facing the RAN. |
| 1554 | * - Also set this codec at the MGW endpoint facing the CN -- we require an exact match on both call |
| 1555 | * legs. |
| 1556 | * - TODO: be aware of transcoding that the MGW is capable of, e.g. AMR octet-aligned to AMR |
| 1557 | * bandwidth-efficient... |
| 1558 | * |
| 1559 | * For 3G: |
| 1560 | * - ran_infra->force_mgw_codecs_to_ran sets VND.3GPP.IUFP as single codec at the MGW towards RAN. |
| 1561 | * - ran_msg_iu.c always returns FR3 (AMR FR) for the assigned codec. Set that at the MGW towards CN. |
| 1562 | * - So the MGW decapsulates IuUP <-> AMR |
| 1563 | */ |
Neels Hofmeyr | 1a07bcd | 2024-01-19 06:37:57 +0100 | [diff] [blame] | 1564 | codec_assigned = codec_mapping_by_gsm0808_speech_codec_type(codec_if_known->type); |
Neels Hofmeyr | 388d7c9 | 2023-03-22 19:03:15 +0100 | [diff] [blame] | 1565 | /* TODO: use codec_mapping_by_gsm0808_speech_codec() to also match on codec_if_known->cfg */ |
Neels Hofmeyr | 1a07bcd | 2024-01-19 06:37:57 +0100 | [diff] [blame] | 1566 | if (!codec_assigned) { |
Neels Hofmeyr | 388d7c9 | 2023-03-22 19:03:15 +0100 | [diff] [blame] | 1567 | LOG_TRANS(cc_trans, LOGL_ERROR, "Unknown codec in Assignment Complete: %s\n", |
| 1568 | gsm0808_speech_codec_type_name(codec_if_known->type)); |
| 1569 | call_leg_release(msc_a->cc.call_leg); |
| 1570 | return; |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 1571 | } |
| 1572 | |
Neels Hofmeyr | cec51b3 | 2023-03-01 03:47:45 +0100 | [diff] [blame] | 1573 | /* Update RAN-side endpoint CI from Assignment result -- unless it is forced by the ran_infra, in which |
| 1574 | * case it remains unchanged as passed to the earlier call of call_leg_ensure_ci(). */ |
| 1575 | if (msc_a->c.ran->force_mgw_codecs_to_ran.count == 0) |
Neels Hofmeyr | 1a07bcd | 2024-01-19 06:37:57 +0100 | [diff] [blame] | 1576 | rtp_stream_set_one_codec(rtps_to_ran, &codec_assigned->sdp); |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 1577 | |
| 1578 | /* Update codec filter with Assignment result, for the CN side */ |
Neels Hofmeyr | 1a07bcd | 2024-01-19 06:37:57 +0100 | [diff] [blame] | 1579 | cc_trans->cc.codecs.assignment = codec_assigned->sdp; |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 1580 | } else { |
| 1581 | /* No codec passed in Assignment Complete, set 'codecs.assignment' to none. */ |
| 1582 | cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){}; |
| 1583 | LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete without voice codec\n"); |
| 1584 | } |
| 1585 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1586 | rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp); |
Pau Espin Pedrol | a3cdab4 | 2019-05-09 17:54:08 +0200 | [diff] [blame] | 1587 | if (rtps_to_ran->use_osmux) |
| 1588 | rtp_stream_set_remote_osmux_cid(rtps_to_ran, |
| 1589 | ac->assignment_complete.osmux_cid); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1590 | rtp_stream_commit(rtps_to_ran); |
| 1591 | |
Neels Hofmeyr | 2d57d6e | 2022-01-13 21:39:58 +0100 | [diff] [blame] | 1592 | /* Remember the Codec List (BSS Supported) */ |
| 1593 | if (ac->assignment_complete.codec_list_bss_supported) |
| 1594 | codec_filter_set_bss(&cc_trans->cc.codecs, ac->assignment_complete.codec_list_bss_supported); |
| 1595 | |
Oliver Smith | ceca8e6 | 2023-05-24 11:15:52 +0200 | [diff] [blame] | 1596 | trans_cc_filter_run(cc_trans); |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 1597 | LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete: RAN: %s, CN: %s\n", |
| 1598 | sdp_audio_codecs_to_str(&rtps_to_ran->codecs), |
Oliver Smith | c63c3a0 | 2023-05-24 10:48:07 +0200 | [diff] [blame] | 1599 | sdp_audio_codecs_to_str(&cc_trans->cc.local.audio_codecs)); |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 1600 | |
| 1601 | if (cc_on_assignment_done(cc_trans)) { |
| 1602 | /* If an error occurred, it was logged in cc_assignment_done() */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1603 | call_leg_release(msc_a->cc.call_leg); |
| 1604 | return; |
| 1605 | } |
| 1606 | } |
| 1607 | |
Neels Hofmeyr | 22d4f35 | 2024-06-21 19:08:34 +0200 | [diff] [blame] | 1608 | /* Invoked when Assignment has failed, either by a failure response, or by timeout. When failing on timeout, |
| 1609 | * pass af == NULL. */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1610 | static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af) |
| 1611 | { |
| 1612 | struct gsm_trans *trans; |
| 1613 | |
Neels Hofmeyr | 22d4f35 | 2024-06-21 19:08:34 +0200 | [diff] [blame] | 1614 | /* Pending assignment has failed. We're no longer waiting for a response now. */ |
| 1615 | osmo_timer_del(&msc_a->cc.assignment_request_pending); |
| 1616 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1617 | /* For a normal voice call, there will be an rtp_stream FSM. */ |
| 1618 | if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) { |
| 1619 | LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n"); |
| 1620 | rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]); |
| 1621 | return; |
| 1622 | } |
| 1623 | |
Andreas Eversberg | 23b53e5 | 2023-06-21 13:30:58 +0200 | [diff] [blame] | 1624 | /* For a voice group call, release is performed by VGCS FSM */ |
| 1625 | trans = trans_find_by_type(msc_a, TRANS_GCC); |
| 1626 | if (trans) { |
| 1627 | vgcs_vbs_caller_assign_fail(trans); |
| 1628 | return; |
| 1629 | } |
| 1630 | trans = trans_find_by_type(msc_a, TRANS_BCC); |
| 1631 | if (trans) { |
| 1632 | vgcs_vbs_caller_assign_fail(trans); |
| 1633 | return; |
| 1634 | } |
| 1635 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1636 | /* Otherwise, a silent call might be active */ |
| 1637 | trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL); |
| 1638 | if (trans) { |
| 1639 | LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n"); |
| 1640 | trans_free(trans); |
| 1641 | return; |
| 1642 | } |
| 1643 | |
| 1644 | /* Neither a voice call nor silent call assignment. Assume the worst and detach. */ |
| 1645 | msc_a_release_cn(msc_a); |
| 1646 | } |
| 1647 | |
Neels Hofmeyr | 22d4f35 | 2024-06-21 19:08:34 +0200 | [diff] [blame] | 1648 | static void assignment_request_timeout_cb(void *data) |
| 1649 | { |
| 1650 | struct msc_a *msc_a = data; |
| 1651 | msc_a_up_call_assignment_failure(msc_a, NULL); |
| 1652 | } |
| 1653 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1654 | static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark, |
| 1655 | struct osmo_gsm48_classmark *dst) |
| 1656 | { |
| 1657 | if (!dst) { |
| 1658 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
| 1659 | |
| 1660 | if (!vsub) |
| 1661 | dst = &msc_a->temporary_classmark; |
| 1662 | else |
| 1663 | dst = &vsub->classmark; |
| 1664 | } |
| 1665 | |
Martin Hauke | 3f07dac | 2019-11-14 17:49:08 +0100 | [diff] [blame] | 1666 | LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n", |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1667 | osmo_gsm48_classmark_a5_name(classmark)); |
| 1668 | osmo_gsm48_classmark_update(dst, classmark); |
| 1669 | |
| 1670 | /* bump subscr conn FSM in case it is waiting for a Classmark Update */ |
| 1671 | if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE) |
| 1672 | osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL); |
| 1673 | } |
| 1674 | |
| 1675 | static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg) |
| 1676 | { |
| 1677 | int sapi = msg->sapi_n_reject.dlci & 0x7; |
| 1678 | if (sapi == UM_SAPI_SMS) |
| 1679 | gsm411_sapi_n_reject(msc_a); |
| 1680 | } |
| 1681 | |
| 1682 | static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event) |
| 1683 | { |
| 1684 | if (!msc_a->ho.fi) { |
| 1685 | LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name); |
| 1686 | return -EINVAL; |
| 1687 | } |
| 1688 | return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d); |
| 1689 | } |
| 1690 | |
| 1691 | int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d) |
| 1692 | { |
| 1693 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
Alexander Couzens | 2aaff75 | 2021-10-19 17:09:11 +0200 | [diff] [blame] | 1694 | struct gsm_network *net = msc_a_net(msc_a); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1695 | const struct ran_msg *msg = d->ran_dec; |
| 1696 | int rc = -99; |
| 1697 | |
| 1698 | switch (msg->msg_type) { |
| 1699 | |
| 1700 | case RAN_MSG_COMPL_L3: |
Neels Hofmeyr | 68f50da | 2020-06-24 14:22:52 +0200 | [diff] [blame] | 1701 | /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code |
| 1702 | * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1703 | msc_a->via_cell = (struct osmo_cell_global_id){ |
| 1704 | .lai.plmn = msc_a_net(msc_a)->plmn, |
| 1705 | }; |
| 1706 | gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id); |
Neels Hofmeyr | ede9083 | 2022-01-13 18:13:15 +0100 | [diff] [blame] | 1707 | |
Neels Hofmeyr | e276ae9 | 2022-01-13 21:38:35 +0100 | [diff] [blame] | 1708 | /* If a codec list was sent along in the RAN_MSG_COMPL_L3, remember it for any upcoming codec |
| 1709 | * resolution. */ |
| 1710 | if (msg->compl_l3.codec_list_bss_supported) { |
| 1711 | msc_a->cc.compl_l3_codec_list_bss_supported = *msg->compl_l3.codec_list_bss_supported; |
| 1712 | if (log_check_level(msc_a->c.ran->log_subsys, LOGL_DEBUG)) { |
| 1713 | struct sdp_audio_codecs ac = {}; |
| 1714 | sdp_audio_codecs_from_speech_codec_list(&ac, &msc_a->cc.compl_l3_codec_list_bss_supported); |
| 1715 | LOG_MSC_A(msc_a, LOGL_DEBUG, "Complete Layer 3: Codec List (BSS Supported): %s\n", |
| 1716 | sdp_audio_codecs_to_str(&ac)); |
| 1717 | } |
| 1718 | } |
| 1719 | |
Neels Hofmeyr | ede9083 | 2022-01-13 18:13:15 +0100 | [diff] [blame] | 1720 | /* Submit the Complete Layer 3 Information DTAP */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1721 | rc = msc_a_up_l3(msc_a, msg->compl_l3.msg); |
| 1722 | if (!rc) { |
| 1723 | struct ran_conn *conn = msub_ran_conn(msc_a->c.msub); |
| 1724 | if (conn) |
| 1725 | ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id); |
| 1726 | } |
| 1727 | break; |
| 1728 | |
| 1729 | case RAN_MSG_DTAP: |
| 1730 | rc = msc_a_up_l3(msc_a, msg->dtap); |
| 1731 | break; |
| 1732 | |
| 1733 | case RAN_MSG_CLEAR_REQUEST: |
| 1734 | rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL); |
| 1735 | break; |
| 1736 | |
| 1737 | case RAN_MSG_CLEAR_COMPLETE: |
| 1738 | switch (msc_a->c.fi->state) { |
| 1739 | case MSC_A_ST_RELEASING: |
| 1740 | msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE); |
| 1741 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASED); |
| 1742 | break; |
| 1743 | case MSC_A_ST_RELEASED: |
| 1744 | break; |
| 1745 | default: |
| 1746 | LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n"); |
| 1747 | msc_a_state_chg(msc_a, MSC_A_ST_RELEASING); |
| 1748 | break; |
| 1749 | } |
| 1750 | rc = 0; |
| 1751 | break; |
| 1752 | |
| 1753 | case RAN_MSG_CLASSMARK_UPDATE: |
| 1754 | msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL); |
| 1755 | rc = 0; |
| 1756 | break; |
| 1757 | |
| 1758 | case RAN_MSG_CIPHER_MODE_COMPLETE: |
| 1759 | /* Remember what Ciphering was negotiated (e.g. for Handover) */ |
| 1760 | if (msg->cipher_mode_complete.alg_id) { |
| 1761 | msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id; |
| 1762 | LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n", |
| 1763 | msc_a->geran_encr.alg_id - 1); |
Alexander Couzens | 2aaff75 | 2021-10-19 17:09:11 +0200 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU) { |
| 1767 | int16_t utran_encryption; |
| 1768 | |
| 1769 | /* utran: ensure chosen ciphering mode is allowed |
| 1770 | * If the IE is missing (utran_encryption == -1), parse it as no encryption */ |
| 1771 | utran_encryption = msg->cipher_mode_complete.utran_encryption; |
| 1772 | if (utran_encryption == -1) |
| 1773 | utran_encryption = 0; |
| 1774 | if ((net->uea_encryption_mask & (1 << utran_encryption)) == 0) { |
| 1775 | /* cipher disallowed */ |
| 1776 | LOG_MSC_A(msc_a, LOGL_ERROR, "Cipher Mode Complete: RNC chosen forbidden ciphering UEA%d\n", |
| 1777 | msg->cipher_mode_complete.utran_encryption); |
| 1778 | vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT); |
| 1779 | rc = 0; |
| 1780 | break; |
| 1781 | } |
| 1782 | } |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1783 | vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL); |
| 1784 | rc = 0; |
Neels Hofmeyr | e9a3911 | 2019-08-29 00:10:49 +0200 | [diff] [blame] | 1785 | |
| 1786 | /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */ |
| 1787 | if (msg->cipher_mode_complete.l3_msg) { |
| 1788 | unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val); |
| 1789 | uint16_t len = msg->cipher_mode_complete.l3_msg->len; |
| 1790 | struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete"); |
| 1791 | unsigned char *pos = msgb_put(dtap, len); |
| 1792 | memcpy(pos, data, len); |
| 1793 | dtap->l3h = pos; |
| 1794 | rc = msc_a_up_l3(msc_a, dtap); |
| 1795 | msgb_free(dtap); |
| 1796 | } |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1797 | break; |
| 1798 | |
| 1799 | case RAN_MSG_CIPHER_MODE_REJECT: |
| 1800 | vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT); |
| 1801 | rc = 0; |
| 1802 | break; |
| 1803 | |
| 1804 | case RAN_MSG_ASSIGNMENT_COMPLETE: |
| 1805 | msc_a_up_call_assignment_complete(msc_a, msg); |
| 1806 | rc = 0; |
| 1807 | break; |
| 1808 | |
| 1809 | case RAN_MSG_ASSIGNMENT_FAILURE: |
| 1810 | msc_a_up_call_assignment_failure(msc_a, msg); |
| 1811 | rc = 0; |
| 1812 | break; |
| 1813 | |
| 1814 | case RAN_MSG_SAPI_N_REJECT: |
| 1815 | msc_a_up_sapi_n_reject(msc_a, msg); |
| 1816 | rc = 0; |
| 1817 | break; |
| 1818 | |
| 1819 | case RAN_MSG_HANDOVER_PERFORMED: |
| 1820 | /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */ |
| 1821 | LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n"); |
| 1822 | break; |
| 1823 | |
| 1824 | case RAN_MSG_HANDOVER_REQUIRED: |
| 1825 | /* The BSS lets us know that it wants to handover to a different cell */ |
| 1826 | rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required); |
| 1827 | break; |
| 1828 | |
| 1829 | case RAN_MSG_HANDOVER_FAILURE: |
| 1830 | rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE); |
| 1831 | break; |
| 1832 | |
Keith Whyte | a1a70be | 2021-05-16 02:59:52 +0200 | [diff] [blame] | 1833 | case RAN_MSG_LCLS_STATUS: |
| 1834 | /* The BSS sends us LCLS_STATUS. We do nothing for now, but it is not an error. */ |
| 1835 | LOG_MSC_A(msc_a, LOGL_DEBUG, "LCLS_STATUS (%s) received from MSC-I\n", |
| 1836 | gsm0808_lcls_status_name(msg->lcls_status.status)); |
| 1837 | rc = 0; |
| 1838 | break; |
| 1839 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1840 | default: |
| 1841 | LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type)); |
| 1842 | rc = -ENOTSUP; |
| 1843 | break; |
| 1844 | } |
| 1845 | return rc; |
| 1846 | } |
| 1847 | |
Andreas Eversberg | f7c6f14 | 2023-04-23 12:10:42 +0200 | [diff] [blame] | 1848 | static int msc_a_rx_vgcs_bss_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg) |
| 1849 | { |
| 1850 | struct vgcs_bss *bss = caller_data; |
| 1851 | struct msc_a *msc_a = (bss->trans) ? bss->trans->msc_a : NULL; |
| 1852 | int rc = 0; |
| 1853 | |
| 1854 | switch (msg->msg_type) { |
| 1855 | case RAN_MSG_VGCS_VBS_SETUP_ACK: |
| 1856 | /* The BSS accepts VGCS/VBS and sends us supported features. */ |
| 1857 | vgcs_vbs_setup_ack(bss, msg); |
| 1858 | break; |
| 1859 | case RAN_MSG_VGCS_VBS_SETUP_REFUSE: |
| 1860 | /* The BSS refuses VGCS/VBS. */ |
| 1861 | vgcs_vbs_setup_refuse(bss, msg); |
| 1862 | break; |
| 1863 | case RAN_MSG_UPLINK_REQUEST: |
| 1864 | /* A mobile station requests the uplink on a VGCS channel. */ |
| 1865 | vgcs_uplink_request(bss, msg); |
| 1866 | break; |
| 1867 | case RAN_MSG_UPLINK_REQUEST_CNF: |
| 1868 | /* The uplink on a VGCS channel has been established. */ |
| 1869 | vgcs_uplink_request_cnf(bss, msg); |
| 1870 | break; |
| 1871 | case RAN_MSG_UPLINK_APPLICATION_DATA: |
| 1872 | /* Application data received on the uplink of a VGCS channel. */ |
| 1873 | vgcs_app_data(bss, msg); |
| 1874 | break; |
| 1875 | case RAN_MSG_DTAP: |
| 1876 | /* BSS confirms the release of the channel. */ |
| 1877 | vgcs_bss_dtap(bss, msg); |
| 1878 | break; |
| 1879 | case RAN_MSG_UPLINK_RELEASE_IND: |
| 1880 | /* A mobile station releases the uplink on a VGCS channel. */ |
| 1881 | vgcs_uplink_release_ind(bss, msg); |
| 1882 | break; |
| 1883 | case RAN_MSG_CLEAR_REQUEST: |
| 1884 | /* BSS indicated that the channel has been released. */ |
| 1885 | vgcs_vbs_clear_req(bss, msg); |
| 1886 | break; |
| 1887 | case RAN_MSG_CLEAR_COMPLETE: |
| 1888 | /* BSS confirms the release of the channel. */ |
| 1889 | vgcs_vbs_clear_cpl(bss, msg); |
| 1890 | break; |
| 1891 | default: |
| 1892 | LOG_MSC_A(msc_a, LOGL_ERROR, "VGCS message from BSS not implemented: %s\n", |
| 1893 | ran_msg_type_name(msg->msg_type)); |
| 1894 | rc = -ENOTSUP; |
| 1895 | break; |
| 1896 | } |
| 1897 | return rc; |
| 1898 | } |
| 1899 | |
| 1900 | int msc_a_rx_vgcs_bss(struct vgcs_bss *bss, struct ran_conn *from_conn, struct msgb *msg) |
| 1901 | { |
| 1902 | struct ran_dec ran_dec; |
| 1903 | |
| 1904 | /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */ |
| 1905 | ran_dec = (struct ran_dec) { |
| 1906 | .caller_data = bss, |
| 1907 | .decode_cb = msc_a_rx_vgcs_bss_decoded, |
| 1908 | }; |
| 1909 | struct ran_peer *ran_peer = from_conn->ran_peer; |
| 1910 | struct ran_infra *ran = ran_peer->sri->ran; |
| 1911 | if (!ran->ran_dec_l2) { |
| 1912 | LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n", |
| 1913 | osmo_rat_type_name(ran->type)); |
| 1914 | return -ENOTSUP; |
| 1915 | } |
| 1916 | return ran->ran_dec_l2(&ran_dec, msg); |
| 1917 | } |
| 1918 | |
| 1919 | static int msc_a_rx_vgcs_cell_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg) |
| 1920 | { |
| 1921 | struct vgcs_bss_cell *cell = caller_data; |
| 1922 | struct msc_a *msc_a = (cell->bss && cell->bss->trans) ? cell->bss->trans->msc_a : NULL; |
| 1923 | int rc = 0; |
| 1924 | |
| 1925 | switch (msg->msg_type) { |
| 1926 | case RAN_MSG_VGCS_VBS_ASSIGN_RES: |
| 1927 | /* The BSS accepts VGCS/VBS channel assignment. */ |
| 1928 | vgcs_vbs_assign_result(cell, msg); |
| 1929 | break; |
| 1930 | case RAN_MSG_VGCS_VBS_ASSIGN_FAIL: |
| 1931 | /* The BSS refuses VGCS/VBS channel assignment. */ |
| 1932 | vgcs_vbs_assign_fail(cell, msg); |
| 1933 | break; |
| 1934 | case RAN_MSG_VGCS_VBS_QUEUING_IND: |
| 1935 | /* The BSS needs more time for VGCS/VBS channel assignment. */ |
| 1936 | vgcs_vbs_queuing_ind(cell); |
| 1937 | break; |
| 1938 | case RAN_MSG_VGCS_VBS_ASSIGN_STATUS: |
| 1939 | /* The BSS gives cell status about VGCS/VBS channel. */ |
| 1940 | vgcs_vbs_assign_status(cell, msg); |
| 1941 | break; |
| 1942 | case RAN_MSG_CLEAR_REQUEST: |
| 1943 | /* BSS indicated that the channel has been released. */ |
| 1944 | vgcs_vbs_clear_req_channel(cell, msg); |
| 1945 | break; |
| 1946 | case RAN_MSG_CLEAR_COMPLETE: |
| 1947 | /* BSS confirms the release of the channel. */ |
| 1948 | vgcs_vbs_clear_cpl_channel(cell, msg); |
| 1949 | break; |
| 1950 | default: |
| 1951 | LOG_MSC_A(msc_a, LOGL_ERROR, "Message from BSS leg not implemented: %s\n", |
| 1952 | ran_msg_type_name(msg->msg_type)); |
| 1953 | rc = -ENOTSUP; |
| 1954 | break; |
| 1955 | } |
| 1956 | return rc; |
| 1957 | } |
| 1958 | |
| 1959 | int msc_a_rx_vgcs_cell(struct vgcs_bss_cell *cell, struct ran_conn *from_conn, struct msgb *msg) |
| 1960 | { |
| 1961 | struct ran_dec ran_dec; |
| 1962 | |
| 1963 | /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */ |
| 1964 | ran_dec = (struct ran_dec) { |
| 1965 | .caller_data = cell, |
| 1966 | .decode_cb = msc_a_rx_vgcs_cell_decoded, |
| 1967 | }; |
| 1968 | struct ran_peer *ran_peer = from_conn->ran_peer; |
| 1969 | struct ran_infra *ran = ran_peer->sri->ran; |
| 1970 | if (!ran->ran_dec_l2) { |
| 1971 | LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n", |
| 1972 | osmo_rat_type_name(ran->type)); |
| 1973 | return -ENOTSUP; |
| 1974 | } |
| 1975 | return ran->ran_dec_l2(&ran_dec, msg); |
| 1976 | } |
| 1977 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1978 | static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d) |
| 1979 | { |
| 1980 | struct msc_t *msc_t = msc_a_msc_t(msc_a); |
| 1981 | int rc = -99; |
| 1982 | |
| 1983 | if (!msc_t) { |
| 1984 | LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n"); |
| 1985 | return -EINVAL; |
| 1986 | } |
| 1987 | |
| 1988 | OSMO_ASSERT(d->ran_dec); |
| 1989 | |
| 1990 | switch (d->ran_dec->msg_type) { |
| 1991 | |
| 1992 | case RAN_MSG_CLEAR_REQUEST: |
| 1993 | rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL); |
| 1994 | break; |
| 1995 | |
| 1996 | case RAN_MSG_CLEAR_COMPLETE: |
| 1997 | rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL); |
| 1998 | break; |
| 1999 | |
| 2000 | case RAN_MSG_CLASSMARK_UPDATE: |
| 2001 | msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark); |
| 2002 | rc = 0; |
| 2003 | break; |
| 2004 | |
| 2005 | case RAN_MSG_HANDOVER_REQUEST_ACK: |
| 2006 | /* new BSS accepts Handover */ |
| 2007 | rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK); |
| 2008 | break; |
| 2009 | |
| 2010 | case RAN_MSG_HANDOVER_DETECT: |
| 2011 | /* new BSS signals the MS is DETECTed on the new lchan */ |
| 2012 | rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT); |
| 2013 | break; |
| 2014 | |
| 2015 | case RAN_MSG_HANDOVER_COMPLETE: |
| 2016 | /* new BSS signals the MS has fully moved to the new lchan */ |
| 2017 | rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE); |
| 2018 | break; |
| 2019 | |
| 2020 | case RAN_MSG_HANDOVER_FAILURE: |
| 2021 | rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE); |
| 2022 | break; |
| 2023 | |
| 2024 | default: |
| 2025 | LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n", |
| 2026 | ran_msg_type_name(d->ran_dec->msg_type)); |
| 2027 | rc = -ENOTSUP; |
| 2028 | break; |
| 2029 | } |
| 2030 | return rc; |
| 2031 | } |
| 2032 | |
| 2033 | int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg) |
| 2034 | { |
| 2035 | struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi); |
| 2036 | struct msc_a_ran_dec_data *d = data; |
| 2037 | int rc = -99; |
| 2038 | |
| 2039 | d->ran_dec = msg; |
| 2040 | |
| 2041 | switch (d->from_role) { |
| 2042 | case MSC_ROLE_I: |
| 2043 | LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type)); |
| 2044 | rc = msc_a_ran_dec_from_msc_i(msc_a, d); |
| 2045 | break; |
| 2046 | |
| 2047 | case MSC_ROLE_T: |
| 2048 | LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n", |
| 2049 | msg->msg_name ? : ran_msg_type_name(msg->msg_type)); |
| 2050 | rc = msc_a_ran_dec_from_msc_t(msc_a, d); |
| 2051 | break; |
| 2052 | |
| 2053 | default: |
| 2054 | LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role), |
| 2055 | ran_msg_type_name(msg->msg_type)); |
| 2056 | return -ENOTSUP; |
| 2057 | } |
| 2058 | |
| 2059 | if (rc) |
| 2060 | LOG_MSC_A(msc_a, LOGL_ERROR, "RAN decode error (rc=%d) for %s from %s\n", rc, ran_msg_type_name(msg->msg_type), |
| 2061 | msc_role_name(d->from_role)); |
| 2062 | return rc; |
| 2063 | } |
| 2064 | |
| 2065 | /* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */ |
| 2066 | int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg, |
| 2067 | const char *file, int line) |
| 2068 | { |
| 2069 | return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role), |
| 2070 | ran_msg, file, line); |
| 2071 | } |
| 2072 | |
| 2073 | /* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an |
| 2074 | * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */ |
| 2075 | int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event, |
| 2076 | const struct ran_msg *ran_msg, |
| 2077 | const char *file, int line) |
| 2078 | { |
| 2079 | struct an_apdu an_apdu = { |
| 2080 | .an_proto = msc_a->c.ran->an_proto, |
| 2081 | .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg), |
| 2082 | }; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2083 | if (!an_apdu.msg) |
| 2084 | return -EIO; |
Vadim Yanitskiy | c44342b | 2021-12-07 18:32:35 +0300 | [diff] [blame] | 2085 | return _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap) |
| 2089 | { |
| 2090 | struct ran_msg ran_msg; |
Neels Hofmeyr | c192c0b | 2019-10-07 21:41:18 +0200 | [diff] [blame] | 2091 | struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data; |
| 2092 | uint8_t pdisc = gsm48_hdr_pdisc(gh); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2093 | |
Neels Hofmeyr | 2e8f881 | 2019-08-21 16:56:41 +0200 | [diff] [blame] | 2094 | if (!msc_a) { |
Neels Hofmeyr | 2e8f881 | 2019-08-21 16:56:41 +0200 | [diff] [blame] | 2095 | LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n", |
| 2096 | gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh))); |
| 2097 | msgb_free(dtap); |
| 2098 | return -EIO; |
| 2099 | } |
| 2100 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2101 | if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) { |
| 2102 | /* The SGs connection to the MME always is at the MSC-A. */ |
| 2103 | return sgs_iface_tx_dtap_ud(msc_a, dtap); |
| 2104 | } |
| 2105 | |
Neels Hofmeyr | c192c0b | 2019-10-07 21:41:18 +0200 | [diff] [blame] | 2106 | LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n", |
| 2107 | gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh))); |
| 2108 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2109 | ran_msg = (struct ran_msg){ |
| 2110 | .msg_type = RAN_MSG_DTAP, |
| 2111 | .dtap = dtap, |
| 2112 | }; |
| 2113 | return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg); |
| 2114 | } |
| 2115 | |
| 2116 | struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only) |
| 2117 | { |
| 2118 | struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub)); |
| 2119 | if (valid_conn_only && !msc_a_is_accepted(msc_a)) |
| 2120 | return NULL; |
| 2121 | return msc_a; |
| 2122 | } |
| 2123 | |
| 2124 | int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role) |
| 2125 | { |
| 2126 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
Vadim Yanitskiy | 435f67f | 2021-06-06 15:47:49 +0200 | [diff] [blame] | 2127 | if (vsub == NULL) |
| 2128 | return -ENODEV; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2129 | struct ran_msg msg = { |
| 2130 | .msg_type = RAN_MSG_COMMON_ID, |
| 2131 | .common_id = { |
| 2132 | .imsi = vsub->imsi, |
Pau Espin Pedrol | 6710670 | 2021-04-27 18:20:15 +0200 | [diff] [blame] | 2133 | .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2134 | }, |
| 2135 | }; |
Pau Espin Pedrol | 6710670 | 2021-04-27 18:20:15 +0200 | [diff] [blame] | 2136 | if (vsub->sgs.last_eutran_plmn_present) { |
| 2137 | memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn, |
| 2138 | sizeof(vsub->sgs.last_eutran_plmn)); |
| 2139 | } |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2140 | |
| 2141 | return msc_a_ran_down(msc_a, to_role, &msg); |
| 2142 | } |
| 2143 | |
| 2144 | static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans) |
| 2145 | { |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 2146 | struct call_leg *cl; |
| 2147 | bool cn_rtp_available; |
| 2148 | bool ran_rtp_available; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2149 | |
| 2150 | OSMO_ASSERT(!msc_a->cc.active_trans); |
| 2151 | msc_a->cc.active_trans = cc_trans; |
| 2152 | |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 2153 | cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){}; |
| 2154 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2155 | OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC); |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 2156 | cl = msc_a_ensure_call_leg(msc_a, cc_trans); |
| 2157 | if (!cl) |
| 2158 | return -EINVAL; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2159 | |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 2160 | /* See if we can set a preliminary codec. If not, pass none for the time being. */ |
Oliver Smith | ceca8e6 | 2023-05-24 11:15:52 +0200 | [diff] [blame] | 2161 | trans_cc_filter_run(cc_trans); |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 2162 | |
| 2163 | cn_rtp_available = call_leg_local_ip(cl, RTP_TO_CN); |
| 2164 | ran_rtp_available = call_leg_local_ip(cl, RTP_TO_RAN); |
| 2165 | |
| 2166 | /* Set up RTP ports for both RAN and CN side. Even though we ask for both at the same time, the |
| 2167 | * osmo_mgcpc_ep_fsm automagically waits for the first CRCX to complete before firing the second CRCX. The one |
| 2168 | * issued first here will also be the first CRCX sent to the MGW. Usually both still need to be set up. */ |
| 2169 | if (!cn_rtp_available) |
Andreas Eversberg | 712b28e | 2023-06-21 11:17:26 +0200 | [diff] [blame] | 2170 | call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans, |
Oliver Smith | c63c3a0 | 2023-05-24 10:48:07 +0200 | [diff] [blame] | 2171 | &cc_trans->cc.local.audio_codecs, NULL); |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 2172 | if (!ran_rtp_available) { |
| 2173 | struct sdp_audio_codecs *codecs; |
| 2174 | if (msc_a->c.ran->force_mgw_codecs_to_ran.count) |
| 2175 | codecs = &msc_a->c.ran->force_mgw_codecs_to_ran; |
| 2176 | else |
Oliver Smith | c63c3a0 | 2023-05-24 10:48:07 +0200 | [diff] [blame] | 2177 | codecs = &cc_trans->cc.local.audio_codecs; |
Andreas Eversberg | 712b28e | 2023-06-21 11:17:26 +0200 | [diff] [blame] | 2178 | return call_leg_ensure_ci(cl, RTP_TO_RAN, cc_trans->call_id, cc_trans, codecs, NULL); |
Neels Hofmeyr | bd5f8e9 | 2022-01-13 23:18:02 +0100 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | /* Should these already be set up, immediately continue by retriggering the events signalling that the RTP |
| 2182 | * ports are available. The ordering is: first CN, then RAN. */ |
| 2183 | if (cn_rtp_available && ran_rtp_available) |
| 2184 | return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]); |
| 2185 | else if (cn_rtp_available) |
| 2186 | return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]); |
| 2187 | /* Otherwise wait for MGCP response and continue from there. */ |
| 2188 | return 0; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | int msc_a_try_call_assignment(struct gsm_trans *cc_trans) |
| 2192 | { |
| 2193 | struct msc_a *msc_a = cc_trans->msc_a; |
| 2194 | OSMO_ASSERT(cc_trans->type == TRANS_CC); |
| 2195 | |
| 2196 | if (msc_a->cc.active_trans == cc_trans) { |
Neels Hofmeyr | b4ef5e7 | 2019-08-30 01:11:12 +0200 | [diff] [blame] | 2197 | LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n"); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2198 | return 0; |
| 2199 | } |
| 2200 | |
| 2201 | if (msc_a->cc.active_trans) { |
| 2202 | LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n"); |
| 2203 | return 0; |
| 2204 | } |
| 2205 | |
| 2206 | LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n"); |
| 2207 | return msc_a_start_assignment(msc_a, cc_trans); |
| 2208 | } |
| 2209 | |
Neels Hofmeyr | 5d53c60 | 2022-04-24 23:37:07 +0200 | [diff] [blame] | 2210 | /* Map CM Service type to use token. |
| 2211 | * Given a CM Service type, return a matching token intended for osmo_use_count. |
| 2212 | * For unknown service type, return NULL. |
| 2213 | */ |
Andreas Eversberg | cd8bd45 | 2023-07-11 14:16:24 +0200 | [diff] [blame] | 2214 | const char *msc_a_cm_service_type_to_use(struct msc_a *msc_a, enum osmo_cm_service_type cm_service_type) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2215 | { |
Andreas Eversberg | cd8bd45 | 2023-07-11 14:16:24 +0200 | [diff] [blame] | 2216 | struct gsm_network *net = msc_a_net(msc_a); |
| 2217 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2218 | switch (cm_service_type) { |
| 2219 | case GSM48_CMSERV_MO_CALL_PACKET: |
| 2220 | case GSM48_CMSERV_EMERGENCY: |
| 2221 | return MSC_A_USE_CM_SERVICE_CC; |
| 2222 | |
| 2223 | case GSM48_CMSERV_SMS: |
| 2224 | return MSC_A_USE_CM_SERVICE_SMS; |
| 2225 | |
| 2226 | case GSM48_CMSERV_SUP_SERV: |
| 2227 | return MSC_A_USE_CM_SERVICE_SS; |
| 2228 | |
Andreas Eversberg | 456c6f7 | 2023-04-23 11:54:16 +0200 | [diff] [blame] | 2229 | case GSM48_CMSERV_VGCS: |
Andreas Eversberg | cd8bd45 | 2023-07-11 14:16:24 +0200 | [diff] [blame] | 2230 | if (net->asci.enable) |
| 2231 | return MSC_A_USE_CM_SERVICE_GCC; |
| 2232 | else |
| 2233 | return NULL; |
Andreas Eversberg | 456c6f7 | 2023-04-23 11:54:16 +0200 | [diff] [blame] | 2234 | |
| 2235 | case GSM48_CMSERV_VBS: |
Andreas Eversberg | cd8bd45 | 2023-07-11 14:16:24 +0200 | [diff] [blame] | 2236 | if (net->asci.enable) |
| 2237 | return MSC_A_USE_CM_SERVICE_BCC; |
| 2238 | else |
| 2239 | return NULL; |
Andreas Eversberg | 456c6f7 | 2023-04-23 11:54:16 +0200 | [diff] [blame] | 2240 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 2241 | default: |
| 2242 | return NULL; |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | void msc_a_release_cn(struct msc_a *msc_a) |
| 2247 | { |
| 2248 | osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL); |
| 2249 | } |
| 2250 | |
| 2251 | void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause) |
| 2252 | { |
| 2253 | osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL); |
| 2254 | } |