]> git.ipfire.org Git - thirdparty/hostap.git/blob - wlantest/rx_data.c
Add attribute for dwell time in QCA vendor scan
[thirdparty/hostap.git] / wlantest / rx_data.c
1 /*
2 * Received Data frame processing
3 * Copyright (c) 2010-2015, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "common/defs.h"
13 #include "common/ieee802_11_defs.h"
14 #include "wlantest.h"
15
16
17 static const char * data_stype(u16 stype)
18 {
19 switch (stype) {
20 case WLAN_FC_STYPE_DATA:
21 return "DATA";
22 case WLAN_FC_STYPE_DATA_CFACK:
23 return "DATA-CFACK";
24 case WLAN_FC_STYPE_DATA_CFPOLL:
25 return "DATA-CFPOLL";
26 case WLAN_FC_STYPE_DATA_CFACKPOLL:
27 return "DATA-CFACKPOLL";
28 case WLAN_FC_STYPE_NULLFUNC:
29 return "NULLFUNC";
30 case WLAN_FC_STYPE_CFACK:
31 return "CFACK";
32 case WLAN_FC_STYPE_CFPOLL:
33 return "CFPOLL";
34 case WLAN_FC_STYPE_CFACKPOLL:
35 return "CFACKPOLL";
36 case WLAN_FC_STYPE_QOS_DATA:
37 return "QOSDATA";
38 case WLAN_FC_STYPE_QOS_DATA_CFACK:
39 return "QOSDATA-CFACK";
40 case WLAN_FC_STYPE_QOS_DATA_CFPOLL:
41 return "QOSDATA-CFPOLL";
42 case WLAN_FC_STYPE_QOS_DATA_CFACKPOLL:
43 return "QOSDATA-CFACKPOLL";
44 case WLAN_FC_STYPE_QOS_NULL:
45 return "QOS-NULL";
46 case WLAN_FC_STYPE_QOS_CFPOLL:
47 return "QOS-CFPOLL";
48 case WLAN_FC_STYPE_QOS_CFACKPOLL:
49 return "QOS-CFACKPOLL";
50 }
51 return "??";
52 }
53
54
55 static void rx_data_eth(struct wlantest *wt, const u8 *bssid,
56 const u8 *sta_addr, const u8 *dst, const u8 *src,
57 u16 ethertype, const u8 *data, size_t len, int prot,
58 const u8 *peer_addr);
59
60 static void rx_data_vlan(struct wlantest *wt, const u8 *bssid,
61 const u8 *sta_addr, const u8 *dst, const u8 *src,
62 const u8 *data, size_t len, int prot,
63 const u8 *peer_addr)
64 {
65 u16 tag;
66
67 if (len < 4)
68 return;
69 tag = WPA_GET_BE16(data);
70 wpa_printf(MSG_MSGDUMP, "VLAN tag: Priority=%u ID=%u",
71 tag >> 12, tag & 0x0ffff);
72 /* ignore VLAN information and process the original frame */
73 rx_data_eth(wt, bssid, sta_addr, dst, src, WPA_GET_BE16(data + 2),
74 data + 4, len - 4, prot, peer_addr);
75 }
76
77
78 static void rx_data_eth(struct wlantest *wt, const u8 *bssid,
79 const u8 *sta_addr, const u8 *dst, const u8 *src,
80 u16 ethertype, const u8 *data, size_t len, int prot,
81 const u8 *peer_addr)
82 {
83 switch (ethertype) {
84 case ETH_P_PAE:
85 rx_data_eapol(wt, bssid, sta_addr, dst, src, data, len, prot);
86 break;
87 case ETH_P_IP:
88 rx_data_ip(wt, bssid, sta_addr, dst, src, data, len,
89 peer_addr);
90 break;
91 case 0x890d:
92 rx_data_80211_encap(wt, bssid, sta_addr, dst, src, data, len);
93 break;
94 case ETH_P_8021Q:
95 rx_data_vlan(wt, bssid, sta_addr, dst, src, data, len, prot,
96 peer_addr);
97 break;
98 }
99 }
100
101
102 static void rx_data_process(struct wlantest *wt, const u8 *bssid,
103 const u8 *sta_addr,
104 const u8 *dst, const u8 *src,
105 const u8 *data, size_t len, int prot,
106 const u8 *peer_addr)
107 {
108 if (len == 0)
109 return;
110
111 if (len >= 8 && os_memcmp(data, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) {
112 rx_data_eth(wt, bssid, sta_addr, dst, src,
113 WPA_GET_BE16(data + 6), data + 8, len - 8, prot,
114 peer_addr);
115 return;
116 }
117
118 wpa_hexdump(MSG_DEBUG, "Unrecognized LLC", data, len > 8 ? 8 : len);
119 }
120
121
122 static u8 * try_all_ptk(struct wlantest *wt, int pairwise_cipher,
123 const struct ieee80211_hdr *hdr,
124 const u8 *data, size_t data_len, size_t *decrypted_len)
125 {
126 struct wlantest_ptk *ptk;
127 u8 *decrypted;
128 int prev_level = wpa_debug_level;
129
130 wpa_debug_level = MSG_WARNING;
131 dl_list_for_each(ptk, &wt->ptk, struct wlantest_ptk, list) {
132 unsigned int tk_len = ptk->ptk_len - 32;
133 decrypted = NULL;
134 if ((pairwise_cipher == WPA_CIPHER_CCMP ||
135 pairwise_cipher == 0) && tk_len == 16) {
136 decrypted = ccmp_decrypt(ptk->ptk.tk, hdr, data,
137 data_len, decrypted_len);
138 } else if ((pairwise_cipher == WPA_CIPHER_CCMP_256 ||
139 pairwise_cipher == 0) && tk_len == 32) {
140 decrypted = ccmp_256_decrypt(ptk->ptk.tk, hdr, data,
141 data_len, decrypted_len);
142 } else if ((pairwise_cipher == WPA_CIPHER_GCMP ||
143 pairwise_cipher == WPA_CIPHER_GCMP_256 ||
144 pairwise_cipher == 0) &&
145 (tk_len == 16 || tk_len == 32)) {
146 decrypted = gcmp_decrypt(ptk->ptk.tk, tk_len, hdr,
147 data, data_len, decrypted_len);
148 } else if ((pairwise_cipher == WPA_CIPHER_TKIP ||
149 pairwise_cipher == 0) && tk_len == 32) {
150 decrypted = tkip_decrypt(ptk->ptk.tk, hdr, data,
151 data_len, decrypted_len);
152 }
153 if (decrypted) {
154 wpa_debug_level = prev_level;
155 add_note(wt, MSG_DEBUG, "Found PTK match from list of all known PTKs");
156 return decrypted;
157 }
158 }
159 wpa_debug_level = prev_level;
160
161 return NULL;
162 }
163
164
165 static void rx_data_bss_prot_group(struct wlantest *wt,
166 const struct ieee80211_hdr *hdr,
167 size_t hdrlen,
168 const u8 *qos, const u8 *dst, const u8 *src,
169 const u8 *data, size_t len)
170 {
171 struct wlantest_bss *bss;
172 int keyid;
173 u8 *decrypted = NULL;
174 size_t dlen;
175 u8 pn[6];
176 int replay = 0;
177
178 bss = bss_get(wt, hdr->addr2);
179 if (bss == NULL)
180 return;
181 if (len < 4) {
182 add_note(wt, MSG_INFO, "Too short group addressed data frame");
183 return;
184 }
185
186 if (bss->group_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
187 !(data[3] & 0x20)) {
188 add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
189 MACSTR " did not have ExtIV bit set to 1",
190 MAC2STR(bss->bssid));
191 return;
192 }
193
194 if (bss->group_cipher == WPA_CIPHER_TKIP) {
195 if (data[3] & 0x1f) {
196 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
197 " used non-zero reserved bit",
198 MAC2STR(bss->bssid));
199 }
200 if (data[1] != ((data[0] | 0x20) & 0x7f)) {
201 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
202 " used incorrect WEPSeed[1] (was 0x%x, "
203 "expected 0x%x)",
204 MAC2STR(bss->bssid), data[1],
205 (data[0] | 0x20) & 0x7f);
206 }
207 } else if (bss->group_cipher == WPA_CIPHER_CCMP) {
208 if (data[2] != 0 || (data[3] & 0x1f) != 0) {
209 add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
210 " used non-zero reserved bit",
211 MAC2STR(bss->bssid));
212 }
213 }
214
215 keyid = data[3] >> 6;
216 if (bss->gtk_len[keyid] == 0 && bss->group_cipher != WPA_CIPHER_WEP40)
217 {
218 add_note(wt, MSG_MSGDUMP, "No GTK known to decrypt the frame "
219 "(A2=" MACSTR " KeyID=%d)",
220 MAC2STR(hdr->addr2), keyid);
221 return;
222 }
223
224 if (bss->group_cipher == WPA_CIPHER_TKIP)
225 tkip_get_pn(pn, data);
226 else if (bss->group_cipher == WPA_CIPHER_WEP40)
227 goto skip_replay_det;
228 else
229 ccmp_get_pn(pn, data);
230 if (os_memcmp(pn, bss->rsc[keyid], 6) <= 0) {
231 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
232 add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
233 " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s",
234 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
235 MAC2STR(hdr->addr3),
236 WLAN_GET_SEQ_SEQ(seq_ctrl),
237 WLAN_GET_SEQ_FRAG(seq_ctrl),
238 (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
239 " Retry" : "");
240 wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
241 wpa_hexdump(MSG_INFO, "RSC", bss->rsc[keyid], 6);
242 replay = 1;
243 }
244
245 skip_replay_det:
246 if (bss->group_cipher == WPA_CIPHER_TKIP)
247 decrypted = tkip_decrypt(bss->gtk[keyid], hdr, data, len,
248 &dlen);
249 else if (bss->group_cipher == WPA_CIPHER_WEP40)
250 decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
251 else if (bss->group_cipher == WPA_CIPHER_CCMP)
252 decrypted = ccmp_decrypt(bss->gtk[keyid], hdr, data, len,
253 &dlen);
254 else if (bss->group_cipher == WPA_CIPHER_CCMP_256)
255 decrypted = ccmp_256_decrypt(bss->gtk[keyid], hdr, data, len,
256 &dlen);
257 else if (bss->group_cipher == WPA_CIPHER_GCMP ||
258 bss->group_cipher == WPA_CIPHER_GCMP_256)
259 decrypted = gcmp_decrypt(bss->gtk[keyid], bss->gtk_len[keyid],
260 hdr, data, len, &dlen);
261
262 if (decrypted) {
263 rx_data_process(wt, bss->bssid, NULL, dst, src, decrypted,
264 dlen, 1, NULL);
265 if (!replay)
266 os_memcpy(bss->rsc[keyid], pn, 6);
267 write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
268 decrypted, dlen);
269 } else
270 add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
271 os_free(decrypted);
272 }
273
274
275 static void rx_data_bss_prot(struct wlantest *wt,
276 const struct ieee80211_hdr *hdr, size_t hdrlen,
277 const u8 *qos, const u8 *dst, const u8 *src,
278 const u8 *data, size_t len)
279 {
280 struct wlantest_bss *bss, *bss2;
281 struct wlantest_sta *sta, *sta2;
282 int keyid;
283 u16 fc = le_to_host16(hdr->frame_control);
284 u8 *decrypted;
285 size_t dlen;
286 int tid;
287 u8 pn[6], *rsc;
288 struct wlantest_tdls *tdls = NULL, *found;
289 const u8 *tk = NULL;
290 int ptk_iter_done = 0;
291 int try_ptk_iter = 0;
292 int replay = 0;
293
294 if (hdr->addr1[0] & 0x01) {
295 rx_data_bss_prot_group(wt, hdr, hdrlen, qos, dst, src,
296 data, len);
297 return;
298 }
299
300 if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
301 (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
302 bss = bss_find(wt, hdr->addr1);
303 if (bss) {
304 sta = sta_find(bss, hdr->addr2);
305 if (sta) {
306 sta->counters[
307 WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
308 }
309 if (!sta || !sta->ptk_set) {
310 bss2 = bss_find(wt, hdr->addr2);
311 if (bss2) {
312 sta2 = sta_find(bss2, hdr->addr1);
313 if (sta2 && (!sta || sta2->ptk_set)) {
314 bss = bss2;
315 sta = sta2;
316 }
317 }
318 }
319 } else {
320 bss = bss_find(wt, hdr->addr2);
321 if (!bss)
322 return;
323 sta = sta_find(bss, hdr->addr1);
324 }
325 } else if (fc & WLAN_FC_TODS) {
326 bss = bss_get(wt, hdr->addr1);
327 if (bss == NULL)
328 return;
329 sta = sta_get(bss, hdr->addr2);
330 if (sta)
331 sta->counters[WLANTEST_STA_COUNTER_PROT_DATA_TX]++;
332 } else if (fc & WLAN_FC_FROMDS) {
333 bss = bss_get(wt, hdr->addr2);
334 if (bss == NULL)
335 return;
336 sta = sta_get(bss, hdr->addr1);
337 } else {
338 bss = bss_get(wt, hdr->addr3);
339 if (bss == NULL)
340 return;
341 sta = sta_find(bss, hdr->addr2);
342 sta2 = sta_find(bss, hdr->addr1);
343 if (sta == NULL || sta2 == NULL)
344 return;
345 found = NULL;
346 dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list)
347 {
348 if ((tdls->init == sta && tdls->resp == sta2) ||
349 (tdls->init == sta2 && tdls->resp == sta)) {
350 found = tdls;
351 if (tdls->link_up)
352 break;
353 }
354 }
355 if (found) {
356 if (!found->link_up)
357 add_note(wt, MSG_DEBUG,
358 "TDLS: Link not up, but Data "
359 "frame seen");
360 tk = found->tpk.tk;
361 tdls = found;
362 }
363 }
364 if ((sta == NULL ||
365 (!sta->ptk_set && sta->pairwise_cipher != WPA_CIPHER_WEP40)) &&
366 tk == NULL) {
367 add_note(wt, MSG_MSGDUMP, "No PTK known to decrypt the frame");
368 if (dl_list_empty(&wt->ptk))
369 return;
370 try_ptk_iter = 1;
371 }
372
373 if (len < 4) {
374 add_note(wt, MSG_INFO, "Too short encrypted data frame");
375 return;
376 }
377
378 if (sta == NULL)
379 return;
380 if (sta->pairwise_cipher & (WPA_CIPHER_TKIP | WPA_CIPHER_CCMP) &&
381 !(data[3] & 0x20)) {
382 add_note(wt, MSG_INFO, "Expected TKIP/CCMP frame from "
383 MACSTR " did not have ExtIV bit set to 1",
384 MAC2STR(src));
385 return;
386 }
387
388 if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP) {
389 if (data[3] & 0x1f) {
390 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
391 " used non-zero reserved bit",
392 MAC2STR(hdr->addr2));
393 }
394 if (data[1] != ((data[0] | 0x20) & 0x7f)) {
395 add_note(wt, MSG_INFO, "TKIP frame from " MACSTR
396 " used incorrect WEPSeed[1] (was 0x%x, "
397 "expected 0x%x)",
398 MAC2STR(hdr->addr2), data[1],
399 (data[0] | 0x20) & 0x7f);
400 }
401 } else if (tk || sta->pairwise_cipher == WPA_CIPHER_CCMP) {
402 if (data[2] != 0 || (data[3] & 0x1f) != 0) {
403 add_note(wt, MSG_INFO, "CCMP frame from " MACSTR
404 " used non-zero reserved bit",
405 MAC2STR(hdr->addr2));
406 }
407 }
408
409 keyid = data[3] >> 6;
410 if (keyid != 0) {
411 add_note(wt, MSG_INFO, "Unexpected non-zero KeyID %d in "
412 "individually addressed Data frame from " MACSTR,
413 keyid, MAC2STR(hdr->addr2));
414 }
415
416 if (qos) {
417 tid = qos[0] & 0x0f;
418 if (fc & WLAN_FC_TODS)
419 sta->tx_tid[tid]++;
420 else
421 sta->rx_tid[tid]++;
422 } else {
423 tid = 0;
424 if (fc & WLAN_FC_TODS)
425 sta->tx_tid[16]++;
426 else
427 sta->rx_tid[16]++;
428 }
429 if (tk) {
430 if (os_memcmp(hdr->addr2, tdls->init->addr, ETH_ALEN) == 0)
431 rsc = tdls->rsc_init[tid];
432 else
433 rsc = tdls->rsc_resp[tid];
434 } else if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
435 (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
436 if (os_memcmp(sta->addr, hdr->addr2, ETH_ALEN) == 0)
437 rsc = sta->rsc_tods[tid];
438 else
439 rsc = sta->rsc_fromds[tid];
440 } else if (fc & WLAN_FC_TODS)
441 rsc = sta->rsc_tods[tid];
442 else
443 rsc = sta->rsc_fromds[tid];
444
445
446 if (tk == NULL && sta->pairwise_cipher == WPA_CIPHER_TKIP)
447 tkip_get_pn(pn, data);
448 else if (sta->pairwise_cipher == WPA_CIPHER_WEP40)
449 goto skip_replay_det;
450 else
451 ccmp_get_pn(pn, data);
452 if (os_memcmp(pn, rsc, 6) <= 0) {
453 u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
454 add_note(wt, MSG_INFO, "CCMP/TKIP replay detected: A1=" MACSTR
455 " A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u%s",
456 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
457 MAC2STR(hdr->addr3),
458 WLAN_GET_SEQ_SEQ(seq_ctrl),
459 WLAN_GET_SEQ_FRAG(seq_ctrl),
460 (le_to_host16(hdr->frame_control) & WLAN_FC_RETRY) ?
461 " Retry" : "");
462 wpa_hexdump(MSG_INFO, "RX PN", pn, 6);
463 wpa_hexdump(MSG_INFO, "RSC", rsc, 6);
464 replay = 1;
465 }
466
467 skip_replay_det:
468 if (tk) {
469 if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256)
470 decrypted = ccmp_256_decrypt(tk, hdr, data, len, &dlen);
471 else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
472 sta->pairwise_cipher == WPA_CIPHER_GCMP_256)
473 decrypted = gcmp_decrypt(tk, sta->ptk.tk_len, hdr, data,
474 len, &dlen);
475 else
476 decrypted = ccmp_decrypt(tk, hdr, data, len, &dlen);
477 } else if (sta->pairwise_cipher == WPA_CIPHER_TKIP) {
478 decrypted = tkip_decrypt(sta->ptk.tk, hdr, data, len, &dlen);
479 } else if (sta->pairwise_cipher == WPA_CIPHER_WEP40) {
480 decrypted = wep_decrypt(wt, hdr, data, len, &dlen);
481 } else if (sta->ptk_set) {
482 if (sta->pairwise_cipher == WPA_CIPHER_CCMP_256)
483 decrypted = ccmp_256_decrypt(sta->ptk.tk, hdr, data,
484 len, &dlen);
485 else if (sta->pairwise_cipher == WPA_CIPHER_GCMP ||
486 sta->pairwise_cipher == WPA_CIPHER_GCMP_256)
487 decrypted = gcmp_decrypt(sta->ptk.tk, sta->ptk.tk_len,
488 hdr, data, len, &dlen);
489 else
490 decrypted = ccmp_decrypt(sta->ptk.tk, hdr, data, len,
491 &dlen);
492 } else {
493 decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, data,
494 len, &dlen);
495 ptk_iter_done = 1;
496 }
497 if (!decrypted && !ptk_iter_done) {
498 decrypted = try_all_ptk(wt, sta->pairwise_cipher, hdr, data,
499 len, &dlen);
500 if (decrypted) {
501 add_note(wt, MSG_DEBUG, "Current PTK did not work, but found a match from all known PTKs");
502 }
503 }
504 if (decrypted) {
505 u16 fc = le_to_host16(hdr->frame_control);
506 const u8 *peer_addr = NULL;
507 if (!(fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)))
508 peer_addr = hdr->addr1;
509 if (!replay)
510 os_memcpy(rsc, pn, 6);
511 rx_data_process(wt, bss->bssid, sta->addr, dst, src, decrypted,
512 dlen, 1, peer_addr);
513 write_pcap_decrypted(wt, (const u8 *) hdr, hdrlen,
514 decrypted, dlen);
515 } else {
516 if (!try_ptk_iter)
517 add_note(wt, MSG_DEBUG, "Failed to decrypt frame");
518
519 /* Assume the frame was corrupted and there was no FCS to check.
520 * Allow retry of this particular frame to be processed so that
521 * it could end up getting decrypted if it was received without
522 * corruption. */
523 sta->allow_duplicate = 1;
524 }
525 os_free(decrypted);
526 }
527
528
529 static void rx_data_bss(struct wlantest *wt, const struct ieee80211_hdr *hdr,
530 size_t hdrlen, const u8 *qos, const u8 *dst,
531 const u8 *src, const u8 *data, size_t len)
532 {
533 u16 fc = le_to_host16(hdr->frame_control);
534 int prot = !!(fc & WLAN_FC_ISWEP);
535
536 if (qos) {
537 u8 ack = (qos[0] & 0x60) >> 5;
538 wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
539 " len=%u%s tid=%u%s%s",
540 MAC2STR(src), MAC2STR(dst), (unsigned int) len,
541 prot ? " Prot" : "", qos[0] & 0x0f,
542 (qos[0] & 0x10) ? " EOSP" : "",
543 ack == 0 ? "" :
544 (ack == 1 ? " NoAck" :
545 (ack == 2 ? " NoExpAck" : " BA")));
546 } else {
547 wpa_printf(MSG_MSGDUMP, "BSS DATA: " MACSTR " -> " MACSTR
548 " len=%u%s",
549 MAC2STR(src), MAC2STR(dst), (unsigned int) len,
550 prot ? " Prot" : "");
551 }
552
553 if (prot)
554 rx_data_bss_prot(wt, hdr, hdrlen, qos, dst, src, data, len);
555 else {
556 const u8 *bssid, *sta_addr, *peer_addr;
557 struct wlantest_bss *bss;
558
559 if (fc & WLAN_FC_TODS) {
560 bssid = hdr->addr1;
561 sta_addr = hdr->addr2;
562 peer_addr = NULL;
563 } else if (fc & WLAN_FC_FROMDS) {
564 bssid = hdr->addr2;
565 sta_addr = hdr->addr1;
566 peer_addr = NULL;
567 } else {
568 bssid = hdr->addr3;
569 sta_addr = hdr->addr2;
570 peer_addr = hdr->addr1;
571 }
572
573 bss = bss_get(wt, bssid);
574 if (bss) {
575 struct wlantest_sta *sta = sta_get(bss, sta_addr);
576
577 if (sta) {
578 if (qos) {
579 int tid = qos[0] & 0x0f;
580 if (fc & WLAN_FC_TODS)
581 sta->tx_tid[tid]++;
582 else
583 sta->rx_tid[tid]++;
584 } else {
585 if (fc & WLAN_FC_TODS)
586 sta->tx_tid[16]++;
587 else
588 sta->rx_tid[16]++;
589 }
590 }
591 }
592
593 rx_data_process(wt, bssid, sta_addr, dst, src, data, len, 0,
594 peer_addr);
595 }
596 }
597
598
599 static struct wlantest_tdls * get_tdls(struct wlantest *wt, const u8 *bssid,
600 const u8 *sta1_addr,
601 const u8 *sta2_addr)
602 {
603 struct wlantest_bss *bss;
604 struct wlantest_sta *sta1, *sta2;
605 struct wlantest_tdls *tdls, *found = NULL;
606
607 bss = bss_find(wt, bssid);
608 if (bss == NULL)
609 return NULL;
610 sta1 = sta_find(bss, sta1_addr);
611 if (sta1 == NULL)
612 return NULL;
613 sta2 = sta_find(bss, sta2_addr);
614 if (sta2 == NULL)
615 return NULL;
616
617 dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
618 if ((tdls->init == sta1 && tdls->resp == sta2) ||
619 (tdls->init == sta2 && tdls->resp == sta1)) {
620 found = tdls;
621 if (tdls->link_up)
622 break;
623 }
624 }
625
626 return found;
627 }
628
629
630 static void add_direct_link(struct wlantest *wt, const u8 *bssid,
631 const u8 *sta1_addr, const u8 *sta2_addr)
632 {
633 struct wlantest_tdls *tdls;
634
635 tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
636 if (tdls == NULL)
637 return;
638
639 if (tdls->link_up)
640 tdls->counters[WLANTEST_TDLS_COUNTER_VALID_DIRECT_LINK]++;
641 else
642 tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_DIRECT_LINK]++;
643 }
644
645
646 static void add_ap_path(struct wlantest *wt, const u8 *bssid,
647 const u8 *sta1_addr, const u8 *sta2_addr)
648 {
649 struct wlantest_tdls *tdls;
650
651 tdls = get_tdls(wt, bssid, sta1_addr, sta2_addr);
652 if (tdls == NULL)
653 return;
654
655 if (tdls->link_up)
656 tdls->counters[WLANTEST_TDLS_COUNTER_INVALID_AP_PATH]++;
657 else
658 tdls->counters[WLANTEST_TDLS_COUNTER_VALID_AP_PATH]++;
659 }
660
661
662 void rx_data(struct wlantest *wt, const u8 *data, size_t len)
663 {
664 const struct ieee80211_hdr *hdr;
665 u16 fc, stype;
666 size_t hdrlen;
667 const u8 *qos = NULL;
668
669 if (len < 24)
670 return;
671
672 hdr = (const struct ieee80211_hdr *) data;
673 fc = le_to_host16(hdr->frame_control);
674 stype = WLAN_FC_GET_STYPE(fc);
675 hdrlen = 24;
676 if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
677 (WLAN_FC_TODS | WLAN_FC_FROMDS))
678 hdrlen += ETH_ALEN;
679 if (stype & 0x08) {
680 qos = data + hdrlen;
681 hdrlen += 2;
682 }
683 if (len < hdrlen)
684 return;
685 wt->rx_data++;
686
687 switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
688 case 0:
689 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s IBSS DA=" MACSTR " SA="
690 MACSTR " BSSID=" MACSTR,
691 data_stype(WLAN_FC_GET_STYPE(fc)),
692 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
693 fc & WLAN_FC_ISWEP ? " Prot" : "",
694 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
695 MAC2STR(hdr->addr3));
696 add_direct_link(wt, hdr->addr3, hdr->addr1, hdr->addr2);
697 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
698 data + hdrlen, len - hdrlen);
699 break;
700 case WLAN_FC_FROMDS:
701 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s FromDS DA=" MACSTR
702 " BSSID=" MACSTR " SA=" MACSTR,
703 data_stype(WLAN_FC_GET_STYPE(fc)),
704 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
705 fc & WLAN_FC_ISWEP ? " Prot" : "",
706 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
707 MAC2STR(hdr->addr3));
708 add_ap_path(wt, hdr->addr2, hdr->addr1, hdr->addr3);
709 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr3,
710 data + hdrlen, len - hdrlen);
711 break;
712 case WLAN_FC_TODS:
713 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s ToDS BSSID=" MACSTR
714 " SA=" MACSTR " DA=" MACSTR,
715 data_stype(WLAN_FC_GET_STYPE(fc)),
716 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
717 fc & WLAN_FC_ISWEP ? " Prot" : "",
718 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
719 MAC2STR(hdr->addr3));
720 add_ap_path(wt, hdr->addr1, hdr->addr3, hdr->addr2);
721 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr3, hdr->addr2,
722 data + hdrlen, len - hdrlen);
723 break;
724 case WLAN_FC_TODS | WLAN_FC_FROMDS:
725 wpa_printf(MSG_EXCESSIVE, "DATA %s%s%s WDS RA=" MACSTR " TA="
726 MACSTR " DA=" MACSTR " SA=" MACSTR,
727 data_stype(WLAN_FC_GET_STYPE(fc)),
728 fc & WLAN_FC_PWRMGT ? " PwrMgt" : "",
729 fc & WLAN_FC_ISWEP ? " Prot" : "",
730 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
731 MAC2STR(hdr->addr3),
732 MAC2STR((const u8 *) (hdr + 1)));
733 rx_data_bss(wt, hdr, hdrlen, qos, hdr->addr1, hdr->addr2,
734 data + hdrlen, len - hdrlen);
735 break;
736 }
737 }