blob: cbbbd62d8d9bfb728aa84b8911bf6bb2284e1deb [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
Kévin Redon640b2ca2018-10-11 08:41:00 +0200388static struct apdu_context ac; // this will hold the complete APDU (across calls)
389
Kévin Redon378d9832018-10-10 00:39:25 +0200390/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
391static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
392{
Kévin Redon640b2ca2018-10-11 08:41:00 +0200393 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 +0200394 int rc;
395
Kévin Redon378d9832018-10-10 00:39:25 +0200396 printf("=> DATA: flags=%x, %s: ", data->flags,
397 osmo_hexdump(data->data, data->data_len));
398
399 rc = apdu_segment_in(&ac, data->data, data->data_len,
Kévin Redon640b2ca2018-10-11 08:41:00 +0200400 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redon378d9832018-10-10 00:39:25 +0200401
Kévin Redon640b2ca2018-10-11 08:41:00 +0200402 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
403 uint8_t* apdu_command = calloc(1, sizeof(ac.hdr) + ac.lc.tot); // to store the APDU command to send
404 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redon378d9832018-10-10 00:39:25 +0200405 if (ac.lc.tot) {
Kévin Redon640b2ca2018-10-11 08:41:00 +0200406 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redon378d9832018-10-10 00:39:25 +0200407 }
Kévin Redon640b2ca2018-10-11 08:41:00 +0200408 // send APDU to card
409 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
410 ipa_client_conn_send_rspro(g_client->bankd_conn, pdu); // send RSPRO packet
411 // the response will come separately
412 free(apdu_command);
413 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
414 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 +0200415 }
416 return 0;
417}
418
419#if 0
420 case SIMTRACE_CMD_DO_ERROR
421 rc = process_do_error(ci, buf, len);
422 break;
423#endif
424
425/*! \brief Process an incoming message from the SIMtrace2 */
426static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
427{
428 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
429 int rc;
430
431 printf("-> %s\n", osmo_hexdump(buf, len));
432
433 buf += sizeof(*sh);
434
435 switch (sh->msg_type) {
436 case SIMTRACE_MSGT_BD_CEMU_STATUS:
437 rc = process_do_status(ci, buf, len);
438 break;
439 case SIMTRACE_MSGT_DO_CEMU_PTS:
440 rc = process_do_pts(ci, buf, len);
441 break;
442 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
443 rc = process_do_rx_da(ci, buf, len);
444 break;
445 default:
446 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
447 rc = -1;
448 break;
449 }
450
451 return rc;
452}
453
454static void print_welcome(void)
455{
456 printf("simtrace2-remsim - Remote SIM card forwarding\n"
457 "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
458 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
459}
460
461static void print_help(void)
462{
463 printf( "\t-r\t--remote-udp-host HOST\n"
464 "\t-p\t--remote-udp-port PORT\n"
465 "\t-h\t--help\n"
466 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
467 "\t-k\t--keep-running\n"
468 "\t-V\t--usb-vendor\tVENDOR_ID\n"
469 "\t-P\t--usb-product\tPRODUCT_ID\n"
470 "\t-C\t--usb-config\tCONFIG_ID\n"
471 "\t-I\t--usb-interface\tINTERFACE_ID\n"
472 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
473 "\t-A\t--usb-address\tADDRESS\n"
474 "\t-H\t--usb-path\tPATH\n"
475 "\n"
476 );
477}
478
479static const struct option opts[] = {
480 { "remote-udp-host", 1, 0, 'r' },
481 { "remote-udp-port", 1, 0, 'p' },
482 { "gsmtap-ip", 1, 0, 'i' },
483 { "help", 0, 0, 'h' },
484 { "keep-running", 0, 0, 'k' },
485 { "usb-vendor", 1, 0, 'V' },
486 { "usb-product", 1, 0, 'P' },
487 { "usb-config", 1, 0, 'C' },
488 { "usb-interface", 1, 0, 'I' },
489 { "usb-altsetting", 1, 0, 'S' },
490 { "usb-address", 1, 0, 'A' },
491 { "usb-path", 1, 0, 'H' },
492 { NULL, 0, 0, 0 }
493};
494
495static void run_mainloop(struct cardem_inst *ci)
496{
497 struct st_transport *transp = ci->slot->transp;
498 unsigned int msg_count, byte_count = 0;
499 uint8_t buf[16*265];
500 int xfer_len;
501 int rc;
502
503 printf("Entering main loop\n");
504
505 while (1) {
506 /* read data from SIMtrace2 device (local or via USB) */
507 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
508 buf, sizeof(buf), &xfer_len, 100);
509 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
510 rc != LIBUSB_ERROR_INTERRUPTED &&
511 rc != LIBUSB_ERROR_IO) {
512 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
513 return;
514 }
515 /* dispatch any incoming data */
516 if (xfer_len > 0) {
517 printf("URB: %s\n", osmo_hexdump(buf, xfer_len));
518 process_usb_msg(ci, buf, xfer_len);
519 msg_count++;
520 byte_count += xfer_len;
521 }
522 // handle remote SIM client fsm
523 // TODO register the USB fd for this select
524 osmo_select_main(true);
525 }
526}
527
528static struct st_transport _transp;
529
530static struct st_slot _slot = {
531 .transp = &_transp,
532 .slot_nr = 0,
533};
534
535struct cardem_inst _ci = {
536 .slot = &_slot,
537};
538
539struct cardem_inst *ci = &_ci;
540
541static void signal_handler(int signal)
542{
543 switch (signal) {
544 case SIGINT:
545 cardem_request_card_insert(ci, false);
546 exit(0);
547 break;
548 default:
549 break;
550 }
551}
552
553/** remsim_client **/
554
555static void push_and_send(struct ipa_client_conn *ipa, struct msgb *msg_tx)
556{
557 ipa_prepend_header_ext(msg_tx, IPAC_PROTO_EXT_RSPRO);
558 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
559 ipa_client_conn_send(ipa, msg_tx);
560 /* msg_tx is now queued and will be freed. */
561}
562
563void ipa_client_conn_send_rspro(struct ipa_client_conn *ipa, RsproPDU_t *rspro)
564{
565 struct msgb *msg = rspro_enc_msg(rspro);
566 OSMO_ASSERT(msg);
567 push_and_send(ipa, msg);
568}
569
570static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
571{
572 RsproPDU_t *pdu = rspro_dec_msg(msg);
573 if (!pdu) {
574 fprintf(stderr, "Error decoding PDU\n");
575 return -1;
576 }
577
578 switch (pdu->msg.present) {
579 case RsproPDUchoice_PR_connectClientRes:
580 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
581 break;
582 default:
583 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
584 return -1;
585 }
586
587 return 0;
588}
589
590int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
591{
592 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
593 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
594 struct bankd_client *bc = conn->data;
595 int rc;
596
597 if (msgb_length(msg) < sizeof(*hh))
598 goto invalid;
599 msg->l2h = &hh->data[0];
600 if (hh->proto != IPAC_PROTO_OSMO)
601 goto invalid;
602 if (!he || msgb_l2len(msg) < sizeof(*he))
603 goto invalid;
604 msg->l2h = &he->data[0];
605
606 if (he->proto != IPAC_PROTO_EXT_RSPRO)
607 goto invalid;
608
609 printf("Received RSPRO %s\n", msgb_hexdump(msg));
610
611 rc = bankd_handle_msg(bc, msg);
612
613 return rc;
614
615invalid:
616 msgb_free(msg);
617 return -1;
618}
619
620static const struct log_info_cat default_categories[] = {
621 [DMAIN] = {
622 .name = "DMAIN",
623 .loglevel = LOGL_DEBUG,
624 .enabled = 1,
625 },
626};
627
628static const struct log_info log_info = {
629 .cat = default_categories,
630 .num_cat = ARRAY_SIZE(default_categories),
631};
632
633int main(int argc, char **argv)
634{
635 struct st_transport *transp = ci->slot->transp;
636 char *gsmtap_host = "127.0.0.1";
637 int rc;
638 int c, ret = 1;
639 int keep_running = 0;
640 int remote_udp_port = 52342;
641 int if_num = 0, vendor_id = -1, product_id = -1;
642 int config_id = -1, altsetting = 0, addr = -1;
643 char *remote_udp_host = NULL;
644 char *path = NULL;
645
646 print_welcome();
647
648 while (1) {
649 int option_index = 0;
650
651 c = getopt_long(argc, argv, "r:p:hi:V:P:C:I:S:A:H:ak", opts, &option_index);
652 if (c == -1)
653 break;
654 switch (c) {
655 case 'r':
656 remote_udp_host = optarg;
657 break;
658 case 'p':
659 remote_udp_port = atoi(optarg);
660 break;
661 case 'h':
662 print_help();
663 exit(0);
664 break;
665 case 'i':
666 gsmtap_host = optarg;
667 break;
668 case 'k':
669 keep_running = 1;
670 break;
671 case 'V':
672 vendor_id = strtol(optarg, NULL, 16);
673 break;
674 case 'P':
675 product_id = strtol(optarg, NULL, 16);
676 break;
677 case 'C':
678 config_id = atoi(optarg);
679 break;
680 case 'I':
681 if_num = atoi(optarg);
682 break;
683 case 'S':
684 altsetting = atoi(optarg);
685 break;
686 case 'A':
687 addr = atoi(optarg);
688 break;
689 case 'H':
690 path = optarg;
691 break;
692 }
693 }
694
695 if (!remote_udp_host && (vendor_id < 0 || product_id < 0)) {
696 fprintf(stderr, "You have to specify the vendor and product ID\n");
697 goto do_exit;
698 }
699
700 transp->udp_fd = -1;
701
702 if (!remote_udp_host) {
703 rc = libusb_init(NULL);
704 if (rc < 0) {
705 fprintf(stderr, "libusb initialization failed\n");
706 goto do_exit;
707 }
708 } else {
709 transp->udp_fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
710 remote_udp_host, remote_udp_port+if_num,
711 OSMO_SOCK_F_CONNECT);
712 if (transp->udp_fd < 0) {
713 fprintf(stderr, "error binding UDP port\n");
714 goto do_exit;
715 }
716 }
717
718 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
719 if (!g_gti) {
720 perror("unable to open GSMTAP");
721 goto close_exit;
722 }
723 gsmtap_source_add_sink(g_gti);
724
725 signal(SIGINT, &signal_handler);
726
727 // initialize remote SIM client
728 g_tall_ctx = talloc_named_const(NULL, 0, "global");
729
730 osmo_fsm_register(&remsim_client_bankd_fsm);
731 osmo_fsm_register(&remsim_client_server_fsm);
732
733 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
734 g_client->bankd_host = "localhost";
735 g_client->bankd_port = 9999;
736 g_client->own_comp_id.type = ComponentType_remsimClient;
737 g_client->clslot = &(ClientSlot_t){ .clientId = 23, .slotNr = 1 };
738 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "fixme-name");
739 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client");
740 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION);
741
742 asn_debug = 0;
743 osmo_init_logging2(g_tall_ctx, &log_info);
744
745 if (bankd_conn_fsm_alloc(g_client) < 0) {
746 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
747 exit(1);
748 }
749
750 // connect to SIMtrace2 cardem
751 do {
752 struct usb_interface_match _ifm, *ifm = &_ifm;
753 ifm->vendor = vendor_id;
754 ifm->product = product_id;
755 ifm->configuration = config_id;
756 ifm->interface = if_num;
757 ifm->altsetting = altsetting;
758 ifm->addr = addr;
759 if (path)
760 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
761 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
762 if (!transp->usb_devh) {
763 fprintf(stderr, "can't open USB device\n");
764 goto close_exit;
765 }
766
767 rc = libusb_claim_interface(transp->usb_devh, if_num);
768 if (rc < 0) {
769 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
770 goto close_exit;
771 }
772
773 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
774 &transp->usb_ep.in, &transp->usb_ep.irq_in);
775 if (rc < 0) {
776 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
777 goto close_exit;
778 }
779
780 /* simulate card-insert to modem (owhw, not qmod) */
781 cardem_request_card_insert(ci, true);
782
783 /* select remote (forwarded) SIM */
784 st_modem_sim_select_remote(ci->slot);
785
786 /* set the ATR */
787 uint8_t real_atr[] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
788 atr_update_csum(real_atr, sizeof(real_atr));
789 cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
790
791 /* select remote (forwarded) SIM */
792 st_modem_reset_pulse(ci->slot, 300);
793
794 run_mainloop(ci);
795 ret = 0;
796
797 if (transp->udp_fd < 0)
798 libusb_release_interface(transp->usb_devh, 0);
799close_exit:
800 if (transp->usb_devh)
801 libusb_close(transp->usb_devh);
802 if (keep_running)
803 sleep(1);
804 } while (keep_running);
805
806 if (transp->udp_fd < 0)
807 libusb_exit(NULL);
808do_exit:
809 return ret;
810}