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