blob: 79da419036c155db7c3aeee254731f34b08fdf31 [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
Kévin Redon206c3d72018-11-12 22:51:37 +0100248// FIXME check if the ATR actually includes a checksum
Kévin Redone0b837c2018-10-10 00:39:25 +0200249static 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 Redon21e31de2018-10-11 17:25:58 +0200268 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redone0b837c2018-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 Redon21e31de2018-10-11 17:25:58 +0200354 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redone0b837c2018-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 Redon21e31de2018-10-11 17:25:58 +0200367 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redone0b837c2018-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 Redon21e31de2018-10-11 17:25:58 +0200378 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redone0b837c2018-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 Redonbc08db52018-10-11 08:41:00 +0200385static struct apdu_context ac; // this will hold the complete APDU (across calls)
386
Kévin Redone0b837c2018-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 Redonbc08db52018-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 Redone0b837c2018-10-10 00:39:25 +0200391 int rc;
392
Kévin Redon21e31de2018-10-11 17:25:58 +0200393 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redone0b837c2018-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 Redonbc08db52018-10-11 08:41:00 +0200397 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200398
Kévin Redonbc08db52018-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 Redone0b837c2018-10-10 00:39:25 +0200402 if (ac.lc.tot) {
Kévin Redonbc08db52018-10-11 08:41:00 +0200403 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redone0b837c2018-10-10 00:39:25 +0200404 }
Kévin Redonbc08db52018-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 Redone0b837c2018-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 Redon21e31de2018-10-11 17:25:58 +0200428 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redone0b837c2018-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 Redone0b837c2018-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) {
Kévin Redon6f074b72018-11-12 22:53:48 +0100468 fprintf(stderr, "BULK IN transfer error: %s\n", libusb_error_name(rc));
Kévin Redone0b837c2018-10-10 00:39:25 +0200469 return;
470 }
471 /* dispatch any incoming data */
472 if (xfer_len > 0) {
Kévin Redone0b837c2018-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
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200510static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu)
511{
512 OSMO_ASSERT(pdu);
513 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
514
515 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
516 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
517 return -1;
518 }
519
520 // save SW to our current APDU context
521 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
522 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200523 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 +0200524 if (card2modem->data.size > 2) { // send PB and data to modem
525 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
526 }
527 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
528
529 return 0;
530}
531
Kévin Redone0b837c2018-10-10 00:39:25 +0200532static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
533{
534 RsproPDU_t *pdu = rspro_dec_msg(msg);
535 if (!pdu) {
536 fprintf(stderr, "Error decoding PDU\n");
537 return -1;
538 }
539
540 switch (pdu->msg.present) {
541 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +0100542 /* Store 'identity' of bankd to in peer_comp_id */
543 rspro_comp_id_retrieve(&bc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
Kévin Redone0b837c2018-10-10 00:39:25 +0200544 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
545 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200546 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
547 bankd_handle_tpduCardToModem(bc, pdu);
548 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200549 default:
550 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
551 return -1;
552 }
553
554 return 0;
555}
556
557int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
558{
559 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
560 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
561 struct bankd_client *bc = conn->data;
562 int rc;
563
564 if (msgb_length(msg) < sizeof(*hh))
565 goto invalid;
566 msg->l2h = &hh->data[0];
567 if (hh->proto != IPAC_PROTO_OSMO)
568 goto invalid;
569 if (!he || msgb_l2len(msg) < sizeof(*he))
570 goto invalid;
571 msg->l2h = &he->data[0];
572
573 if (he->proto != IPAC_PROTO_EXT_RSPRO)
574 goto invalid;
575
576 printf("Received RSPRO %s\n", msgb_hexdump(msg));
577
578 rc = bankd_handle_msg(bc, msg);
579
580 return rc;
581
582invalid:
583 msgb_free(msg);
584 return -1;
585}
586
Harald Weltee56f2b92019-03-02 17:02:13 +0100587/* handle incoming messages from server */
588static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
589{
590 RsproPDU_t *resp;
591
592 switch (pdu->msg.present) {
593 case RsproPDUchoice_PR_connectClientRes:
594 /* Store 'identity' of server in srvc->peer_comp_id */
595 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
596 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
597 break;
598 case RsproPDUchoice_PR_configClientReq:
599 /* store/set the clientID as instructed by the server */
600 if (!g_client->clslot)
601 g_client->clslot = talloc_zero(g_client, ClientSlot_t);
602 *g_client->clslot = pdu->msg.choice.configClientReq.clientSlot;
603 /* store/set the bankd ip/port as instructed by the server */
604 osmo_talloc_replace_string(g_client, &g_client->bankd_host,
605 rspro_IpAddr2str(&pdu->msg.choice.configClientReq.bankd.ip));
606 g_client->bankd_port = ntohs(pdu->msg.choice.configClientReq.bankd.port);
607 /* instruct bankd FSM to connect */
608 osmo_fsm_inst_dispatch(g_client->bankd_fi, BDC_E_ESTABLISH, NULL);
609 /* send response to server */
610 resp = rspro_gen_ConfigClientRes(ResultCode_ok);
611 ipa_client_conn_send_rspro(srvc->conn, resp);
612 break;
613 default:
614 fprintf(stderr, "Unknown/Unsupported RSPRO PDU type: %u\n", pdu->msg.present);
615 return -1;
616 }
617
618 return 0;
619}
620
621
Kévin Redone0b837c2018-10-10 00:39:25 +0200622static const struct log_info_cat default_categories[] = {
623 [DMAIN] = {
624 .name = "DMAIN",
625 .loglevel = LOGL_DEBUG,
626 .enabled = 1,
627 },
628};
629
630static const struct log_info log_info = {
631 .cat = default_categories,
632 .num_cat = ARRAY_SIZE(default_categories),
633};
634
Kévin Redon3ec265b2018-10-11 17:30:33 +0200635static void print_welcome(void)
636{
637 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
638 "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
639 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
640}
641
642static void print_help(void)
643{
Harald Weltee56f2b92019-03-02 17:02:13 +0100644 printf( "\t-s\t--server-host HOST\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200645 "\t-p\t--bankd-port PORT\n"
646 "\t-h\t--help\n"
647 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
648 "\t-k\t--keep-running\n"
649 "\t-V\t--usb-vendor\tVENDOR_ID\n"
650 "\t-P\t--usb-product\tPRODUCT_ID\n"
651 "\t-C\t--usb-config\tCONFIG_ID\n"
652 "\t-I\t--usb-interface\tINTERFACE_ID\n"
653 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
654 "\t-A\t--usb-address\tADDRESS\n"
655 "\t-H\t--usb-path\tPATH\n"
Kévin Redon206c3d72018-11-12 22:51:37 +0100656 "\t-a\t--atr\tATR\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200657 "\n"
658 );
659}
660
661static const struct option opts[] = {
Harald Weltee56f2b92019-03-02 17:02:13 +0100662 { "server-host", 1, 0, 's' },
663 { "server-port", 1, 0, 'p' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200664 { "gsmtap-ip", 1, 0, 'i' },
665 { "help", 0, 0, 'h' },
666 { "keep-running", 0, 0, 'k' },
667 { "usb-vendor", 1, 0, 'V' },
668 { "usb-product", 1, 0, 'P' },
669 { "usb-config", 1, 0, 'C' },
670 { "usb-interface", 1, 0, 'I' },
671 { "usb-altsetting", 1, 0, 'S' },
672 { "usb-address", 1, 0, 'A' },
673 { "usb-path", 1, 0, 'H' },
Kévin Redon206c3d72018-11-12 22:51:37 +0100674 { "atr", 1, 0, 'a' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200675 { NULL, 0, 0, 0 }
676};
677
Kévin Redone0b837c2018-10-10 00:39:25 +0200678int main(int argc, char **argv)
679{
Harald Weltee56f2b92019-03-02 17:02:13 +0100680 struct rspro_server_conn *srvc;
Kévin Redone0b837c2018-10-10 00:39:25 +0200681 struct st_transport *transp = ci->slot->transp;
682 char *gsmtap_host = "127.0.0.1";
683 int rc;
684 int c, ret = 1;
685 int keep_running = 0;
Harald Weltee56f2b92019-03-02 17:02:13 +0100686 int server_port = 9999;
Kévin Redone0b837c2018-10-10 00:39:25 +0200687 int if_num = 0, vendor_id = -1, product_id = -1;
688 int config_id = -1, altsetting = 0, addr = -1;
Harald Weltee56f2b92019-03-02 17:02:13 +0100689 char *server_host = "127.0.0.1";
Kévin Redone0b837c2018-10-10 00:39:25 +0200690 char *path = NULL;
Kévin Redon206c3d72018-11-12 22:51:37 +0100691 uint8_t atr_data[33] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
692 uint8_t atr_len = 2;
Kévin Redone0b837c2018-10-10 00:39:25 +0200693
694 print_welcome();
695
696 while (1) {
697 int option_index = 0;
698
Harald Weltee56f2b92019-03-02 17:02:13 +0100699 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 +0200700 if (c == -1)
701 break;
702 switch (c) {
Harald Weltee56f2b92019-03-02 17:02:13 +0100703 case 's':
704 server_host = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200705 break;
706 case 'p':
Harald Weltee56f2b92019-03-02 17:02:13 +0100707 server_port = atoi(optarg);
Kévin Redonfbca97a2018-10-11 19:13:24 +0200708 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200709 case 'h':
710 print_help();
711 exit(0);
712 break;
713 case 'i':
714 gsmtap_host = optarg;
715 break;
716 case 'k':
717 keep_running = 1;
718 break;
719 case 'V':
720 vendor_id = strtol(optarg, NULL, 16);
721 break;
722 case 'P':
723 product_id = strtol(optarg, NULL, 16);
724 break;
725 case 'C':
726 config_id = atoi(optarg);
727 break;
728 case 'I':
729 if_num = atoi(optarg);
730 break;
731 case 'S':
732 altsetting = atoi(optarg);
733 break;
734 case 'A':
735 addr = atoi(optarg);
736 break;
737 case 'H':
738 path = optarg;
739 break;
Kévin Redon206c3d72018-11-12 22:51:37 +0100740 case 'a':
741 rc = osmo_hexparse(optarg, atr_data, ARRAY_SIZE(atr_data));
742 if (rc < 2 || rc > ARRAY_SIZE(atr_data)) {
743 fprintf(stderr, "ATR matlformed\n");
744 goto do_exit;
745 }
746 atr_len = rc;
747 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200748 }
749 }
750
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200751 if (vendor_id < 0 || product_id < 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200752 fprintf(stderr, "You have to specify the vendor and product ID\n");
753 goto do_exit;
754 }
755
Kévin Redon193a8c12018-10-11 17:24:39 +0200756 rc = libusb_init(NULL);
757 if (rc < 0) {
758 fprintf(stderr, "libusb initialization failed\n");
759 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +0200760 }
761
762 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
763 if (!g_gti) {
764 perror("unable to open GSMTAP");
765 goto close_exit;
766 }
767 gsmtap_source_add_sink(g_gti);
768
769 signal(SIGINT, &signal_handler);
770
771 // initialize remote SIM client
772 g_tall_ctx = talloc_named_const(NULL, 0, "global");
773
Kévin Redone0b837c2018-10-10 00:39:25 +0200774 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +0100775
776 srvc = &g_client->srv_conn;
777 srvc->server_host = server_host;
778 srvc->server_port = server_port;
779 srvc->handle_rx = srvc_handle_rx;
780 srvc->own_comp_id.type = ComponentType_remsimClient;
781 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
782 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
783 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
784 rc = server_conn_fsm_alloc(g_client, srvc);
785 if (rc < 0) {
786 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
787 exit(1);
788 }
Kévin Redone0b837c2018-10-10 00:39:25 +0200789
790 asn_debug = 0;
791 osmo_init_logging2(g_tall_ctx, &log_info);
792
793 if (bankd_conn_fsm_alloc(g_client) < 0) {
794 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
795 exit(1);
796 }
797
798 // connect to SIMtrace2 cardem
799 do {
800 struct usb_interface_match _ifm, *ifm = &_ifm;
801 ifm->vendor = vendor_id;
802 ifm->product = product_id;
803 ifm->configuration = config_id;
804 ifm->interface = if_num;
805 ifm->altsetting = altsetting;
806 ifm->addr = addr;
807 if (path)
808 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
809 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
810 if (!transp->usb_devh) {
811 fprintf(stderr, "can't open USB device\n");
812 goto close_exit;
813 }
814
815 rc = libusb_claim_interface(transp->usb_devh, if_num);
816 if (rc < 0) {
817 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
818 goto close_exit;
819 }
820
821 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
822 &transp->usb_ep.in, &transp->usb_ep.irq_in);
823 if (rc < 0) {
824 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
825 goto close_exit;
826 }
827
Kévin Redon3428e412018-10-11 19:14:00 +0200828 // switch modem SIM port to emulated SIM on OWHW
829 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
830 int modem = -1;
831 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
832 case 0:
833 modem = 1;
834 break;
835 case 1:
836 modem = 2;
837 break;
838 default:
839 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
840 goto close_exit;
841 }
842 //
843 char gpio_path[PATH_MAX];
844 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
845 int connec_st_usim = open(gpio_path, O_WRONLY);
846 if (-1 == connec_st_usim) {
847 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
848 goto close_exit;
849 }
850 if (1 != write(connec_st_usim, "1", 1)) {
851 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
852 goto close_exit;
853 }
854 printf("switched modem %d to emulated USIM\n", modem);
855
856 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
857 int mdm_rst = open(gpio_path, O_WRONLY);
858 if (-1 == mdm_rst) {
859 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
860 goto close_exit;
861 }
862 if (1 != write(mdm_rst, "1", 1)) {
863 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
864 goto close_exit;
865 }
866 sleep(1); // wait a bit to ensure reset is effective
867 if (1 != write(mdm_rst, "0", 1)) {
868 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
869 goto close_exit;
870 }
871 printf("modem %d reset\n", modem);
872 }
873
Kévin Redone0b837c2018-10-10 00:39:25 +0200874 /* simulate card-insert to modem (owhw, not qmod) */
875 cardem_request_card_insert(ci, true);
876
877 /* select remote (forwarded) SIM */
878 st_modem_sim_select_remote(ci->slot);
879
880 /* set the ATR */
Kévin Redon206c3d72018-11-12 22:51:37 +0100881 //atr_update_csum(real_atr, sizeof(real_atr));
882 cardem_request_set_atr(ci, atr_data, atr_len);
Kévin Redone0b837c2018-10-10 00:39:25 +0200883
884 /* select remote (forwarded) SIM */
885 st_modem_reset_pulse(ci->slot, 300);
886
887 run_mainloop(ci);
888 ret = 0;
889
Kévin Redon193a8c12018-10-11 17:24:39 +0200890 libusb_release_interface(transp->usb_devh, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200891close_exit:
892 if (transp->usb_devh)
893 libusb_close(transp->usb_devh);
894 if (keep_running)
895 sleep(1);
896 } while (keep_running);
897
Kévin Redon193a8c12018-10-11 17:24:39 +0200898 libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +0200899do_exit:
900 return ret;
901}