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