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