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