blob: 31c52557053bd1f1fb346a210b7ea9453a0c2bba [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>
Kévin Redond3843e52018-10-11 19:14:00 +020021#include <linux/limits.h>
22#include <sys/stat.h>
23#include <fcntl.h>
Kévin Redon378d9832018-10-10 00:39:25 +020024#include <signal.h>
25#include <getopt.h>
26
27#include <libusb.h>
28
29#include "libusb_util.h"
30#include "simtrace.h"
31#include "simtrace_prot.h"
Kévin Redond3843e52018-10-11 19:14:00 +020032#include "simtrace_usb.h"
Kévin Redon378d9832018-10-10 00:39:25 +020033#include "apdu_dispatch.h"
34#include "simtrace2-discovery.h"
35
36#include <osmocom/core/gsmtap.h>
37#include <osmocom/core/gsmtap_util.h>
38#include <osmocom/core/utils.h>
39#include <osmocom/core/socket.h>
40#include <osmocom/core/msgb.h>
41#include <osmocom/sim/class_tables.h>
42#include <osmocom/sim/sim.h>
43
44/* transport to a SIMtrace device */
45struct st_transport {
46 /* USB */
47 struct libusb_device_handle *usb_devh;
48 struct {
49 uint8_t in;
50 uint8_t out;
51 uint8_t irq_in;
52 } usb_ep;
Kévin Redon378d9832018-10-10 00:39:25 +020053};
54
55/* a SIMtrace slot; communicates over a transport */
56struct st_slot {
57 /* transport through which the slot can be reached */
58 struct st_transport *transp;
59 /* number of the slot within the transport */
60 uint8_t slot_nr;
61};
62
63/* One istance of card emulation */
64struct cardem_inst {
65 /* slot on which this card emulation instance runs */
66 struct st_slot *slot;
67};
68
69/* global GSMTAP instance */
70static struct gsmtap_inst *g_gti;
71
72static struct bankd_client *g_client;
73static void *g_tall_ctx;
74void __thread *talloc_asn1_ctx;
75int asn_debug;
76
77static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
78{
79 struct gsmtap_hdr *gh;
80 unsigned int gross_len = len + sizeof(*gh);
81 uint8_t *buf = malloc(gross_len);
82 int rc;
83
84 if (!buf)
85 return -ENOMEM;
86
87 memset(buf, 0, sizeof(*gh));
88 gh = (struct gsmtap_hdr *) buf;
89 gh->version = GSMTAP_VERSION;
90 gh->hdr_len = sizeof(*gh)/4;
91 gh->type = GSMTAP_TYPE_SIM;
92
93 memcpy(buf + sizeof(*gh), apdu, len);
94
95 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
96 if (rc < 0) {
97 perror("write gsmtap");
98 free(buf);
99 return rc;
100 }
101
102 free(buf);
103 return 0;
104}
105
106/***********************************************************************
107 * SIMTRACE pcore protocol
108 ***********************************************************************/
109
110/*! \brief allocate a message buffer for simtrace use */
111static struct msgb *st_msgb_alloc(void)
112{
113 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
114}
115
116#if 0
117static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
118{
119 printf("APDU: %s\n", osmo_hexdump(buf, len));
120 gsmtap_send_sim(buf, len);
121}
122#endif
123
124/*! \brief Transmit a given command to the SIMtrace2 device */
125int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
126{
127 int rc;
128
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200129 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redon378d9832018-10-10 00:39:25 +0200130
Kévin Redon2b0ab4a2018-10-11 17:24:39 +0200131 int xfer_len;
Kévin Redon378d9832018-10-10 00:39:25 +0200132
Kévin Redon2b0ab4a2018-10-11 17:24:39 +0200133 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
134 msgb_data(msg), msgb_length(msg),
135 &xfer_len, 100000);
Kévin Redon378d9832018-10-10 00:39:25 +0200136
137 msgb_free(msg);
138 return rc;
139}
140
141static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
142 uint8_t slot_nr)
143{
144 struct simtrace_msg_hdr *sh;
145
146 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
147 memset(sh, 0, sizeof(*sh));
148 sh->msg_class = msg_class;
149 sh->msg_type = msg_type;
150 sh->slot_nr = slot_nr;
151 sh->msg_len = msgb_length(msg);
152
153 return sh;
154}
155
156/* transmit a given message to a specified slot. Expects all headers
157 * present before calling the function */
158int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
159 uint8_t msg_class, uint8_t msg_type)
160{
161 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
162
163 return st_transp_tx_msg(slot->transp, msg);
164}
165
166/***********************************************************************
167 * Card Emulation protocol
168 ***********************************************************************/
169
170
171/*! \brief Request the SIMtrace2 to generate a card-insert signal */
172static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
173{
174 struct msgb *msg = st_msgb_alloc();
175 struct cardemu_usb_msg_cardinsert *cins;
176
177 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
178 memset(cins, 0, sizeof(*cins));
179 if (inserted)
180 cins->card_insert = 1;
181
182 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
183}
184
185/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
186static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
187{
188 struct msgb *msg = st_msgb_alloc();
189 struct cardemu_usb_msg_tx_data *txd;
190 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
191
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200192 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redon378d9832018-10-10 00:39:25 +0200193
194 memset(txd, 0, sizeof(*txd));
195 txd->data_len = 1;
196 txd->flags = CEMU_DATA_F_PB_AND_RX;
197 /* one data byte */
198 msgb_put_u8(msg, pb);
199
200 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
201}
202
203/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
204static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
Kévin Redon95c6c5d2018-10-15 19:53:02 +0200205 const uint8_t *data, uint16_t data_len_in)
Kévin Redon378d9832018-10-10 00:39:25 +0200206{
207 struct msgb *msg = st_msgb_alloc();
208 struct cardemu_usb_msg_tx_data *txd;
209 uint8_t *cur;
210
211 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
212
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200213 printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb,
Kévin Redon378d9832018-10-10 00:39:25 +0200214 osmo_hexdump(data, data_len_in), data_len_in);
215
216 memset(txd, 0, sizeof(*txd));
217 txd->data_len = 1 + data_len_in;
218 txd->flags = CEMU_DATA_F_PB_AND_TX;
219 /* procedure byte */
220 msgb_put_u8(msg, pb);
221 /* data */
222 cur = msgb_put(msg, data_len_in);
223 memcpy(cur, data, data_len_in);
224
225 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
226}
227
228/*! \brief Request the SIMtrace2 to send a Status Word */
229static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
230{
231 struct msgb *msg = st_msgb_alloc();
232 struct cardemu_usb_msg_tx_data *txd;
233 uint8_t *cur;
234
235 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
236
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200237 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redon378d9832018-10-10 00:39:25 +0200238
239 memset(txd, 0, sizeof(*txd));
240 txd->data_len = 2;
241 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
242 cur = msgb_put(msg, 2);
243 cur[0] = sw[0];
244 cur[1] = sw[1];
245
246 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
247}
248
249static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
250{
251 uint8_t csum = 0;
252 int i;
253
254 for (i = 1; i < atr_len - 1; i++)
255 csum = csum ^ atr[i];
256
257 atr[atr_len-1] = csum;
258}
259
260static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
261{
262 struct msgb *msg = st_msgb_alloc();
263 struct cardemu_usb_msg_set_atr *satr;
264 uint8_t *cur;
265
266 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
267
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200268 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redon378d9832018-10-10 00:39:25 +0200269
270 memset(satr, 0, sizeof(*satr));
271 satr->atr_len = atr_len;
272 cur = msgb_put(msg, atr_len);
273 memcpy(cur, atr, atr_len);
274
275 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
276}
277
278/***********************************************************************
279 * Modem Control protocol
280 ***********************************************************************/
281
282static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
283{
284 struct msgb *msg = st_msgb_alloc();
285 struct st_modem_reset *sr ;
286
287 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
288 sr->asserted = asserted;
289 sr->pulse_duration_msec = pulse_ms;
290
291 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
292}
293
294/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
295int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
296{
297 return _modem_reset(slot, 2, duration_ms);
298}
299
300/*! \brief assert the RESET line of the modem */
301int st_modem_reset_active(struct st_slot *slot)
302{
303 return _modem_reset(slot, 1, 0);
304}
305
306/*! \brief de-assert the RESET line of the modem */
307int st_modem_reset_inactive(struct st_slot *slot)
308{
309 return _modem_reset(slot, 0, 0);
310}
311
312static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
313{
314 struct msgb *msg = st_msgb_alloc();
315 struct st_modem_sim_select *ss;
316
317 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
318 ss->remote_sim = remote_sim;
319
320 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
321}
322
323/*! \brief select local (physical) SIM for given slot */
324int st_modem_sim_select_local(struct st_slot *slot)
325{
326 return _modem_sim_select(slot, 0);
327}
328
329/*! \brief select remote (emulated/forwarded) SIM for given slot */
330int st_modem_sim_select_remote(struct st_slot *slot)
331{
332 return _modem_sim_select(slot, 1);
333}
334
335/*! \brief Request slot to send us status information about the modem */
336int st_modem_get_status(struct st_slot *slot)
337{
338 struct msgb *msg = st_msgb_alloc();
339
340 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
341}
342
343
344/***********************************************************************
345 * Incoming Messages
346 ***********************************************************************/
347
348/*! \brief Process a STATUS message from the SIMtrace2 */
349static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
350{
351 struct cardemu_usb_msg_status *status;
352 status = (struct cardemu_usb_msg_status *) buf;
353
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200354 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redon378d9832018-10-10 00:39:25 +0200355 status->flags, status->fi, status->di, status->wi,
356 status->waiting_time);
357
358 return 0;
359}
360
361/*! \brief Process a PTS indication message from the SIMtrace2 */
362static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
363{
364 struct cardemu_usb_msg_pts_info *pts;
365 pts = (struct cardemu_usb_msg_pts_info *) buf;
366
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200367 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redon378d9832018-10-10 00:39:25 +0200368
369 return 0;
370}
371
372/*! \brief Process a ERROR indication message from the SIMtrace2 */
373static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
374{
375 struct cardemu_usb_msg_error *err;
376 err = (struct cardemu_usb_msg_error *) buf;
377
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200378 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redon378d9832018-10-10 00:39:25 +0200379 err->severity, err->subsystem, err->code,
380 err->msg_len ? (char *)err->msg : "");
381
382 return 0;
383}
384
Kévin Redon640b2ca2018-10-11 08:41:00 +0200385static struct apdu_context ac; // this will hold the complete APDU (across calls)
386
Kévin Redon378d9832018-10-10 00:39:25 +0200387/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
388static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
389{
Kévin Redon640b2ca2018-10-11 08:41:00 +0200390 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 +0200391 int rc;
392
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200393 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redon378d9832018-10-10 00:39:25 +0200394 osmo_hexdump(data->data, data->data_len));
395
396 rc = apdu_segment_in(&ac, data->data, data->data_len,
Kévin Redon640b2ca2018-10-11 08:41:00 +0200397 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redon378d9832018-10-10 00:39:25 +0200398
Kévin Redon640b2ca2018-10-11 08:41:00 +0200399 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
400 uint8_t* apdu_command = calloc(1, sizeof(ac.hdr) + ac.lc.tot); // to store the APDU command to send
401 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redon378d9832018-10-10 00:39:25 +0200402 if (ac.lc.tot) {
Kévin Redon640b2ca2018-10-11 08:41:00 +0200403 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redon378d9832018-10-10 00:39:25 +0200404 }
Kévin Redon640b2ca2018-10-11 08:41:00 +0200405 // send APDU to card
406 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
407 ipa_client_conn_send_rspro(g_client->bankd_conn, pdu); // send RSPRO packet
408 // the response will come separately
409 free(apdu_command);
410 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
411 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 +0200412 }
413 return 0;
414}
415
416#if 0
417 case SIMTRACE_CMD_DO_ERROR
418 rc = process_do_error(ci, buf, len);
419 break;
420#endif
421
422/*! \brief Process an incoming message from the SIMtrace2 */
423static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
424{
425 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
426 int rc;
427
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200428 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redon378d9832018-10-10 00:39:25 +0200429
430 buf += sizeof(*sh);
431
432 switch (sh->msg_type) {
433 case SIMTRACE_MSGT_BD_CEMU_STATUS:
434 rc = process_do_status(ci, buf, len);
435 break;
436 case SIMTRACE_MSGT_DO_CEMU_PTS:
437 rc = process_do_pts(ci, buf, len);
438 break;
439 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
440 rc = process_do_rx_da(ci, buf, len);
441 break;
442 default:
443 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
444 rc = -1;
445 break;
446 }
447
448 return rc;
449}
450
Kévin Redon378d9832018-10-10 00:39:25 +0200451static void run_mainloop(struct cardem_inst *ci)
452{
453 struct st_transport *transp = ci->slot->transp;
454 unsigned int msg_count, byte_count = 0;
455 uint8_t buf[16*265];
456 int xfer_len;
457 int rc;
458
459 printf("Entering main loop\n");
460
461 while (1) {
462 /* read data from SIMtrace2 device (local or via USB) */
463 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
464 buf, sizeof(buf), &xfer_len, 100);
465 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
466 rc != LIBUSB_ERROR_INTERRUPTED &&
467 rc != LIBUSB_ERROR_IO) {
468 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
469 return;
470 }
471 /* dispatch any incoming data */
472 if (xfer_len > 0) {
Kévin Redon378d9832018-10-10 00:39:25 +0200473 process_usb_msg(ci, buf, xfer_len);
474 msg_count++;
475 byte_count += xfer_len;
476 }
477 // handle remote SIM client fsm
478 // TODO register the USB fd for this select
479 osmo_select_main(true);
480 }
481}
482
483static struct st_transport _transp;
484
485static struct st_slot _slot = {
486 .transp = &_transp,
487 .slot_nr = 0,
488};
489
490struct cardem_inst _ci = {
491 .slot = &_slot,
492};
493
494struct cardem_inst *ci = &_ci;
495
496static void signal_handler(int signal)
497{
498 switch (signal) {
499 case SIGINT:
500 cardem_request_card_insert(ci, false);
501 exit(0);
502 break;
503 default:
504 break;
505 }
506}
507
508/** remsim_client **/
509
510static void push_and_send(struct ipa_client_conn *ipa, struct msgb *msg_tx)
511{
512 ipa_prepend_header_ext(msg_tx, IPAC_PROTO_EXT_RSPRO);
513 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
514 ipa_client_conn_send(ipa, msg_tx);
515 /* msg_tx is now queued and will be freed. */
516}
517
518void ipa_client_conn_send_rspro(struct ipa_client_conn *ipa, RsproPDU_t *rspro)
519{
520 struct msgb *msg = rspro_enc_msg(rspro);
521 OSMO_ASSERT(msg);
522 push_and_send(ipa, msg);
523}
524
Kévin Redon8670fb72018-10-11 08:41:57 +0200525static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu)
526{
527 OSMO_ASSERT(pdu);
528 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
529
530 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
531 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
532 return -1;
533 }
534
535 // save SW to our current APDU context
536 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
537 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon34c0dbe2018-10-11 17:25:58 +0200538 printf("SIMtrace <= SW=0x%02x%02x, len_rx=%d\n", ac.sw[0], ac.sw[1], card2modem->data.size - 2);
Kévin Redon8670fb72018-10-11 08:41:57 +0200539 if (card2modem->data.size > 2) { // send PB and data to modem
540 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
541 }
542 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
543
544 return 0;
545}
546
Kévin Redon378d9832018-10-10 00:39:25 +0200547static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
548{
549 RsproPDU_t *pdu = rspro_dec_msg(msg);
550 if (!pdu) {
551 fprintf(stderr, "Error decoding PDU\n");
552 return -1;
553 }
554
555 switch (pdu->msg.present) {
556 case RsproPDUchoice_PR_connectClientRes:
557 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
558 break;
Kévin Redon8670fb72018-10-11 08:41:57 +0200559 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
560 bankd_handle_tpduCardToModem(bc, pdu);
561 break;
Kévin Redon378d9832018-10-10 00:39:25 +0200562 default:
563 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
564 return -1;
565 }
566
567 return 0;
568}
569
570int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
571{
572 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
573 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
574 struct bankd_client *bc = conn->data;
575 int rc;
576
577 if (msgb_length(msg) < sizeof(*hh))
578 goto invalid;
579 msg->l2h = &hh->data[0];
580 if (hh->proto != IPAC_PROTO_OSMO)
581 goto invalid;
582 if (!he || msgb_l2len(msg) < sizeof(*he))
583 goto invalid;
584 msg->l2h = &he->data[0];
585
586 if (he->proto != IPAC_PROTO_EXT_RSPRO)
587 goto invalid;
588
589 printf("Received RSPRO %s\n", msgb_hexdump(msg));
590
591 rc = bankd_handle_msg(bc, msg);
592
593 return rc;
594
595invalid:
596 msgb_free(msg);
597 return -1;
598}
599
600static const struct log_info_cat default_categories[] = {
601 [DMAIN] = {
602 .name = "DMAIN",
603 .loglevel = LOGL_DEBUG,
604 .enabled = 1,
605 },
606};
607
608static const struct log_info log_info = {
609 .cat = default_categories,
610 .num_cat = ARRAY_SIZE(default_categories),
611};
612
Kévin Redona6e754a2018-10-11 17:30:33 +0200613static void print_welcome(void)
614{
615 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
616 "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
617 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
618}
619
620static void print_help(void)
621{
622 printf( "\t-d\t--bankd-host HOST\n"
623 "\t-p\t--bankd-port PORT\n"
Kévin Redon3443dc62018-10-11 19:13:24 +0200624 "\t-c\t--client-id REMSIM_CLIENT_ID\n"
625 "\t-s\t--slot-nr REMSIM_SLOT_NUMBER\n"
Kévin Redona6e754a2018-10-11 17:30:33 +0200626 "\t-h\t--help\n"
627 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
628 "\t-k\t--keep-running\n"
629 "\t-V\t--usb-vendor\tVENDOR_ID\n"
630 "\t-P\t--usb-product\tPRODUCT_ID\n"
631 "\t-C\t--usb-config\tCONFIG_ID\n"
632 "\t-I\t--usb-interface\tINTERFACE_ID\n"
633 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
634 "\t-A\t--usb-address\tADDRESS\n"
635 "\t-H\t--usb-path\tPATH\n"
636 "\n"
637 );
638}
639
640static const struct option opts[] = {
641 { "bankd-host", 1, 0, 'b' },
642 { "bankd-port", 1, 0, 'p' },
Kévin Redon3443dc62018-10-11 19:13:24 +0200643 { "client-id", 1, 0, 'c' },
644 { "slot-nr", 1, 0, 's' },
Kévin Redona6e754a2018-10-11 17:30:33 +0200645 { "gsmtap-ip", 1, 0, 'i' },
646 { "help", 0, 0, 'h' },
647 { "keep-running", 0, 0, 'k' },
648 { "usb-vendor", 1, 0, 'V' },
649 { "usb-product", 1, 0, 'P' },
650 { "usb-config", 1, 0, 'C' },
651 { "usb-interface", 1, 0, 'I' },
652 { "usb-altsetting", 1, 0, 'S' },
653 { "usb-address", 1, 0, 'A' },
654 { "usb-path", 1, 0, 'H' },
655 { NULL, 0, 0, 0 }
656};
657
Kévin Redon378d9832018-10-10 00:39:25 +0200658int main(int argc, char **argv)
659{
660 struct st_transport *transp = ci->slot->transp;
661 char *gsmtap_host = "127.0.0.1";
662 int rc;
663 int c, ret = 1;
664 int keep_running = 0;
Kévin Redon911d2fb2018-10-11 17:21:13 +0200665 int bankd_port = 9999;
Kévin Redon3443dc62018-10-11 19:13:24 +0200666 int client_id = -1, slot_nr = -1;
Kévin Redon378d9832018-10-10 00:39:25 +0200667 int if_num = 0, vendor_id = -1, product_id = -1;
668 int config_id = -1, altsetting = 0, addr = -1;
Kévin Redon911d2fb2018-10-11 17:21:13 +0200669 char *bankd_host = "127.0.0.1";
Kévin Redon378d9832018-10-10 00:39:25 +0200670 char *path = NULL;
671
672 print_welcome();
673
674 while (1) {
675 int option_index = 0;
676
Kévin Redon3443dc62018-10-11 19:13:24 +0200677 c = getopt_long(argc, argv, "b:p:c:s:hi:V:P:C:I:S:A:H:k", opts, &option_index);
Kévin Redon378d9832018-10-10 00:39:25 +0200678 if (c == -1)
679 break;
680 switch (c) {
Kévin Redon911d2fb2018-10-11 17:21:13 +0200681 case 'b':
682 bankd_host = optarg;
Kévin Redon378d9832018-10-10 00:39:25 +0200683 break;
684 case 'p':
Kévin Redon911d2fb2018-10-11 17:21:13 +0200685 bankd_port = atoi(optarg);
Kévin Redon378d9832018-10-10 00:39:25 +0200686 break;
Kévin Redon3443dc62018-10-11 19:13:24 +0200687 case 'c':
688 client_id = atoi(optarg);
689 break;
690 case 's':
691 slot_nr = atoi(optarg);
692 break;
Kévin Redon378d9832018-10-10 00:39:25 +0200693 case 'h':
694 print_help();
695 exit(0);
696 break;
697 case 'i':
698 gsmtap_host = optarg;
699 break;
700 case 'k':
701 keep_running = 1;
702 break;
703 case 'V':
704 vendor_id = strtol(optarg, NULL, 16);
705 break;
706 case 'P':
707 product_id = strtol(optarg, NULL, 16);
708 break;
709 case 'C':
710 config_id = atoi(optarg);
711 break;
712 case 'I':
713 if_num = atoi(optarg);
714 break;
715 case 'S':
716 altsetting = atoi(optarg);
717 break;
718 case 'A':
719 addr = atoi(optarg);
720 break;
721 case 'H':
722 path = optarg;
723 break;
724 }
725 }
726
Kévin Redon911d2fb2018-10-11 17:21:13 +0200727 if (vendor_id < 0 || product_id < 0) {
Kévin Redon378d9832018-10-10 00:39:25 +0200728 fprintf(stderr, "You have to specify the vendor and product ID\n");
729 goto do_exit;
730 }
731
Kévin Redon3443dc62018-10-11 19:13:24 +0200732 if (client_id < 0 || slot_nr < 0) {
733 fprintf(stderr, "You have to specify the remote SIM client ID and slot number\n");
734 goto do_exit;
735 }
736
Kévin Redon2b0ab4a2018-10-11 17:24:39 +0200737 rc = libusb_init(NULL);
738 if (rc < 0) {
739 fprintf(stderr, "libusb initialization failed\n");
740 goto do_exit;
Kévin Redon378d9832018-10-10 00:39:25 +0200741 }
742
743 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
744 if (!g_gti) {
745 perror("unable to open GSMTAP");
746 goto close_exit;
747 }
748 gsmtap_source_add_sink(g_gti);
749
750 signal(SIGINT, &signal_handler);
751
752 // initialize remote SIM client
753 g_tall_ctx = talloc_named_const(NULL, 0, "global");
754
755 osmo_fsm_register(&remsim_client_bankd_fsm);
756 osmo_fsm_register(&remsim_client_server_fsm);
757
758 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Kévin Redon911d2fb2018-10-11 17:21:13 +0200759 g_client->bankd_host = bankd_host;
760 g_client->bankd_port = bankd_port;
Kévin Redon378d9832018-10-10 00:39:25 +0200761 g_client->own_comp_id.type = ComponentType_remsimClient;
Kévin Redon3443dc62018-10-11 19:13:24 +0200762 g_client->clslot = &(ClientSlot_t){ .clientId = client_id, .slotNr = slot_nr };
Kévin Redon880a6832018-10-11 17:28:25 +0200763 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "simtrace2-remsim-client");
Kévin Redon378d9832018-10-10 00:39:25 +0200764 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client");
765 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION);
766
767 asn_debug = 0;
768 osmo_init_logging2(g_tall_ctx, &log_info);
769
770 if (bankd_conn_fsm_alloc(g_client) < 0) {
771 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
772 exit(1);
773 }
774
775 // connect to SIMtrace2 cardem
776 do {
777 struct usb_interface_match _ifm, *ifm = &_ifm;
778 ifm->vendor = vendor_id;
779 ifm->product = product_id;
780 ifm->configuration = config_id;
781 ifm->interface = if_num;
782 ifm->altsetting = altsetting;
783 ifm->addr = addr;
784 if (path)
785 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
786 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
787 if (!transp->usb_devh) {
788 fprintf(stderr, "can't open USB device\n");
789 goto close_exit;
790 }
791
792 rc = libusb_claim_interface(transp->usb_devh, if_num);
793 if (rc < 0) {
794 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
795 goto close_exit;
796 }
797
798 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
799 &transp->usb_ep.in, &transp->usb_ep.irq_in);
800 if (rc < 0) {
801 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
802 goto close_exit;
803 }
804
Kévin Redond3843e52018-10-11 19:14:00 +0200805 // switch modem SIM port to emulated SIM on OWHW
806 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
807 int modem = -1;
808 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
809 case 0:
810 modem = 1;
811 break;
812 case 1:
813 modem = 2;
814 break;
815 default:
816 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
817 goto close_exit;
818 }
819 //
820 char gpio_path[PATH_MAX];
821 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
822 int connec_st_usim = open(gpio_path, O_WRONLY);
823 if (-1 == connec_st_usim) {
824 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
825 goto close_exit;
826 }
827 if (1 != write(connec_st_usim, "1", 1)) {
828 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
829 goto close_exit;
830 }
831 printf("switched modem %d to emulated USIM\n", modem);
832
833 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
834 int mdm_rst = open(gpio_path, O_WRONLY);
835 if (-1 == mdm_rst) {
836 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
837 goto close_exit;
838 }
839 if (1 != write(mdm_rst, "1", 1)) {
840 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
841 goto close_exit;
842 }
843 sleep(1); // wait a bit to ensure reset is effective
844 if (1 != write(mdm_rst, "0", 1)) {
845 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
846 goto close_exit;
847 }
848 printf("modem %d reset\n", modem);
849 }
850
Kévin Redon378d9832018-10-10 00:39:25 +0200851 /* simulate card-insert to modem (owhw, not qmod) */
852 cardem_request_card_insert(ci, true);
853
854 /* select remote (forwarded) SIM */
855 st_modem_sim_select_remote(ci->slot);
856
857 /* set the ATR */
858 uint8_t real_atr[] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
859 atr_update_csum(real_atr, sizeof(real_atr));
860 cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
861
862 /* select remote (forwarded) SIM */
863 st_modem_reset_pulse(ci->slot, 300);
864
865 run_mainloop(ci);
866 ret = 0;
867
Kévin Redon2b0ab4a2018-10-11 17:24:39 +0200868 libusb_release_interface(transp->usb_devh, 0);
Kévin Redon378d9832018-10-10 00:39:25 +0200869close_exit:
870 if (transp->usb_devh)
871 libusb_close(transp->usb_devh);
872 if (keep_running)
873 sleep(1);
874 } while (keep_running);
875
Kévin Redon2b0ab4a2018-10-11 17:24:39 +0200876 libusb_exit(NULL);
Kévin Redon378d9832018-10-10 00:39:25 +0200877do_exit:
878 return ret;
879}