Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1 | /* MSC subscriber connection implementation */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2016 by sysmocom s.m.f.c. <info@sysmocom.de> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * Author: Neels Hofmeyr |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <osmocom/core/logging.h> |
| 25 | #include <osmocom/core/fsm.h> |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 26 | #include <osmocom/core/signal.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 27 | |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 28 | #include <osmocom/msc/osmo_msc.h> |
| 29 | #include <osmocom/msc/vlr.h> |
| 30 | #include <osmocom/msc/debug.h> |
| 31 | #include <osmocom/msc/transaction.h> |
| 32 | #include <osmocom/msc/signal.h> |
| 33 | #include <osmocom/msc/a_iface.h> |
Daniel Willmann | 4e825b6 | 2018-02-15 10:33:26 +0100 | [diff] [blame] | 34 | #include <osmocom/msc/iucs.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 35 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 36 | #include "../../bscconfig.h" |
| 37 | #ifdef BUILD_IU |
| 38 | #include <osmocom/ranap/iu_client.h> |
| 39 | #else |
| 40 | #include <osmocom/msc/iu_dummy.h> |
| 41 | #endif |
| 42 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 43 | #define SUBSCR_CONN_TIMEOUT 5 /* seconds */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 44 | |
| 45 | static const struct value_string subscr_conn_fsm_event_names[] = { |
| 46 | OSMO_VALUE_STRING(SUBSCR_CONN_E_INVALID), |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 47 | OSMO_VALUE_STRING(SUBSCR_CONN_E_COMPLETE_LAYER_3), |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame^] | 48 | OSMO_VALUE_STRING(SUBSCR_CONN_E_CLASSMARK_UPDATE), |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 49 | OSMO_VALUE_STRING(SUBSCR_CONN_E_ACCEPTED), |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 50 | OSMO_VALUE_STRING(SUBSCR_CONN_E_COMMUNICATING), |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 51 | OSMO_VALUE_STRING(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED), |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 52 | OSMO_VALUE_STRING(SUBSCR_CONN_E_MO_CLOSE), |
| 53 | OSMO_VALUE_STRING(SUBSCR_CONN_E_CN_CLOSE), |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 54 | OSMO_VALUE_STRING(SUBSCR_CONN_E_UNUSED), |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 55 | { 0, NULL } |
| 56 | }; |
| 57 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 58 | static void update_counters(struct osmo_fsm_inst *fi, bool conn_accepted) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 59 | { |
| 60 | struct gsm_subscriber_connection *conn = fi->priv; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 61 | 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 Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 66 | break; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 67 | 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 | |
| 82 | static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted) |
| 83 | { |
| 84 | struct gsm_subscriber_connection *conn = fi->priv; |
| 85 | |
| 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; |
| 98 | msc_subscr_conn_get(conn, MSC_CONN_USE_CM_SERVICE); |
| 99 | } |
| 100 | |
| 101 | if (conn_accepted) |
| 102 | osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, conn->vsub); |
| 103 | } |
| 104 | |
| 105 | static void log_close_event(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 106 | { |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 107 | 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 Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static void subscr_conn_fsm_new(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 115 | { |
| 116 | switch (event) { |
| 117 | case SUBSCR_CONN_E_COMPLETE_LAYER_3: |
| 118 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_AUTH_CIPH, SUBSCR_CONN_TIMEOUT, 0); |
| 119 | return; |
| 120 | |
| 121 | case SUBSCR_CONN_E_ACCEPTED: |
| 122 | evaluate_acceptance_outcome(fi, true); |
| 123 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED, SUBSCR_CONN_TIMEOUT, 0); |
| 124 | return; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 125 | |
| 126 | case SUBSCR_CONN_E_MO_CLOSE: |
| 127 | case SUBSCR_CONN_E_CN_CLOSE: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 128 | log_close_event(fi, event, data); |
| 129 | evaluate_acceptance_outcome(fi, false); |
| 130 | /* fall through */ |
| 131 | case SUBSCR_CONN_E_UNUSED: |
| 132 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0); |
| 133 | return; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 134 | |
| 135 | default: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 136 | OSMO_ASSERT(false); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 137 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 140 | void subscr_conn_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 141 | { |
| 142 | /* If accepted, transition the state, all other cases mean failure. */ |
| 143 | switch (event) { |
| 144 | case SUBSCR_CONN_E_ACCEPTED: |
| 145 | evaluate_acceptance_outcome(fi, true); |
| 146 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED, SUBSCR_CONN_TIMEOUT, 0); |
| 147 | return; |
| 148 | |
| 149 | case SUBSCR_CONN_E_UNUSED: |
| 150 | LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n", |
| 151 | osmo_fsm_event_name(fi->fsm, event)); |
| 152 | return; |
| 153 | |
| 154 | case SUBSCR_CONN_E_MO_CLOSE: |
| 155 | case SUBSCR_CONN_E_CN_CLOSE: |
| 156 | log_close_event(fi, event, data); |
| 157 | evaluate_acceptance_outcome(fi, false); |
| 158 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0); |
| 159 | return; |
| 160 | |
| 161 | |
| 162 | default: |
| 163 | OSMO_ASSERT(false); |
| 164 | } |
| 165 | } |
| 166 | |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame^] | 167 | int msc_classmark_request_then_cipher_mode_cmd(struct gsm_subscriber_connection *conn, bool umts_aka, |
| 168 | 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 | |
| 181 | osmo_fsm_inst_state_chg(conn->fi, SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE, SUBSCR_CONN_TIMEOUT, 0); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static void subscr_conn_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 186 | { |
| 187 | struct gsm_subscriber_connection *conn = fi->priv; |
| 188 | switch (event) { |
| 189 | case SUBSCR_CONN_E_CLASSMARK_UPDATE: |
| 190 | /* 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. |
| 194 | * msc_classmark_request_then_cipher_mode_cmd() sets an enum value to indicate that |
| 195 | * 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. */ |
| 200 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_AUTH_CIPH, SUBSCR_CONN_TIMEOUT, 0); |
| 201 | if (msc_geran_set_cipher_mode(conn, conn->geran_set_cipher_mode.umts_aka, |
| 202 | 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 | |
| 210 | case SUBSCR_CONN_E_UNUSED: |
| 211 | LOGPFSML(fi, LOGL_DEBUG, "Awaiting results for Auth+Ciph, overruling event %s\n", |
| 212 | osmo_fsm_event_name(fi->fsm, event)); |
| 213 | return; |
| 214 | |
| 215 | case SUBSCR_CONN_E_MO_CLOSE: |
| 216 | case SUBSCR_CONN_E_CN_CLOSE: |
| 217 | log_close_event(fi, event, data); |
| 218 | evaluate_acceptance_outcome(fi, false); |
| 219 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0); |
| 220 | return; |
| 221 | |
| 222 | default: |
| 223 | OSMO_ASSERT(false); |
| 224 | } |
| 225 | } |
| 226 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 227 | static bool subscr_conn_fsm_has_active_transactions(struct osmo_fsm_inst *fi) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 228 | { |
| 229 | struct gsm_subscriber_connection *conn = fi->priv; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 230 | struct gsm_trans *trans; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 231 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 232 | if (conn->silent_call) { |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 233 | LOGPFSML(fi, LOGL_DEBUG, "%s: silent call still active\n", __func__); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 234 | return true; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 235 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 236 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 237 | if (conn->received_cm_service_request) { |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 238 | LOGPFSML(fi, LOGL_DEBUG, "%s: still awaiting first request after a CM Service Request\n", |
| 239 | __func__); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 240 | return true; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 241 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 242 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 243 | 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 Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 247 | LOGPFSML(fi, LOGL_DEBUG, "%s: still active: %s\n", |
| 248 | __func__, sr->label); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 249 | } |
| 250 | } |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 251 | return true; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 252 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 253 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 254 | if ((trans = trans_has_conn(conn))) { |
| 255 | LOGPFSML(fi, LOGL_DEBUG, |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 256 | "%s: connection still has active transaction: %s\n", |
| 257 | __func__, gsm48_pdisc_name(trans->protocol)); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 258 | return true; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 259 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 260 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 261 | return false; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 264 | static void subscr_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 265 | { |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 266 | struct gsm_subscriber_connection *conn = fi->priv; |
| 267 | |
| 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 Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 275 | if (!subscr_conn_fsm_has_active_transactions(fi)) |
| 276 | osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_UNUSED, NULL); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 279 | static void subscr_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 280 | { |
| 281 | switch (event) { |
Neels Hofmeyr | 36115c9 | 2018-08-23 15:52:43 +0200 | [diff] [blame] | 282 | case SUBSCR_CONN_E_COMPLETE_LAYER_3: |
| 283 | /* 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 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 288 | case SUBSCR_CONN_E_COMMUNICATING: |
| 289 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_COMMUNICATING, 0, 0); |
| 290 | return; |
| 291 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 292 | case SUBSCR_CONN_E_MO_CLOSE: |
| 293 | case SUBSCR_CONN_E_CN_CLOSE: |
| 294 | log_close_event(fi, event, data); |
| 295 | /* fall through */ |
| 296 | case SUBSCR_CONN_E_UNUSED: |
| 297 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 298 | return; |
| 299 | |
| 300 | default: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 301 | OSMO_ASSERT(false); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 302 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 305 | static void subscr_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 306 | { |
| 307 | switch (event) { |
| 308 | case SUBSCR_CONN_E_COMMUNICATING: |
| 309 | /* no-op */ |
| 310 | return; |
| 311 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 312 | case SUBSCR_CONN_E_MO_CLOSE: |
| 313 | case SUBSCR_CONN_E_CN_CLOSE: |
| 314 | log_close_event(fi, event, data); |
| 315 | /* fall through */ |
| 316 | case SUBSCR_CONN_E_UNUSED: |
| 317 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASING, SUBSCR_CONN_TIMEOUT, 0); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 318 | return; |
| 319 | |
| 320 | default: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 321 | OSMO_ASSERT(false); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 322 | } |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 325 | static int subscr_conn_fsm_timeout(struct osmo_fsm_inst *fi) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 326 | { |
| 327 | struct gsm_subscriber_connection *conn = fi->priv; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 328 | if (msc_subscr_conn_in_release(conn)) { |
| 329 | 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 Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 332 | enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 333 | osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_CN_CLOSE, &cause); |
| 334 | } |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 338 | static void subscr_conn_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 339 | { |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 340 | struct gsm_subscriber_connection *conn = fi->priv; |
| 341 | |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 342 | /* Use count for either conn->a.waiting_for_clear_complete or |
| 343 | * conn->iu.waiting_for_release_complete. 'get' it early, so we don't deallocate after tearing |
| 344 | * down active transactions. Safeguard against double-get (though it shouldn't happen). */ |
| 345 | if (!msc_subscr_conn_used_by(conn, MSC_CONN_USE_RELEASE)) |
| 346 | msc_subscr_conn_get(conn, MSC_CONN_USE_RELEASE); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 347 | |
| 348 | /* Cancel pending CM Service Requests */ |
| 349 | if (conn->received_cm_service_request) { |
| 350 | conn->received_cm_service_request = false; |
| 351 | msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE); |
| 352 | } |
| 353 | |
| 354 | /* Cancel all VLR FSMs, if any */ |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 355 | vlr_subscr_cancel_attach_fsm(conn->vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 356 | |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 357 | if (conn->vsub) { |
| 358 | /* The subscriber has no active connection anymore. |
| 359 | * Restart the periodic Location Update expiry timer for this subscriber. */ |
| 360 | vlr_subscr_enable_expire_lu(conn->vsub); |
| 361 | } |
| 362 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 363 | /* If we're closing in a middle of a trans, we need to clean up */ |
| 364 | trans_conn_closed(conn); |
| 365 | |
| 366 | switch (conn->via_ran) { |
| 367 | case RAN_GERAN_A: |
| 368 | a_iface_tx_clear_cmd(conn); |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 369 | if (conn->a.waiting_for_clear_complete) { |
| 370 | LOGPFSML(fi, LOGL_ERROR, |
| 371 | "Unexpected: conn is already waiting for BSSMAP Clear Complete\n"); |
| 372 | break; |
| 373 | } |
| 374 | conn->a.waiting_for_clear_complete = true; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 375 | break; |
| 376 | case RAN_UTRAN_IU: |
| 377 | ranap_iu_tx_release(conn->iu.ue_ctx, NULL); |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 378 | if (conn->iu.waiting_for_release_complete) { |
| 379 | LOGPFSML(fi, LOGL_ERROR, |
| 380 | "Unexpected: conn is already waiting for Iu Release Complete\n"); |
| 381 | break; |
| 382 | } |
| 383 | conn->iu.waiting_for_release_complete = true; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 384 | break; |
| 385 | default: |
| 386 | LOGP(DMM, LOGL_ERROR, "%s: Unknown RAN type, cannot tx release/clear\n", |
| 387 | vlr_subscr_name(conn->vsub)); |
| 388 | break; |
| 389 | } |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static void subscr_conn_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 393 | { |
| 394 | OSMO_ASSERT(event == SUBSCR_CONN_E_UNUSED); |
| 395 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0); |
| 396 | } |
| 397 | |
| 398 | static void subscr_conn_fsm_released(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 399 | { |
| 400 | /* Terminate, deallocate and also deallocate the gsm_subscriber_connection, which is allocated as |
| 401 | * a talloc child of fi. Also calls the cleanup function. */ |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 402 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | #define S(x) (1 << (x)) |
| 406 | |
| 407 | static const struct osmo_fsm_state subscr_conn_fsm_states[] = { |
| 408 | [SUBSCR_CONN_S_NEW] = { |
| 409 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_NEW), |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 410 | .in_event_mask = S(SUBSCR_CONN_E_COMPLETE_LAYER_3) | |
| 411 | S(SUBSCR_CONN_E_ACCEPTED) | |
| 412 | S(SUBSCR_CONN_E_MO_CLOSE) | |
| 413 | S(SUBSCR_CONN_E_CN_CLOSE) | |
| 414 | S(SUBSCR_CONN_E_UNUSED), |
| 415 | .out_state_mask = S(SUBSCR_CONN_S_AUTH_CIPH) | |
| 416 | S(SUBSCR_CONN_S_ACCEPTED) | |
| 417 | S(SUBSCR_CONN_S_RELEASING), |
| 418 | .action = subscr_conn_fsm_new, |
| 419 | }, |
| 420 | [SUBSCR_CONN_S_AUTH_CIPH] = { |
| 421 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_AUTH_CIPH), |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 422 | .in_event_mask = S(SUBSCR_CONN_E_ACCEPTED) | |
| 423 | S(SUBSCR_CONN_E_MO_CLOSE) | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 424 | S(SUBSCR_CONN_E_CN_CLOSE) | |
| 425 | S(SUBSCR_CONN_E_UNUSED), |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame^] | 426 | .out_state_mask = S(SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE) | |
| 427 | S(SUBSCR_CONN_S_ACCEPTED) | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 428 | S(SUBSCR_CONN_S_RELEASING), |
| 429 | .action = subscr_conn_fsm_auth_ciph, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 430 | }, |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame^] | 431 | [SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE] = { |
| 432 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_WAIT_CLASSMARK_UPDATE), |
| 433 | .in_event_mask = S(SUBSCR_CONN_E_CLASSMARK_UPDATE) | |
| 434 | S(SUBSCR_CONN_E_MO_CLOSE) | |
| 435 | S(SUBSCR_CONN_E_CN_CLOSE) | |
| 436 | S(SUBSCR_CONN_E_UNUSED), |
| 437 | .out_state_mask = S(SUBSCR_CONN_S_AUTH_CIPH) | |
| 438 | S(SUBSCR_CONN_S_RELEASING), |
| 439 | .action = subscr_conn_fsm_wait_classmark_update, |
| 440 | }, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 441 | [SUBSCR_CONN_S_ACCEPTED] = { |
| 442 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_ACCEPTED), |
| 443 | /* allow everything to release for any odd behavior */ |
Neels Hofmeyr | 36115c9 | 2018-08-23 15:52:43 +0200 | [diff] [blame] | 444 | .in_event_mask = S(SUBSCR_CONN_E_COMPLETE_LAYER_3) | |
| 445 | S(SUBSCR_CONN_E_COMMUNICATING) | |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 446 | S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 447 | S(SUBSCR_CONN_E_ACCEPTED) | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 448 | S(SUBSCR_CONN_E_MO_CLOSE) | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 449 | S(SUBSCR_CONN_E_CN_CLOSE) | |
| 450 | S(SUBSCR_CONN_E_UNUSED), |
| 451 | .out_state_mask = S(SUBSCR_CONN_S_RELEASING) | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 452 | S(SUBSCR_CONN_S_COMMUNICATING), |
| 453 | .onenter = subscr_conn_fsm_accepted_enter, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 454 | .action = subscr_conn_fsm_accepted, |
| 455 | }, |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 456 | [SUBSCR_CONN_S_COMMUNICATING] = { |
| 457 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_COMMUNICATING), |
| 458 | /* allow everything to release for any odd behavior */ |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 459 | .in_event_mask = S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 460 | S(SUBSCR_CONN_E_ACCEPTED) | |
| 461 | S(SUBSCR_CONN_E_COMMUNICATING) | |
| 462 | S(SUBSCR_CONN_E_MO_CLOSE) | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 463 | S(SUBSCR_CONN_E_CN_CLOSE) | |
| 464 | S(SUBSCR_CONN_E_UNUSED), |
| 465 | .out_state_mask = S(SUBSCR_CONN_S_RELEASING), |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 466 | .action = subscr_conn_fsm_communicating, |
| 467 | }, |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 468 | [SUBSCR_CONN_S_RELEASING] = { |
| 469 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASING), |
| 470 | .in_event_mask = S(SUBSCR_CONN_E_UNUSED), |
| 471 | .out_state_mask = S(SUBSCR_CONN_S_RELEASED), |
| 472 | .onenter = subscr_conn_fsm_releasing_onenter, |
| 473 | .action = subscr_conn_fsm_releasing, |
| 474 | }, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 475 | [SUBSCR_CONN_S_RELEASED] = { |
| 476 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASED), |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 477 | .onenter = subscr_conn_fsm_released, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 478 | }, |
| 479 | }; |
| 480 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 481 | static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause); |
| 482 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 483 | static struct osmo_fsm subscr_conn_fsm = { |
| 484 | .name = "Subscr_Conn", |
| 485 | .states = subscr_conn_fsm_states, |
| 486 | .num_states = ARRAY_SIZE(subscr_conn_fsm_states), |
| 487 | .allstate_event_mask = 0, |
| 488 | .allstate_action = NULL, |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 489 | .log_subsys = DMM, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 490 | .event_names = subscr_conn_fsm_event_names, |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 491 | .cleanup = subscr_conn_fsm_cleanup, |
| 492 | .timer_cb = subscr_conn_fsm_timeout, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 493 | }; |
| 494 | |
Daniel Willmann | 4e825b6 | 2018-02-15 10:33:26 +0100 | [diff] [blame] | 495 | char *msc_subscr_conn_get_conn_id(struct gsm_subscriber_connection *conn) |
| 496 | { |
| 497 | char *id; |
| 498 | |
| 499 | switch (conn->via_ran) { |
| 500 | case RAN_GERAN_A: |
| 501 | id = talloc_asprintf(conn, "GERAN_A-%08x", conn->a.conn_id); |
| 502 | break; |
| 503 | case RAN_UTRAN_IU: |
| 504 | id = talloc_asprintf(conn, "UTRAN_IU-%08x", iu_get_conn_id(conn->iu.ue_ctx)); |
| 505 | break; |
| 506 | default: |
| 507 | LOGP(DMM, LOGL_ERROR, "RAN of conn %p unknown!\n", conn); |
| 508 | return NULL; |
| 509 | } |
| 510 | |
| 511 | return id; |
| 512 | } |
| 513 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 514 | /* Tidy up before the FSM deallocates */ |
| 515 | static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 516 | { |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 517 | struct gsm_subscriber_connection *conn = fi->priv; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 518 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 519 | if (subscr_conn_fsm_has_active_transactions(fi)) |
| 520 | LOGPFSML(fi, LOGL_ERROR, "Deallocating despite active transactions\n"); |
| 521 | |
| 522 | if (!conn) { |
| 523 | LOGP(DRLL, LOGL_ERROR, "Freeing NULL subscriber connection\n"); |
| 524 | return; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 527 | if (conn->vsub) { |
| 528 | DEBUGP(DRLL, "%s: Freeing subscriber connection\n", vlr_subscr_name(conn->vsub)); |
| 529 | conn->vsub->lu_fsm = NULL; |
| 530 | conn->vsub->msc_conn_ref = NULL; |
| 531 | vlr_subscr_put(conn->vsub); |
| 532 | conn->vsub = NULL; |
| 533 | } else |
| 534 | DEBUGP(DRLL, "Freeing subscriber connection with NULL subscriber\n"); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 535 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 536 | llist_del(&conn->entry); |
| 537 | } |
| 538 | |
| 539 | /* Signal success of Complete Layer 3. Allow to keep the conn open for Auth and Ciph. */ |
| 540 | void msc_subscr_conn_complete_layer_3(struct gsm_subscriber_connection *conn) |
| 541 | { |
| 542 | if (!conn) |
| 543 | return; |
| 544 | osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_COMPLETE_LAYER_3, NULL); |
| 545 | } |
| 546 | |
| 547 | void subscr_conn_release_when_unused(struct gsm_subscriber_connection *conn) |
| 548 | { |
| 549 | if (!conn) |
| 550 | return; |
| 551 | if (msc_subscr_conn_in_release(conn)) { |
| 552 | DEBUGP(DMM, "%s: %s: conn already in release (%s)\n", |
| 553 | vlr_subscr_name(conn->vsub), __func__, |
| 554 | osmo_fsm_inst_state_name(conn->fi)); |
| 555 | return; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 556 | } |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 557 | if (conn->fi->state == SUBSCR_CONN_S_NEW) { |
| 558 | DEBUGP(DMM, "%s: %s: conn still being established (%s)\n", |
| 559 | vlr_subscr_name(conn->vsub), __func__, |
| 560 | osmo_fsm_inst_state_name(conn->fi)); |
| 561 | return; |
| 562 | } |
| 563 | osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_RELEASE_WHEN_UNUSED, NULL); |
| 564 | } |
| 565 | |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 566 | static void conn_close(struct gsm_subscriber_connection *conn, uint32_t cause, uint32_t event) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 567 | { |
| 568 | if (!conn) { |
| 569 | LOGP(DMM, LOGL_ERROR, "Cannot release NULL connection\n"); |
| 570 | return; |
| 571 | } |
| 572 | if (msc_subscr_conn_in_release(conn)) { |
| 573 | DEBUGP(DMM, "%s(vsub=%s, cause=%u): already in release, ignore.\n", |
| 574 | __func__, vlr_subscr_name(conn->vsub), cause); |
| 575 | return; |
| 576 | } |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 577 | osmo_fsm_inst_dispatch(conn->fi, event, &cause); |
| 578 | } |
| 579 | |
| 580 | void msc_subscr_conn_close(struct gsm_subscriber_connection *conn, uint32_t cause) |
| 581 | { |
| 582 | return conn_close(conn, cause, SUBSCR_CONN_E_CN_CLOSE); |
| 583 | } |
| 584 | |
| 585 | void msc_subscr_conn_mo_close(struct gsm_subscriber_connection *conn, uint32_t cause) |
| 586 | { |
| 587 | return conn_close(conn, cause, SUBSCR_CONN_E_MO_CLOSE); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | bool msc_subscr_conn_in_release(struct gsm_subscriber_connection *conn) |
| 591 | { |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 592 | if (!conn || !conn->fi) |
| 593 | return true; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 594 | if (conn->fi->state == SUBSCR_CONN_S_RELEASING) |
| 595 | return true; |
| 596 | if (conn->fi->state == SUBSCR_CONN_S_RELEASED) |
| 597 | return true; |
| 598 | return false; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Max | d83b17b | 2018-02-06 16:51:31 +0100 | [diff] [blame] | 601 | bool msc_subscr_conn_is_accepted(const struct gsm_subscriber_connection *conn) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 602 | { |
| 603 | if (!conn) |
| 604 | return false; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 605 | if (!conn->vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 606 | return false; |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 607 | if (!(conn->fi->state == SUBSCR_CONN_S_ACCEPTED |
| 608 | || conn->fi->state == SUBSCR_CONN_S_COMMUNICATING)) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 609 | return false; |
| 610 | return true; |
| 611 | } |
| 612 | |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 613 | /* Indicate that *some* communication is happening with the phone, so that the conn FSM no longer times |
| 614 | * out to release within a few seconds. */ |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 615 | void msc_subscr_conn_communicating(struct gsm_subscriber_connection *conn) |
| 616 | { |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 617 | osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_COMMUNICATING, NULL); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 620 | void msc_subscr_conn_init(void) |
| 621 | { |
| 622 | osmo_fsm_register(&subscr_conn_fsm); |
| 623 | } |
Neels Hofmeyr | 16c42b5 | 2018-04-02 12:26:16 +0200 | [diff] [blame] | 624 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 625 | /* Allocate a new subscriber conn and FSM. |
| 626 | * Deallocation is by msc_subscr_conn_put(): when the use count reaches zero, the |
| 627 | * SUBSCR_CONN_E_RELEASE_COMPLETE event is dispatched, the FSM terminates and deallocates both FSM and |
| 628 | * conn. As long as the FSM is waiting for responses from the subscriber, it will itself hold a use count |
| 629 | * on the conn. */ |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 630 | struct gsm_subscriber_connection *msc_subscr_conn_alloc(struct gsm_network *network, |
| 631 | enum ran_type via_ran, uint16_t lac) |
| 632 | { |
| 633 | struct gsm_subscriber_connection *conn; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 634 | struct osmo_fsm_inst *fi; |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 635 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 636 | fi = osmo_fsm_inst_alloc(&subscr_conn_fsm, network, NULL, LOGL_DEBUG, NULL); |
| 637 | if (!fi) { |
| 638 | LOGP(DMM, LOGL_ERROR, "Failed to allocate conn FSM\n"); |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 639 | return NULL; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | conn = talloc_zero(fi, struct gsm_subscriber_connection); |
| 643 | if (!conn) { |
| 644 | osmo_fsm_inst_free(fi); |
| 645 | return NULL; |
| 646 | } |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 647 | |
| 648 | *conn = (struct gsm_subscriber_connection){ |
| 649 | .network = network, |
| 650 | .via_ran = via_ran, |
| 651 | .lac = lac, |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 652 | .fi = fi, |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 653 | }; |
| 654 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 655 | fi->priv = conn; |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 656 | llist_add_tail(&conn->entry, &network->subscr_conns); |
| 657 | return conn; |
| 658 | } |
| 659 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 660 | bool msc_subscr_conn_is_establishing_auth_ciph(const struct gsm_subscriber_connection *conn) |
| 661 | { |
| 662 | if (!conn) |
| 663 | return false; |
| 664 | return conn->fi->state == SUBSCR_CONN_S_AUTH_CIPH; |
| 665 | } |
| 666 | |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 667 | |
Neels Hofmeyr | 16c42b5 | 2018-04-02 12:26:16 +0200 | [diff] [blame] | 668 | const struct value_string complete_layer3_type_names[] = { |
| 669 | { COMPLETE_LAYER3_NONE, "NONE" }, |
| 670 | { COMPLETE_LAYER3_LU, "LU" }, |
| 671 | { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" }, |
| 672 | { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" }, |
| 673 | { 0, NULL } |
| 674 | }; |
| 675 | |
| 676 | void msc_subscr_conn_update_id(struct gsm_subscriber_connection *conn, |
| 677 | enum complete_layer3_type from, const char *id) |
| 678 | { |
| 679 | conn->complete_layer3_type = from; |
Neels Hofmeyr | fe4ba7c | 2018-04-02 23:17:50 +0200 | [diff] [blame] | 680 | osmo_fsm_inst_update_id_f(conn->fi, "%s:%s", complete_layer3_type_name(from), id); |
| 681 | LOGPFSML(conn->fi, LOGL_DEBUG, "Updated ID\n"); |
Neels Hofmeyr | 16c42b5 | 2018-04-02 12:26:16 +0200 | [diff] [blame] | 682 | } |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 683 | |
| 684 | static void rx_close_complete(struct gsm_subscriber_connection *conn, const char *label, bool *flag) |
| 685 | { |
| 686 | if (!conn) |
| 687 | return; |
| 688 | if (!msc_subscr_conn_in_release(conn)) { |
| 689 | LOGPFSML(conn->fi, LOGL_ERROR, "Received unexpected %s, discarding right now\n", |
| 690 | label); |
| 691 | trans_conn_closed(conn); |
| 692 | osmo_fsm_inst_term(conn->fi, OSMO_FSM_TERM_ERROR, NULL); |
| 693 | return; |
| 694 | } |
| 695 | if (*flag) { |
| 696 | *flag = false; |
| 697 | msc_subscr_conn_put(conn, MSC_CONN_USE_RELEASE); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | void msc_subscr_conn_rx_bssmap_clear_complete(struct gsm_subscriber_connection *conn) |
| 702 | { |
| 703 | rx_close_complete(conn, "BSSMAP Clear Complete", &conn->a.waiting_for_clear_complete); |
| 704 | } |
| 705 | |
| 706 | void msc_subscr_conn_rx_iu_release_complete(struct gsm_subscriber_connection *conn) |
| 707 | { |
| 708 | rx_close_complete(conn, "Iu Release Complete", &conn->iu.waiting_for_release_complete); |
| 709 | } |