blob: bcab8e48cb3d09f924bc85d29b4e4a073ac38e8b [file] [log] [blame]
Harald Weltea183a6e2016-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 Welte7b423ed2016-06-19 18:06:02 +020026#include <osmocom/core/signal.h>
Harald Weltea183a6e2016-06-17 00:06:42 +020027
28#include <openbsc/osmo_msc.h>
29#include <openbsc/vlr.h>
30#include <openbsc/debug.h>
31#include <openbsc/transaction.h>
Harald Welte7b423ed2016-06-19 18:06:02 +020032#include <openbsc/signal.h>
Philipp Maier4b60d072017-04-09 12:32:51 +020033#include <openbsc/a_iface.h>
34
Harald Welte7b423ed2016-06-19 18:06:02 +020035#define SUBSCR_CONN_TIMEOUT 5 /* seconds */
Harald Weltea183a6e2016-06-17 00:06:42 +020036
37static const struct value_string subscr_conn_fsm_event_names[] = {
38 OSMO_VALUE_STRING(SUBSCR_CONN_E_INVALID),
Harald Welte7b423ed2016-06-19 18:06:02 +020039 OSMO_VALUE_STRING(SUBSCR_CONN_E_START),
Harald Weltea183a6e2016-06-17 00:06:42 +020040 OSMO_VALUE_STRING(SUBSCR_CONN_E_ACCEPTED),
Harald Welte7b423ed2016-06-19 18:06:02 +020041 OSMO_VALUE_STRING(SUBSCR_CONN_E_COMMUNICATING),
Harald Weltea183a6e2016-06-17 00:06:42 +020042 OSMO_VALUE_STRING(SUBSCR_CONN_E_BUMP),
43 OSMO_VALUE_STRING(SUBSCR_CONN_E_MO_CLOSE),
44 OSMO_VALUE_STRING(SUBSCR_CONN_E_CN_CLOSE),
Harald Weltea183a6e2016-06-17 00:06:42 +020045 { 0, NULL }
46};
47
48const struct value_string subscr_conn_from_names[] = {
49 OSMO_VALUE_STRING(SUBSCR_CONN_FROM_INVALID),
50 OSMO_VALUE_STRING(SUBSCR_CONN_FROM_LU),
51 OSMO_VALUE_STRING(SUBSCR_CONN_FROM_CM_SERVICE_REQ),
52 OSMO_VALUE_STRING(SUBSCR_CONN_FROM_PAGING_RESP),
53 { 0, NULL }
54};
55
Neels Hofmeyra1756f32016-05-20 21:59:55 +020056static void paging_event(struct gsm_subscriber_connection *conn,
57 enum gsm_paging_event pe)
Harald Weltea183a6e2016-06-17 00:06:42 +020058{
Harald Welte7b423ed2016-06-19 18:06:02 +020059 subscr_paging_dispatch(GSM_HOOK_RR_PAGING, pe, NULL, conn, conn->vsub);
60}
61
62void subscr_conn_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
63{
64 OSMO_ASSERT(event == SUBSCR_CONN_E_START);
65 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_NEW,
66 SUBSCR_CONN_TIMEOUT, 0);
Harald Weltea183a6e2016-06-17 00:06:42 +020067}
68
69void subscr_conn_fsm_new(struct osmo_fsm_inst *fi, uint32_t event, void *data)
70{
71 struct gsm_subscriber_connection *conn = fi->priv;
72 enum subscr_conn_from from = SUBSCR_CONN_FROM_INVALID;
Harald Welte7b423ed2016-06-19 18:06:02 +020073 bool success;
Harald Weltea183a6e2016-06-17 00:06:42 +020074
75 if (data) {
76 from = *(enum subscr_conn_from*)data;
77 LOGPFSM(fi, "%s\n", subscr_conn_from_name(from));
78 }
79
80 /* If accepted, transition the state, all other cases mean failure. */
81 switch (event) {
82 case SUBSCR_CONN_E_ACCEPTED:
Harald Welte7b423ed2016-06-19 18:06:02 +020083 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_ACCEPTED,
84 SUBSCR_CONN_TIMEOUT, 0);
Harald Weltea183a6e2016-06-17 00:06:42 +020085 break;
86
87 case SUBSCR_CONN_E_MO_CLOSE:
88 case SUBSCR_CONN_E_CN_CLOSE:
Neels Hofmeyra1756f32016-05-20 21:59:55 +020089 if (data)
90 LOGPFSM(fi, "Close event, cause %u\n",
91 *(uint32_t*)data);
92 /* will release further below, see
93 * 'if (fi->state != SUBSCR_CONN_S_ACCEPTED)' */
Harald Weltea183a6e2016-06-17 00:06:42 +020094 break;
95
96 default:
Neels Hofmeyra1756f32016-05-20 21:59:55 +020097 LOGPFSML(fi, LOGL_ERROR,
98 "Unexpected event: %d %s\n", event,
99 osmo_fsm_event_name(fi->fsm, event));
Harald Weltea183a6e2016-06-17 00:06:42 +0200100 break;
101 }
102
Harald Welte7b423ed2016-06-19 18:06:02 +0200103 success = (fi->state == SUBSCR_CONN_S_ACCEPTED);
104
105 if (from == SUBSCR_CONN_FROM_LU)
106 rate_ctr_inc(&conn->network->msc_ctrs->ctr[
107 success ? MSC_CTR_LOC_UPDATE_COMPLETED
108 : MSC_CTR_LOC_UPDATE_FAILED]);
109
110 /* signal paging success or failure in case this was a paging */
111 if (from == SUBSCR_CONN_FROM_PAGING_RESP)
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200112 paging_event(conn,
113 success ? GSM_PAGING_SUCCEEDED
114 : GSM_PAGING_EXPIRED);
115
116 /* FIXME rate counters */
117 /*rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_COMPLETED]);*/
Harald Weltea183a6e2016-06-17 00:06:42 +0200118
119 /* On failure, discard the conn */
Harald Welte7b423ed2016-06-19 18:06:02 +0200120 if (!success) {
Harald Weltea183a6e2016-06-17 00:06:42 +0200121 /* TODO: on MO_CLOSE or CN_CLOSE, first go to RELEASING and
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200122 * await BSC/RNC confirmation? */
Harald Weltea183a6e2016-06-17 00:06:42 +0200123 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
124 return;
125 }
126
Harald Weltea183a6e2016-06-17 00:06:42 +0200127 if (from == SUBSCR_CONN_FROM_CM_SERVICE_REQ) {
128 conn->received_cm_service_request = true;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200129 LOGPFSML(fi, LOGL_DEBUG, "received_cm_service_request = true\n");
Harald Weltea183a6e2016-06-17 00:06:42 +0200130 }
131
132 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_BUMP, data);
133}
134
Harald Weltea183a6e2016-06-17 00:06:42 +0200135static void subscr_conn_fsm_bump(struct osmo_fsm_inst *fi, uint32_t event, void *data)
136{
137 struct gsm_subscriber_connection *conn = fi->priv;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200138 struct gsm_trans *trans;
Harald Weltea183a6e2016-06-17 00:06:42 +0200139
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200140 if (conn->silent_call) {
141 LOGPFSML(fi, LOGL_DEBUG, "bump: silent call still active\n");
Harald Weltea183a6e2016-06-17 00:06:42 +0200142 return;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200143 }
Harald Weltea183a6e2016-06-17 00:06:42 +0200144
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200145 if (conn->received_cm_service_request) {
146 LOGPFSML(fi, LOGL_DEBUG, "bump: still awaiting first request after a CM Service Request\n");
Harald Weltea183a6e2016-06-17 00:06:42 +0200147 return;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200148 }
Harald Weltea183a6e2016-06-17 00:06:42 +0200149
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200150 if (conn->vsub && !llist_empty(&conn->vsub->cs.requests)) {
151 struct subscr_request *sr;
152 if (!log_check_level(fi->fsm->log_subsys, LOGL_DEBUG)) {
153 llist_for_each_entry(sr, &conn->vsub->cs.requests, entry) {
154 LOGPFSML(fi, LOGL_DEBUG, "bump: still active: %s\n",
155 sr->label);
156 }
157 }
Harald Weltea183a6e2016-06-17 00:06:42 +0200158 return;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200159 }
Harald Weltea183a6e2016-06-17 00:06:42 +0200160
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200161 if ((trans = trans_has_conn(conn))) {
162 LOGPFSML(fi, LOGL_DEBUG,
163 "bump: connection still has active transaction: %s\n",
164 gsm48_pdisc_name(trans->protocol));
Harald Weltea183a6e2016-06-17 00:06:42 +0200165 return;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200166 }
Harald Weltea183a6e2016-06-17 00:06:42 +0200167
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200168 LOGPFSML(fi, LOGL_DEBUG, "bump: releasing conn\n");
Harald Weltea183a6e2016-06-17 00:06:42 +0200169 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
170}
171
Harald Welte7b423ed2016-06-19 18:06:02 +0200172static void subscr_conn_fsm_accepted_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
173{
174 struct gsm_subscriber_connection *conn = fi->priv;
175 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, conn->vsub);
176}
177
Harald Weltea183a6e2016-06-17 00:06:42 +0200178static void subscr_conn_fsm_accepted(struct osmo_fsm_inst *fi, uint32_t event, void *data)
179{
180 switch (event) {
Harald Welte7b423ed2016-06-19 18:06:02 +0200181 case SUBSCR_CONN_E_COMMUNICATING:
182 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_COMMUNICATING, 0, 0);
183 return;
184
Harald Weltea183a6e2016-06-17 00:06:42 +0200185 case SUBSCR_CONN_E_BUMP:
186 subscr_conn_fsm_bump(fi, event, data);
187 return;
188
189 default:
190 break;
191 }
192 /* Whatever unexpected happens in the accepted state, it means release.
193 * Even if an unexpected event is passed, the safest thing to do is
194 * discard the conn. We don't expect another SUBSCR_CONN_E_ACCEPTED. */
195 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
196}
197
Harald Welte7b423ed2016-06-19 18:06:02 +0200198static void subscr_conn_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
199{
200 switch (event) {
201 case SUBSCR_CONN_E_COMMUNICATING:
202 /* no-op */
203 return;
204
205 case SUBSCR_CONN_E_BUMP:
206 subscr_conn_fsm_bump(fi, event, data);
207 return;
208
209 default:
210 break;
211 }
212 /* Whatever unexpected happens in the accepted state, it means release.
213 * Even if an unexpected event is passed, the safest thing to do is
214 * discard the conn. We don't expect another SUBSCR_CONN_E_ACCEPTED. */
215 osmo_fsm_inst_state_chg(fi, SUBSCR_CONN_S_RELEASED, 0, 0);
216}
217
218static void subscr_conn_fsm_cleanup(struct osmo_fsm_inst *fi,
219 enum osmo_fsm_term_cause cause)
Harald Weltea183a6e2016-06-17 00:06:42 +0200220{
221 struct gsm_subscriber_connection *conn = fi->priv;
Harald Welte7b423ed2016-06-19 18:06:02 +0200222 fi->priv = NULL;
223
Harald Weltea183a6e2016-06-17 00:06:42 +0200224 if (!conn)
225 return;
Harald Welte7b423ed2016-06-19 18:06:02 +0200226 conn->conn_fsm = NULL;
227 msc_subscr_conn_close(conn, cause);
228 msc_subscr_conn_put(conn);
229}
Harald Weltea183a6e2016-06-17 00:06:42 +0200230
Harald Welte7b423ed2016-06-19 18:06:02 +0200231int subscr_conn_fsm_timeout(struct osmo_fsm_inst *fi)
232{
233 struct gsm_subscriber_connection *conn = fi->priv;
234 if (conn)
235 vlr_subscr_conn_timeout(conn->vsub);
236 osmo_fsm_inst_dispatch(fi, SUBSCR_CONN_E_CN_CLOSE, NULL);
237 return 0;
238}
239
240static void subscr_conn_fsm_release(struct osmo_fsm_inst *fi, uint32_t prev_state)
241{
242 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
Harald Weltea183a6e2016-06-17 00:06:42 +0200243}
244
245#define S(x) (1 << (x))
246
247static const struct osmo_fsm_state subscr_conn_fsm_states[] = {
Harald Welte7b423ed2016-06-19 18:06:02 +0200248 [SUBSCR_CONN_S_INIT] = {
249 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_INIT),
250 .in_event_mask = S(SUBSCR_CONN_E_START),
251 .out_state_mask = S(SUBSCR_CONN_S_NEW),
252 .action = subscr_conn_fsm_init,
253 },
Harald Weltea183a6e2016-06-17 00:06:42 +0200254 [SUBSCR_CONN_S_NEW] = {
255 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_NEW),
256 .in_event_mask = S(SUBSCR_CONN_E_ACCEPTED) |
257 S(SUBSCR_CONN_E_MO_CLOSE) |
Harald Welte7b423ed2016-06-19 18:06:02 +0200258 S(SUBSCR_CONN_E_CN_CLOSE),
Harald Weltea183a6e2016-06-17 00:06:42 +0200259 .out_state_mask = S(SUBSCR_CONN_S_ACCEPTED) |
260 S(SUBSCR_CONN_S_RELEASED),
261 .action = subscr_conn_fsm_new,
262 },
263 [SUBSCR_CONN_S_ACCEPTED] = {
264 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_ACCEPTED),
265 /* allow everything to release for any odd behavior */
Harald Welte7b423ed2016-06-19 18:06:02 +0200266 .in_event_mask = S(SUBSCR_CONN_E_COMMUNICATING) |
267 S(SUBSCR_CONN_E_BUMP) |
268 S(SUBSCR_CONN_E_ACCEPTED) |
Harald Weltea183a6e2016-06-17 00:06:42 +0200269 S(SUBSCR_CONN_E_MO_CLOSE) |
Harald Welte7b423ed2016-06-19 18:06:02 +0200270 S(SUBSCR_CONN_E_CN_CLOSE),
271 .out_state_mask = S(SUBSCR_CONN_S_RELEASED) |
272 S(SUBSCR_CONN_S_COMMUNICATING),
273 .onenter = subscr_conn_fsm_accepted_enter,
Harald Weltea183a6e2016-06-17 00:06:42 +0200274 .action = subscr_conn_fsm_accepted,
275 },
Harald Welte7b423ed2016-06-19 18:06:02 +0200276 [SUBSCR_CONN_S_COMMUNICATING] = {
277 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_COMMUNICATING),
278 /* allow everything to release for any odd behavior */
279 .in_event_mask = S(SUBSCR_CONN_E_BUMP) |
280 S(SUBSCR_CONN_E_ACCEPTED) |
281 S(SUBSCR_CONN_E_COMMUNICATING) |
282 S(SUBSCR_CONN_E_MO_CLOSE) |
283 S(SUBSCR_CONN_E_CN_CLOSE),
284 .out_state_mask = S(SUBSCR_CONN_S_RELEASED),
285 .action = subscr_conn_fsm_communicating,
286 },
Harald Weltea183a6e2016-06-17 00:06:42 +0200287 [SUBSCR_CONN_S_RELEASED] = {
288 .name = OSMO_STRINGIFY(SUBSCR_CONN_S_RELEASED),
289 .onenter = subscr_conn_fsm_release,
290 },
291};
292
293static struct osmo_fsm subscr_conn_fsm = {
294 .name = "Subscr_Conn",
295 .states = subscr_conn_fsm_states,
296 .num_states = ARRAY_SIZE(subscr_conn_fsm_states),
297 .allstate_event_mask = 0,
298 .allstate_action = NULL,
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200299 .log_subsys = DMM,
Harald Weltea183a6e2016-06-17 00:06:42 +0200300 .event_names = subscr_conn_fsm_event_names,
Harald Welte7b423ed2016-06-19 18:06:02 +0200301 .cleanup = subscr_conn_fsm_cleanup,
302 .timer_cb = subscr_conn_fsm_timeout,
Harald Weltea183a6e2016-06-17 00:06:42 +0200303};
304
305int msc_create_conn_fsm(struct gsm_subscriber_connection *conn, const char *id)
306{
307 struct osmo_fsm_inst *fi;
308 OSMO_ASSERT(conn);
309
310 if (conn->conn_fsm) {
311 LOGP(DMM, LOGL_ERROR,
312 "%s: Error: connection already in use\n", id);
313 return -EINVAL;
314 }
315
Harald Welte7b423ed2016-06-19 18:06:02 +0200316 /* Allocate the FSM not with the subscr_conn. Semantically it would
317 * make sense, but in subscr_conn_fsm_cleanup(), we want to discard the
318 * subscriber connection. If the FSM is freed along with the subscriber
319 * connection, then in _osmo_fsm_inst_term() the osmo_fsm_inst_free()
320 * that follows the cleanup() call would run into a double free. */
321 fi = osmo_fsm_inst_alloc(&subscr_conn_fsm, conn->network,
322 msc_subscr_conn_get(conn),
323 LOGL_DEBUG, id);
Harald Weltea183a6e2016-06-17 00:06:42 +0200324
325 if (!fi) {
326 LOGP(DMM, LOGL_ERROR,
327 "%s: Failed to allocate subscr conn master FSM\n", id);
328 return -ENOMEM;
329 }
330 conn->conn_fsm = fi;
Harald Welte7b423ed2016-06-19 18:06:02 +0200331 osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_START, NULL);
Harald Weltea183a6e2016-06-17 00:06:42 +0200332 return 0;
333}
334
335bool msc_subscr_conn_is_accepted(struct gsm_subscriber_connection *conn)
336{
337 if (!conn)
338 return false;
Harald Welte7b423ed2016-06-19 18:06:02 +0200339 if (!conn->vsub)
Harald Weltea183a6e2016-06-17 00:06:42 +0200340 return false;
341 if (!conn->conn_fsm)
342 return false;
Harald Welte7b423ed2016-06-19 18:06:02 +0200343 if (!(conn->conn_fsm->state == SUBSCR_CONN_S_ACCEPTED
344 || conn->conn_fsm->state == SUBSCR_CONN_S_COMMUNICATING))
Harald Weltea183a6e2016-06-17 00:06:42 +0200345 return false;
346 return true;
347}
348
Harald Welte7b423ed2016-06-19 18:06:02 +0200349void msc_subscr_conn_communicating(struct gsm_subscriber_connection *conn)
350{
351 OSMO_ASSERT(conn);
352 osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_COMMUNICATING,
353 NULL);
354}
355
Harald Weltea183a6e2016-06-17 00:06:42 +0200356void msc_subscr_conn_init(void)
357{
358 osmo_fsm_register(&subscr_conn_fsm);
359}