From 275825dde0c4120135d1a563fd861d8bf141729a Mon Sep 17 00:00:00 2001 From: Marc Date: Thu, 13 Jan 2022 01:07:23 +0000 Subject: core: Fix `libwifi_frame_verify()` and clarify usage. --- src/libwifi/core/frame/crc.c | 6 ++++-- src/libwifi/core/frame/crc.h | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/libwifi/core/frame/crc.c b/src/libwifi/core/frame/crc.c index 33dae06..8e45c85 100644 --- a/src/libwifi/core/frame/crc.c +++ b/src/libwifi/core/frame/crc.c @@ -49,15 +49,17 @@ uint32_t libwifi_calculate_fcs(const unsigned char *frame, size_t frame_len) { } /* - * Verify a frame containing a FCS at the end to the FCS calculated + * Verify a raw frame containing a FCS at the end to the FCS calculated * by libwifi. */ int libwifi_frame_verify(void *frame, size_t frame_len) { - uint32_t oCRC = *((uint32_t *) ((char *) frame + frame_len)); + // A frame with a CRC will have the CRC placed at the end, and is 4 bytes long. + uint32_t oCRC = *((uint32_t *) ((char *) frame + (frame_len - 4))); uint32_t rCRC = libwifi_calculate_fcs(frame, frame_len); if (rCRC == oCRC) { return 1; } + return 0; } diff --git a/src/libwifi/core/frame/crc.h b/src/libwifi/core/frame/crc.h index aab1c4e..0f89383 100644 --- a/src/libwifi/core/frame/crc.h +++ b/src/libwifi/core/frame/crc.h @@ -38,7 +38,14 @@ uint32_t libwifi_crc32(const unsigned char *message, int message_len); uint32_t libwifi_calculate_fcs(const unsigned char *frame, size_t frame_len); /** - * Check if the given 802.11 frame has a valid FCS. + * Check if the given raw 802.11 frame has a valid FCS. + * + * This function relies on an assumption that the last 4 bytes of the supplied + * frame is the CRC, as stated in the Radiotap specification. + * + * You can check if the frame data has this field by using libwifi_get_wifi_frame() + * and then checking if the libwifi_frame's flags has the LIBWIFI_FLAGS_FCS_PRESENT + * bit set. * * @param frame An 802.11 frame with an FCS * @param frame_len Length of the frame -- cgit 1.4.1