blob: 9a681b47b528dd80ef9682bd9d6fd1565958b673 [file] [log] [blame]
Kévin Redonefbcf382018-07-07 17:35:15 +02001/* simtrace2-remsim - main program for the host PC to provide a remote SIM
2 * using the SIMtrace 2 firmware in card emulation mode
Harald Welte095ac6c2016-03-19 13:39:33 +01003 *
Kévin Redonefbcf382018-07-07 17:35:15 +02004 * (C) 2016 by Harald Welte <hwelte@hmw-consulting.de>
Harald Welte095ac6c2016-03-19 13:39:33 +01005 *
Kévin Redonefbcf382018-07-07 17:35:15 +02006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
Harald Welte095ac6c2016-03-19 13:39:33 +010010 *
Kévin Redonefbcf382018-07-07 17:35:15 +020011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Harald Welte095ac6c2016-03-19 13:39:33 +010015 *
Kévin Redonefbcf382018-07-07 17:35:15 +020016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Harald Welte095ac6c2016-03-19 13:39:33 +010019 */
Harald Welte095ac6c2016-03-19 13:39:33 +010020#include <errno.h>
21#include <unistd.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <stdint.h>
Harald Welte1871c252016-03-20 15:30:46 +010026#include <signal.h>
Harald Welte095ac6c2016-03-19 13:39:33 +010027#include <time.h>
28#define _GNU_SOURCE
29#include <getopt.h>
30
31#include <sys/time.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36
37#include <libusb.h>
38
Harald Welte822d66e2017-03-06 20:58:03 +010039#include "libusb_util.h"
Harald Welte095ac6c2016-03-19 13:39:33 +010040#include "simtrace.h"
Harald Welte25a9a802017-05-08 13:30:09 +020041#include "simtrace_prot.h"
Harald Welte095ac6c2016-03-19 13:39:33 +010042#include "apdu_dispatch.h"
Harald Welte236caf62016-03-19 21:28:09 +010043#include "simtrace2-discovery.h"
Harald Welte095ac6c2016-03-19 13:39:33 +010044
45#include <osmocom/core/gsmtap.h>
46#include <osmocom/core/gsmtap_util.h>
47#include <osmocom/core/utils.h>
48#include <osmocom/core/socket.h>
Harald Welte25a9a802017-05-08 13:30:09 +020049#include <osmocom/core/msgb.h>
Harald Welte095ac6c2016-03-19 13:39:33 +010050#include <osmocom/sim/class_tables.h>
51#include <osmocom/sim/sim.h>
52
Harald Welte08b77ba2017-05-09 15:11:53 +020053/* transport to a SIMtrace device */
Harald Welted0396052017-05-10 22:19:26 +020054struct st_transport {
Harald Welte08b77ba2017-05-09 15:11:53 +020055 /* USB */
Harald Welte2ba03bb2017-03-06 18:51:39 +010056 struct libusb_device_handle *usb_devh;
57 struct {
58 uint8_t in;
59 uint8_t out;
60 uint8_t irq_in;
61 } usb_ep;
Harald Welte2ba03bb2017-03-06 18:51:39 +010062
Harald Welte08b77ba2017-05-09 15:11:53 +020063 /* UDP */
Harald Welte2ba03bb2017-03-06 18:51:39 +010064 int udp_fd;
Harald Welte08b77ba2017-05-09 15:11:53 +020065};
66
Harald Welted0396052017-05-10 22:19:26 +020067/* a SIMtrace slot; communicates over a transport */
68struct st_slot {
69 /* transport through which the slot can be reached */
70 struct st_transport *transp;
71 /* number of the slot within the transport */
72 uint8_t slot_nr;
73};
74
Harald Welte08b77ba2017-05-09 15:11:53 +020075/* One istance of card emulation */
76struct cardem_inst {
Harald Welted0396052017-05-10 22:19:26 +020077 /* slot on which this card emulation instance runs */
78 struct st_slot *slot;
79 /* libosmosim SIM card profile */
Harald Welte08b77ba2017-05-09 15:11:53 +020080 const struct osim_cla_ins_card_profile *card_prof;
Harald Welted0396052017-05-10 22:19:26 +020081 /* libosmosim SIM card channel */
Harald Welte2ba03bb2017-03-06 18:51:39 +010082 struct osim_chan_hdl *chan;
83};
Harald Welte095ac6c2016-03-19 13:39:33 +010084
Harald Welted0396052017-05-10 22:19:26 +020085/* global GSMTAP instance */
86static struct gsmtap_inst *g_gti;
87
Harald Welte095ac6c2016-03-19 13:39:33 +010088static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
89{
90 struct gsmtap_hdr *gh;
91 unsigned int gross_len = len + sizeof(*gh);
92 uint8_t *buf = malloc(gross_len);
93 int rc;
94
95 if (!buf)
96 return -ENOMEM;
97
98 memset(buf, 0, sizeof(*gh));
99 gh = (struct gsmtap_hdr *) buf;
100 gh->version = GSMTAP_VERSION;
101 gh->hdr_len = sizeof(*gh)/4;
102 gh->type = GSMTAP_TYPE_SIM;
103
104 memcpy(buf + sizeof(*gh), apdu, len);
105
106 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
107 if (rc < 0) {
108 perror("write gsmtap");
109 free(buf);
110 return rc;
111 }
112
113 free(buf);
114 return 0;
115}
116
Harald Welte296c4012017-05-09 15:02:52 +0200117/***********************************************************************
118 * SIMTRACE pcore protocol
119 ***********************************************************************/
120
Harald Welted0396052017-05-10 22:19:26 +0200121/*! \brief allocate a message buffer for simtrace use */
Harald Welte25a9a802017-05-08 13:30:09 +0200122static struct msgb *st_msgb_alloc(void)
123{
124 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
125}
126
Harald Welte095ac6c2016-03-19 13:39:33 +0100127#if 0
128static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
129{
130 printf("APDU: %s\n", osmo_hexdump(buf, len));
131 gsmtap_send_sim(buf, len);
132}
133#endif
134
135/*! \brief Transmit a given command to the SIMtrace2 device */
Harald Welted0396052017-05-10 22:19:26 +0200136int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
Harald Welte095ac6c2016-03-19 13:39:33 +0100137{
Harald Welte25a9a802017-05-08 13:30:09 +0200138 int rc;
Harald Welte095ac6c2016-03-19 13:39:33 +0100139
Harald Welte25a9a802017-05-08 13:30:09 +0200140 printf("<- %s\n", msgb_hexdump(msg));
Harald Welte095ac6c2016-03-19 13:39:33 +0100141
Harald Welte08b77ba2017-05-09 15:11:53 +0200142 if (transp->udp_fd < 0) {
Harald Welte9f38ddd2017-05-10 22:49:02 +0200143 int xfer_len;
Harald Welte25a9a802017-05-08 13:30:09 +0200144
Harald Welte08b77ba2017-05-09 15:11:53 +0200145 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
146 msgb_data(msg), msgb_length(msg),
147 &xfer_len, 100000);
Harald Welte095ac6c2016-03-19 13:39:33 +0100148 } else {
Harald Welte08b77ba2017-05-09 15:11:53 +0200149 rc = write(transp->udp_fd, msgb_data(msg), msgb_length(msg));
Harald Welte095ac6c2016-03-19 13:39:33 +0100150 }
Harald Welte25a9a802017-05-08 13:30:09 +0200151
152 msgb_free(msg);
153 return rc;
154}
155
Harald Welted0396052017-05-10 22:19:26 +0200156static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
157 uint8_t slot_nr)
Harald Welte25a9a802017-05-08 13:30:09 +0200158{
Harald Welte9f38ddd2017-05-10 22:49:02 +0200159 struct simtrace_msg_hdr *sh;
Harald Welte25a9a802017-05-08 13:30:09 +0200160
Harald Welte9f38ddd2017-05-10 22:49:02 +0200161 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
Harald Welte25a9a802017-05-08 13:30:09 +0200162 memset(sh, 0, sizeof(*sh));
163 sh->msg_class = msg_class;
164 sh->msg_type = msg_type;
Harald Welted0396052017-05-10 22:19:26 +0200165 sh->slot_nr = slot_nr;
Harald Welte25a9a802017-05-08 13:30:09 +0200166 sh->msg_len = msgb_length(msg);
Harald Welte9f38ddd2017-05-10 22:49:02 +0200167
168 return sh;
Harald Welte095ac6c2016-03-19 13:39:33 +0100169}
170
Harald Welted0396052017-05-10 22:19:26 +0200171/* transmit a given message to a specified slot. Expects all headers
172 * present before calling the function */
173int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
174 uint8_t msg_class, uint8_t msg_type)
175{
Harald Welted0396052017-05-10 22:19:26 +0200176 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
177
178 return st_transp_tx_msg(slot->transp, msg);
179}
180
Harald Welte296c4012017-05-09 15:02:52 +0200181/***********************************************************************
182 * Card Emulation protocol
183 ***********************************************************************/
184
185
Harald Welte095ac6c2016-03-19 13:39:33 +0100186/*! \brief Request the SIMtrace2 to generate a card-insert signal */
Harald Welte296c4012017-05-09 15:02:52 +0200187static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
Harald Welte095ac6c2016-03-19 13:39:33 +0100188{
Harald Welte25a9a802017-05-08 13:30:09 +0200189 struct msgb *msg = st_msgb_alloc();
190 struct cardemu_usb_msg_cardinsert *cins;
Harald Welte095ac6c2016-03-19 13:39:33 +0100191
Harald Welte25a9a802017-05-08 13:30:09 +0200192 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
193 memset(cins, 0, sizeof(*cins));
Harald Welte095ac6c2016-03-19 13:39:33 +0100194 if (inserted)
Harald Welte25a9a802017-05-08 13:30:09 +0200195 cins->card_insert = 1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100196
Harald Welted0396052017-05-10 22:19:26 +0200197 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
Harald Welte095ac6c2016-03-19 13:39:33 +0100198}
199
200/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
Harald Welte296c4012017-05-09 15:02:52 +0200201static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
Harald Welte095ac6c2016-03-19 13:39:33 +0100202{
Harald Welte25a9a802017-05-08 13:30:09 +0200203 struct msgb *msg = st_msgb_alloc();
Harald Welte095ac6c2016-03-19 13:39:33 +0100204 struct cardemu_usb_msg_tx_data *txd;
Harald Welte25a9a802017-05-08 13:30:09 +0200205 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100206
Harald Welte296c4012017-05-09 15:02:52 +0200207 printf("<= %s(%02x, %d)\n", __func__, pb, le);
Harald Welte095ac6c2016-03-19 13:39:33 +0100208
209 memset(txd, 0, sizeof(*txd));
210 txd->data_len = 1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100211 txd->flags = CEMU_DATA_F_PB_AND_RX;
Harald Welte25a9a802017-05-08 13:30:09 +0200212 /* one data byte */
213 msgb_put_u8(msg, pb);
Harald Welte095ac6c2016-03-19 13:39:33 +0100214
Harald Welted0396052017-05-10 22:19:26 +0200215 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte095ac6c2016-03-19 13:39:33 +0100216}
217
218/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
Harald Welte296c4012017-05-09 15:02:52 +0200219static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
220 const uint8_t *data, uint8_t data_len_in)
Harald Welte095ac6c2016-03-19 13:39:33 +0100221{
Harald Welte25a9a802017-05-08 13:30:09 +0200222 struct msgb *msg = st_msgb_alloc();
Harald Welte095ac6c2016-03-19 13:39:33 +0100223 struct cardemu_usb_msg_tx_data *txd;
Harald Welte25a9a802017-05-08 13:30:09 +0200224 uint8_t *cur;
225
226 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100227
Harald Welte296c4012017-05-09 15:02:52 +0200228 printf("<= %s(%02x, %s, %d)\n", __func__, pb,
229 osmo_hexdump(data, data_len_in), data_len_in);
Harald Welte095ac6c2016-03-19 13:39:33 +0100230
231 memset(txd, 0, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100232 txd->data_len = 1 + data_len_in;
233 txd->flags = CEMU_DATA_F_PB_AND_TX;
Harald Welte25a9a802017-05-08 13:30:09 +0200234 /* procedure byte */
235 msgb_put_u8(msg, pb);
236 /* data */
237 cur = msgb_put(msg, data_len_in);
238 memcpy(cur, data, data_len_in);
Harald Welte095ac6c2016-03-19 13:39:33 +0100239
Harald Welted0396052017-05-10 22:19:26 +0200240 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte095ac6c2016-03-19 13:39:33 +0100241}
242
243/*! \brief Request the SIMtrace2 to send a Status Word */
Harald Welte296c4012017-05-09 15:02:52 +0200244static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
Harald Welte095ac6c2016-03-19 13:39:33 +0100245{
Harald Welte25a9a802017-05-08 13:30:09 +0200246 struct msgb *msg = st_msgb_alloc();
Harald Welte095ac6c2016-03-19 13:39:33 +0100247 struct cardemu_usb_msg_tx_data *txd;
Harald Welte25a9a802017-05-08 13:30:09 +0200248 uint8_t *cur;
249
250 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100251
Harald Welte296c4012017-05-09 15:02:52 +0200252 printf("<= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Harald Welte095ac6c2016-03-19 13:39:33 +0100253
254 memset(txd, 0, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100255 txd->data_len = 2;
256 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
Harald Welte25a9a802017-05-08 13:30:09 +0200257 cur = msgb_put(msg, 2);
258 cur[0] = sw[0];
259 cur[1] = sw[1];
Harald Welte095ac6c2016-03-19 13:39:33 +0100260
Harald Welted0396052017-05-10 22:19:26 +0200261 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte095ac6c2016-03-19 13:39:33 +0100262}
263
Harald Welte9daaa792016-03-20 15:01:20 +0100264static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
265{
266 uint8_t csum = 0;
267 int i;
268
269 for (i = 1; i < atr_len - 1; i++)
270 csum = csum ^ atr[i];
271
272 atr[atr_len-1] = csum;
273}
274
Harald Welte296c4012017-05-09 15:02:52 +0200275static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
Harald Welte9daaa792016-03-20 15:01:20 +0100276{
Harald Welte25a9a802017-05-08 13:30:09 +0200277 struct msgb *msg = st_msgb_alloc();
Harald Welte9daaa792016-03-20 15:01:20 +0100278 struct cardemu_usb_msg_set_atr *satr;
Harald Welte25a9a802017-05-08 13:30:09 +0200279 uint8_t *cur;
280
281 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
Harald Welte9daaa792016-03-20 15:01:20 +0100282
Harald Welte296c4012017-05-09 15:02:52 +0200283 printf("<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Harald Welte9daaa792016-03-20 15:01:20 +0100284
285 memset(satr, 0, sizeof(*satr));
Harald Welte9daaa792016-03-20 15:01:20 +0100286 satr->atr_len = atr_len;
Harald Welte25a9a802017-05-08 13:30:09 +0200287 cur = msgb_put(msg, atr_len);
288 memcpy(cur, atr, atr_len);
Harald Welte9daaa792016-03-20 15:01:20 +0100289
Harald Welted0396052017-05-10 22:19:26 +0200290 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
Harald Welte9daaa792016-03-20 15:01:20 +0100291}
292
Harald Welte296c4012017-05-09 15:02:52 +0200293/***********************************************************************
Harald Welte66de8302017-05-11 01:12:50 +0200294 * Modem Control protocol
295 ***********************************************************************/
296
297static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
298{
299 struct msgb *msg = st_msgb_alloc();
300 struct st_modem_reset *sr ;
301
302 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
303 sr->asserted = asserted;
304 sr->pulse_duration_msec = pulse_ms;
305
306 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
307}
308
309/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
310int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
311{
312 return _modem_reset(slot, 2, duration_ms);
313}
314
315/*! \brief assert the RESET line of the modem */
316int st_modem_reset_active(struct st_slot *slot)
317{
318 return _modem_reset(slot, 1, 0);
319}
320
321/*! \brief de-assert the RESET line of the modem */
322int st_modem_reset_inactive(struct st_slot *slot)
323{
324 return _modem_reset(slot, 0, 0);
325}
326
327static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
328{
329 struct msgb *msg = st_msgb_alloc();
330 struct st_modem_sim_select *ss;
331
332 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
333 ss->remote_sim = remote_sim;
334
335 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
336}
337
338/*! \brief select local (physical) SIM for given slot */
339int st_modem_sim_select_local(struct st_slot *slot)
340{
341 return _modem_sim_select(slot, 0);
342}
343
344/*! \brief select remote (emulated/forwarded) SIM for given slot */
345int st_modem_sim_select_remote(struct st_slot *slot)
346{
347 return _modem_sim_select(slot, 1);
348}
349
350/*! \brief Request slot to send us status information about the modem */
351int st_modem_get_status(struct st_slot *slot)
352{
353 struct msgb *msg = st_msgb_alloc();
354
355 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
356}
357
358
359/***********************************************************************
Harald Welte296c4012017-05-09 15:02:52 +0200360 * Incoming Messages
361 ***********************************************************************/
362
Harald Welte095ac6c2016-03-19 13:39:33 +0100363/*! \brief Process a STATUS message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100364static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100365{
366 struct cardemu_usb_msg_status *status;
367 status = (struct cardemu_usb_msg_status *) buf;
368
369 printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
370 status->flags, status->fi, status->di, status->wi,
371 status->waiting_time);
372
373 return 0;
374}
375
376/*! \brief Process a PTS indication message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100377static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100378{
379 struct cardemu_usb_msg_pts_info *pts;
380 pts = (struct cardemu_usb_msg_pts_info *) buf;
381
382 printf("=> PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
383
384 return 0;
385}
386
387/*! \brief Process a ERROR indication message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100388static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100389{
390 struct cardemu_usb_msg_error *err;
391 err = (struct cardemu_usb_msg_error *) buf;
392
393 printf("=> ERROR: %u/%u/%u: %s\n",
394 err->severity, err->subsystem, err->code,
Harald Welteb170ea92017-03-06 18:59:41 +0100395 err->msg_len ? (char *)err->msg : "");
Harald Welte095ac6c2016-03-19 13:39:33 +0100396
397 return 0;
398}
399
400/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100401static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100402{
403 static struct apdu_context ac;
404 struct cardemu_usb_msg_rx_data *data;
Harald Welte095ac6c2016-03-19 13:39:33 +0100405 int rc;
406
407 data = (struct cardemu_usb_msg_rx_data *) buf;
408
409 printf("=> DATA: flags=%x, %s: ", data->flags,
410 osmo_hexdump(data->data, data->data_len));
411
412 rc = apdu_segment_in(&ac, data->data, data->data_len,
413 data->flags & CEMU_DATA_F_TPDU_HDR);
414
415 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) {
416 struct msgb *tmsg = msgb_alloc(1024, "TPDU");
Harald Welte2ba03bb2017-03-06 18:51:39 +0100417 struct osim_reader_hdl *rh = ci->chan->card->reader;
Harald Welte095ac6c2016-03-19 13:39:33 +0100418 uint8_t *cur;
419
420 /* Copy TPDU header */
421 cur = msgb_put(tmsg, sizeof(ac.hdr));
422 memcpy(cur, &ac.hdr, sizeof(ac.hdr));
423 /* Copy D(c), if any */
424 if (ac.lc.tot) {
425 cur = msgb_put(tmsg, ac.lc.tot);
426 memcpy(cur, ac.dc, ac.lc.tot);
427 }
428 /* send to actual card */
429 tmsg->l3h = tmsg->tail;
430 rc = rh->ops->transceive(rh, tmsg);
431 if (rc < 0) {
432 fprintf(stderr, "error during transceive: %d\n", rc);
433 msgb_free(tmsg);
434 return rc;
435 }
436 msgb_apdu_sw(tmsg) = msgb_get_u16(tmsg);
437 ac.sw[0] = msgb_apdu_sw(tmsg) >> 8;
438 ac.sw[1] = msgb_apdu_sw(tmsg) & 0xff;
439 printf("SW=0x%04x, len_rx=%d\n", msgb_apdu_sw(tmsg), msgb_l3len(tmsg));
440 if (msgb_l3len(tmsg))
Harald Welte296c4012017-05-09 15:02:52 +0200441 cardem_request_pb_and_tx(ci, ac.hdr.ins, tmsg->l3h, msgb_l3len(tmsg));
442 cardem_request_sw_tx(ci, ac.sw);
Harald Welte095ac6c2016-03-19 13:39:33 +0100443 } else if (ac.lc.tot > ac.lc.cur) {
Harald Welte296c4012017-05-09 15:02:52 +0200444 cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur);
Harald Welte095ac6c2016-03-19 13:39:33 +0100445 }
446 return 0;
447}
448
Harald Welte25a9a802017-05-08 13:30:09 +0200449#if 0
450 case SIMTRACE_CMD_DO_ERROR
451 rc = process_do_error(ci, buf, len);
452 break;
453#endif
454
Harald Welte095ac6c2016-03-19 13:39:33 +0100455/*! \brief Process an incoming message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100456static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100457{
Harald Welte25a9a802017-05-08 13:30:09 +0200458 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
Harald Welte095ac6c2016-03-19 13:39:33 +0100459 int rc;
460
461 printf("-> %s\n", osmo_hexdump(buf, len));
462
Harald Welte25a9a802017-05-08 13:30:09 +0200463 buf += sizeof(*sh);
464
Harald Welte095ac6c2016-03-19 13:39:33 +0100465 switch (sh->msg_type) {
Harald Welte25a9a802017-05-08 13:30:09 +0200466 case SIMTRACE_MSGT_BD_CEMU_STATUS:
Harald Welte2ba03bb2017-03-06 18:51:39 +0100467 rc = process_do_status(ci, buf, len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100468 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200469 case SIMTRACE_MSGT_DO_CEMU_PTS:
Harald Welte2ba03bb2017-03-06 18:51:39 +0100470 rc = process_do_pts(ci, buf, len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100471 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200472 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
Harald Welte2ba03bb2017-03-06 18:51:39 +0100473 rc = process_do_rx_da(ci, buf, len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100474 break;
475 default:
476 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
477 rc = -1;
478 break;
479 }
480
481 return rc;
482}
483
484static void print_welcome(void)
485{
486 printf("simtrace2-remsim - Remote SIM card forwarding\n"
Harald Welteb170ea92017-03-06 18:59:41 +0100487 "(C) 2010-2017 by Harald Welte <laforge@gnumonks.org>\n\n");
Harald Welte095ac6c2016-03-19 13:39:33 +0100488}
489
490static void print_help(void)
491{
Harald Welte62bfd8a2017-03-06 20:56:14 +0100492 printf( "\t-r\t--remote-udp-host HOST\n"
493 "\t-p\t--remote-udp-port PORT\n"
Harald Welte095ac6c2016-03-19 13:39:33 +0100494 "\t-h\t--help\n"
Harald Welte62bfd8a2017-03-06 20:56:14 +0100495 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
496 "\t-a\t--skip-atr\n"
Harald Welte095ac6c2016-03-19 13:39:33 +0100497 "\t-k\t--keep-running\n"
Harald Welte822d66e2017-03-06 20:58:03 +0100498 "\t-V\t--usb-vendor\tVENDOR_ID\n"
499 "\t-P\t--usb-product\tPRODUCT_ID\n"
500 "\t-C\t--usb-config\tCONFIG_ID\n"
501 "\t-I\t--usb-interface\tINTERFACE_ID\n"
502 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
503 "\t-A\t--usb-address\tADDRESS\n"
Harald Welte095ac6c2016-03-19 13:39:33 +0100504 "\n"
505 );
506}
507
508static const struct option opts[] = {
Harald Welte62bfd8a2017-03-06 20:56:14 +0100509 { "remote-udp-host", 1, 0, 'r' },
510 { "remote-udp-port", 1, 0, 'p' },
Harald Welte095ac6c2016-03-19 13:39:33 +0100511 { "gsmtap-ip", 1, 0, 'i' },
512 { "skip-atr", 0, 0, 'a' },
513 { "help", 0, 0, 'h' },
514 { "keep-running", 0, 0, 'k' },
Harald Welte822d66e2017-03-06 20:58:03 +0100515 { "usb-vendor", 1, 0, 'V' },
516 { "usb-product", 1, 0, 'P' },
517 { "usb-config", 1, 0, 'C' },
518 { "usb-interface", 1, 0, 'I' },
519 { "usb-altsetting", 1, 0, 'S' },
520 { "usb-address", 1, 0, 'A' },
Harald Weltea6016352017-05-11 01:31:45 +0200521 { "usb-path", 1, 0, 'H' },
Harald Welte095ac6c2016-03-19 13:39:33 +0100522 { NULL, 0, 0, 0 }
523};
524
Harald Welte2ba03bb2017-03-06 18:51:39 +0100525static void run_mainloop(struct cardem_inst *ci)
Harald Welte095ac6c2016-03-19 13:39:33 +0100526{
Harald Welted0396052017-05-10 22:19:26 +0200527 struct st_transport *transp = ci->slot->transp;
Harald Welte095ac6c2016-03-19 13:39:33 +0100528 unsigned int msg_count, byte_count = 0;
Harald Welteb170ea92017-03-06 18:59:41 +0100529 uint8_t buf[16*265];
Harald Welte095ac6c2016-03-19 13:39:33 +0100530 int xfer_len;
531 int rc;
532
533 printf("Entering main loop\n");
534
535 while (1) {
536 /* read data from SIMtrace2 device (local or via USB) */
Harald Welte08b77ba2017-05-09 15:11:53 +0200537 if (transp->udp_fd < 0) {
538 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
Kévin Redon04ccb772018-08-02 17:48:58 +0200539 buf, sizeof(buf), &xfer_len, 100);
Harald Welte37ad41e2017-05-11 01:13:58 +0200540 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
541 rc != LIBUSB_ERROR_INTERRUPTED &&
542 rc != LIBUSB_ERROR_IO) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100543 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
544 return;
545 }
546 } else {
Harald Welte08b77ba2017-05-09 15:11:53 +0200547 rc = read(transp->udp_fd, buf, sizeof(buf));
Harald Welte095ac6c2016-03-19 13:39:33 +0100548 if (rc <= 0) {
549 fprintf(stderr, "shor read from UDP\n");
550 return;
551 }
552 xfer_len = rc;
553 }
554 /* dispatch any incoming data */
555 if (xfer_len > 0) {
Kévin Redon04ccb772018-08-02 17:48:58 +0200556 printf("URB: %s\n", osmo_hexdump(buf, xfer_len));
Harald Welte2ba03bb2017-03-06 18:51:39 +0100557 process_usb_msg(ci, buf, xfer_len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100558 msg_count++;
559 byte_count += xfer_len;
560 }
561 }
562}
563
Harald Welted0396052017-05-10 22:19:26 +0200564static struct st_transport _transp;
565
566static struct st_slot _slot = {
567 .transp = &_transp,
568 .slot_nr = 0,
569};
570
571struct cardem_inst _ci = {
572 .slot = &_slot,
573};
574
575struct cardem_inst *ci = &_ci;
Harald Welte2ba03bb2017-03-06 18:51:39 +0100576
Harald Welte1871c252016-03-20 15:30:46 +0100577static void signal_handler(int signal)
578{
579 switch (signal) {
580 case SIGINT:
Harald Welte296c4012017-05-09 15:02:52 +0200581 cardem_request_card_insert(ci, false);
Harald Welte1871c252016-03-20 15:30:46 +0100582 exit(0);
583 break;
584 default:
585 break;
586 }
587}
588
Harald Welte095ac6c2016-03-19 13:39:33 +0100589int main(int argc, char **argv)
590{
Harald Welted0396052017-05-10 22:19:26 +0200591 struct st_transport *transp = ci->slot->transp;
Harald Welte095ac6c2016-03-19 13:39:33 +0100592 char *gsmtap_host = "127.0.0.1";
593 int rc;
594 int c, ret = 1;
595 int skip_atr = 0;
596 int keep_running = 0;
597 int remote_udp_port = 52342;
Harald Welte822d66e2017-03-06 20:58:03 +0100598 int if_num = 0, vendor_id = -1, product_id = -1;
599 int config_id = -1, altsetting = 0, addr = -1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100600 char *remote_udp_host = NULL;
Harald Weltea6016352017-05-11 01:31:45 +0200601 char *path = NULL;
Harald Welte095ac6c2016-03-19 13:39:33 +0100602 struct osim_reader_hdl *reader;
603 struct osim_card_hdl *card;
604
605 print_welcome();
606
607 while (1) {
608 int option_index = 0;
609
Harald Weltea6016352017-05-11 01:31:45 +0200610 c = getopt_long(argc, argv, "r:p:hi:V:P:C:I:S:A:H:ak", opts, &option_index);
Harald Welte095ac6c2016-03-19 13:39:33 +0100611 if (c == -1)
612 break;
613 switch (c) {
614 case 'r':
615 remote_udp_host = optarg;
616 break;
617 case 'p':
618 remote_udp_port = atoi(optarg);
619 break;
620 case 'h':
621 print_help();
622 exit(0);
623 break;
624 case 'i':
625 gsmtap_host = optarg;
626 break;
627 case 'a':
628 skip_atr = 1;
629 break;
630 case 'k':
631 keep_running = 1;
632 break;
Harald Welte822d66e2017-03-06 20:58:03 +0100633 case 'V':
634 vendor_id = strtol(optarg, NULL, 16);
635 break;
636 case 'P':
637 product_id = strtol(optarg, NULL, 16);
638 break;
639 case 'C':
640 config_id = atoi(optarg);
641 break;
642 case 'I':
643 if_num = atoi(optarg);
644 break;
645 case 'S':
646 altsetting = atoi(optarg);
647 break;
648 case 'A':
649 addr = atoi(optarg);
650 break;
Harald Weltea6016352017-05-11 01:31:45 +0200651 case 'H':
652 path = optarg;
653 break;
Harald Welte095ac6c2016-03-19 13:39:33 +0100654 }
655 }
656
Harald Welte822d66e2017-03-06 20:58:03 +0100657 if (!remote_udp_host && (vendor_id < 0 || product_id < 0)) {
658 fprintf(stderr, "You have to specify the vendor and product ID\n");
659 goto do_exit;
660 }
661
Harald Welte08b77ba2017-05-09 15:11:53 +0200662 transp->udp_fd = -1;
Harald Welte2ba03bb2017-03-06 18:51:39 +0100663
664 ci->card_prof = &osim_uicc_sim_cic_profile;
Harald Welte095ac6c2016-03-19 13:39:33 +0100665
666 if (!remote_udp_host) {
667 rc = libusb_init(NULL);
668 if (rc < 0) {
669 fprintf(stderr, "libusb initialization failed\n");
670 goto do_exit;
671 }
672 } else {
Harald Welte08b77ba2017-05-09 15:11:53 +0200673 transp->udp_fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
674 remote_udp_host, remote_udp_port+if_num,
675 OSMO_SOCK_F_CONNECT);
676 if (transp->udp_fd < 0) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100677 fprintf(stderr, "error binding UDP port\n");
678 goto do_exit;
679 }
680 }
681
682 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
683 if (!g_gti) {
684 perror("unable to open GSMTAP");
685 goto close_exit;
686 }
687 gsmtap_source_add_sink(g_gti);
688
689 reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL);
690 if (!reader) {
691 perror("unable to open PC/SC reader");
692 goto close_exit;
693 }
694
695 card = osim_card_open(reader, OSIM_PROTO_T0);
696 if (!card) {
697 perror("unable to open SIM card");
698 goto close_exit;
699 }
700
Harald Welte2ba03bb2017-03-06 18:51:39 +0100701 ci->chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
702 if (!ci->chan) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100703 perror("SIM card has no channel?!?");
704 goto close_exit;
705 }
706
Harald Welte1871c252016-03-20 15:30:46 +0100707 signal(SIGINT, &signal_handler);
708
Harald Welte095ac6c2016-03-19 13:39:33 +0100709 do {
Harald Welte08b77ba2017-05-09 15:11:53 +0200710 if (transp->udp_fd < 0) {
Harald Welte822d66e2017-03-06 20:58:03 +0100711 struct usb_interface_match _ifm, *ifm = &_ifm;
712 ifm->vendor = vendor_id;
713 ifm->product = product_id;
714 ifm->configuration = config_id;
715 ifm->interface = if_num;
716 ifm->altsetting = altsetting;
717 ifm->addr = addr;
Harald Weltea6016352017-05-11 01:31:45 +0200718 if (path)
719 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
Harald Welte08b77ba2017-05-09 15:11:53 +0200720 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
721 if (!transp->usb_devh) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100722 fprintf(stderr, "can't open USB device\n");
723 goto close_exit;
724 }
725
Harald Welte08b77ba2017-05-09 15:11:53 +0200726 rc = libusb_claim_interface(transp->usb_devh, if_num);
Harald Welte095ac6c2016-03-19 13:39:33 +0100727 if (rc < 0) {
Harald Welte236caf62016-03-19 21:28:09 +0100728 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
729 goto close_exit;
730 }
731
Harald Welte08b77ba2017-05-09 15:11:53 +0200732 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
733 &transp->usb_ep.in, &transp->usb_ep.irq_in);
Harald Welte236caf62016-03-19 21:28:09 +0100734 if (rc < 0) {
735 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
Harald Welte095ac6c2016-03-19 13:39:33 +0100736 goto close_exit;
737 }
738 }
739
Harald Welte66de8302017-05-11 01:12:50 +0200740 /* simulate card-insert to modem (owhw, not qmod) */
Harald Welte296c4012017-05-09 15:02:52 +0200741 cardem_request_card_insert(ci, true);
Harald Welte66de8302017-05-11 01:12:50 +0200742
743 /* select remote (forwarded) SIM */
744 st_modem_sim_select_remote(ci->slot);
745
746 /* set the ATR */
Harald Welte9daaa792016-03-20 15:01:20 +0100747 uint8_t real_atr[] = { 0x3B, 0x9F, 0x96, 0x80, 0x1F, 0xC7, 0x80, 0x31,
748 0xA0, 0x73, 0xBE, 0x21, 0x13, 0x67, 0x43, 0x20,
749 0x07, 0x18, 0x00, 0x00, 0x01, 0xA5 };
750 atr_update_csum(real_atr, sizeof(real_atr));
Harald Welte296c4012017-05-09 15:02:52 +0200751 cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
Harald Welte095ac6c2016-03-19 13:39:33 +0100752
Harald Welte66de8302017-05-11 01:12:50 +0200753 /* select remote (forwarded) SIM */
754 st_modem_reset_pulse(ci->slot, 300);
755
Harald Welte2ba03bb2017-03-06 18:51:39 +0100756 run_mainloop(ci);
Harald Welte095ac6c2016-03-19 13:39:33 +0100757 ret = 0;
758
Harald Welte08b77ba2017-05-09 15:11:53 +0200759 if (transp->udp_fd < 0)
760 libusb_release_interface(transp->usb_devh, 0);
Harald Welte095ac6c2016-03-19 13:39:33 +0100761close_exit:
Harald Welte08b77ba2017-05-09 15:11:53 +0200762 if (transp->usb_devh)
763 libusb_close(transp->usb_devh);
Harald Welte095ac6c2016-03-19 13:39:33 +0100764 if (keep_running)
765 sleep(1);
766 } while (keep_running);
767
Harald Welte08b77ba2017-05-09 15:11:53 +0200768 if (transp->udp_fd < 0)
Harald Welte095ac6c2016-03-19 13:39:33 +0100769 libusb_exit(NULL);
770do_exit:
771 return ret;
772}