blob: 08323005da74f031dca8d05d6bb49e4f5edc0748 [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"
Harald Welte61d98e92019-03-03 15:43:07 +010018#include "debug.h"
Kévin Redone0b837c2018-10-10 00:39:25 +020019
20#include <unistd.h>
21#include <stdio.h>
Kévin Redon3428e412018-10-11 19:14:00 +020022#include <linux/limits.h>
23#include <sys/stat.h>
24#include <fcntl.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020025#include <signal.h>
26#include <getopt.h>
27
28#include <libusb.h>
29
30#include "simtrace2/libusb_util.h"
31#include "simtrace2/simtrace_prot.h"
Kévin Redon3428e412018-10-11 19:14:00 +020032#include "simtrace2/simtrace_usb.h"
Kévin Redone0b837c2018-10-10 00:39:25 +020033#include "simtrace2/apdu_dispatch.h"
34#include "simtrace2/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 Redone0b837c2018-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 Redon21e31de2018-10-11 17:25:58 +0200129 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redone0b837c2018-10-10 00:39:25 +0200130
Kévin Redon193a8c12018-10-11 17:24:39 +0200131 int xfer_len;
Kévin Redone0b837c2018-10-10 00:39:25 +0200132
Kévin Redon193a8c12018-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 Redone0b837c2018-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 Redon21e31de2018-10-11 17:25:58 +0200192 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redone0b837c2018-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 Redonf120b642018-10-15 19:53:02 +0200205 const uint8_t *data, uint16_t data_len_in)
Kévin Redone0b837c2018-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 Redon21e31de2018-10-11 17:25:58 +0200213 printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb,
Kévin Redone0b837c2018-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 Redon21e31de2018-10-11 17:25:58 +0200237 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redone0b837c2018-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
Kévin Redon206c3d72018-11-12 22:51:37 +0100249// FIXME check if the ATR actually includes a checksum
Kévin Redone0b837c2018-10-10 00:39:25 +0200250static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
251{
252 uint8_t csum = 0;
253 int i;
254
255 for (i = 1; i < atr_len - 1; i++)
256 csum = csum ^ atr[i];
257
258 atr[atr_len-1] = csum;
259}
260
261static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
262{
263 struct msgb *msg = st_msgb_alloc();
264 struct cardemu_usb_msg_set_atr *satr;
265 uint8_t *cur;
266
267 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
268
Kévin Redon21e31de2018-10-11 17:25:58 +0200269 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200270
271 memset(satr, 0, sizeof(*satr));
272 satr->atr_len = atr_len;
273 cur = msgb_put(msg, atr_len);
274 memcpy(cur, atr, atr_len);
275
276 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
277}
278
279/***********************************************************************
280 * Modem Control protocol
281 ***********************************************************************/
282
283static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
284{
285 struct msgb *msg = st_msgb_alloc();
286 struct st_modem_reset *sr ;
287
288 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
289 sr->asserted = asserted;
290 sr->pulse_duration_msec = pulse_ms;
291
292 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
293}
294
295/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
296int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
297{
298 return _modem_reset(slot, 2, duration_ms);
299}
300
301/*! \brief assert the RESET line of the modem */
302int st_modem_reset_active(struct st_slot *slot)
303{
304 return _modem_reset(slot, 1, 0);
305}
306
307/*! \brief de-assert the RESET line of the modem */
308int st_modem_reset_inactive(struct st_slot *slot)
309{
310 return _modem_reset(slot, 0, 0);
311}
312
313static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
314{
315 struct msgb *msg = st_msgb_alloc();
316 struct st_modem_sim_select *ss;
317
318 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
319 ss->remote_sim = remote_sim;
320
321 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
322}
323
324/*! \brief select local (physical) SIM for given slot */
325int st_modem_sim_select_local(struct st_slot *slot)
326{
327 return _modem_sim_select(slot, 0);
328}
329
330/*! \brief select remote (emulated/forwarded) SIM for given slot */
331int st_modem_sim_select_remote(struct st_slot *slot)
332{
333 return _modem_sim_select(slot, 1);
334}
335
336/*! \brief Request slot to send us status information about the modem */
337int st_modem_get_status(struct st_slot *slot)
338{
339 struct msgb *msg = st_msgb_alloc();
340
341 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
342}
343
344
345/***********************************************************************
346 * Incoming Messages
347 ***********************************************************************/
348
349/*! \brief Process a STATUS message from the SIMtrace2 */
350static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
351{
352 struct cardemu_usb_msg_status *status;
353 status = (struct cardemu_usb_msg_status *) buf;
354
Kévin Redon21e31de2018-10-11 17:25:58 +0200355 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200356 status->flags, status->fi, status->di, status->wi,
357 status->waiting_time);
358
359 return 0;
360}
361
362/*! \brief Process a PTS indication message from the SIMtrace2 */
363static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
364{
365 struct cardemu_usb_msg_pts_info *pts;
366 pts = (struct cardemu_usb_msg_pts_info *) buf;
367
Kévin Redon21e31de2018-10-11 17:25:58 +0200368 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redone0b837c2018-10-10 00:39:25 +0200369
370 return 0;
371}
372
373/*! \brief Process a ERROR indication message from the SIMtrace2 */
374static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
375{
376 struct cardemu_usb_msg_error *err;
377 err = (struct cardemu_usb_msg_error *) buf;
378
Kévin Redon21e31de2018-10-11 17:25:58 +0200379 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200380 err->severity, err->subsystem, err->code,
381 err->msg_len ? (char *)err->msg : "");
382
383 return 0;
384}
385
Kévin Redonbc08db52018-10-11 08:41:00 +0200386static struct apdu_context ac; // this will hold the complete APDU (across calls)
387
Kévin Redone0b837c2018-10-10 00:39:25 +0200388/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
389static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
390{
Kévin Redonbc08db52018-10-11 08:41:00 +0200391 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 +0200392 int rc;
393
Kévin Redon21e31de2018-10-11 17:25:58 +0200394 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redone0b837c2018-10-10 00:39:25 +0200395 osmo_hexdump(data->data, data->data_len));
396
397 rc = apdu_segment_in(&ac, data->data, data->data_len,
Kévin Redonbc08db52018-10-11 08:41:00 +0200398 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200399
Kévin Redonbc08db52018-10-11 08:41:00 +0200400 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
401 uint8_t* apdu_command = calloc(1, sizeof(ac.hdr) + ac.lc.tot); // to store the APDU command to send
402 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redone0b837c2018-10-10 00:39:25 +0200403 if (ac.lc.tot) {
Kévin Redonbc08db52018-10-11 08:41:00 +0200404 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redone0b837c2018-10-10 00:39:25 +0200405 }
Kévin Redonbc08db52018-10-11 08:41:00 +0200406 // send APDU to card
407 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
408 ipa_client_conn_send_rspro(g_client->bankd_conn, pdu); // send RSPRO packet
409 // the response will come separately
410 free(apdu_command);
411 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
412 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 +0200413 }
414 return 0;
415}
416
417#if 0
418 case SIMTRACE_CMD_DO_ERROR
419 rc = process_do_error(ci, buf, len);
420 break;
421#endif
422
423/*! \brief Process an incoming message from the SIMtrace2 */
424static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
425{
426 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
427 int rc;
428
Kévin Redon21e31de2018-10-11 17:25:58 +0200429 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200430
431 buf += sizeof(*sh);
432
433 switch (sh->msg_type) {
434 case SIMTRACE_MSGT_BD_CEMU_STATUS:
435 rc = process_do_status(ci, buf, len);
436 break;
437 case SIMTRACE_MSGT_DO_CEMU_PTS:
438 rc = process_do_pts(ci, buf, len);
439 break;
440 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
441 rc = process_do_rx_da(ci, buf, len);
442 break;
443 default:
444 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
445 rc = -1;
446 break;
447 }
448
449 return rc;
450}
451
Kévin Redone0b837c2018-10-10 00:39:25 +0200452static void run_mainloop(struct cardem_inst *ci)
453{
454 struct st_transport *transp = ci->slot->transp;
455 unsigned int msg_count, byte_count = 0;
456 uint8_t buf[16*265];
457 int xfer_len;
458 int rc;
459
460 printf("Entering main loop\n");
461
462 while (1) {
463 /* read data from SIMtrace2 device (local or via USB) */
464 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
465 buf, sizeof(buf), &xfer_len, 100);
466 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
467 rc != LIBUSB_ERROR_INTERRUPTED &&
468 rc != LIBUSB_ERROR_IO) {
Kévin Redon6f074b72018-11-12 22:53:48 +0100469 fprintf(stderr, "BULK IN transfer error: %s\n", libusb_error_name(rc));
Kévin Redone0b837c2018-10-10 00:39:25 +0200470 return;
471 }
472 /* dispatch any incoming data */
473 if (xfer_len > 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200474 process_usb_msg(ci, buf, xfer_len);
475 msg_count++;
476 byte_count += xfer_len;
477 }
478 // handle remote SIM client fsm
479 // TODO register the USB fd for this select
480 osmo_select_main(true);
481 }
482}
483
484static struct st_transport _transp;
485
486static struct st_slot _slot = {
487 .transp = &_transp,
488 .slot_nr = 0,
489};
490
491struct cardem_inst _ci = {
492 .slot = &_slot,
493};
494
495struct cardem_inst *ci = &_ci;
496
497static void signal_handler(int signal)
498{
499 switch (signal) {
500 case SIGINT:
501 cardem_request_card_insert(ci, false);
502 exit(0);
503 break;
504 default:
505 break;
506 }
507}
508
509/** remsim_client **/
510
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200511static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu)
512{
513 OSMO_ASSERT(pdu);
514 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
515
516 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
517 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
518 return -1;
519 }
520
521 // save SW to our current APDU context
522 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
523 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200524 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 +0200525 if (card2modem->data.size > 2) { // send PB and data to modem
526 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
527 }
528 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
529
530 return 0;
531}
532
Kévin Redone0b837c2018-10-10 00:39:25 +0200533static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
534{
535 RsproPDU_t *pdu = rspro_dec_msg(msg);
536 if (!pdu) {
537 fprintf(stderr, "Error decoding PDU\n");
538 return -1;
539 }
540
541 switch (pdu->msg.present) {
542 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +0100543 /* Store 'identity' of bankd to in peer_comp_id */
544 rspro_comp_id_retrieve(&bc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
Kévin Redone0b837c2018-10-10 00:39:25 +0200545 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
546 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200547 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
548 bankd_handle_tpduCardToModem(bc, pdu);
549 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200550 default:
551 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
552 return -1;
553 }
554
555 return 0;
556}
557
558int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
559{
560 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
561 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
562 struct bankd_client *bc = conn->data;
563 int rc;
564
565 if (msgb_length(msg) < sizeof(*hh))
566 goto invalid;
567 msg->l2h = &hh->data[0];
568 if (hh->proto != IPAC_PROTO_OSMO)
569 goto invalid;
570 if (!he || msgb_l2len(msg) < sizeof(*he))
571 goto invalid;
572 msg->l2h = &he->data[0];
573
574 if (he->proto != IPAC_PROTO_EXT_RSPRO)
575 goto invalid;
576
577 printf("Received RSPRO %s\n", msgb_hexdump(msg));
578
579 rc = bankd_handle_msg(bc, msg);
580
581 return rc;
582
583invalid:
584 msgb_free(msg);
585 return -1;
586}
587
Harald Weltee56f2b92019-03-02 17:02:13 +0100588/* handle incoming messages from server */
589static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
590{
591 RsproPDU_t *resp;
592
593 switch (pdu->msg.present) {
594 case RsproPDUchoice_PR_connectClientRes:
595 /* Store 'identity' of server in srvc->peer_comp_id */
596 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
597 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
598 break;
599 case RsproPDUchoice_PR_configClientReq:
600 /* store/set the clientID as instructed by the server */
601 if (!g_client->clslot)
602 g_client->clslot = talloc_zero(g_client, ClientSlot_t);
603 *g_client->clslot = pdu->msg.choice.configClientReq.clientSlot;
604 /* store/set the bankd ip/port as instructed by the server */
605 osmo_talloc_replace_string(g_client, &g_client->bankd_host,
606 rspro_IpAddr2str(&pdu->msg.choice.configClientReq.bankd.ip));
607 g_client->bankd_port = ntohs(pdu->msg.choice.configClientReq.bankd.port);
608 /* instruct bankd FSM to connect */
609 osmo_fsm_inst_dispatch(g_client->bankd_fi, BDC_E_ESTABLISH, NULL);
610 /* send response to server */
611 resp = rspro_gen_ConfigClientRes(ResultCode_ok);
612 ipa_client_conn_send_rspro(srvc->conn, resp);
613 break;
614 default:
615 fprintf(stderr, "Unknown/Unsupported RSPRO PDU type: %u\n", pdu->msg.present);
616 return -1;
617 }
618
619 return 0;
620}
621
622
Kévin Redon3ec265b2018-10-11 17:30:33 +0200623static void print_welcome(void)
624{
625 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
626 "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
627 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
628}
629
630static void print_help(void)
631{
Harald Weltee56f2b92019-03-02 17:02:13 +0100632 printf( "\t-s\t--server-host HOST\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200633 "\t-p\t--bankd-port PORT\n"
634 "\t-h\t--help\n"
635 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
636 "\t-k\t--keep-running\n"
637 "\t-V\t--usb-vendor\tVENDOR_ID\n"
638 "\t-P\t--usb-product\tPRODUCT_ID\n"
639 "\t-C\t--usb-config\tCONFIG_ID\n"
640 "\t-I\t--usb-interface\tINTERFACE_ID\n"
641 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
642 "\t-A\t--usb-address\tADDRESS\n"
643 "\t-H\t--usb-path\tPATH\n"
Kévin Redon206c3d72018-11-12 22:51:37 +0100644 "\t-a\t--atr\tATR\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200645 "\n"
646 );
647}
648
649static const struct option opts[] = {
Harald Weltee56f2b92019-03-02 17:02:13 +0100650 { "server-host", 1, 0, 's' },
651 { "server-port", 1, 0, 'p' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200652 { "gsmtap-ip", 1, 0, 'i' },
653 { "help", 0, 0, 'h' },
654 { "keep-running", 0, 0, 'k' },
655 { "usb-vendor", 1, 0, 'V' },
656 { "usb-product", 1, 0, 'P' },
657 { "usb-config", 1, 0, 'C' },
658 { "usb-interface", 1, 0, 'I' },
659 { "usb-altsetting", 1, 0, 'S' },
660 { "usb-address", 1, 0, 'A' },
661 { "usb-path", 1, 0, 'H' },
Kévin Redon206c3d72018-11-12 22:51:37 +0100662 { "atr", 1, 0, 'a' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200663 { NULL, 0, 0, 0 }
664};
665
Kévin Redone0b837c2018-10-10 00:39:25 +0200666int main(int argc, char **argv)
667{
Harald Weltee56f2b92019-03-02 17:02:13 +0100668 struct rspro_server_conn *srvc;
Kévin Redone0b837c2018-10-10 00:39:25 +0200669 struct st_transport *transp = ci->slot->transp;
670 char *gsmtap_host = "127.0.0.1";
671 int rc;
672 int c, ret = 1;
673 int keep_running = 0;
Harald Weltee56f2b92019-03-02 17:02:13 +0100674 int server_port = 9999;
Kévin Redone0b837c2018-10-10 00:39:25 +0200675 int if_num = 0, vendor_id = -1, product_id = -1;
676 int config_id = -1, altsetting = 0, addr = -1;
Harald Weltee56f2b92019-03-02 17:02:13 +0100677 char *server_host = "127.0.0.1";
Kévin Redone0b837c2018-10-10 00:39:25 +0200678 char *path = NULL;
Kévin Redon206c3d72018-11-12 22:51:37 +0100679 uint8_t atr_data[33] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
680 uint8_t atr_len = 2;
Kévin Redone0b837c2018-10-10 00:39:25 +0200681
682 print_welcome();
683
684 while (1) {
685 int option_index = 0;
686
Harald Weltee56f2b92019-03-02 17:02:13 +0100687 c = getopt_long(argc, argv, "s:p:hi:V:P:C:I:S:A:H:a:k", opts, &option_index);
Kévin Redone0b837c2018-10-10 00:39:25 +0200688 if (c == -1)
689 break;
690 switch (c) {
Harald Weltee56f2b92019-03-02 17:02:13 +0100691 case 's':
692 server_host = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200693 break;
694 case 'p':
Harald Weltee56f2b92019-03-02 17:02:13 +0100695 server_port = atoi(optarg);
Kévin Redonfbca97a2018-10-11 19:13:24 +0200696 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200697 case 'h':
698 print_help();
699 exit(0);
700 break;
701 case 'i':
702 gsmtap_host = optarg;
703 break;
704 case 'k':
705 keep_running = 1;
706 break;
707 case 'V':
708 vendor_id = strtol(optarg, NULL, 16);
709 break;
710 case 'P':
711 product_id = strtol(optarg, NULL, 16);
712 break;
713 case 'C':
714 config_id = atoi(optarg);
715 break;
716 case 'I':
717 if_num = atoi(optarg);
718 break;
719 case 'S':
720 altsetting = atoi(optarg);
721 break;
722 case 'A':
723 addr = atoi(optarg);
724 break;
725 case 'H':
726 path = optarg;
727 break;
Kévin Redon206c3d72018-11-12 22:51:37 +0100728 case 'a':
729 rc = osmo_hexparse(optarg, atr_data, ARRAY_SIZE(atr_data));
730 if (rc < 2 || rc > ARRAY_SIZE(atr_data)) {
731 fprintf(stderr, "ATR matlformed\n");
732 goto do_exit;
733 }
734 atr_len = rc;
735 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200736 }
737 }
738
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200739 if (vendor_id < 0 || product_id < 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200740 fprintf(stderr, "You have to specify the vendor and product ID\n");
741 goto do_exit;
742 }
743
Kévin Redon193a8c12018-10-11 17:24:39 +0200744 rc = libusb_init(NULL);
745 if (rc < 0) {
746 fprintf(stderr, "libusb initialization failed\n");
747 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +0200748 }
749
750 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
751 if (!g_gti) {
752 perror("unable to open GSMTAP");
753 goto close_exit;
754 }
755 gsmtap_source_add_sink(g_gti);
756
757 signal(SIGINT, &signal_handler);
758
759 // initialize remote SIM client
760 g_tall_ctx = talloc_named_const(NULL, 0, "global");
761
Kévin Redone0b837c2018-10-10 00:39:25 +0200762 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +0100763
764 srvc = &g_client->srv_conn;
765 srvc->server_host = server_host;
766 srvc->server_port = server_port;
767 srvc->handle_rx = srvc_handle_rx;
768 srvc->own_comp_id.type = ComponentType_remsimClient;
769 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
770 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
771 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
772 rc = server_conn_fsm_alloc(g_client, srvc);
773 if (rc < 0) {
774 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
775 exit(1);
776 }
Kévin Redone0b837c2018-10-10 00:39:25 +0200777
778 asn_debug = 0;
779 osmo_init_logging2(g_tall_ctx, &log_info);
780
781 if (bankd_conn_fsm_alloc(g_client) < 0) {
782 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
783 exit(1);
784 }
785
786 // connect to SIMtrace2 cardem
787 do {
788 struct usb_interface_match _ifm, *ifm = &_ifm;
789 ifm->vendor = vendor_id;
790 ifm->product = product_id;
791 ifm->configuration = config_id;
792 ifm->interface = if_num;
793 ifm->altsetting = altsetting;
794 ifm->addr = addr;
795 if (path)
796 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
797 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
798 if (!transp->usb_devh) {
799 fprintf(stderr, "can't open USB device\n");
800 goto close_exit;
801 }
802
803 rc = libusb_claim_interface(transp->usb_devh, if_num);
804 if (rc < 0) {
805 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
806 goto close_exit;
807 }
808
809 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
810 &transp->usb_ep.in, &transp->usb_ep.irq_in);
811 if (rc < 0) {
812 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
813 goto close_exit;
814 }
815
Kévin Redon3428e412018-10-11 19:14:00 +0200816 // switch modem SIM port to emulated SIM on OWHW
817 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
818 int modem = -1;
819 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
820 case 0:
821 modem = 1;
822 break;
823 case 1:
824 modem = 2;
825 break;
826 default:
827 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
828 goto close_exit;
829 }
830 //
831 char gpio_path[PATH_MAX];
832 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
833 int connec_st_usim = open(gpio_path, O_WRONLY);
834 if (-1 == connec_st_usim) {
835 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
836 goto close_exit;
837 }
838 if (1 != write(connec_st_usim, "1", 1)) {
839 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
840 goto close_exit;
841 }
842 printf("switched modem %d to emulated USIM\n", modem);
843
844 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
845 int mdm_rst = open(gpio_path, O_WRONLY);
846 if (-1 == mdm_rst) {
847 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
848 goto close_exit;
849 }
850 if (1 != write(mdm_rst, "1", 1)) {
851 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
852 goto close_exit;
853 }
854 sleep(1); // wait a bit to ensure reset is effective
855 if (1 != write(mdm_rst, "0", 1)) {
856 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
857 goto close_exit;
858 }
859 printf("modem %d reset\n", modem);
860 }
861
Kévin Redone0b837c2018-10-10 00:39:25 +0200862 /* simulate card-insert to modem (owhw, not qmod) */
863 cardem_request_card_insert(ci, true);
864
865 /* select remote (forwarded) SIM */
866 st_modem_sim_select_remote(ci->slot);
867
868 /* set the ATR */
Kévin Redon206c3d72018-11-12 22:51:37 +0100869 //atr_update_csum(real_atr, sizeof(real_atr));
870 cardem_request_set_atr(ci, atr_data, atr_len);
Kévin Redone0b837c2018-10-10 00:39:25 +0200871
872 /* select remote (forwarded) SIM */
873 st_modem_reset_pulse(ci->slot, 300);
874
875 run_mainloop(ci);
876 ret = 0;
877
Kévin Redon193a8c12018-10-11 17:24:39 +0200878 libusb_release_interface(transp->usb_devh, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200879close_exit:
880 if (transp->usb_devh)
881 libusb_close(transp->usb_devh);
882 if (keep_running)
883 sleep(1);
884 } while (keep_running);
885
Kévin Redon193a8c12018-10-11 17:24:39 +0200886 libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +0200887do_exit:
888 return ret;
889}