2014-12-12 11:17:43 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <ell/ell.h>
|
|
|
|
|
|
|
|
#include "mpdu.h"
|
|
|
|
|
2014-12-17 23:22:06 +01:00
|
|
|
static inline unsigned char bit_field(unsigned char oct, int start, int num)
|
|
|
|
{
|
|
|
|
unsigned char mask = (1 << num) - 1;
|
|
|
|
|
|
|
|
return (oct >> start) & mask;
|
|
|
|
}
|
|
|
|
|
2014-12-12 11:17:43 +01:00
|
|
|
static inline bool next_byte(const unsigned char *mpdu, int len,
|
|
|
|
int *offset, unsigned char *holder)
|
|
|
|
{
|
|
|
|
if (len == *offset)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*holder = mpdu[*offset];
|
|
|
|
*offset = *offset + 1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool next_2bytes(const unsigned char *mpdu, int len,
|
|
|
|
int *offset, uint16_t *holder)
|
|
|
|
{
|
2014-12-16 21:22:13 +01:00
|
|
|
if (len < *offset + 2)
|
2014-12-12 11:17:43 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-17 23:21:05 +01:00
|
|
|
*holder = L_LE16_TO_CPU(*(uint16_t *)(mpdu + *offset));
|
2014-12-12 11:17:43 +01:00
|
|
|
*offset = *offset + 2;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool next_data(const unsigned char *mpdu, int len,
|
|
|
|
int *offset, unsigned char *holder, int t_len)
|
|
|
|
{
|
2014-12-16 21:22:13 +01:00
|
|
|
if (len < *offset + t_len)
|
2014-12-12 11:17:43 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-16 21:26:33 +01:00
|
|
|
memcpy(holder, mpdu + *offset, t_len);
|
2014-12-12 11:17:43 +01:00
|
|
|
*offset += t_len;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool decode_mgmt_header(const unsigned char *mpdu, int len,
|
|
|
|
int *offset, struct mpdu *out)
|
|
|
|
{
|
2014-12-17 23:22:06 +01:00
|
|
|
uint16_t sequence_control;
|
|
|
|
|
2014-12-12 11:17:43 +01:00
|
|
|
if (!next_2bytes(mpdu, len, offset, &out->mgmt_hdr.duration))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!next_data(mpdu, len, offset, out->mgmt_hdr.address_1, 6))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!next_data(mpdu, len, offset, out->mgmt_hdr.address_2, 6))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!next_data(mpdu, len, offset, out->mgmt_hdr.address_3, 6))
|
|
|
|
return false;
|
|
|
|
|
2014-12-17 23:22:06 +01:00
|
|
|
if (!next_2bytes(mpdu, len, offset, &sequence_control))
|
2014-12-12 11:17:43 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-17 23:22:06 +01:00
|
|
|
out->mgmt_hdr.fragment_number = sequence_control & 0x0f;
|
|
|
|
out->mgmt_hdr.sequence_number = sequence_control >> 4;
|
|
|
|
|
2014-12-12 11:17:43 +01:00
|
|
|
if (out->fc.order)
|
|
|
|
*offset += sizeof(uint32_t); /* Skipping ht_control for now */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool decode_authentication_mgmt_mpdu(const unsigned char *mpdu,
|
|
|
|
int len, int *offset, struct mpdu *out)
|
|
|
|
{
|
|
|
|
if (!next_2bytes(mpdu, len, offset, &out->auth.algorithm))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!next_2bytes(mpdu, len, offset, &out->auth.transaction_sequence))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!next_2bytes(mpdu, len, offset, &out->auth.status))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (out->auth.algorithm == MPDU_AUTH_ALGO_SK) {
|
|
|
|
if (out->auth.transaction_sequence < 2 &&
|
|
|
|
out->auth.transaction_sequence > 3)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!next_byte(mpdu, len, offset,
|
|
|
|
&out->auth.challenge_text_len))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!next_data(mpdu, len, offset, out->auth.challenge_text,
|
|
|
|
out->auth.challenge_text_len))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool decode_deauthentication_mgmt_mpdu(const unsigned char *mpdu,
|
|
|
|
int len, int *offset, struct mpdu *out)
|
|
|
|
{
|
|
|
|
return next_2bytes(mpdu, len, offset, &out->deauth.reason_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool decode_mgmt_mpdu(const unsigned char *mpdu, int len,
|
|
|
|
int *offset, struct mpdu *out)
|
|
|
|
{
|
|
|
|
if (!decode_mgmt_header(mpdu, len, offset, out))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (out->fc.subtype) {
|
2014-12-17 23:22:06 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_AUTHENTICATION:
|
2014-12-12 11:17:43 +01:00
|
|
|
return decode_authentication_mgmt_mpdu(mpdu, len, offset, out);
|
2014-12-17 23:22:06 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_DEAUTHENTICATION:
|
2014-12-16 23:57:30 +01:00
|
|
|
return decode_deauthentication_mgmt_mpdu(mpdu, len, offset,
|
|
|
|
out);
|
2014-12-12 11:17:43 +01:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mpdu_decode(const unsigned char *mpdu, int len, struct mpdu *out)
|
|
|
|
{
|
2014-12-17 23:22:06 +01:00
|
|
|
int offset;
|
2014-12-12 11:17:43 +01:00
|
|
|
|
|
|
|
if (!mpdu || !out)
|
|
|
|
return false;
|
|
|
|
|
2014-12-17 23:22:06 +01:00
|
|
|
if (len < 2)
|
2014-12-12 11:17:43 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-17 23:22:06 +01:00
|
|
|
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;
|
|
|
|
|
2014-12-12 11:17:43 +01:00
|
|
|
switch (out->fc.type) {
|
|
|
|
case MPDU_TYPE_MANAGEMENT:
|
|
|
|
return decode_mgmt_mpdu(mpdu, len, &offset, out);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|