blob: 05030d3071cd8d2a4101a7c15ed9f11e0125e1e5 [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:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200115 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_LOC_UPDATE_COMPLETED : MSC_CTR_LOC_UPDATE_FAILED));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100116 break;
117 case COMPLETE_LAYER3_CM_SERVICE_REQ:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200118 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_CM_SERVICE_REQUEST_ACCEPTED : MSC_CTR_CM_SERVICE_REQUEST_REJECTED));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100119 break;
120 case COMPLETE_LAYER3_PAGING_RESP:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200121 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_PAGING_RESP_ACCEPTED : MSC_CTR_PAGING_RESP_REJECTED));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100122 break;
123 default:
124 break;
125 }
126}
127
128static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted)
129{
130 struct msc_a *msc_a = fi->priv;
131 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
132
133 update_counters(fi, conn_accepted);
134
Neels Hofmeyr83cf10c2020-06-24 14:23:26 +0200135 if (conn_accepted) {
136 /* Record the Cell ID seen in Complete Layer 3 Information in the VLR, so that it also shows in vty
137 * 'show' output. */
138 vsub->cgi = msc_a->via_cell;
139 }
140
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100141 /* 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 = {
Neels Hofmeyrd9fe7112020-07-11 00:20:20 +0200776 /* "Call Control" is the only cause code listed in 3GPP TS 48.008 3.2.1.21 CLEAR COMMAND
777 * that qualifies for a normal release situation. (OS#4664) */
778 .gsm0808_cause = GSM0808_CAUSE_CALL_CONTROL,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100779 .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED),
780 },
781 };
782 msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
783 msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Philipp Maier47cf84d2019-08-15 14:56:54 +0200784
785 /* The connection is cleared. The MS will now go back to 4G,
786 Switch the RAN type back to SGS. */
787 if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
788 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100789 }
790
791 if (vsub)
792 vlr_subscr_put(vsub, __func__);
793}
794
795static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
796{
797 struct msc_a *msc_a = fi->priv;
798
799 msc_a_cleanup_rtp_streams(msc_a, event, data);
800
801 switch (event) {
802 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
803 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
804 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
805 return;
806
807 case MSC_A_EV_MO_CLOSE:
808 case MSC_A_EV_CN_CLOSE:
809 case MSC_A_EV_UNUSED:
810 /* Already releasing */
811 return;
812
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100813 case MSC_EV_CALL_LEG_TERM:
814 case MSC_MNCC_EV_CALL_ENDED:
815 /* RTP streams cleaned up above */
816 return;
817
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200818 case MSC_A_EV_HANDOVER_END:
819 /* msc_ho_fsm does cleanup. */
820 return;
821
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100822 default:
823 OSMO_ASSERT(false);
824 }
825}
826
827
828static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
829{
830 struct msc_a *msc_a = msc_a_fi_priv(fi);
831 char buf[128];
832 LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n",
833 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
834 if (osmo_use_count_total(&msc_a->use_count) == 0)
835 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
836}
837
838static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
839{
840 if (event == MSC_A_EV_UNUSED)
841 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
842}
843
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100844void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
845{
846 struct msc_a *msc_a = msc_a_fi_priv(fi);
847
848 trans_conn_closed(msc_a);
849
850 if (msc_a_fsm_has_active_transactions(fi))
851 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
852
853 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
854}
855
856const struct value_string msc_a_fsm_event_names[] = {
857 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
858 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
859 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100860 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
861 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
862 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
863 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
864 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
865 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
866 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
867 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
868 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
869 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
870 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
871 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
872 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
873 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
874 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
875 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
876 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
877 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
878 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
879 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
880 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
881 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
882 {}
883};
884
885#define S(x) (1 << (x))
886
887static const struct osmo_fsm_state msc_a_fsm_states[] = {
888 [MSC_A_ST_VALIDATE_L3] = {
889 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
890 .in_event_mask = 0
891 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
892 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
893 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
894 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
895 | S(MSC_A_EV_MO_CLOSE)
896 | S(MSC_A_EV_CN_CLOSE)
897 | S(MSC_A_EV_UNUSED)
898 ,
899 .out_state_mask = 0
900 | S(MSC_A_ST_VALIDATE_L3)
901 | S(MSC_A_ST_AUTH_CIPH)
902 | S(MSC_A_ST_RELEASING)
903 ,
904 .action = msc_a_fsm_validate_l3,
905 },
906 [MSC_A_ST_AUTH_CIPH] = {
907 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
908 .in_event_mask = 0
909 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
910 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
911 | S(MSC_A_EV_AUTHENTICATED)
912 | S(MSC_A_EV_MO_CLOSE)
913 | S(MSC_A_EV_CN_CLOSE)
914 | S(MSC_A_EV_UNUSED)
915 ,
916 .out_state_mask = 0
917 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
918 | S(MSC_A_ST_AUTHENTICATED)
919 | S(MSC_A_ST_RELEASING)
920 ,
921 .action = msc_a_fsm_auth_ciph,
922 },
923 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
924 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
925 .in_event_mask = 0
926 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
927 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
928 | S(MSC_A_EV_CLASSMARK_UPDATE)
929 | S(MSC_A_EV_MO_CLOSE)
930 | S(MSC_A_EV_CN_CLOSE)
931 ,
932 .out_state_mask = 0
933 | S(MSC_A_ST_AUTH_CIPH)
934 | S(MSC_A_ST_RELEASING)
935 ,
936 .action = msc_a_fsm_wait_classmark_update,
937 },
938 [MSC_A_ST_AUTHENTICATED] = {
939 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
940 /* allow everything to release for any odd behavior */
941 .in_event_mask = 0
942 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
943 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
944 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
945 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
946 | S(MSC_A_EV_MO_CLOSE)
947 | S(MSC_A_EV_CN_CLOSE)
948 | S(MSC_A_EV_UNUSED)
949 ,
950 .out_state_mask = 0
951 | S(MSC_A_ST_RELEASING)
952 | S(MSC_A_ST_COMMUNICATING)
953 ,
954 .onenter = msc_a_fsm_authenticated_enter,
955 .action = msc_a_fsm_authenticated,
956 },
957 [MSC_A_ST_COMMUNICATING] = {
958 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
959 /* allow everything to release for any odd behavior */
960 .in_event_mask = 0
961 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
962 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
963 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
964 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
965 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
966 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
967 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
968 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
969 | S(MSC_A_EV_MO_CLOSE)
970 | S(MSC_A_EV_CN_CLOSE)
971 | S(MSC_A_EV_UNUSED)
972 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
973 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100974 | S(MSC_EV_CALL_LEG_TERM)
975 | S(MSC_MNCC_EV_CALL_ENDED)
976 | S(MSC_A_EV_HANDOVER_REQUIRED)
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_RELEASING)
981 ,
982 .action = msc_a_fsm_communicating,
983 },
984 [MSC_A_ST_RELEASING] = {
985 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
986 .in_event_mask = 0
987 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
988 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
989 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100990 | S(MSC_EV_CALL_LEG_TERM)
991 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200992 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100993 ,
994 .out_state_mask = 0
995 | S(MSC_A_ST_RELEASED)
996 ,
997 .onenter = msc_a_fsm_releasing_onenter,
998 .action = msc_a_fsm_releasing,
999 },
1000 [MSC_A_ST_RELEASED] = {
1001 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
1002 .in_event_mask = 0
1003 | S(MSC_A_EV_UNUSED)
1004 ,
1005 .onenter = msc_a_fsm_released_onenter,
1006 .action = msc_a_fsm_released,
1007 },
1008};
1009
1010static struct osmo_fsm msc_a_fsm = {
1011 .name = "msc_a",
1012 .states = msc_a_fsm_states,
1013 .num_states = ARRAY_SIZE(msc_a_fsm_states),
1014 .log_subsys = DMSC,
1015 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001016 .timer_cb = msc_a_fsm_timer_cb,
1017 .cleanup = msc_a_fsm_cleanup,
1018};
1019
1020static __attribute__((constructor)) void msc_a_fsm_init()
1021{
1022 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1023}
1024
1025static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1026{
1027 struct msc_a *msc_a = e->use_count->talloc_object;
1028 char buf[128];
1029 int32_t total;
1030 int level;
1031
1032 if (!e->use)
1033 return -EINVAL;
1034
1035 total = osmo_use_count_total(&msc_a->use_count);
1036
1037 if (total == 0
1038 || (total == 1 && old_use_count == 0 && e->count == 1))
1039 level = LOGL_INFO;
1040 else
1041 level = LOGL_DEBUG;
1042
1043 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1044 (e->count - old_use_count) > 0? "+" : "-", e->use,
1045 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1046
1047 if (e->count < 0)
1048 return -ERANGE;
1049
1050 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1051
1052 if (total == 0)
1053 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1054 return 0;
1055}
1056
1057struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1058{
1059 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1060 msc_a->use_count = (struct osmo_use_count){
1061 .talloc_object = msc_a,
1062 .use_cb = msc_a_use_cb,
1063 };
1064 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1065 /* Start timeout for first state */
Neels Hofmeyr01653252019-09-03 02:06:22 +02001066 msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001067 return msc_a;
1068}
1069
1070bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1071{
1072 if (!msc_a || !msc_a->c.fi)
1073 return false;
1074 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1075}
1076
1077const struct value_string complete_layer3_type_names[] = {
1078 { COMPLETE_LAYER3_NONE, "NONE" },
1079 { COMPLETE_LAYER3_LU, "LU" },
1080 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1081 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
1082 { 0, NULL }
1083};
1084
1085#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1086 do { \
1087 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1088 ## ARGS, \
1089 msub_ran_conn_name(msc_a->c.msub), \
1090 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1091 == 0) { \
1092 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1093 if (_vsub) { \
1094 if (_vsub->lu_fsm) \
1095 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1096 if (_vsub->auth_fsm) \
1097 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1098 if (_vsub->proc_arq_fsm) \
1099 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1100 } \
1101 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1102 } \
1103 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1104 } while (0)
1105
1106
1107/* 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 +02001108void msc_a_update_id_from_mi(struct msc_a *msc_a, const struct osmo_mobile_identity *mi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001109{
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001110 _msc_a_update_id(msc_a, "%s", osmo_mobile_identity_to_str_c(OTC_SELECT, mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001111}
1112
1113/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1114void msc_a_update_id(struct msc_a *msc_a)
1115{
1116 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1117}
1118
1119/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1120 * instances. */
1121void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1122{
1123 struct msub *msub;
1124 llist_for_each_entry(msub, &msub_list, entry) {
1125 struct vlr_subscr *vsub = msub_vsub(msub);
1126 if (vsub != for_vsub)
1127 continue;
1128 msc_a_update_id(msub_msc_a(msub));
1129 }
1130}
1131
1132static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1133{
1134 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1135 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1136
1137 switch (pdisc) {
1138 case GSM48_PDISC_MM:
1139 switch (msg_type) {
1140 case GSM48_MT_MM_LOC_UPD_REQUEST:
1141 case GSM48_MT_MM_CM_SERV_REQ:
1142 case GSM48_MT_MM_CM_REEST_REQ:
1143 case GSM48_MT_MM_AUTH_RESP:
1144 case GSM48_MT_MM_AUTH_FAIL:
1145 case GSM48_MT_MM_ID_RESP:
1146 case GSM48_MT_MM_TMSI_REALL_COMPL:
1147 case GSM48_MT_MM_IMSI_DETACH_IND:
1148 return true;
1149 default:
1150 break;
1151 }
1152 break;
1153 case GSM48_PDISC_RR:
1154 switch (msg_type) {
1155 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1156 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1157 case GSM48_MT_RR_PAG_RESP:
1158 case GSM48_MT_RR_CIPH_M_COMPL:
1159 return true;
1160 default:
1161 break;
1162 }
1163 break;
1164 default:
1165 break;
1166 }
1167
1168 return false;
1169}
1170
1171/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1172int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1173{
1174 struct gsm48_hdr *gh;
1175 uint8_t pdisc;
1176 int rc;
1177 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1178 int is_r99;
1179
1180 OSMO_ASSERT(msg->l3h);
1181 OSMO_ASSERT(msg);
1182
1183 gh = msgb_l3(msg);
1184 pdisc = gsm48_hdr_pdisc(gh);
1185
1186 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1187 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1188
1189 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1190 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1191 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1192 if (vsub)
1193 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1194 else
1195 is_r99 = compl_l3_msg_is_r99(msg);
1196
1197 if (is_r99 < 0) {
1198 LOG_MSC_A(msc_a, LOGL_ERROR,
1199 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1200 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1201 return -EACCES;
1202 }
1203
1204 if (is_r99 >= 0
1205 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1206 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1207 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1208 return 0;
1209 }
1210
1211 if (!msc_a_is_accepted(msc_a)
1212 && !msg_is_initially_permitted(gh)) {
1213 LOG_MSC_A(msc_a, LOGL_ERROR,
1214 "Message not permitted for initial conn: %s\n",
1215 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1216 return -EACCES;
1217 }
1218
1219 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1220 LOG_MSC_A(msc_a, LOGL_ERROR,
1221 "Illegal situation: RAN type mismatch:"
1222 " attached via %s, received message via %s\n",
1223 osmo_rat_type_name(vsub->cs.attached_via_ran),
1224 osmo_rat_type_name(msc_a->c.ran->type));
1225 return -EACCES;
1226 }
1227
1228#if 0
1229 if (silent_call_reroute(conn, msg))
1230 return silent_call_rx(conn, msg);
1231#endif
1232
1233 switch (pdisc) {
1234 case GSM48_PDISC_CC:
1235 rc = gsm0408_rcv_cc(msc_a, msg);
1236 break;
1237 case GSM48_PDISC_MM:
1238 rc = gsm0408_rcv_mm(msc_a, msg);
1239 break;
1240 case GSM48_PDISC_RR:
1241 rc = gsm0408_rcv_rr(msc_a, msg);
1242 break;
1243 case GSM48_PDISC_SMS:
1244 rc = gsm0411_rcv_sms(msc_a, msg);
1245 break;
1246 case GSM48_PDISC_MM_GPRS:
1247 case GSM48_PDISC_SM_GPRS:
1248 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1249 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1250 rc = -ENOTSUP;
1251 break;
1252 case GSM48_PDISC_NC_SS:
1253 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1254 break;
1255 case GSM48_PDISC_TEST:
1256 rc = gsm0414_rcv_test(msc_a, msg);
1257 break;
1258 default:
1259 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1260 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1261 rc = -EINVAL;
1262 break;
1263 }
1264
1265 return rc;
1266}
1267
1268static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1269{
1270 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
1271 struct rtp_stream *rtps_to_ran = msc_a->cc.call_leg ? msc_a->cc.call_leg->rtp[RTP_TO_RAN] : NULL;
1272
1273 if (!rtps_to_ran) {
1274 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1275 return;
1276 }
1277 if (!cc_trans) {
1278 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but CC transaction is active\n");
1279 return;
1280 }
1281
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001282 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1283 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1284 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1285 call_leg_release(msc_a->cc.call_leg);
1286 return;
1287 }
1288
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001289 /* Update RAN-side endpoint CI: */
1290 rtp_stream_set_codec(rtps_to_ran, ac->assignment_complete.codec);
1291 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001292 if (rtps_to_ran->use_osmux)
1293 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1294 ac->assignment_complete.osmux_cid);
1295
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001296 rtp_stream_commit(rtps_to_ran);
1297
1298 /* Setup CN side endpoint CI:
1299 * Now that
1300 * - the first CI has been created and a definitive endpoint name is assigned to the call_leg's MGW
1301 * endpoint,
1302 * - the Assignment has chosen a speech codec
1303 * go on to create the CN side RTP stream's CI. */
1304 if (call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_CN, cc_trans->callref, cc_trans,
1305 &ac->assignment_complete.codec, NULL)) {
1306 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Error creating MGW CI towards CN\n");
1307 call_leg_release(msc_a->cc.call_leg);
1308 return;
1309 }
1310}
1311
1312static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1313{
1314 struct gsm_trans *trans;
1315
1316 /* For a normal voice call, there will be an rtp_stream FSM. */
1317 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1318 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1319 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1320 return;
1321 }
1322
1323 /* Otherwise, a silent call might be active */
1324 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1325 if (trans) {
1326 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1327 trans_free(trans);
1328 return;
1329 }
1330
1331 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1332 msc_a_release_cn(msc_a);
1333}
1334
1335static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1336 struct osmo_gsm48_classmark *dst)
1337{
1338 if (!dst) {
1339 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1340
1341 if (!vsub)
1342 dst = &msc_a->temporary_classmark;
1343 else
1344 dst = &vsub->classmark;
1345 }
1346
Martin Hauke3f07dac2019-11-14 17:49:08 +01001347 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001348 osmo_gsm48_classmark_a5_name(classmark));
1349 osmo_gsm48_classmark_update(dst, classmark);
1350
1351 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1352 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1353 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1354}
1355
1356static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1357{
1358 int sapi = msg->sapi_n_reject.dlci & 0x7;
1359 if (sapi == UM_SAPI_SMS)
1360 gsm411_sapi_n_reject(msc_a);
1361}
1362
1363static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1364{
1365 if (!msc_a->ho.fi) {
1366 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1367 return -EINVAL;
1368 }
1369 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1370}
1371
1372int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1373{
1374 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1375 const struct ran_msg *msg = d->ran_dec;
1376 int rc = -99;
1377
1378 switch (msg->msg_type) {
1379
1380 case RAN_MSG_COMPL_L3:
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001381 /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code
1382 * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001383 msc_a->via_cell = (struct osmo_cell_global_id){
1384 .lai.plmn = msc_a_net(msc_a)->plmn,
1385 };
1386 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
1387 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1388 if (!rc) {
1389 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1390 if (conn)
1391 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1392 }
1393 break;
1394
1395 case RAN_MSG_DTAP:
1396 rc = msc_a_up_l3(msc_a, msg->dtap);
1397 break;
1398
1399 case RAN_MSG_CLEAR_REQUEST:
1400 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1401 break;
1402
1403 case RAN_MSG_CLEAR_COMPLETE:
1404 switch (msc_a->c.fi->state) {
1405 case MSC_A_ST_RELEASING:
1406 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1407 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1408 break;
1409 case MSC_A_ST_RELEASED:
1410 break;
1411 default:
1412 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1413 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1414 break;
1415 }
1416 rc = 0;
1417 break;
1418
1419 case RAN_MSG_CLASSMARK_UPDATE:
1420 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1421 rc = 0;
1422 break;
1423
1424 case RAN_MSG_CIPHER_MODE_COMPLETE:
1425 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1426 if (msg->cipher_mode_complete.alg_id) {
1427 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1428 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1429 msc_a->geran_encr.alg_id - 1);
1430 };
1431 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1432 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001433
1434 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1435 if (msg->cipher_mode_complete.l3_msg) {
1436 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1437 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1438 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1439 unsigned char *pos = msgb_put(dtap, len);
1440 memcpy(pos, data, len);
1441 dtap->l3h = pos;
1442 rc = msc_a_up_l3(msc_a, dtap);
1443 msgb_free(dtap);
1444 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001445 break;
1446
1447 case RAN_MSG_CIPHER_MODE_REJECT:
1448 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1449 rc = 0;
1450 break;
1451
1452 case RAN_MSG_ASSIGNMENT_COMPLETE:
1453 msc_a_up_call_assignment_complete(msc_a, msg);
1454 rc = 0;
1455 break;
1456
1457 case RAN_MSG_ASSIGNMENT_FAILURE:
1458 msc_a_up_call_assignment_failure(msc_a, msg);
1459 rc = 0;
1460 break;
1461
1462 case RAN_MSG_SAPI_N_REJECT:
1463 msc_a_up_sapi_n_reject(msc_a, msg);
1464 rc = 0;
1465 break;
1466
1467 case RAN_MSG_HANDOVER_PERFORMED:
1468 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1469 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1470 break;
1471
1472 case RAN_MSG_HANDOVER_REQUIRED:
1473 /* The BSS lets us know that it wants to handover to a different cell */
1474 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1475 break;
1476
1477 case RAN_MSG_HANDOVER_FAILURE:
1478 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1479 break;
1480
1481 default:
1482 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1483 rc = -ENOTSUP;
1484 break;
1485 }
1486 return rc;
1487}
1488
1489static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1490{
1491 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1492 int rc = -99;
1493
1494 if (!msc_t) {
1495 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1496 return -EINVAL;
1497 }
1498
1499 OSMO_ASSERT(d->ran_dec);
1500
1501 switch (d->ran_dec->msg_type) {
1502
1503 case RAN_MSG_CLEAR_REQUEST:
1504 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1505 break;
1506
1507 case RAN_MSG_CLEAR_COMPLETE:
1508 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1509 break;
1510
1511 case RAN_MSG_CLASSMARK_UPDATE:
1512 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1513 rc = 0;
1514 break;
1515
1516 case RAN_MSG_HANDOVER_REQUEST_ACK:
1517 /* new BSS accepts Handover */
1518 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1519 break;
1520
1521 case RAN_MSG_HANDOVER_DETECT:
1522 /* new BSS signals the MS is DETECTed on the new lchan */
1523 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1524 break;
1525
1526 case RAN_MSG_HANDOVER_COMPLETE:
1527 /* new BSS signals the MS has fully moved to the new lchan */
1528 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1529 break;
1530
1531 case RAN_MSG_HANDOVER_FAILURE:
1532 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1533 break;
1534
1535 default:
1536 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1537 ran_msg_type_name(d->ran_dec->msg_type));
1538 rc = -ENOTSUP;
1539 break;
1540 }
1541 return rc;
1542}
1543
1544int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1545{
1546 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1547 struct msc_a_ran_dec_data *d = data;
1548 int rc = -99;
1549
1550 d->ran_dec = msg;
1551
1552 switch (d->from_role) {
1553 case MSC_ROLE_I:
1554 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1555 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1556 break;
1557
1558 case MSC_ROLE_T:
1559 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1560 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1561 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1562 break;
1563
1564 default:
1565 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1566 ran_msg_type_name(msg->msg_type));
1567 return -ENOTSUP;
1568 }
1569
1570 if (rc)
1571 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),
1572 msc_role_name(d->from_role));
1573 return rc;
1574}
1575
1576/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1577int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1578 const char *file, int line)
1579{
1580 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1581 ran_msg, file, line);
1582}
1583
1584/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1585 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1586int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1587 const struct ran_msg *ran_msg,
1588 const char *file, int line)
1589{
1590 struct an_apdu an_apdu = {
1591 .an_proto = msc_a->c.ran->an_proto,
1592 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1593 };
1594 int rc;
1595 if (!an_apdu.msg)
1596 return -EIO;
1597 rc = _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
1598 msgb_free(an_apdu.msg);
1599 return rc;
1600}
1601
1602int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
1603{
1604 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001605 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
1606 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001607
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001608 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001609 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
1610 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1611 msgb_free(dtap);
1612 return -EIO;
1613 }
1614
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001615 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
1616 /* The SGs connection to the MME always is at the MSC-A. */
1617 return sgs_iface_tx_dtap_ud(msc_a, dtap);
1618 }
1619
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001620 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
1621 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1622
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001623 ran_msg = (struct ran_msg){
1624 .msg_type = RAN_MSG_DTAP,
1625 .dtap = dtap,
1626 };
1627 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
1628}
1629
1630struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
1631{
1632 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
1633 if (valid_conn_only && !msc_a_is_accepted(msc_a))
1634 return NULL;
1635 return msc_a;
1636}
1637
1638int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
1639{
1640 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Vadim Yanitskiy435f67f2021-06-06 15:47:49 +02001641 if (vsub == NULL)
1642 return -ENODEV;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001643 struct ran_msg msg = {
1644 .msg_type = RAN_MSG_COMMON_ID,
1645 .common_id = {
1646 .imsi = vsub->imsi,
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001647 .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001648 },
1649 };
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001650 if (vsub->sgs.last_eutran_plmn_present) {
1651 memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn,
1652 sizeof(vsub->sgs.last_eutran_plmn));
1653 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001654
1655 return msc_a_ran_down(msc_a, to_role, &msg);
1656}
1657
1658static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
1659{
1660 struct call_leg *cl = msc_a->cc.call_leg;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001661 struct msc_i *msc_i = msc_a_msc_i(msc_a);
1662 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001663
1664 OSMO_ASSERT(!msc_a->cc.active_trans);
1665 msc_a->cc.active_trans = cc_trans;
1666
1667 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
1668
1669 if (!cl) {
1670 cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi,
1671 MSC_EV_CALL_LEG_TERM,
1672 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
Neels Hofmeyr265a4c72019-05-09 16:20:51 +02001673 MSC_EV_CALL_LEG_RTP_COMPLETE);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001674 OSMO_ASSERT(cl);
1675
Martin Hauke3f07dac2019-11-14 17:49:08 +01001676 /* HACK: We put the connection in loopback mode at the beginning to
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001677 * trick the hNodeB into doing the IuUP negotiation with itself.
1678 * This is a hack we need because osmo-mgw does not support IuUP yet, see OS#2459. */
1679 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU)
1680 cl->crcx_conn_mode[RTP_TO_RAN] = MGCP_CONN_LOOPBACK;
1681 }
1682
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001683 if (net->use_osmux != OSMUX_USAGE_OFF) {
1684 msc_i = msc_a_msc_i(msc_a);
1685 if (msc_i->c.remote_to) {
1686 /* TODO: investigate what to do in this case */
1687 LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC");
1688 } else {
1689 cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux;
1690 }
1691 }
1692
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001693 /* This will lead to either MSC_EV_CALL_LEG_LOCAL_ADDR_AVAILABLE or MSC_EV_CALL_LEG_TERM.
1694 * If the local address is already known, then immediately trigger. */
1695 if (call_leg_local_ip(cl, RTP_TO_RAN))
1696 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
1697 else
1698 return call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_RAN, cc_trans->callref, cc_trans, NULL, NULL);
1699}
1700
1701int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
1702{
1703 struct msc_a *msc_a = cc_trans->msc_a;
1704 OSMO_ASSERT(cc_trans->type == TRANS_CC);
1705
1706 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02001707 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001708 return 0;
1709 }
1710
1711 if (msc_a->cc.active_trans) {
1712 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
1713 return 0;
1714 }
1715
1716 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
1717 return msc_a_start_assignment(msc_a, cc_trans);
1718}
1719
1720const char *msc_a_cm_service_type_to_use(enum osmo_cm_service_type cm_service_type)
1721{
1722 switch (cm_service_type) {
1723 case GSM48_CMSERV_MO_CALL_PACKET:
1724 case GSM48_CMSERV_EMERGENCY:
1725 return MSC_A_USE_CM_SERVICE_CC;
1726
1727 case GSM48_CMSERV_SMS:
1728 return MSC_A_USE_CM_SERVICE_SMS;
1729
1730 case GSM48_CMSERV_SUP_SERV:
1731 return MSC_A_USE_CM_SERVICE_SS;
1732
1733 default:
1734 return NULL;
1735 }
1736}
1737
1738void msc_a_release_cn(struct msc_a *msc_a)
1739{
1740 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
1741}
1742
1743void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
1744{
1745 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1746}