blob: b311df3492994b47de36ea51d9ef5ed1dbb7d9bb [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
812void msc_a_fsm_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
813{
814 //struct msc_a *a = msc_a_fi_priv(fi);
815 switch (event) {
816
817 default:
818 return;
819 }
820}
821
822void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
823{
824 struct msc_a *msc_a = msc_a_fi_priv(fi);
825
826 trans_conn_closed(msc_a);
827
828 if (msc_a_fsm_has_active_transactions(fi))
829 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
830
831 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
832}
833
834const struct value_string msc_a_fsm_event_names[] = {
835 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
836 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
837 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100838 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
839 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
840 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
841 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
842 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
843 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
844 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
845 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
846 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
847 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
848 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
849 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
850 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
851 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
852 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
853 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
854 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
855 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
856 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
857 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
858 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
859 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
860 {}
861};
862
863#define S(x) (1 << (x))
864
865static const struct osmo_fsm_state msc_a_fsm_states[] = {
866 [MSC_A_ST_VALIDATE_L3] = {
867 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
868 .in_event_mask = 0
869 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
870 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
871 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
872 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
873 | S(MSC_A_EV_MO_CLOSE)
874 | S(MSC_A_EV_CN_CLOSE)
875 | S(MSC_A_EV_UNUSED)
876 ,
877 .out_state_mask = 0
878 | S(MSC_A_ST_VALIDATE_L3)
879 | S(MSC_A_ST_AUTH_CIPH)
880 | S(MSC_A_ST_RELEASING)
881 ,
882 .action = msc_a_fsm_validate_l3,
883 },
884 [MSC_A_ST_AUTH_CIPH] = {
885 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
886 .in_event_mask = 0
887 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
888 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
889 | S(MSC_A_EV_AUTHENTICATED)
890 | S(MSC_A_EV_MO_CLOSE)
891 | S(MSC_A_EV_CN_CLOSE)
892 | S(MSC_A_EV_UNUSED)
893 ,
894 .out_state_mask = 0
895 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
896 | S(MSC_A_ST_AUTHENTICATED)
897 | S(MSC_A_ST_RELEASING)
898 ,
899 .action = msc_a_fsm_auth_ciph,
900 },
901 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
902 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
903 .in_event_mask = 0
904 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
905 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
906 | S(MSC_A_EV_CLASSMARK_UPDATE)
907 | S(MSC_A_EV_MO_CLOSE)
908 | S(MSC_A_EV_CN_CLOSE)
909 ,
910 .out_state_mask = 0
911 | S(MSC_A_ST_AUTH_CIPH)
912 | S(MSC_A_ST_RELEASING)
913 ,
914 .action = msc_a_fsm_wait_classmark_update,
915 },
916 [MSC_A_ST_AUTHENTICATED] = {
917 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
918 /* allow everything to release for any odd behavior */
919 .in_event_mask = 0
920 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
921 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
922 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
923 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
924 | S(MSC_A_EV_MO_CLOSE)
925 | S(MSC_A_EV_CN_CLOSE)
926 | S(MSC_A_EV_UNUSED)
927 ,
928 .out_state_mask = 0
929 | S(MSC_A_ST_RELEASING)
930 | S(MSC_A_ST_COMMUNICATING)
931 ,
932 .onenter = msc_a_fsm_authenticated_enter,
933 .action = msc_a_fsm_authenticated,
934 },
935 [MSC_A_ST_COMMUNICATING] = {
936 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
937 /* allow everything to release for any odd behavior */
938 .in_event_mask = 0
939 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
940 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
941 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
942 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
943 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
944 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
945 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
946 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
947 | S(MSC_A_EV_MO_CLOSE)
948 | S(MSC_A_EV_CN_CLOSE)
949 | S(MSC_A_EV_UNUSED)
950 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
951 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100952 | S(MSC_EV_CALL_LEG_TERM)
953 | S(MSC_MNCC_EV_CALL_ENDED)
954 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200955 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100956 ,
957 .out_state_mask = 0
958 | S(MSC_A_ST_RELEASING)
959 ,
960 .action = msc_a_fsm_communicating,
961 },
962 [MSC_A_ST_RELEASING] = {
963 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
964 .in_event_mask = 0
965 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
966 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
967 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100968 | S(MSC_EV_CALL_LEG_TERM)
969 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200970 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100971 ,
972 .out_state_mask = 0
973 | S(MSC_A_ST_RELEASED)
974 ,
975 .onenter = msc_a_fsm_releasing_onenter,
976 .action = msc_a_fsm_releasing,
977 },
978 [MSC_A_ST_RELEASED] = {
979 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
980 .in_event_mask = 0
981 | S(MSC_A_EV_UNUSED)
982 ,
983 .onenter = msc_a_fsm_released_onenter,
984 .action = msc_a_fsm_released,
985 },
986};
987
988static struct osmo_fsm msc_a_fsm = {
989 .name = "msc_a",
990 .states = msc_a_fsm_states,
991 .num_states = ARRAY_SIZE(msc_a_fsm_states),
992 .log_subsys = DMSC,
993 .event_names = msc_a_fsm_event_names,
994 .allstate_action = msc_a_fsm_allstate_action,
995 .allstate_event_mask = 0
996 ,
997 .timer_cb = msc_a_fsm_timer_cb,
998 .cleanup = msc_a_fsm_cleanup,
999};
1000
1001static __attribute__((constructor)) void msc_a_fsm_init()
1002{
1003 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1004}
1005
1006static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1007{
1008 struct msc_a *msc_a = e->use_count->talloc_object;
1009 char buf[128];
1010 int32_t total;
1011 int level;
1012
1013 if (!e->use)
1014 return -EINVAL;
1015
1016 total = osmo_use_count_total(&msc_a->use_count);
1017
1018 if (total == 0
1019 || (total == 1 && old_use_count == 0 && e->count == 1))
1020 level = LOGL_INFO;
1021 else
1022 level = LOGL_DEBUG;
1023
1024 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1025 (e->count - old_use_count) > 0? "+" : "-", e->use,
1026 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1027
1028 if (e->count < 0)
1029 return -ERANGE;
1030
1031 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1032
1033 if (total == 0)
1034 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1035 return 0;
1036}
1037
1038struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1039{
1040 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1041 msc_a->use_count = (struct osmo_use_count){
1042 .talloc_object = msc_a,
1043 .use_cb = msc_a_use_cb,
1044 };
1045 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1046 /* Start timeout for first state */
1047 msc_a_state_chg(msc_a, MSC_A_ST_VALIDATE_L3);
1048 return msc_a;
1049}
1050
1051bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1052{
1053 if (!msc_a || !msc_a->c.fi)
1054 return false;
1055 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1056}
1057
1058const struct value_string complete_layer3_type_names[] = {
1059 { COMPLETE_LAYER3_NONE, "NONE" },
1060 { COMPLETE_LAYER3_LU, "LU" },
1061 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1062 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
1063 { 0, NULL }
1064};
1065
1066#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1067 do { \
1068 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1069 ## ARGS, \
1070 msub_ran_conn_name(msc_a->c.msub), \
1071 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1072 == 0) { \
1073 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1074 if (_vsub) { \
1075 if (_vsub->lu_fsm) \
1076 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1077 if (_vsub->auth_fsm) \
1078 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1079 if (_vsub->proc_arq_fsm) \
1080 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1081 } \
1082 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1083 } \
1084 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1085 } while (0)
1086
1087
1088/* Compose an ID almost like gsm48_mi_to_string(), but print the MI type along, and print a TMSI as hex. */
1089void msc_a_update_id_from_mi(struct msc_a *msc_a, const uint8_t mi[], uint8_t mi_len)
1090{
1091 _msc_a_update_id(msc_a, "%s", osmo_mi_name(mi, mi_len));
1092}
1093
1094/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1095void msc_a_update_id(struct msc_a *msc_a)
1096{
1097 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1098}
1099
1100/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1101 * instances. */
1102void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1103{
1104 struct msub *msub;
1105 llist_for_each_entry(msub, &msub_list, entry) {
1106 struct vlr_subscr *vsub = msub_vsub(msub);
1107 if (vsub != for_vsub)
1108 continue;
1109 msc_a_update_id(msub_msc_a(msub));
1110 }
1111}
1112
1113static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1114{
1115 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1116 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1117
1118 switch (pdisc) {
1119 case GSM48_PDISC_MM:
1120 switch (msg_type) {
1121 case GSM48_MT_MM_LOC_UPD_REQUEST:
1122 case GSM48_MT_MM_CM_SERV_REQ:
1123 case GSM48_MT_MM_CM_REEST_REQ:
1124 case GSM48_MT_MM_AUTH_RESP:
1125 case GSM48_MT_MM_AUTH_FAIL:
1126 case GSM48_MT_MM_ID_RESP:
1127 case GSM48_MT_MM_TMSI_REALL_COMPL:
1128 case GSM48_MT_MM_IMSI_DETACH_IND:
1129 return true;
1130 default:
1131 break;
1132 }
1133 break;
1134 case GSM48_PDISC_RR:
1135 switch (msg_type) {
1136 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1137 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1138 case GSM48_MT_RR_PAG_RESP:
1139 case GSM48_MT_RR_CIPH_M_COMPL:
1140 return true;
1141 default:
1142 break;
1143 }
1144 break;
1145 default:
1146 break;
1147 }
1148
1149 return false;
1150}
1151
1152/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1153int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1154{
1155 struct gsm48_hdr *gh;
1156 uint8_t pdisc;
1157 int rc;
1158 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1159 int is_r99;
1160
1161 OSMO_ASSERT(msg->l3h);
1162 OSMO_ASSERT(msg);
1163
1164 gh = msgb_l3(msg);
1165 pdisc = gsm48_hdr_pdisc(gh);
1166
1167 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1168 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1169
1170 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1171 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1172 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1173 if (vsub)
1174 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1175 else
1176 is_r99 = compl_l3_msg_is_r99(msg);
1177
1178 if (is_r99 < 0) {
1179 LOG_MSC_A(msc_a, LOGL_ERROR,
1180 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1181 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1182 return -EACCES;
1183 }
1184
1185 if (is_r99 >= 0
1186 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1187 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1188 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1189 return 0;
1190 }
1191
1192 if (!msc_a_is_accepted(msc_a)
1193 && !msg_is_initially_permitted(gh)) {
1194 LOG_MSC_A(msc_a, LOGL_ERROR,
1195 "Message not permitted for initial conn: %s\n",
1196 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1197 return -EACCES;
1198 }
1199
1200 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1201 LOG_MSC_A(msc_a, LOGL_ERROR,
1202 "Illegal situation: RAN type mismatch:"
1203 " attached via %s, received message via %s\n",
1204 osmo_rat_type_name(vsub->cs.attached_via_ran),
1205 osmo_rat_type_name(msc_a->c.ran->type));
1206 return -EACCES;
1207 }
1208
1209#if 0
1210 if (silent_call_reroute(conn, msg))
1211 return silent_call_rx(conn, msg);
1212#endif
1213
1214 switch (pdisc) {
1215 case GSM48_PDISC_CC:
1216 rc = gsm0408_rcv_cc(msc_a, msg);
1217 break;
1218 case GSM48_PDISC_MM:
1219 rc = gsm0408_rcv_mm(msc_a, msg);
1220 break;
1221 case GSM48_PDISC_RR:
1222 rc = gsm0408_rcv_rr(msc_a, msg);
1223 break;
1224 case GSM48_PDISC_SMS:
1225 rc = gsm0411_rcv_sms(msc_a, msg);
1226 break;
1227 case GSM48_PDISC_MM_GPRS:
1228 case GSM48_PDISC_SM_GPRS:
1229 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1230 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1231 rc = -ENOTSUP;
1232 break;
1233 case GSM48_PDISC_NC_SS:
1234 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1235 break;
1236 case GSM48_PDISC_TEST:
1237 rc = gsm0414_rcv_test(msc_a, msg);
1238 break;
1239 default:
1240 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1241 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1242 rc = -EINVAL;
1243 break;
1244 }
1245
1246 return rc;
1247}
1248
1249static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1250{
1251 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
1252 struct rtp_stream *rtps_to_ran = msc_a->cc.call_leg ? msc_a->cc.call_leg->rtp[RTP_TO_RAN] : NULL;
1253
1254 if (!rtps_to_ran) {
1255 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1256 return;
1257 }
1258 if (!cc_trans) {
1259 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but CC transaction is active\n");
1260 return;
1261 }
1262
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001263 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1264 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1265 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1266 call_leg_release(msc_a->cc.call_leg);
1267 return;
1268 }
1269
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001270 /* Update RAN-side endpoint CI: */
1271 rtp_stream_set_codec(rtps_to_ran, ac->assignment_complete.codec);
1272 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001273 if (rtps_to_ran->use_osmux)
1274 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1275 ac->assignment_complete.osmux_cid);
1276
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001277 rtp_stream_commit(rtps_to_ran);
1278
1279 /* Setup CN side endpoint CI:
1280 * Now that
1281 * - the first CI has been created and a definitive endpoint name is assigned to the call_leg's MGW
1282 * endpoint,
1283 * - the Assignment has chosen a speech codec
1284 * go on to create the CN side RTP stream's CI. */
1285 if (call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_CN, cc_trans->callref, cc_trans,
1286 &ac->assignment_complete.codec, NULL)) {
1287 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Error creating MGW CI towards CN\n");
1288 call_leg_release(msc_a->cc.call_leg);
1289 return;
1290 }
1291}
1292
1293static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1294{
1295 struct gsm_trans *trans;
1296
1297 /* For a normal voice call, there will be an rtp_stream FSM. */
1298 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1299 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1300 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1301 return;
1302 }
1303
1304 /* Otherwise, a silent call might be active */
1305 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1306 if (trans) {
1307 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1308 trans_free(trans);
1309 return;
1310 }
1311
1312 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1313 msc_a_release_cn(msc_a);
1314}
1315
1316static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1317 struct osmo_gsm48_classmark *dst)
1318{
1319 if (!dst) {
1320 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1321
1322 if (!vsub)
1323 dst = &msc_a->temporary_classmark;
1324 else
1325 dst = &vsub->classmark;
1326 }
1327
1328 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities recived from Classmark Update: %s\n",
1329 osmo_gsm48_classmark_a5_name(classmark));
1330 osmo_gsm48_classmark_update(dst, classmark);
1331
1332 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1333 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1334 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1335}
1336
1337static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1338{
1339 int sapi = msg->sapi_n_reject.dlci & 0x7;
1340 if (sapi == UM_SAPI_SMS)
1341 gsm411_sapi_n_reject(msc_a);
1342}
1343
1344static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1345{
1346 if (!msc_a->ho.fi) {
1347 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1348 return -EINVAL;
1349 }
1350 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1351}
1352
1353int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1354{
1355 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1356 const struct ran_msg *msg = d->ran_dec;
1357 int rc = -99;
1358
1359 switch (msg->msg_type) {
1360
1361 case RAN_MSG_COMPL_L3:
1362 msc_a->via_cell = (struct osmo_cell_global_id){
1363 .lai.plmn = msc_a_net(msc_a)->plmn,
1364 };
1365 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
1366 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1367 if (!rc) {
1368 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1369 if (conn)
1370 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1371 }
1372 break;
1373
1374 case RAN_MSG_DTAP:
1375 rc = msc_a_up_l3(msc_a, msg->dtap);
1376 break;
1377
1378 case RAN_MSG_CLEAR_REQUEST:
1379 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1380 break;
1381
1382 case RAN_MSG_CLEAR_COMPLETE:
1383 switch (msc_a->c.fi->state) {
1384 case MSC_A_ST_RELEASING:
1385 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1386 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1387 break;
1388 case MSC_A_ST_RELEASED:
1389 break;
1390 default:
1391 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1392 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1393 break;
1394 }
1395 rc = 0;
1396 break;
1397
1398 case RAN_MSG_CLASSMARK_UPDATE:
1399 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1400 rc = 0;
1401 break;
1402
1403 case RAN_MSG_CIPHER_MODE_COMPLETE:
1404 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1405 if (msg->cipher_mode_complete.alg_id) {
1406 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1407 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1408 msc_a->geran_encr.alg_id - 1);
1409 };
1410 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1411 rc = 0;
1412 break;
1413
1414 case RAN_MSG_CIPHER_MODE_REJECT:
1415 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1416 rc = 0;
1417 break;
1418
1419 case RAN_MSG_ASSIGNMENT_COMPLETE:
1420 msc_a_up_call_assignment_complete(msc_a, msg);
1421 rc = 0;
1422 break;
1423
1424 case RAN_MSG_ASSIGNMENT_FAILURE:
1425 msc_a_up_call_assignment_failure(msc_a, msg);
1426 rc = 0;
1427 break;
1428
1429 case RAN_MSG_SAPI_N_REJECT:
1430 msc_a_up_sapi_n_reject(msc_a, msg);
1431 rc = 0;
1432 break;
1433
1434 case RAN_MSG_HANDOVER_PERFORMED:
1435 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1436 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1437 break;
1438
1439 case RAN_MSG_HANDOVER_REQUIRED:
1440 /* The BSS lets us know that it wants to handover to a different cell */
1441 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1442 break;
1443
1444 case RAN_MSG_HANDOVER_FAILURE:
1445 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1446 break;
1447
1448 default:
1449 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1450 rc = -ENOTSUP;
1451 break;
1452 }
1453 return rc;
1454}
1455
1456static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1457{
1458 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1459 int rc = -99;
1460
1461 if (!msc_t) {
1462 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1463 return -EINVAL;
1464 }
1465
1466 OSMO_ASSERT(d->ran_dec);
1467
1468 switch (d->ran_dec->msg_type) {
1469
1470 case RAN_MSG_CLEAR_REQUEST:
1471 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1472 break;
1473
1474 case RAN_MSG_CLEAR_COMPLETE:
1475 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1476 break;
1477
1478 case RAN_MSG_CLASSMARK_UPDATE:
1479 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1480 rc = 0;
1481 break;
1482
1483 case RAN_MSG_HANDOVER_REQUEST_ACK:
1484 /* new BSS accepts Handover */
1485 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1486 break;
1487
1488 case RAN_MSG_HANDOVER_DETECT:
1489 /* new BSS signals the MS is DETECTed on the new lchan */
1490 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1491 break;
1492
1493 case RAN_MSG_HANDOVER_COMPLETE:
1494 /* new BSS signals the MS has fully moved to the new lchan */
1495 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1496 break;
1497
1498 case RAN_MSG_HANDOVER_FAILURE:
1499 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1500 break;
1501
1502 default:
1503 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1504 ran_msg_type_name(d->ran_dec->msg_type));
1505 rc = -ENOTSUP;
1506 break;
1507 }
1508 return rc;
1509}
1510
1511int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1512{
1513 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1514 struct msc_a_ran_dec_data *d = data;
1515 int rc = -99;
1516
1517 d->ran_dec = msg;
1518
1519 switch (d->from_role) {
1520 case MSC_ROLE_I:
1521 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1522 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1523 break;
1524
1525 case MSC_ROLE_T:
1526 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1527 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1528 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1529 break;
1530
1531 default:
1532 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1533 ran_msg_type_name(msg->msg_type));
1534 return -ENOTSUP;
1535 }
1536
1537 if (rc)
1538 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),
1539 msc_role_name(d->from_role));
1540 return rc;
1541}
1542
1543/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1544int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1545 const char *file, int line)
1546{
1547 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1548 ran_msg, file, line);
1549}
1550
1551/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1552 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1553int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1554 const struct ran_msg *ran_msg,
1555 const char *file, int line)
1556{
1557 struct an_apdu an_apdu = {
1558 .an_proto = msc_a->c.ran->an_proto,
1559 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1560 };
1561 int rc;
1562 if (!an_apdu.msg)
1563 return -EIO;
1564 rc = _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
1565 msgb_free(an_apdu.msg);
1566 return rc;
1567}
1568
1569int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
1570{
1571 struct ran_msg ran_msg;
1572
1573 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
1574 /* The SGs connection to the MME always is at the MSC-A. */
1575 return sgs_iface_tx_dtap_ud(msc_a, dtap);
1576 }
1577
1578 ran_msg = (struct ran_msg){
1579 .msg_type = RAN_MSG_DTAP,
1580 .dtap = dtap,
1581 };
1582 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
1583}
1584
1585struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
1586{
1587 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
1588 if (valid_conn_only && !msc_a_is_accepted(msc_a))
1589 return NULL;
1590 return msc_a;
1591}
1592
1593int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
1594{
1595 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1596 struct ran_msg msg = {
1597 .msg_type = RAN_MSG_COMMON_ID,
1598 .common_id = {
1599 .imsi = vsub->imsi,
1600 },
1601 };
1602
1603 return msc_a_ran_down(msc_a, to_role, &msg);
1604}
1605
1606static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
1607{
1608 struct call_leg *cl = msc_a->cc.call_leg;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001609 struct msc_i *msc_i = msc_a_msc_i(msc_a);
1610 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001611
1612 OSMO_ASSERT(!msc_a->cc.active_trans);
1613 msc_a->cc.active_trans = cc_trans;
1614
1615 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
1616
1617 if (!cl) {
1618 cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi,
1619 MSC_EV_CALL_LEG_TERM,
1620 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
Neels Hofmeyr265a4c72019-05-09 16:20:51 +02001621 MSC_EV_CALL_LEG_RTP_COMPLETE);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001622 OSMO_ASSERT(cl);
1623
1624 /* HACK: We put the connection in loopback mode at the beginnig to
1625 * trick the hNodeB into doing the IuUP negotiation with itself.
1626 * This is a hack we need because osmo-mgw does not support IuUP yet, see OS#2459. */
1627 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU)
1628 cl->crcx_conn_mode[RTP_TO_RAN] = MGCP_CONN_LOOPBACK;
1629 }
1630
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001631 if (net->use_osmux != OSMUX_USAGE_OFF) {
1632 msc_i = msc_a_msc_i(msc_a);
1633 if (msc_i->c.remote_to) {
1634 /* TODO: investigate what to do in this case */
1635 LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC");
1636 } else {
1637 cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux;
1638 }
1639 }
1640
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001641 /* This will lead to either MSC_EV_CALL_LEG_LOCAL_ADDR_AVAILABLE or MSC_EV_CALL_LEG_TERM.
1642 * If the local address is already known, then immediately trigger. */
1643 if (call_leg_local_ip(cl, RTP_TO_RAN))
1644 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
1645 else
1646 return call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_RAN, cc_trans->callref, cc_trans, NULL, NULL);
1647}
1648
1649int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
1650{
1651 struct msc_a *msc_a = cc_trans->msc_a;
1652 OSMO_ASSERT(cc_trans->type == TRANS_CC);
1653
1654 if (msc_a->cc.active_trans == cc_trans) {
1655 /* Assignment for this trans already started earlier. */
1656 return 0;
1657 }
1658
1659 if (msc_a->cc.active_trans) {
1660 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
1661 return 0;
1662 }
1663
1664 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
1665 return msc_a_start_assignment(msc_a, cc_trans);
1666}
1667
1668const char *msc_a_cm_service_type_to_use(enum osmo_cm_service_type cm_service_type)
1669{
1670 switch (cm_service_type) {
1671 case GSM48_CMSERV_MO_CALL_PACKET:
1672 case GSM48_CMSERV_EMERGENCY:
1673 return MSC_A_USE_CM_SERVICE_CC;
1674
1675 case GSM48_CMSERV_SMS:
1676 return MSC_A_USE_CM_SERVICE_SMS;
1677
1678 case GSM48_CMSERV_SUP_SERV:
1679 return MSC_A_USE_CM_SERVICE_SS;
1680
1681 default:
1682 return NULL;
1683 }
1684}
1685
1686void msc_a_release_cn(struct msc_a *msc_a)
1687{
1688 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
1689}
1690
1691void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
1692{
1693 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1694}