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