Kévin Redon | efbcf38 | 2018-07-07 17:35:15 +0200 | [diff] [blame] | 1 | /* simtrace2-remsim - main program for the host PC to provide a remote SIM |
| 2 | * using the SIMtrace 2 firmware in card emulation mode |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 3 | * |
Kévin Redon | efbcf38 | 2018-07-07 17:35:15 +0200 | [diff] [blame] | 4 | * (C) 2016 by Harald Welte <hwelte@hmw-consulting.de> |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 5 | * |
Kévin Redon | efbcf38 | 2018-07-07 17:35:15 +0200 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 10 | * |
Kévin Redon | efbcf38 | 2018-07-07 17:35:15 +0200 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 15 | * |
Kévin Redon | efbcf38 | 2018-07-07 17:35:15 +0200 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 19 | */ |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <unistd.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <stdint.h> |
Harald Welte | 1871c25 | 2016-03-20 15:30:46 +0100 | [diff] [blame] | 26 | #include <signal.h> |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 27 | #include <time.h> |
| 28 | #define _GNU_SOURCE |
| 29 | #include <getopt.h> |
| 30 | |
| 31 | #include <sys/time.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/socket.h> |
| 34 | #include <netinet/in.h> |
| 35 | #include <arpa/inet.h> |
| 36 | |
| 37 | #include <libusb.h> |
| 38 | |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 39 | #include "libusb_util.h" |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 40 | #include "simtrace.h" |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 41 | #include "simtrace_prot.h" |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 42 | #include "apdu_dispatch.h" |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 43 | #include "simtrace2-discovery.h" |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 44 | |
| 45 | #include <osmocom/core/gsmtap.h> |
| 46 | #include <osmocom/core/gsmtap_util.h> |
| 47 | #include <osmocom/core/utils.h> |
| 48 | #include <osmocom/core/socket.h> |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 49 | #include <osmocom/core/msgb.h> |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 50 | #include <osmocom/sim/class_tables.h> |
| 51 | #include <osmocom/sim/sim.h> |
| 52 | |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 53 | /* transport to a SIMtrace device */ |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 54 | struct st_transport { |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 55 | /* USB */ |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 56 | struct libusb_device_handle *usb_devh; |
| 57 | struct { |
| 58 | uint8_t in; |
| 59 | uint8_t out; |
| 60 | uint8_t irq_in; |
| 61 | } usb_ep; |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 62 | |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 63 | /* UDP */ |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 64 | int udp_fd; |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 67 | /* a SIMtrace slot; communicates over a transport */ |
| 68 | struct st_slot { |
| 69 | /* transport through which the slot can be reached */ |
| 70 | struct st_transport *transp; |
| 71 | /* number of the slot within the transport */ |
| 72 | uint8_t slot_nr; |
| 73 | }; |
| 74 | |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 75 | /* One istance of card emulation */ |
| 76 | struct cardem_inst { |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 77 | /* slot on which this card emulation instance runs */ |
| 78 | struct st_slot *slot; |
| 79 | /* libosmosim SIM card profile */ |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 80 | const struct osim_cla_ins_card_profile *card_prof; |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 81 | /* libosmosim SIM card channel */ |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 82 | struct osim_chan_hdl *chan; |
| 83 | }; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 84 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 85 | /* global GSMTAP instance */ |
| 86 | static struct gsmtap_inst *g_gti; |
| 87 | |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 88 | static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len) |
| 89 | { |
| 90 | struct gsmtap_hdr *gh; |
| 91 | unsigned int gross_len = len + sizeof(*gh); |
| 92 | uint8_t *buf = malloc(gross_len); |
| 93 | int rc; |
| 94 | |
| 95 | if (!buf) |
| 96 | return -ENOMEM; |
| 97 | |
| 98 | memset(buf, 0, sizeof(*gh)); |
| 99 | gh = (struct gsmtap_hdr *) buf; |
| 100 | gh->version = GSMTAP_VERSION; |
| 101 | gh->hdr_len = sizeof(*gh)/4; |
| 102 | gh->type = GSMTAP_TYPE_SIM; |
| 103 | |
| 104 | memcpy(buf + sizeof(*gh), apdu, len); |
| 105 | |
| 106 | rc = write(gsmtap_inst_fd(g_gti), buf, gross_len); |
| 107 | if (rc < 0) { |
| 108 | perror("write gsmtap"); |
| 109 | free(buf); |
| 110 | return rc; |
| 111 | } |
| 112 | |
| 113 | free(buf); |
| 114 | return 0; |
| 115 | } |
| 116 | |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 117 | /*********************************************************************** |
| 118 | * SIMTRACE pcore protocol |
| 119 | ***********************************************************************/ |
| 120 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 121 | /*! \brief allocate a message buffer for simtrace use */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 122 | static struct msgb *st_msgb_alloc(void) |
| 123 | { |
| 124 | return msgb_alloc_headroom(1024+32, 32, "SIMtrace"); |
| 125 | } |
| 126 | |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 127 | #if 0 |
| 128 | static void apdu_out_cb(uint8_t *buf, unsigned int len, void *user_data) |
| 129 | { |
| 130 | printf("APDU: %s\n", osmo_hexdump(buf, len)); |
| 131 | gsmtap_send_sim(buf, len); |
| 132 | } |
| 133 | #endif |
| 134 | |
| 135 | /*! \brief Transmit a given command to the SIMtrace2 device */ |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 136 | int st_transp_tx_msg(struct st_transport *transp, struct msgb *msg) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 137 | { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 138 | int rc; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 139 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 140 | printf("<- %s\n", msgb_hexdump(msg)); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 141 | |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 142 | if (transp->udp_fd < 0) { |
Harald Welte | 9f38ddd | 2017-05-10 22:49:02 +0200 | [diff] [blame] | 143 | int xfer_len; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 144 | |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 145 | rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out, |
| 146 | msgb_data(msg), msgb_length(msg), |
| 147 | &xfer_len, 100000); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 148 | } else { |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 149 | rc = write(transp->udp_fd, msgb_data(msg), msgb_length(msg)); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 150 | } |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 151 | |
| 152 | msgb_free(msg); |
| 153 | return rc; |
| 154 | } |
| 155 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 156 | static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class, uint8_t msg_type, |
| 157 | uint8_t slot_nr) |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 158 | { |
Harald Welte | 9f38ddd | 2017-05-10 22:49:02 +0200 | [diff] [blame] | 159 | struct simtrace_msg_hdr *sh; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 160 | |
Harald Welte | 9f38ddd | 2017-05-10 22:49:02 +0200 | [diff] [blame] | 161 | sh = (struct simtrace_msg_hdr *) msgb_push(msg, sizeof(*sh)); |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 162 | memset(sh, 0, sizeof(*sh)); |
| 163 | sh->msg_class = msg_class; |
| 164 | sh->msg_type = msg_type; |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 165 | sh->slot_nr = slot_nr; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 166 | sh->msg_len = msgb_length(msg); |
Harald Welte | 9f38ddd | 2017-05-10 22:49:02 +0200 | [diff] [blame] | 167 | |
| 168 | return sh; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 171 | /* transmit a given message to a specified slot. Expects all headers |
| 172 | * present before calling the function */ |
| 173 | int st_slot_tx_msg(struct st_slot *slot, struct msgb *msg, |
| 174 | uint8_t msg_class, uint8_t msg_type) |
| 175 | { |
Harald Welte | 9f38ddd | 2017-05-10 22:49:02 +0200 | [diff] [blame] | 176 | struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *) msg->data; |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 177 | |
| 178 | sh->slot_nr = slot->slot_nr; |
| 179 | |
| 180 | st_push_hdr(msg, msg_class, msg_type, slot->slot_nr); |
| 181 | |
| 182 | return st_transp_tx_msg(slot->transp, msg); |
| 183 | } |
| 184 | |
| 185 | |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 186 | /*********************************************************************** |
| 187 | * Card Emulation protocol |
| 188 | ***********************************************************************/ |
| 189 | |
| 190 | |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 191 | /*! \brief Request the SIMtrace2 to generate a card-insert signal */ |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 192 | static int cardem_request_card_insert(struct cardem_inst *ci, bool inserted) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 193 | { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 194 | struct msgb *msg = st_msgb_alloc(); |
| 195 | struct cardemu_usb_msg_cardinsert *cins; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 196 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 197 | cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins)); |
| 198 | memset(cins, 0, sizeof(*cins)); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 199 | if (inserted) |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 200 | cins->card_insert = 1; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 201 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 202 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_CARDINSERT); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Rx */ |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 206 | static int cardem_request_pb_and_rx(struct cardem_inst *ci, uint8_t pb, uint8_t le) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 207 | { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 208 | struct msgb *msg = st_msgb_alloc(); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 209 | struct cardemu_usb_msg_tx_data *txd; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 210 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 211 | |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 212 | printf("<= %s(%02x, %d)\n", __func__, pb, le); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 213 | |
| 214 | memset(txd, 0, sizeof(*txd)); |
| 215 | txd->data_len = 1; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 216 | txd->flags = CEMU_DATA_F_PB_AND_RX; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 217 | /* one data byte */ |
| 218 | msgb_put_u8(msg, pb); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 219 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 220 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /*! \brief Request the SIMtrace2 to transmit a Procedure Byte, then Tx */ |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 224 | static int cardem_request_pb_and_tx(struct cardem_inst *ci, uint8_t pb, |
| 225 | const uint8_t *data, uint8_t data_len_in) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 226 | { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 227 | struct msgb *msg = st_msgb_alloc(); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 228 | struct cardemu_usb_msg_tx_data *txd; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 229 | uint8_t *cur; |
| 230 | |
| 231 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 232 | |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 233 | printf("<= %s(%02x, %s, %d)\n", __func__, pb, |
| 234 | osmo_hexdump(data, data_len_in), data_len_in); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 235 | |
| 236 | memset(txd, 0, sizeof(*txd)); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 237 | txd->data_len = 1 + data_len_in; |
| 238 | txd->flags = CEMU_DATA_F_PB_AND_TX; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 239 | /* procedure byte */ |
| 240 | msgb_put_u8(msg, pb); |
| 241 | /* data */ |
| 242 | cur = msgb_put(msg, data_len_in); |
| 243 | memcpy(cur, data, data_len_in); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 244 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 245 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /*! \brief Request the SIMtrace2 to send a Status Word */ |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 249 | static int cardem_request_sw_tx(struct cardem_inst *ci, const uint8_t *sw) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 250 | { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 251 | struct msgb *msg = st_msgb_alloc(); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 252 | struct cardemu_usb_msg_tx_data *txd; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 253 | uint8_t *cur; |
| 254 | |
| 255 | txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd)); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 256 | |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 257 | printf("<= %s(%02x %02x)\n", __func__, sw[0], sw[1]); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 258 | |
| 259 | memset(txd, 0, sizeof(*txd)); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 260 | txd->data_len = 2; |
| 261 | txd->flags = CEMU_DATA_F_PB_AND_TX | CEMU_DATA_F_FINAL; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 262 | cur = msgb_put(msg, 2); |
| 263 | cur[0] = sw[0]; |
| 264 | cur[1] = sw[1]; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 265 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 266 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_TX_DATA); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Harald Welte | 9daaa79 | 2016-03-20 15:01:20 +0100 | [diff] [blame] | 269 | static void atr_update_csum(uint8_t *atr, unsigned int atr_len) |
| 270 | { |
| 271 | uint8_t csum = 0; |
| 272 | int i; |
| 273 | |
| 274 | for (i = 1; i < atr_len - 1; i++) |
| 275 | csum = csum ^ atr[i]; |
| 276 | |
| 277 | atr[atr_len-1] = csum; |
| 278 | } |
| 279 | |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 280 | static int cardem_request_set_atr(struct cardem_inst *ci, const uint8_t *atr, unsigned int atr_len) |
Harald Welte | 9daaa79 | 2016-03-20 15:01:20 +0100 | [diff] [blame] | 281 | { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 282 | struct msgb *msg = st_msgb_alloc(); |
Harald Welte | 9daaa79 | 2016-03-20 15:01:20 +0100 | [diff] [blame] | 283 | struct cardemu_usb_msg_set_atr *satr; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 284 | uint8_t *cur; |
| 285 | |
| 286 | satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr)); |
Harald Welte | 9daaa79 | 2016-03-20 15:01:20 +0100 | [diff] [blame] | 287 | |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 288 | printf("<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len)); |
Harald Welte | 9daaa79 | 2016-03-20 15:01:20 +0100 | [diff] [blame] | 289 | |
| 290 | memset(satr, 0, sizeof(*satr)); |
Harald Welte | 9daaa79 | 2016-03-20 15:01:20 +0100 | [diff] [blame] | 291 | satr->atr_len = atr_len; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 292 | cur = msgb_put(msg, atr_len); |
| 293 | memcpy(cur, atr, atr_len); |
Harald Welte | 9daaa79 | 2016-03-20 15:01:20 +0100 | [diff] [blame] | 294 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 295 | return st_slot_tx_msg(ci->slot, msg, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DT_CEMU_SET_ATR); |
Harald Welte | 9daaa79 | 2016-03-20 15:01:20 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 298 | /*********************************************************************** |
Harald Welte | 66de830 | 2017-05-11 01:12:50 +0200 | [diff] [blame] | 299 | * Modem Control protocol |
| 300 | ***********************************************************************/ |
| 301 | |
| 302 | static int _modem_reset(struct st_slot *slot, uint8_t asserted, uint16_t pulse_ms) |
| 303 | { |
| 304 | struct msgb *msg = st_msgb_alloc(); |
| 305 | struct st_modem_reset *sr ; |
| 306 | |
| 307 | sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr)); |
| 308 | sr->asserted = asserted; |
| 309 | sr->pulse_duration_msec = pulse_ms; |
| 310 | |
| 311 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_RESET); |
| 312 | } |
| 313 | |
| 314 | /*! \brief pulse the RESET line of the modem for \a duration_ms milli-seconds*/ |
| 315 | int st_modem_reset_pulse(struct st_slot *slot, uint16_t duration_ms) |
| 316 | { |
| 317 | return _modem_reset(slot, 2, duration_ms); |
| 318 | } |
| 319 | |
| 320 | /*! \brief assert the RESET line of the modem */ |
| 321 | int st_modem_reset_active(struct st_slot *slot) |
| 322 | { |
| 323 | return _modem_reset(slot, 1, 0); |
| 324 | } |
| 325 | |
| 326 | /*! \brief de-assert the RESET line of the modem */ |
| 327 | int st_modem_reset_inactive(struct st_slot *slot) |
| 328 | { |
| 329 | return _modem_reset(slot, 0, 0); |
| 330 | } |
| 331 | |
| 332 | static int _modem_sim_select(struct st_slot *slot, uint8_t remote_sim) |
| 333 | { |
| 334 | struct msgb *msg = st_msgb_alloc(); |
| 335 | struct st_modem_sim_select *ss; |
| 336 | |
| 337 | ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss)); |
| 338 | ss->remote_sim = remote_sim; |
| 339 | |
| 340 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_DT_MODEM_SIM_SELECT); |
| 341 | } |
| 342 | |
| 343 | /*! \brief select local (physical) SIM for given slot */ |
| 344 | int st_modem_sim_select_local(struct st_slot *slot) |
| 345 | { |
| 346 | return _modem_sim_select(slot, 0); |
| 347 | } |
| 348 | |
| 349 | /*! \brief select remote (emulated/forwarded) SIM for given slot */ |
| 350 | int st_modem_sim_select_remote(struct st_slot *slot) |
| 351 | { |
| 352 | return _modem_sim_select(slot, 1); |
| 353 | } |
| 354 | |
| 355 | /*! \brief Request slot to send us status information about the modem */ |
| 356 | int st_modem_get_status(struct st_slot *slot) |
| 357 | { |
| 358 | struct msgb *msg = st_msgb_alloc(); |
| 359 | |
| 360 | return st_slot_tx_msg(slot, msg, SIMTRACE_MSGC_MODEM, SIMTRACE_MSGT_BD_MODEM_STATUS); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | /*********************************************************************** |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 365 | * Incoming Messages |
| 366 | ***********************************************************************/ |
| 367 | |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 368 | /*! \brief Process a STATUS message from the SIMtrace2 */ |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 369 | static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 370 | { |
| 371 | struct cardemu_usb_msg_status *status; |
| 372 | status = (struct cardemu_usb_msg_status *) buf; |
| 373 | |
| 374 | printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n", |
| 375 | status->flags, status->fi, status->di, status->wi, |
| 376 | status->waiting_time); |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /*! \brief Process a PTS indication message from the SIMtrace2 */ |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 382 | static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 383 | { |
| 384 | struct cardemu_usb_msg_pts_info *pts; |
| 385 | pts = (struct cardemu_usb_msg_pts_info *) buf; |
| 386 | |
| 387 | printf("=> PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req))); |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | /*! \brief Process a ERROR indication message from the SIMtrace2 */ |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 393 | static int process_do_error(struct cardem_inst *ci, uint8_t *buf, int len) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 394 | { |
| 395 | struct cardemu_usb_msg_error *err; |
| 396 | err = (struct cardemu_usb_msg_error *) buf; |
| 397 | |
| 398 | printf("=> ERROR: %u/%u/%u: %s\n", |
| 399 | err->severity, err->subsystem, err->code, |
Harald Welte | b170ea9 | 2017-03-06 18:59:41 +0100 | [diff] [blame] | 400 | err->msg_len ? (char *)err->msg : ""); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | /*! \brief Process a RX-DATA indication message from the SIMtrace2 */ |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 406 | static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 407 | { |
| 408 | static struct apdu_context ac; |
| 409 | struct cardemu_usb_msg_rx_data *data; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 410 | int rc; |
| 411 | |
| 412 | data = (struct cardemu_usb_msg_rx_data *) buf; |
| 413 | |
| 414 | printf("=> DATA: flags=%x, %s: ", data->flags, |
| 415 | osmo_hexdump(data->data, data->data_len)); |
| 416 | |
| 417 | rc = apdu_segment_in(&ac, data->data, data->data_len, |
| 418 | data->flags & CEMU_DATA_F_TPDU_HDR); |
| 419 | |
| 420 | if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { |
| 421 | struct msgb *tmsg = msgb_alloc(1024, "TPDU"); |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 422 | struct osim_reader_hdl *rh = ci->chan->card->reader; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 423 | uint8_t *cur; |
| 424 | |
| 425 | /* Copy TPDU header */ |
| 426 | cur = msgb_put(tmsg, sizeof(ac.hdr)); |
| 427 | memcpy(cur, &ac.hdr, sizeof(ac.hdr)); |
| 428 | /* Copy D(c), if any */ |
| 429 | if (ac.lc.tot) { |
| 430 | cur = msgb_put(tmsg, ac.lc.tot); |
| 431 | memcpy(cur, ac.dc, ac.lc.tot); |
| 432 | } |
| 433 | /* send to actual card */ |
| 434 | tmsg->l3h = tmsg->tail; |
| 435 | rc = rh->ops->transceive(rh, tmsg); |
| 436 | if (rc < 0) { |
| 437 | fprintf(stderr, "error during transceive: %d\n", rc); |
| 438 | msgb_free(tmsg); |
| 439 | return rc; |
| 440 | } |
| 441 | msgb_apdu_sw(tmsg) = msgb_get_u16(tmsg); |
| 442 | ac.sw[0] = msgb_apdu_sw(tmsg) >> 8; |
| 443 | ac.sw[1] = msgb_apdu_sw(tmsg) & 0xff; |
| 444 | printf("SW=0x%04x, len_rx=%d\n", msgb_apdu_sw(tmsg), msgb_l3len(tmsg)); |
| 445 | if (msgb_l3len(tmsg)) |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 446 | cardem_request_pb_and_tx(ci, ac.hdr.ins, tmsg->l3h, msgb_l3len(tmsg)); |
| 447 | cardem_request_sw_tx(ci, ac.sw); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 448 | } else if (ac.lc.tot > ac.lc.cur) { |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 449 | cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 454 | #if 0 |
| 455 | case SIMTRACE_CMD_DO_ERROR |
| 456 | rc = process_do_error(ci, buf, len); |
| 457 | break; |
| 458 | #endif |
| 459 | |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 460 | /*! \brief Process an incoming message from the SIMtrace2 */ |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 461 | static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 462 | { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 463 | struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 464 | int rc; |
| 465 | |
| 466 | printf("-> %s\n", osmo_hexdump(buf, len)); |
| 467 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 468 | buf += sizeof(*sh); |
| 469 | |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 470 | switch (sh->msg_type) { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 471 | case SIMTRACE_MSGT_BD_CEMU_STATUS: |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 472 | rc = process_do_status(ci, buf, len); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 473 | break; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 474 | case SIMTRACE_MSGT_DO_CEMU_PTS: |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 475 | rc = process_do_pts(ci, buf, len); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 476 | break; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 477 | case SIMTRACE_MSGT_DO_CEMU_RX_DATA: |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 478 | rc = process_do_rx_da(ci, buf, len); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 479 | break; |
| 480 | default: |
| 481 | printf("unknown simtrace msg type 0x%02x\n", sh->msg_type); |
| 482 | rc = -1; |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | return rc; |
| 487 | } |
| 488 | |
| 489 | static void print_welcome(void) |
| 490 | { |
| 491 | printf("simtrace2-remsim - Remote SIM card forwarding\n" |
Harald Welte | b170ea9 | 2017-03-06 18:59:41 +0100 | [diff] [blame] | 492 | "(C) 2010-2017 by Harald Welte <laforge@gnumonks.org>\n\n"); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static void print_help(void) |
| 496 | { |
Harald Welte | 62bfd8a | 2017-03-06 20:56:14 +0100 | [diff] [blame] | 497 | printf( "\t-r\t--remote-udp-host HOST\n" |
| 498 | "\t-p\t--remote-udp-port PORT\n" |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 499 | "\t-h\t--help\n" |
Harald Welte | 62bfd8a | 2017-03-06 20:56:14 +0100 | [diff] [blame] | 500 | "\t-i\t--gsmtap-ip\tA.B.C.D\n" |
| 501 | "\t-a\t--skip-atr\n" |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 502 | "\t-k\t--keep-running\n" |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 503 | "\t-V\t--usb-vendor\tVENDOR_ID\n" |
| 504 | "\t-P\t--usb-product\tPRODUCT_ID\n" |
| 505 | "\t-C\t--usb-config\tCONFIG_ID\n" |
| 506 | "\t-I\t--usb-interface\tINTERFACE_ID\n" |
| 507 | "\t-S\t--usb-altsetting ALTSETTING_ID\n" |
| 508 | "\t-A\t--usb-address\tADDRESS\n" |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 509 | "\n" |
| 510 | ); |
| 511 | } |
| 512 | |
| 513 | static const struct option opts[] = { |
Harald Welte | 62bfd8a | 2017-03-06 20:56:14 +0100 | [diff] [blame] | 514 | { "remote-udp-host", 1, 0, 'r' }, |
| 515 | { "remote-udp-port", 1, 0, 'p' }, |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 516 | { "gsmtap-ip", 1, 0, 'i' }, |
| 517 | { "skip-atr", 0, 0, 'a' }, |
| 518 | { "help", 0, 0, 'h' }, |
| 519 | { "keep-running", 0, 0, 'k' }, |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 520 | { "usb-vendor", 1, 0, 'V' }, |
| 521 | { "usb-product", 1, 0, 'P' }, |
| 522 | { "usb-config", 1, 0, 'C' }, |
| 523 | { "usb-interface", 1, 0, 'I' }, |
| 524 | { "usb-altsetting", 1, 0, 'S' }, |
| 525 | { "usb-address", 1, 0, 'A' }, |
Harald Welte | a601635 | 2017-05-11 01:31:45 +0200 | [diff] [blame] | 526 | { "usb-path", 1, 0, 'H' }, |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 527 | { NULL, 0, 0, 0 } |
| 528 | }; |
| 529 | |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 530 | static void run_mainloop(struct cardem_inst *ci) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 531 | { |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 532 | struct st_transport *transp = ci->slot->transp; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 533 | unsigned int msg_count, byte_count = 0; |
Harald Welte | b170ea9 | 2017-03-06 18:59:41 +0100 | [diff] [blame] | 534 | uint8_t buf[16*265]; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 535 | int xfer_len; |
| 536 | int rc; |
| 537 | |
| 538 | printf("Entering main loop\n"); |
| 539 | |
| 540 | while (1) { |
| 541 | /* read data from SIMtrace2 device (local or via USB) */ |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 542 | if (transp->udp_fd < 0) { |
| 543 | rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in, |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 544 | buf, sizeof(buf), &xfer_len, 100000); |
Harald Welte | 37ad41e | 2017-05-11 01:13:58 +0200 | [diff] [blame] | 545 | if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT && |
| 546 | rc != LIBUSB_ERROR_INTERRUPTED && |
| 547 | rc != LIBUSB_ERROR_IO) { |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 548 | fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc); |
| 549 | return; |
| 550 | } |
| 551 | } else { |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 552 | rc = read(transp->udp_fd, buf, sizeof(buf)); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 553 | if (rc <= 0) { |
| 554 | fprintf(stderr, "shor read from UDP\n"); |
| 555 | return; |
| 556 | } |
| 557 | xfer_len = rc; |
| 558 | } |
| 559 | /* dispatch any incoming data */ |
| 560 | if (xfer_len > 0) { |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 561 | printf("URB: %s\n", osmo_hexdump(buf, rc)); |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 562 | process_usb_msg(ci, buf, xfer_len); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 563 | msg_count++; |
| 564 | byte_count += xfer_len; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 569 | static struct st_transport _transp; |
| 570 | |
| 571 | static struct st_slot _slot = { |
| 572 | .transp = &_transp, |
| 573 | .slot_nr = 0, |
| 574 | }; |
| 575 | |
| 576 | struct cardem_inst _ci = { |
| 577 | .slot = &_slot, |
| 578 | }; |
| 579 | |
| 580 | struct cardem_inst *ci = &_ci; |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 581 | |
Harald Welte | 1871c25 | 2016-03-20 15:30:46 +0100 | [diff] [blame] | 582 | static void signal_handler(int signal) |
| 583 | { |
| 584 | switch (signal) { |
| 585 | case SIGINT: |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 586 | cardem_request_card_insert(ci, false); |
Harald Welte | 1871c25 | 2016-03-20 15:30:46 +0100 | [diff] [blame] | 587 | exit(0); |
| 588 | break; |
| 589 | default: |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 594 | int main(int argc, char **argv) |
| 595 | { |
Harald Welte | d039605 | 2017-05-10 22:19:26 +0200 | [diff] [blame] | 596 | struct st_transport *transp = ci->slot->transp; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 597 | char *gsmtap_host = "127.0.0.1"; |
| 598 | int rc; |
| 599 | int c, ret = 1; |
| 600 | int skip_atr = 0; |
| 601 | int keep_running = 0; |
| 602 | int remote_udp_port = 52342; |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 603 | int if_num = 0, vendor_id = -1, product_id = -1; |
| 604 | int config_id = -1, altsetting = 0, addr = -1; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 605 | char *remote_udp_host = NULL; |
Harald Welte | a601635 | 2017-05-11 01:31:45 +0200 | [diff] [blame] | 606 | char *path = NULL; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 607 | struct osim_reader_hdl *reader; |
| 608 | struct osim_card_hdl *card; |
| 609 | |
| 610 | print_welcome(); |
| 611 | |
| 612 | while (1) { |
| 613 | int option_index = 0; |
| 614 | |
Harald Welte | a601635 | 2017-05-11 01:31:45 +0200 | [diff] [blame] | 615 | c = getopt_long(argc, argv, "r:p:hi:V:P:C:I:S:A:H:ak", opts, &option_index); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 616 | if (c == -1) |
| 617 | break; |
| 618 | switch (c) { |
| 619 | case 'r': |
| 620 | remote_udp_host = optarg; |
| 621 | break; |
| 622 | case 'p': |
| 623 | remote_udp_port = atoi(optarg); |
| 624 | break; |
| 625 | case 'h': |
| 626 | print_help(); |
| 627 | exit(0); |
| 628 | break; |
| 629 | case 'i': |
| 630 | gsmtap_host = optarg; |
| 631 | break; |
| 632 | case 'a': |
| 633 | skip_atr = 1; |
| 634 | break; |
| 635 | case 'k': |
| 636 | keep_running = 1; |
| 637 | break; |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 638 | case 'V': |
| 639 | vendor_id = strtol(optarg, NULL, 16); |
| 640 | break; |
| 641 | case 'P': |
| 642 | product_id = strtol(optarg, NULL, 16); |
| 643 | break; |
| 644 | case 'C': |
| 645 | config_id = atoi(optarg); |
| 646 | break; |
| 647 | case 'I': |
| 648 | if_num = atoi(optarg); |
| 649 | break; |
| 650 | case 'S': |
| 651 | altsetting = atoi(optarg); |
| 652 | break; |
| 653 | case 'A': |
| 654 | addr = atoi(optarg); |
| 655 | break; |
Harald Welte | a601635 | 2017-05-11 01:31:45 +0200 | [diff] [blame] | 656 | case 'H': |
| 657 | path = optarg; |
| 658 | break; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 662 | if (!remote_udp_host && (vendor_id < 0 || product_id < 0)) { |
| 663 | fprintf(stderr, "You have to specify the vendor and product ID\n"); |
| 664 | goto do_exit; |
| 665 | } |
| 666 | |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 667 | transp->udp_fd = -1; |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 668 | |
| 669 | ci->card_prof = &osim_uicc_sim_cic_profile; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 670 | |
| 671 | if (!remote_udp_host) { |
| 672 | rc = libusb_init(NULL); |
| 673 | if (rc < 0) { |
| 674 | fprintf(stderr, "libusb initialization failed\n"); |
| 675 | goto do_exit; |
| 676 | } |
| 677 | } else { |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 678 | transp->udp_fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 679 | remote_udp_host, remote_udp_port+if_num, |
| 680 | OSMO_SOCK_F_CONNECT); |
| 681 | if (transp->udp_fd < 0) { |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 682 | fprintf(stderr, "error binding UDP port\n"); |
| 683 | goto do_exit; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0); |
| 688 | if (!g_gti) { |
| 689 | perror("unable to open GSMTAP"); |
| 690 | goto close_exit; |
| 691 | } |
| 692 | gsmtap_source_add_sink(g_gti); |
| 693 | |
| 694 | reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL); |
| 695 | if (!reader) { |
| 696 | perror("unable to open PC/SC reader"); |
| 697 | goto close_exit; |
| 698 | } |
| 699 | |
| 700 | card = osim_card_open(reader, OSIM_PROTO_T0); |
| 701 | if (!card) { |
| 702 | perror("unable to open SIM card"); |
| 703 | goto close_exit; |
| 704 | } |
| 705 | |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 706 | ci->chan = llist_entry(card->channels.next, struct osim_chan_hdl, list); |
| 707 | if (!ci->chan) { |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 708 | perror("SIM card has no channel?!?"); |
| 709 | goto close_exit; |
| 710 | } |
| 711 | |
Harald Welte | 1871c25 | 2016-03-20 15:30:46 +0100 | [diff] [blame] | 712 | signal(SIGINT, &signal_handler); |
| 713 | |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 714 | do { |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 715 | if (transp->udp_fd < 0) { |
Harald Welte | 822d66e | 2017-03-06 20:58:03 +0100 | [diff] [blame] | 716 | struct usb_interface_match _ifm, *ifm = &_ifm; |
| 717 | ifm->vendor = vendor_id; |
| 718 | ifm->product = product_id; |
| 719 | ifm->configuration = config_id; |
| 720 | ifm->interface = if_num; |
| 721 | ifm->altsetting = altsetting; |
| 722 | ifm->addr = addr; |
Harald Welte | a601635 | 2017-05-11 01:31:45 +0200 | [diff] [blame] | 723 | if (path) |
| 724 | osmo_strlcpy(ifm->path, path, sizeof(ifm->path)); |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 725 | transp->usb_devh = usb_open_claim_interface(NULL, ifm); |
| 726 | if (!transp->usb_devh) { |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 727 | fprintf(stderr, "can't open USB device\n"); |
| 728 | goto close_exit; |
| 729 | } |
| 730 | |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 731 | rc = libusb_claim_interface(transp->usb_devh, if_num); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 732 | if (rc < 0) { |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 733 | fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc); |
| 734 | goto close_exit; |
| 735 | } |
| 736 | |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 737 | rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out, |
| 738 | &transp->usb_ep.in, &transp->usb_ep.irq_in); |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 739 | if (rc < 0) { |
| 740 | fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 741 | goto close_exit; |
| 742 | } |
| 743 | } |
| 744 | |
Harald Welte | 66de830 | 2017-05-11 01:12:50 +0200 | [diff] [blame] | 745 | /* simulate card-insert to modem (owhw, not qmod) */ |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 746 | cardem_request_card_insert(ci, true); |
Harald Welte | 66de830 | 2017-05-11 01:12:50 +0200 | [diff] [blame] | 747 | |
| 748 | /* select remote (forwarded) SIM */ |
| 749 | st_modem_sim_select_remote(ci->slot); |
| 750 | |
| 751 | /* set the ATR */ |
Harald Welte | 9daaa79 | 2016-03-20 15:01:20 +0100 | [diff] [blame] | 752 | uint8_t real_atr[] = { 0x3B, 0x9F, 0x96, 0x80, 0x1F, 0xC7, 0x80, 0x31, |
| 753 | 0xA0, 0x73, 0xBE, 0x21, 0x13, 0x67, 0x43, 0x20, |
| 754 | 0x07, 0x18, 0x00, 0x00, 0x01, 0xA5 }; |
| 755 | atr_update_csum(real_atr, sizeof(real_atr)); |
Harald Welte | 296c401 | 2017-05-09 15:02:52 +0200 | [diff] [blame] | 756 | cardem_request_set_atr(ci, real_atr, sizeof(real_atr)); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 757 | |
Harald Welte | 66de830 | 2017-05-11 01:12:50 +0200 | [diff] [blame] | 758 | /* select remote (forwarded) SIM */ |
| 759 | st_modem_reset_pulse(ci->slot, 300); |
| 760 | |
Harald Welte | 2ba03bb | 2017-03-06 18:51:39 +0100 | [diff] [blame] | 761 | run_mainloop(ci); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 762 | ret = 0; |
| 763 | |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 764 | if (transp->udp_fd < 0) |
| 765 | libusb_release_interface(transp->usb_devh, 0); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 766 | close_exit: |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 767 | if (transp->usb_devh) |
| 768 | libusb_close(transp->usb_devh); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 769 | if (keep_running) |
| 770 | sleep(1); |
| 771 | } while (keep_running); |
| 772 | |
Harald Welte | 08b77ba | 2017-05-09 15:11:53 +0200 | [diff] [blame] | 773 | if (transp->udp_fd < 0) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 774 | libusb_exit(NULL); |
| 775 | do_exit: |
| 776 | return ret; |
| 777 | } |