Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1 | /* MSC Handover implementation */ |
| 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 | * Author: Neels Hofmeyr |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 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 General Public License for more details. |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <osmocom/core/fsm.h> |
| 22 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 23 | #include <osmocom/sigtran/sccp_helpers.h> |
| 24 | |
| 25 | #include <osmocom/msc/msc_ho.h> |
| 26 | #include <osmocom/msc/ran_msg.h> |
| 27 | #include <osmocom/msc/msc_a.h> |
| 28 | #include <osmocom/msc/msc_i.h> |
| 29 | #include <osmocom/msc/msc_t.h> |
| 30 | #include <osmocom/msc/e_link.h> |
| 31 | #include <osmocom/msc/msc_i_remote.h> |
| 32 | #include <osmocom/msc/msc_t_remote.h> |
| 33 | #include <osmocom/msc/neighbor_ident.h> |
| 34 | #include <osmocom/msc/gsm_data.h> |
| 35 | #include <osmocom/msc/ran_peer.h> |
| 36 | #include <osmocom/msc/vlr.h> |
| 37 | #include <osmocom/msc/transaction.h> |
| 38 | #include <osmocom/msc/gsm_04_08.h> |
| 39 | #include <osmocom/msc/call_leg.h> |
| 40 | #include <osmocom/msc/rtp_stream.h> |
| 41 | #include <osmocom/msc/mncc_call.h> |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 42 | #include <osmocom/msc/codec_mapping.h> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 43 | |
| 44 | struct osmo_fsm msc_ho_fsm; |
| 45 | |
| 46 | #define MSC_A_USE_HANDOVER "Handover" |
| 47 | |
| 48 | static const struct osmo_tdef_state_timeout msc_ho_fsm_timeouts[32] = { |
| 49 | [MSC_HO_ST_REQUIRED] = { .keep_timer = true, .T = -3 }, |
| 50 | [MSC_HO_ST_WAIT_REQUEST_ACK] = { .keep_timer = true }, |
| 51 | [MSC_HO_ST_WAIT_COMPLETE] = { .T = -3 }, |
| 52 | }; |
| 53 | |
| 54 | /* Transition to a state, using the T timer defined in msc_a_fsm_timeouts. |
| 55 | * The actual timeout value is in turn obtained from network->T_defs. |
| 56 | * Assumes local variable fi exists. */ |
| 57 | #define msc_ho_fsm_state_chg(msc_a, state) \ |
| 58 | osmo_tdef_fsm_inst_state_chg((msc_a)->ho.fi, state, msc_ho_fsm_timeouts, (msc_a)->c.ran->tdefs, 5) |
| 59 | |
| 60 | static __attribute__((constructor)) void msc_ho_fsm_init() |
| 61 | { |
Harald Welte | 34a8cc3 | 2019-12-01 15:32:09 +0100 | [diff] [blame] | 62 | OSMO_ASSERT(osmo_fsm_register(&msc_ho_fsm) == 0); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void msc_ho_down_required_reject(struct msc_a *msc_a, enum gsm0808_cause cause) |
| 66 | { |
Vadim Yanitskiy | db4839c | 2019-12-01 18:52:58 +0700 | [diff] [blame] | 67 | struct msc_i *msc_i; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 68 | uint32_t event; |
| 69 | |
Vadim Yanitskiy | db4839c | 2019-12-01 18:52:58 +0700 | [diff] [blame] | 70 | msc_i = msc_a_msc_i(msc_a); |
| 71 | OSMO_ASSERT(msc_i); |
| 72 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 73 | struct ran_msg ran_enc_msg = { |
| 74 | .msg_type = RAN_MSG_HANDOVER_REQUIRED_REJECT, |
| 75 | .handover_required_reject = { |
| 76 | .cause = cause, |
| 77 | }, |
| 78 | }; |
| 79 | |
| 80 | if (msc_i->c.remote_to) |
| 81 | event = MSC_I_EV_FROM_A_PREPARE_SUBSEQUENT_HANDOVER_ERROR; |
| 82 | else |
| 83 | event = MSC_I_EV_FROM_A_FORWARD_ACCESS_SIGNALLING_REQUEST; |
| 84 | |
| 85 | msc_a_msg_down(msc_a, MSC_ROLE_I, event, &ran_enc_msg); |
| 86 | } |
| 87 | |
| 88 | /* Even though this is using the 3GPP TS 48.008 definitions and naming, the intention is to be RAN implementation agnostic. |
| 89 | * For other RAN types, the 48.008 items shall be translated to their respective counterparts. */ |
| 90 | void msc_ho_start(struct msc_a *msc_a, const struct ran_handover_required *ho_req) |
| 91 | { |
| 92 | if (msc_a->ho.fi) { |
| 93 | LOG_HO(msc_a, LOGL_ERROR, "Rx Handover Required, but Handover is still ongoing\n"); |
| 94 | msc_ho_down_required_reject(msc_a, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | if (!ho_req->cil.id_list_len) { |
| 99 | LOG_HO(msc_a, LOGL_ERROR, "Rx Handover Required without a Cell Identifier List\n"); |
| 100 | msc_ho_down_required_reject(msc_a, GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (msc_a_msc_t(msc_a)) { |
| 105 | LOG_HO(msc_a, LOGL_ERROR, |
| 106 | "Rx Handover Required, but this subscriber still has an active MSC-T role: %s\n", |
| 107 | msc_a_msc_t(msc_a)->c.fi->id); |
| 108 | /* Protocol error because the BSS is not supposed to send another Handover Required before the previous |
| 109 | * attempt has concluded. */ |
| 110 | msc_ho_down_required_reject(msc_a, GSM0808_CAUSE_PROTOCOL_ERROR_BETWEEN_BSS_AND_MSC); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | /* Paranoia: make sure we start with clean state */ |
| 115 | msc_a->ho = (struct msc_ho_state){}; |
| 116 | |
| 117 | msc_a->ho.fi = osmo_fsm_inst_alloc_child(&msc_ho_fsm, msc_a->c.fi, MSC_A_EV_HANDOVER_END); |
| 118 | OSMO_ASSERT(msc_a->ho.fi); |
| 119 | |
| 120 | msc_a->ho.fi->priv = msc_a; |
| 121 | msc_a->ho.info = *ho_req; |
| 122 | msc_a->ho.next_cil_idx = 0; |
| 123 | |
| 124 | /* Start the timeout */ |
| 125 | msc_ho_fsm_state_chg(msc_a, MSC_HO_ST_REQUIRED); |
| 126 | } |
| 127 | |
| 128 | static void msc_ho_rtp_rollback_to_old_cell(struct msc_a *msc_a); |
| 129 | |
| 130 | static void msc_ho_end(struct msc_a *msc_a, bool success, enum gsm0808_cause cause) |
| 131 | { |
| 132 | struct msc_i *msc_i; |
| 133 | struct msc_t *msc_t = msc_a_msc_t(msc_a); |
| 134 | |
| 135 | if (!success) { |
| 136 | msc_ho_rtp_rollback_to_old_cell(msc_a); |
| 137 | msc_ho_down_required_reject(msc_a, cause); |
| 138 | } |
| 139 | |
| 140 | if (success) { |
| 141 | /* Any previous call forwarding to a remote MSC becomes obsolete. */ |
| 142 | if (msc_a->cc.mncc_forwarding_to_remote_ran) { |
| 143 | mncc_call_release(msc_a->cc.mncc_forwarding_to_remote_ran); |
| 144 | msc_a->cc.mncc_forwarding_to_remote_ran = NULL; |
| 145 | } |
| 146 | |
| 147 | /* Replace MSC-I with new MSC-T */ |
| 148 | if (msc_t->c.remote_to) { |
| 149 | /* Inter-MSC Handover. */ |
| 150 | |
| 151 | /* The MNCC forwarding set up for inter-MSC handover, so far transitional in msc_a->ho now |
| 152 | * becomes the "officially" active MNCC forwarding for this call. */ |
| 153 | msc_a->cc.mncc_forwarding_to_remote_ran = msc_a->ho.new_cell.mncc_forwarding_to_remote_ran; |
| 154 | msc_a->ho.new_cell.mncc_forwarding_to_remote_ran = NULL; |
| 155 | mncc_call_reparent(msc_a->cc.mncc_forwarding_to_remote_ran, |
| 156 | msc_a->c.fi, -1, MSC_MNCC_EV_CALL_ENDED, NULL, NULL); |
| 157 | |
| 158 | /* inter-MSC link. msc_i_remote_alloc() properly "steals" the e_link from msc_t. */ |
| 159 | msc_i = msc_i_remote_alloc(msc_a->c.msub, msc_t->c.ran, msc_t->c.remote_to); |
| 160 | OSMO_ASSERT(msc_t->c.remote_to == NULL); |
| 161 | } else { |
| 162 | /* local BSS */ |
| 163 | msc_i = msc_i_alloc(msc_a->c.msub, msc_t->c.ran); |
| 164 | /* msc_i_set_ran_conn() properly "steals" the ran_conn from msc_t */ |
| 165 | msc_i_set_ran_conn(msc_i, msc_t->ran_conn); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | osmo_fsm_inst_term(msc_a->ho.fi, OSMO_FSM_TERM_REGULAR, NULL); |
| 170 | } |
| 171 | |
| 172 | #define msc_ho_failed(msc_a, cause, fmt, args...) do { \ |
| 173 | LOG_HO(msc_a, LOGL_ERROR, fmt, ##args); \ |
| 174 | msc_ho_end(msc_a, false, cause); \ |
| 175 | } while(0) |
| 176 | #define msc_ho_try_next_cell(msc_a, fmt, args...) do {\ |
| 177 | LOG_HO(msc_a, LOGL_ERROR, fmt, ##args); \ |
| 178 | msc_ho_fsm_state_chg(msc_a, MSC_HO_ST_REQUIRED); \ |
| 179 | } while(0) |
| 180 | #define msc_ho_success(msc_a) msc_ho_end(msc_a, true, 0) |
| 181 | |
| 182 | enum msc_neighbor_type msc_ho_find_target_cell(struct msc_a *msc_a, const struct gsm0808_cell_id *cid, |
| 183 | const struct neighbor_ident_entry **remote_msc, |
| 184 | struct ran_peer **ran_peer_from_neighbor_ident, |
| 185 | struct ran_peer **ran_peer_from_seen_cells) |
| 186 | { |
| 187 | struct gsm_network *net = msc_a_net(msc_a); |
| 188 | const struct neighbor_ident_entry *e; |
| 189 | struct sccp_ran_inst *sri; |
| 190 | struct ran_peer *rp_from_neighbor_ident = NULL; |
| 191 | struct ran_peer *rp_from_cell_id = NULL; |
| 192 | struct ran_peer *rp; |
| 193 | int i; |
| 194 | |
| 195 | OSMO_ASSERT(remote_msc); |
| 196 | OSMO_ASSERT(ran_peer_from_neighbor_ident); |
| 197 | OSMO_ASSERT(ran_peer_from_seen_cells); |
| 198 | |
| 199 | e = neighbor_ident_find_by_cell(&net->neighbor_ident_list, msc_a->c.ran->type, cid); |
| 200 | |
| 201 | if (e && e->addr.type == MSC_NEIGHBOR_TYPE_REMOTE_MSC) { |
| 202 | *remote_msc = e; |
| 203 | return MSC_NEIGHBOR_TYPE_REMOTE_MSC; |
| 204 | } |
| 205 | |
| 206 | /* It is not a remote MSC target. Figure out local RAN peers. */ |
| 207 | |
| 208 | if (e && e->addr.type == MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER) { |
| 209 | /* Find local RAN peer in neighbor config. If anything is wrong with that, just keep |
| 210 | * rp_from_neighbor_ident == NULL. */ |
| 211 | |
| 212 | struct sccp_ran_inst *sri_from_neighbor_ident = NULL; |
| 213 | struct osmo_ss7_instance *ss7 = NULL; |
| 214 | |
| 215 | /* Get the sccp_ran_inst with sanity checkin. If anything is fishy, just keep |
| 216 | * sri_from_neighbor_ident == NULL and below code will notice the error. */ |
| 217 | if (e->addr.ran_type < msc_ran_infra_len) { |
| 218 | sri_from_neighbor_ident = msc_ran_infra[e->addr.ran_type].sri; |
| 219 | ss7 = osmo_sccp_get_ss7(sri_from_neighbor_ident->sccp); |
| 220 | if (!ss7) |
| 221 | sri_from_neighbor_ident = NULL; |
| 222 | } |
| 223 | |
| 224 | if (!sri_from_neighbor_ident) { |
| 225 | LOG_HO(msc_a, LOGL_ERROR, "Cannot handover to RAN type %s\n", osmo_rat_type_name(e->addr.ran_type)); |
| 226 | } else { |
| 227 | /* Interpret the point-code string placed in the neighbors config. */ |
| 228 | int pc = osmo_ss7_pointcode_parse(ss7, e->addr.local_ran_peer_pc_str); |
| 229 | |
| 230 | if (pc < 0) { |
| 231 | LOG_HO(msc_a, LOGL_ERROR, "Invalid point code string: %s\n", |
| 232 | osmo_quote_str(e->addr.local_ran_peer_pc_str, -1)); |
| 233 | } else { |
| 234 | struct osmo_sccp_addr addr = {}; |
| 235 | osmo_sccp_make_addr_pc_ssn(&addr, pc, sri_from_neighbor_ident->ran->ssn); |
| 236 | rp_from_neighbor_ident = ran_peer_find_by_addr(sri_from_neighbor_ident, &addr); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (!rp_from_neighbor_ident) { |
| 241 | LOG_HO(msc_a, LOGL_ERROR, "Target RAN peer from neighbor config is not connected:" |
| 242 | " Cell ID %s resolves to target address %s\n", |
| 243 | gsm0808_cell_id_name(cid), e->addr.local_ran_peer_pc_str); |
| 244 | } else if (rp_from_neighbor_ident->fi->state != RAN_PEER_ST_READY) { |
| 245 | LOG_HO(msc_a, LOGL_ERROR, "Target RAN peer in invalid state: %s (%s)\n", |
| 246 | osmo_fsm_inst_state_name(rp_from_neighbor_ident->fi), |
| 247 | rp_from_neighbor_ident->fi->id); |
| 248 | rp_from_neighbor_ident = NULL; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* Figure out actually connected RAN peers for this cell ID. |
| 253 | * If no cell has been found yet at all, this might determine a Handover target, |
| 254 | * otherwise this is for sanity checking. If none is found, just keep rp_from_cell_id == NULL. */ |
| 255 | |
| 256 | /* Iterate all connected RAN peers. Possibly, more than one RAN peer has advertised a match for this Cell ID. |
| 257 | * For example, if the handover target is identified as LAC=23 but there are multiple cells with distinct CIs |
| 258 | * serving in LAC=23, we have an ambiguity. It's up to the user to configure correctly, help with logging. */ |
| 259 | for (i = 0; i < msc_ran_infra_len; i++) { |
| 260 | sri = msc_ran_infra[i].sri; |
| 261 | if (!sri) |
| 262 | continue; |
| 263 | |
| 264 | rp = ran_peer_find_by_cell_id(sri, cid, true); |
| 265 | if (rp && rp->fi && rp->fi->state == RAN_PEER_ST_READY) { |
| 266 | if (rp_from_cell_id) { |
| 267 | LOG_HO(msc_a, LOGL_ERROR, |
| 268 | "Ambiguous match for cell ID %s: more than one RAN type is serving this cell" |
| 269 | " ID: %s and %s\n", |
| 270 | gsm0808_cell_id_name(cid), |
| 271 | rp_from_cell_id->fi->id, |
| 272 | rp->fi->id); |
| 273 | /* But logging is all we're going to do about it. */ |
| 274 | } |
| 275 | |
| 276 | /* Use the first found RAN peer, but if multiple matches are found, favor the one that matches |
| 277 | * the current RAN type. */ |
| 278 | if (!rp_from_cell_id || rp->sri == msc_a->c.ran->sri) |
| 279 | rp_from_cell_id = rp; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* Did we find mismatching targets from neighbor config and from connected cells? */ |
| 284 | if (rp_from_neighbor_ident && rp_from_cell_id |
| 285 | && rp_from_neighbor_ident != rp_from_cell_id) { |
| 286 | LOG_HO(msc_a, LOGL_ERROR, "Ambiguous match for cell ID %s:" |
| 287 | " neighbor config points at %s; a matching cell is also served by connected RAN peer %s\n", |
| 288 | gsm0808_cell_id_name(cid), rp_from_neighbor_ident->fi->id, rp_from_cell_id->fi->id); |
| 289 | /* But logging is all we're going to do about it. */ |
| 290 | } |
| 291 | |
| 292 | if (rp_from_neighbor_ident && rp_from_neighbor_ident->sri != msc_a->c.ran->sri) { |
| 293 | LOG_HO(msc_a, LOGL_ERROR, |
| 294 | "Neighbor config indicates inter-RAT Handover, which is not implemented. Ignoring target %s\n", |
| 295 | rp_from_neighbor_ident->fi->id); |
| 296 | rp_from_neighbor_ident = NULL; |
| 297 | } |
| 298 | |
| 299 | if (rp_from_cell_id && rp_from_cell_id->sri != msc_a->c.ran->sri) { |
| 300 | LOG_HO(msc_a, LOGL_ERROR, |
| 301 | "Target RAN peer indicates inter-RAT Handover, which is not implemented. Ignoring target %s\n", |
| 302 | rp_from_cell_id->fi->id); |
| 303 | rp_from_cell_id = NULL; |
| 304 | } |
| 305 | |
| 306 | *ran_peer_from_neighbor_ident = rp_from_neighbor_ident; |
| 307 | *ran_peer_from_seen_cells = rp_from_cell_id; |
| 308 | |
| 309 | return rp_from_neighbor_ident || rp_from_cell_id ? MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER : MSC_NEIGHBOR_TYPE_NONE; |
| 310 | } |
| 311 | |
| 312 | static bool msc_ho_find_next_target_cell(struct msc_a *msc_a) |
| 313 | { |
| 314 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
| 315 | struct ran_handover_required *info = &msc_a->ho.info; |
| 316 | struct gsm0808_cell_id *cid = &msc_a->ho.new_cell.cid; |
| 317 | const struct neighbor_ident_entry *e; |
| 318 | struct ran_peer *rp_from_neighbor_ident = NULL; |
| 319 | struct ran_peer *rp_from_cell_id = NULL; |
| 320 | struct ran_peer *rp; |
| 321 | |
| 322 | unsigned int cil_idx = msc_a->ho.next_cil_idx; |
| 323 | msc_a->ho.next_cil_idx++; |
| 324 | |
| 325 | msc_a->ho.new_cell.type = MSC_NEIGHBOR_TYPE_NONE; |
| 326 | |
| 327 | if (cil_idx >= info->cil.id_list_len) |
| 328 | return false; |
| 329 | |
| 330 | *cid = (struct gsm0808_cell_id){ |
| 331 | .id_discr = info->cil.id_discr, |
| 332 | .id = info->cil.id_list[cil_idx], |
| 333 | }; |
| 334 | |
| 335 | msc_a->ho.new_cell.cgi = (struct osmo_cell_global_id){ |
| 336 | .lai = vsub->cgi.lai, |
| 337 | }; |
| 338 | gsm0808_cell_id_to_cgi(&msc_a->ho.new_cell.cgi, cid); |
| 339 | |
| 340 | switch (msc_ho_find_target_cell(msc_a, cid, &e, &rp_from_neighbor_ident, &rp_from_cell_id)) { |
| 341 | case MSC_NEIGHBOR_TYPE_REMOTE_MSC: |
| 342 | OSMO_ASSERT(e); |
| 343 | msc_a->ho.new_cell.ran_type = e->addr.ran_type; |
| 344 | msc_a->ho.new_cell.type = MSC_NEIGHBOR_TYPE_REMOTE_MSC; |
| 345 | msc_a->ho.new_cell.msc_ipa_name = e->addr.remote_msc_ipa_name.buf; |
| 346 | return true; |
| 347 | |
| 348 | case MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER: |
| 349 | rp = rp_from_neighbor_ident ? : rp_from_cell_id; |
| 350 | OSMO_ASSERT(rp); |
| 351 | msc_a->ho.new_cell.type = MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER; |
| 352 | msc_a->ho.new_cell.ran_peer = rp; |
| 353 | return true; |
| 354 | |
| 355 | default: |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | LOG_HO(msc_a, LOGL_DEBUG, "Cannot find target peer for cell ID %s\n", gsm0808_cell_id_name(cid)); |
| 360 | /* Try the next cell id, if any. */ |
| 361 | return msc_ho_find_next_target_cell(msc_a); |
| 362 | } |
| 363 | |
| 364 | static void msc_ho_fsm_required_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 365 | { |
| 366 | struct msc_a *msc_a = fi->priv; |
| 367 | |
| 368 | if (!msc_ho_find_next_target_cell(msc_a)) { |
| 369 | int tried = msc_a->ho.next_cil_idx - 1; |
| 370 | msc_ho_failed(msc_a, GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, |
| 371 | "Attempted Handover to %u cells without success\n", tried); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | msc_ho_fsm_state_chg(msc_a, MSC_HO_ST_WAIT_REQUEST_ACK); |
| 376 | } |
| 377 | |
| 378 | static void msc_ho_send_handover_request(struct msc_a *msc_a) |
| 379 | { |
| 380 | struct vlr_subscr *vsub = msc_a_vsub(msc_a); |
| 381 | struct gsm_network *net = msc_a_net(msc_a); |
| 382 | struct gsm0808_channel_type channel_type; |
Neels Hofmeyr | d767c73 | 2023-11-17 04:12:29 +0100 | [diff] [blame] | 383 | struct gsm0808_speech_codec_list scl = {}; |
Philipp Maier | 7da956e | 2020-06-09 14:34:40 +0200 | [diff] [blame] | 384 | struct gsm_trans *cc_trans = msc_a->cc.active_trans; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 385 | struct ran_msg ran_enc_msg = { |
| 386 | .msg_type = RAN_MSG_HANDOVER_REQUEST, |
| 387 | .handover_request = { |
| 388 | .imsi = vsub->imsi, |
| 389 | .classmark = &vsub->classmark, |
| 390 | .geran = { |
| 391 | .chosen_encryption = &msc_a->geran_encr, |
| 392 | .a5_encryption_mask = net->a5_encryption_mask, |
| 393 | }, |
| 394 | .bssap_cause = GSM0808_CAUSE_BETTER_CELL, |
| 395 | .current_channel_type_1_present = msc_a->ho.info.current_channel_type_1_present, |
| 396 | .current_channel_type_1 = msc_a->ho.info.current_channel_type_1, |
| 397 | .speech_version_used = msc_a->ho.info.speech_version_used, |
| 398 | .old_bss_to_new_bss_info_raw = msc_a->ho.info.old_bss_to_new_bss_info_raw, |
| 399 | .old_bss_to_new_bss_info_raw_len = msc_a->ho.info.old_bss_to_new_bss_info_raw_len, |
| 400 | |
| 401 | /* Don't send AoIP Transport Layer Address for inter-MSC Handover */ |
| 402 | .rtp_ran_local = (msc_a->ho.new_cell.type == MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER) |
| 403 | ? call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_RAN) : NULL, |
| 404 | }, |
| 405 | }; |
| 406 | |
Neels Hofmeyr | 73d093a | 2021-06-23 23:54:43 +0200 | [diff] [blame] | 407 | if (msc_a->geran_encr.key_len) |
| 408 | LOG_MSC_A(msc_a, LOGL_DEBUG, "HO Request with ciphering: A5/%d kc %s kc128 %s\n", |
| 409 | msc_a->geran_encr.alg_id - 1, |
| 410 | osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.key, msc_a->geran_encr.key_len), |
| 411 | msc_a->geran_encr.kc128_present ? |
| 412 | osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.kc128, sizeof(msc_a->geran_encr.kc128)) |
| 413 | : "-"); |
| 414 | |
Neels Hofmeyr | b6c11c4 | 2022-08-08 18:15:32 +0200 | [diff] [blame] | 415 | if (cc_trans) { |
Oliver Smith | cfa37cb | 2023-08-22 13:28:18 +0200 | [diff] [blame] | 416 | switch (cc_trans->bearer_cap.transfer) { |
| 417 | case GSM48_BCAP_ITCAP_SPEECH: |
| 418 | if (sdp_audio_codecs_to_gsm0808_channel_type(&channel_type, |
| 419 | &cc_trans->cc.local.audio_codecs)) { |
| 420 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, |
| 421 | "Failed to determine Channel Type for Handover Request message (speech)\n"); |
| 422 | return; |
| 423 | } |
| 424 | break; |
Manawyrm | 1ed12ea | 2023-10-14 17:23:04 +0200 | [diff] [blame] | 425 | case GSM48_BCAP_ITCAP_3k1_AUDIO: |
| 426 | case GSM48_BCAP_ITCAP_FAX_G3: |
Oliver Smith | cfa37cb | 2023-08-22 13:28:18 +0200 | [diff] [blame] | 427 | case GSM48_BCAP_ITCAP_UNR_DIG_INF: |
| 428 | if (csd_bs_list_to_gsm0808_channel_type(&channel_type, &cc_trans->cc.local.bearer_services)) { |
| 429 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, |
| 430 | "Failed to determine Channel Type for Handover Request message (CSD)\n"); |
| 431 | return; |
| 432 | } |
| 433 | break; |
| 434 | default: |
| 435 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, "Failed to create" |
| 436 | " Handover Request message for information transfer capability %d\n", |
| 437 | cc_trans->bearer_cap.transfer); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 438 | return; |
| 439 | } |
Oliver Smith | cfa37cb | 2023-08-22 13:28:18 +0200 | [diff] [blame] | 440 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 441 | ran_enc_msg.handover_request.geran.channel_type = &channel_type; |
Oliver Smith | 0d4607d | 2023-06-22 12:26:11 +0200 | [diff] [blame] | 442 | ran_enc_msg.handover_request.call_id_present = true; |
Andreas Eversberg | 712b28e | 2023-06-21 11:17:26 +0200 | [diff] [blame] | 443 | ran_enc_msg.handover_request.call_id = cc_trans->call_id; |
Neels Hofmeyr | b091d21 | 2022-08-08 19:11:51 +0200 | [diff] [blame] | 444 | |
Neels Hofmeyr | d767c73 | 2023-11-17 04:12:29 +0100 | [diff] [blame] | 445 | /* Call assignment is now capable of re-assigning to overcome a codec mismatch with the remote call leg. |
| 446 | * But for inter-MSC handover, that is not supported yet. So keep here the old limitation of only |
| 447 | * offering the assigned codec. */ |
| 448 | if (sdp_audio_codec_is_set(&cc_trans->cc.codecs.assignment)) |
| 449 | sdp_audio_codec_to_speech_codec_list(&scl, &cc_trans->cc.codecs.assignment); |
| 450 | else |
| 451 | sdp_audio_codecs_to_speech_codec_list(&scl, &cc_trans->cc.local.audio_codecs); |
Neels Hofmeyr | b091d21 | 2022-08-08 19:11:51 +0200 | [diff] [blame] | 452 | if (!scl.len) { |
| 453 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, "Failed to compose" |
| 454 | " Codec List (MSC Preferred) for Handover Request message\n"); |
| 455 | return; |
| 456 | } |
| 457 | ran_enc_msg.handover_request.codec_list_msc_preferred = &scl; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | gsm0808_cell_id_from_cgi(&ran_enc_msg.handover_request.cell_id_serving, CELL_IDENT_WHOLE_GLOBAL, &vsub->cgi); |
| 461 | ran_enc_msg.handover_request.cell_id_target = msc_a->ho.new_cell.cid; |
| 462 | |
| 463 | if (msc_a_msg_down(msc_a, MSC_ROLE_T, MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST, &ran_enc_msg)) |
| 464 | msc_ho_try_next_cell(msc_a, "Failed to send Handover Request message\n"); |
| 465 | } |
| 466 | |
| 467 | static void msc_ho_fsm_wait_request_ack_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 468 | { |
| 469 | struct msc_a *msc_a = fi->priv; |
| 470 | struct msc_i *msc_i = msc_a_msc_i(msc_a); |
| 471 | struct msc_t *msc_t; |
| 472 | struct ran_peer *rp; |
| 473 | const char *ipa_name; |
| 474 | |
| 475 | msc_t = msc_a_msc_t(msc_a); |
| 476 | if (msc_t) { |
| 477 | /* All the other code should prevent this from happening, ever. */ |
| 478 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, |
| 479 | "Cannot initiate Handover Request, there still is an active MSC-T role: %s\n", |
| 480 | msc_t->c.fi->id); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | if (!msc_i) { |
| 485 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, |
| 486 | "Cannot initiate Handover Request, there is no MSC-I role\n"); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | if (!msc_i->c.remote_to |
| 491 | && !(msc_i->ran_conn && msc_i->ran_conn->ran_peer)) { |
| 492 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, |
| 493 | "Cannot initiate Handover Request, MSC-I role has no connection\n"); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | switch (msc_a->ho.new_cell.type) { |
| 498 | case MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER: |
| 499 | rp = msc_a->ho.new_cell.ran_peer; |
| 500 | OSMO_ASSERT(rp && rp->fi); |
| 501 | |
| 502 | if (msc_i->c.remote_to) { |
| 503 | LOG_HO(msc_a, LOGL_INFO, |
| 504 | "Starting inter-MSC Subsequent Handover from remote MSC %s to local %s\n", |
| 505 | msc_i->c.remote_to->remote_name, rp->fi->id); |
| 506 | msc_a->ho.subsequent_ho = true; |
| 507 | } else { |
| 508 | LOG_HO(msc_a, LOGL_INFO, "Starting inter-BSC Handover from %s to %s\n", |
| 509 | msc_i->ran_conn->ran_peer->fi->id, rp->fi->id); |
| 510 | } |
| 511 | |
Vadim Yanitskiy | a870faf | 2019-05-11 02:45:46 +0700 | [diff] [blame] | 512 | msc_t = msc_t_alloc(msc_a->c.msub, rp); |
| 513 | break; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 514 | |
| 515 | case MSC_NEIGHBOR_TYPE_REMOTE_MSC: |
| 516 | ipa_name = msc_a->ho.new_cell.msc_ipa_name; |
| 517 | OSMO_ASSERT(ipa_name); |
| 518 | |
| 519 | if (msc_i->c.remote_to) { |
| 520 | LOG_HO(msc_a, LOGL_INFO, |
| 521 | "Starting inter-MSC Subsequent Handover from remote MSC %s to remote MSC at %s\n", |
| 522 | msc_i->c.remote_to->remote_name, osmo_quote_str(ipa_name, -1)); |
| 523 | msc_a->ho.subsequent_ho = true; |
| 524 | } else { |
| 525 | LOG_HO(msc_a, LOGL_INFO, "Starting inter-MSC Handover from local %s to remote MSC at %s\n", |
| 526 | msc_i->ran_conn->ran_peer->fi->id, |
| 527 | osmo_quote_str(ipa_name, -1)); |
| 528 | } |
| 529 | |
Vadim Yanitskiy | a870faf | 2019-05-11 02:45:46 +0700 | [diff] [blame] | 530 | msc_t = msc_t_remote_alloc(msc_a->c.msub, msc_a->c.ran, |
| 531 | (const uint8_t *) ipa_name, |
| 532 | strlen(ipa_name)); |
| 533 | break; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 534 | |
| 535 | default: |
| 536 | msc_ho_try_next_cell(msc_a, "unknown Handover target type %d\n", msc_a->ho.new_cell.type); |
| 537 | return; |
| 538 | } |
| 539 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 540 | if (!msc_t) { |
| 541 | /* There should definitely be one now. */ |
| 542 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, |
| 543 | "Cannot initiate Handover Request, failed to set up a target MSC-T\n"); |
| 544 | return; |
| 545 | } |
Vadim Yanitskiy | a870faf | 2019-05-11 02:45:46 +0700 | [diff] [blame] | 546 | |
| 547 | msc_ho_send_handover_request(msc_a); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static void msc_ho_rx_request_ack(struct msc_a *msc_a, struct msc_a_ran_dec_data *hra); |
| 551 | |
| 552 | static void msc_ho_fsm_wait_request_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 553 | { |
| 554 | struct msc_a *msc_a = fi->priv; |
| 555 | |
| 556 | switch (event) { |
| 557 | |
| 558 | case MSC_HO_EV_RX_REQUEST_ACK: |
| 559 | msc_ho_rx_request_ack(msc_a, (struct msc_a_ran_dec_data*)data); |
| 560 | return; |
| 561 | |
| 562 | case MSC_HO_EV_RX_FAILURE: |
| 563 | msc_ho_failed(msc_a, GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, |
| 564 | "Received Handover Failure message\n"); |
| 565 | return; |
| 566 | |
| 567 | default: |
| 568 | OSMO_ASSERT(false); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | static void msc_ho_rtp_switch_to_new_cell(struct msc_a *msc_a); |
| 573 | |
| 574 | void msc_ho_mncc_forward_cb(struct mncc_call *mncc_call, const union mncc_msg *mncc_msg, void *data) |
| 575 | { |
| 576 | struct msc_a *msc_a = data; |
| 577 | switch (mncc_msg->msg_type) { |
| 578 | case MNCC_RTP_CONNECT: |
| 579 | msc_a->ho.rtp_switched_to_new_cell = true; |
| 580 | return; |
| 581 | default: |
| 582 | return; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /* Initiate call forwarding via MNCC: call the Handover Number that the other MSC assigned. */ |
| 587 | static int msc_ho_start_inter_msc_call_forwarding(struct msc_a *msc_a, struct msc_t *msc_t, |
| 588 | const struct msc_a_ran_dec_data *hra) |
| 589 | { |
| 590 | const struct osmo_gsup_message *e_info = hra->an_apdu->e_info; |
| 591 | struct gsm_mncc outgoing_call_req = {}; |
| 592 | struct call_leg *cl = msc_a->cc.call_leg; |
| 593 | struct rtp_stream *rtp_to_ran = cl ? cl->rtp[RTP_TO_RAN] : NULL; |
| 594 | struct mncc_call *mncc_call; |
| 595 | |
| 596 | if (!e_info || !e_info->msisdn_enc || !e_info->msisdn_enc_len) { |
| 597 | msc_ho_try_next_cell(msc_a, |
| 598 | "No Handover Number in Handover Request Acknowledge from remote MSC\n"); |
| 599 | return -EINVAL; |
| 600 | } |
| 601 | |
Neels Hofmeyr | da3ce71 | 2019-05-09 14:16:26 +0200 | [diff] [blame] | 602 | if (!rtp_to_ran) { |
| 603 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, "Unexpected: no RTP stream is set up\n"); |
| 604 | return -EINVAL; |
| 605 | } |
| 606 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 607 | /* Backup old cell's RTP IP:port and codec data */ |
| 608 | msc_a->ho.old_cell.ran_remote_rtp = rtp_to_ran->remote; |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 609 | msc_a->ho.old_cell.codecs = rtp_to_ran->codecs; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 610 | |
| 611 | /* Blindly taken over from an MNCC trace of existing code: send an all-zero CCCAP: */ |
| 612 | outgoing_call_req.fields |= MNCC_F_CCCAP; |
| 613 | |
| 614 | /* Called number */ |
| 615 | outgoing_call_req.fields |= MNCC_F_CALLED; |
| 616 | outgoing_call_req.called.plan = 1; /* Empirical magic number. There seem to be no enum or defines for this. |
| 617 | * The only other place setting this apparently is gsm48_decode_called(). */ |
| 618 | if (gsm48_decode_bcd_number2(outgoing_call_req.called.number, sizeof(outgoing_call_req.called.number), |
| 619 | e_info->msisdn_enc, e_info->msisdn_enc_len, 0)) { |
| 620 | msc_ho_try_next_cell(msc_a, |
| 621 | "Failed to decode Handover Number in Handover Request Acknowledge" |
| 622 | " from remote MSC\n"); |
| 623 | return -EINVAL; |
| 624 | } |
| 625 | |
| 626 | if (msc_a->cc.active_trans) { |
| 627 | outgoing_call_req.fields |= MNCC_F_BEARER_CAP; |
| 628 | outgoing_call_req.bearer_cap = msc_a->cc.active_trans->bearer_cap; |
| 629 | } |
| 630 | |
| 631 | mncc_call = mncc_call_alloc(msc_a_vsub(msc_a), |
| 632 | msc_a->ho.fi, |
| 633 | MSC_HO_EV_MNCC_FORWARDING_COMPLETE, |
| 634 | MSC_HO_EV_MNCC_FORWARDING_FAILED, |
| 635 | msc_ho_mncc_forward_cb, msc_a); |
| 636 | |
| 637 | mncc_call_set_rtp_stream(mncc_call, rtp_to_ran); |
| 638 | msc_a->ho.new_cell.mncc_forwarding_to_remote_ran = mncc_call; |
| 639 | return mncc_call_outgoing_start(mncc_call, &outgoing_call_req); |
| 640 | } |
| 641 | |
| 642 | static void msc_ho_rx_request_ack(struct msc_a *msc_a, struct msc_a_ran_dec_data *hra) |
| 643 | { |
| 644 | struct msc_t *msc_t = msc_a_msc_t(msc_a); |
| 645 | struct ran_msg ran_enc_msg; |
| 646 | |
| 647 | OSMO_ASSERT(hra->ran_dec); |
| 648 | OSMO_ASSERT(hra->an_apdu); |
| 649 | |
| 650 | if (!msc_t) { |
| 651 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, "MSC-T role missing\n"); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | if (!hra->ran_dec->handover_request_ack.rr_ho_command |
| 656 | || !hra->ran_dec->handover_request_ack.rr_ho_command_len) { |
| 657 | msc_ho_try_next_cell(msc_a, "Missing mandatory IE in Handover Request Acknowledge:" |
| 658 | " L3 Info (RR Handover Command)\n"); |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | if (!hra->ran_dec->handover_request_ack.chosen_channel_present) { |
| 663 | LOG_HO(msc_a, LOGL_DEBUG, "No 'Chosen Channel' IE in Handover Request Ack\n"); |
| 664 | msc_t->geran.chosen_channel = 0; |
| 665 | } else |
| 666 | msc_t->geran.chosen_channel = hra->ran_dec->handover_request_ack.chosen_channel; |
| 667 | |
| 668 | if (!hra->ran_dec->handover_request_ack.chosen_encr_alg) { |
| 669 | LOG_HO(msc_a, LOGL_DEBUG, "No 'Chosen Encryption Algorithm' IE in Handover Request Ack\n"); |
| 670 | msc_t->geran.chosen_encr_alg = 0; |
| 671 | } else { |
| 672 | msc_t->geran.chosen_encr_alg = hra->ran_dec->handover_request_ack.chosen_encr_alg; |
| 673 | if (msc_t->geran.chosen_encr_alg < 1 || msc_t->geran.chosen_encr_alg > 8) { |
| 674 | msc_ho_try_next_cell(msc_a, "Handover Request Ack: Invalid 'Chosen Encryption Algorithm': %u\n", |
| 675 | msc_t->geran.chosen_encr_alg); |
| 676 | return; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | msc_t->geran.chosen_speech_version = hra->ran_dec->handover_request_ack.chosen_speech_version; |
| 681 | if (!msc_t->geran.chosen_speech_version) |
| 682 | LOG_HO(msc_a, LOGL_DEBUG, "No 'Chosen Speech Version' IE in Handover Request Ack\n"); |
| 683 | |
| 684 | /* Inter-MSC call forwarding? */ |
| 685 | if (msc_a->ho.new_cell.type == MSC_NEIGHBOR_TYPE_REMOTE_MSC) { |
| 686 | if (msc_ho_start_inter_msc_call_forwarding(msc_a, msc_t, hra)) |
| 687 | return; |
| 688 | } |
| 689 | |
| 690 | msc_ho_fsm_state_chg(msc_a, MSC_HO_ST_WAIT_COMPLETE); |
| 691 | |
| 692 | /* Forward the RR Handover Command composed by the new RAN peer down to the old RAN peer */ |
| 693 | ran_enc_msg = (struct ran_msg){ |
| 694 | .msg_type = RAN_MSG_HANDOVER_COMMAND, |
| 695 | .handover_command = { |
| 696 | .rr_ho_command = hra->ran_dec->handover_request_ack.rr_ho_command, |
| 697 | .rr_ho_command_len = hra->ran_dec->handover_request_ack.rr_ho_command_len, |
| 698 | }, |
| 699 | }; |
| 700 | |
| 701 | if (msc_a_msg_down(msc_a, MSC_ROLE_I, |
| 702 | msc_a->ho.subsequent_ho ? MSC_I_EV_FROM_A_PREPARE_SUBSEQUENT_HANDOVER_RESULT |
| 703 | : MSC_I_EV_FROM_A_FORWARD_ACCESS_SIGNALLING_REQUEST, |
| 704 | &ran_enc_msg)) { |
| 705 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, "Failed to send Handover Command\n"); |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | msc_a->ho.new_cell.ran_remote_rtp = hra->ran_dec->handover_request_ack.remote_rtp; |
Neels Hofmeyr | 84ce206 | 2019-10-05 05:15:25 +0200 | [diff] [blame] | 710 | if (osmo_sockaddr_str_is_nonzero(&msc_a->ho.new_cell.ran_remote_rtp)) { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 711 | LOG_HO(msc_a, LOGL_DEBUG, "Request Ack contains cell's RTP address " OSMO_SOCKADDR_STR_FMT "\n", |
| 712 | OSMO_SOCKADDR_STR_FMT_ARGS(&msc_a->ho.new_cell.ran_remote_rtp)); |
| 713 | } |
| 714 | |
| 715 | msc_a->ho.new_cell.codec_present = hra->ran_dec->handover_request_ack.codec_present; |
| 716 | msc_a->ho.new_cell.codec = hra->ran_dec->handover_request_ack.codec; |
| 717 | if (hra->ran_dec->handover_request_ack.codec_present) { |
| 718 | LOG_HO(msc_a, LOGL_DEBUG, "Request Ack contains codec %s\n", |
Neels Hofmeyr | 7934e0d | 2022-10-31 18:13:47 +0100 | [diff] [blame] | 719 | gsm0808_speech_codec_type_name(msc_a->ho.new_cell.codec.type)); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | static void msc_ho_rtp_switch_to_new_cell(struct msc_a *msc_a) |
| 724 | { |
| 725 | struct call_leg *cl = msc_a->cc.call_leg; |
| 726 | struct rtp_stream *rtp_to_ran = cl ? cl->rtp[RTP_TO_RAN] : NULL; |
| 727 | |
| 728 | if (!rtp_to_ran) { |
| 729 | LOG_HO(msc_a, LOGL_DEBUG, "No RTP stream, nothing to switch\n"); |
| 730 | return; |
| 731 | } |
| 732 | |
Neels Hofmeyr | 84ce206 | 2019-10-05 05:15:25 +0200 | [diff] [blame] | 733 | if (!osmo_sockaddr_str_is_nonzero(&msc_a->ho.new_cell.ran_remote_rtp)) { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 734 | LOG_HO(msc_a, LOGL_DEBUG, "New cell's RTP IP:port not yet known, not switching RTP stream\n"); |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | if (msc_a->ho.rtp_switched_to_new_cell) { |
| 739 | LOG_HO(msc_a, LOGL_DEBUG, "Already switched RTP to new cell\n"); |
| 740 | return; |
| 741 | } |
| 742 | msc_a->ho.rtp_switched_to_new_cell = true; |
| 743 | |
| 744 | /* Backup old cell's RTP IP:port and codec data */ |
| 745 | msc_a->ho.old_cell.ran_remote_rtp = rtp_to_ran->remote; |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 746 | msc_a->ho.old_cell.codecs = rtp_to_ran->codecs; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 747 | |
| 748 | LOG_HO(msc_a, LOGL_DEBUG, "Switching RTP stream to new cell: from " OSMO_SOCKADDR_STR_FMT " to " OSMO_SOCKADDR_STR_FMT "\n", |
| 749 | OSMO_SOCKADDR_STR_FMT_ARGS(&msc_a->ho.old_cell.ran_remote_rtp), |
| 750 | OSMO_SOCKADDR_STR_FMT_ARGS(&msc_a->ho.new_cell.ran_remote_rtp)); |
| 751 | |
| 752 | /* If a previous forwarding to a remote MSC is still active, this now becomes no longer responsible for the RTP |
| 753 | * stream. */ |
| 754 | if (msc_a->cc.mncc_forwarding_to_remote_ran) { |
| 755 | if (msc_a->cc.mncc_forwarding_to_remote_ran->rtps != rtp_to_ran) { |
| 756 | LOG_HO(msc_a, LOGL_ERROR, |
| 757 | "Unexpected state: previous MNCC forwarding not using RTP-to-RAN stream\n"); |
| 758 | /* That would be weird, but carry on anyway... */ |
| 759 | } |
| 760 | mncc_call_detach_rtp_stream(msc_a->cc.mncc_forwarding_to_remote_ran); |
| 761 | } |
| 762 | |
| 763 | /* Switch over to the new peer */ |
| 764 | rtp_stream_set_remote_addr(rtp_to_ran, &msc_a->ho.new_cell.ran_remote_rtp); |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 765 | if (msc_a->ho.new_cell.codec_present) { |
Neels Hofmeyr | 7934e0d | 2022-10-31 18:13:47 +0100 | [diff] [blame] | 766 | const struct codec_mapping *m; |
| 767 | m = codec_mapping_by_gsm0808_speech_codec_type(msc_a->ho.new_cell.codec.type); |
| 768 | /* TODO: use codec_mapping_by_gsm0808_speech_codec() to also match on codec.cfg */ |
| 769 | if (!m) |
| 770 | LOG_HO(msc_a, LOGL_ERROR, "Cannot resolve codec: %s\n", |
| 771 | gsm0808_speech_codec_type_name(msc_a->ho.new_cell.codec.type)); |
| 772 | else |
| 773 | rtp_stream_set_one_codec(rtp_to_ran, &m->sdp); |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 774 | } else { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 775 | LOG_HO(msc_a, LOGL_ERROR, "No codec is set\n"); |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 776 | } |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 777 | rtp_stream_commit(rtp_to_ran); |
| 778 | } |
| 779 | |
| 780 | static void msc_ho_rtp_rollback_to_old_cell(struct msc_a *msc_a) |
| 781 | { |
| 782 | struct call_leg *cl = msc_a->cc.call_leg; |
| 783 | struct rtp_stream *rtp_to_ran = cl ? cl->rtp[RTP_TO_RAN] : NULL; |
| 784 | |
| 785 | if (!msc_a->ho.rtp_switched_to_new_cell) { |
| 786 | LOG_HO(msc_a, LOGL_DEBUG, "Not switched RTP to new cell yet, no need to roll back\n"); |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | if (!rtp_to_ran) { |
| 791 | LOG_HO(msc_a, LOGL_DEBUG, "No RTP stream, nothing to switch\n"); |
| 792 | return; |
| 793 | } |
| 794 | |
Neels Hofmeyr | 84ce206 | 2019-10-05 05:15:25 +0200 | [diff] [blame] | 795 | if (!osmo_sockaddr_str_is_nonzero(&msc_a->ho.old_cell.ran_remote_rtp)) { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 796 | LOG_HO(msc_a, LOGL_DEBUG, "Have no RTP IP:port for the old cell, not switching back to\n"); |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | /* The new call forwarding to a remote MSC is no longer needed because the handover failed */ |
| 801 | if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran) |
| 802 | mncc_call_detach_rtp_stream(msc_a->ho.new_cell.mncc_forwarding_to_remote_ran); |
| 803 | |
| 804 | /* If before this handover, there was a call forwarding to a remote MSC in place, this now goes back into |
| 805 | * responsibility. */ |
| 806 | if (msc_a->cc.mncc_forwarding_to_remote_ran) |
| 807 | mncc_call_set_rtp_stream(msc_a->cc.mncc_forwarding_to_remote_ran, rtp_to_ran); |
| 808 | |
| 809 | msc_a->ho.rtp_switched_to_new_cell = false; |
| 810 | msc_a->ho.ready_to_switch_rtp = false; |
| 811 | LOG_HO(msc_a, LOGL_NOTICE, "Switching RTP back to old cell\n"); |
| 812 | |
| 813 | /* Switch back to the old cell */ |
| 814 | rtp_stream_set_remote_addr(rtp_to_ran, &msc_a->ho.old_cell.ran_remote_rtp); |
Neels Hofmeyr | 62bfa37 | 2022-10-31 18:51:07 +0100 | [diff] [blame] | 815 | rtp_stream_set_codecs(rtp_to_ran, &msc_a->ho.old_cell.codecs); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 816 | rtp_stream_commit(rtp_to_ran); |
| 817 | } |
| 818 | |
| 819 | static void msc_ho_send_handover_succeeded(struct msc_a *msc_a) |
| 820 | { |
| 821 | struct ran_msg ran_enc_msg = { |
| 822 | .msg_type = RAN_MSG_HANDOVER_SUCCEEDED, |
| 823 | }; |
| 824 | |
| 825 | if (msc_a_msg_down(msc_a, MSC_ROLE_I, MSC_I_EV_FROM_A_FORWARD_ACCESS_SIGNALLING_REQUEST, &ran_enc_msg)) |
| 826 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, "Failed to send Handover Succeeded message\n"); |
| 827 | } |
| 828 | |
| 829 | static void msc_ho_fsm_wait_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 830 | { |
| 831 | struct msc_a *msc_a = fi->priv; |
| 832 | |
| 833 | switch (event) { |
| 834 | |
| 835 | case MSC_HO_EV_RX_DETECT: |
| 836 | msc_a->ho.ready_to_switch_rtp = true; |
| 837 | /* For inter-MSC, the mncc_fsm switches the rtp_stream upon MNCC_RTP_CONNECT. |
| 838 | * For inter-BSC, need to switch here to the address obtained from Handover Request Ack. */ |
| 839 | if (msc_a->ho.new_cell.type == MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER) |
| 840 | msc_ho_rtp_switch_to_new_cell(msc_a); |
| 841 | msc_ho_send_handover_succeeded(msc_a); |
| 842 | return; |
| 843 | |
| 844 | case MSC_HO_EV_RX_COMPLETE: |
| 845 | msc_ho_success(msc_a); |
| 846 | return; |
| 847 | |
| 848 | case MSC_HO_EV_RX_FAILURE: |
| 849 | msc_ho_failed(msc_a, GSM0808_CAUSE_NO_RADIO_RESOURCE_AVAILABLE, |
| 850 | "Received Handover Failure message\n"); |
| 851 | return; |
| 852 | |
| 853 | case MSC_HO_EV_MNCC_FORWARDING_FAILED: |
| 854 | msc_ho_failed(msc_a, GSM0808_CAUSE_EQUIPMENT_FAILURE, "MNCC Forwarding failed\n"); |
| 855 | return; |
| 856 | |
| 857 | case MSC_HO_EV_MNCC_FORWARDING_COMPLETE: |
| 858 | return; |
| 859 | |
| 860 | default: |
| 861 | OSMO_ASSERT(false); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | static void msc_ho_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) |
| 866 | { |
| 867 | struct msc_a *msc_a = fi->priv; |
| 868 | struct msc_t *msc_t = msc_a_msc_t(msc_a); |
| 869 | |
| 870 | /* paranoia */ |
| 871 | if (msc_a->ho.fi != fi) |
| 872 | return; |
| 873 | |
| 874 | /* Completely clear all handover state */ |
| 875 | msc_a->ho = (struct msc_ho_state){}; |
| 876 | |
| 877 | if (msc_t) |
| 878 | msc_t_clear(msc_t); |
| 879 | } |
| 880 | |
| 881 | static int msc_ho_fsm_timer_cb(struct osmo_fsm_inst *fi) |
| 882 | { |
| 883 | return 1; |
| 884 | } |
| 885 | |
| 886 | #define S(x) (1 << (x)) |
| 887 | |
| 888 | static const struct osmo_fsm_state msc_ho_fsm_states[] = { |
| 889 | [MSC_HO_ST_REQUIRED] = { |
| 890 | .name = OSMO_STRINGIFY(MSC_HO_ST_REQUIRED), |
| 891 | .out_state_mask = 0 |
| 892 | | S(MSC_HO_ST_REQUIRED) |
| 893 | | S(MSC_HO_ST_WAIT_REQUEST_ACK) |
| 894 | , |
| 895 | .onenter = msc_ho_fsm_required_onenter, |
| 896 | }, |
| 897 | [MSC_HO_ST_WAIT_REQUEST_ACK] = { |
| 898 | .name = OSMO_STRINGIFY(MSC_HO_ST_WAIT_REQUEST_ACK), |
| 899 | .in_event_mask = 0 |
| 900 | | S(MSC_HO_EV_RX_REQUEST_ACK) |
| 901 | | S(MSC_HO_EV_RX_FAILURE) |
| 902 | , |
| 903 | .out_state_mask = 0 |
| 904 | | S(MSC_HO_ST_REQUIRED) |
| 905 | | S(MSC_HO_ST_WAIT_COMPLETE) |
| 906 | , |
| 907 | .onenter = msc_ho_fsm_wait_request_ack_onenter, |
| 908 | .action = msc_ho_fsm_wait_request_ack, |
| 909 | }, |
| 910 | [MSC_HO_ST_WAIT_COMPLETE] = { |
| 911 | .name = OSMO_STRINGIFY(MSC_HO_ST_WAIT_COMPLETE), |
| 912 | .in_event_mask = 0 |
| 913 | | S(MSC_HO_EV_RX_DETECT) |
| 914 | | S(MSC_HO_EV_RX_COMPLETE) |
| 915 | | S(MSC_HO_EV_RX_FAILURE) |
| 916 | | S(MSC_HO_EV_MNCC_FORWARDING_COMPLETE) |
| 917 | | S(MSC_HO_EV_MNCC_FORWARDING_FAILED) |
| 918 | , |
| 919 | .action = msc_ho_fsm_wait_complete, |
| 920 | }, |
| 921 | }; |
| 922 | |
| 923 | static const struct value_string msc_ho_fsm_event_names[] = { |
| 924 | OSMO_VALUE_STRING(MSC_HO_EV_RX_REQUEST_ACK), |
| 925 | OSMO_VALUE_STRING(MSC_HO_EV_RX_DETECT), |
| 926 | OSMO_VALUE_STRING(MSC_HO_EV_RX_COMPLETE), |
| 927 | OSMO_VALUE_STRING(MSC_HO_EV_RX_FAILURE), |
| 928 | {} |
| 929 | }; |
| 930 | |
| 931 | struct osmo_fsm msc_ho_fsm = { |
| 932 | .name = "handover", |
| 933 | .states = msc_ho_fsm_states, |
| 934 | .num_states = ARRAY_SIZE(msc_ho_fsm_states), |
| 935 | .log_subsys = DHO, |
| 936 | .event_names = msc_ho_fsm_event_names, |
| 937 | .timer_cb = msc_ho_fsm_timer_cb, |
| 938 | .cleanup = msc_ho_fsm_cleanup, |
| 939 | }; |