aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bsc/lcs_ta_req.c
blob: 3b859a7d353eb1f12464ac4f725b0501179b0546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* Handle LCS BSSLAP TA Request */
/*
 * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * Author: Neels Hofmeyr <neels@hofmeyr.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <osmocom/bsc/lcs_ta_req.h>

#include <osmocom/bsc/lcs_loc_req.h>
#include <osmocom/bsc/lb.h>
#include <osmocom/bsc/gsm_data.h>
#include <osmocom/bsc/paging.h>
#include <osmocom/bsc/bts.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/fsm.h>
#include <osmocom/core/tdef.h>
#include <osmocom/gsm/bsslap.h>

enum lcs_ta_req_fsm_state {
	LCS_TA_REQ_ST_INIT,
	LCS_TA_REQ_ST_WAIT_TA,
	LCS_TA_REQ_ST_GOT_TA,
	LCS_TA_REQ_ST_FAILED,
};

static const struct value_string lcs_ta_req_fsm_event_names[] = {
	OSMO_VALUE_STRING(LCS_TA_REQ_EV_GOT_TA),
	OSMO_VALUE_STRING(LCS_TA_REQ_EV_ABORT),
	{}
};

static const struct osmo_tdef_state_timeout lcs_ta_req_fsm_timeouts[32] = {
	[LCS_TA_REQ_ST_WAIT_TA] = { .T = -12 },
};

/* Transition to a state, using the T timer defined in lcs_ta_req_fsm_timeouts.
 * The actual timeout value is in turn obtained from network->T_defs.
 * Assumes local variable fi exists. */
#define lcs_ta_req_fsm_state_chg(FI, STATE) \
	osmo_tdef_fsm_inst_state_chg(FI, STATE, \
				     lcs_ta_req_fsm_timeouts, \
				     (bsc_gsmnet)->T_defs, \
				     5)

#define lcs_ta_req_fail(cause, fmt, args...) do { \
		LOG_LCS_TA_REQ(lcs_ta_req, LOGL_ERROR, "BSSLAP TA Request failed in state %s: " fmt "\n", \
			       lcs_ta_req ? osmo_fsm_inst_state_name(lcs_ta_req->fi) : "NULL", ## args); \
		lcs_ta_req->failure_cause = cause; \
		lcs_ta_req_fsm_state_chg(lcs_ta_req->fi, LCS_TA_REQ_ST_FAILED); \
	} while(0)

static struct osmo_fsm lcs_ta_req_fsm;

static struct lcs_ta_req *lcs_ta_req_alloc(struct osmo_fsm_inst *parent_fi, uint32_t parent_event_term)
{
	struct lcs_ta_req *lcs_ta_req;

	struct osmo_fsm_inst *fi = osmo_fsm_inst_alloc_child(&lcs_ta_req_fsm, parent_fi, parent_event_term);
	OSMO_ASSERT(fi);

	lcs_ta_req = talloc(fi, struct lcs_ta_req);
	OSMO_ASSERT(lcs_ta_req);
	fi->priv = lcs_ta_req;
	*lcs_ta_req = (struct lcs_ta_req){
		.fi = fi,
	};

	return lcs_ta_req;
}

int lcs_ta_req_start(struct lcs_loc_req *lcs_loc_req)
{
	struct lcs_ta_req *lcs_ta_req;
	if (lcs_loc_req->ta_req) {
		LOG_LCS_TA_REQ(lcs_loc_req->ta_req, LOGL_ERROR,
			       "Cannot start anoter TA Request FSM, this TA Request is still active\n");
		return -ENOTSUP;
	}
	lcs_ta_req = lcs_ta_req_alloc(lcs_loc_req->fi, LCS_LOC_REQ_EV_TA_REQ_END);
	if (!lcs_ta_req) {
		LOG_LCS_LOC_REQ(lcs_loc_req, LOGL_ERROR, "Cannot allocate TA Request FSM");
		return -ENOSPC;
	}
	lcs_ta_req->loc_req = lcs_loc_req;
	lcs_loc_req->ta_req = lcs_ta_req;

	return lcs_ta_req_fsm_state_chg(lcs_ta_req->fi, LCS_TA_REQ_ST_WAIT_TA);
}

static int lcs_ta_req_fsm_timer_cb(struct osmo_fsm_inst *fi)
{
	struct lcs_ta_req *lcs_ta_req = fi->priv;
	lcs_ta_req_fail(LCS_CAUSE_SYSTEM_FAILURE, "Timeout");
	return 1;
}

void lcs_ta_req_wait_ta_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct lcs_ta_req *lcs_ta_req = fi->priv;
	struct lcs_loc_req *loc_req = lcs_ta_req->loc_req;
	struct gsm_lchan *lchan;
	struct bsc_paging_params paging;

	if (osmo_fsm_inst_dispatch(loc_req->fi, LCS_LOC_REQ_EV_TA_REQ_START, lcs_ta_req)) {
		lcs_ta_req_fail(LCS_CAUSE_SYSTEM_FAILURE, "Failed to dispatch LCS_LOC_REQ_EV_TA_REQ_START");
		return;
	}

	paging = (struct bsc_paging_params){
		.reason = BSC_PAGING_FOR_LCS,
		.msc = loc_req->conn->sccp.msc,
		.bsub = loc_req->conn->bsub,
		.tmsi = GSM_RESERVED_TMSI,
		.imsi = loc_req->req.imsi,
		.chan_needed = RSL_CHANNEED_ANY,
	};
	if (paging.bsub)
		bsc_subscr_get(paging.bsub, BSUB_USE_PAGING_START);

	/* Do we already have an active lchan with knowledge of TA? */
	lchan = loc_req->conn->lchan;
	if (lchan) {
		lcs_ta_req_fsm_state_chg(fi, LCS_TA_REQ_ST_GOT_TA);
		return;
	}

	/* No lchan yet, need to start Paging */
	if (loc_req->req.imsi.type != GSM_MI_TYPE_IMSI) {
		lcs_ta_req_fail(LCS_CAUSE_PROTOCOL_ERROR,
				"No IMSI in BSSMAP Location Request and no active lchan, cannot start Paging");
		return;
	}

	if (!loc_req->req.cell_id_present) {
		LOG_LCS_TA_REQ(lcs_ta_req, LOGL_DEBUG,
			       "No Cell Identity in BSSMAP Location Request, paging entire BSS\n");
		paging.cil = (struct gsm0808_cell_id_list2){
			.id_discr = CELL_IDENT_BSS,
		};
	} else {
		paging.cil = (struct gsm0808_cell_id_list2){
			.id_discr = loc_req->req.cell_id.id_discr,
			.id_list = { loc_req->req.cell_id.id },
			.id_list_len = 1,
		};
	}

	bsc_paging_start(&paging);
}

static void lcs_ta_req_wait_ta_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	switch (event) {

	case LCS_TA_REQ_EV_GOT_TA:
		lcs_ta_req_fsm_state_chg(fi, LCS_TA_REQ_ST_GOT_TA);
		break;

	case LCS_TA_REQ_EV_ABORT:
		lcs_ta_req_fsm_state_chg(fi, LCS_TA_REQ_ST_FAILED);
		break;

	default:
		OSMO_ASSERT(false);
	}
}

static int lcs_ta_req_send(struct lcs_ta_req *lcs_ta_req, const struct bssap_le_pdu *bssap_le)
{
	int rc = lb_send(lcs_ta_req->loc_req->conn, bssap_le);
	if (rc)
		lcs_ta_req_fail(LCS_CAUSE_SYSTEM_FAILURE,
				 "Failed to send %s", osmo_bssap_le_pdu_to_str_c(OTC_SELECT, bssap_le));
	return rc;
}

void lcs_ta_req_got_ta_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct lcs_ta_req *lcs_ta_req = fi->priv;
	struct bssap_le_pdu bsslap_ta_resp;
	struct gsm_lchan *lchan = lcs_ta_req->loc_req->conn->lchan;

	if (!lchan) {
		lcs_ta_req_fail(LCS_CAUSE_SYSTEM_FAILURE, "Internal error: no lchan");
		return;
	}

	bsslap_ta_resp = (struct bssap_le_pdu) {
		.discr = BSSAP_LE_MSG_DISCR_BSSMAP_LE,
		.bssmap_le = {
			.msg_type = BSSMAP_LE_MSGT_CONN_ORIENTED_INFO,
			.conn_oriented_info = {
				.apdu = {
					.msg_type = BSSLAP_MSGT_TA_RESPONSE,
					.ta_response = {
						.cell_id = lchan->ts->trx->bts->cell_identity,
						.ta = lchan->last_ta,
					},
				},
			},
		},
	};

	lcs_ta_req_send(lcs_ta_req, &bsslap_ta_resp);
	osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
}

void lcs_ta_req_failed_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct lcs_ta_req *lcs_ta_req = fi->priv;
	struct bssap_le_pdu bsslap_abort;

	bsslap_abort = (struct bssap_le_pdu) {
		.discr = BSSAP_LE_MSG_DISCR_BSSMAP_LE,
		.bssmap_le = {
			.msg_type = BSSMAP_LE_MSGT_CONN_ORIENTED_INFO,
			.conn_oriented_info = {
				.apdu = {
					.msg_type = BSSLAP_MSGT_ABORT,
					.abort = BSSLAP_CAUSE_OTHER_RADIO_EVT_FAIL,
				},
			},
		},
	};

	lcs_ta_req_send(lcs_ta_req, &bsslap_abort);
	osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
}

void lcs_ta_req_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
{
	struct lcs_ta_req *lcs_ta_req = fi->priv;
	if (lcs_ta_req->loc_req->ta_req == lcs_ta_req)
		lcs_ta_req->loc_req->ta_req = NULL;
	/* FSM termination will dispatch LCS_LOC_REQ_EV_TA_REQ_END to the lcs_loc_req FSM */
}


#define S(x)    (1 << (x))

static const struct osmo_fsm_state lcs_ta_req_fsm_states[] = {
	[LCS_TA_REQ_ST_INIT] = {
		.name = "init",
		.out_state_mask = 0
			| S(LCS_TA_REQ_ST_WAIT_TA)
			| S(LCS_TA_REQ_ST_GOT_TA)
			,
	},
	[LCS_TA_REQ_ST_WAIT_TA] = {
		.name = "wait_ta",
		.in_event_mask = 0
			| S(LCS_TA_REQ_EV_GOT_TA)
			| S(LCS_TA_REQ_EV_ABORT)
			,
		.out_state_mask = 0
			| S(LCS_TA_REQ_ST_GOT_TA)
			| S(LCS_TA_REQ_ST_FAILED)
			,
		.onenter = lcs_ta_req_wait_ta_onenter,
		.action = lcs_ta_req_wait_ta_action,
	},
	[LCS_TA_REQ_ST_GOT_TA] = {
		.name = "got_ta",
		.in_event_mask = 0
			,
		.out_state_mask = 0
			,
		.onenter = lcs_ta_req_got_ta_onenter,
	},
	[LCS_TA_REQ_ST_FAILED] = {
		.name = "failed",
		.onenter = lcs_ta_req_failed_onenter,
	},
};

static struct osmo_fsm lcs_ta_req_fsm = {
	.name = "lcs_ta_req",
	.states = lcs_ta_req_fsm_states,
	.num_states = ARRAY_SIZE(lcs_ta_req_fsm_states),
	.log_subsys = DLCS,
	.event_names = lcs_ta_req_fsm_event_names,
	.timer_cb = lcs_ta_req_fsm_timer_cb,
	.cleanup = lcs_ta_req_fsm_cleanup,
};

static __attribute__((constructor)) void lcs_ta_req_fsm_register(void)
{
	OSMO_ASSERT(osmo_fsm_register(&lcs_ta_req_fsm) == 0);
}