blob: dfb6e7c6759d8cf8c87bca42021c16519527bf33 [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
Harald Welte7f7de1e2019-12-15 20:17:44 +010040#include <osmocom/usb/libusb.h>
Harald Welte964cda32019-11-24 22:27:10 +010041#include <osmocom/simtrace2/simtrace_usb.h>
42#include <osmocom/simtrace2/simtrace_prot.h>
Kévin Redon6e3f1122018-07-01 18:24:42 +020043
Harald Welte964cda32019-11-24 22:27:10 +010044#include <osmocom/simtrace2/gsmtap.h>
45
Kévin Redon6e3f1122018-07-01 18:24:42 +020046#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
Kévin Redon31ed8022018-07-10 16:04:00 +020063const struct value_string change_flags[] = {
Harald Welte17cda3d2019-12-17 12:03:48 +010064 { SNIFF_CHANGE_FLAG_CARD_INSERT, "card inserted" },
65 { SNIFF_CHANGE_FLAG_CARD_EJECT, "card ejected" },
66 { SNIFF_CHANGE_FLAG_RESET_ASSERT, "reset asserted" },
67 { SNIFF_CHANGE_FLAG_RESET_DEASSERT, "reset de-asserted" },
68 { SNIFF_CHANGE_FLAG_TIMEOUT_WT, "data transfer timeout" },
69 { 0, NULL }
Kévin Redon31ed8022018-07-10 16:04:00 +020070};
71
72const struct value_string data_flags[] = {
Harald Welte17cda3d2019-12-17 12:03:48 +010073 { SNIFF_DATA_FLAG_ERROR_INCOMPLETE, "incomplete" },
74 { SNIFF_DATA_FLAG_ERROR_MALFORMED, "malformed" },
75 { SNIFF_DATA_FLAG_ERROR_CHECKSUM, "checksum error" },
76 { 0, NULL }
Kévin Redon31ed8022018-07-10 16:04:00 +020077};
78
79static void print_flags(const struct value_string* flag_meanings, uint32_t nb_flags, uint32_t flags) {
80 uint32_t i;
81 for (i = 0; i < nb_flags; i++) {
82 if (flags & flag_meanings[i].value) {
Oliver Smith15e943a2019-11-27 19:08:55 +010083 printf("%s", flag_meanings[i].str);
Kévin Redon31ed8022018-07-10 16:04:00 +020084 flags &= ~flag_meanings[i].value;
85 if (flags) {
86 printf(", ");
87 }
88 }
89 }
90}
91
Kévin Redon709a4312018-07-03 16:10:04 +020092static int process_change(const uint8_t *buf, int len)
Kévin Redon6e3f1122018-07-01 18:24:42 +020093{
94 /* check if there is enough data for the structure */
Kévin Redonf82f0f62018-07-08 15:10:23 +020095 if (len < sizeof(struct sniff_change)) {
Kévin Redon6e3f1122018-07-01 18:24:42 +020096 return -1;
97 }
98 struct sniff_change *change = (struct sniff_change *)buf;
99
Kévin Redon709a4312018-07-03 16:10:04 +0200100 printf("Card state change: ");
Kévin Redon31ed8022018-07-10 16:04:00 +0200101 if (change->flags) {
102 print_flags(change_flags, ARRAY_SIZE(change_flags), change->flags);
103 printf("\n");
104 } else {
105 printf("no changes\n");
Kévin Redon6e3f1122018-07-01 18:24:42 +0200106 }
Kévin Redon6e3f1122018-07-01 18:24:42 +0200107
108 return 0;
109}
110
111/* Table 7 of ISO 7816-3:2006 */
112static const uint16_t fi_table[] = { 372, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0, };
113
114/* Table 8 from ISO 7816-3:2006 */
115static const uint8_t di_table[] = { 0, 1, 2, 4, 8, 16, 32, 64, 12, 20, 2, 4, 8, 16, 32, 64, };
116
Kévin Redon709a4312018-07-03 16:10:04 +0200117static int process_fidi(const uint8_t *buf, int len)
Kévin Redon6e3f1122018-07-01 18:24:42 +0200118{
119 /* check if there is enough data for the structure */
120 if (len<sizeof(struct sniff_fidi)) {
121 return -1;
122 }
123 struct sniff_fidi *fidi = (struct sniff_fidi *)buf;
124
125 printf("Fi/Di switched to %u/%u\n", fi_table[fidi->fidi>>4], di_table[fidi->fidi&0x0f]);
126 return 0;
127}
128
Kévin Redonf82f0f62018-07-08 15:10:23 +0200129static int process_data(enum simtrace_msg_type_sniff type, const uint8_t *buf, int len)
Kévin Redon6e3f1122018-07-01 18:24:42 +0200130{
131 /* check if there is enough data for the structure */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200132 if (len < sizeof(struct sniff_data)) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200133 return -1;
134 }
Kévin Redonf82f0f62018-07-08 15:10:23 +0200135 struct sniff_data *data = (struct sniff_data *)buf;
Kévin Redon6e3f1122018-07-01 18:24:42 +0200136
137 /* check if the data is available */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200138 if (len < sizeof(struct sniff_data) + data->length) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200139 return -2;
140 }
141
Kévin Redonf82f0f62018-07-08 15:10:23 +0200142 /* check type */
143 if (type != SIMTRACE_MSGT_SNIFF_ATR && type != SIMTRACE_MSGT_SNIFF_PPS && type != SIMTRACE_MSGT_SNIFF_TPDU) {
144 return -3;
145 }
146
147 /* Print message */
148 switch (type) {
149 case SIMTRACE_MSGT_SNIFF_ATR:
150 printf("ATR");
151 break;
152 case SIMTRACE_MSGT_SNIFF_PPS:
153 printf("PPS");
154 break;
155 case SIMTRACE_MSGT_SNIFF_TPDU:
156 printf("TPDU");
157 break;
158 default:
159 printf("???");
160 break;
161 }
162 if (data->flags) {
163 printf(" (");
Kévin Redon31ed8022018-07-10 16:04:00 +0200164 print_flags(data_flags, ARRAY_SIZE(data_flags), data->flags);
Kévin Redonf82f0f62018-07-08 15:10:23 +0200165 printf(")");
166 }
167 printf(": ");
Kévin Redon6e3f1122018-07-01 18:24:42 +0200168 uint16_t i;
Kévin Redonf82f0f62018-07-08 15:10:23 +0200169 for (i = 0; i < data->length; i++) {
170 printf("%02x ", data->data[i]);
Kévin Redon6e3f1122018-07-01 18:24:42 +0200171 }
172 printf("\n");
Kévin Redon709a4312018-07-03 16:10:04 +0200173
Kévin Redond1c65362018-07-26 14:51:15 +0200174 /* Send message as GSNTAP */
175 switch (type) {
176 case SIMTRACE_MSGT_SNIFF_ATR:
Harald Welte964cda32019-11-24 22:27:10 +0100177 osmo_st2_gsmtap_send_apdu(GSMTAP_SIM_ATR, data->data, data->length);
Kévin Redond1c65362018-07-26 14:51:15 +0200178 break;
179 case SIMTRACE_MSGT_SNIFF_TPDU:
180 /* TPDU is now considered as APDU since SIMtrace sends complete TPDU */
Harald Welte964cda32019-11-24 22:27:10 +0100181 osmo_st2_gsmtap_send_apdu(GSMTAP_SIM_APDU, data->data, data->length);
Kévin Redond1c65362018-07-26 14:51:15 +0200182 break;
183 default:
184 break;
Kévin Redonf82f0f62018-07-08 15:10:23 +0200185 }
186
Kévin Redon6e3f1122018-07-01 18:24:42 +0200187 return 0;
188}
189
190/*! \brief Process an incoming message from the SIMtrace2 */
Kévin Redon709a4312018-07-03 16:10:04 +0200191static int process_usb_msg(const uint8_t *buf, int len)
Kévin Redon6e3f1122018-07-01 18:24:42 +0200192{
193 /* check if enough data for the header is present */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200194 if (len < sizeof(struct simtrace_msg_hdr)) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200195 return 0;
196 }
197
198 /* check if message is complete */
199 struct simtrace_msg_hdr *msg_hdr = (struct simtrace_msg_hdr *)buf;
Kévin Redonf82f0f62018-07-08 15:10:23 +0200200 if (len < msg_hdr->msg_len) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200201 return 0;
202 }
203 //printf("msg: %s\n", osmo_hexdump(buf, msg_hdr->msg_len));
204
205 /* check for message class */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200206 if (SIMTRACE_MSGC_SNIFF != msg_hdr->msg_class) { /* we only care about sniffing messages */
Kévin Redon6e3f1122018-07-01 18:24:42 +0200207 return msg_hdr->msg_len; /* discard non-sniffing messaged */
208 }
209
210 /* process sniff message payload */
211 buf += sizeof(struct simtrace_msg_hdr);
212 len -= sizeof(struct simtrace_msg_hdr);
213 switch (msg_hdr->msg_type) {
214 case SIMTRACE_MSGT_SNIFF_CHANGE:
215 process_change(buf, len);
216 break;
217 case SIMTRACE_MSGT_SNIFF_FIDI:
218 process_fidi(buf, len);
219 break;
220 case SIMTRACE_MSGT_SNIFF_ATR:
Kévin Redon6e3f1122018-07-01 18:24:42 +0200221 case SIMTRACE_MSGT_SNIFF_PPS:
Kévin Redon6e3f1122018-07-01 18:24:42 +0200222 case SIMTRACE_MSGT_SNIFF_TPDU:
Kévin Redonf82f0f62018-07-08 15:10:23 +0200223 process_data(msg_hdr->msg_type, buf, len);
Kévin Redon6e3f1122018-07-01 18:24:42 +0200224 break;
225 default:
226 printf("unknown SIMtrace msg type 0x%02x\n", msg_hdr->msg_type);
227 break;
228 }
229
230 return msg_hdr->msg_len;
231}
232
233/*! Transport to SIMtrace device (e.g. USB handle) */
234static struct st_transport _transp;
235
236static void run_mainloop()
237{
238 int rc;
239 uint8_t buf[16*256];
240 unsigned int i, buf_i = 0;
241 int xfer_len;
242
243 printf("Entering main loop\n");
244
245 while (true) {
246 /* read data from SIMtrace2 device (via USB) */
247 rc = libusb_bulk_transfer(_transp.usb_devh, _transp.usb_ep.in,
248 &buf[buf_i], sizeof(buf)-buf_i, &xfer_len, 100000);
249 if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
250 rc != LIBUSB_ERROR_INTERRUPTED &&
251 rc != LIBUSB_ERROR_IO) {
252 fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
253 return;
254 }
255 /* dispatch any incoming data */
256 if (xfer_len > 0) {
257 //printf("URB: %s\n", osmo_hexdump(&buf[buf_i], xfer_len));
258 buf_i += xfer_len;
Kévin Redon3b7624c2018-07-10 16:03:27 +0200259 if (buf_i >= sizeof(buf)) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200260 perror("preventing USB buffer overflow");
261 return;
262 }
Kévin Redon3b7624c2018-07-10 16:03:27 +0200263 int processed;
264 while ((processed = process_usb_msg(buf, buf_i)) > 0) {
265 if (processed > buf_i) {
266 break;
267 }
Kévin Redon6e3f1122018-07-01 18:24:42 +0200268 for (i = processed; i < buf_i; i++) {
269 buf[i-processed] = buf[i];
270 }
271 buf_i -= processed;
272 }
273 }
274 }
275}
276
277static void print_welcome(void)
278{
279 printf("simtrace2-sniff - Phone-SIM card communication sniffer \n"
280 "(C) 2010-2017 by Harald Welte <laforge@gnumonks.org>\n"
281 "(C) 2018 by Kevin Redon <kredon@sysmocom.de>\n"
282 "\n"
283 );
284}
285
286static void print_help(void)
287{
288 printf(
289 "\t-h\t--help\n"
290 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
291 "\t-k\t--keep-running\n"
292 "\t-V\t--usb-vendor\tVENDOR_ID\n"
293 "\t-P\t--usb-product\tPRODUCT_ID\n"
294 "\t-C\t--usb-config\tCONFIG_ID\n"
295 "\t-I\t--usb-interface\tINTERFACE_ID\n"
296 "\t-S\t--usb-altsetting ALTSETTING_ID\n"
297 "\t-A\t--usb-address\tADDRESS\n"
298 "\n"
299 );
300}
301
302static const struct option opts[] = {
303 { "help", 0, 0, 'h' },
304 { "gsmtap-ip", 1, 0, 'i' },
305 { "keep-running", 0, 0, 'k' },
306 { "usb-vendor", 1, 0, 'V' },
307 { "usb-product", 1, 0, 'P' },
308 { "usb-config", 1, 0, 'C' },
309 { "usb-interface", 1, 0, 'I' },
310 { "usb-altsetting", 1, 0, 'S' },
311 { "usb-address", 1, 0, 'A' },
312 { NULL, 0, 0, 0 }
313};
314
315/* Known USB device with SIMtrace firmware supporting sniffer */
316static const struct dev_id compatible_dev_ids[] = {
317 { USB_VENDOR_OPENMOKO, USB_PRODUCT_SIMTRACE2 },
318 { 0, 0 }
319};
320
321static void signal_handler(int signal)
322{
323 switch (signal) {
324 case SIGINT:
325 exit(0);
326 break;
327 default:
328 break;
329 }
330}
331
332int main(int argc, char **argv)
333{
334 int i, rc, ret;
335 print_welcome();
336
337 /* Parse arguments */
338 char *gsmtap_host = "127.0.0.1";
339 int keep_running = 0;
340 int vendor_id = -1, product_id = -1, addr = -1, config_id = -1, if_num = -1, altsetting = -1;
341
342 while (1) {
343 int option_index = 0;
344
345 char c = getopt_long(argc, argv, "hi:kV:P:C:I:S:A:", opts, &option_index);
346 if (c == -1)
347 break;
348 switch (c) {
349 case 'h':
350 print_help();
351 exit(0);
352 break;
353 case 'i':
354 gsmtap_host = optarg;
355 break;
356 case 'k':
357 keep_running = 1;
358 break;
359 case 'V':
360 vendor_id = strtol(optarg, NULL, 16);
361 break;
362 case 'P':
363 product_id = strtol(optarg, NULL, 16);
364 break;
365 case 'C':
366 config_id = atoi(optarg);
367 break;
368 case 'I':
369 if_num = atoi(optarg);
370 break;
371 case 'S':
372 altsetting = atoi(optarg);
373 break;
374 case 'A':
375 addr = atoi(optarg);
376 break;
377 }
378 }
379
380 /* Scan for available SIMtrace USB devices supporting sniffing */
381 rc = libusb_init(NULL);
382 if (rc < 0) {
383 fprintf(stderr, "libusb initialization failed\n");
384 goto do_exit;
385 }
386 struct usb_interface_match ifm_scan[16];
Harald Welte7f7de1e2019-12-15 20:17:44 +0100387 int num_interfaces = osmo_libusb_find_matching_interfaces(NULL, compatible_dev_ids,
Kévin Redon6e3f1122018-07-01 18:24:42 +0200388 USB_CLASS_PROPRIETARY, SIMTRACE_SNIFFER_USB_SUBCLASS, -1, ifm_scan, ARRAY_SIZE(ifm_scan));
389 if (num_interfaces <= 0) {
390 perror("No compatible USB devices found");
391 goto do_exit;
392 }
393
394 /* Only keep USB matching arguments */
395 struct usb_interface_match ifm_filtered[ARRAY_SIZE(ifm_scan)];
396 int num_filtered = 0;
397 for (i = 0; i < num_interfaces; i++) {
398 if (vendor_id>=0 && vendor_id!=ifm_scan[i].vendor) {
399 continue;
400 }
401 if (product_id>=0 && product_id!=ifm_scan[i].product) {
402 continue;
403 }
404 if (config_id>=0 && config_id!=ifm_scan[i].configuration) {
405 continue;
406 }
407 if (if_num>=0 && if_num!=ifm_scan[i].interface) {
408 continue;
409 }
410 if (altsetting>=0 && altsetting!=ifm_scan[i].altsetting) {
411 continue;
412 }
413 if (addr>=0 && addr!=ifm_scan[i].addr) {
414 continue;
415 }
416 ifm_filtered[num_filtered++] = ifm_scan[i];
417 }
418 if (1!=num_filtered) {
419 perror("No individual matching USB devices found");
420 printf("Available USB devices:\n");
421 for (i = 0; i < num_interfaces; i++) {
422 printf("\t%04x:%04x Addr=%u, Path=%s, Cfg=%u, Intf=%u, Alt=%u: %d/%d/%d ",
423 ifm_scan[i].vendor, ifm_scan[i].product, ifm_scan[i].addr, ifm_scan[i].path,
424 ifm_scan[i].configuration, ifm_scan[i].interface, ifm_scan[i].altsetting,
425 ifm_scan[i].class, ifm_scan[i].sub_class, ifm_scan[i].protocol);
426 libusb_device_handle *dev_handle;
427 rc = libusb_open(ifm_scan[i].usb_dev, &dev_handle);
428 if (rc < 0) {
429 printf("\n");
430 perror("Cannot open device");
431 continue;
432 }
433 char strbuf[256];
434 rc = libusb_get_string_descriptor_ascii(dev_handle, ifm_scan[i].string_idx,
435 (unsigned char *)strbuf, sizeof(strbuf));
436 libusb_close(dev_handle);
437 if (rc < 0) {
438 printf("\n");
439 perror("Cannot read string");
440 continue;
441 }
442 printf("(%s)\n", strbuf);
443 }
444 goto do_exit;
445 }
446 struct usb_interface_match ifm_selected = ifm_filtered[0];
447 printf("Using USB device %04x:%04x Addr=%u, Path=%s, Cfg=%u, Intf=%u, Alt=%u: %d/%d/%d ",
448 ifm_selected.vendor, ifm_selected.product, ifm_selected.addr, ifm_selected.path,
449 ifm_selected.configuration, ifm_selected.interface, ifm_selected.altsetting,
450 ifm_selected.class, ifm_selected.sub_class, ifm_selected.protocol);
451 libusb_device_handle *dev_handle;
452 rc = libusb_open(ifm_selected.usb_dev, &dev_handle);
453 if (rc < 0) {
454 printf("\n");
455 perror("Cannot open device");
456 }
457 char strbuf[256];
458 rc = libusb_get_string_descriptor_ascii(dev_handle, ifm_selected.string_idx,
459 (unsigned char *)strbuf, sizeof(strbuf));
460 libusb_close(dev_handle);
461 if (rc < 0) {
462 printf("\n");
463 perror("Cannot read string");
464 }
465 printf("(%s)\n", strbuf);
466
Harald Welte964cda32019-11-24 22:27:10 +0100467 rc = osmo_st2_gsmtap_init(gsmtap_host);
468 if (rc < 0) {
Kévin Redon6e3f1122018-07-01 18:24:42 +0200469 perror("unable to open GSMTAP");
470 goto close_exit;
471 }
Kévin Redon6e3f1122018-07-01 18:24:42 +0200472
473 signal(SIGINT, &signal_handler);
474
475 do {
Harald Welte7f7de1e2019-12-15 20:17:44 +0100476 _transp.usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, &ifm_selected);
Kévin Redon6e3f1122018-07-01 18:24:42 +0200477 if (!_transp.usb_devh) {
478 fprintf(stderr, "can't open USB device\n");
479 goto close_exit;
480 }
481
482 rc = libusb_claim_interface(_transp.usb_devh, ifm_selected.interface);
483 if (rc < 0) {
484 fprintf(stderr, "can't claim interface %d; rc=%d\n", ifm_selected.interface, rc);
485 goto close_exit;
486 }
487
Harald Welte7f7de1e2019-12-15 20:17:44 +0100488 rc = osmo_libusb_get_ep_addrs(_transp.usb_devh, ifm_selected.interface, &_transp.usb_ep.out,
489 &_transp.usb_ep.in, &_transp.usb_ep.irq_in);
Kévin Redon6e3f1122018-07-01 18:24:42 +0200490 if (rc < 0) {
491 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
492 goto close_exit;
493 }
494
495 run_mainloop();
496 ret = 0;
497
498 if (_transp.usb_devh)
499 libusb_release_interface(_transp.usb_devh, 0);
500close_exit:
501 if (_transp.usb_devh)
502 libusb_close(_transp.usb_devh);
503 if (keep_running)
504 sleep(1);
505 } while (keep_running);
506
507 libusb_exit(NULL);
508do_exit:
509 return ret;
510}