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