blob: 462f73e0872faada6e9976b10733df55b3ba02b5 [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 Welte9f38ddd2017-05-10 22:49:02 +0200176 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *) msg->data;
Harald Welted0396052017-05-10 22:19:26 +0200177
178 sh->slot_nr = slot->slot_nr;
179
180 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
181
182 return st_transp_tx_msg(slot->transp, msg);
183}
184
185
Harald Welte296c4012017-05-09 15:02:52 +0200186/***********************************************************************
187 * Card Emulation protocol
188 ***********************************************************************/
189
190
Harald Welte095ac6c2016-03-19 13:39:33 +0100191/*! \brief Request the SIMtrace2 to generate a card-insert signal */
Harald Welte296c4012017-05-09 15:02:52 +0200192static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
Harald Welte095ac6c2016-03-19 13:39:33 +0100193{
Harald Welte25a9a802017-05-08 13:30:09 +0200194 struct msgb *msg = st_msgb_alloc();
195 struct cardemu_usb_msg_cardinsert *cins;
Harald Welte095ac6c2016-03-19 13:39:33 +0100196
Harald Welte25a9a802017-05-08 13:30:09 +0200197 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
198 memset(cins, 0, sizeof(*cins));
Harald Welte095ac6c2016-03-19 13:39:33 +0100199 if (inserted)
Harald Welte25a9a802017-05-08 13:30:09 +0200200 cins->card_insert = 1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100201
Harald Welted0396052017-05-10 22:19:26 +0200202 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
Harald Welte095ac6c2016-03-19 13:39:33 +0100203}
204
205/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
Harald Welte296c4012017-05-09 15:02:52 +0200206static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
Harald Welte095ac6c2016-03-19 13:39:33 +0100207{
Harald Welte25a9a802017-05-08 13:30:09 +0200208 struct msgb *msg = st_msgb_alloc();
Harald Welte095ac6c2016-03-19 13:39:33 +0100209 struct cardemu_usb_msg_tx_data *txd;
Harald Welte25a9a802017-05-08 13:30:09 +0200210 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100211
Harald Welte296c4012017-05-09 15:02:52 +0200212 printf("<= %s(%02x, %d)\n", __func__, pb, le);
Harald Welte095ac6c2016-03-19 13:39:33 +0100213
214 memset(txd, 0, sizeof(*txd));
215 txd->data_len = 1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100216 txd->flags = CEMU_DATA_F_PB_AND_RX;
Harald Welte25a9a802017-05-08 13:30:09 +0200217 /* one data byte */
218 msgb_put_u8(msg, pb);
Harald Welte095ac6c2016-03-19 13:39:33 +0100219
Harald Welted0396052017-05-10 22:19:26 +0200220 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte095ac6c2016-03-19 13:39:33 +0100221}
222
223/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
Harald Welte296c4012017-05-09 15:02:52 +0200224static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
225 const uint8_t *data, uint8_t data_len_in)
Harald Welte095ac6c2016-03-19 13:39:33 +0100226{
Harald Welte25a9a802017-05-08 13:30:09 +0200227 struct msgb *msg = st_msgb_alloc();
Harald Welte095ac6c2016-03-19 13:39:33 +0100228 struct cardemu_usb_msg_tx_data *txd;
Harald Welte25a9a802017-05-08 13:30:09 +0200229 uint8_t *cur;
230
231 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100232
Harald Welte296c4012017-05-09 15:02:52 +0200233 printf("<= %s(%02x, %s, %d)\n", __func__, pb,
234 osmo_hexdump(data, data_len_in), data_len_in);
Harald Welte095ac6c2016-03-19 13:39:33 +0100235
236 memset(txd, 0, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100237 txd->data_len = 1 + data_len_in;
238 txd->flags = CEMU_DATA_F_PB_AND_TX;
Harald Welte25a9a802017-05-08 13:30:09 +0200239 /* procedure byte */
240 msgb_put_u8(msg, pb);
241 /* data */
242 cur = msgb_put(msg, data_len_in);
243 memcpy(cur, data, data_len_in);
Harald Welte095ac6c2016-03-19 13:39:33 +0100244
Harald Welted0396052017-05-10 22:19:26 +0200245 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte095ac6c2016-03-19 13:39:33 +0100246}
247
248/*! \brief Request the SIMtrace2 to send a Status Word */
Harald Welte296c4012017-05-09 15:02:52 +0200249static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
Harald Welte095ac6c2016-03-19 13:39:33 +0100250{
Harald Welte25a9a802017-05-08 13:30:09 +0200251 struct msgb *msg = st_msgb_alloc();
Harald Welte095ac6c2016-03-19 13:39:33 +0100252 struct cardemu_usb_msg_tx_data *txd;
Harald Welte25a9a802017-05-08 13:30:09 +0200253 uint8_t *cur;
254
255 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100256
Harald Welte296c4012017-05-09 15:02:52 +0200257 printf("<= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Harald Welte095ac6c2016-03-19 13:39:33 +0100258
259 memset(txd, 0, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100260 txd->data_len = 2;
261 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
Harald Welte25a9a802017-05-08 13:30:09 +0200262 cur = msgb_put(msg, 2);
263 cur[0] = sw[0];
264 cur[1] = sw[1];
Harald Welte095ac6c2016-03-19 13:39:33 +0100265
Harald Welted0396052017-05-10 22:19:26 +0200266 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte095ac6c2016-03-19 13:39:33 +0100267}
268
Harald Welte9daaa792016-03-20 15:01:20 +0100269static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
270{
271 uint8_t csum = 0;
272 int i;
273
274 for (i = 1; i < atr_len - 1; i++)
275 csum = csum ^ atr[i];
276
277 atr[atr_len-1] = csum;
278}
279
Harald Welte296c4012017-05-09 15:02:52 +0200280static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
Harald Welte9daaa792016-03-20 15:01:20 +0100281{
Harald Welte25a9a802017-05-08 13:30:09 +0200282 struct msgb *msg = st_msgb_alloc();
Harald Welte9daaa792016-03-20 15:01:20 +0100283 struct cardemu_usb_msg_set_atr *satr;
Harald Welte25a9a802017-05-08 13:30:09 +0200284 uint8_t *cur;
285
286 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
Harald Welte9daaa792016-03-20 15:01:20 +0100287
Harald Welte296c4012017-05-09 15:02:52 +0200288 printf("<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Harald Welte9daaa792016-03-20 15:01:20 +0100289
290 memset(satr, 0, sizeof(*satr));
Harald Welte9daaa792016-03-20 15:01:20 +0100291 satr->atr_len = atr_len;
Harald Welte25a9a802017-05-08 13:30:09 +0200292 cur = msgb_put(msg, atr_len);
293 memcpy(cur, atr, atr_len);
Harald Welte9daaa792016-03-20 15:01:20 +0100294
Harald Welted0396052017-05-10 22:19:26 +0200295 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
Harald Welte9daaa792016-03-20 15:01:20 +0100296}
297
Harald Welte296c4012017-05-09 15:02:52 +0200298/***********************************************************************
Harald Welte66de8302017-05-11 01:12:50 +0200299 * Modem Control protocol
300 ***********************************************************************/
301
302static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
303{
304 struct msgb *msg = st_msgb_alloc();
305 struct st_modem_reset *sr ;
306
307 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
308 sr->asserted = asserted;
309 sr->pulse_duration_msec = pulse_ms;
310
311 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
312}
313
314/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
315int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
316{
317 return _modem_reset(slot, 2, duration_ms);
318}
319
320/*! \brief assert the RESET line of the modem */
321int st_modem_reset_active(struct st_slot *slot)
322{
323 return _modem_reset(slot, 1, 0);
324}
325
326/*! \brief de-assert the RESET line of the modem */
327int st_modem_reset_inactive(struct st_slot *slot)
328{
329 return _modem_reset(slot, 0, 0);
330}
331
332static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
333{
334 struct msgb *msg = st_msgb_alloc();
335 struct st_modem_sim_select *ss;
336
337 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
338 ss->remote_sim = remote_sim;
339
340 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
341}
342
343/*! \brief select local (physical) SIM for given slot */
344int st_modem_sim_select_local(struct st_slot *slot)
345{
346 return _modem_sim_select(slot, 0);
347}
348
349/*! \brief select remote (emulated/forwarded) SIM for given slot */
350int st_modem_sim_select_remote(struct st_slot *slot)
351{
352 return _modem_sim_select(slot, 1);
353}
354
355/*! \brief Request slot to send us status information about the modem */
356int st_modem_get_status(struct st_slot *slot)
357{
358 struct msgb *msg = st_msgb_alloc();
359
360 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
361}
362
363
364/***********************************************************************
Harald Welte296c4012017-05-09 15:02:52 +0200365 * Incoming Messages
366 ***********************************************************************/
367
Harald Welte095ac6c2016-03-19 13:39:33 +0100368/*! \brief Process a STATUS message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100369static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100370{
371 struct cardemu_usb_msg_status *status;
372 status = (struct cardemu_usb_msg_status *) buf;
373
374 printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
375 status->flags, status->fi, status->di, status->wi,
376 status->waiting_time);
377
378 return 0;
379}
380
381/*! \brief Process a PTS indication message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100382static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100383{
384 struct cardemu_usb_msg_pts_info *pts;
385 pts = (struct cardemu_usb_msg_pts_info *) buf;
386
387 printf("=> PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
388
389 return 0;
390}
391
392/*! \brief Process a ERROR indication message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100393static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100394{
395 struct cardemu_usb_msg_error *err;
396 err = (struct cardemu_usb_msg_error *) buf;
397
398 printf("=> ERROR: %u/%u/%u: %s\n",
399 err->severity, err->subsystem, err->code,
Harald Welteb170ea92017-03-06 18:59:41 +0100400 err->msg_len ? (char *)err->msg : "");
Harald Welte095ac6c2016-03-19 13:39:33 +0100401
402 return 0;
403}
404
405/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100406static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100407{
408 static struct apdu_context ac;
409 struct cardemu_usb_msg_rx_data *data;
Harald Welte095ac6c2016-03-19 13:39:33 +0100410 int rc;
411
412 data = (struct cardemu_usb_msg_rx_data *) buf;
413
414 printf("=> DATA: flags=%x, %s: ", data->flags,
415 osmo_hexdump(data->data, data->data_len));
416
417 rc = apdu_segment_in(&ac, data->data, data->data_len,
418 data->flags & CEMU_DATA_F_TPDU_HDR);
419
420 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) {
421 struct msgb *tmsg = msgb_alloc(1024, "TPDU");
Harald Welte2ba03bb2017-03-06 18:51:39 +0100422 struct osim_reader_hdl *rh = ci->chan->card->reader;
Harald Welte095ac6c2016-03-19 13:39:33 +0100423 uint8_t *cur;
424
425 /* Copy TPDU header */
426 cur = msgb_put(tmsg, sizeof(ac.hdr));
427 memcpy(cur, &ac.hdr, sizeof(ac.hdr));
428 /* Copy D(c), if any */
429 if (ac.lc.tot) {
430 cur = msgb_put(tmsg, ac.lc.tot);
431 memcpy(cur, ac.dc, ac.lc.tot);
432 }
433 /* send to actual card */
434 tmsg->l3h = tmsg->tail;
435 rc = rh->ops->transceive(rh, tmsg);
436 if (rc < 0) {
437 fprintf(stderr, "error during transceive: %d\n", rc);
438 msgb_free(tmsg);
439 return rc;
440 }
441 msgb_apdu_sw(tmsg) = msgb_get_u16(tmsg);
442 ac.sw[0] = msgb_apdu_sw(tmsg) >> 8;
443 ac.sw[1] = msgb_apdu_sw(tmsg) & 0xff;
444 printf("SW=0x%04x, len_rx=%d\n", msgb_apdu_sw(tmsg), msgb_l3len(tmsg));
445 if (msgb_l3len(tmsg))
Harald Welte296c4012017-05-09 15:02:52 +0200446 cardem_request_pb_and_tx(ci, ac.hdr.ins, tmsg->l3h, msgb_l3len(tmsg));
447 cardem_request_sw_tx(ci, ac.sw);
Harald Welte095ac6c2016-03-19 13:39:33 +0100448 } else if (ac.lc.tot > ac.lc.cur) {
Harald Welte296c4012017-05-09 15:02:52 +0200449 cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur);
Harald Welte095ac6c2016-03-19 13:39:33 +0100450 }
451 return 0;
452}
453
Harald Welte25a9a802017-05-08 13:30:09 +0200454#if 0
455 case SIMTRACE_CMD_DO_ERROR
456 rc = process_do_error(ci, buf, len);
457 break;
458#endif
459
Harald Welte095ac6c2016-03-19 13:39:33 +0100460/*! \brief Process an incoming message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100461static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100462{
Harald Welte25a9a802017-05-08 13:30:09 +0200463 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
Harald Welte095ac6c2016-03-19 13:39:33 +0100464 int rc;
465
466 printf("-> %s\n", osmo_hexdump(buf, len));
467
Harald Welte25a9a802017-05-08 13:30:09 +0200468 buf += sizeof(*sh);
469
Harald Welte095ac6c2016-03-19 13:39:33 +0100470 switch (sh->msg_type) {
Harald Welte25a9a802017-05-08 13:30:09 +0200471 case SIMTRACE_MSGT_BD_CEMU_STATUS:
Harald Welte2ba03bb2017-03-06 18:51:39 +0100472 rc = process_do_status(ci, buf, len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100473 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200474 case SIMTRACE_MSGT_DO_CEMU_PTS:
Harald Welte2ba03bb2017-03-06 18:51:39 +0100475 rc = process_do_pts(ci, buf, len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100476 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200477 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
Harald Welte2ba03bb2017-03-06 18:51:39 +0100478 rc = process_do_rx_da(ci, buf, len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100479 break;
480 default:
481 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
482 rc = -1;
483 break;
484 }
485
486 return rc;
487}
488
489static void print_welcome(void)
490{
491 printf("simtrace2-remsim - Remote SIM card forwarding\n"
Harald Welteb170ea92017-03-06 18:59:41 +0100492 "(C) 2010-2017 by Harald Welte <laforge@gnumonks.org>\n\n");
Harald Welte095ac6c2016-03-19 13:39:33 +0100493}
494
495static void print_help(void)
496{
Harald Welte62bfd8a2017-03-06 20:56:14 +0100497 printf( "\t-r\t--remote-udp-host HOST\n"
498 "\t-p\t--remote-udp-port PORT\n"
Harald Welte095ac6c2016-03-19 13:39:33 +0100499 "\t-h\t--help\n"
Harald Welte62bfd8a2017-03-06 20:56:14 +0100500 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
501 "\t-a\t--skip-atr\n"
Harald Welte095ac6c2016-03-19 13:39:33 +0100502 "\t-k\t--keep-running\n"
Harald Welte822d66e2017-03-06 20:58:03 +0100503 "\t-V\t--usb-vendor\tVENDOR_ID\n"
504 "\t-P\t--usb-product\tPRODUCT_ID\n"
505 "\t-C\t--usb-config\tCONFIG_ID\n"
506 "\t-I\t--usb-interface\tINTERFACE_ID\n"
507 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
508 "\t-A\t--usb-address\tADDRESS\n"
Harald Welte095ac6c2016-03-19 13:39:33 +0100509 "\n"
510 );
511}
512
513static const struct option opts[] = {
Harald Welte62bfd8a2017-03-06 20:56:14 +0100514 { "remote-udp-host", 1, 0, 'r' },
515 { "remote-udp-port", 1, 0, 'p' },
Harald Welte095ac6c2016-03-19 13:39:33 +0100516 { "gsmtap-ip", 1, 0, 'i' },
517 { "skip-atr", 0, 0, 'a' },
518 { "help", 0, 0, 'h' },
519 { "keep-running", 0, 0, 'k' },
Harald Welte822d66e2017-03-06 20:58:03 +0100520 { "usb-vendor", 1, 0, 'V' },
521 { "usb-product", 1, 0, 'P' },
522 { "usb-config", 1, 0, 'C' },
523 { "usb-interface", 1, 0, 'I' },
524 { "usb-altsetting", 1, 0, 'S' },
525 { "usb-address", 1, 0, 'A' },
Harald Weltea6016352017-05-11 01:31:45 +0200526 { "usb-path", 1, 0, 'H' },
Harald Welte095ac6c2016-03-19 13:39:33 +0100527 { NULL, 0, 0, 0 }
528};
529
Harald Welte2ba03bb2017-03-06 18:51:39 +0100530static void run_mainloop(struct cardem_inst *ci)
Harald Welte095ac6c2016-03-19 13:39:33 +0100531{
Harald Welted0396052017-05-10 22:19:26 +0200532 struct st_transport *transp = ci->slot->transp;
Harald Welte095ac6c2016-03-19 13:39:33 +0100533 unsigned int msg_count, byte_count = 0;
Harald Welteb170ea92017-03-06 18:59:41 +0100534 uint8_t buf[16*265];
Harald Welte095ac6c2016-03-19 13:39:33 +0100535 int xfer_len;
536 int rc;
537
538 printf("Entering main loop\n");
539
540 while (1) {
541 /* read data from SIMtrace2 device (local or via USB) */
Harald Welte08b77ba2017-05-09 15:11:53 +0200542 if (transp->udp_fd < 0) {
543 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
Harald Welte2ba03bb2017-03-06 18:51:39 +0100544 buf, sizeof(buf), &xfer_len, 100000);
Harald Welte37ad41e2017-05-11 01:13:58 +0200545 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
546 rc != LIBUSB_ERROR_INTERRUPTED &&
547 rc != LIBUSB_ERROR_IO) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100548 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
549 return;
550 }
551 } else {
Harald Welte08b77ba2017-05-09 15:11:53 +0200552 rc = read(transp->udp_fd, buf, sizeof(buf));
Harald Welte095ac6c2016-03-19 13:39:33 +0100553 if (rc <= 0) {
554 fprintf(stderr, "shor read from UDP\n");
555 return;
556 }
557 xfer_len = rc;
558 }
559 /* dispatch any incoming data */
560 if (xfer_len > 0) {
Harald Welte296c4012017-05-09 15:02:52 +0200561 printf("URB: %s\n", osmo_hexdump(buf, rc));
Harald Welte2ba03bb2017-03-06 18:51:39 +0100562 process_usb_msg(ci, buf, xfer_len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100563 msg_count++;
564 byte_count += xfer_len;
565 }
566 }
567}
568
Harald Welted0396052017-05-10 22:19:26 +0200569static struct st_transport _transp;
570
571static struct st_slot _slot = {
572 .transp = &_transp,
573 .slot_nr = 0,
574};
575
576struct cardem_inst _ci = {
577 .slot = &_slot,
578};
579
580struct cardem_inst *ci = &_ci;
Harald Welte2ba03bb2017-03-06 18:51:39 +0100581
Harald Welte1871c252016-03-20 15:30:46 +0100582static void signal_handler(int signal)
583{
584 switch (signal) {
585 case SIGINT:
Harald Welte296c4012017-05-09 15:02:52 +0200586 cardem_request_card_insert(ci, false);
Harald Welte1871c252016-03-20 15:30:46 +0100587 exit(0);
588 break;
589 default:
590 break;
591 }
592}
593
Harald Welte095ac6c2016-03-19 13:39:33 +0100594int main(int argc, char **argv)
595{
Harald Welted0396052017-05-10 22:19:26 +0200596 struct st_transport *transp = ci->slot->transp;
Harald Welte095ac6c2016-03-19 13:39:33 +0100597 char *gsmtap_host = "127.0.0.1";
598 int rc;
599 int c, ret = 1;
600 int skip_atr = 0;
601 int keep_running = 0;
602 int remote_udp_port = 52342;
Harald Welte822d66e2017-03-06 20:58:03 +0100603 int if_num = 0, vendor_id = -1, product_id = -1;
604 int config_id = -1, altsetting = 0, addr = -1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100605 char *remote_udp_host = NULL;
Harald Weltea6016352017-05-11 01:31:45 +0200606 char *path = NULL;
Harald Welte095ac6c2016-03-19 13:39:33 +0100607 struct osim_reader_hdl *reader;
608 struct osim_card_hdl *card;
609
610 print_welcome();
611
612 while (1) {
613 int option_index = 0;
614
Harald Weltea6016352017-05-11 01:31:45 +0200615 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 +0100616 if (c == -1)
617 break;
618 switch (c) {
619 case 'r':
620 remote_udp_host = optarg;
621 break;
622 case 'p':
623 remote_udp_port = atoi(optarg);
624 break;
625 case 'h':
626 print_help();
627 exit(0);
628 break;
629 case 'i':
630 gsmtap_host = optarg;
631 break;
632 case 'a':
633 skip_atr = 1;
634 break;
635 case 'k':
636 keep_running = 1;
637 break;
Harald Welte822d66e2017-03-06 20:58:03 +0100638 case 'V':
639 vendor_id = strtol(optarg, NULL, 16);
640 break;
641 case 'P':
642 product_id = strtol(optarg, NULL, 16);
643 break;
644 case 'C':
645 config_id = atoi(optarg);
646 break;
647 case 'I':
648 if_num = atoi(optarg);
649 break;
650 case 'S':
651 altsetting = atoi(optarg);
652 break;
653 case 'A':
654 addr = atoi(optarg);
655 break;
Harald Weltea6016352017-05-11 01:31:45 +0200656 case 'H':
657 path = optarg;
658 break;
Harald Welte095ac6c2016-03-19 13:39:33 +0100659 }
660 }
661
Harald Welte822d66e2017-03-06 20:58:03 +0100662 if (!remote_udp_host && (vendor_id < 0 || product_id < 0)) {
663 fprintf(stderr, "You have to specify the vendor and product ID\n");
664 goto do_exit;
665 }
666
Harald Welte08b77ba2017-05-09 15:11:53 +0200667 transp->udp_fd = -1;
Harald Welte2ba03bb2017-03-06 18:51:39 +0100668
669 ci->card_prof = &osim_uicc_sim_cic_profile;
Harald Welte095ac6c2016-03-19 13:39:33 +0100670
671 if (!remote_udp_host) {
672 rc = libusb_init(NULL);
673 if (rc < 0) {
674 fprintf(stderr, "libusb initialization failed\n");
675 goto do_exit;
676 }
677 } else {
Harald Welte08b77ba2017-05-09 15:11:53 +0200678 transp->udp_fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
679 remote_udp_host, remote_udp_port+if_num,
680 OSMO_SOCK_F_CONNECT);
681 if (transp->udp_fd < 0) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100682 fprintf(stderr, "error binding UDP port\n");
683 goto do_exit;
684 }
685 }
686
687 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
688 if (!g_gti) {
689 perror("unable to open GSMTAP");
690 goto close_exit;
691 }
692 gsmtap_source_add_sink(g_gti);
693
694 reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL);
695 if (!reader) {
696 perror("unable to open PC/SC reader");
697 goto close_exit;
698 }
699
700 card = osim_card_open(reader, OSIM_PROTO_T0);
701 if (!card) {
702 perror("unable to open SIM card");
703 goto close_exit;
704 }
705
Harald Welte2ba03bb2017-03-06 18:51:39 +0100706 ci->chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
707 if (!ci->chan) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100708 perror("SIM card has no channel?!?");
709 goto close_exit;
710 }
711
Harald Welte1871c252016-03-20 15:30:46 +0100712 signal(SIGINT, &signal_handler);
713
Harald Welte095ac6c2016-03-19 13:39:33 +0100714 do {
Harald Welte08b77ba2017-05-09 15:11:53 +0200715 if (transp->udp_fd < 0) {
Harald Welte822d66e2017-03-06 20:58:03 +0100716 struct usb_interface_match _ifm, *ifm = &_ifm;
717 ifm->vendor = vendor_id;
718 ifm->product = product_id;
719 ifm->configuration = config_id;
720 ifm->interface = if_num;
721 ifm->altsetting = altsetting;
722 ifm->addr = addr;
Harald Weltea6016352017-05-11 01:31:45 +0200723 if (path)
724 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
Harald Welte08b77ba2017-05-09 15:11:53 +0200725 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
726 if (!transp->usb_devh) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100727 fprintf(stderr, "can't open USB device\n");
728 goto close_exit;
729 }
730
Harald Welte08b77ba2017-05-09 15:11:53 +0200731 rc = libusb_claim_interface(transp->usb_devh, if_num);
Harald Welte095ac6c2016-03-19 13:39:33 +0100732 if (rc < 0) {
Harald Welte236caf62016-03-19 21:28:09 +0100733 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
734 goto close_exit;
735 }
736
Harald Welte08b77ba2017-05-09 15:11:53 +0200737 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
738 &transp->usb_ep.in, &transp->usb_ep.irq_in);
Harald Welte236caf62016-03-19 21:28:09 +0100739 if (rc < 0) {
740 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
Harald Welte095ac6c2016-03-19 13:39:33 +0100741 goto close_exit;
742 }
743 }
744
Harald Welte66de8302017-05-11 01:12:50 +0200745 /* simulate card-insert to modem (owhw, not qmod) */
Harald Welte296c4012017-05-09 15:02:52 +0200746 cardem_request_card_insert(ci, true);
Harald Welte66de8302017-05-11 01:12:50 +0200747
748 /* select remote (forwarded) SIM */
749 st_modem_sim_select_remote(ci->slot);
750
751 /* set the ATR */
Harald Welte9daaa792016-03-20 15:01:20 +0100752 uint8_t real_atr[] = { 0x3B, 0x9F, 0x96, 0x80, 0x1F, 0xC7, 0x80, 0x31,
753 0xA0, 0x73, 0xBE, 0x21, 0x13, 0x67, 0x43, 0x20,
754 0x07, 0x18, 0x00, 0x00, 0x01, 0xA5 };
755 atr_update_csum(real_atr, sizeof(real_atr));
Harald Welte296c4012017-05-09 15:02:52 +0200756 cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
Harald Welte095ac6c2016-03-19 13:39:33 +0100757
Harald Welte66de8302017-05-11 01:12:50 +0200758 /* select remote (forwarded) SIM */
759 st_modem_reset_pulse(ci->slot, 300);
760
Harald Welte2ba03bb2017-03-06 18:51:39 +0100761 run_mainloop(ci);
Harald Welte095ac6c2016-03-19 13:39:33 +0100762 ret = 0;
763
Harald Welte08b77ba2017-05-09 15:11:53 +0200764 if (transp->udp_fd < 0)
765 libusb_release_interface(transp->usb_devh, 0);
Harald Welte095ac6c2016-03-19 13:39:33 +0100766close_exit:
Harald Welte08b77ba2017-05-09 15:11:53 +0200767 if (transp->usb_devh)
768 libusb_close(transp->usb_devh);
Harald Welte095ac6c2016-03-19 13:39:33 +0100769 if (keep_running)
770 sleep(1);
771 } while (keep_running);
772
Harald Welte08b77ba2017-05-09 15:11:53 +0200773 if (transp->udp_fd < 0)
Harald Welte095ac6c2016-03-19 13:39:33 +0100774 libusb_exit(NULL);
775do_exit:
776 return ret;
777}