blob: 79eb32bee20b4d9d53a1532a5544ab6f85a0534e [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 Welte3f09f632019-03-31 15:51:13 +020099__attribute__((unused)) static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200100{
101 struct gsmtap_hdr *gh;
102 unsigned int gross_len = len + sizeof(*gh);
103 uint8_t *buf = malloc(gross_len);
104 int rc;
105
106 if (!buf)
107 return -ENOMEM;
108
109 memset(buf, 0, sizeof(*gh));
110 gh = (struct gsmtap_hdr *) buf;
111 gh->version = GSMTAP_VERSION;
112 gh->hdr_len = sizeof(*gh)/4;
113 gh->type = GSMTAP_TYPE_SIM;
114
115 memcpy(buf + sizeof(*gh), apdu, len);
116
117 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
118 if (rc < 0) {
119 perror("write gsmtap");
120 free(buf);
121 return rc;
122 }
123
124 free(buf);
125 return 0;
126}
127
128/***********************************************************************
Harald Weltef14dc042019-03-28 20:29:36 +0100129 * SIMTRACE core protocol
Kévin Redone0b837c2018-10-10 00:39:25 +0200130 ***********************************************************************/
131
132/*! \brief allocate a message buffer for simtrace use */
133static struct msgb *st_msgb_alloc(void)
134{
135 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
136}
137
138#if 0
139static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
140{
141 printf("APDU: %s\n", osmo_hexdump(buf, len));
142 gsmtap_send_sim(buf, len);
143}
144#endif
145
Harald Welte32e2e002019-12-15 23:01:54 +0100146static void usb_out_xfer_cb(struct libusb_transfer *xfer)
147{
148 struct msgb *msg = xfer->user_data;
149
150 switch (xfer->status) {
151 case LIBUSB_TRANSFER_COMPLETED:
152 break;
153 case LIBUSB_TRANSFER_NO_DEVICE:
154 fprintf(stderr, "USB device disappeared\n");
155 exit(23);
156 break;
157 default:
158 osmo_panic("USB OUT transfer failed, status=%u\n", xfer->status);
159 break;
160 }
161
162 msgb_free(msg);
163 libusb_free_transfer(xfer);
164}
165
Kévin Redone0b837c2018-10-10 00:39:25 +0200166/*! \brief Transmit a given command to the SIMtrace2 device */
167int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
168{
Harald Welte32e2e002019-12-15 23:01:54 +0100169 struct libusb_transfer *xfer;
Kévin Redone0b837c2018-10-10 00:39:25 +0200170 int rc;
171
Kévin Redon21e31de2018-10-11 17:25:58 +0200172 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redone0b837c2018-10-10 00:39:25 +0200173
Harald Welte32e2e002019-12-15 23:01:54 +0100174 xfer = libusb_alloc_transfer(0);
175 OSMO_ASSERT(xfer);
176 xfer->dev_handle = transp->usb_devh;
177 xfer->flags = 0;
178 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
179 xfer->endpoint = transp->usb_ep.out;
180 xfer->timeout = 1000;
181 xfer->user_data = msg;
182 xfer->length = msgb_length(msg);
183 xfer->buffer = msgb_data(msg);
184 xfer->callback = usb_out_xfer_cb;
Kévin Redone0b837c2018-10-10 00:39:25 +0200185
Harald Welte32e2e002019-12-15 23:01:54 +0100186 /* submit the OUT transfer */
187 rc = libusb_submit_transfer(xfer);
188 OSMO_ASSERT(rc == 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200189
Kévin Redone0b837c2018-10-10 00:39:25 +0200190 return rc;
191}
192
193static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
194 uint8_t slot_nr)
195{
196 struct simtrace_msg_hdr *sh;
197
198 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
199 memset(sh, 0, sizeof(*sh));
200 sh->msg_class = msg_class;
201 sh->msg_type = msg_type;
202 sh->slot_nr = slot_nr;
203 sh->msg_len = msgb_length(msg);
204
205 return sh;
206}
207
208/* transmit a given message to a specified slot. Expects all headers
209 * present before calling the function */
210int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
211 uint8_t msg_class, uint8_t msg_type)
212{
213 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
214
215 return st_transp_tx_msg(slot->transp, msg);
216}
217
218/***********************************************************************
219 * Card Emulation protocol
220 ***********************************************************************/
221
222
223/*! \brief Request the SIMtrace2 to generate a card-insert signal */
224static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
225{
226 struct msgb *msg = st_msgb_alloc();
227 struct cardemu_usb_msg_cardinsert *cins;
228
229 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
230 memset(cins, 0, sizeof(*cins));
231 if (inserted)
232 cins->card_insert = 1;
233
234 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
235}
236
237/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
238static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
239{
240 struct msgb *msg = st_msgb_alloc();
241 struct cardemu_usb_msg_tx_data *txd;
242 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
243
Kévin Redon21e31de2018-10-11 17:25:58 +0200244 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redone0b837c2018-10-10 00:39:25 +0200245
246 memset(txd, 0, sizeof(*txd));
247 txd->data_len = 1;
248 txd->flags = CEMU_DATA_F_PB_AND_RX;
249 /* one data byte */
250 msgb_put_u8(msg, pb);
251
252 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
253}
254
255/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
256static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
Kévin Redonf120b642018-10-15 19:53:02 +0200257 const uint8_t *data, uint16_t data_len_in)
Kévin Redone0b837c2018-10-10 00:39:25 +0200258{
259 struct msgb *msg = st_msgb_alloc();
260 struct cardemu_usb_msg_tx_data *txd;
261 uint8_t *cur;
262
263 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
264
Kévin Redon21e31de2018-10-11 17:25:58 +0200265 printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb,
Kévin Redone0b837c2018-10-10 00:39:25 +0200266 osmo_hexdump(data, data_len_in), data_len_in);
267
268 memset(txd, 0, sizeof(*txd));
269 txd->data_len = 1 + data_len_in;
270 txd->flags = CEMU_DATA_F_PB_AND_TX;
271 /* procedure byte */
272 msgb_put_u8(msg, pb);
273 /* data */
274 cur = msgb_put(msg, data_len_in);
275 memcpy(cur, data, data_len_in);
276
277 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
278}
279
280/*! \brief Request the SIMtrace2 to send a Status Word */
281static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
282{
283 struct msgb *msg = st_msgb_alloc();
284 struct cardemu_usb_msg_tx_data *txd;
285 uint8_t *cur;
286
287 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
288
Kévin Redon21e31de2018-10-11 17:25:58 +0200289 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redone0b837c2018-10-10 00:39:25 +0200290
291 memset(txd, 0, sizeof(*txd));
292 txd->data_len = 2;
293 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
294 cur = msgb_put(msg, 2);
295 cur[0] = sw[0];
296 cur[1] = sw[1];
297
298 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
299}
300
Harald Welte0e0e9322019-12-16 12:47:18 +0100301/*! \brief Request the SIMtrace2 to send a Status Word */
302static int cardem_request_config(struct cardem_inst *ci, uint32_t features)
303{
304 struct msgb *msg = st_msgb_alloc();
305 struct cardemu_usb_msg_config *cfg;
306
307 cfg = (struct cardemu_usb_msg_config *) msgb_put(msg, sizeof(*cfg));
308
309 printf("SIMtrace <= %s(%08x)\n", __func__, features);
310
311 memset(cfg, 0, sizeof(*cfg));
312 cfg->features = features;
313
314 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_BD_CEMU_CONFIG);
315}
316
Kévin Redon206c3d72018-11-12 22:51:37 +0100317// FIXME check if the ATR actually includes a checksum
Harald Welte3f09f632019-03-31 15:51:13 +0200318__attribute__((unused)) static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200319{
320 uint8_t csum = 0;
321 int i;
322
323 for (i = 1; i < atr_len - 1; i++)
324 csum = csum ^ atr[i];
325
326 atr[atr_len-1] = csum;
327}
328
329static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
330{
331 struct msgb *msg = st_msgb_alloc();
332 struct cardemu_usb_msg_set_atr *satr;
333 uint8_t *cur;
334
335 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
336
Kévin Redon21e31de2018-10-11 17:25:58 +0200337 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200338
339 memset(satr, 0, sizeof(*satr));
340 satr->atr_len = atr_len;
341 cur = msgb_put(msg, atr_len);
342 memcpy(cur, atr, atr_len);
343
344 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
345}
346
347/***********************************************************************
348 * Modem Control protocol
349 ***********************************************************************/
350
351static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
352{
353 struct msgb *msg = st_msgb_alloc();
354 struct st_modem_reset *sr ;
355
356 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
357 sr->asserted = asserted;
358 sr->pulse_duration_msec = pulse_ms;
359
360 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
361}
362
363/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
364int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
365{
366 return _modem_reset(slot, 2, duration_ms);
367}
368
369/*! \brief assert the RESET line of the modem */
370int st_modem_reset_active(struct st_slot *slot)
371{
372 return _modem_reset(slot, 1, 0);
373}
374
375/*! \brief de-assert the RESET line of the modem */
376int st_modem_reset_inactive(struct st_slot *slot)
377{
378 return _modem_reset(slot, 0, 0);
379}
380
381static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
382{
383 struct msgb *msg = st_msgb_alloc();
384 struct st_modem_sim_select *ss;
385
386 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
387 ss->remote_sim = remote_sim;
388
389 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
390}
391
392/*! \brief select local (physical) SIM for given slot */
393int st_modem_sim_select_local(struct st_slot *slot)
394{
395 return _modem_sim_select(slot, 0);
396}
397
398/*! \brief select remote (emulated/forwarded) SIM for given slot */
399int st_modem_sim_select_remote(struct st_slot *slot)
400{
401 return _modem_sim_select(slot, 1);
402}
403
404/*! \brief Request slot to send us status information about the modem */
405int st_modem_get_status(struct st_slot *slot)
406{
407 struct msgb *msg = st_msgb_alloc();
408
409 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
410}
411
412
413/***********************************************************************
414 * Incoming Messages
415 ***********************************************************************/
416
417/*! \brief Process a STATUS message from the SIMtrace2 */
418static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
419{
420 struct cardemu_usb_msg_status *status;
421 status = (struct cardemu_usb_msg_status *) buf;
422
Kévin Redon21e31de2018-10-11 17:25:58 +0200423 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200424 status->flags, status->fi, status->di, status->wi,
425 status->waiting_time);
426
427 return 0;
428}
429
430/*! \brief Process a PTS indication message from the SIMtrace2 */
431static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
432{
433 struct cardemu_usb_msg_pts_info *pts;
434 pts = (struct cardemu_usb_msg_pts_info *) buf;
435
Kévin Redon21e31de2018-10-11 17:25:58 +0200436 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redone0b837c2018-10-10 00:39:25 +0200437
438 return 0;
439}
440
441/*! \brief Process a ERROR indication message from the SIMtrace2 */
Harald Welte3f09f632019-03-31 15:51:13 +0200442__attribute__((unused)) static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200443{
444 struct cardemu_usb_msg_error *err;
445 err = (struct cardemu_usb_msg_error *) buf;
446
Kévin Redon21e31de2018-10-11 17:25:58 +0200447 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200448 err->severity, err->subsystem, err->code,
449 err->msg_len ? (char *)err->msg : "");
450
451 return 0;
452}
453
Harald Weltefd5dafc2019-12-15 21:14:14 +0100454static struct osmo_apdu_context ac; // this will hold the complete APDU (across calls)
Kévin Redonbc08db52018-10-11 08:41:00 +0200455
Kévin Redone0b837c2018-10-10 00:39:25 +0200456/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
457static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
458{
Kévin Redonbc08db52018-10-11 08:41:00 +0200459 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 +0200460 int rc;
461
Kévin Redon21e31de2018-10-11 17:25:58 +0200462 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redone0b837c2018-10-10 00:39:25 +0200463 osmo_hexdump(data->data, data->data_len));
464
Harald Weltefd5dafc2019-12-15 21:14:14 +0100465 rc = osmo_apdu_segment_in(&ac, data->data, data->data_len,
466 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200467
Kévin Redonbc08db52018-10-11 08:41:00 +0200468 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
Harald Welte2ea20b92019-03-30 08:33:49 +0100469 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 +0200470 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redone0b837c2018-10-10 00:39:25 +0200471 if (ac.lc.tot) {
Kévin Redonbc08db52018-10-11 08:41:00 +0200472 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redone0b837c2018-10-10 00:39:25 +0200473 }
Kévin Redonbc08db52018-10-11 08:41:00 +0200474 // send APDU to card
Harald Welte9cf013a2019-03-11 22:19:19 +0100475 BankSlot_t bslot;
476 bank_slot2rspro(&bslot, &g_client->bankd_slot);
477 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 +0100478 server_conn_send_rspro(&g_client->bankd_conn, pdu);
Kévin Redonbc08db52018-10-11 08:41:00 +0200479 // the response will come separately
Kévin Redonbc08db52018-10-11 08:41:00 +0200480 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
481 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 +0200482 }
483 return 0;
484}
485
486#if 0
487 case SIMTRACE_CMD_DO_ERROR
488 rc = process_do_error(ci, buf, len);
489 break;
490#endif
491
492/*! \brief Process an incoming message from the SIMtrace2 */
493static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
494{
495 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
496 int rc;
497
Kévin Redon21e31de2018-10-11 17:25:58 +0200498 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200499
500 buf += sizeof(*sh);
501
502 switch (sh->msg_type) {
503 case SIMTRACE_MSGT_BD_CEMU_STATUS:
504 rc = process_do_status(ci, buf, len);
505 break;
506 case SIMTRACE_MSGT_DO_CEMU_PTS:
507 rc = process_do_pts(ci, buf, len);
508 break;
509 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
510 rc = process_do_rx_da(ci, buf, len);
511 break;
Harald Welte0e0e9322019-12-16 12:47:18 +0100512 case SIMTRACE_MSGT_BD_CEMU_CONFIG:
513 /* firmware confirms configuration change; ignore */
514 break;
515 default:
516 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
517 rc = -1;
518 break;
519 }
520
521 return rc;
522}
523
524
525/*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */
526static int process_irq_status(struct cardem_inst *ci, const uint8_t *buf, int len)
527{
528 const struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf;
529
530 printf("SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
531 status->flags, status->fi, status->di, status->wi,
532 status->waiting_time);
533
534 BankSlot_t bslot;
535 bank_slot2rspro(&bslot, &g_client->bankd_slot);
536 RsproPDU_t *pdu = rspro_gen_ClientSlotStatusInd(g_client->srv_conn.clslot, &bslot,
537 status->flags & CEMU_STATUS_F_RESET_ACTIVE,
538 status->flags & CEMU_STATUS_F_VCC_PRESENT,
539 status->flags & CEMU_STATUS_F_CLK_ACTIVE,
540 -1 /* FIXME: make this dependent on board */);
541 server_conn_send_rspro(&g_client->bankd_conn, pdu);
542
543 return 0;
544}
545
546static int process_usb_msg_irq(struct cardem_inst *ci, const uint8_t *buf, unsigned int len)
547{
548 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
549 int rc;
550
551 printf("SIMtrace IRQ %s\n", osmo_hexdump(buf, len));
552
553 buf += sizeof(*sh);
554
555 switch (sh->msg_type) {
556 case SIMTRACE_MSGT_BD_CEMU_STATUS:
557 rc = process_irq_status(ci, buf, len);
558 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200559 default:
560 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
561 rc = -1;
562 break;
563 }
564
565 return rc;
566}
567
Harald Welte32e2e002019-12-15 23:01:54 +0100568static void usb_in_xfer_cb(struct libusb_transfer *xfer)
Kévin Redone0b837c2018-10-10 00:39:25 +0200569{
Harald Welte32e2e002019-12-15 23:01:54 +0100570 struct cardem_inst *ci = xfer->user_data;
Kévin Redone0b837c2018-10-10 00:39:25 +0200571 int rc;
572
Harald Welte32e2e002019-12-15 23:01:54 +0100573 switch (xfer->status) {
574 case LIBUSB_TRANSFER_COMPLETED:
575 /* hand the message up the stack */
576 process_usb_msg(ci, xfer->buffer, xfer->actual_length);
577 break;
578 case LIBUSB_TRANSFER_NO_DEVICE:
579 fprintf(stderr, "USB device disappeared\n");
580 exit(23);
581 break;
582 default:
583 osmo_panic("USB IN transfer failed, status=%u\n", xfer->status);
584 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200585 }
Harald Welte32e2e002019-12-15 23:01:54 +0100586
587 /* re-submit the IN transfer */
588 rc = libusb_submit_transfer(xfer);
589 OSMO_ASSERT(rc == 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200590}
591
Harald Welte32e2e002019-12-15 23:01:54 +0100592
593static void allocate_and_submit_in(struct cardem_inst *ci)
594{
595 struct st_transport *transp = ci->slot->transp;
596 struct libusb_transfer *xfer;
597 int rc;
598
599 xfer = libusb_alloc_transfer(0);
600 OSMO_ASSERT(xfer);
601 xfer->dev_handle = transp->usb_devh;
602 xfer->flags = 0;
603 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
604 xfer->endpoint = transp->usb_ep.in;
605 xfer->timeout = 0;
606 xfer->user_data = ci;
607 xfer->length = 16*256;
608
609 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
610 OSMO_ASSERT(xfer->buffer);
611 xfer->callback = usb_in_xfer_cb;
612
613 /* submit the IN transfer */
614 rc = libusb_submit_transfer(xfer);
615 OSMO_ASSERT(rc == 0);
616}
617
618
619static void usb_irq_xfer_cb(struct libusb_transfer *xfer)
620{
Harald Welte0e0e9322019-12-16 12:47:18 +0100621 struct cardem_inst *ci = xfer->user_data;
Harald Welte32e2e002019-12-15 23:01:54 +0100622 int rc;
623
624 switch (xfer->status) {
625 case LIBUSB_TRANSFER_COMPLETED:
Harald Welte0e0e9322019-12-16 12:47:18 +0100626 process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length);
Harald Welte32e2e002019-12-15 23:01:54 +0100627 break;
628 case LIBUSB_TRANSFER_NO_DEVICE:
629 fprintf(stderr, "USB device disappeared\n");
630 exit(23);
631 break;
632 default:
633 osmo_panic("USB IRQ transfer failed, status=%u\n", xfer->status);
634 break;
635 }
636
637 /* re-submit the IN transfer */
638 rc = libusb_submit_transfer(xfer);
639 OSMO_ASSERT(rc == 0);
640}
641
642
643static void allocate_and_submit_irq(struct cardem_inst *ci)
644{
645 struct st_transport *transp = ci->slot->transp;
646 struct libusb_transfer *xfer;
647 int rc;
648
649 xfer = libusb_alloc_transfer(0);
650 OSMO_ASSERT(xfer);
651 xfer->dev_handle = transp->usb_devh;
652 xfer->flags = 0;
653 xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
654 xfer->endpoint = transp->usb_ep.irq_in;
655 xfer->timeout = 0;
656 xfer->user_data = ci;
657 xfer->length = 64;
658
659 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
660 OSMO_ASSERT(xfer->buffer);
661 xfer->callback = usb_irq_xfer_cb;
662
663 /* submit the IN transfer */
664 rc = libusb_submit_transfer(xfer);
665 OSMO_ASSERT(rc == 0);
666}
667
668
Kévin Redone0b837c2018-10-10 00:39:25 +0200669static struct st_transport _transp;
670
671static struct st_slot _slot = {
672 .transp = &_transp,
673 .slot_nr = 0,
674};
675
676struct cardem_inst _ci = {
677 .slot = &_slot,
678};
679
680struct cardem_inst *ci = &_ci;
681
682static void signal_handler(int signal)
683{
684 switch (signal) {
685 case SIGINT:
686 cardem_request_card_insert(ci, false);
687 exit(0);
688 break;
689 default:
690 break;
691 }
692}
693
694/** remsim_client **/
695
Harald Welte3e9860b2019-12-02 23:04:54 +0100696static int bankd_handle_tpduCardToModem(struct bankd_client *bc, const RsproPDU_t *pdu)
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200697{
698 OSMO_ASSERT(pdu);
699 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
700
701 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
702 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
703 return -1;
704 }
705
706 // save SW to our current APDU context
707 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
708 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200709 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 +0200710 if (card2modem->data.size > 2) { // send PB and data to modem
711 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
712 }
713 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
714
715 return 0;
716}
717
Harald Welte3e9860b2019-12-02 23:04:54 +0100718static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu)
Harald Weltefa365592019-03-28 20:28:57 +0100719{
720 RsproPDU_t *resp;
721 int rc;
722
723 OSMO_ASSERT(pdu);
724 OSMO_ASSERT(RsproPDUchoice_PR_setAtrReq == pdu->msg.present);
725
726 /* FIXME: is this permitted at any time by the SIMtrace2 cardemfirmware? */
727 rc = cardem_request_set_atr(ci, pdu->msg.choice.setAtrReq.atr.buf,
728 pdu->msg.choice.setAtrReq.atr.size);
729 if (rc == 0)
730 resp = rspro_gen_SetAtrRes(ResultCode_ok);
731 else
732 resp = rspro_gen_SetAtrRes(ResultCode_cardTransmissionError);
733 if (!resp)
734 return -ENOMEM;
Harald Welte3e9860b2019-12-02 23:04:54 +0100735 server_conn_send_rspro(&g_client->bankd_conn, resp);
Harald Weltefa365592019-03-28 20:28:57 +0100736
737 return 0;
738}
739
Harald Welte3e9860b2019-12-02 23:04:54 +0100740/* handle incoming message from bankd */
741static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
Kévin Redone0b837c2018-10-10 00:39:25 +0200742{
Kévin Redone0b837c2018-10-10 00:39:25 +0200743 switch (pdu->msg.present) {
744 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +0100745 /* Store 'identity' of bankd to in peer_comp_id */
Harald Welte3e9860b2019-12-02 23:04:54 +0100746 rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
747 osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
Kévin Redone0b837c2018-10-10 00:39:25 +0200748 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200749 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
Harald Welte3e9860b2019-12-02 23:04:54 +0100750 bankd_handle_tpduCardToModem(g_client, pdu);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200751 break;
Harald Weltefa365592019-03-28 20:28:57 +0100752 case RsproPDUchoice_PR_setAtrReq:
Harald Welte3e9860b2019-12-02 23:04:54 +0100753 bankd_handle_setAtrReq(g_client, pdu);
Harald Weltefa365592019-03-28 20:28:57 +0100754 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200755 default:
Harald Welte3e9860b2019-12-02 23:04:54 +0100756 LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s\n",
757 rspro_msgt_name(pdu));
Kévin Redone0b837c2018-10-10 00:39:25 +0200758 return -1;
759 }
760
761 return 0;
762}
763
Harald Weltee56f2b92019-03-02 17:02:13 +0100764/* handle incoming messages from server */
765static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
766{
767 RsproPDU_t *resp;
768
769 switch (pdu->msg.present) {
770 case RsproPDUchoice_PR_connectClientRes:
771 /* Store 'identity' of server in srvc->peer_comp_id */
772 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
773 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
774 break;
Harald Welted571a3e2019-03-11 22:09:50 +0100775 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100776 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100777 if (!g_client->srv_conn.clslot)
778 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welted571a3e2019-03-11 22:09:50 +0100779 *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
Harald Welte3e9860b2019-12-02 23:04:54 +0100780 if (!g_client->bankd_conn.clslot)
781 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
782 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welted571a3e2019-03-11 22:09:50 +0100783 /* send response to server */
784 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
785 server_conn_send_rspro(srvc, resp);
786 break;
787 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100788 /* store/set the bankd ip/port as instructed by the server */
Harald Welte3e9860b2019-12-02 23:04:54 +0100789 osmo_talloc_replace_string(g_client, &g_client->bankd_conn.server_host,
Harald Welted571a3e2019-03-11 22:09:50 +0100790 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
Harald Welte9cf013a2019-03-11 22:19:19 +0100791 rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
Harald Welte3e9860b2019-12-02 23:04:54 +0100792 g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
Harald Weltee56f2b92019-03-02 17:02:13 +0100793 /* instruct bankd FSM to connect */
Harald Welte3e9860b2019-12-02 23:04:54 +0100794 osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
Harald Weltee56f2b92019-03-02 17:02:13 +0100795 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +0100796 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100797 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +0100798 break;
799 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +0100800 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n",
801 rspro_msgt_name(pdu));
Harald Weltee56f2b92019-03-02 17:02:13 +0100802 return -1;
803 }
804
805 return 0;
806}
807
Harald Weltece638d82019-03-17 09:36:04 +0100808static void handle_sig_usr1(int signal)
809{
810 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +0200811 talloc_report_full(g_tall_ctx, stderr);
Harald Weltece638d82019-03-17 09:36:04 +0100812}
Harald Weltee56f2b92019-03-02 17:02:13 +0100813
Kévin Redon3ec265b2018-10-11 17:30:33 +0200814static void print_welcome(void)
815{
816 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
Harald Welte32e2e002019-12-15 23:01:54 +0100817 "(C) 2010-2019, Harald Welte <laforge@gnumonks.org>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200818 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
819}
820
821static void print_help(void)
822{
Harald Weltee56f2b92019-03-02 17:02:13 +0100823 printf( "\t-s\t--server-host HOST\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200824 "\t-p\t--server-port PORT\n"
Harald Welte72cde102019-03-30 10:43:06 +0100825 "\t-c\t--client-id <0-65535>\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200826 "\t-n\t--client-slot <0-65535>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200827 "\t-h\t--help\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100828 "\t-v\t--version\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200829 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
830 "\t-k\t--keep-running\n"
831 "\t-V\t--usb-vendor\tVENDOR_ID\n"
832 "\t-P\t--usb-product\tPRODUCT_ID\n"
833 "\t-C\t--usb-config\tCONFIG_ID\n"
834 "\t-I\t--usb-interface\tINTERFACE_ID\n"
835 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
836 "\t-A\t--usb-address\tADDRESS\n"
837 "\t-H\t--usb-path\tPATH\n"
Kévin Redon206c3d72018-11-12 22:51:37 +0100838 "\t-a\t--atr\tATR\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200839 "\n"
840 );
841}
842
Harald Welte9636d2b2019-12-16 16:26:50 +0100843#define ATR_SIZE_MAX 55
844struct client_config {
845 char *server_host;
846 int server_port;
847
848 int client_id;
849 int client_slot;
850
851 char *gsmtap_host;
852 bool keep_running;
853
854 struct {
855 uint8_t data[ATR_SIZE_MAX];
856 uint8_t len;
857 } atr;
858
859 struct {
860 int vendor_id;
861 int product_id;
862 int config_id;
863 int if_num;
864 int altsetting;
865 int addr;
866 char *path;
867 } usb;
Kévin Redon3ec265b2018-10-11 17:30:33 +0200868};
869
Harald Welte9636d2b2019-12-16 16:26:50 +0100870static struct client_config *client_config_init(void *ctx)
Kévin Redone0b837c2018-10-10 00:39:25 +0200871{
Harald Welte9636d2b2019-12-16 16:26:50 +0100872 struct client_config *cfg = talloc_zero(ctx, struct client_config);
873 if (!cfg)
874 return NULL;
Kévin Redone0b837c2018-10-10 00:39:25 +0200875
Harald Welte9636d2b2019-12-16 16:26:50 +0100876 cfg->server_host = talloc_strdup(cfg, "127.0.0.1");
877 cfg->server_port = 9998;
878 cfg->client_id = -1;
879 cfg->client_slot = -1;
880 cfg->gsmtap_host = talloc_strdup(cfg, "127.0.0.1");
881 cfg->keep_running = false;
882
883 cfg->usb.vendor_id = -1;
884 cfg->usb.product_id = -1;
885 cfg->usb.config_id = -1;
886 cfg->usb.if_num = -1;
887 cfg->usb.altsetting = 0;
888 cfg->usb.addr = -1;
889 cfg->usb.path = NULL;
890
891 cfg->atr.data[0] = 0x3B;
892 cfg->atr.data[1] = 0x00; // the shortest simplest ATR possible
893 cfg->atr.len = 2;
894
895 return cfg;
896};
897
898static void handle_options(struct client_config *cfg, int argc, char **argv)
899{
900 const struct option opts[] = {
901 { "server-host", 1, 0, 's' },
902 { "server-port", 1, 0, 'p' },
903 { "client-id", 1, 0, 'c' },
904 { "client-slot", 1, 0, 'n' },
905 { "help", 0, 0, 'h' },
906 { "version", 0, 0, 'v' },
907 { "gsmtap-ip", 1, 0, 'i' },
908 { "keep-running", 0, 0, 'k' },
909 { "usb-vendor", 1, 0, 'V' },
910 { "usb-product", 1, 0, 'P' },
911 { "usb-config", 1, 0, 'C' },
912 { "usb-interface", 1, 0, 'I' },
913 { "usb-altsetting", 1, 0, 'S' },
914 { "usb-address", 1, 0, 'A' },
915 { "usb-path", 1, 0, 'H' },
916 { "atr", 1, 0, 'a' },
917 { NULL, 0, 0, 0 }
918 };
919 int c, rc;
Kévin Redone0b837c2018-10-10 00:39:25 +0200920
921 while (1) {
922 int option_index = 0;
923
Harald Weltecd7fcd72019-12-03 21:29:07 +0100924 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 +0200925 if (c == -1)
926 break;
927 switch (c) {
Harald Weltee56f2b92019-03-02 17:02:13 +0100928 case 's':
Harald Welte9636d2b2019-12-16 16:26:50 +0100929 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200930 break;
931 case 'p':
Harald Welte9636d2b2019-12-16 16:26:50 +0100932 cfg->server_port = atoi(optarg);
Kévin Redonfbca97a2018-10-11 19:13:24 +0200933 break;
Harald Welte72cde102019-03-30 10:43:06 +0100934 case 'c':
Harald Welte9636d2b2019-12-16 16:26:50 +0100935 cfg->client_id = atoi(optarg);
Harald Welte72cde102019-03-30 10:43:06 +0100936 break;
937 case 'n':
Harald Welte9636d2b2019-12-16 16:26:50 +0100938 cfg->client_slot = atoi(optarg);
Harald Welte72cde102019-03-30 10:43:06 +0100939 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200940 case 'h':
941 print_help();
942 exit(0);
943 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100944 case 'v':
945 printf("osmo-remsim-client version %s\n", VERSION);
946 exit(0);
947 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200948 case 'i':
Harald Welte9636d2b2019-12-16 16:26:50 +0100949 osmo_talloc_replace_string(cfg, &cfg->gsmtap_host, optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200950 break;
951 case 'k':
Harald Welte9636d2b2019-12-16 16:26:50 +0100952 cfg->keep_running = 1;
Kévin Redone0b837c2018-10-10 00:39:25 +0200953 break;
954 case 'V':
Harald Welte9636d2b2019-12-16 16:26:50 +0100955 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
Kévin Redone0b837c2018-10-10 00:39:25 +0200956 break;
957 case 'P':
Harald Welte9636d2b2019-12-16 16:26:50 +0100958 cfg->usb.product_id = strtol(optarg, NULL, 16);
Kévin Redone0b837c2018-10-10 00:39:25 +0200959 break;
960 case 'C':
Harald Welte9636d2b2019-12-16 16:26:50 +0100961 cfg->usb.config_id = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200962 break;
963 case 'I':
Harald Welte9636d2b2019-12-16 16:26:50 +0100964 cfg->usb.if_num = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200965 break;
966 case 'S':
Harald Welte9636d2b2019-12-16 16:26:50 +0100967 cfg->usb.altsetting = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200968 break;
969 case 'A':
Harald Welte9636d2b2019-12-16 16:26:50 +0100970 cfg->usb.addr = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200971 break;
972 case 'H':
Harald Welte9636d2b2019-12-16 16:26:50 +0100973 cfg->usb.path = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200974 break;
Kévin Redon206c3d72018-11-12 22:51:37 +0100975 case 'a':
Harald Welte9636d2b2019-12-16 16:26:50 +0100976 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
977 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
978 fprintf(stderr, "ATR malformed\n");
979 exit(2);
Kévin Redon206c3d72018-11-12 22:51:37 +0100980 }
Harald Welte9636d2b2019-12-16 16:26:50 +0100981 cfg->atr.len = rc;
Kévin Redon206c3d72018-11-12 22:51:37 +0100982 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200983 }
984 }
985
Harald Welte9636d2b2019-12-16 16:26:50 +0100986 if (argc > optind) {
987 fprintf(stderr, "Unsupported positional arguments on command line\n");
988 exit(2);
Kévin Redone0b837c2018-10-10 00:39:25 +0200989 }
Harald Welte9636d2b2019-12-16 16:26:50 +0100990}
Kévin Redone0b837c2018-10-10 00:39:25 +0200991
Harald Welte9636d2b2019-12-16 16:26:50 +0100992
993int main(int argc, char **argv)
994{
995 struct rspro_server_conn *srvc, *bankdc;
996 struct st_transport *transp = ci->slot->transp;
997 struct client_config *cfg;
998 int rc;
999 int ret = 1;
1000
1001 print_welcome();
Harald Weltece638d82019-03-17 09:36:04 +01001002
Harald Welte972a1e82019-03-30 08:34:30 +01001003 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +02001004 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Weltef7442b52019-07-18 18:54:05 +02001005 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Welte972a1e82019-03-30 08:34:30 +01001006 osmo_init_logging2(g_tall_ctx, &log_info);
1007
Harald Welte9636d2b2019-12-16 16:26:50 +01001008 cfg = client_config_init(g_tall_ctx);
1009 handle_options(cfg, argc, argv);
1010
1011 if (cfg->usb.vendor_id < 0 || cfg->usb.product_id < 0) {
1012 fprintf(stderr, "You have to specify the vendor and product ID\n");
1013 goto do_exit;
1014 }
1015
1016 signal(SIGUSR1, handle_sig_usr1);
1017
Harald Welte32e2e002019-12-15 23:01:54 +01001018 rc = osmo_libusb_init(NULL);
Kévin Redon193a8c12018-10-11 17:24:39 +02001019 if (rc < 0) {
1020 fprintf(stderr, "libusb initialization failed\n");
1021 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +02001022 }
1023
Harald Welte9636d2b2019-12-16 16:26:50 +01001024 g_gti = gsmtap_source_init(cfg->gsmtap_host, GSMTAP_UDP_PORT, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +02001025 if (!g_gti) {
1026 perror("unable to open GSMTAP");
1027 goto close_exit;
1028 }
1029 gsmtap_source_add_sink(g_gti);
1030
1031 signal(SIGINT, &signal_handler);
1032
1033 // initialize remote SIM client
Kévin Redone0b837c2018-10-10 00:39:25 +02001034
Kévin Redone0b837c2018-10-10 00:39:25 +02001035 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +01001036
Harald Welte9636d2b2019-12-16 16:26:50 +01001037 if (cfg->client_id != -1) {
Harald Welte72cde102019-03-30 10:43:06 +01001038 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welte9636d2b2019-12-16 16:26:50 +01001039 g_client->srv_conn.clslot->clientId = cfg->client_id;
1040 /* default to client slot 0 */
1041 if (cfg->client_slot == -1)
1042 g_client->srv_conn.clslot->slotNr = 0;
1043 else
1044 g_client->srv_conn.clslot->slotNr = cfg->client_slot;
Harald Welte3e9860b2019-12-02 23:04:54 +01001045 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
1046 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welte72cde102019-03-30 10:43:06 +01001047 }
1048
Harald Welte9636d2b2019-12-16 16:26:50 +01001049 /* create and [attempt to] establish connection to remsim-server */
Harald Weltee56f2b92019-03-02 17:02:13 +01001050 srvc = &g_client->srv_conn;
Harald Welte9636d2b2019-12-16 16:26:50 +01001051 srvc->server_host = cfg->server_host;
1052 srvc->server_port = cfg->server_port;
Harald Weltee56f2b92019-03-02 17:02:13 +01001053 srvc->handle_rx = srvc_handle_rx;
1054 srvc->own_comp_id.type = ComponentType_remsimClient;
1055 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
1056 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
1057 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
1058 rc = server_conn_fsm_alloc(g_client, srvc);
1059 if (rc < 0) {
1060 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
1061 exit(1);
1062 }
Harald Welted2192e22019-11-07 18:10:57 +01001063 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001064
Harald Welte9636d2b2019-12-16 16:26:50 +01001065 /* create and not yet establish connection to remsim-bankd */
1066 srvc = &g_client->srv_conn;
Harald Welte3e9860b2019-12-02 23:04:54 +01001067 bankdc = &g_client->bankd_conn;
1068 /* server_host / server_port are configured from remsim-server */
1069 bankdc->handle_rx = bankd_handle_rx;
1070 memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id));
1071 rc = server_conn_fsm_alloc(g_client, bankdc);
1072 if (rc < 0) {
1073 fprintf(stderr, "Unable to create bankd conn FSM: %s\n", strerror(errno));
Kévin Redone0b837c2018-10-10 00:39:25 +02001074 exit(1);
1075 }
Harald Welte82194452019-12-14 17:18:34 +01001076 osmo_fsm_inst_update_id(bankdc->fi, "bankd");
Kévin Redone0b837c2018-10-10 00:39:25 +02001077
Harald Welte3e9860b2019-12-02 23:04:54 +01001078 asn_debug = 0;
1079
Kévin Redone0b837c2018-10-10 00:39:25 +02001080 // connect to SIMtrace2 cardem
1081 do {
1082 struct usb_interface_match _ifm, *ifm = &_ifm;
Harald Welte9636d2b2019-12-16 16:26:50 +01001083 ifm->vendor = cfg->usb.vendor_id;
1084 ifm->product = cfg->usb.product_id;
1085 ifm->configuration = cfg->usb.config_id;
1086 ifm->interface = cfg->usb.if_num;
1087 ifm->altsetting = cfg->usb.altsetting;
1088 ifm->addr = cfg->usb.addr;
1089 if (cfg->usb.path)
1090 osmo_strlcpy(ifm->path, cfg->usb.path, sizeof(ifm->path));
Harald Weltefd5dafc2019-12-15 21:14:14 +01001091 transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
Kévin Redone0b837c2018-10-10 00:39:25 +02001092 if (!transp->usb_devh) {
1093 fprintf(stderr, "can't open USB device\n");
1094 goto close_exit;
1095 }
1096
Harald Welte9636d2b2019-12-16 16:26:50 +01001097 rc = libusb_claim_interface(transp->usb_devh, cfg->usb.if_num);
Kévin Redone0b837c2018-10-10 00:39:25 +02001098 if (rc < 0) {
Harald Welte9636d2b2019-12-16 16:26:50 +01001099 fprintf(stderr, "can't claim interface %d; rc=%d\n", cfg->usb.if_num, rc);
Kévin Redone0b837c2018-10-10 00:39:25 +02001100 goto close_exit;
1101 }
1102
Harald Welte9636d2b2019-12-16 16:26:50 +01001103 rc = osmo_libusb_get_ep_addrs(transp->usb_devh, cfg->usb.if_num, &transp->usb_ep.out,
Harald Weltefd5dafc2019-12-15 21:14:14 +01001104 &transp->usb_ep.in, &transp->usb_ep.irq_in);
Kévin Redone0b837c2018-10-10 00:39:25 +02001105 if (rc < 0) {
1106 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
1107 goto close_exit;
1108 }
1109
Kévin Redon3428e412018-10-11 19:14:00 +02001110 // switch modem SIM port to emulated SIM on OWHW
1111 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
1112 int modem = -1;
1113 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
1114 case 0:
1115 modem = 1;
1116 break;
1117 case 1:
1118 modem = 2;
1119 break;
1120 default:
1121 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
1122 goto close_exit;
1123 }
1124 //
1125 char gpio_path[PATH_MAX];
1126 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
1127 int connec_st_usim = open(gpio_path, O_WRONLY);
1128 if (-1 == connec_st_usim) {
1129 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1130 goto close_exit;
1131 }
1132 if (1 != write(connec_st_usim, "1", 1)) {
1133 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1134 goto close_exit;
1135 }
1136 printf("switched modem %d to emulated USIM\n", modem);
1137
1138 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
1139 int mdm_rst = open(gpio_path, O_WRONLY);
1140 if (-1 == mdm_rst) {
1141 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
1142 goto close_exit;
1143 }
1144 if (1 != write(mdm_rst, "1", 1)) {
1145 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1146 goto close_exit;
1147 }
1148 sleep(1); // wait a bit to ensure reset is effective
1149 if (1 != write(mdm_rst, "0", 1)) {
1150 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1151 goto close_exit;
1152 }
1153 printf("modem %d reset\n", modem);
1154 }
1155
Harald Welte0e0e9322019-12-16 12:47:18 +01001156 /* request firmware to generate STATUS on IRQ endpoint */
1157 cardem_request_config(ci, CEMU_FEAT_F_STATUS_IRQ);
1158
Kévin Redone0b837c2018-10-10 00:39:25 +02001159 /* simulate card-insert to modem (owhw, not qmod) */
1160 cardem_request_card_insert(ci, true);
1161
1162 /* select remote (forwarded) SIM */
1163 st_modem_sim_select_remote(ci->slot);
1164
1165 /* set the ATR */
Kévin Redon206c3d72018-11-12 22:51:37 +01001166 //atr_update_csum(real_atr, sizeof(real_atr));
Harald Welte9636d2b2019-12-16 16:26:50 +01001167 cardem_request_set_atr(ci, cfg->atr.data, cfg->atr.len);
Kévin Redone0b837c2018-10-10 00:39:25 +02001168
1169 /* select remote (forwarded) SIM */
1170 st_modem_reset_pulse(ci->slot, 300);
1171
Harald Welte32e2e002019-12-15 23:01:54 +01001172 printf("Entering main loop\n");
1173
1174 allocate_and_submit_irq(ci);
1175 allocate_and_submit_in(ci);
1176
1177 while (1) {
1178 osmo_select_main(false);
1179 }
1180
Kévin Redone0b837c2018-10-10 00:39:25 +02001181 ret = 0;
1182
Kévin Redon193a8c12018-10-11 17:24:39 +02001183 libusb_release_interface(transp->usb_devh, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +02001184close_exit:
1185 if (transp->usb_devh)
1186 libusb_close(transp->usb_devh);
Harald Welte9636d2b2019-12-16 16:26:50 +01001187 if (cfg->keep_running)
Kévin Redone0b837c2018-10-10 00:39:25 +02001188 sleep(1);
Harald Welte9636d2b2019-12-16 16:26:50 +01001189 } while (cfg->keep_running);
Kévin Redone0b837c2018-10-10 00:39:25 +02001190
Kévin Redon193a8c12018-10-11 17:24:39 +02001191 libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001192do_exit:
1193 return ret;
1194}