blob: 512f5128e5d0a5322cc195fcc81e13674aa746c2 [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* 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 Welte2483f1b2016-06-19 18:06:02 +020026#include <osmocom/core/signal.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020027
Neels Hofmeyr90843962017-09-04 15:04:35 +020028#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 Willmann4e825b62018-02-15 10:33:26 +010034#include <osmocom/msc/iucs.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020035
Harald Welte2483f1b2016-06-19 18:06:02 +020036#define SUBSCR_CONN_TIMEOUT 5 /* seconds */
Harald Welteb8b85a12016-06-17 00:06:42 +020037
38static const struct value_string subscr_conn_fsm_event_names[] = {
39 OSMO_VALUE_STRING(SUBSCR_CONN_E_INVALID),
Harald Welte2483f1b2016-06-19 18:06:02 +020040 OSMO_VALUE_STRING(SUBSCR_CONN_E_START),
Harald Welteb8b85a12016-06-17 00:06:42 +020041 OSMO_VALUE_STRING(SUBSCR_CONN_E_ACCEPTED),
Harald Welte2483f1b2016-06-19 18:06:02 +020042 OSMO_VALUE_STRING(SUBSCR_CONN_E_COMMUNICATING),
Harald Welteb8b85a12016-06-17 00:06:42 +020043 OSMO_VALUE_STRING(SUBSCR_CONN_E_BUMP),
44 OSMO_VALUE_STRING(SUBSCR_CONN_E_MO_CLOSE),
45 OSMO_VALUE_STRING(SUBSCR_CONN_E_CN_CLOSE),
Harald Welteb8b85a12016-06-17 00:06:42 +020046 { 0, NULL }
47};
48
49const 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 Hofmeyr84da6b12016-05-20 21:59:55 +020057static void paging_event(struct gsm_subscriber_connection *conn,
58 enum gsm_paging_event pe)
Harald Welteb8b85a12016-06-17 00:06:42 +020059{
Harald Welte2483f1b2016-06-19 18:06:02 +020060 subscr_paging_dispatch(GSM_HOOK_RR_PAGING, pe, NULL, conn, conn->vsub);
61}
62
63void 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 Welteb8b85a12016-06-17 00:06:42 +020068}
69
70void 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 Welte2483f1b2016-06-19 18:06:02 +020074 bool success;
Harald Welteb8b85a12016-06-17 00:06:42 +020075
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 Welte2483f1b2016-06-19 18:06:02 +020084 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED,
85 SUBSCR_CONN_TIMEOUT, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +020086 break;
87
88 case SUBSCR_CONN_E_MO_CLOSE:
89 case SUBSCR_CONN_E_CN_CLOSE:
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020090 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 Welteb8b85a12016-06-17 00:06:42 +020095 break;
96
97 default:
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020098 LOGPFSML(fi, LOGL_ERROR,
99 "Unexpected event: %d %s\n", event,
100 osmo_fsm_event_name(fi->fsm, event));
Harald Welteb8b85a12016-06-17 00:06:42 +0200101 break;
102 }
103
Harald Welte2483f1b2016-06-19 18:06:02 +0200104 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 Hofmeyr84da6b12016-05-20 21:59:55 +0200113 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 Welteb8b85a12016-06-17 00:06:42 +0200119
120 /* On failure, discard the conn */
Harald Welte2483f1b2016-06-19 18:06:02 +0200121 if (!success) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200122 /* TODO: on MO_CLOSE or CN_CLOSE, first go to RELEASING and
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200123 * await BSC/RNC confirmation? */
Harald Welteb8b85a12016-06-17 00:06:42 +0200124 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
125 return;
126 }
127
Harald Welteb8b85a12016-06-17 00:06:42 +0200128 if (from == SUBSCR_CONN_FROM_CM_SERVICE_REQ) {
129 conn->received_cm_service_request = true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200130 LOGPFSML(fi, LOGL_DEBUG, "received_cm_service_request = true\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200131 }
132
133 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_BUMP, data);
134}
135
Harald Welteb8b85a12016-06-17 00:06:42 +0200136static void subscr_conn_fsm_bump(struct osmo_fsm_inst *fi, uint32_t event, void *data)
137{
138 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200139 struct gsm_trans *trans;
Harald Welteb8b85a12016-06-17 00:06:42 +0200140
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200141 if (conn->silent_call) {
142 LOGPFSML(fi, LOGL_DEBUG, "bump: silent call still active\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200143 return;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200144 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200145
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200146 if (conn->received_cm_service_request) {
147 LOGPFSML(fi, LOGL_DEBUG, "bump: still awaiting first request after a CM Service Request\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200148 return;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200149 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200150
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200151 if (conn->vsub && !llist_empty(&conn->vsub->cs.requests)) {
152 struct subscr_request *sr;
153 if (!log_check_level(fi->fsm->log_subsys, LOGL_DEBUG)) {
154 llist_for_each_entry(sr, &conn->vsub->cs.requests, entry) {
155 LOGPFSML(fi, LOGL_DEBUG, "bump: still active: %s\n",
156 sr->label);
157 }
158 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200159 return;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200160 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200161
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200162 if ((trans = trans_has_conn(conn))) {
163 LOGPFSML(fi, LOGL_DEBUG,
164 "bump: connection still has active transaction: %s\n",
165 gsm48_pdisc_name(trans->protocol));
Harald Welteb8b85a12016-06-17 00:06:42 +0200166 return;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200167 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200168
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200169 LOGPFSML(fi, LOGL_DEBUG, "bump: releasing conn\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200170 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
171}
172
Harald Welte2483f1b2016-06-19 18:06:02 +0200173static void subscr_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
174{
175 struct gsm_subscriber_connection *conn = fi->priv;
176 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, conn->vsub);
177}
178
Harald Welteb8b85a12016-06-17 00:06:42 +0200179static void subscr_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data)
180{
181 switch (event) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200182 case SUBSCR_CONN_E_COMMUNICATING:
183 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_COMMUNICATING, 0, 0);
184 return;
185
Harald Welteb8b85a12016-06-17 00:06:42 +0200186 case SUBSCR_CONN_E_BUMP:
187 subscr_conn_fsm_bump(fi, event, data);
188 return;
189
190 default:
191 break;
192 }
193 /* Whatever unexpected happens in the accepted state, it means release.
194 * Even if an unexpected event is passed, the safest thing to do is
195 * discard the conn. We don't expect another SUBSCR_CONN_E_ACCEPTED. */
196 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
197}
198
Harald Welte2483f1b2016-06-19 18:06:02 +0200199static void subscr_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
200{
201 switch (event) {
202 case SUBSCR_CONN_E_COMMUNICATING:
203 /* no-op */
204 return;
205
206 case SUBSCR_CONN_E_BUMP:
207 subscr_conn_fsm_bump(fi, event, data);
208 return;
209
210 default:
211 break;
212 }
213 /* Whatever unexpected happens in the accepted state, it means release.
214 * Even if an unexpected event is passed, the safest thing to do is
215 * discard the conn. We don't expect another SUBSCR_CONN_E_ACCEPTED. */
216 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
217}
218
219static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi,
220 enum osmo_fsm_term_cause cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200221{
222 struct gsm_subscriber_connection *conn = fi->priv;
Harald Welte2483f1b2016-06-19 18:06:02 +0200223 fi->priv = NULL;
224
Harald Welteb8b85a12016-06-17 00:06:42 +0200225 if (!conn)
226 return;
Harald Welte2483f1b2016-06-19 18:06:02 +0200227 conn->conn_fsm = NULL;
228 msc_subscr_conn_close(conn, cause);
Neels Hofmeyr6166f292017-11-22 14:33:12 +0100229 msc_subscr_conn_put(conn, MSC_CONN_USE_FSM);
Harald Welte2483f1b2016-06-19 18:06:02 +0200230}
Harald Welteb8b85a12016-06-17 00:06:42 +0200231
Harald Welte2483f1b2016-06-19 18:06:02 +0200232int subscr_conn_fsm_timeout(struct osmo_fsm_inst *fi)
233{
234 struct gsm_subscriber_connection *conn = fi->priv;
235 if (conn)
236 vlr_subscr_conn_timeout(conn->vsub);
Neels Hofmeyr3ddd7422017-11-18 23:25:41 +0100237 else
238 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_CN_CLOSE, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +0200239 return 0;
240}
241
242static void subscr_conn_fsm_release(struct osmo_fsm_inst *fi, uint32_t prev_state)
243{
244 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200245}
246
247#define S(x) (1 << (x))
248
249static const struct osmo_fsm_state subscr_conn_fsm_states[] = {
Harald Welte2483f1b2016-06-19 18:06:02 +0200250 [SUBSCR_CONN_S_INIT] = {
251 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_INIT),
252 .in_event_mask = S(SUBSCR_CONN_E_START),
253 .out_state_mask = S(SUBSCR_CONN_S_NEW),
254 .action = subscr_conn_fsm_init,
255 },
Harald Welteb8b85a12016-06-17 00:06:42 +0200256 [SUBSCR_CONN_S_NEW] = {
257 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_NEW),
258 .in_event_mask = S(SUBSCR_CONN_E_ACCEPTED) |
259 S(SUBSCR_CONN_E_MO_CLOSE) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200260 S(SUBSCR_CONN_E_CN_CLOSE),
Harald Welteb8b85a12016-06-17 00:06:42 +0200261 .out_state_mask = S(SUBSCR_CONN_S_ACCEPTED) |
262 S(SUBSCR_CONN_S_RELEASED),
263 .action = subscr_conn_fsm_new,
264 },
265 [SUBSCR_CONN_S_ACCEPTED] = {
266 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_ACCEPTED),
267 /* allow everything to release for any odd behavior */
Harald Welte2483f1b2016-06-19 18:06:02 +0200268 .in_event_mask = S(SUBSCR_CONN_E_COMMUNICATING) |
269 S(SUBSCR_CONN_E_BUMP) |
270 S(SUBSCR_CONN_E_ACCEPTED) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200271 S(SUBSCR_CONN_E_MO_CLOSE) |
Harald Welte2483f1b2016-06-19 18:06:02 +0200272 S(SUBSCR_CONN_E_CN_CLOSE),
273 .out_state_mask = S(SUBSCR_CONN_S_RELEASED) |
274 S(SUBSCR_CONN_S_COMMUNICATING),
275 .onenter = subscr_conn_fsm_accepted_enter,
Harald Welteb8b85a12016-06-17 00:06:42 +0200276 .action = subscr_conn_fsm_accepted,
277 },
Harald Welte2483f1b2016-06-19 18:06:02 +0200278 [SUBSCR_CONN_S_COMMUNICATING] = {
279 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_COMMUNICATING),
280 /* allow everything to release for any odd behavior */
281 .in_event_mask = S(SUBSCR_CONN_E_BUMP) |
282 S(SUBSCR_CONN_E_ACCEPTED) |
283 S(SUBSCR_CONN_E_COMMUNICATING) |
284 S(SUBSCR_CONN_E_MO_CLOSE) |
285 S(SUBSCR_CONN_E_CN_CLOSE),
286 .out_state_mask = S(SUBSCR_CONN_S_RELEASED),
287 .action = subscr_conn_fsm_communicating,
288 },
Harald Welteb8b85a12016-06-17 00:06:42 +0200289 [SUBSCR_CONN_S_RELEASED] = {
290 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASED),
291 .onenter = subscr_conn_fsm_release,
292 },
293};
294
295static struct osmo_fsm subscr_conn_fsm = {
296 .name = "Subscr_Conn",
297 .states = subscr_conn_fsm_states,
298 .num_states = ARRAY_SIZE(subscr_conn_fsm_states),
299 .allstate_event_mask = 0,
300 .allstate_action = NULL,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200301 .log_subsys = DMM,
Harald Welteb8b85a12016-06-17 00:06:42 +0200302 .event_names = subscr_conn_fsm_event_names,
Harald Welte2483f1b2016-06-19 18:06:02 +0200303 .cleanup = subscr_conn_fsm_cleanup,
304 .timer_cb = subscr_conn_fsm_timeout,
Harald Welteb8b85a12016-06-17 00:06:42 +0200305};
306
Daniel Willmann4e825b62018-02-15 10:33:26 +0100307char *msc_subscr_conn_get_conn_id(struct gsm_subscriber_connection *conn)
308{
309 char *id;
310
311 switch (conn->via_ran) {
312 case RAN_GERAN_A:
313 id = talloc_asprintf(conn, "GERAN_A-%08x", conn->a.conn_id);
314 break;
315 case RAN_UTRAN_IU:
316 id = talloc_asprintf(conn, "UTRAN_IU-%08x", iu_get_conn_id(conn->iu.ue_ctx));
317 break;
318 default:
319 LOGP(DMM, LOGL_ERROR, "RAN of conn %p unknown!\n", conn);
320 return NULL;
321 }
322
323 return id;
324}
325
Harald Welteb8b85a12016-06-17 00:06:42 +0200326int msc_create_conn_fsm(struct gsm_subscriber_connection *conn, const char *id)
327{
328 struct osmo_fsm_inst *fi;
329 OSMO_ASSERT(conn);
330
331 if (conn->conn_fsm) {
332 LOGP(DMM, LOGL_ERROR,
333 "%s: Error: connection already in use\n", id);
334 return -EINVAL;
335 }
336
Harald Welte2483f1b2016-06-19 18:06:02 +0200337 /* Allocate the FSM not with the subscr_conn. Semantically it would
338 * make sense, but in subscr_conn_fsm_cleanup(), we want to discard the
339 * subscriber connection. If the FSM is freed along with the subscriber
340 * connection, then in _osmo_fsm_inst_term() the osmo_fsm_inst_free()
341 * that follows the cleanup() call would run into a double free. */
342 fi = osmo_fsm_inst_alloc(&subscr_conn_fsm, conn->network,
Neels Hofmeyr6166f292017-11-22 14:33:12 +0100343 msc_subscr_conn_get(conn, MSC_CONN_USE_FSM),
Harald Welte2483f1b2016-06-19 18:06:02 +0200344 LOGL_DEBUG, id);
Harald Welteb8b85a12016-06-17 00:06:42 +0200345
346 if (!fi) {
347 LOGP(DMM, LOGL_ERROR,
348 "%s: Failed to allocate subscr conn master FSM\n", id);
349 return -ENOMEM;
350 }
351 conn->conn_fsm = fi;
Harald Welte2483f1b2016-06-19 18:06:02 +0200352 osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_START, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200353 return 0;
354}
355
Maxd83b17b2018-02-06 16:51:31 +0100356bool msc_subscr_conn_is_accepted(const struct gsm_subscriber_connection *conn)
Harald Welteb8b85a12016-06-17 00:06:42 +0200357{
358 if (!conn)
359 return false;
Harald Welte2483f1b2016-06-19 18:06:02 +0200360 if (!conn->vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200361 return false;
362 if (!conn->conn_fsm)
363 return false;
Harald Welte2483f1b2016-06-19 18:06:02 +0200364 if (!(conn->conn_fsm->state == SUBSCR_CONN_S_ACCEPTED
365 || conn->conn_fsm->state == SUBSCR_CONN_S_COMMUNICATING))
Harald Welteb8b85a12016-06-17 00:06:42 +0200366 return false;
367 return true;
368}
369
Harald Welte2483f1b2016-06-19 18:06:02 +0200370void msc_subscr_conn_communicating(struct gsm_subscriber_connection *conn)
371{
372 OSMO_ASSERT(conn);
373 osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_COMMUNICATING,
374 NULL);
375}
376
Harald Welteb8b85a12016-06-17 00:06:42 +0200377void msc_subscr_conn_init(void)
378{
379 osmo_fsm_register(&subscr_conn_fsm);
380}