summaryrefslogtreecommitdiffstats
path: root/src/host/layer23/src/common/sap_interface.c
blob: 0eac8962f7dc0498b98f6b4df53abba077474ead (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* BTSAP socket interface of layer2/3 stack */

/* (C) 2010 by Holger Hans Peter Freyther
 * (C) 2010,2018 by Harald Welte <laforge@gnumonks.org>
 * (C) 2010 by Andreas Eversberg <jolly@eversberg.eu>
 * (C) 2011 by Nico Golde <nico@ngolde.de>
 * (C) 2018 by Vadim Yanitskiy <axilirator@gmail.com>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <unistd.h>
#include <errno.h>

#include <sys/socket.h>

#include <osmocom/core/write_queue.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/fsm.h>

#include <osmocom/bb/common/osmocom_data.h>
#include <osmocom/bb/common/logging.h>

#include <osmocom/bb/common/sap_interface.h>
#include <osmocom/bb/common/sap_proto.h>
#include <osmocom/bb/common/sap_fsm.h>

/*! Send ATR request to the Server.
 * \param[in] ms MS instance with active SAP connection
 * \returns 0 in case of success, negative in case of error
 */
int sap_send_atr_req(struct osmocom_ms *ms)
{
	struct msgb *msg;
	int rc;

	if (!ms->sap_entity.fi) {
		LOGP(DSAP, LOGL_ERROR, "SAP interface is not connected\n");
		return -EAGAIN;
	}

	msg = sap_msgb_alloc(SAP_TRANSFER_ATR_REQ);
	if (!msg)
		return -ENOMEM;

	rc = osmo_fsm_inst_dispatch(ms->sap_entity.fi,
		SAP_TRANSFER_ATR_REQ, msg);
	if (rc) {
		msgb_free(msg);
		return rc;
	}

	return 0;
}

/*! Send APDU request to the Server.
 * \param[in] ms MS instance with active SAP connection
 * \param[in] apdu APDU to be send
 * \param[in] apdu_len length of APDU
 * \returns 0 in case of success, negative in case of error
 */
int sap_send_apdu(struct osmocom_ms *ms, uint8_t *apdu, uint16_t apdu_len)
{
	struct msgb *msg;
	int rc;

	if (!ms->sap_entity.fi) {
		LOGP(DSAP, LOGL_ERROR, "SAP interface is not connected\n");
		return -EAGAIN;
	}

	msg = sap_msgb_alloc(SAP_TRANSFER_APDU_REQ);
	if (!msg)
		return -ENOMEM;

	sap_msgb_add_param(msg, SAP_COMMAND_APDU, apdu_len, apdu);

	rc = osmo_fsm_inst_dispatch(ms->sap_entity.fi,
		SAP_TRANSFER_APDU_REQ, msg);
	if (rc) {
		msgb_free(msg);
		return rc;
	}

	return 0;
}

/*! Send (SIM) reset request to the Server.
 * \param[in] ms MS instance with active SAP connection
 * \returns 0 in case of success, negative in case of error
 */
int sap_send_reset_req(struct osmocom_ms *ms)
{
	struct msgb *msg;
	int rc;

	if (!ms->sap_entity.fi) {
		LOGP(DSAP, LOGL_ERROR, "SAP interface is not connected\n");
		return -EAGAIN;
	}

	msg = sap_msgb_alloc(SAP_RESET_SIM_REQ);
	if (!msg)
		return -ENOMEM;

	rc = osmo_fsm_inst_dispatch(ms->sap_entity.fi,
		SAP_RESET_SIM_REQ, msg);
	if (rc) {
		msgb_free(msg);
		return rc;
	}

	return 0;
}

/*! Send (SIM) power on request to the Server.
 * \param[in] ms MS instance with active SAP connection
 * \returns 0 in case of success, negative in case of error
 */
int sap_send_poweron_req(struct osmocom_ms *ms)
{
	struct msgb *msg;
	int rc;

	if (!ms->sap_entity.fi) {
		LOGP(DSAP, LOGL_ERROR, "SAP interface is not connected\n");
		return -EAGAIN;
	}

	msg = sap_msgb_alloc(SAP_POWER_SIM_ON_REQ);
	if (!msg)
		return -ENOMEM;

	rc = osmo_fsm_inst_dispatch(ms->sap_entity.fi,
		SAP_POWER_SIM_ON_REQ, msg);
	if (rc) {
		msgb_free(msg);
		return rc;
	}

	return 0;
}

/*! Send (SIM) power off request to the Server.
 * \param[in] ms MS instance with active SAP connection
 * \returns 0 in case of success, negative in case of error
 */
int sap_send_poweroff_req(struct osmocom_ms *ms)
{
	struct msgb *msg;
	int rc;

	if (!ms->sap_entity.fi) {
		LOGP(DSAP, LOGL_ERROR, "SAP interface is not connected\n");
		return -EAGAIN;
	}

	msg = sap_msgb_alloc(SAP_POWER_SIM_OFF_REQ);
	if (!msg)
		return -ENOMEM;

	rc = osmo_fsm_inst_dispatch(ms->sap_entity.fi,
		SAP_POWER_SIM_OFF_REQ, msg);
	if (rc) {
		msgb_free(msg);
		return rc;
	}

	return 0;
}

static int sap_read_cb(struct osmo_fd *fd)
{
	struct osmocom_ms *ms = (struct osmocom_ms *) fd->data;
	struct osmosap_entity *sap = &ms->sap_entity;
	uint8_t buf[GSM_SAP_LENGTH];
	struct msgb *msg;
	ssize_t rc;

	/* Prevent buffer overflow */
	OSMO_ASSERT(sap->max_msg_size <= GSM_SAP_LENGTH);

	rc = read(fd->fd, buf, sap->max_msg_size);
	if (rc < 0) {
		LOGP(DSAP, LOGL_ERROR, "SAP socket failed\n");
		rc = -EIO;
		goto conn_error;
	}
	if (rc == 0) {
		LOGP(DSAP, LOGL_NOTICE, "SAP socket closed by server\n");
		rc = -ECONNREFUSED;
		goto conn_error;
	}

	LOGP(DSAP, LOGL_DEBUG, "RX SAP message '%s' (len=%zd): %s\n",
		get_value_string(sap_msg_names, buf[0]),
		rc, osmo_hexdump(buf, rc));

	/* Parse received SAP message and allocate a new msgb */
	msg = sap_msg_parse(buf, rc, sap->max_msg_size);
	if (!msg) {
		LOGP(DSAP, LOGL_ERROR, "Failed to parse SAP message\n");
		return -EINVAL;
	}

	/* Pass parsed message to our FSM using message ID as event */
	rc = osmo_fsm_inst_dispatch(sap->fi, msg->data[0], msg->data);
	if (rc) {
		msgb_free(msg);
		return rc;
	}

	/* Pass to (optional) SAP message handler */
	if (sap->sap_msg_cb)
		sap->sap_msg_cb(ms, msg);
	else
		msgb_free(msg);

	return 0;

conn_error:
	/* Immediately tear-down FSM */
	osmo_fsm_inst_state_chg(sap->fi, SAP_STATE_NOT_CONNECTED, 0, 0);
	return rc;
}

static int sap_write_cb(struct osmo_fd *fd, struct msgb *msg)
{
	ssize_t rc;

	if (fd->fd <= 0)
		return -EINVAL;

	rc = write(fd->fd, msg->data, msg->len);
	if (rc != msg->len) {
		LOGP(DSAP, LOGL_ERROR, "Failed to write data\n");
		return rc;
	}

	LOGP(DSAP, LOGL_DEBUG, "TX SAP message '%s' (len=%u): %s\n",
		get_value_string(sap_msg_names, msg->data[0]),
		msg->len, osmo_hexdump(msg->data, msg->len));

	return 0;
}

/*! Establishes SAP connection to the Server,
 *  allocates SAP FSM, and triggers connection procedure.
 * \param[in] ms MS instance with configured SAP socket path
 * \returns 0 in case of success, negative in case of error
 */
int sap_open(struct osmocom_ms *ms)
{
	int rc;

	LOGP(DSAP, LOGL_INFO, "Establishing SAP connection "
		"(using socket '%s')\n", ms->settings.sap_socket_path);

	rc = osmo_sock_unix_init_ofd(&ms->sap_wq.bfd, SOCK_STREAM, 0,
		ms->settings.sap_socket_path, OSMO_SOCK_F_CONNECT);
	if (rc < 0) {
		LOGP(DSAP, LOGL_ERROR, "Failed to create unix domain socket %s: %s\n",
		     ms->settings.sap_socket_path, strerror(-rc));
		return rc;
	}

	osmo_wqueue_init(&ms->sap_wq, 100);
	ms->sap_wq.bfd.data = ms;
	ms->sap_wq.read_cb = &sap_read_cb;
	ms->sap_wq.write_cb = &sap_write_cb;

	/* Allocate a SAP FSM for a given ms */
	rc = sap_fsm_alloc(ms);
	if (rc) {
		_sap_close_sock(ms);
		return rc;
	}

	/* Initiate SAP connection with Server */
	LOGP(DSAP, LOGL_DEBUG, "Connecting to the Server...\n");
	return osmo_fsm_inst_state_chg(ms->sap_entity.fi, SAP_STATE_CONNECTING,
		SAP_FSM_CONN_EST_TIMEOUT, SAP_FSM_CONN_EST_T);
}

/*! Closes SAP connection with the Server.
 * \param[in] ms MS instance with active SAP connection
 * \returns 0 in case of success, negative in case of error
 */
int sap_close(struct osmocom_ms *ms)
{
	if (ms->sap_entity.fi == NULL) {
		LOGP(DSAP, LOGL_NOTICE, "No active SAP connection (no FSM)\n");
		return -EINVAL;
	}

	LOGP(DSAP, LOGL_INFO, "Closing SAP connection\n");
	return osmo_fsm_inst_dispatch(ms->sap_entity.fi,
		SAP_DISCONNECT_REQ, NULL);
}

/*! Low-level function for closing SAP (socket) connection.
 * \param[in] ms MS instance with active SAP connection
 * \returns 0 in case of success, negative in case of error
 */
int _sap_close_sock(struct osmocom_ms *ms)
{
	if (ms->sap_wq.bfd.fd <= 0)
		return -EINVAL;

	close(ms->sap_wq.bfd.fd);
	ms->sap_wq.bfd.fd = -1;

	osmo_fd_unregister(&ms->sap_wq.bfd);
	osmo_wqueue_clear(&ms->sap_wq);

	return 0;
}

/*! Init SAP client state for a given MS. */
void sap_init(struct osmocom_ms *ms)
{
	struct osmosap_entity *sap = &ms->sap_entity;

	LOGP(DSAP, LOGL_INFO, "init SAP client\n");

	/* Default MaxMsgSize (to be negotiated) */
	sap->max_msg_size = GSM_SAP_LENGTH;
	/* SIM card status is not known yet */
	sap->card_status = SAP_CARD_STATUS_NOT_ACC;
}