blob: f15bdfed8d04e346b6b2598a865786aa1674b002 [file] [log] [blame]
Harald Welte3dcdd202019-03-09 13:06:46 +01001/* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org>
2 * (C) 2018 by sysmocom - s.f.m.c. GmbH, Author: Kevin Redon
3 *
4 * All Rights Reserved
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Kévin Redone0b837c2018-10-10 00:39:25 +020024
25#include <errno.h>
26#include <string.h>
27
28#include <talloc.h>
29
30#include <osmocom/core/msgb.h>
31#include <osmocom/core/fsm.h>
32#include <osmocom/core/utils.h>
33#include <osmocom/core/logging.h>
34#include <osmocom/core/application.h>
35
36#include <osmocom/abis/ipa.h>
37#include <osmocom/gsm/protocol/ipaccess.h>
38
39#include "rspro_util.h"
40#include "client.h"
Harald Welte61d98e92019-03-03 15:43:07 +010041#include "debug.h"
Kévin Redone0b837c2018-10-10 00:39:25 +020042
43#include <unistd.h>
44#include <stdio.h>
Kévin Redon3428e412018-10-11 19:14:00 +020045#include <linux/limits.h>
46#include <sys/stat.h>
47#include <fcntl.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020048#include <signal.h>
49#include <getopt.h>
50
51#include <libusb.h>
52
53#include "simtrace2/libusb_util.h"
54#include "simtrace2/simtrace_prot.h"
Kévin Redon3428e412018-10-11 19:14:00 +020055#include "simtrace2/simtrace_usb.h"
Kévin Redone0b837c2018-10-10 00:39:25 +020056#include "simtrace2/apdu_dispatch.h"
57#include "simtrace2/simtrace2-discovery.h"
58
59#include <osmocom/core/gsmtap.h>
60#include <osmocom/core/gsmtap_util.h>
61#include <osmocom/core/utils.h>
62#include <osmocom/core/socket.h>
63#include <osmocom/core/msgb.h>
64#include <osmocom/sim/class_tables.h>
65#include <osmocom/sim/sim.h>
66
67/* transport to a SIMtrace device */
68struct st_transport {
69 /* USB */
70 struct libusb_device_handle *usb_devh;
71 struct {
72 uint8_t in;
73 uint8_t out;
74 uint8_t irq_in;
75 } usb_ep;
Kévin Redone0b837c2018-10-10 00:39:25 +020076};
77
78/* a SIMtrace slot; communicates over a transport */
79struct st_slot {
80 /* transport through which the slot can be reached */
81 struct st_transport *transp;
82 /* number of the slot within the transport */
83 uint8_t slot_nr;
84};
85
86/* One istance of card emulation */
87struct cardem_inst {
88 /* slot on which this card emulation instance runs */
89 struct st_slot *slot;
90};
91
92/* global GSMTAP instance */
93static struct gsmtap_inst *g_gti;
94
95static struct bankd_client *g_client;
96static void *g_tall_ctx;
97void __thread *talloc_asn1_ctx;
98int asn_debug;
99
100static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
101{
102 struct gsmtap_hdr *gh;
103 unsigned int gross_len = len + sizeof(*gh);
104 uint8_t *buf = malloc(gross_len);
105 int rc;
106
107 if (!buf)
108 return -ENOMEM;
109
110 memset(buf, 0, sizeof(*gh));
111 gh = (struct gsmtap_hdr *) buf;
112 gh->version = GSMTAP_VERSION;
113 gh->hdr_len = sizeof(*gh)/4;
114 gh->type = GSMTAP_TYPE_SIM;
115
116 memcpy(buf + sizeof(*gh), apdu, len);
117
118 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
119 if (rc < 0) {
120 perror("write gsmtap");
121 free(buf);
122 return rc;
123 }
124
125 free(buf);
126 return 0;
127}
128
129/***********************************************************************
130 * SIMTRACE pcore protocol
131 ***********************************************************************/
132
133/*! \brief allocate a message buffer for simtrace use */
134static struct msgb *st_msgb_alloc(void)
135{
136 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
137}
138
139#if 0
140static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
141{
142 printf("APDU: %s\n", osmo_hexdump(buf, len));
143 gsmtap_send_sim(buf, len);
144}
145#endif
146
147/*! \brief Transmit a given command to the SIMtrace2 device */
148int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
149{
150 int rc;
151
Kévin Redon21e31de2018-10-11 17:25:58 +0200152 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redone0b837c2018-10-10 00:39:25 +0200153
Kévin Redon193a8c12018-10-11 17:24:39 +0200154 int xfer_len;
Kévin Redone0b837c2018-10-10 00:39:25 +0200155
Kévin Redon193a8c12018-10-11 17:24:39 +0200156 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
157 msgb_data(msg), msgb_length(msg),
158 &xfer_len, 100000);
Kévin Redone0b837c2018-10-10 00:39:25 +0200159
160 msgb_free(msg);
161 return rc;
162}
163
164static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
165 uint8_t slot_nr)
166{
167 struct simtrace_msg_hdr *sh;
168
169 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
170 memset(sh, 0, sizeof(*sh));
171 sh->msg_class = msg_class;
172 sh->msg_type = msg_type;
173 sh->slot_nr = slot_nr;
174 sh->msg_len = msgb_length(msg);
175
176 return sh;
177}
178
179/* transmit a given message to a specified slot. Expects all headers
180 * present before calling the function */
181int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
182 uint8_t msg_class, uint8_t msg_type)
183{
184 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
185
186 return st_transp_tx_msg(slot->transp, msg);
187}
188
189/***********************************************************************
190 * Card Emulation protocol
191 ***********************************************************************/
192
193
194/*! \brief Request the SIMtrace2 to generate a card-insert signal */
195static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
196{
197 struct msgb *msg = st_msgb_alloc();
198 struct cardemu_usb_msg_cardinsert *cins;
199
200 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
201 memset(cins, 0, sizeof(*cins));
202 if (inserted)
203 cins->card_insert = 1;
204
205 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
206}
207
208/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
209static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
210{
211 struct msgb *msg = st_msgb_alloc();
212 struct cardemu_usb_msg_tx_data *txd;
213 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
214
Kévin Redon21e31de2018-10-11 17:25:58 +0200215 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redone0b837c2018-10-10 00:39:25 +0200216
217 memset(txd, 0, sizeof(*txd));
218 txd->data_len = 1;
219 txd->flags = CEMU_DATA_F_PB_AND_RX;
220 /* one data byte */
221 msgb_put_u8(msg, pb);
222
223 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
224}
225
226/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
227static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
Kévin Redonf120b642018-10-15 19:53:02 +0200228 const uint8_t *data, uint16_t data_len_in)
Kévin Redone0b837c2018-10-10 00:39:25 +0200229{
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, %s, %d)\n", __func__, pb,
Kévin Redone0b837c2018-10-10 00:39:25 +0200237 osmo_hexdump(data, data_len_in), data_len_in);
238
239 memset(txd, 0, sizeof(*txd));
240 txd->data_len = 1 + data_len_in;
241 txd->flags = CEMU_DATA_F_PB_AND_TX;
242 /* procedure byte */
243 msgb_put_u8(msg, pb);
244 /* data */
245 cur = msgb_put(msg, data_len_in);
246 memcpy(cur, data, data_len_in);
247
248 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
249}
250
251/*! \brief Request the SIMtrace2 to send a Status Word */
252static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
253{
254 struct msgb *msg = st_msgb_alloc();
255 struct cardemu_usb_msg_tx_data *txd;
256 uint8_t *cur;
257
258 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
259
Kévin Redon21e31de2018-10-11 17:25:58 +0200260 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redone0b837c2018-10-10 00:39:25 +0200261
262 memset(txd, 0, sizeof(*txd));
263 txd->data_len = 2;
264 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
265 cur = msgb_put(msg, 2);
266 cur[0] = sw[0];
267 cur[1] = sw[1];
268
269 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
270}
271
Kévin Redon206c3d72018-11-12 22:51:37 +0100272// FIXME check if the ATR actually includes a checksum
Kévin Redone0b837c2018-10-10 00:39:25 +0200273static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
274{
275 uint8_t csum = 0;
276 int i;
277
278 for (i = 1; i < atr_len - 1; i++)
279 csum = csum ^ atr[i];
280
281 atr[atr_len-1] = csum;
282}
283
284static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
285{
286 struct msgb *msg = st_msgb_alloc();
287 struct cardemu_usb_msg_set_atr *satr;
288 uint8_t *cur;
289
290 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
291
Kévin Redon21e31de2018-10-11 17:25:58 +0200292 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200293
294 memset(satr, 0, sizeof(*satr));
295 satr->atr_len = atr_len;
296 cur = msgb_put(msg, atr_len);
297 memcpy(cur, atr, atr_len);
298
299 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
300}
301
302/***********************************************************************
303 * Modem Control protocol
304 ***********************************************************************/
305
306static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
307{
308 struct msgb *msg = st_msgb_alloc();
309 struct st_modem_reset *sr ;
310
311 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
312 sr->asserted = asserted;
313 sr->pulse_duration_msec = pulse_ms;
314
315 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
316}
317
318/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
319int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
320{
321 return _modem_reset(slot, 2, duration_ms);
322}
323
324/*! \brief assert the RESET line of the modem */
325int st_modem_reset_active(struct st_slot *slot)
326{
327 return _modem_reset(slot, 1, 0);
328}
329
330/*! \brief de-assert the RESET line of the modem */
331int st_modem_reset_inactive(struct st_slot *slot)
332{
333 return _modem_reset(slot, 0, 0);
334}
335
336static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
337{
338 struct msgb *msg = st_msgb_alloc();
339 struct st_modem_sim_select *ss;
340
341 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
342 ss->remote_sim = remote_sim;
343
344 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
345}
346
347/*! \brief select local (physical) SIM for given slot */
348int st_modem_sim_select_local(struct st_slot *slot)
349{
350 return _modem_sim_select(slot, 0);
351}
352
353/*! \brief select remote (emulated/forwarded) SIM for given slot */
354int st_modem_sim_select_remote(struct st_slot *slot)
355{
356 return _modem_sim_select(slot, 1);
357}
358
359/*! \brief Request slot to send us status information about the modem */
360int st_modem_get_status(struct st_slot *slot)
361{
362 struct msgb *msg = st_msgb_alloc();
363
364 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
365}
366
367
368/***********************************************************************
369 * Incoming Messages
370 ***********************************************************************/
371
372/*! \brief Process a STATUS message from the SIMtrace2 */
373static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
374{
375 struct cardemu_usb_msg_status *status;
376 status = (struct cardemu_usb_msg_status *) buf;
377
Kévin Redon21e31de2018-10-11 17:25:58 +0200378 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200379 status->flags, status->fi, status->di, status->wi,
380 status->waiting_time);
381
382 return 0;
383}
384
385/*! \brief Process a PTS indication message from the SIMtrace2 */
386static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
387{
388 struct cardemu_usb_msg_pts_info *pts;
389 pts = (struct cardemu_usb_msg_pts_info *) buf;
390
Kévin Redon21e31de2018-10-11 17:25:58 +0200391 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redone0b837c2018-10-10 00:39:25 +0200392
393 return 0;
394}
395
396/*! \brief Process a ERROR indication message from the SIMtrace2 */
397static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
398{
399 struct cardemu_usb_msg_error *err;
400 err = (struct cardemu_usb_msg_error *) buf;
401
Kévin Redon21e31de2018-10-11 17:25:58 +0200402 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200403 err->severity, err->subsystem, err->code,
404 err->msg_len ? (char *)err->msg : "");
405
406 return 0;
407}
408
Kévin Redonbc08db52018-10-11 08:41:00 +0200409static struct apdu_context ac; // this will hold the complete APDU (across calls)
410
Kévin Redone0b837c2018-10-10 00:39:25 +0200411/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
412static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
413{
Kévin Redonbc08db52018-10-11 08:41:00 +0200414 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 +0200415 int rc;
416
Kévin Redon21e31de2018-10-11 17:25:58 +0200417 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redone0b837c2018-10-10 00:39:25 +0200418 osmo_hexdump(data->data, data->data_len));
419
420 rc = apdu_segment_in(&ac, data->data, data->data_len,
Kévin Redonbc08db52018-10-11 08:41:00 +0200421 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200422
Kévin Redonbc08db52018-10-11 08:41:00 +0200423 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
424 uint8_t* apdu_command = calloc(1, sizeof(ac.hdr) + ac.lc.tot); // to store the APDU command to send
425 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redone0b837c2018-10-10 00:39:25 +0200426 if (ac.lc.tot) {
Kévin Redonbc08db52018-10-11 08:41:00 +0200427 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redone0b837c2018-10-10 00:39:25 +0200428 }
Kévin Redonbc08db52018-10-11 08:41:00 +0200429 // send APDU to card
Harald Welteec628e92019-03-08 22:18:31 +0100430 RsproPDU_t *pdu = rspro_gen_TpduModem2Card(g_client->srv_conn.clslot, &(BankSlot_t){ .bankId = 0, .slotNr = 0}, apdu_command, sizeof(ac.hdr) + ac.lc.tot); // create RSPRO packet
Kévin Redonbc08db52018-10-11 08:41:00 +0200431 ipa_client_conn_send_rspro(g_client->bankd_conn, pdu); // send RSPRO packet
432 // the response will come separately
433 free(apdu_command);
434 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
435 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 +0200436 }
437 return 0;
438}
439
440#if 0
441 case SIMTRACE_CMD_DO_ERROR
442 rc = process_do_error(ci, buf, len);
443 break;
444#endif
445
446/*! \brief Process an incoming message from the SIMtrace2 */
447static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
448{
449 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
450 int rc;
451
Kévin Redon21e31de2018-10-11 17:25:58 +0200452 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200453
454 buf += sizeof(*sh);
455
456 switch (sh->msg_type) {
457 case SIMTRACE_MSGT_BD_CEMU_STATUS:
458 rc = process_do_status(ci, buf, len);
459 break;
460 case SIMTRACE_MSGT_DO_CEMU_PTS:
461 rc = process_do_pts(ci, buf, len);
462 break;
463 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
464 rc = process_do_rx_da(ci, buf, len);
465 break;
466 default:
467 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
468 rc = -1;
469 break;
470 }
471
472 return rc;
473}
474
Kévin Redone0b837c2018-10-10 00:39:25 +0200475static void run_mainloop(struct cardem_inst *ci)
476{
477 struct st_transport *transp = ci->slot->transp;
478 unsigned int msg_count, byte_count = 0;
479 uint8_t buf[16*265];
480 int xfer_len;
481 int rc;
482
483 printf("Entering main loop\n");
484
485 while (1) {
486 /* read data from SIMtrace2 device (local or via USB) */
487 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
488 buf, sizeof(buf), &xfer_len, 100);
489 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
490 rc != LIBUSB_ERROR_INTERRUPTED &&
491 rc != LIBUSB_ERROR_IO) {
Kévin Redon6f074b72018-11-12 22:53:48 +0100492 fprintf(stderr, "BULK IN transfer error: %s\n", libusb_error_name(rc));
Kévin Redone0b837c2018-10-10 00:39:25 +0200493 return;
494 }
495 /* dispatch any incoming data */
496 if (xfer_len > 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200497 process_usb_msg(ci, buf, xfer_len);
498 msg_count++;
499 byte_count += xfer_len;
500 }
501 // handle remote SIM client fsm
502 // TODO register the USB fd for this select
503 osmo_select_main(true);
504 }
505}
506
507static struct st_transport _transp;
508
509static struct st_slot _slot = {
510 .transp = &_transp,
511 .slot_nr = 0,
512};
513
514struct cardem_inst _ci = {
515 .slot = &_slot,
516};
517
518struct cardem_inst *ci = &_ci;
519
520static void signal_handler(int signal)
521{
522 switch (signal) {
523 case SIGINT:
524 cardem_request_card_insert(ci, false);
525 exit(0);
526 break;
527 default:
528 break;
529 }
530}
531
532/** remsim_client **/
533
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200534static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu)
535{
536 OSMO_ASSERT(pdu);
537 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
538
539 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
540 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
541 return -1;
542 }
543
544 // save SW to our current APDU context
545 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
546 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200547 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 +0200548 if (card2modem->data.size > 2) { // send PB and data to modem
549 cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
550 }
551 cardem_request_sw_tx(ci, ac.sw); // send SW to modem
552
553 return 0;
554}
555
Kévin Redone0b837c2018-10-10 00:39:25 +0200556static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
557{
558 RsproPDU_t *pdu = rspro_dec_msg(msg);
559 if (!pdu) {
560 fprintf(stderr, "Error decoding PDU\n");
561 return -1;
562 }
563
564 switch (pdu->msg.present) {
565 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +0100566 /* Store 'identity' of bankd to in peer_comp_id */
567 rspro_comp_id_retrieve(&bc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
Kévin Redone0b837c2018-10-10 00:39:25 +0200568 osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
569 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200570 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
571 bankd_handle_tpduCardToModem(bc, pdu);
572 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200573 default:
574 fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg));
575 return -1;
576 }
577
578 return 0;
579}
580
581int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
582{
583 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
584 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
585 struct bankd_client *bc = conn->data;
586 int rc;
587
588 if (msgb_length(msg) < sizeof(*hh))
589 goto invalid;
590 msg->l2h = &hh->data[0];
591 if (hh->proto != IPAC_PROTO_OSMO)
592 goto invalid;
593 if (!he || msgb_l2len(msg) < sizeof(*he))
594 goto invalid;
595 msg->l2h = &he->data[0];
596
597 if (he->proto != IPAC_PROTO_EXT_RSPRO)
598 goto invalid;
599
600 printf("Received RSPRO %s\n", msgb_hexdump(msg));
601
602 rc = bankd_handle_msg(bc, msg);
603
604 return rc;
605
606invalid:
607 msgb_free(msg);
608 return -1;
609}
610
Harald Weltee56f2b92019-03-02 17:02:13 +0100611/* handle incoming messages from server */
612static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
613{
614 RsproPDU_t *resp;
615
616 switch (pdu->msg.present) {
617 case RsproPDUchoice_PR_connectClientRes:
618 /* Store 'identity' of server in srvc->peer_comp_id */
619 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
620 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
621 break;
Harald Welted571a3e2019-03-11 22:09:50 +0100622 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100623 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100624 if (!g_client->srv_conn.clslot)
625 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welted571a3e2019-03-11 22:09:50 +0100626 *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
627 /* send response to server */
628 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
629 server_conn_send_rspro(srvc, resp);
630 break;
631 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100632 /* store/set the bankd ip/port as instructed by the server */
633 osmo_talloc_replace_string(g_client, &g_client->bankd_host,
Harald Welted571a3e2019-03-11 22:09:50 +0100634 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
635 /* FIXME: Store bankslot */
636 //g_client->bankd_slot = pdu->msg.choice.configClientBankReq.bankSlot;
637 g_client->bankd_port = ntohs(pdu->msg.choice.configClientBankReq.bankd.port);
Harald Weltee56f2b92019-03-02 17:02:13 +0100638 /* instruct bankd FSM to connect */
639 osmo_fsm_inst_dispatch(g_client->bankd_fi, BDC_E_ESTABLISH, NULL);
640 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +0100641 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100642 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +0100643 break;
644 default:
645 fprintf(stderr, "Unknown/Unsupported RSPRO PDU type: %u\n", pdu->msg.present);
646 return -1;
647 }
648
649 return 0;
650}
651
652
Kévin Redon3ec265b2018-10-11 17:30:33 +0200653static void print_welcome(void)
654{
655 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
656 "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
657 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
658}
659
660static void print_help(void)
661{
Harald Weltee56f2b92019-03-02 17:02:13 +0100662 printf( "\t-s\t--server-host HOST\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200663 "\t-p\t--bankd-port PORT\n"
664 "\t-h\t--help\n"
665 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
666 "\t-k\t--keep-running\n"
667 "\t-V\t--usb-vendor\tVENDOR_ID\n"
668 "\t-P\t--usb-product\tPRODUCT_ID\n"
669 "\t-C\t--usb-config\tCONFIG_ID\n"
670 "\t-I\t--usb-interface\tINTERFACE_ID\n"
671 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
672 "\t-A\t--usb-address\tADDRESS\n"
673 "\t-H\t--usb-path\tPATH\n"
Kévin Redon206c3d72018-11-12 22:51:37 +0100674 "\t-a\t--atr\tATR\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200675 "\n"
676 );
677}
678
679static const struct option opts[] = {
Harald Weltee56f2b92019-03-02 17:02:13 +0100680 { "server-host", 1, 0, 's' },
681 { "server-port", 1, 0, 'p' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200682 { "gsmtap-ip", 1, 0, 'i' },
683 { "help", 0, 0, 'h' },
684 { "keep-running", 0, 0, 'k' },
685 { "usb-vendor", 1, 0, 'V' },
686 { "usb-product", 1, 0, 'P' },
687 { "usb-config", 1, 0, 'C' },
688 { "usb-interface", 1, 0, 'I' },
689 { "usb-altsetting", 1, 0, 'S' },
690 { "usb-address", 1, 0, 'A' },
691 { "usb-path", 1, 0, 'H' },
Kévin Redon206c3d72018-11-12 22:51:37 +0100692 { "atr", 1, 0, 'a' },
Kévin Redon3ec265b2018-10-11 17:30:33 +0200693 { NULL, 0, 0, 0 }
694};
695
Kévin Redone0b837c2018-10-10 00:39:25 +0200696int main(int argc, char **argv)
697{
Harald Weltee56f2b92019-03-02 17:02:13 +0100698 struct rspro_server_conn *srvc;
Kévin Redone0b837c2018-10-10 00:39:25 +0200699 struct st_transport *transp = ci->slot->transp;
700 char *gsmtap_host = "127.0.0.1";
701 int rc;
702 int c, ret = 1;
703 int keep_running = 0;
Harald Weltee56f2b92019-03-02 17:02:13 +0100704 int server_port = 9999;
Kévin Redone0b837c2018-10-10 00:39:25 +0200705 int if_num = 0, vendor_id = -1, product_id = -1;
706 int config_id = -1, altsetting = 0, addr = -1;
Harald Weltee56f2b92019-03-02 17:02:13 +0100707 char *server_host = "127.0.0.1";
Kévin Redone0b837c2018-10-10 00:39:25 +0200708 char *path = NULL;
Kévin Redon206c3d72018-11-12 22:51:37 +0100709 uint8_t atr_data[33] = { 0x3B, 0x00 }; // the shortest simplest ATR possible
710 uint8_t atr_len = 2;
Kévin Redone0b837c2018-10-10 00:39:25 +0200711
712 print_welcome();
713
714 while (1) {
715 int option_index = 0;
716
Harald Weltee56f2b92019-03-02 17:02:13 +0100717 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 +0200718 if (c == -1)
719 break;
720 switch (c) {
Harald Weltee56f2b92019-03-02 17:02:13 +0100721 case 's':
722 server_host = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200723 break;
724 case 'p':
Harald Weltee56f2b92019-03-02 17:02:13 +0100725 server_port = atoi(optarg);
Kévin Redonfbca97a2018-10-11 19:13:24 +0200726 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200727 case 'h':
728 print_help();
729 exit(0);
730 break;
731 case 'i':
732 gsmtap_host = optarg;
733 break;
734 case 'k':
735 keep_running = 1;
736 break;
737 case 'V':
738 vendor_id = strtol(optarg, NULL, 16);
739 break;
740 case 'P':
741 product_id = strtol(optarg, NULL, 16);
742 break;
743 case 'C':
744 config_id = atoi(optarg);
745 break;
746 case 'I':
747 if_num = atoi(optarg);
748 break;
749 case 'S':
750 altsetting = atoi(optarg);
751 break;
752 case 'A':
753 addr = atoi(optarg);
754 break;
755 case 'H':
756 path = optarg;
757 break;
Kévin Redon206c3d72018-11-12 22:51:37 +0100758 case 'a':
759 rc = osmo_hexparse(optarg, atr_data, ARRAY_SIZE(atr_data));
760 if (rc < 2 || rc > ARRAY_SIZE(atr_data)) {
761 fprintf(stderr, "ATR matlformed\n");
762 goto do_exit;
763 }
764 atr_len = rc;
765 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200766 }
767 }
768
Kévin Redon39eb9dc2018-10-11 17:21:13 +0200769 if (vendor_id < 0 || product_id < 0) {
Kévin Redone0b837c2018-10-10 00:39:25 +0200770 fprintf(stderr, "You have to specify the vendor and product ID\n");
771 goto do_exit;
772 }
773
Kévin Redon193a8c12018-10-11 17:24:39 +0200774 rc = libusb_init(NULL);
775 if (rc < 0) {
776 fprintf(stderr, "libusb initialization failed\n");
777 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +0200778 }
779
780 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
781 if (!g_gti) {
782 perror("unable to open GSMTAP");
783 goto close_exit;
784 }
785 gsmtap_source_add_sink(g_gti);
786
787 signal(SIGINT, &signal_handler);
788
789 // initialize remote SIM client
790 g_tall_ctx = talloc_named_const(NULL, 0, "global");
791
Kévin Redone0b837c2018-10-10 00:39:25 +0200792 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Weltee56f2b92019-03-02 17:02:13 +0100793
794 srvc = &g_client->srv_conn;
795 srvc->server_host = server_host;
796 srvc->server_port = server_port;
797 srvc->handle_rx = srvc_handle_rx;
798 srvc->own_comp_id.type = ComponentType_remsimClient;
799 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
800 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
801 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
802 rc = server_conn_fsm_alloc(g_client, srvc);
803 if (rc < 0) {
804 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
805 exit(1);
806 }
Kévin Redone0b837c2018-10-10 00:39:25 +0200807
808 asn_debug = 0;
809 osmo_init_logging2(g_tall_ctx, &log_info);
810
811 if (bankd_conn_fsm_alloc(g_client) < 0) {
812 fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
813 exit(1);
814 }
815
816 // connect to SIMtrace2 cardem
817 do {
818 struct usb_interface_match _ifm, *ifm = &_ifm;
819 ifm->vendor = vendor_id;
820 ifm->product = product_id;
821 ifm->configuration = config_id;
822 ifm->interface = if_num;
823 ifm->altsetting = altsetting;
824 ifm->addr = addr;
825 if (path)
826 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
827 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
828 if (!transp->usb_devh) {
829 fprintf(stderr, "can't open USB device\n");
830 goto close_exit;
831 }
832
833 rc = libusb_claim_interface(transp->usb_devh, if_num);
834 if (rc < 0) {
835 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
836 goto close_exit;
837 }
838
839 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
840 &transp->usb_ep.in, &transp->usb_ep.irq_in);
841 if (rc < 0) {
842 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
843 goto close_exit;
844 }
845
Kévin Redon3428e412018-10-11 19:14:00 +0200846 // switch modem SIM port to emulated SIM on OWHW
847 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
848 int modem = -1;
849 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
850 case 0:
851 modem = 1;
852 break;
853 case 1:
854 modem = 2;
855 break;
856 default:
857 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
858 goto close_exit;
859 }
860 //
861 char gpio_path[PATH_MAX];
862 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
863 int connec_st_usim = open(gpio_path, O_WRONLY);
864 if (-1 == connec_st_usim) {
865 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
866 goto close_exit;
867 }
868 if (1 != write(connec_st_usim, "1", 1)) {
869 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
870 goto close_exit;
871 }
872 printf("switched modem %d to emulated USIM\n", modem);
873
874 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
875 int mdm_rst = open(gpio_path, O_WRONLY);
876 if (-1 == mdm_rst) {
877 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
878 goto close_exit;
879 }
880 if (1 != write(mdm_rst, "1", 1)) {
881 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
882 goto close_exit;
883 }
884 sleep(1); // wait a bit to ensure reset is effective
885 if (1 != write(mdm_rst, "0", 1)) {
886 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
887 goto close_exit;
888 }
889 printf("modem %d reset\n", modem);
890 }
891
Kévin Redone0b837c2018-10-10 00:39:25 +0200892 /* simulate card-insert to modem (owhw, not qmod) */
893 cardem_request_card_insert(ci, true);
894
895 /* select remote (forwarded) SIM */
896 st_modem_sim_select_remote(ci->slot);
897
898 /* set the ATR */
Kévin Redon206c3d72018-11-12 22:51:37 +0100899 //atr_update_csum(real_atr, sizeof(real_atr));
900 cardem_request_set_atr(ci, atr_data, atr_len);
Kévin Redone0b837c2018-10-10 00:39:25 +0200901
902 /* select remote (forwarded) SIM */
903 st_modem_reset_pulse(ci->slot, 300);
904
905 run_mainloop(ci);
906 ret = 0;
907
Kévin Redon193a8c12018-10-11 17:24:39 +0200908 libusb_release_interface(transp->usb_devh, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200909close_exit:
910 if (transp->usb_devh)
911 libusb_close(transp->usb_devh);
912 if (keep_running)
913 sleep(1);
914 } while (keep_running);
915
Kévin Redon193a8c12018-10-11 17:24:39 +0200916 libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +0200917do_exit:
918 return ret;
919}