blob: dc4708f5e4b37de2f1c72fe47465d5a8972f328b [file] [log] [blame]
Kévin Redon6e3f1122018-07-01 18:24:42 +02001/* simtrace2-sniff - main program for the host PC to communicate with the SIMtrace 2 firmware in sniffer mode */
2/* This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * (C) 2010-2017 by Harald Welte <hwelte@hmw-consulting.de>
17 * (C) 2018 by Kevin Redon <kredon@sysmocom.de>
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>
25#include <signal.h>
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
38#include "libusb_util.h"
39#include "simtrace.h"
40#include "simtrace_usb.h"
41#include "simtrace_prot.h"
42#include "simtrace2-discovery.h"
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>
48#include <osmocom/core/msgb.h>
49#include <osmocom/sim/class_tables.h>
50#include <osmocom/sim/sim.h>
51
52/* transport to a SIMtrace device */
53struct st_transport {
54 /* USB */
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;
61};
62
63/* global GSMTAP instance */
64static struct gsmtap_inst *g_gti;
65
66static int gsmtap_send_sim(const uint8_t *apdu, unsigned int len)
67{
68 struct gsmtap_hdr *gh;
69 unsigned int gross_len = len + sizeof(*gh);
70 uint8_t *buf = malloc(gross_len);
71 int rc;
72
73 if (!buf)
74 return -ENOMEM;
75
76 memset(buf, 0, sizeof(*gh));
77 gh = (struct gsmtap_hdr *) buf;
78 gh->version = GSMTAP_VERSION;
79 gh->hdr_len = sizeof(*gh)/4;
80 gh->type = GSMTAP_TYPE_SIM;
81
82 memcpy(buf + sizeof(*gh), apdu, len);
83
84 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
85 if (rc < 0) {
86 perror("write gsmtap");
87 free(buf);
88 return rc;
89 }
90
91 free(buf);
92 return 0;
93}
94
95static int process_change(uint8_t *buf, int len)
96{
97 /* check if there is enough data for the structure */
98 if (len<sizeof(struct sniff_change)) {
99 return -1;
100 }
101 struct sniff_change *change = (struct sniff_change *)buf;
102
103 if (SNIFF_CHANGE_FLAG_TIMEOUT_WT!=change->flags) {
104 printf("Card state change: ");
105 }
106 if (change->flags&SNIFF_CHANGE_FLAG_CARD_INSERT) {
107 printf("card inserted ");
108 }
109 if (change->flags&SNIFF_CHANGE_FLAG_CARD_EJECT) {
110 printf("card ejected ");
111 }
112 if (change->flags&SNIFF_CHANGE_FLAG_RESET_HOLD) {
113 printf("reset hold ");
114 }
115 if (change->flags&SNIFF_CHANGE_FLAG_RESET_RELEASE) {
116 printf("reset release ");
117 }
118 if (change->flags&SNIFF_CHANGE_FLAG_TIMEOUT_WT) {
119 // do nothing since this also triggers on inactivity
120 }
121 if (SNIFF_CHANGE_FLAG_TIMEOUT_WT!=change->flags) {
122 printf("\n");
123 }
124
125 return 0;
126}
127
128/* Table 7 of ISO 7816-3:2006 */
129static const uint16_t fi_table[] = { 372, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0, };
130
131/* Table 8 from ISO 7816-3:2006 */
132static const uint8_t di_table[] = { 0, 1, 2, 4, 8, 16, 32, 64, 12, 20, 2, 4, 8, 16, 32, 64, };
133
134static int process_fidi(uint8_t *buf, int len)
135{
136 /* check if there is enough data for the structure */
137 if (len<sizeof(struct sniff_fidi)) {
138 return -1;
139 }
140 struct sniff_fidi *fidi = (struct sniff_fidi *)buf;
141
142 printf("Fi/Di switched to %u/%u\n", fi_table[fidi->fidi>>4], di_table[fidi->fidi&0x0f]);
143 return 0;
144}
145
146static int process_atr(uint8_t *buf, int len)
147{
148 /* check if there is enough data for the structure */
149 if (len<sizeof(struct sniff_data)) {
150 return -1;
151 }
152 struct sniff_data *atr = (struct sniff_data *)buf;
153
154 /* check if the data is available */
155 if (len<sizeof(struct sniff_data)+atr->length) {
156 return -2;
157 }
158
159 printf("ATR%s: ", atr->complete ? "" : " (incomplete)");
160 uint16_t i;
161 for (i = 0; i < atr->length; i++) {
162 printf("%02x ", atr->data[i]);
163 }
164 printf("\n");
165 return 0;
166}
167
168static int process_pps(uint8_t *buf, int len)
169{
170 /* check if there is enough data for the structure */
171 if (len<sizeof(struct sniff_data)) {
172 return -1;
173 }
174 struct sniff_data *pps = (struct sniff_data *)buf;
175
176 /* check if the data is available */
177 if (len<sizeof(struct sniff_data)+pps->length) {
178 return -2;
179 }
180
181 printf("PPS%s: ", pps->complete ? "" : " (incomplete) ");
182 uint16_t i;
183 for (i = 0; i < pps->length; i++) {
184 printf("%02x ", pps->data[i]);
185 }
186 printf("\n");
187 return 0;
188}
189
190static int process_tpdu(uint8_t *buf, int len)
191{
192 /* check if there is enough data for the structure */
193 if (len<sizeof(struct sniff_data)) {
194 return -1;
195 }
196 struct sniff_data *tpdu = (struct sniff_data *)buf;
197
198 /* check if the data is available */
199 if (len<sizeof(struct sniff_data)+tpdu->length) {
200 return -2;
201 }
202
203 printf("TPDU%s: ", tpdu->complete ? "" : " (incomplete)");
204 uint16_t i;
205 for (i = 0; i < tpdu->length; i++) {
206 printf("%02x ", tpdu->data[i]);
207 }
208 printf("\n");
209 return 0;
210}
211
212/*! \brief Process an incoming message from the SIMtrace2 */
213static int process_usb_msg(uint8_t *buf, int len)
214{
215 /* check if enough data for the header is present */
216 if (len<sizeof(struct simtrace_msg_hdr)) {
217 return 0;
218 }
219
220 /* check if message is complete */
221 struct simtrace_msg_hdr *msg_hdr = (struct simtrace_msg_hdr *)buf;
222 if (len<msg_hdr->msg_len) {
223 return 0;
224 }
225 //printf("msg: %s\n", osmo_hexdump(buf, msg_hdr->msg_len));
226
227 /* check for message class */
228 if (SIMTRACE_MSGC_SNIFF!=msg_hdr->msg_class) { /* we only care about sniffing messages */
229 return msg_hdr->msg_len; /* discard non-sniffing messaged */
230 }
231
232 /* process sniff message payload */
233 buf += sizeof(struct simtrace_msg_hdr);
234 len -= sizeof(struct simtrace_msg_hdr);
235 switch (msg_hdr->msg_type) {
236 case SIMTRACE_MSGT_SNIFF_CHANGE:
237 process_change(buf, len);
238 break;
239 case SIMTRACE_MSGT_SNIFF_FIDI:
240 process_fidi(buf, len);
241 break;
242 case SIMTRACE_MSGT_SNIFF_ATR:
243 process_atr(buf, len);
244 break;
245 case SIMTRACE_MSGT_SNIFF_PPS:
246 process_pps(buf, len);
247 break;
248 case SIMTRACE_MSGT_SNIFF_TPDU:
249 process_tpdu(buf, len);
250 break;
251 default:
252 printf("unknown SIMtrace msg type 0x%02x\n", msg_hdr->msg_type);
253 break;
254 }
255
256 return msg_hdr->msg_len;
257}
258
259/*! Transport to SIMtrace device (e.g. USB handle) */
260static struct st_transport _transp;
261
262static void run_mainloop()
263{
264 int rc;
265 uint8_t buf[16*256];
266 unsigned int i, buf_i = 0;
267 int xfer_len;
268
269 printf("Entering main loop\n");
270
271 while (true) {
272 /* read data from SIMtrace2 device (via USB) */
273 rc = libusb_bulk_transfer(_transp.usb_devh, _transp.usb_ep.in,
274 &buf[buf_i], sizeof(buf)-buf_i, &xfer_len, 100000);
275 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
276 rc != LIBUSB_ERROR_INTERRUPTED &&
277 rc != LIBUSB_ERROR_IO) {
278 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
279 return;
280 }
281 /* dispatch any incoming data */
282 if (xfer_len > 0) {
283 //printf("URB: %s\n", osmo_hexdump(&buf[buf_i], xfer_len));
284 buf_i += xfer_len;
285 if (buf_i>=sizeof(buf)) {
286 perror("preventing USB buffer overflow");
287 return;
288 }
289 int processed = process_usb_msg(buf, buf_i);
290 if (processed > 0 && processed <= buf_i) {
291 for (i = processed; i < buf_i; i++) {
292 buf[i-processed] = buf[i];
293 }
294 buf_i -= processed;
295 }
296 }
297 }
298}
299
300static void print_welcome(void)
301{
302 printf("simtrace2-sniff - Phone-SIM card communication sniffer \n"
303 "(C) 2010-2017 by Harald Welte <laforge@gnumonks.org>\n"
304 "(C) 2018 by Kevin Redon <kredon@sysmocom.de>\n"
305 "\n"
306 );
307}
308
309static void print_help(void)
310{
311 printf(
312 "\t-h\t--help\n"
313 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
314 "\t-k\t--keep-running\n"
315 "\t-V\t--usb-vendor\tVENDOR_ID\n"
316 "\t-P\t--usb-product\tPRODUCT_ID\n"
317 "\t-C\t--usb-config\tCONFIG_ID\n"
318 "\t-I\t--usb-interface\tINTERFACE_ID\n"
319 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
320 "\t-A\t--usb-address\tADDRESS\n"
321 "\n"
322 );
323}
324
325static const struct option opts[] = {
326 { "help", 0, 0, 'h' },
327 { "gsmtap-ip", 1, 0, 'i' },
328 { "keep-running", 0, 0, 'k' },
329 { "usb-vendor", 1, 0, 'V' },
330 { "usb-product", 1, 0, 'P' },
331 { "usb-config", 1, 0, 'C' },
332 { "usb-interface", 1, 0, 'I' },
333 { "usb-altsetting", 1, 0, 'S' },
334 { "usb-address", 1, 0, 'A' },
335 { NULL, 0, 0, 0 }
336};
337
338/* Known USB device with SIMtrace firmware supporting sniffer */
339static const struct dev_id compatible_dev_ids[] = {
340 { USB_VENDOR_OPENMOKO, USB_PRODUCT_SIMTRACE2 },
341 { 0, 0 }
342};
343
344static void signal_handler(int signal)
345{
346 switch (signal) {
347 case SIGINT:
348 exit(0);
349 break;
350 default:
351 break;
352 }
353}
354
355int main(int argc, char **argv)
356{
357 int i, rc, ret;
358 print_welcome();
359
360 /* Parse arguments */
361 char *gsmtap_host = "127.0.0.1";
362 int keep_running = 0;
363 int vendor_id = -1, product_id = -1, addr = -1, config_id = -1, if_num = -1, altsetting = -1;
364
365 while (1) {
366 int option_index = 0;
367
368 char c = getopt_long(argc, argv, "hi:kV:P:C:I:S:A:", opts, &option_index);
369 if (c == -1)
370 break;
371 switch (c) {
372 case 'h':
373 print_help();
374 exit(0);
375 break;
376 case 'i':
377 gsmtap_host = optarg;
378 break;
379 case 'k':
380 keep_running = 1;
381 break;
382 case 'V':
383 vendor_id = strtol(optarg, NULL, 16);
384 break;
385 case 'P':
386 product_id = strtol(optarg, NULL, 16);
387 break;
388 case 'C':
389 config_id = atoi(optarg);
390 break;
391 case 'I':
392 if_num = atoi(optarg);
393 break;
394 case 'S':
395 altsetting = atoi(optarg);
396 break;
397 case 'A':
398 addr = atoi(optarg);
399 break;
400 }
401 }
402
403 /* Scan for available SIMtrace USB devices supporting sniffing */
404 rc = libusb_init(NULL);
405 if (rc < 0) {
406 fprintf(stderr, "libusb initialization failed\n");
407 goto do_exit;
408 }
409 struct usb_interface_match ifm_scan[16];
410 int num_interfaces = usb_match_interfaces(NULL, compatible_dev_ids,
411 USB_CLASS_PROPRIETARY, SIMTRACE_SNIFFER_USB_SUBCLASS, -1, ifm_scan, ARRAY_SIZE(ifm_scan));
412 if (num_interfaces <= 0) {
413 perror("No compatible USB devices found");
414 goto do_exit;
415 }
416
417 /* Only keep USB matching arguments */
418 struct usb_interface_match ifm_filtered[ARRAY_SIZE(ifm_scan)];
419 int num_filtered = 0;
420 for (i = 0; i < num_interfaces; i++) {
421 if (vendor_id>=0 && vendor_id!=ifm_scan[i].vendor) {
422 continue;
423 }
424 if (product_id>=0 && product_id!=ifm_scan[i].product) {
425 continue;
426 }
427 if (config_id>=0 && config_id!=ifm_scan[i].configuration) {
428 continue;
429 }
430 if (if_num>=0 && if_num!=ifm_scan[i].interface) {
431 continue;
432 }
433 if (altsetting>=0 && altsetting!=ifm_scan[i].altsetting) {
434 continue;
435 }
436 if (addr>=0 && addr!=ifm_scan[i].addr) {
437 continue;
438 }
439 ifm_filtered[num_filtered++] = ifm_scan[i];
440 }
441 if (1!=num_filtered) {
442 perror("No individual matching USB devices found");
443 printf("Available USB devices:\n");
444 for (i = 0; i < num_interfaces; i++) {
445 printf("\t%04x:%04x Addr=%u, Path=%s, Cfg=%u, Intf=%u, Alt=%u: %d/%d/%d ",
446 ifm_scan[i].vendor, ifm_scan[i].product, ifm_scan[i].addr, ifm_scan[i].path,
447 ifm_scan[i].configuration, ifm_scan[i].interface, ifm_scan[i].altsetting,
448 ifm_scan[i].class, ifm_scan[i].sub_class, ifm_scan[i].protocol);
449 libusb_device_handle *dev_handle;
450 rc = libusb_open(ifm_scan[i].usb_dev, &dev_handle);
451 if (rc < 0) {
452 printf("\n");
453 perror("Cannot open device");
454 continue;
455 }
456 char strbuf[256];
457 rc = libusb_get_string_descriptor_ascii(dev_handle, ifm_scan[i].string_idx,
458 (unsigned char *)strbuf, sizeof(strbuf));
459 libusb_close(dev_handle);
460 if (rc < 0) {
461 printf("\n");
462 perror("Cannot read string");
463 continue;
464 }
465 printf("(%s)\n", strbuf);
466 }
467 goto do_exit;
468 }
469 struct usb_interface_match ifm_selected = ifm_filtered[0];
470 printf("Using USB device %04x:%04x Addr=%u, Path=%s, Cfg=%u, Intf=%u, Alt=%u: %d/%d/%d ",
471 ifm_selected.vendor, ifm_selected.product, ifm_selected.addr, ifm_selected.path,
472 ifm_selected.configuration, ifm_selected.interface, ifm_selected.altsetting,
473 ifm_selected.class, ifm_selected.sub_class, ifm_selected.protocol);
474 libusb_device_handle *dev_handle;
475 rc = libusb_open(ifm_selected.usb_dev, &dev_handle);
476 if (rc < 0) {
477 printf("\n");
478 perror("Cannot open device");
479 }
480 char strbuf[256];
481 rc = libusb_get_string_descriptor_ascii(dev_handle, ifm_selected.string_idx,
482 (unsigned char *)strbuf, sizeof(strbuf));
483 libusb_close(dev_handle);
484 if (rc < 0) {
485 printf("\n");
486 perror("Cannot read string");
487 }
488 printf("(%s)\n", strbuf);
489
490 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
491 if (!g_gti) {
492 perror("unable to open GSMTAP");
493 goto close_exit;
494 }
495 gsmtap_source_add_sink(g_gti);
496
497 signal(SIGINT, &signal_handler);
498
499 do {
500 _transp.usb_devh = usb_open_claim_interface(NULL, &ifm_selected);
501 if (!_transp.usb_devh) {
502 fprintf(stderr, "can't open USB device\n");
503 goto close_exit;
504 }
505
506 rc = libusb_claim_interface(_transp.usb_devh, ifm_selected.interface);
507 if (rc < 0) {
508 fprintf(stderr, "can't claim interface %d; rc=%d\n", ifm_selected.interface, rc);
509 goto close_exit;
510 }
511
512 rc = get_usb_ep_addrs(_transp.usb_devh, ifm_selected.interface, &_transp.usb_ep.out,
513 &_transp.usb_ep.in, &_transp.usb_ep.irq_in);
514 if (rc < 0) {
515 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
516 goto close_exit;
517 }
518
519 run_mainloop();
520 ret = 0;
521
522 if (_transp.usb_devh)
523 libusb_release_interface(_transp.usb_devh, 0);
524close_exit:
525 if (_transp.usb_devh)
526 libusb_close(_transp.usb_devh);
527 if (keep_running)
528 sleep(1);
529 } while (keep_running);
530
531 libusb_exit(NULL);
532do_exit:
533 return ret;
534}