blob: 846ce85455e24dabcfa548a7c3d4b58d7528c37c [file] [log] [blame]
Harald Welte3dcdd202019-03-09 13:06:46 +01001/* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org>
2 * (C) 2018 by sysmocom - s.f.m.c. GmbH, Author: Kevin Redon
3 *
4 * All Rights Reserved
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Kévin Redone0b837c2018-10-10 00:39:25 +020024
25#include <errno.h>
26#include <string.h>
27
28#include <talloc.h>
29
30#include <osmocom/core/msgb.h>
31#include <osmocom/core/fsm.h>
32#include <osmocom/core/utils.h>
33#include <osmocom/core/logging.h>
34#include <osmocom/core/application.h>
35
36#include <osmocom/abis/ipa.h>
37#include <osmocom/gsm/protocol/ipaccess.h>
38
39#include "rspro_util.h"
40#include "client.h"
Harald Welte61d98e92019-03-03 15:43:07 +010041#include "debug.h"
Kévin Redone0b837c2018-10-10 00:39:25 +020042
43#include <unistd.h>
44#include <stdio.h>
Kévin Redon3428e412018-10-11 19:14:00 +020045#include <linux/limits.h>
46#include <sys/stat.h>
47#include <fcntl.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020048#include <signal.h>
49#include <getopt.h>
50
51#include <libusb.h>
52
Harald Weltefd5dafc2019-12-15 21:14:14 +010053#include <osmocom/usb/libusb.h>
54#include <osmocom/simtrace2/simtrace_prot.h>
55#include <osmocom/simtrace2/simtrace_usb.h>
56#include <osmocom/simtrace2/apdu_dispatch.h>
Kévin Redone0b837c2018-10-10 00:39:25 +020057
58#include <osmocom/core/gsmtap.h>
59#include <osmocom/core/gsmtap_util.h>
60#include <osmocom/core/utils.h>
61#include <osmocom/core/socket.h>
62#include <osmocom/core/msgb.h>
63#include <osmocom/sim/class_tables.h>
64#include <osmocom/sim/sim.h>
65
66/* transport to a SIMtrace device */
67struct st_transport {
68 /* USB */
69 struct libusb_device_handle *usb_devh;
70 struct {
71 uint8_t in;
72 uint8_t out;
73 uint8_t irq_in;
74 } usb_ep;
Kévin Redone0b837c2018-10-10 00:39:25 +020075};
76
77/* a SIMtrace slot; communicates over a transport */
78struct st_slot {
79 /* transport through which the slot can be reached */
80 struct st_transport *transp;
81 /* number of the slot within the transport */
82 uint8_t slot_nr;
83};
84
85/* One istance of card emulation */
86struct cardem_inst {
87 /* slot on which this card emulation instance runs */
88 struct st_slot *slot;
Harald Welte943ae622019-12-17 01:12:56 +010089 struct cardemu_usb_msg_status last_status;
Harald Welte499e1d92019-12-17 01:13:53 +010090 char *usb_path;
Kévin Redone0b837c2018-10-10 00:39:25 +020091};
92
93/* global GSMTAP instance */
94static struct gsmtap_inst *g_gti;
95
96static struct bankd_client *g_client;
97static void *g_tall_ctx;
98void __thread *talloc_asn1_ctx;
99int asn_debug;
100
Harald Weltea9bc4de2020-02-16 15:40:21 +0100101/* should we leave main loop processing? */
102bool g_leave_main = false;
103
Harald Welte3f09f632019-03-31 15:51:13 +0200104__attribute__((unused)) static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200105{
106 struct gsmtap_hdr *gh;
107 unsigned int gross_len = len + sizeof(*gh);
108 uint8_t *buf = malloc(gross_len);
109 int rc;
110
111 if (!buf)
112 return -ENOMEM;
113
114 memset(buf, 0, sizeof(*gh));
115 gh = (struct gsmtap_hdr *) buf;
116 gh->version = GSMTAP_VERSION;
117 gh->hdr_len = sizeof(*gh)/4;
118 gh->type = GSMTAP_TYPE_SIM;
119
120 memcpy(buf + sizeof(*gh), apdu, len);
121
122 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
123 if (rc < 0) {
124 perror("write gsmtap");
125 free(buf);
126 return rc;
127 }
128
129 free(buf);
130 return 0;
131}
132
133/***********************************************************************
Harald Weltef14dc042019-03-28 20:29:36 +0100134 * SIMTRACE core protocol
Kévin Redone0b837c2018-10-10 00:39:25 +0200135 ***********************************************************************/
136
137/*! \brief allocate a message buffer for simtrace use */
138static struct msgb *st_msgb_alloc(void)
139{
140 return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
141}
142
143#if 0
144static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data)
145{
146 printf("APDU: %s\n", osmo_hexdump(buf, len));
147 gsmtap_send_sim(buf, len);
148}
149#endif
150
Harald Welte32e2e002019-12-15 23:01:54 +0100151static void usb_out_xfer_cb(struct libusb_transfer *xfer)
152{
153 struct msgb *msg = xfer->user_data;
154
155 switch (xfer->status) {
156 case LIBUSB_TRANSFER_COMPLETED:
157 break;
158 case LIBUSB_TRANSFER_NO_DEVICE:
159 fprintf(stderr, "USB device disappeared\n");
Harald Weltea9bc4de2020-02-16 15:40:21 +0100160 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100161 break;
162 default:
Harald Weltea9bc4de2020-02-16 15:40:21 +0100163 fprintf(stderr, "USB OUT transfer failed, status=%u\n", xfer->status);
164 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100165 break;
166 }
167
168 msgb_free(msg);
169 libusb_free_transfer(xfer);
170}
171
Kévin Redone0b837c2018-10-10 00:39:25 +0200172/*! \brief Transmit a given command to the SIMtrace2 device */
173int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg)
174{
Harald Welte32e2e002019-12-15 23:01:54 +0100175 struct libusb_transfer *xfer;
Kévin Redone0b837c2018-10-10 00:39:25 +0200176 int rc;
177
Kévin Redon21e31de2018-10-11 17:25:58 +0200178 printf("SIMtrace <- %s\n", msgb_hexdump(msg));
Kévin Redone0b837c2018-10-10 00:39:25 +0200179
Harald Welte32e2e002019-12-15 23:01:54 +0100180 xfer = libusb_alloc_transfer(0);
181 OSMO_ASSERT(xfer);
182 xfer->dev_handle = transp->usb_devh;
183 xfer->flags = 0;
184 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
185 xfer->endpoint = transp->usb_ep.out;
186 xfer->timeout = 1000;
187 xfer->user_data = msg;
188 xfer->length = msgb_length(msg);
189 xfer->buffer = msgb_data(msg);
190 xfer->callback = usb_out_xfer_cb;
Kévin Redone0b837c2018-10-10 00:39:25 +0200191
Harald Welte32e2e002019-12-15 23:01:54 +0100192 /* submit the OUT transfer */
193 rc = libusb_submit_transfer(xfer);
194 OSMO_ASSERT(rc == 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200195
Kévin Redone0b837c2018-10-10 00:39:25 +0200196 return rc;
197}
198
199static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type,
200 uint8_t slot_nr)
201{
202 struct simtrace_msg_hdr *sh;
203
204 sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh));
205 memset(sh, 0, sizeof(*sh));
206 sh->msg_class = msg_class;
207 sh->msg_type = msg_type;
208 sh->slot_nr = slot_nr;
209 sh->msg_len = msgb_length(msg);
210
211 return sh;
212}
213
214/* transmit a given message to a specified slot. Expects all headers
215 * present before calling the function */
216int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg,
217 uint8_t msg_class, uint8_t msg_type)
218{
219 st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
220
221 return st_transp_tx_msg(slot->transp, msg);
222}
223
224/***********************************************************************
225 * Card Emulation protocol
226 ***********************************************************************/
227
228
229/*! \brief Request the SIMtrace2 to generate a card-insert signal */
230static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted)
231{
232 struct msgb *msg = st_msgb_alloc();
233 struct cardemu_usb_msg_cardinsert *cins;
234
235 cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
236 memset(cins, 0, sizeof(*cins));
237 if (inserted)
238 cins->card_insert = 1;
239
240 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT);
241}
242
243/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */
244static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le)
245{
246 struct msgb *msg = st_msgb_alloc();
247 struct cardemu_usb_msg_tx_data *txd;
248 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
249
Kévin Redon21e31de2018-10-11 17:25:58 +0200250 printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le);
Kévin Redone0b837c2018-10-10 00:39:25 +0200251
252 memset(txd, 0, sizeof(*txd));
253 txd->data_len = 1;
254 txd->flags = CEMU_DATA_F_PB_AND_RX;
255 /* one data byte */
256 msgb_put_u8(msg, pb);
257
258 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
259}
260
261/*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */
262static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb,
Kévin Redonf120b642018-10-15 19:53:02 +0200263 const uint8_t *data, uint16_t data_len_in)
Kévin Redone0b837c2018-10-10 00:39:25 +0200264{
265 struct msgb *msg = st_msgb_alloc();
266 struct cardemu_usb_msg_tx_data *txd;
267 uint8_t *cur;
268
269 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
270
Kévin Redon21e31de2018-10-11 17:25:58 +0200271 printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb,
Kévin Redone0b837c2018-10-10 00:39:25 +0200272 osmo_hexdump(data, data_len_in), data_len_in);
273
274 memset(txd, 0, sizeof(*txd));
275 txd->data_len = 1 + data_len_in;
276 txd->flags = CEMU_DATA_F_PB_AND_TX;
277 /* procedure byte */
278 msgb_put_u8(msg, pb);
279 /* data */
280 cur = msgb_put(msg, data_len_in);
281 memcpy(cur, data, data_len_in);
282
283 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
284}
285
286/*! \brief Request the SIMtrace2 to send a Status Word */
287static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw)
288{
289 struct msgb *msg = st_msgb_alloc();
290 struct cardemu_usb_msg_tx_data *txd;
291 uint8_t *cur;
292
293 txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
294
Kévin Redon21e31de2018-10-11 17:25:58 +0200295 printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
Kévin Redone0b837c2018-10-10 00:39:25 +0200296
297 memset(txd, 0, sizeof(*txd));
298 txd->data_len = 2;
299 txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL;
300 cur = msgb_put(msg, 2);
301 cur[0] = sw[0];
302 cur[1] = sw[1];
303
304 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA);
305}
306
Harald Welte0e0e9322019-12-16 12:47:18 +0100307/*! \brief Request the SIMtrace2 to send a Status Word */
308static int cardem_request_config(struct cardem_inst *ci, uint32_t features)
309{
310 struct msgb *msg = st_msgb_alloc();
311 struct cardemu_usb_msg_config *cfg;
312
313 cfg = (struct cardemu_usb_msg_config *) msgb_put(msg, sizeof(*cfg));
314
315 printf("SIMtrace <= %s(%08x)\n", __func__, features);
316
317 memset(cfg, 0, sizeof(*cfg));
318 cfg->features = features;
319
320 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_BD_CEMU_CONFIG);
321}
322
Kévin Redon206c3d72018-11-12 22:51:37 +0100323// FIXME check if the ATR actually includes a checksum
Harald Welte3f09f632019-03-31 15:51:13 +0200324__attribute__((unused)) static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200325{
326 uint8_t csum = 0;
327 int i;
328
329 for (i = 1; i < atr_len - 1; i++)
330 csum = csum ^ atr[i];
331
332 atr[atr_len-1] = csum;
333}
334
335static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len)
336{
337 struct msgb *msg = st_msgb_alloc();
338 struct cardemu_usb_msg_set_atr *satr;
339 uint8_t *cur;
340
341 satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
342
Kévin Redon21e31de2018-10-11 17:25:58 +0200343 printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200344
345 memset(satr, 0, sizeof(*satr));
346 satr->atr_len = atr_len;
347 cur = msgb_put(msg, atr_len);
348 memcpy(cur, atr, atr_len);
349
350 return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR);
351}
352
353/***********************************************************************
354 * Modem Control protocol
355 ***********************************************************************/
356
357static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms)
358{
359 struct msgb *msg = st_msgb_alloc();
360 struct st_modem_reset *sr ;
361
362 sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
363 sr->asserted = asserted;
364 sr->pulse_duration_msec = pulse_ms;
365
366 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET);
367}
368
369/*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/
370int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms)
371{
372 return _modem_reset(slot, 2, duration_ms);
373}
374
375/*! \brief assert the RESET line of the modem */
376int st_modem_reset_active(struct st_slot *slot)
377{
378 return _modem_reset(slot, 1, 0);
379}
380
381/*! \brief de-assert the RESET line of the modem */
382int st_modem_reset_inactive(struct st_slot *slot)
383{
384 return _modem_reset(slot, 0, 0);
385}
386
387static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim)
388{
389 struct msgb *msg = st_msgb_alloc();
390 struct st_modem_sim_select *ss;
391
392 ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
393 ss->remote_sim = remote_sim;
394
395 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT);
396}
397
398/*! \brief select local (physical) SIM for given slot */
399int st_modem_sim_select_local(struct st_slot *slot)
400{
401 return _modem_sim_select(slot, 0);
402}
403
404/*! \brief select remote (emulated/forwarded) SIM for given slot */
405int st_modem_sim_select_remote(struct st_slot *slot)
406{
407 return _modem_sim_select(slot, 1);
408}
409
410/*! \brief Request slot to send us status information about the modem */
411int st_modem_get_status(struct st_slot *slot)
412{
413 struct msgb *msg = st_msgb_alloc();
414
415 return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS);
416}
417
418
419/***********************************************************************
420 * Incoming Messages
421 ***********************************************************************/
422
423/*! \brief Process a STATUS message from the SIMtrace2 */
424static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
425{
426 struct cardemu_usb_msg_status *status;
427 status = (struct cardemu_usb_msg_status *) buf;
428
Kévin Redon21e31de2018-10-11 17:25:58 +0200429 printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200430 status->flags, status->fi, status->di, status->wi,
431 status->waiting_time);
432
433 return 0;
434}
435
436/*! \brief Process a PTS indication message from the SIMtrace2 */
437static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
438{
439 struct cardemu_usb_msg_pts_info *pts;
440 pts = (struct cardemu_usb_msg_pts_info *) buf;
441
Kévin Redon21e31de2018-10-11 17:25:58 +0200442 printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Kévin Redone0b837c2018-10-10 00:39:25 +0200443
444 return 0;
445}
446
447/*! \brief Process a ERROR indication message from the SIMtrace2 */
Harald Welte3f09f632019-03-31 15:51:13 +0200448__attribute__((unused)) static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len)
Kévin Redone0b837c2018-10-10 00:39:25 +0200449{
450 struct cardemu_usb_msg_error *err;
451 err = (struct cardemu_usb_msg_error *) buf;
452
Kévin Redon21e31de2018-10-11 17:25:58 +0200453 printf("SIMtrace => ERROR: %u/%u/%u: %s\n",
Kévin Redone0b837c2018-10-10 00:39:25 +0200454 err->severity, err->subsystem, err->code,
455 err->msg_len ? (char *)err->msg : "");
456
457 return 0;
458}
459
Harald Weltefd5dafc2019-12-15 21:14:14 +0100460static struct osmo_apdu_context ac; // this will hold the complete APDU (across calls)
Kévin Redonbc08db52018-10-11 08:41:00 +0200461
Kévin Redone0b837c2018-10-10 00:39:25 +0200462/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
463static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
464{
Kévin Redonbc08db52018-10-11 08:41:00 +0200465 struct cardemu_usb_msg_rx_data *data = (struct cardemu_usb_msg_rx_data *) buf; // cast the data from the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200466 int rc;
467
Kévin Redon21e31de2018-10-11 17:25:58 +0200468 printf("SIMtrace => DATA: flags=%x, %s: ", data->flags,
Kévin Redone0b837c2018-10-10 00:39:25 +0200469 osmo_hexdump(data->data, data->data_len));
470
Harald Weltefd5dafc2019-12-15 21:14:14 +0100471 rc = osmo_apdu_segment_in(&ac, data->data, data->data_len,
472 data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message
Kévin Redone0b837c2018-10-10 00:39:25 +0200473
Kévin Redonbc08db52018-10-11 08:41:00 +0200474 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem
Harald Welte2ea20b92019-03-30 08:33:49 +0100475 uint8_t apdu_command[sizeof(ac.hdr) + ac.lc.tot]; // to store the APDU command to send
Kévin Redonbc08db52018-10-11 08:41:00 +0200476 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header
Kévin Redone0b837c2018-10-10 00:39:25 +0200477 if (ac.lc.tot) {
Kévin Redonbc08db52018-10-11 08:41:00 +0200478 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data
Kévin Redone0b837c2018-10-10 00:39:25 +0200479 }
Kévin Redonbc08db52018-10-11 08:41:00 +0200480 // send APDU to card
Harald Welte9cf013a2019-03-11 22:19:19 +0100481 BankSlot_t bslot;
482 bank_slot2rspro(&bslot, &g_client->bankd_slot);
483 RsproPDU_t *pdu = rspro_gen_TpduModem2Card(g_client->srv_conn.clslot, &bslot, apdu_command, sizeof(ac.hdr) + ac.lc.tot); // create RSPRO packet
Harald Welte3e9860b2019-12-02 23:04:54 +0100484 server_conn_send_rspro(&g_client->bankd_conn, pdu);
Kévin Redonbc08db52018-10-11 08:41:00 +0200485 // the response will come separately
Kévin Redonbc08db52018-10-11 08:41:00 +0200486 } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
487 cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur); // send procedure byte to get remaining data
Kévin Redone0b837c2018-10-10 00:39:25 +0200488 }
489 return 0;
490}
491
492#if 0
493 case SIMTRACE_CMD_DO_ERROR
494 rc = process_do_error(ci, buf, len);
495 break;
496#endif
497
498/*! \brief Process an incoming message from the SIMtrace2 */
499static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
500{
501 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
502 int rc;
503
Kévin Redon21e31de2018-10-11 17:25:58 +0200504 printf("SIMtrace -> %s\n", osmo_hexdump(buf, len));
Kévin Redone0b837c2018-10-10 00:39:25 +0200505
506 buf += sizeof(*sh);
507
508 switch (sh->msg_type) {
509 case SIMTRACE_MSGT_BD_CEMU_STATUS:
510 rc = process_do_status(ci, buf, len);
511 break;
512 case SIMTRACE_MSGT_DO_CEMU_PTS:
513 rc = process_do_pts(ci, buf, len);
514 break;
515 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
516 rc = process_do_rx_da(ci, buf, len);
517 break;
Harald Welte0e0e9322019-12-16 12:47:18 +0100518 case SIMTRACE_MSGT_BD_CEMU_CONFIG:
519 /* firmware confirms configuration change; ignore */
520 break;
521 default:
522 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
523 rc = -1;
524 break;
525 }
526
527 return rc;
528}
529
530
531/*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */
532static int process_irq_status(struct cardem_inst *ci, const uint8_t *buf, int len)
533{
534 const struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf;
535
536 printf("SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
537 status->flags, status->fi, status->di, status->wi,
538 status->waiting_time);
539
540 BankSlot_t bslot;
541 bank_slot2rspro(&bslot, &g_client->bankd_slot);
542 RsproPDU_t *pdu = rspro_gen_ClientSlotStatusInd(g_client->srv_conn.clslot, &bslot,
543 status->flags & CEMU_STATUS_F_RESET_ACTIVE,
544 status->flags & CEMU_STATUS_F_VCC_PRESENT,
545 status->flags & CEMU_STATUS_F_CLK_ACTIVE,
546 -1 /* FIXME: make this dependent on board */);
547 server_conn_send_rspro(&g_client->bankd_conn, pdu);
548
Harald Welte943ae622019-12-17 01:12:56 +0100549 if (ci->last_status.flags != status->flags) {
550 ci->last_status = *status;
551 } else
552 ci->last_status = *status;
553
Harald Welte0e0e9322019-12-16 12:47:18 +0100554 return 0;
555}
556
557static int process_usb_msg_irq(struct cardem_inst *ci, const uint8_t *buf, unsigned int len)
558{
559 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
560 int rc;
561
562 printf("SIMtrace IRQ %s\n", osmo_hexdump(buf, len));
563
564 buf += sizeof(*sh);
565
566 switch (sh->msg_type) {
567 case SIMTRACE_MSGT_BD_CEMU_STATUS:
568 rc = process_irq_status(ci, buf, len);
569 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200570 default:
571 printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
572 rc = -1;
573 break;
574 }
575
576 return rc;
577}
578
Harald Welte32e2e002019-12-15 23:01:54 +0100579static void usb_in_xfer_cb(struct libusb_transfer *xfer)
Kévin Redone0b837c2018-10-10 00:39:25 +0200580{
Harald Welte32e2e002019-12-15 23:01:54 +0100581 struct cardem_inst *ci = xfer->user_data;
Kévin Redone0b837c2018-10-10 00:39:25 +0200582 int rc;
583
Harald Welte32e2e002019-12-15 23:01:54 +0100584 switch (xfer->status) {
585 case LIBUSB_TRANSFER_COMPLETED:
586 /* hand the message up the stack */
587 process_usb_msg(ci, xfer->buffer, xfer->actual_length);
588 break;
589 case LIBUSB_TRANSFER_NO_DEVICE:
590 fprintf(stderr, "USB device disappeared\n");
Harald Weltea9bc4de2020-02-16 15:40:21 +0100591 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100592 break;
593 default:
Harald Weltea9bc4de2020-02-16 15:40:21 +0100594 fprintf(stderr, "USB IN transfer failed, status=%u\n", xfer->status);
595 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100596 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200597 }
Harald Welte32e2e002019-12-15 23:01:54 +0100598
599 /* re-submit the IN transfer */
600 rc = libusb_submit_transfer(xfer);
601 OSMO_ASSERT(rc == 0);
Kévin Redone0b837c2018-10-10 00:39:25 +0200602}
603
Harald Welte32e2e002019-12-15 23:01:54 +0100604
605static void allocate_and_submit_in(struct cardem_inst *ci)
606{
607 struct st_transport *transp = ci->slot->transp;
608 struct libusb_transfer *xfer;
609 int rc;
610
611 xfer = libusb_alloc_transfer(0);
612 OSMO_ASSERT(xfer);
613 xfer->dev_handle = transp->usb_devh;
614 xfer->flags = 0;
615 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
616 xfer->endpoint = transp->usb_ep.in;
617 xfer->timeout = 0;
618 xfer->user_data = ci;
619 xfer->length = 16*256;
620
621 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
622 OSMO_ASSERT(xfer->buffer);
623 xfer->callback = usb_in_xfer_cb;
624
625 /* submit the IN transfer */
626 rc = libusb_submit_transfer(xfer);
627 OSMO_ASSERT(rc == 0);
628}
629
630
631static void usb_irq_xfer_cb(struct libusb_transfer *xfer)
632{
Harald Welte0e0e9322019-12-16 12:47:18 +0100633 struct cardem_inst *ci = xfer->user_data;
Harald Welte32e2e002019-12-15 23:01:54 +0100634 int rc;
635
636 switch (xfer->status) {
637 case LIBUSB_TRANSFER_COMPLETED:
Harald Welte0e0e9322019-12-16 12:47:18 +0100638 process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length);
Harald Welte32e2e002019-12-15 23:01:54 +0100639 break;
640 case LIBUSB_TRANSFER_NO_DEVICE:
641 fprintf(stderr, "USB device disappeared\n");
Harald Weltea9bc4de2020-02-16 15:40:21 +0100642 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100643 break;
644 default:
Harald Weltea9bc4de2020-02-16 15:40:21 +0100645 fprintf(stderr, "USB IRQ transfer failed, status=%u\n", xfer->status);
646 g_leave_main = true;
Harald Welte32e2e002019-12-15 23:01:54 +0100647 break;
648 }
649
650 /* re-submit the IN transfer */
651 rc = libusb_submit_transfer(xfer);
652 OSMO_ASSERT(rc == 0);
653}
654
655
656static void allocate_and_submit_irq(struct cardem_inst *ci)
657{
658 struct st_transport *transp = ci->slot->transp;
659 struct libusb_transfer *xfer;
660 int rc;
661
662 xfer = libusb_alloc_transfer(0);
663 OSMO_ASSERT(xfer);
664 xfer->dev_handle = transp->usb_devh;
665 xfer->flags = 0;
666 xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
667 xfer->endpoint = transp->usb_ep.irq_in;
668 xfer->timeout = 0;
669 xfer->user_data = ci;
670 xfer->length = 64;
671
672 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
673 OSMO_ASSERT(xfer->buffer);
674 xfer->callback = usb_irq_xfer_cb;
675
676 /* submit the IN transfer */
677 rc = libusb_submit_transfer(xfer);
678 OSMO_ASSERT(rc == 0);
679}
680
681
Kévin Redone0b837c2018-10-10 00:39:25 +0200682static struct st_transport _transp;
683
684static struct st_slot _slot = {
685 .transp = &_transp,
686 .slot_nr = 0,
687};
688
Harald Welte5ed46932019-12-17 00:02:36 +0100689static struct cardem_inst *g_ci;
Kévin Redone0b837c2018-10-10 00:39:25 +0200690
691static void signal_handler(int signal)
692{
693 switch (signal) {
694 case SIGINT:
Harald Welte5ed46932019-12-17 00:02:36 +0100695 cardem_request_card_insert(g_ci, false);
Kévin Redone0b837c2018-10-10 00:39:25 +0200696 exit(0);
697 break;
698 default:
699 break;
700 }
701}
702
703/** remsim_client **/
704
Harald Welte3e9860b2019-12-02 23:04:54 +0100705static int bankd_handle_tpduCardToModem(struct bankd_client *bc, const RsproPDU_t *pdu)
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200706{
707 OSMO_ASSERT(pdu);
708 OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
709
710 const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem;
711 if (card2modem->data.size < 2) { // at least the two SW bytes are needed
712 return -1;
713 }
714
715 // save SW to our current APDU context
716 ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2];
717 ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1];
Kévin Redon21e31de2018-10-11 17:25:58 +0200718 printf("SIMtrace <= SW=0x%02x%02x, len_rx=%d\n", ac.sw[0], ac.sw[1], card2modem->data.size - 2);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200719 if (card2modem->data.size > 2) { // send PB and data to modem
Harald Welte5ed46932019-12-17 00:02:36 +0100720 cardem_request_pb_and_tx(bc->cardem, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200721 }
Harald Welte5ed46932019-12-17 00:02:36 +0100722 cardem_request_sw_tx(bc->cardem, ac.sw); // send SW to modem
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200723
724 return 0;
725}
726
Harald Welte3e9860b2019-12-02 23:04:54 +0100727static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu)
Harald Weltefa365592019-03-28 20:28:57 +0100728{
729 RsproPDU_t *resp;
730 int rc;
731
732 OSMO_ASSERT(pdu);
733 OSMO_ASSERT(RsproPDUchoice_PR_setAtrReq == pdu->msg.present);
734
735 /* FIXME: is this permitted at any time by the SIMtrace2 cardemfirmware? */
Harald Welte5ed46932019-12-17 00:02:36 +0100736 rc = cardem_request_set_atr(bc->cardem, pdu->msg.choice.setAtrReq.atr.buf,
Harald Weltefa365592019-03-28 20:28:57 +0100737 pdu->msg.choice.setAtrReq.atr.size);
738 if (rc == 0)
739 resp = rspro_gen_SetAtrRes(ResultCode_ok);
740 else
741 resp = rspro_gen_SetAtrRes(ResultCode_cardTransmissionError);
742 if (!resp)
743 return -ENOMEM;
Harald Welte3e9860b2019-12-02 23:04:54 +0100744 server_conn_send_rspro(&g_client->bankd_conn, resp);
Harald Weltefa365592019-03-28 20:28:57 +0100745
746 return 0;
747}
748
Harald Welte3e9860b2019-12-02 23:04:54 +0100749/* handle incoming message from bankd */
750static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
Kévin Redone0b837c2018-10-10 00:39:25 +0200751{
Kévin Redone0b837c2018-10-10 00:39:25 +0200752 switch (pdu->msg.present) {
753 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +0100754 /* Store 'identity' of bankd to in peer_comp_id */
Harald Welte3e9860b2019-12-02 23:04:54 +0100755 rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
756 osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
Kévin Redone0b837c2018-10-10 00:39:25 +0200757 break;
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200758 case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
Harald Welte3e9860b2019-12-02 23:04:54 +0100759 bankd_handle_tpduCardToModem(g_client, pdu);
Kévin Redon9b69a3f2018-10-11 08:41:57 +0200760 break;
Harald Weltefa365592019-03-28 20:28:57 +0100761 case RsproPDUchoice_PR_setAtrReq:
Harald Welte3e9860b2019-12-02 23:04:54 +0100762 bankd_handle_setAtrReq(g_client, pdu);
Harald Weltefa365592019-03-28 20:28:57 +0100763 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200764 default:
Harald Welte3e9860b2019-12-02 23:04:54 +0100765 LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s\n",
766 rspro_msgt_name(pdu));
Kévin Redone0b837c2018-10-10 00:39:25 +0200767 return -1;
768 }
769
770 return 0;
771}
772
Harald Weltee56f2b92019-03-02 17:02:13 +0100773/* handle incoming messages from server */
774static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
775{
776 RsproPDU_t *resp;
777
778 switch (pdu->msg.present) {
779 case RsproPDUchoice_PR_connectClientRes:
780 /* Store 'identity' of server in srvc->peer_comp_id */
781 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
782 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
783 break;
Harald Welted571a3e2019-03-11 22:09:50 +0100784 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100785 /* store/set the clientID as instructed by the server */
Harald Welteec628e92019-03-08 22:18:31 +0100786 if (!g_client->srv_conn.clslot)
787 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welted571a3e2019-03-11 22:09:50 +0100788 *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
Harald Welte3e9860b2019-12-02 23:04:54 +0100789 if (!g_client->bankd_conn.clslot)
790 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
791 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welted571a3e2019-03-11 22:09:50 +0100792 /* send response to server */
793 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
794 server_conn_send_rspro(srvc, resp);
795 break;
796 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100797 /* store/set the bankd ip/port as instructed by the server */
Harald Welte3e9860b2019-12-02 23:04:54 +0100798 osmo_talloc_replace_string(g_client, &g_client->bankd_conn.server_host,
Harald Welted571a3e2019-03-11 22:09:50 +0100799 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
Harald Welte9cf013a2019-03-11 22:19:19 +0100800 rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
Harald Welte3e9860b2019-12-02 23:04:54 +0100801 g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
Harald Weltee56f2b92019-03-02 17:02:13 +0100802 /* instruct bankd FSM to connect */
Harald Welte3e9860b2019-12-02 23:04:54 +0100803 osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
Harald Weltee56f2b92019-03-02 17:02:13 +0100804 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +0100805 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +0100806 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +0100807 break;
808 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +0100809 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n",
810 rspro_msgt_name(pdu));
Harald Weltee56f2b92019-03-02 17:02:13 +0100811 return -1;
812 }
813
814 return 0;
815}
816
Harald Weltece638d82019-03-17 09:36:04 +0100817static void handle_sig_usr1(int signal)
818{
819 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +0200820 talloc_report_full(g_tall_ctx, stderr);
Harald Weltef9187482019-12-14 17:21:08 +0100821 printf("===== NULL\n");
822 talloc_report_full(NULL, stderr);
Harald Weltece638d82019-03-17 09:36:04 +0100823}
Harald Weltee56f2b92019-03-02 17:02:13 +0100824
Kévin Redon3ec265b2018-10-11 17:30:33 +0200825static void print_welcome(void)
826{
827 printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n"
Harald Welte32e2e002019-12-15 23:01:54 +0100828 "(C) 2010-2019, Harald Welte <laforge@gnumonks.org>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200829 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
830}
831
832static void print_help(void)
833{
Harald Weltee56f2b92019-03-02 17:02:13 +0100834 printf( "\t-s\t--server-host HOST\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200835 "\t-p\t--server-port PORT\n"
Harald Welte72cde102019-03-30 10:43:06 +0100836 "\t-c\t--client-id <0-65535>\n"
Kévin Redonda1854c2019-09-17 14:16:54 +0200837 "\t-n\t--client-slot <0-65535>\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200838 "\t-h\t--help\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100839 "\t-v\t--version\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200840 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
841 "\t-k\t--keep-running\n"
842 "\t-V\t--usb-vendor\tVENDOR_ID\n"
843 "\t-P\t--usb-product\tPRODUCT_ID\n"
844 "\t-C\t--usb-config\tCONFIG_ID\n"
845 "\t-I\t--usb-interface\tINTERFACE_ID\n"
846 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
847 "\t-A\t--usb-address\tADDRESS\n"
848 "\t-H\t--usb-path\tPATH\n"
Kévin Redon206c3d72018-11-12 22:51:37 +0100849 "\t-a\t--atr\tATR\n"
Kévin Redon3ec265b2018-10-11 17:30:33 +0200850 "\n"
851 );
852}
853
Harald Welte9636d2b2019-12-16 16:26:50 +0100854static struct client_config *client_config_init(void *ctx)
Kévin Redone0b837c2018-10-10 00:39:25 +0200855{
Harald Welte9636d2b2019-12-16 16:26:50 +0100856 struct client_config *cfg = talloc_zero(ctx, struct client_config);
857 if (!cfg)
858 return NULL;
Kévin Redone0b837c2018-10-10 00:39:25 +0200859
Harald Welte9636d2b2019-12-16 16:26:50 +0100860 cfg->server_host = talloc_strdup(cfg, "127.0.0.1");
861 cfg->server_port = 9998;
862 cfg->client_id = -1;
863 cfg->client_slot = -1;
864 cfg->gsmtap_host = talloc_strdup(cfg, "127.0.0.1");
865 cfg->keep_running = false;
866
867 cfg->usb.vendor_id = -1;
868 cfg->usb.product_id = -1;
869 cfg->usb.config_id = -1;
870 cfg->usb.if_num = -1;
871 cfg->usb.altsetting = 0;
872 cfg->usb.addr = -1;
873 cfg->usb.path = NULL;
874
875 cfg->atr.data[0] = 0x3B;
876 cfg->atr.data[1] = 0x00; // the shortest simplest ATR possible
877 cfg->atr.len = 2;
878
879 return cfg;
880};
881
882static void handle_options(struct client_config *cfg, int argc, char **argv)
883{
884 const struct option opts[] = {
885 { "server-host", 1, 0, 's' },
886 { "server-port", 1, 0, 'p' },
887 { "client-id", 1, 0, 'c' },
888 { "client-slot", 1, 0, 'n' },
889 { "help", 0, 0, 'h' },
890 { "version", 0, 0, 'v' },
891 { "gsmtap-ip", 1, 0, 'i' },
892 { "keep-running", 0, 0, 'k' },
893 { "usb-vendor", 1, 0, 'V' },
894 { "usb-product", 1, 0, 'P' },
895 { "usb-config", 1, 0, 'C' },
896 { "usb-interface", 1, 0, 'I' },
897 { "usb-altsetting", 1, 0, 'S' },
898 { "usb-address", 1, 0, 'A' },
899 { "usb-path", 1, 0, 'H' },
900 { "atr", 1, 0, 'a' },
901 { NULL, 0, 0, 0 }
902 };
903 int c, rc;
Kévin Redone0b837c2018-10-10 00:39:25 +0200904
905 while (1) {
906 int option_index = 0;
907
Harald Weltecd7fcd72019-12-03 21:29:07 +0100908 c = getopt_long(argc, argv, "s:p:c:n:hvi:kV:P:C:I:S:A:H:a:", opts, &option_index);
Kévin Redone0b837c2018-10-10 00:39:25 +0200909 if (c == -1)
910 break;
911 switch (c) {
Harald Weltee56f2b92019-03-02 17:02:13 +0100912 case 's':
Harald Welte9636d2b2019-12-16 16:26:50 +0100913 osmo_talloc_replace_string(cfg, &cfg->server_host, optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200914 break;
915 case 'p':
Harald Welte9636d2b2019-12-16 16:26:50 +0100916 cfg->server_port = atoi(optarg);
Kévin Redonfbca97a2018-10-11 19:13:24 +0200917 break;
Harald Welte72cde102019-03-30 10:43:06 +0100918 case 'c':
Harald Welte9636d2b2019-12-16 16:26:50 +0100919 cfg->client_id = atoi(optarg);
Harald Welte72cde102019-03-30 10:43:06 +0100920 break;
921 case 'n':
Harald Welte9636d2b2019-12-16 16:26:50 +0100922 cfg->client_slot = atoi(optarg);
Harald Welte72cde102019-03-30 10:43:06 +0100923 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200924 case 'h':
925 print_help();
926 exit(0);
927 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100928 case 'v':
929 printf("osmo-remsim-client version %s\n", VERSION);
930 exit(0);
931 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200932 case 'i':
Harald Welte9636d2b2019-12-16 16:26:50 +0100933 osmo_talloc_replace_string(cfg, &cfg->gsmtap_host, optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200934 break;
935 case 'k':
Harald Welte9636d2b2019-12-16 16:26:50 +0100936 cfg->keep_running = 1;
Kévin Redone0b837c2018-10-10 00:39:25 +0200937 break;
938 case 'V':
Harald Welte9636d2b2019-12-16 16:26:50 +0100939 cfg->usb.vendor_id = strtol(optarg, NULL, 16);
Kévin Redone0b837c2018-10-10 00:39:25 +0200940 break;
941 case 'P':
Harald Welte9636d2b2019-12-16 16:26:50 +0100942 cfg->usb.product_id = strtol(optarg, NULL, 16);
Kévin Redone0b837c2018-10-10 00:39:25 +0200943 break;
944 case 'C':
Harald Welte9636d2b2019-12-16 16:26:50 +0100945 cfg->usb.config_id = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200946 break;
947 case 'I':
Harald Welte9636d2b2019-12-16 16:26:50 +0100948 cfg->usb.if_num = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200949 break;
950 case 'S':
Harald Welte9636d2b2019-12-16 16:26:50 +0100951 cfg->usb.altsetting = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200952 break;
953 case 'A':
Harald Welte9636d2b2019-12-16 16:26:50 +0100954 cfg->usb.addr = atoi(optarg);
Kévin Redone0b837c2018-10-10 00:39:25 +0200955 break;
956 case 'H':
Harald Welte9636d2b2019-12-16 16:26:50 +0100957 cfg->usb.path = optarg;
Kévin Redone0b837c2018-10-10 00:39:25 +0200958 break;
Kévin Redon206c3d72018-11-12 22:51:37 +0100959 case 'a':
Harald Welte9636d2b2019-12-16 16:26:50 +0100960 rc = osmo_hexparse(optarg, cfg->atr.data, ARRAY_SIZE(cfg->atr.data));
961 if (rc < 2 || rc > ARRAY_SIZE(cfg->atr.data)) {
962 fprintf(stderr, "ATR malformed\n");
963 exit(2);
Kévin Redon206c3d72018-11-12 22:51:37 +0100964 }
Harald Welte9636d2b2019-12-16 16:26:50 +0100965 cfg->atr.len = rc;
Kévin Redon206c3d72018-11-12 22:51:37 +0100966 break;
Kévin Redone0b837c2018-10-10 00:39:25 +0200967 }
968 }
969
Harald Welte9636d2b2019-12-16 16:26:50 +0100970 if (argc > optind) {
971 fprintf(stderr, "Unsupported positional arguments on command line\n");
972 exit(2);
Kévin Redone0b837c2018-10-10 00:39:25 +0200973 }
Harald Welte9636d2b2019-12-16 16:26:50 +0100974}
Kévin Redone0b837c2018-10-10 00:39:25 +0200975
Harald Welte9636d2b2019-12-16 16:26:50 +0100976
Harald Welte5ed46932019-12-17 00:02:36 +0100977static void main_body(struct cardem_inst *ci, struct client_config *cfg)
Harald Weltea86e6b72019-12-16 16:51:34 +0100978{
979 struct st_transport *transp = ci->slot->transp;
980 struct usb_interface_match _ifm, *ifm = &_ifm;
981 int rc;
982
983 ifm->vendor = cfg->usb.vendor_id;
984 ifm->product = cfg->usb.product_id;
985 ifm->configuration = cfg->usb.config_id;
986 ifm->interface = cfg->usb.if_num;
987 ifm->altsetting = cfg->usb.altsetting;
988 ifm->addr = cfg->usb.addr;
989 if (cfg->usb.path)
990 osmo_strlcpy(ifm->path, cfg->usb.path, sizeof(ifm->path));
991 transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
992 if (!transp->usb_devh) {
993 fprintf(stderr, "can't open USB device\n");
994 return;
995 }
996
Harald Welte499e1d92019-12-17 01:13:53 +0100997 /* (re)determine the USB path of the opened device */
998 talloc_free(ci->usb_path);
999 ci->usb_path = osmo_libusb_dev_get_path_c(ci, libusb_get_device(transp->usb_devh));
1000
Harald Weltea86e6b72019-12-16 16:51:34 +01001001 rc = libusb_claim_interface(transp->usb_devh, cfg->usb.if_num);
1002 if (rc < 0) {
1003 fprintf(stderr, "can't claim interface %d; rc=%d\n", cfg->usb.if_num, rc);
1004 goto close_exit;
1005 }
1006
1007 rc = osmo_libusb_get_ep_addrs(transp->usb_devh, cfg->usb.if_num, &transp->usb_ep.out,
1008 &transp->usb_ep.in, &transp->usb_ep.irq_in);
1009 if (rc < 0) {
1010 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
1011 goto close_exit;
1012 }
1013
1014 // switch modem SIM port to emulated SIM on OWHW
1015 if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW
1016 int modem = -1;
1017 switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM
1018 case 0:
1019 modem = 1;
1020 break;
1021 case 1:
1022 modem = 2;
1023 break;
1024 default:
1025 fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface);
1026 goto close_exit;
1027 }
1028 //
1029 char gpio_path[PATH_MAX];
1030 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem);
1031 int connec_st_usim = open(gpio_path, O_WRONLY);
1032 if (-1 == connec_st_usim) {
1033 fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1034 goto close_exit;
1035 }
1036 if (1 != write(connec_st_usim, "1", 1)) {
1037 fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem);
1038 goto close_exit;
1039 }
1040 printf("switched modem %d to emulated USIM\n", modem);
1041
1042 snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem);
1043 int mdm_rst = open(gpio_path, O_WRONLY);
1044 if (-1 == mdm_rst) {
1045 fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem);
1046 goto close_exit;
1047 }
1048 if (1 != write(mdm_rst, "1", 1)) {
1049 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1050 goto close_exit;
1051 }
1052 sleep(1); // wait a bit to ensure reset is effective
1053 if (1 != write(mdm_rst, "0", 1)) {
1054 fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem);
1055 goto close_exit;
1056 }
1057 printf("modem %d reset\n", modem);
1058 }
1059
1060 /* request firmware to generate STATUS on IRQ endpoint */
1061 cardem_request_config(ci, CEMU_FEAT_F_STATUS_IRQ);
1062
1063 /* simulate card-insert to modem (owhw, not qmod) */
1064 cardem_request_card_insert(ci, true);
1065
1066 /* select remote (forwarded) SIM */
1067 st_modem_sim_select_remote(ci->slot);
1068
1069 /* set the ATR */
1070 //atr_update_csum(real_atr, sizeof(real_atr));
1071 cardem_request_set_atr(ci, cfg->atr.data, cfg->atr.len);
1072
1073 /* select remote (forwarded) SIM */
1074 st_modem_reset_pulse(ci->slot, 300);
1075
1076 printf("Entering main loop\n");
1077
1078 allocate_and_submit_irq(ci);
1079 allocate_and_submit_in(ci);
1080
Harald Weltea9bc4de2020-02-16 15:40:21 +01001081 while (!g_leave_main) {
Harald Weltea86e6b72019-12-16 16:51:34 +01001082 osmo_select_main(false);
1083 }
Harald Weltea9bc4de2020-02-16 15:40:21 +01001084 g_leave_main = false;
Harald Weltea86e6b72019-12-16 16:51:34 +01001085
1086 libusb_release_interface(transp->usb_devh, 0);
1087
1088close_exit:
1089 if (transp->usb_devh)
1090 libusb_close(transp-> usb_devh);
1091}
1092
Harald Welte9636d2b2019-12-16 16:26:50 +01001093int main(int argc, char **argv)
1094{
1095 struct rspro_server_conn *srvc, *bankdc;
Harald Welte9636d2b2019-12-16 16:26:50 +01001096 struct client_config *cfg;
1097 int rc;
1098 int ret = 1;
1099
1100 print_welcome();
Harald Weltece638d82019-03-17 09:36:04 +01001101
Harald Weltef9187482019-12-14 17:21:08 +01001102 talloc_enable_null_tracking();
Harald Welte972a1e82019-03-30 08:34:30 +01001103 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +02001104 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Weltef7442b52019-07-18 18:54:05 +02001105 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Welte972a1e82019-03-30 08:34:30 +01001106 osmo_init_logging2(g_tall_ctx, &log_info);
1107
Harald Welte5ed46932019-12-17 00:02:36 +01001108 g_ci = talloc_zero(g_tall_ctx, struct cardem_inst);
1109 g_ci->slot = &_slot;
1110
1111 cfg = client_config_init(g_ci);
Harald Welte9636d2b2019-12-16 16:26:50 +01001112 handle_options(cfg, argc, argv);
1113
1114 if (cfg->usb.vendor_id < 0 || cfg->usb.product_id < 0) {
1115 fprintf(stderr, "You have to specify the vendor and product ID\n");
1116 goto do_exit;
1117 }
1118
1119 signal(SIGUSR1, handle_sig_usr1);
1120
Harald Welte32e2e002019-12-15 23:01:54 +01001121 rc = osmo_libusb_init(NULL);
Kévin Redon193a8c12018-10-11 17:24:39 +02001122 if (rc < 0) {
1123 fprintf(stderr, "libusb initialization failed\n");
1124 goto do_exit;
Kévin Redone0b837c2018-10-10 00:39:25 +02001125 }
1126
Harald Welte9636d2b2019-12-16 16:26:50 +01001127 g_gti = gsmtap_source_init(cfg->gsmtap_host, GSMTAP_UDP_PORT, 0);
Kévin Redone0b837c2018-10-10 00:39:25 +02001128 if (!g_gti) {
1129 perror("unable to open GSMTAP");
1130 goto close_exit;
1131 }
1132 gsmtap_source_add_sink(g_gti);
1133
1134 signal(SIGINT, &signal_handler);
1135
1136 // initialize remote SIM client
Kévin Redone0b837c2018-10-10 00:39:25 +02001137
Kévin Redone0b837c2018-10-10 00:39:25 +02001138 g_client = talloc_zero(g_tall_ctx, struct bankd_client);
Harald Welte6a0248b2019-12-17 01:11:24 +01001139 g_client->cfg = cfg;
Harald Welte5ed46932019-12-17 00:02:36 +01001140 g_client->cardem = g_ci;
Harald Weltee56f2b92019-03-02 17:02:13 +01001141
Harald Welte9636d2b2019-12-16 16:26:50 +01001142 if (cfg->client_id != -1) {
Harald Welte72cde102019-03-30 10:43:06 +01001143 g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
Harald Welte9636d2b2019-12-16 16:26:50 +01001144 g_client->srv_conn.clslot->clientId = cfg->client_id;
1145 /* default to client slot 0 */
1146 if (cfg->client_slot == -1)
1147 g_client->srv_conn.clslot->slotNr = 0;
1148 else
1149 g_client->srv_conn.clslot->slotNr = cfg->client_slot;
Harald Welte3e9860b2019-12-02 23:04:54 +01001150 g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
1151 *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
Harald Welte72cde102019-03-30 10:43:06 +01001152 }
1153
Harald Welte9636d2b2019-12-16 16:26:50 +01001154 /* create and [attempt to] establish connection to remsim-server */
Harald Weltee56f2b92019-03-02 17:02:13 +01001155 srvc = &g_client->srv_conn;
Harald Welte9636d2b2019-12-16 16:26:50 +01001156 srvc->server_host = cfg->server_host;
1157 srvc->server_port = cfg->server_port;
Harald Weltee56f2b92019-03-02 17:02:13 +01001158 srvc->handle_rx = srvc_handle_rx;
1159 srvc->own_comp_id.type = ComponentType_remsimClient;
1160 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
1161 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
1162 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
1163 rc = server_conn_fsm_alloc(g_client, srvc);
1164 if (rc < 0) {
1165 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
1166 exit(1);
1167 }
Harald Welted2192e22019-11-07 18:10:57 +01001168 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001169
Harald Welte9636d2b2019-12-16 16:26:50 +01001170 /* create and not yet establish connection to remsim-bankd */
1171 srvc = &g_client->srv_conn;
Harald Welte3e9860b2019-12-02 23:04:54 +01001172 bankdc = &g_client->bankd_conn;
1173 /* server_host / server_port are configured from remsim-server */
1174 bankdc->handle_rx = bankd_handle_rx;
1175 memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id));
1176 rc = server_conn_fsm_alloc(g_client, bankdc);
1177 if (rc < 0) {
1178 fprintf(stderr, "Unable to create bankd conn FSM: %s\n", strerror(errno));
Kévin Redone0b837c2018-10-10 00:39:25 +02001179 exit(1);
1180 }
Harald Welte82194452019-12-14 17:18:34 +01001181 osmo_fsm_inst_update_id(bankdc->fi, "bankd");
Kévin Redone0b837c2018-10-10 00:39:25 +02001182
Harald Welte3e9860b2019-12-02 23:04:54 +01001183 asn_debug = 0;
1184
Kévin Redone0b837c2018-10-10 00:39:25 +02001185 // connect to SIMtrace2 cardem
1186 do {
Harald Welte5ed46932019-12-17 00:02:36 +01001187 main_body(g_ci, cfg);
Harald Weltea86e6b72019-12-16 16:51:34 +01001188 sleep(1);
Harald Welte9636d2b2019-12-16 16:26:50 +01001189 } while (cfg->keep_running);
Kévin Redone0b837c2018-10-10 00:39:25 +02001190
Harald Weltea86e6b72019-12-16 16:51:34 +01001191close_exit:
1192 osmo_libusb_exit(NULL);
Kévin Redone0b837c2018-10-10 00:39:25 +02001193do_exit:
1194 return ret;
1195}