blob: e495f102b0744ad1c884256d5dd3a8349a6aa74b [file] [log] [blame]
Harald Welte1b6696f2020-02-16 15:27:15 +01001/* (C) 2018-2020 by Harald Welte <laforge@gnumonks.org>
Harald Welte3dcdd202019-03-09 13:06:46 +01002 * (C) 2018 by sysmocom - s.f.m.c. GmbH, Author: Kevin Redon
3 *
4 * All Rights Reserved
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Kévin Redone0b837c2018-10-10 00:39:25 +020024
25#include <errno.h>
26#include <string.h>
27
28#include <talloc.h>
29
30#include <osmocom/core/msgb.h>
31#include <osmocom/core/fsm.h>
32#include <osmocom/core/utils.h>
33#include <osmocom/core/logging.h>
34#include <osmocom/core/application.h>
Harald Welte55c7f442019-12-17 01:14:43 +010035#include <osmocom/core/exec.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020036
37#include <osmocom/abis/ipa.h>
38#include <osmocom/gsm/protocol/ipaccess.h>
39
40#include "rspro_util.h"
41#include "client.h"
Harald Welte61d98e92019-03-03 15:43:07 +010042#include "debug.h"
Kévin Redone0b837c2018-10-10 00:39:25 +020043
44#include <unistd.h>
45#include <stdio.h>
Kévin Redon3428e412018-10-11 19:14:00 +020046#include <linux/limits.h>
47#include <sys/stat.h>
48#include <fcntl.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020049#include <signal.h>
50#include <getopt.h>
51
52#include <libusb.h>
53
Harald Weltefd5dafc2019-12-15 21:14:14 +010054#include <osmocom/usb/libusb.h>
55#include <osmocom/simtrace2/simtrace_prot.h>
56#include <osmocom/simtrace2/simtrace_usb.h>
57#include <osmocom/simtrace2/apdu_dispatch.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020058
59#include <osmocom/core/gsmtap.h>
60#include <osmocom/core/gsmtap_util.h>
61#include <osmocom/core/utils.h>
62#include <osmocom/core/socket.h>
63#include <osmocom/core/msgb.h>
64#include <osmocom/sim/class_tables.h>
65#include <osmocom/sim/sim.h>
66
67/* transport to a SIMtrace device */
68struct st_transport {
69 /* USB */
70 struct libusb_device_handle *usb_devh;
71 struct {
72 uint8_t in;
73 uint8_t out;
74 uint8_t irq_in;
75 } usb_ep;
Kévin Redone0b837c2018-10-10 00:39:25 +020076};
77
78/* a SIMtrace slot; communicates over a transport */
79struct st_slot {
80 /* transport through which the slot can be reached */
81 struct st_transport *transp;
82 /* number of the slot within the transport */
83 uint8_t slot_nr;
84};
85
86/* One istance of card emulation */
87struct cardem_inst {
88 /* slot on which this card emulation instance runs */
89 struct st_slot *slot;
Harald Welte943ae622019-12-17 01:12:56 +010090 struct cardemu_usb_msg_status last_status;
Harald Welte499e1d92019-12-17 01:13:53 +010091 char *usb_path;
Kévin Redone0b837c2018-10-10 00:39:25 +020092};
93
94/* global GSMTAP instance */
95static struct gsmtap_inst *g_gti;
96
Harald Welte1200c822020-02-13 20:43:27 +010097struct bankd_client *g_client;
Kévin Redone0b837c2018-10-10 00:39:25 +020098static void *g_tall_ctx;
99void __thread *talloc_asn1_ctx;
100int asn_debug;
101
Harald Weltea9bc4de2020-02-16 15:40:21 +0100102/* should we leave main loop processing? */
103bool g_leave_main = false;
104
Harald Welte3f09f632019-03-31 15:51:13 +0200105__attribute__((unused)) static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200106{
107 struct gsmtap_hdr *gh;
108 unsigned int gross_len = len + sizeof(*gh);
109 uint8_t *buf = malloc(gross_len);
110 int rc;
111
112 if (!buf)
113 return -ENOMEM;
114
115 memset(buf, 0, sizeof(*gh));
116 gh = (struct gsmtap_hdr *) buf;
117 gh->version = GSMTAP_VERSION;
118 gh->hdr_len = sizeof(*gh)/4;
119 gh->type = GSMTAP_TYPE_SIM;
120
121 memcpy(buf + sizeof(*gh), apdu, len);
122
123 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
124 if (rc < 0) {
125 perror("write gsmtap");
126 free(buf);
127 return rc;
128 }
129
130 free(buf);
131 return 0;
132}
133
Harald Welte55c7f442019-12-17 01:14:43 +0100134/* build the (additional) environment for executing a script */
135static char **build_script_env(struct bankd_client *clnt, const char *cause)
136{
137 struct cardem_inst *ci = clnt->cardem;
138 char **env = talloc_zero_size(clnt, 256*sizeof(char *));
139 int i = 0;
140
141 if (!env)
142 return NULL;
143
144 env[i++] = talloc_asprintf(env, "REMSIM_CLIENT_VERSION=%s", VERSION);
145
146 env[i++] = talloc_asprintf(env, "REMSIM_SERVER_ADDR=%s:%u",
147 clnt->srv_conn.server_host, clnt->srv_conn.server_port);
148 env[i++] = talloc_asprintf(env, "REMSIM_SERVER_STATE=%s",
149 osmo_fsm_inst_state_name(clnt->srv_conn.fi));
150
151 env[i++] = talloc_asprintf(env, "REMSIM_BANKD_ADDR=%s:%u",
152 clnt->bankd_conn.server_host, clnt->bankd_conn.server_port);
153 env[i++] = talloc_asprintf(env, "REMSIM_BANKD_STATE=%s",
154 osmo_fsm_inst_state_name(clnt->bankd_conn.fi));
155
156
157 if (clnt->srv_conn.clslot) {
158 env[i++] = talloc_asprintf(env, "REMSIM_CLIENT_SLOT=%lu:%lu",
159 g_client->srv_conn.clslot->clientId,
160 g_client->srv_conn.clslot->slotNr);
161 }
162 env[i++] = talloc_asprintf(env, "REMSIM_BANKD_SLOT=%u:%u",
163 clnt->bankd_slot.bank_id, clnt->bankd_slot.slot_nr);
164
165 env[i++] = talloc_asprintf(env, "REMSIM_USB_PATH=%s", ci->usb_path);
166 env[i++] = talloc_asprintf(env, "REMSIM_USB_INTERFACE=%u", clnt->cfg->usb.if_num);
167
168 /* TODO: SIM card state VCC/CLK/RST */
169 env[i++] = talloc_asprintf(env, "REMSIM_SIM_VCC=%u",
170 !!(ci->last_status.flags & CEMU_STATUS_F_VCC_PRESENT));
171 env[i++] = talloc_asprintf(env, "REMSIM_SIM_RST=%u",
172 !!(ci->last_status.flags & CEMU_STATUS_F_RESET_ACTIVE));
173
174 env[i++] = talloc_asprintf(env, "REMSIM_CAUSE=%s", cause);
175
176 /* terminate last entry */
177 env[i++] = NULL;
178 return env;
179}
180
181static int call_script(struct bankd_client *clnt, const char *cause)
182{
183 char **env, *cmd;
184 int rc;
185
186 if (!clnt->cfg->event_script)
187 return 0;
188
189 env = build_script_env(clnt, cause);
190 if (!env)
191 return -ENOMEM;
192
193 cmd = talloc_asprintf(env, "%s %s", clnt->cfg->event_script, cause);
194 if (!cmd) {
195 talloc_free(env);
196 return -ENOMEM;
197 }
198
199 rc = osmo_system_nowait(cmd, osmo_environment_whitelist, env);
200 talloc_free(env);
201
202 return rc;
203}
204
Kévin Redone0b837c2018-10-10 00:39:25 +0200205/***********************************************************************
Harald Weltef14dc042019-03-28 20:29:36 +0100206 * SIMTRACE core protocol
Kévin Redone0b837c2018-10-10 00:39:25 +0200207 ***********************************************************************/
208
209/*! \brief allocate a message buffer for simtrace use */
210static struct msgb *st_msgb_alloc(void)
211{
212 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
213}
214
215#if 0
216static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
217{
218 printf("APDU: %s\n", osmo_hexdump(buf, len));
219 gsmtap_send_sim(buf, len);
220}
221#endif
222
Harald Welte32e2e002019-12-15 23:01:54 +0100223static void usb_out_xfer_cb(struct libusb_transfer *xfer)
224{
225 struct msgb *msg = xfer->user_data;
226
227 switch (xfer->status) {
228 case LIBUSB_TRANSFER_COMPLETED:
229 break;
230 case LIBUSB_TRANSFER_NO_DEVICE:
231 fprintf(stderr, "USB device disappeared\n");
Harald Weltea9bc4de2020-02-16 15:40:21 +0100232 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100233 break;
234 default:
Harald Weltea9bc4de2020-02-16 15:40:21 +0100235 fprintf(stderr, "USB OUT transfer failed, status=%u\n", xfer->status);
236 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100237 break;
238 }
239
240 msgb_free(msg);
241 libusb_free_transfer(xfer);
242}
243
Kévin Redone0b837c2018-10-10 00:39:25 +0200244/*! \brief Transmit a given command to the SIMtrace2 device */
245int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
246{
Harald Welte32e2e002019-12-15 23:01:54 +0100247 struct libusb_transfer *xfer;
Kévin Redone0b837c2018-10-10 00:39:25 +0200248 int rc;
249
Kévin Redon21e31de2018-10-11 17:25:58 +0200250 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redone0b837c2018-10-10 00:39:25 +0200251
Harald Welte32e2e002019-12-15 23:01:54 +0100252 xfer = libusb_alloc_transfer(0);
253 OSMO_ASSERT(xfer);
254 xfer->dev_handle = transp->usb_devh;
255 xfer->flags = 0;
256 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
257 xfer->endpoint = transp->usb_ep.out;
258 xfer->timeout = 1000;
259 xfer->user_data = msg;
260 xfer->length = msgb_length(msg);
261 xfer->buffer = msgb_data(msg);
262 xfer->callback = usb_out_xfer_cb;
Kévin Redone0b837c2018-10-10 00:39:25 +0200263
Harald Welte32e2e002019-12-15 23:01:54 +0100264 /* submit the OUT transfer */
265 rc = libusb_submit_transfer(xfer);
266 OSMO_ASSERT(rc == 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200267
Kévin Redone0b837c2018-10-10 00:39:25 +0200268 return rc;
269}
270
271static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
272 uint8_t slot_nr)
273{
274 struct simtrace_msg_hdr *sh;
275
276 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
277 memset(sh, 0, sizeof(*sh));
278 sh->msg_class = msg_class;
279 sh->msg_type = msg_type;
280 sh->slot_nr = slot_nr;
281 sh->msg_len = msgb_length(msg);
282
283 return sh;
284}
285
286/* transmit a given message to a specified slot. Expects all headers
287 * present before calling the function */
288int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
289 uint8_t msg_class, uint8_t msg_type)
290{
291 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
292
293 return st_transp_tx_msg(slot->transp, msg);
294}
295
296/***********************************************************************
297 * Card Emulation protocol
298 ***********************************************************************/
299
300
301/*! \brief Request the SIMtrace2 to generate a card-insert signal */
302static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
303{
304 struct msgb *msg = st_msgb_alloc();
305 struct cardemu_usb_msg_cardinsert *cins;
306
307 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
308 memset(cins, 0, sizeof(*cins));
309 if (inserted)
310 cins->card_insert = 1;
311
312 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
313}
314
315/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
316static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
317{
318 struct msgb *msg = st_msgb_alloc();
319 struct cardemu_usb_msg_tx_data *txd;
320 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
321
Kévin Redon21e31de2018-10-11 17:25:58 +0200322 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redone0b837c2018-10-10 00:39:25 +0200323
324 memset(txd, 0, sizeof(*txd));
325 txd->data_len = 1;
326 txd->flags = CEMU_DATA_F_PB_AND_RX;
327 /* one data byte */
328 msgb_put_u8(msg, pb);
329
330 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
331}
332
333/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
334static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
Kévin Redonf120b642018-10-15 19:53:02 +0200335 const uint8_t *data, uint16_t data_len_in)
Kévin Redone0b837c2018-10-10 00:39:25 +0200336{
337 struct msgb *msg = st_msgb_alloc();
338 struct cardemu_usb_msg_tx_data *txd;
339 uint8_t *cur;
340
341 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
342
Kévin Redon21e31de2018-10-11 17:25:58 +0200343 printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb,
Kévin Redone0b837c2018-10-10 00:39:25 +0200344 osmo_hexdump(data, data_len_in), data_len_in);
345
346 memset(txd, 0, sizeof(*txd));
347 txd->data_len = 1 + data_len_in;
348 txd->flags = CEMU_DATA_F_PB_AND_TX;
349 /* procedure byte */
350 msgb_put_u8(msg, pb);
351 /* data */
352 cur = msgb_put(msg, data_len_in);
353 memcpy(cur, data, data_len_in);
354
355 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
356}
357
358/*! \brief Request the SIMtrace2 to send a Status Word */
359static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
360{
361 struct msgb *msg = st_msgb_alloc();
362 struct cardemu_usb_msg_tx_data *txd;
363 uint8_t *cur;
364
365 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
366
Kévin Redon21e31de2018-10-11 17:25:58 +0200367 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redone0b837c2018-10-10 00:39:25 +0200368
369 memset(txd, 0, sizeof(*txd));
370 txd->data_len = 2;
371 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
372 cur = msgb_put(msg, 2);
373 cur[0] = sw[0];
374 cur[1] = sw[1];
375
376 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
377}
378
Harald Welte0e0e9322019-12-16 12:47:18 +0100379/*! \brief Request the SIMtrace2 to send a Status Word */
380static int cardem_request_config(struct cardem_inst *ci, uint32_t features)
381{
382 struct msgb *msg = st_msgb_alloc();
383 struct cardemu_usb_msg_config *cfg;
384
385 cfg = (struct cardemu_usb_msg_config *) msgb_put(msg, sizeof(*cfg));
386
387 printf("SIMtrace <= %s(%08x)\n", __func__, features);
388
389 memset(cfg, 0, sizeof(*cfg));
390 cfg->features = features;
391
392 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_BD_CEMU_CONFIG);
393}
394
Kévin Redon206c3d72018-11-12 22:51:37 +0100395// FIXME check if the ATR actually includes a checksum
Harald Welte3f09f632019-03-31 15:51:13 +0200396__attribute__((unused)) static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200397{
398 uint8_t csum = 0;
399 int i;
400
401 for (i = 1; i < atr_len - 1; i++)
402 csum = csum ^ atr[i];
403
404 atr[atr_len-1] = csum;
405}
406
407static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
408{
409 struct msgb *msg = st_msgb_alloc();
410 struct cardemu_usb_msg_set_atr *satr;
411 uint8_t *cur;
412
413 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
414
Kévin Redon21e31de2018-10-11 17:25:58 +0200415 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200416
417 memset(satr, 0, sizeof(*satr));
418 satr->atr_len = atr_len;
419 cur = msgb_put(msg, atr_len);
420 memcpy(cur, atr, atr_len);
421
422 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
423}
424
425/***********************************************************************
426 * Modem Control protocol
427 ***********************************************************************/
428
429static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
430{
431 struct msgb *msg = st_msgb_alloc();
432 struct st_modem_reset *sr ;
433
434 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
435 sr->asserted = asserted;
436 sr->pulse_duration_msec = pulse_ms;
437
438 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
439}
440
441/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
442int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
443{
444 return _modem_reset(slot, 2, duration_ms);
445}
446
447/*! \brief assert the RESET line of the modem */
448int st_modem_reset_active(struct st_slot *slot)
449{
450 return _modem_reset(slot, 1, 0);
451}
452
453/*! \brief de-assert the RESET line of the modem */
454int st_modem_reset_inactive(struct st_slot *slot)
455{
456 return _modem_reset(slot, 0, 0);
457}
458
459static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
460{
461 struct msgb *msg = st_msgb_alloc();
462 struct st_modem_sim_select *ss;
463
464 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
465 ss->remote_sim = remote_sim;
466
467 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
468}
469
470/*! \brief select local (physical) SIM for given slot */
471int st_modem_sim_select_local(struct st_slot *slot)
472{
473 return _modem_sim_select(slot, 0);
474}
475
476/*! \brief select remote (emulated/forwarded) SIM for given slot */
477int st_modem_sim_select_remote(struct st_slot *slot)
478{
479 return _modem_sim_select(slot, 1);
480}
481
482/*! \brief Request slot to send us status information about the modem */
483int st_modem_get_status(struct st_slot *slot)
484{
485 struct msgb *msg = st_msgb_alloc();
486
487 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
488}
489
490
491/***********************************************************************
492 * Incoming Messages
493 ***********************************************************************/
494
495/*! \brief Process a STATUS message from the SIMtrace2 */
496static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
497{
498 struct cardemu_usb_msg_status *status;
499 status = (struct cardemu_usb_msg_status *) buf;
500
Kévin Redon21e31de2018-10-11 17:25:58 +0200501 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200502 status->flags, status->fi, status->di, status->wi,
503 status->waiting_time);
504
505 return 0;
506}
507
508/*! \brief Process a PTS indication message from the SIMtrace2 */
509static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
510{
511 struct cardemu_usb_msg_pts_info *pts;
512 pts = (struct cardemu_usb_msg_pts_info *) buf;
513
Kévin Redon21e31de2018-10-11 17:25:58 +0200514 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redone0b837c2018-10-10 00:39:25 +0200515
516 return 0;
517}
518
519/*! \brief Process a ERROR indication message from the SIMtrace2 */
Harald Welte3f09f632019-03-31 15:51:13 +0200520__attribute__((unused)) static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200521{
522 struct cardemu_usb_msg_error *err;
523 err = (struct cardemu_usb_msg_error *) buf;
524
Kévin Redon21e31de2018-10-11 17:25:58 +0200525 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200526 err->severity, err->subsystem, err->code,
527 err->msg_len ? (char *)err->msg : "");
528
529 return 0;
530}
531
Harald Weltefd5dafc2019-12-15 21:14:14 +0100532static struct osmo_apdu_context ac; // this will hold the complete APDU (across calls)
Kévin Redonbc08db52018-10-11 08:41:00 +0200533
Kévin Redone0b837c2018-10-10 00:39:25 +0200534/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
535static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
536{
Kévin Redonbc08db52018-10-11 08:41:00 +0200537 struct cardemu_usb_msg_rx_data *data = (struct cardemu_usb_msg_rx_data *) buf; // cast the data from the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200538 int rc;
539
Kévin Redon21e31de2018-10-11 17:25:58 +0200540 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redone0b837c2018-10-10 00:39:25 +0200541 osmo_hexdump(data->data, data->data_len));
542
Harald Weltefd5dafc2019-12-15 21:14:14 +0100543 rc = osmo_apdu_segment_in(&ac, data->data, data->data_len,
544 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200545
Kévin Redonbc08db52018-10-11 08:41:00 +0200546 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
Harald Welte2ea20b92019-03-30 08:33:49 +0100547 uint8_t apdu_command[sizeof(ac.hdr) + ac.lc.tot]; // to store the APDU command to send
Kévin Redonbc08db52018-10-11 08:41:00 +0200548 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redone0b837c2018-10-10 00:39:25 +0200549 if (ac.lc.tot) {
Kévin Redonbc08db52018-10-11 08:41:00 +0200550 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redone0b837c2018-10-10 00:39:25 +0200551 }
Kévin Redonbc08db52018-10-11 08:41:00 +0200552 // send APDU to card
Harald Welte9cf013a2019-03-11 22:19:19 +0100553 BankSlot_t bslot;
554 bank_slot2rspro(&bslot, &g_client->bankd_slot);
555 RsproPDU_t *pdu = rspro_gen_TpduModem2Card(g_client->srv_conn.clslot, &bslot, apdu_command, sizeof(ac.hdr) + ac.lc.tot); // create RSPRO packet
Harald Welte3e9860b2019-12-02 23:04:54 +0100556 server_conn_send_rspro(&g_client->bankd_conn, pdu);
Kévin Redonbc08db52018-10-11 08:41:00 +0200557 // the response will come separately
Kévin Redonbc08db52018-10-11 08:41:00 +0200558 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
559 cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur); // send procedure byte to get remaining data
Kévin Redone0b837c2018-10-10 00:39:25 +0200560 }
561 return 0;
562}
563
564#if 0
565 case SIMTRACE_CMD_DO_ERROR
566 rc = process_do_error(ci, buf, len);
567 break;
568#endif
569
570/*! \brief Process an incoming message from the SIMtrace2 */
571static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
572{
573 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
574 int rc;
575
Kévin Redon21e31de2018-10-11 17:25:58 +0200576 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200577
578 buf += sizeof(*sh);
579
580 switch (sh->msg_type) {
581 case SIMTRACE_MSGT_BD_CEMU_STATUS:
582 rc = process_do_status(ci, buf, len);
583 break;
584 case SIMTRACE_MSGT_DO_CEMU_PTS:
585 rc = process_do_pts(ci, buf, len);
586 break;
587 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
588 rc = process_do_rx_da(ci, buf, len);
589 break;
Harald Welte0e0e9322019-12-16 12:47:18 +0100590 case SIMTRACE_MSGT_BD_CEMU_CONFIG:
591 /* firmware confirms configuration change; ignore */
592 break;
593 default:
594 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
595 rc = -1;
596 break;
597 }
598
599 return rc;
600}
601
602
603/*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */
604static int process_irq_status(struct cardem_inst *ci, const uint8_t *buf, int len)
605{
606 const struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf;
607
608 printf("SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
609 status->flags, status->fi, status->di, status->wi,
610 status->waiting_time);
611
612 BankSlot_t bslot;
613 bank_slot2rspro(&bslot, &g_client->bankd_slot);
614 RsproPDU_t *pdu = rspro_gen_ClientSlotStatusInd(g_client->srv_conn.clslot, &bslot,
615 status->flags & CEMU_STATUS_F_RESET_ACTIVE,
616 status->flags & CEMU_STATUS_F_VCC_PRESENT,
617 status->flags & CEMU_STATUS_F_CLK_ACTIVE,
618 -1 /* FIXME: make this dependent on board */);
619 server_conn_send_rspro(&g_client->bankd_conn, pdu);
620
Harald Welte943ae622019-12-17 01:12:56 +0100621 if (ci->last_status.flags != status->flags) {
622 ci->last_status = *status;
Harald Welte55c7f442019-12-17 01:14:43 +0100623 call_script(g_client, "event-modem-status");
Harald Welte943ae622019-12-17 01:12:56 +0100624 } else
625 ci->last_status = *status;
626
Harald Welte0e0e9322019-12-16 12:47:18 +0100627 return 0;
628}
629
630static int process_usb_msg_irq(struct cardem_inst *ci, const uint8_t *buf, unsigned int len)
631{
632 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
633 int rc;
634
635 printf("SIMtrace IRQ %s\n", osmo_hexdump(buf, len));
636
637 buf += sizeof(*sh);
638
639 switch (sh->msg_type) {
640 case SIMTRACE_MSGT_BD_CEMU_STATUS:
641 rc = process_irq_status(ci, buf, len);
642 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200643 default:
644 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
645 rc = -1;
646 break;
647 }
648
649 return rc;
650}
651
Harald Welte32e2e002019-12-15 23:01:54 +0100652static void usb_in_xfer_cb(struct libusb_transfer *xfer)
Kévin Redone0b837c2018-10-10 00:39:25 +0200653{
Harald Welte32e2e002019-12-15 23:01:54 +0100654 struct cardem_inst *ci = xfer->user_data;
Kévin Redone0b837c2018-10-10 00:39:25 +0200655 int rc;
656
Harald Welte32e2e002019-12-15 23:01:54 +0100657 switch (xfer->status) {
658 case LIBUSB_TRANSFER_COMPLETED:
659 /* hand the message up the stack */
660 process_usb_msg(ci, xfer->buffer, xfer->actual_length);
661 break;
662 case LIBUSB_TRANSFER_NO_DEVICE:
663 fprintf(stderr, "USB device disappeared\n");
Harald Weltea9bc4de2020-02-16 15:40:21 +0100664 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100665 break;
666 default:
Harald Weltea9bc4de2020-02-16 15:40:21 +0100667 fprintf(stderr, "USB IN transfer failed, status=%u\n", xfer->status);
668 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100669 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200670 }
Harald Welte32e2e002019-12-15 23:01:54 +0100671
672 /* re-submit the IN transfer */
673 rc = libusb_submit_transfer(xfer);
674 OSMO_ASSERT(rc == 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200675}
676
Harald Welte32e2e002019-12-15 23:01:54 +0100677
678static void allocate_and_submit_in(struct cardem_inst *ci)
679{
680 struct st_transport *transp = ci->slot->transp;
681 struct libusb_transfer *xfer;
682 int rc;
683
684 xfer = libusb_alloc_transfer(0);
685 OSMO_ASSERT(xfer);
686 xfer->dev_handle = transp->usb_devh;
687 xfer->flags = 0;
688 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
689 xfer->endpoint = transp->usb_ep.in;
690 xfer->timeout = 0;
691 xfer->user_data = ci;
692 xfer->length = 16*256;
693
694 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
695 OSMO_ASSERT(xfer->buffer);
696 xfer->callback = usb_in_xfer_cb;
697
698 /* submit the IN transfer */
699 rc = libusb_submit_transfer(xfer);
700 OSMO_ASSERT(rc == 0);
701}
702
703
704static void usb_irq_xfer_cb(struct libusb_transfer *xfer)
705{
Harald Welte0e0e9322019-12-16 12:47:18 +0100706 struct cardem_inst *ci = xfer->user_data;
Harald Welte32e2e002019-12-15 23:01:54 +0100707 int rc;
708
709 switch (xfer->status) {
710 case LIBUSB_TRANSFER_COMPLETED:
Harald Welte0e0e9322019-12-16 12:47:18 +0100711 process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length);
Harald Welte32e2e002019-12-15 23:01:54 +0100712 break;
713 case LIBUSB_TRANSFER_NO_DEVICE:
714 fprintf(stderr, "USB device disappeared\n");
Harald Weltea9bc4de2020-02-16 15:40:21 +0100715 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100716 break;
717 default:
Harald Weltea9bc4de2020-02-16 15:40:21 +0100718 fprintf(stderr, "USB IRQ transfer failed, status=%u\n", xfer->status);
719 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100720 break;
721 }
722
723 /* re-submit the IN transfer */
724 rc = libusb_submit_transfer(xfer);
725 OSMO_ASSERT(rc == 0);
726}
727
728
729static void allocate_and_submit_irq(struct cardem_inst *ci)
730{
731 struct st_transport *transp = ci->slot->transp;
732 struct libusb_transfer *xfer;
733 int rc;
734
735 xfer = libusb_alloc_transfer(0);
736 OSMO_ASSERT(xfer);
737 xfer->dev_handle = transp->usb_devh;
738 xfer->flags = 0;
739 xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
740 xfer->endpoint = transp->usb_ep.irq_in;
741 xfer->timeout = 0;
742 xfer->user_data = ci;
743 xfer->length = 64;
744
745 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
746 OSMO_ASSERT(xfer->buffer);
747 xfer->callback = usb_irq_xfer_cb;
748
749 /* submit the IN transfer */
750 rc = libusb_submit_transfer(xfer);
751 OSMO_ASSERT(rc == 0);
752}
753
754
Kévin Redone0b837c2018-10-10 00:39:25 +0200755static struct st_transport _transp;
756
757static struct st_slot _slot = {
758 .transp = &_transp,
759 .slot_nr = 0,
760};
761
Harald Welte5ed46932019-12-17 00:02:36 +0100762static struct cardem_inst *g_ci;
Kévin Redone0b837c2018-10-10 00:39:25 +0200763
764static void signal_handler(int signal)
765{
766 switch (signal) {
767 case SIGINT:
Harald Welte5ed46932019-12-17 00:02:36 +0100768 cardem_request_card_insert(g_ci, false);
Kévin Redone0b837c2018-10-10 00:39:25 +0200769 exit(0);
770 break;
771 default:
772 break;
773 }
774}
775
776/** remsim_client **/
777
Harald Welte3e9860b2019-12-02 23:04:54 +0100778static int bankd_handle_tpduCardToModem(struct bankd_client *bc, const RsproPDU_t *pdu)
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200779{
780 OSMO_ASSERT(pdu);
781 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
782
783 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
784 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
785 return -1;
786 }
787
788 // save SW to our current APDU context
789 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
790 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200791 printf("SIMtrace <= SW=0x%02x%02x, len_rx=%d\n", ac.sw[0], ac.sw[1], card2modem->data.size - 2);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200792 if (card2modem->data.size > 2) { // send PB and data to modem
Harald Welte5ed46932019-12-17 00:02:36 +0100793 cardem_request_pb_and_tx(bc->cardem, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200794 }
Harald Welte5ed46932019-12-17 00:02:36 +0100795 cardem_request_sw_tx(bc->cardem, ac.sw); // send SW to modem
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200796
797 return 0;
798}
799
Harald Welte3e9860b2019-12-02 23:04:54 +0100800static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu)
Harald Weltefa365592019-03-28 20:28:57 +0100801{
802 RsproPDU_t *resp;
803 int rc;
804
805 OSMO_ASSERT(pdu);
806 OSMO_ASSERT(RsproPDUchoice_PR_setAtrReq == pdu->msg.present);
807
808 /* FIXME: is this permitted at any time by the SIMtrace2 cardemfirmware? */
Harald Welte5ed46932019-12-17 00:02:36 +0100809 rc = cardem_request_set_atr(bc->cardem, pdu->msg.choice.setAtrReq.atr.buf,
Harald Weltefa365592019-03-28 20:28:57 +0100810 pdu->msg.choice.setAtrReq.atr.size);
811 if (rc == 0)
812 resp = rspro_gen_SetAtrRes(ResultCode_ok);
813 else
814 resp = rspro_gen_SetAtrRes(ResultCode_cardTransmissionError);
815 if (!resp)
816 return -ENOMEM;
Harald Welte3e9860b2019-12-02 23:04:54 +0100817 server_conn_send_rspro(&g_client->bankd_conn, resp);
Harald Weltefa365592019-03-28 20:28:57 +0100818
819 return 0;
820}
821
Harald Welte3e9860b2019-12-02 23:04:54 +0100822/* handle incoming message from bankd */
823static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
Kévin Redone0b837c2018-10-10 00:39:25 +0200824{
Kévin Redone0b837c2018-10-10 00:39:25 +0200825 switch (pdu->msg.present) {
826 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +0100827 /* Store 'identity' of bankd to in peer_comp_id */
Harald Welte3e9860b2019-12-02 23:04:54 +0100828 rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
829 osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
Harald Welte55c7f442019-12-17 01:14:43 +0100830 call_script(g_client, "event-bankd-connect");
Kévin Redone0b837c2018-10-10 00:39:25 +0200831 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200832 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
Harald Welte3e9860b2019-12-02 23:04:54 +0100833 bankd_handle_tpduCardToModem(g_client, pdu);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200834 break;
Harald Weltefa365592019-03-28 20:28:57 +0100835 case RsproPDUchoice_PR_setAtrReq:
Harald Welte3e9860b2019-12-02 23:04:54 +0100836 bankd_handle_setAtrReq(g_client, pdu);
Harald Weltefa365592019-03-28 20:28:57 +0100837 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200838 default:
Harald Welte3e9860b2019-12-02 23:04:54 +0100839 LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s\n",
840 rspro_msgt_name(pdu));
Kévin Redone0b837c2018-10-10 00:39:25 +0200841 return -1;
842 }
843
844 return 0;
845}
846
Harald Weltee56f2b92019-03-02 17:02:13 +0100847/* handle incoming messages from server */
848static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
849{
850 RsproPDU_t *resp;
851
852 switch (pdu->msg.present) {
853 case RsproPDUchoice_PR_connectClientRes:
854 /* Store 'identity' of server in srvc->peer_comp_id */
855 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
856 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
Harald Welte55c7f442019-12-17 01:14:43 +0100857 call_script(g_client, "event-server-connect");
Harald Weltee56f2b92019-03-02 17:02:13 +0100858 break;
Harald Welted571a3e2019-03-11 22:09:50 +0100859 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100860 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100861 if (!g_client->srv_conn.clslot)
862 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welted571a3e2019-03-11 22:09:50 +0100863 *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
Harald Welte3e9860b2019-12-02 23:04:54 +0100864 if (!g_client->bankd_conn.clslot)
865 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
866 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welted571a3e2019-03-11 22:09:50 +0100867 /* send response to server */
868 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
869 server_conn_send_rspro(srvc, resp);
870 break;
871 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100872 /* store/set the bankd ip/port as instructed by the server */
Harald Welte3e9860b2019-12-02 23:04:54 +0100873 osmo_talloc_replace_string(g_client, &g_client->bankd_conn.server_host,
Harald Welted571a3e2019-03-11 22:09:50 +0100874 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
Harald Welte9cf013a2019-03-11 22:19:19 +0100875 rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
Harald Welte3e9860b2019-12-02 23:04:54 +0100876 g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
Harald Welte1b6696f2020-02-16 15:27:15 +0100877 /* bankd port 0 is a magic value to indicate "no bankd" */
878 if (g_client->bankd_conn.server_port == 0)
879 osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_DISCONNECT, NULL);
880 else
881 osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
Harald Weltee56f2b92019-03-02 17:02:13 +0100882 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +0100883 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100884 server_conn_send_rspro(srvc, resp);
Harald Welte55c7f442019-12-17 01:14:43 +0100885 call_script(g_client, "event-config-bankd");
Harald Weltee56f2b92019-03-02 17:02:13 +0100886 break;
887 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +0100888 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n",
889 rspro_msgt_name(pdu));
Harald Weltee56f2b92019-03-02 17:02:13 +0100890 return -1;
891 }
892
893 return 0;
894}
895
Harald Weltece638d82019-03-17 09:36:04 +0100896static void handle_sig_usr1(int signal)
897{
898 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +0200899 talloc_report_full(g_tall_ctx, stderr);
Harald Weltef9187482019-12-14 17:21:08 +0100900 printf("===== NULL\n");
901 talloc_report_full(NULL, stderr);
Harald Weltece638d82019-03-17 09:36:04 +0100902}
Harald Weltee56f2b92019-03-02 17:02:13 +0100903
Kévin Redon3ec265b2018-10-11 17:30:33 +0200904static void print_welcome(void)
905{
906 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
Harald Welte1b6696f2020-02-16 15:27:15 +0100907 "(C) 2010-2020, Harald Welte <laforge@gnumonks.org>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200908 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
909}
910
911static void print_help(void)
912{
Harald Weltee56f2b92019-03-02 17:02:13 +0100913 printf( "\t-s\t--server-host HOST\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200914 "\t-p\t--server-port PORT\n"
Harald Welte72cde102019-03-30 10:43:06 +0100915 "\t-c\t--client-id <0-65535>\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200916 "\t-n\t--client-slot <0-65535>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200917 "\t-h\t--help\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100918 "\t-v\t--version\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200919 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
920 "\t-k\t--keep-running\n"
921 "\t-V\t--usb-vendor\tVENDOR_ID\n"
922 "\t-P\t--usb-product\tPRODUCT_ID\n"
923 "\t-C\t--usb-config\tCONFIG_ID\n"
924 "\t-I\t--usb-interface\tINTERFACE_ID\n"
925 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
926 "\t-A\t--usb-address\tADDRESS\n"
927 "\t-H\t--usb-path\tPATH\n"
Kévin Redon206c3d72018-11-12 22:51:37 +0100928 "\t-a\t--atr\tATR\n"
Harald Welte55c7f442019-12-17 01:14:43 +0100929 "\t-e\t--event-script\tPATH\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200930 "\n"
931 );
932}
933
Harald Welte9636d2b2019-12-16 16:26:50 +0100934static struct client_config *client_config_init(void *ctx)
Kévin Redone0b837c2018-10-10 00:39:25 +0200935{
Harald Welte9636d2b2019-12-16 16:26:50 +0100936 struct client_config *cfg = talloc_zero(ctx, struct client_config);
937 if (!cfg)
938 return NULL;
Kévin Redone0b837c2018-10-10 00:39:25 +0200939
Harald Welte9636d2b2019-12-16 16:26:50 +0100940 cfg->server_host = talloc_strdup(cfg, "127.0.0.1");
941 cfg->server_port = 9998;
942 cfg->client_id = -1;
943 cfg->client_slot = -1;
944 cfg->gsmtap_host = talloc_strdup(cfg, "127.0.0.1");
945 cfg->keep_running = false;
946
947 cfg->usb.vendor_id = -1;
948 cfg->usb.product_id = -1;
949 cfg->usb.config_id = -1;
950 cfg->usb.if_num = -1;
951 cfg->usb.altsetting = 0;
952 cfg->usb.addr = -1;
953 cfg->usb.path = NULL;
954
955 cfg->atr.data[0] = 0x3B;
956 cfg->atr.data[1] = 0x00; // the shortest simplest ATR possible
957 cfg->atr.len = 2;
958
959 return cfg;
960};
961
962static void handle_options(struct client_config *cfg, int argc, char **argv)
963{
964 const struct option opts[] = {
965 { "server-host", 1, 0, 's' },
966 { "server-port", 1, 0, 'p' },
967 { "client-id", 1, 0, 'c' },
968 { "client-slot", 1, 0, 'n' },
969 { "help", 0, 0, 'h' },
970 { "version", 0, 0, 'v' },
971 { "gsmtap-ip", 1, 0, 'i' },
972 { "keep-running", 0, 0, 'k' },
973 { "usb-vendor", 1, 0, 'V' },
974 { "usb-product", 1, 0, 'P' },
975 { "usb-config", 1, 0, 'C' },
976 { "usb-interface", 1, 0, 'I' },
977 { "usb-altsetting", 1, 0, 'S' },
978 { "usb-address", 1, 0, 'A' },
979 { "usb-path", 1, 0, 'H' },
980 { "atr", 1, 0, 'a' },
Harald Welte55c7f442019-12-17 01:14:43 +0100981 { "event-script", 1, 0, 'e' },
Harald Welte9636d2b2019-12-16 16:26:50 +0100982 { NULL, 0, 0, 0 }
983 };
984 int c, rc;
Kévin Redone0b837c2018-10-10 00:39:25 +0200985
986 while (1) {
987 int option_index = 0;
988
Harald Welte55c7f442019-12-17 01:14:43 +0100989 c = getopt_long(argc, argv, "s:p:c:n:hvi:kV:P:C:I:S:A:H:a:e:", opts, &option_index);
Kévin Redone0b837c2018-10-10 00:39:25 +0200990 if (c == -1)
991 break;
992 switch (c) {
Harald Weltee56f2b92019-03-02 17:02:13 +0100993 case 's':
Harald Welte9636d2b2019-12-16 16:26:50 +0100994 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200995 break;
996 case 'p':
Harald Welte9636d2b2019-12-16 16:26:50 +0100997 cfg->server_port = atoi(optarg);
Kévin Redonfbca97a2018-10-11 19:13:24 +0200998 break;
Harald Welte72cde102019-03-30 10:43:06 +0100999 case 'c':
Harald Welte9636d2b2019-12-16 16:26:50 +01001000 cfg->client_id = atoi(optarg);
Harald Welte72cde102019-03-30 10:43:06 +01001001 break;
1002 case 'n':
Harald Welte9636d2b2019-12-16 16:26:50 +01001003 cfg->client_slot = atoi(optarg);
Harald Welte72cde102019-03-30 10:43:06 +01001004 break;
Kévin Redone0b837c2018-10-10 00:39:25 +02001005 case 'h':
1006 print_help();
1007 exit(0);
1008 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +01001009 case 'v':
1010 printf("osmo-remsim-client version %s\n", VERSION);
1011 exit(0);
1012 break;
Kévin Redone0b837c2018-10-10 00:39:25 +02001013 case 'i':
Harald Welte9636d2b2019-12-16 16:26:50 +01001014 osmo_talloc_replace_string(cfg, &cfg->gsmtap_host, optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +02001015 break;
1016 case 'k':
Harald Welte9636d2b2019-12-16 16:26:50 +01001017 cfg->keep_running = 1;
Kévin Redone0b837c2018-10-10 00:39:25 +02001018 break;
1019 case 'V':
Harald Welte9636d2b2019-12-16 16:26:50 +01001020 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
Kévin Redone0b837c2018-10-10 00:39:25 +02001021 break;
1022 case 'P':
Harald Welte9636d2b2019-12-16 16:26:50 +01001023 cfg->usb.product_id = strtol(optarg, NULL, 16);
Kévin Redone0b837c2018-10-10 00:39:25 +02001024 break;
1025 case 'C':
Harald Welte9636d2b2019-12-16 16:26:50 +01001026 cfg->usb.config_id = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +02001027 break;
1028 case 'I':
Harald Welte9636d2b2019-12-16 16:26:50 +01001029 cfg->usb.if_num = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +02001030 break;
1031 case 'S':
Harald Welte9636d2b2019-12-16 16:26:50 +01001032 cfg->usb.altsetting = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +02001033 break;
1034 case 'A':
Harald Welte9636d2b2019-12-16 16:26:50 +01001035 cfg->usb.addr = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +02001036 break;
1037 case 'H':
Harald Welte9636d2b2019-12-16 16:26:50 +01001038 cfg->usb.path = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +02001039 break;
Kévin Redon206c3d72018-11-12 22:51:37 +01001040 case 'a':
Harald Welte9636d2b2019-12-16 16:26:50 +01001041 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
1042 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
1043 fprintf(stderr, "ATR malformed\n");
1044 exit(2);
Kévin Redon206c3d72018-11-12 22:51:37 +01001045 }
Harald Welte9636d2b2019-12-16 16:26:50 +01001046 cfg->atr.len = rc;
Kévin Redon206c3d72018-11-12 22:51:37 +01001047 break;
Harald Welte55c7f442019-12-17 01:14:43 +01001048 case 'e':
1049 osmo_talloc_replace_string(cfg, &cfg->event_script, optarg);
1050 break;
Kévin Redone0b837c2018-10-10 00:39:25 +02001051 }
1052 }
1053
Harald Welte9636d2b2019-12-16 16:26:50 +01001054 if (argc > optind) {
1055 fprintf(stderr, "Unsupported positional arguments on command line\n");
1056 exit(2);
Kévin Redone0b837c2018-10-10 00:39:25 +02001057 }
Harald Welte9636d2b2019-12-16 16:26:50 +01001058}
Kévin Redone0b837c2018-10-10 00:39:25 +02001059
Harald Welte9636d2b2019-12-16 16:26:50 +01001060
Harald Welte5ed46932019-12-17 00:02:36 +01001061static void main_body(struct cardem_inst *ci, struct client_config *cfg)
Harald Weltea86e6b72019-12-16 16:51:34 +01001062{
1063 struct st_transport *transp = ci->slot->transp;
1064 struct usb_interface_match _ifm, *ifm = &_ifm;
Harald Welte38d990b2020-02-21 22:07:04 +01001065 int rc, i;
Harald Weltea86e6b72019-12-16 16:51:34 +01001066
1067 ifm->vendor = cfg->usb.vendor_id;
1068 ifm->product = cfg->usb.product_id;
1069 ifm->configuration = cfg->usb.config_id;
1070 ifm->interface = cfg->usb.if_num;
1071 ifm->altsetting = cfg->usb.altsetting;
1072 ifm->addr = cfg->usb.addr;
1073 if (cfg->usb.path)
1074 osmo_strlcpy(ifm->path, cfg->usb.path, sizeof(ifm->path));
1075 transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
1076 if (!transp->usb_devh) {
1077 fprintf(stderr, "can't open USB device\n");
1078 return;
1079 }
1080
Harald Welte499e1d92019-12-17 01:13:53 +01001081 /* (re)determine the USB path of the opened device */
1082 talloc_free(ci->usb_path);
1083 ci->usb_path = osmo_libusb_dev_get_path_c(ci, libusb_get_device(transp->usb_devh));
1084
Harald Weltea86e6b72019-12-16 16:51:34 +01001085 rc = libusb_claim_interface(transp->usb_devh, cfg->usb.if_num);
1086 if (rc < 0) {
1087 fprintf(stderr, "can't claim interface %d; rc=%d\n", cfg->usb.if_num, rc);
1088 goto close_exit;
1089 }
1090
1091 rc = osmo_libusb_get_ep_addrs(transp->usb_devh, cfg->usb.if_num, &transp->usb_ep.out,
1092 &transp->usb_ep.in, &transp->usb_ep.irq_in);
1093 if (rc < 0) {
1094 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
1095 goto close_exit;
1096 }
1097
1098 // switch modem SIM port to emulated SIM on OWHW
1099 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
1100 int modem = -1;
1101 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
1102 case 0:
1103 modem = 1;
1104 break;
1105 case 1:
1106 modem = 2;
1107 break;
1108 default:
1109 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
1110 goto close_exit;
1111 }
1112 //
1113 char gpio_path[PATH_MAX];
1114 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
1115 int connec_st_usim = open(gpio_path, O_WRONLY);
1116 if (-1 == connec_st_usim) {
1117 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1118 goto close_exit;
1119 }
1120 if (1 != write(connec_st_usim, "1", 1)) {
1121 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1122 goto close_exit;
1123 }
1124 printf("switched modem %d to emulated USIM\n", modem);
1125
1126 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
1127 int mdm_rst = open(gpio_path, O_WRONLY);
1128 if (-1 == mdm_rst) {
1129 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
1130 goto close_exit;
1131 }
1132 if (1 != write(mdm_rst, "1", 1)) {
1133 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1134 goto close_exit;
1135 }
1136 sleep(1); // wait a bit to ensure reset is effective
1137 if (1 != write(mdm_rst, "0", 1)) {
1138 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1139 goto close_exit;
1140 }
1141 printf("modem %d reset\n", modem);
1142 }
1143
1144 /* request firmware to generate STATUS on IRQ endpoint */
1145 cardem_request_config(ci, CEMU_FEAT_F_STATUS_IRQ);
1146
1147 /* simulate card-insert to modem (owhw, not qmod) */
1148 cardem_request_card_insert(ci, true);
Harald Welte55c7f442019-12-17 01:14:43 +01001149 call_script(g_client, "request-card-insert");
Harald Weltea86e6b72019-12-16 16:51:34 +01001150
1151 /* select remote (forwarded) SIM */
1152 st_modem_sim_select_remote(ci->slot);
Harald Welte55c7f442019-12-17 01:14:43 +01001153 call_script(g_client, "request-sim-remote");
Harald Weltea86e6b72019-12-16 16:51:34 +01001154
1155 /* set the ATR */
1156 //atr_update_csum(real_atr, sizeof(real_atr));
1157 cardem_request_set_atr(ci, cfg->atr.data, cfg->atr.len);
1158
1159 /* select remote (forwarded) SIM */
1160 st_modem_reset_pulse(ci->slot, 300);
Harald Welte55c7f442019-12-17 01:14:43 +01001161 call_script(g_client, "request-modem-reset");
Harald Weltea86e6b72019-12-16 16:51:34 +01001162
1163 printf("Entering main loop\n");
1164
1165 allocate_and_submit_irq(ci);
Harald Welte38d990b2020-02-21 22:07:04 +01001166 /* submit multiple IN URB in order to work around OS#4409 */
1167 for (i = 0; i < 4; i++)
1168 allocate_and_submit_in(ci);
Harald Weltea86e6b72019-12-16 16:51:34 +01001169
Harald Weltea9bc4de2020-02-16 15:40:21 +01001170 while (!g_leave_main) {
Harald Weltea86e6b72019-12-16 16:51:34 +01001171 osmo_select_main(false);
1172 }
Harald Weltea9bc4de2020-02-16 15:40:21 +01001173 g_leave_main = false;
Harald Weltea86e6b72019-12-16 16:51:34 +01001174
1175 libusb_release_interface(transp->usb_devh, 0);
1176
1177close_exit:
1178 if (transp->usb_devh)
1179 libusb_close(transp-> usb_devh);
1180}
1181
Harald Welte9636d2b2019-12-16 16:26:50 +01001182int main(int argc, char **argv)
1183{
1184 struct rspro_server_conn *srvc, *bankdc;
Harald Welte9636d2b2019-12-16 16:26:50 +01001185 struct client_config *cfg;
1186 int rc;
1187 int ret = 1;
1188
1189 print_welcome();
Harald Weltece638d82019-03-17 09:36:04 +01001190
Harald Weltef9187482019-12-14 17:21:08 +01001191 talloc_enable_null_tracking();
Harald Welte972a1e82019-03-30 08:34:30 +01001192 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +02001193 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Weltef7442b52019-07-18 18:54:05 +02001194 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Welte972a1e82019-03-30 08:34:30 +01001195 osmo_init_logging2(g_tall_ctx, &log_info);
1196
Harald Welte5ed46932019-12-17 00:02:36 +01001197 g_ci = talloc_zero(g_tall_ctx, struct cardem_inst);
1198 g_ci->slot = &_slot;
1199
1200 cfg = client_config_init(g_ci);
Harald Welte9636d2b2019-12-16 16:26:50 +01001201 handle_options(cfg, argc, argv);
1202
1203 if (cfg->usb.vendor_id < 0 || cfg->usb.product_id < 0) {
1204 fprintf(stderr, "You have to specify the vendor and product ID\n");
1205 goto do_exit;
1206 }
1207
1208 signal(SIGUSR1, handle_sig_usr1);
1209
Harald Welte32e2e002019-12-15 23:01:54 +01001210 rc = osmo_libusb_init(NULL);
Kévin Redon193a8c12018-10-11 17:24:39 +02001211 if (rc < 0) {
1212 fprintf(stderr, "libusb initialization failed\n");
1213 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +02001214 }
1215
Harald Welte9636d2b2019-12-16 16:26:50 +01001216 g_gti = gsmtap_source_init(cfg->gsmtap_host, GSMTAP_UDP_PORT, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +02001217 if (!g_gti) {
1218 perror("unable to open GSMTAP");
1219 goto close_exit;
1220 }
1221 gsmtap_source_add_sink(g_gti);
1222
1223 signal(SIGINT, &signal_handler);
1224
1225 // initialize remote SIM client
Kévin Redone0b837c2018-10-10 00:39:25 +02001226
Kévin Redone0b837c2018-10-10 00:39:25 +02001227 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Welte6a0248b2019-12-17 01:11:24 +01001228 g_client->cfg = cfg;
Harald Welte5ed46932019-12-17 00:02:36 +01001229 g_client->cardem = g_ci;
Harald Weltee56f2b92019-03-02 17:02:13 +01001230
Harald Welte9636d2b2019-12-16 16:26:50 +01001231 if (cfg->client_id != -1) {
Harald Welte72cde102019-03-30 10:43:06 +01001232 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welte9636d2b2019-12-16 16:26:50 +01001233 g_client->srv_conn.clslot->clientId = cfg->client_id;
1234 /* default to client slot 0 */
1235 if (cfg->client_slot == -1)
1236 g_client->srv_conn.clslot->slotNr = 0;
1237 else
1238 g_client->srv_conn.clslot->slotNr = cfg->client_slot;
Harald Welte3e9860b2019-12-02 23:04:54 +01001239 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
1240 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welte72cde102019-03-30 10:43:06 +01001241 }
1242
Harald Welte9636d2b2019-12-16 16:26:50 +01001243 /* create and [attempt to] establish connection to remsim-server */
Harald Weltee56f2b92019-03-02 17:02:13 +01001244 srvc = &g_client->srv_conn;
Harald Welte9636d2b2019-12-16 16:26:50 +01001245 srvc->server_host = cfg->server_host;
1246 srvc->server_port = cfg->server_port;
Harald Weltee56f2b92019-03-02 17:02:13 +01001247 srvc->handle_rx = srvc_handle_rx;
1248 srvc->own_comp_id.type = ComponentType_remsimClient;
1249 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
1250 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
1251 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
1252 rc = server_conn_fsm_alloc(g_client, srvc);
1253 if (rc < 0) {
1254 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
1255 exit(1);
1256 }
Harald Welted2192e22019-11-07 18:10:57 +01001257 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001258
Harald Welte9636d2b2019-12-16 16:26:50 +01001259 /* create and not yet establish connection to remsim-bankd */
1260 srvc = &g_client->srv_conn;
Harald Welte3e9860b2019-12-02 23:04:54 +01001261 bankdc = &g_client->bankd_conn;
1262 /* server_host / server_port are configured from remsim-server */
1263 bankdc->handle_rx = bankd_handle_rx;
1264 memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id));
1265 rc = server_conn_fsm_alloc(g_client, bankdc);
1266 if (rc < 0) {
1267 fprintf(stderr, "Unable to create bankd conn FSM: %s\n", strerror(errno));
Kévin Redone0b837c2018-10-10 00:39:25 +02001268 exit(1);
1269 }
Harald Welte82194452019-12-14 17:18:34 +01001270 osmo_fsm_inst_update_id(bankdc->fi, "bankd");
Kévin Redone0b837c2018-10-10 00:39:25 +02001271
Harald Welte3e9860b2019-12-02 23:04:54 +01001272 asn_debug = 0;
1273
Kévin Redone0b837c2018-10-10 00:39:25 +02001274 // connect to SIMtrace2 cardem
1275 do {
Harald Welte5ed46932019-12-17 00:02:36 +01001276 main_body(g_ci, cfg);
Harald Weltea86e6b72019-12-16 16:51:34 +01001277 sleep(1);
Harald Welte9636d2b2019-12-16 16:26:50 +01001278 } while (cfg->keep_running);
Kévin Redone0b837c2018-10-10 00:39:25 +02001279
Harald Weltea86e6b72019-12-16 16:51:34 +01001280close_exit:
1281 osmo_libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001282do_exit:
1283 return ret;
1284}