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