blob: 3fc5da00aafec90618a3dbd917cb1cb550f62b59 [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,
204 const uint8_t *data, uint8_t data_len_in)
205{
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
509static void push_and_send(struct ipa_client_conn *ipa, struct msgb *msg_tx)
510{
511 ipa_prepend_header_ext(msg_tx, IPAC_PROTO_EXT_RSPRO);
512 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
513 ipa_client_conn_send(ipa, msg_tx);
514 /* msg_tx is now queued and will be freed. */
515}
516
517void ipa_client_conn_send_rspro(struct ipa_client_conn *ipa, RsproPDU_t *rspro)
518{
519 struct msgb *msg = rspro_enc_msg(rspro);
520 OSMO_ASSERT(msg);
521 push_and_send(ipa, msg);
522}
523
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200524static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu)
525{
526 OSMO_ASSERT(pdu);
527 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
528
529 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
530 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
531 return -1;
532 }
533
534 // save SW to our current APDU context
535 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
536 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200537 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 +0200538 if (card2modem->data.size > 2) { // send PB and data to modem
539 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
540 }
541 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
542
543 return 0;
544}
545
Kévin Redone0b837c2018-10-10 00:39:25 +0200546static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
547{
548 RsproPDU_t *pdu = rspro_dec_msg(msg);
549 if (!pdu) {
550 fprintf(stderr, "Error decoding PDU\n");
551 return -1;
552 }
553
554 switch (pdu->msg.present) {
555 case RsproPDUchoice_PR_connectClientRes:
556 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
557 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200558 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
559 bankd_handle_tpduCardToModem(bc, pdu);
560 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200561 default:
562 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
563 return -1;
564 }
565
566 return 0;
567}
568
569int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
570{
571 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
572 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
573 struct bankd_client *bc = conn->data;
574 int rc;
575
576 if (msgb_length(msg) < sizeof(*hh))
577 goto invalid;
578 msg->l2h = &hh->data[0];
579 if (hh->proto != IPAC_PROTO_OSMO)
580 goto invalid;
581 if (!he || msgb_l2len(msg) < sizeof(*he))
582 goto invalid;
583 msg->l2h = &he->data[0];
584
585 if (he->proto != IPAC_PROTO_EXT_RSPRO)
586 goto invalid;
587
588 printf("Received RSPRO %s\n", msgb_hexdump(msg));
589
590 rc = bankd_handle_msg(bc, msg);
591
592 return rc;
593
594invalid:
595 msgb_free(msg);
596 return -1;
597}
598
599static const struct log_info_cat default_categories[] = {
600 [DMAIN] = {
601 .name = "DMAIN",
602 .loglevel = LOGL_DEBUG,
603 .enabled = 1,
604 },
605};
606
607static const struct log_info log_info = {
608 .cat = default_categories,
609 .num_cat = ARRAY_SIZE(default_categories),
610};
611
Kévin Redon3ec265b2018-10-11 17:30:33 +0200612static void print_welcome(void)
613{
614 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
615 "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
616 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
617}
618
619static void print_help(void)
620{
621 printf( "\t-d\t--bankd-host HOST\n"
622 "\t-p\t--bankd-port PORT\n"
Kévin Redonfbca97a2018-10-11 19:13:24 +0200623 "\t-c\t--client-id REMSIM_CLIENT_ID\n"
624 "\t-s\t--slot-nr REMSIM_SLOT_NUMBER\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200625 "\t-h\t--help\n"
626 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
627 "\t-k\t--keep-running\n"
628 "\t-V\t--usb-vendor\tVENDOR_ID\n"
629 "\t-P\t--usb-product\tPRODUCT_ID\n"
630 "\t-C\t--usb-config\tCONFIG_ID\n"
631 "\t-I\t--usb-interface\tINTERFACE_ID\n"
632 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
633 "\t-A\t--usb-address\tADDRESS\n"
634 "\t-H\t--usb-path\tPATH\n"
635 "\n"
636 );
637}
638
639static const struct option opts[] = {
640 { "bankd-host", 1, 0, 'b' },
641 { "bankd-port", 1, 0, 'p' },
Kévin Redonfbca97a2018-10-11 19:13:24 +0200642 { "client-id", 1, 0, 'c' },
643 { "slot-nr", 1, 0, 's' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200644 { "gsmtap-ip", 1, 0, 'i' },
645 { "help", 0, 0, 'h' },
646 { "keep-running", 0, 0, 'k' },
647 { "usb-vendor", 1, 0, 'V' },
648 { "usb-product", 1, 0, 'P' },
649 { "usb-config", 1, 0, 'C' },
650 { "usb-interface", 1, 0, 'I' },
651 { "usb-altsetting", 1, 0, 'S' },
652 { "usb-address", 1, 0, 'A' },
653 { "usb-path", 1, 0, 'H' },
654 { NULL, 0, 0, 0 }
655};
656
Kévin Redone0b837c2018-10-10 00:39:25 +0200657int main(int argc, char **argv)
658{
659 struct st_transport *transp = ci->slot->transp;
660 char *gsmtap_host = "127.0.0.1";
661 int rc;
662 int c, ret = 1;
663 int keep_running = 0;
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200664 int bankd_port = 9999;
Kévin Redonfbca97a2018-10-11 19:13:24 +0200665 int client_id = -1, slot_nr = -1;
Kévin Redone0b837c2018-10-10 00:39:25 +0200666 int if_num = 0, vendor_id = -1, product_id = -1;
667 int config_id = -1, altsetting = 0, addr = -1;
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200668 char *bankd_host = "127.0.0.1";
Kévin Redone0b837c2018-10-10 00:39:25 +0200669 char *path = NULL;
670
671 print_welcome();
672
673 while (1) {
674 int option_index = 0;
675
Kévin Redonfbca97a2018-10-11 19:13:24 +0200676 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 +0200677 if (c == -1)
678 break;
679 switch (c) {
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200680 case 'b':
681 bankd_host = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200682 break;
683 case 'p':
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200684 bankd_port = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200685 break;
Kévin Redonfbca97a2018-10-11 19:13:24 +0200686 case 'c':
687 client_id = atoi(optarg);
688 break;
689 case 's':
690 slot_nr = atoi(optarg);
691 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200692 case 'h':
693 print_help();
694 exit(0);
695 break;
696 case 'i':
697 gsmtap_host = optarg;
698 break;
699 case 'k':
700 keep_running = 1;
701 break;
702 case 'V':
703 vendor_id = strtol(optarg, NULL, 16);
704 break;
705 case 'P':
706 product_id = strtol(optarg, NULL, 16);
707 break;
708 case 'C':
709 config_id = atoi(optarg);
710 break;
711 case 'I':
712 if_num = atoi(optarg);
713 break;
714 case 'S':
715 altsetting = atoi(optarg);
716 break;
717 case 'A':
718 addr = atoi(optarg);
719 break;
720 case 'H':
721 path = optarg;
722 break;
723 }
724 }
725
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200726 if (vendor_id < 0 || product_id < 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200727 fprintf(stderr, "You have to specify the vendor and product ID\n");
728 goto do_exit;
729 }
730
Kévin Redonfbca97a2018-10-11 19:13:24 +0200731 if (client_id < 0 || slot_nr < 0) {
732 fprintf(stderr, "You have to specify the remote SIM client ID and slot number\n");
733 goto do_exit;
734 }
735
Kévin Redon193a8c12018-10-11 17:24:39 +0200736 rc = libusb_init(NULL);
737 if (rc < 0) {
738 fprintf(stderr, "libusb initialization failed\n");
739 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +0200740 }
741
742 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
743 if (!g_gti) {
744 perror("unable to open GSMTAP");
745 goto close_exit;
746 }
747 gsmtap_source_add_sink(g_gti);
748
749 signal(SIGINT, &signal_handler);
750
751 // initialize remote SIM client
752 g_tall_ctx = talloc_named_const(NULL, 0, "global");
753
754 osmo_fsm_register(&remsim_client_bankd_fsm);
755 osmo_fsm_register(&remsim_client_server_fsm);
756
757 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200758 g_client->bankd_host = bankd_host;
759 g_client->bankd_port = bankd_port;
Kévin Redone0b837c2018-10-10 00:39:25 +0200760 g_client->own_comp_id.type = ComponentType_remsimClient;
Kévin Redonfbca97a2018-10-11 19:13:24 +0200761 g_client->clslot = &(ClientSlot_t){ .clientId = client_id, .slotNr = slot_nr };
Kévin Redon19c97ed2018-10-11 17:28:25 +0200762 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "simtrace2-remsim-client");
Kévin Redone0b837c2018-10-10 00:39:25 +0200763 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client");
764 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION);
765
766 asn_debug = 0;
767 osmo_init_logging2(g_tall_ctx, &log_info);
768
769 if (bankd_conn_fsm_alloc(g_client) < 0) {
770 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
771 exit(1);
772 }
773
774 // connect to SIMtrace2 cardem
775 do {
776 struct usb_interface_match _ifm, *ifm = &_ifm;
777 ifm->vendor = vendor_id;
778 ifm->product = product_id;
779 ifm->configuration = config_id;
780 ifm->interface = if_num;
781 ifm->altsetting = altsetting;
782 ifm->addr = addr;
783 if (path)
784 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
785 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
786 if (!transp->usb_devh) {
787 fprintf(stderr, "can't open USB device\n");
788 goto close_exit;
789 }
790
791 rc = libusb_claim_interface(transp->usb_devh, if_num);
792 if (rc < 0) {
793 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
794 goto close_exit;
795 }
796
797 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
798 &transp->usb_ep.in, &transp->usb_ep.irq_in);
799 if (rc < 0) {
800 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
801 goto close_exit;
802 }
803
Kévin Redon3428e412018-10-11 19:14:00 +0200804 // switch modem SIM port to emulated SIM on OWHW
805 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
806 int modem = -1;
807 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
808 case 0:
809 modem = 1;
810 break;
811 case 1:
812 modem = 2;
813 break;
814 default:
815 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
816 goto close_exit;
817 }
818 //
819 char gpio_path[PATH_MAX];
820 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
821 int connec_st_usim = open(gpio_path, O_WRONLY);
822 if (-1 == connec_st_usim) {
823 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
824 goto close_exit;
825 }
826 if (1 != write(connec_st_usim, "1", 1)) {
827 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
828 goto close_exit;
829 }
830 printf("switched modem %d to emulated USIM\n", modem);
831
832 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
833 int mdm_rst = open(gpio_path, O_WRONLY);
834 if (-1 == mdm_rst) {
835 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
836 goto close_exit;
837 }
838 if (1 != write(mdm_rst, "1", 1)) {
839 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
840 goto close_exit;
841 }
842 sleep(1); // wait a bit to ensure reset is effective
843 if (1 != write(mdm_rst, "0", 1)) {
844 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
845 goto close_exit;
846 }
847 printf("modem %d reset\n", modem);
848 }
849
Kévin Redone0b837c2018-10-10 00:39:25 +0200850 /* simulate card-insert to modem (owhw, not qmod) */
851 cardem_request_card_insert(ci, true);
852
853 /* select remote (forwarded) SIM */
854 st_modem_sim_select_remote(ci->slot);
855
856 /* set the ATR */
857 uint8_t real_atr[] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
858 atr_update_csum(real_atr, sizeof(real_atr));
859 cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
860
861 /* select remote (forwarded) SIM */
862 st_modem_reset_pulse(ci->slot, 300);
863
864 run_mainloop(ci);
865 ret = 0;
866
Kévin Redon193a8c12018-10-11 17:24:39 +0200867 libusb_release_interface(transp->usb_devh, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200868close_exit:
869 if (transp->usb_devh)
870 libusb_close(transp->usb_devh);
871 if (keep_running)
872 sleep(1);
873 } while (keep_running);
874
Kévin Redon193a8c12018-10-11 17:24:39 +0200875 libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +0200876do_exit:
877 return ret;
878}