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 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 36 | #define SUBSCR_CONN_TIMEOUT 5 /* seconds */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 37 | |
| 38 | static const struct value_string subscr_conn_fsm_event_names[] = { |
| 39 | OSMO_VALUE_STRING(SUBSCR_CONN_E_INVALID), |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 40 | OSMO_VALUE_STRING(SUBSCR_CONN_E_START), |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 41 | OSMO_VALUE_STRING(SUBSCR_CONN_E_ACCEPTED), |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 42 | OSMO_VALUE_STRING(SUBSCR_CONN_E_COMMUNICATING), |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 43 | OSMO_VALUE_STRING(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED), |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 44 | OSMO_VALUE_STRING(SUBSCR_CONN_E_MO_CLOSE), |
| 45 | OSMO_VALUE_STRING(SUBSCR_CONN_E_CN_CLOSE), |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 46 | { 0, NULL } |
| 47 | }; |
| 48 | |
| 49 | const struct value_string subscr_conn_from_names[] = { |
| 50 | OSMO_VALUE_STRING(SUBSCR_CONN_FROM_INVALID), |
| 51 | OSMO_VALUE_STRING(SUBSCR_CONN_FROM_LU), |
| 52 | OSMO_VALUE_STRING(SUBSCR_CONN_FROM_CM_SERVICE_REQ), |
| 53 | OSMO_VALUE_STRING(SUBSCR_CONN_FROM_PAGING_RESP), |
| 54 | { 0, NULL } |
| 55 | }; |
| 56 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 57 | static void paging_event(struct gsm_subscriber_connection *conn, |
| 58 | enum gsm_paging_event pe) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 59 | { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 60 | subscr_paging_dispatch(GSM_HOOK_RR_PAGING, pe, NULL, conn, conn->vsub); |
| 61 | } |
| 62 | |
| 63 | void subscr_conn_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 64 | { |
| 65 | OSMO_ASSERT(event == SUBSCR_CONN_E_START); |
| 66 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_NEW, |
| 67 | SUBSCR_CONN_TIMEOUT, 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void subscr_conn_fsm_new(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 71 | { |
| 72 | struct gsm_subscriber_connection *conn = fi->priv; |
| 73 | enum subscr_conn_from from = SUBSCR_CONN_FROM_INVALID; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 74 | bool success; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 75 | |
| 76 | if (data) { |
| 77 | from = *(enum subscr_conn_from*)data; |
| 78 | LOGPFSM(fi, "%s\n", subscr_conn_from_name(from)); |
| 79 | } |
| 80 | |
| 81 | /* If accepted, transition the state, all other cases mean failure. */ |
| 82 | switch (event) { |
| 83 | case SUBSCR_CONN_E_ACCEPTED: |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 84 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED, |
| 85 | SUBSCR_CONN_TIMEOUT, 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 86 | break; |
| 87 | |
| 88 | case SUBSCR_CONN_E_MO_CLOSE: |
| 89 | case SUBSCR_CONN_E_CN_CLOSE: |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 90 | if (data) |
| 91 | LOGPFSM(fi, "Close event, cause %u\n", |
| 92 | *(uint32_t*)data); |
| 93 | /* will release further below, see |
| 94 | * 'if (fi->state != SUBSCR_CONN_S_ACCEPTED)' */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 95 | break; |
| 96 | |
| 97 | default: |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 98 | LOGPFSML(fi, LOGL_ERROR, |
| 99 | "Unexpected event: %d %s\n", event, |
| 100 | osmo_fsm_event_name(fi->fsm, event)); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 104 | success = (fi->state == SUBSCR_CONN_S_ACCEPTED); |
| 105 | |
| 106 | if (from == SUBSCR_CONN_FROM_LU) |
| 107 | rate_ctr_inc(&conn->network->msc_ctrs->ctr[ |
| 108 | success ? MSC_CTR_LOC_UPDATE_COMPLETED |
| 109 | : MSC_CTR_LOC_UPDATE_FAILED]); |
| 110 | |
| 111 | /* signal paging success or failure in case this was a paging */ |
| 112 | if (from == SUBSCR_CONN_FROM_PAGING_RESP) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 113 | paging_event(conn, |
| 114 | success ? GSM_PAGING_SUCCEEDED |
| 115 | : GSM_PAGING_EXPIRED); |
| 116 | |
| 117 | /* FIXME rate counters */ |
| 118 | /*rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_COMPLETED]);*/ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 119 | |
| 120 | /* On failure, discard the conn */ |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 121 | if (!success) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 122 | /* TODO: on MO_CLOSE or CN_CLOSE, first go to RELEASING and |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 123 | * await BSC/RNC confirmation? */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 124 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0); |
| 125 | return; |
| 126 | } |
| 127 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 128 | if (from == SUBSCR_CONN_FROM_CM_SERVICE_REQ) { |
| 129 | conn->received_cm_service_request = true; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 130 | LOGPFSML(fi, LOGL_DEBUG, "received_cm_service_request = true\n"); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 133 | osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_RELEASE_WHEN_UNUSED, data); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 136 | static void subscr_conn_fsm_release_when_unused(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 137 | { |
| 138 | struct gsm_subscriber_connection *conn = fi->priv; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 139 | struct gsm_trans *trans; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 140 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 141 | if (conn->silent_call) { |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 142 | LOGPFSML(fi, LOGL_DEBUG, "%s: silent call still active\n", __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 143 | return; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 144 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 145 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 146 | if (conn->received_cm_service_request) { |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 147 | LOGPFSML(fi, LOGL_DEBUG, "%s: still awaiting first request after a CM Service Request\n", |
| 148 | __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 149 | return; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 150 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 151 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 152 | if (conn->vsub && !llist_empty(&conn->vsub->cs.requests)) { |
| 153 | struct subscr_request *sr; |
| 154 | if (!log_check_level(fi->fsm->log_subsys, LOGL_DEBUG)) { |
| 155 | llist_for_each_entry(sr, &conn->vsub->cs.requests, entry) { |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 156 | LOGPFSML(fi, LOGL_DEBUG, "%s: still active: %s\n", |
| 157 | __func__, sr->label); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 158 | } |
| 159 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 160 | return; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 161 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 162 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 163 | if ((trans = trans_has_conn(conn))) { |
| 164 | LOGPFSML(fi, LOGL_DEBUG, |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 165 | "%s: connection still has active transaction: %s\n", |
| 166 | __func__, gsm48_pdisc_name(trans->protocol)); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 167 | return; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 168 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 169 | |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 170 | LOGPFSML(fi, LOGL_DEBUG, "%s: releasing conn\n", __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 171 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0); |
| 172 | } |
| 173 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 174 | static void subscr_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 175 | { |
| 176 | struct gsm_subscriber_connection *conn = fi->priv; |
| 177 | osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, conn->vsub); |
| 178 | } |
| 179 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 180 | static void subscr_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 181 | { |
| 182 | switch (event) { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 183 | case SUBSCR_CONN_E_COMMUNICATING: |
| 184 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_COMMUNICATING, 0, 0); |
| 185 | return; |
| 186 | |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 187 | case SUBSCR_CONN_E_RELEASE_WHEN_UNUSED: |
| 188 | subscr_conn_fsm_release_when_unused(fi, event, data); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 189 | return; |
| 190 | |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | /* Whatever unexpected happens in the accepted state, it means release. |
| 195 | * Even if an unexpected event is passed, the safest thing to do is |
| 196 | * discard the conn. We don't expect another SUBSCR_CONN_E_ACCEPTED. */ |
| 197 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0); |
| 198 | } |
| 199 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 200 | static void subscr_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 201 | { |
| 202 | switch (event) { |
| 203 | case SUBSCR_CONN_E_COMMUNICATING: |
| 204 | /* no-op */ |
| 205 | return; |
| 206 | |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 207 | case SUBSCR_CONN_E_RELEASE_WHEN_UNUSED: |
| 208 | subscr_conn_fsm_release_when_unused(fi, event, data); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 209 | return; |
| 210 | |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | /* Whatever unexpected happens in the accepted state, it means release. |
| 215 | * Even if an unexpected event is passed, the safest thing to do is |
| 216 | * discard the conn. We don't expect another SUBSCR_CONN_E_ACCEPTED. */ |
| 217 | osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0); |
| 218 | } |
| 219 | |
| 220 | static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi, |
| 221 | enum osmo_fsm_term_cause cause) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 222 | { |
| 223 | struct gsm_subscriber_connection *conn = fi->priv; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 224 | fi->priv = NULL; |
| 225 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 226 | if (!conn) |
| 227 | return; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 228 | conn->conn_fsm = NULL; |
| 229 | msc_subscr_conn_close(conn, cause); |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 230 | msc_subscr_conn_put(conn, MSC_CONN_USE_FSM); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 231 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 232 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 233 | int subscr_conn_fsm_timeout(struct osmo_fsm_inst *fi) |
| 234 | { |
| 235 | struct gsm_subscriber_connection *conn = fi->priv; |
| 236 | if (conn) |
| 237 | vlr_subscr_conn_timeout(conn->vsub); |
Neels Hofmeyr | 3ddd742 | 2017-11-18 23:25:41 +0100 | [diff] [blame] | 238 | else |
| 239 | osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_CN_CLOSE, NULL); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static void subscr_conn_fsm_release(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 244 | { |
| 245 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | #define S(x) (1 << (x)) |
| 249 | |
| 250 | static const struct osmo_fsm_state subscr_conn_fsm_states[] = { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 251 | [SUBSCR_CONN_S_INIT] = { |
| 252 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_INIT), |
| 253 | .in_event_mask = S(SUBSCR_CONN_E_START), |
| 254 | .out_state_mask = S(SUBSCR_CONN_S_NEW), |
| 255 | .action = subscr_conn_fsm_init, |
| 256 | }, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 257 | [SUBSCR_CONN_S_NEW] = { |
| 258 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_NEW), |
| 259 | .in_event_mask = S(SUBSCR_CONN_E_ACCEPTED) | |
| 260 | S(SUBSCR_CONN_E_MO_CLOSE) | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 261 | S(SUBSCR_CONN_E_CN_CLOSE), |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 262 | .out_state_mask = S(SUBSCR_CONN_S_ACCEPTED) | |
| 263 | S(SUBSCR_CONN_S_RELEASED), |
| 264 | .action = subscr_conn_fsm_new, |
| 265 | }, |
| 266 | [SUBSCR_CONN_S_ACCEPTED] = { |
| 267 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_ACCEPTED), |
| 268 | /* allow everything to release for any odd behavior */ |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 269 | .in_event_mask = S(SUBSCR_CONN_E_COMMUNICATING) | |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 270 | S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 271 | S(SUBSCR_CONN_E_ACCEPTED) | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 272 | S(SUBSCR_CONN_E_MO_CLOSE) | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 273 | S(SUBSCR_CONN_E_CN_CLOSE), |
| 274 | .out_state_mask = S(SUBSCR_CONN_S_RELEASED) | |
| 275 | S(SUBSCR_CONN_S_COMMUNICATING), |
| 276 | .onenter = subscr_conn_fsm_accepted_enter, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 277 | .action = subscr_conn_fsm_accepted, |
| 278 | }, |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 279 | [SUBSCR_CONN_S_COMMUNICATING] = { |
| 280 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_COMMUNICATING), |
| 281 | /* allow everything to release for any odd behavior */ |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 282 | .in_event_mask = S(SUBSCR_CONN_E_RELEASE_WHEN_UNUSED) | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 283 | S(SUBSCR_CONN_E_ACCEPTED) | |
| 284 | S(SUBSCR_CONN_E_COMMUNICATING) | |
| 285 | S(SUBSCR_CONN_E_MO_CLOSE) | |
| 286 | S(SUBSCR_CONN_E_CN_CLOSE), |
| 287 | .out_state_mask = S(SUBSCR_CONN_S_RELEASED), |
| 288 | .action = subscr_conn_fsm_communicating, |
| 289 | }, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 290 | [SUBSCR_CONN_S_RELEASED] = { |
| 291 | .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASED), |
| 292 | .onenter = subscr_conn_fsm_release, |
| 293 | }, |
| 294 | }; |
| 295 | |
| 296 | static struct osmo_fsm subscr_conn_fsm = { |
| 297 | .name = "Subscr_Conn", |
| 298 | .states = subscr_conn_fsm_states, |
| 299 | .num_states = ARRAY_SIZE(subscr_conn_fsm_states), |
| 300 | .allstate_event_mask = 0, |
| 301 | .allstate_action = NULL, |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 302 | .log_subsys = DMM, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 303 | .event_names = subscr_conn_fsm_event_names, |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 304 | .cleanup = subscr_conn_fsm_cleanup, |
| 305 | .timer_cb = subscr_conn_fsm_timeout, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 306 | }; |
| 307 | |
Daniel Willmann | 4e825b6 | 2018-02-15 10:33:26 +0100 | [diff] [blame] | 308 | char *msc_subscr_conn_get_conn_id(struct gsm_subscriber_connection *conn) |
| 309 | { |
| 310 | char *id; |
| 311 | |
| 312 | switch (conn->via_ran) { |
| 313 | case RAN_GERAN_A: |
| 314 | id = talloc_asprintf(conn, "GERAN_A-%08x", conn->a.conn_id); |
| 315 | break; |
| 316 | case RAN_UTRAN_IU: |
| 317 | id = talloc_asprintf(conn, "UTRAN_IU-%08x", iu_get_conn_id(conn->iu.ue_ctx)); |
| 318 | break; |
| 319 | default: |
| 320 | LOGP(DMM, LOGL_ERROR, "RAN of conn %p unknown!\n", conn); |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | return id; |
| 325 | } |
| 326 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 327 | int msc_create_conn_fsm(struct gsm_subscriber_connection *conn, const char *id) |
| 328 | { |
| 329 | struct osmo_fsm_inst *fi; |
| 330 | OSMO_ASSERT(conn); |
| 331 | |
| 332 | if (conn->conn_fsm) { |
| 333 | LOGP(DMM, LOGL_ERROR, |
| 334 | "%s: Error: connection already in use\n", id); |
| 335 | return -EINVAL; |
| 336 | } |
| 337 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 338 | /* Allocate the FSM not with the subscr_conn. Semantically it would |
| 339 | * make sense, but in subscr_conn_fsm_cleanup(), we want to discard the |
| 340 | * subscriber connection. If the FSM is freed along with the subscriber |
| 341 | * connection, then in _osmo_fsm_inst_term() the osmo_fsm_inst_free() |
| 342 | * that follows the cleanup() call would run into a double free. */ |
| 343 | fi = osmo_fsm_inst_alloc(&subscr_conn_fsm, conn->network, |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 344 | msc_subscr_conn_get(conn, MSC_CONN_USE_FSM), |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 345 | LOGL_DEBUG, id); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 346 | |
| 347 | if (!fi) { |
| 348 | LOGP(DMM, LOGL_ERROR, |
| 349 | "%s: Failed to allocate subscr conn master FSM\n", id); |
| 350 | return -ENOMEM; |
| 351 | } |
| 352 | conn->conn_fsm = fi; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 353 | osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_START, NULL); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
Max | d83b17b | 2018-02-06 16:51:31 +0100 | [diff] [blame] | 357 | bool msc_subscr_conn_is_accepted(const struct gsm_subscriber_connection *conn) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 358 | { |
| 359 | if (!conn) |
| 360 | return false; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 361 | if (!conn->vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 362 | return false; |
| 363 | if (!conn->conn_fsm) |
| 364 | return false; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 365 | if (!(conn->conn_fsm->state == SUBSCR_CONN_S_ACCEPTED |
| 366 | || conn->conn_fsm->state == SUBSCR_CONN_S_COMMUNICATING)) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 367 | return false; |
| 368 | return true; |
| 369 | } |
| 370 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 371 | void msc_subscr_conn_communicating(struct gsm_subscriber_connection *conn) |
| 372 | { |
| 373 | OSMO_ASSERT(conn); |
| 374 | osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_COMMUNICATING, |
| 375 | NULL); |
| 376 | } |
| 377 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 378 | void msc_subscr_conn_init(void) |
| 379 | { |
| 380 | osmo_fsm_register(&subscr_conn_fsm); |
| 381 | } |