blob: 0e71984ae704ab723b8e0b645c346a7cadffb998 [file] [log] [blame]
Kévin Redone0b837c2018-10-10 00:39:25 +02001
2#include <errno.h>
3#include <string.h>
4
5#include <talloc.h>
6
7#include <osmocom/core/msgb.h>
8#include <osmocom/core/fsm.h>
9#include <osmocom/core/utils.h>
10#include <osmocom/core/logging.h>
11#include <osmocom/core/application.h>
12
13#include <osmocom/abis/ipa.h>
14#include <osmocom/gsm/protocol/ipaccess.h>
15
16#include "rspro_util.h"
17#include "client.h"
18
19#include <unistd.h>
20#include <stdio.h>
21#include <signal.h>
22#include <getopt.h>
23
24#include <libusb.h>
25
26#include "simtrace2/libusb_util.h"
27#include "simtrace2/simtrace_prot.h"
28#include "simtrace2/apdu_dispatch.h"
29#include "simtrace2/simtrace2-discovery.h"
30
31#include <osmocom/core/gsmtap.h>
32#include <osmocom/core/gsmtap_util.h>
33#include <osmocom/core/utils.h>
34#include <osmocom/core/socket.h>
35#include <osmocom/core/msgb.h>
36#include <osmocom/sim/class_tables.h>
37#include <osmocom/sim/sim.h>
38
39/* transport to a SIMtrace device */
40struct st_transport {
41 /* USB */
42 struct libusb_device_handle *usb_devh;
43 struct {
44 uint8_t in;
45 uint8_t out;
46 uint8_t irq_in;
47 } usb_ep;
Kévin Redone0b837c2018-10-10 00:39:25 +020048};
49
50/* a SIMtrace slot; communicates over a transport */
51struct st_slot {
52 /* transport through which the slot can be reached */
53 struct st_transport *transp;
54 /* number of the slot within the transport */
55 uint8_t slot_nr;
56};
57
58/* One istance of card emulation */
59struct cardem_inst {
60 /* slot on which this card emulation instance runs */
61 struct st_slot *slot;
62};
63
64/* global GSMTAP instance */
65static struct gsmtap_inst *g_gti;
66
67static struct bankd_client *g_client;
68static void *g_tall_ctx;
69void __thread *talloc_asn1_ctx;
70int asn_debug;
71
72static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
73{
74 struct gsmtap_hdr *gh;
75 unsigned int gross_len = len + sizeof(*gh);
76 uint8_t *buf = malloc(gross_len);
77 int rc;
78
79 if (!buf)
80 return -ENOMEM;
81
82 memset(buf, 0, sizeof(*gh));
83 gh = (struct gsmtap_hdr *) buf;
84 gh->version = GSMTAP_VERSION;
85 gh->hdr_len = sizeof(*gh)/4;
86 gh->type = GSMTAP_TYPE_SIM;
87
88 memcpy(buf + sizeof(*gh), apdu, len);
89
90 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
91 if (rc < 0) {
92 perror("write gsmtap");
93 free(buf);
94 return rc;
95 }
96
97 free(buf);
98 return 0;
99}
100
101/***********************************************************************
102 * SIMTRACE pcore protocol
103 ***********************************************************************/
104
105/*! \brief allocate a message buffer for simtrace use */
106static struct msgb *st_msgb_alloc(void)
107{
108 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
109}
110
111#if 0
112static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
113{
114 printf("APDU: %s\n", osmo_hexdump(buf, len));
115 gsmtap_send_sim(buf, len);
116}
117#endif
118
119/*! \brief Transmit a given command to the SIMtrace2 device */
120int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
121{
122 int rc;
123
Kévin Redon21e31de2018-10-11 17:25:58 +0200124 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redone0b837c2018-10-10 00:39:25 +0200125
Kévin Redon193a8c12018-10-11 17:24:39 +0200126 int xfer_len;
Kévin Redone0b837c2018-10-10 00:39:25 +0200127
Kévin Redon193a8c12018-10-11 17:24:39 +0200128 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
129 msgb_data(msg), msgb_length(msg),
130 &xfer_len, 100000);
Kévin Redone0b837c2018-10-10 00:39:25 +0200131
132 msgb_free(msg);
133 return rc;
134}
135
136static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
137 uint8_t slot_nr)
138{
139 struct simtrace_msg_hdr *sh;
140
141 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
142 memset(sh, 0, sizeof(*sh));
143 sh->msg_class = msg_class;
144 sh->msg_type = msg_type;
145 sh->slot_nr = slot_nr;
146 sh->msg_len = msgb_length(msg);
147
148 return sh;
149}
150
151/* transmit a given message to a specified slot. Expects all headers
152 * present before calling the function */
153int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
154 uint8_t msg_class, uint8_t msg_type)
155{
156 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
157
158 return st_transp_tx_msg(slot->transp, msg);
159}
160
161/***********************************************************************
162 * Card Emulation protocol
163 ***********************************************************************/
164
165
166/*! \brief Request the SIMtrace2 to generate a card-insert signal */
167static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
168{
169 struct msgb *msg = st_msgb_alloc();
170 struct cardemu_usb_msg_cardinsert *cins;
171
172 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
173 memset(cins, 0, sizeof(*cins));
174 if (inserted)
175 cins->card_insert = 1;
176
177 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
178}
179
180/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
181static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
182{
183 struct msgb *msg = st_msgb_alloc();
184 struct cardemu_usb_msg_tx_data *txd;
185 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
186
Kévin Redon21e31de2018-10-11 17:25:58 +0200187 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redone0b837c2018-10-10 00:39:25 +0200188
189 memset(txd, 0, sizeof(*txd));
190 txd->data_len = 1;
191 txd->flags = CEMU_DATA_F_PB_AND_RX;
192 /* one data byte */
193 msgb_put_u8(msg, pb);
194
195 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
196}
197
198/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
199static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
200 const uint8_t *data, uint8_t data_len_in)
201{
202 struct msgb *msg = st_msgb_alloc();
203 struct cardemu_usb_msg_tx_data *txd;
204 uint8_t *cur;
205
206 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
207
Kévin Redon21e31de2018-10-11 17:25:58 +0200208 printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb,
Kévin Redone0b837c2018-10-10 00:39:25 +0200209 osmo_hexdump(data, data_len_in), data_len_in);
210
211 memset(txd, 0, sizeof(*txd));
212 txd->data_len = 1 + data_len_in;
213 txd->flags = CEMU_DATA_F_PB_AND_TX;
214 /* procedure byte */
215 msgb_put_u8(msg, pb);
216 /* data */
217 cur = msgb_put(msg, data_len_in);
218 memcpy(cur, data, data_len_in);
219
220 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
221}
222
223/*! \brief Request the SIMtrace2 to send a Status Word */
224static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
225{
226 struct msgb *msg = st_msgb_alloc();
227 struct cardemu_usb_msg_tx_data *txd;
228 uint8_t *cur;
229
230 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
231
Kévin Redon21e31de2018-10-11 17:25:58 +0200232 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redone0b837c2018-10-10 00:39:25 +0200233
234 memset(txd, 0, sizeof(*txd));
235 txd->data_len = 2;
236 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
237 cur = msgb_put(msg, 2);
238 cur[0] = sw[0];
239 cur[1] = sw[1];
240
241 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
242}
243
244static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
245{
246 uint8_t csum = 0;
247 int i;
248
249 for (i = 1; i < atr_len - 1; i++)
250 csum = csum ^ atr[i];
251
252 atr[atr_len-1] = csum;
253}
254
255static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
256{
257 struct msgb *msg = st_msgb_alloc();
258 struct cardemu_usb_msg_set_atr *satr;
259 uint8_t *cur;
260
261 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
262
Kévin Redon21e31de2018-10-11 17:25:58 +0200263 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200264
265 memset(satr, 0, sizeof(*satr));
266 satr->atr_len = atr_len;
267 cur = msgb_put(msg, atr_len);
268 memcpy(cur, atr, atr_len);
269
270 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
271}
272
273/***********************************************************************
274 * Modem Control protocol
275 ***********************************************************************/
276
277static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
278{
279 struct msgb *msg = st_msgb_alloc();
280 struct st_modem_reset *sr ;
281
282 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
283 sr->asserted = asserted;
284 sr->pulse_duration_msec = pulse_ms;
285
286 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
287}
288
289/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
290int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
291{
292 return _modem_reset(slot, 2, duration_ms);
293}
294
295/*! \brief assert the RESET line of the modem */
296int st_modem_reset_active(struct st_slot *slot)
297{
298 return _modem_reset(slot, 1, 0);
299}
300
301/*! \brief de-assert the RESET line of the modem */
302int st_modem_reset_inactive(struct st_slot *slot)
303{
304 return _modem_reset(slot, 0, 0);
305}
306
307static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
308{
309 struct msgb *msg = st_msgb_alloc();
310 struct st_modem_sim_select *ss;
311
312 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
313 ss->remote_sim = remote_sim;
314
315 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
316}
317
318/*! \brief select local (physical) SIM for given slot */
319int st_modem_sim_select_local(struct st_slot *slot)
320{
321 return _modem_sim_select(slot, 0);
322}
323
324/*! \brief select remote (emulated/forwarded) SIM for given slot */
325int st_modem_sim_select_remote(struct st_slot *slot)
326{
327 return _modem_sim_select(slot, 1);
328}
329
330/*! \brief Request slot to send us status information about the modem */
331int st_modem_get_status(struct st_slot *slot)
332{
333 struct msgb *msg = st_msgb_alloc();
334
335 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
336}
337
338
339/***********************************************************************
340 * Incoming Messages
341 ***********************************************************************/
342
343/*! \brief Process a STATUS message from the SIMtrace2 */
344static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
345{
346 struct cardemu_usb_msg_status *status;
347 status = (struct cardemu_usb_msg_status *) buf;
348
Kévin Redon21e31de2018-10-11 17:25:58 +0200349 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200350 status->flags, status->fi, status->di, status->wi,
351 status->waiting_time);
352
353 return 0;
354}
355
356/*! \brief Process a PTS indication message from the SIMtrace2 */
357static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
358{
359 struct cardemu_usb_msg_pts_info *pts;
360 pts = (struct cardemu_usb_msg_pts_info *) buf;
361
Kévin Redon21e31de2018-10-11 17:25:58 +0200362 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redone0b837c2018-10-10 00:39:25 +0200363
364 return 0;
365}
366
367/*! \brief Process a ERROR indication message from the SIMtrace2 */
368static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
369{
370 struct cardemu_usb_msg_error *err;
371 err = (struct cardemu_usb_msg_error *) buf;
372
Kévin Redon21e31de2018-10-11 17:25:58 +0200373 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200374 err->severity, err->subsystem, err->code,
375 err->msg_len ? (char *)err->msg : "");
376
377 return 0;
378}
379
Kévin Redonbc08db52018-10-11 08:41:00 +0200380static struct apdu_context ac; // this will hold the complete APDU (across calls)
381
Kévin Redone0b837c2018-10-10 00:39:25 +0200382/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
383static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
384{
Kévin Redonbc08db52018-10-11 08:41:00 +0200385 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 +0200386 int rc;
387
Kévin Redon21e31de2018-10-11 17:25:58 +0200388 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redone0b837c2018-10-10 00:39:25 +0200389 osmo_hexdump(data->data, data->data_len));
390
391 rc = apdu_segment_in(&ac, data->data, data->data_len,
Kévin Redonbc08db52018-10-11 08:41:00 +0200392 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200393
Kévin Redonbc08db52018-10-11 08:41:00 +0200394 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
395 uint8_t* apdu_command = calloc(1, sizeof(ac.hdr) + ac.lc.tot); // to store the APDU command to send
396 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redone0b837c2018-10-10 00:39:25 +0200397 if (ac.lc.tot) {
Kévin Redonbc08db52018-10-11 08:41:00 +0200398 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redone0b837c2018-10-10 00:39:25 +0200399 }
Kévin Redonbc08db52018-10-11 08:41:00 +0200400 // send APDU to card
401 RsproPDU_t *pdu = rspro_gen_TpduModem2Card(g_client->clslot, &(BankSlot_t){ .bankId = 0, .slotNr = 0}, apdu_command, sizeof(ac.hdr) + ac.lc.tot); // create RSPRO packet
402 ipa_client_conn_send_rspro(g_client->bankd_conn, pdu); // send RSPRO packet
403 // the response will come separately
404 free(apdu_command);
405 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
406 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 +0200407 }
408 return 0;
409}
410
411#if 0
412 case SIMTRACE_CMD_DO_ERROR
413 rc = process_do_error(ci, buf, len);
414 break;
415#endif
416
417/*! \brief Process an incoming message from the SIMtrace2 */
418static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
419{
420 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
421 int rc;
422
Kévin Redon21e31de2018-10-11 17:25:58 +0200423 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200424
425 buf += sizeof(*sh);
426
427 switch (sh->msg_type) {
428 case SIMTRACE_MSGT_BD_CEMU_STATUS:
429 rc = process_do_status(ci, buf, len);
430 break;
431 case SIMTRACE_MSGT_DO_CEMU_PTS:
432 rc = process_do_pts(ci, buf, len);
433 break;
434 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
435 rc = process_do_rx_da(ci, buf, len);
436 break;
437 default:
438 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
439 rc = -1;
440 break;
441 }
442
443 return rc;
444}
445
446static void print_welcome(void)
447{
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200448 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
Kévin Redone0b837c2018-10-10 00:39:25 +0200449 "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
450 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
451}
452
453static void print_help(void)
454{
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200455 printf( "\t-d\t--bankd-host HOST\n"
456 "\t-p\t--bankd-port PORT\n"
Kévin Redone0b837c2018-10-10 00:39:25 +0200457 "\t-h\t--help\n"
458 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
459 "\t-k\t--keep-running\n"
460 "\t-V\t--usb-vendor\tVENDOR_ID\n"
461 "\t-P\t--usb-product\tPRODUCT_ID\n"
462 "\t-C\t--usb-config\tCONFIG_ID\n"
463 "\t-I\t--usb-interface\tINTERFACE_ID\n"
464 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
465 "\t-A\t--usb-address\tADDRESS\n"
466 "\t-H\t--usb-path\tPATH\n"
467 "\n"
468 );
469}
470
471static const struct option opts[] = {
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200472 { "bankd-host", 1, 0, 'b' },
473 { "bankd-port", 1, 0, 'p' },
Kévin Redone0b837c2018-10-10 00:39:25 +0200474 { "gsmtap-ip", 1, 0, 'i' },
475 { "help", 0, 0, 'h' },
476 { "keep-running", 0, 0, 'k' },
477 { "usb-vendor", 1, 0, 'V' },
478 { "usb-product", 1, 0, 'P' },
479 { "usb-config", 1, 0, 'C' },
480 { "usb-interface", 1, 0, 'I' },
481 { "usb-altsetting", 1, 0, 'S' },
482 { "usb-address", 1, 0, 'A' },
483 { "usb-path", 1, 0, 'H' },
484 { NULL, 0, 0, 0 }
485};
486
487static void run_mainloop(struct cardem_inst *ci)
488{
489 struct st_transport *transp = ci->slot->transp;
490 unsigned int msg_count, byte_count = 0;
491 uint8_t buf[16*265];
492 int xfer_len;
493 int rc;
494
495 printf("Entering main loop\n");
496
497 while (1) {
498 /* read data from SIMtrace2 device (local or via USB) */
499 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
500 buf, sizeof(buf), &xfer_len, 100);
501 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
502 rc != LIBUSB_ERROR_INTERRUPTED &&
503 rc != LIBUSB_ERROR_IO) {
504 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
505 return;
506 }
507 /* dispatch any incoming data */
508 if (xfer_len > 0) {
509 printf("URB: %s\n", osmo_hexdump(buf, xfer_len));
510 process_usb_msg(ci, buf, xfer_len);
511 msg_count++;
512 byte_count += xfer_len;
513 }
514 // handle remote SIM client fsm
515 // TODO register the USB fd for this select
516 osmo_select_main(true);
517 }
518}
519
520static struct st_transport _transp;
521
522static struct st_slot _slot = {
523 .transp = &_transp,
524 .slot_nr = 0,
525};
526
527struct cardem_inst _ci = {
528 .slot = &_slot,
529};
530
531struct cardem_inst *ci = &_ci;
532
533static void signal_handler(int signal)
534{
535 switch (signal) {
536 case SIGINT:
537 cardem_request_card_insert(ci, false);
538 exit(0);
539 break;
540 default:
541 break;
542 }
543}
544
545/** remsim_client **/
546
547static void push_and_send(struct ipa_client_conn *ipa, struct msgb *msg_tx)
548{
549 ipa_prepend_header_ext(msg_tx, IPAC_PROTO_EXT_RSPRO);
550 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
551 ipa_client_conn_send(ipa, msg_tx);
552 /* msg_tx is now queued and will be freed. */
553}
554
555void ipa_client_conn_send_rspro(struct ipa_client_conn *ipa, RsproPDU_t *rspro)
556{
557 struct msgb *msg = rspro_enc_msg(rspro);
558 OSMO_ASSERT(msg);
559 push_and_send(ipa, msg);
560}
561
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200562static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu)
563{
564 OSMO_ASSERT(pdu);
565 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
566
567 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
568 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
569 return -1;
570 }
571
572 // save SW to our current APDU context
573 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
574 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200575 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 +0200576 if (card2modem->data.size > 2) { // send PB and data to modem
577 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
578 }
579 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
580
581 return 0;
582}
583
Kévin Redone0b837c2018-10-10 00:39:25 +0200584static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
585{
586 RsproPDU_t *pdu = rspro_dec_msg(msg);
587 if (!pdu) {
588 fprintf(stderr, "Error decoding PDU\n");
589 return -1;
590 }
591
592 switch (pdu->msg.present) {
593 case RsproPDUchoice_PR_connectClientRes:
594 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
595 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200596 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
597 bankd_handle_tpduCardToModem(bc, pdu);
598 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200599 default:
600 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
601 return -1;
602 }
603
604 return 0;
605}
606
607int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
608{
609 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
610 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
611 struct bankd_client *bc = conn->data;
612 int rc;
613
614 if (msgb_length(msg) < sizeof(*hh))
615 goto invalid;
616 msg->l2h = &hh->data[0];
617 if (hh->proto != IPAC_PROTO_OSMO)
618 goto invalid;
619 if (!he || msgb_l2len(msg) < sizeof(*he))
620 goto invalid;
621 msg->l2h = &he->data[0];
622
623 if (he->proto != IPAC_PROTO_EXT_RSPRO)
624 goto invalid;
625
626 printf("Received RSPRO %s\n", msgb_hexdump(msg));
627
628 rc = bankd_handle_msg(bc, msg);
629
630 return rc;
631
632invalid:
633 msgb_free(msg);
634 return -1;
635}
636
637static const struct log_info_cat default_categories[] = {
638 [DMAIN] = {
639 .name = "DMAIN",
640 .loglevel = LOGL_DEBUG,
641 .enabled = 1,
642 },
643};
644
645static const struct log_info log_info = {
646 .cat = default_categories,
647 .num_cat = ARRAY_SIZE(default_categories),
648};
649
650int main(int argc, char **argv)
651{
652 struct st_transport *transp = ci->slot->transp;
653 char *gsmtap_host = "127.0.0.1";
654 int rc;
655 int c, ret = 1;
656 int keep_running = 0;
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200657 int bankd_port = 9999;
Kévin Redone0b837c2018-10-10 00:39:25 +0200658 int if_num = 0, vendor_id = -1, product_id = -1;
659 int config_id = -1, altsetting = 0, addr = -1;
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200660 char *bankd_host = "127.0.0.1";
Kévin Redone0b837c2018-10-10 00:39:25 +0200661 char *path = NULL;
662
663 print_welcome();
664
665 while (1) {
666 int option_index = 0;
667
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200668 c = getopt_long(argc, argv, "b:p:hi:V:P:C:I:S:A:H:k", opts, &option_index);
Kévin Redone0b837c2018-10-10 00:39:25 +0200669 if (c == -1)
670 break;
671 switch (c) {
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200672 case 'b':
673 bankd_host = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200674 break;
675 case 'p':
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200676 bankd_port = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200677 break;
678 case 'h':
679 print_help();
680 exit(0);
681 break;
682 case 'i':
683 gsmtap_host = optarg;
684 break;
685 case 'k':
686 keep_running = 1;
687 break;
688 case 'V':
689 vendor_id = strtol(optarg, NULL, 16);
690 break;
691 case 'P':
692 product_id = strtol(optarg, NULL, 16);
693 break;
694 case 'C':
695 config_id = atoi(optarg);
696 break;
697 case 'I':
698 if_num = atoi(optarg);
699 break;
700 case 'S':
701 altsetting = atoi(optarg);
702 break;
703 case 'A':
704 addr = atoi(optarg);
705 break;
706 case 'H':
707 path = optarg;
708 break;
709 }
710 }
711
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200712 if (vendor_id < 0 || product_id < 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200713 fprintf(stderr, "You have to specify the vendor and product ID\n");
714 goto do_exit;
715 }
716
Kévin Redon193a8c12018-10-11 17:24:39 +0200717 rc = libusb_init(NULL);
718 if (rc < 0) {
719 fprintf(stderr, "libusb initialization failed\n");
720 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +0200721 }
722
723 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
724 if (!g_gti) {
725 perror("unable to open GSMTAP");
726 goto close_exit;
727 }
728 gsmtap_source_add_sink(g_gti);
729
730 signal(SIGINT, &signal_handler);
731
732 // initialize remote SIM client
733 g_tall_ctx = talloc_named_const(NULL, 0, "global");
734
735 osmo_fsm_register(&remsim_client_bankd_fsm);
736 osmo_fsm_register(&remsim_client_server_fsm);
737
738 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200739 g_client->bankd_host = bankd_host;
740 g_client->bankd_port = bankd_port;
Kévin Redone0b837c2018-10-10 00:39:25 +0200741 g_client->own_comp_id.type = ComponentType_remsimClient;
742 g_client->clslot = &(ClientSlot_t){ .clientId = 23, .slotNr = 1 };
743 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "fixme-name");
744 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client");
745 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION);
746
747 asn_debug = 0;
748 osmo_init_logging2(g_tall_ctx, &log_info);
749
750 if (bankd_conn_fsm_alloc(g_client) < 0) {
751 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
752 exit(1);
753 }
754
755 // connect to SIMtrace2 cardem
756 do {
757 struct usb_interface_match _ifm, *ifm = &_ifm;
758 ifm->vendor = vendor_id;
759 ifm->product = product_id;
760 ifm->configuration = config_id;
761 ifm->interface = if_num;
762 ifm->altsetting = altsetting;
763 ifm->addr = addr;
764 if (path)
765 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
766 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
767 if (!transp->usb_devh) {
768 fprintf(stderr, "can't open USB device\n");
769 goto close_exit;
770 }
771
772 rc = libusb_claim_interface(transp->usb_devh, if_num);
773 if (rc < 0) {
774 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
775 goto close_exit;
776 }
777
778 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
779 &transp->usb_ep.in, &transp->usb_ep.irq_in);
780 if (rc < 0) {
781 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
782 goto close_exit;
783 }
784
785 /* simulate card-insert to modem (owhw, not qmod) */
786 cardem_request_card_insert(ci, true);
787
788 /* select remote (forwarded) SIM */
789 st_modem_sim_select_remote(ci->slot);
790
791 /* set the ATR */
792 uint8_t real_atr[] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
793 atr_update_csum(real_atr, sizeof(real_atr));
794 cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
795
796 /* select remote (forwarded) SIM */
797 st_modem_reset_pulse(ci->slot, 300);
798
799 run_mainloop(ci);
800 ret = 0;
801
Kévin Redon193a8c12018-10-11 17:24:39 +0200802 libusb_release_interface(transp->usb_devh, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200803close_exit:
804 if (transp->usb_devh)
805 libusb_close(transp->usb_devh);
806 if (keep_running)
807 sleep(1);
808 } while (keep_running);
809
Kévin Redon193a8c12018-10-11 17:24:39 +0200810 libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +0200811do_exit:
812 return ret;
813}