blob: 365acb434763935c0b10af0c047a652fdfd4eefa [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>
Kévin Redon3428e412018-10-11 19:14:00 +020021#include <linux/limits.h>
22#include <sys/stat.h>
23#include <fcntl.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020024#include <signal.h>
25#include <getopt.h>
26
27#include <libusb.h>
28
29#include "simtrace2/libusb_util.h"
30#include "simtrace2/simtrace_prot.h"
Kévin Redon3428e412018-10-11 19:14:00 +020031#include "simtrace2/simtrace_usb.h"
Kévin Redone0b837c2018-10-10 00:39:25 +020032#include "simtrace2/apdu_dispatch.h"
33#include "simtrace2/simtrace2-discovery.h"
34
35#include <osmocom/core/gsmtap.h>
36#include <osmocom/core/gsmtap_util.h>
37#include <osmocom/core/utils.h>
38#include <osmocom/core/socket.h>
39#include <osmocom/core/msgb.h>
40#include <osmocom/sim/class_tables.h>
41#include <osmocom/sim/sim.h>
42
43/* transport to a SIMtrace device */
44struct st_transport {
45 /* USB */
46 struct libusb_device_handle *usb_devh;
47 struct {
48 uint8_t in;
49 uint8_t out;
50 uint8_t irq_in;
51 } usb_ep;
Kévin Redone0b837c2018-10-10 00:39:25 +020052};
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
Kévin Redon21e31de2018-10-11 17:25:58 +0200128 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redone0b837c2018-10-10 00:39:25 +0200129
Kévin Redon193a8c12018-10-11 17:24:39 +0200130 int xfer_len;
Kévin Redone0b837c2018-10-10 00:39:25 +0200131
Kévin Redon193a8c12018-10-11 17:24:39 +0200132 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
133 msgb_data(msg), msgb_length(msg),
134 &xfer_len, 100000);
Kévin Redone0b837c2018-10-10 00:39:25 +0200135
136 msgb_free(msg);
137 return rc;
138}
139
140static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
141 uint8_t slot_nr)
142{
143 struct simtrace_msg_hdr *sh;
144
145 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
146 memset(sh, 0, sizeof(*sh));
147 sh->msg_class = msg_class;
148 sh->msg_type = msg_type;
149 sh->slot_nr = slot_nr;
150 sh->msg_len = msgb_length(msg);
151
152 return sh;
153}
154
155/* transmit a given message to a specified slot. Expects all headers
156 * present before calling the function */
157int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
158 uint8_t msg_class, uint8_t msg_type)
159{
160 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
161
162 return st_transp_tx_msg(slot->transp, msg);
163}
164
165/***********************************************************************
166 * Card Emulation protocol
167 ***********************************************************************/
168
169
170/*! \brief Request the SIMtrace2 to generate a card-insert signal */
171static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
172{
173 struct msgb *msg = st_msgb_alloc();
174 struct cardemu_usb_msg_cardinsert *cins;
175
176 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
177 memset(cins, 0, sizeof(*cins));
178 if (inserted)
179 cins->card_insert = 1;
180
181 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
182}
183
184/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
185static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
186{
187 struct msgb *msg = st_msgb_alloc();
188 struct cardemu_usb_msg_tx_data *txd;
189 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
190
Kévin Redon21e31de2018-10-11 17:25:58 +0200191 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redone0b837c2018-10-10 00:39:25 +0200192
193 memset(txd, 0, sizeof(*txd));
194 txd->data_len = 1;
195 txd->flags = CEMU_DATA_F_PB_AND_RX;
196 /* one data byte */
197 msgb_put_u8(msg, pb);
198
199 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
200}
201
202/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
203static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
Kévin Redonf120b642018-10-15 19:53:02 +0200204 const uint8_t *data, uint16_t data_len_in)
Kévin Redone0b837c2018-10-10 00:39:25 +0200205{
206 struct msgb *msg = st_msgb_alloc();
207 struct cardemu_usb_msg_tx_data *txd;
208 uint8_t *cur;
209
210 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
211
Kévin Redon21e31de2018-10-11 17:25:58 +0200212 printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb,
Kévin Redone0b837c2018-10-10 00:39:25 +0200213 osmo_hexdump(data, data_len_in), data_len_in);
214
215 memset(txd, 0, sizeof(*txd));
216 txd->data_len = 1 + data_len_in;
217 txd->flags = CEMU_DATA_F_PB_AND_TX;
218 /* procedure byte */
219 msgb_put_u8(msg, pb);
220 /* data */
221 cur = msgb_put(msg, data_len_in);
222 memcpy(cur, data, data_len_in);
223
224 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
225}
226
227/*! \brief Request the SIMtrace2 to send a Status Word */
228static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
229{
230 struct msgb *msg = st_msgb_alloc();
231 struct cardemu_usb_msg_tx_data *txd;
232 uint8_t *cur;
233
234 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
235
Kévin Redon21e31de2018-10-11 17:25:58 +0200236 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redone0b837c2018-10-10 00:39:25 +0200237
238 memset(txd, 0, sizeof(*txd));
239 txd->data_len = 2;
240 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
241 cur = msgb_put(msg, 2);
242 cur[0] = sw[0];
243 cur[1] = sw[1];
244
245 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
246}
247
248static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
249{
250 uint8_t csum = 0;
251 int i;
252
253 for (i = 1; i < atr_len - 1; i++)
254 csum = csum ^ atr[i];
255
256 atr[atr_len-1] = csum;
257}
258
259static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
260{
261 struct msgb *msg = st_msgb_alloc();
262 struct cardemu_usb_msg_set_atr *satr;
263 uint8_t *cur;
264
265 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
266
Kévin Redon21e31de2018-10-11 17:25:58 +0200267 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200268
269 memset(satr, 0, sizeof(*satr));
270 satr->atr_len = atr_len;
271 cur = msgb_put(msg, atr_len);
272 memcpy(cur, atr, atr_len);
273
274 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
275}
276
277/***********************************************************************
278 * Modem Control protocol
279 ***********************************************************************/
280
281static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
282{
283 struct msgb *msg = st_msgb_alloc();
284 struct st_modem_reset *sr ;
285
286 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
287 sr->asserted = asserted;
288 sr->pulse_duration_msec = pulse_ms;
289
290 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
291}
292
293/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
294int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
295{
296 return _modem_reset(slot, 2, duration_ms);
297}
298
299/*! \brief assert the RESET line of the modem */
300int st_modem_reset_active(struct st_slot *slot)
301{
302 return _modem_reset(slot, 1, 0);
303}
304
305/*! \brief de-assert the RESET line of the modem */
306int st_modem_reset_inactive(struct st_slot *slot)
307{
308 return _modem_reset(slot, 0, 0);
309}
310
311static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
312{
313 struct msgb *msg = st_msgb_alloc();
314 struct st_modem_sim_select *ss;
315
316 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
317 ss->remote_sim = remote_sim;
318
319 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
320}
321
322/*! \brief select local (physical) SIM for given slot */
323int st_modem_sim_select_local(struct st_slot *slot)
324{
325 return _modem_sim_select(slot, 0);
326}
327
328/*! \brief select remote (emulated/forwarded) SIM for given slot */
329int st_modem_sim_select_remote(struct st_slot *slot)
330{
331 return _modem_sim_select(slot, 1);
332}
333
334/*! \brief Request slot to send us status information about the modem */
335int st_modem_get_status(struct st_slot *slot)
336{
337 struct msgb *msg = st_msgb_alloc();
338
339 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
340}
341
342
343/***********************************************************************
344 * Incoming Messages
345 ***********************************************************************/
346
347/*! \brief Process a STATUS message from the SIMtrace2 */
348static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
349{
350 struct cardemu_usb_msg_status *status;
351 status = (struct cardemu_usb_msg_status *) buf;
352
Kévin Redon21e31de2018-10-11 17:25:58 +0200353 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200354 status->flags, status->fi, status->di, status->wi,
355 status->waiting_time);
356
357 return 0;
358}
359
360/*! \brief Process a PTS indication message from the SIMtrace2 */
361static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
362{
363 struct cardemu_usb_msg_pts_info *pts;
364 pts = (struct cardemu_usb_msg_pts_info *) buf;
365
Kévin Redon21e31de2018-10-11 17:25:58 +0200366 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redone0b837c2018-10-10 00:39:25 +0200367
368 return 0;
369}
370
371/*! \brief Process a ERROR indication message from the SIMtrace2 */
372static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
373{
374 struct cardemu_usb_msg_error *err;
375 err = (struct cardemu_usb_msg_error *) buf;
376
Kévin Redon21e31de2018-10-11 17:25:58 +0200377 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200378 err->severity, err->subsystem, err->code,
379 err->msg_len ? (char *)err->msg : "");
380
381 return 0;
382}
383
Kévin Redonbc08db52018-10-11 08:41:00 +0200384static struct apdu_context ac; // this will hold the complete APDU (across calls)
385
Kévin Redone0b837c2018-10-10 00:39:25 +0200386/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
387static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
388{
Kévin Redonbc08db52018-10-11 08:41:00 +0200389 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 +0200390 int rc;
391
Kévin Redon21e31de2018-10-11 17:25:58 +0200392 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redone0b837c2018-10-10 00:39:25 +0200393 osmo_hexdump(data->data, data->data_len));
394
395 rc = apdu_segment_in(&ac, data->data, data->data_len,
Kévin Redonbc08db52018-10-11 08:41:00 +0200396 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200397
Kévin Redonbc08db52018-10-11 08:41:00 +0200398 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
399 uint8_t* apdu_command = calloc(1, sizeof(ac.hdr) + ac.lc.tot); // to store the APDU command to send
400 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redone0b837c2018-10-10 00:39:25 +0200401 if (ac.lc.tot) {
Kévin Redonbc08db52018-10-11 08:41:00 +0200402 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redone0b837c2018-10-10 00:39:25 +0200403 }
Kévin Redonbc08db52018-10-11 08:41:00 +0200404 // send APDU to card
405 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
406 ipa_client_conn_send_rspro(g_client->bankd_conn, pdu); // send RSPRO packet
407 // the response will come separately
408 free(apdu_command);
409 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
410 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 +0200411 }
412 return 0;
413}
414
415#if 0
416 case SIMTRACE_CMD_DO_ERROR
417 rc = process_do_error(ci, buf, len);
418 break;
419#endif
420
421/*! \brief Process an incoming message from the SIMtrace2 */
422static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
423{
424 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
425 int rc;
426
Kévin Redon21e31de2018-10-11 17:25:58 +0200427 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200428
429 buf += sizeof(*sh);
430
431 switch (sh->msg_type) {
432 case SIMTRACE_MSGT_BD_CEMU_STATUS:
433 rc = process_do_status(ci, buf, len);
434 break;
435 case SIMTRACE_MSGT_DO_CEMU_PTS:
436 rc = process_do_pts(ci, buf, len);
437 break;
438 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
439 rc = process_do_rx_da(ci, buf, len);
440 break;
441 default:
442 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
443 rc = -1;
444 break;
445 }
446
447 return rc;
448}
449
Kévin Redone0b837c2018-10-10 00:39:25 +0200450static void run_mainloop(struct cardem_inst *ci)
451{
452 struct st_transport *transp = ci->slot->transp;
453 unsigned int msg_count, byte_count = 0;
454 uint8_t buf[16*265];
455 int xfer_len;
456 int rc;
457
458 printf("Entering main loop\n");
459
460 while (1) {
461 /* read data from SIMtrace2 device (local or via USB) */
462 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
463 buf, sizeof(buf), &xfer_len, 100);
464 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
465 rc != LIBUSB_ERROR_INTERRUPTED &&
466 rc != LIBUSB_ERROR_IO) {
467 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
468 return;
469 }
470 /* dispatch any incoming data */
471 if (xfer_len > 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200472 process_usb_msg(ci, buf, xfer_len);
473 msg_count++;
474 byte_count += xfer_len;
475 }
476 // handle remote SIM client fsm
477 // TODO register the USB fd for this select
478 osmo_select_main(true);
479 }
480}
481
482static struct st_transport _transp;
483
484static struct st_slot _slot = {
485 .transp = &_transp,
486 .slot_nr = 0,
487};
488
489struct cardem_inst _ci = {
490 .slot = &_slot,
491};
492
493struct cardem_inst *ci = &_ci;
494
495static void signal_handler(int signal)
496{
497 switch (signal) {
498 case SIGINT:
499 cardem_request_card_insert(ci, false);
500 exit(0);
501 break;
502 default:
503 break;
504 }
505}
506
507/** remsim_client **/
508
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200509static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu)
510{
511 OSMO_ASSERT(pdu);
512 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
513
514 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
515 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
516 return -1;
517 }
518
519 // save SW to our current APDU context
520 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
521 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200522 printf("SIMtrace <= SW=0x%02x%02x, len_rx=%d\n", ac.sw[0], ac.sw[1], card2modem->data.size - 2);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200523 if (card2modem->data.size > 2) { // send PB and data to modem
524 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
525 }
526 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
527
528 return 0;
529}
530
Kévin Redone0b837c2018-10-10 00:39:25 +0200531static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
532{
533 RsproPDU_t *pdu = rspro_dec_msg(msg);
534 if (!pdu) {
535 fprintf(stderr, "Error decoding PDU\n");
536 return -1;
537 }
538
539 switch (pdu->msg.present) {
540 case RsproPDUchoice_PR_connectClientRes:
541 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
542 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200543 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
544 bankd_handle_tpduCardToModem(bc, pdu);
545 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200546 default:
547 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
548 return -1;
549 }
550
551 return 0;
552}
553
554int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
555{
556 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
557 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
558 struct bankd_client *bc = conn->data;
559 int rc;
560
561 if (msgb_length(msg) < sizeof(*hh))
562 goto invalid;
563 msg->l2h = &hh->data[0];
564 if (hh->proto != IPAC_PROTO_OSMO)
565 goto invalid;
566 if (!he || msgb_l2len(msg) < sizeof(*he))
567 goto invalid;
568 msg->l2h = &he->data[0];
569
570 if (he->proto != IPAC_PROTO_EXT_RSPRO)
571 goto invalid;
572
573 printf("Received RSPRO %s\n", msgb_hexdump(msg));
574
575 rc = bankd_handle_msg(bc, msg);
576
577 return rc;
578
579invalid:
580 msgb_free(msg);
581 return -1;
582}
583
584static const struct log_info_cat default_categories[] = {
585 [DMAIN] = {
586 .name = "DMAIN",
587 .loglevel = LOGL_DEBUG,
588 .enabled = 1,
589 },
590};
591
592static const struct log_info log_info = {
593 .cat = default_categories,
594 .num_cat = ARRAY_SIZE(default_categories),
595};
596
Kévin Redon3ec265b2018-10-11 17:30:33 +0200597static void print_welcome(void)
598{
599 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
600 "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
601 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
602}
603
604static void print_help(void)
605{
606 printf( "\t-d\t--bankd-host HOST\n"
607 "\t-p\t--bankd-port PORT\n"
Kévin Redonfbca97a2018-10-11 19:13:24 +0200608 "\t-c\t--client-id REMSIM_CLIENT_ID\n"
609 "\t-s\t--slot-nr REMSIM_SLOT_NUMBER\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200610 "\t-h\t--help\n"
611 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
612 "\t-k\t--keep-running\n"
613 "\t-V\t--usb-vendor\tVENDOR_ID\n"
614 "\t-P\t--usb-product\tPRODUCT_ID\n"
615 "\t-C\t--usb-config\tCONFIG_ID\n"
616 "\t-I\t--usb-interface\tINTERFACE_ID\n"
617 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
618 "\t-A\t--usb-address\tADDRESS\n"
619 "\t-H\t--usb-path\tPATH\n"
620 "\n"
621 );
622}
623
624static const struct option opts[] = {
625 { "bankd-host", 1, 0, 'b' },
626 { "bankd-port", 1, 0, 'p' },
Kévin Redonfbca97a2018-10-11 19:13:24 +0200627 { "client-id", 1, 0, 'c' },
628 { "slot-nr", 1, 0, 's' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200629 { "gsmtap-ip", 1, 0, 'i' },
630 { "help", 0, 0, 'h' },
631 { "keep-running", 0, 0, 'k' },
632 { "usb-vendor", 1, 0, 'V' },
633 { "usb-product", 1, 0, 'P' },
634 { "usb-config", 1, 0, 'C' },
635 { "usb-interface", 1, 0, 'I' },
636 { "usb-altsetting", 1, 0, 'S' },
637 { "usb-address", 1, 0, 'A' },
638 { "usb-path", 1, 0, 'H' },
639 { NULL, 0, 0, 0 }
640};
641
Kévin Redone0b837c2018-10-10 00:39:25 +0200642int main(int argc, char **argv)
643{
644 struct st_transport *transp = ci->slot->transp;
645 char *gsmtap_host = "127.0.0.1";
646 int rc;
647 int c, ret = 1;
648 int keep_running = 0;
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200649 int bankd_port = 9999;
Kévin Redonfbca97a2018-10-11 19:13:24 +0200650 int client_id = -1, slot_nr = -1;
Kévin Redone0b837c2018-10-10 00:39:25 +0200651 int if_num = 0, vendor_id = -1, product_id = -1;
652 int config_id = -1, altsetting = 0, addr = -1;
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200653 char *bankd_host = "127.0.0.1";
Kévin Redone0b837c2018-10-10 00:39:25 +0200654 char *path = NULL;
655
656 print_welcome();
657
658 while (1) {
659 int option_index = 0;
660
Kévin Redonfbca97a2018-10-11 19:13:24 +0200661 c = getopt_long(argc, argv, "b:p:c:s:hi:V:P:C:I:S:A:H:k", opts, &option_index);
Kévin Redone0b837c2018-10-10 00:39:25 +0200662 if (c == -1)
663 break;
664 switch (c) {
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200665 case 'b':
666 bankd_host = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200667 break;
668 case 'p':
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200669 bankd_port = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200670 break;
Kévin Redonfbca97a2018-10-11 19:13:24 +0200671 case 'c':
672 client_id = atoi(optarg);
673 break;
674 case 's':
675 slot_nr = atoi(optarg);
676 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200677 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
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200711 if (vendor_id < 0 || product_id < 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200712 fprintf(stderr, "You have to specify the vendor and product ID\n");
713 goto do_exit;
714 }
715
Kévin Redonfbca97a2018-10-11 19:13:24 +0200716 if (client_id < 0 || slot_nr < 0) {
717 fprintf(stderr, "You have to specify the remote SIM client ID and slot number\n");
718 goto do_exit;
719 }
720
Kévin Redon193a8c12018-10-11 17:24:39 +0200721 rc = libusb_init(NULL);
722 if (rc < 0) {
723 fprintf(stderr, "libusb initialization failed\n");
724 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +0200725 }
726
727 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
728 if (!g_gti) {
729 perror("unable to open GSMTAP");
730 goto close_exit;
731 }
732 gsmtap_source_add_sink(g_gti);
733
734 signal(SIGINT, &signal_handler);
735
736 // initialize remote SIM client
737 g_tall_ctx = talloc_named_const(NULL, 0, "global");
738
739 osmo_fsm_register(&remsim_client_bankd_fsm);
740 osmo_fsm_register(&remsim_client_server_fsm);
741
742 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200743 g_client->bankd_host = bankd_host;
744 g_client->bankd_port = bankd_port;
Kévin Redone0b837c2018-10-10 00:39:25 +0200745 g_client->own_comp_id.type = ComponentType_remsimClient;
Kévin Redonfbca97a2018-10-11 19:13:24 +0200746 g_client->clslot = &(ClientSlot_t){ .clientId = client_id, .slotNr = slot_nr };
Kévin Redon19c97ed2018-10-11 17:28:25 +0200747 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "simtrace2-remsim-client");
Kévin Redone0b837c2018-10-10 00:39:25 +0200748 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client");
749 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION);
750
751 asn_debug = 0;
752 osmo_init_logging2(g_tall_ctx, &log_info);
753
754 if (bankd_conn_fsm_alloc(g_client) < 0) {
755 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
756 exit(1);
757 }
758
759 // connect to SIMtrace2 cardem
760 do {
761 struct usb_interface_match _ifm, *ifm = &_ifm;
762 ifm->vendor = vendor_id;
763 ifm->product = product_id;
764 ifm->configuration = config_id;
765 ifm->interface = if_num;
766 ifm->altsetting = altsetting;
767 ifm->addr = addr;
768 if (path)
769 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
770 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
771 if (!transp->usb_devh) {
772 fprintf(stderr, "can't open USB device\n");
773 goto close_exit;
774 }
775
776 rc = libusb_claim_interface(transp->usb_devh, if_num);
777 if (rc < 0) {
778 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
779 goto close_exit;
780 }
781
782 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
783 &transp->usb_ep.in, &transp->usb_ep.irq_in);
784 if (rc < 0) {
785 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
786 goto close_exit;
787 }
788
Kévin Redon3428e412018-10-11 19:14:00 +0200789 // switch modem SIM port to emulated SIM on OWHW
790 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
791 int modem = -1;
792 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
793 case 0:
794 modem = 1;
795 break;
796 case 1:
797 modem = 2;
798 break;
799 default:
800 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
801 goto close_exit;
802 }
803 //
804 char gpio_path[PATH_MAX];
805 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
806 int connec_st_usim = open(gpio_path, O_WRONLY);
807 if (-1 == connec_st_usim) {
808 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
809 goto close_exit;
810 }
811 if (1 != write(connec_st_usim, "1", 1)) {
812 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
813 goto close_exit;
814 }
815 printf("switched modem %d to emulated USIM\n", modem);
816
817 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
818 int mdm_rst = open(gpio_path, O_WRONLY);
819 if (-1 == mdm_rst) {
820 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
821 goto close_exit;
822 }
823 if (1 != write(mdm_rst, "1", 1)) {
824 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
825 goto close_exit;
826 }
827 sleep(1); // wait a bit to ensure reset is effective
828 if (1 != write(mdm_rst, "0", 1)) {
829 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
830 goto close_exit;
831 }
832 printf("modem %d reset\n", modem);
833 }
834
Kévin Redone0b837c2018-10-10 00:39:25 +0200835 /* simulate card-insert to modem (owhw, not qmod) */
836 cardem_request_card_insert(ci, true);
837
838 /* select remote (forwarded) SIM */
839 st_modem_sim_select_remote(ci->slot);
840
841 /* set the ATR */
842 uint8_t real_atr[] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
843 atr_update_csum(real_atr, sizeof(real_atr));
844 cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
845
846 /* select remote (forwarded) SIM */
847 st_modem_reset_pulse(ci->slot, 300);
848
849 run_mainloop(ci);
850 ret = 0;
851
Kévin Redon193a8c12018-10-11 17:24:39 +0200852 libusb_release_interface(transp->usb_devh, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200853close_exit:
854 if (transp->usb_devh)
855 libusb_close(transp->usb_devh);
856 if (keep_running)
857 sleep(1);
858 } while (keep_running);
859
Kévin Redon193a8c12018-10-11 17:24:39 +0200860 libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +0200861do_exit:
862 return ret;
863}