blob: be6bde0b0f07140ba379c7be41b0a1e01d91ec59 [file] [log] [blame]
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001/* MSC RAN connection implementation */
Harald Welteb8b85a12016-06-17 00:06:42 +02002
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 Hofmeyra8945ce2018-11-30 00:44:32 +010028#include <osmocom/msc/ran_conn.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020029#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>
Harald Welte0df904d2018-12-03 11:00:04 +010034#include <osmocom/msc/sgs_iface.h>
Daniel Willmann4e825b62018-02-15 10:33:26 +010035#include <osmocom/msc/iucs.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020036
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020037#include "../../bscconfig.h"
38#ifdef BUILD_IU
39#include <osmocom/ranap/iu_client.h>
40#else
41#include <osmocom/msc/iu_dummy.h>
42#endif
43
Neels Hofmeyrc036b792018-11-29 22:37:51 +010044#define RAN_CONN_TIMEOUT 5 /* seconds */
Harald Welteb8b85a12016-06-17 00:06:42 +020045
Neels Hofmeyrc036b792018-11-29 22:37:51 +010046static const struct value_string ran_conn_fsm_event_names[] = {
47 OSMO_VALUE_STRING(RAN_CONN_E_COMPLETE_LAYER_3),
48 OSMO_VALUE_STRING(RAN_CONN_E_CLASSMARK_UPDATE),
49 OSMO_VALUE_STRING(RAN_CONN_E_ACCEPTED),
50 OSMO_VALUE_STRING(RAN_CONN_E_COMMUNICATING),
51 OSMO_VALUE_STRING(RAN_CONN_E_RELEASE_WHEN_UNUSED),
52 OSMO_VALUE_STRING(RAN_CONN_E_MO_CLOSE),
53 OSMO_VALUE_STRING(RAN_CONN_E_CN_CLOSE),
54 OSMO_VALUE_STRING(RAN_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{
Neels Hofmeyrc036b792018-11-29 22:37:51 +010060 struct ran_conn *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{
Neels Hofmeyrc036b792018-11-29 22:37:51 +010084 struct ran_conn *conn = fi->priv;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020085
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;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +010098 ran_conn_get(conn, RAN_CONN_USE_CM_SERVICE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020099 }
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
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100114static void ran_conn_fsm_new(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200115{
116 switch (event) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100117 case RAN_CONN_E_COMPLETE_LAYER_3:
118 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_AUTH_CIPH, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200119 return;
120
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100121 case RAN_CONN_E_ACCEPTED:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200122 evaluate_acceptance_outcome(fi, true);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100123 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_ACCEPTED, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200124 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200125
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100126 case RAN_CONN_E_MO_CLOSE:
127 case RAN_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 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100131 case RAN_CONN_E_UNUSED:
132 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200133 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 Hofmeyrc036b792018-11-29 22:37:51 +0100140static void ran_conn_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200141{
142 /* If accepted, transition the state, all other cases mean failure. */
143 switch (event) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100144 case RAN_CONN_E_ACCEPTED:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200145 evaluate_acceptance_outcome(fi, true);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100146 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_ACCEPTED, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200147 return;
148
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100149 case RAN_CONN_E_UNUSED:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200150 LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n",
151 osmo_fsm_event_name(fi->fsm, event));
152 return;
153
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100154 case RAN_CONN_E_MO_CLOSE:
155 case RAN_CONN_E_CN_CLOSE:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200156 log_close_event(fi, event, data);
157 evaluate_acceptance_outcome(fi, false);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100158 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200159 return;
160
161
162 default:
163 OSMO_ASSERT(false);
164 }
165}
166
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100167int ran_conn_classmark_request_then_cipher_mode_cmd(struct ran_conn *conn, bool umts_aka,
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200168 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
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100181 osmo_fsm_inst_state_chg(conn->fi, RAN_CONN_S_WAIT_CLASSMARK_UPDATE, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200182 return 0;
183}
184
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100185static void ran_conn_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200186{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100187 struct ran_conn *conn = fi->priv;
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200188 switch (event) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100189 case RAN_CONN_E_CLASSMARK_UPDATE:
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200190 /* 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.
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100194 * ran_conn_classmark_request_then_cipher_mode_cmd() sets an enum value to indicate that
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200195 * 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. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100200 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_AUTH_CIPH, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100201 if (ran_conn_geran_set_cipher_mode(conn, conn->geran_set_cipher_mode.umts_aka,
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200202 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
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100210 case RAN_CONN_E_UNUSED:
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200211 LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n",
212 osmo_fsm_event_name(fi->fsm, event));
213 return;
214
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100215 case RAN_CONN_E_MO_CLOSE:
216 case RAN_CONN_E_CN_CLOSE:
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200217 log_close_event(fi, event, data);
218 evaluate_acceptance_outcome(fi, false);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100219 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200220 return;
221
222 default:
223 OSMO_ASSERT(false);
224 }
225}
226
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100227static bool ran_conn_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200228{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100229 struct ran_conn *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
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100264static void ran_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
Harald Welte2483f1b2016-06-19 18:06:02 +0200265{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100266 struct ran_conn *conn = fi->priv;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200267
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 Hofmeyrc036b792018-11-29 22:37:51 +0100275 if (!ran_conn_fsm_has_active_transactions(fi))
276 osmo_fsm_inst_dispatch(fi, RAN_CONN_E_UNUSED, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200277}
278
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100279static void ran_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Harald Welteb8b85a12016-06-17 00:06:42 +0200280{
281 switch (event) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100282 case RAN_CONN_E_COMPLETE_LAYER_3:
Neels Hofmeyr36115c92018-08-23 15:52:43 +0200283 /* 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
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100288 case RAN_CONN_E_COMMUNICATING:
289 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_COMMUNICATING, 0, 0);
Harald Welte2483f1b2016-06-19 18:06:02 +0200290 return;
291
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100292 case RAN_CONN_E_MO_CLOSE:
293 case RAN_CONN_E_CN_CLOSE:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200294 log_close_event(fi, event, data);
295 /* fall through */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100296 case RAN_CONN_E_UNUSED:
297 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_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
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100305static void ran_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Harald Welte2483f1b2016-06-19 18:06:02 +0200306{
307 switch (event) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100308 case RAN_CONN_E_COMMUNICATING:
Harald Welte2483f1b2016-06-19 18:06:02 +0200309 /* no-op */
310 return;
311
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100312 case RAN_CONN_E_MO_CLOSE:
313 case RAN_CONN_E_CN_CLOSE:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200314 log_close_event(fi, event, data);
315 /* fall through */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100316 case RAN_CONN_E_UNUSED:
317 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_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 Hofmeyrc036b792018-11-29 22:37:51 +0100325static int ran_conn_fsm_timeout(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200326{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100327 struct ran_conn *conn = fi->priv;
328 if (ran_conn_in_release(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200329 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 Hofmeyrc036b792018-11-29 22:37:51 +0100333 osmo_fsm_inst_dispatch(fi, RAN_CONN_E_CN_CLOSE, &cause);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200334 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200335 return 0;
336}
337
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100338static void ran_conn_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
Harald Welte2483f1b2016-06-19 18:06:02 +0200339{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100340 struct ran_conn *conn = fi->priv;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200341
Harald Welte0df904d2018-12-03 11:00:04 +0100342 /* The SGs interface needs to access vsub struct members to send the
343 * release message, however the following release procedures will
344 * remove conn->vsub, so we need to send the release right now. */
345 if (conn->via_ran == OSMO_RAT_EUTRAN_SGS) {
346 sgs_iface_tx_release(conn);
347 }
348
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200349 /* Use count for either conn->a.waiting_for_clear_complete or
350 * conn->iu.waiting_for_release_complete. 'get' it early, so we don't deallocate after tearing
351 * down active transactions. Safeguard against double-get (though it shouldn't happen). */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100352 if (!ran_conn_used_by(conn, RAN_CONN_USE_RELEASE))
353 ran_conn_get(conn, RAN_CONN_USE_RELEASE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200354
355 /* Cancel pending CM Service Requests */
356 if (conn->received_cm_service_request) {
357 conn->received_cm_service_request = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100358 ran_conn_put(conn, RAN_CONN_USE_CM_SERVICE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200359 }
360
361 /* Cancel all VLR FSMs, if any */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200362 vlr_subscr_cancel_attach_fsm(conn->vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200363
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200364 if (conn->vsub) {
365 /* The subscriber has no active connection anymore.
366 * Restart the periodic Location Update expiry timer for this subscriber. */
367 vlr_subscr_enable_expire_lu(conn->vsub);
368 }
369
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200370 /* If we're closing in a middle of a trans, we need to clean up */
371 trans_conn_closed(conn);
372
373 switch (conn->via_ran) {
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100374 case OSMO_RAT_GERAN_A:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200375 a_iface_tx_clear_cmd(conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200376 if (conn->a.waiting_for_clear_complete) {
377 LOGPFSML(fi, LOGL_ERROR,
378 "Unexpected: conn is already waiting for BSSMAP Clear Complete\n");
379 break;
380 }
381 conn->a.waiting_for_clear_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200382 break;
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100383 case OSMO_RAT_UTRAN_IU:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200384 ranap_iu_tx_release(conn->iu.ue_ctx, NULL);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200385 if (conn->iu.waiting_for_release_complete) {
386 LOGPFSML(fi, LOGL_ERROR,
387 "Unexpected: conn is already waiting for Iu Release Complete\n");
388 break;
389 }
390 conn->iu.waiting_for_release_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200391 break;
Harald Welte0df904d2018-12-03 11:00:04 +0100392 case OSMO_RAT_EUTRAN_SGS:
393 /* Release message is already sent at the beginning of this
394 * functions (see above), but we still need to notify the
395 * conn that a release has been sent / is in progress. */
396 ran_conn_sgs_release_sent(conn);
397 break;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200398 default:
399 LOGP(DMM, LOGL_ERROR, "%s: Unknown RAN type, cannot tx release/clear\n",
400 vlr_subscr_name(conn->vsub));
401 break;
402 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200403}
404
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100405static void ran_conn_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200406{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100407 OSMO_ASSERT(event == RAN_CONN_E_UNUSED);
408 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASED, 0, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200409}
410
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100411static void ran_conn_fsm_released(struct osmo_fsm_inst *fi, uint32_t prev_state)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200412{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100413 /* Terminate, deallocate and also deallocate the ran_conn, which is allocated as
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200414 * a talloc child of fi. Also calls the cleanup function. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200415 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200416}
417
418#define S(x) (1 << (x))
419
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100420static const struct osmo_fsm_state ran_conn_fsm_states[] = {
421 [RAN_CONN_S_NEW] = {
422 .name = OSMO_STRINGIFY(RAN_CONN_S_NEW),
423 .in_event_mask = S(RAN_CONN_E_COMPLETE_LAYER_3) |
424 S(RAN_CONN_E_ACCEPTED) |
425 S(RAN_CONN_E_MO_CLOSE) |
426 S(RAN_CONN_E_CN_CLOSE) |
427 S(RAN_CONN_E_UNUSED),
428 .out_state_mask = S(RAN_CONN_S_AUTH_CIPH) |
429 S(RAN_CONN_S_ACCEPTED) |
430 S(RAN_CONN_S_RELEASING),
431 .action = ran_conn_fsm_new,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200432 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100433 [RAN_CONN_S_AUTH_CIPH] = {
434 .name = OSMO_STRINGIFY(RAN_CONN_S_AUTH_CIPH),
435 .in_event_mask = S(RAN_CONN_E_ACCEPTED) |
436 S(RAN_CONN_E_MO_CLOSE) |
437 S(RAN_CONN_E_CN_CLOSE) |
438 S(RAN_CONN_E_UNUSED),
439 .out_state_mask = S(RAN_CONN_S_WAIT_CLASSMARK_UPDATE) |
440 S(RAN_CONN_S_ACCEPTED) |
441 S(RAN_CONN_S_RELEASING),
442 .action = ran_conn_fsm_auth_ciph,
Harald Welteb8b85a12016-06-17 00:06:42 +0200443 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100444 [RAN_CONN_S_WAIT_CLASSMARK_UPDATE] = {
445 .name = OSMO_STRINGIFY(RAN_CONN_S_WAIT_CLASSMARK_UPDATE),
446 .in_event_mask = S(RAN_CONN_E_CLASSMARK_UPDATE) |
447 S(RAN_CONN_E_MO_CLOSE) |
448 S(RAN_CONN_E_CN_CLOSE) |
449 S(RAN_CONN_E_UNUSED),
450 .out_state_mask = S(RAN_CONN_S_AUTH_CIPH) |
451 S(RAN_CONN_S_RELEASING),
452 .action = ran_conn_fsm_wait_classmark_update,
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200453 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100454 [RAN_CONN_S_ACCEPTED] = {
455 .name = OSMO_STRINGIFY(RAN_CONN_S_ACCEPTED),
Harald Welteb8b85a12016-06-17 00:06:42 +0200456 /* allow everything to release for any odd behavior */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100457 .in_event_mask = S(RAN_CONN_E_COMPLETE_LAYER_3) |
458 S(RAN_CONN_E_COMMUNICATING) |
Vadim Yanitskiyce9e1f62019-01-25 19:37:59 +0700459 S(RAN_CONN_E_RELEASE_WHEN_UNUSED) |
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100460 S(RAN_CONN_E_ACCEPTED) |
461 S(RAN_CONN_E_MO_CLOSE) |
462 S(RAN_CONN_E_CN_CLOSE) |
463 S(RAN_CONN_E_UNUSED),
464 .out_state_mask = S(RAN_CONN_S_RELEASING) |
465 S(RAN_CONN_S_COMMUNICATING),
466 .onenter = ran_conn_fsm_accepted_enter,
467 .action = ran_conn_fsm_accepted,
Harald Welteb8b85a12016-06-17 00:06:42 +0200468 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100469 [RAN_CONN_S_COMMUNICATING] = {
470 .name = OSMO_STRINGIFY(RAN_CONN_S_COMMUNICATING),
Harald Welte2483f1b2016-06-19 18:06:02 +0200471 /* allow everything to release for any odd behavior */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100472 .in_event_mask = S(RAN_CONN_E_RELEASE_WHEN_UNUSED) |
473 S(RAN_CONN_E_ACCEPTED) |
474 S(RAN_CONN_E_COMMUNICATING) |
475 S(RAN_CONN_E_MO_CLOSE) |
476 S(RAN_CONN_E_CN_CLOSE) |
477 S(RAN_CONN_E_UNUSED),
478 .out_state_mask = S(RAN_CONN_S_RELEASING),
479 .action = ran_conn_fsm_communicating,
Harald Welte2483f1b2016-06-19 18:06:02 +0200480 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100481 [RAN_CONN_S_RELEASING] = {
482 .name = OSMO_STRINGIFY(RAN_CONN_S_RELEASING),
483 .in_event_mask = S(RAN_CONN_E_UNUSED),
484 .out_state_mask = S(RAN_CONN_S_RELEASED),
485 .onenter = ran_conn_fsm_releasing_onenter,
486 .action = ran_conn_fsm_releasing,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200487 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100488 [RAN_CONN_S_RELEASED] = {
489 .name = OSMO_STRINGIFY(RAN_CONN_S_RELEASED),
490 .onenter = ran_conn_fsm_released,
Harald Welteb8b85a12016-06-17 00:06:42 +0200491 },
492};
493
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100494static void ran_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200495
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100496static struct osmo_fsm ran_conn_fsm = {
497 .name = "RAN_conn",
498 .states = ran_conn_fsm_states,
499 .num_states = ARRAY_SIZE(ran_conn_fsm_states),
Harald Welteb8b85a12016-06-17 00:06:42 +0200500 .allstate_event_mask = 0,
501 .allstate_action = NULL,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200502 .log_subsys = DMM,
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100503 .event_names = ran_conn_fsm_event_names,
504 .cleanup = ran_conn_fsm_cleanup,
505 .timer_cb = ran_conn_fsm_timeout,
Harald Welteb8b85a12016-06-17 00:06:42 +0200506};
507
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100508/* Return statically allocated string of the ran_conn RAT type and id. */
509const char *ran_conn_get_conn_id(struct ran_conn *conn)
Daniel Willmann4e825b62018-02-15 10:33:26 +0100510{
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100511 static char id[42];
512 int rc;
513 uint32_t conn_id;
Daniel Willmann4e825b62018-02-15 10:33:26 +0100514
515 switch (conn->via_ran) {
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100516 case OSMO_RAT_GERAN_A:
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100517 conn_id = conn->a.conn_id;
Daniel Willmann4e825b62018-02-15 10:33:26 +0100518 break;
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100519 case OSMO_RAT_UTRAN_IU:
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100520 conn_id = iu_get_conn_id(conn->iu.ue_ctx);
Daniel Willmann4e825b62018-02-15 10:33:26 +0100521 break;
522 default:
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100523 return "ran-unknown";
Daniel Willmann4e825b62018-02-15 10:33:26 +0100524 }
525
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100526 rc = snprintf(id, sizeof(id), "%s-%u", osmo_rat_type_name(conn->via_ran), conn_id);
527 /* < 0 is error, == 0 is empty, >= size means truncation. Not really expecting this to catch on in any practical
528 * situation. */
529 if (rc <= 0 || rc >= sizeof(id)) {
530 LOGP(DMM, LOGL_ERROR, "Error with conn id; rc=%d\n", rc);
531 return "conn-id-error";
532 }
Daniel Willmann4e825b62018-02-15 10:33:26 +0100533 return id;
534}
535
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200536/* Tidy up before the FSM deallocates */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100537static void ran_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200538{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100539 struct ran_conn *conn = fi->priv;
Harald Welteb8b85a12016-06-17 00:06:42 +0200540
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100541 if (ran_conn_fsm_has_active_transactions(fi))
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200542 LOGPFSML(fi, LOGL_ERROR, "Deallocating despite active transactions\n");
543
544 if (!conn) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100545 LOGP(DRLL, LOGL_ERROR, "Freeing NULL RAN connection\n");
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200546 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200547 }
548
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200549 if (conn->vsub) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100550 DEBUGP(DRLL, "%s: Freeing RAN connection\n", vlr_subscr_name(conn->vsub));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200551 conn->vsub->lu_fsm = NULL;
552 conn->vsub->msc_conn_ref = NULL;
553 vlr_subscr_put(conn->vsub);
554 conn->vsub = NULL;
555 } else
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100556 DEBUGP(DRLL, "Freeing RAN connection with NULL subscriber\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200557
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200558 llist_del(&conn->entry);
559}
560
561/* Signal success of Complete Layer 3. Allow to keep the conn open for Auth and Ciph. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100562void ran_conn_complete_layer_3(struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200563{
564 if (!conn)
565 return;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100566 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_COMPLETE_LAYER_3, NULL);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200567}
568
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100569void ran_conn_release_when_unused(struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200570{
571 if (!conn)
572 return;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100573 if (ran_conn_in_release(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200574 DEBUGP(DMM, "%s: %s: conn already in release (%s)\n",
575 vlr_subscr_name(conn->vsub), __func__,
576 osmo_fsm_inst_state_name(conn->fi));
577 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200578 }
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100579 if (conn->fi->state == RAN_CONN_S_NEW) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200580 DEBUGP(DMM, "%s: %s: conn still being established (%s)\n",
581 vlr_subscr_name(conn->vsub), __func__,
582 osmo_fsm_inst_state_name(conn->fi));
583 return;
584 }
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100585 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_RELEASE_WHEN_UNUSED, NULL);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200586}
587
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100588static void conn_close(struct ran_conn *conn, uint32_t cause, uint32_t event)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200589{
590 if (!conn) {
591 LOGP(DMM, LOGL_ERROR, "Cannot release NULL connection\n");
592 return;
593 }
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100594 if (ran_conn_in_release(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200595 DEBUGP(DMM, "%s(vsub=%s, cause=%u): already in release, ignore.\n",
596 __func__, vlr_subscr_name(conn->vsub), cause);
597 return;
598 }
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200599 osmo_fsm_inst_dispatch(conn->fi, event, &cause);
600}
601
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100602void ran_conn_close(struct ran_conn *conn, uint32_t cause)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200603{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100604 return conn_close(conn, cause, RAN_CONN_E_CN_CLOSE);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200605}
606
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100607void ran_conn_mo_close(struct ran_conn *conn, uint32_t cause)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200608{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100609 return conn_close(conn, cause, RAN_CONN_E_MO_CLOSE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200610}
611
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100612bool ran_conn_in_release(struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200613{
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200614 if (!conn || !conn->fi)
615 return true;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100616 if (conn->fi->state == RAN_CONN_S_RELEASING)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200617 return true;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100618 if (conn->fi->state == RAN_CONN_S_RELEASED)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200619 return true;
620 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200621}
622
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100623bool ran_conn_is_accepted(const struct ran_conn *conn)
Harald Welteb8b85a12016-06-17 00:06:42 +0200624{
625 if (!conn)
626 return false;
Harald Welte2483f1b2016-06-19 18:06:02 +0200627 if (!conn->vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200628 return false;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100629 if (!(conn->fi->state == RAN_CONN_S_ACCEPTED
630 || conn->fi->state == RAN_CONN_S_COMMUNICATING))
Harald Welteb8b85a12016-06-17 00:06:42 +0200631 return false;
632 return true;
633}
634
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200635/* Indicate that *some* communication is happening with the phone, so that the conn FSM no longer times
636 * out to release within a few seconds. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100637void ran_conn_communicating(struct ran_conn *conn)
Harald Welte2483f1b2016-06-19 18:06:02 +0200638{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100639 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_COMMUNICATING, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200640}
641
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100642void ran_conn_init(void)
Harald Welteb8b85a12016-06-17 00:06:42 +0200643{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100644 osmo_fsm_register(&ran_conn_fsm);
Harald Welteb8b85a12016-06-17 00:06:42 +0200645}
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200646
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100647/* Allocate a new RAN conn and FSM.
648 * Deallocation is by ran_conn_put(): when the use count reaches zero, the
649 * RAN_CONN_E_RELEASE_COMPLETE event is dispatched, the FSM terminates and deallocates both FSM and
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200650 * conn. As long as the FSM is waiting for responses from the subscriber, it will itself hold a use count
651 * on the conn. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100652struct ran_conn *ran_conn_alloc(struct gsm_network *network,
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100653 enum osmo_rat_type via_ran, uint16_t lac)
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200654{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100655 struct ran_conn *conn;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200656 struct osmo_fsm_inst *fi;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200657
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100658 fi = osmo_fsm_inst_alloc(&ran_conn_fsm, network, NULL, LOGL_DEBUG, NULL);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200659 if (!fi) {
660 LOGP(DMM, LOGL_ERROR, "Failed to allocate conn FSM\n");
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200661 return NULL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200662 }
663
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100664 conn = talloc_zero(fi, struct ran_conn);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200665 if (!conn) {
666 osmo_fsm_inst_free(fi);
667 return NULL;
668 }
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200669
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100670 *conn = (struct ran_conn){
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200671 .network = network,
672 .via_ran = via_ran,
673 .lac = lac,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200674 .fi = fi,
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200675 };
676
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100677 switch (via_ran) {
678 case OSMO_RAT_GERAN_A:
679 conn->log_subsys = DBSSAP;
680 break;
681 case OSMO_RAT_UTRAN_IU:
682 conn->log_subsys = DRANAP;
683 break;
Harald Welte0df904d2018-12-03 11:00:04 +0100684 case OSMO_RAT_EUTRAN_SGS:
685 conn->log_subsys = DSGS;
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100686 default:
687 conn->log_subsys = DMSC;
688 break;
689 }
690
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200691 fi->priv = conn;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100692 llist_add_tail(&conn->entry, &network->ran_conns);
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200693 return conn;
694}
695
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100696bool ran_conn_is_establishing_auth_ciph(const struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200697{
698 if (!conn)
699 return false;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100700 return conn->fi->state == RAN_CONN_S_AUTH_CIPH;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200701}
702
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200703
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200704const struct value_string complete_layer3_type_names[] = {
705 { COMPLETE_LAYER3_NONE, "NONE" },
706 { COMPLETE_LAYER3_LU, "LU" },
707 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
708 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
709 { 0, NULL }
710};
711
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100712static void _ran_conn_update_id(struct ran_conn *conn, const char *subscr_identity)
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200713{
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100714 struct vlr_subscr *vsub = conn->vsub;
715
716 if (osmo_fsm_inst_update_id_f(conn->fi, "%s:%s:%s",
717 subscr_identity,
718 ran_conn_get_conn_id(conn),
719 complete_layer3_type_name(conn->complete_layer3_type))
720 != 0)
721 return; /* osmo_fsm_inst_update_id_f() will log an error. */
722
723 if (vsub) {
724 if (vsub->lu_fsm)
725 osmo_fsm_inst_update_id(vsub->lu_fsm, conn->fi->id);
726 if (vsub->auth_fsm)
727 osmo_fsm_inst_update_id(vsub->auth_fsm, conn->fi->id);
728 if (vsub->proc_arq_fsm)
729 osmo_fsm_inst_update_id(vsub->proc_arq_fsm, conn->fi->id);
730 }
731
732 LOGPFSML(conn->fi, LOGL_DEBUG, "Updated ID\n");
733}
734
735/* Compose an ID almost like gsm48_mi_to_string(), but print the MI type along, and print a TMSI as hex. */
736void ran_conn_update_id_from_mi(struct ran_conn *conn, const uint8_t *mi, uint8_t mi_len)
737{
738 _ran_conn_update_id(conn, osmo_mi_name(mi, mi_len));
739}
740
741/* Update ran_conn->fi id string from current conn->vsub and conn->complete_layer3_type. */
742void ran_conn_update_id(struct ran_conn *conn)
743{
744 _ran_conn_update_id(conn, vlr_subscr_name(conn->vsub));
745}
746
747/* Iterate all ran_conn instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
748 * instances. */
749void ran_conn_update_id_for_vsub(struct vlr_subscr *for_vsub)
750{
751 struct gsm_network *network;
752 struct ran_conn *conn;
753 if (!for_vsub)
754 return;
755
756 network = for_vsub->vlr->user_ctx;
757 OSMO_ASSERT(network);
758
759 llist_for_each_entry(conn, &network->ran_conns, entry) {
760 if (conn->vsub == for_vsub)
761 ran_conn_update_id(conn);
762 }
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200763}
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200764
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100765static void rx_close_complete(struct ran_conn *conn, const char *label, bool *flag)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200766{
767 if (!conn)
768 return;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100769 if (!ran_conn_in_release(conn)) {
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200770 LOGPFSML(conn->fi, LOGL_ERROR, "Received unexpected %s, discarding right now\n",
771 label);
772 trans_conn_closed(conn);
773 osmo_fsm_inst_term(conn->fi, OSMO_FSM_TERM_ERROR, NULL);
774 return;
775 }
776 if (*flag) {
777 *flag = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100778 ran_conn_put(conn, RAN_CONN_USE_RELEASE);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200779 }
780}
781
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100782void ran_conn_rx_bssmap_clear_complete(struct ran_conn *conn)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200783{
784 rx_close_complete(conn, "BSSMAP Clear Complete", &conn->a.waiting_for_clear_complete);
785}
786
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100787void ran_conn_rx_iu_release_complete(struct ran_conn *conn)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200788{
789 rx_close_complete(conn, "Iu Release Complete", &conn->iu.waiting_for_release_complete);
790}
Harald Welte0df904d2018-12-03 11:00:04 +0100791
792void ran_conn_sgs_release_sent(struct ran_conn *conn)
793{
794 bool dummy_waiting_for_release_complete = true;
795
796 /* Note: In SGsAP there is no confirmation of a release. */
797 rx_close_complete(conn, "SGs Release Complete", &dummy_waiting_for_release_complete);
798}
Vadim Yanitskiy114bad82019-01-25 19:42:52 +0700799
800const struct value_string ran_conn_use_names[] = {
801 { RAN_CONN_USE_UNTRACKED, "UNTRACKED" },
802 { RAN_CONN_USE_COMPL_L3, "compl_l3" },
803 { RAN_CONN_USE_DTAP, "dtap" },
804 { RAN_CONN_USE_AUTH_CIPH, "auth+ciph" },
805 { RAN_CONN_USE_CM_SERVICE, "cm_service" },
806 { RAN_CONN_USE_TRANS_CC, "trans_cc" },
807 { RAN_CONN_USE_TRANS_SMS, "trans_sms" },
808 { RAN_CONN_USE_TRANS_NC_SS, "trans_nc_ss" },
809 { RAN_CONN_USE_SILENT_CALL, "silent_call" },
810 { RAN_CONN_USE_RELEASE, "release" },
811 { 0, NULL }
812};
813
814static const char *used_ref_counts_str(struct ran_conn *conn)
815{
816 static char buf[256];
817 int bit_nr;
818 char *pos = buf;
819 *pos = '\0';
820
821 if (conn->use_tokens < 0)
822 return "invalid";
823
824#define APPEND_STR(fmt, args...) do { \
825 int remain = sizeof(buf) - (pos - buf) - 1; \
826 int l = -1; \
827 if (remain > 0) \
828 l = snprintf(pos, remain, "%s" fmt, (pos == buf? "" : ","), ##args); \
829 if (l < 0 || l > remain) { \
830 buf[sizeof(buf) - 1] = '\0'; \
831 return buf; \
832 } \
833 pos += l; \
834 } while(0)
835
836 for (bit_nr = 0; (1 << bit_nr) <= conn->use_tokens; bit_nr++) {
837 if (conn->use_tokens & (1 << bit_nr)) {
838 APPEND_STR("%s", get_value_string(ran_conn_use_names, bit_nr));
839 }
840 }
841 return buf;
842#undef APPEND_STR
843}
844
845/* increment the ref-count. Needs to be called by every user */
846struct ran_conn *_ran_conn_get(struct ran_conn *conn, enum ran_conn_use balance_token,
847 const char *file, int line)
848{
849 OSMO_ASSERT(conn);
850
851 if (balance_token != RAN_CONN_USE_UNTRACKED) {
852 uint32_t flag = 1 << balance_token;
853 OSMO_ASSERT(balance_token < 32);
854 if (conn->use_tokens & flag)
855 LOGPSRC(DREF, LOGL_ERROR, file, line,
856 "%s: MSC conn use error: using an already used token: %s\n",
857 vlr_subscr_name(conn->vsub),
858 ran_conn_use_name(balance_token));
859 conn->use_tokens |= flag;
860 }
861
862 conn->use_count++;
863 LOGPSRC(DREF, LOGL_DEBUG, file, line,
864 "%s: MSC conn use + %s == %u (0x%x: %s)\n",
865 vlr_subscr_name(conn->vsub), ran_conn_use_name(balance_token),
866 conn->use_count, conn->use_tokens, used_ref_counts_str(conn));
867
868 return conn;
869}
870
871/* decrement the ref-count. Once it reaches zero, we release */
872void _ran_conn_put(struct ran_conn *conn, enum ran_conn_use balance_token,
873 const char *file, int line)
874{
875 OSMO_ASSERT(conn);
876
877 if (balance_token != RAN_CONN_USE_UNTRACKED) {
878 uint32_t flag = 1 << balance_token;
879 OSMO_ASSERT(balance_token < 32);
880 if (!(conn->use_tokens & flag))
881 LOGPSRC(DREF, LOGL_ERROR, file, line,
882 "%s: MSC conn use error: freeing an unused token: %s\n",
883 vlr_subscr_name(conn->vsub),
884 ran_conn_use_name(balance_token));
885 conn->use_tokens &= ~flag;
886 }
887
888 if (conn->use_count == 0) {
889 LOGPSRC(DREF, LOGL_ERROR, file, line,
890 "%s: MSC conn use - %s failed: is already 0\n",
891 vlr_subscr_name(conn->vsub),
892 ran_conn_use_name(balance_token));
893 return;
894 }
895
896 conn->use_count--;
897 LOGPSRC(DREF, LOGL_DEBUG, file, line,
898 "%s: MSC conn use - %s == %u (0x%x: %s)\n",
899 vlr_subscr_name(conn->vsub), ran_conn_use_name(balance_token),
900 conn->use_count, conn->use_tokens, used_ref_counts_str(conn));
901
902 if (conn->use_count == 0)
903 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_UNUSED, NULL);
904}
905
906bool ran_conn_used_by(struct ran_conn *conn, enum ran_conn_use token)
907{
908 return conn && (conn->use_tokens & (1 << token));
909}