mpdu: Rework structure definitions

The current setup was not endian safe
This commit is contained in:
Denis Kenzior 2014-12-17 16:22:06 -06:00
parent 3ef0ff1e1a
commit d7b6a36db8
2 changed files with 50 additions and 33 deletions

View File

@ -28,6 +28,13 @@
#include "mpdu.h" #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, static inline bool next_byte(const unsigned char *mpdu, int len,
int *offset, unsigned char *holder) 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, static bool decode_mgmt_header(const unsigned char *mpdu, int len,
int *offset, struct mpdu *out) int *offset, struct mpdu *out)
{ {
uint16_t sequence_control;
if (!next_2bytes(mpdu, len, offset, &out->mgmt_hdr.duration)) if (!next_2bytes(mpdu, len, offset, &out->mgmt_hdr.duration))
return false; 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)) if (!next_data(mpdu, len, offset, out->mgmt_hdr.address_3, 6))
return false; return false;
if (!next_2bytes(mpdu, len, offset, &out->mgmt_hdr.sequence_control)) if (!next_2bytes(mpdu, len, offset, &sequence_control))
return false; return false;
out->mgmt_hdr.fragment_number = sequence_control & 0x0f;
out->mgmt_hdr.sequence_number = sequence_control >> 4;
if (out->fc.order) if (out->fc.order)
*offset += sizeof(uint32_t); /* Skipping ht_control for now */ *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; return false;
switch (out->fc.subtype) { switch (out->fc.subtype) {
case MPDU_MGMT_TYPE_AUTHENTICATION: case MPDU_MANAGEMENT_SUBTYPE_AUTHENTICATION:
return decode_authentication_mgmt_mpdu(mpdu, len, offset, out); 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, return decode_deauthentication_mgmt_mpdu(mpdu, len, offset,
out); out);
default: 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) bool mpdu_decode(const unsigned char *mpdu, int len, struct mpdu *out)
{ {
int offset = 0; int offset;
if (!mpdu || !out) if (!mpdu || !out)
return false; return false;
if (!next_2bytes(mpdu, len, &offset, &out->fc.content)) if (len < 2)
return false; 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) { switch (out->fc.type) {
case MPDU_TYPE_MANAGEMENT: case MPDU_TYPE_MANAGEMENT:
return decode_mgmt_mpdu(mpdu, len, &offset, out); return decode_mgmt_mpdu(mpdu, len, &offset, out);

View File

@ -29,9 +29,9 @@ enum mpdu_type {
}; };
/* 802.11, Table 8-1 "Valid type and subtype combinations" */ /* 802.11, Table 8-1 "Valid type and subtype combinations" */
enum mpdu_mgmt_type { enum mpdu_management_subtype {
MPDU_MGMT_TYPE_AUTHENTICATION = 0xB, MPDU_MANAGEMENT_SUBTYPE_AUTHENTICATION = 0xB,
MPDU_MGMT_TYPE_DEAUTHENTICATION = 0xC, MPDU_MANAGEMENT_SUBTYPE_DEAUTHENTICATION = 0xC,
}; };
/* 802.11, Section 8.4.1.1 Authentication Algorithm Number field */ /* 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 */ /* 802.11, Section 8.2.4.1.1, Figure 8-2 */
struct mpdu_fc { struct mpdu_fc {
union { uint8_t protocol_version:2;
struct { uint8_t type:2;
uint16_t protocol_version:2; uint8_t subtype:4;
uint16_t type:2; bool to_ds:1;
uint16_t subtype:4; bool from_ds:1;
uint16_t to_ds:1; bool more_fragments:1;
uint16_t from_ds:1; bool retry:1;
uint16_t more_fragments:1; bool power_mgmt:1;
uint16_t retry:1; bool more_data:1;
uint16_t power_mgmt:1; bool protected_frame:1;
uint16_t more_data:1; bool order:1;
uint16_t protected_frame:1; } __attribute__ ((packed));
uint16_t order:1;
};
uint16_t content;
};
};
/* 802.11, Section 8.3.3.1 */ /* 802.11, Section 8.3.3.1 */
struct mpdu_mgmt_header { struct mpdu_mgmt_header {
@ -66,15 +61,10 @@ struct mpdu_mgmt_header {
unsigned char address_1[6]; unsigned char address_1[6];
unsigned char address_2[6]; unsigned char address_2[6];
unsigned char address_3[6]; unsigned char address_3[6];
union { uint16_t fragment_number:4;
struct { uint16_t sequence_number:12;
uint16_t fragment_number:4;
uint16_t sequence_number:12;
};
uint16_t sequence_control;
};
uint32_t ht_control; /* ToDo? */ uint32_t ht_control; /* ToDo? */
}; } __attribute__ ((packed));
/* 802.11, Section 8.3.3.11 */ /* 802.11, Section 8.3.3.11 */
struct mpdu_authentication { struct mpdu_authentication {