blob: 76c0f0194449c144384e766477ca726eb3db95d2 [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* Code to manage a subscriber's MSC-A role */
2/*
3 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
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>
37#include <osmocom/msc/vlr.h>
38#include <osmocom/msc/transaction.h>
39#include <osmocom/msc/ran_peer.h>
40#include <osmocom/msc/ran_msg_a.h>
41#include <osmocom/msc/ran_msg_iu.h>
42#include <osmocom/msc/sgs_iface.h>
43#include <osmocom/msc/gsm_04_08.h>
44#include <osmocom/msc/gsm_09_11.h>
45#include <osmocom/msc/gsm_04_14.h>
46#include <osmocom/msc/call_leg.h>
47#include <osmocom/msc/rtp_stream.h>
48#include <osmocom/msc/msc_ho.h>
49
50#define MSC_A_USE_WAIT_CLEAR_COMPLETE "wait-Clear-Complete"
51
52static struct osmo_fsm msc_a_fsm;
53
54static const struct osmo_tdef_state_timeout msc_a_fsm_timeouts[32] = {
55 [MSC_A_ST_VALIDATE_L3] = { .T = -1 },
56 [MSC_A_ST_AUTH_CIPH] = { .keep_timer = true },
57 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = { .keep_timer = true },
58 [MSC_A_ST_AUTHENTICATED] = { .keep_timer = true },
59 [MSC_A_ST_RELEASING] = { .T = -2 },
60 [MSC_A_ST_RELEASED] = { .T = -2 },
61};
62
63/* Transition to a state, using the T timer defined in msc_a_fsm_timeouts.
64 * The actual timeout value is in turn obtained from network->T_defs.
65 * Assumes local variable fi exists. */
Neels Hofmeyr01653252019-09-03 02:06:22 +020066#define msc_a_state_chg_always(msc_a, state) \
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010067 osmo_tdef_fsm_inst_state_chg((msc_a)->c.fi, state, msc_a_fsm_timeouts, (msc_a)->c.ran->tdefs, 5)
68
Neels Hofmeyr01653252019-09-03 02:06:22 +020069/* Same as msc_a_state_chg_always() but ignore if the msc_a already is in the target state. */
70#define msc_a_state_chg(msc_a, STATE) do { \
71 if ((msc_a)->c.fi->state != STATE) \
72 msc_a_state_chg_always(msc_a, STATE); \
73 } while(0)
74
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010075struct gsm_network *msc_a_net(const struct msc_a *msc_a)
76{
77 return msub_net(msc_a->c.msub);
78}
79
80struct vlr_subscr *msc_a_vsub(const struct msc_a *msc_a)
81{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020082 if (!msc_a)
83 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010084 return msub_vsub(msc_a->c.msub);
85}
86
87struct msc_i *msc_a_msc_i(const struct msc_a *msc_a)
88{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020089 if (!msc_a)
90 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010091 return msub_msc_i(msc_a->c.msub);
92}
93
94struct msc_t *msc_a_msc_t(const struct msc_a *msc_a)
95{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020096 if (!msc_a)
97 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010098 return msub_msc_t(msc_a->c.msub);
99}
100
101struct msc_a *msc_a_fi_priv(struct osmo_fsm_inst *fi)
102{
103 OSMO_ASSERT(fi);
104 OSMO_ASSERT(fi->fsm == &msc_a_fsm);
105 OSMO_ASSERT(fi->priv);
106 return fi->priv;
107}
108
109static void update_counters(struct osmo_fsm_inst *fi, bool conn_accepted)
110{
111 struct msc_a *msc_a = fi->priv;
112 struct gsm_network *net = msc_a_net(msc_a);
113 switch (msc_a->complete_layer3_type) {
114 case COMPLETE_LAYER3_LU:
115 rate_ctr_inc(&net->msc_ctrs->ctr[
116 conn_accepted ? MSC_CTR_LOC_UPDATE_COMPLETED
117 : MSC_CTR_LOC_UPDATE_FAILED]);
118 break;
119 case COMPLETE_LAYER3_CM_SERVICE_REQ:
120 rate_ctr_inc(&net->msc_ctrs->ctr[
121 conn_accepted ? MSC_CTR_CM_SERVICE_REQUEST_ACCEPTED
122 : MSC_CTR_CM_SERVICE_REQUEST_REJECTED]);
123 break;
124 case COMPLETE_LAYER3_PAGING_RESP:
125 rate_ctr_inc(&net->msc_ctrs->ctr[
126 conn_accepted ? MSC_CTR_PAGING_RESP_ACCEPTED
127 : MSC_CTR_PAGING_RESP_REJECTED]);
128 break;
129 default:
130 break;
131 }
132}
133
134static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted)
135{
136 struct msc_a *msc_a = fi->priv;
137 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
138
139 update_counters(fi, conn_accepted);
140
Neels Hofmeyr83cf10c2020-06-24 14:23:26 +0200141 if (conn_accepted) {
142 /* Record the Cell ID seen in Complete Layer 3 Information in the VLR, so that it also shows in vty
143 * 'show' output. */
144 vsub->cgi = msc_a->via_cell;
145 }
146
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100147 /* Trigger transactions that we paged for */
148 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_PAGING_RESP) {
149 if (conn_accepted)
150 paging_response(msc_a);
151 else
152 paging_expired(vsub);
153 }
154
155 if (conn_accepted)
156 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, msc_a_vsub(msc_a));
157
158 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_LU)
159 msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING);
160}
161
162bool msc_a_is_accepted(const struct msc_a *msc_a)
163{
164 if (!msc_a || !msc_a->c.fi)
165 return false;
166 return msc_a->c.fi->state == MSC_A_ST_AUTHENTICATED
167 || msc_a->c.fi->state == MSC_A_ST_COMMUNICATING;
168}
169
170bool msc_a_in_release(struct msc_a *msc_a)
171{
172 if (!msc_a)
173 return true;
174 if (msc_a->c.fi->state == MSC_A_ST_RELEASING)
175 return true;
176 if (msc_a->c.fi->state == MSC_A_ST_RELEASED)
177 return true;
178 return false;
179}
180
181static int msc_a_ran_dec(struct msc_a *msc_a, const struct an_apdu *an_apdu, enum msc_role from_role)
182{
183 int rc;
184 struct msc_a_ran_dec_data d = {
185 .from_role = from_role,
186 .an_apdu = an_apdu,
187 };
188 msc_a_get(msc_a, __func__);
189 rc = msc_role_ran_decode(msc_a->c.fi, an_apdu, msc_a_ran_decode_cb, &d);
190 msc_a_put(msc_a, __func__);
191 return rc;
192};
193
194static void msc_a_fsm_validate_l3(struct osmo_fsm_inst *fi, uint32_t event, void *data)
195{
196 struct msc_a *msc_a = fi->priv;
197 const struct an_apdu *an_apdu;
198
199 switch (event) {
200 case MSC_A_EV_FROM_I_COMPLETE_LAYER_3:
201 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
202 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
203 an_apdu = data;
204 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
205 return;
206
207 case MSC_A_EV_COMPLETE_LAYER_3_OK:
208 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
209 return;
210
211 case MSC_A_EV_MO_CLOSE:
212 case MSC_A_EV_CN_CLOSE:
213 evaluate_acceptance_outcome(fi, false);
214 /* fall through */
215 case MSC_A_EV_UNUSED:
216 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
217 return;
218
219 default:
220 OSMO_ASSERT(false);
221 }
222}
223
224/* Figure out whether to first send a Classmark Request to the MS to figure out algorithm support. */
225static bool msc_a_need_classmark_for_ciphering(struct msc_a *msc_a)
226{
227 struct gsm_network *net = msc_a_net(msc_a);
228 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
229 int i = 0;
230 bool request_classmark = false;
231
232 /* Only on GERAN-A do we ever need Classmark Information for Ciphering. */
233 if (msc_a->c.ran->type != OSMO_RAT_GERAN_A)
234 return false;
235
236 for (i = 0; i < 8; i++) {
237 int supported;
238
239 /* A5/n permitted by osmo-msc.cfg? */
240 if (!(net->a5_encryption_mask & (1 << i)))
241 continue;
242
243 /* A5/n supported by MS? */
244 supported = osmo_gsm48_classmark_supports_a5(&vsub->classmark, i);
245 if (supported < 0) {
246 LOG_MSC_A(msc_a, LOGL_DEBUG, "For A5/%d, we still need Classmark %d\n", i, -supported);
247 request_classmark = true;
248 }
249 }
250
251 return request_classmark;
252}
253
254static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv);
255
256/* VLR callback for ops.set_ciph_mode() */
257int msc_a_vlr_set_cipher_mode(void *_msc_a, bool umts_aka, bool retrieve_imeisv)
258{
259 struct msc_a *msc_a = _msc_a;
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700260 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100261
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700262 if (!msc_a) {
263 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
264 "MSC-A role is NULL?!?\n");
265 return -EINVAL;
266 }
267
268 vsub = msc_a_vsub(msc_a);
269 if (!vsub || !vsub->last_tuple) {
270 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
271 "vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100272 return -EINVAL;
273 }
274
275 if (msc_a_need_classmark_for_ciphering(msc_a)) {
276 int rc;
277 struct ran_msg msg = {
278 .msg_type = RAN_MSG_CLASSMARK_REQUEST,
279 };
280 rc = msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
281 if (rc) {
282 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Classmark Request\n");
283 return -EIO;
284 }
285
286 msc_a->state_before_classmark_update = msc_a->c.fi->state;
287 msc_a->action_on_classmark_update = (struct msc_a_action_on_classmark_update){
288 .type = MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING,
289 .ciphering = {
290 .umts_aka = umts_aka,
291 .retrieve_imeisv = retrieve_imeisv,
292 },
293 };
294 msc_a_state_chg(msc_a, MSC_A_ST_WAIT_CLASSMARK_UPDATE);
295 return 0;
296 }
297
298 return msc_a_ran_enc_ciphering(msc_a, umts_aka, retrieve_imeisv);
299}
300
301static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv)
302{
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700303 struct gsm_network *net;
304 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100305 struct ran_msg msg;
306
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700307 if (!msc_a) {
308 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
309 "MSC-A role is NULL?!?\n");
310 return -EINVAL;
311 }
312
313 net = msc_a_net(msc_a);
314 vsub = msc_a_vsub(msc_a);
315
316 if (!net || !vsub || !vsub->last_tuple) {
317 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
318 "gsm_network and/or vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100319 return -EINVAL;
320 }
321
322 msg = (struct ran_msg){
323 .msg_type = RAN_MSG_CIPHER_MODE_COMMAND,
324 .cipher_mode_command = {
325 .vec = vsub->last_tuple ? &vsub->last_tuple->vec : NULL,
326 .classmark = &vsub->classmark,
327 .geran = {
328 .umts_aka = umts_aka,
329 .retrieve_imeisv = retrieve_imeisv,
330 .a5_encryption_mask = net->a5_encryption_mask,
331
332 /* for ran_a.c to store the GERAN key that is actually used */
333 .chosen_key = &msc_a->geran_encr,
334 },
335 },
336 };
337
338 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
339 LOG_MSC_A(msc_a, LOGL_ERROR, "Sending Cipher Mode Command failed\n");
340 /* Returning error to the VLR ops.set_ciph_mode() will cancel the attach. Other callers need to take
341 * care of the return value. */
342 return -EINVAL;
343 }
344
345 if (msc_a->geran_encr.key_len)
346 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN encoding chose ciphering key %s\n",
347 osmo_hexdump_nospc(msc_a->geran_encr.key, msc_a->geran_encr.key_len));
348 return 0;
349}
350
351static void msc_a_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
352{
353 struct msc_a *msc_a = fi->priv;
354
355 /* If accepted, transition the state, all other cases mean failure. */
356 switch (event) {
357 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
358 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
359 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
360 return;
361
362 case MSC_A_EV_AUTHENTICATED:
363 msc_a_state_chg(msc_a, MSC_A_ST_AUTHENTICATED);
364 return;
365
366 case MSC_A_EV_UNUSED:
367 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
368 return;
369
370 case MSC_A_EV_MO_CLOSE:
371 case MSC_A_EV_CN_CLOSE:
372 evaluate_acceptance_outcome(fi, false);
373 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
374 return;
375
376
377 default:
378 OSMO_ASSERT(false);
379 }
380}
381
382static void msc_a_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data)
383{
384 struct msc_a *msc_a = fi->priv;
385
386 switch (event) {
387 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
388 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
389 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
390 return;
391
392 case MSC_A_EV_CLASSMARK_UPDATE:
393 switch (msc_a->action_on_classmark_update.type) {
394 case MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING:
395 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
396 if (msc_a_ran_enc_ciphering(msc_a,
397 msc_a->action_on_classmark_update.ciphering.umts_aka,
398 msc_a->action_on_classmark_update.ciphering.retrieve_imeisv)) {
399 LOG_MSC_A(msc_a, LOGL_ERROR,
400 "After Classmark Update, still failed to send Cipher Mode Command\n");
401 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
402 }
403 return;
404
405 default:
406 LOG_MSC_A(msc_a, LOGL_ERROR, "Internal error: After Classmark Update, don't know what to do\n");
407 msc_a_state_chg(msc_a, msc_a->state_before_classmark_update);
408 return;
409 }
410
411 case MSC_A_EV_UNUSED:
412 /* Seems something detached / aborted in the middle of auth+ciph. */
413 evaluate_acceptance_outcome(fi, false);
414 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
415 return;
416
417 case MSC_A_EV_MO_CLOSE:
418 case MSC_A_EV_CN_CLOSE:
419 evaluate_acceptance_outcome(fi, false);
420 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
421 return;
422
423 default:
424 OSMO_ASSERT(false);
425 }
426}
427
428static bool msc_a_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
429{
430 struct msc_a *msc_a = fi->priv;
431 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
432 struct gsm_trans *trans;
433
434 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_SILENT_CALL)) {
435 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: silent call still active\n", __func__);
436 return true;
437 }
438
439 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC)) {
440 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO CC request after a CM Service Request\n",
441 __func__);
442 return true;
443 }
444 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SMS)) {
445 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SMS after a CM Service Request\n",
446 __func__);
447 return true;
448 }
449 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SS)) {
450 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SS after a CM Service Request\n",
451 __func__);
452 return true;
453 }
454
455 if (vsub && !llist_empty(&vsub->cs.requests)) {
456 struct paging_request *pr;
457 llist_for_each_entry(pr, &vsub->cs.requests, entry) {
458 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still active: %s\n", __func__, pr->label);
459 }
460 return true;
461 }
462
463 if ((trans = trans_has_conn(msc_a))) {
464 LOG_MSC_A(msc_a, LOGL_DEBUG, "connection still has active transaction: %s\n",
465 trans_type_name(trans->type));
466 return true;
467 }
468
469 return false;
470}
471
472static void msc_a_fsm_authenticated_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
473{
474 struct msc_a *msc_a = fi->priv;
475 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
476
477 /* Stop Location Update expiry for this subscriber. While the subscriber
478 * has an open connection the LU expiry timer must remain disabled.
479 * Otherwise we would kick the subscriber off the network when the timer
480 * expires e.g. during a long phone call.
481 * The LU expiry timer will restart once the connection is closed. */
482 if (vsub)
483 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
484
485 evaluate_acceptance_outcome(fi, true);
486}
487
488static void msc_a_fsm_authenticated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
489{
490 struct msc_a *msc_a = fi->priv;
491
492 switch (event) {
493 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
494 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
495 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
496 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
497 return;
498
499 case MSC_A_EV_COMPLETE_LAYER_3_OK:
500 /* When Authentication is off, we may already be in the Accepted state when the code
501 * evaluates the Compl L3. Simply ignore. This just cosmetically mutes the error log
502 * about the useless event. */
503 return;
504
505 case MSC_A_EV_TRANSACTION_ACCEPTED:
506 msc_a_state_chg(msc_a, MSC_A_ST_COMMUNICATING);
507 return;
508
509 case MSC_A_EV_MO_CLOSE:
510 case MSC_A_EV_CN_CLOSE:
511 case MSC_A_EV_UNUSED:
512 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
513 return;
514
515 default:
516 OSMO_ASSERT(false);
517 }
518}
519
520/* The MGW has given us a local IP address for the RAN side. Ready to start the Assignment of a voice channel. */
521static void msc_a_call_leg_ran_local_addr_available(struct msc_a *msc_a)
522{
523 struct ran_msg msg;
524 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
525 struct gsm0808_channel_type channel_type;
526
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100527 if (!cc_trans) {
528 LOG_MSC_A(msc_a, LOGL_ERROR, "No CC transaction active\n");
529 call_leg_release(msc_a->cc.call_leg);
530 return;
531 }
532
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100533 /* Once a CI is known, we could also CRCX the CN side of the MGW endpoint, but it makes sense to wait for the
534 * codec to be determined by the Assignment Complete message, first. */
535
536 if (mncc_bearer_cap_to_channel_type(&channel_type, &cc_trans->bearer_cap)) {
537 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose Channel Type from bearer capabilities\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200538 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100539 return;
540 }
541
542 /* The RAN side RTP address is known, so the voice Assignment can commence. */
543 msg = (struct ran_msg){
544 .msg_type = RAN_MSG_ASSIGNMENT_COMMAND,
545 .assignment_command = {
546 .cn_rtp = &msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local,
547 .channel_type = &channel_type,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200548 .osmux_present = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->use_osmux,
549 .osmux_cid = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local_osmux_cid,
Philipp Maierf34d9452020-06-05 15:49:35 +0200550 .call_id_present = true,
551 .call_id = cc_trans->callref,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100552 },
553 };
554 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
555 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Assignment\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200556 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100557 return;
558 }
559}
560
561static void msc_a_call_leg_cn_local_addr_available(struct msc_a *msc_a, struct gsm_trans *cc_trans)
562{
563 if (gsm48_tch_rtp_create(cc_trans)) {
564 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot inform MNCC of RTP address\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200565 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100566 return;
567 }
568}
569
570static struct gsm_trans *find_waiting_call(struct msc_a *msc_a)
571{
572 struct gsm_trans *trans;
573 struct gsm_network *net = msc_a_net(msc_a);
574
575 llist_for_each_entry(trans, &net->trans_list, entry) {
576 if (trans->msc_a != msc_a)
577 continue;
578 if (trans->type != TRANS_CC)
579 continue;
580 if (trans->msc_a->cc.active_trans == trans)
581 continue;
582 return trans;
583 }
584 return NULL;
585}
586
587static void msc_a_cleanup_rtp_streams(struct msc_a *msc_a, uint32_t event, void *data)
588{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100589 switch (event) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100590
591 case MSC_EV_CALL_LEG_TERM:
592 msc_a->cc.call_leg = NULL;
593 if (msc_a->cc.mncc_forwarding_to_remote_ran)
594 msc_a->cc.mncc_forwarding_to_remote_ran->rtps = NULL;
595
Neels Hofmeyr265a4c72019-05-09 16:20:51 +0200596 if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100597 msc_a->ho.new_cell.mncc_forwarding_to_remote_ran->rtps = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100598 return;
599
600 case MSC_MNCC_EV_CALL_ENDED:
601 msc_a->cc.mncc_forwarding_to_remote_ran = NULL;
602 return;
603
604 default:
605 return;
606 }
607}
608
609static void msc_a_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
610{
611 struct msc_a *msc_a = fi->priv;
612 struct rtp_stream *rtps;
613 struct gsm_trans *waiting_trans;
614 struct an_apdu *an_apdu;
615
616 msc_a_cleanup_rtp_streams(msc_a, event, data);
617
618 switch (event) {
619 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
620 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
621 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
622 an_apdu = data;
623 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
624 return;
625
626 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE:
627 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE:
628 case MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST:
629 case MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST:
630 an_apdu = data;
631 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_T);
632 return;
633
634 case MSC_A_EV_TRANSACTION_ACCEPTED:
635 /* no-op */
636 return;
637
638 case MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE:
639 rtps = data;
640 if (!rtps) {
641 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
642 return;
643 }
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100644 if (!msc_a->cc.call_leg) {
645 LOG_MSC_A(msc_a, LOGL_ERROR, "No call leg active\n");
646 return;
647 }
Neels Hofmeyrcc918cb2019-11-28 02:16:34 +0100648 if (!osmo_sockaddr_str_is_nonzero(&rtps->local)) {
649 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n",
650 OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local));
651 call_leg_release(msc_a->cc.call_leg);
652 return;
653 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100654 LOG_MSC_A(msc_a, LOGL_DEBUG,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200655 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
656 rtp_direction_name(rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local),
657 rtps->use_osmux ? "yes" : "no", rtps->local_osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100658 switch (rtps->dir) {
659 case RTP_TO_RAN:
660 msc_a_call_leg_ran_local_addr_available(msc_a);
661 return;
662 case RTP_TO_CN:
663 msc_a_call_leg_cn_local_addr_available(msc_a, rtps->for_trans);
664 return;
665 default:
666 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
667 return;
668 }
669
670 case MSC_EV_CALL_LEG_RTP_COMPLETE:
671 /* Nothing to do. */
672 return;
673
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100674 case MSC_MNCC_EV_CALL_ENDED:
675 /* Cleaned up above */
676 return;
677
678 case MSC_EV_CALL_LEG_TERM:
679 /* RTP streams cleaned up above */
680
681 msc_a_get(msc_a, __func__);
682 if (msc_a->cc.active_trans)
683 trans_free(msc_a->cc.active_trans);
684
685 /* If there is another call still waiting to be activated, this is the time when the mgcp_ctx is
686 * available again and the other call can start assigning. */
687 waiting_trans = find_waiting_call(msc_a);
688 if (waiting_trans) {
689 LOG_MSC_A(msc_a, LOGL_DEBUG, "(ti %02x) Call waiting: starting Assignment\n",
690 waiting_trans->transaction_id);
691 msc_a_try_call_assignment(waiting_trans);
692 }
693 msc_a_put(msc_a, __func__);
694 return;
695
696 case MSC_A_EV_HANDOVER_REQUIRED:
697 msc_ho_start(msc_a, (struct ran_handover_required*)data);
698 return;
699
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200700 case MSC_A_EV_HANDOVER_END:
701 /* Termination event of the msc_ho_fsm. No action needed, it's all done in the msc_ho_fsm cleanup. This
702 * event only exists because osmo_fsm_inst_alloc_child() requires a parent term event; and maybe
703 * interesting for logging. */
704 return;
705
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100706 case MSC_A_EV_MO_CLOSE:
707 case MSC_A_EV_CN_CLOSE:
708 case MSC_A_EV_UNUSED:
709 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
710 return;
711
712 default:
713 OSMO_ASSERT(false);
714 }
715}
716
717static int msc_a_fsm_timer_cb(struct osmo_fsm_inst *fi)
718{
719 struct msc_a *msc_a = fi->priv;
720 if (msc_a_in_release(msc_a)) {
721 LOG_MSC_A(msc_a, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
722 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
723 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
724 } else {
725 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
726 osmo_fsm_inst_dispatch(fi, MSC_A_EV_CN_CLOSE, &cause);
727 }
728 return 0;
729}
730
731static void msc_a_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
732{
733 struct msc_a *msc_a = fi->priv;
734 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
735 int i;
736 char buf[128];
737 const char * const use_counts_to_cancel[] = {
738 MSC_A_USE_LOCATION_UPDATING,
739 MSC_A_USE_CM_SERVICE_CC,
740 MSC_A_USE_CM_SERVICE_SMS,
741 MSC_A_USE_CM_SERVICE_SS,
742 MSC_A_USE_PAGING_RESPONSE,
743 };
744
745 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: msc_a use is %s\n",
746 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
747
748 if (vsub) {
749 vlr_subscr_get(vsub, __func__);
750
751 /* Cancel all VLR FSMs, if any */
752 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
753
754 /* The subscriber has no active connection anymore.
755 * Restart the periodic Location Update expiry timer for this subscriber. */
756 vlr_subscr_enable_expire_lu(vsub);
757 }
758
759 /* If we're closing in a middle of a trans, we need to clean up */
760 trans_conn_closed(msc_a);
761
762 call_leg_release(msc_a->cc.call_leg);
763
764 /* Cancel use counts for pending CM Service / Paging */
765 for (i = 0; i < ARRAY_SIZE(use_counts_to_cancel); i++) {
766 const char *use = use_counts_to_cancel[i];
767 int32_t count = osmo_use_count_by(&msc_a->use_count, use);
768 if (!count)
769 continue;
770 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: canceling still pending use: %s (%d)\n", use, count);
771 osmo_use_count_get_put(&msc_a->use_count, use, -count);
772 }
773
774 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
775 sgs_iface_tx_release(vsub);
776 /* In SGsAP there is no confirmation of a release. */
777 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
778 } else {
779 struct ran_msg msg = {
780 .msg_type = RAN_MSG_CLEAR_COMMAND,
781 .clear_command = {
782 .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED),
783 },
784 };
785 msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
786 msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Philipp Maier47cf84d2019-08-15 14:56:54 +0200787
788 /* The connection is cleared. The MS will now go back to 4G,
789 Switch the RAN type back to SGS. */
790 if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
791 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100792 }
793
794 if (vsub)
795 vlr_subscr_put(vsub, __func__);
796}
797
798static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
799{
800 struct msc_a *msc_a = fi->priv;
801
802 msc_a_cleanup_rtp_streams(msc_a, event, data);
803
804 switch (event) {
805 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
806 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
807 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
808 return;
809
810 case MSC_A_EV_MO_CLOSE:
811 case MSC_A_EV_CN_CLOSE:
812 case MSC_A_EV_UNUSED:
813 /* Already releasing */
814 return;
815
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100816 case MSC_EV_CALL_LEG_TERM:
817 case MSC_MNCC_EV_CALL_ENDED:
818 /* RTP streams cleaned up above */
819 return;
820
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200821 case MSC_A_EV_HANDOVER_END:
822 /* msc_ho_fsm does cleanup. */
823 return;
824
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100825 default:
826 OSMO_ASSERT(false);
827 }
828}
829
830
831static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
832{
833 struct msc_a *msc_a = msc_a_fi_priv(fi);
834 char buf[128];
835 LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n",
836 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
837 if (osmo_use_count_total(&msc_a->use_count) == 0)
838 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
839}
840
841static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
842{
843 if (event == MSC_A_EV_UNUSED)
844 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
845}
846
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100847void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
848{
849 struct msc_a *msc_a = msc_a_fi_priv(fi);
850
851 trans_conn_closed(msc_a);
852
853 if (msc_a_fsm_has_active_transactions(fi))
854 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
855
856 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
857}
858
859const struct value_string msc_a_fsm_event_names[] = {
860 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
861 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
862 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100863 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
864 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
865 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
866 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
867 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
868 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
869 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
870 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
871 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
872 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
873 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
874 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
875 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
876 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
877 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
878 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
879 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
880 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
881 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
882 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
883 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
884 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
885 {}
886};
887
888#define S(x) (1 << (x))
889
890static const struct osmo_fsm_state msc_a_fsm_states[] = {
891 [MSC_A_ST_VALIDATE_L3] = {
892 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
893 .in_event_mask = 0
894 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
895 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
896 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
897 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
898 | S(MSC_A_EV_MO_CLOSE)
899 | S(MSC_A_EV_CN_CLOSE)
900 | S(MSC_A_EV_UNUSED)
901 ,
902 .out_state_mask = 0
903 | S(MSC_A_ST_VALIDATE_L3)
904 | S(MSC_A_ST_AUTH_CIPH)
905 | S(MSC_A_ST_RELEASING)
906 ,
907 .action = msc_a_fsm_validate_l3,
908 },
909 [MSC_A_ST_AUTH_CIPH] = {
910 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
911 .in_event_mask = 0
912 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
913 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
914 | S(MSC_A_EV_AUTHENTICATED)
915 | S(MSC_A_EV_MO_CLOSE)
916 | S(MSC_A_EV_CN_CLOSE)
917 | S(MSC_A_EV_UNUSED)
918 ,
919 .out_state_mask = 0
920 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
921 | S(MSC_A_ST_AUTHENTICATED)
922 | S(MSC_A_ST_RELEASING)
923 ,
924 .action = msc_a_fsm_auth_ciph,
925 },
926 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
927 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
928 .in_event_mask = 0
929 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
930 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
931 | S(MSC_A_EV_CLASSMARK_UPDATE)
932 | S(MSC_A_EV_MO_CLOSE)
933 | S(MSC_A_EV_CN_CLOSE)
934 ,
935 .out_state_mask = 0
936 | S(MSC_A_ST_AUTH_CIPH)
937 | S(MSC_A_ST_RELEASING)
938 ,
939 .action = msc_a_fsm_wait_classmark_update,
940 },
941 [MSC_A_ST_AUTHENTICATED] = {
942 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
943 /* allow everything to release for any odd behavior */
944 .in_event_mask = 0
945 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
946 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
947 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
948 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
949 | S(MSC_A_EV_MO_CLOSE)
950 | S(MSC_A_EV_CN_CLOSE)
951 | S(MSC_A_EV_UNUSED)
952 ,
953 .out_state_mask = 0
954 | S(MSC_A_ST_RELEASING)
955 | S(MSC_A_ST_COMMUNICATING)
956 ,
957 .onenter = msc_a_fsm_authenticated_enter,
958 .action = msc_a_fsm_authenticated,
959 },
960 [MSC_A_ST_COMMUNICATING] = {
961 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
962 /* allow everything to release for any odd behavior */
963 .in_event_mask = 0
964 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
965 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
966 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
967 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
968 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
969 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
970 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
971 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
972 | S(MSC_A_EV_MO_CLOSE)
973 | S(MSC_A_EV_CN_CLOSE)
974 | S(MSC_A_EV_UNUSED)
975 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
976 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100977 | S(MSC_EV_CALL_LEG_TERM)
978 | S(MSC_MNCC_EV_CALL_ENDED)
979 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200980 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100981 ,
982 .out_state_mask = 0
983 | S(MSC_A_ST_RELEASING)
984 ,
985 .action = msc_a_fsm_communicating,
986 },
987 [MSC_A_ST_RELEASING] = {
988 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
989 .in_event_mask = 0
990 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
991 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
992 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100993 | S(MSC_EV_CALL_LEG_TERM)
994 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200995 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100996 ,
997 .out_state_mask = 0
998 | S(MSC_A_ST_RELEASED)
999 ,
1000 .onenter = msc_a_fsm_releasing_onenter,
1001 .action = msc_a_fsm_releasing,
1002 },
1003 [MSC_A_ST_RELEASED] = {
1004 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
1005 .in_event_mask = 0
1006 | S(MSC_A_EV_UNUSED)
1007 ,
1008 .onenter = msc_a_fsm_released_onenter,
1009 .action = msc_a_fsm_released,
1010 },
1011};
1012
1013static struct osmo_fsm msc_a_fsm = {
1014 .name = "msc_a",
1015 .states = msc_a_fsm_states,
1016 .num_states = ARRAY_SIZE(msc_a_fsm_states),
1017 .log_subsys = DMSC,
1018 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001019 .timer_cb = msc_a_fsm_timer_cb,
1020 .cleanup = msc_a_fsm_cleanup,
1021};
1022
1023static __attribute__((constructor)) void msc_a_fsm_init()
1024{
1025 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1026}
1027
1028static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1029{
1030 struct msc_a *msc_a = e->use_count->talloc_object;
1031 char buf[128];
1032 int32_t total;
1033 int level;
1034
1035 if (!e->use)
1036 return -EINVAL;
1037
1038 total = osmo_use_count_total(&msc_a->use_count);
1039
1040 if (total == 0
1041 || (total == 1 && old_use_count == 0 && e->count == 1))
1042 level = LOGL_INFO;
1043 else
1044 level = LOGL_DEBUG;
1045
1046 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1047 (e->count - old_use_count) > 0? "+" : "-", e->use,
1048 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1049
1050 if (e->count < 0)
1051 return -ERANGE;
1052
1053 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1054
1055 if (total == 0)
1056 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1057 return 0;
1058}
1059
1060struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1061{
1062 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1063 msc_a->use_count = (struct osmo_use_count){
1064 .talloc_object = msc_a,
1065 .use_cb = msc_a_use_cb,
1066 };
1067 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1068 /* Start timeout for first state */
Neels Hofmeyr01653252019-09-03 02:06:22 +02001069 msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001070 return msc_a;
1071}
1072
1073bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1074{
1075 if (!msc_a || !msc_a->c.fi)
1076 return false;
1077 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1078}
1079
1080const struct value_string complete_layer3_type_names[] = {
1081 { COMPLETE_LAYER3_NONE, "NONE" },
1082 { COMPLETE_LAYER3_LU, "LU" },
1083 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1084 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
1085 { 0, NULL }
1086};
1087
1088#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1089 do { \
1090 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1091 ## ARGS, \
1092 msub_ran_conn_name(msc_a->c.msub), \
1093 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1094 == 0) { \
1095 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1096 if (_vsub) { \
1097 if (_vsub->lu_fsm) \
1098 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1099 if (_vsub->auth_fsm) \
1100 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1101 if (_vsub->proc_arq_fsm) \
1102 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1103 } \
1104 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1105 } \
1106 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1107 } while (0)
1108
1109
1110/* Compose an ID almost like gsm48_mi_to_string(), but print the MI type along, and print a TMSI as hex. */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001111void msc_a_update_id_from_mi(struct msc_a *msc_a, const struct osmo_mobile_identity *mi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001112{
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001113 _msc_a_update_id(msc_a, "%s", osmo_mobile_identity_to_str_c(OTC_SELECT, mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001114}
1115
1116/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1117void msc_a_update_id(struct msc_a *msc_a)
1118{
1119 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1120}
1121
1122/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1123 * instances. */
1124void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1125{
1126 struct msub *msub;
1127 llist_for_each_entry(msub, &msub_list, entry) {
1128 struct vlr_subscr *vsub = msub_vsub(msub);
1129 if (vsub != for_vsub)
1130 continue;
1131 msc_a_update_id(msub_msc_a(msub));
1132 }
1133}
1134
1135static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1136{
1137 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1138 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1139
1140 switch (pdisc) {
1141 case GSM48_PDISC_MM:
1142 switch (msg_type) {
1143 case GSM48_MT_MM_LOC_UPD_REQUEST:
1144 case GSM48_MT_MM_CM_SERV_REQ:
1145 case GSM48_MT_MM_CM_REEST_REQ:
1146 case GSM48_MT_MM_AUTH_RESP:
1147 case GSM48_MT_MM_AUTH_FAIL:
1148 case GSM48_MT_MM_ID_RESP:
1149 case GSM48_MT_MM_TMSI_REALL_COMPL:
1150 case GSM48_MT_MM_IMSI_DETACH_IND:
1151 return true;
1152 default:
1153 break;
1154 }
1155 break;
1156 case GSM48_PDISC_RR:
1157 switch (msg_type) {
1158 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1159 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1160 case GSM48_MT_RR_PAG_RESP:
1161 case GSM48_MT_RR_CIPH_M_COMPL:
1162 return true;
1163 default:
1164 break;
1165 }
1166 break;
1167 default:
1168 break;
1169 }
1170
1171 return false;
1172}
1173
1174/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1175int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1176{
1177 struct gsm48_hdr *gh;
1178 uint8_t pdisc;
1179 int rc;
1180 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1181 int is_r99;
1182
1183 OSMO_ASSERT(msg->l3h);
1184 OSMO_ASSERT(msg);
1185
1186 gh = msgb_l3(msg);
1187 pdisc = gsm48_hdr_pdisc(gh);
1188
1189 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1190 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1191
1192 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1193 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1194 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1195 if (vsub)
1196 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1197 else
1198 is_r99 = compl_l3_msg_is_r99(msg);
1199
1200 if (is_r99 < 0) {
1201 LOG_MSC_A(msc_a, LOGL_ERROR,
1202 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1203 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1204 return -EACCES;
1205 }
1206
1207 if (is_r99 >= 0
1208 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1209 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1210 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1211 return 0;
1212 }
1213
1214 if (!msc_a_is_accepted(msc_a)
1215 && !msg_is_initially_permitted(gh)) {
1216 LOG_MSC_A(msc_a, LOGL_ERROR,
1217 "Message not permitted for initial conn: %s\n",
1218 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1219 return -EACCES;
1220 }
1221
1222 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1223 LOG_MSC_A(msc_a, LOGL_ERROR,
1224 "Illegal situation: RAN type mismatch:"
1225 " attached via %s, received message via %s\n",
1226 osmo_rat_type_name(vsub->cs.attached_via_ran),
1227 osmo_rat_type_name(msc_a->c.ran->type));
1228 return -EACCES;
1229 }
1230
1231#if 0
1232 if (silent_call_reroute(conn, msg))
1233 return silent_call_rx(conn, msg);
1234#endif
1235
1236 switch (pdisc) {
1237 case GSM48_PDISC_CC:
1238 rc = gsm0408_rcv_cc(msc_a, msg);
1239 break;
1240 case GSM48_PDISC_MM:
1241 rc = gsm0408_rcv_mm(msc_a, msg);
1242 break;
1243 case GSM48_PDISC_RR:
1244 rc = gsm0408_rcv_rr(msc_a, msg);
1245 break;
1246 case GSM48_PDISC_SMS:
1247 rc = gsm0411_rcv_sms(msc_a, msg);
1248 break;
1249 case GSM48_PDISC_MM_GPRS:
1250 case GSM48_PDISC_SM_GPRS:
1251 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1252 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1253 rc = -ENOTSUP;
1254 break;
1255 case GSM48_PDISC_NC_SS:
1256 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1257 break;
1258 case GSM48_PDISC_TEST:
1259 rc = gsm0414_rcv_test(msc_a, msg);
1260 break;
1261 default:
1262 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1263 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1264 rc = -EINVAL;
1265 break;
1266 }
1267
1268 return rc;
1269}
1270
1271static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1272{
1273 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
1274 struct rtp_stream *rtps_to_ran = msc_a->cc.call_leg ? msc_a->cc.call_leg->rtp[RTP_TO_RAN] : NULL;
1275
1276 if (!rtps_to_ran) {
1277 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1278 return;
1279 }
1280 if (!cc_trans) {
1281 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but CC transaction is active\n");
1282 return;
1283 }
1284
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001285 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1286 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1287 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1288 call_leg_release(msc_a->cc.call_leg);
1289 return;
1290 }
1291
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001292 /* Update RAN-side endpoint CI: */
1293 rtp_stream_set_codec(rtps_to_ran, ac->assignment_complete.codec);
1294 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001295 if (rtps_to_ran->use_osmux)
1296 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1297 ac->assignment_complete.osmux_cid);
1298
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001299 rtp_stream_commit(rtps_to_ran);
1300
1301 /* Setup CN side endpoint CI:
1302 * Now that
1303 * - the first CI has been created and a definitive endpoint name is assigned to the call_leg's MGW
1304 * endpoint,
1305 * - the Assignment has chosen a speech codec
1306 * go on to create the CN side RTP stream's CI. */
1307 if (call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_CN, cc_trans->callref, cc_trans,
1308 &ac->assignment_complete.codec, NULL)) {
1309 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Error creating MGW CI towards CN\n");
1310 call_leg_release(msc_a->cc.call_leg);
1311 return;
1312 }
1313}
1314
1315static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1316{
1317 struct gsm_trans *trans;
1318
1319 /* For a normal voice call, there will be an rtp_stream FSM. */
1320 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1321 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1322 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1323 return;
1324 }
1325
1326 /* Otherwise, a silent call might be active */
1327 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1328 if (trans) {
1329 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1330 trans_free(trans);
1331 return;
1332 }
1333
1334 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1335 msc_a_release_cn(msc_a);
1336}
1337
1338static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1339 struct osmo_gsm48_classmark *dst)
1340{
1341 if (!dst) {
1342 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1343
1344 if (!vsub)
1345 dst = &msc_a->temporary_classmark;
1346 else
1347 dst = &vsub->classmark;
1348 }
1349
Martin Hauke3f07dac2019-11-14 17:49:08 +01001350 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001351 osmo_gsm48_classmark_a5_name(classmark));
1352 osmo_gsm48_classmark_update(dst, classmark);
1353
1354 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1355 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1356 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1357}
1358
1359static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1360{
1361 int sapi = msg->sapi_n_reject.dlci & 0x7;
1362 if (sapi == UM_SAPI_SMS)
1363 gsm411_sapi_n_reject(msc_a);
1364}
1365
1366static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1367{
1368 if (!msc_a->ho.fi) {
1369 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1370 return -EINVAL;
1371 }
1372 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1373}
1374
1375int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1376{
1377 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1378 const struct ran_msg *msg = d->ran_dec;
1379 int rc = -99;
1380
1381 switch (msg->msg_type) {
1382
1383 case RAN_MSG_COMPL_L3:
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001384 /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code
1385 * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001386 msc_a->via_cell = (struct osmo_cell_global_id){
1387 .lai.plmn = msc_a_net(msc_a)->plmn,
1388 };
1389 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
1390 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1391 if (!rc) {
1392 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1393 if (conn)
1394 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1395 }
1396 break;
1397
1398 case RAN_MSG_DTAP:
1399 rc = msc_a_up_l3(msc_a, msg->dtap);
1400 break;
1401
1402 case RAN_MSG_CLEAR_REQUEST:
1403 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1404 break;
1405
1406 case RAN_MSG_CLEAR_COMPLETE:
1407 switch (msc_a->c.fi->state) {
1408 case MSC_A_ST_RELEASING:
1409 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1410 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1411 break;
1412 case MSC_A_ST_RELEASED:
1413 break;
1414 default:
1415 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1416 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1417 break;
1418 }
1419 rc = 0;
1420 break;
1421
1422 case RAN_MSG_CLASSMARK_UPDATE:
1423 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1424 rc = 0;
1425 break;
1426
1427 case RAN_MSG_CIPHER_MODE_COMPLETE:
1428 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1429 if (msg->cipher_mode_complete.alg_id) {
1430 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1431 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1432 msc_a->geran_encr.alg_id - 1);
1433 };
1434 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1435 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001436
1437 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1438 if (msg->cipher_mode_complete.l3_msg) {
1439 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1440 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1441 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1442 unsigned char *pos = msgb_put(dtap, len);
1443 memcpy(pos, data, len);
1444 dtap->l3h = pos;
1445 rc = msc_a_up_l3(msc_a, dtap);
1446 msgb_free(dtap);
1447 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001448 break;
1449
1450 case RAN_MSG_CIPHER_MODE_REJECT:
1451 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1452 rc = 0;
1453 break;
1454
1455 case RAN_MSG_ASSIGNMENT_COMPLETE:
1456 msc_a_up_call_assignment_complete(msc_a, msg);
1457 rc = 0;
1458 break;
1459
1460 case RAN_MSG_ASSIGNMENT_FAILURE:
1461 msc_a_up_call_assignment_failure(msc_a, msg);
1462 rc = 0;
1463 break;
1464
1465 case RAN_MSG_SAPI_N_REJECT:
1466 msc_a_up_sapi_n_reject(msc_a, msg);
1467 rc = 0;
1468 break;
1469
1470 case RAN_MSG_HANDOVER_PERFORMED:
1471 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1472 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1473 break;
1474
1475 case RAN_MSG_HANDOVER_REQUIRED:
1476 /* The BSS lets us know that it wants to handover to a different cell */
1477 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1478 break;
1479
1480 case RAN_MSG_HANDOVER_FAILURE:
1481 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1482 break;
1483
1484 default:
1485 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1486 rc = -ENOTSUP;
1487 break;
1488 }
1489 return rc;
1490}
1491
1492static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1493{
1494 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1495 int rc = -99;
1496
1497 if (!msc_t) {
1498 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1499 return -EINVAL;
1500 }
1501
1502 OSMO_ASSERT(d->ran_dec);
1503
1504 switch (d->ran_dec->msg_type) {
1505
1506 case RAN_MSG_CLEAR_REQUEST:
1507 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1508 break;
1509
1510 case RAN_MSG_CLEAR_COMPLETE:
1511 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1512 break;
1513
1514 case RAN_MSG_CLASSMARK_UPDATE:
1515 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1516 rc = 0;
1517 break;
1518
1519 case RAN_MSG_HANDOVER_REQUEST_ACK:
1520 /* new BSS accepts Handover */
1521 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1522 break;
1523
1524 case RAN_MSG_HANDOVER_DETECT:
1525 /* new BSS signals the MS is DETECTed on the new lchan */
1526 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1527 break;
1528
1529 case RAN_MSG_HANDOVER_COMPLETE:
1530 /* new BSS signals the MS has fully moved to the new lchan */
1531 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1532 break;
1533
1534 case RAN_MSG_HANDOVER_FAILURE:
1535 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1536 break;
1537
1538 default:
1539 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1540 ran_msg_type_name(d->ran_dec->msg_type));
1541 rc = -ENOTSUP;
1542 break;
1543 }
1544 return rc;
1545}
1546
1547int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1548{
1549 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1550 struct msc_a_ran_dec_data *d = data;
1551 int rc = -99;
1552
1553 d->ran_dec = msg;
1554
1555 switch (d->from_role) {
1556 case MSC_ROLE_I:
1557 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1558 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1559 break;
1560
1561 case MSC_ROLE_T:
1562 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1563 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1564 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1565 break;
1566
1567 default:
1568 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1569 ran_msg_type_name(msg->msg_type));
1570 return -ENOTSUP;
1571 }
1572
1573 if (rc)
1574 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),
1575 msc_role_name(d->from_role));
1576 return rc;
1577}
1578
1579/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1580int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1581 const char *file, int line)
1582{
1583 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1584 ran_msg, file, line);
1585}
1586
1587/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1588 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1589int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1590 const struct ran_msg *ran_msg,
1591 const char *file, int line)
1592{
1593 struct an_apdu an_apdu = {
1594 .an_proto = msc_a->c.ran->an_proto,
1595 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1596 };
1597 int rc;
1598 if (!an_apdu.msg)
1599 return -EIO;
1600 rc = _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
1601 msgb_free(an_apdu.msg);
1602 return rc;
1603}
1604
1605int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
1606{
1607 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001608 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
1609 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001610
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001611 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001612 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
1613 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1614 msgb_free(dtap);
1615 return -EIO;
1616 }
1617
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001618 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
1619 /* The SGs connection to the MME always is at the MSC-A. */
1620 return sgs_iface_tx_dtap_ud(msc_a, dtap);
1621 }
1622
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001623 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
1624 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1625
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001626 ran_msg = (struct ran_msg){
1627 .msg_type = RAN_MSG_DTAP,
1628 .dtap = dtap,
1629 };
1630 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
1631}
1632
1633struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
1634{
1635 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
1636 if (valid_conn_only && !msc_a_is_accepted(msc_a))
1637 return NULL;
1638 return msc_a;
1639}
1640
1641int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
1642{
1643 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1644 struct ran_msg msg = {
1645 .msg_type = RAN_MSG_COMMON_ID,
1646 .common_id = {
1647 .imsi = vsub->imsi,
1648 },
1649 };
1650
1651 return msc_a_ran_down(msc_a, to_role, &msg);
1652}
1653
1654static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
1655{
1656 struct call_leg *cl = msc_a->cc.call_leg;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001657 struct msc_i *msc_i = msc_a_msc_i(msc_a);
1658 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001659
1660 OSMO_ASSERT(!msc_a->cc.active_trans);
1661 msc_a->cc.active_trans = cc_trans;
1662
1663 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
1664
1665 if (!cl) {
1666 cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi,
1667 MSC_EV_CALL_LEG_TERM,
1668 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
Neels Hofmeyr265a4c72019-05-09 16:20:51 +02001669 MSC_EV_CALL_LEG_RTP_COMPLETE);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001670 OSMO_ASSERT(cl);
1671
Martin Hauke3f07dac2019-11-14 17:49:08 +01001672 /* HACK: We put the connection in loopback mode at the beginning to
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001673 * trick the hNodeB into doing the IuUP negotiation with itself.
1674 * This is a hack we need because osmo-mgw does not support IuUP yet, see OS#2459. */
1675 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU)
1676 cl->crcx_conn_mode[RTP_TO_RAN] = MGCP_CONN_LOOPBACK;
1677 }
1678
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001679 if (net->use_osmux != OSMUX_USAGE_OFF) {
1680 msc_i = msc_a_msc_i(msc_a);
1681 if (msc_i->c.remote_to) {
1682 /* TODO: investigate what to do in this case */
1683 LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC");
1684 } else {
1685 cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux;
1686 }
1687 }
1688
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001689 /* This will lead to either MSC_EV_CALL_LEG_LOCAL_ADDR_AVAILABLE or MSC_EV_CALL_LEG_TERM.
1690 * If the local address is already known, then immediately trigger. */
1691 if (call_leg_local_ip(cl, RTP_TO_RAN))
1692 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
1693 else
1694 return call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_RAN, cc_trans->callref, cc_trans, NULL, NULL);
1695}
1696
1697int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
1698{
1699 struct msc_a *msc_a = cc_trans->msc_a;
1700 OSMO_ASSERT(cc_trans->type == TRANS_CC);
1701
1702 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02001703 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001704 return 0;
1705 }
1706
1707 if (msc_a->cc.active_trans) {
1708 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
1709 return 0;
1710 }
1711
1712 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
1713 return msc_a_start_assignment(msc_a, cc_trans);
1714}
1715
1716const char *msc_a_cm_service_type_to_use(enum osmo_cm_service_type cm_service_type)
1717{
1718 switch (cm_service_type) {
1719 case GSM48_CMSERV_MO_CALL_PACKET:
1720 case GSM48_CMSERV_EMERGENCY:
1721 return MSC_A_USE_CM_SERVICE_CC;
1722
1723 case GSM48_CMSERV_SMS:
1724 return MSC_A_USE_CM_SERVICE_SMS;
1725
1726 case GSM48_CMSERV_SUP_SERV:
1727 return MSC_A_USE_CM_SERVICE_SS;
1728
1729 default:
1730 return NULL;
1731 }
1732}
1733
1734void msc_a_release_cn(struct msc_a *msc_a)
1735{
1736 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
1737}
1738
1739void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
1740{
1741 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1742}