2 * Received frame processing
3 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/radiotap.h"
19 #include "utils/radiotap_iter.h"
20 #include "common/ieee802_11_defs.h"
24 static struct wlantest_sta * rx_get_sta(struct wlantest *wt,
25 const struct ieee80211_hdr *hdr,
26 size_t len, int *to_ap)
29 const u8 *sta_addr, *bssid;
30 struct wlantest_bss *bss;
33 if (hdr->addr1[0] & 0x01)
34 return NULL; /* Ignore group addressed frames */
36 fc = le_to_host16(hdr->frame_control);
37 switch (WLAN_FC_GET_TYPE(fc)) {
38 case WLAN_FC_TYPE_MGMT:
42 if (os_memcmp(bssid, hdr->addr2, ETH_ALEN) == 0) {
43 sta_addr = hdr->addr1;
46 if (os_memcmp(bssid, hdr->addr1, ETH_ALEN) != 0)
47 return NULL; /* Unsupported STA-to-STA frame */
48 sta_addr = hdr->addr2;
52 case WLAN_FC_TYPE_DATA:
55 switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
57 return NULL; /* IBSS not supported */
59 sta_addr = hdr->addr1;
64 sta_addr = hdr->addr2;
68 case WLAN_FC_TODS | WLAN_FC_FROMDS:
69 return NULL; /* WDS not supported */
74 case WLAN_FC_TYPE_CTRL:
75 if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PSPOLL &&
77 sta_addr = hdr->addr2;
87 bss = bss_find(wt, bssid);
90 return sta_find(bss, sta_addr);
94 static void rx_update_ps(struct wlantest *wt, const struct ieee80211_hdr *hdr,
95 size_t len, struct wlantest_sta *sta, int to_ap)
102 fc = le_to_host16(hdr->frame_control);
103 type = WLAN_FC_GET_TYPE(fc);
104 stype = WLAN_FC_GET_STYPE(fc);
107 if (sta->pwrmgt && !sta->pspoll) {
108 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
109 wpa_printf(MSG_DEBUG, "AP " MACSTR " sent a frame "
110 "(%u:%u) to a sleeping STA " MACSTR
112 MAC2STR(sta->bss->bssid),
113 type, stype, MAC2STR(sta->addr),
114 WLAN_GET_SEQ_SEQ(seq_ctrl));
122 if (type == WLAN_FC_TYPE_DATA || type == WLAN_FC_TYPE_MGMT ||
123 (type == WLAN_FC_TYPE_CTRL && stype == WLAN_FC_STYPE_PSPOLL)) {
125 * In theory, the PS state changes only at the end of the frame
126 * exchange that is ACKed by the AP. However, most cases are
127 * handled with this simpler implementation that does not
128 * maintain state through the frame exchange.
130 if (sta->pwrmgt && !(fc & WLAN_FC_PWRMGT)) {
131 wpa_printf(MSG_DEBUG, "STA " MACSTR " woke up from "
132 "sleep", MAC2STR(sta->addr));
134 } else if (!sta->pwrmgt && (fc & WLAN_FC_PWRMGT)) {
135 wpa_printf(MSG_DEBUG, "STA " MACSTR " went to sleep",
141 if (type == WLAN_FC_TYPE_CTRL && stype == WLAN_FC_STYPE_PSPOLL)
146 static int rx_duplicate(struct wlantest *wt, const struct ieee80211_hdr *hdr,
147 size_t len, struct wlantest_sta *sta, int to_ap)
156 fc = le_to_host16(hdr->frame_control);
157 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
158 (WLAN_FC_GET_STYPE(fc) & 0x08) && len >= 26) {
159 const u8 *qos = ((const u8 *) hdr) + 24;
164 seq_ctrl = &sta->seq_ctrl_to_ap[tid];
166 seq_ctrl = &sta->seq_ctrl_to_sta[tid];
168 if ((fc & WLAN_FC_RETRY) && hdr->seq_ctrl == *seq_ctrl) {
169 u16 s = le_to_host16(hdr->seq_ctrl);
170 wpa_printf(MSG_MSGDUMP, "Ignore duplicated frame (seq=%u "
171 "frag=%u A1=" MACSTR " A2=" MACSTR ")",
172 WLAN_GET_SEQ_SEQ(s), WLAN_GET_SEQ_FRAG(s),
173 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2));
177 *seq_ctrl = hdr->seq_ctrl;
183 static void rx_ack(struct wlantest *wt, const struct ieee80211_hdr *hdr)
185 struct ieee80211_hdr *last = (struct ieee80211_hdr *) wt->last_hdr;
188 if (wt->last_len < 24 || (last->addr1[0] & 0x01) ||
189 os_memcmp(hdr->addr1, last->addr2, ETH_ALEN) != 0) {
190 wpa_printf(MSG_MSGDUMP, "Unknown Ack frame (previous frame "
195 /* Ack to the previous frame */
196 fc = le_to_host16(last->frame_control);
197 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT)
198 rx_mgmt_ack(wt, last);
202 static void rx_frame(struct wlantest *wt, const u8 *data, size_t len)
204 const struct ieee80211_hdr *hdr;
206 struct wlantest_sta *sta;
209 wpa_hexdump(MSG_EXCESSIVE, "RX frame", data, len);
213 hdr = (const struct ieee80211_hdr *) data;
214 fc = le_to_host16(hdr->frame_control);
215 if (fc & WLAN_FC_PVER) {
216 wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected pver=%d",
221 sta = rx_get_sta(wt, hdr, len, &to_ap);
223 switch (WLAN_FC_GET_TYPE(fc)) {
224 case WLAN_FC_TYPE_MGMT:
227 if (rx_duplicate(wt, hdr, len, sta, to_ap))
229 rx_update_ps(wt, hdr, len, sta, to_ap);
230 rx_mgmt(wt, data, len);
232 case WLAN_FC_TYPE_CTRL:
236 rx_update_ps(wt, hdr, len, sta, to_ap);
237 if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACK)
240 case WLAN_FC_TYPE_DATA:
243 if (rx_duplicate(wt, hdr, len, sta, to_ap))
245 rx_update_ps(wt, hdr, len, sta, to_ap);
246 rx_data(wt, data, len);
249 wpa_printf(MSG_DEBUG, "Drop RX frame with unexpected type %d",
250 WLAN_FC_GET_TYPE(fc));
254 os_memcpy(wt->last_hdr, data, len > sizeof(wt->last_hdr) ?
255 sizeof(wt->last_hdr) : len);
260 static void tx_status(struct wlantest *wt, const u8 *data, size_t len, int ack)
262 wpa_printf(MSG_DEBUG, "TX status: ack=%d", ack);
263 wpa_hexdump(MSG_EXCESSIVE, "TX status frame", data, len);
267 static int check_fcs(const u8 *frame, size_t frame_len, const u8 *fcs)
269 if (WPA_GET_LE32(fcs) != crc32(frame, frame_len))
275 void wlantest_process(struct wlantest *wt, const u8 *data, size_t len)
277 struct ieee80211_radiotap_iterator iter;
279 int rxflags = 0, txflags = 0, failed = 0, fcs = 0;
280 const u8 *frame, *fcspos;
283 wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
285 if (ieee80211_radiotap_iterator_init(&iter, (void *) data, len)) {
286 wpa_printf(MSG_INFO, "Invalid radiotap frame");
291 ret = ieee80211_radiotap_iterator_next(&iter);
292 wpa_printf(MSG_EXCESSIVE, "radiotap iter: %d "
293 "this_arg_index=%d", ret, iter.this_arg_index);
297 wpa_printf(MSG_INFO, "Invalid radiotap header: %d",
301 switch (iter.this_arg_index) {
302 case IEEE80211_RADIOTAP_FLAGS:
303 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
306 case IEEE80211_RADIOTAP_RX_FLAGS:
309 case IEEE80211_RADIOTAP_TX_FLAGS:
311 failed = le_to_host16((*(u16 *) iter.this_arg)) &
312 IEEE80211_RADIOTAP_F_TX_FAIL;
318 if (iter.max_length == 8) {
319 wpa_printf(MSG_DEBUG, "Skip frame inserted by wlantest");
322 frame = data + iter.max_length;
323 frame_len = len - iter.max_length;
325 if (fcs && frame_len >= 4) {
327 fcspos = frame + frame_len;
328 if (check_fcs(frame, frame_len, fcspos) < 0) {
329 wpa_printf(MSG_EXCESSIVE, "Drop RX frame with invalid "
336 if (rxflags && txflags)
339 rx_frame(wt, frame, frame_len);
341 tx_status(wt, frame, frame_len, !failed);
345 void wlantest_process_prism(struct wlantest *wt, const u8 *data, size_t len)
348 const u8 *frame, *fcspos;
352 wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
356 hdrlen = WPA_GET_LE32(data + 4);
359 wpa_printf(MSG_INFO, "Too short frame to include prism "
364 frame = data + hdrlen;
365 frame_len = len - hdrlen;
368 if (fcs && frame_len >= 4) {
370 fcspos = frame + frame_len;
371 if (check_fcs(frame, frame_len, fcspos) < 0) {
372 wpa_printf(MSG_EXCESSIVE, "Drop RX frame with invalid "
379 rx_frame(wt, frame, frame_len);
383 void wlantest_process_80211(struct wlantest *wt, const u8 *data, size_t len)
385 wpa_hexdump(MSG_EXCESSIVE, "Process data", data, len);
386 rx_frame(wt, data, len);