blob: 515fd50beabd2cbdbcd9e319a9c527cecd428e8d [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
Harald Welte5ed46932019-12-17 00:02:36 +0100682static struct cardem_inst *g_ci;
Kévin Redone0b837c2018-10-10 00:39:25 +0200683
684static void signal_handler(int signal)
685{
686 switch (signal) {
687 case SIGINT:
Harald Welte5ed46932019-12-17 00:02:36 +0100688 cardem_request_card_insert(g_ci, false);
Kévin Redone0b837c2018-10-10 00:39:25 +0200689 exit(0);
690 break;
691 default:
692 break;
693 }
694}
695
696/** remsim_client **/
697
Harald Welte3e9860b2019-12-02 23:04:54 +0100698static int bankd_handle_tpduCardToModem(struct bankd_client *bc, const RsproPDU_t *pdu)
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200699{
700 OSMO_ASSERT(pdu);
701 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
702
703 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
704 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
705 return -1;
706 }
707
708 // save SW to our current APDU context
709 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
710 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200711 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 +0200712 if (card2modem->data.size > 2) { // send PB and data to modem
Harald Welte5ed46932019-12-17 00:02:36 +0100713 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 +0200714 }
Harald Welte5ed46932019-12-17 00:02:36 +0100715 cardem_request_sw_tx(bc->cardem, ac.sw); // send SW to modem
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200716
717 return 0;
718}
719
Harald Welte3e9860b2019-12-02 23:04:54 +0100720static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu)
Harald Weltefa365592019-03-28 20:28:57 +0100721{
722 RsproPDU_t *resp;
723 int rc;
724
725 OSMO_ASSERT(pdu);
726 OSMO_ASSERT(RsproPDUchoice_PR_setAtrReq == pdu->msg.present);
727
728 /* FIXME: is this permitted at any time by the SIMtrace2 cardemfirmware? */
Harald Welte5ed46932019-12-17 00:02:36 +0100729 rc = cardem_request_set_atr(bc->cardem, pdu->msg.choice.setAtrReq.atr.buf,
Harald Weltefa365592019-03-28 20:28:57 +0100730 pdu->msg.choice.setAtrReq.atr.size);
731 if (rc == 0)
732 resp = rspro_gen_SetAtrRes(ResultCode_ok);
733 else
734 resp = rspro_gen_SetAtrRes(ResultCode_cardTransmissionError);
735 if (!resp)
736 return -ENOMEM;
Harald Welte3e9860b2019-12-02 23:04:54 +0100737 server_conn_send_rspro(&g_client->bankd_conn, resp);
Harald Weltefa365592019-03-28 20:28:57 +0100738
739 return 0;
740}
741
Harald Welte3e9860b2019-12-02 23:04:54 +0100742/* handle incoming message from bankd */
743static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
Kévin Redone0b837c2018-10-10 00:39:25 +0200744{
Kévin Redone0b837c2018-10-10 00:39:25 +0200745 switch (pdu->msg.present) {
746 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +0100747 /* Store 'identity' of bankd to in peer_comp_id */
Harald Welte3e9860b2019-12-02 23:04:54 +0100748 rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
749 osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
Kévin Redone0b837c2018-10-10 00:39:25 +0200750 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200751 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
Harald Welte3e9860b2019-12-02 23:04:54 +0100752 bankd_handle_tpduCardToModem(g_client, pdu);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200753 break;
Harald Weltefa365592019-03-28 20:28:57 +0100754 case RsproPDUchoice_PR_setAtrReq:
Harald Welte3e9860b2019-12-02 23:04:54 +0100755 bankd_handle_setAtrReq(g_client, pdu);
Harald Weltefa365592019-03-28 20:28:57 +0100756 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200757 default:
Harald Welte3e9860b2019-12-02 23:04:54 +0100758 LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s\n",
759 rspro_msgt_name(pdu));
Kévin Redone0b837c2018-10-10 00:39:25 +0200760 return -1;
761 }
762
763 return 0;
764}
765
Harald Weltee56f2b92019-03-02 17:02:13 +0100766/* handle incoming messages from server */
767static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
768{
769 RsproPDU_t *resp;
770
771 switch (pdu->msg.present) {
772 case RsproPDUchoice_PR_connectClientRes:
773 /* Store 'identity' of server in srvc->peer_comp_id */
774 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
775 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
776 break;
Harald Welted571a3e2019-03-11 22:09:50 +0100777 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100778 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100779 if (!g_client->srv_conn.clslot)
780 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welted571a3e2019-03-11 22:09:50 +0100781 *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
Harald Welte3e9860b2019-12-02 23:04:54 +0100782 if (!g_client->bankd_conn.clslot)
783 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
784 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welted571a3e2019-03-11 22:09:50 +0100785 /* send response to server */
786 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
787 server_conn_send_rspro(srvc, resp);
788 break;
789 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100790 /* store/set the bankd ip/port as instructed by the server */
Harald Welte3e9860b2019-12-02 23:04:54 +0100791 osmo_talloc_replace_string(g_client, &g_client->bankd_conn.server_host,
Harald Welted571a3e2019-03-11 22:09:50 +0100792 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
Harald Welte9cf013a2019-03-11 22:19:19 +0100793 rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
Harald Welte3e9860b2019-12-02 23:04:54 +0100794 g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
Harald Weltee56f2b92019-03-02 17:02:13 +0100795 /* instruct bankd FSM to connect */
Harald Welte3e9860b2019-12-02 23:04:54 +0100796 osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
Harald Weltee56f2b92019-03-02 17:02:13 +0100797 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +0100798 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100799 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +0100800 break;
801 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +0100802 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n",
803 rspro_msgt_name(pdu));
Harald Weltee56f2b92019-03-02 17:02:13 +0100804 return -1;
805 }
806
807 return 0;
808}
809
Harald Weltece638d82019-03-17 09:36:04 +0100810static void handle_sig_usr1(int signal)
811{
812 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +0200813 talloc_report_full(g_tall_ctx, stderr);
Harald Weltef9187482019-12-14 17:21:08 +0100814 printf("===== NULL\n");
815 talloc_report_full(NULL, stderr);
Harald Weltece638d82019-03-17 09:36:04 +0100816}
Harald Weltee56f2b92019-03-02 17:02:13 +0100817
Kévin Redon3ec265b2018-10-11 17:30:33 +0200818static void print_welcome(void)
819{
820 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
Harald Welte32e2e002019-12-15 23:01:54 +0100821 "(C) 2010-2019, Harald Welte <laforge@gnumonks.org>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200822 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
823}
824
825static void print_help(void)
826{
Harald Weltee56f2b92019-03-02 17:02:13 +0100827 printf( "\t-s\t--server-host HOST\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200828 "\t-p\t--server-port PORT\n"
Harald Welte72cde102019-03-30 10:43:06 +0100829 "\t-c\t--client-id <0-65535>\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200830 "\t-n\t--client-slot <0-65535>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200831 "\t-h\t--help\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100832 "\t-v\t--version\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200833 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
834 "\t-k\t--keep-running\n"
835 "\t-V\t--usb-vendor\tVENDOR_ID\n"
836 "\t-P\t--usb-product\tPRODUCT_ID\n"
837 "\t-C\t--usb-config\tCONFIG_ID\n"
838 "\t-I\t--usb-interface\tINTERFACE_ID\n"
839 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
840 "\t-A\t--usb-address\tADDRESS\n"
841 "\t-H\t--usb-path\tPATH\n"
Kévin Redon206c3d72018-11-12 22:51:37 +0100842 "\t-a\t--atr\tATR\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200843 "\n"
844 );
845}
846
Harald Welte9636d2b2019-12-16 16:26:50 +0100847static struct client_config *client_config_init(void *ctx)
Kévin Redone0b837c2018-10-10 00:39:25 +0200848{
Harald Welte9636d2b2019-12-16 16:26:50 +0100849 struct client_config *cfg = talloc_zero(ctx, struct client_config);
850 if (!cfg)
851 return NULL;
Kévin Redone0b837c2018-10-10 00:39:25 +0200852
Harald Welte9636d2b2019-12-16 16:26:50 +0100853 cfg->server_host = talloc_strdup(cfg, "127.0.0.1");
854 cfg->server_port = 9998;
855 cfg->client_id = -1;
856 cfg->client_slot = -1;
857 cfg->gsmtap_host = talloc_strdup(cfg, "127.0.0.1");
858 cfg->keep_running = false;
859
860 cfg->usb.vendor_id = -1;
861 cfg->usb.product_id = -1;
862 cfg->usb.config_id = -1;
863 cfg->usb.if_num = -1;
864 cfg->usb.altsetting = 0;
865 cfg->usb.addr = -1;
866 cfg->usb.path = NULL;
867
868 cfg->atr.data[0] = 0x3B;
869 cfg->atr.data[1] = 0x00; // the shortest simplest ATR possible
870 cfg->atr.len = 2;
871
872 return cfg;
873};
874
875static void handle_options(struct client_config *cfg, int argc, char **argv)
876{
877 const struct option opts[] = {
878 { "server-host", 1, 0, 's' },
879 { "server-port", 1, 0, 'p' },
880 { "client-id", 1, 0, 'c' },
881 { "client-slot", 1, 0, 'n' },
882 { "help", 0, 0, 'h' },
883 { "version", 0, 0, 'v' },
884 { "gsmtap-ip", 1, 0, 'i' },
885 { "keep-running", 0, 0, 'k' },
886 { "usb-vendor", 1, 0, 'V' },
887 { "usb-product", 1, 0, 'P' },
888 { "usb-config", 1, 0, 'C' },
889 { "usb-interface", 1, 0, 'I' },
890 { "usb-altsetting", 1, 0, 'S' },
891 { "usb-address", 1, 0, 'A' },
892 { "usb-path", 1, 0, 'H' },
893 { "atr", 1, 0, 'a' },
894 { NULL, 0, 0, 0 }
895 };
896 int c, rc;
Kévin Redone0b837c2018-10-10 00:39:25 +0200897
898 while (1) {
899 int option_index = 0;
900
Harald Weltecd7fcd72019-12-03 21:29:07 +0100901 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 +0200902 if (c == -1)
903 break;
904 switch (c) {
Harald Weltee56f2b92019-03-02 17:02:13 +0100905 case 's':
Harald Welte9636d2b2019-12-16 16:26:50 +0100906 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200907 break;
908 case 'p':
Harald Welte9636d2b2019-12-16 16:26:50 +0100909 cfg->server_port = atoi(optarg);
Kévin Redonfbca97a2018-10-11 19:13:24 +0200910 break;
Harald Welte72cde102019-03-30 10:43:06 +0100911 case 'c':
Harald Welte9636d2b2019-12-16 16:26:50 +0100912 cfg->client_id = atoi(optarg);
Harald Welte72cde102019-03-30 10:43:06 +0100913 break;
914 case 'n':
Harald Welte9636d2b2019-12-16 16:26:50 +0100915 cfg->client_slot = atoi(optarg);
Harald Welte72cde102019-03-30 10:43:06 +0100916 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200917 case 'h':
918 print_help();
919 exit(0);
920 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100921 case 'v':
922 printf("osmo-remsim-client version %s\n", VERSION);
923 exit(0);
924 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200925 case 'i':
Harald Welte9636d2b2019-12-16 16:26:50 +0100926 osmo_talloc_replace_string(cfg, &cfg->gsmtap_host, optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200927 break;
928 case 'k':
Harald Welte9636d2b2019-12-16 16:26:50 +0100929 cfg->keep_running = 1;
Kévin Redone0b837c2018-10-10 00:39:25 +0200930 break;
931 case 'V':
Harald Welte9636d2b2019-12-16 16:26:50 +0100932 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
Kévin Redone0b837c2018-10-10 00:39:25 +0200933 break;
934 case 'P':
Harald Welte9636d2b2019-12-16 16:26:50 +0100935 cfg->usb.product_id = strtol(optarg, NULL, 16);
Kévin Redone0b837c2018-10-10 00:39:25 +0200936 break;
937 case 'C':
Harald Welte9636d2b2019-12-16 16:26:50 +0100938 cfg->usb.config_id = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200939 break;
940 case 'I':
Harald Welte9636d2b2019-12-16 16:26:50 +0100941 cfg->usb.if_num = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200942 break;
943 case 'S':
Harald Welte9636d2b2019-12-16 16:26:50 +0100944 cfg->usb.altsetting = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200945 break;
946 case 'A':
Harald Welte9636d2b2019-12-16 16:26:50 +0100947 cfg->usb.addr = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200948 break;
949 case 'H':
Harald Welte9636d2b2019-12-16 16:26:50 +0100950 cfg->usb.path = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200951 break;
Kévin Redon206c3d72018-11-12 22:51:37 +0100952 case 'a':
Harald Welte9636d2b2019-12-16 16:26:50 +0100953 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
954 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
955 fprintf(stderr, "ATR malformed\n");
956 exit(2);
Kévin Redon206c3d72018-11-12 22:51:37 +0100957 }
Harald Welte9636d2b2019-12-16 16:26:50 +0100958 cfg->atr.len = rc;
Kévin Redon206c3d72018-11-12 22:51:37 +0100959 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200960 }
961 }
962
Harald Welte9636d2b2019-12-16 16:26:50 +0100963 if (argc > optind) {
964 fprintf(stderr, "Unsupported positional arguments on command line\n");
965 exit(2);
Kévin Redone0b837c2018-10-10 00:39:25 +0200966 }
Harald Welte9636d2b2019-12-16 16:26:50 +0100967}
Kévin Redone0b837c2018-10-10 00:39:25 +0200968
Harald Welte9636d2b2019-12-16 16:26:50 +0100969
Harald Welte5ed46932019-12-17 00:02:36 +0100970static void main_body(struct cardem_inst *ci, struct client_config *cfg)
Harald Weltea86e6b72019-12-16 16:51:34 +0100971{
972 struct st_transport *transp = ci->slot->transp;
973 struct usb_interface_match _ifm, *ifm = &_ifm;
974 int rc;
975
976 ifm->vendor = cfg->usb.vendor_id;
977 ifm->product = cfg->usb.product_id;
978 ifm->configuration = cfg->usb.config_id;
979 ifm->interface = cfg->usb.if_num;
980 ifm->altsetting = cfg->usb.altsetting;
981 ifm->addr = cfg->usb.addr;
982 if (cfg->usb.path)
983 osmo_strlcpy(ifm->path, cfg->usb.path, sizeof(ifm->path));
984 transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
985 if (!transp->usb_devh) {
986 fprintf(stderr, "can't open USB device\n");
987 return;
988 }
989
990 rc = libusb_claim_interface(transp->usb_devh, cfg->usb.if_num);
991 if (rc < 0) {
992 fprintf(stderr, "can't claim interface %d; rc=%d\n", cfg->usb.if_num, rc);
993 goto close_exit;
994 }
995
996 rc = osmo_libusb_get_ep_addrs(transp->usb_devh, cfg->usb.if_num, &transp->usb_ep.out,
997 &transp->usb_ep.in, &transp->usb_ep.irq_in);
998 if (rc < 0) {
999 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
1000 goto close_exit;
1001 }
1002
1003 // switch modem SIM port to emulated SIM on OWHW
1004 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
1005 int modem = -1;
1006 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
1007 case 0:
1008 modem = 1;
1009 break;
1010 case 1:
1011 modem = 2;
1012 break;
1013 default:
1014 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
1015 goto close_exit;
1016 }
1017 //
1018 char gpio_path[PATH_MAX];
1019 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
1020 int connec_st_usim = open(gpio_path, O_WRONLY);
1021 if (-1 == connec_st_usim) {
1022 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1023 goto close_exit;
1024 }
1025 if (1 != write(connec_st_usim, "1", 1)) {
1026 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1027 goto close_exit;
1028 }
1029 printf("switched modem %d to emulated USIM\n", modem);
1030
1031 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
1032 int mdm_rst = open(gpio_path, O_WRONLY);
1033 if (-1 == mdm_rst) {
1034 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
1035 goto close_exit;
1036 }
1037 if (1 != write(mdm_rst, "1", 1)) {
1038 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1039 goto close_exit;
1040 }
1041 sleep(1); // wait a bit to ensure reset is effective
1042 if (1 != write(mdm_rst, "0", 1)) {
1043 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1044 goto close_exit;
1045 }
1046 printf("modem %d reset\n", modem);
1047 }
1048
1049 /* request firmware to generate STATUS on IRQ endpoint */
1050 cardem_request_config(ci, CEMU_FEAT_F_STATUS_IRQ);
1051
1052 /* simulate card-insert to modem (owhw, not qmod) */
1053 cardem_request_card_insert(ci, true);
1054
1055 /* select remote (forwarded) SIM */
1056 st_modem_sim_select_remote(ci->slot);
1057
1058 /* set the ATR */
1059 //atr_update_csum(real_atr, sizeof(real_atr));
1060 cardem_request_set_atr(ci, cfg->atr.data, cfg->atr.len);
1061
1062 /* select remote (forwarded) SIM */
1063 st_modem_reset_pulse(ci->slot, 300);
1064
1065 printf("Entering main loop\n");
1066
1067 allocate_and_submit_irq(ci);
1068 allocate_and_submit_in(ci);
1069
Harald Weltea9bc4de2020-02-16 15:40:21 +01001070 while (!g_leave_main) {
Harald Weltea86e6b72019-12-16 16:51:34 +01001071 osmo_select_main(false);
1072 }
Harald Weltea9bc4de2020-02-16 15:40:21 +01001073 g_leave_main = false;
Harald Weltea86e6b72019-12-16 16:51:34 +01001074
1075 libusb_release_interface(transp->usb_devh, 0);
1076
1077close_exit:
1078 if (transp->usb_devh)
1079 libusb_close(transp-> usb_devh);
1080}
1081
Harald Welte9636d2b2019-12-16 16:26:50 +01001082int main(int argc, char **argv)
1083{
1084 struct rspro_server_conn *srvc, *bankdc;
Harald Welte9636d2b2019-12-16 16:26:50 +01001085 struct client_config *cfg;
1086 int rc;
1087 int ret = 1;
1088
1089 print_welcome();
Harald Weltece638d82019-03-17 09:36:04 +01001090
Harald Weltef9187482019-12-14 17:21:08 +01001091 talloc_enable_null_tracking();
Harald Welte972a1e82019-03-30 08:34:30 +01001092 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +02001093 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Weltef7442b52019-07-18 18:54:05 +02001094 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Welte972a1e82019-03-30 08:34:30 +01001095 osmo_init_logging2(g_tall_ctx, &log_info);
1096
Harald Welte5ed46932019-12-17 00:02:36 +01001097 g_ci = talloc_zero(g_tall_ctx, struct cardem_inst);
1098 g_ci->slot = &_slot;
1099
1100 cfg = client_config_init(g_ci);
Harald Welte9636d2b2019-12-16 16:26:50 +01001101 handle_options(cfg, argc, argv);
1102
1103 if (cfg->usb.vendor_id < 0 || cfg->usb.product_id < 0) {
1104 fprintf(stderr, "You have to specify the vendor and product ID\n");
1105 goto do_exit;
1106 }
1107
1108 signal(SIGUSR1, handle_sig_usr1);
1109
Harald Welte32e2e002019-12-15 23:01:54 +01001110 rc = osmo_libusb_init(NULL);
Kévin Redon193a8c12018-10-11 17:24:39 +02001111 if (rc < 0) {
1112 fprintf(stderr, "libusb initialization failed\n");
1113 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +02001114 }
1115
Harald Welte9636d2b2019-12-16 16:26:50 +01001116 g_gti = gsmtap_source_init(cfg->gsmtap_host, GSMTAP_UDP_PORT, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +02001117 if (!g_gti) {
1118 perror("unable to open GSMTAP");
1119 goto close_exit;
1120 }
1121 gsmtap_source_add_sink(g_gti);
1122
1123 signal(SIGINT, &signal_handler);
1124
1125 // initialize remote SIM client
Kévin Redone0b837c2018-10-10 00:39:25 +02001126
Kévin Redone0b837c2018-10-10 00:39:25 +02001127 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Welte6a0248b2019-12-17 01:11:24 +01001128 g_client->cfg = cfg;
Harald Welte5ed46932019-12-17 00:02:36 +01001129 g_client->cardem = g_ci;
Harald Weltee56f2b92019-03-02 17:02:13 +01001130
Harald Welte9636d2b2019-12-16 16:26:50 +01001131 if (cfg->client_id != -1) {
Harald Welte72cde102019-03-30 10:43:06 +01001132 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welte9636d2b2019-12-16 16:26:50 +01001133 g_client->srv_conn.clslot->clientId = cfg->client_id;
1134 /* default to client slot 0 */
1135 if (cfg->client_slot == -1)
1136 g_client->srv_conn.clslot->slotNr = 0;
1137 else
1138 g_client->srv_conn.clslot->slotNr = cfg->client_slot;
Harald Welte3e9860b2019-12-02 23:04:54 +01001139 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
1140 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welte72cde102019-03-30 10:43:06 +01001141 }
1142
Harald Welte9636d2b2019-12-16 16:26:50 +01001143 /* create and [attempt to] establish connection to remsim-server */
Harald Weltee56f2b92019-03-02 17:02:13 +01001144 srvc = &g_client->srv_conn;
Harald Welte9636d2b2019-12-16 16:26:50 +01001145 srvc->server_host = cfg->server_host;
1146 srvc->server_port = cfg->server_port;
Harald Weltee56f2b92019-03-02 17:02:13 +01001147 srvc->handle_rx = srvc_handle_rx;
1148 srvc->own_comp_id.type = ComponentType_remsimClient;
1149 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
1150 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
1151 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
1152 rc = server_conn_fsm_alloc(g_client, srvc);
1153 if (rc < 0) {
1154 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
1155 exit(1);
1156 }
Harald Welted2192e22019-11-07 18:10:57 +01001157 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001158
Harald Welte9636d2b2019-12-16 16:26:50 +01001159 /* create and not yet establish connection to remsim-bankd */
1160 srvc = &g_client->srv_conn;
Harald Welte3e9860b2019-12-02 23:04:54 +01001161 bankdc = &g_client->bankd_conn;
1162 /* server_host / server_port are configured from remsim-server */
1163 bankdc->handle_rx = bankd_handle_rx;
1164 memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id));
1165 rc = server_conn_fsm_alloc(g_client, bankdc);
1166 if (rc < 0) {
1167 fprintf(stderr, "Unable to create bankd conn FSM: %s\n", strerror(errno));
Kévin Redone0b837c2018-10-10 00:39:25 +02001168 exit(1);
1169 }
Harald Welte82194452019-12-14 17:18:34 +01001170 osmo_fsm_inst_update_id(bankdc->fi, "bankd");
Kévin Redone0b837c2018-10-10 00:39:25 +02001171
Harald Welte3e9860b2019-12-02 23:04:54 +01001172 asn_debug = 0;
1173
Kévin Redone0b837c2018-10-10 00:39:25 +02001174 // connect to SIMtrace2 cardem
1175 do {
Harald Welte5ed46932019-12-17 00:02:36 +01001176 main_body(g_ci, cfg);
Harald Weltea86e6b72019-12-16 16:51:34 +01001177 sleep(1);
Harald Welte9636d2b2019-12-16 16:26:50 +01001178 } while (cfg->keep_running);
Kévin Redone0b837c2018-10-10 00:39:25 +02001179
Harald Weltea86e6b72019-12-16 16:51:34 +01001180close_exit:
1181 osmo_libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001182do_exit:
1183 return ret;
1184}