Kévin Redon | 378d983 | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1 | |
| 2 | #include <errno.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include <talloc.h> |
| 6 | |
| 7 | #include <osmocom/core/msgb.h> |
| 8 | #include <osmocom/core/fsm.h> |
| 9 | #include <osmocom/core/utils.h> |
| 10 | #include <osmocom/core/logging.h> |
| 11 | #include <osmocom/core/application.h> |
| 12 | |
| 13 | #include <osmocom/abis/ipa.h> |
| 14 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 15 | |
| 16 | #include "rspro_util.h" |
| 17 | #include "client.h" |
| 18 | |
| 19 | #include <unistd.h> |
| 20 | #include <stdio.h> |
| 21 | #include <signal.h> |
| 22 | #include <getopt.h> |
| 23 | |
| 24 | #include <libusb.h> |
| 25 | |
| 26 | #include "libusb_util.h" |
| 27 | #include "simtrace.h" |
| 28 | #include "simtrace_prot.h" |
| 29 | #include "apdu_dispatch.h" |
| 30 | #include "simtrace2-discovery.h" |
| 31 | |
| 32 | #include <osmocom/core/gsmtap.h> |
| 33 | #include <osmocom/core/gsmtap_util.h> |
| 34 | #include <osmocom/core/utils.h> |
| 35 | #include <osmocom/core/socket.h> |
| 36 | #include <osmocom/core/msgb.h> |
| 37 | #include <osmocom/sim/class_tables.h> |
| 38 | #include <osmocom/sim/sim.h> |
| 39 | |
| 40 | /* transport to a SIMtrace device */ |
| 41 | struct st_transport { |
| 42 | /* USB */ |
| 43 | struct libusb_device_handle *usb_devh; |
| 44 | struct { |
| 45 | uint8_t in; |
| 46 | uint8_t out; |
| 47 | uint8_t irq_in; |
| 48 | } usb_ep; |
| 49 | |
| 50 | /* UDP */ |
| 51 | int udp_fd; |
| 52 | }; |
| 53 | |
| 54 | /* a SIMtrace slot; communicates over a transport */ |
| 55 | struct st_slot { |
| 56 | /* transport through which the slot can be reached */ |
| 57 | struct st_transport *transp; |
| 58 | /* number of the slot within the transport */ |
| 59 | uint8_t slot_nr; |
| 60 | }; |
| 61 | |
| 62 | /* One istance of card emulation */ |
| 63 | struct cardem_inst { |
| 64 | /* slot on which this card emulation instance runs */ |
| 65 | struct st_slot *slot; |
| 66 | }; |
| 67 | |
| 68 | /* global GSMTAP instance */ |
| 69 | static struct gsmtap_inst *g_gti; |
| 70 | |
| 71 | static struct bankd_client *g_client; |
| 72 | static void *g_tall_ctx; |
| 73 | void __thread *talloc_asn1_ctx; |
| 74 | int asn_debug; |
| 75 | |
| 76 | static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len) |
| 77 | { |
| 78 | struct gsmtap_hdr *gh; |
| 79 | unsigned int gross_len = len + sizeof(*gh); |
| 80 | uint8_t *buf = malloc(gross_len); |
| 81 | int rc; |
| 82 | |
| 83 | if (!buf) |
| 84 | return -ENOMEM; |
| 85 | |
| 86 | memset(buf, 0, sizeof(*gh)); |
| 87 | gh = (struct gsmtap_hdr *) buf; |
| 88 | gh->version = GSMTAP_VERSION; |
| 89 | gh->hdr_len = sizeof(*gh)/4; |
| 90 | gh->type = GSMTAP_TYPE_SIM; |
| 91 | |
| 92 | memcpy(buf + sizeof(*gh), apdu, len); |
| 93 | |
| 94 | rc = write(gsmtap_inst_fd(g_gti), buf, gross_len); |
| 95 | if (rc < 0) { |
| 96 | perror("write gsmtap"); |
| 97 | free(buf); |
| 98 | return rc; |
| 99 | } |
| 100 | |
| 101 | free(buf); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /*********************************************************************** |
| 106 | * SIMTRACE pcore protocol |
| 107 | ***********************************************************************/ |
| 108 | |
| 109 | /*! \brief allocate a message buffer for simtrace use */ |
| 110 | static struct msgb *st_msgb_alloc(void) |
| 111 | { |
| 112 | return msgb_alloc_headroom(1024+32, 32, "SIMtrace"); |
| 113 | } |
| 114 | |
| 115 | #if 0 |
| 116 | static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data) |
| 117 | { |
| 118 | printf("APDU: %s\n", osmo_hexdump(buf, len)); |
| 119 | gsmtap_send_sim(buf, len); |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | /*! \brief Transmit a given command to the SIMtrace2 device */ |
| 124 | int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg) |
| 125 | { |
| 126 | int rc; |
| 127 | |
| 128 | printf("<- %s\n", msgb_hexdump(msg)); |
| 129 | |
| 130 | if (transp->udp_fd < 0) { |
| 131 | int xfer_len; |
| 132 | |
| 133 | rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out, |
| 134 | msgb_data(msg), msgb_length(msg), |
| 135 | &xfer_len, 100000); |
| 136 | } else { |
| 137 | rc = write(transp->udp_fd, msgb_data(msg), msgb_length(msg)); |
| 138 | } |
| 139 | |
| 140 | msgb_free(msg); |
| 141 | return rc; |
| 142 | } |
| 143 | |
| 144 | static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type, |
| 145 | uint8_t slot_nr) |
| 146 | { |
| 147 | struct simtrace_msg_hdr *sh; |
| 148 | |
| 149 | sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh)); |
| 150 | memset(sh, 0, sizeof(*sh)); |
| 151 | sh->msg_class = msg_class; |
| 152 | sh->msg_type = msg_type; |
| 153 | sh->slot_nr = slot_nr; |
| 154 | sh->msg_len = msgb_length(msg); |
| 155 | |
| 156 | return sh; |
| 157 | } |
| 158 | |
| 159 | /* transmit a given message to a specified slot. Expects all headers |
| 160 | * present before calling the function */ |
| 161 | int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg, |
| 162 | uint8_t msg_class, uint8_t msg_type) |
| 163 | { |
| 164 | st_push_hdr(msg, msg_class, msg_type, slot->slot_nr); |
| 165 | |
| 166 | return st_transp_tx_msg(slot->transp, msg); |
| 167 | } |
| 168 | |
| 169 | /*********************************************************************** |
| 170 | * Card Emulation protocol |
| 171 | ***********************************************************************/ |
| 172 | |
| 173 | |
| 174 | /*! \brief Request the SIMtrace2 to generate a card-insert signal */ |
| 175 | static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted) |
| 176 | { |
| 177 | struct msgb *msg = st_msgb_alloc(); |
| 178 | struct cardemu_usb_msg_cardinsert *cins; |
| 179 | |
| 180 | cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins)); |
| 181 | memset(cins, 0, sizeof(*cins)); |
| 182 | if (inserted) |
| 183 | cins->card_insert = 1; |
| 184 | |
| 185 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT); |
| 186 | } |
| 187 | |
| 188 | /*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */ |
| 189 | static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le) |
| 190 | { |
| 191 | struct msgb *msg = st_msgb_alloc(); |
| 192 | struct cardemu_usb_msg_tx_data *txd; |
| 193 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
| 194 | |
| 195 | printf("<= %s(%02x, %d)\n", __func__, pb, le); |
| 196 | |
| 197 | memset(txd, 0, sizeof(*txd)); |
| 198 | txd->data_len = 1; |
| 199 | txd->flags = CEMU_DATA_F_PB_AND_RX; |
| 200 | /* one data byte */ |
| 201 | msgb_put_u8(msg, pb); |
| 202 | |
| 203 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
| 204 | } |
| 205 | |
| 206 | /*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */ |
| 207 | static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb, |
| 208 | const uint8_t *data, uint8_t data_len_in) |
| 209 | { |
| 210 | struct msgb *msg = st_msgb_alloc(); |
| 211 | struct cardemu_usb_msg_tx_data *txd; |
| 212 | uint8_t *cur; |
| 213 | |
| 214 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
| 215 | |
| 216 | printf("<= %s(%02x, %s, %d)\n", __func__, pb, |
| 217 | osmo_hexdump(data, data_len_in), data_len_in); |
| 218 | |
| 219 | memset(txd, 0, sizeof(*txd)); |
| 220 | txd->data_len = 1 + data_len_in; |
| 221 | txd->flags = CEMU_DATA_F_PB_AND_TX; |
| 222 | /* procedure byte */ |
| 223 | msgb_put_u8(msg, pb); |
| 224 | /* data */ |
| 225 | cur = msgb_put(msg, data_len_in); |
| 226 | memcpy(cur, data, data_len_in); |
| 227 | |
| 228 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
| 229 | } |
| 230 | |
| 231 | /*! \brief Request the SIMtrace2 to send a Status Word */ |
| 232 | static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw) |
| 233 | { |
| 234 | struct msgb *msg = st_msgb_alloc(); |
| 235 | struct cardemu_usb_msg_tx_data *txd; |
| 236 | uint8_t *cur; |
| 237 | |
| 238 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
| 239 | |
| 240 | printf("<= %s(%02x %02x)\n", __func__, sw[0], sw[1]); |
| 241 | |
| 242 | memset(txd, 0, sizeof(*txd)); |
| 243 | txd->data_len = 2; |
| 244 | txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL; |
| 245 | cur = msgb_put(msg, 2); |
| 246 | cur[0] = sw[0]; |
| 247 | cur[1] = sw[1]; |
| 248 | |
| 249 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
| 250 | } |
| 251 | |
| 252 | static void atr_update_csum(uint8_t *atr, unsigned int atr_len) |
| 253 | { |
| 254 | uint8_t csum = 0; |
| 255 | int i; |
| 256 | |
| 257 | for (i = 1; i < atr_len - 1; i++) |
| 258 | csum = csum ^ atr[i]; |
| 259 | |
| 260 | atr[atr_len-1] = csum; |
| 261 | } |
| 262 | |
| 263 | static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len) |
| 264 | { |
| 265 | struct msgb *msg = st_msgb_alloc(); |
| 266 | struct cardemu_usb_msg_set_atr *satr; |
| 267 | uint8_t *cur; |
| 268 | |
| 269 | satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr)); |
| 270 | |
| 271 | printf("<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len)); |
| 272 | |
| 273 | memset(satr, 0, sizeof(*satr)); |
| 274 | satr->atr_len = atr_len; |
| 275 | cur = msgb_put(msg, atr_len); |
| 276 | memcpy(cur, atr, atr_len); |
| 277 | |
| 278 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR); |
| 279 | } |
| 280 | |
| 281 | /*********************************************************************** |
| 282 | * Modem Control protocol |
| 283 | ***********************************************************************/ |
| 284 | |
| 285 | static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms) |
| 286 | { |
| 287 | struct msgb *msg = st_msgb_alloc(); |
| 288 | struct st_modem_reset *sr ; |
| 289 | |
| 290 | sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr)); |
| 291 | sr->asserted = asserted; |
| 292 | sr->pulse_duration_msec = pulse_ms; |
| 293 | |
| 294 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET); |
| 295 | } |
| 296 | |
| 297 | /*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/ |
| 298 | int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms) |
| 299 | { |
| 300 | return _modem_reset(slot, 2, duration_ms); |
| 301 | } |
| 302 | |
| 303 | /*! \brief assert the RESET line of the modem */ |
| 304 | int st_modem_reset_active(struct st_slot *slot) |
| 305 | { |
| 306 | return _modem_reset(slot, 1, 0); |
| 307 | } |
| 308 | |
| 309 | /*! \brief de-assert the RESET line of the modem */ |
| 310 | int st_modem_reset_inactive(struct st_slot *slot) |
| 311 | { |
| 312 | return _modem_reset(slot, 0, 0); |
| 313 | } |
| 314 | |
| 315 | static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim) |
| 316 | { |
| 317 | struct msgb *msg = st_msgb_alloc(); |
| 318 | struct st_modem_sim_select *ss; |
| 319 | |
| 320 | ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss)); |
| 321 | ss->remote_sim = remote_sim; |
| 322 | |
| 323 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT); |
| 324 | } |
| 325 | |
| 326 | /*! \brief select local (physical) SIM for given slot */ |
| 327 | int st_modem_sim_select_local(struct st_slot *slot) |
| 328 | { |
| 329 | return _modem_sim_select(slot, 0); |
| 330 | } |
| 331 | |
| 332 | /*! \brief select remote (emulated/forwarded) SIM for given slot */ |
| 333 | int st_modem_sim_select_remote(struct st_slot *slot) |
| 334 | { |
| 335 | return _modem_sim_select(slot, 1); |
| 336 | } |
| 337 | |
| 338 | /*! \brief Request slot to send us status information about the modem */ |
| 339 | int st_modem_get_status(struct st_slot *slot) |
| 340 | { |
| 341 | struct msgb *msg = st_msgb_alloc(); |
| 342 | |
| 343 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /*********************************************************************** |
| 348 | * Incoming Messages |
| 349 | ***********************************************************************/ |
| 350 | |
| 351 | /*! \brief Process a STATUS message from the SIMtrace2 */ |
| 352 | static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len) |
| 353 | { |
| 354 | struct cardemu_usb_msg_status *status; |
| 355 | status = (struct cardemu_usb_msg_status *) buf; |
| 356 | |
| 357 | printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n", |
| 358 | status->flags, status->fi, status->di, status->wi, |
| 359 | status->waiting_time); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /*! \brief Process a PTS indication message from the SIMtrace2 */ |
| 365 | static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len) |
| 366 | { |
| 367 | struct cardemu_usb_msg_pts_info *pts; |
| 368 | pts = (struct cardemu_usb_msg_pts_info *) buf; |
| 369 | |
| 370 | printf("=> PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req))); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | /*! \brief Process a ERROR indication message from the SIMtrace2 */ |
| 376 | static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len) |
| 377 | { |
| 378 | struct cardemu_usb_msg_error *err; |
| 379 | err = (struct cardemu_usb_msg_error *) buf; |
| 380 | |
| 381 | printf("=> ERROR: %u/%u/%u: %s\n", |
| 382 | err->severity, err->subsystem, err->code, |
| 383 | err->msg_len ? (char *)err->msg : ""); |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
Kévin Redon | 640b2ca | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 388 | static struct apdu_context ac; // this will hold the complete APDU (across calls) |
| 389 | |
Kévin Redon | 378d983 | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 390 | /*! \brief Process a RX-DATA indication message from the SIMtrace2 */ |
| 391 | static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len) |
| 392 | { |
Kévin Redon | 640b2ca | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 393 | struct cardemu_usb_msg_rx_data *data = (struct cardemu_usb_msg_rx_data *) buf; // cast the data from the USB message |
Kévin Redon | 378d983 | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 394 | int rc; |
| 395 | |
Kévin Redon | 378d983 | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 396 | printf("=> DATA: flags=%x, %s: ", data->flags, |
| 397 | osmo_hexdump(data->data, data->data_len)); |
| 398 | |
| 399 | rc = apdu_segment_in(&ac, data->data, data->data_len, |
Kévin Redon | 640b2ca | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 400 | data->flags & CEMU_DATA_F_TPDU_HDR); // parse the APDU data in the USB message |
Kévin Redon | 378d983 | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 401 | |
Kévin Redon | 640b2ca | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 402 | if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem |
| 403 | uint8_t* apdu_command = calloc(1, sizeof(ac.hdr) + ac.lc.tot); // to store the APDU command to send |
| 404 | memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr)); // copy APDU command header |
Kévin Redon | 378d983 | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 405 | if (ac.lc.tot) { |
Kévin Redon | 640b2ca | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 406 | memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot); // copy APDU command data |
Kévin Redon | 378d983 | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 407 | } |
Kévin Redon | 640b2ca | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 408 | // send APDU to card |
| 409 | RsproPDU_t *pdu = rspro_gen_TpduModem2Card(g_client->clslot, &(BankSlot_t){ .bankId = 0, .slotNr = 0}, apdu_command, sizeof(ac.hdr) + ac.lc.tot); // create RSPRO packet |
| 410 | ipa_client_conn_send_rspro(g_client->bankd_conn, pdu); // send RSPRO packet |
| 411 | // the response will come separately |
| 412 | free(apdu_command); |
| 413 | } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem |
| 414 | cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur); // send procedure byte to get remaining data |
Kévin Redon | 378d983 | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 415 | } |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | #if 0 |
| 420 | case SIMTRACE_CMD_DO_ERROR |
| 421 | rc = process_do_error(ci, buf, len); |
| 422 | break; |
| 423 | #endif |
| 424 | |
| 425 | /*! \brief Process an incoming message from the SIMtrace2 */ |
| 426 | static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len) |
| 427 | { |
| 428 | struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf; |
| 429 | int rc; |
| 430 | |
| 431 | printf("-> %s\n", osmo_hexdump(buf, len)); |
| 432 | |
| 433 | buf += sizeof(*sh); |
| 434 | |
| 435 | switch (sh->msg_type) { |
| 436 | case SIMTRACE_MSGT_BD_CEMU_STATUS: |
| 437 | rc = process_do_status(ci, buf, len); |
| 438 | break; |
| 439 | case SIMTRACE_MSGT_DO_CEMU_PTS: |
| 440 | rc = process_do_pts(ci, buf, len); |
| 441 | break; |
| 442 | case SIMTRACE_MSGT_DO_CEMU_RX_DATA: |
| 443 | rc = process_do_rx_da(ci, buf, len); |
| 444 | break; |
| 445 | default: |
| 446 | printf("unknown simtrace msg type 0x%02x\n", sh->msg_type); |
| 447 | rc = -1; |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | return rc; |
| 452 | } |
| 453 | |
| 454 | static void print_welcome(void) |
| 455 | { |
| 456 | printf("simtrace2-remsim - Remote SIM card forwarding\n" |
| 457 | "(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n" |
| 458 | "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n"); |
| 459 | } |
| 460 | |
| 461 | static void print_help(void) |
| 462 | { |
| 463 | printf( "\t-r\t--remote-udp-host HOST\n" |
| 464 | "\t-p\t--remote-udp-port PORT\n" |
| 465 | "\t-h\t--help\n" |
| 466 | "\t-i\t--gsmtap-ip\tA.B.C.D\n" |
| 467 | "\t-k\t--keep-running\n" |
| 468 | "\t-V\t--usb-vendor\tVENDOR_ID\n" |
| 469 | "\t-P\t--usb-product\tPRODUCT_ID\n" |
| 470 | "\t-C\t--usb-config\tCONFIG_ID\n" |
| 471 | "\t-I\t--usb-interface\tINTERFACE_ID\n" |
| 472 | "\t-S\t--usb-altsetting ALTSETTING_ID\n" |
| 473 | "\t-A\t--usb-address\tADDRESS\n" |
| 474 | "\t-H\t--usb-path\tPATH\n" |
| 475 | "\n" |
| 476 | ); |
| 477 | } |
| 478 | |
| 479 | static const struct option opts[] = { |
| 480 | { "remote-udp-host", 1, 0, 'r' }, |
| 481 | { "remote-udp-port", 1, 0, 'p' }, |
| 482 | { "gsmtap-ip", 1, 0, 'i' }, |
| 483 | { "help", 0, 0, 'h' }, |
| 484 | { "keep-running", 0, 0, 'k' }, |
| 485 | { "usb-vendor", 1, 0, 'V' }, |
| 486 | { "usb-product", 1, 0, 'P' }, |
| 487 | { "usb-config", 1, 0, 'C' }, |
| 488 | { "usb-interface", 1, 0, 'I' }, |
| 489 | { "usb-altsetting", 1, 0, 'S' }, |
| 490 | { "usb-address", 1, 0, 'A' }, |
| 491 | { "usb-path", 1, 0, 'H' }, |
| 492 | { NULL, 0, 0, 0 } |
| 493 | }; |
| 494 | |
| 495 | static void run_mainloop(struct cardem_inst *ci) |
| 496 | { |
| 497 | struct st_transport *transp = ci->slot->transp; |
| 498 | unsigned int msg_count, byte_count = 0; |
| 499 | uint8_t buf[16*265]; |
| 500 | int xfer_len; |
| 501 | int rc; |
| 502 | |
| 503 | printf("Entering main loop\n"); |
| 504 | |
| 505 | while (1) { |
| 506 | /* read data from SIMtrace2 device (local or via USB) */ |
| 507 | rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in, |
| 508 | buf, sizeof(buf), &xfer_len, 100); |
| 509 | if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT && |
| 510 | rc != LIBUSB_ERROR_INTERRUPTED && |
| 511 | rc != LIBUSB_ERROR_IO) { |
| 512 | fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc); |
| 513 | return; |
| 514 | } |
| 515 | /* dispatch any incoming data */ |
| 516 | if (xfer_len > 0) { |
| 517 | printf("URB: %s\n", osmo_hexdump(buf, xfer_len)); |
| 518 | process_usb_msg(ci, buf, xfer_len); |
| 519 | msg_count++; |
| 520 | byte_count += xfer_len; |
| 521 | } |
| 522 | // handle remote SIM client fsm |
| 523 | // TODO register the USB fd for this select |
| 524 | osmo_select_main(true); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | static struct st_transport _transp; |
| 529 | |
| 530 | static struct st_slot _slot = { |
| 531 | .transp = &_transp, |
| 532 | .slot_nr = 0, |
| 533 | }; |
| 534 | |
| 535 | struct cardem_inst _ci = { |
| 536 | .slot = &_slot, |
| 537 | }; |
| 538 | |
| 539 | struct cardem_inst *ci = &_ci; |
| 540 | |
| 541 | static void signal_handler(int signal) |
| 542 | { |
| 543 | switch (signal) { |
| 544 | case SIGINT: |
| 545 | cardem_request_card_insert(ci, false); |
| 546 | exit(0); |
| 547 | break; |
| 548 | default: |
| 549 | break; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | /** remsim_client **/ |
| 554 | |
| 555 | static void push_and_send(struct ipa_client_conn *ipa, struct msgb *msg_tx) |
| 556 | { |
| 557 | ipa_prepend_header_ext(msg_tx, IPAC_PROTO_EXT_RSPRO); |
| 558 | ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO); |
| 559 | ipa_client_conn_send(ipa, msg_tx); |
| 560 | /* msg_tx is now queued and will be freed. */ |
| 561 | } |
| 562 | |
| 563 | void ipa_client_conn_send_rspro(struct ipa_client_conn *ipa, RsproPDU_t *rspro) |
| 564 | { |
| 565 | struct msgb *msg = rspro_enc_msg(rspro); |
| 566 | OSMO_ASSERT(msg); |
| 567 | push_and_send(ipa, msg); |
| 568 | } |
| 569 | |
Kévin Redon | 8670fb7 | 2018-10-11 08:41:57 +0200 | [diff] [blame] | 570 | static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu) |
| 571 | { |
| 572 | OSMO_ASSERT(pdu); |
| 573 | OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present); |
| 574 | |
| 575 | const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem; |
| 576 | if (card2modem->data.size < 2) { // at least the two SW bytes are needed |
| 577 | return -1; |
| 578 | } |
| 579 | |
| 580 | // save SW to our current APDU context |
| 581 | ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2]; |
| 582 | ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1]; |
| 583 | printf("SW=0x%02x%02x, len_rx=%d\n", ac.sw[0], ac.sw[1], card2modem->data.size - 2); |
| 584 | if (card2modem->data.size > 2) { // send PB and data to modem |
| 585 | cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2); |
| 586 | } |
| 587 | cardem_request_sw_tx(ci, ac.sw); // send SW to modem |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
Kévin Redon | 378d983 | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 592 | static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg) |
| 593 | { |
| 594 | RsproPDU_t *pdu = rspro_dec_msg(msg); |
| 595 | if (!pdu) { |
| 596 | fprintf(stderr, "Error decoding PDU\n"); |
| 597 | return -1; |
| 598 | } |
| 599 | |
| 600 | switch (pdu->msg.present) { |
| 601 | case RsproPDUchoice_PR_connectClientRes: |
| 602 | osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu); |
| 603 | break; |
Kévin Redon | 8670fb7 | 2018-10-11 08:41:57 +0200 | [diff] [blame] | 604 | case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received |
| 605 | bankd_handle_tpduCardToModem(bc, pdu); |
| 606 | break; |
Kévin Redon | 378d983 | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 607 | default: |
| 608 | fprintf(stderr, "Unknown/Unsuppoerted RSPRO PDU: %s\n", msgb_hexdump(msg)); |
| 609 | return -1; |
| 610 | } |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg) |
| 616 | { |
| 617 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 618 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 619 | struct bankd_client *bc = conn->data; |
| 620 | int rc; |
| 621 | |
| 622 | if (msgb_length(msg) < sizeof(*hh)) |
| 623 | goto invalid; |
| 624 | msg->l2h = &hh->data[0]; |
| 625 | if (hh->proto != IPAC_PROTO_OSMO) |
| 626 | goto invalid; |
| 627 | if (!he || msgb_l2len(msg) < sizeof(*he)) |
| 628 | goto invalid; |
| 629 | msg->l2h = &he->data[0]; |
| 630 | |
| 631 | if (he->proto != IPAC_PROTO_EXT_RSPRO) |
| 632 | goto invalid; |
| 633 | |
| 634 | printf("Received RSPRO %s\n", msgb_hexdump(msg)); |
| 635 | |
| 636 | rc = bankd_handle_msg(bc, msg); |
| 637 | |
| 638 | return rc; |
| 639 | |
| 640 | invalid: |
| 641 | msgb_free(msg); |
| 642 | return -1; |
| 643 | } |
| 644 | |
| 645 | static const struct log_info_cat default_categories[] = { |
| 646 | [DMAIN] = { |
| 647 | .name = "DMAIN", |
| 648 | .loglevel = LOGL_DEBUG, |
| 649 | .enabled = 1, |
| 650 | }, |
| 651 | }; |
| 652 | |
| 653 | static const struct log_info log_info = { |
| 654 | .cat = default_categories, |
| 655 | .num_cat = ARRAY_SIZE(default_categories), |
| 656 | }; |
| 657 | |
| 658 | int main(int argc, char **argv) |
| 659 | { |
| 660 | struct st_transport *transp = ci->slot->transp; |
| 661 | char *gsmtap_host = "127.0.0.1"; |
| 662 | int rc; |
| 663 | int c, ret = 1; |
| 664 | int keep_running = 0; |
| 665 | int remote_udp_port = 52342; |
| 666 | int if_num = 0, vendor_id = -1, product_id = -1; |
| 667 | int config_id = -1, altsetting = 0, addr = -1; |
| 668 | char *remote_udp_host = NULL; |
| 669 | char *path = NULL; |
| 670 | |
| 671 | print_welcome(); |
| 672 | |
| 673 | while (1) { |
| 674 | int option_index = 0; |
| 675 | |
| 676 | c = getopt_long(argc, argv, "r:p:hi:V:P:C:I:S:A:H:ak", opts, &option_index); |
| 677 | if (c == -1) |
| 678 | break; |
| 679 | switch (c) { |
| 680 | case 'r': |
| 681 | remote_udp_host = optarg; |
| 682 | break; |
| 683 | case 'p': |
| 684 | remote_udp_port = atoi(optarg); |
| 685 | break; |
| 686 | case 'h': |
| 687 | print_help(); |
| 688 | exit(0); |
| 689 | break; |
| 690 | case 'i': |
| 691 | gsmtap_host = optarg; |
| 692 | break; |
| 693 | case 'k': |
| 694 | keep_running = 1; |
| 695 | break; |
| 696 | case 'V': |
| 697 | vendor_id = strtol(optarg, NULL, 16); |
| 698 | break; |
| 699 | case 'P': |
| 700 | product_id = strtol(optarg, NULL, 16); |
| 701 | break; |
| 702 | case 'C': |
| 703 | config_id = atoi(optarg); |
| 704 | break; |
| 705 | case 'I': |
| 706 | if_num = atoi(optarg); |
| 707 | break; |
| 708 | case 'S': |
| 709 | altsetting = atoi(optarg); |
| 710 | break; |
| 711 | case 'A': |
| 712 | addr = atoi(optarg); |
| 713 | break; |
| 714 | case 'H': |
| 715 | path = optarg; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (!remote_udp_host && (vendor_id < 0 || product_id < 0)) { |
| 721 | fprintf(stderr, "You have to specify the vendor and product ID\n"); |
| 722 | goto do_exit; |
| 723 | } |
| 724 | |
| 725 | transp->udp_fd = -1; |
| 726 | |
| 727 | if (!remote_udp_host) { |
| 728 | rc = libusb_init(NULL); |
| 729 | if (rc < 0) { |
| 730 | fprintf(stderr, "libusb initialization failed\n"); |
| 731 | goto do_exit; |
| 732 | } |
| 733 | } else { |
| 734 | transp->udp_fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 735 | remote_udp_host, remote_udp_port+if_num, |
| 736 | OSMO_SOCK_F_CONNECT); |
| 737 | if (transp->udp_fd < 0) { |
| 738 | fprintf(stderr, "error binding UDP port\n"); |
| 739 | goto do_exit; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0); |
| 744 | if (!g_gti) { |
| 745 | perror("unable to open GSMTAP"); |
| 746 | goto close_exit; |
| 747 | } |
| 748 | gsmtap_source_add_sink(g_gti); |
| 749 | |
| 750 | signal(SIGINT, &signal_handler); |
| 751 | |
| 752 | // initialize remote SIM client |
| 753 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
| 754 | |
| 755 | osmo_fsm_register(&remsim_client_bankd_fsm); |
| 756 | osmo_fsm_register(&remsim_client_server_fsm); |
| 757 | |
| 758 | g_client = talloc_zero(g_tall_ctx, struct bankd_client); |
| 759 | g_client->bankd_host = "localhost"; |
| 760 | g_client->bankd_port = 9999; |
| 761 | g_client->own_comp_id.type = ComponentType_remsimClient; |
| 762 | g_client->clslot = &(ClientSlot_t){ .clientId = 23, .slotNr = 1 }; |
| 763 | OSMO_STRLCPY_ARRAY(g_client->own_comp_id.name, "fixme-name"); |
| 764 | OSMO_STRLCPY_ARRAY(g_client->own_comp_id.software, "remsim-client"); |
| 765 | OSMO_STRLCPY_ARRAY(g_client->own_comp_id.sw_version, PACKAGE_VERSION); |
| 766 | |
| 767 | asn_debug = 0; |
| 768 | osmo_init_logging2(g_tall_ctx, &log_info); |
| 769 | |
| 770 | if (bankd_conn_fsm_alloc(g_client) < 0) { |
| 771 | fprintf(stderr, "Unable to connect: %s\n", strerror(errno)); |
| 772 | exit(1); |
| 773 | } |
| 774 | |
| 775 | // connect to SIMtrace2 cardem |
| 776 | do { |
| 777 | struct usb_interface_match _ifm, *ifm = &_ifm; |
| 778 | ifm->vendor = vendor_id; |
| 779 | ifm->product = product_id; |
| 780 | ifm->configuration = config_id; |
| 781 | ifm->interface = if_num; |
| 782 | ifm->altsetting = altsetting; |
| 783 | ifm->addr = addr; |
| 784 | if (path) |
| 785 | osmo_strlcpy(ifm->path, path, sizeof(ifm->path)); |
| 786 | transp->usb_devh = usb_open_claim_interface(NULL, ifm); |
| 787 | if (!transp->usb_devh) { |
| 788 | fprintf(stderr, "can't open USB device\n"); |
| 789 | goto close_exit; |
| 790 | } |
| 791 | |
| 792 | rc = libusb_claim_interface(transp->usb_devh, if_num); |
| 793 | if (rc < 0) { |
| 794 | fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc); |
| 795 | goto close_exit; |
| 796 | } |
| 797 | |
| 798 | rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out, |
| 799 | &transp->usb_ep.in, &transp->usb_ep.irq_in); |
| 800 | if (rc < 0) { |
| 801 | fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc); |
| 802 | goto close_exit; |
| 803 | } |
| 804 | |
| 805 | /* simulate card-insert to modem (owhw, not qmod) */ |
| 806 | cardem_request_card_insert(ci, true); |
| 807 | |
| 808 | /* select remote (forwarded) SIM */ |
| 809 | st_modem_sim_select_remote(ci->slot); |
| 810 | |
| 811 | /* set the ATR */ |
| 812 | uint8_t real_atr[] = { 0x3B, 0x00 }; // the shortest simplest ATR possible |
| 813 | atr_update_csum(real_atr, sizeof(real_atr)); |
| 814 | cardem_request_set_atr(ci, real_atr, sizeof(real_atr)); |
| 815 | |
| 816 | /* select remote (forwarded) SIM */ |
| 817 | st_modem_reset_pulse(ci->slot, 300); |
| 818 | |
| 819 | run_mainloop(ci); |
| 820 | ret = 0; |
| 821 | |
| 822 | if (transp->udp_fd < 0) |
| 823 | libusb_release_interface(transp->usb_devh, 0); |
| 824 | close_exit: |
| 825 | if (transp->usb_devh) |
| 826 | libusb_close(transp->usb_devh); |
| 827 | if (keep_running) |
| 828 | sleep(1); |
| 829 | } while (keep_running); |
| 830 | |
| 831 | if (transp->udp_fd < 0) |
| 832 | libusb_exit(NULL); |
| 833 | do_exit: |
| 834 | return ret; |
| 835 | } |