blob: dc074c911941d272049f40b2b6b39a9e47585e84 [file] [log] [blame]
Kévin Redon378d9832018-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 "libusb_util.h"
27#include "simtrace.h"
28#include "simtrace_prot.h"
29#include "apdu_dispatch.h"
30#include "simtrace2-discovery.h"
31
32#include <osmocom/core/gsmtap.h>
33#include <osmocom/core/gsmtap_util.h>
34#include <osmocom/core/utils.h>
35#include <osmocom/core/socket.h>
36#include <osmocom/core/msgb.h>
37#include <osmocom/sim/class_tables.h>
38#include <osmocom/sim/sim.h>
39
40/* transport to a SIMtrace device */
41struct st_transport {
42 /* USB */
43 struct libusb_device_handle *usb_devh;
44 struct {
45 uint8_t in;
46 uint8_t out;
47 uint8_t irq_in;
48 } usb_ep;
Kévin Redon378d9832018-10-10 00:39:25 +020049};
50
51/* a SIMtrace slot; communicates over a transport */
52struct st_slot {
53 /* transport through which the slot can be reached */
54 struct st_transport *transp;
55 /* number of the slot within the transport */
56 uint8_t slot_nr;
57};
58
59/* One istance of card emulation */
60struct cardem_inst {
61 /* slot on which this card emulation instance runs */
62 struct st_slot *slot;
63};
64
65/* global GSMTAP instance */
66static struct gsmtap_inst *g_gti;
67
68static struct bankd_client *g_client;
69static void *g_tall_ctx;
70void __thread *talloc_asn1_ctx;
71int asn_debug;
72
73static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
74{
75 struct gsmtap_hdr *gh;
76 unsigned int gross_len = len + sizeof(*gh);
77 uint8_t *buf = malloc(gross_len);
78 int rc;
79
80 if (!buf)
81 return -ENOMEM;
82
83 memset(buf, 0, sizeof(*gh));
84 gh = (struct gsmtap_hdr *) buf;
85 gh->version = GSMTAP_VERSION;
86 gh->hdr_len = sizeof(*gh)/4;
87 gh->type = GSMTAP_TYPE_SIM;
88
89 memcpy(buf + sizeof(*gh), apdu, len);
90
91 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
92 if (rc < 0) {
93 perror("write gsmtap");
94 free(buf);
95 return rc;
96 }
97
98 free(buf);
99 return 0;
100}
101
102/***********************************************************************
103 * SIMTRACE pcore protocol
104 ***********************************************************************/
105
106/*! \brief allocate a message buffer for simtrace use */
107static struct msgb *st_msgb_alloc(void)
108{
109 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
110}
111
112#if 0
113static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
114{
115 printf("APDU: %s\n", osmo_hexdump(buf, len));
116 gsmtap_send_sim(buf, len);
117}
118#endif
119
120/*! \brief Transmit a given command to the SIMtrace2 device */
121int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
122{
123 int rc;
124
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200125 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redon378d9832018-10-10 00:39:25 +0200126
Kévin Redon2b0ab4a2018-10-11 17:24:39 +0200127 int xfer_len;
Kévin Redon378d9832018-10-10 00:39:25 +0200128
Kévin Redon2b0ab4a2018-10-11 17:24:39 +0200129 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
130 msgb_data(msg), msgb_length(msg),
131 &xfer_len, 100000);
Kévin Redon378d9832018-10-10 00:39:25 +0200132
133 msgb_free(msg);
134 return rc;
135}
136
137static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
138 uint8_t slot_nr)
139{
140 struct simtrace_msg_hdr *sh;
141
142 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
143 memset(sh, 0, sizeof(*sh));
144 sh->msg_class = msg_class;
145 sh->msg_type = msg_type;
146 sh->slot_nr = slot_nr;
147 sh->msg_len = msgb_length(msg);
148
149 return sh;
150}
151
152/* transmit a given message to a specified slot. Expects all headers
153 * present before calling the function */
154int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
155 uint8_t msg_class, uint8_t msg_type)
156{
157 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
158
159 return st_transp_tx_msg(slot->transp, msg);
160}
161
162/***********************************************************************
163 * Card Emulation protocol
164 ***********************************************************************/
165
166
167/*! \brief Request the SIMtrace2 to generate a card-insert signal */
168static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
169{
170 struct msgb *msg = st_msgb_alloc();
171 struct cardemu_usb_msg_cardinsert *cins;
172
173 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
174 memset(cins, 0, sizeof(*cins));
175 if (inserted)
176 cins->card_insert = 1;
177
178 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
179}
180
181/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
182static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
183{
184 struct msgb *msg = st_msgb_alloc();
185 struct cardemu_usb_msg_tx_data *txd;
186 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
187
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200188 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redon378d9832018-10-10 00:39:25 +0200189
190 memset(txd, 0, sizeof(*txd));
191 txd->data_len = 1;
192 txd->flags = CEMU_DATA_F_PB_AND_RX;
193 /* one data byte */
194 msgb_put_u8(msg, pb);
195
196 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
197}
198
199/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
200static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
201 const uint8_t *data, uint8_t data_len_in)
202{
203 struct msgb *msg = st_msgb_alloc();
204 struct cardemu_usb_msg_tx_data *txd;
205 uint8_t *cur;
206
207 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
208
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200209 printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb,
Kévin Redon378d9832018-10-10 00:39:25 +0200210 osmo_hexdump(data, data_len_in), data_len_in);
211
212 memset(txd, 0, sizeof(*txd));
213 txd->data_len = 1 + data_len_in;
214 txd->flags = CEMU_DATA_F_PB_AND_TX;
215 /* procedure byte */
216 msgb_put_u8(msg, pb);
217 /* data */
218 cur = msgb_put(msg, data_len_in);
219 memcpy(cur, data, data_len_in);
220
221 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
222}
223
224/*! \brief Request the SIMtrace2 to send a Status Word */
225static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
226{
227 struct msgb *msg = st_msgb_alloc();
228 struct cardemu_usb_msg_tx_data *txd;
229 uint8_t *cur;
230
231 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
232
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200233 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redon378d9832018-10-10 00:39:25 +0200234
235 memset(txd, 0, sizeof(*txd));
236 txd->data_len = 2;
237 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
238 cur = msgb_put(msg, 2);
239 cur[0] = sw[0];
240 cur[1] = sw[1];
241
242 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
243}
244
245static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
246{
247 uint8_t csum = 0;
248 int i;
249
250 for (i = 1; i < atr_len - 1; i++)
251 csum = csum ^ atr[i];
252
253 atr[atr_len-1] = csum;
254}
255
256static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
257{
258 struct msgb *msg = st_msgb_alloc();
259 struct cardemu_usb_msg_set_atr *satr;
260 uint8_t *cur;
261
262 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
263
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200264 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redon378d9832018-10-10 00:39:25 +0200265
266 memset(satr, 0, sizeof(*satr));
267 satr->atr_len = atr_len;
268 cur = msgb_put(msg, atr_len);
269 memcpy(cur, atr, atr_len);
270
271 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
272}
273
274/***********************************************************************
275 * Modem Control protocol
276 ***********************************************************************/
277
278static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
279{
280 struct msgb *msg = st_msgb_alloc();
281 struct st_modem_reset *sr ;
282
283 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
284 sr->asserted = asserted;
285 sr->pulse_duration_msec = pulse_ms;
286
287 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
288}
289
290/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
291int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
292{
293 return _modem_reset(slot, 2, duration_ms);
294}
295
296/*! \brief assert the RESET line of the modem */
297int st_modem_reset_active(struct st_slot *slot)
298{
299 return _modem_reset(slot, 1, 0);
300}
301
302/*! \brief de-assert the RESET line of the modem */
303int st_modem_reset_inactive(struct st_slot *slot)
304{
305 return _modem_reset(slot, 0, 0);
306}
307
308static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
309{
310 struct msgb *msg = st_msgb_alloc();
311 struct st_modem_sim_select *ss;
312
313 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
314 ss->remote_sim = remote_sim;
315
316 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
317}
318
319/*! \brief select local (physical) SIM for given slot */
320int st_modem_sim_select_local(struct st_slot *slot)
321{
322 return _modem_sim_select(slot, 0);
323}
324
325/*! \brief select remote (emulated/forwarded) SIM for given slot */
326int st_modem_sim_select_remote(struct st_slot *slot)
327{
328 return _modem_sim_select(slot, 1);
329}
330
331/*! \brief Request slot to send us status information about the modem */
332int st_modem_get_status(struct st_slot *slot)
333{
334 struct msgb *msg = st_msgb_alloc();
335
336 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
337}
338
339
340/***********************************************************************
341 * Incoming Messages
342 ***********************************************************************/
343
344/*! \brief Process a STATUS message from the SIMtrace2 */
345static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
346{
347 struct cardemu_usb_msg_status *status;
348 status = (struct cardemu_usb_msg_status *) buf;
349
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200350 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redon378d9832018-10-10 00:39:25 +0200351 status->flags, status->fi, status->di, status->wi,
352 status->waiting_time);
353
354 return 0;
355}
356
357/*! \brief Process a PTS indication message from the SIMtrace2 */
358static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
359{
360 struct cardemu_usb_msg_pts_info *pts;
361 pts = (struct cardemu_usb_msg_pts_info *) buf;
362
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200363 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redon378d9832018-10-10 00:39:25 +0200364
365 return 0;
366}
367
368/*! \brief Process a ERROR indication message from the SIMtrace2 */
369static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
370{
371 struct cardemu_usb_msg_error *err;
372 err = (struct cardemu_usb_msg_error *) buf;
373
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200374 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redon378d9832018-10-10 00:39:25 +0200375 err->severity, err->subsystem, err->code,
376 err->msg_len ? (char *)err->msg : "");
377
378 return 0;
379}
380
Kévin Redon640b2ca2018-10-11 08:41:00 +0200381static struct apdu_context ac; // this will hold the complete APDU (across calls)
382
Kévin Redon378d9832018-10-10 00:39:25 +0200383/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
384static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
385{
Kévin Redon640b2ca2018-10-11 08:41:00 +0200386 struct cardemu_usb_msg_rx_data *data = (struct cardemu_usb_msg_rx_data *) buf; // cast the data from the USB message
Kévin Redon378d9832018-10-10 00:39:25 +0200387 int rc;
388
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200389 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redon378d9832018-10-10 00:39:25 +0200390 osmo_hexdump(data->data, data->data_len));
391
392 rc = apdu_segment_in(&ac, data->data, data->data_len,
Kévin Redon640b2ca2018-10-11 08:41:00 +0200393 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redon378d9832018-10-10 00:39:25 +0200394
Kévin Redon640b2ca2018-10-11 08:41:00 +0200395 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
396 uint8_t* apdu_command = calloc(1, sizeof(ac.hdr) + ac.lc.tot); // to store the APDU command to send
397 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redon378d9832018-10-10 00:39:25 +0200398 if (ac.lc.tot) {
Kévin Redon640b2ca2018-10-11 08:41:00 +0200399 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redon378d9832018-10-10 00:39:25 +0200400 }
Kévin Redon640b2ca2018-10-11 08:41:00 +0200401 // send APDU to card
402 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
403 ipa_client_conn_send_rspro(g_client->bankd_conn, pdu); // send RSPRO packet
404 // the response will come separately
405 free(apdu_command);
406 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
407 cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur); // send procedure byte to get remaining data
Kévin Redon378d9832018-10-10 00:39:25 +0200408 }
409 return 0;
410}
411
412#if 0
413 case SIMTRACE_CMD_DO_ERROR
414 rc = process_do_error(ci, buf, len);
415 break;
416#endif
417
418/*! \brief Process an incoming message from the SIMtrace2 */
419static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
420{
421 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
422 int rc;
423
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200424 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redon378d9832018-10-10 00:39:25 +0200425
426 buf += sizeof(*sh);
427
428 switch (sh->msg_type) {
429 case SIMTRACE_MSGT_BD_CEMU_STATUS:
430 rc = process_do_status(ci, buf, len);
431 break;
432 case SIMTRACE_MSGT_DO_CEMU_PTS:
433 rc = process_do_pts(ci, buf, len);
434 break;
435 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
436 rc = process_do_rx_da(ci, buf, len);
437 break;
438 default:
439 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
440 rc = -1;
441 break;
442 }
443
444 return rc;
445}
446
447static void print_welcome(void)
448{
Kévin Redon911d2fb2018-10-11 17:21:13 +0200449 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
Kévin Redon378d9832018-10-10 00:39:25 +0200450 "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
451 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
452}
453
454static void print_help(void)
455{
Kévin Redon911d2fb2018-10-11 17:21:13 +0200456 printf( "\t-d\t--bankd-host HOST\n"
457 "\t-p\t--bankd-port PORT\n"
Kévin Redon378d9832018-10-10 00:39:25 +0200458 "\t-h\t--help\n"
459 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
460 "\t-k\t--keep-running\n"
461 "\t-V\t--usb-vendor\tVENDOR_ID\n"
462 "\t-P\t--usb-product\tPRODUCT_ID\n"
463 "\t-C\t--usb-config\tCONFIG_ID\n"
464 "\t-I\t--usb-interface\tINTERFACE_ID\n"
465 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
466 "\t-A\t--usb-address\tADDRESS\n"
467 "\t-H\t--usb-path\tPATH\n"
468 "\n"
469 );
470}
471
472static const struct option opts[] = {
Kévin Redon911d2fb2018-10-11 17:21:13 +0200473 { "bankd-host", 1, 0, 'b' },
474 { "bankd-port", 1, 0, 'p' },
Kévin Redon378d9832018-10-10 00:39:25 +0200475 { "gsmtap-ip", 1, 0, 'i' },
476 { "help", 0, 0, 'h' },
477 { "keep-running", 0, 0, 'k' },
478 { "usb-vendor", 1, 0, 'V' },
479 { "usb-product", 1, 0, 'P' },
480 { "usb-config", 1, 0, 'C' },
481 { "usb-interface", 1, 0, 'I' },
482 { "usb-altsetting", 1, 0, 'S' },
483 { "usb-address", 1, 0, 'A' },
484 { "usb-path", 1, 0, 'H' },
485 { NULL, 0, 0, 0 }
486};
487
488static void run_mainloop(struct cardem_inst *ci)
489{
490 struct st_transport *transp = ci->slot->transp;
491 unsigned int msg_count, byte_count = 0;
492 uint8_t buf[16*265];
493 int xfer_len;
494 int rc;
495
496 printf("Entering main loop\n");
497
498 while (1) {
499 /* read data from SIMtrace2 device (local or via USB) */
500 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
501 buf, sizeof(buf), &xfer_len, 100);
502 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
503 rc != LIBUSB_ERROR_INTERRUPTED &&
504 rc != LIBUSB_ERROR_IO) {
505 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
506 return;
507 }
508 /* dispatch any incoming data */
509 if (xfer_len > 0) {
Kévin Redon378d9832018-10-10 00:39:25 +0200510 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 Redon8670fb72018-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 Redon34c0dbe2018-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 Redon8670fb72018-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 Redon378d9832018-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 Redon8670fb72018-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 Redon378d9832018-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 Redon911d2fb2018-10-11 17:21:13 +0200657 int bankd_port = 9999;
Kévin Redon378d9832018-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 Redon911d2fb2018-10-11 17:21:13 +0200660 char *bankd_host = "127.0.0.1";
Kévin Redon378d9832018-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 Redon911d2fb2018-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 Redon378d9832018-10-10 00:39:25 +0200669 if (c == -1)
670 break;
671 switch (c) {
Kévin Redon911d2fb2018-10-11 17:21:13 +0200672 case 'b':
673 bankd_host = optarg;
Kévin Redon378d9832018-10-10 00:39:25 +0200674 break;
675 case 'p':
Kévin Redon911d2fb2018-10-11 17:21:13 +0200676 bankd_port = atoi(optarg);
Kévin Redon378d9832018-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 Redon911d2fb2018-10-11 17:21:13 +0200712 if (vendor_id < 0 || product_id < 0) {
Kévin Redon378d9832018-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 Redon2b0ab4a2018-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 Redon378d9832018-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 Redon911d2fb2018-10-11 17:21:13 +0200739 g_client->bankd_host = bankd_host;
740 g_client->bankd_port = bankd_port;
Kévin Redon378d9832018-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 Redon2b0ab4a2018-10-11 17:24:39 +0200802 libusb_release_interface(transp->usb_devh, 0);
Kévin Redon378d9832018-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 Redon2b0ab4a2018-10-11 17:24:39 +0200810 libusb_exit(NULL);
Kévin Redon378d9832018-10-10 00:39:25 +0200811do_exit:
812 return ret;
813}