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