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