Skip to content

Commit

Permalink
src/whsniff.c: add -k option to keep original FCS
Browse files Browse the repository at this point in the history
Allow the user to keep the original FCS sent by the CC2531 which
contains the RSSI and LQI. These values can be interpreted by wireshark
as a "TI CC24xx FCS format"

Reason: Add -k option to keep original FCS
Author: Fabrice Fontaine
Ref: 698285
License: same as original file

698285 by Fabrice Fontaine: Patch for whsniff (Zigbee sniffer)

Signed-off-by: Fabrice Fontaine <[email protected]>
  • Loading branch information
ffontaine committed Nov 6, 2020
1 parent 76bb8cb commit dfc3c3d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
4 changes: 4 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ $ path/to/whsniff -c channel_number > /tmp/pipes/whsniff
```sh
$ path/to/whsniff -c channel_number > filename.pcap
```
* You can also keep the original FCS sent by the CC2531 through the -k option. The original FCS contains the RSSI and LQI. It can be interpreted by wireshark as a "TI CC24xx FCS format":
```sh
$ path/to/whsniff -k -c channel_number > /tmp/pipes/whsniff
```
* If you see something like `libusb: error [_get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/001/006: Permission denied` you can use `udev`

1. Add below contents to `/etc/udev/rules.d/54-cc2531.rules`
Expand Down
35 changes: 22 additions & 13 deletions src/whsniff.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static uint16_t ieee802154_crc16(uint8_t *tvb, uint32_t offset, uint32_t len);


//--------------------------------------------
static int packet_handler(unsigned char *buf, int cnt)
static int packet_handler(unsigned char *buf, int cnt, uint8_t keep_original_fcs)
{
usb_header_type *usb_header;
usb_data_header_type *usb_data_header;
Expand Down Expand Up @@ -162,7 +162,6 @@ static int packet_handler(unsigned char *buf, int cnt)
pcaprec_hdr.orig_len = (uint32_t)usb_data_header->wpan_len;

fwrite(&pcaprec_hdr, sizeof(pcaprec_hdr), 1, stdout);
fwrite(&buf[sizeof(usb_data_header_type)], 1, usb_data_header->wpan_len - 2, stdout);

// SmartRF™ Packet Sniffer User’s Manual (SWRU187G)
// FCS:
Expand All @@ -172,15 +171,21 @@ static int packet_handler(unsigned char *buf, int cnt)
// Bit 6-0: If Correlation used: Correlation value.
// If Correlation not used: LQI.

fcs = 0;
if (buf[sizeof(usb_data_header_type) + usb_data_header->wpan_len - 1] & 0x80)
if (keep_original_fcs)
fwrite(&buf[sizeof(usb_data_header_type)], 1, usb_data_header->wpan_len, stdout);
else
{
// CRC OK
fcs = ieee802154_crc16((uint8_t *)&buf[sizeof(usb_data_header_type)], 0, usb_data_header->wpan_len - 2);
}
le_fcs = htole16(fcs);
fwrite(&buf[sizeof(usb_data_header_type)], 1, usb_data_header->wpan_len - 2, stdout);
fcs = 0;
if (buf[sizeof(usb_data_header_type) + usb_data_header->wpan_len - 1] & 0x80)
{
// CRC OK
fcs = ieee802154_crc16((uint8_t *)&buf[sizeof(usb_data_header_type)], 0, usb_data_header->wpan_len - 2);
}
le_fcs = htole16(fcs);

fwrite(&le_fcs, sizeof(le_fcs), 1, stdout);
fwrite(&le_fcs, sizeof(le_fcs), 1, stdout);
}
fflush(stdout);

break;
Expand Down Expand Up @@ -209,7 +214,7 @@ void signal_handler(int sig)
//--------------------------------------------
void print_usage()
{
printf("Usage: whsniff -c channel\n");
printf("Usage: whsniff -c channel [-k]\n");
}

//--------------------------------------------
Expand All @@ -219,6 +224,7 @@ int main(int argc, char *argv[])
libusb_device_handle *handle;
libusb_device *dev;
uint8_t channel;
uint8_t keep_original_fcs = 0;
int option;
static unsigned char usb_buf[BUF_SIZE];
static int usb_cnt;
Expand All @@ -232,14 +238,14 @@ int main(int argc, char *argv[])
// pipe closed
signal(SIGPIPE, signal_handler);

if (argc != 3)
if (argc != 3 && argc != 4)
{
print_usage();
exit(EXIT_FAILURE);
}

option = 0;
while ((option = getopt(argc, argv, "c:")) != -1)
while ((option = getopt(argc, argv, "c:k")) != -1)
{
switch (option)
{
Expand All @@ -251,6 +257,9 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
break;
case 'k':
keep_original_fcs = 1;
break;
default:
print_usage();
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -379,7 +388,7 @@ int main(int argc, char *argv[])

for (;;)
{
res = packet_handler(&recv_buf[0], recv_cnt);
res = packet_handler(&recv_buf[0], recv_cnt, keep_original_fcs);
if (res < 0)
break;
recv_cnt -= res;
Expand Down

0 comments on commit dfc3c3d

Please sign in to comment.