blob: 4eefa6df227d9048e40e1137af52793ccb0826c1 [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>
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
Neels Hofmeyrc036b792018-11-29 22:37:51 +010043#define RAN_CONN_TIMEOUT 5 /* seconds */
Harald Welteb8b85a12016-06-17 00:06:42 +020044
Neels Hofmeyrc036b792018-11-29 22:37:51 +010045static const struct value_string ran_conn_fsm_event_names[] = {
46 OSMO_VALUE_STRING(RAN_CONN_E_COMPLETE_LAYER_3),
47 OSMO_VALUE_STRING(RAN_CONN_E_CLASSMARK_UPDATE),
48 OSMO_VALUE_STRING(RAN_CONN_E_ACCEPTED),
49 OSMO_VALUE_STRING(RAN_CONN_E_COMMUNICATING),
50 OSMO_VALUE_STRING(RAN_CONN_E_RELEASE_WHEN_UNUSED),
51 OSMO_VALUE_STRING(RAN_CONN_E_MO_CLOSE),
52 OSMO_VALUE_STRING(RAN_CONN_E_CN_CLOSE),
53 OSMO_VALUE_STRING(RAN_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{
Neels Hofmeyrc036b792018-11-29 22:37:51 +010059 struct ran_conn *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{
Neels Hofmeyrc036b792018-11-29 22:37:51 +010083 struct ran_conn *conn = fi->priv;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020084
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;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +010097 ran_conn_get(conn, RAN_CONN_USE_CM_SERVICE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +020098 }
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
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100113static void ran_conn_fsm_new(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200114{
115 switch (event) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100116 case RAN_CONN_E_COMPLETE_LAYER_3:
117 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_AUTH_CIPH, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200118 return;
119
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100120 case RAN_CONN_E_ACCEPTED:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200121 evaluate_acceptance_outcome(fi, true);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100122 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_ACCEPTED, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200123 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200124
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100125 case RAN_CONN_E_MO_CLOSE:
126 case RAN_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 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100130 case RAN_CONN_E_UNUSED:
131 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200132 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 Hofmeyrc036b792018-11-29 22:37:51 +0100139static void ran_conn_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200140{
141 /* If accepted, transition the state, all other cases mean failure. */
142 switch (event) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100143 case RAN_CONN_E_ACCEPTED:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200144 evaluate_acceptance_outcome(fi, true);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100145 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_ACCEPTED, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200146 return;
147
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100148 case RAN_CONN_E_UNUSED:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200149 LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n",
150 osmo_fsm_event_name(fi->fsm, event));
151 return;
152
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100153 case RAN_CONN_E_MO_CLOSE:
154 case RAN_CONN_E_CN_CLOSE:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200155 log_close_event(fi, event, data);
156 evaluate_acceptance_outcome(fi, false);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100157 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200158 return;
159
160
161 default:
162 OSMO_ASSERT(false);
163 }
164}
165
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100166int ran_conn_classmark_request_then_cipher_mode_cmd(struct ran_conn *conn, bool umts_aka,
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200167 bool retrieve_imeisv)
168{
169 int rc;
170 conn->geran_set_cipher_mode.umts_aka = umts_aka;
171 conn->geran_set_cipher_mode.retrieve_imeisv = retrieve_imeisv;
172
173 rc = a_iface_tx_classmark_request(conn);
174 if (rc) {
175 LOGP(DMM, LOGL_ERROR, "%s: cannot send BSSMAP Classmark Request\n",
176 vlr_subscr_name(conn->vsub));
177 return -EIO;
178 }
179
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100180 osmo_fsm_inst_state_chg(conn->fi, RAN_CONN_S_WAIT_CLASSMARK_UPDATE, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200181 return 0;
182}
183
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100184static void ran_conn_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200185{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100186 struct ran_conn *conn = fi->priv;
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200187 switch (event) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100188 case RAN_CONN_E_CLASSMARK_UPDATE:
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200189 /* Theoretically, this event can be used for requesting Classmark in various situations.
190 * So far though, the only time we send a Classmark Request is during Ciphering. As soon
191 * as more such situations arise, we need to add state to indicate what action should
192 * follow after a Classmark Update is received (e.g.
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100193 * ran_conn_classmark_request_then_cipher_mode_cmd() sets an enum value to indicate that
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200194 * Ciphering should continue afterwards). But right now, it is accurate to always
195 * continue with Ciphering: */
196
197 /* During Ciphering, we needed Classmark information. The Classmark Update has come in,
198 * go back into the Set Ciphering Command procedure. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100199 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_AUTH_CIPH, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100200 if (ran_conn_geran_set_cipher_mode(conn, conn->geran_set_cipher_mode.umts_aka,
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200201 conn->geran_set_cipher_mode.retrieve_imeisv)) {
202 LOGPFSML(fi, LOGL_ERROR,
203 "Sending Cipher Mode Command failed, aborting attach\n");
204 vlr_subscr_cancel_attach_fsm(conn->vsub, OSMO_FSM_TERM_ERROR,
205 GSM48_REJECT_NETWORK_FAILURE);
206 }
207 return;
208
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100209 case RAN_CONN_E_UNUSED:
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200210 LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n",
211 osmo_fsm_event_name(fi->fsm, event));
212 return;
213
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100214 case RAN_CONN_E_MO_CLOSE:
215 case RAN_CONN_E_CN_CLOSE:
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200216 log_close_event(fi, event, data);
217 evaluate_acceptance_outcome(fi, false);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100218 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200219 return;
220
221 default:
222 OSMO_ASSERT(false);
223 }
224}
225
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100226static bool ran_conn_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200227{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100228 struct ran_conn *conn = fi->priv;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200229 struct gsm_trans *trans;
Harald Welteb8b85a12016-06-17 00:06:42 +0200230
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200231 if (conn->silent_call) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100232 LOGPFSML(fi, LOGL_DEBUG, "%s: silent call still active\n", __func__);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200233 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200234 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200235
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200236 if (conn->received_cm_service_request) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100237 LOGPFSML(fi, LOGL_DEBUG, "%s: still awaiting first request after a CM Service Request\n",
238 __func__);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200239 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200240 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200241
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200242 if (conn->vsub && !llist_empty(&conn->vsub->cs.requests)) {
243 struct subscr_request *sr;
244 if (!log_check_level(fi->fsm->log_subsys, LOGL_DEBUG)) {
245 llist_for_each_entry(sr, &conn->vsub->cs.requests, entry) {
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100246 LOGPFSML(fi, LOGL_DEBUG, "%s: still active: %s\n",
247 __func__, sr->label);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200248 }
249 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200250 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200251 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200252
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200253 if ((trans = trans_has_conn(conn))) {
254 LOGPFSML(fi, LOGL_DEBUG,
Neels Hofmeyre9e2f5c2018-03-15 13:26:43 +0100255 "%s: connection still has active transaction: %s\n",
256 __func__, gsm48_pdisc_name(trans->protocol));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200257 return true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200258 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200259
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200260 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200261}
262
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100263static void ran_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
Harald Welte2483f1b2016-06-19 18:06:02 +0200264{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100265 struct ran_conn *conn = fi->priv;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200266
267 /* Stop Location Update expiry for this subscriber. While the subscriber
268 * has an open connection the LU expiry timer must remain disabled.
269 * Otherwise we would kick the subscriber off the network when the timer
270 * expires e.g. during a long phone call.
271 * The LU expiry timer will restart once the connection is closed. */
272 conn->vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
273
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100274 if (!ran_conn_fsm_has_active_transactions(fi))
275 osmo_fsm_inst_dispatch(fi, RAN_CONN_E_UNUSED, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200276}
277
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100278static void ran_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Harald Welteb8b85a12016-06-17 00:06:42 +0200279{
280 switch (event) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100281 case RAN_CONN_E_COMPLETE_LAYER_3:
Neels Hofmeyr36115c92018-08-23 15:52:43 +0200282 /* When Authentication is off, we may already be in the Accepted state when the code
283 * evaluates the Compl L3. Simply ignore. This just cosmetically mutes the error log
284 * about the useless event. */
285 return;
286
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100287 case RAN_CONN_E_COMMUNICATING:
288 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_COMMUNICATING, 0, 0);
Harald Welte2483f1b2016-06-19 18:06:02 +0200289 return;
290
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100291 case RAN_CONN_E_MO_CLOSE:
292 case RAN_CONN_E_CN_CLOSE:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200293 log_close_event(fi, event, data);
294 /* fall through */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100295 case RAN_CONN_E_UNUSED:
296 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200297 return;
298
299 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200300 OSMO_ASSERT(false);
Harald Welteb8b85a12016-06-17 00:06:42 +0200301 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200302}
303
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100304static void ran_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Harald Welte2483f1b2016-06-19 18:06:02 +0200305{
306 switch (event) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100307 case RAN_CONN_E_COMMUNICATING:
Harald Welte2483f1b2016-06-19 18:06:02 +0200308 /* no-op */
309 return;
310
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100311 case RAN_CONN_E_MO_CLOSE:
312 case RAN_CONN_E_CN_CLOSE:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200313 log_close_event(fi, event, data);
314 /* fall through */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100315 case RAN_CONN_E_UNUSED:
316 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0);
Harald Welte2483f1b2016-06-19 18:06:02 +0200317 return;
318
319 default:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200320 OSMO_ASSERT(false);
Harald Welte2483f1b2016-06-19 18:06:02 +0200321 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200322}
323
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100324static int ran_conn_fsm_timeout(struct osmo_fsm_inst *fi)
Harald Welteb8b85a12016-06-17 00:06:42 +0200325{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100326 struct ran_conn *conn = fi->priv;
327 if (ran_conn_in_release(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200328 LOGPFSML(fi, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
329 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_TIMEOUT, NULL);
330 } else {
Neels Hofmeyr15809592018-04-06 02:57:51 +0200331 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100332 osmo_fsm_inst_dispatch(fi, RAN_CONN_E_CN_CLOSE, &cause);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200333 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200334 return 0;
335}
336
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100337static void ran_conn_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
Harald Welte2483f1b2016-06-19 18:06:02 +0200338{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100339 struct ran_conn *conn = fi->priv;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200340
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200341 /* Use count for either conn->a.waiting_for_clear_complete or
342 * conn->iu.waiting_for_release_complete. 'get' it early, so we don't deallocate after tearing
343 * down active transactions. Safeguard against double-get (though it shouldn't happen). */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100344 if (!ran_conn_used_by(conn, RAN_CONN_USE_RELEASE))
345 ran_conn_get(conn, RAN_CONN_USE_RELEASE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200346
347 /* Cancel pending CM Service Requests */
348 if (conn->received_cm_service_request) {
349 conn->received_cm_service_request = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100350 ran_conn_put(conn, RAN_CONN_USE_CM_SERVICE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200351 }
352
353 /* Cancel all VLR FSMs, if any */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200354 vlr_subscr_cancel_attach_fsm(conn->vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200355
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200356 if (conn->vsub) {
357 /* The subscriber has no active connection anymore.
358 * Restart the periodic Location Update expiry timer for this subscriber. */
359 vlr_subscr_enable_expire_lu(conn->vsub);
360 }
361
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200362 /* If we're closing in a middle of a trans, we need to clean up */
363 trans_conn_closed(conn);
364
365 switch (conn->via_ran) {
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100366 case OSMO_RAT_GERAN_A:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200367 a_iface_tx_clear_cmd(conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200368 if (conn->a.waiting_for_clear_complete) {
369 LOGPFSML(fi, LOGL_ERROR,
370 "Unexpected: conn is already waiting for BSSMAP Clear Complete\n");
371 break;
372 }
373 conn->a.waiting_for_clear_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200374 break;
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100375 case OSMO_RAT_UTRAN_IU:
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200376 ranap_iu_tx_release(conn->iu.ue_ctx, NULL);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200377 if (conn->iu.waiting_for_release_complete) {
378 LOGPFSML(fi, LOGL_ERROR,
379 "Unexpected: conn is already waiting for Iu Release Complete\n");
380 break;
381 }
382 conn->iu.waiting_for_release_complete = true;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200383 break;
384 default:
385 LOGP(DMM, LOGL_ERROR, "%s: Unknown RAN type, cannot tx release/clear\n",
386 vlr_subscr_name(conn->vsub));
387 break;
388 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200389}
390
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100391static void ran_conn_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200392{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100393 OSMO_ASSERT(event == RAN_CONN_E_UNUSED);
394 osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASED, 0, 0);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200395}
396
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100397static void ran_conn_fsm_released(struct osmo_fsm_inst *fi, uint32_t prev_state)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200398{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100399 /* Terminate, deallocate and also deallocate the ran_conn, which is allocated as
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200400 * a talloc child of fi. Also calls the cleanup function. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200401 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200402}
403
404#define S(x) (1 << (x))
405
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100406static const struct osmo_fsm_state ran_conn_fsm_states[] = {
407 [RAN_CONN_S_NEW] = {
408 .name = OSMO_STRINGIFY(RAN_CONN_S_NEW),
409 .in_event_mask = S(RAN_CONN_E_COMPLETE_LAYER_3) |
410 S(RAN_CONN_E_ACCEPTED) |
411 S(RAN_CONN_E_MO_CLOSE) |
412 S(RAN_CONN_E_CN_CLOSE) |
413 S(RAN_CONN_E_UNUSED),
414 .out_state_mask = S(RAN_CONN_S_AUTH_CIPH) |
415 S(RAN_CONN_S_ACCEPTED) |
416 S(RAN_CONN_S_RELEASING),
417 .action = ran_conn_fsm_new,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200418 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100419 [RAN_CONN_S_AUTH_CIPH] = {
420 .name = OSMO_STRINGIFY(RAN_CONN_S_AUTH_CIPH),
421 .in_event_mask = S(RAN_CONN_E_ACCEPTED) |
422 S(RAN_CONN_E_MO_CLOSE) |
423 S(RAN_CONN_E_CN_CLOSE) |
424 S(RAN_CONN_E_UNUSED),
425 .out_state_mask = S(RAN_CONN_S_WAIT_CLASSMARK_UPDATE) |
426 S(RAN_CONN_S_ACCEPTED) |
427 S(RAN_CONN_S_RELEASING),
428 .action = ran_conn_fsm_auth_ciph,
Harald Welteb8b85a12016-06-17 00:06:42 +0200429 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100430 [RAN_CONN_S_WAIT_CLASSMARK_UPDATE] = {
431 .name = OSMO_STRINGIFY(RAN_CONN_S_WAIT_CLASSMARK_UPDATE),
432 .in_event_mask = S(RAN_CONN_E_CLASSMARK_UPDATE) |
433 S(RAN_CONN_E_MO_CLOSE) |
434 S(RAN_CONN_E_CN_CLOSE) |
435 S(RAN_CONN_E_UNUSED),
436 .out_state_mask = S(RAN_CONN_S_AUTH_CIPH) |
437 S(RAN_CONN_S_RELEASING),
438 .action = ran_conn_fsm_wait_classmark_update,
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200439 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100440 [RAN_CONN_S_ACCEPTED] = {
441 .name = OSMO_STRINGIFY(RAN_CONN_S_ACCEPTED),
Harald Welteb8b85a12016-06-17 00:06:42 +0200442 /* allow everything to release for any odd behavior */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100443 .in_event_mask = S(RAN_CONN_E_COMPLETE_LAYER_3) |
444 S(RAN_CONN_E_COMMUNICATING) |
Vadim Yanitskiyce9e1f62019-01-25 19:37:59 +0700445 S(RAN_CONN_E_RELEASE_WHEN_UNUSED) |
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100446 S(RAN_CONN_E_ACCEPTED) |
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_RELEASING) |
451 S(RAN_CONN_S_COMMUNICATING),
452 .onenter = ran_conn_fsm_accepted_enter,
453 .action = ran_conn_fsm_accepted,
Harald Welteb8b85a12016-06-17 00:06:42 +0200454 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100455 [RAN_CONN_S_COMMUNICATING] = {
456 .name = OSMO_STRINGIFY(RAN_CONN_S_COMMUNICATING),
Harald Welte2483f1b2016-06-19 18:06:02 +0200457 /* allow everything to release for any odd behavior */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100458 .in_event_mask = S(RAN_CONN_E_RELEASE_WHEN_UNUSED) |
459 S(RAN_CONN_E_ACCEPTED) |
460 S(RAN_CONN_E_COMMUNICATING) |
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 .action = ran_conn_fsm_communicating,
Harald Welte2483f1b2016-06-19 18:06:02 +0200466 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100467 [RAN_CONN_S_RELEASING] = {
468 .name = OSMO_STRINGIFY(RAN_CONN_S_RELEASING),
469 .in_event_mask = S(RAN_CONN_E_UNUSED),
470 .out_state_mask = S(RAN_CONN_S_RELEASED),
471 .onenter = ran_conn_fsm_releasing_onenter,
472 .action = ran_conn_fsm_releasing,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200473 },
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100474 [RAN_CONN_S_RELEASED] = {
475 .name = OSMO_STRINGIFY(RAN_CONN_S_RELEASED),
476 .onenter = ran_conn_fsm_released,
Harald Welteb8b85a12016-06-17 00:06:42 +0200477 },
478};
479
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100480static void ran_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200481
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100482static struct osmo_fsm ran_conn_fsm = {
483 .name = "RAN_conn",
484 .states = ran_conn_fsm_states,
485 .num_states = ARRAY_SIZE(ran_conn_fsm_states),
Harald Welteb8b85a12016-06-17 00:06:42 +0200486 .allstate_event_mask = 0,
487 .allstate_action = NULL,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200488 .log_subsys = DMM,
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100489 .event_names = ran_conn_fsm_event_names,
490 .cleanup = ran_conn_fsm_cleanup,
491 .timer_cb = ran_conn_fsm_timeout,
Harald Welteb8b85a12016-06-17 00:06:42 +0200492};
493
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100494/* Return statically allocated string of the ran_conn RAT type and id. */
495const char *ran_conn_get_conn_id(struct ran_conn *conn)
Daniel Willmann4e825b62018-02-15 10:33:26 +0100496{
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100497 static char id[42];
498 int rc;
499 uint32_t conn_id;
Daniel Willmann4e825b62018-02-15 10:33:26 +0100500
501 switch (conn->via_ran) {
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100502 case OSMO_RAT_GERAN_A:
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100503 conn_id = conn->a.conn_id;
Daniel Willmann4e825b62018-02-15 10:33:26 +0100504 break;
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100505 case OSMO_RAT_UTRAN_IU:
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100506 conn_id = iu_get_conn_id(conn->iu.ue_ctx);
Daniel Willmann4e825b62018-02-15 10:33:26 +0100507 break;
508 default:
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100509 return "ran-unknown";
Daniel Willmann4e825b62018-02-15 10:33:26 +0100510 }
511
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100512 rc = snprintf(id, sizeof(id), "%s-%u", osmo_rat_type_name(conn->via_ran), conn_id);
513 /* < 0 is error, == 0 is empty, >= size means truncation. Not really expecting this to catch on in any practical
514 * situation. */
515 if (rc <= 0 || rc >= sizeof(id)) {
516 LOGP(DMM, LOGL_ERROR, "Error with conn id; rc=%d\n", rc);
517 return "conn-id-error";
518 }
Daniel Willmann4e825b62018-02-15 10:33:26 +0100519 return id;
520}
521
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200522/* Tidy up before the FSM deallocates */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100523static void ran_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200524{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100525 struct ran_conn *conn = fi->priv;
Harald Welteb8b85a12016-06-17 00:06:42 +0200526
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100527 if (ran_conn_fsm_has_active_transactions(fi))
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200528 LOGPFSML(fi, LOGL_ERROR, "Deallocating despite active transactions\n");
529
530 if (!conn) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100531 LOGP(DRLL, LOGL_ERROR, "Freeing NULL RAN connection\n");
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200532 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200533 }
534
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200535 if (conn->vsub) {
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100536 DEBUGP(DRLL, "%s: Freeing RAN connection\n", vlr_subscr_name(conn->vsub));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200537 conn->vsub->lu_fsm = NULL;
538 conn->vsub->msc_conn_ref = NULL;
539 vlr_subscr_put(conn->vsub);
540 conn->vsub = NULL;
541 } else
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100542 DEBUGP(DRLL, "Freeing RAN connection with NULL subscriber\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200543
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200544 llist_del(&conn->entry);
545}
546
547/* Signal success of Complete Layer 3. Allow to keep the conn open for Auth and Ciph. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100548void ran_conn_complete_layer_3(struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200549{
550 if (!conn)
551 return;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100552 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_COMPLETE_LAYER_3, NULL);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200553}
554
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100555void ran_conn_release_when_unused(struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200556{
557 if (!conn)
558 return;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100559 if (ran_conn_in_release(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200560 DEBUGP(DMM, "%s: %s: conn already in release (%s)\n",
561 vlr_subscr_name(conn->vsub), __func__,
562 osmo_fsm_inst_state_name(conn->fi));
563 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200564 }
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100565 if (conn->fi->state == RAN_CONN_S_NEW) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200566 DEBUGP(DMM, "%s: %s: conn still being established (%s)\n",
567 vlr_subscr_name(conn->vsub), __func__,
568 osmo_fsm_inst_state_name(conn->fi));
569 return;
570 }
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100571 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_RELEASE_WHEN_UNUSED, NULL);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200572}
573
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100574static void conn_close(struct ran_conn *conn, uint32_t cause, uint32_t event)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200575{
576 if (!conn) {
577 LOGP(DMM, LOGL_ERROR, "Cannot release NULL connection\n");
578 return;
579 }
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100580 if (ran_conn_in_release(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200581 DEBUGP(DMM, "%s(vsub=%s, cause=%u): already in release, ignore.\n",
582 __func__, vlr_subscr_name(conn->vsub), cause);
583 return;
584 }
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200585 osmo_fsm_inst_dispatch(conn->fi, event, &cause);
586}
587
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100588void ran_conn_close(struct ran_conn *conn, uint32_t cause)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200589{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100590 return conn_close(conn, cause, RAN_CONN_E_CN_CLOSE);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200591}
592
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100593void ran_conn_mo_close(struct ran_conn *conn, uint32_t cause)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200594{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100595 return conn_close(conn, cause, RAN_CONN_E_MO_CLOSE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200596}
597
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100598bool ran_conn_in_release(struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200599{
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200600 if (!conn || !conn->fi)
601 return true;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100602 if (conn->fi->state == RAN_CONN_S_RELEASING)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200603 return true;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100604 if (conn->fi->state == RAN_CONN_S_RELEASED)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200605 return true;
606 return false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200607}
608
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100609bool ran_conn_is_accepted(const struct ran_conn *conn)
Harald Welteb8b85a12016-06-17 00:06:42 +0200610{
611 if (!conn)
612 return false;
Harald Welte2483f1b2016-06-19 18:06:02 +0200613 if (!conn->vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200614 return false;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100615 if (!(conn->fi->state == RAN_CONN_S_ACCEPTED
616 || conn->fi->state == RAN_CONN_S_COMMUNICATING))
Harald Welteb8b85a12016-06-17 00:06:42 +0200617 return false;
618 return true;
619}
620
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200621/* Indicate that *some* communication is happening with the phone, so that the conn FSM no longer times
622 * out to release within a few seconds. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100623void ran_conn_communicating(struct ran_conn *conn)
Harald Welte2483f1b2016-06-19 18:06:02 +0200624{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100625 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_COMMUNICATING, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200626}
627
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100628void ran_conn_init(void)
Harald Welteb8b85a12016-06-17 00:06:42 +0200629{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100630 osmo_fsm_register(&ran_conn_fsm);
Harald Welteb8b85a12016-06-17 00:06:42 +0200631}
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200632
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100633/* Allocate a new RAN conn and FSM.
634 * Deallocation is by ran_conn_put(): when the use count reaches zero, the
635 * RAN_CONN_E_RELEASE_COMPLETE event is dispatched, the FSM terminates and deallocates both FSM and
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200636 * conn. As long as the FSM is waiting for responses from the subscriber, it will itself hold a use count
637 * on the conn. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100638struct ran_conn *ran_conn_alloc(struct gsm_network *network,
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100639 enum osmo_rat_type via_ran, uint16_t lac)
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200640{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100641 struct ran_conn *conn;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200642 struct osmo_fsm_inst *fi;
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200643
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100644 fi = osmo_fsm_inst_alloc(&ran_conn_fsm, network, NULL, LOGL_DEBUG, NULL);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200645 if (!fi) {
646 LOGP(DMM, LOGL_ERROR, "Failed to allocate conn FSM\n");
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200647 return NULL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200648 }
649
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100650 conn = talloc_zero(fi, struct ran_conn);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200651 if (!conn) {
652 osmo_fsm_inst_free(fi);
653 return NULL;
654 }
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200655
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100656 *conn = (struct ran_conn){
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200657 .network = network,
658 .via_ran = via_ran,
659 .lac = lac,
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200660 .fi = fi,
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200661 };
662
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100663 switch (via_ran) {
664 case OSMO_RAT_GERAN_A:
665 conn->log_subsys = DBSSAP;
666 break;
667 case OSMO_RAT_UTRAN_IU:
668 conn->log_subsys = DRANAP;
669 break;
670 default:
671 conn->log_subsys = DMSC;
672 break;
673 }
674
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200675 fi->priv = conn;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100676 llist_add_tail(&conn->entry, &network->ran_conns);
Neels Hofmeyr93c74632018-04-02 23:10:28 +0200677 return conn;
678}
679
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100680bool ran_conn_is_establishing_auth_ciph(const struct ran_conn *conn)
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200681{
682 if (!conn)
683 return false;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100684 return conn->fi->state == RAN_CONN_S_AUTH_CIPH;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200685}
686
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200687
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200688const struct value_string complete_layer3_type_names[] = {
689 { COMPLETE_LAYER3_NONE, "NONE" },
690 { COMPLETE_LAYER3_LU, "LU" },
691 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
692 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
693 { 0, NULL }
694};
695
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100696static void _ran_conn_update_id(struct ran_conn *conn, const char *subscr_identity)
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200697{
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100698 struct vlr_subscr *vsub = conn->vsub;
699
700 if (osmo_fsm_inst_update_id_f(conn->fi, "%s:%s:%s",
701 subscr_identity,
702 ran_conn_get_conn_id(conn),
703 complete_layer3_type_name(conn->complete_layer3_type))
704 != 0)
705 return; /* osmo_fsm_inst_update_id_f() will log an error. */
706
707 if (vsub) {
708 if (vsub->lu_fsm)
709 osmo_fsm_inst_update_id(vsub->lu_fsm, conn->fi->id);
710 if (vsub->auth_fsm)
711 osmo_fsm_inst_update_id(vsub->auth_fsm, conn->fi->id);
712 if (vsub->proc_arq_fsm)
713 osmo_fsm_inst_update_id(vsub->proc_arq_fsm, conn->fi->id);
714 }
715
716 LOGPFSML(conn->fi, LOGL_DEBUG, "Updated ID\n");
717}
718
719/* Compose an ID almost like gsm48_mi_to_string(), but print the MI type along, and print a TMSI as hex. */
720void ran_conn_update_id_from_mi(struct ran_conn *conn, const uint8_t *mi, uint8_t mi_len)
721{
722 _ran_conn_update_id(conn, osmo_mi_name(mi, mi_len));
723}
724
725/* Update ran_conn->fi id string from current conn->vsub and conn->complete_layer3_type. */
726void ran_conn_update_id(struct ran_conn *conn)
727{
728 _ran_conn_update_id(conn, vlr_subscr_name(conn->vsub));
729}
730
731/* Iterate all ran_conn instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
732 * instances. */
733void ran_conn_update_id_for_vsub(struct vlr_subscr *for_vsub)
734{
735 struct gsm_network *network;
736 struct ran_conn *conn;
737 if (!for_vsub)
738 return;
739
740 network = for_vsub->vlr->user_ctx;
741 OSMO_ASSERT(network);
742
743 llist_for_each_entry(conn, &network->ran_conns, entry) {
744 if (conn->vsub == for_vsub)
745 ran_conn_update_id(conn);
746 }
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200747}
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200748
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100749static void rx_close_complete(struct ran_conn *conn, const char *label, bool *flag)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200750{
751 if (!conn)
752 return;
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100753 if (!ran_conn_in_release(conn)) {
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200754 LOGPFSML(conn->fi, LOGL_ERROR, "Received unexpected %s, discarding right now\n",
755 label);
756 trans_conn_closed(conn);
757 osmo_fsm_inst_term(conn->fi, OSMO_FSM_TERM_ERROR, NULL);
758 return;
759 }
760 if (*flag) {
761 *flag = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100762 ran_conn_put(conn, RAN_CONN_USE_RELEASE);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200763 }
764}
765
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100766void ran_conn_rx_bssmap_clear_complete(struct ran_conn *conn)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200767{
768 rx_close_complete(conn, "BSSMAP Clear Complete", &conn->a.waiting_for_clear_complete);
769}
770
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100771void ran_conn_rx_iu_release_complete(struct ran_conn *conn)
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200772{
773 rx_close_complete(conn, "Iu Release Complete", &conn->iu.waiting_for_release_complete);
774}