blob: e54e542b9966bea76df35f4f5ec92d5f2891bb29 [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 Hofmeyr8e2c6a32019-03-07 03:06:16 +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");
Neels Hofmeyr8e2c6a32019-03-07 03:06:16 +0100543 trans_conn_closed(conn);
544 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200545
546 if (!conn) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100547 LOGP(DRLL, LOGL_ERROR, "Freeing NULL RAN connection\n");
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200548 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200549 }
550
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200551 if (conn->vsub) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100552 DEBUGP(DRLL, "%s: Freeing RAN connection\n", vlr_subscr_name(conn->vsub));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200553 conn->vsub->lu_fsm = NULL;
554 conn->vsub->msc_conn_ref = NULL;
555 vlr_subscr_put(conn->vsub);
556 conn->vsub = NULL;
557 } else
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100558 DEBUGP(DRLL, "Freeing RAN connection with NULL subscriber\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200559
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200560 llist_del(&conn->entry);
561}
562
563/* Signal success of Complete Layer 3. Allow to keep the conn open for Auth and Ciph. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100564void ran_conn_complete_layer_3(struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200565{
566 if (!conn)
567 return;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100568 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_COMPLETE_LAYER_3, NULL);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200569}
570
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100571void ran_conn_release_when_unused(struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200572{
573 if (!conn)
574 return;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100575 if (ran_conn_in_release(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200576 DEBUGP(DMM, "%s: %s: conn already in release (%s)\n",
577 vlr_subscr_name(conn->vsub), __func__,
578 osmo_fsm_inst_state_name(conn->fi));
579 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200580 }
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100581 if (conn->fi->state == RAN_CONN_S_NEW) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200582 DEBUGP(DMM, "%s: %s: conn still being established (%s)\n",
583 vlr_subscr_name(conn->vsub), __func__,
584 osmo_fsm_inst_state_name(conn->fi));
585 return;
586 }
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100587 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_RELEASE_WHEN_UNUSED, NULL);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200588}
589
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100590static void conn_close(struct ran_conn *conn, uint32_t cause, uint32_t event)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200591{
592 if (!conn) {
593 LOGP(DMM, LOGL_ERROR, "Cannot release NULL connection\n");
594 return;
595 }
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100596 if (ran_conn_in_release(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200597 DEBUGP(DMM, "%s(vsub=%s, cause=%u): already in release, ignore.\n",
598 __func__, vlr_subscr_name(conn->vsub), cause);
599 return;
600 }
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200601 osmo_fsm_inst_dispatch(conn->fi, event, &cause);
602}
603
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100604void ran_conn_close(struct ran_conn *conn, uint32_t cause)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200605{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100606 return conn_close(conn, cause, RAN_CONN_E_CN_CLOSE);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200607}
608
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100609void ran_conn_mo_close(struct ran_conn *conn, uint32_t cause)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200610{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100611 return conn_close(conn, cause, RAN_CONN_E_MO_CLOSE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200612}
613
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100614bool ran_conn_in_release(struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200615{
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200616 if (!conn || !conn->fi)
617 return true;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100618 if (conn->fi->state == RAN_CONN_S_RELEASING)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200619 return true;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100620 if (conn->fi->state == RAN_CONN_S_RELEASED)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200621 return true;
622 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200623}
624
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100625bool ran_conn_is_accepted(const struct ran_conn *conn)
Harald Welteb8b85a12016-06-17 00:06:42 +0200626{
627 if (!conn)
628 return false;
Harald Welte2483f1b2016-06-19 18:06:02 +0200629 if (!conn->vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200630 return false;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100631 if (!(conn->fi->state == RAN_CONN_S_ACCEPTED
632 || conn->fi->state == RAN_CONN_S_COMMUNICATING))
Harald Welteb8b85a12016-06-17 00:06:42 +0200633 return false;
634 return true;
635}
636
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200637/* Indicate that *some* communication is happening with the phone, so that the conn FSM no longer times
638 * out to release within a few seconds. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100639void ran_conn_communicating(struct ran_conn *conn)
Harald Welte2483f1b2016-06-19 18:06:02 +0200640{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100641 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_COMMUNICATING, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200642}
643
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100644void ran_conn_init(void)
Harald Welteb8b85a12016-06-17 00:06:42 +0200645{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100646 osmo_fsm_register(&ran_conn_fsm);
Harald Welteb8b85a12016-06-17 00:06:42 +0200647}
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200648
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100649/* Allocate a new RAN conn and FSM.
650 * Deallocation is by ran_conn_put(): when the use count reaches zero, the
651 * RAN_CONN_E_RELEASE_COMPLETE event is dispatched, the FSM terminates and deallocates both FSM and
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200652 * conn. As long as the FSM is waiting for responses from the subscriber, it will itself hold a use count
653 * on the conn. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100654struct ran_conn *ran_conn_alloc(struct gsm_network *network,
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100655 enum osmo_rat_type via_ran, uint16_t lac)
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200656{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100657 struct ran_conn *conn;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200658 struct osmo_fsm_inst *fi;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200659
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100660 fi = osmo_fsm_inst_alloc(&ran_conn_fsm, network, NULL, LOGL_DEBUG, NULL);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200661 if (!fi) {
662 LOGP(DMM, LOGL_ERROR, "Failed to allocate conn FSM\n");
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200663 return NULL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200664 }
665
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100666 conn = talloc_zero(fi, struct ran_conn);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200667 if (!conn) {
668 osmo_fsm_inst_free(fi);
669 return NULL;
670 }
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200671
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100672 *conn = (struct ran_conn){
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200673 .network = network,
674 .via_ran = via_ran,
675 .lac = lac,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200676 .fi = fi,
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200677 };
678
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100679 switch (via_ran) {
680 case OSMO_RAT_GERAN_A:
681 conn->log_subsys = DBSSAP;
682 break;
683 case OSMO_RAT_UTRAN_IU:
684 conn->log_subsys = DRANAP;
685 break;
Harald Welte0df904d2018-12-03 11:00:04 +0100686 case OSMO_RAT_EUTRAN_SGS:
687 conn->log_subsys = DSGS;
Vadim Yanitskiy1396e1c2019-02-23 15:51:23 +0700688 break;
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100689 default:
690 conn->log_subsys = DMSC;
691 break;
692 }
693
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200694 fi->priv = conn;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100695 llist_add_tail(&conn->entry, &network->ran_conns);
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200696 return conn;
697}
698
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100699bool ran_conn_is_establishing_auth_ciph(const struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200700{
701 if (!conn)
702 return false;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100703 return conn->fi->state == RAN_CONN_S_AUTH_CIPH;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200704}
705
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200706
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200707const struct value_string complete_layer3_type_names[] = {
708 { COMPLETE_LAYER3_NONE, "NONE" },
709 { COMPLETE_LAYER3_LU, "LU" },
710 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
711 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
712 { 0, NULL }
713};
714
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100715static void _ran_conn_update_id(struct ran_conn *conn, const char *subscr_identity)
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200716{
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100717 struct vlr_subscr *vsub = conn->vsub;
718
719 if (osmo_fsm_inst_update_id_f(conn->fi, "%s:%s:%s",
720 subscr_identity,
721 ran_conn_get_conn_id(conn),
722 complete_layer3_type_name(conn->complete_layer3_type))
723 != 0)
724 return; /* osmo_fsm_inst_update_id_f() will log an error. */
725
726 if (vsub) {
727 if (vsub->lu_fsm)
728 osmo_fsm_inst_update_id(vsub->lu_fsm, conn->fi->id);
729 if (vsub->auth_fsm)
730 osmo_fsm_inst_update_id(vsub->auth_fsm, conn->fi->id);
731 if (vsub->proc_arq_fsm)
732 osmo_fsm_inst_update_id(vsub->proc_arq_fsm, conn->fi->id);
733 }
734
735 LOGPFSML(conn->fi, LOGL_DEBUG, "Updated ID\n");
736}
737
738/* Compose an ID almost like gsm48_mi_to_string(), but print the MI type along, and print a TMSI as hex. */
739void ran_conn_update_id_from_mi(struct ran_conn *conn, const uint8_t *mi, uint8_t mi_len)
740{
741 _ran_conn_update_id(conn, osmo_mi_name(mi, mi_len));
742}
743
744/* Update ran_conn->fi id string from current conn->vsub and conn->complete_layer3_type. */
745void ran_conn_update_id(struct ran_conn *conn)
746{
747 _ran_conn_update_id(conn, vlr_subscr_name(conn->vsub));
748}
749
750/* Iterate all ran_conn instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
751 * instances. */
752void ran_conn_update_id_for_vsub(struct vlr_subscr *for_vsub)
753{
754 struct gsm_network *network;
755 struct ran_conn *conn;
756 if (!for_vsub)
757 return;
758
759 network = for_vsub->vlr->user_ctx;
760 OSMO_ASSERT(network);
761
762 llist_for_each_entry(conn, &network->ran_conns, entry) {
763 if (conn->vsub == for_vsub)
764 ran_conn_update_id(conn);
765 }
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200766}
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200767
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100768static void rx_close_complete(struct ran_conn *conn, const char *label, bool *flag)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200769{
770 if (!conn)
771 return;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100772 if (!ran_conn_in_release(conn)) {
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200773 LOGPFSML(conn->fi, LOGL_ERROR, "Received unexpected %s, discarding right now\n",
774 label);
775 trans_conn_closed(conn);
776 osmo_fsm_inst_term(conn->fi, OSMO_FSM_TERM_ERROR, NULL);
777 return;
778 }
779 if (*flag) {
780 *flag = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100781 ran_conn_put(conn, RAN_CONN_USE_RELEASE);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200782 }
783}
784
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100785void ran_conn_rx_bssmap_clear_complete(struct ran_conn *conn)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200786{
787 rx_close_complete(conn, "BSSMAP Clear Complete", &conn->a.waiting_for_clear_complete);
788}
789
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100790void ran_conn_rx_iu_release_complete(struct ran_conn *conn)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200791{
792 rx_close_complete(conn, "Iu Release Complete", &conn->iu.waiting_for_release_complete);
793}
Harald Welte0df904d2018-12-03 11:00:04 +0100794
795void ran_conn_sgs_release_sent(struct ran_conn *conn)
796{
797 bool dummy_waiting_for_release_complete = true;
798
799 /* Note: In SGsAP there is no confirmation of a release. */
800 rx_close_complete(conn, "SGs Release Complete", &dummy_waiting_for_release_complete);
801}
Vadim Yanitskiy114bad82019-01-25 19:42:52 +0700802
803const struct value_string ran_conn_use_names[] = {
804 { RAN_CONN_USE_UNTRACKED, "UNTRACKED" },
805 { RAN_CONN_USE_COMPL_L3, "compl_l3" },
806 { RAN_CONN_USE_DTAP, "dtap" },
807 { RAN_CONN_USE_AUTH_CIPH, "auth+ciph" },
808 { RAN_CONN_USE_CM_SERVICE, "cm_service" },
809 { RAN_CONN_USE_TRANS_CC, "trans_cc" },
810 { RAN_CONN_USE_TRANS_SMS, "trans_sms" },
811 { RAN_CONN_USE_TRANS_NC_SS, "trans_nc_ss" },
812 { RAN_CONN_USE_SILENT_CALL, "silent_call" },
813 { RAN_CONN_USE_RELEASE, "release" },
814 { 0, NULL }
815};
816
817static const char *used_ref_counts_str(struct ran_conn *conn)
818{
819 static char buf[256];
820 int bit_nr;
821 char *pos = buf;
822 *pos = '\0';
823
824 if (conn->use_tokens < 0)
825 return "invalid";
826
827#define APPEND_STR(fmt, args...) do { \
828 int remain = sizeof(buf) - (pos - buf) - 1; \
829 int l = -1; \
830 if (remain > 0) \
831 l = snprintf(pos, remain, "%s" fmt, (pos == buf? "" : ","), ##args); \
832 if (l < 0 || l > remain) { \
833 buf[sizeof(buf) - 1] = '\0'; \
834 return buf; \
835 } \
836 pos += l; \
837 } while(0)
838
839 for (bit_nr = 0; (1 << bit_nr) <= conn->use_tokens; bit_nr++) {
840 if (conn->use_tokens & (1 << bit_nr)) {
841 APPEND_STR("%s", get_value_string(ran_conn_use_names, bit_nr));
842 }
843 }
844 return buf;
845#undef APPEND_STR
846}
847
848/* increment the ref-count. Needs to be called by every user */
849struct ran_conn *_ran_conn_get(struct ran_conn *conn, enum ran_conn_use balance_token,
850 const char *file, int line)
851{
852 OSMO_ASSERT(conn);
853
854 if (balance_token != RAN_CONN_USE_UNTRACKED) {
855 uint32_t flag = 1 << balance_token;
856 OSMO_ASSERT(balance_token < 32);
857 if (conn->use_tokens & flag)
858 LOGPSRC(DREF, LOGL_ERROR, file, line,
859 "%s: MSC conn use error: using an already used token: %s\n",
860 vlr_subscr_name(conn->vsub),
861 ran_conn_use_name(balance_token));
862 conn->use_tokens |= flag;
863 }
864
865 conn->use_count++;
866 LOGPSRC(DREF, LOGL_DEBUG, file, line,
867 "%s: MSC conn use + %s == %u (0x%x: %s)\n",
868 vlr_subscr_name(conn->vsub), ran_conn_use_name(balance_token),
869 conn->use_count, conn->use_tokens, used_ref_counts_str(conn));
870
871 return conn;
872}
873
874/* decrement the ref-count. Once it reaches zero, we release */
875void _ran_conn_put(struct ran_conn *conn, enum ran_conn_use balance_token,
876 const char *file, int line)
877{
878 OSMO_ASSERT(conn);
879
880 if (balance_token != RAN_CONN_USE_UNTRACKED) {
881 uint32_t flag = 1 << balance_token;
882 OSMO_ASSERT(balance_token < 32);
883 if (!(conn->use_tokens & flag))
884 LOGPSRC(DREF, LOGL_ERROR, file, line,
885 "%s: MSC conn use error: freeing an unused token: %s\n",
886 vlr_subscr_name(conn->vsub),
887 ran_conn_use_name(balance_token));
888 conn->use_tokens &= ~flag;
889 }
890
891 if (conn->use_count == 0) {
892 LOGPSRC(DREF, LOGL_ERROR, file, line,
893 "%s: MSC conn use - %s failed: is already 0\n",
894 vlr_subscr_name(conn->vsub),
895 ran_conn_use_name(balance_token));
896 return;
897 }
898
899 conn->use_count--;
900 LOGPSRC(DREF, LOGL_DEBUG, file, line,
901 "%s: MSC conn use - %s == %u (0x%x: %s)\n",
902 vlr_subscr_name(conn->vsub), ran_conn_use_name(balance_token),
903 conn->use_count, conn->use_tokens, used_ref_counts_str(conn));
904
905 if (conn->use_count == 0)
906 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_UNUSED, NULL);
907}
908
909bool ran_conn_used_by(struct ran_conn *conn, enum ran_conn_use token)
910{
911 return conn && (conn->use_tokens & (1 << token));
912}