blob: a4048f4523540664b9bd73c1d9ed7fb0b864bf9e [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
521 /* Once a CI is known, we could also CRCX the CN side of the MGW endpoint, but it makes sense to wait for the
522 * codec to be determined by the Assignment Complete message, first. */
523
524 if (mncc_bearer_cap_to_channel_type(&channel_type, &cc_trans->bearer_cap)) {
525 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose Channel Type from bearer capabilities\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200526 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100527 return;
528 }
529
530 /* The RAN side RTP address is known, so the voice Assignment can commence. */
531 msg = (struct ran_msg){
532 .msg_type = RAN_MSG_ASSIGNMENT_COMMAND,
533 .assignment_command = {
534 .cn_rtp = &msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local,
535 .channel_type = &channel_type,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200536 .osmux_present = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->use_osmux,
537 .osmux_cid = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local_osmux_cid,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100538 },
539 };
540 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
541 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Assignment\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200542 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100543 return;
544 }
545}
546
547static void msc_a_call_leg_cn_local_addr_available(struct msc_a *msc_a, struct gsm_trans *cc_trans)
548{
549 if (gsm48_tch_rtp_create(cc_trans)) {
550 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot inform MNCC of RTP address\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200551 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100552 return;
553 }
554}
555
556static struct gsm_trans *find_waiting_call(struct msc_a *msc_a)
557{
558 struct gsm_trans *trans;
559 struct gsm_network *net = msc_a_net(msc_a);
560
561 llist_for_each_entry(trans, &net->trans_list, entry) {
562 if (trans->msc_a != msc_a)
563 continue;
564 if (trans->type != TRANS_CC)
565 continue;
566 if (trans->msc_a->cc.active_trans == trans)
567 continue;
568 return trans;
569 }
570 return NULL;
571}
572
573static void msc_a_cleanup_rtp_streams(struct msc_a *msc_a, uint32_t event, void *data)
574{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100575 switch (event) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100576
577 case MSC_EV_CALL_LEG_TERM:
578 msc_a->cc.call_leg = NULL;
579 if (msc_a->cc.mncc_forwarding_to_remote_ran)
580 msc_a->cc.mncc_forwarding_to_remote_ran->rtps = NULL;
581
Neels Hofmeyr265a4c72019-05-09 16:20:51 +0200582 if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100583 msc_a->ho.new_cell.mncc_forwarding_to_remote_ran->rtps = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100584 return;
585
586 case MSC_MNCC_EV_CALL_ENDED:
587 msc_a->cc.mncc_forwarding_to_remote_ran = NULL;
588 return;
589
590 default:
591 return;
592 }
593}
594
595static void msc_a_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
596{
597 struct msc_a *msc_a = fi->priv;
598 struct rtp_stream *rtps;
599 struct gsm_trans *waiting_trans;
600 struct an_apdu *an_apdu;
601
602 msc_a_cleanup_rtp_streams(msc_a, event, data);
603
604 switch (event) {
605 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
606 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
607 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
608 an_apdu = data;
609 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
610 return;
611
612 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE:
613 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE:
614 case MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST:
615 case MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST:
616 an_apdu = data;
617 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_T);
618 return;
619
620 case MSC_A_EV_TRANSACTION_ACCEPTED:
621 /* no-op */
622 return;
623
624 case MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE:
625 rtps = data;
626 if (!rtps) {
627 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
628 return;
629 }
Neels Hofmeyrcc918cb2019-11-28 02:16:34 +0100630 if (!osmo_sockaddr_str_is_nonzero(&rtps->local)) {
631 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n",
632 OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local));
633 call_leg_release(msc_a->cc.call_leg);
634 return;
635 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100636 LOG_MSC_A(msc_a, LOGL_DEBUG,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200637 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
638 rtp_direction_name(rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local),
639 rtps->use_osmux ? "yes" : "no", rtps->local_osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100640 switch (rtps->dir) {
641 case RTP_TO_RAN:
642 msc_a_call_leg_ran_local_addr_available(msc_a);
643 return;
644 case RTP_TO_CN:
645 msc_a_call_leg_cn_local_addr_available(msc_a, rtps->for_trans);
646 return;
647 default:
648 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
649 return;
650 }
651
652 case MSC_EV_CALL_LEG_RTP_COMPLETE:
653 /* Nothing to do. */
654 return;
655
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100656 case MSC_MNCC_EV_CALL_ENDED:
657 /* Cleaned up above */
658 return;
659
660 case MSC_EV_CALL_LEG_TERM:
661 /* RTP streams cleaned up above */
662
663 msc_a_get(msc_a, __func__);
664 if (msc_a->cc.active_trans)
665 trans_free(msc_a->cc.active_trans);
666
667 /* If there is another call still waiting to be activated, this is the time when the mgcp_ctx is
668 * available again and the other call can start assigning. */
669 waiting_trans = find_waiting_call(msc_a);
670 if (waiting_trans) {
671 LOG_MSC_A(msc_a, LOGL_DEBUG, "(ti %02x) Call waiting: starting Assignment\n",
672 waiting_trans->transaction_id);
673 msc_a_try_call_assignment(waiting_trans);
674 }
675 msc_a_put(msc_a, __func__);
676 return;
677
678 case MSC_A_EV_HANDOVER_REQUIRED:
679 msc_ho_start(msc_a, (struct ran_handover_required*)data);
680 return;
681
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200682 case MSC_A_EV_HANDOVER_END:
683 /* Termination event of the msc_ho_fsm. No action needed, it's all done in the msc_ho_fsm cleanup. This
684 * event only exists because osmo_fsm_inst_alloc_child() requires a parent term event; and maybe
685 * interesting for logging. */
686 return;
687
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100688 case MSC_A_EV_MO_CLOSE:
689 case MSC_A_EV_CN_CLOSE:
690 case MSC_A_EV_UNUSED:
691 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
692 return;
693
694 default:
695 OSMO_ASSERT(false);
696 }
697}
698
699static int msc_a_fsm_timer_cb(struct osmo_fsm_inst *fi)
700{
701 struct msc_a *msc_a = fi->priv;
702 if (msc_a_in_release(msc_a)) {
703 LOG_MSC_A(msc_a, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
704 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
705 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
706 } else {
707 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
708 osmo_fsm_inst_dispatch(fi, MSC_A_EV_CN_CLOSE, &cause);
709 }
710 return 0;
711}
712
713static void msc_a_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
714{
715 struct msc_a *msc_a = fi->priv;
716 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
717 int i;
718 char buf[128];
719 const char * const use_counts_to_cancel[] = {
720 MSC_A_USE_LOCATION_UPDATING,
721 MSC_A_USE_CM_SERVICE_CC,
722 MSC_A_USE_CM_SERVICE_SMS,
723 MSC_A_USE_CM_SERVICE_SS,
724 MSC_A_USE_PAGING_RESPONSE,
725 };
726
727 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: msc_a use is %s\n",
728 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
729
730 if (vsub) {
731 vlr_subscr_get(vsub, __func__);
732
733 /* Cancel all VLR FSMs, if any */
734 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
735
736 /* The subscriber has no active connection anymore.
737 * Restart the periodic Location Update expiry timer for this subscriber. */
738 vlr_subscr_enable_expire_lu(vsub);
739 }
740
741 /* If we're closing in a middle of a trans, we need to clean up */
742 trans_conn_closed(msc_a);
743
744 call_leg_release(msc_a->cc.call_leg);
745
746 /* Cancel use counts for pending CM Service / Paging */
747 for (i = 0; i < ARRAY_SIZE(use_counts_to_cancel); i++) {
748 const char *use = use_counts_to_cancel[i];
749 int32_t count = osmo_use_count_by(&msc_a->use_count, use);
750 if (!count)
751 continue;
752 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: canceling still pending use: %s (%d)\n", use, count);
753 osmo_use_count_get_put(&msc_a->use_count, use, -count);
754 }
755
756 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
757 sgs_iface_tx_release(vsub);
758 /* In SGsAP there is no confirmation of a release. */
759 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
760 } else {
761 struct ran_msg msg = {
762 .msg_type = RAN_MSG_CLEAR_COMMAND,
763 .clear_command = {
764 .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED),
765 },
766 };
767 msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
768 msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Philipp Maier47cf84d2019-08-15 14:56:54 +0200769
770 /* The connection is cleared. The MS will now go back to 4G,
771 Switch the RAN type back to SGS. */
772 if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
773 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100774 }
775
776 if (vsub)
777 vlr_subscr_put(vsub, __func__);
778}
779
780static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
781{
782 struct msc_a *msc_a = fi->priv;
783
784 msc_a_cleanup_rtp_streams(msc_a, event, data);
785
786 switch (event) {
787 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
788 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
789 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
790 return;
791
792 case MSC_A_EV_MO_CLOSE:
793 case MSC_A_EV_CN_CLOSE:
794 case MSC_A_EV_UNUSED:
795 /* Already releasing */
796 return;
797
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100798 case MSC_EV_CALL_LEG_TERM:
799 case MSC_MNCC_EV_CALL_ENDED:
800 /* RTP streams cleaned up above */
801 return;
802
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200803 case MSC_A_EV_HANDOVER_END:
804 /* msc_ho_fsm does cleanup. */
805 return;
806
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100807 default:
808 OSMO_ASSERT(false);
809 }
810}
811
812
813static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
814{
815 struct msc_a *msc_a = msc_a_fi_priv(fi);
816 char buf[128];
817 LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n",
818 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
819 if (osmo_use_count_total(&msc_a->use_count) == 0)
820 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
821}
822
823static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
824{
825 if (event == MSC_A_EV_UNUSED)
826 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
827}
828
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100829void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
830{
831 struct msc_a *msc_a = msc_a_fi_priv(fi);
832
833 trans_conn_closed(msc_a);
834
835 if (msc_a_fsm_has_active_transactions(fi))
836 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
837
838 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
839}
840
841const struct value_string msc_a_fsm_event_names[] = {
842 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
843 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
844 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100845 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
846 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
847 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
848 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
849 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
850 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
851 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
852 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
853 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
854 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
855 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
856 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
857 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
858 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
859 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
860 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
861 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
862 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
863 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
864 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
865 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
866 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
867 {}
868};
869
870#define S(x) (1 << (x))
871
872static const struct osmo_fsm_state msc_a_fsm_states[] = {
873 [MSC_A_ST_VALIDATE_L3] = {
874 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
875 .in_event_mask = 0
876 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
877 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
878 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
879 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
880 | S(MSC_A_EV_MO_CLOSE)
881 | S(MSC_A_EV_CN_CLOSE)
882 | S(MSC_A_EV_UNUSED)
883 ,
884 .out_state_mask = 0
885 | S(MSC_A_ST_VALIDATE_L3)
886 | S(MSC_A_ST_AUTH_CIPH)
887 | S(MSC_A_ST_RELEASING)
888 ,
889 .action = msc_a_fsm_validate_l3,
890 },
891 [MSC_A_ST_AUTH_CIPH] = {
892 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
893 .in_event_mask = 0
894 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
895 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
896 | S(MSC_A_EV_AUTHENTICATED)
897 | S(MSC_A_EV_MO_CLOSE)
898 | S(MSC_A_EV_CN_CLOSE)
899 | S(MSC_A_EV_UNUSED)
900 ,
901 .out_state_mask = 0
902 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
903 | S(MSC_A_ST_AUTHENTICATED)
904 | S(MSC_A_ST_RELEASING)
905 ,
906 .action = msc_a_fsm_auth_ciph,
907 },
908 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
909 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
910 .in_event_mask = 0
911 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
912 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
913 | S(MSC_A_EV_CLASSMARK_UPDATE)
914 | S(MSC_A_EV_MO_CLOSE)
915 | S(MSC_A_EV_CN_CLOSE)
916 ,
917 .out_state_mask = 0
918 | S(MSC_A_ST_AUTH_CIPH)
919 | S(MSC_A_ST_RELEASING)
920 ,
921 .action = msc_a_fsm_wait_classmark_update,
922 },
923 [MSC_A_ST_AUTHENTICATED] = {
924 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
925 /* allow everything to release for any odd behavior */
926 .in_event_mask = 0
927 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
928 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
929 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
930 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
931 | S(MSC_A_EV_MO_CLOSE)
932 | S(MSC_A_EV_CN_CLOSE)
933 | S(MSC_A_EV_UNUSED)
934 ,
935 .out_state_mask = 0
936 | S(MSC_A_ST_RELEASING)
937 | S(MSC_A_ST_COMMUNICATING)
938 ,
939 .onenter = msc_a_fsm_authenticated_enter,
940 .action = msc_a_fsm_authenticated,
941 },
942 [MSC_A_ST_COMMUNICATING] = {
943 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
944 /* allow everything to release for any odd behavior */
945 .in_event_mask = 0
946 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
947 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
948 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
949 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
950 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
951 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
952 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
953 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
954 | S(MSC_A_EV_MO_CLOSE)
955 | S(MSC_A_EV_CN_CLOSE)
956 | S(MSC_A_EV_UNUSED)
957 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
958 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100959 | S(MSC_EV_CALL_LEG_TERM)
960 | S(MSC_MNCC_EV_CALL_ENDED)
961 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200962 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100963 ,
964 .out_state_mask = 0
965 | S(MSC_A_ST_RELEASING)
966 ,
967 .action = msc_a_fsm_communicating,
968 },
969 [MSC_A_ST_RELEASING] = {
970 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
971 .in_event_mask = 0
972 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
973 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
974 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100975 | S(MSC_EV_CALL_LEG_TERM)
976 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200977 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100978 ,
979 .out_state_mask = 0
980 | S(MSC_A_ST_RELEASED)
981 ,
982 .onenter = msc_a_fsm_releasing_onenter,
983 .action = msc_a_fsm_releasing,
984 },
985 [MSC_A_ST_RELEASED] = {
986 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
987 .in_event_mask = 0
988 | S(MSC_A_EV_UNUSED)
989 ,
990 .onenter = msc_a_fsm_released_onenter,
991 .action = msc_a_fsm_released,
992 },
993};
994
995static struct osmo_fsm msc_a_fsm = {
996 .name = "msc_a",
997 .states = msc_a_fsm_states,
998 .num_states = ARRAY_SIZE(msc_a_fsm_states),
999 .log_subsys = DMSC,
1000 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001001 .timer_cb = msc_a_fsm_timer_cb,
1002 .cleanup = msc_a_fsm_cleanup,
1003};
1004
1005static __attribute__((constructor)) void msc_a_fsm_init()
1006{
1007 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1008}
1009
1010static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1011{
1012 struct msc_a *msc_a = e->use_count->talloc_object;
1013 char buf[128];
1014 int32_t total;
1015 int level;
1016
1017 if (!e->use)
1018 return -EINVAL;
1019
1020 total = osmo_use_count_total(&msc_a->use_count);
1021
1022 if (total == 0
1023 || (total == 1 && old_use_count == 0 && e->count == 1))
1024 level = LOGL_INFO;
1025 else
1026 level = LOGL_DEBUG;
1027
1028 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1029 (e->count - old_use_count) > 0? "+" : "-", e->use,
1030 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1031
1032 if (e->count < 0)
1033 return -ERANGE;
1034
1035 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1036
1037 if (total == 0)
1038 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1039 return 0;
1040}
1041
1042struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1043{
1044 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1045 msc_a->use_count = (struct osmo_use_count){
1046 .talloc_object = msc_a,
1047 .use_cb = msc_a_use_cb,
1048 };
1049 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1050 /* Start timeout for first state */
Neels Hofmeyr01653252019-09-03 02:06:22 +02001051 msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001052 return msc_a;
1053}
1054
1055bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1056{
1057 if (!msc_a || !msc_a->c.fi)
1058 return false;
1059 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1060}
1061
1062const struct value_string complete_layer3_type_names[] = {
1063 { COMPLETE_LAYER3_NONE, "NONE" },
1064 { COMPLETE_LAYER3_LU, "LU" },
1065 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1066 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
1067 { 0, NULL }
1068};
1069
1070#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1071 do { \
1072 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1073 ## ARGS, \
1074 msub_ran_conn_name(msc_a->c.msub), \
1075 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1076 == 0) { \
1077 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1078 if (_vsub) { \
1079 if (_vsub->lu_fsm) \
1080 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1081 if (_vsub->auth_fsm) \
1082 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1083 if (_vsub->proc_arq_fsm) \
1084 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1085 } \
1086 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1087 } \
1088 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1089 } while (0)
1090
1091
1092/* Compose an ID almost like gsm48_mi_to_string(), but print the MI type along, and print a TMSI as hex. */
1093void msc_a_update_id_from_mi(struct msc_a *msc_a, const uint8_t mi[], uint8_t mi_len)
1094{
1095 _msc_a_update_id(msc_a, "%s", osmo_mi_name(mi, mi_len));
1096}
1097
1098/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1099void msc_a_update_id(struct msc_a *msc_a)
1100{
1101 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1102}
1103
1104/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1105 * instances. */
1106void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1107{
1108 struct msub *msub;
1109 llist_for_each_entry(msub, &msub_list, entry) {
1110 struct vlr_subscr *vsub = msub_vsub(msub);
1111 if (vsub != for_vsub)
1112 continue;
1113 msc_a_update_id(msub_msc_a(msub));
1114 }
1115}
1116
1117static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1118{
1119 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1120 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1121
1122 switch (pdisc) {
1123 case GSM48_PDISC_MM:
1124 switch (msg_type) {
1125 case GSM48_MT_MM_LOC_UPD_REQUEST:
1126 case GSM48_MT_MM_CM_SERV_REQ:
1127 case GSM48_MT_MM_CM_REEST_REQ:
1128 case GSM48_MT_MM_AUTH_RESP:
1129 case GSM48_MT_MM_AUTH_FAIL:
1130 case GSM48_MT_MM_ID_RESP:
1131 case GSM48_MT_MM_TMSI_REALL_COMPL:
1132 case GSM48_MT_MM_IMSI_DETACH_IND:
1133 return true;
1134 default:
1135 break;
1136 }
1137 break;
1138 case GSM48_PDISC_RR:
1139 switch (msg_type) {
1140 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1141 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1142 case GSM48_MT_RR_PAG_RESP:
1143 case GSM48_MT_RR_CIPH_M_COMPL:
1144 return true;
1145 default:
1146 break;
1147 }
1148 break;
1149 default:
1150 break;
1151 }
1152
1153 return false;
1154}
1155
1156/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1157int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1158{
1159 struct gsm48_hdr *gh;
1160 uint8_t pdisc;
1161 int rc;
1162 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1163 int is_r99;
1164
1165 OSMO_ASSERT(msg->l3h);
1166 OSMO_ASSERT(msg);
1167
1168 gh = msgb_l3(msg);
1169 pdisc = gsm48_hdr_pdisc(gh);
1170
1171 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1172 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1173
1174 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1175 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1176 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1177 if (vsub)
1178 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1179 else
1180 is_r99 = compl_l3_msg_is_r99(msg);
1181
1182 if (is_r99 < 0) {
1183 LOG_MSC_A(msc_a, LOGL_ERROR,
1184 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1185 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1186 return -EACCES;
1187 }
1188
1189 if (is_r99 >= 0
1190 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1191 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1192 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1193 return 0;
1194 }
1195
1196 if (!msc_a_is_accepted(msc_a)
1197 && !msg_is_initially_permitted(gh)) {
1198 LOG_MSC_A(msc_a, LOGL_ERROR,
1199 "Message not permitted for initial conn: %s\n",
1200 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1201 return -EACCES;
1202 }
1203
1204 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1205 LOG_MSC_A(msc_a, LOGL_ERROR,
1206 "Illegal situation: RAN type mismatch:"
1207 " attached via %s, received message via %s\n",
1208 osmo_rat_type_name(vsub->cs.attached_via_ran),
1209 osmo_rat_type_name(msc_a->c.ran->type));
1210 return -EACCES;
1211 }
1212
1213#if 0
1214 if (silent_call_reroute(conn, msg))
1215 return silent_call_rx(conn, msg);
1216#endif
1217
1218 switch (pdisc) {
1219 case GSM48_PDISC_CC:
1220 rc = gsm0408_rcv_cc(msc_a, msg);
1221 break;
1222 case GSM48_PDISC_MM:
1223 rc = gsm0408_rcv_mm(msc_a, msg);
1224 break;
1225 case GSM48_PDISC_RR:
1226 rc = gsm0408_rcv_rr(msc_a, msg);
1227 break;
1228 case GSM48_PDISC_SMS:
1229 rc = gsm0411_rcv_sms(msc_a, msg);
1230 break;
1231 case GSM48_PDISC_MM_GPRS:
1232 case GSM48_PDISC_SM_GPRS:
1233 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1234 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1235 rc = -ENOTSUP;
1236 break;
1237 case GSM48_PDISC_NC_SS:
1238 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1239 break;
1240 case GSM48_PDISC_TEST:
1241 rc = gsm0414_rcv_test(msc_a, msg);
1242 break;
1243 default:
1244 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1245 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1246 rc = -EINVAL;
1247 break;
1248 }
1249
1250 return rc;
1251}
1252
1253static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1254{
1255 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
1256 struct rtp_stream *rtps_to_ran = msc_a->cc.call_leg ? msc_a->cc.call_leg->rtp[RTP_TO_RAN] : NULL;
1257
1258 if (!rtps_to_ran) {
1259 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1260 return;
1261 }
1262 if (!cc_trans) {
1263 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but CC transaction is active\n");
1264 return;
1265 }
1266
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001267 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1268 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1269 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1270 call_leg_release(msc_a->cc.call_leg);
1271 return;
1272 }
1273
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001274 /* Update RAN-side endpoint CI: */
1275 rtp_stream_set_codec(rtps_to_ran, ac->assignment_complete.codec);
1276 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001277 if (rtps_to_ran->use_osmux)
1278 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1279 ac->assignment_complete.osmux_cid);
1280
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001281 rtp_stream_commit(rtps_to_ran);
1282
1283 /* Setup CN side endpoint CI:
1284 * Now that
1285 * - the first CI has been created and a definitive endpoint name is assigned to the call_leg's MGW
1286 * endpoint,
1287 * - the Assignment has chosen a speech codec
1288 * go on to create the CN side RTP stream's CI. */
1289 if (call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_CN, cc_trans->callref, cc_trans,
1290 &ac->assignment_complete.codec, NULL)) {
1291 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Error creating MGW CI towards CN\n");
1292 call_leg_release(msc_a->cc.call_leg);
1293 return;
1294 }
1295}
1296
1297static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1298{
1299 struct gsm_trans *trans;
1300
1301 /* For a normal voice call, there will be an rtp_stream FSM. */
1302 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1303 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1304 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1305 return;
1306 }
1307
1308 /* Otherwise, a silent call might be active */
1309 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1310 if (trans) {
1311 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1312 trans_free(trans);
1313 return;
1314 }
1315
1316 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1317 msc_a_release_cn(msc_a);
1318}
1319
1320static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1321 struct osmo_gsm48_classmark *dst)
1322{
1323 if (!dst) {
1324 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1325
1326 if (!vsub)
1327 dst = &msc_a->temporary_classmark;
1328 else
1329 dst = &vsub->classmark;
1330 }
1331
Martin Hauke3f07dac2019-11-14 17:49:08 +01001332 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001333 osmo_gsm48_classmark_a5_name(classmark));
1334 osmo_gsm48_classmark_update(dst, classmark);
1335
1336 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1337 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1338 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1339}
1340
1341static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1342{
1343 int sapi = msg->sapi_n_reject.dlci & 0x7;
1344 if (sapi == UM_SAPI_SMS)
1345 gsm411_sapi_n_reject(msc_a);
1346}
1347
1348static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1349{
1350 if (!msc_a->ho.fi) {
1351 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1352 return -EINVAL;
1353 }
1354 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1355}
1356
1357int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1358{
1359 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1360 const struct ran_msg *msg = d->ran_dec;
1361 int rc = -99;
1362
1363 switch (msg->msg_type) {
1364
1365 case RAN_MSG_COMPL_L3:
1366 msc_a->via_cell = (struct osmo_cell_global_id){
1367 .lai.plmn = msc_a_net(msc_a)->plmn,
1368 };
1369 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
1370 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1371 if (!rc) {
1372 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1373 if (conn)
1374 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1375 }
1376 break;
1377
1378 case RAN_MSG_DTAP:
1379 rc = msc_a_up_l3(msc_a, msg->dtap);
1380 break;
1381
1382 case RAN_MSG_CLEAR_REQUEST:
1383 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1384 break;
1385
1386 case RAN_MSG_CLEAR_COMPLETE:
1387 switch (msc_a->c.fi->state) {
1388 case MSC_A_ST_RELEASING:
1389 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1390 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1391 break;
1392 case MSC_A_ST_RELEASED:
1393 break;
1394 default:
1395 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1396 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1397 break;
1398 }
1399 rc = 0;
1400 break;
1401
1402 case RAN_MSG_CLASSMARK_UPDATE:
1403 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1404 rc = 0;
1405 break;
1406
1407 case RAN_MSG_CIPHER_MODE_COMPLETE:
1408 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1409 if (msg->cipher_mode_complete.alg_id) {
1410 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1411 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1412 msc_a->geran_encr.alg_id - 1);
1413 };
1414 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1415 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001416
1417 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1418 if (msg->cipher_mode_complete.l3_msg) {
1419 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1420 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1421 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1422 unsigned char *pos = msgb_put(dtap, len);
1423 memcpy(pos, data, len);
1424 dtap->l3h = pos;
1425 rc = msc_a_up_l3(msc_a, dtap);
1426 msgb_free(dtap);
1427 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001428 break;
1429
1430 case RAN_MSG_CIPHER_MODE_REJECT:
1431 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1432 rc = 0;
1433 break;
1434
1435 case RAN_MSG_ASSIGNMENT_COMPLETE:
1436 msc_a_up_call_assignment_complete(msc_a, msg);
1437 rc = 0;
1438 break;
1439
1440 case RAN_MSG_ASSIGNMENT_FAILURE:
1441 msc_a_up_call_assignment_failure(msc_a, msg);
1442 rc = 0;
1443 break;
1444
1445 case RAN_MSG_SAPI_N_REJECT:
1446 msc_a_up_sapi_n_reject(msc_a, msg);
1447 rc = 0;
1448 break;
1449
1450 case RAN_MSG_HANDOVER_PERFORMED:
1451 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1452 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1453 break;
1454
1455 case RAN_MSG_HANDOVER_REQUIRED:
1456 /* The BSS lets us know that it wants to handover to a different cell */
1457 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1458 break;
1459
1460 case RAN_MSG_HANDOVER_FAILURE:
1461 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1462 break;
1463
1464 default:
1465 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1466 rc = -ENOTSUP;
1467 break;
1468 }
1469 return rc;
1470}
1471
1472static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1473{
1474 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1475 int rc = -99;
1476
1477 if (!msc_t) {
1478 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1479 return -EINVAL;
1480 }
1481
1482 OSMO_ASSERT(d->ran_dec);
1483
1484 switch (d->ran_dec->msg_type) {
1485
1486 case RAN_MSG_CLEAR_REQUEST:
1487 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1488 break;
1489
1490 case RAN_MSG_CLEAR_COMPLETE:
1491 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1492 break;
1493
1494 case RAN_MSG_CLASSMARK_UPDATE:
1495 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1496 rc = 0;
1497 break;
1498
1499 case RAN_MSG_HANDOVER_REQUEST_ACK:
1500 /* new BSS accepts Handover */
1501 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1502 break;
1503
1504 case RAN_MSG_HANDOVER_DETECT:
1505 /* new BSS signals the MS is DETECTed on the new lchan */
1506 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1507 break;
1508
1509 case RAN_MSG_HANDOVER_COMPLETE:
1510 /* new BSS signals the MS has fully moved to the new lchan */
1511 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1512 break;
1513
1514 case RAN_MSG_HANDOVER_FAILURE:
1515 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1516 break;
1517
1518 default:
1519 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1520 ran_msg_type_name(d->ran_dec->msg_type));
1521 rc = -ENOTSUP;
1522 break;
1523 }
1524 return rc;
1525}
1526
1527int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1528{
1529 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1530 struct msc_a_ran_dec_data *d = data;
1531 int rc = -99;
1532
1533 d->ran_dec = msg;
1534
1535 switch (d->from_role) {
1536 case MSC_ROLE_I:
1537 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1538 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1539 break;
1540
1541 case MSC_ROLE_T:
1542 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1543 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1544 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1545 break;
1546
1547 default:
1548 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1549 ran_msg_type_name(msg->msg_type));
1550 return -ENOTSUP;
1551 }
1552
1553 if (rc)
1554 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),
1555 msc_role_name(d->from_role));
1556 return rc;
1557}
1558
1559/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1560int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1561 const char *file, int line)
1562{
1563 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1564 ran_msg, file, line);
1565}
1566
1567/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1568 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1569int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1570 const struct ran_msg *ran_msg,
1571 const char *file, int line)
1572{
1573 struct an_apdu an_apdu = {
1574 .an_proto = msc_a->c.ran->an_proto,
1575 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1576 };
1577 int rc;
1578 if (!an_apdu.msg)
1579 return -EIO;
1580 rc = _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
1581 msgb_free(an_apdu.msg);
1582 return rc;
1583}
1584
1585int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
1586{
1587 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001588 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
1589 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001590
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001591 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001592 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
1593 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1594 msgb_free(dtap);
1595 return -EIO;
1596 }
1597
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001598 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
1599 /* The SGs connection to the MME always is at the MSC-A. */
1600 return sgs_iface_tx_dtap_ud(msc_a, dtap);
1601 }
1602
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001603 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
1604 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1605
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001606 ran_msg = (struct ran_msg){
1607 .msg_type = RAN_MSG_DTAP,
1608 .dtap = dtap,
1609 };
1610 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
1611}
1612
1613struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
1614{
1615 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
1616 if (valid_conn_only && !msc_a_is_accepted(msc_a))
1617 return NULL;
1618 return msc_a;
1619}
1620
1621int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
1622{
1623 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1624 struct ran_msg msg = {
1625 .msg_type = RAN_MSG_COMMON_ID,
1626 .common_id = {
1627 .imsi = vsub->imsi,
1628 },
1629 };
1630
1631 return msc_a_ran_down(msc_a, to_role, &msg);
1632}
1633
1634static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
1635{
1636 struct call_leg *cl = msc_a->cc.call_leg;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001637 struct msc_i *msc_i = msc_a_msc_i(msc_a);
1638 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001639
1640 OSMO_ASSERT(!msc_a->cc.active_trans);
1641 msc_a->cc.active_trans = cc_trans;
1642
1643 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
1644
1645 if (!cl) {
1646 cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi,
1647 MSC_EV_CALL_LEG_TERM,
1648 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
Neels Hofmeyr265a4c72019-05-09 16:20:51 +02001649 MSC_EV_CALL_LEG_RTP_COMPLETE);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001650 OSMO_ASSERT(cl);
1651
Martin Hauke3f07dac2019-11-14 17:49:08 +01001652 /* HACK: We put the connection in loopback mode at the beginning to
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001653 * trick the hNodeB into doing the IuUP negotiation with itself.
1654 * This is a hack we need because osmo-mgw does not support IuUP yet, see OS#2459. */
1655 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU)
1656 cl->crcx_conn_mode[RTP_TO_RAN] = MGCP_CONN_LOOPBACK;
1657 }
1658
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001659 if (net->use_osmux != OSMUX_USAGE_OFF) {
1660 msc_i = msc_a_msc_i(msc_a);
1661 if (msc_i->c.remote_to) {
1662 /* TODO: investigate what to do in this case */
1663 LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC");
1664 } else {
1665 cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux;
1666 }
1667 }
1668
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001669 /* This will lead to either MSC_EV_CALL_LEG_LOCAL_ADDR_AVAILABLE or MSC_EV_CALL_LEG_TERM.
1670 * If the local address is already known, then immediately trigger. */
1671 if (call_leg_local_ip(cl, RTP_TO_RAN))
1672 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
1673 else
1674 return call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_RAN, cc_trans->callref, cc_trans, NULL, NULL);
1675}
1676
1677int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
1678{
1679 struct msc_a *msc_a = cc_trans->msc_a;
1680 OSMO_ASSERT(cc_trans->type == TRANS_CC);
1681
1682 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02001683 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001684 return 0;
1685 }
1686
1687 if (msc_a->cc.active_trans) {
1688 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
1689 return 0;
1690 }
1691
1692 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
1693 return msc_a_start_assignment(msc_a, cc_trans);
1694}
1695
1696const char *msc_a_cm_service_type_to_use(enum osmo_cm_service_type cm_service_type)
1697{
1698 switch (cm_service_type) {
1699 case GSM48_CMSERV_MO_CALL_PACKET:
1700 case GSM48_CMSERV_EMERGENCY:
1701 return MSC_A_USE_CM_SERVICE_CC;
1702
1703 case GSM48_CMSERV_SMS:
1704 return MSC_A_USE_CM_SERVICE_SMS;
1705
1706 case GSM48_CMSERV_SUP_SERV:
1707 return MSC_A_USE_CM_SERVICE_SS;
1708
1709 default:
1710 return NULL;
1711 }
1712}
1713
1714void msc_a_release_cn(struct msc_a *msc_a)
1715{
1716 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
1717}
1718
1719void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
1720{
1721 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1722}