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