This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
Kévin Redon gerrit-no-reply at lists.osmocom.orgKévin Redon has uploaded this change for review. ( https://gerrit.osmocom.org/9878
Change subject: sniffing: move conversion convertion and flag processing from ISR to main loop to keep ISR fast and focus on data capture
......................................................................
sniffing: move conversion convertion and flag processing from ISR to main loop to keep ISR fast and focus on data capture
Change-Id: Ieefa8a5f81dbcc12c1ad3059660dbffa0c1a4961
---
M firmware/libcommon/source/sniffer.c
M host/simtrace2-sniff.c
2 files changed, 98 insertions(+), 73 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/simtrace2 refs/changes/78/9878/1
diff --git a/firmware/libcommon/source/sniffer.c b/firmware/libcommon/source/sniffer.c
index fbf0022..441df7d 100644
--- a/firmware/libcommon/source/sniffer.c
+++ b/firmware/libcommon/source/sniffer.c
@@ -19,8 +19,7 @@
*/
/* This code implement the Sniffer mode to sniff the communication between a SIM card (or any ISO 7816 smart card) and a phone (or any ISO 7816 card reader).
* For historical reasons (i.e. SIMtrace hardware) the USART peripheral connected to the SIM card is used.
- * TODO handle RST, PTS, and send data over USB
- * TODO put common ISO7816-3 code is separate library (and combine clean with iso7816_4)
+ * TODO put common ISO7816-3 code is separate library (and combine clean with iso7816_4)
*/
#include "board.h"
#include "simtrace.h"
@@ -188,35 +187,6 @@
*/
static const uint8_t convention_convert_lut[256] = { 0xff, 0x7f, 0xbf, 0x3f, 0xdf, 0x5f, 0x9f, 0x1f, 0xef, 0x6f, 0xaf, 0x2f, 0xcf, 0x4f, 0x8f, 0x0f, 0xf7, 0x77, 0xb7, 0x37, 0xd7, 0x57, 0x97, 0x17, 0xe7, 0x67, 0xa7, 0x27, 0xc7, 0x47, 0x87, 0x07, 0xfb, 0x7b, 0xbb, 0x3b, 0xdb, 0x5b, 0x9b, 0x1b, 0xeb, 0x6b, 0xab, 0x2b, 0xcb, 0x4b, 0x8b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 0xe8, 0x68, 0xa8, 0x28, 0xc8, 0x48, 0x88, 0x08, 0xf0, 0x70, 0xb0, 0x30, 0xd0, 0x50, 0x90, 0x10, 0xe0, 0x60, 0xa0, 0x20, 0xc0, 0x40, 0x80, 0x00, };
-/*! Send card change flags over USB
- * @param[in] flags change flags corresponding to SIMTRACE_MSGT_SNIFF_CHANGE
- */
-static void usb_send_change(uint32_t flags)
-{
- /* Check flags */
- if(0==flags) { /* no changes */
- return;
- }
-
- /* Send message over USB */
- struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN);
- if (!usb_msg) {
- return;
- }
- struct simtrace_msg_hdr *usb_msg_header;
- usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header));
- usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;
- memset(usb_msg_header, 0, sizeof(*usb_msg_header));
- usb_msg_header->msg_class = SIMTRACE_MSGC_SNIFF;
- usb_msg_header->msg_type = SIMTRACE_MSGT_SNIFF_CHANGE;
- usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header);
- struct sniff_change *usb_sniff_change;
- usb_sniff_change = (struct sniff_change *) msgb_put(usb_msg, sizeof(*usb_sniff_change));
- usb_sniff_change->flags = flags;
- usb_msg_header->msg_len = msgb_length(usb_msg);
- usb_buf_submit(usb_msg);
-}
-
/*! Update the ISO 7816-3 state
* @param[in] iso_state_new new ISO 7816-3 state to update to
*/
@@ -232,11 +202,11 @@
switch (iso_state_new) {
case ISO7816_S_RESET:
update_fidi(sniff_usart.base, 0x11); /* reset baud rate to default Di/Fi values */
- usb_send_change(SNIFF_CHANGE_FLAG_RESET_HOLD); /* send reset change to host software over USB */
+ change_flags |= SNIFF_CHANGE_FLAG_RESET_HOLD; /* set flag and let main loop send it */
break;
case ISO7816_S_WAIT_ATR:
rbuf_reset(&sniff_buffer); /* reset buffer for new communication */
- usb_send_change(SNIFF_CHANGE_FLAG_RESET_RELEASE); /* send reset change to host software over USB */
+ change_flags |= SNIFF_CHANGE_FLAG_RESET_RELEASE; /* set flag and let main loop send it */
break;
case ISO7816_S_IN_ATR:
atr_i = 0;
@@ -714,42 +684,34 @@
void Sniffer_usart_isr(void)
{
/* Read channel status register */
- uint32_t csr = sniff_usart.base->US_CSR & sniff_usart.base->US_IMR;
+ uint32_t csr = sniff_usart.base->US_CSR;
+ /* Verify if there was an error */
+ if (csr & US_CSR_OVRE) {
+ TRACE_WARNING("USART overrun error\n\r");
+ sniff_usart.base->US_CR |= US_CR_RSTSTA;
+ }
+ if (csr & US_CSR_FRAME) {
+ TRACE_WARNING("USART framing error\n\r");
+ sniff_usart.base->US_CR |= US_CR_RSTSTA;
+ }
/* Verify if character has been received */
if (csr & US_CSR_RXRDY) {
/* Read communication data byte between phone and SIM */
uint8_t byte = sniff_usart.base->US_RHR;
- /* Convert convention if required */
- if (convention_convert) {
- byte = convention_convert_lut[byte];
- }
/* Store sniffed data into buffer (also clear interrupt */
- rbuf_write(&sniff_buffer, byte);
+ if (rbuf_is_full(&sniff_buffer)) {
+ TRACE_ERROR("USART buffer full\n\r");
+ } else {
+ rbuf_write(&sniff_buffer, byte);
+ }
}
+
/* Verify it WT timeout occurred, to detect unresponsive card */
if (csr & US_CSR_TIMEOUT) {
/* Stop timeout until next character is received */
sniff_usart.base->US_CR |= US_CR_STTTO;
- /* Use timeout to detect end of ATR/PPS/TPDU */
- switch (iso_state) {
- case ISO7816_S_RESET:
- case ISO7816_S_WAIT_ATR:
- break;
- case ISO7816_S_IN_ATR:
- change_state(ISO7816_S_WAIT_ATR);
- break;
- case ISO7816_S_WAIT_TPDU:
- break;
- case ISO7816_S_WAIT_PPS_RSP:
- case ISO7816_S_IN_TPDU:
- case ISO7816_S_IN_PPS_REQ:
- case ISO7816_S_IN_PPS_RSP:
- change_state(ISO7816_S_WAIT_TPDU);
- break;
- default:
- break;
- }
- usb_send_change(SNIFF_CHANGE_FLAG_TIMEOUT_WT); /* send timeout to host software over USB */
+ /* Just set the flag and let the main loop handle it */
+ change_flags |= SNIFF_CHANGE_FLAG_TIMEOUT_WT;
}
}
@@ -853,6 +815,40 @@
}
}
+/*! Send card change flags over USB
+ * @param[in] flags change flags corresponding to SIMTRACE_MSGT_SNIFF_CHANGE
+ * @note Also print the TPDU over the debug console
+ */
+static void usb_send_change(uint32_t flags)
+{
+ /* Check flags */
+ if(0==flags) { /* no changes */
+ return;
+ }
+
+ if (flags&SNIFF_CHANGE_FLAG_TIMEOUT_WT) {
+ printf("waiting time (WT) timeout\n\r");
+ }
+
+ /* Send message over USB */
+ struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN);
+ if (!usb_msg) {
+ return;
+ }
+ struct simtrace_msg_hdr *usb_msg_header;
+ usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header));
+ usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;
+ memset(usb_msg_header, 0, sizeof(*usb_msg_header));
+ usb_msg_header->msg_class = SIMTRACE_MSGC_SNIFF;
+ usb_msg_header->msg_type = SIMTRACE_MSGT_SNIFF_CHANGE;
+ usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header);
+ struct sniff_change *usb_sniff_change;
+ usb_sniff_change = (struct sniff_change *) msgb_put(usb_msg, sizeof(*usb_sniff_change));
+ usb_sniff_change->flags = flags;
+ usb_msg_header->msg_len = msgb_length(usb_msg);
+ usb_buf_submit(usb_msg);
+}
+
/* Main (idle/busy) loop of this USB configuration */
void Sniffer_run(void)
{
@@ -871,7 +867,11 @@
/* Handle sniffed data */
if (!rbuf_is_empty(&sniff_buffer)) { /* use if instead of while to let the main loop restart the watchdog */
uint8_t byte = rbuf_read(&sniff_buffer);
- TRACE_DEBUG_WP("< 0x%02x\n\r", byte);
+ /* Convert convention if required */
+ if (convention_convert) {
+ byte = convention_convert_lut[byte];
+ }
+ //TRACE_ERROR_WP(">%02x", byte);
switch (iso_state) { /* Handle byte depending on state */
case ISO7816_S_RESET: /* During reset we shouldn't receive any data */
break;
@@ -908,6 +908,31 @@
/* Handle flags */
if (change_flags) { /* WARNING this is not synced with the data buffer handling */
+ if (change_flags&SNIFF_CHANGE_FLAG_TIMEOUT_WT) {
+ /* Use timeout to detect interrupted data transmission */
+ switch (iso_state) {
+ case ISO7816_S_IN_ATR:
+ usb_send_atr(false); /* send incomplete ATR to host software using USB */
+ change_state(ISO7816_S_WAIT_ATR);
+ break;
+ case ISO7816_S_IN_TPDU:
+ usb_send_tpdu(false); /* send incomplete PPS to host software using USB */
+ change_state(ISO7816_S_WAIT_TPDU);
+ break;
+ case ISO7816_S_IN_PPS_REQ:
+ case ISO7816_S_IN_PPS_RSP:
+ usb_send_pps(false); /* send incomplete TPDU to host software using USB */
+ change_state(ISO7816_S_WAIT_TPDU);
+ break;
+ default:
+ change_flags &= ~SNIFF_CHANGE_FLAG_TIMEOUT_WT; /* We don't care about the timeout is all other cases */
+ break;
+ }
+ }
+ if (change_flags) {
+ usb_send_change(change_flags); /* send timeout to host software over USB */
+ change_flags = 0; /* Reset flags */
+ }
}
}
#endif /* HAVE_SNIFFER */
diff --git a/host/simtrace2-sniff.c b/host/simtrace2-sniff.c
index df3e28a..bbee518 100644
--- a/host/simtrace2-sniff.c
+++ b/host/simtrace2-sniff.c
@@ -92,7 +92,7 @@
return 0;
}
-static int process_change(uint8_t *buf, int len)
+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)) {
@@ -100,9 +100,7 @@
}
struct sniff_change *change = (struct sniff_change *)buf;
- if (SNIFF_CHANGE_FLAG_TIMEOUT_WT!=change->flags) {
- printf("Card state change: ");
- }
+ printf("Card state change: ");
if (change->flags&SNIFF_CHANGE_FLAG_CARD_INSERT) {
printf("card inserted ");
}
@@ -116,11 +114,9 @@
printf("reset release ");
}
if (change->flags&SNIFF_CHANGE_FLAG_TIMEOUT_WT) {
- // do nothing since this also triggers on inactivity
+ printf("data transfer timeout ");
}
- if (SNIFF_CHANGE_FLAG_TIMEOUT_WT!=change->flags) {
- printf("\n");
- }
+ printf("\n");
return 0;
}
@@ -131,7 +127,7 @@
/* 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(uint8_t *buf, int len)
+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)) {
@@ -143,7 +139,7 @@
return 0;
}
-static int process_atr(uint8_t *buf, int len)
+static int process_atr(const uint8_t *buf, int len)
{
/* check if there is enough data for the structure */
if (len<sizeof(struct sniff_data)) {
@@ -164,7 +160,7 @@
return 0;
}
-static int process_pps(uint8_t *buf, int len)
+static int process_pps(const uint8_t *buf, int len)
{
/* check if there is enough data for the structure */
if (len<sizeof(struct sniff_data)) {
@@ -185,7 +181,7 @@
return 0;
}
-static int process_tpdu(uint8_t *buf, int len)
+static int process_tpdu(const uint8_t *buf, int len)
{
/* check if there is enough data for the structure */
if (len<sizeof(struct sniff_data)) {
@@ -198,16 +194,20 @@
return -2;
}
+ /* print TPDU */
printf("TPDU%s: ", tpdu->complete ? "" : " (incomplete)");
for (uint16_t i=0; i<tpdu->length; i++) {
printf("%02x ", tpdu->data[i]);
}
printf("\n");
+
+ /* send TPDU (now considered as APDU) to GSMTAP */
+ gsmtap_send_sim(tpdu->data, tpdu->length);
return 0;
}
/*! \brief Process an incoming message from the SIMtrace2 */
-static int process_usb_msg(uint8_t *buf, int len)
+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)) {
--
To view, visit https://gerrit.osmocom.org/9878
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: simtrace2
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ieefa8a5f81dbcc12c1ad3059660dbffa0c1a4961
Gerrit-Change-Number: 9878
Gerrit-PatchSet: 1
Gerrit-Owner: Kévin Redon <kredon at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180704/3ef2b3c9/attachment.htm>