blob: bd69eedbaba2c615483340e740ec54b8d94737c1 [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,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200530 .osmux_present = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->use_osmux,
531 .osmux_cid = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local_osmux_cid,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100532 },
533 };
534 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
535 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Assignment\n");
536 /* FIXME: ERROR HANDLING */
537 return;
538 }
539}
540
541static void msc_a_call_leg_cn_local_addr_available(struct msc_a *msc_a, struct gsm_trans *cc_trans)
542{
543 if (gsm48_tch_rtp_create(cc_trans)) {
544 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot inform MNCC of RTP address\n");
545 /* FIXME: ERROR HANDLING */
546 return;
547 }
548}
549
550static struct gsm_trans *find_waiting_call(struct msc_a *msc_a)
551{
552 struct gsm_trans *trans;
553 struct gsm_network *net = msc_a_net(msc_a);
554
555 llist_for_each_entry(trans, &net->trans_list, entry) {
556 if (trans->msc_a != msc_a)
557 continue;
558 if (trans->type != TRANS_CC)
559 continue;
560 if (trans->msc_a->cc.active_trans == trans)
561 continue;
562 return trans;
563 }
564 return NULL;
565}
566
567static void msc_a_cleanup_rtp_streams(struct msc_a *msc_a, uint32_t event, void *data)
568{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100569 switch (event) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100570
571 case MSC_EV_CALL_LEG_TERM:
572 msc_a->cc.call_leg = NULL;
573 if (msc_a->cc.mncc_forwarding_to_remote_ran)
574 msc_a->cc.mncc_forwarding_to_remote_ran->rtps = NULL;
575
Neels Hofmeyr265a4c72019-05-09 16:20:51 +0200576 if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100577 msc_a->ho.new_cell.mncc_forwarding_to_remote_ran->rtps = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100578 return;
579
580 case MSC_MNCC_EV_CALL_ENDED:
581 msc_a->cc.mncc_forwarding_to_remote_ran = NULL;
582 return;
583
584 default:
585 return;
586 }
587}
588
589static void msc_a_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
590{
591 struct msc_a *msc_a = fi->priv;
592 struct rtp_stream *rtps;
593 struct gsm_trans *waiting_trans;
594 struct an_apdu *an_apdu;
595
596 msc_a_cleanup_rtp_streams(msc_a, event, data);
597
598 switch (event) {
599 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
600 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
601 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
602 an_apdu = data;
603 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
604 return;
605
606 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE:
607 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE:
608 case MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST:
609 case MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST:
610 an_apdu = data;
611 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_T);
612 return;
613
614 case MSC_A_EV_TRANSACTION_ACCEPTED:
615 /* no-op */
616 return;
617
618 case MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE:
619 rtps = data;
620 if (!rtps) {
621 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
622 return;
623 }
624 LOG_MSC_A(msc_a, LOGL_DEBUG,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200625 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
626 rtp_direction_name(rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local),
627 rtps->use_osmux ? "yes" : "no", rtps->local_osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100628 switch (rtps->dir) {
629 case RTP_TO_RAN:
630 msc_a_call_leg_ran_local_addr_available(msc_a);
631 return;
632 case RTP_TO_CN:
633 msc_a_call_leg_cn_local_addr_available(msc_a, rtps->for_trans);
634 return;
635 default:
636 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
637 return;
638 }
639
640 case MSC_EV_CALL_LEG_RTP_COMPLETE:
641 /* Nothing to do. */
642 return;
643
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100644 case MSC_MNCC_EV_CALL_ENDED:
645 /* Cleaned up above */
646 return;
647
648 case MSC_EV_CALL_LEG_TERM:
649 /* RTP streams cleaned up above */
650
651 msc_a_get(msc_a, __func__);
652 if (msc_a->cc.active_trans)
653 trans_free(msc_a->cc.active_trans);
654
655 /* If there is another call still waiting to be activated, this is the time when the mgcp_ctx is
656 * available again and the other call can start assigning. */
657 waiting_trans = find_waiting_call(msc_a);
658 if (waiting_trans) {
659 LOG_MSC_A(msc_a, LOGL_DEBUG, "(ti %02x) Call waiting: starting Assignment\n",
660 waiting_trans->transaction_id);
661 msc_a_try_call_assignment(waiting_trans);
662 }
663 msc_a_put(msc_a, __func__);
664 return;
665
666 case MSC_A_EV_HANDOVER_REQUIRED:
667 msc_ho_start(msc_a, (struct ran_handover_required*)data);
668 return;
669
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200670 case MSC_A_EV_HANDOVER_END:
671 /* Termination event of the msc_ho_fsm. No action needed, it's all done in the msc_ho_fsm cleanup. This
672 * event only exists because osmo_fsm_inst_alloc_child() requires a parent term event; and maybe
673 * interesting for logging. */
674 return;
675
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100676 case MSC_A_EV_MO_CLOSE:
677 case MSC_A_EV_CN_CLOSE:
678 case MSC_A_EV_UNUSED:
679 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
680 return;
681
682 default:
683 OSMO_ASSERT(false);
684 }
685}
686
687static int msc_a_fsm_timer_cb(struct osmo_fsm_inst *fi)
688{
689 struct msc_a *msc_a = fi->priv;
690 if (msc_a_in_release(msc_a)) {
691 LOG_MSC_A(msc_a, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
692 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
693 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
694 } else {
695 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
696 osmo_fsm_inst_dispatch(fi, MSC_A_EV_CN_CLOSE, &cause);
697 }
698 return 0;
699}
700
701static void msc_a_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
702{
703 struct msc_a *msc_a = fi->priv;
704 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
705 int i;
706 char buf[128];
707 const char * const use_counts_to_cancel[] = {
708 MSC_A_USE_LOCATION_UPDATING,
709 MSC_A_USE_CM_SERVICE_CC,
710 MSC_A_USE_CM_SERVICE_SMS,
711 MSC_A_USE_CM_SERVICE_SS,
712 MSC_A_USE_PAGING_RESPONSE,
713 };
714
715 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: msc_a use is %s\n",
716 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
717
718 if (vsub) {
719 vlr_subscr_get(vsub, __func__);
720
721 /* Cancel all VLR FSMs, if any */
722 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
723
724 /* The subscriber has no active connection anymore.
725 * Restart the periodic Location Update expiry timer for this subscriber. */
726 vlr_subscr_enable_expire_lu(vsub);
727 }
728
729 /* If we're closing in a middle of a trans, we need to clean up */
730 trans_conn_closed(msc_a);
731
732 call_leg_release(msc_a->cc.call_leg);
733
734 /* Cancel use counts for pending CM Service / Paging */
735 for (i = 0; i < ARRAY_SIZE(use_counts_to_cancel); i++) {
736 const char *use = use_counts_to_cancel[i];
737 int32_t count = osmo_use_count_by(&msc_a->use_count, use);
738 if (!count)
739 continue;
740 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: canceling still pending use: %s (%d)\n", use, count);
741 osmo_use_count_get_put(&msc_a->use_count, use, -count);
742 }
743
744 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
745 sgs_iface_tx_release(vsub);
746 /* In SGsAP there is no confirmation of a release. */
747 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
748 } else {
749 struct ran_msg msg = {
750 .msg_type = RAN_MSG_CLEAR_COMMAND,
751 .clear_command = {
752 .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED),
753 },
754 };
755 msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
756 msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
757 }
758
759 if (vsub)
760 vlr_subscr_put(vsub, __func__);
761}
762
763static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
764{
765 struct msc_a *msc_a = fi->priv;
766
767 msc_a_cleanup_rtp_streams(msc_a, event, data);
768
769 switch (event) {
770 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
771 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
772 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
773 return;
774
775 case MSC_A_EV_MO_CLOSE:
776 case MSC_A_EV_CN_CLOSE:
777 case MSC_A_EV_UNUSED:
778 /* Already releasing */
779 return;
780
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100781 case MSC_EV_CALL_LEG_TERM:
782 case MSC_MNCC_EV_CALL_ENDED:
783 /* RTP streams cleaned up above */
784 return;
785
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200786 case MSC_A_EV_HANDOVER_END:
787 /* msc_ho_fsm does cleanup. */
788 return;
789
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100790 default:
791 OSMO_ASSERT(false);
792 }
793}
794
795
796static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
797{
798 struct msc_a *msc_a = msc_a_fi_priv(fi);
799 char buf[128];
800 LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n",
801 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
802 if (osmo_use_count_total(&msc_a->use_count) == 0)
803 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
804}
805
806static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
807{
808 if (event == MSC_A_EV_UNUSED)
809 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
810}
811
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100812void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
813{
814 struct msc_a *msc_a = msc_a_fi_priv(fi);
815
816 trans_conn_closed(msc_a);
817
818 if (msc_a_fsm_has_active_transactions(fi))
819 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
820
821 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
822}
823
824const struct value_string msc_a_fsm_event_names[] = {
825 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
826 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
827 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100828 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
829 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
830 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
831 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
832 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
833 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
834 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
835 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
836 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
837 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
838 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
839 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
840 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
841 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
842 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
843 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
844 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
845 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
846 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
847 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
848 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
849 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
850 {}
851};
852
853#define S(x) (1 << (x))
854
855static const struct osmo_fsm_state msc_a_fsm_states[] = {
856 [MSC_A_ST_VALIDATE_L3] = {
857 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
858 .in_event_mask = 0
859 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
860 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
861 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
862 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
863 | S(MSC_A_EV_MO_CLOSE)
864 | S(MSC_A_EV_CN_CLOSE)
865 | S(MSC_A_EV_UNUSED)
866 ,
867 .out_state_mask = 0
868 | S(MSC_A_ST_VALIDATE_L3)
869 | S(MSC_A_ST_AUTH_CIPH)
870 | S(MSC_A_ST_RELEASING)
871 ,
872 .action = msc_a_fsm_validate_l3,
873 },
874 [MSC_A_ST_AUTH_CIPH] = {
875 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
876 .in_event_mask = 0
877 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
878 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
879 | S(MSC_A_EV_AUTHENTICATED)
880 | S(MSC_A_EV_MO_CLOSE)
881 | S(MSC_A_EV_CN_CLOSE)
882 | S(MSC_A_EV_UNUSED)
883 ,
884 .out_state_mask = 0
885 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
886 | S(MSC_A_ST_AUTHENTICATED)
887 | S(MSC_A_ST_RELEASING)
888 ,
889 .action = msc_a_fsm_auth_ciph,
890 },
891 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
892 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
893 .in_event_mask = 0
894 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
895 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
896 | S(MSC_A_EV_CLASSMARK_UPDATE)
897 | S(MSC_A_EV_MO_CLOSE)
898 | S(MSC_A_EV_CN_CLOSE)
899 ,
900 .out_state_mask = 0
901 | S(MSC_A_ST_AUTH_CIPH)
902 | S(MSC_A_ST_RELEASING)
903 ,
904 .action = msc_a_fsm_wait_classmark_update,
905 },
906 [MSC_A_ST_AUTHENTICATED] = {
907 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
908 /* allow everything to release for any odd behavior */
909 .in_event_mask = 0
910 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
911 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
912 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
913 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
914 | S(MSC_A_EV_MO_CLOSE)
915 | S(MSC_A_EV_CN_CLOSE)
916 | S(MSC_A_EV_UNUSED)
917 ,
918 .out_state_mask = 0
919 | S(MSC_A_ST_RELEASING)
920 | S(MSC_A_ST_COMMUNICATING)
921 ,
922 .onenter = msc_a_fsm_authenticated_enter,
923 .action = msc_a_fsm_authenticated,
924 },
925 [MSC_A_ST_COMMUNICATING] = {
926 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
927 /* allow everything to release for any odd behavior */
928 .in_event_mask = 0
929 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
930 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
931 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
932 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
933 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
934 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
935 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
936 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
937 | S(MSC_A_EV_MO_CLOSE)
938 | S(MSC_A_EV_CN_CLOSE)
939 | S(MSC_A_EV_UNUSED)
940 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
941 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100942 | S(MSC_EV_CALL_LEG_TERM)
943 | S(MSC_MNCC_EV_CALL_ENDED)
944 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200945 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100946 ,
947 .out_state_mask = 0
948 | S(MSC_A_ST_RELEASING)
949 ,
950 .action = msc_a_fsm_communicating,
951 },
952 [MSC_A_ST_RELEASING] = {
953 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
954 .in_event_mask = 0
955 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
956 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
957 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100958 | S(MSC_EV_CALL_LEG_TERM)
959 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200960 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100961 ,
962 .out_state_mask = 0
963 | S(MSC_A_ST_RELEASED)
964 ,
965 .onenter = msc_a_fsm_releasing_onenter,
966 .action = msc_a_fsm_releasing,
967 },
968 [MSC_A_ST_RELEASED] = {
969 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
970 .in_event_mask = 0
971 | S(MSC_A_EV_UNUSED)
972 ,
973 .onenter = msc_a_fsm_released_onenter,
974 .action = msc_a_fsm_released,
975 },
976};
977
978static struct osmo_fsm msc_a_fsm = {
979 .name = "msc_a",
980 .states = msc_a_fsm_states,
981 .num_states = ARRAY_SIZE(msc_a_fsm_states),
982 .log_subsys = DMSC,
983 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100984 .timer_cb = msc_a_fsm_timer_cb,
985 .cleanup = msc_a_fsm_cleanup,
986};
987
988static __attribute__((constructor)) void msc_a_fsm_init()
989{
990 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
991}
992
993static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
994{
995 struct msc_a *msc_a = e->use_count->talloc_object;
996 char buf[128];
997 int32_t total;
998 int level;
999
1000 if (!e->use)
1001 return -EINVAL;
1002
1003 total = osmo_use_count_total(&msc_a->use_count);
1004
1005 if (total == 0
1006 || (total == 1 && old_use_count == 0 && e->count == 1))
1007 level = LOGL_INFO;
1008 else
1009 level = LOGL_DEBUG;
1010
1011 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1012 (e->count - old_use_count) > 0? "+" : "-", e->use,
1013 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1014
1015 if (e->count < 0)
1016 return -ERANGE;
1017
1018 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1019
1020 if (total == 0)
1021 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1022 return 0;
1023}
1024
1025struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1026{
1027 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1028 msc_a->use_count = (struct osmo_use_count){
1029 .talloc_object = msc_a,
1030 .use_cb = msc_a_use_cb,
1031 };
1032 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1033 /* Start timeout for first state */
1034 msc_a_state_chg(msc_a, MSC_A_ST_VALIDATE_L3);
1035 return msc_a;
1036}
1037
1038bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1039{
1040 if (!msc_a || !msc_a->c.fi)
1041 return false;
1042 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1043}
1044
1045const struct value_string complete_layer3_type_names[] = {
1046 { COMPLETE_LAYER3_NONE, "NONE" },
1047 { COMPLETE_LAYER3_LU, "LU" },
1048 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1049 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
1050 { 0, NULL }
1051};
1052
1053#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1054 do { \
1055 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1056 ## ARGS, \
1057 msub_ran_conn_name(msc_a->c.msub), \
1058 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1059 == 0) { \
1060 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1061 if (_vsub) { \
1062 if (_vsub->lu_fsm) \
1063 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1064 if (_vsub->auth_fsm) \
1065 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1066 if (_vsub->proc_arq_fsm) \
1067 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1068 } \
1069 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1070 } \
1071 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1072 } while (0)
1073
1074
1075/* Compose an ID almost like gsm48_mi_to_string(), but print the MI type along, and print a TMSI as hex. */
1076void msc_a_update_id_from_mi(struct msc_a *msc_a, const uint8_t mi[], uint8_t mi_len)
1077{
1078 _msc_a_update_id(msc_a, "%s", osmo_mi_name(mi, mi_len));
1079}
1080
1081/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1082void msc_a_update_id(struct msc_a *msc_a)
1083{
1084 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1085}
1086
1087/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1088 * instances. */
1089void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1090{
1091 struct msub *msub;
1092 llist_for_each_entry(msub, &msub_list, entry) {
1093 struct vlr_subscr *vsub = msub_vsub(msub);
1094 if (vsub != for_vsub)
1095 continue;
1096 msc_a_update_id(msub_msc_a(msub));
1097 }
1098}
1099
1100static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1101{
1102 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1103 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1104
1105 switch (pdisc) {
1106 case GSM48_PDISC_MM:
1107 switch (msg_type) {
1108 case GSM48_MT_MM_LOC_UPD_REQUEST:
1109 case GSM48_MT_MM_CM_SERV_REQ:
1110 case GSM48_MT_MM_CM_REEST_REQ:
1111 case GSM48_MT_MM_AUTH_RESP:
1112 case GSM48_MT_MM_AUTH_FAIL:
1113 case GSM48_MT_MM_ID_RESP:
1114 case GSM48_MT_MM_TMSI_REALL_COMPL:
1115 case GSM48_MT_MM_IMSI_DETACH_IND:
1116 return true;
1117 default:
1118 break;
1119 }
1120 break;
1121 case GSM48_PDISC_RR:
1122 switch (msg_type) {
1123 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1124 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1125 case GSM48_MT_RR_PAG_RESP:
1126 case GSM48_MT_RR_CIPH_M_COMPL:
1127 return true;
1128 default:
1129 break;
1130 }
1131 break;
1132 default:
1133 break;
1134 }
1135
1136 return false;
1137}
1138
1139/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1140int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1141{
1142 struct gsm48_hdr *gh;
1143 uint8_t pdisc;
1144 int rc;
1145 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1146 int is_r99;
1147
1148 OSMO_ASSERT(msg->l3h);
1149 OSMO_ASSERT(msg);
1150
1151 gh = msgb_l3(msg);
1152 pdisc = gsm48_hdr_pdisc(gh);
1153
1154 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1155 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1156
1157 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1158 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1159 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1160 if (vsub)
1161 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1162 else
1163 is_r99 = compl_l3_msg_is_r99(msg);
1164
1165 if (is_r99 < 0) {
1166 LOG_MSC_A(msc_a, LOGL_ERROR,
1167 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1168 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1169 return -EACCES;
1170 }
1171
1172 if (is_r99 >= 0
1173 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1174 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1175 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1176 return 0;
1177 }
1178
1179 if (!msc_a_is_accepted(msc_a)
1180 && !msg_is_initially_permitted(gh)) {
1181 LOG_MSC_A(msc_a, LOGL_ERROR,
1182 "Message not permitted for initial conn: %s\n",
1183 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1184 return -EACCES;
1185 }
1186
1187 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1188 LOG_MSC_A(msc_a, LOGL_ERROR,
1189 "Illegal situation: RAN type mismatch:"
1190 " attached via %s, received message via %s\n",
1191 osmo_rat_type_name(vsub->cs.attached_via_ran),
1192 osmo_rat_type_name(msc_a->c.ran->type));
1193 return -EACCES;
1194 }
1195
1196#if 0
1197 if (silent_call_reroute(conn, msg))
1198 return silent_call_rx(conn, msg);
1199#endif
1200
1201 switch (pdisc) {
1202 case GSM48_PDISC_CC:
1203 rc = gsm0408_rcv_cc(msc_a, msg);
1204 break;
1205 case GSM48_PDISC_MM:
1206 rc = gsm0408_rcv_mm(msc_a, msg);
1207 break;
1208 case GSM48_PDISC_RR:
1209 rc = gsm0408_rcv_rr(msc_a, msg);
1210 break;
1211 case GSM48_PDISC_SMS:
1212 rc = gsm0411_rcv_sms(msc_a, msg);
1213 break;
1214 case GSM48_PDISC_MM_GPRS:
1215 case GSM48_PDISC_SM_GPRS:
1216 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1217 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1218 rc = -ENOTSUP;
1219 break;
1220 case GSM48_PDISC_NC_SS:
1221 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1222 break;
1223 case GSM48_PDISC_TEST:
1224 rc = gsm0414_rcv_test(msc_a, msg);
1225 break;
1226 default:
1227 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1228 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1229 rc = -EINVAL;
1230 break;
1231 }
1232
1233 return rc;
1234}
1235
1236static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1237{
1238 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
1239 struct rtp_stream *rtps_to_ran = msc_a->cc.call_leg ? msc_a->cc.call_leg->rtp[RTP_TO_RAN] : NULL;
1240
1241 if (!rtps_to_ran) {
1242 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1243 return;
1244 }
1245 if (!cc_trans) {
1246 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but CC transaction is active\n");
1247 return;
1248 }
1249
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001250 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1251 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1252 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1253 call_leg_release(msc_a->cc.call_leg);
1254 return;
1255 }
1256
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001257 /* Update RAN-side endpoint CI: */
1258 rtp_stream_set_codec(rtps_to_ran, ac->assignment_complete.codec);
1259 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001260 if (rtps_to_ran->use_osmux)
1261 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1262 ac->assignment_complete.osmux_cid);
1263
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001264 rtp_stream_commit(rtps_to_ran);
1265
1266 /* Setup CN side endpoint CI:
1267 * Now that
1268 * - the first CI has been created and a definitive endpoint name is assigned to the call_leg's MGW
1269 * endpoint,
1270 * - the Assignment has chosen a speech codec
1271 * go on to create the CN side RTP stream's CI. */
1272 if (call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_CN, cc_trans->callref, cc_trans,
1273 &ac->assignment_complete.codec, NULL)) {
1274 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Error creating MGW CI towards CN\n");
1275 call_leg_release(msc_a->cc.call_leg);
1276 return;
1277 }
1278}
1279
1280static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1281{
1282 struct gsm_trans *trans;
1283
1284 /* For a normal voice call, there will be an rtp_stream FSM. */
1285 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1286 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1287 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1288 return;
1289 }
1290
1291 /* Otherwise, a silent call might be active */
1292 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1293 if (trans) {
1294 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1295 trans_free(trans);
1296 return;
1297 }
1298
1299 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1300 msc_a_release_cn(msc_a);
1301}
1302
1303static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1304 struct osmo_gsm48_classmark *dst)
1305{
1306 if (!dst) {
1307 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1308
1309 if (!vsub)
1310 dst = &msc_a->temporary_classmark;
1311 else
1312 dst = &vsub->classmark;
1313 }
1314
1315 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities recived from Classmark Update: %s\n",
1316 osmo_gsm48_classmark_a5_name(classmark));
1317 osmo_gsm48_classmark_update(dst, classmark);
1318
1319 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1320 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1321 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1322}
1323
1324static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1325{
1326 int sapi = msg->sapi_n_reject.dlci & 0x7;
1327 if (sapi == UM_SAPI_SMS)
1328 gsm411_sapi_n_reject(msc_a);
1329}
1330
1331static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1332{
1333 if (!msc_a->ho.fi) {
1334 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1335 return -EINVAL;
1336 }
1337 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1338}
1339
1340int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1341{
1342 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1343 const struct ran_msg *msg = d->ran_dec;
1344 int rc = -99;
1345
1346 switch (msg->msg_type) {
1347
1348 case RAN_MSG_COMPL_L3:
1349 msc_a->via_cell = (struct osmo_cell_global_id){
1350 .lai.plmn = msc_a_net(msc_a)->plmn,
1351 };
1352 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
1353 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1354 if (!rc) {
1355 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1356 if (conn)
1357 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1358 }
1359 break;
1360
1361 case RAN_MSG_DTAP:
1362 rc = msc_a_up_l3(msc_a, msg->dtap);
1363 break;
1364
1365 case RAN_MSG_CLEAR_REQUEST:
1366 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1367 break;
1368
1369 case RAN_MSG_CLEAR_COMPLETE:
1370 switch (msc_a->c.fi->state) {
1371 case MSC_A_ST_RELEASING:
1372 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1373 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1374 break;
1375 case MSC_A_ST_RELEASED:
1376 break;
1377 default:
1378 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1379 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1380 break;
1381 }
1382 rc = 0;
1383 break;
1384
1385 case RAN_MSG_CLASSMARK_UPDATE:
1386 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1387 rc = 0;
1388 break;
1389
1390 case RAN_MSG_CIPHER_MODE_COMPLETE:
1391 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1392 if (msg->cipher_mode_complete.alg_id) {
1393 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1394 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1395 msc_a->geran_encr.alg_id - 1);
1396 };
1397 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1398 rc = 0;
1399 break;
1400
1401 case RAN_MSG_CIPHER_MODE_REJECT:
1402 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1403 rc = 0;
1404 break;
1405
1406 case RAN_MSG_ASSIGNMENT_COMPLETE:
1407 msc_a_up_call_assignment_complete(msc_a, msg);
1408 rc = 0;
1409 break;
1410
1411 case RAN_MSG_ASSIGNMENT_FAILURE:
1412 msc_a_up_call_assignment_failure(msc_a, msg);
1413 rc = 0;
1414 break;
1415
1416 case RAN_MSG_SAPI_N_REJECT:
1417 msc_a_up_sapi_n_reject(msc_a, msg);
1418 rc = 0;
1419 break;
1420
1421 case RAN_MSG_HANDOVER_PERFORMED:
1422 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1423 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1424 break;
1425
1426 case RAN_MSG_HANDOVER_REQUIRED:
1427 /* The BSS lets us know that it wants to handover to a different cell */
1428 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1429 break;
1430
1431 case RAN_MSG_HANDOVER_FAILURE:
1432 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1433 break;
1434
1435 default:
1436 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1437 rc = -ENOTSUP;
1438 break;
1439 }
1440 return rc;
1441}
1442
1443static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1444{
1445 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1446 int rc = -99;
1447
1448 if (!msc_t) {
1449 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1450 return -EINVAL;
1451 }
1452
1453 OSMO_ASSERT(d->ran_dec);
1454
1455 switch (d->ran_dec->msg_type) {
1456
1457 case RAN_MSG_CLEAR_REQUEST:
1458 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1459 break;
1460
1461 case RAN_MSG_CLEAR_COMPLETE:
1462 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1463 break;
1464
1465 case RAN_MSG_CLASSMARK_UPDATE:
1466 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1467 rc = 0;
1468 break;
1469
1470 case RAN_MSG_HANDOVER_REQUEST_ACK:
1471 /* new BSS accepts Handover */
1472 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1473 break;
1474
1475 case RAN_MSG_HANDOVER_DETECT:
1476 /* new BSS signals the MS is DETECTed on the new lchan */
1477 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1478 break;
1479
1480 case RAN_MSG_HANDOVER_COMPLETE:
1481 /* new BSS signals the MS has fully moved to the new lchan */
1482 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1483 break;
1484
1485 case RAN_MSG_HANDOVER_FAILURE:
1486 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1487 break;
1488
1489 default:
1490 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1491 ran_msg_type_name(d->ran_dec->msg_type));
1492 rc = -ENOTSUP;
1493 break;
1494 }
1495 return rc;
1496}
1497
1498int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1499{
1500 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1501 struct msc_a_ran_dec_data *d = data;
1502 int rc = -99;
1503
1504 d->ran_dec = msg;
1505
1506 switch (d->from_role) {
1507 case MSC_ROLE_I:
1508 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1509 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1510 break;
1511
1512 case MSC_ROLE_T:
1513 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1514 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1515 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1516 break;
1517
1518 default:
1519 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1520 ran_msg_type_name(msg->msg_type));
1521 return -ENOTSUP;
1522 }
1523
1524 if (rc)
1525 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),
1526 msc_role_name(d->from_role));
1527 return rc;
1528}
1529
1530/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1531int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1532 const char *file, int line)
1533{
1534 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1535 ran_msg, file, line);
1536}
1537
1538/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1539 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1540int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1541 const struct ran_msg *ran_msg,
1542 const char *file, int line)
1543{
1544 struct an_apdu an_apdu = {
1545 .an_proto = msc_a->c.ran->an_proto,
1546 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1547 };
1548 int rc;
1549 if (!an_apdu.msg)
1550 return -EIO;
1551 rc = _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
1552 msgb_free(an_apdu.msg);
1553 return rc;
1554}
1555
1556int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
1557{
1558 struct ran_msg ran_msg;
1559
1560 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
1561 /* The SGs connection to the MME always is at the MSC-A. */
1562 return sgs_iface_tx_dtap_ud(msc_a, dtap);
1563 }
1564
1565 ran_msg = (struct ran_msg){
1566 .msg_type = RAN_MSG_DTAP,
1567 .dtap = dtap,
1568 };
1569 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
1570}
1571
1572struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
1573{
1574 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
1575 if (valid_conn_only && !msc_a_is_accepted(msc_a))
1576 return NULL;
1577 return msc_a;
1578}
1579
1580int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
1581{
1582 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1583 struct ran_msg msg = {
1584 .msg_type = RAN_MSG_COMMON_ID,
1585 .common_id = {
1586 .imsi = vsub->imsi,
1587 },
1588 };
1589
1590 return msc_a_ran_down(msc_a, to_role, &msg);
1591}
1592
1593static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
1594{
1595 struct call_leg *cl = msc_a->cc.call_leg;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001596 struct msc_i *msc_i = msc_a_msc_i(msc_a);
1597 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001598
1599 OSMO_ASSERT(!msc_a->cc.active_trans);
1600 msc_a->cc.active_trans = cc_trans;
1601
1602 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
1603
1604 if (!cl) {
1605 cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi,
1606 MSC_EV_CALL_LEG_TERM,
1607 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
Neels Hofmeyr265a4c72019-05-09 16:20:51 +02001608 MSC_EV_CALL_LEG_RTP_COMPLETE);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001609 OSMO_ASSERT(cl);
1610
1611 /* HACK: We put the connection in loopback mode at the beginnig to
1612 * trick the hNodeB into doing the IuUP negotiation with itself.
1613 * This is a hack we need because osmo-mgw does not support IuUP yet, see OS#2459. */
1614 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU)
1615 cl->crcx_conn_mode[RTP_TO_RAN] = MGCP_CONN_LOOPBACK;
1616 }
1617
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001618 if (net->use_osmux != OSMUX_USAGE_OFF) {
1619 msc_i = msc_a_msc_i(msc_a);
1620 if (msc_i->c.remote_to) {
1621 /* TODO: investigate what to do in this case */
1622 LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC");
1623 } else {
1624 cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux;
1625 }
1626 }
1627
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001628 /* This will lead to either MSC_EV_CALL_LEG_LOCAL_ADDR_AVAILABLE or MSC_EV_CALL_LEG_TERM.
1629 * If the local address is already known, then immediately trigger. */
1630 if (call_leg_local_ip(cl, RTP_TO_RAN))
1631 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
1632 else
1633 return call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_RAN, cc_trans->callref, cc_trans, NULL, NULL);
1634}
1635
1636int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
1637{
1638 struct msc_a *msc_a = cc_trans->msc_a;
1639 OSMO_ASSERT(cc_trans->type == TRANS_CC);
1640
1641 if (msc_a->cc.active_trans == cc_trans) {
1642 /* Assignment for this trans already started earlier. */
1643 return 0;
1644 }
1645
1646 if (msc_a->cc.active_trans) {
1647 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
1648 return 0;
1649 }
1650
1651 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
1652 return msc_a_start_assignment(msc_a, cc_trans);
1653}
1654
1655const char *msc_a_cm_service_type_to_use(enum osmo_cm_service_type cm_service_type)
1656{
1657 switch (cm_service_type) {
1658 case GSM48_CMSERV_MO_CALL_PACKET:
1659 case GSM48_CMSERV_EMERGENCY:
1660 return MSC_A_USE_CM_SERVICE_CC;
1661
1662 case GSM48_CMSERV_SMS:
1663 return MSC_A_USE_CM_SERVICE_SMS;
1664
1665 case GSM48_CMSERV_SUP_SERV:
1666 return MSC_A_USE_CM_SERVICE_SS;
1667
1668 default:
1669 return NULL;
1670 }
1671}
1672
1673void msc_a_release_cn(struct msc_a *msc_a)
1674{
1675 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
1676}
1677
1678void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
1679{
1680 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1681}