Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 1 | /* (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 Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 24 | |
| 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 Welte | 61d98e9 | 2019-03-03 15:43:07 +0100 | [diff] [blame] | 41 | #include "debug.h" |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 42 | |
| 43 | #include <unistd.h> |
| 44 | #include <stdio.h> |
Kévin Redon | 3428e41 | 2018-10-11 19:14:00 +0200 | [diff] [blame] | 45 | #include <linux/limits.h> |
| 46 | #include <sys/stat.h> |
| 47 | #include <fcntl.h> |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 48 | #include <signal.h> |
| 49 | #include <getopt.h> |
| 50 | |
| 51 | #include <libusb.h> |
| 52 | |
Harald Welte | fd5dafc | 2019-12-15 21:14:14 +0100 | [diff] [blame] | 53 | #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 Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 57 | |
| 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 */ |
| 67 | struct 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 Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /* a SIMtrace slot; communicates over a transport */ |
| 78 | struct 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 */ |
| 86 | struct cardem_inst { |
| 87 | /* slot on which this card emulation instance runs */ |
| 88 | struct st_slot *slot; |
| 89 | }; |
| 90 | |
| 91 | /* global GSMTAP instance */ |
| 92 | static struct gsmtap_inst *g_gti; |
| 93 | |
| 94 | static struct bankd_client *g_client; |
| 95 | static void *g_tall_ctx; |
| 96 | void __thread *talloc_asn1_ctx; |
| 97 | int asn_debug; |
| 98 | |
Harald Welte | 3f09f63 | 2019-03-31 15:51:13 +0200 | [diff] [blame] | 99 | __attribute__((unused)) static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 100 | { |
| 101 | struct gsmtap_hdr *gh; |
| 102 | unsigned int gross_len = len + sizeof(*gh); |
| 103 | uint8_t *buf = malloc(gross_len); |
| 104 | int rc; |
| 105 | |
| 106 | if (!buf) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | memset(buf, 0, sizeof(*gh)); |
| 110 | gh = (struct gsmtap_hdr *) buf; |
| 111 | gh->version = GSMTAP_VERSION; |
| 112 | gh->hdr_len = sizeof(*gh)/4; |
| 113 | gh->type = GSMTAP_TYPE_SIM; |
| 114 | |
| 115 | memcpy(buf + sizeof(*gh), apdu, len); |
| 116 | |
| 117 | rc = write(gsmtap_inst_fd(g_gti), buf, gross_len); |
| 118 | if (rc < 0) { |
| 119 | perror("write gsmtap"); |
| 120 | free(buf); |
| 121 | return rc; |
| 122 | } |
| 123 | |
| 124 | free(buf); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /*********************************************************************** |
Harald Welte | f14dc04 | 2019-03-28 20:29:36 +0100 | [diff] [blame] | 129 | * SIMTRACE core protocol |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 130 | ***********************************************************************/ |
| 131 | |
| 132 | /*! \brief allocate a message buffer for simtrace use */ |
| 133 | static struct msgb *st_msgb_alloc(void) |
| 134 | { |
| 135 | return msgb_alloc_headroom(1024+32, 32, "SIMtrace"); |
| 136 | } |
| 137 | |
| 138 | #if 0 |
| 139 | static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data) |
| 140 | { |
| 141 | printf("APDU: %s\n", osmo_hexdump(buf, len)); |
| 142 | gsmtap_send_sim(buf, len); |
| 143 | } |
| 144 | #endif |
| 145 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 146 | static void usb_out_xfer_cb(struct libusb_transfer *xfer) |
| 147 | { |
| 148 | struct msgb *msg = xfer->user_data; |
| 149 | |
| 150 | switch (xfer->status) { |
| 151 | case LIBUSB_TRANSFER_COMPLETED: |
| 152 | break; |
| 153 | case LIBUSB_TRANSFER_NO_DEVICE: |
| 154 | fprintf(stderr, "USB device disappeared\n"); |
| 155 | exit(23); |
| 156 | break; |
| 157 | default: |
| 158 | osmo_panic("USB OUT transfer failed, status=%u\n", xfer->status); |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | msgb_free(msg); |
| 163 | libusb_free_transfer(xfer); |
| 164 | } |
| 165 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 166 | /*! \brief Transmit a given command to the SIMtrace2 device */ |
| 167 | int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg) |
| 168 | { |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 169 | struct libusb_transfer *xfer; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 170 | int rc; |
| 171 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 172 | printf("SIMtrace <- %s\n", msgb_hexdump(msg)); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 173 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 174 | xfer = libusb_alloc_transfer(0); |
| 175 | OSMO_ASSERT(xfer); |
| 176 | xfer->dev_handle = transp->usb_devh; |
| 177 | xfer->flags = 0; |
| 178 | xfer->type = LIBUSB_TRANSFER_TYPE_BULK; |
| 179 | xfer->endpoint = transp->usb_ep.out; |
| 180 | xfer->timeout = 1000; |
| 181 | xfer->user_data = msg; |
| 182 | xfer->length = msgb_length(msg); |
| 183 | xfer->buffer = msgb_data(msg); |
| 184 | xfer->callback = usb_out_xfer_cb; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 185 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 186 | /* submit the OUT transfer */ |
| 187 | rc = libusb_submit_transfer(xfer); |
| 188 | OSMO_ASSERT(rc == 0); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 189 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 190 | return rc; |
| 191 | } |
| 192 | |
| 193 | static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type, |
| 194 | uint8_t slot_nr) |
| 195 | { |
| 196 | struct simtrace_msg_hdr *sh; |
| 197 | |
| 198 | sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh)); |
| 199 | memset(sh, 0, sizeof(*sh)); |
| 200 | sh->msg_class = msg_class; |
| 201 | sh->msg_type = msg_type; |
| 202 | sh->slot_nr = slot_nr; |
| 203 | sh->msg_len = msgb_length(msg); |
| 204 | |
| 205 | return sh; |
| 206 | } |
| 207 | |
| 208 | /* transmit a given message to a specified slot. Expects all headers |
| 209 | * present before calling the function */ |
| 210 | int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg, |
| 211 | uint8_t msg_class, uint8_t msg_type) |
| 212 | { |
| 213 | st_push_hdr(msg, msg_class, msg_type, slot->slot_nr); |
| 214 | |
| 215 | return st_transp_tx_msg(slot->transp, msg); |
| 216 | } |
| 217 | |
| 218 | /*********************************************************************** |
| 219 | * Card Emulation protocol |
| 220 | ***********************************************************************/ |
| 221 | |
| 222 | |
| 223 | /*! \brief Request the SIMtrace2 to generate a card-insert signal */ |
| 224 | static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted) |
| 225 | { |
| 226 | struct msgb *msg = st_msgb_alloc(); |
| 227 | struct cardemu_usb_msg_cardinsert *cins; |
| 228 | |
| 229 | cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins)); |
| 230 | memset(cins, 0, sizeof(*cins)); |
| 231 | if (inserted) |
| 232 | cins->card_insert = 1; |
| 233 | |
| 234 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT); |
| 235 | } |
| 236 | |
| 237 | /*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */ |
| 238 | static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le) |
| 239 | { |
| 240 | struct msgb *msg = st_msgb_alloc(); |
| 241 | struct cardemu_usb_msg_tx_data *txd; |
| 242 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
| 243 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 244 | printf("SIMtrace <= %s(%02x, %d)\n", __func__, pb, le); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 245 | |
| 246 | memset(txd, 0, sizeof(*txd)); |
| 247 | txd->data_len = 1; |
| 248 | txd->flags = CEMU_DATA_F_PB_AND_RX; |
| 249 | /* one data byte */ |
| 250 | msgb_put_u8(msg, pb); |
| 251 | |
| 252 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
| 253 | } |
| 254 | |
| 255 | /*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */ |
| 256 | static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb, |
Kévin Redon | f120b64 | 2018-10-15 19:53:02 +0200 | [diff] [blame] | 257 | const uint8_t *data, uint16_t data_len_in) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 258 | { |
| 259 | struct msgb *msg = st_msgb_alloc(); |
| 260 | struct cardemu_usb_msg_tx_data *txd; |
| 261 | uint8_t *cur; |
| 262 | |
| 263 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
| 264 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 265 | printf("SIMtrace <= %s(%02x, %s, %d)\n", __func__, pb, |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 266 | osmo_hexdump(data, data_len_in), data_len_in); |
| 267 | |
| 268 | memset(txd, 0, sizeof(*txd)); |
| 269 | txd->data_len = 1 + data_len_in; |
| 270 | txd->flags = CEMU_DATA_F_PB_AND_TX; |
| 271 | /* procedure byte */ |
| 272 | msgb_put_u8(msg, pb); |
| 273 | /* data */ |
| 274 | cur = msgb_put(msg, data_len_in); |
| 275 | memcpy(cur, data, data_len_in); |
| 276 | |
| 277 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
| 278 | } |
| 279 | |
| 280 | /*! \brief Request the SIMtrace2 to send a Status Word */ |
| 281 | static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw) |
| 282 | { |
| 283 | struct msgb *msg = st_msgb_alloc(); |
| 284 | struct cardemu_usb_msg_tx_data *txd; |
| 285 | uint8_t *cur; |
| 286 | |
| 287 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
| 288 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 289 | printf("SIMtrace <= %s(%02x %02x)\n", __func__, sw[0], sw[1]); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 290 | |
| 291 | memset(txd, 0, sizeof(*txd)); |
| 292 | txd->data_len = 2; |
| 293 | txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL; |
| 294 | cur = msgb_put(msg, 2); |
| 295 | cur[0] = sw[0]; |
| 296 | cur[1] = sw[1]; |
| 297 | |
| 298 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
| 299 | } |
| 300 | |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 301 | // FIXME check if the ATR actually includes a checksum |
Harald Welte | 3f09f63 | 2019-03-31 15:51:13 +0200 | [diff] [blame] | 302 | __attribute__((unused)) static void atr_update_csum(uint8_t *atr, unsigned int atr_len) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 303 | { |
| 304 | uint8_t csum = 0; |
| 305 | int i; |
| 306 | |
| 307 | for (i = 1; i < atr_len - 1; i++) |
| 308 | csum = csum ^ atr[i]; |
| 309 | |
| 310 | atr[atr_len-1] = csum; |
| 311 | } |
| 312 | |
| 313 | static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len) |
| 314 | { |
| 315 | struct msgb *msg = st_msgb_alloc(); |
| 316 | struct cardemu_usb_msg_set_atr *satr; |
| 317 | uint8_t *cur; |
| 318 | |
| 319 | satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr)); |
| 320 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 321 | printf("SIMtrace <= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len)); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 322 | |
| 323 | memset(satr, 0, sizeof(*satr)); |
| 324 | satr->atr_len = atr_len; |
| 325 | cur = msgb_put(msg, atr_len); |
| 326 | memcpy(cur, atr, atr_len); |
| 327 | |
| 328 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR); |
| 329 | } |
| 330 | |
| 331 | /*********************************************************************** |
| 332 | * Modem Control protocol |
| 333 | ***********************************************************************/ |
| 334 | |
| 335 | static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms) |
| 336 | { |
| 337 | struct msgb *msg = st_msgb_alloc(); |
| 338 | struct st_modem_reset *sr ; |
| 339 | |
| 340 | sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr)); |
| 341 | sr->asserted = asserted; |
| 342 | sr->pulse_duration_msec = pulse_ms; |
| 343 | |
| 344 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET); |
| 345 | } |
| 346 | |
| 347 | /*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/ |
| 348 | int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms) |
| 349 | { |
| 350 | return _modem_reset(slot, 2, duration_ms); |
| 351 | } |
| 352 | |
| 353 | /*! \brief assert the RESET line of the modem */ |
| 354 | int st_modem_reset_active(struct st_slot *slot) |
| 355 | { |
| 356 | return _modem_reset(slot, 1, 0); |
| 357 | } |
| 358 | |
| 359 | /*! \brief de-assert the RESET line of the modem */ |
| 360 | int st_modem_reset_inactive(struct st_slot *slot) |
| 361 | { |
| 362 | return _modem_reset(slot, 0, 0); |
| 363 | } |
| 364 | |
| 365 | static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim) |
| 366 | { |
| 367 | struct msgb *msg = st_msgb_alloc(); |
| 368 | struct st_modem_sim_select *ss; |
| 369 | |
| 370 | ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss)); |
| 371 | ss->remote_sim = remote_sim; |
| 372 | |
| 373 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT); |
| 374 | } |
| 375 | |
| 376 | /*! \brief select local (physical) SIM for given slot */ |
| 377 | int st_modem_sim_select_local(struct st_slot *slot) |
| 378 | { |
| 379 | return _modem_sim_select(slot, 0); |
| 380 | } |
| 381 | |
| 382 | /*! \brief select remote (emulated/forwarded) SIM for given slot */ |
| 383 | int st_modem_sim_select_remote(struct st_slot *slot) |
| 384 | { |
| 385 | return _modem_sim_select(slot, 1); |
| 386 | } |
| 387 | |
| 388 | /*! \brief Request slot to send us status information about the modem */ |
| 389 | int st_modem_get_status(struct st_slot *slot) |
| 390 | { |
| 391 | struct msgb *msg = st_msgb_alloc(); |
| 392 | |
| 393 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS); |
| 394 | } |
| 395 | |
| 396 | |
| 397 | /*********************************************************************** |
| 398 | * Incoming Messages |
| 399 | ***********************************************************************/ |
| 400 | |
| 401 | /*! \brief Process a STATUS message from the SIMtrace2 */ |
| 402 | static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len) |
| 403 | { |
| 404 | struct cardemu_usb_msg_status *status; |
| 405 | status = (struct cardemu_usb_msg_status *) buf; |
| 406 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 407 | printf("SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n", |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 408 | status->flags, status->fi, status->di, status->wi, |
| 409 | status->waiting_time); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /*! \brief Process a PTS indication message from the SIMtrace2 */ |
| 415 | static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len) |
| 416 | { |
| 417 | struct cardemu_usb_msg_pts_info *pts; |
| 418 | pts = (struct cardemu_usb_msg_pts_info *) buf; |
| 419 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 420 | printf("SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req))); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /*! \brief Process a ERROR indication message from the SIMtrace2 */ |
Harald Welte | 3f09f63 | 2019-03-31 15:51:13 +0200 | [diff] [blame] | 426 | __attribute__((unused)) static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 427 | { |
| 428 | struct cardemu_usb_msg_error *err; |
| 429 | err = (struct cardemu_usb_msg_error *) buf; |
| 430 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 431 | printf("SIMtrace => ERROR: %u/%u/%u: %s\n", |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 432 | err->severity, err->subsystem, err->code, |
| 433 | err->msg_len ? (char *)err->msg : ""); |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
Harald Welte | fd5dafc | 2019-12-15 21:14:14 +0100 | [diff] [blame] | 438 | static struct osmo_apdu_context ac; // this will hold the complete APDU (across calls) |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 439 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 440 | /*! \brief Process a RX-DATA indication message from the SIMtrace2 */ |
| 441 | static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len) |
| 442 | { |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 443 | 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] | 444 | int rc; |
| 445 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 446 | printf("SIMtrace => DATA: flags=%x, %s: ", data->flags, |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 447 | osmo_hexdump(data->data, data->data_len)); |
| 448 | |
Harald Welte | fd5dafc | 2019-12-15 21:14:14 +0100 | [diff] [blame] | 449 | rc = osmo_apdu_segment_in(&ac, data->data, data->data_len, |
| 450 | 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] | 451 | |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 452 | if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { // there is no pending data coming from the modem |
Harald Welte | 2ea20b9 | 2019-03-30 08:33:49 +0100 | [diff] [blame] | 453 | uint8_t apdu_command[sizeof(ac.hdr) + ac.lc.tot]; // to store the APDU command to send |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 454 | 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] | 455 | if (ac.lc.tot) { |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 456 | 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] | 457 | } |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 458 | // send APDU to card |
Harald Welte | 9cf013a | 2019-03-11 22:19:19 +0100 | [diff] [blame] | 459 | BankSlot_t bslot; |
| 460 | bank_slot2rspro(&bslot, &g_client->bankd_slot); |
| 461 | RsproPDU_t *pdu = rspro_gen_TpduModem2Card(g_client->srv_conn.clslot, &bslot, apdu_command, sizeof(ac.hdr) + ac.lc.tot); // create RSPRO packet |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 462 | server_conn_send_rspro(&g_client->bankd_conn, pdu); |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 463 | // the response will come separately |
Kévin Redon | bc08db5 | 2018-10-11 08:41:00 +0200 | [diff] [blame] | 464 | } else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem |
| 465 | 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] | 466 | } |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | #if 0 |
| 471 | case SIMTRACE_CMD_DO_ERROR |
| 472 | rc = process_do_error(ci, buf, len); |
| 473 | break; |
| 474 | #endif |
| 475 | |
| 476 | /*! \brief Process an incoming message from the SIMtrace2 */ |
| 477 | static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len) |
| 478 | { |
| 479 | struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf; |
| 480 | int rc; |
| 481 | |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 482 | printf("SIMtrace -> %s\n", osmo_hexdump(buf, len)); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 483 | |
| 484 | buf += sizeof(*sh); |
| 485 | |
| 486 | switch (sh->msg_type) { |
| 487 | case SIMTRACE_MSGT_BD_CEMU_STATUS: |
| 488 | rc = process_do_status(ci, buf, len); |
| 489 | break; |
| 490 | case SIMTRACE_MSGT_DO_CEMU_PTS: |
| 491 | rc = process_do_pts(ci, buf, len); |
| 492 | break; |
| 493 | case SIMTRACE_MSGT_DO_CEMU_RX_DATA: |
| 494 | rc = process_do_rx_da(ci, buf, len); |
| 495 | break; |
| 496 | default: |
| 497 | printf("unknown simtrace msg type 0x%02x\n", sh->msg_type); |
| 498 | rc = -1; |
| 499 | break; |
| 500 | } |
| 501 | |
| 502 | return rc; |
| 503 | } |
| 504 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 505 | static void usb_in_xfer_cb(struct libusb_transfer *xfer) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 506 | { |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 507 | struct cardem_inst *ci = xfer->user_data; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 508 | int rc; |
| 509 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 510 | switch (xfer->status) { |
| 511 | case LIBUSB_TRANSFER_COMPLETED: |
| 512 | /* hand the message up the stack */ |
| 513 | process_usb_msg(ci, xfer->buffer, xfer->actual_length); |
| 514 | break; |
| 515 | case LIBUSB_TRANSFER_NO_DEVICE: |
| 516 | fprintf(stderr, "USB device disappeared\n"); |
| 517 | exit(23); |
| 518 | break; |
| 519 | default: |
| 520 | osmo_panic("USB IN transfer failed, status=%u\n", xfer->status); |
| 521 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 522 | } |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 523 | |
| 524 | /* re-submit the IN transfer */ |
| 525 | rc = libusb_submit_transfer(xfer); |
| 526 | OSMO_ASSERT(rc == 0); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 527 | } |
| 528 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 529 | |
| 530 | static void allocate_and_submit_in(struct cardem_inst *ci) |
| 531 | { |
| 532 | struct st_transport *transp = ci->slot->transp; |
| 533 | struct libusb_transfer *xfer; |
| 534 | int rc; |
| 535 | |
| 536 | xfer = libusb_alloc_transfer(0); |
| 537 | OSMO_ASSERT(xfer); |
| 538 | xfer->dev_handle = transp->usb_devh; |
| 539 | xfer->flags = 0; |
| 540 | xfer->type = LIBUSB_TRANSFER_TYPE_BULK; |
| 541 | xfer->endpoint = transp->usb_ep.in; |
| 542 | xfer->timeout = 0; |
| 543 | xfer->user_data = ci; |
| 544 | xfer->length = 16*256; |
| 545 | |
| 546 | xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length); |
| 547 | OSMO_ASSERT(xfer->buffer); |
| 548 | xfer->callback = usb_in_xfer_cb; |
| 549 | |
| 550 | /* submit the IN transfer */ |
| 551 | rc = libusb_submit_transfer(xfer); |
| 552 | OSMO_ASSERT(rc == 0); |
| 553 | } |
| 554 | |
| 555 | |
| 556 | static void usb_irq_xfer_cb(struct libusb_transfer *xfer) |
| 557 | { |
| 558 | int rc; |
| 559 | |
| 560 | switch (xfer->status) { |
| 561 | case LIBUSB_TRANSFER_COMPLETED: |
| 562 | /* FIXME: do something with the received data */ |
| 563 | break; |
| 564 | case LIBUSB_TRANSFER_NO_DEVICE: |
| 565 | fprintf(stderr, "USB device disappeared\n"); |
| 566 | exit(23); |
| 567 | break; |
| 568 | default: |
| 569 | osmo_panic("USB IRQ transfer failed, status=%u\n", xfer->status); |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | /* re-submit the IN transfer */ |
| 574 | rc = libusb_submit_transfer(xfer); |
| 575 | OSMO_ASSERT(rc == 0); |
| 576 | } |
| 577 | |
| 578 | |
| 579 | static void allocate_and_submit_irq(struct cardem_inst *ci) |
| 580 | { |
| 581 | struct st_transport *transp = ci->slot->transp; |
| 582 | struct libusb_transfer *xfer; |
| 583 | int rc; |
| 584 | |
| 585 | xfer = libusb_alloc_transfer(0); |
| 586 | OSMO_ASSERT(xfer); |
| 587 | xfer->dev_handle = transp->usb_devh; |
| 588 | xfer->flags = 0; |
| 589 | xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; |
| 590 | xfer->endpoint = transp->usb_ep.irq_in; |
| 591 | xfer->timeout = 0; |
| 592 | xfer->user_data = ci; |
| 593 | xfer->length = 64; |
| 594 | |
| 595 | xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length); |
| 596 | OSMO_ASSERT(xfer->buffer); |
| 597 | xfer->callback = usb_irq_xfer_cb; |
| 598 | |
| 599 | /* submit the IN transfer */ |
| 600 | rc = libusb_submit_transfer(xfer); |
| 601 | OSMO_ASSERT(rc == 0); |
| 602 | } |
| 603 | |
| 604 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 605 | static struct st_transport _transp; |
| 606 | |
| 607 | static struct st_slot _slot = { |
| 608 | .transp = &_transp, |
| 609 | .slot_nr = 0, |
| 610 | }; |
| 611 | |
| 612 | struct cardem_inst _ci = { |
| 613 | .slot = &_slot, |
| 614 | }; |
| 615 | |
| 616 | struct cardem_inst *ci = &_ci; |
| 617 | |
| 618 | static void signal_handler(int signal) |
| 619 | { |
| 620 | switch (signal) { |
| 621 | case SIGINT: |
| 622 | cardem_request_card_insert(ci, false); |
| 623 | exit(0); |
| 624 | break; |
| 625 | default: |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /** remsim_client **/ |
| 631 | |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 632 | static int bankd_handle_tpduCardToModem(struct bankd_client *bc, const RsproPDU_t *pdu) |
Kévin Redon | 9b69a3f | 2018-10-11 08:41:57 +0200 | [diff] [blame] | 633 | { |
| 634 | OSMO_ASSERT(pdu); |
| 635 | OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present); |
| 636 | |
| 637 | const struct TpduCardToModem *card2modem = &pdu->msg.choice.tpduCardToModem; |
| 638 | if (card2modem->data.size < 2) { // at least the two SW bytes are needed |
| 639 | return -1; |
| 640 | } |
| 641 | |
| 642 | // save SW to our current APDU context |
| 643 | ac.sw[0] = card2modem->data.buf[card2modem->data.size - 2]; |
| 644 | ac.sw[1] = card2modem->data.buf[card2modem->data.size - 1]; |
Kévin Redon | 21e31de | 2018-10-11 17:25:58 +0200 | [diff] [blame] | 645 | printf("SIMtrace <= SW=0x%02x%02x, len_rx=%d\n", ac.sw[0], ac.sw[1], card2modem->data.size - 2); |
Kévin Redon | 9b69a3f | 2018-10-11 08:41:57 +0200 | [diff] [blame] | 646 | if (card2modem->data.size > 2) { // send PB and data to modem |
| 647 | cardem_request_pb_and_tx(ci, ac.hdr.ins, card2modem->data.buf, card2modem->data.size - 2); |
| 648 | } |
| 649 | cardem_request_sw_tx(ci, ac.sw); // send SW to modem |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 654 | static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu) |
Harald Welte | fa36559 | 2019-03-28 20:28:57 +0100 | [diff] [blame] | 655 | { |
| 656 | RsproPDU_t *resp; |
| 657 | int rc; |
| 658 | |
| 659 | OSMO_ASSERT(pdu); |
| 660 | OSMO_ASSERT(RsproPDUchoice_PR_setAtrReq == pdu->msg.present); |
| 661 | |
| 662 | /* FIXME: is this permitted at any time by the SIMtrace2 cardemfirmware? */ |
| 663 | rc = cardem_request_set_atr(ci, pdu->msg.choice.setAtrReq.atr.buf, |
| 664 | pdu->msg.choice.setAtrReq.atr.size); |
| 665 | if (rc == 0) |
| 666 | resp = rspro_gen_SetAtrRes(ResultCode_ok); |
| 667 | else |
| 668 | resp = rspro_gen_SetAtrRes(ResultCode_cardTransmissionError); |
| 669 | if (!resp) |
| 670 | return -ENOMEM; |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 671 | server_conn_send_rspro(&g_client->bankd_conn, resp); |
Harald Welte | fa36559 | 2019-03-28 20:28:57 +0100 | [diff] [blame] | 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 676 | /* handle incoming message from bankd */ |
| 677 | static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu) |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 678 | { |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 679 | switch (pdu->msg.present) { |
| 680 | case RsproPDUchoice_PR_connectClientRes: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 681 | /* Store 'identity' of bankd to in peer_comp_id */ |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 682 | rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity); |
| 683 | osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 684 | break; |
Kévin Redon | 9b69a3f | 2018-10-11 08:41:57 +0200 | [diff] [blame] | 685 | case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 686 | bankd_handle_tpduCardToModem(g_client, pdu); |
Kévin Redon | 9b69a3f | 2018-10-11 08:41:57 +0200 | [diff] [blame] | 687 | break; |
Harald Welte | fa36559 | 2019-03-28 20:28:57 +0100 | [diff] [blame] | 688 | case RsproPDUchoice_PR_setAtrReq: |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 689 | bankd_handle_setAtrReq(g_client, pdu); |
Harald Welte | fa36559 | 2019-03-28 20:28:57 +0100 | [diff] [blame] | 690 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 691 | default: |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 692 | LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s\n", |
| 693 | rspro_msgt_name(pdu)); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 694 | return -1; |
| 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 700 | /* handle incoming messages from server */ |
| 701 | static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu) |
| 702 | { |
| 703 | RsproPDU_t *resp; |
| 704 | |
| 705 | switch (pdu->msg.present) { |
| 706 | case RsproPDUchoice_PR_connectClientRes: |
| 707 | /* Store 'identity' of server in srvc->peer_comp_id */ |
| 708 | rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity); |
| 709 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu); |
| 710 | break; |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 711 | case RsproPDUchoice_PR_configClientIdReq: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 712 | /* store/set the clientID as instructed by the server */ |
Harald Welte | ec628e9 | 2019-03-08 22:18:31 +0100 | [diff] [blame] | 713 | if (!g_client->srv_conn.clslot) |
| 714 | g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 715 | *g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot; |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 716 | if (!g_client->bankd_conn.clslot) |
| 717 | g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
| 718 | *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot; |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 719 | /* send response to server */ |
| 720 | resp = rspro_gen_ConfigClientIdRes(ResultCode_ok); |
| 721 | server_conn_send_rspro(srvc, resp); |
| 722 | break; |
| 723 | case RsproPDUchoice_PR_configClientBankReq: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 724 | /* store/set the bankd ip/port as instructed by the server */ |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 725 | osmo_talloc_replace_string(g_client, &g_client->bankd_conn.server_host, |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 726 | rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip)); |
Harald Welte | 9cf013a | 2019-03-11 22:19:19 +0100 | [diff] [blame] | 727 | rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot); |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 728 | g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port; |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 729 | /* instruct bankd FSM to connect */ |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 730 | osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 731 | /* send response to server */ |
Harald Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 732 | resp = rspro_gen_ConfigClientBankRes(ResultCode_ok); |
Harald Welte | a844bb0 | 2019-03-09 13:38:50 +0100 | [diff] [blame] | 733 | server_conn_send_rspro(srvc, resp); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 734 | break; |
| 735 | default: |
Harald Welte | 8d8d4f1 | 2019-03-27 22:50:39 +0100 | [diff] [blame] | 736 | LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n", |
| 737 | rspro_msgt_name(pdu)); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 738 | return -1; |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 744 | static void handle_sig_usr1(int signal) |
| 745 | { |
| 746 | OSMO_ASSERT(signal == SIGUSR1); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 747 | talloc_report_full(g_tall_ctx, stderr); |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 748 | } |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 749 | |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 750 | static void print_welcome(void) |
| 751 | { |
| 752 | printf("simtrace2-remsim-client - Remote SIM card client for SIMtrace\n" |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 753 | "(C) 2010-2019, Harald Welte <laforge@gnumonks.org>\n" |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 754 | "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n"); |
| 755 | } |
| 756 | |
| 757 | static void print_help(void) |
| 758 | { |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 759 | printf( "\t-s\t--server-host HOST\n" |
Kévin Redon | da1854c | 2019-09-17 14:16:54 +0200 | [diff] [blame] | 760 | "\t-p\t--server-port PORT\n" |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 761 | "\t-c\t--client-id <0-65535>\n" |
Kévin Redon | da1854c | 2019-09-17 14:16:54 +0200 | [diff] [blame] | 762 | "\t-n\t--client-slot <0-65535>\n" |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 763 | "\t-h\t--help\n" |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 764 | "\t-v\t--version\n" |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 765 | "\t-i\t--gsmtap-ip\tA.B.C.D\n" |
| 766 | "\t-k\t--keep-running\n" |
| 767 | "\t-V\t--usb-vendor\tVENDOR_ID\n" |
| 768 | "\t-P\t--usb-product\tPRODUCT_ID\n" |
| 769 | "\t-C\t--usb-config\tCONFIG_ID\n" |
| 770 | "\t-I\t--usb-interface\tINTERFACE_ID\n" |
| 771 | "\t-S\t--usb-altsetting ALTSETTING_ID\n" |
| 772 | "\t-A\t--usb-address\tADDRESS\n" |
| 773 | "\t-H\t--usb-path\tPATH\n" |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 774 | "\t-a\t--atr\tATR\n" |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 775 | "\n" |
| 776 | ); |
| 777 | } |
| 778 | |
| 779 | static const struct option opts[] = { |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 780 | { "server-host", 1, 0, 's' }, |
| 781 | { "server-port", 1, 0, 'p' }, |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 782 | { "client-id", 1, 0, 'c' }, |
| 783 | { "client-slot", 1, 0, 'n' }, |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 784 | { "help", 0, 0, 'h' }, |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 785 | { "version", 0, 0, 'v' }, |
Harald Welte | fd5a62a | 2019-03-30 09:06:39 +0100 | [diff] [blame] | 786 | { "gsmtap-ip", 1, 0, 'i' }, |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 787 | { "keep-running", 0, 0, 'k' }, |
| 788 | { "usb-vendor", 1, 0, 'V' }, |
| 789 | { "usb-product", 1, 0, 'P' }, |
| 790 | { "usb-config", 1, 0, 'C' }, |
| 791 | { "usb-interface", 1, 0, 'I' }, |
| 792 | { "usb-altsetting", 1, 0, 'S' }, |
| 793 | { "usb-address", 1, 0, 'A' }, |
| 794 | { "usb-path", 1, 0, 'H' }, |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 795 | { "atr", 1, 0, 'a' }, |
Kévin Redon | 3ec265b | 2018-10-11 17:30:33 +0200 | [diff] [blame] | 796 | { NULL, 0, 0, 0 } |
| 797 | }; |
| 798 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 799 | int main(int argc, char **argv) |
| 800 | { |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 801 | struct rspro_server_conn *srvc, *bankdc; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 802 | struct st_transport *transp = ci->slot->transp; |
| 803 | char *gsmtap_host = "127.0.0.1"; |
| 804 | int rc; |
| 805 | int c, ret = 1; |
| 806 | int keep_running = 0; |
Harald Welte | 7817b20 | 2019-03-30 08:34:14 +0100 | [diff] [blame] | 807 | int server_port = 9998; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 808 | int if_num = 0, vendor_id = -1, product_id = -1; |
| 809 | int config_id = -1, altsetting = 0, addr = -1; |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 810 | int client_id = -1, client_slot = -1; |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 811 | char *server_host = "127.0.0.1"; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 812 | char *path = NULL; |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 813 | uint8_t atr_data[33] = { 0x3B, 0x00 }; // the shortest simplest ATR possible |
| 814 | uint8_t atr_len = 2; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 815 | |
| 816 | print_welcome(); |
| 817 | |
| 818 | while (1) { |
| 819 | int option_index = 0; |
| 820 | |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 821 | c = getopt_long(argc, argv, "s:p:c:n:hvi:kV:P:C:I:S:A:H:a:", opts, &option_index); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 822 | if (c == -1) |
| 823 | break; |
| 824 | switch (c) { |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 825 | case 's': |
| 826 | server_host = optarg; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 827 | break; |
| 828 | case 'p': |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 829 | server_port = atoi(optarg); |
Kévin Redon | fbca97a | 2018-10-11 19:13:24 +0200 | [diff] [blame] | 830 | break; |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 831 | case 'c': |
| 832 | client_id = atoi(optarg); |
| 833 | break; |
| 834 | case 'n': |
| 835 | client_slot = atoi(optarg); |
| 836 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 837 | case 'h': |
| 838 | print_help(); |
| 839 | exit(0); |
| 840 | break; |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 841 | case 'v': |
| 842 | printf("osmo-remsim-client version %s\n", VERSION); |
| 843 | exit(0); |
| 844 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 845 | case 'i': |
| 846 | gsmtap_host = optarg; |
| 847 | break; |
| 848 | case 'k': |
| 849 | keep_running = 1; |
| 850 | break; |
| 851 | case 'V': |
| 852 | vendor_id = strtol(optarg, NULL, 16); |
| 853 | break; |
| 854 | case 'P': |
| 855 | product_id = strtol(optarg, NULL, 16); |
| 856 | break; |
| 857 | case 'C': |
| 858 | config_id = atoi(optarg); |
| 859 | break; |
| 860 | case 'I': |
| 861 | if_num = atoi(optarg); |
| 862 | break; |
| 863 | case 'S': |
| 864 | altsetting = atoi(optarg); |
| 865 | break; |
| 866 | case 'A': |
| 867 | addr = atoi(optarg); |
| 868 | break; |
| 869 | case 'H': |
| 870 | path = optarg; |
| 871 | break; |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 872 | case 'a': |
| 873 | rc = osmo_hexparse(optarg, atr_data, ARRAY_SIZE(atr_data)); |
| 874 | if (rc < 2 || rc > ARRAY_SIZE(atr_data)) { |
| 875 | fprintf(stderr, "ATR matlformed\n"); |
| 876 | goto do_exit; |
| 877 | } |
| 878 | atr_len = rc; |
| 879 | break; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Kévin Redon | 39eb9dc | 2018-10-11 17:21:13 +0200 | [diff] [blame] | 883 | if (vendor_id < 0 || product_id < 0) { |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 884 | fprintf(stderr, "You have to specify the vendor and product ID\n"); |
| 885 | goto do_exit; |
| 886 | } |
| 887 | |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 888 | signal(SIGUSR1, handle_sig_usr1); |
| 889 | |
Harald Welte | 972a1e8 | 2019-03-30 08:34:30 +0100 | [diff] [blame] | 890 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 891 | talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1"); |
Harald Welte | f7442b5 | 2019-07-18 18:54:05 +0200 | [diff] [blame] | 892 | msgb_talloc_ctx_init(g_tall_ctx, 0); |
Harald Welte | 972a1e8 | 2019-03-30 08:34:30 +0100 | [diff] [blame] | 893 | osmo_init_logging2(g_tall_ctx, &log_info); |
| 894 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 895 | rc = osmo_libusb_init(NULL); |
Kévin Redon | 193a8c1 | 2018-10-11 17:24:39 +0200 | [diff] [blame] | 896 | if (rc < 0) { |
| 897 | fprintf(stderr, "libusb initialization failed\n"); |
| 898 | goto do_exit; |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0); |
| 902 | if (!g_gti) { |
| 903 | perror("unable to open GSMTAP"); |
| 904 | goto close_exit; |
| 905 | } |
| 906 | gsmtap_source_add_sink(g_gti); |
| 907 | |
| 908 | signal(SIGINT, &signal_handler); |
| 909 | |
| 910 | // initialize remote SIM client |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 911 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 912 | g_client = talloc_zero(g_tall_ctx, struct bankd_client); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 913 | |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 914 | if (client_id != -1) { |
| 915 | /* default to client slot 0 */ |
| 916 | if (client_slot == -1) |
| 917 | client_slot = 0; |
| 918 | g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
| 919 | g_client->srv_conn.clslot->clientId = client_id; |
| 920 | g_client->srv_conn.clslot->slotNr = client_slot; |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 921 | g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t); |
| 922 | *g_client->bankd_conn.clslot = *g_client->srv_conn.clslot; |
Harald Welte | 72cde10 | 2019-03-30 10:43:06 +0100 | [diff] [blame] | 923 | } |
| 924 | |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 925 | srvc = &g_client->srv_conn; |
| 926 | srvc->server_host = server_host; |
| 927 | srvc->server_port = server_port; |
| 928 | srvc->handle_rx = srvc_handle_rx; |
| 929 | srvc->own_comp_id.type = ComponentType_remsimClient; |
| 930 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client"); |
| 931 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client"); |
| 932 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION); |
| 933 | rc = server_conn_fsm_alloc(g_client, srvc); |
| 934 | if (rc < 0) { |
| 935 | fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno)); |
| 936 | exit(1); |
| 937 | } |
Harald Welte | d2192e2 | 2019-11-07 18:10:57 +0100 | [diff] [blame] | 938 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 939 | |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 940 | bankdc = &g_client->bankd_conn; |
| 941 | /* server_host / server_port are configured from remsim-server */ |
| 942 | bankdc->handle_rx = bankd_handle_rx; |
| 943 | memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id)); |
| 944 | rc = server_conn_fsm_alloc(g_client, bankdc); |
| 945 | if (rc < 0) { |
| 946 | fprintf(stderr, "Unable to create bankd conn FSM: %s\n", strerror(errno)); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 947 | exit(1); |
| 948 | } |
Harald Welte | 8219445 | 2019-12-14 17:18:34 +0100 | [diff] [blame] | 949 | osmo_fsm_inst_update_id(bankdc->fi, "bankd"); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 950 | |
Harald Welte | 3e9860b | 2019-12-02 23:04:54 +0100 | [diff] [blame] | 951 | asn_debug = 0; |
| 952 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 953 | // connect to SIMtrace2 cardem |
| 954 | do { |
| 955 | struct usb_interface_match _ifm, *ifm = &_ifm; |
| 956 | ifm->vendor = vendor_id; |
| 957 | ifm->product = product_id; |
| 958 | ifm->configuration = config_id; |
| 959 | ifm->interface = if_num; |
| 960 | ifm->altsetting = altsetting; |
| 961 | ifm->addr = addr; |
| 962 | if (path) |
| 963 | osmo_strlcpy(ifm->path, path, sizeof(ifm->path)); |
Harald Welte | fd5dafc | 2019-12-15 21:14:14 +0100 | [diff] [blame] | 964 | transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 965 | if (!transp->usb_devh) { |
| 966 | fprintf(stderr, "can't open USB device\n"); |
| 967 | goto close_exit; |
| 968 | } |
| 969 | |
| 970 | rc = libusb_claim_interface(transp->usb_devh, if_num); |
| 971 | if (rc < 0) { |
| 972 | fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc); |
| 973 | goto close_exit; |
| 974 | } |
| 975 | |
Harald Welte | fd5dafc | 2019-12-15 21:14:14 +0100 | [diff] [blame] | 976 | rc = osmo_libusb_get_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out, |
| 977 | &transp->usb_ep.in, &transp->usb_ep.irq_in); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 978 | if (rc < 0) { |
| 979 | fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc); |
| 980 | goto close_exit; |
| 981 | } |
| 982 | |
Kévin Redon | 3428e41 | 2018-10-11 19:14:00 +0200 | [diff] [blame] | 983 | // switch modem SIM port to emulated SIM on OWHW |
| 984 | if (USB_VENDOR_OPENMOKO == ifm->vendor && USB_PRODUCT_OWHW_SAM3 == ifm->product) { // we are on the OWHW |
| 985 | int modem = -1; |
| 986 | switch (ifm->interface) { // the USB interface indicates for which modem we want to emulate the SIM |
| 987 | case 0: |
| 988 | modem = 1; |
| 989 | break; |
| 990 | case 1: |
| 991 | modem = 2; |
| 992 | break; |
| 993 | default: |
| 994 | fprintf(stderr, "unknown GPIO for SIMtrace interface %d\n", ifm->interface); |
| 995 | goto close_exit; |
| 996 | } |
| 997 | // |
| 998 | char gpio_path[PATH_MAX]; |
| 999 | snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/connect_st_usim%d/value", modem); |
| 1000 | int connec_st_usim = open(gpio_path, O_WRONLY); |
| 1001 | if (-1 == connec_st_usim) { |
| 1002 | fprintf(stderr, "can't open GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem); |
| 1003 | goto close_exit; |
| 1004 | } |
| 1005 | if (1 != write(connec_st_usim, "1", 1)) { |
| 1006 | fprintf(stderr, "can't write GPIO %s to switch modem %d to emulated USIM\n", gpio_path, modem); |
| 1007 | goto close_exit; |
| 1008 | } |
| 1009 | printf("switched modem %d to emulated USIM\n", modem); |
| 1010 | |
| 1011 | snprintf(gpio_path, sizeof(gpio_path), "/dev/gpio/mdm%d_rst/value", modem); |
| 1012 | int mdm_rst = open(gpio_path, O_WRONLY); |
| 1013 | if (-1 == mdm_rst) { |
| 1014 | fprintf(stderr, "can't open GPIO %s to reset modem %d\n", gpio_path, modem); |
| 1015 | goto close_exit; |
| 1016 | } |
| 1017 | if (1 != write(mdm_rst, "1", 1)) { |
| 1018 | fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem); |
| 1019 | goto close_exit; |
| 1020 | } |
| 1021 | sleep(1); // wait a bit to ensure reset is effective |
| 1022 | if (1 != write(mdm_rst, "0", 1)) { |
| 1023 | fprintf(stderr, "can't write GPIO %s to reset modem %d\n", gpio_path, modem); |
| 1024 | goto close_exit; |
| 1025 | } |
| 1026 | printf("modem %d reset\n", modem); |
| 1027 | } |
| 1028 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1029 | /* simulate card-insert to modem (owhw, not qmod) */ |
| 1030 | cardem_request_card_insert(ci, true); |
| 1031 | |
| 1032 | /* select remote (forwarded) SIM */ |
| 1033 | st_modem_sim_select_remote(ci->slot); |
| 1034 | |
| 1035 | /* set the ATR */ |
Kévin Redon | 206c3d7 | 2018-11-12 22:51:37 +0100 | [diff] [blame] | 1036 | //atr_update_csum(real_atr, sizeof(real_atr)); |
| 1037 | cardem_request_set_atr(ci, atr_data, atr_len); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1038 | |
| 1039 | /* select remote (forwarded) SIM */ |
| 1040 | st_modem_reset_pulse(ci->slot, 300); |
| 1041 | |
Harald Welte | 32e2e00 | 2019-12-15 23:01:54 +0100 | [diff] [blame] | 1042 | printf("Entering main loop\n"); |
| 1043 | |
| 1044 | allocate_and_submit_irq(ci); |
| 1045 | allocate_and_submit_in(ci); |
| 1046 | |
| 1047 | while (1) { |
| 1048 | osmo_select_main(false); |
| 1049 | } |
| 1050 | |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1051 | ret = 0; |
| 1052 | |
Kévin Redon | 193a8c1 | 2018-10-11 17:24:39 +0200 | [diff] [blame] | 1053 | libusb_release_interface(transp->usb_devh, 0); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1054 | close_exit: |
| 1055 | if (transp->usb_devh) |
| 1056 | libusb_close(transp->usb_devh); |
| 1057 | if (keep_running) |
| 1058 | sleep(1); |
| 1059 | } while (keep_running); |
| 1060 | |
Kévin Redon | 193a8c1 | 2018-10-11 17:24:39 +0200 | [diff] [blame] | 1061 | libusb_exit(NULL); |
Kévin Redon | e0b837c | 2018-10-10 00:39:25 +0200 | [diff] [blame] | 1062 | do_exit: |
| 1063 | return ret; |
| 1064 | } |