blob: 9793524f7c4567bcfcd05b9c76b36bb7e1d77744 [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
125 printf("<- %s\n", msgb_hexdump(msg));
126
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
188 printf("<= %s(%02x, %d)\n", __func__, pb, le);
189
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
209 printf("<= %s(%02x, %s, %d)\n", __func__, pb,
210 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
233 printf("<= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
234
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
264 printf("<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
265
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
350 printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
351 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
363 printf("=> PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
364
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
374 printf("=> ERROR: %u/%u/%u: %s\n",
375 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 Redon378d9832018-10-10 00:39:25 +0200389 printf("=> DATA: flags=%x, %s: ", data->flags,
390 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
424 printf("-> %s\n", osmo_hexdump(buf, len));
425
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) {
510 printf("URB: %s\n", osmo_hexdump(buf, xfer_len));
511 process_usb_msg(ci, buf, xfer_len);
512 msg_count++;
513 byte_count += xfer_len;
514 }
515 // handle remote SIM client fsm
516 // TODO register the USB fd for this select
517 osmo_select_main(true);
518 }
519}
520
521static struct st_transport _transp;
522
523static struct st_slot _slot = {
524 .transp = &_transp,
525 .slot_nr = 0,
526};
527
528struct cardem_inst _ci = {
529 .slot = &_slot,
530};
531
532struct cardem_inst *ci = &_ci;
533
534static void signal_handler(int signal)
535{
536 switch (signal) {
537 case SIGINT:
538 cardem_request_card_insert(ci, false);
539 exit(0);
540 break;
541 default:
542 break;
543 }
544}
545
546/** remsim_client **/
547
548static void push_and_send(struct ipa_client_conn *ipa, struct msgb *msg_tx)
549{
550 ipa_prepend_header_ext(msg_tx, IPAC_PROTO_EXT_RSPRO);
551 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
552 ipa_client_conn_send(ipa, msg_tx);
553 /* msg_tx is now queued and will be freed. */
554}
555
556void ipa_client_conn_send_rspro(struct ipa_client_conn *ipa, RsproPDU_t *rspro)
557{
558 struct msgb *msg = rspro_enc_msg(rspro);
559 OSMO_ASSERT(msg);
560 push_and_send(ipa, msg);
561}
562
Kévin Redon8670fb72018-10-11 08:41:57 +0200563static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu)
564{
565 OSMO_ASSERT(pdu);
566 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
567
568 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
569 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
570 return -1;
571 }
572
573 // save SW to our current APDU context
574 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
575 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
576 printf("SW=0x%02x%02x, len_rx=%d\n", ac.sw[0], ac.sw[1], card2modem->data.size - 2);
577 if (card2modem->data.size > 2) { // send PB and data to modem
578 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
579 }
580 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
581
582 return 0;
583}
584
Kévin Redon378d9832018-10-10 00:39:25 +0200585static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
586{
587 RsproPDU_t *pdu = rspro_dec_msg(msg);
588 if (!pdu) {
589 fprintf(stderr, "Error decoding PDU\n");
590 return -1;
591 }
592
593 switch (pdu->msg.present) {
594 case RsproPDUchoice_PR_connectClientRes:
595 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
596 break;
Kévin Redon8670fb72018-10-11 08:41:57 +0200597 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
598 bankd_handle_tpduCardToModem(bc, pdu);
599 break;
Kévin Redon378d9832018-10-10 00:39:25 +0200600 default:
601 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
602 return -1;
603 }
604
605 return 0;
606}
607
608int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
609{
610 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
611 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
612 struct bankd_client *bc = conn->data;
613 int rc;
614
615 if (msgb_length(msg) < sizeof(*hh))
616 goto invalid;
617 msg->l2h = &hh->data[0];
618 if (hh->proto != IPAC_PROTO_OSMO)
619 goto invalid;
620 if (!he || msgb_l2len(msg) < sizeof(*he))
621 goto invalid;
622 msg->l2h = &he->data[0];
623
624 if (he->proto != IPAC_PROTO_EXT_RSPRO)
625 goto invalid;
626
627 printf("Received RSPRO %s\n", msgb_hexdump(msg));
628
629 rc = bankd_handle_msg(bc, msg);
630
631 return rc;
632
633invalid:
634 msgb_free(msg);
635 return -1;
636}
637
638static const struct log_info_cat default_categories[] = {
639 [DMAIN] = {
640 .name = "DMAIN",
641 .loglevel = LOGL_DEBUG,
642 .enabled = 1,
643 },
644};
645
646static const struct log_info log_info = {
647 .cat = default_categories,
648 .num_cat = ARRAY_SIZE(default_categories),
649};
650
651int main(int argc, char **argv)
652{
653 struct st_transport *transp = ci->slot->transp;
654 char *gsmtap_host = "127.0.0.1";
655 int rc;
656 int c, ret = 1;
657 int keep_running = 0;
Kévin Redon911d2fb2018-10-11 17:21:13 +0200658 int bankd_port = 9999;
Kévin Redon378d9832018-10-10 00:39:25 +0200659 int if_num = 0, vendor_id = -1, product_id = -1;
660 int config_id = -1, altsetting = 0, addr = -1;
Kévin Redon911d2fb2018-10-11 17:21:13 +0200661 char *bankd_host = "127.0.0.1";
Kévin Redon378d9832018-10-10 00:39:25 +0200662 char *path = NULL;
663
664 print_welcome();
665
666 while (1) {
667 int option_index = 0;
668
Kévin Redon911d2fb2018-10-11 17:21:13 +0200669 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 +0200670 if (c == -1)
671 break;
672 switch (c) {
Kévin Redon911d2fb2018-10-11 17:21:13 +0200673 case 'b':
674 bankd_host = optarg;
Kévin Redon378d9832018-10-10 00:39:25 +0200675 break;
676 case 'p':
Kévin Redon911d2fb2018-10-11 17:21:13 +0200677 bankd_port = atoi(optarg);
Kévin Redon378d9832018-10-10 00:39:25 +0200678 break;
679 case 'h':
680 print_help();
681 exit(0);
682 break;
683 case 'i':
684 gsmtap_host = optarg;
685 break;
686 case 'k':
687 keep_running = 1;
688 break;
689 case 'V':
690 vendor_id = strtol(optarg, NULL, 16);
691 break;
692 case 'P':
693 product_id = strtol(optarg, NULL, 16);
694 break;
695 case 'C':
696 config_id = atoi(optarg);
697 break;
698 case 'I':
699 if_num = atoi(optarg);
700 break;
701 case 'S':
702 altsetting = atoi(optarg);
703 break;
704 case 'A':
705 addr = atoi(optarg);
706 break;
707 case 'H':
708 path = optarg;
709 break;
710 }
711 }
712
Kévin Redon911d2fb2018-10-11 17:21:13 +0200713 if (vendor_id < 0 || product_id < 0) {
Kévin Redon378d9832018-10-10 00:39:25 +0200714 fprintf(stderr, "You have to specify the vendor and product ID\n");
715 goto do_exit;
716 }
717
Kévin Redon2b0ab4a2018-10-11 17:24:39 +0200718 rc = libusb_init(NULL);
719 if (rc < 0) {
720 fprintf(stderr, "libusb initialization failed\n");
721 goto do_exit;
Kévin Redon378d9832018-10-10 00:39:25 +0200722 }
723
724 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
725 if (!g_gti) {
726 perror("unable to open GSMTAP");
727 goto close_exit;
728 }
729 gsmtap_source_add_sink(g_gti);
730
731 signal(SIGINT, &signal_handler);
732
733 // initialize remote SIM client
734 g_tall_ctx = talloc_named_const(NULL, 0, "global");
735
736 osmo_fsm_register(&remsim_client_bankd_fsm);
737 osmo_fsm_register(&remsim_client_server_fsm);
738
739 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Kévin Redon911d2fb2018-10-11 17:21:13 +0200740 g_client->bankd_host = bankd_host;
741 g_client->bankd_port = bankd_port;
Kévin Redon378d9832018-10-10 00:39:25 +0200742 g_client->own_comp_id.type = ComponentType_remsimClient;
743 g_client->clslot = &(ClientSlot_t){ .clientId = 23, .slotNr = 1 };
744 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "fixme-name");
745 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client");
746 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION);
747
748 asn_debug = 0;
749 osmo_init_logging2(g_tall_ctx, &log_info);
750
751 if (bankd_conn_fsm_alloc(g_client) < 0) {
752 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
753 exit(1);
754 }
755
756 // connect to SIMtrace2 cardem
757 do {
758 struct usb_interface_match _ifm, *ifm = &_ifm;
759 ifm->vendor = vendor_id;
760 ifm->product = product_id;
761 ifm->configuration = config_id;
762 ifm->interface = if_num;
763 ifm->altsetting = altsetting;
764 ifm->addr = addr;
765 if (path)
766 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
767 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
768 if (!transp->usb_devh) {
769 fprintf(stderr, "can't open USB device\n");
770 goto close_exit;
771 }
772
773 rc = libusb_claim_interface(transp->usb_devh, if_num);
774 if (rc < 0) {
775 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
776 goto close_exit;
777 }
778
779 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
780 &transp->usb_ep.in, &transp->usb_ep.irq_in);
781 if (rc < 0) {
782 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
783 goto close_exit;
784 }
785
786 /* simulate card-insert to modem (owhw, not qmod) */
787 cardem_request_card_insert(ci, true);
788
789 /* select remote (forwarded) SIM */
790 st_modem_sim_select_remote(ci->slot);
791
792 /* set the ATR */
793 uint8_t real_atr[] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
794 atr_update_csum(real_atr, sizeof(real_atr));
795 cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
796
797 /* select remote (forwarded) SIM */
798 st_modem_reset_pulse(ci->slot, 300);
799
800 run_mainloop(ci);
801 ret = 0;
802
Kévin Redon2b0ab4a2018-10-11 17:24:39 +0200803 libusb_release_interface(transp->usb_devh, 0);
Kévin Redon378d9832018-10-10 00:39:25 +0200804close_exit:
805 if (transp->usb_devh)
806 libusb_close(transp->usb_devh);
807 if (keep_running)
808 sleep(1);
809 } while (keep_running);
810
Kévin Redon2b0ab4a2018-10-11 17:24:39 +0200811 libusb_exit(NULL);
Kévin Redon378d9832018-10-10 00:39:25 +0200812do_exit:
813 return ret;
814}