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