blob: 1af327407bf9c5730a50ae5f0e52039d2a5d8cca [file] [log] [blame]
Harald Welte3dcdd202019-03-09 13:06:46 +01001/* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org>
2 * (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>
35
36#include <osmocom/abis/ipa.h>
37#include <osmocom/gsm/protocol/ipaccess.h>
38
39#include "rspro_util.h"
40#include "client.h"
Harald Welte61d98e92019-03-03 15:43:07 +010041#include "debug.h"
Kévin Redone0b837c2018-10-10 00:39:25 +020042
43#include <unistd.h>
44#include <stdio.h>
Kévin Redon3428e412018-10-11 19:14:00 +020045#include <linux/limits.h>
46#include <sys/stat.h>
47#include <fcntl.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020048#include <signal.h>
49#include <getopt.h>
50
51#include <libusb.h>
52
Harald Weltefd5dafc2019-12-15 21:14:14 +010053#include <osmocom/usb/libusb.h>
54#include <osmocom/simtrace2/simtrace_prot.h>
55#include <osmocom/simtrace2/simtrace_usb.h>
56#include <osmocom/simtrace2/apdu_dispatch.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020057
58#include <osmocom/core/gsmtap.h>
59#include <osmocom/core/gsmtap_util.h>
60#include <osmocom/core/utils.h>
61#include <osmocom/core/socket.h>
62#include <osmocom/core/msgb.h>
63#include <osmocom/sim/class_tables.h>
64#include <osmocom/sim/sim.h>
65
66/* transport to a SIMtrace device */
67struct st_transport {
68 /* USB */
69 struct libusb_device_handle *usb_devh;
70 struct {
71 uint8_t in;
72 uint8_t out;
73 uint8_t irq_in;
74 } usb_ep;
Kévin Redone0b837c2018-10-10 00:39:25 +020075};
76
77/* a SIMtrace slot; communicates over a transport */
78struct st_slot {
79 /* transport through which the slot can be reached */
80 struct st_transport *transp;
81 /* number of the slot within the transport */
82 uint8_t slot_nr;
83};
84
85/* One istance of card emulation */
86struct cardem_inst {
87 /* slot on which this card emulation instance runs */
88 struct st_slot *slot;
89};
90
91/* global GSMTAP instance */
92static struct gsmtap_inst *g_gti;
93
94static struct bankd_client *g_client;
95static void *g_tall_ctx;
96void __thread *talloc_asn1_ctx;
97int asn_debug;
98
Harald Weltea9bc4de2020-02-16 15:40:21 +010099/* should we leave main loop processing? */
100bool g_leave_main = false;
101
Harald Welte3f09f632019-03-31 15:51:13 +0200102__attribute__((unused)) static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200103{
104 struct gsmtap_hdr *gh;
105 unsigned int gross_len = len + sizeof(*gh);
106 uint8_t *buf = malloc(gross_len);
107 int rc;
108
109 if (!buf)
110 return -ENOMEM;
111
112 memset(buf, 0, sizeof(*gh));
113 gh = (struct gsmtap_hdr *) buf;
114 gh->version = GSMTAP_VERSION;
115 gh->hdr_len = sizeof(*gh)/4;
116 gh->type = GSMTAP_TYPE_SIM;
117
118 memcpy(buf + sizeof(*gh), apdu, len);
119
120 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
121 if (rc < 0) {
122 perror("write gsmtap");
123 free(buf);
124 return rc;
125 }
126
127 free(buf);
128 return 0;
129}
130
131/***********************************************************************
Harald Weltef14dc042019-03-28 20:29:36 +0100132 * SIMTRACE core protocol
Kévin Redone0b837c2018-10-10 00:39:25 +0200133 ***********************************************************************/
134
135/*! \brief allocate a message buffer for simtrace use */
136static struct msgb *st_msgb_alloc(void)
137{
138 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
139}
140
141#if 0
142static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
143{
144 printf("APDU: %s\n", osmo_hexdump(buf, len));
145 gsmtap_send_sim(buf, len);
146}
147#endif
148
Harald Welte32e2e002019-12-15 23:01:54 +0100149static void usb_out_xfer_cb(struct libusb_transfer *xfer)
150{
151 struct msgb *msg = xfer->user_data;
152
153 switch (xfer->status) {
154 case LIBUSB_TRANSFER_COMPLETED:
155 break;
156 case LIBUSB_TRANSFER_NO_DEVICE:
157 fprintf(stderr, "USB device disappeared\n");
Harald Weltea9bc4de2020-02-16 15:40:21 +0100158 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100159 break;
160 default:
Harald Weltea9bc4de2020-02-16 15:40:21 +0100161 fprintf(stderr, "USB OUT transfer failed, status=%u\n", xfer->status);
162 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100163 break;
164 }
165
166 msgb_free(msg);
167 libusb_free_transfer(xfer);
168}
169
Kévin Redone0b837c2018-10-10 00:39:25 +0200170/*! \brief Transmit a given command to the SIMtrace2 device */
171int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
172{
Harald Welte32e2e002019-12-15 23:01:54 +0100173 struct libusb_transfer *xfer;
Kévin Redone0b837c2018-10-10 00:39:25 +0200174 int rc;
175
Kévin Redon21e31de2018-10-11 17:25:58 +0200176 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redone0b837c2018-10-10 00:39:25 +0200177
Harald Welte32e2e002019-12-15 23:01:54 +0100178 xfer = libusb_alloc_transfer(0);
179 OSMO_ASSERT(xfer);
180 xfer->dev_handle = transp->usb_devh;
181 xfer->flags = 0;
182 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
183 xfer->endpoint = transp->usb_ep.out;
184 xfer->timeout = 1000;
185 xfer->user_data = msg;
186 xfer->length = msgb_length(msg);
187 xfer->buffer = msgb_data(msg);
188 xfer->callback = usb_out_xfer_cb;
Kévin Redone0b837c2018-10-10 00:39:25 +0200189
Harald Welte32e2e002019-12-15 23:01:54 +0100190 /* submit the OUT transfer */
191 rc = libusb_submit_transfer(xfer);
192 OSMO_ASSERT(rc == 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200193
Kévin Redone0b837c2018-10-10 00:39:25 +0200194 return rc;
195}
196
197static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
198 uint8_t slot_nr)
199{
200 struct simtrace_msg_hdr *sh;
201
202 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
203 memset(sh, 0, sizeof(*sh));
204 sh->msg_class = msg_class;
205 sh->msg_type = msg_type;
206 sh->slot_nr = slot_nr;
207 sh->msg_len = msgb_length(msg);
208
209 return sh;
210}
211
212/* transmit a given message to a specified slot. Expects all headers
213 * present before calling the function */
214int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
215 uint8_t msg_class, uint8_t msg_type)
216{
217 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
218
219 return st_transp_tx_msg(slot->transp, msg);
220}
221
222/***********************************************************************
223 * Card Emulation protocol
224 ***********************************************************************/
225
226
227/*! \brief Request the SIMtrace2 to generate a card-insert signal */
228static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
229{
230 struct msgb *msg = st_msgb_alloc();
231 struct cardemu_usb_msg_cardinsert *cins;
232
233 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
234 memset(cins, 0, sizeof(*cins));
235 if (inserted)
236 cins->card_insert = 1;
237
238 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
239}
240
241/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
242static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
243{
244 struct msgb *msg = st_msgb_alloc();
245 struct cardemu_usb_msg_tx_data *txd;
246 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
247
Kévin Redon21e31de2018-10-11 17:25:58 +0200248 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redone0b837c2018-10-10 00:39:25 +0200249
250 memset(txd, 0, sizeof(*txd));
251 txd->data_len = 1;
252 txd->flags = CEMU_DATA_F_PB_AND_RX;
253 /* one data byte */
254 msgb_put_u8(msg, pb);
255
256 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
257}
258
259/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
260static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
Kévin Redonf120b642018-10-15 19:53:02 +0200261 const uint8_t *data, uint16_t data_len_in)
Kévin Redone0b837c2018-10-10 00:39:25 +0200262{
263 struct msgb *msg = st_msgb_alloc();
264 struct cardemu_usb_msg_tx_data *txd;
265 uint8_t *cur;
266
267 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
268
Kévin Redon21e31de2018-10-11 17:25:58 +0200269 printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb,
Kévin Redone0b837c2018-10-10 00:39:25 +0200270 osmo_hexdump(data, data_len_in), data_len_in);
271
272 memset(txd, 0, sizeof(*txd));
273 txd->data_len = 1 + data_len_in;
274 txd->flags = CEMU_DATA_F_PB_AND_TX;
275 /* procedure byte */
276 msgb_put_u8(msg, pb);
277 /* data */
278 cur = msgb_put(msg, data_len_in);
279 memcpy(cur, data, data_len_in);
280
281 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
282}
283
284/*! \brief Request the SIMtrace2 to send a Status Word */
285static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
286{
287 struct msgb *msg = st_msgb_alloc();
288 struct cardemu_usb_msg_tx_data *txd;
289 uint8_t *cur;
290
291 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
292
Kévin Redon21e31de2018-10-11 17:25:58 +0200293 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redone0b837c2018-10-10 00:39:25 +0200294
295 memset(txd, 0, sizeof(*txd));
296 txd->data_len = 2;
297 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
298 cur = msgb_put(msg, 2);
299 cur[0] = sw[0];
300 cur[1] = sw[1];
301
302 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
303}
304
Harald Welte0e0e9322019-12-16 12:47:18 +0100305/*! \brief Request the SIMtrace2 to send a Status Word */
306static int cardem_request_config(struct cardem_inst *ci, uint32_t features)
307{
308 struct msgb *msg = st_msgb_alloc();
309 struct cardemu_usb_msg_config *cfg;
310
311 cfg = (struct cardemu_usb_msg_config *) msgb_put(msg, sizeof(*cfg));
312
313 printf("SIMtrace <= %s(%08x)\n", __func__, features);
314
315 memset(cfg, 0, sizeof(*cfg));
316 cfg->features = features;
317
318 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_BD_CEMU_CONFIG);
319}
320
Kévin Redon206c3d72018-11-12 22:51:37 +0100321// FIXME check if the ATR actually includes a checksum
Harald Welte3f09f632019-03-31 15:51:13 +0200322__attribute__((unused)) static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200323{
324 uint8_t csum = 0;
325 int i;
326
327 for (i = 1; i < atr_len - 1; i++)
328 csum = csum ^ atr[i];
329
330 atr[atr_len-1] = csum;
331}
332
333static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
334{
335 struct msgb *msg = st_msgb_alloc();
336 struct cardemu_usb_msg_set_atr *satr;
337 uint8_t *cur;
338
339 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
340
Kévin Redon21e31de2018-10-11 17:25:58 +0200341 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200342
343 memset(satr, 0, sizeof(*satr));
344 satr->atr_len = atr_len;
345 cur = msgb_put(msg, atr_len);
346 memcpy(cur, atr, atr_len);
347
348 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
349}
350
351/***********************************************************************
352 * Modem Control protocol
353 ***********************************************************************/
354
355static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
356{
357 struct msgb *msg = st_msgb_alloc();
358 struct st_modem_reset *sr ;
359
360 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
361 sr->asserted = asserted;
362 sr->pulse_duration_msec = pulse_ms;
363
364 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
365}
366
367/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
368int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
369{
370 return _modem_reset(slot, 2, duration_ms);
371}
372
373/*! \brief assert the RESET line of the modem */
374int st_modem_reset_active(struct st_slot *slot)
375{
376 return _modem_reset(slot, 1, 0);
377}
378
379/*! \brief de-assert the RESET line of the modem */
380int st_modem_reset_inactive(struct st_slot *slot)
381{
382 return _modem_reset(slot, 0, 0);
383}
384
385static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
386{
387 struct msgb *msg = st_msgb_alloc();
388 struct st_modem_sim_select *ss;
389
390 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
391 ss->remote_sim = remote_sim;
392
393 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
394}
395
396/*! \brief select local (physical) SIM for given slot */
397int st_modem_sim_select_local(struct st_slot *slot)
398{
399 return _modem_sim_select(slot, 0);
400}
401
402/*! \brief select remote (emulated/forwarded) SIM for given slot */
403int st_modem_sim_select_remote(struct st_slot *slot)
404{
405 return _modem_sim_select(slot, 1);
406}
407
408/*! \brief Request slot to send us status information about the modem */
409int st_modem_get_status(struct st_slot *slot)
410{
411 struct msgb *msg = st_msgb_alloc();
412
413 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
414}
415
416
417/***********************************************************************
418 * Incoming Messages
419 ***********************************************************************/
420
421/*! \brief Process a STATUS message from the SIMtrace2 */
422static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
423{
424 struct cardemu_usb_msg_status *status;
425 status = (struct cardemu_usb_msg_status *) buf;
426
Kévin Redon21e31de2018-10-11 17:25:58 +0200427 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200428 status->flags, status->fi, status->di, status->wi,
429 status->waiting_time);
430
431 return 0;
432}
433
434/*! \brief Process a PTS indication message from the SIMtrace2 */
435static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
436{
437 struct cardemu_usb_msg_pts_info *pts;
438 pts = (struct cardemu_usb_msg_pts_info *) buf;
439
Kévin Redon21e31de2018-10-11 17:25:58 +0200440 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redone0b837c2018-10-10 00:39:25 +0200441
442 return 0;
443}
444
445/*! \brief Process a ERROR indication message from the SIMtrace2 */
Harald Welte3f09f632019-03-31 15:51:13 +0200446__attribute__((unused)) static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200447{
448 struct cardemu_usb_msg_error *err;
449 err = (struct cardemu_usb_msg_error *) buf;
450
Kévin Redon21e31de2018-10-11 17:25:58 +0200451 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200452 err->severity, err->subsystem, err->code,
453 err->msg_len ? (char *)err->msg : "");
454
455 return 0;
456}
457
Harald Weltefd5dafc2019-12-15 21:14:14 +0100458static struct osmo_apdu_context ac; // this will hold the complete APDU (across calls)
Kévin Redonbc08db52018-10-11 08:41:00 +0200459
Kévin Redone0b837c2018-10-10 00:39:25 +0200460/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
461static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
462{
Kévin Redonbc08db52018-10-11 08:41:00 +0200463 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 +0200464 int rc;
465
Kévin Redon21e31de2018-10-11 17:25:58 +0200466 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redone0b837c2018-10-10 00:39:25 +0200467 osmo_hexdump(data->data, data->data_len));
468
Harald Weltefd5dafc2019-12-15 21:14:14 +0100469 rc = osmo_apdu_segment_in(&ac, data->data, data->data_len,
470 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200471
Kévin Redonbc08db52018-10-11 08:41:00 +0200472 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
Harald Welte2ea20b92019-03-30 08:33:49 +0100473 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 +0200474 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redone0b837c2018-10-10 00:39:25 +0200475 if (ac.lc.tot) {
Kévin Redonbc08db52018-10-11 08:41:00 +0200476 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redone0b837c2018-10-10 00:39:25 +0200477 }
Kévin Redonbc08db52018-10-11 08:41:00 +0200478 // send APDU to card
Harald Welte9cf013a2019-03-11 22:19:19 +0100479 BankSlot_t bslot;
480 bank_slot2rspro(&bslot, &g_client->bankd_slot);
481 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 +0100482 server_conn_send_rspro(&g_client->bankd_conn, pdu);
Kévin Redonbc08db52018-10-11 08:41:00 +0200483 // the response will come separately
Kévin Redonbc08db52018-10-11 08:41:00 +0200484 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
485 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 +0200486 }
487 return 0;
488}
489
490#if 0
491 case SIMTRACE_CMD_DO_ERROR
492 rc = process_do_error(ci, buf, len);
493 break;
494#endif
495
496/*! \brief Process an incoming message from the SIMtrace2 */
497static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
498{
499 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
500 int rc;
501
Kévin Redon21e31de2018-10-11 17:25:58 +0200502 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200503
504 buf += sizeof(*sh);
505
506 switch (sh->msg_type) {
507 case SIMTRACE_MSGT_BD_CEMU_STATUS:
508 rc = process_do_status(ci, buf, len);
509 break;
510 case SIMTRACE_MSGT_DO_CEMU_PTS:
511 rc = process_do_pts(ci, buf, len);
512 break;
513 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
514 rc = process_do_rx_da(ci, buf, len);
515 break;
Harald Welte0e0e9322019-12-16 12:47:18 +0100516 case SIMTRACE_MSGT_BD_CEMU_CONFIG:
517 /* firmware confirms configuration change; ignore */
518 break;
519 default:
520 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
521 rc = -1;
522 break;
523 }
524
525 return rc;
526}
527
528
529/*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */
530static int process_irq_status(struct cardem_inst *ci, const uint8_t *buf, int len)
531{
532 const struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf;
533
534 printf("SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
535 status->flags, status->fi, status->di, status->wi,
536 status->waiting_time);
537
538 BankSlot_t bslot;
539 bank_slot2rspro(&bslot, &g_client->bankd_slot);
540 RsproPDU_t *pdu = rspro_gen_ClientSlotStatusInd(g_client->srv_conn.clslot, &bslot,
541 status->flags & CEMU_STATUS_F_RESET_ACTIVE,
542 status->flags & CEMU_STATUS_F_VCC_PRESENT,
543 status->flags & CEMU_STATUS_F_CLK_ACTIVE,
544 -1 /* FIXME: make this dependent on board */);
545 server_conn_send_rspro(&g_client->bankd_conn, pdu);
546
547 return 0;
548}
549
550static int process_usb_msg_irq(struct cardem_inst *ci, const uint8_t *buf, unsigned int len)
551{
552 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
553 int rc;
554
555 printf("SIMtrace IRQ %s\n", osmo_hexdump(buf, len));
556
557 buf += sizeof(*sh);
558
559 switch (sh->msg_type) {
560 case SIMTRACE_MSGT_BD_CEMU_STATUS:
561 rc = process_irq_status(ci, buf, len);
562 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200563 default:
564 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
565 rc = -1;
566 break;
567 }
568
569 return rc;
570}
571
Harald Welte32e2e002019-12-15 23:01:54 +0100572static void usb_in_xfer_cb(struct libusb_transfer *xfer)
Kévin Redone0b837c2018-10-10 00:39:25 +0200573{
Harald Welte32e2e002019-12-15 23:01:54 +0100574 struct cardem_inst *ci = xfer->user_data;
Kévin Redone0b837c2018-10-10 00:39:25 +0200575 int rc;
576
Harald Welte32e2e002019-12-15 23:01:54 +0100577 switch (xfer->status) {
578 case LIBUSB_TRANSFER_COMPLETED:
579 /* hand the message up the stack */
580 process_usb_msg(ci, xfer->buffer, xfer->actual_length);
581 break;
582 case LIBUSB_TRANSFER_NO_DEVICE:
583 fprintf(stderr, "USB device disappeared\n");
Harald Weltea9bc4de2020-02-16 15:40:21 +0100584 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100585 break;
586 default:
Harald Weltea9bc4de2020-02-16 15:40:21 +0100587 fprintf(stderr, "USB IN transfer failed, status=%u\n", xfer->status);
588 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100589 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200590 }
Harald Welte32e2e002019-12-15 23:01:54 +0100591
592 /* re-submit the IN transfer */
593 rc = libusb_submit_transfer(xfer);
594 OSMO_ASSERT(rc == 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200595}
596
Harald Welte32e2e002019-12-15 23:01:54 +0100597
598static void allocate_and_submit_in(struct cardem_inst *ci)
599{
600 struct st_transport *transp = ci->slot->transp;
601 struct libusb_transfer *xfer;
602 int rc;
603
604 xfer = libusb_alloc_transfer(0);
605 OSMO_ASSERT(xfer);
606 xfer->dev_handle = transp->usb_devh;
607 xfer->flags = 0;
608 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
609 xfer->endpoint = transp->usb_ep.in;
610 xfer->timeout = 0;
611 xfer->user_data = ci;
612 xfer->length = 16*256;
613
614 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
615 OSMO_ASSERT(xfer->buffer);
616 xfer->callback = usb_in_xfer_cb;
617
618 /* submit the IN transfer */
619 rc = libusb_submit_transfer(xfer);
620 OSMO_ASSERT(rc == 0);
621}
622
623
624static void usb_irq_xfer_cb(struct libusb_transfer *xfer)
625{
Harald Welte0e0e9322019-12-16 12:47:18 +0100626 struct cardem_inst *ci = xfer->user_data;
Harald Welte32e2e002019-12-15 23:01:54 +0100627 int rc;
628
629 switch (xfer->status) {
630 case LIBUSB_TRANSFER_COMPLETED:
Harald Welte0e0e9322019-12-16 12:47:18 +0100631 process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length);
Harald Welte32e2e002019-12-15 23:01:54 +0100632 break;
633 case LIBUSB_TRANSFER_NO_DEVICE:
634 fprintf(stderr, "USB device disappeared\n");
Harald Weltea9bc4de2020-02-16 15:40:21 +0100635 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100636 break;
637 default:
Harald Weltea9bc4de2020-02-16 15:40:21 +0100638 fprintf(stderr, "USB IRQ transfer failed, status=%u\n", xfer->status);
639 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100640 break;
641 }
642
643 /* re-submit the IN transfer */
644 rc = libusb_submit_transfer(xfer);
645 OSMO_ASSERT(rc == 0);
646}
647
648
649static void allocate_and_submit_irq(struct cardem_inst *ci)
650{
651 struct st_transport *transp = ci->slot->transp;
652 struct libusb_transfer *xfer;
653 int rc;
654
655 xfer = libusb_alloc_transfer(0);
656 OSMO_ASSERT(xfer);
657 xfer->dev_handle = transp->usb_devh;
658 xfer->flags = 0;
659 xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
660 xfer->endpoint = transp->usb_ep.irq_in;
661 xfer->timeout = 0;
662 xfer->user_data = ci;
663 xfer->length = 64;
664
665 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
666 OSMO_ASSERT(xfer->buffer);
667 xfer->callback = usb_irq_xfer_cb;
668
669 /* submit the IN transfer */
670 rc = libusb_submit_transfer(xfer);
671 OSMO_ASSERT(rc == 0);
672}
673
674
Kévin Redone0b837c2018-10-10 00:39:25 +0200675static struct st_transport _transp;
676
677static struct st_slot _slot = {
678 .transp = &_transp,
679 .slot_nr = 0,
680};
681
682struct cardem_inst _ci = {
683 .slot = &_slot,
684};
685
686struct cardem_inst *ci = &_ci;
687
688static void signal_handler(int signal)
689{
690 switch (signal) {
691 case SIGINT:
692 cardem_request_card_insert(ci, false);
693 exit(0);
694 break;
695 default:
696 break;
697 }
698}
699
700/** remsim_client **/
701
Harald Welte3e9860b2019-12-02 23:04:54 +0100702static int bankd_handle_tpduCardToModem(struct bankd_client *bc, const RsproPDU_t *pdu)
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200703{
704 OSMO_ASSERT(pdu);
705 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
706
707 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
708 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
709 return -1;
710 }
711
712 // save SW to our current APDU context
713 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
714 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200715 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 +0200716 if (card2modem->data.size > 2) { // send PB and data to modem
717 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
718 }
719 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
720
721 return 0;
722}
723
Harald Welte3e9860b2019-12-02 23:04:54 +0100724static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu)
Harald Weltefa365592019-03-28 20:28:57 +0100725{
726 RsproPDU_t *resp;
727 int rc;
728
729 OSMO_ASSERT(pdu);
730 OSMO_ASSERT(RsproPDUchoice_PR_setAtrReq == pdu->msg.present);
731
732 /* FIXME: is this permitted at any time by the SIMtrace2 cardemfirmware? */
733 rc = cardem_request_set_atr(ci, pdu->msg.choice.setAtrReq.atr.buf,
734 pdu->msg.choice.setAtrReq.atr.size);
735 if (rc == 0)
736 resp = rspro_gen_SetAtrRes(ResultCode_ok);
737 else
738 resp = rspro_gen_SetAtrRes(ResultCode_cardTransmissionError);
739 if (!resp)
740 return -ENOMEM;
Harald Welte3e9860b2019-12-02 23:04:54 +0100741 server_conn_send_rspro(&g_client->bankd_conn, resp);
Harald Weltefa365592019-03-28 20:28:57 +0100742
743 return 0;
744}
745
Harald Welte3e9860b2019-12-02 23:04:54 +0100746/* handle incoming message from bankd */
747static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
Kévin Redone0b837c2018-10-10 00:39:25 +0200748{
Kévin Redone0b837c2018-10-10 00:39:25 +0200749 switch (pdu->msg.present) {
750 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +0100751 /* Store 'identity' of bankd to in peer_comp_id */
Harald Welte3e9860b2019-12-02 23:04:54 +0100752 rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
753 osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
Kévin Redone0b837c2018-10-10 00:39:25 +0200754 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200755 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
Harald Welte3e9860b2019-12-02 23:04:54 +0100756 bankd_handle_tpduCardToModem(g_client, pdu);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200757 break;
Harald Weltefa365592019-03-28 20:28:57 +0100758 case RsproPDUchoice_PR_setAtrReq:
Harald Welte3e9860b2019-12-02 23:04:54 +0100759 bankd_handle_setAtrReq(g_client, pdu);
Harald Weltefa365592019-03-28 20:28:57 +0100760 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200761 default:
Harald Welte3e9860b2019-12-02 23:04:54 +0100762 LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s\n",
763 rspro_msgt_name(pdu));
Kévin Redone0b837c2018-10-10 00:39:25 +0200764 return -1;
765 }
766
767 return 0;
768}
769
Harald Weltee56f2b92019-03-02 17:02:13 +0100770/* handle incoming messages from server */
771static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
772{
773 RsproPDU_t *resp;
774
775 switch (pdu->msg.present) {
776 case RsproPDUchoice_PR_connectClientRes:
777 /* Store 'identity' of server in srvc->peer_comp_id */
778 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
779 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
780 break;
Harald Welted571a3e2019-03-11 22:09:50 +0100781 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100782 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100783 if (!g_client->srv_conn.clslot)
784 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welted571a3e2019-03-11 22:09:50 +0100785 *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
Harald Welte3e9860b2019-12-02 23:04:54 +0100786 if (!g_client->bankd_conn.clslot)
787 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
788 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welted571a3e2019-03-11 22:09:50 +0100789 /* send response to server */
790 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
791 server_conn_send_rspro(srvc, resp);
792 break;
793 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100794 /* store/set the bankd ip/port as instructed by the server */
Harald Welte3e9860b2019-12-02 23:04:54 +0100795 osmo_talloc_replace_string(g_client, &g_client->bankd_conn.server_host,
Harald Welted571a3e2019-03-11 22:09:50 +0100796 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
Harald Welte9cf013a2019-03-11 22:19:19 +0100797 rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
Harald Welte3e9860b2019-12-02 23:04:54 +0100798 g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
Harald Weltee56f2b92019-03-02 17:02:13 +0100799 /* instruct bankd FSM to connect */
Harald Welte3e9860b2019-12-02 23:04:54 +0100800 osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
Harald Weltee56f2b92019-03-02 17:02:13 +0100801 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +0100802 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100803 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +0100804 break;
805 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +0100806 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n",
807 rspro_msgt_name(pdu));
Harald Weltee56f2b92019-03-02 17:02:13 +0100808 return -1;
809 }
810
811 return 0;
812}
813
Harald Weltece638d82019-03-17 09:36:04 +0100814static void handle_sig_usr1(int signal)
815{
816 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +0200817 talloc_report_full(g_tall_ctx, stderr);
Harald Weltef9187482019-12-14 17:21:08 +0100818 printf("===== NULL\n");
819 talloc_report_full(NULL, stderr);
Harald Weltece638d82019-03-17 09:36:04 +0100820}
Harald Weltee56f2b92019-03-02 17:02:13 +0100821
Kévin Redon3ec265b2018-10-11 17:30:33 +0200822static void print_welcome(void)
823{
824 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
Harald Welte32e2e002019-12-15 23:01:54 +0100825 "(C) 2010-2019, Harald Welte <laforge@gnumonks.org>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200826 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
827}
828
829static void print_help(void)
830{
Harald Weltee56f2b92019-03-02 17:02:13 +0100831 printf( "\t-s\t--server-host HOST\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200832 "\t-p\t--server-port PORT\n"
Harald Welte72cde102019-03-30 10:43:06 +0100833 "\t-c\t--client-id <0-65535>\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200834 "\t-n\t--client-slot <0-65535>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200835 "\t-h\t--help\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100836 "\t-v\t--version\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200837 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
838 "\t-k\t--keep-running\n"
839 "\t-V\t--usb-vendor\tVENDOR_ID\n"
840 "\t-P\t--usb-product\tPRODUCT_ID\n"
841 "\t-C\t--usb-config\tCONFIG_ID\n"
842 "\t-I\t--usb-interface\tINTERFACE_ID\n"
843 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
844 "\t-A\t--usb-address\tADDRESS\n"
845 "\t-H\t--usb-path\tPATH\n"
Kévin Redon206c3d72018-11-12 22:51:37 +0100846 "\t-a\t--atr\tATR\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200847 "\n"
848 );
849}
850
Harald Welte9636d2b2019-12-16 16:26:50 +0100851#define ATR_SIZE_MAX 55
852struct client_config {
853 char *server_host;
854 int server_port;
855
856 int client_id;
857 int client_slot;
858
859 char *gsmtap_host;
860 bool keep_running;
861
862 struct {
863 uint8_t data[ATR_SIZE_MAX];
864 uint8_t len;
865 } atr;
866
867 struct {
868 int vendor_id;
869 int product_id;
870 int config_id;
871 int if_num;
872 int altsetting;
873 int addr;
874 char *path;
875 } usb;
Kévin Redon3ec265b2018-10-11 17:30:33 +0200876};
877
Harald Welte9636d2b2019-12-16 16:26:50 +0100878static struct client_config *client_config_init(void *ctx)
Kévin Redone0b837c2018-10-10 00:39:25 +0200879{
Harald Welte9636d2b2019-12-16 16:26:50 +0100880 struct client_config *cfg = talloc_zero(ctx, struct client_config);
881 if (!cfg)
882 return NULL;
Kévin Redone0b837c2018-10-10 00:39:25 +0200883
Harald Welte9636d2b2019-12-16 16:26:50 +0100884 cfg->server_host = talloc_strdup(cfg, "127.0.0.1");
885 cfg->server_port = 9998;
886 cfg->client_id = -1;
887 cfg->client_slot = -1;
888 cfg->gsmtap_host = talloc_strdup(cfg, "127.0.0.1");
889 cfg->keep_running = false;
890
891 cfg->usb.vendor_id = -1;
892 cfg->usb.product_id = -1;
893 cfg->usb.config_id = -1;
894 cfg->usb.if_num = -1;
895 cfg->usb.altsetting = 0;
896 cfg->usb.addr = -1;
897 cfg->usb.path = NULL;
898
899 cfg->atr.data[0] = 0x3B;
900 cfg->atr.data[1] = 0x00; // the shortest simplest ATR possible
901 cfg->atr.len = 2;
902
903 return cfg;
904};
905
906static void handle_options(struct client_config *cfg, int argc, char **argv)
907{
908 const struct option opts[] = {
909 { "server-host", 1, 0, 's' },
910 { "server-port", 1, 0, 'p' },
911 { "client-id", 1, 0, 'c' },
912 { "client-slot", 1, 0, 'n' },
913 { "help", 0, 0, 'h' },
914 { "version", 0, 0, 'v' },
915 { "gsmtap-ip", 1, 0, 'i' },
916 { "keep-running", 0, 0, 'k' },
917 { "usb-vendor", 1, 0, 'V' },
918 { "usb-product", 1, 0, 'P' },
919 { "usb-config", 1, 0, 'C' },
920 { "usb-interface", 1, 0, 'I' },
921 { "usb-altsetting", 1, 0, 'S' },
922 { "usb-address", 1, 0, 'A' },
923 { "usb-path", 1, 0, 'H' },
924 { "atr", 1, 0, 'a' },
925 { NULL, 0, 0, 0 }
926 };
927 int c, rc;
Kévin Redone0b837c2018-10-10 00:39:25 +0200928
929 while (1) {
930 int option_index = 0;
931
Harald Weltecd7fcd72019-12-03 21:29:07 +0100932 c = getopt_long(argc, argv, "s:p:c:n:hvi:kV:P:C:I:S:A:H:a:", opts, &option_index);
Kévin Redone0b837c2018-10-10 00:39:25 +0200933 if (c == -1)
934 break;
935 switch (c) {
Harald Weltee56f2b92019-03-02 17:02:13 +0100936 case 's':
Harald Welte9636d2b2019-12-16 16:26:50 +0100937 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200938 break;
939 case 'p':
Harald Welte9636d2b2019-12-16 16:26:50 +0100940 cfg->server_port = atoi(optarg);
Kévin Redonfbca97a2018-10-11 19:13:24 +0200941 break;
Harald Welte72cde102019-03-30 10:43:06 +0100942 case 'c':
Harald Welte9636d2b2019-12-16 16:26:50 +0100943 cfg->client_id = atoi(optarg);
Harald Welte72cde102019-03-30 10:43:06 +0100944 break;
945 case 'n':
Harald Welte9636d2b2019-12-16 16:26:50 +0100946 cfg->client_slot = atoi(optarg);
Harald Welte72cde102019-03-30 10:43:06 +0100947 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200948 case 'h':
949 print_help();
950 exit(0);
951 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100952 case 'v':
953 printf("osmo-remsim-client version %s\n", VERSION);
954 exit(0);
955 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200956 case 'i':
Harald Welte9636d2b2019-12-16 16:26:50 +0100957 osmo_talloc_replace_string(cfg, &cfg->gsmtap_host, optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200958 break;
959 case 'k':
Harald Welte9636d2b2019-12-16 16:26:50 +0100960 cfg->keep_running = 1;
Kévin Redone0b837c2018-10-10 00:39:25 +0200961 break;
962 case 'V':
Harald Welte9636d2b2019-12-16 16:26:50 +0100963 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
Kévin Redone0b837c2018-10-10 00:39:25 +0200964 break;
965 case 'P':
Harald Welte9636d2b2019-12-16 16:26:50 +0100966 cfg->usb.product_id = strtol(optarg, NULL, 16);
Kévin Redone0b837c2018-10-10 00:39:25 +0200967 break;
968 case 'C':
Harald Welte9636d2b2019-12-16 16:26:50 +0100969 cfg->usb.config_id = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200970 break;
971 case 'I':
Harald Welte9636d2b2019-12-16 16:26:50 +0100972 cfg->usb.if_num = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200973 break;
974 case 'S':
Harald Welte9636d2b2019-12-16 16:26:50 +0100975 cfg->usb.altsetting = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200976 break;
977 case 'A':
Harald Welte9636d2b2019-12-16 16:26:50 +0100978 cfg->usb.addr = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200979 break;
980 case 'H':
Harald Welte9636d2b2019-12-16 16:26:50 +0100981 cfg->usb.path = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200982 break;
Kévin Redon206c3d72018-11-12 22:51:37 +0100983 case 'a':
Harald Welte9636d2b2019-12-16 16:26:50 +0100984 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
985 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
986 fprintf(stderr, "ATR malformed\n");
987 exit(2);
Kévin Redon206c3d72018-11-12 22:51:37 +0100988 }
Harald Welte9636d2b2019-12-16 16:26:50 +0100989 cfg->atr.len = rc;
Kévin Redon206c3d72018-11-12 22:51:37 +0100990 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200991 }
992 }
993
Harald Welte9636d2b2019-12-16 16:26:50 +0100994 if (argc > optind) {
995 fprintf(stderr, "Unsupported positional arguments on command line\n");
996 exit(2);
Kévin Redone0b837c2018-10-10 00:39:25 +0200997 }
Harald Welte9636d2b2019-12-16 16:26:50 +0100998}
Kévin Redone0b837c2018-10-10 00:39:25 +0200999
Harald Welte9636d2b2019-12-16 16:26:50 +01001000
Harald Weltea86e6b72019-12-16 16:51:34 +01001001static void main_body(struct client_config *cfg)
1002{
1003 struct st_transport *transp = ci->slot->transp;
1004 struct usb_interface_match _ifm, *ifm = &_ifm;
1005 int rc;
1006
1007 ifm->vendor = cfg->usb.vendor_id;
1008 ifm->product = cfg->usb.product_id;
1009 ifm->configuration = cfg->usb.config_id;
1010 ifm->interface = cfg->usb.if_num;
1011 ifm->altsetting = cfg->usb.altsetting;
1012 ifm->addr = cfg->usb.addr;
1013 if (cfg->usb.path)
1014 osmo_strlcpy(ifm->path, cfg->usb.path, sizeof(ifm->path));
1015 transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
1016 if (!transp->usb_devh) {
1017 fprintf(stderr, "can't open USB device\n");
1018 return;
1019 }
1020
1021 rc = libusb_claim_interface(transp->usb_devh, cfg->usb.if_num);
1022 if (rc < 0) {
1023 fprintf(stderr, "can't claim interface %d; rc=%d\n", cfg->usb.if_num, rc);
1024 goto close_exit;
1025 }
1026
1027 rc = osmo_libusb_get_ep_addrs(transp->usb_devh, cfg->usb.if_num, &transp->usb_ep.out,
1028 &transp->usb_ep.in, &transp->usb_ep.irq_in);
1029 if (rc < 0) {
1030 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
1031 goto close_exit;
1032 }
1033
1034 // switch modem SIM port to emulated SIM on OWHW
1035 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
1036 int modem = -1;
1037 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
1038 case 0:
1039 modem = 1;
1040 break;
1041 case 1:
1042 modem = 2;
1043 break;
1044 default:
1045 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
1046 goto close_exit;
1047 }
1048 //
1049 char gpio_path[PATH_MAX];
1050 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
1051 int connec_st_usim = open(gpio_path, O_WRONLY);
1052 if (-1 == connec_st_usim) {
1053 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1054 goto close_exit;
1055 }
1056 if (1 != write(connec_st_usim, "1", 1)) {
1057 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1058 goto close_exit;
1059 }
1060 printf("switched modem %d to emulated USIM\n", modem);
1061
1062 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
1063 int mdm_rst = open(gpio_path, O_WRONLY);
1064 if (-1 == mdm_rst) {
1065 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
1066 goto close_exit;
1067 }
1068 if (1 != write(mdm_rst, "1", 1)) {
1069 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1070 goto close_exit;
1071 }
1072 sleep(1); // wait a bit to ensure reset is effective
1073 if (1 != write(mdm_rst, "0", 1)) {
1074 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1075 goto close_exit;
1076 }
1077 printf("modem %d reset\n", modem);
1078 }
1079
1080 /* request firmware to generate STATUS on IRQ endpoint */
1081 cardem_request_config(ci, CEMU_FEAT_F_STATUS_IRQ);
1082
1083 /* simulate card-insert to modem (owhw, not qmod) */
1084 cardem_request_card_insert(ci, true);
1085
1086 /* select remote (forwarded) SIM */
1087 st_modem_sim_select_remote(ci->slot);
1088
1089 /* set the ATR */
1090 //atr_update_csum(real_atr, sizeof(real_atr));
1091 cardem_request_set_atr(ci, cfg->atr.data, cfg->atr.len);
1092
1093 /* select remote (forwarded) SIM */
1094 st_modem_reset_pulse(ci->slot, 300);
1095
1096 printf("Entering main loop\n");
1097
1098 allocate_and_submit_irq(ci);
1099 allocate_and_submit_in(ci);
1100
Harald Weltea9bc4de2020-02-16 15:40:21 +01001101 while (!g_leave_main) {
Harald Weltea86e6b72019-12-16 16:51:34 +01001102 osmo_select_main(false);
1103 }
Harald Weltea9bc4de2020-02-16 15:40:21 +01001104 g_leave_main = false;
Harald Weltea86e6b72019-12-16 16:51:34 +01001105
1106 libusb_release_interface(transp->usb_devh, 0);
1107
1108close_exit:
1109 if (transp->usb_devh)
1110 libusb_close(transp-> usb_devh);
1111}
1112
Harald Welte9636d2b2019-12-16 16:26:50 +01001113int main(int argc, char **argv)
1114{
1115 struct rspro_server_conn *srvc, *bankdc;
Harald Welte9636d2b2019-12-16 16:26:50 +01001116 struct client_config *cfg;
1117 int rc;
1118 int ret = 1;
1119
1120 print_welcome();
Harald Weltece638d82019-03-17 09:36:04 +01001121
Harald Weltef9187482019-12-14 17:21:08 +01001122 talloc_enable_null_tracking();
Harald Welte972a1e82019-03-30 08:34:30 +01001123 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +02001124 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Weltef7442b52019-07-18 18:54:05 +02001125 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Welte972a1e82019-03-30 08:34:30 +01001126 osmo_init_logging2(g_tall_ctx, &log_info);
1127
Harald Welte9636d2b2019-12-16 16:26:50 +01001128 cfg = client_config_init(g_tall_ctx);
1129 handle_options(cfg, argc, argv);
1130
1131 if (cfg->usb.vendor_id < 0 || cfg->usb.product_id < 0) {
1132 fprintf(stderr, "You have to specify the vendor and product ID\n");
1133 goto do_exit;
1134 }
1135
1136 signal(SIGUSR1, handle_sig_usr1);
1137
Harald Welte32e2e002019-12-15 23:01:54 +01001138 rc = osmo_libusb_init(NULL);
Kévin Redon193a8c12018-10-11 17:24:39 +02001139 if (rc < 0) {
1140 fprintf(stderr, "libusb initialization failed\n");
1141 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +02001142 }
1143
Harald Welte9636d2b2019-12-16 16:26:50 +01001144 g_gti = gsmtap_source_init(cfg->gsmtap_host, GSMTAP_UDP_PORT, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +02001145 if (!g_gti) {
1146 perror("unable to open GSMTAP");
1147 goto close_exit;
1148 }
1149 gsmtap_source_add_sink(g_gti);
1150
1151 signal(SIGINT, &signal_handler);
1152
1153 // initialize remote SIM client
Kévin Redone0b837c2018-10-10 00:39:25 +02001154
Kévin Redone0b837c2018-10-10 00:39:25 +02001155 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +01001156
Harald Welte9636d2b2019-12-16 16:26:50 +01001157 if (cfg->client_id != -1) {
Harald Welte72cde102019-03-30 10:43:06 +01001158 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welte9636d2b2019-12-16 16:26:50 +01001159 g_client->srv_conn.clslot->clientId = cfg->client_id;
1160 /* default to client slot 0 */
1161 if (cfg->client_slot == -1)
1162 g_client->srv_conn.clslot->slotNr = 0;
1163 else
1164 g_client->srv_conn.clslot->slotNr = cfg->client_slot;
Harald Welte3e9860b2019-12-02 23:04:54 +01001165 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
1166 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welte72cde102019-03-30 10:43:06 +01001167 }
1168
Harald Welte9636d2b2019-12-16 16:26:50 +01001169 /* create and [attempt to] establish connection to remsim-server */
Harald Weltee56f2b92019-03-02 17:02:13 +01001170 srvc = &g_client->srv_conn;
Harald Welte9636d2b2019-12-16 16:26:50 +01001171 srvc->server_host = cfg->server_host;
1172 srvc->server_port = cfg->server_port;
Harald Weltee56f2b92019-03-02 17:02:13 +01001173 srvc->handle_rx = srvc_handle_rx;
1174 srvc->own_comp_id.type = ComponentType_remsimClient;
1175 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
1176 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
1177 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
1178 rc = server_conn_fsm_alloc(g_client, srvc);
1179 if (rc < 0) {
1180 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
1181 exit(1);
1182 }
Harald Welted2192e22019-11-07 18:10:57 +01001183 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001184
Harald Welte9636d2b2019-12-16 16:26:50 +01001185 /* create and not yet establish connection to remsim-bankd */
1186 srvc = &g_client->srv_conn;
Harald Welte3e9860b2019-12-02 23:04:54 +01001187 bankdc = &g_client->bankd_conn;
1188 /* server_host / server_port are configured from remsim-server */
1189 bankdc->handle_rx = bankd_handle_rx;
1190 memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id));
1191 rc = server_conn_fsm_alloc(g_client, bankdc);
1192 if (rc < 0) {
1193 fprintf(stderr, "Unable to create bankd conn FSM: %s\n", strerror(errno));
Kévin Redone0b837c2018-10-10 00:39:25 +02001194 exit(1);
1195 }
Harald Welte82194452019-12-14 17:18:34 +01001196 osmo_fsm_inst_update_id(bankdc->fi, "bankd");
Kévin Redone0b837c2018-10-10 00:39:25 +02001197
Harald Welte3e9860b2019-12-02 23:04:54 +01001198 asn_debug = 0;
1199
Kévin Redone0b837c2018-10-10 00:39:25 +02001200 // connect to SIMtrace2 cardem
1201 do {
Harald Weltea86e6b72019-12-16 16:51:34 +01001202 main_body(cfg);
1203 sleep(1);
Harald Welte9636d2b2019-12-16 16:26:50 +01001204 } while (cfg->keep_running);
Kévin Redone0b837c2018-10-10 00:39:25 +02001205
Harald Weltea86e6b72019-12-16 16:51:34 +01001206close_exit:
1207 osmo_libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001208do_exit:
1209 return ret;
1210}