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