blob: e6fa7e11f0f14504367e21d3160a90f06ad374fe [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* MSC subscriber connection implementation */
2
3/*
4 * (C) 2016 by sysmocom s.m.f.c. <info@sysmocom.de>
5 * 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[] = {
46 OSMO_VALUE_STRING(SUBSCR_CONN_E_INVALID),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020047 OSMO_VALUE_STRING(SUBSCR_CONN_E_COMPLETE_LAYER_3),
Neels Hofmeyr3117b702018-09-13 03:23:07 +020048 OSMO_VALUE_STRING(SUBSCR_CONN_E_CLASSMARK_UPDATE),
Harald Welteb8b85a12016-06-17 00:06:42 +020049 OSMO_VALUE_STRING(SUBSCR_CONN_E_ACCEPTED),
Harald Welte2483f1b2016-06-19 18:06:02 +020050 OSMO_VALUE_STRING(SUBSCR_CONN_E_COMMUNICATING),
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +010051 OSMO_VALUE_STRING(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED),
Harald Welteb8b85a12016-06-17 00:06:42 +020052 OSMO_VALUE_STRING(SUBSCR_CONN_E_MO_CLOSE),
53 OSMO_VALUE_STRING(SUBSCR_CONN_E_CN_CLOSE),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020054 OSMO_VALUE_STRING(SUBSCR_CONN_E_UNUSED),
Harald Welteb8b85a12016-06-17 00:06:42 +020055 { 0, NULL }
56};
57
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020058static void update_counters(struct osmo_fsm_inst *fi, bool conn_accepted)
Harald Welteb8b85a12016-06-17 00:06:42 +020059{
60 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020061 switch (conn->complete_layer3_type) {
62 case COMPLETE_LAYER3_LU:
63 rate_ctr_inc(&conn->network->msc_ctrs->ctr[
64 conn_accepted ? MSC_CTR_LOC_UPDATE_COMPLETED
65 : MSC_CTR_LOC_UPDATE_FAILED]);
Harald Welteb8b85a12016-06-17 00:06:42 +020066 break;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020067 case COMPLETE_LAYER3_CM_SERVICE_REQ:
68 rate_ctr_inc(&conn->network->msc_ctrs->ctr[
69 conn_accepted ? MSC_CTR_CM_SERVICE_REQUEST_ACCEPTED
70 : MSC_CTR_CM_SERVICE_REQUEST_REJECTED]);
71 break;
72 case COMPLETE_LAYER3_PAGING_RESP:
73 rate_ctr_inc(&conn->network->msc_ctrs->ctr[
74 conn_accepted ? MSC_CTR_PAGING_RESP_ACCEPTED
75 : MSC_CTR_PAGING_RESP_REJECTED]);
76 break;
77 default:
78 break;
79 }
80}
81
82static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted)
83{
84 struct gsm_subscriber_connection *conn = fi->priv;
85
86 update_counters(fi, conn_accepted);
87
88 /* Trigger transactions that we paged for */
89 if (conn->complete_layer3_type == COMPLETE_LAYER3_PAGING_RESP) {
90 subscr_paging_dispatch(GSM_HOOK_RR_PAGING,
91 conn_accepted ? GSM_PAGING_SUCCEEDED : GSM_PAGING_EXPIRED,
92 NULL, conn, conn->vsub);
93 }
94
95 if (conn->complete_layer3_type == COMPLETE_LAYER3_CM_SERVICE_REQ
96 && conn_accepted) {
97 conn->received_cm_service_request = true;
98 msc_subscr_conn_get(conn, MSC_CONN_USE_CM_SERVICE);
99 }
100
101 if (conn_accepted)
102 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, conn->vsub);
103}
104
105static void log_close_event(struct osmo_fsm_inst *fi, uint32_t event, void *data)
106{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200107 enum gsm48_reject_value *cause = data;
108 /* The close event itself is logged by the FSM. We can only add the cause value, if present. */
109 if (!cause || !*cause)
110 return;
111 LOGPFSML(fi, LOGL_NOTICE, "Close event, cause: %s\n", gsm48_reject_value_name(*cause));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200112}
113
114static void subscr_conn_fsm_new(struct osmo_fsm_inst *fi, uint32_t event, void *data)
115{
116 switch (event) {
117 case SUBSCR_CONN_E_COMPLETE_LAYER_3:
118 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_AUTH_CIPH, SUBSCR_CONN_TIMEOUT, 0);
119 return;
120
121 case SUBSCR_CONN_E_ACCEPTED:
122 evaluate_acceptance_outcome(fi, true);
123 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED, SUBSCR_CONN_TIMEOUT, 0);
124 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200125
126 case SUBSCR_CONN_E_MO_CLOSE:
127 case SUBSCR_CONN_E_CN_CLOSE:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200128 log_close_event(fi, event, data);
129 evaluate_acceptance_outcome(fi, false);
130 /* fall through */
131 case SUBSCR_CONN_E_UNUSED:
132 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
133 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200134
135 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200136 OSMO_ASSERT(false);
Harald Welteb8b85a12016-06-17 00:06:42 +0200137 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200138}
139
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200140void subscr_conn_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
141{
142 /* If accepted, transition the state, all other cases mean failure. */
143 switch (event) {
144 case SUBSCR_CONN_E_ACCEPTED:
145 evaluate_acceptance_outcome(fi, true);
146 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED, SUBSCR_CONN_TIMEOUT, 0);
147 return;
148
149 case SUBSCR_CONN_E_UNUSED:
150 LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n",
151 osmo_fsm_event_name(fi->fsm, event));
152 return;
153
154 case SUBSCR_CONN_E_MO_CLOSE:
155 case SUBSCR_CONN_E_CN_CLOSE:
156 log_close_event(fi, event, data);
157 evaluate_acceptance_outcome(fi, false);
158 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
159 return;
160
161
162 default:
163 OSMO_ASSERT(false);
164 }
165}
166
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200167int msc_classmark_request_then_cipher_mode_cmd(struct gsm_subscriber_connection *conn, bool umts_aka,
168 bool retrieve_imeisv)
169{
170 int rc;
171 conn->geran_set_cipher_mode.umts_aka = umts_aka;
172 conn->geran_set_cipher_mode.retrieve_imeisv = retrieve_imeisv;
173
174 rc = a_iface_tx_classmark_request(conn);
175 if (rc) {
176 LOGP(DMM, LOGL_ERROR, "%s: cannot send BSSMAP Classmark Request\n",
177 vlr_subscr_name(conn->vsub));
178 return -EIO;
179 }
180
181 osmo_fsm_inst_state_chg(conn->fi, SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE, SUBSCR_CONN_TIMEOUT, 0);
182 return 0;
183}
184
185static void subscr_conn_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data)
186{
187 struct gsm_subscriber_connection *conn = fi->priv;
188 switch (event) {
189 case SUBSCR_CONN_E_CLASSMARK_UPDATE:
190 /* Theoretically, this event can be used for requesting Classmark in various situations.
191 * So far though, the only time we send a Classmark Request is during Ciphering. As soon
192 * as more such situations arise, we need to add state to indicate what action should
193 * follow after a Classmark Update is received (e.g.
194 * msc_classmark_request_then_cipher_mode_cmd() sets an enum value to indicate that
195 * Ciphering should continue afterwards). But right now, it is accurate to always
196 * continue with Ciphering: */
197
198 /* During Ciphering, we needed Classmark information. The Classmark Update has come in,
199 * go back into the Set Ciphering Command procedure. */
200 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_AUTH_CIPH, SUBSCR_CONN_TIMEOUT, 0);
201 if (msc_geran_set_cipher_mode(conn, conn->geran_set_cipher_mode.umts_aka,
202 conn->geran_set_cipher_mode.retrieve_imeisv)) {
203 LOGPFSML(fi, LOGL_ERROR,
204 "Sending Cipher Mode Command failed, aborting attach\n");
205 vlr_subscr_cancel_attach_fsm(conn->vsub, OSMO_FSM_TERM_ERROR,
206 GSM48_REJECT_NETWORK_FAILURE);
207 }
208 return;
209
210 case SUBSCR_CONN_E_UNUSED:
211 LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n",
212 osmo_fsm_event_name(fi->fsm, event));
213 return;
214
215 case SUBSCR_CONN_E_MO_CLOSE:
216 case SUBSCR_CONN_E_CN_CLOSE:
217 log_close_event(fi, event, data);
218 evaluate_acceptance_outcome(fi, false);
219 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
220 return;
221
222 default:
223 OSMO_ASSERT(false);
224 }
225}
226
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200227static bool subscr_conn_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200228{
229 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200230 struct gsm_trans *trans;
Harald Welteb8b85a12016-06-17 00:06:42 +0200231
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200232 if (conn->silent_call) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100233 LOGPFSML(fi, LOGL_DEBUG, "%s: silent call still active\n", __func__);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200234 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200235 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200236
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200237 if (conn->received_cm_service_request) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100238 LOGPFSML(fi, LOGL_DEBUG, "%s: still awaiting first request after a CM Service Request\n",
239 __func__);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200240 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200241 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200242
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200243 if (conn->vsub && !llist_empty(&conn->vsub->cs.requests)) {
244 struct subscr_request *sr;
245 if (!log_check_level(fi->fsm->log_subsys, LOGL_DEBUG)) {
246 llist_for_each_entry(sr, &conn->vsub->cs.requests, entry) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100247 LOGPFSML(fi, LOGL_DEBUG, "%s: still active: %s\n",
248 __func__, sr->label);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200249 }
250 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200251 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200252 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200253
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200254 if ((trans = trans_has_conn(conn))) {
255 LOGPFSML(fi, LOGL_DEBUG,
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100256 "%s: connection still has active transaction: %s\n",
257 __func__, gsm48_pdisc_name(trans->protocol));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200258 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200259 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200260
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200261 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200262}
263
Harald Welte2483f1b2016-06-19 18:06:02 +0200264static void subscr_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
265{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200266 struct gsm_subscriber_connection *conn = fi->priv;
267
268 /* Stop Location Update expiry for this subscriber. While the subscriber
269 * has an open connection the LU expiry timer must remain disabled.
270 * Otherwise we would kick the subscriber off the network when the timer
271 * expires e.g. during a long phone call.
272 * The LU expiry timer will restart once the connection is closed. */
273 conn->vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
274
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200275 if (!subscr_conn_fsm_has_active_transactions(fi))
276 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_UNUSED, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200277}
278
Harald Welteb8b85a12016-06-17 00:06:42 +0200279static void subscr_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data)
280{
281 switch (event) {
Neels Hofmeyr36115c92018-08-23 15:52:43 +0200282 case SUBSCR_CONN_E_COMPLETE_LAYER_3:
283 /* When Authentication is off, we may already be in the Accepted state when the code
284 * evaluates the Compl L3. Simply ignore. This just cosmetically mutes the error log
285 * about the useless event. */
286 return;
287
Harald Welte2483f1b2016-06-19 18:06:02 +0200288 case SUBSCR_CONN_E_COMMUNICATING:
289 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_COMMUNICATING, 0, 0);
290 return;
291
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200292 case SUBSCR_CONN_E_MO_CLOSE:
293 case SUBSCR_CONN_E_CN_CLOSE:
294 log_close_event(fi, event, data);
295 /* fall through */
296 case SUBSCR_CONN_E_UNUSED:
297 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200298 return;
299
300 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200301 OSMO_ASSERT(false);
Harald Welteb8b85a12016-06-17 00:06:42 +0200302 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200303}
304
Harald Welte2483f1b2016-06-19 18:06:02 +0200305static void subscr_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
306{
307 switch (event) {
308 case SUBSCR_CONN_E_COMMUNICATING:
309 /* no-op */
310 return;
311
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200312 case SUBSCR_CONN_E_MO_CLOSE:
313 case SUBSCR_CONN_E_CN_CLOSE:
314 log_close_event(fi, event, data);
315 /* fall through */
316 case SUBSCR_CONN_E_UNUSED:
317 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
Harald Welte2483f1b2016-06-19 18:06:02 +0200318 return;
319
320 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200321 OSMO_ASSERT(false);
Harald Welte2483f1b2016-06-19 18:06:02 +0200322 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200323}
324
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200325static int subscr_conn_fsm_timeout(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200326{
327 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200328 if (msc_subscr_conn_in_release(conn)) {
329 LOGPFSML(fi, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
330 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_TIMEOUT, NULL);
331 } else {
Neels Hofmeyr15809592018-04-06 02:57:51 +0200332 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200333 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_CN_CLOSE, &cause);
334 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200335 return 0;
336}
337
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200338static void subscr_conn_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
Harald Welte2483f1b2016-06-19 18:06:02 +0200339{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200340 struct gsm_subscriber_connection *conn = fi->priv;
341
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200342 /* Use count for either conn->a.waiting_for_clear_complete or
343 * conn->iu.waiting_for_release_complete. 'get' it early, so we don't deallocate after tearing
344 * down active transactions. Safeguard against double-get (though it shouldn't happen). */
345 if (!msc_subscr_conn_used_by(conn, MSC_CONN_USE_RELEASE))
346 msc_subscr_conn_get(conn, MSC_CONN_USE_RELEASE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200347
348 /* Cancel pending CM Service Requests */
349 if (conn->received_cm_service_request) {
350 conn->received_cm_service_request = false;
351 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
352 }
353
354 /* Cancel all VLR FSMs, if any */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200355 vlr_subscr_cancel_attach_fsm(conn->vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200356
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200357 if (conn->vsub) {
358 /* The subscriber has no active connection anymore.
359 * Restart the periodic Location Update expiry timer for this subscriber. */
360 vlr_subscr_enable_expire_lu(conn->vsub);
361 }
362
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200363 /* If we're closing in a middle of a trans, we need to clean up */
364 trans_conn_closed(conn);
365
366 switch (conn->via_ran) {
367 case RAN_GERAN_A:
368 a_iface_tx_clear_cmd(conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200369 if (conn->a.waiting_for_clear_complete) {
370 LOGPFSML(fi, LOGL_ERROR,
371 "Unexpected: conn is already waiting for BSSMAP Clear Complete\n");
372 break;
373 }
374 conn->a.waiting_for_clear_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200375 break;
376 case RAN_UTRAN_IU:
377 ranap_iu_tx_release(conn->iu.ue_ctx, NULL);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200378 if (conn->iu.waiting_for_release_complete) {
379 LOGPFSML(fi, LOGL_ERROR,
380 "Unexpected: conn is already waiting for Iu Release Complete\n");
381 break;
382 }
383 conn->iu.waiting_for_release_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200384 break;
385 default:
386 LOGP(DMM, LOGL_ERROR, "%s: Unknown RAN type, cannot tx release/clear\n",
387 vlr_subscr_name(conn->vsub));
388 break;
389 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200390}
391
392static void subscr_conn_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
393{
394 OSMO_ASSERT(event == SUBSCR_CONN_E_UNUSED);
395 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
396}
397
398static void subscr_conn_fsm_released(struct osmo_fsm_inst *fi, uint32_t prev_state)
399{
400 /* Terminate, deallocate and also deallocate the gsm_subscriber_connection, which is allocated as
401 * a talloc child of fi. Also calls the cleanup function. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200402 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200403}
404
405#define S(x) (1 << (x))
406
407static const struct osmo_fsm_state subscr_conn_fsm_states[] = {
408 [SUBSCR_CONN_S_NEW] = {
409 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_NEW),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200410 .in_event_mask = S(SUBSCR_CONN_E_COMPLETE_LAYER_3) |
411 S(SUBSCR_CONN_E_ACCEPTED) |
412 S(SUBSCR_CONN_E_MO_CLOSE) |
413 S(SUBSCR_CONN_E_CN_CLOSE) |
414 S(SUBSCR_CONN_E_UNUSED),
415 .out_state_mask = S(SUBSCR_CONN_S_AUTH_CIPH) |
416 S(SUBSCR_CONN_S_ACCEPTED) |
417 S(SUBSCR_CONN_S_RELEASING),
418 .action = subscr_conn_fsm_new,
419 },
420 [SUBSCR_CONN_S_AUTH_CIPH] = {
421 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_AUTH_CIPH),
Harald Welteb8b85a12016-06-17 00:06:42 +0200422 .in_event_mask = S(SUBSCR_CONN_E_ACCEPTED) |
423 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200424 S(SUBSCR_CONN_E_CN_CLOSE) |
425 S(SUBSCR_CONN_E_UNUSED),
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200426 .out_state_mask = S(SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE) |
427 S(SUBSCR_CONN_S_ACCEPTED) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200428 S(SUBSCR_CONN_S_RELEASING),
429 .action = subscr_conn_fsm_auth_ciph,
Harald Welteb8b85a12016-06-17 00:06:42 +0200430 },
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200431 [SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE] = {
432 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE),
433 .in_event_mask = S(SUBSCR_CONN_E_CLASSMARK_UPDATE) |
434 S(SUBSCR_CONN_E_MO_CLOSE) |
435 S(SUBSCR_CONN_E_CN_CLOSE) |
436 S(SUBSCR_CONN_E_UNUSED),
437 .out_state_mask = S(SUBSCR_CONN_S_AUTH_CIPH) |
438 S(SUBSCR_CONN_S_RELEASING),
439 .action = subscr_conn_fsm_wait_classmark_update,
440 },
Harald Welteb8b85a12016-06-17 00:06:42 +0200441 [SUBSCR_CONN_S_ACCEPTED] = {
442 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_ACCEPTED),
443 /* allow everything to release for any odd behavior */
Neels Hofmeyr36115c92018-08-23 15:52:43 +0200444 .in_event_mask = S(SUBSCR_CONN_E_COMPLETE_LAYER_3) |
445 S(SUBSCR_CONN_E_COMMUNICATING) |
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100446 S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200447 S(SUBSCR_CONN_E_ACCEPTED) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200448 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200449 S(SUBSCR_CONN_E_CN_CLOSE) |
450 S(SUBSCR_CONN_E_UNUSED),
451 .out_state_mask = S(SUBSCR_CONN_S_RELEASING) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200452 S(SUBSCR_CONN_S_COMMUNICATING),
453 .onenter = subscr_conn_fsm_accepted_enter,
Harald Welteb8b85a12016-06-17 00:06:42 +0200454 .action = subscr_conn_fsm_accepted,
455 },
Harald Welte2483f1b2016-06-19 18:06:02 +0200456 [SUBSCR_CONN_S_COMMUNICATING] = {
457 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_COMMUNICATING),
458 /* allow everything to release for any odd behavior */
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100459 .in_event_mask = S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200460 S(SUBSCR_CONN_E_ACCEPTED) |
461 S(SUBSCR_CONN_E_COMMUNICATING) |
462 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200463 S(SUBSCR_CONN_E_CN_CLOSE) |
464 S(SUBSCR_CONN_E_UNUSED),
465 .out_state_mask = S(SUBSCR_CONN_S_RELEASING),
Harald Welte2483f1b2016-06-19 18:06:02 +0200466 .action = subscr_conn_fsm_communicating,
467 },
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200468 [SUBSCR_CONN_S_RELEASING] = {
469 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASING),
470 .in_event_mask = S(SUBSCR_CONN_E_UNUSED),
471 .out_state_mask = S(SUBSCR_CONN_S_RELEASED),
472 .onenter = subscr_conn_fsm_releasing_onenter,
473 .action = subscr_conn_fsm_releasing,
474 },
Harald Welteb8b85a12016-06-17 00:06:42 +0200475 [SUBSCR_CONN_S_RELEASED] = {
476 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASED),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200477 .onenter = subscr_conn_fsm_released,
Harald Welteb8b85a12016-06-17 00:06:42 +0200478 },
479};
480
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200481static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
482
Harald Welteb8b85a12016-06-17 00:06:42 +0200483static struct osmo_fsm subscr_conn_fsm = {
484 .name = "Subscr_Conn",
485 .states = subscr_conn_fsm_states,
486 .num_states = ARRAY_SIZE(subscr_conn_fsm_states),
487 .allstate_event_mask = 0,
488 .allstate_action = NULL,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200489 .log_subsys = DMM,
Harald Welteb8b85a12016-06-17 00:06:42 +0200490 .event_names = subscr_conn_fsm_event_names,
Harald Welte2483f1b2016-06-19 18:06:02 +0200491 .cleanup = subscr_conn_fsm_cleanup,
492 .timer_cb = subscr_conn_fsm_timeout,
Harald Welteb8b85a12016-06-17 00:06:42 +0200493};
494
Daniel Willmann4e825b62018-02-15 10:33:26 +0100495char *msc_subscr_conn_get_conn_id(struct gsm_subscriber_connection *conn)
496{
497 char *id;
498
499 switch (conn->via_ran) {
500 case RAN_GERAN_A:
501 id = talloc_asprintf(conn, "GERAN_A-%08x", conn->a.conn_id);
502 break;
503 case RAN_UTRAN_IU:
504 id = talloc_asprintf(conn, "UTRAN_IU-%08x", iu_get_conn_id(conn->iu.ue_ctx));
505 break;
506 default:
507 LOGP(DMM, LOGL_ERROR, "RAN of conn %p unknown!\n", conn);
508 return NULL;
509 }
510
511 return id;
512}
513
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200514/* Tidy up before the FSM deallocates */
515static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200516{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200517 struct gsm_subscriber_connection *conn = fi->priv;
Harald Welteb8b85a12016-06-17 00:06:42 +0200518
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200519 if (subscr_conn_fsm_has_active_transactions(fi))
520 LOGPFSML(fi, LOGL_ERROR, "Deallocating despite active transactions\n");
521
522 if (!conn) {
523 LOGP(DRLL, LOGL_ERROR, "Freeing NULL subscriber connection\n");
524 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200525 }
526
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200527 if (conn->vsub) {
528 DEBUGP(DRLL, "%s: Freeing subscriber connection\n", vlr_subscr_name(conn->vsub));
529 conn->vsub->lu_fsm = NULL;
530 conn->vsub->msc_conn_ref = NULL;
531 vlr_subscr_put(conn->vsub);
532 conn->vsub = NULL;
533 } else
534 DEBUGP(DRLL, "Freeing subscriber connection with NULL subscriber\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200535
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200536 llist_del(&conn->entry);
537}
538
539/* Signal success of Complete Layer 3. Allow to keep the conn open for Auth and Ciph. */
540void msc_subscr_conn_complete_layer_3(struct gsm_subscriber_connection *conn)
541{
542 if (!conn)
543 return;
544 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_COMPLETE_LAYER_3, NULL);
545}
546
547void subscr_conn_release_when_unused(struct gsm_subscriber_connection *conn)
548{
549 if (!conn)
550 return;
551 if (msc_subscr_conn_in_release(conn)) {
552 DEBUGP(DMM, "%s: %s: conn already in release (%s)\n",
553 vlr_subscr_name(conn->vsub), __func__,
554 osmo_fsm_inst_state_name(conn->fi));
555 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200556 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200557 if (conn->fi->state == SUBSCR_CONN_S_NEW) {
558 DEBUGP(DMM, "%s: %s: conn still being established (%s)\n",
559 vlr_subscr_name(conn->vsub), __func__,
560 osmo_fsm_inst_state_name(conn->fi));
561 return;
562 }
563 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_RELEASE_WHEN_UNUSED, NULL);
564}
565
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200566static void conn_close(struct gsm_subscriber_connection *conn, uint32_t cause, uint32_t event)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200567{
568 if (!conn) {
569 LOGP(DMM, LOGL_ERROR, "Cannot release NULL connection\n");
570 return;
571 }
572 if (msc_subscr_conn_in_release(conn)) {
573 DEBUGP(DMM, "%s(vsub=%s, cause=%u): already in release, ignore.\n",
574 __func__, vlr_subscr_name(conn->vsub), cause);
575 return;
576 }
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200577 osmo_fsm_inst_dispatch(conn->fi, event, &cause);
578}
579
580void msc_subscr_conn_close(struct gsm_subscriber_connection *conn, uint32_t cause)
581{
582 return conn_close(conn, cause, SUBSCR_CONN_E_CN_CLOSE);
583}
584
585void msc_subscr_conn_mo_close(struct gsm_subscriber_connection *conn, uint32_t cause)
586{
587 return conn_close(conn, cause, SUBSCR_CONN_E_MO_CLOSE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200588}
589
590bool msc_subscr_conn_in_release(struct gsm_subscriber_connection *conn)
591{
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200592 if (!conn || !conn->fi)
593 return true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200594 if (conn->fi->state == SUBSCR_CONN_S_RELEASING)
595 return true;
596 if (conn->fi->state == SUBSCR_CONN_S_RELEASED)
597 return true;
598 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200599}
600
Maxd83b17b2018-02-06 16:51:31 +0100601bool msc_subscr_conn_is_accepted(const struct gsm_subscriber_connection *conn)
Harald Welteb8b85a12016-06-17 00:06:42 +0200602{
603 if (!conn)
604 return false;
Harald Welte2483f1b2016-06-19 18:06:02 +0200605 if (!conn->vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200606 return false;
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200607 if (!(conn->fi->state == SUBSCR_CONN_S_ACCEPTED
608 || conn->fi->state == SUBSCR_CONN_S_COMMUNICATING))
Harald Welteb8b85a12016-06-17 00:06:42 +0200609 return false;
610 return true;
611}
612
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200613/* Indicate that *some* communication is happening with the phone, so that the conn FSM no longer times
614 * out to release within a few seconds. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200615void msc_subscr_conn_communicating(struct gsm_subscriber_connection *conn)
616{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200617 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_COMMUNICATING, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200618}
619
Harald Welteb8b85a12016-06-17 00:06:42 +0200620void msc_subscr_conn_init(void)
621{
622 osmo_fsm_register(&subscr_conn_fsm);
623}
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200624
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200625/* Allocate a new subscriber conn and FSM.
626 * Deallocation is by msc_subscr_conn_put(): when the use count reaches zero, the
627 * SUBSCR_CONN_E_RELEASE_COMPLETE event is dispatched, the FSM terminates and deallocates both FSM and
628 * conn. As long as the FSM is waiting for responses from the subscriber, it will itself hold a use count
629 * on the conn. */
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200630struct gsm_subscriber_connection *msc_subscr_conn_alloc(struct gsm_network *network,
631 enum ran_type via_ran, uint16_t lac)
632{
633 struct gsm_subscriber_connection *conn;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200634 struct osmo_fsm_inst *fi;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200635
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200636 fi = osmo_fsm_inst_alloc(&subscr_conn_fsm, network, NULL, LOGL_DEBUG, NULL);
637 if (!fi) {
638 LOGP(DMM, LOGL_ERROR, "Failed to allocate conn FSM\n");
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200639 return NULL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200640 }
641
642 conn = talloc_zero(fi, struct gsm_subscriber_connection);
643 if (!conn) {
644 osmo_fsm_inst_free(fi);
645 return NULL;
646 }
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200647
648 *conn = (struct gsm_subscriber_connection){
649 .network = network,
650 .via_ran = via_ran,
651 .lac = lac,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200652 .fi = fi,
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200653 };
654
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200655 fi->priv = conn;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200656 llist_add_tail(&conn->entry, &network->subscr_conns);
657 return conn;
658}
659
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200660bool msc_subscr_conn_is_establishing_auth_ciph(const struct gsm_subscriber_connection *conn)
661{
662 if (!conn)
663 return false;
664 return conn->fi->state == SUBSCR_CONN_S_AUTH_CIPH;
665}
666
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200667
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200668const struct value_string complete_layer3_type_names[] = {
669 { COMPLETE_LAYER3_NONE, "NONE" },
670 { COMPLETE_LAYER3_LU, "LU" },
671 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
672 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
673 { 0, NULL }
674};
675
676void msc_subscr_conn_update_id(struct gsm_subscriber_connection *conn,
677 enum complete_layer3_type from, const char *id)
678{
679 conn->complete_layer3_type = from;
Neels Hofmeyrfe4ba7c2018-04-02 23:17:50 +0200680 osmo_fsm_inst_update_id_f(conn->fi, "%s:%s", complete_layer3_type_name(from), id);
681 LOGPFSML(conn->fi, LOGL_DEBUG, "Updated ID\n");
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200682}
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200683
684static void rx_close_complete(struct gsm_subscriber_connection *conn, const char *label, bool *flag)
685{
686 if (!conn)
687 return;
688 if (!msc_subscr_conn_in_release(conn)) {
689 LOGPFSML(conn->fi, LOGL_ERROR, "Received unexpected %s, discarding right now\n",
690 label);
691 trans_conn_closed(conn);
692 osmo_fsm_inst_term(conn->fi, OSMO_FSM_TERM_ERROR, NULL);
693 return;
694 }
695 if (*flag) {
696 *flag = false;
697 msc_subscr_conn_put(conn, MSC_CONN_USE_RELEASE);
698 }
699}
700
701void msc_subscr_conn_rx_bssmap_clear_complete(struct gsm_subscriber_connection *conn)
702{
703 rx_close_complete(conn, "BSSMAP Clear Complete", &conn->a.waiting_for_clear_complete);
704}
705
706void msc_subscr_conn_rx_iu_release_complete(struct gsm_subscriber_connection *conn)
707{
708 rx_close_complete(conn, "Iu Release Complete", &conn->iu.waiting_for_release_complete);
709}