blob: 1cf09a63f5efcb8265c6843772fc586e916ea6c2 [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:
542 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
543 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200544 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
545 bankd_handle_tpduCardToModem(bc, pdu);
546 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200547 default:
548 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
549 return -1;
550 }
551
552 return 0;
553}
554
555int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
556{
557 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
558 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
559 struct bankd_client *bc = conn->data;
560 int rc;
561
562 if (msgb_length(msg) < sizeof(*hh))
563 goto invalid;
564 msg->l2h = &hh->data[0];
565 if (hh->proto != IPAC_PROTO_OSMO)
566 goto invalid;
567 if (!he || msgb_l2len(msg) < sizeof(*he))
568 goto invalid;
569 msg->l2h = &he->data[0];
570
571 if (he->proto != IPAC_PROTO_EXT_RSPRO)
572 goto invalid;
573
574 printf("Received RSPRO %s\n", msgb_hexdump(msg));
575
576 rc = bankd_handle_msg(bc, msg);
577
578 return rc;
579
580invalid:
581 msgb_free(msg);
582 return -1;
583}
584
585static const struct log_info_cat default_categories[] = {
586 [DMAIN] = {
587 .name = "DMAIN",
588 .loglevel = LOGL_DEBUG,
589 .enabled = 1,
590 },
591};
592
593static const struct log_info log_info = {
594 .cat = default_categories,
595 .num_cat = ARRAY_SIZE(default_categories),
596};
597
Kévin Redon3ec265b2018-10-11 17:30:33 +0200598static void print_welcome(void)
599{
600 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
601 "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
602 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
603}
604
605static void print_help(void)
606{
607 printf( "\t-d\t--bankd-host HOST\n"
608 "\t-p\t--bankd-port PORT\n"
Kévin Redonfbca97a2018-10-11 19:13:24 +0200609 "\t-c\t--client-id REMSIM_CLIENT_ID\n"
610 "\t-s\t--slot-nr REMSIM_SLOT_NUMBER\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200611 "\t-h\t--help\n"
612 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
613 "\t-k\t--keep-running\n"
614 "\t-V\t--usb-vendor\tVENDOR_ID\n"
615 "\t-P\t--usb-product\tPRODUCT_ID\n"
616 "\t-C\t--usb-config\tCONFIG_ID\n"
617 "\t-I\t--usb-interface\tINTERFACE_ID\n"
618 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
619 "\t-A\t--usb-address\tADDRESS\n"
620 "\t-H\t--usb-path\tPATH\n"
Kévin Redon206c3d72018-11-12 22:51:37 +0100621 "\t-a\t--atr\tATR\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200622 "\n"
623 );
624}
625
626static const struct option opts[] = {
627 { "bankd-host", 1, 0, 'b' },
628 { "bankd-port", 1, 0, 'p' },
Kévin Redonfbca97a2018-10-11 19:13:24 +0200629 { "client-id", 1, 0, 'c' },
630 { "slot-nr", 1, 0, 's' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200631 { "gsmtap-ip", 1, 0, 'i' },
632 { "help", 0, 0, 'h' },
633 { "keep-running", 0, 0, 'k' },
634 { "usb-vendor", 1, 0, 'V' },
635 { "usb-product", 1, 0, 'P' },
636 { "usb-config", 1, 0, 'C' },
637 { "usb-interface", 1, 0, 'I' },
638 { "usb-altsetting", 1, 0, 'S' },
639 { "usb-address", 1, 0, 'A' },
640 { "usb-path", 1, 0, 'H' },
Kévin Redon206c3d72018-11-12 22:51:37 +0100641 { "atr", 1, 0, 'a' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200642 { NULL, 0, 0, 0 }
643};
644
Kévin Redone0b837c2018-10-10 00:39:25 +0200645int main(int argc, char **argv)
646{
647 struct st_transport *transp = ci->slot->transp;
648 char *gsmtap_host = "127.0.0.1";
649 int rc;
650 int c, ret = 1;
651 int keep_running = 0;
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200652 int bankd_port = 9999;
Kévin Redonfbca97a2018-10-11 19:13:24 +0200653 int client_id = -1, slot_nr = -1;
Kévin Redone0b837c2018-10-10 00:39:25 +0200654 int if_num = 0, vendor_id = -1, product_id = -1;
655 int config_id = -1, altsetting = 0, addr = -1;
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200656 char *bankd_host = "127.0.0.1";
Kévin Redone0b837c2018-10-10 00:39:25 +0200657 char *path = NULL;
Kévin Redon206c3d72018-11-12 22:51:37 +0100658 uint8_t atr_data[33] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
659 uint8_t atr_len = 2;
Kévin Redone0b837c2018-10-10 00:39:25 +0200660
661 print_welcome();
662
663 while (1) {
664 int option_index = 0;
665
Kévin Redon206c3d72018-11-12 22:51:37 +0100666 c = getopt_long(argc, argv, "b:p:c:s:hi:V:P:C:I:S:A:H:a:k", opts, &option_index);
Kévin Redone0b837c2018-10-10 00:39:25 +0200667 if (c == -1)
668 break;
669 switch (c) {
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200670 case 'b':
671 bankd_host = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200672 break;
673 case 'p':
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200674 bankd_port = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200675 break;
Kévin Redonfbca97a2018-10-11 19:13:24 +0200676 case 'c':
677 client_id = atoi(optarg);
678 break;
679 case 's':
680 slot_nr = atoi(optarg);
681 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200682 case 'h':
683 print_help();
684 exit(0);
685 break;
686 case 'i':
687 gsmtap_host = optarg;
688 break;
689 case 'k':
690 keep_running = 1;
691 break;
692 case 'V':
693 vendor_id = strtol(optarg, NULL, 16);
694 break;
695 case 'P':
696 product_id = strtol(optarg, NULL, 16);
697 break;
698 case 'C':
699 config_id = atoi(optarg);
700 break;
701 case 'I':
702 if_num = atoi(optarg);
703 break;
704 case 'S':
705 altsetting = atoi(optarg);
706 break;
707 case 'A':
708 addr = atoi(optarg);
709 break;
710 case 'H':
711 path = optarg;
712 break;
Kévin Redon206c3d72018-11-12 22:51:37 +0100713 case 'a':
714 rc = osmo_hexparse(optarg, atr_data, ARRAY_SIZE(atr_data));
715 if (rc < 2 || rc > ARRAY_SIZE(atr_data)) {
716 fprintf(stderr, "ATR matlformed\n");
717 goto do_exit;
718 }
719 atr_len = rc;
720 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200721 }
722 }
723
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200724 if (vendor_id < 0 || product_id < 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200725 fprintf(stderr, "You have to specify the vendor and product ID\n");
726 goto do_exit;
727 }
728
Kévin Redonfbca97a2018-10-11 19:13:24 +0200729 if (client_id < 0 || slot_nr < 0) {
730 fprintf(stderr, "You have to specify the remote SIM client ID and slot number\n");
731 goto do_exit;
732 }
733
Kévin Redon193a8c12018-10-11 17:24:39 +0200734 rc = libusb_init(NULL);
735 if (rc < 0) {
736 fprintf(stderr, "libusb initialization failed\n");
737 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +0200738 }
739
740 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
741 if (!g_gti) {
742 perror("unable to open GSMTAP");
743 goto close_exit;
744 }
745 gsmtap_source_add_sink(g_gti);
746
747 signal(SIGINT, &signal_handler);
748
749 // initialize remote SIM client
750 g_tall_ctx = talloc_named_const(NULL, 0, "global");
751
752 osmo_fsm_register(&remsim_client_bankd_fsm);
753 osmo_fsm_register(&remsim_client_server_fsm);
754
755 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200756 g_client->bankd_host = bankd_host;
757 g_client->bankd_port = bankd_port;
Kévin Redone0b837c2018-10-10 00:39:25 +0200758 g_client->own_comp_id.type = ComponentType_remsimClient;
Kévin Redonfbca97a2018-10-11 19:13:24 +0200759 g_client->clslot = &(ClientSlot_t){ .clientId = client_id, .slotNr = slot_nr };
Kévin Redon19c97ed2018-10-11 17:28:25 +0200760 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "simtrace2-remsim-client");
Kévin Redone0b837c2018-10-10 00:39:25 +0200761 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client");
762 OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION);
763
764 asn_debug = 0;
765 osmo_init_logging2(g_tall_ctx, &log_info);
766
767 if (bankd_conn_fsm_alloc(g_client) < 0) {
768 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
769 exit(1);
770 }
771
772 // connect to SIMtrace2 cardem
773 do {
774 struct usb_interface_match _ifm, *ifm = &_ifm;
775 ifm->vendor = vendor_id;
776 ifm->product = product_id;
777 ifm->configuration = config_id;
778 ifm->interface = if_num;
779 ifm->altsetting = altsetting;
780 ifm->addr = addr;
781 if (path)
782 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
783 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
784 if (!transp->usb_devh) {
785 fprintf(stderr, "can't open USB device\n");
786 goto close_exit;
787 }
788
789 rc = libusb_claim_interface(transp->usb_devh, if_num);
790 if (rc < 0) {
791 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
792 goto close_exit;
793 }
794
795 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
796 &transp->usb_ep.in, &transp->usb_ep.irq_in);
797 if (rc < 0) {
798 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
799 goto close_exit;
800 }
801
Kévin Redon3428e412018-10-11 19:14:00 +0200802 // switch modem SIM port to emulated SIM on OWHW
803 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
804 int modem = -1;
805 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
806 case 0:
807 modem = 1;
808 break;
809 case 1:
810 modem = 2;
811 break;
812 default:
813 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
814 goto close_exit;
815 }
816 //
817 char gpio_path[PATH_MAX];
818 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
819 int connec_st_usim = open(gpio_path, O_WRONLY);
820 if (-1 == connec_st_usim) {
821 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
822 goto close_exit;
823 }
824 if (1 != write(connec_st_usim, "1", 1)) {
825 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
826 goto close_exit;
827 }
828 printf("switched modem %d to emulated USIM\n", modem);
829
830 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
831 int mdm_rst = open(gpio_path, O_WRONLY);
832 if (-1 == mdm_rst) {
833 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
834 goto close_exit;
835 }
836 if (1 != write(mdm_rst, "1", 1)) {
837 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
838 goto close_exit;
839 }
840 sleep(1); // wait a bit to ensure reset is effective
841 if (1 != write(mdm_rst, "0", 1)) {
842 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
843 goto close_exit;
844 }
845 printf("modem %d reset\n", modem);
846 }
847
Kévin Redone0b837c2018-10-10 00:39:25 +0200848 /* simulate card-insert to modem (owhw, not qmod) */
849 cardem_request_card_insert(ci, true);
850
851 /* select remote (forwarded) SIM */
852 st_modem_sim_select_remote(ci->slot);
853
854 /* set the ATR */
Kévin Redon206c3d72018-11-12 22:51:37 +0100855 //atr_update_csum(real_atr, sizeof(real_atr));
856 cardem_request_set_atr(ci, atr_data, atr_len);
Kévin Redone0b837c2018-10-10 00:39:25 +0200857
858 /* select remote (forwarded) SIM */
859 st_modem_reset_pulse(ci->slot, 300);
860
861 run_mainloop(ci);
862 ret = 0;
863
Kévin Redon193a8c12018-10-11 17:24:39 +0200864 libusb_release_interface(transp->usb_devh, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200865close_exit:
866 if (transp->usb_devh)
867 libusb_close(transp->usb_devh);
868 if (keep_running)
869 sleep(1);
870 } while (keep_running);
871
Kévin Redon193a8c12018-10-11 17:24:39 +0200872 libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +0200873do_exit:
874 return ret;
875}