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>
|
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
#include "ie.h"
|
2014-12-12 11:17:43 +01:00
|
|
|
#include "mpdu.h"
|
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
static bool validate_mgmt_header(const struct mmpdu_header *mpdu,
|
|
|
|
int len, int *offset)
|
2014-12-17 23:22:06 +01:00
|
|
|
{
|
2014-12-18 00:44:44 +01:00
|
|
|
/* Duration + Address1 + Address 2 + Address 3 + SeqCntrl */
|
|
|
|
if (len < *offset + 22)
|
2014-12-12 11:17:43 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
*offset += 22;
|
2014-12-12 11:17:43 +01:00
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
if (!mpdu->fc.order)
|
|
|
|
return true;
|
2014-12-12 11:17:43 +01:00
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
if (len < *offset + 4)
|
2014-12-12 11:17:43 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
*offset += 4;
|
2014-12-12 11:17:43 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
static bool validate_on_ies_start_position_mmpdu(
|
|
|
|
const struct mmpdu_header *mpdu,
|
|
|
|
int len, int *offset, int position)
|
2015-01-21 12:36:41 +01:00
|
|
|
{
|
|
|
|
return *offset + position < len;
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
static bool validate_atim_mmpdu(const struct mmpdu_header *mpdu,
|
|
|
|
int len, int *offset)
|
2015-01-21 12:36:39 +01:00
|
|
|
{
|
|
|
|
return *offset == len;
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
static bool validate_disassociation_mmpdu(const struct mmpdu_header *mpdu,
|
2015-01-21 12:36:40 +01:00
|
|
|
int len, int *offset)
|
|
|
|
{
|
|
|
|
*offset += 2;
|
|
|
|
return *offset <= len;
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
static bool validate_authentication_mmpdu(const struct mmpdu_header *mpdu,
|
2014-12-18 00:44:44 +01:00
|
|
|
int len, int *offset)
|
2014-12-12 11:17:43 +01:00
|
|
|
{
|
2015-01-22 19:38:28 +01:00
|
|
|
uint16_t transaction_sequence;
|
2017-08-31 04:04:41 +02:00
|
|
|
const struct mmpdu_authentication *body = (const void *) mpdu + *offset;
|
2015-01-22 19:38:28 +01:00
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
if (len < *offset + 6)
|
2014-12-12 11:17:43 +01:00
|
|
|
return false;
|
|
|
|
|
2015-01-21 21:24:24 +01:00
|
|
|
*offset += 6;
|
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
switch (L_LE16_TO_CPU(body->algorithm)) {
|
|
|
|
case MMPDU_AUTH_ALGO_OPEN_SYSTEM:
|
2014-12-18 00:44:44 +01:00
|
|
|
return *offset <= len;
|
2017-08-31 04:04:41 +02:00
|
|
|
case MMPDU_AUTH_ALGO_SHARED_KEY:
|
2015-01-22 19:38:28 +01:00
|
|
|
transaction_sequence =
|
2017-08-31 04:04:41 +02:00
|
|
|
L_LE16_TO_CPU(body->transaction_sequence);
|
2015-01-22 19:38:28 +01:00
|
|
|
|
|
|
|
if (transaction_sequence < 2 || transaction_sequence > 3)
|
2014-12-18 00:44:44 +01:00
|
|
|
return *offset == len;
|
2014-12-12 11:17:43 +01:00
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
if (len < *offset + 2)
|
|
|
|
return false;
|
2014-12-12 11:17:43 +01:00
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
*offset += 2;
|
2014-12-12 11:17:43 +01:00
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
if (body->shared_key_23.element_id != IE_TYPE_CHALLENGE_TEXT)
|
2014-12-12 11:17:43 +01:00
|
|
|
return false;
|
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
*offset += body->shared_key_23.challenge_text_len;
|
2014-12-18 00:44:44 +01:00
|
|
|
return *offset <= len;
|
|
|
|
default:
|
|
|
|
return false;
|
2014-12-12 11:17:43 +01:00
|
|
|
}
|
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
return false;
|
2014-12-12 11:17:43 +01:00
|
|
|
}
|
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
static bool validate_deauthentication_mmpdu(const struct mmpdu_header *mpdu,
|
2014-12-18 00:44:44 +01:00
|
|
|
int len, int *offset)
|
2014-12-12 11:17:43 +01:00
|
|
|
{
|
2014-12-18 00:44:44 +01:00
|
|
|
*offset += 2;
|
|
|
|
return *offset <= len;
|
2014-12-12 11:17:43 +01:00
|
|
|
}
|
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
static bool validate_mgmt_mpdu(const struct mmpdu_header *mpdu, int len,
|
|
|
|
int *offset)
|
2014-12-12 11:17:43 +01:00
|
|
|
{
|
2014-12-18 00:44:44 +01:00
|
|
|
if (!validate_mgmt_header(mpdu, len, offset))
|
2014-12-12 11:17:43 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
switch (mpdu->fc.subtype) {
|
2015-01-21 12:36:41 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_ASSOCIATION_REQUEST:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_on_ies_start_position_mmpdu(mpdu, len,
|
2015-01-21 12:36:41 +01:00
|
|
|
offset, 9);
|
2015-01-21 12:36:42 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_ASSOCIATION_RESPONSE:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_on_ies_start_position_mmpdu(mpdu, len,
|
2015-01-21 12:36:42 +01:00
|
|
|
offset, 9);
|
2015-01-21 12:36:43 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_REASSOCIATION_REQUEST:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_on_ies_start_position_mmpdu(mpdu, len,
|
2015-01-21 12:36:43 +01:00
|
|
|
offset, 15);
|
2015-01-21 12:36:44 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_REASSOCIATION_RESPONSE:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_on_ies_start_position_mmpdu(mpdu, len,
|
2015-01-21 12:36:44 +01:00
|
|
|
offset, 9);
|
2015-01-21 12:36:45 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_PROBE_REQUEST:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_on_ies_start_position_mmpdu(mpdu, len,
|
2015-01-21 12:36:45 +01:00
|
|
|
offset, 0);
|
2015-01-21 12:36:46 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_PROBE_RESPONSE:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_on_ies_start_position_mmpdu(mpdu, len,
|
2015-01-21 12:36:46 +01:00
|
|
|
offset, 5);
|
2015-01-21 12:36:47 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_TIMING_ADVERTISEMENT:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_on_ies_start_position_mmpdu(mpdu, len,
|
2015-01-21 12:36:47 +01:00
|
|
|
offset, 3);
|
2015-01-21 12:36:48 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_BEACON:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_on_ies_start_position_mmpdu(mpdu, len,
|
2015-01-21 12:36:48 +01:00
|
|
|
offset, 5);
|
2015-01-21 12:36:39 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_ATIM:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_atim_mmpdu(mpdu, len, offset);
|
2015-01-21 12:36:40 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_DISASSOCIATION:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_disassociation_mmpdu(mpdu, len, offset);
|
2014-12-17 23:22:06 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_AUTHENTICATION:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_authentication_mmpdu(mpdu, len, offset);
|
2014-12-17 23:22:06 +01:00
|
|
|
case MPDU_MANAGEMENT_SUBTYPE_DEAUTHENTICATION:
|
2017-08-31 04:04:41 +02:00
|
|
|
return validate_deauthentication_mmpdu(mpdu, len, offset);
|
2014-12-12 11:17:43 +01:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
const struct mmpdu_header *mpdu_validate(const uint8_t *frame, int len)
|
2014-12-12 11:17:43 +01:00
|
|
|
{
|
2017-08-31 04:04:41 +02:00
|
|
|
const struct mpdu_fc *fc;
|
|
|
|
const struct mmpdu_header *mmpdu;
|
2014-12-17 23:22:06 +01:00
|
|
|
int offset;
|
2014-12-12 11:17:43 +01:00
|
|
|
|
2014-12-18 00:44:44 +01:00
|
|
|
if (!frame)
|
2015-01-22 16:58:21 +01:00
|
|
|
return NULL;
|
2014-12-12 11:17:43 +01:00
|
|
|
|
2014-12-17 23:22:06 +01:00
|
|
|
if (len < 2)
|
2015-01-22 16:58:21 +01:00
|
|
|
return NULL;
|
2014-12-12 11:17:43 +01:00
|
|
|
|
2014-12-17 23:22:06 +01:00
|
|
|
offset = 2;
|
2017-08-31 04:04:41 +02:00
|
|
|
fc = (const struct mpdu_fc *) frame;
|
2014-12-17 23:22:06 +01:00
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
switch (fc->type) {
|
2014-12-12 11:17:43 +01:00
|
|
|
case MPDU_TYPE_MANAGEMENT:
|
2017-09-01 01:23:47 +02:00
|
|
|
mmpdu = (const struct mmpdu_header *) frame;
|
2017-08-31 04:04:41 +02:00
|
|
|
|
|
|
|
if (validate_mgmt_mpdu(mmpdu, len, &offset))
|
|
|
|
return mmpdu;
|
|
|
|
|
|
|
|
return NULL;
|
2014-12-12 11:17:43 +01:00
|
|
|
default:
|
2015-01-22 16:58:21 +01:00
|
|
|
return NULL;
|
2014-12-12 11:17:43 +01:00
|
|
|
}
|
2017-08-31 04:04:41 +02:00
|
|
|
}
|
2014-12-12 11:17:43 +01:00
|
|
|
|
2017-08-31 04:04:41 +02:00
|
|
|
static size_t mmpdu_header_len(const struct mmpdu_header *mmpdu)
|
|
|
|
{
|
|
|
|
return mmpdu->fc.order == 0 ? 24 : 28;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *mmpdu_body(const struct mmpdu_header *mmpdu)
|
|
|
|
{
|
|
|
|
return ((const uint8_t *) mmpdu + mmpdu_header_len(mmpdu));
|
2014-12-12 11:17:43 +01:00
|
|
|
}
|