blob: f9e1985861db616e2146744af5e5cd8d77060a0d [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* MSC subscriber connection implementation */
2
3/*
Neels Hofmeyrdbaab502018-11-30 00:45:07 +01004 * (C) 2016-2018 by sysmocom s.m.f.c. <info@sysmocom.de>
Harald Welteb8b85a12016-06-17 00:06:42 +02005 * All Rights Reserved
6 *
7 * Author: Neels Hofmeyr
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <osmocom/core/logging.h>
25#include <osmocom/core/fsm.h>
Harald Welte2483f1b2016-06-19 18:06:02 +020026#include <osmocom/core/signal.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020027
Neels Hofmeyr90843962017-09-04 15:04:35 +020028#include <osmocom/msc/osmo_msc.h>
29#include <osmocom/msc/vlr.h>
30#include <osmocom/msc/debug.h>
31#include <osmocom/msc/transaction.h>
32#include <osmocom/msc/signal.h>
33#include <osmocom/msc/a_iface.h>
Daniel Willmann4e825b62018-02-15 10:33:26 +010034#include <osmocom/msc/iucs.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020035
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020036#include "../../bscconfig.h"
37#ifdef BUILD_IU
38#include <osmocom/ranap/iu_client.h>
39#else
40#include <osmocom/msc/iu_dummy.h>
41#endif
42
Harald Welte2483f1b2016-06-19 18:06:02 +020043#define SUBSCR_CONN_TIMEOUT 5 /* seconds */
Harald Welteb8b85a12016-06-17 00:06:42 +020044
45static const struct value_string subscr_conn_fsm_event_names[] = {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020046 OSMO_VALUE_STRING(SUBSCR_CONN_E_COMPLETE_LAYER_3),
Neels Hofmeyr3117b702018-09-13 03:23:07 +020047 OSMO_VALUE_STRING(SUBSCR_CONN_E_CLASSMARK_UPDATE),
Harald Welteb8b85a12016-06-17 00:06:42 +020048 OSMO_VALUE_STRING(SUBSCR_CONN_E_ACCEPTED),
Harald Welte2483f1b2016-06-19 18:06:02 +020049 OSMO_VALUE_STRING(SUBSCR_CONN_E_COMMUNICATING),
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +010050 OSMO_VALUE_STRING(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED),
Harald Welteb8b85a12016-06-17 00:06:42 +020051 OSMO_VALUE_STRING(SUBSCR_CONN_E_MO_CLOSE),
52 OSMO_VALUE_STRING(SUBSCR_CONN_E_CN_CLOSE),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020053 OSMO_VALUE_STRING(SUBSCR_CONN_E_UNUSED),
Harald Welteb8b85a12016-06-17 00:06:42 +020054 { 0, NULL }
55};
56
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020057static void update_counters(struct osmo_fsm_inst *fi, bool conn_accepted)
Harald Welteb8b85a12016-06-17 00:06:42 +020058{
59 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020060 switch (conn->complete_layer3_type) {
61 case COMPLETE_LAYER3_LU:
62 rate_ctr_inc(&conn->network->msc_ctrs->ctr[
63 conn_accepted ? MSC_CTR_LOC_UPDATE_COMPLETED
64 : MSC_CTR_LOC_UPDATE_FAILED]);
Harald Welteb8b85a12016-06-17 00:06:42 +020065 break;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020066 case COMPLETE_LAYER3_CM_SERVICE_REQ:
67 rate_ctr_inc(&conn->network->msc_ctrs->ctr[
68 conn_accepted ? MSC_CTR_CM_SERVICE_REQUEST_ACCEPTED
69 : MSC_CTR_CM_SERVICE_REQUEST_REJECTED]);
70 break;
71 case COMPLETE_LAYER3_PAGING_RESP:
72 rate_ctr_inc(&conn->network->msc_ctrs->ctr[
73 conn_accepted ? MSC_CTR_PAGING_RESP_ACCEPTED
74 : MSC_CTR_PAGING_RESP_REJECTED]);
75 break;
76 default:
77 break;
78 }
79}
80
81static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted)
82{
83 struct gsm_subscriber_connection *conn = fi->priv;
84
85 update_counters(fi, conn_accepted);
86
87 /* Trigger transactions that we paged for */
88 if (conn->complete_layer3_type == COMPLETE_LAYER3_PAGING_RESP) {
89 subscr_paging_dispatch(GSM_HOOK_RR_PAGING,
90 conn_accepted ? GSM_PAGING_SUCCEEDED : GSM_PAGING_EXPIRED,
91 NULL, conn, conn->vsub);
92 }
93
94 if (conn->complete_layer3_type == COMPLETE_LAYER3_CM_SERVICE_REQ
95 && conn_accepted) {
96 conn->received_cm_service_request = true;
97 msc_subscr_conn_get(conn, MSC_CONN_USE_CM_SERVICE);
98 }
99
100 if (conn_accepted)
101 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, conn->vsub);
102}
103
104static void log_close_event(struct osmo_fsm_inst *fi, uint32_t event, void *data)
105{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200106 enum gsm48_reject_value *cause = data;
107 /* The close event itself is logged by the FSM. We can only add the cause value, if present. */
108 if (!cause || !*cause)
109 return;
110 LOGPFSML(fi, LOGL_NOTICE, "Close event, cause: %s\n", gsm48_reject_value_name(*cause));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200111}
112
113static void subscr_conn_fsm_new(struct osmo_fsm_inst *fi, uint32_t event, void *data)
114{
115 switch (event) {
116 case SUBSCR_CONN_E_COMPLETE_LAYER_3:
117 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_AUTH_CIPH, SUBSCR_CONN_TIMEOUT, 0);
118 return;
119
120 case SUBSCR_CONN_E_ACCEPTED:
121 evaluate_acceptance_outcome(fi, true);
122 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED, SUBSCR_CONN_TIMEOUT, 0);
123 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200124
125 case SUBSCR_CONN_E_MO_CLOSE:
126 case SUBSCR_CONN_E_CN_CLOSE:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200127 log_close_event(fi, event, data);
128 evaluate_acceptance_outcome(fi, false);
129 /* fall through */
130 case SUBSCR_CONN_E_UNUSED:
131 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
132 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200133
134 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200135 OSMO_ASSERT(false);
Harald Welteb8b85a12016-06-17 00:06:42 +0200136 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200137}
138
Neels Hofmeyr7486a942018-11-30 00:11:36 +0100139static void subscr_conn_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200140{
141 /* If accepted, transition the state, all other cases mean failure. */
142 switch (event) {
143 case SUBSCR_CONN_E_ACCEPTED:
144 evaluate_acceptance_outcome(fi, true);
145 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED, SUBSCR_CONN_TIMEOUT, 0);
146 return;
147
148 case SUBSCR_CONN_E_UNUSED:
149 LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n",
150 osmo_fsm_event_name(fi->fsm, event));
151 return;
152
153 case SUBSCR_CONN_E_MO_CLOSE:
154 case SUBSCR_CONN_E_CN_CLOSE:
155 log_close_event(fi, event, data);
156 evaluate_acceptance_outcome(fi, false);
157 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
158 return;
159
160
161 default:
162 OSMO_ASSERT(false);
163 }
164}
165
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200166int msc_classmark_request_then_cipher_mode_cmd(struct gsm_subscriber_connection *conn, bool umts_aka,
167 bool retrieve_imeisv)
168{
169 int rc;
170 conn->geran_set_cipher_mode.umts_aka = umts_aka;
171 conn->geran_set_cipher_mode.retrieve_imeisv = retrieve_imeisv;
172
173 rc = a_iface_tx_classmark_request(conn);
174 if (rc) {
175 LOGP(DMM, LOGL_ERROR, "%s: cannot send BSSMAP Classmark Request\n",
176 vlr_subscr_name(conn->vsub));
177 return -EIO;
178 }
179
180 osmo_fsm_inst_state_chg(conn->fi, SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE, SUBSCR_CONN_TIMEOUT, 0);
181 return 0;
182}
183
184static void subscr_conn_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data)
185{
186 struct gsm_subscriber_connection *conn = fi->priv;
187 switch (event) {
188 case SUBSCR_CONN_E_CLASSMARK_UPDATE:
189 /* Theoretically, this event can be used for requesting Classmark in various situations.
190 * So far though, the only time we send a Classmark Request is during Ciphering. As soon
191 * as more such situations arise, we need to add state to indicate what action should
192 * follow after a Classmark Update is received (e.g.
193 * msc_classmark_request_then_cipher_mode_cmd() sets an enum value to indicate that
194 * Ciphering should continue afterwards). But right now, it is accurate to always
195 * continue with Ciphering: */
196
197 /* During Ciphering, we needed Classmark information. The Classmark Update has come in,
198 * go back into the Set Ciphering Command procedure. */
199 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_AUTH_CIPH, SUBSCR_CONN_TIMEOUT, 0);
200 if (msc_geran_set_cipher_mode(conn, conn->geran_set_cipher_mode.umts_aka,
201 conn->geran_set_cipher_mode.retrieve_imeisv)) {
202 LOGPFSML(fi, LOGL_ERROR,
203 "Sending Cipher Mode Command failed, aborting attach\n");
204 vlr_subscr_cancel_attach_fsm(conn->vsub, OSMO_FSM_TERM_ERROR,
205 GSM48_REJECT_NETWORK_FAILURE);
206 }
207 return;
208
209 case SUBSCR_CONN_E_UNUSED:
210 LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n",
211 osmo_fsm_event_name(fi->fsm, event));
212 return;
213
214 case SUBSCR_CONN_E_MO_CLOSE:
215 case SUBSCR_CONN_E_CN_CLOSE:
216 log_close_event(fi, event, data);
217 evaluate_acceptance_outcome(fi, false);
218 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
219 return;
220
221 default:
222 OSMO_ASSERT(false);
223 }
224}
225
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200226static bool subscr_conn_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200227{
228 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200229 struct gsm_trans *trans;
Harald Welteb8b85a12016-06-17 00:06:42 +0200230
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200231 if (conn->silent_call) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100232 LOGPFSML(fi, LOGL_DEBUG, "%s: silent call still active\n", __func__);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200233 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200234 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200235
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200236 if (conn->received_cm_service_request) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100237 LOGPFSML(fi, LOGL_DEBUG, "%s: still awaiting first request after a CM Service Request\n",
238 __func__);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200239 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200240 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200241
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200242 if (conn->vsub && !llist_empty(&conn->vsub->cs.requests)) {
243 struct subscr_request *sr;
244 if (!log_check_level(fi->fsm->log_subsys, LOGL_DEBUG)) {
245 llist_for_each_entry(sr, &conn->vsub->cs.requests, entry) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100246 LOGPFSML(fi, LOGL_DEBUG, "%s: still active: %s\n",
247 __func__, sr->label);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200248 }
249 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200250 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200251 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200252
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200253 if ((trans = trans_has_conn(conn))) {
254 LOGPFSML(fi, LOGL_DEBUG,
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100255 "%s: connection still has active transaction: %s\n",
256 __func__, gsm48_pdisc_name(trans->protocol));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200257 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200258 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200259
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200260 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200261}
262
Harald Welte2483f1b2016-06-19 18:06:02 +0200263static void subscr_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
264{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200265 struct gsm_subscriber_connection *conn = fi->priv;
266
267 /* Stop Location Update expiry for this subscriber. While the subscriber
268 * has an open connection the LU expiry timer must remain disabled.
269 * Otherwise we would kick the subscriber off the network when the timer
270 * expires e.g. during a long phone call.
271 * The LU expiry timer will restart once the connection is closed. */
272 conn->vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
273
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200274 if (!subscr_conn_fsm_has_active_transactions(fi))
275 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_UNUSED, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200276}
277
Harald Welteb8b85a12016-06-17 00:06:42 +0200278static void subscr_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data)
279{
280 switch (event) {
Neels Hofmeyr36115c92018-08-23 15:52:43 +0200281 case SUBSCR_CONN_E_COMPLETE_LAYER_3:
282 /* When Authentication is off, we may already be in the Accepted state when the code
283 * evaluates the Compl L3. Simply ignore. This just cosmetically mutes the error log
284 * about the useless event. */
285 return;
286
Harald Welte2483f1b2016-06-19 18:06:02 +0200287 case SUBSCR_CONN_E_COMMUNICATING:
288 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_COMMUNICATING, 0, 0);
289 return;
290
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200291 case SUBSCR_CONN_E_MO_CLOSE:
292 case SUBSCR_CONN_E_CN_CLOSE:
293 log_close_event(fi, event, data);
294 /* fall through */
295 case SUBSCR_CONN_E_UNUSED:
296 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200297 return;
298
299 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200300 OSMO_ASSERT(false);
Harald Welteb8b85a12016-06-17 00:06:42 +0200301 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200302}
303
Harald Welte2483f1b2016-06-19 18:06:02 +0200304static void subscr_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
305{
306 switch (event) {
307 case SUBSCR_CONN_E_COMMUNICATING:
308 /* no-op */
309 return;
310
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200311 case SUBSCR_CONN_E_MO_CLOSE:
312 case SUBSCR_CONN_E_CN_CLOSE:
313 log_close_event(fi, event, data);
314 /* fall through */
315 case SUBSCR_CONN_E_UNUSED:
316 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
Harald Welte2483f1b2016-06-19 18:06:02 +0200317 return;
318
319 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200320 OSMO_ASSERT(false);
Harald Welte2483f1b2016-06-19 18:06:02 +0200321 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200322}
323
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200324static int subscr_conn_fsm_timeout(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200325{
326 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200327 if (msc_subscr_conn_in_release(conn)) {
328 LOGPFSML(fi, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
329 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_TIMEOUT, NULL);
330 } else {
Neels Hofmeyr15809592018-04-06 02:57:51 +0200331 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200332 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_CN_CLOSE, &cause);
333 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200334 return 0;
335}
336
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200337static void subscr_conn_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
Harald Welte2483f1b2016-06-19 18:06:02 +0200338{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200339 struct gsm_subscriber_connection *conn = fi->priv;
340
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200341 /* Use count for either conn->a.waiting_for_clear_complete or
342 * conn->iu.waiting_for_release_complete. 'get' it early, so we don't deallocate after tearing
343 * down active transactions. Safeguard against double-get (though it shouldn't happen). */
344 if (!msc_subscr_conn_used_by(conn, MSC_CONN_USE_RELEASE))
345 msc_subscr_conn_get(conn, MSC_CONN_USE_RELEASE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200346
347 /* Cancel pending CM Service Requests */
348 if (conn->received_cm_service_request) {
349 conn->received_cm_service_request = false;
350 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
351 }
352
353 /* Cancel all VLR FSMs, if any */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200354 vlr_subscr_cancel_attach_fsm(conn->vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200355
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200356 if (conn->vsub) {
357 /* The subscriber has no active connection anymore.
358 * Restart the periodic Location Update expiry timer for this subscriber. */
359 vlr_subscr_enable_expire_lu(conn->vsub);
360 }
361
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200362 /* If we're closing in a middle of a trans, we need to clean up */
363 trans_conn_closed(conn);
364
365 switch (conn->via_ran) {
366 case RAN_GERAN_A:
367 a_iface_tx_clear_cmd(conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200368 if (conn->a.waiting_for_clear_complete) {
369 LOGPFSML(fi, LOGL_ERROR,
370 "Unexpected: conn is already waiting for BSSMAP Clear Complete\n");
371 break;
372 }
373 conn->a.waiting_for_clear_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200374 break;
375 case RAN_UTRAN_IU:
376 ranap_iu_tx_release(conn->iu.ue_ctx, NULL);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200377 if (conn->iu.waiting_for_release_complete) {
378 LOGPFSML(fi, LOGL_ERROR,
379 "Unexpected: conn is already waiting for Iu Release Complete\n");
380 break;
381 }
382 conn->iu.waiting_for_release_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200383 break;
384 default:
385 LOGP(DMM, LOGL_ERROR, "%s: Unknown RAN type, cannot tx release/clear\n",
386 vlr_subscr_name(conn->vsub));
387 break;
388 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200389}
390
391static void subscr_conn_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
392{
393 OSMO_ASSERT(event == SUBSCR_CONN_E_UNUSED);
394 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
395}
396
397static void subscr_conn_fsm_released(struct osmo_fsm_inst *fi, uint32_t prev_state)
398{
399 /* Terminate, deallocate and also deallocate the gsm_subscriber_connection, which is allocated as
400 * a talloc child of fi. Also calls the cleanup function. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200401 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200402}
403
404#define S(x) (1 << (x))
405
406static const struct osmo_fsm_state subscr_conn_fsm_states[] = {
407 [SUBSCR_CONN_S_NEW] = {
408 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_NEW),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200409 .in_event_mask = S(SUBSCR_CONN_E_COMPLETE_LAYER_3) |
410 S(SUBSCR_CONN_E_ACCEPTED) |
411 S(SUBSCR_CONN_E_MO_CLOSE) |
412 S(SUBSCR_CONN_E_CN_CLOSE) |
413 S(SUBSCR_CONN_E_UNUSED),
414 .out_state_mask = S(SUBSCR_CONN_S_AUTH_CIPH) |
415 S(SUBSCR_CONN_S_ACCEPTED) |
416 S(SUBSCR_CONN_S_RELEASING),
417 .action = subscr_conn_fsm_new,
418 },
419 [SUBSCR_CONN_S_AUTH_CIPH] = {
420 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_AUTH_CIPH),
Harald Welteb8b85a12016-06-17 00:06:42 +0200421 .in_event_mask = S(SUBSCR_CONN_E_ACCEPTED) |
422 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200423 S(SUBSCR_CONN_E_CN_CLOSE) |
424 S(SUBSCR_CONN_E_UNUSED),
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200425 .out_state_mask = S(SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE) |
426 S(SUBSCR_CONN_S_ACCEPTED) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200427 S(SUBSCR_CONN_S_RELEASING),
428 .action = subscr_conn_fsm_auth_ciph,
Harald Welteb8b85a12016-06-17 00:06:42 +0200429 },
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200430 [SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE] = {
431 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE),
432 .in_event_mask = S(SUBSCR_CONN_E_CLASSMARK_UPDATE) |
433 S(SUBSCR_CONN_E_MO_CLOSE) |
434 S(SUBSCR_CONN_E_CN_CLOSE) |
435 S(SUBSCR_CONN_E_UNUSED),
436 .out_state_mask = S(SUBSCR_CONN_S_AUTH_CIPH) |
437 S(SUBSCR_CONN_S_RELEASING),
438 .action = subscr_conn_fsm_wait_classmark_update,
439 },
Harald Welteb8b85a12016-06-17 00:06:42 +0200440 [SUBSCR_CONN_S_ACCEPTED] = {
441 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_ACCEPTED),
442 /* allow everything to release for any odd behavior */
Neels Hofmeyr36115c92018-08-23 15:52:43 +0200443 .in_event_mask = S(SUBSCR_CONN_E_COMPLETE_LAYER_3) |
444 S(SUBSCR_CONN_E_COMMUNICATING) |
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100445 S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200446 S(SUBSCR_CONN_E_ACCEPTED) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200447 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200448 S(SUBSCR_CONN_E_CN_CLOSE) |
449 S(SUBSCR_CONN_E_UNUSED),
450 .out_state_mask = S(SUBSCR_CONN_S_RELEASING) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200451 S(SUBSCR_CONN_S_COMMUNICATING),
452 .onenter = subscr_conn_fsm_accepted_enter,
Harald Welteb8b85a12016-06-17 00:06:42 +0200453 .action = subscr_conn_fsm_accepted,
454 },
Harald Welte2483f1b2016-06-19 18:06:02 +0200455 [SUBSCR_CONN_S_COMMUNICATING] = {
456 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_COMMUNICATING),
457 /* allow everything to release for any odd behavior */
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100458 .in_event_mask = S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200459 S(SUBSCR_CONN_E_ACCEPTED) |
460 S(SUBSCR_CONN_E_COMMUNICATING) |
461 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200462 S(SUBSCR_CONN_E_CN_CLOSE) |
463 S(SUBSCR_CONN_E_UNUSED),
464 .out_state_mask = S(SUBSCR_CONN_S_RELEASING),
Harald Welte2483f1b2016-06-19 18:06:02 +0200465 .action = subscr_conn_fsm_communicating,
466 },
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200467 [SUBSCR_CONN_S_RELEASING] = {
468 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASING),
469 .in_event_mask = S(SUBSCR_CONN_E_UNUSED),
470 .out_state_mask = S(SUBSCR_CONN_S_RELEASED),
471 .onenter = subscr_conn_fsm_releasing_onenter,
472 .action = subscr_conn_fsm_releasing,
473 },
Harald Welteb8b85a12016-06-17 00:06:42 +0200474 [SUBSCR_CONN_S_RELEASED] = {
475 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASED),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200476 .onenter = subscr_conn_fsm_released,
Harald Welteb8b85a12016-06-17 00:06:42 +0200477 },
478};
479
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200480static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
481
Harald Welteb8b85a12016-06-17 00:06:42 +0200482static struct osmo_fsm subscr_conn_fsm = {
483 .name = "Subscr_Conn",
484 .states = subscr_conn_fsm_states,
485 .num_states = ARRAY_SIZE(subscr_conn_fsm_states),
486 .allstate_event_mask = 0,
487 .allstate_action = NULL,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200488 .log_subsys = DMM,
Harald Welteb8b85a12016-06-17 00:06:42 +0200489 .event_names = subscr_conn_fsm_event_names,
Harald Welte2483f1b2016-06-19 18:06:02 +0200490 .cleanup = subscr_conn_fsm_cleanup,
491 .timer_cb = subscr_conn_fsm_timeout,
Harald Welteb8b85a12016-06-17 00:06:42 +0200492};
493
Daniel Willmann4e825b62018-02-15 10:33:26 +0100494char *msc_subscr_conn_get_conn_id(struct gsm_subscriber_connection *conn)
495{
496 char *id;
497
498 switch (conn->via_ran) {
499 case RAN_GERAN_A:
500 id = talloc_asprintf(conn, "GERAN_A-%08x", conn->a.conn_id);
501 break;
502 case RAN_UTRAN_IU:
503 id = talloc_asprintf(conn, "UTRAN_IU-%08x", iu_get_conn_id(conn->iu.ue_ctx));
504 break;
505 default:
506 LOGP(DMM, LOGL_ERROR, "RAN of conn %p unknown!\n", conn);
507 return NULL;
508 }
509
510 return id;
511}
512
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200513/* Tidy up before the FSM deallocates */
514static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200515{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200516 struct gsm_subscriber_connection *conn = fi->priv;
Harald Welteb8b85a12016-06-17 00:06:42 +0200517
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200518 if (subscr_conn_fsm_has_active_transactions(fi))
519 LOGPFSML(fi, LOGL_ERROR, "Deallocating despite active transactions\n");
520
521 if (!conn) {
522 LOGP(DRLL, LOGL_ERROR, "Freeing NULL subscriber connection\n");
523 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200524 }
525
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200526 if (conn->vsub) {
527 DEBUGP(DRLL, "%s: Freeing subscriber connection\n", vlr_subscr_name(conn->vsub));
528 conn->vsub->lu_fsm = NULL;
529 conn->vsub->msc_conn_ref = NULL;
530 vlr_subscr_put(conn->vsub);
531 conn->vsub = NULL;
532 } else
533 DEBUGP(DRLL, "Freeing subscriber connection with NULL subscriber\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200534
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200535 llist_del(&conn->entry);
536}
537
538/* Signal success of Complete Layer 3. Allow to keep the conn open for Auth and Ciph. */
539void msc_subscr_conn_complete_layer_3(struct gsm_subscriber_connection *conn)
540{
541 if (!conn)
542 return;
543 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_COMPLETE_LAYER_3, NULL);
544}
545
546void subscr_conn_release_when_unused(struct gsm_subscriber_connection *conn)
547{
548 if (!conn)
549 return;
550 if (msc_subscr_conn_in_release(conn)) {
551 DEBUGP(DMM, "%s: %s: conn already in release (%s)\n",
552 vlr_subscr_name(conn->vsub), __func__,
553 osmo_fsm_inst_state_name(conn->fi));
554 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200555 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200556 if (conn->fi->state == SUBSCR_CONN_S_NEW) {
557 DEBUGP(DMM, "%s: %s: conn still being established (%s)\n",
558 vlr_subscr_name(conn->vsub), __func__,
559 osmo_fsm_inst_state_name(conn->fi));
560 return;
561 }
562 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_RELEASE_WHEN_UNUSED, NULL);
563}
564
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200565static void conn_close(struct gsm_subscriber_connection *conn, uint32_t cause, uint32_t event)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200566{
567 if (!conn) {
568 LOGP(DMM, LOGL_ERROR, "Cannot release NULL connection\n");
569 return;
570 }
571 if (msc_subscr_conn_in_release(conn)) {
572 DEBUGP(DMM, "%s(vsub=%s, cause=%u): already in release, ignore.\n",
573 __func__, vlr_subscr_name(conn->vsub), cause);
574 return;
575 }
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200576 osmo_fsm_inst_dispatch(conn->fi, event, &cause);
577}
578
579void msc_subscr_conn_close(struct gsm_subscriber_connection *conn, uint32_t cause)
580{
581 return conn_close(conn, cause, SUBSCR_CONN_E_CN_CLOSE);
582}
583
584void msc_subscr_conn_mo_close(struct gsm_subscriber_connection *conn, uint32_t cause)
585{
586 return conn_close(conn, cause, SUBSCR_CONN_E_MO_CLOSE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200587}
588
589bool msc_subscr_conn_in_release(struct gsm_subscriber_connection *conn)
590{
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200591 if (!conn || !conn->fi)
592 return true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200593 if (conn->fi->state == SUBSCR_CONN_S_RELEASING)
594 return true;
595 if (conn->fi->state == SUBSCR_CONN_S_RELEASED)
596 return true;
597 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200598}
599
Maxd83b17b2018-02-06 16:51:31 +0100600bool msc_subscr_conn_is_accepted(const struct gsm_subscriber_connection *conn)
Harald Welteb8b85a12016-06-17 00:06:42 +0200601{
602 if (!conn)
603 return false;
Harald Welte2483f1b2016-06-19 18:06:02 +0200604 if (!conn->vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200605 return false;
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200606 if (!(conn->fi->state == SUBSCR_CONN_S_ACCEPTED
607 || conn->fi->state == SUBSCR_CONN_S_COMMUNICATING))
Harald Welteb8b85a12016-06-17 00:06:42 +0200608 return false;
609 return true;
610}
611
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200612/* Indicate that *some* communication is happening with the phone, so that the conn FSM no longer times
613 * out to release within a few seconds. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200614void msc_subscr_conn_communicating(struct gsm_subscriber_connection *conn)
615{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200616 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_COMMUNICATING, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200617}
618
Harald Welteb8b85a12016-06-17 00:06:42 +0200619void msc_subscr_conn_init(void)
620{
621 osmo_fsm_register(&subscr_conn_fsm);
622}
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200623
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200624/* Allocate a new subscriber conn and FSM.
625 * Deallocation is by msc_subscr_conn_put(): when the use count reaches zero, the
626 * SUBSCR_CONN_E_RELEASE_COMPLETE event is dispatched, the FSM terminates and deallocates both FSM and
627 * conn. As long as the FSM is waiting for responses from the subscriber, it will itself hold a use count
628 * on the conn. */
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200629struct gsm_subscriber_connection *msc_subscr_conn_alloc(struct gsm_network *network,
630 enum ran_type via_ran, uint16_t lac)
631{
632 struct gsm_subscriber_connection *conn;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200633 struct osmo_fsm_inst *fi;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200634
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200635 fi = osmo_fsm_inst_alloc(&subscr_conn_fsm, network, NULL, LOGL_DEBUG, NULL);
636 if (!fi) {
637 LOGP(DMM, LOGL_ERROR, "Failed to allocate conn FSM\n");
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200638 return NULL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200639 }
640
641 conn = talloc_zero(fi, struct gsm_subscriber_connection);
642 if (!conn) {
643 osmo_fsm_inst_free(fi);
644 return NULL;
645 }
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200646
647 *conn = (struct gsm_subscriber_connection){
648 .network = network,
649 .via_ran = via_ran,
650 .lac = lac,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200651 .fi = fi,
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200652 };
653
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200654 fi->priv = conn;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200655 llist_add_tail(&conn->entry, &network->subscr_conns);
656 return conn;
657}
658
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200659bool msc_subscr_conn_is_establishing_auth_ciph(const struct gsm_subscriber_connection *conn)
660{
661 if (!conn)
662 return false;
663 return conn->fi->state == SUBSCR_CONN_S_AUTH_CIPH;
664}
665
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200666
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200667const struct value_string complete_layer3_type_names[] = {
668 { COMPLETE_LAYER3_NONE, "NONE" },
669 { COMPLETE_LAYER3_LU, "LU" },
670 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
671 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
672 { 0, NULL }
673};
674
675void msc_subscr_conn_update_id(struct gsm_subscriber_connection *conn,
676 enum complete_layer3_type from, const char *id)
677{
678 conn->complete_layer3_type = from;
Neels Hofmeyrfe4ba7c2018-04-02 23:17:50 +0200679 osmo_fsm_inst_update_id_f(conn->fi, "%s:%s", complete_layer3_type_name(from), id);
680 LOGPFSML(conn->fi, LOGL_DEBUG, "Updated ID\n");
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200681}
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200682
683static void rx_close_complete(struct gsm_subscriber_connection *conn, const char *label, bool *flag)
684{
685 if (!conn)
686 return;
687 if (!msc_subscr_conn_in_release(conn)) {
688 LOGPFSML(conn->fi, LOGL_ERROR, "Received unexpected %s, discarding right now\n",
689 label);
690 trans_conn_closed(conn);
691 osmo_fsm_inst_term(conn->fi, OSMO_FSM_TERM_ERROR, NULL);
692 return;
693 }
694 if (*flag) {
695 *flag = false;
696 msc_subscr_conn_put(conn, MSC_CONN_USE_RELEASE);
697 }
698}
699
700void msc_subscr_conn_rx_bssmap_clear_complete(struct gsm_subscriber_connection *conn)
701{
702 rx_close_complete(conn, "BSSMAP Clear Complete", &conn->a.waiting_for_clear_complete);
703}
704
705void msc_subscr_conn_rx_iu_release_complete(struct gsm_subscriber_connection *conn)
706{
707 rx_close_complete(conn, "Iu Release Complete", &conn->iu.waiting_for_release_complete);
708}