Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 1 | /* MSC RAN connection implementation */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 2 | |
| 3 | /* |
Neels Hofmeyr | dbaab50 | 2018-11-30 00:45:07 +0100 | [diff] [blame] | 4 | * (C) 2016-2018 by sysmocom s.m.f.c. <info@sysmocom.de> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 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 | a8945ce | 2018-11-30 00:44:32 +0100 | [diff] [blame] | 28 | #include <osmocom/msc/ran_conn.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 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> |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 34 | #include <osmocom/msc/sgs_iface.h> |
Daniel Willmann | 4e825b6 | 2018-02-15 10:33:26 +0100 | [diff] [blame] | 35 | #include <osmocom/msc/iucs.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 36 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 37 | #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 Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 44 | #define RAN_CONN_TIMEOUT 5 /* seconds */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 45 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 46 | static 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 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 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 60 | struct ran_conn *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 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 84 | struct ran_conn *conn = fi->priv; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 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; |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 98 | ran_conn_get(conn, RAN_CONN_USE_CM_SERVICE); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 114 | static void ran_conn_fsm_new(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 115 | { |
| 116 | switch (event) { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 117 | case RAN_CONN_E_COMPLETE_LAYER_3: |
| 118 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_AUTH_CIPH, RAN_CONN_TIMEOUT, 0); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 119 | return; |
| 120 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 121 | case RAN_CONN_E_ACCEPTED: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 122 | evaluate_acceptance_outcome(fi, true); |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 123 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_ACCEPTED, RAN_CONN_TIMEOUT, 0); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 124 | return; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 125 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 126 | case RAN_CONN_E_MO_CLOSE: |
| 127 | case RAN_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 */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 131 | case RAN_CONN_E_UNUSED: |
| 132 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 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 | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 140 | static void ran_conn_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 141 | { |
| 142 | /* If accepted, transition the state, all other cases mean failure. */ |
| 143 | switch (event) { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 144 | case RAN_CONN_E_ACCEPTED: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 145 | evaluate_acceptance_outcome(fi, true); |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 146 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_ACCEPTED, RAN_CONN_TIMEOUT, 0); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 147 | return; |
| 148 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 149 | case RAN_CONN_E_UNUSED: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 154 | case RAN_CONN_E_MO_CLOSE: |
| 155 | case RAN_CONN_E_CN_CLOSE: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 156 | log_close_event(fi, event, data); |
| 157 | evaluate_acceptance_outcome(fi, false); |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 158 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 159 | return; |
| 160 | |
| 161 | |
| 162 | default: |
| 163 | OSMO_ASSERT(false); |
| 164 | } |
| 165 | } |
| 166 | |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 167 | int ran_conn_classmark_request_then_cipher_mode_cmd(struct ran_conn *conn, bool umts_aka, |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 181 | osmo_fsm_inst_state_chg(conn->fi, RAN_CONN_S_WAIT_CLASSMARK_UPDATE, RAN_CONN_TIMEOUT, 0); |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 185 | static void ran_conn_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 186 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 187 | struct ran_conn *conn = fi->priv; |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 188 | switch (event) { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 189 | case RAN_CONN_E_CLASSMARK_UPDATE: |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 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. |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 194 | * ran_conn_classmark_request_then_cipher_mode_cmd() sets an enum value to indicate that |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 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. */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 200 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_AUTH_CIPH, RAN_CONN_TIMEOUT, 0); |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 201 | if (ran_conn_geran_set_cipher_mode(conn, conn->geran_set_cipher_mode.umts_aka, |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 210 | case RAN_CONN_E_UNUSED: |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 215 | case RAN_CONN_E_MO_CLOSE: |
| 216 | case RAN_CONN_E_CN_CLOSE: |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 217 | log_close_event(fi, event, data); |
| 218 | evaluate_acceptance_outcome(fi, false); |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 219 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_CONN_TIMEOUT, 0); |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 220 | return; |
| 221 | |
| 222 | default: |
| 223 | OSMO_ASSERT(false); |
| 224 | } |
| 225 | } |
| 226 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 227 | static bool ran_conn_fsm_has_active_transactions(struct osmo_fsm_inst *fi) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 228 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 229 | struct ran_conn *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 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 264 | static void ran_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 265 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 266 | struct ran_conn *conn = fi->priv; |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 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 | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 275 | if (!ran_conn_fsm_has_active_transactions(fi)) |
| 276 | osmo_fsm_inst_dispatch(fi, RAN_CONN_E_UNUSED, NULL); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 279 | static void ran_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 280 | { |
| 281 | switch (event) { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 282 | case RAN_CONN_E_COMPLETE_LAYER_3: |
Neels Hofmeyr | 36115c9 | 2018-08-23 15:52:43 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 288 | case RAN_CONN_E_COMMUNICATING: |
| 289 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_COMMUNICATING, 0, 0); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 290 | return; |
| 291 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 292 | case RAN_CONN_E_MO_CLOSE: |
| 293 | case RAN_CONN_E_CN_CLOSE: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 294 | log_close_event(fi, event, data); |
| 295 | /* fall through */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 296 | case RAN_CONN_E_UNUSED: |
| 297 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_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 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 305 | static void ran_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 306 | { |
| 307 | switch (event) { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 308 | case RAN_CONN_E_COMMUNICATING: |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 309 | /* no-op */ |
| 310 | return; |
| 311 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 312 | case RAN_CONN_E_MO_CLOSE: |
| 313 | case RAN_CONN_E_CN_CLOSE: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 314 | log_close_event(fi, event, data); |
| 315 | /* fall through */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 316 | case RAN_CONN_E_UNUSED: |
| 317 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASING, RAN_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 | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 325 | static int ran_conn_fsm_timeout(struct osmo_fsm_inst *fi) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 326 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 327 | struct ran_conn *conn = fi->priv; |
| 328 | if (ran_conn_in_release(conn)) { |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 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 | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 333 | osmo_fsm_inst_dispatch(fi, RAN_CONN_E_CN_CLOSE, &cause); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 334 | } |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 338 | static void ran_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 | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 340 | struct ran_conn *conn = fi->priv; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 341 | |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 342 | /* 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 Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 349 | /* 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 Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 352 | if (!ran_conn_used_by(conn, RAN_CONN_USE_RELEASE)) |
| 353 | ran_conn_get(conn, RAN_CONN_USE_RELEASE); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 354 | |
| 355 | /* Cancel pending CM Service Requests */ |
| 356 | if (conn->received_cm_service_request) { |
| 357 | conn->received_cm_service_request = false; |
Neels Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 358 | ran_conn_put(conn, RAN_CONN_USE_CM_SERVICE); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* Cancel all VLR FSMs, if any */ |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 362 | 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] | 363 | |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 364 | 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 Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 370 | /* 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 Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 374 | case OSMO_RAT_GERAN_A: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 375 | a_iface_tx_clear_cmd(conn); |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 376 | 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 Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 382 | break; |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 383 | case OSMO_RAT_UTRAN_IU: |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 384 | ranap_iu_tx_release(conn->iu.ue_ctx, NULL); |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 385 | 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 Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 391 | break; |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 392 | 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 Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 398 | 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 Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 405 | static void ran_conn_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 406 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 407 | OSMO_ASSERT(event == RAN_CONN_E_UNUSED); |
| 408 | osmo_fsm_inst_state_chg(fi, RAN_CONN_S_RELEASED, 0, 0); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 411 | static void ran_conn_fsm_released(struct osmo_fsm_inst *fi, uint32_t prev_state) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 412 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 413 | /* Terminate, deallocate and also deallocate the ran_conn, which is allocated as |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 414 | * a talloc child of fi. Also calls the cleanup function. */ |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 415 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | #define S(x) (1 << (x)) |
| 419 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 420 | static 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 Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 432 | }, |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 433 | [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 Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 443 | }, |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 444 | [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 Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 453 | }, |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 454 | [RAN_CONN_S_ACCEPTED] = { |
| 455 | .name = OSMO_STRINGIFY(RAN_CONN_S_ACCEPTED), |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 456 | /* allow everything to release for any odd behavior */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 457 | .in_event_mask = S(RAN_CONN_E_COMPLETE_LAYER_3) | |
| 458 | S(RAN_CONN_E_COMMUNICATING) | |
Vadim Yanitskiy | ce9e1f6 | 2019-01-25 19:37:59 +0700 | [diff] [blame] | 459 | S(RAN_CONN_E_RELEASE_WHEN_UNUSED) | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 460 | 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 Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 468 | }, |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 469 | [RAN_CONN_S_COMMUNICATING] = { |
| 470 | .name = OSMO_STRINGIFY(RAN_CONN_S_COMMUNICATING), |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 471 | /* allow everything to release for any odd behavior */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 472 | .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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 480 | }, |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 481 | [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 Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 487 | }, |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 488 | [RAN_CONN_S_RELEASED] = { |
| 489 | .name = OSMO_STRINGIFY(RAN_CONN_S_RELEASED), |
| 490 | .onenter = ran_conn_fsm_released, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 491 | }, |
| 492 | }; |
| 493 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 494 | static void ran_conn_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 495 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 496 | static 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 Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 500 | .allstate_event_mask = 0, |
| 501 | .allstate_action = NULL, |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 502 | .log_subsys = DMM, |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 503 | .event_names = ran_conn_fsm_event_names, |
| 504 | .cleanup = ran_conn_fsm_cleanup, |
| 505 | .timer_cb = ran_conn_fsm_timeout, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 506 | }; |
| 507 | |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 508 | /* Return statically allocated string of the ran_conn RAT type and id. */ |
| 509 | const char *ran_conn_get_conn_id(struct ran_conn *conn) |
Daniel Willmann | 4e825b6 | 2018-02-15 10:33:26 +0100 | [diff] [blame] | 510 | { |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 511 | static char id[42]; |
| 512 | int rc; |
| 513 | uint32_t conn_id; |
Daniel Willmann | 4e825b6 | 2018-02-15 10:33:26 +0100 | [diff] [blame] | 514 | |
| 515 | switch (conn->via_ran) { |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 516 | case OSMO_RAT_GERAN_A: |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 517 | conn_id = conn->a.conn_id; |
Daniel Willmann | 4e825b6 | 2018-02-15 10:33:26 +0100 | [diff] [blame] | 518 | break; |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 519 | case OSMO_RAT_UTRAN_IU: |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 520 | conn_id = iu_get_conn_id(conn->iu.ue_ctx); |
Daniel Willmann | 4e825b6 | 2018-02-15 10:33:26 +0100 | [diff] [blame] | 521 | break; |
| 522 | default: |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 523 | return "ran-unknown"; |
Daniel Willmann | 4e825b6 | 2018-02-15 10:33:26 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 526 | 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 Willmann | 4e825b6 | 2018-02-15 10:33:26 +0100 | [diff] [blame] | 533 | return id; |
| 534 | } |
| 535 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 536 | /* Tidy up before the FSM deallocates */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 537 | static void ran_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] | 538 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 539 | struct ran_conn *conn = fi->priv; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 540 | |
Neels Hofmeyr | 8e2c6a3 | 2019-03-07 03:06:16 +0100 | [diff] [blame] | 541 | if (ran_conn_fsm_has_active_transactions(fi)) { |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 542 | LOGPFSML(fi, LOGL_ERROR, "Deallocating despite active transactions\n"); |
Neels Hofmeyr | 8e2c6a3 | 2019-03-07 03:06:16 +0100 | [diff] [blame] | 543 | trans_conn_closed(conn); |
| 544 | } |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 545 | |
| 546 | if (!conn) { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 547 | LOGP(DRLL, LOGL_ERROR, "Freeing NULL RAN connection\n"); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 548 | return; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 549 | } |
| 550 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 551 | if (conn->vsub) { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 552 | DEBUGP(DRLL, "%s: Freeing RAN connection\n", vlr_subscr_name(conn->vsub)); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 553 | 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 Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 558 | DEBUGP(DRLL, "Freeing RAN connection with NULL subscriber\n"); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 559 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 560 | llist_del(&conn->entry); |
| 561 | } |
| 562 | |
| 563 | /* Signal success of Complete Layer 3. Allow to keep the conn open for Auth and Ciph. */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 564 | void ran_conn_complete_layer_3(struct ran_conn *conn) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 565 | { |
| 566 | if (!conn) |
| 567 | return; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 568 | osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_COMPLETE_LAYER_3, NULL); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 571 | void ran_conn_release_when_unused(struct ran_conn *conn) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 572 | { |
| 573 | if (!conn) |
| 574 | return; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 575 | if (ran_conn_in_release(conn)) { |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 576 | 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 Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 580 | } |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 581 | if (conn->fi->state == RAN_CONN_S_NEW) { |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 582 | 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 Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 587 | osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_RELEASE_WHEN_UNUSED, NULL); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 590 | static void conn_close(struct ran_conn *conn, uint32_t cause, uint32_t event) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 591 | { |
| 592 | if (!conn) { |
| 593 | LOGP(DMM, LOGL_ERROR, "Cannot release NULL connection\n"); |
| 594 | return; |
| 595 | } |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 596 | if (ran_conn_in_release(conn)) { |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 597 | DEBUGP(DMM, "%s(vsub=%s, cause=%u): already in release, ignore.\n", |
| 598 | __func__, vlr_subscr_name(conn->vsub), cause); |
| 599 | return; |
| 600 | } |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 601 | osmo_fsm_inst_dispatch(conn->fi, event, &cause); |
| 602 | } |
| 603 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 604 | void ran_conn_close(struct ran_conn *conn, uint32_t cause) |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 605 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 606 | return conn_close(conn, cause, RAN_CONN_E_CN_CLOSE); |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 607 | } |
| 608 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 609 | void ran_conn_mo_close(struct ran_conn *conn, uint32_t cause) |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 610 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 611 | return conn_close(conn, cause, RAN_CONN_E_MO_CLOSE); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 614 | bool ran_conn_in_release(struct ran_conn *conn) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 615 | { |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 616 | if (!conn || !conn->fi) |
| 617 | return true; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 618 | if (conn->fi->state == RAN_CONN_S_RELEASING) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 619 | return true; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 620 | if (conn->fi->state == RAN_CONN_S_RELEASED) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 621 | return true; |
| 622 | return false; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 623 | } |
| 624 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 625 | bool ran_conn_is_accepted(const struct ran_conn *conn) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 626 | { |
| 627 | if (!conn) |
| 628 | return false; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 629 | if (!conn->vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 630 | return false; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 631 | if (!(conn->fi->state == RAN_CONN_S_ACCEPTED |
| 632 | || conn->fi->state == RAN_CONN_S_COMMUNICATING)) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 633 | return false; |
| 634 | return true; |
| 635 | } |
| 636 | |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 637 | /* 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 Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 639 | void ran_conn_communicating(struct ran_conn *conn) |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 640 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 641 | osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_COMMUNICATING, NULL); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 642 | } |
| 643 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 644 | void ran_conn_init(void) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 645 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 646 | osmo_fsm_register(&ran_conn_fsm); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 647 | } |
Neels Hofmeyr | 16c42b5 | 2018-04-02 12:26:16 +0200 | [diff] [blame] | 648 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 649 | /* 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 Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 652 | * 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 Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 654 | struct ran_conn *ran_conn_alloc(struct gsm_network *network, |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 655 | enum osmo_rat_type via_ran, uint16_t lac) |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 656 | { |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 657 | struct ran_conn *conn; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 658 | struct osmo_fsm_inst *fi; |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 659 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 660 | fi = osmo_fsm_inst_alloc(&ran_conn_fsm, network, NULL, LOGL_DEBUG, NULL); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 661 | if (!fi) { |
| 662 | LOGP(DMM, LOGL_ERROR, "Failed to allocate conn FSM\n"); |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 663 | return NULL; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 666 | conn = talloc_zero(fi, struct ran_conn); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 667 | if (!conn) { |
| 668 | osmo_fsm_inst_free(fi); |
| 669 | return NULL; |
| 670 | } |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 671 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 672 | *conn = (struct ran_conn){ |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 673 | .network = network, |
| 674 | .via_ran = via_ran, |
| 675 | .lac = lac, |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 676 | .fi = fi, |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 677 | }; |
| 678 | |
Neels Hofmeyr | 46c06e2 | 2019-01-04 17:42:05 +0100 | [diff] [blame] | 679 | 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 Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 686 | case OSMO_RAT_EUTRAN_SGS: |
| 687 | conn->log_subsys = DSGS; |
Vadim Yanitskiy | 1396e1c | 2019-02-23 15:51:23 +0700 | [diff] [blame] | 688 | break; |
Neels Hofmeyr | 46c06e2 | 2019-01-04 17:42:05 +0100 | [diff] [blame] | 689 | default: |
| 690 | conn->log_subsys = DMSC; |
| 691 | break; |
| 692 | } |
| 693 | |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 694 | fi->priv = conn; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 695 | llist_add_tail(&conn->entry, &network->ran_conns); |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 696 | return conn; |
| 697 | } |
| 698 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 699 | bool ran_conn_is_establishing_auth_ciph(const struct ran_conn *conn) |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 700 | { |
| 701 | if (!conn) |
| 702 | return false; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 703 | return conn->fi->state == RAN_CONN_S_AUTH_CIPH; |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 704 | } |
| 705 | |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 706 | |
Neels Hofmeyr | 16c42b5 | 2018-04-02 12:26:16 +0200 | [diff] [blame] | 707 | const 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 Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 715 | static void _ran_conn_update_id(struct ran_conn *conn, const char *subscr_identity) |
Neels Hofmeyr | 16c42b5 | 2018-04-02 12:26:16 +0200 | [diff] [blame] | 716 | { |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 717 | 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. */ |
| 739 | void 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. */ |
| 745 | void 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. */ |
| 752 | void 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 Hofmeyr | 16c42b5 | 2018-04-02 12:26:16 +0200 | [diff] [blame] | 766 | } |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 767 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 768 | static void rx_close_complete(struct ran_conn *conn, const char *label, bool *flag) |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 769 | { |
| 770 | if (!conn) |
| 771 | return; |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 772 | if (!ran_conn_in_release(conn)) { |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 773 | 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 Hofmeyr | 3c20a5e | 2018-11-30 01:08:36 +0100 | [diff] [blame] | 781 | ran_conn_put(conn, RAN_CONN_USE_RELEASE); |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 785 | void ran_conn_rx_bssmap_clear_complete(struct ran_conn *conn) |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 786 | { |
| 787 | rx_close_complete(conn, "BSSMAP Clear Complete", &conn->a.waiting_for_clear_complete); |
| 788 | } |
| 789 | |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 790 | void ran_conn_rx_iu_release_complete(struct ran_conn *conn) |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 791 | { |
| 792 | rx_close_complete(conn, "Iu Release Complete", &conn->iu.waiting_for_release_complete); |
| 793 | } |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 794 | |
| 795 | void 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 Yanitskiy | 114bad8 | 2019-01-25 19:42:52 +0700 | [diff] [blame] | 802 | |
| 803 | const 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 | |
| 817 | static 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 */ |
| 849 | struct 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 */ |
| 875 | void _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 | |
| 909 | bool ran_conn_used_by(struct ran_conn *conn, enum ran_conn_use token) |
| 910 | { |
| 911 | return conn && (conn->use_tokens & (1 << token)); |
| 912 | } |