diff --git a/src/mpdu.c b/src/mpdu.c index 13c96156..392ebacf 100644 --- a/src/mpdu.c +++ b/src/mpdu.c @@ -28,6 +28,13 @@ #include "mpdu.h" +static inline unsigned char bit_field(unsigned char oct, int start, int num) +{ + unsigned char mask = (1 << num) - 1; + + return (oct >> start) & mask; +} + static inline bool next_byte(const unsigned char *mpdu, int len, int *offset, unsigned char *holder) { @@ -67,6 +74,8 @@ static inline bool next_data(const unsigned char *mpdu, int len, static bool decode_mgmt_header(const unsigned char *mpdu, int len, int *offset, struct mpdu *out) { + uint16_t sequence_control; + if (!next_2bytes(mpdu, len, offset, &out->mgmt_hdr.duration)) return false; @@ -79,9 +88,12 @@ static bool decode_mgmt_header(const unsigned char *mpdu, int len, if (!next_data(mpdu, len, offset, out->mgmt_hdr.address_3, 6)) return false; - if (!next_2bytes(mpdu, len, offset, &out->mgmt_hdr.sequence_control)) + if (!next_2bytes(mpdu, len, offset, &sequence_control)) return false; + out->mgmt_hdr.fragment_number = sequence_control & 0x0f; + out->mgmt_hdr.sequence_number = sequence_control >> 4; + if (out->fc.order) *offset += sizeof(uint32_t); /* Skipping ht_control for now */ @@ -130,9 +142,9 @@ static bool decode_mgmt_mpdu(const unsigned char *mpdu, int len, return false; switch (out->fc.subtype) { - case MPDU_MGMT_TYPE_AUTHENTICATION: + case MPDU_MANAGEMENT_SUBTYPE_AUTHENTICATION: return decode_authentication_mgmt_mpdu(mpdu, len, offset, out); - case MPDU_MGMT_TYPE_DEAUTHENTICATION: + case MPDU_MANAGEMENT_SUBTYPE_DEAUTHENTICATION: return decode_deauthentication_mgmt_mpdu(mpdu, len, offset, out); default: @@ -144,14 +156,29 @@ static bool decode_mgmt_mpdu(const unsigned char *mpdu, int len, bool mpdu_decode(const unsigned char *mpdu, int len, struct mpdu *out) { - int offset = 0; + int offset; if (!mpdu || !out) return false; - if (!next_2bytes(mpdu, len, &offset, &out->fc.content)) + if (len < 2) return false; + out->fc.protocol_version = bit_field(mpdu[0], 0, 2); + out->fc.type = bit_field(mpdu[0], 2, 2); + out->fc.subtype = bit_field(mpdu[0], 4, 4); + + out->fc.to_ds = bit_field(mpdu[1], 0, 1); + out->fc.from_ds = bit_field(mpdu[1], 1, 1); + out->fc.more_fragments = bit_field(mpdu[1], 2, 1); + out->fc.retry = bit_field(mpdu[1], 3, 1); + out->fc.power_mgmt = bit_field(mpdu[1], 4, 1); + out->fc.more_data = bit_field(mpdu[1], 5, 1); + out->fc.protected_frame = bit_field(mpdu[1], 6, 1); + out->fc.order = bit_field(mpdu[1], 7, 1); + + offset = 2; + switch (out->fc.type) { case MPDU_TYPE_MANAGEMENT: return decode_mgmt_mpdu(mpdu, len, &offset, out); diff --git a/src/mpdu.h b/src/mpdu.h index 06cd9711..458b3707 100644 --- a/src/mpdu.h +++ b/src/mpdu.h @@ -29,9 +29,9 @@ enum mpdu_type { }; /* 802.11, Table 8-1 "Valid type and subtype combinations" */ -enum mpdu_mgmt_type { - MPDU_MGMT_TYPE_AUTHENTICATION = 0xB, - MPDU_MGMT_TYPE_DEAUTHENTICATION = 0xC, +enum mpdu_management_subtype { + MPDU_MANAGEMENT_SUBTYPE_AUTHENTICATION = 0xB, + MPDU_MANAGEMENT_SUBTYPE_DEAUTHENTICATION = 0xC, }; /* 802.11, Section 8.4.1.1 Authentication Algorithm Number field */ @@ -42,23 +42,18 @@ enum mpdu_authentication_algorithm_number { /* 802.11, Section 8.2.4.1.1, Figure 8-2 */ struct mpdu_fc { - union { - struct { - uint16_t protocol_version:2; - uint16_t type:2; - uint16_t subtype:4; - uint16_t to_ds:1; - uint16_t from_ds:1; - uint16_t more_fragments:1; - uint16_t retry:1; - uint16_t power_mgmt:1; - uint16_t more_data:1; - uint16_t protected_frame:1; - uint16_t order:1; - }; - uint16_t content; - }; -}; + uint8_t protocol_version:2; + uint8_t type:2; + uint8_t subtype:4; + bool to_ds:1; + bool from_ds:1; + bool more_fragments:1; + bool retry:1; + bool power_mgmt:1; + bool more_data:1; + bool protected_frame:1; + bool order:1; +} __attribute__ ((packed)); /* 802.11, Section 8.3.3.1 */ struct mpdu_mgmt_header { @@ -66,15 +61,10 @@ struct mpdu_mgmt_header { unsigned char address_1[6]; unsigned char address_2[6]; unsigned char address_3[6]; - union { - struct { - uint16_t fragment_number:4; - uint16_t sequence_number:12; - }; - uint16_t sequence_control; - }; + uint16_t fragment_number:4; + uint16_t sequence_number:12; uint32_t ht_control; /* ToDo? */ -}; +} __attribute__ ((packed)); /* 802.11, Section 8.3.3.11 */ struct mpdu_authentication {