blob: 9f280bcde577dce2687ab23582dfad877e14762f [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),
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{
106 if (data)
107 LOGPFSML(fi, LOGL_DEBUG, "Close event, cause %u\n", *(uint32_t*)data);
108}
109
110static void subscr_conn_fsm_new(struct osmo_fsm_inst *fi, uint32_t event, void *data)
111{
112 switch (event) {
113 case SUBSCR_CONN_E_COMPLETE_LAYER_3:
114 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_AUTH_CIPH, SUBSCR_CONN_TIMEOUT, 0);
115 return;
116
117 case SUBSCR_CONN_E_ACCEPTED:
118 evaluate_acceptance_outcome(fi, true);
119 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED, SUBSCR_CONN_TIMEOUT, 0);
120 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200121
122 case SUBSCR_CONN_E_MO_CLOSE:
123 case SUBSCR_CONN_E_CN_CLOSE:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200124 log_close_event(fi, event, data);
125 evaluate_acceptance_outcome(fi, false);
126 /* fall through */
127 case SUBSCR_CONN_E_UNUSED:
128 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
129 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200130
131 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200132 OSMO_ASSERT(false);
Harald Welteb8b85a12016-06-17 00:06:42 +0200133 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200134}
135
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200136void subscr_conn_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
137{
138 /* If accepted, transition the state, all other cases mean failure. */
139 switch (event) {
140 case SUBSCR_CONN_E_ACCEPTED:
141 evaluate_acceptance_outcome(fi, true);
142 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED, SUBSCR_CONN_TIMEOUT, 0);
143 return;
144
145 case SUBSCR_CONN_E_UNUSED:
146 LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n",
147 osmo_fsm_event_name(fi->fsm, event));
148 return;
149
150 case SUBSCR_CONN_E_MO_CLOSE:
151 case SUBSCR_CONN_E_CN_CLOSE:
152 log_close_event(fi, event, data);
153 evaluate_acceptance_outcome(fi, false);
154 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
155 return;
156
157
158 default:
159 OSMO_ASSERT(false);
160 }
161}
162
163static bool subscr_conn_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200164{
165 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200166 struct gsm_trans *trans;
Harald Welteb8b85a12016-06-17 00:06:42 +0200167
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200168 if (conn->silent_call) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100169 LOGPFSML(fi, LOGL_DEBUG, "%s: silent call still active\n", __func__);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200170 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200171 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200172
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200173 if (conn->received_cm_service_request) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100174 LOGPFSML(fi, LOGL_DEBUG, "%s: still awaiting first request after a CM Service Request\n",
175 __func__);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200176 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200177 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200178
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200179 if (conn->vsub && !llist_empty(&conn->vsub->cs.requests)) {
180 struct subscr_request *sr;
181 if (!log_check_level(fi->fsm->log_subsys, LOGL_DEBUG)) {
182 llist_for_each_entry(sr, &conn->vsub->cs.requests, entry) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100183 LOGPFSML(fi, LOGL_DEBUG, "%s: still active: %s\n",
184 __func__, sr->label);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200185 }
186 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200187 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200188 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200189
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200190 if ((trans = trans_has_conn(conn))) {
191 LOGPFSML(fi, LOGL_DEBUG,
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100192 "%s: connection still has active transaction: %s\n",
193 __func__, gsm48_pdisc_name(trans->protocol));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200194 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200195 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200196
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200197 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200198}
199
Harald Welte2483f1b2016-06-19 18:06:02 +0200200static void subscr_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
201{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200202 if (!subscr_conn_fsm_has_active_transactions(fi))
203 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_UNUSED, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200204}
205
Harald Welteb8b85a12016-06-17 00:06:42 +0200206static void subscr_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data)
207{
208 switch (event) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200209 case SUBSCR_CONN_E_COMMUNICATING:
210 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_COMMUNICATING, 0, 0);
211 return;
212
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200213 case SUBSCR_CONN_E_MO_CLOSE:
214 case SUBSCR_CONN_E_CN_CLOSE:
215 log_close_event(fi, event, data);
216 /* fall through */
217 case SUBSCR_CONN_E_UNUSED:
218 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200219 return;
220
221 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200222 OSMO_ASSERT(false);
Harald Welteb8b85a12016-06-17 00:06:42 +0200223 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200224}
225
Harald Welte2483f1b2016-06-19 18:06:02 +0200226static void subscr_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
227{
228 switch (event) {
229 case SUBSCR_CONN_E_COMMUNICATING:
230 /* no-op */
231 return;
232
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200233 case SUBSCR_CONN_E_MO_CLOSE:
234 case SUBSCR_CONN_E_CN_CLOSE:
235 log_close_event(fi, event, data);
236 /* fall through */
237 case SUBSCR_CONN_E_UNUSED:
238 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
Harald Welte2483f1b2016-06-19 18:06:02 +0200239 return;
240
241 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200242 OSMO_ASSERT(false);
Harald Welte2483f1b2016-06-19 18:06:02 +0200243 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200244}
245
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200246static int subscr_conn_fsm_timeout(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200247{
248 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200249 if (msc_subscr_conn_in_release(conn)) {
250 LOGPFSML(fi, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
251 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_TIMEOUT, NULL);
252 } else {
253 uint32_t cause = GSM_CAUSE_NET_FAIL;
254 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_CN_CLOSE, &cause);
255 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200256 return 0;
257}
258
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200259static void subscr_conn_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
Harald Welte2483f1b2016-06-19 18:06:02 +0200260{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200261 struct gsm_subscriber_connection *conn = fi->priv;
262
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200263 /* Use count for either conn->a.waiting_for_clear_complete or
264 * conn->iu.waiting_for_release_complete. 'get' it early, so we don't deallocate after tearing
265 * down active transactions. Safeguard against double-get (though it shouldn't happen). */
266 if (!msc_subscr_conn_used_by(conn, MSC_CONN_USE_RELEASE))
267 msc_subscr_conn_get(conn, MSC_CONN_USE_RELEASE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200268
269 /* Cancel pending CM Service Requests */
270 if (conn->received_cm_service_request) {
271 conn->received_cm_service_request = false;
272 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
273 }
274
275 /* Cancel all VLR FSMs, if any */
276 vlr_subscr_conn_timeout(conn->vsub);
277
278 /* If we're closing in a middle of a trans, we need to clean up */
279 trans_conn_closed(conn);
280
281 switch (conn->via_ran) {
282 case RAN_GERAN_A:
283 a_iface_tx_clear_cmd(conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200284 if (conn->a.waiting_for_clear_complete) {
285 LOGPFSML(fi, LOGL_ERROR,
286 "Unexpected: conn is already waiting for BSSMAP Clear Complete\n");
287 break;
288 }
289 conn->a.waiting_for_clear_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200290 break;
291 case RAN_UTRAN_IU:
292 ranap_iu_tx_release(conn->iu.ue_ctx, NULL);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200293 if (conn->iu.waiting_for_release_complete) {
294 LOGPFSML(fi, LOGL_ERROR,
295 "Unexpected: conn is already waiting for Iu Release Complete\n");
296 break;
297 }
298 conn->iu.waiting_for_release_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200299 break;
300 default:
301 LOGP(DMM, LOGL_ERROR, "%s: Unknown RAN type, cannot tx release/clear\n",
302 vlr_subscr_name(conn->vsub));
303 break;
304 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200305}
306
307static void subscr_conn_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
308{
309 OSMO_ASSERT(event == SUBSCR_CONN_E_UNUSED);
310 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
311}
312
313static void subscr_conn_fsm_released(struct osmo_fsm_inst *fi, uint32_t prev_state)
314{
315 /* Terminate, deallocate and also deallocate the gsm_subscriber_connection, which is allocated as
316 * a talloc child of fi. Also calls the cleanup function. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200317 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200318}
319
320#define S(x) (1 << (x))
321
322static const struct osmo_fsm_state subscr_conn_fsm_states[] = {
323 [SUBSCR_CONN_S_NEW] = {
324 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_NEW),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200325 .in_event_mask = S(SUBSCR_CONN_E_COMPLETE_LAYER_3) |
326 S(SUBSCR_CONN_E_ACCEPTED) |
327 S(SUBSCR_CONN_E_MO_CLOSE) |
328 S(SUBSCR_CONN_E_CN_CLOSE) |
329 S(SUBSCR_CONN_E_UNUSED),
330 .out_state_mask = S(SUBSCR_CONN_S_AUTH_CIPH) |
331 S(SUBSCR_CONN_S_ACCEPTED) |
332 S(SUBSCR_CONN_S_RELEASING),
333 .action = subscr_conn_fsm_new,
334 },
335 [SUBSCR_CONN_S_AUTH_CIPH] = {
336 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_AUTH_CIPH),
Harald Welteb8b85a12016-06-17 00:06:42 +0200337 .in_event_mask = S(SUBSCR_CONN_E_ACCEPTED) |
338 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200339 S(SUBSCR_CONN_E_CN_CLOSE) |
340 S(SUBSCR_CONN_E_UNUSED),
Harald Welteb8b85a12016-06-17 00:06:42 +0200341 .out_state_mask = S(SUBSCR_CONN_S_ACCEPTED) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200342 S(SUBSCR_CONN_S_RELEASING),
343 .action = subscr_conn_fsm_auth_ciph,
Harald Welteb8b85a12016-06-17 00:06:42 +0200344 },
345 [SUBSCR_CONN_S_ACCEPTED] = {
346 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_ACCEPTED),
347 /* allow everything to release for any odd behavior */
Harald Welte2483f1b2016-06-19 18:06:02 +0200348 .in_event_mask = S(SUBSCR_CONN_E_COMMUNICATING) |
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100349 S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200350 S(SUBSCR_CONN_E_ACCEPTED) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200351 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200352 S(SUBSCR_CONN_E_CN_CLOSE) |
353 S(SUBSCR_CONN_E_UNUSED),
354 .out_state_mask = S(SUBSCR_CONN_S_RELEASING) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200355 S(SUBSCR_CONN_S_COMMUNICATING),
356 .onenter = subscr_conn_fsm_accepted_enter,
Harald Welteb8b85a12016-06-17 00:06:42 +0200357 .action = subscr_conn_fsm_accepted,
358 },
Harald Welte2483f1b2016-06-19 18:06:02 +0200359 [SUBSCR_CONN_S_COMMUNICATING] = {
360 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_COMMUNICATING),
361 /* allow everything to release for any odd behavior */
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100362 .in_event_mask = S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200363 S(SUBSCR_CONN_E_ACCEPTED) |
364 S(SUBSCR_CONN_E_COMMUNICATING) |
365 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200366 S(SUBSCR_CONN_E_CN_CLOSE) |
367 S(SUBSCR_CONN_E_UNUSED),
368 .out_state_mask = S(SUBSCR_CONN_S_RELEASING),
Harald Welte2483f1b2016-06-19 18:06:02 +0200369 .action = subscr_conn_fsm_communicating,
370 },
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200371 [SUBSCR_CONN_S_RELEASING] = {
372 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASING),
373 .in_event_mask = S(SUBSCR_CONN_E_UNUSED),
374 .out_state_mask = S(SUBSCR_CONN_S_RELEASED),
375 .onenter = subscr_conn_fsm_releasing_onenter,
376 .action = subscr_conn_fsm_releasing,
377 },
Harald Welteb8b85a12016-06-17 00:06:42 +0200378 [SUBSCR_CONN_S_RELEASED] = {
379 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASED),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200380 .onenter = subscr_conn_fsm_released,
Harald Welteb8b85a12016-06-17 00:06:42 +0200381 },
382};
383
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200384static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
385
Harald Welteb8b85a12016-06-17 00:06:42 +0200386static struct osmo_fsm subscr_conn_fsm = {
387 .name = "Subscr_Conn",
388 .states = subscr_conn_fsm_states,
389 .num_states = ARRAY_SIZE(subscr_conn_fsm_states),
390 .allstate_event_mask = 0,
391 .allstate_action = NULL,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200392 .log_subsys = DMM,
Harald Welteb8b85a12016-06-17 00:06:42 +0200393 .event_names = subscr_conn_fsm_event_names,
Harald Welte2483f1b2016-06-19 18:06:02 +0200394 .cleanup = subscr_conn_fsm_cleanup,
395 .timer_cb = subscr_conn_fsm_timeout,
Harald Welteb8b85a12016-06-17 00:06:42 +0200396};
397
Daniel Willmann4e825b62018-02-15 10:33:26 +0100398char *msc_subscr_conn_get_conn_id(struct gsm_subscriber_connection *conn)
399{
400 char *id;
401
402 switch (conn->via_ran) {
403 case RAN_GERAN_A:
404 id = talloc_asprintf(conn, "GERAN_A-%08x", conn->a.conn_id);
405 break;
406 case RAN_UTRAN_IU:
407 id = talloc_asprintf(conn, "UTRAN_IU-%08x", iu_get_conn_id(conn->iu.ue_ctx));
408 break;
409 default:
410 LOGP(DMM, LOGL_ERROR, "RAN of conn %p unknown!\n", conn);
411 return NULL;
412 }
413
414 return id;
415}
416
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200417/* Tidy up before the FSM deallocates */
418static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200419{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200420 struct gsm_subscriber_connection *conn = fi->priv;
Harald Welteb8b85a12016-06-17 00:06:42 +0200421
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200422 if (subscr_conn_fsm_has_active_transactions(fi))
423 LOGPFSML(fi, LOGL_ERROR, "Deallocating despite active transactions\n");
424
425 if (!conn) {
426 LOGP(DRLL, LOGL_ERROR, "Freeing NULL subscriber connection\n");
427 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200428 }
429
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200430 if (conn->vsub) {
431 DEBUGP(DRLL, "%s: Freeing subscriber connection\n", vlr_subscr_name(conn->vsub));
432 conn->vsub->lu_fsm = NULL;
433 conn->vsub->msc_conn_ref = NULL;
434 vlr_subscr_put(conn->vsub);
435 conn->vsub = NULL;
436 } else
437 DEBUGP(DRLL, "Freeing subscriber connection with NULL subscriber\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200438
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200439 llist_del(&conn->entry);
440}
441
442/* Signal success of Complete Layer 3. Allow to keep the conn open for Auth and Ciph. */
443void msc_subscr_conn_complete_layer_3(struct gsm_subscriber_connection *conn)
444{
445 if (!conn)
446 return;
447 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_COMPLETE_LAYER_3, NULL);
448}
449
450void subscr_conn_release_when_unused(struct gsm_subscriber_connection *conn)
451{
452 if (!conn)
453 return;
454 if (msc_subscr_conn_in_release(conn)) {
455 DEBUGP(DMM, "%s: %s: conn already in release (%s)\n",
456 vlr_subscr_name(conn->vsub), __func__,
457 osmo_fsm_inst_state_name(conn->fi));
458 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200459 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200460 if (conn->fi->state == SUBSCR_CONN_S_NEW) {
461 DEBUGP(DMM, "%s: %s: conn still being established (%s)\n",
462 vlr_subscr_name(conn->vsub), __func__,
463 osmo_fsm_inst_state_name(conn->fi));
464 return;
465 }
466 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_RELEASE_WHEN_UNUSED, NULL);
467}
468
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200469static void conn_close(struct gsm_subscriber_connection *conn, uint32_t cause, uint32_t event)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200470{
471 if (!conn) {
472 LOGP(DMM, LOGL_ERROR, "Cannot release NULL connection\n");
473 return;
474 }
475 if (msc_subscr_conn_in_release(conn)) {
476 DEBUGP(DMM, "%s(vsub=%s, cause=%u): already in release, ignore.\n",
477 __func__, vlr_subscr_name(conn->vsub), cause);
478 return;
479 }
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200480 osmo_fsm_inst_dispatch(conn->fi, event, &cause);
481}
482
483void msc_subscr_conn_close(struct gsm_subscriber_connection *conn, uint32_t cause)
484{
485 return conn_close(conn, cause, SUBSCR_CONN_E_CN_CLOSE);
486}
487
488void msc_subscr_conn_mo_close(struct gsm_subscriber_connection *conn, uint32_t cause)
489{
490 return conn_close(conn, cause, SUBSCR_CONN_E_MO_CLOSE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200491}
492
493bool msc_subscr_conn_in_release(struct gsm_subscriber_connection *conn)
494{
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200495 if (!conn || !conn->fi)
496 return true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200497 if (conn->fi->state == SUBSCR_CONN_S_RELEASING)
498 return true;
499 if (conn->fi->state == SUBSCR_CONN_S_RELEASED)
500 return true;
501 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200502}
503
Maxd83b17b2018-02-06 16:51:31 +0100504bool msc_subscr_conn_is_accepted(const struct gsm_subscriber_connection *conn)
Harald Welteb8b85a12016-06-17 00:06:42 +0200505{
506 if (!conn)
507 return false;
Harald Welte2483f1b2016-06-19 18:06:02 +0200508 if (!conn->vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200509 return false;
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200510 if (!(conn->fi->state == SUBSCR_CONN_S_ACCEPTED
511 || conn->fi->state == SUBSCR_CONN_S_COMMUNICATING))
Harald Welteb8b85a12016-06-17 00:06:42 +0200512 return false;
513 return true;
514}
515
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200516/* Indicate that *some* communication is happening with the phone, so that the conn FSM no longer times
517 * out to release within a few seconds. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200518void msc_subscr_conn_communicating(struct gsm_subscriber_connection *conn)
519{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200520 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_COMMUNICATING, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200521}
522
Harald Welteb8b85a12016-06-17 00:06:42 +0200523void msc_subscr_conn_init(void)
524{
525 osmo_fsm_register(&subscr_conn_fsm);
526}
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200527
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200528/* Allocate a new subscriber conn and FSM.
529 * Deallocation is by msc_subscr_conn_put(): when the use count reaches zero, the
530 * SUBSCR_CONN_E_RELEASE_COMPLETE event is dispatched, the FSM terminates and deallocates both FSM and
531 * conn. As long as the FSM is waiting for responses from the subscriber, it will itself hold a use count
532 * on the conn. */
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200533struct gsm_subscriber_connection *msc_subscr_conn_alloc(struct gsm_network *network,
534 enum ran_type via_ran, uint16_t lac)
535{
536 struct gsm_subscriber_connection *conn;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200537 struct osmo_fsm_inst *fi;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200538
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200539 fi = osmo_fsm_inst_alloc(&subscr_conn_fsm, network, NULL, LOGL_DEBUG, NULL);
540 if (!fi) {
541 LOGP(DMM, LOGL_ERROR, "Failed to allocate conn FSM\n");
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200542 return NULL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200543 }
544
545 conn = talloc_zero(fi, struct gsm_subscriber_connection);
546 if (!conn) {
547 osmo_fsm_inst_free(fi);
548 return NULL;
549 }
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200550
551 *conn = (struct gsm_subscriber_connection){
552 .network = network,
553 .via_ran = via_ran,
554 .lac = lac,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200555 .fi = fi,
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200556 };
557
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200558 fi->priv = conn;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200559 llist_add_tail(&conn->entry, &network->subscr_conns);
560 return conn;
561}
562
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200563bool msc_subscr_conn_is_establishing_auth_ciph(const struct gsm_subscriber_connection *conn)
564{
565 if (!conn)
566 return false;
567 return conn->fi->state == SUBSCR_CONN_S_AUTH_CIPH;
568}
569
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200570
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200571const struct value_string complete_layer3_type_names[] = {
572 { COMPLETE_LAYER3_NONE, "NONE" },
573 { COMPLETE_LAYER3_LU, "LU" },
574 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
575 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
576 { 0, NULL }
577};
578
579void msc_subscr_conn_update_id(struct gsm_subscriber_connection *conn,
580 enum complete_layer3_type from, const char *id)
581{
582 conn->complete_layer3_type = from;
Neels Hofmeyrfe4ba7c2018-04-02 23:17:50 +0200583 osmo_fsm_inst_update_id_f(conn->fi, "%s:%s", complete_layer3_type_name(from), id);
584 LOGPFSML(conn->fi, LOGL_DEBUG, "Updated ID\n");
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200585}
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200586
587static void rx_close_complete(struct gsm_subscriber_connection *conn, const char *label, bool *flag)
588{
589 if (!conn)
590 return;
591 if (!msc_subscr_conn_in_release(conn)) {
592 LOGPFSML(conn->fi, LOGL_ERROR, "Received unexpected %s, discarding right now\n",
593 label);
594 trans_conn_closed(conn);
595 osmo_fsm_inst_term(conn->fi, OSMO_FSM_TERM_ERROR, NULL);
596 return;
597 }
598 if (*flag) {
599 *flag = false;
600 msc_subscr_conn_put(conn, MSC_CONN_USE_RELEASE);
601 }
602}
603
604void msc_subscr_conn_rx_bssmap_clear_complete(struct gsm_subscriber_connection *conn)
605{
606 rx_close_complete(conn, "BSSMAP Clear Complete", &conn->a.waiting_for_clear_complete);
607}
608
609void msc_subscr_conn_rx_iu_release_complete(struct gsm_subscriber_connection *conn)
610{
611 rx_close_complete(conn, "Iu Release Complete", &conn->iu.waiting_for_release_complete);
612}