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