Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 1 | |
| 2 | /* simtrace2-protocol - USB protocol library code for SIMtrace2 |
| 3 | * |
| 4 | * (C) 2016-2019 by Harald Welte <hwelte@hmw-consulting.de> |
| 5 | * (C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <stdint.h> |
| 28 | #include <signal.h> |
| 29 | #include <time.h> |
| 30 | #define _GNU_SOURCE |
| 31 | #include <getopt.h> |
| 32 | |
| 33 | #include <sys/time.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <netinet/in.h> |
| 37 | #include <arpa/inet.h> |
| 38 | |
| 39 | #include <libusb.h> |
| 40 | |
| 41 | //#include <osmocom/simtrace2/libusb_util.h> |
| 42 | #include <osmocom/simtrace2/simtrace_prot.h> |
| 43 | #include <osmocom/simtrace2/simtrace2_api.h> |
| 44 | //#include "apdu_dispatch.h" |
| 45 | //#include "simtrace2-discovery.h" |
| 46 | |
| 47 | #include <osmocom/core/utils.h> |
| 48 | #include <osmocom/core/socket.h> |
| 49 | #include <osmocom/core/msgb.h> |
| 50 | #include <osmocom/sim/class_tables.h> |
| 51 | #include <osmocom/sim/sim.h> |
| 52 | |
| 53 | /*********************************************************************** |
| 54 | * SIMTRACE core protocol |
| 55 | ***********************************************************************/ |
| 56 | |
| 57 | /*! \brief allocate a message buffer for simtrace use */ |
| 58 | static struct msgb *st_msgb_alloc(void) |
| 59 | { |
| 60 | return msgb_alloc_headroom(1024+32, 32, "SIMtrace"); |
| 61 | } |
| 62 | |
| 63 | #if 0 |
| 64 | static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data) |
| 65 | { |
| 66 | printf("APDU: %s\n", osmo_hexdump(buf, len)); |
| 67 | gsmtap_send_sim(buf, len); |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | /*! \brief Transmit a given command to the SIMtrace2 device */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 72 | int osmo_st2_transp_tx_msg(struct osmo_st2_transport *transp, struct msgb *msg) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 73 | { |
| 74 | int rc; |
| 75 | |
| 76 | printf("<- %s\n", msgb_hexdump(msg)); |
| 77 | |
| 78 | if (transp->udp_fd < 0) { |
| 79 | int xfer_len; |
| 80 | |
| 81 | rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out, |
| 82 | msgb_data(msg), msgb_length(msg), |
| 83 | &xfer_len, 100000); |
| 84 | } else { |
| 85 | rc = write(transp->udp_fd, msgb_data(msg), msgb_length(msg)); |
| 86 | } |
| 87 | |
| 88 | msgb_free(msg); |
| 89 | return rc; |
| 90 | } |
| 91 | |
| 92 | static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type, |
| 93 | uint8_t slot_nr) |
| 94 | { |
| 95 | struct simtrace_msg_hdr *sh; |
| 96 | |
| 97 | sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh)); |
| 98 | memset(sh, 0, sizeof(*sh)); |
| 99 | sh->msg_class = msg_class; |
| 100 | sh->msg_type = msg_type; |
| 101 | sh->slot_nr = slot_nr; |
| 102 | sh->msg_len = msgb_length(msg); |
| 103 | |
| 104 | return sh; |
| 105 | } |
| 106 | |
| 107 | /* transmit a given message to a specified slot. Expects all headers |
| 108 | * present before calling the function */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 109 | int osmo_st2_slot_tx_msg(struct osmo_st2_slot *slot, struct msgb *msg, |
| 110 | uint8_t msg_class, uint8_t msg_type) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 111 | { |
| 112 | st_push_hdr(msg, msg_class, msg_type, slot->slot_nr); |
| 113 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 114 | return osmo_st2_transp_tx_msg(slot->transp, msg); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /*********************************************************************** |
| 118 | * Card Emulation protocol |
| 119 | ***********************************************************************/ |
| 120 | |
| 121 | |
| 122 | /*! \brief Request the SIMtrace2 to generate a card-insert signal */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 123 | int osmo_st2_cardem_request_card_insert(struct osmo_st2_cardem_inst *ci, bool inserted) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 124 | { |
| 125 | struct msgb *msg = st_msgb_alloc(); |
| 126 | struct cardemu_usb_msg_cardinsert *cins; |
| 127 | |
| 128 | cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins)); |
| 129 | memset(cins, 0, sizeof(*cins)); |
| 130 | if (inserted) |
| 131 | cins->card_insert = 1; |
| 132 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 133 | return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 137 | int osmo_st2_cardem_request_pb_and_rx(struct osmo_st2_cardem_inst *ci, uint8_t pb, uint8_t le) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 138 | { |
| 139 | struct msgb *msg = st_msgb_alloc(); |
| 140 | struct cardemu_usb_msg_tx_data *txd; |
| 141 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
| 142 | |
| 143 | printf("<= %s(%02x, %d)\n", __func__, pb, le); |
| 144 | |
| 145 | memset(txd, 0, sizeof(*txd)); |
| 146 | txd->data_len = 1; |
| 147 | txd->flags = CEMU_DATA_F_PB_AND_RX; |
| 148 | /* one data byte */ |
| 149 | msgb_put_u8(msg, pb); |
| 150 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 151 | return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 155 | int osmo_st2_cardem_request_pb_and_tx(struct osmo_st2_cardem_inst *ci, uint8_t pb, |
| 156 | const uint8_t *data, uint16_t data_len_in) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 157 | { |
| 158 | struct msgb *msg = st_msgb_alloc(); |
| 159 | struct cardemu_usb_msg_tx_data *txd; |
| 160 | uint8_t *cur; |
| 161 | |
| 162 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
| 163 | |
| 164 | printf("<= %s(%02x, %s, %d)\n", __func__, pb, |
| 165 | osmo_hexdump(data, data_len_in), data_len_in); |
| 166 | |
| 167 | memset(txd, 0, sizeof(*txd)); |
| 168 | txd->data_len = 1 + data_len_in; |
| 169 | txd->flags = CEMU_DATA_F_PB_AND_TX; |
| 170 | /* procedure byte */ |
| 171 | msgb_put_u8(msg, pb); |
| 172 | /* data */ |
| 173 | cur = msgb_put(msg, data_len_in); |
| 174 | memcpy(cur, data, data_len_in); |
| 175 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 176 | return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /*! \brief Request the SIMtrace2 to send a Status Word */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 180 | int osmo_st2_cardem_request_sw_tx(struct osmo_st2_cardem_inst *ci, const uint8_t *sw) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 181 | { |
| 182 | struct msgb *msg = st_msgb_alloc(); |
| 183 | struct cardemu_usb_msg_tx_data *txd; |
| 184 | uint8_t *cur; |
| 185 | |
| 186 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
| 187 | |
| 188 | printf("<= %s(%02x %02x)\n", __func__, sw[0], sw[1]); |
| 189 | |
| 190 | memset(txd, 0, sizeof(*txd)); |
| 191 | txd->data_len = 2; |
| 192 | txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL; |
| 193 | cur = msgb_put(msg, 2); |
| 194 | cur[0] = sw[0]; |
| 195 | cur[1] = sw[1]; |
| 196 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 197 | return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 200 | int osmo_st2_cardem_request_set_atr(struct osmo_st2_cardem_inst *ci, const uint8_t *atr, unsigned int atr_len) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 201 | { |
| 202 | struct msgb *msg = st_msgb_alloc(); |
| 203 | struct cardemu_usb_msg_set_atr *satr; |
| 204 | uint8_t *cur; |
| 205 | |
| 206 | satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr)); |
| 207 | |
| 208 | printf("<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len)); |
| 209 | |
| 210 | memset(satr, 0, sizeof(*satr)); |
| 211 | satr->atr_len = atr_len; |
| 212 | cur = msgb_put(msg, atr_len); |
| 213 | memcpy(cur, atr, atr_len); |
| 214 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 215 | return osmo_st2_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /*********************************************************************** |
| 219 | * Modem Control protocol |
| 220 | ***********************************************************************/ |
| 221 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 222 | static int _modem_reset(struct osmo_st2_slot *slot, uint8_t asserted, uint16_t pulse_ms) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 223 | { |
| 224 | struct msgb *msg = st_msgb_alloc(); |
| 225 | struct st_modem_reset *sr ; |
| 226 | |
| 227 | sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr)); |
| 228 | sr->asserted = asserted; |
| 229 | sr->pulse_duration_msec = pulse_ms; |
| 230 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 231 | return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 235 | int osmo_st2_modem_reset_pulse(struct osmo_st2_slot *slot, uint16_t duration_ms) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 236 | { |
| 237 | return _modem_reset(slot, 2, duration_ms); |
| 238 | } |
| 239 | |
| 240 | /*! \brief assert the RESET line of the modem */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 241 | int osmo_st2_modem_reset_active(struct osmo_st2_slot *slot) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 242 | { |
| 243 | return _modem_reset(slot, 1, 0); |
| 244 | } |
| 245 | |
| 246 | /*! \brief de-assert the RESET line of the modem */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 247 | int osmo_st2_modem_reset_inactive(struct osmo_st2_slot *slot) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 248 | { |
| 249 | return _modem_reset(slot, 0, 0); |
| 250 | } |
| 251 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 252 | static int _modem_sim_select(struct osmo_st2_slot *slot, uint8_t remote_sim) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 253 | { |
| 254 | struct msgb *msg = st_msgb_alloc(); |
| 255 | struct st_modem_sim_select *ss; |
| 256 | |
| 257 | ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss)); |
| 258 | ss->remote_sim = remote_sim; |
| 259 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 260 | return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /*! \brief select local (physical) SIM for given slot */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 264 | int osmo_st2_modem_sim_select_local(struct osmo_st2_slot *slot) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 265 | { |
| 266 | return _modem_sim_select(slot, 0); |
| 267 | } |
| 268 | |
| 269 | /*! \brief select remote (emulated/forwarded) SIM for given slot */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 270 | int osmo_st2_modem_sim_select_remote(struct osmo_st2_slot *slot) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 271 | { |
| 272 | return _modem_sim_select(slot, 1); |
| 273 | } |
| 274 | |
| 275 | /*! \brief Request slot to send us status information about the modem */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 276 | int osmo_st2_modem_get_status(struct osmo_st2_slot *slot) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 277 | { |
| 278 | struct msgb *msg = st_msgb_alloc(); |
| 279 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame^] | 280 | return osmo_st2_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 281 | } |