mirror of
https://gitea.osmocom.org/sim-card/simtrace2.git
synced 2026-03-18 14:28:33 +03:00
host: use autotools and split shared code to libosmo-simtrace2
Change-Id: I57e77f927ee9e169cc794c5dc6b128a2d590201b
This commit is contained in:
18
host/src/Makefile.am
Normal file
18
host/src/Makefile.am
Normal file
@@ -0,0 +1,18 @@
|
||||
AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include
|
||||
AM_CFLAGS=-Wall -g $(LIBOSMOCORE_CFLAGS) $(LIBOSMOSIM_CFLAGS) $(LIBUSB_CFLAGS) $(COVERAGE_FLAGS)
|
||||
AM_LDFLAGS=$(COVERAGE_LDFLAGS)
|
||||
|
||||
LDADD= $(top_builddir)/lib/libosmo-simtrace2.la \
|
||||
$(LIBOSMOCORE_LIBS) $(LIBOSMOSIM_LIBS) $(LIBUSB_LIBS)
|
||||
|
||||
noinst_HEADERS = simtrace2-discovery.h
|
||||
|
||||
bin_PROGRAMS = simtrace2-remsim simtrace2-remsim-usb2udp simtrace2-list simtrace2-sniff
|
||||
|
||||
simtrace2_remsim_SOURCES = simtrace2-remsim.c simtrace2-discovery.c
|
||||
|
||||
simtrace2_remsim_usb2udp_SOURCES = usb2udp.c simtrace2-discovery.c
|
||||
|
||||
simtrace2_list_SOURCES = simtrace2_usb.c
|
||||
|
||||
simtrace2_sniff_SOURCES = simtrace2-sniff.c simtrace2-discovery.c
|
||||
94
host/src/simtrace2-discovery.c
Normal file
94
host/src/simtrace2-discovery.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/* simtrace2-discovery - host PC library to scan for matching USB
|
||||
* devices
|
||||
*
|
||||
* (C) 2016 by Harald Welte <hwelte@hmw-consulting.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
#include <stdint.h>
|
||||
|
||||
#include <libusb.h>
|
||||
|
||||
/*! \brief obtain the endpoint addresses for a given USB interface */
|
||||
int get_usb_ep_addrs(libusb_device_handle *devh, unsigned int if_num,
|
||||
uint8_t *out, uint8_t *in, uint8_t *irq)
|
||||
{
|
||||
libusb_device *dev = libusb_get_device(devh);
|
||||
struct libusb_config_descriptor *cdesc;
|
||||
const struct libusb_interface_descriptor *idesc;
|
||||
const struct libusb_interface *iface;
|
||||
int rc, l;
|
||||
|
||||
rc = libusb_get_active_config_descriptor(dev, &cdesc);
|
||||
if (rc < 0)
|
||||
return rc;
|
||||
|
||||
iface = &cdesc->interface[if_num];
|
||||
/* FIXME: we assume there's no altsetting */
|
||||
idesc = &iface->altsetting[0];
|
||||
|
||||
for (l = 0; l < idesc->bNumEndpoints; l++) {
|
||||
const struct libusb_endpoint_descriptor *edesc = &idesc->endpoint[l];
|
||||
switch (edesc->bmAttributes & 3) {
|
||||
case LIBUSB_TRANSFER_TYPE_BULK:
|
||||
if (edesc->bEndpointAddress & 0x80) {
|
||||
if (in)
|
||||
*in = edesc->bEndpointAddress;
|
||||
} else {
|
||||
if (out)
|
||||
*out = edesc->bEndpointAddress;
|
||||
}
|
||||
break;
|
||||
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
||||
if (irq)
|
||||
*irq = edesc->bEndpointAddress;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
struct libusb_device_descriptor ddesc;
|
||||
int rc, i, j, k;
|
||||
|
||||
rc = libusb_get_device_descriptor(devh, &ddesc);
|
||||
if (rc < 0)
|
||||
return;
|
||||
|
||||
for (i = 0; i < ddesc.bNumConfigurations; i++) {
|
||||
struct libusb_config_descriptor *cdesc;
|
||||
rc = libusb_get_config_descriptor(devh, i, &cdesc);
|
||||
if (rc < 0)
|
||||
return;
|
||||
|
||||
for (j = 0; j < cdesc->bNumInterfaces; j++) {
|
||||
const struct libusb_interface *iface = cdesc->interface[j];
|
||||
for (k = 0; k < iface->num_altsetting; k++) {
|
||||
const struct libusb_interface_descriptor *idesc = iface->altsetting[k];
|
||||
/* make sure this is the interface we're looking for */
|
||||
if (idesc->bInterfaceClass != 0xFF ||
|
||||
idesc->bInterfaceSubClass != if_class ||
|
||||
idsec->bInterfaceProtocol != if_proto)
|
||||
continue;
|
||||
/* FIXME */
|
||||
}
|
||||
}
|
||||
|
||||
libusb_free_config_descriptor(cdesc);
|
||||
}
|
||||
#endif
|
||||
26
host/src/simtrace2-discovery.h
Normal file
26
host/src/simtrace2-discovery.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/* simtrace2-discovery - host PC library to scan for matching USB
|
||||
* devices
|
||||
*
|
||||
* (C) 2016 by Harald Welte <hwelte@hmw-consulting.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <libusb.h>
|
||||
|
||||
int get_usb_ep_addrs(libusb_device_handle *devh, unsigned int if_num,
|
||||
uint8_t *out, uint8_t *in, uint8_t *irq);
|
||||
463
host/src/simtrace2-remsim.c
Normal file
463
host/src/simtrace2-remsim.c
Normal file
@@ -0,0 +1,463 @@
|
||||
/* simtrace2-remsim - main program for the host PC to provide a remote SIM
|
||||
* using the SIMtrace 2 firmware in card emulation mode
|
||||
*
|
||||
* (C) 2016-2017 by Harald Welte <hwelte@hmw-consulting.de>
|
||||
* (C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#define _GNU_SOURCE
|
||||
#include <getopt.h>
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <libusb.h>
|
||||
|
||||
#include <osmocom/simtrace2/libusb_util.h>
|
||||
#include <osmocom/simtrace2/simtrace2_api.h>
|
||||
#include <osmocom/simtrace2/simtrace_prot.h>
|
||||
#include <osmocom/simtrace2/apdu_dispatch.h>
|
||||
#include <osmocom/simtrace2/gsmtap.h>
|
||||
|
||||
#include "simtrace2-discovery.h"
|
||||
|
||||
#include <osmocom/core/utils.h>
|
||||
#include <osmocom/core/socket.h>
|
||||
#include <osmocom/core/msgb.h>
|
||||
#include <osmocom/sim/class_tables.h>
|
||||
#include <osmocom/sim/sim.h>
|
||||
|
||||
static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
|
||||
{
|
||||
uint8_t csum = 0;
|
||||
int i;
|
||||
|
||||
for (i = 1; i < atr_len - 1; i++)
|
||||
csum = csum ^ atr[i];
|
||||
|
||||
atr[atr_len-1] = csum;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* Incoming Messages
|
||||
***********************************************************************/
|
||||
|
||||
/*! \brief Process a STATUS message from the SIMtrace2 */
|
||||
static int process_do_status(struct cardem_inst *ci, uint8_t *buf, int len)
|
||||
{
|
||||
struct cardemu_usb_msg_status *status;
|
||||
status = (struct cardemu_usb_msg_status *) buf;
|
||||
|
||||
printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
|
||||
status->flags, status->fi, status->di, status->wi,
|
||||
status->waiting_time);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! \brief Process a PTS indication message from the SIMtrace2 */
|
||||
static int process_do_pts(struct cardem_inst *ci, uint8_t *buf, int len)
|
||||
{
|
||||
struct cardemu_usb_msg_pts_info *pts;
|
||||
pts = (struct cardemu_usb_msg_pts_info *) buf;
|
||||
|
||||
printf("=> PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
|
||||
static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
|
||||
{
|
||||
static struct apdu_context ac;
|
||||
struct cardemu_usb_msg_rx_data *data;
|
||||
int rc;
|
||||
|
||||
data = (struct cardemu_usb_msg_rx_data *) buf;
|
||||
|
||||
printf("=> DATA: flags=%x, %s: ", data->flags,
|
||||
osmo_hexdump(data->data, data->data_len));
|
||||
|
||||
rc = apdu_segment_in(&ac, data->data, data->data_len,
|
||||
data->flags & CEMU_DATA_F_TPDU_HDR);
|
||||
|
||||
if (rc & APDU_ACT_TX_CAPDU_TO_CARD) {
|
||||
struct msgb *tmsg = msgb_alloc(1024, "TPDU");
|
||||
struct osim_reader_hdl *rh = ci->chan->card->reader;
|
||||
uint8_t *cur;
|
||||
|
||||
/* Copy TPDU header */
|
||||
cur = msgb_put(tmsg, sizeof(ac.hdr));
|
||||
memcpy(cur, &ac.hdr, sizeof(ac.hdr));
|
||||
/* Copy D(c), if any */
|
||||
if (ac.lc.tot) {
|
||||
cur = msgb_put(tmsg, ac.lc.tot);
|
||||
memcpy(cur, ac.dc, ac.lc.tot);
|
||||
}
|
||||
/* send to actual card */
|
||||
tmsg->l3h = tmsg->tail;
|
||||
rc = rh->ops->transceive(rh, tmsg);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "error during transceive: %d\n", rc);
|
||||
msgb_free(tmsg);
|
||||
return rc;
|
||||
}
|
||||
msgb_apdu_sw(tmsg) = msgb_get_u16(tmsg);
|
||||
ac.sw[0] = msgb_apdu_sw(tmsg) >> 8;
|
||||
ac.sw[1] = msgb_apdu_sw(tmsg) & 0xff;
|
||||
printf("SW=0x%04x, len_rx=%d\n", msgb_apdu_sw(tmsg), msgb_l3len(tmsg));
|
||||
if (msgb_l3len(tmsg))
|
||||
cardem_request_pb_and_tx(ci, ac.hdr.ins, tmsg->l3h, msgb_l3len(tmsg));
|
||||
cardem_request_sw_tx(ci, ac.sw);
|
||||
} else if (ac.lc.tot > ac.lc.cur) {
|
||||
cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! \brief Process an incoming message from the SIMtrace2 */
|
||||
static int process_usb_msg(struct cardem_inst *ci, uint8_t *buf, int len)
|
||||
{
|
||||
struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
|
||||
int rc;
|
||||
|
||||
printf("-> %s\n", osmo_hexdump(buf, len));
|
||||
|
||||
buf += sizeof(*sh);
|
||||
|
||||
switch (sh->msg_type) {
|
||||
case SIMTRACE_MSGT_BD_CEMU_STATUS:
|
||||
rc = process_do_status(ci, buf, len);
|
||||
break;
|
||||
case SIMTRACE_MSGT_DO_CEMU_PTS:
|
||||
rc = process_do_pts(ci, buf, len);
|
||||
break;
|
||||
case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
|
||||
rc = process_do_rx_da(ci, buf, len);
|
||||
break;
|
||||
default:
|
||||
printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static void print_welcome(void)
|
||||
{
|
||||
printf("simtrace2-remsim - Remote SIM card forwarding\n"
|
||||
"(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
|
||||
"(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
|
||||
}
|
||||
|
||||
static void print_help(void)
|
||||
{
|
||||
printf( "\t-r\t--remote-udp-host HOST\n"
|
||||
"\t-p\t--remote-udp-port PORT\n"
|
||||
"\t-h\t--help\n"
|
||||
"\t-i\t--gsmtap-ip\tA.B.C.D\n"
|
||||
"\t-a\t--skip-atr\n"
|
||||
"\t-k\t--keep-running\n"
|
||||
"\t-V\t--usb-vendor\tVENDOR_ID\n"
|
||||
"\t-P\t--usb-product\tPRODUCT_ID\n"
|
||||
"\t-C\t--usb-config\tCONFIG_ID\n"
|
||||
"\t-I\t--usb-interface\tINTERFACE_ID\n"
|
||||
"\t-S\t--usb-altsetting ALTSETTING_ID\n"
|
||||
"\t-A\t--usb-address\tADDRESS\n"
|
||||
"\t-H\t--usb-path\tPATH\n"
|
||||
"\n"
|
||||
);
|
||||
}
|
||||
|
||||
static const struct option opts[] = {
|
||||
{ "remote-udp-host", 1, 0, 'r' },
|
||||
{ "remote-udp-port", 1, 0, 'p' },
|
||||
{ "gsmtap-ip", 1, 0, 'i' },
|
||||
{ "skip-atr", 0, 0, 'a' },
|
||||
{ "help", 0, 0, 'h' },
|
||||
{ "keep-running", 0, 0, 'k' },
|
||||
{ "usb-vendor", 1, 0, 'V' },
|
||||
{ "usb-product", 1, 0, 'P' },
|
||||
{ "usb-config", 1, 0, 'C' },
|
||||
{ "usb-interface", 1, 0, 'I' },
|
||||
{ "usb-altsetting", 1, 0, 'S' },
|
||||
{ "usb-address", 1, 0, 'A' },
|
||||
{ "usb-path", 1, 0, 'H' },
|
||||
{ NULL, 0, 0, 0 }
|
||||
};
|
||||
|
||||
static void run_mainloop(struct cardem_inst *ci)
|
||||
{
|
||||
struct st_transport *transp = ci->slot->transp;
|
||||
unsigned int msg_count, byte_count = 0;
|
||||
uint8_t buf[16*265];
|
||||
int xfer_len;
|
||||
int rc;
|
||||
|
||||
printf("Entering main loop\n");
|
||||
|
||||
while (1) {
|
||||
/* read data from SIMtrace2 device (local or via USB) */
|
||||
if (transp->udp_fd < 0) {
|
||||
rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
|
||||
buf, sizeof(buf), &xfer_len, 100);
|
||||
if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
|
||||
rc != LIBUSB_ERROR_INTERRUPTED &&
|
||||
rc != LIBUSB_ERROR_IO) {
|
||||
fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
rc = read(transp->udp_fd, buf, sizeof(buf));
|
||||
if (rc <= 0) {
|
||||
fprintf(stderr, "shor read from UDP\n");
|
||||
return;
|
||||
}
|
||||
xfer_len = rc;
|
||||
}
|
||||
/* dispatch any incoming data */
|
||||
if (xfer_len > 0) {
|
||||
printf("URB: %s\n", osmo_hexdump(buf, xfer_len));
|
||||
process_usb_msg(ci, buf, xfer_len);
|
||||
msg_count++;
|
||||
byte_count += xfer_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static struct st_transport _transp;
|
||||
|
||||
static struct st_slot _slot = {
|
||||
.transp = &_transp,
|
||||
.slot_nr = 0,
|
||||
};
|
||||
|
||||
struct cardem_inst _ci = {
|
||||
.slot = &_slot,
|
||||
};
|
||||
|
||||
struct cardem_inst *ci = &_ci;
|
||||
|
||||
static void signal_handler(int signal)
|
||||
{
|
||||
switch (signal) {
|
||||
case SIGINT:
|
||||
cardem_request_card_insert(ci, false);
|
||||
exit(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct st_transport *transp = ci->slot->transp;
|
||||
char *gsmtap_host = "127.0.0.1";
|
||||
int rc;
|
||||
int c, ret = 1;
|
||||
int skip_atr = 0;
|
||||
int keep_running = 0;
|
||||
int remote_udp_port = 52342;
|
||||
int if_num = 0, vendor_id = -1, product_id = -1;
|
||||
int config_id = -1, altsetting = 0, addr = -1;
|
||||
char *remote_udp_host = NULL;
|
||||
char *path = NULL;
|
||||
struct osim_reader_hdl *reader;
|
||||
struct osim_card_hdl *card;
|
||||
|
||||
print_welcome();
|
||||
|
||||
while (1) {
|
||||
int option_index = 0;
|
||||
|
||||
c = getopt_long(argc, argv, "r:p:hi:V:P:C:I:S:A:H:ak", opts, &option_index);
|
||||
if (c == -1)
|
||||
break;
|
||||
switch (c) {
|
||||
case 'r':
|
||||
remote_udp_host = optarg;
|
||||
break;
|
||||
case 'p':
|
||||
remote_udp_port = atoi(optarg);
|
||||
break;
|
||||
case 'h':
|
||||
print_help();
|
||||
exit(0);
|
||||
break;
|
||||
case 'i':
|
||||
gsmtap_host = optarg;
|
||||
break;
|
||||
case 'a':
|
||||
skip_atr = 1;
|
||||
break;
|
||||
case 'k':
|
||||
keep_running = 1;
|
||||
break;
|
||||
case 'V':
|
||||
vendor_id = strtol(optarg, NULL, 16);
|
||||
break;
|
||||
case 'P':
|
||||
product_id = strtol(optarg, NULL, 16);
|
||||
break;
|
||||
case 'C':
|
||||
config_id = atoi(optarg);
|
||||
break;
|
||||
case 'I':
|
||||
if_num = atoi(optarg);
|
||||
break;
|
||||
case 'S':
|
||||
altsetting = atoi(optarg);
|
||||
break;
|
||||
case 'A':
|
||||
addr = atoi(optarg);
|
||||
break;
|
||||
case 'H':
|
||||
path = optarg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!remote_udp_host && (vendor_id < 0 || product_id < 0)) {
|
||||
fprintf(stderr, "You have to specify the vendor and product ID\n");
|
||||
goto do_exit;
|
||||
}
|
||||
|
||||
transp->udp_fd = -1;
|
||||
|
||||
ci->card_prof = &osim_uicc_sim_cic_profile;
|
||||
|
||||
if (!remote_udp_host) {
|
||||
rc = libusb_init(NULL);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "libusb initialization failed\n");
|
||||
goto do_exit;
|
||||
}
|
||||
} else {
|
||||
transp->udp_fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
|
||||
remote_udp_host, remote_udp_port+if_num,
|
||||
OSMO_SOCK_F_CONNECT);
|
||||
if (transp->udp_fd < 0) {
|
||||
fprintf(stderr, "error binding UDP port\n");
|
||||
goto do_exit;
|
||||
}
|
||||
}
|
||||
|
||||
rc = osmo_st2_gsmtap_init(gsmtap_host);
|
||||
if (rc < 0) {
|
||||
perror("unable to open GSMTAP");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
reader = osim_reader_open(OSIM_READER_DRV_PCSC, 0, "", NULL);
|
||||
if (!reader) {
|
||||
perror("unable to open PC/SC reader");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
card = osim_card_open(reader, OSIM_PROTO_T0);
|
||||
if (!card) {
|
||||
perror("unable to open SIM card");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
ci->chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
|
||||
if (!ci->chan) {
|
||||
perror("SIM card has no channel?!?");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
signal(SIGINT, &signal_handler);
|
||||
|
||||
do {
|
||||
if (transp->udp_fd < 0) {
|
||||
struct usb_interface_match _ifm, *ifm = &_ifm;
|
||||
ifm->vendor = vendor_id;
|
||||
ifm->product = product_id;
|
||||
ifm->configuration = config_id;
|
||||
ifm->interface = if_num;
|
||||
ifm->altsetting = altsetting;
|
||||
ifm->addr = addr;
|
||||
if (path)
|
||||
osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
|
||||
transp->usb_devh = usb_open_claim_interface(NULL, ifm);
|
||||
if (!transp->usb_devh) {
|
||||
fprintf(stderr, "can't open USB device\n");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
rc = libusb_claim_interface(transp->usb_devh, if_num);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
|
||||
&transp->usb_ep.in, &transp->usb_ep.irq_in);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
|
||||
goto close_exit;
|
||||
}
|
||||
}
|
||||
|
||||
/* simulate card-insert to modem (owhw, not qmod) */
|
||||
cardem_request_card_insert(ci, true);
|
||||
|
||||
/* select remote (forwarded) SIM */
|
||||
st_modem_sim_select_remote(ci->slot);
|
||||
|
||||
if (!skip_atr) {
|
||||
/* set the ATR */
|
||||
uint8_t real_atr[] = { 0x3B, 0x9F, 0x96, 0x80, 0x1F, 0xC7, 0x80, 0x31,
|
||||
0xA0, 0x73, 0xBE, 0x21, 0x13, 0x67, 0x43, 0x20,
|
||||
0x07, 0x18, 0x00, 0x00, 0x01, 0xA5 };
|
||||
atr_update_csum(real_atr, sizeof(real_atr));
|
||||
cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
|
||||
}
|
||||
|
||||
/* select remote (forwarded) SIM */
|
||||
st_modem_reset_pulse(ci->slot, 300);
|
||||
|
||||
run_mainloop(ci);
|
||||
ret = 0;
|
||||
|
||||
if (transp->udp_fd < 0)
|
||||
libusb_release_interface(transp->usb_devh, 0);
|
||||
close_exit:
|
||||
if (transp->usb_devh)
|
||||
libusb_close(transp->usb_devh);
|
||||
if (keep_running)
|
||||
sleep(1);
|
||||
} while (keep_running);
|
||||
|
||||
if (transp->udp_fd < 0)
|
||||
libusb_exit(NULL);
|
||||
do_exit:
|
||||
return ret;
|
||||
}
|
||||
541
host/src/simtrace2-sniff.c
Normal file
541
host/src/simtrace2-sniff.c
Normal file
@@ -0,0 +1,541 @@
|
||||
/* simtrace2-sniff - main program for the host PC to communicate with the
|
||||
* SIMtrace 2 firmware in sniffer mode
|
||||
*
|
||||
* (C) 2016 by Harald Welte <hwelte@hmw-consulting.de>
|
||||
* (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#define _GNU_SOURCE
|
||||
#include <getopt.h>
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <libusb.h>
|
||||
|
||||
#include <osmocom/simtrace2/libusb_util.h>
|
||||
#include <osmocom/simtrace2/simtrace_usb.h>
|
||||
#include <osmocom/simtrace2/simtrace_prot.h>
|
||||
#include "simtrace2-discovery.h"
|
||||
|
||||
#include <osmocom/simtrace2/gsmtap.h>
|
||||
|
||||
#include <osmocom/core/utils.h>
|
||||
#include <osmocom/core/socket.h>
|
||||
#include <osmocom/core/msgb.h>
|
||||
#include <osmocom/sim/class_tables.h>
|
||||
#include <osmocom/sim/sim.h>
|
||||
|
||||
/* transport to a SIMtrace device */
|
||||
struct st_transport {
|
||||
/* USB */
|
||||
struct libusb_device_handle *usb_devh;
|
||||
struct {
|
||||
uint8_t in;
|
||||
uint8_t out;
|
||||
uint8_t irq_in;
|
||||
} usb_ep;
|
||||
};
|
||||
|
||||
const struct value_string change_flags[] = {
|
||||
{
|
||||
.value = SNIFF_CHANGE_FLAG_CARD_INSERT,
|
||||
.str = "card inserted",
|
||||
},
|
||||
{
|
||||
.value = SNIFF_CHANGE_FLAG_CARD_EJECT,
|
||||
.str = "card ejected",
|
||||
},
|
||||
{
|
||||
.value = SNIFF_CHANGE_FLAG_RESET_ASSERT,
|
||||
.str = "reset asserted",
|
||||
},
|
||||
{
|
||||
.value = SNIFF_CHANGE_FLAG_RESET_DEASSERT,
|
||||
.str = "reset de-asserted",
|
||||
},
|
||||
{
|
||||
.value = SNIFF_CHANGE_FLAG_TIMEOUT_WT,
|
||||
.str = "data transfer timeout",
|
||||
},
|
||||
{
|
||||
.value = 0,
|
||||
.str = NULL,
|
||||
},
|
||||
};
|
||||
|
||||
const struct value_string data_flags[] = {
|
||||
{
|
||||
.value = SNIFF_DATA_FLAG_ERROR_INCOMPLETE,
|
||||
.str = "incomplete",
|
||||
},
|
||||
{
|
||||
.value = SNIFF_DATA_FLAG_ERROR_MALFORMED,
|
||||
.str = "malformed",
|
||||
},
|
||||
{
|
||||
.value = SNIFF_DATA_FLAG_ERROR_CHECKSUM,
|
||||
.str = "checksum error",
|
||||
},
|
||||
{
|
||||
.value = 0,
|
||||
.str = NULL,
|
||||
},
|
||||
};
|
||||
|
||||
static void print_flags(const struct value_string* flag_meanings, uint32_t nb_flags, uint32_t flags) {
|
||||
uint32_t i;
|
||||
for (i = 0; i < nb_flags; i++) {
|
||||
if (flags & flag_meanings[i].value) {
|
||||
printf(flag_meanings[i].str);
|
||||
flags &= ~flag_meanings[i].value;
|
||||
if (flags) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int process_change(const uint8_t *buf, int len)
|
||||
{
|
||||
/* check if there is enough data for the structure */
|
||||
if (len < sizeof(struct sniff_change)) {
|
||||
return -1;
|
||||
}
|
||||
struct sniff_change *change = (struct sniff_change *)buf;
|
||||
|
||||
printf("Card state change: ");
|
||||
if (change->flags) {
|
||||
print_flags(change_flags, ARRAY_SIZE(change_flags), change->flags);
|
||||
printf("\n");
|
||||
} else {
|
||||
printf("no changes\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Table 7 of ISO 7816-3:2006 */
|
||||
static const uint16_t fi_table[] = { 372, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0, };
|
||||
|
||||
/* Table 8 from ISO 7816-3:2006 */
|
||||
static const uint8_t di_table[] = { 0, 1, 2, 4, 8, 16, 32, 64, 12, 20, 2, 4, 8, 16, 32, 64, };
|
||||
|
||||
static int process_fidi(const uint8_t *buf, int len)
|
||||
{
|
||||
/* check if there is enough data for the structure */
|
||||
if (len<sizeof(struct sniff_fidi)) {
|
||||
return -1;
|
||||
}
|
||||
struct sniff_fidi *fidi = (struct sniff_fidi *)buf;
|
||||
|
||||
printf("Fi/Di switched to %u/%u\n", fi_table[fidi->fidi>>4], di_table[fidi->fidi&0x0f]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int process_data(enum simtrace_msg_type_sniff type, const uint8_t *buf, int len)
|
||||
{
|
||||
/* check if there is enough data for the structure */
|
||||
if (len < sizeof(struct sniff_data)) {
|
||||
return -1;
|
||||
}
|
||||
struct sniff_data *data = (struct sniff_data *)buf;
|
||||
|
||||
/* check if the data is available */
|
||||
if (len < sizeof(struct sniff_data) + data->length) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* check type */
|
||||
if (type != SIMTRACE_MSGT_SNIFF_ATR && type != SIMTRACE_MSGT_SNIFF_PPS && type != SIMTRACE_MSGT_SNIFF_TPDU) {
|
||||
return -3;
|
||||
}
|
||||
|
||||
/* Print message */
|
||||
switch (type) {
|
||||
case SIMTRACE_MSGT_SNIFF_ATR:
|
||||
printf("ATR");
|
||||
break;
|
||||
case SIMTRACE_MSGT_SNIFF_PPS:
|
||||
printf("PPS");
|
||||
break;
|
||||
case SIMTRACE_MSGT_SNIFF_TPDU:
|
||||
printf("TPDU");
|
||||
break;
|
||||
default:
|
||||
printf("???");
|
||||
break;
|
||||
}
|
||||
if (data->flags) {
|
||||
printf(" (");
|
||||
print_flags(data_flags, ARRAY_SIZE(data_flags), data->flags);
|
||||
printf(")");
|
||||
}
|
||||
printf(": ");
|
||||
uint16_t i;
|
||||
for (i = 0; i < data->length; i++) {
|
||||
printf("%02x ", data->data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
/* Send message as GSNTAP */
|
||||
switch (type) {
|
||||
case SIMTRACE_MSGT_SNIFF_ATR:
|
||||
osmo_st2_gsmtap_send_apdu(GSMTAP_SIM_ATR, data->data, data->length);
|
||||
break;
|
||||
case SIMTRACE_MSGT_SNIFF_TPDU:
|
||||
/* TPDU is now considered as APDU since SIMtrace sends complete TPDU */
|
||||
osmo_st2_gsmtap_send_apdu(GSMTAP_SIM_APDU, data->data, data->length);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! \brief Process an incoming message from the SIMtrace2 */
|
||||
static int process_usb_msg(const uint8_t *buf, int len)
|
||||
{
|
||||
/* check if enough data for the header is present */
|
||||
if (len < sizeof(struct simtrace_msg_hdr)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* check if message is complete */
|
||||
struct simtrace_msg_hdr *msg_hdr = (struct simtrace_msg_hdr *)buf;
|
||||
if (len < msg_hdr->msg_len) {
|
||||
return 0;
|
||||
}
|
||||
//printf("msg: %s\n", osmo_hexdump(buf, msg_hdr->msg_len));
|
||||
|
||||
/* check for message class */
|
||||
if (SIMTRACE_MSGC_SNIFF != msg_hdr->msg_class) { /* we only care about sniffing messages */
|
||||
return msg_hdr->msg_len; /* discard non-sniffing messaged */
|
||||
}
|
||||
|
||||
/* process sniff message payload */
|
||||
buf += sizeof(struct simtrace_msg_hdr);
|
||||
len -= sizeof(struct simtrace_msg_hdr);
|
||||
switch (msg_hdr->msg_type) {
|
||||
case SIMTRACE_MSGT_SNIFF_CHANGE:
|
||||
process_change(buf, len);
|
||||
break;
|
||||
case SIMTRACE_MSGT_SNIFF_FIDI:
|
||||
process_fidi(buf, len);
|
||||
break;
|
||||
case SIMTRACE_MSGT_SNIFF_ATR:
|
||||
case SIMTRACE_MSGT_SNIFF_PPS:
|
||||
case SIMTRACE_MSGT_SNIFF_TPDU:
|
||||
process_data(msg_hdr->msg_type, buf, len);
|
||||
break;
|
||||
default:
|
||||
printf("unknown SIMtrace msg type 0x%02x\n", msg_hdr->msg_type);
|
||||
break;
|
||||
}
|
||||
|
||||
return msg_hdr->msg_len;
|
||||
}
|
||||
|
||||
/*! Transport to SIMtrace device (e.g. USB handle) */
|
||||
static struct st_transport _transp;
|
||||
|
||||
static void run_mainloop()
|
||||
{
|
||||
int rc;
|
||||
uint8_t buf[16*256];
|
||||
unsigned int i, buf_i = 0;
|
||||
int xfer_len;
|
||||
|
||||
printf("Entering main loop\n");
|
||||
|
||||
while (true) {
|
||||
/* read data from SIMtrace2 device (via USB) */
|
||||
rc = libusb_bulk_transfer(_transp.usb_devh, _transp.usb_ep.in,
|
||||
&buf[buf_i], sizeof(buf)-buf_i, &xfer_len, 100000);
|
||||
if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
|
||||
rc != LIBUSB_ERROR_INTERRUPTED &&
|
||||
rc != LIBUSB_ERROR_IO) {
|
||||
fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
|
||||
return;
|
||||
}
|
||||
/* dispatch any incoming data */
|
||||
if (xfer_len > 0) {
|
||||
//printf("URB: %s\n", osmo_hexdump(&buf[buf_i], xfer_len));
|
||||
buf_i += xfer_len;
|
||||
if (buf_i >= sizeof(buf)) {
|
||||
perror("preventing USB buffer overflow");
|
||||
return;
|
||||
}
|
||||
int processed;
|
||||
while ((processed = process_usb_msg(buf, buf_i)) > 0) {
|
||||
if (processed > buf_i) {
|
||||
break;
|
||||
}
|
||||
for (i = processed; i < buf_i; i++) {
|
||||
buf[i-processed] = buf[i];
|
||||
}
|
||||
buf_i -= processed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void print_welcome(void)
|
||||
{
|
||||
printf("simtrace2-sniff - Phone-SIM card communication sniffer \n"
|
||||
"(C) 2010-2017 by Harald Welte <laforge@gnumonks.org>\n"
|
||||
"(C) 2018 by Kevin Redon <kredon@sysmocom.de>\n"
|
||||
"\n"
|
||||
);
|
||||
}
|
||||
|
||||
static void print_help(void)
|
||||
{
|
||||
printf(
|
||||
"\t-h\t--help\n"
|
||||
"\t-i\t--gsmtap-ip\tA.B.C.D\n"
|
||||
"\t-k\t--keep-running\n"
|
||||
"\t-V\t--usb-vendor\tVENDOR_ID\n"
|
||||
"\t-P\t--usb-product\tPRODUCT_ID\n"
|
||||
"\t-C\t--usb-config\tCONFIG_ID\n"
|
||||
"\t-I\t--usb-interface\tINTERFACE_ID\n"
|
||||
"\t-S\t--usb-altsetting ALTSETTING_ID\n"
|
||||
"\t-A\t--usb-address\tADDRESS\n"
|
||||
"\n"
|
||||
);
|
||||
}
|
||||
|
||||
static const struct option opts[] = {
|
||||
{ "help", 0, 0, 'h' },
|
||||
{ "gsmtap-ip", 1, 0, 'i' },
|
||||
{ "keep-running", 0, 0, 'k' },
|
||||
{ "usb-vendor", 1, 0, 'V' },
|
||||
{ "usb-product", 1, 0, 'P' },
|
||||
{ "usb-config", 1, 0, 'C' },
|
||||
{ "usb-interface", 1, 0, 'I' },
|
||||
{ "usb-altsetting", 1, 0, 'S' },
|
||||
{ "usb-address", 1, 0, 'A' },
|
||||
{ NULL, 0, 0, 0 }
|
||||
};
|
||||
|
||||
/* Known USB device with SIMtrace firmware supporting sniffer */
|
||||
static const struct dev_id compatible_dev_ids[] = {
|
||||
{ USB_VENDOR_OPENMOKO, USB_PRODUCT_SIMTRACE2 },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
static void signal_handler(int signal)
|
||||
{
|
||||
switch (signal) {
|
||||
case SIGINT:
|
||||
exit(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, rc, ret;
|
||||
print_welcome();
|
||||
|
||||
/* Parse arguments */
|
||||
char *gsmtap_host = "127.0.0.1";
|
||||
int keep_running = 0;
|
||||
int vendor_id = -1, product_id = -1, addr = -1, config_id = -1, if_num = -1, altsetting = -1;
|
||||
|
||||
while (1) {
|
||||
int option_index = 0;
|
||||
|
||||
char c = getopt_long(argc, argv, "hi:kV:P:C:I:S:A:", opts, &option_index);
|
||||
if (c == -1)
|
||||
break;
|
||||
switch (c) {
|
||||
case 'h':
|
||||
print_help();
|
||||
exit(0);
|
||||
break;
|
||||
case 'i':
|
||||
gsmtap_host = optarg;
|
||||
break;
|
||||
case 'k':
|
||||
keep_running = 1;
|
||||
break;
|
||||
case 'V':
|
||||
vendor_id = strtol(optarg, NULL, 16);
|
||||
break;
|
||||
case 'P':
|
||||
product_id = strtol(optarg, NULL, 16);
|
||||
break;
|
||||
case 'C':
|
||||
config_id = atoi(optarg);
|
||||
break;
|
||||
case 'I':
|
||||
if_num = atoi(optarg);
|
||||
break;
|
||||
case 'S':
|
||||
altsetting = atoi(optarg);
|
||||
break;
|
||||
case 'A':
|
||||
addr = atoi(optarg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Scan for available SIMtrace USB devices supporting sniffing */
|
||||
rc = libusb_init(NULL);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "libusb initialization failed\n");
|
||||
goto do_exit;
|
||||
}
|
||||
struct usb_interface_match ifm_scan[16];
|
||||
int num_interfaces = usb_match_interfaces(NULL, compatible_dev_ids,
|
||||
USB_CLASS_PROPRIETARY, SIMTRACE_SNIFFER_USB_SUBCLASS, -1, ifm_scan, ARRAY_SIZE(ifm_scan));
|
||||
if (num_interfaces <= 0) {
|
||||
perror("No compatible USB devices found");
|
||||
goto do_exit;
|
||||
}
|
||||
|
||||
/* Only keep USB matching arguments */
|
||||
struct usb_interface_match ifm_filtered[ARRAY_SIZE(ifm_scan)];
|
||||
int num_filtered = 0;
|
||||
for (i = 0; i < num_interfaces; i++) {
|
||||
if (vendor_id>=0 && vendor_id!=ifm_scan[i].vendor) {
|
||||
continue;
|
||||
}
|
||||
if (product_id>=0 && product_id!=ifm_scan[i].product) {
|
||||
continue;
|
||||
}
|
||||
if (config_id>=0 && config_id!=ifm_scan[i].configuration) {
|
||||
continue;
|
||||
}
|
||||
if (if_num>=0 && if_num!=ifm_scan[i].interface) {
|
||||
continue;
|
||||
}
|
||||
if (altsetting>=0 && altsetting!=ifm_scan[i].altsetting) {
|
||||
continue;
|
||||
}
|
||||
if (addr>=0 && addr!=ifm_scan[i].addr) {
|
||||
continue;
|
||||
}
|
||||
ifm_filtered[num_filtered++] = ifm_scan[i];
|
||||
}
|
||||
if (1!=num_filtered) {
|
||||
perror("No individual matching USB devices found");
|
||||
printf("Available USB devices:\n");
|
||||
for (i = 0; i < num_interfaces; i++) {
|
||||
printf("\t%04x:%04x Addr=%u, Path=%s, Cfg=%u, Intf=%u, Alt=%u: %d/%d/%d ",
|
||||
ifm_scan[i].vendor, ifm_scan[i].product, ifm_scan[i].addr, ifm_scan[i].path,
|
||||
ifm_scan[i].configuration, ifm_scan[i].interface, ifm_scan[i].altsetting,
|
||||
ifm_scan[i].class, ifm_scan[i].sub_class, ifm_scan[i].protocol);
|
||||
libusb_device_handle *dev_handle;
|
||||
rc = libusb_open(ifm_scan[i].usb_dev, &dev_handle);
|
||||
if (rc < 0) {
|
||||
printf("\n");
|
||||
perror("Cannot open device");
|
||||
continue;
|
||||
}
|
||||
char strbuf[256];
|
||||
rc = libusb_get_string_descriptor_ascii(dev_handle, ifm_scan[i].string_idx,
|
||||
(unsigned char *)strbuf, sizeof(strbuf));
|
||||
libusb_close(dev_handle);
|
||||
if (rc < 0) {
|
||||
printf("\n");
|
||||
perror("Cannot read string");
|
||||
continue;
|
||||
}
|
||||
printf("(%s)\n", strbuf);
|
||||
}
|
||||
goto do_exit;
|
||||
}
|
||||
struct usb_interface_match ifm_selected = ifm_filtered[0];
|
||||
printf("Using USB device %04x:%04x Addr=%u, Path=%s, Cfg=%u, Intf=%u, Alt=%u: %d/%d/%d ",
|
||||
ifm_selected.vendor, ifm_selected.product, ifm_selected.addr, ifm_selected.path,
|
||||
ifm_selected.configuration, ifm_selected.interface, ifm_selected.altsetting,
|
||||
ifm_selected.class, ifm_selected.sub_class, ifm_selected.protocol);
|
||||
libusb_device_handle *dev_handle;
|
||||
rc = libusb_open(ifm_selected.usb_dev, &dev_handle);
|
||||
if (rc < 0) {
|
||||
printf("\n");
|
||||
perror("Cannot open device");
|
||||
}
|
||||
char strbuf[256];
|
||||
rc = libusb_get_string_descriptor_ascii(dev_handle, ifm_selected.string_idx,
|
||||
(unsigned char *)strbuf, sizeof(strbuf));
|
||||
libusb_close(dev_handle);
|
||||
if (rc < 0) {
|
||||
printf("\n");
|
||||
perror("Cannot read string");
|
||||
}
|
||||
printf("(%s)\n", strbuf);
|
||||
|
||||
rc = osmo_st2_gsmtap_init(gsmtap_host);
|
||||
if (rc < 0) {
|
||||
perror("unable to open GSMTAP");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
signal(SIGINT, &signal_handler);
|
||||
|
||||
do {
|
||||
_transp.usb_devh = usb_open_claim_interface(NULL, &ifm_selected);
|
||||
if (!_transp.usb_devh) {
|
||||
fprintf(stderr, "can't open USB device\n");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
rc = libusb_claim_interface(_transp.usb_devh, ifm_selected.interface);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "can't claim interface %d; rc=%d\n", ifm_selected.interface, rc);
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
rc = get_usb_ep_addrs(_transp.usb_devh, ifm_selected.interface, &_transp.usb_ep.out,
|
||||
&_transp.usb_ep.in, &_transp.usb_ep.irq_in);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
run_mainloop();
|
||||
ret = 0;
|
||||
|
||||
if (_transp.usb_devh)
|
||||
libusb_release_interface(_transp.usb_devh, 0);
|
||||
close_exit:
|
||||
if (_transp.usb_devh)
|
||||
libusb_close(_transp.usb_devh);
|
||||
if (keep_running)
|
||||
sleep(1);
|
||||
} while (keep_running);
|
||||
|
||||
libusb_exit(NULL);
|
||||
do_exit:
|
||||
return ret;
|
||||
}
|
||||
89
host/src/simtrace2_usb.c
Normal file
89
host/src/simtrace2_usb.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/* simtrace2_usb - host PC application to list found SIMtrace 2 USB devices
|
||||
*
|
||||
* (C) 2016 by Harald Welte <hwelte@hmw-consulting.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <osmocom/core/utils.h>
|
||||
|
||||
#include <osmocom/simtrace2/libusb_util.h>
|
||||
#include <osmocom/simtrace2/simtrace_usb.h>
|
||||
|
||||
static const struct dev_id compatible_dev_ids[] = {
|
||||
{ USB_VENDOR_OPENMOKO, USB_PRODUCT_OWHW_SAM3 },
|
||||
{ USB_VENDOR_OPENMOKO, USB_PRODUCT_QMOD_SAM3 },
|
||||
{ USB_VENDOR_OPENMOKO, USB_PRODUCT_SIMTRACE2 },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
static int find_devices(void)
|
||||
{
|
||||
struct usb_interface_match ifm[16];
|
||||
int rc, i, num_interfaces;
|
||||
|
||||
/* scan for USB devices matching SIMtrace USB ID with proprietary class */
|
||||
rc = usb_match_interfaces(NULL, compatible_dev_ids,
|
||||
USB_CLASS_PROPRIETARY, -1, -1, ifm, ARRAY_SIZE(ifm));
|
||||
printf("USB matches: %d\n", rc);
|
||||
if (rc < 0)
|
||||
return rc;
|
||||
num_interfaces = rc;
|
||||
|
||||
for (i = 0; i < num_interfaces; i++) {
|
||||
struct usb_interface_match *m = &ifm[i];
|
||||
libusb_device_handle *dev_handle;
|
||||
char strbuf[256];
|
||||
|
||||
printf("\t%04x:%04x Addr=%u, Path=%s, Cfg=%u, Intf=%u, Alt=%u: %d/%d/%d ",
|
||||
m->vendor, m->product, m->addr, m->path,
|
||||
m->configuration, m->interface, m->altsetting,
|
||||
m->class, m->sub_class, m->protocol);
|
||||
|
||||
rc = libusb_open(m->usb_dev, &dev_handle);
|
||||
if (rc < 0) {
|
||||
printf("\n");
|
||||
perror("Cannot open device");
|
||||
continue;
|
||||
}
|
||||
rc = libusb_get_string_descriptor_ascii(dev_handle, m->string_idx,
|
||||
(unsigned char *)strbuf, sizeof(strbuf));
|
||||
libusb_close(dev_handle);
|
||||
if (rc < 0) {
|
||||
printf("\n");
|
||||
perror("Cannot read string");
|
||||
continue;
|
||||
}
|
||||
printf("(%s)\n", strbuf);
|
||||
#if 0
|
||||
dev_handle = usb_open_claim_interface(NULL, m);
|
||||
printf("dev_handle=%p\n", dev_handle);
|
||||
libusb_close(dev_handle);
|
||||
#endif
|
||||
}
|
||||
|
||||
return num_interfaces;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
libusb_init(NULL);
|
||||
find_devices();
|
||||
return 0;
|
||||
}
|
||||
285
host/src/usb2udp.c
Normal file
285
host/src/usb2udp.c
Normal file
@@ -0,0 +1,285 @@
|
||||
/* simtrace - main program for the host PC
|
||||
*
|
||||
* (C) 2010-2016 by Harald Welte <hwelte@hmw-consulting.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
#define _GNU_SOURCE
|
||||
#include <getopt.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <libusb.h>
|
||||
|
||||
#include <osmocom/simtrace2/simtrace_usb.h>
|
||||
#include <osmocom/simtrace2/simtrace_prot.h>
|
||||
#include "simtrace2-discovery.h"
|
||||
|
||||
#include <osmocom/core/utils.h>
|
||||
#include <osmocom/core/socket.h>
|
||||
#include <osmocom/core/select.h>
|
||||
|
||||
struct libusb_device_handle *g_devh;
|
||||
static struct sockaddr_in g_sa_remote;
|
||||
static struct osmo_fd g_udp_ofd;
|
||||
|
||||
static void print_welcome(void)
|
||||
{
|
||||
printf("usb2udp - UDP/IP forwarding of SIMtrace card emulation\n"
|
||||
"(C) 2016 by Harald Welte <laforge@gnumonks.org>\n\n");
|
||||
}
|
||||
|
||||
static void print_help(void)
|
||||
{
|
||||
printf( "\t-h\t--help\n"
|
||||
"\t-i\t--interface <0-255>\n"
|
||||
"\n"
|
||||
);
|
||||
}
|
||||
|
||||
struct ep_buf {
|
||||
uint8_t ep;
|
||||
uint8_t buf[1024];
|
||||
struct libusb_transfer *xfer;
|
||||
};
|
||||
static struct ep_buf g_buf_in;
|
||||
static struct ep_buf g_buf_out;
|
||||
|
||||
static void usb_in_xfer_cb(struct libusb_transfer *xfer)
|
||||
{
|
||||
int rc;
|
||||
|
||||
printf("xfer_cb(ep=%02x): status=%d, flags=0x%x, type=%u, len=%u, act_len=%u\n",
|
||||
xfer->endpoint, xfer->status, xfer->flags, xfer->type, xfer->length, xfer->actual_length);
|
||||
switch (xfer->status) {
|
||||
case LIBUSB_TRANSFER_COMPLETED:
|
||||
if (xfer->endpoint == g_buf_in.ep) {
|
||||
/* process the data */
|
||||
printf("read %d bytes from SIMTRACE, forwarding to UDP\n", xfer->actual_length);
|
||||
rc = sendto(g_udp_ofd.fd, xfer->buffer, xfer->actual_length, 0, (struct sockaddr *)&g_sa_remote, sizeof(g_sa_remote));
|
||||
if (rc <= 0) {
|
||||
fprintf(stderr, "error writing to UDP\n");
|
||||
}
|
||||
/* and re-submit the URB */
|
||||
libusb_submit_transfer(xfer);
|
||||
} else if (xfer->endpoint == g_buf_out.ep) {
|
||||
/* re-enable reading from the UDP side */
|
||||
g_udp_ofd.when |= BSC_FD_READ;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "xfer_cb(ERROR '%s')\n", osmo_hexdump_nospc(xfer->buffer, xfer->actual_length));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void init_ep_buf(struct ep_buf *epb)
|
||||
{
|
||||
if (!epb->xfer)
|
||||
epb->xfer = libusb_alloc_transfer(0);
|
||||
|
||||
epb->xfer->flags = 0;
|
||||
|
||||
libusb_fill_bulk_transfer(epb->xfer, g_devh, epb->ep, epb->buf, sizeof(epb->buf), usb_in_xfer_cb, NULL, 0);
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* libosmocore main loop integration of libusb async I/O
|
||||
***********************************************************************/
|
||||
|
||||
static int g_libusb_pending = 0;
|
||||
|
||||
static int ofd_libusb_cb(struct osmo_fd *ofd, unsigned int what)
|
||||
{
|
||||
/* FIXME */
|
||||
g_libusb_pending = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* call-back when libusb adds a FD */
|
||||
static void libusb_fd_added_cb(int fd, short events, void *user_data)
|
||||
{
|
||||
struct osmo_fd *ofd = talloc_zero(NULL, struct osmo_fd);
|
||||
|
||||
printf("%s(%u, %x)\n", __func__, fd, events);
|
||||
|
||||
ofd->fd = fd;
|
||||
ofd->cb = &ofd_libusb_cb;
|
||||
if (events & POLLIN)
|
||||
ofd->when |= BSC_FD_READ;
|
||||
if (events & POLLOUT)
|
||||
ofd->when |= BSC_FD_WRITE;
|
||||
|
||||
osmo_fd_register(ofd);
|
||||
}
|
||||
|
||||
/* call-back when libusb removes a FD */
|
||||
static void libusb_fd_removed_cb(int fd, void *user_data)
|
||||
{
|
||||
|
||||
printf("%s(%u)\n", __func__, fd);
|
||||
#if 0
|
||||
struct osmo_fd *ofd;
|
||||
/* FIXME: This needs new export in libosmocore! */
|
||||
ofd = osmo_fd_get_by_fd(fd);
|
||||
|
||||
if (ofd) {
|
||||
osmo_fd_unregister(ofd);
|
||||
talloc_free(ofd);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* call-back when the UDP socket is readable */
|
||||
static int ofd_udp_cb(struct osmo_fd *ofd, unsigned int what)
|
||||
{
|
||||
int rc;
|
||||
socklen_t addrlen = sizeof(g_sa_remote);
|
||||
|
||||
rc = recvfrom(ofd->fd, g_buf_out.buf, sizeof(g_buf_out.buf), 0,
|
||||
(struct sockaddr *)&g_sa_remote, &addrlen);
|
||||
if (rc <= 0) {
|
||||
fprintf(stderr, "error reading from UDP\n");
|
||||
return 0;
|
||||
}
|
||||
printf("read %d bytes from UDP, forwarding to SIMTRACE\n", rc);
|
||||
g_buf_out.xfer->length = rc;
|
||||
|
||||
/* disable further READ interest for the UDP socket */
|
||||
ofd->when &= ~BSC_FD_READ;
|
||||
|
||||
/* submit the URB on the OUT end point */
|
||||
libusb_submit_transfer(g_buf_out.xfer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void run_mainloop(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
printf("Entering main loop\n");
|
||||
|
||||
while (1) {
|
||||
osmo_select_main(0);
|
||||
if (g_libusb_pending) {
|
||||
struct timeval tv;
|
||||
memset(&tv, 0, sizeof(tv));
|
||||
rc = libusb_handle_events_timeout_completed(NULL, &tv, NULL);
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "handle_events_timeout_completed == %d\n", rc);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int rc;
|
||||
int c, ret = 1;
|
||||
int local_udp_port = 52342;
|
||||
unsigned int if_num = 0;
|
||||
|
||||
print_welcome();
|
||||
|
||||
while (1) {
|
||||
int option_index = 0;
|
||||
static const struct option opts[] = {
|
||||
{ "udp-port", 1, 0, 'u' },
|
||||
{ "interface", 1, 0, 'I' },
|
||||
{ "help", 0, 0, 'h' },
|
||||
{ NULL, 0, 0, 0 }
|
||||
};
|
||||
|
||||
c = getopt_long(argc, argv, "u:I:h", opts, &option_index);
|
||||
if (c == -1)
|
||||
break;
|
||||
switch (c) {
|
||||
case 'u':
|
||||
local_udp_port = atoi(optarg);
|
||||
break;
|
||||
case 'I':
|
||||
if_num = atoi(optarg);
|
||||
break;
|
||||
case 'h':
|
||||
print_help();
|
||||
exit(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
rc = libusb_init(NULL);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "libusb initialization failed\n");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
libusb_set_pollfd_notifiers(NULL, &libusb_fd_added_cb, &libusb_fd_removed_cb, NULL);
|
||||
|
||||
g_devh = libusb_open_device_with_vid_pid(NULL, USB_VENDOR_OPENMOKO, USB_PRODUCT_OWHW_SAM3);
|
||||
if (!g_devh) {
|
||||
fprintf(stderr, "can't open USB device\n");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
rc = libusb_claim_interface(g_devh, if_num);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "can't claim interface %u; rc=%d\n", if_num, rc);
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
/* open UDP socket, register with select handling and mark it
|
||||
* readable */
|
||||
g_udp_ofd.cb = ofd_udp_cb;
|
||||
osmo_sock_init_ofd(&g_udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, local_udp_port + if_num, OSMO_SOCK_F_BIND);
|
||||
|
||||
rc = get_usb_ep_addrs(g_devh, if_num, &g_buf_out.ep, &g_buf_in.ep, NULL);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "couldn't find enpdoint addresses; rc=%d\n", rc);
|
||||
goto close_exit;
|
||||
}
|
||||
/* initialize USB buffers / transfers */
|
||||
init_ep_buf(&g_buf_out);
|
||||
init_ep_buf(&g_buf_in);
|
||||
|
||||
/* submit the first transfer for the IN endpoint */
|
||||
libusb_submit_transfer(g_buf_in.xfer);
|
||||
|
||||
run_mainloop();
|
||||
|
||||
ret = 0;
|
||||
|
||||
libusb_release_interface(g_devh, 0);
|
||||
close_exit:
|
||||
if (g_devh)
|
||||
libusb_close(g_devh);
|
||||
|
||||
libusb_exit(NULL);
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user