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