blob: df38f77822a2f496c0465530ddaefb2926be0ab7 [file] [log] [blame]
Harald Welte095ac6c2016-03-19 13:39:33 +01001/* simtrace2-remsim - main program for the host PC
2 *
Harald Welte2ba03bb2017-03-06 18:51:39 +01003 * (C) 2010-2017 by Harald Welte <hwelte@hmw-consulting.de>
Harald Welte095ac6c2016-03-19 13:39:33 +01004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19#include <errno.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdint.h>
Harald Welte1871c252016-03-20 15:30:46 +010025#include <signal.h>
Harald Welte095ac6c2016-03-19 13:39:33 +010026#include <time.h>
27#define _GNU_SOURCE
28#include <getopt.h>
29
30#include <sys/time.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35
36#include <libusb.h>
37
Harald Welte822d66e2017-03-06 20:58:03 +010038#include "libusb_util.h"
Harald Welte095ac6c2016-03-19 13:39:33 +010039#include "simtrace.h"
Harald Welte25a9a802017-05-08 13:30:09 +020040#include "simtrace_prot.h"
Harald Welte095ac6c2016-03-19 13:39:33 +010041#include "apdu_dispatch.h"
Harald Welte236caf62016-03-19 21:28:09 +010042#include "simtrace2-discovery.h"
Harald Welte095ac6c2016-03-19 13:39:33 +010043
44#include <osmocom/core/gsmtap.h>
45#include <osmocom/core/gsmtap_util.h>
46#include <osmocom/core/utils.h>
47#include <osmocom/core/socket.h>
Harald Welte25a9a802017-05-08 13:30:09 +020048#include <osmocom/core/msgb.h>
Harald Welte095ac6c2016-03-19 13:39:33 +010049#include <osmocom/sim/class_tables.h>
50#include <osmocom/sim/sim.h>
51
Harald Welte08b77ba2017-05-09 15:11:53 +020052/* transport to a SIMtrace device */
Harald Welted0396052017-05-10 22:19:26 +020053struct st_transport {
Harald Welte08b77ba2017-05-09 15:11:53 +020054 /* USB */
Harald Welte2ba03bb2017-03-06 18:51:39 +010055 struct libusb_device_handle *usb_devh;
56 struct {
57 uint8_t in;
58 uint8_t out;
59 uint8_t irq_in;
60 } usb_ep;
Harald Welte2ba03bb2017-03-06 18:51:39 +010061
Harald Welte08b77ba2017-05-09 15:11:53 +020062 /* UDP */
Harald Welte2ba03bb2017-03-06 18:51:39 +010063 int udp_fd;
Harald Welte08b77ba2017-05-09 15:11:53 +020064};
65
Harald Welted0396052017-05-10 22:19:26 +020066/* a SIMtrace slot; communicates over a transport */
67struct st_slot {
68 /* transport through which the slot can be reached */
69 struct st_transport *transp;
70 /* number of the slot within the transport */
71 uint8_t slot_nr;
72};
73
Harald Welte08b77ba2017-05-09 15:11:53 +020074/* One istance of card emulation */
75struct cardem_inst {
Harald Welted0396052017-05-10 22:19:26 +020076 /* slot on which this card emulation instance runs */
77 struct st_slot *slot;
78 /* libosmosim SIM card profile */
Harald Welte08b77ba2017-05-09 15:11:53 +020079 const struct osim_cla_ins_card_profile *card_prof;
Harald Welted0396052017-05-10 22:19:26 +020080 /* libosmosim SIM card channel */
Harald Welte2ba03bb2017-03-06 18:51:39 +010081 struct osim_chan_hdl *chan;
82};
Harald Welte095ac6c2016-03-19 13:39:33 +010083
Harald Welted0396052017-05-10 22:19:26 +020084/* global GSMTAP instance */
85static struct gsmtap_inst *g_gti;
86
Harald Welte095ac6c2016-03-19 13:39:33 +010087static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
88{
89 struct gsmtap_hdr *gh;
90 unsigned int gross_len = len + sizeof(*gh);
91 uint8_t *buf = malloc(gross_len);
92 int rc;
93
94 if (!buf)
95 return -ENOMEM;
96
97 memset(buf, 0, sizeof(*gh));
98 gh = (struct gsmtap_hdr *) buf;
99 gh->version = GSMTAP_VERSION;
100 gh->hdr_len = sizeof(*gh)/4;
101 gh->type = GSMTAP_TYPE_SIM;
102
103 memcpy(buf + sizeof(*gh), apdu, len);
104
105 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
106 if (rc < 0) {
107 perror("write gsmtap");
108 free(buf);
109 return rc;
110 }
111
112 free(buf);
113 return 0;
114}
115
Harald Welte296c4012017-05-09 15:02:52 +0200116/***********************************************************************
117 * SIMTRACE pcore protocol
118 ***********************************************************************/
119
Harald Welted0396052017-05-10 22:19:26 +0200120/*! \brief allocate a message buffer for simtrace use */
Harald Welte25a9a802017-05-08 13:30:09 +0200121static struct msgb *st_msgb_alloc(void)
122{
123 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
124}
125
Harald Welte095ac6c2016-03-19 13:39:33 +0100126#if 0
127static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
128{
129 printf("APDU: %s\n", osmo_hexdump(buf, len));
130 gsmtap_send_sim(buf, len);
131}
132#endif
133
134/*! \brief Transmit a given command to the SIMtrace2 device */
Harald Welted0396052017-05-10 22:19:26 +0200135int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
Harald Welte095ac6c2016-03-19 13:39:33 +0100136{
Harald Welte25a9a802017-05-08 13:30:09 +0200137 int rc;
Harald Welte095ac6c2016-03-19 13:39:33 +0100138
Harald Welte25a9a802017-05-08 13:30:09 +0200139 printf("<- %s\n", msgb_hexdump(msg));
Harald Welte095ac6c2016-03-19 13:39:33 +0100140
Harald Welte08b77ba2017-05-09 15:11:53 +0200141 if (transp->udp_fd < 0) {
Harald Welte9f38ddd2017-05-10 22:49:02 +0200142 int xfer_len;
Harald Welte25a9a802017-05-08 13:30:09 +0200143
Harald Welte08b77ba2017-05-09 15:11:53 +0200144 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
145 msgb_data(msg), msgb_length(msg),
146 &xfer_len, 100000);
Harald Welte095ac6c2016-03-19 13:39:33 +0100147 } else {
Harald Welte08b77ba2017-05-09 15:11:53 +0200148 rc = write(transp->udp_fd, msgb_data(msg), msgb_length(msg));
Harald Welte095ac6c2016-03-19 13:39:33 +0100149 }
Harald Welte25a9a802017-05-08 13:30:09 +0200150
151 msgb_free(msg);
152 return rc;
153}
154
Harald Welted0396052017-05-10 22:19:26 +0200155static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
156 uint8_t slot_nr)
Harald Welte25a9a802017-05-08 13:30:09 +0200157{
Harald Welte9f38ddd2017-05-10 22:49:02 +0200158 struct simtrace_msg_hdr *sh;
Harald Welte25a9a802017-05-08 13:30:09 +0200159
Harald Welte9f38ddd2017-05-10 22:49:02 +0200160 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
Harald Welte25a9a802017-05-08 13:30:09 +0200161 memset(sh, 0, sizeof(*sh));
162 sh->msg_class = msg_class;
163 sh->msg_type = msg_type;
Harald Welted0396052017-05-10 22:19:26 +0200164 sh->slot_nr = slot_nr;
Harald Welte25a9a802017-05-08 13:30:09 +0200165 sh->msg_len = msgb_length(msg);
Harald Welte9f38ddd2017-05-10 22:49:02 +0200166
167 return sh;
Harald Welte095ac6c2016-03-19 13:39:33 +0100168}
169
Harald Welted0396052017-05-10 22:19:26 +0200170/* transmit a given message to a specified slot. Expects all headers
171 * present before calling the function */
172int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
173 uint8_t msg_class, uint8_t msg_type)
174{
Harald Welte9f38ddd2017-05-10 22:49:02 +0200175 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *) msg->data;
Harald Welted0396052017-05-10 22:19:26 +0200176
177 sh->slot_nr = slot->slot_nr;
178
179 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
180
181 return st_transp_tx_msg(slot->transp, msg);
182}
183
184
Harald Welte296c4012017-05-09 15:02:52 +0200185/***********************************************************************
186 * Card Emulation protocol
187 ***********************************************************************/
188
189
Harald Welte095ac6c2016-03-19 13:39:33 +0100190/*! \brief Request the SIMtrace2 to generate a card-insert signal */
Harald Welte296c4012017-05-09 15:02:52 +0200191static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
Harald Welte095ac6c2016-03-19 13:39:33 +0100192{
Harald Welte25a9a802017-05-08 13:30:09 +0200193 struct msgb *msg = st_msgb_alloc();
194 struct cardemu_usb_msg_cardinsert *cins;
Harald Welte095ac6c2016-03-19 13:39:33 +0100195
Harald Welte25a9a802017-05-08 13:30:09 +0200196 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
197 memset(cins, 0, sizeof(*cins));
Harald Welte095ac6c2016-03-19 13:39:33 +0100198 if (inserted)
Harald Welte25a9a802017-05-08 13:30:09 +0200199 cins->card_insert = 1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100200
Harald Welted0396052017-05-10 22:19:26 +0200201 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
Harald Welte095ac6c2016-03-19 13:39:33 +0100202}
203
204/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
Harald Welte296c4012017-05-09 15:02:52 +0200205static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
Harald Welte095ac6c2016-03-19 13:39:33 +0100206{
Harald Welte25a9a802017-05-08 13:30:09 +0200207 struct msgb *msg = st_msgb_alloc();
Harald Welte095ac6c2016-03-19 13:39:33 +0100208 struct cardemu_usb_msg_tx_data *txd;
Harald Welte25a9a802017-05-08 13:30:09 +0200209 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100210
Harald Welte296c4012017-05-09 15:02:52 +0200211 printf("<= %s(%02x, %d)\n", __func__, pb, le);
Harald Welte095ac6c2016-03-19 13:39:33 +0100212
213 memset(txd, 0, sizeof(*txd));
214 txd->data_len = 1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100215 txd->flags = CEMU_DATA_F_PB_AND_RX;
Harald Welte25a9a802017-05-08 13:30:09 +0200216 /* one data byte */
217 msgb_put_u8(msg, pb);
Harald Welte095ac6c2016-03-19 13:39:33 +0100218
Harald Welted0396052017-05-10 22:19:26 +0200219 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte095ac6c2016-03-19 13:39:33 +0100220}
221
222/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
Harald Welte296c4012017-05-09 15:02:52 +0200223static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
224 const uint8_t *data, uint8_t data_len_in)
Harald Welte095ac6c2016-03-19 13:39:33 +0100225{
Harald Welte25a9a802017-05-08 13:30:09 +0200226 struct msgb *msg = st_msgb_alloc();
Harald Welte095ac6c2016-03-19 13:39:33 +0100227 struct cardemu_usb_msg_tx_data *txd;
Harald Welte25a9a802017-05-08 13:30:09 +0200228 uint8_t *cur;
229
230 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100231
Harald Welte296c4012017-05-09 15:02:52 +0200232 printf("<= %s(%02x, %s, %d)\n", __func__, pb,
233 osmo_hexdump(data, data_len_in), data_len_in);
Harald Welte095ac6c2016-03-19 13:39:33 +0100234
235 memset(txd, 0, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100236 txd->data_len = 1 + data_len_in;
237 txd->flags = CEMU_DATA_F_PB_AND_TX;
Harald Welte25a9a802017-05-08 13:30:09 +0200238 /* procedure byte */
239 msgb_put_u8(msg, pb);
240 /* data */
241 cur = msgb_put(msg, data_len_in);
242 memcpy(cur, data, data_len_in);
Harald Welte095ac6c2016-03-19 13:39:33 +0100243
Harald Welted0396052017-05-10 22:19:26 +0200244 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte095ac6c2016-03-19 13:39:33 +0100245}
246
247/*! \brief Request the SIMtrace2 to send a Status Word */
Harald Welte296c4012017-05-09 15:02:52 +0200248static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
Harald Welte095ac6c2016-03-19 13:39:33 +0100249{
Harald Welte25a9a802017-05-08 13:30:09 +0200250 struct msgb *msg = st_msgb_alloc();
Harald Welte095ac6c2016-03-19 13:39:33 +0100251 struct cardemu_usb_msg_tx_data *txd;
Harald Welte25a9a802017-05-08 13:30:09 +0200252 uint8_t *cur;
253
254 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100255
Harald Welte296c4012017-05-09 15:02:52 +0200256 printf("<= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Harald Welte095ac6c2016-03-19 13:39:33 +0100257
258 memset(txd, 0, sizeof(*txd));
Harald Welte095ac6c2016-03-19 13:39:33 +0100259 txd->data_len = 2;
260 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
Harald Welte25a9a802017-05-08 13:30:09 +0200261 cur = msgb_put(msg, 2);
262 cur[0] = sw[0];
263 cur[1] = sw[1];
Harald Welte095ac6c2016-03-19 13:39:33 +0100264
Harald Welted0396052017-05-10 22:19:26 +0200265 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
Harald Welte095ac6c2016-03-19 13:39:33 +0100266}
267
Harald Welte9daaa792016-03-20 15:01:20 +0100268static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
269{
270 uint8_t csum = 0;
271 int i;
272
273 for (i = 1; i < atr_len - 1; i++)
274 csum = csum ^ atr[i];
275
276 atr[atr_len-1] = csum;
277}
278
Harald Welte296c4012017-05-09 15:02:52 +0200279static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
Harald Welte9daaa792016-03-20 15:01:20 +0100280{
Harald Welte25a9a802017-05-08 13:30:09 +0200281 struct msgb *msg = st_msgb_alloc();
Harald Welte9daaa792016-03-20 15:01:20 +0100282 struct cardemu_usb_msg_set_atr *satr;
Harald Welte25a9a802017-05-08 13:30:09 +0200283 uint8_t *cur;
284
285 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
Harald Welte9daaa792016-03-20 15:01:20 +0100286
Harald Welte296c4012017-05-09 15:02:52 +0200287 printf("<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Harald Welte9daaa792016-03-20 15:01:20 +0100288
289 memset(satr, 0, sizeof(*satr));
Harald Welte9daaa792016-03-20 15:01:20 +0100290 satr->atr_len = atr_len;
Harald Welte25a9a802017-05-08 13:30:09 +0200291 cur = msgb_put(msg, atr_len);
292 memcpy(cur, atr, atr_len);
Harald Welte9daaa792016-03-20 15:01:20 +0100293
Harald Welted0396052017-05-10 22:19:26 +0200294 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
Harald Welte9daaa792016-03-20 15:01:20 +0100295}
296
Harald Welte296c4012017-05-09 15:02:52 +0200297/***********************************************************************
Harald Welte66de8302017-05-11 01:12:50 +0200298 * Modem Control protocol
299 ***********************************************************************/
300
301static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
302{
303 struct msgb *msg = st_msgb_alloc();
304 struct st_modem_reset *sr ;
305
306 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
307 sr->asserted = asserted;
308 sr->pulse_duration_msec = pulse_ms;
309
310 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
311}
312
313/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
314int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
315{
316 return _modem_reset(slot, 2, duration_ms);
317}
318
319/*! \brief assert the RESET line of the modem */
320int st_modem_reset_active(struct st_slot *slot)
321{
322 return _modem_reset(slot, 1, 0);
323}
324
325/*! \brief de-assert the RESET line of the modem */
326int st_modem_reset_inactive(struct st_slot *slot)
327{
328 return _modem_reset(slot, 0, 0);
329}
330
331static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
332{
333 struct msgb *msg = st_msgb_alloc();
334 struct st_modem_sim_select *ss;
335
336 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
337 ss->remote_sim = remote_sim;
338
339 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
340}
341
342/*! \brief select local (physical) SIM for given slot */
343int st_modem_sim_select_local(struct st_slot *slot)
344{
345 return _modem_sim_select(slot, 0);
346}
347
348/*! \brief select remote (emulated/forwarded) SIM for given slot */
349int st_modem_sim_select_remote(struct st_slot *slot)
350{
351 return _modem_sim_select(slot, 1);
352}
353
354/*! \brief Request slot to send us status information about the modem */
355int st_modem_get_status(struct st_slot *slot)
356{
357 struct msgb *msg = st_msgb_alloc();
358
359 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
360}
361
362
363/***********************************************************************
Harald Welte296c4012017-05-09 15:02:52 +0200364 * Incoming Messages
365 ***********************************************************************/
366
Harald Welte095ac6c2016-03-19 13:39:33 +0100367/*! \brief Process a STATUS message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100368static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100369{
370 struct cardemu_usb_msg_status *status;
371 status = (struct cardemu_usb_msg_status *) buf;
372
373 printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
374 status->flags, status->fi, status->di, status->wi,
375 status->waiting_time);
376
377 return 0;
378}
379
380/*! \brief Process a PTS indication message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100381static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100382{
383 struct cardemu_usb_msg_pts_info *pts;
384 pts = (struct cardemu_usb_msg_pts_info *) buf;
385
386 printf("=> PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
387
388 return 0;
389}
390
391/*! \brief Process a ERROR indication message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100392static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100393{
394 struct cardemu_usb_msg_error *err;
395 err = (struct cardemu_usb_msg_error *) buf;
396
397 printf("=> ERROR: %u/%u/%u: %s\n",
398 err->severity, err->subsystem, err->code,
Harald Welteb170ea92017-03-06 18:59:41 +0100399 err->msg_len ? (char *)err->msg : "");
Harald Welte095ac6c2016-03-19 13:39:33 +0100400
401 return 0;
402}
403
404/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100405static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100406{
407 static struct apdu_context ac;
408 struct cardemu_usb_msg_rx_data *data;
Harald Welte095ac6c2016-03-19 13:39:33 +0100409 int rc;
410
411 data = (struct cardemu_usb_msg_rx_data *) buf;
412
413 printf("=> DATA: flags=%x, %s: ", data->flags,
414 osmo_hexdump(data->data, data->data_len));
415
416 rc = apdu_segment_in(&ac, data->data, data->data_len,
417 data->flags & CEMU_DATA_F_TPDU_HDR);
418
419 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) {
420 struct msgb *tmsg = msgb_alloc(1024, "TPDU");
Harald Welte2ba03bb2017-03-06 18:51:39 +0100421 struct osim_reader_hdl *rh = ci->chan->card->reader;
Harald Welte095ac6c2016-03-19 13:39:33 +0100422 uint8_t *cur;
423
424 /* Copy TPDU header */
425 cur = msgb_put(tmsg, sizeof(ac.hdr));
426 memcpy(cur, &ac.hdr, sizeof(ac.hdr));
427 /* Copy D(c), if any */
428 if (ac.lc.tot) {
429 cur = msgb_put(tmsg, ac.lc.tot);
430 memcpy(cur, ac.dc, ac.lc.tot);
431 }
432 /* send to actual card */
433 tmsg->l3h = tmsg->tail;
434 rc = rh->ops->transceive(rh, tmsg);
435 if (rc < 0) {
436 fprintf(stderr, "error during transceive: %d\n", rc);
437 msgb_free(tmsg);
438 return rc;
439 }
440 msgb_apdu_sw(tmsg) = msgb_get_u16(tmsg);
441 ac.sw[0] = msgb_apdu_sw(tmsg) >> 8;
442 ac.sw[1] = msgb_apdu_sw(tmsg) & 0xff;
443 printf("SW=0x%04x, len_rx=%d\n", msgb_apdu_sw(tmsg), msgb_l3len(tmsg));
444 if (msgb_l3len(tmsg))
Harald Welte296c4012017-05-09 15:02:52 +0200445 cardem_request_pb_and_tx(ci, ac.hdr.ins, tmsg->l3h, msgb_l3len(tmsg));
446 cardem_request_sw_tx(ci, ac.sw);
Harald Welte095ac6c2016-03-19 13:39:33 +0100447 } else if (ac.lc.tot > ac.lc.cur) {
Harald Welte296c4012017-05-09 15:02:52 +0200448 cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur);
Harald Welte095ac6c2016-03-19 13:39:33 +0100449 }
450 return 0;
451}
452
Harald Welte25a9a802017-05-08 13:30:09 +0200453#if 0
454 case SIMTRACE_CMD_DO_ERROR
455 rc = process_do_error(ci, buf, len);
456 break;
457#endif
458
Harald Welte095ac6c2016-03-19 13:39:33 +0100459/*! \brief Process an incoming message from the SIMtrace2 */
Harald Welte2ba03bb2017-03-06 18:51:39 +0100460static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
Harald Welte095ac6c2016-03-19 13:39:33 +0100461{
Harald Welte25a9a802017-05-08 13:30:09 +0200462 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
Harald Welte095ac6c2016-03-19 13:39:33 +0100463 int rc;
464
465 printf("-> %s\n", osmo_hexdump(buf, len));
466
Harald Welte25a9a802017-05-08 13:30:09 +0200467 buf += sizeof(*sh);
468
Harald Welte095ac6c2016-03-19 13:39:33 +0100469 switch (sh->msg_type) {
Harald Welte25a9a802017-05-08 13:30:09 +0200470 case SIMTRACE_MSGT_BD_CEMU_STATUS:
Harald Welte2ba03bb2017-03-06 18:51:39 +0100471 rc = process_do_status(ci, buf, len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100472 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200473 case SIMTRACE_MSGT_DO_CEMU_PTS:
Harald Welte2ba03bb2017-03-06 18:51:39 +0100474 rc = process_do_pts(ci, buf, len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100475 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200476 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
Harald Welte2ba03bb2017-03-06 18:51:39 +0100477 rc = process_do_rx_da(ci, buf, len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100478 break;
479 default:
480 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
481 rc = -1;
482 break;
483 }
484
485 return rc;
486}
487
488static void print_welcome(void)
489{
490 printf("simtrace2-remsim - Remote SIM card forwarding\n"
Harald Welteb170ea92017-03-06 18:59:41 +0100491 "(C) 2010-2017 by Harald Welte <laforge@gnumonks.org>\n\n");
Harald Welte095ac6c2016-03-19 13:39:33 +0100492}
493
494static void print_help(void)
495{
Harald Welte62bfd8a2017-03-06 20:56:14 +0100496 printf( "\t-r\t--remote-udp-host HOST\n"
497 "\t-p\t--remote-udp-port PORT\n"
Harald Welte095ac6c2016-03-19 13:39:33 +0100498 "\t-h\t--help\n"
Harald Welte62bfd8a2017-03-06 20:56:14 +0100499 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
500 "\t-a\t--skip-atr\n"
Harald Welte095ac6c2016-03-19 13:39:33 +0100501 "\t-k\t--keep-running\n"
Harald Welte822d66e2017-03-06 20:58:03 +0100502 "\t-V\t--usb-vendor\tVENDOR_ID\n"
503 "\t-P\t--usb-product\tPRODUCT_ID\n"
504 "\t-C\t--usb-config\tCONFIG_ID\n"
505 "\t-I\t--usb-interface\tINTERFACE_ID\n"
506 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
507 "\t-A\t--usb-address\tADDRESS\n"
Harald Welte095ac6c2016-03-19 13:39:33 +0100508 "\n"
509 );
510}
511
512static const struct option opts[] = {
Harald Welte62bfd8a2017-03-06 20:56:14 +0100513 { "remote-udp-host", 1, 0, 'r' },
514 { "remote-udp-port", 1, 0, 'p' },
Harald Welte095ac6c2016-03-19 13:39:33 +0100515 { "gsmtap-ip", 1, 0, 'i' },
516 { "skip-atr", 0, 0, 'a' },
517 { "help", 0, 0, 'h' },
518 { "keep-running", 0, 0, 'k' },
Harald Welte822d66e2017-03-06 20:58:03 +0100519 { "usb-vendor", 1, 0, 'V' },
520 { "usb-product", 1, 0, 'P' },
521 { "usb-config", 1, 0, 'C' },
522 { "usb-interface", 1, 0, 'I' },
523 { "usb-altsetting", 1, 0, 'S' },
524 { "usb-address", 1, 0, 'A' },
Harald Weltea6016352017-05-11 01:31:45 +0200525 { "usb-path", 1, 0, 'H' },
Harald Welte095ac6c2016-03-19 13:39:33 +0100526 { NULL, 0, 0, 0 }
527};
528
Harald Welte2ba03bb2017-03-06 18:51:39 +0100529static void run_mainloop(struct cardem_inst *ci)
Harald Welte095ac6c2016-03-19 13:39:33 +0100530{
Harald Welted0396052017-05-10 22:19:26 +0200531 struct st_transport *transp = ci->slot->transp;
Harald Welte095ac6c2016-03-19 13:39:33 +0100532 unsigned int msg_count, byte_count = 0;
Harald Welteb170ea92017-03-06 18:59:41 +0100533 uint8_t buf[16*265];
Harald Welte095ac6c2016-03-19 13:39:33 +0100534 int xfer_len;
535 int rc;
536
537 printf("Entering main loop\n");
538
539 while (1) {
540 /* read data from SIMtrace2 device (local or via USB) */
Harald Welte08b77ba2017-05-09 15:11:53 +0200541 if (transp->udp_fd < 0) {
542 rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
Harald Welte2ba03bb2017-03-06 18:51:39 +0100543 buf, sizeof(buf), &xfer_len, 100000);
Harald Welte37ad41e2017-05-11 01:13:58 +0200544 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
545 rc != LIBUSB_ERROR_INTERRUPTED &&
546 rc != LIBUSB_ERROR_IO) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100547 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
548 return;
549 }
550 } else {
Harald Welte08b77ba2017-05-09 15:11:53 +0200551 rc = read(transp->udp_fd, buf, sizeof(buf));
Harald Welte095ac6c2016-03-19 13:39:33 +0100552 if (rc <= 0) {
553 fprintf(stderr, "shor read from UDP\n");
554 return;
555 }
556 xfer_len = rc;
557 }
558 /* dispatch any incoming data */
559 if (xfer_len > 0) {
Harald Welte296c4012017-05-09 15:02:52 +0200560 printf("URB: %s\n", osmo_hexdump(buf, rc));
Harald Welte2ba03bb2017-03-06 18:51:39 +0100561 process_usb_msg(ci, buf, xfer_len);
Harald Welte095ac6c2016-03-19 13:39:33 +0100562 msg_count++;
563 byte_count += xfer_len;
564 }
565 }
566}
567
Harald Welted0396052017-05-10 22:19:26 +0200568static struct st_transport _transp;
569
570static struct st_slot _slot = {
571 .transp = &_transp,
572 .slot_nr = 0,
573};
574
575struct cardem_inst _ci = {
576 .slot = &_slot,
577};
578
579struct cardem_inst *ci = &_ci;
Harald Welte2ba03bb2017-03-06 18:51:39 +0100580
Harald Welte1871c252016-03-20 15:30:46 +0100581static void signal_handler(int signal)
582{
583 switch (signal) {
584 case SIGINT:
Harald Welte296c4012017-05-09 15:02:52 +0200585 cardem_request_card_insert(ci, false);
Harald Welte1871c252016-03-20 15:30:46 +0100586 exit(0);
587 break;
588 default:
589 break;
590 }
591}
592
Harald Welte095ac6c2016-03-19 13:39:33 +0100593int main(int argc, char **argv)
594{
Harald Welted0396052017-05-10 22:19:26 +0200595 struct st_transport *transp = ci->slot->transp;
Harald Welte095ac6c2016-03-19 13:39:33 +0100596 char *gsmtap_host = "127.0.0.1";
597 int rc;
598 int c, ret = 1;
599 int skip_atr = 0;
600 int keep_running = 0;
601 int remote_udp_port = 52342;
Harald Welte822d66e2017-03-06 20:58:03 +0100602 int if_num = 0, vendor_id = -1, product_id = -1;
603 int config_id = -1, altsetting = 0, addr = -1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100604 char *remote_udp_host = NULL;
Harald Weltea6016352017-05-11 01:31:45 +0200605 char *path = NULL;
Harald Welte095ac6c2016-03-19 13:39:33 +0100606 struct osim_reader_hdl *reader;
607 struct osim_card_hdl *card;
608
609 print_welcome();
610
611 while (1) {
612 int option_index = 0;
613
Harald Weltea6016352017-05-11 01:31:45 +0200614 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 +0100615 if (c == -1)
616 break;
617 switch (c) {
618 case 'r':
619 remote_udp_host = optarg;
620 break;
621 case 'p':
622 remote_udp_port = atoi(optarg);
623 break;
624 case 'h':
625 print_help();
626 exit(0);
627 break;
628 case 'i':
629 gsmtap_host = optarg;
630 break;
631 case 'a':
632 skip_atr = 1;
633 break;
634 case 'k':
635 keep_running = 1;
636 break;
Harald Welte822d66e2017-03-06 20:58:03 +0100637 case 'V':
638 vendor_id = strtol(optarg, NULL, 16);
639 break;
640 case 'P':
641 product_id = strtol(optarg, NULL, 16);
642 break;
643 case 'C':
644 config_id = atoi(optarg);
645 break;
646 case 'I':
647 if_num = atoi(optarg);
648 break;
649 case 'S':
650 altsetting = atoi(optarg);
651 break;
652 case 'A':
653 addr = atoi(optarg);
654 break;
Harald Weltea6016352017-05-11 01:31:45 +0200655 case 'H':
656 path = optarg;
657 break;
Harald Welte095ac6c2016-03-19 13:39:33 +0100658 }
659 }
660
Harald Welte822d66e2017-03-06 20:58:03 +0100661 if (!remote_udp_host && (vendor_id < 0 || product_id < 0)) {
662 fprintf(stderr, "You have to specify the vendor and product ID\n");
663 goto do_exit;
664 }
665
Harald Welte08b77ba2017-05-09 15:11:53 +0200666 transp->udp_fd = -1;
Harald Welte2ba03bb2017-03-06 18:51:39 +0100667
668 ci->card_prof = &osim_uicc_sim_cic_profile;
Harald Welte095ac6c2016-03-19 13:39:33 +0100669
670 if (!remote_udp_host) {
671 rc = libusb_init(NULL);
672 if (rc < 0) {
673 fprintf(stderr, "libusb initialization failed\n");
674 goto do_exit;
675 }
676 } else {
Harald Welte08b77ba2017-05-09 15:11:53 +0200677 transp->udp_fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
678 remote_udp_host, remote_udp_port+if_num,
679 OSMO_SOCK_F_CONNECT);
680 if (transp->udp_fd < 0) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100681 fprintf(stderr, "error binding UDP port\n");
682 goto do_exit;
683 }
684 }
685
686 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
687 if (!g_gti) {
688 perror("unable to open GSMTAP");
689 goto close_exit;
690 }
691 gsmtap_source_add_sink(g_gti);
692
693 reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL);
694 if (!reader) {
695 perror("unable to open PC/SC reader");
696 goto close_exit;
697 }
698
699 card = osim_card_open(reader, OSIM_PROTO_T0);
700 if (!card) {
701 perror("unable to open SIM card");
702 goto close_exit;
703 }
704
Harald Welte2ba03bb2017-03-06 18:51:39 +0100705 ci->chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
706 if (!ci->chan) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100707 perror("SIM card has no channel?!?");
708 goto close_exit;
709 }
710
Harald Welte1871c252016-03-20 15:30:46 +0100711 signal(SIGINT, &signal_handler);
712
Harald Welte095ac6c2016-03-19 13:39:33 +0100713 do {
Harald Welte08b77ba2017-05-09 15:11:53 +0200714 if (transp->udp_fd < 0) {
Harald Welte822d66e2017-03-06 20:58:03 +0100715 struct usb_interface_match _ifm, *ifm = &_ifm;
716 ifm->vendor = vendor_id;
717 ifm->product = product_id;
718 ifm->configuration = config_id;
719 ifm->interface = if_num;
720 ifm->altsetting = altsetting;
721 ifm->addr = addr;
Harald Weltea6016352017-05-11 01:31:45 +0200722 if (path)
723 osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
Harald Welte08b77ba2017-05-09 15:11:53 +0200724 transp->usb_devh = usb_open_claim_interface(NULL, ifm);
725 if (!transp->usb_devh) {
Harald Welte095ac6c2016-03-19 13:39:33 +0100726 fprintf(stderr, "can't open USB device\n");
727 goto close_exit;
728 }
729
Harald Welte08b77ba2017-05-09 15:11:53 +0200730 rc = libusb_claim_interface(transp->usb_devh, if_num);
Harald Welte095ac6c2016-03-19 13:39:33 +0100731 if (rc < 0) {
Harald Welte236caf62016-03-19 21:28:09 +0100732 fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
733 goto close_exit;
734 }
735
Harald Welte08b77ba2017-05-09 15:11:53 +0200736 rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
737 &transp->usb_ep.in, &transp->usb_ep.irq_in);
Harald Welte236caf62016-03-19 21:28:09 +0100738 if (rc < 0) {
739 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
Harald Welte095ac6c2016-03-19 13:39:33 +0100740 goto close_exit;
741 }
742 }
743
Harald Welte66de8302017-05-11 01:12:50 +0200744 /* simulate card-insert to modem (owhw, not qmod) */
Harald Welte296c4012017-05-09 15:02:52 +0200745 cardem_request_card_insert(ci, true);
Harald Welte66de8302017-05-11 01:12:50 +0200746
747 /* select remote (forwarded) SIM */
748 st_modem_sim_select_remote(ci->slot);
749
750 /* set the ATR */
Harald Welte9daaa792016-03-20 15:01:20 +0100751 uint8_t real_atr[] = { 0x3B, 0x9F, 0x96, 0x80, 0x1F, 0xC7, 0x80, 0x31,
752 0xA0, 0x73, 0xBE, 0x21, 0x13, 0x67, 0x43, 0x20,
753 0x07, 0x18, 0x00, 0x00, 0x01, 0xA5 };
754 atr_update_csum(real_atr, sizeof(real_atr));
Harald Welte296c4012017-05-09 15:02:52 +0200755 cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
Harald Welte095ac6c2016-03-19 13:39:33 +0100756
Harald Welte66de8302017-05-11 01:12:50 +0200757 /* select remote (forwarded) SIM */
758 st_modem_reset_pulse(ci->slot, 300);
759
Harald Welte2ba03bb2017-03-06 18:51:39 +0100760 run_mainloop(ci);
Harald Welte095ac6c2016-03-19 13:39:33 +0100761 ret = 0;
762
Harald Welte08b77ba2017-05-09 15:11:53 +0200763 if (transp->udp_fd < 0)
764 libusb_release_interface(transp->usb_devh, 0);
Harald Welte095ac6c2016-03-19 13:39:33 +0100765close_exit:
Harald Welte08b77ba2017-05-09 15:11:53 +0200766 if (transp->usb_devh)
767 libusb_close(transp->usb_devh);
Harald Welte095ac6c2016-03-19 13:39:33 +0100768 if (keep_running)
769 sleep(1);
770 } while (keep_running);
771
Harald Welte08b77ba2017-05-09 15:11:53 +0200772 if (transp->udp_fd < 0)
Harald Welte095ac6c2016-03-19 13:39:33 +0100773 libusb_exit(NULL);
774do_exit:
775 return ret;
776}