blob: 368ae604a71637d7d4d4f589142e027de05f6bc6 [file] [log] [blame]
Kévin Redonefbcf382018-07-07 17:35:15 +02001/* simtrace2-sniff - main program for the host PC to communicate with the
2 * SIMtrace 2 firmware in sniffer mode
3 *
4 * (C) 2016 by Harald Welte <hwelte@hmw-consulting.de>
5 * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
6 *
7 * This program is free software; you can redistribute it and/or
Kévin Redon6e3f1122018-07-01 18:24:42 +02008 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Kévin Redon6e3f1122018-07-01 18:24:42 +020020 */
21#include <errno.h>
22#include <unistd.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdint.h>
27#include <signal.h>
28#include <time.h>
29#define _GNU_SOURCE
30#include <getopt.h>
31
32#include <sys/time.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37
38#include <libusb.h>
39
40#include "libusb_util.h"
41#include "simtrace.h"
42#include "simtrace_usb.h"
43#include "simtrace_prot.h"
44#include "simtrace2-discovery.h"
45
46#include <osmocom/core/gsmtap.h>
47#include <osmocom/core/gsmtap_util.h>
48#include <osmocom/core/utils.h>
49#include <osmocom/core/socket.h>
50#include <osmocom/core/msgb.h>
51#include <osmocom/sim/class_tables.h>
52#include <osmocom/sim/sim.h>
53
54/* transport to a SIMtrace device */
55struct st_transport {
56 /* USB */
57 struct libusb_device_handle *usb_devh;
58 struct {
59 uint8_t in;
60 uint8_t out;
61 uint8_t irq_in;
62 } usb_ep;
63};
64
65/* global GSMTAP instance */
66static struct gsmtap_inst *g_gti;
67
Kévin Redond1c65362018-07-26 14:51:15 +020068static int gsmtap_send_sim(uint8_t sub_type, const uint8_t *data, unsigned int len)
Kévin Redon6e3f1122018-07-01 18:24:42 +020069{
70 struct gsmtap_hdr *gh;
71 unsigned int gross_len = len + sizeof(*gh);
72 uint8_t *buf = malloc(gross_len);
73 int rc;
74
75 if (!buf)
76 return -ENOMEM;
77
78 memset(buf, 0, sizeof(*gh));
79 gh = (struct gsmtap_hdr *) buf;
80 gh->version = GSMTAP_VERSION;
81 gh->hdr_len = sizeof(*gh)/4;
82 gh->type = GSMTAP_TYPE_SIM;
Kévin Redond1c65362018-07-26 14:51:15 +020083 gh->sub_type = sub_type;
Kévin Redon6e3f1122018-07-01 18:24:42 +020084
Kévin Redond1c65362018-07-26 14:51:15 +020085 memcpy(buf + sizeof(*gh), data, len);
Kévin Redon6e3f1122018-07-01 18:24:42 +020086
87 rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
88 if (rc < 0) {
89 perror("write gsmtap");
90 free(buf);
91 return rc;
92 }
93
94 free(buf);
95 return 0;
96}
97
Kévin Redon31ed8022018-07-10 16:04:00 +020098const struct value_string change_flags[] = {
99 {
100 .value = SNIFF_CHANGE_FLAG_CARD_INSERT,
101 .str = "card inserted",
102 },
103 {
104 .value = SNIFF_CHANGE_FLAG_CARD_EJECT,
105 .str = "card ejected",
106 },
107 {
Kévin Redon8e84f812018-07-26 15:34:03 +0200108 .value = SNIFF_CHANGE_FLAG_RESET_ASSERT,
109 .str = "reset asserted",
Kévin Redon31ed8022018-07-10 16:04:00 +0200110 },
111 {
Kévin Redon8e84f812018-07-26 15:34:03 +0200112 .value = SNIFF_CHANGE_FLAG_RESET_DEASSERT,
113 .str = "reset de-asserted",
Kévin Redon31ed8022018-07-10 16:04:00 +0200114 },
115 {
116 .value = SNIFF_CHANGE_FLAG_TIMEOUT_WT,
117 .str = "data transfer timeout",
118 },
119 {
120 .value = 0,
121 .str = NULL,
122 },
123};
124
125const struct value_string data_flags[] = {
126 {
127 .value = SNIFF_DATA_FLAG_ERROR_INCOMPLETE,
128 .str = "incomplete",
129 },
130 {
131 .value = SNIFF_DATA_FLAG_ERROR_MALFORMED,
132 .str = "malformed",
133 },
134 {
Kévin Redonf66af0c2018-07-11 10:27:13 +0200135 .value = SNIFF_DATA_FLAG_ERROR_CHECKSUM,
136 .str = "checksum error",
137 },
Kévin Redon69719962018-07-28 17:11:21 +0200138 {
139 .value = 0,
140 .str = NULL,
141 },
Kévin Redon31ed8022018-07-10 16:04:00 +0200142};
143
144static void print_flags(const struct value_string* flag_meanings, uint32_t nb_flags, uint32_t flags) {
145 uint32_t i;
146 for (i = 0; i < nb_flags; i++) {
147 if (flags & flag_meanings[i].value) {
148 printf(flag_meanings[i].str);
149 flags &= ~flag_meanings[i].value;
150 if (flags) {
151 printf(", ");
152 }
153 }
154 }
155}
156
Kévin Redon709a4312018-07-03 16:10:04 +0200157static int process_change(const uint8_t *buf, int len)
Kévin Redon6e3f1122018-07-01 18:24:42 +0200158{
159 /* check if there is enough data for the structure */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200160 if (len < sizeof(struct sniff_change)) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200161 return -1;
162 }
163 struct sniff_change *change = (struct sniff_change *)buf;
164
Kévin Redon709a4312018-07-03 16:10:04 +0200165 printf("Card state change: ");
Kévin Redon31ed8022018-07-10 16:04:00 +0200166 if (change->flags) {
167 print_flags(change_flags, ARRAY_SIZE(change_flags), change->flags);
168 printf("\n");
169 } else {
170 printf("no changes\n");
Kévin Redon6e3f1122018-07-01 18:24:42 +0200171 }
Kévin Redon6e3f1122018-07-01 18:24:42 +0200172
173 return 0;
174}
175
176/* Table 7 of ISO 7816-3:2006 */
177static const uint16_t fi_table[] = { 372, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0, };
178
179/* Table 8 from ISO 7816-3:2006 */
180static const uint8_t di_table[] = { 0, 1, 2, 4, 8, 16, 32, 64, 12, 20, 2, 4, 8, 16, 32, 64, };
181
Kévin Redon709a4312018-07-03 16:10:04 +0200182static int process_fidi(const uint8_t *buf, int len)
Kévin Redon6e3f1122018-07-01 18:24:42 +0200183{
184 /* check if there is enough data for the structure */
185 if (len<sizeof(struct sniff_fidi)) {
186 return -1;
187 }
188 struct sniff_fidi *fidi = (struct sniff_fidi *)buf;
189
190 printf("Fi/Di switched to %u/%u\n", fi_table[fidi->fidi>>4], di_table[fidi->fidi&0x0f]);
191 return 0;
192}
193
Kévin Redonf82f0f62018-07-08 15:10:23 +0200194static int process_data(enum simtrace_msg_type_sniff type, const uint8_t *buf, int len)
Kévin Redon6e3f1122018-07-01 18:24:42 +0200195{
196 /* check if there is enough data for the structure */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200197 if (len < sizeof(struct sniff_data)) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200198 return -1;
199 }
Kévin Redonf82f0f62018-07-08 15:10:23 +0200200 struct sniff_data *data = (struct sniff_data *)buf;
Kévin Redon6e3f1122018-07-01 18:24:42 +0200201
202 /* check if the data is available */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200203 if (len < sizeof(struct sniff_data) + data->length) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200204 return -2;
205 }
206
Kévin Redonf82f0f62018-07-08 15:10:23 +0200207 /* check type */
208 if (type != SIMTRACE_MSGT_SNIFF_ATR && type != SIMTRACE_MSGT_SNIFF_PPS && type != SIMTRACE_MSGT_SNIFF_TPDU) {
209 return -3;
210 }
211
212 /* Print message */
213 switch (type) {
214 case SIMTRACE_MSGT_SNIFF_ATR:
215 printf("ATR");
216 break;
217 case SIMTRACE_MSGT_SNIFF_PPS:
218 printf("PPS");
219 break;
220 case SIMTRACE_MSGT_SNIFF_TPDU:
221 printf("TPDU");
222 break;
223 default:
224 printf("???");
225 break;
226 }
227 if (data->flags) {
228 printf(" (");
Kévin Redon31ed8022018-07-10 16:04:00 +0200229 print_flags(data_flags, ARRAY_SIZE(data_flags), data->flags);
Kévin Redonf82f0f62018-07-08 15:10:23 +0200230 printf(")");
231 }
232 printf(": ");
Kévin Redon6e3f1122018-07-01 18:24:42 +0200233 uint16_t i;
Kévin Redonf82f0f62018-07-08 15:10:23 +0200234 for (i = 0; i < data->length; i++) {
235 printf("%02x ", data->data[i]);
Kévin Redon6e3f1122018-07-01 18:24:42 +0200236 }
237 printf("\n");
Kévin Redon709a4312018-07-03 16:10:04 +0200238
Kévin Redond1c65362018-07-26 14:51:15 +0200239 /* Send message as GSNTAP */
240 switch (type) {
241 case SIMTRACE_MSGT_SNIFF_ATR:
242 gsmtap_send_sim(GSMTAP_SIM_ATR, data->data, data->length);
243 break;
244 case SIMTRACE_MSGT_SNIFF_TPDU:
245 /* TPDU is now considered as APDU since SIMtrace sends complete TPDU */
246 gsmtap_send_sim(GSMTAP_SIM_APDU, data->data, data->length);
247 break;
248 default:
249 break;
Kévin Redonf82f0f62018-07-08 15:10:23 +0200250 }
251
Kévin Redon6e3f1122018-07-01 18:24:42 +0200252 return 0;
253}
254
255/*! \brief Process an incoming message from the SIMtrace2 */
Kévin Redon709a4312018-07-03 16:10:04 +0200256static int process_usb_msg(const uint8_t *buf, int len)
Kévin Redon6e3f1122018-07-01 18:24:42 +0200257{
258 /* check if enough data for the header is present */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200259 if (len < sizeof(struct simtrace_msg_hdr)) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200260 return 0;
261 }
262
263 /* check if message is complete */
264 struct simtrace_msg_hdr *msg_hdr = (struct simtrace_msg_hdr *)buf;
Kévin Redonf82f0f62018-07-08 15:10:23 +0200265 if (len < msg_hdr->msg_len) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200266 return 0;
267 }
268 //printf("msg: %s\n", osmo_hexdump(buf, msg_hdr->msg_len));
269
270 /* check for message class */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200271 if (SIMTRACE_MSGC_SNIFF != msg_hdr->msg_class) { /* we only care about sniffing messages */
Kévin Redon6e3f1122018-07-01 18:24:42 +0200272 return msg_hdr->msg_len; /* discard non-sniffing messaged */
273 }
274
275 /* process sniff message payload */
276 buf += sizeof(struct simtrace_msg_hdr);
277 len -= sizeof(struct simtrace_msg_hdr);
278 switch (msg_hdr->msg_type) {
279 case SIMTRACE_MSGT_SNIFF_CHANGE:
280 process_change(buf, len);
281 break;
282 case SIMTRACE_MSGT_SNIFF_FIDI:
283 process_fidi(buf, len);
284 break;
285 case SIMTRACE_MSGT_SNIFF_ATR:
Kévin Redon6e3f1122018-07-01 18:24:42 +0200286 case SIMTRACE_MSGT_SNIFF_PPS:
Kévin Redon6e3f1122018-07-01 18:24:42 +0200287 case SIMTRACE_MSGT_SNIFF_TPDU:
Kévin Redonf82f0f62018-07-08 15:10:23 +0200288 process_data(msg_hdr->msg_type, buf, len);
Kévin Redon6e3f1122018-07-01 18:24:42 +0200289 break;
290 default:
291 printf("unknown SIMtrace msg type 0x%02x\n", msg_hdr->msg_type);
292 break;
293 }
294
295 return msg_hdr->msg_len;
296}
297
298/*! Transport to SIMtrace device (e.g. USB handle) */
299static struct st_transport _transp;
300
301static void run_mainloop()
302{
303 int rc;
304 uint8_t buf[16*256];
305 unsigned int i, buf_i = 0;
306 int xfer_len;
307
308 printf("Entering main loop\n");
309
310 while (true) {
311 /* read data from SIMtrace2 device (via USB) */
312 rc = libusb_bulk_transfer(_transp.usb_devh, _transp.usb_ep.in,
313 &buf[buf_i], sizeof(buf)-buf_i, &xfer_len, 100000);
314 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
315 rc != LIBUSB_ERROR_INTERRUPTED &&
316 rc != LIBUSB_ERROR_IO) {
317 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
318 return;
319 }
320 /* dispatch any incoming data */
321 if (xfer_len > 0) {
322 //printf("URB: %s\n", osmo_hexdump(&buf[buf_i], xfer_len));
323 buf_i += xfer_len;
Kévin Redon3b7624c2018-07-10 16:03:27 +0200324 if (buf_i >= sizeof(buf)) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200325 perror("preventing USB buffer overflow");
326 return;
327 }
Kévin Redon3b7624c2018-07-10 16:03:27 +0200328 int processed;
329 while ((processed = process_usb_msg(buf, buf_i)) > 0) {
330 if (processed > buf_i) {
331 break;
332 }
Kévin Redon6e3f1122018-07-01 18:24:42 +0200333 for (i = processed; i < buf_i; i++) {
334 buf[i-processed] = buf[i];
335 }
336 buf_i -= processed;
337 }
338 }
339 }
340}
341
342static void print_welcome(void)
343{
344 printf("simtrace2-sniff - Phone-SIM card communication sniffer \n"
345 "(C) 2010-2017 by Harald Welte <laforge@gnumonks.org>\n"
346 "(C) 2018 by Kevin Redon <kredon@sysmocom.de>\n"
347 "\n"
348 );
349}
350
351static void print_help(void)
352{
353 printf(
354 "\t-h\t--help\n"
355 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
356 "\t-k\t--keep-running\n"
357 "\t-V\t--usb-vendor\tVENDOR_ID\n"
358 "\t-P\t--usb-product\tPRODUCT_ID\n"
359 "\t-C\t--usb-config\tCONFIG_ID\n"
360 "\t-I\t--usb-interface\tINTERFACE_ID\n"
361 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
362 "\t-A\t--usb-address\tADDRESS\n"
363 "\n"
364 );
365}
366
367static const struct option opts[] = {
368 { "help", 0, 0, 'h' },
369 { "gsmtap-ip", 1, 0, 'i' },
370 { "keep-running", 0, 0, 'k' },
371 { "usb-vendor", 1, 0, 'V' },
372 { "usb-product", 1, 0, 'P' },
373 { "usb-config", 1, 0, 'C' },
374 { "usb-interface", 1, 0, 'I' },
375 { "usb-altsetting", 1, 0, 'S' },
376 { "usb-address", 1, 0, 'A' },
377 { NULL, 0, 0, 0 }
378};
379
380/* Known USB device with SIMtrace firmware supporting sniffer */
381static const struct dev_id compatible_dev_ids[] = {
382 { USB_VENDOR_OPENMOKO, USB_PRODUCT_SIMTRACE2 },
383 { 0, 0 }
384};
385
386static void signal_handler(int signal)
387{
388 switch (signal) {
389 case SIGINT:
390 exit(0);
391 break;
392 default:
393 break;
394 }
395}
396
397int main(int argc, char **argv)
398{
399 int i, rc, ret;
400 print_welcome();
401
402 /* Parse arguments */
403 char *gsmtap_host = "127.0.0.1";
404 int keep_running = 0;
405 int vendor_id = -1, product_id = -1, addr = -1, config_id = -1, if_num = -1, altsetting = -1;
406
407 while (1) {
408 int option_index = 0;
409
410 char c = getopt_long(argc, argv, "hi:kV:P:C:I:S:A:", opts, &option_index);
411 if (c == -1)
412 break;
413 switch (c) {
414 case 'h':
415 print_help();
416 exit(0);
417 break;
418 case 'i':
419 gsmtap_host = optarg;
420 break;
421 case 'k':
422 keep_running = 1;
423 break;
424 case 'V':
425 vendor_id = strtol(optarg, NULL, 16);
426 break;
427 case 'P':
428 product_id = strtol(optarg, NULL, 16);
429 break;
430 case 'C':
431 config_id = atoi(optarg);
432 break;
433 case 'I':
434 if_num = atoi(optarg);
435 break;
436 case 'S':
437 altsetting = atoi(optarg);
438 break;
439 case 'A':
440 addr = atoi(optarg);
441 break;
442 }
443 }
444
445 /* Scan for available SIMtrace USB devices supporting sniffing */
446 rc = libusb_init(NULL);
447 if (rc < 0) {
448 fprintf(stderr, "libusb initialization failed\n");
449 goto do_exit;
450 }
451 struct usb_interface_match ifm_scan[16];
452 int num_interfaces = usb_match_interfaces(NULL, compatible_dev_ids,
453 USB_CLASS_PROPRIETARY, SIMTRACE_SNIFFER_USB_SUBCLASS, -1, ifm_scan, ARRAY_SIZE(ifm_scan));
454 if (num_interfaces <= 0) {
455 perror("No compatible USB devices found");
456 goto do_exit;
457 }
458
459 /* Only keep USB matching arguments */
460 struct usb_interface_match ifm_filtered[ARRAY_SIZE(ifm_scan)];
461 int num_filtered = 0;
462 for (i = 0; i < num_interfaces; i++) {
463 if (vendor_id>=0 && vendor_id!=ifm_scan[i].vendor) {
464 continue;
465 }
466 if (product_id>=0 && product_id!=ifm_scan[i].product) {
467 continue;
468 }
469 if (config_id>=0 && config_id!=ifm_scan[i].configuration) {
470 continue;
471 }
472 if (if_num>=0 && if_num!=ifm_scan[i].interface) {
473 continue;
474 }
475 if (altsetting>=0 && altsetting!=ifm_scan[i].altsetting) {
476 continue;
477 }
478 if (addr>=0 && addr!=ifm_scan[i].addr) {
479 continue;
480 }
481 ifm_filtered[num_filtered++] = ifm_scan[i];
482 }
483 if (1!=num_filtered) {
484 perror("No individual matching USB devices found");
485 printf("Available USB devices:\n");
486 for (i = 0; i < num_interfaces; i++) {
487 printf("\t%04x:%04x Addr=%u, Path=%s, Cfg=%u, Intf=%u, Alt=%u: %d/%d/%d ",
488 ifm_scan[i].vendor, ifm_scan[i].product, ifm_scan[i].addr, ifm_scan[i].path,
489 ifm_scan[i].configuration, ifm_scan[i].interface, ifm_scan[i].altsetting,
490 ifm_scan[i].class, ifm_scan[i].sub_class, ifm_scan[i].protocol);
491 libusb_device_handle *dev_handle;
492 rc = libusb_open(ifm_scan[i].usb_dev, &dev_handle);
493 if (rc < 0) {
494 printf("\n");
495 perror("Cannot open device");
496 continue;
497 }
498 char strbuf[256];
499 rc = libusb_get_string_descriptor_ascii(dev_handle, ifm_scan[i].string_idx,
500 (unsigned char *)strbuf, sizeof(strbuf));
501 libusb_close(dev_handle);
502 if (rc < 0) {
503 printf("\n");
504 perror("Cannot read string");
505 continue;
506 }
507 printf("(%s)\n", strbuf);
508 }
509 goto do_exit;
510 }
511 struct usb_interface_match ifm_selected = ifm_filtered[0];
512 printf("Using USB device %04x:%04x Addr=%u, Path=%s, Cfg=%u, Intf=%u, Alt=%u: %d/%d/%d ",
513 ifm_selected.vendor, ifm_selected.product, ifm_selected.addr, ifm_selected.path,
514 ifm_selected.configuration, ifm_selected.interface, ifm_selected.altsetting,
515 ifm_selected.class, ifm_selected.sub_class, ifm_selected.protocol);
516 libusb_device_handle *dev_handle;
517 rc = libusb_open(ifm_selected.usb_dev, &dev_handle);
518 if (rc < 0) {
519 printf("\n");
520 perror("Cannot open device");
521 }
522 char strbuf[256];
523 rc = libusb_get_string_descriptor_ascii(dev_handle, ifm_selected.string_idx,
524 (unsigned char *)strbuf, sizeof(strbuf));
525 libusb_close(dev_handle);
526 if (rc < 0) {
527 printf("\n");
528 perror("Cannot read string");
529 }
530 printf("(%s)\n", strbuf);
531
532 g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
533 if (!g_gti) {
534 perror("unable to open GSMTAP");
535 goto close_exit;
536 }
537 gsmtap_source_add_sink(g_gti);
538
539 signal(SIGINT, &signal_handler);
540
541 do {
542 _transp.usb_devh = usb_open_claim_interface(NULL, &ifm_selected);
543 if (!_transp.usb_devh) {
544 fprintf(stderr, "can't open USB device\n");
545 goto close_exit;
546 }
547
548 rc = libusb_claim_interface(_transp.usb_devh, ifm_selected.interface);
549 if (rc < 0) {
550 fprintf(stderr, "can't claim interface %d; rc=%d\n", ifm_selected.interface, rc);
551 goto close_exit;
552 }
553
554 rc = get_usb_ep_addrs(_transp.usb_devh, ifm_selected.interface, &_transp.usb_ep.out,
555 &_transp.usb_ep.in, &_transp.usb_ep.irq_in);
556 if (rc < 0) {
557 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
558 goto close_exit;
559 }
560
561 run_mainloop();
562 ret = 0;
563
564 if (_transp.usb_devh)
565 libusb_release_interface(_transp.usb_devh, 0);
566close_exit:
567 if (_transp.usb_devh)
568 libusb_close(_transp.usb_devh);
569 if (keep_running)
570 sleep(1);
571 } while (keep_running);
572
573 libusb_exit(NULL);
574do_exit:
575 return ret;
576}