2010-11-06 15:20:45 +01:00
|
|
|
/*
|
|
|
|
* Received frame processing
|
|
|
|
* Copyright (c) 2010, Jouni Malinen <j@w1.fi>
|
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2010-11-06 15:20:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utils/includes.h"
|
|
|
|
|
|
|
|
#include "utils/common.h"
|
2015-09-02 15:16:50 +02:00
|
|
|
#include "utils/crc32.h"
|
2010-11-06 15:20:45 +01:00
|
|
|
#include "utils/radiotap.h"
|
|
|
|
#include "utils/radiotap_iter.h"
|
|
|
|
#include "common/ieee802_11_defs.h"
|
2014-04-09 11:53:16 +02:00
|
|
|
#include "common/qca-vendor.h"
|
2010-11-06 15:20:45 +01:00
|
|
|
#include "wlantest.h"
|
|
|
|
|
|
|
|
|
2011-01-07 14:54:58 +01:00
|
|
|
static struct wlantest_sta * rx_get_sta(struct wlantest *wt,
|
|
|
|
const struct ieee80211_hdr *hdr,
|
|
|
|
size_t len, int *to_ap)
|
2010-11-23 13:27:28 +01:00
|
|
|
{
|
|
|
|
u16 fc;
|
|
|
|
const u8 *sta_addr, *bssid;
|
|
|
|
struct wlantest_bss *bss;
|
|
|
|
|
2011-01-07 14:54:58 +01:00
|
|
|
*to_ap = 0;
|
2010-11-23 13:27:28 +01:00
|
|
|
if (hdr->addr1[0] & 0x01)
|
2011-01-07 14:54:58 +01:00
|
|
|
return NULL; /* Ignore group addressed frames */
|
2010-11-23 13:27:28 +01:00
|
|
|
|
|
|
|
fc = le_to_host16(hdr->frame_control);
|
2011-01-07 14:54:58 +01:00
|
|
|
switch (WLAN_FC_GET_TYPE(fc)) {
|
|
|
|
case WLAN_FC_TYPE_MGMT:
|
|
|
|
if (len < 24)
|
|
|
|
return NULL;
|
2010-11-23 13:27:28 +01:00
|
|
|
bssid = hdr->addr3;
|
|
|
|
if (os_memcmp(bssid, hdr->addr2, ETH_ALEN) == 0) {
|
|
|
|
sta_addr = hdr->addr1;
|
2011-01-07 14:54:58 +01:00
|
|
|
*to_ap = 0;
|
2010-11-23 13:27:28 +01:00
|
|
|
} else {
|
|
|
|
if (os_memcmp(bssid, hdr->addr1, ETH_ALEN) != 0)
|
2011-01-07 14:54:58 +01:00
|
|
|
return NULL; /* Unsupported STA-to-STA frame */
|
2010-11-23 13:27:28 +01:00
|
|
|
sta_addr = hdr->addr2;
|
2011-01-07 14:54:58 +01:00
|
|
|
*to_ap = 1;
|
2010-11-23 13:27:28 +01:00
|
|
|
}
|
2011-01-07 14:54:58 +01:00
|
|
|
break;
|
|
|
|
case WLAN_FC_TYPE_DATA:
|
|
|
|
if (len < 24)
|
|
|
|
return NULL;
|
2010-11-23 13:27:28 +01:00
|
|
|
switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
|
|
|
|
case 0:
|
2011-01-07 14:54:58 +01:00
|
|
|
return NULL; /* IBSS not supported */
|
2010-11-23 13:27:28 +01:00
|
|
|
case WLAN_FC_FROMDS:
|
|
|
|
sta_addr = hdr->addr1;
|
|
|
|
bssid = hdr->addr2;
|
2011-01-07 14:54:58 +01:00
|
|
|
*to_ap = 0;
|
2010-11-23 13:27:28 +01:00
|
|
|
break;
|
|
|
|
case WLAN_FC_TODS:
|
|
|
|
sta_addr = hdr->addr2;
|
|
|
|
bssid = hdr->addr1;
|
2011-01-07 14:54:58 +01:00
|
|
|
*to_ap = 1;
|
2010-11-23 13:27:28 +01:00
|
|
|
break;
|
|
|
|
case WLAN_FC_TODS | WLAN_FC_FROMDS:
|
2011-01-07 14:54:58 +01:00
|
|
|
return NULL; /* WDS not supported */
|
2010-11-23 13:27:28 +01:00
|
|
|
default:
|
2011-01-07 14:54:58 +01:00
|
|
|
return NULL;
|
2010-11-23 13:27:28 +01:00
|
|
|
}
|
2011-01-07 14:54:58 +01:00
|
|
|
break;
|
|
|
|
case WLAN_FC_TYPE_CTRL:
|
|
|
|
if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PSPOLL &&
|
|
|
|
len >= 16) {
|
|
|
|
sta_addr = hdr->addr2;
|
|
|
|
bssid = hdr->addr1;
|
|
|
|
*to_ap = 1;
|
|
|
|
} else
|
|
|
|
return NULL;
|
|
|
|
break;
|
2011-01-26 20:29:28 +01:00
|
|
|
default:
|
|
|
|
return NULL;
|
2010-11-23 13:27:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bss = bss_find(wt, bssid);
|
|
|
|
if (bss == NULL)
|
2011-01-26 20:29:28 +01:00
|
|
|
return NULL;
|
2011-01-07 14:54:58 +01:00
|
|
|
return sta_find(bss, sta_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void rx_update_ps(struct wlantest *wt, const struct ieee80211_hdr *hdr,
|
|
|
|
size_t len, struct wlantest_sta *sta, int to_ap)
|
|
|
|
{
|
|
|
|
u16 fc, type, stype;
|
|
|
|
|
|
|
|
if (sta == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fc = le_to_host16(hdr->frame_control);
|
|
|
|
type = WLAN_FC_GET_TYPE(fc);
|
|
|
|
stype = WLAN_FC_GET_STYPE(fc);
|
|
|
|
|
|
|
|
if (!to_ap) {
|
|
|
|
if (sta->pwrmgt && !sta->pspoll) {
|
|
|
|
u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_DEBUG, "AP " MACSTR " sent a frame "
|
|
|
|
"(%u:%u) to a sleeping STA " MACSTR
|
|
|
|
" (seq=%u)",
|
|
|
|
MAC2STR(sta->bss->bssid),
|
|
|
|
type, stype, MAC2STR(sta->addr),
|
|
|
|
WLAN_GET_SEQ_SEQ(seq_ctrl));
|
2011-01-07 14:54:58 +01:00
|
|
|
} else
|
|
|
|
sta->pspoll = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sta->pspoll = 0;
|
|
|
|
|
|
|
|
if (type == WLAN_FC_TYPE_DATA || type == WLAN_FC_TYPE_MGMT ||
|
|
|
|
(type == WLAN_FC_TYPE_CTRL && stype == WLAN_FC_STYPE_PSPOLL)) {
|
|
|
|
/*
|
|
|
|
* In theory, the PS state changes only at the end of the frame
|
|
|
|
* exchange that is ACKed by the AP. However, most cases are
|
|
|
|
* handled with this simpler implementation that does not
|
|
|
|
* maintain state through the frame exchange.
|
|
|
|
*/
|
|
|
|
if (sta->pwrmgt && !(fc & WLAN_FC_PWRMGT)) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_DEBUG, "STA " MACSTR " woke up from "
|
|
|
|
"sleep", MAC2STR(sta->addr));
|
2011-01-07 14:54:58 +01:00
|
|
|
sta->pwrmgt = 0;
|
|
|
|
} else if (!sta->pwrmgt && (fc & WLAN_FC_PWRMGT)) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_DEBUG, "STA " MACSTR " went to sleep",
|
|
|
|
MAC2STR(sta->addr));
|
2011-01-07 14:54:58 +01:00
|
|
|
sta->pwrmgt = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == WLAN_FC_TYPE_CTRL && stype == WLAN_FC_STYPE_PSPOLL)
|
|
|
|
sta->pspoll = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int rx_duplicate(struct wlantest *wt, const struct ieee80211_hdr *hdr,
|
|
|
|
size_t len, struct wlantest_sta *sta, int to_ap)
|
|
|
|
{
|
|
|
|
u16 fc;
|
|
|
|
int tid = 16;
|
|
|
|
le16 *seq_ctrl;
|
|
|
|
|
2010-11-23 13:27:28 +01:00
|
|
|
if (sta == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2011-01-07 14:54:58 +01:00
|
|
|
fc = le_to_host16(hdr->frame_control);
|
|
|
|
if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
|
|
|
|
(WLAN_FC_GET_STYPE(fc) & 0x08) && len >= 26) {
|
|
|
|
const u8 *qos = ((const u8 *) hdr) + 24;
|
|
|
|
tid = qos[0] & 0x0f;
|
|
|
|
}
|
|
|
|
|
2010-11-23 13:27:28 +01:00
|
|
|
if (to_ap)
|
|
|
|
seq_ctrl = &sta->seq_ctrl_to_ap[tid];
|
|
|
|
else
|
|
|
|
seq_ctrl = &sta->seq_ctrl_to_sta[tid];
|
|
|
|
|
|
|
|
if ((fc & WLAN_FC_RETRY) && hdr->seq_ctrl == *seq_ctrl) {
|
|
|
|
u16 s = le_to_host16(hdr->seq_ctrl);
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_MSGDUMP, "Ignore duplicated frame (seq=%u "
|
|
|
|
"frag=%u A1=" MACSTR " A2=" MACSTR ")",
|
|
|
|
WLAN_GET_SEQ_SEQ(s), WLAN_GET_SEQ_FRAG(s),
|
|
|
|
MAC2STR(hdr->addr1), MAC2STR(hdr->addr2));
|
2010-11-23 13:27:28 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*seq_ctrl = hdr->seq_ctrl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-03 18:28:40 +01:00
|
|
|
static void rx_ack(struct wlantest *wt, const struct ieee80211_hdr *hdr)
|
|
|
|
{
|
|
|
|
struct ieee80211_hdr *last = (struct ieee80211_hdr *) wt->last_hdr;
|
|
|
|
u16 fc;
|
|
|
|
|
|
|
|
if (wt->last_len < 24 || (last->addr1[0] & 0x01) ||
|
|
|
|
os_memcmp(hdr->addr1, last->addr2, ETH_ALEN) != 0) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_MSGDUMP, "Unknown Ack frame (previous frame "
|
|
|
|
"not seen)");
|
2011-01-03 18:28:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ack to the previous frame */
|
|
|
|
fc = le_to_host16(last->frame_control);
|
|
|
|
if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
|
|
|
|
rx_mgmt_ack(wt, last);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-06 15:20:45 +01:00
|
|
|
static void rx_frame(struct wlantest *wt, const u8 *data, size_t len)
|
|
|
|
{
|
|
|
|
const struct ieee80211_hdr *hdr;
|
|
|
|
u16 fc;
|
2011-01-07 14:54:58 +01:00
|
|
|
struct wlantest_sta *sta;
|
|
|
|
int to_ap;
|
2010-11-06 15:20:45 +01:00
|
|
|
|
|
|
|
wpa_hexdump(MSG_EXCESSIVE, "RX frame", data, len);
|
|
|
|
if (len < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hdr = (const struct ieee80211_hdr *) data;
|
|
|
|
fc = le_to_host16(hdr->frame_control);
|
|
|
|
if (fc & WLAN_FC_PVER) {
|
|
|
|
wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected pver=%d",
|
|
|
|
fc & WLAN_FC_PVER);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-07 14:54:58 +01:00
|
|
|
sta = rx_get_sta(wt, hdr, len, &to_ap);
|
|
|
|
|
2010-11-06 15:20:45 +01:00
|
|
|
switch (WLAN_FC_GET_TYPE(fc)) {
|
|
|
|
case WLAN_FC_TYPE_MGMT:
|
2010-11-23 13:27:28 +01:00
|
|
|
if (len < 24)
|
|
|
|
break;
|
2011-01-07 14:54:58 +01:00
|
|
|
if (rx_duplicate(wt, hdr, len, sta, to_ap))
|
2010-11-23 13:27:28 +01:00
|
|
|
break;
|
2011-01-07 14:54:58 +01:00
|
|
|
rx_update_ps(wt, hdr, len, sta, to_ap);
|
2010-11-06 15:20:45 +01:00
|
|
|
rx_mgmt(wt, data, len);
|
|
|
|
break;
|
|
|
|
case WLAN_FC_TYPE_CTRL:
|
|
|
|
if (len < 10)
|
2010-11-23 13:27:28 +01:00
|
|
|
break;
|
2010-11-06 15:20:45 +01:00
|
|
|
wt->rx_ctrl++;
|
2011-01-07 14:54:58 +01:00
|
|
|
rx_update_ps(wt, hdr, len, sta, to_ap);
|
2011-01-03 18:28:40 +01:00
|
|
|
if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACK)
|
|
|
|
rx_ack(wt, hdr);
|
2010-11-06 15:20:45 +01:00
|
|
|
break;
|
|
|
|
case WLAN_FC_TYPE_DATA:
|
2010-11-23 13:27:28 +01:00
|
|
|
if (len < 24)
|
|
|
|
break;
|
2011-01-07 14:54:58 +01:00
|
|
|
if (rx_duplicate(wt, hdr, len, sta, to_ap))
|
2010-11-23 13:27:28 +01:00
|
|
|
break;
|
2011-01-07 14:54:58 +01:00
|
|
|
rx_update_ps(wt, hdr, len, sta, to_ap);
|
2010-11-06 15:20:45 +01:00
|
|
|
rx_data(wt, data, len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected type %d",
|
|
|
|
WLAN_FC_GET_TYPE(fc));
|
|
|
|
break;
|
|
|
|
}
|
2011-01-03 18:28:40 +01:00
|
|
|
|
|
|
|
os_memcpy(wt->last_hdr, data, len > sizeof(wt->last_hdr) ?
|
|
|
|
sizeof(wt->last_hdr) : len);
|
|
|
|
wt->last_len = len;
|
2010-11-06 15:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void tx_status(struct wlantest *wt, const u8 *data, size_t len, int ack)
|
|
|
|
{
|
|
|
|
wpa_printf(MSG_DEBUG, "TX status: ack=%d", ack);
|
|
|
|
wpa_hexdump(MSG_EXCESSIVE, "TX status frame", data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int check_fcs(const u8 *frame, size_t frame_len, const u8 *fcs)
|
|
|
|
{
|
|
|
|
if (WPA_GET_LE32(fcs) != crc32(frame, frame_len))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wlantest_process(struct wlantest *wt, const u8 *data, size_t len)
|
|
|
|
{
|
|
|
|
struct ieee80211_radiotap_iterator iter;
|
|
|
|
int ret;
|
|
|
|
int rxflags = 0, txflags = 0, failed = 0, fcs = 0;
|
|
|
|
const u8 *frame, *fcspos;
|
|
|
|
size_t frame_len;
|
|
|
|
|
|
|
|
wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
|
|
|
|
|
2014-04-09 11:50:54 +02:00
|
|
|
if (ieee80211_radiotap_iterator_init(&iter, (void *) data, len, NULL)) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_INFO, "Invalid radiotap frame");
|
2010-11-06 15:20:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ret = ieee80211_radiotap_iterator_next(&iter);
|
|
|
|
wpa_printf(MSG_EXCESSIVE, "radiotap iter: %d "
|
|
|
|
"this_arg_index=%d", ret, iter.this_arg_index);
|
|
|
|
if (ret == -ENOENT)
|
|
|
|
break;
|
|
|
|
if (ret) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_INFO, "Invalid radiotap header: %d",
|
|
|
|
ret);
|
2010-11-06 15:20:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (iter.this_arg_index) {
|
|
|
|
case IEEE80211_RADIOTAP_FLAGS:
|
|
|
|
if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
|
|
|
|
fcs = 1;
|
|
|
|
break;
|
|
|
|
case IEEE80211_RADIOTAP_RX_FLAGS:
|
|
|
|
rxflags = 1;
|
|
|
|
break;
|
|
|
|
case IEEE80211_RADIOTAP_TX_FLAGS:
|
|
|
|
txflags = 1;
|
|
|
|
failed = le_to_host16((*(u16 *) iter.this_arg)) &
|
|
|
|
IEEE80211_RADIOTAP_F_TX_FAIL;
|
|
|
|
break;
|
2014-04-09 11:53:16 +02:00
|
|
|
case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
|
|
|
|
if (WPA_GET_BE24(iter.this_arg) == OUI_QCA &&
|
|
|
|
iter.this_arg[3] == QCA_RADIOTAP_VID_WLANTEST) {
|
|
|
|
add_note(wt, MSG_DEBUG,
|
|
|
|
"Skip frame inserted by wlantest");
|
|
|
|
return;
|
|
|
|
}
|
2010-11-06 15:20:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 11:50:54 +02:00
|
|
|
frame = data + iter._max_length;
|
|
|
|
frame_len = len - iter._max_length;
|
2010-11-06 15:20:45 +01:00
|
|
|
|
|
|
|
if (fcs && frame_len >= 4) {
|
|
|
|
frame_len -= 4;
|
|
|
|
fcspos = frame + frame_len;
|
|
|
|
if (check_fcs(frame, frame_len, fcspos) < 0) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_EXCESSIVE, "Drop RX frame with "
|
|
|
|
"invalid FCS");
|
2010-11-06 15:20:45 +01:00
|
|
|
wt->fcs_error++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rxflags && txflags)
|
|
|
|
return;
|
|
|
|
if (!txflags)
|
|
|
|
rx_frame(wt, frame, frame_len);
|
2012-12-16 11:35:07 +01:00
|
|
|
else {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_EXCESSIVE, "TX status - process as RX of "
|
|
|
|
"local frame");
|
2010-11-06 15:20:45 +01:00
|
|
|
tx_status(wt, frame, frame_len, !failed);
|
2012-12-16 11:35:07 +01:00
|
|
|
/* Process as RX frame to support local monitor interface */
|
|
|
|
rx_frame(wt, frame, frame_len);
|
|
|
|
}
|
2010-11-06 15:20:45 +01:00
|
|
|
}
|
2010-12-02 21:56:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
void wlantest_process_prism(struct wlantest *wt, const u8 *data, size_t len)
|
|
|
|
{
|
|
|
|
int fcs = 0;
|
|
|
|
const u8 *frame, *fcspos;
|
|
|
|
size_t frame_len;
|
|
|
|
u32 hdrlen;
|
|
|
|
|
|
|
|
wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
|
|
|
|
|
|
|
|
if (len < 8)
|
|
|
|
return;
|
|
|
|
hdrlen = WPA_GET_LE32(data + 4);
|
|
|
|
|
|
|
|
if (len < hdrlen) {
|
|
|
|
wpa_printf(MSG_INFO, "Too short frame to include prism "
|
|
|
|
"header");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame = data + hdrlen;
|
|
|
|
frame_len = len - hdrlen;
|
|
|
|
fcs = 1;
|
|
|
|
|
|
|
|
if (fcs && frame_len >= 4) {
|
|
|
|
frame_len -= 4;
|
|
|
|
fcspos = frame + frame_len;
|
|
|
|
if (check_fcs(frame, frame_len, fcspos) < 0) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_EXCESSIVE, "Drop RX frame with "
|
|
|
|
"invalid FCS");
|
2010-12-02 21:56:37 +01:00
|
|
|
wt->fcs_error++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rx_frame(wt, frame, frame_len);
|
|
|
|
}
|
2010-12-04 20:18:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
void wlantest_process_80211(struct wlantest *wt, const u8 *data, size_t len)
|
|
|
|
{
|
|
|
|
wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
|
2013-01-17 11:55:30 +01:00
|
|
|
|
|
|
|
if (wt->assume_fcs && len >= 4) {
|
|
|
|
const u8 *fcspos;
|
|
|
|
|
|
|
|
len -= 4;
|
|
|
|
fcspos = data + len;
|
|
|
|
if (check_fcs(data, len, fcspos) < 0) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_EXCESSIVE, "Drop RX frame with "
|
|
|
|
"invalid FCS");
|
2013-01-17 11:55:30 +01:00
|
|
|
wt->fcs_error++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-04 20:18:21 +01:00
|
|
|
rx_frame(wt, data, len);
|
|
|
|
}
|