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