blob: c1d0e11a5e7133aa72ad3b9780f889fee547c182 [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{
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 Hofmeyre3d3dc62018-03-31 00:02:14 +0200139void subscr_conn_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
140{
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
166static bool subscr_conn_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200167{
168 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200169 struct gsm_trans *trans;
Harald Welteb8b85a12016-06-17 00:06:42 +0200170
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200171 if (conn->silent_call) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100172 LOGPFSML(fi, LOGL_DEBUG, "%s: silent call still active\n", __func__);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200173 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200174 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200175
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200176 if (conn->received_cm_service_request) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100177 LOGPFSML(fi, LOGL_DEBUG, "%s: still awaiting first request after a CM Service Request\n",
178 __func__);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200179 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200180 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200181
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200182 if (conn->vsub && !llist_empty(&conn->vsub->cs.requests)) {
183 struct subscr_request *sr;
184 if (!log_check_level(fi->fsm->log_subsys, LOGL_DEBUG)) {
185 llist_for_each_entry(sr, &conn->vsub->cs.requests, entry) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100186 LOGPFSML(fi, LOGL_DEBUG, "%s: still active: %s\n",
187 __func__, sr->label);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200188 }
189 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200190 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200191 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200192
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200193 if ((trans = trans_has_conn(conn))) {
194 LOGPFSML(fi, LOGL_DEBUG,
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100195 "%s: connection still has active transaction: %s\n",
196 __func__, gsm48_pdisc_name(trans->protocol));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200197 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200198 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200199
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200200 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200201}
202
Harald Welte2483f1b2016-06-19 18:06:02 +0200203static void subscr_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
204{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200205 struct gsm_subscriber_connection *conn = fi->priv;
206
207 /* Stop Location Update expiry for this subscriber. While the subscriber
208 * has an open connection the LU expiry timer must remain disabled.
209 * Otherwise we would kick the subscriber off the network when the timer
210 * expires e.g. during a long phone call.
211 * The LU expiry timer will restart once the connection is closed. */
212 conn->vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
213
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200214 if (!subscr_conn_fsm_has_active_transactions(fi))
215 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_UNUSED, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200216}
217
Harald Welteb8b85a12016-06-17 00:06:42 +0200218static void subscr_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data)
219{
220 switch (event) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200221 case SUBSCR_CONN_E_COMMUNICATING:
222 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_COMMUNICATING, 0, 0);
223 return;
224
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200225 case SUBSCR_CONN_E_MO_CLOSE:
226 case SUBSCR_CONN_E_CN_CLOSE:
227 log_close_event(fi, event, data);
228 /* fall through */
229 case SUBSCR_CONN_E_UNUSED:
230 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200231 return;
232
233 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200234 OSMO_ASSERT(false);
Harald Welteb8b85a12016-06-17 00:06:42 +0200235 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200236}
237
Harald Welte2483f1b2016-06-19 18:06:02 +0200238static void subscr_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
239{
240 switch (event) {
241 case SUBSCR_CONN_E_COMMUNICATING:
242 /* no-op */
243 return;
244
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200245 case SUBSCR_CONN_E_MO_CLOSE:
246 case SUBSCR_CONN_E_CN_CLOSE:
247 log_close_event(fi, event, data);
248 /* fall through */
249 case SUBSCR_CONN_E_UNUSED:
250 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0);
Harald Welte2483f1b2016-06-19 18:06:02 +0200251 return;
252
253 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200254 OSMO_ASSERT(false);
Harald Welte2483f1b2016-06-19 18:06:02 +0200255 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200256}
257
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200258static int subscr_conn_fsm_timeout(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200259{
260 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200261 if (msc_subscr_conn_in_release(conn)) {
262 LOGPFSML(fi, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
263 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_TIMEOUT, NULL);
264 } else {
Neels Hofmeyr15809592018-04-06 02:57:51 +0200265 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200266 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_CN_CLOSE, &cause);
267 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200268 return 0;
269}
270
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200271static void subscr_conn_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
Harald Welte2483f1b2016-06-19 18:06:02 +0200272{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200273 struct gsm_subscriber_connection *conn = fi->priv;
274
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200275 /* Use count for either conn->a.waiting_for_clear_complete or
276 * conn->iu.waiting_for_release_complete. 'get' it early, so we don't deallocate after tearing
277 * down active transactions. Safeguard against double-get (though it shouldn't happen). */
278 if (!msc_subscr_conn_used_by(conn, MSC_CONN_USE_RELEASE))
279 msc_subscr_conn_get(conn, MSC_CONN_USE_RELEASE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200280
281 /* Cancel pending CM Service Requests */
282 if (conn->received_cm_service_request) {
283 conn->received_cm_service_request = false;
284 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
285 }
286
287 /* Cancel all VLR FSMs, if any */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200288 vlr_subscr_cancel_attach_fsm(conn->vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200289
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200290 if (conn->vsub) {
291 /* The subscriber has no active connection anymore.
292 * Restart the periodic Location Update expiry timer for this subscriber. */
293 vlr_subscr_enable_expire_lu(conn->vsub);
294 }
295
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200296 /* If we're closing in a middle of a trans, we need to clean up */
297 trans_conn_closed(conn);
298
299 switch (conn->via_ran) {
300 case RAN_GERAN_A:
301 a_iface_tx_clear_cmd(conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200302 if (conn->a.waiting_for_clear_complete) {
303 LOGPFSML(fi, LOGL_ERROR,
304 "Unexpected: conn is already waiting for BSSMAP Clear Complete\n");
305 break;
306 }
307 conn->a.waiting_for_clear_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200308 break;
309 case RAN_UTRAN_IU:
310 ranap_iu_tx_release(conn->iu.ue_ctx, NULL);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200311 if (conn->iu.waiting_for_release_complete) {
312 LOGPFSML(fi, LOGL_ERROR,
313 "Unexpected: conn is already waiting for Iu Release Complete\n");
314 break;
315 }
316 conn->iu.waiting_for_release_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200317 break;
318 default:
319 LOGP(DMM, LOGL_ERROR, "%s: Unknown RAN type, cannot tx release/clear\n",
320 vlr_subscr_name(conn->vsub));
321 break;
322 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200323}
324
325static void subscr_conn_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
326{
327 OSMO_ASSERT(event == SUBSCR_CONN_E_UNUSED);
328 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
329}
330
331static void subscr_conn_fsm_released(struct osmo_fsm_inst *fi, uint32_t prev_state)
332{
333 /* Terminate, deallocate and also deallocate the gsm_subscriber_connection, which is allocated as
334 * a talloc child of fi. Also calls the cleanup function. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200335 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200336}
337
338#define S(x) (1 << (x))
339
340static const struct osmo_fsm_state subscr_conn_fsm_states[] = {
341 [SUBSCR_CONN_S_NEW] = {
342 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_NEW),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200343 .in_event_mask = S(SUBSCR_CONN_E_COMPLETE_LAYER_3) |
344 S(SUBSCR_CONN_E_ACCEPTED) |
345 S(SUBSCR_CONN_E_MO_CLOSE) |
346 S(SUBSCR_CONN_E_CN_CLOSE) |
347 S(SUBSCR_CONN_E_UNUSED),
348 .out_state_mask = S(SUBSCR_CONN_S_AUTH_CIPH) |
349 S(SUBSCR_CONN_S_ACCEPTED) |
350 S(SUBSCR_CONN_S_RELEASING),
351 .action = subscr_conn_fsm_new,
352 },
353 [SUBSCR_CONN_S_AUTH_CIPH] = {
354 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_AUTH_CIPH),
Harald Welteb8b85a12016-06-17 00:06:42 +0200355 .in_event_mask = S(SUBSCR_CONN_E_ACCEPTED) |
356 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200357 S(SUBSCR_CONN_E_CN_CLOSE) |
358 S(SUBSCR_CONN_E_UNUSED),
Harald Welteb8b85a12016-06-17 00:06:42 +0200359 .out_state_mask = S(SUBSCR_CONN_S_ACCEPTED) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200360 S(SUBSCR_CONN_S_RELEASING),
361 .action = subscr_conn_fsm_auth_ciph,
Harald Welteb8b85a12016-06-17 00:06:42 +0200362 },
363 [SUBSCR_CONN_S_ACCEPTED] = {
364 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_ACCEPTED),
365 /* allow everything to release for any odd behavior */
Harald Welte2483f1b2016-06-19 18:06:02 +0200366 .in_event_mask = S(SUBSCR_CONN_E_COMMUNICATING) |
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100367 S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200368 S(SUBSCR_CONN_E_ACCEPTED) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200369 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200370 S(SUBSCR_CONN_E_CN_CLOSE) |
371 S(SUBSCR_CONN_E_UNUSED),
372 .out_state_mask = S(SUBSCR_CONN_S_RELEASING) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200373 S(SUBSCR_CONN_S_COMMUNICATING),
374 .onenter = subscr_conn_fsm_accepted_enter,
Harald Welteb8b85a12016-06-17 00:06:42 +0200375 .action = subscr_conn_fsm_accepted,
376 },
Harald Welte2483f1b2016-06-19 18:06:02 +0200377 [SUBSCR_CONN_S_COMMUNICATING] = {
378 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_COMMUNICATING),
379 /* allow everything to release for any odd behavior */
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100380 .in_event_mask = S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200381 S(SUBSCR_CONN_E_ACCEPTED) |
382 S(SUBSCR_CONN_E_COMMUNICATING) |
383 S(SUBSCR_CONN_E_MO_CLOSE) |
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200384 S(SUBSCR_CONN_E_CN_CLOSE) |
385 S(SUBSCR_CONN_E_UNUSED),
386 .out_state_mask = S(SUBSCR_CONN_S_RELEASING),
Harald Welte2483f1b2016-06-19 18:06:02 +0200387 .action = subscr_conn_fsm_communicating,
388 },
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200389 [SUBSCR_CONN_S_RELEASING] = {
390 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASING),
391 .in_event_mask = S(SUBSCR_CONN_E_UNUSED),
392 .out_state_mask = S(SUBSCR_CONN_S_RELEASED),
393 .onenter = subscr_conn_fsm_releasing_onenter,
394 .action = subscr_conn_fsm_releasing,
395 },
Harald Welteb8b85a12016-06-17 00:06:42 +0200396 [SUBSCR_CONN_S_RELEASED] = {
397 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASED),
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200398 .onenter = subscr_conn_fsm_released,
Harald Welteb8b85a12016-06-17 00:06:42 +0200399 },
400};
401
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200402static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
403
Harald Welteb8b85a12016-06-17 00:06:42 +0200404static struct osmo_fsm subscr_conn_fsm = {
405 .name = "Subscr_Conn",
406 .states = subscr_conn_fsm_states,
407 .num_states = ARRAY_SIZE(subscr_conn_fsm_states),
408 .allstate_event_mask = 0,
409 .allstate_action = NULL,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200410 .log_subsys = DMM,
Harald Welteb8b85a12016-06-17 00:06:42 +0200411 .event_names = subscr_conn_fsm_event_names,
Harald Welte2483f1b2016-06-19 18:06:02 +0200412 .cleanup = subscr_conn_fsm_cleanup,
413 .timer_cb = subscr_conn_fsm_timeout,
Harald Welteb8b85a12016-06-17 00:06:42 +0200414};
415
Daniel Willmann4e825b62018-02-15 10:33:26 +0100416char *msc_subscr_conn_get_conn_id(struct gsm_subscriber_connection *conn)
417{
418 char *id;
419
420 switch (conn->via_ran) {
421 case RAN_GERAN_A:
422 id = talloc_asprintf(conn, "GERAN_A-%08x", conn->a.conn_id);
423 break;
424 case RAN_UTRAN_IU:
425 id = talloc_asprintf(conn, "UTRAN_IU-%08x", iu_get_conn_id(conn->iu.ue_ctx));
426 break;
427 default:
428 LOGP(DMM, LOGL_ERROR, "RAN of conn %p unknown!\n", conn);
429 return NULL;
430 }
431
432 return id;
433}
434
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200435/* Tidy up before the FSM deallocates */
436static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200437{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200438 struct gsm_subscriber_connection *conn = fi->priv;
Harald Welteb8b85a12016-06-17 00:06:42 +0200439
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200440 if (subscr_conn_fsm_has_active_transactions(fi))
441 LOGPFSML(fi, LOGL_ERROR, "Deallocating despite active transactions\n");
442
443 if (!conn) {
444 LOGP(DRLL, LOGL_ERROR, "Freeing NULL subscriber connection\n");
445 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200446 }
447
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200448 if (conn->vsub) {
449 DEBUGP(DRLL, "%s: Freeing subscriber connection\n", vlr_subscr_name(conn->vsub));
450 conn->vsub->lu_fsm = NULL;
451 conn->vsub->msc_conn_ref = NULL;
452 vlr_subscr_put(conn->vsub);
453 conn->vsub = NULL;
454 } else
455 DEBUGP(DRLL, "Freeing subscriber connection with NULL subscriber\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200456
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200457 llist_del(&conn->entry);
458}
459
460/* Signal success of Complete Layer 3. Allow to keep the conn open for Auth and Ciph. */
461void msc_subscr_conn_complete_layer_3(struct gsm_subscriber_connection *conn)
462{
463 if (!conn)
464 return;
465 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_COMPLETE_LAYER_3, NULL);
466}
467
468void subscr_conn_release_when_unused(struct gsm_subscriber_connection *conn)
469{
470 if (!conn)
471 return;
472 if (msc_subscr_conn_in_release(conn)) {
473 DEBUGP(DMM, "%s: %s: conn already in release (%s)\n",
474 vlr_subscr_name(conn->vsub), __func__,
475 osmo_fsm_inst_state_name(conn->fi));
476 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200477 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200478 if (conn->fi->state == SUBSCR_CONN_S_NEW) {
479 DEBUGP(DMM, "%s: %s: conn still being established (%s)\n",
480 vlr_subscr_name(conn->vsub), __func__,
481 osmo_fsm_inst_state_name(conn->fi));
482 return;
483 }
484 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_RELEASE_WHEN_UNUSED, NULL);
485}
486
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200487static void conn_close(struct gsm_subscriber_connection *conn, uint32_t cause, uint32_t event)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200488{
489 if (!conn) {
490 LOGP(DMM, LOGL_ERROR, "Cannot release NULL connection\n");
491 return;
492 }
493 if (msc_subscr_conn_in_release(conn)) {
494 DEBUGP(DMM, "%s(vsub=%s, cause=%u): already in release, ignore.\n",
495 __func__, vlr_subscr_name(conn->vsub), cause);
496 return;
497 }
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200498 osmo_fsm_inst_dispatch(conn->fi, event, &cause);
499}
500
501void msc_subscr_conn_close(struct gsm_subscriber_connection *conn, uint32_t cause)
502{
503 return conn_close(conn, cause, SUBSCR_CONN_E_CN_CLOSE);
504}
505
506void msc_subscr_conn_mo_close(struct gsm_subscriber_connection *conn, uint32_t cause)
507{
508 return conn_close(conn, cause, SUBSCR_CONN_E_MO_CLOSE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200509}
510
511bool msc_subscr_conn_in_release(struct gsm_subscriber_connection *conn)
512{
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200513 if (!conn || !conn->fi)
514 return true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200515 if (conn->fi->state == SUBSCR_CONN_S_RELEASING)
516 return true;
517 if (conn->fi->state == SUBSCR_CONN_S_RELEASED)
518 return true;
519 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200520}
521
Maxd83b17b2018-02-06 16:51:31 +0100522bool msc_subscr_conn_is_accepted(const struct gsm_subscriber_connection *conn)
Harald Welteb8b85a12016-06-17 00:06:42 +0200523{
524 if (!conn)
525 return false;
Harald Welte2483f1b2016-06-19 18:06:02 +0200526 if (!conn->vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200527 return false;
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200528 if (!(conn->fi->state == SUBSCR_CONN_S_ACCEPTED
529 || conn->fi->state == SUBSCR_CONN_S_COMMUNICATING))
Harald Welteb8b85a12016-06-17 00:06:42 +0200530 return false;
531 return true;
532}
533
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200534/* Indicate that *some* communication is happening with the phone, so that the conn FSM no longer times
535 * out to release within a few seconds. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200536void msc_subscr_conn_communicating(struct gsm_subscriber_connection *conn)
537{
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200538 osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_COMMUNICATING, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200539}
540
Harald Welteb8b85a12016-06-17 00:06:42 +0200541void msc_subscr_conn_init(void)
542{
543 osmo_fsm_register(&subscr_conn_fsm);
544}
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200545
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200546/* Allocate a new subscriber conn and FSM.
547 * Deallocation is by msc_subscr_conn_put(): when the use count reaches zero, the
548 * SUBSCR_CONN_E_RELEASE_COMPLETE event is dispatched, the FSM terminates and deallocates both FSM and
549 * conn. As long as the FSM is waiting for responses from the subscriber, it will itself hold a use count
550 * on the conn. */
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200551struct gsm_subscriber_connection *msc_subscr_conn_alloc(struct gsm_network *network,
552 enum ran_type via_ran, uint16_t lac)
553{
554 struct gsm_subscriber_connection *conn;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200555 struct osmo_fsm_inst *fi;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200556
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200557 fi = osmo_fsm_inst_alloc(&subscr_conn_fsm, network, NULL, LOGL_DEBUG, NULL);
558 if (!fi) {
559 LOGP(DMM, LOGL_ERROR, "Failed to allocate conn FSM\n");
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200560 return NULL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200561 }
562
563 conn = talloc_zero(fi, struct gsm_subscriber_connection);
564 if (!conn) {
565 osmo_fsm_inst_free(fi);
566 return NULL;
567 }
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200568
569 *conn = (struct gsm_subscriber_connection){
570 .network = network,
571 .via_ran = via_ran,
572 .lac = lac,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200573 .fi = fi,
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200574 };
575
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200576 fi->priv = conn;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200577 llist_add_tail(&conn->entry, &network->subscr_conns);
578 return conn;
579}
580
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200581bool msc_subscr_conn_is_establishing_auth_ciph(const struct gsm_subscriber_connection *conn)
582{
583 if (!conn)
584 return false;
585 return conn->fi->state == SUBSCR_CONN_S_AUTH_CIPH;
586}
587
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200588
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200589const struct value_string complete_layer3_type_names[] = {
590 { COMPLETE_LAYER3_NONE, "NONE" },
591 { COMPLETE_LAYER3_LU, "LU" },
592 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
593 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
594 { 0, NULL }
595};
596
597void msc_subscr_conn_update_id(struct gsm_subscriber_connection *conn,
598 enum complete_layer3_type from, const char *id)
599{
600 conn->complete_layer3_type = from;
Neels Hofmeyrfe4ba7c2018-04-02 23:17:50 +0200601 osmo_fsm_inst_update_id_f(conn->fi, "%s:%s", complete_layer3_type_name(from), id);
602 LOGPFSML(conn->fi, LOGL_DEBUG, "Updated ID\n");
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200603}
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200604
605static void rx_close_complete(struct gsm_subscriber_connection *conn, const char *label, bool *flag)
606{
607 if (!conn)
608 return;
609 if (!msc_subscr_conn_in_release(conn)) {
610 LOGPFSML(conn->fi, LOGL_ERROR, "Received unexpected %s, discarding right now\n",
611 label);
612 trans_conn_closed(conn);
613 osmo_fsm_inst_term(conn->fi, OSMO_FSM_TERM_ERROR, NULL);
614 return;
615 }
616 if (*flag) {
617 *flag = false;
618 msc_subscr_conn_put(conn, MSC_CONN_USE_RELEASE);
619 }
620}
621
622void msc_subscr_conn_rx_bssmap_clear_complete(struct gsm_subscriber_connection *conn)
623{
624 rx_close_complete(conn, "BSSMAP Clear Complete", &conn->a.waiting_for_clear_complete);
625}
626
627void msc_subscr_conn_rx_iu_release_complete(struct gsm_subscriber_connection *conn)
628{
629 rx_close_complete(conn, "Iu Release Complete", &conn->iu.waiting_for_release_complete);
630}