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